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