]>
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"
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 m_bmpSmileXpm(smile_xpm
), m_iconSmileXpm(smile_xpm
)
106 my_horse_png
= (wxBitmap
*) NULL
;
107 my_horse_jpeg
= (wxBitmap
*) NULL
;
108 my_horse_gif
= (wxBitmap
*) NULL
;
109 my_horse_bmp
= (wxBitmap
*) NULL
;
110 my_horse_pcx
= (wxBitmap
*) NULL
;
111 my_horse_pnm
= (wxBitmap
*) NULL
;
112 my_horse_tiff
= (wxBitmap
*) NULL
;
113 my_smile_xbm
= (wxBitmap
*) NULL
;
114 my_square
= (wxBitmap
*) NULL
;
115 my_anti
= (wxBitmap
*) NULL
;
117 SetBackgroundColour(* wxWHITE
);
119 wxBitmap
bitmap( 100, 100 );
122 dc
.SelectObject( bitmap
);
123 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
124 dc
.SetPen( *wxBLACK_PEN
);
125 dc
.DrawRectangle( 0, 0, 100, 100 );
126 dc
.SetBrush( *wxWHITE_BRUSH
);
127 dc
.DrawRectangle( 20, 20, 60, 60 );
128 dc
.SelectObject( wxNullBitmap
);
130 // try to find the directory with our images
132 if ( wxFile::Exists("./horse.png") )
134 else if ( wxFile::Exists("../horse.png") )
137 wxLogWarning("Can't find image files in either '.' or '..'!");
139 wxImage
image( bitmap
);
142 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
) )
143 wxLogError("Can't save file");
145 if ( !image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
) )
146 wxLogError("Can't load PNG image");
148 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
149 #endif // wxUSE_LIBPNG
152 if ( !image
.LoadFile( dir
+ wxString("horse.jpg") ) )
153 wxLogError("Can't load JPG image");
155 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
156 #endif // wxUSE_LIBJPEG
159 if ( !image
.LoadFile( dir
+ wxString("horse.gif") ) )
160 wxLogError("Can't load GIF image");
162 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
166 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
167 wxLogError("Can't load PCX image");
169 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
172 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
173 wxLogError("Can't load BMP image");
175 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
178 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
179 wxLogError("Can't load PNM image");
181 my_horse_pnm
= new wxBitmap( image
.ConvertToBitmap() );
185 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
186 wxLogError("Can't load TIFF image");
188 my_horse_tiff
= new wxBitmap( image
.ConvertToBitmap() );
191 image
.LoadFile( dir
+ wxString("test.png") );
192 my_square
= new wxBitmap( image
.ConvertToBitmap() );
194 CreateAntiAliasedBitmap();
196 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
200 MyCanvas::~MyCanvas()
204 delete my_horse_jpeg
;
208 delete my_horse_tiff
;
214 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
216 wxPaintDC
dc( this );
219 dc
.DrawText( "Loaded image", 30, 10 );
220 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
222 dc
.DrawText( "Drawn directly", 150, 10 );
223 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
224 dc
.SetPen( *wxBLACK_PEN
);
225 dc
.DrawRectangle( 150, 30, 100, 100 );
226 dc
.SetBrush( *wxWHITE_BRUSH
);
227 dc
.DrawRectangle( 170, 50, 60, 60 );
229 if (my_anti
&& my_anti
->Ok())
230 dc
.DrawBitmap( *my_anti
, 280, 30 );
232 dc
.DrawText( "PNG handler", 30, 135 );
233 if (my_horse_png
&& my_horse_png
->Ok())
235 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
236 wxRect
rect(0,0,100,100);
237 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
238 dc
.DrawText( "GetSubBitmap()", 280, 190 );
239 dc
.DrawBitmap( sub
, 280, 210 );
242 dc
.DrawText( "JPEG handler", 30, 365 );
243 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
244 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
246 dc
.DrawText( "GIF handler", 30, 595 );
247 if (my_horse_gif
&& my_horse_gif
->Ok())
248 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
250 dc
.DrawText( "PCX handler", 30, 825 );
251 if (my_horse_pcx
&& my_horse_pcx
->Ok())
252 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
254 dc
.DrawText( "BMP handler", 30, 1055 );
255 if (my_horse_bmp
&& my_horse_bmp
->Ok())
256 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
258 dc
.DrawText( "PNM handler", 30, 1285 );
259 if (my_horse_pnm
&& my_horse_pnm
->Ok())
260 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
262 dc
.DrawText( "TIFF handler", 30, 1515 );
263 if (my_horse_tiff
&& my_horse_tiff
->Ok())
264 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
266 if (my_smile_xbm
&& my_smile_xbm
->Ok())
268 dc
.DrawText( "XBM bitmap", 30, 1745 );
269 dc
.SetTextForeground( "RED" );
270 dc
.SetTextBackground( "GREEN" );
271 dc
.DrawBitmap( *my_smile_xbm
, 30, 1760 );
273 dc
.DrawText( "After wxImage conversion", 150, 1745 );
274 wxImage
i( *my_smile_xbm
);
275 i
.SetMaskColour( 0,0,0 );
276 i
.Replace( 255,255,255,
277 wxRED_PEN
->GetColour().Red(),
278 wxRED_PEN
->GetColour().Green(),
279 wxRED_PEN
->GetColour().Blue() );
280 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1760, TRUE
);
282 dc
.SetTextForeground( "BLACK" );
284 wxBitmap
mono( 60,50,1 );
286 memdc
.SelectObject( mono
);
287 memdc
.SetPen( *wxTRANSPARENT_PEN
);
288 memdc
.SetBrush( *wxWHITE_BRUSH
);
289 memdc
.DrawRectangle( 0,0,60,50 );
290 memdc
.SetTextForeground( *wxBLACK
);
291 memdc
.DrawText( "Hi!", 5, 5 );
292 memdc
.SetBrush( *wxBLACK_BRUSH
);
293 memdc
.DrawRectangle( 33,5,20,20 );
294 memdc
.SetPen( *wxRED_PEN
);
295 memdc
.DrawLine( 5, 42, 50, 42 );
296 memdc
.SelectObject( wxNullBitmap
);
299 dc
.DrawText( "Mono bitmap", 30, 1845 );
300 dc
.SetTextForeground( "RED" );
301 dc
.SetTextBackground( "GREEN" );
302 dc
.DrawBitmap( mono
, 30, 1860 );
304 dc
.SetTextForeground( "BLACK" );
305 dc
.DrawText( "After wxImage conversion", 150, 1845 );
306 dc
.SetTextForeground( "RED" );
308 i
.SetMaskColour( 255,255,255 );
310 wxRED_PEN
->GetColour().Red(),
311 wxRED_PEN
->GetColour().Green(),
312 wxRED_PEN
->GetColour().Blue() );
313 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1860, TRUE
);
314 dc
.SetTextForeground( "BLACK" );
317 dc
.DrawText("XPM bitmap", 30, 1950);
318 if ( m_bmpSmileXpm
.Ok() )
320 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 1975, TRUE
);
323 dc
.DrawText("XPM icon", 150, 1950);
324 if ( m_iconSmileXpm
.Ok() )
326 dc
.DrawIcon(m_iconSmileXpm
, 150, 1975);
330 void MyCanvas::CreateAntiAliasedBitmap()
332 wxBitmap
bitmap( 300, 300 );
336 dc
.SelectObject( bitmap
);
340 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
341 dc
.SetTextForeground( "RED" );
342 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
343 dc
.DrawText( "And a Rectangle.", 20, 60 );
345 dc
.SetBrush( *wxRED_BRUSH
);
346 dc
.SetPen( *wxTRANSPARENT_PEN
);
347 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
349 wxImage
original( bitmap
);
350 wxImage
anti( 150, 150 );
352 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
354 for (int y
= 1; y
< 149; y
++)
355 for (int x
= 1; x
< 149; x
++)
357 int red
= original
.GetRed( x
*2, y
*2 ) +
358 original
.GetRed( x
*2-1, y
*2 ) +
359 original
.GetRed( x
*2, y
*2+1 ) +
360 original
.GetRed( x
*2+1, y
*2+1 );
363 int green
= original
.GetGreen( x
*2, y
*2 ) +
364 original
.GetGreen( x
*2-1, y
*2 ) +
365 original
.GetGreen( x
*2, y
*2+1 ) +
366 original
.GetGreen( x
*2+1, y
*2+1 );
369 int blue
= original
.GetBlue( x
*2, y
*2 ) +
370 original
.GetBlue( x
*2-1, y
*2 ) +
371 original
.GetBlue( x
*2, y
*2+1 ) +
372 original
.GetBlue( x
*2+1, y
*2+1 );
374 anti
.SetRGB( x
, y
, red
, green
, blue
);
376 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
381 const int ID_QUIT
= 108;
382 const int ID_ABOUT
= 109;
384 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
386 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
387 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
388 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
392 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
393 wxPoint(20,20), wxSize(470,360) )
395 wxMenu
*file_menu
= new wxMenu();
396 file_menu
->Append( ID_ABOUT
, "&About..");
397 file_menu
->Append( ID_QUIT
, "E&xit");
399 wxMenuBar
*menu_bar
= new wxMenuBar();
400 menu_bar
->Append(file_menu
, "&File");
402 SetMenuBar( menu_bar
);
405 int widths
[] = { -1, 100 };
406 SetStatusWidths( 2, widths
);
408 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
410 // 500 width * 2100 height
411 m_canvas
->SetScrollbars( 10, 10, 50, 210 );
414 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
419 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
421 (void)wxMessageBox( "wxImage demo\n"
422 "Robert Roebling (c) 1998,2000",
423 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
426 //-----------------------------------------------------------------------------
428 //-----------------------------------------------------------------------------
433 wxImage::AddHandler( new wxPNGHandler
);
437 wxImage::AddHandler( new wxJPEGHandler
);
441 wxImage::AddHandler( new wxTIFFHandler
);
445 wxImage::AddHandler( new wxGIFHandler
);
449 wxImage::AddHandler( new wxPCXHandler
);
453 wxImage::AddHandler( new wxPNMHandler
);
456 wxFrame
*frame
= new MyFrame();