/* sRGB.cpp * Daniel Scharstein 2/11/09 * applies inverse sRGB transform to (hopefully) linearize an image */ static char *usage = "\n usage: %s [-quiet] sRGB.png linear.png\n"; #include #include #include "imageLib.h" // convert from sRGB to linear // see http://en.wikipedia.org/wiki/SRGB void inv_sRGB(CByteImage im) { CShape sh = im.Shape(); int width = sh.width, height = sh.height; int ncols = __min(sh.nBands, 3); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { uchar *p = &im.Pixel(x, y, 0); for (int b = 0; b < ncols; b++) { double v = p[b] / 255.0; if (v <= 0.04045) v /= 12.92; else v = pow((v + 0.055) / 1.055, 2.4); int n = (int)( 255.0 * v + 0.5); if (n < 0) throw CError("n < 0"); if (n > 255) throw CError("n > 255"); p[b] = n; } } } } int main(int argc, char *argv[]) { try { int verbose = 1; int argn = 1; if (argc > 1 && argv[1][0]=='-' && argv[1][1]=='q') { verbose = 0; argn++; } if (argn == argc-2) { CByteImage im; ReadImageVerb (im, argv[argn++], verbose); inv_sRGB(im); WriteImageVerb(im, argv[argn++], verbose); } else throw CError(usage, argv[0]); } catch (CError &err) { fprintf(stderr, err.message); fprintf(stderr, "\n"); return -1; } return 0; }