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