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_gif.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, "gif_test_dec_file.raw")
37 #define DECODE_MEM_PATH tzplatform_mkpath(TZ_USER_CONTENT, "gif_test_dec_mem.raw")
38 #define ENCODE_FILE_PATH tzplatform_mkpath(TZ_USER_CONTENT, "gif_test_enc_file.gif")
39 #define ENCODE_MEM_PATH tzplatform_mkpath(TZ_USER_CONTENT, "gif_test_enc_mem.gif")
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;
65 /* for reading file */
66 static void *g_readed_data = NULL;
67 static size_t g_readed_size = 0;
69 static mm_util_image_h g_decoded_data = NULL;
71 static gboolean _read_file(char *path, void **data, size_t *length)
76 if (!path || !data || length == 0) {
77 fprintf(stderr, "\t[GIF_testsuite] invalid data %s %p %p\n", path, data, length);
81 fprintf(stderr, "\t[GIF_testsuite] %s read\n", path);
83 fp = fopen(path, "r");
85 fprintf(stderr, "\t[GIF_testsuite] fopen failed (%d) \n", errno);
89 if (fseek(fp, 0, SEEK_END) < 0) {
90 fprintf(stderr, "\t[GIF_testsuite] fseek failed \n");
97 fprintf(stderr, "\t[GIF_testsuite] ftell failed \n");
103 *data = (void *)calloc(1, len);
105 fprintf(stderr, "\tmemory allocation failed \n");
110 *length = fread(*data, 1, (size_t)len, fp);
111 if (*length != len) {
112 fprintf(stderr, "\t[GIF_testsuite] fread failed \n");
122 *length = (size_t)len;
124 fprintf(stderr, "\t[GIF_testsuite] %s %zu read DONE\n", path, *length);
129 static gboolean _write_file(const char *path, void *data, size_t length)
134 if (!path || !data || length == 0) {
135 fprintf(stderr, "\t[GIF_testsuite] invalid data %s %p %zu\n", path, data, length);
139 fprintf(stderr, "\t[GIF_testsuite] %s %p %zu write\n", path, data, length);
141 fp = fopen(path, "w");
143 fprintf(stderr, "\t[GIF_testsuite] fopen failed (%d) \n", errno);
147 len = fwrite(data, 1, length, fp);
149 fprintf(stderr, "\t[GIF_testsuite] fwrite failed \n");
155 fprintf(stderr, "\t[GIF_testsuite] %s write DONE\n", path);
160 gboolean _get_input_data(const char *argv, const long min, const long max, int *data)
162 if (argv == NULL || strlen(argv) == 0)
165 long temp = g_ascii_strtoll(argv, NULL, 10);
166 if (temp < min || temp > max)
174 void _print_help(const char *argv0)
176 fprintf(stderr, "\t[usage]\n");
177 fprintf(stderr, "\t\t1. decode & encode : %s mode path\n", argv0);
178 fprintf(stderr, "\t\t2. decode : %s mode path\n", argv0);
179 fprintf(stderr, "\t\t3. encode : %s mode path width height\n", argv0);
180 fprintf(stderr, "\t\t4. mode : 0 - auto, 1 - decode from file, 2 - decode from memory, 3 - encode to file, 4 - encode to memeory\n");
183 gboolean _get_arguments(int argc, char *argv[])
185 int width = 0, height = 0;
187 if (FALSE == _get_input_data(argv[1], TEST_AUTO, TEST_NUM - 1, &g_test_mode)) {
188 fprintf(stderr, "\t[GIF_testsuite] wrong mode(%s) for test\n", argv[1]);
189 _print_help(argv[0]);
193 g_path = g_strdup(argv[2]);
194 if (g_path == NULL) {
195 fprintf(stderr, "\t[GIF_testsuite] Not enough memory\n");
199 if ((g_test_mode == TEST_AUTO) || (g_test_mode == TEST_DECODE_FILE) || (g_test_mode == TEST_DECODE_MEMORY)) {
201 } else if (g_test_mode == TEST_ENCODE_FILE || g_test_mode == TEST_ENCODE_MEMORY) {
203 fprintf(stderr, "\t[GIF_testsuite] not enough args\n");
204 _print_help(argv[0]);
207 if (FALSE == _get_input_data(argv[3], 0, INT_MAX, &width)) {
208 fprintf(stderr, "\t[GIF_testsuite] wrong width %s\n", argv[3]);
211 g_width = (unsigned int)width;
212 if (FALSE == _get_input_data(argv[4], 0, INT_MAX, &height)) {
213 fprintf(stderr, "\t[GIF_testsuite] wrong height %s\n", argv[4]);
216 g_height = (unsigned int)height;
218 fprintf(stderr, "\t[GIF_testsuite] wrong mode for test %s\n", argv[1]);
225 gboolean _test_decode(const bmp_test_mode_e mode)
228 unsigned char *data = NULL;
231 /* test decoding gif */
232 if (mode == TEST_DECODE_FILE) {
233 ret = mm_util_decode_from_gif_file(g_path, &g_decoded_data);
234 if (ret != MM_UTIL_ERROR_NONE) {
235 fprintf(stderr, "\t[GIF_testsuite] mm_util_decode_from_bmp_file failed %d\n", ret);
238 ret = mm_image_get_image(g_decoded_data, &g_width, &g_height, NULL, &data, &size);
239 if (ret != MM_UTIL_ERROR_NONE) {
240 fprintf(stderr, "\t[GIF_testsuite] mm_image_get_image failed %d\n", ret);
243 if (FALSE == _write_file(DECODE_FILE_PATH, data, size)) {
244 fprintf(stderr, "\t[GIF_testsuite] writing decoded data failed : %s\n", DECODE_FILE_PATH);
249 } else if (mode == TEST_DECODE_MEMORY) {
250 if (FALSE == _read_file(g_path, &g_readed_data, &g_readed_size)) {
251 fprintf(stderr, "\t[GIF_testsuite] reading file error\n");
255 ret = mm_util_decode_from_gif_memory(g_readed_data, g_readed_size, &g_decoded_data);
256 if (ret != MM_UTIL_ERROR_NONE) {
257 fprintf(stderr, "\t[GIF_testsuite] mm_util_decode_from_bmp_memory failed %d\n", ret);
260 ret = mm_image_get_image(g_decoded_data, &g_width, &g_height, NULL, &data, &size);
261 if (ret != MM_UTIL_ERROR_NONE) {
262 fprintf(stderr, "\t[GIF_testsuite] mm_image_get_image failed %d\n", ret);
265 if (FALSE == _write_file(DECODE_MEM_PATH, data, size)) {
266 fprintf(stderr, "\t[GIF_testsuite] writing decoded data failed : %s\n", DECODE_MEM_PATH);
276 gboolean _test_encode(const bmp_test_mode_e mode)
279 /* for encoding gif to memory */
280 void *encoded_data = NULL;
281 size_t encoded_size = 0;
283 if ((mode != TEST_ENCODE_FILE) && (mode != TEST_ENCODE_MEMORY))
286 if (FALSE == _read_file(g_path, &g_readed_data, &g_readed_size)) {
287 fprintf(stderr, "\t[GIF_testsuite] reading file error\n");
290 ret = mm_image_create_image(g_width, g_height, MM_UTIL_COLOR_RGBA, (unsigned char *)g_readed_data, g_readed_size, &g_decoded_data);
291 if (ret != MM_UTIL_ERROR_NONE) {
292 fprintf(stderr, "\t[GIF_testsuite] mm_image_create_image failed : %d\n", ret);
296 /* test encoding gif */
297 if (mode == TEST_ENCODE_FILE) {
298 ret = mm_util_encode_to_gif_file(&g_decoded_data, 1, ENCODE_FILE_PATH);
299 if (ret != MM_UTIL_ERROR_NONE) {
300 fprintf(stderr, "\t[GIF_testsuite] mm_util_encode_gif_to_file failed : %d\n", ret);
303 } else if (mode == TEST_ENCODE_MEMORY) {
304 ret = mm_util_encode_to_gif_memory(&g_decoded_data, 1, &encoded_data, &encoded_size);
305 if (ret != MM_UTIL_ERROR_NONE) {
306 fprintf(stderr, "\t[GIF_testsuite] mm_util_encode_gif_to_memory failed : %d\n", ret);
307 SAFE_FREE(encoded_data);
310 if (FALSE == _write_file(ENCODE_MEM_PATH, encoded_data, encoded_size)) {
311 fprintf(stderr, "\t[GIF_testsuite] writing decoded data failed : %s\n", ENCODE_MEM_PATH);
312 SAFE_FREE(encoded_data);
317 SAFE_FREE(encoded_data);
321 gboolean _test_auto()
323 bmp_test_mode_e test_mode = TEST_DECODE_FILE;
325 while (test_mode < TEST_NUM) {
326 fprintf(stderr, "\t[GIF_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST START\n", MODE_TO_STR[test_mode]);
327 if (TEST_ENCODE_FILE == test_mode) {
329 g_path = g_strdup(DECODE_FILE_PATH);
330 if (g_path == NULL) {
331 fprintf(stderr, "\t[GIF_testsuite] Not enough memory\n");
335 /* test decoding gif */
336 if ((test_mode == TEST_DECODE_FILE) || (test_mode == TEST_DECODE_MEMORY)) {
337 if (FALSE == _test_decode(test_mode)) {
338 fprintf(stderr, "\t[GIF_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST FAIL\n", MODE_TO_STR[test_mode]);
343 /* test encoding gif */
344 if ((test_mode == TEST_ENCODE_FILE) || (test_mode == TEST_ENCODE_MEMORY)) {
345 if (FALSE == _test_encode(test_mode)) {
346 fprintf(stderr, "\t[GIF_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST FAIL\n", MODE_TO_STR[test_mode]);
351 fprintf(stderr, "\t[GIF_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST SUCCESS\n", MODE_TO_STR[test_mode]);
353 SAFE_FREE(g_readed_data);
354 SAFE_IMAGE_FREE(g_decoded_data);
361 int main(int argc, char *argv[])
364 _print_help(argv[0]);
368 if (FALSE == _get_arguments(argc, argv)) {
369 fprintf(stderr, "\t[GIF_testsuite] _get_arguments failed\n");
373 /* test all functions automatically */
374 if (g_test_mode == TEST_AUTO) {
375 if (FALSE == _test_auto())
376 fprintf(stderr, "\t[GIF_testsuite] _test_auto failed\n");
380 /* test decoding gif */
381 if ((g_test_mode == TEST_DECODE_FILE) || (g_test_mode == TEST_DECODE_MEMORY)) {
382 if (FALSE == _test_decode(g_test_mode)) {
383 fprintf(stderr, "\t[GIF_testsuite] _test_decode(%s) failed\n", MODE_TO_STR[g_test_mode]);
388 /* test encoding gif */
389 if ((g_test_mode == TEST_ENCODE_FILE) || (g_test_mode == TEST_ENCODE_MEMORY)) {
390 if (FALSE == _test_encode(g_test_mode)) {
391 fprintf(stderr, "\t[GIF_testsuite] _test_encode(%s) failed\n", MODE_TO_STR[g_test_mode]);
398 SAFE_FREE(g_readed_data);
399 SAFE_IMAGE_FREE(g_decoded_data);