summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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;