]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagxpm.cpp
Minor header cleaning.
[wxWidgets.git] / src / common / imagxpm.cpp
CommitLineData
c96ea657 1/////////////////////////////////////////////////////////////////////////////
8898456d 2// Name: src/common/imagxpm.cpp
c96ea657 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
c96ea657
VS
65// For compilers that support precompilation, includes "wx.h".
66#include "wx/wxprec.h"
67
68#ifdef __BORLANDC__
8898456d 69 #pragma hdrstop
c96ea657
VS
70#endif
71
8898456d
WS
72#if wxUSE_XPM
73
c96ea657 74#ifndef WX_PRECOMP
8898456d
WS
75 #include "wx/log.h"
76 #include "wx/intl.h"
de6185e2 77 #include "wx/utils.h"
c96ea657
VS
78#endif
79
c96ea657
VS
80#include "wx/imagxpm.h"
81#include "wx/wfstream.h"
424f5e27 82#include "wx/xpmdecod.h"
c96ea657
VS
83
84IMPLEMENT_DYNAMIC_CLASS(wxXPMHandler,wxImageHandler)
85
86//-----------------------------------------------------------------------------
87// wxXPMHandler
88//-----------------------------------------------------------------------------
89
90#if wxUSE_STREAMS
91
33ac7e6f 92bool wxXPMHandler::LoadFile(wxImage *image,
424f5e27 93 wxInputStream& stream,
38caaa61 94 bool WXUNUSED(verbose), int WXUNUSED(index))
c96ea657 95{
424f5e27
VS
96 wxXPMDecoder decoder;
97
98 wxImage img = decoder.ReadFile(stream);
99 if ( !img.Ok() )
7beb59f3 100 return false;
424f5e27 101 *image = img;
7beb59f3 102 return true;
c96ea657
VS
103}
104
2b5f62a0
VZ
105
106static char hexArray[] = "0123456789ABCDEF";
107
108static void DecToHex(int dec, char *buf)
109{
110 int firstDigit = (int)(dec/16.0);
111 int secondDigit = (int)(dec - (firstDigit*16.0));
112 buf[0] = hexArray[firstDigit];
113 buf[1] = hexArray[secondDigit];
114 buf[2] = 0;
115}
116
117
c96ea657 118bool wxXPMHandler::SaveFile(wxImage * image,
33ac7e6f 119 wxOutputStream& stream, bool WXUNUSED(verbose))
c96ea657
VS
120{
121 wxString tmp;
122 char tmp_c;
33ac7e6f 123
c96ea657
VS
124 // 1. count colours:
125 #define MaxCixels 92
33ac7e6f 126 static const char Cixel[MaxCixels+1] =
c96ea657
VS
127 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
128 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
129 int chars_per_pixel;
130 int cols;
131 int i, j, k;
33ac7e6f 132
c96ea657
VS
133 cols = image->CountColours();
134 chars_per_pixel = 1;
135 for ( k = MaxCixels; cols > k; k *= MaxCixels)
136 chars_per_pixel++;
137
7beb59f3 138 // 2. write the header:
fd94e8aa
VS
139 wxString sName;
140 if ( image->HasOption(wxIMAGE_OPTION_FILENAME) )
141 {
142 wxSplitPath(image->GetOption(wxIMAGE_OPTION_FILENAME),
143 NULL, &sName, NULL);
144 sName << wxT("_xpm");
145 }
7beb59f3 146
8898456d 147 if ( !sName.empty() )
fd94e8aa 148 sName = wxString(wxT("/* XPM */\nstatic char *")) + sName;
7beb59f3 149 else
fd94e8aa 150 sName = wxT("/* XPM */\nstatic char *xpm_data");
2b5f62a0 151 stream.Write( (const char*) sName.ToAscii(), sName.Len() );
fd94e8aa 152
ffe107c8 153 char tmpbuf[200];
2ef44ad5 154 // VS: 200b is safe upper bound for anything produced by sprintf below
fd94e8aa 155 // (<101 bytes the string, neither %i can expand into more than 10 chars)
7beb59f3 156 sprintf(tmpbuf,
fd94e8aa 157 "[] = {\n"
c96ea657 158 "/* columns rows colors chars-per-pixel */\n"
33ac7e6f 159 "\"%i %i %i %i\",\n",
c96ea657 160 image->GetWidth(), image->GetHeight(), cols, chars_per_pixel);
ffe107c8 161 stream.Write(tmpbuf, strlen(tmpbuf));
c96ea657
VS
162
163 // 3. create color symbols table:
952ae1e8
VS
164 wxImageHistogram histogram;
165 image->ComputeHistogram(histogram);
c96ea657
VS
166
167 char *symbols_data = new char[cols * (chars_per_pixel+1)];
168 char **symbols = new char*[cols];
169
170 // 2a. find mask colour:
952ae1e8 171 unsigned long mask_key = 0x1000000 /*invalid RGB value*/;
c96ea657
VS
172 if (image->HasMask())
173 mask_key = (image->GetMaskRed() << 16) |
174 (image->GetMaskGreen() << 8) | image->GetMaskBlue();
33ac7e6f 175
c96ea657 176 // 2b. generate colour table:
952ae1e8 177 for (wxImageHistogram::iterator entry = histogram.begin();
60d8e886 178 entry != histogram.end(); ++entry )
c96ea657 179 {
952ae1e8 180 unsigned long index = entry->second.index;
c96ea657
VS
181 symbols[index] = symbols_data + index * (chars_per_pixel+1);
182 char *sym = symbols[index];
183
184 k = index % MaxCixels;
185 sym[0] = Cixel[k];
186 for (j = 1; j < chars_per_pixel; j++)
187 {
188 k = ((index - k) / MaxCixels) % MaxCixels;
189 sym[j] = Cixel[k];
190 }
191 sym[j] = '\0';
192
952ae1e8 193 unsigned long key = entry->first;
c96ea657
VS
194
195 if (key == 0)
2b5f62a0 196 sprintf( tmpbuf, "\"%s c Black\",\n", sym);
c96ea657 197 else if (key == mask_key)
2b5f62a0 198 sprintf( tmpbuf, "\"%s c None\",\n", sym);
c96ea657 199 else
2b5f62a0
VZ
200 {
201 char rbuf[3];
202 DecToHex( (unsigned char)(key >> 16), rbuf );
203 char gbuf[3];
204 DecToHex( (unsigned char)(key >> 8), gbuf );
205 char bbuf[3];
206 DecToHex( (unsigned char)(key), bbuf );
207 sprintf( tmpbuf, "\"%s c #%s%s%s\",\n", sym, rbuf, gbuf, bbuf );
208 }
209 stream.Write( tmpbuf, strlen(tmpbuf) );
c96ea657
VS
210 }
211
212 tmp = wxT("/* pixels */\n");
8898456d 213 stream.Write( (const char*) tmp.ToAscii(), tmp.length() );
c96ea657
VS
214
215 unsigned char *data = image->GetData();
216 for (j = 0; j < image->GetHeight(); j++)
217 {
218 tmp_c = '\"'; stream.Write(&tmp_c, 1);
219 for (i = 0; i < image->GetWidth(); i++, data += 3)
220 {
221 unsigned long key = (data[0] << 16) | (data[1] << 8) | (data[2]);
952ae1e8 222 stream.Write(symbols[histogram[key].index], chars_per_pixel);
c96ea657
VS
223 }
224 tmp_c = '\"'; stream.Write(&tmp_c, 1);
225 if ( j + 1 < image->GetHeight() )
226 {
227 tmp_c = ','; stream.Write(&tmp_c, 1);
228 }
229 tmp_c = '\n'; stream.Write(&tmp_c, 1);
230 }
231 tmp = wxT("};\n");
2b5f62a0 232 stream.Write( (const char*) tmp.ToAscii(), 3 );
33ac7e6f 233
2ef44ad5 234 // Clean up:
c96ea657
VS
235 delete[] symbols;
236 delete[] symbols_data;
237
7beb59f3 238 return true;
c96ea657
VS
239}
240
241bool wxXPMHandler::DoCanRead(wxInputStream& stream)
242{
424f5e27
VS
243 wxXPMDecoder decoder;
244 return decoder.CanRead(stream);
c96ea657
VS
245}
246
247#endif // wxUSE_STREAMS
248
c67d6888 249#endif // wxUSE_XPM