ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

pixelformat.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef GRAPHICS_PIXELFORMAT_H
00024 #define GRAPHICS_PIXELFORMAT_H
00025 
00026 #include "common/scummsys.h"
00027 #include "common/str.h"
00028 
00029 namespace Graphics {
00030 
00032 template<int depth>
00033 struct ColorComponent {
00034 };
00035 
00036 template<>
00037 struct ColorComponent<0> {
00038     static inline uint expand(uint value) {
00039         return 0;
00040     }
00041 };
00042 
00043 template<>
00044 struct ColorComponent<1> {
00045     static inline uint expand(uint value) {
00046         value &= 1;
00047         return value |
00048                (value << 1) |
00049                (value << 2) |
00050                (value << 3) |
00051                (value << 4) |
00052                (value << 5) |
00053                (value << 6) |
00054                (value << 7);
00055     }
00056 };
00057 
00058 template<>
00059 struct ColorComponent<2> {
00060     static inline uint expand(uint value) {
00061         value &= 3;
00062         return value |
00063                (value << 2) |
00064                (value << 4) |
00065                (value << 6);
00066     }
00067 };
00068 
00069 template<>
00070 struct ColorComponent<3> {
00071     static inline uint expand(uint value) {
00072         value &= 7;
00073         return (value << 5) |
00074                (value << 2) |
00075                (value >> 1);
00076     }
00077 };
00078 
00079 template<>
00080 struct ColorComponent<4> {
00081     static inline uint expand(uint value) {
00082         value &= 15;
00083         return value |
00084                (value << 4);
00085     }
00086 };
00087 
00088 template<>
00089 struct ColorComponent<5> {
00090     static inline uint expand(uint value) {
00091         value &= 31;
00092         return (value << 3) |
00093                (value >> 2);
00094     }
00095 };
00096 
00097 template<>
00098 struct ColorComponent<6> {
00099     static inline uint expand(uint value) {
00100         value &= 63;
00101         return (value << 2) |
00102                (value >> 4);
00103     }
00104 };
00105 
00106 template<>
00107 struct ColorComponent<7> {
00108     static inline uint expand(uint value) {
00109         value &= 127;
00110         return (value << 1) |
00111                (value >> 6);
00112     }
00113 };
00114 
00115 template<>
00116 struct ColorComponent<8> {
00117     static inline uint expand(uint value) {
00118         return value & 255;
00119     }
00120 };
00121 
00136 struct PixelFormat {
00137     byte bytesPerPixel; 
00139     byte rLoss, gLoss, bLoss, aLoss; 
00140     byte rShift, gShift, bShift, aShift; 
00142     inline PixelFormat() {
00143         bytesPerPixel =
00144         rLoss = gLoss = bLoss = aLoss =
00145         rShift = gShift = bShift = aShift = 0;
00146     }
00147 
00148     inline PixelFormat(byte BytesPerPixel,
00149                         byte RBits, byte GBits, byte BBits, byte ABits,
00150                         byte RShift, byte GShift, byte BShift, byte AShift) {
00151         bytesPerPixel = BytesPerPixel;
00152         rLoss = 8 - RBits;
00153         gLoss = 8 - GBits;
00154         bLoss = 8 - BBits;
00155         aLoss = 8 - ABits;
00156         rShift = RShift;
00157         gShift = GShift;
00158         bShift = BShift;
00159         aShift = AShift;
00160     }
00161 
00162     static inline PixelFormat createFormatCLUT8() {
00163         return PixelFormat(1, 0, 0, 0, 0, 0, 0, 0, 0);
00164     }
00165 
00166     inline bool operator==(const PixelFormat &fmt) const {
00167         // TODO: If aLoss==8, then the value of aShift is irrelevant, and should be ignored.
00168         return bytesPerPixel == fmt.bytesPerPixel &&
00169                rLoss == fmt.rLoss &&
00170                gLoss == fmt.gLoss &&
00171                bLoss == fmt.bLoss &&
00172                aLoss == fmt.aLoss &&
00173                rShift == fmt.rShift &&
00174                gShift == fmt.gShift &&
00175                bShift == fmt.bShift &&
00176                aShift == fmt.aShift;
00177     }
00178 
00179     inline bool operator!=(const PixelFormat &fmt) const {
00180         return !(*this == fmt);
00181     }
00182 
00183     inline uint32 RGBToColor(uint8 r, uint8 g, uint8 b) const {
00184         return
00185             ((0xFF >> aLoss) << aShift) |
00186             ((   r >> rLoss) << rShift) |
00187             ((   g >> gLoss) << gShift) |
00188             ((   b >> bLoss) << bShift);
00189     }
00190 
00191     inline uint32 ARGBToColor(uint8 a, uint8 r, uint8 g, uint8 b) const {
00192         return
00193             ((a >> aLoss) << aShift) |
00194             ((r >> rLoss) << rShift) |
00195             ((g >> gLoss) << gShift) |
00196             ((b >> bLoss) << bShift);
00197     }
00198 
00199     inline void colorToRGB(uint32 color, uint8 &r, uint8 &g, uint8 &b) const {
00200         r = expand(rBits(), color >> rShift);
00201         g = expand(gBits(), color >> gShift);
00202         b = expand(bBits(), color >> bShift);
00203     }
00204 
00205     inline void colorToARGB(uint32 color, uint8 &a, uint8 &r, uint8 &g, uint8 &b) const {
00206         a = (aBits() == 0) ? 0xFF : expand(aBits(), color >> aShift);
00207         r = expand(rBits(), color >> rShift);
00208         g = expand(gBits(), color >> gShift);
00209         b = expand(bBits(), color >> bShift);
00210     }
00211 
00213     // Convenience functions for getting number of color component bits //
00215 
00216     inline byte rBits() const {
00217         return (8 - rLoss);
00218     }
00219 
00220     inline byte gBits() const {
00221         return (8 - gLoss);
00222     }
00223 
00224     inline byte bBits() const {
00225         return (8 - bLoss);
00226     }
00227 
00228     inline byte aBits() const {
00229         return (8 - aLoss);
00230     }
00231 
00232     inline byte bpp() const {
00233         return rBits() + gBits() + bBits() + aBits();
00234     }
00235 
00237     // Convenience functions for getting color components' maximum values //
00239 
00240     inline uint rMax() const {
00241         return (1 << rBits()) - 1;
00242     }
00243 
00244     inline uint gMax() const {
00245         return (1 << gBits()) - 1;
00246     }
00247 
00248     inline uint bMax() const {
00249         return (1 << bBits()) - 1;
00250     }
00251 
00252     inline uint aMax() const {
00253         return (1 << aBits()) - 1;
00254     }
00255 
00257     static inline uint expand(uint bits, uint color) {
00258         switch (bits) {
00259         case 0:
00260             return ColorComponent<0>::expand(color);
00261         case 1:
00262             return ColorComponent<1>::expand(color);
00263         case 2:
00264             return ColorComponent<2>::expand(color);
00265         case 3:
00266             return ColorComponent<3>::expand(color);
00267         case 4:
00268             return ColorComponent<4>::expand(color);
00269         case 5:
00270             return ColorComponent<5>::expand(color);
00271         case 6:
00272             return ColorComponent<6>::expand(color);
00273         case 7:
00274             return ColorComponent<7>::expand(color);
00275         case 8:
00276             return ColorComponent<8>::expand(color);
00277         }
00278 
00279         // Unsupported
00280         return 0;
00281     }
00282 
00283     Common::String toString() const;
00284 };
00285 
00286 } // End of namespace Graphics
00287 
00288 #endif


Generated on Sat Feb 16 2019 05:01:01 for ResidualVM by doxygen 1.7.1
curved edge   curved edge