]> git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
d0d48d451d329ab023ac71db7e724e6503da9a28
[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 // derived classes
26
27 class MyFrame;
28 class MyApp;
29
30 // MyCanvas
31
32 class MyCanvas: public wxScrolledWindow
33 {
34 public:
35 MyCanvas() {};
36 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
37 ~MyCanvas();
38 void OnPaint( wxPaintEvent &event );
39 void CreateAntiAliasedBitmap();
40
41 wxBitmap *my_horse_png;
42 wxBitmap *my_horse_jpeg;
43 wxBitmap *my_horse_gif;
44 wxBitmap *my_horse_bmp;
45 wxBitmap *my_horse_pcx;
46 wxBitmap *my_square;
47 wxBitmap *my_anti;
48
49 DECLARE_DYNAMIC_CLASS(MyCanvas)
50 DECLARE_EVENT_TABLE()
51 };
52
53 // MyFrame
54
55 class MyFrame: public wxFrame
56 {
57 public:
58 MyFrame();
59
60 void OnAbout( wxCommandEvent &event );
61 void OnQuit( wxCommandEvent &event );
62
63 MyCanvas *m_canvas;
64
65 DECLARE_DYNAMIC_CLASS(MyFrame)
66 DECLARE_EVENT_TABLE()
67 };
68
69 // MyApp
70
71 class MyApp: public wxApp
72 {
73 public:
74 virtual bool OnInit();
75 };
76
77 // main program
78
79 IMPLEMENT_APP(MyApp)
80
81 // MyCanvas
82
83 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
84
85 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
86 EVT_PAINT(MyCanvas::OnPaint)
87 END_EVENT_TABLE()
88
89 MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
90 const wxPoint &pos, const wxSize &size )
91 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
92 {
93 my_horse_png = (wxBitmap*) NULL;
94 my_horse_jpeg = (wxBitmap*) NULL;
95 my_horse_gif = (wxBitmap*) NULL;
96 my_horse_bmp = (wxBitmap*) NULL;
97 my_horse_pcx = (wxBitmap*) NULL;
98 my_square = (wxBitmap*) NULL;
99 my_anti = (wxBitmap*) NULL;
100
101 SetBackgroundColour(* wxWHITE);
102
103 wxBitmap bitmap( 100, 100 );
104
105 wxMemoryDC dc;
106 dc.SelectObject( bitmap );
107 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
108 dc.SetPen( *wxWHITE_PEN );
109 dc.DrawRectangle( 0, 0, 100, 100 );
110 dc.SelectObject( wxNullBitmap );
111
112 // try to find the directory with our images
113 wxString dir;
114 if ( wxFile::Exists("./horse.png") )
115 dir = "./";
116 else if ( wxFile::Exists("../horse.png") )
117 dir = "../";
118 else
119 wxLogWarning("Can't find image files in either '.' or '..'!");
120
121 wxImage image( bitmap );
122
123 if ( !image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ) )
124 wxLogError("Can't save file");
125
126 if ( !image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG ) )
127 wxLogError("Can't load PNG image");
128 else
129 my_horse_png = new wxBitmap( image.ConvertToBitmap() );
130
131 if ( !image.LoadFile( dir + wxString("horse.jpg") ) )
132 wxLogError("Can't load JPG image");
133 else
134 my_horse_jpeg = new wxBitmap( image.ConvertToBitmap() );
135
136 if ( !image.LoadFile( dir + wxString("horse.gif") ) )
137 wxLogError("Can't load GIF image");
138 else
139 my_horse_gif = new wxBitmap( image.ConvertToBitmap() );
140
141 if ( !image.LoadFile( dir + wxString("horse.pcx"), wxBITMAP_TYPE_PCX ) )
142 wxLogError("Can't load PCX image");
143 else
144 my_horse_pcx = new wxBitmap( image.ConvertToBitmap() );
145
146 if ( !image.LoadFile( dir + wxString("horse.bmp"), wxBITMAP_TYPE_BMP ) )
147 wxLogError("Can't load BMP image");
148 else
149 my_horse_bmp = new wxBitmap( image.ConvertToBitmap() );
150
151 image.LoadFile( dir + wxString("test.png") );
152 my_square = new wxBitmap( image.ConvertToBitmap() );
153
154 CreateAntiAliasedBitmap();
155 }
156
157 MyCanvas::~MyCanvas()
158 {
159 delete my_horse_png;
160 delete my_horse_jpeg;
161 delete my_horse_gif;
162 delete my_horse_bmp;
163 delete my_horse_pcx;
164 delete my_square;
165 delete my_anti;
166 }
167
168 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
169 {
170 wxPaintDC dc( this );
171 PrepareDC( dc );
172
173 dc.DrawText( "Loaded image", 30, 10 );
174 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
175
176 dc.DrawText( "Drawn directly", 150, 10 );
177 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
178 dc.SetPen( *wxWHITE_PEN );
179 dc.DrawRectangle( 150, 30, 100, 100 );
180
181 if (my_anti && my_anti->Ok()) dc.DrawBitmap( *my_anti, 250, 140 );
182
183 dc.DrawText( "PNG handler", 30, 135 );
184 if (my_horse_png && my_horse_png->Ok()) dc.DrawBitmap( *my_horse_png, 30, 150 );
185
186 dc.DrawText( "JPEG handler", 30, 365 );
187 if (my_horse_jpeg && my_horse_jpeg->Ok()) dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
188
189 dc.DrawText( "GIF handler", 30, 595 );
190 if (my_horse_gif && my_horse_gif->Ok()) dc.DrawBitmap( *my_horse_gif, 30, 610 );
191
192 dc.DrawText( "PCX handler", 30, 825 );
193 if (my_horse_pcx && my_horse_pcx->Ok()) dc.DrawBitmap( *my_horse_pcx, 30, 840 );
194
195 dc.DrawText( "BMP handler", 30, 1055 );
196 if (my_horse_bmp && my_horse_bmp->Ok()) dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
197 }
198
199 void MyCanvas::CreateAntiAliasedBitmap()
200 {
201 wxBitmap bitmap( 300, 300 );
202
203 wxMemoryDC dc;
204
205 dc.SelectObject( bitmap );
206
207 dc.Clear();
208
209 dc.SetFont( wxFont( 24, wxDECORATIVE, wxDEFAULT, wxDEFAULT ) );
210 dc.SetTextForeground( "RED" );
211 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
212 dc.DrawText( "And a Rectangle.", 20, 60 );
213
214 dc.SetBrush( *wxRED_BRUSH );
215 dc.SetPen( *wxTRANSPARENT_PEN );
216 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
217
218 wxImage original( bitmap );
219 wxImage anti( 150, 150 );
220
221 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
222
223 for (int y = 1; y < 149; y++)
224 for (int x = 1; x < 149; x++)
225 {
226 int red = original.GetRed( x*2, y*2 ) +
227 original.GetRed( x*2-1, y*2 ) +
228 original.GetRed( x*2, y*2+1 ) +
229 original.GetRed( x*2+1, y*2+1 );
230 red = red/4;
231
232 int green = original.GetGreen( x*2, y*2 ) +
233 original.GetGreen( x*2-1, y*2 ) +
234 original.GetGreen( x*2, y*2+1 ) +
235 original.GetGreen( x*2+1, y*2+1 );
236 green = green/4;
237
238 int blue = original.GetBlue( x*2, y*2 ) +
239 original.GetBlue( x*2-1, y*2 ) +
240 original.GetBlue( x*2, y*2+1 ) +
241 original.GetBlue( x*2+1, y*2+1 );
242 blue = blue/4;
243 anti.SetRGB( x, y, red, green, blue );
244 }
245 my_anti = new wxBitmap( anti.ConvertToBitmap() );
246 }
247
248 // MyFrame
249
250 const int ID_QUIT = 108;
251 const int ID_ABOUT = 109;
252
253 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
254
255 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
256 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
257 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
258 END_EVENT_TABLE()
259
260 MyFrame::MyFrame()
261 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
262 wxPoint(20,20), wxSize(470,360) )
263 {
264 wxMenu *file_menu = new wxMenu();
265 file_menu->Append( ID_ABOUT, "&About..");
266 file_menu->Append( ID_QUIT, "E&xit");
267
268 wxMenuBar *menu_bar = new wxMenuBar();
269 menu_bar->Append(file_menu, "&File");
270
271 SetMenuBar( menu_bar );
272
273 CreateStatusBar(2);
274 int widths[] = { -1, 100 };
275 SetStatusWidths( 2, widths );
276
277 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
278
279 // 500 width * 1300 height
280 m_canvas->SetScrollbars( 10, 10, 50, 130 );
281 }
282
283 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
284 {
285 Close( TRUE );
286 }
287
288 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
289 {
290 (void)wxMessageBox( "wxImage demo\n"
291 "\n"
292 "Robert Roebling (c) 1998",
293 "About wxImage Demo", wxICON_INFORMATION | wxOK );
294 }
295
296 //-----------------------------------------------------------------------------
297 // MyApp
298 //-----------------------------------------------------------------------------
299
300 bool MyApp::OnInit()
301 {
302 #if wxUSE_LIBPNG
303 wxImage::AddHandler( new wxPNGHandler );
304 #endif
305
306 #if wxUSE_LIBJPEG
307 wxImage::AddHandler( new wxJPEGHandler );
308 #endif
309
310 wxImage::AddHandler( new wxGIFHandler );
311 wxImage::AddHandler( new wxPCXHandler );
312
313 wxFrame *frame = new MyFrame();
314 frame->Show( TRUE );
315
316 return TRUE;
317 }
318