1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxXPMHandler
4 // Author: Vaclav Slavik, Robert Roebling
6 // Copyright: (c) 2001 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 This file is partially based on source code of ImageMagick by John Cristy. Its
13 license is as follows:
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
26 % Read/Write ImageMagick Image Format. %
34 % Copyright (C) 2001 ImageMagick Studio, a non-profit organization dedicated %
35 % to making software imaging solutions freely available. %
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: %
44 % The above copyright notice and this permission notice shall be included in %
45 % all copies or substantial portions of ImageMagick. %
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 %
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. %
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
66 #pragma implementation "imagxpm.h"
69 // For compilers that support precompilation, includes "wx.h".
70 #include "wx/wxprec.h"
82 #include "wx/imagxpm.h"
83 #include "wx/wfstream.h"
87 #include "wx/xpmdecod.h"
89 IMPLEMENT_DYNAMIC_CLASS(wxXPMHandler
,wxImageHandler
)
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
97 bool wxXPMHandler::LoadFile(wxImage
*image
,
98 wxInputStream
& stream
,
99 bool WXUNUSED(verbose
), int WXUNUSED(index
))
101 wxXPMDecoder decoder
;
103 wxImage img
= decoder
.ReadFile(stream
);
111 static char hexArray
[] = "0123456789ABCDEF";
113 static void DecToHex(int dec
, char *buf
)
115 int firstDigit
= (int)(dec
/16.0);
116 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
117 buf
[0] = hexArray
[firstDigit
];
118 buf
[1] = hexArray
[secondDigit
];
123 bool wxXPMHandler::SaveFile(wxImage
* image
,
124 wxOutputStream
& stream
, bool WXUNUSED(verbose
))
131 static const char Cixel
[MaxCixels
+1] =
132 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
133 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
138 cols
= image
->CountColours();
140 for ( k
= MaxCixels
; cols
> k
; k
*= MaxCixels
)
143 // 2. write the header:
145 if ( image
->HasOption(wxIMAGE_OPTION_FILENAME
) )
147 wxSplitPath(image
->GetOption(wxIMAGE_OPTION_FILENAME
),
149 sName
<< wxT("_xpm");
152 if ( !sName
.IsEmpty() )
153 sName
= wxString(wxT("/* XPM */\nstatic char *")) + sName
;
155 sName
= wxT("/* XPM */\nstatic char *xpm_data");
156 stream
.Write( (const char*) sName
.ToAscii(), sName
.Len() );
159 // VS: 200b is safe upper bound for anything produced by sprintf below
160 // (<101 bytes the string, neither %i can expand into more than 10 chars)
163 "/* columns rows colors chars-per-pixel */\n"
164 "\"%i %i %i %i\",\n",
165 image
->GetWidth(), image
->GetHeight(), cols
, chars_per_pixel
);
166 stream
.Write(tmpbuf
, strlen(tmpbuf
));
168 // 3. create color symbols table:
169 wxImageHistogram histogram
;
170 image
->ComputeHistogram(histogram
);
172 char *symbols_data
= new char[cols
* (chars_per_pixel
+1)];
173 char **symbols
= new char*[cols
];
175 // 2a. find mask colour:
176 unsigned long mask_key
= 0x1000000 /*invalid RGB value*/;
177 if (image
->HasMask())
178 mask_key
= (image
->GetMaskRed() << 16) |
179 (image
->GetMaskGreen() << 8) | image
->GetMaskBlue();
181 // 2b. generate colour table:
182 for (wxImageHistogram::iterator entry
= histogram
.begin();
183 entry
!= histogram
.end(); ++entry
)
185 unsigned long index
= entry
->second
.index
;
186 symbols
[index
] = symbols_data
+ index
* (chars_per_pixel
+1);
187 char *sym
= symbols
[index
];
189 k
= index
% MaxCixels
;
191 for (j
= 1; j
< chars_per_pixel
; j
++)
193 k
= ((index
- k
) / MaxCixels
) % MaxCixels
;
198 unsigned long key
= entry
->first
;
201 sprintf( tmpbuf
, "\"%s c Black\",\n", sym
);
202 else if (key
== mask_key
)
203 sprintf( tmpbuf
, "\"%s c None\",\n", sym
);
207 DecToHex( (unsigned char)(key
>> 16), rbuf
);
209 DecToHex( (unsigned char)(key
>> 8), gbuf
);
211 DecToHex( (unsigned char)(key
), bbuf
);
212 sprintf( tmpbuf
, "\"%s c #%s%s%s\",\n", sym
, rbuf
, gbuf
, bbuf
);
214 stream
.Write( tmpbuf
, strlen(tmpbuf
) );
217 tmp
= wxT("/* pixels */\n");
218 stream
.Write( (const char*) tmp
.ToAscii(), tmp
.Length() );
220 unsigned char *data
= image
->GetData();
221 for (j
= 0; j
< image
->GetHeight(); j
++)
223 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
224 for (i
= 0; i
< image
->GetWidth(); i
++, data
+= 3)
226 unsigned long key
= (data
[0] << 16) | (data
[1] << 8) | (data
[2]);
227 stream
.Write(symbols
[histogram
[key
].index
], chars_per_pixel
);
229 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
230 if ( j
+ 1 < image
->GetHeight() )
232 tmp_c
= ','; stream
.Write(&tmp_c
, 1);
234 tmp_c
= '\n'; stream
.Write(&tmp_c
, 1);
237 stream
.Write( (const char*) tmp
.ToAscii(), 3 );
241 delete[] symbols_data
;
246 bool wxXPMHandler::DoCanRead(wxInputStream
& stream
)
248 wxXPMDecoder decoder
;
249 return decoder
.CanRead(stream
);
252 #endif // wxUSE_STREAMS