]>
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"
34 class MyCanvas
: public wxScrolledWindow
38 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
40 void OnPaint( wxPaintEvent
&event
);
41 void CreateAntiAliasedBitmap();
43 wxBitmap
*my_horse_png
;
44 wxBitmap
*my_horse_jpeg
;
45 wxBitmap
*my_horse_gif
;
46 wxBitmap
*my_horse_bmp
;
47 wxBitmap
*my_horse_pcx
;
48 wxBitmap
*my_horse_pnm
;
49 wxBitmap
*my_horse_tiff
;
50 wxBitmap
*my_smile_xbm
;
55 wxBitmap m_bmpSmileXpm
;
56 wxIcon m_iconSmileXpm
;
59 DECLARE_DYNAMIC_CLASS(MyCanvas
)
65 class MyFrame
: public wxFrame
70 void OnAbout( wxCommandEvent
&event
);
71 void OnNewFrame( wxCommandEvent
&event
);
72 void OnQuit( wxCommandEvent
&event
);
77 DECLARE_DYNAMIC_CLASS(MyFrame
)
81 class MyImageFrame
: public wxFrame
84 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
85 : wxFrame(parent
, -1, _T("Frame with image"),
86 wxDefaultPosition
, wxDefaultSize
,
87 wxCAPTION
| wxSYSTEM_MENU
),
90 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
93 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
96 dc
.DrawBitmap( m_bitmap
, 0, 0 );
102 DECLARE_EVENT_TABLE()
107 class MyApp
: public wxApp
110 virtual bool OnInit();
119 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
121 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
122 EVT_PAINT(MyImageFrame::OnPaint
)
125 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
126 EVT_PAINT(MyCanvas::OnPaint
)
129 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
130 const wxPoint
&pos
, const wxSize
&size
)
131 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
132 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
133 , m_bmpSmileXpm((const char **) smile_xpm
)
134 , m_iconSmileXpm((const char **) smile_xpm
)
137 my_horse_png
= (wxBitmap
*) NULL
;
138 my_horse_jpeg
= (wxBitmap
*) NULL
;
139 my_horse_gif
= (wxBitmap
*) NULL
;
140 my_horse_bmp
= (wxBitmap
*) NULL
;
141 my_horse_pcx
= (wxBitmap
*) NULL
;
142 my_horse_pnm
= (wxBitmap
*) NULL
;
143 my_horse_tiff
= (wxBitmap
*) NULL
;
144 my_smile_xbm
= (wxBitmap
*) NULL
;
145 my_square
= (wxBitmap
*) NULL
;
146 my_anti
= (wxBitmap
*) NULL
;
148 SetBackgroundColour(* wxWHITE
);
150 wxBitmap
bitmap( 100, 100 );
153 dc
.SelectObject( bitmap
);
154 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
155 dc
.SetPen( *wxBLACK_PEN
);
156 dc
.DrawRectangle( 0, 0, 100, 100 );
157 dc
.SetBrush( *wxWHITE_BRUSH
);
158 dc
.DrawRectangle( 20, 20, 60, 60 );
159 dc
.SelectObject( wxNullBitmap
);
161 // try to find the directory with our images
163 if ( wxFile::Exists("./horse.png") )
165 else if ( wxFile::Exists("../horse.png") )
168 wxLogWarning("Can't find image files in either '.' or '..'!");
170 wxImage
image( bitmap
);
173 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
))
174 wxLogError("Can't save file");
178 image
.LoadFile( dir
+ wxString("test.png") );
179 my_square
= new wxBitmap( image
.ConvertToBitmap() );
183 if ( !image
.LoadFile( dir
+ wxString("horse.png")) )
184 wxLogError("Can't load PNG image");
186 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
187 #endif // wxUSE_LIBPNG
192 if ( !image
.LoadFile( dir
+ wxString("horse.jpg")) )
193 wxLogError("Can't load JPG image");
195 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
196 #endif // wxUSE_LIBJPEG
201 if ( !image
.LoadFile( dir
+ wxString("horse.gif")) )
202 wxLogError("Can't load GIF image");
204 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
210 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
211 wxLogError("Can't load PCX image");
213 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
218 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
219 wxLogError("Can't load BMP image");
221 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
226 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
227 wxLogError("Can't load PNM image");
229 my_horse_pnm
= new wxBitmap( image
.ConvertToBitmap() );
235 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
236 wxLogError("Can't load TIFF image");
238 my_horse_tiff
= new wxBitmap( image
.ConvertToBitmap() );
241 CreateAntiAliasedBitmap();
243 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
247 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
248 // demonstrates XPM automatically using the mask when saving
249 if ( m_bmpSmileXpm
.Ok() )
250 m_bmpSmileXpm
.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM
);
255 MyCanvas::~MyCanvas()
259 delete my_horse_jpeg
;
263 delete my_horse_tiff
;
269 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
271 wxPaintDC
dc( this );
274 dc
.DrawText( "Loaded image", 30, 10 );
275 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
277 dc
.DrawText( "Drawn directly", 150, 10 );
278 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
279 dc
.SetPen( *wxBLACK_PEN
);
280 dc
.DrawRectangle( 150, 30, 100, 100 );
281 dc
.SetBrush( *wxWHITE_BRUSH
);
282 dc
.DrawRectangle( 170, 50, 60, 60 );
284 if (my_anti
&& my_anti
->Ok())
285 dc
.DrawBitmap( *my_anti
, 280, 30 );
287 dc
.DrawText( "PNG handler", 30, 135 );
288 if (my_horse_png
&& my_horse_png
->Ok())
290 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
291 wxRect
rect(0,0,100,100);
292 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
293 dc
.DrawText( "GetSubBitmap()", 280, 190 );
294 dc
.DrawBitmap( sub
, 280, 210 );
297 dc
.DrawText( "JPEG handler", 30, 365 );
298 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
299 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
301 dc
.DrawText( "GIF handler", 30, 595 );
302 if (my_horse_gif
&& my_horse_gif
->Ok())
303 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
305 dc
.DrawText( "PCX handler", 30, 825 );
306 if (my_horse_pcx
&& my_horse_pcx
->Ok())
307 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
309 dc
.DrawText( "BMP handler", 30, 1055 );
310 if (my_horse_bmp
&& my_horse_bmp
->Ok())
311 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
313 dc
.DrawText( "PNM handler", 30, 1285 );
314 if (my_horse_pnm
&& my_horse_pnm
->Ok())
315 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
317 dc
.DrawText( "TIFF handler", 30, 1515 );
318 if (my_horse_tiff
&& my_horse_tiff
->Ok())
319 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
321 if (my_smile_xbm
&& my_smile_xbm
->Ok())
323 dc
.DrawText( "XBM bitmap", 30, 1745 );
324 dc
.DrawText( "(green on red)", 30, 1760 );
325 dc
.SetTextForeground( "GREEN" );
326 dc
.SetTextBackground( "RED" );
327 dc
.DrawBitmap( *my_smile_xbm
, 30, 1780 );
329 dc
.SetTextForeground( "BLACK" );
330 dc
.DrawText( "After wxImage conversion", 150, 1745 );
331 dc
.DrawText( "(red on white)", 150, 1760 );
332 dc
.SetTextForeground( "RED" );
333 wxImage
i( *my_smile_xbm
);
334 i
.SetMaskColour( 255, 255, 255 );
336 wxRED_PEN
->GetColour().Red(),
337 wxRED_PEN
->GetColour().Green(),
338 wxRED_PEN
->GetColour().Blue() );
339 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1780, TRUE
);
340 dc
.SetTextForeground( "BLACK" );
344 wxBitmap
mono( 60,50,1 );
346 memdc
.SelectObject( mono
);
347 memdc
.SetPen( *wxBLACK_PEN
);
348 memdc
.SetBrush( *wxWHITE_BRUSH
);
349 memdc
.DrawRectangle( 0,0,60,50 );
350 memdc
.SetTextForeground( *wxBLACK
);
351 memdc
.DrawText( "Hi!", 5, 5 );
352 memdc
.SetBrush( *wxBLACK_BRUSH
);
353 memdc
.DrawRectangle( 33,5,20,20 );
354 memdc
.SetPen( *wxRED_PEN
);
355 memdc
.DrawLine( 5, 42, 50, 42 );
356 memdc
.SelectObject( wxNullBitmap
);
360 dc
.DrawText( "Mono bitmap", 30, 1865 );
361 dc
.DrawText( "(red on green)", 30, 1880 );
362 dc
.SetTextForeground( "RED" );
363 dc
.SetTextBackground( "GREEN" );
364 dc
.DrawBitmap( mono
, 30, 1900 );
366 dc
.SetTextForeground( "BLACK" );
367 dc
.DrawText( "After wxImage conversion", 150, 1865 );
368 dc
.DrawText( "(red on white)", 150, 1880 );
369 dc
.SetTextForeground( "RED" );
371 i
.SetMaskColour( 255,255,255 );
373 wxRED_PEN
->GetColour().Red(),
374 wxRED_PEN
->GetColour().Green(),
375 wxRED_PEN
->GetColour().Blue() );
376 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1900, TRUE
);
377 dc
.SetTextForeground( "BLACK" );
380 dc
.DrawText("XPM bitmap", 30, 2000);
381 if ( m_bmpSmileXpm
.Ok() )
383 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2020, TRUE
);
386 dc
.DrawText("XPM icon", 150, 2000);
387 if ( m_iconSmileXpm
.Ok() )
389 dc
.DrawIcon(m_iconSmileXpm
, 150, 2020);
393 void MyCanvas::CreateAntiAliasedBitmap()
395 wxBitmap
bitmap( 300, 300 );
399 dc
.SelectObject( bitmap
);
403 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
404 dc
.SetTextForeground( "RED" );
405 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
406 dc
.DrawText( "And a Rectangle.", 20, 60 );
408 dc
.SetBrush( *wxRED_BRUSH
);
409 dc
.SetPen( *wxTRANSPARENT_PEN
);
410 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
412 wxImage
original( bitmap
);
413 wxImage
anti( 150, 150 );
415 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
417 for (int y
= 1; y
< 149; y
++)
418 for (int x
= 1; x
< 149; x
++)
420 int red
= original
.GetRed( x
*2, y
*2 ) +
421 original
.GetRed( x
*2-1, y
*2 ) +
422 original
.GetRed( x
*2, y
*2+1 ) +
423 original
.GetRed( x
*2+1, y
*2+1 );
426 int green
= original
.GetGreen( x
*2, y
*2 ) +
427 original
.GetGreen( x
*2-1, y
*2 ) +
428 original
.GetGreen( x
*2, y
*2+1 ) +
429 original
.GetGreen( x
*2+1, y
*2+1 );
432 int blue
= original
.GetBlue( x
*2, y
*2 ) +
433 original
.GetBlue( x
*2-1, y
*2 ) +
434 original
.GetBlue( x
*2, y
*2+1 ) +
435 original
.GetBlue( x
*2+1, y
*2+1 );
437 anti
.SetRGB( x
, y
, red
, green
, blue
);
439 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
444 const int ID_QUIT
= 108;
445 const int ID_ABOUT
= 109;
446 const int ID_NEW
= 110;
448 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
450 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
451 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
452 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
453 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
457 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
458 wxPoint(20,20), wxSize(470,360) )
460 wxMenu
*file_menu
= new wxMenu();
461 file_menu
->Append( ID_NEW
, "&Show image...");
462 file_menu
->AppendSeparator();
463 file_menu
->Append( ID_ABOUT
, "&About...");
464 file_menu
->AppendSeparator();
465 file_menu
->Append( ID_QUIT
, "E&xit");
467 wxMenuBar
*menu_bar
= new wxMenuBar();
468 menu_bar
->Append(file_menu
, "&File");
470 SetMenuBar( menu_bar
);
473 int widths
[] = { -1, 100 };
474 SetStatusWidths( 2, widths
);
476 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
478 // 500 width * 2100 height
479 m_canvas
->SetScrollbars( 10, 10, 50, 220 );
482 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
487 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
489 (void)wxMessageBox( "wxImage demo\n"
490 "Robert Roebling (c) 1998,2000",
491 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
494 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
496 wxString filename
= wxFileSelector(_T("Select image file"));
501 if ( !image
.LoadFile(filename
) )
503 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
508 (new MyImageFrame(this, image
.ConvertToBitmap()))->Show();
511 //-----------------------------------------------------------------------------
513 //-----------------------------------------------------------------------------
518 wxImage::AddHandler( new wxPNGHandler
);
522 wxImage::AddHandler( new wxJPEGHandler
);
526 wxImage::AddHandler( new wxTIFFHandler
);
530 wxImage::AddHandler( new wxGIFHandler
);
534 wxImage::AddHandler( new wxPCXHandler
);
538 wxImage::AddHandler( new wxPNMHandler
);
541 wxFrame
*frame
= new MyFrame();