]>
git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
4 * Author: Robert Roebling
6 * Copyright: (C) 1998, Robert Roebling
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
32 class MyCanvas
: public wxScrolledWindow
36 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
38 void OnPaint( wxPaintEvent
&event
);
39 void CreateAntiAliasedBitmap();
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
;
50 DECLARE_DYNAMIC_CLASS(MyCanvas
)
56 class MyFrame
: public wxFrame
61 void OnAbout( wxCommandEvent
&event
);
62 void OnQuit( wxCommandEvent
&event
);
66 DECLARE_DYNAMIC_CLASS(MyFrame
)
72 class MyApp
: public wxApp
75 virtual bool OnInit();
84 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
86 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
87 EVT_PAINT(MyCanvas::OnPaint
)
90 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
91 const wxPoint
&pos
, const wxSize
&size
)
92 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
94 my_horse_png
= (wxBitmap
*) NULL
;
95 my_horse_jpeg
= (wxBitmap
*) NULL
;
96 my_horse_gif
= (wxBitmap
*) NULL
;
97 my_horse_bmp
= (wxBitmap
*) NULL
;
98 my_horse_pcx
= (wxBitmap
*) NULL
;
99 my_horse_pnm
= (wxBitmap
*) NULL
;
100 my_square
= (wxBitmap
*) NULL
;
101 my_anti
= (wxBitmap
*) NULL
;
103 SetBackgroundColour(* wxWHITE
);
105 wxBitmap
bitmap( 100, 100 );
108 dc
.SelectObject( bitmap
);
109 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
110 dc
.SetPen( *wxWHITE_PEN
);
111 dc
.DrawRectangle( 0, 0, 100, 100 );
112 dc
.SelectObject( wxNullBitmap
);
114 // try to find the directory with our images
116 if ( wxFile::Exists("./horse.png") )
118 else if ( wxFile::Exists("../horse.png") )
121 wxLogWarning("Can't find image files in either '.' or '..'!");
123 wxImage
image( bitmap
);
125 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
) )
126 wxLogError("Can't save file");
128 if ( !image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
) )
129 wxLogError("Can't load PNG image");
131 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
133 if ( !image
.LoadFile( dir
+ wxString("horse.jpg") ) )
134 wxLogError("Can't load JPG image");
136 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
139 if ( !image
.LoadFile( dir
+ wxString("horse.gif") ) )
140 wxLogError("Can't load GIF image");
142 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
146 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
147 wxLogError("Can't load PCX image");
149 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
152 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
153 wxLogError("Can't load BMP image");
155 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
158 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
159 wxLogError("Can't load PNM image");
161 my_horse_pnm
= new wxBitmap( image
.ConvertToBitmap() );
164 image
.LoadFile( dir
+ wxString("test.png") );
165 my_square
= new wxBitmap( image
.ConvertToBitmap() );
167 CreateAntiAliasedBitmap();
170 MyCanvas::~MyCanvas()
174 delete my_horse_jpeg
;
182 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
184 wxPaintDC
dc( this );
187 dc
.DrawText( "Loaded image", 30, 10 );
188 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
190 dc
.DrawText( "Drawn directly", 150, 10 );
191 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
192 dc
.SetPen( *wxWHITE_PEN
);
193 dc
.DrawRectangle( 150, 30, 100, 100 );
195 if (my_anti
&& my_anti
->Ok()) dc
.DrawBitmap( *my_anti
, 250, 140 );
197 dc
.DrawText( "PNG handler", 30, 135 );
198 if (my_horse_png
&& my_horse_png
->Ok()) dc
.DrawBitmap( *my_horse_png
, 30, 150 );
200 dc
.DrawText( "JPEG handler", 30, 365 );
201 if (my_horse_jpeg
&& my_horse_jpeg
->Ok()) dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
203 dc
.DrawText( "GIF handler", 30, 595 );
204 if (my_horse_gif
&& my_horse_gif
->Ok()) dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
206 dc
.DrawText( "PCX handler", 30, 825 );
207 if (my_horse_pcx
&& my_horse_pcx
->Ok()) dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
209 dc
.DrawText( "BMP handler", 30, 1055 );
210 if (my_horse_bmp
&& my_horse_bmp
->Ok()) dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
212 dc
.DrawText( "PNM handler", 30, 1285 );
213 if (my_horse_pnm
&& my_horse_pnm
->Ok()) dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
216 void MyCanvas::CreateAntiAliasedBitmap()
218 wxBitmap
bitmap( 300, 300 );
222 dc
.SelectObject( bitmap
);
226 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
227 dc
.SetTextForeground( "RED" );
228 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
229 dc
.DrawText( "And a Rectangle.", 20, 60 );
231 dc
.SetBrush( *wxRED_BRUSH
);
232 dc
.SetPen( *wxTRANSPARENT_PEN
);
233 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
235 wxImage
original( bitmap
);
236 wxImage
anti( 150, 150 );
238 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
240 for (int y
= 1; y
< 149; y
++)
241 for (int x
= 1; x
< 149; x
++)
243 int red
= original
.GetRed( x
*2, y
*2 ) +
244 original
.GetRed( x
*2-1, y
*2 ) +
245 original
.GetRed( x
*2, y
*2+1 ) +
246 original
.GetRed( x
*2+1, y
*2+1 );
249 int green
= original
.GetGreen( x
*2, y
*2 ) +
250 original
.GetGreen( x
*2-1, y
*2 ) +
251 original
.GetGreen( x
*2, y
*2+1 ) +
252 original
.GetGreen( x
*2+1, y
*2+1 );
255 int blue
= original
.GetBlue( x
*2, y
*2 ) +
256 original
.GetBlue( x
*2-1, y
*2 ) +
257 original
.GetBlue( x
*2, y
*2+1 ) +
258 original
.GetBlue( x
*2+1, y
*2+1 );
260 anti
.SetRGB( x
, y
, red
, green
, blue
);
262 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
267 const int ID_QUIT
= 108;
268 const int ID_ABOUT
= 109;
270 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
272 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
273 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
274 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
278 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
279 wxPoint(20,20), wxSize(470,360) )
281 wxMenu
*file_menu
= new wxMenu();
282 file_menu
->Append( ID_ABOUT
, "&About..");
283 file_menu
->Append( ID_QUIT
, "E&xit");
285 wxMenuBar
*menu_bar
= new wxMenuBar();
286 menu_bar
->Append(file_menu
, "&File");
288 SetMenuBar( menu_bar
);
291 int widths
[] = { -1, 100 };
292 SetStatusWidths( 2, widths
);
294 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
296 // 500 width * 1300 height
297 m_canvas
->SetScrollbars( 10, 10, 50, 152 );
300 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
305 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
307 (void)wxMessageBox( "wxImage demo\n"
309 "Robert Roebling (c) 1998",
310 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
313 //-----------------------------------------------------------------------------
315 //-----------------------------------------------------------------------------
320 wxImage::AddHandler( new wxPNGHandler
);
324 wxImage::AddHandler( new wxJPEGHandler
);
328 wxImage::AddHandler( new wxGIFHandler
);
332 wxImage::AddHandler( new wxPCXHandler
);
336 wxImage::AddHandler( new wxPNMHandler
);
339 wxFrame
*frame
= new MyFrame();