]>
git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
63cc8f3b17019ec17fd92373b0fa0399eeb98efe
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 if ( !image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
) )
149 wxLogError("Can't load PNG image");
151 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
152 #endif // wxUSE_LIBPNG
155 if ( !image
.LoadFile( dir
+ wxString("horse.jpg") ) )
156 wxLogError("Can't load JPG image");
158 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
159 #endif // wxUSE_LIBJPEG
162 if ( !image
.LoadFile( dir
+ wxString("horse.gif") ) )
163 wxLogError("Can't load GIF image");
165 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
169 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
170 wxLogError("Can't load PCX image");
172 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
174 image
.LoadFile( dir
+ wxString("test.pcx") );
175 my_square
= new wxBitmap( image
.ConvertToBitmap() );
178 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
179 wxLogError("Can't load BMP image");
181 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
184 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
185 wxLogError("Can't load PNM image");
187 my_horse_pnm
= new wxBitmap( image
.ConvertToBitmap() );
191 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
192 wxLogError("Can't load TIFF image");
194 my_horse_tiff
= new wxBitmap( image
.ConvertToBitmap() );
197 CreateAntiAliasedBitmap();
199 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
202 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
203 // demonstrates XPM automatically using the mask when saving
204 if ( m_bmpSmileXpm
.Ok() )
205 m_bmpSmileXpm
.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM
);
209 MyCanvas::~MyCanvas()
213 delete my_horse_jpeg
;
217 delete my_horse_tiff
;
223 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
225 wxPaintDC
dc( this );
228 dc
.DrawText( "Loaded image", 30, 10 );
229 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
231 dc
.DrawText( "Drawn directly", 150, 10 );
232 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
233 dc
.SetPen( *wxBLACK_PEN
);
234 dc
.DrawRectangle( 150, 30, 100, 100 );
235 dc
.SetBrush( *wxWHITE_BRUSH
);
236 dc
.DrawRectangle( 170, 50, 60, 60 );
238 if (my_anti
&& my_anti
->Ok())
239 dc
.DrawBitmap( *my_anti
, 280, 30 );
241 dc
.DrawText( "PNG handler", 30, 135 );
242 if (my_horse_png
&& my_horse_png
->Ok())
244 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
245 wxRect
rect(0,0,100,100);
246 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
247 dc
.DrawText( "GetSubBitmap()", 280, 190 );
248 dc
.DrawBitmap( sub
, 280, 210 );
251 dc
.DrawText( "JPEG handler", 30, 365 );
252 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
253 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
255 dc
.DrawText( "GIF handler", 30, 595 );
256 if (my_horse_gif
&& my_horse_gif
->Ok())
257 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
259 dc
.DrawText( "PCX handler", 30, 825 );
260 if (my_horse_pcx
&& my_horse_pcx
->Ok())
261 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
263 dc
.DrawText( "BMP handler", 30, 1055 );
264 if (my_horse_bmp
&& my_horse_bmp
->Ok())
265 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
267 dc
.DrawText( "PNM handler", 30, 1285 );
268 if (my_horse_pnm
&& my_horse_pnm
->Ok())
269 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
271 dc
.DrawText( "TIFF handler", 30, 1515 );
272 if (my_horse_tiff
&& my_horse_tiff
->Ok())
273 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
275 if (my_smile_xbm
&& my_smile_xbm
->Ok())
277 dc
.DrawText( "XBM bitmap", 30, 1745 );
278 dc
.SetTextForeground( "RED" );
279 dc
.SetTextBackground( "GREEN" );
280 dc
.DrawBitmap( *my_smile_xbm
, 30, 1760 );
282 dc
.DrawText( "After wxImage conversion", 150, 1745 );
283 wxImage
i( *my_smile_xbm
);
284 i
.SetMaskColour( 0,0,0 );
285 i
.Replace( 255,255,255,
286 wxRED_PEN
->GetColour().Red(),
287 wxRED_PEN
->GetColour().Green(),
288 wxRED_PEN
->GetColour().Blue() );
289 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1760, TRUE
);
291 dc
.SetTextForeground( "BLACK" );
293 wxBitmap
mono( 60,50,1 );
295 memdc
.SelectObject( mono
);
296 memdc
.SetPen( *wxBLACK_PEN
);
297 memdc
.SetBrush( *wxWHITE_BRUSH
);
298 memdc
.DrawRectangle( 0,0,60,50 );
299 memdc
.SetTextForeground( *wxBLACK
);
300 memdc
.DrawText( "Hi!", 5, 5 );
301 memdc
.SetBrush( *wxBLACK_BRUSH
);
302 memdc
.DrawRectangle( 33,5,20,20 );
303 memdc
.SetPen( *wxRED_PEN
);
304 memdc
.DrawLine( 5, 42, 50, 42 );
305 memdc
.SelectObject( wxNullBitmap
);
308 dc
.DrawText( "Mono bitmap", 30, 1845 );
309 dc
.SetTextForeground( "RED" );
310 dc
.SetTextBackground( "GREEN" );
311 dc
.DrawBitmap( mono
, 30, 1860 );
313 dc
.SetTextForeground( "BLACK" );
314 dc
.DrawText( "After wxImage conversion", 150, 1845 );
315 dc
.SetTextForeground( "RED" );
317 i
.SetMaskColour( 255,255,255 );
319 wxRED_PEN
->GetColour().Red(),
320 wxRED_PEN
->GetColour().Green(),
321 wxRED_PEN
->GetColour().Blue() );
322 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1860, TRUE
);
323 dc
.SetTextForeground( "BLACK" );
326 dc
.DrawText("XPM bitmap", 30, 1950);
327 if ( m_bmpSmileXpm
.Ok() )
329 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 1975, TRUE
);
332 dc
.DrawText("XPM icon", 150, 1950);
333 if ( m_iconSmileXpm
.Ok() )
335 dc
.DrawIcon(m_iconSmileXpm
, 150, 1975);
339 void MyCanvas::CreateAntiAliasedBitmap()
341 wxBitmap
bitmap( 300, 300 );
345 dc
.SelectObject( bitmap
);
349 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
350 dc
.SetTextForeground( "RED" );
351 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
352 dc
.DrawText( "And a Rectangle.", 20, 60 );
354 dc
.SetBrush( *wxRED_BRUSH
);
355 dc
.SetPen( *wxTRANSPARENT_PEN
);
356 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
358 wxImage
original( bitmap
);
359 wxImage
anti( 150, 150 );
361 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
363 for (int y
= 1; y
< 149; y
++)
364 for (int x
= 1; x
< 149; x
++)
366 int red
= original
.GetRed( x
*2, y
*2 ) +
367 original
.GetRed( x
*2-1, y
*2 ) +
368 original
.GetRed( x
*2, y
*2+1 ) +
369 original
.GetRed( x
*2+1, y
*2+1 );
372 int green
= original
.GetGreen( x
*2, y
*2 ) +
373 original
.GetGreen( x
*2-1, y
*2 ) +
374 original
.GetGreen( x
*2, y
*2+1 ) +
375 original
.GetGreen( x
*2+1, y
*2+1 );
378 int blue
= original
.GetBlue( x
*2, y
*2 ) +
379 original
.GetBlue( x
*2-1, y
*2 ) +
380 original
.GetBlue( x
*2, y
*2+1 ) +
381 original
.GetBlue( x
*2+1, y
*2+1 );
383 anti
.SetRGB( x
, y
, red
, green
, blue
);
385 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
390 const int ID_QUIT
= 108;
391 const int ID_ABOUT
= 109;
393 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
395 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
396 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
397 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
401 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
402 wxPoint(20,20), wxSize(470,360) )
404 wxMenu
*file_menu
= new wxMenu();
405 file_menu
->Append( ID_ABOUT
, "&About...");
406 file_menu
->Append( ID_QUIT
, "E&xit");
408 wxMenuBar
*menu_bar
= new wxMenuBar();
409 menu_bar
->Append(file_menu
, "&File");
411 SetMenuBar( menu_bar
);
414 int widths
[] = { -1, 100 };
415 SetStatusWidths( 2, widths
);
417 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
419 // 500 width * 2100 height
420 m_canvas
->SetScrollbars( 10, 10, 50, 210 );
423 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
428 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
430 (void)wxMessageBox( "wxImage demo\n"
431 "Robert Roebling (c) 1998,2000",
432 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
435 //-----------------------------------------------------------------------------
437 //-----------------------------------------------------------------------------
442 wxImage::AddHandler( new wxPNGHandler
);
446 wxImage::AddHandler( new wxJPEGHandler
);
450 wxImage::AddHandler( new wxTIFFHandler
);
454 wxImage::AddHandler( new wxGIFHandler
);
458 wxImage::AddHandler( new wxPCXHandler
);
462 wxImage::AddHandler( new wxPNMHandler
);
465 wxFrame
*frame
= new MyFrame();