Blender  V2.59
PixelFormat.h
Go to the documentation of this file.
00001 /*
00002  * $Id: PixelFormat.h 36546 2011-05-08 09:05:52Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * Contributors: Amorilia (amorilia@users.sourceforge.net)
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00030 /*
00031  * This file is based on a similar file from the NVIDIA texture tools
00032  * (http://nvidia-texture-tools.googlecode.com/)
00033  *
00034  * Original license from NVIDIA follows.
00035  */
00036 
00037 // Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
00038 // 
00039 // Permission is hereby granted, free of charge, to any person
00040 // obtaining a copy of this software and associated documentation
00041 // files (the "Software"), to deal in the Software without
00042 // restriction, including without limitation the rights to use,
00043 // copy, modify, merge, publish, distribute, sublicense, and/or sell
00044 // copies of the Software, and to permit persons to whom the
00045 // Software is furnished to do so, subject to the following
00046 // conditions:
00047 // 
00048 // The above copyright notice and this permission notice shall be
00049 // included in all copies or substantial portions of the Software.
00050 // 
00051 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00052 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00053 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00054 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00055 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00056 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00057 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00058 // OTHER DEALINGS IN THE SOFTWARE.
00059 
00060 #ifndef _DDS_PIXELFORMAT_H
00061 #define _DDS_PIXELFORMAT_H
00062 
00063 #include <Common.h>
00064 
00065         namespace PixelFormat
00066         {
00067 
00068                 // Convert component @a c having @a inbits to the returned value having @a outbits.
00069                 inline uint convert(uint c, uint inbits, uint outbits)
00070                 {
00071                         if (inbits == 0)
00072                         {
00073                                 return 0;
00074                         }
00075                         else if (inbits >= outbits)
00076                         {
00077                                 // truncate
00078                                 return c >> (inbits - outbits);
00079                         }
00080                         else
00081                         {
00082                                 // bitexpand
00083                                 return (c << (outbits - inbits)) | convert(c, inbits, outbits - inbits);
00084                         }
00085                 }
00086 
00087                 // Get pixel component shift and size given its mask.
00088                 inline void maskShiftAndSize(uint mask, uint * shift, uint * size)
00089                 {
00090                         if (!mask)
00091                         {
00092                                 *shift = 0;
00093                                 *size = 0;
00094                                 return;
00095                         }
00096 
00097                         *shift = 0;
00098                         while((mask & 1) == 0) {
00099                                 ++(*shift);
00100                                 mask >>= 1;
00101                         }
00102                         
00103                         *size = 0;
00104                         while((mask & 1) == 1) {
00105                                 ++(*size);
00106                                 mask >>= 1;
00107                         }
00108                 }
00109 
00110         inline float quantizeCeil(float f, int inbits, int outbits)
00111         {
00112             //uint i = f * (float(1 << inbits) - 1);
00113             //i = convert(i, inbits, outbits);
00114             //float result = float(i) / (float(1 << outbits) - 1);
00115             //nvCheck(result >= f);
00116             float result;
00117             int offset = 0;
00118             do {
00119                 uint i = offset + uint(f * (float(1 << inbits) - 1));
00120                 i = convert(i, inbits, outbits);
00121                 result = float(i) / (float(1 << outbits) - 1);
00122                 offset++;
00123             } while (result < f);
00124 
00125             return result;
00126         }
00127 
00128         /*
00129         inline float quantizeRound(float f, int bits)
00130         {
00131             float scale = float(1 << bits);
00132             return fround(f * scale) / scale;
00133         }
00134 
00135         inline float quantizeFloor(float f, int bits)
00136         {
00137             float scale = float(1 << bits);
00138             return floor(f * scale) / scale;
00139         }
00140         */
00141 
00142         } // PixelFormat namespace
00143 
00144 #endif // _DDS_IMAGE_PIXELFORMAT_H