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