Skip to content

Commit 4d76491

Browse files
authored
Merge pull request #124 from saxbophone/josh/113-refactor-render-backends
Re-organise and refactor the rendering units
2 parents b627b88 + 715091a commit 4d76491

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

saxbospiral/render.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ static void get_bounds(sxbp_spiral_t spiral, sxbp_co_ord_t* bounds) {
8383
* - That image->pixels is NULL
8484
* - That spiral.lines is not NULL
8585
*/
86-
sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
86+
sxbp_status_t sxbp_render_spiral_raw(
87+
sxbp_spiral_t spiral, sxbp_bitmap_t* image
88+
) {
8789
// preconditional assertions
8890
assert(image->pixels == NULL);
8991
assert(spiral.lines != NULL);
@@ -159,6 +161,40 @@ sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
159161
return result;
160162
}
161163

164+
/*
165+
* given a spiral struct, a pointer to a blank buffer and a pointer to a
166+
* function with the appropriate signature, render the spiral as a bitmap and
167+
* then call the pointed-to function to render the image to the buffer (in
168+
* whatever format the function pointer is written for)
169+
* returns a status struct with error information (if any)
170+
*
171+
* Asserts:
172+
* - That spiral.lines is not NULL
173+
* - That buffer->bytes is NULL
174+
* - That the function pointer is not NULL
175+
*/
176+
sxbp_status_t sxbp_render_spiral_image(
177+
sxbp_spiral_t spiral, sxbp_buffer_t* buffer,
178+
sxbp_status_t(* image_writer_callback)(
179+
sxbp_bitmap_t image, sxbp_buffer_t* buffer
180+
)
181+
) {
182+
// preconditional assertions
183+
assert(spiral.lines != NULL);
184+
assert(buffer->bytes == NULL);
185+
assert(image_writer_callback != NULL);
186+
// create bitmap to render raw image to
187+
sxbp_bitmap_t raw_image = {0, 0, NULL};
188+
// render spiral to raw image (and store success/failure)
189+
sxbp_status_t result = sxbp_render_spiral_raw(spiral, &raw_image);
190+
// check return status
191+
if(result != SXBP_OPERATION_OK) {
192+
return result;
193+
}
194+
// render to buffer using callback (and return its status code)
195+
return image_writer_callback(raw_image, buffer);
196+
}
197+
162198
#ifdef __cplusplus
163199
} // extern "C"
164200
#endif

saxbospiral/render.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,28 @@ typedef struct sxbp_bitmap_t {
4848
* - That image->pixels is NULL
4949
* - That spiral.lines is not NULL
5050
*/
51-
sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image);
51+
sxbp_status_t sxbp_render_spiral_raw(
52+
sxbp_spiral_t spiral, sxbp_bitmap_t* image
53+
);
54+
55+
/*
56+
* given a spiral struct, a pointer to a blank buffer and a pointer to a
57+
* function with the appropriate signature, render the spiral as a bitmap and
58+
* then call the pointed-to function to render the image to the buffer (in
59+
* whatever format the function pointer is written for)
60+
* returns a status struct with error information (if any)
61+
*
62+
* Asserts:
63+
* - That spiral.lines is not NULL
64+
* - That buffer->bytes is NULL
65+
* - That the function pointer is not NULL
66+
*/
67+
sxbp_status_t sxbp_render_spiral_image(
68+
sxbp_spiral_t spiral, sxbp_buffer_t* buffer,
69+
sxbp_status_t(* image_writer_callback)(
70+
sxbp_bitmap_t image, sxbp_buffer_t* buffer
71+
)
72+
);
5273

5374
#ifdef __cplusplus
5475
} // extern "C"

saxbospiral/render_backends/png_backend.c renamed to saxbospiral/render_backends/backend_png.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* This source file forms part of libsaxbospiral, a library which generates
33
* experimental 2D spiral-like shapes based on input binary data.
44
*
5-
* This compilation unit provides functionality to render a bitmap struct to PNG
5+
* This compilation unit provides functionality to render a bitmap struct to a
6+
* PNG image (stored in a buffer).
67
*
78
*
89
*
@@ -28,7 +29,7 @@
2829

2930
#include "../saxbospiral.h"
3031
#include "../render.h"
31-
#include "png_backend.h"
32+
#include "backend_png.h"
3233

3334

3435
#ifdef __cplusplus
@@ -89,7 +90,7 @@ static void cleanup_png_lib(
8990
* - That bitmap.pixels is not NULL
9091
* - That buffer->bytes is NULL
9192
*/
92-
sxbp_status_t sxbp_write_png_image(
93+
sxbp_status_t sxbp_render_backend_png(
9394
sxbp_bitmap_t bitmap, sxbp_buffer_t* buffer
9495
) {
9596
// preconditional assertsions

saxbospiral/render_backends/png_backend.h renamed to saxbospiral/render_backends/backend_png.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* This source file forms part of libsaxbospiral, a library which generates
33
* experimental 2D spiral-like shapes based on input binary data.
44
*
5-
* This compilation unit provides functionality to render a bitmap struct to PNG
5+
* This compilation unit provides functionality to render a bitmap struct to a
6+
* PNG image (stored in a buffer).
67
*
78
*
89
*
@@ -20,8 +21,8 @@
2021
* You should have received a copy of the GNU Affero General Public License
2122
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2223
*/
23-
#ifndef SAXBOPHONE_SAXBOSPIRAL_PNG_BACKEND_H
24-
#define SAXBOPHONE_SAXBOSPIRAL_PNG_BACKEND_H
24+
#ifndef SAXBOPHONE_SAXBOSPIRAL_BACKEND_PNG_H
25+
#define SAXBOPHONE_SAXBOSPIRAL_BACKEND_PNG_H
2526

2627
#include "../saxbospiral.h"
2728
#include "../render.h"
@@ -40,7 +41,9 @@ extern "C"{
4041
* - That bitmap.pixels is not NULL
4142
* - That buffer->bytes is NULL
4243
*/
43-
sxbp_status_t sxbp_write_png_image(sxbp_bitmap_t bitmap, sxbp_buffer_t* buffer);
44+
sxbp_status_t sxbp_render_backend_png(
45+
sxbp_bitmap_t bitmap, sxbp_buffer_t* buffer
46+
);
4447

4548
#ifdef __cplusplus
4649
} // extern "C"

0 commit comments

Comments
 (0)