[trunk] add "-mct {0,1,2}" option to opj_compress to allow MCT to be
[openjpeg.git] / src / bin / jp2 / convert.c
index 72babbaad903db8f619df89edc52cfda0e31161b..d3e9773c912d530e407f1554fa5aeca3ef578382 100644 (file)
@@ -94,7 +94,7 @@ struct tga_header
 
 static unsigned short get_ushort(unsigned short val) {
 
-#ifdef WORDS_BIGENDIAN
+#ifdef OPJ_BIG_ENDIAN
     return( ((val & 0xff) << 8) + (val >> 8) );
 #else
     return( val );
@@ -181,9 +181,9 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
     return 1;
 }
 
-#if WORDS_BIGENDIAN == 1
+#ifdef OPJ_BIG_ENDIAN
 
-static inline int16_t swap16(int16_t x)
+static INLINE int16_t swap16(int16_t x)
 {
     return((((u_int16_t)x & 0x00ffU) <<  8) |
            (((u_int16_t)x & 0xff00U) >>  8));
@@ -228,7 +228,7 @@ static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
     image_w = (unsigned short)width;
     image_h = (unsigned short) height;
 
-#if WORDS_BIGENDIAN == 0
+#ifndef OPJ_BIG_ENDIAN
     if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
     if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
 #else
@@ -416,6 +416,7 @@ int imagetotga(opj_image_t * image, const char *outfile) {
     float scale;
     FILE *fdest;
     size_t res;
+    fails = 1;
 
     fdest = fopen(outfile, "wb");
     if (!fdest) {
@@ -711,7 +712,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
 
         /* Place the cursor at the beginning of the image information */
         fseek(IN, 0, SEEK_SET);
-        fseek(IN, File_h.bfOffBits, SEEK_SET);
+        fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
 
         W = Info_h.biWidth;
         H = Info_h.biHeight;
@@ -772,7 +773,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
 
             /* Place the cursor at the beginning of the image information */
             fseek(IN, 0, SEEK_SET);
-            fseek(IN, File_h.bfOffBits, SEEK_SET);
+            fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
 
             W = Info_h.biWidth;
             H = Info_h.biHeight;
@@ -924,7 +925,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
 
                 /* Place the cursor at the beginning of the image information */
                 fseek(IN, 0, SEEK_SET);
-                fseek(IN, File_h.bfOffBits, SEEK_SET);
+                fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
 
                 W = Info_h.biWidth;
                 H = Info_h.biHeight;
@@ -1420,13 +1421,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
 
 #define CLAMP(x,a,b) x < a ? a : (x > b ? b : x)
 
-#ifdef _MSC_VER
-#define STIN static __inline
-#else
-#define STIN static inline
-#endif
-
-STIN int clamp( const int value, const int prec, const int sgnd )
+static INLINE int clamp( const int value, const int prec, const int sgnd )
 {
   if( sgnd )
     {
@@ -1721,7 +1716,7 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph)
         }
         if(ph->depth < 1 || ph->depth > 4) return;
 
-        if(ph->width && ph->height && ph->depth & ph->maxval && ttype)
+        if(ph->width && ph->height && ph->depth && ph->maxval && ttype)
             ph->ok = 1;
     }
     else
@@ -1872,8 +1867,12 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
             {
                 for(compno = 0; compno < numcomps; compno++)
                 {
-                    if ( !fread(&c0, 1, 1, fp) )
-                        fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+                if ( !fread(&c0, 1, 1, fp) )
+                  {
+                  fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
+                  opj_image_destroy(image);
+                  return NULL;
+                  }
                     if(one)
                     {
                         image->comps[compno].data[i] = c0;
@@ -2105,8 +2104,15 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
 
     for (compno = 0; compno < ncomp; compno++)
     {
-        if (ncomp > 1)
-            sprintf(destname, "%d.%s", compno, outfile);
+    if (ncomp > 1)
+      {
+      /*sprintf(destname, "%d.%s", compno, outfile);*/
+      const size_t olen = strlen(outfile);
+      const size_t dotpos = olen - 4;
+
+      strncpy(destname, outfile, dotpos);
+      sprintf(destname+dotpos, "_%d.pgm", compno);
+      }
         else
             sprintf(destname, "%s", outfile);
 
@@ -3216,7 +3222,7 @@ opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
     opj_image_cmptparm_t cmptparm[4];
     int sub_dx, sub_dy;
     unsigned int nr_comp;
-    int *r, *g, *b, *a;
+    int *r, *g, *b, *a = NULL;
     unsigned char sigbuf[8];
 
     if((reader = fopen(read_idf, "rb")) == NULL)
@@ -3306,7 +3312,7 @@ opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
 
     png_read_image(png, rows);
 
-    memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
+    memset(cmptparm, 0, sizeof(cmptparm));
 
     sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy;
 
@@ -3334,7 +3340,7 @@ opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
     r = image->comps[0].data;
     g = image->comps[1].data;
     b = image->comps[2].data;
-    a = image->comps[3].data;
+    if(has_alpha) a = image->comps[3].data;
 
     for(i = 0; i < height; ++i)
     {