]>
Commit | Line | Data |
---|---|---|
01111366 RR |
1 | /* |
2 | * Program: image | |
1d5b7a0b | 3 | * |
01111366 RR |
4 | * Author: Robert Roebling |
5 | * | |
6 | * Copyright: (C) 1998, Robert Roebling | |
7 | * | |
8 | */ | |
9 | ||
3d05544e JS |
10 | // For compilers that support precompilation, includes "wx/wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
17 | #ifndef WX_PRECOMP | |
01111366 | 18 | #include "wx/wx.h" |
3d05544e JS |
19 | #endif |
20 | ||
01111366 | 21 | #include "wx/image.h" |
a23fd0e1 | 22 | #include "wx/file.h" |
a8d4e3ac VS |
23 | #include "wx/mstream.h" |
24 | #include "wx/wfstream.h" | |
1971d23c | 25 | #include "wx/quantize.h" |
a23fd0e1 | 26 | |
dc1dbfc6 | 27 | #include "smile.xbm" |
d2aa73a8 VZ |
28 | |
29 | #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW | |
30 | #include "smile.xpm" | |
31 | #endif | |
dc1dbfc6 | 32 | |
a8d4e3ac | 33 | |
01111366 RR |
34 | // derived classes |
35 | ||
36 | class MyFrame; | |
37 | class MyApp; | |
38 | ||
39 | // MyCanvas | |
40 | ||
41 | class MyCanvas: public wxScrolledWindow | |
42 | { | |
1d5b7a0b | 43 | public: |
51b07644 | 44 | MyCanvas() {} |
01111366 | 45 | MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size ); |
1d5b7a0b | 46 | ~MyCanvas(); |
01111366 | 47 | void OnPaint( wxPaintEvent &event ); |
9390a202 | 48 | void CreateAntiAliasedBitmap(); |
1d5b7a0b | 49 | |
e1929140 RR |
50 | wxBitmap *my_horse_png; |
51 | wxBitmap *my_horse_jpeg; | |
52 | wxBitmap *my_horse_gif; | |
ca26177c | 53 | wxBitmap *my_horse_bmp; |
51b07644 | 54 | wxBitmap *my_horse_bmp2; |
cbd4be25 | 55 | wxBitmap *my_horse_pcx; |
6319afe3 | 56 | wxBitmap *my_horse_pnm; |
60a41aee | 57 | wxBitmap *my_horse_tiff; |
9d8bdf2d | 58 | wxBitmap *my_horse_xpm; |
bf504f98 VS |
59 | wxBitmap *my_horse_ico32; |
60 | wxBitmap *my_horse_ico16; | |
61 | wxBitmap *my_horse_ico; | |
62 | wxBitmap *my_horse_cur; | |
2b5f62a0 | 63 | wxBitmap *my_horse_ani; |
51b07644 | 64 | |
dc1dbfc6 | 65 | wxBitmap *my_smile_xbm; |
e8fdc264 | 66 | wxBitmap *my_square; |
9390a202 | 67 | wxBitmap *my_anti; |
1d5b7a0b | 68 | |
bf504f98 | 69 | int xH, yH ; |
2b5f62a0 | 70 | int m_ani_images ; |
bf504f98 | 71 | |
bea56879 VZ |
72 | protected: |
73 | wxBitmap m_bmpSmileXpm; | |
74 | wxIcon m_iconSmileXpm; | |
75 | ||
60a41aee | 76 | private: |
1d5b7a0b VZ |
77 | DECLARE_DYNAMIC_CLASS(MyCanvas) |
78 | DECLARE_EVENT_TABLE() | |
01111366 RR |
79 | }; |
80 | ||
18134a1c VZ |
81 | |
82 | const int nChoices = 8 ; | |
83 | static const wxString bppchoices[nChoices] = | |
84 | { | |
51b07644 VZ |
85 | _T("1 bpp color"), |
86 | _T("1 bpp B&W"), | |
87 | _T("4 bpp color"), | |
88 | _T("8 bpp color"), | |
89 | _T("8 bpp greyscale"), | |
90 | _T("8 bpp red"), | |
91 | _T("8 bpp own palette"), | |
92 | _T("24 bpp") | |
18134a1c VZ |
93 | }; |
94 | ||
95 | static const int bppvalues[nChoices] = | |
96 | { | |
97 | wxBMP_1BPP, | |
98 | wxBMP_1BPP_BW, | |
99 | wxBMP_4BPP, | |
100 | wxBMP_8BPP, | |
101 | wxBMP_8BPP_GREY, | |
102 | wxBMP_8BPP_RED, | |
103 | wxBMP_8BPP_PALETTE, | |
104 | wxBMP_24BPP | |
105 | }; | |
106 | ||
01111366 RR |
107 | // MyFrame |
108 | ||
18134a1c | 109 | |
01111366 RR |
110 | class MyFrame: public wxFrame |
111 | { | |
1d5b7a0b VZ |
112 | public: |
113 | MyFrame(); | |
01111366 | 114 | |
01111366 | 115 | void OnAbout( wxCommandEvent &event ); |
ab0f0386 | 116 | void OnNewFrame( wxCommandEvent &event ); |
01111366 | 117 | void OnQuit( wxCommandEvent &event ); |
1d5b7a0b | 118 | |
01111366 | 119 | MyCanvas *m_canvas; |
1d5b7a0b | 120 | |
60a41aee | 121 | private: |
1d5b7a0b VZ |
122 | DECLARE_DYNAMIC_CLASS(MyFrame) |
123 | DECLARE_EVENT_TABLE() | |
01111366 RR |
124 | }; |
125 | ||
ab0f0386 VZ |
126 | class MyImageFrame : public wxFrame |
127 | { | |
128 | public: | |
129 | MyImageFrame(wxFrame *parent, const wxBitmap& bitmap) | |
1971d23c | 130 | : wxFrame(parent, -1, _T("Double click to save"), |
ab0f0386 | 131 | wxDefaultPosition, wxDefaultSize, |
f6bcfd97 | 132 | wxCAPTION | wxSYSTEM_MENU), |
1971d23c | 133 | m_bitmap(bitmap) |
ab0f0386 VZ |
134 | { |
135 | SetClientSize(bitmap.GetWidth(), bitmap.GetHeight()); | |
136 | } | |
137 | ||
0c543b7a VZ |
138 | void OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
139 | { | |
140 | // do nothing here to be able to see how transparent images are shown | |
141 | } | |
142 | ||
ab0f0386 VZ |
143 | void OnPaint(wxPaintEvent& WXUNUSED(event)) |
144 | { | |
145 | wxPaintDC dc( this ); | |
0c543b7a | 146 | dc.DrawBitmap( m_bitmap, 0, 0, TRUE /* use mask */ ); |
ab0f0386 VZ |
147 | } |
148 | ||
c5c843e0 | 149 | void OnSave(wxMouseEvent& WXUNUSED(event)) |
1971d23c | 150 | { |
368d59f0 | 151 | wxImage image = m_bitmap.ConvertToImage(); |
1971d23c | 152 | |
51b07644 VZ |
153 | int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"), |
154 | _T("Set BMP BPP"), | |
18134a1c | 155 | nChoices, |
1971d23c VZ |
156 | bppchoices, |
157 | this); | |
158 | if ( bppselection == -1 ) | |
159 | { | |
160 | // cancelled | |
161 | return; | |
162 | } | |
163 | ||
fd94e8aa | 164 | image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, bppvalues[bppselection]); |
1971d23c VZ |
165 | |
166 | wxString deffilename = bppchoices[bppselection]; | |
4693b20c MB |
167 | deffilename.Replace(wxT(" "), wxT("_")); |
168 | deffilename += wxT(".bmp"); | |
169 | wxString savefilename = wxFileSelector( wxT("Save Image"), | |
170 | wxT(""), | |
1971d23c | 171 | deffilename, |
4693b20c MB |
172 | (const wxChar *)NULL, |
173 | wxT("BMP files (*.bmp)|*.bmp|") | |
174 | wxT("PNG files (*.png)|*.png|") | |
175 | wxT("JPEG files (*.jpg)|*.jpg|") | |
176 | wxT("GIF files (*.gif)|*.gif|") | |
177 | wxT("TIFF files (*.tif)|*.tif|") | |
bf504f98 VS |
178 | wxT("PCX files (*.pcx)|*.pcx|") |
179 | wxT("ICO files (*.ico)|*.ico|") | |
180 | wxT("CUR files (*.cur)|*.cur"), | |
1971d23c VZ |
181 | wxSAVE); |
182 | ||
183 | if ( savefilename.empty() ) | |
184 | return; | |
185 | ||
fd94e8aa | 186 | if ( image.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT) == wxBMP_8BPP_PALETTE ) |
1971d23c VZ |
187 | { |
188 | unsigned char *cmap = new unsigned char [256]; | |
189 | for ( int i = 0; i < 256; i++ ) | |
190 | cmap[i] = i; | |
191 | image.SetPalette(wxPalette(256, cmap, cmap, cmap)); | |
192 | ||
193 | delete cmap; | |
194 | } | |
195 | ||
45647dcf | 196 | bool loaded; |
1971d23c VZ |
197 | wxString extension = savefilename.AfterLast('.').Lower(); |
198 | ||
51b07644 | 199 | if (extension == _T("cur")) |
45647dcf | 200 | { |
51b07644 VZ |
201 | image.Rescale(32,32); |
202 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0); | |
45647dcf VS |
203 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0); |
204 | // This shows how you can save an image with explicitly | |
205 | // specified image format: | |
206 | loaded = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR); | |
207 | } | |
51b07644 | 208 | else |
45647dcf VS |
209 | { |
210 | // This one guesses image format from filename extension | |
211 | // (it may fail if the extension is not recognized): | |
212 | loaded = image.SaveFile(savefilename); | |
213 | } | |
51b07644 | 214 | |
45647dcf | 215 | if ( !loaded ) |
51b07644 VZ |
216 | wxMessageBox(_T("No handler for this file type."), |
217 | _T("File was not saved"), | |
1971d23c VZ |
218 | wxOK|wxCENTRE, this); |
219 | } | |
220 | ||
ab0f0386 VZ |
221 | private: |
222 | wxBitmap m_bitmap; | |
223 | ||
224 | DECLARE_EVENT_TABLE() | |
225 | }; | |
226 | ||
01111366 RR |
227 | // MyApp |
228 | ||
229 | class MyApp: public wxApp | |
230 | { | |
1d5b7a0b VZ |
231 | public: |
232 | virtual bool OnInit(); | |
01111366 RR |
233 | }; |
234 | ||
235 | // main program | |
236 | ||
237 | IMPLEMENT_APP(MyApp) | |
238 | ||
239 | // MyCanvas | |
240 | ||
241 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) | |
242 | ||
ab0f0386 | 243 | BEGIN_EVENT_TABLE(MyImageFrame, wxFrame) |
0c543b7a VZ |
244 | EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground) |
245 | EVT_PAINT(MyImageFrame::OnPaint) | |
246 | EVT_LEFT_DCLICK(MyImageFrame::OnSave) | |
ab0f0386 VZ |
247 | END_EVENT_TABLE() |
248 | ||
1d5b7a0b VZ |
249 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) |
250 | EVT_PAINT(MyCanvas::OnPaint) | |
01111366 RR |
251 | END_EVENT_TABLE() |
252 | ||
acbd13a3 | 253 | MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, |
1d5b7a0b | 254 | const wxPoint &pos, const wxSize &size ) |
81278df2 VZ |
255 | : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER ) |
256 | #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW | |
257 | , m_bmpSmileXpm((const char **) smile_xpm) | |
258 | , m_iconSmileXpm((const char **) smile_xpm) | |
259 | #endif | |
01111366 | 260 | { |
60a41aee RR |
261 | my_horse_png = (wxBitmap*) NULL; |
262 | my_horse_jpeg = (wxBitmap*) NULL; | |
263 | my_horse_gif = (wxBitmap*) NULL; | |
264 | my_horse_bmp = (wxBitmap*) NULL; | |
51b07644 | 265 | my_horse_bmp2 = (wxBitmap*) NULL; |
60a41aee RR |
266 | my_horse_pcx = (wxBitmap*) NULL; |
267 | my_horse_pnm = (wxBitmap*) NULL; | |
268 | my_horse_tiff = (wxBitmap*) NULL; | |
9d8bdf2d | 269 | my_horse_xpm = (wxBitmap*) NULL; |
bf504f98 VS |
270 | my_horse_ico32 = (wxBitmap*) NULL; |
271 | my_horse_ico16 = (wxBitmap*) NULL; | |
272 | my_horse_ico = (wxBitmap*) NULL; | |
273 | my_horse_cur = (wxBitmap*) NULL; | |
2b5f62a0 | 274 | my_horse_ani = (wxBitmap*) NULL; |
bf504f98 | 275 | |
dc1dbfc6 | 276 | my_smile_xbm = (wxBitmap*) NULL; |
60a41aee RR |
277 | my_square = (wxBitmap*) NULL; |
278 | my_anti = (wxBitmap*) NULL; | |
279 | ||
2b5f62a0 VZ |
280 | m_ani_images = 0 ; |
281 | ||
60a41aee RR |
282 | SetBackgroundColour(* wxWHITE); |
283 | ||
284 | wxBitmap bitmap( 100, 100 ); | |
285 | ||
286 | wxMemoryDC dc; | |
287 | dc.SelectObject( bitmap ); | |
a60b1f5d | 288 | dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) ); |
cf3da716 | 289 | dc.SetPen( *wxBLACK_PEN ); |
60a41aee | 290 | dc.DrawRectangle( 0, 0, 100, 100 ); |
cf3da716 RR |
291 | dc.SetBrush( *wxWHITE_BRUSH ); |
292 | dc.DrawRectangle( 20, 20, 60, 60 ); | |
60a41aee RR |
293 | dc.SelectObject( wxNullBitmap ); |
294 | ||
295 | // try to find the directory with our images | |
296 | wxString dir; | |
4693b20c | 297 | if ( wxFile::Exists(wxT("./horse.png")) ) |
2b5f62a0 | 298 | dir = wxT("./"); |
4693b20c | 299 | else if ( wxFile::Exists(wxT("../horse.png")) ) |
2b5f62a0 | 300 | dir = wxT("../"); |
60a41aee | 301 | else |
4693b20c | 302 | wxLogWarning(wxT("Can't find image files in either '.' or '..'!")); |
60a41aee | 303 | |
a8d4e3ac | 304 | wxImage image = bitmap.ConvertToImage(); |
60a41aee | 305 | |
bea56879 | 306 | #if wxUSE_LIBPNG |
51b07644 | 307 | if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG )) |
4693b20c | 308 | wxLogError(wxT("Can't save file")); |
f6bcfd97 | 309 | |
176fe5af | 310 | image.Destroy(); |
60a41aee | 311 | |
51b07644 | 312 | image.LoadFile( dir + _T("test.png") ); |
a8d4e3ac | 313 | my_square = new wxBitmap( image ); |
f6bcfd97 | 314 | |
176fe5af | 315 | image.Destroy(); |
cc977e5f | 316 | |
51b07644 | 317 | if ( !image.LoadFile( dir + _T("horse.png")) ) |
4693b20c | 318 | wxLogError(wxT("Can't load PNG image")); |
60a41aee | 319 | else |
a8d4e3ac | 320 | my_horse_png = new wxBitmap( image ); |
bea56879 | 321 | #endif // wxUSE_LIBPNG |
60a41aee | 322 | |
bea56879 | 323 | #if wxUSE_LIBJPEG |
176fe5af | 324 | image.Destroy(); |
279ababf | 325 | |
51b07644 | 326 | if ( !image.LoadFile( dir + _T("horse.jpg")) ) |
4693b20c | 327 | wxLogError(wxT("Can't load JPG image")); |
60a41aee | 328 | else |
9d8bdf2d | 329 | my_horse_jpeg = new wxBitmap( image ); |
bea56879 | 330 | #endif // wxUSE_LIBJPEG |
6e47faf1 JS |
331 | |
332 | #if wxUSE_GIF | |
176fe5af | 333 | image.Destroy(); |
279ababf | 334 | |
2b5f62a0 | 335 | if ( !image.LoadFile( dir + _T("horse.gif" )) ) |
4693b20c | 336 | wxLogError(wxT("Can't load GIF image")); |
60a41aee | 337 | else |
9d8bdf2d | 338 | my_horse_gif = new wxBitmap( image ); |
6e47faf1 | 339 | #endif |
cbd4be25 | 340 | |
6e47faf1 | 341 | #if wxUSE_PCX |
176fe5af | 342 | image.Destroy(); |
279ababf | 343 | |
51b07644 | 344 | if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) ) |
4693b20c | 345 | wxLogError(wxT("Can't load PCX image")); |
60a41aee | 346 | else |
9d8bdf2d | 347 | my_horse_pcx = new wxBitmap( image ); |
6e47faf1 | 348 | #endif |
cbd4be25 | 349 | |
176fe5af | 350 | image.Destroy(); |
279ababf | 351 | |
51b07644 | 352 | if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) ) |
4693b20c | 353 | wxLogError(wxT("Can't load BMP image")); |
60a41aee | 354 | else |
9d8bdf2d | 355 | my_horse_bmp = new wxBitmap( image ); |
6319afe3 | 356 | |
a8d4e3ac | 357 | #if wxUSE_XPM |
9d8bdf2d VS |
358 | image.Destroy(); |
359 | ||
51b07644 | 360 | if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) ) |
4693b20c | 361 | wxLogError(wxT("Can't load XPM image")); |
9d8bdf2d VS |
362 | else |
363 | my_horse_xpm = new wxBitmap( image ); | |
364 | ||
51b07644 | 365 | if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM )) |
4693b20c | 366 | wxLogError(wxT("Can't save file")); |
a8d4e3ac VS |
367 | #endif |
368 | ||
6e47faf1 | 369 | #if wxUSE_PNM |
176fe5af | 370 | image.Destroy(); |
279ababf | 371 | |
51b07644 | 372 | if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) ) |
4693b20c | 373 | wxLogError(wxT("Can't load PNM image")); |
60a41aee | 374 | else |
9d8bdf2d | 375 | my_horse_pnm = new wxBitmap( image ); |
6e47faf1 | 376 | #endif |
f048e32f | 377 | |
60a41aee | 378 | #if wxUSE_LIBTIFF |
176fe5af | 379 | image.Destroy(); |
279ababf | 380 | |
51b07644 | 381 | if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) ) |
4693b20c | 382 | wxLogError(wxT("Can't load TIFF image")); |
60a41aee | 383 | else |
9d8bdf2d | 384 | my_horse_tiff = new wxBitmap( image ); |
60a41aee RR |
385 | #endif |
386 | ||
60a41aee | 387 | CreateAntiAliasedBitmap(); |
dc1dbfc6 RL |
388 | |
389 | my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width, | |
390 | smile_height, 1 ); | |
bfef20bf | 391 | |
81278df2 | 392 | #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW |
bfef20bf GRG |
393 | // demonstrates XPM automatically using the mask when saving |
394 | if ( m_bmpSmileXpm.Ok() ) | |
51b07644 | 395 | m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM); |
81278df2 | 396 | #endif |
bf504f98 VS |
397 | |
398 | #if wxUSE_ICO_CUR | |
399 | image.Destroy(); | |
400 | ||
51b07644 | 401 | if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) ) |
bf504f98 VS |
402 | wxLogError(wxT("Can't load first ICO image")); |
403 | else | |
404 | my_horse_ico32 = new wxBitmap( image ); | |
51b07644 | 405 | |
bf504f98 VS |
406 | image.Destroy(); |
407 | ||
51b07644 | 408 | if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) ) |
bf504f98 VS |
409 | wxLogError(wxT("Can't load second ICO image")); |
410 | else | |
411 | my_horse_ico16 = new wxBitmap( image ); | |
412 | ||
413 | image.Destroy(); | |
414 | ||
51b07644 | 415 | if ( !image.LoadFile( dir + _T("horse.ico") ) ) |
bf504f98 VS |
416 | wxLogError(wxT("Can't load best ICO image")); |
417 | else | |
418 | my_horse_ico = new wxBitmap( image ); | |
419 | ||
420 | image.Destroy(); | |
421 | ||
51b07644 | 422 | if ( !image.LoadFile( dir + _T("horse.cur"), wxBITMAP_TYPE_CUR ) ) |
bf504f98 VS |
423 | wxLogError(wxT("Can't load best ICO image")); |
424 | else | |
51b07644 | 425 | { |
bf504f98 | 426 | my_horse_cur = new wxBitmap( image ); |
fd94e8aa VS |
427 | xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ; |
428 | yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ; | |
51b07644 | 429 | } |
2b5f62a0 | 430 | |
08811762 | 431 | m_ani_images = wxImage::GetImageCount ( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI ); |
2b5f62a0 VZ |
432 | if (m_ani_images==0) |
433 | wxLogError(wxT("No ANI-format images found")); | |
434 | else | |
435 | my_horse_ani = new wxBitmap [m_ani_images]; | |
436 | int i ; | |
437 | for (i=0; i < m_ani_images; i++) | |
438 | { | |
439 | image.Destroy(); | |
08811762 | 440 | if (!image.LoadFile( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI, i )) |
2b5f62a0 VZ |
441 | { |
442 | wxString tmp = wxT("Can't load image number "); | |
443 | tmp << i ; | |
444 | wxLogError(tmp); | |
445 | } | |
446 | else | |
447 | my_horse_ani [i] = wxBitmap( image ); | |
448 | } | |
449 | ||
450 | ||
bf504f98 VS |
451 | #endif |
452 | ||
51b07644 VZ |
453 | image.Destroy(); |
454 | ||
455 | // test image loading from stream | |
456 | wxFile file(dir + _T("horse.bmp")); | |
457 | off_t len = file.Length(); | |
458 | void *data = malloc(len); | |
459 | if ( file.Read(data, len) != len ) | |
460 | wxLogError(_T("Reading bitmap file failed")); | |
461 | else | |
462 | { | |
463 | wxMemoryInputStream mis(data, len); | |
464 | if ( !image.LoadFile(mis) ) | |
465 | wxLogError(wxT("Can't load BMP image from stream")); | |
466 | else | |
467 | my_horse_bmp2 = new wxBitmap( image ); | |
468 | } | |
469 | ||
470 | free(data); | |
01111366 RR |
471 | } |
472 | ||
1d5b7a0b | 473 | MyCanvas::~MyCanvas() |
01111366 | 474 | { |
60a41aee RR |
475 | delete my_horse_pnm; |
476 | delete my_horse_png; | |
477 | delete my_horse_jpeg; | |
478 | delete my_horse_gif; | |
479 | delete my_horse_bmp; | |
51b07644 | 480 | delete my_horse_bmp2; |
60a41aee RR |
481 | delete my_horse_pcx; |
482 | delete my_horse_tiff; | |
9d8bdf2d | 483 | delete my_horse_xpm; |
bf504f98 VS |
484 | delete my_horse_ico32; |
485 | delete my_horse_ico16; | |
486 | delete my_horse_ico; | |
2f6c54eb | 487 | delete my_horse_cur; |
2b5f62a0 | 488 | delete [] my_horse_ani; |
dc1dbfc6 | 489 | delete my_smile_xbm; |
60a41aee RR |
490 | delete my_square; |
491 | delete my_anti; | |
01111366 RR |
492 | } |
493 | ||
494 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
495 | { | |
be25e480 RR |
496 | wxPaintDC dc( this ); |
497 | PrepareDC( dc ); | |
a91b47e8 | 498 | |
51b07644 | 499 | dc.DrawText( _T("Loaded image"), 30, 10 ); |
be25e480 | 500 | if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 ); |
f048e32f | 501 | |
51b07644 | 502 | dc.DrawText( _T("Drawn directly"), 150, 10 ); |
a60b1f5d | 503 | dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) ); |
cf3da716 | 504 | dc.SetPen( *wxBLACK_PEN ); |
be25e480 | 505 | dc.DrawRectangle( 150, 30, 100, 100 ); |
cf3da716 RR |
506 | dc.SetBrush( *wxWHITE_BRUSH ); |
507 | dc.DrawRectangle( 170, 50, 60, 60 ); | |
be25e480 | 508 | |
408b4168 | 509 | if (my_anti && my_anti->Ok()) |
be25e480 RR |
510 | dc.DrawBitmap( *my_anti, 280, 30 ); |
511 | ||
51b07644 | 512 | dc.DrawText( _T("PNG handler"), 30, 135 ); |
be25e480 | 513 | if (my_horse_png && my_horse_png->Ok()) |
408b4168 | 514 | { |
be25e480 RR |
515 | dc.DrawBitmap( *my_horse_png, 30, 150 ); |
516 | wxRect rect(0,0,100,100); | |
517 | wxBitmap sub( my_horse_png->GetSubBitmap(rect) ); | |
51b07644 | 518 | dc.DrawText( _T("GetSubBitmap()"), 280, 190 ); |
be25e480 RR |
519 | dc.DrawBitmap( sub, 280, 210 ); |
520 | } | |
521 | ||
51b07644 | 522 | dc.DrawText( _T("JPEG handler"), 30, 365 ); |
408b4168 | 523 | if (my_horse_jpeg && my_horse_jpeg->Ok()) |
be25e480 RR |
524 | dc.DrawBitmap( *my_horse_jpeg, 30, 380 ); |
525 | ||
51b07644 | 526 | dc.DrawText( _T("GIF handler"), 30, 595 ); |
408b4168 | 527 | if (my_horse_gif && my_horse_gif->Ok()) |
be25e480 RR |
528 | dc.DrawBitmap( *my_horse_gif, 30, 610 ); |
529 | ||
51b07644 | 530 | dc.DrawText( _T("PCX handler"), 30, 825 ); |
408b4168 | 531 | if (my_horse_pcx && my_horse_pcx->Ok()) |
be25e480 RR |
532 | dc.DrawBitmap( *my_horse_pcx, 30, 840 ); |
533 | ||
51b07644 | 534 | dc.DrawText( _T("BMP handler"), 30, 1055 ); |
408b4168 | 535 | if (my_horse_bmp && my_horse_bmp->Ok()) |
be25e480 RR |
536 | dc.DrawBitmap( *my_horse_bmp, 30, 1070 ); |
537 | ||
51b07644 VZ |
538 | dc.DrawText( _T("BMP read from memory"), 280, 1055 ); |
539 | if (my_horse_bmp2 && my_horse_bmp2->Ok()) | |
540 | dc.DrawBitmap( *my_horse_bmp2, 280, 1070 ); | |
541 | ||
542 | dc.DrawText( _T("PNM handler"), 30, 1285 ); | |
408b4168 | 543 | if (my_horse_pnm && my_horse_pnm->Ok()) |
be25e480 | 544 | dc.DrawBitmap( *my_horse_pnm, 30, 1300 ); |
408b4168 | 545 | |
51b07644 | 546 | dc.DrawText( _T("TIFF handler"), 30, 1515 ); |
408b4168 | 547 | if (my_horse_tiff && my_horse_tiff->Ok()) |
4b05d973 | 548 | dc.DrawBitmap( *my_horse_tiff, 30, 1530 ); |
be25e480 | 549 | |
51b07644 | 550 | dc.DrawText( _T("XPM handler"), 30, 1745 ); |
9d8bdf2d VS |
551 | if (my_horse_xpm && my_horse_xpm->Ok()) |
552 | dc.DrawBitmap( *my_horse_xpm, 30, 1760 ); | |
553 | ||
bf504f98 | 554 | |
408b4168 | 555 | if (my_smile_xbm && my_smile_xbm->Ok()) |
be25e480 | 556 | { |
51b07644 VZ |
557 | dc.DrawText( _T("XBM bitmap"), 30, 1975 ); |
558 | dc.DrawText( _T("(green on red)"), 30, 1990 ); | |
559 | dc.SetTextForeground( _T("GREEN") ); | |
560 | dc.SetTextBackground( _T("RED") ); | |
9d8bdf2d | 561 | dc.DrawBitmap( *my_smile_xbm, 30, 2010 ); |
408b4168 | 562 | |
a60b1f5d | 563 | dc.SetTextForeground( wxT("BLACK") ); |
51b07644 VZ |
564 | dc.DrawText( _T("After wxImage conversion"), 150, 1975 ); |
565 | dc.DrawText( _T("(red on white)"), 150, 1990 ); | |
a60b1f5d | 566 | dc.SetTextForeground( wxT("RED") ); |
a8d4e3ac | 567 | wxImage i = my_smile_xbm->ConvertToImage(); |
176fe5af GRG |
568 | i.SetMaskColour( 255, 255, 255 ); |
569 | i.Replace( 0, 0, 0, | |
be25e480 RR |
570 | wxRED_PEN->GetColour().Red(), |
571 | wxRED_PEN->GetColour().Green(), | |
572 | wxRED_PEN->GetColour().Blue() ); | |
368d59f0 | 573 | dc.DrawBitmap( wxBitmap(i), 150, 2010, TRUE ); |
a60b1f5d | 574 | dc.SetTextForeground( wxT("BLACK") ); |
be25e480 | 575 | } |
176fe5af | 576 | |
408b4168 | 577 | |
41fbc841 | 578 | wxBitmap mono( 60,50,1 ); |
82ea63e6 RR |
579 | wxMemoryDC memdc; |
580 | memdc.SelectObject( mono ); | |
bfef20bf | 581 | memdc.SetPen( *wxBLACK_PEN ); |
82ea63e6 | 582 | memdc.SetBrush( *wxWHITE_BRUSH ); |
41fbc841 RR |
583 | memdc.DrawRectangle( 0,0,60,50 ); |
584 | memdc.SetTextForeground( *wxBLACK ); | |
51b07644 | 585 | memdc.DrawText( _T("Hi!"), 5, 5 ); |
41fbc841 RR |
586 | memdc.SetBrush( *wxBLACK_BRUSH ); |
587 | memdc.DrawRectangle( 33,5,20,20 ); | |
588 | memdc.SetPen( *wxRED_PEN ); | |
589 | memdc.DrawLine( 5, 42, 50, 42 ); | |
82ea63e6 | 590 | memdc.SelectObject( wxNullBitmap ); |
176fe5af | 591 | |
408b4168 | 592 | if (mono.Ok()) |
82ea63e6 | 593 | { |
51b07644 VZ |
594 | dc.DrawText( _T("Mono bitmap"), 30, 2095 ); |
595 | dc.DrawText( _T("(red on green)"), 30, 2110 ); | |
a60b1f5d MB |
596 | dc.SetTextForeground( wxT("RED") ); |
597 | dc.SetTextBackground( wxT("GREEN") ); | |
9d8bdf2d | 598 | dc.DrawBitmap( mono, 30, 2130 ); |
408b4168 | 599 | |
a60b1f5d | 600 | dc.SetTextForeground( wxT("BLACK") ); |
51b07644 VZ |
601 | dc.DrawText( _T("After wxImage conversion"), 150, 2095 ); |
602 | dc.DrawText( _T("(red on white)"), 150, 2110 ); | |
a60b1f5d | 603 | dc.SetTextForeground( wxT("RED") ); |
a8d4e3ac | 604 | wxImage i = mono.ConvertToImage(); |
408b4168 VZ |
605 | i.SetMaskColour( 255,255,255 ); |
606 | i.Replace( 0,0,0, | |
82ea63e6 RR |
607 | wxRED_PEN->GetColour().Red(), |
608 | wxRED_PEN->GetColour().Green(), | |
609 | wxRED_PEN->GetColour().Blue() ); | |
368d59f0 | 610 | dc.DrawBitmap( wxBitmap(i), 150, 2130, TRUE ); |
a60b1f5d | 611 | dc.SetTextForeground( wxT("BLACK") ); |
bea56879 VZ |
612 | } |
613 | ||
51b07644 | 614 | dc.DrawText(_T("XPM bitmap"), 30, 2230); |
bea56879 VZ |
615 | if ( m_bmpSmileXpm.Ok() ) |
616 | { | |
9d8bdf2d | 617 | dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, TRUE); |
bea56879 VZ |
618 | } |
619 | ||
51b07644 | 620 | dc.DrawText(_T("XPM icon"), 150, 2230); |
bea56879 VZ |
621 | if ( m_iconSmileXpm.Ok() ) |
622 | { | |
9d8bdf2d | 623 | dc.DrawIcon(m_iconSmileXpm, 150, 2250); |
82ea63e6 | 624 | } |
bf504f98 | 625 | |
51b07644 | 626 | dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 ); |
bf504f98 VS |
627 | if (my_horse_ico32 && my_horse_ico32->Ok()) |
628 | dc.DrawBitmap( *my_horse_ico32, 30, 2330, TRUE ); | |
629 | ||
51b07644 | 630 | dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 ); |
bf504f98 VS |
631 | if (my_horse_ico16 && my_horse_ico16->Ok()) |
632 | dc.DrawBitmap( *my_horse_ico16, 230, 2330, TRUE ); | |
51b07644 VZ |
633 | |
634 | dc.DrawText( _T("ICO handler (best image)"), 430, 2290 ); | |
bf504f98 VS |
635 | if (my_horse_ico && my_horse_ico->Ok()) |
636 | dc.DrawBitmap( *my_horse_ico, 430, 2330, TRUE ); | |
637 | ||
51b07644 | 638 | dc.DrawText( _T("CUR handler"), 30, 2390 ); |
bf504f98 | 639 | if (my_horse_cur && my_horse_cur->Ok()) |
51b07644 | 640 | { |
bf504f98 VS |
641 | dc.DrawBitmap( *my_horse_cur, 30, 2420, TRUE ); |
642 | dc.SetPen (*wxRED_PEN); | |
643 | dc.DrawLine (xH-10,yH,xH+10,yH); | |
644 | dc.DrawLine (xH,yH-10,xH,yH+10); | |
51b07644 | 645 | } |
2b5f62a0 VZ |
646 | dc.DrawText( _T("ANI handler"), 230, 2390 ); |
647 | int i ; | |
648 | for (i=0; i < m_ani_images; i ++) | |
649 | if (my_horse_ani[i].Ok()) | |
650 | { | |
651 | dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, TRUE ); | |
652 | } | |
9390a202 | 653 | } |
1d5b7a0b | 654 | |
9390a202 RR |
655 | void MyCanvas::CreateAntiAliasedBitmap() |
656 | { | |
657 | wxBitmap bitmap( 300, 300 ); | |
a91b47e8 | 658 | |
9390a202 | 659 | wxMemoryDC dc; |
a91b47e8 | 660 | |
9390a202 | 661 | dc.SelectObject( bitmap ); |
a91b47e8 | 662 | |
9390a202 | 663 | dc.Clear(); |
f048e32f | 664 | |
6e47faf1 | 665 | dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) ); |
a60b1f5d | 666 | dc.SetTextForeground( wxT("RED") ); |
51b07644 VZ |
667 | dc.DrawText( _T("This is anti-aliased Text."), 20, 20 ); |
668 | dc.DrawText( _T("And a Rectangle."), 20, 60 ); | |
f048e32f | 669 | |
9390a202 | 670 | dc.SetBrush( *wxRED_BRUSH ); |
91b8de8d | 671 | dc.SetPen( *wxTRANSPARENT_PEN ); |
9390a202 | 672 | dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 ); |
f048e32f | 673 | |
a8d4e3ac | 674 | wxImage original= bitmap.ConvertToImage(); |
9390a202 | 675 | wxImage anti( 150, 150 ); |
a91b47e8 | 676 | |
9390a202 | 677 | /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */ |
f048e32f | 678 | |
9390a202 RR |
679 | for (int y = 1; y < 149; y++) |
680 | for (int x = 1; x < 149; x++) | |
681 | { | |
682 | int red = original.GetRed( x*2, y*2 ) + | |
f048e32f VZ |
683 | original.GetRed( x*2-1, y*2 ) + |
684 | original.GetRed( x*2, y*2+1 ) + | |
9390a202 | 685 | original.GetRed( x*2+1, y*2+1 ); |
f048e32f VZ |
686 | red = red/4; |
687 | ||
9390a202 | 688 | int green = original.GetGreen( x*2, y*2 ) + |
f048e32f VZ |
689 | original.GetGreen( x*2-1, y*2 ) + |
690 | original.GetGreen( x*2, y*2+1 ) + | |
9390a202 | 691 | original.GetGreen( x*2+1, y*2+1 ); |
f048e32f VZ |
692 | green = green/4; |
693 | ||
9390a202 | 694 | int blue = original.GetBlue( x*2, y*2 ) + |
f048e32f VZ |
695 | original.GetBlue( x*2-1, y*2 ) + |
696 | original.GetBlue( x*2, y*2+1 ) + | |
9390a202 | 697 | original.GetBlue( x*2+1, y*2+1 ); |
f048e32f | 698 | blue = blue/4; |
9390a202 RR |
699 | anti.SetRGB( x, y, red, green, blue ); |
700 | } | |
368d59f0 | 701 | my_anti = new wxBitmap(anti); |
01111366 RR |
702 | } |
703 | ||
704 | // MyFrame | |
705 | ||
79490c3d VZ |
706 | const int ID_QUIT = 108; |
707 | const int ID_ABOUT = 109; | |
ab0f0386 | 708 | const int ID_NEW = 110; |
01111366 RR |
709 | |
710 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
711 | ||
712 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
713 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) | |
714 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
ab0f0386 | 715 | EVT_MENU (ID_NEW, MyFrame::OnNewFrame) |
01111366 RR |
716 | END_EVENT_TABLE() |
717 | ||
1d5b7a0b | 718 | MyFrame::MyFrame() |
51b07644 | 719 | : wxFrame( (wxFrame *)NULL, -1, _T("wxImage sample"), |
1d5b7a0b | 720 | wxPoint(20,20), wxSize(470,360) ) |
01111366 RR |
721 | { |
722 | wxMenu *file_menu = new wxMenu(); | |
51b07644 | 723 | file_menu->Append( ID_NEW, _T("&Show image...")); |
ab0f0386 | 724 | file_menu->AppendSeparator(); |
51b07644 | 725 | file_menu->Append( ID_ABOUT, _T("&About...")); |
ab0f0386 | 726 | file_menu->AppendSeparator(); |
51b07644 | 727 | file_menu->Append( ID_QUIT, _T("E&xit")); |
1d5b7a0b | 728 | |
01111366 | 729 | wxMenuBar *menu_bar = new wxMenuBar(); |
51b07644 | 730 | menu_bar->Append(file_menu, _T("&File")); |
3d05544e | 731 | |
01111366 | 732 | SetMenuBar( menu_bar ); |
1d5b7a0b | 733 | |
917e533b RR |
734 | CreateStatusBar(2); |
735 | int widths[] = { -1, 100 }; | |
736 | SetStatusWidths( 2, widths ); | |
1d5b7a0b | 737 | |
fb1585ae | 738 | m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) ); |
cbd4be25 | 739 | |
bf504f98 VS |
740 | // 500 width * 2500 height |
741 | m_canvas->SetScrollbars( 10, 10, 50, 250 ); | |
01111366 RR |
742 | } |
743 | ||
744 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
745 | { | |
746 | Close( TRUE ); | |
747 | } | |
748 | ||
749 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
750 | { | |
51b07644 VZ |
751 | (void)wxMessageBox( _T("wxImage demo\n") |
752 | _T("Robert Roebling (c) 1998,2000"), | |
753 | _T("About wxImage Demo"), wxICON_INFORMATION | wxOK ); | |
fb1585ae RR |
754 | } |
755 | ||
ab0f0386 VZ |
756 | void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) ) |
757 | { | |
f6bcfd97 BP |
758 | wxString filename = wxFileSelector(_T("Select image file")); |
759 | if ( !filename ) | |
760 | return; | |
761 | ||
762 | wxImage image; | |
763 | if ( !image.LoadFile(filename) ) | |
764 | { | |
765 | wxLogError(_T("Couldn't load image from '%s'."), filename.c_str()); | |
766 | ||
767 | return; | |
768 | } | |
769 | ||
368d59f0 | 770 | (new MyImageFrame(this, wxBitmap(image)))->Show(); |
ab0f0386 VZ |
771 | } |
772 | ||
01111366 RR |
773 | //----------------------------------------------------------------------------- |
774 | // MyApp | |
775 | //----------------------------------------------------------------------------- | |
776 | ||
1d5b7a0b | 777 | bool MyApp::OnInit() |
01111366 | 778 | { |
e9c4b1a2 JS |
779 | #if wxUSE_LIBPNG |
780 | wxImage::AddHandler( new wxPNGHandler ); | |
781 | #endif | |
782 | ||
783 | #if wxUSE_LIBJPEG | |
784 | wxImage::AddHandler( new wxJPEGHandler ); | |
785 | #endif | |
786 | ||
60a41aee RR |
787 | #if wxUSE_LIBTIFF |
788 | wxImage::AddHandler( new wxTIFFHandler ); | |
789 | #endif | |
790 | ||
6e47faf1 | 791 | #if wxUSE_GIF |
e1929140 | 792 | wxImage::AddHandler( new wxGIFHandler ); |
6e47faf1 JS |
793 | #endif |
794 | ||
795 | #if wxUSE_PCX | |
cbd4be25 | 796 | wxImage::AddHandler( new wxPCXHandler ); |
6e47faf1 JS |
797 | #endif |
798 | ||
799 | #if wxUSE_PNM | |
6319afe3 | 800 | wxImage::AddHandler( new wxPNMHandler ); |
6e47faf1 | 801 | #endif |
e1929140 | 802 | |
a8d4e3ac VS |
803 | #if wxUSE_XPM |
804 | wxImage::AddHandler( new wxXPMHandler ); | |
805 | #endif | |
806 | ||
bf504f98 VS |
807 | #if wxUSE_ICO_CUR |
808 | wxImage::AddHandler( new wxICOHandler ); | |
809 | wxImage::AddHandler( new wxCURHandler ); | |
2b5f62a0 | 810 | wxImage::AddHandler( new wxANIHandler ); |
bf504f98 VS |
811 | #endif |
812 | ||
01111366 RR |
813 | wxFrame *frame = new MyFrame(); |
814 | frame->Show( TRUE ); | |
1d5b7a0b | 815 | |
01111366 RR |
816 | return TRUE; |
817 | } | |
818 |