summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libdimension/gl.c99
-rw-r--r--libdimension/png.c71
2 files changed, 27 insertions, 143 deletions
diff --git a/libdimension/gl.c b/libdimension/gl.c
index a968d75..71d0194 100644
--- a/libdimension/gl.c
+++ b/libdimension/gl.c
@@ -29,9 +29,21 @@
#include <stdint.h>
/** GL optimizer callback. */
-static void dmnsn_gl_optimizer_fn(const dmnsn_canvas *canvas,
- dmnsn_canvas_optimizer optimizer,
- size_t x, size_t y);
+static void
+dmnsn_gl_optimizer_fn(const dmnsn_canvas *canvas,
+ dmnsn_canvas_optimizer optimizer, size_t x, size_t y)
+{
+ GLushort *pixel = (GLushort *)optimizer.ptr + 4*(y*canvas->width + x);
+ dmnsn_color color = dmnsn_get_pixel(canvas, x, y);
+ color = dmnsn_remove_filter(color);
+ color = dmnsn_color_to_sRGB(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;
+}
/* Optimize canvas for GL drawing */
int
@@ -87,38 +99,10 @@ dmnsn_gl_write_canvas(const dmnsn_canvas *canvas)
color = dmnsn_color_to_sRGB(color);
/* Saturate R, G, and B to [0, UINT16_MAX] */
-
- if (color.R <= 0.0) {
- pixel[0] = 0;
- } else if (color.R >= 1.0) {
- pixel[0] = UINT16_MAX;
- } else {
- pixel[0] = color.R*UINT16_MAX;
- }
-
- if (color.G <= 0.0) {
- pixel[1] = 0;
- } else if (color.G >= 1.0) {
- pixel[1] = UINT16_MAX;
- } else {
- pixel[1] = color.G*UINT16_MAX;
- }
-
- if (color.B <= 0.0) {
- pixel[2] = 0;
- } else if (color.B >= 1.0) {
- pixel[2] = UINT16_MAX;
- } else {
- pixel[2] = color.B*UINT16_MAX;
- }
-
- if (color.trans <= 0.0) {
- pixel[3] = 0;
- } else if (color.trans >= 1.0) {
- pixel[3] = UINT16_MAX;
- } else {
- pixel[3] = color.trans*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;
}
}
@@ -163,48 +147,3 @@ dmnsn_gl_read_canvas(size_t x0, size_t y0,
dmnsn_free(pixels);
return canvas;
}
-
-/* GL optimizer callback */
-static void
-dmnsn_gl_optimizer_fn(const dmnsn_canvas *canvas,
- dmnsn_canvas_optimizer optimizer, size_t x, size_t y)
-{
- GLushort *pixel = (GLushort *)optimizer.ptr + 4*(y*canvas->width + x);
- dmnsn_color color = dmnsn_get_pixel(canvas, x, y);
- color = dmnsn_remove_filter(color);
- color = dmnsn_color_to_sRGB(color);
-
- /* Saturate R, G, and B to [0, UINT16_MAX] */
-
- if (color.R <= 0.0) {
- pixel[0] = 0;
- } else if (color.R >= 1.0) {
- pixel[0] = UINT16_MAX;
- } else {
- pixel[0] = color.R*UINT16_MAX;
- }
-
- if (color.G <= 0.0) {
- pixel[1] = 0;
- } else if (color.G >= 1.0) {
- pixel[1] = UINT16_MAX;
- } else {
- pixel[1] = color.G*UINT16_MAX;
- }
-
- if (color.B <= 0.0) {
- pixel[2] = 0;
- } else if (color.B >= 1.0) {
- pixel[2] = UINT16_MAX;
- } else {
- pixel[2] = color.B*UINT16_MAX;
- }
-
- if (color.trans <= 0.0) {
- pixel[3] = 0;
- } else if (color.trans >= 1.0) {
- pixel[3] = UINT16_MAX;
- } else {
- pixel[3] = color.trans*UINT16_MAX;
- }
-}
diff --git a/libdimension/png.c b/libdimension/png.c
index 36164d1..7f5da8f 100644
--- a/libdimension/png.c
+++ b/libdimension/png.c
@@ -69,38 +69,10 @@ dmnsn_png_optimizer_fn(const dmnsn_canvas *canvas,
color = dmnsn_color_to_sRGB(color);
/* Saturate R, G, and B to [0, UINT16_MAX] */
-
- if (color.R <= 0.0) {
- pixel[0] = 0;
- } else if (color.R >= 1.0) {
- pixel[0] = UINT16_MAX;
- } else {
- pixel[0] = color.R*UINT16_MAX;
- }
-
- if (color.G <= 0.0) {
- pixel[1] = 0;
- } else if (color.G >= 1.0) {
- pixel[1] = UINT16_MAX;
- } else {
- pixel[1] = color.G*UINT16_MAX;
- }
-
- if (color.B <= 0.0) {
- pixel[2] = 0;
- } else if (color.B >= 1.0) {
- pixel[2] = UINT16_MAX;
- } else {
- pixel[2] = color.B*UINT16_MAX;
- }
-
- if (color.trans <= 0.0) {
- pixel[3] = 0;
- } else if (color.trans >= 1.0) {
- pixel[3] = UINT16_MAX;
- } else {
- pixel[3] = color.trans*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;
}
/** Payload type for PNG write thread callback. */
@@ -272,37 +244,10 @@ dmnsn_png_write_canvas_thread(void *ptr)
/* Saturate R, G, and B to [0, UINT16_MAX] */
- if (color.R <= 0.0) {
- row[4*x] = 0;
- } else if (color.R >= 1.0) {
- row[4*x] = UINT16_MAX;
- } else {
- row[4*x] = color.R*UINT16_MAX;
- }
-
- if (color.G <= 0.0) {
- row[4*x + 1] = 0;
- } else if (color.G >= 1.0) {
- row[4*x + 1] = UINT16_MAX;
- } else {
- row[4*x + 1] = color.G*UINT16_MAX;
- }
-
- if (color.B <= 0.0) {
- row[4*x + 2] = 0;
- } else if (color.B >= 1.0) {
- row[4*x + 2] = UINT16_MAX;
- } else {
- row[4*x + 2] = color.B*UINT16_MAX;
- }
-
- if (color.trans <= 0.0) {
- row[4*x + 3] = 0;
- } else if (color.trans >= 1.0) {
- row[4*x + 3] = UINT16_MAX;
- } else {
- row[4*x + 3] = color.trans*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;
}
/* Write the row */