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