]>
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"
37 class MyCanvas
: public wxScrolledWindow
41 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
43 void OnPaint( wxPaintEvent
&event
);
44 void CreateAntiAliasedBitmap();
46 wxBitmap
*my_horse_png
;
47 wxBitmap
*my_horse_jpeg
;
48 wxBitmap
*my_horse_gif
;
49 wxBitmap
*my_horse_bmp
;
50 wxBitmap
*my_horse_pcx
;
51 wxBitmap
*my_horse_pnm
;
52 wxBitmap
*my_horse_tiff
;
53 wxBitmap
*my_horse_xpm
;
55 wxBitmap
*my_smile_xbm
;
60 wxBitmap m_bmpSmileXpm
;
61 wxIcon m_iconSmileXpm
;
64 DECLARE_DYNAMIC_CLASS(MyCanvas
)
70 class MyFrame
: public wxFrame
75 void OnAbout( wxCommandEvent
&event
);
76 void OnNewFrame( wxCommandEvent
&event
);
77 void OnQuit( wxCommandEvent
&event
);
82 DECLARE_DYNAMIC_CLASS(MyFrame
)
86 class MyImageFrame
: public wxFrame
89 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
90 : wxFrame(parent
, -1, _T("Frame with image"),
91 wxDefaultPosition
, wxDefaultSize
,
92 wxCAPTION
| wxSYSTEM_MENU
),
95 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
98 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
100 wxPaintDC
dc( this );
101 dc
.DrawBitmap( m_bitmap
, 0, 0 );
107 DECLARE_EVENT_TABLE()
112 class MyApp
: public wxApp
115 virtual bool OnInit();
124 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
126 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
127 EVT_PAINT(MyImageFrame::OnPaint
)
130 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
131 EVT_PAINT(MyCanvas::OnPaint
)
134 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
135 const wxPoint
&pos
, const wxSize
&size
)
136 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
137 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
138 , m_bmpSmileXpm((const char **) smile_xpm
)
139 , m_iconSmileXpm((const char **) smile_xpm
)
142 my_horse_png
= (wxBitmap
*) NULL
;
143 my_horse_jpeg
= (wxBitmap
*) NULL
;
144 my_horse_gif
= (wxBitmap
*) NULL
;
145 my_horse_bmp
= (wxBitmap
*) NULL
;
146 my_horse_pcx
= (wxBitmap
*) NULL
;
147 my_horse_pnm
= (wxBitmap
*) NULL
;
148 my_horse_tiff
= (wxBitmap
*) NULL
;
149 my_horse_xpm
= (wxBitmap
*) NULL
;
150 my_smile_xbm
= (wxBitmap
*) NULL
;
151 my_square
= (wxBitmap
*) NULL
;
152 my_anti
= (wxBitmap
*) NULL
;
154 SetBackgroundColour(* wxWHITE
);
156 wxBitmap
bitmap( 100, 100 );
159 dc
.SelectObject( bitmap
);
160 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
161 dc
.SetPen( *wxBLACK_PEN
);
162 dc
.DrawRectangle( 0, 0, 100, 100 );
163 dc
.SetBrush( *wxWHITE_BRUSH
);
164 dc
.DrawRectangle( 20, 20, 60, 60 );
165 dc
.SelectObject( wxNullBitmap
);
167 // try to find the directory with our images
169 if ( wxFile::Exists("./horse.png") )
171 else if ( wxFile::Exists("../horse.png") )
174 wxLogWarning("Can't find image files in either '.' or '..'!");
176 wxImage image
= bitmap
.ConvertToImage();
179 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
))
180 wxLogError("Can't save file");
184 image
.LoadFile( dir
+ wxString("test.png") );
185 my_square
= new wxBitmap( image
);
189 if ( !image
.LoadFile( dir
+ wxString("horse.png")) )
190 wxLogError("Can't load PNG image");
192 my_horse_png
= new wxBitmap( image
);
193 #endif // wxUSE_LIBPNG
198 if ( !image
.LoadFile( dir
+ wxString("horse.jpg")) )
199 wxLogError("Can't load JPG image");
201 my_horse_jpeg
= new wxBitmap( image
);
202 #endif // wxUSE_LIBJPEG
207 if ( !image
.LoadFile( dir
+ wxString("horse.gif")) )
208 wxLogError("Can't load GIF image");
210 my_horse_gif
= new wxBitmap( image
);
216 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
217 wxLogError("Can't load PCX image");
219 my_horse_pcx
= new wxBitmap( image
);
224 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
225 wxLogError("Can't load BMP image");
227 my_horse_bmp
= new wxBitmap( image
);
232 if ( !image
.LoadFile( dir
+ wxString("horse.xpm"), wxBITMAP_TYPE_XPM
) )
233 wxLogError("Can't load XPM image");
235 my_horse_xpm
= new wxBitmap( image
);
237 if ( !image
.SaveFile( dir
+ wxString("test.xpm"), wxBITMAP_TYPE_XPM
))
238 wxLogError("Can't save file");
244 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
245 wxLogError("Can't load PNM image");
247 my_horse_pnm
= new wxBitmap( image
);
253 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
254 wxLogError("Can't load TIFF image");
256 my_horse_tiff
= new wxBitmap( image
);
259 CreateAntiAliasedBitmap();
261 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
264 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
265 // demonstrates XPM automatically using the mask when saving
266 if ( m_bmpSmileXpm
.Ok() )
267 m_bmpSmileXpm
.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM
);
271 MyCanvas::~MyCanvas()
275 delete my_horse_jpeg
;
279 delete my_horse_tiff
;
286 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
288 wxPaintDC
dc( this );
291 dc
.DrawText( "Loaded image", 30, 10 );
292 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
294 dc
.DrawText( "Drawn directly", 150, 10 );
295 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
296 dc
.SetPen( *wxBLACK_PEN
);
297 dc
.DrawRectangle( 150, 30, 100, 100 );
298 dc
.SetBrush( *wxWHITE_BRUSH
);
299 dc
.DrawRectangle( 170, 50, 60, 60 );
301 if (my_anti
&& my_anti
->Ok())
302 dc
.DrawBitmap( *my_anti
, 280, 30 );
304 dc
.DrawText( "PNG handler", 30, 135 );
305 if (my_horse_png
&& my_horse_png
->Ok())
307 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
308 wxRect
rect(0,0,100,100);
309 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
310 dc
.DrawText( "GetSubBitmap()", 280, 190 );
311 dc
.DrawBitmap( sub
, 280, 210 );
314 dc
.DrawText( "JPEG handler", 30, 365 );
315 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
316 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
318 dc
.DrawText( "GIF handler", 30, 595 );
319 if (my_horse_gif
&& my_horse_gif
->Ok())
320 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
322 dc
.DrawText( "PCX handler", 30, 825 );
323 if (my_horse_pcx
&& my_horse_pcx
->Ok())
324 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
326 dc
.DrawText( "BMP handler", 30, 1055 );
327 if (my_horse_bmp
&& my_horse_bmp
->Ok())
328 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
330 dc
.DrawText( "PNM handler", 30, 1285 );
331 if (my_horse_pnm
&& my_horse_pnm
->Ok())
332 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
334 dc
.DrawText( "TIFF handler", 30, 1515 );
335 if (my_horse_tiff
&& my_horse_tiff
->Ok())
336 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
338 dc
.DrawText( "XPM handler", 30, 1745 );
339 if (my_horse_xpm
&& my_horse_xpm
->Ok())
340 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
342 if (my_smile_xbm
&& my_smile_xbm
->Ok())
344 dc
.DrawText( "XBM bitmap", 30, 1975 );
345 dc
.DrawText( "(green on red)", 30, 1990 );
346 dc
.SetTextForeground( "GREEN" );
347 dc
.SetTextBackground( "RED" );
348 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
350 dc
.SetTextForeground( "BLACK" );
351 dc
.DrawText( "After wxImage conversion", 150, 1975 );
352 dc
.DrawText( "(red on white)", 150, 1990 );
353 dc
.SetTextForeground( "RED" );
354 wxImage i
= my_smile_xbm
->ConvertToImage();
355 i
.SetMaskColour( 255, 255, 255 );
357 wxRED_PEN
->GetColour().Red(),
358 wxRED_PEN
->GetColour().Green(),
359 wxRED_PEN
->GetColour().Blue() );
360 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 2010, TRUE
);
361 dc
.SetTextForeground( "BLACK" );
365 wxBitmap
mono( 60,50,1 );
367 memdc
.SelectObject( mono
);
368 memdc
.SetPen( *wxBLACK_PEN
);
369 memdc
.SetBrush( *wxWHITE_BRUSH
);
370 memdc
.DrawRectangle( 0,0,60,50 );
371 memdc
.SetTextForeground( *wxBLACK
);
372 memdc
.DrawText( "Hi!", 5, 5 );
373 memdc
.SetBrush( *wxBLACK_BRUSH
);
374 memdc
.DrawRectangle( 33,5,20,20 );
375 memdc
.SetPen( *wxRED_PEN
);
376 memdc
.DrawLine( 5, 42, 50, 42 );
377 memdc
.SelectObject( wxNullBitmap
);
381 dc
.DrawText( "Mono bitmap", 30, 2095 );
382 dc
.DrawText( "(red on green)", 30, 2110 );
383 dc
.SetTextForeground( "RED" );
384 dc
.SetTextBackground( "GREEN" );
385 dc
.DrawBitmap( mono
, 30, 2130 );
387 dc
.SetTextForeground( "BLACK" );
388 dc
.DrawText( "After wxImage conversion", 150, 2095 );
389 dc
.DrawText( "(red on white)", 150, 2110 );
390 dc
.SetTextForeground( "RED" );
391 wxImage i
= mono
.ConvertToImage();
392 i
.SetMaskColour( 255,255,255 );
394 wxRED_PEN
->GetColour().Red(),
395 wxRED_PEN
->GetColour().Green(),
396 wxRED_PEN
->GetColour().Blue() );
397 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 2130, TRUE
);
398 dc
.SetTextForeground( "BLACK" );
401 dc
.DrawText("XPM bitmap", 30, 2230);
402 if ( m_bmpSmileXpm
.Ok() )
404 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, TRUE
);
407 dc
.DrawText("XPM icon", 150, 2230);
408 if ( m_iconSmileXpm
.Ok() )
410 dc
.DrawIcon(m_iconSmileXpm
, 150, 2250);
414 void MyCanvas::CreateAntiAliasedBitmap()
416 wxBitmap
bitmap( 300, 300 );
420 dc
.SelectObject( bitmap
);
424 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
425 dc
.SetTextForeground( "RED" );
426 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
427 dc
.DrawText( "And a Rectangle.", 20, 60 );
429 dc
.SetBrush( *wxRED_BRUSH
);
430 dc
.SetPen( *wxTRANSPARENT_PEN
);
431 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
433 wxImage original
= bitmap
.ConvertToImage();
434 wxImage
anti( 150, 150 );
436 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
438 for (int y
= 1; y
< 149; y
++)
439 for (int x
= 1; x
< 149; x
++)
441 int red
= original
.GetRed( x
*2, y
*2 ) +
442 original
.GetRed( x
*2-1, y
*2 ) +
443 original
.GetRed( x
*2, y
*2+1 ) +
444 original
.GetRed( x
*2+1, y
*2+1 );
447 int green
= original
.GetGreen( x
*2, y
*2 ) +
448 original
.GetGreen( x
*2-1, y
*2 ) +
449 original
.GetGreen( x
*2, y
*2+1 ) +
450 original
.GetGreen( x
*2+1, y
*2+1 );
453 int blue
= original
.GetBlue( x
*2, y
*2 ) +
454 original
.GetBlue( x
*2-1, y
*2 ) +
455 original
.GetBlue( x
*2, y
*2+1 ) +
456 original
.GetBlue( x
*2+1, y
*2+1 );
458 anti
.SetRGB( x
, y
, red
, green
, blue
);
460 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
465 const int ID_QUIT
= 108;
466 const int ID_ABOUT
= 109;
467 const int ID_NEW
= 110;
469 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
471 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
472 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
473 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
474 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
478 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
479 wxPoint(20,20), wxSize(470,360) )
481 wxMenu
*file_menu
= new wxMenu();
482 file_menu
->Append( ID_NEW
, "&Show image...");
483 file_menu
->AppendSeparator();
484 file_menu
->Append( ID_ABOUT
, "&About...");
485 file_menu
->AppendSeparator();
486 file_menu
->Append( ID_QUIT
, "E&xit");
488 wxMenuBar
*menu_bar
= new wxMenuBar();
489 menu_bar
->Append(file_menu
, "&File");
491 SetMenuBar( menu_bar
);
494 int widths
[] = { -1, 100 };
495 SetStatusWidths( 2, widths
);
497 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
499 // 500 width * 2100 height
500 m_canvas
->SetScrollbars( 10, 10, 50, 220 );
503 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
508 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
510 (void)wxMessageBox( "wxImage demo\n"
511 "Robert Roebling (c) 1998,2000",
512 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
515 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
517 wxString filename
= wxFileSelector(_T("Select image file"));
522 if ( !image
.LoadFile(filename
) )
524 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
529 (new MyImageFrame(this, image
.ConvertToBitmap()))->Show();
532 //-----------------------------------------------------------------------------
534 //-----------------------------------------------------------------------------
539 wxImage::AddHandler( new wxPNGHandler
);
543 wxImage::AddHandler( new wxJPEGHandler
);
547 wxImage::AddHandler( new wxTIFFHandler
);
551 wxImage::AddHandler( new wxGIFHandler
);
555 wxImage::AddHandler( new wxPCXHandler
);
559 wxImage::AddHandler( new wxPNMHandler
);
563 wxImage::AddHandler( new wxXPMHandler
);
566 wxFrame
*frame
= new MyFrame();