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