tbm_surface_internal: Removes avoidable malloc() for png dump 49/213449/2
authorYoungJun Cho <yj44.cho@samsung.com>
Thu, 5 Sep 2019 10:06:04 +0000 (19:06 +0900)
committerChangyeon Lee <cyeon.lee@samsung.com>
Mon, 16 Sep 2019 05:00:08 +0000 (14:00 +0900)
This patch removes avoidable malloc() for png dump.

Change-Id: I7e64c7b5d14f752aa5c209138cefad9fe2f6bb3a
Signed-off-by: YoungJun Cho <yj44.cho@samsung.com>
src/tbm_surface_internal.c

index 4cc186fbb2eca067c56b7041bbc0825aed5a4f83..532d51f9fde210c84388149f8f702be270c7edfc 100644 (file)
@@ -1767,13 +1767,12 @@ _tbm_surface_internal_dump_file_raw(const char *file, void *data1, int size1,
 }
 
 static void
-_tbm_surface_internal_dump_file_png(const char *file, const void *data, int width, int height, int format)
+_tbm_surface_internal_dump_file_png(const char *file, 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, y;
+       int depth = 8;
 
        if (_tbm_surface_check_file_is_symbolic_link(file))
                TBM_ERR("%s is symbolic link\n", file);
@@ -1831,43 +1830,12 @@ _tbm_surface_internal_dump_file_png(const char *file, const void *data, int widt
                return;
        }
 
-       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;
-                       }
-               }
-       }
+       for (int y = 0; y < height; ++y)
+               row_pointers[y] = data + width * pixel_size * y;
 
        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);