2009-04-16 117 views
2

我有一個CGImage(核心圖形,C/C++)。這是灰度。那麼,最初是B/W,但CGImage可能是RGB。這應該不重要。我想創建一個CCITT-Group 4 TIFF。從CGImage生成CCITT壓縮的TIFF

我可以創建一個LZW TIFF(灰度或顏色),通過創建一個帶有正確字典的目的地並添加圖像。沒問題。

但是,似乎沒有用於表示CCITT-4的等效kCGImagePropertyTIFFCompression值。它應該是4,但是這會產生未壓縮的。

我有一個手動CCITT壓縮程序,所以如果我可以得到二進制(每像素1位)數據,我設置。但我似乎無法從CGImage中獲取1個BPP數據。我有代碼應該把CGImage放到CGBitmapContext中,然後給我數據,但它似乎給我全黑。

我已經問了一些問題,今天試圖得到這個,但我只是想,讓我們問我真的想要回答的問題,看看有人能回答它。

有GOT可以做到這一點。我必須失去一些愚蠢的東西。它是什麼?

回答

1

ImageMagick可以轉換和幾乎任何圖像格式。因爲它是開源的,你可以去閱讀source code來找到你的問題的答案。

如果您使用C++,您甚至可以在您的應用程序中使用ImageMagick API

編輯:
如果你可以從CGImage數據以任何格式(和它聽起來像您可以),你可以使用ImageMagick的它無論從任何轉換格式是你從CGImage到任何其他格式由ImageMagick支持(您所需的TIFF格式)。

編輯: Technical Q&A QA1509 Getting the pixel data from a CGImage object狀態:

在Mac OS X 10.5或更高版本,一個新的呼叫已被添加,允許你從CGImage對象獲得實際的像素數據。這個調用CGDataProviderCopyData返回一個CFData對象,該對象包含有問題的圖像的像素數據。

一旦你有像素數據,你可以使用ImageMagick來轉換它。

+0

這沒有幫助,因爲它根本沒有顯示圖像。所以它不處理CGImage對象。假設我有1Bit數據,我已經有讀取組4文件並編寫它們的代碼。我需要以這種方式從CGImage中獲取數據,以便我可以編寫TIFF ... – 2009-04-17 13:20:41

+0

如果您可以以任何格式從CGImage獲取數據(並且聽起來可以),則可以使用ImageMagick將其從CGImage獲得的格式轉換爲ImageMagick支持的任何其他格式(您所需的TIFF格式) 。 – lothar 2009-04-17 15:34:01

+0

好吧,我真的不能從CGImage中獲取二進制數據...我可以用其他格式保存文檔,但我寧願不保存,然後轉換... – 2009-04-20 13:55:30

1

NSBitmapImageRep宣稱能夠生成CCITT FAX Group 4壓縮的TIFF。所以像這樣的東西可能會做伎倆(未經測試):

CFDataRef tiffFaxG4DataForCGImage(CGImageRef cgImage) { 
    NSBitmapImageRep *imageRep = 
     [[[NSBitmapImageRep alloc] initWithCGImage:cgImage] autorelease]; 
    NSData *tiffData = 
     [imageRep TIFFRepresentationUsingCompression:NSTIFFCompressionCCITTFAX4 
              factor:0.0f]; 
    return (CFDataRef) tiffData; 
} 

此函數應該返回您尋找的數據。

2

這似乎工作,併產生不全黑輸出。可能有辦法做到這一點,不需要手動將灰度轉換爲灰度,但至少可以工作!

static void WriteCCITTTiffWithCGImage_URL_(CGImageRef im, CFURLRef url) { 
    // produce grayscale image 
    CGImageRef grayscaleImage; 
    { 
     CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray); 
     CGContextRef bitmapCtx = CGBitmapContextCreate(NULL, CGImageGetWidth(im), CGImageGetHeight(im), 8, 0, colorSpace, kCGImageAlphaNone); 
     CGContextDrawImage(bitmapCtx, CGRectMake(0,0,CGImageGetWidth(im), CGImageGetHeight(im)), im); 
     grayscaleImage = CGBitmapContextCreateImage(bitmapCtx); 
     CFRelease(bitmapCtx); 
     CFRelease(colorSpace); 
    } 

    // generate options for ImageIO. Man this sucks in C. 
    CFMutableDictionaryRef options = CFDictionaryCreateMutable(kCFAllocatorDefault, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 
    { 
     { 
      CFMutableDictionaryRef tiffOptions = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 
      int fourInt = 4; 
      CFNumberRef fourNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &fourInt); 
      CFDictionarySetValue(tiffOptions, kCGImagePropertyTIFFCompression, fourNumber); 
      CFRelease(fourNumber); 

      CFDictionarySetValue(options, kCGImagePropertyTIFFDictionary, tiffOptions); 

      CFRelease(tiffOptions);     
     } 

     { 
      int oneInt = 1; 
      CFNumberRef oneNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &oneInt); 

      CFDictionarySetValue(options, kCGImagePropertyDepth, oneNumber); 

      CFRelease(oneNumber); 
     } 
    } 

    // write file 
    CGImageDestinationRef idst = CGImageDestinationCreateWithURL(url, kUTTypeTIFF, 1, NULL); 
    CGImageDestinationAddImage(idst, grayscaleImage, options); 
    CGImageDestinationFinalize(idst); 

    // clean up 
    CFRelease(idst); 
    CFRelease(options); 
    CFRelease(grayscaleImage); 
} 


Nepheli:tmp ken$ tiffutil -info /tmp/output.tiff 
Directory at 0x1200 
    Image Width: 842 Image Length: 562 
    Bits/Sample: 1 
    Sample Format: unsigned integer 
    Compression Scheme: CCITT Group 4 facsimile encoding 
    Photometric Interpretation: "min-is-black" 
    Orientation: row 0 top, col 0 lhs 
    Samples/Pixel: 1 
    Number of Strips: 1 
    Planar Configuration: Not planar