]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagxpm.cpp
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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 #pragma implementation "imagxpm.h"
69 // For compilers that support precompilation, includes "wx.h".
70 #include "wx/wxprec.h"
82 #include "wx/imagxpm.h"
83 #include "wx/wfstream.h"
87 #include "wx/xpmdecod.h"
89 IMPLEMENT_DYNAMIC_CLASS(wxXPMHandler
,wxImageHandler
)
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
97 bool wxXPMHandler::LoadFile(wxImage
*image
,
98 wxInputStream
& stream
,
99 bool WXUNUSED(verbose
), int WXUNUSED(index
))
101 wxXPMDecoder decoder
;
103 wxImage img
= decoder
.ReadFile(stream
);
110 bool wxXPMHandler::SaveFile(wxImage
* image
,
111 wxOutputStream
& stream
, bool WXUNUSED(verbose
))
118 static const char Cixel
[MaxCixels
+1] =
119 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
120 "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
125 cols
= image
->CountColours();
127 for ( k
= MaxCixels
; cols
> k
; k
*= MaxCixels
)
130 // 2. write the header:
132 // VS: 200b is safe upper bound for anything produced by sprintf below
133 // (101 bytes the string, neither %i can expand into more than 10 chars)
136 "static char *xpm_data[] = {\n"
137 "/* columns rows colors chars-per-pixel */\n"
138 "\"%i %i %i %i\",\n",
139 image
->GetWidth(), image
->GetHeight(), cols
, chars_per_pixel
);
140 stream
.Write(tmpbuf
, strlen(tmpbuf
));
142 // 3. create color symbols table:
143 wxHashTable
table(wxKEY_INTEGER
);
144 table
.DeleteContents(TRUE
);
145 image
->ComputeHistogram(table
);
147 char *symbols_data
= new char[cols
* (chars_per_pixel
+1)];
148 char **symbols
= new char*[cols
];
150 // 2a. find mask colour:
152 if (image
->HasMask())
153 mask_key
= (image
->GetMaskRed() << 16) |
154 (image
->GetMaskGreen() << 8) | image
->GetMaskBlue();
156 // 2b. generate colour table:
159 while ((node
= table
.Next()) != NULL
)
161 wxHNode
*hnode
= (wxHNode
*) node
->GetData();
162 long index
= hnode
->index
;
163 symbols
[index
] = symbols_data
+ index
* (chars_per_pixel
+1);
164 char *sym
= symbols
[index
];
166 k
= index
% MaxCixels
;
168 for (j
= 1; j
< chars_per_pixel
; j
++)
170 k
= ((index
- k
) / MaxCixels
) % MaxCixels
;
175 long key
= node
->GetKeyInteger();
178 tmp
.Printf(wxT("\"%s c Black\",\n"), sym
);
179 else if (key
== mask_key
)
180 tmp
.Printf(wxT("\"%s c None\",\n"), sym
);
182 tmp
.Printf(wxT("\"%s c #%s%s%s\",\n"), sym
,
183 wxDecToHex((unsigned char)(key
>> 16)).c_str(),
184 wxDecToHex((unsigned char)(key
>> 8)).c_str(),
185 wxDecToHex((unsigned char)(key
)).c_str());
186 stream
.Write(tmp
.mb_str(), tmp
.Length());
189 tmp
= wxT("/* pixels */\n");
190 stream
.Write(tmp
.mb_str(), tmp
.Length());
192 unsigned char *data
= image
->GetData();
193 for (j
= 0; j
< image
->GetHeight(); j
++)
195 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
196 for (i
= 0; i
< image
->GetWidth(); i
++, data
+= 3)
198 unsigned long key
= (data
[0] << 16) | (data
[1] << 8) | (data
[2]);
199 wxHNode
*hnode
= (wxHNode
*) table
.Get(key
);
200 stream
.Write(symbols
[hnode
->index
], chars_per_pixel
);
202 tmp_c
= '\"'; stream
.Write(&tmp_c
, 1);
203 if ( j
+ 1 < image
->GetHeight() )
205 tmp_c
= ','; stream
.Write(&tmp_c
, 1);
207 tmp_c
= '\n'; stream
.Write(&tmp_c
, 1);
210 stream
.Write(tmp
.mb_str(), 3);
214 delete[] symbols_data
;
216 // FIXME: it will be better to use macros-based wxHashTable & DeleteContents(TRUE)
218 while ((node
= table
.Next()) != NULL
)
220 delete (wxHNode
*) node
->GetData();
226 bool wxXPMHandler::DoCanRead(wxInputStream
& stream
)
228 wxXPMDecoder decoder
;
229 return decoder
.CanRead(stream
);
232 #endif // wxUSE_STREAMS