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