]>
Commit | Line | Data |
---|---|---|
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 | // derived classes | |
24 | ||
25 | class MyFrame; | |
26 | class MyApp; | |
27 | ||
28 | // MyCanvas | |
29 | ||
30 | class MyCanvas: public wxScrolledWindow | |
31 | { | |
32 | public: | |
33 | MyCanvas() {}; | |
34 | MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size ); | |
35 | ~MyCanvas(); | |
36 | void OnPaint( wxPaintEvent &event ); | |
37 | void CreateAntiAliasedBitmap(); | |
38 | ||
39 | wxBitmap *my_horse_png; | |
40 | wxBitmap *my_horse_jpeg; | |
41 | wxBitmap *my_horse_gif; | |
42 | wxBitmap *my_square; | |
43 | wxBitmap *my_anti; | |
44 | ||
45 | DECLARE_DYNAMIC_CLASS(MyCanvas) | |
46 | DECLARE_EVENT_TABLE() | |
47 | }; | |
48 | ||
49 | // MyFrame | |
50 | ||
51 | class MyFrame: public wxFrame | |
52 | { | |
53 | public: | |
54 | MyFrame(); | |
55 | ||
56 | void OnAbout( wxCommandEvent &event ); | |
57 | void OnQuit( wxCommandEvent &event ); | |
58 | ||
59 | MyCanvas *m_canvas; | |
60 | ||
61 | DECLARE_DYNAMIC_CLASS(MyFrame) | |
62 | DECLARE_EVENT_TABLE() | |
63 | }; | |
64 | ||
65 | // MyApp | |
66 | ||
67 | class MyApp: public wxApp | |
68 | { | |
69 | public: | |
70 | virtual bool OnInit(); | |
71 | }; | |
72 | ||
73 | // main program | |
74 | ||
75 | IMPLEMENT_APP(MyApp) | |
76 | ||
77 | // MyCanvas | |
78 | ||
79 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) | |
80 | ||
81 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
82 | EVT_PAINT(MyCanvas::OnPaint) | |
83 | END_EVENT_TABLE() | |
84 | ||
85 | MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, | |
86 | const wxPoint &pos, const wxSize &size ) | |
87 | : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER ) | |
88 | { | |
89 | my_horse_png = (wxBitmap*) NULL; | |
90 | my_horse_jpeg = (wxBitmap*) NULL; | |
91 | my_horse_gif = (wxBitmap*) NULL; | |
92 | my_square = (wxBitmap*) NULL; | |
93 | my_anti = (wxBitmap*) NULL; | |
94 | ||
95 | SetBackgroundColour(* wxWHITE); | |
96 | ||
97 | wxBitmap bitmap( 100, 100 ); | |
98 | ||
99 | wxMemoryDC dc; | |
100 | dc.SelectObject( bitmap ); | |
101 | dc.SetBrush( wxBrush( "orange", wxSOLID ) ); | |
102 | dc.SetPen( *wxWHITE_PEN ); | |
103 | dc.DrawRectangle( 0, 0, 100, 100 ); | |
104 | dc.SelectObject( wxNullBitmap ); | |
105 | ||
106 | wxString dir(""); | |
107 | ||
108 | #ifdef __WXGTK__ | |
109 | dir = wxString("../"); | |
110 | #endif | |
111 | ||
112 | wxImage image( bitmap ); | |
113 | image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ); | |
114 | ||
115 | image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG ); | |
116 | my_horse_png = new wxBitmap( image.ConvertToBitmap() ); | |
117 | ||
118 | image.LoadFile( dir + wxString("horse.jpg"), wxBITMAP_TYPE_JPEG ); | |
119 | my_horse_jpeg = new wxBitmap( image.ConvertToBitmap() ); | |
120 | ||
121 | image.LoadFile( dir + wxString("horse.gif"), wxBITMAP_TYPE_GIF ); | |
122 | my_horse_gif = new wxBitmap( image.ConvertToBitmap() ); | |
123 | ||
124 | image.LoadFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ); | |
125 | my_square = new wxBitmap( image.ConvertToBitmap() ); | |
126 | ||
127 | CreateAntiAliasedBitmap(); | |
128 | } | |
129 | ||
130 | MyCanvas::~MyCanvas() | |
131 | { | |
132 | delete my_horse_png; | |
133 | delete my_horse_jpeg; | |
134 | delete my_horse_gif; | |
135 | delete my_square; | |
136 | delete my_anti; | |
137 | } | |
138 | ||
139 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
140 | { | |
141 | wxPaintDC dc( this ); | |
142 | PrepareDC( dc ); | |
143 | ||
144 | dc.DrawText( "Loaded image", 30, 10 ); | |
145 | if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 ); | |
146 | ||
147 | dc.DrawText( "Drawn directly", 150, 10 ); | |
148 | dc.SetBrush( wxBrush( "orange", wxSOLID ) ); | |
149 | dc.SetPen( *wxWHITE_PEN ); | |
150 | dc.DrawRectangle( 150, 30, 100, 100 ); | |
151 | ||
152 | if (my_anti && my_anti->Ok()) dc.DrawBitmap( *my_anti, 250, 140 ); | |
153 | ||
154 | dc.DrawText( "PNG handler", 30, 135 ); | |
155 | if (my_horse_png && my_horse_png->Ok()) dc.DrawBitmap( *my_horse_png, 30, 150 ); | |
156 | ||
157 | dc.DrawText( "JPEG handler", 30, 365 ); | |
158 | if (my_horse_jpeg && my_horse_jpeg->Ok()) dc.DrawBitmap( *my_horse_jpeg, 30, 380 ); | |
159 | ||
160 | dc.DrawText( "GIF handler", 30, 595 ); | |
161 | if (my_horse_gif && my_horse_gif->Ok()) dc.DrawBitmap( *my_horse_gif, 30, 610 ); | |
162 | } | |
163 | ||
164 | void MyCanvas::CreateAntiAliasedBitmap() | |
165 | { | |
166 | wxBitmap bitmap( 300, 300 ); | |
167 | ||
168 | wxMemoryDC dc; | |
169 | ||
170 | dc.SelectObject( bitmap ); | |
171 | ||
172 | dc.Clear(); | |
173 | ||
174 | dc.SetFont( wxFont( 24, wxDECORATIVE, wxDEFAULT, wxDEFAULT ) ); | |
175 | dc.SetTextForeground( "RED" ); | |
176 | dc.DrawText( "This is anti-aliased Text.", 20, 20 ); | |
177 | dc.DrawText( "And a Rectangle.", 20, 60 ); | |
178 | ||
179 | dc.SetBrush( *wxRED_BRUSH ); | |
180 | dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 ); | |
181 | ||
182 | wxImage original( bitmap ); | |
183 | wxImage anti( 150, 150 ); | |
184 | ||
185 | /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */ | |
186 | ||
187 | for (int y = 1; y < 149; y++) | |
188 | for (int x = 1; x < 149; x++) | |
189 | { | |
190 | int red = original.GetRed( x*2, y*2 ) + | |
191 | original.GetRed( x*2-1, y*2 ) + | |
192 | original.GetRed( x*2, y*2+1 ) + | |
193 | original.GetRed( x*2+1, y*2+1 ); | |
194 | red = red/4; | |
195 | ||
196 | int green = original.GetGreen( x*2, y*2 ) + | |
197 | original.GetGreen( x*2-1, y*2 ) + | |
198 | original.GetGreen( x*2, y*2+1 ) + | |
199 | original.GetGreen( x*2+1, y*2+1 ); | |
200 | green = green/4; | |
201 | ||
202 | int blue = original.GetBlue( x*2, y*2 ) + | |
203 | original.GetBlue( x*2-1, y*2 ) + | |
204 | original.GetBlue( x*2, y*2+1 ) + | |
205 | original.GetBlue( x*2+1, y*2+1 ); | |
206 | blue = blue/4; | |
207 | anti.SetRGB( x, y, red, green, blue ); | |
208 | } | |
209 | my_anti = new wxBitmap( anti.ConvertToBitmap() ); | |
210 | } | |
211 | ||
212 | // MyFrame | |
213 | ||
214 | const int ID_QUIT = 108; | |
215 | const int ID_ABOUT = 109; | |
216 | ||
217 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
218 | ||
219 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
220 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) | |
221 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
222 | END_EVENT_TABLE() | |
223 | ||
224 | MyFrame::MyFrame() | |
225 | : wxFrame( (wxFrame *)NULL, -1, "wxImage sample", | |
226 | wxPoint(20,20), wxSize(470,360) ) | |
227 | { | |
228 | wxMenu *file_menu = new wxMenu(); | |
229 | file_menu->Append( ID_ABOUT, "&About.."); | |
230 | file_menu->Append( ID_QUIT, "E&xit"); | |
231 | ||
232 | wxMenuBar *menu_bar = new wxMenuBar(); | |
233 | menu_bar->Append(file_menu, "&File"); | |
234 | ||
235 | SetMenuBar( menu_bar ); | |
236 | ||
237 | CreateStatusBar(2); | |
238 | int widths[] = { -1, 100 }; | |
239 | SetStatusWidths( 2, widths ); | |
240 | ||
241 | m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) ); | |
242 | m_canvas->SetScrollbars( 10, 10, 50, 100 ); | |
243 | } | |
244 | ||
245 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
246 | { | |
247 | Close( TRUE ); | |
248 | } | |
249 | ||
250 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
251 | { | |
252 | (void)wxMessageBox( "wxImage demo\n" | |
253 | "Robert Roebling (c) 1998", | |
254 | "About wxImage Demo", wxICON_INFORMATION | wxOK ); | |
255 | } | |
256 | ||
257 | //----------------------------------------------------------------------------- | |
258 | // MyApp | |
259 | //----------------------------------------------------------------------------- | |
260 | ||
261 | bool MyApp::OnInit() | |
262 | { | |
263 | #if wxUSE_LIBPNG | |
264 | wxImage::AddHandler( new wxPNGHandler ); | |
265 | #endif | |
266 | ||
267 | #if wxUSE_LIBJPEG | |
268 | wxImage::AddHandler( new wxJPEGHandler ); | |
269 | #endif | |
270 | ||
271 | wxImage::AddHandler( new wxGIFHandler ); | |
272 | ||
273 | wxFrame *frame = new MyFrame(); | |
274 | frame->Show( TRUE ); | |
275 | ||
276 | return TRUE; | |
277 | } | |
278 |