Re: wxEuphoria image scaling

new topic     » goto parent     » topic index » view thread      » older message » newer message
buzzo said...

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...

  1. Create a bitmap from your source (file, memory, etc.)
  2. Convert the bitmap to an image.
  3. Delete the original bitmap (if necessary).
  4. Manipulate the image accordingly (rescale, etc.)
  5. Create a new bitmap from the image.
  6. 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

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu