File indexing completed on 2024-05-19 15:12:18

0001 /*
0002     Large image displaying library.
0003 
0004     Copyright (C) 2004 Maks Orlovich (maksim@kde.org)
0005 
0006     Permission is hereby granted, free of charge, to any person obtaining a copy
0007     of this software and associated documentation files (the "Software"), to deal
0008     in the Software without restriction, including without limitation the rights
0009     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0010     copies of the Software, and to permit persons to whom the Software is
0011     furnished to do so, subject to the following conditions:
0012 
0013     The above copyright notice and this permission notice shall be included in
0014     all copies or substantial portions of the Software.
0015 
0016     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0017     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0018     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0019     AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0020     AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0021     CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0022 
0023 */
0024 
0025 #ifndef IMAGE_H
0026 #define IMAGE_H
0027 
0028 #include <QByteArray>
0029 #include <QSize>
0030 #include <QTimer>
0031 #include <QPair>
0032 #include <QMap>
0033 
0034 #include <khtml_settings.h>
0035 
0036 #include "imageformat.h"
0037 
0038 class QPainter;
0039 
0040 namespace khtmlImLoad
0041 {
0042 
0043 class ImageOwner;
0044 class ImageLoader;
0045 class PixmapPlane;
0046 
0047 /**
0048  An image represents a static picture or an animation, that
0049  may be incrementally loaded.
0050 */
0051 class Image
0052 {
0053 public:
0054     /**
0055      Creates an image with a given owner; the owner will be notified about the repaint event, the image geometry,
0056      and so on; and also if the image can not be decoded. The image must be fed data through the processData()
0057      call; and it will take care of the rest automatically.
0058     */
0059     Image(ImageOwner *owner);
0060 
0061     /**
0062      Provides new data for decoding. The method will return false if there is no longer any use to feeding it more data
0063      (i.e. if the image is complete, or broken, etc.); however, it is safe to do so.
0064     */
0065     bool processData(uchar *data, int length);
0066 
0067     /**
0068      Notifies the image that the data source is exhausted, in case it cares. This should be called at the
0069      end of the data stream in order for non-incremental decoders to work
0070     */
0071     void processEOF();
0072 
0073     /**
0074      Cleans up
0075     */
0076     ~Image();
0077 
0078     /**
0079      Returns the image's size
0080     */
0081     QSize size() const;
0082 
0083     /**
0084      Returns true if the image has been fully loaded
0085     */
0086     bool complete() const;
0087 
0088     /**
0089      Returns true if the image may have an alpha channel
0090     */
0091     bool hasAlpha() const;
0092 
0093     /**
0094      Returns the image of basic content. Should be treated as READ ONLY.
0095      (but see CanvasImage)
0096     */
0097     QImage *qimage()  const;
0098 
0099     /**
0100      Enables or disables animations
0101     */
0102     void setShowAnimations(KHTMLSettings::KAnimationAdvice);
0103 private:
0104     //Interface to the loader.
0105     friend class ImageLoader;
0106 
0107     /**
0108      Called from the loader to notify of canvas geometry.
0109      The loader must also call notifyAppendFrame to
0110      create 1 or more frames
0111     */
0112     void notifyImageInfo(int width, int height);
0113 
0114     /**
0115      Called to notify of format of a frame
0116     */
0117     void notifyAppendFrame(int fwidth, int fheight, const ImageFormat &format);
0118 
0119     /**
0120      Called from the loader to feed a new scanline (in consecutive order in each frame), through
0121      various progressive versions
0122      */
0123     void notifyScanline(unsigned char version, unsigned char *data);
0124 
0125     /**
0126      Called from the loader to feed a new image, through
0127      various progressive versions
0128      */
0129     void notifyQImage(unsigned char version, const QImage *image);
0130 
0131     /**
0132      Called from loader to request the current contents of the line in the basic plane
0133      */
0134     void requestScanline(unsigned int lineNum, unsigned char *lineBuf);
0135 
0136     //### FIXME: restore the animprovider interface
0137 
0138 private: //Interface to the painter.
0139     friend class ImagePainter;
0140     bool mayPaint()
0141     {
0142         return original;
0143     }
0144     void derefSize(QSize size);
0145     void refSize(QSize size);
0146     PixmapPlane *getSize(QSize size);
0147 
0148 protected:
0149     ImageOwner *owner;
0150 
0151     //Update reporting to owner
0152     friend class Updater;
0153     friend class AnimProvider;
0154     bool updatesPending;
0155     int updatesStartLine;
0156     int updatesEndLine;
0157 
0158     //Incorporate the scanline into update range
0159     void requestUpdate(int line);
0160 
0161     //Sets the state as not having updates
0162     void noUpdates();
0163 
0164     /**
0165      Called by the updater when the image should tell its owners about new changes
0166     */
0167     void notifyPerformUpdate();
0168 
0169     /**
0170      Called when animation frame changes, requesting the owner to repaint
0171     */
0172     void notifyFrameChange();
0173 
0174     //Loader stuff.
0175     QByteArray bufferPreDetect;
0176     ImageLoader *loader;
0177     PixmapPlane *loaderPlane;
0178     unsigned int loaderScanline;
0179 
0180     bool fullyDecoded;
0181     bool inError;
0182 
0183     //A little helper to set the error condition.
0184     void loadError();
0185 
0186     //Image state
0187     unsigned int width, height;
0188     PixmapPlane *original;
0189     QMap<QPair<int, int>, PixmapPlane *> scaled;
0190     KHTMLSettings::KAnimationAdvice animationAdvice;
0191 
0192 };
0193 
0194 }
0195 
0196 #endif