]>
git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
bcc4ed44085ad2675d1a1737026a6df95437bec7
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"
35 class MyCanvas
: public wxScrolledWindow
39 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
41 void OnPaint( wxPaintEvent
&event
);
42 void CreateAntiAliasedBitmap();
44 wxBitmap
*my_horse_png
;
45 wxBitmap
*my_horse_jpeg
;
46 wxBitmap
*my_horse_gif
;
47 wxBitmap
*my_horse_bmp
;
48 wxBitmap
*my_horse_pcx
;
49 wxBitmap
*my_horse_pnm
;
50 wxBitmap
*my_horse_tiff
;
51 wxBitmap
*my_smile_xbm
;
56 wxBitmap m_bmpSmileXpm
;
57 wxIcon m_iconSmileXpm
;
60 DECLARE_DYNAMIC_CLASS(MyCanvas
)
66 class MyFrame
: public wxFrame
71 void OnAbout( wxCommandEvent
&event
);
72 void OnQuit( wxCommandEvent
&event
);
77 DECLARE_DYNAMIC_CLASS(MyFrame
)
83 class MyApp
: public wxApp
86 virtual bool OnInit();
95 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
97 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
98 EVT_PAINT(MyCanvas::OnPaint
)
101 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
102 const wxPoint
&pos
, const wxSize
&size
)
103 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
104 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
105 , m_bmpSmileXpm((const char **) smile_xpm
)
106 , m_iconSmileXpm((const char **) smile_xpm
)
109 my_horse_png
= (wxBitmap
*) NULL
;
110 my_horse_jpeg
= (wxBitmap
*) NULL
;
111 my_horse_gif
= (wxBitmap
*) NULL
;
112 my_horse_bmp
= (wxBitmap
*) NULL
;
113 my_horse_pcx
= (wxBitmap
*) NULL
;
114 my_horse_pnm
= (wxBitmap
*) NULL
;
115 my_horse_tiff
= (wxBitmap
*) NULL
;
116 my_smile_xbm
= (wxBitmap
*) NULL
;
117 my_square
= (wxBitmap
*) NULL
;
118 my_anti
= (wxBitmap
*) NULL
;
120 SetBackgroundColour(* wxWHITE
);
122 wxBitmap
bitmap( 100, 100 );
125 dc
.SelectObject( bitmap
);
126 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
127 dc
.SetPen( *wxBLACK_PEN
);
128 dc
.DrawRectangle( 0, 0, 100, 100 );
129 dc
.SetBrush( *wxWHITE_BRUSH
);
130 dc
.DrawRectangle( 20, 20, 60, 60 );
131 dc
.SelectObject( wxNullBitmap
);
133 // try to find the directory with our images
135 if ( wxFile::Exists("./horse.png") )
137 else if ( wxFile::Exists("../horse.png") )
140 wxLogWarning("Can't find image files in either '.' or '..'!");
142 wxImage
image( bitmap
);
145 image
.LoadFile( dir
+ wxString("test.png") );
146 my_square
= new wxBitmap( image
.ConvertToBitmap() );
148 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
) )
149 wxLogError("Can't save file");
151 image
= wxImage( 100, 100 );
153 if ( !image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
) )
154 wxLogError("Can't load PNG image");
156 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
157 #endif // wxUSE_LIBPNG
160 image
= wxImage( 100, 100 );
162 if ( !image
.LoadFile( dir
+ wxString("horse.jpg") ) )
163 wxLogError("Can't load JPG image");
165 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
166 #endif // wxUSE_LIBJPEG
169 image
= wxImage( 100, 100 );
171 if ( !image
.LoadFile( dir
+ wxString("horse.gif") ) )
172 wxLogError("Can't load GIF image");
174 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
178 image
= wxImage( 100, 100 );
180 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
181 wxLogError("Can't load PCX image");
183 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
186 image
= wxImage( 100, 100 );
188 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
189 wxLogError("Can't load BMP image");
191 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
194 image
= wxImage( 100, 100 );
196 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
197 wxLogError("Can't load PNM image");
199 my_horse_pnm
= new wxBitmap( image
.ConvertToBitmap() );
203 image
= wxImage( 100, 100 );
205 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
206 wxLogError("Can't load TIFF image");
208 my_horse_tiff
= new wxBitmap( image
.ConvertToBitmap() );
211 CreateAntiAliasedBitmap();
213 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
217 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
218 // demonstrates XPM automatically using the mask when saving
219 if ( m_bmpSmileXpm
.Ok() )
220 m_bmpSmileXpm
.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM
);
225 MyCanvas::~MyCanvas()
229 delete my_horse_jpeg
;
233 delete my_horse_tiff
;
239 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
241 wxPaintDC
dc( this );
244 dc
.DrawText( "Loaded image", 30, 10 );
245 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
247 dc
.DrawText( "Drawn directly", 150, 10 );
248 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
249 dc
.SetPen( *wxBLACK_PEN
);
250 dc
.DrawRectangle( 150, 30, 100, 100 );
251 dc
.SetBrush( *wxWHITE_BRUSH
);
252 dc
.DrawRectangle( 170, 50, 60, 60 );
254 if (my_anti
&& my_anti
->Ok())
255 dc
.DrawBitmap( *my_anti
, 280, 30 );
257 dc
.DrawText( "PNG handler", 30, 135 );
258 if (my_horse_png
&& my_horse_png
->Ok())
260 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
261 wxRect
rect(0,0,100,100);
262 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
263 dc
.DrawText( "GetSubBitmap()", 280, 190 );
264 dc
.DrawBitmap( sub
, 280, 210 );
267 dc
.DrawText( "JPEG handler", 30, 365 );
268 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
269 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
271 dc
.DrawText( "GIF handler", 30, 595 );
272 if (my_horse_gif
&& my_horse_gif
->Ok())
273 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
275 dc
.DrawText( "PCX handler", 30, 825 );
276 if (my_horse_pcx
&& my_horse_pcx
->Ok())
277 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
279 dc
.DrawText( "BMP handler", 30, 1055 );
280 if (my_horse_bmp
&& my_horse_bmp
->Ok())
281 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
283 dc
.DrawText( "PNM handler", 30, 1285 );
284 if (my_horse_pnm
&& my_horse_pnm
->Ok())
285 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
287 dc
.DrawText( "TIFF handler", 30, 1515 );
288 if (my_horse_tiff
&& my_horse_tiff
->Ok())
289 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
291 if (my_smile_xbm
&& my_smile_xbm
->Ok())
293 dc
.DrawText( "XBM bitmap", 30, 1745 );
294 dc
.SetTextForeground( "RED" );
295 dc
.SetTextBackground( "GREEN" );
296 dc
.DrawBitmap( *my_smile_xbm
, 30, 1760 );
298 dc
.DrawText( "After wxImage conversion", 150, 1745 );
299 wxImage
i( *my_smile_xbm
);
300 i
.SetMaskColour( 0,0,0 );
301 i
.Replace( 255,255,255,
302 wxRED_PEN
->GetColour().Red(),
303 wxRED_PEN
->GetColour().Green(),
304 wxRED_PEN
->GetColour().Blue() );
305 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1760, TRUE
);
307 dc
.SetTextForeground( "BLACK" );
309 wxBitmap
mono( 60,50,1 );
311 memdc
.SelectObject( mono
);
312 memdc
.SetPen( *wxBLACK_PEN
);
313 memdc
.SetBrush( *wxWHITE_BRUSH
);
314 memdc
.DrawRectangle( 0,0,60,50 );
315 memdc
.SetTextForeground( *wxBLACK
);
316 memdc
.DrawText( "Hi!", 5, 5 );
317 memdc
.SetBrush( *wxBLACK_BRUSH
);
318 memdc
.DrawRectangle( 33,5,20,20 );
319 memdc
.SetPen( *wxRED_PEN
);
320 memdc
.DrawLine( 5, 42, 50, 42 );
321 memdc
.SelectObject( wxNullBitmap
);
324 dc
.DrawText( "Mono bitmap", 30, 1845 );
325 dc
.SetTextForeground( "RED" );
326 dc
.SetTextBackground( "GREEN" );
327 dc
.DrawBitmap( mono
, 30, 1860 );
329 dc
.SetTextForeground( "BLACK" );
330 dc
.DrawText( "After wxImage conversion", 150, 1845 );
331 dc
.SetTextForeground( "RED" );
333 i
.SetMaskColour( 255,255,255 );
335 wxRED_PEN
->GetColour().Red(),
336 wxRED_PEN
->GetColour().Green(),
337 wxRED_PEN
->GetColour().Blue() );
338 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1860, TRUE
);
339 dc
.SetTextForeground( "BLACK" );
342 dc
.DrawText("XPM bitmap", 30, 1950);
343 if ( m_bmpSmileXpm
.Ok() )
345 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 1975, TRUE
);
348 dc
.DrawText("XPM icon", 150, 1950);
349 if ( m_iconSmileXpm
.Ok() )
351 dc
.DrawIcon(m_iconSmileXpm
, 150, 1975);
355 void MyCanvas::CreateAntiAliasedBitmap()
357 wxBitmap
bitmap( 300, 300 );
361 dc
.SelectObject( bitmap
);
365 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
366 dc
.SetTextForeground( "RED" );
367 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
368 dc
.DrawText( "And a Rectangle.", 20, 60 );
370 dc
.SetBrush( *wxRED_BRUSH
);
371 dc
.SetPen( *wxTRANSPARENT_PEN
);
372 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
374 wxImage
original( bitmap
);
375 wxImage
anti( 150, 150 );
377 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
379 for (int y
= 1; y
< 149; y
++)
380 for (int x
= 1; x
< 149; x
++)
382 int red
= original
.GetRed( x
*2, y
*2 ) +
383 original
.GetRed( x
*2-1, y
*2 ) +
384 original
.GetRed( x
*2, y
*2+1 ) +
385 original
.GetRed( x
*2+1, y
*2+1 );
388 int green
= original
.GetGreen( x
*2, y
*2 ) +
389 original
.GetGreen( x
*2-1, y
*2 ) +
390 original
.GetGreen( x
*2, y
*2+1 ) +
391 original
.GetGreen( x
*2+1, y
*2+1 );
394 int blue
= original
.GetBlue( x
*2, y
*2 ) +
395 original
.GetBlue( x
*2-1, y
*2 ) +
396 original
.GetBlue( x
*2, y
*2+1 ) +
397 original
.GetBlue( x
*2+1, y
*2+1 );
399 anti
.SetRGB( x
, y
, red
, green
, blue
);
401 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
406 const int ID_QUIT
= 108;
407 const int ID_ABOUT
= 109;
409 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
411 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
412 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
413 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
417 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
418 wxPoint(20,20), wxSize(470,360) )
420 wxMenu
*file_menu
= new wxMenu();
421 file_menu
->Append( ID_ABOUT
, "&About...");
422 file_menu
->Append( ID_QUIT
, "E&xit");
424 wxMenuBar
*menu_bar
= new wxMenuBar();
425 menu_bar
->Append(file_menu
, "&File");
427 SetMenuBar( menu_bar
);
430 int widths
[] = { -1, 100 };
431 SetStatusWidths( 2, widths
);
433 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
435 // 500 width * 2100 height
436 m_canvas
->SetScrollbars( 10, 10, 50, 210 );
439 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
444 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
446 (void)wxMessageBox( "wxImage demo\n"
447 "Robert Roebling (c) 1998,2000",
448 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
451 //-----------------------------------------------------------------------------
453 //-----------------------------------------------------------------------------
458 wxImage::AddHandler( new wxPNGHandler
);
462 wxImage::AddHandler( new wxJPEGHandler
);
466 wxImage::AddHandler( new wxTIFFHandler
);
470 wxImage::AddHandler( new wxGIFHandler
);
474 wxImage::AddHandler( new wxPCXHandler
);
478 wxImage::AddHandler( new wxPNMHandler
);
481 wxFrame
*frame
= new MyFrame();