mesa: fix texture3D mipmap generation for UNSIGNED_BYTE_3_3_2 and 4_4
[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 "mtypes.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
293 else if (datatype == GL_SHORT && comps == 4) {
294 GLuint i, j, k;
295 const GLshort(*rowA)[4] = (const GLshort(*)[4]) srcRowA;
296 const GLshort(*rowB)[4] = (const GLshort(*)[4]) srcRowB;
297 GLshort(*dst)[4] = (GLshort(*)[4]) dstRow;
298 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
299 i++, j += colStride, k += colStride) {
300 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
301 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
302 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
303 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
304 }
305 }
306 else if (datatype == GL_SHORT && comps == 3) {
307 GLuint i, j, k;
308 const GLshort(*rowA)[3] = (const GLshort(*)[3]) srcRowA;
309 const GLshort(*rowB)[3] = (const GLshort(*)[3]) srcRowB;
310 GLshort(*dst)[3] = (GLshort(*)[3]) dstRow;
311 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
312 i++, j += colStride, k += colStride) {
313 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
314 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
315 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
316 }
317 }
318 else if (datatype == GL_SHORT && comps == 2) {
319 GLuint i, j, k;
320 const GLshort(*rowA)[2] = (const GLshort(*)[2]) srcRowA;
321 const GLshort(*rowB)[2] = (const GLshort(*)[2]) srcRowB;
322 GLshort(*dst)[2] = (GLshort(*)[2]) dstRow;
323 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
324 i++, j += colStride, k += colStride) {
325 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
326 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
327 }
328 }
329 else if (datatype == GL_SHORT && comps == 1) {
330 GLuint i, j, k;
331 const GLshort *rowA = (const GLshort *) srcRowA;
332 const GLshort *rowB = (const GLshort *) srcRowB;
333 GLshort *dst = (GLshort *) dstRow;
334 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
335 i++, j += colStride, k += colStride) {
336 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) / 4;
337 }
338 }
339
340 else if (datatype == GL_FLOAT && comps == 4) {
341 GLuint i, j, k;
342 const GLfloat(*rowA)[4] = (const GLfloat(*)[4]) srcRowA;
343 const GLfloat(*rowB)[4] = (const GLfloat(*)[4]) srcRowB;
344 GLfloat(*dst)[4] = (GLfloat(*)[4]) dstRow;
345 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
346 i++, j += colStride, k += colStride) {
347 dst[i][0] = (rowA[j][0] + rowA[k][0] +
348 rowB[j][0] + rowB[k][0]) * 0.25F;
349 dst[i][1] = (rowA[j][1] + rowA[k][1] +
350 rowB[j][1] + rowB[k][1]) * 0.25F;
351 dst[i][2] = (rowA[j][2] + rowA[k][2] +
352 rowB[j][2] + rowB[k][2]) * 0.25F;
353 dst[i][3] = (rowA[j][3] + rowA[k][3] +
354 rowB[j][3] + rowB[k][3]) * 0.25F;
355 }
356 }
357 else if (datatype == GL_FLOAT && comps == 3) {
358 GLuint i, j, k;
359 const GLfloat(*rowA)[3] = (const GLfloat(*)[3]) srcRowA;
360 const GLfloat(*rowB)[3] = (const GLfloat(*)[3]) srcRowB;
361 GLfloat(*dst)[3] = (GLfloat(*)[3]) dstRow;
362 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
363 i++, j += colStride, k += colStride) {
364 dst[i][0] = (rowA[j][0] + rowA[k][0] +
365 rowB[j][0] + rowB[k][0]) * 0.25F;
366 dst[i][1] = (rowA[j][1] + rowA[k][1] +
367 rowB[j][1] + rowB[k][1]) * 0.25F;
368 dst[i][2] = (rowA[j][2] + rowA[k][2] +
369 rowB[j][2] + rowB[k][2]) * 0.25F;
370 }
371 }
372 else if (datatype == GL_FLOAT && comps == 2) {
373 GLuint i, j, k;
374 const GLfloat(*rowA)[2] = (const GLfloat(*)[2]) srcRowA;
375 const GLfloat(*rowB)[2] = (const GLfloat(*)[2]) srcRowB;
376 GLfloat(*dst)[2] = (GLfloat(*)[2]) dstRow;
377 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
378 i++, j += colStride, k += colStride) {
379 dst[i][0] = (rowA[j][0] + rowA[k][0] +
380 rowB[j][0] + rowB[k][0]) * 0.25F;
381 dst[i][1] = (rowA[j][1] + rowA[k][1] +
382 rowB[j][1] + rowB[k][1]) * 0.25F;
383 }
384 }
385 else if (datatype == GL_FLOAT && comps == 1) {
386 GLuint i, j, k;
387 const GLfloat *rowA = (const GLfloat *) srcRowA;
388 const GLfloat *rowB = (const GLfloat *) srcRowB;
389 GLfloat *dst = (GLfloat *) dstRow;
390 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
391 i++, j += colStride, k += colStride) {
392 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) * 0.25F;
393 }
394 }
395
396 else if (datatype == GL_HALF_FLOAT_ARB && comps == 4) {
397 GLuint i, j, k, comp;
398 const GLhalfARB(*rowA)[4] = (const GLhalfARB(*)[4]) srcRowA;
399 const GLhalfARB(*rowB)[4] = (const GLhalfARB(*)[4]) srcRowB;
400 GLhalfARB(*dst)[4] = (GLhalfARB(*)[4]) dstRow;
401 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
402 i++, j += colStride, k += colStride) {
403 for (comp = 0; comp < 4; comp++) {
404 GLfloat aj, ak, bj, bk;
405 aj = _mesa_half_to_float(rowA[j][comp]);
406 ak = _mesa_half_to_float(rowA[k][comp]);
407 bj = _mesa_half_to_float(rowB[j][comp]);
408 bk = _mesa_half_to_float(rowB[k][comp]);
409 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
410 }
411 }
412 }
413 else if (datatype == GL_HALF_FLOAT_ARB && comps == 3) {
414 GLuint i, j, k, comp;
415 const GLhalfARB(*rowA)[3] = (const GLhalfARB(*)[3]) srcRowA;
416 const GLhalfARB(*rowB)[3] = (const GLhalfARB(*)[3]) srcRowB;
417 GLhalfARB(*dst)[3] = (GLhalfARB(*)[3]) dstRow;
418 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
419 i++, j += colStride, k += colStride) {
420 for (comp = 0; comp < 3; comp++) {
421 GLfloat aj, ak, bj, bk;
422 aj = _mesa_half_to_float(rowA[j][comp]);
423 ak = _mesa_half_to_float(rowA[k][comp]);
424 bj = _mesa_half_to_float(rowB[j][comp]);
425 bk = _mesa_half_to_float(rowB[k][comp]);
426 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
427 }
428 }
429 }
430 else if (datatype == GL_HALF_FLOAT_ARB && comps == 2) {
431 GLuint i, j, k, comp;
432 const GLhalfARB(*rowA)[2] = (const GLhalfARB(*)[2]) srcRowA;
433 const GLhalfARB(*rowB)[2] = (const GLhalfARB(*)[2]) srcRowB;
434 GLhalfARB(*dst)[2] = (GLhalfARB(*)[2]) dstRow;
435 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
436 i++, j += colStride, k += colStride) {
437 for (comp = 0; comp < 2; comp++) {
438 GLfloat aj, ak, bj, bk;
439 aj = _mesa_half_to_float(rowA[j][comp]);
440 ak = _mesa_half_to_float(rowA[k][comp]);
441 bj = _mesa_half_to_float(rowB[j][comp]);
442 bk = _mesa_half_to_float(rowB[k][comp]);
443 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
444 }
445 }
446 }
447 else if (datatype == GL_HALF_FLOAT_ARB && comps == 1) {
448 GLuint i, j, k;
449 const GLhalfARB *rowA = (const GLhalfARB *) srcRowA;
450 const GLhalfARB *rowB = (const GLhalfARB *) srcRowB;
451 GLhalfARB *dst = (GLhalfARB *) dstRow;
452 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
453 i++, j += colStride, k += colStride) {
454 GLfloat aj, ak, bj, bk;
455 aj = _mesa_half_to_float(rowA[j]);
456 ak = _mesa_half_to_float(rowA[k]);
457 bj = _mesa_half_to_float(rowB[j]);
458 bk = _mesa_half_to_float(rowB[k]);
459 dst[i] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
460 }
461 }
462
463 else if (datatype == GL_UNSIGNED_INT && comps == 1) {
464 GLuint i, j, k;
465 const GLuint *rowA = (const GLuint *) srcRowA;
466 const GLuint *rowB = (const GLuint *) srcRowB;
467 GLuint *dst = (GLuint *) dstRow;
468 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
469 i++, j += colStride, k += colStride) {
470 dst[i] = (GLfloat)(rowA[j] / 4 + rowA[k] / 4 + rowB[j] / 4 + rowB[k] / 4);
471 }
472 }
473
474 else if (datatype == GL_UNSIGNED_SHORT_5_6_5 && comps == 3) {
475 GLuint i, j, k;
476 const GLushort *rowA = (const GLushort *) srcRowA;
477 const GLushort *rowB = (const GLushort *) srcRowB;
478 GLushort *dst = (GLushort *) dstRow;
479 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
480 i++, j += colStride, k += colStride) {
481 const GLint rowAr0 = rowA[j] & 0x1f;
482 const GLint rowAr1 = rowA[k] & 0x1f;
483 const GLint rowBr0 = rowB[j] & 0x1f;
484 const GLint rowBr1 = rowB[k] & 0x1f;
485 const GLint rowAg0 = (rowA[j] >> 5) & 0x3f;
486 const GLint rowAg1 = (rowA[k] >> 5) & 0x3f;
487 const GLint rowBg0 = (rowB[j] >> 5) & 0x3f;
488 const GLint rowBg1 = (rowB[k] >> 5) & 0x3f;
489 const GLint rowAb0 = (rowA[j] >> 11) & 0x1f;
490 const GLint rowAb1 = (rowA[k] >> 11) & 0x1f;
491 const GLint rowBb0 = (rowB[j] >> 11) & 0x1f;
492 const GLint rowBb1 = (rowB[k] >> 11) & 0x1f;
493 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
494 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
495 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
496 dst[i] = (blue << 11) | (green << 5) | red;
497 }
498 }
499 else if (datatype == GL_UNSIGNED_SHORT_4_4_4_4 && comps == 4) {
500 GLuint i, j, k;
501 const GLushort *rowA = (const GLushort *) srcRowA;
502 const GLushort *rowB = (const GLushort *) srcRowB;
503 GLushort *dst = (GLushort *) dstRow;
504 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
505 i++, j += colStride, k += colStride) {
506 const GLint rowAr0 = rowA[j] & 0xf;
507 const GLint rowAr1 = rowA[k] & 0xf;
508 const GLint rowBr0 = rowB[j] & 0xf;
509 const GLint rowBr1 = rowB[k] & 0xf;
510 const GLint rowAg0 = (rowA[j] >> 4) & 0xf;
511 const GLint rowAg1 = (rowA[k] >> 4) & 0xf;
512 const GLint rowBg0 = (rowB[j] >> 4) & 0xf;
513 const GLint rowBg1 = (rowB[k] >> 4) & 0xf;
514 const GLint rowAb0 = (rowA[j] >> 8) & 0xf;
515 const GLint rowAb1 = (rowA[k] >> 8) & 0xf;
516 const GLint rowBb0 = (rowB[j] >> 8) & 0xf;
517 const GLint rowBb1 = (rowB[k] >> 8) & 0xf;
518 const GLint rowAa0 = (rowA[j] >> 12) & 0xf;
519 const GLint rowAa1 = (rowA[k] >> 12) & 0xf;
520 const GLint rowBa0 = (rowB[j] >> 12) & 0xf;
521 const GLint rowBa1 = (rowB[k] >> 12) & 0xf;
522 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
523 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
524 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
525 const GLint alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
526 dst[i] = (alpha << 12) | (blue << 8) | (green << 4) | red;
527 }
528 }
529 else if (datatype == GL_UNSIGNED_SHORT_1_5_5_5_REV && comps == 4) {
530 GLuint i, j, k;
531 const GLushort *rowA = (const GLushort *) srcRowA;
532 const GLushort *rowB = (const GLushort *) srcRowB;
533 GLushort *dst = (GLushort *) dstRow;
534 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
535 i++, j += colStride, k += colStride) {
536 const GLint rowAr0 = rowA[j] & 0x1f;
537 const GLint rowAr1 = rowA[k] & 0x1f;
538 const GLint rowBr0 = rowB[j] & 0x1f;
539 const GLint rowBr1 = rowB[k] & 0x1f;
540 const GLint rowAg0 = (rowA[j] >> 5) & 0x1f;
541 const GLint rowAg1 = (rowA[k] >> 5) & 0x1f;
542 const GLint rowBg0 = (rowB[j] >> 5) & 0x1f;
543 const GLint rowBg1 = (rowB[k] >> 5) & 0x1f;
544 const GLint rowAb0 = (rowA[j] >> 10) & 0x1f;
545 const GLint rowAb1 = (rowA[k] >> 10) & 0x1f;
546 const GLint rowBb0 = (rowB[j] >> 10) & 0x1f;
547 const GLint rowBb1 = (rowB[k] >> 10) & 0x1f;
548 const GLint rowAa0 = (rowA[j] >> 15) & 0x1;
549 const GLint rowAa1 = (rowA[k] >> 15) & 0x1;
550 const GLint rowBa0 = (rowB[j] >> 15) & 0x1;
551 const GLint rowBa1 = (rowB[k] >> 15) & 0x1;
552 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
553 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
554 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
555 const GLint alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
556 dst[i] = (alpha << 15) | (blue << 10) | (green << 5) | red;
557 }
558 }
559 else if (datatype == GL_UNSIGNED_SHORT_5_5_5_1 && comps == 4) {
560 GLuint i, j, k;
561 const GLushort *rowA = (const GLushort *) srcRowA;
562 const GLushort *rowB = (const GLushort *) srcRowB;
563 GLushort *dst = (GLushort *) dstRow;
564 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
565 i++, j += colStride, k += colStride) {
566 const GLint rowAr0 = (rowA[j] >> 11) & 0x1f;
567 const GLint rowAr1 = (rowA[k] >> 11) & 0x1f;
568 const GLint rowBr0 = (rowB[j] >> 11) & 0x1f;
569 const GLint rowBr1 = (rowB[k] >> 11) & 0x1f;
570 const GLint rowAg0 = (rowA[j] >> 6) & 0x1f;
571 const GLint rowAg1 = (rowA[k] >> 6) & 0x1f;
572 const GLint rowBg0 = (rowB[j] >> 6) & 0x1f;
573 const GLint rowBg1 = (rowB[k] >> 6) & 0x1f;
574 const GLint rowAb0 = (rowA[j] >> 1) & 0x1f;
575 const GLint rowAb1 = (rowA[k] >> 1) & 0x1f;
576 const GLint rowBb0 = (rowB[j] >> 1) & 0x1f;
577 const GLint rowBb1 = (rowB[k] >> 1) & 0x1f;
578 const GLint rowAa0 = (rowA[j] & 0x1);
579 const GLint rowAa1 = (rowA[k] & 0x1);
580 const GLint rowBa0 = (rowB[j] & 0x1);
581 const GLint rowBa1 = (rowB[k] & 0x1);
582 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
583 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
584 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
585 const GLint alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
586 dst[i] = (red << 11) | (green << 6) | (blue << 1) | alpha;
587 }
588 }
589
590 else if (datatype == GL_UNSIGNED_BYTE_3_3_2 && comps == 3) {
591 GLuint i, j, k;
592 const GLubyte *rowA = (const GLubyte *) srcRowA;
593 const GLubyte *rowB = (const GLubyte *) srcRowB;
594 GLubyte *dst = (GLubyte *) dstRow;
595 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
596 i++, j += colStride, k += colStride) {
597 const GLint rowAr0 = rowA[j] & 0x3;
598 const GLint rowAr1 = rowA[k] & 0x3;
599 const GLint rowBr0 = rowB[j] & 0x3;
600 const GLint rowBr1 = rowB[k] & 0x3;
601 const GLint rowAg0 = (rowA[j] >> 2) & 0x7;
602 const GLint rowAg1 = (rowA[k] >> 2) & 0x7;
603 const GLint rowBg0 = (rowB[j] >> 2) & 0x7;
604 const GLint rowBg1 = (rowB[k] >> 2) & 0x7;
605 const GLint rowAb0 = (rowA[j] >> 5) & 0x7;
606 const GLint rowAb1 = (rowA[k] >> 5) & 0x7;
607 const GLint rowBb0 = (rowB[j] >> 5) & 0x7;
608 const GLint rowBb1 = (rowB[k] >> 5) & 0x7;
609 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
610 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
611 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
612 dst[i] = (blue << 5) | (green << 2) | red;
613 }
614 }
615
616 else if (datatype == MESA_UNSIGNED_BYTE_4_4 && comps == 2) {
617 GLuint i, j, k;
618 const GLubyte *rowA = (const GLubyte *) srcRowA;
619 const GLubyte *rowB = (const GLubyte *) srcRowB;
620 GLubyte *dst = (GLubyte *) dstRow;
621 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
622 i++, j += colStride, k += colStride) {
623 const GLint rowAr0 = rowA[j] & 0xf;
624 const GLint rowAr1 = rowA[k] & 0xf;
625 const GLint rowBr0 = rowB[j] & 0xf;
626 const GLint rowBr1 = rowB[k] & 0xf;
627 const GLint rowAg0 = (rowA[j] >> 4) & 0xf;
628 const GLint rowAg1 = (rowA[k] >> 4) & 0xf;
629 const GLint rowBg0 = (rowB[j] >> 4) & 0xf;
630 const GLint rowBg1 = (rowB[k] >> 4) & 0xf;
631 const GLint r = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
632 const GLint g = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
633 dst[i] = (g << 4) | r;
634 }
635 }
636
637 else {
638 _mesa_problem(NULL, "bad format in do_row()");
639 }
640 }
641
642
643 /**
644 * Average together four rows of a source image to produce a single new
645 * row in the dest image. It's legal for the two source rows to point
646 * to the same data. The source width must be equal to either the
647 * dest width or two times the dest width.
648 *
649 * \param datatype GL pixel type \c GL_UNSIGNED_BYTE, \c GL_UNSIGNED_SHORT,
650 * \c GL_FLOAT, etc.
651 * \param comps number of components per pixel (1..4)
652 * \param srcWidth Width of a row in the source data
653 * \param srcRowA Pointer to one of the rows of source data
654 * \param srcRowB Pointer to one of the rows of source data
655 * \param srcRowC Pointer to one of the rows of source data
656 * \param srcRowD Pointer to one of the rows of source data
657 * \param dstWidth Width of a row in the destination data
658 * \param srcRowA Pointer to the row of destination data
659 */
660 static void
661 do_row_3D(GLenum datatype, GLuint comps, GLint srcWidth,
662 const GLvoid *srcRowA, const GLvoid *srcRowB,
663 const GLvoid *srcRowC, const GLvoid *srcRowD,
664 GLint dstWidth, GLvoid *dstRow)
665 {
666 const GLuint k0 = (srcWidth == dstWidth) ? 0 : 1;
667 const GLuint colStride = (srcWidth == dstWidth) ? 1 : 2;
668 GLuint i, j, k;
669
670 ASSERT(comps >= 1);
671 ASSERT(comps <= 4);
672
673 if ((datatype == GL_UNSIGNED_BYTE) && (comps == 4)) {
674 DECLARE_ROW_POINTERS(GLubyte, 4);
675
676 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
677 i++, j += colStride, k += colStride) {
678 FILTER_3D(0);
679 FILTER_3D(1);
680 FILTER_3D(2);
681 FILTER_3D(3);
682 }
683 }
684 else if ((datatype == GL_UNSIGNED_BYTE) && (comps == 3)) {
685 DECLARE_ROW_POINTERS(GLubyte, 3);
686
687 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
688 i++, j += colStride, k += colStride) {
689 FILTER_3D(0);
690 FILTER_3D(1);
691 FILTER_3D(2);
692 }
693 }
694 else if ((datatype == GL_UNSIGNED_BYTE) && (comps == 2)) {
695 DECLARE_ROW_POINTERS(GLubyte, 2);
696
697 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
698 i++, j += colStride, k += colStride) {
699 FILTER_3D(0);
700 FILTER_3D(1);
701 }
702 }
703 else if ((datatype == GL_UNSIGNED_BYTE) && (comps == 1)) {
704 DECLARE_ROW_POINTERS(GLubyte, 1);
705
706 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
707 i++, j += colStride, k += colStride) {
708 FILTER_3D(0);
709 }
710 }
711 else if ((datatype == GL_BYTE) && (comps == 4)) {
712 DECLARE_ROW_POINTERS(GLbyte, 4);
713
714 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
715 i++, j += colStride, k += colStride) {
716 FILTER_3D_SIGNED(0);
717 FILTER_3D_SIGNED(1);
718 FILTER_3D_SIGNED(2);
719 FILTER_3D_SIGNED(3);
720 }
721 }
722 else if ((datatype == GL_BYTE) && (comps == 3)) {
723 DECLARE_ROW_POINTERS(GLbyte, 3);
724
725 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
726 i++, j += colStride, k += colStride) {
727 FILTER_3D_SIGNED(0);
728 FILTER_3D_SIGNED(1);
729 FILTER_3D_SIGNED(2);
730 }
731 }
732 else if ((datatype == GL_BYTE) && (comps == 2)) {
733 DECLARE_ROW_POINTERS(GLbyte, 2);
734
735 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
736 i++, j += colStride, k += colStride) {
737 FILTER_3D_SIGNED(0);
738 FILTER_3D_SIGNED(1);
739 }
740 }
741 else if ((datatype == GL_BYTE) && (comps == 1)) {
742 DECLARE_ROW_POINTERS(GLbyte, 1);
743
744 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
745 i++, j += colStride, k += colStride) {
746 FILTER_3D_SIGNED(0);
747 }
748 }
749 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 4)) {
750 DECLARE_ROW_POINTERS(GLushort, 4);
751
752 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
753 i++, j += colStride, k += colStride) {
754 FILTER_3D(0);
755 FILTER_3D(1);
756 FILTER_3D(2);
757 FILTER_3D(3);
758 }
759 }
760 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 3)) {
761 DECLARE_ROW_POINTERS(GLushort, 3);
762
763 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
764 i++, j += colStride, k += colStride) {
765 FILTER_3D(0);
766 FILTER_3D(1);
767 FILTER_3D(2);
768 }
769 }
770 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 2)) {
771 DECLARE_ROW_POINTERS(GLushort, 2);
772
773 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
774 i++, j += colStride, k += colStride) {
775 FILTER_3D(0);
776 FILTER_3D(1);
777 }
778 }
779 else if ((datatype == GL_UNSIGNED_SHORT) && (comps == 1)) {
780 DECLARE_ROW_POINTERS(GLushort, 1);
781
782 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
783 i++, j += colStride, k += colStride) {
784 FILTER_3D(0);
785 }
786 }
787 else if ((datatype == GL_SHORT) && (comps == 4)) {
788 DECLARE_ROW_POINTERS(GLshort, 4);
789
790 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
791 i++, j += colStride, k += colStride) {
792 FILTER_3D(0);
793 FILTER_3D(1);
794 FILTER_3D(2);
795 FILTER_3D(3);
796 }
797 }
798 else if ((datatype == GL_SHORT) && (comps == 3)) {
799 DECLARE_ROW_POINTERS(GLshort, 3);
800
801 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
802 i++, j += colStride, k += colStride) {
803 FILTER_3D(0);
804 FILTER_3D(1);
805 FILTER_3D(2);
806 }
807 }
808 else if ((datatype == GL_SHORT) && (comps == 2)) {
809 DECLARE_ROW_POINTERS(GLshort, 2);
810
811 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
812 i++, j += colStride, k += colStride) {
813 FILTER_3D(0);
814 FILTER_3D(1);
815 }
816 }
817 else if ((datatype == GL_SHORT) && (comps == 1)) {
818 DECLARE_ROW_POINTERS(GLshort, 1);
819
820 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
821 i++, j += colStride, k += colStride) {
822 FILTER_3D(0);
823 }
824 }
825 else if ((datatype == GL_FLOAT) && (comps == 4)) {
826 DECLARE_ROW_POINTERS(GLfloat, 4);
827
828 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
829 i++, j += colStride, k += colStride) {
830 FILTER_F_3D(0);
831 FILTER_F_3D(1);
832 FILTER_F_3D(2);
833 FILTER_F_3D(3);
834 }
835 }
836 else if ((datatype == GL_FLOAT) && (comps == 3)) {
837 DECLARE_ROW_POINTERS(GLfloat, 3);
838
839 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
840 i++, j += colStride, k += colStride) {
841 FILTER_F_3D(0);
842 FILTER_F_3D(1);
843 FILTER_F_3D(2);
844 }
845 }
846 else if ((datatype == GL_FLOAT) && (comps == 2)) {
847 DECLARE_ROW_POINTERS(GLfloat, 2);
848
849 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
850 i++, j += colStride, k += colStride) {
851 FILTER_F_3D(0);
852 FILTER_F_3D(1);
853 }
854 }
855 else if ((datatype == GL_FLOAT) && (comps == 1)) {
856 DECLARE_ROW_POINTERS(GLfloat, 1);
857
858 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
859 i++, j += colStride, k += colStride) {
860 FILTER_F_3D(0);
861 }
862 }
863 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 4)) {
864 DECLARE_ROW_POINTERS(GLhalfARB, 4);
865
866 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
867 i++, j += colStride, k += colStride) {
868 FILTER_HF_3D(0);
869 FILTER_HF_3D(1);
870 FILTER_HF_3D(2);
871 FILTER_HF_3D(3);
872 }
873 }
874 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 3)) {
875 DECLARE_ROW_POINTERS(GLhalfARB, 4);
876
877 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
878 i++, j += colStride, k += colStride) {
879 FILTER_HF_3D(0);
880 FILTER_HF_3D(1);
881 FILTER_HF_3D(2);
882 }
883 }
884 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 2)) {
885 DECLARE_ROW_POINTERS(GLhalfARB, 4);
886
887 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
888 i++, j += colStride, k += colStride) {
889 FILTER_HF_3D(0);
890 FILTER_HF_3D(1);
891 }
892 }
893 else if ((datatype == GL_HALF_FLOAT_ARB) && (comps == 1)) {
894 DECLARE_ROW_POINTERS(GLhalfARB, 4);
895
896 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
897 i++, j += colStride, k += colStride) {
898 FILTER_HF_3D(0);
899 }
900 }
901 else if ((datatype == GL_UNSIGNED_INT) && (comps == 1)) {
902 const GLuint *rowA = (const GLuint *) srcRowA;
903 const GLuint *rowB = (const GLuint *) srcRowB;
904 const GLuint *rowC = (const GLuint *) srcRowC;
905 const GLuint *rowD = (const GLuint *) srcRowD;
906 GLfloat *dst = (GLfloat *) dstRow;
907
908 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
909 i++, j += colStride, k += colStride) {
910 const uint64_t tmp = (((uint64_t) rowA[j] + (uint64_t) rowA[k])
911 + ((uint64_t) rowB[j] + (uint64_t) rowB[k])
912 + ((uint64_t) rowC[j] + (uint64_t) rowC[k])
913 + ((uint64_t) rowD[j] + (uint64_t) rowD[k]));
914 dst[i] = (GLfloat)((double) tmp * 0.125);
915 }
916 }
917 else if ((datatype == GL_UNSIGNED_SHORT_5_6_5) && (comps == 3)) {
918 DECLARE_ROW_POINTERS0(GLushort);
919
920 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
921 i++, j += colStride, k += colStride) {
922 const GLint rowAr0 = rowA[j] & 0x1f;
923 const GLint rowAr1 = rowA[k] & 0x1f;
924 const GLint rowBr0 = rowB[j] & 0x1f;
925 const GLint rowBr1 = rowB[k] & 0x1f;
926 const GLint rowCr0 = rowC[j] & 0x1f;
927 const GLint rowCr1 = rowC[k] & 0x1f;
928 const GLint rowDr0 = rowD[j] & 0x1f;
929 const GLint rowDr1 = rowD[k] & 0x1f;
930 const GLint rowAg0 = (rowA[j] >> 5) & 0x3f;
931 const GLint rowAg1 = (rowA[k] >> 5) & 0x3f;
932 const GLint rowBg0 = (rowB[j] >> 5) & 0x3f;
933 const GLint rowBg1 = (rowB[k] >> 5) & 0x3f;
934 const GLint rowCg0 = (rowC[j] >> 5) & 0x3f;
935 const GLint rowCg1 = (rowC[k] >> 5) & 0x3f;
936 const GLint rowDg0 = (rowD[j] >> 5) & 0x3f;
937 const GLint rowDg1 = (rowD[k] >> 5) & 0x3f;
938 const GLint rowAb0 = (rowA[j] >> 11) & 0x1f;
939 const GLint rowAb1 = (rowA[k] >> 11) & 0x1f;
940 const GLint rowBb0 = (rowB[j] >> 11) & 0x1f;
941 const GLint rowBb1 = (rowB[k] >> 11) & 0x1f;
942 const GLint rowCb0 = (rowC[j] >> 11) & 0x1f;
943 const GLint rowCb1 = (rowC[k] >> 11) & 0x1f;
944 const GLint rowDb0 = (rowD[j] >> 11) & 0x1f;
945 const GLint rowDb1 = (rowD[k] >> 11) & 0x1f;
946 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
947 rowCr0, rowCr1, rowDr0, rowDr1);
948 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
949 rowCg0, rowCg1, rowDg0, rowDg1);
950 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
951 rowCb0, rowCb1, rowDb0, rowDb1);
952 dst[i] = (b << 11) | (g << 5) | r;
953 }
954 }
955 else if ((datatype == GL_UNSIGNED_SHORT_4_4_4_4) && (comps == 4)) {
956 DECLARE_ROW_POINTERS0(GLushort);
957
958 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
959 i++, j += colStride, k += colStride) {
960 const GLint rowAr0 = rowA[j] & 0xf;
961 const GLint rowAr1 = rowA[k] & 0xf;
962 const GLint rowBr0 = rowB[j] & 0xf;
963 const GLint rowBr1 = rowB[k] & 0xf;
964 const GLint rowCr0 = rowC[j] & 0xf;
965 const GLint rowCr1 = rowC[k] & 0xf;
966 const GLint rowDr0 = rowD[j] & 0xf;
967 const GLint rowDr1 = rowD[k] & 0xf;
968 const GLint rowAg0 = (rowA[j] >> 4) & 0xf;
969 const GLint rowAg1 = (rowA[k] >> 4) & 0xf;
970 const GLint rowBg0 = (rowB[j] >> 4) & 0xf;
971 const GLint rowBg1 = (rowB[k] >> 4) & 0xf;
972 const GLint rowCg0 = (rowC[j] >> 4) & 0xf;
973 const GLint rowCg1 = (rowC[k] >> 4) & 0xf;
974 const GLint rowDg0 = (rowD[j] >> 4) & 0xf;
975 const GLint rowDg1 = (rowD[k] >> 4) & 0xf;
976 const GLint rowAb0 = (rowA[j] >> 8) & 0xf;
977 const GLint rowAb1 = (rowA[k] >> 8) & 0xf;
978 const GLint rowBb0 = (rowB[j] >> 8) & 0xf;
979 const GLint rowBb1 = (rowB[k] >> 8) & 0xf;
980 const GLint rowCb0 = (rowC[j] >> 8) & 0xf;
981 const GLint rowCb1 = (rowC[k] >> 8) & 0xf;
982 const GLint rowDb0 = (rowD[j] >> 8) & 0xf;
983 const GLint rowDb1 = (rowD[k] >> 8) & 0xf;
984 const GLint rowAa0 = (rowA[j] >> 12) & 0xf;
985 const GLint rowAa1 = (rowA[k] >> 12) & 0xf;
986 const GLint rowBa0 = (rowB[j] >> 12) & 0xf;
987 const GLint rowBa1 = (rowB[k] >> 12) & 0xf;
988 const GLint rowCa0 = (rowC[j] >> 12) & 0xf;
989 const GLint rowCa1 = (rowC[k] >> 12) & 0xf;
990 const GLint rowDa0 = (rowD[j] >> 12) & 0xf;
991 const GLint rowDa1 = (rowD[k] >> 12) & 0xf;
992 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
993 rowCr0, rowCr1, rowDr0, rowDr1);
994 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
995 rowCg0, rowCg1, rowDg0, rowDg1);
996 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
997 rowCb0, rowCb1, rowDb0, rowDb1);
998 const GLint a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
999 rowCa0, rowCa1, rowDa0, rowDa1);
1000
1001 dst[i] = (a << 12) | (b << 8) | (g << 4) | r;
1002 }
1003 }
1004 else if ((datatype == GL_UNSIGNED_SHORT_1_5_5_5_REV) && (comps == 4)) {
1005 DECLARE_ROW_POINTERS0(GLushort);
1006
1007 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
1008 i++, j += colStride, k += colStride) {
1009 const GLint rowAr0 = rowA[j] & 0x1f;
1010 const GLint rowAr1 = rowA[k] & 0x1f;
1011 const GLint rowBr0 = rowB[j] & 0x1f;
1012 const GLint rowBr1 = rowB[k] & 0x1f;
1013 const GLint rowCr0 = rowC[j] & 0x1f;
1014 const GLint rowCr1 = rowC[k] & 0x1f;
1015 const GLint rowDr0 = rowD[j] & 0x1f;
1016 const GLint rowDr1 = rowD[k] & 0x1f;
1017 const GLint rowAg0 = (rowA[j] >> 5) & 0x1f;
1018 const GLint rowAg1 = (rowA[k] >> 5) & 0x1f;
1019 const GLint rowBg0 = (rowB[j] >> 5) & 0x1f;
1020 const GLint rowBg1 = (rowB[k] >> 5) & 0x1f;
1021 const GLint rowCg0 = (rowC[j] >> 5) & 0x1f;
1022 const GLint rowCg1 = (rowC[k] >> 5) & 0x1f;
1023 const GLint rowDg0 = (rowD[j] >> 5) & 0x1f;
1024 const GLint rowDg1 = (rowD[k] >> 5) & 0x1f;
1025 const GLint rowAb0 = (rowA[j] >> 10) & 0x1f;
1026 const GLint rowAb1 = (rowA[k] >> 10) & 0x1f;
1027 const GLint rowBb0 = (rowB[j] >> 10) & 0x1f;
1028 const GLint rowBb1 = (rowB[k] >> 10) & 0x1f;
1029 const GLint rowCb0 = (rowC[j] >> 10) & 0x1f;
1030 const GLint rowCb1 = (rowC[k] >> 10) & 0x1f;
1031 const GLint rowDb0 = (rowD[j] >> 10) & 0x1f;
1032 const GLint rowDb1 = (rowD[k] >> 10) & 0x1f;
1033 const GLint rowAa0 = (rowA[j] >> 15) & 0x1;
1034 const GLint rowAa1 = (rowA[k] >> 15) & 0x1;
1035 const GLint rowBa0 = (rowB[j] >> 15) & 0x1;
1036 const GLint rowBa1 = (rowB[k] >> 15) & 0x1;
1037 const GLint rowCa0 = (rowC[j] >> 15) & 0x1;
1038 const GLint rowCa1 = (rowC[k] >> 15) & 0x1;
1039 const GLint rowDa0 = (rowD[j] >> 15) & 0x1;
1040 const GLint rowDa1 = (rowD[k] >> 15) & 0x1;
1041 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
1042 rowCr0, rowCr1, rowDr0, rowDr1);
1043 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
1044 rowCg0, rowCg1, rowDg0, rowDg1);
1045 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
1046 rowCb0, rowCb1, rowDb0, rowDb1);
1047 const GLint a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
1048 rowCa0, rowCa1, rowDa0, rowDa1);
1049
1050 dst[i] = (a << 15) | (b << 10) | (g << 5) | r;
1051 }
1052 }
1053 else if ((datatype == GL_UNSIGNED_SHORT_5_5_5_1) && (comps == 4)) {
1054 DECLARE_ROW_POINTERS0(GLushort);
1055
1056 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
1057 i++, j += colStride, k += colStride) {
1058 const GLint rowAr0 = (rowA[j] >> 11) & 0x1f;
1059 const GLint rowAr1 = (rowA[k] >> 11) & 0x1f;
1060 const GLint rowBr0 = (rowB[j] >> 11) & 0x1f;
1061 const GLint rowBr1 = (rowB[k] >> 11) & 0x1f;
1062 const GLint rowCr0 = (rowC[j] >> 11) & 0x1f;
1063 const GLint rowCr1 = (rowC[k] >> 11) & 0x1f;
1064 const GLint rowDr0 = (rowD[j] >> 11) & 0x1f;
1065 const GLint rowDr1 = (rowD[k] >> 11) & 0x1f;
1066 const GLint rowAg0 = (rowA[j] >> 6) & 0x1f;
1067 const GLint rowAg1 = (rowA[k] >> 6) & 0x1f;
1068 const GLint rowBg0 = (rowB[j] >> 6) & 0x1f;
1069 const GLint rowBg1 = (rowB[k] >> 6) & 0x1f;
1070 const GLint rowCg0 = (rowC[j] >> 6) & 0x1f;
1071 const GLint rowCg1 = (rowC[k] >> 6) & 0x1f;
1072 const GLint rowDg0 = (rowD[j] >> 6) & 0x1f;
1073 const GLint rowDg1 = (rowD[k] >> 6) & 0x1f;
1074 const GLint rowAb0 = (rowA[j] >> 1) & 0x1f;
1075 const GLint rowAb1 = (rowA[k] >> 1) & 0x1f;
1076 const GLint rowBb0 = (rowB[j] >> 1) & 0x1f;
1077 const GLint rowBb1 = (rowB[k] >> 1) & 0x1f;
1078 const GLint rowCb0 = (rowC[j] >> 1) & 0x1f;
1079 const GLint rowCb1 = (rowC[k] >> 1) & 0x1f;
1080 const GLint rowDb0 = (rowD[j] >> 1) & 0x1f;
1081 const GLint rowDb1 = (rowD[k] >> 1) & 0x1f;
1082 const GLint rowAa0 = (rowA[j] & 0x1);
1083 const GLint rowAa1 = (rowA[k] & 0x1);
1084 const GLint rowBa0 = (rowB[j] & 0x1);
1085 const GLint rowBa1 = (rowB[k] & 0x1);
1086 const GLint rowCa0 = (rowC[j] & 0x1);
1087 const GLint rowCa1 = (rowC[k] & 0x1);
1088 const GLint rowDa0 = (rowD[j] & 0x1);
1089 const GLint rowDa1 = (rowD[k] & 0x1);
1090 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
1091 rowCr0, rowCr1, rowDr0, rowDr1);
1092 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
1093 rowCg0, rowCg1, rowDg0, rowDg1);
1094 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
1095 rowCb0, rowCb1, rowDb0, rowDb1);
1096 const GLint a = FILTER_SUM_3D(rowAa0, rowAa1, rowBa0, rowBa1,
1097 rowCa0, rowCa1, rowDa0, rowDa1);
1098
1099 dst[i] = (r << 11) | (g << 6) | (b << 1) | a;
1100 }
1101 }
1102 else if ((datatype == GL_UNSIGNED_BYTE_3_3_2) && (comps == 3)) {
1103 DECLARE_ROW_POINTERS0(GLubyte);
1104
1105 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
1106 i++, j += colStride, k += colStride) {
1107 const GLint rowAr0 = rowA[j] & 0x3;
1108 const GLint rowAr1 = rowA[k] & 0x3;
1109 const GLint rowBr0 = rowB[j] & 0x3;
1110 const GLint rowBr1 = rowB[k] & 0x3;
1111 const GLint rowCr0 = rowC[j] & 0x3;
1112 const GLint rowCr1 = rowC[k] & 0x3;
1113 const GLint rowDr0 = rowD[j] & 0x3;
1114 const GLint rowDr1 = rowD[k] & 0x3;
1115 const GLint rowAg0 = (rowA[j] >> 2) & 0x7;
1116 const GLint rowAg1 = (rowA[k] >> 2) & 0x7;
1117 const GLint rowBg0 = (rowB[j] >> 2) & 0x7;
1118 const GLint rowBg1 = (rowB[k] >> 2) & 0x7;
1119 const GLint rowCg0 = (rowC[j] >> 2) & 0x7;
1120 const GLint rowCg1 = (rowC[k] >> 2) & 0x7;
1121 const GLint rowDg0 = (rowD[j] >> 2) & 0x7;
1122 const GLint rowDg1 = (rowD[k] >> 2) & 0x7;
1123 const GLint rowAb0 = (rowA[j] >> 5) & 0x7;
1124 const GLint rowAb1 = (rowA[k] >> 5) & 0x7;
1125 const GLint rowBb0 = (rowB[j] >> 5) & 0x7;
1126 const GLint rowBb1 = (rowB[k] >> 5) & 0x7;
1127 const GLint rowCb0 = (rowC[j] >> 5) & 0x7;
1128 const GLint rowCb1 = (rowC[k] >> 5) & 0x7;
1129 const GLint rowDb0 = (rowD[j] >> 5) & 0x7;
1130 const GLint rowDb1 = (rowD[k] >> 5) & 0x7;
1131 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
1132 rowCr0, rowCr1, rowDr0, rowDr1);
1133 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
1134 rowCg0, rowCg1, rowDg0, rowDg1);
1135 const GLint b = FILTER_SUM_3D(rowAb0, rowAb1, rowBb0, rowBb1,
1136 rowCb0, rowCb1, rowDb0, rowDb1);
1137 dst[i] = (b << 5) | (g << 2) | r;
1138 }
1139 }
1140 else if (datatype == MESA_UNSIGNED_BYTE_4_4 && comps == 2) {
1141 DECLARE_ROW_POINTERS0(GLubyte);
1142
1143 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
1144 i++, j += colStride, k += colStride) {
1145 const GLint rowAr0 = rowA[j] & 0xf;
1146 const GLint rowAr1 = rowA[k] & 0xf;
1147 const GLint rowBr0 = rowB[j] & 0xf;
1148 const GLint rowBr1 = rowB[k] & 0xf;
1149 const GLint rowCr0 = rowC[j] & 0xf;
1150 const GLint rowCr1 = rowC[k] & 0xf;
1151 const GLint rowDr0 = rowD[j] & 0xf;
1152 const GLint rowDr1 = rowD[k] & 0xf;
1153 const GLint rowAg0 = (rowA[j] >> 4) & 0xf;
1154 const GLint rowAg1 = (rowA[k] >> 4) & 0xf;
1155 const GLint rowBg0 = (rowB[j] >> 4) & 0xf;
1156 const GLint rowBg1 = (rowB[k] >> 4) & 0xf;
1157 const GLint rowCg0 = (rowC[j] >> 4) & 0xf;
1158 const GLint rowCg1 = (rowC[k] >> 4) & 0xf;
1159 const GLint rowDg0 = (rowD[j] >> 4) & 0xf;
1160 const GLint rowDg1 = (rowD[k] >> 4) & 0xf;
1161 const GLint r = FILTER_SUM_3D(rowAr0, rowAr1, rowBr0, rowBr1,
1162 rowCr0, rowCr1, rowDr0, rowDr1);
1163 const GLint g = FILTER_SUM_3D(rowAg0, rowAg1, rowBg0, rowBg1,
1164 rowCg0, rowCg1, rowDg0, rowDg1);
1165 dst[i] = (g << 4) | r;
1166 }
1167 }
1168 else {
1169 _mesa_problem(NULL, "bad format in do_row()");
1170 }
1171 }
1172
1173
1174 /*
1175 * These functions generate a 1/2-size mipmap image from a source image.
1176 * Texture borders are handled by copying or averaging the source image's
1177 * border texels, depending on the scale-down factor.
1178 */
1179
1180 static void
1181 make_1d_mipmap(GLenum datatype, GLuint comps, GLint border,
1182 GLint srcWidth, const GLubyte *srcPtr,
1183 GLint dstWidth, GLubyte *dstPtr)
1184 {
1185 const GLint bpt = bytes_per_pixel(datatype, comps);
1186 const GLubyte *src;
1187 GLubyte *dst;
1188
1189 /* skip the border pixel, if any */
1190 src = srcPtr + border * bpt;
1191 dst = dstPtr + border * bpt;
1192
1193 /* we just duplicate the input row, kind of hack, saves code */
1194 do_row(datatype, comps, srcWidth - 2 * border, src, src,
1195 dstWidth - 2 * border, dst);
1196
1197 if (border) {
1198 /* copy left-most pixel from source */
1199 assert(dstPtr);
1200 assert(srcPtr);
1201 memcpy(dstPtr, srcPtr, bpt);
1202 /* copy right-most pixel from source */
1203 memcpy(dstPtr + (dstWidth - 1) * bpt,
1204 srcPtr + (srcWidth - 1) * bpt,
1205 bpt);
1206 }
1207 }
1208
1209
1210 static void
1211 make_2d_mipmap(GLenum datatype, GLuint comps, GLint border,
1212 GLint srcWidth, GLint srcHeight,
1213 const GLubyte *srcPtr, GLint srcRowStride,
1214 GLint dstWidth, GLint dstHeight,
1215 GLubyte *dstPtr, GLint dstRowStride)
1216 {
1217 const GLint bpt = bytes_per_pixel(datatype, comps);
1218 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1219 const GLint dstWidthNB = dstWidth - 2 * border;
1220 const GLint dstHeightNB = dstHeight - 2 * border;
1221 const GLint srcRowBytes = bpt * srcRowStride;
1222 const GLint dstRowBytes = bpt * dstRowStride;
1223 const GLubyte *srcA, *srcB;
1224 GLubyte *dst;
1225 GLint row, srcRowStep;
1226
1227 /* Compute src and dst pointers, skipping any border */
1228 srcA = srcPtr + border * ((srcWidth + 1) * bpt);
1229 if (srcHeight > 1 && srcHeight > dstHeight) {
1230 /* sample from two source rows */
1231 srcB = srcA + srcRowBytes;
1232 srcRowStep = 2;
1233 }
1234 else {
1235 /* sample from one source row */
1236 srcB = srcA;
1237 srcRowStep = 1;
1238 }
1239
1240 dst = dstPtr + border * ((dstWidth + 1) * bpt);
1241
1242 for (row = 0; row < dstHeightNB; row++) {
1243 do_row(datatype, comps, srcWidthNB, srcA, srcB,
1244 dstWidthNB, dst);
1245 srcA += srcRowStep * srcRowBytes;
1246 srcB += srcRowStep * srcRowBytes;
1247 dst += dstRowBytes;
1248 }
1249
1250 /* This is ugly but probably won't be used much */
1251 if (border > 0) {
1252 /* fill in dest border */
1253 /* lower-left border pixel */
1254 assert(dstPtr);
1255 assert(srcPtr);
1256 memcpy(dstPtr, srcPtr, bpt);
1257 /* lower-right border pixel */
1258 memcpy(dstPtr + (dstWidth - 1) * bpt,
1259 srcPtr + (srcWidth - 1) * bpt, bpt);
1260 /* upper-left border pixel */
1261 memcpy(dstPtr + dstWidth * (dstHeight - 1) * bpt,
1262 srcPtr + srcWidth * (srcHeight - 1) * bpt, bpt);
1263 /* upper-right border pixel */
1264 memcpy(dstPtr + (dstWidth * dstHeight - 1) * bpt,
1265 srcPtr + (srcWidth * srcHeight - 1) * bpt, bpt);
1266 /* lower border */
1267 do_row(datatype, comps, srcWidthNB,
1268 srcPtr + bpt,
1269 srcPtr + bpt,
1270 dstWidthNB, dstPtr + bpt);
1271 /* upper border */
1272 do_row(datatype, comps, srcWidthNB,
1273 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1274 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1275 dstWidthNB,
1276 dstPtr + (dstWidth * (dstHeight - 1) + 1) * bpt);
1277 /* left and right borders */
1278 if (srcHeight == dstHeight) {
1279 /* copy border pixel from src to dst */
1280 for (row = 1; row < srcHeight; row++) {
1281 memcpy(dstPtr + dstWidth * row * bpt,
1282 srcPtr + srcWidth * row * bpt, bpt);
1283 memcpy(dstPtr + (dstWidth * row + dstWidth - 1) * bpt,
1284 srcPtr + (srcWidth * row + srcWidth - 1) * bpt, bpt);
1285 }
1286 }
1287 else {
1288 /* average two src pixels each dest pixel */
1289 for (row = 0; row < dstHeightNB; row += 2) {
1290 do_row(datatype, comps, 1,
1291 srcPtr + (srcWidth * (row * 2 + 1)) * bpt,
1292 srcPtr + (srcWidth * (row * 2 + 2)) * bpt,
1293 1, dstPtr + (dstWidth * row + 1) * bpt);
1294 do_row(datatype, comps, 1,
1295 srcPtr + (srcWidth * (row * 2 + 1) + srcWidth - 1) * bpt,
1296 srcPtr + (srcWidth * (row * 2 + 2) + srcWidth - 1) * bpt,
1297 1, dstPtr + (dstWidth * row + 1 + dstWidth - 1) * bpt);
1298 }
1299 }
1300 }
1301 }
1302
1303
1304 static void
1305 make_3d_mipmap(GLenum datatype, GLuint comps, GLint border,
1306 GLint srcWidth, GLint srcHeight, GLint srcDepth,
1307 const GLubyte *srcPtr, GLint srcRowStride,
1308 GLint dstWidth, GLint dstHeight, GLint dstDepth,
1309 GLubyte *dstPtr, GLint dstRowStride)
1310 {
1311 const GLint bpt = bytes_per_pixel(datatype, comps);
1312 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1313 const GLint srcDepthNB = srcDepth - 2 * border;
1314 const GLint dstWidthNB = dstWidth - 2 * border;
1315 const GLint dstHeightNB = dstHeight - 2 * border;
1316 const GLint dstDepthNB = dstDepth - 2 * border;
1317 GLint img, row;
1318 GLint bytesPerSrcImage, bytesPerDstImage;
1319 GLint bytesPerSrcRow, bytesPerDstRow;
1320 GLint srcImageOffset, srcRowOffset;
1321
1322 (void) srcDepthNB; /* silence warnings */
1323
1324
1325 bytesPerSrcImage = srcWidth * srcHeight * bpt;
1326 bytesPerDstImage = dstWidth * dstHeight * bpt;
1327
1328 bytesPerSrcRow = srcWidth * bpt;
1329 bytesPerDstRow = dstWidth * bpt;
1330
1331 /* Offset between adjacent src images to be averaged together */
1332 srcImageOffset = (srcDepth == dstDepth) ? 0 : bytesPerSrcImage;
1333
1334 /* Offset between adjacent src rows to be averaged together */
1335 srcRowOffset = (srcHeight == dstHeight) ? 0 : srcWidth * bpt;
1336
1337 /*
1338 * Need to average together up to 8 src pixels for each dest pixel.
1339 * Break that down into 3 operations:
1340 * 1. take two rows from source image and average them together.
1341 * 2. take two rows from next source image and average them together.
1342 * 3. take the two averaged rows and average them for the final dst row.
1343 */
1344
1345 /*
1346 printf("mip3d %d x %d x %d -> %d x %d x %d\n",
1347 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
1348 */
1349
1350 for (img = 0; img < dstDepthNB; img++) {
1351 /* first source image pointer, skipping border */
1352 const GLubyte *imgSrcA = srcPtr
1353 + (bytesPerSrcImage + bytesPerSrcRow + border) * bpt * border
1354 + img * (bytesPerSrcImage + srcImageOffset);
1355 /* second source image pointer, skipping border */
1356 const GLubyte *imgSrcB = imgSrcA + srcImageOffset;
1357 /* address of the dest image, skipping border */
1358 GLubyte *imgDst = dstPtr
1359 + (bytesPerDstImage + bytesPerDstRow + border) * bpt * border
1360 + img * bytesPerDstImage;
1361
1362 /* setup the four source row pointers and the dest row pointer */
1363 const GLubyte *srcImgARowA = imgSrcA;
1364 const GLubyte *srcImgARowB = imgSrcA + srcRowOffset;
1365 const GLubyte *srcImgBRowA = imgSrcB;
1366 const GLubyte *srcImgBRowB = imgSrcB + srcRowOffset;
1367 GLubyte *dstImgRow = imgDst;
1368
1369 for (row = 0; row < dstHeightNB; row++) {
1370 do_row_3D(datatype, comps, srcWidthNB,
1371 srcImgARowA, srcImgARowB,
1372 srcImgBRowA, srcImgBRowB,
1373 dstWidthNB, dstImgRow);
1374
1375 /* advance to next rows */
1376 srcImgARowA += bytesPerSrcRow + srcRowOffset;
1377 srcImgARowB += bytesPerSrcRow + srcRowOffset;
1378 srcImgBRowA += bytesPerSrcRow + srcRowOffset;
1379 srcImgBRowB += bytesPerSrcRow + srcRowOffset;
1380 dstImgRow += bytesPerDstRow;
1381 }
1382 }
1383
1384
1385 /* Luckily we can leverage the make_2d_mipmap() function here! */
1386 if (border > 0) {
1387 /* do front border image */
1388 make_2d_mipmap(datatype, comps, 1, srcWidth, srcHeight, srcPtr, srcRowStride,
1389 dstWidth, dstHeight, dstPtr, dstRowStride);
1390 /* do back border image */
1391 make_2d_mipmap(datatype, comps, 1, srcWidth, srcHeight,
1392 srcPtr + bytesPerSrcImage * (srcDepth - 1), srcRowStride,
1393 dstWidth, dstHeight,
1394 dstPtr + bytesPerDstImage * (dstDepth - 1), dstRowStride);
1395 /* do four remaining border edges that span the image slices */
1396 if (srcDepth == dstDepth) {
1397 /* just copy border pixels from src to dst */
1398 for (img = 0; img < dstDepthNB; img++) {
1399 const GLubyte *src;
1400 GLubyte *dst;
1401
1402 /* do border along [img][row=0][col=0] */
1403 src = srcPtr + (img + 1) * bytesPerSrcImage;
1404 dst = dstPtr + (img + 1) * bytesPerDstImage;
1405 memcpy(dst, src, bpt);
1406
1407 /* do border along [img][row=dstHeight-1][col=0] */
1408 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1409 + (srcHeight - 1) * bytesPerSrcRow;
1410 dst = dstPtr + (img + 1) * bytesPerDstImage
1411 + (dstHeight - 1) * bytesPerDstRow;
1412 memcpy(dst, src, bpt);
1413
1414 /* do border along [img][row=0][col=dstWidth-1] */
1415 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1416 + (srcWidth - 1) * bpt;
1417 dst = dstPtr + (img + 1) * bytesPerDstImage
1418 + (dstWidth - 1) * bpt;
1419 memcpy(dst, src, bpt);
1420
1421 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1422 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1423 + (bytesPerSrcImage - bpt);
1424 dst = dstPtr + (img + 1) * bytesPerDstImage
1425 + (bytesPerDstImage - bpt);
1426 memcpy(dst, src, bpt);
1427 }
1428 }
1429 else {
1430 /* average border pixels from adjacent src image pairs */
1431 ASSERT(srcDepthNB == 2 * dstDepthNB);
1432 for (img = 0; img < dstDepthNB; img++) {
1433 const GLubyte *src;
1434 GLubyte *dst;
1435
1436 /* do border along [img][row=0][col=0] */
1437 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage;
1438 dst = dstPtr + (img + 1) * bytesPerDstImage;
1439 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1440
1441 /* do border along [img][row=dstHeight-1][col=0] */
1442 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1443 + (srcHeight - 1) * bytesPerSrcRow;
1444 dst = dstPtr + (img + 1) * bytesPerDstImage
1445 + (dstHeight - 1) * bytesPerDstRow;
1446 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1447
1448 /* do border along [img][row=0][col=dstWidth-1] */
1449 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1450 + (srcWidth - 1) * bpt;
1451 dst = dstPtr + (img + 1) * bytesPerDstImage
1452 + (dstWidth - 1) * bpt;
1453 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1454
1455 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
1456 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
1457 + (bytesPerSrcImage - bpt);
1458 dst = dstPtr + (img + 1) * bytesPerDstImage
1459 + (bytesPerDstImage - bpt);
1460 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
1461 }
1462 }
1463 }
1464 }
1465
1466
1467 static void
1468 make_1d_stack_mipmap(GLenum datatype, GLuint comps, GLint border,
1469 GLint srcWidth, const GLubyte *srcPtr, GLuint srcRowStride,
1470 GLint dstWidth, GLint dstHeight,
1471 GLubyte *dstPtr, GLuint dstRowStride )
1472 {
1473 const GLint bpt = bytes_per_pixel(datatype, comps);
1474 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1475 const GLint dstWidthNB = dstWidth - 2 * border;
1476 const GLint dstHeightNB = dstHeight - 2 * border;
1477 const GLint srcRowBytes = bpt * srcRowStride;
1478 const GLint dstRowBytes = bpt * dstRowStride;
1479 const GLubyte *src;
1480 GLubyte *dst;
1481 GLint row;
1482
1483 /* Compute src and dst pointers, skipping any border */
1484 src = srcPtr + border * ((srcWidth + 1) * bpt);
1485 dst = dstPtr + border * ((dstWidth + 1) * bpt);
1486
1487 for (row = 0; row < dstHeightNB; row++) {
1488 do_row(datatype, comps, srcWidthNB, src, src,
1489 dstWidthNB, dst);
1490 src += srcRowBytes;
1491 dst += dstRowBytes;
1492 }
1493
1494 if (border) {
1495 /* copy left-most pixel from source */
1496 assert(dstPtr);
1497 assert(srcPtr);
1498 memcpy(dstPtr, srcPtr, bpt);
1499 /* copy right-most pixel from source */
1500 memcpy(dstPtr + (dstWidth - 1) * bpt,
1501 srcPtr + (srcWidth - 1) * bpt,
1502 bpt);
1503 }
1504 }
1505
1506
1507 /**
1508 * \bug
1509 * There is quite a bit of refactoring that could be done with this function
1510 * and \c make_2d_mipmap.
1511 */
1512 static void
1513 make_2d_stack_mipmap(GLenum datatype, GLuint comps, GLint border,
1514 GLint srcWidth, GLint srcHeight,
1515 const GLubyte *srcPtr, GLint srcRowStride,
1516 GLint dstWidth, GLint dstHeight, GLint dstDepth,
1517 GLubyte *dstPtr, GLint dstRowStride)
1518 {
1519 const GLint bpt = bytes_per_pixel(datatype, comps);
1520 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
1521 const GLint dstWidthNB = dstWidth - 2 * border;
1522 const GLint dstHeightNB = dstHeight - 2 * border;
1523 const GLint dstDepthNB = dstDepth - 2 * border;
1524 const GLint srcRowBytes = bpt * srcRowStride;
1525 const GLint dstRowBytes = bpt * dstRowStride;
1526 const GLubyte *srcA, *srcB;
1527 GLubyte *dst;
1528 GLint layer;
1529 GLint row;
1530
1531 /* Compute src and dst pointers, skipping any border */
1532 srcA = srcPtr + border * ((srcWidth + 1) * bpt);
1533 if (srcHeight > 1)
1534 srcB = srcA + srcRowBytes;
1535 else
1536 srcB = srcA;
1537 dst = dstPtr + border * ((dstWidth + 1) * bpt);
1538
1539 for (layer = 0; layer < dstDepthNB; layer++) {
1540 for (row = 0; row < dstHeightNB; row++) {
1541 do_row(datatype, comps, srcWidthNB, srcA, srcB,
1542 dstWidthNB, dst);
1543 srcA += 2 * srcRowBytes;
1544 srcB += 2 * srcRowBytes;
1545 dst += dstRowBytes;
1546 }
1547
1548 /* This is ugly but probably won't be used much */
1549 if (border > 0) {
1550 /* fill in dest border */
1551 /* lower-left border pixel */
1552 assert(dstPtr);
1553 assert(srcPtr);
1554 memcpy(dstPtr, srcPtr, bpt);
1555 /* lower-right border pixel */
1556 memcpy(dstPtr + (dstWidth - 1) * bpt,
1557 srcPtr + (srcWidth - 1) * bpt, bpt);
1558 /* upper-left border pixel */
1559 memcpy(dstPtr + dstWidth * (dstHeight - 1) * bpt,
1560 srcPtr + srcWidth * (srcHeight - 1) * bpt, bpt);
1561 /* upper-right border pixel */
1562 memcpy(dstPtr + (dstWidth * dstHeight - 1) * bpt,
1563 srcPtr + (srcWidth * srcHeight - 1) * bpt, bpt);
1564 /* lower border */
1565 do_row(datatype, comps, srcWidthNB,
1566 srcPtr + bpt,
1567 srcPtr + bpt,
1568 dstWidthNB, dstPtr + bpt);
1569 /* upper border */
1570 do_row(datatype, comps, srcWidthNB,
1571 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1572 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
1573 dstWidthNB,
1574 dstPtr + (dstWidth * (dstHeight - 1) + 1) * bpt);
1575 /* left and right borders */
1576 if (srcHeight == dstHeight) {
1577 /* copy border pixel from src to dst */
1578 for (row = 1; row < srcHeight; row++) {
1579 memcpy(dstPtr + dstWidth * row * bpt,
1580 srcPtr + srcWidth * row * bpt, bpt);
1581 memcpy(dstPtr + (dstWidth * row + dstWidth - 1) * bpt,
1582 srcPtr + (srcWidth * row + srcWidth - 1) * bpt, bpt);
1583 }
1584 }
1585 else {
1586 /* average two src pixels each dest pixel */
1587 for (row = 0; row < dstHeightNB; row += 2) {
1588 do_row(datatype, comps, 1,
1589 srcPtr + (srcWidth * (row * 2 + 1)) * bpt,
1590 srcPtr + (srcWidth * (row * 2 + 2)) * bpt,
1591 1, dstPtr + (dstWidth * row + 1) * bpt);
1592 do_row(datatype, comps, 1,
1593 srcPtr + (srcWidth * (row * 2 + 1) + srcWidth - 1) * bpt,
1594 srcPtr + (srcWidth * (row * 2 + 2) + srcWidth - 1) * bpt,
1595 1, dstPtr + (dstWidth * row + 1 + dstWidth - 1) * bpt);
1596 }
1597 }
1598 }
1599 }
1600 }
1601
1602
1603 /**
1604 * Down-sample a texture image to produce the next lower mipmap level.
1605 * \param comps components per texel (1, 2, 3 or 4)
1606 * \param srcRowStride stride between source rows, in texels
1607 * \param dstRowStride stride between destination rows, in texels
1608 */
1609 void
1610 _mesa_generate_mipmap_level(GLenum target,
1611 GLenum datatype, GLuint comps,
1612 GLint border,
1613 GLint srcWidth, GLint srcHeight, GLint srcDepth,
1614 const GLubyte *srcData,
1615 GLint srcRowStride,
1616 GLint dstWidth, GLint dstHeight, GLint dstDepth,
1617 GLubyte *dstData,
1618 GLint dstRowStride)
1619 {
1620 /*
1621 * We use simple 2x2 averaging to compute the next mipmap level.
1622 */
1623 switch (target) {
1624 case GL_TEXTURE_1D:
1625 make_1d_mipmap(datatype, comps, border,
1626 srcWidth, srcData,
1627 dstWidth, dstData);
1628 break;
1629 case GL_TEXTURE_2D:
1630 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
1631 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
1632 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
1633 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
1634 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
1635 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
1636 make_2d_mipmap(datatype, comps, border,
1637 srcWidth, srcHeight, srcData, srcRowStride,
1638 dstWidth, dstHeight, dstData, dstRowStride);
1639 break;
1640 case GL_TEXTURE_3D:
1641 make_3d_mipmap(datatype, comps, border,
1642 srcWidth, srcHeight, srcDepth,
1643 srcData, srcRowStride,
1644 dstWidth, dstHeight, dstDepth,
1645 dstData, dstRowStride);
1646 break;
1647 case GL_TEXTURE_1D_ARRAY_EXT:
1648 make_1d_stack_mipmap(datatype, comps, border,
1649 srcWidth, srcData, srcRowStride,
1650 dstWidth, dstHeight,
1651 dstData, dstRowStride);
1652 break;
1653 case GL_TEXTURE_2D_ARRAY_EXT:
1654 make_2d_stack_mipmap(datatype, comps, border,
1655 srcWidth, srcHeight,
1656 srcData, srcRowStride,
1657 dstWidth, dstHeight,
1658 dstDepth, dstData, dstRowStride);
1659 break;
1660 case GL_TEXTURE_RECTANGLE_NV:
1661 /* no mipmaps, do nothing */
1662 break;
1663 default:
1664 _mesa_problem(NULL, "bad dimensions in _mesa_generate_mipmaps");
1665 return;
1666 }
1667 }
1668
1669
1670 /**
1671 * compute next (level+1) image size
1672 * \return GL_FALSE if no smaller size can be generated (eg. src is 1x1x1 size)
1673 */
1674 static GLboolean
1675 next_mipmap_level_size(GLenum target, GLint border,
1676 GLint srcWidth, GLint srcHeight, GLint srcDepth,
1677 GLint *dstWidth, GLint *dstHeight, GLint *dstDepth)
1678 {
1679 if (srcWidth - 2 * border > 1) {
1680 *dstWidth = (srcWidth - 2 * border) / 2 + 2 * border;
1681 }
1682 else {
1683 *dstWidth = srcWidth; /* can't go smaller */
1684 }
1685
1686 if ((srcHeight - 2 * border > 1) &&
1687 (target != GL_TEXTURE_1D_ARRAY_EXT)) {
1688 *dstHeight = (srcHeight - 2 * border) / 2 + 2 * border;
1689 }
1690 else {
1691 *dstHeight = srcHeight; /* can't go smaller */
1692 }
1693
1694 if ((srcDepth - 2 * border > 1) &&
1695 (target != GL_TEXTURE_2D_ARRAY_EXT)) {
1696 *dstDepth = (srcDepth - 2 * border) / 2 + 2 * border;
1697 }
1698 else {
1699 *dstDepth = srcDepth; /* can't go smaller */
1700 }
1701
1702 if (*dstWidth == srcWidth &&
1703 *dstHeight == srcHeight &&
1704 *dstDepth == srcDepth) {
1705 return GL_FALSE;
1706 }
1707 else {
1708 return GL_TRUE;
1709 }
1710 }
1711
1712
1713
1714
1715 /**
1716 * Automatic mipmap generation.
1717 * This is the fallback/default function for ctx->Driver.GenerateMipmap().
1718 * Generate a complete set of mipmaps from texObj's BaseLevel image.
1719 * Stop at texObj's MaxLevel or when we get to the 1x1 texture.
1720 * For cube maps, target will be one of
1721 * GL_TEXTURE_CUBE_MAP_POSITIVE/NEGATIVE_X/Y/Z; never GL_TEXTURE_CUBE_MAP.
1722 */
1723 void
1724 _mesa_generate_mipmap(struct gl_context *ctx, GLenum target,
1725 struct gl_texture_object *texObj)
1726 {
1727 const struct gl_texture_image *srcImage;
1728 gl_format convertFormat;
1729 const GLubyte *srcData = NULL;
1730 GLubyte *dstData = NULL;
1731 GLint level, maxLevels;
1732 GLenum datatype;
1733 GLuint comps;
1734
1735 ASSERT(texObj);
1736 srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
1737 ASSERT(srcImage);
1738
1739 maxLevels = _mesa_max_texture_levels(ctx, texObj->Target);
1740 ASSERT(maxLevels > 0); /* bad target */
1741
1742 /* Find convertFormat - the format that do_row() will process */
1743
1744 if (_mesa_is_format_compressed(srcImage->TexFormat)) {
1745 /* setup for compressed textures - need to allocate temporary
1746 * image buffers to hold uncompressed images.
1747 */
1748 GLuint row;
1749 GLint components, size;
1750 GLchan *dst;
1751
1752 assert(texObj->Target == GL_TEXTURE_2D ||
1753 texObj->Target == GL_TEXTURE_CUBE_MAP_ARB);
1754
1755 if (srcImage->_BaseFormat == GL_RGB) {
1756 convertFormat = MESA_FORMAT_RGB888;
1757 components = 3;
1758 }
1759 else if (srcImage->_BaseFormat == GL_RGBA) {
1760 convertFormat = MESA_FORMAT_RGBA8888;
1761 components = 4;
1762 }
1763 else {
1764 _mesa_problem(ctx, "bad srcImage->_BaseFormat in _mesa_generate_mipmaps");
1765 return;
1766 }
1767
1768 /* allocate storage for uncompressed GL_RGB or GL_RGBA images */
1769 size = _mesa_bytes_per_pixel(srcImage->_BaseFormat, CHAN_TYPE)
1770 * srcImage->Width * srcImage->Height * srcImage->Depth + 20;
1771 /* 20 extra bytes, just be safe when calling last FetchTexel */
1772 srcData = (GLubyte *) malloc(size);
1773 if (!srcData) {
1774 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generate mipmaps");
1775 return;
1776 }
1777 dstData = (GLubyte *) malloc(size / 2); /* 1/4 would probably be OK */
1778 if (!dstData) {
1779 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generate mipmaps");
1780 free((void *) srcData);
1781 return;
1782 }
1783
1784 /* decompress base image here */
1785 dst = (GLchan *) srcData;
1786 for (row = 0; row < srcImage->Height; row++) {
1787 GLuint col;
1788 for (col = 0; col < srcImage->Width; col++) {
1789 srcImage->FetchTexelc(srcImage, col, row, 0, dst);
1790 dst += components;
1791 }
1792 }
1793 }
1794 else {
1795 /* uncompressed */
1796 convertFormat = srcImage->TexFormat;
1797 }
1798
1799 _mesa_format_to_type_and_comps(convertFormat, &datatype, &comps);
1800
1801 for (level = texObj->BaseLevel; level < texObj->MaxLevel
1802 && level < maxLevels - 1; level++) {
1803 /* generate image[level+1] from image[level] */
1804 const struct gl_texture_image *srcImage;
1805 struct gl_texture_image *dstImage;
1806 GLint srcWidth, srcHeight, srcDepth;
1807 GLint dstWidth, dstHeight, dstDepth;
1808 GLint border;
1809 GLboolean nextLevel;
1810
1811 /* get src image parameters */
1812 srcImage = _mesa_select_tex_image(ctx, texObj, target, level);
1813 ASSERT(srcImage);
1814 srcWidth = srcImage->Width;
1815 srcHeight = srcImage->Height;
1816 srcDepth = srcImage->Depth;
1817 border = srcImage->Border;
1818
1819 nextLevel = next_mipmap_level_size(target, border,
1820 srcWidth, srcHeight, srcDepth,
1821 &dstWidth, &dstHeight, &dstDepth);
1822 if (!nextLevel) {
1823 /* all done */
1824 if (_mesa_is_format_compressed(srcImage->TexFormat)) {
1825 free((void *) srcData);
1826 free(dstData);
1827 }
1828 return;
1829 }
1830
1831 /* get dest gl_texture_image */
1832 dstImage = _mesa_get_tex_image(ctx, texObj, target, level + 1);
1833 if (!dstImage) {
1834 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
1835 return;
1836 }
1837
1838 /* Free old image data */
1839 if (dstImage->Data)
1840 ctx->Driver.FreeTexImageData(ctx, dstImage);
1841
1842 /* initialize new image */
1843 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
1844 dstDepth, border, srcImage->InternalFormat,
1845 srcImage->TexFormat);
1846 dstImage->DriverData = NULL;
1847 dstImage->FetchTexelc = srcImage->FetchTexelc;
1848 dstImage->FetchTexelf = srcImage->FetchTexelf;
1849
1850 /* Alloc new teximage data buffer */
1851 {
1852 GLuint size = _mesa_format_image_size(dstImage->TexFormat,
1853 dstWidth, dstHeight, dstDepth);
1854 dstImage->Data = _mesa_alloc_texmemory(size);
1855 if (!dstImage->Data) {
1856 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
1857 return;
1858 }
1859 }
1860
1861 /* Setup src and dest data pointers */
1862 if (_mesa_is_format_compressed(dstImage->TexFormat)) {
1863 /* srcData and dstData are already set */
1864 ASSERT(srcData);
1865 ASSERT(dstData);
1866 }
1867 else {
1868 srcData = (const GLubyte *) srcImage->Data;
1869 dstData = (GLubyte *) dstImage->Data;
1870 }
1871
1872 ASSERT(dstImage->TexFormat);
1873 ASSERT(dstImage->FetchTexelc);
1874 ASSERT(dstImage->FetchTexelf);
1875
1876 _mesa_generate_mipmap_level(target, datatype, comps, border,
1877 srcWidth, srcHeight, srcDepth,
1878 srcData, srcImage->RowStride,
1879 dstWidth, dstHeight, dstDepth,
1880 dstData, dstImage->RowStride);
1881
1882
1883 if (_mesa_is_format_compressed(dstImage->TexFormat)) {
1884 GLubyte *temp;
1885 /* compress image from dstData into dstImage->Data */
1886 const GLenum srcFormat = _mesa_get_format_base_format(convertFormat);
1887 GLint dstRowStride
1888 = _mesa_format_row_stride(dstImage->TexFormat, dstWidth);
1889 ASSERT(srcFormat == GL_RGB || srcFormat == GL_RGBA);
1890
1891 _mesa_texstore(ctx, 2, dstImage->_BaseFormat,
1892 dstImage->TexFormat,
1893 dstImage->Data,
1894 0, 0, 0, /* dstX/Y/Zoffset */
1895 dstRowStride, 0, /* strides */
1896 dstWidth, dstHeight, 1, /* size */
1897 srcFormat, CHAN_TYPE,
1898 dstData, /* src data, actually */
1899 &ctx->DefaultPacking);
1900
1901 /* swap src and dest pointers */
1902 temp = (GLubyte *) srcData;
1903 srcData = dstData;
1904 dstData = temp;
1905 }
1906
1907 } /* loop over mipmap levels */
1908 }
1909
1910
1911 /**
1912 * Helper function for drivers which need to rescale texture images to
1913 * certain aspect ratios.
1914 * Nearest filtering only (for broken hardware that can't support
1915 * all aspect ratios). This can be made a lot faster, but I don't
1916 * really care enough...
1917 */
1918 void
1919 _mesa_rescale_teximage2d(GLuint bytesPerPixel,
1920 GLuint srcStrideInPixels,
1921 GLuint dstRowStride,
1922 GLint srcWidth, GLint srcHeight,
1923 GLint dstWidth, GLint dstHeight,
1924 const GLvoid *srcImage, GLvoid *dstImage)
1925 {
1926 GLint row, col;
1927
1928 #define INNER_LOOP( TYPE, HOP, WOP ) \
1929 for ( row = 0 ; row < dstHeight ; row++ ) { \
1930 GLint srcRow = row HOP hScale; \
1931 for ( col = 0 ; col < dstWidth ; col++ ) { \
1932 GLint srcCol = col WOP wScale; \
1933 dst[col] = src[srcRow * srcStrideInPixels + srcCol]; \
1934 } \
1935 dst = (TYPE *) ((GLubyte *) dst + dstRowStride); \
1936 } \
1937
1938 #define RESCALE_IMAGE( TYPE ) \
1939 do { \
1940 const TYPE *src = (const TYPE *)srcImage; \
1941 TYPE *dst = (TYPE *)dstImage; \
1942 \
1943 if ( srcHeight < dstHeight ) { \
1944 const GLint hScale = dstHeight / srcHeight; \
1945 if ( srcWidth < dstWidth ) { \
1946 const GLint wScale = dstWidth / srcWidth; \
1947 INNER_LOOP( TYPE, /, / ); \
1948 } \
1949 else { \
1950 const GLint wScale = srcWidth / dstWidth; \
1951 INNER_LOOP( TYPE, /, * ); \
1952 } \
1953 } \
1954 else { \
1955 const GLint hScale = srcHeight / dstHeight; \
1956 if ( srcWidth < dstWidth ) { \
1957 const GLint wScale = dstWidth / srcWidth; \
1958 INNER_LOOP( TYPE, *, / ); \
1959 } \
1960 else { \
1961 const GLint wScale = srcWidth / dstWidth; \
1962 INNER_LOOP( TYPE, *, * ); \
1963 } \
1964 } \
1965 } while (0)
1966
1967 switch ( bytesPerPixel ) {
1968 case 4:
1969 RESCALE_IMAGE( GLuint );
1970 break;
1971
1972 case 2:
1973 RESCALE_IMAGE( GLushort );
1974 break;
1975
1976 case 1:
1977 RESCALE_IMAGE( GLubyte );
1978 break;
1979 default:
1980 _mesa_problem(NULL,"unexpected bytes/pixel in _mesa_rescale_teximage2d");
1981 }
1982 }
1983
1984
1985 /**
1986 * Upscale an image by replication, not (typical) stretching.
1987 * We use this when the image width or height is less than a
1988 * certain size (4, 8) and we need to upscale an image.
1989 */
1990 void
1991 _mesa_upscale_teximage2d(GLsizei inWidth, GLsizei inHeight,
1992 GLsizei outWidth, GLsizei outHeight,
1993 GLint comps, const GLchan *src, GLint srcRowStride,
1994 GLchan *dest )
1995 {
1996 GLint i, j, k;
1997
1998 ASSERT(outWidth >= inWidth);
1999 ASSERT(outHeight >= inHeight);
2000 #if 0
2001 ASSERT(inWidth == 1 || inWidth == 2 || inHeight == 1 || inHeight == 2);
2002 ASSERT((outWidth & 3) == 0);
2003 ASSERT((outHeight & 3) == 0);
2004 #endif
2005
2006 for (i = 0; i < outHeight; i++) {
2007 const GLint ii = i % inHeight;
2008 for (j = 0; j < outWidth; j++) {
2009 const GLint jj = j % inWidth;
2010 for (k = 0; k < comps; k++) {
2011 dest[(i * outWidth + j) * comps + k]
2012 = src[ii * srcRowStride + jj * comps + k];
2013 }
2014 }
2015 }
2016 }
2017