]>
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_smile_xbm
;
58 wxBitmap m_bmpSmileXpm
;
59 wxIcon m_iconSmileXpm
;
62 DECLARE_DYNAMIC_CLASS(MyCanvas
)
68 class MyFrame
: public wxFrame
73 void OnAbout( wxCommandEvent
&event
);
74 void OnNewFrame( wxCommandEvent
&event
);
75 void OnQuit( wxCommandEvent
&event
);
80 DECLARE_DYNAMIC_CLASS(MyFrame
)
84 class MyImageFrame
: public wxFrame
87 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
88 : wxFrame(parent
, -1, _T("Frame with image"),
89 wxDefaultPosition
, wxDefaultSize
,
90 wxCAPTION
| wxSYSTEM_MENU
),
93 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
96 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
99 dc
.DrawBitmap( m_bitmap
, 0, 0 );
105 DECLARE_EVENT_TABLE()
110 class MyApp
: public wxApp
113 virtual bool OnInit();
122 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
124 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
125 EVT_PAINT(MyImageFrame::OnPaint
)
128 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
129 EVT_PAINT(MyCanvas::OnPaint
)
132 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
133 const wxPoint
&pos
, const wxSize
&size
)
134 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
135 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
136 , m_bmpSmileXpm((const char **) smile_xpm
)
137 , m_iconSmileXpm((const char **) smile_xpm
)
140 my_horse_png
= (wxBitmap
*) NULL
;
141 my_horse_jpeg
= (wxBitmap
*) NULL
;
142 my_horse_gif
= (wxBitmap
*) NULL
;
143 my_horse_bmp
= (wxBitmap
*) NULL
;
144 my_horse_pcx
= (wxBitmap
*) NULL
;
145 my_horse_pnm
= (wxBitmap
*) NULL
;
146 my_horse_tiff
= (wxBitmap
*) NULL
;
147 my_smile_xbm
= (wxBitmap
*) NULL
;
148 my_square
= (wxBitmap
*) NULL
;
149 my_anti
= (wxBitmap
*) NULL
;
151 SetBackgroundColour(* wxWHITE
);
153 wxBitmap
bitmap( 100, 100 );
156 dc
.SelectObject( bitmap
);
157 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
158 dc
.SetPen( *wxBLACK_PEN
);
159 dc
.DrawRectangle( 0, 0, 100, 100 );
160 dc
.SetBrush( *wxWHITE_BRUSH
);
161 dc
.DrawRectangle( 20, 20, 60, 60 );
162 dc
.SelectObject( wxNullBitmap
);
164 // try to find the directory with our images
166 if ( wxFile::Exists("./horse.png") )
168 else if ( wxFile::Exists("../horse.png") )
171 wxLogWarning("Can't find image files in either '.' or '..'!");
173 wxImage image
= bitmap
.ConvertToImage();
176 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
))
177 wxLogError("Can't save file");
181 image
.LoadFile( dir
+ wxString("test.png") );
182 my_square
= new wxBitmap( image
);
186 if ( !image
.LoadFile( dir
+ wxString("horse.png")) )
187 wxLogError("Can't load PNG image");
189 my_horse_png
= new wxBitmap( image
);
190 #endif // wxUSE_LIBPNG
195 if ( !image
.LoadFile( dir
+ wxString("horse.jpg")) )
196 wxLogError("Can't load JPG image");
198 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
199 #endif // wxUSE_LIBJPEG
204 if ( !image
.LoadFile( dir
+ wxString("horse.gif")) )
205 wxLogError("Can't load GIF image");
207 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
213 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
214 wxLogError("Can't load PCX image");
216 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
221 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
222 wxLogError("Can't load BMP image");
224 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
227 if ( !image
.SaveFile( dir
+ wxString("test.xpm"), wxBITMAP_TYPE_XPM
))
228 wxLogError("Can't save file");
234 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
235 wxLogError("Can't load PNM image");
237 my_horse_pnm
= new wxBitmap( image
.ConvertToBitmap() );
243 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
244 wxLogError("Can't load TIFF image");
246 my_horse_tiff
= new wxBitmap( image
.ConvertToBitmap() );
249 CreateAntiAliasedBitmap();
251 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
254 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
255 // demonstrates XPM automatically using the mask when saving
256 if ( m_bmpSmileXpm
.Ok() )
257 m_bmpSmileXpm
.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM
);
261 MyCanvas::~MyCanvas()
265 delete my_horse_jpeg
;
269 delete my_horse_tiff
;
275 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
277 wxPaintDC
dc( this );
280 dc
.DrawText( "Loaded image", 30, 10 );
281 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
283 dc
.DrawText( "Drawn directly", 150, 10 );
284 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
285 dc
.SetPen( *wxBLACK_PEN
);
286 dc
.DrawRectangle( 150, 30, 100, 100 );
287 dc
.SetBrush( *wxWHITE_BRUSH
);
288 dc
.DrawRectangle( 170, 50, 60, 60 );
290 if (my_anti
&& my_anti
->Ok())
291 dc
.DrawBitmap( *my_anti
, 280, 30 );
293 dc
.DrawText( "PNG handler", 30, 135 );
294 if (my_horse_png
&& my_horse_png
->Ok())
296 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
297 wxRect
rect(0,0,100,100);
298 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
299 dc
.DrawText( "GetSubBitmap()", 280, 190 );
300 dc
.DrawBitmap( sub
, 280, 210 );
303 dc
.DrawText( "JPEG handler", 30, 365 );
304 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
305 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
307 dc
.DrawText( "GIF handler", 30, 595 );
308 if (my_horse_gif
&& my_horse_gif
->Ok())
309 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
311 dc
.DrawText( "PCX handler", 30, 825 );
312 if (my_horse_pcx
&& my_horse_pcx
->Ok())
313 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
315 dc
.DrawText( "BMP handler", 30, 1055 );
316 if (my_horse_bmp
&& my_horse_bmp
->Ok())
317 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
319 dc
.DrawText( "PNM handler", 30, 1285 );
320 if (my_horse_pnm
&& my_horse_pnm
->Ok())
321 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
323 dc
.DrawText( "TIFF handler", 30, 1515 );
324 if (my_horse_tiff
&& my_horse_tiff
->Ok())
325 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
327 if (my_smile_xbm
&& my_smile_xbm
->Ok())
329 dc
.DrawText( "XBM bitmap", 30, 1745 );
330 dc
.DrawText( "(green on red)", 30, 1760 );
331 dc
.SetTextForeground( "GREEN" );
332 dc
.SetTextBackground( "RED" );
333 dc
.DrawBitmap( *my_smile_xbm
, 30, 1780 );
335 dc
.SetTextForeground( "BLACK" );
336 dc
.DrawText( "After wxImage conversion", 150, 1745 );
337 dc
.DrawText( "(red on white)", 150, 1760 );
338 dc
.SetTextForeground( "RED" );
339 wxImage i
= my_smile_xbm
->ConvertToImage();
340 i
.SetMaskColour( 255, 255, 255 );
342 wxRED_PEN
->GetColour().Red(),
343 wxRED_PEN
->GetColour().Green(),
344 wxRED_PEN
->GetColour().Blue() );
345 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1780, TRUE
);
346 dc
.SetTextForeground( "BLACK" );
350 wxBitmap
mono( 60,50,1 );
352 memdc
.SelectObject( mono
);
353 memdc
.SetPen( *wxBLACK_PEN
);
354 memdc
.SetBrush( *wxWHITE_BRUSH
);
355 memdc
.DrawRectangle( 0,0,60,50 );
356 memdc
.SetTextForeground( *wxBLACK
);
357 memdc
.DrawText( "Hi!", 5, 5 );
358 memdc
.SetBrush( *wxBLACK_BRUSH
);
359 memdc
.DrawRectangle( 33,5,20,20 );
360 memdc
.SetPen( *wxRED_PEN
);
361 memdc
.DrawLine( 5, 42, 50, 42 );
362 memdc
.SelectObject( wxNullBitmap
);
366 dc
.DrawText( "Mono bitmap", 30, 1865 );
367 dc
.DrawText( "(red on green)", 30, 1880 );
368 dc
.SetTextForeground( "RED" );
369 dc
.SetTextBackground( "GREEN" );
370 dc
.DrawBitmap( mono
, 30, 1900 );
372 dc
.SetTextForeground( "BLACK" );
373 dc
.DrawText( "After wxImage conversion", 150, 1865 );
374 dc
.DrawText( "(red on white)", 150, 1880 );
375 dc
.SetTextForeground( "RED" );
376 wxImage i
= mono
.ConvertToImage();
377 i
.SetMaskColour( 255,255,255 );
379 wxRED_PEN
->GetColour().Red(),
380 wxRED_PEN
->GetColour().Green(),
381 wxRED_PEN
->GetColour().Blue() );
382 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1900, TRUE
);
383 dc
.SetTextForeground( "BLACK" );
386 dc
.DrawText("XPM bitmap", 30, 2000);
387 if ( m_bmpSmileXpm
.Ok() )
389 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2020, TRUE
);
392 dc
.DrawText("XPM icon", 150, 2000);
393 if ( m_iconSmileXpm
.Ok() )
395 dc
.DrawIcon(m_iconSmileXpm
, 150, 2020);
399 void MyCanvas::CreateAntiAliasedBitmap()
401 wxBitmap
bitmap( 300, 300 );
405 dc
.SelectObject( bitmap
);
409 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
410 dc
.SetTextForeground( "RED" );
411 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
412 dc
.DrawText( "And a Rectangle.", 20, 60 );
414 dc
.SetBrush( *wxRED_BRUSH
);
415 dc
.SetPen( *wxTRANSPARENT_PEN
);
416 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
418 wxImage original
= bitmap
.ConvertToImage();
419 wxImage
anti( 150, 150 );
421 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
423 for (int y
= 1; y
< 149; y
++)
424 for (int x
= 1; x
< 149; x
++)
426 int red
= original
.GetRed( x
*2, y
*2 ) +
427 original
.GetRed( x
*2-1, y
*2 ) +
428 original
.GetRed( x
*2, y
*2+1 ) +
429 original
.GetRed( x
*2+1, y
*2+1 );
432 int green
= original
.GetGreen( x
*2, y
*2 ) +
433 original
.GetGreen( x
*2-1, y
*2 ) +
434 original
.GetGreen( x
*2, y
*2+1 ) +
435 original
.GetGreen( x
*2+1, y
*2+1 );
438 int blue
= original
.GetBlue( x
*2, y
*2 ) +
439 original
.GetBlue( x
*2-1, y
*2 ) +
440 original
.GetBlue( x
*2, y
*2+1 ) +
441 original
.GetBlue( x
*2+1, y
*2+1 );
443 anti
.SetRGB( x
, y
, red
, green
, blue
);
445 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
450 const int ID_QUIT
= 108;
451 const int ID_ABOUT
= 109;
452 const int ID_NEW
= 110;
454 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
456 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
457 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
458 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
459 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
463 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
464 wxPoint(20,20), wxSize(470,360) )
466 wxMenu
*file_menu
= new wxMenu();
467 file_menu
->Append( ID_NEW
, "&Show image...");
468 file_menu
->AppendSeparator();
469 file_menu
->Append( ID_ABOUT
, "&About...");
470 file_menu
->AppendSeparator();
471 file_menu
->Append( ID_QUIT
, "E&xit");
473 wxMenuBar
*menu_bar
= new wxMenuBar();
474 menu_bar
->Append(file_menu
, "&File");
476 SetMenuBar( menu_bar
);
479 int widths
[] = { -1, 100 };
480 SetStatusWidths( 2, widths
);
482 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
484 // 500 width * 2100 height
485 m_canvas
->SetScrollbars( 10, 10, 50, 220 );
488 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
493 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
495 (void)wxMessageBox( "wxImage demo\n"
496 "Robert Roebling (c) 1998,2000",
497 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
500 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
502 wxString filename
= wxFileSelector(_T("Select image file"));
507 if ( !image
.LoadFile(filename
) )
509 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
514 (new MyImageFrame(this, image
.ConvertToBitmap()))->Show();
517 //-----------------------------------------------------------------------------
519 //-----------------------------------------------------------------------------
524 wxImage::AddHandler( new wxPNGHandler
);
528 wxImage::AddHandler( new wxJPEGHandler
);
532 wxImage::AddHandler( new wxTIFFHandler
);
536 wxImage::AddHandler( new wxGIFHandler
);
540 wxImage::AddHandler( new wxPCXHandler
);
544 wxImage::AddHandler( new wxPNMHandler
);
548 wxImage::AddHandler( new wxXPMHandler
);
551 wxFrame
*frame
= new MyFrame();