ResizeImage Bug?

BlitzPlus Forums/BlitzPlus Programming/ResizeImage Bug?

VIP3R(Posted 2005) [#1]
Hi all,

I've stumbled across an oddity with the ResizeImage command and wondered if it's a bug.

Resizing an image of 500x300 pixels down to 133x80 pixels results in an image of 133x81 pixels.

Why is that? Shouldn't it be cropped to the specified size?

Example, no media required...



Grey Alien(Posted 2005) [#2]
I found the same thing a while ago, it anoyed me so I used some faster scaling code that I found in the archives instead.


Wayne(Posted 2005) [#3]
It would seem the compiler writer missed it by one.
I'm surprised that the error has gone on this long.
Is that bug in B3D too ?

hehe

lets hope it get's reviewed and fixed with next release.


sswift(Posted 2005) [#4]
Here's a function to scale an image fast. It's much faster than the built in function, and accurate. Also it should be twice as fast as the version I have in the code archives due to changing a single flag on the scratch image.

; -------------------------------------------------------------------------------------------------------------------------------------
; This function scales an image an arbitrary amount on the X and Y axis, and returns a pointer to the new image.
; The original image is not modified.
;
; The scale can either be a relative width and height, ie, 0.5 of the width of the original image, or it can be an exact size in pixels.
; If an exact size is desired, set ExactSize to true.
;
; This function is 80x faster than the ScaleImage function that comes with Blitz! 
; -------------------------------------------------------------------------------------------------------------------------------------
Function ScaleImageFast(SrcImage, ScaleX#, ScaleY#, ExactSize=False)

	Local SrcWidth,  SrcHeight
	Local DestWidth, DestHeight
	Local ScratchImage, DestImage
	Local SrcBuffer, ScratchBuffer, DestBuffer
	Local X1, Y1, X2, Y2
	
	; Get the width and height of the source image. 	
		SrcWidth  = ImageWidth(SrcImage)
		SrcHeight = ImageHeight(SrcImage)

	; Calculate the width and height of the dest image, or the scale.
		If ExactSize = False
	
			DestWidth  = Floor(SrcWidth  * ScaleX#)
			DestHeight = Floor(SrcHeight * ScaleY#)
	
		Else
	
			DestWidth  = ScaleX#
			DestHeight = ScaleY#

			ScaleX# = Float(DestWidth)  / Float(SrcWidth)
			ScaleY# = Float(DestHeight) / Float(SrcHeight)
	
		EndIf			

	; If the image does not need to be scaled, just copy the image and exit the function.
		If (SrcWidth = DestWidth) And (SrcHeight = DestHeight) Then Return CopyImage(SrcImage)

	; Create a scratch image that is as tall as the source image, and as wide as the destination image.
		ScratchImage = CreateImage(DestWidth, SrcHeight, 1, 1)
				
	; Create the destination image.
		DestImage = CreateImage(DestWidth, DestHeight, 1, 1) 

	; Get pointers to the image buffers.
		SrcBuffer     = ImageBuffer(SrcImage)
		ScratchBuffer = ImageBuffer(ScratchImage)
		DestBuffer    = ImageBuffer(DestImage)

	; Duplicate columns from source image to scratch image.
		For X2 = 0 To DestWidth-1
			X1 = Floor(X2 / ScaleX#)
			CopyRect X1, 0, 1, SrcHeight, X2, 0, SrcBuffer, ScratchBuffer
		Next
			
	; Duplicate rows from scratch image to destination image.
		For Y2 = 0 To DestHeight-1
			Y1 = Floor(Y2 / ScaleY#)
			CopyRect 0, Y1, DestWidth, 1, 0, Y2, ScratchBuffer, DestBuffer
		Next
						
	; Free the scratch image.
		FreeImage ScratchImage					
						
	; Return the new image.
		Return DestImage
					
End Function	



Grey Alien(Posted 2005) [#5]
That's exactly the code I meant. The flags are safer like that on Create image too in the event of an Alt+Tab the images won't corrupt.