Need some help with bitmap manipulating

Christian Ehrlicher Ch.Ehrlicher at gmx.de
Sat Nov 27 14:54:20 CET 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello

I've currently some problems copying a qimage/qpixmap to the clipboard.
My problem is that I don't know how to create a correct DIB which can be
passed to the clipboard.

Here my current idea, but it won't work when I try to paste the image
into Paint.

QByteArray QWinMimeDIB::convertFromMime( QByteArray data, const char*
mime, int cf )
{
~    QByteArray ret;
~    if ( qstrnicmp( mime, "image/", 6 ) || cf != CF_DIB )
~        return ret;
~    QByteArray ar;
~    QImage img;
~    QString format = QString( mime ).remove( "image/" );

~    img.loadFromData( ( unsigned char* ) data.data(), data.size() );
~    if ( img.isNull() )
~        return ret;
~    img = img.mirror( false, true );    /* No other idea atm */

~    int newsize = sizeof ( BITMAPINFO );

	switch (img.depth()) {
~        case 1:
~            newsize += sizeof( RGBQUAD ) * 2;
~            break;
~        case 4:
~            newsize += sizeof( RGBQUAD ) * 16;
~            break;
~        case 8:
~            newsize += sizeof( RGBQUAD ) * 256;
~            break;
~        case 16:
~        case 24:
~        case 32:
~            break;
~        default:
~            qDebug("Unsupported DIB-format (depth: %d)", img.depth());
~            break;
~    }
~    newsize += img.width() * img.height() * img.depth() / 8;
~    ret.resize( newsize, QGArray::SpeedOptim );

~    BITMAPINFO *bmpInfo = (BITMAPINFO*)ret.data();
~    uchar *ptr = (uchar*)bmpInfo + sizeof(BITMAPINFO);

~    bmpInfo->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
~    bmpInfo->bmiHeader.biWidth = img.width();
~    bmpInfo->bmiHeader.biHeight = img.height();
~    bmpInfo->bmiHeader.biPlanes = 1;
~    bmpInfo->bmiHeader.biBitCount = img.depth();
~    bmpInfo->bmiHeader.biCompression = BI_RGB;
~    bmpInfo->bmiHeader.biSizeImage = 0;
~    bmpInfo->bmiHeader.biXPelsPerMeter = 0;
~    bmpInfo->bmiHeader.biYPelsPerMeter = 0;
~    bmpInfo->bmiHeader.biClrUsed = 0;
~    bmpInfo->bmiHeader.biClrImportant = 0;

~    int usg = DIB_PAL_COLORS;
~    switch (img.depth()) {
~        case 1:
~            memcpy( img.colorTable(), bmpInfo->bmiColors, sizeof(
RGBQUAD ) * 2 );
~            ptr += sizeof( RGBQUAD ) * 2;
~            break;
~        case 4:
~            memcpy( img.colorTable(), bmpInfo->bmiColors, sizeof(
RGBQUAD ) * 16);
~            ptr += sizeof( RGBQUAD ) * 16;
~            break;
~        case 8:
~            memcpy( img.colorTable(), bmpInfo->bmiColors, sizeof(
RGBQUAD ) * 256);
~            ptr += sizeof( RGBQUAD ) * 256;
~            break;
~        case 16:
~        case 24:
~        case 32:
~            usg = DIB_RGB_COLORS;
~            break;
~        default:
~            qDebug("Unsupported DIB-format (biBitCount: %d)", img.depth());
~            break;
~    }
~    // this is wrong in my eyes...
~    memcpy( ptr, img.bits(), img.width() * img.height() * img.depth() / 8);
~    return ret;
}

Thx
Christian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFBqIcMnNKwkgf+zVMRAuDLAJ96rE4aEWbSDR1JN3dbscSJtg8iOwCfUHhS
hlq83B4AYdKPUrcom6gqHMM=
=0KNy
-----END PGP SIGNATURE-----


More information about the kde-cygwin mailing list