Blender  V3.3
PixelFormat.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 /*
8  * This file is based on a similar file from the NVIDIA texture tools
9  * (http://nvidia-texture-tools.googlecode.com/)
10  *
11  * Original license from NVIDIA follows.
12  */
13 
14 /* Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
15  *
16  * Permission is hereby granted, free of charge, to any person
17  * obtaining a copy of this software and associated documentation
18  * files (the "Software"), to deal in the Software without
19  * restriction, including without limitation the rights to use,
20  * copy, modify, merge, publish, distribute, sublicense, and/or sell
21  * copies of the Software, and to permit persons to whom the
22  * Software is furnished to do so, subject to the following
23  * conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  * OTHER DEALINGS IN THE SOFTWARE.
36  */
37 
38 #pragma once
39 
40 #include "Common.h"
41 
42 namespace PixelFormat {
43 
45 inline uint convert(uint c, uint inbits, uint outbits)
46 {
47  if (inbits == 0) {
48  return 0;
49  }
50  else if (inbits >= outbits) {
51  /* truncate */
52  return c >> (inbits - outbits);
53  }
54  else {
55  /* bitexpand */
56  return (c << (outbits - inbits)) | convert(c, inbits, outbits - inbits);
57  }
58 }
59 
60 /* Get pixel component shift and size given its mask. */
61 inline void maskShiftAndSize(uint mask, uint *shift, uint *size)
62 {
63  if (!mask) {
64  *shift = 0;
65  *size = 0;
66  return;
67  }
68 
69  *shift = 0;
70  while ((mask & 1) == 0) {
71  ++(*shift);
72  mask >>= 1;
73  }
74 
75  *size = 0;
76  while ((mask & 1) == 1) {
77  ++(*size);
78  mask >>= 1;
79  }
80 }
81 
82 inline float quantizeCeil(float f, int inbits, int outbits)
83 {
84 #if 0
85  uint i = f * (float(1 << inbits) - 1);
86  i = convert(i, inbits, outbits);
87  float result = float(i) / (float(1 << outbits) - 1);
88  nvCheck(result >= f);
89 #endif
90  float result;
91 
92  int offset = 0;
93  do {
94  uint i = offset + uint(f * (float(1 << inbits) - 1));
95  i = convert(i, inbits, outbits);
96  result = float(i) / (float(1 << outbits) - 1);
97  offset++;
98  } while (result < f);
99 
100  return result;
101 }
102 
103 #if 0
104 inline float quantizeRound(float f, int bits)
105 {
106  float scale = float(1 << bits);
107  return fround(f * scale) / scale;
108 }
109 
110 inline float quantizeFloor(float f, int bits)
111 {
112  float scale = float(1 << bits);
113  return floor(f * scale) / scale;
114 }
115 #endif
116 
117 } /* namespace PixelFormat */
typedef float(TangentPoint)[2]
unsigned int uint
Definition: BLI_sys_types.h:67
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
static unsigned c
Definition: RandGen.cpp:83
uint convert(uint c, uint inbits, uint outbits)
Definition: PixelFormat.h:45
float quantizeCeil(float f, int inbits, int outbits)
Definition: PixelFormat.h:82
void maskShiftAndSize(uint mask, uint *shift, uint *size)
Definition: PixelFormat.h:61
T floor(const T &a)