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