bool ossimOpjCompressor::writeTile( ossimImageData* srcTile, ossim_uint32 tileIndex ) { bool result = false; if ( srcTile ) { if (srcTile->getDataObjectStatus() != OSSIM_NULL) { ossimIrect tileRect = srcTile->getImageRectangle(); // Write the tile out: if ( opj_write_tile( m_codec, tileIndex, srcTile->getUcharBuf(), srcTile->getDataSizeInBytes(), m_stream ) ) { result = true; } else { ossimNotify(ossimNotifyLevel_WARN) << "ossimOpjCompressor::writeTile ERROR on tile index: " << tileIndex << std::endl; } } else { ossimNotify(ossimNotifyLevel_WARN) << "ossimOpjCompressor::writeTile ERROR null tile passed in!" << endl; } } return result; } // End: ossimOpjCompressor::writeTile
int main (int argc, char *argv[]) { opj_cparameters_t l_param; opj_codec_t * l_codec; opj_image_t * l_image; opj_image_cmptparm_t l_params [NUM_COMPS_MAX]; opj_stream_t * l_stream; OPJ_UINT32 l_nb_tiles; OPJ_UINT32 l_data_size; unsigned char len; #ifdef USING_MCT const OPJ_FLOAT32 l_mct [] = { 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 }; const OPJ_INT32 l_offsets [] = { 128 , 128 , 128 }; #endif opj_image_cmptparm_t * l_current_param_ptr; OPJ_UINT32 i; OPJ_BYTE *l_data; OPJ_UINT32 num_comps; int image_width; int image_height; int tile_width; int tile_height; int comp_prec; int irreversible; char output_file[64]; /* should be test_tile_encoder 3 2000 2000 1000 1000 8 tte1.j2k */ if( argc == 9 ) { num_comps = (OPJ_UINT32)atoi( argv[1] ); image_width = atoi( argv[2] ); image_height = atoi( argv[3] ); tile_width = atoi( argv[4] ); tile_height = atoi( argv[5] ); comp_prec = atoi( argv[6] ); irreversible = atoi( argv[7] ); strcpy(output_file, argv[8] ); } else { num_comps = 3; image_width = 2000; image_height = 2000; tile_width = 1000; tile_height = 1000; comp_prec = 8; irreversible = 1; strcpy(output_file, "test.j2k" ); } if( num_comps > NUM_COMPS_MAX ) { return 1; } l_nb_tiles = (OPJ_UINT32)(image_width/tile_width) * (OPJ_UINT32)(image_height/tile_height); l_data_size = (OPJ_UINT32)tile_width * (OPJ_UINT32)tile_height * (OPJ_UINT32)num_comps * (OPJ_UINT32)(comp_prec/8); l_data = (OPJ_BYTE*) malloc(l_data_size * sizeof(OPJ_BYTE)); fprintf(stdout, "Encoding random values -> keep in mind that this is very hard to compress\n"); for (i=0;i<l_data_size;++i) { l_data[i] = (OPJ_BYTE)i; /*rand();*/ } opj_set_default_encoder_parameters(&l_param); /** you may here add custom encoding parameters */ /* rate specifications */ /** number of quality layers in the stream */ l_param.tcp_numlayers = 1; l_param.cp_fixed_quality = 1; l_param.tcp_distoratio[0] = 20; /* is using others way of calculation */ /* l_param.cp_disto_alloc = 1 or l_param.cp_fixed_alloc = 1 */ /* l_param.tcp_rates[0] = ... */ /* tile definitions parameters */ /* position of the tile grid aligned with the image */ l_param.cp_tx0 = 0; l_param.cp_ty0 = 0; /* tile size, we are using tile based encoding */ l_param.tile_size_on = OPJ_TRUE; l_param.cp_tdx = tile_width; l_param.cp_tdy = tile_height; /* use irreversible encoding ?*/ l_param.irreversible = irreversible; /* do not bother with mct, the rsiz is set when calling opj_set_MCT*/ /*l_param.cp_rsiz = OPJ_STD_RSIZ;*/ /* no cinema */ /*l_param.cp_cinema = 0;*/ /* no not bother using SOP or EPH markers, do not use custom size precinct */ /* number of precincts to specify */ /* l_param.csty = 0;*/ /* l_param.res_spec = ... */ /* l_param.prch_init[i] = .. */ /* l_param.prcw_init[i] = .. */ /* do not use progression order changes */ /*l_param.numpocs = 0;*/ /* l_param.POC[i].... */ /* do not restrain the size for a component.*/ /* l_param.max_comp_size = 0; */ /** block encoding style for each component, do not use at the moment */ /** J2K_CCP_CBLKSTY_TERMALL, J2K_CCP_CBLKSTY_LAZY, J2K_CCP_CBLKSTY_VSC, J2K_CCP_CBLKSTY_SEGSYM, J2K_CCP_CBLKSTY_RESET */ /* l_param.mode = 0;*/ /** number of resolutions */ l_param.numresolution = 6; /** progression order to use*/ /** OPJ_LRCP, OPJ_RLCP, OPJ_RPCL, PCRL, CPRL */ l_param.prog_order = OPJ_LRCP; /** no "region" of interest, more precisally component */ /* l_param.roi_compno = -1; */ /* l_param.roi_shift = 0; */ /* we are not using multiple tile parts for a tile. */ /* l_param.tp_on = 0; */ /* l_param.tp_flag = 0; */ /* if we are using mct */ #ifdef USING_MCT opj_set_MCT(&l_param,l_mct,l_offsets,NUM_COMPS); #endif /* image definition */ l_current_param_ptr = l_params; for (i=0;i<num_comps;++i) { /* do not bother bpp useless */ /*l_current_param_ptr->bpp = COMP_PREC;*/ l_current_param_ptr->dx = 1; l_current_param_ptr->dy = 1; l_current_param_ptr->h = (OPJ_UINT32)image_height; l_current_param_ptr->w = (OPJ_UINT32)image_width; l_current_param_ptr->sgnd = 0; l_current_param_ptr->prec = (OPJ_UINT32)comp_prec; l_current_param_ptr->x0 = 0; l_current_param_ptr->y0 = 0; ++l_current_param_ptr; } /* should we do j2k or jp2 ?*/ len = (unsigned char)strlen( output_file ); if( strcmp( output_file + len - 4, ".jp2" ) == 0 ) { l_codec = opj_create_compress(OPJ_CODEC_JP2); } else { l_codec = opj_create_compress(OPJ_CODEC_J2K); } if (!l_codec) { return 1; } /* catch events using our callbacks and give a local context */ opj_set_info_handler(l_codec, info_callback,00); opj_set_warning_handler(l_codec, warning_callback,00); opj_set_error_handler(l_codec, error_callback,00); l_image = opj_image_tile_create(num_comps,l_params,OPJ_CLRSPC_SRGB); if (! l_image) { opj_destroy_codec(l_codec); return 1; } l_image->x0 = 0; l_image->y0 = 0; l_image->x1 = (OPJ_UINT32)image_width; l_image->y1 = (OPJ_UINT32)image_height; l_image->color_space = OPJ_CLRSPC_SRGB; if (! opj_setup_encoder(l_codec,&l_param,l_image)) { fprintf(stderr, "ERROR -> test_tile_encoder: failed to setup the codec!\n"); opj_destroy_codec(l_codec); opj_image_destroy(l_image); return 1; } l_stream = opj_stream_create_default_file_stream_v3(output_file, OPJ_FALSE); if (! l_stream) { fprintf(stderr, "ERROR -> test_tile_encoder: failed to create the stream from the output file %s !\n",output_file ); opj_destroy_codec(l_codec); opj_image_destroy(l_image); return 1; } if (! opj_start_compress(l_codec,l_image,l_stream)) { fprintf(stderr, "ERROR -> test_tile_encoder: failed to start compress!\n"); opj_stream_destroy_v3(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(l_image); return 1; } for (i=0;i<l_nb_tiles;++i) { if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) { fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i); opj_stream_destroy_v3(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(l_image); return 1; } } if (! opj_end_compress(l_codec,l_stream)) { fprintf(stderr, "ERROR -> test_tile_encoder: failed to end compress!\n"); opj_stream_destroy_v3(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(l_image); return 1; } opj_stream_destroy_v3(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(l_image); free(l_data); /* Print profiling*/ /*PROFPRINT();*/ return 0; }
static int j2k_encode_entry(Imaging im, ImagingCodecState state, ImagingIncrementalCodec encoder) { JPEG2KENCODESTATE *context = (JPEG2KENCODESTATE *)state->context; opj_stream_t *stream = NULL; opj_image_t *image = NULL; opj_codec_t *codec = NULL; opj_cparameters_t params; unsigned components; OPJ_COLOR_SPACE color_space; opj_image_cmptparm_t image_params[4]; unsigned xsiz, ysiz; unsigned tile_width, tile_height; unsigned tiles_x, tiles_y, num_tiles; unsigned x, y, tile_ndx; unsigned n; j2k_pack_tile_t pack; int ret = -1; stream = opj_stream_default_create(OPJ_FALSE); if (!stream) { state->errcode = IMAGING_CODEC_BROKEN; state->state = J2K_STATE_FAILED; goto quick_exit; } opj_stream_set_write_function(stream, j2k_write); opj_stream_set_skip_function(stream, j2k_skip); opj_stream_set_seek_function(stream, j2k_seek); opj_stream_set_user_data(stream, encoder); /* Setup an opj_image */ if (strcmp (im->mode, "L") == 0) { components = 1; color_space = OPJ_CLRSPC_GRAY; pack = j2k_pack_l; } else if (strcmp (im->mode, "LA") == 0) { components = 2; color_space = OPJ_CLRSPC_GRAY; pack = j2k_pack_la; } else if (strcmp (im->mode, "RGB") == 0) { components = 3; color_space = OPJ_CLRSPC_SRGB; pack = j2k_pack_rgb; } else if (strcmp (im->mode, "YCbCr") == 0) { components = 3; color_space = OPJ_CLRSPC_SYCC; pack = j2k_pack_rgb; } else if (strcmp (im->mode, "RGBA") == 0) { components = 4; color_space = OPJ_CLRSPC_SRGB; pack = j2k_pack_rgba; } else { state->errcode = IMAGING_CODEC_BROKEN; state->state = J2K_STATE_FAILED; goto quick_exit; } for (n = 0; n < components; ++n) { image_params[n].dx = image_params[n].dy = 1; image_params[n].w = im->xsize; image_params[n].h = im->ysize; image_params[n].x0 = image_params[n].y0 = 0; image_params[n].prec = 8; image_params[n].bpp = 8; image_params[n].sgnd = 0; } image = opj_image_create(components, image_params, color_space); /* Setup compression context */ context->error_msg = NULL; opj_set_default_encoder_parameters(¶ms); params.image_offset_x0 = context->offset_x; params.image_offset_y0 = context->offset_y; if (context->tile_size_x && context->tile_size_y) { params.tile_size_on = OPJ_TRUE; params.cp_tx0 = context->tile_offset_x; params.cp_ty0 = context->tile_offset_y; params.cp_tdx = context->tile_size_x; params.cp_tdy = context->tile_size_y; tile_width = params.cp_tdx; tile_height = params.cp_tdy; } else { params.cp_tx0 = 0; params.cp_ty0 = 0; params.cp_tdx = 1; params.cp_tdy = 1; tile_width = im->xsize; tile_height = im->ysize; } if (context->quality_layers && PySequence_Check(context->quality_layers)) { Py_ssize_t len = PySequence_Length(context->quality_layers); Py_ssize_t n; float *pq; if (len) { if (len > sizeof(params.tcp_rates) / sizeof(params.tcp_rates[0])) len = sizeof(params.tcp_rates)/sizeof(params.tcp_rates[0]); params.tcp_numlayers = (int)len; if (context->quality_is_in_db) { params.cp_disto_alloc = params.cp_fixed_alloc = 0; params.cp_fixed_quality = 1; pq = params.tcp_distoratio; } else { params.cp_disto_alloc = 1; params.cp_fixed_alloc = params.cp_fixed_quality = 0; pq = params.tcp_rates; } for (n = 0; n < len; ++n) { PyObject *obj = PySequence_ITEM(context->quality_layers, n); pq[n] = PyFloat_AsDouble(obj); } } } else { params.tcp_numlayers = 1; params.tcp_rates[0] = 0; params.cp_disto_alloc = 1; } if (context->num_resolutions) params.numresolution = context->num_resolutions; if (context->cblk_width >= 4 && context->cblk_width <= 1024 && context->cblk_height >= 4 && context->cblk_height <= 1024 && context->cblk_width * context->cblk_height <= 4096) { params.cblockw_init = context->cblk_width; params.cblockh_init = context->cblk_height; } if (context->precinct_width >= 4 && context->precinct_height >= 4 && context->precinct_width >= context->cblk_width && context->precinct_height > context->cblk_height) { params.prcw_init[0] = context->precinct_width; params.prch_init[0] = context->precinct_height; params.res_spec = 1; params.csty |= 0x01; } params.irreversible = context->irreversible; params.prog_order = context->progression; params.cp_cinema = context->cinema_mode; switch (params.cp_cinema) { case OPJ_OFF: params.cp_rsiz = OPJ_STD_RSIZ; break; case OPJ_CINEMA2K_24: case OPJ_CINEMA2K_48: params.cp_rsiz = OPJ_CINEMA2K; if (params.numresolution > 6) params.numresolution = 6; break; case OPJ_CINEMA4K_24: params.cp_rsiz = OPJ_CINEMA4K; if (params.numresolution > 7) params.numresolution = 7; break; } if (context->cinema_mode != OPJ_OFF) j2k_set_cinema_params(im, components, ¶ms); /* Set up the reference grid in the image */ image->x0 = params.image_offset_x0; image->y0 = params.image_offset_y0; image->x1 = xsiz = im->xsize + params.image_offset_x0; image->y1 = ysiz = im->ysize + params.image_offset_y0; /* Create the compressor */ codec = opj_create_compress(context->format); if (!codec) { state->errcode = IMAGING_CODEC_BROKEN; state->state = J2K_STATE_FAILED; goto quick_exit; } opj_set_error_handler(codec, j2k_error, context); opj_setup_encoder(codec, ¶ms, image); /* Start encoding */ if (!opj_start_compress(codec, image, stream)) { state->errcode = IMAGING_CODEC_BROKEN; state->state = J2K_STATE_FAILED; goto quick_exit; } /* Write each tile */ tiles_x = (im->xsize + (params.image_offset_x0 - params.cp_tx0) + tile_width - 1) / tile_width; tiles_y = (im->ysize + (params.image_offset_y0 - params.cp_ty0) + tile_height - 1) / tile_height; num_tiles = tiles_x * tiles_y; state->buffer = malloc (tile_width * tile_height * components); tile_ndx = 0; for (y = 0; y < tiles_y; ++y) { unsigned ty0 = params.cp_ty0 + y * tile_height; unsigned ty1 = ty0 + tile_height; unsigned pixy, pixh; if (ty0 < params.image_offset_y0) ty0 = params.image_offset_y0; if (ty1 > ysiz) ty1 = ysiz; pixy = ty0 - params.image_offset_y0; pixh = ty1 - ty0; for (x = 0; x < tiles_x; ++x) { unsigned tx0 = params.cp_tx0 + x * tile_width; unsigned tx1 = tx0 + tile_width; unsigned pixx, pixw; unsigned data_size; if (tx0 < params.image_offset_x0) tx0 = params.image_offset_x0; if (tx1 > xsiz) tx1 = xsiz; pixx = tx0 - params.image_offset_x0; pixw = tx1 - tx0; pack(im, state->buffer, pixx, pixy, pixw, pixh); data_size = pixw * pixh * components; if (!opj_write_tile(codec, tile_ndx++, state->buffer, data_size, stream)) { state->errcode = IMAGING_CODEC_BROKEN; state->state = J2K_STATE_FAILED; goto quick_exit; } } } if (!opj_end_compress(codec, stream)) { state->errcode = IMAGING_CODEC_BROKEN; state->state = J2K_STATE_FAILED; goto quick_exit; } state->errcode = IMAGING_CODEC_END; state->state = J2K_STATE_DONE; ret = (int)ImagingIncrementalCodecBytesInBuffer(encoder); quick_exit: if (codec) opj_destroy_codec(codec); if (image) opj_image_destroy(image); if (stream) opj_stream_destroy(stream); return ret; }
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; }
OpenJPEGWriter_setTile(J2K_USER_DATA *data, nrt_Uint32 tileX, nrt_Uint32 tileY, const nrt_Uint8 *buf, nrt_Uint32 tileSize, nrt_Error *error) { OpenJPEGWriterImpl *impl = (OpenJPEGWriterImpl*) data; NRT_BOOL rc = NRT_SUCCESS; nrt_Uint32 xTiles, yTiles, tileIndex, width, height, tileWidth, tileHeight; nrt_Uint32 thisTileWidth, thisTileHeight, thisTileSize, nComponents, nBytes; nrt_Uint8* newTileBuf = NULL; xTiles = j2k_Container_getTilesX(impl->container, error); yTiles = j2k_Container_getTilesY(impl->container, error); width = j2k_Container_getWidth(impl->container, error); height = j2k_Container_getHeight(impl->container, error); tileWidth = j2k_Container_getTileWidth(impl->container, error); tileHeight = j2k_Container_getTileHeight(impl->container, error); nComponents = j2k_Container_getNumComponents(impl->container, error); nBytes = (j2k_Container_getPrecision(impl->container, error) - 1) / 8 + 1; tileIndex = tileY * xTiles + tileX; memset(error->message, 0, NRT_MAX_EMESSAGE); if(!opj_set_error_handler(impl->codec, OpenJPEG_errorHandler, error)) { nrt_Error_init(error, "Unable to set OpenJPEG error handler", NRT_CTXT, NRT_ERR_UNK); goto CATCH_ERROR; } /* Check for edge case where we may have partial tile */ thisTileWidth = tileWidth; thisTileHeight = tileHeight; if (tileX == xTiles - 1 && width % tileWidth != 0) thisTileWidth = width % tileWidth; if (tileY == yTiles - 1 && height % tileHeight != 0) thisTileHeight = height % tileHeight; thisTileSize = thisTileWidth * thisTileHeight * nComponents * nBytes; if(thisTileSize != tileSize) tileSize = thisTileSize; if(thisTileWidth < tileWidth) { /* TODO: The current approach below only works for single band * imagery. For RGB data, I believe it is stored as all * red, then all green, then all blue, so we would need * a temp buffer rather than reusing the current buffer. */ if (nComponents != 1) { nrt_Error_init( error, "Partial tile width not implemented for multi-band", NRT_CTXT, NRT_ERR_UNK); goto CATCH_ERROR; } /* We have a tile that is wider than it "should" be * Need to create smaller buffer to pass to write function */ { OPJ_UINT32 ii; size_t srcOffset = 0; size_t destOffset = 0; const size_t srcStride = tileWidth * nBytes; const size_t destStride = thisTileWidth * nBytes; newTileBuf = (nrt_Uint8*) J2K_MALLOC(thisTileSize); if(!newTileBuf) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); goto CATCH_ERROR; } for(ii = 0; ii < thisTileHeight; ++ii, srcOffset += srcStride, destOffset += destStride) memcpy(newTileBuf + destOffset, buf + srcOffset, destStride); buf = newTileBuf; } } if (!opj_write_tile(impl->codec, tileIndex, (OPJ_BYTE* )buf, tileSize, impl->stream)) { nrt_Error ioError; const nrt_Off currentPos = nrt_IOInterface_tell(impl->compressed, &ioError); const nrt_Off ioSize = nrt_IOInterface_getSize(impl->compressed, &ioError); if (NRT_IO_SUCCESS(currentPos) && NRT_IO_SUCCESS(ioSize) && currentPos + OPENJPEG_STREAM_SIZE >= ioSize) { /* The write failed because implStreamWrite() failed because * nrt_IOInterface_write() failed because we didn't have enough * room left in the buffer that we copy to prior to flushing out * to disk in OpenJPEGWriter_write(). The buffer is sized to the * uncompressed image size, so this only occurs if the compressed * image is actually larger than the uncompressed size. * TODO: Handle resizing the buffer on the fly when this occurs * inside implStreamWrite(). Long-term if we're able to thread * per tile, we won't have to reallocate nearly as much. */ nrt_Error_init(error, "Error writing tile: Compressed image is larger " "than uncompressed image", NRT_CTXT, NRT_ERR_INVALID_OBJECT); } /*nrt_Error_init(error, "Error writing tile", NRT_CTXT, NRT_ERR_INVALID_OBJECT);*/ goto CATCH_ERROR; } goto CLEANUP; CATCH_ERROR: { rc = NRT_FAILURE; } CLEANUP: { if(newTileBuf) J2K_FREE(newTileBuf); } return rc; }