I've been working on a function to crop an image but I'm stuck at the moment. This is what is happening on screen:
The function:
// How I'm calling the function. The original image is 1920x1080.
newImgID = CropImage( 1, 0, 0, 1080, 1080 )
function clamp( _val as integer, _min as integer, _max as integer )
if _val < _min
exitfunction _min
elseif _val > _max
exitfunction _max
endif
endfunction _val
function CropImage( _imgID, _x, _y, _w, _h )
if GetImageExists( _imgID ) = 0 then exitfunction 0
_imgWidth = GetImageWidth( _imgID )
//~ _imgHeight = GetImageHeight( _imgID )
_memFrom = CreateMemblockFromImage( _imgID )
_memFromMax = GetMemblockSize( _memFrom ) - 4
_memToSize = ( ( _w * _h ) * 4 ) + 12
_memTo = CreateMemblock( _memToSize )
_memToMax = _memToSize - 4
// Set header.
SetMemblockInt( _memTo, 0, _w )
SetMemblockInt( _memTo, 4, _h )
SetMemblockInt( _memTo, 8, 32 )
_memTo_P = 12
for yy = _y to _h
for xx = _x to _w
_memFrom_P = ( xx + ( yy * _imgWidth ) ) * 4
_memFrom_P = clamp( _memFrom_P, 12, _memFromMax )
_memTo_P = clamp( _memTo_P, 12, _memToMax )
SetMemblockByte(_memTo, _memTo_P, GetMemblockByte( _memFrom, _memFrom_P ) )
SetMemblockByte(_memTo, _memTo_P + 1, GetMemblockByte( _memFrom, _memFrom_P + 1 ) )
SetMemblockByte(_memTo, _memTo_P + 2, GetMemblockByte( _memFrom, _memFrom_P + 2 ) )
SetMemblockByte(_memTo, _memTo_P + 3, GetMemblockByte( _memFrom, _memFrom_P + 3 ) )
//~ Print_int( "xx: ", xx )
//~ Print_int( "yy: ", yy )
//~ Print_int( "_memTo_P: ", _memTo_P )
//~ Print_int( "_memFrom_P: ", _memFrom_P )
inc _memTo_P, 4
//~ Sync()
//~ sleep( 750 )
next xx
next yy
retImg = CreateImageFromMemblock( _memTo )
DeleteMemblock( _memTo )
DeleteMemblock( _memTo_P )
endfunction retImg