- {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
- uint16* sampleinfo;
- uint16 extrasamples;
-
- TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
- &extrasamples, &sampleinfo);
-
- if(extrasamples >= 1)
- {
- switch(sampleinfo[0])
- {
- case EXTRASAMPLE_UNSPECIFIED:
-/* Workaround for some images without correct info about alpha channel
-*/
- if(tiSpp > 3)
- has_alpha = 1;
- break;
-
- case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */
- case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */
- has_alpha = 1;
- break;
- }
- }
- else /* extrasamples == 0 */
- if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
- }
-
-/* initialize image components
-*/
- memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
-
- if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
- {
- numcomps = 3 + has_alpha;
- color_space = CLRSPC_SRGB;
-
-/*#define USETILEMODE*/
- for(j = 0; j < numcomps; j++)
- {
- if(parameters->cp_cinema)
- {
- cmptparm[j].prec = 12;
- cmptparm[j].bpp = 12;
- }
- else
- {
- cmptparm[j].prec = tiBps;
- cmptparm[j].bpp = tiBps;
- }
- cmptparm[j].dx = subsampling_dx;
- cmptparm[j].dy = subsampling_dy;
- cmptparm[j].w = w;
- cmptparm[j].h = h;
-#ifdef USETILEMODE
- cmptparm[j].x0 = 0;
- cmptparm[j].y0 = 0;
-#endif
- }
-
-#ifdef USETILEMODE
- image = opj_image_tile_create(numcomps,&cmptparm[0],color_space);
-#else
- image = opj_image_create(numcomps, &cmptparm[0], color_space);
-#endif
-
- if(!image)
- {
- TIFFClose(tif);
- return NULL;
- }
-/* set image offset and reference grid
-*/
- image->x0 = parameters->image_offset_x0;
- image->y0 = parameters->image_offset_y0;
- image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
- image->x0 + (w - 1) * subsampling_dx + 1;
- image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
- image->y0 + (h - 1) * subsampling_dy + 1;
-
- buf = _TIFFmalloc(TIFFStripSize(tif));
-
- strip_size=TIFFStripSize(tif);
- index = 0;
- imgsize = image->comps[0].w * image->comps[0].h ;
-/* Read the Image components
-*/
- for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
- {
- unsigned char *dat8;
- int step;
- tsize_t i, ssize;
- ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
- dat8 = (unsigned char*)buf;
-
- if(tiBps == 16)
- {
- step = 6 + has_alpha + has_alpha;
-
- for(i = 0; i < ssize; i += step)
- {
- if(index < imgsize)
- {
- image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; /* R */
- image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */
- image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */
- if(has_alpha)
- image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
-
- if(parameters->cp_cinema)
- {
-/* Rounding 16 to 12 bits
-*/
- image->comps[0].data[index] =
- (image->comps[0].data[index] + 0x08) >> 4 ;
- image->comps[1].data[index] =
- (image->comps[1].data[index] + 0x08) >> 4 ;
- image->comps[2].data[index] =
- (image->comps[2].data[index] + 0x08) >> 4 ;
- if(has_alpha)
- image->comps[3].data[index] =
- (image->comps[3].data[index] + 0x08) >> 4 ;
- }
- index++;
- }
- else
- break;
- }/*for(i = 0)*/
- }/*if(tiBps == 16)*/
- else
- if(tiBps == 8)
- {
- step = 3 + has_alpha;
-
- for(i = 0; i < ssize; i += step)
- {
- if(index < imgsize)
- {
-#ifndef USETILEMODE
- image->comps[0].data[index] = dat8[i+0];/* R */
- image->comps[1].data[index] = dat8[i+1];/* G */
- image->comps[2].data[index] = dat8[i+2];/* B */
- if(has_alpha)
- image->comps[3].data[index] = dat8[i+3];
-#endif
-
- if(parameters->cp_cinema)
- {
-/* Rounding 8 to 12 bits
-*/
-#ifndef USETILEMODE
- image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
- image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
- image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
- if(has_alpha)
- image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
-#endif
- }
- index++;
- }/*if(index*/
- else
- break;
- }/*for(i )*/
- }/*if( tiBps == 8)*/
- else
- if(tiBps == 12)/* CINEMA file */
- {
- step = 9;
-
- for(i = 0; i < ssize; i += step)
- {
- if((index < imgsize)&(index+1 < imgsize))
- {
- image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
- image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
-
- image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
- image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
-
- image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
- image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
-
- index += 2;
- }
- else
- break;
- }/*for(i )*/
- }
- }/*for(strip = 0; )*/
-
- _TIFFfree(buf);
- TIFFClose(tif);
-
- return image;
- }/*RGB(A)*/
-
- if(tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
- {
- numcomps = 1 + has_alpha;
- color_space = CLRSPC_GRAY;
-
- for(j = 0; j < numcomps; ++j)
- {
- cmptparm[j].prec = tiBps;
- cmptparm[j].bpp = tiBps;
- cmptparm[j].dx = subsampling_dx;
- cmptparm[j].dy = subsampling_dy;
- cmptparm[j].w = w;
- cmptparm[j].h = h;
- }
-#ifdef USETILEMODE
- image = opj_image_tile_create(numcomps,&cmptparm[0],color_space);
-#else
- image = opj_image_create(numcomps, &cmptparm[0], color_space);
-#endif
-
- if(!image)
- {
- TIFFClose(tif);
- return NULL;
- }
-/* set image offset and reference grid
-*/
- image->x0 = parameters->image_offset_x0;
- image->y0 = parameters->image_offset_y0;
- image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 :
- image->x0 + (w - 1) * subsampling_dx + 1;
- image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 :
- image->y0 + (h - 1) * subsampling_dy + 1;
-
- buf = _TIFFmalloc(TIFFStripSize(tif));
-
- strip_size = TIFFStripSize(tif);
- index = 0;
- imgsize = image->comps[0].w * image->comps[0].h ;
-/* Read the Image components
-*/
- for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++)
- {
- unsigned char *dat8;
- tsize_t i, ssize;
- int step;
-
- ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
- dat8 = (unsigned char*)buf;
-
- if(tiBps == 16)
- {
- step = 2 + has_alpha + has_alpha;
-
- for(i = 0; i < ssize; i += step)
- {
- if(index < imgsize)
- {
- image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
- if(has_alpha)
- image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2];
- index++;
- }
- else
- break;
- }/*for(i )*/
- }
- else
- if(tiBps == 8)
- {
- step = 1 + has_alpha;
-
- for(i = 0; i < ssize; i += step)
- {
- if(index < imgsize)
- {
- image->comps[0].data[index] = dat8[i+0];
- if(has_alpha)
- image->comps[1].data[index] = dat8[i+1];
- index++;
- }
- else
- break;
- }/*for(i )*/
- }
- }/*for(strip = 0;*/
-
- _TIFFfree(buf);
- TIFFClose(tif);
-
- }/*GRAY(A)*/
-
- return image;
-
-}/* tiftoimage() */
-
-#endif /* HAVE_LIBTIFF */
-
-/* -->> -->> -->> -->>
-
- RAW IMAGE FORMAT
-
- <<-- <<-- <<-- <<-- */
-static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, opj_bool big_endian) {
- int subsampling_dx = parameters->subsampling_dx;
- int subsampling_dy = parameters->subsampling_dy;
-
- FILE *f = NULL;
- int i, compno, numcomps, w, h;
- OPJ_COLOR_SPACE color_space;
- opj_image_cmptparm_t *cmptparm;
- opj_image_t * image = NULL;
- unsigned short ch;
-
- if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
- {
- fprintf(stderr,"\nError: invalid raw image parameters\n");
- fprintf(stderr,"Please use the Format option -F:\n");
- 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");
- return NULL;
- }
-
- f = fopen(filename, "rb");
- if (!f) {
- fprintf(stderr, "Failed to open %s for reading !!\n", filename);
- fprintf(stderr,"Aborting\n");
- return NULL;
- }
- numcomps = raw_cp->rawComp;
- color_space = CLRSPC_SRGB;
- w = raw_cp->rawWidth;
- h = raw_cp->rawHeight;
- cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
-
- /* initialize image components */
- memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
- for(i = 0; i < numcomps; i++) {
- cmptparm[i].prec = raw_cp->rawBitDepth;
- cmptparm[i].bpp = raw_cp->rawBitDepth;
- cmptparm[i].sgnd = raw_cp->rawSigned;
- cmptparm[i].dx = subsampling_dx;
- cmptparm[i].dy = subsampling_dy;
- cmptparm[i].w = w;
- cmptparm[i].h = h;
- }
- /* create the image */
- image = opj_image_create(numcomps, &cmptparm[0], color_space);
- if(!image) {
- fclose(f);
- return NULL;
- }
- /* set image offset and reference grid */
- image->x0 = parameters->image_offset_x0;
- image->y0 = parameters->image_offset_y0;
- image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
- image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
-
- if(raw_cp->rawBitDepth <= 8)
- {
- unsigned char value = 0;
- for(compno = 0; compno < numcomps; compno++) {
- for (i = 0; i < w * h; i++) {
- if (!fread(&value, 1, 1, f)) {
- fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
- return NULL;
- }
- image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
- }
- }
- }
- else if(raw_cp->rawBitDepth <= 16)
- {
- unsigned short value;
- for(compno = 0; compno < numcomps; compno++) {
- for (i = 0; i < w * h; i++) {
- unsigned char temp1;
- unsigned char temp2;
- if (!fread(&temp1, 1, 1, f)) {
- fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
- return NULL;
- }
- if (!fread(&temp2, 1, 1, f)) {
- fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
- return NULL;
- }
- if( big_endian )
- {
- value = temp1 << 8;
- value += temp2;
- }
- else
- {
- value = temp2 << 8;
- value += temp1;
- }
- image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
- }
- }
- }
- else {
- fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
- return NULL;
- }
-
- if (fread(&ch, 1, 1, f)) {
- fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
- }
- fclose(f);
-
- return image;
-}
-
-opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
- return rawtoimage_common(filename, parameters, raw_cp, OPJ_FALSE);
-}
-
-opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
- return rawtoimage_common(filename, parameters, raw_cp, OPJ_TRUE);
-}
-
-static int imagetoraw_common(opj_image_t * image, const char *outfile, opj_bool big_endian)
-{
- FILE *rawFile = NULL;
- size_t res;
- int compno;
- int w, h;
- int line, row;
- int *ptr;
-
- if((image->numcomps * image->x1 * image->y1) == 0)
- {
- fprintf(stderr,"\nError: invalid raw image parameters\n");
- return 1;
- }
-
- rawFile = fopen(outfile, "wb");
- if (!rawFile) {
- fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
- return 1;
- }
-
- fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
-
- for(compno = 0; compno < image->numcomps; compno++)
- {
- fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
- image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
-
- w = image->comps[compno].w;
- h = image->comps[compno].h;
-
- if(image->comps[compno].prec <= 8)
- {
- if(image->comps[compno].sgnd == 1)
- {
- signed char curr;
- int mask = (1 << image->comps[compno].prec) - 1;
- ptr = image->comps[compno].data;
- for (line = 0; line < h; line++) {
- for(row = 0; row < w; row++) {
- curr = (signed char) (*ptr & mask);
- res = fwrite(&curr, sizeof(signed char), 1, rawFile);
- if( res < 1 ) {
- fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
- return 1;
- }
- ptr++;
- }
- }
- }
- else if(image->comps[compno].sgnd == 0)
- {
- unsigned char curr;
- int mask = (1 << image->comps[compno].prec) - 1;
- ptr = image->comps[compno].data;
- for (line = 0; line < h; line++) {
- for(row = 0; row < w; row++) {
- curr = (unsigned char) (*ptr & mask);
- res = fwrite(&curr, sizeof(unsigned char), 1, rawFile);
- if( res < 1 ) {
- fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
- return 1;
- }
- ptr++;
- }
- }
- }
- }
- else if(image->comps[compno].prec <= 16)
- {
- if(image->comps[compno].sgnd == 1)
- {
- signed short int curr;
- int mask = (1 << image->comps[compno].prec) - 1;
- ptr = image->comps[compno].data;
- for (line = 0; line < h; line++) {
- for(row = 0; row < w; row++) {
- unsigned char temp1;
- unsigned char temp2;
- curr = (signed short int) (*ptr & mask);
- if( big_endian )
- {
- temp1 = (unsigned char) (curr >> 8);
- temp2 = (unsigned char) curr;
- }
- else
- {
- temp2 = (unsigned char) (curr >> 8);
- temp1 = (unsigned char) curr;
- }
- res = fwrite(&temp1, 1, 1, rawFile);
- if( res < 1 ) {
- fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
- return 1;
- }
- res = fwrite(&temp2, 1, 1, rawFile);
- if( res < 1 ) {
- fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
- return 1;
- }
- ptr++;
- }
- }
- }
- else if(image->comps[compno].sgnd == 0)
- {
- unsigned short int curr;
- int mask = (1 << image->comps[compno].prec) - 1;
- ptr = image->comps[compno].data;
- for (line = 0; line < h; line++) {
- for(row = 0; row < w; row++) {
- unsigned char temp1;
- unsigned char temp2;
- curr = (unsigned short int) (*ptr & mask);
- if( big_endian )
- {
- temp1 = (unsigned char) (curr >> 8);
- temp2 = (unsigned char) curr;
- }
- else
- {
- temp2 = (unsigned char) (curr >> 8);
- temp1 = (unsigned char) curr;
- }
- res = fwrite(&temp1, 1, 1, rawFile);
- if( res < 1 ) {
- fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
- return 1;
- }
- res = fwrite(&temp2, 1, 1, rawFile);
- if( res < 1 ) {
- fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
- return 1;
- }
- ptr++;
- }
- }
- }
- }
- else if (image->comps[compno].prec <= 32)
- {
- fprintf(stderr,"More than 16 bits per component no handled yet\n");
- return 1;
- }
- else
- {
- fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
- return 1;
- }
- }
- fclose(rawFile);
- return 0;
-}
-
-int imagetoraw(opj_image_t * image, const char *outfile)
-{
- return imagetoraw_common(image, outfile, OPJ_TRUE);
-}
-
-int imagetorawl(opj_image_t * image, const char *outfile)
-{
- return imagetoraw_common(image, outfile, OPJ_FALSE);
-}
-
-#ifdef HAVE_LIBPNG
-
-#define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
-#define MAGIC_SIZE 8
-/* PNG allows bits per sample: 1, 2, 4, 8, 16 */
-
-opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
-{
- png_structp png;
- png_infop info;
- double gamma, display_exponent;
- int bit_depth, interlace_type,compression_type, filter_type;
- int unit;
- png_uint_32 resx, resy;
- unsigned int i, j;
- png_uint_32 width, height;
- int color_type, has_alpha, is16;
- unsigned char *s;
- FILE *reader;
- unsigned char **rows;
-/* 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;
- unsigned char sigbuf[8];
-
- if((reader = fopen(read_idf, "rb")) == NULL)
- {
- fprintf(stderr,"pngtoimage: can not open %s\n",read_idf);
- return NULL;
- }
- image = NULL; png = NULL; rows = NULL;
-
- if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
- || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0)
- {
- fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf);
- goto fin;
- }
-/* 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_set_sig_bytes(png, MAGIC_SIZE);
-
- png_read_info(png, info);
-
- if(png_get_IHDR(png, info, &width, &height,
- &bit_depth, &color_type, &interlace_type,
- &compression_type, &filter_type) == 0)
- goto fin;
-
-/* png_set_expand():
- * expand paletted images to RGB, expand grayscale images of
- * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
- * to alpha channels.
-*/
- 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);
-
- is16 = (bit_depth == 16);
-
-/* 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);
-
- 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);
-
- nr_comp = 3 + has_alpha;
-
- bit_depth = png_get_bit_depth(png, info);
-
- rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*));
- for(i = 0; i < height; ++i)
- rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info));
-
- png_read_image(png, rows);
-
- 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 = bit_depth;
-/* bits_per_pixel: 8 or 16 */
- cmptparm[i].bpp = bit_depth;
- 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 = image->x0 + (width - 1) * sub_dx + 1 + image->x0;
- image->y1 = image->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;
-
- for(i = 0; i < height; ++i)
- {
- s = rows[i];
-
- for(j = 0; j < width; ++j)
- {
- if(is16)
- {
- *r++ = s[0]<<8|s[1]; s += 2;
-
- *g++ = s[0]<<8|s[1]; s += 2;
-
- *b++ = s[0]<<8|s[1]; s += 2;
-
- if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; }
-
- continue;
- }
- *r++ = *s++; *g++ = *s++; *b++ = *s++;
-
- if(has_alpha) *a++ = *s++;
- }
- }
-fin:
- if(rows)
- {
- for(i = 0; i < height; ++i)
- free(rows[i]);
- free(rows);
- }
- if(png)
- png_destroy_read_struct(&png, &info, NULL);
-
- fclose(reader);
-
- return image;
-
-}/* pngtoimage() */
-
-int imagetopng(opj_image_t * image, const char *write_idf)
-{
- FILE *writer;
- png_structp png;
- png_infop info;
- int *red, *green, *blue, *alpha;
- unsigned char *row_buf, *d;
- int has_alpha, width, height, nr_comp, color_type;
- int adjustR, adjustG, adjustB, adjustA, x, y, fails;
- int prec, ushift, dshift, is16, force16, force8;
- unsigned short mask = 0xffff;
- png_color_8 sig_bit;
-
- is16 = force16 = force8 = ushift = dshift = 0; fails = 1;
- prec = image->comps[0].prec;
- nr_comp = image->numcomps;
-
- if(prec > 8 && prec < 16)
- {
- ushift = 16 - prec; dshift = prec - ushift;
- prec = 16; force16 = 1;
- }
- else
- if(prec < 8 && nr_comp > 1)/* GRAY_ALPHA, RGB, RGB_ALPHA */
- {
- ushift = 8 - prec; dshift = 8 - ushift;
- prec = 8; force8 = 1;
- }
-
- if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16)
- {
- fprintf(stderr,"imagetopng: can not create %s"
- "\n\twrong bit_depth %d\n", write_idf, prec);
- return fails;
- }
- writer = fopen(write_idf, "wb");
-
- if(writer == NULL) return fails;
-
- info = NULL; has_alpha = 0;
-
-/* 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 = png_create_write_struct(PNG_LIBPNG_VER_STRING,
- NULL, NULL, NULL);
-/*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
-
- if(png == NULL) goto fin;
-
-/* Allocate/initialize the image information data. REQUIRED
-*/
- info = png_create_info_struct(png);
-
- if(info == 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))) goto fin;
-
-/* I/O initialization functions is REQUIRED
-*/
- png_init_io(png, 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
- *
- * ERRORS:
- *
- * color_type == PNG_COLOR_TYPE_PALETTE && bit_depth > 8
- * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8
- * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8
- * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8
- *
-*/
- png_set_compression_level(png, Z_BEST_COMPRESSION);
-
- if(prec == 16) mask = 0xffff;
- else
- if(prec == 8) mask = 0x00ff;
- else
- if(prec == 4) mask = 0x000f;
- else
- if(prec == 2) mask = 0x0003;
- else
- if(prec == 1) mask = 0x0001;
-
- if(nr_comp >= 3
- && image->comps[0].dx == image->comps[1].dx
- && image->comps[1].dx == image->comps[2].dx
- && image->comps[0].dy == image->comps[1].dy
- && image->comps[1].dy == image->comps[2].dy
- && image->comps[0].prec == image->comps[1].prec
- && image->comps[1].prec == image->comps[2].prec)
- {
- int v;
-
- has_alpha = (nr_comp > 3);
-
- is16 = (prec == 16);
-
- width = image->comps[0].w;
- height = image->comps[0].h;
-
- red = image->comps[0].data;
- green = image->comps[1].data;
- blue = image->comps[2].data;
-
- sig_bit.red = sig_bit.green = sig_bit.blue = prec;
-
- if(has_alpha)
- {
- sig_bit.alpha = prec;
- alpha = image->comps[3].data;
- color_type = PNG_COLOR_TYPE_RGB_ALPHA;
- adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0);
- }
- else
- {
- sig_bit.alpha = 0; alpha = NULL;
- color_type = PNG_COLOR_TYPE_RGB;
- adjustA = 0;
- }
- png_set_sBIT(png, info, &sig_bit);
-
- png_set_IHDR(png, info, width, height, prec,
- color_type,
- PNG_INTERLACE_NONE,
- PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
-/*=============================*/
- png_write_info(png, info);
-/*=============================*/
- if(prec < 8)
- {
- png_set_packing(png);
- }
- adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
- adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
- adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
-
- row_buf = (unsigned char*)malloc(width * nr_comp * 2);
-
- for(y = 0; y < height; ++y)
- {
- d = row_buf;
-
- for(x = 0; x < width; ++x)
- {
- if(is16)
- {
- v = *red + adjustR; ++red;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
-
- v = *green + adjustG; ++green;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
-
- v = *blue + adjustB; ++blue;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
-
- if(has_alpha)
- {
- v = *alpha + adjustA; ++alpha;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
- }
- continue;
- }/* if(is16) */
-
- v = *red + adjustR; ++red;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
-
- v = *green + adjustG; ++green;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
-
- v = *blue + adjustB; ++blue;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
-
- if(has_alpha)
- {
- v = *alpha + adjustA; ++alpha;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
- }
- } /* for(x) */
-
- png_write_row(png, row_buf);
-
- } /* for(y) */
- free(row_buf);
-
- }/* nr_comp >= 3 */
- else
- if(nr_comp == 1 /* GRAY */
- || ( nr_comp == 2 /* GRAY_ALPHA */
- && image->comps[0].dx == image->comps[1].dx
- && image->comps[0].dy == image->comps[1].dy
- && image->comps[0].prec == image->comps[1].prec))
- {
- int v;
-
- red = image->comps[0].data;
-
- sig_bit.gray = prec;
- sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0;
- alpha = NULL; adjustA = 0;
- color_type = PNG_COLOR_TYPE_GRAY;
-
- if(nr_comp == 2)
- {
- has_alpha = 1; sig_bit.alpha = prec;
- alpha = image->comps[1].data;
- color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
- adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
- }
- width = image->comps[0].w;
- height = image->comps[0].h;
-
- png_set_IHDR(png, info, width, height, sig_bit.gray,
- color_type,
- PNG_INTERLACE_NONE,
- PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
-
- png_set_sBIT(png, info, &sig_bit);
-/*=============================*/
- png_write_info(png, info);
-/*=============================*/
- adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
-
- if(prec < 8)
- {
- png_set_packing(png);
- }
-
- if(prec > 8)
- {
- row_buf = (unsigned char*)
- malloc(width * nr_comp * sizeof(unsigned short));
-
- for(y = 0; y < height; ++y)
- {
- d = row_buf;
-
- for(x = 0; x < width; ++x)
- {
- v = *red + adjustR; ++red;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
-
- if(has_alpha)
- {
- v = *alpha++;
-
- if(force16) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
- }
- }/* for(x) */
- png_write_row(png, row_buf);
-
- } /* for(y) */
- free(row_buf);
- }
- else /* prec <= 8 */
- {
- row_buf = (unsigned char*)calloc(width, nr_comp * 2);
-
- for(y = 0; y < height; ++y)
- {
- d = row_buf;
-
- for(x = 0; x < width; ++x)
- {
- v = *red + adjustR; ++red;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
-
- if(has_alpha)
- {
- v = *alpha + adjustA; ++alpha;
-
- if(force8) { v = (v<<ushift) + (v>>dshift); }
-
- *d++ = (unsigned char)(v & mask);
- }
- }/* for(x) */
-
- png_write_row(png, row_buf);
-
- } /* for(y) */
- free(row_buf);
- }
- }
- else
- {
- fprintf(stderr,"imagetopng: can not create %s\n",write_idf);
- goto fin;
- }
- png_write_end(png, info);
-
- fails = 0;
-
-fin:
-
- if(png)
- {
- png_destroy_write_struct(&png, &info);
- }
- fclose(writer);
-
- if(fails) remove(write_idf);
-
- return fails;
-}/* imagetopng() */
-#endif /* HAVE_LIBPNG */