]> git.saurik.com Git - wxWidgets.git/commitdiff
Now saves in eiher 8bit or 24bit
authorGuillermo Rodriguez Garcia <guille@iies.es>
Wed, 15 Dec 1999 11:46:19 +0000 (11:46 +0000)
committerGuillermo Rodriguez Garcia <guille@iies.es>
Wed, 15 Dec 1999 11:46:19 +0000 (11:46 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4956 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/imagpcx.cpp

index 93d35028709696c1e1246ab992fcd73f68955bf4..5a6ca8b0a0a150f83b0c408656637ef88a6a3c67 100644 (file)
 #include "wx/log.h"
 #include "wx/intl.h"
 
 #include "wx/log.h"
 #include "wx/intl.h"
 
+#include "wx/hash.h"
+#include "wx/list.h"
+#include "wx/object.h"
+
 //-----------------------------------------------------------------------------
 // PCX decoding
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 // PCX decoding
 //-----------------------------------------------------------------------------
@@ -269,24 +273,33 @@ int ReadPCX(wxImage *image, wxInputStream& stream)
 int SavePCX(wxImage *image, wxOutputStream& stream)
 {
     unsigned char hdr[128];         // PCX header
 int SavePCX(wxImage *image, wxOutputStream& stream)
 {
     unsigned char hdr[128];         // PCX header
+    unsigned char pal[768];         // palette for 8 bit images
     unsigned char *p;               // space to store one scanline
     unsigned char *src;             // pointer into wxImage data
     unsigned int width, height;     // size of the image
     unsigned int bytesperline;      // bytes per line (each plane)
     unsigned char *p;               // space to store one scanline
     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 = 3;                // number of planes
-    int format = wxPCX_24BIT;       // image format (8 bit, 24 bit)
+    int nplanes;                    // number of planes
+    int format;                     // 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;
  
     unsigned int i;
  
-    /* XXX
-    build_palette();
-    if (num_of_colors <= 256)
+    // Get the histogram of the image, and decide whether to save
+    // as 8 bit or 24 bit, according to the number of colours.
+    //
+    ncolours = image->ComputeHistogram(h);
+
+    if (ncolours <= 256)
     {
         format = wxPCX_8BIT;
         nplanes = 1;
     }
     else
     {
         format = wxPCX_8BIT;
         nplanes = 1;
     }
     else
-        free_palette();
-    */
+    {
+        format = wxPCX_24BIT;
+        nplanes = 3;
+    }
 
     // Get image dimensions, calculate bytesperline (must be even,
     // according to PCX specs) and allocate space for one complete
 
     // Get image dimensions, calculate bytesperline (must be even,
     // according to PCX specs) and allocate space for one complete
@@ -297,7 +310,6 @@ int SavePCX(wxImage *image, wxOutputStream& stream)
 
     width = image->GetWidth();
     height = image->GetHeight();
 
     width = image->GetWidth();
     height = image->GetHeight();
-    nplanes = 3;                    /* 1 for wxPCX_8BIT */
     bytesperline = width;
     if (bytesperline % 2)
         bytesperline++;
     bytesperline = width;
     if (bytesperline % 2)
         bytesperline++;
@@ -334,19 +346,25 @@ int SavePCX(wxImage *image, wxOutputStream& stream)
         switch (format)
         {
             case wxPCX_8BIT:
         switch (format)
         {
             case wxPCX_8BIT:
-            /* XXX
             {
             {
+                unsigned char r, g, b;
+                wxHNode *hnode;
+
                 for (i = 0; i < width; i++)
                 {
                 for (i = 0; i < width; i++)
                 {
-                    hash = *(src++) << 24 +
-                           *(src++) << 16 +
-                           *(src++) << 8;
-
-                    p[i] = palette_lookup(hash);
+                    r = *(src++);
+                    g = *(src++);
+                    b = *(src++);
+                    key = (r << 16) | (g << 8) | b;
+
+                    hnode = (wxHNode *) h.Get(key);
+                    if (!hnode)
+                        wxLogError("!hnode");
+                    else
+                        p[i] = hnode->index;
                 }
                 break;
             }
                 }
                 break;
             }
-            */
             case wxPCX_24BIT:
             {
                 for (i = 0; i < width; i++)
             case wxPCX_24BIT:
             {
                 for (i = 0; i < width; i++)
@@ -364,18 +382,40 @@ int SavePCX(wxImage *image, wxOutputStream& stream)
     
     free(p);
 
     
     free(p);
 
-    /* XXX
+    // For 8 bit images, build the palette and write it to the stream
+    //
     if (format == wxPCX_8BIT)
     {
     if (format == wxPCX_8BIT)
     {
+        wxNode *node;
+        wxHNode *hnode;
+
+        // zero unused colours
+        memset(pal, 0, sizeof(pal));
+
+        h.BeginFind();
+        while ((node = h.Next()) != NULL)
+        {
+            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;
+            }
+        }
+
         stream.PutC(12);
         stream.PutC(12);
-        dump_palette();
+        stream.Write(pal, 768);
     }
     }
-    */
 
     return wxPCX_OK;
 }
 
 
     return wxPCX_OK;
 }
 
-
 //-----------------------------------------------------------------------------
 // wxPCXHandler
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 // wxPCXHandler
 //-----------------------------------------------------------------------------