From 6aafcec6823d2b99c40b2ce85ed6581b6c3af3ea Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 19 Oct 2011 14:26:51 -0400 Subject: Make PNG and GL share the same optimizer. --- libdimension/Makefile.am | 2 ++ libdimension/dimension-internal.h | 1 + libdimension/gl.c | 38 ++--------------------- libdimension/png.c | 41 ++----------------------- libdimension/rgba16.c | 63 +++++++++++++++++++++++++++++++++++++++ libdimension/rgba16.h | 32 ++++++++++++++++++++ 6 files changed, 103 insertions(+), 74 deletions(-) create mode 100644 libdimension/rgba16.c create mode 100644 libdimension/rgba16.h (limited to 'libdimension') diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am index 73a4ae5..35f56f7 100644 --- a/libdimension/Makefile.am +++ b/libdimension/Makefile.am @@ -105,6 +105,8 @@ libdimension_la_SOURCES = $(nobase_include_HEADERS) \ raytrace.c \ refcount-internal.h \ reflection.c \ + rgba16.c \ + rgba16.h \ scene.c \ solid_pigment.c \ sphere.c \ diff --git a/libdimension/dimension-internal.h b/libdimension/dimension-internal.h index a7eb213..15a7e4c 100644 --- a/libdimension/dimension-internal.h +++ b/libdimension/dimension-internal.h @@ -36,5 +36,6 @@ #include "future-impl.h" #include "threads.h" #include "prtree.h" +#include "rgba16.h" #endif /* DIMENSION_INTERNAL_H */ diff --git a/libdimension/gl.c b/libdimension/gl.c index ff7906c..1250307 100644 --- a/libdimension/gl.c +++ b/libdimension/gl.c @@ -23,48 +23,16 @@ * OpenGL import/export. */ -#include "dimension.h" +#include "dimension-internal.h" #include #include #include -/** 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_canvas_get_pixel(canvas, x, y); - color = dmnsn_remove_filter(color); - color = dmnsn_color_to_sRGB(color); - color = dmnsn_color_saturate(color); - - 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); -} - /* Optimize canvas for GL drawing */ int dmnsn_gl_optimize_canvas(dmnsn_canvas *canvas) { - /* Check if we've already optimized this canvas */ - DMNSN_ARRAY_FOREACH (dmnsn_canvas_optimizer *, i, canvas->optimizers) { - if (i->optimizer_fn == dmnsn_gl_optimizer_fn) { - return 0; - } - } - - dmnsn_canvas_optimizer optimizer; - optimizer.optimizer_fn = dmnsn_gl_optimizer_fn; - optimizer.free_fn = dmnsn_free; - - /* Allocate a buffer to hold RGB values */ - optimizer.ptr = dmnsn_malloc(4*canvas->width*canvas->height*sizeof(GLushort)); - - /* Set a new optimizer */ - dmnsn_canvas_optimize(canvas, optimizer); + dmnsn_rgba16_optimize_canvas(canvas); return 0; } @@ -81,7 +49,7 @@ dmnsn_gl_write_canvas(const dmnsn_canvas *canvas) /* Check if we can optimize this */ DMNSN_ARRAY_FOREACH (dmnsn_canvas_optimizer *, i, canvas->optimizers) { - if (i->optimizer_fn == dmnsn_gl_optimizer_fn) { + if (i->optimizer_fn == dmnsn_rgba16_optimizer_fn) { glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_SHORT, i->ptr); return glGetError() == GL_NO_ERROR ? 0 : 1; } diff --git a/libdimension/png.c b/libdimension/png.c index 8fc2c1f..82fa31a 100644 --- a/libdimension/png.c +++ b/libdimension/png.c @@ -30,51 +30,14 @@ #include #include -/** PNG optimizer callback. */ -static void dmnsn_png_optimizer_fn(const dmnsn_canvas *canvas, - dmnsn_canvas_optimizer optimizer, - size_t x, size_t y); - /* Optimize canvas for PNG exporting */ int dmnsn_png_optimize_canvas(dmnsn_canvas *canvas) { - /* Check if we've already optimized this canvas */ - DMNSN_ARRAY_FOREACH (dmnsn_canvas_optimizer *, i, canvas->optimizers) { - if (i->optimizer_fn == dmnsn_png_optimizer_fn) { - return 0; - } - } - - dmnsn_canvas_optimizer optimizer; - optimizer.optimizer_fn = dmnsn_png_optimizer_fn; - optimizer.free_fn = dmnsn_free; - - optimizer.ptr = dmnsn_malloc(4*canvas->width*canvas->height*sizeof(uint16_t)); - - dmnsn_canvas_optimize(canvas, optimizer); + dmnsn_rgba16_optimize_canvas(canvas); return 0; } -/* PNG optimizer callback */ -static void -dmnsn_png_optimizer_fn(const dmnsn_canvas *canvas, - dmnsn_canvas_optimizer optimizer, size_t x, size_t y) -{ - dmnsn_color color; - uint16_t *pixel = (uint16_t *)optimizer.ptr + 4*(y*canvas->width + x); - - color = dmnsn_canvas_get_pixel(canvas, x, y); - color = dmnsn_remove_filter(color); - color = dmnsn_color_to_sRGB(color); - color = dmnsn_color_saturate(color); - - 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. */ typedef struct { dmnsn_future *future; @@ -215,7 +178,7 @@ dmnsn_png_write_canvas_thread(void *ptr) DMNSN_ARRAY_FOREACH (dmnsn_canvas_optimizer *, i, payload->canvas->optimizers) { - if (i->optimizer_fn == dmnsn_png_optimizer_fn) { + if (i->optimizer_fn == dmnsn_rgba16_optimizer_fn) { for (size_t y = 0; y < height; ++y) { /* Invert the rows. PNG coordinates are fourth quadrant. */ uint16_t *row = (uint16_t *)i->ptr + 4*(height - y - 1)*width; diff --git a/libdimension/rgba16.c b/libdimension/rgba16.c new file mode 100644 index 0000000..980a2d3 --- /dev/null +++ b/libdimension/rgba16.c @@ -0,0 +1,63 @@ +/************************************************************************* + * Copyright (C) 2010-2011 Tavian Barnes * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * . * + *************************************************************************/ + +/** + * @file + * 16-bit RGBA canvas optimizer. + */ + +#include "dimension-internal.h" +#include + +void +dmnsn_rgba16_optimize_canvas(dmnsn_canvas *canvas) +{ + /* Check if we've already optimized this canvas */ + DMNSN_ARRAY_FOREACH (dmnsn_canvas_optimizer *, i, canvas->optimizers) { + if (i->optimizer_fn == dmnsn_rgba16_optimizer_fn) { + return; + } + } + + dmnsn_canvas_optimizer optimizer; + optimizer.optimizer_fn = dmnsn_rgba16_optimizer_fn; + optimizer.free_fn = dmnsn_free; + optimizer.ptr = dmnsn_malloc(4*canvas->width*canvas->height*sizeof(uint16_t)); + + dmnsn_canvas_optimize(canvas, optimizer); +} + +/* PNG optimizer callback */ +void +dmnsn_rgba16_optimizer_fn(const dmnsn_canvas *canvas, + dmnsn_canvas_optimizer optimizer, size_t x, size_t y) +{ + uint16_t *pixel = (uint16_t *)optimizer.ptr + 4*(y*canvas->width + x); + dmnsn_color color; + color = dmnsn_canvas_get_pixel(canvas, x, y); + color = dmnsn_remove_filter(color); + color = dmnsn_color_to_sRGB(color); + color = dmnsn_color_saturate(color); + + 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); +} diff --git a/libdimension/rgba16.h b/libdimension/rgba16.h new file mode 100644 index 0000000..24af74a --- /dev/null +++ b/libdimension/rgba16.h @@ -0,0 +1,32 @@ +/************************************************************************* + * Copyright (C) 2010-2011 Tavian Barnes * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * . * + *************************************************************************/ + +/** + * @file + * 16-bit RGBA canvas optimizer interface, shared between PNG and GL optimizers. + */ + +/** Apply the rgba16 optimizer to a canvas. */ +void dmnsn_rgba16_optimize_canvas(dmnsn_canvas *canvas); + +/** RGBA16 optimizer callback. */ +void dmnsn_rgba16_optimizer_fn(const dmnsn_canvas *canvas, + dmnsn_canvas_optimizer optimizer, + size_t x, size_t y); -- cgit v1.2.3