0
votes

I have to create grayscale image and then save it to png and then convert to tiff for sending to server. The problem is when I set brightness to values >0.0 then tiff image is all white, but when brightness is == 0.0 or < 0.0 then everything is ok.

my code for setting brightness:

CIImage* ciImage = image.CIImage;
    if (ciImage == nil){
        ciImage = [[CIImage alloc] initWithCGImage: image.CGImage];
    }
    
    CIImage* filteredImage = [ciImage imageByApplyingFilter:@"CIColorControls" withInputParameters:@{kCIInputBrightnessKey: [NSNumber numberWithFloat: brightness]}];
    return [[UIImage alloc] initWithCIImage:filteredImage];

Convertion to tif:

CGImageRef srcCGImage = [self CGImage];
    if(srcCGImage == nil){
        CIImage* ciImage = [self CIImage];
        CIContext* context1 = [[CIContext alloc] initWithOptions:nil];
        srcCGImage = [context1 createCGImage:ciImage fromRect:ciImage.extent];
    }
    CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(srcCGImage));
    unsigned char *pixelDataPtr = (unsigned char *)CFDataGetBytePtr(pixelData);
    
   
    
    NSString *file = [directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", originalFile]];
    TIFF *tiff;
    if ((tiff = TIFFOpen([file UTF8String], "w")) == NULL) {
        if (pixelData) CFRelease(pixelData);
        NSArray *actionTitles = @[@"OK"];
        return nil;
    }
    
    unsigned int width = self.size.width;
    unsigned int height = self.size.height;
    
    TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, width);
    TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, height);
    
    tiff = [self setupTiffTagsForFile:tiff withMetadata:metadata];
    tiff = [self setupExifTagsForFile:tiff withMetadata:metadata];
    
    uint32 *ptr = (uint32 *)pixelDataPtr;
    unsigned char eightPixels;
    tmsize_t bytesPerStrip = ceil(width/8.0);
    unsigned char *strip = (unsigned char *)_TIFFmalloc(bytesPerStrip);
    
    for (int y=0; y<height; y++) {
        for (int x=0; x<width; x++) {
            unsigned char pixel = (unsigned char)((*ptr++) & 0xFF); // lower byte, image is RGBP
            eightPixels = strip[x/8];
            eightPixels = eightPixels << 1;
            if (!pixel) eightPixels = eightPixels | 1; // black=1 in tiff image without TIFFTAG_PHOTOMETRIC header
            strip[x/8] = eightPixels;
        }
        TIFFWriteEncodedStrip(tiff, y, strip, bytesPerStrip);
    }