]>
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"
34 class MyCanvas
: public wxScrolledWindow
38 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
40 void OnPaint( wxPaintEvent
&event
);
41 void CreateAntiAliasedBitmap();
43 wxBitmap
*my_horse_png
;
44 wxBitmap
*my_horse_jpeg
;
45 wxBitmap
*my_horse_gif
;
46 wxBitmap
*my_horse_bmp
;
47 wxBitmap
*my_horse_pcx
;
48 wxBitmap
*my_horse_pnm
;
49 wxBitmap
*my_horse_tiff
;
50 wxBitmap
*my_smile_xbm
;
55 wxBitmap m_bmpSmileXpm
;
56 wxIcon m_iconSmileXpm
;
59 DECLARE_DYNAMIC_CLASS(MyCanvas
)
65 class MyFrame
: public wxFrame
70 void OnAbout( wxCommandEvent
&event
);
71 void OnQuit( wxCommandEvent
&event
);
76 DECLARE_DYNAMIC_CLASS(MyFrame
)
82 class MyApp
: public wxApp
85 virtual bool OnInit();
94 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
96 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
97 EVT_PAINT(MyCanvas::OnPaint
)
100 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
101 const wxPoint
&pos
, const wxSize
&size
)
102 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
103 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
104 , m_bmpSmileXpm((const char **) smile_xpm
)
105 , m_iconSmileXpm((const char **) smile_xpm
)
108 my_horse_png
= (wxBitmap
*) NULL
;
109 my_horse_jpeg
= (wxBitmap
*) NULL
;
110 my_horse_gif
= (wxBitmap
*) NULL
;
111 my_horse_bmp
= (wxBitmap
*) NULL
;
112 my_horse_pcx
= (wxBitmap
*) NULL
;
113 my_horse_pnm
= (wxBitmap
*) NULL
;
114 my_horse_tiff
= (wxBitmap
*) NULL
;
115 my_smile_xbm
= (wxBitmap
*) NULL
;
116 my_square
= (wxBitmap
*) NULL
;
117 my_anti
= (wxBitmap
*) NULL
;
119 SetBackgroundColour(* wxWHITE
);
121 wxBitmap
bitmap( 100, 100 );
124 dc
.SelectObject( bitmap
);
125 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
126 dc
.SetPen( *wxBLACK_PEN
);
127 dc
.DrawRectangle( 0, 0, 100, 100 );
128 dc
.SetBrush( *wxWHITE_BRUSH
);
129 dc
.DrawRectangle( 20, 20, 60, 60 );
130 dc
.SelectObject( wxNullBitmap
);
132 // try to find the directory with our images
134 if ( wxFile::Exists("./horse.png") )
136 else if ( wxFile::Exists("../horse.png") )
139 wxLogWarning("Can't find image files in either '.' or '..'!");
141 wxImage
image( bitmap
);
144 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
))
145 wxLogError("Can't save file");
149 image
.LoadFile( dir
+ wxString("test.png") );
150 my_square
= new wxBitmap( image
.ConvertToBitmap() );
154 if ( !image
.LoadFile( dir
+ wxString("horse.png")) )
155 wxLogError("Can't load PNG image");
157 my_horse_png
= new wxBitmap( image
.ConvertToBitmap() );
158 #endif // wxUSE_LIBPNG
163 if ( !image
.LoadFile( dir
+ wxString("horse.jpg")) )
164 wxLogError("Can't load JPG image");
166 my_horse_jpeg
= new wxBitmap( image
.ConvertToBitmap() );
167 #endif // wxUSE_LIBJPEG
172 if ( !image
.LoadFile( dir
+ wxString("horse.gif")) )
173 wxLogError("Can't load GIF image");
175 my_horse_gif
= new wxBitmap( image
.ConvertToBitmap() );
181 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
182 wxLogError("Can't load PCX image");
184 my_horse_pcx
= new wxBitmap( image
.ConvertToBitmap() );
189 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
190 wxLogError("Can't load BMP image");
192 my_horse_bmp
= new wxBitmap( image
.ConvertToBitmap() );
197 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
198 wxLogError("Can't load PNM image");
200 my_horse_pnm
= new wxBitmap( image
.ConvertToBitmap() );
206 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
207 wxLogError("Can't load TIFF image");
209 my_horse_tiff
= new wxBitmap( image
.ConvertToBitmap() );
212 CreateAntiAliasedBitmap();
214 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
218 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
219 // demonstrates XPM automatically using the mask when saving
220 if ( m_bmpSmileXpm
.Ok() )
221 m_bmpSmileXpm
.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM
);
226 MyCanvas::~MyCanvas()
230 delete my_horse_jpeg
;
234 delete my_horse_tiff
;
240 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
242 wxPaintDC
dc( this );
245 dc
.DrawText( "Loaded image", 30, 10 );
246 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
248 dc
.DrawText( "Drawn directly", 150, 10 );
249 dc
.SetBrush( wxBrush( "orange", wxSOLID
) );
250 dc
.SetPen( *wxBLACK_PEN
);
251 dc
.DrawRectangle( 150, 30, 100, 100 );
252 dc
.SetBrush( *wxWHITE_BRUSH
);
253 dc
.DrawRectangle( 170, 50, 60, 60 );
255 if (my_anti
&& my_anti
->Ok())
256 dc
.DrawBitmap( *my_anti
, 280, 30 );
258 dc
.DrawText( "PNG handler", 30, 135 );
259 if (my_horse_png
&& my_horse_png
->Ok())
261 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
262 wxRect
rect(0,0,100,100);
263 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
264 dc
.DrawText( "GetSubBitmap()", 280, 190 );
265 dc
.DrawBitmap( sub
, 280, 210 );
268 dc
.DrawText( "JPEG handler", 30, 365 );
269 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
270 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
272 dc
.DrawText( "GIF handler", 30, 595 );
273 if (my_horse_gif
&& my_horse_gif
->Ok())
274 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
276 dc
.DrawText( "PCX handler", 30, 825 );
277 if (my_horse_pcx
&& my_horse_pcx
->Ok())
278 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
280 dc
.DrawText( "BMP handler", 30, 1055 );
281 if (my_horse_bmp
&& my_horse_bmp
->Ok())
282 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
284 dc
.DrawText( "PNM handler", 30, 1285 );
285 if (my_horse_pnm
&& my_horse_pnm
->Ok())
286 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
288 dc
.DrawText( "TIFF handler", 30, 1515 );
289 if (my_horse_tiff
&& my_horse_tiff
->Ok())
290 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
292 if (my_smile_xbm
&& my_smile_xbm
->Ok())
294 dc
.DrawText( "XBM bitmap", 30, 1745 );
295 dc
.DrawText( "(green on red)", 30, 1760 );
296 dc
.SetTextForeground( "GREEN" );
297 dc
.SetTextBackground( "RED" );
298 dc
.DrawBitmap( *my_smile_xbm
, 30, 1780 );
300 dc
.SetTextForeground( "BLACK" );
301 dc
.DrawText( "After wxImage conversion", 150, 1745 );
302 dc
.DrawText( "(red on white)", 150, 1760 );
303 dc
.SetTextForeground( "RED" );
304 wxImage
i( *my_smile_xbm
);
305 i
.SetMaskColour( 255, 255, 255 );
307 wxRED_PEN
->GetColour().Red(),
308 wxRED_PEN
->GetColour().Green(),
309 wxRED_PEN
->GetColour().Blue() );
310 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1780, TRUE
);
311 dc
.SetTextForeground( "BLACK" );
315 wxBitmap
mono( 60,50,1 );
317 memdc
.SelectObject( mono
);
318 memdc
.SetPen( *wxBLACK_PEN
);
319 memdc
.SetBrush( *wxWHITE_BRUSH
);
320 memdc
.DrawRectangle( 0,0,60,50 );
321 memdc
.SetTextForeground( *wxBLACK
);
322 memdc
.DrawText( "Hi!", 5, 5 );
323 memdc
.SetBrush( *wxBLACK_BRUSH
);
324 memdc
.DrawRectangle( 33,5,20,20 );
325 memdc
.SetPen( *wxRED_PEN
);
326 memdc
.DrawLine( 5, 42, 50, 42 );
327 memdc
.SelectObject( wxNullBitmap
);
331 dc
.DrawText( "Mono bitmap", 30, 1865 );
332 dc
.DrawText( "(red on green)", 30, 1880 );
333 dc
.SetTextForeground( "RED" );
334 dc
.SetTextBackground( "GREEN" );
335 dc
.DrawBitmap( mono
, 30, 1900 );
337 dc
.SetTextForeground( "BLACK" );
338 dc
.DrawText( "After wxImage conversion", 150, 1865 );
339 dc
.DrawText( "(red on white)", 150, 1880 );
340 dc
.SetTextForeground( "RED" );
342 i
.SetMaskColour( 255,255,255 );
344 wxRED_PEN
->GetColour().Red(),
345 wxRED_PEN
->GetColour().Green(),
346 wxRED_PEN
->GetColour().Blue() );
347 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 1900, TRUE
);
348 dc
.SetTextForeground( "BLACK" );
351 dc
.DrawText("XPM bitmap", 30, 2000);
352 if ( m_bmpSmileXpm
.Ok() )
354 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2020, TRUE
);
357 dc
.DrawText("XPM icon", 150, 2000);
358 if ( m_iconSmileXpm
.Ok() )
360 dc
.DrawIcon(m_iconSmileXpm
, 150, 2020);
364 void MyCanvas::CreateAntiAliasedBitmap()
366 wxBitmap
bitmap( 300, 300 );
370 dc
.SelectObject( bitmap
);
374 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
375 dc
.SetTextForeground( "RED" );
376 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
377 dc
.DrawText( "And a Rectangle.", 20, 60 );
379 dc
.SetBrush( *wxRED_BRUSH
);
380 dc
.SetPen( *wxTRANSPARENT_PEN
);
381 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
383 wxImage
original( bitmap
);
384 wxImage
anti( 150, 150 );
386 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
388 for (int y
= 1; y
< 149; y
++)
389 for (int x
= 1; x
< 149; x
++)
391 int red
= original
.GetRed( x
*2, y
*2 ) +
392 original
.GetRed( x
*2-1, y
*2 ) +
393 original
.GetRed( x
*2, y
*2+1 ) +
394 original
.GetRed( x
*2+1, y
*2+1 );
397 int green
= original
.GetGreen( x
*2, y
*2 ) +
398 original
.GetGreen( x
*2-1, y
*2 ) +
399 original
.GetGreen( x
*2, y
*2+1 ) +
400 original
.GetGreen( x
*2+1, y
*2+1 );
403 int blue
= original
.GetBlue( x
*2, y
*2 ) +
404 original
.GetBlue( x
*2-1, y
*2 ) +
405 original
.GetBlue( x
*2, y
*2+1 ) +
406 original
.GetBlue( x
*2+1, y
*2+1 );
408 anti
.SetRGB( x
, y
, red
, green
, blue
);
410 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
415 const int ID_QUIT
= 108;
416 const int ID_ABOUT
= 109;
418 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
420 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
421 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
422 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
426 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
427 wxPoint(20,20), wxSize(470,360) )
429 wxMenu
*file_menu
= new wxMenu();
430 file_menu
->Append( ID_ABOUT
, "&About...");
431 file_menu
->Append( ID_QUIT
, "E&xit");
433 wxMenuBar
*menu_bar
= new wxMenuBar();
434 menu_bar
->Append(file_menu
, "&File");
436 SetMenuBar( menu_bar
);
439 int widths
[] = { -1, 100 };
440 SetStatusWidths( 2, widths
);
442 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
444 // 500 width * 2100 height
445 m_canvas
->SetScrollbars( 10, 10, 50, 220 );
448 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
453 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
455 (void)wxMessageBox( "wxImage demo\n"
456 "Robert Roebling (c) 1998,2000",
457 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
460 //-----------------------------------------------------------------------------
462 //-----------------------------------------------------------------------------
467 wxImage::AddHandler( new wxPNGHandler
);
471 wxImage::AddHandler( new wxJPEGHandler
);
475 wxImage::AddHandler( new wxTIFFHandler
);
479 wxImage::AddHandler( new wxGIFHandler
);
483 wxImage::AddHandler( new wxPCXHandler
);
487 wxImage::AddHandler( new wxPNMHandler
);
490 wxFrame
*frame
= new MyFrame();