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