]>
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"
30 class MyCanvas
: public wxScrolledWindow
34 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
36 void OnPaint( wxPaintEvent
&event
);
37 void CreateAntiAliasedBitmap();
43 DECLARE_DYNAMIC_CLASS(MyCanvas
)
49 class MyFrame
: public wxFrame
54 void OnAbout( wxCommandEvent
&event
);
55 void OnQuit( wxCommandEvent
&event
);
59 DECLARE_DYNAMIC_CLASS(MyFrame
)
65 class MyApp
: public wxApp
68 virtual bool OnInit();
77 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
79 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
80 EVT_PAINT(MyCanvas::OnPaint
)
83 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
84 const wxPoint
&pos
, const wxSize
&size
)
85 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
87 SetBackgroundColour(* wxWHITE
);
89 wxBitmap
bitmap( 100, 100 );
92 dc
.SelectObject( bitmap
);
93 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
94 dc
.SetPen( *wxWHITE_PEN
);
95 dc
.DrawRectangle( 0, 0, 100, 100 );
96 dc
.SelectObject( wxNullBitmap
);
101 dir
= wxString("../");
104 wxImage
image( bitmap
);
105 image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
);
107 image
.LoadFile( dir
+ wxString("horse.png"), wxBITMAP_TYPE_PNG
);
108 my_horse
= new wxBitmap( image
.ConvertToBitmap() );
110 image
.LoadFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
);
111 my_square
= new wxBitmap( image
.ConvertToBitmap() );
113 CreateAntiAliasedBitmap();
116 MyCanvas::~MyCanvas()
123 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
125 wxPaintDC
dc( this );
128 dc
.DrawText( "Loaded image", 30, 10 );
129 if (my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
131 dc
.DrawText( "Drawn directly", 150, 10 );
132 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
133 dc
.SetPen( *wxWHITE_PEN
);
134 dc
.DrawRectangle( 150, 30, 100, 100 );
136 if (my_anti
->Ok()) dc
.DrawBitmap( *my_anti
, 250, 140 );
138 if (my_horse
->Ok()) dc
.DrawBitmap( *my_horse
, 30, 140 );
141 void MyCanvas::CreateAntiAliasedBitmap()
143 wxBitmap
bitmap( 300, 300 );
145 dc
.SelectObject( bitmap
);
149 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxDEFAULT
, wxDEFAULT
) );
150 dc
.SetTextForeground( "RED" );
151 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
152 dc
.DrawText( "And a Rectangle.", 20, 60 );
154 dc
.SetBrush( *wxRED_BRUSH
);
155 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
157 wxImage
original( bitmap
);
158 wxImage
anti( 150, 150 );
160 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
162 for (int y
= 1; y
< 149; y
++)
163 for (int x
= 1; x
< 149; x
++)
165 int red
= original
.GetRed( x
*2, y
*2 ) +
166 original
.GetRed( x
*2-1, y
*2 ) +
167 original
.GetRed( x
*2, y
*2+1 ) +
168 original
.GetRed( x
*2+1, y
*2+1 );
171 int green
= original
.GetGreen( x
*2, y
*2 ) +
172 original
.GetGreen( x
*2-1, y
*2 ) +
173 original
.GetGreen( x
*2, y
*2+1 ) +
174 original
.GetGreen( x
*2+1, y
*2+1 );
177 int blue
= original
.GetBlue( x
*2, y
*2 ) +
178 original
.GetBlue( x
*2-1, y
*2 ) +
179 original
.GetBlue( x
*2, y
*2+1 ) +
180 original
.GetBlue( x
*2+1, y
*2+1 );
182 anti
.SetRGB( x
, y
, red
, green
, blue
);
184 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
189 const int ID_QUIT
= 108;
190 const int ID_ABOUT
= 109;
192 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
194 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
195 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
196 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
200 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
201 wxPoint(20,20), wxSize(470,360) )
203 wxMenu
*file_menu
= new wxMenu();
204 file_menu
->Append( ID_ABOUT
, "About..");
205 file_menu
->Append( ID_QUIT
, "Exit");
207 wxMenuBar
*menu_bar
= new wxMenuBar();
208 menu_bar
->Append(file_menu
, "File");
210 SetMenuBar( menu_bar
);
213 int widths
[] = { -1, 100 };
214 SetStatusWidths( 2, widths
);
216 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
217 m_canvas
->SetScrollbars( 10, 10, 50, 50 );
220 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
225 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
227 (void)wxMessageBox( "wxImage demo\n"
228 "Robert Roebling (c) 1998",
229 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
232 //-----------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------
238 wxImage::AddHandler( new wxPNGHandler
);
240 wxFrame
*frame
= new MyFrame();