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"
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
);
106 static char hexArray
[] = "0123456789ABCDEF";
108 static void DecToHex(int dec
, char *buf
)
110 int firstDigit
= (int)(dec
/16.0);
111 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
112 buf
[0] = hexArray
[firstDigit
];
113 buf
[1] = hexArray
[secondDigit
];
118 bool wxXPMHandler::SaveFile(wxImage
* image
,
119 wxOutputStream
& stream
, bool WXUNUSED(verbose
))
126 static const char Cixel
[MaxCixels
+1] =
127 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
128 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
133 cols
= image
->CountColours();
135 for ( k
= MaxCixels
; cols
> k
; k
*= MaxCixels
)
138 // 2. write the header:
140 if ( image
->HasOption(wxIMAGE_OPTION_FILENAME
) )
142 wxSplitPath(image
->GetOption(wxIMAGE_OPTION_FILENAME
),
144 sName
<< wxT("_xpm");
147 if ( !sName
.empty() )
148 sName
= wxString(wxT("/* XPM */\nstatic char *")) + sName
;
150 sName
= wxT("/* XPM */\nstatic char *xpm_data");
151 stream
.Write( (const char*) sName
.ToAscii(), sName
.Len() );
154 // VS: 200b is safe upper bound for anything produced by sprintf below
155 // (<101 bytes the string, neither %i can expand into more than 10 chars)
158 "/* columns rows colors chars-per-pixel */\n"
159 "\"%i %i %i %i\",\n",
160 image
->GetWidth(), image
->GetHeight(), cols
, chars_per_pixel
);
161 stream
.Write(tmpbuf
, strlen(tmpbuf
));
163 // 3. create color symbols table:
164 wxImageHistogram histogram
;
165 image
->ComputeHistogram(histogram
);
167 char *symbols_data
= new char[cols
* (chars_per_pixel
+1)];
168 char **symbols
= new char*[cols
];
170 // 2a. find mask colour:
171 unsigned long mask_key
= 0x1000000 /*invalid RGB value*/;
172 if (image
->HasMask())
173 mask_key
= (image
->GetMaskRed() << 16) |
174 (image
->GetMaskGreen() << 8) | image
->GetMaskBlue();
176 // 2b. generate colour table:
177 for (wxImageHistogram::iterator entry
= histogram
.begin();
178 entry
!= histogram
.end(); ++entry
)
180 unsigned long index
= entry
->second
.index
;
181 symbols
[index
] = symbols_data
+ index
* (chars_per_pixel
+1);
182 char *sym
= symbols
[index
];
184 k
= index
% MaxCixels
;
186 for (j
= 1; j
< chars_per_pixel
; j
++)
188 k
= ((index
- k
) / MaxCixels
) % MaxCixels
;
193 unsigned long key
= entry
->first
;
196 sprintf( tmpbuf
, "\"%s c Black\",\n", sym
);
197 else if (key
== mask_key
)
198 sprintf( tmpbuf
, "\"%s c None\",\n", sym
);
202 DecToHex( (unsigned char)(key
>> 16), rbuf
);
204 DecToHex( (unsigned char)(key
>> 8), gbuf
);
206 DecToHex( (unsigned char)(key
), bbuf
);
207 sprintf( tmpbuf
, "\"%s c #%s%s%s\",\n", sym
, rbuf
, gbuf
, bbuf
);
209 stream
.Write( tmpbuf
, strlen(tmpbuf
) );
212 tmp
= wxT("/* pixels */\n");
213 stream
.Write( (const char*) tmp
.ToAscii(), tmp
.length() );
215 unsigned char *data
= image
->GetData();
216 for (j
= 0; j
< image
->GetHeight(); j
++)
218 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
219 for (i
= 0; i
< image
->GetWidth(); i
++, data
+= 3)
221 unsigned long key
= (data
[0] << 16) | (data
[1] << 8) | (data
[2]);
222 stream
.Write(symbols
[histogram
[key
].index
], chars_per_pixel
);
224 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
225 if ( j
+ 1 < image
->GetHeight() )
227 tmp_c
= ','; stream
.Write(&tmp_c
, 1);
229 tmp_c
= '\n'; stream
.Write(&tmp_c
, 1);
232 stream
.Write( (const char*) tmp
.ToAscii(), 3 );
236 delete[] symbols_data
;
241 bool wxXPMHandler::DoCanRead(wxInputStream
& stream
)
243 wxXPMDecoder decoder
;
244 return decoder
.CanRead(stream
);
247 #endif // wxUSE_STREAMS