Blender V2.27

PixelFormat.h

00001 
00025 /*
00026  * This file is based on a similar file from the NVIDIA texture tools
00027  * (http://nvidia-texture-tools.googlecode.com/)
00028  *
00029  * Original license from NVIDIA follows.
00030  */
00031 
00032 // Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
00033 // 
00034 // Permission is hereby granted, free of charge, to any person
00035 // obtaining a copy of this software and associated documentation
00036 // files (the "Software"), to deal in the Software without
00037 // restriction, including without limitation the rights to use,
00038 // copy, modify, merge, publish, distribute, sublicense, and/or sell
00039 // copies of the Software, and to permit persons to whom the
00040 // Software is furnished to do so, subject to the following
00041 // conditions:
00042 // 
00043 // The above copyright notice and this permission notice shall be
00044 // included in all copies or substantial portions of the Software.
00045 // 
00046 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00047 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00048 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00049 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00050 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00051 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00052 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00053 // OTHER DEALINGS IN THE SOFTWARE.
00054 
00055 #ifndef _DDS_PIXELFORMAT_H
00056 #define _DDS_PIXELFORMAT_H
00057 
00058 #include <Common.h>
00059 
00060         namespace PixelFormat
00061         {
00062 
00063                 // Convert component @a c having @a inbits to the returned value having @a outbits.
00064                 inline uint convert(uint c, uint inbits, uint outbits)
00065                 {
00066                         if (inbits == 0)
00067                         {
00068                                 return 0;
00069                         }
00070                         else if (inbits >= outbits)
00071                         {
00072                                 // truncate
00073                                 return c >> (inbits - outbits);
00074                         }
00075                         else
00076                         {
00077                                 // bitexpand
00078                                 return (c << (outbits - inbits)) | convert(c, inbits, outbits - inbits);
00079                         }
00080                 }
00081 
00082                 // Get pixel component shift and size given its mask.
00083                 inline void maskShiftAndSize(uint mask, uint * shift, uint * size)
00084                 {
00085                         if (!mask)
00086                         {
00087                                 *shift = 0;
00088                                 *size = 0;
00089                                 return;
00090                         }
00091 
00092                         *shift = 0;
00093                         while((mask & 1) == 0) {
00094                                 ++(*shift);
00095                                 mask >>= 1;
00096                         }
00097                         
00098                         *size = 0;
00099                         while((mask & 1) == 1) {
00100                                 ++(*size);
00101                                 mask >>= 1;
00102                         }
00103                 }
00104 
00105         } // PixelFormat namespace
00106 
00107 #endif // _DDS_IMAGE_PIXELFORMAT_H