]>
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
;
47 DECLARE_DYNAMIC_CLASS(MyCanvas
)
53 class MyFrame
: public wxFrame
58 void OnAbout( wxCommandEvent
&event
);
59 void OnQuit( wxCommandEvent
&event
);
63 DECLARE_DYNAMIC_CLASS(MyFrame
)
69 class MyApp
: public wxApp
72 virtual bool OnInit();
81 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
83 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
84 EVT_PAINT(MyCanvas::OnPaint
)
87 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
88 const wxPoint
&pos
, const wxSize
&size
)
89 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
91 my_horse_png
= (wxBitmap
*) NULL
;
92 my_horse_jpeg
= (wxBitmap
*) NULL
;
93 my_horse_gif
= (wxBitmap
*) NULL
;
94 my_square
= (wxBitmap
*) NULL
;
95 my_anti
= (wxBitmap
*) NULL
;
97 SetBackgroundColour(* wxWHITE
);
99 wxBitmap
bitmap( 100, 100 );
102 dc
.SelectObject( bitmap
);
103 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
104 dc
.SetPen( *wxWHITE_PEN
);
105 dc
.DrawRectangle( 0, 0, 100, 100 );
106 dc
.SelectObject( wxNullBitmap
);
108 // try to find the directory with our images
110 if ( wxFile::Exists("./horse.png") )
112 else if ( wxFile::Exists("../horse.png") )
115 wxLogWarning("Can't find image files in either '.' or '..'!");
117 wxImage
image( bitmap
);
118 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
) )
119 wxLogError("Can't save file");
121 if ( !image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
) )
122 wxLogError("Can't load PNG image");
124 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
126 if ( !image
.LoadFile( dir
+ wxString("horse.jpg"), wxBITMAP_TYPE_JPEG
) )
127 wxLogError("Can't load JPG image");
129 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
131 if ( !image
.LoadFile( dir
+ wxString("horse.gif"), wxBITMAP_TYPE_GIF
) )
132 wxLogError("Can't load GIF image");
134 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
136 image
.LoadFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
);
137 my_square
= new wxBitmap( image
.ConvertToBitmap() );
139 CreateAntiAliasedBitmap();
142 MyCanvas::~MyCanvas()
145 delete my_horse_jpeg
;
151 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
153 wxPaintDC
dc( this );
156 dc
.DrawText( "Loaded image", 30, 10 );
157 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
159 dc
.DrawText( "Drawn directly", 150, 10 );
160 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
161 dc
.SetPen( *wxWHITE_PEN
);
162 dc
.DrawRectangle( 150, 30, 100, 100 );
164 if (my_anti
&& my_anti
->Ok()) dc
.DrawBitmap( *my_anti
, 250, 140 );
166 dc
.DrawText( "PNG handler", 30, 135 );
167 if (my_horse_png
&& my_horse_png
->Ok()) dc
.DrawBitmap( *my_horse_png
, 30, 150 );
169 dc
.DrawText( "JPEG handler", 30, 365 );
170 if (my_horse_jpeg
&& my_horse_jpeg
->Ok()) dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
172 dc
.DrawText( "GIF handler", 30, 595 );
173 if (my_horse_gif
&& my_horse_gif
->Ok()) dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
176 void MyCanvas::CreateAntiAliasedBitmap()
178 wxBitmap
bitmap( 300, 300 );
182 dc
.SelectObject( bitmap
);
186 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxDEFAULT
, wxDEFAULT
) );
187 dc
.SetTextForeground( "RED" );
188 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
189 dc
.DrawText( "And a Rectangle.", 20, 60 );
191 dc
.SetBrush( *wxRED_BRUSH
);
192 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
194 wxImage
original( bitmap
);
195 wxImage
anti( 150, 150 );
197 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
199 for (int y
= 1; y
< 149; y
++)
200 for (int x
= 1; x
< 149; x
++)
202 int red
= original
.GetRed( x
*2, y
*2 ) +
203 original
.GetRed( x
*2-1, y
*2 ) +
204 original
.GetRed( x
*2, y
*2+1 ) +
205 original
.GetRed( x
*2+1, y
*2+1 );
208 int green
= original
.GetGreen( x
*2, y
*2 ) +
209 original
.GetGreen( x
*2-1, y
*2 ) +
210 original
.GetGreen( x
*2, y
*2+1 ) +
211 original
.GetGreen( x
*2+1, y
*2+1 );
214 int blue
= original
.GetBlue( x
*2, y
*2 ) +
215 original
.GetBlue( x
*2-1, y
*2 ) +
216 original
.GetBlue( x
*2, y
*2+1 ) +
217 original
.GetBlue( x
*2+1, y
*2+1 );
219 anti
.SetRGB( x
, y
, red
, green
, blue
);
221 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
226 const int ID_QUIT
= 108;
227 const int ID_ABOUT
= 109;
229 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
231 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
232 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
233 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
237 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
238 wxPoint(20,20), wxSize(470,360) )
240 wxMenu
*file_menu
= new wxMenu();
241 file_menu
->Append( ID_ABOUT
, "&About..");
242 file_menu
->Append( ID_QUIT
, "E&xit");
244 wxMenuBar
*menu_bar
= new wxMenuBar();
245 menu_bar
->Append(file_menu
, "&File");
247 SetMenuBar( menu_bar
);
250 int widths
[] = { -1, 100 };
251 SetStatusWidths( 2, widths
);
253 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
254 m_canvas
->SetScrollbars( 10, 10, 50, 100 );
257 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
262 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
264 (void)wxMessageBox( "wxImage demo\n"
265 "Robert Roebling (c) 1998",
266 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
269 //-----------------------------------------------------------------------------
271 //-----------------------------------------------------------------------------
276 wxImage::AddHandler( new wxPNGHandler
);
280 wxImage::AddHandler( new wxJPEGHandler
);
283 wxImage::AddHandler( new wxGIFHandler
);
285 wxFrame
*frame
= new MyFrame();