Revert "tbm_surface_internal: Removes avoidable malloc() for png dump" 94/214694/1
authorJunkyeong Kim <jk0430.kim@samsung.com>
Wed, 25 Sep 2019 09:51:57 +0000 (18:51 +0900)
committerJunkyeong Kim <jk0430.kim@samsung.com>
Wed, 25 Sep 2019 09:52:12 +0000 (18:52 +0900)
This reverts commit 019b0a2750ab389bf26575caf8cbf832b64bc64b.

Change-Id: Iae72cb1137b63fe0e176d3ff80e168d3c09d91ed
Signed-off-by: Junkyeong Kim <jk0430.kim@samsung.com>
src/tbm_surface_internal.c

index 2da84bd..659b19e 100644 (file)
@@ -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);