summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-07-04 21:53:55 +0000
committerTavian Barnes <tavianator@gmail.com>2009-07-04 21:53:55 +0000
commit8085395acc28d8c77901615f8862ceeda5773b09 (patch)
treec8261638a486191a3c2cea1ed91590f8104878c7 /libdimension
parentd3ad0b6a25146318d153ecd6c792e41a9a0a6025 (diff)
downloaddimension-8085395acc28d8c77901615f8862ceeda5773b09.tar.xz
New interface for optimizing canvas conversions by registering
dmnsn_set_pixel() callbacks.
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/canvas.c39
-rw-r--r--libdimension/dimension/canvas.h36
-rw-r--r--libdimension/dimension/gl.h3
-rw-r--r--libdimension/dimension/png.h3
-rw-r--r--libdimension/gl.c136
-rw-r--r--libdimension/png.c98
6 files changed, 276 insertions, 39 deletions
diff --git a/libdimension/canvas.c b/libdimension/canvas.c
index 8d37c73..d53c7ef 100644
--- a/libdimension/canvas.c
+++ b/libdimension/canvas.c
@@ -40,6 +40,9 @@ dmnsn_new_canvas(unsigned int x, unsigned int y)
free(canvas);
return NULL;
}
+
+ /* Allocate room for the optimizers */
+ canvas->optimizers = dmnsn_new_array(sizeof(dmnsn_canvas_optimizer));
}
return canvas;
@@ -49,9 +52,45 @@ dmnsn_new_canvas(unsigned int x, unsigned int y)
void
dmnsn_delete_canvas(dmnsn_canvas *canvas)
{
+ unsigned int i;
+ dmnsn_canvas_optimizer optimizer;
+
if (canvas) {
+ /* Free the optimizers */
+ for (i = 0; i < dmnsn_array_size(canvas->optimizers); ++i) {
+ dmnsn_array_get(canvas->optimizers, i, &optimizer);
+ if (optimizer.free_fn) {
+ optimizer.free_fn(optimizer.ptr);
+ }
+ }
+
/* Free the pixels and canvas */
free(canvas->pixels);
free(canvas);
}
}
+
+/* Set a canvas optimizer */
+void
+dmnsn_optimize_canvas(dmnsn_canvas *canvas, dmnsn_canvas_optimizer optimizer)
+{
+ dmnsn_array_push(canvas->optimizers, &optimizer);
+}
+
+/* Set the color of a pixel */
+void
+dmnsn_set_pixel(dmnsn_canvas *canvas, unsigned int x, unsigned int y,
+ dmnsn_color color)
+{
+ unsigned int i;
+ dmnsn_canvas_optimizer optimizer;
+
+ /* Set the pixel */
+ canvas->pixels[y*canvas->x + x] = color;
+
+ /* Call the optimizers */
+ for (i = 0; i < dmnsn_array_size(canvas->optimizers); ++i) {
+ dmnsn_array_get(canvas->optimizers, i, &optimizer);
+ optimizer.optimizer_fn(canvas, optimizer, x, y);
+ }
+}
diff --git a/libdimension/dimension/canvas.h b/libdimension/dimension/canvas.h
index 4c94c33..a14333d 100644
--- a/libdimension/dimension/canvas.h
+++ b/libdimension/dimension/canvas.h
@@ -34,12 +34,34 @@ typedef struct {
* at (a,b) is accessible as pixels[b*x + a].
*/
dmnsn_color *pixels;
+
+ /* An array of dmnsn_canvas_optimizer's */
+ dmnsn_array *optimizers;
} dmnsn_canvas;
+typedef struct dmnsn_canvas_optimizer dmnsn_canvas_optimizer;
+
+/* Canvas optimizer callback types */
+typedef void dmnsn_canvas_optimizer_fn(dmnsn_canvas *canvas,
+ dmnsn_canvas_optimizer optimizer,
+ unsigned int x, unsigned int y);
+typedef void dmnsn_canvas_optimizer_free_fn(void *ptr);
+
+/* Canvas optimizer */
+struct dmnsn_canvas_optimizer {
+ dmnsn_canvas_optimizer_fn *optimizer_fn;
+ dmnsn_canvas_optimizer_free_fn *free_fn;
+ void *ptr;
+};
+
/* Allocate and free a canvas */
dmnsn_canvas *dmnsn_new_canvas(unsigned int x, unsigned int y);
void dmnsn_delete_canvas(dmnsn_canvas *canvas);
+/* Set a canvas optimizer */
+void dmnsn_optimize_canvas(dmnsn_canvas *canvas,
+ dmnsn_canvas_optimizer optimizer);
+
/* Pixel accessors */
DMNSN_INLINE dmnsn_color
@@ -48,17 +70,7 @@ dmnsn_get_pixel(const dmnsn_canvas *canvas, unsigned int x, unsigned int y)
return canvas->pixels[y*canvas->x + x];
}
-DMNSN_INLINE void
-dmnsn_set_pixel(dmnsn_canvas *canvas,
- unsigned int x, unsigned int y, dmnsn_color color)
-{
- canvas->pixels[y*canvas->x + x] = color;
-}
-
-DMNSN_INLINE dmnsn_color *
-dmnsn_pixel_at(dmnsn_canvas *canvas, unsigned int x, unsigned int y)
-{
- return canvas->pixels + y*canvas->x + x;
-}
+void dmnsn_set_pixel(dmnsn_canvas *canvas, unsigned int x, unsigned int y,
+ dmnsn_color color);
#endif /* DIMENSION_CANVAS_H */
diff --git a/libdimension/dimension/gl.h b/libdimension/dimension/gl.h
index f7de782..f81f662 100644
--- a/libdimension/dimension/gl.h
+++ b/libdimension/dimension/gl.h
@@ -26,7 +26,8 @@
#ifndef DIMENSION_GL_H
#define DIMENSION_GL_H
-#include <stdio.h>
+/* Optimize canvas for GL drawing */
+int dmnsn_gl_optimize_canvas(dmnsn_canvas *canvas);
/* Write canvas to GL framebuffer. Returns 0 on success, nonzero on failure. */
int dmnsn_gl_write_canvas(const dmnsn_canvas *canvas);
diff --git a/libdimension/dimension/png.h b/libdimension/dimension/png.h
index 858f79f..f1b1470 100644
--- a/libdimension/dimension/png.h
+++ b/libdimension/dimension/png.h
@@ -27,6 +27,9 @@
#include <stdio.h>
+/* Optimize canvas for PNG exporting */
+int dmnsn_png_optimize_canvas(dmnsn_canvas *canvas);
+
/* Write canvas to file in PNG format. Returns 0 on success, nonzero on
failure */
int dmnsn_png_write_canvas(const dmnsn_canvas *canvas, FILE *file);
diff --git a/libdimension/gl.c b/libdimension/gl.c
index e67551b..a37d7f7 100644
--- a/libdimension/gl.c
+++ b/libdimension/gl.c
@@ -20,21 +20,67 @@
#include "dimension.h"
#include <GL/gl.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+/* GL optimizer callback */
+static void dmnsn_gl_optimizer_fn(dmnsn_canvas *canvas,
+ dmnsn_canvas_optimizer optimizer,
+ unsigned int x, unsigned int y);
+
+/* Optimize canvas for GL drawing */
+int
+dmnsn_gl_optimize_canvas(dmnsn_canvas *canvas)
+{
+ dmnsn_canvas_optimizer optimizer;
+ unsigned int i;
+
+ /* Check if we've already optimized this canvas */
+ for (i = 0; i < dmnsn_array_size(canvas->optimizers); ++i) {
+ dmnsn_array_get(canvas->optimizers, i, &optimizer);
+ if (optimizer.optimizer_fn == &dmnsn_gl_optimizer_fn) {
+ return 0;
+ }
+ }
+
+ optimizer.optimizer_fn = &dmnsn_gl_optimizer_fn;
+ optimizer.free_fn = &free;
+
+ optimizer.ptr = malloc(4*canvas->x*canvas->y*sizeof(GLushort));
+ if (!optimizer.ptr) {
+ return 1;
+ }
+
+ dmnsn_optimize_canvas(canvas, optimizer);
+
+ return 0;
+}
/* Write canvas to GL framebuffer. Returns 0 on success, nonzero on failure */
int
dmnsn_gl_write_canvas(const dmnsn_canvas *canvas)
{
- GLuint *pixels; /* Array of 32-bit ints in RGBA order */
- GLuint *pixel;
+ dmnsn_canvas_optimizer optimizer;
+ GLushort *pixels; /* Array of 16-bit ints in RGBA order */
+ GLushort *pixel;
dmnsn_sRGB sRGB;
dmnsn_color color;
- unsigned int x, y, width, height;
+ unsigned int i, x, y, width, height;
width = canvas->x;
height = canvas->y;
- pixels = malloc(4*width*height*sizeof(GLuint));
+ /* Check if we can optimize this */
+ for (i = 0; i < dmnsn_array_size(canvas->optimizers); ++i) {
+ dmnsn_array_get(canvas->optimizers, i, &optimizer);
+ if (optimizer.optimizer_fn == &dmnsn_gl_optimizer_fn) {
+ glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_SHORT, optimizer.ptr);
+ return 0;
+ }
+ }
+
+ /* We couldn't, so to this non-optimally */
+ pixels = malloc(4*width*height*sizeof(GLushort));
if (!pixels) {
return 1;
}
@@ -46,38 +92,38 @@ dmnsn_gl_write_canvas(const dmnsn_canvas *canvas)
color = dmnsn_get_pixel(canvas, x, y);
sRGB = dmnsn_sRGB_from_color(color);
- /* Saturate R, G, and B to [0, UINT32_MAX] */
+ /* Saturate R, G, and B to [0, UINT16_MAX] */
if (sRGB.R <= 0.0) {
pixel[0] = 0;
} else if (sRGB.R >= 1.0) {
- pixel[0] = UINT32_MAX;
+ pixel[0] = UINT16_MAX;
} else {
- pixel[0] = sRGB.R*UINT32_MAX;
+ pixel[0] = sRGB.R*UINT16_MAX;
}
if (sRGB.G <= 0.0) {
pixel[1] = 0;
} else if (sRGB.G >= 1.0) {
- pixel[1] = UINT32_MAX;
+ pixel[1] = UINT16_MAX;
} else {
- pixel[1] = sRGB.G*UINT32_MAX;
+ pixel[1] = sRGB.G*UINT16_MAX;
}
if (sRGB.B <= 0.0) {
pixel[2] = 0;
} else if (sRGB.B >= 1.0) {
- pixel[2] = UINT32_MAX;
+ pixel[2] = UINT16_MAX;
} else {
- pixel[2] = sRGB.B*UINT32_MAX;
+ pixel[2] = sRGB.B*UINT16_MAX;
}
/* color.filter + color.trans is in [0.0, 1.0] by definition */
- pixel[3] = (color.filter + color.trans)*UINT32_MAX;
+ pixel[3] = (color.filter + color.trans)*UINT16_MAX;
}
}
- glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_INT, pixels);
+ glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_SHORT, pixels);
free(pixels);
return 0;
@@ -89,10 +135,10 @@ dmnsn_gl_read_canvas(unsigned int x0, unsigned int y0,
unsigned int width, unsigned int height)
{
dmnsn_canvas *canvas;
- GLuint *pixels; /* Array of 32-bit ints in RGBA order */
- GLuint *pixel;
+ GLushort *pixels; /* Array of 16-bit ints in RGBA order */
+ GLushort *pixel;
dmnsn_sRGB sRGB;
- dmnsn_color *color;
+ dmnsn_color color;
unsigned int x, y;
canvas = dmnsn_new_canvas(width, height);
@@ -100,28 +146,70 @@ dmnsn_gl_read_canvas(unsigned int x0, unsigned int y0,
return NULL;
}
- pixels = malloc(4*width*height*sizeof(GLuint));
+ pixels = malloc(4*width*height*sizeof(GLushort));
if (!pixels) {
dmnsn_delete_canvas(canvas);
return NULL;
}
- glReadPixels(x0, y0, width, height, GL_RGBA, GL_UNSIGNED_INT, pixels);
+ glReadPixels(x0, y0, width, height, GL_RGBA, GL_UNSIGNED_SHORT, pixels);
for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) {
- color = dmnsn_pixel_at(canvas, x, y);
pixel = pixels + 4*(y*width + x);
- sRGB.R = ((double)pixel[0])/UINT32_MAX;
- sRGB.G = ((double)pixel[1])/UINT32_MAX;
- sRGB.B = ((double)pixel[2])/UINT32_MAX;
+ sRGB.R = ((double)pixel[0])/UINT16_MAX;
+ sRGB.G = ((double)pixel[1])/UINT16_MAX;
+ sRGB.B = ((double)pixel[2])/UINT16_MAX;
- *color = dmnsn_color_from_sRGB(sRGB);
- color->filter = ((double)pixel[3])/UINT32_MAX;
+ color = dmnsn_color_from_sRGB(sRGB);
+ color.filter = ((double)pixel[3])/UINT16_MAX;
+ dmnsn_set_pixel(canvas, x, y, color);
}
}
free(pixels);
return canvas;
}
+
+/* GL optimizer callback */
+static void
+dmnsn_gl_optimizer_fn(dmnsn_canvas *canvas, dmnsn_canvas_optimizer optimizer,
+ unsigned int x, unsigned int y)
+{
+ dmnsn_color color;
+ dmnsn_sRGB sRGB;
+ GLushort *pixel = (GLushort *)optimizer.ptr + 4*(y*canvas->x + x);
+
+ color = dmnsn_get_pixel(canvas, x, y);
+ sRGB = dmnsn_sRGB_from_color(color);
+
+ /* Saturate R, G, and B to [0, UINT16_MAX] */
+
+ if (sRGB.R <= 0.0) {
+ pixel[0] = 0;
+ } else if (sRGB.R >= 1.0) {
+ pixel[0] = UINT16_MAX;
+ } else {
+ pixel[0] = sRGB.R*UINT16_MAX;
+ }
+
+ if (sRGB.G <= 0.0) {
+ pixel[1] = 0;
+ } else if (sRGB.G >= 1.0) {
+ pixel[1] = UINT16_MAX;
+ } else {
+ pixel[1] = sRGB.G*UINT16_MAX;
+ }
+
+ if (sRGB.B <= 0.0) {
+ pixel[2] = 0;
+ } else if (sRGB.B >= 1.0) {
+ pixel[2] = UINT16_MAX;
+ } else {
+ pixel[2] = sRGB.B*UINT16_MAX;
+ }
+
+ /* color.filter + color.trans is in [0.0, 1.0] by definition */
+ pixel[3] = (color.filter + color.trans)*UINT16_MAX;
+}
diff --git a/libdimension/png.c b/libdimension/png.c
index 6bb1f13..c8ffc62 100644
--- a/libdimension/png.c
+++ b/libdimension/png.c
@@ -24,6 +24,82 @@
#include <setjmp.h>
#include <arpa/inet.h>
#include <stdlib.h>
+#include <stdint.h>
+
+/* PNG optimizer callback */
+static void dmnsn_png_optimizer_fn(dmnsn_canvas *canvas,
+ dmnsn_canvas_optimizer optimizer,
+ unsigned int x, unsigned int y);
+
+/* Optimize canvas for PNG exporting */
+int
+dmnsn_png_optimize_canvas(dmnsn_canvas *canvas)
+{
+ dmnsn_canvas_optimizer optimizer;
+ unsigned int i;
+
+ /* Check if we've already optimized this canvas */
+ for (i = 0; i < dmnsn_array_size(canvas->optimizers); ++i) {
+ dmnsn_array_get(canvas->optimizers, i, &optimizer);
+ if (optimizer.optimizer_fn == &dmnsn_png_optimizer_fn) {
+ return 0;
+ }
+ }
+
+ optimizer.optimizer_fn = &dmnsn_png_optimizer_fn;
+ optimizer.free_fn = &free;
+
+ optimizer.ptr = malloc(4*canvas->x*canvas->y*sizeof(uint16_t));
+ if (!optimizer.ptr) {
+ return 1;
+ }
+
+ dmnsn_optimize_canvas(canvas, optimizer);
+
+ return 0;
+}
+
+/* PNG optimizer callback */
+static void
+dmnsn_png_optimizer_fn(dmnsn_canvas *canvas, dmnsn_canvas_optimizer optimizer,
+ unsigned int x, unsigned int y)
+{
+ dmnsn_color color;
+ dmnsn_sRGB sRGB;
+ uint16_t *pixel = (uint16_t *)optimizer.ptr + 4*(y*canvas->x + x);
+
+ color = dmnsn_get_pixel(canvas, x, y);
+ sRGB = dmnsn_sRGB_from_color(color);
+
+ /* Saturate R, G, and B to [0, UINT16_MAX] */
+
+ if (sRGB.R <= 0.0) {
+ pixel[0] = 0;
+ } else if (sRGB.R >= 1.0) {
+ pixel[0] = UINT16_MAX;
+ } else {
+ pixel[0] = sRGB.R*UINT16_MAX;
+ }
+
+ if (sRGB.G <= 0.0) {
+ pixel[1] = 0;
+ } else if (sRGB.G >= 1.0) {
+ pixel[1] = UINT16_MAX;
+ } else {
+ pixel[1] = sRGB.G*UINT16_MAX;
+ }
+
+ if (sRGB.B <= 0.0) {
+ pixel[2] = 0;
+ } else if (sRGB.B >= 1.0) {
+ pixel[2] = UINT16_MAX;
+ } else {
+ pixel[2] = sRGB.B*UINT16_MAX;
+ }
+
+ /* color.filter + color.trans is in [0.0, 1.0] by definition */
+ pixel[3] = (color.filter + color.trans)*UINT16_MAX;
+}
/* Payload to store function arguments for thread callbacks */
@@ -154,7 +230,7 @@ dmnsn_png_read_canvas_thread(void *ptr)
if (retval) {
*payload->canvas = dmnsn_png_read_canvas_impl(payload->progress,
payload->file);
- *retval = payload->canvas ? 0 : 1; /* Fail if it returned NULL */
+ *retval = *payload->canvas ? 0 : 1; /* Fail if it returned NULL */
}
dmnsn_done_progress(payload->progress);
free(payload);
@@ -166,10 +242,11 @@ static int
dmnsn_png_write_canvas_impl(dmnsn_progress *progress,
const dmnsn_canvas *canvas, FILE *file)
{
+ dmnsn_canvas_optimizer optimizer;
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
- unsigned int x, y;
+ unsigned int i, x, y;
uint16_t *row = NULL;
dmnsn_color color;
dmnsn_sRGB sRGB;
@@ -219,6 +296,23 @@ dmnsn_png_write_canvas_impl(dmnsn_progress *progress,
png_set_swap(png_ptr);
}
+ /* Check if we can optimize this */
+ for (i = 0; i < dmnsn_array_size(canvas->optimizers); ++i) {
+ dmnsn_array_get(canvas->optimizers, i, &optimizer);
+ if (optimizer.optimizer_fn == &dmnsn_png_optimizer_fn) {
+ for (y = 0; y < height; ++y) {
+ png_write_row(png_ptr,
+ (png_bytep)((uint16_t *)optimizer.ptr + 4*y*width));
+ dmnsn_increment_progress(progress);
+ }
+
+ /* Finish the PNG file */
+ png_write_end(png_ptr, info_ptr);
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+ return 0;
+ }
+ }
+
/* Allocate the temporary row of RGBA values */
row = malloc(4*sizeof(uint16_t)*width);
if (!row) {