ICCProfile JPEGInstance::ReadICCProfile() { CheckOpenStream( m_reader, "ReadICCProfile" ); ICCProfile icc = m_reader->ReadICCProfile(); if ( icc.IsProfile() ) if ( m_reader->JPEGOptions().verbosity > 0 ) Console().WriteLn( "<end><cbr>ICC profile extracted: \'" + icc.Description() + "\', " + String( icc.ProfileSize() ) + " bytes." ); return icc; }
void JPCInstance::SetFormatSpecificData( const void* data ) { CheckOpenStream( !m_path.IsEmpty(), "SetFormatSpecificData" ); const JPEG2000FormatOptions* o = JPEG2000FormatOptions::FromGenericDataBlock( data ); if ( o != nullptr ) m_jp2Options = o->options; }
void JPEGInstance::SetFormatSpecificData( const void* data ) { CheckOpenStream( m_writer, "SetFormatSpecificData" ); const JPEGFormat::FormatOptions* o = JPEGFormat::FormatOptions::FromGenericDataBlock( data ); if ( o != nullptr ) m_writer->SetJPEGOptions( o->options ); }
void JPEGInstance::WriteICCProfile( const ICCProfile& icc ) { CheckOpenStream( m_writer, "WriteICCProfile" ); if ( icc.IsProfile() ) { m_writer->WriteICCProfile( icc ); if ( m_writer->JPEGOptions().verbosity > 0 ) Console().WriteLn( "<end><cbr>ICC profile embedded: \'" + icc.Description() + "\', " + String( icc.ProfileSize() ) + " bytes." ); } }
void JPEGInstance::SetOptions( const ImageOptions& newOptions ) { CheckOpenStream( m_writer, "SetOptions" ); ImageOptions options = newOptions; if ( !m_queriedOptions ) { JPEGFormat::EmbeddingOverrides overrides = JPEGFormat::DefaultEmbeddingOverrides(); if ( overrides.overrideICCProfileEmbedding ) options.embedICCProfile = overrides.embedICCProfiles; } m_writer->SetOptions( options ); }
void JPCInstance::SetOptions( const ImageOptions& imageOptions ) { CheckOpenStream( !m_path.IsEmpty(), "SetOptions" ); m_options = imageOptions; // Override embedding options, if requested and not already asked them. if ( !IsCodeStream() && !m_queriedOptions ) { JP2Format::EmbeddingOverrides overrides = JP2Format::DefaultEmbeddingOverrides(); if ( overrides.overrideICCProfileEmbedding ) m_options.embedICCProfile = overrides.embedICCProfiles; } }
ICCProfile JP2Instance::ReadICCProfile() { CheckOpenStream( !m_path.IsEmpty() && s_jasperInitialized, "ReadICCProfile" ); if ( jas_image_cmprof( m_jp2Image ) == nullptr ) return ICCProfile(); jas_stream_t* iccStream = nullptr; try { // Create a growable memory stream for output iccStream = jas_stream_memopen( 0, 0 ); if ( iccStream == nullptr ) JP2KERROR( "Extracting ICC profile from JPEG2000 image: Unable to create JasPer stream" ); if ( jas_iccprof_save( jas_image_cmprof( m_jp2Image )->iccprof, iccStream ) < 0 ) JP2KERROR( "Extracting ICC profile from JPEG2000 image: Error saving profile to JasPer stream" ); long iccSize = jas_stream_tell( iccStream ); if ( iccSize <= 0 ) JP2KERROR( "Extracting ICC profile from JPEG2000 image: Invalid JasPer stream position" ); ByteArray iccData( iccSize ); jas_stream_rewind( iccStream ); if ( jas_stream_read( iccStream, iccData.Begin(), iccSize ) != iccSize ) JP2KERROR( "Extracting ICC profile from JPEG2000 image: Error reading JasPer stream" ); jas_stream_close( iccStream ), iccStream = nullptr; ICCProfile icc( iccData ); if ( icc.IsProfile() ) Console().WriteLn( "<end><cbr>ICC profile extracted: \'" + icc.Description() + "\', " + String( icc.ProfileSize() ) + " bytes." ); return icc; } catch ( ... ) { if ( iccStream != nullptr ) jas_stream_close( iccStream ); Close(); throw; } }
static void ReadJPEGImage( GenericImage<P>& image, JPEGReader* reader, int& readCount ) { CheckOpenStream( reader, "ReadImage" ); /* * The readCount thing is a trick to allow reading the same JPEG image * multiple times from the same format instance. That does not work with * jpeglib. An ugly trick, but hey, works. */ if ( readCount++ ) { AutoPointer<JPEGReader> newReader( new JPEGReader ); newReader->Open( reader->Path() ); newReader->ReadImage( image ); } else reader->ReadImage( image ); }
void JP2Instance::WriteICCProfile( const ICCProfile& icc ) { CheckOpenStream( !m_path.IsEmpty(), "WriteICCProfile" ); if ( m_jp2CMProfile != nullptr ) jas_cmprof_destroy( m_jp2CMProfile ), m_jp2CMProfile = nullptr; if ( icc.IsProfile() ) { jas_iccprof_t* jp2ICCProfile = nullptr; try { ByteArray iccData = icc.ProfileData(); iccData.EnsureUnique(); jp2ICCProfile = jas_iccprof_createfrombuf( iccData.Begin(), int( icc.ProfileSize() ) ); if ( jp2ICCProfile == nullptr ) JP2KERROR( "Unable to generate embedded ICC profile in JPEG2000 image" ); m_jp2CMProfile = jas_cmprof_createfromiccprof( jp2ICCProfile ); if ( m_jp2CMProfile == nullptr ) JP2KERROR( "Unable to embed ICC profile in JPEG2000 image" ); jas_iccprof_destroy( jp2ICCProfile ), jp2ICCProfile = nullptr; Console().WriteLn( "<end><cbr>ICC profile embedded: \'" + icc.Description() + "\', " + String( icc.ProfileSize() ) + " bytes." ); } catch ( ... ) { if ( jp2ICCProfile != nullptr ) jas_iccprof_destroy( jp2ICCProfile ); Close(); throw; } } }
void JPCInstance::CreateImage( const ImageInfo& info ) { CheckOpenStream( !m_path.IsEmpty(), "CreateImage" ); InitJasPer(); if ( !info.IsValid() ) JP2KERROR( "Invalid image parameters in JPEG2000 file creation" ); if ( info.colorSpace != ColorSpace::RGB && info.colorSpace != ColorSpace::Gray ) JP2KERROR( "Unsupported color space in JPEG2000 file creation" ); if ( m_options.bitsPerSample != 8 ) if ( m_jp2Options.lossyCompression || m_options.bitsPerSample < 8 ) m_options.bitsPerSample = 8; else if ( m_options.bitsPerSample != 16 ) m_options.bitsPerSample = 16; if ( m_jp2CMProfile == nullptr ) m_options.embedICCProfile = false; m_options.ieeefpSampleFormat = m_options.complexSample = false; IsoString path8 = #ifdef __PCL_WINDOWS File::UnixPathToWindows( m_path ).ToMBS(); #else m_path.ToUTF8(); #endif m_jp2Stream = jas_stream_fopen( path8.c_str(), "wb" ); if ( m_jp2Stream == nullptr ) JP2KERROR( "Unable to create JPEG2000 file" ); m_jp2Image = jas_image_create0(); if ( m_jp2Image == nullptr ) JP2KERROR( "Unable to create JPEG2000 image" ); for ( int c = 0; c < info.numberOfChannels; ++c ) { jas_image_cmptparm_t p; ::memset( &p, 0, sizeof( jas_image_cmptparm_t ) ); p.tlx = p.tly = 0; // top-left corner position p.hstep = p.vstep = 1; // coordinate grid step sizes p.width = info.width; // width in pixels p.height = info.height; // height in pixels p.prec = m_options.bitsPerSample; // bit depth: 8 or 16 bits p.sgnd = m_jp2Options.signedSample; // signed or unsigned samples if ( jas_image_addcmpt( m_jp2Image, c, &p ) != 0 ) JP2KERROR( "Unable to create JPEG2000 image component" ); } if ( info.colorSpace == ColorSpace::Gray ) { jas_image_setclrspc( m_jp2Image, m_options.embedICCProfile ? JAS_CLRSPC_GENGRAY : JAS_CLRSPC_SGRAY ); jas_image_setcmpttype( m_jp2Image, 0, JAS_IMAGE_CT_COLOR( JAS_CLRSPC_CHANIND_GRAY_Y ) ); if ( info.numberOfChannels > 1 ) jas_image_setcmpttype( m_jp2Image, 1, JAS_IMAGE_CT_COLOR( JAS_IMAGE_CT_OPACITY ) ); } else { jas_image_setclrspc( m_jp2Image, m_options.embedICCProfile ? JAS_CLRSPC_GENRGB : JAS_CLRSPC_SRGB ); jas_image_setcmpttype( m_jp2Image, 0, JAS_IMAGE_CT_COLOR( JAS_CLRSPC_CHANIND_RGB_R ) ); jas_image_setcmpttype( m_jp2Image, 1, JAS_IMAGE_CT_COLOR( JAS_CLRSPC_CHANIND_RGB_G ) ); jas_image_setcmpttype( m_jp2Image, 2, JAS_IMAGE_CT_COLOR( JAS_CLRSPC_CHANIND_RGB_B ) ); if ( info.numberOfChannels > 3 ) jas_image_setcmpttype( m_jp2Image, 3, JAS_IMAGE_CT_COLOR( JAS_IMAGE_CT_OPACITY ) ); } if ( m_options.embedICCProfile ) jas_image_setcmprof( m_jp2Image, m_jp2CMProfile ); if ( m_jp2Options.resolutionData ) { m_jp2Image->rescm_ = m_options.metricResolution; m_jp2Image->hdispres_ = RoundI( m_options.xResolution ); m_jp2Image->vdispres_ = RoundI( m_options.yResolution ); } }
static void WriteJPEGImage( const GenericImage<P>& image, JPEGWriter* writer ) { CheckOpenStream( writer, "WriteImage" ); image.SelectNominalChannels(); // JPEG doesn't support alpha channels writer->WriteImage( image ); }