]>
Commit | Line | Data |
---|---|---|
5a566d89 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: samples/image/image.cpp | |
3 | // Purpose: sample showing operations with wxImage | |
4 | // Author: Robert Roebling | |
5 | // Modified by: | |
6 | // Created: 1998 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998-2005 Robert Roebling | |
9 | // License: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
01111366 | 11 | |
3d05544e JS |
12 | // For compilers that support precompilation, includes "wx/wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
01111366 | 20 | #include "wx/wx.h" |
3d05544e JS |
21 | #endif |
22 | ||
01111366 | 23 | #include "wx/image.h" |
a23fd0e1 | 24 | #include "wx/file.h" |
5a566d89 | 25 | #include "wx/filename.h" |
a8d4e3ac VS |
26 | #include "wx/mstream.h" |
27 | #include "wx/wfstream.h" | |
1971d23c | 28 | #include "wx/quantize.h" |
a23fd0e1 | 29 | |
da9df1f5 VZ |
30 | #if wxUSE_CLIPBOARD |
31 | #include "wx/dataobj.h" | |
32 | #include "wx/clipbrd.h" | |
33 | #endif // wxUSE_CLIPBOARD | |
34 | ||
dc1dbfc6 | 35 | #include "smile.xbm" |
3ef37e7f | 36 | #include "smile.xpm" |
dc1dbfc6 | 37 | |
284f2b59 | 38 | #if defined(__WXMSW__) |
481721e8 | 39 | #ifdef wxHAVE_RAW_BITMAP |
9b24b388 | 40 | #include "wx/rawbmp.h" |
481721e8 | 41 | #endif |
c698eae5 | 42 | #endif |
a8d4e3ac | 43 | |
284f2b59 RR |
44 | #if defined(__WXMAC__) || defined(__WXGTK__) |
45 | #define wxHAVE_RAW_BITMAP | |
46 | #include "wx/rawbmp.h" | |
47 | #endif | |
48 | ||
01111366 RR |
49 | // derived classes |
50 | ||
51 | class MyFrame; | |
52 | class MyApp; | |
53 | ||
54 | // MyCanvas | |
55 | ||
56 | class MyCanvas: public wxScrolledWindow | |
57 | { | |
1d5b7a0b | 58 | public: |
51b07644 | 59 | MyCanvas() {} |
01111366 | 60 | MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size ); |
1d5b7a0b | 61 | ~MyCanvas(); |
01111366 | 62 | void OnPaint( wxPaintEvent &event ); |
9390a202 | 63 | void CreateAntiAliasedBitmap(); |
1d5b7a0b | 64 | |
e1929140 RR |
65 | wxBitmap *my_horse_png; |
66 | wxBitmap *my_horse_jpeg; | |
67 | wxBitmap *my_horse_gif; | |
ca26177c | 68 | wxBitmap *my_horse_bmp; |
51b07644 | 69 | wxBitmap *my_horse_bmp2; |
cbd4be25 | 70 | wxBitmap *my_horse_pcx; |
6319afe3 | 71 | wxBitmap *my_horse_pnm; |
60a41aee | 72 | wxBitmap *my_horse_tiff; |
9d8bdf2d | 73 | wxBitmap *my_horse_xpm; |
bf504f98 VS |
74 | wxBitmap *my_horse_ico32; |
75 | wxBitmap *my_horse_ico16; | |
76 | wxBitmap *my_horse_ico; | |
77 | wxBitmap *my_horse_cur; | |
2b5f62a0 | 78 | wxBitmap *my_horse_ani; |
51b07644 | 79 | |
dc1dbfc6 | 80 | wxBitmap *my_smile_xbm; |
e8fdc264 | 81 | wxBitmap *my_square; |
9390a202 | 82 | wxBitmap *my_anti; |
1d5b7a0b | 83 | |
3e8711ce JS |
84 | wxBitmap *my_horse_asciigrey_pnm; |
85 | wxBitmap *my_horse_rawgrey_pnm; | |
86 | ||
978d3d36 VZ |
87 | wxBitmap *colorized_horse_jpeg; |
88 | ||
bf504f98 | 89 | int xH, yH ; |
2b5f62a0 | 90 | int m_ani_images ; |
bf504f98 | 91 | |
bea56879 VZ |
92 | protected: |
93 | wxBitmap m_bmpSmileXpm; | |
94 | wxIcon m_iconSmileXpm; | |
95 | ||
60a41aee | 96 | private: |
1d5b7a0b VZ |
97 | DECLARE_DYNAMIC_CLASS(MyCanvas) |
98 | DECLARE_EVENT_TABLE() | |
01111366 RR |
99 | }; |
100 | ||
18134a1c | 101 | |
01111366 RR |
102 | // MyFrame |
103 | ||
18134a1c | 104 | |
01111366 RR |
105 | class MyFrame: public wxFrame |
106 | { | |
1d5b7a0b VZ |
107 | public: |
108 | MyFrame(); | |
01111366 | 109 | |
01111366 | 110 | void OnAbout( wxCommandEvent &event ); |
ab0f0386 | 111 | void OnNewFrame( wxCommandEvent &event ); |
b3f04dc2 VZ |
112 | #ifdef wxHAVE_RAW_BITMAP |
113 | void OnTestRawBitmap( wxCommandEvent &event ); | |
114 | #endif // wxHAVE_RAW_BITMAP | |
01111366 | 115 | void OnQuit( wxCommandEvent &event ); |
1d5b7a0b | 116 | |
dd38c875 | 117 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
118 | void OnCopy(wxCommandEvent& event); |
119 | void OnPaste(wxCommandEvent& event); | |
120 | #endif // wxUSE_CLIPBOARD | |
121 | ||
01111366 | 122 | MyCanvas *m_canvas; |
1d5b7a0b | 123 | |
60a41aee | 124 | private: |
1d5b7a0b VZ |
125 | DECLARE_DYNAMIC_CLASS(MyFrame) |
126 | DECLARE_EVENT_TABLE() | |
01111366 RR |
127 | }; |
128 | ||
ab0f0386 VZ |
129 | class MyImageFrame : public wxFrame |
130 | { | |
131 | public: | |
132 | MyImageFrame(wxFrame *parent, const wxBitmap& bitmap) | |
47f797bd | 133 | : wxFrame(parent, wxID_ANY, _T("Double click to save"), |
ab0f0386 | 134 | wxDefaultPosition, wxDefaultSize, |
40c79fab | 135 | wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX), |
1971d23c | 136 | m_bitmap(bitmap) |
ab0f0386 VZ |
137 | { |
138 | SetClientSize(bitmap.GetWidth(), bitmap.GetHeight()); | |
139 | } | |
140 | ||
0c543b7a VZ |
141 | void OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
142 | { | |
143 | // do nothing here to be able to see how transparent images are shown | |
144 | } | |
145 | ||
ab0f0386 VZ |
146 | void OnPaint(wxPaintEvent& WXUNUSED(event)) |
147 | { | |
148 | wxPaintDC dc( this ); | |
47f797bd | 149 | dc.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ ); |
ab0f0386 VZ |
150 | } |
151 | ||
c5c843e0 | 152 | void OnSave(wxMouseEvent& WXUNUSED(event)) |
1971d23c | 153 | { |
368d59f0 | 154 | wxImage image = m_bitmap.ConvertToImage(); |
1971d23c | 155 | |
4693b20c MB |
156 | wxString savefilename = wxFileSelector( wxT("Save Image"), |
157 | wxT(""), | |
5a566d89 | 158 | wxT(""), |
4693b20c | 159 | (const wxChar *)NULL, |
d0ee33f5 WS |
160 | wxT("BMP files (*.bmp)|*.bmp|") |
161 | wxT("PNG files (*.png)|*.png|") | |
162 | wxT("JPEG files (*.jpg)|*.jpg|") | |
163 | wxT("GIF files (*.gif)|*.gif|") | |
164 | wxT("TIFF files (*.tif)|*.tif|") | |
165 | wxT("PCX files (*.pcx)|*.pcx|") | |
166 | wxT("ICO files (*.ico)|*.ico|") | |
167 | wxT("CUR files (*.cur)|*.cur"), | |
47f797bd WS |
168 | wxSAVE, |
169 | this); | |
1971d23c VZ |
170 | |
171 | if ( savefilename.empty() ) | |
172 | return; | |
173 | ||
5a566d89 VZ |
174 | wxString extension; |
175 | wxFileName::SplitPath(savefilename, NULL, NULL, &extension); | |
1971d23c | 176 | |
5a566d89 VZ |
177 | bool saved = false; |
178 | if ( extension == _T("bpp") ) | |
179 | { | |
180 | static const int bppvalues[] = | |
181 | { | |
182 | wxBMP_1BPP, | |
183 | wxBMP_1BPP_BW, | |
184 | wxBMP_4BPP, | |
185 | wxBMP_8BPP, | |
186 | wxBMP_8BPP_GREY, | |
187 | wxBMP_8BPP_RED, | |
188 | wxBMP_8BPP_PALETTE, | |
189 | wxBMP_24BPP | |
190 | }; | |
191 | ||
24207dfc | 192 | const wxString bppchoices[] = |
5a566d89 VZ |
193 | { |
194 | _T("1 bpp color"), | |
195 | _T("1 bpp B&W"), | |
196 | _T("4 bpp color"), | |
197 | _T("8 bpp color"), | |
198 | _T("8 bpp greyscale"), | |
199 | _T("8 bpp red"), | |
200 | _T("8 bpp own palette"), | |
201 | _T("24 bpp") | |
202 | }; | |
203 | ||
204 | int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"), | |
205 | _T("Image sample: save file"), | |
206 | WXSIZEOF(bppchoices), | |
207 | bppchoices, | |
208 | this); | |
209 | if ( bppselection != -1 ) | |
210 | { | |
211 | int format = bppvalues[bppselection]; | |
212 | image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, format); | |
213 | ||
214 | if ( format == wxBMP_8BPP_PALETTE ) | |
215 | { | |
216 | unsigned char *cmap = new unsigned char [256]; | |
217 | for ( int i = 0; i < 256; i++ ) | |
218 | cmap[i] = (unsigned char)i; | |
219 | image.SetPalette(wxPalette(256, cmap, cmap, cmap)); | |
220 | ||
d0ee33f5 | 221 | delete[] cmap; |
5a566d89 VZ |
222 | } |
223 | } | |
1971d23c | 224 | } |
5a566d89 VZ |
225 | else if ( extension == _T("png") ) |
226 | { | |
227 | static const int pngvalues[] = | |
228 | { | |
229 | wxPNG_TYPE_COLOUR, | |
230 | wxPNG_TYPE_COLOUR, | |
231 | wxPNG_TYPE_GREY, | |
232 | wxPNG_TYPE_GREY, | |
233 | wxPNG_TYPE_GREY_RED, | |
234 | wxPNG_TYPE_GREY_RED, | |
235 | }; | |
236 | ||
24207dfc | 237 | const wxString pngchoices[] = |
5a566d89 VZ |
238 | { |
239 | _T("Colour 8bpp"), | |
240 | _T("Colour 16bpp"), | |
241 | _T("Grey 8bpp"), | |
242 | _T("Grey 16bpp"), | |
243 | _T("Grey red 8bpp"), | |
244 | _T("Grey red 16bpp"), | |
245 | }; | |
246 | ||
247 | int sel = wxGetSingleChoiceIndex(_T("Set PNG format"), | |
248 | _T("Image sample: save file"), | |
249 | WXSIZEOF(pngchoices), | |
250 | pngchoices, | |
251 | this); | |
252 | if ( sel != -1 ) | |
253 | { | |
254 | image.SetOption(wxIMAGE_OPTION_PNG_FORMAT, pngvalues[sel]); | |
255 | image.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH, sel % 2 ? 16 : 8); | |
256 | } | |
257 | } | |
258 | else if ( extension == _T("cur") ) | |
45647dcf | 259 | { |
51b07644 VZ |
260 | image.Rescale(32,32); |
261 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0); | |
45647dcf VS |
262 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0); |
263 | // This shows how you can save an image with explicitly | |
264 | // specified image format: | |
5a566d89 | 265 | saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR); |
45647dcf | 266 | } |
5a566d89 VZ |
267 | |
268 | if ( !saved ) | |
45647dcf VS |
269 | { |
270 | // This one guesses image format from filename extension | |
271 | // (it may fail if the extension is not recognized): | |
5a566d89 | 272 | image.SaveFile(savefilename); |
45647dcf | 273 | } |
1971d23c VZ |
274 | } |
275 | ||
ab0f0386 VZ |
276 | private: |
277 | wxBitmap m_bitmap; | |
278 | ||
279 | DECLARE_EVENT_TABLE() | |
280 | }; | |
281 | ||
b3f04dc2 VZ |
282 | #ifdef wxHAVE_RAW_BITMAP |
283 | ||
284 | #include "wx/rawbmp.h" | |
285 | ||
286 | class MyRawBitmapFrame : public wxFrame | |
287 | { | |
288 | public: | |
289 | enum | |
290 | { | |
291 | BORDER = 15, | |
292 | SIZE = 150, | |
293 | REAL_SIZE = SIZE - 2*BORDER | |
294 | }; | |
295 | ||
296 | MyRawBitmapFrame(wxFrame *parent) | |
47f797bd | 297 | : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")), |
b3f04dc2 VZ |
298 | m_bitmap(SIZE, SIZE, 32) |
299 | { | |
300 | SetClientSize(SIZE, SIZE); | |
301 | ||
095980e1 VZ |
302 | // another possibility: wxNativePixelData (don't forget to remove code |
303 | // setting alpha in the loop below then) | |
304 | typedef wxAlphaPixelData Data; | |
305 | ||
306 | Data data(m_bitmap, wxPoint(BORDER, BORDER), wxSize(REAL_SIZE, REAL_SIZE)); | |
b3f04dc2 VZ |
307 | if ( !data ) |
308 | { | |
309 | wxLogError(_T("Failed to gain raw access to bitmap data")); | |
310 | return; | |
311 | } | |
312 | ||
4a101327 | 313 | data.UseAlpha(); |
10b41b53 | 314 | |
095980e1 | 315 | Data::Iterator p(data); |
b3f04dc2 | 316 | |
b3f04dc2 VZ |
317 | for ( int y = 0; y < REAL_SIZE; ++y ) |
318 | { | |
095980e1 | 319 | Data::Iterator rowStart = p; |
b3f04dc2 VZ |
320 | |
321 | int r = y < REAL_SIZE/3 ? 255 : 0, | |
322 | g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0, | |
323 | b = 2*(REAL_SIZE/3) <= y ? 255 : 0; | |
324 | ||
325 | for ( int x = 0; x < REAL_SIZE; ++x ) | |
326 | { | |
b068dfe2 VZ |
327 | // note that RGB must be premultiplied by alpha |
328 | unsigned a = (Data::Iterator::ChannelType)((x*255.)/REAL_SIZE); | |
5c99abbe MW |
329 | p.Red() = r * a / 256; |
330 | p.Green() = g * a / 256; | |
331 | p.Blue() = b * a / 256; | |
b068dfe2 | 332 | p.Alpha() = a; |
b3f04dc2 VZ |
333 | |
334 | ++p; // same as p.OffsetX(1) | |
335 | } | |
336 | ||
337 | p = rowStart; | |
1d2f48b6 | 338 | p.OffsetY(data, 1); |
b3f04dc2 VZ |
339 | } |
340 | } | |
341 | ||
342 | void OnPaint(wxPaintEvent& WXUNUSED(event)) | |
343 | { | |
344 | wxPaintDC dc( this ); | |
345 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER); | |
346 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER); | |
347 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER); | |
47f797bd | 348 | dc.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ ); |
b3f04dc2 VZ |
349 | } |
350 | ||
351 | private: | |
352 | wxBitmap m_bitmap; | |
353 | ||
354 | DECLARE_EVENT_TABLE() | |
355 | }; | |
356 | ||
357 | #endif // wxHAVE_RAW_BITMAP | |
358 | ||
01111366 RR |
359 | // MyApp |
360 | ||
361 | class MyApp: public wxApp | |
362 | { | |
1d5b7a0b VZ |
363 | public: |
364 | virtual bool OnInit(); | |
01111366 RR |
365 | }; |
366 | ||
367 | // main program | |
368 | ||
369 | IMPLEMENT_APP(MyApp) | |
370 | ||
371 | // MyCanvas | |
372 | ||
373 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) | |
374 | ||
ab0f0386 | 375 | BEGIN_EVENT_TABLE(MyImageFrame, wxFrame) |
0c543b7a VZ |
376 | EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground) |
377 | EVT_PAINT(MyImageFrame::OnPaint) | |
378 | EVT_LEFT_DCLICK(MyImageFrame::OnSave) | |
ab0f0386 VZ |
379 | END_EVENT_TABLE() |
380 | ||
b3f04dc2 VZ |
381 | #ifdef wxHAVE_RAW_BITMAP |
382 | ||
383 | BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame) | |
384 | EVT_PAINT(MyRawBitmapFrame::OnPaint) | |
385 | END_EVENT_TABLE() | |
386 | ||
387 | #endif // wxHAVE_RAW_BITMAP | |
388 | ||
1d5b7a0b VZ |
389 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) |
390 | EVT_PAINT(MyCanvas::OnPaint) | |
01111366 RR |
391 | END_EVENT_TABLE() |
392 | ||
acbd13a3 | 393 | MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, |
1d5b7a0b | 394 | const wxPoint &pos, const wxSize &size ) |
81278df2 | 395 | : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER ) |
81278df2 VZ |
396 | , m_bmpSmileXpm((const char **) smile_xpm) |
397 | , m_iconSmileXpm((const char **) smile_xpm) | |
01111366 | 398 | { |
60a41aee RR |
399 | my_horse_png = (wxBitmap*) NULL; |
400 | my_horse_jpeg = (wxBitmap*) NULL; | |
401 | my_horse_gif = (wxBitmap*) NULL; | |
402 | my_horse_bmp = (wxBitmap*) NULL; | |
51b07644 | 403 | my_horse_bmp2 = (wxBitmap*) NULL; |
60a41aee RR |
404 | my_horse_pcx = (wxBitmap*) NULL; |
405 | my_horse_pnm = (wxBitmap*) NULL; | |
406 | my_horse_tiff = (wxBitmap*) NULL; | |
9d8bdf2d | 407 | my_horse_xpm = (wxBitmap*) NULL; |
bf504f98 VS |
408 | my_horse_ico32 = (wxBitmap*) NULL; |
409 | my_horse_ico16 = (wxBitmap*) NULL; | |
410 | my_horse_ico = (wxBitmap*) NULL; | |
411 | my_horse_cur = (wxBitmap*) NULL; | |
2b5f62a0 | 412 | my_horse_ani = (wxBitmap*) NULL; |
978d3d36 | 413 | colorized_horse_jpeg = (wxBitmap*) NULL; |
bf504f98 | 414 | |
dc1dbfc6 | 415 | my_smile_xbm = (wxBitmap*) NULL; |
60a41aee RR |
416 | my_square = (wxBitmap*) NULL; |
417 | my_anti = (wxBitmap*) NULL; | |
418 | ||
3e8711ce JS |
419 | my_horse_asciigrey_pnm = (wxBitmap*) NULL; |
420 | my_horse_rawgrey_pnm = (wxBitmap*) NULL; | |
421 | ||
2b5f62a0 VZ |
422 | m_ani_images = 0 ; |
423 | ||
60a41aee RR |
424 | SetBackgroundColour(* wxWHITE); |
425 | ||
426 | wxBitmap bitmap( 100, 100 ); | |
427 | ||
428 | wxMemoryDC dc; | |
429 | dc.SelectObject( bitmap ); | |
a60b1f5d | 430 | dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) ); |
cf3da716 | 431 | dc.SetPen( *wxBLACK_PEN ); |
60a41aee | 432 | dc.DrawRectangle( 0, 0, 100, 100 ); |
cf3da716 RR |
433 | dc.SetBrush( *wxWHITE_BRUSH ); |
434 | dc.DrawRectangle( 20, 20, 60, 60 ); | |
60a41aee RR |
435 | dc.SelectObject( wxNullBitmap ); |
436 | ||
437 | // try to find the directory with our images | |
438 | wxString dir; | |
4693b20c | 439 | if ( wxFile::Exists(wxT("./horse.png")) ) |
2b5f62a0 | 440 | dir = wxT("./"); |
4693b20c | 441 | else if ( wxFile::Exists(wxT("../horse.png")) ) |
2b5f62a0 | 442 | dir = wxT("../"); |
60a41aee | 443 | else |
4693b20c | 444 | wxLogWarning(wxT("Can't find image files in either '.' or '..'!")); |
60a41aee | 445 | |
a8d4e3ac | 446 | wxImage image = bitmap.ConvertToImage(); |
60a41aee | 447 | |
bea56879 | 448 | #if wxUSE_LIBPNG |
51b07644 | 449 | if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG )) |
4693b20c | 450 | wxLogError(wxT("Can't save file")); |
f6bcfd97 | 451 | |
176fe5af | 452 | image.Destroy(); |
60a41aee | 453 | |
30ad0a14 VZ |
454 | if ( image.LoadFile( dir + _T("test.png") ) ) |
455 | my_square = new wxBitmap( image ); | |
f6bcfd97 | 456 | |
176fe5af | 457 | image.Destroy(); |
cc977e5f | 458 | |
51b07644 | 459 | if ( !image.LoadFile( dir + _T("horse.png")) ) |
4693b20c | 460 | wxLogError(wxT("Can't load PNG image")); |
60a41aee | 461 | else |
a8d4e3ac | 462 | my_horse_png = new wxBitmap( image ); |
bea56879 | 463 | #endif // wxUSE_LIBPNG |
60a41aee | 464 | |
bea56879 | 465 | #if wxUSE_LIBJPEG |
176fe5af | 466 | image.Destroy(); |
279ababf | 467 | |
51b07644 | 468 | if ( !image.LoadFile( dir + _T("horse.jpg")) ) |
4693b20c | 469 | wxLogError(wxT("Can't load JPG image")); |
60a41aee | 470 | else |
978d3d36 | 471 | { |
9d8bdf2d | 472 | my_horse_jpeg = new wxBitmap( image ); |
978d3d36 VZ |
473 | // Colorize by rotating green hue to red |
474 | wxImage::HSVValue greenHSV = wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0)); | |
475 | wxImage::HSVValue redHSV = wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0)); | |
476 | image.RotateHue(redHSV.hue - greenHSV.hue); | |
477 | colorized_horse_jpeg = new wxBitmap( image ); | |
478 | } | |
bea56879 | 479 | #endif // wxUSE_LIBJPEG |
6e47faf1 JS |
480 | |
481 | #if wxUSE_GIF | |
176fe5af | 482 | image.Destroy(); |
279ababf | 483 | |
2b5f62a0 | 484 | if ( !image.LoadFile( dir + _T("horse.gif" )) ) |
4693b20c | 485 | wxLogError(wxT("Can't load GIF image")); |
60a41aee | 486 | else |
9d8bdf2d | 487 | my_horse_gif = new wxBitmap( image ); |
6e47faf1 | 488 | #endif |
cbd4be25 | 489 | |
6e47faf1 | 490 | #if wxUSE_PCX |
176fe5af | 491 | image.Destroy(); |
279ababf | 492 | |
51b07644 | 493 | if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) ) |
4693b20c | 494 | wxLogError(wxT("Can't load PCX image")); |
60a41aee | 495 | else |
9d8bdf2d | 496 | my_horse_pcx = new wxBitmap( image ); |
6e47faf1 | 497 | #endif |
cbd4be25 | 498 | |
176fe5af | 499 | image.Destroy(); |
279ababf | 500 | |
51b07644 | 501 | if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) ) |
4693b20c | 502 | wxLogError(wxT("Can't load BMP image")); |
60a41aee | 503 | else |
9d8bdf2d | 504 | my_horse_bmp = new wxBitmap( image ); |
6319afe3 | 505 | |
a8d4e3ac | 506 | #if wxUSE_XPM |
9d8bdf2d VS |
507 | image.Destroy(); |
508 | ||
51b07644 | 509 | if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) ) |
4693b20c | 510 | wxLogError(wxT("Can't load XPM image")); |
9d8bdf2d VS |
511 | else |
512 | my_horse_xpm = new wxBitmap( image ); | |
513 | ||
51b07644 | 514 | if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM )) |
4693b20c | 515 | wxLogError(wxT("Can't save file")); |
a8d4e3ac VS |
516 | #endif |
517 | ||
6e47faf1 | 518 | #if wxUSE_PNM |
176fe5af | 519 | image.Destroy(); |
279ababf | 520 | |
51b07644 | 521 | if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) ) |
4693b20c | 522 | wxLogError(wxT("Can't load PNM image")); |
60a41aee | 523 | else |
9d8bdf2d | 524 | my_horse_pnm = new wxBitmap( image ); |
3e8711ce JS |
525 | |
526 | image.Destroy(); | |
527 | ||
528 | if ( !image.LoadFile( dir + _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM ) ) | |
529 | wxLogError(wxT("Can't load PNM image")); | |
530 | else | |
531 | my_horse_asciigrey_pnm = new wxBitmap( image ); | |
532 | ||
533 | image.Destroy(); | |
534 | ||
535 | if ( !image.LoadFile( dir + _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM ) ) | |
536 | wxLogError(wxT("Can't load PNM image")); | |
537 | else | |
538 | my_horse_rawgrey_pnm = new wxBitmap( image ); | |
6e47faf1 | 539 | #endif |
f048e32f | 540 | |
60a41aee | 541 | #if wxUSE_LIBTIFF |
176fe5af | 542 | image.Destroy(); |
279ababf | 543 | |
51b07644 | 544 | if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) ) |
4693b20c | 545 | wxLogError(wxT("Can't load TIFF image")); |
60a41aee | 546 | else |
9d8bdf2d | 547 | my_horse_tiff = new wxBitmap( image ); |
60a41aee RR |
548 | #endif |
549 | ||
60a41aee | 550 | CreateAntiAliasedBitmap(); |
dc1dbfc6 RL |
551 | |
552 | my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width, | |
553 | smile_height, 1 ); | |
bfef20bf GRG |
554 | |
555 | // demonstrates XPM automatically using the mask when saving | |
556 | if ( m_bmpSmileXpm.Ok() ) | |
51b07644 | 557 | m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM); |
bf504f98 VS |
558 | |
559 | #if wxUSE_ICO_CUR | |
560 | image.Destroy(); | |
561 | ||
51b07644 | 562 | if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) ) |
bf504f98 VS |
563 | wxLogError(wxT("Can't load first ICO image")); |
564 | else | |
565 | my_horse_ico32 = new wxBitmap( image ); | |
51b07644 | 566 | |
bf504f98 VS |
567 | image.Destroy(); |
568 | ||
51b07644 | 569 | if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) ) |
bf504f98 VS |
570 | wxLogError(wxT("Can't load second ICO image")); |
571 | else | |
572 | my_horse_ico16 = new wxBitmap( image ); | |
573 | ||
574 | image.Destroy(); | |
575 | ||
51b07644 | 576 | if ( !image.LoadFile( dir + _T("horse.ico") ) ) |
bf504f98 VS |
577 | wxLogError(wxT("Can't load best ICO image")); |
578 | else | |
579 | my_horse_ico = new wxBitmap( image ); | |
580 | ||
581 | image.Destroy(); | |
582 | ||
51b07644 | 583 | if ( !image.LoadFile( dir + _T("horse.cur"), wxBITMAP_TYPE_CUR ) ) |
bf504f98 VS |
584 | wxLogError(wxT("Can't load best ICO image")); |
585 | else | |
51b07644 | 586 | { |
bf504f98 | 587 | my_horse_cur = new wxBitmap( image ); |
fd94e8aa VS |
588 | xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ; |
589 | yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ; | |
51b07644 | 590 | } |
2b5f62a0 | 591 | |
08811762 | 592 | m_ani_images = wxImage::GetImageCount ( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI ); |
2b5f62a0 VZ |
593 | if (m_ani_images==0) |
594 | wxLogError(wxT("No ANI-format images found")); | |
595 | else | |
596 | my_horse_ani = new wxBitmap [m_ani_images]; | |
597 | int i ; | |
598 | for (i=0; i < m_ani_images; i++) | |
599 | { | |
600 | image.Destroy(); | |
08811762 | 601 | if (!image.LoadFile( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI, i )) |
2b5f62a0 VZ |
602 | { |
603 | wxString tmp = wxT("Can't load image number "); | |
604 | tmp << i ; | |
605 | wxLogError(tmp); | |
606 | } | |
925e9792 | 607 | else |
2b5f62a0 VZ |
608 | my_horse_ani [i] = wxBitmap( image ); |
609 | } | |
da9df1f5 | 610 | #endif // wxUSE_ICO_CUR |
bf504f98 | 611 | |
51b07644 VZ |
612 | image.Destroy(); |
613 | ||
614 | // test image loading from stream | |
615 | wxFile file(dir + _T("horse.bmp")); | |
30ad0a14 | 616 | if ( file.IsOpened() ) |
51b07644 | 617 | { |
9386cb75 WS |
618 | wxFileOffset len = file.Length(); |
619 | size_t dataSize = (size_t)len; | |
620 | void *data = malloc(dataSize); | |
621 | if ( file.Read(data, dataSize) != len ) | |
30ad0a14 | 622 | wxLogError(_T("Reading bitmap file failed")); |
51b07644 | 623 | else |
30ad0a14 | 624 | { |
9386cb75 | 625 | wxMemoryInputStream mis(data, dataSize); |
30ad0a14 VZ |
626 | if ( !image.LoadFile(mis) ) |
627 | wxLogError(wxT("Can't load BMP image from stream")); | |
628 | else | |
629 | my_horse_bmp2 = new wxBitmap( image ); | |
630 | } | |
51b07644 | 631 | |
30ad0a14 VZ |
632 | free(data); |
633 | } | |
01111366 RR |
634 | } |
635 | ||
1d5b7a0b | 636 | MyCanvas::~MyCanvas() |
01111366 | 637 | { |
60a41aee RR |
638 | delete my_horse_pnm; |
639 | delete my_horse_png; | |
640 | delete my_horse_jpeg; | |
641 | delete my_horse_gif; | |
642 | delete my_horse_bmp; | |
51b07644 | 643 | delete my_horse_bmp2; |
60a41aee RR |
644 | delete my_horse_pcx; |
645 | delete my_horse_tiff; | |
9d8bdf2d | 646 | delete my_horse_xpm; |
bf504f98 VS |
647 | delete my_horse_ico32; |
648 | delete my_horse_ico16; | |
649 | delete my_horse_ico; | |
2f6c54eb | 650 | delete my_horse_cur; |
2b5f62a0 | 651 | delete [] my_horse_ani; |
dc1dbfc6 | 652 | delete my_smile_xbm; |
60a41aee RR |
653 | delete my_square; |
654 | delete my_anti; | |
3e8711ce JS |
655 | delete my_horse_asciigrey_pnm; |
656 | delete my_horse_rawgrey_pnm; | |
978d3d36 | 657 | delete colorized_horse_jpeg; |
01111366 RR |
658 | } |
659 | ||
660 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
661 | { | |
be25e480 RR |
662 | wxPaintDC dc( this ); |
663 | PrepareDC( dc ); | |
a91b47e8 | 664 | |
51b07644 | 665 | dc.DrawText( _T("Loaded image"), 30, 10 ); |
be25e480 | 666 | if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 ); |
f048e32f | 667 | |
51b07644 | 668 | dc.DrawText( _T("Drawn directly"), 150, 10 ); |
a60b1f5d | 669 | dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) ); |
cf3da716 | 670 | dc.SetPen( *wxBLACK_PEN ); |
be25e480 | 671 | dc.DrawRectangle( 150, 30, 100, 100 ); |
cf3da716 RR |
672 | dc.SetBrush( *wxWHITE_BRUSH ); |
673 | dc.DrawRectangle( 170, 50, 60, 60 ); | |
be25e480 | 674 | |
408b4168 | 675 | if (my_anti && my_anti->Ok()) |
be25e480 RR |
676 | dc.DrawBitmap( *my_anti, 280, 30 ); |
677 | ||
51b07644 | 678 | dc.DrawText( _T("PNG handler"), 30, 135 ); |
be25e480 | 679 | if (my_horse_png && my_horse_png->Ok()) |
408b4168 | 680 | { |
be25e480 RR |
681 | dc.DrawBitmap( *my_horse_png, 30, 150 ); |
682 | wxRect rect(0,0,100,100); | |
683 | wxBitmap sub( my_horse_png->GetSubBitmap(rect) ); | |
51b07644 | 684 | dc.DrawText( _T("GetSubBitmap()"), 280, 190 ); |
be25e480 RR |
685 | dc.DrawBitmap( sub, 280, 210 ); |
686 | } | |
687 | ||
51b07644 | 688 | dc.DrawText( _T("JPEG handler"), 30, 365 ); |
408b4168 | 689 | if (my_horse_jpeg && my_horse_jpeg->Ok()) |
be25e480 RR |
690 | dc.DrawBitmap( *my_horse_jpeg, 30, 380 ); |
691 | ||
51b07644 | 692 | dc.DrawText( _T("GIF handler"), 30, 595 ); |
408b4168 | 693 | if (my_horse_gif && my_horse_gif->Ok()) |
be25e480 RR |
694 | dc.DrawBitmap( *my_horse_gif, 30, 610 ); |
695 | ||
51b07644 | 696 | dc.DrawText( _T("PCX handler"), 30, 825 ); |
408b4168 | 697 | if (my_horse_pcx && my_horse_pcx->Ok()) |
be25e480 RR |
698 | dc.DrawBitmap( *my_horse_pcx, 30, 840 ); |
699 | ||
51b07644 | 700 | dc.DrawText( _T("BMP handler"), 30, 1055 ); |
408b4168 | 701 | if (my_horse_bmp && my_horse_bmp->Ok()) |
be25e480 RR |
702 | dc.DrawBitmap( *my_horse_bmp, 30, 1070 ); |
703 | ||
51b07644 VZ |
704 | dc.DrawText( _T("BMP read from memory"), 280, 1055 ); |
705 | if (my_horse_bmp2 && my_horse_bmp2->Ok()) | |
706 | dc.DrawBitmap( *my_horse_bmp2, 280, 1070 ); | |
707 | ||
708 | dc.DrawText( _T("PNM handler"), 30, 1285 ); | |
408b4168 | 709 | if (my_horse_pnm && my_horse_pnm->Ok()) |
be25e480 | 710 | dc.DrawBitmap( *my_horse_pnm, 30, 1300 ); |
d0ee33f5 | 711 | |
3e8711ce JS |
712 | dc.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 ); |
713 | if (my_horse_asciigrey_pnm && my_horse_asciigrey_pnm->Ok()) | |
714 | dc.DrawBitmap( *my_horse_asciigrey_pnm, 280, 1300 ); | |
715 | ||
716 | dc.DrawText( _T("PNM handler (raw grey)"), 530, 1285 ); | |
717 | if (my_horse_rawgrey_pnm && my_horse_rawgrey_pnm->Ok()) | |
718 | dc.DrawBitmap( *my_horse_rawgrey_pnm, 530, 1300 ); | |
408b4168 | 719 | |
51b07644 | 720 | dc.DrawText( _T("TIFF handler"), 30, 1515 ); |
408b4168 | 721 | if (my_horse_tiff && my_horse_tiff->Ok()) |
4b05d973 | 722 | dc.DrawBitmap( *my_horse_tiff, 30, 1530 ); |
be25e480 | 723 | |
51b07644 | 724 | dc.DrawText( _T("XPM handler"), 30, 1745 ); |
9d8bdf2d VS |
725 | if (my_horse_xpm && my_horse_xpm->Ok()) |
726 | dc.DrawBitmap( *my_horse_xpm, 30, 1760 ); | |
727 | ||
bf504f98 | 728 | |
408b4168 | 729 | if (my_smile_xbm && my_smile_xbm->Ok()) |
be25e480 | 730 | { |
51b07644 VZ |
731 | dc.DrawText( _T("XBM bitmap"), 30, 1975 ); |
732 | dc.DrawText( _T("(green on red)"), 30, 1990 ); | |
733 | dc.SetTextForeground( _T("GREEN") ); | |
734 | dc.SetTextBackground( _T("RED") ); | |
9d8bdf2d | 735 | dc.DrawBitmap( *my_smile_xbm, 30, 2010 ); |
408b4168 | 736 | |
a60b1f5d | 737 | dc.SetTextForeground( wxT("BLACK") ); |
51b07644 VZ |
738 | dc.DrawText( _T("After wxImage conversion"), 150, 1975 ); |
739 | dc.DrawText( _T("(red on white)"), 150, 1990 ); | |
a60b1f5d | 740 | dc.SetTextForeground( wxT("RED") ); |
a8d4e3ac | 741 | wxImage i = my_smile_xbm->ConvertToImage(); |
176fe5af GRG |
742 | i.SetMaskColour( 255, 255, 255 ); |
743 | i.Replace( 0, 0, 0, | |
be25e480 RR |
744 | wxRED_PEN->GetColour().Red(), |
745 | wxRED_PEN->GetColour().Green(), | |
746 | wxRED_PEN->GetColour().Blue() ); | |
47f797bd | 747 | dc.DrawBitmap( wxBitmap(i), 150, 2010, true ); |
a60b1f5d | 748 | dc.SetTextForeground( wxT("BLACK") ); |
be25e480 | 749 | } |
176fe5af | 750 | |
408b4168 | 751 | |
41fbc841 | 752 | wxBitmap mono( 60,50,1 ); |
82ea63e6 RR |
753 | wxMemoryDC memdc; |
754 | memdc.SelectObject( mono ); | |
bfef20bf | 755 | memdc.SetPen( *wxBLACK_PEN ); |
82ea63e6 | 756 | memdc.SetBrush( *wxWHITE_BRUSH ); |
41fbc841 RR |
757 | memdc.DrawRectangle( 0,0,60,50 ); |
758 | memdc.SetTextForeground( *wxBLACK ); | |
9fe7d430 RR |
759 | #ifndef __WXGTK20__ |
760 | // I cannot convince GTK2 to draw into mono bitmaps | |
51b07644 | 761 | memdc.DrawText( _T("Hi!"), 5, 5 ); |
9fe7d430 | 762 | #endif |
41fbc841 RR |
763 | memdc.SetBrush( *wxBLACK_BRUSH ); |
764 | memdc.DrawRectangle( 33,5,20,20 ); | |
765 | memdc.SetPen( *wxRED_PEN ); | |
766 | memdc.DrawLine( 5, 42, 50, 42 ); | |
82ea63e6 | 767 | memdc.SelectObject( wxNullBitmap ); |
176fe5af | 768 | |
408b4168 | 769 | if (mono.Ok()) |
82ea63e6 | 770 | { |
51b07644 VZ |
771 | dc.DrawText( _T("Mono bitmap"), 30, 2095 ); |
772 | dc.DrawText( _T("(red on green)"), 30, 2110 ); | |
a60b1f5d MB |
773 | dc.SetTextForeground( wxT("RED") ); |
774 | dc.SetTextBackground( wxT("GREEN") ); | |
9d8bdf2d | 775 | dc.DrawBitmap( mono, 30, 2130 ); |
408b4168 | 776 | |
a60b1f5d | 777 | dc.SetTextForeground( wxT("BLACK") ); |
51b07644 VZ |
778 | dc.DrawText( _T("After wxImage conversion"), 150, 2095 ); |
779 | dc.DrawText( _T("(red on white)"), 150, 2110 ); | |
a60b1f5d | 780 | dc.SetTextForeground( wxT("RED") ); |
a8d4e3ac | 781 | wxImage i = mono.ConvertToImage(); |
408b4168 VZ |
782 | i.SetMaskColour( 255,255,255 ); |
783 | i.Replace( 0,0,0, | |
82ea63e6 RR |
784 | wxRED_PEN->GetColour().Red(), |
785 | wxRED_PEN->GetColour().Green(), | |
786 | wxRED_PEN->GetColour().Blue() ); | |
47f797bd | 787 | dc.DrawBitmap( wxBitmap(i), 150, 2130, true ); |
a60b1f5d | 788 | dc.SetTextForeground( wxT("BLACK") ); |
bea56879 VZ |
789 | } |
790 | ||
f9ebac93 RR |
791 | // For testing transparency |
792 | dc.SetBrush( *wxRED_BRUSH ); | |
793 | dc.DrawRectangle( 20, 2220, 560, 68 ); | |
925e9792 | 794 | |
f9ebac93 | 795 | dc.DrawText(_T("XPM bitmap"), 30, 2230 ); |
bea56879 | 796 | if ( m_bmpSmileXpm.Ok() ) |
47f797bd | 797 | dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, true); |
bea56879 | 798 | |
f9ebac93 | 799 | dc.DrawText(_T("XPM icon"), 110, 2230 ); |
bea56879 | 800 | if ( m_iconSmileXpm.Ok() ) |
f9ebac93 | 801 | dc.DrawIcon(m_iconSmileXpm, 110, 2250); |
925e9792 WS |
802 | |
803 | // testing icon -> bitmap conversion | |
f9ebac93 | 804 | wxBitmap to_blit( m_iconSmileXpm ); |
3ef37e7f JS |
805 | if (to_blit.Ok()) |
806 | { | |
807 | dc.DrawText( _T("SubBitmap"), 170, 2230 ); | |
808 | wxBitmap sub = to_blit.GetSubBitmap( wxRect(0,0,15,15) ); | |
809 | if (sub.Ok()) | |
47f797bd | 810 | dc.DrawBitmap( sub, 170, 2250, true ); |
3ef37e7f JS |
811 | |
812 | dc.DrawText( _T("Enlarged"), 250, 2230 ); | |
813 | dc.SetUserScale( 1.5, 1.5 ); | |
47f797bd | 814 | dc.DrawBitmap( to_blit, (int)(250/1.5), (int)(2250/1.5), true ); |
3ef37e7f | 815 | dc.SetUserScale( 2, 2 ); |
47f797bd | 816 | dc.DrawBitmap( to_blit, (int)(300/2), (int)(2250/2), true ); |
3ef37e7f | 817 | dc.SetUserScale( 1.0, 1.0 ); |
925e9792 | 818 | |
3ef37e7f JS |
819 | dc.DrawText( _T("Blit"), 400, 2230); |
820 | wxMemoryDC blit_dc; | |
821 | blit_dc.SelectObject( to_blit ); | |
47f797bd | 822 | dc.Blit( 400, 2250, to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true ); |
3ef37e7f | 823 | dc.SetUserScale( 1.5, 1.5 ); |
47f797bd | 824 | dc.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true ); |
3ef37e7f | 825 | dc.SetUserScale( 2, 2 ); |
47f797bd | 826 | dc.Blit( (int)(500/2), (int)(2250/2), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true ); |
3ef37e7f JS |
827 | dc.SetUserScale( 1.0, 1.0 ); |
828 | } | |
bf504f98 | 829 | |
51b07644 | 830 | dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 ); |
bf504f98 | 831 | if (my_horse_ico32 && my_horse_ico32->Ok()) |
47f797bd | 832 | dc.DrawBitmap( *my_horse_ico32, 30, 2330, true ); |
bf504f98 | 833 | |
51b07644 | 834 | dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 ); |
bf504f98 | 835 | if (my_horse_ico16 && my_horse_ico16->Ok()) |
47f797bd | 836 | dc.DrawBitmap( *my_horse_ico16, 230, 2330, true ); |
51b07644 VZ |
837 | |
838 | dc.DrawText( _T("ICO handler (best image)"), 430, 2290 ); | |
bf504f98 | 839 | if (my_horse_ico && my_horse_ico->Ok()) |
47f797bd | 840 | dc.DrawBitmap( *my_horse_ico, 430, 2330, true ); |
bf504f98 | 841 | |
51b07644 | 842 | dc.DrawText( _T("CUR handler"), 30, 2390 ); |
bf504f98 | 843 | if (my_horse_cur && my_horse_cur->Ok()) |
51b07644 | 844 | { |
47f797bd | 845 | dc.DrawBitmap( *my_horse_cur, 30, 2420, true ); |
bf504f98 VS |
846 | dc.SetPen (*wxRED_PEN); |
847 | dc.DrawLine (xH-10,yH,xH+10,yH); | |
848 | dc.DrawLine (xH,yH-10,xH,yH+10); | |
51b07644 | 849 | } |
2b5f62a0 VZ |
850 | dc.DrawText( _T("ANI handler"), 230, 2390 ); |
851 | int i ; | |
852 | for (i=0; i < m_ani_images; i ++) | |
853 | if (my_horse_ani[i].Ok()) | |
854 | { | |
47f797bd | 855 | dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, true ); |
2b5f62a0 | 856 | } |
978d3d36 VZ |
857 | #if wxUSE_LIBJPEG |
858 | if (colorized_horse_jpeg) | |
859 | { | |
860 | dc.DrawText( _T("Colorize image by rotating green hue to red"), 30, 2490 ); | |
861 | dc.DrawBitmap( *colorized_horse_jpeg, 30, 2520 ); | |
862 | } | |
863 | #endif // wxUSE_LIBJPEG | |
864 | ||
9390a202 | 865 | } |
1d5b7a0b | 866 | |
9390a202 RR |
867 | void MyCanvas::CreateAntiAliasedBitmap() |
868 | { | |
869 | wxBitmap bitmap( 300, 300 ); | |
a91b47e8 | 870 | |
9390a202 | 871 | wxMemoryDC dc; |
a91b47e8 | 872 | |
9390a202 | 873 | dc.SelectObject( bitmap ); |
a91b47e8 | 874 | |
9390a202 | 875 | dc.Clear(); |
f048e32f | 876 | |
6e47faf1 | 877 | dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) ); |
a60b1f5d | 878 | dc.SetTextForeground( wxT("RED") ); |
51b07644 VZ |
879 | dc.DrawText( _T("This is anti-aliased Text."), 20, 20 ); |
880 | dc.DrawText( _T("And a Rectangle."), 20, 60 ); | |
f048e32f | 881 | |
9390a202 | 882 | dc.SetBrush( *wxRED_BRUSH ); |
91b8de8d | 883 | dc.SetPen( *wxTRANSPARENT_PEN ); |
9390a202 | 884 | dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 ); |
f048e32f | 885 | |
a8d4e3ac | 886 | wxImage original= bitmap.ConvertToImage(); |
9390a202 | 887 | wxImage anti( 150, 150 ); |
a91b47e8 | 888 | |
9390a202 | 889 | /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */ |
f048e32f | 890 | |
9390a202 RR |
891 | for (int y = 1; y < 149; y++) |
892 | for (int x = 1; x < 149; x++) | |
893 | { | |
894 | int red = original.GetRed( x*2, y*2 ) + | |
f048e32f VZ |
895 | original.GetRed( x*2-1, y*2 ) + |
896 | original.GetRed( x*2, y*2+1 ) + | |
9390a202 | 897 | original.GetRed( x*2+1, y*2+1 ); |
f048e32f VZ |
898 | red = red/4; |
899 | ||
9390a202 | 900 | int green = original.GetGreen( x*2, y*2 ) + |
f048e32f VZ |
901 | original.GetGreen( x*2-1, y*2 ) + |
902 | original.GetGreen( x*2, y*2+1 ) + | |
9390a202 | 903 | original.GetGreen( x*2+1, y*2+1 ); |
f048e32f VZ |
904 | green = green/4; |
905 | ||
9390a202 | 906 | int blue = original.GetBlue( x*2, y*2 ) + |
f048e32f VZ |
907 | original.GetBlue( x*2-1, y*2 ) + |
908 | original.GetBlue( x*2, y*2+1 ) + | |
9390a202 | 909 | original.GetBlue( x*2+1, y*2+1 ); |
f048e32f | 910 | blue = blue/4; |
925e9792 | 911 | anti.SetRGB( x, y, (unsigned char)red, (unsigned char)green, (unsigned char)blue ); |
9390a202 | 912 | } |
368d59f0 | 913 | my_anti = new wxBitmap(anti); |
01111366 RR |
914 | } |
915 | ||
916 | // MyFrame | |
917 | ||
b3f04dc2 VZ |
918 | enum |
919 | { | |
91b07357 JS |
920 | ID_QUIT = wxID_EXIT, |
921 | ID_ABOUT = wxID_ABOUT, | |
922 | ID_NEW = 100, | |
923 | ID_SHOWRAW = 101 | |
b3f04dc2 | 924 | }; |
01111366 RR |
925 | |
926 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
927 | ||
928 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
929 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) | |
930 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
ab0f0386 | 931 | EVT_MENU (ID_NEW, MyFrame::OnNewFrame) |
b3f04dc2 VZ |
932 | #ifdef wxHAVE_RAW_BITMAP |
933 | EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap) | |
934 | #endif | |
da9df1f5 | 935 | |
dd38c875 | 936 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
937 | EVT_MENU(wxID_COPY, MyFrame::OnCopy) |
938 | EVT_MENU(wxID_PASTE, MyFrame::OnPaste) | |
939 | #endif // wxUSE_CLIPBOARD | |
01111366 RR |
940 | END_EVENT_TABLE() |
941 | ||
1d5b7a0b | 942 | MyFrame::MyFrame() |
47f797bd | 943 | : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"), |
1d5b7a0b | 944 | wxPoint(20,20), wxSize(470,360) ) |
01111366 | 945 | { |
da9df1f5 VZ |
946 | wxMenuBar *menu_bar = new wxMenuBar(); |
947 | ||
948 | wxMenu *menuImage = new wxMenu; | |
949 | menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O")); | |
1e3c12d7 | 950 | |
b3f04dc2 | 951 | #ifdef wxHAVE_RAW_BITMAP |
da9df1f5 | 952 | menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R")); |
b3f04dc2 | 953 | #endif |
da9df1f5 VZ |
954 | menuImage->AppendSeparator(); |
955 | menuImage->Append( ID_ABOUT, _T("&About...")); | |
956 | menuImage->AppendSeparator(); | |
957 | menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q")); | |
958 | menu_bar->Append(menuImage, _T("&Image")); | |
959 | ||
dd38c875 | 960 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
961 | wxMenu *menuClipboard = new wxMenu; |
962 | menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C")); | |
963 | menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V")); | |
964 | menu_bar->Append(menuClipboard, _T("&Clipboard")); | |
965 | #endif // wxUSE_CLIPBOARD | |
3d05544e | 966 | |
01111366 | 967 | SetMenuBar( menu_bar ); |
1d5b7a0b | 968 | |
8520f137 | 969 | #if wxUSE_STATUSBAR |
917e533b RR |
970 | CreateStatusBar(2); |
971 | int widths[] = { -1, 100 }; | |
972 | SetStatusWidths( 2, widths ); | |
8520f137 | 973 | #endif // wxUSE_STATUSBAR |
1d5b7a0b | 974 | |
47f797bd | 975 | m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) ); |
cbd4be25 | 976 | |
978d3d36 VZ |
977 | // 500 width * 2750 height |
978 | m_canvas->SetScrollbars( 10, 10, 50, 275 ); | |
01111366 RR |
979 | } |
980 | ||
981 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
982 | { | |
47f797bd | 983 | Close( true ); |
01111366 RR |
984 | } |
985 | ||
986 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
987 | { | |
51b07644 VZ |
988 | (void)wxMessageBox( _T("wxImage demo\n") |
989 | _T("Robert Roebling (c) 1998,2000"), | |
990 | _T("About wxImage Demo"), wxICON_INFORMATION | wxOK ); | |
fb1585ae RR |
991 | } |
992 | ||
ab0f0386 VZ |
993 | void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) ) |
994 | { | |
f6bcfd97 BP |
995 | wxString filename = wxFileSelector(_T("Select image file")); |
996 | if ( !filename ) | |
997 | return; | |
998 | ||
999 | wxImage image; | |
1000 | if ( !image.LoadFile(filename) ) | |
1001 | { | |
1002 | wxLogError(_T("Couldn't load image from '%s'."), filename.c_str()); | |
1003 | ||
1004 | return; | |
1005 | } | |
1006 | ||
368d59f0 | 1007 | (new MyImageFrame(this, wxBitmap(image)))->Show(); |
ab0f0386 VZ |
1008 | } |
1009 | ||
b3f04dc2 VZ |
1010 | #ifdef wxHAVE_RAW_BITMAP |
1011 | ||
481721e8 | 1012 | void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) ) |
b3f04dc2 VZ |
1013 | { |
1014 | (new MyRawBitmapFrame(this))->Show(); | |
1015 | } | |
1016 | ||
1017 | #endif // wxHAVE_RAW_BITMAP | |
1018 | ||
dd38c875 | 1019 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
1020 | |
1021 | void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1022 | { | |
1023 | wxBitmapDataObject *dobjBmp = new wxBitmapDataObject; | |
1024 | dobjBmp->SetBitmap(*m_canvas->my_horse_png); | |
1025 | ||
dd38c875 MB |
1026 | wxTheClipboard->Open(); |
1027 | ||
da9df1f5 VZ |
1028 | if ( !wxTheClipboard->SetData(dobjBmp) ) |
1029 | { | |
1030 | wxLogError(_T("Failed to copy bitmap to clipboard")); | |
1031 | } | |
dd38c875 MB |
1032 | |
1033 | wxTheClipboard->Close(); | |
da9df1f5 VZ |
1034 | } |
1035 | ||
1036 | void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
1037 | { | |
1038 | wxBitmapDataObject dobjBmp; | |
dd38c875 MB |
1039 | |
1040 | wxTheClipboard->Open(); | |
da9df1f5 VZ |
1041 | if ( !wxTheClipboard->GetData(dobjBmp) ) |
1042 | { | |
1043 | wxLogMessage(_T("No bitmap data in the clipboard")); | |
1044 | } | |
1045 | else | |
1046 | { | |
1047 | (new MyImageFrame(this, dobjBmp.GetBitmap()))->Show(); | |
1048 | } | |
dd38c875 | 1049 | wxTheClipboard->Close(); |
da9df1f5 VZ |
1050 | } |
1051 | ||
1052 | #endif // wxUSE_CLIPBOARD | |
1053 | ||
01111366 RR |
1054 | //----------------------------------------------------------------------------- |
1055 | // MyApp | |
1056 | //----------------------------------------------------------------------------- | |
1057 | ||
1d5b7a0b | 1058 | bool MyApp::OnInit() |
01111366 | 1059 | { |
e9c4b1a2 JS |
1060 | #if wxUSE_LIBPNG |
1061 | wxImage::AddHandler( new wxPNGHandler ); | |
1062 | #endif | |
1063 | ||
1064 | #if wxUSE_LIBJPEG | |
1065 | wxImage::AddHandler( new wxJPEGHandler ); | |
1066 | #endif | |
1067 | ||
60a41aee RR |
1068 | #if wxUSE_LIBTIFF |
1069 | wxImage::AddHandler( new wxTIFFHandler ); | |
1070 | #endif | |
1071 | ||
6e47faf1 | 1072 | #if wxUSE_GIF |
e1929140 | 1073 | wxImage::AddHandler( new wxGIFHandler ); |
6e47faf1 JS |
1074 | #endif |
1075 | ||
1076 | #if wxUSE_PCX | |
cbd4be25 | 1077 | wxImage::AddHandler( new wxPCXHandler ); |
6e47faf1 JS |
1078 | #endif |
1079 | ||
1080 | #if wxUSE_PNM | |
6319afe3 | 1081 | wxImage::AddHandler( new wxPNMHandler ); |
6e47faf1 | 1082 | #endif |
e1929140 | 1083 | |
a8d4e3ac VS |
1084 | #if wxUSE_XPM |
1085 | wxImage::AddHandler( new wxXPMHandler ); | |
1086 | #endif | |
1087 | ||
bf504f98 VS |
1088 | #if wxUSE_ICO_CUR |
1089 | wxImage::AddHandler( new wxICOHandler ); | |
1090 | wxImage::AddHandler( new wxCURHandler ); | |
2b5f62a0 | 1091 | wxImage::AddHandler( new wxANIHandler ); |
bf504f98 VS |
1092 | #endif |
1093 | ||
01111366 | 1094 | wxFrame *frame = new MyFrame(); |
47f797bd | 1095 | frame->Show( true ); |
1d5b7a0b | 1096 | |
47f797bd | 1097 | return true; |
01111366 RR |
1098 | } |
1099 |