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"
23 #include "wx/mstream.h"
24 #include "wx/wfstream.h"
25 #include "wx/quantize.h"
38 class MyCanvas
: public wxScrolledWindow
42 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
44 void OnPaint( wxPaintEvent
&event
);
45 void CreateAntiAliasedBitmap();
47 wxBitmap
*my_horse_png
;
48 wxBitmap
*my_horse_jpeg
;
49 wxBitmap
*my_horse_gif
;
50 wxBitmap
*my_horse_bmp
;
51 wxBitmap
*my_horse_pcx
;
52 wxBitmap
*my_horse_pnm
;
53 wxBitmap
*my_horse_tiff
;
54 wxBitmap
*my_horse_xpm
;
55 wxBitmap
*my_horse_ico32
;
56 wxBitmap
*my_horse_ico16
;
57 wxBitmap
*my_horse_ico
;
58 wxBitmap
*my_horse_cur
;
60 wxBitmap
*my_smile_xbm
;
67 wxBitmap m_bmpSmileXpm
;
68 wxIcon m_iconSmileXpm
;
71 DECLARE_DYNAMIC_CLASS(MyCanvas
)
76 const int nChoices
= 8 ;
77 static const wxString bppchoices
[nChoices
] =
89 static const int bppvalues
[nChoices
] =
104 class MyFrame
: public wxFrame
109 void OnAbout( wxCommandEvent
&event
);
110 void OnNewFrame( wxCommandEvent
&event
);
111 void OnQuit( wxCommandEvent
&event
);
116 DECLARE_DYNAMIC_CLASS(MyFrame
)
117 DECLARE_EVENT_TABLE()
120 class MyImageFrame
: public wxFrame
123 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
124 : wxFrame(parent
, -1, _T("Double click to save"),
125 wxDefaultPosition
, wxDefaultSize
,
126 wxCAPTION
| wxSYSTEM_MENU
),
129 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
132 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
134 wxPaintDC
dc( this );
135 //TRUE for masked images
136 dc
.DrawBitmap( m_bitmap
, 0, 0, TRUE
);
139 void OnSave(wxCommandEvent
& WXUNUSED(event
))
141 wxImage
image(m_bitmap
);
143 int bppselection
= wxGetSingleChoiceIndex("Set BMP BPP",
148 if ( bppselection
== -1 )
154 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, bppvalues
[bppselection
]);
156 wxString deffilename
= bppchoices
[bppselection
];
157 deffilename
.Replace(wxT(" "), wxT("_"));
158 deffilename
+= wxT(".bmp");
159 wxString savefilename
= wxFileSelector( wxT("Save Image"),
162 (const wxChar
*)NULL
,
163 wxT("BMP files (*.bmp)|*.bmp|")
164 wxT("PNG files (*.png)|*.png|")
165 wxT("JPEG files (*.jpg)|*.jpg|")
166 wxT("GIF files (*.gif)|*.gif|")
167 wxT("TIFF files (*.tif)|*.tif|")
168 wxT("PCX files (*.pcx)|*.pcx|")
169 wxT("ICO files (*.ico)|*.ico|")
170 wxT("CUR files (*.cur)|*.cur"),
173 if ( savefilename
.empty() )
176 if ( image
.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
) == wxBMP_8BPP_PALETTE
)
178 unsigned char *cmap
= new unsigned char [256];
179 for ( int i
= 0; i
< 256; i
++ )
181 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
187 wxString extension
= savefilename
.AfterLast('.').Lower();
189 if (extension
== "cur")
191 image
.Rescale(32,32);
192 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
193 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
194 // This shows how you can save an image with explicitly
195 // specified image format:
196 loaded
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
200 // This one guesses image format from filename extension
201 // (it may fail if the extension is not recognized):
202 loaded
= image
.SaveFile(savefilename
);
206 wxMessageBox("No handler for this file type.",
207 "File was not saved",
208 wxOK
|wxCENTRE
, this);
214 DECLARE_EVENT_TABLE()
219 class MyApp
: public wxApp
222 virtual bool OnInit();
231 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
233 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
234 EVT_PAINT(MyImageFrame::OnPaint
)
235 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
238 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
239 EVT_PAINT(MyCanvas::OnPaint
)
242 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
243 const wxPoint
&pos
, const wxSize
&size
)
244 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
245 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
246 , m_bmpSmileXpm((const char **) smile_xpm
)
247 , m_iconSmileXpm((const char **) smile_xpm
)
250 my_horse_png
= (wxBitmap
*) NULL
;
251 my_horse_jpeg
= (wxBitmap
*) NULL
;
252 my_horse_gif
= (wxBitmap
*) NULL
;
253 my_horse_bmp
= (wxBitmap
*) NULL
;
254 my_horse_pcx
= (wxBitmap
*) NULL
;
255 my_horse_pnm
= (wxBitmap
*) NULL
;
256 my_horse_tiff
= (wxBitmap
*) NULL
;
257 my_horse_xpm
= (wxBitmap
*) NULL
;
258 my_horse_ico32
= (wxBitmap
*) NULL
;
259 my_horse_ico16
= (wxBitmap
*) NULL
;
260 my_horse_ico
= (wxBitmap
*) NULL
;
261 my_horse_cur
= (wxBitmap
*) NULL
;
263 my_smile_xbm
= (wxBitmap
*) NULL
;
264 my_square
= (wxBitmap
*) NULL
;
265 my_anti
= (wxBitmap
*) NULL
;
267 SetBackgroundColour(* wxWHITE
);
269 wxBitmap
bitmap( 100, 100 );
272 dc
.SelectObject( bitmap
);
273 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
274 dc
.SetPen( *wxBLACK_PEN
);
275 dc
.DrawRectangle( 0, 0, 100, 100 );
276 dc
.SetBrush( *wxWHITE_BRUSH
);
277 dc
.DrawRectangle( 20, 20, 60, 60 );
278 dc
.SelectObject( wxNullBitmap
);
280 // try to find the directory with our images
282 if ( wxFile::Exists(wxT("./horse.png")) )
284 else if ( wxFile::Exists(wxT("../horse.png")) )
287 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
289 wxImage image
= bitmap
.ConvertToImage();
292 if ( !image
.SaveFile( dir
+ wxString("test.png"), wxBITMAP_TYPE_PNG
))
293 wxLogError(wxT("Can't save file"));
297 image
.LoadFile( dir
+ wxString("test.png") );
298 my_square
= new wxBitmap( image
);
302 if ( !image
.LoadFile( dir
+ wxString("horse.png")) )
303 wxLogError(wxT("Can't load PNG image"));
305 my_horse_png
= new wxBitmap( image
);
306 #endif // wxUSE_LIBPNG
311 if ( !image
.LoadFile( dir
+ wxString("horse.jpg")) )
312 wxLogError(wxT("Can't load JPG image"));
314 my_horse_jpeg
= new wxBitmap( image
);
315 #endif // wxUSE_LIBJPEG
320 if ( !image
.LoadFile( dir
+ wxString("horse.gif")) )
321 wxLogError(wxT("Can't load GIF image"));
323 my_horse_gif
= new wxBitmap( image
);
329 if ( !image
.LoadFile( dir
+ wxString("horse.pcx"), wxBITMAP_TYPE_PCX
) )
330 wxLogError(wxT("Can't load PCX image"));
332 my_horse_pcx
= new wxBitmap( image
);
337 if ( !image
.LoadFile( dir
+ wxString("horse.bmp"), wxBITMAP_TYPE_BMP
) )
338 wxLogError(wxT("Can't load BMP image"));
340 my_horse_bmp
= new wxBitmap( image
);
345 if ( !image
.LoadFile( dir
+ wxString("horse.xpm"), wxBITMAP_TYPE_XPM
) )
346 wxLogError(wxT("Can't load XPM image"));
348 my_horse_xpm
= new wxBitmap( image
);
350 if ( !image
.SaveFile( dir
+ wxString("test.xpm"), wxBITMAP_TYPE_XPM
))
351 wxLogError(wxT("Can't save file"));
357 if ( !image
.LoadFile( dir
+ wxString("horse.pnm"), wxBITMAP_TYPE_PNM
) )
358 wxLogError(wxT("Can't load PNM image"));
360 my_horse_pnm
= new wxBitmap( image
);
366 if ( !image
.LoadFile( dir
+ wxString("horse.tif"), wxBITMAP_TYPE_TIF
) )
367 wxLogError(wxT("Can't load TIFF image"));
369 my_horse_tiff
= new wxBitmap( image
);
372 CreateAntiAliasedBitmap();
374 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
377 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
378 // demonstrates XPM automatically using the mask when saving
379 if ( m_bmpSmileXpm
.Ok() )
380 m_bmpSmileXpm
.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM
);
386 if ( !image
.LoadFile( dir
+ wxString("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
387 wxLogError(wxT("Can't load first ICO image"));
389 my_horse_ico32
= new wxBitmap( image
);
393 if ( !image
.LoadFile( dir
+ wxString("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
394 wxLogError(wxT("Can't load second ICO image"));
396 my_horse_ico16
= new wxBitmap( image
);
400 if ( !image
.LoadFile( dir
+ wxString("horse.ico") ) )
401 wxLogError(wxT("Can't load best ICO image"));
403 my_horse_ico
= new wxBitmap( image
);
407 if ( !image
.LoadFile( dir
+ wxString("horse.cur"), wxBITMAP_TYPE_CUR
) )
408 wxLogError(wxT("Can't load best ICO image"));
411 my_horse_cur
= new wxBitmap( image
);
412 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
413 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
420 MyCanvas::~MyCanvas()
424 delete my_horse_jpeg
;
428 delete my_horse_tiff
;
430 delete my_horse_ico32
;
431 delete my_horse_ico16
;
438 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
440 wxPaintDC
dc( this );
443 dc
.DrawText( "Loaded image", 30, 10 );
444 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
446 dc
.DrawText( "Drawn directly", 150, 10 );
447 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
448 dc
.SetPen( *wxBLACK_PEN
);
449 dc
.DrawRectangle( 150, 30, 100, 100 );
450 dc
.SetBrush( *wxWHITE_BRUSH
);
451 dc
.DrawRectangle( 170, 50, 60, 60 );
453 if (my_anti
&& my_anti
->Ok())
454 dc
.DrawBitmap( *my_anti
, 280, 30 );
456 dc
.DrawText( "PNG handler", 30, 135 );
457 if (my_horse_png
&& my_horse_png
->Ok())
459 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
460 wxRect
rect(0,0,100,100);
461 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
462 dc
.DrawText( "GetSubBitmap()", 280, 190 );
463 dc
.DrawBitmap( sub
, 280, 210 );
466 dc
.DrawText( "JPEG handler", 30, 365 );
467 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
468 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
470 dc
.DrawText( "GIF handler", 30, 595 );
471 if (my_horse_gif
&& my_horse_gif
->Ok())
472 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
474 dc
.DrawText( "PCX handler", 30, 825 );
475 if (my_horse_pcx
&& my_horse_pcx
->Ok())
476 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
478 dc
.DrawText( "BMP handler", 30, 1055 );
479 if (my_horse_bmp
&& my_horse_bmp
->Ok())
480 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
482 dc
.DrawText( "PNM handler", 30, 1285 );
483 if (my_horse_pnm
&& my_horse_pnm
->Ok())
484 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
486 dc
.DrawText( "TIFF handler", 30, 1515 );
487 if (my_horse_tiff
&& my_horse_tiff
->Ok())
488 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
490 dc
.DrawText( "XPM handler", 30, 1745 );
491 if (my_horse_xpm
&& my_horse_xpm
->Ok())
492 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
495 if (my_smile_xbm
&& my_smile_xbm
->Ok())
497 dc
.DrawText( "XBM bitmap", 30, 1975 );
498 dc
.DrawText( "(green on red)", 30, 1990 );
499 dc
.SetTextForeground( wxT("GREEN") );
500 dc
.SetTextBackground( wxT("RED") );
501 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
503 dc
.SetTextForeground( wxT("BLACK") );
504 dc
.DrawText( "After wxImage conversion", 150, 1975 );
505 dc
.DrawText( "(red on white)", 150, 1990 );
506 dc
.SetTextForeground( wxT("RED") );
507 wxImage i
= my_smile_xbm
->ConvertToImage();
508 i
.SetMaskColour( 255, 255, 255 );
510 wxRED_PEN
->GetColour().Red(),
511 wxRED_PEN
->GetColour().Green(),
512 wxRED_PEN
->GetColour().Blue() );
513 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 2010, TRUE
);
514 dc
.SetTextForeground( wxT("BLACK") );
518 wxBitmap
mono( 60,50,1 );
520 memdc
.SelectObject( mono
);
521 memdc
.SetPen( *wxBLACK_PEN
);
522 memdc
.SetBrush( *wxWHITE_BRUSH
);
523 memdc
.DrawRectangle( 0,0,60,50 );
524 memdc
.SetTextForeground( *wxBLACK
);
525 memdc
.DrawText( "Hi!", 5, 5 );
526 memdc
.SetBrush( *wxBLACK_BRUSH
);
527 memdc
.DrawRectangle( 33,5,20,20 );
528 memdc
.SetPen( *wxRED_PEN
);
529 memdc
.DrawLine( 5, 42, 50, 42 );
530 memdc
.SelectObject( wxNullBitmap
);
534 dc
.DrawText( "Mono bitmap", 30, 2095 );
535 dc
.DrawText( "(red on green)", 30, 2110 );
536 dc
.SetTextForeground( wxT("RED") );
537 dc
.SetTextBackground( wxT("GREEN") );
538 dc
.DrawBitmap( mono
, 30, 2130 );
540 dc
.SetTextForeground( wxT("BLACK") );
541 dc
.DrawText( "After wxImage conversion", 150, 2095 );
542 dc
.DrawText( "(red on white)", 150, 2110 );
543 dc
.SetTextForeground( wxT("RED") );
544 wxImage i
= mono
.ConvertToImage();
545 i
.SetMaskColour( 255,255,255 );
547 wxRED_PEN
->GetColour().Red(),
548 wxRED_PEN
->GetColour().Green(),
549 wxRED_PEN
->GetColour().Blue() );
550 dc
.DrawBitmap( i
.ConvertToBitmap(), 150, 2130, TRUE
);
551 dc
.SetTextForeground( wxT("BLACK") );
554 dc
.DrawText("XPM bitmap", 30, 2230);
555 if ( m_bmpSmileXpm
.Ok() )
557 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, TRUE
);
560 dc
.DrawText("XPM icon", 150, 2230);
561 if ( m_iconSmileXpm
.Ok() )
563 dc
.DrawIcon(m_iconSmileXpm
, 150, 2250);
566 dc
.DrawText( "ICO handler (1st image)", 30, 2290 );
567 if (my_horse_ico32
&& my_horse_ico32
->Ok())
568 dc
.DrawBitmap( *my_horse_ico32
, 30, 2330, TRUE
);
570 dc
.DrawText( "ICO handler (2nd image)", 230, 2290 );
571 if (my_horse_ico16
&& my_horse_ico16
->Ok())
572 dc
.DrawBitmap( *my_horse_ico16
, 230, 2330, TRUE
);
574 dc
.DrawText( "ICO handler (best image)", 430, 2290 );
575 if (my_horse_ico
&& my_horse_ico
->Ok())
576 dc
.DrawBitmap( *my_horse_ico
, 430, 2330, TRUE
);
578 dc
.DrawText( "CUR handler", 30, 2390 );
579 if (my_horse_cur
&& my_horse_cur
->Ok())
581 dc
.DrawBitmap( *my_horse_cur
, 30, 2420, TRUE
);
582 dc
.SetPen (*wxRED_PEN
);
583 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
584 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
588 void MyCanvas::CreateAntiAliasedBitmap()
590 wxBitmap
bitmap( 300, 300 );
594 dc
.SelectObject( bitmap
);
598 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
599 dc
.SetTextForeground( wxT("RED") );
600 dc
.DrawText( "This is anti-aliased Text.", 20, 20 );
601 dc
.DrawText( "And a Rectangle.", 20, 60 );
603 dc
.SetBrush( *wxRED_BRUSH
);
604 dc
.SetPen( *wxTRANSPARENT_PEN
);
605 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
607 wxImage original
= bitmap
.ConvertToImage();
608 wxImage
anti( 150, 150 );
610 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
612 for (int y
= 1; y
< 149; y
++)
613 for (int x
= 1; x
< 149; x
++)
615 int red
= original
.GetRed( x
*2, y
*2 ) +
616 original
.GetRed( x
*2-1, y
*2 ) +
617 original
.GetRed( x
*2, y
*2+1 ) +
618 original
.GetRed( x
*2+1, y
*2+1 );
621 int green
= original
.GetGreen( x
*2, y
*2 ) +
622 original
.GetGreen( x
*2-1, y
*2 ) +
623 original
.GetGreen( x
*2, y
*2+1 ) +
624 original
.GetGreen( x
*2+1, y
*2+1 );
627 int blue
= original
.GetBlue( x
*2, y
*2 ) +
628 original
.GetBlue( x
*2-1, y
*2 ) +
629 original
.GetBlue( x
*2, y
*2+1 ) +
630 original
.GetBlue( x
*2+1, y
*2+1 );
632 anti
.SetRGB( x
, y
, red
, green
, blue
);
634 my_anti
= new wxBitmap( anti
.ConvertToBitmap() );
639 const int ID_QUIT
= 108;
640 const int ID_ABOUT
= 109;
641 const int ID_NEW
= 110;
643 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
645 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
646 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
647 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
648 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
652 : wxFrame( (wxFrame
*)NULL
, -1, "wxImage sample",
653 wxPoint(20,20), wxSize(470,360) )
655 wxMenu
*file_menu
= new wxMenu();
656 file_menu
->Append( ID_NEW
, "&Show image...");
657 file_menu
->AppendSeparator();
658 file_menu
->Append( ID_ABOUT
, "&About...");
659 file_menu
->AppendSeparator();
660 file_menu
->Append( ID_QUIT
, "E&xit");
662 wxMenuBar
*menu_bar
= new wxMenuBar();
663 menu_bar
->Append(file_menu
, "&File");
665 SetMenuBar( menu_bar
);
668 int widths
[] = { -1, 100 };
669 SetStatusWidths( 2, widths
);
671 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
673 // 500 width * 2500 height
674 m_canvas
->SetScrollbars( 10, 10, 50, 250 );
677 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
682 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
684 (void)wxMessageBox( "wxImage demo\n"
685 "Robert Roebling (c) 1998,2000",
686 "About wxImage Demo", wxICON_INFORMATION
| wxOK
);
689 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
691 wxString filename
= wxFileSelector(_T("Select image file"));
696 if ( !image
.LoadFile(filename
) )
698 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
703 (new MyImageFrame(this, image
.ConvertToBitmap()))->Show();
706 //-----------------------------------------------------------------------------
708 //-----------------------------------------------------------------------------
713 wxImage::AddHandler( new wxPNGHandler
);
717 wxImage::AddHandler( new wxJPEGHandler
);
721 wxImage::AddHandler( new wxTIFFHandler
);
725 wxImage::AddHandler( new wxGIFHandler
);
729 wxImage::AddHandler( new wxPCXHandler
);
733 wxImage::AddHandler( new wxPNMHandler
);
737 wxImage::AddHandler( new wxXPMHandler
);
741 wxImage::AddHandler( new wxICOHandler
);
742 wxImage::AddHandler( new wxCURHandler
);
745 wxFrame
*frame
= new MyFrame();