YAL bugfix

Blitz3D Forums/Blitz3D Programming/YAL bugfix

sswift(Posted 2003) [#1]
Function LMImageMeasureContrast%(img)

	;minvalue = 255
	;maxvalue = 0
		
	minvalue_r = 255
	minvalue_g = 255
	minvalue_b = 255
	
	width = ImageWidth(img)
	height = ImageHeight(img)
	
	LockBuffer(ImageBuffer(img))
	
	For y = 0 To height-1
		For x = 0 To width-1
			rgb1 = ReadPixelFast(x, y, ImageBuffer(img)) And $FFFFFF
			r1 = (rgb1 Shr 16 And %11111111)
			g1 = (rgb1 Shr 8 And %11111111)
			b1 = (rgb1 And %11111111)
			
;			If r1 > maxvalue Then maxvalue = r1
;			If g1 > maxvalue Then maxvalue = g1
;			If b1 > maxvalue Then maxvalue = b1

;			If r1 < minvalue Then minvalue = r1
;			If g1 < minvalue Then minvalue = g1
;			If b1 < minvalue Then minvalue = b1

			; (sswift)
			; What you really want to measure is the contrast of each channel, and then select the channel with the
			; max contrast.  What you're doing above would assume that an image that is pure red has a lot of contrast
			; because blue and green are 0 and red is 255.
			
			If r1 > maxvalue_r Then maxvalue_r = r1
			If g1 > maxvalue_g Then maxvalue_g = g1
			If b1 > maxvalue_b Then maxvalue_b = b1

			If r1 < minvalue_r Then minvalue_r = r1
			If g1 < minvalue_g Then minvalue_g = g1
			If b1 < minvalue_b Then minvalue_b = b1

		Next
	Next
	UnlockBuffer(ImageBuffer(img))
	
	;(sswift)
	
		contrast_r = maxvalue_r - minvalue_r
		contrast_g = maxvalue_g - minvalue_g
		contrast_b = maxvalue_b - minvalue_b
	
		If (contrast_r > contrast_g) And (contrast_r > contrast_b) Then Return contrast_r
		If (contrast_g > contrast_r) And (contrast_g > contrast_b) Then Return contrast_g
		Return contrast_b
	
	;Return maxvalue - minvalue
		
End Function