]> git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
fixed false error report with helpfiles without index
[wxWidgets.git] / samples / image / image.cpp
1 /*
2 * Program: image
3 *
4 * Author: Robert Roebling
5 *
6 * Copyright: (C) 1998, Robert Roebling
7 *
8 */
9
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
18 #include "wx/wx.h"
19 #endif
20
21 #include "wx/image.h"
22
23 #include "wx/file.h"
24
25 #include "smile.xbm"
26 #include "smile.xpm"
27
28 // derived classes
29
30 class MyFrame;
31 class MyApp;
32
33 // MyCanvas
34
35 class MyCanvas: public wxScrolledWindow
36 {
37 public:
38 MyCanvas() {};
39 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
40 ~MyCanvas();
41 void OnPaint( wxPaintEvent &event );
42 void CreateAntiAliasedBitmap();
43
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;
52 wxBitmap *my_square;
53 wxBitmap *my_anti;
54
55 protected:
56 wxBitmap m_bmpSmileXpm;
57 wxIcon m_iconSmileXpm;
58
59 private:
60 DECLARE_DYNAMIC_CLASS(MyCanvas)
61 DECLARE_EVENT_TABLE()
62 };
63
64 // MyFrame
65
66 class MyFrame: public wxFrame
67 {
68 public:
69 MyFrame();
70
71 void OnAbout( wxCommandEvent &event );
72 void OnQuit( wxCommandEvent &event );
73
74 MyCanvas *m_canvas;
75
76 private:
77 DECLARE_DYNAMIC_CLASS(MyFrame)
78 DECLARE_EVENT_TABLE()
79 };
80
81 // MyApp
82
83 class MyApp: public wxApp
84 {
85 public:
86 virtual bool OnInit();
87 };
88
89 // main program
90
91 IMPLEMENT_APP(MyApp)
92
93 // MyCanvas
94
95 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
96
97 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
98 EVT_PAINT(MyCanvas::OnPaint)
99 END_EVENT_TABLE()
100
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(smile_xpm), m_iconSmileXpm(smile_xpm)
105 {
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;
113 my_smile_xbm = (wxBitmap*) NULL;
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 ) );
124 dc.SetPen( *wxBLACK_PEN );
125 dc.DrawRectangle( 0, 0, 100, 100 );
126 dc.SetBrush( *wxWHITE_BRUSH );
127 dc.DrawRectangle( 20, 20, 60, 60 );
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
141 #if wxUSE_LIBPNG
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() );
149 #endif // wxUSE_LIBPNG
150
151 #if wxUSE_LIBJPEG
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() );
156 #endif // wxUSE_LIBJPEG
157
158 #if wxUSE_GIF
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() );
163 #endif
164
165 #if wxUSE_PCX
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() );
170 #endif
171
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() );
176
177 #if wxUSE_PNM
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() );
182 #endif
183
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() );
193
194 CreateAntiAliasedBitmap();
195
196 my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width,
197 smile_height, 1 );
198 }
199
200 MyCanvas::~MyCanvas()
201 {
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;
209 delete my_smile_xbm;
210 delete my_square;
211 delete my_anti;
212 }
213
214 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
215 {
216 wxPaintDC dc( this );
217 PrepareDC( dc );
218
219 dc.DrawText( "Loaded image", 30, 10 );
220 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
221
222 dc.DrawText( "Drawn directly", 150, 10 );
223 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
224 dc.SetPen( *wxBLACK_PEN );
225 dc.DrawRectangle( 150, 30, 100, 100 );
226 dc.SetBrush( *wxWHITE_BRUSH );
227 dc.DrawRectangle( 170, 50, 60, 60 );
228
229 if (my_anti && my_anti->Ok())
230 dc.DrawBitmap( *my_anti, 280, 30 );
231
232 dc.DrawText( "PNG handler", 30, 135 );
233 if (my_horse_png && my_horse_png->Ok())
234 {
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 );
243 if (my_horse_jpeg && my_horse_jpeg->Ok())
244 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
245
246 dc.DrawText( "GIF handler", 30, 595 );
247 if (my_horse_gif && my_horse_gif->Ok())
248 dc.DrawBitmap( *my_horse_gif, 30, 610 );
249
250 dc.DrawText( "PCX handler", 30, 825 );
251 if (my_horse_pcx && my_horse_pcx->Ok())
252 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
253
254 dc.DrawText( "BMP handler", 30, 1055 );
255 if (my_horse_bmp && my_horse_bmp->Ok())
256 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
257
258 dc.DrawText( "PNM handler", 30, 1285 );
259 if (my_horse_pnm && my_horse_pnm->Ok())
260 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
261
262 dc.DrawText( "TIFF handler", 30, 1515 );
263 if (my_horse_tiff && my_horse_tiff->Ok())
264 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
265
266 if (my_smile_xbm && my_smile_xbm->Ok())
267 {
268 dc.DrawText( "XBM bitmap", 30, 1745 );
269 dc.SetTextForeground( "RED" );
270 dc.SetTextBackground( "GREEN" );
271 dc.DrawBitmap( *my_smile_xbm, 30, 1760 );
272
273 dc.DrawText( "After wxImage conversion", 150, 1745 );
274 wxImage i( *my_smile_xbm );
275 i.SetMaskColour( 0,0,0 );
276 i.Replace( 255,255,255,
277 wxRED_PEN->GetColour().Red(),
278 wxRED_PEN->GetColour().Green(),
279 wxRED_PEN->GetColour().Blue() );
280 dc.DrawBitmap( i.ConvertToBitmap(), 150, 1760, TRUE );
281 }
282 dc.SetTextForeground( "BLACK" );
283
284 wxBitmap mono( 60,50,1 );
285 wxMemoryDC memdc;
286 memdc.SelectObject( mono );
287 memdc.SetPen( *wxTRANSPARENT_PEN );
288 memdc.SetBrush( *wxWHITE_BRUSH );
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 );
296 memdc.SelectObject( wxNullBitmap );
297 if (mono.Ok())
298 {
299 dc.DrawText( "Mono bitmap", 30, 1845 );
300 dc.SetTextForeground( "RED" );
301 dc.SetTextBackground( "GREEN" );
302 dc.DrawBitmap( mono, 30, 1860 );
303
304 dc.SetTextForeground( "BLACK" );
305 dc.DrawText( "After wxImage conversion", 150, 1845 );
306 dc.SetTextForeground( "RED" );
307 wxImage i( mono );
308 i.SetMaskColour( 255,255,255 );
309 i.Replace( 0,0,0,
310 wxRED_PEN->GetColour().Red(),
311 wxRED_PEN->GetColour().Green(),
312 wxRED_PEN->GetColour().Blue() );
313 dc.DrawBitmap( i.ConvertToBitmap(), 150, 1860, TRUE );
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);
327 }
328 }
329
330 void MyCanvas::CreateAntiAliasedBitmap()
331 {
332 wxBitmap bitmap( 300, 300 );
333
334 wxMemoryDC dc;
335
336 dc.SelectObject( bitmap );
337
338 dc.Clear();
339
340 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
341 dc.SetTextForeground( "RED" );
342 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
343 dc.DrawText( "And a Rectangle.", 20, 60 );
344
345 dc.SetBrush( *wxRED_BRUSH );
346 dc.SetPen( *wxTRANSPARENT_PEN );
347 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
348
349 wxImage original( bitmap );
350 wxImage anti( 150, 150 );
351
352 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
353
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 ) +
358 original.GetRed( x*2-1, y*2 ) +
359 original.GetRed( x*2, y*2+1 ) +
360 original.GetRed( x*2+1, y*2+1 );
361 red = red/4;
362
363 int green = original.GetGreen( x*2, y*2 ) +
364 original.GetGreen( x*2-1, y*2 ) +
365 original.GetGreen( x*2, y*2+1 ) +
366 original.GetGreen( x*2+1, y*2+1 );
367 green = green/4;
368
369 int blue = original.GetBlue( x*2, y*2 ) +
370 original.GetBlue( x*2-1, y*2 ) +
371 original.GetBlue( x*2, y*2+1 ) +
372 original.GetBlue( x*2+1, y*2+1 );
373 blue = blue/4;
374 anti.SetRGB( x, y, red, green, blue );
375 }
376 my_anti = new wxBitmap( anti.ConvertToBitmap() );
377 }
378
379 // MyFrame
380
381 const int ID_QUIT = 108;
382 const int ID_ABOUT = 109;
383
384 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
385
386 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
387 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
388 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
389 END_EVENT_TABLE()
390
391 MyFrame::MyFrame()
392 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
393 wxPoint(20,20), wxSize(470,360) )
394 {
395 wxMenu *file_menu = new wxMenu();
396 file_menu->Append( ID_ABOUT, "&About..");
397 file_menu->Append( ID_QUIT, "E&xit");
398
399 wxMenuBar *menu_bar = new wxMenuBar();
400 menu_bar->Append(file_menu, "&File");
401
402 SetMenuBar( menu_bar );
403
404 CreateStatusBar(2);
405 int widths[] = { -1, 100 };
406 SetStatusWidths( 2, widths );
407
408 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
409
410 // 500 width * 2100 height
411 m_canvas->SetScrollbars( 10, 10, 50, 210 );
412 }
413
414 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
415 {
416 Close( TRUE );
417 }
418
419 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
420 {
421 (void)wxMessageBox( "wxImage demo\n"
422 "Robert Roebling (c) 1998,2000",
423 "About wxImage Demo", wxICON_INFORMATION | wxOK );
424 }
425
426 //-----------------------------------------------------------------------------
427 // MyApp
428 //-----------------------------------------------------------------------------
429
430 bool MyApp::OnInit()
431 {
432 #if wxUSE_LIBPNG
433 wxImage::AddHandler( new wxPNGHandler );
434 #endif
435
436 #if wxUSE_LIBJPEG
437 wxImage::AddHandler( new wxJPEGHandler );
438 #endif
439
440 #if wxUSE_LIBTIFF
441 wxImage::AddHandler( new wxTIFFHandler );
442 #endif
443
444 #if wxUSE_GIF
445 wxImage::AddHandler( new wxGIFHandler );
446 #endif
447
448 #if wxUSE_PCX
449 wxImage::AddHandler( new wxPCXHandler );
450 #endif
451
452 #if wxUSE_PNM
453 wxImage::AddHandler( new wxPNMHandler );
454 #endif
455
456 wxFrame *frame = new MyFrame();
457 frame->Show( TRUE );
458
459 return TRUE;
460 }
461