]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
test for timegm() added
[wxWidgets.git] / samples / image / image.cpp
CommitLineData
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
a23fd0e1
VZ
23#include "wx/file.h"
24
01111366
RR
25// derived classes
26
27class MyFrame;
28class MyApp;
29
30// MyCanvas
31
32class MyCanvas: public wxScrolledWindow
33{
1d5b7a0b
VZ
34public:
35 MyCanvas() {};
01111366 36 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
1d5b7a0b 37 ~MyCanvas();
01111366 38 void OnPaint( wxPaintEvent &event );
9390a202 39 void CreateAntiAliasedBitmap();
1d5b7a0b 40
e1929140
RR
41 wxBitmap *my_horse_png;
42 wxBitmap *my_horse_jpeg;
43 wxBitmap *my_horse_gif;
ca26177c 44 wxBitmap *my_horse_bmp;
cbd4be25 45 wxBitmap *my_horse_pcx;
6319afe3 46 wxBitmap *my_horse_pnm;
60a41aee 47 wxBitmap *my_horse_tiff;
e8fdc264 48 wxBitmap *my_square;
9390a202 49 wxBitmap *my_anti;
1d5b7a0b 50
60a41aee 51private:
1d5b7a0b
VZ
52 DECLARE_DYNAMIC_CLASS(MyCanvas)
53 DECLARE_EVENT_TABLE()
01111366
RR
54};
55
56// MyFrame
57
58class MyFrame: public wxFrame
59{
1d5b7a0b
VZ
60public:
61 MyFrame();
01111366 62
01111366
RR
63 void OnAbout( wxCommandEvent &event );
64 void OnQuit( wxCommandEvent &event );
1d5b7a0b 65
01111366 66 MyCanvas *m_canvas;
1d5b7a0b 67
60a41aee 68private:
1d5b7a0b
VZ
69 DECLARE_DYNAMIC_CLASS(MyFrame)
70 DECLARE_EVENT_TABLE()
01111366
RR
71};
72
73// MyApp
74
75class MyApp: public wxApp
76{
1d5b7a0b
VZ
77public:
78 virtual bool OnInit();
01111366
RR
79};
80
81// main program
82
83IMPLEMENT_APP(MyApp)
84
85// MyCanvas
86
87IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
88
1d5b7a0b
VZ
89BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
90 EVT_PAINT(MyCanvas::OnPaint)
01111366
RR
91END_EVENT_TABLE()
92
acbd13a3 93MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
1d5b7a0b
VZ
94 const wxPoint &pos, const wxSize &size )
95 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
01111366 96{
60a41aee
RR
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() );
6e47faf1
JS
141
142#if wxUSE_GIF
60a41aee
RR
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() );
6e47faf1 147#endif
cbd4be25 148
6e47faf1 149#if wxUSE_PCX
60a41aee
RR
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() );
6e47faf1 154#endif
cbd4be25 155
60a41aee
RR
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() );
6319afe3 160
6e47faf1 161#if wxUSE_PNM
60a41aee
RR
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() );
6e47faf1 166#endif
f048e32f 167
60a41aee
RR
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() );
f048e32f 177
60a41aee 178 CreateAntiAliasedBitmap();
01111366
RR
179}
180
1d5b7a0b 181MyCanvas::~MyCanvas()
01111366 182{
60a41aee
RR
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;
01111366
RR
192}
193
194void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
195{
196 wxPaintDC dc( this );
197 PrepareDC( dc );
a91b47e8 198
9390a202 199 dc.DrawText( "Loaded image", 30, 10 );
a91b47e8 200 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
f048e32f 201
9390a202 202 dc.DrawText( "Drawn directly", 150, 10 );
910276d7 203 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
e8fdc264 204 dc.SetPen( *wxWHITE_PEN );
9390a202
RR
205 dc.DrawRectangle( 150, 30, 100, 100 );
206
a91b47e8 207 if (my_anti && my_anti->Ok()) dc.DrawBitmap( *my_anti, 250, 140 );
f048e32f 208
e1929140
RR
209 dc.DrawText( "PNG handler", 30, 135 );
210 if (my_horse_png && my_horse_png->Ok()) dc.DrawBitmap( *my_horse_png, 30, 150 );
f048e32f 211
e1929140
RR
212 dc.DrawText( "JPEG handler", 30, 365 );
213 if (my_horse_jpeg && my_horse_jpeg->Ok()) dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
f048e32f 214
e1929140
RR
215 dc.DrawText( "GIF handler", 30, 595 );
216 if (my_horse_gif && my_horse_gif->Ok()) dc.DrawBitmap( *my_horse_gif, 30, 610 );
cbd4be25
GRG
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 );
6319afe3
GL
223
224 dc.DrawText( "PNM handler", 30, 1285 );
225 if (my_horse_pnm && my_horse_pnm->Ok()) dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
60a41aee 226
0b72db08
RR
227 dc.DrawText( "TIFF handler", 30, 1515 );
228 if (my_horse_tiff && my_horse_tiff->Ok()) dc.DrawBitmap( *my_horse_pnm, 30, 1530 );
9390a202 229}
1d5b7a0b 230
9390a202
RR
231void MyCanvas::CreateAntiAliasedBitmap()
232{
233 wxBitmap bitmap( 300, 300 );
a91b47e8 234
9390a202 235 wxMemoryDC dc;
a91b47e8 236
9390a202 237 dc.SelectObject( bitmap );
a91b47e8 238
9390a202 239 dc.Clear();
f048e32f 240
6e47faf1 241 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
9390a202
RR
242 dc.SetTextForeground( "RED" );
243 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
244 dc.DrawText( "And a Rectangle.", 20, 60 );
f048e32f 245
9390a202 246 dc.SetBrush( *wxRED_BRUSH );
91b8de8d 247 dc.SetPen( *wxTRANSPARENT_PEN );
9390a202 248 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
f048e32f 249
9390a202
RR
250 wxImage original( bitmap );
251 wxImage anti( 150, 150 );
a91b47e8 252
9390a202 253 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
f048e32f 254
9390a202
RR
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 ) +
f048e32f
VZ
259 original.GetRed( x*2-1, y*2 ) +
260 original.GetRed( x*2, y*2+1 ) +
9390a202 261 original.GetRed( x*2+1, y*2+1 );
f048e32f
VZ
262 red = red/4;
263
9390a202 264 int green = original.GetGreen( x*2, y*2 ) +
f048e32f
VZ
265 original.GetGreen( x*2-1, y*2 ) +
266 original.GetGreen( x*2, y*2+1 ) +
9390a202 267 original.GetGreen( x*2+1, y*2+1 );
f048e32f
VZ
268 green = green/4;
269
9390a202 270 int blue = original.GetBlue( x*2, y*2 ) +
f048e32f
VZ
271 original.GetBlue( x*2-1, y*2 ) +
272 original.GetBlue( x*2, y*2+1 ) +
9390a202 273 original.GetBlue( x*2+1, y*2+1 );
f048e32f 274 blue = blue/4;
9390a202
RR
275 anti.SetRGB( x, y, red, green, blue );
276 }
277 my_anti = new wxBitmap( anti.ConvertToBitmap() );
01111366
RR
278}
279
280// MyFrame
281
79490c3d
VZ
282const int ID_QUIT = 108;
283const int ID_ABOUT = 109;
01111366
RR
284
285IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
286
287BEGIN_EVENT_TABLE(MyFrame,wxFrame)
288 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
289 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
290END_EVENT_TABLE()
291
1d5b7a0b
VZ
292MyFrame::MyFrame()
293 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
294 wxPoint(20,20), wxSize(470,360) )
01111366
RR
295{
296 wxMenu *file_menu = new wxMenu();
90e58684
RR
297 file_menu->Append( ID_ABOUT, "&About..");
298 file_menu->Append( ID_QUIT, "E&xit");
1d5b7a0b 299
01111366 300 wxMenuBar *menu_bar = new wxMenuBar();
90e58684 301 menu_bar->Append(file_menu, "&File");
3d05544e 302
01111366 303 SetMenuBar( menu_bar );
1d5b7a0b 304
917e533b
RR
305 CreateStatusBar(2);
306 int widths[] = { -1, 100 };
307 SetStatusWidths( 2, widths );
1d5b7a0b 308
fb1585ae 309 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
cbd4be25
GRG
310
311 // 500 width * 1300 height
60a41aee 312 m_canvas->SetScrollbars( 10, 10, 50, 180 );
01111366
RR
313}
314
315void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
316{
317 Close( TRUE );
318}
319
320void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
321{
1d5b7a0b 322 (void)wxMessageBox( "wxImage demo\n"
8a5137d7 323 "\n"
1d5b7a0b
VZ
324 "Robert Roebling (c) 1998",
325 "About wxImage Demo", wxICON_INFORMATION | wxOK );
fb1585ae
RR
326}
327
01111366
RR
328//-----------------------------------------------------------------------------
329// MyApp
330//-----------------------------------------------------------------------------
331
1d5b7a0b 332bool MyApp::OnInit()
01111366 333{
e9c4b1a2
JS
334#if wxUSE_LIBPNG
335 wxImage::AddHandler( new wxPNGHandler );
336#endif
337
338#if wxUSE_LIBJPEG
339 wxImage::AddHandler( new wxJPEGHandler );
340#endif
341
60a41aee
RR
342#if wxUSE_LIBTIFF
343 wxImage::AddHandler( new wxTIFFHandler );
344#endif
345
6e47faf1 346#if wxUSE_GIF
e1929140 347 wxImage::AddHandler( new wxGIFHandler );
6e47faf1
JS
348#endif
349
350#if wxUSE_PCX
cbd4be25 351 wxImage::AddHandler( new wxPCXHandler );
6e47faf1
JS
352#endif
353
354#if wxUSE_PNM
6319afe3 355 wxImage::AddHandler( new wxPNMHandler );
6e47faf1 356#endif
e1929140 357
01111366
RR
358 wxFrame *frame = new MyFrame();
359 frame->Show( TRUE );
1d5b7a0b 360
01111366
RR
361 return TRUE;
362}
363