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