How to convert Magick::Image from ImageMagick 7 to QImage?

20 Views Asked by At

This was a quite easy task in ImageMagick 6, just getConstPixels(), ColorRGB and you are done.

But I'm unable to do the same task with ImageMagick 7, I got an ugly mostly pink something.

What am I doing wrong?

QImage
convert( const Magick::Image & img )
{
    QImage qimg( static_cast< int > ( img.columns() ),
        static_cast< int > ( img.rows() ), QImage::Format_RGB888 );
    using Quantum = MagickCore::Quantum;
    const Quantum * pixels;

    for( int y = 0; y < qimg.height(); ++y)
    {
        pixels = img.getConstPixels( 0, y, static_cast< std::size_t > ( qimg.width() ), 1 );

        for( int x = 0; x < qimg.width(); ++x )
        {
            int red = 0;
            int green = 0;
            int blue = 0;

            if( MagickCore::GetPixelRedTraits( img.constImage() ) )
                red = ( (double) GetPixelRed( img.constImage(), pixels ) /
                    (double) QuantumRange ) * 255;

            if( MagickCore::GetPixelGreenTraits( img.constImage() ) )
                green = ( (double) GetPixelGreen( img.constImage(), pixels ) /
                    (double) QuantumRange ) * 255;

            if( MagickCore::GetPixelBlueTraits( img.constImage() ) )
                blue = ( (double) GetPixelBlue( img.constImage(), pixels ) /
                    (double) QuantumRange ) * 255;

            qimg.setPixel( x, y, QColor( red, green, blue ).rgb() );

            pixels += MagickCore::GetPixelChannels( img.constImage() );
        }
    }

    return qimg;
}

Thank you.

Source image - Source image.

Result - Result.

As I can guess, in ImageMagick 7 something wrong with touching pixel by X, Y. Am I right?

1

There are 1 best solutions below

0
Igor Mironchik On

I have no clue why the above code doesn't work, but I found a following solution:

QImage
convert( const Magick::Image & img )
{
    QImage qimg( static_cast< int > ( img.columns() ),
        static_cast< int > ( img.rows() ), QImage::Format_ARGB32 );

    for( int y = 0; y < qimg.height(); ++y )
    {
        for( int x = 0; x < qimg.width(); ++x )
        {
            Magick::PixelInfo p = img.pixelColor( x, y );

            qimg.setPixel( x, y, QColor( MagickCore::ScaleQuantumToChar( p.red ),
                MagickCore::ScaleQuantumToChar( p.green ),
                MagickCore::ScaleQuantumToChar( p.blue ),
                MagickCore::ScaleQuantumToChar( p.alpha ) ).rgba() );
        }
    }

    return qimg;
}