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