HomeForumSourceResearchGuide
Sign in to contribute to source. how it works
Component media.image.ImageTransform by barry
expand copy to clipboardexpand
interface ImageTransformLib {
	PixelMapYUV rgbaToYUV(PixelMapYUV output, PixelMap input, byte format)
	PixelMap yuvToRGBA(PixelMap output, PixelMapYUV input, bool useChroma, int r, int g, int b, int fgt, int bgt)
	PixelMap chromaKey(PixelMap output, PixelMap input, int r, int g, int b, int fgt, int bgt, byte pixelFormat)
	PixelMap runMask(PixelMap output, PixelMap input, byte mask[], byte pixelFormat)
}

component provides ImageTransform requires io.Output out, data.IntUtil iu, native ImageTransformLib lib {

	PixelMap pixOut
	PixelMapYUV pixOutYUV

	ImageTransform:ImageTransform(WH outputSize)
		{
		pixOut = new PixelMap(new WH(outputSize.width, outputSize.height), new byte[outputSize.width * outputSize.height * 4])

		int strideY = outputSize.width
		int strideU = outputSize.width/2
		int strideV = outputSize.width/2
		pixOutYUV = new PixelMapYUV(new WH(outputSize.width, outputSize.height), new byte[strideY * outputSize.height], new byte[strideU * outputSize.height], new byte[strideV * outputSize.height], strideY, strideU, strideV)
		}

	PixelMapYUV ImageTransform:rgbaToYUV(PixelMap p, opt byte format)
		{
		return lib.rgbaToYUV(pixOutYUV, p, format)
		}

	PixelMap ImageTransform:yuvToRGBA(PixelMapYUV p, opt ChromaKeySpec spec)
		{
		if (spec != null)
			return lib.yuvToRGBA(pixOut, p, true, spec.r, spec.g, spec.b, spec.fgTolerance, spec.bgTolerance)
			else
			return lib.yuvToRGBA(pixOut, p, false, 0, 0, 0, 0, 0)
		}
	
	PixelMap ImageTransform:crop(PixelMap p, int x, int y)
		{
		int width = pixOut.size.width
		int height = pixOut.size.height

		if (x + width > p.size.width)
			throw new Exception("crop range out of bounds")
		
		if (y + height > p.size.height)
			throw new Exception("crop range is out of bounds")
		
		int resultIndex = 0
		
		for (int rowIndex = y; rowIndex < (height+y); rowIndex++)
			{
			int byteIndex = (rowIndex * p.size.width * 4) + (x * 4)

			for (int colIndex = x; colIndex < (width+x); colIndex++)
				{
				pixOut.pixels[resultIndex] = p.pixels[byteIndex]
				pixOut.pixels[resultIndex+1] = p.pixels[byteIndex+1]
				pixOut.pixels[resultIndex+2] = p.pixels[byteIndex+2]
				pixOut.pixels[resultIndex+3] = p.pixels[byteIndex+3]

				byteIndex += 4
				resultIndex += 4
				}
			}

		return pixOut
		}
	
	PixelMap ImageTransform:chromaKey(PixelMap p, ChromaKeySpec spec, opt byte pixelFormat)
		{
		lib.chromaKey(pixOut, p, spec.r, spec.g, spec.b, spec.fgTolerance, spec.bgTolerance, pixelFormat)
		return pixOut
		}
	
	PixelMap ImageTransform:runAlphaMask(PixelMap p, byte mask[], opt byte pixelFormat)
		{
		lib.runMask(pixOut, p, mask, pixelFormat)
		return pixOut
		}
	
}
Revision history
To propose a new revision to this entity, use dana source put -uc your/new/version.dn -n media.image.ImageTransform -m "reason for update" -u yourUsername
Version 4 (this version) by barry
Notes for this version: Improves performance by pre-allocating a storage area for return values, adds a run-length alpha mask function
Version 3 by barry
Version 2 by barry
Version 1 by barry