-
-
Save marcinlawnik/758e927ce5292d417e3daaf10e2611f7 to your computer and use it in GitHub Desktop.
Conversion between QImage and Magick::Image. Tested and reliable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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