Loading image with Magick++, displaying with Qt

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
RSX
Posts: 1
Joined: 2013-04-27T10:54:50-07:00
Authentication code: 6789

Loading image with Magick++, displaying with Qt

Post by RSX »

Hello, I have some problems using Magick++ with Qt.

Spec:
Magick++: 6.8.5
Qt: 5.0.2
Compiler: MSVC2010
Test Image: 600x600 JPEG

What I want to achieve is load with Magick++ images with various format (i.e. psd), put it in QPixmap and display in window.

Attempt 1:

Code: Select all

Magick::Image image;
try {
    image.read(qPrintable(fileName));
}
catch (Magick::Exception &error_) {
    QMessageBox::warning(this, "Error!", QString("%1").arg(error_.what()));
    return QPixmap();
}

Magick::Blob blob;
image.write(&blob, "XPM");

QPixmap pixmap;
pixmap.loadFromData((char*)blob.data(), "XPM");
return pixmap;
This actually works, but...

Code: Select all

image.write(&blob, "XPM");
takes 4 seconds, and the displayed image looks like lost quality (or is slightly blurry).

Attempt 2:

Code: Select all

Magick::Image image;
try {
    image.read(qPrintable(fileName));
}
catch (Magick::Exception &error_) {
    QMessageBox::warning(this, "Error!", QString("%1").arg(error_.what()));
    return QPixmap();
}

QImage img(image.columns(), image.rows(), QImage::Format_RGB32);
Magick::ColorRGB rgb;
for (int x = 0; x < img.width(); x++) {
    for (int y = 0; y < img.height(); y++) {
        rgb = *image.getPixels(x, y, 1, 1);
        QColor color(rgb.redQuantum(), rgb.greenQuantum(), rgb.blueQuantum());
        img.setPixel(x, y, color.rgb());
    }
}
return QPixmap::fromImage(img);
There's no noticeable delay now when loading image, but displayed image has wrong colors (gets blue-ish kinda).
I tested this code also with image 1x1 which had color (R: 183, G: 113, B: 51), but Magick++ returns it as (R: 113, G: 51, B: 51).

Any ideas what are the problem in those attempts?
zester
Posts: 2
Joined: 2013-05-09T18:15:49-07:00
Authentication code: 6789

Re: Loading image with Magick++, displaying with Qt

Post by zester »

I cant be exactly sure on your issue but I do know that ...

1. Image Magick is particular about what you give it to process, Example: You dont want to process a 8Bit image as a 32Bit image.
and
2. You have to do a color conversion when passing an image from Qt to Magick++ and vise versa.

Also note for the future when processing a image, such as adding a blur effect there are situations when a standard Blur or Gaussian Blur is more appropriate.
zester
Posts: 2
Joined: 2013-05-09T18:15:49-07:00
Authentication code: 6789

Re: Loading image with Magick++, displaying with Qt

Post by zester »

Try this code ...

Code: Select all

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;
}


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;
}
Post Reply