]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagxpm.cpp
support mac on little endian systems
[wxWidgets.git] / src / common / imagxpm.cpp
CommitLineData
c96ea657
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: imagxpm.cpp
3// Purpose: wxXPMHandler
424f5e27 4// Author: Vaclav Slavik, Robert Roebling
c96ea657
VS
5// RCS-ID: $Id$
6// Copyright: (c) 2001 Vaclav Slavik
65571936 7// Licence: wxWindows licence
c96ea657
VS
8/////////////////////////////////////////////////////////////////////////////
9
10/*
11
12This file is partially based on source code of ImageMagick by John Cristy. Its
13license is as follows:
14
15%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16% %
17% %
18% %
19% X X PPPP M M %
20% X X P P MM MM %
21% X PPPP M M M %
22% X X P M M %
23% X X P M M %
24% %
25% %
26% Read/Write ImageMagick Image Format. %
27% %
28% %
29% Software Design %
30% John Cristy %
31% July 1992 %
32% %
33% %
34% Copyright (C) 2001 ImageMagick Studio, a non-profit organization dedicated %
35% to making software imaging solutions freely available. %
36% %
37% Permission is hereby granted, free of charge, to any person obtaining a %
38% copy of this software and associated documentation files ("ImageMagick"), %
39% to deal in ImageMagick without restriction, including without limitation %
40% the rights to use, copy, modify, merge, publish, distribute, sublicense, %
41% and/or sell copies of ImageMagick, and to permit persons to whom the %
42% ImageMagick is furnished to do so, subject to the following conditions: %
43% %
44% The above copyright notice and this permission notice shall be included in %
45% all copies or substantial portions of ImageMagick. %
46% %
47% The software is provided "as is", without warranty of any kind, express or %
48% implied, including but not limited to the warranties of merchantability, %
49% fitness for a particular purpose and noninfringement. In no event shall %
50% ImageMagick Studio be liable for any claim, damages or other liability, %
51% whether in an action of contract, tort or otherwise, arising from, out of %
52% or in connection with ImageMagick or the use or other dealings in %
53% ImageMagick. %
54% %
55% Except as contained in this notice, the name of the ImageMagick Studio %
56% shall not be used in advertising or otherwise to promote the sale, use or %
57% other dealings in ImageMagick without prior written authorization from the %
58% ImageMagick Studio. %
59% %
60%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61%
62%
63*/
64
14f355c2 65#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c96ea657
VS
66#pragma implementation "imagxpm.h"
67#endif
68
69// For compilers that support precompilation, includes "wx.h".
70#include "wx/wxprec.h"
71
72#ifdef __BORLANDC__
73# pragma hdrstop
74#endif
75
76#ifndef WX_PRECOMP
77# include "wx/defs.h"
78#endif
79
c67d6888 80#if wxUSE_XPM
c96ea657
VS
81
82#include "wx/imagxpm.h"
83#include "wx/wfstream.h"
84#include "wx/log.h"
85#include "wx/intl.h"
86#include "wx/utils.h"
424f5e27 87#include "wx/xpmdecod.h"
c96ea657
VS
88
89IMPLEMENT_DYNAMIC_CLASS(wxXPMHandler,wxImageHandler)
90
91//-----------------------------------------------------------------------------
92// wxXPMHandler
93//-----------------------------------------------------------------------------
94
95#if wxUSE_STREAMS
96
33ac7e6f 97bool wxXPMHandler::LoadFile(wxImage *image,
424f5e27 98 wxInputStream& stream,
38caaa61 99 bool WXUNUSED(verbose), int WXUNUSED(index))
c96ea657 100{
424f5e27
VS
101 wxXPMDecoder decoder;
102
103 wxImage img = decoder.ReadFile(stream);
104 if ( !img.Ok() )
7beb59f3 105 return false;
424f5e27 106 *image = img;
7beb59f3 107 return true;
c96ea657
VS
108}
109
2b5f62a0
VZ
110
111static char hexArray[] = "0123456789ABCDEF";
112
113static void DecToHex(int dec, char *buf)
114{
115 int firstDigit = (int)(dec/16.0);
116 int secondDigit = (int)(dec - (firstDigit*16.0));
117 buf[0] = hexArray[firstDigit];
118 buf[1] = hexArray[secondDigit];
119 buf[2] = 0;
120}
121
122
c96ea657 123bool wxXPMHandler::SaveFile(wxImage * image,
33ac7e6f 124 wxOutputStream& stream, bool WXUNUSED(verbose))
c96ea657
VS
125{
126 wxString tmp;
127 char tmp_c;
33ac7e6f 128
c96ea657
VS
129 // 1. count colours:
130 #define MaxCixels 92
33ac7e6f 131 static const char Cixel[MaxCixels+1] =
c96ea657
VS
132 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
133 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
134 int chars_per_pixel;
135 int cols;
136 int i, j, k;
33ac7e6f 137
c96ea657
VS
138 cols = image->CountColours();
139 chars_per_pixel = 1;
140 for ( k = MaxCixels; cols > k; k *= MaxCixels)
141 chars_per_pixel++;
142
7beb59f3 143 // 2. write the header:
fd94e8aa
VS
144 wxString sName;
145 if ( image->HasOption(wxIMAGE_OPTION_FILENAME) )
146 {
147 wxSplitPath(image->GetOption(wxIMAGE_OPTION_FILENAME),
148 NULL, &sName, NULL);
149 sName << wxT("_xpm");
150 }
7beb59f3 151
fd94e8aa
VS
152 if ( !sName.IsEmpty() )
153 sName = wxString(wxT("/* XPM */\nstatic char *")) + sName;
7beb59f3 154 else
fd94e8aa 155 sName = wxT("/* XPM */\nstatic char *xpm_data");
2b5f62a0 156 stream.Write( (const char*) sName.ToAscii(), sName.Len() );
fd94e8aa 157
ffe107c8 158 char tmpbuf[200];
2ef44ad5 159 // VS: 200b is safe upper bound for anything produced by sprintf below
fd94e8aa 160 // (<101 bytes the string, neither %i can expand into more than 10 chars)
7beb59f3 161 sprintf(tmpbuf,
fd94e8aa 162 "[] = {\n"
c96ea657 163 "/* columns rows colors chars-per-pixel */\n"
33ac7e6f 164 "\"%i %i %i %i\",\n",
c96ea657 165 image->GetWidth(), image->GetHeight(), cols, chars_per_pixel);
ffe107c8 166 stream.Write(tmpbuf, strlen(tmpbuf));
c96ea657
VS
167
168 // 3. create color symbols table:
952ae1e8
VS
169 wxImageHistogram histogram;
170 image->ComputeHistogram(histogram);
c96ea657
VS
171
172 char *symbols_data = new char[cols * (chars_per_pixel+1)];
173 char **symbols = new char*[cols];
174
175 // 2a. find mask colour:
952ae1e8 176 unsigned long mask_key = 0x1000000 /*invalid RGB value*/;
c96ea657
VS
177 if (image->HasMask())
178 mask_key = (image->GetMaskRed() << 16) |
179 (image->GetMaskGreen() << 8) | image->GetMaskBlue();
33ac7e6f 180
c96ea657 181 // 2b. generate colour table:
952ae1e8 182 for (wxImageHistogram::iterator entry = histogram.begin();
60d8e886 183 entry != histogram.end(); ++entry )
c96ea657 184 {
952ae1e8 185 unsigned long index = entry->second.index;
c96ea657
VS
186 symbols[index] = symbols_data + index * (chars_per_pixel+1);
187 char *sym = symbols[index];
188
189 k = index % MaxCixels;
190 sym[0] = Cixel[k];
191 for (j = 1; j < chars_per_pixel; j++)
192 {
193 k = ((index - k) / MaxCixels) % MaxCixels;
194 sym[j] = Cixel[k];
195 }
196 sym[j] = '\0';
197
952ae1e8 198 unsigned long key = entry->first;
c96ea657
VS
199
200 if (key == 0)
2b5f62a0 201 sprintf( tmpbuf, "\"%s c Black\",\n", sym);
c96ea657 202 else if (key == mask_key)
2b5f62a0 203 sprintf( tmpbuf, "\"%s c None\",\n", sym);
c96ea657 204 else
2b5f62a0
VZ
205 {
206 char rbuf[3];
207 DecToHex( (unsigned char)(key >> 16), rbuf );
208 char gbuf[3];
209 DecToHex( (unsigned char)(key >> 8), gbuf );
210 char bbuf[3];
211 DecToHex( (unsigned char)(key), bbuf );
212 sprintf( tmpbuf, "\"%s c #%s%s%s\",\n", sym, rbuf, gbuf, bbuf );
213 }
214 stream.Write( tmpbuf, strlen(tmpbuf) );
c96ea657
VS
215 }
216
217 tmp = wxT("/* pixels */\n");
2b5f62a0 218 stream.Write( (const char*) tmp.ToAscii(), tmp.Length() );
c96ea657
VS
219
220 unsigned char *data = image->GetData();
221 for (j = 0; j < image->GetHeight(); j++)
222 {
223 tmp_c = '\"'; stream.Write(&tmp_c, 1);
224 for (i = 0; i < image->GetWidth(); i++, data += 3)
225 {
226 unsigned long key = (data[0] << 16) | (data[1] << 8) | (data[2]);
952ae1e8 227 stream.Write(symbols[histogram[key].index], chars_per_pixel);
c96ea657
VS
228 }
229 tmp_c = '\"'; stream.Write(&tmp_c, 1);
230 if ( j + 1 < image->GetHeight() )
231 {
232 tmp_c = ','; stream.Write(&tmp_c, 1);
233 }
234 tmp_c = '\n'; stream.Write(&tmp_c, 1);
235 }
236 tmp = wxT("};\n");
2b5f62a0 237 stream.Write( (const char*) tmp.ToAscii(), 3 );
33ac7e6f 238
2ef44ad5 239 // Clean up:
c96ea657
VS
240 delete[] symbols;
241 delete[] symbols_data;
242
7beb59f3 243 return true;
c96ea657
VS
244}
245
246bool wxXPMHandler::DoCanRead(wxInputStream& stream)
247{
424f5e27
VS
248 wxXPMDecoder decoder;
249 return decoder.CanRead(stream);
c96ea657
VS
250}
251
252#endif // wxUSE_STREAMS
253
c67d6888 254#endif // wxUSE_XPM