From 455f03d06b3860a5551ba4ea4a8690ebac982867 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Fri, 4 Dec 2009 16:58:54 +0000 Subject: Don't allocate rows individually. svn path=/trunk/tools/alphagen/; revision=9716 --- alphagen.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/alphagen.c b/alphagen.c index e6247fc..940cab8 100644 --- a/alphagen.c +++ b/alphagen.c @@ -12,7 +12,7 @@ * Input: * blackbg.png - The image exported on a black background * whitebg.png - The image exported on a white background - * Output + * Output: * alpha.png - The resultant image with alpha channel * * Note that black.png and white.png must be the same size. @@ -117,28 +117,27 @@ bool image_init(struct image *img, int width, int height, int channels) img->height = height; img->channels = channels; + row_data_width = width * channels; // Allocate memory for row pointers img->d = (uint8_t**)malloc(height * size); if (img->d == NULL) return false; - row_data_width = width * channels; - for (int i = 0; i < height; i++) { - // Allocate memory for row data - img->d[i] = (uint8_t*)malloc(row_data_width); - if (img->d[i] == NULL) - return false; + // Allocate memory for image data + img->d[0] = (uint8_t*)malloc(height * row_data_width * size); + if (img->d[0] == NULL) + return false; + + for (int i = 1; i < height; i++) { + // Set pointers to each row + img->d[i] = img->d[i - 1] + row_data_width; } return true; } void image_free(struct image *img) { - for (int i = 0; i < img->height; i++) { - // Free row data - free(img->d[i]); - } - // Free row pointers + free(img->d[0]); free(img->d); } -- cgit v1.2.3