在ImageMagick 7的API c++中从PixelPacket移植对像素的访问

Porting access to pixels from PixelPacket in API C++ of ImageMagick 7

本文关键字:PixelPacket 像素 访问 中从 ImageMagick API c++      更新时间:2023-10-16

我尝试用ImageMagick-7.0.0-Q16-HDRI移植代码。

此代码适用于ImageMagick-6.9.1-Q16:

QString ImageMagick::fromQImage(const QImage &qimage){
    if (!qimage.size().isValid()) return "ImageMagick::fromQImage - Empty Qimage";
    image = Magick::Image(Magick::Geometry(qimage.width(), qimage.height()), Magick::ColorRGB(0.5, 0.2, 0.3));
    double scale = 1 / 255.0;
    image.modifyImage();
    Magick::PixelPacket *pixels;
    Magick::ColorRGB mgc;
    for (int y = 0; y < qimage.height(); y++) {
        pixels = image.setPixels(0, y, image.columns(), 1);
        for (int x = 0; x < qimage.width(); x++) {
            QColor pix = qimage.pixel(x, y);
            // *pixels++ = Magick::ColorRGB(256 * pix.red(), 256 * pix.green(), 256 * pix.blue());
            mgc.red(scale *pix.red());
            mgc.green(scale *pix.green());
            mgc.blue(scale *pix.blue());
            // *pixels++ = Magick::ColorRGB(scale *pix.red(), scale * pix.green(), scale * pix.blue());
            pixels[x] = mgc;
        }
        image.syncPixels();
    }
    return "";
}

现在我必须使用by http://www.imagemagick.org/script/porting.php代替PixelPacket:

MagickCore::Quantum *pixels;

在某些情况下:

  • pixels[0]-像素
  • 的红色通道
  • 像素[1]-像素
  • 的绿色通道
  • pixels[2]-像素
  • 的蓝色通道

如何使用SetPixelRed()…代替pixels[x] = mgc;

MagickCore::SetPixelRed((MagickCore::Image*)&image,scale*pix.red(),pixels+x*3);
MagickCore::SetPixelGreen((MagickCore::Image*)&image,scale*pix.green(),pixels+x*3);
MagickCore::SetPixelBlue((MagickCore::Image*)&image,scale*pix.blue(),pixels+x*3);

但是现在呢?

找到下一个解:

size_t imageQuantum = MAGICKCORE_QUANTUM_DEPTH;
size_t toFloatQuantum=(pow(2,imageQuantum)-1);
size_t imageChanells=image.channels();

pixels[x*imageChanells]=mgc.red()*toFloatQuantum;
pixels[x*imageChanells+1]=mgc.green()*toFloatQuantum;
pixels[x*imageChanells+2]=mgc.blue()*toFloatQuantum;

这是一个非常好的问题,但是我认为你从错误的角度来处理这个问题。

如果Magick::Pixels.get返回Quantum,则只需将QColor直接应用于值,sync

Magick::Image img(Magick::Geometry(2,2), Magick::Color("white"));
img.modifyImage();
Magick::Pixels view(img);
Magick::Quantum * pixels = view.get(0, 0, 2, 2);
int x,y;
for (y= 0; y < 2 ; y++ ) {
    for (x = 0 ; x < 2 ; x++ ) {
        // As we starting the pixels as white, we can
        // leverage multiplication assignment (*=) operator.
        *pixels++ *= pix.red()  / 255.0; // Red
        *pixels++ *= pix.green()/ 255.0; // Green
        *pixels++ *= pix.blue() / 255.0; // Blue
    }
}
view.sync()

不需要从MagickCore中调用任何其他类