]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
more tests 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;
e8fdc264 44 wxBitmap *my_square;
9390a202 45 wxBitmap *my_anti;
1d5b7a0b
VZ
46
47 DECLARE_DYNAMIC_CLASS(MyCanvas)
48 DECLARE_EVENT_TABLE()
01111366
RR
49};
50
51// MyFrame
52
53class MyFrame: public wxFrame
54{
1d5b7a0b
VZ
55public:
56 MyFrame();
01111366 57
01111366
RR
58 void OnAbout( wxCommandEvent &event );
59 void OnQuit( wxCommandEvent &event );
1d5b7a0b 60
01111366 61 MyCanvas *m_canvas;
1d5b7a0b
VZ
62
63 DECLARE_DYNAMIC_CLASS(MyFrame)
64 DECLARE_EVENT_TABLE()
01111366
RR
65};
66
67// MyApp
68
69class MyApp: public wxApp
70{
1d5b7a0b
VZ
71public:
72 virtual bool OnInit();
01111366
RR
73};
74
75// main program
76
77IMPLEMENT_APP(MyApp)
78
79// MyCanvas
80
81IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
82
1d5b7a0b
VZ
83BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
84 EVT_PAINT(MyCanvas::OnPaint)
01111366
RR
85END_EVENT_TABLE()
86
acbd13a3 87MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
1d5b7a0b
VZ
88 const wxPoint &pos, const wxSize &size )
89 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
01111366 90{
e1929140
RR
91 my_horse_png = (wxBitmap*) NULL;
92 my_horse_jpeg = (wxBitmap*) NULL;
93 my_horse_gif = (wxBitmap*) NULL;
a91b47e8
JS
94 my_square = (wxBitmap*) NULL;
95 my_anti = (wxBitmap*) NULL;
96
3d05544e
JS
97 SetBackgroundColour(* wxWHITE);
98
dc86cb34 99 wxBitmap bitmap( 100, 100 );
1d5b7a0b 100
dc86cb34
RR
101 wxMemoryDC dc;
102 dc.SelectObject( bitmap );
910276d7 103 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
e8fdc264 104 dc.SetPen( *wxWHITE_PEN );
dc86cb34
RR
105 dc.DrawRectangle( 0, 0, 100, 100 );
106 dc.SelectObject( wxNullBitmap );
3d05544e 107
a23fd0e1
VZ
108 // try to find the directory with our images
109 wxString dir;
110 if ( wxFile::Exists("./horse.png") )
111 dir = "./";
112 else if ( wxFile::Exists("../horse.png") )
113 dir = "../";
114 else
115 wxLogWarning("Can't find image files in either '.' or '..'!");
3d05544e 116
8b53e5a2 117 wxImage image( bitmap );
a23fd0e1
VZ
118 if ( !image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ) )
119 wxLogError("Can't save file");
b75867a6 120
a23fd0e1
VZ
121 if ( !image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG ) )
122 wxLogError("Can't load PNG image");
123 else
124 my_horse_png = new wxBitmap( image.ConvertToBitmap() );
e1929140 125
a23fd0e1
VZ
126 if ( !image.LoadFile( dir + wxString("horse.jpg"), wxBITMAP_TYPE_JPEG ) )
127 wxLogError("Can't load JPG image");
128 else
129 my_horse_jpeg = new wxBitmap( image.ConvertToBitmap() );
e1929140 130
a23fd0e1
VZ
131 if ( !image.LoadFile( dir + wxString("horse.gif"), wxBITMAP_TYPE_GIF ) )
132 wxLogError("Can't load GIF image");
133 else
134 my_horse_gif = new wxBitmap( image.ConvertToBitmap() );
b75867a6 135
3d05544e 136 image.LoadFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
8b53e5a2 137 my_square = new wxBitmap( image.ConvertToBitmap() );
96d5ab4d 138
9390a202 139 CreateAntiAliasedBitmap();
01111366
RR
140}
141
1d5b7a0b 142MyCanvas::~MyCanvas()
01111366 143{
e1929140
RR
144 delete my_horse_png;
145 delete my_horse_jpeg;
146 delete my_horse_gif;
e8fdc264 147 delete my_square;
9390a202 148 delete my_anti;
01111366
RR
149}
150
151void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
152{
153 wxPaintDC dc( this );
154 PrepareDC( dc );
a91b47e8 155
9390a202 156 dc.DrawText( "Loaded image", 30, 10 );
a91b47e8 157 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
eefa26be 158
9390a202 159 dc.DrawText( "Drawn directly", 150, 10 );
910276d7 160 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
e8fdc264 161 dc.SetPen( *wxWHITE_PEN );
9390a202
RR
162 dc.DrawRectangle( 150, 30, 100, 100 );
163
a91b47e8 164 if (my_anti && my_anti->Ok()) dc.DrawBitmap( *my_anti, 250, 140 );
6e13c196 165
e1929140
RR
166 dc.DrawText( "PNG handler", 30, 135 );
167 if (my_horse_png && my_horse_png->Ok()) dc.DrawBitmap( *my_horse_png, 30, 150 );
168
169 dc.DrawText( "JPEG handler", 30, 365 );
170 if (my_horse_jpeg && my_horse_jpeg->Ok()) dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
171
172 dc.DrawText( "GIF handler", 30, 595 );
173 if (my_horse_gif && my_horse_gif->Ok()) dc.DrawBitmap( *my_horse_gif, 30, 610 );
9390a202 174}
1d5b7a0b 175
9390a202
RR
176void MyCanvas::CreateAntiAliasedBitmap()
177{
178 wxBitmap bitmap( 300, 300 );
a91b47e8 179
9390a202 180 wxMemoryDC dc;
a91b47e8 181
9390a202 182 dc.SelectObject( bitmap );
a91b47e8 183
9390a202
RR
184 dc.Clear();
185
186 dc.SetFont( wxFont( 24, wxDECORATIVE, wxDEFAULT, wxDEFAULT ) );
187 dc.SetTextForeground( "RED" );
188 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
189 dc.DrawText( "And a Rectangle.", 20, 60 );
190
191 dc.SetBrush( *wxRED_BRUSH );
192 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
193
194 wxImage original( bitmap );
195 wxImage anti( 150, 150 );
a91b47e8 196
9390a202
RR
197 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
198
199 for (int y = 1; y < 149; y++)
200 for (int x = 1; x < 149; x++)
201 {
202 int red = original.GetRed( x*2, y*2 ) +
203 original.GetRed( x*2-1, y*2 ) +
204 original.GetRed( x*2, y*2+1 ) +
205 original.GetRed( x*2+1, y*2+1 );
206 red = red/4;
207
208 int green = original.GetGreen( x*2, y*2 ) +
209 original.GetGreen( x*2-1, y*2 ) +
210 original.GetGreen( x*2, y*2+1 ) +
211 original.GetGreen( x*2+1, y*2+1 );
212 green = green/4;
213
214 int blue = original.GetBlue( x*2, y*2 ) +
215 original.GetBlue( x*2-1, y*2 ) +
216 original.GetBlue( x*2, y*2+1 ) +
217 original.GetBlue( x*2+1, y*2+1 );
218 blue = blue/4;
219 anti.SetRGB( x, y, red, green, blue );
220 }
221 my_anti = new wxBitmap( anti.ConvertToBitmap() );
01111366
RR
222}
223
224// MyFrame
225
79490c3d
VZ
226const int ID_QUIT = 108;
227const int ID_ABOUT = 109;
01111366
RR
228
229IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
230
231BEGIN_EVENT_TABLE(MyFrame,wxFrame)
232 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
233 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
234END_EVENT_TABLE()
235
1d5b7a0b
VZ
236MyFrame::MyFrame()
237 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
238 wxPoint(20,20), wxSize(470,360) )
01111366
RR
239{
240 wxMenu *file_menu = new wxMenu();
90e58684
RR
241 file_menu->Append( ID_ABOUT, "&About..");
242 file_menu->Append( ID_QUIT, "E&xit");
1d5b7a0b 243
01111366 244 wxMenuBar *menu_bar = new wxMenuBar();
90e58684 245 menu_bar->Append(file_menu, "&File");
3d05544e 246
01111366 247 SetMenuBar( menu_bar );
1d5b7a0b 248
917e533b
RR
249 CreateStatusBar(2);
250 int widths[] = { -1, 100 };
251 SetStatusWidths( 2, widths );
1d5b7a0b 252
fb1585ae 253 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
e1929140 254 m_canvas->SetScrollbars( 10, 10, 50, 100 );
01111366
RR
255}
256
257void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
258{
259 Close( TRUE );
260}
261
262void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
263{
1d5b7a0b
VZ
264 (void)wxMessageBox( "wxImage demo\n"
265 "Robert Roebling (c) 1998",
266 "About wxImage Demo", wxICON_INFORMATION | wxOK );
fb1585ae
RR
267}
268
01111366
RR
269//-----------------------------------------------------------------------------
270// MyApp
271//-----------------------------------------------------------------------------
272
1d5b7a0b 273bool MyApp::OnInit()
01111366 274{
e9c4b1a2
JS
275#if wxUSE_LIBPNG
276 wxImage::AddHandler( new wxPNGHandler );
277#endif
278
279#if wxUSE_LIBJPEG
280 wxImage::AddHandler( new wxJPEGHandler );
281#endif
282
e1929140
RR
283 wxImage::AddHandler( new wxGIFHandler );
284
01111366
RR
285 wxFrame *frame = new MyFrame();
286 frame->Show( TRUE );
1d5b7a0b 287
01111366
RR
288 return TRUE;
289}
290