summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/canvas_pigment.c27
-rw-r--r--libdimension/dimension/pigment.h23
-rw-r--r--libdimension/pigment.c30
-rw-r--r--libdimension/pigment_map.c49
4 files changed, 81 insertions, 48 deletions
diff --git a/libdimension/canvas_pigment.c b/libdimension/canvas_pigment.c
index 6847b6c..85ae551 100644
--- a/libdimension/canvas_pigment.c
+++ b/libdimension/canvas_pigment.c
@@ -1,5 +1,5 @@
/*************************************************************************
- * Copyright (C) 2010-2011 Tavian Barnes <tavianator@tavianator.com> *
+ * Copyright (C) 2010-2014 Tavian Barnes <tavianator@tavianator.com> *
* *
* This file is part of The Dimension Library. *
* *
@@ -25,11 +25,19 @@
#include "dimension.h"
+/** Canvas pigment type. */
+typedef struct dmnsn_canvas_pigment {
+ dmnsn_pigment pigment;
+ dmnsn_canvas *canvas;
+} dmnsn_canvas_pigment;
+
/** Canvas pigment color callback. */
static dmnsn_tcolor
dmnsn_canvas_pigment_fn(const dmnsn_pigment *pigment, dmnsn_vector v)
{
- dmnsn_canvas *canvas = pigment->ptr;
+ const dmnsn_canvas_pigment *canvas_pigment =
+ (const dmnsn_canvas_pigment *)pigment;
+ dmnsn_canvas *canvas = canvas_pigment->canvas;
size_t x = llround((fmod(v.x, 1.0) + 1.0)*(canvas->width - 1));
size_t y = llround((fmod(v.y, 1.0) + 1.0)*(canvas->height - 1));
@@ -38,18 +46,23 @@ dmnsn_canvas_pigment_fn(const dmnsn_pigment *pigment, dmnsn_vector v)
/** Canvas pigment destructor. */
static void
-dmnsn_canvas_pigment_free_fn(void *ptr)
+dmnsn_canvas_pigment_free_fn(dmnsn_pigment *pigment)
{
- dmnsn_delete_canvas(ptr);
+ dmnsn_canvas_pigment *canvas_pigment = (dmnsn_canvas_pigment *)pigment;
+ dmnsn_delete_canvas(canvas_pigment->canvas);
+ dmnsn_free(canvas_pigment);
}
/* Create a canvas color */
dmnsn_pigment *
dmnsn_new_canvas_pigment(dmnsn_canvas *canvas)
{
- dmnsn_pigment *pigment = dmnsn_new_pigment();
+ dmnsn_canvas_pigment *canvas_pigment = DMNSN_MALLOC(dmnsn_canvas_pigment);
+ canvas_pigment->canvas = canvas;
+
+ dmnsn_pigment *pigment = &canvas_pigment->pigment;
+ dmnsn_init_pigment(pigment);
pigment->pigment_fn = dmnsn_canvas_pigment_fn;
- pigment->free_fn = dmnsn_canvas_pigment_free_fn;
- pigment->ptr = canvas;
+ pigment->free_fn = dmnsn_canvas_pigment_free_fn;
return pigment;
}
diff --git a/libdimension/dimension/pigment.h b/libdimension/dimension/pigment.h
index b046993..455ee72 100644
--- a/libdimension/dimension/pigment.h
+++ b/libdimension/dimension/pigment.h
@@ -1,5 +1,5 @@
/*************************************************************************
- * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> *
+ * Copyright (C) 2009-2014 Tavian Barnes <tavianator@tavianator.com> *
* *
* This file is part of The Dimension Library. *
* *
@@ -41,21 +41,24 @@ typedef dmnsn_tcolor dmnsn_pigment_fn(const dmnsn_pigment *pigment,
*/
typedef void dmnsn_pigment_initialize_fn(dmnsn_pigment *pigment);
+/**
+ * Pigment destructor callback.
+ * @param[in,out] pigment The pigment to destroy.
+ */
+typedef void dmnsn_pigment_free_fn(dmnsn_pigment *pigment);
+
/** A pigment. */
struct dmnsn_pigment {
- dmnsn_pigment_fn *pigment_fn; /**< The pigment callback. */
+ dmnsn_pigment_fn *pigment_fn; /**< The pigment callback. */
dmnsn_pigment_initialize_fn *initialize_fn; /**< The initializer callback. */
- dmnsn_free_fn *free_fn; /**< The destructor callback. */
+ dmnsn_pigment_free_fn *free_fn; /**< The destructor callback. */
- dmnsn_matrix trans; /**< Transformation matrix. */
+ dmnsn_matrix trans; /**< Transformation matrix. */
dmnsn_matrix trans_inv; /**< The inverse of the transformation matrix. */
/** Quick color -- used for low-quality renders. */
dmnsn_tcolor quick_color;
- /** Generic pointer. */
- void *ptr;
-
DMNSN_REFCOUNT; /** Reference count. */
bool initialized; /** @internal Whether the pigment is initialized. */
};
@@ -67,6 +70,12 @@ struct dmnsn_pigment {
dmnsn_pigment *dmnsn_new_pigment(void);
/**
+ * Initialize a dmnsn_pigment field.
+ * @param[out] pigment The pigment to initialize.
+ */
+void dmnsn_init_pigment(dmnsn_pigment *pigment);
+
+/**
* Delete a pigment.
* @param[in,out] pigment The pigment to delete.
*/
diff --git a/libdimension/pigment.c b/libdimension/pigment.c
index 6134c2b..e392d7b 100644
--- a/libdimension/pigment.c
+++ b/libdimension/pigment.c
@@ -25,19 +25,32 @@
#include "dimension-internal.h"
+static void
+dmnsn_default_pigment_free_fn(dmnsn_pigment *pigment)
+{
+ dmnsn_free(pigment);
+}
+
/* Allocate a dummy pigment */
dmnsn_pigment *
dmnsn_new_pigment(void)
{
dmnsn_pigment *pigment = DMNSN_MALLOC(dmnsn_pigment);
- pigment->pigment_fn = NULL;
+ dmnsn_init_pigment(pigment);
+ return pigment;
+}
+
+/* Initialize a pigment */
+void
+dmnsn_init_pigment(dmnsn_pigment *pigment)
+{
+ pigment->pigment_fn = NULL;
pigment->initialize_fn = NULL;
- pigment->free_fn = NULL;
- pigment->trans = dmnsn_identity_matrix();
- pigment->quick_color = DMNSN_TCOLOR(dmnsn_black);
- pigment->initialized = false;
+ pigment->free_fn = dmnsn_default_pigment_free_fn;
+ pigment->trans = dmnsn_identity_matrix();
+ pigment->quick_color = DMNSN_TCOLOR(dmnsn_black);
+ pigment->initialized = false;
DMNSN_REFCOUNT_INIT(pigment);
- return pigment;
}
/* Free a pigment */
@@ -45,10 +58,7 @@ void
dmnsn_delete_pigment(dmnsn_pigment *pigment)
{
if (DMNSN_DECREF(pigment)) {
- if (pigment->free_fn) {
- pigment->free_fn(pigment->ptr);
- }
- dmnsn_free(pigment);
+ pigment->free_fn(pigment);
}
}
diff --git a/libdimension/pigment_map.c b/libdimension/pigment_map.c
index b8d7b12..a3e5dfd 100644
--- a/libdimension/pigment_map.c
+++ b/libdimension/pigment_map.c
@@ -49,41 +49,43 @@ dmnsn_new_pigment_map(void)
return pigment_map;
}
-/** Payload for a pigment_map pigment. */
-typedef struct dmnsn_pigment_map_payload {
+/** Pigment map type. */
+typedef struct dmnsn_pigment_map {
+ dmnsn_pigment pigment;
dmnsn_pattern *pattern;
dmnsn_map *map;
dmnsn_pigment_map_flags flags;
-} dmnsn_pigment_map_payload;
+} dmnsn_pigment_map;
-/** Free a pigment_map payload. */
+/** Free a pigment map. */
static void
-dmnsn_delete_pigment_map_payload(void *ptr)
+dmnsn_pigment_map_free_fn(dmnsn_pigment *pigment)
{
- dmnsn_pigment_map_payload *payload = ptr;
- dmnsn_delete_map(payload->map);
- dmnsn_delete_pattern(payload->pattern);
- dmnsn_free(payload);
+ dmnsn_pigment_map *pigment_map = (dmnsn_pigment_map *)pigment;
+ dmnsn_delete_map(pigment_map->map);
+ dmnsn_delete_pattern(pigment_map->pattern);
+ dmnsn_free(pigment_map);
}
/** pigment_map pigment callback. */
static dmnsn_tcolor
dmnsn_pigment_map_pigment_fn(const dmnsn_pigment *pigment, dmnsn_vector v)
{
- const dmnsn_pigment_map_payload *payload = pigment->ptr;
+ const dmnsn_pigment_map *pigment_map = (const dmnsn_pigment_map *)pigment;
double n;
dmnsn_pigment *pigment1, *pigment2;
- dmnsn_map_evaluate(payload->map, dmnsn_pattern_value(payload->pattern, v),
+ dmnsn_map_evaluate(pigment_map->map,
+ dmnsn_pattern_value(pigment_map->pattern, v),
&n, &pigment1, &pigment2);
dmnsn_tcolor color1 = dmnsn_pigment_evaluate(pigment1, v);
dmnsn_tcolor color2 = dmnsn_pigment_evaluate(pigment2, v);
- if (payload->flags == DMNSN_PIGMENT_MAP_SRGB) {
+ if (pigment_map->flags == DMNSN_PIGMENT_MAP_SRGB) {
color1.c = dmnsn_color_to_sRGB(color1.c);
color2.c = dmnsn_color_to_sRGB(color2.c);
}
dmnsn_tcolor ret = dmnsn_tcolor_gradient(color1, color2, n);
- if (payload->flags == DMNSN_PIGMENT_MAP_SRGB) {
+ if (pigment_map->flags == DMNSN_PIGMENT_MAP_SRGB) {
ret.c = dmnsn_color_from_sRGB(ret.c);
}
@@ -94,24 +96,23 @@ dmnsn_pigment_map_pigment_fn(const dmnsn_pigment *pigment, dmnsn_vector v)
static void
dmnsn_pigment_map_initialize_fn(dmnsn_pigment *pigment)
{
- dmnsn_pigment_map_payload *payload = pigment->ptr;
- dmnsn_map_apply(payload->map, dmnsn_initialize_mapped_pigment);
+ dmnsn_pigment_map *pigment_map = (dmnsn_pigment_map *)pigment;
+ dmnsn_map_apply(pigment_map->map, dmnsn_initialize_mapped_pigment);
}
dmnsn_pigment *
dmnsn_new_pigment_map_pigment(dmnsn_pattern *pattern, dmnsn_map *map,
dmnsn_pigment_map_flags flags)
{
- dmnsn_pigment *pigment = dmnsn_new_pigment();
+ dmnsn_pigment_map *pigment_map = DMNSN_MALLOC(dmnsn_pigment_map);
+ pigment_map->pattern = pattern;
+ pigment_map->map = map;
+ pigment_map->flags = flags;
- dmnsn_pigment_map_payload *payload = DMNSN_MALLOC(dmnsn_pigment_map_payload);
- payload->pattern = pattern;
- payload->map = map;
- payload->flags = flags;
-
- pigment->pigment_fn = dmnsn_pigment_map_pigment_fn;
+ dmnsn_pigment *pigment = &pigment_map->pigment;
+ dmnsn_init_pigment(pigment);
+ pigment->pigment_fn = dmnsn_pigment_map_pigment_fn;
pigment->initialize_fn = dmnsn_pigment_map_initialize_fn;
- pigment->free_fn = dmnsn_delete_pigment_map_payload;
- pigment->ptr = payload;
+ pigment->free_fn = dmnsn_pigment_map_free_fn;
return pigment;
}