]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
SN: Added suitable includes for EMX (#ifdef'ed __EMX__),
[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
RR
218
219 if (my_anti && my_anti->Ok())
220 dc.DrawBitmap( *my_anti, 280, 30 );
221
222 dc.DrawText( "PNG handler", 30, 135 );
223 if (my_horse_png && my_horse_png->Ok())
224 {
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 );
233 if (my_horse_jpeg && my_horse_jpeg->Ok())
234 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
235
236 dc.DrawText( "GIF handler", 30, 595 );
237 if (my_horse_gif && my_horse_gif->Ok())
238 dc.DrawBitmap( *my_horse_gif, 30, 610 );
239
240 dc.DrawText( "PCX handler", 30, 825 );
241 if (my_horse_pcx && my_horse_pcx->Ok())
242 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
243
244 dc.DrawText( "BMP handler", 30, 1055 );
245 if (my_horse_bmp && my_horse_bmp->Ok())
246 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
247
248 dc.DrawText( "PNM handler", 30, 1285 );
249 if (my_horse_pnm && my_horse_pnm->Ok())
250 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
60a41aee 251
be25e480
RR
252 dc.DrawText( "TIFF handler", 30, 1515 );
253 if (my_horse_tiff && my_horse_tiff->Ok())
4b05d973 254 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
be25e480
RR
255
256 if (my_smile_xbm && my_smile_xbm->Ok())
257 {
258 dc.DrawText( "XBM bitmap", 30, 1745 );
82ea63e6
RR
259 dc.SetTextForeground( "RED" );
260 dc.SetTextBackground( "GREEN" );
be25e480
RR
261 dc.DrawBitmap( *my_smile_xbm, 30, 1760 );
262
263 dc.DrawText( "After wxImage conversion", 150, 1745 );
264 wxImage i( *my_smile_xbm );
82ea63e6
RR
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
RR
272 dc.SetTextForeground( "BLACK" );
273
274 wxBitmap mono( 30,30,1 );
275 wxMemoryDC memdc;
276 memdc.SelectObject( mono );
277 memdc.SetPen( *wxTRANSPARENT_PEN );
278 memdc.SetBrush( *wxBLACK_BRUSH );
279 memdc.DrawRectangle( 0,0,30,30 );
280 memdc.SetBrush( *wxWHITE_BRUSH );
281 memdc.DrawRectangle( 5,5,20,20 );
282 memdc.SelectObject( wxNullBitmap );
283 if (mono.Ok())
284 {
285 dc.DrawText( "Mono bitmap", 30, 1845 );
286 dc.SetTextForeground( "RED" );
287 dc.SetTextBackground( "GREEN" );
288 dc.DrawBitmap( mono, 30, 1860 );
289
290 dc.DrawText( "After wxImage conversion", 150, 1845 );
291 wxImage i( mono );
292 i.SetMaskColour( 0,0,0 );
293 i.Replace( 255,255,255,
294 wxRED_PEN->GetColour().Red(),
295 wxRED_PEN->GetColour().Green(),
296 wxRED_PEN->GetColour().Blue() );
297 dc.DrawBitmap( i.ConvertToBitmap(), 150, 1860, TRUE );
298 }
9390a202 299}
1d5b7a0b 300
9390a202
RR
301void MyCanvas::CreateAntiAliasedBitmap()
302{
303 wxBitmap bitmap( 300, 300 );
a91b47e8 304
9390a202 305 wxMemoryDC dc;
a91b47e8 306
9390a202 307 dc.SelectObject( bitmap );
a91b47e8 308
9390a202 309 dc.Clear();
f048e32f 310
6e47faf1 311 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
9390a202
RR
312 dc.SetTextForeground( "RED" );
313 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
314 dc.DrawText( "And a Rectangle.", 20, 60 );
f048e32f 315
9390a202 316 dc.SetBrush( *wxRED_BRUSH );
91b8de8d 317 dc.SetPen( *wxTRANSPARENT_PEN );
9390a202 318 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
f048e32f 319
9390a202
RR
320 wxImage original( bitmap );
321 wxImage anti( 150, 150 );
a91b47e8 322
9390a202 323 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
f048e32f 324
9390a202
RR
325 for (int y = 1; y < 149; y++)
326 for (int x = 1; x < 149; x++)
327 {
328 int red = original.GetRed( x*2, y*2 ) +
f048e32f
VZ
329 original.GetRed( x*2-1, y*2 ) +
330 original.GetRed( x*2, y*2+1 ) +
9390a202 331 original.GetRed( x*2+1, y*2+1 );
f048e32f
VZ
332 red = red/4;
333
9390a202 334 int green = original.GetGreen( x*2, y*2 ) +
f048e32f
VZ
335 original.GetGreen( x*2-1, y*2 ) +
336 original.GetGreen( x*2, y*2+1 ) +
9390a202 337 original.GetGreen( x*2+1, y*2+1 );
f048e32f
VZ
338 green = green/4;
339
9390a202 340 int blue = original.GetBlue( x*2, y*2 ) +
f048e32f
VZ
341 original.GetBlue( x*2-1, y*2 ) +
342 original.GetBlue( x*2, y*2+1 ) +
9390a202 343 original.GetBlue( x*2+1, y*2+1 );
f048e32f 344 blue = blue/4;
9390a202
RR
345 anti.SetRGB( x, y, red, green, blue );
346 }
347 my_anti = new wxBitmap( anti.ConvertToBitmap() );
01111366
RR
348}
349
350// MyFrame
351
79490c3d
VZ
352const int ID_QUIT = 108;
353const int ID_ABOUT = 109;
01111366
RR
354
355IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
356
357BEGIN_EVENT_TABLE(MyFrame,wxFrame)
358 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
359 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
360END_EVENT_TABLE()
361
1d5b7a0b
VZ
362MyFrame::MyFrame()
363 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
364 wxPoint(20,20), wxSize(470,360) )
01111366
RR
365{
366 wxMenu *file_menu = new wxMenu();
90e58684
RR
367 file_menu->Append( ID_ABOUT, "&About..");
368 file_menu->Append( ID_QUIT, "E&xit");
1d5b7a0b 369
01111366 370 wxMenuBar *menu_bar = new wxMenuBar();
90e58684 371 menu_bar->Append(file_menu, "&File");
3d05544e 372
01111366 373 SetMenuBar( menu_bar );
1d5b7a0b 374
917e533b
RR
375 CreateStatusBar(2);
376 int widths[] = { -1, 100 };
377 SetStatusWidths( 2, widths );
1d5b7a0b 378
fb1585ae 379 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
cbd4be25 380
dc1dbfc6 381 // 500 width * 1900 height
82ea63e6 382 m_canvas->SetScrollbars( 10, 10, 50, 200 );
01111366
RR
383}
384
385void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
386{
387 Close( TRUE );
388}
389
390void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
391{
1d5b7a0b 392 (void)wxMessageBox( "wxImage demo\n"
8a5137d7 393 "\n"
1d5b7a0b
VZ
394 "Robert Roebling (c) 1998",
395 "About wxImage Demo", wxICON_INFORMATION | wxOK );
fb1585ae
RR
396}
397
01111366
RR
398//-----------------------------------------------------------------------------
399// MyApp
400//-----------------------------------------------------------------------------
401
1d5b7a0b 402bool MyApp::OnInit()
01111366 403{
e9c4b1a2
JS
404#if wxUSE_LIBPNG
405 wxImage::AddHandler( new wxPNGHandler );
406#endif
407
408#if wxUSE_LIBJPEG
409 wxImage::AddHandler( new wxJPEGHandler );
410#endif
411
60a41aee
RR
412#if wxUSE_LIBTIFF
413 wxImage::AddHandler( new wxTIFFHandler );
414#endif
415
6e47faf1 416#if wxUSE_GIF
e1929140 417 wxImage::AddHandler( new wxGIFHandler );
6e47faf1
JS
418#endif
419
420#if wxUSE_PCX
cbd4be25 421 wxImage::AddHandler( new wxPCXHandler );
6e47faf1
JS
422#endif
423
424#if wxUSE_PNM
6319afe3 425 wxImage::AddHandler( new wxPNMHandler );
6e47faf1 426#endif
e1929140 427
01111366
RR
428 wxFrame *frame = new MyFrame();
429 frame->Show( TRUE );
1d5b7a0b 430
01111366
RR
431 return TRUE;
432}
433