opj_stream_t* ossimOpjCompressor::createOpjStream( std::ostream* os ) const { opj_stream_t* stream = 0; if ( os ) { // stream = opj_stream_create_default_file_stream("test.jp2", OPJ_FALSE); // OPJ stream: // stream = opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE, OPJ_FALSE); // stream = opj_stream_create(512, OPJ_FALSE); // stream = opj_stream_create_default_file_stream("test.jp2", OPJ_FALSE); // false = output. stream = opj_stream_create(1048576, OPJ_FALSE); if ( stream ) { // cout << "stream good..." << endl; // Set OPJ user stream: opj_stream_set_user_data(stream, os, ossim_opj_free_user_ostream_data); // Set callbacks: // opj_stream_set_read_function(m_stream, ossim_opj_stream_read); // opj_stream_set_read_function(stream, ossim_opj_imagedata_read); opj_stream_set_write_function(stream, ossim_opj_ostream_write); opj_stream_set_skip_function(stream, ossim_opj_ostream_skip); opj_stream_set_seek_function(stream, ossim_opj_ostream_seek); } } return stream; } // End: ossimOpjCompressor::createOpjStream()
OpenJPEG_createIO(nrt_IOInterface* io, IOControl* ioControl, nrt_Off length, int isInput, nrt_Error *error) { opj_stream_t *stream = NULL; stream = opj_stream_create(OPENJPEG_STREAM_SIZE, isInput); if (!stream) { nrt_Error_init(error, "Error creating openjpeg stream", NRT_CTXT, NRT_ERR_MEMORY); } else { ioControl->io = io; ioControl->offset = nrt_IOInterface_tell(io, error); if (length > 0) ioControl->length = length; else ioControl->length = nrt_IOInterface_getSize(io, error) - ioControl->offset; opj_stream_set_user_data(stream, ioControl); opj_stream_set_read_function(stream, implStreamRead); opj_stream_set_seek_function(stream, implStreamSeek); opj_stream_set_skip_function(stream, implStreamSkip); opj_stream_set_write_function(stream, implStreamWrite); } return stream; }
J2KFIO_t* opj_freeimage_stream_create(FreeImageIO *io, fi_handle handle, BOOL bRead) { if(!handle) { return NULL; } J2KFIO_t *fio = (J2KFIO_t*)malloc(sizeof(J2KFIO_t)); if(fio) { fio->io = io; fio->handle = handle; opj_stream_t *l_stream = opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE, bRead ? OPJ_TRUE : OPJ_FALSE); if (l_stream) { opj_stream_set_user_data(l_stream, fio); opj_stream_set_user_data_length(l_stream, _LengthProc(fio)); opj_stream_set_read_function(l_stream, (opj_stream_read_fn)_ReadProc); opj_stream_set_write_function(l_stream, (opj_stream_write_fn)_WriteProc); opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn)_SkipProc); opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn)_SeekProc); fio->stream = l_stream; return fio; } else { free(fio); } } return NULL; }
static opj_stream_t *opj_stream_create_from_file(const char *filepath, OPJ_UINT32 p_size, OPJ_BOOL p_is_read_stream, FILE **r_file) { FILE *p_file = BLI_fopen(filepath, p_is_read_stream ? "rb" : "wb"); if (p_file == NULL) { return NULL; } opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream); if (l_stream == NULL) { fclose(p_file); return NULL; } opj_stream_set_user_data(l_stream, p_file, opj_free_from_file); opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file)); opj_stream_set_write_function(l_stream, opj_write_from_file); opj_stream_set_read_function(l_stream, opj_read_from_file); opj_stream_set_skip_function(l_stream, opj_skip_from_file); opj_stream_set_seek_function(l_stream, opj_seek_from_file); if (r_file) { *r_file = p_file; } return l_stream; }
/* Static generator of opj_stream from file stream */ static opj_stream_t * opjCreateStream(FILE *fp, l_int32 is_read_stream) { opj_stream_t *l_stream; PROCNAME("opjStreamCreate"); if (!fp) return (opj_stream_t *)ERROR_PTR("fp not defined", procName, NULL); l_stream = opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE, is_read_stream); if (!l_stream) return (opj_stream_t *)ERROR_PTR("stream not made", procName, NULL); #if OPJ_VERSION_MINOR == 0 opj_stream_set_user_data(l_stream, fp); #else opj_stream_set_user_data(l_stream, fp, (opj_stream_free_user_data_fn)NULL); #endif opj_stream_set_user_data_length(l_stream, opj_get_user_data_length(fp)); opj_stream_set_read_function(l_stream, (opj_stream_read_fn)opj_read_from_file); opj_stream_set_write_function(l_stream, (opj_stream_write_fn)opj_write_from_file); opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn)opj_skip_from_file); opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn)opj_seek_from_file); return l_stream; }
opj_stream_t* OPJ_CALLCONV opj_stream_create_memory_stream(DecodeData* p_mem,OPJ_UINT32 p_size, bool p_is_read_stream) { opj_stream_t* l_stream = NULL; if (! p_mem) return NULL; l_stream = opj_stream_create(p_size,p_is_read_stream); if (!l_stream) return NULL; opj_stream_set_user_data(l_stream, p_mem, NULL); opj_stream_set_user_data_length(l_stream, p_mem->src_size); opj_stream_set_read_function(l_stream,(opj_stream_read_fn) opj_read_from_memory); opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_memory); opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_memory); opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_memory); return l_stream; }
static opj_stream_t *opj_stream_create_from_buffer(struct BufInfo *p_file, OPJ_UINT32 p_size, OPJ_BOOL p_is_read_stream) { opj_stream_t *l_stream = opj_stream_create(p_size, p_is_read_stream); if (l_stream == NULL) { return NULL; } opj_stream_set_user_data(l_stream, p_file, opj_read_from_buffer_free); opj_stream_set_user_data_length(l_stream, p_file->len); opj_stream_set_read_function(l_stream, opj_read_from_buffer); #if 0 /* UNUSED */ opj_stream_set_write_function(l_stream, opj_write_from_buffer); #endif opj_stream_set_skip_function(l_stream, opj_skip_from_buffer); opj_stream_set_seek_function(l_stream, opj_seek_from_buffer); return l_stream; }
opj_stream_t* fx_opj_stream_create_memory_stream(DecodeData* data, OPJ_SIZE_T p_size, OPJ_BOOL p_is_read_stream) { opj_stream_t* l_stream = 00; if (!data || !data->src_data || data->src_size <= 0) { return nullptr; } l_stream = opj_stream_create(p_size, p_is_read_stream); if (!l_stream) { return nullptr; } opj_stream_set_user_data(l_stream, data, nullptr); opj_stream_set_user_data_length(l_stream, data->src_size); opj_stream_set_read_function(l_stream, opj_read_from_memory); opj_stream_set_write_function(l_stream, opj_write_from_memory); opj_stream_set_skip_function(l_stream, opj_skip_from_memory); opj_stream_set_seek_function(l_stream, opj_seek_from_memory); return l_stream; }
opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream( const char *fname, OPJ_SIZE_T p_size, OPJ_BOOL p_is_read_stream) { opj_stream_t* l_stream = 00; FILE *p_file; const char *mode; if (! fname) { return NULL; } if (p_is_read_stream) { mode = "rb"; } else { mode = "wb"; } p_file = fopen(fname, mode); if (! p_file) { return NULL; } l_stream = opj_stream_create(p_size, p_is_read_stream); if (! l_stream) { fclose(p_file); return NULL; } opj_stream_set_user_data(l_stream, p_file, (opj_stream_free_user_data_fn) fclose); opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file)); opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file); opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file); opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file); opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file); return l_stream; }
opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file,OPJ_UINT32 p_size,bool p_is_read_stream) { opj_stream_t* l_stream = 00; if (! p_file) { return 00; } l_stream = opj_stream_create(p_size,p_is_read_stream); if (! l_stream) { return 00; } opj_stream_set_user_data(l_stream,p_file); opj_stream_set_read_function(l_stream,(opj_stream_read_fn) opj_read_from_file); opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file); opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file); opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file); return l_stream; }
opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream ( FILE * p_file, OPJ_SIZE_T p_size, OPJ_BOOL p_is_read_stream) { opj_stream_t* l_stream = 00; if (! p_file) { return NULL; } l_stream = opj_stream_create(p_size,p_is_read_stream); if (! l_stream) { return NULL; } opj_stream_set_user_data(l_stream, p_file); opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file)); opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file); opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file); opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file); opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file); return l_stream; }
static GstFlowReturn gst_openjpeg_enc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame) { GstOpenJPEGEnc *self = GST_OPENJPEG_ENC (encoder); GstFlowReturn ret = GST_FLOW_OK; #ifdef HAVE_OPENJPEG_1 opj_cinfo_t *enc; GstMapInfo map; guint length; opj_cio_t *io; #else opj_codec_t *enc; opj_stream_t *stream; MemStream mstream; #endif opj_image_t *image; GstVideoFrame vframe; GST_DEBUG_OBJECT (self, "Handling frame"); enc = opj_create_compress (self->codec_format); if (!enc) goto initialization_error; #ifdef HAVE_OPENJPEG_1 if (G_UNLIKELY (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_TRACE)) { opj_event_mgr_t callbacks; callbacks.error_handler = gst_openjpeg_enc_opj_error; callbacks.warning_handler = gst_openjpeg_enc_opj_warning; callbacks.info_handler = gst_openjpeg_enc_opj_info; opj_set_event_mgr ((opj_common_ptr) enc, &callbacks, self); } else { opj_set_event_mgr ((opj_common_ptr) enc, NULL, NULL); } #else if (G_UNLIKELY (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_TRACE)) { opj_set_info_handler (enc, gst_openjpeg_enc_opj_info, self); opj_set_warning_handler (enc, gst_openjpeg_enc_opj_warning, self); opj_set_error_handler (enc, gst_openjpeg_enc_opj_error, self); } else { opj_set_info_handler (enc, NULL, NULL); opj_set_warning_handler (enc, NULL, NULL); opj_set_error_handler (enc, NULL, NULL); } #endif if (!gst_video_frame_map (&vframe, &self->input_state->info, frame->input_buffer, GST_MAP_READ)) goto map_read_error; image = gst_openjpeg_enc_fill_image (self, &vframe); if (!image) goto fill_image_error; gst_video_frame_unmap (&vframe); opj_setup_encoder (enc, &self->params, image); #ifdef HAVE_OPENJPEG_1 io = opj_cio_open ((opj_common_ptr) enc, NULL, 0); if (!io) goto open_error; if (!opj_encode (enc, io, image, NULL)) goto encode_error; opj_image_destroy (image); length = cio_tell (io); ret = gst_video_encoder_allocate_output_frame (encoder, frame, length + (self->is_jp2c ? 8 : 0)); if (ret != GST_FLOW_OK) goto allocate_error; gst_buffer_fill (frame->output_buffer, self->is_jp2c ? 8 : 0, io->buffer, length); if (self->is_jp2c) { gst_buffer_map (frame->output_buffer, &map, GST_MAP_WRITE); GST_WRITE_UINT32_BE (map.data, length + 8); GST_WRITE_UINT32_BE (map.data + 4, GST_MAKE_FOURCC ('j', 'p', '2', 'c')); gst_buffer_unmap (frame->output_buffer, &map); } opj_cio_close (io); opj_destroy_compress (enc); #else stream = opj_stream_create (4096, OPJ_FALSE); if (!stream) goto open_error; mstream.allocsize = 4096; mstream.data = g_malloc (mstream.allocsize); mstream.offset = 0; mstream.size = 0; opj_stream_set_read_function (stream, read_fn); opj_stream_set_write_function (stream, write_fn); opj_stream_set_skip_function (stream, skip_fn); opj_stream_set_seek_function (stream, seek_fn); opj_stream_set_user_data (stream, &mstream); opj_stream_set_user_data_length (stream, mstream.size); if (!opj_start_compress (enc, image, stream)) goto encode_error; if (!opj_encode (enc, stream)) goto encode_error; if (!opj_end_compress (enc, stream)) goto encode_error; opj_image_destroy (image); opj_stream_destroy (stream); opj_destroy_codec (enc); frame->output_buffer = gst_buffer_new (); if (self->is_jp2c) { GstMapInfo map; GstMemory *mem; mem = gst_allocator_alloc (NULL, 8, NULL); gst_memory_map (mem, &map, GST_MAP_WRITE); GST_WRITE_UINT32_BE (map.data, mstream.size + 8); GST_WRITE_UINT32_BE (map.data + 4, GST_MAKE_FOURCC ('j', 'p', '2', 'c')); gst_memory_unmap (mem, &map); gst_buffer_append_memory (frame->output_buffer, mem); } gst_buffer_append_memory (frame->output_buffer, gst_memory_new_wrapped (0, mstream.data, mstream.allocsize, 0, mstream.size, NULL, (GDestroyNotify) g_free)); #endif ret = gst_video_encoder_finish_frame (encoder, frame); return ret; initialization_error: { gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, LIBRARY, INIT, ("Failed to initialize OpenJPEG encoder"), (NULL)); return GST_FLOW_ERROR; } map_read_error: { #ifdef HAVE_OPENJPEG_1 opj_destroy_compress (enc); #else opj_destroy_codec (enc); #endif gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, CORE, FAILED, ("Failed to map input buffer"), (NULL)); return GST_FLOW_ERROR; } fill_image_error: { #ifdef HAVE_OPENJPEG_1 opj_destroy_compress (enc); #else opj_destroy_codec (enc); #endif gst_video_frame_unmap (&vframe); gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, LIBRARY, INIT, ("Failed to fill OpenJPEG image"), (NULL)); return GST_FLOW_ERROR; } open_error: { opj_image_destroy (image); #ifdef HAVE_OPENJPEG_1 opj_destroy_compress (enc); #else opj_destroy_codec (enc); #endif gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, LIBRARY, INIT, ("Failed to open OpenJPEG data"), (NULL)); return GST_FLOW_ERROR; } encode_error: { #ifdef HAVE_OPENJPEG_1 opj_cio_close (io); opj_image_destroy (image); opj_destroy_compress (enc); #else opj_stream_destroy (stream); g_free (mstream.data); opj_image_destroy (image); opj_destroy_codec (enc); #endif gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, STREAM, ENCODE, ("Failed to encode OpenJPEG stream"), (NULL)); return GST_FLOW_ERROR; } #ifdef HAVE_OPENJPEG_1 allocate_error: { opj_cio_close (io); opj_destroy_compress (enc); gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, CORE, FAILED, ("Failed to allocate output buffer"), (NULL)); return ret; } #endif }
static Image *ReadJP2Image(const ImageInfo *image_info,ExceptionInfo *exception) { const char *option; Image *image; int jp2_status; MagickBooleanType status; opj_codec_t *jp2_codec; opj_codestream_index_t *codestream_index = (opj_codestream_index_t *) NULL; opj_dparameters_t parameters; opj_image_t *jp2_image; opj_stream_t *jp2_stream; register ssize_t i; ssize_t y; unsigned char sans[4]; /* Open image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickCoreSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickCoreSignature); image=AcquireImage(image_info,exception); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } /* Initialize JP2 codec. */ if (ReadBlob(image,4,sans) != 4) { image=DestroyImageList(image); return((Image *) NULL); } (void) SeekBlob(image,SEEK_SET,0); if (LocaleCompare(image_info->magick,"JPT") == 0) jp2_codec=opj_create_decompress(OPJ_CODEC_JPT); else if (IsJ2K(sans,4) != MagickFalse) jp2_codec=opj_create_decompress(OPJ_CODEC_J2K); else jp2_codec=opj_create_decompress(OPJ_CODEC_JP2); opj_set_warning_handler(jp2_codec,JP2WarningHandler,exception); opj_set_error_handler(jp2_codec,JP2ErrorHandler,exception); opj_set_default_decoder_parameters(¶meters); option=GetImageOption(image_info,"jp2:reduce-factor"); if (option != (const char *) NULL) parameters.cp_reduce=StringToInteger(option); option=GetImageOption(image_info,"jp2:quality-layers"); if (option != (const char *) NULL) parameters.cp_layer=StringToInteger(option); if (opj_setup_decoder(jp2_codec,¶meters) == 0) { opj_destroy_codec(jp2_codec); ThrowReaderException(DelegateError,"UnableToManageJP2Stream"); } jp2_stream=opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE,1); opj_stream_set_read_function(jp2_stream,JP2ReadHandler); opj_stream_set_write_function(jp2_stream,JP2WriteHandler); opj_stream_set_seek_function(jp2_stream,JP2SeekHandler); opj_stream_set_skip_function(jp2_stream,JP2SkipHandler); opj_stream_set_user_data(jp2_stream,image,NULL); opj_stream_set_user_data_length(jp2_stream,GetBlobSize(image)); if (opj_read_header(jp2_stream,jp2_codec,&jp2_image) == 0) { opj_stream_destroy(jp2_stream); opj_destroy_codec(jp2_codec); ThrowReaderException(DelegateError,"UnableToDecodeImageFile"); } jp2_status=1; if ((image->columns != 0) && (image->rows != 0)) { /* Extract an area from the image. */ jp2_status=opj_set_decode_area(jp2_codec,jp2_image, (OPJ_INT32) image->extract_info.x,(OPJ_INT32) image->extract_info.y, (OPJ_INT32) (image->extract_info.x+(ssize_t) image->columns), (OPJ_INT32) (image->extract_info.y+(ssize_t) image->rows)); if (jp2_status == 0) { opj_stream_destroy(jp2_stream); opj_destroy_codec(jp2_codec); opj_image_destroy(jp2_image); ThrowReaderException(DelegateError,"UnableToDecodeImageFile"); } } if ((image_info->number_scenes != 0) && (image_info->scene != 0)) jp2_status=opj_get_decoded_tile(jp2_codec,jp2_stream,jp2_image, (unsigned int) image_info->scene-1); else if (image->ping == MagickFalse) { jp2_status=opj_decode(jp2_codec,jp2_stream,jp2_image); if (jp2_status != 0) jp2_status=opj_end_decompress(jp2_codec,jp2_stream); } if (jp2_status == 0) { opj_stream_destroy(jp2_stream); opj_destroy_codec(jp2_codec); opj_image_destroy(jp2_image); ThrowReaderException(DelegateError,"UnableToDecodeImageFile"); } opj_stream_destroy(jp2_stream); for (i=0; i < (ssize_t) jp2_image->numcomps; i++) { if ((jp2_image->comps[i].dx == 0) || (jp2_image->comps[i].dy == 0)) { opj_destroy_codec(jp2_codec); opj_image_destroy(jp2_image); ThrowReaderException(CoderError,"IrregularChannelGeometryNotSupported") } }
OFCondition DJCompressJP2K::encode( Uint16 columns, Uint16 rows, EP_Interpretation colorSpace, Uint16 samplesPerPixel, Uint16 * image_buffer, Uint16 * & to, Uint32 & length, Uint8 bitsAllocated, Uint8 pixelRepresentation, double minUsed, double maxUsed) { int bitsstored = bitsAllocated; if( samplesPerPixel > 1) bitsstored = bitsAllocated = 8; OFBool isSigned = pixelRepresentation; if( bitsAllocated >= 16) { int amplitude = maxUsed; if( minUsed < 0) amplitude -= minUsed; int bits = 1, value = 2; while( value < amplitude && bits <= 16) { value *= 2; bits++; } if( minUsed < 0) // K A10009536850 22.06.12 bits++; if( bits < 9) bits = 9; // avoid the artifacts... switch to lossless if( (maxUsed >= 32000 && minUsed <= -32000) || maxUsed >= 65000 || bits > 16) quality = 0; if( bits > 16) bits = 16; bitsstored = bits; } opj_cparameters_t parameters; opj_image_t *image = NULL; // printf( "JP2K OPJ-DCMTK-Encode "); opj_set_default_encoder_parameters(¶meters); parameters.tcp_numlayers = 1; parameters.cp_disto_alloc = 1; switch( quality) { case 0: // DCMLosslessQuality parameters.tcp_rates[0] = 0; break; case 1: // DCMHighQuality parameters.tcp_rates[0] = 4; break; case 2: // DCMMediumQuality if( columns <= 600 || rows <= 600) parameters.tcp_rates[0] = 6; else parameters.tcp_rates[0] = 8; break; case 3: // DCMLowQuality parameters.tcp_rates[0] = 16; break; default: //printf( "****** warning unknown compression rate -> MediumQuality : %d", quality); if( columns <= 600 || rows <= 600) parameters.tcp_rates[0] = 6; else parameters.tcp_rates[0] = 8; break; } int image_width = columns; int image_height = rows; int sample_pixel = samplesPerPixel; if (colorSpace == EPI_Monochrome1 || colorSpace == EPI_Monochrome2) { } else { if( sample_pixel != 3) printf( "*** RGB Photometric?, but... SamplesPerPixel != 3 ?"); sample_pixel = 3; } image = rawtoimage( (char *)image_buffer, ¶meters, static_cast<int>( columns*rows*samplesPerPixel*bitsAllocated/8), image_width, image_height, sample_pixel, bitsAllocated, bitsstored, isSigned, quality, 0); if(!image) { fprintf(stderr, "Unable to load buffer image\n"); return EC_Normal; } parameters.cod_format = 0; /* J2K format output */ int codestream_length; opj_codec_t *l_codec = 00; l_codec = opj_create_compress(OPJ_CODEC_J2K); 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); /* setup the encoder parameters using the current image and using user parameters */ opj_setup_encoder(l_codec, ¶meters, image); char * l_out; size_t l_size; FILE * l_file = open_memstream(&l_out, &l_size); opj_stream_t * l_stream = opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE, OPJ_FALSE); if (! l_stream){ fprintf(stderr, "failed to create stream\n"); return EC_Normal; } opj_stream_set_user_data(l_stream, l_file, (opj_stream_free_user_data_fn)NULL); opj_stream_set_write_function(l_stream, (opj_stream_write_fn)_write); /* encode the image */ int bSuccess = opj_start_compress(l_codec, image, l_stream); if (!bSuccess) { fprintf(stderr, "failed to encode image\n"); return EC_Normal; } bSuccess = bSuccess && opj_encode(l_codec, l_stream); if (!bSuccess) { opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); fprintf(stderr, "failed to encode image 2\n"); remove(parameters.outfile); return EC_Normal; } bSuccess = bSuccess && opj_end_compress(l_codec, l_stream); if (!bSuccess) { fprintf(stderr, "failed to encode image: opj_end_compress\n"); return EC_Normal; } /* l_file memstream must be flushed to allow accessint to l_out and l_size values */ fflush(l_file); #ifdef _WIN32 get_buffer_and_size(l_file); #endif fclose(l_file); /* copy compressed stream to "to" UINT8* */ to = new Uint16[l_size]; memcpy( to, l_out, l_size); length = l_size; /* free remaining compression structures */ free(l_out); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); // } return EC_Normal; }
opj_stream_t* OPJ_CALLCONV opj_stream_default_create(OPJ_BOOL l_is_input) { return opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE,l_is_input); }
/** * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream. * @return a stream object. */ opj_stream_t* opj_stream_default_create(bool l_is_input) { return opj_stream_create(J2K_STREAM_CHUNK_SIZE,l_is_input); }
static GstFlowReturn gst_openjpeg_dec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame) { GstOpenJPEGDec *self = GST_OPENJPEG_DEC (decoder); GstFlowReturn ret = GST_FLOW_OK; gint64 deadline; GstMapInfo map; #ifdef HAVE_OPENJPEG_1 opj_dinfo_t *dec; opj_cio_t *io; #else opj_codec_t *dec; opj_stream_t *stream; MemStream mstream; #endif opj_image_t *image; GstVideoFrame vframe; opj_dparameters_t params; GST_DEBUG_OBJECT (self, "Handling frame"); deadline = gst_video_decoder_get_max_decode_time (decoder, frame); if (deadline < 0) { GST_LOG_OBJECT (self, "Dropping too late frame: deadline %" G_GINT64_FORMAT, deadline); ret = gst_video_decoder_drop_frame (decoder, frame); return ret; } dec = opj_create_decompress (self->codec_format); if (!dec) goto initialization_error; #ifdef HAVE_OPENJPEG_1 if (G_UNLIKELY (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_TRACE)) { opj_event_mgr_t callbacks; callbacks.error_handler = gst_openjpeg_dec_opj_error; callbacks.warning_handler = gst_openjpeg_dec_opj_warning; callbacks.info_handler = gst_openjpeg_dec_opj_info; opj_set_event_mgr ((opj_common_ptr) dec, &callbacks, self); } else { opj_set_event_mgr ((opj_common_ptr) dec, NULL, NULL); } #else if (G_UNLIKELY (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_TRACE)) { opj_set_info_handler (dec, gst_openjpeg_dec_opj_info, self); opj_set_warning_handler (dec, gst_openjpeg_dec_opj_warning, self); opj_set_error_handler (dec, gst_openjpeg_dec_opj_error, self); } else { opj_set_info_handler (dec, NULL, NULL); opj_set_warning_handler (dec, NULL, NULL); opj_set_error_handler (dec, NULL, NULL); } #endif params = self->params; if (self->ncomps) params.jpwl_exp_comps = self->ncomps; opj_setup_decoder (dec, ¶ms); if (!gst_buffer_map (frame->input_buffer, &map, GST_MAP_READ)) goto map_read_error; #ifdef HAVE_OPENJPEG_1 io = opj_cio_open ((opj_common_ptr) dec, map.data + (self->is_jp2c ? 8 : 0), map.size - (self->is_jp2c ? 8 : 0)); if (!io) goto open_error; image = opj_decode (dec, io); if (!image) goto decode_error; #else stream = opj_stream_create (4096, OPJ_TRUE); if (!stream) goto open_error; mstream.data = map.data + (self->is_jp2c ? 8 : 0); mstream.offset = 0; mstream.size = map.size - (self->is_jp2c ? 8 : 0); opj_stream_set_read_function (stream, read_fn); opj_stream_set_write_function (stream, write_fn); opj_stream_set_skip_function (stream, skip_fn); opj_stream_set_seek_function (stream, seek_fn); opj_stream_set_user_data (stream, &mstream); opj_stream_set_user_data_length (stream, mstream.size); image = NULL; if (!opj_read_header (stream, dec, &image)) goto decode_error; if (!opj_decode (dec, stream, image)) goto decode_error; #endif gst_buffer_unmap (frame->input_buffer, &map); ret = gst_openjpeg_dec_negotiate (self, image); if (ret != GST_FLOW_OK) goto negotiate_error; ret = gst_video_decoder_allocate_output_frame (decoder, frame); if (ret != GST_FLOW_OK) goto allocate_error; if (!gst_video_frame_map (&vframe, &self->output_state->info, frame->output_buffer, GST_MAP_WRITE)) goto map_write_error; self->fill_frame (&vframe, image); gst_video_frame_unmap (&vframe); #ifdef HAVE_OPENJPEG_1 opj_cio_close (io); opj_image_destroy (image); opj_destroy_decompress (dec); #else opj_end_decompress (dec, stream); opj_stream_destroy (stream); opj_image_destroy (image); opj_destroy_codec (dec); #endif ret = gst_video_decoder_finish_frame (decoder, frame); return ret; initialization_error: { gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, LIBRARY, INIT, ("Failed to initialize OpenJPEG decoder"), (NULL)); return GST_FLOW_ERROR; } map_read_error: { #ifdef HAVE_OPENJPEG_1 opj_destroy_decompress (dec); #else opj_destroy_codec (dec); #endif gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, CORE, FAILED, ("Failed to map input buffer"), (NULL)); return GST_FLOW_ERROR; } open_error: { #ifdef HAVE_OPENJPEG_1 opj_destroy_decompress (dec); #else opj_destroy_codec (dec); #endif gst_buffer_unmap (frame->input_buffer, &map); gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, LIBRARY, INIT, ("Failed to open OpenJPEG stream"), (NULL)); return GST_FLOW_ERROR; } decode_error: { if (image) opj_image_destroy (image); #ifdef HAVE_OPENJPEG_1 opj_cio_close (io); opj_destroy_decompress (dec); #else opj_stream_destroy (stream); opj_destroy_codec (dec); #endif gst_buffer_unmap (frame->input_buffer, &map); gst_video_codec_frame_unref (frame); GST_VIDEO_DECODER_ERROR (self, 1, STREAM, DECODE, ("Failed to decode OpenJPEG stream"), (NULL), ret); return ret; } negotiate_error: { opj_image_destroy (image); #ifdef HAVE_OPENJPEG_1 opj_cio_close (io); opj_destroy_decompress (dec); #else opj_stream_destroy (stream); opj_destroy_codec (dec); #endif gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, ("Failed to negotiate"), (NULL)); return ret; } allocate_error: { opj_image_destroy (image); #ifdef HAVE_OPENJPEG_1 opj_cio_close (io); opj_destroy_decompress (dec); #else opj_stream_destroy (stream); opj_destroy_codec (dec); #endif gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, CORE, FAILED, ("Failed to allocate output buffer"), (NULL)); return ret; } map_write_error: { opj_image_destroy (image); #ifdef HAVE_OPENJPEG_1 opj_cio_close (io); opj_destroy_decompress (dec); #else opj_stream_destroy (stream); opj_destroy_codec (dec); #endif gst_video_codec_frame_unref (frame); GST_ELEMENT_ERROR (self, CORE, FAILED, ("Failed to map output buffer"), (NULL)); return GST_FLOW_ERROR; } }