Blender  V2.93
PixelFormat.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 /*
22  * This file is based on a similar file from the NVIDIA texture tools
23  * (http://nvidia-texture-tools.googlecode.com/)
24  *
25  * Original license from NVIDIA follows.
26  */
27 
28 /* Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
29  *
30  * Permission is hereby granted, free of charge, to any person
31  * obtaining a copy of this software and associated documentation
32  * files (the "Software"), to deal in the Software without
33  * restriction, including without limitation the rights to use,
34  * copy, modify, merge, publish, distribute, sublicense, and/or sell
35  * copies of the Software, and to permit persons to whom the
36  * Software is furnished to do so, subject to the following
37  * conditions:
38  *
39  * The above copyright notice and this permission notice shall be
40  * included in all copies or substantial portions of the Software.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
43  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
44  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
45  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
46  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
47  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
48  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
49  * OTHER DEALINGS IN THE SOFTWARE.
50  */
51 
52 #pragma once
53 
54 #include "Common.h"
55 
56 namespace PixelFormat {
57 
59 inline uint convert(uint c, uint inbits, uint outbits)
60 {
61  if (inbits == 0) {
62  return 0;
63  }
64  else if (inbits >= outbits) {
65  /* truncate */
66  return c >> (inbits - outbits);
67  }
68  else {
69  /* bitexpand */
70  return (c << (outbits - inbits)) | convert(c, inbits, outbits - inbits);
71  }
72 }
73 
74 /* Get pixel component shift and size given its mask. */
75 inline void maskShiftAndSize(uint mask, uint *shift, uint *size)
76 {
77  if (!mask) {
78  *shift = 0;
79  *size = 0;
80  return;
81  }
82 
83  *shift = 0;
84  while ((mask & 1) == 0) {
85  ++(*shift);
86  mask >>= 1;
87  }
88 
89  *size = 0;
90  while ((mask & 1) == 1) {
91  ++(*size);
92  mask >>= 1;
93  }
94 }
95 
96 inline float quantizeCeil(float f, int inbits, int outbits)
97 {
98 #if 0
99  uint i = f * (float(1 << inbits) - 1);
100  i = convert(i, inbits, outbits);
101  float result = float(i) / (float(1 << outbits) - 1);
102  nvCheck(result >= f);
103 #endif
104  float result;
105 
106  int offset = 0;
107  do {
108  uint i = offset + uint(f * (float(1 << inbits) - 1));
109  i = convert(i, inbits, outbits);
110  result = float(i) / (float(1 << outbits) - 1);
111  offset++;
112  } while (result < f);
113 
114  return result;
115 }
116 
117 #if 0
118 inline float quantizeRound(float f, int bits)
119 {
120  float scale = float(1 << bits);
121  return fround(f * scale) / scale;
122 }
123 
124 inline float quantizeFloor(float f, int bits)
125 {
126  float scale = float(1 << bits);
127  return floor(f * scale) / scale;
128 }
129 #endif
130 
131 } /* namespace PixelFormat */
typedef float(TangentPoint)[2]
unsigned int uint
Definition: BLI_sys_types.h:83
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
static unsigned c
Definition: RandGen.cpp:97
uint convert(uint c, uint inbits, uint outbits)
Definition: PixelFormat.h:59
float quantizeCeil(float f, int inbits, int outbits)
Definition: PixelFormat.h:96
void maskShiftAndSize(uint mask, uint *shift, uint *size)
Definition: PixelFormat.h:75
ccl_device_inline float2 floor(const float2 &a)
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)