Skip to content

Instantly share code, notes, and snippets.

@marcinlawnik
Forked from pelsedyr/qimg_image_conv.cpp
Created May 21, 2017 15:22
Show Gist options
  • Save marcinlawnik/758e927ce5292d417e3daaf10e2611f7 to your computer and use it in GitHub Desktop.
Save marcinlawnik/758e927ce5292d417e3daaf10e2611f7 to your computer and use it in GitHub Desktop.
Conversion between QImage and Magick::Image. Tested and reliable.
Image* MainWindow::toImage(QImage* qimage)
{
qDebug() << "toImage:" << qimage->width() << qimage->height();
Image *newImage = new Image(Magick::Geometry(qimage->width(), qimage->height()), Magick::ColorRGB(0.5, 0.2, 0.3));
double scale = 1 / 256.0;
newImage->modifyImage();
Magick::PixelPacket *pixels;
Magick::ColorRGB mgc;
for (int y = 0; y < qimage->height(); y++) {
pixels = newImage->setPixels(0, y, newImage->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++ = mgc;
}
newImage->syncPixels();
}
return newImage;
}
QImage* MainWindow::toQImage(Image *image)
{
qDebug() << "toQImage:" << image->columns() << image->rows();
QImage *newQImage = new QImage(image->columns(), image->rows(), QImage::Format_RGB32);
const Magick::PixelPacket *pixels;
Magick::ColorRGB rgb;
for (int y = 0; y < newQImage->height(); y++) {
pixels = image->getConstPixels(0, y, newQImage->width(), 1);
for (int x = 0; x < newQImage->width(); x++) {
rgb = (*(pixels + x));
newQImage->setPixel(x, y, QColor((int) (255 * rgb.red())
, (int) (255 * rgb.green())
, (int) (255 * rgb.blue())).rgb());
}
}
return newQImage;
}
@Zaraka
Copy link

Zaraka commented Sep 10, 2022

I have revised the code to include an alpha channel. I have also made a few changes like not putting QImage to heap as it is implicitly shared object and skipping the int conversion with the utilization of double format. Tested with ImageMagick6

Magick::Image* MagickUtils::toImage(const QImage& qimage) {
    Magick::Image* newImage = new Magick::Image(Magick::Geometry(qimage.width(), qimage.height()), Magick::Color());

    const int height = qimage.height();
    const int width  = qimage.width();

    newImage->modifyImage();
    for (int y = 0; y < height; y++) {
        Magick::PixelPacket* pixels = newImage->setPixels(0, y, newImage->columns(), 1);
        for (int x = 0; x < width; x++) {
            const QColor pix = qimage.pixelColor(x, y);
            pixels->red      = Magick::Color::scaleDoubleToQuantum(pix.redF());
            pixels->green    = Magick::Color::scaleDoubleToQuantum(pix.greenF());
            pixels->blue     = Magick::Color::scaleDoubleToQuantum(pix.blueF());
            pixels->opacity  = Magick::Color::scaleDoubleToQuantum(pix.alphaF());
            pixels++;
        }
        newImage->syncPixels();
    }

    return newImage;
}

QImage MagickUtils::toQImage(Magick::Image* image) {
    QImage newQImage = QImage(static_cast<int>(image->columns()), static_cast<int>(image->rows()), QImage::Format_RGBA8888);

    for (int y = 0; y < newQImage.height(); y++) {
        const Magick::PixelPacket* pixels = image->getConstPixels(0, y, newQImage.width(), 1);
        for (int x = 0; x < newQImage.width(); x++) {
            const Magick::PixelPacket& pixel = pixels[x];
            newQImage.setPixel(x,
                               y,
                               QColor::fromRgbF(Magick::Color::scaleQuantumToDouble(pixel.red),
                                                Magick::Color::scaleQuantumToDouble(pixel.green),
                                                Magick::Color::scaleQuantumToDouble(pixel.blue),
                                                Magick::Color::scaleQuantumToDouble(pixel.opacity))
                                   .rgba());
        }
    }

    return newQImage;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment