summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2019-07-17 03:51:11 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2019-07-17 03:51:11 (EDT)
commit8ac67db24ff4c8a1364c3cc3278fe75642514706 (patch)
tree7b82ee41881f7d9a74652126ffb5c4c9c33616d2
parent1636c5d78f53e9b71f108a2c40aeec1f56bb05b8 (diff)
image_render(): Allocate and free row buffers
Move code from image_new() and image_destroy() and drop rows member from struct image.
-rw-r--r--src/image.c37
1 files changed, 19 insertions, 18 deletions
diff --git a/src/image.c b/src/image.c
index 3b653f8..b2cefc9 100644
--- a/src/image.c
+++ b/src/image.c
@@ -35,7 +35,6 @@
struct image {
png_structp png_ptr;
png_infop info_ptr;
- png_bytepp rows;
};
static png_color palette[PALETTE_SIZE];
@@ -45,9 +44,6 @@ image_new(const char *file_name)
{
struct image *image;
FILE *fp;
- size_t i;
- size_t width = 32;
- size_t height = 32;
image = calloc(1, sizeof(*image));
if (!image) {
@@ -73,39 +69,44 @@ image_new(const char *file_name)
png_init_io(image->png_ptr, fp);
+ return image;
+}
+
+void
+image_render(struct image *image)
+{
+ size_t width = 32;
+ size_t height = 32;
+ png_bytepp rows;
+ size_t i;
+
png_set_IHDR(image->png_ptr, image->info_ptr, width, height,
BIT_DEPTH, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_set_PLTE(image->png_ptr, image->info_ptr, palette, PALETTE_SIZE);
- image->rows = png_malloc(image->png_ptr, height * sizeof(*image->rows));
+ rows = png_malloc(image->png_ptr, height * sizeof(*rows));
for (i = 0; i < height; ++i) {
- image->rows[i] = png_malloc(image->png_ptr, width * PIXEL_SIZE);
+ rows[i] = png_malloc(image->png_ptr, width * PIXEL_SIZE);
}
- png_set_rows(image->png_ptr, image->info_ptr, image->rows);
+ png_set_rows(image->png_ptr, image->info_ptr, rows);
- return image;
-}
-
-void
-image_render(struct image *image)
-{
png_write_png(image->png_ptr, image->info_ptr, PNG_TRANSFORM_IDENTITY,
NULL);
+
+ for (i = 0; i < height; ++i) {
+ png_free(image->png_ptr, rows[i]);
+ }
+ png_free(image->png_ptr, rows);
}
struct image *
image_destroy(struct image **image_p)
{
struct image *image;
- size_t i;
image = *image_p;
- for (i = 0; i < 32; ++i) {
- png_free(image->png_ptr, image->rows[i]);
- }
- png_free(image->png_ptr, image->rows);
png_destroy_write_struct(&image->png_ptr, &image->info_ptr);
free(image);
return image = NULL;