Main Menu

Misc Image Functions

Started by kevin, November 09, 2012, 10:39:34 AM

Previous topic - Next topic

kevin

  Misc Image Functions

       There's lots of image related functions spread all over the  boards,  so in this thread we'll just make a collection small but handy functions for doing 'something' to and image all in the one place.  



 ResizeIMage

        This function resizes the image without scaling it.  Handy if you want to add a boarder around an image or something.



PlayBASIC Code: [Select]
; Note: Function assumes you're going to pass it legal width and height.  The offset allows you to reposition the old image pixels inside the new image.    

Psub ResizeIMage(ThisImage,NewWidth,NewHeight,OffsetX=0,OffsetY=0)

if ThisImage
if GetIMageStatus(ThisIMage)

oldimg=getsurface()

MaskCOlour=GetIMageMaskCOlour(ThisIMage)
ThisImageTYpe=GetImageType(ThisIMage)

img=NewImage(NewWidth,NEwHeight,ThisIMageType)
IMageMaskColour IMg,MaskCOlour

rendertoimage Img
Cls MaskCOlour
drawimage ThisIMage,OffsetX,OffsetY,false

rendertoimage OldIMg

CopyIMage Img,THisImage
deleteimage IMg

endif
endif

EndPsub





 Copy IMage Faster

        This is a replacement copyimage function from the V1.64N wip blog.   The function tries to avoid a destructive copy and just copy the pixels between the two surfaces where possible.   If doesn't both checking if the destination surface type and source surface types match..    

PlayBASIC Code: [Select]
  Width=1024
Height=800
IMg=NewFXIMage(Width,Height)

Img_Copy=GetFreeImage()

rendertoimage img

For lp =0 to 200
x=rnd(width)
y=rnd(Height)
circlec x,y,rnd(100),true,rndrgb()

next


rendertoscreen

do
Cls

; CopyIMage Img,Img_Copy

CopyIMageFaster(Img,Img_Copy)

drawimage img_Copy,0,0,false

print fps()

Sync
loop


Psub CopyIMageFaster(SrcImg,DestImg)
CopyIt=0

if GetIMageStatus(DestImg)=false
CopyIt=true
else

State=GetImageWidth(SrcImg)=GetImageWidth(DestImg)
State+=GetImageHeight(SrcImg)=GetImageHeight(DestImg)

if State=2
surface=getsurface()
drawimage SrcIMg,0,0,false
rendertoimage surface
else
CopyIt=true
endif

endif

if CopyIt
CopyIMage SrcIMg,DestImg
endif

EndPsub






 CloneImage

        This functions creates copy of the source image and returns the new images index.

PlayBASIC Code: [Select]
Psub CloneIMage(SrcIMage)
ThisIMage=0
if SrcImage
if GetImageStatus(SrcImage)
ThisImage=GetFreeIMage()
CopyIMage SrcIMage,ThisIMage
endif
endif
EndPsub ThisIMage





 GetNewImage

        This functions creates a new image from a section of another image.    

PlayBASIC Code: [Select]
Psub GetNewIMage(SrcImage,x1,y1,x2,y2)

ThisImage=0
if SrcImage
if GetIMageStatus(SrcImage)
Width =abs(x2-x1)
Height =abs(y2-y1)

ThisImage=NewImage(Width,Height,GetImageType(SrcIMage) )
CopyRect SrcIMage,0,0,GetIMageWidth(SrcIMage),GetIMageHeight(SrcIMage),ThisImage,0,0
IMageMaskColour ThisImage,GetIMageMaskCOlour(Srcimage)

endif
endif
EndPSub ThisIMage







 Find The Next Used Image

        This function scans the image list looking the next used image index after the selected index.  


PlayBASIC Code: [Select]
Psub Find_Next_Used_Image(StartIMage)


NextImage=0
For lp= StartIMage+1 to GetIMagequantity()
if GetIMageSTatus(lp)
NExtIMage=lp
exit
endif
next

if NExtIMage=0
For lp= 1 to StartIMage-1
if GetIMageSTatus(lp)
NExtIMage=lp
exit
endif
next
endif

if NextIMage=0
NextIMage=StartIMage

endif

EndPsub NextImage






  Change Image Mask Colour

     This function rebuilds the passed image with the user defined mask colour.  To do it, we just create a second image (same size + type), clear it to the new mask colour, then drawn the old image over it. 

PlayBASIC Code: [Select]
Function Change_Mask_Colour(ThisImage,NewMaskCOlour)
oldsurface=getsurface()
Width =GetImageWidth(ThisImage)
Height =GetImageHeight(ThisImage)
Format =GetImageType(ThisImage)
TempImage=NewImage(Width,Height,Format)
rendertoimage TempImage
cls NewMaskColour
DrawIMage ThisImage,0,0,true
CopyRect TempImage,0,0,width,height,ThisImage,0,0
imagemaskcolour ThisImage,NewMaskCOlour
rendertoimage oldsurface
deleteimage TempImage
EndFunction





Related Articles:

   * Load Image Strip/Pages to an Array
   * Compare Images
   * Make Shadowed Thumbnail
   * Create Mirrored Image
   * Create Shadowed Image
   * Custom Scroll Image





baggey

Ive been thinking about Emulating the speecy BORDER function. This will be very usefull indeed! Thanks Kevin.

PlayBASIC Code: [Select]
; Note: Function assumes you're going to pass it legal width and height.  The offset allows you to reposition the old image pixels inside the new image.    

Psub ResizeIMage(ThisImage,NewWidth,NewHeight,OffsetX=0,OffsetY=0)

if ThisImage
if GetIMageStatus(ThisIMage)

oldimg=getsurface()

MaskCOlour=GetIMageMaskCOlour(ThisIMage)
ThisImageTYpe=GetImageType(ThisIMage)

img=NewImage(NewWidth,NEwHeight,ThisIMageType)
IMageMaskColour IMg,MaskCOlour

rendertoimage Img
Cls MaskCOlour
drawimage ThisIMage,OffsetX,OffsetY,false

rendertoimage OldIMg

CopyIMage Img,THisImage
deleteimage IMg

endif
endif

EndPsub




Kind regards Baggey
Jesus was only famous because of his dad

kevin

  Low Level Pixel Manipulation Example:


   This example shows how to compute the position of pixel in memory given it's Pointer and Pitch.   

PlayBASIC Code: [Select]
   // CReate an image 800,600 in FX format
IMage =NewIMage(800,600,2)


// call our manual fill screen function
Fill_Image(Image,$ff80F6)


// draw the state of this to the screen
drawimage IMage,0,0,false
sync
waitkey



Function Fill_Image(ThisImage,ThisRGB)

local OldSurface=GetSurface()

rendertoimage IMage

// Get the depth of this image
If GetIMageDepth(ThisImage)=32

lockbuffer

// Get the POinter to top left pixel in this image
local Ptr=GetIMagePtr(ThisIMage)

// get the number of bytes that a row of pixels takes
local Pitch =GetIMagePitch(ThisImage)


Local Xlp,Ylp

local Height=GetIMageHeight(ThisImage)

// Manually fill each row with random colours
For ylp=0 to Height-1

// Compute where this row of pixels starts in memory
local RowPtr = Ptr +(ylp*Pitch)


// compute the colour so this row by a sinus fade
FadeColour = rgbfade(ThisRGB,50+sin(ylp)*50)

For Xlp=0 to (GetIMageWidth(ThisImage)-1)*4 step 4

// poke the 32bit rgb colour directly into the image memory
PokeInt RowPtr+Xlp,FadeColour

next

next

unlockbuffer

endif

rendertoimage oldsurface


EndFunction