]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/image/image.cpp
updated opengl
[wxWidgets.git] / samples / image / image.cpp
... / ...
CommitLineData
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#include "wx/file.h"
23#include "wx/mstream.h"
24#include "wx/wfstream.h"
25
26#include "smile.xbm"
27#include "smile.xpm"
28
29
30// derived classes
31
32class MyFrame;
33class MyApp;
34
35// MyCanvas
36
37class MyCanvas: public wxScrolledWindow
38{
39public:
40 MyCanvas() {};
41 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
42 ~MyCanvas();
43 void OnPaint( wxPaintEvent &event );
44 void CreateAntiAliasedBitmap();
45
46 wxBitmap *my_horse_png;
47 wxBitmap *my_horse_jpeg;
48 wxBitmap *my_horse_gif;
49 wxBitmap *my_horse_bmp;
50 wxBitmap *my_horse_pcx;
51 wxBitmap *my_horse_pnm;
52 wxBitmap *my_horse_tiff;
53 wxBitmap *my_horse_xpm;
54
55 wxBitmap *my_smile_xbm;
56 wxBitmap *my_square;
57 wxBitmap *my_anti;
58
59protected:
60 wxBitmap m_bmpSmileXpm;
61 wxIcon m_iconSmileXpm;
62
63private:
64 DECLARE_DYNAMIC_CLASS(MyCanvas)
65 DECLARE_EVENT_TABLE()
66};
67
68// MyFrame
69
70class MyFrame: public wxFrame
71{
72public:
73 MyFrame();
74
75 void OnAbout( wxCommandEvent &event );
76 void OnNewFrame( wxCommandEvent &event );
77 void OnQuit( wxCommandEvent &event );
78
79 MyCanvas *m_canvas;
80
81private:
82 DECLARE_DYNAMIC_CLASS(MyFrame)
83 DECLARE_EVENT_TABLE()
84};
85
86class MyImageFrame : public wxFrame
87{
88public:
89 MyImageFrame(wxFrame *parent, const wxBitmap& bitmap)
90 : wxFrame(parent, -1, _T("Frame with image"),
91 wxDefaultPosition, wxDefaultSize,
92 wxCAPTION | wxSYSTEM_MENU),
93 m_bitmap(bitmap)
94 {
95 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
96 }
97
98 void OnPaint(wxPaintEvent& WXUNUSED(event))
99 {
100 wxPaintDC dc( this );
101 dc.DrawBitmap( m_bitmap, 0, 0 );
102 }
103
104private:
105 wxBitmap m_bitmap;
106
107 DECLARE_EVENT_TABLE()
108};
109
110// MyApp
111
112class MyApp: public wxApp
113{
114public:
115 virtual bool OnInit();
116};
117
118// main program
119
120IMPLEMENT_APP(MyApp)
121
122// MyCanvas
123
124IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
125
126BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
127 EVT_PAINT(MyImageFrame::OnPaint)
128END_EVENT_TABLE()
129
130BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
131 EVT_PAINT(MyCanvas::OnPaint)
132END_EVENT_TABLE()
133
134MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
135 const wxPoint &pos, const wxSize &size )
136 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
137#if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
138 , m_bmpSmileXpm((const char **) smile_xpm)
139 , m_iconSmileXpm((const char **) smile_xpm)
140#endif
141{
142 my_horse_png = (wxBitmap*) NULL;
143 my_horse_jpeg = (wxBitmap*) NULL;
144 my_horse_gif = (wxBitmap*) NULL;
145 my_horse_bmp = (wxBitmap*) NULL;
146 my_horse_pcx = (wxBitmap*) NULL;
147 my_horse_pnm = (wxBitmap*) NULL;
148 my_horse_tiff = (wxBitmap*) NULL;
149 my_horse_xpm = (wxBitmap*) NULL;
150 my_smile_xbm = (wxBitmap*) NULL;
151 my_square = (wxBitmap*) NULL;
152 my_anti = (wxBitmap*) NULL;
153
154 SetBackgroundColour(* wxWHITE);
155
156 wxBitmap bitmap( 100, 100 );
157
158 wxMemoryDC dc;
159 dc.SelectObject( bitmap );
160 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
161 dc.SetPen( *wxBLACK_PEN );
162 dc.DrawRectangle( 0, 0, 100, 100 );
163 dc.SetBrush( *wxWHITE_BRUSH );
164 dc.DrawRectangle( 20, 20, 60, 60 );
165 dc.SelectObject( wxNullBitmap );
166
167 // try to find the directory with our images
168 wxString dir;
169 if ( wxFile::Exists("./horse.png") )
170 dir = "./";
171 else if ( wxFile::Exists("../horse.png") )
172 dir = "../";
173 else
174 wxLogWarning("Can't find image files in either '.' or '..'!");
175
176 wxImage image = bitmap.ConvertToImage();
177
178#if wxUSE_LIBPNG
179 if ( !image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ))
180 wxLogError("Can't save file");
181
182 image.Destroy();
183
184 image.LoadFile( dir + wxString("test.png") );
185 my_square = new wxBitmap( image );
186
187 image.Destroy();
188
189 if ( !image.LoadFile( dir + wxString("horse.png")) )
190 wxLogError("Can't load PNG image");
191 else
192 my_horse_png = new wxBitmap( image );
193#endif // wxUSE_LIBPNG
194
195#if wxUSE_LIBJPEG
196 image.Destroy();
197
198 if ( !image.LoadFile( dir + wxString("horse.jpg")) )
199 wxLogError("Can't load JPG image");
200 else
201 my_horse_jpeg = new wxBitmap( image );
202#endif // wxUSE_LIBJPEG
203
204#if wxUSE_GIF
205 image.Destroy();
206
207 if ( !image.LoadFile( dir + wxString("horse.gif")) )
208 wxLogError("Can't load GIF image");
209 else
210 my_horse_gif = new wxBitmap( image );
211#endif
212
213#if wxUSE_PCX
214 image.Destroy();
215
216 if ( !image.LoadFile( dir + wxString("horse.pcx"), wxBITMAP_TYPE_PCX ) )
217 wxLogError("Can't load PCX image");
218 else
219 my_horse_pcx = new wxBitmap( image );
220#endif
221
222 image.Destroy();
223
224 if ( !image.LoadFile( dir + wxString("horse.bmp"), wxBITMAP_TYPE_BMP ) )
225 wxLogError("Can't load BMP image");
226 else
227 my_horse_bmp = new wxBitmap( image );
228
229#if wxUSE_XPM
230 image.Destroy();
231
232 if ( !image.LoadFile( dir + wxString("horse.xpm"), wxBITMAP_TYPE_XPM ) )
233 wxLogError("Can't load XPM image");
234 else
235 my_horse_xpm = new wxBitmap( image );
236
237 if ( !image.SaveFile( dir + wxString("test.xpm"), wxBITMAP_TYPE_XPM ))
238 wxLogError("Can't save file");
239#endif
240
241#if wxUSE_PNM
242 image.Destroy();
243
244 if ( !image.LoadFile( dir + wxString("horse.pnm"), wxBITMAP_TYPE_PNM ) )
245 wxLogError("Can't load PNM image");
246 else
247 my_horse_pnm = new wxBitmap( image );
248#endif
249
250#if wxUSE_LIBTIFF
251 image.Destroy();
252
253 if ( !image.LoadFile( dir + wxString("horse.tif"), wxBITMAP_TYPE_TIF ) )
254 wxLogError("Can't load TIFF image");
255 else
256 my_horse_tiff = new wxBitmap( image );
257#endif
258
259 CreateAntiAliasedBitmap();
260
261 my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width,
262 smile_height, 1 );
263
264#if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
265 // demonstrates XPM automatically using the mask when saving
266 if ( m_bmpSmileXpm.Ok() )
267 m_bmpSmileXpm.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM);
268#endif
269}
270
271MyCanvas::~MyCanvas()
272{
273 delete my_horse_pnm;
274 delete my_horse_png;
275 delete my_horse_jpeg;
276 delete my_horse_gif;
277 delete my_horse_bmp;
278 delete my_horse_pcx;
279 delete my_horse_tiff;
280 delete my_horse_xpm;
281 delete my_smile_xbm;
282 delete my_square;
283 delete my_anti;
284}
285
286void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
287{
288 wxPaintDC dc( this );
289 PrepareDC( dc );
290
291 dc.DrawText( "Loaded image", 30, 10 );
292 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
293
294 dc.DrawText( "Drawn directly", 150, 10 );
295 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
296 dc.SetPen( *wxBLACK_PEN );
297 dc.DrawRectangle( 150, 30, 100, 100 );
298 dc.SetBrush( *wxWHITE_BRUSH );
299 dc.DrawRectangle( 170, 50, 60, 60 );
300
301 if (my_anti && my_anti->Ok())
302 dc.DrawBitmap( *my_anti, 280, 30 );
303
304 dc.DrawText( "PNG handler", 30, 135 );
305 if (my_horse_png && my_horse_png->Ok())
306 {
307 dc.DrawBitmap( *my_horse_png, 30, 150 );
308 wxRect rect(0,0,100,100);
309 wxBitmap sub( my_horse_png->GetSubBitmap(rect) );
310 dc.DrawText( "GetSubBitmap()", 280, 190 );
311 dc.DrawBitmap( sub, 280, 210 );
312 }
313
314 dc.DrawText( "JPEG handler", 30, 365 );
315 if (my_horse_jpeg && my_horse_jpeg->Ok())
316 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
317
318 dc.DrawText( "GIF handler", 30, 595 );
319 if (my_horse_gif && my_horse_gif->Ok())
320 dc.DrawBitmap( *my_horse_gif, 30, 610 );
321
322 dc.DrawText( "PCX handler", 30, 825 );
323 if (my_horse_pcx && my_horse_pcx->Ok())
324 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
325
326 dc.DrawText( "BMP handler", 30, 1055 );
327 if (my_horse_bmp && my_horse_bmp->Ok())
328 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
329
330 dc.DrawText( "PNM handler", 30, 1285 );
331 if (my_horse_pnm && my_horse_pnm->Ok())
332 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
333
334 dc.DrawText( "TIFF handler", 30, 1515 );
335 if (my_horse_tiff && my_horse_tiff->Ok())
336 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
337
338 dc.DrawText( "XPM handler", 30, 1745 );
339 if (my_horse_xpm && my_horse_xpm->Ok())
340 dc.DrawBitmap( *my_horse_xpm, 30, 1760 );
341
342 if (my_smile_xbm && my_smile_xbm->Ok())
343 {
344 dc.DrawText( "XBM bitmap", 30, 1975 );
345 dc.DrawText( "(green on red)", 30, 1990 );
346 dc.SetTextForeground( "GREEN" );
347 dc.SetTextBackground( "RED" );
348 dc.DrawBitmap( *my_smile_xbm, 30, 2010 );
349
350 dc.SetTextForeground( "BLACK" );
351 dc.DrawText( "After wxImage conversion", 150, 1975 );
352 dc.DrawText( "(red on white)", 150, 1990 );
353 dc.SetTextForeground( "RED" );
354 wxImage i = my_smile_xbm->ConvertToImage();
355 i.SetMaskColour( 255, 255, 255 );
356 i.Replace( 0, 0, 0,
357 wxRED_PEN->GetColour().Red(),
358 wxRED_PEN->GetColour().Green(),
359 wxRED_PEN->GetColour().Blue() );
360 dc.DrawBitmap( i.ConvertToBitmap(), 150, 2010, TRUE );
361 dc.SetTextForeground( "BLACK" );
362 }
363
364
365 wxBitmap mono( 60,50,1 );
366 wxMemoryDC memdc;
367 memdc.SelectObject( mono );
368 memdc.SetPen( *wxBLACK_PEN );
369 memdc.SetBrush( *wxWHITE_BRUSH );
370 memdc.DrawRectangle( 0,0,60,50 );
371 memdc.SetTextForeground( *wxBLACK );
372 memdc.DrawText( "Hi!", 5, 5 );
373 memdc.SetBrush( *wxBLACK_BRUSH );
374 memdc.DrawRectangle( 33,5,20,20 );
375 memdc.SetPen( *wxRED_PEN );
376 memdc.DrawLine( 5, 42, 50, 42 );
377 memdc.SelectObject( wxNullBitmap );
378
379 if (mono.Ok())
380 {
381 dc.DrawText( "Mono bitmap", 30, 2095 );
382 dc.DrawText( "(red on green)", 30, 2110 );
383 dc.SetTextForeground( "RED" );
384 dc.SetTextBackground( "GREEN" );
385 dc.DrawBitmap( mono, 30, 2130 );
386
387 dc.SetTextForeground( "BLACK" );
388 dc.DrawText( "After wxImage conversion", 150, 2095 );
389 dc.DrawText( "(red on white)", 150, 2110 );
390 dc.SetTextForeground( "RED" );
391 wxImage i = mono.ConvertToImage();
392 i.SetMaskColour( 255,255,255 );
393 i.Replace( 0,0,0,
394 wxRED_PEN->GetColour().Red(),
395 wxRED_PEN->GetColour().Green(),
396 wxRED_PEN->GetColour().Blue() );
397 dc.DrawBitmap( i.ConvertToBitmap(), 150, 2130, TRUE );
398 dc.SetTextForeground( "BLACK" );
399 }
400
401 dc.DrawText("XPM bitmap", 30, 2230);
402 if ( m_bmpSmileXpm.Ok() )
403 {
404 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, TRUE);
405 }
406
407 dc.DrawText("XPM icon", 150, 2230);
408 if ( m_iconSmileXpm.Ok() )
409 {
410 dc.DrawIcon(m_iconSmileXpm, 150, 2250);
411 }
412}
413
414void MyCanvas::CreateAntiAliasedBitmap()
415{
416 wxBitmap bitmap( 300, 300 );
417
418 wxMemoryDC dc;
419
420 dc.SelectObject( bitmap );
421
422 dc.Clear();
423
424 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
425 dc.SetTextForeground( "RED" );
426 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
427 dc.DrawText( "And a Rectangle.", 20, 60 );
428
429 dc.SetBrush( *wxRED_BRUSH );
430 dc.SetPen( *wxTRANSPARENT_PEN );
431 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
432
433 wxImage original= bitmap.ConvertToImage();
434 wxImage anti( 150, 150 );
435
436 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
437
438 for (int y = 1; y < 149; y++)
439 for (int x = 1; x < 149; x++)
440 {
441 int red = original.GetRed( x*2, y*2 ) +
442 original.GetRed( x*2-1, y*2 ) +
443 original.GetRed( x*2, y*2+1 ) +
444 original.GetRed( x*2+1, y*2+1 );
445 red = red/4;
446
447 int green = original.GetGreen( x*2, y*2 ) +
448 original.GetGreen( x*2-1, y*2 ) +
449 original.GetGreen( x*2, y*2+1 ) +
450 original.GetGreen( x*2+1, y*2+1 );
451 green = green/4;
452
453 int blue = original.GetBlue( x*2, y*2 ) +
454 original.GetBlue( x*2-1, y*2 ) +
455 original.GetBlue( x*2, y*2+1 ) +
456 original.GetBlue( x*2+1, y*2+1 );
457 blue = blue/4;
458 anti.SetRGB( x, y, red, green, blue );
459 }
460 my_anti = new wxBitmap( anti.ConvertToBitmap() );
461}
462
463// MyFrame
464
465const int ID_QUIT = 108;
466const int ID_ABOUT = 109;
467const int ID_NEW = 110;
468
469IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
470
471BEGIN_EVENT_TABLE(MyFrame,wxFrame)
472 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
473 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
474 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
475END_EVENT_TABLE()
476
477MyFrame::MyFrame()
478 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
479 wxPoint(20,20), wxSize(470,360) )
480{
481 wxMenu *file_menu = new wxMenu();
482 file_menu->Append( ID_NEW, "&Show image...");
483 file_menu->AppendSeparator();
484 file_menu->Append( ID_ABOUT, "&About...");
485 file_menu->AppendSeparator();
486 file_menu->Append( ID_QUIT, "E&xit");
487
488 wxMenuBar *menu_bar = new wxMenuBar();
489 menu_bar->Append(file_menu, "&File");
490
491 SetMenuBar( menu_bar );
492
493 CreateStatusBar(2);
494 int widths[] = { -1, 100 };
495 SetStatusWidths( 2, widths );
496
497 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
498
499 // 500 width * 2100 height
500 m_canvas->SetScrollbars( 10, 10, 50, 220 );
501}
502
503void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
504{
505 Close( TRUE );
506}
507
508void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
509{
510 (void)wxMessageBox( "wxImage demo\n"
511 "Robert Roebling (c) 1998,2000",
512 "About wxImage Demo", wxICON_INFORMATION | wxOK );
513}
514
515void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
516{
517 wxString filename = wxFileSelector(_T("Select image file"));
518 if ( !filename )
519 return;
520
521 wxImage image;
522 if ( !image.LoadFile(filename) )
523 {
524 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
525
526 return;
527 }
528
529 (new MyImageFrame(this, image.ConvertToBitmap()))->Show();
530}
531
532//-----------------------------------------------------------------------------
533// MyApp
534//-----------------------------------------------------------------------------
535
536bool MyApp::OnInit()
537{
538#if wxUSE_LIBPNG
539 wxImage::AddHandler( new wxPNGHandler );
540#endif
541
542#if wxUSE_LIBJPEG
543 wxImage::AddHandler( new wxJPEGHandler );
544#endif
545
546#if wxUSE_LIBTIFF
547 wxImage::AddHandler( new wxTIFFHandler );
548#endif
549
550#if wxUSE_GIF
551 wxImage::AddHandler( new wxGIFHandler );
552#endif
553
554#if wxUSE_PCX
555 wxImage::AddHandler( new wxPCXHandler );
556#endif
557
558#if wxUSE_PNM
559 wxImage::AddHandler( new wxPNMHandler );
560#endif
561
562#if wxUSE_XPM
563 wxImage::AddHandler( new wxXPMHandler );
564#endif
565
566 wxFrame *frame = new MyFrame();
567 frame->Show( TRUE );
568
569 return TRUE;
570}
571