我正在尝试使用 libtiff 将 UIImage 写为 tiff。问题是,即使我将其写入为每像素 1 位,但当我期望大约 100k 或更少时,文件仍会出现在 2-5MB 范围内。
这就是我所拥有的。
- (void) convertUIImage:(UIImage *)uiImage toTiff:(NSString *)file withThreshold:(float)threshold {
TIFF *tiff;
if ((tiff = TIFFOpen([file UTF8String], "w")) == NULL) {
[[[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Unable to write to file %@.", file] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];
return;
}
CGImageRef image = [uiImage CGImage];
CGDataProviderRef provider = CGImageGetDataProvider(image);
CFDataRef pixelData = CGDataProviderCopyData(provider);
unsigned char *buffer = (unsigned char *)CFDataGetBytePtr(pixelData);
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(image);
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(image);
size_t compBits = CGImageGetBitsPerComponent(image);
size_t pixelBits = CGImageGetBitsPerPixel(image);
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
NSLog(@"bitmapInfo=%d, alphaInfo=%d, pixelBits=%lu, compBits=%lu, width=%lu, height=%lu", bitmapInfo, alphaInfo, pixelBits, compBits, width, height);
TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 1);
TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tiff, TIFFTAG_ROWSPERSTRIP, 1);
TIFFSetField(tiff, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tiff, TIFFTAG_XRESOLUTION, 200.0);
TIFFSetField(tiff, TIFFTAG_YRESOLUTION, 200.0);
TIFFSetField(tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
unsigned char red, green, blue, gray, bite;
unsigned char *line = (unsigned char *)_TIFFmalloc(width/8);
unsigned long pos;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pos = y * width * 4 + x * 4; // multiplying by four because each pixel is represented by four bytes
red = buffer[ pos ];
green = buffer[ pos + 1 ];
blue = buffer[ pos + 2 ];
gray = .3 * red + .59 * green + .11 * blue; // http://answers.yahoo.com/question/index?qid=20100608031814AAeBHPU
bite = line[x / 8];
bite = bite << 1;
if (gray > threshold) bite = bite | 1;
// NSLog(@"y=%d, x=%d, byte=%d, red=%d, green=%d, blue=%d, gray=%d, before=%@, after=%@", y, x, x/8, red, green, blue, gray, [self bitStringForChar:line[x / 8]], [self bitStringForChar:bite]);
line[x / 8] = bite;
}
TIFFWriteEncodedStrip(tiff, y, line, width);
}
// Close the file and free buffer
TIFFClose(tiff);
if (line) _TIFFfree(line);
if (pixelData) CFRelease(pixelData);
}
第一 NSLog 行说:
bitmapInfo=5, alphaInfo=5, pixelBits=32, compBits=8, width=3264, height=2448
我也有这个项目的一个版本,它使用 GPUImage 代替。有了它,我可以将相同的图像缩小到大约 130k 作为 8 位 PNG。如果我将该 PNG 发送到 PNG 优化器站点,他们可以将其降低到大约 25k。如果有人可以告诉我如何编写从我的 GPUImage 过滤器生成的 1 位 PNG,我将放弃 tiff。
谢谢!