Add performance benchmarking scripts
[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, not used when both input files are TIF)\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 ppm file: %s\n", filenameComponentPGX);
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   opj_image_t* image_read = NULL;
251   opj_cparameters_t parameters;
252   (void)nbFilenamePGX;
253   (void)separator;
254
255   /* conformance test suite produce annoying warning/error:
256    * TIFFReadDirectory: Warning, /.../data/baseline/conformance/jp2_1.tif: unknown field with tag 37724 (0x935c) encountered.
257    * TIFFOpen: /.../data/baseline/nonregression/opj_jp2_1.tif: Cannot open.
258    * On Win32 this open a message box by default, so remove it from the test suite:
259    */
260 #ifdef OPJ_HAVE_LIBTIFF
261   TIFFSetWarningHandler(NULL);
262   TIFFSetErrorHandler(NULL);
263 #endif
264
265   if ( strlen(separator) != 0 ) return NULL;
266
267   /* set encoding parameters to default values */
268   opj_set_default_encoder_parameters(&parameters);
269   parameters.decod_format = TIF_DFMT;
270   strcpy(parameters.infile, filename);
271
272   /* Read the tif file corresponding to the component */
273 #ifdef OPJ_HAVE_LIBTIFF
274   image_read = tiftoimage(filename, &parameters);
275 #endif
276   if (!image_read)
277     {
278     fprintf(stderr, "Unable to load TIF file\n");
279     return NULL;
280     }
281
282   return image_read;
283 }
284
285 static opj_image_t* readImageFromFilePGX(const char* filename, int nbFilenamePGX, const char *separator)
286 {
287   int it_file;
288   opj_image_t* image_read = NULL;
289   opj_image_t* image = NULL;
290   opj_cparameters_t parameters;
291   opj_image_cmptparm_t* param_image_read;
292   int** data;
293
294   /* If separator is empty => nb file to read is equal to one*/
295   if ( strlen(separator) == 0 )
296     nbFilenamePGX = 1;
297
298   /* set encoding parameters to default values */
299   opj_set_default_encoder_parameters(&parameters);
300   parameters.decod_format = PGX_DFMT;
301   strcpy(parameters.infile, filename);
302
303   /* Allocate memory*/
304   param_image_read = malloc((size_t)nbFilenamePGX * sizeof(opj_image_cmptparm_t));
305   data = malloc((size_t)nbFilenamePGX * sizeof(*data));
306
307   for (it_file = 0; it_file < nbFilenamePGX; it_file++)
308     {
309     /* Create the right filename*/
310     char *filenameComponentPGX;
311     if (strlen(separator) == 0)
312       {
313       filenameComponentPGX = malloc((strlen(filename) + 1) * sizeof(*filenameComponentPGX));
314       strcpy(filenameComponentPGX, filename);
315       }
316     else
317       filenameComponentPGX = createMultiComponentsFilename(filename, it_file, separator);
318
319     /* Read the pgx file corresponding to the component */
320     image_read = pgxtoimage(filenameComponentPGX, &parameters);
321     if (!image_read)
322       {
323       int it_free_data;
324       fprintf(stderr, "Unable to load pgx file\n");
325
326       free(param_image_read);
327
328       for (it_free_data = 0; it_free_data < it_file; it_free_data++) {
329         free(data[it_free_data]);
330       }
331       free(data);
332
333       free(filenameComponentPGX);
334
335       return NULL;
336       }
337
338     /* Set the image_read parameters*/
339     param_image_read[it_file].x0 = 0;
340     param_image_read[it_file].y0 = 0;
341     param_image_read[it_file].dx = 0;
342     param_image_read[it_file].dy = 0;
343     param_image_read[it_file].h = image_read->comps->h;
344     param_image_read[it_file].w = image_read->comps->w;
345     param_image_read[it_file].bpp = image_read->comps->bpp;
346     param_image_read[it_file].prec = image_read->comps->prec;
347     param_image_read[it_file].sgnd = image_read->comps->sgnd;
348
349     /* Copy data*/
350     data[it_file] = malloc(param_image_read[it_file].h * param_image_read[it_file].w * sizeof(int));
351     memcpy(data[it_file], image_read->comps->data, image_read->comps->h * image_read->comps->w * sizeof(int));
352
353     /* Free memory*/
354     opj_image_destroy(image_read);
355     free(filenameComponentPGX);
356     }
357
358   image = opj_image_create((OPJ_UINT32)nbFilenamePGX, param_image_read, OPJ_CLRSPC_UNSPECIFIED);
359   for (it_file = 0; it_file < nbFilenamePGX; it_file++)
360     {
361     /* Copy data into output image and free memory*/
362     memcpy(image->comps[it_file].data, data[it_file], image->comps[it_file].h * image->comps[it_file].w * sizeof(int));
363     free(data[it_file]);
364     }
365
366   /* Free memory*/
367   free(param_image_read);
368   free(data);
369
370   return image;
371 }
372
373 #if defined(OPJ_HAVE_LIBPNG) && 0 /* remove for now */
374 /*******************************************************************************
375  *
376  *******************************************************************************/
377 static int imageToPNG(const opj_image_t* image, const char* filename, int num_comp_select)
378 {
379   opj_image_cmptparm_t param_image_write;
380   opj_image_t* image_write = NULL;
381
382   param_image_write.x0 = 0;
383   param_image_write.y0 = 0;
384   param_image_write.dx = 0;
385   param_image_write.dy = 0;
386   param_image_write.h = image->comps[num_comp_select].h;
387   param_image_write.w = image->comps[num_comp_select].w;
388   param_image_write.bpp = image->comps[num_comp_select].bpp;
389   param_image_write.prec = image->comps[num_comp_select].prec;
390   param_image_write.sgnd = image->comps[num_comp_select].sgnd;
391
392   image_write = opj_image_create(1u, &param_image_write, OPJ_CLRSPC_GRAY);
393   memcpy(image_write->comps->data, image->comps[num_comp_select].data, param_image_write.h * param_image_write.w * sizeof(int));
394
395   imagetopng(image_write, filename);
396
397   opj_image_destroy(image_write);
398
399   return EXIT_SUCCESS;
400 }
401 #endif
402
403 typedef struct test_cmp_parameters
404 {
405   /**  */
406   char* base_filename;
407   /**  */
408   char* test_filename;
409   /** Number of components */
410   int nbcomp;
411   /**  */
412   double* tabMSEvalues;
413   /**  */
414   double* tabPEAKvalues;
415   /**  */
416   int nr_flag;
417   /**  */
418   char separator_base[2];
419   /**  */
420   char separator_test[2];
421
422 } test_cmp_parameters;
423
424 /* return decode format PGX / TIF / PPM , return -1 on error */
425 static int get_decod_format(test_cmp_parameters* param)
426 {
427   int base_format = get_decod_format_from_string( param->base_filename );
428   int test_format = get_decod_format_from_string( param->test_filename );
429   if( base_format != test_format ) return -1;
430   /* handle case -1: */
431   return base_format;
432 }
433
434 /*******************************************************************************
435  * Parse command line
436  *******************************************************************************/
437 static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
438 {
439   char *MSElistvalues = NULL;  char *PEAKlistvalues= NULL;
440   char *separatorList = NULL;
441   size_t sizemembasefile, sizememtestfile;
442   int index, flagM=0, flagP=0;
443   const char optlist[] = "b:t:n:m:p:s:d";
444   int c;
445
446   /* Init parameters*/
447   param->base_filename = NULL;
448   param->test_filename = NULL;
449   param->nbcomp = 0;
450   param->tabMSEvalues = NULL;
451   param->tabPEAKvalues = NULL;
452   param->nr_flag = 0;
453   param->separator_base[0] = 0;
454   param->separator_test[0] = 0;
455
456   opj_opterr = 0;
457
458   while ((c = opj_getopt(argc, argv, optlist)) != -1)
459     switch (c)
460       {
461       case 'b':
462         sizemembasefile = strlen(opj_optarg) + 1;
463         param->base_filename = (char*) malloc(sizemembasefile);
464         strcpy(param->base_filename, opj_optarg);
465         /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
466         break;
467       case 't':
468         sizememtestfile = strlen(opj_optarg) + 1;
469         param->test_filename = (char*) malloc(sizememtestfile);
470         strcpy(param->test_filename, opj_optarg);
471         /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
472        break;
473       case 'n':
474         param->nbcomp = atoi(opj_optarg);
475         break;
476       case 'm':
477         MSElistvalues = opj_optarg;
478         flagM = 1;
479         break;
480       case 'p':
481         PEAKlistvalues = opj_optarg;
482         flagP = 1;
483         break;
484       case 'd':
485         param->nr_flag = 1;
486         break;
487       case 's':
488         separatorList = opj_optarg;
489         break;
490       case '?':
491         if ((opj_optopt == 'b') || (opj_optopt == 't') || (opj_optopt == 'n') || (opj_optopt == 'p') || (opj_optopt == 'm') || (opj_optopt
492             == 's'))
493           fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
494         else
495           if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
496           else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
497         return 1;
498       default:
499         fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
500         break;
501       }
502
503   if (opj_optind != argc)
504     {
505     for (index = opj_optind; index < argc; index++)
506       fprintf(stderr,"Non-option argument %s\n", argv[index]);
507     return 1;
508     }
509
510   if (param->nbcomp == 0)
511     {
512     fprintf(stderr,"Need to indicate the number of components !\n");
513     return 1;
514     }
515   /* else */
516   if ( flagM && flagP )
517     {
518     param->tabMSEvalues = parseToleranceValues( MSElistvalues, param->nbcomp);
519     param->tabPEAKvalues = parseToleranceValues( PEAKlistvalues, param->nbcomp);
520     if ( (param->tabMSEvalues == NULL) || (param->tabPEAKvalues == NULL))
521       {
522       fprintf(stderr,"MSE and PEAK values are not correct (respectively need %d values)\n",param->nbcomp);
523       return 1;
524       }
525     }
526
527   /* Get separators after corresponding letter (b or t)*/
528   if (separatorList != NULL)
529     {
530     if( (strlen(separatorList) ==2) || (strlen(separatorList) ==4) )
531       {
532       /* keep original string*/
533       size_t sizeseplist = strlen(separatorList)+1;
534       char* separatorList2 = (char*)malloc( sizeseplist );
535       strcpy(separatorList2, separatorList);
536       /*printf("separatorList2 = %s [%d / %d]\n", separatorList2, strlen(separatorList2), sizeseplist);*/
537
538       if (strlen(separatorList) == 2) /* one separator behind b or t*/
539         {
540         char *resultT = NULL;
541         resultT = strtok(separatorList2, "t");
542         if (strlen(resultT) == strlen(separatorList)) /* didn't find t character, try to find b*/
543           {
544           char *resultB = NULL;
545           resultB = strtok(resultT, "b");
546           if (strlen(resultB) == 1)
547             {
548             param->separator_base[0] = separatorList[1];
549             param->separator_base[1] = 0;
550             param->separator_test[0] = 0;
551             }
552           else /* not found b*/
553             {
554             free(separatorList2);
555             return 1;
556             }
557           }
558         else /* found t*/
559           {
560           param->separator_base[0] = 0;
561           param->separator_test[0] = separatorList[1];
562           param->separator_test[1] = 0;
563           }
564         /*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) );*/
565         }
566       else /* == 4 characters we must found t and b*/
567         {
568         char *resultT = NULL;
569         resultT = strtok(separatorList2, "t");
570         if (strlen(resultT) == 3) /* found t in first place*/
571           {
572           char *resultB = NULL;
573           resultB = strtok(resultT, "b");
574           if (strlen(resultB) == 1) /* found b after t*/
575             {
576             param->separator_test[0] = separatorList[1];
577             param->separator_test[1] = 0;
578             param->separator_base[0] = separatorList[3];
579             param->separator_base[1] = 0;
580             }
581           else /* didn't find b after t*/
582             {
583             free(separatorList2);
584             return 1;
585             }
586           }
587         else /* == 2, didn't find t in first place*/
588           {
589           char *resultB = NULL;
590           resultB = strtok(resultT, "b");
591           if (strlen(resultB) == 1) /* found b in first place*/
592             {
593             param->separator_base[0] = separatorList[1];
594             param->separator_base[1] = 0;
595             param->separator_test[0] = separatorList[3];
596             param->separator_test[1] = 0;
597             }
598           else /* didn't found b in first place => problem*/
599             {
600             free(separatorList2);
601             return 1;
602             }
603           }
604         }
605       free(separatorList2);
606       }
607     else /* wrong number of argument after -s*/
608       {
609       return 1;
610       }
611     }
612   else
613     {
614     if (param->nbcomp == 1)
615       {
616       assert( param->separator_base[0] == 0 );
617       assert( param->separator_test[0] == 0 );
618       }
619     else
620       {
621       fprintf(stderr,"If number of component is > 1, we need separator\n");
622       return 1;
623       }
624     }
625
626
627   if ( (param->nr_flag) && (flagP || flagM) )
628     {
629     fprintf(stderr,"Wrong input parameters list: it is non-regression test or tolerance comparison\n");
630     return 1;
631     }
632   if ( (!param->nr_flag) && (!flagP || !flagM) )
633     {
634     fprintf(stderr,"Wrong input parameters list: it is non-regression test or tolerance comparison\n");
635     return 1;
636     }
637
638   return 0;
639 }
640
641 /*******************************************************************************
642  * MAIN
643  *******************************************************************************/
644 int main(int argc, char **argv)
645 {
646   test_cmp_parameters inParam;
647   OPJ_UINT32 it_comp, itpxl;
648   int failed = 1;
649   int nbFilenamePGXbase = 0, nbFilenamePGXtest = 0;
650   char *filenamePNGtest= NULL, *filenamePNGbase = NULL, *filenamePNGdiff = NULL;
651   size_t memsizebasefilename, memsizetestfilename;
652   size_t memsizedifffilename;
653   int valueDiff = 0, nbPixelDiff = 0;
654   double sumDiff = 0.0;
655   /* Structures to store image parameters and data*/
656   opj_image_t *imageBase = NULL, *imageTest = NULL, *imageDiff = NULL;
657   opj_image_cmptparm_t* param_image_diff = NULL;
658   int decod_format;
659
660   /* Get parameters from command line*/
661   if( parse_cmdline_cmp(argc, argv, &inParam) )
662     {
663     compare_images_help_display();
664     goto cleanup;
665     }
666
667   /* Display Parameters*/
668   printf("******Parameters********* \n");
669   printf(" base_filename = %s\n"
670          " test_filename = %s\n"
671          " nb of Components = %d\n"
672          " Non regression test = %d\n"
673          " separator Base = %s\n"
674          " separator Test = %s\n",
675          inParam.base_filename, inParam.test_filename, inParam.nbcomp,
676          inParam.nr_flag, inParam.separator_base, inParam.separator_test);
677
678   if ( (inParam.tabMSEvalues != NULL) && (inParam.tabPEAKvalues != NULL))
679     {
680     int it_comp2;
681     printf(" MSE values = [");
682     for (it_comp2 = 0; it_comp2 < inParam.nbcomp; it_comp2++)
683       printf(" %f ", inParam.tabMSEvalues[it_comp2]);
684     printf("]\n");
685     printf(" PEAK values = [");
686     for (it_comp2 = 0; it_comp2 < inParam.nbcomp; it_comp2++)
687       printf(" %f ", inParam.tabPEAKvalues[it_comp2]);
688     printf("]\n");
689     printf(" Non-regression test = %d\n", inParam.nr_flag);
690     }
691
692   if (strlen(inParam.separator_base) != 0)
693     nbFilenamePGXbase = inParam.nbcomp;
694
695   if (strlen(inParam.separator_test) != 0)
696     nbFilenamePGXtest = inParam.nbcomp;
697
698   printf(" NbFilename to generate from base filename = %d\n", nbFilenamePGXbase);
699   printf(" NbFilename to generate from test filename = %d\n", nbFilenamePGXtest);
700   printf("************************* \n");
701
702   /*----------BASELINE IMAGE--------*/
703   memsizebasefilename = strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
704   memsizetestfilename = strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
705
706   decod_format = get_decod_format(&inParam);
707   if( decod_format == -1 )
708     {
709     fprintf( stderr, "Unhandled file format\n" );
710     goto cleanup;
711     }
712   assert( decod_format == PGX_DFMT || decod_format == TIF_DFMT || decod_format == PXM_DFMT );
713
714   if( decod_format == PGX_DFMT )
715     {
716     imageBase = readImageFromFilePGX( inParam.base_filename, nbFilenamePGXbase, inParam.separator_base);
717     if ( imageBase == NULL )
718       goto cleanup;
719     }
720   else if( decod_format == TIF_DFMT )
721     {
722     imageBase = readImageFromFileTIF( inParam.base_filename, nbFilenamePGXbase, "");
723     if ( imageBase == NULL )
724       goto cleanup;
725     }
726   else if( decod_format == PXM_DFMT )
727     {
728     imageBase = readImageFromFilePPM( inParam.base_filename, nbFilenamePGXbase, inParam.separator_base);
729     if ( imageBase == NULL )
730       goto cleanup;
731     }
732
733   filenamePNGbase = (char*) malloc(memsizebasefilename);
734   strcpy(filenamePNGbase, inParam.test_filename);
735   strcat(filenamePNGbase, ".base");
736   /*printf("filenamePNGbase = %s [%d / %d octets]\n",filenamePNGbase, strlen(filenamePNGbase),memsizebasefilename );*/
737
738   /*----------TEST IMAGE--------*/
739
740   if( decod_format == PGX_DFMT )
741     {
742     imageTest = readImageFromFilePGX(inParam.test_filename, nbFilenamePGXtest, inParam.separator_test);
743     if ( imageTest == NULL )
744       goto cleanup;
745     }
746   else if( decod_format == TIF_DFMT )
747     {
748     imageTest = readImageFromFileTIF(inParam.test_filename, nbFilenamePGXtest, "");
749     if ( imageTest == NULL )
750       goto cleanup;
751     }
752   else if( decod_format == PXM_DFMT )
753     {
754     imageTest = readImageFromFilePPM(inParam.test_filename, nbFilenamePGXtest, inParam.separator_test);
755     if ( imageTest == NULL )
756       goto cleanup;
757     }
758
759   filenamePNGtest = (char*) malloc(memsizetestfilename);
760   strcpy(filenamePNGtest, inParam.test_filename);
761   strcat(filenamePNGtest, ".test");
762   /*printf("filenamePNGtest = %s [%d / %d octets]\n",filenamePNGtest, strlen(filenamePNGtest),memsizetestfilename );*/
763
764   /*----------DIFF IMAGE--------*/
765
766   /* Allocate memory*/
767   param_image_diff = malloc( imageBase->numcomps * sizeof(opj_image_cmptparm_t));
768
769   /* Comparison of header parameters*/
770   printf("Step 1 -> Header comparison\n");
771
772   /* check dimensions (issue 286)*/
773   if(imageBase->numcomps != imageTest->numcomps )
774     {
775     printf("ERROR: dim mismatch (%d><%d)\n", imageBase->numcomps, imageTest->numcomps);
776     goto cleanup;
777     }
778
779   for (it_comp = 0; it_comp < imageBase->numcomps; it_comp++)
780     {
781     param_image_diff[it_comp].x0 = 0;
782     param_image_diff[it_comp].y0 = 0;
783     param_image_diff[it_comp].dx = 0;
784     param_image_diff[it_comp].dy = 0;
785     param_image_diff[it_comp].sgnd = 0;
786     param_image_diff[it_comp].prec = 8;
787     param_image_diff[it_comp].bpp = 1;
788     param_image_diff[it_comp].h = imageBase->comps[it_comp].h;
789     param_image_diff[it_comp].w = imageBase->comps[it_comp].w;
790
791     if (imageBase->comps[it_comp].sgnd != imageTest->comps[it_comp].sgnd)
792       {
793       printf("ERROR: sign mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).sgnd, ((imageTest->comps)[it_comp]).sgnd);
794       goto cleanup;
795       }
796
797     if (((imageBase->comps)[it_comp]).prec != ((imageTest->comps)[it_comp]).prec)
798       {
799       printf("ERROR: prec mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).prec, ((imageTest->comps)[it_comp]).prec);
800       goto cleanup;
801       }
802
803     if (((imageBase->comps)[it_comp]).bpp != ((imageTest->comps)[it_comp]).bpp)
804       {
805       printf("ERROR: byte per pixel mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).bpp, ((imageTest->comps)[it_comp]).bpp);
806       goto cleanup;
807       }
808
809     if (((imageBase->comps)[it_comp]).h != ((imageTest->comps)[it_comp]).h)
810       {
811       printf("ERROR: height mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).h, ((imageTest->comps)[it_comp]).h);
812       goto cleanup;
813       }
814
815     if (((imageBase->comps)[it_comp]).w != ((imageTest->comps)[it_comp]).w)
816       {
817       printf("ERROR: width mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).w, ((imageTest->comps)[it_comp]).w);
818       goto cleanup;
819       }
820     }
821
822    imageDiff = opj_image_create(imageBase->numcomps, param_image_diff, OPJ_CLRSPC_UNSPECIFIED);
823    /* Free memory*/
824    free(param_image_diff); param_image_diff = NULL;
825
826    /* Measurement computation*/
827    printf("Step 2 -> measurement comparison\n");
828
829    memsizedifffilename = strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
830    filenamePNGdiff = (char*) malloc(memsizedifffilename);
831    strcpy(filenamePNGdiff, inParam.test_filename);
832    strcat(filenamePNGdiff, ".diff");
833    /*printf("filenamePNGdiff = %s [%d / %d octets]\n",filenamePNGdiff, strlen(filenamePNGdiff),memsizedifffilename );*/
834
835    /* Compute pixel diff*/
836    for (it_comp = 0; it_comp < imageDiff->numcomps; it_comp++)
837      {
838      double SE=0,PEAK=0;
839      double MSE=0;
840      for (itpxl = 0; itpxl < ((imageDiff->comps)[it_comp]).w * ((imageDiff->comps)[it_comp]).h; itpxl++)
841        {
842        if (abs( ((imageBase->comps)[it_comp]).data[itpxl] - ((imageTest->comps)[it_comp]).data[itpxl] ) > 0)
843          {
844          valueDiff = ((imageBase->comps)[it_comp]).data[itpxl] - ((imageTest->comps)[it_comp]).data[itpxl];
845          ((imageDiff->comps)[it_comp]).data[itpxl] = abs(valueDiff);
846          sumDiff += valueDiff;
847          nbPixelDiff++;
848
849          SE += (double)valueDiff * valueDiff;
850          PEAK = (PEAK > abs(valueDiff)) ? PEAK : abs(valueDiff);
851          }
852        else
853          ((imageDiff->comps)[it_comp]).data[itpxl] = 0;
854        }/* h*w loop */
855
856      MSE = SE / ( ((imageDiff->comps)[it_comp]).w * ((imageDiff->comps)[it_comp]).h );
857
858      if (!inParam.nr_flag && (inParam.tabMSEvalues != NULL) && (inParam.tabPEAKvalues != NULL))
859        { /* Conformance test*/
860        printf("<DartMeasurement name=\"PEAK_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, PEAK);
861        printf("<DartMeasurement name=\"MSE_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, MSE);
862
863        if ( (MSE > inParam.tabMSEvalues[it_comp]) || (PEAK > inParam.tabPEAKvalues[it_comp]) )
864          {
865          printf("ERROR: MSE (%f) or PEAK (%f) values produced by the decoded file are greater "
866            "than the allowable error (respectively %f and %f) \n",
867            MSE, PEAK, inParam.tabMSEvalues[it_comp], inParam.tabPEAKvalues[it_comp]);
868          goto cleanup;
869          }
870        }
871      else  /* Non regression-test */
872        {
873        if ( nbPixelDiff > 0)
874          {
875          char it_compc[255];
876          it_compc[0] = 0;
877
878          printf("<DartMeasurement name=\"NumberOfPixelsWithDifferences_%d\" type=\"numeric/int\"> %d </DartMeasurement> \n", it_comp, nbPixelDiff);
879          printf("<DartMeasurement name=\"ComponentError_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, sumDiff);
880          printf("<DartMeasurement name=\"PEAK_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, PEAK);
881          printf("<DartMeasurement name=\"MSE_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, MSE);
882
883 #ifdef OPJ_HAVE_LIBPNG
884            {
885            char *filenamePNGbase_it_comp, *filenamePNGtest_it_comp, *filenamePNGdiff_it_comp;
886
887            filenamePNGbase_it_comp = (char*) malloc(memsizebasefilename);
888            strcpy(filenamePNGbase_it_comp,filenamePNGbase);
889
890            filenamePNGtest_it_comp = (char*) malloc(memsizetestfilename);
891            strcpy(filenamePNGtest_it_comp,filenamePNGtest);
892
893            filenamePNGdiff_it_comp = (char*) malloc(memsizedifffilename);
894            strcpy(filenamePNGdiff_it_comp,filenamePNGdiff);
895
896            sprintf(it_compc, "_%i", it_comp);
897            strcat(it_compc,".png");
898            strcat(filenamePNGbase_it_comp, it_compc);
899            /*printf("filenamePNGbase_it = %s [%d / %d octets]\n",filenamePNGbase_it_comp, strlen(filenamePNGbase_it_comp),memsizebasefilename );*/
900            strcat(filenamePNGtest_it_comp, it_compc);
901            /*printf("filenamePNGtest_it = %s [%d / %d octets]\n",filenamePNGtest_it_comp, strlen(filenamePNGtest_it_comp),memsizetestfilename );*/
902            strcat(filenamePNGdiff_it_comp, it_compc);
903            /*printf("filenamePNGdiff_it = %s [%d / %d octets]\n",filenamePNGdiff_it_comp, strlen(filenamePNGdiff_it_comp),memsizedifffilename );*/
904
905            /*
906            if ( imageToPNG(imageBase, filenamePNGbase_it_comp, it_comp) == EXIT_SUCCESS )
907            {
908            printf("<DartMeasurementFile name=\"BaselineImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGbase_it_comp);
909            }
910
911            if ( imageToPNG(imageTest, filenamePNGtest_it_comp, it_comp) == EXIT_SUCCESS )
912            {
913            printf("<DartMeasurementFile name=\"TestImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGtest_it_comp);
914            }
915
916            if ( imageToPNG(imageDiff, filenamePNGdiff_it_comp, it_comp) == EXIT_SUCCESS )
917            {
918            printf("<DartMeasurementFile name=\"DiffferenceImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGdiff_it_comp);
919            }
920             */
921
922            free(filenamePNGbase_it_comp);
923            free(filenamePNGtest_it_comp);
924            free(filenamePNGdiff_it_comp);
925            }
926 #endif
927          goto cleanup;
928          }
929        }
930      } /* it_comp loop */
931
932    printf("---- TEST SUCCEED ----\n");
933    failed = 0;
934 cleanup:
935   /*-----------------------------*/
936   free(param_image_diff);
937   /* Free memory */
938   opj_image_destroy(imageBase);
939   opj_image_destroy(imageTest);
940   opj_image_destroy(imageDiff);
941
942   free(filenamePNGbase);
943   free(filenamePNGtest);
944   free(filenamePNGdiff);
945
946   free(inParam.tabMSEvalues);
947   free(inParam.tabPEAKvalues);
948   free(inParam.base_filename);
949   free(inParam.test_filename);
950
951   return failed ? EXIT_FAILURE : EXIT_SUCCESS;
952 }