File indexing completed on 2024-12-08 12:19:27

0001 /*
0002     Large 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_FORMAT_H
0026 #define IMAGE_FORMAT_H
0027 
0028 #include <QColor>
0029 #include <QVector>
0030 #include <QImage>
0031 #include <assert.h>
0032 
0033 namespace khtmlImLoad
0034 {
0035 
0036 struct ImageFormat {
0037     ImageFormat(): type(Image_INVALID)
0038     {}
0039 
0040     enum Type {
0041         Image_RGB_32,     // 32-bit RGB
0042         Image_ARGB_32,    // 32-bit ARGB; this will be premultiplied upon reception
0043         // so should not be used in combination with requestScanline
0044         Image_ARGB_32_DontPremult,  // 32-bit ARGB that will be kept as-is
0045         // should only be used for interlaced images
0046         // as it is slower
0047         Image_Palette_8,   //8-bit paletted image
0048         Image_INVALID
0049     } type;
0050 
0051     int depth() const
0052     {
0053         switch (type) {
0054         case Image_RGB_32:
0055         case Image_ARGB_32:
0056         case Image_ARGB_32_DontPremult:
0057             return 4;
0058         default:
0059             return 1;
0060         }
0061     }
0062 
0063     QImage makeImage(int width, int height) const
0064     {
0065         QImage toRet;
0066         switch (type) {
0067         case Image_RGB_32:
0068             toRet = QImage(width, height, QImage::Format_RGB32);
0069             break;
0070         case Image_ARGB_32:
0071             toRet = QImage(width, height, QImage::Format_ARGB32_Premultiplied);
0072             break;
0073         case Image_ARGB_32_DontPremult:
0074             toRet = QImage(width, height, QImage::Format_ARGB32);
0075             break;
0076         case Image_Palette_8:
0077             toRet = QImage(width, height, QImage::Format_Indexed8);
0078             // Make sure we're completely robust if the loader is buggy..
0079             while (palette.size() < 256) {
0080                 palette.append(0);
0081             }
0082             toRet.setColorTable(palette);
0083             break;
0084         default:
0085             assert(false);
0086         }
0087 
0088         return toRet;
0089     }
0090 
0091     bool hasAlpha() const
0092     {
0093         return (type == Image_ARGB_32 || type == Image_ARGB_32_DontPremult);
0094     }
0095 
0096     mutable QVector<QRgb> palette;
0097 
0098     //A helper for setting up a format descriptor for 8-bit grayscale
0099     void greyscaleSetup()
0100     {
0101         palette.clear();
0102         for (int i = 0; i < 256; i++) {
0103             palette.append(qRgb(i, i, i));
0104         }
0105         type = ImageFormat::Image_Palette_8;
0106     }
0107 };
0108 
0109 }
0110 
0111 #endif