TGC Codebase Backup



Scale Image whilst retaining Alpha/Transparency values by Scraggle

19th Jan 2006 6:01
Summary

If you want to resize/Scale an image you would have to use bitmaps and unfortunately, bitmaps discard any alpha values. So I have written this function to allow you te resize an im



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `Memblock Scale
`======================
`©Scraggle
`======================
`19th January 2006

load image "BackDrop.jpg",1
load image "Warning.png",2

ResizeImage(2,256,256)

sync on

sprite 1,0,0,2
size sprite 1,256,256

do
	paste image 1,0,0,1
	paste image 2,50,50,1
	
	sprite 1,mousex(),mousey(),2
	
	sync
loop

`***********************************
`   RESIZE IMAGE USING MEMBLOCKS
`***********************************
Function ResizeImage(Image,NewWidth,NewHeight)
	
	local BmC as integer = 1	`Bitmap Colour
	local BmA as integer = 2	`Bitmap Alpha
	local BmCs as integer = 3	`Bitmap Colour Scaled
	local BmAs as integer = 4	`Bitmap Alpha Scaled
	local MbC as integer = 1	`Memblock Colour
	local MbA as integer = 2	`Memblock Alpha
	local Width as dword			`Original Width
	local Height as dword		`Original Height
	local Depth as Dword			
	
	`Put original image in a memblock and get its dimensions
	make memblock from image MbC , Image
	Width = memblock dword(MbC , 0)
	Height = memblock dword(MbC , 4)
	Depth = memblock dword(MbC , 8)
		
	`Make a coloured alpha version of the memblock
	make memblock from image MbA , Image
	
	`Delete the original image for later use
	delete image Image
	
	`Replace the Blue channel with the Alpha channel
	for x = 0 to Width 
		for y = 0 to Height
			
			Position = (y * Width * 4) + (x * 4) + 12
			if Position < get memblock size(MbA)
				Alpha = memblock byte(MbA , Position + 3)
				write memblock byte MbA , Position , Alpha
			endif
			
		next y
	next x
	
	`Put the coloured memblock and the Alpha'd memblock onto bitmaps
	make bitmap from memblock BmC , MbC
	make bitmap from memblock BmA , MbA
	
	`Delete the memblocks for later use
	delete memblock MbC
	delete memblock MbA
	
	`Scale both bitmaps
	create bitmap BmCs ,NewWidth ,NewHeight
	create bitmap BmAs ,NewWidth ,NewHeight
	
	copy bitmap BmC ,0,0,Width,Height ,BmCs ,0,0,NewWidth,NewHeight
	copy bitmap BmA ,0,0,Width,Height ,BmAs ,0,0,NewWidth,NewHeight
	
	`Get two new memblocks from the scaled bitmaps
	make memblock from bitmap MbC , BmCs
	make memblock from bitmap MbA , BmAs
	
	`Copy the contents of the blue channel of the
	`Alpha'd memblock to the alpha channel of the other
	for x = 0 to NewWidth
		for y = 0 to NewHeight
			
			Position = (y * NewWidth * 4) + (x * 4) + 12
			if Position < get memblock size(MbA)
				Alpha = memblock byte(MbA , Position)
				write memblock byte MbC , Position + 3 , Alpha
			endif
			
		next y
	next x
	
	`Create a final scaled image from the resultant memblock
	make image from memblock Image , MbC
	
	`Clear up unwanted memblocks and bitmaps
	delete memblock MbA
	delete memblock MbC
	delete bitmap BmA
	delete bitmap BmC
	delete bitmap BmCs
	delete bitmap BmAs

EndFunction