backport rev 1013-1014 from trunk
[openjpeg.git] / tests / comparePGXimages.c
1 /*
2  * Copyright (c) 2011, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /*
28  * comparePGXimages.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
40 #include "opj_config.h"
41 #include "opj_getopt.h"
42
43 #include "openjpeg.h"
44 #include "format_defs.h"
45 #include "convert.h"
46
47 double* parseToleranceValues( char* inArg, const int nbcomp);
48 void comparePGXimages_help_display(void);
49 opj_image_t* readImageFromFilePGX(char* filename, int nbFilenamePGX, char *separator);
50 #ifdef HAVE_LIBPNG
51 int imageToPNG(const opj_image_t* image, const char* filename, int num_comp_select);
52 #endif
53
54 typedef struct test_cmp_parameters
55 {
56   /**  */
57   char* base_filename;
58   /**  */
59   char* test_filename;
60   /** Number of components */
61   int nbcomp;
62   /**  */
63   double* tabMSEvalues;
64   /**  */
65   double* tabPEAKvalues;
66   /**  */
67   int nr_flag;
68   /**  */
69   char separator_base[2];
70   /**  */
71   char separator_test[2];
72
73 } test_cmp_parameters;
74
75 /*******************************************************************************
76  * Command line help function
77  *******************************************************************************/
78 void comparePGXimages_help_display(void) {
79   fprintf(stdout,"\nList of parameters for the comparePGX function  \n");
80   fprintf(stdout,"\n");
81   fprintf(stdout,"  -b \t REQUIRED \t filename to the reference/baseline PGX image \n");
82   fprintf(stdout,"  -t \t REQUIRED \t filename to the test PGX image\n");
83   fprintf(stdout,"  -n \t REQUIRED \t number of component of the image (used to generate correct filename)\n");
84   fprintf(stdout,"  -m \t OPTIONAL \t list of MSE tolerances, separated by : (size must correspond to the number of component) of \n");
85   fprintf(stdout,"  -p \t OPTIONAL \t list of PEAK tolerances, separated by : (size must correspond to the number of component) \n");
86   fprintf(stdout,"  -s \t OPTIONAL \t 1 or 2 filename separator to take into account PGX image with different components, "
87                                       "please indicate b or t before separator to indicate respectively the separator "
88                                       "for ref/base file and for test file.  \n");
89   fprintf(stdout,"  -r \t OPTIONAL \t indicate if you want to run this function as conformance test or as non regression test\n");
90   fprintf(stdout,"\n");
91 }
92
93 /*******************************************************************************
94  * Parse command line
95  *******************************************************************************/
96 int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
97 {
98   char *MSElistvalues = NULL;  char *PEAKlistvalues= NULL;
99   char *separatorList = NULL;
100   int sizemembasefile, sizememtestfile;
101   int index, flagM=0, flagP=0;
102   const char optlist[] = "b:t:n:m:p:s:d";
103   int c;
104
105   // Init parameters
106   param->base_filename = NULL;
107   param->test_filename = NULL;
108   param->nbcomp = 0;
109   param->tabMSEvalues = NULL;
110   param->tabPEAKvalues = NULL;
111   param->nr_flag = 0;
112
113   opj_opterr = 0;
114
115   while ((c = opj_getopt(argc, argv, optlist)) != -1)
116     switch (c)
117       {
118       case 'b':
119         sizemembasefile = (int)strlen(opj_optarg)+1;
120         param->base_filename = (char*) malloc(sizemembasefile);
121         param->base_filename[0] = '\0';
122         strncpy(param->base_filename, opj_optarg, strlen(opj_optarg));
123         param->base_filename[strlen(opj_optarg)] = '\0';
124         //printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );
125         break;
126       case 't':
127         sizememtestfile = (int) strlen(opj_optarg) + 1;
128         param->test_filename = (char*) malloc(sizememtestfile);
129         param->test_filename[0] = '\0';
130         strncpy(param->test_filename, opj_optarg, strlen(opj_optarg));
131         param->test_filename[strlen(opj_optarg)] = '\0';
132         //printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);
133        break;
134       case 'n':
135         param->nbcomp = atoi(opj_optarg);
136         break;
137       case 'm':
138         MSElistvalues = opj_optarg;
139         flagM = 1;
140         break;
141       case 'p':
142         PEAKlistvalues = opj_optarg;
143         flagP = 1;
144         break;
145       case 'd':
146         param->nr_flag = 1;
147         break;
148       case 's':
149         separatorList = opj_optarg;
150         break;
151       case '?':
152         if ((opj_optopt == 'b') || (opj_optopt == 't') || (opj_optopt == 'n') || (opj_optopt == 'p') || (opj_optopt == 'm') || (opj_optopt
153             == 's'))
154           fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
155         else
156           if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
157           else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
158         return 1;
159       default:
160         fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
161         break;
162       }
163
164   if (opj_optind != argc)
165     {
166     for (index = opj_optind; index < argc; index++)
167       fprintf(stderr,"Non-option argument %s\n", argv[index]);
168     return EXIT_FAILURE;
169     }
170
171   if (param->nbcomp == 0)
172     {
173     fprintf(stderr,"Need to indicate the number of components !\n");
174     return EXIT_FAILURE;
175     }
176   else
177     {
178     if ( flagM && flagP )
179       {
180       param->tabMSEvalues = parseToleranceValues( MSElistvalues, param->nbcomp);
181       param->tabPEAKvalues = parseToleranceValues( PEAKlistvalues, param->nbcomp);
182       if ( (param->tabMSEvalues == NULL) || (param->tabPEAKvalues == NULL))
183         {
184         fprintf(stderr,"MSE and PEAK values are not correct (respectively need %d values)\n",param->nbcomp);
185         return EXIT_FAILURE;
186         }
187       }
188     /*else
189       {
190
191       }*/
192     }
193
194   // Get separators after corresponding letter (b or t)
195   if (separatorList != NULL)
196     {
197     if( (strlen(separatorList) ==2) || (strlen(separatorList) ==4) )
198       {
199       // keep original string
200       int sizeseplist = (int)strlen(separatorList)+1;
201       char* separatorList2 = (char*)malloc( sizeseplist );
202       separatorList2[0] = '\0';
203       strncpy(separatorList2, separatorList, strlen(separatorList));
204       separatorList2[strlen(separatorList)] = '\0';
205       //printf("separatorList2 = %s [%d / %d]\n", separatorList2, strlen(separatorList2), sizeseplist);
206
207       if (strlen(separatorList) == 2) // one separator behind b or t
208         {
209         char *resultT = NULL;
210         resultT = strtok(separatorList2, "t");
211         if (strlen(resultT) == strlen(separatorList)) // didn't find t character, try to find b
212           {
213           char *resultB = NULL;
214           resultB = strtok(resultT, "b");
215           if (strlen(resultB) == 1)
216             {
217             param->separator_base[0] = separatorList[1];param->separator_base[1] = '\0';
218             param->separator_test[0] ='\0';
219             }
220           else // not found b
221             {
222             free(separatorList2);
223             return EXIT_FAILURE;
224             }
225           }
226         else // found t
227           {
228           param->separator_base[0] ='\0';
229           param->separator_test[0] = separatorList[1];param->separator_test[1] = '\0';
230           }
231         //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) );
232         }
233       else // == 4 characters we must found t and b
234         {
235         char *resultT = NULL;
236         resultT = strtok(separatorList2, "t");
237         if (strlen(resultT) == 3) // found t in first place
238           {
239           char *resultB = NULL;
240           resultB = strtok(resultT, "b");
241           if (strlen(resultB) == 1) // found b after t
242             {
243             param->separator_test[0] = separatorList[1];param->separator_test[1] = '\0';
244             param->separator_base[0] = separatorList[3];param->separator_base[1] = '\0';
245             }
246           else // didn't find b after t
247             {
248             free(separatorList2);
249             return EXIT_FAILURE;
250             }
251           }
252         else // == 2, didn't find t in first place
253           {
254           char *resultB = NULL;
255           resultB = strtok(resultT, "b");
256           if (strlen(resultB) == 1) // found b in first place
257             {
258             param->separator_base[0] = separatorList[1]; param->separator_base[1] = '\0';
259             param->separator_test[0] = separatorList[3]; param->separator_test[1] = '\0';
260             }
261           else // didn't found b in first place => problem
262             {
263             free(separatorList2);
264             return EXIT_FAILURE;
265             }
266           }
267         }
268       free(separatorList2);
269       }
270     else // wrong number of argument after -s
271       {
272       return EXIT_FAILURE;
273       }
274     }
275   else
276     {
277     if (param->nbcomp == 1)
278       {
279       param->separator_base[0] = '\0';
280       param->separator_test[0] = '\0';
281       }
282     else
283       {
284       fprintf(stderr,"If number of component is > 1, we need separator\n");
285       return EXIT_FAILURE;
286       }
287     }
288
289
290   if ( (param->nr_flag) && (flagP || flagM) )
291     {
292     fprintf(stderr,"Wrong input parameters list: it is non-regression test or tolerance comparison\n");
293     return EXIT_FAILURE;
294     }
295   if ( (!param->nr_flag) && (!flagP || !flagM) )
296     {
297     fprintf(stderr,"Wrong input parameters list: it is non-regression test or tolerance comparison\n");
298     return EXIT_FAILURE;
299     }
300
301   return EXIT_SUCCESS;
302 }
303
304 /*******************************************************************************
305  * Parse MSE and PEAK input values (
306  * separator = ":"
307  *******************************************************************************/
308 double* parseToleranceValues( char* inArg, const int nbcomp)
309 {
310   double* outArgs= malloc(nbcomp * sizeof(double));
311   int it_comp = 0;
312   char delims[] = ":";
313   char *result = NULL;
314   result = strtok( inArg, delims );
315
316   while( (result != NULL) && (it_comp < nbcomp ))
317     {
318       outArgs[it_comp] = atof(result);
319       result = strtok( NULL, delims );
320       it_comp++;
321     }
322
323   if (it_comp != nbcomp)
324   {
325         free(outArgs);
326     return NULL;
327   }
328   else
329     return outArgs;
330 }
331 /*******************************************************************************
332  * Create filenames from a filename by used separator and nb components
333  * (begin to 0)
334  *******************************************************************************/
335 char* createMultiComponentsFilename(const char* inFilename, const int indexF, const char* separator)
336 {
337   char s[255];
338   char *outFilename, *ptr;
339   char token = '.';
340   int posToken = 0;
341
342   //printf("inFilename = %s\n", inFilename);
343   if ((ptr = strrchr(inFilename, token)) != NULL)
344     {
345     posToken = (int) (strlen(inFilename) - strlen(ptr));
346     //printf("Position of %c character inside inFilename = %d\n", token, posToken);
347     }
348   else
349     {
350     //printf("Token %c not found\n", token);
351     outFilename = (char*)malloc(1);
352     outFilename[0] = '\0';
353     return outFilename;
354     }
355
356   outFilename = (char*)malloc((posToken + 7) * sizeof(char)); //6
357
358   strncpy(outFilename, inFilename, posToken);
359
360   outFilename[posToken] = '\0';
361
362   strcat(outFilename, separator);
363
364   sprintf(s, "%i", indexF);
365   strcat(outFilename, s);
366
367   strcat(outFilename, ".pgx");
368
369   //printf("outfilename: %s\n", outFilename);
370   return outFilename;
371 }
372 /*******************************************************************************
373  *
374  *******************************************************************************/
375 opj_image_t* readImageFromFilePGX(char* filename, int nbFilenamePGX, char *separator)
376 {
377   int it_file;
378   opj_image_t* image_read = NULL;
379   opj_image_t* image = NULL;
380   opj_cparameters_t parameters;
381   opj_image_cmptparm_t* param_image_read;
382   int** data;
383
384   // If separator is empty => nb file to read is equal to one
385   if ( strlen(separator) == 0 )
386       nbFilenamePGX = 1;
387
388   /* set encoding parameters to default values */
389   opj_set_default_encoder_parameters(&parameters);
390   parameters.decod_format = PGX_DFMT;
391   strncpy(parameters.infile, filename, sizeof(parameters.infile)-1);
392
393   // Allocate memory
394   param_image_read = malloc(nbFilenamePGX * sizeof(opj_image_cmptparm_t));
395   data = malloc(nbFilenamePGX * sizeof(*data));
396
397   it_file = 0;
398   for (it_file = 0; it_file < nbFilenamePGX; it_file++)
399     {
400     // Create the right filename
401     char *filenameComponentPGX;
402     if (strlen(separator) == 0)
403       {
404       filenameComponentPGX = malloc((strlen(filename) + 1) * sizeof(*filenameComponentPGX));
405       strcpy(filenameComponentPGX, filename);
406       }
407     else
408       filenameComponentPGX = createMultiComponentsFilename(filename, it_file, separator);
409
410     // Read the pgx file corresponding to the component
411     image_read = pgxtoimage(filenameComponentPGX, &parameters);
412     if (!image_read)
413     {
414         int it_free_data;
415                 fprintf(stderr, "Unable to load pgx file\n");
416
417                 free(param_image_read);
418
419                 for (it_free_data = 0; it_free_data < it_file; it_free_data++) {
420                         free(data[it_free_data]);
421                 }
422                 free(data);
423
424                 free(filenameComponentPGX);
425
426                 return NULL;
427         }
428
429     // Set the image_read parameters
430     param_image_read[it_file].x0 = 0;
431     param_image_read[it_file].y0 = 0;
432     param_image_read[it_file].dx = 0;
433     param_image_read[it_file].dy = 0;
434     param_image_read[it_file].h = image_read->comps->h;
435     param_image_read[it_file].w = image_read->comps->w;
436     param_image_read[it_file].bpp = image_read->comps->bpp;
437     param_image_read[it_file].prec = image_read->comps->prec;
438     param_image_read[it_file].sgnd = image_read->comps->sgnd;
439
440     // Copy data
441     data[it_file] = malloc(param_image_read[it_file].h * param_image_read[it_file].w * sizeof(int));
442     memcpy(data[it_file], image_read->comps->data, image_read->comps->h * image_read->comps->w * sizeof(int));
443
444     // Free memory
445     opj_image_destroy(image_read);
446     free(filenameComponentPGX);
447     }
448
449   image = opj_image_create(nbFilenamePGX, param_image_read, CLRSPC_UNSPECIFIED);
450   for (it_file = 0; it_file < nbFilenamePGX; it_file++)
451     {
452     // Copy data into output image and free memory
453     memcpy(image->comps[it_file].data, data[it_file], image->comps[it_file].h * image->comps[it_file].w * sizeof(int));
454     free(data[it_file]);
455     }
456
457   // Free memory
458   free(param_image_read);
459   free(data);
460
461   return image;
462 }
463
464 /*******************************************************************************
465  *
466  *******************************************************************************/
467 #ifdef HAVE_LIBPNG
468 int imageToPNG(const opj_image_t* image, const char* filename, int num_comp_select)
469 {
470   opj_image_cmptparm_t param_image_write;
471   opj_image_t* image_write = NULL;
472
473   param_image_write.x0 = 0;
474   param_image_write.y0 = 0;
475   param_image_write.dx = 0;
476   param_image_write.dy = 0;
477   param_image_write.h = image->comps[num_comp_select].h;
478   param_image_write.w = image->comps[num_comp_select].w;
479   param_image_write.bpp = image->comps[num_comp_select].bpp;
480   param_image_write.prec = image->comps[num_comp_select].prec;
481   param_image_write.sgnd = image->comps[num_comp_select].sgnd;
482
483   image_write = opj_image_create(1, &param_image_write, CLRSPC_GRAY);
484   memcpy(image_write->comps->data, image->comps[num_comp_select].data, param_image_write.h * param_image_write.w * sizeof(int));
485
486   imagetopng(image_write, filename);
487
488   opj_image_destroy(image_write);
489
490   return EXIT_SUCCESS;
491 }
492 #endif
493
494 /*******************************************************************************
495  * MAIN
496  *******************************************************************************/
497 int main(int argc, char **argv)
498 {
499   test_cmp_parameters inParam;
500   int it_comp, itpxl;
501   int failed = 0;
502   int nbFilenamePGXbase, nbFilenamePGXtest;
503   char *filenamePNGtest= NULL, *filenamePNGbase = NULL, *filenamePNGdiff = NULL;
504   int memsizebasefilename, memsizetestfilename, memsizedifffilename;
505   int valueDiff = 0, nbPixelDiff = 0;
506   double sumDiff = 0.0;
507   // Structures to store image parameters and data
508   opj_image_t *imageBase = NULL, *imageTest = NULL, *imageDiff = NULL;
509   opj_image_cmptparm_t* param_image_diff;
510
511   // Get parameters from command line
512   if( parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE )
513     {
514     comparePGXimages_help_display();
515     if (inParam.tabMSEvalues) free(inParam.tabMSEvalues);
516     if (inParam.tabPEAKvalues) free(inParam.tabPEAKvalues);
517     if (inParam.base_filename) free(inParam.base_filename);
518     if (inParam.test_filename) free(inParam.test_filename);
519     return EXIT_FAILURE;
520     }
521
522   // Display Parameters
523   printf("******Parameters********* \n");
524   printf(" base_filename = %s\n"
525          " test_filename = %s\n"
526          " nb of Components = %d\n"
527          " Non regression test = %d\n"
528          " separator Base = %s\n"
529          " separator Test = %s\n",
530          inParam.base_filename, inParam.test_filename, inParam.nbcomp,
531          inParam.nr_flag, inParam.separator_base, inParam.separator_test);
532
533   if ( (inParam.tabMSEvalues != NULL) && (inParam.tabPEAKvalues != NULL))
534   {
535     printf(" MSE values = [");
536     for (it_comp = 0; it_comp < inParam.nbcomp; it_comp++)
537       printf(" %f ", inParam.tabMSEvalues[it_comp]);
538     printf("]\n");
539     printf(" PEAK values = [");
540     for (it_comp = 0; it_comp < inParam.nbcomp; it_comp++)
541       printf(" %f ", inParam.tabPEAKvalues[it_comp]);
542     printf("]\n");
543     printf(" Non-regression test = %d\n", inParam.nr_flag);
544     }
545
546   if (strlen(inParam.separator_base) == 0)
547     nbFilenamePGXbase = 0;
548   else
549     nbFilenamePGXbase = inParam.nbcomp;
550
551   if (strlen(inParam.separator_test) == 0)
552     nbFilenamePGXtest = 0;
553   else
554     nbFilenamePGXtest = inParam.nbcomp;
555
556   printf(" NbFilename to generate from base filename = %d\n", nbFilenamePGXbase);
557   printf(" NbFilename to generate from test filename = %d\n", nbFilenamePGXtest);
558   printf("************************* \n");
559
560   //----------BASELINE IMAGE--------
561   //
562   memsizebasefilename = (int)strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
563   memsizetestfilename = (int)strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
564
565   imageBase = readImageFromFilePGX( inParam.base_filename, nbFilenamePGXbase, inParam.separator_base);
566   if ( imageBase != NULL)
567     {
568     filenamePNGbase = (char*) malloc(memsizebasefilename);
569     filenamePNGbase[0] = '\0';
570     strncpy(filenamePNGbase, inParam.test_filename, strlen(inParam.test_filename));
571     filenamePNGbase[strlen(inParam.test_filename)] = '\0';
572     strcat(filenamePNGbase, ".base");
573     //printf("filenamePNGbase = %s [%d / %d octets]\n",filenamePNGbase, strlen(filenamePNGbase),memsizebasefilename );
574     }
575   else
576     {
577     if (inParam.tabMSEvalues) free(inParam.tabMSEvalues);
578     if (inParam.tabPEAKvalues) free(inParam.tabPEAKvalues);
579     if (inParam.base_filename) free(inParam.base_filename);
580     if (inParam.test_filename) free(inParam.test_filename);
581     return EXIT_FAILURE;
582     }
583
584   //----------TEST IMAGE--------
585   //
586
587   imageTest = readImageFromFilePGX(inParam.test_filename, nbFilenamePGXtest, inParam.separator_test);
588   if ( imageTest != NULL)
589     {
590     filenamePNGtest = (char*) malloc(memsizetestfilename);
591     filenamePNGtest[0] = '\0';
592     strncpy(filenamePNGtest, inParam.test_filename, strlen(inParam.test_filename));
593     filenamePNGtest[strlen(inParam.test_filename)] = '\0';
594     strcat(filenamePNGtest, ".test");
595     //printf("filenamePNGtest = %s [%d / %d octets]\n",filenamePNGtest, strlen(filenamePNGtest),memsizetestfilename );
596     }
597   else
598     {
599     if (inParam.tabMSEvalues) free(inParam.tabMSEvalues);
600     if (inParam.tabPEAKvalues) free(inParam.tabPEAKvalues);
601     if (inParam.base_filename) free(inParam.base_filename);
602     if (inParam.test_filename) free(inParam.test_filename);
603     free(filenamePNGbase);
604     return EXIT_FAILURE;
605     }
606
607   //----------DIFF IMAGE--------
608   //
609
610   // Allocate memory
611   param_image_diff = malloc( imageBase->numcomps * sizeof(opj_image_cmptparm_t));
612
613   // Comparison of header parameters
614   printf("Step 1 -> Header comparison\n");
615
616   for (it_comp = 0; it_comp < imageBase->numcomps; it_comp++)
617     {
618     param_image_diff[it_comp].x0 = 0;
619     param_image_diff[it_comp].y0 = 0;
620     param_image_diff[it_comp].dx = 0;
621     param_image_diff[it_comp].dy = 0;
622
623     if (imageBase->comps[it_comp].sgnd != imageTest->comps[it_comp].sgnd)
624       {
625       printf("ERROR: sign mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).sgnd, ((imageTest->comps)[it_comp]).sgnd);
626       failed = 1;
627       }
628     else
629       param_image_diff[it_comp].sgnd = 0 ;
630
631     if (((imageBase->comps)[it_comp]).prec != ((imageTest->comps)[it_comp]).prec)
632       {
633       printf("ERROR: prec mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).prec, ((imageTest->comps)[it_comp]).prec);
634       failed = 1;
635       }
636     else
637       param_image_diff[it_comp].prec = 8 ;
638
639     if (((imageBase->comps)[it_comp]).bpp != ((imageTest->comps)[it_comp]).bpp)
640       {
641       printf("ERROR: byte per pixel mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).bpp, ((imageTest->comps)[it_comp]).bpp);
642       failed = 1;
643       }
644     else
645       param_image_diff[it_comp].bpp = 1 ;
646
647     if (((imageBase->comps)[it_comp]).h != ((imageTest->comps)[it_comp]).h)
648       {
649       printf("ERROR: height mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).h, ((imageTest->comps)[it_comp]).h);
650       failed = 1;
651       }
652     else
653       param_image_diff[it_comp].h = imageBase->comps[it_comp].h ;
654
655     if (((imageBase->comps)[it_comp]).w != ((imageTest->comps)[it_comp]).w)
656       {
657       printf("ERROR: width mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).w, ((imageTest->comps)[it_comp]).w);
658       failed = 1;
659       }
660     else
661       param_image_diff[it_comp].w = imageBase->comps[it_comp].w ;
662     }
663
664    // If only one parameter is different, we stop the test
665    if (failed)
666      {
667      free(inParam.tabMSEvalues);
668      free(inParam.tabPEAKvalues);
669      free(inParam.base_filename);
670      free(inParam.test_filename);
671
672      free(filenamePNGbase);
673      free(filenamePNGtest);
674
675      opj_image_destroy(imageBase);
676      opj_image_destroy(imageTest);
677
678      free(param_image_diff);
679
680      return EXIT_FAILURE;
681      }
682
683    imageDiff = opj_image_create(imageBase->numcomps, param_image_diff, CLRSPC_UNSPECIFIED);
684    // Free memory
685    free(param_image_diff);
686
687    // Measurement computation
688    printf("Step 2 -> measurement comparison\n");
689
690    memsizedifffilename = strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
691    filenamePNGdiff = (char*) malloc(memsizedifffilename);
692    filenamePNGdiff[0] = '\0';
693    strncpy(filenamePNGdiff, inParam.test_filename, strlen(inParam.test_filename));
694    filenamePNGdiff[strlen(inParam.test_filename)] = '\0';
695    strcat(filenamePNGdiff, ".diff");
696    //printf("filenamePNGdiff = %s [%d / %d octets]\n",filenamePNGdiff, strlen(filenamePNGdiff),memsizedifffilename );
697
698    // Compute pixel diff
699    for (it_comp = 0; it_comp < imageDiff->numcomps; it_comp++)
700      {
701      double SE=0,PEAK=0;
702      double MSE=0;
703      char *filenamePNGbase_it_comp, *filenamePNGtest_it_comp, *filenamePNGdiff_it_comp;
704
705      filenamePNGbase_it_comp = (char*) malloc(memsizebasefilename);
706      filenamePNGbase_it_comp[0] = '\0';
707      strncpy(filenamePNGbase_it_comp,filenamePNGbase,strlen(filenamePNGbase));
708      filenamePNGbase_it_comp[strlen(filenamePNGbase)] = '\0';
709
710      filenamePNGtest_it_comp = (char*) malloc(memsizetestfilename);
711      filenamePNGtest_it_comp[0] = '\0';
712      strncpy(filenamePNGtest_it_comp,filenamePNGtest,strlen(filenamePNGtest));
713      filenamePNGtest_it_comp[strlen(filenamePNGtest)] = '\0';
714
715      filenamePNGdiff_it_comp = (char*) malloc(memsizedifffilename);
716      filenamePNGdiff_it_comp[0] = '\0';
717      strncpy(filenamePNGdiff_it_comp,filenamePNGdiff,strlen(filenamePNGdiff));
718      filenamePNGdiff_it_comp[strlen(filenamePNGdiff)] = '\0';
719
720      for (itpxl = 0; itpxl < ((imageDiff->comps)[it_comp]).w * ((imageDiff->comps)[it_comp]).h; itpxl++)
721        {
722        if (abs( ((imageBase->comps)[it_comp]).data[itpxl] - ((imageTest->comps)[it_comp]).data[itpxl] ) > 0)
723          {
724          valueDiff = ((imageBase->comps)[it_comp]).data[itpxl] - ((imageTest->comps)[it_comp]).data[itpxl];
725          ((imageDiff->comps)[it_comp]).data[itpxl] = abs(valueDiff);
726          sumDiff += (double)valueDiff;
727          nbPixelDiff++;
728
729          SE += (double)(valueDiff * valueDiff);
730          PEAK = (PEAK > abs(valueDiff)) ? PEAK : abs(valueDiff);
731          }
732        else
733          ((imageDiff->comps)[it_comp]).data[itpxl] = 0;
734        }// h*w loop
735
736      MSE = SE / ( ((imageDiff->comps)[it_comp]).w * ((imageDiff->comps)[it_comp]).h );
737
738      if (!inParam.nr_flag && (inParam.tabMSEvalues != NULL) && (inParam.tabPEAKvalues != NULL))
739        { // Conformance test
740        printf("<DartMeasurement name=\"PEAK_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, PEAK);
741        printf("<DartMeasurement name=\"MSE_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, MSE);
742
743        if ( (MSE > inParam.tabMSEvalues[it_comp]) || (PEAK > inParam.tabPEAKvalues[it_comp]) )
744          {
745          printf("ERROR: MSE (%f) or PEAK (%f) values produced by the decoded file are greater "
746                 "than the allowable error (respectively %f and %f) \n",
747                 MSE, PEAK, inParam.tabMSEvalues[it_comp], inParam.tabPEAKvalues[it_comp]);
748          failed = 1;
749          }
750        }
751      else  // Non regression-test
752        {
753        if ( nbPixelDiff > 0)
754          {
755          char it_compc[255];
756          it_compc[0] = '\0';
757
758          printf("<DartMeasurement name=\"NumberOfPixelsWithDifferences_%d\" type=\"numeric/int\"> %d </DartMeasurement> \n", it_comp, nbPixelDiff);
759          printf("<DartMeasurement name=\"ComponentError_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, sumDiff);
760
761 #ifdef HAVE_LIBPNG
762          sprintf(it_compc, "_%i", it_comp);
763          strcat(it_compc,".png");
764          strcat(filenamePNGbase_it_comp, it_compc);
765          //printf("filenamePNGbase_it = %s [%d / %d octets]\n",filenamePNGbase_it_comp, strlen(filenamePNGbase_it_comp),memsizebasefilename );
766          strcat(filenamePNGtest_it_comp, it_compc);
767          //printf("filenamePNGtest_it = %s [%d / %d octets]\n",filenamePNGtest_it_comp, strlen(filenamePNGtest_it_comp),memsizetestfilename );
768          strcat(filenamePNGdiff_it_comp, it_compc);
769          //printf("filenamePNGdiff_it = %s [%d / %d octets]\n",filenamePNGdiff_it_comp, strlen(filenamePNGdiff_it_comp),memsizedifffilename );
770
771          if ( imageToPNG(imageBase, filenamePNGbase_it_comp, it_comp) == EXIT_SUCCESS )
772            {
773            printf("<DartMeasurementFile name=\"BaselineImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGbase_it_comp);
774            }
775
776          if ( imageToPNG(imageTest, filenamePNGtest_it_comp, it_comp) == EXIT_SUCCESS )
777            {
778            printf("<DartMeasurementFile name=\"TestImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGtest_it_comp);
779            }
780
781          if ( imageToPNG(imageDiff, filenamePNGdiff_it_comp, it_comp) == EXIT_SUCCESS )
782            {
783            printf("<DartMeasurementFile name=\"DiffferenceImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGdiff_it_comp);
784            }
785 #endif
786          failed = 1;
787          }
788        }
789      free(filenamePNGbase_it_comp);
790      free(filenamePNGtest_it_comp);
791      free(filenamePNGdiff_it_comp);
792      } // it_comp loop
793
794   //-----------------------------
795   // Free memory
796   opj_image_destroy(imageBase);
797   opj_image_destroy(imageTest);
798   opj_image_destroy(imageDiff);
799
800   free(filenamePNGbase);
801   free(filenamePNGtest);
802   free(filenamePNGdiff);
803
804   free(inParam.tabMSEvalues);
805   free(inParam.tabPEAKvalues);
806   free(inParam.base_filename);
807   free(inParam.test_filename);
808
809   if (failed)
810     return EXIT_FAILURE;
811   else
812     {
813     printf("---- TEST SUCCEED ----\n");
814     return EXIT_SUCCESS;
815     }
816 }