[trunk] Rework the code to simplify cleanup code
[openjpeg.git] / tests / compareRAWimages.c
1 /*
2  * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France 
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /*
28  * compareRAWimages.c
29  *
30  *  Created on: 31 August 2011
31  *      Author: mickael
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38
39 #include "opj_getopt.h"
40
41 typedef struct test_cmp_parameters
42 {
43   /**  */
44   char* base_filename;
45   /**  */
46   char* test_filename;
47 } test_cmp_parameters;
48
49 /*******************************************************************************
50  * Command line help function
51  *******************************************************************************/
52 static void compareRAWimages_help_display(void) {
53   fprintf(stdout,"\nList of parameters for the comparePGX function  \n");
54   fprintf(stdout,"\n");
55   fprintf(stdout,"  -b \t REQUIRED \t filename to the reference/baseline RAW image \n");
56   fprintf(stdout,"  -t \t REQUIRED \t filename to the test RAW image\n");
57   fprintf(stdout,"\n");
58 }
59
60 /*******************************************************************************
61  * Parse command line
62  *******************************************************************************/
63 static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
64 {
65   size_t sizemembasefile, sizememtestfile;
66   int index;
67   const char optlist[] = "b:t:";
68   int c;
69
70   /* Init parameters*/
71   param->base_filename = NULL;
72   param->test_filename = NULL;
73
74   opj_opterr = 0;
75   while ((c = opj_getopt(argc, argv, optlist)) != -1)
76     switch (c)
77       {
78     case 'b':
79       sizemembasefile = strlen(opj_optarg)+1;
80       free(param->base_filename); // handle dup option
81       param->base_filename = (char*) malloc(sizemembasefile);
82       strcpy(param->base_filename, opj_optarg);
83       /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
84       break;
85     case 't':
86       sizememtestfile = strlen(opj_optarg) + 1;
87       free(param->test_filename); // handle dup option
88       param->test_filename = (char*) malloc(sizememtestfile);
89       strcpy(param->test_filename, opj_optarg);
90       /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
91       break;
92     case '?':
93       if ((opj_optopt == 'b') || (opj_optopt == 't'))
94         fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
95       else
96         if (isprint(opj_optopt))        fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
97         else    fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
98       return 1;
99     default:
100       fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
101       break;
102       }
103
104   if (opj_optind != argc) {
105     for (index = opj_optind; index < argc; index++)
106       fprintf(stderr,"Non-option argument %s\n", argv[index]);
107     return 1;
108   }
109
110   return 0;
111 }
112
113 /*******************************************************************************
114  * MAIN
115  *******************************************************************************/
116 int main(int argc, char **argv)
117 {
118   int pos = 0;
119   test_cmp_parameters inParam;
120   FILE *file_test=NULL, *file_base=NULL;
121   unsigned char equal = 1;
122
123   /* Get parameters from command line*/
124   if (parse_cmdline_cmp(argc, argv, &inParam))
125     {
126     compareRAWimages_help_display();
127     goto cleanup;
128     }
129
130   file_test = fopen(inParam.test_filename, "rb");
131   if (!file_test) {
132     fprintf(stderr, "Failed to open %s for reading !!\n", inParam.test_filename);
133     goto cleanup;
134   }
135
136   file_base = fopen(inParam.base_filename, "rb");
137   if (!file_base) {
138     fprintf(stderr, "Failed to open %s for reading !!\n", inParam.base_filename);
139     goto cleanup;
140   }
141
142   /* Read simultaneously the two files*/
143   while (equal)
144     {
145     unsigned char value_test = 0;
146     unsigned char eof_test = 0;
147     unsigned char value_base = 0;
148     unsigned char eof_base = 0;
149
150     /* Read one byte*/
151     if (!fread(&value_test, 1, 1, file_test)) {
152       eof_test = 1;
153     }
154
155     /* Read one byte*/
156     if (!fread(&value_base, 1, 1, file_base)) {
157       eof_base = 1;
158     }
159
160     /* End of file reached by the two files?*/
161     if (eof_test && eof_base)
162       break;
163
164     /* End of file reached only by one file?*/
165     if (eof_test || eof_base)
166       {
167       fprintf(stdout,"Files have different sizes.\n");
168       equal = 0;
169       }
170
171     /* Binary values are equal?*/
172     if (value_test != value_base)
173       {
174       fprintf(stdout,"Binary values read in the file are different %x vs %x at position %d.\n", value_test, value_base, pos);
175       equal = 0;
176       }
177     pos++;
178     }
179
180   if(equal) fprintf(stdout,"---- TEST SUCCEED: Files are equal ----\n");
181 cleanup:
182   if(file_test) fclose(file_test);
183   if(file_base) fclose(file_base);
184
185   /* Free Memory */
186   free(inParam.base_filename);
187   free(inParam.test_filename);
188
189   return equal ? EXIT_SUCCESS : EXIT_FAILURE;
190 }