summaryrefslogtreecommitdiffstats
path: root/libdimension/png.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2011-09-18 14:04:19 -0400
committerTavian Barnes <tavianator@gmail.com>2011-09-18 14:04:19 -0400
commitb2e1763c7d54e76a49f6f4434996f37882ae2171 (patch)
treecefad99c5d5d2724d432c43ef762bc9e23f2eb2e /libdimension/png.c
parent126211b917626fe7531310971981ee5d06026625 (diff)
downloaddimension-b2e1763c7d54e76a49f6f4434996f37882ae2171.tar.xz
Round correctly when converting colors to integers.
Diffstat (limited to 'libdimension/png.c')
-rw-r--r--libdimension/png.c21
1 files changed, 10 insertions, 11 deletions
diff --git a/libdimension/png.c b/libdimension/png.c
index 7f5da8f..21ad764 100644
--- a/libdimension/png.c
+++ b/libdimension/png.c
@@ -67,12 +67,12 @@ dmnsn_png_optimizer_fn(const dmnsn_canvas *canvas,
color = dmnsn_get_pixel(canvas, x, y);
color = dmnsn_remove_filter(color);
color = dmnsn_color_to_sRGB(color);
+ color = dmnsn_color_saturate(color);
- /* Saturate R, G, and B to [0, UINT16_MAX] */
- pixel[0] = dmnsn_min(dmnsn_max(color.R, 0.0), 1.0)*UINT16_MAX;
- pixel[1] = dmnsn_min(dmnsn_max(color.G, 0.0), 1.0)*UINT16_MAX;
- pixel[2] = dmnsn_min(dmnsn_max(color.B, 0.0), 1.0)*UINT16_MAX;
- pixel[3] = dmnsn_min(dmnsn_max(color.trans, 0.0), 1.0)*UINT16_MAX;
+ pixel[0] = lround(color.R*UINT16_MAX);
+ pixel[1] = lround(color.G*UINT16_MAX);
+ pixel[2] = lround(color.B*UINT16_MAX);
+ pixel[3] = lround(color.trans*UINT16_MAX);
}
/** Payload type for PNG write thread callback. */
@@ -241,13 +241,12 @@ dmnsn_png_write_canvas_thread(void *ptr)
dmnsn_color color = dmnsn_get_pixel(payload->canvas, x, height - y - 1);
color = dmnsn_remove_filter(color);
color = dmnsn_color_to_sRGB(color);
+ color = dmnsn_color_saturate(color);
- /* Saturate R, G, and B to [0, UINT16_MAX] */
-
- row[4*x] = dmnsn_min(dmnsn_max(color.R, 0.0), 1.0)*UINT16_MAX;
- row[4*x + 1] = dmnsn_min(dmnsn_max(color.G, 0.0), 1.0)*UINT16_MAX;
- row[4*x + 2] = dmnsn_min(dmnsn_max(color.B, 0.0), 1.0)*UINT16_MAX;
- row[4*x + 3] = dmnsn_min(dmnsn_max(color.trans, 0.0), 1.0)*UINT16_MAX;
+ row[4*x] = lround(color.R*UINT16_MAX);
+ row[4*x + 1] = lround(color.G*UINT16_MAX);
+ row[4*x + 2] = lround(color.B*UINT16_MAX);
+ row[4*x + 3] = lround(color.trans*UINT16_MAX);
}
/* Write the row */