Here's a collection of image and sprite manipulation functions free of charge (none of this silly pay for code in the store nonsense!)
All the parameters are fully explained in the source code below.
I'd welcome any feedback
SetSpriteTiled( spr, w#, h# )
Description: Performs in much the same way as a simple SetSpriteSize()
However, the texture remains the same size and is instead
tiled across the sprite
SetSpriteSize9Slice( sImg, tSpr, tW, tH, cW, cH )
Description: Creates a sprite by resizing an image without affecting the corners
by dividing the image into nine parts, positioning the
corners and then resizing everything else to fit
SetSpriteSize9SliceFromArea( sImg, tSpr, sX, sY, sW, sH, tW, tH, cW, cH )
Description: Creates a sprite by resizing an image without affecting the corners
by dividing the image into nine parts, positioning the
corners and then resizing everything else to fit.
It differs from SetSpriteSize9Slice() in that an area is passed to the function
and the new sprite is captured and resized from that area of the source image
instead of using the complete image.
GetImage9Slice( sImg, tImgs[], cW, cH )
Description: Slices an image into 9 parts and places the image ID's in an array
GetSprite9Slice( sImg, tSpr[], cW, cH ))
Description: Slices an image into 9 parts, creates sprites from them and places
the sprite ID's in an array
BlendImages( iUpper, iLower, ialpha, Width, Height)
Description: This function will take two images of any size (equal or not)
and blend them together according to the values of a third
greyscale alpha map image
CopyMemblockAreaToMemblock( sMb, tMb, x, y, w, h, tX, tY )
Description: Copy a rectangular area from one memblock to another.
The rectangle can be anywhere in the source image
and it can be positione anywhere in the target image.
CopyImageAreaToImage( sImg, tImg, x, y, w, h, tX, tY )
Description: Copies a rectangular area of an image into another image
The rectangle can be anywhere in the source image
and it can be positioned anywhere in the target image.
CopySpriteAreaToSprite( sSpr, tSpr, x, y, w, h, tX, tY )
Description: Copies a rectangular area of a sprite into another sprite
The rectangle can be anywhere in the source sprite
and it can be positione anywhere in the target sprite.
CopySpriteAreaToMemblock( sSpr, tMb, x, y, w, h, tX, tY )
Description: Copies a rectangular area of a sprite into a memblock
The rectangle can be anywhere in the source sprite
and it can be positione anywhere in the target memblock.
CopyImageAreaToMemblock( sImg, tMb, x, y, w, h, tX, tY )
Description: Copies a rectangular area of an image into a memblock
The rectangle can be anywhere in the source image
and it can be positione anywhere in the target memblock.
CopyImageAreaToSprite( sImg, tSpr, x, y, w, h, tX, tY )
Description: Copies a rectangular area of an image into a sprite
The rectangle can be anywhere in the source image
and it can be positione anywhere in the target sprite.
CreateMemblockEmptyImage(w, h)
Description: Creates a new memblock ready to create an image and
populates the header of the memblock with the dimensions.
CreateMemblockMask( sImg, rm, gm, bm )
Description: Creates a mask based on the alpha channel of the source image
but only if the source image colour matches that passed in
CreateMemblockMaskFromArea( sImg, rm, gm, bm, x, y, w, h )
Description: Creates a mask based on the alpha channel of the source image
but only if the source image colour matches that passed in
and then only within the area specified by the passed parameters
ApplyMaskToImage( img, mask, x, y )
Description: Applies a previously created mask to the alpha channel of an image
Both the mask and the image must exist or the function will fail
InvertMemblockalpha( mb )
Description: Inverts the alpha channel of a given memblock
InvertImageAlpha( img )
Description: Inverts the alpha channel of a given image
/*********** CopyMemblockAreaToMemblock( sMb, tMb, x, y, w, h, tX, tY ) ***************
Description: Copy a rectangular area from one memblock to another.
The rectangle can be anywhere in the source image
and it can be positione anywhere in the target image.
Parameters: sMB - The source memblock - must exit or the function will fail
tMB - The target memblock - must exit or the function will fail
unless the parameter is -1, in which case a new memblock will be created
x,y - The top left of the rectangle to capture
w,h = The width and height of the rectangle to capture
If these values are negative then absolute coordinate are used instead
tX,tY - The top left of the target image to insert the extracted image into.
If tMB = -1 then these values are ignored
Returns: The ID of the target memblock - Only used if a new memblock has been created
*/
function CopyMemblockAreaToMemblock( sMb, tMb, x, y, w, h, tX, tY )
if not GetMemblockExists(sMB)
message( "CopyMemblockAreaToMemblock() called"+chr(13)+"with invalid source memblock")
exitfunction 0
endif
sW = GetMemblockInt(sMB, 0)
if w < 0 then w = abs(w)-x
if h < 0 then h = abs(h)-y
if tMb = -1
tMb = CreateMemblockEmptyImage(w,h)
tW = w
tX = 0
txOff = 0
tyOff = 0
else
if not GetMemblockExists(tMb)
message( "CopyMemblockAreaToMemblock() called"+chr(13)+"with invalid target memblock")
exitfunction 0
endif
insert = TRUE
tW = GetMemblockInt(tMb, 0)
txOff = tX*4
tyOff = (tW*tY*4)
endif
for k = y to y+h
syOff = ((sW*k)*4)+12
CopyMemblock(sMb, tMb, syOff+(x*4), tyOff+txOff+12, w*4 )
if insert
inc tyOff, tw*4
else
inc tyOff, w*4
endif
next k
endfunction tMb
/********** CopyImageAreaToImage( sImg, tImg, x, y, w, h, tX, tY ) *********************
Description: Copies a rectangular area of an image into another image
The rectangle can be anywhere in the source image
and it can be positioned anywhere in the target image.
Parameters: sImg - The source image - must exit or the function will fail
tImg - The target image - must exit or the function will fail
unless the parameter is -1, in which case a new image will be created
x,y - The top left of the rectangle to capture
w,h - The width and height of the rectangle to capture
If these values are negative then absolute coordinate are used instead
tX,tY - The top left of the target image to insert the extracted image into.
If tImg = -1 then these values are ignored
Returns: The ID of the target image - Only used if a new image has been created
*/
function CopyImageAreaToImage( sImg, tImg, x, y, w, h, tX, tY )
if not GetImageExists(sImg)
message( "CopyImageAreaToImage() called"+chr(13)+"with invalid source image")
exitfunction 0
endif
if tImg > -1
if not GetImageExists(sImg)
message( "CopyImageAreaToImage() called"+chr(13)+"with invalid target image")
exitfunction 0
endif
tMb = CreateMemblockFromImage(tImg)
else
tMb = -1
endif
if w < 0 then w = abs(w)-x
if h < 0 then h = abs(h)-y
sMb = CreateMemblockFromImage(sImg)
tMb2 = CopyMemblockAreaToMemblock( sMb, tMb, x, y, w, h, tX, tY )
tImg = CreateImageFromMemblock( tMb2 )
DeleteMemblock(sMb)
DeleteMemblock(tMb2)
endfunction tImg
/********** CopySpriteAreaToSprite( sSpr, tSpr, x, y, w, h, tX, tY ) *******************
Description: Copies a rectangular area of a sprite into another sprite
The rectangle can be anywhere in the source sprite
and it can be positione anywhere in the target sprite.
Parameters: sSpr - The source sprite - must exit or the function will fail
tSpr - The target sprite - must exit or the function will fail
unless the parameter is -1, in which case a new sprite will be created
x,y - The top left of the rectangle to capture
w,h - The width and height of the rectangle to capture
If these values are negative then absolute coordinate are used instead
tX,tY - The top left of the target sprite to insert the extracted sprite into.
If tSpr = -1 then these values are ignored
Returns: The ID of the target sprite - Only used if a new sprite has been created
*/
function CopySpriteAreaToSprite( sSpr, tSpr, x, y, w, h, tX, tY )
if not GetSpriteExists(sSpr)
message( "CopySpriteAreaToSprite() called"+chr(13)+"with invalid source sprite")
exitfunction 0
endif
if tSpr > -1
if not GetSpriteExists(sSpr)
message( "CopySpriteAreaToSprite() called"+chr(13)+"with invalid target sprite")
exitfunction 0
endif
tImg = GetSpriteImageID(tSpr)
tMb = CreateMemblockFromImage(tImg)
else
tMb = -1
endif
if w < 0 then w = abs(w)-x
if h < 0 then h = abs(h)-y
sImg = GetSpriteImageID(sSpr)
sMb = CreateMemblockFromImage(sImg)
tMb = CopyMemblockAreaToMemblock( sMb, tMb, x, y, w, h, tX, tY )
tImg = CreateImageFromMemblock( tMb )
if tSpr > -1
SetSpriteImage(tSpr, tImg)
else
tSpr = CreateSprite(tImg)
endif
DeleteMemblock(sMB)
endfunction tSpr
/********** CopySpriteAreaToMemblock( sSpr, tMb, x, y, w, h, tX, tY ) ****************
Description: Copies a rectangular area of a sprite into a memblock
The rectangle can be anywhere in the source sprite
and it can be positione anywhere in the target memblock.
Parameters: sSpr - The source sprite - must exit or the function will fail
tMb - The target memblock - must exit or the function will fail
unless the parameter is -1, in which case a new memblock will be created
x,y - The top left of the rectangle to capture
w,h - The width and height of the rectangle to capture
If these values are negative then absolute coordinate are used instead
tX,tY - The top left of the target memblock to insert the extracted sprite into.
If tMb = -1 then these values are ignored
Returns: The ID of the target memblock - Only used if a new memblock has been created
*/
function CopySpriteAreaToMemblock( sSpr, tMb, x, y, w, h, tX, tY )
if not GetSpriteExists(sSpr)
message( "CopySpriteAreaToMemblock() called"+chr(13)+"with invalid source sprite")
exitfunction 0
endif
if w < 0 then w = abs(w)-x
if h < 0 then h = abs(h)-y
sMb = CreateMemblockFromImage( GetSpriteImageID(sSpr) )
tMb = CopyMemblockAreaToMemblock( sMb, tMb, x, y, w, h, tX, tY )
DeleteMemblock(sMB)
endfunction tMb
/********** CopyImageAreaToMemblock( sImg, tMb, x, y, w, h, tX, tY ) ********************
Description: Copies a rectangular area of an image into a memblock
The rectangle can be anywhere in the source image
and it can be positione anywhere in the target memblock.
Parameters: sImg - The source image - must exit or the function will fail
tMb - The target memblock - must exit or the function will fail
unless the parameter is -1, in which case a new memblock will be created
x,y - The top left of the rectangle to capture
w,h - The width and height of the rectangle to capture
If these values are negative then absolute coordinate are used instead
tX,tY - The top left of the target memblock to insert the extracted image into.
If tMb = -1 then these values are ignored
Returns: The ID of the target memblock - Only used if a new memblock has been created
*/
function CopyImageAreaToMemblock( sImg, tMb, x, y, w, h, tX, tY )
if not GetImageExists(sImg)
message( "CopyImageAreaToMemblock() called"+chr(13)+"with invalid source image")
exitfunction 0
endif
sMb = CreateMemblockFromImage( sImg )
rMb = CopyMemblockAreaToMemblock( sMb, tMb, x, y, w, h, tX, tY )
DeleteMemblock(sMB)
endfunction rMb
/********** CopyImageAreaToSprite( sImg, tSpr, x, y, w, h, tX, tY ) *********************
Description: Copies a rectangular area of an image into a sprite
The rectangle can be anywhere in the source image
and it can be positione anywhere in the target sprite.
Parameters: sImg - The source image - must exit or the function will fail
tSpr - The target sprite - must exit or the function will fail
unless the parameter is -1, in which case a new sprite will be created
x,y - The top left of the rectangle to capture
w,h - The width and height of the rectangle to capture
If these values are negative then absolute coordinate are used instead
tX,tY - The top left of the target sprite to insert the extracted image into.
If tMb = -1 then these values are ignored
Returns: The ID of the target sprite - Only used if a new sprite has been created
*/
function CopyImageAreaToSprite( sImg, tSpr, x, y, w, h, tX, tY )
if not GetImageExists(sImg)
message( "CopyImageAreaToSprite() called"+chr(13)+"with invalid source image")
exitfunction 0
endif
if tSpr > -1
if not GetSpriteExists(tSpr)
message( "CopySpriteAreaToSprite() called"+chr(13)+"with invalid target sprite")
exitfunction 0
endif
tMb = CreateMemblockFromImage( GetSpriteImageID(tSpr) )
else
tMb = -1
endif
sMb = CreateMemblockFromImage(sImg)
rMb = CopyMemblockAreaToMemblock( sMb, tMb, x, y, w, h, tX, tY )
tImg = CreateImageFromMemblock(rMB)
if tSpr = -1
tSpr = createsprite(tImg)
else
SetSpriteImage(tSpr, tImg)
endif
DeleteMemblock(sMb)
DeleteMemblock(tMb)
endfunction tSpr
/************* SetSpriteSize9Slice( sImg, tSpr, tW, tH, cW, cH ) *************
Description: Creates a sprite by resizing an image without affecting the corners
by dividing the image into nine parts, positioning the
corners and then resizing everything else to fit
Parameters: sImg - The source image, this must exist of the function will fail
tSpr - The target sprite, this must exist of the function will fail
unless the parameter is -1 in which case a new sprite is returned
tW - The wisth of the target wprite
tH - The height of the target sprite
cW - The width of the corner that will not be resized,
if -1 then the width of the source image is divided evenly in 3 parts
cH - The height of the corner that will not be resized
if -1 then the height of the source image is divided evenly in 3 parts
Returns: tSpr - The value of the target sprite - only used if the passed tSpr is -1
*/
function SetSpriteSize9Slice( sImg, tSpr, tW, tH, cW, cH )
if not GetImageExists(sImg)
message( "SetSpriteSize9Slice() called"+chr(13)+"with invalid source image")
exitfunction 0
endif
if tSpr > -1
if not GetSpriteExists(tSpr)
message( "CopySpriteAreaToSprite() called"+chr(13)+"with invalid target sprite")
exitfunction 0
endif
endif
if cW = -1 then cW = GetImageWidth(sImg)/3
if cH = -1 then cH = GetImageHeight(sImg)/3
tTopW = iMax(tW - cW*2, 1)
tLeftH = iMax(tH - cH*2, 1)
sW = GetImageWidth(sImg)
sH = GetImageHeight(sImg)
tMb = CreateMemblockEmptyImage( tW, tH )
c = MakeColor(200, 0, 100)
for k = 12 to tW*tH*4+12 step 4
SetMemblockInt(tMb,k,c)
next k
//Place Corners
tMb = CopyImageAreaToMemblock( sImg, tMb, 0, 0, cW, cH, 0, 0 )
tMb = CopyImageAreaToMemblock( sImg, tMb, sW-cW, 0, cW, cH, tW-cW, 0 )
tMb = CopyImageAreaToMemblock( sImg, tMb, 0, sH-cH, cW, cH, 0, tH-cH )
tMb = CopyImageAreaToMemblock( sImg, tMb, sW-cW, sH-cH, cW, cH, tW-cW, tH-cH )
//Make Top
sTopW = sW - cW*2
tempImg = myResizeImage( CopyImageAreaToImage( sImg, -1, cW, 0, sTopW, cH, 0, 0 ), tTopW, cH)
tMb = CopyImageAreaToMemblock( tempImg, tMb, 0, 0, tTopW, cH, cW, 0 )
DeleteImage(tempImg)
//Make Bottom
tempImg = myResizeImage( CopyImageAreaToImage( sImg, -1, cW, sH-cH, sTopW, cH, 0, 0 ), tTopW, cH)
tMb = CopyImageAreaToMemblock( tempImg, tMb, 0, 0, tTopW, cH, cW, tH-cH )
DeleteImage(tempImg)
//Make Left
sLeftH = sH - cH*2
tempImg = myResizeImage( CopyImageAreaToImage( sImg, -1, 0, cH, cW, sLeftH, 0, 0 ), cW, tLeftH)
tMb = CopyImageAreaToMemblock( tempImg, tMb, 0, 0, cW, tLeftH, 0, cH )
DeleteImage(tempImg)
//Make Right
tempImg = myResizeImage( CopyImageAreaToImage( sImg, -1, sW-cW, cH, cW, sLeftH, 0, 0 ), cW, tLeftH)
tMb = CopyImageAreaToMemblock( tempImg, tMb, 0, 0, cW, tLeftH, tW-cW, cH )
DeleteImage(tempImg)
//Make Middle
tempImg = myResizeImage( CopyImageAreaToImage( sImg, -1, cW, cH, sTopW, sLeftH, 0, 0 ), tTopW, tLeftH)
tMb = CopyImageAreaToMemblock( tempImg, tMb, 0, 0, tTopW, tLeftH, cW, cH )
DeleteImage(tempImg)
if tSpr = -1
tSpr = createsprite( CreateImageFromMemblock( tMb ) )
else
SetSpriteImage( tSpr, CreateImageFromMemblock( tMb ) )
endif
DeleteMemblock(tMB)
endfunction tSpr
/************ SetSpriteSize9SliceFromArea( sImg, tSpr, sX, sY, sW, sH, tW, tH, cW, cH ) *****************
Description: Creates a sprite by resizing an image without affecting the corners
by dividing the image into nine parts, positioning the
corners and then resizing everything else to fit.
It differs from SetSpriteSize9Slice() in that an area is passed to the function
and the new sprite is captured and resized from that area of the source image
instead of using the complete image.
Parameters: sImg - The source image, this must exist of the function will fail
tSpr - The target sprite, this must exist of the function will fail
unless the parameter is -1 in which case a new sprite is returned
sX - The X position of the area to capture from the source image
sY - The Y position of the area to capture from the source image
sW - The width of the area to capture from.
If the value is negative then this is an absolute value instead
sH - The height of the area to capture from
If the value is negative then this is an absolute value instead
tW - The wisth of the target sprite
tH - The height of the target sprite
cW - The width of the corner that will not be resized,
if -1 then the width of the source image is divided evenly in 3 parts
cH - The height of the corner that will not be resized
if -1 then the height of the source image is divided evenly in 3 parts
Returns: tSpr - The value of the target sprite - only used if the passed tSpr is -1
*/
function SetSpriteSize9SliceFromArea( sImg, tSpr, sX, sY, sW, sH, tW, tH, cW, cH )
if sW < 0 then sW = abs(sW)-sX
if sH < 0 then sH = abs(sH)-sY
areaImg = CopyImageAreaToImage( sImg, -1, sX, sY, sW, sH, 0, 0 )
tSpr = SetSpriteSize9Slice( areaImg, tSpr, tW, tH, cW, cH )
endfunction tSpr
/************* GetImage9Slice( sImg, tImgs[], cW, cH ) *************
Description: Slices an image into 9 parts and places them in an array
Parameters: sImg - The source image, this must exist of the function will fail
tImgs - an array of integers with a size of 8 that you must have previously
defined. The resultant images will be stored in the array with 0 at the
top left then working across and down.
cW - The width of the corner that will not be resized,
if -1 then the width of the source image is divided evenly in 3 parts
cH - The height of the corner that will not be resized
if -1 then the height of the source image is divided evenly in 3 parts
Returns: 1 if successful or -1 if failed
Although not a return - the tImgs array will be populated with image ID's
*/
function GetImage9Slice( sImg, tImgs REF as integer[], cx, cy )
if not GetImageExists(sImg)
message( "GetImage9Slice()"+chr(13)+"called with invalid source image")
exitfunction -1
endif
if cW = -1 then cW = GetImageWidth(sImg)/3
if cH = -1 then cH = GetImageHeight(sImg)/3
imageWidth = GetImageWidth(sImg)
imageHeight = GetImageHeight(sImg)
middleWidth = imageWidth-cx*2
middleHeight = imageHeight-cy*2
sMb = CreateMemblockFromImage(sImg)
mb as integer[8]
//Top Left
mb[0] = CreateMemblockEmptyImage(cx, cy)
CopyMemblockAreaToMemblock(sMb, mb[0], 0, 0, cx, cy, 0, 0)
//Top Middle
mb[1] = CreateMemblockEmptyImage(middleWidth, cy)
CopyMemblockAreaToMemblock(sMb, mb[1], cx, 0, middleWidth, cy, 0, 0)
//Top Right
mb[2] = CreateMemblockEmptyImage(cx, cy)
CopyMemblockAreaToMemblock(sMb, mb[2], imageWidth-cx, 0, cx, cy, 0, 0)
//Middle Left
mb[3] = CreateMemblockEmptyImage(cx, middleHeight)
CopyMemblockAreaToMemblock(sMb, mb[3], 0, cy, cx, middleHeight, 0, 0)
//Middle Middle
mb[4] = CreateMemblockEmptyImage(middleWidth, middleHeight)
CopyMemblockAreaToMemblock(sMb, mb[4], cx, cy, middleWidth, middleHeight, 0, 0)
//Middle Right
mb[5] = CreateMemblockEmptyImage(cx, middleHeight)
CopyMemblockAreaToMemblock(sMb, mb[5], imageWidth-cx, cy, cx, middleHeight, 0, 0)
//Bottom Left
mb[6] = CreateMemblockEmptyImage(cx, cy)
CopyMemblockAreaToMemblock(sMb, mb[6], 0, imageHeight-cy, cx, cy, 0, 0)
//Bottom Middle
mb[7] = CreateMemblockEmptyImage(middleWidth, cy)
CopyMemblockAreaToMemblock(sMb, mb[7], cx, imageWidth-cx, middleWidth, cy, 0, 0)
//Bottom Right
mb[8] = CreateMemblockEmptyImage(cx, cy)
CopyMemblockAreaToMemblock(sMb, mb[8], imageWidth-cx, imageHeight-cy, cx, cy, 0, 0)
for k = 0 to 8
tImgs[k] = CreateImageFromMemblock(mb[k])
DeleteMemblock(mb[k])
next k
endfunction 1
/************* GetSprite9Slice( sImg, tSpr[], cW, cH ) *************
Description: Slices an image into 9 parts and places them in an array
Parameters: sImg - The source image, this must exist of the function will fail
tSpr - an array of integers with a size of 8 that you must have previously
defined. The resultant sprites will be stored in the array with 0 at the
top left then working across and down.
cW - The width of the corner that will not be resized,
if -1 then the width of the source image is divided evenly in 3 parts
cH - The height of the corner that will not be resized
if -1 then the height of the source image is divided evenly in 3 parts
Returns: 1 if successful or -1 if failed
Although not a return - the tSpr array will be populated with sprite ID's
*/
function GetSprite9Slice( sImg, tSpr REF as integer[], cx, cy )
if not GetImageExists(sImg)
message( "GetSprite9Slice()"+chr(13)+"called with invalid source image")
exitfunction -1
endif
tImgs as integer[8]
if GetImage9Slice( sImg, tImgs, cx, cy ) = -1
exitfunction -1
endif
for k = 0 to 8
tSpr[k] = CreateSprite(tImgs[k])
SetSpriteVisible(tSpr[k], FALSE)
next k
endfunction 1
/********** CreateMemblockEmptyImage(w, h) **************************
Description: Creates a new memblock ready to create an image and
populates the header of the memblock with the dimensions.
Parameters: w - The width of the image
h - The height of the image
Returns: mb - The memblock ID of the newly created memblock
*/
function CreateMemblockEmptyImage(w, h)
mb = CreateMemblock(w*h*4+12)
SetMemblockInt(mb, 0, w)
SetMemblockInt(mb, 4, h)
SetMemblockInt(mb, 8, 32)
endfunction mb
/********** CreateMemblockMask( sImg, rm, gm, bm ) **************************
Description: Creates a mask based on the alpha channel of the source image
but only if the source image colour matches that passed in
Parameters: sImg - The source image used to create a mask
rm - the red component of the source image used to establish
if a mask should be created from that pixel
rg - The green component (as above)
rb - The blue component (as above)
Returns: tMb - The memblock ID of the newly created memblock
*/
function CreateMemblockMask( sImg, rm, gm, bm )
if not GetImageExists(sImg)
message( "CreateMemblockMask() called"+chr(13)+"with invalid source image")
exitfunction 0
endif
sMb = CreateMemblockFromImage(sImg)
tMb = CreateMemblockEmptyImage( GetImageWidth(sImg), GetImageHeight(sImg) )
for k = 12 to GetMemblockSize(sMb) step 4
if GetMemblockByte(sMb, k) = rm or rm = -1
if GetMemblockByte(sMb, k+1) = gm or gm = -1
if GetMemblockByte(sMb, k+2) = bm or bm = -1
for j = 0 to 3
SetMemblockByte(tMb, k+j, GetMemblockByte(sMb, k+3))
next j
endif
endif
endif
next k
img = CreateImageFromMemblock(tMb)
SaveImage(img, "mask.png")
endfunction tMb
/********** CreateMemblockMaskFromArea( sImg, rm, gm, bm, x, y, w, h ) **************************
Description: Creates a mask based on the alpha channel of the source image
but only if the source image colour matches that passed in
and then only within the area specified by the passed parameters
Parameters: sImg - The source image used to create a mask
rm - the red component of the source image used to establish
if a mask should be created from that pixel
rg - The green component (as above)
rb - The blue component (as above)
sX - The X position of the area to capture from the source image
sY - The Y position of the area to capture from the source image
sW - The width of the area to capture from.
If the value is negative then this is an absolute value instead
sH - The height of the area to capture from
If the value is negative then this is an absolute value instead
Returns: tMb - The memblock ID of the newly created memblock
*/
function CreateMemblockMaskFromArea( sImg, rm, gm, bm, x, y, w, h )
if not GetImageExists(sImg)
message( "CreateMemblockMask() called"+chr(13)+"with invalid source image")
exitfunction 0
endif
if w < 0 then w = abs(w)-x
if h < 0 then h = abs(h)-y
SetSpriteTransparency(sImg, 1)
tempMb = CreateMemblockEmptyImage( GetImageWidth(sImg), GetImageHeight(sImg) )
tempMb = CopyImageAreaToMemblock( sImg, tempMb, x, y, w, h ,0, 0 )
tempImg = CreateImageFromMemblock(tempMb)
tMb = CreateMemblockMask(tempImg, rm, gm, bm)
DeleteImage(tempImg)
DeleteMemblock(tempMb)
endfunction tMb
/********** ApplyMaskToImage( img, mask, x, y ) **************************
Description: Applies a previously created mask to the alpha channel of an image
Both the mask and the image must exist or the function will fail
Parameters: img - The image to apply the alpha mask on to
mask - the ID of a memblock holding a previously created mask
x,y - the coordinates to offset the mask.
Returns: nothing
*/
function ApplyMaskToImage( img, mask, x, y )
tempImg = CreateImageFromMemblock(mask)
SetImageMask(img, tempImg, 4, 4, x, y )
DeleteImage(tempImg)
endfunction
/********** InvertMemblockalpha( mb ) **************************
Description: Inverts the alpha channel of a given memblock
Parameters: mb - The memblock who's alpha channel you wish to invert
Returns: nothing
*/
function InvertMemblockAlpha( mb )
for k = 15 to GetMemblockSize(mb) step 4
thisalpha = GetMemblockByte(mb, k)
alpha = 255-thisalpha
SetMemblockByte(mb, k, alpha)
next k
endfunction
/********** InvertImageAlpha( img ) **************************
Description: Inverts the alpha channel of a given image
Parameters: img - The image who's alpha channel you wish to invert
Returns: nothing
*/
function InvertImageAlpha( img )
mb = CreateMemblockFromImage(img)
for k = 15 to GetMemblockSize(mb) step 4
thisalpha = GetMemblockByte(mb, k)
alpha = 255-thisalpha
SetMemblockByte(mb, k, alpha)
next k
CreateImageFromMemblock(img, mb)
DeleteMemblock(mb)
endfunction
/********** SetSpriteTiled( spr, w#, h# ) **************************
Description: Performs in much the same way as a simpe SetSpriteSize()
However, the texture remains the same size and is instead
tiled across the sprite
Parameters: spr - The sprite to resize
w#,h# - the new width of height of the sprite
Returns: nothing
*/
function SetSpriteTiled( spr, w#, h# )
SetSpriteSize(spr, w#, h#)
img = GetSpriteImageID(spr)
SetImageWrapU(img, 1)
SetImageWrapV(img, 1)
qx# = (GetImageWidth(img)/w#)
qy# = (GetImageHeight(img)/h#)
SetSpriteUVScale(spr, qx#, qy#)
endfunction
/********** BlendImages( iUpper, iLower, ialpha, Width, Height) *************
Description: This function will take two images of any size (equal or not)
and blend them together according to the values of a third
greyscale alpha map image
Parameters: iUpper - The image that will show through in the white areas of the alpha map
iLower - The image that will show through in the black areas of the alpha map
ialpha - The greyscale image used to blend iUpper & iLower
Width - The width you want the resultant image to be
Height - The height you want the resultant image to be
Returns: iResult - The image number of the resultant blended image
Note: The images do not need to be the same size nor do any of them need to
be the size of the resultant image.
They will all be scaled automatically.
*/
function BlendImages( iUpper, iLower, ialpha, Width, Height)
Local Percent as float
Local UpperBlue as integer
Local UpperGreen as integer
Local UpperRed as integer
Local LowerBlue as integer
Local LowerGreen as integer
Local LowerRed as integer
Local ResultBlue as integer
Local ResultGreen as integer
Local ResultRed as integer
Local mUpper as integer
Local mLower as integer
Local malpha as integer
Local mResult as integer
Local iResult as integer
if getimagewidth(iUpper) <> width or getimageheight(iUpper) <> height then iUpper = myResizeImage( iUpper, Width, Height )
if getimagewidth(iLower) <> width or getimageheight(iLower) <> height then iLower = myResizeImage( iLower, Width, Height )
if getimagewidth(ialpha) <> width or getimageheight(ialpha) <> height then ialpha = myResizeImage( ialpha, Width, Height )
mUpper = 1
mLower = 2
malpha = 3
mResult = 4
CreateMemblockFromImage( mUpper, iUpper )
CreateMemblockFromImage( mLower, iLower )
CreateMemblockFromImage( malpha, ialpha )
CreateMemblockFromImage( mResult, iUpper )
mbSize = getmemblocksize( mUpper )
for Pos = 12 to mbSize step 4
Percent = ( getmemblockbyte( malpha, Pos ) / 255.0 )
UpperBlue = getMemblockByte( mUpper, Pos + 0 )
UpperGreen = getMemblockByte( mUpper, Pos + 1 )
UpperRed = getMemblockByte( mUpper, Pos + 2 )
LowerBlue = getMemblockByte( mLower, Pos + 0 )
LowerGreen = getMemblockByte( mLower, Pos + 1 )
LowerRed = getMemblockByte( mLower, Pos + 2 )
ResultBlue = UpperBlue * ( 1- Percent ) + LowerBlue * Percent
ResultGreen = UpperGreen * ( 1- Percent ) + LowerGreen * Percent
ResultRed = UpperRed * ( 1- Percent ) + LowerRed * Percent
setmemblockbyte( mResult, Pos + 0, ResultBlue )
setmemblockbyte( mResult, Pos + 1, ResultGreen )
setmemblockbyte( mResult, Pos + 2, ResultRed )
setmemblockbyte( mResult, Pos + 3, 255 )
next Pos
iResult = CreateImageFromMemblock( mResult )
deletememblock( mUpper )
deletememblock( mLower )
deletememblock( malpha )
deletememblock( mResult )
endfunction iResult
AGK V2 user - Tier 1 (mostly)