]> git.saurik.com Git - wxWidgets.git/commitdiff
more options for JPEG and TIFF handlers (slightly modified patch 1044470)
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 19 Feb 2005 16:32:29 +0000 (16:32 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 19 Feb 2005 16:32:29 +0000 (16:32 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@32189 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/image.h
include/wx/imagjpeg.h
include/wx/imagtiff.h
src/common/imagjpeg.cpp
src/common/imagtiff.cpp

index 6fb884955b062e61ec306cf1a72bd836aafffd03..7a9e084fb276ef9be0ca13752c480562748cd7f4 100644 (file)
 
 #define wxIMAGE_OPTION_FILENAME wxString(_T("FileName"))
 
+#define wxIMAGE_OPTION_RESOLUTION            wxString(_T("Resolution"))
+#define wxIMAGE_OPTION_RESOLUTIONX           wxString(_T("ResolutionX"))
+#define wxIMAGE_OPTION_RESOLUTIONY           wxString(_T("ResolutionY"))
+
+#define wxIMAGE_OPTION_RESOLUTIONUNIT        wxString(_T("ResolutionUnit"))
+
+// constants used with wxIMAGE_OPTION_RESOLUTIONUNIT
+enum
+{
+    wxIMAGE_RESOLUTION_INCHES = 1,
+    wxIMAGE_RESOLUTION_CM = 2
+};
+
 //-----------------------------------------------------------------------------
 // classes
 //-----------------------------------------------------------------------------
index bf7594fae1cc3c3f15470eb951b4a26dc3454b93..e51491e8f7559e6f13b28c0bb1597eee48b15ae7 100644 (file)
 
 #include "wx/image.h"
 
-// defines for wxImage::SetOption
-#define wxIMAGE_OPTION_RESOLUTION            wxString(_T("Resolution"))
-#define wxIMAGE_OPTION_RESOLUTIONUNIT        wxString(_T("ResolutionUnit"))
-
-enum
-{
-    wxIMAGE_RESOLUTION_INCHES = 1,
-    wxIMAGE_RESOLUTION_CM = 2
-};
 
 //-----------------------------------------------------------------------------
 // wxJPEGHandler
@@ -53,8 +44,5 @@ private:
 };
 #endif
 
-
-
-#endif
-  // _WX_IMAGJPEG_H_
+#endif // _WX_IMAGJPEG_H_
 
index 4607162bf06459fe0819ef1cd61baf8fe463c8af..44e8c6fa6426e85036d7739db89eccadf96449d3 100644 (file)
 
 #include "wx/image.h"
 
+// defines for wxImage::SetOption
+#define wxIMAGE_OPTION_BITSPERSAMPLE               wxString(_T("BitsPerSample"))
+#define wxIMAGE_OPTION_SAMPLESPERPIXEL             wxString(_T("SamplesPerPixel"))
+#define wxIMAGE_OPTION_COMPRESSION                 wxString(_T("Compression"))
+#define wxIMAGE_OPTION_IMAGEDESCRIPTOR             wxString(_T("ImageDescriptor"))
 
 //-----------------------------------------------------------------------------
 // wxTIFFHandler
index d7cd54a73c863488a946f48b10c11d27b79d06cc..42b995aae9b664f0eacbe676ba1f53dd8b48d68b 100644 (file)
@@ -359,17 +359,36 @@ bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbo
     if (image->HasOption(wxT("quality")))
         jpeg_set_quality(&cinfo, image->GetOptionInt(wxT("quality")), TRUE);
 
-    // sets the resolution fields in the output file
-    if (image->HasOption(wxIMAGE_OPTION_RESOLUTION))
+    // set the resolution fields in the output file
+    UINT16 resX,
+           resY;
+    if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) &&
+         image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) )
     {
-        cinfo.X_density =
-        cinfo.Y_density = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION);
+        resX = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX);
+        resY = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
+    }
+    else if ( image->HasOption(wxIMAGE_OPTION_RESOLUTION) )
+    {
+        resX =
+        resY = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION);
+    }
+    else
+    {
+        resX =
+        resY = 0;
+    }
+
+    if ( resX && resY )
+    {
+        cinfo.X_density = resX;
+        cinfo.Y_density = resY;
     }
 
     // sets the resolution unit field in the output file
     // wxIMAGE_RESOLUTION_INCHES for inches
     // wxIMAGE_RESOLUTION_CM for centimeters
-    if (image->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT))
+    if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT) )
     {
         cinfo.density_unit = (UINT8)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT);
     }
index 3cd681f75775d59cf0ae59de5e1cfcb905297ecf..63e7b60c78a4b5ea526af7a78c573ce32a970147 100644 (file)
@@ -333,19 +333,48 @@ bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbo
         return false;
     }
 
+    TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
     TIFFSetField(tif, TIFFTAG_IMAGEWIDTH,  (uint32)image->GetWidth());
     TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight());
     TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
-    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
-    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
-    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
-    TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
 
-    tsize_t linebytes = (tsize_t)image->GetWidth() * 3;
+    if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) &&
+            image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) )
+    {
+        TIFFSetField(tif, TIFFTAG_XRESOLUTION,
+                        image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX));
+        TIFFSetField(tif, TIFFTAG_YRESOLUTION,
+                        image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY));
+    }
+
+    int spp = image->GetOptionInt(wxIMAGE_OPTION_SAMPLESPERPIXEL);
+    if ( !spp )
+        spp = 3;
+
+    int bpp = image->GetOptionInt(wxIMAGE_OPTION_BITSPERSAMPLE);
+    if ( !bpp )
+        bpp=8;
+
+    int compression = image->GetOptionInt(wxIMAGE_OPTION_COMPRESSION);
+    if ( !compression )
+        compression=COMPRESSION_LZW;
+
+    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp);
+    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bpp);
+    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, spp*bpp == 1 ? PHOTOMETRIC_MINISBLACK
+                                                        : PHOTOMETRIC_RGB);
+    TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
+
+    // scanlinesize if determined by spp and bpp
+    tsize_t linebytes = (tsize_t)image->GetWidth() * spp * bpp / 8;
+
+    if ( (image->GetWidth() % 8 > 0) && (spp * bpp < 8) )
+        linebytes+=1;
+
     unsigned char *buf;
 
-    if (TIFFScanlineSize(tif) > linebytes)
+    if (TIFFScanlineSize(tif) > linebytes || (spp * bpp < 24))
     {
         buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
         if (!buf)
@@ -363,16 +392,49 @@ bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbo
         buf = NULL;
     }
 
-    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
-        TIFFDefaultStripSize(tif, (uint32) -1));
+    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,TIFFDefaultStripSize(tif, (uint32) -1));
+
+    uint8 bitmask;
 
     unsigned char *ptr = image->GetData();
-    for (int row = 0; row < image->GetHeight(); row++)
+    for ( int row = 0; row < image->GetHeight(); row++ )
     {
-        if (buf)
-            memcpy(buf, ptr, image->GetWidth());
+        if ( buf )
+        {
+            if ( spp * bpp > 1 )
+            {
+                // color image
+                memcpy(buf, ptr, image->GetWidth());
+            }
+            else // black and white image
+            {
+                for ( int column = 0; column < linebytes; column++ )
+                {
+                    uint8 reverse = 0;
+                    bitmask = 1;
+                    for ( int bp = 0; bp < 8; bp++ )
+                    {
+                        if ( ptr[column*24 + bp*3] > 0 )
+                        {
+                            // check only red as this is sufficient
+                            reverse = reverse | 128 >> bp;
+                        }
+
+                        bitmask <<= 1;
+                    }
+
+                    // FIXME: what's this??
+#ifdef __WXGTK__
+                    buf[column]=~reverse;
+
+#else
+                    buf[column]=reverse;
+#endif
+                }
+            }
+        }
 
-        if (TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0)
+        if ( TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0 )
         {
             if (verbose)
                 wxLogError( _("TIFF: Error writing image.") );
@@ -383,13 +445,14 @@ bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbo
 
             return false;
         }
+
         ptr += image->GetWidth()*3;
     }
 
     (void) TIFFClose(tif);
 
     if (buf)
-    _TIFFfree(buf);
+        _TIFFfree(buf);
 
     return true;
 }