TIFF change 6.
[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_horse_pcx;
46 wxBitmap *my_horse_pnm;
47 wxBitmap *my_horse_tiff;
48 wxBitmap *my_square;
49 wxBitmap *my_anti;
50
51 private:
52 DECLARE_DYNAMIC_CLASS(MyCanvas)
53 DECLARE_EVENT_TABLE()
54 };
55
56 // MyFrame
57
58 class MyFrame: public wxFrame
59 {
60 public:
61 MyFrame();
62
63 void OnAbout( wxCommandEvent &event );
64 void OnQuit( wxCommandEvent &event );
65
66 MyCanvas *m_canvas;
67
68 private:
69 DECLARE_DYNAMIC_CLASS(MyFrame)
70 DECLARE_EVENT_TABLE()
71 };
72
73 // MyApp
74
75 class MyApp: public wxApp
76 {
77 public:
78 virtual bool OnInit();
79 };
80
81 // main program
82
83 IMPLEMENT_APP(MyApp)
84
85 // MyCanvas
86
87 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
88
89 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
90 EVT_PAINT(MyCanvas::OnPaint)
91 END_EVENT_TABLE()
92
93 MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
94 const wxPoint &pos, const wxSize &size )
95 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
96 {
97 my_horse_png = (wxBitmap*) NULL;
98 my_horse_jpeg = (wxBitmap*) NULL;
99 my_horse_gif = (wxBitmap*) NULL;
100 my_horse_bmp = (wxBitmap*) NULL;
101 my_horse_pcx = (wxBitmap*) NULL;
102 my_horse_pnm = (wxBitmap*) NULL;
103 my_horse_tiff = (wxBitmap*) NULL;
104 my_square = (wxBitmap*) NULL;
105 my_anti = (wxBitmap*) NULL;
106
107 SetBackgroundColour(* wxWHITE);
108
109 wxBitmap bitmap( 100, 100 );
110
111 wxMemoryDC dc;
112 dc.SelectObject( bitmap );
113 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
114 dc.SetPen( *wxWHITE_PEN );
115 dc.DrawRectangle( 0, 0, 100, 100 );
116 dc.SelectObject( wxNullBitmap );
117
118 // try to find the directory with our images
119 wxString dir;
120 if ( wxFile::Exists("./horse.png") )
121 dir = "./";
122 else if ( wxFile::Exists("../horse.png") )
123 dir = "../";
124 else
125 wxLogWarning("Can't find image files in either '.' or '..'!");
126
127 wxImage image( bitmap );
128
129 if ( !image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ) )
130 wxLogError("Can't save file");
131
132 if ( !image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG ) )
133 wxLogError("Can't load PNG image");
134 else
135 my_horse_png = new wxBitmap( image.ConvertToBitmap() );
136
137 if ( !image.LoadFile( dir + wxString("horse.jpg") ) )
138 wxLogError("Can't load JPG image");
139 else
140 my_horse_jpeg = new wxBitmap( image.ConvertToBitmap() );
141
142 #if wxUSE_GIF
143 if ( !image.LoadFile( dir + wxString("horse.gif") ) )
144 wxLogError("Can't load GIF image");
145 else
146 my_horse_gif = new wxBitmap( image.ConvertToBitmap() );
147 #endif
148
149 #if wxUSE_PCX
150 if ( !image.LoadFile( dir + wxString("horse.pcx"), wxBITMAP_TYPE_PCX ) )
151 wxLogError("Can't load PCX image");
152 else
153 my_horse_pcx = new wxBitmap( image.ConvertToBitmap() );
154 #endif
155
156 if ( !image.LoadFile( dir + wxString("horse.bmp"), wxBITMAP_TYPE_BMP ) )
157 wxLogError("Can't load BMP image");
158 else
159 my_horse_bmp = new wxBitmap( image.ConvertToBitmap() );
160
161 #if wxUSE_PNM
162 if ( !image.LoadFile( dir + wxString("horse.pnm"), wxBITMAP_TYPE_PNM ) )
163 wxLogError("Can't load PNM image");
164 else
165 my_horse_pnm = new wxBitmap( image.ConvertToBitmap() );
166 #endif
167
168 #if wxUSE_LIBTIFF
169 if ( !image.LoadFile( dir + wxString("horse.tif"), wxBITMAP_TYPE_TIF ) )
170 wxLogError("Can't load TIFF image");
171 else
172 my_horse_tiff = new wxBitmap( image.ConvertToBitmap() );
173 #endif
174
175 image.LoadFile( dir + wxString("test.png") );
176 my_square = new wxBitmap( image.ConvertToBitmap() );
177
178 CreateAntiAliasedBitmap();
179 }
180
181 MyCanvas::~MyCanvas()
182 {
183 delete my_horse_pnm;
184 delete my_horse_png;
185 delete my_horse_jpeg;
186 delete my_horse_gif;
187 delete my_horse_bmp;
188 delete my_horse_pcx;
189 delete my_horse_tiff;
190 delete my_square;
191 delete my_anti;
192 }
193
194 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
195 {
196 wxPaintDC dc( this );
197 PrepareDC( dc );
198
199 dc.DrawText( "Loaded image", 30, 10 );
200 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
201
202 dc.DrawText( "Drawn directly", 150, 10 );
203 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
204 dc.SetPen( *wxWHITE_PEN );
205 dc.DrawRectangle( 150, 30, 100, 100 );
206
207 if (my_anti && my_anti->Ok()) dc.DrawBitmap( *my_anti, 250, 140 );
208
209 dc.DrawText( "PNG handler", 30, 135 );
210 if (my_horse_png && my_horse_png->Ok()) dc.DrawBitmap( *my_horse_png, 30, 150 );
211
212 dc.DrawText( "JPEG handler", 30, 365 );
213 if (my_horse_jpeg && my_horse_jpeg->Ok()) dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
214
215 dc.DrawText( "GIF handler", 30, 595 );
216 if (my_horse_gif && my_horse_gif->Ok()) dc.DrawBitmap( *my_horse_gif, 30, 610 );
217
218 dc.DrawText( "PCX handler", 30, 825 );
219 if (my_horse_pcx && my_horse_pcx->Ok()) dc.DrawBitmap( *my_horse_pcx, 30, 840 );
220
221 dc.DrawText( "BMP handler", 30, 1055 );
222 if (my_horse_bmp && my_horse_bmp->Ok()) dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
223
224 dc.DrawText( "PNM handler", 30, 1285 );
225 if (my_horse_pnm && my_horse_pnm->Ok()) dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
226
227 dc.DrawText( "TIFF handler", 30, 1415 );
228 if (my_horse_tiff && my_horse_tiff->Ok()) dc.DrawBitmap( *my_horse_pnm, 30, 1430 );
229 }
230
231 void MyCanvas::CreateAntiAliasedBitmap()
232 {
233 wxBitmap bitmap( 300, 300 );
234
235 wxMemoryDC dc;
236
237 dc.SelectObject( bitmap );
238
239 dc.Clear();
240
241 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
242 dc.SetTextForeground( "RED" );
243 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
244 dc.DrawText( "And a Rectangle.", 20, 60 );
245
246 dc.SetBrush( *wxRED_BRUSH );
247 dc.SetPen( *wxTRANSPARENT_PEN );
248 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
249
250 wxImage original( bitmap );
251 wxImage anti( 150, 150 );
252
253 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
254
255 for (int y = 1; y < 149; y++)
256 for (int x = 1; x < 149; x++)
257 {
258 int red = original.GetRed( x*2, y*2 ) +
259 original.GetRed( x*2-1, y*2 ) +
260 original.GetRed( x*2, y*2+1 ) +
261 original.GetRed( x*2+1, y*2+1 );
262 red = red/4;
263
264 int green = original.GetGreen( x*2, y*2 ) +
265 original.GetGreen( x*2-1, y*2 ) +
266 original.GetGreen( x*2, y*2+1 ) +
267 original.GetGreen( x*2+1, y*2+1 );
268 green = green/4;
269
270 int blue = original.GetBlue( x*2, y*2 ) +
271 original.GetBlue( x*2-1, y*2 ) +
272 original.GetBlue( x*2, y*2+1 ) +
273 original.GetBlue( x*2+1, y*2+1 );
274 blue = blue/4;
275 anti.SetRGB( x, y, red, green, blue );
276 }
277 my_anti = new wxBitmap( anti.ConvertToBitmap() );
278 }
279
280 // MyFrame
281
282 const int ID_QUIT = 108;
283 const int ID_ABOUT = 109;
284
285 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
286
287 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
288 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
289 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
290 END_EVENT_TABLE()
291
292 MyFrame::MyFrame()
293 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
294 wxPoint(20,20), wxSize(470,360) )
295 {
296 wxMenu *file_menu = new wxMenu();
297 file_menu->Append( ID_ABOUT, "&About..");
298 file_menu->Append( ID_QUIT, "E&xit");
299
300 wxMenuBar *menu_bar = new wxMenuBar();
301 menu_bar->Append(file_menu, "&File");
302
303 SetMenuBar( menu_bar );
304
305 CreateStatusBar(2);
306 int widths[] = { -1, 100 };
307 SetStatusWidths( 2, widths );
308
309 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
310
311 // 500 width * 1300 height
312 m_canvas->SetScrollbars( 10, 10, 50, 180 );
313 }
314
315 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
316 {
317 Close( TRUE );
318 }
319
320 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
321 {
322 (void)wxMessageBox( "wxImage demo\n"
323 "\n"
324 "Robert Roebling (c) 1998",
325 "About wxImage Demo", wxICON_INFORMATION | wxOK );
326 }
327
328 //-----------------------------------------------------------------------------
329 // MyApp
330 //-----------------------------------------------------------------------------
331
332 bool MyApp::OnInit()
333 {
334 #if wxUSE_LIBPNG
335 wxImage::AddHandler( new wxPNGHandler );
336 #endif
337
338 #if wxUSE_LIBJPEG
339 wxImage::AddHandler( new wxJPEGHandler );
340 #endif
341
342 #if wxUSE_LIBTIFF
343 wxImage::AddHandler( new wxTIFFHandler );
344 #endif
345
346 #if wxUSE_GIF
347 wxImage::AddHandler( new wxGIFHandler );
348 #endif
349
350 #if wxUSE_PCX
351 wxImage::AddHandler( new wxPCXHandler );
352 #endif
353
354 #if wxUSE_PNM
355 wxImage::AddHandler( new wxPNMHandler );
356 #endif
357
358 wxFrame *frame = new MyFrame();
359 frame->Show( TRUE );
360
361 return TRUE;
362 }
363