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/TIF/PNM image \n");
87 fprintf(stdout," -t \t REQUIRED \t filename to the test PGX/TIF/PNM image\n");
88 fprintf(stdout," -n \t REQUIRED \t number of component of the image (used to generate correct filename)\n");
89 fprintf(stdout," -m \t OPTIONAL \t list of MSE tolerances, separated by : (size must correspond to the number of component) of \n");
90 fprintf(stdout," -p \t OPTIONAL \t list of PEAK tolerances, separated by : (size must correspond to the number of component) \n");
91 fprintf(stdout," -s \t OPTIONAL \t 1 or 2 filename separator to take into account PGX/PNM image with different components, "
92 "please indicate b or t before separator to indicate respectively the separator "
93 "for ref/base file and for test file. \n");
94 fprintf(stdout," -d \t OPTIONAL \t indicate if you want to run this function as conformance test or as non regression test\n");
98 static int get_decod_format_from_string(const char *filename)
101 char * ext = strrchr(filename, dot);
102 if( strcmp(ext,".pgx") == 0 ) return PGX_DFMT;
103 if( strcmp(ext,".tif") == 0 ) return TIF_DFMT;
104 if( strcmp(ext,".ppm") == 0 ) return PXM_DFMT;
109 /*******************************************************************************
110 * Create filenames from a filename using separator and nb components
112 *******************************************************************************/
113 static char* createMultiComponentsFilename(const char* inFilename, const int indexF, const char* separator)
116 char *outFilename, *ptr;
117 const char token = '.';
121 /*printf("inFilename = %s\n", inFilename);*/
122 if ((ptr = strrchr(inFilename, token)) != NULL)
124 posToken = strlen(inFilename) - strlen(ptr);
125 /*printf("Position of %c character inside inFilename = %d\n", token, posToken);*/
129 /*printf("Token %c not found\n", token);*/
130 outFilename = (char*)malloc(1);
131 outFilename[0] = '\0';
135 outFilename = (char*)malloc((posToken + 7) * sizeof(char)); /*6*/
137 strncpy(outFilename, inFilename, posToken);
138 outFilename[posToken] = '\0';
139 strcat(outFilename, separator);
140 sprintf(s, "%i", indexF);
141 strcat(outFilename, s);
143 decod_format = get_decod_format_from_string(inFilename);
144 if( decod_format == PGX_DFMT )
146 strcat(outFilename, ".pgx");
148 else if( decod_format == PXM_DFMT )
150 strcat(outFilename, ".pgm");
153 /*printf("outfilename: %s\n", outFilename);*/
157 /*******************************************************************************
159 *******************************************************************************/
160 static opj_image_t* readImageFromFilePPM(const char* filename, int nbFilenamePGX, const char *separator)
163 opj_image_t* image_read = NULL;
164 opj_image_t* image = NULL;
165 opj_cparameters_t parameters;
166 opj_image_cmptparm_t* param_image_read;
169 /* If separator is empty => nb file to read is equal to one*/
170 if ( strlen(separator) == 0 )
173 /* set encoding parameters to default values */
174 opj_set_default_encoder_parameters(¶meters);
175 parameters.decod_format = PXM_DFMT;
176 strcpy(parameters.infile, filename);
179 param_image_read = malloc((size_t)nbFilenamePGX * sizeof(opj_image_cmptparm_t));
180 data = malloc((size_t)nbFilenamePGX * sizeof(*data));
182 for (it_file = 0; it_file < nbFilenamePGX; it_file++)
184 /* Create the right filename*/
185 char *filenameComponentPGX;
186 if (strlen(separator) == 0)
188 filenameComponentPGX = malloc((strlen(filename) + 1) * sizeof(*filenameComponentPGX));
189 strcpy(filenameComponentPGX, filename);
192 filenameComponentPGX = createMultiComponentsFilename(filename, it_file, separator);
194 /* Read the tif file corresponding to the component */
195 image_read = pnmtoimage(filenameComponentPGX, ¶meters);
199 fprintf(stderr, "Unable to load ppm file: %s\n", filenameComponentPGX);
201 free(param_image_read);
203 for (it_free_data = 0; it_free_data < it_file; it_free_data++) {
204 free(data[it_free_data]);
208 free(filenameComponentPGX);
213 /* Set the image_read parameters*/
214 param_image_read[it_file].x0 = 0;
215 param_image_read[it_file].y0 = 0;
216 param_image_read[it_file].dx = 0;
217 param_image_read[it_file].dy = 0;
218 param_image_read[it_file].h = image_read->comps->h;
219 param_image_read[it_file].w = image_read->comps->w;
220 param_image_read[it_file].bpp = image_read->comps->bpp;
221 param_image_read[it_file].prec = image_read->comps->prec;
222 param_image_read[it_file].sgnd = image_read->comps->sgnd;
225 data[it_file] = malloc(param_image_read[it_file].h * param_image_read[it_file].w * sizeof(int));
226 memcpy(data[it_file], image_read->comps->data, image_read->comps->h * image_read->comps->w * sizeof(int));
229 opj_image_destroy(image_read);
230 free(filenameComponentPGX);
233 image = opj_image_create((OPJ_UINT32)nbFilenamePGX, param_image_read, OPJ_CLRSPC_UNSPECIFIED);
234 for (it_file = 0; it_file < nbFilenamePGX; it_file++)
236 /* Copy data into output image and free memory*/
237 memcpy(image->comps[it_file].data, data[it_file], image->comps[it_file].h * image->comps[it_file].w * sizeof(int));
242 free(param_image_read);
248 static opj_image_t* readImageFromFileTIF(const char* filename, int nbFilenamePGX, const char *separator)
250 opj_image_t* image_read = NULL;
251 opj_cparameters_t parameters;
255 /* conformance test suite produce annoying warning/error:
256 * TIFFReadDirectory: Warning, /.../data/baseline/conformance/jp2_1.tif: unknown field with tag 37724 (0x935c) encountered.
257 * TIFFOpen: /.../data/baseline/nonregression/opj_jp2_1.tif: Cannot open.
258 * On Win32 this open a message box by default, so remove it from the test suite:
260 #ifdef OPJ_HAVE_LIBTIFF
261 TIFFSetWarningHandler(NULL);
262 TIFFSetErrorHandler(NULL);
265 if ( strlen(separator) != 0 ) return NULL;
267 /* set encoding parameters to default values */
268 opj_set_default_encoder_parameters(¶meters);
269 parameters.decod_format = TIF_DFMT;
270 strcpy(parameters.infile, filename);
272 /* Read the tif file corresponding to the component */
273 #ifdef OPJ_HAVE_LIBTIFF
274 image_read = tiftoimage(filename, ¶meters);
278 fprintf(stderr, "Unable to load TIF file\n");
282 /* \postconditions */
283 assert( image_read->numcomps == 1 || image_read->numcomps == 3 );
287 static opj_image_t* readImageFromFilePGX(const char* filename, int nbFilenamePGX, const char *separator)
290 opj_image_t* image_read = NULL;
291 opj_image_t* image = NULL;
292 opj_cparameters_t parameters;
293 opj_image_cmptparm_t* param_image_read;
296 /* If separator is empty => nb file to read is equal to one*/
297 if ( strlen(separator) == 0 )
300 /* set encoding parameters to default values */
301 opj_set_default_encoder_parameters(¶meters);
302 parameters.decod_format = PGX_DFMT;
303 strcpy(parameters.infile, filename);
306 param_image_read = malloc((size_t)nbFilenamePGX * sizeof(opj_image_cmptparm_t));
307 data = malloc((size_t)nbFilenamePGX * sizeof(*data));
309 for (it_file = 0; it_file < nbFilenamePGX; it_file++)
311 /* Create the right filename*/
312 char *filenameComponentPGX;
313 if (strlen(separator) == 0)
315 filenameComponentPGX = malloc((strlen(filename) + 1) * sizeof(*filenameComponentPGX));
316 strcpy(filenameComponentPGX, filename);
319 filenameComponentPGX = createMultiComponentsFilename(filename, it_file, separator);
321 /* Read the pgx file corresponding to the component */
322 image_read = pgxtoimage(filenameComponentPGX, ¶meters);
326 fprintf(stderr, "Unable to load pgx file\n");
328 free(param_image_read);
330 for (it_free_data = 0; it_free_data < it_file; it_free_data++) {
331 free(data[it_free_data]);
335 free(filenameComponentPGX);
340 /* Set the image_read parameters*/
341 param_image_read[it_file].x0 = 0;
342 param_image_read[it_file].y0 = 0;
343 param_image_read[it_file].dx = 0;
344 param_image_read[it_file].dy = 0;
345 param_image_read[it_file].h = image_read->comps->h;
346 param_image_read[it_file].w = image_read->comps->w;
347 param_image_read[it_file].bpp = image_read->comps->bpp;
348 param_image_read[it_file].prec = image_read->comps->prec;
349 param_image_read[it_file].sgnd = image_read->comps->sgnd;
352 data[it_file] = malloc(param_image_read[it_file].h * param_image_read[it_file].w * sizeof(int));
353 memcpy(data[it_file], image_read->comps->data, image_read->comps->h * image_read->comps->w * sizeof(int));
356 opj_image_destroy(image_read);
357 free(filenameComponentPGX);
360 image = opj_image_create((OPJ_UINT32)nbFilenamePGX, param_image_read, OPJ_CLRSPC_UNSPECIFIED);
361 for (it_file = 0; it_file < nbFilenamePGX; it_file++)
363 /* Copy data into output image and free memory*/
364 memcpy(image->comps[it_file].data, data[it_file], image->comps[it_file].h * image->comps[it_file].w * sizeof(int));
369 free(param_image_read);
375 #if defined(OPJ_HAVE_LIBPNG) && 0 /* remove for now */
376 /*******************************************************************************
378 *******************************************************************************/
379 static int imageToPNG(const opj_image_t* image, const char* filename, int num_comp_select)
381 opj_image_cmptparm_t param_image_write;
382 opj_image_t* image_write = NULL;
384 param_image_write.x0 = 0;
385 param_image_write.y0 = 0;
386 param_image_write.dx = 0;
387 param_image_write.dy = 0;
388 param_image_write.h = image->comps[num_comp_select].h;
389 param_image_write.w = image->comps[num_comp_select].w;
390 param_image_write.bpp = image->comps[num_comp_select].bpp;
391 param_image_write.prec = image->comps[num_comp_select].prec;
392 param_image_write.sgnd = image->comps[num_comp_select].sgnd;
394 image_write = opj_image_create(1u, ¶m_image_write, OPJ_CLRSPC_GRAY);
395 memcpy(image_write->comps->data, image->comps[num_comp_select].data, param_image_write.h * param_image_write.w * sizeof(int));
397 imagetopng(image_write, filename);
399 opj_image_destroy(image_write);
405 typedef struct test_cmp_parameters
411 /** Number of components */
414 double* tabMSEvalues;
416 double* tabPEAKvalues;
420 char separator_base[2];
422 char separator_test[2];
424 } test_cmp_parameters;
426 /* return decode format PGX / TIF / PPM , return -1 on error */
427 static int get_decod_format(test_cmp_parameters* param)
429 int base_format = get_decod_format_from_string( param->base_filename );
430 int test_format = get_decod_format_from_string( param->test_filename );
431 if( base_format != test_format ) return -1;
432 /* handle case -1: */
436 /*******************************************************************************
438 *******************************************************************************/
439 static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
441 char *MSElistvalues = NULL; char *PEAKlistvalues= NULL;
442 char *separatorList = NULL;
443 size_t sizemembasefile, sizememtestfile;
444 int index, flagM=0, flagP=0;
445 const char optlist[] = "b:t:n:m:p:s:d";
449 param->base_filename = NULL;
450 param->test_filename = NULL;
452 param->tabMSEvalues = NULL;
453 param->tabPEAKvalues = NULL;
455 param->separator_base[0] = 0;
456 param->separator_test[0] = 0;
460 while ((c = opj_getopt(argc, argv, optlist)) != -1)
464 sizemembasefile = strlen(opj_optarg) + 1;
465 param->base_filename = (char*) malloc(sizemembasefile);
466 strcpy(param->base_filename, opj_optarg);
467 /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
470 sizememtestfile = strlen(opj_optarg) + 1;
471 param->test_filename = (char*) malloc(sizememtestfile);
472 strcpy(param->test_filename, opj_optarg);
473 /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
476 param->nbcomp = atoi(opj_optarg);
479 MSElistvalues = opj_optarg;
483 PEAKlistvalues = opj_optarg;
490 separatorList = opj_optarg;
493 if ((opj_optopt == 'b') || (opj_optopt == 't') || (opj_optopt == 'n') || (opj_optopt == 'p') || (opj_optopt == 'm') || (opj_optopt
495 fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
497 if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
498 else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
501 fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
505 if (opj_optind != argc)
507 for (index = opj_optind; index < argc; index++)
508 fprintf(stderr,"Non-option argument %s\n", argv[index]);
512 if (param->nbcomp == 0)
514 fprintf(stderr,"Need to indicate the number of components !\n");
518 if ( flagM && flagP )
520 param->tabMSEvalues = parseToleranceValues( MSElistvalues, param->nbcomp);
521 param->tabPEAKvalues = parseToleranceValues( PEAKlistvalues, param->nbcomp);
522 if ( (param->tabMSEvalues == NULL) || (param->tabPEAKvalues == NULL))
524 fprintf(stderr,"MSE and PEAK values are not correct (respectively need %d values)\n",param->nbcomp);
529 /* Get separators after corresponding letter (b or t)*/
530 if (separatorList != NULL)
532 if( (strlen(separatorList) ==2) || (strlen(separatorList) ==4) )
534 /* keep original string*/
535 size_t sizeseplist = strlen(separatorList)+1;
536 char* separatorList2 = (char*)malloc( sizeseplist );
537 strcpy(separatorList2, separatorList);
538 /*printf("separatorList2 = %s [%d / %d]\n", separatorList2, strlen(separatorList2), sizeseplist);*/
540 if (strlen(separatorList) == 2) /* one separator behind b or t*/
542 char *resultT = NULL;
543 resultT = strtok(separatorList2, "t");
544 if (strlen(resultT) == strlen(separatorList)) /* didn't find t character, try to find b*/
546 char *resultB = NULL;
547 resultB = strtok(resultT, "b");
548 if (strlen(resultB) == 1)
550 param->separator_base[0] = separatorList[1];
551 param->separator_base[1] = 0;
552 param->separator_test[0] = 0;
554 else /* not found b*/
556 free(separatorList2);
562 param->separator_base[0] = 0;
563 param->separator_test[0] = separatorList[1];
564 param->separator_test[1] = 0;
566 /*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) );*/
568 else /* == 4 characters we must found t and b*/
570 char *resultT = NULL;
571 resultT = strtok(separatorList2, "t");
572 if (strlen(resultT) == 3) /* found t in first place*/
574 char *resultB = NULL;
575 resultB = strtok(resultT, "b");
576 if (strlen(resultB) == 1) /* found b after t*/
578 param->separator_test[0] = separatorList[1];
579 param->separator_test[1] = 0;
580 param->separator_base[0] = separatorList[3];
581 param->separator_base[1] = 0;
583 else /* didn't find b after t*/
585 free(separatorList2);
589 else /* == 2, didn't find t in first place*/
591 char *resultB = NULL;
592 resultB = strtok(resultT, "b");
593 if (strlen(resultB) == 1) /* found b in first place*/
595 param->separator_base[0] = separatorList[1];
596 param->separator_base[1] = 0;
597 param->separator_test[0] = separatorList[3];
598 param->separator_test[1] = 0;
600 else /* didn't found b in first place => problem*/
602 free(separatorList2);
607 free(separatorList2);
609 else /* wrong number of argument after -s*/
616 if (param->nbcomp == 1)
618 assert( param->separator_base[0] == 0 );
619 assert( param->separator_test[0] == 0 );
623 fprintf(stderr,"If number of component is > 1, we need separator\n");
629 if ( (param->nr_flag) && (flagP || flagM) )
631 fprintf(stderr,"Wrong input parameters list: it is non-regression test or tolerance comparison\n");
634 if ( (!param->nr_flag) && (!flagP || !flagM) )
636 fprintf(stderr,"Wrong input parameters list: it is non-regression test or tolerance comparison\n");
643 /*******************************************************************************
645 *******************************************************************************/
646 int main(int argc, char **argv)
648 test_cmp_parameters inParam;
649 OPJ_UINT32 it_comp, itpxl;
651 int nbFilenamePGXbase = 0, nbFilenamePGXtest = 0;
652 char *filenamePNGtest= NULL, *filenamePNGbase = NULL, *filenamePNGdiff = NULL;
653 size_t memsizebasefilename, memsizetestfilename;
654 size_t memsizedifffilename;
655 int valueDiff = 0, nbPixelDiff = 0;
656 double sumDiff = 0.0;
657 /* Structures to store image parameters and data*/
658 opj_image_t *imageBase = NULL, *imageTest = NULL, *imageDiff = NULL;
659 opj_image_cmptparm_t* param_image_diff = NULL;
662 /* Get parameters from command line*/
663 if( parse_cmdline_cmp(argc, argv, &inParam) )
665 compare_images_help_display();
669 /* Display Parameters*/
670 printf("******Parameters********* \n");
671 printf(" base_filename = %s\n"
672 " test_filename = %s\n"
673 " nb of Components = %d\n"
674 " Non regression test = %d\n"
675 " separator Base = %s\n"
676 " separator Test = %s\n",
677 inParam.base_filename, inParam.test_filename, inParam.nbcomp,
678 inParam.nr_flag, inParam.separator_base, inParam.separator_test);
680 if ( (inParam.tabMSEvalues != NULL) && (inParam.tabPEAKvalues != NULL))
683 printf(" MSE values = [");
684 for (it_comp2 = 0; it_comp2 < inParam.nbcomp; it_comp2++)
685 printf(" %f ", inParam.tabMSEvalues[it_comp2]);
687 printf(" PEAK values = [");
688 for (it_comp2 = 0; it_comp2 < inParam.nbcomp; it_comp2++)
689 printf(" %f ", inParam.tabPEAKvalues[it_comp2]);
691 printf(" Non-regression test = %d\n", inParam.nr_flag);
694 if (strlen(inParam.separator_base) != 0)
695 nbFilenamePGXbase = inParam.nbcomp;
697 if (strlen(inParam.separator_test) != 0)
698 nbFilenamePGXtest = inParam.nbcomp;
700 printf(" NbFilename to generate from base filename = %d\n", nbFilenamePGXbase);
701 printf(" NbFilename to generate from test filename = %d\n", nbFilenamePGXtest);
702 printf("************************* \n");
704 /*----------BASELINE IMAGE--------*/
705 memsizebasefilename = strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
706 memsizetestfilename = strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
708 decod_format = get_decod_format(&inParam);
709 if( decod_format == -1 )
711 fprintf( stderr, "Unhandled file format\n" );
714 assert( decod_format == PGX_DFMT || decod_format == TIF_DFMT || decod_format == PXM_DFMT );
716 if( decod_format == PGX_DFMT )
718 imageBase = readImageFromFilePGX( inParam.base_filename, nbFilenamePGXbase, inParam.separator_base);
719 if ( imageBase == NULL )
722 else if( decod_format == TIF_DFMT )
724 imageBase = readImageFromFileTIF( inParam.base_filename, nbFilenamePGXbase, "");
725 if ( imageBase == NULL )
728 else if( decod_format == PXM_DFMT )
730 imageBase = readImageFromFilePPM( inParam.base_filename, nbFilenamePGXbase, inParam.separator_base);
731 if ( imageBase == NULL )
735 filenamePNGbase = (char*) malloc(memsizebasefilename);
736 strcpy(filenamePNGbase, inParam.test_filename);
737 strcat(filenamePNGbase, ".base");
738 /*printf("filenamePNGbase = %s [%d / %d octets]\n",filenamePNGbase, strlen(filenamePNGbase),memsizebasefilename );*/
740 /*----------TEST IMAGE--------*/
742 if( decod_format == PGX_DFMT )
744 imageTest = readImageFromFilePGX(inParam.test_filename, nbFilenamePGXtest, inParam.separator_test);
745 if ( imageTest == NULL )
748 else if( decod_format == TIF_DFMT )
750 imageTest = readImageFromFileTIF(inParam.test_filename, nbFilenamePGXtest, "");
751 if ( imageTest == NULL )
754 else if( decod_format == PXM_DFMT )
756 imageTest = readImageFromFilePPM(inParam.test_filename, nbFilenamePGXtest, inParam.separator_test);
757 if ( imageTest == NULL )
761 filenamePNGtest = (char*) malloc(memsizetestfilename);
762 strcpy(filenamePNGtest, inParam.test_filename);
763 strcat(filenamePNGtest, ".test");
764 /*printf("filenamePNGtest = %s [%d / %d octets]\n",filenamePNGtest, strlen(filenamePNGtest),memsizetestfilename );*/
766 /*----------DIFF IMAGE--------*/
769 param_image_diff = malloc( imageBase->numcomps * sizeof(opj_image_cmptparm_t));
771 /* Comparison of header parameters*/
772 printf("Step 1 -> Header comparison\n");
774 /* check dimensions (issue 286)*/
775 if(imageBase->numcomps != imageTest->numcomps )
777 printf("ERROR: dim mismatch (%d><%d)\n", imageBase->numcomps, imageTest->numcomps);
781 for (it_comp = 0; it_comp < imageBase->numcomps; it_comp++)
783 param_image_diff[it_comp].x0 = 0;
784 param_image_diff[it_comp].y0 = 0;
785 param_image_diff[it_comp].dx = 0;
786 param_image_diff[it_comp].dy = 0;
787 param_image_diff[it_comp].sgnd = 0;
788 param_image_diff[it_comp].prec = 8;
789 param_image_diff[it_comp].bpp = 1;
790 param_image_diff[it_comp].h = imageBase->comps[it_comp].h;
791 param_image_diff[it_comp].w = imageBase->comps[it_comp].w;
793 if (imageBase->comps[it_comp].sgnd != imageTest->comps[it_comp].sgnd)
795 printf("ERROR: sign mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).sgnd, ((imageTest->comps)[it_comp]).sgnd);
799 if (((imageBase->comps)[it_comp]).prec != ((imageTest->comps)[it_comp]).prec)
801 printf("ERROR: prec mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).prec, ((imageTest->comps)[it_comp]).prec);
805 if (((imageBase->comps)[it_comp]).bpp != ((imageTest->comps)[it_comp]).bpp)
807 printf("ERROR: byte per pixel mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).bpp, ((imageTest->comps)[it_comp]).bpp);
811 if (((imageBase->comps)[it_comp]).h != ((imageTest->comps)[it_comp]).h)
813 printf("ERROR: height mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).h, ((imageTest->comps)[it_comp]).h);
817 if (((imageBase->comps)[it_comp]).w != ((imageTest->comps)[it_comp]).w)
819 printf("ERROR: width mismatch [comp %d] (%d><%d)\n", it_comp, ((imageBase->comps)[it_comp]).w, ((imageTest->comps)[it_comp]).w);
824 imageDiff = opj_image_create(imageBase->numcomps, param_image_diff, OPJ_CLRSPC_UNSPECIFIED);
826 free(param_image_diff); param_image_diff = NULL;
828 /* Measurement computation*/
829 printf("Step 2 -> measurement comparison\n");
831 memsizedifffilename = strlen(inParam.test_filename) + 1 + 5 + 2 + 4;
832 filenamePNGdiff = (char*) malloc(memsizedifffilename);
833 strcpy(filenamePNGdiff, inParam.test_filename);
834 strcat(filenamePNGdiff, ".diff");
835 /*printf("filenamePNGdiff = %s [%d / %d octets]\n",filenamePNGdiff, strlen(filenamePNGdiff),memsizedifffilename );*/
837 /* Compute pixel diff*/
838 for (it_comp = 0; it_comp < imageDiff->numcomps; it_comp++)
842 for (itpxl = 0; itpxl < ((imageDiff->comps)[it_comp]).w * ((imageDiff->comps)[it_comp]).h; itpxl++)
844 if (abs( ((imageBase->comps)[it_comp]).data[itpxl] - ((imageTest->comps)[it_comp]).data[itpxl] ) > 0)
846 valueDiff = ((imageBase->comps)[it_comp]).data[itpxl] - ((imageTest->comps)[it_comp]).data[itpxl];
847 ((imageDiff->comps)[it_comp]).data[itpxl] = abs(valueDiff);
848 sumDiff += valueDiff;
851 SE += (double)valueDiff * valueDiff;
852 PEAK = (PEAK > abs(valueDiff)) ? PEAK : abs(valueDiff);
855 ((imageDiff->comps)[it_comp]).data[itpxl] = 0;
858 MSE = SE / ( ((imageDiff->comps)[it_comp]).w * ((imageDiff->comps)[it_comp]).h );
860 if (!inParam.nr_flag && (inParam.tabMSEvalues != NULL) && (inParam.tabPEAKvalues != NULL))
861 { /* Conformance test*/
862 printf("<DartMeasurement name=\"PEAK_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, PEAK);
863 printf("<DartMeasurement name=\"MSE_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, MSE);
865 if ( (MSE > inParam.tabMSEvalues[it_comp]) || (PEAK > inParam.tabPEAKvalues[it_comp]) )
867 printf("ERROR: MSE (%f) or PEAK (%f) values produced by the decoded file are greater "
868 "than the allowable error (respectively %f and %f) \n",
869 MSE, PEAK, inParam.tabMSEvalues[it_comp], inParam.tabPEAKvalues[it_comp]);
873 else /* Non regression-test */
875 if ( nbPixelDiff > 0)
880 printf("<DartMeasurement name=\"NumberOfPixelsWithDifferences_%d\" type=\"numeric/int\"> %d </DartMeasurement> \n", it_comp, nbPixelDiff);
881 printf("<DartMeasurement name=\"ComponentError_%d\" type=\"numeric/double\"> %f </DartMeasurement> \n", it_comp, sumDiff);
883 #ifdef OPJ_HAVE_LIBPNG
885 char *filenamePNGbase_it_comp, *filenamePNGtest_it_comp, *filenamePNGdiff_it_comp;
887 filenamePNGbase_it_comp = (char*) malloc(memsizebasefilename);
888 strcpy(filenamePNGbase_it_comp,filenamePNGbase);
890 filenamePNGtest_it_comp = (char*) malloc(memsizetestfilename);
891 strcpy(filenamePNGtest_it_comp,filenamePNGtest);
893 filenamePNGdiff_it_comp = (char*) malloc(memsizedifffilename);
894 strcpy(filenamePNGdiff_it_comp,filenamePNGdiff);
896 sprintf(it_compc, "_%i", it_comp);
897 strcat(it_compc,".png");
898 strcat(filenamePNGbase_it_comp, it_compc);
899 /*printf("filenamePNGbase_it = %s [%d / %d octets]\n",filenamePNGbase_it_comp, strlen(filenamePNGbase_it_comp),memsizebasefilename );*/
900 strcat(filenamePNGtest_it_comp, it_compc);
901 /*printf("filenamePNGtest_it = %s [%d / %d octets]\n",filenamePNGtest_it_comp, strlen(filenamePNGtest_it_comp),memsizetestfilename );*/
902 strcat(filenamePNGdiff_it_comp, it_compc);
903 /*printf("filenamePNGdiff_it = %s [%d / %d octets]\n",filenamePNGdiff_it_comp, strlen(filenamePNGdiff_it_comp),memsizedifffilename );*/
906 if ( imageToPNG(imageBase, filenamePNGbase_it_comp, it_comp) == EXIT_SUCCESS )
908 printf("<DartMeasurementFile name=\"BaselineImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGbase_it_comp);
911 if ( imageToPNG(imageTest, filenamePNGtest_it_comp, it_comp) == EXIT_SUCCESS )
913 printf("<DartMeasurementFile name=\"TestImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGtest_it_comp);
916 if ( imageToPNG(imageDiff, filenamePNGdiff_it_comp, it_comp) == EXIT_SUCCESS )
918 printf("<DartMeasurementFile name=\"DiffferenceImage_%d\" type=\"image/png\"> %s </DartMeasurementFile> \n", it_comp, filenamePNGdiff_it_comp);
922 free(filenamePNGbase_it_comp);
923 free(filenamePNGtest_it_comp);
924 free(filenamePNGdiff_it_comp);
932 printf("---- TEST SUCCEED ----\n");
935 /*-----------------------------*/
936 free(param_image_diff);
938 opj_image_destroy(imageBase);
939 opj_image_destroy(imageTest);
940 opj_image_destroy(imageDiff);
942 free(filenamePNGbase);
943 free(filenamePNGtest);
944 free(filenamePNGdiff);
946 free(inParam.tabMSEvalues);
947 free(inParam.tabPEAKvalues);
948 free(inParam.base_filename);
949 free(inParam.test_filename);
951 return failed ? EXIT_FAILURE : EXIT_SUCCESS;