]>
git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
e6b535b937c78d7aeb16b65c69c39923091d7bc7
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"
30 class MyCanvas
: public wxScrolledWindow
34 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
36 void OnPaint( wxPaintEvent
&event
);
37 void CreateAntiAliasedBitmap();
39 wxBitmap
*my_horse_png
;
40 wxBitmap
*my_horse_jpeg
;
41 wxBitmap
*my_horse_gif
;
45 DECLARE_DYNAMIC_CLASS(MyCanvas
)
51 class MyFrame
: public wxFrame
56 void OnAbout( wxCommandEvent
&event
);
57 void OnQuit( wxCommandEvent
&event
);
61 DECLARE_DYNAMIC_CLASS(MyFrame
)
67 class MyApp
: public wxApp
70 virtual bool OnInit();
79 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
81 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
82 EVT_PAINT(MyCanvas::OnPaint
)
85 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
86 const wxPoint
&pos
, const wxSize
&size
)
87 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
89 my_horse_png
= (wxBitmap
*) NULL
;
90 my_horse_jpeg
= (wxBitmap
*) NULL
;
91 my_horse_gif
= (wxBitmap
*) NULL
;
92 my_square
= (wxBitmap
*) NULL
;
93 my_anti
= (wxBitmap
*) NULL
;
95 SetBackgroundColour(* wxWHITE
);
97 wxBitmap
bitmap( 100, 100 );
100 dc
.SelectObject( bitmap
);
101 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
102 dc
.SetPen( *wxWHITE_PEN
);
103 dc
.DrawRectangle( 0, 0, 100, 100 );
104 dc
.SelectObject( wxNullBitmap
);
109 dir
= wxString("../");
112 wxImage
image( bitmap
);
113 image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
);
115 image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
);
116 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
118 image
.LoadFile( dir
+ wxString("horse.jpg"), wxBITMAP_TYPE_JPEG
);
119 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
121 image
.LoadFile( dir
+ wxString("horse.gif"), wxBITMAP_TYPE_GIF
);
122 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
124 image
.LoadFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
);
125 my_square
= new wxBitmap( image
.ConvertToBitmap() );
127 CreateAntiAliasedBitmap();
130 MyCanvas::~MyCanvas()
133 delete my_horse_jpeg
;
139 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
141 wxPaintDC
dc( this );
144 dc
.DrawText( "Loaded image", 30, 10 );
145 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
147 dc
.DrawText( "Drawn directly", 150, 10 );
148 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
149 dc
.SetPen( *wxWHITE_PEN
);
150 dc
.DrawRectangle( 150, 30, 100, 100 );
152 if (my_anti
&& my_anti
->Ok()) dc
.DrawBitmap( *my_anti
, 250, 140 );
154 dc
.DrawText( "PNG handler", 30, 135 );
155 if (my_horse_png
&& my_horse_png
->Ok()) dc
.DrawBitmap( *my_horse_png
, 30, 150 );
157 dc
.DrawText( "JPEG handler", 30, 365 );
158 if (my_horse_jpeg
&& my_horse_jpeg
->Ok()) dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
160 dc
.DrawText( "GIF handler", 30, 595 );
161 if (my_horse_gif
&& my_horse_gif
->Ok()) dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
164 void MyCanvas::CreateAntiAliasedBitmap()
166 wxBitmap
bitmap( 300, 300 );
170 dc
.SelectObject( bitmap
);
174 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxDEFAULT
, wxDEFAULT
) );
175 dc
.SetTextForeground( "RED" );
176 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
177 dc
.DrawText( "And a Rectangle.", 20, 60 );
179 dc
.SetBrush( *wxRED_BRUSH
);
180 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
182 wxImage
original( bitmap
);
183 wxImage
anti( 150, 150 );
185 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
187 for (int y
= 1; y
< 149; y
++)
188 for (int x
= 1; x
< 149; x
++)
190 int red
= original
.GetRed( x
*2, y
*2 ) +
191 original
.GetRed( x
*2-1, y
*2 ) +
192 original
.GetRed( x
*2, y
*2+1 ) +
193 original
.GetRed( x
*2+1, y
*2+1 );
196 int green
= original
.GetGreen( x
*2, y
*2 ) +
197 original
.GetGreen( x
*2-1, y
*2 ) +
198 original
.GetGreen( x
*2, y
*2+1 ) +
199 original
.GetGreen( x
*2+1, y
*2+1 );
202 int blue
= original
.GetBlue( x
*2, y
*2 ) +
203 original
.GetBlue( x
*2-1, y
*2 ) +
204 original
.GetBlue( x
*2, y
*2+1 ) +
205 original
.GetBlue( x
*2+1, y
*2+1 );
207 anti
.SetRGB( x
, y
, red
, green
, blue
);
209 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
214 const int ID_QUIT
= 108;
215 const int ID_ABOUT
= 109;
217 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
219 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
220 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
221 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
225 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
226 wxPoint(20,20), wxSize(470,360) )
228 wxMenu
*file_menu
= new wxMenu();
229 file_menu
->Append( ID_ABOUT
, "About..");
230 file_menu
->Append( ID_QUIT
, "Exit");
232 wxMenuBar
*menu_bar
= new wxMenuBar();
233 menu_bar
->Append(file_menu
, "File");
235 SetMenuBar( menu_bar
);
238 int widths
[] = { -1, 100 };
239 SetStatusWidths( 2, widths
);
241 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
242 m_canvas
->SetScrollbars( 10, 10, 50, 100 );
245 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
250 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
252 (void)wxMessageBox( "wxImage demo\n"
253 "Robert Roebling (c) 1998",
254 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
257 //-----------------------------------------------------------------------------
259 //-----------------------------------------------------------------------------
264 wxImage::AddHandler( new wxPNGHandler
);
268 wxImage::AddHandler( new wxJPEGHandler
);
271 wxImage::AddHandler( new wxGIFHandler
);
273 wxFrame
*frame
= new MyFrame();