]>
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 DECLARE_DYNAMIC_CLASS(MyCanvas
)
61 class MyFrame
: public wxFrame
66 void OnAbout( wxCommandEvent
&event
);
67 void OnQuit( wxCommandEvent
&event
);
72 DECLARE_DYNAMIC_CLASS(MyFrame
)
78 class MyApp
: public wxApp
81 virtual bool OnInit();
90 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
92 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
93 EVT_PAINT(MyCanvas::OnPaint
)
96 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
97 const wxPoint
&pos
, const wxSize
&size
)
98 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
100 my_horse_png
= (wxBitmap
*) NULL
;
101 my_horse_jpeg
= (wxBitmap
*) NULL
;
102 my_horse_gif
= (wxBitmap
*) NULL
;
103 my_horse_bmp
= (wxBitmap
*) NULL
;
104 my_horse_pcx
= (wxBitmap
*) NULL
;
105 my_horse_pnm
= (wxBitmap
*) NULL
;
106 my_horse_tiff
= (wxBitmap
*) NULL
;
107 my_smile_xbm
= (wxBitmap
*) NULL
;
108 my_square
= (wxBitmap
*) NULL
;
109 my_anti
= (wxBitmap
*) NULL
;
111 SetBackgroundColour(* wxWHITE
);
113 wxBitmap
bitmap( 100, 100 );
116 dc
.SelectObject( bitmap
);
117 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
118 dc
.SetPen( *wxBLACK_PEN
);
119 dc
.DrawRectangle( 0, 0, 100, 100 );
120 dc
.SetBrush( *wxWHITE_BRUSH
);
121 dc
.DrawRectangle( 20, 20, 60, 60 );
122 dc
.SelectObject( wxNullBitmap
);
124 // try to find the directory with our images
126 if ( wxFile::Exists("./horse.png") )
128 else if ( wxFile::Exists("../horse.png") )
131 wxLogWarning("Can't find image files in either '.' or '..'!");
133 wxImage
image( bitmap
);
135 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
) )
136 wxLogError("Can't save file");
138 if ( !image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
) )
139 wxLogError("Can't load PNG image");
141 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
143 if ( !image
.LoadFile( dir
+ wxString("horse.jpg") ) )
144 wxLogError("Can't load JPG image");
146 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
149 if ( !image
.LoadFile( dir
+ wxString("horse.gif") ) )
150 wxLogError("Can't load GIF image");
152 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
156 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
157 wxLogError("Can't load PCX image");
159 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
162 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
163 wxLogError("Can't load BMP image");
165 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
168 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
169 wxLogError("Can't load PNM image");
171 my_horse_pnm
= new wxBitmap( image
.ConvertToBitmap() );
175 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
176 wxLogError("Can't load TIFF image");
178 my_horse_tiff
= new wxBitmap( image
.ConvertToBitmap() );
181 image
.LoadFile( dir
+ wxString("test.png") );
182 my_square
= new wxBitmap( image
.ConvertToBitmap() );
184 CreateAntiAliasedBitmap();
186 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
190 MyCanvas::~MyCanvas()
194 delete my_horse_jpeg
;
198 delete my_horse_tiff
;
204 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
206 wxPaintDC
dc( this );
209 dc
.DrawText( "Loaded image", 30, 10 );
210 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
212 dc
.DrawText( "Drawn directly", 150, 10 );
213 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
214 dc
.SetPen( *wxBLACK_PEN
);
215 dc
.DrawRectangle( 150, 30, 100, 100 );
216 dc
.SetBrush( *wxWHITE_BRUSH
);
217 dc
.DrawRectangle( 170, 50, 60, 60 );
219 if (my_anti
&& my_anti
->Ok())
220 dc
.DrawBitmap( *my_anti
, 280, 30 );
222 dc
.DrawText( "PNG handler", 30, 135 );
223 if (my_horse_png
&& my_horse_png
->Ok())
225 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
226 wxRect
rect(0,0,100,100);
227 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
228 dc
.DrawText( "GetSubBitmap()", 280, 190 );
229 dc
.DrawBitmap( sub
, 280, 210 );
232 dc
.DrawText( "JPEG handler", 30, 365 );
233 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
234 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
236 dc
.DrawText( "GIF handler", 30, 595 );
237 if (my_horse_gif
&& my_horse_gif
->Ok())
238 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
240 dc
.DrawText( "PCX handler", 30, 825 );
241 if (my_horse_pcx
&& my_horse_pcx
->Ok())
242 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
244 dc
.DrawText( "BMP handler", 30, 1055 );
245 if (my_horse_bmp
&& my_horse_bmp
->Ok())
246 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
248 dc
.DrawText( "PNM handler", 30, 1285 );
249 if (my_horse_pnm
&& my_horse_pnm
->Ok())
250 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
252 dc
.DrawText( "TIFF handler", 30, 1515 );
253 if (my_horse_tiff
&& my_horse_tiff
->Ok())
254 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
256 if (my_smile_xbm
&& my_smile_xbm
->Ok())
258 dc
.DrawText( "XBM bitmap", 30, 1745 );
259 dc
.SetTextForeground( "RED" );
260 dc
.SetTextBackground( "GREEN" );
261 dc
.DrawBitmap( *my_smile_xbm
, 30, 1760 );
263 dc
.DrawText( "After wxImage conversion", 150, 1745 );
264 wxImage
i( *my_smile_xbm
);
265 i
.SetMaskColour( 0,0,0 );
266 i
.Replace( 255,255,255,
267 wxRED_PEN
->GetColour().Red(),
268 wxRED_PEN
->GetColour().Green(),
269 wxRED_PEN
->GetColour().Blue() );
270 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1760, TRUE
);
272 dc
.SetTextForeground( "BLACK" );
274 wxBitmap
mono( 60,50,1 );
276 memdc
.SelectObject( mono
);
277 memdc
.SetPen( *wxTRANSPARENT_PEN
);
278 memdc
.SetBrush( *wxWHITE_BRUSH
);
279 memdc
.DrawRectangle( 0,0,60,50 );
280 memdc
.SetTextForeground( *wxBLACK
);
281 memdc
.DrawText( "Hi!", 5, 5 );
282 memdc
.SetBrush( *wxBLACK_BRUSH
);
283 memdc
.DrawRectangle( 33,5,20,20 );
284 memdc
.SetPen( *wxRED_PEN
);
285 memdc
.DrawLine( 5, 42, 50, 42 );
286 memdc
.SelectObject( wxNullBitmap
);
289 dc
.DrawText( "Mono bitmap", 30, 1845 );
290 dc
.SetTextForeground( "RED" );
291 dc
.SetTextBackground( "GREEN" );
292 dc
.DrawBitmap( mono
, 30, 1860 );
294 dc
.DrawText( "After wxImage conversion", 150, 1845 );
296 i
.SetMaskColour( 255,255,255 );
298 wxRED_PEN
->GetColour().Red(),
299 wxRED_PEN
->GetColour().Green(),
300 wxRED_PEN
->GetColour().Blue() );
301 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1860, TRUE
);
305 void MyCanvas::CreateAntiAliasedBitmap()
307 wxBitmap
bitmap( 300, 300 );
311 dc
.SelectObject( bitmap
);
315 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
316 dc
.SetTextForeground( "RED" );
317 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
318 dc
.DrawText( "And a Rectangle.", 20, 60 );
320 dc
.SetBrush( *wxRED_BRUSH
);
321 dc
.SetPen( *wxTRANSPARENT_PEN
);
322 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
324 wxImage
original( bitmap
);
325 wxImage
anti( 150, 150 );
327 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
329 for (int y
= 1; y
< 149; y
++)
330 for (int x
= 1; x
< 149; x
++)
332 int red
= original
.GetRed( x
*2, y
*2 ) +
333 original
.GetRed( x
*2-1, y
*2 ) +
334 original
.GetRed( x
*2, y
*2+1 ) +
335 original
.GetRed( x
*2+1, y
*2+1 );
338 int green
= original
.GetGreen( x
*2, y
*2 ) +
339 original
.GetGreen( x
*2-1, y
*2 ) +
340 original
.GetGreen( x
*2, y
*2+1 ) +
341 original
.GetGreen( x
*2+1, y
*2+1 );
344 int blue
= original
.GetBlue( x
*2, y
*2 ) +
345 original
.GetBlue( x
*2-1, y
*2 ) +
346 original
.GetBlue( x
*2, y
*2+1 ) +
347 original
.GetBlue( x
*2+1, y
*2+1 );
349 anti
.SetRGB( x
, y
, red
, green
, blue
);
351 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
356 const int ID_QUIT
= 108;
357 const int ID_ABOUT
= 109;
359 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
361 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
362 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
363 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
367 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
368 wxPoint(20,20), wxSize(470,360) )
370 wxMenu
*file_menu
= new wxMenu();
371 file_menu
->Append( ID_ABOUT
, "&About..");
372 file_menu
->Append( ID_QUIT
, "E&xit");
374 wxMenuBar
*menu_bar
= new wxMenuBar();
375 menu_bar
->Append(file_menu
, "&File");
377 SetMenuBar( menu_bar
);
380 int widths
[] = { -1, 100 };
381 SetStatusWidths( 2, widths
);
383 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
385 // 500 width * 1900 height
386 m_canvas
->SetScrollbars( 10, 10, 50, 200 );
389 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
394 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
396 (void)wxMessageBox( "wxImage demo\n"
397 "Robert Roebling (c) 1998,2000",
398 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
401 //-----------------------------------------------------------------------------
403 //-----------------------------------------------------------------------------
408 wxImage::AddHandler( new wxPNGHandler
);
412 wxImage::AddHandler( new wxJPEGHandler
);
416 wxImage::AddHandler( new wxTIFFHandler
);
420 wxImage::AddHandler( new wxGIFHandler
);
424 wxImage::AddHandler( new wxPCXHandler
);
428 wxImage::AddHandler( new wxPNMHandler
);
431 wxFrame
*frame
= new MyFrame();