4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Vineeth T M <vineeth.tm@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
28 #include <mm_util_png.h>
29 #include <mm_util_image.h>
30 #include <tzplatform_config.h>
32 #define SAFE_FREE(x) { if (x != NULL) { free(x); x = NULL; } }
33 #define SAFE_G_FREE(x) { if (x != NULL) { g_free(x); x = NULL; } }
34 #define SAFE_IMAGE_FREE(x) { if (x != NULL) { mm_image_destroy_image(x); x = NULL; } }
36 #define DECODE_FILE_PATH tzplatform_mkpath(TZ_USER_CONTENT, "png_test_dec_file.raw")
37 #define DECODE_MEM_PATH tzplatform_mkpath(TZ_USER_CONTENT, "png_test_dec_mem.raw")
38 #define ENCODE_FILE_PATH tzplatform_mkpath(TZ_USER_CONTENT, "png_test_enc_file.png")
39 #define ENCODE_MEM_PATH tzplatform_mkpath(TZ_USER_CONTENT, "png_test_enc_mem.png")
50 static char *MODE_TO_STR[] = {
60 static int g_test_mode = 0;
61 static char *g_path = NULL;
62 static unsigned int g_width = 0;
63 static unsigned int g_height = 0;
64 static int g_compression = MM_UTIL_COMPRESSION_6;
66 /* for reading file */
67 static void *g_readed_data = NULL;
68 static size_t g_readed_size = 0;
70 static mm_util_image_h g_decoded_data = NULL;
72 static gboolean _read_file(char *path, void **data, size_t *length)
77 if (!path || !data || length == 0) {
78 fprintf(stderr, "\t[PNG_testsuite] invalid data %s %p %p\n", path, data, length);
82 fprintf(stderr, "\t[PNG_testsuite] %s read\n", path);
84 fp = fopen(path, "r");
86 fprintf(stderr, "\t[PNG_testsuite] fopen failed (%d) \n", errno);
90 if (fseek(fp, 0, SEEK_END) < 0) {
91 fprintf(stderr, "\t[PNG_testsuite] fseek failed \n");
98 fprintf(stderr, "\t[PNG_testsuite] ftell failed \n");
104 *data = (void *)calloc(1, len);
106 fprintf(stderr, "\tmemory allocation failed \n");
111 *length = fread(*data, 1, (size_t)len, fp);
112 if (*length != len) {
113 fprintf(stderr, "\t[PNG_testsuite] fread failed \n");
123 *length = (size_t)len;
125 fprintf(stderr, "\t[PNG_testsuite] %s %zu read DONE\n", path, *length);
130 static gboolean _write_file(const char *path, void *data, size_t length)
135 if (!path || !data || length == 0) {
136 fprintf(stderr, "\t[PNG_testsuite] invalid data %s %p %zu\n", path, data, length);
140 fprintf(stderr, "\t[PNG_testsuite] %s %p %zu write\n", path, data, length);
142 fp = fopen(path, "w");
144 fprintf(stderr, "\t[PNG_testsuite] fopen failed (%d) \n", errno);
148 len = fwrite(data, 1, length, fp);
150 fprintf(stderr, "\t[PNG_testsuite] fwrite failed \n");
156 fprintf(stderr, "\t[PNG_testsuite] %s write DONE\n", path);
161 gboolean _get_input_data(const char *argv, const long min, const long max, int *data)
163 if (argv == NULL || strlen(argv) == 0)
166 long temp = g_ascii_strtoll(argv, NULL, 10);
167 if (temp < min || temp > max)
175 void _print_help(const char *argv0)
177 fprintf(stderr, "\t[usage]\n");
178 fprintf(stderr, "\t\t1. decode & encode : %s mode path compression_level(opt.)\n", argv0);
179 fprintf(stderr, "\t\t2. decode : %s mode path\n", argv0);
180 fprintf(stderr, "\t\t3. encode : %s mode path width(mand.) height(mand.) compression_level(opt.)\n", argv0);
181 fprintf(stderr, "\t\t4. mode : 0 - auto, 1 - decode from file, 2 - decode from memory, 3 - encode to file, 4 - encode to memeory\n");
184 gboolean _get_arguments(int argc, char *argv[])
186 int width = 0, height = 0;
188 if (FALSE == _get_input_data(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
189 fprintf(stderr, "\t[PNG_testsuite] wrong mode(%s) for test\n", argv[1]);
190 _print_help(argv[0]);
194 g_path = g_strdup(argv[2]);
195 if (g_path == NULL) {
196 fprintf(stderr, "\t[PNG_testsuite] Not enough memory\n");
200 if (g_test_mode == TEST_AUTO) {
201 if (FALSE == _get_input_data(argv[3], MM_UTIL_COMPRESSION_1, MM_UTIL_COMPRESSION_9, &g_compression))
202 fprintf(stderr, "\t[PNG_testsuite] compression_level is default(%d)\n", g_compression);
203 } else if ((g_test_mode == TEST_DECODE_FILE) || (g_test_mode == TEST_DECODE_MEMORY)) {
205 } else if (g_test_mode == TEST_ENCODE_FILE || g_test_mode == TEST_ENCODE_MEMORY) {
207 fprintf(stderr, "\t[PNG_testsuite] not enough args\n");
208 _print_help(argv[0]);
211 if (FALSE == _get_input_data(argv[3], 0, INT_MAX, &width)) {
212 fprintf(stderr, "\t[PNG_testsuite] wrong width %s\n", argv[3]);
215 g_width = (unsigned int)width;
216 if (FALSE == _get_input_data(argv[4], 0, INT_MAX, &height)) {
217 fprintf(stderr, "\t[PNG_testsuite] wrong height %s\n", argv[4]);
220 g_height = (unsigned int)height;
221 if (FALSE == _get_input_data(argv[5], MM_UTIL_COMPRESSION_1, MM_UTIL_COMPRESSION_9, &g_compression))
222 fprintf(stderr, "\t[PNG_testsuite] wrong compression_level %s\n", argv[5]);
224 fprintf(stderr, "\t[PNG_testsuite] wrong mode for test %s\n", argv[1]);
231 gboolean _test_decode(const png_test_mode_e mode)
234 unsigned char *data = NULL;
237 /* test decoding png */
238 if (mode == TEST_DECODE_FILE) {
239 ret = mm_util_decode_from_png_file(g_path, &g_decoded_data);
240 if (ret != MM_UTIL_ERROR_NONE) {
241 fprintf(stderr, "\t[PNG_testsuite] mm_util_decode_from_png_file failed %d\n", ret);
244 ret = mm_image_get_image(g_decoded_data, &g_width, &g_height, NULL, &data, &size);
245 if (ret != MM_UTIL_ERROR_NONE) {
246 fprintf(stderr, "\t[PNG_testsuite] mm_image_get_image failed %d\n", ret);
249 if (FALSE == _write_file(DECODE_FILE_PATH, data, size)) {
250 fprintf(stderr, "\t[PNG_testsuite] writing decoded data failed : %s\n", DECODE_FILE_PATH);
255 } else if (mode == TEST_DECODE_MEMORY) {
256 if (FALSE == _read_file(g_path, &g_readed_data, &g_readed_size)) {
257 fprintf(stderr, "\t[PNG_testsuite] reading file error\n");
261 ret = mm_util_decode_from_png_memory(g_readed_data, g_readed_size, &g_decoded_data);
262 if (ret != MM_UTIL_ERROR_NONE) {
263 fprintf(stderr, "\t[PNG_testsuite] mm_util_decode_from_png_memory failed %d\n", ret);
266 ret = mm_image_get_image(g_decoded_data, &g_width, &g_height, NULL, &data, &size);
267 if (ret != MM_UTIL_ERROR_NONE) {
268 fprintf(stderr, "\t[PNG_testsuite] mm_image_get_image failed %d\n", ret);
271 if (FALSE == _write_file(DECODE_MEM_PATH, data, size)) {
272 fprintf(stderr, "\t[PNG_testsuite] writing decoded data failed : %s\n", DECODE_MEM_PATH);
282 gboolean _test_encode(const png_test_mode_e mode)
285 /* for encoding png to memory */
286 void *encoded_data = NULL;
287 size_t encoded_size = 0;
289 if ((mode != TEST_ENCODE_FILE) && (mode != TEST_ENCODE_MEMORY))
292 if (FALSE == _read_file(g_path, &g_readed_data, &g_readed_size)) {
293 fprintf(stderr, "\t[PNG_testsuite] reading file error\n");
296 ret = mm_image_create_image(g_width, g_height, MM_UTIL_COLOR_RGBA, (unsigned char *)g_readed_data,
297 g_readed_size, &g_decoded_data);
298 if (ret != MM_UTIL_ERROR_NONE) {
299 fprintf(stderr, "\t[PNG_testsuite] mm_image_create_image failed : %d\n", ret);
303 /* test encoding png */
304 if (mode == TEST_ENCODE_FILE) {
305 ret = mm_util_encode_to_png_file(g_decoded_data, g_compression, ENCODE_FILE_PATH);
306 if (ret != MM_UTIL_ERROR_NONE) {
307 fprintf(stderr, "\t[PNG_testsuite] mm_util_encode_to_png_file failed : %d\n", ret);
310 } else if (mode == TEST_ENCODE_MEMORY) {
311 ret = mm_util_encode_to_png_memory(g_decoded_data, g_compression, &encoded_data, &encoded_size);
312 if (ret != MM_UTIL_ERROR_NONE) {
313 fprintf(stderr, "\t[PNG_testsuite] mm_util_encode_to_png_memory failed : %d\n", ret);
314 SAFE_FREE(encoded_data);
317 if (FALSE == _write_file(ENCODE_MEM_PATH, encoded_data, (size_t)encoded_size)) {
318 fprintf(stderr, "\t[PNG_testsuite] writing decoded data failed : %s\n", ENCODE_MEM_PATH);
319 SAFE_FREE(encoded_data);
324 SAFE_FREE(encoded_data);
328 gboolean _test_auto()
330 png_test_mode_e test_mode = TEST_DECODE_FILE;
332 while (test_mode < TEST_NUM) {
333 fprintf(stderr, "\t[PNG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST START\n", MODE_TO_STR[test_mode]);
334 if (TEST_ENCODE_FILE == test_mode) {
336 g_path = g_strdup(DECODE_FILE_PATH);
337 if (g_path == NULL) {
338 fprintf(stderr, "\t[PNG_testsuite] Not enough memory\n");
342 /* test decoding png */
343 if ((test_mode == TEST_DECODE_FILE) || (test_mode == TEST_DECODE_MEMORY)) {
344 if (FALSE == _test_decode(test_mode)) {
345 fprintf(stderr, "\t[PNG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST FAIL\n", MODE_TO_STR[test_mode]);
350 /* test encoding png */
351 if ((test_mode == TEST_ENCODE_FILE) || (test_mode == TEST_ENCODE_MEMORY)) {
352 if (FALSE == _test_encode(test_mode)) {
353 fprintf(stderr, "\t[PNG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST FAIL\n", MODE_TO_STR[test_mode]);
358 fprintf(stderr, "\t[PNG_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST SUCCESS\n", MODE_TO_STR[test_mode]);
360 SAFE_FREE(g_readed_data);
361 SAFE_IMAGE_FREE(g_decoded_data);
368 int main(int argc, char *argv[])
371 _print_help(argv[0]);
375 if (FALSE == _get_arguments(argc, argv)) {
376 fprintf(stderr, "\t[PNG_testsuite] _get_arguments failed\n");
380 /* test all functions automatically */
381 if (g_test_mode == TEST_AUTO) {
382 if (FALSE == _test_auto())
383 fprintf(stderr, "\t[PNG_testsuite] _test_auto failed\n");
387 /* test decoding png */
388 if ((g_test_mode == TEST_DECODE_FILE) || (g_test_mode == TEST_DECODE_MEMORY)) {
389 if (FALSE == _test_decode(g_test_mode)) {
390 fprintf(stderr, "\t[PNG_testsuite] _test_decode(%s) failed\n", MODE_TO_STR[g_test_mode]);
395 /* test encoding png */
396 if ((g_test_mode == TEST_ENCODE_FILE) || (g_test_mode == TEST_ENCODE_MEMORY)) {
397 if (FALSE == _test_encode(g_test_mode)) {
398 fprintf(stderr, "\t[PNG_testsuite] _test_encode(%s) failed\n", MODE_TO_STR[g_test_mode]);
405 SAFE_FREE(g_readed_data);
406 SAFE_IMAGE_FREE(g_decoded_data);