void* OPJSupport::decompressJPEG2KWithBuffer(void* inputBuffer, void* jp2Data, long jp2DataSize, long *decompressedBufferSize, int *colorModel) { //decoderMutex.lock(); opj_dparameters_t parameters; int i; int width, height; OPJ_BOOL hasAlpha, fails = OPJ_FALSE; OPJ_CODEC_FORMAT codec_format; unsigned char rc, gc, bc, ac; if (jp2DataSize<12) { //decoderMutex.unlock(); return 0; } /*-----------------------------------------------*/ decode_info_t decodeInfo; memset(&decodeInfo, 0, sizeof(decode_info_t)); opj_set_default_decoder_parameters(¶meters); parameters.decod_format = buffer_format(jp2Data); // Create the stream opj_buffer_info_t bufferInfo; bufferInfo.cur = bufferInfo.buf = (OPJ_BYTE *)jp2Data; bufferInfo.len = (OPJ_SIZE_T) jp2DataSize; // Create the stream decodeInfo.stream = opj_stream_create_buffer_stream(&bufferInfo , OPJ_STREAM_READ); //GROK //decodeInfo.stream = opj_stream_create_buffer_stream((OPJ_BYTE *)jp2Data, (OPJ_SIZE_T) jp2DataSize , OPJ_STREAM_READ); if (!decodeInfo.stream) { fprintf(stderr,"%s:%d:\n\tNO decodeInfo.stream\n",__FILE__,__LINE__); //decoderMutex.unlock(); return NULL; } /*-----------------------------------------------*/ switch (parameters.decod_format) { case J2K_CFMT: /* JPEG-2000 codestream */ codec_format = OPJ_CODEC_J2K; break; case JP2_CFMT: /* JPEG 2000 compressed image data */ codec_format = OPJ_CODEC_JP2; break; case JPT_CFMT: /* JPEG 2000, JPIP */ codec_format = OPJ_CODEC_JPT; break; case -1: default: release(&decodeInfo); fprintf(stderr,"%s:%d: decode format missing\n",__FILE__,__LINE__); //decoderMutex.unlock(); return NULL; } /*-----------------------------------------------*/ while(1) { int user_changed_tile=0, user_changed_reduction=0; int max_tiles=0, max_reduction=0; fails = OPJ_TRUE; decodeInfo.codec = opj_create_decompress(codec_format); if (decodeInfo.codec == NULL) { fprintf(stderr,"%s:%d:\n\tNO codec\n",__FILE__,__LINE__); break; } #ifdef OPJ_VERBOSE opj_set_info_handler(decodeInfo.codec, info_callback, this); opj_set_warning_handler(decodeInfo.codec, warning_callback, this); #endif opj_set_error_handler(decodeInfo.codec, error_callback, this); // Setup the decoder decoding parameters if ( !opj_setup_decoder(decodeInfo.codec, ¶meters)) { fprintf(stderr,"%s:%d:\n\topj_setup_decoder failed\n",__FILE__,__LINE__); break; } if (user_changed_tile && user_changed_reduction) { int reduction=0; opj_set_decoded_resolution_factor(decodeInfo.codec, reduction); } /* Read the main header of the codestream and if necessary the JP2 boxes * see openjpeg.c * For OPJ_CODEC_JP2 it will call 'opj_jp2_read_header()' in jp2.c:2276 * then call 'opj_j2k_read_header()' */ if( !opj_read_header(decodeInfo.stream, decodeInfo.codec, &(decodeInfo.image))) { fprintf(stderr,"%s:%d:\n\topj_read_header failed\n",__FILE__,__LINE__); break; } if ( !(user_changed_tile && user_changed_reduction) || (max_tiles <= 0) || (max_reduction <= 0) ) { decodeInfo.cstr_info = opj_get_cstr_info(decodeInfo.codec); max_reduction = decodeInfo.cstr_info->m_default_tile_info.tccp_info->numresolutions; max_tiles = decodeInfo.cstr_info->tw * decodeInfo.cstr_info->th; decodeInfo.cstr_index = opj_get_cstr_index(decodeInfo.codec); } if (!parameters.nb_tile_to_decode) { int user_changed_area=0; if(user_changed_area) { } /* Optional if you want decode the entire image */ if (!opj_set_decode_area(decodeInfo.codec, decodeInfo.image, (OPJ_INT32)parameters.DA_x0, (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)) { fprintf(stderr,"%s:%d:\n\topj_set_decode_area failed\n",__FILE__,__LINE__); break; } /* Get the decoded image */ if (!opj_decode(decodeInfo.codec, decodeInfo.stream, decodeInfo.image)) { fprintf(stderr,"%s:%d:\n\topj_decode failed\n",__FILE__,__LINE__); //decoderMutex.unlock(); return NULL; } if (!opj_end_decompress(decodeInfo.codec, decodeInfo.stream)) { fprintf(stderr,"%s:%d:\n\topj_end_decompress failed\n",__FILE__,__LINE__); break; } } else { if (!opj_get_decoded_tile(decodeInfo.codec, decodeInfo.stream, decodeInfo.image, parameters.tile_index)) { fprintf(stderr,"%s:%d:\n\topj_get_decoded_tile failed\n",__FILE__,__LINE__); break; } } fails = OPJ_FALSE; break; } // while decodeInfo.deleteImage = fails; if (fails) { //decoderMutex.unlock(); return NULL; } decodeInfo.deleteImage = OPJ_TRUE; if(decodeInfo.image->color_space == OPJ_CLRSPC_SYCC) { //disable for now //color_sycc_to_rgb(decodeInfo.image); } if (decodeInfo.image->color_space != OPJ_CLRSPC_SYCC && decodeInfo.image->numcomps == 3 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[0].dy && decodeInfo.image->comps[1].dx != 1) { decodeInfo.image->color_space = OPJ_CLRSPC_SYCC; } else if(decodeInfo.image->numcomps <= 2) { decodeInfo.image->color_space = OPJ_CLRSPC_GRAY; } if (decodeInfo.image->icc_profile_buf) { #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) color_apply_icc_profile(decodeInfo.image); #endif free(decodeInfo.image->icc_profile_buf); decodeInfo.image->icc_profile_buf = NULL; decodeInfo.image->icc_profile_len = 0; } //--- width = decodeInfo.image->comps[0].w; height = decodeInfo.image->comps[0].h; long depth = (decodeInfo.image->comps[0].prec + 7)/8; long decompressSize = width * height * decodeInfo.image->numcomps * depth; if (decompressedBufferSize) *decompressedBufferSize = decompressSize;; if (!inputBuffer ) { inputBuffer = malloc(decompressSize); } if (colorModel) *colorModel = 0; if ((decodeInfo.image->numcomps >= 3 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[1].dx && decodeInfo.image->comps[1].dx == decodeInfo.image->comps[2].dx && decodeInfo.image->comps[0].dy == decodeInfo.image->comps[1].dy && decodeInfo.image->comps[1].dy == decodeInfo.image->comps[2].dy && decodeInfo.image->comps[0].prec == decodeInfo.image->comps[1].prec && decodeInfo.image->comps[1].prec == decodeInfo.image->comps[2].prec )/* RGB[A] */ || (decodeInfo.image->numcomps == 2 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[1].dx && decodeInfo.image->comps[0].dy == decodeInfo.image->comps[1].dy && decodeInfo.image->comps[0].prec == decodeInfo.image->comps[1].prec ) ) /* GA */ { int has_alpha4, has_alpha2, has_rgb; int *red, *green, *blue, *alpha; if (colorModel) *colorModel = 1; alpha = NULL; has_rgb = (decodeInfo.image->numcomps == 3); has_alpha4 = (decodeInfo.image->numcomps == 4); has_alpha2 = (decodeInfo.image->numcomps == 2); hasAlpha = (has_alpha4 || has_alpha2); if(has_rgb) { red = decodeInfo.image->comps[0].data; green = decodeInfo.image->comps[1].data; blue = decodeInfo.image->comps[2].data; if(has_alpha4) { alpha = decodeInfo.image->comps[3].data; } } /* if(has_rgb) */ else { red = green = blue = decodeInfo.image->comps[0].data; if(has_alpha2) { alpha = decodeInfo.image->comps[1].data; } } /* if(has_rgb) */ ac = 255;/* 255: FULLY_OPAQUE; 0: FULLY_TRANSPARENT */ unsigned char* ptrIBody = (unsigned char*)inputBuffer; for(i = 0; i < width*height; i++) { rc = (unsigned char)*red++; gc = (unsigned char)*green++; bc = (unsigned char)*blue++; if(hasAlpha) { ac = (unsigned char)*alpha++;; } /* A R G B */ //*ptrIBody++ = (int)((ac<<24) | (rc<<16) | (gc<<8) | bc); *ptrIBody = rc; ptrIBody++; *ptrIBody = gc; ptrIBody++; *ptrIBody = bc; ptrIBody++; if (hasAlpha) { *ptrIBody = ac; ptrIBody++; } } /* for(i) */ }/* if (decodeInfo.image->numcomps >= 3 */ else if(decodeInfo.image->numcomps == 1) /* Grey */ { /* 1 component 8 or 16 bpp decodeInfo.image */ int *grey = decodeInfo.image->comps[0].data; if(decodeInfo.image->comps[0].prec <= 8) { char* ptrBBody = (char*)inputBuffer; for(i=0; i<width*height; i++) { *ptrBBody++ = *grey++; } /* Replace image8 buffer: */ } else /* prec[9:16] */ { int *grey = decodeInfo.image->comps[0].data; //int ushift = 0, dshift = 0, force16 = 0; short* ptrSBody = (short*)inputBuffer; for(i=0; i<width*height; i++) { //disable shift up for signed data: don't know why we are doing this *ptrSBody++ = *grey++; } /* Replace image16 buffer: */ } } else { int *grey; fprintf(stderr,"%s:%d:Can show only first component of decodeInfo.image\n" " components(%d) prec(%d) color_space[%d](%s)\n" " RECT(%d,%d,%d,%d)\n",__FILE__,__LINE__,decodeInfo.image->numcomps, decodeInfo.image->comps[0].prec, decodeInfo.image->color_space,clr_space(decodeInfo.image->color_space), decodeInfo.image->x0,decodeInfo.image->y0,decodeInfo.image->x1,decodeInfo.image->y1 ); for(i = 0; i < decodeInfo.image->numcomps; ++i) { fprintf(stderr,"[%d]dx(%d) dy(%d) w(%d) h(%d) signed(%u)\n",i, decodeInfo.image->comps[i].dx ,decodeInfo.image->comps[i].dy, decodeInfo.image->comps[i].w,decodeInfo.image->comps[i].h, decodeInfo.image->comps[i].sgnd); } /* 1 component 8 or 16 bpp decodeInfo.image */ grey = decodeInfo.image->comps[0].data; if(decodeInfo.image->comps[0].prec <= 8) { char* ptrBBody = (char*)inputBuffer; for(i=0; i<width*height; i++) { *ptrBBody++ = *grey++; } /* Replace image8 buffer: */ } else /* prec[9:16] */ { int *grey; //int ushift = 0, dshift = 0, force16 = 0; grey = decodeInfo.image->comps[0].data; short* ptrSBody = (short*)inputBuffer; for(i=0; i<width*height; i++) { *ptrSBody++ = *grey++; } /* Replace image16 buffer: */ } } release(&decodeInfo); opj_destroy_cstr_index(&(decodeInfo.cstr_index)); //decoderMutex.unlock(); return inputBuffer; }
unsigned char * OPJSupport::compressJPEG2K(void *data, int samplesPerPixel, int rows, int columns, int bitsstored, //precision, unsigned char bitsAllocated, bool sign, int rate, long *compressedDataSize) { //enconderMutex.lock(); opj_cparameters_t parameters; opj_stream_t *l_stream = 00; opj_codec_t* l_codec = 00; opj_image_t *image = NULL; OPJ_BOOL bSuccess; OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */ OPJ_UINT32 l_nb_tiles = 4; OPJ_BOOL fails = OPJ_FALSE; OPJ_CODEC_FORMAT codec_format; memset(¶meters, 0, sizeof(parameters)); opj_set_default_encoder_parameters(¶meters); parameters.outfile[0] = '\0'; parameters.tcp_numlayers = 1; parameters.cp_disto_alloc = 1; parameters.tcp_rates[0] = rate; parameters.cod_format = JP2_CFMT; //JP2_CFMT; //J2K_CFMT; OPJ_BOOL forceJ2K = (parameters.cod_format == J2K_CFMT ? OPJ_FALSE:(((OPJ_TRUE /*force here*/)))); #ifdef WITH_OPJ_FILE_STREAM tmpnam(parameters.outfile); #endif image = rawtoimage( (char*) data, ¶meters, static_cast<int>( columns*rows*samplesPerPixel*bitsAllocated/8), // [data length], fragment_size columns, rows, samplesPerPixel, bitsAllocated, bitsstored, sign, /*quality,*/ 0); if (image == NULL) { /* close and free the byte stream */ if (l_stream) opj_stream_destroy(l_stream); /* free remaining compression structures */ if (l_codec) opj_destroy_codec(l_codec); /* free image data */ if (image) opj_image_destroy(image); *compressedDataSize = 0; //enconderMutex.unlock(); return NULL; } /*-----------------------------------------------*/ switch (parameters.cod_format) { case J2K_CFMT: /* JPEG-2000 codestream */ codec_format = OPJ_CODEC_J2K; break; case JP2_CFMT: /* JPEG 2000 compressed image data */ codec_format = OPJ_CODEC_JP2; break; case JPT_CFMT: /* JPEG 2000, JPIP */ codec_format = OPJ_CODEC_JPT; break; case -1: default: fprintf(stderr,"%s:%d: encode format missing\n",__FILE__,__LINE__); /* close and free the byte stream */ if (l_stream) opj_stream_destroy(l_stream); /* free remaining compression structures */ if (l_codec) opj_destroy_codec(l_codec); /* free image data */ if (image) opj_image_destroy(image); *compressedDataSize = 0; //enconderMutex.unlock(); return NULL; } /* see test_tile_encoder.c:232 and opj_compress.c:1746 */ l_codec = opj_create_compress(codec_format); if (!l_codec) { fprintf(stderr,"%s:%d:\n\tNO codec\n",__FILE__,__LINE__); /* close and free the byte stream */ if (l_stream) opj_stream_destroy(l_stream); /* free remaining compression structures */ if (l_codec) opj_destroy_codec(l_codec); /* free image data */ if (image) opj_image_destroy(image); *compressedDataSize = 0; //enconderMutex.unlock(); return NULL; } #ifdef OPJ_VERBOSE opj_set_info_handler(l_codec, info_callback, this); opj_set_warning_handler(l_codec, warning_callback, this); #endif opj_set_error_handler(l_codec, error_callback, this); if ( !opj_setup_encoder(l_codec, ¶meters, image)) { fprintf(stderr,"%s:%d:\n\topj_setup_encoder failed\n",__FILE__,__LINE__); /* close and free the byte stream */ if (l_stream) opj_stream_destroy(l_stream); /* free remaining compression structures */ if (l_codec) opj_destroy_codec(l_codec); /* free image data */ if (image) opj_image_destroy(image); *compressedDataSize = 0; //enconderMutex.unlock(); return NULL; } // Create the stream #ifdef WITH_OPJ_BUFFER_STREAM opj_buffer_info_t bufferInfo; bufferInfo.cur = bufferInfo.buf = (OPJ_BYTE *)data; bufferInfo.len = (OPJ_SIZE_T) rows * columns; l_stream = opj_stream_create_buffer_stream(&bufferInfo, OPJ_STREAM_WRITE); //printf("%p\n",bufferInfo.buf); //printf("%lu\n",bufferInfo.len); #endif #ifdef WITH_OPJ_FILE_STREAM l_stream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_STREAM_WRITE); #endif if (!l_stream) { fprintf(stderr,"%s:%d:\n\tstream creation failed\n",__FILE__,__LINE__); /* close and free the byte stream */ if (l_stream) opj_stream_destroy(l_stream); /* free remaining compression structures */ if (l_codec) opj_destroy_codec(l_codec); /* free image data */ if (image) opj_image_destroy(image); *compressedDataSize = 0; //enconderMutex.unlock(); return NULL; } while(1) { // int tile_index=-1, user_changed_tile=0, user_changed_reduction=0; // int max_tiles=0, max_reduction=0; fails = OPJ_TRUE; /* encode the image */ bSuccess = opj_start_compress(l_codec, image, l_stream); if (!bSuccess) { fprintf(stderr,"%s:%d:\n\topj_start_compress failed\n",__FILE__,__LINE__); break; } if ( bSuccess && bUseTiles ) { OPJ_BYTE *l_data = NULL; OPJ_UINT32 l_data_size = 512*512*3; //FIXME l_data = (OPJ_BYTE*) malloc(l_data_size * sizeof(OPJ_BYTE)); memset(l_data, 0, l_data_size * sizeof(OPJ_BYTE)); //assert( l_data ); if (!l_data) { /* close and free the byte stream */ if (l_stream) opj_stream_destroy(l_stream); /* free remaining compression structures */ if (l_codec) opj_destroy_codec(l_codec); /* free image data */ if (image) opj_image_destroy(image); *compressedDataSize = 0; //enconderMutex.unlock(); return NULL; } for (int i=0;i<l_nb_tiles;++i) { if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) { fprintf(stderr, "\nERROR -> test_tile_encoder: failed to write the tile %d!\n",i); /* close and free the byte stream */ if (l_stream) opj_stream_destroy(l_stream); /* free remaining compression structures */ if (l_codec) opj_destroy_codec(l_codec); /* free image data */ if (image) opj_image_destroy(image); free(l_data); *compressedDataSize = 0; //enconderMutex.unlock(); return NULL; } } free(l_data); } else { if (!opj_encode(l_codec, l_stream)) { fprintf(stderr,"%s:%d:\n\topj_encode failed\n",__FILE__,__LINE__); break; } } if (!opj_end_compress(l_codec, l_stream)) { fprintf(stderr,"%s:%d:\n\topj_end_compress failed\n",__FILE__,__LINE__); break; } fails = OPJ_FALSE; break; } // while *compressedDataSize = 0; unsigned char *to = NULL; /* close and free the byte stream */ if (l_stream) opj_stream_destroy(l_stream); /* free remaining compression structures */ if (l_codec) opj_destroy_codec(l_codec); /* free image data */ if (image) opj_image_destroy(image); if (fails) { #ifdef WITH_OPJ_FILE_STREAM if (parameters.outfile[0] != '\0') remove(parameters.outfile); #endif } else { #ifdef WITH_OPJ_BUFFER_STREAM //printf("%p\n",bufferInfo.buf); //printf("%lu\n",bufferInfo.len); //to=(unsigned char *) malloc(bufferInfo.len); //memcpy(to,l_stream,bufferInfo.len); #endif #ifdef WITH_OPJ_FILE_STREAM // Open the temp file and get the encoded data into 'to' // and the length into 'length' FILE *f = NULL; if (parameters.outfile[0] != '\0') { f = fopen(parameters.outfile, "rb"); } long length = 0; if (f != NULL) { fseek(f, 0, SEEK_END); length = ftell(f); fseek(f, 0, SEEK_SET); if (forceJ2K) { length -= 85; fseek(f, 85, SEEK_SET); } if (length % 2) { length++; // ensure even length //fprintf(stdout,"Padded to %li\n", length); } to = (unsigned char *) malloc(length); fread(to, length, 1, f); //printf("%s %lu\n",parameters.outfile,length);; fclose(f); } *compressedDataSize = length; if (parameters.outfile[0] != '\0') { remove(parameters.outfile); } #endif } //enconderMutex.unlock(); return to; }
void* OPJSupport::decompressJPEG2KWithBuffer ( void* inputBuffer, void* jp2Data, long jp2DataSize, long *decompressedBufferSize, int *colorModel){ opj_dparameters_t parameters; OPJ_BOOL hasFile = OPJ_FALSE; opj_buffer_info_t buf_info; int i, decod_format; int width, height; OPJ_BOOL hasAlpha, fails = OPJ_FALSE; OPJ_CODEC_FORMAT codec_format; unsigned char rc, gc, bc, ac; decode_info_t decodeInfo; memset(&decodeInfo, 0, sizeof(decode_info_t)); memset(&buf_info, 0, sizeof(opj_buffer_info_t)); if (jp2Data != NULL) { buf_info.len = jp2DataSize; buf_info.buf = (OPJ_BYTE*)jp2Data; buf_info.cur = buf_info.buf; } opj_set_default_decoder_parameters(¶meters); decod_format = buffer_format(&buf_info); if(decod_format == -1) { fprintf(stderr,"%s:%d: decode format missing\n",__FILE__,__LINE__); release(&decodeInfo); return 0; } /*-----------------------------------------------*/ if(decod_format == J2K_CFMT) codec_format = OPJ_CODEC_J2K; else if(decod_format == JP2_CFMT) codec_format = OPJ_CODEC_JP2; else if(decod_format == JPT_CFMT) codec_format = OPJ_CODEC_JPT; else { /* clarified in infile_format() : */ release(&decodeInfo); return 0; } parameters.decod_format = decod_format; while(1) { int tile_index=-1, user_changed_tile=0, user_changed_reduction=0; int max_tiles=0, max_reduction=0; fails = OPJ_TRUE; decodeInfo.stream = opj_stream_create_buffer_stream(&buf_info, 1); if(decodeInfo.stream == NULL) { fprintf(stderr,"%s:%d: NO decodeInfo.stream\n",__FILE__,__LINE__); break; } decodeInfo.codec = opj_create_decompress(codec_format); if(decodeInfo.codec == NULL) { fprintf(stderr,"%s:%d: NO coded\n",__FILE__,__LINE__); break; } // opj_set_info_handler(decodeInfo.codec, error_callback, this); // opj_set_info_handler(decodeInfo.codec, warning_callback, this); // opj_set_info_handler(decodeInfo.codec, info_callback, this); if( !opj_setup_decoder(decodeInfo.codec, ¶meters)) { fprintf(stderr,"%s:%d:\n\topj_setup_decoder failed\n",__FILE__,__LINE__); break; } if(user_changed_tile && user_changed_reduction) { int reduction=0; opj_set_decoded_resolution_factor(decodeInfo.codec, reduction); } if( !opj_read_header(decodeInfo.stream, decodeInfo.codec, &decodeInfo.image)) { fprintf(stderr,"%s:%d:\n\topj_read_header failed\n",__FILE__,__LINE__); break; } if( !(user_changed_tile && user_changed_reduction) || (max_tiles <= 0) || (max_reduction <= 0) ) { opj_codestream_info_v2_t *cstr; cstr = opj_get_cstr_info(decodeInfo.codec); max_reduction = cstr->m_default_tile_info.tccp_info->numresolutions; max_tiles = cstr->tw * cstr->th; } if(tile_index < 0) { unsigned int x0, y0, x1, y1; int user_changed_area=0; x0 = y0 = x1 = y1 = 0; if(user_changed_area) { } if( !opj_set_decode_area(decodeInfo.codec, decodeInfo.image, x0, y0, x1, y1)) { fprintf(stderr,"%s:%d:\n\topj_set_decode_area failed\n",__FILE__,__LINE__); break; } if( !opj_decode(decodeInfo.codec, decodeInfo.stream, decodeInfo.image)) { fprintf(stderr,"%s:%d:\n\topj_decode failed\n",__FILE__,__LINE__); break; } } /* if(tile_index < 0) */ else { if( !opj_get_decoded_tile(decodeInfo.codec, decodeInfo.stream, decodeInfo.image, tile_index)) { fprintf(stderr,"%s:%d:\n\topj_get_decoded_tile failed\n",__FILE__,__LINE__); break; } } if( !opj_end_decompress(decodeInfo.codec, decodeInfo.stream)) { fprintf(stderr,"%s:%d:\n\topj_end_decompress failed\n",__FILE__,__LINE__); break; } fails = OPJ_FALSE; break; } decodeInfo.deleteImage = fails; release(&decodeInfo); if(fails) { return 0; } decodeInfo.deleteImage = OPJ_TRUE; if(decodeInfo.image->color_space != OPJ_CLRSPC_SYCC && decodeInfo.image->numcomps == 3 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[0].dy && decodeInfo.image->comps[1].dx != 1) decodeInfo.image->color_space = OPJ_CLRSPC_SYCC; else if(decodeInfo.image->numcomps <= 2) decodeInfo.image->color_space = OPJ_CLRSPC_GRAY; if(decodeInfo.image->color_space == OPJ_CLRSPC_SYCC) { //disable for now //color_sycc_to_rgb(decodeInfo.image); } if(decodeInfo.image->icc_profile_buf) { #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) color_apply_icc_profile(decodeInfo.image); #endif free(decodeInfo.image->icc_profile_buf); decodeInfo.image->icc_profile_buf = NULL; decodeInfo.image->icc_profile_len = 0; } width = decodeInfo.image->comps[0].w; height = decodeInfo.image->comps[0].h; long depth = (decodeInfo.image->comps[0].prec + 7)/8; long decompressSize = width * height * decodeInfo.image->numcomps * depth; if (decompressedBufferSize) *decompressedBufferSize = decompressSize;; if (!inputBuffer ) { inputBuffer = malloc(decompressSize); } if (colorModel) *colorModel = 0; if ((decodeInfo.image->numcomps >= 3 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[1].dx && decodeInfo.image->comps[1].dx == decodeInfo.image->comps[2].dx && decodeInfo.image->comps[0].dy == decodeInfo.image->comps[1].dy && decodeInfo.image->comps[1].dy == decodeInfo.image->comps[2].dy && decodeInfo.image->comps[0].prec == decodeInfo.image->comps[1].prec && decodeInfo.image->comps[1].prec == decodeInfo.image->comps[2].prec )/* RGB[A] */ || (decodeInfo.image->numcomps == 2 && decodeInfo.image->comps[0].dx == decodeInfo.image->comps[1].dx && decodeInfo.image->comps[0].dy == decodeInfo.image->comps[1].dy && decodeInfo.image->comps[0].prec == decodeInfo.image->comps[1].prec ) ) /* GA */ { int has_alpha4, has_alpha2, has_rgb; int *red, *green, *blue, *alpha; if (colorModel) *colorModel = 1; alpha = NULL; has_rgb = (decodeInfo.image->numcomps == 3); has_alpha4 = (decodeInfo.image->numcomps == 4); has_alpha2 = (decodeInfo.image->numcomps == 2); hasAlpha = (has_alpha4 || has_alpha2); if(has_rgb) { red = decodeInfo.image->comps[0].data; green = decodeInfo.image->comps[1].data; blue = decodeInfo.image->comps[2].data; if(has_alpha4) { alpha = decodeInfo.image->comps[3].data; } } /* if(has_rgb) */ else { red = green = blue = decodeInfo.image->comps[0].data; if(has_alpha2) { alpha = decodeInfo.image->comps[1].data; } } /* if(has_rgb) */ ac = 255;/* 255: FULLY_OPAQUE; 0: FULLY_TRANSPARENT */ unsigned char* ptrIBody = (unsigned char*)inputBuffer; for(i = 0; i < width*height; i++) { rc = (unsigned char) *red++; gc = (unsigned char)*green++; bc = (unsigned char)*blue++; if(hasAlpha) { ac = (unsigned char)*alpha++; // A R G B unsigned int pixel = (int)((ac<<24) | (rc<<16) | (gc<<8) | bc); *reinterpret_cast<unsigned int*>(ptrIBody) = pixel; ptrIBody += sizeof(unsigned int); } else { *ptrIBody++ = bc; *ptrIBody++ = gc; *ptrIBody++ = rc; } } /* for(i) */ }/* if (decodeInfo.image->numcomps >= 3 */ else if(decodeInfo.image->numcomps == 1) /* Grey */ { /* 1 component 8 or 16 bpp decodeInfo.image */ int *grey = decodeInfo.image->comps[0].data; if(decodeInfo.image->comps[0].prec <= 8) { char* ptrBBody = (char*)inputBuffer; for(i=0; i<width*height; i++) { *ptrBBody++ = *grey++; } /* Replace image8 buffer: */ } else /* prec[9:16] */ { int *grey; int ushift = 0, dshift = 0, force16 = 0; grey = decodeInfo.image->comps[0].data; short* ptrSBody = (short*)inputBuffer; for(i=0; i<width*height; i++) { //disable shift up for signed data: don't know why we are doing this *ptrSBody++ = *grey++; } /* Replace image16 buffer: */ } } else { int *grey; fprintf(stderr,"%s:%d:Can show only first component of decodeInfo.image\n" " components(%d) prec(%d) color_space[%d](%s)\n" " RECT(%d,%d,%d,%d)\n",__FILE__,__LINE__,decodeInfo.image->numcomps, decodeInfo.image->comps[0].prec, decodeInfo.image->color_space,clr_space(decodeInfo.image->color_space), decodeInfo.image->x0,decodeInfo.image->y0,decodeInfo.image->x1,decodeInfo.image->y1 ); for(i = 0; i < decodeInfo.image->numcomps; ++i) { fprintf(stderr,"[%d]dx(%d) dy(%d) w(%d) h(%d) signed(%u)\n",i, decodeInfo.image->comps[i].dx ,decodeInfo.image->comps[i].dy, decodeInfo.image->comps[i].w,decodeInfo.image->comps[i].h, decodeInfo.image->comps[i].sgnd); } /* 1 component 8 or 16 bpp decodeInfo.image */ grey = decodeInfo.image->comps[0].data; if(decodeInfo.image->comps[0].prec <= 8) { char* ptrBBody = (char*)inputBuffer; for(i=0; i<width*height; i++) { *ptrBBody++ = *grey++; } /* Replace image8 buffer: */ } else /* prec[9:16] */ { int *grey; int ushift = 0, dshift = 0, force16 = 0; grey = decodeInfo.image->comps[0].data; short* ptrSBody = (short*)inputBuffer; for(i=0; i<width*height; i++) { *ptrSBody++ = *grey++; } /* Replace image16 buffer: */ } } release(&decodeInfo); return inputBuffer; }