]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/imagpcx.cpp
Accomodate changes in CountColours
[wxWidgets.git] / src / common / imagpcx.cpp
index 5a6ca8b0a0a150f83b0c408656637ef88a6a3c67..c8420edf10dcb949d290a1002133c6dcd8806bda 100644 (file)
@@ -8,10 +8,9 @@
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
-/*
-   We don't put pragma implement in this file because it is already present in
-   src/common/image.cpp
-*/
+#ifdef __GNUG__
+#pragma implementation "imagpcx.h"
+#endif
 
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
@@ -26,7 +25,7 @@
 
 #if wxUSE_STREAMS && wxUSE_PCX
 
-#include "wx/image.h"
+#include "wx/imagpcx.h"
 #include "wx/wfstream.h"
 #include "wx/module.h"
 #include "wx/log.h"
@@ -37,7 +36,7 @@
 #include "wx/object.h"
 
 //-----------------------------------------------------------------------------
-// PCX decoding
+// RLE encoding and decoding
 //-----------------------------------------------------------------------------
 
 void RLEencode(unsigned char *p, unsigned int size, wxOutputStream& s)
@@ -77,7 +76,6 @@ void RLEencode(unsigned char *p, unsigned int size, wxOutputStream& s)
         }
     }
 
-
     // Write the last one and return;
     //
     if ((cont > 1) || ((last & 0xC0) == 0xC0))
@@ -121,7 +119,11 @@ void RLEdecode(unsigned char *p, unsigned int size, wxInputStream& s)
 }
 
 
-/* PCX header */
+//-----------------------------------------------------------------------------
+// PCX reading and saving
+//-----------------------------------------------------------------------------
+
+// PCX header
 #define HDR_MANUFACTURER    0
 #define HDR_VERSION         1
 #define HDR_ENCODING        2
@@ -148,6 +150,7 @@ enum {
     wxPCX_VERERR = 3        // error in pcx version number
 };
 
+
 // ReadPCX:
 //  Loads a PCX file into the wxImage object pointed by image.
 //  Returns wxPCX_OK on success, or an error code otherwise
@@ -278,28 +281,20 @@ int SavePCX(wxImage *image, wxOutputStream& stream)
     unsigned char *src;             // pointer into wxImage data
     unsigned int width, height;     // size of the image
     unsigned int bytesperline;      // bytes per line (each plane)
-    int nplanes;                    // number of planes
-    int format;                     // image format (8 bit, 24 bit)
+    int nplanes = 3;                // number of planes
+    int format = wxPCX_24BIT;       // image format (8 bit, 24 bit)
     wxHashTable h(wxKEY_INTEGER);   // image histogram
-    unsigned long ncolours;         // num. of different colours
     unsigned long key;              // key in the hashtable
     unsigned int i;
  
-    // Get the histogram of the image, and decide whether to save
-    // as 8 bit or 24 bit, according to the number of colours.
+    // See if we can save as 8 bit.
     //
-    ncolours = image->ComputeHistogram(h);
-
-    if (ncolours <= 256)
+    if (image->CountColours(256) <= 256)
     {
+        image->ComputeHistogram(h);
         format = wxPCX_8BIT;
         nplanes = 1;
     }
-    else
-    {
-        format = wxPCX_24BIT;
-        nplanes = 3;
-    }
 
     // Get image dimensions, calculate bytesperline (must be even,
     // according to PCX specs) and allocate space for one complete
@@ -358,10 +353,7 @@ int SavePCX(wxImage *image, wxOutputStream& stream)
                     key = (r << 16) | (g << 8) | b;
 
                     hnode = (wxHNode *) h.Get(key);
-                    if (!hnode)
-                        wxLogError("!hnode");
-                    else
-                        p[i] = hnode->index;
+                    p[i] = hnode->index;
                 }
                 break;
             }
@@ -376,7 +368,7 @@ int SavePCX(wxImage *image, wxOutputStream& stream)
                 break;
             }
         }
-    
+
         RLEencode(p, bytesperline * nplanes, stream);
     }
     
@@ -398,15 +390,10 @@ int SavePCX(wxImage *image, wxOutputStream& stream)
             key = node->GetKeyInteger();
             hnode = (wxHNode *) node->GetData();
 
-            if (!hnode)
-                wxLogError("!hnode");
-            else
-            {
-                pal[3 * hnode->index]     = (unsigned char)(key >> 16);
-                pal[3 * hnode->index + 1] = (unsigned char)(key >> 8);
-                pal[3 * hnode->index + 2] = (unsigned char)(key);
-                delete hnode;
-            }
+            pal[3 * hnode->index]     = (unsigned char)(key >> 16);
+            pal[3 * hnode->index + 1] = (unsigned char)(key >> 8);
+            pal[3 * hnode->index + 2] = (unsigned char)(key);
+            delete hnode;
         }
 
         stream.PutC(12);