Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / mesa / main / mipmap.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file mipmap.c mipmap generation and teximage resizing functions.
28 */
29
30 #include "imports.h"
31 #include "formats.h"
32 #include "mipmap.h"
33 #include "texcompress.h"
34 #include "teximage.h"
35 #include "texstore.h"
36 #include "image.h"
37
38
39
40 static GLint
41 bytes_per_pixel(GLenum datatype, GLuint comps)
42 {
43 GLint b = _mesa_sizeof_packed_type(datatype);
44 assert(b >= 0);
45
46 if (_mesa_type_is_packed(datatype))
47 return b;
48 else
49 return b * comps;
50 }
51
52
53 /**
54 * \name Support macros for do_row and do_row_3d
55 *
56 * The macro madness is here for two reasons. First, it compacts the code
57 * slightly. Second, it makes it much easier to adjust the specifics of the
58 * filter to tune the rounding characteristics.
59 */
60 /*@{*/
61 #define DECLARE_ROW_POINTERS(t, e) \
62 const t(*rowA)[e] = (const t(*)[e]) srcRowA; \
63 const t(*rowB)[e] = (const t(*)[e]) srcRowB; \
64 const t(*rowC)[e] = (const t(*)[e]) srcRowC; \
65 const t(*rowD)[e] = (const t(*)[e]) srcRowD; \
66 t(*dst)[e] = (t(*)[e]) dstRow
67
68 #define DECLARE_ROW_POINTERS0(t) \
69 const t *rowA = (const t *) srcRowA; \
70 const t *rowB = (const t *) srcRowB; \
71 const t *rowC = (const t *) srcRowC; \
72 const t *rowD = (const t *) srcRowD; \
73 t *dst = (t *) dstRow
74
75 #define FILTER_SUM_3D(Aj, Ak, Bj, Bk, Cj, Ck, Dj, Dk) \
76 ((unsigned) Aj + (unsigned) Ak \
77 + (unsigned) Bj + (unsigned) Bk \
78 + (unsigned) Cj + (unsigned) Ck \
79 + (unsigned) Dj + (unsigned) Dk \
80 + 4) >> 3
81
82 #define FILTER_3D(e) \
83 do { \
84 dst[i][e] = FILTER_SUM_3D(rowA[j][e], rowA[k][e], \
85 rowB[j][e], rowB[k][e], \
86 rowC[j][e], rowC[k][e], \
87 rowD[j][e], rowD[k][e]); \
88 } while(0)
89
90 #define FILTER_SUM_3D_SIGNED(Aj, Ak, Bj, Bk, Cj, Ck, Dj, Dk) \
91 (Aj + Ak \
92 + Bj + Bk \
93 + Cj + Ck \
94 + Dj + Dk \
95 + 4) / 8
96
97 #define FILTER_3D_SIGNED(e) \
98 do { \
99 dst[i][e] = FILTER_SUM_3D_SIGNED(rowA[j][e], rowA[k][e], \
100 rowB[j][e], rowB[k][e], \
101 rowC[j][e], rowC[k][e], \
102 rowD[j][e], rowD[k][e]); \
103 } while(0)
104
105 #define FILTER_F_3D(e) \
106 do { \
107 dst[i][e] = (rowA[j][e] + rowA[k][e] \
108 + rowB[j][e] + rowB[k][e] \
109 + rowC[j][e] + rowC[k][e] \
110 + rowD[j][e] + rowD[k][e]) * 0.125F; \
111 } while(0)
112
113 #define FILTER_HF_3D(e) \
114 do { \
115 const GLfloat aj = _mesa_half_to_float(rowA[j][e]); \
116 const GLfloat ak = _mesa_half_to_float(rowA[k][e]); \
117 const GLfloat bj = _mesa_half_to_float(rowB[j][e]); \
118 const GLfloat bk = _mesa_half_to_float(rowB[k][e]); \
119 const GLfloat cj = _mesa_half_to_float(rowC[j][e]); \
120 const GLfloat ck = _mesa_half_to_float(rowC[k][e]); \
121 const GLfloat dj = _mesa_half_to_float(rowD[j][e]); \
122 const GLfloat dk = _mesa_half_to_float(rowD[k][e]); \
123 dst[i][e] = _mesa_float_to_half((aj + ak + bj + bk + cj + ck + dj + dk) \
124 * 0.125F); \
125 } while(0)
126 /*@}*/
127
128
129 /**
130 * Average together two rows of a source image to produce a single new
131 * row in the dest image. It's legal for the two source rows to point
132 * to the same data. The source width must be equal to either the
133 * dest width or two times the dest width.
134 * \param datatype GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, GL_FLOAT, etc.
135 * \param comps number of components per pixel (1..4)
136 */
137 static void
138 do_row(GLenum datatype, GLuint comps, GLint srcWidth,
139 const GLvoid *srcRowA, const GLvoid *srcRowB,
140 GLint dstWidth, GLvoid *dstRow)
141 {
142 const GLuint k0 = (srcWidth == dstWidth) ? 0 : 1;
143 const GLuint colStride = (srcWidth == dstWidth) ? 1 : 2;
144
145 ASSERT(comps >= 1);
146 ASSERT(comps <= 4);
147
148 /* This assertion is no longer valid with non-power-of-2 textures
149 assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth);
150 */
151
152 if (datatype == GL_UNSIGNED_BYTE && comps == 4) {
153 GLuint i, j, k;
154 const GLubyte(*rowA)[4] = (const GLubyte(*)[4]) srcRowA;
155 const GLubyte(*rowB)[4] = (const GLubyte(*)[4]) srcRowB;
156 GLubyte(*dst)[4] = (GLubyte(*)[4]) dstRow;
157 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
158 i++, j += colStride, k += colStride) {
159 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
160 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
161 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
162 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
163 }
164 }
165 else if (datatype == GL_UNSIGNED_BYTE && comps == 3) {
166 GLuint i, j, k;
167 const GLubyte(*rowA)[3] = (const GLubyte(*)[3]) srcRowA;
168 const GLubyte(*rowB)[3] = (const GLubyte(*)[3]) srcRowB;
169 GLubyte(*dst)[3] = (GLubyte(*)[3]) dstRow;
170 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
171 i++, j += colStride, k += colStride) {
172 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
173 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
174 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
175 }
176 }
177 else if (datatype == GL_UNSIGNED_BYTE && comps == 2) {
178 GLuint i, j, k;
179 const GLubyte(*rowA)[2] = (const GLubyte(*)[2]) srcRowA;
180 const GLubyte(*rowB)[2] = (const GLubyte(*)[2]) srcRowB;
181 GLubyte(*dst)[2] = (GLubyte(*)[2]) dstRow;
182 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
183 i++, j += colStride, k += colStride) {
184 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) >> 2;
185 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) >> 2;
186 }
187 }
188 else if (datatype == GL_UNSIGNED_BYTE && comps == 1) {
189 GLuint i, j, k;
190 const GLubyte *rowA = (const GLubyte *) srcRowA;
191 const GLubyte *rowB = (const GLubyte *) srcRowB;
192 GLubyte *dst = (GLubyte *) dstRow;
193 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
194 i++, j += colStride, k += colStride) {
195 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) >> 2;
196 }
197 }
198
199 else if (datatype == GL_BYTE && comps == 4) {
200 GLuint i, j, k;
201 const GLbyte(*rowA)[4] = (const GLbyte(*)[4]) srcRowA;
202 const GLbyte(*rowB)[4] = (const GLbyte(*)[4]) srcRowB;
203 GLbyte(*dst)[4] = (GLbyte(*)[4]) dstRow;
204 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
205 i++, j += colStride, k += colStride) {
206 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
207 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
208 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
209 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
210 }
211 }
212 else if (datatype == GL_BYTE && comps == 3) {
213 GLuint i, j, k;
214 const GLbyte(*rowA)[3] = (const GLbyte(*)[3]) srcRowA;
215 const GLbyte(*rowB)[3] = (const GLbyte(*)[3]) srcRowB;
216 GLbyte(*dst)[3] = (GLbyte(*)[3]) dstRow;
217 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
218 i++, j += colStride, k += colStride) {
219 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
220 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
221 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
222 }
223 }
224 else if (datatype == GL_BYTE && comps == 2) {
225 GLuint i, j, k;
226 const GLbyte(*rowA)[2] = (const GLbyte(*)[2]) srcRowA;
227 const GLbyte(*rowB)[2] = (const GLbyte(*)[2]) srcRowB;
228 GLbyte(*dst)[2] = (GLbyte(*)[2]) dstRow;
229 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
230 i++, j += colStride, k += colStride) {
231 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
232 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
233 }
234 }
235 else if (datatype == GL_BYTE && comps == 1) {
236 GLuint i, j, k;
237 const GLbyte *rowA = (const GLbyte *) srcRowA;
238 const GLbyte *rowB = (const GLbyte *) srcRowB;
239 GLbyte *dst = (GLbyte *) dstRow;
240 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
241 i++, j += colStride, k += colStride) {
242 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) / 4;
243 }
244 }
245
246 else if (datatype == GL_UNSIGNED_SHORT && comps == 4) {
247 GLuint i, j, k;
248 const GLushort(*rowA)[4] = (const GLushort(*)[4]) srcRowA;
249 const GLushort(*rowB)[4] = (const GLushort(*)[4]) srcRowB;
250 GLushort(*dst)[4] = (GLushort(*)[4]) dstRow;
251 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
252 i++, j += colStride, k += colStride) {
253 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
254 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
255 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
256 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
257 }
258 }
259 else if (datatype == GL_UNSIGNED_SHORT && comps == 3) {
260 GLuint i, j, k;
261 const GLushort(*rowA)[3] = (const GLushort(*)[3]) srcRowA;
262 const GLushort(*rowB)[3] = (const GLushort(*)[3]) srcRowB;
263 GLushort(*dst)[3] = (GLushort(*)[3]) dstRow;
264 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
265 i++, j += colStride, k += colStride) {
266 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
267 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
268 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
269 }
270 }
271 else if (datatype == GL_UNSIGNED_SHORT && comps == 2) {
272 GLuint i, j, k;
273 const GLushort(*rowA)[2] = (const GLushort(*)[2]) srcRowA;
274 const GLushort(*rowB)[2] = (const GLushort(*)[2]) srcRowB;
275 GLushort(*dst)[2] = (GLushort(*)[2]) dstRow;
276 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
277 i++, j += colStride, k += colStride) {
278 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
279 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
280 }
281 }
282 else if (datatype == GL_UNSIGNED_SHORT && comps == 1) {
283 GLuint i, j, k;
284 const GLushort *rowA = (const GLushort *) srcRowA;
285 const GLushort *rowB = (const GLushort *) srcRowB;
286 GLushort *dst = (GLushort *) dstRow;
287 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
288 i++, j += colStride, k += colStride) {
289 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) / 4;
290 }
291 }
292 else if (datatype == GL_FLOAT && comps == 4) {
293 GLuint i, j, k;
294 const GLfloat(*rowA)[4] = (const GLfloat(*)[4]) srcRowA;
295 const GLfloat(*rowB)[4] = (const GLfloat(*)[4]) srcRowB;
296 GLfloat(*dst)[4] = (GLfloat(*)[4]) dstRow;
297 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
298 i++, j += colStride, k += colStride) {
299 dst[i][0] = (rowA[j][0] + rowA[k][0] +
300 rowB[j][0] + rowB[k][0]) * 0.25F;
301 dst[i][1] = (rowA[j][1] + rowA[k][1] +
302 rowB[j][1] + rowB[k][1]) * 0.25F;
303 dst[i][2] = (rowA[j][2] + rowA[k][2] +
304 rowB[j][2] + rowB[k][2]) * 0.25F;
305 dst[i][3] = (rowA[j][3] + rowA[k][3] +
306 rowB[j][3] + rowB[k][3]) * 0.25F;
307 }
308 }
309 else if (datatype == GL_FLOAT && comps == 3) {
310 GLuint i, j, k;
311 const GLfloat(*rowA)[3] = (const GLfloat(*)[3]) srcRowA;
312 const GLfloat(*rowB)[3] = (const GLfloat(*)[3]) srcRowB;
313 GLfloat(*dst)[3] = (GLfloat(*)[3]) dstRow;
314 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
315 i++, j += colStride, k += colStride) {
316 dst[i][0] = (rowA[j][0] + rowA[k][0] +
317 rowB[j][0] + rowB[k][0]) * 0.25F;
318 dst[i][1] = (rowA[j][1] + rowA[k][1] +
319 rowB[j][1] + rowB[k][1]) * 0.25F;
320 dst[i][2] = (rowA[j][2] + rowA[k][2] +
321 rowB[j][2] + rowB[k][2]) * 0.25F;
322 }
323 }
324 else if (datatype == GL_FLOAT && comps == 2) {
325 GLuint i, j, k;
326 const GLfloat(*rowA)[2] = (const GLfloat(*)[2]) srcRowA;
327 const GLfloat(*rowB)[2] = (const GLfloat(*)[2]) srcRowB;
328 GLfloat(*dst)[2] = (GLfloat(*)[2]) dstRow;
329 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
330 i++, j += colStride, k += colStride) {
331 dst[i][0] = (rowA[j][0] + rowA[k][0] +
332 rowB[j][0] + rowB[k][0]) * 0.25F;
333 dst[i][1] = (rowA[j][1] + rowA[k][1] +
334 rowB[j][1] + rowB[k][1]) * 0.25F;
335 }
336 }
337 else if (datatype == GL_FLOAT && comps == 1) {
338 GLuint i, j, k;
339 const GLfloat *rowA = (const GLfloat *) srcRowA;
340 const GLfloat *rowB = (const GLfloat *) srcRowB;
341 GLfloat *dst = (GLfloat *) dstRow;
342 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
343 i++, j += colStride, k += colStride) {
344 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) * 0.25F;
345 }
346 }
347
348 else if (datatype == GL_HALF_FLOAT_ARB && comps == 4) {
349 GLuint i, j, k, comp;
350 const GLhalfARB(*rowA)[4] = (const GLhalfARB(*)[4]) srcRowA;
351 const GLhalfARB(*rowB)[4] = (const GLhalfARB(*)[4]) srcRowB;
352 GLhalfARB(*dst)[4] = (GLhalfARB(*)[4]) dstRow;
353 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
354 i++, j += colStride, k += colStride) {
355 for (comp = 0; comp < 4; comp++) {
356 GLfloat aj, ak, bj, bk;
357 aj = _mesa_half_to_float(rowA[j][comp]);
358 ak = _mesa_half_to_float(rowA[k][comp]);
359 bj = _mesa_half_to_float(rowB[j][comp]);
360 bk = _mesa_half_to_float(rowB[k][comp]);
361 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
362 }
363 }
364 }
365 else if (datatype == GL_HALF_FLOAT_ARB && comps == 3) {
366 GLuint i, j, k, comp;
367 const GLhalfARB(*rowA)[3] = (const GLhalfARB(*)[3]) srcRowA;
368 const GLhalfARB(*rowB)[3] = (const GLhalfARB(*)[3]) srcRowB;
369 GLhalfARB(*dst)[3] = (GLhalfARB(*)[3]) dstRow;
370 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
371 i++, j += colStride, k += colStride) {
372 for (comp = 0; comp < 3; comp++) {
373 GLfloat aj, ak, bj, bk;
374 aj = _mesa_half_to_float(rowA[j][comp]);
375 ak = _mesa_half_to_float(rowA[k][comp]);
376 bj = _mesa_half_to_float(rowB[j][comp]);
377 bk = _mesa_half_to_float(rowB[k][comp]);
378 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
379 }
380 }
381 }
382 else if (datatype == GL_HALF_FLOAT_ARB && comps == 2) {
383 GLuint i, j, k, comp;
384 const GLhalfARB(*rowA)[2] = (const GLhalfARB(*)[2]) srcRowA;
385 const GLhalfARB(*rowB)[2] = (const GLhalfARB(*)[2]) srcRowB;
386 GLhalfARB(*dst)[2] = (GLhalfARB(*)[2]) dstRow;
387 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
388 i++, j += colStride, k += colStride) {
389 for (comp = 0; comp < 2; comp++) {
390 GLfloat aj, ak, bj, bk;
391 aj = _mesa_half_to_float(rowA[j][comp]);
392 ak = _mesa_half_to_float(rowA[k][comp]);
393 bj = _mesa_half_to_float(rowB[j][comp]);
394 bk = _mesa_half_to_float(rowB[k][comp]);
395 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
396 }
397 }
398 }
399 else if (datatype == GL_HALF_FLOAT_ARB && comps == 1) {
400 GLuint i, j, k;
401 const GLhalfARB *rowA = (const GLhalfARB *) srcRowA;
402 const GLhalfARB *rowB = (const GLhalfARB *) srcRowB;
403 GLhalfARB *dst = (GLhalfARB *) dstRow;
404 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
405 i++, j += colStride, k += colStride) {
406 GLfloat aj, ak, bj, bk;
407 aj = _mesa_half_to_float(rowA[j]);
408 ak = _mesa_half_to_float(rowA[k]);
409 bj = _mesa_half_to_float(rowB[j]);
410 bk = _mesa_half_to_float(rowB[k]);
411 dst[i] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
412 }
413 }
414
415 else if (datatype == GL_UNSIGNED_INT && comps == 1) {
416 GLuint i, j, k;
417 const GLuint *rowA = (const GLuint *) srcRowA;
418 const GLuint *rowB = (const GLuint *) srcRowB;
419 GLfloat *dst = (GLfloat *) dstRow;
420 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
421 i++, j += colStride, k += colStride) {
422 dst[i] = (GLfloat)(rowA[j] / 4 + rowA[k] / 4 + rowB[j] / 4 + rowB[k] / 4);
423 }
424 }
425
426 else if (datatype == GL_UNSIGNED_SHORT_5_6_5 && comps == 3) {
427 GLuint i, j, k;
428 const GLushort *rowA = (const GLushort *) srcRowA;
429 const GLushort *rowB = (const GLushort *) srcRowB;
430 GLushort *dst = (GLushort *) dstRow;
431 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
432 i++, j += colStride, k += colStride) {
433 const GLint rowAr0 = rowA[j] & 0x1f;
434 const GLint rowAr1 = rowA[k] & 0x1f;
435 const GLint rowBr0 = rowB[j] & 0x1f;
436 const GLint rowBr1 = rowB[k] & 0x1f;
437 const GLint rowAg0 = (rowA[j] >> 5) & 0x3f;
438 const GLint rowAg1 = (rowA[k] >> 5) & 0x3f;
439 const GLint rowBg0 = (rowB[j] >> 5) & 0x3f;
440 const GLint rowBg1 = (rowB[k] >> 5) & 0x3f;
441 const GLint rowAb0 = (rowA[j] >> 11) & 0x1f;
442 const GLint rowAb1 = (rowA[k] >> 11) & 0x1f;
443 const GLint rowBb0 = (rowB[j] >> 11) & 0x1f;
444 const GLint rowBb1 = (rowB[k] >> 11) & 0x1f;
445 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
446 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
447 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
448 dst[i] = (blue << 11) | (green << 5) | red;
449 }
450 }
451 else if (datatype == GL_UNSIGNED_SHORT_4_4_4_4 && comps == 4) {
452 GLuint i, j, k;
453 const GLushort *rowA = (const GLushort *) srcRowA;
454 const GLushort *rowB = (const GLushort *) srcRowB;
455 GLushort *dst = (GLushort *) dstRow;
456 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
457 i++, j += colStride, k += colStride) {
458 const GLint rowAr0 = rowA[j] & 0xf;
459 const GLint rowAr1 = rowA[k] & 0xf;
460 const GLint rowBr0 = rowB[j] & 0xf;
461 const GLint rowBr1 = rowB[k] & 0xf;
462 const GLint rowAg0 = (rowA[j] >> 4) & 0xf;
463 const GLint rowAg1 = (rowA[k] >> 4) & 0xf;
464 const GLint rowBg0 = (rowB[j] >> 4) & 0xf;
465 const GLint rowBg1 = (rowB[k] >> 4) & 0xf;
466 const GLint rowAb0 = (rowA[j] >> 8) & 0xf;
467 const GLint rowAb1 = (rowA[k] >> 8) & 0xf;
468 const GLint rowBb0 = (rowB[j] >> 8) & 0xf;
469 const GLint rowBb1 = (rowB[k] >> 8) & 0xf;
470 const GLint rowAa0 = (rowA[j] >> 12) & 0xf;
471 const GLint rowAa1 = (rowA[k] >> 12) & 0xf;
472 const GLint rowBa0 = (rowB[j] >> 12) & 0xf;
473 const GLint rowBa1 = (rowB[k] >> 12) & 0xf;
474 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
475 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
476 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
477 const GLint alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
478 dst[i] = (alpha << 12) | (blue << 8) | (green << 4) | red;
479 }
480 }
481 else if (datatype == GL_UNSIGNED_SHORT_1_5_5_5_REV && comps == 4) {
482 GLuint i, j, k;
483 const GLushort *rowA = (const GLushort *) srcRowA;
484 const GLushort *rowB = (const GLushort *) srcRowB;
485 GLushort *dst = (GLushort *) dstRow;
486 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
487 i++, j += colStride, k += colStride) {
488 const GLint rowAr0 = rowA[j] & 0x1f;
489 const GLint rowAr1 = rowA[k] & 0x1f;
490 const GLint rowBr0 = rowB[j] & 0x1f;
491 const GLint rowBr1 = rowB[k] & 0x1f;
492 const GLint rowAg0 = (rowA[j] >> 5) & 0x1f;
493 const GLint rowAg1 = (rowA[k] >> 5) & 0x1f;
494 const GLint rowBg0 = (rowB[j] >> 5) & 0x1f;
495 const GLint rowBg1 = (rowB[k] >> 5) & 0x1f;
496 const GLint rowAb0 = (rowA[j] >> 10) & 0x1f;
497 const GLint rowAb1 = (rowA[k] >> 10) & 0x1f;
498 const GLint rowBb0 = (rowB[j] >> 10) & 0x1f;
499 const GLint rowBb1 = (rowB[k] >> 10) & 0x1f;
500 const GLint rowAa0 = (rowA[j] >> 15) & 0x1;
501 const GLint rowAa1 = (rowA[k] >> 15) & 0x1;
502 const GLint rowBa0 = (rowB[j] >> 15) & 0x1;
503 const GLint rowBa1 = (rowB[k] >> 15) & 0x1;
504 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
505 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
506 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
507 const GLint alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
508 dst[i] = (alpha << 15) | (blue << 10) | (green << 5) | red;
509 }
510 }
511 else if (datatype == GL_UNSIGNED_BYTE_3_3_2 && comps == 3) {
512 GLuint i, j, k;
513 const GLubyte *rowA = (const GLubyte *) srcRowA;
514 const GLubyte *rowB = (const GLubyte *) srcRowB;
515 GLubyte *dst = (GLubyte *) dstRow;
516 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
517 i++, j += colStride, k += colStride) {
518 const GLint rowAr0 = rowA[j] & 0x3;
519 const GLint rowAr1 = rowA[k] & 0x3;
520 const GLint rowBr0 = rowB[j] & 0x3;
521 const GLint rowBr1 = rowB[k] & 0x3;
522 const GLint rowAg0 = (rowA[j] >> 2) & 0x7;
523 const GLint rowAg1 = (rowA[k] >> 2) & 0x7;
524 const GLint rowBg0 = (rowB[j] >> 2) & 0x7;
525 const GLint rowBg1 = (rowB[k] >> 2) & 0x7;
526 const GLint rowAb0 = (rowA[j] >> 5) & 0x7;
527 const GLint rowAb1 = (rowA[k] >> 5) & 0x7;
528 const GLint rowBb0 = (rowB[j] >> 5) & 0x7;
529 const GLint rowBb1 = (rowB[k] >> 5) & 0x7;
530 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
531 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
532 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
533 dst[i] = (blue << 5) | (green << 2) | red;
534 }
535 }
536 else {
537 _mesa_problem(NULL, "bad format in do_row()");
538 }
539 }
540
541
542 /**
543 * Average together four rows of a source image to produce a single new
544 * row in the dest image. It's legal for the two source rows to point
545 * to the same data. The source width must be equal to either the
546 * dest width or two times the dest width.
547 *
548 * \param datatype GL pixel type \c GL_UNSIGNED_BYTE, \c GL_UNSIGNED_SHORT,
549 * \c GL_FLOAT, etc.
550 * \param comps number of components per pixel (1..4)
551 * \param srcWidth Width of a row in the source data
552 * \param srcRowA Pointer to one of the rows of source data
553 * \param srcRowB Pointer to one of the rows of source data
554 * \param srcRowC Pointer to one of the rows of source data
555 * \param srcRowD Pointer to one of the rows of source data
556 * \param dstWidth Width of a row in the destination data
557 * \param srcRowA Pointer to the row of destination data
558 */
559 static void
560 do_row_3D(GLenum datatype, GLuint comps, GLint srcWidth,
561 const GLvoid *srcRowA, const GLvoid *srcRowB,
562 const GLvoid *srcRowC, const GLvoid *srcRowD,
563 GLint dstWidth, GLvoid *dstRow)
564 {
565 const GLuint k0 = (srcWidth == dstWidth) ? 0 : 1;
566 const GLuint colStride = (srcWidth == dstWidth) ? 1 : 2;
567 GLuint i, j, k;
568
569 ASSERT(comps >= 1);
570 ASSERT(comps <= 4);
571
572 if ((datatype == GL_UNSIGNED_BYTE) && (comps == 4)) {
573 DECLARE_ROW_POINTERS(GLubyte, 4);
574
575 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
576 i++, j += colStride, k += colStride) {
577 FILTER_3D(0);
578 FILTER_3D(1);
579 FILTER_3D(2);
580 FILTER_3D(3);
581 }
582 }
583 else if ((datatype == GL_UNSIGNED_BYTE) && (comps == 3)) {
584 DECLARE_ROW_POINTERS(GLubyte, 3);
585
586 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
587 i++, j += colStride, k += colStride) {
588 FILTER_3D(0);
589 FILTER_3D(1);
590 FILTER_3D(2);
591 }
592 }
593 else if ((datatype == GL_UNSIGNED_BYTE) && (comps == 2)) {
594 DECLARE_ROW_POINTERS(GLubyte, 2);
595
596 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
597 i++, j += colStride, k += colStride) {
598 FILTER_3D(0);
599 FILTER_3D(1);
600 }
601 }
602 else if ((datatype == GL_UNSIGNED_BYTE) && (comps == 1)) {
603 DECLARE_ROW_POINTERS(GLubyte, 1);
604
605 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
606 i++, j += colStride, k += colStride) {
607 FILTER_3D(0);
608 }
609 }
610 if ((datatype == GL_BYTE) && (comps == 4)) {
611 DECLARE_ROW_POINTERS(GLbyte, 4);
612
613 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
614 i++, j += colStride, k += colStride) {
615 FILTER_3D_SIGNED(0);
616 FILTER_3D_SIGNED(1);
617 FILTER_3D_SIGNED(2);
618 FILTER_3D_SIGNED(3);
619 }
620 }
621 else if ((datatype == GL_BYTE) && (comps == 3)) {
622 DECLARE_ROW_POINTERS(GLbyte, 3);
623
624 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
625 i++, j += colStride, k += colStride) {
626 FILTER_3D_SIGNED(0);
627 FILTER_3D_SIGNED(1);
628 FILTER_3D_SIGNED(2);
629 }
630 }
631 else if ((datatype == GL_BYTE) && (comps == 2)) {
632 DECLARE_ROW_POINTERS(GLbyte, 2);
633
634 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
635 i++, j += colStride, k += colStride) {
636 FILTER_3D_SIGNED(0);
637 FILTER_3D_SIGNED(1);
638 }
639 }
640 else if ((datatype == GL_BYTE) && (comps == 1)) {
641 DECLARE_ROW_POINTERS(GLbyte, 1);
642
643 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
644 i++, j += colStride, k += colStride) {
645 FILTER_3D_SIGNED(0);
646 }
647 }
648 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 4)) {
649 DECLARE_ROW_POINTERS(GLushort, 4);
650
651 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
652 i++, j += colStride, k += colStride) {
653 FILTER_3D(0);
654 FILTER_3D(1);
655 FILTER_3D(2);
656 FILTER_3D(3);
657 }
658 }
659 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 3)) {
660 DECLARE_ROW_POINTERS(GLushort, 3);
661
662 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
663 i++, j += colStride, k += colStride) {
664 FILTER_3D(0);
665 FILTER_3D(1);
666 FILTER_3D(2);
667 }
668 }
669 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 2)) {
670 DECLARE_ROW_POINTERS(GLushort, 2);
671
672 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
673 i++, j += colStride, k += colStride) {
674 FILTER_3D(0);
675 FILTER_3D(1);
676 }
677 }
678 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 1)) {
679 DECLARE_ROW_POINTERS(GLushort, 1);
680
681 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
682 i++, j += colStride, k += colStride) {
683 FILTER_3D(0);
684 }
685 }
686 else if ((datatype == GL_FLOAT) && (comps == 4)) {
687 DECLARE_ROW_POINTERS(GLfloat, 4);
688
689 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
690 i++, j += colStride, k += colStride) {
691 FILTER_F_3D(0);
692 FILTER_F_3D(1);
693 FILTER_F_3D(2);
694 FILTER_F_3D(3);
695 }
696 }
697 else if ((datatype == GL_FLOAT) && (comps == 3)) {
698 DECLARE_ROW_POINTERS(GLfloat, 3);
699
700 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
701 i++, j += colStride, k += colStride) {
702 FILTER_F_3D(0);
703 FILTER_F_3D(1);
704 FILTER_F_3D(2);
705 }
706 }
707 else if ((datatype == GL_FLOAT) && (comps == 2)) {
708 DECLARE_ROW_POINTERS(GLfloat, 2);
709
710 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
711 i++, j += colStride, k += colStride) {
712 FILTER_F_3D(0);
713 FILTER_F_3D(1);
714 }
715 }
716 else if ((datatype == GL_FLOAT) && (comps == 1)) {
717 DECLARE_ROW_POINTERS(GLfloat, 1);
718
719 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
720 i++, j += colStride, k += colStride) {
721 FILTER_F_3D(0);
722 }
723 }
724 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 4)) {
725 DECLARE_ROW_POINTERS(GLhalfARB, 4);
726
727 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
728 i++, j += colStride, k += colStride) {
729 FILTER_HF_3D(0);
730 FILTER_HF_3D(1);
731 FILTER_HF_3D(2);
732 FILTER_HF_3D(3);
733 }
734 }
735 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 3)) {
736 DECLARE_ROW_POINTERS(GLhalfARB, 4);
737
738 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
739 i++, j += colStride, k += colStride) {
740 FILTER_HF_3D(0);
741 FILTER_HF_3D(1);
742 FILTER_HF_3D(2);
743 }
744 }
745 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 2)) {
746 DECLARE_ROW_POINTERS(GLhalfARB, 4);
747
748 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
749 i++, j += colStride, k += colStride) {
750 FILTER_HF_3D(0);
751 FILTER_HF_3D(1);
752 }
753 }
754 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 1)) {
755 DECLARE_ROW_POINTERS(GLhalfARB, 4);
756
757 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
758 i++, j += colStride, k += colStride) {
759 FILTER_HF_3D(0);
760 }
761 }
762 else if ((datatype == GL_UNSIGNED_INT) && (comps == 1)) {
763 const GLuint *rowA = (const GLuint *) srcRowA;
764 const GLuint *rowB = (const GLuint *) srcRowB;
765 const GLuint *rowC = (const GLuint *) srcRowC;
766 const GLuint *rowD = (const GLuint *) srcRowD;
767 GLfloat *dst = (GLfloat *) dstRow;
768
769 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
770 i++, j += colStride, k += colStride) {
771 const uint64_t tmp = (((uint64_t) rowA[j] + (uint64_t) rowA[k])
772 + ((uint64_t) rowB[j] + (uint64_t) rowB[k])
773 + ((uint64_t) rowC[j] + (uint64_t) rowC[k])
774 + ((uint64_t) rowD[j] + (uint64_t) rowD[k]));
775 dst[i] = (GLfloat)((double) tmp * 0.125);
776 }
777 }
778 else if ((datatype == GL_UNSIGNED_SHORT_5_6_5) && (comps == 3)) {
779 DECLARE_ROW_POINTERS0(GLushort);
780
781 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
782 i++, j += colStride, k += colStride) {
783 const GLint rowAr0 = rowA[j] & 0x1f;
784 const GLint rowAr1 = rowA[k] & 0x1f;
785 const GLint rowBr0 = rowB[j] & 0x1f;
786 const GLint rowBr1 = rowB[k] & 0x1f;
787 const GLint rowCr0 = rowC[j] & 0x1f;
788 const GLint rowCr1 = rowC[k] & 0x1f;
789 const GLint rowDr0 = rowD[j] & 0x1f;
790 const GLint rowDr1 = rowD[k] & 0x1f;
791 const GLint rowAg0 = (rowA[j] >> 5) & 0x3f;
792 const GLint rowAg1 = (rowA[k] >> 5) & 0x3f;
793 const GLint rowBg0 = (rowB[j] >> 5) & 0x3f;
794 const GLint rowBg1 = (rowB[k] >> 5) & 0x3f;
795 const GLint rowCg0 = (rowC[j] >> 5) & 0x3f;
796 const GLint rowCg1 = (rowC[k] >> 5) & 0x3f;
797 const GLint rowDg0 = (rowD[j] >> 5) & 0x3f;
798 const GLint rowDg1 = (rowD[k] >> 5) & 0x3f;
799 const GLint rowAb0 = (rowA[j] >> 11) & 0x1f;
800 const GLint rowAb1 = (rowA[k] >> 11) & 0x1f;
801 const GLint rowBb0 = (rowB[j] >> 11) & 0x1f;
802 const GLint rowBb1 = (rowB[k] >> 11) & 0x1f;
803 const GLint rowCb0 = (rowC[j] >> 11) & 0x1f;
804 const GLint rowCb1 = (rowC[k] >> 11) & 0x1f;
805 const GLint rowDb0 = (rowD[j] >> 11) & 0x1f;
806 const GLint rowDb1 = (rowD[k] >> 11) & 0x1f;
807 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
808 rowCr0, rowCr1, rowDr0, rowDr1);
809 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
810 rowCg0, rowCg1, rowDg0, rowDg1);
811 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
812 rowCb0, rowCb1, rowDb0, rowDb1);
813 dst[i] = (b << 11) | (g << 5) | r;
814 }
815 }
816 else if ((datatype == GL_UNSIGNED_SHORT_4_4_4_4) && (comps == 4)) {
817 DECLARE_ROW_POINTERS0(GLushort);
818
819 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
820 i++, j += colStride, k += colStride) {
821 const GLint rowAr0 = rowA[j] & 0xf;
822 const GLint rowAr1 = rowA[k] & 0xf;
823 const GLint rowBr0 = rowB[j] & 0xf;
824 const GLint rowBr1 = rowB[k] & 0xf;
825 const GLint rowCr0 = rowC[j] & 0xf;
826 const GLint rowCr1 = rowC[k] & 0xf;
827 const GLint rowDr0 = rowD[j] & 0xf;
828 const GLint rowDr1 = rowD[k] & 0xf;
829 const GLint rowAg0 = (rowA[j] >> 4) & 0xf;
830 const GLint rowAg1 = (rowA[k] >> 4) & 0xf;
831 const GLint rowBg0 = (rowB[j] >> 4) & 0xf;
832 const GLint rowBg1 = (rowB[k] >> 4) & 0xf;
833 const GLint rowCg0 = (rowC[j] >> 4) & 0xf;
834 const GLint rowCg1 = (rowC[k] >> 4) & 0xf;
835 const GLint rowDg0 = (rowD[j] >> 4) & 0xf;
836 const GLint rowDg1 = (rowD[k] >> 4) & 0xf;
837 const GLint rowAb0 = (rowA[j] >> 8) & 0xf;
838 const GLint rowAb1 = (rowA[k] >> 8) & 0xf;
839 const GLint rowBb0 = (rowB[j] >> 8) & 0xf;
840 const GLint rowBb1 = (rowB[k] >> 8) & 0xf;
841 const GLint rowCb0 = (rowC[j] >> 8) & 0xf;
842 const GLint rowCb1 = (rowC[k] >> 8) & 0xf;
843 const GLint rowDb0 = (rowD[j] >> 8) & 0xf;
844 const GLint rowDb1 = (rowD[k] >> 8) & 0xf;
845 const GLint rowAa0 = (rowA[j] >> 12) & 0xf;
846 const GLint rowAa1 = (rowA[k] >> 12) & 0xf;
847 const GLint rowBa0 = (rowB[j] >> 12) & 0xf;
848 const GLint rowBa1 = (rowB[k] >> 12) & 0xf;
849 const GLint rowCa0 = (rowC[j] >> 12) & 0xf;
850 const GLint rowCa1 = (rowC[k] >> 12) & 0xf;
851 const GLint rowDa0 = (rowD[j] >> 12) & 0xf;
852 const GLint rowDa1 = (rowD[k] >> 12) & 0xf;
853 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
854 rowCr0, rowCr1, rowDr0, rowDr1);
855 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
856 rowCg0, rowCg1, rowDg0, rowDg1);
857 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
858 rowCb0, rowCb1, rowDb0, rowDb1);
859 const GLint a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
860 rowCa0, rowCa1, rowDa0, rowDa1);
861
862 dst[i] = (a << 12) | (b << 8) | (g << 4) | r;
863 }
864 }
865 else if ((datatype == GL_UNSIGNED_SHORT_1_5_5_5_REV) && (comps == 4)) {
866 DECLARE_ROW_POINTERS0(GLushort);
867
868 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
869 i++, j += colStride, k += colStride) {
870 const GLint rowAr0 = rowA[j] & 0x1f;
871 const GLint rowAr1 = rowA[k] & 0x1f;
872 const GLint rowBr0 = rowB[j] & 0x1f;
873 const GLint rowBr1 = rowB[k] & 0x1f;
874 const GLint rowCr0 = rowC[j] & 0x1f;
875 const GLint rowCr1 = rowC[k] & 0x1f;
876 const GLint rowDr0 = rowD[j] & 0x1f;
877 const GLint rowDr1 = rowD[k] & 0x1f;
878 const GLint rowAg0 = (rowA[j] >> 5) & 0x1f;
879 const GLint rowAg1 = (rowA[k] >> 5) & 0x1f;
880 const GLint rowBg0 = (rowB[j] >> 5) & 0x1f;
881 const GLint rowBg1 = (rowB[k] >> 5) & 0x1f;
882 const GLint rowCg0 = (rowC[j] >> 5) & 0x1f;
883 const GLint rowCg1 = (rowC[k] >> 5) & 0x1f;
884 const GLint rowDg0 = (rowD[j] >> 5) & 0x1f;
885 const GLint rowDg1 = (rowD[k] >> 5) & 0x1f;
886 const GLint rowAb0 = (rowA[j] >> 10) & 0x1f;
887 const GLint rowAb1 = (rowA[k] >> 10) & 0x1f;
888 const GLint rowBb0 = (rowB[j] >> 10) & 0x1f;
889 const GLint rowBb1 = (rowB[k] >> 10) & 0x1f;
890 const GLint rowCb0 = (rowC[j] >> 10) & 0x1f;
891 const GLint rowCb1 = (rowC[k] >> 10) & 0x1f;
892 const GLint rowDb0 = (rowD[j] >> 10) & 0x1f;
893 const GLint rowDb1 = (rowD[k] >> 10) & 0x1f;
894 const GLint rowAa0 = (rowA[j] >> 15) & 0x1;
895 const GLint rowAa1 = (rowA[k] >> 15) & 0x1;
896 const GLint rowBa0 = (rowB[j] >> 15) & 0x1;
897 const GLint rowBa1 = (rowB[k] >> 15) & 0x1;
898 const GLint rowCa0 = (rowC[j] >> 15) & 0x1;
899 const GLint rowCa1 = (rowC[k] >> 15) & 0x1;
900 const GLint rowDa0 = (rowD[j] >> 15) & 0x1;
901 const GLint rowDa1 = (rowD[k] >> 15) & 0x1;
902 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
903 rowCr0, rowCr1, rowDr0, rowDr1);
904 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
905 rowCg0, rowCg1, rowDg0, rowDg1);
906 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
907 rowCb0, rowCb1, rowDb0, rowDb1);
908 const GLint a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
909 rowCa0, rowCa1, rowDa0, rowDa1);
910
911 dst[i] = (a << 15) | (b << 10) | (g << 5) | r;
912 }
913 }
914 else if ((datatype == GL_UNSIGNED_BYTE_3_3_2) && (comps == 3)) {
915 DECLARE_ROW_POINTERS0(GLushort);
916
917 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
918 i++, j += colStride, k += colStride) {
919 const GLint rowAr0 = rowA[j] & 0x3;
920 const GLint rowAr1 = rowA[k] & 0x3;
921 const GLint rowBr0 = rowB[j] & 0x3;
922 const GLint rowBr1 = rowB[k] & 0x3;
923 const GLint rowCr0 = rowC[j] & 0x3;
924 const GLint rowCr1 = rowC[k] & 0x3;
925 const GLint rowDr0 = rowD[j] & 0x3;
926 const GLint rowDr1 = rowD[k] & 0x3;
927 const GLint rowAg0 = (rowA[j] >> 2) & 0x7;
928 const GLint rowAg1 = (rowA[k] >> 2) & 0x7;
929 const GLint rowBg0 = (rowB[j] >> 2) & 0x7;
930 const GLint rowBg1 = (rowB[k] >> 2) & 0x7;
931 const GLint rowCg0 = (rowC[j] >> 2) & 0x7;
932 const GLint rowCg1 = (rowC[k] >> 2) & 0x7;
933 const GLint rowDg0 = (rowD[j] >> 2) & 0x7;
934 const GLint rowDg1 = (rowD[k] >> 2) & 0x7;
935 const GLint rowAb0 = (rowA[j] >> 5) & 0x7;
936 const GLint rowAb1 = (rowA[k] >> 5) & 0x7;
937 const GLint rowBb0 = (rowB[j] >> 5) & 0x7;
938 const GLint rowBb1 = (rowB[k] >> 5) & 0x7;
939 const GLint rowCb0 = (rowC[j] >> 5) & 0x7;
940 const GLint rowCb1 = (rowC[k] >> 5) & 0x7;
941 const GLint rowDb0 = (rowD[j] >> 5) & 0x7;
942 const GLint rowDb1 = (rowD[k] >> 5) & 0x7;
943 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
944 rowCr0, rowCr1, rowDr0, rowDr1);
945 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
946 rowCg0, rowCg1, rowDg0, rowDg1);
947 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
948 rowCb0, rowCb1, rowDb0, rowDb1);
949 dst[i] = (b << 5) | (g << 2) | r;
950 }
951 }
952 else {
953 _mesa_problem(NULL, "bad format in do_row()");
954 }
955 }
956
957
958 /*
959 * These functions generate a 1/2-size mipmap image from a source image.
960 * Texture borders are handled by copying or averaging the source image's
961 * border texels, depending on the scale-down factor.
962 */
963
964 static void
965 make_1d_mipmap(GLenum datatype, GLuint comps, GLint border,
966 GLint srcWidth, const GLubyte *srcPtr,
967 GLint dstWidth, GLubyte *dstPtr)
968 {
969 const GLint bpt = bytes_per_pixel(datatype, comps);
970 const GLubyte *src;
971 GLubyte *dst;
972
973 /* skip the border pixel, if any */
974 src = srcPtr + border * bpt;
975 dst = dstPtr + border * bpt;
976
977 /* we just duplicate the input row, kind of hack, saves code */
978 do_row(datatype, comps, srcWidth - 2 * border, src, src,
979 dstWidth - 2 * border, dst);
980
981 if (border) {
982 /* copy left-most pixel from source */
983 MEMCPY(dstPtr, srcPtr, bpt);
984 /* copy right-most pixel from source */
985 MEMCPY(dstPtr + (dstWidth - 1) * bpt,
986 srcPtr + (srcWidth - 1) * bpt,
987 bpt);
988 }
989 }
990
991
992 static void
993 make_2d_mipmap(GLenum datatype, GLuint comps, GLint border,
994 GLint srcWidth, GLint srcHeight,
995 const GLubyte *srcPtr, GLint srcRowStride,
996 GLint dstWidth, GLint dstHeight,
997 GLubyte *dstPtr, GLint dstRowStride)
998 {
999 const GLint bpt = bytes_per_pixel(datatype, comps);
1000 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1001 const GLint dstWidthNB = dstWidth - 2 * border;
1002 const GLint dstHeightNB = dstHeight - 2 * border;
1003 const GLint srcRowBytes = bpt * srcRowStride;
1004 const GLint dstRowBytes = bpt * dstRowStride;
1005 const GLubyte *srcA, *srcB;
1006 GLubyte *dst;
1007 GLint row;
1008
1009 /* Compute src and dst pointers, skipping any border */
1010 srcA = srcPtr + border * ((srcWidth + 1) * bpt);
1011 if (srcHeight > 1)
1012 srcB = srcA + srcRowBytes;
1013 else
1014 srcB = srcA;
1015 dst = dstPtr + border * ((dstWidth + 1) * bpt);
1016
1017 for (row = 0; row < dstHeightNB; row++) {
1018 do_row(datatype, comps, srcWidthNB, srcA, srcB,
1019 dstWidthNB, dst);
1020 srcA += 2 * srcRowBytes;
1021 srcB += 2 * srcRowBytes;
1022 dst += dstRowBytes;
1023 }
1024
1025 /* This is ugly but probably won't be used much */
1026 if (border > 0) {
1027 /* fill in dest border */
1028 /* lower-left border pixel */
1029 MEMCPY(dstPtr, srcPtr, bpt);
1030 /* lower-right border pixel */
1031 MEMCPY(dstPtr + (dstWidth - 1) * bpt,
1032 srcPtr + (srcWidth - 1) * bpt, bpt);
1033 /* upper-left border pixel */
1034 MEMCPY(dstPtr + dstWidth * (dstHeight - 1) * bpt,
1035 srcPtr + srcWidth * (srcHeight - 1) * bpt, bpt);
1036 /* upper-right border pixel */
1037 MEMCPY(dstPtr + (dstWidth * dstHeight - 1) * bpt,
1038 srcPtr + (srcWidth * srcHeight - 1) * bpt, bpt);
1039 /* lower border */
1040 do_row(datatype, comps, srcWidthNB,
1041 srcPtr + bpt,
1042 srcPtr + bpt,
1043 dstWidthNB, dstPtr + bpt);
1044 /* upper border */
1045 do_row(datatype, comps, srcWidthNB,
1046 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1047 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1048 dstWidthNB,
1049 dstPtr + (dstWidth * (dstHeight - 1) + 1) * bpt);
1050 /* left and right borders */
1051 if (srcHeight == dstHeight) {
1052 /* copy border pixel from src to dst */
1053 for (row = 1; row < srcHeight; row++) {
1054 MEMCPY(dstPtr + dstWidth * row * bpt,
1055 srcPtr + srcWidth * row * bpt, bpt);
1056 MEMCPY(dstPtr + (dstWidth * row + dstWidth - 1) * bpt,
1057 srcPtr + (srcWidth * row + srcWidth - 1) * bpt, bpt);
1058 }
1059 }
1060 else {
1061 /* average two src pixels each dest pixel */
1062 for (row = 0; row < dstHeightNB; row += 2) {
1063 do_row(datatype, comps, 1,
1064 srcPtr + (srcWidth * (row * 2 + 1)) * bpt,
1065 srcPtr + (srcWidth * (row * 2 + 2)) * bpt,
1066 1, dstPtr + (dstWidth * row + 1) * bpt);
1067 do_row(datatype, comps, 1,
1068 srcPtr + (srcWidth * (row * 2 + 1) + srcWidth - 1) * bpt,
1069 srcPtr + (srcWidth * (row * 2 + 2) + srcWidth - 1) * bpt,
1070 1, dstPtr + (dstWidth * row + 1 + dstWidth - 1) * bpt);
1071 }
1072 }
1073 }
1074 }
1075
1076
1077 static void
1078 make_3d_mipmap(GLenum datatype, GLuint comps, GLint border,
1079 GLint srcWidth, GLint srcHeight, GLint srcDepth,
1080 const GLubyte *srcPtr, GLint srcRowStride,
1081 GLint dstWidth, GLint dstHeight, GLint dstDepth,
1082 GLubyte *dstPtr, GLint dstRowStride)
1083 {
1084 const GLint bpt = bytes_per_pixel(datatype, comps);
1085 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1086 const GLint srcDepthNB = srcDepth - 2 * border;
1087 const GLint dstWidthNB = dstWidth - 2 * border;
1088 const GLint dstHeightNB = dstHeight - 2 * border;
1089 const GLint dstDepthNB = dstDepth - 2 * border;
1090 GLint img, row;
1091 GLint bytesPerSrcImage, bytesPerDstImage;
1092 GLint bytesPerSrcRow, bytesPerDstRow;
1093 GLint srcImageOffset, srcRowOffset;
1094
1095 (void) srcDepthNB; /* silence warnings */
1096
1097
1098 bytesPerSrcImage = srcWidth * srcHeight * bpt;
1099 bytesPerDstImage = dstWidth * dstHeight * bpt;
1100
1101 bytesPerSrcRow = srcWidth * bpt;
1102 bytesPerDstRow = dstWidth * bpt;
1103
1104 /* Offset between adjacent src images to be averaged together */
1105 srcImageOffset = (srcDepth == dstDepth) ? 0 : bytesPerSrcImage;
1106
1107 /* Offset between adjacent src rows to be averaged together */
1108 srcRowOffset = (srcHeight == dstHeight) ? 0 : srcWidth * bpt;
1109
1110 /*
1111 * Need to average together up to 8 src pixels for each dest pixel.
1112 * Break that down into 3 operations:
1113 * 1. take two rows from source image and average them together.
1114 * 2. take two rows from next source image and average them together.
1115 * 3. take the two averaged rows and average them for the final dst row.
1116 */
1117
1118 /*
1119 _mesa_printf("mip3d %d x %d x %d -> %d x %d x %d\n",
1120 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
1121 */
1122
1123 for (img = 0; img < dstDepthNB; img++) {
1124 /* first source image pointer, skipping border */
1125 const GLubyte *imgSrcA = srcPtr
1126 + (bytesPerSrcImage + bytesPerSrcRow + border) * bpt * border
1127 + img * (bytesPerSrcImage + srcImageOffset);
1128 /* second source image pointer, skipping border */
1129 const GLubyte *imgSrcB = imgSrcA + srcImageOffset;
1130 /* address of the dest image, skipping border */
1131 GLubyte *imgDst = dstPtr
1132 + (bytesPerDstImage + bytesPerDstRow + border) * bpt * border
1133 + img * bytesPerDstImage;
1134
1135 /* setup the four source row pointers and the dest row pointer */
1136 const GLubyte *srcImgARowA = imgSrcA;
1137 const GLubyte *srcImgARowB = imgSrcA + srcRowOffset;
1138 const GLubyte *srcImgBRowA = imgSrcB;
1139 const GLubyte *srcImgBRowB = imgSrcB + srcRowOffset;
1140 GLubyte *dstImgRow = imgDst;
1141
1142 for (row = 0; row < dstHeightNB; row++) {
1143 do_row_3D(datatype, comps, srcWidthNB,
1144 srcImgARowA, srcImgARowB,
1145 srcImgBRowA, srcImgBRowB,
1146 dstWidthNB, dstImgRow);
1147
1148 /* advance to next rows */
1149 srcImgARowA += bytesPerSrcRow + srcRowOffset;
1150 srcImgARowB += bytesPerSrcRow + srcRowOffset;
1151 srcImgBRowA += bytesPerSrcRow + srcRowOffset;
1152 srcImgBRowB += bytesPerSrcRow + srcRowOffset;
1153 dstImgRow += bytesPerDstRow;
1154 }
1155 }
1156
1157
1158 /* Luckily we can leverage the make_2d_mipmap() function here! */
1159 if (border > 0) {
1160 /* do front border image */
1161 make_2d_mipmap(datatype, comps, 1, srcWidth, srcHeight, srcPtr, srcRowStride,
1162 dstWidth, dstHeight, dstPtr, dstRowStride);
1163 /* do back border image */
1164 make_2d_mipmap(datatype, comps, 1, srcWidth, srcHeight,
1165 srcPtr + bytesPerSrcImage * (srcDepth - 1), srcRowStride,
1166 dstWidth, dstHeight,
1167 dstPtr + bytesPerDstImage * (dstDepth - 1), dstRowStride);
1168 /* do four remaining border edges that span the image slices */
1169 if (srcDepth == dstDepth) {
1170 /* just copy border pixels from src to dst */
1171 for (img = 0; img < dstDepthNB; img++) {
1172 const GLubyte *src;
1173 GLubyte *dst;
1174
1175 /* do border along [img][row=0][col=0] */
1176 src = srcPtr + (img + 1) * bytesPerSrcImage;
1177 dst = dstPtr + (img + 1) * bytesPerDstImage;
1178 MEMCPY(dst, src, bpt);
1179
1180 /* do border along [img][row=dstHeight-1][col=0] */
1181 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1182 + (srcHeight - 1) * bytesPerSrcRow;
1183 dst = dstPtr + (img + 1) * bytesPerDstImage
1184 + (dstHeight - 1) * bytesPerDstRow;
1185 MEMCPY(dst, src, bpt);
1186
1187 /* do border along [img][row=0][col=dstWidth-1] */
1188 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1189 + (srcWidth - 1) * bpt;
1190 dst = dstPtr + (img + 1) * bytesPerDstImage
1191 + (dstWidth - 1) * bpt;
1192 MEMCPY(dst, src, bpt);
1193
1194 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1195 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1196 + (bytesPerSrcImage - bpt);
1197 dst = dstPtr + (img + 1) * bytesPerDstImage
1198 + (bytesPerDstImage - bpt);
1199 MEMCPY(dst, src, bpt);
1200 }
1201 }
1202 else {
1203 /* average border pixels from adjacent src image pairs */
1204 ASSERT(srcDepthNB == 2 * dstDepthNB);
1205 for (img = 0; img < dstDepthNB; img++) {
1206 const GLubyte *src;
1207 GLubyte *dst;
1208
1209 /* do border along [img][row=0][col=0] */
1210 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage;
1211 dst = dstPtr + (img + 1) * bytesPerDstImage;
1212 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1213
1214 /* do border along [img][row=dstHeight-1][col=0] */
1215 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1216 + (srcHeight - 1) * bytesPerSrcRow;
1217 dst = dstPtr + (img + 1) * bytesPerDstImage
1218 + (dstHeight - 1) * bytesPerDstRow;
1219 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1220
1221 /* do border along [img][row=0][col=dstWidth-1] */
1222 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1223 + (srcWidth - 1) * bpt;
1224 dst = dstPtr + (img + 1) * bytesPerDstImage
1225 + (dstWidth - 1) * bpt;
1226 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1227
1228 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1229 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1230 + (bytesPerSrcImage - bpt);
1231 dst = dstPtr + (img + 1) * bytesPerDstImage
1232 + (bytesPerDstImage - bpt);
1233 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1234 }
1235 }
1236 }
1237 }
1238
1239
1240 static void
1241 make_1d_stack_mipmap(GLenum datatype, GLuint comps, GLint border,
1242 GLint srcWidth, const GLubyte *srcPtr, GLuint srcRowStride,
1243 GLint dstWidth, GLint dstHeight,
1244 GLubyte *dstPtr, GLuint dstRowStride )
1245 {
1246 const GLint bpt = bytes_per_pixel(datatype, comps);
1247 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1248 const GLint dstWidthNB = dstWidth - 2 * border;
1249 const GLint dstHeightNB = dstHeight - 2 * border;
1250 const GLint srcRowBytes = bpt * srcRowStride;
1251 const GLint dstRowBytes = bpt * dstRowStride;
1252 const GLubyte *src;
1253 GLubyte *dst;
1254 GLint row;
1255
1256 /* Compute src and dst pointers, skipping any border */
1257 src = srcPtr + border * ((srcWidth + 1) * bpt);
1258 dst = dstPtr + border * ((dstWidth + 1) * bpt);
1259
1260 for (row = 0; row < dstHeightNB; row++) {
1261 do_row(datatype, comps, srcWidthNB, src, src,
1262 dstWidthNB, dst);
1263 src += srcRowBytes;
1264 dst += dstRowBytes;
1265 }
1266
1267 if (border) {
1268 /* copy left-most pixel from source */
1269 MEMCPY(dstPtr, srcPtr, bpt);
1270 /* copy right-most pixel from source */
1271 MEMCPY(dstPtr + (dstWidth - 1) * bpt,
1272 srcPtr + (srcWidth - 1) * bpt,
1273 bpt);
1274 }
1275 }
1276
1277
1278 /**
1279 * \bug
1280 * There is quite a bit of refactoring that could be done with this function
1281 * and \c make_2d_mipmap.
1282 */
1283 static void
1284 make_2d_stack_mipmap(GLenum datatype, GLuint comps, GLint border,
1285 GLint srcWidth, GLint srcHeight,
1286 const GLubyte *srcPtr, GLint srcRowStride,
1287 GLint dstWidth, GLint dstHeight, GLint dstDepth,
1288 GLubyte *dstPtr, GLint dstRowStride)
1289 {
1290 const GLint bpt = bytes_per_pixel(datatype, comps);
1291 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1292 const GLint dstWidthNB = dstWidth - 2 * border;
1293 const GLint dstHeightNB = dstHeight - 2 * border;
1294 const GLint dstDepthNB = dstDepth - 2 * border;
1295 const GLint srcRowBytes = bpt * srcRowStride;
1296 const GLint dstRowBytes = bpt * dstRowStride;
1297 const GLubyte *srcA, *srcB;
1298 GLubyte *dst;
1299 GLint layer;
1300 GLint row;
1301
1302 /* Compute src and dst pointers, skipping any border */
1303 srcA = srcPtr + border * ((srcWidth + 1) * bpt);
1304 if (srcHeight > 1)
1305 srcB = srcA + srcRowBytes;
1306 else
1307 srcB = srcA;
1308 dst = dstPtr + border * ((dstWidth + 1) * bpt);
1309
1310 for (layer = 0; layer < dstDepthNB; layer++) {
1311 for (row = 0; row < dstHeightNB; row++) {
1312 do_row(datatype, comps, srcWidthNB, srcA, srcB,
1313 dstWidthNB, dst);
1314 srcA += 2 * srcRowBytes;
1315 srcB += 2 * srcRowBytes;
1316 dst += dstRowBytes;
1317 }
1318
1319 /* This is ugly but probably won't be used much */
1320 if (border > 0) {
1321 /* fill in dest border */
1322 /* lower-left border pixel */
1323 MEMCPY(dstPtr, srcPtr, bpt);
1324 /* lower-right border pixel */
1325 MEMCPY(dstPtr + (dstWidth - 1) * bpt,
1326 srcPtr + (srcWidth - 1) * bpt, bpt);
1327 /* upper-left border pixel */
1328 MEMCPY(dstPtr + dstWidth * (dstHeight - 1) * bpt,
1329 srcPtr + srcWidth * (srcHeight - 1) * bpt, bpt);
1330 /* upper-right border pixel */
1331 MEMCPY(dstPtr + (dstWidth * dstHeight - 1) * bpt,
1332 srcPtr + (srcWidth * srcHeight - 1) * bpt, bpt);
1333 /* lower border */
1334 do_row(datatype, comps, srcWidthNB,
1335 srcPtr + bpt,
1336 srcPtr + bpt,
1337 dstWidthNB, dstPtr + bpt);
1338 /* upper border */
1339 do_row(datatype, comps, srcWidthNB,
1340 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1341 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1342 dstWidthNB,
1343 dstPtr + (dstWidth * (dstHeight - 1) + 1) * bpt);
1344 /* left and right borders */
1345 if (srcHeight == dstHeight) {
1346 /* copy border pixel from src to dst */
1347 for (row = 1; row < srcHeight; row++) {
1348 MEMCPY(dstPtr + dstWidth * row * bpt,
1349 srcPtr + srcWidth * row * bpt, bpt);
1350 MEMCPY(dstPtr + (dstWidth * row + dstWidth - 1) * bpt,
1351 srcPtr + (srcWidth * row + srcWidth - 1) * bpt, bpt);
1352 }
1353 }
1354 else {
1355 /* average two src pixels each dest pixel */
1356 for (row = 0; row < dstHeightNB; row += 2) {
1357 do_row(datatype, comps, 1,
1358 srcPtr + (srcWidth * (row * 2 + 1)) * bpt,
1359 srcPtr + (srcWidth * (row * 2 + 2)) * bpt,
1360 1, dstPtr + (dstWidth * row + 1) * bpt);
1361 do_row(datatype, comps, 1,
1362 srcPtr + (srcWidth * (row * 2 + 1) + srcWidth - 1) * bpt,
1363 srcPtr + (srcWidth * (row * 2 + 2) + srcWidth - 1) * bpt,
1364 1, dstPtr + (dstWidth * row + 1 + dstWidth - 1) * bpt);
1365 }
1366 }
1367 }
1368 }
1369 }
1370
1371
1372 /**
1373 * Down-sample a texture image to produce the next lower mipmap level.
1374 * \param comps components per texel (1, 2, 3 or 4)
1375 * \param srcRowStride stride between source rows, in texels
1376 * \param dstRowStride stride between destination rows, in texels
1377 */
1378 void
1379 _mesa_generate_mipmap_level(GLenum target,
1380 GLenum datatype, GLuint comps,
1381 GLint border,
1382 GLint srcWidth, GLint srcHeight, GLint srcDepth,
1383 const GLubyte *srcData,
1384 GLint srcRowStride,
1385 GLint dstWidth, GLint dstHeight, GLint dstDepth,
1386 GLubyte *dstData,
1387 GLint dstRowStride)
1388 {
1389 /*
1390 * We use simple 2x2 averaging to compute the next mipmap level.
1391 */
1392 switch (target) {
1393 case GL_TEXTURE_1D:
1394 make_1d_mipmap(datatype, comps, border,
1395 srcWidth, srcData,
1396 dstWidth, dstData);
1397 break;
1398 case GL_TEXTURE_2D:
1399 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
1400 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
1401 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
1402 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
1403 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
1404 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
1405 make_2d_mipmap(datatype, comps, border,
1406 srcWidth, srcHeight, srcData, srcRowStride,
1407 dstWidth, dstHeight, dstData, dstRowStride);
1408 break;
1409 case GL_TEXTURE_3D:
1410 make_3d_mipmap(datatype, comps, border,
1411 srcWidth, srcHeight, srcDepth,
1412 srcData, srcRowStride,
1413 dstWidth, dstHeight, dstDepth,
1414 dstData, dstRowStride);
1415 break;
1416 case GL_TEXTURE_1D_ARRAY_EXT:
1417 make_1d_stack_mipmap(datatype, comps, border,
1418 srcWidth, srcData, srcRowStride,
1419 dstWidth, dstHeight,
1420 dstData, dstRowStride);
1421 break;
1422 case GL_TEXTURE_2D_ARRAY_EXT:
1423 make_2d_stack_mipmap(datatype, comps, border,
1424 srcWidth, srcHeight,
1425 srcData, srcRowStride,
1426 dstWidth, dstHeight,
1427 dstDepth, dstData, dstRowStride);
1428 break;
1429 case GL_TEXTURE_RECTANGLE_NV:
1430 /* no mipmaps, do nothing */
1431 break;
1432 default:
1433 _mesa_problem(NULL, "bad dimensions in _mesa_generate_mipmaps");
1434 return;
1435 }
1436 }
1437
1438
1439 /**
1440 * compute next (level+1) image size
1441 * \return GL_FALSE if no smaller size can be generated (eg. src is 1x1x1 size)
1442 */
1443 static GLboolean
1444 next_mipmap_level_size(GLenum target, GLint border,
1445 GLint srcWidth, GLint srcHeight, GLint srcDepth,
1446 GLint *dstWidth, GLint *dstHeight, GLint *dstDepth)
1447 {
1448 if (srcWidth - 2 * border > 1) {
1449 *dstWidth = (srcWidth - 2 * border) / 2 + 2 * border;
1450 }
1451 else {
1452 *dstWidth = srcWidth; /* can't go smaller */
1453 }
1454
1455 if ((srcHeight - 2 * border > 1) &&
1456 (target != GL_TEXTURE_1D_ARRAY_EXT)) {
1457 *dstHeight = (srcHeight - 2 * border) / 2 + 2 * border;
1458 }
1459 else {
1460 *dstHeight = srcHeight; /* can't go smaller */
1461 }
1462
1463 if ((srcDepth - 2 * border > 1) &&
1464 (target != GL_TEXTURE_2D_ARRAY_EXT)) {
1465 *dstDepth = (srcDepth - 2 * border) / 2 + 2 * border;
1466 }
1467 else {
1468 *dstDepth = srcDepth; /* can't go smaller */
1469 }
1470
1471 if (*dstWidth == srcWidth &&
1472 *dstHeight == srcHeight &&
1473 *dstDepth == srcDepth) {
1474 return GL_FALSE;
1475 }
1476 else {
1477 return GL_TRUE;
1478 }
1479 }
1480
1481
1482
1483
1484 /**
1485 * Automatic mipmap generation.
1486 * This is the fallback/default function for ctx->Driver.GenerateMipmap().
1487 * Generate a complete set of mipmaps from texObj's BaseLevel image.
1488 * Stop at texObj's MaxLevel or when we get to the 1x1 texture.
1489 * For cube maps, target will be one of
1490 * GL_TEXTURE_CUBE_MAP_POSITIVE/NEGATIVE_X/Y/Z; never GL_TEXTURE_CUBE_MAP.
1491 */
1492 void
1493 _mesa_generate_mipmap(GLcontext *ctx, GLenum target,
1494 struct gl_texture_object *texObj)
1495 {
1496 const struct gl_texture_image *srcImage;
1497 gl_format convertFormat;
1498 const GLubyte *srcData = NULL;
1499 GLubyte *dstData = NULL;
1500 GLint level, maxLevels;
1501 GLenum datatype;
1502 GLuint comps;
1503
1504 ASSERT(texObj);
1505 srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
1506 ASSERT(srcImage);
1507
1508 maxLevels = _mesa_max_texture_levels(ctx, texObj->Target);
1509 ASSERT(maxLevels > 0); /* bad target */
1510
1511 /* Find convertFormat - the format that do_row() will process */
1512
1513 if (_mesa_is_format_compressed(srcImage->TexFormat)) {
1514 /* setup for compressed textures - need to allocate temporary
1515 * image buffers to hold uncompressed images.
1516 */
1517 GLuint row;
1518 GLint components, size;
1519 GLchan *dst;
1520
1521 assert(texObj->Target == GL_TEXTURE_2D ||
1522 texObj->Target == GL_TEXTURE_CUBE_MAP_ARB);
1523
1524 if (srcImage->_BaseFormat == GL_RGB) {
1525 convertFormat = MESA_FORMAT_RGB888;
1526 components = 3;
1527 }
1528 else if (srcImage->_BaseFormat == GL_RGBA) {
1529 convertFormat = MESA_FORMAT_RGBA8888;
1530 components = 4;
1531 }
1532 else {
1533 _mesa_problem(ctx, "bad srcImage->_BaseFormat in _mesa_generate_mipmaps");
1534 return;
1535 }
1536
1537 /* allocate storage for uncompressed GL_RGB or GL_RGBA images */
1538 size = _mesa_bytes_per_pixel(srcImage->_BaseFormat, CHAN_TYPE)
1539 * srcImage->Width * srcImage->Height * srcImage->Depth + 20;
1540 /* 20 extra bytes, just be safe when calling last FetchTexel */
1541 srcData = (GLubyte *) _mesa_malloc(size);
1542 if (!srcData) {
1543 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generate mipmaps");
1544 return;
1545 }
1546 dstData = (GLubyte *) _mesa_malloc(size / 2); /* 1/4 would probably be OK */
1547 if (!dstData) {
1548 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generate mipmaps");
1549 _mesa_free((void *) srcData);
1550 return;
1551 }
1552
1553 /* decompress base image here */
1554 dst = (GLchan *) srcData;
1555 for (row = 0; row < srcImage->Height; row++) {
1556 GLuint col;
1557 for (col = 0; col < srcImage->Width; col++) {
1558 srcImage->FetchTexelc(srcImage, col, row, 0, dst);
1559 dst += components;
1560 }
1561 }
1562 }
1563 else {
1564 /* uncompressed */
1565 convertFormat = srcImage->TexFormat;
1566 }
1567
1568 _mesa_format_to_type_and_comps(convertFormat, &datatype, &comps);
1569
1570 for (level = texObj->BaseLevel; level < texObj->MaxLevel
1571 && level < maxLevels - 1; level++) {
1572 /* generate image[level+1] from image[level] */
1573 const struct gl_texture_image *srcImage;
1574 struct gl_texture_image *dstImage;
1575 GLint srcWidth, srcHeight, srcDepth;
1576 GLint dstWidth, dstHeight, dstDepth;
1577 GLint border, bytesPerTexel;
1578 GLboolean nextLevel;
1579
1580 /* get src image parameters */
1581 srcImage = _mesa_select_tex_image(ctx, texObj, target, level);
1582 ASSERT(srcImage);
1583 srcWidth = srcImage->Width;
1584 srcHeight = srcImage->Height;
1585 srcDepth = srcImage->Depth;
1586 border = srcImage->Border;
1587
1588 nextLevel = next_mipmap_level_size(target, border,
1589 srcWidth, srcHeight, srcDepth,
1590 &dstWidth, &dstHeight, &dstDepth);
1591 if (!nextLevel) {
1592 /* all done */
1593 if (_mesa_is_format_compressed(srcImage->TexFormat)) {
1594 _mesa_free((void *) srcData);
1595 _mesa_free(dstData);
1596 }
1597 return;
1598 }
1599
1600 /* get dest gl_texture_image */
1601 dstImage = _mesa_get_tex_image(ctx, texObj, target, level + 1);
1602 if (!dstImage) {
1603 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
1604 return;
1605 }
1606
1607 /* Free old image data */
1608 if (dstImage->Data)
1609 ctx->Driver.FreeTexImageData(ctx, dstImage);
1610
1611 /* initialize new image */
1612 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
1613 dstDepth, border, srcImage->InternalFormat);
1614 dstImage->DriverData = NULL;
1615 dstImage->TexFormat = srcImage->TexFormat;
1616 dstImage->FetchTexelc = srcImage->FetchTexelc;
1617 dstImage->FetchTexelf = srcImage->FetchTexelf;
1618
1619 /* Alloc new teximage data buffer.
1620 * Setup src and dest data pointers.
1621 */
1622 if (_mesa_is_format_compressed(dstImage->TexFormat)) {
1623 GLuint dstCompressedSize =
1624 _mesa_format_image_size(dstImage->TexFormat, dstImage->Width,
1625 dstImage->Height, dstImage->Depth);
1626 ASSERT(dstCompressedSize > 0);
1627
1628 dstImage->Data = _mesa_alloc_texmemory(dstCompressedSize);
1629 if (!dstImage->Data) {
1630 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
1631 return;
1632 }
1633 /* srcData and dstData are already set */
1634 ASSERT(srcData);
1635 ASSERT(dstData);
1636 }
1637 else {
1638 bytesPerTexel = _mesa_get_format_bytes(dstImage->TexFormat);
1639 ASSERT(dstWidth * dstHeight * dstDepth * bytesPerTexel > 0);
1640 dstImage->Data = _mesa_alloc_texmemory(dstWidth * dstHeight
1641 * dstDepth * bytesPerTexel);
1642 if (!dstImage->Data) {
1643 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
1644 return;
1645 }
1646 srcData = (const GLubyte *) srcImage->Data;
1647 dstData = (GLubyte *) dstImage->Data;
1648 }
1649
1650 ASSERT(dstImage->TexFormat);
1651 ASSERT(dstImage->FetchTexelc);
1652 ASSERT(dstImage->FetchTexelf);
1653
1654 _mesa_generate_mipmap_level(target, datatype, comps, border,
1655 srcWidth, srcHeight, srcDepth,
1656 srcData, srcImage->RowStride,
1657 dstWidth, dstHeight, dstDepth,
1658 dstData, dstImage->RowStride);
1659
1660
1661 if (_mesa_is_format_compressed(dstImage->TexFormat)) {
1662 GLubyte *temp;
1663 /* compress image from dstData into dstImage->Data */
1664 const GLenum srcFormat = _mesa_get_format_base_format(convertFormat);
1665 GLint dstRowStride
1666 = _mesa_format_row_stride(dstImage->TexFormat, dstWidth);
1667 ASSERT(srcFormat == GL_RGB || srcFormat == GL_RGBA);
1668
1669 _mesa_texstore(ctx, 2, dstImage->_BaseFormat,
1670 dstImage->TexFormat,
1671 dstImage->Data,
1672 0, 0, 0, /* dstX/Y/Zoffset */
1673 dstRowStride, 0, /* strides */
1674 dstWidth, dstHeight, 1, /* size */
1675 srcFormat, CHAN_TYPE,
1676 dstData, /* src data, actually */
1677 &ctx->DefaultPacking);
1678
1679 /* swap src and dest pointers */
1680 temp = (GLubyte *) srcData;
1681 srcData = dstData;
1682 dstData = temp;
1683 }
1684
1685 } /* loop over mipmap levels */
1686 }
1687
1688
1689 /**
1690 * Helper function for drivers which need to rescale texture images to
1691 * certain aspect ratios.
1692 * Nearest filtering only (for broken hardware that can't support
1693 * all aspect ratios). This can be made a lot faster, but I don't
1694 * really care enough...
1695 */
1696 void
1697 _mesa_rescale_teximage2d(GLuint bytesPerPixel,
1698 GLuint srcStrideInPixels,
1699 GLuint dstRowStride,
1700 GLint srcWidth, GLint srcHeight,
1701 GLint dstWidth, GLint dstHeight,
1702 const GLvoid *srcImage, GLvoid *dstImage)
1703 {
1704 GLint row, col;
1705
1706 #define INNER_LOOP( TYPE, HOP, WOP ) \
1707 for ( row = 0 ; row < dstHeight ; row++ ) { \
1708 GLint srcRow = row HOP hScale; \
1709 for ( col = 0 ; col < dstWidth ; col++ ) { \
1710 GLint srcCol = col WOP wScale; \
1711 dst[col] = src[srcRow * srcStrideInPixels + srcCol]; \
1712 } \
1713 dst = (TYPE *) ((GLubyte *) dst + dstRowStride); \
1714 } \
1715
1716 #define RESCALE_IMAGE( TYPE ) \
1717 do { \
1718 const TYPE *src = (const TYPE *)srcImage; \
1719 TYPE *dst = (TYPE *)dstImage; \
1720 \
1721 if ( srcHeight < dstHeight ) { \
1722 const GLint hScale = dstHeight / srcHeight; \
1723 if ( srcWidth < dstWidth ) { \
1724 const GLint wScale = dstWidth / srcWidth; \
1725 INNER_LOOP( TYPE, /, / ); \
1726 } \
1727 else { \
1728 const GLint wScale = srcWidth / dstWidth; \
1729 INNER_LOOP( TYPE, /, * ); \
1730 } \
1731 } \
1732 else { \
1733 const GLint hScale = srcHeight / dstHeight; \
1734 if ( srcWidth < dstWidth ) { \
1735 const GLint wScale = dstWidth / srcWidth; \
1736 INNER_LOOP( TYPE, *, / ); \
1737 } \
1738 else { \
1739 const GLint wScale = srcWidth / dstWidth; \
1740 INNER_LOOP( TYPE, *, * ); \
1741 } \
1742 } \
1743 } while (0)
1744
1745 switch ( bytesPerPixel ) {
1746 case 4:
1747 RESCALE_IMAGE( GLuint );
1748 break;
1749
1750 case 2:
1751 RESCALE_IMAGE( GLushort );
1752 break;
1753
1754 case 1:
1755 RESCALE_IMAGE( GLubyte );
1756 break;
1757 default:
1758 _mesa_problem(NULL,"unexpected bytes/pixel in _mesa_rescale_teximage2d");
1759 }
1760 }
1761
1762
1763 /**
1764 * Upscale an image by replication, not (typical) stretching.
1765 * We use this when the image width or height is less than a
1766 * certain size (4, 8) and we need to upscale an image.
1767 */
1768 void
1769 _mesa_upscale_teximage2d(GLsizei inWidth, GLsizei inHeight,
1770 GLsizei outWidth, GLsizei outHeight,
1771 GLint comps, const GLchan *src, GLint srcRowStride,
1772 GLchan *dest )
1773 {
1774 GLint i, j, k;
1775
1776 ASSERT(outWidth >= inWidth);
1777 ASSERT(outHeight >= inHeight);
1778 #if 0
1779 ASSERT(inWidth == 1 || inWidth == 2 || inHeight == 1 || inHeight == 2);
1780 ASSERT((outWidth & 3) == 0);
1781 ASSERT((outHeight & 3) == 0);
1782 #endif
1783
1784 for (i = 0; i < outHeight; i++) {
1785 const GLint ii = i % inHeight;
1786 for (j = 0; j < outWidth; j++) {
1787 const GLint jj = j % inWidth;
1788 for (k = 0; k < comps; k++) {
1789 dest[(i * outWidth + j) * comps + k]
1790 = src[ii * srcRowStride + jj * comps + k];
1791 }
1792 }
1793 }
1794 }
1795