[trunk] Run richter test suite, correct a regression introduced in r2668
[openjpeg.git] / tests / compare_images.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  * compare_images.c
29  *
30  *  Created on: 8 juil. 2011
31  *      Author: mickael
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <math.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <assert.h>
40
41 #include "opj_apps_config.h"
42 #include "opj_getopt.h"
43
44 #include "openjpeg.h"
45 #include "format_defs.h"
46 #include "convert.h"
47
48 #ifdef OPJ_HAVE_LIBTIFF
49 #include <tiffio.h> /* TIFFSetWarningHandler */
50 #endif /* OPJ_HAVE_LIBTIFF */
51
52 /*******************************************************************************
53  * Parse MSE and PEAK input values (
54  * separator = ":"
55  *******************************************************************************/
56 static double* parseToleranceValues( char* inArg, const int nbcomp)
57 {
58   double* outArgs= malloc((size_t)nbcomp * sizeof(double));
59   int it_comp = 0;
60   const char delims[] = ":";
61   char *result = strtok( inArg, delims );
62
63   while( (result != NULL) && (it_comp < nbcomp ))
64     {
65     outArgs[it_comp] = atof(result);
66     result = strtok( NULL, delims );
67     it_comp++;
68     }
69
70   if (it_comp != nbcomp)
71     {
72     free(outArgs);
73     return NULL;
74     }
75   /* else */
76   return outArgs;
77 }
78
79 /*******************************************************************************
80  * Command line help function
81  *******************************************************************************/
82 static void compare_images_help_display(void)
83 {
84   fprintf(stdout,"\nList of parameters for the compare_images function  \n");
85   fprintf(stdout,"\n");
86   fprintf(stdout,"  -b \t REQUIRED \t filename to the reference/baseline PGX/TIF/PNM image \n");
87   fprintf(stdout,"  -t \t REQUIRED \t filename to the test PGX/TIF/PNM image\n");
88   fprintf(stdout,"  -n \t REQUIRED \t number of component of the image (used to generate correct filename)\n");
89   fprintf(stdout,"  -m \t OPTIONAL \t list of MSE tolerances, separated by : (size must correspond to the number of component) of \n");
90   fprintf(stdout,"  -p \t OPTIONAL \t list of PEAK tolerances, separated by : (size must correspond to the number of component) \n");
91   fprintf(stdout,"  -s \t OPTIONAL \t 1 or 2 filename separator to take into account PGX/PNM image with different components, "
92                                       "please indicate b or t before separator to indicate respectively the separator "
93                                       "for ref/base file and for test file.  \n");
94   fprintf(stdout,"  -d \t OPTIONAL \t indicate if you want to run this function as conformance test or as non regression test\n");
95   fprintf(stdout,"\n");
96 }
97
98 static int get_decod_format_from_string(const char *filename)
99 {
100   const int dot = '.';
101   char * ext = strrchr(filename, dot);
102   if( strcmp(ext,".pgx") == 0 ) return PGX_DFMT;
103   if( strcmp(ext,".tif") == 0 ) return TIF_DFMT;
104   if( strcmp(ext,".ppm") == 0 ) return PXM_DFMT;
105   return -1;
106 }
107
108
109 /*******************************************************************************
110  * Create filenames from a filename using separator and nb components
111  * (begin from 0)
112  *******************************************************************************/
113 static char* createMultiComponentsFilename(const char* inFilename, const int indexF, const char* separator)
114 {
115   char s[255];
116   char *outFilename, *ptr;
117   const char token = '.';
118   size_t posToken = 0;
119   int decod_format;
120
121   /*printf("inFilename = %s\n", inFilename);*/
122   if ((ptr = strrchr(inFilename, token)) != NULL)
123     {
124     posToken = strlen(inFilename) - strlen(ptr);
125     /*printf("Position of %c character inside inFilename = %d\n", token, posToken);*/
126     }
127   else
128     {
129     /*printf("Token %c not found\n", token);*/
130     outFilename = (char*)malloc(1);
131     outFilename[0] = '\0';
132     return outFilename;
133     }
134
135   outFilename = (char*)malloc((posToken + 7) * sizeof(char)); /*6*/
136
137   strncpy(outFilename, inFilename, posToken);
138   outFilename[posToken] = '\0';
139   strcat(outFilename, separator);
140   sprintf(s, "%i", indexF);
141   strcat(outFilename, s);
142
143   decod_format = get_decod_format_from_string(inFilename);
144   if( decod_format == PGX_DFMT )
145     {
146     strcat(outFilename, ".pgx");
147     }
148   else if( decod_format == PXM_DFMT )
149     {
150     strcat(outFilename, ".pgm");
151     }
152
153   /*printf("outfilename: %s\n", outFilename);*/
154   return outFilename;
155 }
156
157 /*******************************************************************************
158  *
159  *******************************************************************************/
160 static opj_image_t* readImageFromFilePPM(const char* filename, int nbFilenamePGX, const char *separator)
161 {
162   int it_file;
163   opj_image_t* image_read = NULL;
164   opj_image_t* image = NULL;
165   opj_cparameters_t parameters;
166   opj_image_cmptparm_t* param_image_read;
167   int** data;
168
169   /* If separator is empty => nb file to read is equal to one*/
170   if ( strlen(separator) == 0 )
171     nbFilenamePGX = 1;
172
173   /* set encoding parameters to default values */
174   opj_set_default_encoder_parameters(&parameters);
175   parameters.decod_format = PXM_DFMT;
176   strcpy(parameters.infile, filename);
177
178   /* Allocate memory*/
179   param_image_read = malloc((size_t)nbFilenamePGX * sizeof(opj_image_cmptparm_t));
180   data = malloc((size_t)nbFilenamePGX * sizeof(*data));
181
182   for (it_file = 0; it_file < nbFilenamePGX; it_file++)
183     {
184     /* Create the right filename*/
185     char *filenameComponentPGX;
186     if (strlen(separator) == 0)
187       {
188       filenameComponentPGX = malloc((strlen(filename) + 1) * sizeof(*filenameComponentPGX));
189       strcpy(filenameComponentPGX, filename);
190       }
191     else
192       filenameComponentPGX = createMultiComponentsFilename(filename, it_file, separator);
193
194     /* Read the tif file corresponding to the component */
195     image_read = pnmtoimage(filenameComponentPGX, &parameters);
196     if (!image_read)
197       {
198       int it_free_data;
199       fprintf(stderr, "Unable to load pgx file\n");
200
201       free(param_image_read);
202
203       for (it_free_data = 0; it_free_data < it_file; it_free_data++) {
204         free(data[it_free_data]);
205       }
206       free(data);
207
208       free(filenameComponentPGX);
209
210       return NULL;
211       }
212
213     /* Set the image_read parameters*/
214     param_image_read[it_file].x0 = 0;
215     param_image_read[it_file].y0 = 0;
216     param_image_read[it_file].dx = 0;
217     param_image_read[it_file].dy = 0;
218     param_image_read[it_file].h = image_read->comps->h;
219     param_image_read[it_file].w = image_read->comps->w;
220     param_image_read[it_file].bpp = image_read->comps->bpp;
221     param_image_read[it_file].prec = image_read->comps->prec;
222     param_image_read[it_file].sgnd = image_read->comps->sgnd;
223
224     /* Copy data*/
225     data[it_file] = malloc(param_image_read[it_file].h * param_image_read[it_file].w * sizeof(int));
226     memcpy(data[it_file], image_read->comps->data, image_read->comps->h * image_read->comps->w * sizeof(int));
227
228     /* Free memory*/
229     opj_image_destroy(image_read);
230     free(filenameComponentPGX);
231     }
232
233   image = opj_image_create((OPJ_UINT32)nbFilenamePGX, param_image_read, OPJ_CLRSPC_UNSPECIFIED);
234   for (it_file = 0; it_file < nbFilenamePGX; it_file++)
235     {
236     /* Copy data into output image and free memory*/
237     memcpy(image->comps[it_file].data, data[it_file], image->comps[it_file].h * image->comps[it_file].w * sizeof(int));
238     free(data[it_file]);
239     }
240
241   /* Free memory*/
242   free(param_image_read);
243   free(data);
244
245   return image;
246 }
247
248 static opj_image_t* readImageFromFileTIF(const char* filename, int nbFilenamePGX, const char *separator)
249 {
250   int it_file;
251   opj_image_t* image_read = NULL;
252   opj_image_t* image = NULL;
253   opj_cparameters_t parameters;
254   opj_image_cmptparm_t* param_image_read;
255   int** data;
256
257   /* conformance test suite produce annoying warning/error:
258    * TIFFReadDirectory: Warning, /.../data/baseline/conformance/jp2_1.tif: unknown field with tag 37724 (0x935c) encountered.
259    * TIFFOpen: /.../data/baseline/nonregression/opj_jp2_1.tif: Cannot open.
260    * On Win32 this open a message box by default, so remove it from the test suite:
261    */
262 #ifdef OPJ_HAVE_LIBTIFF
263   TIFFSetWarningHandler(NULL);
264   TIFFSetErrorHandler(NULL);
265 #endif
266
267   /* If separator is empty => nb file to read is equal to one*/
268   if ( strlen(separator) == 0 )
269     nbFilenamePGX = 1;
270
271   /* set encoding parameters to default values */
272   opj_set_default_encoder_parameters(&parameters);
273   parameters.decod_format = TIF_DFMT;
274   strcpy(parameters.infile, filename);
275
276   /* Allocate memory*/
277   param_image_read = malloc((size_t)nbFilenamePGX * sizeof(opj_image_cmptparm_t));
278   data = malloc((size_t)nbFilenamePGX * sizeof(*data));
279
280   for (it_file = 0; it_file < nbFilenamePGX; it_file++)
281     {
282     /* Create the right filename*/
283     char *filenameComponentPGX;
284     if (strlen(separator) == 0)
285       {
286       filenameComponentPGX = malloc((strlen(filename) + 1) * sizeof(*filenameComponentPGX));
287       strcpy(filenameComponentPGX, filename);
288       }
289     else
290       filenameComponentPGX = createMultiComponentsFilename(filename, it_file, separator);
291
292     /* Read the tif file corresponding to the component */
293 #ifdef OPJ_HAVE_LIBTIFF
294     image_read = tiftoimage(filenameComponentPGX, &parameters);
295 #endif
296     if (!image_read)
297       {
298       int it_free_data;
299       fprintf(stderr, "Unable to load TIF file\n");
300
301       free(param_image_read);
302
303       for (it_free_data = 0; it_free_data < it_file; it_free_data++) {
304         free(data[it_free_data]);
305       }
306       free(data);
307
308       free(filenameComponentPGX);
309
310       return NULL;
311       }
312
313     /* Set the image_read parameters*/
314     param_image_read[it_file].x0 = 0;
315     param_image_read[it_file].y0 = 0;
316     param_image_read[it_file].dx = 0;
317     param_image_read[it_file].dy = 0;
318     param_image_read[it_file].h = image_read->comps->h;
319     param_image_read[it_file].w = image_read->comps->w;
320     param_image_read[it_file].bpp = image_read->comps->bpp;
321     param_image_read[it_file].prec = image_read->comps->prec;
322     param_image_read[it_file].sgnd = image_read->comps->sgnd;
323
324     /* Copy data*/
325     data[it_file] = malloc(param_image_read[it_file].h * param_image_read[it_file].w * sizeof(int));
326     memcpy(data[it_file], image_read->comps->data, image_read->comps->h * image_read->comps->w * sizeof(int));
327
328     /* Free memory*/
329     opj_image_destroy(image_read);
330     free(filenameComponentPGX);
331     }
332
333   image = opj_image_create((OPJ_UINT32)nbFilenamePGX, param_image_read, OPJ_CLRSPC_UNSPECIFIED);
334   for (it_file = 0; it_file < nbFilenamePGX; it_file++)
335     {
336     /* Copy data into output image and free memory*/
337     memcpy(image->comps[it_file].data, data[it_file], image->comps[it_file].h * image->comps[it_file].w * sizeof(int));
338     free(data[it_file]);
339     }
340
341   /* Free memory*/
342   free(param_image_read);
343   free(data);
344
345   return image;
346 }
347
348 static opj_image_t* readImageFromFilePGX(const char* filename, int nbFilenamePGX, const char *separator)
349 {
350   int it_file;
351   opj_image_t* image_read = NULL;
352   opj_image_t* image = NULL;
353   opj_cparameters_t parameters;
354   opj_image_cmptparm_t* param_image_read;
355   int** data;
356
357   /* If separator is empty => nb file to read is equal to one*/
358   if ( strlen(separator) == 0 )
359     nbFilenamePGX = 1;
360
361   /* set encoding parameters to default values */
362   opj_set_default_encoder_parameters(&parameters);
363   parameters.decod_format = PGX_DFMT;
364   strcpy(parameters.infile, filename);
365
366   /* Allocate memory*/
367   param_image_read = malloc((size_t)nbFilenamePGX * sizeof(opj_image_cmptparm_t));
368   data = malloc((size_t)nbFilenamePGX * sizeof(*data));
369
370   for (it_file = 0; it_file < nbFilenamePGX; it_file++)
371     {
372     /* Create the right filename*/
373     char *filenameComponentPGX;
374     if (strlen(separator) == 0)
375       {
376       filenameComponentPGX = malloc((strlen(filename) + 1) * sizeof(*filenameComponentPGX));
377       strcpy(filenameComponentPGX, filename);
378       }
379     else
380       filenameComponentPGX = createMultiComponentsFilename(filename, it_file, separator);
381
382     /* Read the pgx file corresponding to the component */
383     image_read = pgxtoimage(filenameComponentPGX, &parameters);
384     if (!image_read)
385       {
386       int it_free_data;
387       fprintf(stderr, "Unable to load pgx file\n");
388
389       free(param_image_read);
390
391       for (it_free_data = 0; it_free_data < it_file; it_free_data++) {
392         free(data[it_free_data]);
393       }
394       free(data);
395
396       free(filenameComponentPGX);
397
398       return NULL;
399       }
400
401     /* Set the image_read parameters*/
402     param_image_read[it_file].x0 = 0;
403     param_image_read[it_file].y0 = 0;
404     param_image_read[it_file].dx = 0;
405     param_image_read[it_file].dy = 0;
406     param_image_read[it_file].h = image_read->comps->h;
407     param_image_read[it_file].w = image_read->comps->w;
408     param_image_read[it_file].bpp = image_read->comps->bpp;
409     param_image_read[it_file].prec = image_read->comps->prec;
410     param_image_read[it_file].sgnd = image_read->comps->sgnd;
411
412     /* Copy data*/
413     data[it_file] = malloc(param_image_read[it_file].h * param_image_read[it_file].w * sizeof(int));
414     memcpy(data[it_file], image_read->comps->data, image_read->comps->h * image_read->comps->w * sizeof(int));
415
416     /* Free memory*/
417     opj_image_destroy(image_read);
418     free(filenameComponentPGX);
419     }
420
421   image = opj_image_create((OPJ_UINT32)nbFilenamePGX, param_image_read, OPJ_CLRSPC_UNSPECIFIED);
422   for (it_file = 0; it_file < nbFilenamePGX; it_file++)
423     {
424     /* Copy data into output image and free memory*/
425     memcpy(image->comps[it_file].data, data[it_file], image->comps[it_file].h * image->comps[it_file].w * sizeof(int));
426     free(data[it_file]);
427     }
428
429   /* Free memory*/
430   free(param_image_read);
431   free(data);
432
433   return image;
434 }
435
436 #if defined(OPJ_HAVE_LIBPNG) && 0 /* remove for now */
437 /*******************************************************************************
438  *
439  *******************************************************************************/
440 static int imageToPNG(const opj_image_t* image, const char* filename, int num_comp_select)
441 {
442   opj_image_cmptparm_t param_image_write;
443   opj_image_t* image_write = NULL;
444
445   param_image_write.x0 = 0;
446   param_image_write.y0 = 0;
447   param_image_write.dx = 0;
448   param_image_write.dy = 0;
449   param_image_write.h = image->comps[num_comp_select].h;
450   param_image_write.w = image->comps[num_comp_select].w;
451   param_image_write.bpp = image->comps[num_comp_select].bpp;
452   param_image_write.prec = image->comps[num_comp_select].prec;
453   param_image_write.sgnd = image->comps[num_comp_select].sgnd;
454
455   image_write = opj_image_create(1u, &param_image_write, OPJ_CLRSPC_GRAY);
456   memcpy(image_write->comps->data, image->comps[num_comp_select].data, param_image_write.h * param_image_write.w * sizeof(int));
457
458   imagetopng(image_write, filename);
459
460   opj_image_destroy(image_write);
461
462   return EXIT_SUCCESS;
463 }
464 #endif
465
466 typedef struct test_cmp_parameters
467 {
468   /**  */
469   char* base_filename;
470   /**  */
471   char* test_filename;
472   /** Number of components */
473   int nbcomp;
474   /**  */
475   double* tabMSEvalues;
476   /**  */
477   double* tabPEAKvalues;
478   /**  */
479   int nr_flag;
480   /**  */
481   char separator_base[2];
482   /**  */
483   char separator_test[2];
484
485 } test_cmp_parameters;
486
487 /* return decode format PGX / TIF / PPM , return -1 on error */
488 static int get_decod_format(test_cmp_parameters* param)
489 {
490   int base_format = get_decod_format_from_string( param->base_filename );
491   int test_format = get_decod_format_from_string( param->test_filename );
492   if( base_format != test_format ) return -1;
493   /* handle case -1: */
494   return base_format;
495 }
496
497 /*******************************************************************************
498  * Parse command line
499  *******************************************************************************/
500 static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
501 {
502   char *MSElistvalues = NULL;  char *PEAKlistvalues= NULL;
503   char *separatorList = NULL;
504   size_t sizemembasefile, sizememtestfile;
505   int index, flagM=0, flagP=0;
506   const char optlist[] = "b:t:n:m:p:s:d";
507   int c;
508
509   /* Init parameters*/
510   param->base_filename = NULL;
511   param->test_filename = NULL;
512   param->nbcomp = 0;
513   param->tabMSEvalues = NULL;
514   param->tabPEAKvalues = NULL;
515   param->nr_flag = 0;
516   param->separator_base[0] = 0;
517   param->separator_test[0] = 0;
518
519   opj_opterr = 0;
520
521   while ((c = opj_getopt(argc, argv, optlist)) != -1)
522     switch (c)
523       {
524       case 'b':
525         sizemembasefile = strlen(opj_optarg) + 1;
526         param->base_filename = (char*) malloc(sizemembasefile);
527         strcpy(param->base_filename, opj_optarg);
528         /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
529         break;
530       case 't':
531         sizememtestfile = strlen(opj_optarg) + 1;
532         param->test_filename = (char*) malloc(sizememtestfile);
533         strcpy(param->test_filename, opj_optarg);
534         /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
535        break;
536       case 'n':
537         param->nbcomp = atoi(opj_optarg);
538         break;
539       case 'm':
540         MSElistvalues = opj_optarg;
541         flagM = 1;
542         break;
543       case 'p':
544         PEAKlistvalues = opj_optarg;
545         flagP = 1;
546         break;
547       case 'd':
548         param->nr_flag = 1;
549         break;
550       case 's':
551         separatorList = opj_optarg;
552         break;
553       case '?':
554         if ((opj_optopt == 'b') || (opj_optopt == 't') || (opj_optopt == 'n') || (opj_optopt == 'p') || (opj_optopt == 'm') || (opj_optopt
555             == 's'))
556           fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
557         else
558           if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
559           else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
560         return 1;
561       default:
562         fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
563         break;
564       }
565
566   if (opj_optind != argc)
567     {
568     for (index = opj_optind; index < argc; index++)
569       fprintf(stderr,"Non-option argument %s\n", argv[index]);
570     return 1;
571     }
572
573   if (param->nbcomp == 0)
574     {
575     fprintf(stderr,"Need to indicate the number of components !\n");
576     return 1;
577     }
578   /* else */
579   if ( flagM && flagP )
580     {
581     param->tabMSEvalues = parseToleranceValues( MSElistvalues, param->nbcomp);
582     param->tabPEAKvalues = parseToleranceValues( PEAKlistvalues, param->nbcomp);
583     if ( (param->tabMSEvalues == NULL) || (param->tabPEAKvalues == NULL))
584       {
585       fprintf(stderr,"MSE and PEAK values are not correct (respectively need %d values)\n",param->nbcomp);
586       return 1;
587       }
588     }
589
590   /* Get separators after corresponding letter (b or t)*/
591   if (separatorList != NULL)
592     {
593     if( (strlen(separatorList) ==2) || (strlen(separatorList) ==4) )
594       {
595       /* keep original string*/
596       size_t sizeseplist = strlen(separatorList)+1;
597       char* separatorList2 = (char*)malloc( sizeseplist );
598       strcpy(separatorList2, separatorList);
599       /*printf("separatorList2 = %s [%d / %d]\n", separatorList2, strlen(separatorList2), sizeseplist);*/
600
601       if (strlen(separatorList) == 2) /* one separator behind b or t*/
602         {
603         char *resultT = NULL;
604         resultT = strtok(separatorList2, "t");
605         if (strlen(resultT) == strlen(separatorList)) /* didn't find t character, try to find b*/
606           {
607           char *resultB = NULL;
608           resultB = strtok(resultT, "b");
609           if (strlen(resultB) == 1)
610             {
611             param->separator_base[0] = separatorList[1];
612             param->separator_base[1] = 0;
613             param->separator_test[0] = 0;
614             }
615           else /* not found b*/
616             {
617             free(separatorList2);
618             return 1;
619             }
620           }
621         else /* found t*/
622           {
623           param->separator_base[0] = 0;
624           param->separator_test[0] = separatorList[1];
625           param->separator_test[1] = 0;
626           }
627         /*printf("sep b = %s [%d] and sep t = %s [%d]\n",param->separator_base, strlen(param->separator_base), param->separator_test, strlen(param->separator_test) );*/
628         }
629       else /* == 4 characters we must found t and b*/
630         {
631         char *resultT = NULL;
632         resultT = strtok(separatorList2, "t");
633         if (strlen(resultT) == 3) /* found t in first place*/
634           {
635           char *resultB = NULL;
636           resultB = strtok(resultT, "b");
637           if (strlen(resultB) == 1) /* found b after t*/
638             {
639             param->separator_test[0] = separatorList[1];
640             param->separator_test[1] = 0;
641             param->separator_base[0] = separatorList[3];
642             param->separator_base[1] = 0;
643             }
644           else /* didn't find b after t*/
645             {
646             free(separatorList2);
647             return 1;
648             }
649           }
650         else /* == 2, didn't find t in first place*/
651           {
652           char *resultB = NULL;
653           resultB = strtok(resultT, "b");
654           if (strlen(resultB) == 1) /* found b in first place*/
655             {
656             param->separator_base[0] = separatorList[1];
657             param->separator_base[1] = 0;
658             param->separator_test[0] = separatorList[3];
659             param->separator_test[1] = 0;
660             }
661           else /* didn't found b in first place => problem*/
662             {
663             free(separatorList2);
664             return 1;
665             }
666           }
667         }
668       free(separatorList2);
669       }
670     else /* wrong number of argument after -s*/
671       {
672       return 1;
673       }
674     }
675   else
676     {
677     if (param->nbcomp == 1)
678       {
679       assert( param->separator_base[0] == 0 );
680       assert( param->separator_test[0] == 0 );
681       }
682     else
683       {
684       fprintf(stderr,"If number of component is > 1, we need separator\n");
685       return 1;
686       }
687     }
688
689
690   if ( (param->nr_flag) && (flagP || flagM) )
691     {
692     fprintf(stderr,"Wrong input parameters list: it is non-regression test or tolerance comparison\n");
693     return 1;
694     }
695   if ( (!param->nr_flag) && (!flagP || !flagM) )
696     {
697     fprintf(stderr,"Wrong input parameters list: it is non-regression test or tolerance comparison\n");
698     return 1;
699     }
700
701   return 0;
702 }
703
704 /*******************************************************************************
705  * MAIN
706  *******************************************************************************/
707 int main(int argc, char **argv)
708 {
709   test_cmp_parameters inParam;
710   OPJ_UINT32 it_comp, itpxl;
711   int failed = 1;
712   int nbFilenamePGXbase = 0, nbFilenamePGXtest = 0;
713   char *filenamePNGtest= NULL, *filenamePNGbase = NULL, *filenamePNGdiff = NULL;
714   int memsizebasefilename, memsizetestfilename;
715   size_t memsizedifffilename;
716   int valueDiff = 0, nbPixelDiff = 0;
717   double sumDiff = 0.0;
718   /* Structures to store image parameters and data*/
719   opj_image_t *imageBase = NULL, *imageTest = NULL, *imageDiff = NULL;
720   opj_image_cmptparm_t* param_image_diff = NULL;
721   int decod_format;
722
723   /* Get parameters from command line*/
724   if( parse_cmdline_cmp(argc, argv, &inParam) )
725     {
726     compare_images_help_display();
727     goto cleanup;
728     }
729
730   /* Display Parameters*/
731   printf("******Parameters********* \n");
732   printf(" base_filename = %s\n"
733          " test_filename = %s\n"
734          " nb of Components = %d\n"
735          " Non regression test = %d\n"
736          " separator Base = %s\n"
737          " separator Test = %s\n",
738          inParam.base_filename, inParam.test_filename, inParam.nbcomp,
739          inParam.nr_flag, inParam.separator_base, inParam.separator_test);
740
741   if ( (inParam.tabMSEvalues != NULL) && (inParam.tabPEAKvalues != NULL))
742     {
743     int it_comp2;
744     printf(" MSE values = [");
745     for (it_comp2 = 0; it_comp2 < inParam.nbcomp; it_comp2++)
746       printf(" %f ", inParam.tabMSEvalues[it_comp2]);
747     printf("]\n");
748     printf(" PEAK values = [");
749     for (it_comp2 = 0; it_comp2 < inParam.nbcomp; it_comp2++)
750       printf(" %f ", inParam.tabPEAKvalues[it_comp2]);
751     printf("]\n");
752     printf(" Non-regression test = %d\n", inParam.nr_flag);
753     }
754
755   if (strlen(inParam.separator_base) != 0)
756     nbFilenamePGXbase = inParam.nbcomp;
757
758   if (strlen(inParam.separator_test) != 0)
759     nbFilenamePGXtest = inParam.nbcomp;
760
761   printf(" NbFilename to generate from base filename = %d\n", nbFilenamePGXbase);
762   printf(" NbFilename to generate from test filename = %d\n", nbFilenamePGXtest);
763   printf("************************* \n");
764
765   /*----------BASELINE IMAGE--------*/
766   memsizebasefilename = (int)strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
767   memsizetestfilename = (int)strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
768
769   decod_format = get_decod_format(&inParam);
770   if( decod_format == -1 )
771     {
772     fprintf( stderr, "Unhandled file format\n" );
773     goto cleanup;
774     }
775   assert( decod_format == PGX_DFMT || decod_format == TIF_DFMT || decod_format == PXM_DFMT );
776
777   if( decod_format == PGX_DFMT )
778     {
779     imageBase = readImageFromFilePGX( inParam.base_filename, nbFilenamePGXbase, inParam.separator_base);
780     if ( imageBase == NULL )
781       goto cleanup;
782     }
783   else if( decod_format == TIF_DFMT )
784     {
785     imageBase = readImageFromFileTIF( inParam.base_filename, nbFilenamePGXbase, "");
786     if ( imageBase == NULL )
787       goto cleanup;
788     }
789   else if( decod_format == PXM_DFMT )
790     {
791     imageBase = readImageFromFilePPM( inParam.base_filename, nbFilenamePGXbase, inParam.separator_base);
792     if ( imageBase == NULL )
793       goto cleanup;
794     }
795
796   filenamePNGbase = (char*) malloc((size_t)memsizebasefilename);
797   strcpy(filenamePNGbase, inParam.test_filename);
798   strcat(filenamePNGbase, ".base");
799   /*printf("filenamePNGbase = %s [%d / %d octets]\n",filenamePNGbase, strlen(filenamePNGbase),memsizebasefilename );*/
800
801   /*----------TEST IMAGE--------*/
802
803   if( decod_format == PGX_DFMT )
804     {
805     imageTest = readImageFromFilePGX(inParam.test_filename, nbFilenamePGXtest, inParam.separator_test);
806     if ( imageTest == NULL )
807       goto cleanup;
808     }
809   else if( decod_format == TIF_DFMT )
810     {
811     imageTest = readImageFromFileTIF(inParam.test_filename, nbFilenamePGXtest, "");
812     if ( imageTest == NULL )
813       goto cleanup;
814     }
815   else if( decod_format == PXM_DFMT )
816     {
817     imageTest = readImageFromFilePPM(inParam.test_filename, nbFilenamePGXtest, inParam.separator_test);
818     if ( imageTest == NULL )
819       goto cleanup;
820     }
821
822   filenamePNGtest = (char*) malloc((size_t)memsizetestfilename);
823   strcpy(filenamePNGtest, inParam.test_filename);
824   strcat(filenamePNGtest, ".test");
825   /*printf("filenamePNGtest = %s [%d / %d octets]\n",filenamePNGtest, strlen(filenamePNGtest),memsizetestfilename );*/
826
827   /*----------DIFF IMAGE--------*/
828
829   /* Allocate memory*/
830   param_image_diff = malloc( imageBase->numcomps * sizeof(opj_image_cmptparm_t));
831
832   /* Comparison of header parameters*/
833   printf("Step 1 -> Header comparison\n");
834
835   for (it_comp = 0; it_comp < imageBase->numcomps; it_comp++)
836     {
837     param_image_diff[it_comp].x0 = 0;
838     param_image_diff[it_comp].y0 = 0;
839     param_image_diff[it_comp].dx = 0;
840     param_image_diff[it_comp].dy = 0;
841     param_image_diff[it_comp].sgnd = 0;
842     param_image_diff[it_comp].prec = 8;
843     param_image_diff[it_comp].bpp = 1;
844     param_image_diff[it_comp].h = imageBase->comps[it_comp].h;
845     param_image_diff[it_comp].w = imageBase->comps[it_comp].w;
846
847     if (imageBase->comps[it_comp].sgnd != imageTest->comps[it_comp].sgnd)
848       {
849       printf("ERROR: sign mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).sgnd, ((imageTest->comps)[it_comp]).sgnd);
850       goto cleanup;
851       }
852
853     if (((imageBase->comps)[it_comp]).prec != ((imageTest->comps)[it_comp]).prec)
854       {
855       printf("ERROR: prec mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).prec, ((imageTest->comps)[it_comp]).prec);
856       goto cleanup;
857       }
858
859     if (((imageBase->comps)[it_comp]).bpp != ((imageTest->comps)[it_comp]).bpp)
860       {
861       printf("ERROR: byte per pixel mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).bpp, ((imageTest->comps)[it_comp]).bpp);
862       goto cleanup;
863       }
864
865     if (((imageBase->comps)[it_comp]).h != ((imageTest->comps)[it_comp]).h)
866       {
867       printf("ERROR: height mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).h, ((imageTest->comps)[it_comp]).h);
868       goto cleanup;
869       }
870
871     if (((imageBase->comps)[it_comp]).w != ((imageTest->comps)[it_comp]).w)
872       {
873       printf("ERROR: width mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).w, ((imageTest->comps)[it_comp]).w);
874       goto cleanup;
875       }
876     }
877
878    imageDiff = opj_image_create(imageBase->numcomps, param_image_diff, OPJ_CLRSPC_UNSPECIFIED);
879    /* Free memory*/
880    free(param_image_diff); param_image_diff = NULL;
881
882    /* Measurement computation*/
883    printf("Step 2 -> measurement comparison\n");
884
885    memsizedifffilename = strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
886    filenamePNGdiff = (char*) malloc(memsizedifffilename);
887    strcpy(filenamePNGdiff, inParam.test_filename);
888    strcat(filenamePNGdiff, ".diff");
889    /*printf("filenamePNGdiff = %s [%d / %d octets]\n",filenamePNGdiff, strlen(filenamePNGdiff),memsizedifffilename );*/
890
891    /* Compute pixel diff*/
892    for (it_comp = 0; it_comp < imageDiff->numcomps; it_comp++)
893      {
894      double SE=0,PEAK=0;
895      double MSE=0;
896      for (itpxl = 0; itpxl < ((imageDiff->comps)[it_comp]).w * ((imageDiff->comps)[it_comp]).h; itpxl++)
897        {
898        if (abs( ((imageBase->comps)[it_comp]).data[itpxl] - ((imageTest->comps)[it_comp]).data[itpxl] ) > 0)
899          {
900          valueDiff = ((imageBase->comps)[it_comp]).data[itpxl] - ((imageTest->comps)[it_comp]).data[itpxl];
901          ((imageDiff->comps)[it_comp]).data[itpxl] = abs(valueDiff);
902          sumDiff += valueDiff;
903          nbPixelDiff++;
904
905          SE += (double)valueDiff * valueDiff;
906          PEAK = (PEAK > abs(valueDiff)) ? PEAK : abs(valueDiff);
907          }
908        else
909          ((imageDiff->comps)[it_comp]).data[itpxl] = 0;
910        }/* h*w loop */
911
912      MSE = SE / ( ((imageDiff->comps)[it_comp]).w * ((imageDiff->comps)[it_comp]).h );
913
914      if (!inParam.nr_flag && (inParam.tabMSEvalues != NULL) && (inParam.tabPEAKvalues != NULL))
915        { /* Conformance test*/
916        printf("<DartMeasurement name=\"PEAK_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, PEAK);
917        printf("<DartMeasurement name=\"MSE_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, MSE);
918
919        if ( (MSE > inParam.tabMSEvalues[it_comp]) || (PEAK > inParam.tabPEAKvalues[it_comp]) )
920          {
921          printf("ERROR: MSE (%f) or PEAK (%f) values produced by the decoded file are greater "
922            "than the allowable error (respectively %f and %f) \n",
923            MSE, PEAK, inParam.tabMSEvalues[it_comp], inParam.tabPEAKvalues[it_comp]);
924          goto cleanup;
925          }
926        }
927      else  /* Non regression-test */
928        {
929        if ( nbPixelDiff > 0)
930          {
931          char it_compc[255];
932          it_compc[0] = 0;
933
934          printf("<DartMeasurement name=\"NumberOfPixelsWithDifferences_%d\" type=\"numeric/int\"> %d </DartMeasurement> \n", it_comp, nbPixelDiff);
935          printf("<DartMeasurement name=\"ComponentError_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, sumDiff);
936
937 #ifdef OPJ_HAVE_LIBPNG
938            {
939            char *filenamePNGbase_it_comp, *filenamePNGtest_it_comp, *filenamePNGdiff_it_comp;
940
941            filenamePNGbase_it_comp = (char*) malloc((size_t)memsizebasefilename);
942            strcpy(filenamePNGbase_it_comp,filenamePNGbase);
943
944            filenamePNGtest_it_comp = (char*) malloc((size_t)memsizetestfilename);
945            strcpy(filenamePNGtest_it_comp,filenamePNGtest);
946
947            filenamePNGdiff_it_comp = (char*) malloc(memsizedifffilename);
948            strcpy(filenamePNGdiff_it_comp,filenamePNGdiff);
949
950            sprintf(it_compc, "_%i", it_comp);
951            strcat(it_compc,".png");
952            strcat(filenamePNGbase_it_comp, it_compc);
953            /*printf("filenamePNGbase_it = %s [%d / %d octets]\n",filenamePNGbase_it_comp, strlen(filenamePNGbase_it_comp),memsizebasefilename );*/
954            strcat(filenamePNGtest_it_comp, it_compc);
955            /*printf("filenamePNGtest_it = %s [%d / %d octets]\n",filenamePNGtest_it_comp, strlen(filenamePNGtest_it_comp),memsizetestfilename );*/
956            strcat(filenamePNGdiff_it_comp, it_compc);
957            /*printf("filenamePNGdiff_it = %s [%d / %d octets]\n",filenamePNGdiff_it_comp, strlen(filenamePNGdiff_it_comp),memsizedifffilename );*/
958
959            /*
960            if ( imageToPNG(imageBase, filenamePNGbase_it_comp, it_comp) == EXIT_SUCCESS )
961            {
962            printf("<DartMeasurementFile name=\"BaselineImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGbase_it_comp);
963            }
964
965            if ( imageToPNG(imageTest, filenamePNGtest_it_comp, it_comp) == EXIT_SUCCESS )
966            {
967            printf("<DartMeasurementFile name=\"TestImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGtest_it_comp);
968            }
969
970            if ( imageToPNG(imageDiff, filenamePNGdiff_it_comp, it_comp) == EXIT_SUCCESS )
971            {
972            printf("<DartMeasurementFile name=\"DiffferenceImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGdiff_it_comp);
973            }
974             */
975
976            free(filenamePNGbase_it_comp);
977            free(filenamePNGtest_it_comp);
978            free(filenamePNGdiff_it_comp);
979            }
980 #endif
981          goto cleanup;
982          }
983        }
984      } /* it_comp loop */
985
986    printf("---- TEST SUCCEED ----\n");
987    failed = 0;
988 cleanup:
989   /*-----------------------------*/
990   free(param_image_diff);
991   /* Free memory */
992   opj_image_destroy(imageBase);
993   opj_image_destroy(imageTest);
994   opj_image_destroy(imageDiff);
995
996   free(filenamePNGbase);
997   free(filenamePNGtest);
998   free(filenamePNGdiff);
999
1000   free(inParam.tabMSEvalues);
1001   free(inParam.tabPEAKvalues);
1002   free(inParam.base_filename);
1003   free(inParam.test_filename);
1004
1005   return failed ? EXIT_FAILURE : EXIT_SUCCESS;
1006 }