Przeglądaj źródła

adapted ImageFile to write ImageMagick comparable pngs

Alexander Freytag 11 lat temu
rodzic
commit
619c509633
2 zmienionych plików z 87 dodań i 45 usunięć
  1. 79 43
      core/image/ImageFile.cpp
  2. 8 2
      core/image/ImageFile.h

+ 79 - 43
core/image/ImageFile.cpp

@@ -136,52 +136,88 @@ void ImageFile::readerPNG(GrayColorImageCommonImplementationT<Ipp32f> *image)
 	
     }
 
-    template<>
-    void ImageFile::writerMagick(const GrayColorImageCommonImplementationT<unsigned char> *image) const
+template<>
+void ImageFile::writerMagick(const GrayColorImageCommonImplementationT<unsigned char> *image) const
+{
+  if ( image->channels() == 1 ) // gray image
+  {
+      Magick::Image magick_image;
+    // checking whether we have not a simple aligned image (block in memory)
+    if ( image->rowStepsize() != image->width() )
     {
-	if ( image->channels() == 1 ) {
-	    Magick::Image magick_image;
-		// checking whether we have not a simple aligned image (block in memory)
-		if ( image->rowStepsize() != image->width() ) {
-			// this means that we have a sub image or an IPP image
-			// (i.e. some pixels are skipped in each row)
-			// The Blob interface of ImageMagick is not flexible enough to handle this case
-			//
-			// Solution: we have to copy the image :(
-			// We do this by creating an empty NICE image
-			ImageT<unsigned char> image_noalignment;
-			// Setting the memory alignment and copying from image
-			image_noalignment.copyFrom ( *image, GrayColorImageCommonImplementation::noAlignment );
-			// and doing the conversion to a Magick image
-			// the alternative would be to directly copy from image to magick_image without memory
-			// operations, but this could be slower
-			Magick::Blob blob ( image_noalignment.getPixelPointer(), image_noalignment.width()*image_noalignment.height() );
-	    	magick_image.magick ("GRAY");
+      // this means that we have a sub image or an IPP image
+      // (i.e. some pixels are skipped in each row)
+      // The Blob interface of ImageMagick is not flexible enough to handle this case
+      //
+      // Solution: we have to copy the image :(
+      // We do this by creating an empty NICE image
+      ImageT<unsigned char> image_noalignment;
+      // Setting the memory alignment and copying from image
+      image_noalignment.copyFrom ( *image, GrayColorImageCommonImplementation::noAlignment );
+      // and doing the conversion to a Magick image
+      // the alternative would be to directly copy from image to magick_image without memory
+      // operations, but this could be slower
+      Magick::Blob blob ( image_noalignment.getPixelPointer(), image_noalignment.width()*image_noalignment.height() );
+        magick_image.magick ("GRAY");
             magick_image.read ( blob, Magick::Geometry ( image_noalignment.width(), image_noalignment.height() ), 8 );
-	    	magick_image.write ( filename );
-		} else {
-        	Magick::Blob blob ( image->getPixelPointer(), image->width()*image->height() );
-	    	magick_image.magick ("GRAY");
-            magick_image.read ( blob, Magick::Geometry ( image->width(), image->height() ), 8 );
-	    	magick_image.write ( filename );
-		}
-	} else {
-		// see above, the same problems occur for RGB images
-		if ( image->rowStepsize() != image->width() ) {
-			ColorImageT<unsigned char> image_noalignment;
-			image_noalignment.copyFrom ( *image, GrayColorImageCommonImplementation::noAlignment );
-			Magick::Image magick_image ( image_noalignment.width(), image_noalignment.height(),
-				"RGB", Magick::CharPixel, image_noalignment.getPixelPointer() );
-	    	magick_image.write ( filename );
-
-		} else {
-			// we do not need the Blob definition here (somehow strange)
-		    Magick::Image magick_image ( image->width(), image->height(),
-				"RGB", Magick::CharPixel, image->getPixelPointer() );
-	    	magick_image.write ( filename );
-		}
-	}
+        magick_image.write ( filename );
+    }
+    else
+    {
+        Magick::Blob blob ( image->getPixelPointer(), image->width()*image->height() );
+        
+        magick_image.magick ("GRAY");
+        magick_image.read ( blob, Magick::Geometry ( image->width(), image->height() ), 8 );
+        
+        magick_image.write ( filename );
     }
+  }
+  else // color image
+  {
+    // see above, the same problems occur for RGB images
+    if ( image->rowStepsize() != image->width() )
+    {
+      ColorImageT<unsigned char> image_noalignment;
+      
+      image_noalignment.copyFrom ( *image, GrayColorImageCommonImplementation::noAlignment );
+      Magick::Image magick_image ( image_noalignment.width(), image_noalignment.height(),
+        "RGB", Magick::CharPixel, image_noalignment.getPixelPointer() );
+      
+      std::string pngEnding ( ".png" );
+      // do we currently want to write a png file?
+      // if so, adapt writing parameters accordingly 
+      //
+      // needed for compatibility with png writer
+      if ( filename.compare( filename.length() - pngEnding.length(), pngEnding.length(), pngEnding) == 0)
+      {
+        magick_image.defineValue( "png", "bit-depth", "8");
+        magick_image.defineValue( "png", "format", "png8");
+      }
+      
+      magick_image.write ( filename );
+
+    }
+    else
+    {
+      // we do not need the Blob definition here (somehow strange)
+      Magick::Image magick_image ( image->width(), image->height(),
+      "RGB", Magick::CharPixel, image->getPixelPointer() );
+        
+      std::string pngEnding ( ".png" );
+      // do we currently want to write a png file?
+      // if so, adapt writing parameters accordingly 
+      //
+      // needed for compatibility with png writer
+      if ( filename.compare( filename.length() - pngEnding.length(), pngEnding.length(), pngEnding) == 0)
+      {
+        magick_image.defineValue( "png", "bit-depth", "8");
+        magick_image.defineValue( "png", "format", "png8");
+      }
+      
+      magick_image.write ( filename );
+    }
+  }
+}
 #endif
 
 ImageFile::Format ImageFile::name2Format(const std::string &filename)

+ 8 - 2
core/image/ImageFile.h

@@ -4,8 +4,10 @@
  * See file License for license information.
  */
 /*****************************************************************************/
-/*! \file ImageFile.h 
-    \brief ImageFile class declaration
+/** @file ImageFile.h 
+    @brief ImageFile class declaration
+    @author Michael Koch, Erik Rodner, Alexander Freytag
+    @date 22-01-2014 (dd-mm-yyyy)
  */
 /*****************************************************************************/
 /*
@@ -230,6 +232,10 @@ class ImageFile
 /*****************************************************************************/
 /*
  *  $Log: ImageFile.h,v $
+ * 
+ *  Revision 1.6  22-01-2014 (dd-mm-yyyy) freytag
+ *  changed png writing with ImageMagick to be comparable to libPNG
+ * 
  *  Revision 1.5  2009/08/03 15:23:49  koch
  *  deleted unnecessary .h
  *