From 1722db77070dd3df740aa424575103a83f160994 Mon Sep 17 00:00:00 2001 From: Junkyeong Kim Date: Wed, 25 Sep 2019 18:51:57 +0900 Subject: [PATCH] Revert "tbm_surface_internal: Removes avoidable malloc() for png dump" This reverts commit 019b0a2750ab389bf26575caf8cbf832b64bc64b. Change-Id: Iae72cb1137b63fe0e176d3ff80e168d3c09d91ed Signed-off-by: Junkyeong Kim --- src/tbm_surface_internal.c | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/tbm_surface_internal.c b/src/tbm_surface_internal.c index 2da84bd..659b19e 100644 --- a/src/tbm_surface_internal.c +++ b/src/tbm_surface_internal.c @@ -1767,12 +1767,13 @@ _tbm_surface_internal_dump_file_raw(const char *file, void *data1, int size1, } static void -_tbm_surface_internal_dump_file_png(const char *file, void *data, int width, int height, int format) +_tbm_surface_internal_dump_file_png(const char *file, const void *data, int width, int height, int format) { + unsigned int *blocks = (unsigned int *)data; FILE *fp; int pixel_size; png_bytep *row_pointers; - int depth = 8; + int depth = 8, y; if (_tbm_surface_check_file_is_symbolic_link(file)) TBM_ERR("%s is symbolic link\n", file); @@ -1830,12 +1831,43 @@ _tbm_surface_internal_dump_file_png(const char *file, void *data, int width, int return; } - for (int y = 0; y < height; ++y) - row_pointers[y] = data + width * pixel_size * y; + for (y = 0; y < height; ++y) { + png_bytep row; + int x = 0; + + row = png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size); + if (!row) { + TBM_ERR("fail to allocate the png row.\n"); + for (x = 0; x < y; x++) + png_free(pPngStruct, row_pointers[x]); + png_free(pPngStruct, row_pointers); + png_destroy_write_struct(&pPngStruct, &pPngInfo); + fclose(fp); + return; + } + row_pointers[y] = (png_bytep)row; + + for (x = 0; x < width; ++x) { + unsigned int curBlock = blocks[y * width + x]; + + if (pixel_size == 3) { // XRGB8888 + row[x * pixel_size] = (curBlock & 0xFF); + row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF; + row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF; + } else { // ARGB8888 + row[x * pixel_size] = (curBlock & 0xFF); + row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF; + row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF; + row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF; + } + } + } png_write_image(pPngStruct, row_pointers); png_write_end(pPngStruct, pPngInfo); + for (y = 0; y < height; y++) + png_free(pPngStruct, row_pointers[y]); png_free(pPngStruct, row_pointers); png_destroy_write_struct(&pPngStruct, &pPngInfo); -- 2.7.4