How to apply LUT (color table) to CGImage or UIImage in iOS?

Given CGImage or UIImage, how can I apply a custom color lookup table (aka LUT, CLUT, Color Map)? That is, how can I match the colors in the image with the new colors, given the match?

+5
source share
1 answer

I will describe three approaches that you can take.

  • Do it manually
  • Use CIFilter (available in iOS 5)
  • Use a shader (GPU program)

Manual approach

First get the raw image data from UIImage. You can do this by creating an array of bytes of the appropriate size (width * height * components) and then entering it into the CGBitmapContext. Something like that:

    using (var colorSpace = CGColorSpace.CreateDeviceRGB())
    using (var context = new CGBitmapContext(
        bytes, width, height, bitsPerComponent, bytesPerRow, 
        colorSpace, CGBitmapFlags.ByteOrder32Big | CGBitmapFlags.PremultipliedLast))
    {
        var drawRect = new RectangleF(-rectangle.X, -rectangle.Y, image.CGImage.Width, image.CGImage.Height);
        context.ClipToRect(new RectangleF(0, 0, width, height));
        context.DrawImage(drawRect, image.CGImage);
    }

(, ). , Look-Up .

, CGDataProvider , a CGImage , a UIImage CGImage.

CIFilter

iOS 5, Apple . , , . , , , .

CIFilter, inputImage, OutputImage. . documentation CICategoryColorAdjustment CICategoryColorEffect. CIToneCurve, CIFalseColor, CIColorMap CIColorCube. , CIColorMap iOS.

, , CIFalseColor.

CIColorCube . , CIFilters ( ) . , , (CIColorCube, CIFilter.FromName("CIColorCube")). , .

static void PopulateColorCubeFilter(CIFilter filter)
{
    if (filter.Name != "CIColorCube")
        return;

    int dimension = 64;  // Must be power of 2, max of 128 (max of 64 on ios)
    int cubeDataSize = 4 * dimension * dimension * dimension;
    filter[new NSString("inputCubeDimension")] = new NSNumber(dimension);

    // 2 : 32 /4 = 8 = 2^3
    // 4 : 256 /4 = 64 = 4^3
    // 8 : 2048 /4 = 512 = 8^3

    var cubeData = new byte[cubeDataSize];
    var rnd = new Random();
    rnd.NextBytes(cubeData);
    for (int i = 3; i < cubeDataSize; i += 4)
        cubeData[i] = 255;
    filter[new NSString("inputCubeData")] = NSData.FromArray(cubeData);
}

, , , - . , , , .

  • (aka Sampler2D)
  • ( 1D , OpenGL ES, , 2D-).
  • , . .
+11

All Articles