fixed bitmap constructor for monochrome bitmaps
[wxWidgets.git] / samples / image / image.cpp
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 wxBitmap bm( 1200, 1, 1 );
148 dc.DrawBitmap( bm, 10, 10 );
149
150 dc.DrawText( "Drawn directly", 150, 10 );
151 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
152 dc.SetPen( *wxWHITE_PEN );
153 dc.DrawRectangle( 150, 30, 100, 100 );
154
155 if (my_anti && my_anti->Ok()) dc.DrawBitmap( *my_anti, 250, 140 );
156
157 dc.DrawText( "PNG handler", 30, 135 );
158 if (my_horse_png && my_horse_png->Ok()) dc.DrawBitmap( *my_horse_png, 30, 150 );
159
160 dc.DrawText( "JPEG handler", 30, 365 );
161 if (my_horse_jpeg && my_horse_jpeg->Ok()) dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
162
163 dc.DrawText( "GIF handler", 30, 595 );
164 if (my_horse_gif && my_horse_gif->Ok()) dc.DrawBitmap( *my_horse_gif, 30, 610 );
165 }
166
167 void MyCanvas::CreateAntiAliasedBitmap()
168 {
169 wxBitmap bitmap( 300, 300 );
170
171 wxMemoryDC dc;
172
173 dc.SelectObject( bitmap );
174
175 dc.Clear();
176
177 dc.SetFont( wxFont( 24, wxDECORATIVE, wxDEFAULT, wxDEFAULT ) );
178 dc.SetTextForeground( "RED" );
179 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
180 dc.DrawText( "And a Rectangle.", 20, 60 );
181
182 dc.SetBrush( *wxRED_BRUSH );
183 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
184
185 wxImage original( bitmap );
186 wxImage anti( 150, 150 );
187
188 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
189
190 for (int y = 1; y < 149; y++)
191 for (int x = 1; x < 149; x++)
192 {
193 int red = original.GetRed( x*2, y*2 ) +
194 original.GetRed( x*2-1, y*2 ) +
195 original.GetRed( x*2, y*2+1 ) +
196 original.GetRed( x*2+1, y*2+1 );
197 red = red/4;
198
199 int green = original.GetGreen( x*2, y*2 ) +
200 original.GetGreen( x*2-1, y*2 ) +
201 original.GetGreen( x*2, y*2+1 ) +
202 original.GetGreen( x*2+1, y*2+1 );
203 green = green/4;
204
205 int blue = original.GetBlue( x*2, y*2 ) +
206 original.GetBlue( x*2-1, y*2 ) +
207 original.GetBlue( x*2, y*2+1 ) +
208 original.GetBlue( x*2+1, y*2+1 );
209 blue = blue/4;
210 anti.SetRGB( x, y, red, green, blue );
211 }
212 my_anti = new wxBitmap( anti.ConvertToBitmap() );
213 }
214
215 // MyFrame
216
217 const int ID_QUIT = 108;
218 const int ID_ABOUT = 109;
219
220 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
221
222 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
223 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
224 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
225 END_EVENT_TABLE()
226
227 MyFrame::MyFrame()
228 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
229 wxPoint(20,20), wxSize(470,360) )
230 {
231 wxMenu *file_menu = new wxMenu();
232 file_menu->Append( ID_ABOUT, "About..");
233 file_menu->Append( ID_QUIT, "Exit");
234
235 wxMenuBar *menu_bar = new wxMenuBar();
236 menu_bar->Append(file_menu, "File");
237
238 SetMenuBar( menu_bar );
239
240 CreateStatusBar(2);
241 int widths[] = { -1, 100 };
242 SetStatusWidths( 2, widths );
243
244 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
245 m_canvas->SetScrollbars( 10, 10, 50, 100 );
246 }
247
248 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
249 {
250 Close( TRUE );
251 }
252
253 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
254 {
255 (void)wxMessageBox( "wxImage demo\n"
256 "Robert Roebling (c) 1998",
257 "About wxImage Demo", wxICON_INFORMATION | wxOK );
258 }
259
260 //-----------------------------------------------------------------------------
261 // MyApp
262 //-----------------------------------------------------------------------------
263
264 bool MyApp::OnInit()
265 {
266 #if wxUSE_LIBPNG
267 wxImage::AddHandler( new wxPNGHandler );
268 #endif
269
270 #if wxUSE_LIBJPEG
271 wxImage::AddHandler( new wxJPEGHandler );
272 #endif
273
274 wxImage::AddHandler( new wxGIFHandler );
275
276 wxFrame *frame = new MyFrame();
277 frame->Show( TRUE );
278
279 return TRUE;
280 }
281