Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在NSImage和Leptonica Pix之间转换_Objective C_Cocoa_Nsimage_Leptonica - Fatal编程技术网

Objective c 在NSImage和Leptonica Pix之间转换

Objective c 在NSImage和Leptonica Pix之间转换,objective-c,cocoa,nsimage,leptonica,Objective C,Cocoa,Nsimage,Leptonica,我正在开发一个Cocoa OS X程序来清理扫描的页面,我想用它来完成繁重的工作。我在和中找到了一些信息。我当然可以从NSImage获得CGImage,并且可以将数据写入Leptonica Pix图像。我的问题是,75%的时候,我的图像会扭曲成理发店的杆状图案(从图像顶部到底部的每一行连续像素都会越来越向右移动)。有时候,尽管这幅画的效果很好。我假设我在设置图像数据时做了一些错误的事情,但这并不是我的专长,所以我很难理解这个问题。我正在使用以下代码创建Pix图像: CGImageRef myCG

我正在开发一个Cocoa OS X程序来清理扫描的页面,我想用它来完成繁重的工作。我在和中找到了一些信息。我当然可以从NSImage获得CGImage,并且可以将数据写入Leptonica Pix图像。我的问题是,75%的时候,我的图像会扭曲成理发店的杆状图案(从图像顶部到底部的每一行连续像素都会越来越向右移动)。有时候,尽管这幅画的效果很好。我假设我在设置图像数据时做了一些错误的事情,但这并不是我的专长,所以我很难理解这个问题。我正在使用以下代码创建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                    */
};
“理发店极型模式”是每行像素数据字节数错误的典型标志

您应该根据返回的值
wpl
。很可能:

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

基于
CGImageGetWidth()
,图像的每行字节数与您的猜测不同的原因有很多。例如,它可能出于性能原因而被四舍五入,或者该图像可能是较宽图像的子图像。

它也可能不是每分量8位RGBA。它可以使用浮点组件、16位整数组件,或者在不同的颜色空间中,或者它们的一些组合。某些组合甚至可以为您提供非RGBA的四字节像素(例如,8位CMYK和浮点灰度),因此您不会得到理发杆图案,但您的输出看起来会有几种奇怪的效果。