]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/imagxpm.cpp
support for unicode font names
[wxWidgets.git] / src / common / imagxpm.cpp
... / ...
CommitLineData
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
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
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#ifndef WX_PRECOMP
73# include "wx/defs.h"
74#endif
75
76#if wxUSE_XPM
77
78#include "wx/imagxpm.h"
79#include "wx/wfstream.h"
80#include "wx/log.h"
81#include "wx/intl.h"
82#include "wx/utils.h"
83#include "wx/xpmdecod.h"
84
85IMPLEMENT_DYNAMIC_CLASS(wxXPMHandler,wxImageHandler)
86
87//-----------------------------------------------------------------------------
88// wxXPMHandler
89//-----------------------------------------------------------------------------
90
91#if wxUSE_STREAMS
92
93bool 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
107static char hexArray[] = "0123456789ABCDEF";
108
109static void DecToHex(int dec, char *buf)
110{
111 int firstDigit = (int)(dec/16.0);
112 int secondDigit = (int)(dec - (firstDigit*16.0));
113 buf[0] = hexArray[firstDigit];
114 buf[1] = hexArray[secondDigit];
115 buf[2] = 0;
116}
117
118
119bool wxXPMHandler::SaveFile(wxImage * image,
120 wxOutputStream& stream, bool WXUNUSED(verbose))
121{
122 wxString tmp;
123 char tmp_c;
124
125 // 1. count colours:
126 #define MaxCixels 92
127 static const char Cixel[MaxCixels+1] =
128 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
129 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
130 int chars_per_pixel;
131 int cols;
132 int i, j, k;
133
134 cols = image->CountColours();
135 chars_per_pixel = 1;
136 for ( k = MaxCixels; cols > k; k *= MaxCixels)
137 chars_per_pixel++;
138
139 // 2. write the header:
140 wxString sName;
141 if ( image->HasOption(wxIMAGE_OPTION_FILENAME) )
142 {
143 wxSplitPath(image->GetOption(wxIMAGE_OPTION_FILENAME),
144 NULL, &sName, NULL);
145 sName << wxT("_xpm");
146 }
147
148 if ( !sName.IsEmpty() )
149 sName = wxString(wxT("/* XPM */\nstatic char *")) + sName;
150 else
151 sName = wxT("/* XPM */\nstatic char *xpm_data");
152 stream.Write( (const char*) sName.ToAscii(), sName.Len() );
153
154 char tmpbuf[200];
155 // VS: 200b is safe upper bound for anything produced by sprintf below
156 // (<101 bytes the string, neither %i can expand into more than 10 chars)
157 sprintf(tmpbuf,
158 "[] = {\n"
159 "/* columns rows colors chars-per-pixel */\n"
160 "\"%i %i %i %i\",\n",
161 image->GetWidth(), image->GetHeight(), cols, chars_per_pixel);
162 stream.Write(tmpbuf, strlen(tmpbuf));
163
164 // 3. create color symbols table:
165 wxImageHistogram histogram;
166 image->ComputeHistogram(histogram);
167
168 char *symbols_data = new char[cols * (chars_per_pixel+1)];
169 char **symbols = new char*[cols];
170
171 // 2a. find mask colour:
172 unsigned long mask_key = 0x1000000 /*invalid RGB value*/;
173 if (image->HasMask())
174 mask_key = (image->GetMaskRed() << 16) |
175 (image->GetMaskGreen() << 8) | image->GetMaskBlue();
176
177 // 2b. generate colour table:
178 for (wxImageHistogram::iterator entry = histogram.begin();
179 entry != histogram.end(); ++entry )
180 {
181 unsigned long index = entry->second.index;
182 symbols[index] = symbols_data + index * (chars_per_pixel+1);
183 char *sym = symbols[index];
184
185 k = index % MaxCixels;
186 sym[0] = Cixel[k];
187 for (j = 1; j < chars_per_pixel; j++)
188 {
189 k = ((index - k) / MaxCixels) % MaxCixels;
190 sym[j] = Cixel[k];
191 }
192 sym[j] = '\0';
193
194 unsigned long key = entry->first;
195
196 if (key == 0)
197 sprintf( tmpbuf, "\"%s c Black\",\n", sym);
198 else if (key == mask_key)
199 sprintf( tmpbuf, "\"%s c None\",\n", sym);
200 else
201 {
202 char rbuf[3];
203 DecToHex( (unsigned char)(key >> 16), rbuf );
204 char gbuf[3];
205 DecToHex( (unsigned char)(key >> 8), gbuf );
206 char bbuf[3];
207 DecToHex( (unsigned char)(key), bbuf );
208 sprintf( tmpbuf, "\"%s c #%s%s%s\",\n", sym, rbuf, gbuf, bbuf );
209 }
210 stream.Write( tmpbuf, strlen(tmpbuf) );
211 }
212
213 tmp = wxT("/* pixels */\n");
214 stream.Write( (const char*) tmp.ToAscii(), tmp.Length() );
215
216 unsigned char *data = image->GetData();
217 for (j = 0; j < image->GetHeight(); j++)
218 {
219 tmp_c = '\"'; stream.Write(&tmp_c, 1);
220 for (i = 0; i < image->GetWidth(); i++, data += 3)
221 {
222 unsigned long key = (data[0] << 16) | (data[1] << 8) | (data[2]);
223 stream.Write(symbols[histogram[key].index], chars_per_pixel);
224 }
225 tmp_c = '\"'; stream.Write(&tmp_c, 1);
226 if ( j + 1 < image->GetHeight() )
227 {
228 tmp_c = ','; stream.Write(&tmp_c, 1);
229 }
230 tmp_c = '\n'; stream.Write(&tmp_c, 1);
231 }
232 tmp = wxT("};\n");
233 stream.Write( (const char*) tmp.ToAscii(), 3 );
234
235 // Clean up:
236 delete[] symbols;
237 delete[] symbols_data;
238
239 return true;
240}
241
242bool wxXPMHandler::DoCanRead(wxInputStream& stream)
243{
244 wxXPMDecoder decoder;
245 return decoder.CanRead(stream);
246}
247
248#endif // wxUSE_STREAMS
249
250#endif // wxUSE_XPM