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