jgtrosh

chromab.c

Nov 16th, 2014
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <gd.h>
  2.  
  3. typedef struct {
  4.     int rx, ry, gx, gy, bx, by;
  5. } coloffset;
  6.  
  7. /* expect im and out to have the same dimensions */
  8. void chromab(gdImagePtr im, gdImagePtr out, int frm,
  9.              coloffset offsetfunc(int, int, int)) {
  10.     int x, y, colin, colout, coldest, black, sx, sy,
  11.         r, g, b, rx, ry, gx, gy, bx, by;
  12.     coloffset co;
  13.  
  14.     black = gdImageColorResolve(out, 0, 0, 0);
  15.     gdImageFilledRectangle(out, 0, 0, out->sx, out->sy, black);
  16.     for (y=0; y<im->sy; y++)
  17.         for (x=0; x<im->sx; x++) {
  18.             co = offsetfunc(frm, x, y);
  19.             rx = co.rx; ry = co.ry;
  20.             gx = co.gx; gy = co.gy;
  21.             bx = co.bx; by = co.by;
  22.             colin = gdImageGetPixel(im, x, y);
  23.             /* red */
  24.             coldest = gdImageGetPixel(out, x+rx, y+ry);
  25.             r = gdImageRed  (out, coldest) + gdImageRed(im, colin);
  26.             g = gdImageGreen(out, coldest);
  27.             b = gdImageBlue (out, coldest);
  28.             r = r<255?r:255; g = g<255?g:255; b = b<255?b:255;
  29.             colout = gdImageColorResolve(out, r, g, b);
  30.             gdImageSetPixel(out, x+rx, y+ry, colout);
  31.             gdImageColorDeallocate(out, colout);
  32.             /* green */
  33.             coldest = gdImageGetPixel(out, x+gx, y+gy);
  34.             r = gdImageRed  (out, coldest);
  35.             g = gdImageGreen(out, coldest) + gdImageGreen(im, colin);
  36.             b = gdImageBlue (out, coldest);
  37.             r = r<255?r:255; g = g<255?g:255; b = b<255?b:255;
  38.             colout = gdImageColorResolve(out, r, g, b);
  39.             gdImageSetPixel(out, x+gx, y+gy, colout);
  40.             gdImageColorDeallocate(out, colout);
  41.             /* blue */
  42.             coldest = gdImageGetPixel(out, x+bx, y+by);
  43.             r = gdImageRed  (out, coldest);
  44.             g = gdImageGreen(out, coldest);
  45.             b = gdImageBlue (out, coldest) + gdImageBlue(im, colin);
  46.             r = r<255?r:255; g = g<255?g:255; b = b<255?b:255;
  47.             colout = gdImageColorResolve(out, r, g, b);
  48.             gdImageSetPixel(out, x+bx, y+by, colout);
  49.             gdImageColorDeallocate(out, colout);
  50.         }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment