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