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