Objective c 将图像转换为宝丽来滤光片

Objective c 将图像转换为宝丽来滤光片,objective-c,Objective C,我正试图通过以下方式将图像转换为宝丽来滤光片: int m_width = self.size.width; int m_height = self.size.height; uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t)); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); C

我正试图通过以下方式将图像转换为宝丽来滤光片:

   int m_width = self.size.width;
    int m_height = self.size.height;

    uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(
                                                 rgbImage,
                                                 m_width,
                                                 m_height,
                                                 8, 
                                                 m_width * 4, 
                                                 colorSpace, 
                                                 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast
                                                 );
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextSetShouldAntialias(context, NO);
    CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [self CGImage]);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    // now convert to grayscale
    uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
    for(int y = 0; y < m_height; y++) 
    {
        for(int x = 0; x < m_width; x++)
        {
            uint32_t rgbPixel=rgbImage[y*m_width+x];
            uint32_t redIn = 0, greenIn = 0,blueIn = 0,max = 0,min = 0,chroma = 0,hue = 0,saturation = 0,redOut = 0,greenOut = 0,blueOut = 0;

            redIn = (rgbPixel>>24)&255;
            greenIn = (rgbPixel>>16)&255;
            blueIn = (rgbPixel>>8)&255;

            //NSLog(@"redIn %u greenIn %u blueIn %u",redIn,greenIn,blueIn);

            max = redIn > greenIn ? redIn > blueIn ? redIn : blueIn : greenIn > blueIn ? greenIn : blueIn;
            min = redIn < greenIn ? redIn < blueIn ? redIn : blueIn : greenIn < blueIn ? greenIn : blueIn;
            chroma = max - min;

            if(chroma != 0)
            {
                if(max == redIn)
                    hue = ((greenIn - blueIn) / chroma) %6 ;
                else if(max == greenIn)
                    hue = ((blueIn - redIn)/chroma) + 2;
                else if(max == blueIn)
                    hue = ((redIn - greenIn)/chroma) + 4;

                hue = (hue+20)%6;

                if(chroma != 0)
                    saturation = chroma / max;

                if(hue >= 0 && hue < 1)
                {
                    redOut = chroma + max - chroma;
                    greenOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
                    blueOut = 0 + max - chroma;
                }
                else if(hue >= 1 && hue < 2)
                {
                    redOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
                    greenOut = chroma + max - chroma;
                    blueOut = 0 + max - chroma;
                }
                else if(hue >= 2 && hue < 3)
                {
                    redOut = 0 + max - chroma;
                    greenOut = chroma + max - chroma;
                    blueOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
                }
                else if(hue >= 3 && hue < 4)
                {
                    redOut = 0 + max - chroma;
                    greenOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
                    blueOut = chroma + max - chroma;
                }
                else if(hue >= 4 && hue < 5)
                {
                    redOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
                    greenOut = 0 + max - chroma;
                    blueOut = chroma + max - chroma;

                }
                else if(hue >= 5 && hue < 6)
                {
                    redOut = chroma + max - chroma;
                    greenOut = 0 + max - chroma;
                    blueOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
                }
            }
            //NSLog(@"redOut %u greenOut %u blueOut %u",redOut,greenOut,blueOut);
            m_imageData[y*m_width+x]=redOut + greenOut + blueOut;
        }
    }
    free(rgbImage);

    // convert from a gray scale image back into a UIImage
    uint8_t *result = (uint8_t *) calloc(m_width * m_height *sizeof(uint32_t), 1);

    // process the image back to rgb
    for(int i = 0; i < m_height * m_width; i++) 
    {
        result[i*4]=0;
        int val=m_imageData[i];
        result[i*4+1]=val;
        result[i*4+2]=val;
        result[i*4+3]=val;
    }
    free(m_imageData);

    // create a UIImage
    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(result, 
                                    m_width, 
                                    m_height, 
                                    8, 
                                    m_width * sizeof(uint32_t), 
                                    colorSpace,
                                    kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast
                                    );
    CGImageRef image = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
#ifdef kNYXReturnRetainedObjects 
    UIImage* resultUIImage = [[UIImage alloc] initWithCGImage:image];
#else
    UIImage *resultUIImage = [UIImage imageWithCGImage:image];
#endif
    CGImageRelease(image);

    free(result);

    return resultUIImage;
int m_width=self.size.width;
int m_height=self.size.height;
uint32_t*rgbImage=(uint32_t*)malloc(m_宽度*m_高度*尺寸(uint32_t));
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
CGContextRef context=CGBitmapContextCreate(
RGB图像,
m_宽度,
m_高度,
8.
m_宽度*4,
色彩空间,
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast
);
CGContextSetInterpolationQuality(上下文,kCGInterpolationHigh);
CGContextSetShouldAntialias(上下文,否);
CGContextDrawImage(上下文,CGRectMake(0,0,m_宽度,m_高度),[self CGImage]);
CGContextRelease(上下文);
CGCOLORSPACTERELEASE(色彩空间);
//现在转换为灰度
uint8_t*m_图像数据=(uint8_t*)malloc(m_宽度*m_高度);
对于(int y=0;y>24)和255;
格林宁=(rgbPixel>>16)和255;
蓝蛋白=(rgbPixel>>8)和255;
//NSLog(@“redIn%u greenIn%u blueIn%u”,redIn,greenIn,blueIn);
max=redIn>greenIn?redIn>blueIn?redIn:blueIn:greenIn>blueIn?greenIn:blueIn;
最小值=redIn=0&&色调<1)
{
redOut=色度+最大-色度;
绿度=色度*(1-abs((色调%2)-1))+最大色度;
蓝显=0+最大-色度;
}
否则如果(色调>=1&&色调<2)
{
redOut=色度*(1-abs((色调%2)-1))+最大色度;
绿显=色度+最大-色度;
蓝显=0+最大-色度;
}
否则如果(色调>=2&&色调<3)
{
重做=0+最大-色度;
绿显=色度+最大-色度;
蓝显=色度*(1-abs((色调%2)-1))+最大色度;
}
否则如果(色调>=3&&色调<4)
{
重做=0+最大-色度;
绿度=色度*(1-abs((色调%2)-1))+最大色度;
蓝显=色度+最大-色度;
}
否则如果(色调>=4&&色调<5)
{
redOut=色度*(1-abs((色调%2)-1))+最大色度;
绿度=0+最大-色度;
蓝显=色度+最大-色度;
}
否则如果(色调>=5&&色调<6)
{
redOut=色度+最大-色度;
绿度=0+最大-色度;
蓝显=色度*(1-abs((色调%2)-1))+最大色度;
}
}
//NSLog(@“重做%u绿色输出%u蓝色输出%u”,重做,绿色输出,蓝色输出);
m_imageData[y*m_width+x]=重做+绿色化+蓝色化;
}
}
免费(rgbImage);
//将灰度图像转换回UIImage
uint8_t*结果=(uint8_t*)calloc(m_宽度*m_高度*尺寸(uint32_t),1);
//将图像处理回rgb
对于(int i=0;i
但我在执行这段代码后不会得到宝丽来的图像。
这段代码有什么问题,或者你能给我一些其他的建议吗。

这段代码最明显的问题是,在方法结束时,你创建了一个绘图上下文,在其中什么都不做,然后从中获取一个图像并将其作为
UIImage
返回。这大概是空白的。我认为您正在尝试获取
结果中数据中保存的图像-我认为您需要查看
CGImageCr