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
);
105 bool wxXPMHandler::SaveFile(wxImage
* image
,
106 wxOutputStream
& stream
, bool WXUNUSED(verbose
))
110 static const char Cixel
[MaxCixels
+1] =
111 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
112 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
115 wxImageHistogram histogram
;
116 int cols
= int(image
->ComputeHistogram(histogram
));
118 int chars_per_pixel
= 1;
119 for ( k
= MaxCixels
; cols
> k
; k
*= MaxCixels
)
122 // 2. write the header:
124 if ( image
->HasOption(wxIMAGE_OPTION_FILENAME
) )
126 wxSplitPath(image
->GetOption(wxIMAGE_OPTION_FILENAME
),
128 sName
<< wxT("_xpm");
131 if ( !sName
.empty() )
132 sName
= wxString(wxT("/* XPM */\nstatic char *")) + sName
;
134 sName
= wxT("/* XPM */\nstatic char *xpm_data");
135 stream
.Write( (const char*) sName
.ToAscii(), sName
.Len() );
138 // VS: 200b is safe upper bound for anything produced by sprintf below
139 // (<101 bytes the string, neither %i can expand into more than 10 chars)
142 "/* columns rows colors chars-per-pixel */\n"
143 "\"%i %i %i %i\",\n",
144 image
->GetWidth(), image
->GetHeight(), cols
, chars_per_pixel
);
145 stream
.Write(tmpbuf
, strlen(tmpbuf
));
147 // 3. create color symbols table:
148 char *symbols_data
= new char[cols
* (chars_per_pixel
+1)];
149 char **symbols
= new char*[cols
];
151 // 2a. find mask colour:
152 unsigned long mask_key
= 0x1000000 /*invalid RGB value*/;
153 if (image
->HasMask())
154 mask_key
= (image
->GetMaskRed() << 16) |
155 (image
->GetMaskGreen() << 8) | image
->GetMaskBlue();
157 // 2b. generate colour table:
158 for (wxImageHistogram::iterator entry
= histogram
.begin();
159 entry
!= histogram
.end(); ++entry
)
161 unsigned long index
= entry
->second
.index
;
162 symbols
[index
] = symbols_data
+ index
* (chars_per_pixel
+1);
163 char *sym
= symbols
[index
];
165 for (j
= 0; j
< chars_per_pixel
; j
++)
167 sym
[j
] = Cixel
[index
% MaxCixels
];
172 unsigned long key
= entry
->first
;
175 sprintf( tmpbuf
, "\"%s c Black\",\n", sym
);
176 else if (key
== mask_key
)
177 sprintf( tmpbuf
, "\"%s c None\",\n", sym
);
180 wxByte r
= wxByte(key
>> 16);
181 wxByte g
= wxByte(key
>> 8);
182 wxByte b
= wxByte(key
);
183 sprintf(tmpbuf
, "\"%s c #%02X%02X%02X\",\n", sym
, r
, g
, b
);
185 stream
.Write( tmpbuf
, strlen(tmpbuf
) );
188 stream
.Write("/* pixels */\n", 13);
190 unsigned char *data
= image
->GetData();
191 for (j
= 0; j
< image
->GetHeight(); j
++)
194 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
195 for (i
= 0; i
< image
->GetWidth(); i
++, data
+= 3)
197 unsigned long key
= (data
[0] << 16) | (data
[1] << 8) | (data
[2]);
198 stream
.Write(symbols
[histogram
[key
].index
], chars_per_pixel
);
200 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
201 if ( j
+ 1 < image
->GetHeight() )
203 tmp_c
= ','; stream
.Write(&tmp_c
, 1);
205 tmp_c
= '\n'; stream
.Write(&tmp_c
, 1);
207 stream
.Write("};\n", 3 );
211 delete[] symbols_data
;
216 bool wxXPMHandler::DoCanRead(wxInputStream
& stream
)
218 wxXPMDecoder decoder
;
219 return decoder
.CanRead(stream
);
222 #endif // wxUSE_STREAMS