git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4956
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
#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
//-----------------------------------------------------------------------------
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
- /* 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
+ {
+ 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
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++;
switch (format)
{
case wxPCX_8BIT:
switch (format)
{
case wxPCX_8BIT:
+ 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;
case wxPCX_24BIT:
{
for (i = 0; i < width; i++)
case wxPCX_24BIT:
{
for (i = 0; i < width; i++)
+ // 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.Write(pal, 768);
//-----------------------------------------------------------------------------
// wxPCXHandler
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// wxPCXHandler
//-----------------------------------------------------------------------------