Browse Source

* img2rubik.c: add quick and dirty dithering code to img2rubik.

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2738 92316355-f0b4-4df1-b90c-862c8a59935f
remotes/tiles
sam 16 years ago
parent
commit
db43a4df0d
1 changed files with 94 additions and 5 deletions
  1. +94
    -5
      examples/img2rubik.c

+ 94
- 5
examples/img2rubik.c View File

@@ -12,16 +12,105 @@ int main(int argc, char *argv[])
{
static double const mypal[] =
{
0.900, 0.001, 0.001, /* red */
0.001, 0.800, 0.001, /* green */
0.005, 0.001, 0.500, /* blue */
#if 1
0.800, 0.001, 0.001, /* red */
0.001, 0.700, 0.001, /* green */
0.005, 0.001, 0.600, /* blue */
0.900, 0.900, 0.900, /* white */
0.900, 0.900, 0.001, /* yellow */
0.800, 0.400, 0.001, /* orange */
0.850, 0.450, 0.001, /* orange */
#else
1,1,1,
0,.05,.60,
1,0,0,
.15,.76,0,
.96,1,0,
1,.61,0,
#endif
};
#define NCOLORS ((int)(sizeof(mypal)/sizeof(*mypal)/3))

int x, y, w, h;

pipi_image_t *src = pipi_load(argv[1]);
pipi_image_t *dst = pipi_reduce(src, 6, mypal);
pipi_image_t *dst = pipi_reduce(src, NCOLORS, mypal);

pipi_pixels_t *p = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
float *data = (float *)p->pixels;
w = p->w;
h = p->h;

for(y = 0; y < h; y++)
for(x = 0; x < w; x++)
{
double pr, pg, pb, qr, qg, qb, er, eg, eb;

pr = data[4 * (y * w + x) + 2];
pg = data[4 * (y * w + x) + 1];
pb = data[4 * (y * w + x)];

double dist = 10.0;
int n, best = -1;

for(n = 0; n < NCOLORS; n++)
{
double dr, dg, db, d;

dr = pr - mypal[n * 3];
dg = pg - mypal[n * 3 + 1];
db = pb - mypal[n * 3 + 2];

d = dr * dr + dg * dg + db * db;
//d = 0.299 * dr * dr + 0.587 * dg * dg + 0.114 * db * db;

if(d < dist)
{
best = n;
dist = d;
}
}

qr = mypal[best * 3];
qg = mypal[best * 3 + 1];
qb = mypal[best * 3 + 2];

er = (pr - qr) / 16;
eg = (pg - qg) / 16;
eb = (pb - qb) / 16;

if(x < w - 1)
{
data[4 * (y * w + x + 1) + 2] += 7 * er;
data[4 * (y * w + x + 1) + 1] += 7 * eg;
data[4 * (y * w + x + 1)] += 7 * eb;
}

if(y < h - 1)
{
if(x < w - 1)
{
data[4 * (y * w + x + w + 1) + 2] += 1 * er;
data[4 * (y * w + x + w + 1) + 1] += 1 * eg;
data[4 * (y * w + x + w + 1)] += 1 * eb;
}

data[4 * (y * w + x + w) + 2] += 5 * er;
data[4 * (y * w + x + w) + 1] += 5 * eg;
data[4 * (y * w + x + w)] += 5 * eb;

if(x > 0)
{
data[4 * (y * w + x + w - 1) + 2] += 3 * er;
data[4 * (y * w + x + w - 1) + 1] += 3 * eg;
data[4 * (y * w + x + w - 1)] += 3 * eb;
}
}

data[4 * (y * w + x) + 2] = qr;
data[4 * (y * w + x) + 1] = qg;
data[4 * (y * w + x)] = qb;
}

pipi_save(dst, argv[2]);

return 0;


Loading…
Cancel
Save