1. wxEuphoria image scaling
- Posted by buzzo Nov 20, 2014
- 2120 views
set_bitmap_size seems to crop the image from 0,0 to width, height..
rescale ( atom image, atom width, atom height, atom quality ) might work if I could find info on quality.
the documentation mentions "scale", but can not find it.. same with wxwidgets docs
no problems if I do not attempt to rescale .. but if I do then, flash and gone..
bmp = create( wxBitmap, {BM_FROM_FILE, filename, wxBITMAP_TYPE_ANY} ) convert_to_image ( bmp ) bmp_size = get_bitmap_size( bmp ) puts(1,to_string(bmp_size[1]) & " " & to_string(bmp_size[1]) & "\n") rescale ( bmp,floor(bmp_size[1] * .1),floor(bmp_size[2] * .1) , 1) draw_bitmap( dc, bmp, 10,500, wxFalse )
needing help ..
Thanks
Buzzo
2. Re: wxEuphoria image scaling
- Posted by evanmars Nov 21, 2014
- 2114 views
http://docs.wxwidgets.org/3.0/image_8h.html#a8cb59f5925c96a706710106f679d171a
wxImage & Rescale (int width, int height, wxImageResizeQuality quality=wxIMAGE_QUALITY_NORMAL)
Changes the size of the image in-place by scaling it: after a call to this function,the image will have the given width and height.
wxIMAGE_QUALITY_NEAREST Simplest and fastest algorithm.
wxIMAGE_QUALITY_BILINEAR Compromise between wxIMAGE_QUALITY_NEAREST and wxIMAGE_QUALITY_BICUBIC.
wxIMAGE_QUALITY_BICUBIC Highest quality but slowest execution time.
wxIMAGE_QUALITY_BOX_AVERAGE Use surrounding pixels to calculate an average that will be used for new pixels.
This method is typically used when reducing the size of an image.
wxIMAGE_QUALITY_NORMAL Default image resizing algorithm used by wxImage::Scale().
Currently the same as wxIMAGE_QUALITY_NEAREST.
wxIMAGE_QUALITY_HIGH Best image resizing algorithm.
Since version 2.9.2 this results in wxIMAGE_QUALITY_BOX_AVERAGE being used when reducing the size of the image (meaning that both the new width and height will be smaller than the original size). Otherwise wxIMAGE_QUALITY_BICUBIC is used.
Around line 4322 in wxeud.e
public constant wxIMAGE_QUALITY_NORMAL = 0, wxIMAGE_QUALITY_HIGH = 1
3. Re: wxEuphoria image scaling
- Posted by buzzo Nov 21, 2014
- 2052 views
Thanks Evan
However, no matter what is put in the third parameter, the program crashes with out any error or ex.err.. removing the math did not change anything.. the mystery continues.
Buzzo
4. Re: wxEuphoria image scaling
- Posted by evanmars Nov 21, 2014
- 2051 views
Sorry, that's about as much 'help' as I can give.
I'm just starting to experiment with wxEuphoria myself.
5. Re: wxEuphoria image scaling
- Posted by evanmars Nov 21, 2014
- 2029 views
Don't know if it helps, but adding another
bmp_size = get_bitmap_size( bmp ) puts(1,to_string(bmp_size[1]) & " " & to_string(bmp_size[1]) & "\n")
after the rescale shows the new size to be 0,0
6. Re: wxEuphoria image scaling
- Posted by ghaberek (admin) Nov 21, 2014
- 2087 views
Thanks Evan
However, no matter what is put in the third parameter, the program crashes with out any error or ex.err.. removing the math did not change anything.. the mystery continues.
Buzzo
The convert_to_image() routine is a function, not a procedure. It returns a wxImage object that you have to assign to something.
I think this is the cause of your abrupt crash, because you are passing a wxBitmap object to the wxImage rescale() function.
Bitmap manipulation generally goes like this...
- Create a bitmap from your source (file, memory, etc.)
- Convert the bitmap to an image.
- Delete the original bitmap (if necessary).
- Manipulate the image accordingly (rescale, etc.)
- Create a new bitmap from the image.
- Delete the temporary image object.
Here is a reusable function that performs all these steps.
-- scale_bitmap.e include wxeud.e public function scale_bitmap( atom bmp, object scale, atom quality = wxIMAGE_QUALITY_NORMAL, integer delete_original = wxFalse ) -- 'scale' can be either a new size (e.g. {width,height}) or a ratio (e.g. '0.50' for 50%) -- get the current bitmap size sequence bmp_size = get_bitmap_size( bmp ) -- use the current size as the default new size sequence new_size = bmp_size if atom( scale ) then -- this is a scaling ratio, so multiply it by the current size new_size = bmp_size * scale elsif sequence( scale ) and length( scale ) = 2 then -- this is a specific new size to use new_size = scale end if -- create a wxImage from the wxBitmap atom img = convert_to_image( bmp ) if delete_original then -- delete the original bitmap object delete_instance( bmp ) end if -- scale the image to the new size using the given quality img = rescale( img, new_size[1], new_size[2], quality ) -- create a new bitmap from the image bmp = create( wxBitmap, {BM_FROM_IMAGE, img} ) -- delete the temporary image object delete_instance( img ) -- return the new bitmap return bmp end function
And a demo that uses this function.
-- bitmap_demo.ex include wxeud.e include scale_bitmap.e -- some constants for easy reading constant NULL = 0 constant wxID_ANY = -1 -- create an empty bitmap to prevent crashing, because -- wxStaticBitmap controls need *something* when created constant wxNullBitmap = create( wxBitmap, {BM_IN_MEMORY,0,0} ) constant MainFrame = create( wxFrame, {NULL, wxID_ANY, "Bitmap scaling demo", -1, -1, 640, 480} ), MainPanel = create( wxPanel, {MainFrame} ), Bitmap1 = create( wxStaticBitmap, {MainPanel, wxID_ANY, wxNullBitmap, 10, 10, 300, 225} ), Bitmap2 = create( wxStaticBitmap, {MainPanel, wxID_ANY, wxNullBitmap, 320, 10, 300, 225} ), $ -- you can get 'space.jpg' from the wxEuphoria 'demo/resources' directory constant space_jpg1 = create( wxBitmap, {BM_FROM_FILE, "space.jpg", wxBITMAP_TYPE_JPEG} ) set_staticbitmap( Bitmap1, space_jpg1 ) -- create a scaled copy of the bitmap at 50% constant space_jpg2 = scale_bitmap( space_jpg1, 0.50 ) set_staticbitmap( Bitmap2, space_jpg2 ) wxMain( MainFrame )
Hope this helps,
-Greg
7. Re: wxEuphoria image scaling
- Posted by buzzo Nov 21, 2014
- 2055 views
Thank you again Greg... much food for thought in your reply.
After some study, I have revised my original procedures as below..
procedure onPaint_HelloWin( atom this, atom event, atom it, atom event_type ) atom bmp2 dc = create(wxPaintDC,{this}) -- should always use 'this' parameter here begin_drawing( dc ) set_font(dc, Arial) set_text_color ( dc, BrightRed ) wx_puts( {HelloWin,10,10,dc}, "Hello, World!") -- add 'dc' parameter here to get color filename = "c://Euphoria//relatives//Genealogy//Scans//ROS_jr_youth.jpg" bmp = create( wxBitmap, {BM_FROM_FILE, filename, wxBITMAP_TYPE_ANY} ) bmp2 = convert_to_image ( bmp ) bmp_size = get_bitmap_size( bmp2 ) rescale ( bmp2, floor(bmp_size[1] * .1),floor(bmp_size[2] * .1), 0) bmp = create( wxBitmap, {BM_FROM_IMAGE, bmp2} ) draw_bitmap( dc, bmp, 10,500, wxFalse ) end_drawing( dc ) delete_instance( dc ) end procedure set_event_handler( HelloWin, get_id(HelloWin), wxEVT_PAINT, routine_id( "onPaint_HelloWin" )) procedure onPaint_HelloBox( atom this, atom event, atom it, atom event_type ) atom bmp2 dc = create(wxPaintDC,{this}) -- should always use 'this' parameter here begin_drawing( dc ) set_pen( dc, BrightBlue) set_brush( dc,BlueBrush) draw_rectangle(dc,0,0,400,400) -- needs to start at 0,0 (relative to HelloBox) set_pen( dc, BrightRed) set_brush( dc,RedBrush) draw_rectangle(dc,100,100,40,40) set_font(dc, Arial) set_text_color ( dc, BrightRed ) wx_puts( {HelloBox,10,10,dc}, "Hello, World!") -- add 'dc' parameter here to get color filename = "c://Euphoria//relatives//notebook.png" bmp = create( wxBitmap, {BM_FROM_FILE, filename, wxBITMAP_TYPE_ANY} ) draw_bitmap( dc, bmp, 105,105, wxFalse ) filename = "c://Euphoria//relatives//Genealogy//Scans//ROS_jr_youth.jpg" bmp = create( wxBitmap, {BM_FROM_FILE, filename, wxBITMAP_TYPE_ANY} ) bmp2 = convert_to_image ( bmp ) bmp_size = get_bitmap_size( bmp2 ) rescale ( bmp2, floor(bmp_size[1] * .1),floor(bmp_size[2] * .1), 0) bmp = create( wxBitmap, {BM_FROM_IMAGE, bmp2} ) draw_bitmap( dc, bmp, 10,150, wxFalse ) end_drawing( dc ) delete_instance( dc ) end procedure set_event_handler( HelloBox, get_id(HelloBox), wxEVT_PAINT, routine_id( "onPaint_HelloBox" ))
Would not be able to complete this project without your aid and assistance..
Sure is nice when things work out!
Buzzo
8. Re: wxEuphoria image scaling
- Posted by ghaberek (admin) Nov 21, 2014
- 2052 views
It's good to see that things are working out for you. Here are a few more tips based on your current code...
- It looks like you're repeating a lot of code when loading and rescaling your bitmaps during each paint event.
- Load your bitmaps once at the start of your code, before you declare your controls. You already did this with the wxColor objects, which is a good start. Otherwise you'll waste system resource loading the same bitmap over and over again in a paint event.
- Move the code to rescale a bitmap into a separate function as I did above. Then use this to scale your static bitmaps once, outside the paint event. Follow the Don't repeat yourself (DRY) principle.
- Keep your paint events as slim as possible. Your applications can become slow or unresponsive if the paint event takes too long. You can create a wxBitmap in memory as a buffer and draw everything to that, and then copy it to the window with draw_bitmap() during the paint event.
- Load your bitmaps once at the start of your code, before you declare your controls. You already did this with the wxColor objects, which is a good start. Otherwise you'll waste system resource loading the same bitmap over and over again in a paint event.
- You do not have to double-up on forward slashes (/) in strings. You only have to "escape" backslashes (\) because the backslash servers as the "escape" character for other values, like tab (\t) or new line (\n).
-Greg
9. Re: wxEuphoria image scaling
- Posted by buzzo Nov 21, 2014
- 1997 views
Greg, I will keep all of your advice in mind.
Would not be this deep into WxEuphoria without your help.
Buzzo