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