1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagxpm.cpp
3 // Purpose: wxXPMHandler
4 // Author: Vaclav Slavik, Robert Roebling
5 // Copyright: (c) 2001 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
11 This file is partially based on source code of ImageMagick by John Cristy. Its
12 license is as follows:
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25 % Read/Write ImageMagick Image Format. %
33 % Copyright (C) 2001 ImageMagick Studio, a non-profit organization dedicated %
34 % to making software imaging solutions freely available. %
36 % Permission is hereby granted, free of charge, to any person obtaining a %
37 % copy of this software and associated documentation files ("ImageMagick"), %
38 % to deal in ImageMagick without restriction, including without limitation %
39 % the rights to use, copy, modify, merge, publish, distribute, sublicense, %
40 % and/or sell copies of ImageMagick, and to permit persons to whom the %
41 % ImageMagick is furnished to do so, subject to the following conditions: %
43 % The above copyright notice and this permission notice shall be included in %
44 % all copies or substantial portions of ImageMagick. %
46 % The software is provided "as is", without warranty of any kind, express or %
47 % implied, including but not limited to the warranties of merchantability, %
48 % fitness for a particular purpose and noninfringement. In no event shall %
49 % ImageMagick Studio be liable for any claim, damages or other liability, %
50 % whether in an action of contract, tort or otherwise, arising from, out of %
51 % or in connection with ImageMagick or the use or other dealings in %
54 % Except as contained in this notice, the name of the ImageMagick Studio %
55 % shall not be used in advertising or otherwise to promote the sale, use or %
56 % other dealings in ImageMagick without prior written authorization from the %
57 % ImageMagick Studio. %
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 // For compilers that support precompilation, includes "wx.h".
65 #include "wx/wxprec.h"
79 #include "wx/imagxpm.h"
80 #include "wx/wfstream.h"
81 #include "wx/xpmdecod.h"
82 #include "wx/filename.h"
84 IMPLEMENT_DYNAMIC_CLASS(wxXPMHandler
,wxImageHandler
)
86 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
92 bool wxXPMHandler::LoadFile(wxImage
*image
,
93 wxInputStream
& stream
,
94 bool WXUNUSED(verbose
), int WXUNUSED(index
))
98 wxImage img
= decoder
.ReadFile(stream
);
108 // Make the given string a valid C identifier.
110 // All invalid characters are simply replaced by underscores and underscore is
111 // also prepended in the beginning if the initial character is not alphabetic.
113 MakeValidCIdent(wxString
* str
)
115 const wxChar chUnderscore
= wxT('_');
117 for ( wxString::iterator it
= str
->begin(); it
!= str
->end(); ++it
)
119 const wxChar ch
= *it
;
122 if ( it
== str
->begin() )
124 // Identifiers can't start with a digit.
125 str
->insert(0, chUnderscore
); // prepend underscore
126 it
= str
->begin(); // restart as string changed
130 else if ( !wxIsalpha(ch
) && ch
!= chUnderscore
)
132 // Not a valid character in C identifiers.
137 // Double underscores are not allowed in normal C identifiers and are
139 str
->Replace(wxT("__"), wxT("_"));
142 } // anonymous namespace
144 bool wxXPMHandler::SaveFile(wxImage
* image
,
145 wxOutputStream
& stream
, bool WXUNUSED(verbose
))
149 static const char Cixel
[MaxCixels
+1] =
150 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
151 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
154 wxImageHistogram histogram
;
155 int cols
= int(image
->ComputeHistogram(histogram
));
157 int chars_per_pixel
= 1;
158 for ( k
= MaxCixels
; cols
> k
; k
*= MaxCixels
)
161 // 2. write the header:
163 if ( image
->HasOption(wxIMAGE_OPTION_FILENAME
) )
165 sName
= wxFileName(image
->GetOption(wxIMAGE_OPTION_FILENAME
)).GetName();
166 MakeValidCIdent(&sName
);
167 sName
<< wxT("_xpm");
170 if ( !sName
.empty() )
171 sName
= wxString(wxT("/* XPM */\nstatic const char *")) + sName
;
173 sName
= wxT("/* XPM */\nstatic const char *xpm_data");
174 stream
.Write( (const char*) sName
.ToAscii(), sName
.Len() );
177 // VS: 200b is safe upper bound for anything produced by sprintf below
178 // (<101 bytes the string, neither %i can expand into more than 10 chars)
181 "/* columns rows colors chars-per-pixel */\n"
182 "\"%i %i %i %i\",\n",
183 image
->GetWidth(), image
->GetHeight(), cols
, chars_per_pixel
);
184 stream
.Write(tmpbuf
, strlen(tmpbuf
));
186 // 3. create color symbols table:
187 char *symbols_data
= new char[cols
* (chars_per_pixel
+1)];
188 char **symbols
= new char*[cols
];
190 // 2a. find mask colour:
191 unsigned long mask_key
= 0x1000000 /*invalid RGB value*/;
192 if (image
->HasMask())
193 mask_key
= (image
->GetMaskRed() << 16) |
194 (image
->GetMaskGreen() << 8) | image
->GetMaskBlue();
196 // 2b. generate colour table:
197 for (wxImageHistogram::iterator entry
= histogram
.begin();
198 entry
!= histogram
.end(); ++entry
)
200 unsigned long index
= entry
->second
.index
;
201 symbols
[index
] = symbols_data
+ index
* (chars_per_pixel
+1);
202 char *sym
= symbols
[index
];
204 for (j
= 0; j
< chars_per_pixel
; j
++)
206 sym
[j
] = Cixel
[index
% MaxCixels
];
211 unsigned long key
= entry
->first
;
214 sprintf( tmpbuf
, "\"%s c Black\",\n", sym
);
215 else if (key
== mask_key
)
216 sprintf( tmpbuf
, "\"%s c None\",\n", sym
);
219 wxByte r
= wxByte(key
>> 16);
220 wxByte g
= wxByte(key
>> 8);
221 wxByte b
= wxByte(key
);
222 sprintf(tmpbuf
, "\"%s c #%02X%02X%02X\",\n", sym
, r
, g
, b
);
224 stream
.Write( tmpbuf
, strlen(tmpbuf
) );
227 stream
.Write("/* pixels */\n", 13);
229 unsigned char *data
= image
->GetData();
230 for (j
= 0; j
< image
->GetHeight(); j
++)
233 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
234 for (i
= 0; i
< image
->GetWidth(); i
++, data
+= 3)
236 unsigned long key
= (data
[0] << 16) | (data
[1] << 8) | (data
[2]);
237 stream
.Write(symbols
[histogram
[key
].index
], chars_per_pixel
);
239 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
240 if ( j
+ 1 < image
->GetHeight() )
242 tmp_c
= ','; stream
.Write(&tmp_c
, 1);
244 tmp_c
= '\n'; stream
.Write(&tmp_c
, 1);
246 stream
.Write("};\n", 3 );
250 delete[] symbols_data
;
255 bool wxXPMHandler::DoCanRead(wxInputStream
& stream
)
257 wxXPMDecoder decoder
;
258 return decoder
.CanRead(stream
);
259 // it's ok to modify the stream position here
262 #endif // wxUSE_STREAMS