summaryrefslogtreecommitdiff
path: root/noncore/graphics/opie-eye/slave/bmp_slave.cpp
blob: 0efadac4f67e68f63d7a33f00ba88f12e4522d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include  "bmp_slave.h"

#include "thumbnailtool.h"

#include <qimage.h>
#include <qobject.h>
#include <qfile.h>
#include <qpixmap.h>
#include <qstring.h>

PHUNK_VIEW_INTERFACE( "Bmp", BmpSlave );
namespace {

    struct pBmpHeader {
        // file header of bmp
        char type[2]; // must be "BM" otherwise it is no bmp
        Q_INT32 hSize;
        Q_INT32 reserved1,reserved2;

        Q_INT16  Planes;
        Q_INT16  BitCount;
        Q_INT32  Size;
        Q_INT32  Width;
        Q_INT32  Height;
        Q_INT32  Compression;
        Q_INT32  SizeImage;
        Q_INT32  XPerMeter;
        Q_INT32  YPerMeter;
        Q_INT32  ClrUsed;
        Q_INT32  ClrImportant;
    };

    class BmpHeader {
    protected:
        void read_data();
        QString _name;
        QFile _inputfile;
        pBmpHeader m_Header;

        static const int OLD = 12;
        static const int WIN = 40;
        static const int OS2 = 64;
        static const int RGB = 0;
        static const int RLE8 = 1;
        static const int RLE4 = 2;

    public:
        BmpHeader(const QString&fname);
        virtual ~BmpHeader();
        bool isBmp()const{return qstrncmp(m_Header.type,"BM",2)==0;}
        bool isCompressed()const{return m_Header.Compression != 0;}
        QSize imageSize(){return QSize(m_Header.Width,m_Header.Height);}
        QString imageCompression()const;
        int bitsPixel()const{return m_Header.BitCount;}
        int Size()const{return m_Header.hSize;}
        int compressedSize()const{return m_Header.SizeImage;}
        int ColorsUsed()const{return m_Header.ClrUsed;}
        int XPix()const{return m_Header.XPerMeter;}
        int YPix()const{return m_Header.YPerMeter;}
    };

    QString BmpHeader::imageCompression()const
    {
        switch (m_Header.Compression) {
            case RLE8:
                return "8Bit RLE Encoding";
                break;
            case RLE4:
                return "4Bit RLE Encoding";
                break;
            case RGB:
            default:
                return "No encoding";
        }
    }

    BmpHeader::BmpHeader(const QString&fname)
        : _name(fname),_inputfile(_name)
    {
        read_data();
    }

    void BmpHeader::read_data() {
        memset(&m_Header,0,sizeof(pBmpHeader));
        if (!_inputfile.open(IO_Raw|IO_ReadOnly))
            return;
        QDataStream s(&_inputfile);
        s.setByteOrder( QDataStream::LittleEndian );
        s.readRawBytes(m_Header.type,2);
        if (!isBmp()) {
            _inputfile.close();
            return;
        }
        s >> m_Header.hSize;
        s >> m_Header.reserved1 >> m_Header.reserved2;
        s >> m_Header.Size;
        if ( m_Header.Size == BmpHeader::WIN || m_Header.Size == BmpHeader::OS2 ) {
            s >> m_Header.Width >> m_Header.Height >> m_Header.Planes >> m_Header.BitCount;
            s >> m_Header.Compression >> m_Header.SizeImage;
            s >> m_Header.XPerMeter >> m_Header.YPerMeter;
            s >> m_Header.ClrUsed >> m_Header.ClrImportant;
        } else {
            Q_INT16 w, h;
            s >> w >> h >> m_Header.Planes >> m_Header.BitCount;
            m_Header.Width  = w;
            m_Header.Height = h;
            m_Header.Compression = BmpHeader::RGB;
            m_Header.SizeImage = 0;
            m_Header.XPerMeter = m_Header.YPerMeter = 0;
            m_Header.ClrUsed = m_Header.ClrImportant = 0;
        }
        _inputfile.close();
    }

    BmpHeader::~BmpHeader() {
    }
}


BmpSlave::BmpSlave()
    : SlaveInterface(QStringList("bmp"))
{}

BmpSlave::~BmpSlave() {

}

QString BmpSlave::iconViewName(const QString& str) {
    QString st;
    BmpHeader bh(str);
    if (!bh.isBmp()) {
        st.append("No bmp file");
        return st;
    }
    QSize isize = bh.imageSize();
    st+=QObject::tr("Dimensions: %1 x %2\n").arg(isize.width()).arg(isize.height());
    st+=QObject::tr("Size: %1\n").arg(bh.Size());
    st+=QObject::tr("Depth: %1\n").arg(bh.bitsPixel());
    return st;
}

QString BmpSlave::fullImageInfo( const QString& str) {
    QString st = "<qt>";
    BmpHeader bh(str);
    if (!bh.isBmp()) {
        st.append("No bmp file");
        st.append(  "</qt>" );
        return st;
    }
    QSize isize = bh.imageSize();
    st+=QObject::tr("Dimensions: %1 x %2\n").arg(isize.width()).arg(isize.height());
    st+=QObject::tr("Size: %1\n").arg(bh.Size());
    st+=QObject::tr("Compression: %1\n").arg(bh.imageCompression());
    if (bh.isCompressed()) {
        st+=QObject::tr("Compressed size: %1").arg(bh.compressedSize());
    }
    st+=QObject::tr("Depth: %1\n").arg(bh.bitsPixel());
    st+=QObject::tr("used colors: %1\n").arg(bh.ColorsUsed());
    st+=QObject::tr("Resolution: %1 x %2\n").arg(bh.XPix()).arg(bh.YPix());
    st.append(  "</qt>" );
    return st;
}

QPixmap BmpSlave::pixmap(const QString& path, int width, int height ) {
    static QImage img;
    img.load( path );
    if ( img.isNull() ) {
        QPixmap pix;
        return pix;
    }

    return ThumbNailTool::scaleImage( img, width,height );
}