2012-12-17 42 views
0

我正在致力於一個Cocoa OS X程序來清理掃描的頁面,並且想使用Leptonica's library來完成繁重的工作。我在this postthis onethis one中發現了一些信息。我當然可以從NSImage獲取CGImage,並可以將數據寫入Leptonica Pix圖像。我遇到的問題是,75%的圖像出現扭曲的理髮店杆型圖案(從圖像的頂部到底部的每個連續像素行向右和向右移動)。有時雖然圖片出來很好。我假設我在設置圖像數據時做了一些錯誤,但這並不是我的特長,所以我無法理解這個問題。在NSImage和Leptonica之間轉換Pix

CGImageRef myCGImage = [processedImage CGImageForProposedRect:NULL context:NULL hints:NULL]; 
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(myCGImage)); 
const UInt8 *imageData = CFDataGetBytePtr(data); 

Pix *myPix = (Pix *) malloc(sizeof(Pix)); 
myPix->w = (int)CGImageGetWidth (myCGImage); 
myPix->h = (int)CGImageGetHeight (myCGImage); 
myPix->d = (int)CGImageGetBitsPerPixel(myCGImage); 
myPix->wpl = ((CGImageGetWidth (myCGImage)*CGImageGetBitsPerPixel(myCGImage))+31)/32; 
myPix->informat = IFF_TIFF; 
myPix->data = (l_uint32 *) imageData; 
myPix->colormap = NULL; 

在PIX結構定義如下::

/*-------------------------------------------------------------------------* 
*        Basic Pix         * 
*-------------------------------------------------------------------------*/ 
struct Pix 
{ 
uint32    w;   /* width in pixels     */ 
uint32    h;   /* height in pixels     */ 
uint32    d;   /* depth in bits      */ 
uint32    wpl;   /* 32-bit words/line     */ 
uint32    refcount; /* reference count (1 if no clones) */ 
int    xres;  /* image res (ppi) in x direction */ 
            /* (use 0 if unknown)    */ 
int    yres;  /* image res (ppi) in y direction */ 
            /* (use 0 if unknown)    */ 
int    informat; /* input file format, IFF_*   */ 
char    *text;  /* text string associated with pix */ 
struct PixColormap *colormap; /* colormap (may be null)   */ 
uint32   *data;  /* the image data     */ 
}; 

回答

0

的「理髮店極型圖案」是具有錯誤的數目的經典標誌我使用以下代碼創建PIX圖像每行像素數據的字節數。

您應該基於CGImageGetBytesPerRow返回的值wpl。最有可能的:

myPix->wpl = CGImageGetBytesPerRow(myCGImage)/4; 

有幾個原因,圖像的字節數,每行會基於CGImageGetWidth()你的猜測有所不同。例如,它可能出於性能原因而被四捨五入,或者圖像可能是更廣泛圖像的子圖像。

+0

它也可能不是8位每分量RGBA。它可以使用浮點組件,16位整數組件,或者位於不同的顏色空間或其組合中。有些組合甚至可以給你不是RGBA的四字節像素(例如,8位CMYK和浮點灰度),所以你不會得到理髮師的極點模式,但是你的輸出看起來會有幾種奇怪的。 –