Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85502 - in sandbox/gil/boost/gil/extension/io2: . backends/detail backends/gp backends/libjpeg backends/libpng backends/libtiff
From: dsaritz_at_[hidden]
Date: 2013-08-28 13:55:45


Author: psiha
Date: 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013)
New Revision: 85502
URL: http://svn.boost.org/trac/boost/changeset/85502

Log:
Partially converted LibJPEG, LibPNG and GDI+ backends to the new file and class layout.

Added:
   sandbox/gil/boost/gil/extension/io2/backends/gp/
   sandbox/gil/boost/gil/extension/io2/backends/gp/backend.hpp
      - copied, changed from r85344, sandbox/gil/boost/gil/extension/io2/gp_image.hpp
   sandbox/gil/boost/gil/extension/io2/backends/gp/reader.hpp
      - copied unchanged from r85344, sandbox/gil/boost/gil/extension/io2/gp_image.hpp
   sandbox/gil/boost/gil/extension/io2/backends/gp/writer.hpp
      - copied unchanged from r85344, sandbox/gil/boost/gil/extension/io2/gp_image.hpp
   sandbox/gil/boost/gil/extension/io2/backends/libjpeg/
   sandbox/gil/boost/gil/extension/io2/backends/libjpeg/backend.hpp
      - copied, changed from r85344, sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp
   sandbox/gil/boost/gil/extension/io2/backends/libjpeg/reader.hpp
      - copied, changed from r85344, sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp
   sandbox/gil/boost/gil/extension/io2/backends/libjpeg/writer.hpp
      - copied, changed from r85344, sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp
   sandbox/gil/boost/gil/extension/io2/backends/libpng/
   sandbox/gil/boost/gil/extension/io2/backends/libpng/backend.hpp
      - copied, changed from r85344, sandbox/gil/boost/gil/extension/io2/libpng_image.hpp
   sandbox/gil/boost/gil/extension/io2/backends/libpng/reader.hpp
      - copied, changed from r85344, sandbox/gil/boost/gil/extension/io2/libpng_image.hpp
   sandbox/gil/boost/gil/extension/io2/backends/libpng/writer.hpp
      - copied, changed from r85344, sandbox/gil/boost/gil/extension/io2/libpng_image.hpp
Deleted:
   sandbox/gil/boost/gil/extension/io2/gp_image.hpp
   sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp
   sandbox/gil/boost/gil/extension/io2/libpng_image.hpp
Text files modified:
   sandbox/gil/boost/gil/extension/io2/backends/detail/backend.hpp | 4
   sandbox/gil/boost/gil/extension/io2/backends/gp/backend.hpp | 465 ----------------
   sandbox/gil/boost/gil/extension/io2/backends/gp/reader.hpp | 784 ++++++++++++++++++++++++++++
   sandbox/gil/boost/gil/extension/io2/backends/gp/writer.hpp | 784 ++++++++++++++++++++++++++++
   sandbox/gil/boost/gil/extension/io2/backends/libjpeg/backend.hpp | 958 +++-------------------------------
   sandbox/gil/boost/gil/extension/io2/backends/libjpeg/reader.hpp | 484 ----------------
   sandbox/gil/boost/gil/extension/io2/backends/libjpeg/writer.hpp | 855 ------------------------------
   sandbox/gil/boost/gil/extension/io2/backends/libpng/backend.hpp | 540 +-----------------
   sandbox/gil/boost/gil/extension/io2/backends/libpng/reader.hpp | 441 ---------------
   sandbox/gil/boost/gil/extension/io2/backends/libpng/writer.hpp | 595 ---------------------
   sandbox/gil/boost/gil/extension/io2/backends/libtiff/writer.hpp | 4
   /dev/null | 784 ----------------------------
   /dev/null | 1095 ----------------------------------------
   /dev/null | 759 ---------------------------
   14 files changed, 1803 insertions(+), 6749 deletions(-)

Modified: sandbox/gil/boost/gil/extension/io2/backends/detail/backend.hpp
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/backends/detail/backend.hpp Wed Aug 28 09:54:25 2013 (r85501)
+++ sandbox/gil/boost/gil/extension/io2/backends/detail/backend.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85502)
@@ -224,6 +224,8 @@
         /// MPL vector of pixel<> specializations supported by the backend.
     typedef typename backend_traits<Impl>::supported_pixel_formats_t supported_pixel_formats;
 
+ /// Native backend <-> GIL format mapping and conversion
+ ///
     template <typename PixelType, bool IsPlanar>
     struct native_format
         : backend_traits<Impl>::gil_to_native_format:: BOOST_NESTED_TEMPLATE apply<PixelType, IsPlanar>::type
@@ -256,6 +258,8 @@
 
     // https://bugzilla.gnome.org/show_bug.cgi?id=705937
         /// \name IO: readers and writers
+ /// GIL::IO2 offers separate classes for reading and writing images as this
+ /// best maps to usual use cases and intended workflows of individual backends
         /// @{
 
     template <typename Source> struct reader_for : gil::io::reader_for<Impl, Source> {}; ///< Returns the class capable of reading images from the Source device

Copied and modified: sandbox/gil/boost/gil/extension/io2/backends/gp/backend.hpp (from r85344, sandbox/gil/boost/gil/extension/io2/gp_image.hpp)
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/gp_image.hpp Tue Aug 13 18:43:39 2013 (r85344, copy source)
+++ sandbox/gil/boost/gil/extension/io2/backends/gp/backend.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85502)
@@ -1,7 +1,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 ///
-/// \file gp_image.hpp
-/// ------------------
+/// \file gp/backend.hpp
+/// --------------------
 ///
 /// Base IO interface GDI+ implementation.
 ///
@@ -16,9 +16,9 @@
 ///
 ////////////////////////////////////////////////////////////////////////////////
 //------------------------------------------------------------------------------
+#ifndef backend_hpp__3B1ED5BC_42C6_4EC6_B700_01C1B8646431
+#define backend_hpp__3B1ED5BC_42C6_4EC6_B700_01C1B8646431
 #pragma once
-#ifndef gp_private_base_hpp__3B1ED5BC_42C6_4EC6_B700_01C1B8646431
-#define gp_private_base_hpp__3B1ED5BC_42C6_4EC6_B700_01C1B8646431
 //------------------------------------------------------------------------------
 #include "detail/io_error.hpp"
 #include "detail/gp_extern_lib_guard.hpp"
@@ -167,93 +167,6 @@
         : Gdiplus::Rect( Gdiplus::Point( top_left.x, top_left.y ), Gdiplus::Size( bottom_right.x - top_left.x, bottom_right.y - top_left.y ) ) {}
 };
 
-
-class gp_writer : public open_on_write_writer
-{
-public:
- typedef std::pair<Gdiplus::GpBitmap *, Gdiplus::EncoderParameters *> lib_object_t;
-
- // The passed View object must outlive the GpBitmap object (GDI+ uses lazy
- // evaluation).
- template <class View>
- explicit gp_writer( View & view )
- {
- BOOST_STATIC_ASSERT(( gp_is_supported<typename View::value_type, is_planar<View>::value>::value ));
-
- // http://msdn.microsoft.com/en-us/library/ms536315(VS.85).aspx
- // stride has to be a multiple of 4 bytes
- BOOST_ASSERT( !( view.pixels().row_size() % sizeof( Gdiplus::ARGB ) ) );
-
- ensure_result
- (
- Gdiplus::DllExports::GdipCreateBitmapFromScan0
- (
- view.width (),
- view.height(),
- view.pixels().row_size(),
- gil_to_gp_format<typename View::value_type, is_planar<View>::value>::value,
- backend_base::get_raw_data( view ),
- &lib_object().first
- )
- );
- BOOST_ASSERT( lib_object().first );
- }
-
- ~gp_writer()
- {
- detail::verify_result( Gdiplus::DllExports::GdipDisposeImage( lib_object().first ) );
- }
-
- template <typename Target>
- void write_default( Target const & target, format_tag const format )
- {
- BOOST_ASSERT( lib_object().second == NULL );
- write( target, format );
- }
-
- void write( char const * const pFilename, format_tag const format ) const
- {
- write( detail::wide_path( pFilename ), format );
- }
-
- void write( wchar_t const * const pFilename, format_tag const format ) const
- {
- detail::ensure_result
- (
- Gdiplus::DllExports::GdipSaveImageToFile
- (
- lib_object().first,
- pFilename,
- &encoder_id( format ),
- lib_object().second
- )
- );
- }
-
- lib_object_t & lib_object() { return lib_instance_; }
- lib_object_t const & lib_object() const { return lib_instance_; }
-
-private:
- static CLSID const & encoder_id( format_tag const format )
- {
- static CLSID const ids[ number_of_known_formats ] =
- {
- { 0x557CF400, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // BMP
- { 0x557CF402, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // GIF
- { 0x557CF401, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // JPEG
- { 0x557CF406, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // PNG
- { 0x557CF405, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // TIFF
- CLSID_NULL // TGA
- };
- BOOST_ASSERT( ids[ format ] != CLSID_NULL );
- return ids[ format ];
- }
-
-private:
- lib_object_t lib_instance_;
-};
-
-
 class gp_view_base;
 
 //------------------------------------------------------------------------------
@@ -347,7 +260,7 @@
 };
 
 
-#if defined(BOOST_MSVC)
+#if defined( BOOST_MSVC )
 # pragma warning( push )
 # pragma warning( disable : 4127 ) // "conditional expression is constant"
 #endif
@@ -361,26 +274,6 @@
     typedef detail::gp_user_guard guard;
 
 public: /// \ingroup Construction
- explicit gp_image( wchar_t const * const filename )
- {
- detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromFile( filename, &pBitmap_ ) );
- BOOST_ASSERT( pBitmap_ );
- }
-
- explicit gp_image( char const * const filename )
- {
- detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromFile( detail::wide_path( filename ), &pBitmap_ ) );
- BOOST_ASSERT( pBitmap_ );
- }
-
- // The passed IStream object must outlive the GpBitmap object (GDI+ uses
- // lazy evaluation).
- explicit gp_image( IStream & stream )
- {
- detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromStream( &stream, &pBitmap_ ) );
- BOOST_ASSERT( pBitmap_ );
- }
-
     ~gp_image()
     {
         detail::verify_result( Gdiplus::DllExports::GdipDisposeImage( pBitmap_ ) );
@@ -402,53 +295,6 @@
         return pixel_format;
     }
 
- format_t closest_gil_supported_format() const
- {
- //http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.gdi/2008-01/msg00044.html
- switch ( format() )
- {
- case PixelFormat16bppGrayScale:
- case PixelFormat48bppRGB :
- case PixelFormat32bppRGB :
- case PixelFormat24bppRGB :
- return
- PixelFormat24bppRGB;
-
- case PixelFormat64bppPARGB :
- case PixelFormat32bppPARGB :
- case PixelFormat64bppARGB :
- case PixelFormat32bppARGB :
- case PixelFormat16bppARGB1555 :
- return
- PixelFormat32bppARGB;
-
- case PixelFormat16bppRGB565 :
- case PixelFormat16bppRGB555 :
- case PixelFormat8bppIndexed :
- case PixelFormat4bppIndexed :
- case PixelFormat1bppIndexed :
- return
- PixelFormat16bppRGB565;
-
- case PixelFormat32bppCMYK:
- return
- #if (GDIPVER >= 0x0110)
- PixelFormat32bppCMYK;
- #else
- PixelFormat24bppRGB;
- #endif // (GDIPVER >= 0x0110)
-
- default:
- BF_UNREACHABLE_CODE
- return PixelFormatUndefined;
- }
- }
-
- image_type_id current_image_format_id() const
- {
- return image_format_id( closest_gil_supported_format() );
- }
-
     static image_type_id image_format_id( format_t const closest_gil_supported_format )
     {
         switch ( closest_gil_supported_format )
@@ -466,319 +312,22 @@
         }
     }
 
-public: // Low-level (row, strip, tile) access
- static bool can_do_roi_access() { return true; }
-
- class sequential_row_read_state
- :
- private detail::cumulative_result
- {
- public:
- using detail::cumulative_result::failed;
- void throw_if_error() const { detail::cumulative_result::throw_if_error( "GDI+ failure" ); }
-
- BOOST_STATIC_CONSTANT( bool, throws_on_error = false );
-
- private:
- sequential_row_read_state( gp_image const & source_image )
- :
- roi_( 0, 0, source_image.dimensions().x, 1 )
- {
- bitmapData_.Width = roi_.Width;
- bitmapData_.Height = 1;
- bitmapData_.PixelFormat = source_image.format();
- bitmapData_.Stride = bitmapData_.Width * source_image.cached_format_size( bitmapData_.PixelFormat );
- bitmapData_.Reserved = 0;
- }
-
- private: friend gp_image;
- Gdiplus::Rect roi_ ;
- Gdiplus::BitmapData bitmapData_;
- };
-
- sequential_row_read_state begin_sequential_row_read() const { return sequential_row_read_state( *this ); }
-
- /// \todo Kill duplication with raw_convert_to_prepared_view().
- /// (04.01.2011.) (Domagoj Saric)
- void read_row( sequential_row_read_state & state, unsigned char * const p_row_storage ) const
- {
- using namespace detail ;
- using namespace Gdiplus;
-
- state.bitmapData_.Scan0 = p_row_storage;
-
- state.accumulate_equal
- (
- DllExports::GdipBitmapLockBits
- (
- pBitmap_,
- &state.roi_,
- ImageLockModeRead | ImageLockModeUserInputBuf,
- state.bitmapData_.PixelFormat,
- &state.bitmapData_
- ),
- Gdiplus::Ok
- );
- verify_result( DllExports::GdipBitmapUnlockBits( pBitmap_, &state.bitmapData_ ) );
-
- ++state.roi_.Y;
- }
-
     ::Gdiplus::GpBitmap & lib_object() { return *pBitmap_; }
     ::Gdiplus::GpBitmap const & lib_object() const { return const_cast<gp_image &>( *this ).lib_object(); }
 
-private: // Private backend_base interface.
- friend class base_t;
-
- template <class MyView, class TargetView, class Converter>
- void generic_convert_to_prepared_view( TargetView const & view, Converter const & converter ) const
- {
- BOOST_ASSERT( !dimensions_mismatch( view ) );
- //BOOST_ASSERT( !formats_mismatch ( view ) );
-
- using namespace detail ;
- using namespace Gdiplus;
-
- point2<std::ptrdiff_t> const & targetDimensions( original_view( view ).dimensions() );
- gp_roi const roi( get_offset<gp_roi::offset_t>( view ), targetDimensions.x, targetDimensions.y );
- format_t const my_format( gil_to_gp_format<typename View::value_type, is_planar<View>::value>::value );
- BitmapData bitmapData;
- ensure_result
- (
- DllExports::GdipBitmapLockBits
- (
- pBitmap_,
- &roi,
- ImageLockModeRead,
- my_format,
- &bitmapData
- )
- );
- BOOST_ASSERT( bitmapData.PixelFormat == my_format );
- copy_and_convert_pixels // This must not throw!
- (
- interleaved_view
- (
- bitmapData.Width ,
- bitmapData.Height,
- gil_reinterpret_cast_c<typename MyView::value_type const *>( bitmapData.Scan0 ),
- bitmapData.Stride
- ),
- view,
- converter
- );
- verify_result( DllExports::GdipBitmapUnlockBits( pBitmap_, &bitmapData ) );
- }
-
-
- void raw_convert_to_prepared_view( view_data_t const & view_data ) const
- {
- BOOST_ASSERT( view_data.Scan0 );
-
- using namespace detail ;
- using namespace Gdiplus;
-
- BitmapData * const pMutableBitmapData( const_cast<BitmapData *>( static_cast<BitmapData const *>( &view_data ) ) );
- GpStatus const load_result
- (
- DllExports::GdipBitmapLockBits
- (
- pBitmap_,
- view_data.p_roi_,
- ImageLockModeRead | ImageLockModeUserInputBuf,
- view_data.PixelFormat,
- pMutableBitmapData
- )
- );
- GpStatus const unlock_result( DllExports::GdipBitmapUnlockBits( pBitmap_, pMutableBitmapData ) );
- ensure_result( load_result );
- verify_result( unlock_result );
- }
-
-
- void raw_copy_to_prepared_view( view_data_t const & view_data ) const
- {
- BOOST_ASSERT( view_data.Width == static_cast<UINT>( dimensions().x ) );
- BOOST_ASSERT( view_data.Height == static_cast<UINT>( dimensions().y ) );
- //...this need not hold as it can be used to perform GDI+ default
- //internal colour conversion...maybe i'll provide another worker
- //function...
- //BOOST_ASSERT( view_data.PixelFormat == format () );
- raw_convert_to_prepared_view( view_data );
- }
-
-
- static std::size_t cached_format_size( format_t const format )
- {
- return Gdiplus::GetPixelFormatSize( format );
- }
-
-private:
- template <Gdiplus::PixelFormat desired_format>
- void pre_palettized_conversion( mpl::true_ /*is_indexed*/ )
- {
- #if (GDIPVER >= 0x0110)
- // A GDI+ 1.1 (a non-distributable version, distributed with MS Office
- // 2003 and MS Windows Vista and MS Windows 7) 'enhanced'/'tuned'
- // version of the conversion routine for indexed/palettized image
- // formats. Unless/until proven useful, pretty much still a GDI+ 1.1
- // tester...
-
- BOOST_ASSERT( !has_alpha<desired_format>::value && "Is this possible for indexed formats?" );
- std::size_t const number_of_pixel_bits ( pixel_size<desired_format>::value );
- std::size_t const number_of_palette_colours( static_cast<std::size_t>( ( 2 << ( number_of_pixel_bits - 1 ) ) - 1 ) );
- std::size_t const palette_size ( sizeof( ColorPalette ) + number_of_palette_colours * sizeof( ARGB ) );
- aligned_storage
- <
- palette_size,
- alignment_of<ColorPalette>::value
- >::type palette_storage;
- ColorPalette & palette( *static_cast<ColorPalette *>( palette_storage.address() ) );
- palette.Flags = ( PaletteFlagsHasAlpha * has_alpha<desired_format>::value ) |
- ( PaletteFlagsGrayScale * is_same<typename color_space_type<View>::type, gray_t>::value );
- palette.Count = number_of_palette_colours;
-
- verify_result
- (
- DllExports::GdipInitializePalette
- (
- &palette,
- PaletteTypeOptimal,
- number_of_palette_colours,
- has_alpha<desired_format>::value,
- pBitmap_
- )
- );
-
- ensure_result
- (
- DllExports::GdipBitmapConvertFormat
- (
- pBitmap_,
- desired_format,
- DitherTypeErrorDiffusion,
- PaletteTypeOptimal,
- &palette,
- 50
- )
- );
- #endif // (GDIPVER >= 0x0110)
- }
-
- template <Gdiplus::PixelFormat desired_format>
- void pre_palettized_conversion( mpl::false_ /*not is_indexed*/ ) const {}
-
 private:
     friend class detail::gp_view_base;
 
     Gdiplus::GpBitmap * /*const*/ pBitmap_;
 };
 
-#if defined(BOOST_MSVC)
+#if defined( BOOST_MSVC )
 # pragma warning( pop )
 #endif
 
-namespace detail
-{
-//------------------------------------------------------------------------------
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class gp_view_base
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class gp_view_base : noncopyable
-{
-public:
- ~gp_view_base()
- {
- detail::verify_result( Gdiplus::DllExports::GdipBitmapUnlockBits( &bitmap_, &bitmapData_ ) );
- }
-
-protected:
- gp_view_base( gp_image & bitmap, unsigned int const lock_mode, gp_image::roi const * const p_roi = 0 )
- :
- bitmap_( *bitmap.pBitmap_ )
- {
- std::memset( &bitmapData_, 0, sizeof( bitmapData_ ) );
-
- detail::ensure_result
- (
- Gdiplus::DllExports::GdipBitmapLockBits
- (
- &bitmap_,
- p_roi,
- lock_mode,
- bitmap.format(),
- &bitmapData_
- )
- );
- }
-
- template <typename Pixel>
- typename type_from_x_iterator<Pixel *>::view_t
- get_typed_view()
- {
- //todo assert correct type...
- interleaved_view<Pixel *>( bitmapData_.Width, bitmapData_.Height, bitmapData_.Scan0, bitmapData_.Stride );
- }
-
-private:
- Gdiplus::GpBitmap & bitmap_ ;
- Gdiplus::BitmapData bitmapData_;
-};
-
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class gp_view
-///
-////////////////////////////////////////////////////////////////////////////////
-
-template <typename Pixel>
-class gp_view
- :
- private gp_view_base,
- public type_from_x_iterator<Pixel *>::view_t
-{
-public:
- gp_view( gp_image & image, gp_image::roi const * const p_roi = 0 )
- :
- gp_view_base( image, ImageLockModeRead | ( is_const<Pixel>::value * ImageLockModeWrite ), p_roi ),
- type_from_x_iterator<Pixel *>::view_t( get_typed_view<Pixel>() )
- {}
-};
-
-
-//...mhmh...to be seen if necessary...
-//template <typename Pixel>
-//class gp_const_view
-// :
-// private gp_view_base,
-// public type_from_x_iterator<Pixel const *>::view_t
-//{
-//public:
-// gp_const_view( gp_image & image, gp_image::roi const * const p_roi = 0 )
-// :
-// gp_view_base( image, ImageLockModeRead ),
-// type_from_x_iterator<Pixel const *>::view_t( get_typed_view<Pixel const>() )
-// {}
-//};
-
-
-//...mhmh...to be implemented...
-//template <class Impl, class SupportedPixelFormats, class ROI>
-//inline
-//typename backend<Impl, SupportedPixelFormats, ROI>::view_t
-//view( backend<Impl, SupportedPixelFormats, ROI> & img );// { return img._view; }
-
-//------------------------------------------------------------------------------
-} // namespace detail
 //------------------------------------------------------------------------------
 } // namespace gil
 //------------------------------------------------------------------------------
 } // namespace boost
 //------------------------------------------------------------------------------
-#endif // gp_private_base_hpp
+#endif // backend_hpp

Copied: sandbox/gil/boost/gil/extension/io2/backends/gp/reader.hpp (from r85344, sandbox/gil/boost/gil/extension/io2/gp_image.hpp)
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ sandbox/gil/boost/gil/extension/io2/backends/gp/reader.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85502, copy of r85344, sandbox/gil/boost/gil/extension/io2/gp_image.hpp)
@@ -0,0 +1,784 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// \file gp_image.hpp
+/// ------------------
+///
+/// Base IO interface GDI+ implementation.
+///
+/// Copyright (c) 2010.-2013. Domagoj Saric
+///
+/// Use, modification and distribution is subject to the
+/// Boost Software License, Version 1.0.
+/// (See accompanying file LICENSE_1_0.txt or copy at
+/// http://www.boost.org/LICENSE_1_0.txt)
+///
+/// For more information, see http://www.boost.org
+///
+////////////////////////////////////////////////////////////////////////////////
+//------------------------------------------------------------------------------
+#pragma once
+#ifndef gp_private_base_hpp__3B1ED5BC_42C6_4EC6_B700_01C1B8646431
+#define gp_private_base_hpp__3B1ED5BC_42C6_4EC6_B700_01C1B8646431
+//------------------------------------------------------------------------------
+#include "detail/io_error.hpp"
+#include "detail/gp_extern_lib_guard.hpp"
+#include "detail/windows_shared.hpp"
+#include "detail/windows_shared_istreams.hpp"
+#include "backend.hpp"
+
+#include <boost/array.hpp>
+#include <boost/mpl/eval_if.hpp>
+#include <boost/mpl/integral_c.hpp>
+#include <boost/range/iterator_range.hpp>
+#include <boost/static_assert.hpp>
+#include <boost/type_traits/is_pod.hpp>
+
+#include <algorithm>
+//------------------------------------------------------------------------------
+namespace boost
+{
+//------------------------------------------------------------------------------
+namespace gil
+{
+//------------------------------------------------------------------------------
+namespace detail
+{
+//------------------------------------------------------------------------------
+
+template <Gdiplus::PixelFormat gp_format> struct is_canonical : mpl::bool_ <(gp_format & PixelFormatCanonical) != 0> {};
+template <Gdiplus::PixelFormat gp_format> struct is_extended : mpl::bool_ <(gp_format & PixelFormatExtended ) != 0> {};
+template <Gdiplus::PixelFormat gp_format> struct is_indexed : mpl::bool_ <(gp_format & PixelFormatIndexed ) != 0> {};
+template <Gdiplus::PixelFormat gp_format> struct has_alpha : mpl::bool_ <(gp_format & PixelFormatAlpha ) != 0> {};
+template <Gdiplus::PixelFormat gp_format> struct has_premultiplied_alpha : mpl::bool_ <(gp_format & PixelFormatPAlpha ) != 0> {};
+template <Gdiplus::PixelFormat gp_format> struct is_supported : mpl::bool_ <(gp_format & PixelFormatGDI ) != 0 || ( ( gp_format == PixelFormat32bppCMYK ) && ( GDIPVER >= 0x0110 ) )> {};
+template <Gdiplus::PixelFormat gp_format> struct pixel_size : mpl::size_t<( gp_format >> 8 ) & 0xff > {};
+
+
+/// @see GdiplusPixelFormats.h: ARGB -> little endian BGRA
+typedef bgra_layout_t gp_alpha_layout_t;
+typedef bgr_layout_t gp_layout_t;
+
+typedef packed_pixel_type<uint16_t, mpl::vector3_c<unsigned,5,6,5>, gp_layout_t>::type gp_bgr565_pixel_t;
+
+template <typename Pixel, bool IsPlanar>
+struct gil_to_gp_format : mpl::integral_c<Gdiplus::PixelFormat, PixelFormatUndefined> {};
+
+template <> struct gil_to_gp_format<gp_bgr565_pixel_t, false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat16bppRGB565 > {};
+template <> struct gil_to_gp_format<bgr8_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat24bppRGB > {};
+template <> struct gil_to_gp_format<bgra8_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat32bppARGB > {};
+template <> struct gil_to_gp_format<gray16_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat16bppGrayScale> {};
+template <> struct gil_to_gp_format<bgr16_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat48bppRGB > {};
+template <> struct gil_to_gp_format<bgra16_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat64bppARGB > {};
+#if (GDIPVER >= 0x0110)
+template <> struct gil_to_gp_format<cmyk8_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat32bppCMYK > {};
+#endif // (GDIPVER >= 0x0110)
+
+
+template <typename Pixel, bool IsPlanar>
+struct gp_is_supported : is_supported<gil_to_gp_format<Pixel, IsPlanar>::value>{};
+
+
+typedef mpl::
+#if (GDIPVER >= 0x0110)
+ vector4
+#else
+ vector3
+#endif
+<
+ image<bgr8_pixel_t , false>,
+ image<bgra8_pixel_t , false>,
+ image<gp_bgr565_pixel_t, false>
+ #if (GDIPVER >= 0x0110)
+ ,image<cmyk8_pixel_t , false>
+ #endif
+> gp_supported_pixel_formats;
+
+
+typedef iterator_range<TCHAR const *> string_chunk_t;
+
+
+/*string_chunk_t*/char const * error_string( Gdiplus::GpStatus const status )
+{
+ using namespace Gdiplus;
+ switch ( status )
+ {
+ case GenericError : return "Generic GDI+ error";
+ case OutOfMemory : return "Out of memory";
+ case ObjectBusy : return "Object busy";
+ case InsufficientBuffer : return "Insufficient buffer";
+ case NotImplemented : return "Not implemented";
+ case Win32Error : return "Win32 subsystem failure";
+ case Aborted : return "Aborted";
+ case FileNotFound : return "File not found";
+ case ValueOverflow : return "Numeric overflow";
+ case AccessDenied : return "Access denied";
+ case UnknownImageFormat : return "Unknown image format";
+ case FontFamilyNotFound : return "Font family not found";
+ case FontStyleNotFound : return "Font style not found";
+ case NotTrueTypeFont : return "A non TrueType font specified";
+ case UnsupportedGdiplusVersion : return "Unsupported GDI+ version";
+ case PropertyNotFound : return "Specified property does not exist in the image";
+ case PropertyNotSupported : return "Specified property not supported by the format of the image";
+ #if (GDIPVER >= 0x0110)
+ case ProfileNotFound : return "Profile required to save an image in CMYK format not found";
+ #endif //(GDIPVER >= 0x0110)
+ }
+
+ // Programmer errors:
+ switch ( status )
+ {
+ case Ok : BOOST_ASSERT( !"Should not be called for no error" ); BF_UNREACHABLE_CODE break;
+ case InvalidParameter : BOOST_ASSERT( !"Invalid parameter" ); BF_UNREACHABLE_CODE break;
+ case WrongState : BOOST_ASSERT( !"Object in wrong state" ); BF_UNREACHABLE_CODE break;
+ case GdiplusNotInitialized : BOOST_ASSERT( !"GDI+ not initialized" ); BF_UNREACHABLE_CODE break;
+
+ default: BOOST_ASSERT( !"Unknown GDI+ status code." ); BF_UNREACHABLE_CODE break;
+ }
+}
+
+inline void ensure_result( Gdiplus::GpStatus const result )
+{
+ if ( result != Gdiplus::Ok )
+ io_error( error_string( result )/*.begin()*/ );
+}
+
+inline void verify_result( Gdiplus::GpStatus const result )
+{
+ BOOST_VERIFY( result == Gdiplus::Ok );
+}
+
+
+class gp_roi : public Gdiplus::Rect
+{
+public:
+ typedef INT value_type;
+ typedef point2<value_type> point_t ;
+
+ typedef point_t offset_t ;
+
+public:
+ gp_roi( value_type const x, value_type const y, value_type const width, value_type const height )
+ : Gdiplus::Rect( x, y, width, height ) {}
+
+ gp_roi( offset_t const offset, value_type const width, value_type const height )
+ : Gdiplus::Rect( Gdiplus::Point( offset.x, offset.y ), Gdiplus::Size( width, height ) ) {}
+
+ gp_roi( offset_t const top_left, offset_t const bottom_right )
+ : Gdiplus::Rect( Gdiplus::Point( top_left.x, top_left.y ), Gdiplus::Size( bottom_right.x - top_left.x, bottom_right.y - top_left.y ) ) {}
+};
+
+
+class gp_writer : public open_on_write_writer
+{
+public:
+ typedef std::pair<Gdiplus::GpBitmap *, Gdiplus::EncoderParameters *> lib_object_t;
+
+ // The passed View object must outlive the GpBitmap object (GDI+ uses lazy
+ // evaluation).
+ template <class View>
+ explicit gp_writer( View & view )
+ {
+ BOOST_STATIC_ASSERT(( gp_is_supported<typename View::value_type, is_planar<View>::value>::value ));
+
+ // http://msdn.microsoft.com/en-us/library/ms536315(VS.85).aspx
+ // stride has to be a multiple of 4 bytes
+ BOOST_ASSERT( !( view.pixels().row_size() % sizeof( Gdiplus::ARGB ) ) );
+
+ ensure_result
+ (
+ Gdiplus::DllExports::GdipCreateBitmapFromScan0
+ (
+ view.width (),
+ view.height(),
+ view.pixels().row_size(),
+ gil_to_gp_format<typename View::value_type, is_planar<View>::value>::value,
+ backend_base::get_raw_data( view ),
+ &lib_object().first
+ )
+ );
+ BOOST_ASSERT( lib_object().first );
+ }
+
+ ~gp_writer()
+ {
+ detail::verify_result( Gdiplus::DllExports::GdipDisposeImage( lib_object().first ) );
+ }
+
+ template <typename Target>
+ void write_default( Target const & target, format_tag const format )
+ {
+ BOOST_ASSERT( lib_object().second == NULL );
+ write( target, format );
+ }
+
+ void write( char const * const pFilename, format_tag const format ) const
+ {
+ write( detail::wide_path( pFilename ), format );
+ }
+
+ void write( wchar_t const * const pFilename, format_tag const format ) const
+ {
+ detail::ensure_result
+ (
+ Gdiplus::DllExports::GdipSaveImageToFile
+ (
+ lib_object().first,
+ pFilename,
+ &encoder_id( format ),
+ lib_object().second
+ )
+ );
+ }
+
+ lib_object_t & lib_object() { return lib_instance_; }
+ lib_object_t const & lib_object() const { return lib_instance_; }
+
+private:
+ static CLSID const & encoder_id( format_tag const format )
+ {
+ static CLSID const ids[ number_of_known_formats ] =
+ {
+ { 0x557CF400, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // BMP
+ { 0x557CF402, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // GIF
+ { 0x557CF401, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // JPEG
+ { 0x557CF406, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // PNG
+ { 0x557CF405, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // TIFF
+ CLSID_NULL // TGA
+ };
+ BOOST_ASSERT( ids[ format ] != CLSID_NULL );
+ return ids[ format ];
+ }
+
+private:
+ lib_object_t lib_instance_;
+};
+
+
+class gp_view_base;
+
+//------------------------------------------------------------------------------
+} // namespace detail
+
+class gp_image;
+
+template <>
+struct backend_traits<gp_image>
+{
+ typedef Gdiplus::PixelFormat format_t;
+
+ typedef detail::gp_supported_pixel_formats supported_pixel_formats_t;
+
+ typedef detail::gp_roi roi_t;
+
+ struct gil_to_native_format
+ {
+ template <typename Pixel, bool IsPlanar>
+ struct apply : detail::gil_to_gp_format<Pixel, IsPlanar> {};
+ };
+
+ template <typename Pixel, bool IsPlanar>
+ struct is_supported : detail::gp_is_supported<Pixel, IsPlanar> {};
+
+ typedef mpl::map5
+ <
+ mpl::pair<char const *, gp_image >,
+ mpl::pair<wchar_t const *, gp_image >,
+ mpl::pair<IStream , gp_image >,
+ mpl::pair<FILE , detail::input_FILE_for_IStream_extender <gp_image> >,
+ mpl::pair<memory_range_t , detail::memory_chunk_for_IStream_extender<gp_image> >
+ > native_sources;
+
+ typedef mpl::map4
+ <
+ mpl::pair<char const *, detail::gp_writer>,
+ mpl::pair<wchar_t const *, detail::gp_writer>,
+ mpl::pair<IStream , detail::gp_writer>,
+ mpl::pair<FILE , detail::gp_writer>
+ > native_sinks;
+
+ typedef mpl::vector5_c<format_tag, bmp, gif, jpeg, png, tiff> supported_image_formats;
+
+ class view_data_t : public Gdiplus::BitmapData
+ {
+ public:
+ template <typename View>
+ view_data_t( View const & view ) : p_roi_( 0 ) { set_bitmapdata_for_view( view ); }
+
+ template <typename View>
+ view_data_t( View const & view, roi_t::offset_t const & offset )
+ :
+ p_roi_( static_cast<roi_t const *>( optional_roi_.address() ) )
+ {
+ set_bitmapdata_for_view( view );
+ new ( optional_roi_.address() ) roi_t( offset, Width, Height );
+ }
+
+ void set_format( format_t const format ) { PixelFormat = format; }
+
+ public:
+ Gdiplus::Rect const * const p_roi_;
+
+ private:
+ template <typename View>
+ void set_bitmapdata_for_view( View const & view )
+ {
+ using namespace detail;
+
+ BOOST_STATIC_ASSERT(( is_supported<typename View::value_type, is_planar<View>::value>::value ));
+
+ Width = view.width ();
+ Height = view.height();
+ Stride = view.pixels().row_size();
+ PixelFormat = detail::gil_to_gp_format<typename View::value_type, is_planar<View>::value>::value;
+ Scan0 = backend_base::get_raw_data( view );
+ Reserved = 0;
+ }
+
+ void operator=( view_data_t const & );
+
+ private:
+ aligned_storage<sizeof( roi_t ), alignment_of<roi_t>::value>::type optional_roi_;
+ };
+
+ typedef view_data_t writer_view_data_t;
+
+ BOOST_STATIC_CONSTANT( unsigned int, desired_alignment = sizeof( Gdiplus::ARGB ) );
+ BOOST_STATIC_CONSTANT( bool , builtin_conversion = true );
+};
+
+
+#if defined(BOOST_MSVC)
+# pragma warning( push )
+# pragma warning( disable : 4127 ) // "conditional expression is constant"
+#endif
+
+class gp_image
+ :
+ private detail::gp_guard,
+ public detail::backend<gp_image>
+{
+public:
+ typedef detail::gp_user_guard guard;
+
+public: /// \ingroup Construction
+ explicit gp_image( wchar_t const * const filename )
+ {
+ detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromFile( filename, &pBitmap_ ) );
+ BOOST_ASSERT( pBitmap_ );
+ }
+
+ explicit gp_image( char const * const filename )
+ {
+ detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromFile( detail::wide_path( filename ), &pBitmap_ ) );
+ BOOST_ASSERT( pBitmap_ );
+ }
+
+ // The passed IStream object must outlive the GpBitmap object (GDI+ uses
+ // lazy evaluation).
+ explicit gp_image( IStream & stream )
+ {
+ detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromStream( &stream, &pBitmap_ ) );
+ BOOST_ASSERT( pBitmap_ );
+ }
+
+ ~gp_image()
+ {
+ detail::verify_result( Gdiplus::DllExports::GdipDisposeImage( pBitmap_ ) );
+ }
+
+public:
+ point2<std::ptrdiff_t> dimensions() const
+ {
+ using namespace Gdiplus;
+ REAL width, height;
+ detail::verify_result( DllExports::GdipGetImageDimension( const_cast<GpBitmap *>( pBitmap_ ), &width, &height ) );
+ return point2<std::ptrdiff_t>( static_cast<std::ptrdiff_t>( width ), static_cast<std::ptrdiff_t>( height ) );
+ }
+
+ format_t format() const
+ {
+ format_t pixel_format;
+ detail::verify_result( Gdiplus::DllExports::GdipGetImagePixelFormat( pBitmap_, &pixel_format ) );
+ return pixel_format;
+ }
+
+ format_t closest_gil_supported_format() const
+ {
+ //http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.gdi/2008-01/msg00044.html
+ switch ( format() )
+ {
+ case PixelFormat16bppGrayScale:
+ case PixelFormat48bppRGB :
+ case PixelFormat32bppRGB :
+ case PixelFormat24bppRGB :
+ return
+ PixelFormat24bppRGB;
+
+ case PixelFormat64bppPARGB :
+ case PixelFormat32bppPARGB :
+ case PixelFormat64bppARGB :
+ case PixelFormat32bppARGB :
+ case PixelFormat16bppARGB1555 :
+ return
+ PixelFormat32bppARGB;
+
+ case PixelFormat16bppRGB565 :
+ case PixelFormat16bppRGB555 :
+ case PixelFormat8bppIndexed :
+ case PixelFormat4bppIndexed :
+ case PixelFormat1bppIndexed :
+ return
+ PixelFormat16bppRGB565;
+
+ case PixelFormat32bppCMYK:
+ return
+ #if (GDIPVER >= 0x0110)
+ PixelFormat32bppCMYK;
+ #else
+ PixelFormat24bppRGB;
+ #endif // (GDIPVER >= 0x0110)
+
+ default:
+ BF_UNREACHABLE_CODE
+ return PixelFormatUndefined;
+ }
+ }
+
+ image_type_id current_image_format_id() const
+ {
+ return image_format_id( closest_gil_supported_format() );
+ }
+
+ static image_type_id image_format_id( format_t const closest_gil_supported_format )
+ {
+ switch ( closest_gil_supported_format )
+ {
+ case PixelFormat24bppRGB : return 0;
+ case PixelFormat32bppARGB : return 1;
+ case PixelFormat16bppRGB565 : return 2;
+ #if (GDIPVER >= 0x0110)
+ case PixelFormat32bppCMYK : return 3;
+ #endif
+
+ default:
+ BF_UNREACHABLE_CODE
+ return unsupported_format;
+ }
+ }
+
+public: // Low-level (row, strip, tile) access
+ static bool can_do_roi_access() { return true; }
+
+ class sequential_row_read_state
+ :
+ private detail::cumulative_result
+ {
+ public:
+ using detail::cumulative_result::failed;
+ void throw_if_error() const { detail::cumulative_result::throw_if_error( "GDI+ failure" ); }
+
+ BOOST_STATIC_CONSTANT( bool, throws_on_error = false );
+
+ private:
+ sequential_row_read_state( gp_image const & source_image )
+ :
+ roi_( 0, 0, source_image.dimensions().x, 1 )
+ {
+ bitmapData_.Width = roi_.Width;
+ bitmapData_.Height = 1;
+ bitmapData_.PixelFormat = source_image.format();
+ bitmapData_.Stride = bitmapData_.Width * source_image.cached_format_size( bitmapData_.PixelFormat );
+ bitmapData_.Reserved = 0;
+ }
+
+ private: friend gp_image;
+ Gdiplus::Rect roi_ ;
+ Gdiplus::BitmapData bitmapData_;
+ };
+
+ sequential_row_read_state begin_sequential_row_read() const { return sequential_row_read_state( *this ); }
+
+ /// \todo Kill duplication with raw_convert_to_prepared_view().
+ /// (04.01.2011.) (Domagoj Saric)
+ void read_row( sequential_row_read_state & state, unsigned char * const p_row_storage ) const
+ {
+ using namespace detail ;
+ using namespace Gdiplus;
+
+ state.bitmapData_.Scan0 = p_row_storage;
+
+ state.accumulate_equal
+ (
+ DllExports::GdipBitmapLockBits
+ (
+ pBitmap_,
+ &state.roi_,
+ ImageLockModeRead | ImageLockModeUserInputBuf,
+ state.bitmapData_.PixelFormat,
+ &state.bitmapData_
+ ),
+ Gdiplus::Ok
+ );
+ verify_result( DllExports::GdipBitmapUnlockBits( pBitmap_, &state.bitmapData_ ) );
+
+ ++state.roi_.Y;
+ }
+
+ ::Gdiplus::GpBitmap & lib_object() { return *pBitmap_; }
+ ::Gdiplus::GpBitmap const & lib_object() const { return const_cast<gp_image &>( *this ).lib_object(); }
+
+private: // Private backend_base interface.
+ friend class base_t;
+
+ template <class MyView, class TargetView, class Converter>
+ void generic_convert_to_prepared_view( TargetView const & view, Converter const & converter ) const
+ {
+ BOOST_ASSERT( !dimensions_mismatch( view ) );
+ //BOOST_ASSERT( !formats_mismatch ( view ) );
+
+ using namespace detail ;
+ using namespace Gdiplus;
+
+ point2<std::ptrdiff_t> const & targetDimensions( original_view( view ).dimensions() );
+ gp_roi const roi( get_offset<gp_roi::offset_t>( view ), targetDimensions.x, targetDimensions.y );
+ format_t const my_format( gil_to_gp_format<typename View::value_type, is_planar<View>::value>::value );
+ BitmapData bitmapData;
+ ensure_result
+ (
+ DllExports::GdipBitmapLockBits
+ (
+ pBitmap_,
+ &roi,
+ ImageLockModeRead,
+ my_format,
+ &bitmapData
+ )
+ );
+ BOOST_ASSERT( bitmapData.PixelFormat == my_format );
+ copy_and_convert_pixels // This must not throw!
+ (
+ interleaved_view
+ (
+ bitmapData.Width ,
+ bitmapData.Height,
+ gil_reinterpret_cast_c<typename MyView::value_type const *>( bitmapData.Scan0 ),
+ bitmapData.Stride
+ ),
+ view,
+ converter
+ );
+ verify_result( DllExports::GdipBitmapUnlockBits( pBitmap_, &bitmapData ) );
+ }
+
+
+ void raw_convert_to_prepared_view( view_data_t const & view_data ) const
+ {
+ BOOST_ASSERT( view_data.Scan0 );
+
+ using namespace detail ;
+ using namespace Gdiplus;
+
+ BitmapData * const pMutableBitmapData( const_cast<BitmapData *>( static_cast<BitmapData const *>( &view_data ) ) );
+ GpStatus const load_result
+ (
+ DllExports::GdipBitmapLockBits
+ (
+ pBitmap_,
+ view_data.p_roi_,
+ ImageLockModeRead | ImageLockModeUserInputBuf,
+ view_data.PixelFormat,
+ pMutableBitmapData
+ )
+ );
+ GpStatus const unlock_result( DllExports::GdipBitmapUnlockBits( pBitmap_, pMutableBitmapData ) );
+ ensure_result( load_result );
+ verify_result( unlock_result );
+ }
+
+
+ void raw_copy_to_prepared_view( view_data_t const & view_data ) const
+ {
+ BOOST_ASSERT( view_data.Width == static_cast<UINT>( dimensions().x ) );
+ BOOST_ASSERT( view_data.Height == static_cast<UINT>( dimensions().y ) );
+ //...this need not hold as it can be used to perform GDI+ default
+ //internal colour conversion...maybe i'll provide another worker
+ //function...
+ //BOOST_ASSERT( view_data.PixelFormat == format () );
+ raw_convert_to_prepared_view( view_data );
+ }
+
+
+ static std::size_t cached_format_size( format_t const format )
+ {
+ return Gdiplus::GetPixelFormatSize( format );
+ }
+
+private:
+ template <Gdiplus::PixelFormat desired_format>
+ void pre_palettized_conversion( mpl::true_ /*is_indexed*/ )
+ {
+ #if (GDIPVER >= 0x0110)
+ // A GDI+ 1.1 (a non-distributable version, distributed with MS Office
+ // 2003 and MS Windows Vista and MS Windows 7) 'enhanced'/'tuned'
+ // version of the conversion routine for indexed/palettized image
+ // formats. Unless/until proven useful, pretty much still a GDI+ 1.1
+ // tester...
+
+ BOOST_ASSERT( !has_alpha<desired_format>::value && "Is this possible for indexed formats?" );
+ std::size_t const number_of_pixel_bits ( pixel_size<desired_format>::value );
+ std::size_t const number_of_palette_colours( static_cast<std::size_t>( ( 2 << ( number_of_pixel_bits - 1 ) ) - 1 ) );
+ std::size_t const palette_size ( sizeof( ColorPalette ) + number_of_palette_colours * sizeof( ARGB ) );
+ aligned_storage
+ <
+ palette_size,
+ alignment_of<ColorPalette>::value
+ >::type palette_storage;
+ ColorPalette & palette( *static_cast<ColorPalette *>( palette_storage.address() ) );
+ palette.Flags = ( PaletteFlagsHasAlpha * has_alpha<desired_format>::value ) |
+ ( PaletteFlagsGrayScale * is_same<typename color_space_type<View>::type, gray_t>::value );
+ palette.Count = number_of_palette_colours;
+
+ verify_result
+ (
+ DllExports::GdipInitializePalette
+ (
+ &palette,
+ PaletteTypeOptimal,
+ number_of_palette_colours,
+ has_alpha<desired_format>::value,
+ pBitmap_
+ )
+ );
+
+ ensure_result
+ (
+ DllExports::GdipBitmapConvertFormat
+ (
+ pBitmap_,
+ desired_format,
+ DitherTypeErrorDiffusion,
+ PaletteTypeOptimal,
+ &palette,
+ 50
+ )
+ );
+ #endif // (GDIPVER >= 0x0110)
+ }
+
+ template <Gdiplus::PixelFormat desired_format>
+ void pre_palettized_conversion( mpl::false_ /*not is_indexed*/ ) const {}
+
+private:
+ friend class detail::gp_view_base;
+
+ Gdiplus::GpBitmap * /*const*/ pBitmap_;
+};
+
+#if defined(BOOST_MSVC)
+# pragma warning( pop )
+#endif
+
+namespace detail
+{
+//------------------------------------------------------------------------------
+
+////////////////////////////////////////////////////////////////////////////////
+///
+/// \class gp_view_base
+///
+////////////////////////////////////////////////////////////////////////////////
+
+class gp_view_base : noncopyable
+{
+public:
+ ~gp_view_base()
+ {
+ detail::verify_result( Gdiplus::DllExports::GdipBitmapUnlockBits( &bitmap_, &bitmapData_ ) );
+ }
+
+protected:
+ gp_view_base( gp_image & bitmap, unsigned int const lock_mode, gp_image::roi const * const p_roi = 0 )
+ :
+ bitmap_( *bitmap.pBitmap_ )
+ {
+ std::memset( &bitmapData_, 0, sizeof( bitmapData_ ) );
+
+ detail::ensure_result
+ (
+ Gdiplus::DllExports::GdipBitmapLockBits
+ (
+ &bitmap_,
+ p_roi,
+ lock_mode,
+ bitmap.format(),
+ &bitmapData_
+ )
+ );
+ }
+
+ template <typename Pixel>
+ typename type_from_x_iterator<Pixel *>::view_t
+ get_typed_view()
+ {
+ //todo assert correct type...
+ interleaved_view<Pixel *>( bitmapData_.Width, bitmapData_.Height, bitmapData_.Scan0, bitmapData_.Stride );
+ }
+
+private:
+ Gdiplus::GpBitmap & bitmap_ ;
+ Gdiplus::BitmapData bitmapData_;
+};
+
+
+
+////////////////////////////////////////////////////////////////////////////////
+///
+/// \class gp_view
+///
+////////////////////////////////////////////////////////////////////////////////
+
+template <typename Pixel>
+class gp_view
+ :
+ private gp_view_base,
+ public type_from_x_iterator<Pixel *>::view_t
+{
+public:
+ gp_view( gp_image & image, gp_image::roi const * const p_roi = 0 )
+ :
+ gp_view_base( image, ImageLockModeRead | ( is_const<Pixel>::value * ImageLockModeWrite ), p_roi ),
+ type_from_x_iterator<Pixel *>::view_t( get_typed_view<Pixel>() )
+ {}
+};
+
+
+//...mhmh...to be seen if necessary...
+//template <typename Pixel>
+//class gp_const_view
+// :
+// private gp_view_base,
+// public type_from_x_iterator<Pixel const *>::view_t
+//{
+//public:
+// gp_const_view( gp_image & image, gp_image::roi const * const p_roi = 0 )
+// :
+// gp_view_base( image, ImageLockModeRead ),
+// type_from_x_iterator<Pixel const *>::view_t( get_typed_view<Pixel const>() )
+// {}
+//};
+
+
+//...mhmh...to be implemented...
+//template <class Impl, class SupportedPixelFormats, class ROI>
+//inline
+//typename backend<Impl, SupportedPixelFormats, ROI>::view_t
+//view( backend<Impl, SupportedPixelFormats, ROI> & img );// { return img._view; }
+
+//------------------------------------------------------------------------------
+} // namespace detail
+//------------------------------------------------------------------------------
+} // namespace gil
+//------------------------------------------------------------------------------
+} // namespace boost
+//------------------------------------------------------------------------------
+#endif // gp_private_base_hpp

Copied: sandbox/gil/boost/gil/extension/io2/backends/gp/writer.hpp (from r85344, sandbox/gil/boost/gil/extension/io2/gp_image.hpp)
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ sandbox/gil/boost/gil/extension/io2/backends/gp/writer.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85502, copy of r85344, sandbox/gil/boost/gil/extension/io2/gp_image.hpp)
@@ -0,0 +1,784 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// \file gp_image.hpp
+/// ------------------
+///
+/// Base IO interface GDI+ implementation.
+///
+/// Copyright (c) 2010.-2013. Domagoj Saric
+///
+/// Use, modification and distribution is subject to the
+/// Boost Software License, Version 1.0.
+/// (See accompanying file LICENSE_1_0.txt or copy at
+/// http://www.boost.org/LICENSE_1_0.txt)
+///
+/// For more information, see http://www.boost.org
+///
+////////////////////////////////////////////////////////////////////////////////
+//------------------------------------------------------------------------------
+#pragma once
+#ifndef gp_private_base_hpp__3B1ED5BC_42C6_4EC6_B700_01C1B8646431
+#define gp_private_base_hpp__3B1ED5BC_42C6_4EC6_B700_01C1B8646431
+//------------------------------------------------------------------------------
+#include "detail/io_error.hpp"
+#include "detail/gp_extern_lib_guard.hpp"
+#include "detail/windows_shared.hpp"
+#include "detail/windows_shared_istreams.hpp"
+#include "backend.hpp"
+
+#include <boost/array.hpp>
+#include <boost/mpl/eval_if.hpp>
+#include <boost/mpl/integral_c.hpp>
+#include <boost/range/iterator_range.hpp>
+#include <boost/static_assert.hpp>
+#include <boost/type_traits/is_pod.hpp>
+
+#include <algorithm>
+//------------------------------------------------------------------------------
+namespace boost
+{
+//------------------------------------------------------------------------------
+namespace gil
+{
+//------------------------------------------------------------------------------
+namespace detail
+{
+//------------------------------------------------------------------------------
+
+template <Gdiplus::PixelFormat gp_format> struct is_canonical : mpl::bool_ <(gp_format & PixelFormatCanonical) != 0> {};
+template <Gdiplus::PixelFormat gp_format> struct is_extended : mpl::bool_ <(gp_format & PixelFormatExtended ) != 0> {};
+template <Gdiplus::PixelFormat gp_format> struct is_indexed : mpl::bool_ <(gp_format & PixelFormatIndexed ) != 0> {};
+template <Gdiplus::PixelFormat gp_format> struct has_alpha : mpl::bool_ <(gp_format & PixelFormatAlpha ) != 0> {};
+template <Gdiplus::PixelFormat gp_format> struct has_premultiplied_alpha : mpl::bool_ <(gp_format & PixelFormatPAlpha ) != 0> {};
+template <Gdiplus::PixelFormat gp_format> struct is_supported : mpl::bool_ <(gp_format & PixelFormatGDI ) != 0 || ( ( gp_format == PixelFormat32bppCMYK ) && ( GDIPVER >= 0x0110 ) )> {};
+template <Gdiplus::PixelFormat gp_format> struct pixel_size : mpl::size_t<( gp_format >> 8 ) & 0xff > {};
+
+
+/// @see GdiplusPixelFormats.h: ARGB -> little endian BGRA
+typedef bgra_layout_t gp_alpha_layout_t;
+typedef bgr_layout_t gp_layout_t;
+
+typedef packed_pixel_type<uint16_t, mpl::vector3_c<unsigned,5,6,5>, gp_layout_t>::type gp_bgr565_pixel_t;
+
+template <typename Pixel, bool IsPlanar>
+struct gil_to_gp_format : mpl::integral_c<Gdiplus::PixelFormat, PixelFormatUndefined> {};
+
+template <> struct gil_to_gp_format<gp_bgr565_pixel_t, false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat16bppRGB565 > {};
+template <> struct gil_to_gp_format<bgr8_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat24bppRGB > {};
+template <> struct gil_to_gp_format<bgra8_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat32bppARGB > {};
+template <> struct gil_to_gp_format<gray16_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat16bppGrayScale> {};
+template <> struct gil_to_gp_format<bgr16_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat48bppRGB > {};
+template <> struct gil_to_gp_format<bgra16_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat64bppARGB > {};
+#if (GDIPVER >= 0x0110)
+template <> struct gil_to_gp_format<cmyk8_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat32bppCMYK > {};
+#endif // (GDIPVER >= 0x0110)
+
+
+template <typename Pixel, bool IsPlanar>
+struct gp_is_supported : is_supported<gil_to_gp_format<Pixel, IsPlanar>::value>{};
+
+
+typedef mpl::
+#if (GDIPVER >= 0x0110)
+ vector4
+#else
+ vector3
+#endif
+<
+ image<bgr8_pixel_t , false>,
+ image<bgra8_pixel_t , false>,
+ image<gp_bgr565_pixel_t, false>
+ #if (GDIPVER >= 0x0110)
+ ,image<cmyk8_pixel_t , false>
+ #endif
+> gp_supported_pixel_formats;
+
+
+typedef iterator_range<TCHAR const *> string_chunk_t;
+
+
+/*string_chunk_t*/char const * error_string( Gdiplus::GpStatus const status )
+{
+ using namespace Gdiplus;
+ switch ( status )
+ {
+ case GenericError : return "Generic GDI+ error";
+ case OutOfMemory : return "Out of memory";
+ case ObjectBusy : return "Object busy";
+ case InsufficientBuffer : return "Insufficient buffer";
+ case NotImplemented : return "Not implemented";
+ case Win32Error : return "Win32 subsystem failure";
+ case Aborted : return "Aborted";
+ case FileNotFound : return "File not found";
+ case ValueOverflow : return "Numeric overflow";
+ case AccessDenied : return "Access denied";
+ case UnknownImageFormat : return "Unknown image format";
+ case FontFamilyNotFound : return "Font family not found";
+ case FontStyleNotFound : return "Font style not found";
+ case NotTrueTypeFont : return "A non TrueType font specified";
+ case UnsupportedGdiplusVersion : return "Unsupported GDI+ version";
+ case PropertyNotFound : return "Specified property does not exist in the image";
+ case PropertyNotSupported : return "Specified property not supported by the format of the image";
+ #if (GDIPVER >= 0x0110)
+ case ProfileNotFound : return "Profile required to save an image in CMYK format not found";
+ #endif //(GDIPVER >= 0x0110)
+ }
+
+ // Programmer errors:
+ switch ( status )
+ {
+ case Ok : BOOST_ASSERT( !"Should not be called for no error" ); BF_UNREACHABLE_CODE break;
+ case InvalidParameter : BOOST_ASSERT( !"Invalid parameter" ); BF_UNREACHABLE_CODE break;
+ case WrongState : BOOST_ASSERT( !"Object in wrong state" ); BF_UNREACHABLE_CODE break;
+ case GdiplusNotInitialized : BOOST_ASSERT( !"GDI+ not initialized" ); BF_UNREACHABLE_CODE break;
+
+ default: BOOST_ASSERT( !"Unknown GDI+ status code." ); BF_UNREACHABLE_CODE break;
+ }
+}
+
+inline void ensure_result( Gdiplus::GpStatus const result )
+{
+ if ( result != Gdiplus::Ok )
+ io_error( error_string( result )/*.begin()*/ );
+}
+
+inline void verify_result( Gdiplus::GpStatus const result )
+{
+ BOOST_VERIFY( result == Gdiplus::Ok );
+}
+
+
+class gp_roi : public Gdiplus::Rect
+{
+public:
+ typedef INT value_type;
+ typedef point2<value_type> point_t ;
+
+ typedef point_t offset_t ;
+
+public:
+ gp_roi( value_type const x, value_type const y, value_type const width, value_type const height )
+ : Gdiplus::Rect( x, y, width, height ) {}
+
+ gp_roi( offset_t const offset, value_type const width, value_type const height )
+ : Gdiplus::Rect( Gdiplus::Point( offset.x, offset.y ), Gdiplus::Size( width, height ) ) {}
+
+ gp_roi( offset_t const top_left, offset_t const bottom_right )
+ : Gdiplus::Rect( Gdiplus::Point( top_left.x, top_left.y ), Gdiplus::Size( bottom_right.x - top_left.x, bottom_right.y - top_left.y ) ) {}
+};
+
+
+class gp_writer : public open_on_write_writer
+{
+public:
+ typedef std::pair<Gdiplus::GpBitmap *, Gdiplus::EncoderParameters *> lib_object_t;
+
+ // The passed View object must outlive the GpBitmap object (GDI+ uses lazy
+ // evaluation).
+ template <class View>
+ explicit gp_writer( View & view )
+ {
+ BOOST_STATIC_ASSERT(( gp_is_supported<typename View::value_type, is_planar<View>::value>::value ));
+
+ // http://msdn.microsoft.com/en-us/library/ms536315(VS.85).aspx
+ // stride has to be a multiple of 4 bytes
+ BOOST_ASSERT( !( view.pixels().row_size() % sizeof( Gdiplus::ARGB ) ) );
+
+ ensure_result
+ (
+ Gdiplus::DllExports::GdipCreateBitmapFromScan0
+ (
+ view.width (),
+ view.height(),
+ view.pixels().row_size(),
+ gil_to_gp_format<typename View::value_type, is_planar<View>::value>::value,
+ backend_base::get_raw_data( view ),
+ &lib_object().first
+ )
+ );
+ BOOST_ASSERT( lib_object().first );
+ }
+
+ ~gp_writer()
+ {
+ detail::verify_result( Gdiplus::DllExports::GdipDisposeImage( lib_object().first ) );
+ }
+
+ template <typename Target>
+ void write_default( Target const & target, format_tag const format )
+ {
+ BOOST_ASSERT( lib_object().second == NULL );
+ write( target, format );
+ }
+
+ void write( char const * const pFilename, format_tag const format ) const
+ {
+ write( detail::wide_path( pFilename ), format );
+ }
+
+ void write( wchar_t const * const pFilename, format_tag const format ) const
+ {
+ detail::ensure_result
+ (
+ Gdiplus::DllExports::GdipSaveImageToFile
+ (
+ lib_object().first,
+ pFilename,
+ &encoder_id( format ),
+ lib_object().second
+ )
+ );
+ }
+
+ lib_object_t & lib_object() { return lib_instance_; }
+ lib_object_t const & lib_object() const { return lib_instance_; }
+
+private:
+ static CLSID const & encoder_id( format_tag const format )
+ {
+ static CLSID const ids[ number_of_known_formats ] =
+ {
+ { 0x557CF400, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // BMP
+ { 0x557CF402, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // GIF
+ { 0x557CF401, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // JPEG
+ { 0x557CF406, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // PNG
+ { 0x557CF405, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // TIFF
+ CLSID_NULL // TGA
+ };
+ BOOST_ASSERT( ids[ format ] != CLSID_NULL );
+ return ids[ format ];
+ }
+
+private:
+ lib_object_t lib_instance_;
+};
+
+
+class gp_view_base;
+
+//------------------------------------------------------------------------------
+} // namespace detail
+
+class gp_image;
+
+template <>
+struct backend_traits<gp_image>
+{
+ typedef Gdiplus::PixelFormat format_t;
+
+ typedef detail::gp_supported_pixel_formats supported_pixel_formats_t;
+
+ typedef detail::gp_roi roi_t;
+
+ struct gil_to_native_format
+ {
+ template <typename Pixel, bool IsPlanar>
+ struct apply : detail::gil_to_gp_format<Pixel, IsPlanar> {};
+ };
+
+ template <typename Pixel, bool IsPlanar>
+ struct is_supported : detail::gp_is_supported<Pixel, IsPlanar> {};
+
+ typedef mpl::map5
+ <
+ mpl::pair<char const *, gp_image >,
+ mpl::pair<wchar_t const *, gp_image >,
+ mpl::pair<IStream , gp_image >,
+ mpl::pair<FILE , detail::input_FILE_for_IStream_extender <gp_image> >,
+ mpl::pair<memory_range_t , detail::memory_chunk_for_IStream_extender<gp_image> >
+ > native_sources;
+
+ typedef mpl::map4
+ <
+ mpl::pair<char const *, detail::gp_writer>,
+ mpl::pair<wchar_t const *, detail::gp_writer>,
+ mpl::pair<IStream , detail::gp_writer>,
+ mpl::pair<FILE , detail::gp_writer>
+ > native_sinks;
+
+ typedef mpl::vector5_c<format_tag, bmp, gif, jpeg, png, tiff> supported_image_formats;
+
+ class view_data_t : public Gdiplus::BitmapData
+ {
+ public:
+ template <typename View>
+ view_data_t( View const & view ) : p_roi_( 0 ) { set_bitmapdata_for_view( view ); }
+
+ template <typename View>
+ view_data_t( View const & view, roi_t::offset_t const & offset )
+ :
+ p_roi_( static_cast<roi_t const *>( optional_roi_.address() ) )
+ {
+ set_bitmapdata_for_view( view );
+ new ( optional_roi_.address() ) roi_t( offset, Width, Height );
+ }
+
+ void set_format( format_t const format ) { PixelFormat = format; }
+
+ public:
+ Gdiplus::Rect const * const p_roi_;
+
+ private:
+ template <typename View>
+ void set_bitmapdata_for_view( View const & view )
+ {
+ using namespace detail;
+
+ BOOST_STATIC_ASSERT(( is_supported<typename View::value_type, is_planar<View>::value>::value ));
+
+ Width = view.width ();
+ Height = view.height();
+ Stride = view.pixels().row_size();
+ PixelFormat = detail::gil_to_gp_format<typename View::value_type, is_planar<View>::value>::value;
+ Scan0 = backend_base::get_raw_data( view );
+ Reserved = 0;
+ }
+
+ void operator=( view_data_t const & );
+
+ private:
+ aligned_storage<sizeof( roi_t ), alignment_of<roi_t>::value>::type optional_roi_;
+ };
+
+ typedef view_data_t writer_view_data_t;
+
+ BOOST_STATIC_CONSTANT( unsigned int, desired_alignment = sizeof( Gdiplus::ARGB ) );
+ BOOST_STATIC_CONSTANT( bool , builtin_conversion = true );
+};
+
+
+#if defined(BOOST_MSVC)
+# pragma warning( push )
+# pragma warning( disable : 4127 ) // "conditional expression is constant"
+#endif
+
+class gp_image
+ :
+ private detail::gp_guard,
+ public detail::backend<gp_image>
+{
+public:
+ typedef detail::gp_user_guard guard;
+
+public: /// \ingroup Construction
+ explicit gp_image( wchar_t const * const filename )
+ {
+ detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromFile( filename, &pBitmap_ ) );
+ BOOST_ASSERT( pBitmap_ );
+ }
+
+ explicit gp_image( char const * const filename )
+ {
+ detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromFile( detail::wide_path( filename ), &pBitmap_ ) );
+ BOOST_ASSERT( pBitmap_ );
+ }
+
+ // The passed IStream object must outlive the GpBitmap object (GDI+ uses
+ // lazy evaluation).
+ explicit gp_image( IStream & stream )
+ {
+ detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromStream( &stream, &pBitmap_ ) );
+ BOOST_ASSERT( pBitmap_ );
+ }
+
+ ~gp_image()
+ {
+ detail::verify_result( Gdiplus::DllExports::GdipDisposeImage( pBitmap_ ) );
+ }
+
+public:
+ point2<std::ptrdiff_t> dimensions() const
+ {
+ using namespace Gdiplus;
+ REAL width, height;
+ detail::verify_result( DllExports::GdipGetImageDimension( const_cast<GpBitmap *>( pBitmap_ ), &width, &height ) );
+ return point2<std::ptrdiff_t>( static_cast<std::ptrdiff_t>( width ), static_cast<std::ptrdiff_t>( height ) );
+ }
+
+ format_t format() const
+ {
+ format_t pixel_format;
+ detail::verify_result( Gdiplus::DllExports::GdipGetImagePixelFormat( pBitmap_, &pixel_format ) );
+ return pixel_format;
+ }
+
+ format_t closest_gil_supported_format() const
+ {
+ //http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.gdi/2008-01/msg00044.html
+ switch ( format() )
+ {
+ case PixelFormat16bppGrayScale:
+ case PixelFormat48bppRGB :
+ case PixelFormat32bppRGB :
+ case PixelFormat24bppRGB :
+ return
+ PixelFormat24bppRGB;
+
+ case PixelFormat64bppPARGB :
+ case PixelFormat32bppPARGB :
+ case PixelFormat64bppARGB :
+ case PixelFormat32bppARGB :
+ case PixelFormat16bppARGB1555 :
+ return
+ PixelFormat32bppARGB;
+
+ case PixelFormat16bppRGB565 :
+ case PixelFormat16bppRGB555 :
+ case PixelFormat8bppIndexed :
+ case PixelFormat4bppIndexed :
+ case PixelFormat1bppIndexed :
+ return
+ PixelFormat16bppRGB565;
+
+ case PixelFormat32bppCMYK:
+ return
+ #if (GDIPVER >= 0x0110)
+ PixelFormat32bppCMYK;
+ #else
+ PixelFormat24bppRGB;
+ #endif // (GDIPVER >= 0x0110)
+
+ default:
+ BF_UNREACHABLE_CODE
+ return PixelFormatUndefined;
+ }
+ }
+
+ image_type_id current_image_format_id() const
+ {
+ return image_format_id( closest_gil_supported_format() );
+ }
+
+ static image_type_id image_format_id( format_t const closest_gil_supported_format )
+ {
+ switch ( closest_gil_supported_format )
+ {
+ case PixelFormat24bppRGB : return 0;
+ case PixelFormat32bppARGB : return 1;
+ case PixelFormat16bppRGB565 : return 2;
+ #if (GDIPVER >= 0x0110)
+ case PixelFormat32bppCMYK : return 3;
+ #endif
+
+ default:
+ BF_UNREACHABLE_CODE
+ return unsupported_format;
+ }
+ }
+
+public: // Low-level (row, strip, tile) access
+ static bool can_do_roi_access() { return true; }
+
+ class sequential_row_read_state
+ :
+ private detail::cumulative_result
+ {
+ public:
+ using detail::cumulative_result::failed;
+ void throw_if_error() const { detail::cumulative_result::throw_if_error( "GDI+ failure" ); }
+
+ BOOST_STATIC_CONSTANT( bool, throws_on_error = false );
+
+ private:
+ sequential_row_read_state( gp_image const & source_image )
+ :
+ roi_( 0, 0, source_image.dimensions().x, 1 )
+ {
+ bitmapData_.Width = roi_.Width;
+ bitmapData_.Height = 1;
+ bitmapData_.PixelFormat = source_image.format();
+ bitmapData_.Stride = bitmapData_.Width * source_image.cached_format_size( bitmapData_.PixelFormat );
+ bitmapData_.Reserved = 0;
+ }
+
+ private: friend gp_image;
+ Gdiplus::Rect roi_ ;
+ Gdiplus::BitmapData bitmapData_;
+ };
+
+ sequential_row_read_state begin_sequential_row_read() const { return sequential_row_read_state( *this ); }
+
+ /// \todo Kill duplication with raw_convert_to_prepared_view().
+ /// (04.01.2011.) (Domagoj Saric)
+ void read_row( sequential_row_read_state & state, unsigned char * const p_row_storage ) const
+ {
+ using namespace detail ;
+ using namespace Gdiplus;
+
+ state.bitmapData_.Scan0 = p_row_storage;
+
+ state.accumulate_equal
+ (
+ DllExports::GdipBitmapLockBits
+ (
+ pBitmap_,
+ &state.roi_,
+ ImageLockModeRead | ImageLockModeUserInputBuf,
+ state.bitmapData_.PixelFormat,
+ &state.bitmapData_
+ ),
+ Gdiplus::Ok
+ );
+ verify_result( DllExports::GdipBitmapUnlockBits( pBitmap_, &state.bitmapData_ ) );
+
+ ++state.roi_.Y;
+ }
+
+ ::Gdiplus::GpBitmap & lib_object() { return *pBitmap_; }
+ ::Gdiplus::GpBitmap const & lib_object() const { return const_cast<gp_image &>( *this ).lib_object(); }
+
+private: // Private backend_base interface.
+ friend class base_t;
+
+ template <class MyView, class TargetView, class Converter>
+ void generic_convert_to_prepared_view( TargetView const & view, Converter const & converter ) const
+ {
+ BOOST_ASSERT( !dimensions_mismatch( view ) );
+ //BOOST_ASSERT( !formats_mismatch ( view ) );
+
+ using namespace detail ;
+ using namespace Gdiplus;
+
+ point2<std::ptrdiff_t> const & targetDimensions( original_view( view ).dimensions() );
+ gp_roi const roi( get_offset<gp_roi::offset_t>( view ), targetDimensions.x, targetDimensions.y );
+ format_t const my_format( gil_to_gp_format<typename View::value_type, is_planar<View>::value>::value );
+ BitmapData bitmapData;
+ ensure_result
+ (
+ DllExports::GdipBitmapLockBits
+ (
+ pBitmap_,
+ &roi,
+ ImageLockModeRead,
+ my_format,
+ &bitmapData
+ )
+ );
+ BOOST_ASSERT( bitmapData.PixelFormat == my_format );
+ copy_and_convert_pixels // This must not throw!
+ (
+ interleaved_view
+ (
+ bitmapData.Width ,
+ bitmapData.Height,
+ gil_reinterpret_cast_c<typename MyView::value_type const *>( bitmapData.Scan0 ),
+ bitmapData.Stride
+ ),
+ view,
+ converter
+ );
+ verify_result( DllExports::GdipBitmapUnlockBits( pBitmap_, &bitmapData ) );
+ }
+
+
+ void raw_convert_to_prepared_view( view_data_t const & view_data ) const
+ {
+ BOOST_ASSERT( view_data.Scan0 );
+
+ using namespace detail ;
+ using namespace Gdiplus;
+
+ BitmapData * const pMutableBitmapData( const_cast<BitmapData *>( static_cast<BitmapData const *>( &view_data ) ) );
+ GpStatus const load_result
+ (
+ DllExports::GdipBitmapLockBits
+ (
+ pBitmap_,
+ view_data.p_roi_,
+ ImageLockModeRead | ImageLockModeUserInputBuf,
+ view_data.PixelFormat,
+ pMutableBitmapData
+ )
+ );
+ GpStatus const unlock_result( DllExports::GdipBitmapUnlockBits( pBitmap_, pMutableBitmapData ) );
+ ensure_result( load_result );
+ verify_result( unlock_result );
+ }
+
+
+ void raw_copy_to_prepared_view( view_data_t const & view_data ) const
+ {
+ BOOST_ASSERT( view_data.Width == static_cast<UINT>( dimensions().x ) );
+ BOOST_ASSERT( view_data.Height == static_cast<UINT>( dimensions().y ) );
+ //...this need not hold as it can be used to perform GDI+ default
+ //internal colour conversion...maybe i'll provide another worker
+ //function...
+ //BOOST_ASSERT( view_data.PixelFormat == format () );
+ raw_convert_to_prepared_view( view_data );
+ }
+
+
+ static std::size_t cached_format_size( format_t const format )
+ {
+ return Gdiplus::GetPixelFormatSize( format );
+ }
+
+private:
+ template <Gdiplus::PixelFormat desired_format>
+ void pre_palettized_conversion( mpl::true_ /*is_indexed*/ )
+ {
+ #if (GDIPVER >= 0x0110)
+ // A GDI+ 1.1 (a non-distributable version, distributed with MS Office
+ // 2003 and MS Windows Vista and MS Windows 7) 'enhanced'/'tuned'
+ // version of the conversion routine for indexed/palettized image
+ // formats. Unless/until proven useful, pretty much still a GDI+ 1.1
+ // tester...
+
+ BOOST_ASSERT( !has_alpha<desired_format>::value && "Is this possible for indexed formats?" );
+ std::size_t const number_of_pixel_bits ( pixel_size<desired_format>::value );
+ std::size_t const number_of_palette_colours( static_cast<std::size_t>( ( 2 << ( number_of_pixel_bits - 1 ) ) - 1 ) );
+ std::size_t const palette_size ( sizeof( ColorPalette ) + number_of_palette_colours * sizeof( ARGB ) );
+ aligned_storage
+ <
+ palette_size,
+ alignment_of<ColorPalette>::value
+ >::type palette_storage;
+ ColorPalette & palette( *static_cast<ColorPalette *>( palette_storage.address() ) );
+ palette.Flags = ( PaletteFlagsHasAlpha * has_alpha<desired_format>::value ) |
+ ( PaletteFlagsGrayScale * is_same<typename color_space_type<View>::type, gray_t>::value );
+ palette.Count = number_of_palette_colours;
+
+ verify_result
+ (
+ DllExports::GdipInitializePalette
+ (
+ &palette,
+ PaletteTypeOptimal,
+ number_of_palette_colours,
+ has_alpha<desired_format>::value,
+ pBitmap_
+ )
+ );
+
+ ensure_result
+ (
+ DllExports::GdipBitmapConvertFormat
+ (
+ pBitmap_,
+ desired_format,
+ DitherTypeErrorDiffusion,
+ PaletteTypeOptimal,
+ &palette,
+ 50
+ )
+ );
+ #endif // (GDIPVER >= 0x0110)
+ }
+
+ template <Gdiplus::PixelFormat desired_format>
+ void pre_palettized_conversion( mpl::false_ /*not is_indexed*/ ) const {}
+
+private:
+ friend class detail::gp_view_base;
+
+ Gdiplus::GpBitmap * /*const*/ pBitmap_;
+};
+
+#if defined(BOOST_MSVC)
+# pragma warning( pop )
+#endif
+
+namespace detail
+{
+//------------------------------------------------------------------------------
+
+////////////////////////////////////////////////////////////////////////////////
+///
+/// \class gp_view_base
+///
+////////////////////////////////////////////////////////////////////////////////
+
+class gp_view_base : noncopyable
+{
+public:
+ ~gp_view_base()
+ {
+ detail::verify_result( Gdiplus::DllExports::GdipBitmapUnlockBits( &bitmap_, &bitmapData_ ) );
+ }
+
+protected:
+ gp_view_base( gp_image & bitmap, unsigned int const lock_mode, gp_image::roi const * const p_roi = 0 )
+ :
+ bitmap_( *bitmap.pBitmap_ )
+ {
+ std::memset( &bitmapData_, 0, sizeof( bitmapData_ ) );
+
+ detail::ensure_result
+ (
+ Gdiplus::DllExports::GdipBitmapLockBits
+ (
+ &bitmap_,
+ p_roi,
+ lock_mode,
+ bitmap.format(),
+ &bitmapData_
+ )
+ );
+ }
+
+ template <typename Pixel>
+ typename type_from_x_iterator<Pixel *>::view_t
+ get_typed_view()
+ {
+ //todo assert correct type...
+ interleaved_view<Pixel *>( bitmapData_.Width, bitmapData_.Height, bitmapData_.Scan0, bitmapData_.Stride );
+ }
+
+private:
+ Gdiplus::GpBitmap & bitmap_ ;
+ Gdiplus::BitmapData bitmapData_;
+};
+
+
+
+////////////////////////////////////////////////////////////////////////////////
+///
+/// \class gp_view
+///
+////////////////////////////////////////////////////////////////////////////////
+
+template <typename Pixel>
+class gp_view
+ :
+ private gp_view_base,
+ public type_from_x_iterator<Pixel *>::view_t
+{
+public:
+ gp_view( gp_image & image, gp_image::roi const * const p_roi = 0 )
+ :
+ gp_view_base( image, ImageLockModeRead | ( is_const<Pixel>::value * ImageLockModeWrite ), p_roi ),
+ type_from_x_iterator<Pixel *>::view_t( get_typed_view<Pixel>() )
+ {}
+};
+
+
+//...mhmh...to be seen if necessary...
+//template <typename Pixel>
+//class gp_const_view
+// :
+// private gp_view_base,
+// public type_from_x_iterator<Pixel const *>::view_t
+//{
+//public:
+// gp_const_view( gp_image & image, gp_image::roi const * const p_roi = 0 )
+// :
+// gp_view_base( image, ImageLockModeRead ),
+// type_from_x_iterator<Pixel const *>::view_t( get_typed_view<Pixel const>() )
+// {}
+//};
+
+
+//...mhmh...to be implemented...
+//template <class Impl, class SupportedPixelFormats, class ROI>
+//inline
+//typename backend<Impl, SupportedPixelFormats, ROI>::view_t
+//view( backend<Impl, SupportedPixelFormats, ROI> & img );// { return img._view; }
+
+//------------------------------------------------------------------------------
+} // namespace detail
+//------------------------------------------------------------------------------
+} // namespace gil
+//------------------------------------------------------------------------------
+} // namespace boost
+//------------------------------------------------------------------------------
+#endif // gp_private_base_hpp

Copied and modified: sandbox/gil/boost/gil/extension/io2/backends/libjpeg/backend.hpp (from r85344, sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp)
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp Tue Aug 13 18:43:39 2013 (r85344, copy source)
+++ sandbox/gil/boost/gil/extension/io2/backends/libjpeg/backend.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85502)
@@ -1,11 +1,12 @@
 ////////////////////////////////////////////////////////////////////////////////
 ///
-/// \file libjpeg_image.hpp
-/// -----------------------
+/// \file backend.hpp
+/// -----------------
 ///
-/// Copyright (c) Domagoj Saric 2010.
+/// Copyright (c) Domagoj Saric 2010.-2013
 ///
-/// Use, modification and distribution is subject to the Boost Software License, Version 1.0.
+/// Use, modification and distribution is subject to the
+/// Boost Software License, Version 1.0.
 /// (See accompanying file LICENSE_1_0.txt or copy at
 /// http://www.boost.org/LICENSE_1_0.txt)
 ///
@@ -13,14 +14,16 @@
 ///
 ////////////////////////////////////////////////////////////////////////////////
 //------------------------------------------------------------------------------
+#ifndef backend_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
+#define backend_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
 #pragma once
-#ifndef libjpeg_image_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
-#define libjpeg_image_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
 //------------------------------------------------------------------------------
-#include "backend.hpp"
-#include "detail/io_error.hpp"
-#include "detail/libx_shared.hpp"
-#include "detail/shared.hpp"
+#include "boost/gil/extension/io2/backends/detail/backend.hpp"
+
+#include "boost/gil/extension/io2/detail/io_error.hpp"
+#include "boost/gil/extension/io2/detail/libx_shared.hpp"
+#include "boost/gil/extension/io2/detail/platform_specifics.hpp"
+#include "boost/gil/extension/io2/detail/shared.hpp"
 
 #include <boost/array.hpp>
 #include <boost/range/size.hpp>
@@ -86,14 +89,84 @@
     } libjpeg_object;
 };
 
+//------------------------------------------------------------------------------
+} // namespace detail
+
+
+class libjpeg_image;
+
+template <>
+struct backend_traits<libjpeg_image>
+{
+ typedef ::J_COLOR_SPACE format_t;
+ typedef detail::libjpeg_supported_pixel_formats supported_pixel_formats_t;
+ typedef detail::libjpeg_roi roi_t;
+ typedef detail::view_data_t view_data_t;
+
+ struct gil_to_native_format
+ {
+ template <typename Pixel, bool IsPlanar>
+ struct apply : detail::gil_to_libjpeg_format<Pixel, IsPlanar> {};
+ };
+
+ template <typename Pixel, bool IsPlanar>
+ struct is_supported : detail::libjpeg_is_supported<Pixel, IsPlanar> {};
+
+ typedef mpl::map3
+ <
+ mpl::pair<memory_range_t , detail::seekable_input_memory_range_extender<libjpeg_image> >,
+ mpl::pair<FILE , libjpeg_image >,
+ mpl::pair<char const *, detail::input_c_str_for_mmap_extender <libjpeg_image> >
+ > native_sources;
+
+ typedef mpl::map2
+ <
+ mpl::pair<FILE , detail::libjpeg_writer>,
+ mpl::pair<char const *, detail::libjpeg_writer>
+ > native_sinks;
+
+ typedef mpl::vector1_c<format_tag, jpeg> supported_image_formats;
+
+ typedef view_data_t writer_view_data_t;
+
+ BOOST_STATIC_CONSTANT( unsigned int, desired_alignment = sizeof( void * ) );
+ BOOST_STATIC_CONSTANT( bool , builtin_conversion = true );
+}; // struct backend_traits<libjpeg_image>
+
+
+////////////////////////////////////////////////////////////////////////////////
+///
+/// \class libjpeg_image
+///
+////////////////////////////////////////////////////////////////////////////////
 
 #if defined( BOOST_MSVC )
 # pragma warning( push )
 # pragma warning( disable : 4127 ) // "conditional expression is constant"
 #endif
 
-class libjpeg_base : private libjpeg_object_wrapper_t
+class libjpeg_image
+ :
+ private detail::libjpeg_object_wrapper_t,
+ public detail::backend<libjpeg_image>
 {
+public:
+ struct guard {};
+
+public:
+ static image_type_id image_format_id(format_t const closest_gil_supported_format)
+ {
+ switch ( closest_gil_supported_format )
+ {
+ case JCS_RGB : return 0;
+ case JCS_GRAYSCALE: return 1;
+ case JCS_CMYK : return 2;
+ default:
+ BOOST_ASSERT( closest_gil_supported_format == JCS_UNKNOWN );
+ return unsupported_format;
+ }
+ }
+
 protected:
     struct for_decompressor {};
     struct for_compressor {};
@@ -102,24 +175,24 @@
     libjpeg_base( for_decompressor ) BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
     {
         initialize_error_handler();
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
+ #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
+ if ( setjmp( error_handler_target() ) )
+ throw_jpeg_error();
+ #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
         jpeg_create_decompress( &decompressor() );
     }
 
     libjpeg_base( for_compressor ) BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
     {
         initialize_error_handler();
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
+ #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
+ if ( setjmp( error_handler_target() ) )
+ throw_jpeg_error();
+ #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
         jpeg_create_compress( &compressor() );
     }
 
- ~libjpeg_base() { jpeg_destroy( &common() ); }
+ ~libjpeg_base() BOOST_BF_NOTHROW { jpeg_destroy( &common() ); }
 
     void abort() const { jpeg_abort( &mutable_this().common() ); }
 
@@ -138,9 +211,9 @@
         return *static_cast<libjpeg_base *>( gil_reinterpret_cast<libjpeg_object_wrapper_t *>( p_libjpeg_object ) );
     }
 
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jmp_buf & error_handler_target() const { return longjmp_target_; }
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
+#ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
+ jmp_buf & error_handler_target() const { return longjmp_target_; }
+#endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
 
     static void fatal_error_handler( j_common_ptr const p_cinfo )
     {
@@ -195,837 +268,14 @@
     }
 
 private:
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- mutable jmp_buf longjmp_target_;
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- jpeg_error_mgr jerr_ ;
-};
-
-
-struct decompression_setup_data_t
-{
- decompression_setup_data_t( J_COLOR_SPACE const format, JSAMPROW const buffer, libjpeg_roi::offset_t const offset )
- : format_( format ), buffer_( buffer ), offset_( offset ) {}
-
- J_COLOR_SPACE /*const*/ format_;
- JSAMPROW /*const*/ buffer_;
- libjpeg_roi::offset_t /*const*/ offset_;
-};
-
-struct view_data_t : decompression_setup_data_t
-{
- template <class View>
- /*explicit*/ view_data_t( View const & view, libjpeg_roi::offset_t const offset = 0 )
- :
- decompression_setup_data_t
- (
- gil_to_libjpeg_format<typename View::value_type, is_planar<View>::value>::value,
- backend_base::get_raw_data( view ),
- offset
- ),
- height_( view.height() ),
- width_ ( view.width () ),
- stride_( view.pixels().row_size() ),
- number_of_channels_( num_channels<View>::value )
- {
- BOOST_STATIC_ASSERT(( libjpeg_is_supported<typename View::value_type, is_planar<View>::value>::value ));
- }
-
- void set_format( J_COLOR_SPACE const format ) { format_ = format; }
-
- unsigned int /*const*/ height_;
- unsigned int /*const*/ width_ ;
- unsigned int /*const*/ stride_;
- unsigned int number_of_channels_;
-};
-
-
-class libjpeg_writer
- :
- private libjpeg_base,
- public configure_on_write_writer
-{
-protected:
- BOOST_STATIC_CONSTANT( bool, auto_closes_device = true );
-
-public:
- explicit libjpeg_writer( char const * const p_target_file_name )
- :
- libjpeg_base( for_compressor() )
- {
- setup_destination( p_target_file_name );
- }
-
- explicit libjpeg_writer( FILE & file )
- :
- libjpeg_base( for_compressor() )
- {
- setup_destination( file );
- }
-
- ~libjpeg_writer()
- {
- jpeg_finish_compress( &compressor() );
- }
-
- jpeg_compress_struct & lib_object() { return compressor(); }
- jpeg_compress_struct const & lib_object() const { return const_cast<libjpeg_writer &>( *this ).lib_object(); }
-
- void write_default( view_data_t const & view ) BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- setup_compression( view );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jpeg_set_defaults( &compressor() );
- //jpeg_set_quality( &compressor(), 100, false );
-
- do_write( view );
- }
-
- void write( view_data_t const & view )
- {
- setup_compression( view );
- do_write ( view );
- }
-
-private:
- void setup_compression( view_data_t const & view )
- {
- compressor().image_width = static_cast<JDIMENSION>( view.width_ );
- compressor().image_height = static_cast<JDIMENSION>( view.height_ );
- compressor().input_components = view.number_of_channels_;
- compressor().in_color_space = view.format_;
- }
-
- void do_write( view_data_t const & view ) BOOST_GIL_CAN_THROW
- {
- BOOST_ASSERT( view.format_ != JCS_UNKNOWN );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- jpeg_start_compress( &compressor(), false );
-
- JSAMPLE * p_row( view.buffer_ );
- JSAMPLE * const p_end( memunit_advanced( view.buffer_, view.height_ * view.stride_ ) );
- while ( p_row < p_end )
- {
- write_row( p_row );
- memunit_advance( p_row, view.stride_ );
- }
- }
-
- void write_row( JSAMPLE * p_row ) BOOST_GIL_CAN_THROW
- {
- BOOST_VERIFY( jpeg_write_scanlines( &compressor(), &p_row, 1 ) == 1 );
- }
-
- static libjpeg_writer & get_writer( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( static_cast<libjpeg_writer &>( base( gil_reinterpret_cast<j_common_ptr>( p_cinfo ) ) ) );
- BOOST_ASSERT( p_cinfo->dest == &writer.destination_manager_ );
- return writer;
- }
-
- void setup_destination()
- {
- destination_manager_.next_output_byte = 0;
- destination_manager_.free_in_buffer = 0;
-
- BOOST_ASSERT( compressor().dest == NULL );
- compressor().dest = &destination_manager_;
- }
-
- void setup_destination( FILE & file )
- {
- setup_destination();
-
- compressor().client_data = &file;
-
- destination_manager_.init_destination = &init_destination ;
- destination_manager_.empty_output_buffer = &empty_FILE_buffer ;
- destination_manager_.term_destination = &term_FILE_destination;
- }
-
- void setup_destination( char const * const p_file_name )
- {
- int const file_descriptor( /*std*/::open( p_file_name, BF_MSVC_SPECIFIC( O_BINARY | ) O_CREAT | O_WRONLY, S_IREAD | S_IWRITE ) );
- if ( file_descriptor < 0 )
- throw_jpeg_error();
-
- setup_destination();
-
- compressor().client_data = reinterpret_cast<void *>( file_descriptor );
-
- destination_manager_.init_destination = &init_destination ;
- destination_manager_.empty_output_buffer = &empty_fd_buffer ;
- destination_manager_.term_destination = &term_and_close_fd_destination;
- }
-
- static void BF_CDECL init_destination( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
-
- writer.destination_manager_.next_output_byte = writer.write_buffer_.begin();
- writer.destination_manager_.free_in_buffer = writer.write_buffer_.size ();
- }
-
- void write_FILE_bytes( std::size_t const number_of_bytes )
- {
- if
- (
- /*std*/::fwrite
- (
- write_buffer_.begin(),
- number_of_bytes,
- 1,
- static_cast<FILE *>( compressor().client_data )
- ) != 1
- )
- fatal_error_handler( &common() );
- }
-
- void write_fd_bytes( std::size_t const number_of_bytes )
- {
- if
- (
- /*std*/::write
- (
- reinterpret_cast<long>( compressor().client_data ),
- write_buffer_.begin(),
- number_of_bytes
- ) != static_cast<int>( number_of_bytes )
- )
- fatal_error_handler( &common() );
- }
-
- static boolean BF_CDECL empty_FILE_buffer( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
- writer.write_FILE_bytes( writer.write_buffer_.size() );
- init_destination( p_cinfo );
- return true;
- }
-
- static boolean BF_CDECL empty_fd_buffer( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
- writer.write_fd_bytes( writer.write_buffer_.size() );
- init_destination( p_cinfo );
- return true;
- }
-
- static void BF_CDECL term_FILE_destination( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
-
- std::size_t const remaining_bytes( writer.write_buffer_.size() - writer.destination_manager_.free_in_buffer );
-
- writer.write_FILE_bytes( remaining_bytes );
- }
-
- static void BF_CDECL term_fd_destination( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
-
- std::size_t const remaining_bytes( writer.write_buffer_.size() - writer.destination_manager_.free_in_buffer );
-
- writer.write_fd_bytes( remaining_bytes );
- }
-
- // Ensure that jpeg_finish_compress() is called so that this gets called...
- static void BF_CDECL term_and_close_FILE_destination( j_compress_ptr const p_cinfo )
- {
- term_FILE_destination( p_cinfo );
- BOOST_VERIFY( /*std*/::fclose( static_cast<FILE *>( get_writer( p_cinfo ).compressor().client_data ) ) == 0 );
- }
-
- static void BF_CDECL term_and_close_fd_destination( j_compress_ptr const p_cinfo )
- {
- term_fd_destination( p_cinfo );
- BOOST_VERIFY( /*std*/::close( reinterpret_cast<long>( get_writer( p_cinfo ).compressor().client_data ) ) == 0 );
- }
-
-private:
- jpeg_destination_mgr destination_manager_;
- array<unsigned char, 65536> write_buffer_ ;
-};
-//------------------------------------------------------------------------------
-} // namespace detail
-
-
-class libjpeg_image;
-
-template <>
-struct backend_traits<libjpeg_image>
-{
- typedef ::J_COLOR_SPACE format_t;
- typedef detail::libjpeg_supported_pixel_formats supported_pixel_formats_t;
- typedef detail::libjpeg_roi roi_t;
- typedef detail::view_data_t view_data_t;
-
- struct gil_to_native_format
- {
- template <typename Pixel, bool IsPlanar>
- struct apply : detail::gil_to_libjpeg_format<Pixel, IsPlanar> {};
- };
-
- template <typename Pixel, bool IsPlanar>
- struct is_supported : detail::libjpeg_is_supported<Pixel, IsPlanar> {};
-
- typedef mpl::map3
- <
- mpl::pair<memory_range_t , detail::seekable_input_memory_range_extender<libjpeg_image> >,
- mpl::pair<FILE , libjpeg_image >,
- mpl::pair<char const *, detail::input_c_str_for_mmap_extender <libjpeg_image> >
- > native_sources;
-
- typedef mpl::map2
- <
- mpl::pair<FILE , detail::libjpeg_writer>,
- mpl::pair<char const *, detail::libjpeg_writer>
- > native_sinks;
-
- typedef mpl::vector1_c<format_tag, jpeg> supported_image_formats;
-
- typedef view_data_t writer_view_data_t;
-
- BOOST_STATIC_CONSTANT( unsigned int, desired_alignment = sizeof( void * ) );
- BOOST_STATIC_CONSTANT( bool , builtin_conversion = true );
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libjpeg_image
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libjpeg_image
- :
- public detail::libjpeg_base,
- public detail::backend<libjpeg_image>
-{
-public:
- struct guard {};
-
-public:
- format_t format() const { return decompressor().jpeg_color_space; }
-
- format_t closest_gil_supported_format() const
- {
- format_t const current_format( format() );
- #ifdef _DEBUG
- switch ( current_format )
- {
- case JCS_RGB :
- case JCS_YCbCr :
- case JCS_CMYK :
- case JCS_YCCK :
- case JCS_GRAYSCALE:
- case JCS_UNKNOWN :
- break;
-
- default:
- BOOST_ASSERT( !"Unknown format code." );
- }
- #endif
-
- switch ( current_format )
- {
- case JCS_YCbCr: return JCS_RGB ;
- case JCS_YCCK : return JCS_CMYK ;
- default : return current_format;
- }
- }
-
- image_type_id current_image_format_id() const
- {
- return image_format_id( closest_gil_supported_format() );
- }
-
- static image_type_id image_format_id( format_t const closest_gil_supported_format )
- {
- switch ( closest_gil_supported_format )
- {
- case JCS_RGB : return 0;
- case JCS_GRAYSCALE: return 1;
- case JCS_CMYK : return 2;
- default:
- BOOST_ASSERT( closest_gil_supported_format == JCS_UNKNOWN );
- return unsupported_format;
- }
- }
-
- point2<std::ptrdiff_t> dimensions() const
- {
- // Implementation note:
- // A user might have setup output image scaling through the low-level
- // lib_object accessor.
- // (17.10.2010.) (Domagoj Saric)
- if ( dirty_output_dimensions_ )
- {
- jpeg_calc_output_dimensions( &const_cast<libjpeg_image &>( *this ).lib_object() );
- dirty_output_dimensions_ = false;
- }
- return point2<std::ptrdiff_t>( decompressor().output_width, decompressor().output_height );
- }
-
-public: // Low-level (row, strip, tile) access
- void read_row( sequential_row_read_state, unsigned char * const p_row_storage ) const
- {
- read_scanline( p_row_storage );
- }
-
- jpeg_decompress_struct & lib_object() { dirty_output_dimensions_ = true; return decompressor(); }
- jpeg_decompress_struct const & lib_object() const { return decompressor(); }
-
-public: /// \ingroup Construction
- explicit libjpeg_image( memory_range_t & memory_chunk )
- :
- libjpeg_base( for_decompressor() )
- {
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- setup_source( memory_chunk );
-
- read_header();
- }
-
- explicit libjpeg_image( FILE & file )
- :
- libjpeg_base( for_decompressor() )
- {
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- setup_source( file );
-
- read_header();
- }
-
-private: // Private interface for the base backend<> class.
- // Implementation note:
- // MSVC 10 accepts friend base_t and friend class base_t, Clang 2.8
- // accepts friend class base_t, Apple Clang 1.6 and GCC (4.2 and 4.6) accept
- // neither.
- // (13.01.2011.) (Domagoj Saric)
- friend class detail::backend<libjpeg_image>;
-
- void raw_convert_to_prepared_view( detail::view_data_t const & view_data ) const BOOST_GIL_CAN_THROW
- {
- setup_decompression( view_data );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- JSAMPROW scanlines[ 4 ] =
- {
- view_data.buffer_,
- scanlines[ 0 ] + view_data.stride_,
- scanlines[ 1 ] + view_data.stride_,
- scanlines[ 2 ] + view_data.stride_
- };
-
- BOOST_ASSERT( boost::size( scanlines ) >= decompressor().rec_outbuf_height );
- unsigned int scanlines_to_read( view_data.height_ );
- for ( ; ; )
- {
- BOOST_ASSERT( scanlines_to_read <= ( decompressor().output_height - decompressor().output_scanline ) );
- unsigned int const lines_read
- (
- read_scanlines
- (
- scanlines,
- std::min<std::size_t>( boost::size( scanlines ), scanlines_to_read )
- )
- );
- scanlines_to_read -= lines_read;
- if ( !scanlines_to_read )
- return;
-
- scanlines[ 0 ] = scanlines[ lines_read - 1 ] + view_data.stride_;
- scanlines[ 1 ] = scanlines[ 0 ] + view_data.stride_;
- scanlines[ 2 ] = scanlines[ 1 ] + view_data.stride_;
- scanlines[ 3 ] = scanlines[ 2 ] + view_data.stride_;
- }
- }
-
-
- template <class MyView, class TargetView, class Converter>
- void generic_convert_to_prepared_view( TargetView const & view, Converter const & converter ) const
- {
- using namespace detail;
-
- typedef typename MyView::value_type pixel_t;
- std::size_t const scanline_length ( decompressor().image_width * decompressor().num_components );
- scoped_array<JSAMPLE> const p_scanline_buffer( new JSAMPLE[ scanline_length ] );
- JSAMPROW scanline ( p_scanline_buffer.get() );
- JSAMPROW const scanline_end( scanline + scanline_length );
-
- format_t const my_format( gil_to_libjpeg_format<typename MyView::value_type, is_planar<MyView>::value>::value );
- BOOST_ASSERT( this->closest_gil_supported_format() == my_format );
- setup_decompression
- (
- decompression_setup_data_t
- (
- my_format,
- scanline,
- detail::get_offset<offset_t>( view )
- )
- );
-
- BOOST_ASSERT( decompressor().output_width == decompressor().image_width );
- BOOST_ASSERT( decompressor().output_components == decompressor().num_components );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- unsigned int const scanlines_to_read( detail::original_view( view ).dimensions().y );
- for ( unsigned int scanline_index( 0 ); scanline_index < scanlines_to_read; ++scanline_index )
- {
- read_scanline( scanline );
-
- typedef typename get_original_view_t<TargetView>::type::x_iterator target_x_iterator;
-
- pixel_t const * p_source_pixel( gil_reinterpret_cast_c<pixel_t const *>( scanline ) );
- target_x_iterator p_target_pixel( original_view( view ).row_begin( scanline_index ) );
- while ( p_source_pixel < gil_reinterpret_cast_c<pixel_t const *>( scanline_end ) )
- {
- converter( *p_source_pixel, *p_target_pixel );
- ++p_source_pixel;
- ++p_target_pixel;
- }
- }
- }
-
-
- void raw_copy_to_prepared_view( detail::view_data_t const & view_data ) const
- {
- BOOST_ASSERT( view_data.width_ == static_cast<unsigned int>( dimensions().x ) );
- BOOST_ASSERT( view_data.height_ == static_cast<unsigned int>( dimensions().y ) );
- BOOST_ASSERT( view_data.format_ == closest_gil_supported_format() );
- raw_convert_to_prepared_view( view_data );
- }
-
-
- static unsigned int cached_format_size( format_t const format )
- {
- switch ( format )
- {
- case JCS_RGB:
- case JCS_YCbCr:
- return 3;
- case JCS_CMYK:
- case JCS_YCCK:
- return 4;
- case JCS_GRAYSCALE:
- return 1;
-
- default:
- BOOST_ASSERT( !"Invalid or unknown format specified." );
- BF_UNREACHABLE_CODE
- return 0;
- }
- }
-
-private:
- void setup_decompression( detail::decompression_setup_data_t const & view_data ) const BOOST_GIL_CAN_THROW
- {
- unsigned int const state ( decompressor().global_state );
- unsigned int rows_to_skip( view_data.offset_ );
- if
- (
- ( state != DSTATE_SCANNING ) ||
- ( decompressor().out_color_space != view_data.format_ ) ||
- ( decompressor().output_scanline > static_cast<JDIMENSION>( view_data.offset_ ) )
- )
- {
- BOOST_ASSERT
- (
- ( state == DSTATE_READY ) ||
- ( state == DSTATE_SCANNING )
- );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- if ( state == DSTATE_SCANNING )
- abort();
-
- mutable_this().decompressor().out_color_space = view_data.format_;
- BOOST_VERIFY( jpeg_start_decompress( &mutable_this().decompressor() ) );
- BOOST_ASSERT( decompressor().output_scanline == 0 );
- }
- else
- rows_to_skip -= decompressor().output_scanline;
-
- if ( rows_to_skip )
- skip_rows( rows_to_skip, view_data.buffer_ );
- BOOST_ASSERT( decompressor().output_scanline == static_cast<JDIMENSION>( view_data.offset_ ) );
- }
-
- void skip_rows( unsigned int const number_of_rows_to_skip, JSAMPROW /*const*/ dummy_scanline_buffer ) const
- {
- BOOST_ASSERT( decompressor().raw_data_out == false );
- BOOST_ASSERT( decompressor().global_state == DSTATE_SCANNING );
- mutable_this().decompressor().raw_data_out = true;
- mutable_this().decompressor().global_state = DSTATE_RAW_OK;
-
- unsigned int const max_number_of_components( 4 ); // CMYK
- unsigned int const max_sampling_factor ( 2 ); // Documentation
- unsigned int const mcu_row_size ( max_sampling_factor * DCTSIZE ); // Documentation
- BOOST_ASSERT( decompressor().num_components <= max_number_of_components );
- BOOST_ASSERT( decompressor().max_v_samp_factor <= max_sampling_factor );
-
- JSAMPROW dummy_component_2d_array[ mcu_row_size ];
- JSAMPARRAY dummy_scan_lines [ max_number_of_components ];
-
- std::fill( begin( dummy_component_2d_array ), end( dummy_component_2d_array ), dummy_scanline_buffer );
- std::fill( begin( dummy_scan_lines ), end( dummy_scan_lines ), &dummy_component_2d_array[ 0 ] );
-
- unsigned int number_of_rows_to_skip_using_scanlines
- (
- number_of_rows_to_skip % mcu_row_size
- );
- unsigned int number_of_rows_to_skip_using_raw
- (
- number_of_rows_to_skip - number_of_rows_to_skip_using_scanlines
- );
-
- while ( number_of_rows_to_skip_using_raw )
- {
- unsigned int const lines_to_read( boost::size( dummy_component_2d_array ) );
- read_raw_data( dummy_scan_lines, lines_to_read );
- number_of_rows_to_skip_using_raw -= lines_to_read;
- }
-
- mutable_this().decompressor().raw_data_out = false;
- mutable_this().decompressor().global_state = DSTATE_SCANNING;
- while ( number_of_rows_to_skip_using_scanlines-- )
- {
- read_scanline( dummy_scanline_buffer );
- }
- }
-
- unsigned int read_scanlines( JSAMPROW scanlines[], unsigned int const scanlines_to_read ) const BOOST_GIL_CAN_THROW
- {
- return jpeg_read_scanlines
- (
- &mutable_this().decompressor(),
- scanlines,
- scanlines_to_read
- );
- }
-
- void read_scanline( JSAMPROW scanline ) const BOOST_GIL_CAN_THROW
- {
- BOOST_VERIFY
- (
- read_scanlines( &scanline, 1 ) == 1
- );
- }
-
- void read_raw_data( JSAMPARRAY scanlines[], unsigned int const scanlines_to_read ) const BOOST_GIL_CAN_THROW
- {
- BOOST_VERIFY
- (
- jpeg_read_raw_data
- (
- &mutable_this().decompressor(),
- scanlines,
- scanlines_to_read
- ) == scanlines_to_read
- );
- }
-
- libjpeg_image & mutable_this() const { return static_cast<libjpeg_image &>( libjpeg_base::mutable_this() ); }
-
-private:
- friend class libjpeg_view_base;
-
- void read_header()
- {
- BOOST_VERIFY( jpeg_read_header( &decompressor(), true ) == JPEG_HEADER_OK );
-
- // Implementation note:
- // To enable users to setup output scaling we use the output
- // dimensions to report the image dimensions in the dimensions() getter
- // so we have to manually initialize them here.
- // (17.10.2010.) (Domagoj Saric)
- BOOST_ASSERT( decompressor().output_width == 0 );
- BOOST_ASSERT( decompressor().output_height == 0 );
-
- decompressor().output_width = decompressor().image_width ;
- decompressor().output_height = decompressor().image_height;
-
- dirty_output_dimensions_ = false;
-
- detail::io_error_if( decompressor().data_precision != BITS_IN_JSAMPLE, "Unsupported image file data precision." );
- }
-
- // Unextracted "libjpeg_reader" interface.
- void setup_source()
- {
- BOOST_ASSERT( decompressor().src == NULL );
- decompressor().src = &source_manager_;
- }
-
- void setup_source( memory_range_t & memory_chunk )
- {
- setup_source();
-
- decompressor().client_data = &memory_chunk;
-
- source_manager_.next_input_byte = memory_chunk.begin();
- source_manager_.bytes_in_buffer = memory_chunk.size ();
-
- source_manager_.init_source = &init_memory_chunk_source;
- source_manager_.fill_input_buffer = &fill_memory_chunk_buffer;
- source_manager_.skip_input_data = &skip_memory_chunk_data ;
- source_manager_.resync_to_restart = &jpeg_resync_to_restart ;
- source_manager_.term_source = &term_memory_chunk_source;
- }
-
- void setup_source( FILE & file )
- {
- setup_source();
-
- decompressor().client_data = &file;
-
- source_manager_.next_input_byte = read_buffer_.begin();
- source_manager_.bytes_in_buffer = 0;
-
- source_manager_.init_source = &init_FILE_source ;
- source_manager_.fill_input_buffer = &fill_FILE_buffer ;
- source_manager_.skip_input_data = &skip_FILE_data ;
- source_manager_.resync_to_restart = &jpeg_resync_to_restart;
- source_manager_.term_source = &term_FILE_source ;
- }
-
-
- static void BF_CDECL init_FILE_source( j_decompress_ptr const p_cinfo )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- reader.source_manager_.next_input_byte = reader.read_buffer_.begin();
- reader.source_manager_.bytes_in_buffer = 0;
- }
-
- static boolean BF_CDECL fill_FILE_buffer( j_decompress_ptr const p_cinfo )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- std::size_t bytes_read
- (
- /*std*/::fread
- (
- reader.read_buffer_.begin(),
- 1,
- reader.read_buffer_.size(),
- static_cast<FILE *>( reader.decompressor().client_data )
- )
- );
-
- if ( bytes_read == 0 )
- {
- // Insert a fake EOI marker (see the comment for the default library
- // implementation).
- reader.read_buffer_[ 0 ] = 0xFF;
- reader.read_buffer_[ 1 ] = JPEG_EOI;
-
- bytes_read = 2;
- }
-
- reader.source_manager_.next_input_byte = reader.read_buffer_.begin();
- reader.source_manager_.bytes_in_buffer = bytes_read;
-
- return true;
- }
-
- static void BF_CDECL skip_FILE_data( j_decompress_ptr const p_cinfo, long num_bytes )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- if ( static_cast<std::size_t>( num_bytes ) <= reader.source_manager_.bytes_in_buffer )
- {
- reader.source_manager_.next_input_byte += num_bytes;
- reader.source_manager_.bytes_in_buffer -= num_bytes;
- }
- else
- {
- num_bytes -= reader.source_manager_.bytes_in_buffer;
- /*std*/::fseek( static_cast<FILE *>( reader.decompressor().client_data ), num_bytes, SEEK_CUR ); //...failure?
- reader.source_manager_.next_input_byte = 0;
- reader.source_manager_.bytes_in_buffer = 0;
- }
- }
-
- static void BF_CDECL term_FILE_source( j_decompress_ptr /*p_cinfo*/ )
- {
- }
-
- // Ensure that jpeg_finish_decompress() is called so that this gets called...
- static void BF_CDECL term_and_close_FILE_source( j_decompress_ptr const p_cinfo )
- {
- term_FILE_source( p_cinfo );
- BOOST_VERIFY( /*std*/::fclose( static_cast<FILE *>( get_reader( p_cinfo ).decompressor().client_data ) ) == 0 );
- }
-
- static void BF_CDECL init_memory_chunk_source( j_decompress_ptr /*p_cinfo*/ )
- {
- }
-
- static boolean BF_CDECL fill_memory_chunk_buffer( j_decompress_ptr /*p_cinfo*/ )
- {
- BF_UNREACHABLE_CODE
- return true;
- }
-
- static void BF_CDECL skip_memory_chunk_data( j_decompress_ptr const p_cinfo, long num_bytes )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- BOOST_ASSERT( static_cast<std::size_t>( num_bytes ) <= reader.source_manager_.bytes_in_buffer );
- reader.source_manager_.next_input_byte += num_bytes;
- reader.source_manager_.bytes_in_buffer -= num_bytes;
- }
-
- static void BF_CDECL term_memory_chunk_source( j_decompress_ptr /*p_cinfo*/ )
- {
- }
+#ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
+ mutable jmp_buf longjmp_target_;
+#endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
 
- static libjpeg_image & get_reader( j_decompress_ptr const p_cinfo )
- {
- libjpeg_image & reader( static_cast<libjpeg_image &>( base( gil_reinterpret_cast<j_common_ptr>( p_cinfo ) ) ) );
- BOOST_ASSERT( p_cinfo->src == &reader.source_manager_ );
- return reader;
- }
+ jpeg_error_mgr jerr_;
+}; // class libjpeg_image
 
-private:
- jpeg_source_mgr source_manager_ ;
- mutable bool dirty_output_dimensions_;
- array<JOCTET, 4096> read_buffer_ ;//...zzz...extract to a wrapper...not needed for in memory sources...
-};
-
-#if defined(BOOST_MSVC)
+#if defined( BOOST_MSVC )
 # pragma warning( pop )
 #endif
 
@@ -1088,8 +338,8 @@
 } // namespace boost
 //------------------------------------------------------------------------------
 
-#if defined(BOOST_MSVC)
+#if defined( BOOST_MSVC )
     #pragma warning( pop )
 #endif // MSVC
 
-#endif // libjpeg_image_hpp
+#endif // backend_hpp

Copied and modified: sandbox/gil/boost/gil/extension/io2/backends/libjpeg/reader.hpp (from r85344, sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp)
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp Tue Aug 13 18:43:39 2013 (r85344, copy source)
+++ sandbox/gil/boost/gil/extension/io2/backends/libjpeg/reader.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85502)
@@ -3,7 +3,7 @@
 /// \file libjpeg_image.hpp
 /// -----------------------
 ///
-/// Copyright (c) Domagoj Saric 2010.
+/// Copyright (c) Domagoj Saric 2010.-2013.
 ///
 /// Use, modification and distribution is subject to the Boost Software License, Version 1.0.
 /// (See accompanying file LICENSE_1_0.txt or copy at
@@ -13,34 +13,24 @@
 ///
 ////////////////////////////////////////////////////////////////////////////////
 //------------------------------------------------------------------------------
+#ifndef reader_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
+#define reader_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
 #pragma once
-#ifndef libjpeg_image_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
-#define libjpeg_image_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
 //------------------------------------------------------------------------------
 #include "backend.hpp"
-#include "detail/io_error.hpp"
-#include "detail/libx_shared.hpp"
-#include "detail/shared.hpp"
+#include "../detail/reader.hpp"
+
+#include "boost/gil/extension/io2/detail/io_error.hpp"
+#include "boost/gil/extension/io2/detail/libx_shared.hpp"
+#include "boost/gil/extension/io2/detail/platform_specifics.hpp"
+#include "boost/gil/extension/io2/detail/shared.hpp"
+
+#include "boost/gil/image_view_factory.hpp"
 
 #include <boost/array.hpp>
-#include <boost/range/size.hpp>
+#include <boost/mpl/vector.hpp>
 #include <boost/smart_ptr/scoped_array.hpp>
 
-#define JPEG_INTERNALS
-#include "jpeglib.h"
-#undef JPEG_INTERNALS
-
-#ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- #include <csetjmp>
-#endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-#include <cstdlib>
-#if defined(BOOST_MSVC)
- #pragma warning( push )
- #pragma warning( disable : 4996 ) // "The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name."
- #include "io.h"
- #include "sys/stat.h"
-#endif // MSVC
-#include "fcntl.h"
 //------------------------------------------------------------------------------
 namespace boost
 {
@@ -52,157 +42,11 @@
 {
 //------------------------------------------------------------------------------
 
-BOOST_STATIC_ASSERT( BITS_IN_JSAMPLE == 8 );
-
-template <typename Pixel, bool isPlanar>
-struct gil_to_libjpeg_format : mpl::integral_c<J_COLOR_SPACE, JCS_UNKNOWN> {};
-
-template <> struct gil_to_libjpeg_format<rgb8_pixel_t , false> : mpl::integral_c<J_COLOR_SPACE, JCS_RGB > {};
-template <> struct gil_to_libjpeg_format<gray8_pixel_t, false> : mpl::integral_c<J_COLOR_SPACE, JCS_GRAYSCALE> {};
-template <> struct gil_to_libjpeg_format<cmyk8_pixel_t, false> : mpl::integral_c<J_COLOR_SPACE, JCS_CMYK > {};
-
-
-template <typename Pixel, bool IsPlanar>
-struct libjpeg_is_supported : mpl::bool_<gil_to_libjpeg_format<Pixel, IsPlanar>::value != JCS_UNKNOWN> {};
-
-
-typedef mpl::vector3
-<
- image<rgb8_pixel_t , false>,
- image<gray8_pixel_t, false>,
- image<cmyk8_pixel_t, false>
-> libjpeg_supported_pixel_formats;
-
-
-typedef generic_vertical_roi libjpeg_roi;
-
-
-struct libjpeg_object_wrapper_t
-{
- union aux_union
- {
- jpeg_compress_struct compressor_;
- jpeg_decompress_struct decompressor_;
- } libjpeg_object;
-};
-
-
 #if defined( BOOST_MSVC )
 # pragma warning( push )
 # pragma warning( disable : 4127 ) // "conditional expression is constant"
 #endif
 
-class libjpeg_base : private libjpeg_object_wrapper_t
-{
-protected:
- struct for_decompressor {};
- struct for_compressor {};
-
-protected:
- libjpeg_base( for_decompressor ) BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- initialize_error_handler();
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jpeg_create_decompress( &decompressor() );
- }
-
- libjpeg_base( for_compressor ) BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- initialize_error_handler();
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jpeg_create_compress( &compressor() );
- }
-
- ~libjpeg_base() { jpeg_destroy( &common() ); }
-
- void abort() const { jpeg_abort( &mutable_this().common() ); }
-
- jpeg_common_struct & common () { return *gil_reinterpret_cast<j_common_ptr>( &libjpeg_object.compressor_ ); }
- jpeg_common_struct const & common () const { return const_cast<libjpeg_base &>( *this ).common (); }
- jpeg_compress_struct & compressor () { return libjpeg_object.compressor_ ; }
- jpeg_compress_struct const & compressor () const { return const_cast<libjpeg_base &>( *this ).compressor (); }
- jpeg_decompress_struct & decompressor() { return libjpeg_object.decompressor_; }
- jpeg_decompress_struct const & decompressor() const { return const_cast<libjpeg_base &>( *this ).decompressor(); }
-
- libjpeg_base & mutable_this() const { return const_cast<libjpeg_base &>( *this ); }
-
- static libjpeg_base & base( jpeg_common_struct * const p_libjpeg_object )
- {
- BOOST_ASSERT( p_libjpeg_object );
- return *static_cast<libjpeg_base *>( gil_reinterpret_cast<libjpeg_object_wrapper_t *>( p_libjpeg_object ) );
- }
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jmp_buf & error_handler_target() const { return longjmp_target_; }
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- static void fatal_error_handler( j_common_ptr const p_cinfo )
- {
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- throw_jpeg_error();
- ignore_unused_variable_warning( p_cinfo );
- #else
- longjmp( base( p_cinfo ).error_handler_target(), 1 );
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- }
-
- static void throw_jpeg_error()
- {
- io_error( "LibJPEG error" );
- }
-
-private:
- void initialize_error_handler()
- {
- #ifndef NDEBUG
- jpeg_std_error( &jerr_ );
- #else
- std::memset( &jerr_, 0, sizeof( jerr_ ) );
- jerr_.emit_message = &emit_message ;
- jerr_.output_message = &output_message ;
- jerr_.format_message = &format_message ;
- jerr_.reset_error_mgr = &reset_error_mgr;
- #endif // NDEBUG
- jerr_.error_exit = &error_exit;
-
- common().err = &jerr_;
- }
-
- static void BF_CDECL error_exit( j_common_ptr const p_cinfo )
- {
- #ifndef NDEBUG
- p_cinfo->err->output_message( p_cinfo );
- #endif
-
- fatal_error_handler( p_cinfo );
- }
-
- static void BF_CDECL output_message( j_common_ptr /*p_cinfo*/ ) {}
- static void BF_CDECL emit_message ( j_common_ptr /*p_cinfo*/, int /*msg_level*/ ) {}
- static void BF_CDECL format_message( j_common_ptr /*p_cinfo*/, char * /*buffer*/ ) {}
-
- static void BF_CDECL reset_error_mgr( j_common_ptr const p_cinfo )
- {
- BOOST_ASSERT( p_cinfo->err->num_warnings == 0 );
- BOOST_ASSERT( p_cinfo->err->msg_code == 0 );
- ignore_unused_variable_warning( p_cinfo );
- }
-
-private:
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- mutable jmp_buf longjmp_target_;
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- jpeg_error_mgr jerr_ ;
-};
-
-
 struct decompression_setup_data_t
 {
     decompression_setup_data_t( J_COLOR_SPACE const format, JSAMPROW const buffer, libjpeg_roi::offset_t const offset )
@@ -240,265 +84,9 @@
     unsigned int number_of_channels_;
 };
 
-
-class libjpeg_writer
- :
- private libjpeg_base,
- public configure_on_write_writer
-{
-protected:
- BOOST_STATIC_CONSTANT( bool, auto_closes_device = true );
-
-public:
- explicit libjpeg_writer( char const * const p_target_file_name )
- :
- libjpeg_base( for_compressor() )
- {
- setup_destination( p_target_file_name );
- }
-
- explicit libjpeg_writer( FILE & file )
- :
- libjpeg_base( for_compressor() )
- {
- setup_destination( file );
- }
-
- ~libjpeg_writer()
- {
- jpeg_finish_compress( &compressor() );
- }
-
- jpeg_compress_struct & lib_object() { return compressor(); }
- jpeg_compress_struct const & lib_object() const { return const_cast<libjpeg_writer &>( *this ).lib_object(); }
-
- void write_default( view_data_t const & view ) BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- setup_compression( view );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jpeg_set_defaults( &compressor() );
- //jpeg_set_quality( &compressor(), 100, false );
-
- do_write( view );
- }
-
- void write( view_data_t const & view )
- {
- setup_compression( view );
- do_write ( view );
- }
-
-private:
- void setup_compression( view_data_t const & view )
- {
- compressor().image_width = static_cast<JDIMENSION>( view.width_ );
- compressor().image_height = static_cast<JDIMENSION>( view.height_ );
- compressor().input_components = view.number_of_channels_;
- compressor().in_color_space = view.format_;
- }
-
- void do_write( view_data_t const & view ) BOOST_GIL_CAN_THROW
- {
- BOOST_ASSERT( view.format_ != JCS_UNKNOWN );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- jpeg_start_compress( &compressor(), false );
-
- JSAMPLE * p_row( view.buffer_ );
- JSAMPLE * const p_end( memunit_advanced( view.buffer_, view.height_ * view.stride_ ) );
- while ( p_row < p_end )
- {
- write_row( p_row );
- memunit_advance( p_row, view.stride_ );
- }
- }
-
- void write_row( JSAMPLE * p_row ) BOOST_GIL_CAN_THROW
- {
- BOOST_VERIFY( jpeg_write_scanlines( &compressor(), &p_row, 1 ) == 1 );
- }
-
- static libjpeg_writer & get_writer( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( static_cast<libjpeg_writer &>( base( gil_reinterpret_cast<j_common_ptr>( p_cinfo ) ) ) );
- BOOST_ASSERT( p_cinfo->dest == &writer.destination_manager_ );
- return writer;
- }
-
- void setup_destination()
- {
- destination_manager_.next_output_byte = 0;
- destination_manager_.free_in_buffer = 0;
-
- BOOST_ASSERT( compressor().dest == NULL );
- compressor().dest = &destination_manager_;
- }
-
- void setup_destination( FILE & file )
- {
- setup_destination();
-
- compressor().client_data = &file;
-
- destination_manager_.init_destination = &init_destination ;
- destination_manager_.empty_output_buffer = &empty_FILE_buffer ;
- destination_manager_.term_destination = &term_FILE_destination;
- }
-
- void setup_destination( char const * const p_file_name )
- {
- int const file_descriptor( /*std*/::open( p_file_name, BF_MSVC_SPECIFIC( O_BINARY | ) O_CREAT | O_WRONLY, S_IREAD | S_IWRITE ) );
- if ( file_descriptor < 0 )
- throw_jpeg_error();
-
- setup_destination();
-
- compressor().client_data = reinterpret_cast<void *>( file_descriptor );
-
- destination_manager_.init_destination = &init_destination ;
- destination_manager_.empty_output_buffer = &empty_fd_buffer ;
- destination_manager_.term_destination = &term_and_close_fd_destination;
- }
-
- static void BF_CDECL init_destination( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
-
- writer.destination_manager_.next_output_byte = writer.write_buffer_.begin();
- writer.destination_manager_.free_in_buffer = writer.write_buffer_.size ();
- }
-
- void write_FILE_bytes( std::size_t const number_of_bytes )
- {
- if
- (
- /*std*/::fwrite
- (
- write_buffer_.begin(),
- number_of_bytes,
- 1,
- static_cast<FILE *>( compressor().client_data )
- ) != 1
- )
- fatal_error_handler( &common() );
- }
-
- void write_fd_bytes( std::size_t const number_of_bytes )
- {
- if
- (
- /*std*/::write
- (
- reinterpret_cast<long>( compressor().client_data ),
- write_buffer_.begin(),
- number_of_bytes
- ) != static_cast<int>( number_of_bytes )
- )
- fatal_error_handler( &common() );
- }
-
- static boolean BF_CDECL empty_FILE_buffer( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
- writer.write_FILE_bytes( writer.write_buffer_.size() );
- init_destination( p_cinfo );
- return true;
- }
-
- static boolean BF_CDECL empty_fd_buffer( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
- writer.write_fd_bytes( writer.write_buffer_.size() );
- init_destination( p_cinfo );
- return true;
- }
-
- static void BF_CDECL term_FILE_destination( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
-
- std::size_t const remaining_bytes( writer.write_buffer_.size() - writer.destination_manager_.free_in_buffer );
-
- writer.write_FILE_bytes( remaining_bytes );
- }
-
- static void BF_CDECL term_fd_destination( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
-
- std::size_t const remaining_bytes( writer.write_buffer_.size() - writer.destination_manager_.free_in_buffer );
-
- writer.write_fd_bytes( remaining_bytes );
- }
-
- // Ensure that jpeg_finish_compress() is called so that this gets called...
- static void BF_CDECL term_and_close_FILE_destination( j_compress_ptr const p_cinfo )
- {
- term_FILE_destination( p_cinfo );
- BOOST_VERIFY( /*std*/::fclose( static_cast<FILE *>( get_writer( p_cinfo ).compressor().client_data ) ) == 0 );
- }
-
- static void BF_CDECL term_and_close_fd_destination( j_compress_ptr const p_cinfo )
- {
- term_fd_destination( p_cinfo );
- BOOST_VERIFY( /*std*/::close( reinterpret_cast<long>( get_writer( p_cinfo ).compressor().client_data ) ) == 0 );
- }
-
-private:
- jpeg_destination_mgr destination_manager_;
- array<unsigned char, 65536> write_buffer_ ;
-};
 //------------------------------------------------------------------------------
 } // namespace detail
-
-
-class libjpeg_image;
-
-template <>
-struct backend_traits<libjpeg_image>
-{
- typedef ::J_COLOR_SPACE format_t;
- typedef detail::libjpeg_supported_pixel_formats supported_pixel_formats_t;
- typedef detail::libjpeg_roi roi_t;
- typedef detail::view_data_t view_data_t;
-
- struct gil_to_native_format
- {
- template <typename Pixel, bool IsPlanar>
- struct apply : detail::gil_to_libjpeg_format<Pixel, IsPlanar> {};
- };
-
- template <typename Pixel, bool IsPlanar>
- struct is_supported : detail::libjpeg_is_supported<Pixel, IsPlanar> {};
-
- typedef mpl::map3
- <
- mpl::pair<memory_range_t , detail::seekable_input_memory_range_extender<libjpeg_image> >,
- mpl::pair<FILE , libjpeg_image >,
- mpl::pair<char const *, detail::input_c_str_for_mmap_extender <libjpeg_image> >
- > native_sources;
-
- typedef mpl::map2
- <
- mpl::pair<FILE , detail::libjpeg_writer>,
- mpl::pair<char const *, detail::libjpeg_writer>
- > native_sinks;
-
- typedef mpl::vector1_c<format_tag, jpeg> supported_image_formats;
-
- typedef view_data_t writer_view_data_t;
-
- BOOST_STATIC_CONSTANT( unsigned int, desired_alignment = sizeof( void * ) );
- BOOST_STATIC_CONSTANT( bool , builtin_conversion = true );
-};
+//------------------------------------------------------------------------------
 
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -507,15 +95,11 @@
 ///
 ////////////////////////////////////////////////////////////////////////////////
 
-class libjpeg_image
+class libjpeg_reader
     :
- public detail::libjpeg_base,
- public detail::backend<libjpeg_image>
+ public libjpeg_image
 {
 public:
- struct guard {};
-
-public:
     format_t format() const { return decompressor().jpeg_color_space; }
 
     format_t closest_gil_supported_format() const
@@ -550,19 +134,7 @@
         return image_format_id( closest_gil_supported_format() );
     }
 
- static image_type_id image_format_id( format_t const closest_gil_supported_format )
- {
- switch ( closest_gil_supported_format )
- {
- case JCS_RGB : return 0;
- case JCS_GRAYSCALE: return 1;
- case JCS_CMYK : return 2;
- default:
- BOOST_ASSERT( closest_gil_supported_format == JCS_UNKNOWN );
- return unsupported_format;
- }
- }
-
+
     point2<std::ptrdiff_t> dimensions() const
     {
         // Implementation note:
@@ -587,7 +159,8 @@
     jpeg_decompress_struct const & lib_object() const { return decompressor(); }
 
 public: /// \ingroup Construction
- explicit libjpeg_image( memory_range_t & memory_chunk )
+ template <class Device>
+ explicit libjpeg_image( Device & device )
         :
         libjpeg_base( for_decompressor() )
     {
@@ -596,24 +169,11 @@
                 libjpeg_base::throw_jpeg_error();
         #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
 
- setup_source( memory_chunk );
+ setup_source( device );
 
         read_header();
     }
 
- explicit libjpeg_image( FILE & file )
- :
- libjpeg_base( for_decompressor() )
- {
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- setup_source( file );
-
- read_header();
- }
 
 private: // Private interface for the base backend<> class.
     // Implementation note:
@@ -1025,7 +585,7 @@
     array<JOCTET, 4096> read_buffer_ ;//...zzz...extract to a wrapper...not needed for in memory sources...
 };
 
-#if defined(BOOST_MSVC)
+#if defined( BOOST_MSVC )
 # pragma warning( pop )
 #endif
 
@@ -1088,8 +648,8 @@
 } // namespace boost
 //------------------------------------------------------------------------------
 
-#if defined(BOOST_MSVC)
+#if defined( BOOST_MSVC )
     #pragma warning( pop )
 #endif // MSVC
 
-#endif // libjpeg_image_hpp
+#endif // reader_hpp

Copied and modified: sandbox/gil/boost/gil/extension/io2/backends/libjpeg/writer.hpp (from r85344, sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp)
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp Tue Aug 13 18:43:39 2013 (r85344, copy source)
+++ sandbox/gil/boost/gil/extension/io2/backends/libjpeg/writer.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85502)
@@ -3,9 +3,10 @@
 /// \file libjpeg_image.hpp
 /// -----------------------
 ///
-/// Copyright (c) Domagoj Saric 2010.
+/// Copyright (c) Domagoj Saric 2010.-2013.
 ///
-/// Use, modification and distribution is subject to the Boost Software License, Version 1.0.
+/// Use, modification and distribution is subject to the
+/// Boost Software License, Version 1.0.
 /// (See accompanying file LICENSE_1_0.txt or copy at
 /// http://www.boost.org/LICENSE_1_0.txt)
 ///
@@ -13,34 +14,19 @@
 ///
 ////////////////////////////////////////////////////////////////////////////////
 //------------------------------------------------------------------------------
+#ifndef writer_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
+#define writer_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
 #pragma once
-#ifndef libjpeg_image_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
-#define libjpeg_image_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
 //------------------------------------------------------------------------------
 #include "backend.hpp"
-#include "detail/io_error.hpp"
-#include "detail/libx_shared.hpp"
-#include "detail/shared.hpp"
+#include "../detail/writer.hpp"
 
-#include <boost/array.hpp>
-#include <boost/range/size.hpp>
-#include <boost/smart_ptr/scoped_array.hpp>
+#include "boost/gil/extension/io2/detail/io_error.hpp"
+#include "boost/gil/extension/io2/detail/libx_shared.hpp"
+#include "boost/gil/extension/io2/detail/platform_specifics.hpp"
+#include "boost/gil/extension/io2/detail/shared.hpp"
 
-#define JPEG_INTERNALS
-#include "jpeglib.h"
-#undef JPEG_INTERNALS
-
-#ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- #include <csetjmp>
-#endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-#include <cstdlib>
-#if defined(BOOST_MSVC)
- #pragma warning( push )
- #pragma warning( disable : 4996 ) // "The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name."
- #include "io.h"
- #include "sys/stat.h"
-#endif // MSVC
-#include "fcntl.h"
+#include <boost/array.hpp>
 //------------------------------------------------------------------------------
 namespace boost
 {
@@ -52,199 +38,10 @@
 {
 //------------------------------------------------------------------------------
 
-BOOST_STATIC_ASSERT( BITS_IN_JSAMPLE == 8 );
-
-template <typename Pixel, bool isPlanar>
-struct gil_to_libjpeg_format : mpl::integral_c<J_COLOR_SPACE, JCS_UNKNOWN> {};
-
-template <> struct gil_to_libjpeg_format<rgb8_pixel_t , false> : mpl::integral_c<J_COLOR_SPACE, JCS_RGB > {};
-template <> struct gil_to_libjpeg_format<gray8_pixel_t, false> : mpl::integral_c<J_COLOR_SPACE, JCS_GRAYSCALE> {};
-template <> struct gil_to_libjpeg_format<cmyk8_pixel_t, false> : mpl::integral_c<J_COLOR_SPACE, JCS_CMYK > {};
-
-
-template <typename Pixel, bool IsPlanar>
-struct libjpeg_is_supported : mpl::bool_<gil_to_libjpeg_format<Pixel, IsPlanar>::value != JCS_UNKNOWN> {};
-
-
-typedef mpl::vector3
-<
- image<rgb8_pixel_t , false>,
- image<gray8_pixel_t, false>,
- image<cmyk8_pixel_t, false>
-> libjpeg_supported_pixel_formats;
-
-
-typedef generic_vertical_roi libjpeg_roi;
-
-
-struct libjpeg_object_wrapper_t
-{
- union aux_union
- {
- jpeg_compress_struct compressor_;
- jpeg_decompress_struct decompressor_;
- } libjpeg_object;
-};
-
-
-#if defined( BOOST_MSVC )
-# pragma warning( push )
-# pragma warning( disable : 4127 ) // "conditional expression is constant"
-#endif
-
-class libjpeg_base : private libjpeg_object_wrapper_t
-{
-protected:
- struct for_decompressor {};
- struct for_compressor {};
-
-protected:
- libjpeg_base( for_decompressor ) BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- initialize_error_handler();
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jpeg_create_decompress( &decompressor() );
- }
-
- libjpeg_base( for_compressor ) BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- initialize_error_handler();
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jpeg_create_compress( &compressor() );
- }
-
- ~libjpeg_base() { jpeg_destroy( &common() ); }
-
- void abort() const { jpeg_abort( &mutable_this().common() ); }
-
- jpeg_common_struct & common () { return *gil_reinterpret_cast<j_common_ptr>( &libjpeg_object.compressor_ ); }
- jpeg_common_struct const & common () const { return const_cast<libjpeg_base &>( *this ).common (); }
- jpeg_compress_struct & compressor () { return libjpeg_object.compressor_ ; }
- jpeg_compress_struct const & compressor () const { return const_cast<libjpeg_base &>( *this ).compressor (); }
- jpeg_decompress_struct & decompressor() { return libjpeg_object.decompressor_; }
- jpeg_decompress_struct const & decompressor() const { return const_cast<libjpeg_base &>( *this ).decompressor(); }
-
- libjpeg_base & mutable_this() const { return const_cast<libjpeg_base &>( *this ); }
-
- static libjpeg_base & base( jpeg_common_struct * const p_libjpeg_object )
- {
- BOOST_ASSERT( p_libjpeg_object );
- return *static_cast<libjpeg_base *>( gil_reinterpret_cast<libjpeg_object_wrapper_t *>( p_libjpeg_object ) );
- }
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jmp_buf & error_handler_target() const { return longjmp_target_; }
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- static void fatal_error_handler( j_common_ptr const p_cinfo )
- {
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- throw_jpeg_error();
- ignore_unused_variable_warning( p_cinfo );
- #else
- longjmp( base( p_cinfo ).error_handler_target(), 1 );
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- }
-
- static void throw_jpeg_error()
- {
- io_error( "LibJPEG error" );
- }
-
-private:
- void initialize_error_handler()
- {
- #ifndef NDEBUG
- jpeg_std_error( &jerr_ );
- #else
- std::memset( &jerr_, 0, sizeof( jerr_ ) );
- jerr_.emit_message = &emit_message ;
- jerr_.output_message = &output_message ;
- jerr_.format_message = &format_message ;
- jerr_.reset_error_mgr = &reset_error_mgr;
- #endif // NDEBUG
- jerr_.error_exit = &error_exit;
-
- common().err = &jerr_;
- }
-
- static void BF_CDECL error_exit( j_common_ptr const p_cinfo )
- {
- #ifndef NDEBUG
- p_cinfo->err->output_message( p_cinfo );
- #endif
-
- fatal_error_handler( p_cinfo );
- }
-
- static void BF_CDECL output_message( j_common_ptr /*p_cinfo*/ ) {}
- static void BF_CDECL emit_message ( j_common_ptr /*p_cinfo*/, int /*msg_level*/ ) {}
- static void BF_CDECL format_message( j_common_ptr /*p_cinfo*/, char * /*buffer*/ ) {}
-
- static void BF_CDECL reset_error_mgr( j_common_ptr const p_cinfo )
- {
- BOOST_ASSERT( p_cinfo->err->num_warnings == 0 );
- BOOST_ASSERT( p_cinfo->err->msg_code == 0 );
- ignore_unused_variable_warning( p_cinfo );
- }
-
-private:
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- mutable jmp_buf longjmp_target_;
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- jpeg_error_mgr jerr_ ;
-};
-
-
-struct decompression_setup_data_t
-{
- decompression_setup_data_t( J_COLOR_SPACE const format, JSAMPROW const buffer, libjpeg_roi::offset_t const offset )
- : format_( format ), buffer_( buffer ), offset_( offset ) {}
-
- J_COLOR_SPACE /*const*/ format_;
- JSAMPROW /*const*/ buffer_;
- libjpeg_roi::offset_t /*const*/ offset_;
-};
-
-struct view_data_t : decompression_setup_data_t
-{
- template <class View>
- /*explicit*/ view_data_t( View const & view, libjpeg_roi::offset_t const offset = 0 )
- :
- decompression_setup_data_t
- (
- gil_to_libjpeg_format<typename View::value_type, is_planar<View>::value>::value,
- backend_base::get_raw_data( view ),
- offset
- ),
- height_( view.height() ),
- width_ ( view.width () ),
- stride_( view.pixels().row_size() ),
- number_of_channels_( num_channels<View>::value )
- {
- BOOST_STATIC_ASSERT(( libjpeg_is_supported<typename View::value_type, is_planar<View>::value>::value ));
- }
-
- void set_format( J_COLOR_SPACE const format ) { format_ = format; }
-
- unsigned int /*const*/ height_;
- unsigned int /*const*/ width_ ;
- unsigned int /*const*/ stride_;
- unsigned int number_of_channels_;
-};
-
-
 class libjpeg_writer
     :
- private libjpeg_base,
- public configure_on_write_writer
+ public libjpeg_image,
+ public configure_on_write_writer
 {
 protected:
     BOOST_STATIC_CONSTANT( bool, auto_closes_device = true );
@@ -460,636 +257,14 @@
 } // namespace detail
 
 
-class libjpeg_image;
-
-template <>
-struct backend_traits<libjpeg_image>
-{
- typedef ::J_COLOR_SPACE format_t;
- typedef detail::libjpeg_supported_pixel_formats supported_pixel_formats_t;
- typedef detail::libjpeg_roi roi_t;
- typedef detail::view_data_t view_data_t;
-
- struct gil_to_native_format
- {
- template <typename Pixel, bool IsPlanar>
- struct apply : detail::gil_to_libjpeg_format<Pixel, IsPlanar> {};
- };
-
- template <typename Pixel, bool IsPlanar>
- struct is_supported : detail::libjpeg_is_supported<Pixel, IsPlanar> {};
-
- typedef mpl::map3
- <
- mpl::pair<memory_range_t , detail::seekable_input_memory_range_extender<libjpeg_image> >,
- mpl::pair<FILE , libjpeg_image >,
- mpl::pair<char const *, detail::input_c_str_for_mmap_extender <libjpeg_image> >
- > native_sources;
-
- typedef mpl::map2
- <
- mpl::pair<FILE , detail::libjpeg_writer>,
- mpl::pair<char const *, detail::libjpeg_writer>
- > native_sinks;
-
- typedef mpl::vector1_c<format_tag, jpeg> supported_image_formats;
-
- typedef view_data_t writer_view_data_t;
-
- BOOST_STATIC_CONSTANT( unsigned int, desired_alignment = sizeof( void * ) );
- BOOST_STATIC_CONSTANT( bool , builtin_conversion = true );
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libjpeg_image
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libjpeg_image
- :
- public detail::libjpeg_base,
- public detail::backend<libjpeg_image>
-{
-public:
- struct guard {};
-
-public:
- format_t format() const { return decompressor().jpeg_color_space; }
-
- format_t closest_gil_supported_format() const
- {
- format_t const current_format( format() );
- #ifdef _DEBUG
- switch ( current_format )
- {
- case JCS_RGB :
- case JCS_YCbCr :
- case JCS_CMYK :
- case JCS_YCCK :
- case JCS_GRAYSCALE:
- case JCS_UNKNOWN :
- break;
-
- default:
- BOOST_ASSERT( !"Unknown format code." );
- }
- #endif
-
- switch ( current_format )
- {
- case JCS_YCbCr: return JCS_RGB ;
- case JCS_YCCK : return JCS_CMYK ;
- default : return current_format;
- }
- }
-
- image_type_id current_image_format_id() const
- {
- return image_format_id( closest_gil_supported_format() );
- }
-
- static image_type_id image_format_id( format_t const closest_gil_supported_format )
- {
- switch ( closest_gil_supported_format )
- {
- case JCS_RGB : return 0;
- case JCS_GRAYSCALE: return 1;
- case JCS_CMYK : return 2;
- default:
- BOOST_ASSERT( closest_gil_supported_format == JCS_UNKNOWN );
- return unsupported_format;
- }
- }
-
- point2<std::ptrdiff_t> dimensions() const
- {
- // Implementation note:
- // A user might have setup output image scaling through the low-level
- // lib_object accessor.
- // (17.10.2010.) (Domagoj Saric)
- if ( dirty_output_dimensions_ )
- {
- jpeg_calc_output_dimensions( &const_cast<libjpeg_image &>( *this ).lib_object() );
- dirty_output_dimensions_ = false;
- }
- return point2<std::ptrdiff_t>( decompressor().output_width, decompressor().output_height );
- }
-
-public: // Low-level (row, strip, tile) access
- void read_row( sequential_row_read_state, unsigned char * const p_row_storage ) const
- {
- read_scanline( p_row_storage );
- }
-
- jpeg_decompress_struct & lib_object() { dirty_output_dimensions_ = true; return decompressor(); }
- jpeg_decompress_struct const & lib_object() const { return decompressor(); }
-
-public: /// \ingroup Construction
- explicit libjpeg_image( memory_range_t & memory_chunk )
- :
- libjpeg_base( for_decompressor() )
- {
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- setup_source( memory_chunk );
-
- read_header();
- }
-
- explicit libjpeg_image( FILE & file )
- :
- libjpeg_base( for_decompressor() )
- {
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- setup_source( file );
-
- read_header();
- }
-
-private: // Private interface for the base backend<> class.
- // Implementation note:
- // MSVC 10 accepts friend base_t and friend class base_t, Clang 2.8
- // accepts friend class base_t, Apple Clang 1.6 and GCC (4.2 and 4.6) accept
- // neither.
- // (13.01.2011.) (Domagoj Saric)
- friend class detail::backend<libjpeg_image>;
-
- void raw_convert_to_prepared_view( detail::view_data_t const & view_data ) const BOOST_GIL_CAN_THROW
- {
- setup_decompression( view_data );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- JSAMPROW scanlines[ 4 ] =
- {
- view_data.buffer_,
- scanlines[ 0 ] + view_data.stride_,
- scanlines[ 1 ] + view_data.stride_,
- scanlines[ 2 ] + view_data.stride_
- };
-
- BOOST_ASSERT( boost::size( scanlines ) >= decompressor().rec_outbuf_height );
- unsigned int scanlines_to_read( view_data.height_ );
- for ( ; ; )
- {
- BOOST_ASSERT( scanlines_to_read <= ( decompressor().output_height - decompressor().output_scanline ) );
- unsigned int const lines_read
- (
- read_scanlines
- (
- scanlines,
- std::min<std::size_t>( boost::size( scanlines ), scanlines_to_read )
- )
- );
- scanlines_to_read -= lines_read;
- if ( !scanlines_to_read )
- return;
-
- scanlines[ 0 ] = scanlines[ lines_read - 1 ] + view_data.stride_;
- scanlines[ 1 ] = scanlines[ 0 ] + view_data.stride_;
- scanlines[ 2 ] = scanlines[ 1 ] + view_data.stride_;
- scanlines[ 3 ] = scanlines[ 2 ] + view_data.stride_;
- }
- }
-
-
- template <class MyView, class TargetView, class Converter>
- void generic_convert_to_prepared_view( TargetView const & view, Converter const & converter ) const
- {
- using namespace detail;
-
- typedef typename MyView::value_type pixel_t;
- std::size_t const scanline_length ( decompressor().image_width * decompressor().num_components );
- scoped_array<JSAMPLE> const p_scanline_buffer( new JSAMPLE[ scanline_length ] );
- JSAMPROW scanline ( p_scanline_buffer.get() );
- JSAMPROW const scanline_end( scanline + scanline_length );
-
- format_t const my_format( gil_to_libjpeg_format<typename MyView::value_type, is_planar<MyView>::value>::value );
- BOOST_ASSERT( this->closest_gil_supported_format() == my_format );
- setup_decompression
- (
- decompression_setup_data_t
- (
- my_format,
- scanline,
- detail::get_offset<offset_t>( view )
- )
- );
-
- BOOST_ASSERT( decompressor().output_width == decompressor().image_width );
- BOOST_ASSERT( decompressor().output_components == decompressor().num_components );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- unsigned int const scanlines_to_read( detail::original_view( view ).dimensions().y );
- for ( unsigned int scanline_index( 0 ); scanline_index < scanlines_to_read; ++scanline_index )
- {
- read_scanline( scanline );
-
- typedef typename get_original_view_t<TargetView>::type::x_iterator target_x_iterator;
-
- pixel_t const * p_source_pixel( gil_reinterpret_cast_c<pixel_t const *>( scanline ) );
- target_x_iterator p_target_pixel( original_view( view ).row_begin( scanline_index ) );
- while ( p_source_pixel < gil_reinterpret_cast_c<pixel_t const *>( scanline_end ) )
- {
- converter( *p_source_pixel, *p_target_pixel );
- ++p_source_pixel;
- ++p_target_pixel;
- }
- }
- }
-
-
- void raw_copy_to_prepared_view( detail::view_data_t const & view_data ) const
- {
- BOOST_ASSERT( view_data.width_ == static_cast<unsigned int>( dimensions().x ) );
- BOOST_ASSERT( view_data.height_ == static_cast<unsigned int>( dimensions().y ) );
- BOOST_ASSERT( view_data.format_ == closest_gil_supported_format() );
- raw_convert_to_prepared_view( view_data );
- }
-
-
- static unsigned int cached_format_size( format_t const format )
- {
- switch ( format )
- {
- case JCS_RGB:
- case JCS_YCbCr:
- return 3;
- case JCS_CMYK:
- case JCS_YCCK:
- return 4;
- case JCS_GRAYSCALE:
- return 1;
-
- default:
- BOOST_ASSERT( !"Invalid or unknown format specified." );
- BF_UNREACHABLE_CODE
- return 0;
- }
- }
-
-private:
- void setup_decompression( detail::decompression_setup_data_t const & view_data ) const BOOST_GIL_CAN_THROW
- {
- unsigned int const state ( decompressor().global_state );
- unsigned int rows_to_skip( view_data.offset_ );
- if
- (
- ( state != DSTATE_SCANNING ) ||
- ( decompressor().out_color_space != view_data.format_ ) ||
- ( decompressor().output_scanline > static_cast<JDIMENSION>( view_data.offset_ ) )
- )
- {
- BOOST_ASSERT
- (
- ( state == DSTATE_READY ) ||
- ( state == DSTATE_SCANNING )
- );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- if ( state == DSTATE_SCANNING )
- abort();
-
- mutable_this().decompressor().out_color_space = view_data.format_;
- BOOST_VERIFY( jpeg_start_decompress( &mutable_this().decompressor() ) );
- BOOST_ASSERT( decompressor().output_scanline == 0 );
- }
- else
- rows_to_skip -= decompressor().output_scanline;
-
- if ( rows_to_skip )
- skip_rows( rows_to_skip, view_data.buffer_ );
- BOOST_ASSERT( decompressor().output_scanline == static_cast<JDIMENSION>( view_data.offset_ ) );
- }
-
- void skip_rows( unsigned int const number_of_rows_to_skip, JSAMPROW /*const*/ dummy_scanline_buffer ) const
- {
- BOOST_ASSERT( decompressor().raw_data_out == false );
- BOOST_ASSERT( decompressor().global_state == DSTATE_SCANNING );
- mutable_this().decompressor().raw_data_out = true;
- mutable_this().decompressor().global_state = DSTATE_RAW_OK;
-
- unsigned int const max_number_of_components( 4 ); // CMYK
- unsigned int const max_sampling_factor ( 2 ); // Documentation
- unsigned int const mcu_row_size ( max_sampling_factor * DCTSIZE ); // Documentation
- BOOST_ASSERT( decompressor().num_components <= max_number_of_components );
- BOOST_ASSERT( decompressor().max_v_samp_factor <= max_sampling_factor );
-
- JSAMPROW dummy_component_2d_array[ mcu_row_size ];
- JSAMPARRAY dummy_scan_lines [ max_number_of_components ];
-
- std::fill( begin( dummy_component_2d_array ), end( dummy_component_2d_array ), dummy_scanline_buffer );
- std::fill( begin( dummy_scan_lines ), end( dummy_scan_lines ), &dummy_component_2d_array[ 0 ] );
-
- unsigned int number_of_rows_to_skip_using_scanlines
- (
- number_of_rows_to_skip % mcu_row_size
- );
- unsigned int number_of_rows_to_skip_using_raw
- (
- number_of_rows_to_skip - number_of_rows_to_skip_using_scanlines
- );
-
- while ( number_of_rows_to_skip_using_raw )
- {
- unsigned int const lines_to_read( boost::size( dummy_component_2d_array ) );
- read_raw_data( dummy_scan_lines, lines_to_read );
- number_of_rows_to_skip_using_raw -= lines_to_read;
- }
-
- mutable_this().decompressor().raw_data_out = false;
- mutable_this().decompressor().global_state = DSTATE_SCANNING;
- while ( number_of_rows_to_skip_using_scanlines-- )
- {
- read_scanline( dummy_scanline_buffer );
- }
- }
-
- unsigned int read_scanlines( JSAMPROW scanlines[], unsigned int const scanlines_to_read ) const BOOST_GIL_CAN_THROW
- {
- return jpeg_read_scanlines
- (
- &mutable_this().decompressor(),
- scanlines,
- scanlines_to_read
- );
- }
-
- void read_scanline( JSAMPROW scanline ) const BOOST_GIL_CAN_THROW
- {
- BOOST_VERIFY
- (
- read_scanlines( &scanline, 1 ) == 1
- );
- }
-
- void read_raw_data( JSAMPARRAY scanlines[], unsigned int const scanlines_to_read ) const BOOST_GIL_CAN_THROW
- {
- BOOST_VERIFY
- (
- jpeg_read_raw_data
- (
- &mutable_this().decompressor(),
- scanlines,
- scanlines_to_read
- ) == scanlines_to_read
- );
- }
-
- libjpeg_image & mutable_this() const { return static_cast<libjpeg_image &>( libjpeg_base::mutable_this() ); }
-
-private:
- friend class libjpeg_view_base;
-
- void read_header()
- {
- BOOST_VERIFY( jpeg_read_header( &decompressor(), true ) == JPEG_HEADER_OK );
-
- // Implementation note:
- // To enable users to setup output scaling we use the output
- // dimensions to report the image dimensions in the dimensions() getter
- // so we have to manually initialize them here.
- // (17.10.2010.) (Domagoj Saric)
- BOOST_ASSERT( decompressor().output_width == 0 );
- BOOST_ASSERT( decompressor().output_height == 0 );
-
- decompressor().output_width = decompressor().image_width ;
- decompressor().output_height = decompressor().image_height;
-
- dirty_output_dimensions_ = false;
-
- detail::io_error_if( decompressor().data_precision != BITS_IN_JSAMPLE, "Unsupported image file data precision." );
- }
-
- // Unextracted "libjpeg_reader" interface.
- void setup_source()
- {
- BOOST_ASSERT( decompressor().src == NULL );
- decompressor().src = &source_manager_;
- }
-
- void setup_source( memory_range_t & memory_chunk )
- {
- setup_source();
-
- decompressor().client_data = &memory_chunk;
-
- source_manager_.next_input_byte = memory_chunk.begin();
- source_manager_.bytes_in_buffer = memory_chunk.size ();
-
- source_manager_.init_source = &init_memory_chunk_source;
- source_manager_.fill_input_buffer = &fill_memory_chunk_buffer;
- source_manager_.skip_input_data = &skip_memory_chunk_data ;
- source_manager_.resync_to_restart = &jpeg_resync_to_restart ;
- source_manager_.term_source = &term_memory_chunk_source;
- }
-
- void setup_source( FILE & file )
- {
- setup_source();
-
- decompressor().client_data = &file;
-
- source_manager_.next_input_byte = read_buffer_.begin();
- source_manager_.bytes_in_buffer = 0;
-
- source_manager_.init_source = &init_FILE_source ;
- source_manager_.fill_input_buffer = &fill_FILE_buffer ;
- source_manager_.skip_input_data = &skip_FILE_data ;
- source_manager_.resync_to_restart = &jpeg_resync_to_restart;
- source_manager_.term_source = &term_FILE_source ;
- }
-
-
- static void BF_CDECL init_FILE_source( j_decompress_ptr const p_cinfo )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- reader.source_manager_.next_input_byte = reader.read_buffer_.begin();
- reader.source_manager_.bytes_in_buffer = 0;
- }
-
- static boolean BF_CDECL fill_FILE_buffer( j_decompress_ptr const p_cinfo )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- std::size_t bytes_read
- (
- /*std*/::fread
- (
- reader.read_buffer_.begin(),
- 1,
- reader.read_buffer_.size(),
- static_cast<FILE *>( reader.decompressor().client_data )
- )
- );
-
- if ( bytes_read == 0 )
- {
- // Insert a fake EOI marker (see the comment for the default library
- // implementation).
- reader.read_buffer_[ 0 ] = 0xFF;
- reader.read_buffer_[ 1 ] = JPEG_EOI;
-
- bytes_read = 2;
- }
-
- reader.source_manager_.next_input_byte = reader.read_buffer_.begin();
- reader.source_manager_.bytes_in_buffer = bytes_read;
-
- return true;
- }
-
- static void BF_CDECL skip_FILE_data( j_decompress_ptr const p_cinfo, long num_bytes )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- if ( static_cast<std::size_t>( num_bytes ) <= reader.source_manager_.bytes_in_buffer )
- {
- reader.source_manager_.next_input_byte += num_bytes;
- reader.source_manager_.bytes_in_buffer -= num_bytes;
- }
- else
- {
- num_bytes -= reader.source_manager_.bytes_in_buffer;
- /*std*/::fseek( static_cast<FILE *>( reader.decompressor().client_data ), num_bytes, SEEK_CUR ); //...failure?
- reader.source_manager_.next_input_byte = 0;
- reader.source_manager_.bytes_in_buffer = 0;
- }
- }
-
- static void BF_CDECL term_FILE_source( j_decompress_ptr /*p_cinfo*/ )
- {
- }
-
- // Ensure that jpeg_finish_decompress() is called so that this gets called...
- static void BF_CDECL term_and_close_FILE_source( j_decompress_ptr const p_cinfo )
- {
- term_FILE_source( p_cinfo );
- BOOST_VERIFY( /*std*/::fclose( static_cast<FILE *>( get_reader( p_cinfo ).decompressor().client_data ) ) == 0 );
- }
-
- static void BF_CDECL init_memory_chunk_source( j_decompress_ptr /*p_cinfo*/ )
- {
- }
-
- static boolean BF_CDECL fill_memory_chunk_buffer( j_decompress_ptr /*p_cinfo*/ )
- {
- BF_UNREACHABLE_CODE
- return true;
- }
-
- static void BF_CDECL skip_memory_chunk_data( j_decompress_ptr const p_cinfo, long num_bytes )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- BOOST_ASSERT( static_cast<std::size_t>( num_bytes ) <= reader.source_manager_.bytes_in_buffer );
- reader.source_manager_.next_input_byte += num_bytes;
- reader.source_manager_.bytes_in_buffer -= num_bytes;
- }
-
- static void BF_CDECL term_memory_chunk_source( j_decompress_ptr /*p_cinfo*/ )
- {
- }
-
- static libjpeg_image & get_reader( j_decompress_ptr const p_cinfo )
- {
- libjpeg_image & reader( static_cast<libjpeg_image &>( base( gil_reinterpret_cast<j_common_ptr>( p_cinfo ) ) ) );
- BOOST_ASSERT( p_cinfo->src == &reader.source_manager_ );
- return reader;
- }
-
-private:
- jpeg_source_mgr source_manager_ ;
- mutable bool dirty_output_dimensions_;
- array<JOCTET, 4096> read_buffer_ ;//...zzz...extract to a wrapper...not needed for in memory sources...
-};
-
-#if defined(BOOST_MSVC)
-# pragma warning( pop )
-#endif
-
-
-//...zzz...incomplete...
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libjpeg_view_base
-///
-////////////////////////////////////////////////////////////////////////////////
-
-//class libjpeg_view_base : noncopyable
-//{
-//public:
-// ~libjpeg_view_base()
-// {
-// }
-//
-//protected:
-// libjpeg_view_base( libjpeg_image & bitmap, unsigned int const lock_mode, libjpeg_image::roi const * const p_roi = 0 )
-// {
-// }
-//
-// template <typename Pixel>
-// typename type_from_x_iterator<Pixel *>::view_t
-// get_typed_view()
-// {
-// //todo assert correct type...
-// interleaved_view<Pixel *>( bitmapData_.Width, bitmapData_.Height, bitmapData_.Scan0, bitmapData_.Stride );
-// }
-//
-//private:
-//};
-
-
-//...zzz...incomplete...
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libjpeg_view
-///
-////////////////////////////////////////////////////////////////////////////////
-
-//template <typename Pixel>
-//class libjpeg_view
-// :
-// private libjpeg_view_base,
-// public type_from_x_iterator<Pixel *>::view_t
-//{
-//public:
-// libjpeg_view( libjpeg_image & image, libjpeg_image::roi const * const p_roi = 0 )
-// :
-// libjpeg_view_base( image, ImageLockModeRead | ( is_const<Pixel>::value * ImageLockModeWrite ), p_roi ),
-// type_from_x_iterator<Pixel *>::view_t( get_typed_view<Pixel>() )
-// {}
-//};
-
 //------------------------------------------------------------------------------
 } // namespace gil
 //------------------------------------------------------------------------------
 } // namespace boost
 //------------------------------------------------------------------------------
 
-#if defined(BOOST_MSVC)
+#if defined( BOOST_MSVC )
     #pragma warning( pop )
 #endif // MSVC
 
-#endif // libjpeg_image_hpp
+#endif // writer_hpp

Copied and modified: sandbox/gil/boost/gil/extension/io2/backends/libpng/backend.hpp (from r85344, sandbox/gil/boost/gil/extension/io2/libpng_image.hpp)
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/libpng_image.hpp Tue Aug 13 18:43:39 2013 (r85344, copy source)
+++ sandbox/gil/boost/gil/extension/io2/backends/libpng/backend.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85502)
@@ -1,11 +1,12 @@
 ////////////////////////////////////////////////////////////////////////////////
 ///
-/// \file libpng_image.hpp
-/// ----------------------
+/// \file libpng/backend.hpp
+/// ------------------------
 ///
-/// Copyright (c) Domagoj Saric 2010.
+/// Copyright (c) Domagoj Saric 2010.-2013.
 ///
-/// Use, modification and distribution is subject to the Boost Software License, Version 1.0.
+/// Use, modification and distribution is subject to the
+/// Boost Software License, Version 1.0.
 /// (See accompanying file LICENSE_1_0.txt or copy at
 /// http://www.boost.org/LICENSE_1_0.txt)
 ///
@@ -13,8 +14,8 @@
 ///
 ////////////////////////////////////////////////////////////////////////////////
 //------------------------------------------------------------------------------
-#ifndef libpng_image_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
-#define libpng_image_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
+#ifndef backend_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
+#define backend_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
 #pragma once
 //------------------------------------------------------------------------------
 #include "backend.hpp"
@@ -205,173 +206,7 @@
 private:
     png_struct * __restrict p_png_ ;
     png_info * __restrict p_info_;
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_base
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_base : public lib_object_t
-{
-public:
- typedef lib_object_t lib_object_t;
-
- lib_object_t & lib_object() { return *this; }
-
-protected:
- libpng_base( png_struct * const p_png )
- :
- lib_object_t( p_png, ::png_create_info_struct( p_png ) )
- {}
-
- //...zzz...forced by LibPNG into either duplication or this anti-pattern...
- bool successful_creation() const { return is_valid(); }
-
- static std::size_t format_bit_depth( libpng_view_data_t::format_t const format )
- {
- return ( format >> 16 ) & 0xFF;
- }
-
- static std::size_t format_colour_type( libpng_view_data_t::format_t const format )
- {
- return format & 0xFF;
- }
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jmp_buf & error_handler_target() const { return png_object().jmpbuf; }
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_writer
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_writer
- :
- public libpng_base,
- public configure_on_write_writer
-{
-public:
- void write_default( libpng_view_data_t const & view )
- {
- ::png_set_IHDR
- (
- &png_object () ,
- &info_object() ,
- view.width_ ,
- view.height_ ,
- format_bit_depth ( view.format_ ),
- format_colour_type( view.format_ ),
- PNG_INTERLACE_NONE ,
- PNG_COMPRESSION_TYPE_DEFAULT ,
- PNG_FILTER_TYPE_DEFAULT
- );
-
- //::png_set_invert_alpha( &png_object() );
-
- write( view );
- }
-
- void write( libpng_view_data_t const & view ) BOOST_GIL_CAN_THROW
- {
- BOOST_ASSERT( view.format_ != JCS_UNKNOWN );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- detail::throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- if ( little_endian() )
- ::png_set_swap( &png_object() );
-
- ::png_write_info( &png_object(), &info_object() );
-
- png_byte * p_row( view.buffer_ );
- png_byte * const p_end( memunit_advanced( view.buffer_, view.height_ * view.stride_ ) );
- while ( p_row < p_end )
- {
- ::png_write_row( &png_object(), p_row );
- memunit_advance( p_row, view.stride_ );
- }
-
- ::png_write_end( &png_object(), 0 );
- }
-
-protected:
- libpng_writer( void * const p_target_object, png_rw_ptr const write_data_fn, png_flush_ptr const output_flush_fn )
- :
- libpng_base( ::png_create_write_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
- {
- if ( !successful_creation() )
- cleanup_and_throw_libpng_error();
-
- setup_destination( p_target_object, write_data_fn, output_flush_fn );
- }
-
- ~libpng_writer()
- {
- destroy_write_struct();
- }
-
-private:
- void destroy_write_struct() { ::png_destroy_write_struct( &png_object_for_destruction(), &info_object_for_destruction() ); }
-
- void cleanup_and_throw_libpng_error()
- {
- destroy_write_struct();
- detail::throw_libpng_error();
- }
-
- void setup_destination( void * const p_target_object, png_rw_ptr const write_data_fn, png_flush_ptr const output_flush_fn )
- {
- ::png_set_write_fn( &png_object(), p_target_object, write_data_fn, output_flush_fn );
- }
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_writer_FILE
-///
-/// \brief
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_writer_FILE : public libpng_writer
-{
-public:
- libpng_writer_FILE( FILE & file )
- :
- libpng_writer( &file, &png_write_data, &png_flush_data )
- {}
-
-private:
- static void PNGAPI png_write_data( png_structp const png_ptr, png_bytep const data, png_size_t const length )
- {
- BOOST_ASSERT( png_ptr );
-
- png_size_t const written_size
- (
- static_cast<png_size_t>( std::fwrite( data, 1, length, static_cast<FILE *>( png_ptr->io_ptr ) ) )
- );
-
- if ( written_size != length )
- png_error_function( png_ptr, "Write Error" );
- }
-
- static void PNGAPI png_flush_data( png_structp const png_ptr )
- {
- BOOST_ASSERT( png_ptr );
-
- std::fflush( static_cast<FILE *>( png_ptr->io_ptr ) );
- }
-};
+}; // class lib_object_t
 
 //------------------------------------------------------------------------------
 } // namespace detail
@@ -427,333 +262,60 @@
 ////////////////////////////////////////////////////////////////////////////////
 
 class libpng_image
- :
- public detail::backend<libpng_image>,
- private detail::libpng_base
+ :
+ public detail::lib_object_t,
+ public detail::backend<libpng_image>
 {
 public:
- struct guard {};
-
-public:
- format_t format() const
- {
- return ::png_get_color_type( &png_object(), &info_object() ) | ( bit_depth() << 16 );
- }
-
- format_t closest_gil_supported_format() const
- {
- format_t const current_format( format() );
-
- switch ( current_format & 0xFF )
- {
- default: return current_format;
-
- case PNG_COLOR_TYPE_PALETTE : return PNG_COLOR_TYPE_RGB | ( 8 << 16 ); // 8-bit RGB
- case PNG_COLOR_TYPE_GRAY_ALPHA: return PNG_COLOR_TYPE_RGBA | ( ( ( current_format >> 16 ) & 0xFF ) << 16 ); // (bits of current_format) RGBA
- }
- }
-
- image_type_id current_image_format_id() const
- {
- return image_format_id( closest_gil_supported_format() );
- }
-
- static image_type_id image_format_id( format_t const closest_gil_supported_format )
- {
- switch ( closest_gil_supported_format )
- {
- case PNG_COLOR_TYPE_RGB | ( 8 << 16 ): return 0;
- case PNG_COLOR_TYPE_RGB_ALPHA | ( 8 << 16 ): return 1;
- case PNG_COLOR_TYPE_GRAY | ( 8 << 16 ): return 2;
- case PNG_COLOR_TYPE_RGB | ( 16 << 16 ): return 3;
- case PNG_COLOR_TYPE_RGB_ALPHA | ( 16 << 16 ): return 4;
- case PNG_COLOR_TYPE_GRAY | ( 16 << 16 ): return 5;
-
- default:
- return unsupported_format;
- }
- }
-
- std::size_t pixel_size() const
- {
- return number_of_channels() * bit_depth() / 8;
- }
-
-public: /// \ingroup Construction
- explicit libpng_image( FILE & file )
- :
- libpng_base( ::png_create_read_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
- {
- if ( !successful_creation() )
- cleanup_and_throw_libpng_error();
-
- #ifdef PNG_NO_STDIO
- ::png_set_read_fn( &png_object(), &file, &png_FILE_read_data );
- #else
- ::png_init_io( &png_object(), &file );
- #endif
-
- init();
- }
-
- explicit libpng_image( memory_range_t & in_memory_image )
- :
- libpng_base( ::png_create_read_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
- {
- if ( !successful_creation() )
- cleanup_and_throw_libpng_error();
- //png_rw_ptr
- ::png_set_read_fn( &png_object(), &in_memory_image, &png_memory_chunk_read_data );
-
- init();
- }
-
- ~libpng_image()
- {
- destroy_read_struct();
- }
-
-public:
- point2<std::ptrdiff_t> dimensions() const
- {
- return point2<std::ptrdiff_t>
- (
- ::png_get_image_width ( &png_object(), &info_object() ),
- ::png_get_image_height( &png_object(), &info_object() )
- );
- }
-
-public: // Low-level (row, strip, tile) access
- void read_row( sequential_row_read_state, unsigned char * const p_row_storage ) const
- {
- read_row( p_row_storage );
- }
-
- png_struct & lib_object() const { return png_object(); }
-
-private: // Private backend_base interface.
- // Implementation note:
- // MSVC 10 accepts friend base_t and friend class base_t, Clang 2.8
- // accepts friend class base_t, Apple Clang 1.6 and GCC (4.2 and 4.6) accept
- // neither.
- // (13.01.2011.) (Domagoj Saric)
- friend class detail::backend<libpng_image>;
-
- template <class MyView, class TargetView, class Converter>
- void generic_convert_to_prepared_view( TargetView const & view, Converter const & converter ) const BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- using namespace detail;
-
- std::size_t const row_length ( ::png_get_rowbytes( &png_object(), &info_object() ) );
- scoped_array<png_byte> const p_row_buffer( new png_byte[ row_length ] );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- detail::throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- unsigned int number_of_passes( ::png_set_interlace_handling( &png_object() ) );
- BF_ASSUME( ( number_of_passes == 1 ) || ( number_of_passes == 7 ) );
- BOOST_ASSERT( ( number_of_passes == 1 ) && "Missing interlaced support for the generic conversion case." );
- ignore_unused_variable_warning( number_of_passes );
-
- if ( is_offset_view<TargetView>::value )
- skip_rows( get_offset<offset_t>( view ) );
-
- png_byte * const p_row ( p_row_buffer.get() );
- png_byte const * const p_row_end( p_row + row_length );
-
- unsigned int const rows_to_read( original_view( view ).dimensions().y );
- for ( unsigned int row_index( 0 ); row_index < rows_to_read; ++row_index )
- {
- read_row( p_row );
-
- typedef typename MyView::value_type pixel_t;
- typedef typename get_original_view_t<TargetView>::type::x_iterator x_iterator;
-
- pixel_t const * p_source_pixel( gil_reinterpret_cast_c<pixel_t const *>( p_row ) );
- x_iterator p_target_pixel( original_view( view ).row_begin( row_index ) );
- while ( p_source_pixel < gil_reinterpret_cast_c<pixel_t const *>( p_row_end ) )
- {
- converter( *p_source_pixel, *p_target_pixel );
- ++p_source_pixel;
- ++p_target_pixel;
- }
- }
- }
-
- void raw_convert_to_prepared_view( detail::libpng_view_data_t const & view_data ) const
- {
- BOOST_ASSERT( view_data.width_ == static_cast<unsigned int>( dimensions().x ) );
- BOOST_ASSERT( view_data.height_ == static_cast<unsigned int>( dimensions().y ) );
- BOOST_ASSERT( view_data.format_ == static_cast<unsigned int>( closest_gil_supported_format() ) );
-
- unsigned int const bit_depth ( ( view_data.format_ >> 16 ) & 0xFF );
- unsigned int const colour_type( view_data.format_ & 0xFF );
+ struct guard {};
 
- if ( colour_type == PNG_COLOR_TYPE_PALETTE )
- {
- ::png_set_palette_to_rgb( &png_object() );
- }
- else
- if ( ( colour_type == PNG_COLOR_TYPE_GRAY ) && ( bit_depth < 8 ) )
- {
- ::png_set_expand_gray_1_2_4_to_8( &png_object() );
- }
+ typedef lib_object_t lib_object_t;
 
- if ( !( colour_type & PNG_COLOR_MASK_ALPHA ) )
- ::png_set_strip_alpha( &png_object() );
+ lib_object_t & lib_object() { return *this; }
 
- if ( ::png_get_valid( &png_object(), &info_object(), PNG_INFO_tRNS ) )
- ::png_set_tRNS_to_alpha( &png_object() );
+ static image_type_id image_format_id( format_t const closest_gil_supported_format )
+ {
+ switch ( closest_gil_supported_format )
+ {
+ case PNG_COLOR_TYPE_RGB | ( 8 << 16 ) : return 0;
+ case PNG_COLOR_TYPE_RGB_ALPHA | ( 8 << 16 ) : return 1;
+ case PNG_COLOR_TYPE_GRAY | ( 8 << 16 ) : return 2;
+ case PNG_COLOR_TYPE_RGB | ( 16 << 16 ) : return 3;
+ case PNG_COLOR_TYPE_RGB_ALPHA | ( 16 << 16 ) : return 4;
+ case PNG_COLOR_TYPE_GRAY | ( 16 << 16 ) : return 5;
+
+ default:
+ return unsupported_format;
+ }
+ }
 
- if ( bit_depth == 8 )
- ::png_set_strip_16( &png_object() );
-
- //...zzz...
-
- //if (color_type == PNG_COLOR_TYPE_RGB ||
- // color_type == PNG_COLOR_TYPE_RGB_ALPHA)
- // png_set_bgr(png_ptr);
-
- //if (color_type == PNG_COLOR_TYPE_RGB ||
- // color_type == PNG_COLOR_TYPE_GRAY)
- // png_set_add_alpha(png_ptr, filler, PNG_FILLER_AFTER);
-
- //if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
- // png_set_swap_alpha(png_ptr);
-
- //if (color_type == PNG_COLOR_TYPE_GRAY ||
- // color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
- // png_set_gray_to_rgb(png_ptr);
-
- //if (color_type == PNG_COLOR_TYPE_RGB ||
- // color_type == PNG_COLOR_TYPE_RGB_ALPHA)
- // png_set_rgb_to_gray_fixed(png_ptr, error_action,
- // int red_weight, int green_weight);
-
- ::png_read_update_info( &png_object(), &info_object() );
-
- raw_copy_to_prepared_view( view_data );
- }
-
- void raw_copy_to_prepared_view( detail::libpng_view_data_t const view_data ) const BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- detail::throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- unsigned int number_of_passes( ::png_set_interlace_handling( &png_object() ) );
- BF_ASSUME( ( number_of_passes == 1 ) || ( number_of_passes == 7 ) );
-
- skip_rows( view_data.offset_ );
-
- while ( number_of_passes-- )
- {
- png_byte * p_row( view_data.buffer_ );
- png_byte const * const p_end( p_row + ( view_data.height_ * view_data.stride_ ) );
- while ( p_row < p_end )
- {
- read_row( p_row );
- memunit_advance( p_row, view_data.stride_ );
- }
- }
- }
-
- std::size_t cached_format_size( format_t const format ) const
- {
- return number_of_channels() * format_bit_depth( format ) / 8;
- }
-
-private:
- void cleanup_and_throw_libpng_error()
- {
- destroy_read_struct();
- detail::throw_libpng_error();
- }
-
- void init() BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- try
- {
- #else
- if ( setjmp( error_handler_target() ) )
- cleanup_and_throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- ::png_read_info( &png_object(), &info_object() );
- if ( little_endian() )
- ::png_set_swap( &png_object() );
-
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- }
- catch (...)
- {
- destroy_read_struct();
- throw;
- }
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- }
-
- void skip_rows( unsigned int const row_to_skip_to ) const
- {
- BOOST_ASSERT( ( row_to_skip_to >= png_object().row_number ) && "No 'rewind' capability for LibPNG yet." );
-
- unsigned int number_of_rows_to_skip( row_to_skip_to - png_object().row_number );
- while ( number_of_rows_to_skip-- )
- {
- BF_ASSUME( row_to_skip_to != 0 );
- read_row( NULL );
- }
- }
-
- unsigned int number_of_channels() const { return ::png_get_channels ( &png_object(), &info_object() ); }
- std::size_t bit_depth () const { return ::png_get_bit_depth( &png_object(), &info_object() ); }
-
- void destroy_read_struct() { ::png_destroy_read_struct( &png_object_for_destruction(), &info_object_for_destruction(), NULL ); }
-
- void read_row( png_byte * const p_row ) const BOOST_GIL_CAN_THROW
- {
- ::png_read_row( &png_object(), p_row, NULL );
- }
-
- static void PNGAPI png_FILE_read_data( png_structp const png_ptr, png_bytep const data, png_size_t const length )
- {
- BOOST_ASSERT( png_ptr );
- BOOST_ASSERT( png_ptr->io_ptr );
-
- png_size_t const read_size
- (
- static_cast<png_size_t>( std::fread( data, 1, length, static_cast<FILE *>( png_ptr->io_ptr ) ) )
- );
-
- if ( read_size != length )
- detail::png_error_function( png_ptr, "Read Error" );
- }
-
- static void PNGAPI png_memory_chunk_read_data( png_structp const png_ptr, png_bytep const data, png_size_t const length )
- {
- BOOST_ASSERT( png_ptr );
- BOOST_ASSERT( png_ptr->io_ptr );
-
- memory_range_t & memory_chunk( *static_cast<memory_range_t *>( png_ptr->io_ptr ) );
+protected:
+ libpng_image( png_struct * const p_png )
+ :
+ lib_object_t( p_png, ::png_create_info_struct( p_png ) )
+ {}
+
+ //...zzz...forced by LibPNG into either duplication or this anti-pattern...
+ bool successful_creation() const { return is_valid(); }
+
+ static std::size_t format_bit_depth( libpng_view_data_t::format_t const format )
+ {
+ return ( format >> 16 ) & 0xFF;
+ }
+
+ static std::size_t format_colour_type( libpng_view_data_t::format_t const format )
+ {
+ return format & 0xFF;
+ }
 
- if ( length <= static_cast<std::size_t>( memory_chunk.size() ) )
- {
- std::memcpy( data, memory_chunk.begin(), length );
- memory_chunk.advance_begin( length );
- }
- else
- detail::png_error_function( png_ptr, "Read Error" );
- }
+#ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
+ jmp_buf & error_handler_target() const { return png_object().jmpbuf; }
+#endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
 };
 
-
 //------------------------------------------------------------------------------
 } // namespace gil
 //------------------------------------------------------------------------------
 } // namespace boost
 //------------------------------------------------------------------------------
-#endif // libpng_image_hpp
+#endif // backend_hpp

Copied and modified: sandbox/gil/boost/gil/extension/io2/backends/libpng/reader.hpp (from r85344, sandbox/gil/boost/gil/extension/io2/libpng_image.hpp)
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/libpng_image.hpp Tue Aug 13 18:43:39 2013 (r85344, copy source)
+++ sandbox/gil/boost/gil/extension/io2/backends/libpng/reader.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85502)
@@ -1,11 +1,12 @@
 ////////////////////////////////////////////////////////////////////////////////
 ///
-/// \file libpng_image.hpp
-/// ----------------------
+/// \file libpng/reader.hpp
+/// ------------------------
 ///
-/// Copyright (c) Domagoj Saric 2010.
+/// Copyright (c) Domagoj Saric 2010.-2013.
 ///
-/// Use, modification and distribution is subject to the Boost Software License, Version 1.0.
+/// Use, modification and distribution is subject to the
+/// Boost Software License, Version 1.0.
 /// (See accompanying file LICENSE_1_0.txt or copy at
 /// http://www.boost.org/LICENSE_1_0.txt)
 ///
@@ -13,8 +14,8 @@
 ///
 ////////////////////////////////////////////////////////////////////////////////
 //------------------------------------------------------------------------------
-#ifndef libpng_image_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
-#define libpng_image_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
+#ifndef reader_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
+#define reader_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
 #pragma once
 //------------------------------------------------------------------------------
 #include "backend.hpp"
@@ -42,394 +43,21 @@
 {
 //------------------------------------------------------------------------------
 
-template <typename Pixel, bool isPlanar>
-struct gil_to_libpng_format : mpl::integral_c<unsigned int, backend_base::unsupported_format> {};
-
-/// \todo Add bgr-layout formats as LibPNG actually supports them through
-/// png_set_bgr().
-/// (19.09.2010.) (Domagoj Saric)
-template <> struct gil_to_libpng_format<rgb8_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB | ( 8 << 16 )> {};
-template <> struct gil_to_libpng_format<rgba8_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB_ALPHA | ( 8 << 16 )> {};
-template <> struct gil_to_libpng_format<gray8_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_GRAY | ( 8 << 16 )> {};
-template <> struct gil_to_libpng_format<rgb16_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB | ( 16 << 16 )> {};
-template <> struct gil_to_libpng_format<rgba16_pixel_t, false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB_ALPHA | ( 16 << 16 )> {};
-template <> struct gil_to_libpng_format<gray16_pixel_t, false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_GRAY | ( 16 << 16 )> {};
-
-
-template <typename Pixel, bool IsPlanar>
-struct libpng_is_supported : mpl::bool_<gil_to_libpng_format<Pixel, IsPlanar>::value != -1> {};
-
-template <typename View>
-struct libpng_is_view_supported : libpng_is_supported<typename View::value_type, is_planar<View>::value> {};
-
-
-typedef mpl::vector6
-<
- image<rgb8_pixel_t , false>,
- image<rgba8_pixel_t , false>,
- image<gray8_pixel_t , false>,
- image<rgb16_pixel_t , false>,
- image<rgba16_pixel_t, false>,
- image<gray16_pixel_t, false>
-> libpng_supported_pixel_formats;
-
-
-typedef generic_vertical_roi libpng_roi;
-
-
-struct libpng_view_data_t
-{
- typedef unsigned int format_t;
-
- template <class View>
- /*explicit*/ libpng_view_data_t( View const & view, libpng_roi::offset_t const offset = 0 )
- :
- format_( gil_to_libpng_format<typename View::value_type, is_planar<View>::value>::value ),
- buffer_( backend_base::get_raw_data( view ) ),
- offset_( offset ),
- height_( view.height() ),
- width_ ( view.width () ),
- stride_( view.pixels().row_size() ),
- number_of_channels_( num_channels<View>::value )
- {
- BOOST_STATIC_ASSERT( libpng_is_view_supported<View>::value );
- }
-
- void set_format( unsigned int const format ) { format_ = format; }
-
- format_t /*const*/ format_;
- png_byte * const buffer_;
- libpng_roi::offset_t const offset_;
-
- unsigned int const height_;
- unsigned int const width_ ;
- unsigned int const stride_;
- unsigned int const number_of_channels_;
-};
-
-
-inline void throw_libpng_error()
-{
- io_error( "LibPNG failure" );
-}
-
-
-inline void PNGAPI png_warning_function( png_structp /*png_ptr*/, png_const_charp const message )
-{
- #ifdef _DEBUG
- std::puts( message );
- #else
- ignore_unused_variable_warning( message );
- #endif // _DEBUG
-}
-
-
-inline void PNGAPI png_error_function( png_structp const png_ptr, png_const_charp const error_message )
-{
- png_warning_function( png_ptr, error_message );
-
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- throw_libpng_error();
- boost::ignore_unused_variable_warning( png_ptr );
- #else
- longjmp( png_ptr->jmpbuf, 1 );
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-}
-
-
-//...zzz...this will blow-up at link-time when included in multiple .cpps unless it gets moved into a separate module...
-#ifdef PNG_NO_ERROR_TEXT
- extern "C" void PNGAPI png_error( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_error_function( png_ptr, error_message );
- }
-
- extern "C" void PNGAPI png_chunk_error( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_error( png_ptr, error_message );
- }
-#endif // PNG_NO_ERROR_TEXT
-
-#ifdef PNG_NO_WARNINGS
- extern "C" void PNGAPI png_warning( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_warning_function( png_ptr, error_message );
- }
-
- extern "C" void PNGAPI png_chunk_warning( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_warning_function( png_ptr, error_message );
- }
-#endif // PNG_NO_WARNINGS
-
-
-class lib_object_t : noncopyable
-{
-public:
- png_struct & png_object() const
- {
- BOOST_ASSERT( p_png_ && "Call this only after ensuring creation success!" );
- png_struct & png( *p_png_ );
- BF_ASSUME( &png != 0 );
- return png;
- }
-
- png_info & info_object() const
- {
- BOOST_ASSERT( p_png_ && "Call this only after ensuring creation success!" );
- png_info & info( *p_info_ );
- BF_ASSUME( &info != 0 );
- return info;
- }
-
-protected:
- lib_object_t( png_struct * const p_png, png_info * const p_info )
- :
- p_png_ ( p_png ),
- p_info_( p_info )
- {}
-
-#ifndef NDEBUG
- ~lib_object_t()
- {
- BOOST_ASSERT( !p_png_ && !p_info_ && "The concrete class must do the cleanup!" );
- }
-#endif // NDEBUG
-
- //...zzz...forced by LibPNG into either duplication or this anti-pattern...
- bool is_valid() const { return p_png_ && p_info_; }
-
- png_struct * & png_object_for_destruction () { return p_png_ ; }
- png_info * & info_object_for_destruction() { return p_info_; }
-
-private:
- png_struct * __restrict p_png_ ;
- png_info * __restrict p_info_;
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_base
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_base : public lib_object_t
-{
-public:
- typedef lib_object_t lib_object_t;
-
- lib_object_t & lib_object() { return *this; }
-
-protected:
- libpng_base( png_struct * const p_png )
- :
- lib_object_t( p_png, ::png_create_info_struct( p_png ) )
- {}
-
- //...zzz...forced by LibPNG into either duplication or this anti-pattern...
- bool successful_creation() const { return is_valid(); }
-
- static std::size_t format_bit_depth( libpng_view_data_t::format_t const format )
- {
- return ( format >> 16 ) & 0xFF;
- }
-
- static std::size_t format_colour_type( libpng_view_data_t::format_t const format )
- {
- return format & 0xFF;
- }
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jmp_buf & error_handler_target() const { return png_object().jmpbuf; }
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_writer
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_writer
- :
- public libpng_base,
- public configure_on_write_writer
-{
-public:
- void write_default( libpng_view_data_t const & view )
- {
- ::png_set_IHDR
- (
- &png_object () ,
- &info_object() ,
- view.width_ ,
- view.height_ ,
- format_bit_depth ( view.format_ ),
- format_colour_type( view.format_ ),
- PNG_INTERLACE_NONE ,
- PNG_COMPRESSION_TYPE_DEFAULT ,
- PNG_FILTER_TYPE_DEFAULT
- );
-
- //::png_set_invert_alpha( &png_object() );
-
- write( view );
- }
-
- void write( libpng_view_data_t const & view ) BOOST_GIL_CAN_THROW
- {
- BOOST_ASSERT( view.format_ != JCS_UNKNOWN );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- detail::throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- if ( little_endian() )
- ::png_set_swap( &png_object() );
-
- ::png_write_info( &png_object(), &info_object() );
-
- png_byte * p_row( view.buffer_ );
- png_byte * const p_end( memunit_advanced( view.buffer_, view.height_ * view.stride_ ) );
- while ( p_row < p_end )
- {
- ::png_write_row( &png_object(), p_row );
- memunit_advance( p_row, view.stride_ );
- }
-
- ::png_write_end( &png_object(), 0 );
- }
-
-protected:
- libpng_writer( void * const p_target_object, png_rw_ptr const write_data_fn, png_flush_ptr const output_flush_fn )
- :
- libpng_base( ::png_create_write_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
- {
- if ( !successful_creation() )
- cleanup_and_throw_libpng_error();
-
- setup_destination( p_target_object, write_data_fn, output_flush_fn );
- }
-
- ~libpng_writer()
- {
- destroy_write_struct();
- }
-
-private:
- void destroy_write_struct() { ::png_destroy_write_struct( &png_object_for_destruction(), &info_object_for_destruction() ); }
-
- void cleanup_and_throw_libpng_error()
- {
- destroy_write_struct();
- detail::throw_libpng_error();
- }
-
- void setup_destination( void * const p_target_object, png_rw_ptr const write_data_fn, png_flush_ptr const output_flush_fn )
- {
- ::png_set_write_fn( &png_object(), p_target_object, write_data_fn, output_flush_fn );
- }
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_writer_FILE
-///
-/// \brief
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_writer_FILE : public libpng_writer
-{
-public:
- libpng_writer_FILE( FILE & file )
- :
- libpng_writer( &file, &png_write_data, &png_flush_data )
- {}
-
-private:
- static void PNGAPI png_write_data( png_structp const png_ptr, png_bytep const data, png_size_t const length )
- {
- BOOST_ASSERT( png_ptr );
-
- png_size_t const written_size
- (
- static_cast<png_size_t>( std::fwrite( data, 1, length, static_cast<FILE *>( png_ptr->io_ptr ) ) )
- );
-
- if ( written_size != length )
- png_error_function( png_ptr, "Write Error" );
- }
-
- static void PNGAPI png_flush_data( png_structp const png_ptr )
- {
- BOOST_ASSERT( png_ptr );
-
- std::fflush( static_cast<FILE *>( png_ptr->io_ptr ) );
- }
-};
-
 //------------------------------------------------------------------------------
 } // namespace detail
 
 
-class libpng_image;
-
-template <>
-struct backend_traits<libpng_image>
-{
- typedef detail::libpng_supported_pixel_formats supported_pixel_formats_t;
- typedef detail::libpng_roi roi_t;
- typedef detail::libpng_view_data_t view_data_t;
- typedef detail::libpng_view_data_t::format_t format_t;
-
- struct gil_to_native_format
- {
- template <typename Pixel, bool IsPlanar>
- struct apply : detail::gil_to_libpng_format<Pixel, IsPlanar> {};
- };
-
- template <typename Pixel, bool IsPlanar>
- struct is_supported : detail::libpng_is_supported<Pixel, IsPlanar> {};
-
- typedef mpl::map3
- <
- mpl::pair<memory_range_t , detail::seekable_input_memory_range_extender<libpng_image> >,
- mpl::pair<FILE , libpng_image >,
- mpl::pair<char const *, detail::input_c_str_for_mmap_extender <libpng_image> >
- > native_sources;
-
- typedef mpl::map2
- <
- mpl::pair<FILE , detail::libpng_writer_FILE >,
- mpl::pair<char const *, detail::output_c_str_for_c_file_extender<detail::libpng_writer_FILE> >
- > native_sinks;
-
- typedef mpl::vector1_c<format_tag, png> supported_image_formats;
-
- typedef view_data_t writer_view_data_t;
-
- BOOST_STATIC_CONSTANT( unsigned int, desired_alignment = sizeof( void * ) );
- BOOST_STATIC_CONSTANT( bool , builtin_conversion = true );
-};
-
-
 ////////////////////////////////////////////////////////////////////////////////
 ///
-/// \class libpng_image
+/// \class libpng_reader
 ///
 /// \brief
 ///
 ////////////////////////////////////////////////////////////////////////////////
 
-class libpng_image
+class libpng_reader
     :
- public detail::backend<libpng_image>,
- private detail::libpng_base
+ public detail::libpng_image
 {
 public:
     struct guard {};
@@ -448,7 +76,7 @@
         {
             default: return current_format;
 
- case PNG_COLOR_TYPE_PALETTE : return PNG_COLOR_TYPE_RGB | ( 8 << 16 ); // 8-bit RGB
+ case PNG_COLOR_TYPE_PALETTE : return PNG_COLOR_TYPE_RGB | ( 8 << 16 ); // 8-bit RGB
             case PNG_COLOR_TYPE_GRAY_ALPHA: return PNG_COLOR_TYPE_RGBA | ( ( ( current_format >> 16 ) & 0xFF ) << 16 ); // (bits of current_format) RGBA
         }
     }
@@ -458,21 +86,6 @@
         return image_format_id( closest_gil_supported_format() );
     }
 
- static image_type_id image_format_id( format_t const closest_gil_supported_format )
- {
- switch ( closest_gil_supported_format )
- {
- case PNG_COLOR_TYPE_RGB | ( 8 << 16 ): return 0;
- case PNG_COLOR_TYPE_RGB_ALPHA | ( 8 << 16 ): return 1;
- case PNG_COLOR_TYPE_GRAY | ( 8 << 16 ): return 2;
- case PNG_COLOR_TYPE_RGB | ( 16 << 16 ): return 3;
- case PNG_COLOR_TYPE_RGB_ALPHA | ( 16 << 16 ): return 4;
- case PNG_COLOR_TYPE_GRAY | ( 16 << 16 ): return 5;
-
- default:
- return unsupported_format;
- }
- }
 
     std::size_t pixel_size() const
     {
@@ -480,35 +93,28 @@
     }
 
 public: /// \ingroup Construction
- explicit libpng_image( FILE & file )
+ template <class Device>
+ explicit libpng_image( Device & device )
         :
         libpng_base( ::png_create_read_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
     {
         if ( !successful_creation() )
             cleanup_and_throw_libpng_error();
 
- #ifdef PNG_NO_STDIO
- ::png_set_read_fn( &png_object(), &file, &png_FILE_read_data );
- #else
- ::png_init_io( &png_object(), &file );
- #endif
+ #ifndef PNG_NO_STDIO
+ if ( boost::is_same<Device, ::FILE>::value )
+ ::png_init_io( &png_object(), &device );
+ else
+ #else
+ //png_rw_ptr
+ ::png_set_read_fn( &png_object(), &device, &png_read_data<Device> );
+ #endif
 
         init();
     }
 
- explicit libpng_image( memory_range_t & in_memory_image )
- :
- libpng_base( ::png_create_read_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
- {
- if ( !successful_creation() )
- cleanup_and_throw_libpng_error();
- //png_rw_ptr
- ::png_set_read_fn( &png_object(), &in_memory_image, &png_memory_chunk_read_data );
 
- init();
- }
-
- ~libpng_image()
+ ~libpng_reader()
     {
         destroy_read_struct();
     }
@@ -750,10 +356,9 @@
     }
 };
 
-
 //------------------------------------------------------------------------------
 } // namespace gil
 //------------------------------------------------------------------------------
 } // namespace boost
 //------------------------------------------------------------------------------
-#endif // libpng_image_hpp
+#endif // reader_hpp

Copied and modified: sandbox/gil/boost/gil/extension/io2/backends/libpng/writer.hpp (from r85344, sandbox/gil/boost/gil/extension/io2/libpng_image.hpp)
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/libpng_image.hpp Tue Aug 13 18:43:39 2013 (r85344, copy source)
+++ sandbox/gil/boost/gil/extension/io2/backends/libpng/writer.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85502)
@@ -1,11 +1,12 @@
 ////////////////////////////////////////////////////////////////////////////////
 ///
-/// \file libpng_image.hpp
-/// ----------------------
+/// \file libpng/writer.hpp
+/// ------------------------
 ///
-/// Copyright (c) Domagoj Saric 2010.
+/// Copyright (c) Domagoj Saric 2010.-2013.
 ///
-/// Use, modification and distribution is subject to the Boost Software License, Version 1.0.
+/// Use, modification and distribution is subject to the
+/// Boost Software License, Version 1.0.
 /// (See accompanying file LICENSE_1_0.txt or copy at
 /// http://www.boost.org/LICENSE_1_0.txt)
 ///
@@ -13,8 +14,8 @@
 ///
 ////////////////////////////////////////////////////////////////////////////////
 //------------------------------------------------------------------------------
-#ifndef libpng_image_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
-#define libpng_image_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
+#ifndef writer_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
+#define writer_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
 #pragma once
 //------------------------------------------------------------------------------
 #include "backend.hpp"
@@ -42,210 +43,6 @@
 {
 //------------------------------------------------------------------------------
 
-template <typename Pixel, bool isPlanar>
-struct gil_to_libpng_format : mpl::integral_c<unsigned int, backend_base::unsupported_format> {};
-
-/// \todo Add bgr-layout formats as LibPNG actually supports them through
-/// png_set_bgr().
-/// (19.09.2010.) (Domagoj Saric)
-template <> struct gil_to_libpng_format<rgb8_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB | ( 8 << 16 )> {};
-template <> struct gil_to_libpng_format<rgba8_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB_ALPHA | ( 8 << 16 )> {};
-template <> struct gil_to_libpng_format<gray8_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_GRAY | ( 8 << 16 )> {};
-template <> struct gil_to_libpng_format<rgb16_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB | ( 16 << 16 )> {};
-template <> struct gil_to_libpng_format<rgba16_pixel_t, false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB_ALPHA | ( 16 << 16 )> {};
-template <> struct gil_to_libpng_format<gray16_pixel_t, false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_GRAY | ( 16 << 16 )> {};
-
-
-template <typename Pixel, bool IsPlanar>
-struct libpng_is_supported : mpl::bool_<gil_to_libpng_format<Pixel, IsPlanar>::value != -1> {};
-
-template <typename View>
-struct libpng_is_view_supported : libpng_is_supported<typename View::value_type, is_planar<View>::value> {};
-
-
-typedef mpl::vector6
-<
- image<rgb8_pixel_t , false>,
- image<rgba8_pixel_t , false>,
- image<gray8_pixel_t , false>,
- image<rgb16_pixel_t , false>,
- image<rgba16_pixel_t, false>,
- image<gray16_pixel_t, false>
-> libpng_supported_pixel_formats;
-
-
-typedef generic_vertical_roi libpng_roi;
-
-
-struct libpng_view_data_t
-{
- typedef unsigned int format_t;
-
- template <class View>
- /*explicit*/ libpng_view_data_t( View const & view, libpng_roi::offset_t const offset = 0 )
- :
- format_( gil_to_libpng_format<typename View::value_type, is_planar<View>::value>::value ),
- buffer_( backend_base::get_raw_data( view ) ),
- offset_( offset ),
- height_( view.height() ),
- width_ ( view.width () ),
- stride_( view.pixels().row_size() ),
- number_of_channels_( num_channels<View>::value )
- {
- BOOST_STATIC_ASSERT( libpng_is_view_supported<View>::value );
- }
-
- void set_format( unsigned int const format ) { format_ = format; }
-
- format_t /*const*/ format_;
- png_byte * const buffer_;
- libpng_roi::offset_t const offset_;
-
- unsigned int const height_;
- unsigned int const width_ ;
- unsigned int const stride_;
- unsigned int const number_of_channels_;
-};
-
-
-inline void throw_libpng_error()
-{
- io_error( "LibPNG failure" );
-}
-
-
-inline void PNGAPI png_warning_function( png_structp /*png_ptr*/, png_const_charp const message )
-{
- #ifdef _DEBUG
- std::puts( message );
- #else
- ignore_unused_variable_warning( message );
- #endif // _DEBUG
-}
-
-
-inline void PNGAPI png_error_function( png_structp const png_ptr, png_const_charp const error_message )
-{
- png_warning_function( png_ptr, error_message );
-
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- throw_libpng_error();
- boost::ignore_unused_variable_warning( png_ptr );
- #else
- longjmp( png_ptr->jmpbuf, 1 );
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-}
-
-
-//...zzz...this will blow-up at link-time when included in multiple .cpps unless it gets moved into a separate module...
-#ifdef PNG_NO_ERROR_TEXT
- extern "C" void PNGAPI png_error( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_error_function( png_ptr, error_message );
- }
-
- extern "C" void PNGAPI png_chunk_error( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_error( png_ptr, error_message );
- }
-#endif // PNG_NO_ERROR_TEXT
-
-#ifdef PNG_NO_WARNINGS
- extern "C" void PNGAPI png_warning( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_warning_function( png_ptr, error_message );
- }
-
- extern "C" void PNGAPI png_chunk_warning( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_warning_function( png_ptr, error_message );
- }
-#endif // PNG_NO_WARNINGS
-
-
-class lib_object_t : noncopyable
-{
-public:
- png_struct & png_object() const
- {
- BOOST_ASSERT( p_png_ && "Call this only after ensuring creation success!" );
- png_struct & png( *p_png_ );
- BF_ASSUME( &png != 0 );
- return png;
- }
-
- png_info & info_object() const
- {
- BOOST_ASSERT( p_png_ && "Call this only after ensuring creation success!" );
- png_info & info( *p_info_ );
- BF_ASSUME( &info != 0 );
- return info;
- }
-
-protected:
- lib_object_t( png_struct * const p_png, png_info * const p_info )
- :
- p_png_ ( p_png ),
- p_info_( p_info )
- {}
-
-#ifndef NDEBUG
- ~lib_object_t()
- {
- BOOST_ASSERT( !p_png_ && !p_info_ && "The concrete class must do the cleanup!" );
- }
-#endif // NDEBUG
-
- //...zzz...forced by LibPNG into either duplication or this anti-pattern...
- bool is_valid() const { return p_png_ && p_info_; }
-
- png_struct * & png_object_for_destruction () { return p_png_ ; }
- png_info * & info_object_for_destruction() { return p_info_; }
-
-private:
- png_struct * __restrict p_png_ ;
- png_info * __restrict p_info_;
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_base
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_base : public lib_object_t
-{
-public:
- typedef lib_object_t lib_object_t;
-
- lib_object_t & lib_object() { return *this; }
-
-protected:
- libpng_base( png_struct * const p_png )
- :
- lib_object_t( p_png, ::png_create_info_struct( p_png ) )
- {}
-
- //...zzz...forced by LibPNG into either duplication or this anti-pattern...
- bool successful_creation() const { return is_valid(); }
-
- static std::size_t format_bit_depth( libpng_view_data_t::format_t const format )
- {
- return ( format >> 16 ) & 0xFF;
- }
-
- static std::size_t format_colour_type( libpng_view_data_t::format_t const format )
- {
- return format & 0xFF;
- }
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jmp_buf & error_handler_target() const { return png_object().jmpbuf; }
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-};
-
-
 ////////////////////////////////////////////////////////////////////////////////
 ///
 /// \class libpng_writer
@@ -375,385 +172,9 @@
 
 //------------------------------------------------------------------------------
 } // namespace detail
-
-
-class libpng_image;
-
-template <>
-struct backend_traits<libpng_image>
-{
- typedef detail::libpng_supported_pixel_formats supported_pixel_formats_t;
- typedef detail::libpng_roi roi_t;
- typedef detail::libpng_view_data_t view_data_t;
- typedef detail::libpng_view_data_t::format_t format_t;
-
- struct gil_to_native_format
- {
- template <typename Pixel, bool IsPlanar>
- struct apply : detail::gil_to_libpng_format<Pixel, IsPlanar> {};
- };
-
- template <typename Pixel, bool IsPlanar>
- struct is_supported : detail::libpng_is_supported<Pixel, IsPlanar> {};
-
- typedef mpl::map3
- <
- mpl::pair<memory_range_t , detail::seekable_input_memory_range_extender<libpng_image> >,
- mpl::pair<FILE , libpng_image >,
- mpl::pair<char const *, detail::input_c_str_for_mmap_extender <libpng_image> >
- > native_sources;
-
- typedef mpl::map2
- <
- mpl::pair<FILE , detail::libpng_writer_FILE >,
- mpl::pair<char const *, detail::output_c_str_for_c_file_extender<detail::libpng_writer_FILE> >
- > native_sinks;
-
- typedef mpl::vector1_c<format_tag, png> supported_image_formats;
-
- typedef view_data_t writer_view_data_t;
-
- BOOST_STATIC_CONSTANT( unsigned int, desired_alignment = sizeof( void * ) );
- BOOST_STATIC_CONSTANT( bool , builtin_conversion = true );
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_image
-///
-/// \brief
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_image
- :
- public detail::backend<libpng_image>,
- private detail::libpng_base
-{
-public:
- struct guard {};
-
-public:
- format_t format() const
- {
- return ::png_get_color_type( &png_object(), &info_object() ) | ( bit_depth() << 16 );
- }
-
- format_t closest_gil_supported_format() const
- {
- format_t const current_format( format() );
-
- switch ( current_format & 0xFF )
- {
- default: return current_format;
-
- case PNG_COLOR_TYPE_PALETTE : return PNG_COLOR_TYPE_RGB | ( 8 << 16 ); // 8-bit RGB
- case PNG_COLOR_TYPE_GRAY_ALPHA: return PNG_COLOR_TYPE_RGBA | ( ( ( current_format >> 16 ) & 0xFF ) << 16 ); // (bits of current_format) RGBA
- }
- }
-
- image_type_id current_image_format_id() const
- {
- return image_format_id( closest_gil_supported_format() );
- }
-
- static image_type_id image_format_id( format_t const closest_gil_supported_format )
- {
- switch ( closest_gil_supported_format )
- {
- case PNG_COLOR_TYPE_RGB | ( 8 << 16 ): return 0;
- case PNG_COLOR_TYPE_RGB_ALPHA | ( 8 << 16 ): return 1;
- case PNG_COLOR_TYPE_GRAY | ( 8 << 16 ): return 2;
- case PNG_COLOR_TYPE_RGB | ( 16 << 16 ): return 3;
- case PNG_COLOR_TYPE_RGB_ALPHA | ( 16 << 16 ): return 4;
- case PNG_COLOR_TYPE_GRAY | ( 16 << 16 ): return 5;
-
- default:
- return unsupported_format;
- }
- }
-
- std::size_t pixel_size() const
- {
- return number_of_channels() * bit_depth() / 8;
- }
-
-public: /// \ingroup Construction
- explicit libpng_image( FILE & file )
- :
- libpng_base( ::png_create_read_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
- {
- if ( !successful_creation() )
- cleanup_and_throw_libpng_error();
-
- #ifdef PNG_NO_STDIO
- ::png_set_read_fn( &png_object(), &file, &png_FILE_read_data );
- #else
- ::png_init_io( &png_object(), &file );
- #endif
-
- init();
- }
-
- explicit libpng_image( memory_range_t & in_memory_image )
- :
- libpng_base( ::png_create_read_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
- {
- if ( !successful_creation() )
- cleanup_and_throw_libpng_error();
- //png_rw_ptr
- ::png_set_read_fn( &png_object(), &in_memory_image, &png_memory_chunk_read_data );
-
- init();
- }
-
- ~libpng_image()
- {
- destroy_read_struct();
- }
-
-public:
- point2<std::ptrdiff_t> dimensions() const
- {
- return point2<std::ptrdiff_t>
- (
- ::png_get_image_width ( &png_object(), &info_object() ),
- ::png_get_image_height( &png_object(), &info_object() )
- );
- }
-
-public: // Low-level (row, strip, tile) access
- void read_row( sequential_row_read_state, unsigned char * const p_row_storage ) const
- {
- read_row( p_row_storage );
- }
-
- png_struct & lib_object() const { return png_object(); }
-
-private: // Private backend_base interface.
- // Implementation note:
- // MSVC 10 accepts friend base_t and friend class base_t, Clang 2.8
- // accepts friend class base_t, Apple Clang 1.6 and GCC (4.2 and 4.6) accept
- // neither.
- // (13.01.2011.) (Domagoj Saric)
- friend class detail::backend<libpng_image>;
-
- template <class MyView, class TargetView, class Converter>
- void generic_convert_to_prepared_view( TargetView const & view, Converter const & converter ) const BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- using namespace detail;
-
- std::size_t const row_length ( ::png_get_rowbytes( &png_object(), &info_object() ) );
- scoped_array<png_byte> const p_row_buffer( new png_byte[ row_length ] );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- detail::throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- unsigned int number_of_passes( ::png_set_interlace_handling( &png_object() ) );
- BF_ASSUME( ( number_of_passes == 1 ) || ( number_of_passes == 7 ) );
- BOOST_ASSERT( ( number_of_passes == 1 ) && "Missing interlaced support for the generic conversion case." );
- ignore_unused_variable_warning( number_of_passes );
-
- if ( is_offset_view<TargetView>::value )
- skip_rows( get_offset<offset_t>( view ) );
-
- png_byte * const p_row ( p_row_buffer.get() );
- png_byte const * const p_row_end( p_row + row_length );
-
- unsigned int const rows_to_read( original_view( view ).dimensions().y );
- for ( unsigned int row_index( 0 ); row_index < rows_to_read; ++row_index )
- {
- read_row( p_row );
-
- typedef typename MyView::value_type pixel_t;
- typedef typename get_original_view_t<TargetView>::type::x_iterator x_iterator;
-
- pixel_t const * p_source_pixel( gil_reinterpret_cast_c<pixel_t const *>( p_row ) );
- x_iterator p_target_pixel( original_view( view ).row_begin( row_index ) );
- while ( p_source_pixel < gil_reinterpret_cast_c<pixel_t const *>( p_row_end ) )
- {
- converter( *p_source_pixel, *p_target_pixel );
- ++p_source_pixel;
- ++p_target_pixel;
- }
- }
- }
-
- void raw_convert_to_prepared_view( detail::libpng_view_data_t const & view_data ) const
- {
- BOOST_ASSERT( view_data.width_ == static_cast<unsigned int>( dimensions().x ) );
- BOOST_ASSERT( view_data.height_ == static_cast<unsigned int>( dimensions().y ) );
- BOOST_ASSERT( view_data.format_ == static_cast<unsigned int>( closest_gil_supported_format() ) );
-
- unsigned int const bit_depth ( ( view_data.format_ >> 16 ) & 0xFF );
- unsigned int const colour_type( view_data.format_ & 0xFF );
-
- if ( colour_type == PNG_COLOR_TYPE_PALETTE )
- {
- ::png_set_palette_to_rgb( &png_object() );
- }
- else
- if ( ( colour_type == PNG_COLOR_TYPE_GRAY ) && ( bit_depth < 8 ) )
- {
- ::png_set_expand_gray_1_2_4_to_8( &png_object() );
- }
-
- if ( !( colour_type & PNG_COLOR_MASK_ALPHA ) )
- ::png_set_strip_alpha( &png_object() );
-
- if ( ::png_get_valid( &png_object(), &info_object(), PNG_INFO_tRNS ) )
- ::png_set_tRNS_to_alpha( &png_object() );
-
- if ( bit_depth == 8 )
- ::png_set_strip_16( &png_object() );
-
- //...zzz...
-
- //if (color_type == PNG_COLOR_TYPE_RGB ||
- // color_type == PNG_COLOR_TYPE_RGB_ALPHA)
- // png_set_bgr(png_ptr);
-
- //if (color_type == PNG_COLOR_TYPE_RGB ||
- // color_type == PNG_COLOR_TYPE_GRAY)
- // png_set_add_alpha(png_ptr, filler, PNG_FILLER_AFTER);
-
- //if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
- // png_set_swap_alpha(png_ptr);
-
- //if (color_type == PNG_COLOR_TYPE_GRAY ||
- // color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
- // png_set_gray_to_rgb(png_ptr);
-
- //if (color_type == PNG_COLOR_TYPE_RGB ||
- // color_type == PNG_COLOR_TYPE_RGB_ALPHA)
- // png_set_rgb_to_gray_fixed(png_ptr, error_action,
- // int red_weight, int green_weight);
-
- ::png_read_update_info( &png_object(), &info_object() );
-
- raw_copy_to_prepared_view( view_data );
- }
-
- void raw_copy_to_prepared_view( detail::libpng_view_data_t const view_data ) const BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- detail::throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- unsigned int number_of_passes( ::png_set_interlace_handling( &png_object() ) );
- BF_ASSUME( ( number_of_passes == 1 ) || ( number_of_passes == 7 ) );
-
- skip_rows( view_data.offset_ );
-
- while ( number_of_passes-- )
- {
- png_byte * p_row( view_data.buffer_ );
- png_byte const * const p_end( p_row + ( view_data.height_ * view_data.stride_ ) );
- while ( p_row < p_end )
- {
- read_row( p_row );
- memunit_advance( p_row, view_data.stride_ );
- }
- }
- }
-
- std::size_t cached_format_size( format_t const format ) const
- {
- return number_of_channels() * format_bit_depth( format ) / 8;
- }
-
-private:
- void cleanup_and_throw_libpng_error()
- {
- destroy_read_struct();
- detail::throw_libpng_error();
- }
-
- void init() BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- try
- {
- #else
- if ( setjmp( error_handler_target() ) )
- cleanup_and_throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- ::png_read_info( &png_object(), &info_object() );
- if ( little_endian() )
- ::png_set_swap( &png_object() );
-
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- }
- catch (...)
- {
- destroy_read_struct();
- throw;
- }
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- }
-
- void skip_rows( unsigned int const row_to_skip_to ) const
- {
- BOOST_ASSERT( ( row_to_skip_to >= png_object().row_number ) && "No 'rewind' capability for LibPNG yet." );
-
- unsigned int number_of_rows_to_skip( row_to_skip_to - png_object().row_number );
- while ( number_of_rows_to_skip-- )
- {
- BF_ASSUME( row_to_skip_to != 0 );
- read_row( NULL );
- }
- }
-
- unsigned int number_of_channels() const { return ::png_get_channels ( &png_object(), &info_object() ); }
- std::size_t bit_depth () const { return ::png_get_bit_depth( &png_object(), &info_object() ); }
-
- void destroy_read_struct() { ::png_destroy_read_struct( &png_object_for_destruction(), &info_object_for_destruction(), NULL ); }
-
- void read_row( png_byte * const p_row ) const BOOST_GIL_CAN_THROW
- {
- ::png_read_row( &png_object(), p_row, NULL );
- }
-
- static void PNGAPI png_FILE_read_data( png_structp const png_ptr, png_bytep const data, png_size_t const length )
- {
- BOOST_ASSERT( png_ptr );
- BOOST_ASSERT( png_ptr->io_ptr );
-
- png_size_t const read_size
- (
- static_cast<png_size_t>( std::fread( data, 1, length, static_cast<FILE *>( png_ptr->io_ptr ) ) )
- );
-
- if ( read_size != length )
- detail::png_error_function( png_ptr, "Read Error" );
- }
-
- static void PNGAPI png_memory_chunk_read_data( png_structp const png_ptr, png_bytep const data, png_size_t const length )
- {
- BOOST_ASSERT( png_ptr );
- BOOST_ASSERT( png_ptr->io_ptr );
-
- memory_range_t & memory_chunk( *static_cast<memory_range_t *>( png_ptr->io_ptr ) );
-
- if ( length <= static_cast<std::size_t>( memory_chunk.size() ) )
- {
- std::memcpy( data, memory_chunk.begin(), length );
- memory_chunk.advance_begin( length );
- }
- else
- detail::png_error_function( png_ptr, "Read Error" );
- }
-};
-
-
 //------------------------------------------------------------------------------
 } // namespace gil
 //------------------------------------------------------------------------------
 } // namespace boost
 //------------------------------------------------------------------------------
-#endif // libpng_image_hpp
+#endif // writer_hpp

Modified: sandbox/gil/boost/gil/extension/io2/backends/libtiff/writer.hpp
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/backends/libtiff/writer.hpp Wed Aug 28 09:54:25 2013 (r85501)
+++ sandbox/gil/boost/gil/extension/io2/backends/libtiff/writer.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85502)
@@ -5,7 +5,7 @@
 ///
 /// LibTIFF writer.
 ///
-/// Copyright (c) Domagoj Saric 2010.-2011.
+/// Copyright (c) Domagoj Saric 2010.-2013.
 ///
 /// Use, modification and distribution is subject to the Boost Software License, Version 1.0.
 /// (See accompanying file LICENSE_1_0.txt or copy at
@@ -26,8 +26,6 @@
 #include "boost/gil/extension/io2/detail/libx_shared.hpp"
 #include "boost/gil/extension/io2/detail/platform_specifics.hpp"
 #include "boost/gil/extension/io2/detail/shared.hpp"
-
-#include <boost/array.hpp>
 //------------------------------------------------------------------------------
 namespace boost
 {

Deleted: sandbox/gil/boost/gil/extension/io2/gp_image.hpp
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/gp_image.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85501)
+++ /dev/null 00:00:00 1970 (deleted)
@@ -1,784 +0,0 @@
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \file gp_image.hpp
-/// ------------------
-///
-/// Base IO interface GDI+ implementation.
-///
-/// Copyright (c) 2010.-2013. Domagoj Saric
-///
-/// Use, modification and distribution is subject to the
-/// Boost Software License, Version 1.0.
-/// (See accompanying file LICENSE_1_0.txt or copy at
-/// http://www.boost.org/LICENSE_1_0.txt)
-///
-/// For more information, see http://www.boost.org
-///
-////////////////////////////////////////////////////////////////////////////////
-//------------------------------------------------------------------------------
-#pragma once
-#ifndef gp_private_base_hpp__3B1ED5BC_42C6_4EC6_B700_01C1B8646431
-#define gp_private_base_hpp__3B1ED5BC_42C6_4EC6_B700_01C1B8646431
-//------------------------------------------------------------------------------
-#include "detail/io_error.hpp"
-#include "detail/gp_extern_lib_guard.hpp"
-#include "detail/windows_shared.hpp"
-#include "detail/windows_shared_istreams.hpp"
-#include "backend.hpp"
-
-#include <boost/array.hpp>
-#include <boost/mpl/eval_if.hpp>
-#include <boost/mpl/integral_c.hpp>
-#include <boost/range/iterator_range.hpp>
-#include <boost/static_assert.hpp>
-#include <boost/type_traits/is_pod.hpp>
-
-#include <algorithm>
-//------------------------------------------------------------------------------
-namespace boost
-{
-//------------------------------------------------------------------------------
-namespace gil
-{
-//------------------------------------------------------------------------------
-namespace detail
-{
-//------------------------------------------------------------------------------
-
-template <Gdiplus::PixelFormat gp_format> struct is_canonical : mpl::bool_ <(gp_format & PixelFormatCanonical) != 0> {};
-template <Gdiplus::PixelFormat gp_format> struct is_extended : mpl::bool_ <(gp_format & PixelFormatExtended ) != 0> {};
-template <Gdiplus::PixelFormat gp_format> struct is_indexed : mpl::bool_ <(gp_format & PixelFormatIndexed ) != 0> {};
-template <Gdiplus::PixelFormat gp_format> struct has_alpha : mpl::bool_ <(gp_format & PixelFormatAlpha ) != 0> {};
-template <Gdiplus::PixelFormat gp_format> struct has_premultiplied_alpha : mpl::bool_ <(gp_format & PixelFormatPAlpha ) != 0> {};
-template <Gdiplus::PixelFormat gp_format> struct is_supported : mpl::bool_ <(gp_format & PixelFormatGDI ) != 0 || ( ( gp_format == PixelFormat32bppCMYK ) && ( GDIPVER >= 0x0110 ) )> {};
-template <Gdiplus::PixelFormat gp_format> struct pixel_size : mpl::size_t<( gp_format >> 8 ) & 0xff > {};
-
-
-/// @see GdiplusPixelFormats.h: ARGB -> little endian BGRA
-typedef bgra_layout_t gp_alpha_layout_t;
-typedef bgr_layout_t gp_layout_t;
-
-typedef packed_pixel_type<uint16_t, mpl::vector3_c<unsigned,5,6,5>, gp_layout_t>::type gp_bgr565_pixel_t;
-
-template <typename Pixel, bool IsPlanar>
-struct gil_to_gp_format : mpl::integral_c<Gdiplus::PixelFormat, PixelFormatUndefined> {};
-
-template <> struct gil_to_gp_format<gp_bgr565_pixel_t, false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat16bppRGB565 > {};
-template <> struct gil_to_gp_format<bgr8_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat24bppRGB > {};
-template <> struct gil_to_gp_format<bgra8_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat32bppARGB > {};
-template <> struct gil_to_gp_format<gray16_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat16bppGrayScale> {};
-template <> struct gil_to_gp_format<bgr16_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat48bppRGB > {};
-template <> struct gil_to_gp_format<bgra16_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat64bppARGB > {};
-#if (GDIPVER >= 0x0110)
-template <> struct gil_to_gp_format<cmyk8_pixel_t , false> : mpl::integral_c<Gdiplus::PixelFormat, PixelFormat32bppCMYK > {};
-#endif // (GDIPVER >= 0x0110)
-
-
-template <typename Pixel, bool IsPlanar>
-struct gp_is_supported : is_supported<gil_to_gp_format<Pixel, IsPlanar>::value>{};
-
-
-typedef mpl::
-#if (GDIPVER >= 0x0110)
- vector4
-#else
- vector3
-#endif
-<
- image<bgr8_pixel_t , false>,
- image<bgra8_pixel_t , false>,
- image<gp_bgr565_pixel_t, false>
- #if (GDIPVER >= 0x0110)
- ,image<cmyk8_pixel_t , false>
- #endif
-> gp_supported_pixel_formats;
-
-
-typedef iterator_range<TCHAR const *> string_chunk_t;
-
-
-/*string_chunk_t*/char const * error_string( Gdiplus::GpStatus const status )
-{
- using namespace Gdiplus;
- switch ( status )
- {
- case GenericError : return "Generic GDI+ error";
- case OutOfMemory : return "Out of memory";
- case ObjectBusy : return "Object busy";
- case InsufficientBuffer : return "Insufficient buffer";
- case NotImplemented : return "Not implemented";
- case Win32Error : return "Win32 subsystem failure";
- case Aborted : return "Aborted";
- case FileNotFound : return "File not found";
- case ValueOverflow : return "Numeric overflow";
- case AccessDenied : return "Access denied";
- case UnknownImageFormat : return "Unknown image format";
- case FontFamilyNotFound : return "Font family not found";
- case FontStyleNotFound : return "Font style not found";
- case NotTrueTypeFont : return "A non TrueType font specified";
- case UnsupportedGdiplusVersion : return "Unsupported GDI+ version";
- case PropertyNotFound : return "Specified property does not exist in the image";
- case PropertyNotSupported : return "Specified property not supported by the format of the image";
- #if (GDIPVER >= 0x0110)
- case ProfileNotFound : return "Profile required to save an image in CMYK format not found";
- #endif //(GDIPVER >= 0x0110)
- }
-
- // Programmer errors:
- switch ( status )
- {
- case Ok : BOOST_ASSERT( !"Should not be called for no error" ); BF_UNREACHABLE_CODE break;
- case InvalidParameter : BOOST_ASSERT( !"Invalid parameter" ); BF_UNREACHABLE_CODE break;
- case WrongState : BOOST_ASSERT( !"Object in wrong state" ); BF_UNREACHABLE_CODE break;
- case GdiplusNotInitialized : BOOST_ASSERT( !"GDI+ not initialized" ); BF_UNREACHABLE_CODE break;
-
- default: BOOST_ASSERT( !"Unknown GDI+ status code." ); BF_UNREACHABLE_CODE break;
- }
-}
-
-inline void ensure_result( Gdiplus::GpStatus const result )
-{
- if ( result != Gdiplus::Ok )
- io_error( error_string( result )/*.begin()*/ );
-}
-
-inline void verify_result( Gdiplus::GpStatus const result )
-{
- BOOST_VERIFY( result == Gdiplus::Ok );
-}
-
-
-class gp_roi : public Gdiplus::Rect
-{
-public:
- typedef INT value_type;
- typedef point2<value_type> point_t ;
-
- typedef point_t offset_t ;
-
-public:
- gp_roi( value_type const x, value_type const y, value_type const width, value_type const height )
- : Gdiplus::Rect( x, y, width, height ) {}
-
- gp_roi( offset_t const offset, value_type const width, value_type const height )
- : Gdiplus::Rect( Gdiplus::Point( offset.x, offset.y ), Gdiplus::Size( width, height ) ) {}
-
- gp_roi( offset_t const top_left, offset_t const bottom_right )
- : Gdiplus::Rect( Gdiplus::Point( top_left.x, top_left.y ), Gdiplus::Size( bottom_right.x - top_left.x, bottom_right.y - top_left.y ) ) {}
-};
-
-
-class gp_writer : public open_on_write_writer
-{
-public:
- typedef std::pair<Gdiplus::GpBitmap *, Gdiplus::EncoderParameters *> lib_object_t;
-
- // The passed View object must outlive the GpBitmap object (GDI+ uses lazy
- // evaluation).
- template <class View>
- explicit gp_writer( View & view )
- {
- BOOST_STATIC_ASSERT(( gp_is_supported<typename View::value_type, is_planar<View>::value>::value ));
-
- // http://msdn.microsoft.com/en-us/library/ms536315(VS.85).aspx
- // stride has to be a multiple of 4 bytes
- BOOST_ASSERT( !( view.pixels().row_size() % sizeof( Gdiplus::ARGB ) ) );
-
- ensure_result
- (
- Gdiplus::DllExports::GdipCreateBitmapFromScan0
- (
- view.width (),
- view.height(),
- view.pixels().row_size(),
- gil_to_gp_format<typename View::value_type, is_planar<View>::value>::value,
- backend_base::get_raw_data( view ),
- &lib_object().first
- )
- );
- BOOST_ASSERT( lib_object().first );
- }
-
- ~gp_writer()
- {
- detail::verify_result( Gdiplus::DllExports::GdipDisposeImage( lib_object().first ) );
- }
-
- template <typename Target>
- void write_default( Target const & target, format_tag const format )
- {
- BOOST_ASSERT( lib_object().second == NULL );
- write( target, format );
- }
-
- void write( char const * const pFilename, format_tag const format ) const
- {
- write( detail::wide_path( pFilename ), format );
- }
-
- void write( wchar_t const * const pFilename, format_tag const format ) const
- {
- detail::ensure_result
- (
- Gdiplus::DllExports::GdipSaveImageToFile
- (
- lib_object().first,
- pFilename,
- &encoder_id( format ),
- lib_object().second
- )
- );
- }
-
- lib_object_t & lib_object() { return lib_instance_; }
- lib_object_t const & lib_object() const { return lib_instance_; }
-
-private:
- static CLSID const & encoder_id( format_tag const format )
- {
- static CLSID const ids[ number_of_known_formats ] =
- {
- { 0x557CF400, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // BMP
- { 0x557CF402, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // GIF
- { 0x557CF401, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // JPEG
- { 0x557CF406, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // PNG
- { 0x557CF405, 0x1A04, 0x11D3, 0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E }, // TIFF
- CLSID_NULL // TGA
- };
- BOOST_ASSERT( ids[ format ] != CLSID_NULL );
- return ids[ format ];
- }
-
-private:
- lib_object_t lib_instance_;
-};
-
-
-class gp_view_base;
-
-//------------------------------------------------------------------------------
-} // namespace detail
-
-class gp_image;
-
-template <>
-struct backend_traits<gp_image>
-{
- typedef Gdiplus::PixelFormat format_t;
-
- typedef detail::gp_supported_pixel_formats supported_pixel_formats_t;
-
- typedef detail::gp_roi roi_t;
-
- struct gil_to_native_format
- {
- template <typename Pixel, bool IsPlanar>
- struct apply : detail::gil_to_gp_format<Pixel, IsPlanar> {};
- };
-
- template <typename Pixel, bool IsPlanar>
- struct is_supported : detail::gp_is_supported<Pixel, IsPlanar> {};
-
- typedef mpl::map5
- <
- mpl::pair<char const *, gp_image >,
- mpl::pair<wchar_t const *, gp_image >,
- mpl::pair<IStream , gp_image >,
- mpl::pair<FILE , detail::input_FILE_for_IStream_extender <gp_image> >,
- mpl::pair<memory_range_t , detail::memory_chunk_for_IStream_extender<gp_image> >
- > native_sources;
-
- typedef mpl::map4
- <
- mpl::pair<char const *, detail::gp_writer>,
- mpl::pair<wchar_t const *, detail::gp_writer>,
- mpl::pair<IStream , detail::gp_writer>,
- mpl::pair<FILE , detail::gp_writer>
- > native_sinks;
-
- typedef mpl::vector5_c<format_tag, bmp, gif, jpeg, png, tiff> supported_image_formats;
-
- class view_data_t : public Gdiplus::BitmapData
- {
- public:
- template <typename View>
- view_data_t( View const & view ) : p_roi_( 0 ) { set_bitmapdata_for_view( view ); }
-
- template <typename View>
- view_data_t( View const & view, roi_t::offset_t const & offset )
- :
- p_roi_( static_cast<roi_t const *>( optional_roi_.address() ) )
- {
- set_bitmapdata_for_view( view );
- new ( optional_roi_.address() ) roi_t( offset, Width, Height );
- }
-
- void set_format( format_t const format ) { PixelFormat = format; }
-
- public:
- Gdiplus::Rect const * const p_roi_;
-
- private:
- template <typename View>
- void set_bitmapdata_for_view( View const & view )
- {
- using namespace detail;
-
- BOOST_STATIC_ASSERT(( is_supported<typename View::value_type, is_planar<View>::value>::value ));
-
- Width = view.width ();
- Height = view.height();
- Stride = view.pixels().row_size();
- PixelFormat = detail::gil_to_gp_format<typename View::value_type, is_planar<View>::value>::value;
- Scan0 = backend_base::get_raw_data( view );
- Reserved = 0;
- }
-
- void operator=( view_data_t const & );
-
- private:
- aligned_storage<sizeof( roi_t ), alignment_of<roi_t>::value>::type optional_roi_;
- };
-
- typedef view_data_t writer_view_data_t;
-
- BOOST_STATIC_CONSTANT( unsigned int, desired_alignment = sizeof( Gdiplus::ARGB ) );
- BOOST_STATIC_CONSTANT( bool , builtin_conversion = true );
-};
-
-
-#if defined(BOOST_MSVC)
-# pragma warning( push )
-# pragma warning( disable : 4127 ) // "conditional expression is constant"
-#endif
-
-class gp_image
- :
- private detail::gp_guard,
- public detail::backend<gp_image>
-{
-public:
- typedef detail::gp_user_guard guard;
-
-public: /// \ingroup Construction
- explicit gp_image( wchar_t const * const filename )
- {
- detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromFile( filename, &pBitmap_ ) );
- BOOST_ASSERT( pBitmap_ );
- }
-
- explicit gp_image( char const * const filename )
- {
- detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromFile( detail::wide_path( filename ), &pBitmap_ ) );
- BOOST_ASSERT( pBitmap_ );
- }
-
- // The passed IStream object must outlive the GpBitmap object (GDI+ uses
- // lazy evaluation).
- explicit gp_image( IStream & stream )
- {
- detail::ensure_result( Gdiplus::DllExports::GdipCreateBitmapFromStream( &stream, &pBitmap_ ) );
- BOOST_ASSERT( pBitmap_ );
- }
-
- ~gp_image()
- {
- detail::verify_result( Gdiplus::DllExports::GdipDisposeImage( pBitmap_ ) );
- }
-
-public:
- point2<std::ptrdiff_t> dimensions() const
- {
- using namespace Gdiplus;
- REAL width, height;
- detail::verify_result( DllExports::GdipGetImageDimension( const_cast<GpBitmap *>( pBitmap_ ), &width, &height ) );
- return point2<std::ptrdiff_t>( static_cast<std::ptrdiff_t>( width ), static_cast<std::ptrdiff_t>( height ) );
- }
-
- format_t format() const
- {
- format_t pixel_format;
- detail::verify_result( Gdiplus::DllExports::GdipGetImagePixelFormat( pBitmap_, &pixel_format ) );
- return pixel_format;
- }
-
- format_t closest_gil_supported_format() const
- {
- //http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.gdi/2008-01/msg00044.html
- switch ( format() )
- {
- case PixelFormat16bppGrayScale:
- case PixelFormat48bppRGB :
- case PixelFormat32bppRGB :
- case PixelFormat24bppRGB :
- return
- PixelFormat24bppRGB;
-
- case PixelFormat64bppPARGB :
- case PixelFormat32bppPARGB :
- case PixelFormat64bppARGB :
- case PixelFormat32bppARGB :
- case PixelFormat16bppARGB1555 :
- return
- PixelFormat32bppARGB;
-
- case PixelFormat16bppRGB565 :
- case PixelFormat16bppRGB555 :
- case PixelFormat8bppIndexed :
- case PixelFormat4bppIndexed :
- case PixelFormat1bppIndexed :
- return
- PixelFormat16bppRGB565;
-
- case PixelFormat32bppCMYK:
- return
- #if (GDIPVER >= 0x0110)
- PixelFormat32bppCMYK;
- #else
- PixelFormat24bppRGB;
- #endif // (GDIPVER >= 0x0110)
-
- default:
- BF_UNREACHABLE_CODE
- return PixelFormatUndefined;
- }
- }
-
- image_type_id current_image_format_id() const
- {
- return image_format_id( closest_gil_supported_format() );
- }
-
- static image_type_id image_format_id( format_t const closest_gil_supported_format )
- {
- switch ( closest_gil_supported_format )
- {
- case PixelFormat24bppRGB : return 0;
- case PixelFormat32bppARGB : return 1;
- case PixelFormat16bppRGB565 : return 2;
- #if (GDIPVER >= 0x0110)
- case PixelFormat32bppCMYK : return 3;
- #endif
-
- default:
- BF_UNREACHABLE_CODE
- return unsupported_format;
- }
- }
-
-public: // Low-level (row, strip, tile) access
- static bool can_do_roi_access() { return true; }
-
- class sequential_row_read_state
- :
- private detail::cumulative_result
- {
- public:
- using detail::cumulative_result::failed;
- void throw_if_error() const { detail::cumulative_result::throw_if_error( "GDI+ failure" ); }
-
- BOOST_STATIC_CONSTANT( bool, throws_on_error = false );
-
- private:
- sequential_row_read_state( gp_image const & source_image )
- :
- roi_( 0, 0, source_image.dimensions().x, 1 )
- {
- bitmapData_.Width = roi_.Width;
- bitmapData_.Height = 1;
- bitmapData_.PixelFormat = source_image.format();
- bitmapData_.Stride = bitmapData_.Width * source_image.cached_format_size( bitmapData_.PixelFormat );
- bitmapData_.Reserved = 0;
- }
-
- private: friend gp_image;
- Gdiplus::Rect roi_ ;
- Gdiplus::BitmapData bitmapData_;
- };
-
- sequential_row_read_state begin_sequential_row_read() const { return sequential_row_read_state( *this ); }
-
- /// \todo Kill duplication with raw_convert_to_prepared_view().
- /// (04.01.2011.) (Domagoj Saric)
- void read_row( sequential_row_read_state & state, unsigned char * const p_row_storage ) const
- {
- using namespace detail ;
- using namespace Gdiplus;
-
- state.bitmapData_.Scan0 = p_row_storage;
-
- state.accumulate_equal
- (
- DllExports::GdipBitmapLockBits
- (
- pBitmap_,
- &state.roi_,
- ImageLockModeRead | ImageLockModeUserInputBuf,
- state.bitmapData_.PixelFormat,
- &state.bitmapData_
- ),
- Gdiplus::Ok
- );
- verify_result( DllExports::GdipBitmapUnlockBits( pBitmap_, &state.bitmapData_ ) );
-
- ++state.roi_.Y;
- }
-
- ::Gdiplus::GpBitmap & lib_object() { return *pBitmap_; }
- ::Gdiplus::GpBitmap const & lib_object() const { return const_cast<gp_image &>( *this ).lib_object(); }
-
-private: // Private backend_base interface.
- friend class base_t;
-
- template <class MyView, class TargetView, class Converter>
- void generic_convert_to_prepared_view( TargetView const & view, Converter const & converter ) const
- {
- BOOST_ASSERT( !dimensions_mismatch( view ) );
- //BOOST_ASSERT( !formats_mismatch ( view ) );
-
- using namespace detail ;
- using namespace Gdiplus;
-
- point2<std::ptrdiff_t> const & targetDimensions( original_view( view ).dimensions() );
- gp_roi const roi( get_offset<gp_roi::offset_t>( view ), targetDimensions.x, targetDimensions.y );
- format_t const my_format( gil_to_gp_format<typename View::value_type, is_planar<View>::value>::value );
- BitmapData bitmapData;
- ensure_result
- (
- DllExports::GdipBitmapLockBits
- (
- pBitmap_,
- &roi,
- ImageLockModeRead,
- my_format,
- &bitmapData
- )
- );
- BOOST_ASSERT( bitmapData.PixelFormat == my_format );
- copy_and_convert_pixels // This must not throw!
- (
- interleaved_view
- (
- bitmapData.Width ,
- bitmapData.Height,
- gil_reinterpret_cast_c<typename MyView::value_type const *>( bitmapData.Scan0 ),
- bitmapData.Stride
- ),
- view,
- converter
- );
- verify_result( DllExports::GdipBitmapUnlockBits( pBitmap_, &bitmapData ) );
- }
-
-
- void raw_convert_to_prepared_view( view_data_t const & view_data ) const
- {
- BOOST_ASSERT( view_data.Scan0 );
-
- using namespace detail ;
- using namespace Gdiplus;
-
- BitmapData * const pMutableBitmapData( const_cast<BitmapData *>( static_cast<BitmapData const *>( &view_data ) ) );
- GpStatus const load_result
- (
- DllExports::GdipBitmapLockBits
- (
- pBitmap_,
- view_data.p_roi_,
- ImageLockModeRead | ImageLockModeUserInputBuf,
- view_data.PixelFormat,
- pMutableBitmapData
- )
- );
- GpStatus const unlock_result( DllExports::GdipBitmapUnlockBits( pBitmap_, pMutableBitmapData ) );
- ensure_result( load_result );
- verify_result( unlock_result );
- }
-
-
- void raw_copy_to_prepared_view( view_data_t const & view_data ) const
- {
- BOOST_ASSERT( view_data.Width == static_cast<UINT>( dimensions().x ) );
- BOOST_ASSERT( view_data.Height == static_cast<UINT>( dimensions().y ) );
- //...this need not hold as it can be used to perform GDI+ default
- //internal colour conversion...maybe i'll provide another worker
- //function...
- //BOOST_ASSERT( view_data.PixelFormat == format () );
- raw_convert_to_prepared_view( view_data );
- }
-
-
- static std::size_t cached_format_size( format_t const format )
- {
- return Gdiplus::GetPixelFormatSize( format );
- }
-
-private:
- template <Gdiplus::PixelFormat desired_format>
- void pre_palettized_conversion( mpl::true_ /*is_indexed*/ )
- {
- #if (GDIPVER >= 0x0110)
- // A GDI+ 1.1 (a non-distributable version, distributed with MS Office
- // 2003 and MS Windows Vista and MS Windows 7) 'enhanced'/'tuned'
- // version of the conversion routine for indexed/palettized image
- // formats. Unless/until proven useful, pretty much still a GDI+ 1.1
- // tester...
-
- BOOST_ASSERT( !has_alpha<desired_format>::value && "Is this possible for indexed formats?" );
- std::size_t const number_of_pixel_bits ( pixel_size<desired_format>::value );
- std::size_t const number_of_palette_colours( static_cast<std::size_t>( ( 2 << ( number_of_pixel_bits - 1 ) ) - 1 ) );
- std::size_t const palette_size ( sizeof( ColorPalette ) + number_of_palette_colours * sizeof( ARGB ) );
- aligned_storage
- <
- palette_size,
- alignment_of<ColorPalette>::value
- >::type palette_storage;
- ColorPalette & palette( *static_cast<ColorPalette *>( palette_storage.address() ) );
- palette.Flags = ( PaletteFlagsHasAlpha * has_alpha<desired_format>::value ) |
- ( PaletteFlagsGrayScale * is_same<typename color_space_type<View>::type, gray_t>::value );
- palette.Count = number_of_palette_colours;
-
- verify_result
- (
- DllExports::GdipInitializePalette
- (
- &palette,
- PaletteTypeOptimal,
- number_of_palette_colours,
- has_alpha<desired_format>::value,
- pBitmap_
- )
- );
-
- ensure_result
- (
- DllExports::GdipBitmapConvertFormat
- (
- pBitmap_,
- desired_format,
- DitherTypeErrorDiffusion,
- PaletteTypeOptimal,
- &palette,
- 50
- )
- );
- #endif // (GDIPVER >= 0x0110)
- }
-
- template <Gdiplus::PixelFormat desired_format>
- void pre_palettized_conversion( mpl::false_ /*not is_indexed*/ ) const {}
-
-private:
- friend class detail::gp_view_base;
-
- Gdiplus::GpBitmap * /*const*/ pBitmap_;
-};
-
-#if defined(BOOST_MSVC)
-# pragma warning( pop )
-#endif
-
-namespace detail
-{
-//------------------------------------------------------------------------------
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class gp_view_base
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class gp_view_base : noncopyable
-{
-public:
- ~gp_view_base()
- {
- detail::verify_result( Gdiplus::DllExports::GdipBitmapUnlockBits( &bitmap_, &bitmapData_ ) );
- }
-
-protected:
- gp_view_base( gp_image & bitmap, unsigned int const lock_mode, gp_image::roi const * const p_roi = 0 )
- :
- bitmap_( *bitmap.pBitmap_ )
- {
- std::memset( &bitmapData_, 0, sizeof( bitmapData_ ) );
-
- detail::ensure_result
- (
- Gdiplus::DllExports::GdipBitmapLockBits
- (
- &bitmap_,
- p_roi,
- lock_mode,
- bitmap.format(),
- &bitmapData_
- )
- );
- }
-
- template <typename Pixel>
- typename type_from_x_iterator<Pixel *>::view_t
- get_typed_view()
- {
- //todo assert correct type...
- interleaved_view<Pixel *>( bitmapData_.Width, bitmapData_.Height, bitmapData_.Scan0, bitmapData_.Stride );
- }
-
-private:
- Gdiplus::GpBitmap & bitmap_ ;
- Gdiplus::BitmapData bitmapData_;
-};
-
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class gp_view
-///
-////////////////////////////////////////////////////////////////////////////////
-
-template <typename Pixel>
-class gp_view
- :
- private gp_view_base,
- public type_from_x_iterator<Pixel *>::view_t
-{
-public:
- gp_view( gp_image & image, gp_image::roi const * const p_roi = 0 )
- :
- gp_view_base( image, ImageLockModeRead | ( is_const<Pixel>::value * ImageLockModeWrite ), p_roi ),
- type_from_x_iterator<Pixel *>::view_t( get_typed_view<Pixel>() )
- {}
-};
-
-
-//...mhmh...to be seen if necessary...
-//template <typename Pixel>
-//class gp_const_view
-// :
-// private gp_view_base,
-// public type_from_x_iterator<Pixel const *>::view_t
-//{
-//public:
-// gp_const_view( gp_image & image, gp_image::roi const * const p_roi = 0 )
-// :
-// gp_view_base( image, ImageLockModeRead ),
-// type_from_x_iterator<Pixel const *>::view_t( get_typed_view<Pixel const>() )
-// {}
-//};
-
-
-//...mhmh...to be implemented...
-//template <class Impl, class SupportedPixelFormats, class ROI>
-//inline
-//typename backend<Impl, SupportedPixelFormats, ROI>::view_t
-//view( backend<Impl, SupportedPixelFormats, ROI> & img );// { return img._view; }
-
-//------------------------------------------------------------------------------
-} // namespace detail
-//------------------------------------------------------------------------------
-} // namespace gil
-//------------------------------------------------------------------------------
-} // namespace boost
-//------------------------------------------------------------------------------
-#endif // gp_private_base_hpp

Deleted: sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/libjpeg_image.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85501)
+++ /dev/null 00:00:00 1970 (deleted)
@@ -1,1095 +0,0 @@
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \file libjpeg_image.hpp
-/// -----------------------
-///
-/// Copyright (c) Domagoj Saric 2010.
-///
-/// Use, modification and distribution is subject to the Boost Software License, Version 1.0.
-/// (See accompanying file LICENSE_1_0.txt or copy at
-/// http://www.boost.org/LICENSE_1_0.txt)
-///
-/// For more information, see http://www.boost.org
-///
-////////////////////////////////////////////////////////////////////////////////
-//------------------------------------------------------------------------------
-#pragma once
-#ifndef libjpeg_image_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
-#define libjpeg_image_hpp__7C5F6951_A00F_4E0D_9783_488A49B1CA2B
-//------------------------------------------------------------------------------
-#include "backend.hpp"
-#include "detail/io_error.hpp"
-#include "detail/libx_shared.hpp"
-#include "detail/shared.hpp"
-
-#include <boost/array.hpp>
-#include <boost/range/size.hpp>
-#include <boost/smart_ptr/scoped_array.hpp>
-
-#define JPEG_INTERNALS
-#include "jpeglib.h"
-#undef JPEG_INTERNALS
-
-#ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- #include <csetjmp>
-#endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-#include <cstdlib>
-#if defined(BOOST_MSVC)
- #pragma warning( push )
- #pragma warning( disable : 4996 ) // "The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name."
- #include "io.h"
- #include "sys/stat.h"
-#endif // MSVC
-#include "fcntl.h"
-//------------------------------------------------------------------------------
-namespace boost
-{
-//------------------------------------------------------------------------------
-namespace gil
-{
-//------------------------------------------------------------------------------
-namespace detail
-{
-//------------------------------------------------------------------------------
-
-BOOST_STATIC_ASSERT( BITS_IN_JSAMPLE == 8 );
-
-template <typename Pixel, bool isPlanar>
-struct gil_to_libjpeg_format : mpl::integral_c<J_COLOR_SPACE, JCS_UNKNOWN> {};
-
-template <> struct gil_to_libjpeg_format<rgb8_pixel_t , false> : mpl::integral_c<J_COLOR_SPACE, JCS_RGB > {};
-template <> struct gil_to_libjpeg_format<gray8_pixel_t, false> : mpl::integral_c<J_COLOR_SPACE, JCS_GRAYSCALE> {};
-template <> struct gil_to_libjpeg_format<cmyk8_pixel_t, false> : mpl::integral_c<J_COLOR_SPACE, JCS_CMYK > {};
-
-
-template <typename Pixel, bool IsPlanar>
-struct libjpeg_is_supported : mpl::bool_<gil_to_libjpeg_format<Pixel, IsPlanar>::value != JCS_UNKNOWN> {};
-
-
-typedef mpl::vector3
-<
- image<rgb8_pixel_t , false>,
- image<gray8_pixel_t, false>,
- image<cmyk8_pixel_t, false>
-> libjpeg_supported_pixel_formats;
-
-
-typedef generic_vertical_roi libjpeg_roi;
-
-
-struct libjpeg_object_wrapper_t
-{
- union aux_union
- {
- jpeg_compress_struct compressor_;
- jpeg_decompress_struct decompressor_;
- } libjpeg_object;
-};
-
-
-#if defined( BOOST_MSVC )
-# pragma warning( push )
-# pragma warning( disable : 4127 ) // "conditional expression is constant"
-#endif
-
-class libjpeg_base : private libjpeg_object_wrapper_t
-{
-protected:
- struct for_decompressor {};
- struct for_compressor {};
-
-protected:
- libjpeg_base( for_decompressor ) BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- initialize_error_handler();
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jpeg_create_decompress( &decompressor() );
- }
-
- libjpeg_base( for_compressor ) BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- initialize_error_handler();
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jpeg_create_compress( &compressor() );
- }
-
- ~libjpeg_base() { jpeg_destroy( &common() ); }
-
- void abort() const { jpeg_abort( &mutable_this().common() ); }
-
- jpeg_common_struct & common () { return *gil_reinterpret_cast<j_common_ptr>( &libjpeg_object.compressor_ ); }
- jpeg_common_struct const & common () const { return const_cast<libjpeg_base &>( *this ).common (); }
- jpeg_compress_struct & compressor () { return libjpeg_object.compressor_ ; }
- jpeg_compress_struct const & compressor () const { return const_cast<libjpeg_base &>( *this ).compressor (); }
- jpeg_decompress_struct & decompressor() { return libjpeg_object.decompressor_; }
- jpeg_decompress_struct const & decompressor() const { return const_cast<libjpeg_base &>( *this ).decompressor(); }
-
- libjpeg_base & mutable_this() const { return const_cast<libjpeg_base &>( *this ); }
-
- static libjpeg_base & base( jpeg_common_struct * const p_libjpeg_object )
- {
- BOOST_ASSERT( p_libjpeg_object );
- return *static_cast<libjpeg_base *>( gil_reinterpret_cast<libjpeg_object_wrapper_t *>( p_libjpeg_object ) );
- }
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jmp_buf & error_handler_target() const { return longjmp_target_; }
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- static void fatal_error_handler( j_common_ptr const p_cinfo )
- {
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- throw_jpeg_error();
- ignore_unused_variable_warning( p_cinfo );
- #else
- longjmp( base( p_cinfo ).error_handler_target(), 1 );
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- }
-
- static void throw_jpeg_error()
- {
- io_error( "LibJPEG error" );
- }
-
-private:
- void initialize_error_handler()
- {
- #ifndef NDEBUG
- jpeg_std_error( &jerr_ );
- #else
- std::memset( &jerr_, 0, sizeof( jerr_ ) );
- jerr_.emit_message = &emit_message ;
- jerr_.output_message = &output_message ;
- jerr_.format_message = &format_message ;
- jerr_.reset_error_mgr = &reset_error_mgr;
- #endif // NDEBUG
- jerr_.error_exit = &error_exit;
-
- common().err = &jerr_;
- }
-
- static void BF_CDECL error_exit( j_common_ptr const p_cinfo )
- {
- #ifndef NDEBUG
- p_cinfo->err->output_message( p_cinfo );
- #endif
-
- fatal_error_handler( p_cinfo );
- }
-
- static void BF_CDECL output_message( j_common_ptr /*p_cinfo*/ ) {}
- static void BF_CDECL emit_message ( j_common_ptr /*p_cinfo*/, int /*msg_level*/ ) {}
- static void BF_CDECL format_message( j_common_ptr /*p_cinfo*/, char * /*buffer*/ ) {}
-
- static void BF_CDECL reset_error_mgr( j_common_ptr const p_cinfo )
- {
- BOOST_ASSERT( p_cinfo->err->num_warnings == 0 );
- BOOST_ASSERT( p_cinfo->err->msg_code == 0 );
- ignore_unused_variable_warning( p_cinfo );
- }
-
-private:
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- mutable jmp_buf longjmp_target_;
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- jpeg_error_mgr jerr_ ;
-};
-
-
-struct decompression_setup_data_t
-{
- decompression_setup_data_t( J_COLOR_SPACE const format, JSAMPROW const buffer, libjpeg_roi::offset_t const offset )
- : format_( format ), buffer_( buffer ), offset_( offset ) {}
-
- J_COLOR_SPACE /*const*/ format_;
- JSAMPROW /*const*/ buffer_;
- libjpeg_roi::offset_t /*const*/ offset_;
-};
-
-struct view_data_t : decompression_setup_data_t
-{
- template <class View>
- /*explicit*/ view_data_t( View const & view, libjpeg_roi::offset_t const offset = 0 )
- :
- decompression_setup_data_t
- (
- gil_to_libjpeg_format<typename View::value_type, is_planar<View>::value>::value,
- backend_base::get_raw_data( view ),
- offset
- ),
- height_( view.height() ),
- width_ ( view.width () ),
- stride_( view.pixels().row_size() ),
- number_of_channels_( num_channels<View>::value )
- {
- BOOST_STATIC_ASSERT(( libjpeg_is_supported<typename View::value_type, is_planar<View>::value>::value ));
- }
-
- void set_format( J_COLOR_SPACE const format ) { format_ = format; }
-
- unsigned int /*const*/ height_;
- unsigned int /*const*/ width_ ;
- unsigned int /*const*/ stride_;
- unsigned int number_of_channels_;
-};
-
-
-class libjpeg_writer
- :
- private libjpeg_base,
- public configure_on_write_writer
-{
-protected:
- BOOST_STATIC_CONSTANT( bool, auto_closes_device = true );
-
-public:
- explicit libjpeg_writer( char const * const p_target_file_name )
- :
- libjpeg_base( for_compressor() )
- {
- setup_destination( p_target_file_name );
- }
-
- explicit libjpeg_writer( FILE & file )
- :
- libjpeg_base( for_compressor() )
- {
- setup_destination( file );
- }
-
- ~libjpeg_writer()
- {
- jpeg_finish_compress( &compressor() );
- }
-
- jpeg_compress_struct & lib_object() { return compressor(); }
- jpeg_compress_struct const & lib_object() const { return const_cast<libjpeg_writer &>( *this ).lib_object(); }
-
- void write_default( view_data_t const & view ) BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- setup_compression( view );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jpeg_set_defaults( &compressor() );
- //jpeg_set_quality( &compressor(), 100, false );
-
- do_write( view );
- }
-
- void write( view_data_t const & view )
- {
- setup_compression( view );
- do_write ( view );
- }
-
-private:
- void setup_compression( view_data_t const & view )
- {
- compressor().image_width = static_cast<JDIMENSION>( view.width_ );
- compressor().image_height = static_cast<JDIMENSION>( view.height_ );
- compressor().input_components = view.number_of_channels_;
- compressor().in_color_space = view.format_;
- }
-
- void do_write( view_data_t const & view ) BOOST_GIL_CAN_THROW
- {
- BOOST_ASSERT( view.format_ != JCS_UNKNOWN );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- jpeg_start_compress( &compressor(), false );
-
- JSAMPLE * p_row( view.buffer_ );
- JSAMPLE * const p_end( memunit_advanced( view.buffer_, view.height_ * view.stride_ ) );
- while ( p_row < p_end )
- {
- write_row( p_row );
- memunit_advance( p_row, view.stride_ );
- }
- }
-
- void write_row( JSAMPLE * p_row ) BOOST_GIL_CAN_THROW
- {
- BOOST_VERIFY( jpeg_write_scanlines( &compressor(), &p_row, 1 ) == 1 );
- }
-
- static libjpeg_writer & get_writer( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( static_cast<libjpeg_writer &>( base( gil_reinterpret_cast<j_common_ptr>( p_cinfo ) ) ) );
- BOOST_ASSERT( p_cinfo->dest == &writer.destination_manager_ );
- return writer;
- }
-
- void setup_destination()
- {
- destination_manager_.next_output_byte = 0;
- destination_manager_.free_in_buffer = 0;
-
- BOOST_ASSERT( compressor().dest == NULL );
- compressor().dest = &destination_manager_;
- }
-
- void setup_destination( FILE & file )
- {
- setup_destination();
-
- compressor().client_data = &file;
-
- destination_manager_.init_destination = &init_destination ;
- destination_manager_.empty_output_buffer = &empty_FILE_buffer ;
- destination_manager_.term_destination = &term_FILE_destination;
- }
-
- void setup_destination( char const * const p_file_name )
- {
- int const file_descriptor( /*std*/::open( p_file_name, BF_MSVC_SPECIFIC( O_BINARY | ) O_CREAT | O_WRONLY, S_IREAD | S_IWRITE ) );
- if ( file_descriptor < 0 )
- throw_jpeg_error();
-
- setup_destination();
-
- compressor().client_data = reinterpret_cast<void *>( file_descriptor );
-
- destination_manager_.init_destination = &init_destination ;
- destination_manager_.empty_output_buffer = &empty_fd_buffer ;
- destination_manager_.term_destination = &term_and_close_fd_destination;
- }
-
- static void BF_CDECL init_destination( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
-
- writer.destination_manager_.next_output_byte = writer.write_buffer_.begin();
- writer.destination_manager_.free_in_buffer = writer.write_buffer_.size ();
- }
-
- void write_FILE_bytes( std::size_t const number_of_bytes )
- {
- if
- (
- /*std*/::fwrite
- (
- write_buffer_.begin(),
- number_of_bytes,
- 1,
- static_cast<FILE *>( compressor().client_data )
- ) != 1
- )
- fatal_error_handler( &common() );
- }
-
- void write_fd_bytes( std::size_t const number_of_bytes )
- {
- if
- (
- /*std*/::write
- (
- reinterpret_cast<long>( compressor().client_data ),
- write_buffer_.begin(),
- number_of_bytes
- ) != static_cast<int>( number_of_bytes )
- )
- fatal_error_handler( &common() );
- }
-
- static boolean BF_CDECL empty_FILE_buffer( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
- writer.write_FILE_bytes( writer.write_buffer_.size() );
- init_destination( p_cinfo );
- return true;
- }
-
- static boolean BF_CDECL empty_fd_buffer( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
- writer.write_fd_bytes( writer.write_buffer_.size() );
- init_destination( p_cinfo );
- return true;
- }
-
- static void BF_CDECL term_FILE_destination( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
-
- std::size_t const remaining_bytes( writer.write_buffer_.size() - writer.destination_manager_.free_in_buffer );
-
- writer.write_FILE_bytes( remaining_bytes );
- }
-
- static void BF_CDECL term_fd_destination( j_compress_ptr const p_cinfo )
- {
- libjpeg_writer & writer( get_writer( p_cinfo ) );
-
- std::size_t const remaining_bytes( writer.write_buffer_.size() - writer.destination_manager_.free_in_buffer );
-
- writer.write_fd_bytes( remaining_bytes );
- }
-
- // Ensure that jpeg_finish_compress() is called so that this gets called...
- static void BF_CDECL term_and_close_FILE_destination( j_compress_ptr const p_cinfo )
- {
- term_FILE_destination( p_cinfo );
- BOOST_VERIFY( /*std*/::fclose( static_cast<FILE *>( get_writer( p_cinfo ).compressor().client_data ) ) == 0 );
- }
-
- static void BF_CDECL term_and_close_fd_destination( j_compress_ptr const p_cinfo )
- {
- term_fd_destination( p_cinfo );
- BOOST_VERIFY( /*std*/::close( reinterpret_cast<long>( get_writer( p_cinfo ).compressor().client_data ) ) == 0 );
- }
-
-private:
- jpeg_destination_mgr destination_manager_;
- array<unsigned char, 65536> write_buffer_ ;
-};
-//------------------------------------------------------------------------------
-} // namespace detail
-
-
-class libjpeg_image;
-
-template <>
-struct backend_traits<libjpeg_image>
-{
- typedef ::J_COLOR_SPACE format_t;
- typedef detail::libjpeg_supported_pixel_formats supported_pixel_formats_t;
- typedef detail::libjpeg_roi roi_t;
- typedef detail::view_data_t view_data_t;
-
- struct gil_to_native_format
- {
- template <typename Pixel, bool IsPlanar>
- struct apply : detail::gil_to_libjpeg_format<Pixel, IsPlanar> {};
- };
-
- template <typename Pixel, bool IsPlanar>
- struct is_supported : detail::libjpeg_is_supported<Pixel, IsPlanar> {};
-
- typedef mpl::map3
- <
- mpl::pair<memory_range_t , detail::seekable_input_memory_range_extender<libjpeg_image> >,
- mpl::pair<FILE , libjpeg_image >,
- mpl::pair<char const *, detail::input_c_str_for_mmap_extender <libjpeg_image> >
- > native_sources;
-
- typedef mpl::map2
- <
- mpl::pair<FILE , detail::libjpeg_writer>,
- mpl::pair<char const *, detail::libjpeg_writer>
- > native_sinks;
-
- typedef mpl::vector1_c<format_tag, jpeg> supported_image_formats;
-
- typedef view_data_t writer_view_data_t;
-
- BOOST_STATIC_CONSTANT( unsigned int, desired_alignment = sizeof( void * ) );
- BOOST_STATIC_CONSTANT( bool , builtin_conversion = true );
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libjpeg_image
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libjpeg_image
- :
- public detail::libjpeg_base,
- public detail::backend<libjpeg_image>
-{
-public:
- struct guard {};
-
-public:
- format_t format() const { return decompressor().jpeg_color_space; }
-
- format_t closest_gil_supported_format() const
- {
- format_t const current_format( format() );
- #ifdef _DEBUG
- switch ( current_format )
- {
- case JCS_RGB :
- case JCS_YCbCr :
- case JCS_CMYK :
- case JCS_YCCK :
- case JCS_GRAYSCALE:
- case JCS_UNKNOWN :
- break;
-
- default:
- BOOST_ASSERT( !"Unknown format code." );
- }
- #endif
-
- switch ( current_format )
- {
- case JCS_YCbCr: return JCS_RGB ;
- case JCS_YCCK : return JCS_CMYK ;
- default : return current_format;
- }
- }
-
- image_type_id current_image_format_id() const
- {
- return image_format_id( closest_gil_supported_format() );
- }
-
- static image_type_id image_format_id( format_t const closest_gil_supported_format )
- {
- switch ( closest_gil_supported_format )
- {
- case JCS_RGB : return 0;
- case JCS_GRAYSCALE: return 1;
- case JCS_CMYK : return 2;
- default:
- BOOST_ASSERT( closest_gil_supported_format == JCS_UNKNOWN );
- return unsupported_format;
- }
- }
-
- point2<std::ptrdiff_t> dimensions() const
- {
- // Implementation note:
- // A user might have setup output image scaling through the low-level
- // lib_object accessor.
- // (17.10.2010.) (Domagoj Saric)
- if ( dirty_output_dimensions_ )
- {
- jpeg_calc_output_dimensions( &const_cast<libjpeg_image &>( *this ).lib_object() );
- dirty_output_dimensions_ = false;
- }
- return point2<std::ptrdiff_t>( decompressor().output_width, decompressor().output_height );
- }
-
-public: // Low-level (row, strip, tile) access
- void read_row( sequential_row_read_state, unsigned char * const p_row_storage ) const
- {
- read_scanline( p_row_storage );
- }
-
- jpeg_decompress_struct & lib_object() { dirty_output_dimensions_ = true; return decompressor(); }
- jpeg_decompress_struct const & lib_object() const { return decompressor(); }
-
-public: /// \ingroup Construction
- explicit libjpeg_image( memory_range_t & memory_chunk )
- :
- libjpeg_base( for_decompressor() )
- {
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- setup_source( memory_chunk );
-
- read_header();
- }
-
- explicit libjpeg_image( FILE & file )
- :
- libjpeg_base( for_decompressor() )
- {
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- setup_source( file );
-
- read_header();
- }
-
-private: // Private interface for the base backend<> class.
- // Implementation note:
- // MSVC 10 accepts friend base_t and friend class base_t, Clang 2.8
- // accepts friend class base_t, Apple Clang 1.6 and GCC (4.2 and 4.6) accept
- // neither.
- // (13.01.2011.) (Domagoj Saric)
- friend class detail::backend<libjpeg_image>;
-
- void raw_convert_to_prepared_view( detail::view_data_t const & view_data ) const BOOST_GIL_CAN_THROW
- {
- setup_decompression( view_data );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- JSAMPROW scanlines[ 4 ] =
- {
- view_data.buffer_,
- scanlines[ 0 ] + view_data.stride_,
- scanlines[ 1 ] + view_data.stride_,
- scanlines[ 2 ] + view_data.stride_
- };
-
- BOOST_ASSERT( boost::size( scanlines ) >= decompressor().rec_outbuf_height );
- unsigned int scanlines_to_read( view_data.height_ );
- for ( ; ; )
- {
- BOOST_ASSERT( scanlines_to_read <= ( decompressor().output_height - decompressor().output_scanline ) );
- unsigned int const lines_read
- (
- read_scanlines
- (
- scanlines,
- std::min<std::size_t>( boost::size( scanlines ), scanlines_to_read )
- )
- );
- scanlines_to_read -= lines_read;
- if ( !scanlines_to_read )
- return;
-
- scanlines[ 0 ] = scanlines[ lines_read - 1 ] + view_data.stride_;
- scanlines[ 1 ] = scanlines[ 0 ] + view_data.stride_;
- scanlines[ 2 ] = scanlines[ 1 ] + view_data.stride_;
- scanlines[ 3 ] = scanlines[ 2 ] + view_data.stride_;
- }
- }
-
-
- template <class MyView, class TargetView, class Converter>
- void generic_convert_to_prepared_view( TargetView const & view, Converter const & converter ) const
- {
- using namespace detail;
-
- typedef typename MyView::value_type pixel_t;
- std::size_t const scanline_length ( decompressor().image_width * decompressor().num_components );
- scoped_array<JSAMPLE> const p_scanline_buffer( new JSAMPLE[ scanline_length ] );
- JSAMPROW scanline ( p_scanline_buffer.get() );
- JSAMPROW const scanline_end( scanline + scanline_length );
-
- format_t const my_format( gil_to_libjpeg_format<typename MyView::value_type, is_planar<MyView>::value>::value );
- BOOST_ASSERT( this->closest_gil_supported_format() == my_format );
- setup_decompression
- (
- decompression_setup_data_t
- (
- my_format,
- scanline,
- detail::get_offset<offset_t>( view )
- )
- );
-
- BOOST_ASSERT( decompressor().output_width == decompressor().image_width );
- BOOST_ASSERT( decompressor().output_components == decompressor().num_components );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- unsigned int const scanlines_to_read( detail::original_view( view ).dimensions().y );
- for ( unsigned int scanline_index( 0 ); scanline_index < scanlines_to_read; ++scanline_index )
- {
- read_scanline( scanline );
-
- typedef typename get_original_view_t<TargetView>::type::x_iterator target_x_iterator;
-
- pixel_t const * p_source_pixel( gil_reinterpret_cast_c<pixel_t const *>( scanline ) );
- target_x_iterator p_target_pixel( original_view( view ).row_begin( scanline_index ) );
- while ( p_source_pixel < gil_reinterpret_cast_c<pixel_t const *>( scanline_end ) )
- {
- converter( *p_source_pixel, *p_target_pixel );
- ++p_source_pixel;
- ++p_target_pixel;
- }
- }
- }
-
-
- void raw_copy_to_prepared_view( detail::view_data_t const & view_data ) const
- {
- BOOST_ASSERT( view_data.width_ == static_cast<unsigned int>( dimensions().x ) );
- BOOST_ASSERT( view_data.height_ == static_cast<unsigned int>( dimensions().y ) );
- BOOST_ASSERT( view_data.format_ == closest_gil_supported_format() );
- raw_convert_to_prepared_view( view_data );
- }
-
-
- static unsigned int cached_format_size( format_t const format )
- {
- switch ( format )
- {
- case JCS_RGB:
- case JCS_YCbCr:
- return 3;
- case JCS_CMYK:
- case JCS_YCCK:
- return 4;
- case JCS_GRAYSCALE:
- return 1;
-
- default:
- BOOST_ASSERT( !"Invalid or unknown format specified." );
- BF_UNREACHABLE_CODE
- return 0;
- }
- }
-
-private:
- void setup_decompression( detail::decompression_setup_data_t const & view_data ) const BOOST_GIL_CAN_THROW
- {
- unsigned int const state ( decompressor().global_state );
- unsigned int rows_to_skip( view_data.offset_ );
- if
- (
- ( state != DSTATE_SCANNING ) ||
- ( decompressor().out_color_space != view_data.format_ ) ||
- ( decompressor().output_scanline > static_cast<JDIMENSION>( view_data.offset_ ) )
- )
- {
- BOOST_ASSERT
- (
- ( state == DSTATE_READY ) ||
- ( state == DSTATE_SCANNING )
- );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( libjpeg_base::error_handler_target() ) )
- libjpeg_base::throw_jpeg_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- if ( state == DSTATE_SCANNING )
- abort();
-
- mutable_this().decompressor().out_color_space = view_data.format_;
- BOOST_VERIFY( jpeg_start_decompress( &mutable_this().decompressor() ) );
- BOOST_ASSERT( decompressor().output_scanline == 0 );
- }
- else
- rows_to_skip -= decompressor().output_scanline;
-
- if ( rows_to_skip )
- skip_rows( rows_to_skip, view_data.buffer_ );
- BOOST_ASSERT( decompressor().output_scanline == static_cast<JDIMENSION>( view_data.offset_ ) );
- }
-
- void skip_rows( unsigned int const number_of_rows_to_skip, JSAMPROW /*const*/ dummy_scanline_buffer ) const
- {
- BOOST_ASSERT( decompressor().raw_data_out == false );
- BOOST_ASSERT( decompressor().global_state == DSTATE_SCANNING );
- mutable_this().decompressor().raw_data_out = true;
- mutable_this().decompressor().global_state = DSTATE_RAW_OK;
-
- unsigned int const max_number_of_components( 4 ); // CMYK
- unsigned int const max_sampling_factor ( 2 ); // Documentation
- unsigned int const mcu_row_size ( max_sampling_factor * DCTSIZE ); // Documentation
- BOOST_ASSERT( decompressor().num_components <= max_number_of_components );
- BOOST_ASSERT( decompressor().max_v_samp_factor <= max_sampling_factor );
-
- JSAMPROW dummy_component_2d_array[ mcu_row_size ];
- JSAMPARRAY dummy_scan_lines [ max_number_of_components ];
-
- std::fill( begin( dummy_component_2d_array ), end( dummy_component_2d_array ), dummy_scanline_buffer );
- std::fill( begin( dummy_scan_lines ), end( dummy_scan_lines ), &dummy_component_2d_array[ 0 ] );
-
- unsigned int number_of_rows_to_skip_using_scanlines
- (
- number_of_rows_to_skip % mcu_row_size
- );
- unsigned int number_of_rows_to_skip_using_raw
- (
- number_of_rows_to_skip - number_of_rows_to_skip_using_scanlines
- );
-
- while ( number_of_rows_to_skip_using_raw )
- {
- unsigned int const lines_to_read( boost::size( dummy_component_2d_array ) );
- read_raw_data( dummy_scan_lines, lines_to_read );
- number_of_rows_to_skip_using_raw -= lines_to_read;
- }
-
- mutable_this().decompressor().raw_data_out = false;
- mutable_this().decompressor().global_state = DSTATE_SCANNING;
- while ( number_of_rows_to_skip_using_scanlines-- )
- {
- read_scanline( dummy_scanline_buffer );
- }
- }
-
- unsigned int read_scanlines( JSAMPROW scanlines[], unsigned int const scanlines_to_read ) const BOOST_GIL_CAN_THROW
- {
- return jpeg_read_scanlines
- (
- &mutable_this().decompressor(),
- scanlines,
- scanlines_to_read
- );
- }
-
- void read_scanline( JSAMPROW scanline ) const BOOST_GIL_CAN_THROW
- {
- BOOST_VERIFY
- (
- read_scanlines( &scanline, 1 ) == 1
- );
- }
-
- void read_raw_data( JSAMPARRAY scanlines[], unsigned int const scanlines_to_read ) const BOOST_GIL_CAN_THROW
- {
- BOOST_VERIFY
- (
- jpeg_read_raw_data
- (
- &mutable_this().decompressor(),
- scanlines,
- scanlines_to_read
- ) == scanlines_to_read
- );
- }
-
- libjpeg_image & mutable_this() const { return static_cast<libjpeg_image &>( libjpeg_base::mutable_this() ); }
-
-private:
- friend class libjpeg_view_base;
-
- void read_header()
- {
- BOOST_VERIFY( jpeg_read_header( &decompressor(), true ) == JPEG_HEADER_OK );
-
- // Implementation note:
- // To enable users to setup output scaling we use the output
- // dimensions to report the image dimensions in the dimensions() getter
- // so we have to manually initialize them here.
- // (17.10.2010.) (Domagoj Saric)
- BOOST_ASSERT( decompressor().output_width == 0 );
- BOOST_ASSERT( decompressor().output_height == 0 );
-
- decompressor().output_width = decompressor().image_width ;
- decompressor().output_height = decompressor().image_height;
-
- dirty_output_dimensions_ = false;
-
- detail::io_error_if( decompressor().data_precision != BITS_IN_JSAMPLE, "Unsupported image file data precision." );
- }
-
- // Unextracted "libjpeg_reader" interface.
- void setup_source()
- {
- BOOST_ASSERT( decompressor().src == NULL );
- decompressor().src = &source_manager_;
- }
-
- void setup_source( memory_range_t & memory_chunk )
- {
- setup_source();
-
- decompressor().client_data = &memory_chunk;
-
- source_manager_.next_input_byte = memory_chunk.begin();
- source_manager_.bytes_in_buffer = memory_chunk.size ();
-
- source_manager_.init_source = &init_memory_chunk_source;
- source_manager_.fill_input_buffer = &fill_memory_chunk_buffer;
- source_manager_.skip_input_data = &skip_memory_chunk_data ;
- source_manager_.resync_to_restart = &jpeg_resync_to_restart ;
- source_manager_.term_source = &term_memory_chunk_source;
- }
-
- void setup_source( FILE & file )
- {
- setup_source();
-
- decompressor().client_data = &file;
-
- source_manager_.next_input_byte = read_buffer_.begin();
- source_manager_.bytes_in_buffer = 0;
-
- source_manager_.init_source = &init_FILE_source ;
- source_manager_.fill_input_buffer = &fill_FILE_buffer ;
- source_manager_.skip_input_data = &skip_FILE_data ;
- source_manager_.resync_to_restart = &jpeg_resync_to_restart;
- source_manager_.term_source = &term_FILE_source ;
- }
-
-
- static void BF_CDECL init_FILE_source( j_decompress_ptr const p_cinfo )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- reader.source_manager_.next_input_byte = reader.read_buffer_.begin();
- reader.source_manager_.bytes_in_buffer = 0;
- }
-
- static boolean BF_CDECL fill_FILE_buffer( j_decompress_ptr const p_cinfo )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- std::size_t bytes_read
- (
- /*std*/::fread
- (
- reader.read_buffer_.begin(),
- 1,
- reader.read_buffer_.size(),
- static_cast<FILE *>( reader.decompressor().client_data )
- )
- );
-
- if ( bytes_read == 0 )
- {
- // Insert a fake EOI marker (see the comment for the default library
- // implementation).
- reader.read_buffer_[ 0 ] = 0xFF;
- reader.read_buffer_[ 1 ] = JPEG_EOI;
-
- bytes_read = 2;
- }
-
- reader.source_manager_.next_input_byte = reader.read_buffer_.begin();
- reader.source_manager_.bytes_in_buffer = bytes_read;
-
- return true;
- }
-
- static void BF_CDECL skip_FILE_data( j_decompress_ptr const p_cinfo, long num_bytes )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- if ( static_cast<std::size_t>( num_bytes ) <= reader.source_manager_.bytes_in_buffer )
- {
- reader.source_manager_.next_input_byte += num_bytes;
- reader.source_manager_.bytes_in_buffer -= num_bytes;
- }
- else
- {
- num_bytes -= reader.source_manager_.bytes_in_buffer;
- /*std*/::fseek( static_cast<FILE *>( reader.decompressor().client_data ), num_bytes, SEEK_CUR ); //...failure?
- reader.source_manager_.next_input_byte = 0;
- reader.source_manager_.bytes_in_buffer = 0;
- }
- }
-
- static void BF_CDECL term_FILE_source( j_decompress_ptr /*p_cinfo*/ )
- {
- }
-
- // Ensure that jpeg_finish_decompress() is called so that this gets called...
- static void BF_CDECL term_and_close_FILE_source( j_decompress_ptr const p_cinfo )
- {
- term_FILE_source( p_cinfo );
- BOOST_VERIFY( /*std*/::fclose( static_cast<FILE *>( get_reader( p_cinfo ).decompressor().client_data ) ) == 0 );
- }
-
- static void BF_CDECL init_memory_chunk_source( j_decompress_ptr /*p_cinfo*/ )
- {
- }
-
- static boolean BF_CDECL fill_memory_chunk_buffer( j_decompress_ptr /*p_cinfo*/ )
- {
- BF_UNREACHABLE_CODE
- return true;
- }
-
- static void BF_CDECL skip_memory_chunk_data( j_decompress_ptr const p_cinfo, long num_bytes )
- {
- libjpeg_image & reader( get_reader( p_cinfo ) );
-
- BOOST_ASSERT( static_cast<std::size_t>( num_bytes ) <= reader.source_manager_.bytes_in_buffer );
- reader.source_manager_.next_input_byte += num_bytes;
- reader.source_manager_.bytes_in_buffer -= num_bytes;
- }
-
- static void BF_CDECL term_memory_chunk_source( j_decompress_ptr /*p_cinfo*/ )
- {
- }
-
- static libjpeg_image & get_reader( j_decompress_ptr const p_cinfo )
- {
- libjpeg_image & reader( static_cast<libjpeg_image &>( base( gil_reinterpret_cast<j_common_ptr>( p_cinfo ) ) ) );
- BOOST_ASSERT( p_cinfo->src == &reader.source_manager_ );
- return reader;
- }
-
-private:
- jpeg_source_mgr source_manager_ ;
- mutable bool dirty_output_dimensions_;
- array<JOCTET, 4096> read_buffer_ ;//...zzz...extract to a wrapper...not needed for in memory sources...
-};
-
-#if defined(BOOST_MSVC)
-# pragma warning( pop )
-#endif
-
-
-//...zzz...incomplete...
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libjpeg_view_base
-///
-////////////////////////////////////////////////////////////////////////////////
-
-//class libjpeg_view_base : noncopyable
-//{
-//public:
-// ~libjpeg_view_base()
-// {
-// }
-//
-//protected:
-// libjpeg_view_base( libjpeg_image & bitmap, unsigned int const lock_mode, libjpeg_image::roi const * const p_roi = 0 )
-// {
-// }
-//
-// template <typename Pixel>
-// typename type_from_x_iterator<Pixel *>::view_t
-// get_typed_view()
-// {
-// //todo assert correct type...
-// interleaved_view<Pixel *>( bitmapData_.Width, bitmapData_.Height, bitmapData_.Scan0, bitmapData_.Stride );
-// }
-//
-//private:
-//};
-
-
-//...zzz...incomplete...
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libjpeg_view
-///
-////////////////////////////////////////////////////////////////////////////////
-
-//template <typename Pixel>
-//class libjpeg_view
-// :
-// private libjpeg_view_base,
-// public type_from_x_iterator<Pixel *>::view_t
-//{
-//public:
-// libjpeg_view( libjpeg_image & image, libjpeg_image::roi const * const p_roi = 0 )
-// :
-// libjpeg_view_base( image, ImageLockModeRead | ( is_const<Pixel>::value * ImageLockModeWrite ), p_roi ),
-// type_from_x_iterator<Pixel *>::view_t( get_typed_view<Pixel>() )
-// {}
-//};
-
-//------------------------------------------------------------------------------
-} // namespace gil
-//------------------------------------------------------------------------------
-} // namespace boost
-//------------------------------------------------------------------------------
-
-#if defined(BOOST_MSVC)
- #pragma warning( pop )
-#endif // MSVC
-
-#endif // libjpeg_image_hpp

Deleted: sandbox/gil/boost/gil/extension/io2/libpng_image.hpp
==============================================================================
--- sandbox/gil/boost/gil/extension/io2/libpng_image.hpp 2013-08-28 13:55:45 EDT (Wed, 28 Aug 2013) (r85501)
+++ /dev/null 00:00:00 1970 (deleted)
@@ -1,759 +0,0 @@
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \file libpng_image.hpp
-/// ----------------------
-///
-/// Copyright (c) Domagoj Saric 2010.
-///
-/// Use, modification and distribution is subject to the Boost Software License, Version 1.0.
-/// (See accompanying file LICENSE_1_0.txt or copy at
-/// http://www.boost.org/LICENSE_1_0.txt)
-///
-/// For more information, see http://www.boost.org
-///
-////////////////////////////////////////////////////////////////////////////////
-//------------------------------------------------------------------------------
-#ifndef libpng_image_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
-#define libpng_image_hpp__9E0C99E6_EAE8_4D71_844B_93518FFCB5CE
-#pragma once
-//------------------------------------------------------------------------------
-#include "backend.hpp"
-#include "detail/platform_specifics.hpp"
-#include "detail/io_error.hpp"
-#include "detail/libx_shared.hpp"
-#include "detail/shared.hpp"
-
-#include "boost/scoped_array.hpp"
-
-#include "png.h"
-
-#ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- #include <csetjmp>
-#endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-#include <cstdlib>
-//------------------------------------------------------------------------------
-namespace boost
-{
-//------------------------------------------------------------------------------
-namespace gil
-{
-//------------------------------------------------------------------------------
-namespace detail
-{
-//------------------------------------------------------------------------------
-
-template <typename Pixel, bool isPlanar>
-struct gil_to_libpng_format : mpl::integral_c<unsigned int, backend_base::unsupported_format> {};
-
-/// \todo Add bgr-layout formats as LibPNG actually supports them through
-/// png_set_bgr().
-/// (19.09.2010.) (Domagoj Saric)
-template <> struct gil_to_libpng_format<rgb8_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB | ( 8 << 16 )> {};
-template <> struct gil_to_libpng_format<rgba8_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB_ALPHA | ( 8 << 16 )> {};
-template <> struct gil_to_libpng_format<gray8_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_GRAY | ( 8 << 16 )> {};
-template <> struct gil_to_libpng_format<rgb16_pixel_t , false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB | ( 16 << 16 )> {};
-template <> struct gil_to_libpng_format<rgba16_pixel_t, false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_RGB_ALPHA | ( 16 << 16 )> {};
-template <> struct gil_to_libpng_format<gray16_pixel_t, false> : mpl::integral_c<unsigned int, PNG_COLOR_TYPE_GRAY | ( 16 << 16 )> {};
-
-
-template <typename Pixel, bool IsPlanar>
-struct libpng_is_supported : mpl::bool_<gil_to_libpng_format<Pixel, IsPlanar>::value != -1> {};
-
-template <typename View>
-struct libpng_is_view_supported : libpng_is_supported<typename View::value_type, is_planar<View>::value> {};
-
-
-typedef mpl::vector6
-<
- image<rgb8_pixel_t , false>,
- image<rgba8_pixel_t , false>,
- image<gray8_pixel_t , false>,
- image<rgb16_pixel_t , false>,
- image<rgba16_pixel_t, false>,
- image<gray16_pixel_t, false>
-> libpng_supported_pixel_formats;
-
-
-typedef generic_vertical_roi libpng_roi;
-
-
-struct libpng_view_data_t
-{
- typedef unsigned int format_t;
-
- template <class View>
- /*explicit*/ libpng_view_data_t( View const & view, libpng_roi::offset_t const offset = 0 )
- :
- format_( gil_to_libpng_format<typename View::value_type, is_planar<View>::value>::value ),
- buffer_( backend_base::get_raw_data( view ) ),
- offset_( offset ),
- height_( view.height() ),
- width_ ( view.width () ),
- stride_( view.pixels().row_size() ),
- number_of_channels_( num_channels<View>::value )
- {
- BOOST_STATIC_ASSERT( libpng_is_view_supported<View>::value );
- }
-
- void set_format( unsigned int const format ) { format_ = format; }
-
- format_t /*const*/ format_;
- png_byte * const buffer_;
- libpng_roi::offset_t const offset_;
-
- unsigned int const height_;
- unsigned int const width_ ;
- unsigned int const stride_;
- unsigned int const number_of_channels_;
-};
-
-
-inline void throw_libpng_error()
-{
- io_error( "LibPNG failure" );
-}
-
-
-inline void PNGAPI png_warning_function( png_structp /*png_ptr*/, png_const_charp const message )
-{
- #ifdef _DEBUG
- std::puts( message );
- #else
- ignore_unused_variable_warning( message );
- #endif // _DEBUG
-}
-
-
-inline void PNGAPI png_error_function( png_structp const png_ptr, png_const_charp const error_message )
-{
- png_warning_function( png_ptr, error_message );
-
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- throw_libpng_error();
- boost::ignore_unused_variable_warning( png_ptr );
- #else
- longjmp( png_ptr->jmpbuf, 1 );
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-}
-
-
-//...zzz...this will blow-up at link-time when included in multiple .cpps unless it gets moved into a separate module...
-#ifdef PNG_NO_ERROR_TEXT
- extern "C" void PNGAPI png_error( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_error_function( png_ptr, error_message );
- }
-
- extern "C" void PNGAPI png_chunk_error( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_error( png_ptr, error_message );
- }
-#endif // PNG_NO_ERROR_TEXT
-
-#ifdef PNG_NO_WARNINGS
- extern "C" void PNGAPI png_warning( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_warning_function( png_ptr, error_message );
- }
-
- extern "C" void PNGAPI png_chunk_warning( png_structp const png_ptr, png_const_charp const error_message )
- {
- png_warning_function( png_ptr, error_message );
- }
-#endif // PNG_NO_WARNINGS
-
-
-class lib_object_t : noncopyable
-{
-public:
- png_struct & png_object() const
- {
- BOOST_ASSERT( p_png_ && "Call this only after ensuring creation success!" );
- png_struct & png( *p_png_ );
- BF_ASSUME( &png != 0 );
- return png;
- }
-
- png_info & info_object() const
- {
- BOOST_ASSERT( p_png_ && "Call this only after ensuring creation success!" );
- png_info & info( *p_info_ );
- BF_ASSUME( &info != 0 );
- return info;
- }
-
-protected:
- lib_object_t( png_struct * const p_png, png_info * const p_info )
- :
- p_png_ ( p_png ),
- p_info_( p_info )
- {}
-
-#ifndef NDEBUG
- ~lib_object_t()
- {
- BOOST_ASSERT( !p_png_ && !p_info_ && "The concrete class must do the cleanup!" );
- }
-#endif // NDEBUG
-
- //...zzz...forced by LibPNG into either duplication or this anti-pattern...
- bool is_valid() const { return p_png_ && p_info_; }
-
- png_struct * & png_object_for_destruction () { return p_png_ ; }
- png_info * & info_object_for_destruction() { return p_info_; }
-
-private:
- png_struct * __restrict p_png_ ;
- png_info * __restrict p_info_;
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_base
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_base : public lib_object_t
-{
-public:
- typedef lib_object_t lib_object_t;
-
- lib_object_t & lib_object() { return *this; }
-
-protected:
- libpng_base( png_struct * const p_png )
- :
- lib_object_t( p_png, ::png_create_info_struct( p_png ) )
- {}
-
- //...zzz...forced by LibPNG into either duplication or this anti-pattern...
- bool successful_creation() const { return is_valid(); }
-
- static std::size_t format_bit_depth( libpng_view_data_t::format_t const format )
- {
- return ( format >> 16 ) & 0xFF;
- }
-
- static std::size_t format_colour_type( libpng_view_data_t::format_t const format )
- {
- return format & 0xFF;
- }
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- jmp_buf & error_handler_target() const { return png_object().jmpbuf; }
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_writer
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_writer
- :
- public libpng_base,
- public configure_on_write_writer
-{
-public:
- void write_default( libpng_view_data_t const & view )
- {
- ::png_set_IHDR
- (
- &png_object () ,
- &info_object() ,
- view.width_ ,
- view.height_ ,
- format_bit_depth ( view.format_ ),
- format_colour_type( view.format_ ),
- PNG_INTERLACE_NONE ,
- PNG_COMPRESSION_TYPE_DEFAULT ,
- PNG_FILTER_TYPE_DEFAULT
- );
-
- //::png_set_invert_alpha( &png_object() );
-
- write( view );
- }
-
- void write( libpng_view_data_t const & view ) BOOST_GIL_CAN_THROW
- {
- BOOST_ASSERT( view.format_ != JCS_UNKNOWN );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- detail::throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- if ( little_endian() )
- ::png_set_swap( &png_object() );
-
- ::png_write_info( &png_object(), &info_object() );
-
- png_byte * p_row( view.buffer_ );
- png_byte * const p_end( memunit_advanced( view.buffer_, view.height_ * view.stride_ ) );
- while ( p_row < p_end )
- {
- ::png_write_row( &png_object(), p_row );
- memunit_advance( p_row, view.stride_ );
- }
-
- ::png_write_end( &png_object(), 0 );
- }
-
-protected:
- libpng_writer( void * const p_target_object, png_rw_ptr const write_data_fn, png_flush_ptr const output_flush_fn )
- :
- libpng_base( ::png_create_write_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
- {
- if ( !successful_creation() )
- cleanup_and_throw_libpng_error();
-
- setup_destination( p_target_object, write_data_fn, output_flush_fn );
- }
-
- ~libpng_writer()
- {
- destroy_write_struct();
- }
-
-private:
- void destroy_write_struct() { ::png_destroy_write_struct( &png_object_for_destruction(), &info_object_for_destruction() ); }
-
- void cleanup_and_throw_libpng_error()
- {
- destroy_write_struct();
- detail::throw_libpng_error();
- }
-
- void setup_destination( void * const p_target_object, png_rw_ptr const write_data_fn, png_flush_ptr const output_flush_fn )
- {
- ::png_set_write_fn( &png_object(), p_target_object, write_data_fn, output_flush_fn );
- }
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_writer_FILE
-///
-/// \brief
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_writer_FILE : public libpng_writer
-{
-public:
- libpng_writer_FILE( FILE & file )
- :
- libpng_writer( &file, &png_write_data, &png_flush_data )
- {}
-
-private:
- static void PNGAPI png_write_data( png_structp const png_ptr, png_bytep const data, png_size_t const length )
- {
- BOOST_ASSERT( png_ptr );
-
- png_size_t const written_size
- (
- static_cast<png_size_t>( std::fwrite( data, 1, length, static_cast<FILE *>( png_ptr->io_ptr ) ) )
- );
-
- if ( written_size != length )
- png_error_function( png_ptr, "Write Error" );
- }
-
- static void PNGAPI png_flush_data( png_structp const png_ptr )
- {
- BOOST_ASSERT( png_ptr );
-
- std::fflush( static_cast<FILE *>( png_ptr->io_ptr ) );
- }
-};
-
-//------------------------------------------------------------------------------
-} // namespace detail
-
-
-class libpng_image;
-
-template <>
-struct backend_traits<libpng_image>
-{
- typedef detail::libpng_supported_pixel_formats supported_pixel_formats_t;
- typedef detail::libpng_roi roi_t;
- typedef detail::libpng_view_data_t view_data_t;
- typedef detail::libpng_view_data_t::format_t format_t;
-
- struct gil_to_native_format
- {
- template <typename Pixel, bool IsPlanar>
- struct apply : detail::gil_to_libpng_format<Pixel, IsPlanar> {};
- };
-
- template <typename Pixel, bool IsPlanar>
- struct is_supported : detail::libpng_is_supported<Pixel, IsPlanar> {};
-
- typedef mpl::map3
- <
- mpl::pair<memory_range_t , detail::seekable_input_memory_range_extender<libpng_image> >,
- mpl::pair<FILE , libpng_image >,
- mpl::pair<char const *, detail::input_c_str_for_mmap_extender <libpng_image> >
- > native_sources;
-
- typedef mpl::map2
- <
- mpl::pair<FILE , detail::libpng_writer_FILE >,
- mpl::pair<char const *, detail::output_c_str_for_c_file_extender<detail::libpng_writer_FILE> >
- > native_sinks;
-
- typedef mpl::vector1_c<format_tag, png> supported_image_formats;
-
- typedef view_data_t writer_view_data_t;
-
- BOOST_STATIC_CONSTANT( unsigned int, desired_alignment = sizeof( void * ) );
- BOOST_STATIC_CONSTANT( bool , builtin_conversion = true );
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
-///
-/// \class libpng_image
-///
-/// \brief
-///
-////////////////////////////////////////////////////////////////////////////////
-
-class libpng_image
- :
- public detail::backend<libpng_image>,
- private detail::libpng_base
-{
-public:
- struct guard {};
-
-public:
- format_t format() const
- {
- return ::png_get_color_type( &png_object(), &info_object() ) | ( bit_depth() << 16 );
- }
-
- format_t closest_gil_supported_format() const
- {
- format_t const current_format( format() );
-
- switch ( current_format & 0xFF )
- {
- default: return current_format;
-
- case PNG_COLOR_TYPE_PALETTE : return PNG_COLOR_TYPE_RGB | ( 8 << 16 ); // 8-bit RGB
- case PNG_COLOR_TYPE_GRAY_ALPHA: return PNG_COLOR_TYPE_RGBA | ( ( ( current_format >> 16 ) & 0xFF ) << 16 ); // (bits of current_format) RGBA
- }
- }
-
- image_type_id current_image_format_id() const
- {
- return image_format_id( closest_gil_supported_format() );
- }
-
- static image_type_id image_format_id( format_t const closest_gil_supported_format )
- {
- switch ( closest_gil_supported_format )
- {
- case PNG_COLOR_TYPE_RGB | ( 8 << 16 ): return 0;
- case PNG_COLOR_TYPE_RGB_ALPHA | ( 8 << 16 ): return 1;
- case PNG_COLOR_TYPE_GRAY | ( 8 << 16 ): return 2;
- case PNG_COLOR_TYPE_RGB | ( 16 << 16 ): return 3;
- case PNG_COLOR_TYPE_RGB_ALPHA | ( 16 << 16 ): return 4;
- case PNG_COLOR_TYPE_GRAY | ( 16 << 16 ): return 5;
-
- default:
- return unsupported_format;
- }
- }
-
- std::size_t pixel_size() const
- {
- return number_of_channels() * bit_depth() / 8;
- }
-
-public: /// \ingroup Construction
- explicit libpng_image( FILE & file )
- :
- libpng_base( ::png_create_read_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
- {
- if ( !successful_creation() )
- cleanup_and_throw_libpng_error();
-
- #ifdef PNG_NO_STDIO
- ::png_set_read_fn( &png_object(), &file, &png_FILE_read_data );
- #else
- ::png_init_io( &png_object(), &file );
- #endif
-
- init();
- }
-
- explicit libpng_image( memory_range_t & in_memory_image )
- :
- libpng_base( ::png_create_read_struct_2( PNG_LIBPNG_VER_STRING, NULL, &detail::png_error_function, &detail::png_warning_function, NULL, NULL, NULL ) )
- {
- if ( !successful_creation() )
- cleanup_and_throw_libpng_error();
- //png_rw_ptr
- ::png_set_read_fn( &png_object(), &in_memory_image, &png_memory_chunk_read_data );
-
- init();
- }
-
- ~libpng_image()
- {
- destroy_read_struct();
- }
-
-public:
- point2<std::ptrdiff_t> dimensions() const
- {
- return point2<std::ptrdiff_t>
- (
- ::png_get_image_width ( &png_object(), &info_object() ),
- ::png_get_image_height( &png_object(), &info_object() )
- );
- }
-
-public: // Low-level (row, strip, tile) access
- void read_row( sequential_row_read_state, unsigned char * const p_row_storage ) const
- {
- read_row( p_row_storage );
- }
-
- png_struct & lib_object() const { return png_object(); }
-
-private: // Private backend_base interface.
- // Implementation note:
- // MSVC 10 accepts friend base_t and friend class base_t, Clang 2.8
- // accepts friend class base_t, Apple Clang 1.6 and GCC (4.2 and 4.6) accept
- // neither.
- // (13.01.2011.) (Domagoj Saric)
- friend class detail::backend<libpng_image>;
-
- template <class MyView, class TargetView, class Converter>
- void generic_convert_to_prepared_view( TargetView const & view, Converter const & converter ) const BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- using namespace detail;
-
- std::size_t const row_length ( ::png_get_rowbytes( &png_object(), &info_object() ) );
- scoped_array<png_byte> const p_row_buffer( new png_byte[ row_length ] );
-
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- detail::throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- unsigned int number_of_passes( ::png_set_interlace_handling( &png_object() ) );
- BF_ASSUME( ( number_of_passes == 1 ) || ( number_of_passes == 7 ) );
- BOOST_ASSERT( ( number_of_passes == 1 ) && "Missing interlaced support for the generic conversion case." );
- ignore_unused_variable_warning( number_of_passes );
-
- if ( is_offset_view<TargetView>::value )
- skip_rows( get_offset<offset_t>( view ) );
-
- png_byte * const p_row ( p_row_buffer.get() );
- png_byte const * const p_row_end( p_row + row_length );
-
- unsigned int const rows_to_read( original_view( view ).dimensions().y );
- for ( unsigned int row_index( 0 ); row_index < rows_to_read; ++row_index )
- {
- read_row( p_row );
-
- typedef typename MyView::value_type pixel_t;
- typedef typename get_original_view_t<TargetView>::type::x_iterator x_iterator;
-
- pixel_t const * p_source_pixel( gil_reinterpret_cast_c<pixel_t const *>( p_row ) );
- x_iterator p_target_pixel( original_view( view ).row_begin( row_index ) );
- while ( p_source_pixel < gil_reinterpret_cast_c<pixel_t const *>( p_row_end ) )
- {
- converter( *p_source_pixel, *p_target_pixel );
- ++p_source_pixel;
- ++p_target_pixel;
- }
- }
- }
-
- void raw_convert_to_prepared_view( detail::libpng_view_data_t const & view_data ) const
- {
- BOOST_ASSERT( view_data.width_ == static_cast<unsigned int>( dimensions().x ) );
- BOOST_ASSERT( view_data.height_ == static_cast<unsigned int>( dimensions().y ) );
- BOOST_ASSERT( view_data.format_ == static_cast<unsigned int>( closest_gil_supported_format() ) );
-
- unsigned int const bit_depth ( ( view_data.format_ >> 16 ) & 0xFF );
- unsigned int const colour_type( view_data.format_ & 0xFF );
-
- if ( colour_type == PNG_COLOR_TYPE_PALETTE )
- {
- ::png_set_palette_to_rgb( &png_object() );
- }
- else
- if ( ( colour_type == PNG_COLOR_TYPE_GRAY ) && ( bit_depth < 8 ) )
- {
- ::png_set_expand_gray_1_2_4_to_8( &png_object() );
- }
-
- if ( !( colour_type & PNG_COLOR_MASK_ALPHA ) )
- ::png_set_strip_alpha( &png_object() );
-
- if ( ::png_get_valid( &png_object(), &info_object(), PNG_INFO_tRNS ) )
- ::png_set_tRNS_to_alpha( &png_object() );
-
- if ( bit_depth == 8 )
- ::png_set_strip_16( &png_object() );
-
- //...zzz...
-
- //if (color_type == PNG_COLOR_TYPE_RGB ||
- // color_type == PNG_COLOR_TYPE_RGB_ALPHA)
- // png_set_bgr(png_ptr);
-
- //if (color_type == PNG_COLOR_TYPE_RGB ||
- // color_type == PNG_COLOR_TYPE_GRAY)
- // png_set_add_alpha(png_ptr, filler, PNG_FILLER_AFTER);
-
- //if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
- // png_set_swap_alpha(png_ptr);
-
- //if (color_type == PNG_COLOR_TYPE_GRAY ||
- // color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
- // png_set_gray_to_rgb(png_ptr);
-
- //if (color_type == PNG_COLOR_TYPE_RGB ||
- // color_type == PNG_COLOR_TYPE_RGB_ALPHA)
- // png_set_rgb_to_gray_fixed(png_ptr, error_action,
- // int red_weight, int green_weight);
-
- ::png_read_update_info( &png_object(), &info_object() );
-
- raw_copy_to_prepared_view( view_data );
- }
-
- void raw_copy_to_prepared_view( detail::libpng_view_data_t const view_data ) const BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- #ifndef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- if ( setjmp( error_handler_target() ) )
- detail::throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- unsigned int number_of_passes( ::png_set_interlace_handling( &png_object() ) );
- BF_ASSUME( ( number_of_passes == 1 ) || ( number_of_passes == 7 ) );
-
- skip_rows( view_data.offset_ );
-
- while ( number_of_passes-- )
- {
- png_byte * p_row( view_data.buffer_ );
- png_byte const * const p_end( p_row + ( view_data.height_ * view_data.stride_ ) );
- while ( p_row < p_end )
- {
- read_row( p_row );
- memunit_advance( p_row, view_data.stride_ );
- }
- }
- }
-
- std::size_t cached_format_size( format_t const format ) const
- {
- return number_of_channels() * format_bit_depth( format ) / 8;
- }
-
-private:
- void cleanup_and_throw_libpng_error()
- {
- destroy_read_struct();
- detail::throw_libpng_error();
- }
-
- void init() BOOST_GIL_CAN_THROW //...zzz...a plain throw(...) would be enough here but it chokes GCC...
- {
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- try
- {
- #else
- if ( setjmp( error_handler_target() ) )
- cleanup_and_throw_libpng_error();
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
-
- ::png_read_info( &png_object(), &info_object() );
- if ( little_endian() )
- ::png_set_swap( &png_object() );
-
- #ifdef BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- }
- catch (...)
- {
- destroy_read_struct();
- throw;
- }
- #endif // BOOST_GIL_THROW_THROUGH_C_SUPPORTED
- }
-
- void skip_rows( unsigned int const row_to_skip_to ) const
- {
- BOOST_ASSERT( ( row_to_skip_to >= png_object().row_number ) && "No 'rewind' capability for LibPNG yet." );
-
- unsigned int number_of_rows_to_skip( row_to_skip_to - png_object().row_number );
- while ( number_of_rows_to_skip-- )
- {
- BF_ASSUME( row_to_skip_to != 0 );
- read_row( NULL );
- }
- }
-
- unsigned int number_of_channels() const { return ::png_get_channels ( &png_object(), &info_object() ); }
- std::size_t bit_depth () const { return ::png_get_bit_depth( &png_object(), &info_object() ); }
-
- void destroy_read_struct() { ::png_destroy_read_struct( &png_object_for_destruction(), &info_object_for_destruction(), NULL ); }
-
- void read_row( png_byte * const p_row ) const BOOST_GIL_CAN_THROW
- {
- ::png_read_row( &png_object(), p_row, NULL );
- }
-
- static void PNGAPI png_FILE_read_data( png_structp const png_ptr, png_bytep const data, png_size_t const length )
- {
- BOOST_ASSERT( png_ptr );
- BOOST_ASSERT( png_ptr->io_ptr );
-
- png_size_t const read_size
- (
- static_cast<png_size_t>( std::fread( data, 1, length, static_cast<FILE *>( png_ptr->io_ptr ) ) )
- );
-
- if ( read_size != length )
- detail::png_error_function( png_ptr, "Read Error" );
- }
-
- static void PNGAPI png_memory_chunk_read_data( png_structp const png_ptr, png_bytep const data, png_size_t const length )
- {
- BOOST_ASSERT( png_ptr );
- BOOST_ASSERT( png_ptr->io_ptr );
-
- memory_range_t & memory_chunk( *static_cast<memory_range_t *>( png_ptr->io_ptr ) );
-
- if ( length <= static_cast<std::size_t>( memory_chunk.size() ) )
- {
- std::memcpy( data, memory_chunk.begin(), length );
- memory_chunk.advance_begin( length );
- }
- else
- detail::png_error_function( png_ptr, "Read Error" );
- }
-};
-
-
-//------------------------------------------------------------------------------
-} // namespace gil
-//------------------------------------------------------------------------------
-} // namespace boost
-//------------------------------------------------------------------------------
-#endif // libpng_image_hpp


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk