]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
Added DEVNAMES to wxPrintDialog to remember printer name; fixed wxDC::Clear
[wxWidgets.git] / samples / image / image.cpp
CommitLineData
01111366
RR
1/*
2 * Program: image
1d5b7a0b 3 *
01111366
RR
4 * Author: Robert Roebling
5 *
6 * Copyright: (C) 1998, Robert Roebling
7 *
8 */
9
3d05544e
JS
10// For compilers that support precompilation, includes "wx/wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
14#pragma hdrstop
15#endif
16
17#ifndef WX_PRECOMP
01111366 18#include "wx/wx.h"
3d05544e
JS
19#endif
20
01111366
RR
21#include "wx/image.h"
22
a23fd0e1
VZ
23#include "wx/file.h"
24
dc1dbfc6 25#include "smile.xbm"
bea56879 26#include "smile.xpm"
dc1dbfc6 27
01111366
RR
28// derived classes
29
30class MyFrame;
31class MyApp;
32
33// MyCanvas
34
35class MyCanvas: public wxScrolledWindow
36{
1d5b7a0b
VZ
37public:
38 MyCanvas() {};
01111366 39 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
1d5b7a0b 40 ~MyCanvas();
01111366 41 void OnPaint( wxPaintEvent &event );
9390a202 42 void CreateAntiAliasedBitmap();
1d5b7a0b 43
e1929140
RR
44 wxBitmap *my_horse_png;
45 wxBitmap *my_horse_jpeg;
46 wxBitmap *my_horse_gif;
ca26177c 47 wxBitmap *my_horse_bmp;
cbd4be25 48 wxBitmap *my_horse_pcx;
6319afe3 49 wxBitmap *my_horse_pnm;
60a41aee 50 wxBitmap *my_horse_tiff;
dc1dbfc6 51 wxBitmap *my_smile_xbm;
e8fdc264 52 wxBitmap *my_square;
9390a202 53 wxBitmap *my_anti;
1d5b7a0b 54
bea56879
VZ
55protected:
56 wxBitmap m_bmpSmileXpm;
57 wxIcon m_iconSmileXpm;
58
60a41aee 59private:
1d5b7a0b
VZ
60 DECLARE_DYNAMIC_CLASS(MyCanvas)
61 DECLARE_EVENT_TABLE()
01111366
RR
62};
63
64// MyFrame
65
66class MyFrame: public wxFrame
67{
1d5b7a0b
VZ
68public:
69 MyFrame();
01111366 70
01111366
RR
71 void OnAbout( wxCommandEvent &event );
72 void OnQuit( wxCommandEvent &event );
1d5b7a0b 73
01111366 74 MyCanvas *m_canvas;
1d5b7a0b 75
60a41aee 76private:
1d5b7a0b
VZ
77 DECLARE_DYNAMIC_CLASS(MyFrame)
78 DECLARE_EVENT_TABLE()
01111366
RR
79};
80
81// MyApp
82
83class MyApp: public wxApp
84{
1d5b7a0b
VZ
85public:
86 virtual bool OnInit();
01111366
RR
87};
88
89// main program
90
91IMPLEMENT_APP(MyApp)
92
93// MyCanvas
94
95IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
96
1d5b7a0b
VZ
97BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
98 EVT_PAINT(MyCanvas::OnPaint)
01111366
RR
99END_EVENT_TABLE()
100
acbd13a3 101MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
1d5b7a0b 102 const wxPoint &pos, const wxSize &size )
bea56879
VZ
103 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER ),
104 m_bmpSmileXpm(smile_xpm), m_iconSmileXpm(smile_xpm)
01111366 105{
60a41aee
RR
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;
dc1dbfc6 113 my_smile_xbm = (wxBitmap*) NULL;
60a41aee
RR
114 my_square = (wxBitmap*) NULL;
115 my_anti = (wxBitmap*) NULL;
116
117 SetBackgroundColour(* wxWHITE);
118
119 wxBitmap bitmap( 100, 100 );
120
121 wxMemoryDC dc;
122 dc.SelectObject( bitmap );
123 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
cf3da716 124 dc.SetPen( *wxBLACK_PEN );
60a41aee 125 dc.DrawRectangle( 0, 0, 100, 100 );
cf3da716
RR
126 dc.SetBrush( *wxWHITE_BRUSH );
127 dc.DrawRectangle( 20, 20, 60, 60 );
60a41aee
RR
128 dc.SelectObject( wxNullBitmap );
129
130 // try to find the directory with our images
131 wxString dir;
132 if ( wxFile::Exists("./horse.png") )
133 dir = "./";
134 else if ( wxFile::Exists("../horse.png") )
135 dir = "../";
136 else
137 wxLogWarning("Can't find image files in either '.' or '..'!");
138
139 wxImage image( bitmap );
140
bea56879 141#if wxUSE_LIBPNG
60a41aee
RR
142 if ( !image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ) )
143 wxLogError("Can't save file");
144
145 if ( !image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG ) )
146 wxLogError("Can't load PNG image");
147 else
148 my_horse_png = new wxBitmap( image.ConvertToBitmap() );
bea56879 149#endif // wxUSE_LIBPNG
60a41aee 150
bea56879 151#if wxUSE_LIBJPEG
60a41aee
RR
152 if ( !image.LoadFile( dir + wxString("horse.jpg") ) )
153 wxLogError("Can't load JPG image");
154 else
155 my_horse_jpeg = new wxBitmap( image.ConvertToBitmap() );
bea56879 156#endif // wxUSE_LIBJPEG
6e47faf1
JS
157
158#if wxUSE_GIF
60a41aee
RR
159 if ( !image.LoadFile( dir + wxString("horse.gif") ) )
160 wxLogError("Can't load GIF image");
161 else
162 my_horse_gif = new wxBitmap( image.ConvertToBitmap() );
6e47faf1 163#endif
cbd4be25 164
6e47faf1 165#if wxUSE_PCX
60a41aee
RR
166 if ( !image.LoadFile( dir + wxString("horse.pcx"), wxBITMAP_TYPE_PCX ) )
167 wxLogError("Can't load PCX image");
168 else
169 my_horse_pcx = new wxBitmap( image.ConvertToBitmap() );
6e47faf1 170#endif
cbd4be25 171
60a41aee
RR
172 if ( !image.LoadFile( dir + wxString("horse.bmp"), wxBITMAP_TYPE_BMP ) )
173 wxLogError("Can't load BMP image");
174 else
175 my_horse_bmp = new wxBitmap( image.ConvertToBitmap() );
6319afe3 176
6e47faf1 177#if wxUSE_PNM
60a41aee
RR
178 if ( !image.LoadFile( dir + wxString("horse.pnm"), wxBITMAP_TYPE_PNM ) )
179 wxLogError("Can't load PNM image");
180 else
181 my_horse_pnm = new wxBitmap( image.ConvertToBitmap() );
6e47faf1 182#endif
f048e32f 183
60a41aee
RR
184#if wxUSE_LIBTIFF
185 if ( !image.LoadFile( dir + wxString("horse.tif"), wxBITMAP_TYPE_TIF ) )
186 wxLogError("Can't load TIFF image");
187 else
188 my_horse_tiff = new wxBitmap( image.ConvertToBitmap() );
189#endif
190
191 image.LoadFile( dir + wxString("test.png") );
192 my_square = new wxBitmap( image.ConvertToBitmap() );
f048e32f 193
60a41aee 194 CreateAntiAliasedBitmap();
dc1dbfc6
RL
195
196 my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width,
197 smile_height, 1 );
01111366
RR
198}
199
1d5b7a0b 200MyCanvas::~MyCanvas()
01111366 201{
60a41aee
RR
202 delete my_horse_pnm;
203 delete my_horse_png;
204 delete my_horse_jpeg;
205 delete my_horse_gif;
206 delete my_horse_bmp;
207 delete my_horse_pcx;
208 delete my_horse_tiff;
dc1dbfc6 209 delete my_smile_xbm;
60a41aee
RR
210 delete my_square;
211 delete my_anti;
01111366
RR
212}
213
214void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
215{
be25e480
RR
216 wxPaintDC dc( this );
217 PrepareDC( dc );
a91b47e8 218
be25e480
RR
219 dc.DrawText( "Loaded image", 30, 10 );
220 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
f048e32f 221
be25e480
RR
222 dc.DrawText( "Drawn directly", 150, 10 );
223 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
cf3da716 224 dc.SetPen( *wxBLACK_PEN );
be25e480 225 dc.DrawRectangle( 150, 30, 100, 100 );
cf3da716
RR
226 dc.SetBrush( *wxWHITE_BRUSH );
227 dc.DrawRectangle( 170, 50, 60, 60 );
be25e480 228
408b4168 229 if (my_anti && my_anti->Ok())
be25e480
RR
230 dc.DrawBitmap( *my_anti, 280, 30 );
231
232 dc.DrawText( "PNG handler", 30, 135 );
233 if (my_horse_png && my_horse_png->Ok())
408b4168 234 {
be25e480
RR
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 );
240 }
241
242 dc.DrawText( "JPEG handler", 30, 365 );
408b4168 243 if (my_horse_jpeg && my_horse_jpeg->Ok())
be25e480
RR
244 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
245
246 dc.DrawText( "GIF handler", 30, 595 );
408b4168 247 if (my_horse_gif && my_horse_gif->Ok())
be25e480
RR
248 dc.DrawBitmap( *my_horse_gif, 30, 610 );
249
250 dc.DrawText( "PCX handler", 30, 825 );
408b4168 251 if (my_horse_pcx && my_horse_pcx->Ok())
be25e480
RR
252 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
253
254 dc.DrawText( "BMP handler", 30, 1055 );
408b4168 255 if (my_horse_bmp && my_horse_bmp->Ok())
be25e480
RR
256 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
257
258 dc.DrawText( "PNM handler", 30, 1285 );
408b4168 259 if (my_horse_pnm && my_horse_pnm->Ok())
be25e480 260 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
408b4168 261
be25e480 262 dc.DrawText( "TIFF handler", 30, 1515 );
408b4168 263 if (my_horse_tiff && my_horse_tiff->Ok())
4b05d973 264 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
be25e480 265
408b4168 266 if (my_smile_xbm && my_smile_xbm->Ok())
be25e480
RR
267 {
268 dc.DrawText( "XBM bitmap", 30, 1745 );
82ea63e6
RR
269 dc.SetTextForeground( "RED" );
270 dc.SetTextBackground( "GREEN" );
be25e480 271 dc.DrawBitmap( *my_smile_xbm, 30, 1760 );
408b4168 272
be25e480
RR
273 dc.DrawText( "After wxImage conversion", 150, 1745 );
274 wxImage i( *my_smile_xbm );
408b4168
VZ
275 i.SetMaskColour( 0,0,0 );
276 i.Replace( 255,255,255,
be25e480
RR
277 wxRED_PEN->GetColour().Red(),
278 wxRED_PEN->GetColour().Green(),
279 wxRED_PEN->GetColour().Blue() );
280 dc.DrawBitmap( i.ConvertToBitmap(), 150, 1760, TRUE );
281 }
82ea63e6 282 dc.SetTextForeground( "BLACK" );
408b4168 283
41fbc841 284 wxBitmap mono( 60,50,1 );
82ea63e6
RR
285 wxMemoryDC memdc;
286 memdc.SelectObject( mono );
287 memdc.SetPen( *wxTRANSPARENT_PEN );
82ea63e6 288 memdc.SetBrush( *wxWHITE_BRUSH );
41fbc841
RR
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 );
82ea63e6 296 memdc.SelectObject( wxNullBitmap );
408b4168 297 if (mono.Ok())
82ea63e6
RR
298 {
299 dc.DrawText( "Mono bitmap", 30, 1845 );
300 dc.SetTextForeground( "RED" );
301 dc.SetTextBackground( "GREEN" );
302 dc.DrawBitmap( mono, 30, 1860 );
408b4168 303
bea56879 304 dc.SetTextForeground( "BLACK" );
82ea63e6 305 dc.DrawText( "After wxImage conversion", 150, 1845 );
bea56879 306 dc.SetTextForeground( "RED" );
82ea63e6 307 wxImage i( mono );
408b4168
VZ
308 i.SetMaskColour( 255,255,255 );
309 i.Replace( 0,0,0,
82ea63e6
RR
310 wxRED_PEN->GetColour().Red(),
311 wxRED_PEN->GetColour().Green(),
312 wxRED_PEN->GetColour().Blue() );
313 dc.DrawBitmap( i.ConvertToBitmap(), 150, 1860, TRUE );
bea56879
VZ
314 dc.SetTextForeground( "BLACK" );
315 }
316
317 dc.DrawText("XPM bitmap", 30, 1950);
318 if ( m_bmpSmileXpm.Ok() )
319 {
320 dc.DrawBitmap(m_bmpSmileXpm, 30, 1975, TRUE);
321 }
322
323 dc.DrawText("XPM icon", 150, 1950);
324 if ( m_iconSmileXpm.Ok() )
325 {
326 dc.DrawIcon(m_iconSmileXpm, 150, 1975);
82ea63e6 327 }
9390a202 328}
1d5b7a0b 329
9390a202
RR
330void MyCanvas::CreateAntiAliasedBitmap()
331{
332 wxBitmap bitmap( 300, 300 );
a91b47e8 333
9390a202 334 wxMemoryDC dc;
a91b47e8 335
9390a202 336 dc.SelectObject( bitmap );
a91b47e8 337
9390a202 338 dc.Clear();
f048e32f 339
6e47faf1 340 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
9390a202
RR
341 dc.SetTextForeground( "RED" );
342 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
343 dc.DrawText( "And a Rectangle.", 20, 60 );
f048e32f 344
9390a202 345 dc.SetBrush( *wxRED_BRUSH );
91b8de8d 346 dc.SetPen( *wxTRANSPARENT_PEN );
9390a202 347 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
f048e32f 348
9390a202
RR
349 wxImage original( bitmap );
350 wxImage anti( 150, 150 );
a91b47e8 351
9390a202 352 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
f048e32f 353
9390a202
RR
354 for (int y = 1; y < 149; y++)
355 for (int x = 1; x < 149; x++)
356 {
357 int red = original.GetRed( x*2, y*2 ) +
f048e32f
VZ
358 original.GetRed( x*2-1, y*2 ) +
359 original.GetRed( x*2, y*2+1 ) +
9390a202 360 original.GetRed( x*2+1, y*2+1 );
f048e32f
VZ
361 red = red/4;
362
9390a202 363 int green = original.GetGreen( x*2, y*2 ) +
f048e32f
VZ
364 original.GetGreen( x*2-1, y*2 ) +
365 original.GetGreen( x*2, y*2+1 ) +
9390a202 366 original.GetGreen( x*2+1, y*2+1 );
f048e32f
VZ
367 green = green/4;
368
9390a202 369 int blue = original.GetBlue( x*2, y*2 ) +
f048e32f
VZ
370 original.GetBlue( x*2-1, y*2 ) +
371 original.GetBlue( x*2, y*2+1 ) +
9390a202 372 original.GetBlue( x*2+1, y*2+1 );
f048e32f 373 blue = blue/4;
9390a202
RR
374 anti.SetRGB( x, y, red, green, blue );
375 }
376 my_anti = new wxBitmap( anti.ConvertToBitmap() );
01111366
RR
377}
378
379// MyFrame
380
79490c3d
VZ
381const int ID_QUIT = 108;
382const int ID_ABOUT = 109;
01111366
RR
383
384IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
385
386BEGIN_EVENT_TABLE(MyFrame,wxFrame)
387 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
388 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
389END_EVENT_TABLE()
390
1d5b7a0b
VZ
391MyFrame::MyFrame()
392 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
393 wxPoint(20,20), wxSize(470,360) )
01111366
RR
394{
395 wxMenu *file_menu = new wxMenu();
90e58684
RR
396 file_menu->Append( ID_ABOUT, "&About..");
397 file_menu->Append( ID_QUIT, "E&xit");
1d5b7a0b 398
01111366 399 wxMenuBar *menu_bar = new wxMenuBar();
90e58684 400 menu_bar->Append(file_menu, "&File");
3d05544e 401
01111366 402 SetMenuBar( menu_bar );
1d5b7a0b 403
917e533b
RR
404 CreateStatusBar(2);
405 int widths[] = { -1, 100 };
406 SetStatusWidths( 2, widths );
1d5b7a0b 407
fb1585ae 408 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
cbd4be25 409
bea56879
VZ
410 // 500 width * 2100 height
411 m_canvas->SetScrollbars( 10, 10, 50, 210 );
01111366
RR
412}
413
414void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
415{
416 Close( TRUE );
417}
418
419void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
420{
1d5b7a0b 421 (void)wxMessageBox( "wxImage demo\n"
41fbc841 422 "Robert Roebling (c) 1998,2000",
1d5b7a0b 423 "About wxImage Demo", wxICON_INFORMATION | wxOK );
fb1585ae
RR
424}
425
01111366
RR
426//-----------------------------------------------------------------------------
427// MyApp
428//-----------------------------------------------------------------------------
429
1d5b7a0b 430bool MyApp::OnInit()
01111366 431{
e9c4b1a2
JS
432#if wxUSE_LIBPNG
433 wxImage::AddHandler( new wxPNGHandler );
434#endif
435
436#if wxUSE_LIBJPEG
437 wxImage::AddHandler( new wxJPEGHandler );
438#endif
439
60a41aee
RR
440#if wxUSE_LIBTIFF
441 wxImage::AddHandler( new wxTIFFHandler );
442#endif
443
6e47faf1 444#if wxUSE_GIF
e1929140 445 wxImage::AddHandler( new wxGIFHandler );
6e47faf1
JS
446#endif
447
448#if wxUSE_PCX
cbd4be25 449 wxImage::AddHandler( new wxPCXHandler );
6e47faf1
JS
450#endif
451
452#if wxUSE_PNM
6319afe3 453 wxImage::AddHandler( new wxPNMHandler );
6e47faf1 454#endif
e1929140 455
01111366
RR
456 wxFrame *frame = new MyFrame();
457 frame->Show( TRUE );
1d5b7a0b 458
01111366
RR
459 return TRUE;
460}
461