-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcro_mipmap.h
336 lines (290 loc) · 10 KB
/
cro_mipmap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
CRO Libaries -- Mipmap Generation -- public domain
no warranty is offered or implied; use this code at your own risk
C code for created various types of mipmaps for textures.
Pixel types supported: 8 bit 4 channels (i.e. 32-bit such as RGBA, ABGR, etc)
32-bit floating point pixels (ie depth textures)
Down sampling functions: Average (traditional mipmap generation)
Minimum (for depth values)
Maximum (for depth values)
Minimum and maximum (done at the same time to
avoid redundant work of
deparate calls)
=========================================================================
LICENSE
This software is in the public domain. Where that dedication is not
recognized, you are granted a perpetual, irrevocable license to copy,
distribute, and modify this file as you see fit.
=========================================================================
Credits:
Written by Cody Olivier
*/
#ifndef cro_mipmap_h
#define cro_mipmap_h
#ifdef CRO_Min
#undef CRO_Min;
#endif
#ifdef CRO_Max
#undef CRO_Max;
#endif
#define CRO_Max( x, y ) ( (x) > (y) ? (x) : (y) )
#define CRO_Min( x, y ) ( (x) < (y) ? (x) : (y) )
static inline void cro_GetMipMapSize( unsigned int width, unsigned int height,
unsigned int *newWidth, unsigned int *newHeight)
{
*newWidth = width >> 1;
*newHeight = height >> 1;
}
//
//----------------------------------------------------
//
// Integer MipMaps
// -These functions assume that each pixel is made up of 4 channels, each 1 byte.
// -Odd image dimensions will be rounded down when down sampled.
// -Odd width will ignore last column, odd height will ignore last row
// -These functions will not allocate memory. If you are unsure how big the
// generated mip map will be, use cro_GetMipMapSize.
//
// Main Use: 32-bit color images
//
//----------------------------------------------------
//
static inline int cro_AvgBits( int n1, int n2, int n3, int n4, int bitShifts )
{
int byte1 = (n1 >> bitShifts) & 0xff;
int byte2 = (n2 >> bitShifts) & 0xff;
int byte3 = (n3 >> bitShifts) & 0xff;
int byte4 = (n4 >> bitShifts) & 0xff;
return ( byte1 + byte2 + byte3 + byte4 ) >> 2;
}
static inline int cro_MaxBits( int n1, int n2, int n3, int n4, int bitShifts )
{
int byte1 = (n1 >> bitShifts) & 0xff;
int byte2 = (n2 >> bitShifts) & 0xff;
int byte3 = (n3 >> bitShifts) & 0xff;
int byte4 = (n4 >> bitShifts) & 0xff;
return CRO_Max( CRO_Max( byte1, byte2 ), CRO_Max( byte3, byte4 ) );
}
static inline int cro_MinBits( int n1, int n2, int n3, int n4, int bitShifts )
{
int byte1 = (n1 >> bitShifts) & 0xff;
int byte2 = (n2 >> bitShifts) & 0xff;
int byte3 = (n3 >> bitShifts) & 0xff;
int byte4 = (n4 >> bitShifts) & 0xff;
return CRO_Min( CRO_Min( byte1, byte2 ), CRO_Min( byte3, byte4 ) );
}
static inline void cro_MinMaxBits( int n1, int n2, int n3, int n4, int bitShifts, int *min, int *max )
{
int byte1 = (n1 >> bitShifts) & 0xff;
int byte2 = (n2 >> bitShifts) & 0xff;
int byte3 = (n3 >> bitShifts) & 0xff;
int byte4 = (n4 >> bitShifts) & 0xff;
int max1, max2, min1, min2;
if ( byte1 > byte2 )
{
max1 = byte1;
min1 = byte2;
}
else
{
max1 = byte2;
min1 = byte1;
}
if ( byte3 > byte4 )
{
max2 = byte3;
min2 = byte4;
}
else
{
max2 = byte4;
min2 = byte3;
}
*min = CRO_Min( min1, min2 );
*max = CRO_Max( max1, max2 );
}
static inline int cro_GetMipMapLevels( unsigned int width, unsigned int height )
{
int levels = 0;
while ( width > 1 && height > 1 )
{
width >>= 1;
height >>= 1;
levels++;
}
return levels;
}
#define CRO_GenMipMapI_Template( name, fn ) \
static inline void cro_GenMipMap##name##I( const int *image, unsigned int width, unsigned int height, int *newImage ) \
{ \
if ( width == 0 || height == 0 ) \
return; \
\
unsigned int newWidth, newHeight; \
int *newPixel = newImage; \
cro_GetMipMapSize( width, height, &newWidth, &newHeight ); \
\
for ( int h = 0; h < newHeight; ++h ) \
{ \
const int *row1 = &image[ width * ( 2 * h + 0 ) ]; \
const int *row2 = &image[ width * ( 2 * h + 1 ) ]; \
for ( int w = 0; w < newWidth; ++w, ++newPixel, row1 += 2, row2 += 2 ) \
{ \
int p1 = row1[0]; \
int p2 = row2[0]; \
int p3 = row1[1]; \
int p4 = row2[1]; \
\
int byte1 = fn( p1, p2, p3, p4, 24 ); \
int byte2 = fn( p1, p2, p3, p4, 16 ); \
int byte3 = fn( p1, p2, p3, p4, 8 ); \
int byte4 = fn( p1, p2, p3, p4, 0 ); \
\
*newPixel = ( byte1 << 24 ) | \
( byte2 << 16 ) | \
( byte3 << 8 ) | \
( byte4 << 0 ); \
} \
} \
}
// template macros for: cro_GenMipMapAvgI
// cro_GenMipMapMinI
// cro_GenMipMapMaxI
CRO_GenMipMapI_Template( Avg, cro_AvgBits );
CRO_GenMipMapI_Template( Min, cro_MinBits );
CRO_GenMipMapI_Template( Max, cro_MaxBits );
static inline void cro_GenMipMapMinMaxI( const int *image, unsigned int width, unsigned int height, int *minMip, int *maxMip )
{
if ( width == 0 || height == 0 )
return;
unsigned int newWidth, newHeight;
int *minPixel = minMip;
int *maxPixel = maxMip;
cro_GetMipMapSize( width, height, &newWidth, &newHeight );
for ( int h = 0; h < newHeight; ++h )
{
const int *row1 = &image[ width * ( 2 * h + 0 ) ];
const int *row2 = &image[ width * ( 2 * h + 1 ) ];
for ( int w = 0; w < newWidth; ++w, ++minPixel, ++maxPixel, row1 += 2, row2 += 2 )
{
int p1 = row1[0];
int p2 = row2[0];
int p3 = row1[1];
int p4 = row2[1];
int max1, max2, max3, max4;
int min1, min2, min3, min4;
cro_MinMaxBits( p1, p2, p3, p4, 24, &min1, &max1 );
cro_MinMaxBits( p1, p2, p3, p4, 16, &min2, &max2 );
cro_MinMaxBits( p1, p2, p3, p4, 8, &min3, &max3 );
cro_MinMaxBits( p1, p2, p3, p4, 0, &min4, &max4 );
*minMip = ( min1 << 24 ) | ( min2 << 16 ) | ( min3 << 8 ) | ( min4 << 0 );
*maxMip = ( max1 << 24 ) | ( max2 << 16 ) | ( max3 << 8 ) | ( max4 << 0 );
}
}
}
//
//----------------------------------------------------
//
// Float MipMaps
// -Odd image dimensions will be rounded down when down sampled.
// -Odd width will ignore last column, odd height will ignore last row
// -These functions will not allocate memory. If you are unsure how big the
// generated mip map will be, use cro_GetMipMapSize.
//
// Main Use: determining min/max values of a depth texture.
// Avg varient is added for completeness
//
//----------------------------------------------------
//
static inline float cro_AvgF( float n1, float n2, float n3, float n4 )
{
//NOTE: doesn't take into account if sum overflows before being multiplied
return ( n1 + n2 + n3 + n4 ) * 0.25f;
}
static inline float cro_MinF( float n1, float n2, float n3, float n4 )
{
return CRO_Min( CRO_Min( n1, n2 ), CRO_Min( n3, n4 ));
}
static inline float cro_MaxF( float n1, float n2, float n3, float n4 )
{
return CRO_Max( CRO_Max( n1, n2 ), CRO_Max( n3, n4 ));
}
static inline void cro_MinMaxF( float n1, float n2, float n3, float n4, float *min, float *max )
{
float max1, max2, min1, min2;
if ( n1 > n2 )
{
max1 = n1;
min1 = n2;
}
else
{
max1 = n2;
min1 = n1;
}
if ( n3 > n4 )
{
max2 = n3;
min2 = n4;
}
else
{
max2 = n4;
min2 = n3;
}
*min = CRO_Min( min1, min2 );
*max = CRO_Max( max1, max2 );
}
#define CRO_GenMipMapF_Template( name, fn ) \
static inline void cro_GenMipMap##name##F( const float *image, unsigned int width, unsigned int height, float *newImage ) \
{ \
if ( width == 0 || height == 0 ) \
return; \
\
unsigned int newWidth, newHeight; \
float *newPixel = newImage; \
cro_GetMipMapSize( width, height, &newWidth, &newHeight ); \
\
for ( int h = 0; h < newHeight; ++h ) \
{ \
const float *row1 = &image[ width * ( 2 * h + 0 ) ]; \
const float *row2 = &image[ width * ( 2 * h + 1 ) ]; \
for ( int w = 0; w < newWidth; ++w, ++newPixel, row1 += 2, row2 += 2 ) \
{ \
float p1 = row1[0]; \
float p2 = row2[0]; \
float p3 = row1[1]; \
float p4 = row2[1]; \
*newPixel = fn( p1, p2, p3, p4 ); \
} \
} \
}
// template macros for: cro_GenMipMapAvgF
// cro_GenMipMapMinF
// cro_GenMipMapMaxF
CRO_GenMipMapF_Template( Avg, cro_AvgF );
CRO_GenMipMapF_Template( Min, cro_MinF );
CRO_GenMipMapF_Template( Max, cro_MaxF );
static inline void cro_GenMipMapMinMaxF( const float *image, unsigned int width, unsigned int height, float *minMip, float *maxMip )
{
if ( width == 0 || height == 0 )
return;
unsigned int newWidth, newHeight;
float *minPixel = minMip;
float *maxPixel = maxMip;
cro_GetMipMapSize( width, height, &newWidth, &newHeight );
for ( int h = 0; h < newHeight; ++h )
{
const float *row1 = &image[ width * ( 2 * h + 0 ) ];
const float *row2 = &image[ width * ( 2 * h + 1 ) ];
for ( int w = 0; w < newWidth; ++w, ++minPixel, ++maxPixel, row1 += 2, row2 += 2 )
{
int p1 = row1[0];
int p2 = row2[0];
int p3 = row1[1];
int p4 = row2[1];
cro_MinMaxF( p1, p2, p3, p4, minPixel, maxPixel );
}
}
}
#endif