interface ImageTransformLib {
PixelMapYUV rgbaToYUV(PixelMap p)
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)
}
component provides ImageTransform requires io.Output out, data.IntUtil iu, native ImageTransformLib lib {
PixelMapYUV ImageTransform:rgbaToYUV(PixelMap p)
{
return lib.rgbaToYUV(p)
}
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)
{
return lib.chromaKey(p, spec.r, spec.g, spec.b, spec.fgTolerance, spec.bgTolerance)
}
}