1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagxpm.cpp
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"
80 #include "wx/imagxpm.h"
81 #include "wx/wfstream.h"
82 #include "wx/xpmdecod.h"
83 #include "wx/filename.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
);
109 // Make the given string a valid C identifier.
111 // All invalid characters are simply replaced by underscores and underscore is
112 // also prepended in the beginning if the initial character is not alphabetic.
114 MakeValidCIdent(wxString
* str
)
116 const wxChar chUnderscore
= wxT('_');
118 for ( wxString::iterator it
= str
->begin(); it
!= str
->end(); ++it
)
120 const wxChar ch
= *it
;
123 if ( it
== str
->begin() )
125 // Identifiers can't start with a digit.
126 str
->insert(0, chUnderscore
); // prepend underscore
127 it
= str
->begin(); // restart as string changed
131 else if ( !wxIsalpha(ch
) && ch
!= chUnderscore
)
133 // Not a valid character in C identifiers.
138 // Double underscores are not allowed in normal C identifiers and are
140 str
->Replace(wxT("__"), wxT("_"));
143 } // anonymous namespace
145 bool wxXPMHandler::SaveFile(wxImage
* image
,
146 wxOutputStream
& stream
, bool WXUNUSED(verbose
))
150 static const char Cixel
[MaxCixels
+1] =
151 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
152 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
155 wxImageHistogram histogram
;
156 int cols
= int(image
->ComputeHistogram(histogram
));
158 int chars_per_pixel
= 1;
159 for ( k
= MaxCixels
; cols
> k
; k
*= MaxCixels
)
162 // 2. write the header:
164 if ( image
->HasOption(wxIMAGE_OPTION_FILENAME
) )
166 sName
= wxFileName(image
->GetOption(wxIMAGE_OPTION_FILENAME
)).GetName();
167 MakeValidCIdent(&sName
);
168 sName
<< wxT("_xpm");
171 if ( !sName
.empty() )
172 sName
= wxString(wxT("/* XPM */\nstatic const char *")) + sName
;
174 sName
= wxT("/* XPM */\nstatic const char *xpm_data");
175 stream
.Write( (const char*) sName
.ToAscii(), sName
.Len() );
178 // VS: 200b is safe upper bound for anything produced by sprintf below
179 // (<101 bytes the string, neither %i can expand into more than 10 chars)
182 "/* columns rows colors chars-per-pixel */\n"
183 "\"%i %i %i %i\",\n",
184 image
->GetWidth(), image
->GetHeight(), cols
, chars_per_pixel
);
185 stream
.Write(tmpbuf
, strlen(tmpbuf
));
187 // 3. create color symbols table:
188 char *symbols_data
= new char[cols
* (chars_per_pixel
+1)];
189 char **symbols
= new char*[cols
];
191 // 2a. find mask colour:
192 unsigned long mask_key
= 0x1000000 /*invalid RGB value*/;
193 if (image
->HasMask())
194 mask_key
= (image
->GetMaskRed() << 16) |
195 (image
->GetMaskGreen() << 8) | image
->GetMaskBlue();
197 // 2b. generate colour table:
198 for (wxImageHistogram::iterator entry
= histogram
.begin();
199 entry
!= histogram
.end(); ++entry
)
201 unsigned long index
= entry
->second
.index
;
202 symbols
[index
] = symbols_data
+ index
* (chars_per_pixel
+1);
203 char *sym
= symbols
[index
];
205 for (j
= 0; j
< chars_per_pixel
; j
++)
207 sym
[j
] = Cixel
[index
% MaxCixels
];
212 unsigned long key
= entry
->first
;
215 sprintf( tmpbuf
, "\"%s c Black\",\n", sym
);
216 else if (key
== mask_key
)
217 sprintf( tmpbuf
, "\"%s c None\",\n", sym
);
220 wxByte r
= wxByte(key
>> 16);
221 wxByte g
= wxByte(key
>> 8);
222 wxByte b
= wxByte(key
);
223 sprintf(tmpbuf
, "\"%s c #%02X%02X%02X\",\n", sym
, r
, g
, b
);
225 stream
.Write( tmpbuf
, strlen(tmpbuf
) );
228 stream
.Write("/* pixels */\n", 13);
230 unsigned char *data
= image
->GetData();
231 for (j
= 0; j
< image
->GetHeight(); j
++)
234 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
235 for (i
= 0; i
< image
->GetWidth(); i
++, data
+= 3)
237 unsigned long key
= (data
[0] << 16) | (data
[1] << 8) | (data
[2]);
238 stream
.Write(symbols
[histogram
[key
].index
], chars_per_pixel
);
240 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
241 if ( j
+ 1 < image
->GetHeight() )
243 tmp_c
= ','; stream
.Write(&tmp_c
, 1);
245 tmp_c
= '\n'; stream
.Write(&tmp_c
, 1);
247 stream
.Write("};\n", 3 );
251 delete[] symbols_data
;
256 bool wxXPMHandler::DoCanRead(wxInputStream
& stream
)
258 wxXPMDecoder decoder
;
259 return decoder
.CanRead(stream
);
260 // it's ok to modify the stream position here
263 #endif // wxUSE_STREAMS