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

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

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

	PixelMap ImageTransform:yuvToRGBA(PixelMapYUV p, opt ChromaKeySpec spec)
		{
		if (spec != null)
			return lib.yuvToRGBA(p, true, spec.r, spec.g, spec.b, spec.fgTolerance, spec.bgTolerance)
			else
			return lib.yuvToRGBA(p, false, 0, 0, 0, 0, 0)
		}
	
	PixelMap ImageTransform:crop(PixelMap p, int x, int y, int width, int 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")
		
		PixelMap result = new PixelMap(new WH(width, height))

		result.pixels = new byte[width * height * 4]

		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++)
				{
				result.pixels[resultIndex] = p.pixels[byteIndex]
				result.pixels[resultIndex+1] = p.pixels[byteIndex+1]
				result.pixels[resultIndex+2] = p.pixels[byteIndex+2]
				result.pixels[resultIndex+3] = p.pixels[byteIndex+3]

				byteIndex += 4
				resultIndex += 4
				}
			}

		return result
		}
	
	PixelMap ImageTransform:chromaKey(PixelMap p, ChromaKeySpec spec, opt byte pixelFormat)
		{
		return lib.chromaKey(p, spec.r, spec.g, spec.b, spec.fgTolerance, spec.bgTolerance, pixelFormat)
		}
	
}
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 by barry
Version 3 (this version) by barry
Notes for this version: Adds optional pixel format input selection for rbgaToYUV() and chromaKey()
Version 2 by barry
Version 1 by barry