]>
git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
24306d42a60cfcc1df29b5d122b87f5a80cea45b
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
;
48 DECLARE_DYNAMIC_CLASS(MyCanvas
)
54 class MyFrame
: public wxFrame
59 void OnAbout( wxCommandEvent
&event
);
60 void OnQuit( wxCommandEvent
&event
);
64 DECLARE_DYNAMIC_CLASS(MyFrame
)
70 class MyApp
: public wxApp
73 virtual bool OnInit();
82 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
84 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
85 EVT_PAINT(MyCanvas::OnPaint
)
88 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
89 const wxPoint
&pos
, const wxSize
&size
)
90 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
92 my_horse_png
= (wxBitmap
*) NULL
;
93 my_horse_jpeg
= (wxBitmap
*) NULL
;
94 my_horse_gif
= (wxBitmap
*) NULL
;
95 my_horse_bmp
= (wxBitmap
*) NULL
;
96 my_square
= (wxBitmap
*) NULL
;
97 my_anti
= (wxBitmap
*) NULL
;
99 SetBackgroundColour(* wxWHITE
);
101 wxBitmap
bitmap( 100, 100 );
104 dc
.SelectObject( bitmap
);
105 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
106 dc
.SetPen( *wxWHITE_PEN
);
107 dc
.DrawRectangle( 0, 0, 100, 100 );
108 dc
.SelectObject( wxNullBitmap
);
110 // try to find the directory with our images
112 if ( wxFile::Exists("./horse.png") )
114 else if ( wxFile::Exists("../horse.png") )
117 wxLogWarning("Can't find image files in either '.' or '..'!");
119 wxImage
image( bitmap
);
121 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
) )
122 wxLogError("Can't save file");
124 if ( !image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
) )
125 wxLogError("Can't load PNG image");
127 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
129 if ( !image
.LoadFile( dir
+ wxString("horse.jpg") ) )
130 wxLogError("Can't load JPG image");
132 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
134 if ( !image
.LoadFile( dir
+ wxString("horse.gif") ) )
135 wxLogError("Can't load GIF image");
137 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
139 if ( !image
.LoadFile( dir
+ wxString("horse.bmp") ) )
140 wxLogError("Can't load BMP image");
142 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
144 image
.LoadFile( dir
+ wxString("test.png") );
145 my_square
= new wxBitmap( image
.ConvertToBitmap() );
147 CreateAntiAliasedBitmap();
150 MyCanvas::~MyCanvas()
153 delete my_horse_jpeg
;
160 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
162 wxPaintDC
dc( this );
165 dc
.DrawText( "Loaded image", 30, 10 );
166 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
168 dc
.DrawText( "Drawn directly", 150, 10 );
169 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
170 dc
.SetPen( *wxWHITE_PEN
);
171 dc
.DrawRectangle( 150, 30, 100, 100 );
173 if (my_anti
&& my_anti
->Ok()) dc
.DrawBitmap( *my_anti
, 250, 140 );
175 dc
.DrawText( "PNG handler", 30, 135 );
176 if (my_horse_png
&& my_horse_png
->Ok()) dc
.DrawBitmap( *my_horse_png
, 30, 150 );
178 dc
.DrawText( "JPEG handler", 30, 365 );
179 if (my_horse_jpeg
&& my_horse_jpeg
->Ok()) dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
181 dc
.DrawText( "GIF handler", 30, 595 );
182 if (my_horse_gif
&& my_horse_gif
->Ok()) dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
184 dc
.DrawText( "BMP handler", 30, 815 );
185 if (my_horse_bmp
&& my_horse_bmp
->Ok()) dc
.DrawBitmap( *my_horse_bmp
, 30, 830 );
188 void MyCanvas::CreateAntiAliasedBitmap()
190 wxBitmap
bitmap( 300, 300 );
194 dc
.SelectObject( bitmap
);
198 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxDEFAULT
, wxDEFAULT
) );
199 dc
.SetTextForeground( "RED" );
200 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
201 dc
.DrawText( "And a Rectangle.", 20, 60 );
203 dc
.SetBrush( *wxRED_BRUSH
);
204 dc
.SetPen( *wxTRANSPARENT_PEN
);
205 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
207 wxImage
original( bitmap
);
208 wxImage
anti( 150, 150 );
210 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
212 for (int y
= 1; y
< 149; y
++)
213 for (int x
= 1; x
< 149; x
++)
215 int red
= original
.GetRed( x
*2, y
*2 ) +
216 original
.GetRed( x
*2-1, y
*2 ) +
217 original
.GetRed( x
*2, y
*2+1 ) +
218 original
.GetRed( x
*2+1, y
*2+1 );
221 int green
= original
.GetGreen( x
*2, y
*2 ) +
222 original
.GetGreen( x
*2-1, y
*2 ) +
223 original
.GetGreen( x
*2, y
*2+1 ) +
224 original
.GetGreen( x
*2+1, y
*2+1 );
227 int blue
= original
.GetBlue( x
*2, y
*2 ) +
228 original
.GetBlue( x
*2-1, y
*2 ) +
229 original
.GetBlue( x
*2, y
*2+1 ) +
230 original
.GetBlue( x
*2+1, y
*2+1 );
232 anti
.SetRGB( x
, y
, red
, green
, blue
);
234 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
239 const int ID_QUIT
= 108;
240 const int ID_ABOUT
= 109;
242 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
244 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
245 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
246 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
250 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
251 wxPoint(20,20), wxSize(470,360) )
253 wxMenu
*file_menu
= new wxMenu();
254 file_menu
->Append( ID_ABOUT
, "&About..");
255 file_menu
->Append( ID_QUIT
, "E&xit");
257 wxMenuBar
*menu_bar
= new wxMenuBar();
258 menu_bar
->Append(file_menu
, "&File");
260 SetMenuBar( menu_bar
);
263 int widths
[] = { -1, 100 };
264 SetStatusWidths( 2, widths
);
266 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
267 m_canvas
->SetScrollbars( 10, 10, 50, 100 );
270 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
275 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
277 (void)wxMessageBox( "wxImage demo\n"
279 "Robert Roebling (c) 1998",
280 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
283 //-----------------------------------------------------------------------------
285 //-----------------------------------------------------------------------------
290 wxImage::AddHandler( new wxPNGHandler
);
294 wxImage::AddHandler( new wxJPEGHandler
);
297 wxImage::AddHandler( new wxGIFHandler
);
299 wxFrame
*frame
= new MyFrame();