]>
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"
34 class MyCanvas
: public wxScrolledWindow
38 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
40 void OnPaint( wxPaintEvent
&event
);
41 void CreateAntiAliasedBitmap();
43 wxBitmap
*my_horse_png
;
44 wxBitmap
*my_horse_jpeg
;
45 wxBitmap
*my_horse_gif
;
46 wxBitmap
*my_horse_bmp
;
47 wxBitmap
*my_horse_pcx
;
48 wxBitmap
*my_horse_pnm
;
49 wxBitmap
*my_horse_tiff
;
50 wxBitmap
*my_smile_xbm
;
55 DECLARE_DYNAMIC_CLASS(MyCanvas
)
61 class MyFrame
: public wxFrame
66 void OnAbout( wxCommandEvent
&event
);
67 void OnQuit( wxCommandEvent
&event
);
72 DECLARE_DYNAMIC_CLASS(MyFrame
)
78 class MyApp
: public wxApp
81 virtual bool OnInit();
90 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
92 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
93 EVT_PAINT(MyCanvas::OnPaint
)
96 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
97 const wxPoint
&pos
, const wxSize
&size
)
98 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
100 my_horse_png
= (wxBitmap
*) NULL
;
101 my_horse_jpeg
= (wxBitmap
*) NULL
;
102 my_horse_gif
= (wxBitmap
*) NULL
;
103 my_horse_bmp
= (wxBitmap
*) NULL
;
104 my_horse_pcx
= (wxBitmap
*) NULL
;
105 my_horse_pnm
= (wxBitmap
*) NULL
;
106 my_horse_tiff
= (wxBitmap
*) NULL
;
107 my_smile_xbm
= (wxBitmap
*) NULL
;
108 my_square
= (wxBitmap
*) NULL
;
109 my_anti
= (wxBitmap
*) NULL
;
111 SetBackgroundColour(* wxWHITE
);
113 wxBitmap
bitmap( 100, 100 );
116 dc
.SelectObject( bitmap
);
117 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
118 dc
.SetPen( *wxWHITE_PEN
);
119 dc
.DrawRectangle( 0, 0, 100, 100 );
120 dc
.SelectObject( wxNullBitmap
);
122 // try to find the directory with our images
124 if ( wxFile::Exists("./horse.png") )
126 else if ( wxFile::Exists("../horse.png") )
129 wxLogWarning("Can't find image files in either '.' or '..'!");
131 wxImage
image( bitmap
);
133 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
) )
134 wxLogError("Can't save file");
136 if ( !image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
) )
137 wxLogError("Can't load PNG image");
139 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
141 if ( !image
.LoadFile( dir
+ wxString("horse.jpg") ) )
142 wxLogError("Can't load JPG image");
144 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
147 if ( !image
.LoadFile( dir
+ wxString("horse.gif") ) )
148 wxLogError("Can't load GIF image");
150 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
154 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
155 wxLogError("Can't load PCX image");
157 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
160 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
161 wxLogError("Can't load BMP image");
163 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
166 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
167 wxLogError("Can't load PNM image");
169 my_horse_pnm
= new wxBitmap( image
.ConvertToBitmap() );
173 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
174 wxLogError("Can't load TIFF image");
176 my_horse_tiff
= new wxBitmap( image
.ConvertToBitmap() );
179 image
.LoadFile( dir
+ wxString("test.png") );
180 my_square
= new wxBitmap( image
.ConvertToBitmap() );
182 CreateAntiAliasedBitmap();
184 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
188 MyCanvas::~MyCanvas()
192 delete my_horse_jpeg
;
196 delete my_horse_tiff
;
202 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
204 wxPaintDC
dc( this );
207 dc
.DrawText( "Loaded image", 30, 10 );
208 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
210 dc
.DrawText( "Drawn directly", 150, 10 );
211 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
212 dc
.SetPen( *wxWHITE_PEN
);
213 dc
.DrawRectangle( 150, 30, 100, 100 );
215 if (my_anti
&& my_anti
->Ok()) dc
.DrawBitmap( *my_anti
, 280, 30 );
217 dc
.DrawText( "PNG handler", 30, 135 );
218 if (my_horse_png
&& my_horse_png
->Ok())
220 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
221 wxRect
rect(0,0,100,100);
222 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
223 dc
.DrawText( "GetSubBitmap()", 280, 190 );
224 dc
.DrawBitmap( sub
, 280, 210 );
227 dc
.DrawText( "JPEG handler", 30, 365 );
228 if (my_horse_jpeg
&& my_horse_jpeg
->Ok()) dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
230 dc
.DrawText( "GIF handler", 30, 595 );
231 if (my_horse_gif
&& my_horse_gif
->Ok()) dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
233 dc
.DrawText( "PCX handler", 30, 825 );
234 if (my_horse_pcx
&& my_horse_pcx
->Ok()) dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
236 dc
.DrawText( "BMP handler", 30, 1055 );
237 if (my_horse_bmp
&& my_horse_bmp
->Ok()) dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
239 dc
.DrawText( "PNM handler", 30, 1285 );
240 if (my_horse_pnm
&& my_horse_pnm
->Ok()) dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
242 dc
.DrawText( "TIFF handler", 30, 1515 );
243 if (my_horse_tiff
&& my_horse_tiff
->Ok()) dc
.DrawBitmap( *my_horse_pnm
, 30, 1530 );
245 dc
.DrawText( "XBM bitmap", 30, 1745 );
246 dc
.SetPen( *wxRED_PEN
);
247 if (my_smile_xbm
&& my_smile_xbm
->Ok()) {
248 dc
.DrawBitmap( *my_smile_xbm
, 30, 1760 );
249 dc
.DrawText( "..after wxImage conversion", 150, 1745 );
250 wxImage
i( *my_smile_xbm
);
251 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1760 );
255 void MyCanvas::CreateAntiAliasedBitmap()
257 wxBitmap
bitmap( 300, 300 );
261 dc
.SelectObject( bitmap
);
265 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
266 dc
.SetTextForeground( "RED" );
267 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
268 dc
.DrawText( "And a Rectangle.", 20, 60 );
270 dc
.SetBrush( *wxRED_BRUSH
);
271 dc
.SetPen( *wxTRANSPARENT_PEN
);
272 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
274 wxImage
original( bitmap
);
275 wxImage
anti( 150, 150 );
277 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
279 for (int y
= 1; y
< 149; y
++)
280 for (int x
= 1; x
< 149; x
++)
282 int red
= original
.GetRed( x
*2, y
*2 ) +
283 original
.GetRed( x
*2-1, y
*2 ) +
284 original
.GetRed( x
*2, y
*2+1 ) +
285 original
.GetRed( x
*2+1, y
*2+1 );
288 int green
= original
.GetGreen( x
*2, y
*2 ) +
289 original
.GetGreen( x
*2-1, y
*2 ) +
290 original
.GetGreen( x
*2, y
*2+1 ) +
291 original
.GetGreen( x
*2+1, y
*2+1 );
294 int blue
= original
.GetBlue( x
*2, y
*2 ) +
295 original
.GetBlue( x
*2-1, y
*2 ) +
296 original
.GetBlue( x
*2, y
*2+1 ) +
297 original
.GetBlue( x
*2+1, y
*2+1 );
299 anti
.SetRGB( x
, y
, red
, green
, blue
);
301 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
306 const int ID_QUIT
= 108;
307 const int ID_ABOUT
= 109;
309 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
311 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
312 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
313 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
317 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
318 wxPoint(20,20), wxSize(470,360) )
320 wxMenu
*file_menu
= new wxMenu();
321 file_menu
->Append( ID_ABOUT
, "&About..");
322 file_menu
->Append( ID_QUIT
, "E&xit");
324 wxMenuBar
*menu_bar
= new wxMenuBar();
325 menu_bar
->Append(file_menu
, "&File");
327 SetMenuBar( menu_bar
);
330 int widths
[] = { -1, 100 };
331 SetStatusWidths( 2, widths
);
333 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
335 // 500 width * 1900 height
336 m_canvas
->SetScrollbars( 10, 10, 50, 190 );
339 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
344 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
346 (void)wxMessageBox( "wxImage demo\n"
348 "Robert Roebling (c) 1998",
349 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
352 //-----------------------------------------------------------------------------
354 //-----------------------------------------------------------------------------
359 wxImage::AddHandler( new wxPNGHandler
);
363 wxImage::AddHandler( new wxJPEGHandler
);
367 wxImage::AddHandler( new wxTIFFHandler
);
371 wxImage::AddHandler( new wxGIFHandler
);
375 wxImage::AddHandler( new wxPCXHandler
);
379 wxImage::AddHandler( new wxPNMHandler
);
382 wxFrame
*frame
= new MyFrame();