]>
git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
4 * Author: Robert Roebling
6 * Copyright: (C) 1998, Robert Roebling
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
23 #include "wx/mstream.h"
24 #include "wx/wfstream.h"
25 #include "wx/quantize.h"
38 class MyCanvas
: public wxScrolledWindow
42 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
44 void OnPaint( wxPaintEvent
&event
);
45 void CreateAntiAliasedBitmap();
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
;
56 wxBitmap
*my_smile_xbm
;
61 wxBitmap m_bmpSmileXpm
;
62 wxIcon m_iconSmileXpm
;
65 DECLARE_DYNAMIC_CLASS(MyCanvas
)
71 class MyFrame
: public wxFrame
76 void OnAbout( wxCommandEvent
&event
);
77 void OnNewFrame( wxCommandEvent
&event
);
78 void OnQuit( wxCommandEvent
&event
);
83 DECLARE_DYNAMIC_CLASS(MyFrame
)
87 class MyImageFrame
: public wxFrame
90 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
91 : wxFrame(parent
, -1, _T("Double click to save"),
92 wxDefaultPosition
, wxDefaultSize
,
93 wxCAPTION
| wxSYSTEM_MENU
),
96 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
99 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
101 wxPaintDC
dc( this );
102 dc
.DrawBitmap( m_bitmap
, 0, 0 );
105 void OnSave(wxCommandEvent
& WXUNUSED(event
))
107 wxImage
image(m_bitmap
);
109 static const wxString bppchoices
[8] =
121 static const int bppvalues
[WXSIZEOF(bppchoices
)] =
133 int bppselection
= wxGetSingleChoiceIndex("Set BMP BPP",
135 WXSIZEOF(bppchoices
),
138 if ( bppselection
== -1 )
144 image
.SetOption(wxBMP_FORMAT
, bppvalues
[bppselection
]);
146 wxString deffilename
= bppchoices
[bppselection
];
147 deffilename
.Replace(" ", "_");
148 deffilename
+= ".bmp";
149 wxString savefilename
= wxFileSelector( "Save Image",
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",
161 if ( savefilename
.empty() )
164 if ( image
.GetOptionInt(wxBMP_FORMAT
) == wxBMP_8BPP_PALETTE
)
166 unsigned char *cmap
= new unsigned char [256];
167 for ( int i
= 0; i
< 256; i
++ )
169 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
176 wxString extension
= savefilename
.AfterLast('.').Lower();
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
);
191 wxMessageBox("Unknown file type, see options in file selector.",
193 wxOK
|wxCENTRE
, this);
199 DECLARE_EVENT_TABLE()
204 class MyApp
: public wxApp
207 virtual bool OnInit();
216 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
218 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
219 EVT_PAINT(MyImageFrame::OnPaint
)
220 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
223 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
224 EVT_PAINT(MyCanvas::OnPaint
)
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
)
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
;
247 SetBackgroundColour(* wxWHITE
);
249 wxBitmap
bitmap( 100, 100 );
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
);
260 // try to find the directory with our images
262 if ( wxFile::Exists("./horse.png") )
264 else if ( wxFile::Exists("../horse.png") )
267 wxLogWarning("Can't find image files in either '.' or '..'!");
269 wxImage image
= bitmap
.ConvertToImage();
272 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
))
273 wxLogError("Can't save file");
277 image
.LoadFile( dir
+ wxString("test.png") );
278 my_square
= new wxBitmap( image
);
282 if ( !image
.LoadFile( dir
+ wxString("horse.png")) )
283 wxLogError("Can't load PNG image");
285 my_horse_png
= new wxBitmap( image
);
286 #endif // wxUSE_LIBPNG
291 if ( !image
.LoadFile( dir
+ wxString("horse.jpg")) )
292 wxLogError("Can't load JPG image");
294 my_horse_jpeg
= new wxBitmap( image
);
295 #endif // wxUSE_LIBJPEG
300 if ( !image
.LoadFile( dir
+ wxString("horse.gif")) )
301 wxLogError("Can't load GIF image");
303 my_horse_gif
= new wxBitmap( image
);
309 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
310 wxLogError("Can't load PCX image");
312 my_horse_pcx
= new wxBitmap( image
);
317 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
318 wxLogError("Can't load BMP image");
320 my_horse_bmp
= new wxBitmap( image
);
325 if ( !image
.LoadFile( dir
+ wxString("horse.xpm"), wxBITMAP_TYPE_XPM
) )
326 wxLogError("Can't load XPM image");
328 my_horse_xpm
= new wxBitmap( image
);
330 if ( !image
.SaveFile( dir
+ wxString("test.xpm"), wxBITMAP_TYPE_XPM
))
331 wxLogError("Can't save file");
337 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
338 wxLogError("Can't load PNM image");
340 my_horse_pnm
= new wxBitmap( image
);
346 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
347 wxLogError("Can't load TIFF image");
349 my_horse_tiff
= new wxBitmap( image
);
352 CreateAntiAliasedBitmap();
354 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
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
);
364 MyCanvas::~MyCanvas()
368 delete my_horse_jpeg
;
372 delete my_horse_tiff
;
379 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
381 wxPaintDC
dc( this );
384 dc
.DrawText( "Loaded image", 30, 10 );
385 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
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 );
394 if (my_anti
&& my_anti
->Ok())
395 dc
.DrawBitmap( *my_anti
, 280, 30 );
397 dc
.DrawText( "PNG handler", 30, 135 );
398 if (my_horse_png
&& my_horse_png
->Ok())
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 );
407 dc
.DrawText( "JPEG handler", 30, 365 );
408 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
409 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
411 dc
.DrawText( "GIF handler", 30, 595 );
412 if (my_horse_gif
&& my_horse_gif
->Ok())
413 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
415 dc
.DrawText( "PCX handler", 30, 825 );
416 if (my_horse_pcx
&& my_horse_pcx
->Ok())
417 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
419 dc
.DrawText( "BMP handler", 30, 1055 );
420 if (my_horse_bmp
&& my_horse_bmp
->Ok())
421 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
423 dc
.DrawText( "PNM handler", 30, 1285 );
424 if (my_horse_pnm
&& my_horse_pnm
->Ok())
425 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
427 dc
.DrawText( "TIFF handler", 30, 1515 );
428 if (my_horse_tiff
&& my_horse_tiff
->Ok())
429 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
431 dc
.DrawText( "XPM handler", 30, 1745 );
432 if (my_horse_xpm
&& my_horse_xpm
->Ok())
433 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
435 if (my_smile_xbm
&& my_smile_xbm
->Ok())
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 );
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 );
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" );
458 wxBitmap
mono( 60,50,1 );
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
);
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 );
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 );
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" );
494 dc
.DrawText("XPM bitmap", 30, 2230);
495 if ( m_bmpSmileXpm
.Ok() )
497 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, TRUE
);
500 dc
.DrawText("XPM icon", 150, 2230);
501 if ( m_iconSmileXpm
.Ok() )
503 dc
.DrawIcon(m_iconSmileXpm
, 150, 2250);
507 void MyCanvas::CreateAntiAliasedBitmap()
509 wxBitmap
bitmap( 300, 300 );
513 dc
.SelectObject( bitmap
);
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 );
522 dc
.SetBrush( *wxRED_BRUSH
);
523 dc
.SetPen( *wxTRANSPARENT_PEN
);
524 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
526 wxImage original
= bitmap
.ConvertToImage();
527 wxImage
anti( 150, 150 );
529 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
531 for (int y
= 1; y
< 149; y
++)
532 for (int x
= 1; x
< 149; x
++)
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 );
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 );
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 );
551 anti
.SetRGB( x
, y
, red
, green
, blue
);
553 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
558 const int ID_QUIT
= 108;
559 const int ID_ABOUT
= 109;
560 const int ID_NEW
= 110;
562 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
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
)
571 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
572 wxPoint(20,20), wxSize(470,360) )
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");
581 wxMenuBar
*menu_bar
= new wxMenuBar();
582 menu_bar
->Append(file_menu
, "&File");
584 SetMenuBar( menu_bar
);
587 int widths
[] = { -1, 100 };
588 SetStatusWidths( 2, widths
);
590 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
592 // 500 width * 2100 height
593 m_canvas
->SetScrollbars( 10, 10, 50, 220 );
596 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
601 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
603 (void)wxMessageBox( "wxImage demo\n"
604 "Robert Roebling (c) 1998,2000",
605 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
608 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
610 wxString filename
= wxFileSelector(_T("Select image file"));
615 if ( !image
.LoadFile(filename
) )
617 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
622 (new MyImageFrame(this, image
.ConvertToBitmap()))->Show();
625 //-----------------------------------------------------------------------------
627 //-----------------------------------------------------------------------------
632 wxImage::AddHandler( new wxPNGHandler
);
636 wxImage::AddHandler( new wxJPEGHandler
);
640 wxImage::AddHandler( new wxTIFFHandler
);
644 wxImage::AddHandler( new wxGIFHandler
);
648 wxImage::AddHandler( new wxPCXHandler
);
652 wxImage::AddHandler( new wxPNMHandler
);
656 wxImage::AddHandler( new wxXPMHandler
);
659 wxFrame
*frame
= new MyFrame();