]>
git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
d0d48d451d329ab023ac71db7e724e6503da9a28
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
;
49 DECLARE_DYNAMIC_CLASS(MyCanvas
)
55 class MyFrame
: public wxFrame
60 void OnAbout( wxCommandEvent
&event
);
61 void OnQuit( wxCommandEvent
&event
);
65 DECLARE_DYNAMIC_CLASS(MyFrame
)
71 class MyApp
: public wxApp
74 virtual bool OnInit();
83 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
85 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
86 EVT_PAINT(MyCanvas::OnPaint
)
89 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
90 const wxPoint
&pos
, const wxSize
&size
)
91 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
93 my_horse_png
= (wxBitmap
*) NULL
;
94 my_horse_jpeg
= (wxBitmap
*) NULL
;
95 my_horse_gif
= (wxBitmap
*) NULL
;
96 my_horse_bmp
= (wxBitmap
*) NULL
;
97 my_horse_pcx
= (wxBitmap
*) NULL
;
98 my_square
= (wxBitmap
*) NULL
;
99 my_anti
= (wxBitmap
*) NULL
;
101 SetBackgroundColour(* wxWHITE
);
103 wxBitmap
bitmap( 100, 100 );
106 dc
.SelectObject( bitmap
);
107 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
108 dc
.SetPen( *wxWHITE_PEN
);
109 dc
.DrawRectangle( 0, 0, 100, 100 );
110 dc
.SelectObject( wxNullBitmap
);
112 // try to find the directory with our images
114 if ( wxFile::Exists("./horse.png") )
116 else if ( wxFile::Exists("../horse.png") )
119 wxLogWarning("Can't find image files in either '.' or '..'!");
121 wxImage
image( bitmap
);
123 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
) )
124 wxLogError("Can't save file");
126 if ( !image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
) )
127 wxLogError("Can't load PNG image");
129 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
131 if ( !image
.LoadFile( dir
+ wxString("horse.jpg") ) )
132 wxLogError("Can't load JPG image");
134 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
136 if ( !image
.LoadFile( dir
+ wxString("horse.gif") ) )
137 wxLogError("Can't load GIF image");
139 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
141 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
142 wxLogError("Can't load PCX image");
144 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
146 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
147 wxLogError("Can't load BMP image");
149 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
151 image
.LoadFile( dir
+ wxString("test.png") );
152 my_square
= new wxBitmap( image
.ConvertToBitmap() );
154 CreateAntiAliasedBitmap();
157 MyCanvas::~MyCanvas()
160 delete my_horse_jpeg
;
168 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
170 wxPaintDC
dc( this );
173 dc
.DrawText( "Loaded image", 30, 10 );
174 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
176 dc
.DrawText( "Drawn directly", 150, 10 );
177 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
178 dc
.SetPen( *wxWHITE_PEN
);
179 dc
.DrawRectangle( 150, 30, 100, 100 );
181 if (my_anti
&& my_anti
->Ok()) dc
.DrawBitmap( *my_anti
, 250, 140 );
183 dc
.DrawText( "PNG handler", 30, 135 );
184 if (my_horse_png
&& my_horse_png
->Ok()) dc
.DrawBitmap( *my_horse_png
, 30, 150 );
186 dc
.DrawText( "JPEG handler", 30, 365 );
187 if (my_horse_jpeg
&& my_horse_jpeg
->Ok()) dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
189 dc
.DrawText( "GIF handler", 30, 595 );
190 if (my_horse_gif
&& my_horse_gif
->Ok()) dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
192 dc
.DrawText( "PCX handler", 30, 825 );
193 if (my_horse_pcx
&& my_horse_pcx
->Ok()) dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
195 dc
.DrawText( "BMP handler", 30, 1055 );
196 if (my_horse_bmp
&& my_horse_bmp
->Ok()) dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
199 void MyCanvas::CreateAntiAliasedBitmap()
201 wxBitmap
bitmap( 300, 300 );
205 dc
.SelectObject( bitmap
);
209 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxDEFAULT
, wxDEFAULT
) );
210 dc
.SetTextForeground( "RED" );
211 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
212 dc
.DrawText( "And a Rectangle.", 20, 60 );
214 dc
.SetBrush( *wxRED_BRUSH
);
215 dc
.SetPen( *wxTRANSPARENT_PEN
);
216 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
218 wxImage
original( bitmap
);
219 wxImage
anti( 150, 150 );
221 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
223 for (int y
= 1; y
< 149; y
++)
224 for (int x
= 1; x
< 149; x
++)
226 int red
= original
.GetRed( x
*2, y
*2 ) +
227 original
.GetRed( x
*2-1, y
*2 ) +
228 original
.GetRed( x
*2, y
*2+1 ) +
229 original
.GetRed( x
*2+1, y
*2+1 );
232 int green
= original
.GetGreen( x
*2, y
*2 ) +
233 original
.GetGreen( x
*2-1, y
*2 ) +
234 original
.GetGreen( x
*2, y
*2+1 ) +
235 original
.GetGreen( x
*2+1, y
*2+1 );
238 int blue
= original
.GetBlue( x
*2, y
*2 ) +
239 original
.GetBlue( x
*2-1, y
*2 ) +
240 original
.GetBlue( x
*2, y
*2+1 ) +
241 original
.GetBlue( x
*2+1, y
*2+1 );
243 anti
.SetRGB( x
, y
, red
, green
, blue
);
245 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
250 const int ID_QUIT
= 108;
251 const int ID_ABOUT
= 109;
253 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
255 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
256 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
257 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
261 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
262 wxPoint(20,20), wxSize(470,360) )
264 wxMenu
*file_menu
= new wxMenu();
265 file_menu
->Append( ID_ABOUT
, "&About..");
266 file_menu
->Append( ID_QUIT
, "E&xit");
268 wxMenuBar
*menu_bar
= new wxMenuBar();
269 menu_bar
->Append(file_menu
, "&File");
271 SetMenuBar( menu_bar
);
274 int widths
[] = { -1, 100 };
275 SetStatusWidths( 2, widths
);
277 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
279 // 500 width * 1300 height
280 m_canvas
->SetScrollbars( 10, 10, 50, 130 );
283 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
288 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
290 (void)wxMessageBox( "wxImage demo\n"
292 "Robert Roebling (c) 1998",
293 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
296 //-----------------------------------------------------------------------------
298 //-----------------------------------------------------------------------------
303 wxImage::AddHandler( new wxPNGHandler
);
307 wxImage::AddHandler( new wxJPEGHandler
);
310 wxImage::AddHandler( new wxGIFHandler
);
311 wxImage::AddHandler( new wxPCXHandler
);
313 wxFrame
*frame
= new MyFrame();