7 #include <tbm_surface.h>
8 #include <tbm_surface_internal.h>
12 #include "tdm_private.h"
16 static const char *dump_prefix[2] = {"png", "yuv"};
19 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
20 int size2, void *data3, int size3)
23 FILE *fp = fopen(file, "w+");
24 TDM_RETURN_IF_FAIL(fp != NULL);
26 blocks = (unsigned int *)data1;
27 fwrite(blocks, 1, size1, fp);
30 blocks = (unsigned int *)data2;
31 fwrite(blocks, 1, size2, fp);
35 blocks = (unsigned int *)data3;
36 fwrite(blocks, 1, size3, fp);
43 _tdm_helper_dump_png(const char *file, const void *data, int width,
46 FILE *fp = fopen(file, "wb");
47 TDM_RETURN_IF_FAIL(fp != NULL);
49 png_structp pPngStruct =
50 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
56 png_infop pPngInfo = png_create_info_struct(pPngStruct);
58 png_destroy_write_struct(&pPngStruct, NULL);
63 png_init_io(pPngStruct, fp);
64 png_set_IHDR(pPngStruct,
71 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
73 png_set_bgr(pPngStruct);
74 png_write_info(pPngStruct, pPngInfo);
76 const int pixel_size = 4; // RGBA
77 png_bytep *row_pointers =
78 png_malloc(pPngStruct, height * sizeof(png_byte *));
80 unsigned int *blocks = (unsigned int *)data;
84 for (; y < height; ++y) {
86 png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
87 row_pointers[y] = (png_bytep)row;
88 for (x = 0; x < width; ++x) {
89 unsigned int curBlock = blocks[y * width + x];
90 row[x * pixel_size] = (curBlock & 0xFF);
91 row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
92 row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
93 row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
97 png_write_image(pPngStruct, row_pointers);
98 png_write_end(pPngStruct, pPngInfo);
100 for (y = 0; y < height; y++)
101 png_free(pPngStruct, row_pointers[y]);
102 png_free(pPngStruct, row_pointers);
104 png_destroy_write_struct(&pPngStruct, &pPngInfo);
110 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
112 tbm_surface_info_s info;
116 TDM_RETURN_IF_FAIL(buffer != NULL);
117 TDM_RETURN_IF_FAIL(file != NULL);
119 ret = tbm_surface_map(buffer, TBM_DEVICE_CPU, &info);
120 TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
122 len = strnlen(file, 1024);
123 if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
124 prefix = dump_prefix[0];
126 prefix = dump_prefix[1];
128 if (strncmp(file + (len - 3), prefix, 3)) {
129 TDM_ERR("can't dump to '%s' file", file + (len - 3));
130 tbm_surface_unmap(buffer);
134 switch (info.format) {
135 case TBM_FORMAT_ARGB8888:
136 case TBM_FORMAT_XRGB8888:
137 _tdm_helper_dump_png(file, info.planes[0].ptr,
138 info.planes[0].stride >> 2, info.height);
140 case TBM_FORMAT_YVU420:
141 case TBM_FORMAT_YUV420:
142 _tdm_helper_dump_raw(file,
144 info.planes[0].stride * info.height,
146 info.planes[1].stride * (info.height >> 1),
148 info.planes[2].stride * (info.height >> 1));
150 case TBM_FORMAT_NV12:
151 case TBM_FORMAT_NV21:
152 _tdm_helper_dump_raw(file,
154 info.planes[0].stride * info.height,
156 info.planes[1].stride * (info.height >> 1), NULL,
159 case TBM_FORMAT_YUYV:
160 case TBM_FORMAT_UYVY:
161 _tdm_helper_dump_raw(file,
163 info.planes[0].stride * info.height, NULL, 0,
167 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR (info.format));
168 tbm_surface_unmap(buffer);
172 tbm_surface_unmap(buffer);
174 TDM_INFO("dump %s", file);