diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-03-20 04:06:39 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-03-20 04:06:39 +0000 |
commit | 047d3248896d375a8fbc80dbbf573b81a3e5a927 (patch) | |
tree | cdfdeebf0477ac62711683a5c267235f0a286ecd /libdimension-png/png.c | |
parent | 5ac6158f8ff999d4db18fb805c02b5c733e75ddb (diff) | |
download | dimension-047d3248896d375a8fbc80dbbf573b81a3e5a927.tar.xz |
Revamp color handling.
Diffstat (limited to 'libdimension-png/png.c')
-rw-r--r-- | libdimension-png/png.c | 88 |
1 files changed, 60 insertions, 28 deletions
diff --git a/libdimension-png/png.c b/libdimension-png/png.c index 750b5af..dbcd21a 100644 --- a/libdimension-png/png.c +++ b/libdimension-png/png.c @@ -32,7 +32,8 @@ dmnsn_png_write_canvas(const dmnsn_canvas *canvas, FILE *file) png_uint_32 width, height; unsigned int x, y; uint16_t *row = NULL; - dmnsn_pixel *pixel; + dmnsn_color *pixel; + dmnsn_sRGB sRGB; width = canvas->x; height = canvas->y; @@ -80,16 +81,27 @@ dmnsn_png_write_canvas(const dmnsn_canvas *canvas, FILE *file) for (x = 0; x < width; ++x) { /* Invert the rows. PNG coordinates are fourth quadrant. */ pixel = canvas->pixels + (height - y - 1)*width + x; + sRGB = dmnsn_sRGB_from_color(*pixel); - row[4*x] = pixel->r; - row[4*x + 1] = pixel->g; - row[4*x + 2] = pixel->b; + if (sRGB.R < 1.0) { + row[4*x] = sRGB.R*UINT16_MAX; + } else { + row[4*x] = UINT16_MAX; + } - if (pixel->a > pixel->t) { - row[4*x + 3] = pixel->a; + if (sRGB.G < 1.0) { + row[4*x + 1] = sRGB.G*UINT16_MAX; + } else { + row[4*x + 1] = UINT16_MAX; + } + + if (sRGB.B < 1.0) { + row[4*x + 2] = sRGB.B*UINT16_MAX; } else { - row[4*x + 3] = pixel->t; + row[4*x + 2] = UINT16_MAX; } + + row[4*x + 3] = (pixel->filter + pixel->trans)*UINT16_MAX; } png_write_row(png_ptr, (png_bytep)row); } @@ -112,7 +124,8 @@ dmnsn_png_read_canvas(dmnsn_canvas **canvas, FILE *file) png_bytep image = NULL; png_bytep *row_pointers = NULL; unsigned int x, y; - dmnsn_pixel *pixel; + dmnsn_color *pixel; + dmnsn_sRGB sRGB; png_bytep png_pixel; /* Ensure that *canvas can always be deleted. */ @@ -203,11 +216,18 @@ dmnsn_png_read_canvas(dmnsn_canvas **canvas, FILE *file) pixel = (*canvas)->pixels + (height - y - 1)*width + x; png_pixel = image + 8*(y*width + x); - pixel->r = (png_pixel[0] << UINT16_C(8)) + png_pixel[1]; - pixel->g = (png_pixel[2] << UINT16_C(8)) + png_pixel[3]; - pixel->b = (png_pixel[4] << UINT16_C(8)) + png_pixel[5]; - pixel->a = (png_pixel[6] << UINT16_C(8)) + png_pixel[7]; - pixel->t = 0; + sRGB.R = ((double)((png_pixel[0] << UINT16_C(8)) + png_pixel[1])) + /UINT16_MAX; + sRGB.G = ((double)((png_pixel[2] << UINT16_C(8)) + png_pixel[3])) + /UINT16_MAX; + sRGB.B = ((double)((png_pixel[4] << UINT16_C(8)) + png_pixel[5])) + /UINT16_MAX; + + *pixel = dmnsn_color_from_sRGB(sRGB); + + pixel->filter = 0.0; + pixel->trans = ((double)((png_pixel[6] << UINT16_C(8)) + + png_pixel[7]))/UINT16_MAX; } } } else { @@ -216,11 +236,17 @@ dmnsn_png_read_canvas(dmnsn_canvas **canvas, FILE *file) pixel = (*canvas)->pixels + (height - y - 1)*width + x; png_pixel = image + 6*(y*width + x); - pixel->r = (png_pixel[0] << UINT16_C(8)) + png_pixel[1]; - pixel->g = (png_pixel[2] << UINT16_C(8)) + png_pixel[3]; - pixel->b = (png_pixel[4] << UINT16_C(8)) + png_pixel[5]; - pixel->a = 0; - pixel->t = 0; + sRGB.R = ((double)((png_pixel[0] << UINT16_C(8)) + png_pixel[1])) + /UINT16_MAX; + sRGB.G = ((double)((png_pixel[2] << UINT16_C(8)) + png_pixel[3])) + /UINT16_MAX; + sRGB.B = ((double)((png_pixel[4] << UINT16_C(8)) + png_pixel[5])) + /UINT16_MAX; + + *pixel = dmnsn_color_from_sRGB(sRGB); + + pixel->filter = 0.0; + pixel->trans = 0.0; } } } @@ -232,11 +258,14 @@ dmnsn_png_read_canvas(dmnsn_canvas **canvas, FILE *file) pixel = (*canvas)->pixels + (height - y - 1)*width + x; png_pixel = image + 4*(y*width + x); - pixel->r = png_pixel[0] << UINT16_C(8); - pixel->g = png_pixel[1] << UINT16_C(8); - pixel->b = png_pixel[2] << UINT16_C(8); - pixel->a = png_pixel[3] << UINT16_C(8); - pixel->t = 0; + sRGB.R = ((double)png_pixel[0])/UINT8_MAX; + sRGB.G = ((double)png_pixel[1])/UINT8_MAX; + sRGB.B = ((double)png_pixel[2])/UINT8_MAX; + + *pixel = dmnsn_color_from_sRGB(sRGB); + + pixel->filter = 0.0; + pixel->trans = ((double)png_pixel[3])/UINT8_MAX; } } } else { @@ -245,11 +274,14 @@ dmnsn_png_read_canvas(dmnsn_canvas **canvas, FILE *file) pixel = (*canvas)->pixels + (height - y - 1)*width + x; png_pixel = image + 3*(y*width + x); - pixel->r = png_pixel[0] << UINT16_C(8); - pixel->g = png_pixel[1] << UINT16_C(8); - pixel->b = png_pixel[2] << UINT16_C(8); - pixel->a = 0; - pixel->t = 0; + sRGB.R = ((double)png_pixel[0])/UINT8_MAX; + sRGB.G = ((double)png_pixel[1])/UINT8_MAX; + sRGB.B = ((double)png_pixel[2])/UINT8_MAX; + + *pixel = dmnsn_color_from_sRGB(sRGB); + + pixel->filter = 0.0; + pixel->trans = 0.0; } } } |