[trunk] Fix find_path behavior within cross-compilation execution
[openjpeg.git] / tests / compareRAWimages.c
index 631d028d785719bc5edbcea6362c2aef4837763b..4f18352932edab3b4ec2c30fa9bb184c19302e23 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
+ * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France 
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -36,9 +36,7 @@
 #include <string.h>
 #include <ctype.h>
 
-#include "getopt.h"
-
-void compareRAWimages_help_display(void);
+#include "opj_getopt.h"
 
 typedef struct test_cmp_parameters
 {
@@ -51,7 +49,7 @@ typedef struct test_cmp_parameters
 /*******************************************************************************
  * Command line help function
  *******************************************************************************/
-void compareRAWimages_help_display(void) {
+static void compareRAWimages_help_display(void) {
        fprintf(stdout,"\nList of parameters for the comparePGX function  \n");
        fprintf(stdout,"\n");
        fprintf(stdout,"  -b \t REQUIRED \t filename to the reference/baseline RAW image \n");
@@ -62,51 +60,51 @@ void compareRAWimages_help_display(void) {
 /*******************************************************************************
  * Parse command line
  *******************************************************************************/
-int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
+static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
 {
        int sizemembasefile, sizememtestfile;
        int index;
        const char optlist[] = "b:t:";
        int c;
 
-       // Init parameters
+       /* Init parameters*/
        param->base_filename = NULL;
        param->test_filename = NULL;
 
-       opterr = 0;
-       while ((c = getopt(argc, argv, optlist)) != -1)
+       opj_opterr = 0;
+       while ((c = opj_getopt(argc, argv, optlist)) != -1)
                switch (c)
                {
                case 'b':
-                       sizemembasefile = (int)strlen(optarg)+1;
+                       sizemembasefile = (int)strlen(opj_optarg)+1;
                        param->base_filename = (char*) malloc(sizemembasefile);
                        param->base_filename[0] = '\0';
-                       strncpy(param->base_filename, optarg, strlen(optarg));
-                       param->base_filename[strlen(optarg)] = '\0';
-                       //printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );
+                       strncpy(param->base_filename, opj_optarg, strlen(opj_optarg));
+                       param->base_filename[strlen(opj_optarg)] = '\0';
+                       /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
                        break;
                case 't':
-                       sizememtestfile = (int) strlen(optarg) + 1;
+                       sizememtestfile = (int) strlen(opj_optarg) + 1;
                        param->test_filename = (char*) malloc(sizememtestfile);
                        param->test_filename[0] = '\0';
-                       strncpy(param->test_filename, optarg, strlen(optarg));
-                       param->test_filename[strlen(optarg)] = '\0';
-                       //printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);
+                       strncpy(param->test_filename, opj_optarg, strlen(opj_optarg));
+                       param->test_filename[strlen(opj_optarg)] = '\0';
+                       /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
                        break;
                case '?':
-                       if ((optopt == 'b') || (optopt == 't'))
-                               fprintf(stderr, "Option -%c requires an argument.\n", optopt);
+                       if ((opj_optopt == 'b') || (opj_optopt == 't'))
+                               fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
                        else
-                               if (isprint(optopt))    fprintf(stderr, "Unknown option `-%c'.\n", optopt);
-                               else    fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
+                               if (isprint(opj_optopt))        fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
+                               else    fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
                        return 1;
                default:
-                       fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, optarg);
+                       fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
                        break;
                }
 
-       if (optind != argc) {
-               for (index = optind; index < argc; index++)
+       if (opj_optind != argc) {
+               for (index = opj_optind; index < argc; index++)
                        fprintf(stderr,"Non-option argument %s\n", argv[index]);
                return EXIT_FAILURE;
     }
@@ -119,30 +117,65 @@ int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
  *******************************************************************************/
 int main(int argc, char **argv)
 {
+  int pos = 0;
        test_cmp_parameters inParam;
        FILE *file_test=NULL, *file_base=NULL;
        unsigned char equal = 1;
 
-       // Get parameters from command line
+       /* Get parameters from command line*/
        if (parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE)
        {
                compareRAWimages_help_display();
+
+               /* Free Memory */
+               if (inParam.base_filename){
+                       free(inParam.base_filename);
+                       inParam.base_filename = NULL;
+               }
+               if (inParam.test_filename){
+                       free(inParam.test_filename);
+                       inParam.test_filename = NULL;
+               }
+
                return EXIT_FAILURE;
        }
 
        file_test = fopen(inParam.test_filename, "rb");
        if (!file_test) {
                fprintf(stderr, "Failed to open %s for reading !!\n", inParam.test_filename);
+
+               /* Free Memory */
+               if (inParam.base_filename){
+                       free(inParam.base_filename);
+                       inParam.base_filename = NULL;
+               }
+               if (inParam.test_filename){
+                       free(inParam.test_filename);
+                       inParam.test_filename = NULL;
+               }
+
                return EXIT_FAILURE;
        }
 
        file_base = fopen(inParam.base_filename, "rb");
        if (!file_base) {
                fprintf(stderr, "Failed to open %s for reading !!\n", inParam.base_filename);
+
+               /* Free Memory */
+               if (inParam.base_filename){
+                       free(inParam.base_filename);
+                       inParam.base_filename = NULL;
+               }
+               if (inParam.test_filename){
+                       free(inParam.test_filename);
+                       inParam.test_filename = NULL;
+               }
+
+               fclose(file_test);
                return EXIT_FAILURE;
        }
 
-       // Read simultaneously the two files
+       /* Read simultaneously the two files*/
        while (equal)
        {
                unsigned char value_test = 0;
@@ -150,33 +183,44 @@ int main(int argc, char **argv)
                unsigned char value_base = 0;
                unsigned char eof_base = 0;
 
-               // Read one byte
+               /* Read one byte*/
                if (!fread(&value_test, 1, 1, file_test)) {
                        eof_test = 1;
                }
 
-               // Read one byte
+               /* Read one byte*/
                if (!fread(&value_base, 1, 1, file_base)) {
-                       eof_base = 1;;
+                       eof_base = 1;
                }
 
-               // End of file reached by the two files?
+               /* End of file reached by the two files?*/
                if (eof_test && eof_base)
                        break;
 
-               // End of file reached only by one file?
+               /* End of file reached only by one file?*/
                if (eof_test || eof_base)
                {
                        fprintf(stdout,"Files have different sizes.\n");
                        equal = 0;
                }
 
-               // Binary values are equal?
+               /* Binary values are equal?*/
                if (value_test != value_base)
                {
-                       fprintf(stdout,"Binary values read in the file are different.\n");
+                       fprintf(stdout,"Binary values read in the file are different %x vs %x at position %d.\n", value_test, value_base, pos);
                        equal = 0;
                }
+    pos++;
+       }
+
+       /* Free Memory */
+       if (inParam.base_filename){
+               free(inParam.base_filename);
+               inParam.base_filename = NULL;
+       }
+       if (inParam.test_filename){
+               free(inParam.test_filename);
+               inParam.test_filename = NULL;
        }
 
        fclose(file_test);