1/*
2 Large image displaying library.
3
4 Copyright (C) 2004 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_H
26#define IMAGE_H
27
28#include <QByteArray>
29#include <QSize>
30#include <QTimer>
31#include <QPair>
32#include <QMap>
33
34#include <khtml_settings.h>
35
36#include "imageformat.h"
37
38class QPainter;
39
40namespace khtmlImLoad
41{
42
43class ImageOwner;
44class ImageLoader;
45class PixmapPlane;
46
47/**
48 An image represents a static picture or an animation, that
49 may be incrementally loaded.
50*/
51class Image
52{
53public:
54 /**
55 Creates an image with a given owner; the owner will be notified about the repaint event, the image geometry,
56 and so on; and also if the image can not be decoded. The image must be fed data through the processData()
57 call; and it will take care of the rest automatically.
58 */
59 Image(ImageOwner *owner);
60
61 /**
62 Provides new data for decoding. The method will return false if there is no longer any use to feeding it more data
63 (i.e. if the image is complete, or broken, etc.); however, it is safe to do so.
64 */
65 bool processData(uchar *data, int length);
66
67 /**
68 Notifies the image that the data source is exhausted, in case it cares. This should be called at the
69 end of the data stream in order for non-incremental decoders to work
70 */
71 void processEOF();
72
73 /**
74 Cleans up
75 */
76 ~Image();
77
78 /**
79 Returns the image's size
80 */
81 QSize size() const;
82
83 /**
84 Returns true if the image has been fully loaded
85 */
86 bool complete() const;
87
88 /**
89 Returns true if the image may have an alpha channel
90 */
91 bool hasAlpha() const;
92
93 /**
94 Returns the image of basic content. Should be treated as READ ONLY.
95 (but see CanvasImage)
96 */
97 QImage *qimage() const;
98
99 /**
100 Enables or disables animations
101 */
102 void setShowAnimations(KHTMLSettings::KAnimationAdvice);
103private:
104 //Interface to the loader.
105 friend class ImageLoader;
106
107 /**
108 Called from the loader to notify of canvas geometry.
109 The loader must also call notifyAppendFrame to
110 create 1 or more frames
111 */
112 void notifyImageInfo(int width, int height);
113
114 /**
115 Called to notify of format of a frame
116 */
117 void notifyAppendFrame(int fwidth, int fheight, const ImageFormat &format);
118
119 /**
120 Called from the loader to feed a new scanline (in consecutive order in each frame), through
121 various progressive versions
122 */
123 void notifyScanline(unsigned char version, unsigned char *data);
124
125 /**
126 Called from the loader to feed a new image, through
127 various progressive versions
128 */
129 void notifyQImage(unsigned char version, const QImage *image);
130
131 /**
132 Called from loader to request the current contents of the line in the basic plane
133 */
134 void requestScanline(unsigned int lineNum, unsigned char *lineBuf);
135
136 //### FIXME: restore the animprovider interface
137
138private: //Interface to the painter.
139 friend class ImagePainter;
140 bool mayPaint()
141 {
142 return original;
143 }
144 void derefSize(QSize size);
145 void refSize(QSize size);
146 PixmapPlane *getSize(QSize size);
147
148protected:
149 ImageOwner *owner;
150
151 //Update reporting to owner
152 friend class Updater;
153 friend class AnimProvider;
154 bool updatesPending;
155 int updatesStartLine;
156 int updatesEndLine;
157
158 //Incorporate the scanline into update range
159 void requestUpdate(int line);
160
161 //Sets the state as not having updates
162 void noUpdates();
163
164 /**
165 Called by the updater when the image should tell its owners about new changes
166 */
167 void notifyPerformUpdate();
168
169 /**
170 Called when animation frame changes, requesting the owner to repaint
171 */
172 void notifyFrameChange();
173
174 //Loader stuff.
175 QByteArray bufferPreDetect;
176 ImageLoader *loader;
177 PixmapPlane *loaderPlane;
178 unsigned int loaderScanline;
179
180 bool fullyDecoded;
181 bool inError;
182
183 //A little helper to set the error condition.
184 void loadError();
185
186 //Image state
187 unsigned int width, height;
188 PixmapPlane *original;
189 QMap<QPair<int, int>, PixmapPlane *> scaled;
190 KHTMLSettings::KAnimationAdvice animationAdvice;
191
192};
193
194}
195
196#endif
197