Changeset 2738


Ignore:
Timestamp:
08/20/08 03:38:51 (5 years ago)
Author:
sam
Message:
  • img2rubik.c: add quick and dirty dithering code to img2rubik.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libpipi/trunk/examples/img2rubik.c

    r2736 r2738  
    1313    static double const mypal[] = 
    1414    { 
    15         0.900, 0.001, 0.001, /* red */ 
    16         0.001, 0.800, 0.001, /* green */ 
    17         0.005, 0.001, 0.500, /* blue */ 
     15#if 1 
     16        0.800, 0.001, 0.001, /* red */ 
     17        0.001, 0.700, 0.001, /* green */ 
     18        0.005, 0.001, 0.600, /* blue */ 
    1819        0.900, 0.900, 0.900, /* white */ 
    1920        0.900, 0.900, 0.001, /* yellow */ 
    20         0.800, 0.400, 0.001, /* orange */ 
     21        0.850, 0.450, 0.001, /* orange */ 
     22#else 
     23        1,1,1, 
     24        0,.05,.60, 
     25        1,0,0, 
     26        .15,.76,0, 
     27        .96,1,0, 
     28        1,.61,0, 
     29#endif 
    2130    }; 
     31#define NCOLORS ((int)(sizeof(mypal)/sizeof(*mypal)/3)) 
     32 
     33    int x, y, w, h; 
    2234 
    2335    pipi_image_t *src = pipi_load(argv[1]); 
    24     pipi_image_t *dst = pipi_reduce(src, 6, mypal); 
     36    pipi_image_t *dst = pipi_reduce(src, NCOLORS, mypal); 
     37 
     38    pipi_pixels_t *p = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); 
     39    float *data = (float *)p->pixels; 
     40    w = p->w; 
     41    h = p->h; 
     42 
     43    for(y = 0; y < h; y++) 
     44        for(x = 0; x < w; x++) 
     45        { 
     46            double pr, pg, pb, qr, qg, qb, er, eg, eb; 
     47 
     48            pr = data[4 * (y * w + x) + 2]; 
     49            pg = data[4 * (y * w + x) + 1]; 
     50            pb = data[4 * (y * w + x)]; 
     51 
     52            double dist = 10.0; 
     53            int n, best = -1; 
     54 
     55            for(n = 0; n < NCOLORS; n++) 
     56            { 
     57                double dr, dg, db, d; 
     58 
     59                dr = pr - mypal[n * 3]; 
     60                dg = pg - mypal[n * 3 + 1]; 
     61                db = pb - mypal[n * 3 + 2]; 
     62 
     63                d = dr * dr + dg * dg + db * db; 
     64                //d = 0.299 * dr * dr + 0.587 * dg * dg + 0.114 * db * db; 
     65 
     66                if(d < dist) 
     67                { 
     68                    best = n; 
     69                    dist = d; 
     70                } 
     71            } 
     72 
     73            qr = mypal[best * 3]; 
     74            qg = mypal[best * 3 + 1]; 
     75            qb = mypal[best * 3 + 2]; 
     76 
     77            er = (pr - qr) / 16; 
     78            eg = (pg - qg) / 16; 
     79            eb = (pb - qb) / 16; 
     80 
     81            if(x < w - 1) 
     82            { 
     83                data[4 * (y * w + x + 1) + 2] += 7 * er; 
     84                data[4 * (y * w + x + 1) + 1] += 7 * eg; 
     85                data[4 * (y * w + x + 1)] += 7 * eb; 
     86            } 
     87 
     88            if(y < h - 1) 
     89            { 
     90                if(x < w - 1) 
     91                { 
     92                    data[4 * (y * w + x + w + 1) + 2] += 1 * er; 
     93                    data[4 * (y * w + x + w + 1) + 1] += 1 * eg; 
     94                    data[4 * (y * w + x + w + 1)] += 1 * eb; 
     95                } 
     96 
     97                data[4 * (y * w + x + w) + 2] += 5 * er; 
     98                data[4 * (y * w + x + w) + 1] += 5 * eg; 
     99                data[4 * (y * w + x + w)] += 5 * eb; 
     100 
     101                if(x > 0) 
     102                { 
     103                    data[4 * (y * w + x + w - 1) + 2] += 3 * er; 
     104                    data[4 * (y * w + x + w - 1) + 1] += 3 * eg; 
     105                    data[4 * (y * w + x + w - 1)] += 3 * eb; 
     106                } 
     107            } 
     108 
     109            data[4 * (y * w + x) + 2] = qr; 
     110            data[4 * (y * w + x) + 1] = qg; 
     111            data[4 * (y * w + x)] = qb; 
     112        } 
     113 
    25114    pipi_save(dst, argv[2]); 
    26115 
Note: See TracChangeset for help on using the changeset viewer.