document that CanRead() and GetImageCount() functions of wxImageHandlers do NOT modif...
[wxWidgets.git] / src / common / imagxpm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagxpm.cpp
3 // Purpose: wxXPMHandler
4 // Author: Vaclav Slavik, Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) 2001 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 /*
11
12 This file is partially based on source code of ImageMagick by John Cristy. Its
13 license 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
65 // For compilers that support precompilation, includes "wx.h".
66 #include "wx/wxprec.h"
67
68 #ifdef __BORLANDC__
69 #pragma hdrstop
70 #endif
71
72 #if wxUSE_XPM
73
74 #ifndef WX_PRECOMP
75 #include "wx/log.h"
76 #include "wx/intl.h"
77 #include "wx/utils.h"
78 #endif
79
80 #include "wx/imagxpm.h"
81 #include "wx/wfstream.h"
82 #include "wx/xpmdecod.h"
83 #include "wx/filename.h"
84
85 IMPLEMENT_DYNAMIC_CLASS(wxXPMHandler,wxImageHandler)
86
87 //-----------------------------------------------------------------------------
88 // wxXPMHandler
89 //-----------------------------------------------------------------------------
90
91 #if wxUSE_STREAMS
92
93 bool wxXPMHandler::LoadFile(wxImage *image,
94 wxInputStream& stream,
95 bool WXUNUSED(verbose), int WXUNUSED(index))
96 {
97 wxXPMDecoder decoder;
98
99 wxImage img = decoder.ReadFile(stream);
100 if ( !img.Ok() )
101 return false;
102 *image = img;
103 return true;
104 }
105
106 bool wxXPMHandler::SaveFile(wxImage * image,
107 wxOutputStream& stream, bool WXUNUSED(verbose))
108 {
109 // 1. count colours:
110 #define MaxCixels 92
111 static const char Cixel[MaxCixels+1] =
112 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
113 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
114 int i, j, k;
115
116 wxImageHistogram histogram;
117 int cols = int(image->ComputeHistogram(histogram));
118
119 int chars_per_pixel = 1;
120 for ( k = MaxCixels; cols > k; k *= MaxCixels)
121 chars_per_pixel++;
122
123 // 2. write the header:
124 wxString sName;
125 if ( image->HasOption(wxIMAGE_OPTION_FILENAME) )
126 {
127 wxFileName::SplitPath(image->GetOption(wxIMAGE_OPTION_FILENAME),
128 NULL, &sName, NULL);
129 sName << wxT("_xpm");
130 }
131
132 if ( !sName.empty() )
133 sName = wxString(wxT("/* XPM */\nstatic const char *")) + sName;
134 else
135 sName = wxT("/* XPM */\nstatic const char *xpm_data");
136 stream.Write( (const char*) sName.ToAscii(), sName.Len() );
137
138 char tmpbuf[200];
139 // VS: 200b is safe upper bound for anything produced by sprintf below
140 // (<101 bytes the string, neither %i can expand into more than 10 chars)
141 sprintf(tmpbuf,
142 "[] = {\n"
143 "/* columns rows colors chars-per-pixel */\n"
144 "\"%i %i %i %i\",\n",
145 image->GetWidth(), image->GetHeight(), cols, chars_per_pixel);
146 stream.Write(tmpbuf, strlen(tmpbuf));
147
148 // 3. create color symbols table:
149 char *symbols_data = new char[cols * (chars_per_pixel+1)];
150 char **symbols = new char*[cols];
151
152 // 2a. find mask colour:
153 unsigned long mask_key = 0x1000000 /*invalid RGB value*/;
154 if (image->HasMask())
155 mask_key = (image->GetMaskRed() << 16) |
156 (image->GetMaskGreen() << 8) | image->GetMaskBlue();
157
158 // 2b. generate colour table:
159 for (wxImageHistogram::iterator entry = histogram.begin();
160 entry != histogram.end(); ++entry )
161 {
162 unsigned long index = entry->second.index;
163 symbols[index] = symbols_data + index * (chars_per_pixel+1);
164 char *sym = symbols[index];
165
166 for (j = 0; j < chars_per_pixel; j++)
167 {
168 sym[j] = Cixel[index % MaxCixels];
169 index /= MaxCixels;
170 }
171 sym[j] = '\0';
172
173 unsigned long key = entry->first;
174
175 if (key == 0)
176 sprintf( tmpbuf, "\"%s c Black\",\n", sym);
177 else if (key == mask_key)
178 sprintf( tmpbuf, "\"%s c None\",\n", sym);
179 else
180 {
181 wxByte r = wxByte(key >> 16);
182 wxByte g = wxByte(key >> 8);
183 wxByte b = wxByte(key);
184 sprintf(tmpbuf, "\"%s c #%02X%02X%02X\",\n", sym, r, g, b);
185 }
186 stream.Write( tmpbuf, strlen(tmpbuf) );
187 }
188
189 stream.Write("/* pixels */\n", 13);
190
191 unsigned char *data = image->GetData();
192 for (j = 0; j < image->GetHeight(); j++)
193 {
194 char tmp_c;
195 tmp_c = '\"'; stream.Write(&tmp_c, 1);
196 for (i = 0; i < image->GetWidth(); i++, data += 3)
197 {
198 unsigned long key = (data[0] << 16) | (data[1] << 8) | (data[2]);
199 stream.Write(symbols[histogram[key].index], chars_per_pixel);
200 }
201 tmp_c = '\"'; stream.Write(&tmp_c, 1);
202 if ( j + 1 < image->GetHeight() )
203 {
204 tmp_c = ','; stream.Write(&tmp_c, 1);
205 }
206 tmp_c = '\n'; stream.Write(&tmp_c, 1);
207 }
208 stream.Write("};\n", 3 );
209
210 // Clean up:
211 delete[] symbols;
212 delete[] symbols_data;
213
214 return true;
215 }
216
217 bool wxXPMHandler::DoCanRead(wxInputStream& stream)
218 {
219 wxXPMDecoder decoder;
220 return decoder.CanRead(stream);
221 // it's ok to modify the stream position here
222 }
223
224 #endif // wxUSE_STREAMS
225
226 #endif // wxUSE_XPM