2 * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 * Created on: 8 juil. 2011
41 #include "opj_apps_config.h"
42 #include "opj_getopt.h"
45 #include "format_defs.h"
48 #ifdef OPJ_HAVE_LIBTIFF
49 #include <tiffio.h> /* TIFFSetWarningHandler */
50 #endif /* OPJ_HAVE_LIBTIFF */
52 /*******************************************************************************
53 * Parse MSE and PEAK input values (
55 *******************************************************************************/
56 static double* parseToleranceValues( char* inArg, const int nbcomp)
58 double* outArgs= malloc((size_t)nbcomp * sizeof(double));
60 const char delims[] = ":";
61 char *result = strtok( inArg, delims );
63 while( (result != NULL) && (it_comp < nbcomp ))
65 outArgs[it_comp] = atof(result);
66 result = strtok( NULL, delims );
70 if (it_comp != nbcomp)
79 /*******************************************************************************
80 * Command line help function
81 *******************************************************************************/
82 static void compare_images_help_display(void)
84 fprintf(stdout,"\nList of parameters for the compare_images function \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");
98 /*******************************************************************************
99 * Create filenames from a filename using separator and nb components
101 *******************************************************************************/
102 static char* createMultiComponentsFilename(const char* inFilename, const int indexF, const char* separator)
105 char *outFilename, *ptr;
106 const char token = '.';
109 /*printf("inFilename = %s\n", inFilename);*/
110 if ((ptr = strrchr(inFilename, token)) != NULL)
112 posToken = (int) (strlen(inFilename) - strlen(ptr));
113 /*printf("Position of %c character inside inFilename = %d\n", token, posToken);*/
117 /*printf("Token %c not found\n", token);*/
118 outFilename = (char*)malloc(1);
119 outFilename[0] = '\0';
123 outFilename = (char*)malloc((size_t)(posToken + 7) * sizeof(char)); /*6*/
125 strncpy(outFilename, inFilename, (size_t)posToken);
127 outFilename[posToken] = '\0';
129 strcat(outFilename, separator);
131 sprintf(s, "%i", indexF);
132 strcat(outFilename, s);
134 strcat(outFilename, ".pgx");
136 /*printf("outfilename: %s\n", outFilename);*/
140 /*******************************************************************************
142 *******************************************************************************/
143 static opj_image_t* readImageFromFilePPM(const char* filename, int nbFilenamePGX, const char *separator)
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;
152 /* If separator is empty => nb file to read is equal to one*/
153 if ( strlen(separator) == 0 )
156 /* set encoding parameters to default values */
157 opj_set_default_encoder_parameters(¶meters);
158 parameters.decod_format = PXM_DFMT;
159 strcpy(parameters.infile, filename);
162 param_image_read = malloc((size_t)nbFilenamePGX * sizeof(opj_image_cmptparm_t));
163 data = malloc((size_t)nbFilenamePGX * sizeof(*data));
165 for (it_file = 0; it_file < nbFilenamePGX; it_file++)
167 /* Create the right filename*/
168 char *filenameComponentPGX;
169 if (strlen(separator) == 0)
171 filenameComponentPGX = malloc((strlen(filename) + 1) * sizeof(*filenameComponentPGX));
172 strcpy(filenameComponentPGX, filename);
175 filenameComponentPGX = createMultiComponentsFilename(filename, it_file, separator);
177 /* Read the tif file corresponding to the component */
178 image_read = pnmtoimage(filenameComponentPGX, ¶meters);
182 fprintf(stderr, "Unable to load pgx file\n");
184 free(param_image_read);
186 for (it_free_data = 0; it_free_data < it_file; it_free_data++) {
187 free(data[it_free_data]);
191 free(filenameComponentPGX);
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;
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));
212 opj_image_destroy(image_read);
213 free(filenameComponentPGX);
216 image = opj_image_create((OPJ_UINT32)nbFilenamePGX, param_image_read, OPJ_CLRSPC_UNSPECIFIED);
217 for (it_file = 0; it_file < nbFilenamePGX; it_file++)
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));
225 free(param_image_read);
231 static opj_image_t* readImageFromFileTIF(const char* filename, int nbFilenamePGX, const char *separator)
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;
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:
245 TIFFSetWarningHandler(NULL);
246 TIFFSetErrorHandler(NULL);
248 /* If separator is empty => nb file to read is equal to one*/
249 if ( strlen(separator) == 0 )
252 /* set encoding parameters to default values */
253 opj_set_default_encoder_parameters(¶meters);
254 parameters.decod_format = TIF_DFMT;
255 strcpy(parameters.infile, filename);
258 param_image_read = malloc((size_t)nbFilenamePGX * sizeof(opj_image_cmptparm_t));
259 data = malloc((size_t)nbFilenamePGX * sizeof(*data));
261 for (it_file = 0; it_file < nbFilenamePGX; it_file++)
263 /* Create the right filename*/
264 char *filenameComponentPGX;
265 if (strlen(separator) == 0)
267 filenameComponentPGX = malloc((strlen(filename) + 1) * sizeof(*filenameComponentPGX));
268 strcpy(filenameComponentPGX, filename);
271 filenameComponentPGX = createMultiComponentsFilename(filename, it_file, separator);
273 /* Read the tif file corresponding to the component */
274 image_read = tiftoimage(filenameComponentPGX, ¶meters);
278 fprintf(stderr, "Unable to load pgx file\n");
280 free(param_image_read);
282 for (it_free_data = 0; it_free_data < it_file; it_free_data++) {
283 free(data[it_free_data]);
287 free(filenameComponentPGX);
292 /* Set the image_read parameters*/
293 param_image_read[it_file].x0 = 0;
294 param_image_read[it_file].y0 = 0;
295 param_image_read[it_file].dx = 0;
296 param_image_read[it_file].dy = 0;
297 param_image_read[it_file].h = image_read->comps->h;
298 param_image_read[it_file].w = image_read->comps->w;
299 param_image_read[it_file].bpp = image_read->comps->bpp;
300 param_image_read[it_file].prec = image_read->comps->prec;
301 param_image_read[it_file].sgnd = image_read->comps->sgnd;
304 data[it_file] = malloc(param_image_read[it_file].h * param_image_read[it_file].w * sizeof(int));
305 memcpy(data[it_file], image_read->comps->data, image_read->comps->h * image_read->comps->w * sizeof(int));
308 opj_image_destroy(image_read);
309 free(filenameComponentPGX);
312 image = opj_image_create((OPJ_UINT32)nbFilenamePGX, param_image_read, OPJ_CLRSPC_UNSPECIFIED);
313 for (it_file = 0; it_file < nbFilenamePGX; it_file++)
315 /* Copy data into output image and free memory*/
316 memcpy(image->comps[it_file].data, data[it_file], image->comps[it_file].h * image->comps[it_file].w * sizeof(int));
321 free(param_image_read);
327 static opj_image_t* readImageFromFilePGX(const char* filename, int nbFilenamePGX, const char *separator)
330 opj_image_t* image_read = NULL;
331 opj_image_t* image = NULL;
332 opj_cparameters_t parameters;
333 opj_image_cmptparm_t* param_image_read;
336 /* If separator is empty => nb file to read is equal to one*/
337 if ( strlen(separator) == 0 )
340 /* set encoding parameters to default values */
341 opj_set_default_encoder_parameters(¶meters);
342 parameters.decod_format = PGX_DFMT;
343 strcpy(parameters.infile, filename);
346 param_image_read = malloc((size_t)nbFilenamePGX * sizeof(opj_image_cmptparm_t));
347 data = malloc((size_t)nbFilenamePGX * sizeof(*data));
349 for (it_file = 0; it_file < nbFilenamePGX; it_file++)
351 /* Create the right filename*/
352 char *filenameComponentPGX;
353 if (strlen(separator) == 0)
355 filenameComponentPGX = malloc((strlen(filename) + 1) * sizeof(*filenameComponentPGX));
356 strcpy(filenameComponentPGX, filename);
359 filenameComponentPGX = createMultiComponentsFilename(filename, it_file, separator);
361 /* Read the pgx file corresponding to the component */
362 image_read = pgxtoimage(filenameComponentPGX, ¶meters);
366 fprintf(stderr, "Unable to load pgx file\n");
368 free(param_image_read);
370 for (it_free_data = 0; it_free_data < it_file; it_free_data++) {
371 free(data[it_free_data]);
375 free(filenameComponentPGX);
380 /* Set the image_read parameters*/
381 param_image_read[it_file].x0 = 0;
382 param_image_read[it_file].y0 = 0;
383 param_image_read[it_file].dx = 0;
384 param_image_read[it_file].dy = 0;
385 param_image_read[it_file].h = image_read->comps->h;
386 param_image_read[it_file].w = image_read->comps->w;
387 param_image_read[it_file].bpp = image_read->comps->bpp;
388 param_image_read[it_file].prec = image_read->comps->prec;
389 param_image_read[it_file].sgnd = image_read->comps->sgnd;
392 data[it_file] = malloc(param_image_read[it_file].h * param_image_read[it_file].w * sizeof(int));
393 memcpy(data[it_file], image_read->comps->data, image_read->comps->h * image_read->comps->w * sizeof(int));
396 opj_image_destroy(image_read);
397 free(filenameComponentPGX);
400 image = opj_image_create((OPJ_UINT32)nbFilenamePGX, param_image_read, OPJ_CLRSPC_UNSPECIFIED);
401 for (it_file = 0; it_file < nbFilenamePGX; it_file++)
403 /* Copy data into output image and free memory*/
404 memcpy(image->comps[it_file].data, data[it_file], image->comps[it_file].h * image->comps[it_file].w * sizeof(int));
409 free(param_image_read);
415 #if defined(OPJ_HAVE_LIBPNG) && 0 /* remove for now */
416 /*******************************************************************************
418 *******************************************************************************/
419 static int imageToPNG(const opj_image_t* image, const char* filename, int num_comp_select)
421 opj_image_cmptparm_t param_image_write;
422 opj_image_t* image_write = NULL;
424 param_image_write.x0 = 0;
425 param_image_write.y0 = 0;
426 param_image_write.dx = 0;
427 param_image_write.dy = 0;
428 param_image_write.h = image->comps[num_comp_select].h;
429 param_image_write.w = image->comps[num_comp_select].w;
430 param_image_write.bpp = image->comps[num_comp_select].bpp;
431 param_image_write.prec = image->comps[num_comp_select].prec;
432 param_image_write.sgnd = image->comps[num_comp_select].sgnd;
434 image_write = opj_image_create(1u, ¶m_image_write, OPJ_CLRSPC_GRAY);
435 memcpy(image_write->comps->data, image->comps[num_comp_select].data, param_image_write.h * param_image_write.w * sizeof(int));
437 imagetopng(image_write, filename);
439 opj_image_destroy(image_write);
445 typedef struct test_cmp_parameters
451 /** Number of components */
454 double* tabMSEvalues;
456 double* tabPEAKvalues;
460 char separator_base[2];
462 char separator_test[2];
464 } test_cmp_parameters;
466 /* return decode format PGX / TIF , return -1 on error */
467 static int get_decod_format(test_cmp_parameters* param)
470 char * base_ext = strrchr(param->base_filename, dot);
471 char * test_ext = strrchr(param->test_filename, dot);
472 if( !base_ext || !test_ext ) return -1;
473 if( strcmp(base_ext,test_ext) != 0 ) return -1;
474 if( strcmp(base_ext,".pgx") == 0 ) return PGX_DFMT;
475 if( strcmp(base_ext,".tif") == 0 ) return TIF_DFMT;
476 if( strcmp(base_ext,".ppm") == 0 ) return PXM_DFMT;
480 /*******************************************************************************
482 *******************************************************************************/
483 static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
485 char *MSElistvalues = NULL; char *PEAKlistvalues= NULL;
486 char *separatorList = NULL;
487 int sizemembasefile, sizememtestfile;
488 int index, flagM=0, flagP=0;
489 const char optlist[] = "b:t:n:m:p:s:d";
493 param->base_filename = NULL;
494 param->test_filename = NULL;
496 param->tabMSEvalues = NULL;
497 param->tabPEAKvalues = NULL;
499 param->separator_base[0] = 0;
500 param->separator_test[0] = 0;
504 while ((c = opj_getopt(argc, argv, optlist)) != -1)
508 sizemembasefile = (int)strlen(opj_optarg)+1;
509 param->base_filename = (char*) malloc((size_t)sizemembasefile);
510 param->base_filename[0] = '\0';
511 strncpy(param->base_filename, opj_optarg, strlen(opj_optarg));
512 param->base_filename[strlen(opj_optarg)] = '\0';
513 /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
516 sizememtestfile = (int) strlen(opj_optarg) + 1;
517 param->test_filename = (char*) malloc((size_t)sizememtestfile);
518 param->test_filename[0] = '\0';
519 strncpy(param->test_filename, opj_optarg, strlen(opj_optarg));
520 param->test_filename[strlen(opj_optarg)] = '\0';
521 /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
524 param->nbcomp = atoi(opj_optarg);
527 MSElistvalues = opj_optarg;
531 PEAKlistvalues = opj_optarg;
538 separatorList = opj_optarg;
541 if ((opj_optopt == 'b') || (opj_optopt == 't') || (opj_optopt == 'n') || (opj_optopt == 'p') || (opj_optopt == 'm') || (opj_optopt
543 fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
545 if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
546 else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
549 fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
553 if (opj_optind != argc)
555 for (index = opj_optind; index < argc; index++)
556 fprintf(stderr,"Non-option argument %s\n", argv[index]);
560 if (param->nbcomp == 0)
562 fprintf(stderr,"Need to indicate the number of components !\n");
566 if ( flagM && flagP )
568 param->tabMSEvalues = parseToleranceValues( MSElistvalues, param->nbcomp);
569 param->tabPEAKvalues = parseToleranceValues( PEAKlistvalues, param->nbcomp);
570 if ( (param->tabMSEvalues == NULL) || (param->tabPEAKvalues == NULL))
572 fprintf(stderr,"MSE and PEAK values are not correct (respectively need %d values)\n",param->nbcomp);
577 /* Get separators after corresponding letter (b or t)*/
578 if (separatorList != NULL)
580 if( (strlen(separatorList) ==2) || (strlen(separatorList) ==4) )
582 /* keep original string*/
583 int sizeseplist = (int)strlen(separatorList)+1;
584 char* separatorList2 = (char*)malloc( (size_t)sizeseplist );
585 separatorList2[0] = '\0';
586 strncpy(separatorList2, separatorList, strlen(separatorList));
587 separatorList2[strlen(separatorList)] = '\0';
588 /*printf("separatorList2 = %s [%d / %d]\n", separatorList2, strlen(separatorList2), sizeseplist);*/
590 if (strlen(separatorList) == 2) /* one separator behind b or t*/
592 char *resultT = NULL;
593 resultT = strtok(separatorList2, "t");
594 if (strlen(resultT) == strlen(separatorList)) /* didn't find t character, try to find b*/
596 char *resultB = NULL;
597 resultB = strtok(resultT, "b");
598 if (strlen(resultB) == 1)
600 param->separator_base[0] = separatorList[1];
601 param->separator_base[1] = 0;
602 param->separator_test[0] = 0;
604 else /* not found b*/
606 free(separatorList2);
612 param->separator_base[0] = 0;
613 param->separator_test[0] = separatorList[1];
614 param->separator_test[1] = 0;
616 /*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) );*/
618 else /* == 4 characters we must found t and b*/
620 char *resultT = NULL;
621 resultT = strtok(separatorList2, "t");
622 if (strlen(resultT) == 3) /* found t in first place*/
624 char *resultB = NULL;
625 resultB = strtok(resultT, "b");
626 if (strlen(resultB) == 1) /* found b after t*/
628 param->separator_test[0] = separatorList[1];
629 param->separator_test[1] = 0;
630 param->separator_base[0] = separatorList[3];
631 param->separator_base[1] = 0;
633 else /* didn't find b after t*/
635 free(separatorList2);
639 else /* == 2, didn't find t in first place*/
641 char *resultB = NULL;
642 resultB = strtok(resultT, "b");
643 if (strlen(resultB) == 1) /* found b in first place*/
645 param->separator_base[0] = separatorList[1];
646 param->separator_base[1] = 0;
647 param->separator_test[0] = separatorList[3];
648 param->separator_test[1] = 0;
650 else /* didn't found b in first place => problem*/
652 free(separatorList2);
657 free(separatorList2);
659 else /* wrong number of argument after -s*/
666 if (param->nbcomp == 1)
668 assert( param->separator_base[0] == 0 );
669 assert( param->separator_test[0] == 0 );
673 fprintf(stderr,"If number of component is > 1, we need separator\n");
679 if ( (param->nr_flag) && (flagP || flagM) )
681 fprintf(stderr,"Wrong input parameters list: it is non-regression test or tolerance comparison\n");
684 if ( (!param->nr_flag) && (!flagP || !flagM) )
686 fprintf(stderr,"Wrong input parameters list: it is non-regression test or tolerance comparison\n");
693 /*******************************************************************************
695 *******************************************************************************/
696 int main(int argc, char **argv)
698 test_cmp_parameters inParam;
699 OPJ_UINT32 it_comp, itpxl;
701 int nbFilenamePGXbase = 0, nbFilenamePGXtest = 0;
702 char *filenamePNGtest= NULL, *filenamePNGbase = NULL, *filenamePNGdiff = NULL;
703 int memsizebasefilename, memsizetestfilename;
704 size_t memsizedifffilename;
705 int valueDiff = 0, nbPixelDiff = 0;
706 double sumDiff = 0.0;
707 /* Structures to store image parameters and data*/
708 opj_image_t *imageBase = NULL, *imageTest = NULL, *imageDiff = NULL;
709 opj_image_cmptparm_t* param_image_diff = NULL;
712 /* Get parameters from command line*/
713 if( parse_cmdline_cmp(argc, argv, &inParam) )
715 compare_images_help_display();
719 /* Display Parameters*/
720 printf("******Parameters********* \n");
721 printf(" base_filename = %s\n"
722 " test_filename = %s\n"
723 " nb of Components = %d\n"
724 " Non regression test = %d\n"
725 " separator Base = %s\n"
726 " separator Test = %s\n",
727 inParam.base_filename, inParam.test_filename, inParam.nbcomp,
728 inParam.nr_flag, inParam.separator_base, inParam.separator_test);
730 if ( (inParam.tabMSEvalues != NULL) && (inParam.tabPEAKvalues != NULL))
733 printf(" MSE values = [");
734 for (it_comp = 0; it_comp < inParam.nbcomp; it_comp++)
735 printf(" %f ", inParam.tabMSEvalues[it_comp]);
737 printf(" PEAK values = [");
738 for (it_comp = 0; it_comp < inParam.nbcomp; it_comp++)
739 printf(" %f ", inParam.tabPEAKvalues[it_comp]);
741 printf(" Non-regression test = %d\n", inParam.nr_flag);
744 if (strlen(inParam.separator_base) != 0)
745 nbFilenamePGXbase = inParam.nbcomp;
747 if (strlen(inParam.separator_test) != 0)
748 nbFilenamePGXtest = inParam.nbcomp;
750 printf(" NbFilename to generate from base filename = %d\n", nbFilenamePGXbase);
751 printf(" NbFilename to generate from test filename = %d\n", nbFilenamePGXtest);
752 printf("************************* \n");
754 /*----------BASELINE IMAGE--------*/
755 memsizebasefilename = (int)strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
756 memsizetestfilename = (int)strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
758 decod_format = get_decod_format(&inParam);
759 if( decod_format == -1 )
761 fprintf( stderr, "Unhandled file format\n" );
764 assert( decod_format == PGX_DFMT || decod_format == TIF_DFMT || decod_format == PXM_DFMT );
766 if( decod_format == PGX_DFMT )
768 imageBase = readImageFromFilePGX( inParam.base_filename, nbFilenamePGXbase, inParam.separator_base);
769 if ( imageBase == NULL )
772 else if( decod_format == TIF_DFMT )
774 imageBase = readImageFromFileTIF( inParam.base_filename, nbFilenamePGXbase, "");
775 if ( imageBase == NULL )
778 else if( decod_format == PXM_DFMT )
780 imageBase = readImageFromFilePPM( inParam.base_filename, nbFilenamePGXbase, "");
781 if ( imageBase == NULL )
785 filenamePNGbase = (char*) malloc((size_t)memsizebasefilename);
786 strcpy(filenamePNGbase, inParam.test_filename);
787 strcat(filenamePNGbase, ".base");
788 /*printf("filenamePNGbase = %s [%d / %d octets]\n",filenamePNGbase, strlen(filenamePNGbase),memsizebasefilename );*/
790 /*----------TEST IMAGE--------*/
792 if( decod_format == PGX_DFMT )
794 imageTest = readImageFromFilePGX(inParam.test_filename, nbFilenamePGXtest, inParam.separator_test);
795 if ( imageTest == NULL )
798 else if( decod_format == TIF_DFMT )
800 imageTest = readImageFromFileTIF(inParam.test_filename, nbFilenamePGXtest, "");
801 if ( imageTest == NULL )
804 else if( decod_format == PXM_DFMT )
806 imageTest = readImageFromFilePPM(inParam.test_filename, nbFilenamePGXtest, "");
807 if ( imageTest == NULL )
811 filenamePNGtest = (char*) malloc((size_t)memsizetestfilename);
812 strcpy(filenamePNGtest, inParam.test_filename);
813 strcat(filenamePNGtest, ".test");
814 /*printf("filenamePNGtest = %s [%d / %d octets]\n",filenamePNGtest, strlen(filenamePNGtest),memsizetestfilename );*/
816 /*----------DIFF IMAGE--------*/
819 param_image_diff = malloc( imageBase->numcomps * sizeof(opj_image_cmptparm_t));
821 /* Comparison of header parameters*/
822 printf("Step 1 -> Header comparison\n");
824 for (it_comp = 0; it_comp < imageBase->numcomps; it_comp++)
826 param_image_diff[it_comp].x0 = 0;
827 param_image_diff[it_comp].y0 = 0;
828 param_image_diff[it_comp].dx = 0;
829 param_image_diff[it_comp].dy = 0;
830 param_image_diff[it_comp].sgnd = 0;
831 param_image_diff[it_comp].prec = 8;
832 param_image_diff[it_comp].bpp = 1;
833 param_image_diff[it_comp].h = imageBase->comps[it_comp].h;
834 param_image_diff[it_comp].w = imageBase->comps[it_comp].w;
836 if (imageBase->comps[it_comp].sgnd != imageTest->comps[it_comp].sgnd)
838 printf("ERROR: sign mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).sgnd, ((imageTest->comps)[it_comp]).sgnd);
842 if (((imageBase->comps)[it_comp]).prec != ((imageTest->comps)[it_comp]).prec)
844 printf("ERROR: prec mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).prec, ((imageTest->comps)[it_comp]).prec);
848 if (((imageBase->comps)[it_comp]).bpp != ((imageTest->comps)[it_comp]).bpp)
850 printf("ERROR: byte per pixel mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).bpp, ((imageTest->comps)[it_comp]).bpp);
854 if (((imageBase->comps)[it_comp]).h != ((imageTest->comps)[it_comp]).h)
856 printf("ERROR: height mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).h, ((imageTest->comps)[it_comp]).h);
860 if (((imageBase->comps)[it_comp]).w != ((imageTest->comps)[it_comp]).w)
862 printf("ERROR: width mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).w, ((imageTest->comps)[it_comp]).w);
867 imageDiff = opj_image_create(imageBase->numcomps, param_image_diff, OPJ_CLRSPC_UNSPECIFIED);
869 free(param_image_diff); param_image_diff = NULL;
871 /* Measurement computation*/
872 printf("Step 2 -> measurement comparison\n");
874 memsizedifffilename = strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
875 filenamePNGdiff = (char*) malloc(memsizedifffilename);
876 strcpy(filenamePNGdiff, inParam.test_filename);
877 strcat(filenamePNGdiff, ".diff");
878 /*printf("filenamePNGdiff = %s [%d / %d octets]\n",filenamePNGdiff, strlen(filenamePNGdiff),memsizedifffilename );*/
880 /* Compute pixel diff*/
881 for (it_comp = 0; it_comp < imageDiff->numcomps; it_comp++)
885 for (itpxl = 0; itpxl < ((imageDiff->comps)[it_comp]).w * ((imageDiff->comps)[it_comp]).h; itpxl++)
887 if (abs( ((imageBase->comps)[it_comp]).data[itpxl] - ((imageTest->comps)[it_comp]).data[itpxl] ) > 0)
889 valueDiff = ((imageBase->comps)[it_comp]).data[itpxl] - ((imageTest->comps)[it_comp]).data[itpxl];
890 ((imageDiff->comps)[it_comp]).data[itpxl] = abs(valueDiff);
891 sumDiff += valueDiff;
894 SE += (double)valueDiff * valueDiff;
895 PEAK = (PEAK > abs(valueDiff)) ? PEAK : abs(valueDiff);
898 ((imageDiff->comps)[it_comp]).data[itpxl] = 0;
901 MSE = SE / ( ((imageDiff->comps)[it_comp]).w * ((imageDiff->comps)[it_comp]).h );
903 if (!inParam.nr_flag && (inParam.tabMSEvalues != NULL) && (inParam.tabPEAKvalues != NULL))
904 { /* Conformance test*/
905 printf("<DartMeasurement name=\"PEAK_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, PEAK);
906 printf("<DartMeasurement name=\"MSE_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, MSE);
908 if ( (MSE > inParam.tabMSEvalues[it_comp]) || (PEAK > inParam.tabPEAKvalues[it_comp]) )
910 printf("ERROR: MSE (%f) or PEAK (%f) values produced by the decoded file are greater "
911 "than the allowable error (respectively %f and %f) \n",
912 MSE, PEAK, inParam.tabMSEvalues[it_comp], inParam.tabPEAKvalues[it_comp]);
916 else /* Non regression-test */
918 if ( nbPixelDiff > 0)
923 printf("<DartMeasurement name=\"NumberOfPixelsWithDifferences_%d\" type=\"numeric/int\"> %d </DartMeasurement> \n", it_comp, nbPixelDiff);
924 printf("<DartMeasurement name=\"ComponentError_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, sumDiff);
926 #ifdef OPJ_HAVE_LIBPNG
928 char *filenamePNGbase_it_comp, *filenamePNGtest_it_comp, *filenamePNGdiff_it_comp;
930 filenamePNGbase_it_comp = (char*) malloc((size_t)memsizebasefilename);
931 strcpy(filenamePNGbase_it_comp,filenamePNGbase);
933 filenamePNGtest_it_comp = (char*) malloc((size_t)memsizetestfilename);
934 strcpy(filenamePNGtest_it_comp,filenamePNGtest);
936 filenamePNGdiff_it_comp = (char*) malloc(memsizedifffilename);
937 strcpy(filenamePNGdiff_it_comp,filenamePNGdiff);
939 sprintf(it_compc, "_%i", it_comp);
940 strcat(it_compc,".png");
941 strcat(filenamePNGbase_it_comp, it_compc);
942 /*printf("filenamePNGbase_it = %s [%d / %d octets]\n",filenamePNGbase_it_comp, strlen(filenamePNGbase_it_comp),memsizebasefilename );*/
943 strcat(filenamePNGtest_it_comp, it_compc);
944 /*printf("filenamePNGtest_it = %s [%d / %d octets]\n",filenamePNGtest_it_comp, strlen(filenamePNGtest_it_comp),memsizetestfilename );*/
945 strcat(filenamePNGdiff_it_comp, it_compc);
946 /*printf("filenamePNGdiff_it = %s [%d / %d octets]\n",filenamePNGdiff_it_comp, strlen(filenamePNGdiff_it_comp),memsizedifffilename );*/
949 if ( imageToPNG(imageBase, filenamePNGbase_it_comp, it_comp) == EXIT_SUCCESS )
951 printf("<DartMeasurementFile name=\"BaselineImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGbase_it_comp);
954 if ( imageToPNG(imageTest, filenamePNGtest_it_comp, it_comp) == EXIT_SUCCESS )
956 printf("<DartMeasurementFile name=\"TestImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGtest_it_comp);
959 if ( imageToPNG(imageDiff, filenamePNGdiff_it_comp, it_comp) == EXIT_SUCCESS )
961 printf("<DartMeasurementFile name=\"DiffferenceImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGdiff_it_comp);
965 free(filenamePNGbase_it_comp);
966 free(filenamePNGtest_it_comp);
967 free(filenamePNGdiff_it_comp);
975 printf("---- TEST SUCCEED ----\n");
978 /*-----------------------------*/
979 free(param_image_diff);
981 opj_image_destroy(imageBase);
982 opj_image_destroy(imageTest);
983 opj_image_destroy(imageDiff);
985 free(filenamePNGbase);
986 free(filenamePNGtest);
987 free(filenamePNGdiff);
989 free(inParam.tabMSEvalues);
990 free(inParam.tabPEAKvalues);
991 free(inParam.base_filename);
992 free(inParam.test_filename);
994 return failed ? EXIT_FAILURE : EXIT_SUCCESS;