1/*
2 Large image displaying library.
3
4 Copyright (C) 2004,2005 Maks Orlovich (maksim@kde.org)
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23*/
24
25#ifndef IMAGE_FORMAT_H
26#define IMAGE_FORMAT_H
27
28#include <QColor>
29#include <QVector>
30#include <QImage>
31#include <assert.h>
32
33namespace khtmlImLoad
34{
35
36struct ImageFormat {
37 ImageFormat(): type(Image_INVALID)
38 {}
39
40 enum Type {
41 Image_RGB_32, // 32-bit RGB
42 Image_ARGB_32, // 32-bit ARGB; this will be premultiplied upon reception
43 // so should not be used in combination with requestScanline
44 Image_ARGB_32_DontPremult, // 32-bit ARGB that will be kept as-is
45 // should only be used for interlaced images
46 // as it is slower
47 Image_Palette_8, //8-bit paletted image
48 Image_INVALID
49 } type;
50
51 int depth() const
52 {
53 switch (type) {
54 case Image_RGB_32:
55 case Image_ARGB_32:
56 case Image_ARGB_32_DontPremult:
57 return 4;
58 default:
59 return 1;
60 }
61 }
62
63 QImage makeImage(int width, int height) const
64 {
65 QImage toRet;
66 switch (type) {
67 case Image_RGB_32:
68 toRet = QImage(width, height, QImage::Format_RGB32);
69 break;
70 case Image_ARGB_32:
71 toRet = QImage(width, height, QImage::Format_ARGB32_Premultiplied);
72 break;
73 case Image_ARGB_32_DontPremult:
74 toRet = QImage(width, height, QImage::Format_ARGB32);
75 break;
76 case Image_Palette_8:
77 toRet = QImage(width, height, QImage::Format_Indexed8);
78 // Make sure we're completely robust if the loader is buggy..
79 while (palette.size() < 256) {
80 palette.append(0);
81 }
82 toRet.setColorTable(palette);
83 break;
84 default:
85 assert(false);
86 }
87
88 return toRet;
89 }
90
91 bool hasAlpha() const
92 {
93 return (type == Image_ARGB_32 || type == Image_ARGB_32_DontPremult);
94 }
95
96 mutable QVector<QRgb> palette;
97
98 //A helper for setting up a format descriptor for 8-bit grayscale
99 void greyscaleSetup()
100 {
101 palette.clear();
102 for (int i = 0; i < 256; i++) {
103 palette.append(qRgb(i, i, i));
104 }
105 type = ImageFormat::Image_Palette_8;
106 }
107};
108
109}
110
111#endif
112