]> granicus.if.org Git - openjpeg/commitdiff
Added support for PNG image format [Not yet functional under WIN32]. Thanks to Winfri...
authorFrancois-Olivier Devaux <fodevaux@users.noreply.github.com>
Fri, 26 Mar 2010 15:46:17 +0000 (15:46 +0000)
committerFrancois-Olivier Devaux <fodevaux@users.noreply.github.com>
Fri, 26 Mar 2010 15:46:17 +0000 (15:46 +0000)
ChangeLog
codec/Makefile
codec/convert.c
codec/convert.h
codec/image_to_j2k.c
codec/j2k_to_image.c

index 5dd9201f3eb03ccd4859af9c632f510b7b781bad..4ca3380f14d90461b4b988c76d471f3a8f73da07 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,9 @@ What's New for OpenJPEG
 ! : changed
 + : added
 
+March 26, 2010
+* [FOD] Added support for PNG image format [Not yet functional under WIN32]. Thanks to Winfried for this patch.        See details here http://code.google.com/p/openjpeg/issues/detail?id=16
+
 March 24, 2010
 * [FOD] Code improvements using 'g++-4.4.0' to trace the code. Thanks to Winfried for this patch.              
 
index 14487a5f231ee5469071b1b8af98fc5cbca25767..58996fb4cb81de1ff8aca278f65ee93311d3800c 100644 (file)
@@ -5,10 +5,10 @@ CFLAGS = -O3 -lstdc++ # -g -p -pg
 all: j2k_to_image image_to_j2k
 
 j2k_to_image: j2k_to_image.c ../libopenjpeg.a
-       gcc $(CFLAGS) compat/getopt.c index.c convert.c j2k_to_image.c -o j2k_to_image -L.. -lopenjpeg -I ../libopenjpeg/ -lm -ltiff
+       gcc $(CFLAGS) compat/getopt.c index.c convert.c j2k_to_image.c -o j2k_to_image -L.. -lopenjpeg -I ../libopenjpeg/ -lm -ltiff -lpng
 
 image_to_j2k: image_to_j2k.c ../libopenjpeg.a
-       gcc $(CFLAGS) compat/getopt.c index.c convert.c image_to_j2k.c -o image_to_j2k -L.. -lopenjpeg -I ../libopenjpeg/ -lm -ltiff
+       gcc $(CFLAGS) compat/getopt.c index.c convert.c image_to_j2k.c -o image_to_j2k -L.. -lopenjpeg -I ../libopenjpeg/ -lm -ltiff -lpng
 
 clean:
        rm -f j2k_to_image image_to_j2k
index 182477b3299c390da3a2a2e74c3d72270960f657..631cae051e8f81be7ad8cc3a2ae4bf21e6b5b1ef 100644 (file)
 #include <string.h>
 #ifdef WIN32
 #include "../libs/libtiff/tiffio.h"
+#include "../libs/libpng/png.h"
 #else
 #include <tiffio.h>
+#include <png.h>
 #endif /* WIN32 */
 #include "openjpeg.h"
 #include "convert.h"
@@ -2120,9 +2122,9 @@ int imagetoraw(opj_image_t * image, const char *outfile)
                                        for(row = 0; row < w; row++)    {                                       
                                                unsigned char temp;
                                                curr = (signed short int) (*ptr & mask);                                                
-                                               temp = curr >> 8;
+                                               temp = (unsigned char) (curr >> 8);
                                                fwrite(&temp, 1, 1, rawFile);
-                                               temp = curr;
+                                               temp = (unsigned char) curr;
                                                fwrite(&temp, 1, 1, rawFile);
                                                ptr++;
                                        }
@@ -2137,9 +2139,9 @@ int imagetoraw(opj_image_t * image, const char *outfile)
                                        for(row = 0; row < w; row++)    {                               
                                                unsigned char temp;
                                                curr = (unsigned short int) (*ptr & mask);                                              
-                                               temp = curr >> 8;
+                                               temp = (unsigned char) (curr >> 8);
                                                fwrite(&temp, 1, 1, rawFile);
-                                               temp = curr;
+                                               temp = (unsigned char) curr;
                                                fwrite(&temp, 1, 1, rawFile);
                                                ptr++;
                                        }
@@ -2160,3 +2162,357 @@ int imagetoraw(opj_image_t * image, const char *outfile)
        fclose(rawFile);
        return 0;
 }
+
+opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
+#ifdef WIN32
+{
+       printf("Error. PNG format is not yet handled under windows\n");
+       return NULL;
+}
+#else
+{
+    png_bytep row;
+    png_structp png;
+    png_infop info;
+    double gamma, display_exponent;
+    int bit_depth, interlace_type, compression_type, filter_type;
+    int unit, pass, nr_passes;
+    png_uint_32 resx, resy;
+    unsigned int i, max, src_w;
+    png_uint_32 width, height;
+    int color_type, has_alpha;
+    unsigned char *png_buf, *s;
+    FILE *reader;
+/* j2k: */
+    opj_image_t *image;
+    opj_image_cmptparm_t cmptparm[4];
+    int sub_dx, sub_dy;
+    unsigned int nr_comp;
+    int *r, *g, *b, *a;
+
+    if ((reader = fopen(read_idf, "rb")) == NULL) {
+       fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
+       return NULL;
+    }
+    nr_passes = 0;
+    png_buf = NULL;
+
+/* libpng-VERSION/example.c: 
+ * PC : screen_gamma = 2.2;
+ * Mac: screen_gamma = 1.7 or 1.0;
+*/
+    display_exponent = 2.2;
+
+    if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
+                                     NULL, NULL, NULL)) == NULL)
+       goto fin;
+    if ((info = png_create_info_struct(png)) == NULL)
+       goto fin;
+
+    if (setjmp(png_jmpbuf(png)))
+       goto fin;
+
+    png_init_io(png, reader);
+    png_read_info(png, info);
+
+    png_get_IHDR(png, info, &width, &height,
+                &bit_depth, &color_type, &interlace_type,
+                &compression_type, &filter_type);
+
+    if (color_type == PNG_COLOR_TYPE_PALETTE)
+       png_set_expand(png);
+    else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
+       png_set_expand(png);
+
+    if (png_get_valid(png, info, PNG_INFO_tRNS))
+       png_set_expand(png);
+
+    if (bit_depth == 16)
+       png_set_strip_16(png);
+
+/* GRAY => RGB; GRAY_ALPHA => RGBA
+*/
+    if (color_type == PNG_COLOR_TYPE_GRAY
+       || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
+       png_set_gray_to_rgb(png);
+       color_type =
+           (color_type == PNG_COLOR_TYPE_GRAY ? PNG_COLOR_TYPE_RGB :
+            PNG_COLOR_TYPE_RGB_ALPHA);
+    }
+    if (!png_get_gAMA(png, info, &gamma))
+       gamma = 0.45455;
+
+    png_set_gamma(png, display_exponent, gamma);
+
+    nr_passes = png_set_interlace_handling(png);
+
+    png_read_update_info(png, info);
+
+    png_get_pHYs(png, info, &resx, &resy, &unit);
+
+    color_type = png_get_color_type(png, info);
+
+    has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
+
+    if (has_alpha)
+       nr_comp = 4;
+    else
+       nr_comp = 3;
+
+    src_w = width * nr_comp;
+    png_buf = (unsigned char *) malloc(src_w * height);
+
+    if (nr_passes == 0)
+       nr_passes = 1;
+
+    for (pass = 0; pass < nr_passes; pass++) {
+       s = png_buf;
+
+       for (i = 0; i < height; i++) {
+/* libpng.3:
+ * If you want the "sparkle" effect, just call png_read_rows() as
+ * normal, with the third parameter NULL.
+*/
+           png_read_rows(png, &s, NULL, 1);
+
+           s += src_w;
+       }
+    }
+    memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
+
+    sub_dx = params->subsampling_dx;
+    sub_dy = params->subsampling_dy;
+
+    for (i = 0; i < nr_comp; ++i) {
+       cmptparm[i].prec = 8;
+       cmptparm[i].bpp = 8;
+       cmptparm[i].sgnd = 0;
+       cmptparm[i].dx = sub_dx;
+       cmptparm[i].dy = sub_dy;
+       cmptparm[i].w = width;
+       cmptparm[i].h = height;
+    }
+
+    image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
+
+    if (image == NULL)
+       goto fin;
+
+    image->x0 = params->image_offset_x0;
+    image->y0 = params->image_offset_y0;
+    image->x1 =
+       params->image_offset_x0 + (width - 1) * sub_dx + 1 + image->x0;
+    image->y1 =
+       params->image_offset_y0 + (height - 1) * sub_dy + 1 + image->y0;
+
+    r = image->comps[0].data;
+    g = image->comps[1].data;
+    b = image->comps[2].data;
+    a = image->comps[3].data;
+    s = png_buf;
+
+    max = width * height;
+
+    for (i = 0; i < max; ++i) {
+       *r++ = *s++;
+       *g++ = *s++;
+       *b++ = *s++;
+
+       if (has_alpha)
+           *a++ = *s++;
+    }
+
+  fin:
+    if (png)
+       png_destroy_read_struct(&png, &info, NULL);
+    if (png_buf)
+       free(png_buf);
+
+    fclose(reader);
+
+    return image;
+
+}                              /* pngtoimage() */
+#endif
+
+int imagetopng(opj_image_t * image, const char *write_idf)
+#ifdef WIN32
+{
+       printf("Error. PNG format is not yet handled under windows\n");
+       return -1;
+}
+#else
+{
+    FILE *writer;
+    png_structp png_ptr;
+    png_infop info_ptr;
+    int *rs, *gs, *bs, *as;
+    unsigned char *row_buf, *d;
+    int fails, mono, graya, rgb, rgba;
+    int width, height, nr_colors, color_type;
+    int bit_depth, adjust, x, y;
+    png_color_8 sig_bit;
+
+    writer = fopen(write_idf, "wb");
+
+    if (writer == NULL)
+       return 1;
+
+    info_ptr = NULL;
+    fails = 1;
+
+/* Create and initialize the png_struct with the desired error handler
+ * functions.  If you want to use the default stderr and longjump method,
+ * you can supply NULL for the last three parameters.  We also check that
+ * the library version is compatible with the one used at compile time,
+ * in case we are using dynamically linked libraries.  REQUIRED.
+*/
+    png_ptr =
+       png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+/*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
+
+    if (png_ptr == NULL)
+       goto fin;
+
+/* Allocate/initialize the image information data.  REQUIRED 
+*/
+    info_ptr = png_create_info_struct(png_ptr);
+
+    if (info_ptr == NULL)
+       goto fin;
+
+/* Set error handling.  REQUIRED if you are not supplying your own
+ * error handling functions in the png_create_write_struct() call.
+*/
+    if (setjmp(png_jmpbuf(png_ptr)))
+       goto fin;
+
+/* I/O initialization functions is REQUIRED 
+*/
+    png_init_io(png_ptr, writer);
+
+/* Set the image information here.  Width and height are up to 2^31,
+ * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
+ * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
+ * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
+ * or PNG_COLOR_TYPE_RGB_ALPHA.  interlace is either PNG_INTERLACE_NONE or
+ * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
+ * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. 
+ * REQUIRED
+*/
+    png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
+
+    mono = graya = rgb = rgba = adjust = 0;
+
+    nr_colors = image->numcomps;
+    width = image->comps[0].w;
+    height = image->comps[0].h;
+    rs = image->comps[0].data;
+
+    if (nr_colors == 2) {
+       graya = 1;
+       color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
+       sig_bit.gray = image->comps[0].prec;
+       bit_depth = image->comps[0].prec;
+       as = image->comps[1].data;
+    } else if (nr_colors == 3) {
+       rgb = 1;
+       color_type = PNG_COLOR_TYPE_RGB;
+       sig_bit.red = image->comps[0].prec;
+       sig_bit.green = image->comps[1].prec;
+       sig_bit.blue = image->comps[2].prec;
+       bit_depth = image->comps[0].prec;
+       gs = image->comps[1].data;
+       bs = image->comps[2].data;
+       if (image->comps[0].sgnd)
+           adjust = 1 << (image->comps[0].prec - 1);
+    } else if (nr_colors == 4) {
+       rgb = rgba = 1;
+       color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+       sig_bit.red = image->comps[0].prec;
+       sig_bit.green = image->comps[1].prec;
+       sig_bit.blue = image->comps[2].prec;
+       sig_bit.alpha = image->comps[3].prec;
+       bit_depth = image->comps[0].prec;
+       gs = image->comps[1].data;
+       bs = image->comps[2].data;
+       as = image->comps[3].data;
+       if (image->comps[0].sgnd)
+           adjust = 1 << (image->comps[0].prec - 1);
+    } else {
+       mono = 1;
+       color_type = PNG_COLOR_TYPE_GRAY;
+       sig_bit.gray = image->comps[0].prec;
+       bit_depth = image->comps[0].prec;
+    }
+    png_set_sBIT(png_ptr, info_ptr, &sig_bit);
+
+    png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
+                color_type,
+                PNG_INTERLACE_NONE,
+                PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
+
+#ifdef HIDDEN_CODE
+/* Optional gamma chunk is strongly suggested if you have any guess
+ * as to the correct gamma of the image.
+*/
+
+    if (gamma > 0.0) {
+       png_set_gAMA(png_ptr, info_ptr, gamma);
+    }
+#endif                         /* HIDDEN_CODE */
+#ifdef HIDDEN_CODE
+    if (have_bg) {
+       png_color_16 background;
+
+       background.red = bg_red;
+       background.green = bg_green;
+       background.blue = bg_blue;
+
+       png_set_bKGD(png_ptr, info_ptr, &background);
+    }
+#endif                         /* HIDDEN_CODE */
+    png_write_info(png_ptr, info_ptr);
+
+    png_set_packing(png_ptr);
+
+    row_buf = (unsigned char *) malloc(width * nr_colors);
+
+    for (y = 0; y < height; ++y) {
+       d = row_buf;
+
+       for (x = 0; x < width; ++x) {
+           if (mono) {
+               *d++ = (unsigned char) *rs++;
+           }
+           if (graya) {
+               *d++ = (unsigned char) *rs++;
+               *d++ = (unsigned char) *as++;
+           } else if (rgb) {
+               *d++ = (unsigned char) (*rs++ + adjust);
+               *d++ = (unsigned char) (*gs++ + adjust);
+               *d++ = (unsigned char) (*bs++ + adjust);
+
+               if (rgba)
+                   *d++ = (unsigned char) (*as++ + adjust);
+           }
+       }                       /* for(x) */
+
+       png_write_row(png_ptr, row_buf);
+
+    }                          /* for(y) */
+
+    png_write_end(png_ptr, info_ptr);
+
+    fails = 0;
+
+  fin:
+
+    if (png_ptr) {
+       png_destroy_write_struct(&png_ptr, &info_ptr);
+    }
+    fclose(writer);
+
+    return fails;
+}
+#endif
index 98611c66b4f1a08e99d1f46d8f8108aac83c3e12..1dc58d7233a69b8aace759fdae99d95f9579f234 100644 (file)
@@ -55,7 +55,7 @@ int imagetotga(opj_image_t * image, const char *outfile);
 opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters);
 int imagetobmp(opj_image_t *image, const char *outfile);
 
-/* TIFF to image conversion*/
+/* TIFF conversion*/
 opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters);
 int imagetotif(opj_image_t *image, const char *outfile);
 /**
@@ -74,5 +74,9 @@ int imagetopnm(opj_image_t *image, const char *outfile);
 int imagetoraw(opj_image_t * image, const char *outfile);
 opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp);
 
+/* PNG conversion*/
+extern int imagetopng(opj_image_t *image, const char *write_idf);
+extern opj_image_t* pngtoimage(const char *filename, opj_cparameters_t *parameters);
+
 #endif /* __J2K_CONVERT_H */
 
index 75d1571004811f808faa25b0518768d0b6a0dc1b..5dd1035581c9c911372a5babeb48b26000438bf3 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (c) 2001-2003, David Janssens
  * Copyright (c) 2002-2003, Yannick Verschueren
  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
- * Copyright (c) 2005, Herve Drolon, FreeImage Team 
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2006-2007, Parvatha Elangovan
  * All rights reserved.
  *
@@ -59,7 +59,7 @@
 #define TIF_DFMT 14
 #define RAW_DFMT 15
 #define TGA_DFMT 16
-
+#define PNG_DFMT 17
 /* ----------------------------------------------------------------------- */
 #define CINEMA_24_CS 1302083   /*Codestream length for 24fps*/
 #define CINEMA_48_CS 651041            /*Codestream length for 48fps*/
@@ -152,10 +152,10 @@ void encode_help_display() {
        fprintf(stdout,"-h           : display the help information \n ");
        fprintf(stdout,"\n");
        fprintf(stdout,"-cinema2K    : Digital Cinema 2K profile compliant codestream for 2K resolution.(-cinema2k 24 or 48) \n");
-  fprintf(stdout,"       Need to specify the frames per second for a 2K resolution. Only 24 or 48 fps is allowed\n"); 
+  fprintf(stdout,"       Need to specify the frames per second for a 2K resolution. Only 24 or 48 fps is allowed\n");
        fprintf(stdout,"\n");
        fprintf(stdout,"-cinema4K    : Digital Cinema 4K profile compliant codestream for 4K resolution \n");
-       fprintf(stdout,"          Frames per second not required. Default value is 24fps\n"); 
+       fprintf(stdout,"          Frames per second not required. Default value is 24fps\n");
        fprintf(stdout,"\n");
        fprintf(stdout,"-r           : different compression ratios for successive layers (-r 20,10,5)\n ");
        fprintf(stdout,"                 - The rate specified for each quality level is the desired \n");
@@ -342,7 +342,7 @@ OPJ_PROG_ORDER give_progression(char progression[4]) {
 
 int get_num_images(char *imgdirpath){
        DIR *dir;
-       struct dirent* content; 
+       struct dirent* content;
        int num_images = 0;
 
        /*Reading the input images from given input directory*/
@@ -364,7 +364,7 @@ int get_num_images(char *imgdirpath){
 
 int load_images(dircnt_t *dirptr, char *imgdirpath){
        DIR *dir;
-       struct dirent* content; 
+       struct dirent* content;
        int i = 0;
 
        /*Reading the input images from given input directory*/
@@ -384,16 +384,16 @@ int load_images(dircnt_t *dirptr, char *imgdirpath){
                strcpy(dirptr->filename[i],content->d_name);
                i++;
        }
-       return 0;       
+       return 0;
 }
 
 int get_file_format(char *filename) {
        unsigned int i;
        static const char *extension[] = {
-    "pgx", "pnm", "pgm", "ppm", "bmp", "tif", "raw", "tga", "j2k", "jp2", "j2c"
+    "pgx", "pnm", "pgm", "ppm", "bmp", "tif", "raw", "tga", "png", "j2k", "jp2", "j2c"
     };
        static const int format[] = {
-    PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT
+    PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT
     };
        char * ext = strrchr(filename, '.');
        if (ext == NULL)
@@ -440,15 +440,15 @@ char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_cparamet
 }
 
 static int initialise_4K_poc(opj_poc_t *POC, int numres){
-       POC[0].tile  = 1; 
-       POC[0].resno0  = 0; 
+       POC[0].tile  = 1;
+       POC[0].resno0  = 0;
        POC[0].compno0 = 0;
        POC[0].layno1  = 1;
        POC[0].resno1  = numres-1;
        POC[0].compno1 = 3;
        POC[0].prg1 = CPRL;
        POC[1].tile  = 1;
-       POC[1].resno0  = numres-1; 
+       POC[1].resno0  = numres-1;
        POC[1].compno0 = 0;
        POC[1].layno1  = 1;
        POC[1].resno1  = numres;
@@ -461,7 +461,7 @@ void cinema_parameters(opj_cparameters_t *parameters){
        parameters->tile_size_on = false;
        parameters->cp_tdx=1;
        parameters->cp_tdy=1;
-       
+
        /*Tile part*/
        parameters->tp_flag = 'C';
        parameters->tp_on = 1;
@@ -473,7 +473,7 @@ void cinema_parameters(opj_cparameters_t *parameters){
        parameters->image_offset_y0 = 0;
 
        /*Codeblock size= 32*32*/
-       parameters->cblockw_init = 32;  
+       parameters->cblockw_init = 32;
        parameters->cblockh_init = 32;
        parameters->csty |= 0x01;
 
@@ -508,7 +508,7 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_
                        parameters->cp_rsiz = STD_RSIZ;
                }
        break;
-       
+
        case CINEMA4K_24:
                if(parameters->numresolution < 1){
                                parameters->numresolution = 1;
@@ -516,7 +516,7 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_
                                parameters->numresolution = 7;
                        }
                if (!((image->comps[0].w == 4096) | (image->comps[0].h == 2160))){
-                       fprintf(stdout,"Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4" 
+                       fprintf(stdout,"Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4"
                                "(4K profile) compliance requires that at least one of coordinates match 4096 x 2160\n",
                                image->comps[0].w,image->comps[0].h);
                        parameters->cp_rsiz = STD_RSIZ;
@@ -531,13 +531,13 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_
                        for(i=0 ; i<parameters->tcp_numlayers ; i++){
                                temp_rate = 0 ;
                                if (img_fol->rates[i]== 0){
-                                       parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
+                                       parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
                                        (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
                                }else{
-                                       temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
+                                       temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
                                                (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
                                        if (temp_rate > CINEMA_24_CS ){
-                                               parameters->tcp_rates[i]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
+                                               parameters->tcp_rates[i]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
                                                (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
                                        }else{
                                                parameters->tcp_rates[i]= img_fol->rates[i];
@@ -546,18 +546,18 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_
                        }
                        parameters->max_comp_size = COMP_24_CS;
                        break;
-               
+
                case CINEMA2K_48:
                        for(i=0 ; i<parameters->tcp_numlayers ; i++){
                                temp_rate = 0 ;
                                if (img_fol->rates[i]== 0){
-                                       parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
+                                       parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
                                        (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
                                }else{
-                                       temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
+                                       temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
                                                (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
                                        if (temp_rate > CINEMA_48_CS ){
-                                               parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ 
+                                               parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
                                                (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
                                        }else{
                                                parameters->tcp_rates[i]= img_fol->rates[i];
@@ -614,11 +614,12 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                        case TIF_DFMT:
                                        case RAW_DFMT:
                                        case TGA_DFMT:
+                                       case PNG_DFMT:
                                                break;
                                        default:
                                                fprintf(stderr,
                                                        "!! Unrecognized format for infile : %s "
-              "[accept only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga] !!\n\n", 
+              "[accept only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga] !!\n\n",
                                                        infile);
                                                return 1;
                                }
@@ -687,7 +688,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
 
                                /* ----------------------------------------------------- */
 
-                       
+
                        case 'F':                       /* Raw image format parameters */
                        {
                                char signo;
@@ -707,7 +708,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
                                                fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
                                                fprintf(stderr,"Aborting\n");
-                                       }                                       
+                                       }
                                }
                                else {
                                        fprintf(stderr,"\nError: invalid raw image parameters\n");
@@ -1000,16 +1001,16 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                        break;
 
                        /* ------------------------------------------------------ */
-                       
+
                        case 'v':                       /* Tile part generation*/
                        {
                                parameters->tp_flag = optarg[0];
                                parameters->tp_on = 1;
                        }
-                       break;  
+                       break;
 
                                /* ------------------------------------------------------ */
-                       
+
                        case 'z':                       /* Image Directory path */
                        {
                                img_fol->imgdirpath = (char*)malloc(strlen(optarg) + 1);
@@ -1019,7 +1020,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                        break;
 
                                /* ------------------------------------------------------ */
-                       
+
                        case 'w':                       /* Digital Cinema 2K profile compliance*/
                        {
                                int fps=0;
@@ -1034,12 +1035,12 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                }
                                fprintf(stdout,"CINEMA 2K compliant codestream\n");
                                parameters->cp_rsiz = CINEMA2K;
-                               
+
                        }
                        break;
-                               
+
                                /* ------------------------------------------------------ */
-                       
+
                        case 'y':                       /* Digital Cinema 4K profile compliance*/
                        {
                                parameters->cp_cinema = CINEMA4K_24;
@@ -1047,13 +1048,13 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                parameters->cp_rsiz = CINEMA4K;
                        }
                        break;
-                               
+
                                /* ------------------------------------------------------ */
 
 /* UniPG>> */
 #ifdef USE_JPWL
                                /* ------------------------------------------------------ */
-                       
+
                        case 'W':                       /* JPWL capabilities switched on */
                        {
                                char *token = NULL;
@@ -1277,7 +1278,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                        fprintf(stderr, "ERROR -> invalid sensitivity method selection = %s\n", token);
                                                        return 1;
                                                };
-                                               
+
                                                parameters->jpwl_sens_size = 2; /* 2 bytes for default size */
                                        }
 
@@ -1304,7 +1305,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                        fprintf(stderr, "ERROR -> invalid addressing selection = %s\n", token);
                                                        return 1;
                                                };
-                                               
+
                                        }
 
                                        /* search sensitivity size */
@@ -1330,7 +1331,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                        fprintf(stderr, "ERROR -> invalid size selection = %s\n", token);
                                                        return 1;
                                                };
-                                               
+
                                        }
 
                                        /* search range method */
@@ -1356,7 +1357,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                                        fprintf(stderr, "ERROR -> invalid range selection = %s\n", token);
                                                        return 1;
                                                };
-                                               
+
                                        }
 
                                        /* next token or bust */
@@ -1511,14 +1512,14 @@ int main(int argc, char **argv) {
        if(parse_cmdline_encoder(argc, argv, &parameters,&img_fol, &raw_cp, indexfilename) == 1) {
                return 1;
        }
-       
+
        if (parameters.cp_cinema){
                img_fol.rates = (float*)malloc(parameters.tcp_numlayers * sizeof(float));
                for(i=0; i< parameters.tcp_numlayers; i++){
                        img_fol.rates[i] = parameters.tcp_rates[i];
                }
                cinema_parameters(&parameters);
-       }                               
+       }
 
        /* Create comment for codestream */
        if(parameters.cp_comment == NULL) {
@@ -1564,7 +1565,7 @@ int main(int argc, char **argv) {
        for(imageno=0;imageno<num_images;imageno++)     {
                image = NULL;
                fprintf(stderr,"\n");
-               
+
                if(img_fol.set_imgdir==1){
                        if (get_next_file(imageno, dirptr,&img_fol, &parameters)) {
                                fprintf(stderr,"skipping file...\n");
@@ -1584,9 +1585,11 @@ int main(int argc, char **argv) {
                                break;
                        case TGA_DFMT:
                                break;
+                       case PNG_DFMT:
+                               break;
                        default:
                                fprintf(stderr,"skipping file...\n");
-                               continue;                       
+                               continue;
                }
 
                        /* decode the source image */
@@ -1597,7 +1600,7 @@ int main(int argc, char **argv) {
                                        image = pgxtoimage(parameters.infile, &parameters);
                                        if (!image) {
                                                fprintf(stderr, "Unable to load pgx file\n");
-                                               return 1; 
+                                               return 1;
                                        }
                                        break;
 
@@ -1616,7 +1619,7 @@ int main(int argc, char **argv) {
                                                return 1;
                                        }
                                        break;
-                       
+
                                case TIF_DFMT:
                                        image = tiftoimage(parameters.infile, &parameters);
                                        if (!image) {
@@ -1640,6 +1643,14 @@ int main(int argc, char **argv) {
                                                return 1;
                                        }
                                break;
+
+                               case PNG_DFMT:
+                                       image = pngtoimage(parameters.infile, &parameters);
+                                       if (!image) {
+                                               fprintf(stderr, "Unable to load png file\n");
+                                               return 1;
+                                       }
+                                       break;
                }
                        /* Decide if MCT should be used */
                        parameters.tcp_mct = image->numcomps == 3 ? 1 : 0;
@@ -1715,7 +1726,7 @@ int main(int argc, char **argv) {
                                opj_cinfo_t* cinfo = opj_create_compress(CODEC_JP2);
 
                                /* catch events using our callbacks and give a local context */
-                               opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);                   
+                               opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);
 
                                /* setup the encoder parameters using the current image and using user parameters */
                                opj_setup_encoder(cinfo, &parameters, image);
@@ -1747,7 +1758,7 @@ int main(int argc, char **argv) {
                                fprintf(stderr,"Generated outfile %s\n",parameters.outfile);
                                /* close and free the byte stream */
                                opj_cio_close(cio);
-                               
+
                                /* Write the index to disk */
                                if (*indexfilename) {
                                        bSuccess = write_index_file(&cstr_info, indexfilename);
@@ -1761,7 +1772,7 @@ int main(int argc, char **argv) {
                                if (*indexfilename)
                                        opj_destroy_cstr_info(&cstr_info);
                        }
-       
+
                        /* free image data */
                        opj_image_destroy(image);
        }
index d93c417b8ab0b0fc6db1d5cf3e96f5f00c64a83c..32213151a03ac085a5d0479cd3b61d5e23b57cfc 100644 (file)
@@ -58,7 +58,7 @@
 #define TIF_DFMT 14
 #define RAW_DFMT 15
 #define TGA_DFMT 16
-
+#define PNG_DFMT 17
 /* ----------------------------------------------------------------------- */
 
 typedef struct dircnt{
@@ -188,8 +188,8 @@ int load_images(dircnt_t *dirptr, char *imgdirpath){
 
 int get_file_format(char *filename) {
        unsigned int i;
-       static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "j2k", "jp2", "jpt", "j2c" };
-       static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT };
+       static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c" };
+       static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT };
        char * ext = strrchr(filename, '.');
        if (ext == NULL)
                return -1;
@@ -286,6 +286,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                                        case TIF_DFMT:
                                        case RAW_DFMT:
                                        case TGA_DFMT:
+                                       case PNG_DFMT:
                                                break;
                                        default:
                                                fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outfile);
@@ -323,6 +324,9 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                                        case TGA_DFMT:
                                                img_fol->out_format = "raw";
                                                break;
+                                       case PNG_DFMT:
+                                               img_fol->out_format = "png";
+                                               break;
                                        default:
                                                fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outformat);
                                                return 1;
@@ -786,6 +790,15 @@ int main(int argc, char **argv) {
                                fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile);
                        }
                        break;
+
+               case PNG_DFMT:                  /* PNG */
+                       if(imagetopng(image, parameters.outfile)){
+                               fprintf(stdout,"Error generating png file. Outfile %s not generated\n",parameters.outfile);
+                       }
+                       else {
+                               fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile);
+                       }
+                       break;
                }
 
                /* free remaining structures */