]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagxpm.cpp
Minor mods
[wxWidgets.git] / src / common / imagxpm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: imagxpm.cpp
3 // Purpose: wxXPMHandler
4 // Author: Vaclav Slavik
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 #ifdef __GNUG__
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
80 #if wxUSE_IMAGE && wxUSE_XPM
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"
87
88
89 IMPLEMENT_DYNAMIC_CLASS(wxXPMHandler,wxImageHandler)
90
91 //-----------------------------------------------------------------------------
92 // wxXPMHandler
93 //-----------------------------------------------------------------------------
94
95 #if wxUSE_STREAMS
96
97 bool wxXPMHandler::LoadFile(wxImage *WXUNUSED(image),
98 wxInputStream& WXUNUSED(stream),
99 bool verbose, int WXUNUSED(index))
100 {
101 if (verbose)
102 wxLogDebug(_("XPM: the handler is write-only!"));
103 return FALSE;
104 }
105
106 bool wxXPMHandler::SaveFile(wxImage * image,
107 wxOutputStream& stream, bool verbose)
108 {
109 wxString tmp;
110 char tmp_c;
111
112 // 1. count colours:
113 #define MaxCixels 92
114 static const char Cixel[MaxCixels+1] =
115 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
116 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
117 int chars_per_pixel;
118 int cols;
119 int i, j, k;
120
121 cols = image->CountColours();
122 chars_per_pixel = 1;
123 for ( k = MaxCixels; cols > k; k *= MaxCixels)
124 chars_per_pixel++;
125
126 // 2. write the header:
127 tmp.Printf("/* XPM */\n"
128 "static char *xpm_data[] = {\n"
129 "/* columns rows colors chars-per-pixel */\n"
130 "\"%i %i %i %i\",\n",
131 image->GetWidth(), image->GetHeight(), cols, chars_per_pixel);
132 stream.Write(tmp.mb_str(), tmp.Length());
133
134 // 3. create color symbols table:
135 wxHashTable table(wxKEY_INTEGER);
136 image->ComputeHistogram(table);
137
138 char *symbols_data = new char[cols * (chars_per_pixel+1)];
139 char **symbols = new char*[cols];
140
141 // 2a. find mask colour:
142 long mask_key = -1;
143 if (image->HasMask())
144 mask_key = (image->GetMaskRed() << 16) |
145 (image->GetMaskGreen() << 8) | image->GetMaskBlue();
146
147 // 2b. generate colour table:
148 table.BeginFind();
149 wxNode *node = NULL;
150 while ((node = table.Next()) != NULL)
151 {
152 wxHNode *hnode = (wxHNode*) node->GetData();
153 long index = hnode->index;
154 symbols[index] = symbols_data + index * (chars_per_pixel+1);
155 char *sym = symbols[index];
156
157 k = index % MaxCixels;
158 sym[0] = Cixel[k];
159 for (j = 1; j < chars_per_pixel; j++)
160 {
161 k = ((index - k) / MaxCixels) % MaxCixels;
162 sym[j] = Cixel[k];
163 }
164 sym[j] = '\0';
165
166 long key = node->GetKeyInteger();
167
168 if (key == 0)
169 tmp.Printf(wxT("\"%s c Black\",\n"), sym);
170 else if (key == mask_key)
171 tmp.Printf(wxT("\"%s c None\",\n"), sym);
172 else
173 tmp.Printf(wxT("\"%s c #%s%s%s\",\n"), sym,
174 wxDecToHex((unsigned char)(key >> 16)).c_str(),
175 wxDecToHex((unsigned char)(key >> 8)).c_str(),
176 wxDecToHex((unsigned char)(key)).c_str());
177 stream.Write(tmp.mb_str(), tmp.Length());
178 }
179
180 tmp = wxT("/* pixels */\n");
181 stream.Write(tmp.mb_str(), tmp.Length());
182
183 unsigned char *data = image->GetData();
184 for (j = 0; j < image->GetHeight(); j++)
185 {
186 tmp_c = '\"'; stream.Write(&tmp_c, 1);
187 for (i = 0; i < image->GetWidth(); i++, data += 3)
188 {
189 unsigned long key = (data[0] << 16) | (data[1] << 8) | (data[2]);
190 wxHNode *hnode = (wxHNode*) table.Get(key);
191 stream.Write(symbols[hnode->index], chars_per_pixel);
192 }
193 tmp_c = '\"'; stream.Write(&tmp_c, 1);
194 if ( j + 1 < image->GetHeight() )
195 {
196 tmp_c = ','; stream.Write(&tmp_c, 1);
197 }
198 tmp_c = '\n'; stream.Write(&tmp_c, 1);
199 }
200 tmp = wxT("};\n");
201 stream.Write(tmp.mb_str(), 3);
202
203 delete[] symbols;
204 delete[] symbols_data;
205
206 return TRUE;
207 }
208
209 bool wxXPMHandler::DoCanRead(wxInputStream& stream)
210 {
211 unsigned char buf[9];
212
213 stream.Read(buf, 9);
214 stream.SeekI(-9, wxFromCurrent);
215
216 return (memcmp(buf, "/* XPM */", 9) == 0);
217 }
218
219 #endif // wxUSE_STREAMS
220
221 #endif // wxUSE_XPM && wxUSE_IMAGE