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