@@ -421,89 +421,98 @@ namespace IJGVERS {
421421
422422void JPEGCODEC::Decode (DicomPixelData^ oldPixelData, DicomPixelData^ newPixelData, DicomJpegParams^ params, int frame) {
423423 PinnedByteArray^ jpegArray = gcnew PinnedByteArray (oldPixelData->GetFrame (frame)->Data );
424-
425- jpeg_decompress_struct dinfo;
426- memset (&dinfo, 0 , sizeof (dinfo));
427-
428- IJGVERS::SourceManagerStruct src;
429- memset (&src, 0 , sizeof (IJGVERS::SourceManagerStruct));
430- src.pub .init_source = IJGVERS::initSource;
431- src.pub .fill_input_buffer = IJGVERS::fillInputBuffer;
432- src.pub .skip_input_data = IJGVERS::skipInputData;
433- src.pub .resync_to_restart = jpeg_resync_to_restart;
434- src.pub .term_source = IJGVERS::termSource;
435- src.pub .bytes_in_buffer = 0 ;
436- src.pub .next_input_byte = NULL ;
437- src.skip_bytes = 0 ;
438- src.next_buffer = (unsigned char *)(void *)jpegArray->Pointer ;
439- src.next_buffer_size = (unsigned int *)jpegArray->ByteSize ;
440-
441- IJGVERS::ErrorStruct jerr;
442- memset (&jerr, 0 , sizeof (IJGVERS::ErrorStruct));
443- dinfo.err = jpeg_std_error (&jerr.pub );
444- jerr.pub .error_exit = IJGVERS::ErrorExit;
445- jerr.pub .output_message = IJGVERS::OutputMessage;
424+ try {
425+ jpeg_decompress_struct dinfo;
426+ memset (&dinfo, 0 , sizeof (dinfo));
427+
428+ IJGVERS::SourceManagerStruct src;
429+ memset (&src, 0 , sizeof (IJGVERS::SourceManagerStruct));
430+ src.pub .init_source = IJGVERS::initSource;
431+ src.pub .fill_input_buffer = IJGVERS::fillInputBuffer;
432+ src.pub .skip_input_data = IJGVERS::skipInputData;
433+ src.pub .resync_to_restart = jpeg_resync_to_restart;
434+ src.pub .term_source = IJGVERS::termSource;
435+ src.pub .bytes_in_buffer = 0 ;
436+ src.pub .next_input_byte = NULL ;
437+ src.skip_bytes = 0 ;
438+ src.next_buffer = (unsigned char *)(void *)jpegArray->Pointer ;
439+ src.next_buffer_size = (unsigned int *)jpegArray->ByteSize ;
440+
441+ IJGVERS::ErrorStruct jerr;
442+ memset (&jerr, 0 , sizeof (IJGVERS::ErrorStruct));
443+ dinfo.err = jpeg_std_error (&jerr.pub );
444+ jerr.pub .error_exit = IJGVERS::ErrorExit;
445+ jerr.pub .output_message = IJGVERS::OutputMessage;
446446
447- jpeg_create_decompress (&dinfo);
448- dinfo.src = (jpeg_source_mgr*)&src.pub ;
447+ jpeg_create_decompress (&dinfo);
448+ dinfo.src = (jpeg_source_mgr*)&src.pub ;
449449
450- if (jpeg_read_header (&dinfo, TRUE ) == JPEG_SUSPENDED)
451- throw gcnew DicomCodecException (" Unable to decompress JPEG: Suspended" );
450+ if (jpeg_read_header (&dinfo, TRUE ) == JPEG_SUSPENDED)
451+ throw gcnew DicomCodecException (" Unable to decompress JPEG: Suspended" );
452+
453+ if (newPixelData->PhotometricInterpretation == PhotometricInterpretation::YbrFull422 || newPixelData->PhotometricInterpretation == PhotometricInterpretation::YbrPartial422)
454+ newPixelData->PhotometricInterpretation = PhotometricInterpretation::YbrFull;
455+ else
456+ newPixelData->PhotometricInterpretation = oldPixelData->PhotometricInterpretation ;
457+
458+ if (params->ConvertColorspaceToRGB && (dinfo.out_color_space == JCS_YCbCr || dinfo.out_color_space == JCS_RGB)) {
459+ if (oldPixelData->PixelRepresentation == PixelRepresentation::Signed)
460+ throw gcnew DicomCodecException (" JPEG codec unable to perform colorspace conversion on signed pixel data" );
461+ // dinfo.jpeg_color_space = JCS_YCbCr;
462+ dinfo.out_color_space = JCS_RGB;
463+ newPixelData->PhotometricInterpretation = PhotometricInterpretation::Rgb;
464+ newPixelData->PlanarConfiguration = PlanarConfiguration::Interleaved;
465+ }
466+ else {
467+ dinfo.jpeg_color_space = JCS_UNKNOWN;
468+ dinfo.out_color_space = JCS_UNKNOWN;
469+ }
452470
453- if (newPixelData->PhotometricInterpretation == PhotometricInterpretation::YbrFull422 || newPixelData->PhotometricInterpretation == PhotometricInterpretation::YbrPartial422)
454- newPixelData->PhotometricInterpretation = PhotometricInterpretation::YbrFull;
455- else
456- newPixelData->PhotometricInterpretation = oldPixelData->PhotometricInterpretation ;
457-
458- if (params->ConvertColorspaceToRGB && (dinfo.out_color_space == JCS_YCbCr || dinfo.out_color_space == JCS_RGB)) {
459- if (oldPixelData->PixelRepresentation == PixelRepresentation::Signed)
460- throw gcnew DicomCodecException (" JPEG codec unable to perform colorspace conversion on signed pixel data" );
461- // dinfo.jpeg_color_space = JCS_YCbCr;
462- dinfo.out_color_space = JCS_RGB;
463- newPixelData->PhotometricInterpretation = PhotometricInterpretation::Rgb;
464- newPixelData->PlanarConfiguration = PlanarConfiguration::Interleaved;
465- }
466- else {
467- dinfo.jpeg_color_space = JCS_UNKNOWN;
468- dinfo.out_color_space = JCS_UNKNOWN;
469- }
470-
471- if (newPixelData->PhotometricInterpretation == PhotometricInterpretation::YbrFull)
472- newPixelData->PlanarConfiguration = PlanarConfiguration::Planar;
471+ if (newPixelData->PhotometricInterpretation == PhotometricInterpretation::YbrFull)
472+ newPixelData->PlanarConfiguration = PlanarConfiguration::Planar;
473473
474- jpeg_calc_output_dimensions (&dinfo);
475- jpeg_start_decompress (&dinfo);
474+ jpeg_calc_output_dimensions (&dinfo);
475+ jpeg_start_decompress (&dinfo);
476476
477- int rowSize = dinfo.output_width * dinfo.output_components * sizeof (JSAMPLE);
478- int frameSize = rowSize * dinfo.output_height ;
479- if ((frameSize % 2 ) != 0 )
480- frameSize++;
477+ int rowSize = dinfo.output_width * dinfo.output_components * sizeof (JSAMPLE);
478+ int frameSize = rowSize * dinfo.output_height ;
479+ if ((frameSize % 2 ) != 0 )
480+ frameSize++;
481481
482- PinnedByteArray^ frameArray = gcnew PinnedByteArray (frameSize);
483- unsigned char * framePtr = (unsigned char *)(void *)frameArray->Pointer ;
482+ PinnedByteArray^ frameArray = gcnew PinnedByteArray (frameSize);
483+ unsigned char * framePtr = (unsigned char *)(void *)frameArray->Pointer ;
484484
485- while (dinfo.output_scanline < dinfo.output_height ) {
486- int rows = jpeg_read_scanlines (&dinfo, (JSAMPARRAY)&framePtr, 1 );
487- framePtr += rows * rowSize;
488- }
485+ while (dinfo.output_scanline < dinfo.output_height ) {
486+ int rows = jpeg_read_scanlines (&dinfo, (JSAMPARRAY)&framePtr, 1 );
487+ framePtr += rows * rowSize;
488+ }
489489
490- jpeg_destroy_decompress (&dinfo);
490+ jpeg_destroy_decompress (&dinfo);
491491
492- IByteBuffer^ buffer;
493- if (frameArray->Count >= (1 * 1024 * 1024 ) || oldPixelData->NumberOfFrames > 1 )
494- buffer = gcnew TempFileBuffer (frameArray->Data );
495- else
496- buffer = gcnew MemoryByteBuffer (frameArray->Data );
497- buffer = EvenLengthBuffer::Create (buffer);
498-
499- if (newPixelData->PlanarConfiguration == PlanarConfiguration::Planar && newPixelData->SamplesPerPixel > 1 ) {
500- if (oldPixelData->SamplesPerPixel != 3 || oldPixelData->BitsStored > 8 )
501- throw gcnew DicomCodecException (" Planar reconfiguration only implemented for SamplesPerPixel=3 && BitsStores <= 8" );
492+ IByteBuffer^ buffer;
493+ if (frameArray->Count >= (1 * 1024 * 1024 ) || oldPixelData->NumberOfFrames > 1 )
494+ buffer = gcnew TempFileBuffer (frameArray->Data );
495+ else
496+ buffer = gcnew MemoryByteBuffer (frameArray->Data );
497+ buffer = EvenLengthBuffer::Create (buffer);
498+
499+ if (newPixelData->PlanarConfiguration == PlanarConfiguration::Planar && newPixelData->SamplesPerPixel > 1 ) {
500+ if (oldPixelData->SamplesPerPixel != 3 || oldPixelData->BitsStored > 8 )
501+ throw gcnew DicomCodecException (" Planar reconfiguration only implemented for SamplesPerPixel=3 && BitsStores <= 8" );
502502
503- buffer = PixelDataConverter::InterleavedToPlanar24 (buffer);
504- }
503+ buffer = PixelDataConverter::InterleavedToPlanar24 (buffer);
504+ }
505505
506- newPixelData->AddFrame (buffer);
506+ newPixelData->AddFrame (buffer);
507+
508+ delete frameArray;
509+ }
510+ finally{
511+ if (jpegArray != nullptr ){
512+ delete jpegArray;
513+ jpegArray = nullptr ;
514+ }
515+ }
507516}
508517
509518int JPEGCODEC::ScanHeaderForPrecision (DicomPixelData^ pixelData) {
0 commit comments