]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagxpm.cpp
moved wxapp_install_idle_handler and g_isIdle from many cpp files into gtk/private...
[wxWidgets.git] / src / common / imagxpm.cpp
CommitLineData
c96ea657
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: imagxpm.cpp
3// Purpose: wxXPMHandler
424f5e27 4// Author: Vaclav Slavik, Robert Roebling
c96ea657
VS
5// RCS-ID: $Id$
6// Copyright: (c) 2001 Vaclav Slavik
65571936 7// Licence: wxWindows licence
c96ea657
VS
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
c96ea657
VS
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
c67d6888 76#if wxUSE_XPM
c96ea657
VS
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"
424f5e27 83#include "wx/xpmdecod.h"
c96ea657
VS
84
85IMPLEMENT_DYNAMIC_CLASS(wxXPMHandler,wxImageHandler)
86
87//-----------------------------------------------------------------------------
88// wxXPMHandler
89//-----------------------------------------------------------------------------
90
91#if wxUSE_STREAMS
92
33ac7e6f 93bool wxXPMHandler::LoadFile(wxImage *image,
424f5e27 94 wxInputStream& stream,
38caaa61 95 bool WXUNUSED(verbose), int WXUNUSED(index))
c96ea657 96{
424f5e27
VS
97 wxXPMDecoder decoder;
98
99 wxImage img = decoder.ReadFile(stream);
100 if ( !img.Ok() )
7beb59f3 101 return false;
424f5e27 102 *image = img;
7beb59f3 103 return true;
c96ea657
VS
104}
105
2b5f62a0
VZ
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
c96ea657 119bool wxXPMHandler::SaveFile(wxImage * image,
33ac7e6f 120 wxOutputStream& stream, bool WXUNUSED(verbose))
c96ea657
VS
121{
122 wxString tmp;
123 char tmp_c;
33ac7e6f 124
c96ea657
VS
125 // 1. count colours:
126 #define MaxCixels 92
33ac7e6f 127 static const char Cixel[MaxCixels+1] =
c96ea657
VS
128 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
129 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
130 int chars_per_pixel;
131 int cols;
132 int i, j, k;
33ac7e6f 133
c96ea657
VS
134 cols = image->CountColours();
135 chars_per_pixel = 1;
136 for ( k = MaxCixels; cols > k; k *= MaxCixels)
137 chars_per_pixel++;
138
7beb59f3 139 // 2. write the header:
fd94e8aa
VS
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 }
7beb59f3 147
fd94e8aa
VS
148 if ( !sName.IsEmpty() )
149 sName = wxString(wxT("/* XPM */\nstatic char *")) + sName;
7beb59f3 150 else
fd94e8aa 151 sName = wxT("/* XPM */\nstatic char *xpm_data");
2b5f62a0 152 stream.Write( (const char*) sName.ToAscii(), sName.Len() );
fd94e8aa 153
ffe107c8 154 char tmpbuf[200];
2ef44ad5 155 // VS: 200b is safe upper bound for anything produced by sprintf below
fd94e8aa 156 // (<101 bytes the string, neither %i can expand into more than 10 chars)
7beb59f3 157 sprintf(tmpbuf,
fd94e8aa 158 "[] = {\n"
c96ea657 159 "/* columns rows colors chars-per-pixel */\n"
33ac7e6f 160 "\"%i %i %i %i\",\n",
c96ea657 161 image->GetWidth(), image->GetHeight(), cols, chars_per_pixel);
ffe107c8 162 stream.Write(tmpbuf, strlen(tmpbuf));
c96ea657
VS
163
164 // 3. create color symbols table:
952ae1e8
VS
165 wxImageHistogram histogram;
166 image->ComputeHistogram(histogram);
c96ea657
VS
167
168 char *symbols_data = new char[cols * (chars_per_pixel+1)];
169 char **symbols = new char*[cols];
170
171 // 2a. find mask colour:
952ae1e8 172 unsigned long mask_key = 0x1000000 /*invalid RGB value*/;
c96ea657
VS
173 if (image->HasMask())
174 mask_key = (image->GetMaskRed() << 16) |
175 (image->GetMaskGreen() << 8) | image->GetMaskBlue();
33ac7e6f 176
c96ea657 177 // 2b. generate colour table:
952ae1e8 178 for (wxImageHistogram::iterator entry = histogram.begin();
60d8e886 179 entry != histogram.end(); ++entry )
c96ea657 180 {
952ae1e8 181 unsigned long index = entry->second.index;
c96ea657
VS
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
952ae1e8 194 unsigned long key = entry->first;
c96ea657
VS
195
196 if (key == 0)
2b5f62a0 197 sprintf( tmpbuf, "\"%s c Black\",\n", sym);
c96ea657 198 else if (key == mask_key)
2b5f62a0 199 sprintf( tmpbuf, "\"%s c None\",\n", sym);
c96ea657 200 else
2b5f62a0
VZ
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) );
c96ea657
VS
211 }
212
213 tmp = wxT("/* pixels */\n");
2b5f62a0 214 stream.Write( (const char*) tmp.ToAscii(), tmp.Length() );
c96ea657
VS
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]);
952ae1e8 223 stream.Write(symbols[histogram[key].index], chars_per_pixel);
c96ea657
VS
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");
2b5f62a0 233 stream.Write( (const char*) tmp.ToAscii(), 3 );
33ac7e6f 234
2ef44ad5 235 // Clean up:
c96ea657
VS
236 delete[] symbols;
237 delete[] symbols_data;
238
7beb59f3 239 return true;
c96ea657
VS
240}
241
242bool wxXPMHandler::DoCanRead(wxInputStream& stream)
243{
424f5e27
VS
244 wxXPMDecoder decoder;
245 return decoder.CanRead(stream);
c96ea657
VS
246}
247
248#endif // wxUSE_STREAMS
249
c67d6888 250#endif // wxUSE_XPM