从XBM创建QImage

时间:2015-08-21 10:11:57

标签: c++ qt qimage qt4.8

XBM格式存储的原始数据创建QBitmap,可以轻松完成QBitmap::fromData(width, height, data, QImage::Format_MonoLSB)data是XBM文件的原始数据。

现在,如果我们想要在这次创建QImage怎么办?的确,我们想要从相同的XBM原始数据创建QImage。 但是还有一个额外的限制:此处不应使用QBitmap,这意味着将QBitmap转换为QImage将不是一个可接受的解决方案。

我试过QImage(data, width, height, QImage::Format_MonoLSB) 但它没有产生正确的图像,因为QImage期望位是32位对齐,参见Qt的4.8文档:

  

QImage :: QImage(uchar * data,int width,int height,Format format)
  构造具有给定宽度,高度和格式的图像   现有的内存缓冲区,数据。宽度和高度必须是   以像素为单位指定,数据必须是32位对齐,并且每个扫描线都是   图像中的数据也必须是32位对齐

问题是XBM是1位对齐的(不是32位对齐),因此QImage生成的QImage(data, width, height, QImage::Format_MonoLSB)是垃圾(未定义的行为,"随机"图像)。

那么,如何从XBM的原始QImage创建data

修改 这是一个用于构建QImage的XBM文件:

#define x_width 66
#define x_height 27
static char x_bits[] = {
   0xff, 0xff, 0xff, 0xff, 0xff,
   0xff, 0xff, 0xff, 0xff, ...
};

2 个答案:

答案 0 :(得分:1)

这是一个实现。它是线程安全的,你可以在多个线程上分发这些图像的构造,如果有很多线程的话。它当时处理32位输出。

必须将可用字节数传递给fromLittleEndian,以避免读取输入数据的末尾。

screenshot

#include <QtWidgets>
#include <cstdint>

inline bool isAlignedTo(const void * p, int bits) {
  return (reinterpret_cast<uintptr_t>(p) & ((bits/8)-1)) == 0;
}

inline uint32_t fromLittleEndian(const void *p, int bytes) {
  Q_ASSERT(bytes > 0);
  uint32_t val;
  if (bytes >= 4) {
    val = *reinterpret_cast<const uint32_t*>(p);
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
    val = val >> 24 | (val >> 8 & 0xFF00U) | (val << 8 & 0xFF0000U) | val << 24;
#endif
  } else {
    auto pp = reinterpret_cast<const uint8_t*>(p);
    val = *pp++;
    if (bytes > 1) val |= static_cast<uint32_t>(*pp++) << 8;
    if (bytes > 2) val |= static_cast<uint32_t>(*pp) << 16;
  }
  return val;
}

QImage fromXBM(const uchar * data, int width, int height,
               QImageCleanupFunction cleanup = 0, void * cleanupData = 0) {
  Q_ASSERT(width && height);
  if ((width & 31) == 0 && isAlignedTo(data, 32)) // 32-bit aligned
    return QImage(data, width, height, QImage::Format_MonoLSB, cleanup, cleanupData);
  if ((width & 7) == 0) // 8-bit aligned
    return QImage(data, width, height, width/8, QImage::Format_MonoLSB, cleanup, cleanupData);

  // Note: Source pixels are stored LSB first, MSB last.
  QImage img(width, height, QImage::Format_MonoLSB);
  Q_ASSERT((img.bytesPerLine() & 0x3) == 0); // must be 32-bit aligned
  uint32_t * dst = reinterpret_cast<uint32_t*>(img.bits());
  const int wordsPerRow = width / 32 + ((width & 31) ? 1 : 0);
  int shift = 0;
  int bytesLeft = (width * height)/8 + ((width * height & 7) ? 1 : 0);
  uint32_t src = fromLittleEndian(data, bytesLeft);
  bytesLeft -= 4;
  data += 4;
  while (height--) {
    QList<uint32_t> w;
    int widthLeft = width;
    for (int c = 0; c < wordsPerRow; ++c) {
      Q_ASSERT(widthLeft > 0);
      uint32_t word = src >> shift;
      if (32-shift < widthLeft) {
        src = fromLittleEndian(data, bytesLeft);
        bytesLeft -= 4;
        data += 4;
        if (shift) word |= src << (32-shift);
      }
      widthLeft -= 32;
      *dst++ = word;
    }
    shift = (shift + width) & 31;
  }
  // Dispose of the data since we've made a copy
  if (cleanup) cleanup(cleanupData);
  return img;
}

// w = 5 h = 5
// 1...1   0x11,3
// .1.1.   0x40 0x01,6
// ..1..        0x10,1
// .1.1.        0x00 0x05,4
// 1...1             0x10  0x01,7
//         0x51 0x11 0x15  0x01

const int X_w = 5, X_h = 5;
const uchar X_bits[] = { 0x51, 0x11, 0x15, 0x01 };

// 17 iterations of rule 30 cellular automaton, see
// Stephen Wolfram, The New Kind Of Science
// from https://lost-contact.mit.edu/afs/hep.wisc.edu/apps/Mathematica-7.0/Documentation/English/System/ExampleData/
const int rule30_w = 40, rule30_h = 17;
const uchar rule30_bits[] = {
 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x80, 0x03, 0x00, 0x00, 0x00, 0xc0,
 0x04, 0x00, 0x00, 0x00, 0x60, 0x0f, 0x00, 0x00, 0x00, 0x30, 0x11, 0x00,
 0x00, 0x00, 0xd8, 0x3b, 0x00, 0x00, 0x00, 0x4c, 0x48, 0x00, 0x00, 0x00,
 0xf6, 0xfc, 0x00, 0x00, 0x00, 0x13, 0x07, 0x01, 0x00, 0x80, 0xbd, 0x89,
 0x03, 0x00, 0xc0, 0x84, 0xde, 0x04, 0x00, 0x60, 0xcf, 0x42, 0x0f, 0x00,
 0x30, 0x71, 0x66, 0x11, 0x00, 0xd8, 0x9b, 0x3b, 0x3b, 0x00, 0x4c, 0xe8,
 0xc8, 0x49, 0x00, 0xf6, 0x2c, 0x7d, 0xfe, 0x00, 0x13, 0xe7, 0x85, 0x03,
 0x01
};

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  auto X = fromXBM(X_bits, X_w, X_h);
  X.invertPixels();
  auto bigX = X.scaled(X.size() * 4);
  auto rule30 = fromXBM(rule30_bits, rule30_w, rule30_h);
  rule30.invertPixels();
  auto big30 = rule30.scaled(rule30.size()*2);
  QWidget w;
  QHBoxLayout layout(&w);
  QLabel labelX, label30;
  layout.addWidget(&labelX);
  layout.addWidget(&label30);
  labelX.setPixmap(QPixmap::fromImage(bigX));
  label30.setPixmap(QPixmap::fromImage(big30));
  w.show();
  return a.exec();
}

答案 1 :(得分:1)

好的,事实证明,Qt alreay已经实现了一个简单的代码,可以从QImage的原始数据构建XBM

QImage image(size, monoFormat);
image.setColor(0, QColor(Qt::color0).rgb());
image.setColor(1, QColor(Qt::color1).rgb());

// Need to memcpy each line separatly since QImage is 32bit aligned and
// this data is only byte aligned...
int bytesPerLine = (size.width() + 7) / 8;
for (int y = 0; y < size.height(); ++y)
{
   memcpy(image.scanLine(y), bits + bytesPerLine * y, bytesPerLine);
}

此代码用于qbitmap.cpp,更具体地说,用于: QBitmap::fromData(const QSize &size, const uchar *bits, QImage::Format monoFormat)