Created
November 26, 2013 18:22
-
-
Save mmalex/7663290 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sillycocg.cpp : disgusting test code. loads a 320x180 rgb image from input.raw | |
// writes out a (fixed) photoshop-style 3 bytes per index palette file to ycocg.act | |
// you can load this in photoshop via image/mode/index then image/mode/color-table.../load... | |
// then it writes out an 8bpp indexed image to output.raw | |
// its kinda a hindsight is 2020 thing, the old 'fake truecolor' demos that did RGB stripes/ | |
// patterns - I wondered what it would be like to do something like | |
// http://www.pmavridis.com/research/fbcompression/ | |
// but 15 years ago in 256 color mode, so you compute 2 channels per pixel instead of 3, | |
// and those channels are Y and one of Co or Cg; then you alternate Co/Cg in a checkerboard | |
// and pack 4 bits y, 3 bits chroma, 1 bit checkerboard switch. | |
// it's kinda meh, but if I'd thought of it 15 years ago, I would definitely have made a | |
// cool demo with it. alas, I'm a bit late :) | |
#include "stdafx.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef unsigned char u8; | |
int clamp(int i) { if (i<0) return 0; if (i>255) return 255; return i; } | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
u8 input[180][320][3]; | |
FILE *f=fopen("c:/input.raw","rb"); | |
fread(input,320,180*3,f); | |
fclose(f); | |
f=fopen("c:/ycocg.act","wb"); | |
for (int y=0;y<16;++y) | |
for (int c=0;c<8;++c) | |
for (int p=0;p<2;++p) { | |
int Cg = p ? 0 : (c*32+16-128),Co = p ? (c*32+16-128) : 0,Y=y*16+8; | |
int R=clamp(Y + Co-Cg),G=clamp(Y + Cg),B=clamp(Y - Co-Cg); | |
fwrite(&R,1,1,f); fwrite(&G,1,1,f); fwrite(&B,1,1,f); | |
} | |
fclose(f); | |
f=fopen("c:/output.raw","wb"); | |
for (int y=0;y<20;++y) | |
for (int x=0;x<320;++x) { | |
int i=x; if (i>255) i=0;if (y>10) i=0;fwrite(&i,1,1,f); | |
} | |
for (int y=0;y<180;++y) | |
for (int x=0;x<320;++x) { | |
int R=input[y][x][0],G=input[y][x][1],B=input[y][x][2]; | |
int Y=int(0.5+0.25*R+0.5*G+0.25*B),Co=int(0.5+0.5*R-0.5*B),Cg=int(0.5+-0.25*R+0.5*G-0.25*B); | |
Co*=2; Cg*=2; | |
Y=clamp(Y+(rand()&15))/16; | |
Co=clamp(Co+128+(rand()&31))/32; | |
Cg=clamp(Cg+128+(rand()&31))/32; | |
int ch=(x^y)&1; | |
if (!ch) Co=Cg; | |
int i=(Y<<4)+(Co<<1)+ch; | |
fwrite(&i,1,1,f); | |
} | |
fclose(f); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment