]>
git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
b55dcc9eb18d103058614a2d309bb83ad462fa64
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((const char **) smile_xpm
),
105 m_iconSmileXpm((const char **) smile_xpm
)
107 my_horse_png
= (wxBitmap
*) NULL
;
108 my_horse_jpeg
= (wxBitmap
*) NULL
;
109 my_horse_gif
= (wxBitmap
*) NULL
;
110 my_horse_bmp
= (wxBitmap
*) NULL
;
111 my_horse_pcx
= (wxBitmap
*) NULL
;
112 my_horse_pnm
= (wxBitmap
*) NULL
;
113 my_horse_tiff
= (wxBitmap
*) NULL
;
114 my_smile_xbm
= (wxBitmap
*) NULL
;
115 my_square
= (wxBitmap
*) NULL
;
116 my_anti
= (wxBitmap
*) NULL
;
118 SetBackgroundColour(* wxWHITE
);
120 wxBitmap
bitmap( 100, 100 );
123 dc
.SelectObject( bitmap
);
124 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
125 dc
.SetPen( *wxBLACK_PEN
);
126 dc
.DrawRectangle( 0, 0, 100, 100 );
127 dc
.SetBrush( *wxWHITE_BRUSH
);
128 dc
.DrawRectangle( 20, 20, 60, 60 );
129 dc
.SelectObject( wxNullBitmap
);
131 // try to find the directory with our images
133 if ( wxFile::Exists("./horse.png") )
135 else if ( wxFile::Exists("../horse.png") )
138 wxLogWarning("Can't find image files in either '.' or '..'!");
140 wxImage
image( bitmap
);
143 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
) )
144 wxLogError("Can't save file");
146 if ( !image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
) )
147 wxLogError("Can't load PNG image");
149 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
150 #endif // wxUSE_LIBPNG
153 if ( !image
.LoadFile( dir
+ wxString("horse.jpg") ) )
154 wxLogError("Can't load JPG image");
156 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
157 #endif // wxUSE_LIBJPEG
160 if ( !image
.LoadFile( dir
+ wxString("horse.gif") ) )
161 wxLogError("Can't load GIF image");
163 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
167 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
168 wxLogError("Can't load PCX image");
170 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
173 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
174 wxLogError("Can't load BMP image");
176 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
179 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
180 wxLogError("Can't load PNM image");
182 my_horse_pnm
= new wxBitmap( image
.ConvertToBitmap() );
186 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
187 wxLogError("Can't load TIFF image");
189 my_horse_tiff
= new wxBitmap( image
.ConvertToBitmap() );
192 image
.LoadFile( dir
+ wxString("test.pcx") );
193 my_square
= new wxBitmap( image
.ConvertToBitmap() );
195 CreateAntiAliasedBitmap();
197 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
200 // demonstrates XPM automatically using the mask when saving
201 if ( m_bmpSmileXpm
.Ok() )
202 m_bmpSmileXpm
.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM
);
205 MyCanvas::~MyCanvas()
209 delete my_horse_jpeg
;
213 delete my_horse_tiff
;
219 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
221 wxPaintDC
dc( this );
224 dc
.DrawText( "Loaded image", 30, 10 );
225 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
227 dc
.DrawText( "Drawn directly", 150, 10 );
228 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
229 dc
.SetPen( *wxBLACK_PEN
);
230 dc
.DrawRectangle( 150, 30, 100, 100 );
231 dc
.SetBrush( *wxWHITE_BRUSH
);
232 dc
.DrawRectangle( 170, 50, 60, 60 );
234 if (my_anti
&& my_anti
->Ok())
235 dc
.DrawBitmap( *my_anti
, 280, 30 );
237 dc
.DrawText( "PNG handler", 30, 135 );
238 if (my_horse_png
&& my_horse_png
->Ok())
240 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
241 wxRect
rect(0,0,100,100);
242 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
243 dc
.DrawText( "GetSubBitmap()", 280, 190 );
244 dc
.DrawBitmap( sub
, 280, 210 );
247 dc
.DrawText( "JPEG handler", 30, 365 );
248 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
249 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
251 dc
.DrawText( "GIF handler", 30, 595 );
252 if (my_horse_gif
&& my_horse_gif
->Ok())
253 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
255 dc
.DrawText( "PCX handler", 30, 825 );
256 if (my_horse_pcx
&& my_horse_pcx
->Ok())
257 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
259 dc
.DrawText( "BMP handler", 30, 1055 );
260 if (my_horse_bmp
&& my_horse_bmp
->Ok())
261 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
263 dc
.DrawText( "PNM handler", 30, 1285 );
264 if (my_horse_pnm
&& my_horse_pnm
->Ok())
265 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
267 dc
.DrawText( "TIFF handler", 30, 1515 );
268 if (my_horse_tiff
&& my_horse_tiff
->Ok())
269 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
271 if (my_smile_xbm
&& my_smile_xbm
->Ok())
273 dc
.DrawText( "XBM bitmap", 30, 1745 );
274 dc
.SetTextForeground( "RED" );
275 dc
.SetTextBackground( "GREEN" );
276 dc
.DrawBitmap( *my_smile_xbm
, 30, 1760 );
278 dc
.DrawText( "After wxImage conversion", 150, 1745 );
279 wxImage
i( *my_smile_xbm
);
280 i
.SetMaskColour( 0,0,0 );
281 i
.Replace( 255,255,255,
282 wxRED_PEN
->GetColour().Red(),
283 wxRED_PEN
->GetColour().Green(),
284 wxRED_PEN
->GetColour().Blue() );
285 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1760, TRUE
);
287 dc
.SetTextForeground( "BLACK" );
289 wxBitmap
mono( 60,50,1 );
291 memdc
.SelectObject( mono
);
292 memdc
.SetPen( *wxBLACK_PEN
);
293 memdc
.SetBrush( *wxWHITE_BRUSH
);
294 memdc
.DrawRectangle( 0,0,60,50 );
295 memdc
.SetTextForeground( *wxBLACK
);
296 memdc
.DrawText( "Hi!", 5, 5 );
297 memdc
.SetBrush( *wxBLACK_BRUSH
);
298 memdc
.DrawRectangle( 33,5,20,20 );
299 memdc
.SetPen( *wxRED_PEN
);
300 memdc
.DrawLine( 5, 42, 50, 42 );
301 memdc
.SelectObject( wxNullBitmap
);
304 dc
.DrawText( "Mono bitmap", 30, 1845 );
305 dc
.SetTextForeground( "RED" );
306 dc
.SetTextBackground( "GREEN" );
307 dc
.DrawBitmap( mono
, 30, 1860 );
309 dc
.SetTextForeground( "BLACK" );
310 dc
.DrawText( "After wxImage conversion", 150, 1845 );
311 dc
.SetTextForeground( "RED" );
313 i
.SetMaskColour( 255,255,255 );
315 wxRED_PEN
->GetColour().Red(),
316 wxRED_PEN
->GetColour().Green(),
317 wxRED_PEN
->GetColour().Blue() );
318 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1860, TRUE
);
319 dc
.SetTextForeground( "BLACK" );
322 dc
.DrawText("XPM bitmap", 30, 1950);
323 if ( m_bmpSmileXpm
.Ok() )
325 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 1975, TRUE
);
328 dc
.DrawText("XPM icon", 150, 1950);
329 if ( m_iconSmileXpm
.Ok() )
331 dc
.DrawIcon(m_iconSmileXpm
, 150, 1975);
335 void MyCanvas::CreateAntiAliasedBitmap()
337 wxBitmap
bitmap( 300, 300 );
341 dc
.SelectObject( bitmap
);
345 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
346 dc
.SetTextForeground( "RED" );
347 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
348 dc
.DrawText( "And a Rectangle.", 20, 60 );
350 dc
.SetBrush( *wxRED_BRUSH
);
351 dc
.SetPen( *wxTRANSPARENT_PEN
);
352 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
354 wxImage
original( bitmap
);
355 wxImage
anti( 150, 150 );
357 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
359 for (int y
= 1; y
< 149; y
++)
360 for (int x
= 1; x
< 149; x
++)
362 int red
= original
.GetRed( x
*2, y
*2 ) +
363 original
.GetRed( x
*2-1, y
*2 ) +
364 original
.GetRed( x
*2, y
*2+1 ) +
365 original
.GetRed( x
*2+1, y
*2+1 );
368 int green
= original
.GetGreen( x
*2, y
*2 ) +
369 original
.GetGreen( x
*2-1, y
*2 ) +
370 original
.GetGreen( x
*2, y
*2+1 ) +
371 original
.GetGreen( x
*2+1, y
*2+1 );
374 int blue
= original
.GetBlue( x
*2, y
*2 ) +
375 original
.GetBlue( x
*2-1, y
*2 ) +
376 original
.GetBlue( x
*2, y
*2+1 ) +
377 original
.GetBlue( x
*2+1, y
*2+1 );
379 anti
.SetRGB( x
, y
, red
, green
, blue
);
381 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
386 const int ID_QUIT
= 108;
387 const int ID_ABOUT
= 109;
389 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
391 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
392 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
393 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
397 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
398 wxPoint(20,20), wxSize(470,360) )
400 wxMenu
*file_menu
= new wxMenu();
401 file_menu
->Append( ID_ABOUT
, "&About..");
402 file_menu
->Append( ID_QUIT
, "E&xit");
404 wxMenuBar
*menu_bar
= new wxMenuBar();
405 menu_bar
->Append(file_menu
, "&File");
407 SetMenuBar( menu_bar
);
410 int widths
[] = { -1, 100 };
411 SetStatusWidths( 2, widths
);
413 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
415 // 500 width * 2100 height
416 m_canvas
->SetScrollbars( 10, 10, 50, 210 );
419 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
424 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
426 (void)wxMessageBox( "wxImage demo\n"
427 "Robert Roebling (c) 1998,2000",
428 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
431 //-----------------------------------------------------------------------------
433 //-----------------------------------------------------------------------------
438 wxImage::AddHandler( new wxPNGHandler
);
442 wxImage::AddHandler( new wxJPEGHandler
);
446 wxImage::AddHandler( new wxTIFFHandler
);
450 wxImage::AddHandler( new wxGIFHandler
);
454 wxImage::AddHandler( new wxPCXHandler
);
458 wxImage::AddHandler( new wxPNMHandler
);
461 wxFrame
*frame
= new MyFrame();