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