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_bmp.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, "bmp_test_dec_file.raw")
37 #define DECODE_MEM_PATH tzplatform_mkpath(TZ_USER_CONTENT, "bmp_test_dec_mem.raw")
38 #define ENCODE_FILE_PATH tzplatform_mkpath(TZ_USER_CONTENT, "bmp_test_enc_file.bmp")
39 #define ENCODE_MEM_PATH tzplatform_mkpath(TZ_USER_CONTENT, "bmp_test_enc_mem.bmp")
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[BMP_testsuite] invalid data %s %p %p\n", path, data, length);
81 fprintf(stderr, "\t[BMP_testsuite] %s read\n", path);
83 fp = fopen(path, "r");
85 fprintf(stderr, "\t[BMP_testsuite] fopen failed (%d) \n", errno);
89 if (fseek(fp, 0, SEEK_END) < 0) {
90 fprintf(stderr, "\t[BMP_testsuite] fseek failed \n");
97 fprintf(stderr, "\t[BMP_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[BMP_testsuite] fread failed \n");
122 *length = (size_t)len;
124 fprintf(stderr, "\t[BMP_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[BMP_testsuite] invalid data %s %p %zu\n", path, data, length);
139 fprintf(stderr, "\t[BMP_testsuite] %s %p %zu write\n", path, data, length);
141 fp = fopen(path, "w");
143 fprintf(stderr, "\t[BMP_testsuite] fopen failed (%d) \n", errno);
147 len = fwrite(data, 1, length, fp);
149 fprintf(stderr, "\t[BMP_testsuite] fwrite failed \n");
155 fprintf(stderr, "\t[BMP_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[BMP_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[BMP_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[BMP_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[BMP_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[BMP_testsuite] wrong height %s\n", argv[4]);
216 g_height = (unsigned int)height;
218 fprintf(stderr, "\t[BMP_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 bmp */
232 if (mode == TEST_DECODE_FILE) {
233 ret = mm_util_decode_from_bmp_file(g_path, &g_decoded_data);
234 if (ret != MM_UTIL_ERROR_NONE) {
235 fprintf(stderr, "\t[BMP_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[BMP_testsuite] mm_image_get_image failed %d\n", ret);
243 if (FALSE == _write_file(DECODE_FILE_PATH, data, size)) {
244 fprintf(stderr, "\t[BMP_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[BMP_testsuite] reading file error\n");
255 ret = mm_util_decode_from_bmp_memory(g_readed_data, g_readed_size, &g_decoded_data);
256 if (ret != MM_UTIL_ERROR_NONE) {
257 fprintf(stderr, "\t[BMP_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[BMP_testsuite] mm_image_get_image failed %d\n", ret);
265 if (FALSE == _write_file(DECODE_MEM_PATH, data, size)) {
266 fprintf(stderr, "\t[BMP_testsuite] writing decoded data failed : %s\n", DECODE_MEM_PATH);
276 gboolean _test_encode(const bmp_test_mode_e mode)
279 /* for encoding bmp 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[BMP_testsuite] reading file error\n");
290 ret = mm_image_create_image(g_width, g_height, MM_UTIL_COLOR_RGBA, (unsigned char *)g_readed_data,
291 g_readed_size, &g_decoded_data);
292 if (ret != MM_UTIL_ERROR_NONE) {
293 fprintf(stderr, "\t[BMP_testsuite] mm_image_create_image failed : %d\n", ret);
297 /* test encoding bmp */
298 if (mode == TEST_ENCODE_FILE) {
299 ret = mm_util_encode_bmp_to_file(g_decoded_data, ENCODE_FILE_PATH);
300 if (ret != MM_UTIL_ERROR_NONE) {
301 fprintf(stderr, "\t[BMP_testsuite] mm_util_encode_bmp_to_file failed : %d\n", ret);
304 } else if (mode == TEST_ENCODE_MEMORY) {
305 ret = mm_util_encode_bmp_to_memory(g_decoded_data, &encoded_data, &encoded_size);
306 if (ret != MM_UTIL_ERROR_NONE) {
307 fprintf(stderr, "\t[BMP_testsuite] mm_util_encode_bmp_to_memory failed : %d\n", ret);
308 SAFE_FREE(encoded_data);
311 if (FALSE == _write_file(ENCODE_MEM_PATH, encoded_data, encoded_size)) {
312 fprintf(stderr, "\t[BMP_testsuite] writing decoded data failed : %s\n", ENCODE_MEM_PATH);
313 SAFE_FREE(encoded_data);
318 SAFE_FREE(encoded_data);
322 gboolean _test_auto()
324 bmp_test_mode_e test_mode = TEST_DECODE_FILE;
326 while (test_mode < TEST_NUM) {
327 fprintf(stderr, "\t[BMP_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST START\n", MODE_TO_STR[test_mode]);
328 if (TEST_ENCODE_FILE == test_mode) {
330 g_path = g_strdup(DECODE_FILE_PATH);
331 if (g_path == NULL) {
332 fprintf(stderr, "\t[BMP_testsuite] Not enough memory\n");
336 /* test decoding bmp */
337 if ((test_mode == TEST_DECODE_FILE) || (test_mode == TEST_DECODE_MEMORY)) {
338 if (FALSE == _test_decode(test_mode)) {
339 fprintf(stderr, "\t[BMP_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST FAIL\n", MODE_TO_STR[test_mode]);
344 /* test encoding bmp */
345 if ((test_mode == TEST_ENCODE_FILE) || (test_mode == TEST_ENCODE_MEMORY)) {
346 if (FALSE == _test_encode(test_mode)) {
347 fprintf(stderr, "\t[BMP_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST FAIL\n", MODE_TO_STR[test_mode]);
352 fprintf(stderr, "\t[BMP_testsuite] >>>>>>>>>>>>>>>>>>>>>> \'%s\' TEST SUCCESS\n", MODE_TO_STR[test_mode]);
354 SAFE_FREE(g_readed_data);
355 SAFE_IMAGE_FREE(g_decoded_data);
362 int main(int argc, char *argv[])
365 _print_help(argv[0]);
369 if (FALSE == _get_arguments(argc, argv)) {
370 fprintf(stderr, "\t[BMP_testsuite] _get_arguments failed\n");
374 /* test all functions automatically */
375 if (g_test_mode == TEST_AUTO) {
376 if (FALSE == _test_auto())
377 fprintf(stderr, "\t[BMP_testsuite] _test_auto failed\n");
381 /* test decoding bmp */
382 if ((g_test_mode == TEST_DECODE_FILE) || (g_test_mode == TEST_DECODE_MEMORY)) {
383 if (FALSE == _test_decode(g_test_mode)) {
384 fprintf(stderr, "\t[BMP_testsuite] _test_decode(%s) failed\n", MODE_TO_STR[g_test_mode]);
389 /* test encoding bmp */
390 if ((g_test_mode == TEST_ENCODE_FILE) || (g_test_mode == TEST_ENCODE_MEMORY)) {
391 if (FALSE == _test_encode(g_test_mode)) {
392 fprintf(stderr, "\t[BMP_testsuite] _test_encode(%s) failed\n", MODE_TO_STR[g_test_mode]);
399 SAFE_FREE(g_readed_data);
400 SAFE_IMAGE_FREE(g_decoded_data);