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 // For compilers that support precompilation, includes "wx.h".
66 #include "wx/wxprec.h"
78 #include "wx/imagxpm.h"
79 #include "wx/wfstream.h"
83 #include "wx/xpmdecod.h"
85 IMPLEMENT_DYNAMIC_CLASS(wxXPMHandler
,wxImageHandler
)
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
93 bool wxXPMHandler::LoadFile(wxImage
*image
,
94 wxInputStream
& stream
,
95 bool WXUNUSED(verbose
), int WXUNUSED(index
))
99 wxImage img
= decoder
.ReadFile(stream
);
107 static char hexArray
[] = "0123456789ABCDEF";
109 static void DecToHex(int dec
, char *buf
)
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
];
119 bool wxXPMHandler::SaveFile(wxImage
* image
,
120 wxOutputStream
& stream
, bool WXUNUSED(verbose
))
127 static const char Cixel
[MaxCixels
+1] =
128 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
129 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
134 cols
= image
->CountColours();
136 for ( k
= MaxCixels
; cols
> k
; k
*= MaxCixels
)
139 // 2. write the header:
141 if ( image
->HasOption(wxIMAGE_OPTION_FILENAME
) )
143 wxSplitPath(image
->GetOption(wxIMAGE_OPTION_FILENAME
),
145 sName
<< wxT("_xpm");
148 if ( !sName
.IsEmpty() )
149 sName
= wxString(wxT("/* XPM */\nstatic char *")) + sName
;
151 sName
= wxT("/* XPM */\nstatic char *xpm_data");
152 stream
.Write( (const char*) sName
.ToAscii(), sName
.Len() );
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)
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
));
164 // 3. create color symbols table:
165 wxImageHistogram histogram
;
166 image
->ComputeHistogram(histogram
);
168 char *symbols_data
= new char[cols
* (chars_per_pixel
+1)];
169 char **symbols
= new char*[cols
];
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();
177 // 2b. generate colour table:
178 for (wxImageHistogram::iterator entry
= histogram
.begin();
179 entry
!= histogram
.end(); ++entry
)
181 unsigned long index
= entry
->second
.index
;
182 symbols
[index
] = symbols_data
+ index
* (chars_per_pixel
+1);
183 char *sym
= symbols
[index
];
185 k
= index
% MaxCixels
;
187 for (j
= 1; j
< chars_per_pixel
; j
++)
189 k
= ((index
- k
) / MaxCixels
) % MaxCixels
;
194 unsigned long key
= entry
->first
;
197 sprintf( tmpbuf
, "\"%s c Black\",\n", sym
);
198 else if (key
== mask_key
)
199 sprintf( tmpbuf
, "\"%s c None\",\n", sym
);
203 DecToHex( (unsigned char)(key
>> 16), rbuf
);
205 DecToHex( (unsigned char)(key
>> 8), gbuf
);
207 DecToHex( (unsigned char)(key
), bbuf
);
208 sprintf( tmpbuf
, "\"%s c #%s%s%s\",\n", sym
, rbuf
, gbuf
, bbuf
);
210 stream
.Write( tmpbuf
, strlen(tmpbuf
) );
213 tmp
= wxT("/* pixels */\n");
214 stream
.Write( (const char*) tmp
.ToAscii(), tmp
.Length() );
216 unsigned char *data
= image
->GetData();
217 for (j
= 0; j
< image
->GetHeight(); j
++)
219 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
220 for (i
= 0; i
< image
->GetWidth(); i
++, data
+= 3)
222 unsigned long key
= (data
[0] << 16) | (data
[1] << 8) | (data
[2]);
223 stream
.Write(symbols
[histogram
[key
].index
], chars_per_pixel
);
225 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
226 if ( j
+ 1 < image
->GetHeight() )
228 tmp_c
= ','; stream
.Write(&tmp_c
, 1);
230 tmp_c
= '\n'; stream
.Write(&tmp_c
, 1);
233 stream
.Write( (const char*) tmp
.ToAscii(), 3 );
237 delete[] symbols_data
;
242 bool wxXPMHandler::DoCanRead(wxInputStream
& stream
)
244 wxXPMDecoder decoder
;
245 return decoder
.CanRead(stream
);
248 #endif // wxUSE_STREAMS