]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagxpm.cpp
updated setup.h for OpenVMS
[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"
bd365871 83#include "wx/filename.h"
c96ea657
VS
84
85IMPLEMENT_DYNAMIC_CLASS(wxXPMHandler,wxImageHandler)
86
87//-----------------------------------------------------------------------------
88// wxXPMHandler
89//-----------------------------------------------------------------------------
90
91#if wxUSE_STREAMS
92
33ac7e6f 93bool wxXPMHandler::LoadFile(wxImage *image,
424f5e27 94 wxInputStream& stream,
38caaa61 95 bool WXUNUSED(verbose), int WXUNUSED(index))
c96ea657 96{
424f5e27
VS
97 wxXPMDecoder decoder;
98
99 wxImage img = decoder.ReadFile(stream);
a1b806b9 100 if ( !img.IsOk() )
7beb59f3 101 return false;
424f5e27 102 *image = img;
7beb59f3 103 return true;
c96ea657
VS
104}
105
8b23d3b0
VZ
106namespace
107{
108
109// Make the given string a valid C identifier.
110//
111// All invalid characters are simply replaced by underscores and underscore is
112// also prepended in the beginning if the initial character is not alphabetic.
113void
114MakeValidCIdent(wxString* str)
115{
116 const wxChar chUnderscore = wxT('_');
117
118 for ( wxString::iterator it = str->begin(); it != str->end(); ++it )
119 {
120 const wxChar ch = *it;
121 if ( wxIsdigit(ch) )
122 {
123 if ( it == str->begin() )
124 {
125 // Identifiers can't start with a digit.
126 str->insert(0, chUnderscore); // prepend underscore
127 it = str->begin(); // restart as string changed
128 continue;
129 }
130 }
131 else if ( !wxIsalpha(ch) && ch != chUnderscore )
132 {
133 // Not a valid character in C identifiers.
134 *it = chUnderscore;
135 }
136 }
137
138 // Double underscores are not allowed in normal C identifiers and are
139 // useless anyhow.
140 str->Replace(wxT("__"), wxT("_"));
141}
142
143} // anonymous namespace
144
c96ea657 145bool wxXPMHandler::SaveFile(wxImage * image,
33ac7e6f 146 wxOutputStream& stream, bool WXUNUSED(verbose))
c96ea657 147{
c96ea657
VS
148 // 1. count colours:
149 #define MaxCixels 92
33ac7e6f 150 static const char Cixel[MaxCixels+1] =
c96ea657
VS
151 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
152 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
c96ea657 153 int i, j, k;
33ac7e6f 154
2eaf6433
PC
155 wxImageHistogram histogram;
156 int cols = int(image->ComputeHistogram(histogram));
157
158 int chars_per_pixel = 1;
c96ea657
VS
159 for ( k = MaxCixels; cols > k; k *= MaxCixels)
160 chars_per_pixel++;
161
7beb59f3 162 // 2. write the header:
fd94e8aa
VS
163 wxString sName;
164 if ( image->HasOption(wxIMAGE_OPTION_FILENAME) )
165 {
8b23d3b0
VZ
166 sName = wxFileName(image->GetOption(wxIMAGE_OPTION_FILENAME)).GetName();
167 MakeValidCIdent(&sName);
fd94e8aa
VS
168 sName << wxT("_xpm");
169 }
7beb59f3 170
8898456d 171 if ( !sName.empty() )
90e4fb5d 172 sName = wxString(wxT("/* XPM */\nstatic const char *")) + sName;
7beb59f3 173 else
90e4fb5d 174 sName = wxT("/* XPM */\nstatic const char *xpm_data");
2b5f62a0 175 stream.Write( (const char*) sName.ToAscii(), sName.Len() );
fd94e8aa 176
ffe107c8 177 char tmpbuf[200];
2ef44ad5 178 // VS: 200b is safe upper bound for anything produced by sprintf below
fd94e8aa 179 // (<101 bytes the string, neither %i can expand into more than 10 chars)
7beb59f3 180 sprintf(tmpbuf,
fd94e8aa 181 "[] = {\n"
c96ea657 182 "/* columns rows colors chars-per-pixel */\n"
33ac7e6f 183 "\"%i %i %i %i\",\n",
c96ea657 184 image->GetWidth(), image->GetHeight(), cols, chars_per_pixel);
ffe107c8 185 stream.Write(tmpbuf, strlen(tmpbuf));
c96ea657
VS
186
187 // 3. create color symbols table:
c96ea657
VS
188 char *symbols_data = new char[cols * (chars_per_pixel+1)];
189 char **symbols = new char*[cols];
190
191 // 2a. find mask colour:
952ae1e8 192 unsigned long mask_key = 0x1000000 /*invalid RGB value*/;
c96ea657
VS
193 if (image->HasMask())
194 mask_key = (image->GetMaskRed() << 16) |
195 (image->GetMaskGreen() << 8) | image->GetMaskBlue();
33ac7e6f 196
c96ea657 197 // 2b. generate colour table:
952ae1e8 198 for (wxImageHistogram::iterator entry = histogram.begin();
60d8e886 199 entry != histogram.end(); ++entry )
c96ea657 200 {
952ae1e8 201 unsigned long index = entry->second.index;
c96ea657
VS
202 symbols[index] = symbols_data + index * (chars_per_pixel+1);
203 char *sym = symbols[index];
204
0200fb27 205 for (j = 0; j < chars_per_pixel; j++)
c96ea657 206 {
0200fb27
PC
207 sym[j] = Cixel[index % MaxCixels];
208 index /= MaxCixels;
c96ea657
VS
209 }
210 sym[j] = '\0';
211
952ae1e8 212 unsigned long key = entry->first;
c96ea657
VS
213
214 if (key == 0)
2b5f62a0 215 sprintf( tmpbuf, "\"%s c Black\",\n", sym);
c96ea657 216 else if (key == mask_key)
2b5f62a0 217 sprintf( tmpbuf, "\"%s c None\",\n", sym);
c96ea657 218 else
2b5f62a0 219 {
2eaf6433
PC
220 wxByte r = wxByte(key >> 16);
221 wxByte g = wxByte(key >> 8);
222 wxByte b = wxByte(key);
223 sprintf(tmpbuf, "\"%s c #%02X%02X%02X\",\n", sym, r, g, b);
2b5f62a0
VZ
224 }
225 stream.Write( tmpbuf, strlen(tmpbuf) );
c96ea657
VS
226 }
227
2eaf6433 228 stream.Write("/* pixels */\n", 13);
c96ea657
VS
229
230 unsigned char *data = image->GetData();
231 for (j = 0; j < image->GetHeight(); j++)
232 {
2eaf6433 233 char tmp_c;
c96ea657
VS
234 tmp_c = '\"'; stream.Write(&tmp_c, 1);
235 for (i = 0; i < image->GetWidth(); i++, data += 3)
236 {
237 unsigned long key = (data[0] << 16) | (data[1] << 8) | (data[2]);
952ae1e8 238 stream.Write(symbols[histogram[key].index], chars_per_pixel);
c96ea657
VS
239 }
240 tmp_c = '\"'; stream.Write(&tmp_c, 1);
241 if ( j + 1 < image->GetHeight() )
242 {
243 tmp_c = ','; stream.Write(&tmp_c, 1);
244 }
245 tmp_c = '\n'; stream.Write(&tmp_c, 1);
246 }
2eaf6433 247 stream.Write("};\n", 3 );
33ac7e6f 248
2ef44ad5 249 // Clean up:
c96ea657
VS
250 delete[] symbols;
251 delete[] symbols_data;
252
7beb59f3 253 return true;
c96ea657
VS
254}
255
256bool wxXPMHandler::DoCanRead(wxInputStream& stream)
257{
424f5e27
VS
258 wxXPMDecoder decoder;
259 return decoder.CanRead(stream);
8faef7cc 260 // it's ok to modify the stream position here
c96ea657
VS
261}
262
263#endif // wxUSE_STREAMS
264
c67d6888 265#endif // wxUSE_XPM