static void ApplyInverseRealFourierTransform_1( GenericImage<P>& image, const DComplexImage& dft, bool parallel, int maxProcessors ) { DImage tmp; tmp.Status() = image.Status(); image.FreeData(); ApplyInverseRealFourierTransform_2( tmp, dft, parallel, maxProcessors ); image.SetStatusCallback( 0 ); image.Assign( tmp ); image.Status() = tmp.Status(); }
static void ApplyInverseRealFourierTransform( GenericImage<P>& image, const ImageVariant& dft, bool parallel, int maxProcessors ) { if ( !dft || dft->IsEmpty() ) { image.FreeData(); return; } switch ( dft.BitsPerSample() ) { case 32: ApplyInverseRealFourierTransform_1( image, static_cast<const FComplexImage&>( *dft ), parallel, maxProcessors ); break; case 64: ApplyInverseRealFourierTransform_1( image, static_cast<const DComplexImage&>( *dft ), parallel, maxProcessors ); break; } }
template <class P> static void Rotate90CW( GenericImage<P>& image ) { image.SetUnique(); int w = image.Width(); int h = image.Height(); int h1 = h - 1; int n = image.NumberOfChannels(); size_type N = image.NumberOfPixels(); typename GenericImage<P>::color_space cs0 = image.ColorSpace(); StatusMonitor status = image.Status(); typename P::sample** f0 = 0; try { if ( image.Status().IsInitializationEnabled() ) status.Initialize( "Rotate 90 degrees, clockwise", n*N ); f0 = image.ReleaseData(); typename GenericImage<P>::sample_array tmp( N ); for ( int c = 0; c < n; ++c, status += N ) { typename P::sample* f = f0[c]; typename P::sample* t = tmp.Begin(); ::memcpy( t, f, N*P::BytesPerSample() ); for ( int y = 0; y < h; ++y ) for ( int x = 0, h1y = h1-y; x < w; ++x, ++t ) f[x*h + h1y] = *t; } image.ImportData( f0, h, w, n, cs0 ).Status() = status; } catch ( ... ) { if ( f0 != 0 ) { for ( int c = 0; c < n; ++c ) if ( f0[c] != 0 ) image.Allocator().Deallocate( f0[c] ); image.Allocator().Deallocate( f0 ); image.FreeData(); } throw; } }
static void ApplyInverseRealFourierTransform_2( GenericImage<P>& image, const GenericImage<P1>& dft, bool parallel, int maxProcessors ) { if ( dft.IsEmpty() ) { image.FreeData(); return; } int w = dft.Width(); int h = dft.Height(); image.AllocateData( 2*(w - 1), h, dft.NumberOfChannels(), dft.ColorSpace() ); bool statusInitialized = false; if ( image.Status().IsInitializationEnabled() ) { image.Status().Initialize( "Inverse FFT", image.NumberOfChannels()*size_type( w + h ) ); image.Status().DisableInitialization(); statusInitialized = true; } try { FFTR F( h, w, image.Status() ); F.EnableParallelProcessing( parallel, maxProcessors ); for ( int c = 0; c < image.NumberOfChannels(); ++c ) F( image[c], dft[c] ); if ( statusInitialized ) image.Status().EnableInitialization(); } catch ( ... ) { if ( statusInitialized ) image.Status().EnableInitialization(); throw; } }
template <class P> inline static void Apply( GenericImage<P>& image, const IntegerResample& Z ) { int width = image.Width(); int w0 = width; int height = image.Height(); int h0 = height; Z.GetNewSizes( width, height ); if ( width == w0 && height == h0 ) return; if ( width == 0 || height == 0 ) { image.FreeData(); return; } image.EnsureUnique(); typename P::sample* f = 0; typename P::sample** f0 = 0; int n = image.NumberOfChannels(); size_type N = image.NumberOfPixels(); typename GenericImage<P>::color_space cs0 = image.ColorSpace(); StatusMonitor status = image.Status(); int z = pcl::Abs( Z.ZoomFactor() ); int z2 = z*z; int n2 = z2 >> 1; try { if ( status.IsInitializationEnabled() ) { String info = (Z.ZoomFactor() > 0) ? "Upsampling" : "Downsampling"; info.AppendFormat( " %d:%d, %dx%d", (Z.ZoomFactor() > 0) ? z : 1, (Z.ZoomFactor() > 0) ? 1 : z, width, height ); if ( Z.ZoomFactor() < 0 ) { info += ", "; switch ( Z.DownsampleMode() ) { default: case IntegerDownsampleMode::Average: info += "average"; break; case IntegerDownsampleMode::Median: info += "median"; break; case IntegerDownsampleMode::Maximum: info += "maximum"; break; case IntegerDownsampleMode::Minimum: info += "minimum"; break; } } status.Initialize( info, n*N ); } GenericVector<typename P::sample> fm; if ( Z.ZoomFactor() < 0 && Z.DownsampleMode() == IntegerDownsampleMode::Median ) fm = GenericVector<typename P::sample>( z2 ); f0 = image.ReleaseData(); for ( int c = 0; c < n; ++c, status += N ) { f = image.Allocator().AllocatePixels( width, height ); if ( Z.ZoomFactor() > 0 ) { const typename P::sample* f0c = f0[c]; for ( int y = 0; y < h0; ++y ) { int yz = y*z; for ( int x = 0; x < w0; ++x ) { int xz = x*z; typename P::sample v = *f0c++; for ( int i = 0; i < z; ++i ) { typename P::sample* fi = f + (size_type( yz + i )*width + xz); for ( int j = 0; j < z; ++j ) *fi++ = v; } } } } else { typename P::sample* fz = f; for ( int y = 0; y < height; ++y ) { const typename P::sample* fy = f0[c] + size_type( y )*z*w0; for ( int x = 0; x < width; ++x ) { const typename P::sample* fyx = fy + x*z; switch ( Z.DownsampleMode() ) { default: case IntegerDownsampleMode::Average: { double s = 0; for ( int i = 0; i < z; ++i, fyx += w0 ) for ( int j = 0; j < z; ++j ) s += fyx[j]; *fz++ = typename P::sample( P::IsFloatSample() ? s/z2 : Round( s/z2 ) ); } break; case IntegerDownsampleMode::Median: { typename P::sample* fmi = *fm; for ( int i = 0; i < z; ++i, fyx += w0 ) for ( int j = 0; j < z; ++j ) *fmi++ = fyx[j]; *fz++ = (z & 1) ? *Select( *fm, fm.At( z2 ), n2 ) : P::FloatToSample( 0.5*(double( *Select( *fm, fm.At( z2 ), n2 ) ) + double( *Select( *fm, fm.At( z2 ), n2-1 ) )) ); } break; case IntegerDownsampleMode::Maximum: { *fz = P::MinSampleValue(); for ( int i = 0; i < z; ++i, fyx += w0 ) for ( int j = 0; j < z; ++j ) if ( fyx[j] > *fz ) *fz = fyx[j]; ++fz; } break; case IntegerDownsampleMode::Minimum: { *fz = P::MaxSampleValue(); for ( int i = 0; i < z; ++i, fyx += w0 ) for ( int j = 0; j < z; ++j ) if ( fyx[j] < *fz ) *fz = fyx[j]; ++fz; } break; } } } } image.Allocator().Deallocate( f0[c] ); f0[c] = f; f = 0; } image.ImportData( f0, width, height, n, cs0 ).Status() = status; } catch ( ... ) { if ( f != 0 ) image.Allocator().Deallocate( f ); if ( f0 != 0 ) { for ( int c = 0; c < n; ++c ) if ( f0[c] != 0 ) image.Allocator().Deallocate( f0[c] ); image.Allocator().Deallocate( f0 ); } image.FreeData(); throw; } }
template <class P> static void Apply( GenericImage<P>& image, const Translation& translation ) { if ( translation.Delta() == 0.0 ) return; int width = image.Width(); int height = image.Height(); if ( width == 0 || height == 0 ) return; image.EnsureUnique(); typename P::sample* f = nullptr; typename P::sample** f0 = nullptr; int n = image.NumberOfChannels(); typename GenericImage<P>::color_space cs0 = image.ColorSpace(); StatusMonitor status = image.Status(); int numberOfThreads = translation.IsParallelProcessingEnabled() ? Min( translation.MaxProcessors(), pcl::Thread::NumberOfThreads( height, 1 ) ) : 1; int rowsPerThread = height/numberOfThreads; try { size_type N = size_type( width )*size_type( height ); if ( status.IsInitializationEnabled() ) status.Initialize( String().Format( "Translate dx=%.3lf, dy=%.3lf, ", translation.Delta().x, translation.Delta().y ) + translation.Interpolation().Description(), size_type( n )*N ); f0 = image.ReleaseData(); for ( int c = 0; c < n; ++c ) { ThreadData<P> data( translation.Delta(), width, height, status, N ); data.f = f = image.Allocator().AllocatePixels( size_type( width )*size_type( height ) ); data.fillValue = (c < translation.FillValues().Length()) ? P::ToSample( translation.FillValues()[c] ) : P::MinSampleValue(); ReferenceArray<Thread<P> > threads; for ( int i = 0, j = 1; i < numberOfThreads; ++i, ++j ) threads.Add( new Thread<P>( data, translation.Interpolation().NewInterpolator<P>( f0[c], width, height ), i*rowsPerThread, (j < numberOfThreads) ? j*rowsPerThread : height ) ); AbstractImage::RunThreads( threads, data ); threads.Destroy(); image.Allocator().Deallocate( f0[c] ); f0[c] = f; f = nullptr; status = data.status; } image.ImportData( f0, width, height, n, cs0 ).Status() = status; } catch ( ... ) { if ( f != nullptr ) image.Allocator().Deallocate( f ); if ( f0 != nullptr ) { for ( int c = 0; c < n; ++c ) if ( f0[c] != nullptr ) image.Allocator().Deallocate( f0[c] ); image.Allocator().Deallocate( f0 ); } image.FreeData(); throw; } }
static void ReadJPEGImage( GenericImage<P>& img, JPEGReader& reader, JPEGFileData* fileData ) { if ( !reader.IsOpen() ) throw JPEG::InvalidReadOperation( String() ); JSAMPLE* buffer = nullptr; // one-row sample array for scanline reading typename P::sample** v = nullptr; // pointers to destination scan lines try { // Set parameters for decompression. // Most parameters have already been established by Open(). // We just ensure that we'll get either a grayscale or a RGB color image. if ( jpeg_decompressor->out_color_space != JCS_GRAYSCALE ) jpeg_decompressor->out_color_space = JCS_RGB; // Start decompressor. ::jpeg_start_decompress( jpeg_decompressor ); // Allocate pixel data. img.AllocateData( jpeg_decompressor->output_width, jpeg_decompressor->output_height, jpeg_decompressor->output_components, (jpeg_decompressor->out_color_space == JCS_GRAYSCALE) ? ColorSpace::Gray : ColorSpace::RGB ); // Initialize status callback. if ( img.Status().IsInitializationEnabled() ) img.Status().Initialize( String().Format( "Decompressing JPEG: %d channel(s), %dx%d pixels", img.NumberOfChannels(), img.Width(), img.Height() ), img.NumberOfSamples() ); // // Read pixels row by row. // // JSAMPLEs per row in output buffer. int row_stride = img.Width() * img.NumberOfChannels(); // Make a one-row-high sample array. buffer = new JSAMPLE[ row_stride ]; // JPEG organization is chunky; PCL images are planar. v = new typename P::sample*[ img.NumberOfChannels() ]; while ( jpeg_decompressor->output_scanline < jpeg_decompressor->output_height ) { ::jpeg_read_scanlines( jpeg_decompressor, &buffer, 1 ); const JSAMPLE* b = buffer; for ( int c = 0; c < img.NumberOfChannels(); ++c ) v[c] = img.ScanLine( jpeg_decompressor->output_scanline-1, c ); for ( int i = 0; i < img.Width(); ++i ) for ( int c = 0; c < img.NumberOfChannels(); ++c, ++b ) *v[c]++ = P::IsFloatSample() ? typename P::sample( *b ) : P::ToSample( *b ); img.Status() += img.Width()*img.NumberOfChannels(); } // Clean up temporary structures. delete [] v, v = nullptr; delete [] buffer, buffer = nullptr; // Finish decompression. ::jpeg_finish_decompress( jpeg_decompressor ); // ### TODO --> At this point we might check whether any corrupt-data // warnings occurred (test whether jerr.pub.num_warnings is nonzero). } catch ( ... ) { reader.Close(); if ( buffer != nullptr ) delete [] buffer; if ( v != nullptr ) delete [] v; img.FreeData(); throw; } }
template <class P> static void Apply( GenericImage<P>& image, const Resample& resample ) { int width = image.Width(); int w0 = width; int height = image.Height(); int h0 = height; resample.GetNewSizes( width, height ); if ( width == w0 && height == h0 ) return; if ( width <= 0 || height <= 0 ) { image.FreeData(); return; } image.EnsureUnique(); typename P::sample* f = nullptr; typename P::sample** f0 = nullptr; int n = image.NumberOfChannels(); typename GenericImage<P>::color_space cs0 = image.ColorSpace(); double rx = double( w0 )/width; double ry = double( h0 )/height; StatusMonitor status = image.Status(); int numberOfThreads = resample.IsParallelProcessingEnabled() ? Min( resample.MaxProcessors(), pcl::Thread::NumberOfThreads( height, 1 ) ) : 1; int rowsPerThread = height/numberOfThreads; try { size_type N = size_type( width )*size_type( height ); if ( status.IsInitializationEnabled() ) status.Initialize( String().Format( "Resampling to %dx%d px, ", width, height ) + resample.Interpolation().Description(), size_type( n )*N ); f0 = image.ReleaseData(); for ( int c = 0; c < n; ++c ) { ThreadData<P> data( rx, ry, width, status, N ); data.f = f = image.Allocator().AllocatePixels( width, height ); ReferenceArray<Thread<P> > threads; for ( int i = 0, j = 1; i < numberOfThreads; ++i, ++j ) threads.Add( new Thread<P>( data, resample.Interpolation().NewInterpolator<P>( f0[c], w0, h0 ), i*rowsPerThread, (j < numberOfThreads) ? j*rowsPerThread : height ) ); AbstractImage::RunThreads( threads, data ); threads.Destroy(); image.Allocator().Deallocate( f0[c] ); f0[c] = f; f = nullptr; status = data.status; } image.ImportData( f0, width, height, n, cs0 ).Status() = status; } catch ( ... ) { if ( f != nullptr ) image.Allocator().Deallocate( f ); if ( f0 != nullptr ) { for ( int c = 0; c < n; ++c ) if ( f0[c] != nullptr ) image.Allocator().Deallocate( f0[c] ); image.Allocator().Deallocate( f0 ); } image.FreeData(); throw; } }