[opengtl-commits] [280] add a png DeCoder

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 280
Author:   cyrille
Date:     2008-07-02 00:13:52 +0200 (Wed, 02 Jul 2008)

Log Message:
-----------
add a png DeCoder

Modified Paths:
--------------
    trunk/OpenGTL/CMakeLists.txt

Added Paths:
-----------
    trunk/OpenGTL/Extensions/
    trunk/OpenGTL/Extensions/CMakeLists.txt
    trunk/OpenGTL/Extensions/PngDC/
    trunk/OpenGTL/Extensions/PngDC/CMakeLists.txt
    trunk/OpenGTL/Extensions/PngDC/PngDC.cpp
    trunk/OpenGTL/Extensions/PngDC/PngDC.h


Modified: trunk/OpenGTL/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/CMakeLists.txt	2008-07-01 22:13:20 UTC (rev 279)
+++ trunk/OpenGTL/CMakeLists.txt	2008-07-01 22:13:52 UTC (rev 280)
@@ -6,14 +6,15 @@
 
 set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules )
 
-set(OPENGTL_VERSION "0.9.3")
+set(OPENGTL_VERSION "0.9.5")
 set(OPENGTL_LIB_VERSION ${OPENGTL_VERSION})
-set(OPENGTL_LIB_SOVERSION "0.3")
+set(OPENGTL_LIB_SOVERSION "0.4")
 
 set(LIB_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/)
 set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include/)
 set(BIN_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/bin/)
 set(SHARE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/share/OpenGTL/)
+set(GTLIMAGEIO_EXTENSIONS_INSTALL_DIR ${SHARE_INSTALL_DIR}/GTLImageIO/Extensions)
 
 add_definitions(-Wall -Werror )
 
@@ -24,3 +25,4 @@
 add_subdirectory(OpenGTL)
 add_subdirectory(OpenCTL)
 add_subdirectory(OpenShiva)
+add_subdirectory(Extensions)

Added: trunk/OpenGTL/Extensions/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/Extensions/CMakeLists.txt	                        (rev 0)
+++ trunk/OpenGTL/Extensions/CMakeLists.txt	2008-07-01 22:13:52 UTC (rev 280)
@@ -0,0 +1,5 @@
+find_package(PNG )
+
+if( PNG_FOUND )
+  add_subdirectory( PngDC )
+endif( PNG_FOUND )

Added: trunk/OpenGTL/Extensions/PngDC/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/Extensions/PngDC/CMakeLists.txt	                        (rev 0)
+++ trunk/OpenGTL/Extensions/PngDC/CMakeLists.txt	2008-07-01 22:13:52 UTC (rev 280)
@@ -0,0 +1,20 @@
+# add_subdirectory(tests)
+
+## GTLImageIO library ##
+
+include_directories(${PNG_INCLUDE_DIR})
+
+set(PNG_DC_SRCS
+    PngDC.cpp )
+
+add_library(PngDC SHARED ${PNG_DC_SRCS} )
+target_link_libraries(PngDC GTLImageIO ${PNG_LIBRARIES})
+
+# __STDC_LIMIT_MACROS is needed by LLVM's DataTypes.h
+add_definitions( "-D__STDC_LIMIT_MACROS" )
+add_definitions( -DCOUMPONENT_NAME=\"\\\"PngDC\\\"\" )
+add_definitions( ${PNG_DEFINITIONS} )
+
+# Install target
+install(TARGETS PngDC  DESTINATION ${GTLIMAGEIO_EXTENSIONS_INSTALL_DIR} )
+# install( FILES ImageDC.h DESTINATION ${INCLUDE_INSTALL_DIR}/GTLImageIO ) # NO REASON TO INSTALL HEADERS FOR NOW

Added: trunk/OpenGTL/Extensions/PngDC/PngDC.cpp
===================================================================
--- trunk/OpenGTL/Extensions/PngDC/PngDC.cpp	                        (rev 0)
+++ trunk/OpenGTL/Extensions/PngDC/PngDC.cpp	2008-07-01 22:13:52 UTC (rev 280)
@@ -0,0 +1,251 @@
+/*
+ *  Copyright (c) 2008 Cyrille Berger <cberger@xxxxxxxxxxx>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "PngDC.h"
+
+#include <png.h>
+
+#include <GTLCore/Macros_p.h>
+#include <GTLImageIO/ImageDCRegistry.h>
+#include <GTLCore/Image.h>
+#include <GTLCore/Type.h>
+#include <GTLCore/Region.h>
+#include <GTLCore/Debug.h>
+#include <GTLCore/PixelDescription.h>
+
+STATIC_INITIALISATION( PngDC )
+{
+  GTLImageIO::ImageDCRegistry::instance()->registerDC( new PngDC );
+}
+
+PngDC::PngDC()
+{
+  addReadWriteExtension("png");
+}
+
+PngDC::~PngDC()
+{
+}
+
+GTLCore::AbstractImage* PngDC::decode( const GTLCore::String& _fileName, GTLCore::Region* _region, GTLCore::String* _errorMessage ) const
+{
+  FILE *fp = fopen( _fileName.c_str(), "rb"); 
+  COND_TELL_ERROR( fp, "Can't open file: " + _fileName);
+  
+  // Create info structure
+  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
+  COND_TELL_ERROR( png_ptr, "Can't initialize libpng." );
+  
+  // Create info structure
+  png_infop info_ptr = png_create_info_struct(png_ptr);
+  if(not info_ptr) 
+  {
+    png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
+    fclose(fp);
+    TELL_ERROR( "Can't initialize libpng." );
+  }
+  
+  // Create info structure
+  png_infop end_info = png_create_info_struct(png_ptr);
+  if (!end_info) 
+  {
+    png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
+    fclose(fp);
+    TELL_ERROR( "Can't initialize libpng." );
+  }
+  
+  // Initialise jump
+  if (setjmp(png_jmpbuf(png_ptr))) 
+  {
+    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+    fclose(fp);
+    TELL_ERROR( "Can't initialize libpng." );
+  }
+  
+  // Initialise IO
+  png_init_io(png_ptr, fp);
+  
+  // Read information
+  png_read_info(png_ptr, info_ptr);
+  
+  // Query information
+  png_uint_32 width;
+  png_uint_32 height;
+  int bit_depth;
+  int color_type;
+
+  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
+  
+  const GTLCore::Type* channelType = 0;
+  switch( bit_depth )
+  {
+    case 8:
+      channelType = GTLCore::Type::UnsignedInteger8;
+      break;
+    case 16:
+      channelType = GTLCore::Type::UnsignedInteger16;
+      break;
+    default:
+      png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+      fclose(fp);
+      TELL_ERROR( "Unsupported bit depth: " + GTLCore::String::number( bit_depth ) );
+  }
+  GTL_ASSERT( channelType );
+  int channelsCount = 0;
+  switch( color_type )
+  {
+    case PNG_COLOR_TYPE_GRAY:
+      channelsCount = 1;
+      break;
+    case PNG_COLOR_TYPE_GRAY_ALPHA:
+      channelsCount = 2;
+      break;
+    case PNG_COLOR_TYPE_RGB:
+      channelsCount = 3;
+      break;
+    case PNG_COLOR_TYPE_RGB_ALPHA:
+      channelsCount = 4;
+      break;
+    default:
+      png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+      fclose(fp);
+      TELL_ERROR( "Unsupported color type: " + GTLCore::String::number( color_type ) );
+  }
+  GTL_ASSERT( channelsCount != 0);
+  GTLCore::PixelDescription pixelDescription( channelType, channelsCount );
+  
+  // Allocate buffer image
+  GTLCore::Image* image = new GTLCore::Image( width, height, pixelDescription );
+  if( _region )
+  {
+    _region->setWidth( width );
+    _region->setHeight( height );
+  }
+  
+  png_bytep* row_pointers = new png_bytep[height];
+
+  for (uint y = 0; y < height; y++)
+  {
+    row_pointers[y] = reinterpret_cast<png_byte*>(image->data(0, y));
+  }
+  // Read data
+  png_read_image(png_ptr, row_pointers);
+  
+  // Cleanup
+  png_read_end(png_ptr, end_info);
+  png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+  delete[] row_pointers;
+  fclose(fp);
+  
+  return image;
+}
+
+bool PngDC::encode( const GTLCore::AbstractImage* _image, const GTLCore::Region& _region, const GTLCore::String& _fileName, GTLCore::String* _errorMessage ) const
+{
+  GTL_ASSERT( _image );
+  FILE *fp = fopen(_fileName.c_str(), "wb");
+  COND_TELL_ERROR( fp, "Can't open file: " + _fileName);
+  
+  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
+  COND_TELL_ERROR( png_ptr, "Can't initialize libpng." );
+  
+  // Create info structure
+  png_infop info_ptr = png_create_info_struct(png_ptr);
+  if(not info_ptr) 
+  {
+    png_destroy_write_struct(&png_ptr, 0);
+    fclose(fp);
+    TELL_ERROR( "Can't initialize libpng." );
+  }
+  
+  // Set error handler
+  if (setjmp(png_jmpbuf(png_ptr))) 
+  {
+    png_destroy_write_struct(&png_ptr, &info_ptr); 
+    fclose(fp); 
+    TELL_ERROR( "Can't initialize libpng." );
+  }
+  
+  // Check that all channels have the same type
+  if( not _image->pixelDescription().sameTypeChannels() )
+  {
+    png_destroy_write_struct(&png_ptr, &info_ptr); 
+    fclose(fp); 
+    TELL_ERROR( "All channels must have the same type." );
+  }
+  // Guess the bit depth
+  int bit_depth = 0;
+  switch( _image->pixelDescription().channelTypes()[0]->dataType() )
+  {
+    case GTLCore::Type::UNSIGNED_INTEGER8:
+      bit_depth = 8;
+      break;
+    case GTLCore::Type::UNSIGNED_INTEGER16:
+      bit_depth = 16;
+      break;
+    default:
+      png_destroy_write_struct(&png_ptr, &info_ptr);
+      fclose(fp);
+      TELL_ERROR( "Unsupported bit depth: " + GTLCore::String::number( bit_depth ) );
+  }
+  // Guess the color type
+  int color_type;
+  switch( _image->pixelDescription().channels() )
+  {
+    case 1:
+      color_type = PNG_COLOR_TYPE_GRAY;
+      break;
+    case 2:
+      color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
+      break;
+    case 3:
+      color_type = PNG_COLOR_TYPE_RGB;
+      break;
+    case 4:
+      color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+      break;
+    default:
+      png_destroy_write_struct(&png_ptr, &info_ptr);
+      fclose(fp);
+      TELL_ERROR( "Unsupported color type: " + GTLCore::String::number( color_type ) );
+  }
+  // Set information
+  png_set_IHDR( png_ptr, info_ptr, _region.width(), _region.height()
+  , bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+  png_init_io(png_ptr, fp );
+  png_write_info(png_ptr, info_ptr);
+  
+  int pixel_size = (_image->pixelDescription().bitsSize() / 8);
+  png_bytep row_pointer = new png_byte[ pixel_size * _region.width()  ];
+
+  for(int y = 0; y < _region.height(); ++y)
+  {
+    for(int x = 0; x < _region.width(); ++x)
+    {
+      memcpy( row_pointer + x * pixel_size, _image->data( x, y ), pixel_size );
+    }
+    png_write_row(png_ptr, row_pointer);
+  }
+  // Cleanup
+  delete[] row_pointer;
+  png_write_end(png_ptr, info_ptr);
+  png_destroy_write_struct(&png_ptr, &info_ptr);
+  fclose(fp);
+  return true;
+}


Property changes on: trunk/OpenGTL/Extensions/PngDC/PngDC.cpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/OpenGTL/Extensions/PngDC/PngDC.h
===================================================================
--- trunk/OpenGTL/Extensions/PngDC/PngDC.h	                        (rev 0)
+++ trunk/OpenGTL/Extensions/PngDC/PngDC.h	2008-07-01 22:13:52 UTC (rev 280)
@@ -0,0 +1,28 @@
+/*
+ *  Copyright (c) 2008 Cyrille Berger <cberger@xxxxxxxxxxx>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include <GTLImageIO/ImageDC.h>
+
+class PngDC : public GTLImageIO::ImageDC {
+  public:
+    PngDC();
+    virtual ~PngDC();
+    virtual GTLCore::AbstractImage* decode( const GTLCore::String& _fileName, GTLCore::Region*  = 0, GTLCore::String* _errorMessage = 0 ) const;
+    virtual bool encode( const GTLCore::AbstractImage* _image, const GTLCore::Region& _region, const GTLCore::String& _fileName, GTLCore::String* _errorMessage = 0 ) const;
+};


Property changes on: trunk/OpenGTL/Extensions/PngDC/PngDC.h
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/