]>
Commit | Line | Data |
---|---|---|
01111366 RR |
1 | /* |
2 | * Program: image | |
1d5b7a0b | 3 | * |
01111366 RR |
4 | * Author: Robert Roebling |
5 | * | |
6 | * Copyright: (C) 1998, Robert Roebling | |
7 | * | |
8 | */ | |
9 | ||
3d05544e JS |
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 | |
01111366 | 18 | #include "wx/wx.h" |
3d05544e JS |
19 | #endif |
20 | ||
01111366 RR |
21 | #include "wx/image.h" |
22 | ||
a23fd0e1 VZ |
23 | #include "wx/file.h" |
24 | ||
dc1dbfc6 RL |
25 | #include "smile.xbm" |
26 | ||
01111366 RR |
27 | // derived classes |
28 | ||
29 | class MyFrame; | |
30 | class MyApp; | |
31 | ||
32 | // MyCanvas | |
33 | ||
34 | class MyCanvas: public wxScrolledWindow | |
35 | { | |
1d5b7a0b VZ |
36 | public: |
37 | MyCanvas() {}; | |
01111366 | 38 | MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size ); |
1d5b7a0b | 39 | ~MyCanvas(); |
01111366 | 40 | void OnPaint( wxPaintEvent &event ); |
9390a202 | 41 | void CreateAntiAliasedBitmap(); |
1d5b7a0b | 42 | |
e1929140 RR |
43 | wxBitmap *my_horse_png; |
44 | wxBitmap *my_horse_jpeg; | |
45 | wxBitmap *my_horse_gif; | |
ca26177c | 46 | wxBitmap *my_horse_bmp; |
cbd4be25 | 47 | wxBitmap *my_horse_pcx; |
6319afe3 | 48 | wxBitmap *my_horse_pnm; |
60a41aee | 49 | wxBitmap *my_horse_tiff; |
dc1dbfc6 | 50 | wxBitmap *my_smile_xbm; |
e8fdc264 | 51 | wxBitmap *my_square; |
9390a202 | 52 | wxBitmap *my_anti; |
1d5b7a0b | 53 | |
60a41aee | 54 | private: |
1d5b7a0b VZ |
55 | DECLARE_DYNAMIC_CLASS(MyCanvas) |
56 | DECLARE_EVENT_TABLE() | |
01111366 RR |
57 | }; |
58 | ||
59 | // MyFrame | |
60 | ||
61 | class MyFrame: public wxFrame | |
62 | { | |
1d5b7a0b VZ |
63 | public: |
64 | MyFrame(); | |
01111366 | 65 | |
01111366 RR |
66 | void OnAbout( wxCommandEvent &event ); |
67 | void OnQuit( wxCommandEvent &event ); | |
1d5b7a0b | 68 | |
01111366 | 69 | MyCanvas *m_canvas; |
1d5b7a0b | 70 | |
60a41aee | 71 | private: |
1d5b7a0b VZ |
72 | DECLARE_DYNAMIC_CLASS(MyFrame) |
73 | DECLARE_EVENT_TABLE() | |
01111366 RR |
74 | }; |
75 | ||
76 | // MyApp | |
77 | ||
78 | class MyApp: public wxApp | |
79 | { | |
1d5b7a0b VZ |
80 | public: |
81 | virtual bool OnInit(); | |
01111366 RR |
82 | }; |
83 | ||
84 | // main program | |
85 | ||
86 | IMPLEMENT_APP(MyApp) | |
87 | ||
88 | // MyCanvas | |
89 | ||
90 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) | |
91 | ||
1d5b7a0b VZ |
92 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) |
93 | EVT_PAINT(MyCanvas::OnPaint) | |
01111366 RR |
94 | END_EVENT_TABLE() |
95 | ||
acbd13a3 | 96 | MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, |
1d5b7a0b VZ |
97 | const wxPoint &pos, const wxSize &size ) |
98 | : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER ) | |
01111366 | 99 | { |
60a41aee RR |
100 | my_horse_png = (wxBitmap*) NULL; |
101 | my_horse_jpeg = (wxBitmap*) NULL; | |
102 | my_horse_gif = (wxBitmap*) NULL; | |
103 | my_horse_bmp = (wxBitmap*) NULL; | |
104 | my_horse_pcx = (wxBitmap*) NULL; | |
105 | my_horse_pnm = (wxBitmap*) NULL; | |
106 | my_horse_tiff = (wxBitmap*) NULL; | |
dc1dbfc6 | 107 | my_smile_xbm = (wxBitmap*) NULL; |
60a41aee RR |
108 | my_square = (wxBitmap*) NULL; |
109 | my_anti = (wxBitmap*) NULL; | |
110 | ||
111 | SetBackgroundColour(* wxWHITE); | |
112 | ||
113 | wxBitmap bitmap( 100, 100 ); | |
114 | ||
115 | wxMemoryDC dc; | |
116 | dc.SelectObject( bitmap ); | |
117 | dc.SetBrush( wxBrush( "orange", wxSOLID ) ); | |
118 | dc.SetPen( *wxWHITE_PEN ); | |
119 | dc.DrawRectangle( 0, 0, 100, 100 ); | |
120 | dc.SelectObject( wxNullBitmap ); | |
121 | ||
122 | // try to find the directory with our images | |
123 | wxString dir; | |
124 | if ( wxFile::Exists("./horse.png") ) | |
125 | dir = "./"; | |
126 | else if ( wxFile::Exists("../horse.png") ) | |
127 | dir = "../"; | |
128 | else | |
129 | wxLogWarning("Can't find image files in either '.' or '..'!"); | |
130 | ||
131 | wxImage image( bitmap ); | |
132 | ||
133 | if ( !image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ) ) | |
134 | wxLogError("Can't save file"); | |
135 | ||
136 | if ( !image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG ) ) | |
137 | wxLogError("Can't load PNG image"); | |
138 | else | |
139 | my_horse_png = new wxBitmap( image.ConvertToBitmap() ); | |
140 | ||
141 | if ( !image.LoadFile( dir + wxString("horse.jpg") ) ) | |
142 | wxLogError("Can't load JPG image"); | |
143 | else | |
144 | my_horse_jpeg = new wxBitmap( image.ConvertToBitmap() ); | |
6e47faf1 JS |
145 | |
146 | #if wxUSE_GIF | |
60a41aee RR |
147 | if ( !image.LoadFile( dir + wxString("horse.gif") ) ) |
148 | wxLogError("Can't load GIF image"); | |
149 | else | |
150 | my_horse_gif = new wxBitmap( image.ConvertToBitmap() ); | |
6e47faf1 | 151 | #endif |
cbd4be25 | 152 | |
6e47faf1 | 153 | #if wxUSE_PCX |
60a41aee RR |
154 | if ( !image.LoadFile( dir + wxString("horse.pcx"), wxBITMAP_TYPE_PCX ) ) |
155 | wxLogError("Can't load PCX image"); | |
156 | else | |
157 | my_horse_pcx = new wxBitmap( image.ConvertToBitmap() ); | |
6e47faf1 | 158 | #endif |
cbd4be25 | 159 | |
60a41aee RR |
160 | if ( !image.LoadFile( dir + wxString("horse.bmp"), wxBITMAP_TYPE_BMP ) ) |
161 | wxLogError("Can't load BMP image"); | |
162 | else | |
163 | my_horse_bmp = new wxBitmap( image.ConvertToBitmap() ); | |
6319afe3 | 164 | |
6e47faf1 | 165 | #if wxUSE_PNM |
60a41aee RR |
166 | if ( !image.LoadFile( dir + wxString("horse.pnm"), wxBITMAP_TYPE_PNM ) ) |
167 | wxLogError("Can't load PNM image"); | |
168 | else | |
169 | my_horse_pnm = new wxBitmap( image.ConvertToBitmap() ); | |
6e47faf1 | 170 | #endif |
f048e32f | 171 | |
60a41aee RR |
172 | #if wxUSE_LIBTIFF |
173 | if ( !image.LoadFile( dir + wxString("horse.tif"), wxBITMAP_TYPE_TIF ) ) | |
174 | wxLogError("Can't load TIFF image"); | |
175 | else | |
176 | my_horse_tiff = new wxBitmap( image.ConvertToBitmap() ); | |
177 | #endif | |
178 | ||
179 | image.LoadFile( dir + wxString("test.png") ); | |
180 | my_square = new wxBitmap( image.ConvertToBitmap() ); | |
f048e32f | 181 | |
60a41aee | 182 | CreateAntiAliasedBitmap(); |
dc1dbfc6 RL |
183 | |
184 | my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width, | |
185 | smile_height, 1 ); | |
01111366 RR |
186 | } |
187 | ||
1d5b7a0b | 188 | MyCanvas::~MyCanvas() |
01111366 | 189 | { |
60a41aee RR |
190 | delete my_horse_pnm; |
191 | delete my_horse_png; | |
192 | delete my_horse_jpeg; | |
193 | delete my_horse_gif; | |
194 | delete my_horse_bmp; | |
195 | delete my_horse_pcx; | |
196 | delete my_horse_tiff; | |
dc1dbfc6 | 197 | delete my_smile_xbm; |
60a41aee RR |
198 | delete my_square; |
199 | delete my_anti; | |
01111366 RR |
200 | } |
201 | ||
202 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
203 | { | |
be25e480 RR |
204 | wxPaintDC dc( this ); |
205 | PrepareDC( dc ); | |
a91b47e8 | 206 | |
be25e480 RR |
207 | dc.DrawText( "Loaded image", 30, 10 ); |
208 | if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 ); | |
f048e32f | 209 | |
be25e480 RR |
210 | dc.DrawText( "Drawn directly", 150, 10 ); |
211 | dc.SetBrush( wxBrush( "orange", wxSOLID ) ); | |
212 | dc.SetPen( *wxWHITE_PEN ); | |
213 | dc.DrawRectangle( 150, 30, 100, 100 ); | |
214 | ||
215 | if (my_anti && my_anti->Ok()) | |
216 | dc.DrawBitmap( *my_anti, 280, 30 ); | |
217 | ||
218 | dc.DrawText( "PNG handler", 30, 135 ); | |
219 | if (my_horse_png && my_horse_png->Ok()) | |
220 | { | |
221 | dc.DrawBitmap( *my_horse_png, 30, 150 ); | |
222 | wxRect rect(0,0,100,100); | |
223 | wxBitmap sub( my_horse_png->GetSubBitmap(rect) ); | |
224 | dc.DrawText( "GetSubBitmap()", 280, 190 ); | |
225 | dc.DrawBitmap( sub, 280, 210 ); | |
226 | } | |
227 | ||
228 | dc.DrawText( "JPEG handler", 30, 365 ); | |
229 | if (my_horse_jpeg && my_horse_jpeg->Ok()) | |
230 | dc.DrawBitmap( *my_horse_jpeg, 30, 380 ); | |
231 | ||
232 | dc.DrawText( "GIF handler", 30, 595 ); | |
233 | if (my_horse_gif && my_horse_gif->Ok()) | |
234 | dc.DrawBitmap( *my_horse_gif, 30, 610 ); | |
235 | ||
236 | dc.DrawText( "PCX handler", 30, 825 ); | |
237 | if (my_horse_pcx && my_horse_pcx->Ok()) | |
238 | dc.DrawBitmap( *my_horse_pcx, 30, 840 ); | |
239 | ||
240 | dc.DrawText( "BMP handler", 30, 1055 ); | |
241 | if (my_horse_bmp && my_horse_bmp->Ok()) | |
242 | dc.DrawBitmap( *my_horse_bmp, 30, 1070 ); | |
243 | ||
244 | dc.DrawText( "PNM handler", 30, 1285 ); | |
245 | if (my_horse_pnm && my_horse_pnm->Ok()) | |
246 | dc.DrawBitmap( *my_horse_pnm, 30, 1300 ); | |
60a41aee | 247 | |
be25e480 RR |
248 | dc.DrawText( "TIFF handler", 30, 1515 ); |
249 | if (my_horse_tiff && my_horse_tiff->Ok()) | |
250 | dc.DrawBitmap( *my_horse_pnm, 30, 1530 ); | |
251 | ||
252 | if (my_smile_xbm && my_smile_xbm->Ok()) | |
253 | { | |
254 | dc.DrawText( "XBM bitmap", 30, 1745 ); | |
255 | dc.SetPen( *wxRED_PEN ); | |
256 | dc.DrawBitmap( *my_smile_xbm, 30, 1760 ); | |
257 | ||
258 | dc.DrawText( "After wxImage conversion", 150, 1745 ); | |
259 | wxImage i( *my_smile_xbm ); | |
260 | i.SetMaskColour( 0,0,0 ); | |
261 | i.Replace( 255,255,255, | |
262 | wxRED_PEN->GetColour().Red(), | |
263 | wxRED_PEN->GetColour().Green(), | |
264 | wxRED_PEN->GetColour().Blue() ); | |
265 | dc.DrawBitmap( i.ConvertToBitmap(), 150, 1760, TRUE ); | |
266 | } | |
9390a202 | 267 | } |
1d5b7a0b | 268 | |
9390a202 RR |
269 | void MyCanvas::CreateAntiAliasedBitmap() |
270 | { | |
271 | wxBitmap bitmap( 300, 300 ); | |
a91b47e8 | 272 | |
9390a202 | 273 | wxMemoryDC dc; |
a91b47e8 | 274 | |
9390a202 | 275 | dc.SelectObject( bitmap ); |
a91b47e8 | 276 | |
9390a202 | 277 | dc.Clear(); |
f048e32f | 278 | |
6e47faf1 | 279 | dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) ); |
9390a202 RR |
280 | dc.SetTextForeground( "RED" ); |
281 | dc.DrawText( "This is anti-aliased Text.", 20, 20 ); | |
282 | dc.DrawText( "And a Rectangle.", 20, 60 ); | |
f048e32f | 283 | |
9390a202 | 284 | dc.SetBrush( *wxRED_BRUSH ); |
91b8de8d | 285 | dc.SetPen( *wxTRANSPARENT_PEN ); |
9390a202 | 286 | dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 ); |
f048e32f | 287 | |
9390a202 RR |
288 | wxImage original( bitmap ); |
289 | wxImage anti( 150, 150 ); | |
a91b47e8 | 290 | |
9390a202 | 291 | /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */ |
f048e32f | 292 | |
9390a202 RR |
293 | for (int y = 1; y < 149; y++) |
294 | for (int x = 1; x < 149; x++) | |
295 | { | |
296 | int red = original.GetRed( x*2, y*2 ) + | |
f048e32f VZ |
297 | original.GetRed( x*2-1, y*2 ) + |
298 | original.GetRed( x*2, y*2+1 ) + | |
9390a202 | 299 | original.GetRed( x*2+1, y*2+1 ); |
f048e32f VZ |
300 | red = red/4; |
301 | ||
9390a202 | 302 | int green = original.GetGreen( x*2, y*2 ) + |
f048e32f VZ |
303 | original.GetGreen( x*2-1, y*2 ) + |
304 | original.GetGreen( x*2, y*2+1 ) + | |
9390a202 | 305 | original.GetGreen( x*2+1, y*2+1 ); |
f048e32f VZ |
306 | green = green/4; |
307 | ||
9390a202 | 308 | int blue = original.GetBlue( x*2, y*2 ) + |
f048e32f VZ |
309 | original.GetBlue( x*2-1, y*2 ) + |
310 | original.GetBlue( x*2, y*2+1 ) + | |
9390a202 | 311 | original.GetBlue( x*2+1, y*2+1 ); |
f048e32f | 312 | blue = blue/4; |
9390a202 RR |
313 | anti.SetRGB( x, y, red, green, blue ); |
314 | } | |
315 | my_anti = new wxBitmap( anti.ConvertToBitmap() ); | |
01111366 RR |
316 | } |
317 | ||
318 | // MyFrame | |
319 | ||
79490c3d VZ |
320 | const int ID_QUIT = 108; |
321 | const int ID_ABOUT = 109; | |
01111366 RR |
322 | |
323 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
324 | ||
325 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
326 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) | |
327 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
328 | END_EVENT_TABLE() | |
329 | ||
1d5b7a0b VZ |
330 | MyFrame::MyFrame() |
331 | : wxFrame( (wxFrame *)NULL, -1, "wxImage sample", | |
332 | wxPoint(20,20), wxSize(470,360) ) | |
01111366 RR |
333 | { |
334 | wxMenu *file_menu = new wxMenu(); | |
90e58684 RR |
335 | file_menu->Append( ID_ABOUT, "&About.."); |
336 | file_menu->Append( ID_QUIT, "E&xit"); | |
1d5b7a0b | 337 | |
01111366 | 338 | wxMenuBar *menu_bar = new wxMenuBar(); |
90e58684 | 339 | menu_bar->Append(file_menu, "&File"); |
3d05544e | 340 | |
01111366 | 341 | SetMenuBar( menu_bar ); |
1d5b7a0b | 342 | |
917e533b RR |
343 | CreateStatusBar(2); |
344 | int widths[] = { -1, 100 }; | |
345 | SetStatusWidths( 2, widths ); | |
1d5b7a0b | 346 | |
fb1585ae | 347 | m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) ); |
cbd4be25 | 348 | |
dc1dbfc6 RL |
349 | // 500 width * 1900 height |
350 | m_canvas->SetScrollbars( 10, 10, 50, 190 ); | |
01111366 RR |
351 | } |
352 | ||
353 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
354 | { | |
355 | Close( TRUE ); | |
356 | } | |
357 | ||
358 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
359 | { | |
1d5b7a0b | 360 | (void)wxMessageBox( "wxImage demo\n" |
8a5137d7 | 361 | "\n" |
1d5b7a0b VZ |
362 | "Robert Roebling (c) 1998", |
363 | "About wxImage Demo", wxICON_INFORMATION | wxOK ); | |
fb1585ae RR |
364 | } |
365 | ||
01111366 RR |
366 | //----------------------------------------------------------------------------- |
367 | // MyApp | |
368 | //----------------------------------------------------------------------------- | |
369 | ||
1d5b7a0b | 370 | bool MyApp::OnInit() |
01111366 | 371 | { |
e9c4b1a2 JS |
372 | #if wxUSE_LIBPNG |
373 | wxImage::AddHandler( new wxPNGHandler ); | |
374 | #endif | |
375 | ||
376 | #if wxUSE_LIBJPEG | |
377 | wxImage::AddHandler( new wxJPEGHandler ); | |
378 | #endif | |
379 | ||
60a41aee RR |
380 | #if wxUSE_LIBTIFF |
381 | wxImage::AddHandler( new wxTIFFHandler ); | |
382 | #endif | |
383 | ||
6e47faf1 | 384 | #if wxUSE_GIF |
e1929140 | 385 | wxImage::AddHandler( new wxGIFHandler ); |
6e47faf1 JS |
386 | #endif |
387 | ||
388 | #if wxUSE_PCX | |
cbd4be25 | 389 | wxImage::AddHandler( new wxPCXHandler ); |
6e47faf1 JS |
390 | #endif |
391 | ||
392 | #if wxUSE_PNM | |
6319afe3 | 393 | wxImage::AddHandler( new wxPNMHandler ); |
6e47faf1 | 394 | #endif |
e1929140 | 395 | |
01111366 RR |
396 | wxFrame *frame = new MyFrame(); |
397 | frame->Show( TRUE ); | |
1d5b7a0b | 398 | |
01111366 RR |
399 | return TRUE; |
400 | } | |
401 |