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