]>
git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
0f1b6cf0aaa98c744bada36be2bc063c7358b31b
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 wxBitmap
bm( 1200, 1, 1 );
148 dc
.DrawBitmap( bm
, 10, 10 );
150 dc
.DrawText( "Drawn directly", 150, 10 );
151 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
152 dc
.SetPen( *wxWHITE_PEN
);
153 dc
.DrawRectangle( 150, 30, 100, 100 );
155 if (my_anti
&& my_anti
->Ok()) dc
.DrawBitmap( *my_anti
, 250, 140 );
157 dc
.DrawText( "PNG handler", 30, 135 );
158 if (my_horse_png
&& my_horse_png
->Ok()) dc
.DrawBitmap( *my_horse_png
, 30, 150 );
160 dc
.DrawText( "JPEG handler", 30, 365 );
161 if (my_horse_jpeg
&& my_horse_jpeg
->Ok()) dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
163 dc
.DrawText( "GIF handler", 30, 595 );
164 if (my_horse_gif
&& my_horse_gif
->Ok()) dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
167 void MyCanvas::CreateAntiAliasedBitmap()
169 wxBitmap
bitmap( 300, 300 );
173 dc
.SelectObject( bitmap
);
177 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxDEFAULT
, wxDEFAULT
) );
178 dc
.SetTextForeground( "RED" );
179 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
180 dc
.DrawText( "And a Rectangle.", 20, 60 );
182 dc
.SetBrush( *wxRED_BRUSH
);
183 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
185 wxImage
original( bitmap
);
186 wxImage
anti( 150, 150 );
188 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
190 for (int y
= 1; y
< 149; y
++)
191 for (int x
= 1; x
< 149; x
++)
193 int red
= original
.GetRed( x
*2, y
*2 ) +
194 original
.GetRed( x
*2-1, y
*2 ) +
195 original
.GetRed( x
*2, y
*2+1 ) +
196 original
.GetRed( x
*2+1, y
*2+1 );
199 int green
= original
.GetGreen( x
*2, y
*2 ) +
200 original
.GetGreen( x
*2-1, y
*2 ) +
201 original
.GetGreen( x
*2, y
*2+1 ) +
202 original
.GetGreen( x
*2+1, y
*2+1 );
205 int blue
= original
.GetBlue( x
*2, y
*2 ) +
206 original
.GetBlue( x
*2-1, y
*2 ) +
207 original
.GetBlue( x
*2, y
*2+1 ) +
208 original
.GetBlue( x
*2+1, y
*2+1 );
210 anti
.SetRGB( x
, y
, red
, green
, blue
);
212 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
217 const int ID_QUIT
= 108;
218 const int ID_ABOUT
= 109;
220 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
222 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
223 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
224 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
228 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
229 wxPoint(20,20), wxSize(470,360) )
231 wxMenu
*file_menu
= new wxMenu();
232 file_menu
->Append( ID_ABOUT
, "About..");
233 file_menu
->Append( ID_QUIT
, "Exit");
235 wxMenuBar
*menu_bar
= new wxMenuBar();
236 menu_bar
->Append(file_menu
, "File");
238 SetMenuBar( menu_bar
);
241 int widths
[] = { -1, 100 };
242 SetStatusWidths( 2, widths
);
244 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
245 m_canvas
->SetScrollbars( 10, 10, 50, 100 );
248 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
253 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
255 (void)wxMessageBox( "wxImage demo\n"
256 "Robert Roebling (c) 1998",
257 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
260 //-----------------------------------------------------------------------------
262 //-----------------------------------------------------------------------------
267 wxImage::AddHandler( new wxPNGHandler
);
271 wxImage::AddHandler( new wxJPEGHandler
);
274 wxImage::AddHandler( new wxGIFHandler
);
276 wxFrame
*frame
= new MyFrame();