]>
git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
a0a7f0e5c87ea2c78b653364a840fb3a5d793729
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 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
) )
146 wxLogError("Can't save file");
148 image
= wxImage( 100, 100 );
150 image
.LoadFile( dir
+ wxString("test.png") );
151 my_square
= new wxBitmap( image
.ConvertToBitmap() );
153 image
= wxImage( 100, 100 );
155 if ( !image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
) )
156 wxLogError("Can't load PNG image");
158 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
159 #endif // wxUSE_LIBPNG
162 image
= wxImage( 100, 100 );
164 if ( !image
.LoadFile( dir
+ wxString("horse.jpg") ) )
165 wxLogError("Can't load JPG image");
167 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
168 #endif // wxUSE_LIBJPEG
171 image
= wxImage( 100, 100 );
173 if ( !image
.LoadFile( dir
+ wxString("horse.gif") ) )
174 wxLogError("Can't load GIF image");
176 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
180 image
= wxImage( 100, 100 );
182 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
183 wxLogError("Can't load PCX image");
185 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
188 image
= wxImage( 100, 100 );
190 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
191 wxLogError("Can't load BMP image");
193 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
196 image
= wxImage( 100, 100 );
198 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
199 wxLogError("Can't load PNM image");
201 my_horse_pnm
= new wxBitmap( image
.ConvertToBitmap() );
205 image
= wxImage( 100, 100 );
207 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
208 wxLogError("Can't load TIFF image");
210 my_horse_tiff
= new wxBitmap( image
.ConvertToBitmap() );
213 CreateAntiAliasedBitmap();
215 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
219 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
220 // demonstrates XPM automatically using the mask when saving
221 if ( m_bmpSmileXpm
.Ok() )
222 m_bmpSmileXpm
.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM
);
227 MyCanvas::~MyCanvas()
231 delete my_horse_jpeg
;
235 delete my_horse_tiff
;
241 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
243 wxPaintDC
dc( this );
246 dc
.DrawText( "Loaded image", 30, 10 );
247 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
249 dc
.DrawText( "Drawn directly", 150, 10 );
250 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
251 dc
.SetPen( *wxBLACK_PEN
);
252 dc
.DrawRectangle( 150, 30, 100, 100 );
253 dc
.SetBrush( *wxWHITE_BRUSH
);
254 dc
.DrawRectangle( 170, 50, 60, 60 );
256 if (my_anti
&& my_anti
->Ok())
257 dc
.DrawBitmap( *my_anti
, 280, 30 );
259 dc
.DrawText( "PNG handler", 30, 135 );
260 if (my_horse_png
&& my_horse_png
->Ok())
262 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
263 wxRect
rect(0,0,100,100);
264 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
265 dc
.DrawText( "GetSubBitmap()", 280, 190 );
266 dc
.DrawBitmap( sub
, 280, 210 );
269 dc
.DrawText( "JPEG handler", 30, 365 );
270 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
271 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
273 dc
.DrawText( "GIF handler", 30, 595 );
274 if (my_horse_gif
&& my_horse_gif
->Ok())
275 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
277 dc
.DrawText( "PCX handler", 30, 825 );
278 if (my_horse_pcx
&& my_horse_pcx
->Ok())
279 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
281 dc
.DrawText( "BMP handler", 30, 1055 );
282 if (my_horse_bmp
&& my_horse_bmp
->Ok())
283 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
285 dc
.DrawText( "PNM handler", 30, 1285 );
286 if (my_horse_pnm
&& my_horse_pnm
->Ok())
287 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
289 dc
.DrawText( "TIFF handler", 30, 1515 );
290 if (my_horse_tiff
&& my_horse_tiff
->Ok())
291 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
293 if (my_smile_xbm
&& my_smile_xbm
->Ok())
295 dc
.DrawText( "XBM bitmap", 30, 1745 );
296 dc
.SetTextForeground( "RED" );
297 dc
.SetTextBackground( "GREEN" );
298 dc
.DrawBitmap( *my_smile_xbm
, 30, 1760 );
300 dc
.DrawText( "After wxImage conversion", 150, 1745 );
301 wxImage
i( *my_smile_xbm
);
302 i
.SetMaskColour( 0,0,0 );
303 i
.Replace( 255,255,255,
304 wxRED_PEN
->GetColour().Red(),
305 wxRED_PEN
->GetColour().Green(),
306 wxRED_PEN
->GetColour().Blue() );
307 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1760, TRUE
);
309 dc
.SetTextForeground( "BLACK" );
311 wxBitmap
mono( 60,50,1 );
313 memdc
.SelectObject( mono
);
314 memdc
.SetPen( *wxBLACK_PEN
);
315 memdc
.SetBrush( *wxWHITE_BRUSH
);
316 memdc
.DrawRectangle( 0,0,60,50 );
317 memdc
.SetTextForeground( *wxBLACK
);
318 memdc
.DrawText( "Hi!", 5, 5 );
319 memdc
.SetBrush( *wxBLACK_BRUSH
);
320 memdc
.DrawRectangle( 33,5,20,20 );
321 memdc
.SetPen( *wxRED_PEN
);
322 memdc
.DrawLine( 5, 42, 50, 42 );
323 memdc
.SelectObject( wxNullBitmap
);
326 dc
.DrawText( "Mono bitmap", 30, 1845 );
327 dc
.SetTextForeground( "RED" );
328 dc
.SetTextBackground( "GREEN" );
329 dc
.DrawBitmap( mono
, 30, 1860 );
331 dc
.SetTextForeground( "BLACK" );
332 dc
.DrawText( "After wxImage conversion", 150, 1845 );
333 dc
.SetTextForeground( "RED" );
335 i
.SetMaskColour( 255,255,255 );
337 wxRED_PEN
->GetColour().Red(),
338 wxRED_PEN
->GetColour().Green(),
339 wxRED_PEN
->GetColour().Blue() );
340 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1860, TRUE
);
341 dc
.SetTextForeground( "BLACK" );
344 dc
.DrawText("XPM bitmap", 30, 1950);
345 if ( m_bmpSmileXpm
.Ok() )
347 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 1975, TRUE
);
350 dc
.DrawText("XPM icon", 150, 1950);
351 if ( m_iconSmileXpm
.Ok() )
353 dc
.DrawIcon(m_iconSmileXpm
, 150, 1975);
357 void MyCanvas::CreateAntiAliasedBitmap()
359 wxBitmap
bitmap( 300, 300 );
363 dc
.SelectObject( bitmap
);
367 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
368 dc
.SetTextForeground( "RED" );
369 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
370 dc
.DrawText( "And a Rectangle.", 20, 60 );
372 dc
.SetBrush( *wxRED_BRUSH
);
373 dc
.SetPen( *wxTRANSPARENT_PEN
);
374 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
376 wxImage
original( bitmap
);
377 wxImage
anti( 150, 150 );
379 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
381 for (int y
= 1; y
< 149; y
++)
382 for (int x
= 1; x
< 149; x
++)
384 int red
= original
.GetRed( x
*2, y
*2 ) +
385 original
.GetRed( x
*2-1, y
*2 ) +
386 original
.GetRed( x
*2, y
*2+1 ) +
387 original
.GetRed( x
*2+1, y
*2+1 );
390 int green
= original
.GetGreen( x
*2, y
*2 ) +
391 original
.GetGreen( x
*2-1, y
*2 ) +
392 original
.GetGreen( x
*2, y
*2+1 ) +
393 original
.GetGreen( x
*2+1, y
*2+1 );
396 int blue
= original
.GetBlue( x
*2, y
*2 ) +
397 original
.GetBlue( x
*2-1, y
*2 ) +
398 original
.GetBlue( x
*2, y
*2+1 ) +
399 original
.GetBlue( x
*2+1, y
*2+1 );
401 anti
.SetRGB( x
, y
, red
, green
, blue
);
403 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
408 const int ID_QUIT
= 108;
409 const int ID_ABOUT
= 109;
411 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
413 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
414 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
415 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
419 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
420 wxPoint(20,20), wxSize(470,360) )
422 wxMenu
*file_menu
= new wxMenu();
423 file_menu
->Append( ID_ABOUT
, "&About...");
424 file_menu
->Append( ID_QUIT
, "E&xit");
426 wxMenuBar
*menu_bar
= new wxMenuBar();
427 menu_bar
->Append(file_menu
, "&File");
429 SetMenuBar( menu_bar
);
432 int widths
[] = { -1, 100 };
433 SetStatusWidths( 2, widths
);
435 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
437 // 500 width * 2100 height
438 m_canvas
->SetScrollbars( 10, 10, 50, 210 );
441 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
446 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
448 (void)wxMessageBox( "wxImage demo\n"
449 "Robert Roebling (c) 1998,2000",
450 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
453 //-----------------------------------------------------------------------------
455 //-----------------------------------------------------------------------------
460 wxImage::AddHandler( new wxPNGHandler
);
464 wxImage::AddHandler( new wxJPEGHandler
);
468 wxImage::AddHandler( new wxTIFFHandler
);
472 wxImage::AddHandler( new wxGIFHandler
);
476 wxImage::AddHandler( new wxPCXHandler
);
480 wxImage::AddHandler( new wxPNMHandler
);
483 wxFrame
*frame
= new MyFrame();