2012-12-17 46 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和浮点灰度),所以你不会得到理发师的极点模式,但是你的输出看起来会有几种奇怪的。 –