将Leptonica Pix对象转换为QPixmap(或其他图像对象)

Tob*_*oby 0 c++ qt image qpixmap

我正在使用Leptonica Library处理一些图片.之后我想在我的QT GUI中显示它们.Leptonica使用自己的格式Pix作为图像,而QT使用他们自己的格式QPixmap.目前我唯一的办法是将处理后的图片保存为文件(如bmp),然后再通过QT函数调用再次加载.现在我想在我的代码中转换它们,所以我不需要绕道而行将它们保存在文件系统中.任何想法如何做到这一点?

最好的祝福

//编辑:

好的,正如我已经建议的那样,我试图将PIX*转换为QImage.PIX*的定义如下:

http://tpgit.github.com/Leptonica/pix_8h_source.html

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

而QImage为我提供了这样的方法:

http://developer.qt.nokia.com/doc/qt-4.8/qimage.html#QImage-7

QImage ( const uchar * data, 
    int width, 
    int height, 
    int bytesPerLine, 
    Format format )
Run Code Online (Sandbox Code Playgroud)

我假设我不能在调用构造函数时将数据从PIX复制到QImage.我想我需要填充Pixel的QImage Pixel,但实际上我不知道怎么做?我是否需要遍历所有坐标?我如何看待钻头深度?这里有什么想法?

use*_*678 6

我使用它来转换到PIX的QImage:

PIX* TessTools::qImage2PIX(QImage& qImage) {
  PIX * pixs;
  l_uint32 *lines;

  qImage = qImage.rgbSwapped();
  int width = qImage.width();
  int height = qImage.height();
  int depth = qImage.depth();
  int wpl = qImage.bytesPerLine() / 4;

  pixs = pixCreate(width, height, depth);
  pixSetWpl(pixs, wpl);
  pixSetColormap(pixs, NULL);
  l_uint32 *datas = pixs->data;

  for (int y = 0; y < height; y++) {
    lines = datas + y * wpl;
    QByteArray a((const char*)qImage.scanLine(y), qImage.bytesPerLine());
    for (int j = 0; j < a.size(); j++) {
      *((l_uint8 *)lines + j) = a[j];
    }
  }
  return pixEndianByteSwapNew(pixs);
}
Run Code Online (Sandbox Code Playgroud)

这用于将PIX转换为QImage:

QImage TessTools::PIX2QImage(PIX *pixImage) {
  int width = pixGetWidth(pixImage);
  int height = pixGetHeight(pixImage);
  int depth = pixGetDepth(pixImage);
  int bytesPerLine = pixGetWpl(pixImage) * 4;
  l_uint32 * s_data = pixGetData(pixEndianByteSwapNew(pixImage));

  QImage::Format format;
  if (depth == 1)
    format = QImage::Format_Mono;
  else if (depth == 8)
    format = QImage::Format_Indexed8;
  else
    format = QImage::Format_RGB32;

  QImage result((uchar*)s_data, width, height, bytesPerLine, format);

  // Handle pallete
  QVector<QRgb> _bwCT;
  _bwCT.append(qRgb(255,255,255));
  _bwCT.append(qRgb(0,0,0));

  QVector<QRgb> _grayscaleCT(256);
  for (int i = 0; i < 256; i++)  {
    _grayscaleCT.append(qRgb(i, i, i));
  }
  if (depth == 1) {
    result.setColorTable(_bwCT);
  }  else if (depth == 8)  {
    result.setColorTable(_grayscaleCT);

  } else {
    result.setColorTable(_grayscaleCT);
  }

  if (result.isNull()) {
    static QImage none(0,0,QImage::Format_Invalid);
    qDebug() << "***Invalid format!!!";
    return none;
  }

  return result.rgbSwapped();
}
Run Code Online (Sandbox Code Playgroud)