[trunk] use the right v2 structure
[openjpeg.git] / tests / compareRAWimages.c
1 /*
2  * Copyright (c) 2011, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
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 void compareRAWimages_help_display(void);
42
43 typedef struct test_cmp_parameters
44 {
45         /**  */
46         char* base_filename;
47         /**  */
48         char* test_filename;
49 } test_cmp_parameters;
50
51 /*******************************************************************************
52  * Command line help function
53  *******************************************************************************/
54 void compareRAWimages_help_display(void) {
55         fprintf(stdout,"\nList of parameters for the comparePGX function  \n");
56         fprintf(stdout,"\n");
57         fprintf(stdout,"  -b \t REQUIRED \t filename to the reference/baseline RAW image \n");
58         fprintf(stdout,"  -t \t REQUIRED \t filename to the test RAW image\n");
59         fprintf(stdout,"\n");
60 }
61
62 /*******************************************************************************
63  * Parse command line
64  *******************************************************************************/
65 static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
66 {
67         int sizemembasefile, sizememtestfile;
68         int index;
69         const char optlist[] = "b:t:";
70         int c;
71
72         /* Init parameters*/
73         param->base_filename = NULL;
74         param->test_filename = NULL;
75
76         opj_opterr = 0;
77         while ((c = opj_getopt(argc, argv, optlist)) != -1)
78                 switch (c)
79                 {
80                 case 'b':
81                         sizemembasefile = (int)strlen(opj_optarg)+1;
82                         param->base_filename = (char*) malloc(sizemembasefile);
83                         param->base_filename[0] = '\0';
84                         strncpy(param->base_filename, opj_optarg, strlen(opj_optarg));
85                         param->base_filename[strlen(opj_optarg)] = '\0';
86                         /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
87                         break;
88                 case 't':
89                         sizememtestfile = (int) strlen(opj_optarg) + 1;
90                         param->test_filename = (char*) malloc(sizememtestfile);
91                         param->test_filename[0] = '\0';
92                         strncpy(param->test_filename, opj_optarg, strlen(opj_optarg));
93                         param->test_filename[strlen(opj_optarg)] = '\0';
94                         /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
95                         break;
96                 case '?':
97                         if ((opj_optopt == 'b') || (opj_optopt == 't'))
98                                 fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
99                         else
100                                 if (isprint(opj_optopt))        fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
101                                 else    fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
102                         return 1;
103                 default:
104                         fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
105                         break;
106                 }
107
108         if (opj_optind != argc) {
109                 for (index = opj_optind; index < argc; index++)
110                         fprintf(stderr,"Non-option argument %s\n", argv[index]);
111                 return EXIT_FAILURE;
112     }
113
114   return EXIT_SUCCESS;
115 }
116
117 /*******************************************************************************
118  * MAIN
119  *******************************************************************************/
120 int main(int argc, char **argv)
121 {
122         test_cmp_parameters inParam;
123         FILE *file_test=NULL, *file_base=NULL;
124         unsigned char equal = 1;
125
126         /* Get parameters from command line*/
127         if (parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE)
128         {
129                 compareRAWimages_help_display();
130
131                 /* Free Memory */
132                 if (inParam.base_filename){
133                         free(inParam.base_filename);
134                         inParam.base_filename = NULL;
135                 }
136                 if (inParam.test_filename){
137                         free(inParam.test_filename);
138                         inParam.test_filename = NULL;
139                 }
140
141                 return EXIT_FAILURE;
142         }
143
144         file_test = fopen(inParam.test_filename, "rb");
145         if (!file_test) {
146                 fprintf(stderr, "Failed to open %s for reading !!\n", inParam.test_filename);
147
148                 /* Free Memory */
149                 if (inParam.base_filename){
150                         free(inParam.base_filename);
151                         inParam.base_filename = NULL;
152                 }
153                 if (inParam.test_filename){
154                         free(inParam.test_filename);
155                         inParam.test_filename = NULL;
156                 }
157
158                 return EXIT_FAILURE;
159         }
160
161         file_base = fopen(inParam.base_filename, "rb");
162         if (!file_base) {
163                 fprintf(stderr, "Failed to open %s for reading !!\n", inParam.base_filename);
164
165                 /* Free Memory */
166                 if (inParam.base_filename){
167                         free(inParam.base_filename);
168                         inParam.base_filename = NULL;
169                 }
170                 if (inParam.test_filename){
171                         free(inParam.test_filename);
172                         inParam.test_filename = NULL;
173                 }
174
175                 fclose(file_test);
176                 return EXIT_FAILURE;
177         }
178
179         /* Read simultaneously the two files*/
180         while (equal)
181         {
182                 unsigned char value_test = 0;
183                 unsigned char eof_test = 0;
184                 unsigned char value_base = 0;
185                 unsigned char eof_base = 0;
186
187                 /* Read one byte*/
188                 if (!fread(&value_test, 1, 1, file_test)) {
189                         eof_test = 1;
190                 }
191
192                 /* Read one byte*/
193                 if (!fread(&value_base, 1, 1, file_base)) {
194                         eof_base = 1;;
195                 }
196
197                 /* End of file reached by the two files?*/
198                 if (eof_test && eof_base)
199                         break;
200
201                 /* End of file reached only by one file?*/
202                 if (eof_test || eof_base)
203                 {
204                         fprintf(stdout,"Files have different sizes.\n");
205                         equal = 0;
206                 }
207
208                 /* Binary values are equal?*/
209                 if (value_test != value_base)
210                 {
211                         fprintf(stdout,"Binary values read in the file are different.\n");
212                         equal = 0;
213                 }
214         }
215
216         /* Free Memory */
217         if (inParam.base_filename){
218                 free(inParam.base_filename);
219                 inParam.base_filename = NULL;
220         }
221         if (inParam.test_filename){
222                 free(inParam.test_filename);
223                 inParam.test_filename = NULL;
224         }
225
226         fclose(file_test);
227         fclose(file_base);
228
229         if (equal)
230         {
231                 fprintf(stdout,"---- TEST SUCCEED: Files are equal ----\n");
232                 return EXIT_SUCCESS;
233         }
234         else
235                 return EXIT_FAILURE;
236 }