File indexing completed on 2024-05-19 11:38:33

0001 /*
0002     Progressive image displaying library.
0003 
0004     Copyright (C) 2004,2005 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_LOADER_H
0026 #define IMAGE_LOADER_H
0027 
0028 #include "imageformat.h"
0029 #include "image.h"
0030 
0031 namespace khtmlImLoad
0032 {
0033 
0034 class Image;
0035 
0036 /**
0037  A base class for decoders. The decoders should inherit off this, and use the protected functions to
0038  feed their images data
0039 */
0040 class ImageLoader
0041 {
0042 protected:
0043     Image *image;
0044 
0045     ImageLoader()
0046     {
0047         image = nullptr;
0048     }
0049 
0050     /**
0051      Call to declare canvas geometry and format. May only be called once
0052      */
0053     void notifyImageInfo(int width, int height)
0054     {
0055         image->notifyImageInfo(width, height);
0056     }
0057 
0058     /**
0059      Call to declare frame geometry, should be called for each frame.
0060     */
0061     void notifyAppendFrame(int fwidth, int fheight, const ImageFormat &format)
0062     {
0063         image->notifyAppendFrame(fwidth, fheight, format);
0064     }
0065 
0066     /**
0067      wrapper for the above method for single-frame images
0068     */
0069     void notifySingleFrameImage(int width, int height, const ImageFormat &format)
0070     {
0071         notifyImageInfo(width, height);
0072         notifyAppendFrame(width, height, format);
0073     }
0074 
0075     /**
0076      Call to provide a scanline, for active frame, and given "version".
0077      The decoder must feed multiple versions of the image inside here, top-to-bottom,
0078      and incrementing versions. When it's done, it should either feed the last
0079      version with FinalVersionID, or call notifyFinished() separately.
0080     */
0081     void notifyScanline(unsigned char version, unsigned char *line)
0082     {
0083         image->notifyScanline(version, line);
0084     }
0085 
0086     void notifyQImage(unsigned char version, const QImage *qimage)
0087     {
0088         image->notifyQImage(version, qimage);
0089     }
0090 
0091     /**
0092      Call this to exract the current state of a scanline to the provided bufer
0093     */
0094     void requestScanline(unsigned int lineNum, unsigned char *lineBuf)
0095     {
0096         image->requestScanline(lineNum, lineBuf);
0097     }
0098 
0099     /**
0100      Call this to get the first frame
0101     */
0102     PixmapPlane *requestFrame0()
0103     {
0104         return image->getSize(image->size());
0105     }
0106 
0107 public:
0108     //Some constants. Not consts since some compilers are broken
0109     enum {
0110         DefaultFrame   = 0,
0111         FinalVersionID = 0xff
0112     };
0113 
0114     void setImage(Image *_image)
0115     {
0116         image = _image;
0117     }
0118 
0119     virtual ~ImageLoader()
0120     {}
0121 
0122     enum Status {
0123         Done  = -2,
0124         Error = -1
0125     };
0126 
0127     /**
0128      Decodes a portion of the image, and returns the appropriate
0129      status, or the number of bytes read
0130     */
0131     virtual int processData(uchar *data, int length) = 0;
0132 
0133     /**
0134      This method is called to notify the decoder that the input is done, if the decoder
0135      has not already indicated some sort of completion on the stream; this is intended
0136      mostly for non-incremental decoders. It should return Done if all is OK. Note that
0137      the "if" above should mean that the non-incremental decoders should just return
0138      the bytes read in processData
0139     */
0140     virtual int processEOF()
0141     {
0142         return Done; //### Probably should be Error if notifyImageInfo has not been called
0143     }
0144 };
0145 
0146 }
0147 
0148 #endif