db8ab65401fbc7667ba4f66de70b667dbc881e54
[mesa.git] / src / mesa / main / mipmap.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 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 static void
49 mesa_format_to_type_and_comps(const struct gl_texture_format *format,
50 GLenum *datatype, GLuint *comps)
51 {
52 switch (format->MesaFormat) {
53 case MESA_FORMAT_RGBA8888:
54 case MESA_FORMAT_RGBA8888_REV:
55 case MESA_FORMAT_ARGB8888:
56 case MESA_FORMAT_ARGB8888_REV:
57 *datatype = CHAN_TYPE;
58 *comps = 4;
59 return;
60 case MESA_FORMAT_RGB888:
61 case MESA_FORMAT_BGR888:
62 *datatype = GL_UNSIGNED_BYTE;
63 *comps = 3;
64 return;
65 case MESA_FORMAT_RGB565:
66 case MESA_FORMAT_RGB565_REV:
67 *datatype = GL_UNSIGNED_SHORT_5_6_5;
68 *comps = 3;
69 return;
70
71 case MESA_FORMAT_ARGB4444:
72 case MESA_FORMAT_ARGB4444_REV:
73 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
74 *comps = 4;
75 return;
76
77 case MESA_FORMAT_ARGB1555:
78 case MESA_FORMAT_ARGB1555_REV:
79 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
80 *comps = 3;
81 return;
82
83 case MESA_FORMAT_AL88:
84 case MESA_FORMAT_AL88_REV:
85 *datatype = GL_UNSIGNED_BYTE;
86 *comps = 2;
87 return;
88 case MESA_FORMAT_RGB332:
89 *datatype = GL_UNSIGNED_BYTE_3_3_2;
90 *comps = 3;
91 return;
92
93 case MESA_FORMAT_A8:
94 case MESA_FORMAT_L8:
95 case MESA_FORMAT_I8:
96 case MESA_FORMAT_CI8:
97 *datatype = GL_UNSIGNED_BYTE;
98 *comps = 1;
99 return;
100
101 case MESA_FORMAT_YCBCR:
102 case MESA_FORMAT_YCBCR_REV:
103 *datatype = GL_UNSIGNED_SHORT;
104 *comps = 2;
105 return;
106
107 case MESA_FORMAT_Z24_S8:
108 *datatype = GL_UNSIGNED_INT;
109 *comps = 1; /* XXX OK? */
110 return;
111
112 case MESA_FORMAT_Z16:
113 *datatype = GL_UNSIGNED_SHORT;
114 *comps = 1;
115 return;
116
117 case MESA_FORMAT_Z32:
118 *datatype = GL_UNSIGNED_INT;
119 *comps = 1;
120 return;
121
122 case MESA_FORMAT_SRGB8:
123 *datatype = GL_UNSIGNED_BYTE;
124 *comps = 3;
125 return;
126 case MESA_FORMAT_SRGBA8:
127 *datatype = GL_UNSIGNED_BYTE;
128 *comps = 4;
129 return;
130 case MESA_FORMAT_SL8:
131 *datatype = GL_UNSIGNED_BYTE;
132 *comps = 1;
133 return;
134 case MESA_FORMAT_SLA8:
135 *datatype = GL_UNSIGNED_BYTE;
136 *comps = 2;
137 return;
138
139 case MESA_FORMAT_RGB_FXT1:
140 case MESA_FORMAT_RGBA_FXT1:
141 case MESA_FORMAT_RGB_DXT1:
142 case MESA_FORMAT_RGBA_DXT1:
143 case MESA_FORMAT_RGBA_DXT3:
144 case MESA_FORMAT_RGBA_DXT5:
145 /* XXX generate error instead? */
146 *datatype = GL_UNSIGNED_BYTE;
147 *comps = 0;
148 return;
149
150 case MESA_FORMAT_RGBA:
151 *datatype = CHAN_TYPE;
152 *comps = 4;
153 return;
154 case MESA_FORMAT_RGB:
155 *datatype = CHAN_TYPE;
156 *comps = 3;
157 return;
158 case MESA_FORMAT_LUMINANCE_ALPHA:
159 *datatype = CHAN_TYPE;
160 *comps = 2;
161 return;
162 case MESA_FORMAT_ALPHA:
163 case MESA_FORMAT_LUMINANCE:
164 case MESA_FORMAT_INTENSITY:
165 *datatype = CHAN_TYPE;
166 *comps = 1;
167 return;
168
169 case MESA_FORMAT_RGBA_FLOAT32:
170 *datatype = GL_FLOAT;
171 *comps = 4;
172 return;
173 case MESA_FORMAT_RGBA_FLOAT16:
174 *datatype = GL_HALF_FLOAT_ARB;
175 *comps = 4;
176 return;
177 case MESA_FORMAT_RGB_FLOAT32:
178 *datatype = GL_FLOAT;
179 *comps = 3;
180 return;
181 case MESA_FORMAT_RGB_FLOAT16:
182 *datatype = GL_HALF_FLOAT_ARB;
183 *comps = 3;
184 return;
185 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32:
186 *datatype = GL_FLOAT;
187 *comps = 2;
188 return;
189 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16:
190 *datatype = GL_HALF_FLOAT_ARB;
191 *comps = 2;
192 return;
193 case MESA_FORMAT_ALPHA_FLOAT32:
194 case MESA_FORMAT_LUMINANCE_FLOAT32:
195 case MESA_FORMAT_INTENSITY_FLOAT32:
196 *datatype = GL_FLOAT;
197 *comps = 1;
198 return;
199 case MESA_FORMAT_ALPHA_FLOAT16:
200 case MESA_FORMAT_LUMINANCE_FLOAT16:
201 case MESA_FORMAT_INTENSITY_FLOAT16:
202 *datatype = GL_HALF_FLOAT_ARB;
203 *comps = 1;
204 return;
205
206 default:
207 _mesa_problem(NULL, "bad texture format in mesa_format_to_type_and_comps");
208 *datatype = 0;
209 *comps = 1;
210 }
211 }
212
213
214 /**
215 * Average together two rows of a source image to produce a single new
216 * row in the dest image. It's legal for the two source rows to point
217 * to the same data. The source width must be equal to either the
218 * dest width or two times the dest width.
219 * \param datatype GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, GL_FLOAT, etc.
220 * \param comps number of components per pixel (1..4)
221 */
222 static void
223 do_row(GLenum datatype, GLuint comps, GLint srcWidth,
224 const GLvoid *srcRowA, const GLvoid *srcRowB,
225 GLint dstWidth, GLvoid *dstRow)
226 {
227 const GLuint k0 = (srcWidth == dstWidth) ? 0 : 1;
228 const GLuint colStride = (srcWidth == dstWidth) ? 1 : 2;
229
230 ASSERT(comps >= 1);
231 ASSERT(comps <= 4);
232
233 /* This assertion is no longer valid with non-power-of-2 textures
234 assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth);
235 */
236
237 if (datatype == GL_UNSIGNED_BYTE && comps == 4) {
238 GLuint i, j, k;
239 const GLubyte(*rowA)[4] = (const GLubyte(*)[4]) srcRowA;
240 const GLubyte(*rowB)[4] = (const GLubyte(*)[4]) srcRowB;
241 GLubyte(*dst)[4] = (GLubyte(*)[4]) dstRow;
242 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
243 i++, j += colStride, k += colStride) {
244 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
245 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
246 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
247 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
248 }
249 }
250 else if (datatype == GL_UNSIGNED_BYTE && comps == 3) {
251 GLuint i, j, k;
252 const GLubyte(*rowA)[3] = (const GLubyte(*)[3]) srcRowA;
253 const GLubyte(*rowB)[3] = (const GLubyte(*)[3]) srcRowB;
254 GLubyte(*dst)[3] = (GLubyte(*)[3]) dstRow;
255 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
256 i++, j += colStride, k += colStride) {
257 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
258 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
259 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
260 }
261 }
262 else if (datatype == GL_UNSIGNED_BYTE && comps == 2) {
263 GLuint i, j, k;
264 const GLubyte(*rowA)[2] = (const GLubyte(*)[2]) srcRowA;
265 const GLubyte(*rowB)[2] = (const GLubyte(*)[2]) srcRowB;
266 GLubyte(*dst)[2] = (GLubyte(*)[2]) dstRow;
267 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
268 i++, j += colStride, k += colStride) {
269 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) >> 2;
270 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) >> 2;
271 }
272 }
273 else if (datatype == GL_UNSIGNED_BYTE && comps == 1) {
274 GLuint i, j, k;
275 const GLubyte *rowA = (const GLubyte *) srcRowA;
276 const GLubyte *rowB = (const GLubyte *) srcRowB;
277 GLubyte *dst = (GLubyte *) dstRow;
278 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
279 i++, j += colStride, k += colStride) {
280 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) >> 2;
281 }
282 }
283
284 else if (datatype == GL_UNSIGNED_SHORT && comps == 4) {
285 GLuint i, j, k;
286 const GLushort(*rowA)[4] = (const GLushort(*)[4]) srcRowA;
287 const GLushort(*rowB)[4] = (const GLushort(*)[4]) srcRowB;
288 GLushort(*dst)[4] = (GLushort(*)[4]) dstRow;
289 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
290 i++, j += colStride, k += colStride) {
291 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
292 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
293 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
294 dst[i][3] = (rowA[j][3] + rowA[k][3] + rowB[j][3] + rowB[k][3]) / 4;
295 }
296 }
297 else if (datatype == GL_UNSIGNED_SHORT && comps == 3) {
298 GLuint i, j, k;
299 const GLushort(*rowA)[3] = (const GLushort(*)[3]) srcRowA;
300 const GLushort(*rowB)[3] = (const GLushort(*)[3]) srcRowB;
301 GLushort(*dst)[3] = (GLushort(*)[3]) dstRow;
302 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
303 i++, j += colStride, k += colStride) {
304 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
305 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
306 dst[i][2] = (rowA[j][2] + rowA[k][2] + rowB[j][2] + rowB[k][2]) / 4;
307 }
308 }
309 else if (datatype == GL_UNSIGNED_SHORT && comps == 2) {
310 GLuint i, j, k;
311 const GLushort(*rowA)[2] = (const GLushort(*)[2]) srcRowA;
312 const GLushort(*rowB)[2] = (const GLushort(*)[2]) srcRowB;
313 GLushort(*dst)[2] = (GLushort(*)[2]) dstRow;
314 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
315 i++, j += colStride, k += colStride) {
316 dst[i][0] = (rowA[j][0] + rowA[k][0] + rowB[j][0] + rowB[k][0]) / 4;
317 dst[i][1] = (rowA[j][1] + rowA[k][1] + rowB[j][1] + rowB[k][1]) / 4;
318 }
319 }
320 else if (datatype == GL_UNSIGNED_SHORT && comps == 1) {
321 GLuint i, j, k;
322 const GLushort *rowA = (const GLushort *) srcRowA;
323 const GLushort *rowB = (const GLushort *) srcRowB;
324 GLushort *dst = (GLushort *) dstRow;
325 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
326 i++, j += colStride, k += colStride) {
327 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) / 4;
328 }
329 }
330
331 else if (datatype == GL_FLOAT && comps == 4) {
332 GLuint i, j, k;
333 const GLfloat(*rowA)[4] = (const GLfloat(*)[4]) srcRowA;
334 const GLfloat(*rowB)[4] = (const GLfloat(*)[4]) srcRowB;
335 GLfloat(*dst)[4] = (GLfloat(*)[4]) dstRow;
336 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
337 i++, j += colStride, k += colStride) {
338 dst[i][0] = (rowA[j][0] + rowA[k][0] +
339 rowB[j][0] + rowB[k][0]) * 0.25F;
340 dst[i][1] = (rowA[j][1] + rowA[k][1] +
341 rowB[j][1] + rowB[k][1]) * 0.25F;
342 dst[i][2] = (rowA[j][2] + rowA[k][2] +
343 rowB[j][2] + rowB[k][2]) * 0.25F;
344 dst[i][3] = (rowA[j][3] + rowA[k][3] +
345 rowB[j][3] + rowB[k][3]) * 0.25F;
346 }
347 }
348 else if (datatype == GL_FLOAT && comps == 3) {
349 GLuint i, j, k;
350 const GLfloat(*rowA)[3] = (const GLfloat(*)[3]) srcRowA;
351 const GLfloat(*rowB)[3] = (const GLfloat(*)[3]) srcRowB;
352 GLfloat(*dst)[3] = (GLfloat(*)[3]) dstRow;
353 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
354 i++, j += colStride, k += colStride) {
355 dst[i][0] = (rowA[j][0] + rowA[k][0] +
356 rowB[j][0] + rowB[k][0]) * 0.25F;
357 dst[i][1] = (rowA[j][1] + rowA[k][1] +
358 rowB[j][1] + rowB[k][1]) * 0.25F;
359 dst[i][2] = (rowA[j][2] + rowA[k][2] +
360 rowB[j][2] + rowB[k][2]) * 0.25F;
361 }
362 }
363 else if (datatype == GL_FLOAT && comps == 2) {
364 GLuint i, j, k;
365 const GLfloat(*rowA)[2] = (const GLfloat(*)[2]) srcRowA;
366 const GLfloat(*rowB)[2] = (const GLfloat(*)[2]) srcRowB;
367 GLfloat(*dst)[2] = (GLfloat(*)[2]) dstRow;
368 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
369 i++, j += colStride, k += colStride) {
370 dst[i][0] = (rowA[j][0] + rowA[k][0] +
371 rowB[j][0] + rowB[k][0]) * 0.25F;
372 dst[i][1] = (rowA[j][1] + rowA[k][1] +
373 rowB[j][1] + rowB[k][1]) * 0.25F;
374 }
375 }
376 else if (datatype == GL_FLOAT && comps == 1) {
377 GLuint i, j, k;
378 const GLfloat *rowA = (const GLfloat *) srcRowA;
379 const GLfloat *rowB = (const GLfloat *) srcRowB;
380 GLfloat *dst = (GLfloat *) dstRow;
381 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
382 i++, j += colStride, k += colStride) {
383 dst[i] = (rowA[j] + rowA[k] + rowB[j] + rowB[k]) * 0.25F;
384 }
385 }
386
387 else if (datatype == GL_HALF_FLOAT_ARB && comps == 4) {
388 GLuint i, j, k, comp;
389 const GLhalfARB(*rowA)[4] = (const GLhalfARB(*)[4]) srcRowA;
390 const GLhalfARB(*rowB)[4] = (const GLhalfARB(*)[4]) srcRowB;
391 GLhalfARB(*dst)[4] = (GLhalfARB(*)[4]) dstRow;
392 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
393 i++, j += colStride, k += colStride) {
394 for (comp = 0; comp < 4; comp++) {
395 GLfloat aj, ak, bj, bk;
396 aj = _mesa_half_to_float(rowA[j][comp]);
397 ak = _mesa_half_to_float(rowA[k][comp]);
398 bj = _mesa_half_to_float(rowB[j][comp]);
399 bk = _mesa_half_to_float(rowB[k][comp]);
400 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
401 }
402 }
403 }
404 else if (datatype == GL_HALF_FLOAT_ARB && comps == 3) {
405 GLuint i, j, k, comp;
406 const GLhalfARB(*rowA)[3] = (const GLhalfARB(*)[3]) srcRowA;
407 const GLhalfARB(*rowB)[3] = (const GLhalfARB(*)[3]) srcRowB;
408 GLhalfARB(*dst)[3] = (GLhalfARB(*)[3]) dstRow;
409 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
410 i++, j += colStride, k += colStride) {
411 for (comp = 0; comp < 3; comp++) {
412 GLfloat aj, ak, bj, bk;
413 aj = _mesa_half_to_float(rowA[j][comp]);
414 ak = _mesa_half_to_float(rowA[k][comp]);
415 bj = _mesa_half_to_float(rowB[j][comp]);
416 bk = _mesa_half_to_float(rowB[k][comp]);
417 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
418 }
419 }
420 }
421 else if (datatype == GL_HALF_FLOAT_ARB && comps == 2) {
422 GLuint i, j, k, comp;
423 const GLhalfARB(*rowA)[2] = (const GLhalfARB(*)[2]) srcRowA;
424 const GLhalfARB(*rowB)[2] = (const GLhalfARB(*)[2]) srcRowB;
425 GLhalfARB(*dst)[2] = (GLhalfARB(*)[2]) dstRow;
426 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
427 i++, j += colStride, k += colStride) {
428 for (comp = 0; comp < 2; comp++) {
429 GLfloat aj, ak, bj, bk;
430 aj = _mesa_half_to_float(rowA[j][comp]);
431 ak = _mesa_half_to_float(rowA[k][comp]);
432 bj = _mesa_half_to_float(rowB[j][comp]);
433 bk = _mesa_half_to_float(rowB[k][comp]);
434 dst[i][comp] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
435 }
436 }
437 }
438 else if (datatype == GL_HALF_FLOAT_ARB && comps == 1) {
439 GLuint i, j, k;
440 const GLhalfARB *rowA = (const GLhalfARB *) srcRowA;
441 const GLhalfARB *rowB = (const GLhalfARB *) srcRowB;
442 GLhalfARB *dst = (GLhalfARB *) dstRow;
443 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
444 i++, j += colStride, k += colStride) {
445 GLfloat aj, ak, bj, bk;
446 aj = _mesa_half_to_float(rowA[j]);
447 ak = _mesa_half_to_float(rowA[k]);
448 bj = _mesa_half_to_float(rowB[j]);
449 bk = _mesa_half_to_float(rowB[k]);
450 dst[i] = _mesa_float_to_half((aj + ak + bj + bk) * 0.25F);
451 }
452 }
453
454 else if (datatype == GL_UNSIGNED_INT && comps == 1) {
455 GLuint i, j, k;
456 const GLuint *rowA = (const GLuint *) srcRowA;
457 const GLuint *rowB = (const GLuint *) srcRowB;
458 GLfloat *dst = (GLfloat *) dstRow;
459 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
460 i++, j += colStride, k += colStride) {
461 dst[i] = rowA[j] / 4 + rowA[k] / 4 + rowB[j] / 4 + rowB[k] / 4;
462 }
463 }
464
465 else if (datatype == GL_UNSIGNED_SHORT_5_6_5 && comps == 3) {
466 GLuint i, j, k;
467 const GLushort *rowA = (const GLushort *) srcRowA;
468 const GLushort *rowB = (const GLushort *) srcRowB;
469 GLushort *dst = (GLushort *) dstRow;
470 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
471 i++, j += colStride, k += colStride) {
472 const GLint rowAr0 = rowA[j] & 0x1f;
473 const GLint rowAr1 = rowA[k] & 0x1f;
474 const GLint rowBr0 = rowB[j] & 0x1f;
475 const GLint rowBr1 = rowB[k] & 0x1f;
476 const GLint rowAg0 = (rowA[j] >> 5) & 0x3f;
477 const GLint rowAg1 = (rowA[k] >> 5) & 0x3f;
478 const GLint rowBg0 = (rowB[j] >> 5) & 0x3f;
479 const GLint rowBg1 = (rowB[k] >> 5) & 0x3f;
480 const GLint rowAb0 = (rowA[j] >> 11) & 0x1f;
481 const GLint rowAb1 = (rowA[k] >> 11) & 0x1f;
482 const GLint rowBb0 = (rowB[j] >> 11) & 0x1f;
483 const GLint rowBb1 = (rowB[k] >> 11) & 0x1f;
484 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
485 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
486 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
487 dst[i] = (blue << 11) | (green << 5) | red;
488 }
489 }
490 else if (datatype == GL_UNSIGNED_SHORT_4_4_4_4 && comps == 4) {
491 GLuint i, j, k;
492 const GLushort *rowA = (const GLushort *) srcRowA;
493 const GLushort *rowB = (const GLushort *) srcRowB;
494 GLushort *dst = (GLushort *) dstRow;
495 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
496 i++, j += colStride, k += colStride) {
497 const GLint rowAr0 = rowA[j] & 0xf;
498 const GLint rowAr1 = rowA[k] & 0xf;
499 const GLint rowBr0 = rowB[j] & 0xf;
500 const GLint rowBr1 = rowB[k] & 0xf;
501 const GLint rowAg0 = (rowA[j] >> 4) & 0xf;
502 const GLint rowAg1 = (rowA[k] >> 4) & 0xf;
503 const GLint rowBg0 = (rowB[j] >> 4) & 0xf;
504 const GLint rowBg1 = (rowB[k] >> 4) & 0xf;
505 const GLint rowAb0 = (rowA[j] >> 8) & 0xf;
506 const GLint rowAb1 = (rowA[k] >> 8) & 0xf;
507 const GLint rowBb0 = (rowB[j] >> 8) & 0xf;
508 const GLint rowBb1 = (rowB[k] >> 8) & 0xf;
509 const GLint rowAa0 = (rowA[j] >> 12) & 0xf;
510 const GLint rowAa1 = (rowA[k] >> 12) & 0xf;
511 const GLint rowBa0 = (rowB[j] >> 12) & 0xf;
512 const GLint rowBa1 = (rowB[k] >> 12) & 0xf;
513 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
514 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
515 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
516 const GLint alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
517 dst[i] = (alpha << 12) | (blue << 8) | (green << 4) | red;
518 }
519 }
520 else if (datatype == GL_UNSIGNED_SHORT_1_5_5_5_REV && comps == 4) {
521 GLuint i, j, k;
522 const GLushort *rowA = (const GLushort *) srcRowA;
523 const GLushort *rowB = (const GLushort *) srcRowB;
524 GLushort *dst = (GLushort *) dstRow;
525 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
526 i++, j += colStride, k += colStride) {
527 const GLint rowAr0 = rowA[j] & 0x1f;
528 const GLint rowAr1 = rowA[k] & 0x1f;
529 const GLint rowBr0 = rowB[j] & 0x1f;
530 const GLint rowBr1 = rowB[k] & 0xf;
531 const GLint rowAg0 = (rowA[j] >> 5) & 0x1f;
532 const GLint rowAg1 = (rowA[k] >> 5) & 0x1f;
533 const GLint rowBg0 = (rowB[j] >> 5) & 0x1f;
534 const GLint rowBg1 = (rowB[k] >> 5) & 0x1f;
535 const GLint rowAb0 = (rowA[j] >> 10) & 0x1f;
536 const GLint rowAb1 = (rowA[k] >> 10) & 0x1f;
537 const GLint rowBb0 = (rowB[j] >> 10) & 0x1f;
538 const GLint rowBb1 = (rowB[k] >> 10) & 0x1f;
539 const GLint rowAa0 = (rowA[j] >> 15) & 0x1;
540 const GLint rowAa1 = (rowA[k] >> 15) & 0x1;
541 const GLint rowBa0 = (rowB[j] >> 15) & 0x1;
542 const GLint rowBa1 = (rowB[k] >> 15) & 0x1;
543 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
544 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
545 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
546 const GLint alpha = (rowAa0 + rowAa1 + rowBa0 + rowBa1) >> 2;
547 dst[i] = (alpha << 15) | (blue << 10) | (green << 5) | red;
548 }
549 }
550 else if (datatype == GL_UNSIGNED_BYTE_3_3_2 && comps == 3) {
551 GLuint i, j, k;
552 const GLubyte *rowA = (const GLubyte *) srcRowA;
553 const GLubyte *rowB = (const GLubyte *) srcRowB;
554 GLubyte *dst = (GLubyte *) dstRow;
555 for (i = j = 0, k = k0; i < (GLuint) dstWidth;
556 i++, j += colStride, k += colStride) {
557 const GLint rowAr0 = rowA[j] & 0x3;
558 const GLint rowAr1 = rowA[k] & 0x3;
559 const GLint rowBr0 = rowB[j] & 0x3;
560 const GLint rowBr1 = rowB[k] & 0x3;
561 const GLint rowAg0 = (rowA[j] >> 2) & 0x7;
562 const GLint rowAg1 = (rowA[k] >> 2) & 0x7;
563 const GLint rowBg0 = (rowB[j] >> 2) & 0x7;
564 const GLint rowBg1 = (rowB[k] >> 2) & 0x7;
565 const GLint rowAb0 = (rowA[j] >> 5) & 0x7;
566 const GLint rowAb1 = (rowA[k] >> 5) & 0x7;
567 const GLint rowBb0 = (rowB[j] >> 5) & 0x7;
568 const GLint rowBb1 = (rowB[k] >> 5) & 0x7;
569 const GLint red = (rowAr0 + rowAr1 + rowBr0 + rowBr1) >> 2;
570 const GLint green = (rowAg0 + rowAg1 + rowBg0 + rowBg1) >> 2;
571 const GLint blue = (rowAb0 + rowAb1 + rowBb0 + rowBb1) >> 2;
572 dst[i] = (blue << 5) | (green << 2) | red;
573 }
574 }
575 else {
576 _mesa_problem(NULL, "bad format in do_row()");
577 }
578 }
579
580
581 /*
582 * These functions generate a 1/2-size mipmap image from a source image.
583 * Texture borders are handled by copying or averaging the source image's
584 * border texels, depending on the scale-down factor.
585 */
586
587 static void
588 make_1d_mipmap(GLenum datatype, GLuint comps, GLint border,
589 GLint srcWidth, const GLubyte *srcPtr,
590 GLint dstWidth, GLubyte *dstPtr)
591 {
592 const GLint bpt = bytes_per_pixel(datatype, comps);
593 const GLubyte *src;
594 GLubyte *dst;
595
596 /* skip the border pixel, if any */
597 src = srcPtr + border * bpt;
598 dst = dstPtr + border * bpt;
599
600 /* we just duplicate the input row, kind of hack, saves code */
601 do_row(datatype, comps, srcWidth - 2 * border, src, src,
602 dstWidth - 2 * border, dst);
603
604 if (border) {
605 /* copy left-most pixel from source */
606 MEMCPY(dstPtr, srcPtr, bpt);
607 /* copy right-most pixel from source */
608 MEMCPY(dstPtr + (dstWidth - 1) * bpt,
609 srcPtr + (srcWidth - 1) * bpt,
610 bpt);
611 }
612 }
613
614
615 /**
616 * XXX need to use the tex image's row stride!
617 */
618 static void
619 make_2d_mipmap(GLenum datatype, GLuint comps, GLint border,
620 GLint srcWidth, GLint srcHeight, const GLubyte *srcPtr,
621 GLint dstWidth, GLint dstHeight, GLubyte *dstPtr)
622 {
623 const GLint bpt = bytes_per_pixel(datatype, comps);
624 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
625 const GLint dstWidthNB = dstWidth - 2 * border;
626 const GLint dstHeightNB = dstHeight - 2 * border;
627 const GLint srcRowStride = bpt * srcWidth;
628 const GLint dstRowStride = bpt * dstWidth;
629 const GLubyte *srcA, *srcB;
630 GLubyte *dst;
631 GLint row;
632
633 /* Compute src and dst pointers, skipping any border */
634 srcA = srcPtr + border * ((srcWidth + 1) * bpt);
635 if (srcHeight > 1)
636 srcB = srcA + srcRowStride;
637 else
638 srcB = srcA;
639 dst = dstPtr + border * ((dstWidth + 1) * bpt);
640
641 for (row = 0; row < dstHeightNB; row++) {
642 do_row(datatype, comps, srcWidthNB, srcA, srcB,
643 dstWidthNB, dst);
644 srcA += 2 * srcRowStride;
645 srcB += 2 * srcRowStride;
646 dst += dstRowStride;
647 }
648
649 /* This is ugly but probably won't be used much */
650 if (border > 0) {
651 /* fill in dest border */
652 /* lower-left border pixel */
653 MEMCPY(dstPtr, srcPtr, bpt);
654 /* lower-right border pixel */
655 MEMCPY(dstPtr + (dstWidth - 1) * bpt,
656 srcPtr + (srcWidth - 1) * bpt, bpt);
657 /* upper-left border pixel */
658 MEMCPY(dstPtr + dstWidth * (dstHeight - 1) * bpt,
659 srcPtr + srcWidth * (srcHeight - 1) * bpt, bpt);
660 /* upper-right border pixel */
661 MEMCPY(dstPtr + (dstWidth * dstHeight - 1) * bpt,
662 srcPtr + (srcWidth * srcHeight - 1) * bpt, bpt);
663 /* lower border */
664 do_row(datatype, comps, srcWidthNB,
665 srcPtr + bpt,
666 srcPtr + bpt,
667 dstWidthNB, dstPtr + bpt);
668 /* upper border */
669 do_row(datatype, comps, srcWidthNB,
670 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
671 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
672 dstWidthNB,
673 dstPtr + (dstWidth * (dstHeight - 1) + 1) * bpt);
674 /* left and right borders */
675 if (srcHeight == dstHeight) {
676 /* copy border pixel from src to dst */
677 for (row = 1; row < srcHeight; row++) {
678 MEMCPY(dstPtr + dstWidth * row * bpt,
679 srcPtr + srcWidth * row * bpt, bpt);
680 MEMCPY(dstPtr + (dstWidth * row + dstWidth - 1) * bpt,
681 srcPtr + (srcWidth * row + srcWidth - 1) * bpt, bpt);
682 }
683 }
684 else {
685 /* average two src pixels each dest pixel */
686 for (row = 0; row < dstHeightNB; row += 2) {
687 do_row(datatype, comps, 1,
688 srcPtr + (srcWidth * (row * 2 + 1)) * bpt,
689 srcPtr + (srcWidth * (row * 2 + 2)) * bpt,
690 1, dstPtr + (dstWidth * row + 1) * bpt);
691 do_row(datatype, comps, 1,
692 srcPtr + (srcWidth * (row * 2 + 1) + srcWidth - 1) * bpt,
693 srcPtr + (srcWidth * (row * 2 + 2) + srcWidth - 1) * bpt,
694 1, dstPtr + (dstWidth * row + 1 + dstWidth - 1) * bpt);
695 }
696 }
697 }
698 }
699
700
701 static void
702 make_3d_mipmap(GLenum datatype, GLuint comps, GLint border,
703 GLint srcWidth, GLint srcHeight, GLint srcDepth,
704 const GLubyte *srcPtr,
705 GLint dstWidth, GLint dstHeight, GLint dstDepth,
706 GLubyte *dstPtr)
707 {
708 const GLint bpt = bytes_per_pixel(datatype, comps);
709 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
710 const GLint srcDepthNB = srcDepth - 2 * border;
711 const GLint dstWidthNB = dstWidth - 2 * border;
712 const GLint dstHeightNB = dstHeight - 2 * border;
713 const GLint dstDepthNB = dstDepth - 2 * border;
714 GLvoid *tmpRowA, *tmpRowB;
715 GLint img, row;
716 GLint bytesPerSrcImage, bytesPerDstImage;
717 GLint bytesPerSrcRow, bytesPerDstRow;
718 GLint srcImageOffset, srcRowOffset;
719
720 (void) srcDepthNB; /* silence warnings */
721
722 /* Need two temporary row buffers */
723 tmpRowA = _mesa_malloc(srcWidth * bpt);
724 if (!tmpRowA)
725 return;
726 tmpRowB = _mesa_malloc(srcWidth * bpt);
727 if (!tmpRowB) {
728 _mesa_free(tmpRowA);
729 return;
730 }
731
732 bytesPerSrcImage = srcWidth * srcHeight * bpt;
733 bytesPerDstImage = dstWidth * dstHeight * bpt;
734
735 bytesPerSrcRow = srcWidth * bpt;
736 bytesPerDstRow = dstWidth * bpt;
737
738 /* Offset between adjacent src images to be averaged together */
739 srcImageOffset = (srcDepth == dstDepth) ? 0 : bytesPerSrcImage;
740
741 /* Offset between adjacent src rows to be averaged together */
742 srcRowOffset = (srcHeight == dstHeight) ? 0 : srcWidth * bpt;
743
744 /*
745 * Need to average together up to 8 src pixels for each dest pixel.
746 * Break that down into 3 operations:
747 * 1. take two rows from source image and average them together.
748 * 2. take two rows from next source image and average them together.
749 * 3. take the two averaged rows and average them for the final dst row.
750 */
751
752 /*
753 _mesa_printf("mip3d %d x %d x %d -> %d x %d x %d\n",
754 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
755 */
756
757 for (img = 0; img < dstDepthNB; img++) {
758 /* first source image pointer, skipping border */
759 const GLubyte *imgSrcA = srcPtr
760 + (bytesPerSrcImage + bytesPerSrcRow + border) * bpt * border
761 + img * (bytesPerSrcImage + srcImageOffset);
762 /* second source image pointer, skipping border */
763 const GLubyte *imgSrcB = imgSrcA + srcImageOffset;
764 /* address of the dest image, skipping border */
765 GLubyte *imgDst = dstPtr
766 + (bytesPerDstImage + bytesPerDstRow + border) * bpt * border
767 + img * bytesPerDstImage;
768
769 /* setup the four source row pointers and the dest row pointer */
770 const GLubyte *srcImgARowA = imgSrcA;
771 const GLubyte *srcImgARowB = imgSrcA + srcRowOffset;
772 const GLubyte *srcImgBRowA = imgSrcB;
773 const GLubyte *srcImgBRowB = imgSrcB + srcRowOffset;
774 GLubyte *dstImgRow = imgDst;
775
776 for (row = 0; row < dstHeightNB; row++) {
777 /* Average together two rows from first src image */
778 do_row(datatype, comps, srcWidthNB, srcImgARowA, srcImgARowB,
779 srcWidthNB, tmpRowA);
780 /* Average together two rows from second src image */
781 do_row(datatype, comps, srcWidthNB, srcImgBRowA, srcImgBRowB,
782 srcWidthNB, tmpRowB);
783 /* Average together the temp rows to make the final row */
784 do_row(datatype, comps, srcWidthNB, tmpRowA, tmpRowB,
785 dstWidthNB, dstImgRow);
786 /* advance to next rows */
787 srcImgARowA += bytesPerSrcRow + srcRowOffset;
788 srcImgARowB += bytesPerSrcRow + srcRowOffset;
789 srcImgBRowA += bytesPerSrcRow + srcRowOffset;
790 srcImgBRowB += bytesPerSrcRow + srcRowOffset;
791 dstImgRow += bytesPerDstRow;
792 }
793 }
794
795 _mesa_free(tmpRowA);
796 _mesa_free(tmpRowB);
797
798 /* Luckily we can leverage the make_2d_mipmap() function here! */
799 if (border > 0) {
800 /* do front border image */
801 make_2d_mipmap(datatype, comps, 1, srcWidth, srcHeight, srcPtr,
802 dstWidth, dstHeight, dstPtr);
803 /* do back border image */
804 make_2d_mipmap(datatype, comps, 1, srcWidth, srcHeight,
805 srcPtr + bytesPerSrcImage * (srcDepth - 1),
806 dstWidth, dstHeight,
807 dstPtr + bytesPerDstImage * (dstDepth - 1));
808 /* do four remaining border edges that span the image slices */
809 if (srcDepth == dstDepth) {
810 /* just copy border pixels from src to dst */
811 for (img = 0; img < dstDepthNB; img++) {
812 const GLubyte *src;
813 GLubyte *dst;
814
815 /* do border along [img][row=0][col=0] */
816 src = srcPtr + (img + 1) * bytesPerSrcImage;
817 dst = dstPtr + (img + 1) * bytesPerDstImage;
818 MEMCPY(dst, src, bpt);
819
820 /* do border along [img][row=dstHeight-1][col=0] */
821 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
822 + (srcHeight - 1) * bytesPerSrcRow;
823 dst = dstPtr + (img + 1) * bytesPerDstImage
824 + (dstHeight - 1) * bytesPerDstRow;
825 MEMCPY(dst, src, bpt);
826
827 /* do border along [img][row=0][col=dstWidth-1] */
828 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
829 + (srcWidth - 1) * bpt;
830 dst = dstPtr + (img + 1) * bytesPerDstImage
831 + (dstWidth - 1) * bpt;
832 MEMCPY(dst, src, bpt);
833
834 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
835 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
836 + (bytesPerSrcImage - bpt);
837 dst = dstPtr + (img + 1) * bytesPerDstImage
838 + (bytesPerDstImage - bpt);
839 MEMCPY(dst, src, bpt);
840 }
841 }
842 else {
843 /* average border pixels from adjacent src image pairs */
844 ASSERT(srcDepthNB == 2 * dstDepthNB);
845 for (img = 0; img < dstDepthNB; img++) {
846 const GLubyte *src;
847 GLubyte *dst;
848
849 /* do border along [img][row=0][col=0] */
850 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage;
851 dst = dstPtr + (img + 1) * bytesPerDstImage;
852 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
853
854 /* do border along [img][row=dstHeight-1][col=0] */
855 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
856 + (srcHeight - 1) * bytesPerSrcRow;
857 dst = dstPtr + (img + 1) * bytesPerDstImage
858 + (dstHeight - 1) * bytesPerDstRow;
859 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
860
861 /* do border along [img][row=0][col=dstWidth-1] */
862 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
863 + (srcWidth - 1) * bpt;
864 dst = dstPtr + (img + 1) * bytesPerDstImage
865 + (dstWidth - 1) * bpt;
866 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
867
868 /* do border along [img][row=dstHeight-1][col=dstWidth-1] */
869 src = srcPtr + (img * 2 + 1) * bytesPerSrcImage
870 + (bytesPerSrcImage - bpt);
871 dst = dstPtr + (img + 1) * bytesPerDstImage
872 + (bytesPerDstImage - bpt);
873 do_row(datatype, comps, 1, src, src + srcImageOffset, 1, dst);
874 }
875 }
876 }
877 }
878
879
880 static void
881 make_1d_stack_mipmap(GLenum datatype, GLuint comps, GLint border,
882 GLint srcWidth, const GLubyte *srcPtr,
883 GLint dstWidth, GLint dstHeight, GLubyte *dstPtr)
884 {
885 const GLint bpt = bytes_per_pixel(datatype, comps);
886 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
887 const GLint dstWidthNB = dstWidth - 2 * border;
888 const GLint dstHeightNB = dstHeight - 2 * border;
889 const GLint srcRowStride = bpt * srcWidth;
890 const GLint dstRowStride = bpt * dstWidth;
891 const GLubyte *src;
892 GLubyte *dst;
893 GLint row;
894
895 /* Compute src and dst pointers, skipping any border */
896 src = srcPtr + border * ((srcWidth + 1) * bpt);
897 dst = dstPtr + border * ((dstWidth + 1) * bpt);
898
899 for (row = 0; row < dstHeightNB; row++) {
900 do_row(datatype, comps, srcWidthNB, src, src,
901 dstWidthNB, dst);
902 src += srcRowStride;
903 dst += dstRowStride;
904 }
905
906 if (border) {
907 /* copy left-most pixel from source */
908 MEMCPY(dstPtr, srcPtr, bpt);
909 /* copy right-most pixel from source */
910 MEMCPY(dstPtr + (dstWidth - 1) * bpt,
911 srcPtr + (srcWidth - 1) * bpt,
912 bpt);
913 }
914 }
915
916
917 /**
918 * \bugs
919 * There is quite a bit of refactoring that could be done with this function
920 * and \c make_2d_mipmap.
921 */
922 static void
923 make_2d_stack_mipmap(GLenum datatype, GLuint comps, GLint border,
924 GLint srcWidth, GLint srcHeight, const GLubyte *srcPtr,
925 GLint dstWidth, GLint dstHeight, GLint dstDepth,
926 GLubyte *dstPtr)
927 {
928 const GLint bpt = bytes_per_pixel(datatype, comps);
929 const GLint srcWidthNB = srcWidth - 2 * border; /* sizes w/out border */
930 const GLint dstWidthNB = dstWidth - 2 * border;
931 const GLint dstHeightNB = dstHeight - 2 * border;
932 const GLint dstDepthNB = dstDepth - 2 * border;
933 const GLint srcRowStride = bpt * srcWidth;
934 const GLint dstRowStride = bpt * dstWidth;
935 const GLubyte *srcA, *srcB;
936 GLubyte *dst;
937 GLint layer;
938 GLint row;
939
940 /* Compute src and dst pointers, skipping any border */
941 srcA = srcPtr + border * ((srcWidth + 1) * bpt);
942 if (srcHeight > 1)
943 srcB = srcA + srcRowStride;
944 else
945 srcB = srcA;
946 dst = dstPtr + border * ((dstWidth + 1) * bpt);
947
948 for (layer = 0; layer < dstDepthNB; layer++) {
949 for (row = 0; row < dstHeightNB; row++) {
950 do_row(datatype, comps, srcWidthNB, srcA, srcB,
951 dstWidthNB, dst);
952 srcA += 2 * srcRowStride;
953 srcB += 2 * srcRowStride;
954 dst += dstRowStride;
955 }
956
957 /* This is ugly but probably won't be used much */
958 if (border > 0) {
959 /* fill in dest border */
960 /* lower-left border pixel */
961 MEMCPY(dstPtr, srcPtr, bpt);
962 /* lower-right border pixel */
963 MEMCPY(dstPtr + (dstWidth - 1) * bpt,
964 srcPtr + (srcWidth - 1) * bpt, bpt);
965 /* upper-left border pixel */
966 MEMCPY(dstPtr + dstWidth * (dstHeight - 1) * bpt,
967 srcPtr + srcWidth * (srcHeight - 1) * bpt, bpt);
968 /* upper-right border pixel */
969 MEMCPY(dstPtr + (dstWidth * dstHeight - 1) * bpt,
970 srcPtr + (srcWidth * srcHeight - 1) * bpt, bpt);
971 /* lower border */
972 do_row(datatype, comps, srcWidthNB,
973 srcPtr + bpt,
974 srcPtr + bpt,
975 dstWidthNB, dstPtr + bpt);
976 /* upper border */
977 do_row(datatype, comps, srcWidthNB,
978 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
979 srcPtr + (srcWidth * (srcHeight - 1) + 1) * bpt,
980 dstWidthNB,
981 dstPtr + (dstWidth * (dstHeight - 1) + 1) * bpt);
982 /* left and right borders */
983 if (srcHeight == dstHeight) {
984 /* copy border pixel from src to dst */
985 for (row = 1; row < srcHeight; row++) {
986 MEMCPY(dstPtr + dstWidth * row * bpt,
987 srcPtr + srcWidth * row * bpt, bpt);
988 MEMCPY(dstPtr + (dstWidth * row + dstWidth - 1) * bpt,
989 srcPtr + (srcWidth * row + srcWidth - 1) * bpt, bpt);
990 }
991 }
992 else {
993 /* average two src pixels each dest pixel */
994 for (row = 0; row < dstHeightNB; row += 2) {
995 do_row(datatype, comps, 1,
996 srcPtr + (srcWidth * (row * 2 + 1)) * bpt,
997 srcPtr + (srcWidth * (row * 2 + 2)) * bpt,
998 1, dstPtr + (dstWidth * row + 1) * bpt);
999 do_row(datatype, comps, 1,
1000 srcPtr + (srcWidth * (row * 2 + 1) + srcWidth - 1) * bpt,
1001 srcPtr + (srcWidth * (row * 2 + 2) + srcWidth - 1) * bpt,
1002 1, dstPtr + (dstWidth * row + 1 + dstWidth - 1) * bpt);
1003 }
1004 }
1005 }
1006 }
1007 }
1008
1009
1010 /**
1011 * For GL_SGIX_generate_mipmap:
1012 * Generate a complete set of mipmaps from texObj's base-level image.
1013 * Stop at texObj's MaxLevel or when we get to the 1x1 texture.
1014 */
1015 void
1016 _mesa_generate_mipmap(GLcontext *ctx, GLenum target,
1017 struct gl_texture_object *texObj)
1018 {
1019 const struct gl_texture_image *srcImage;
1020 const struct gl_texture_format *convertFormat;
1021 const GLubyte *srcData = NULL;
1022 GLubyte *dstData = NULL;
1023 GLint level, maxLevels;
1024 GLenum datatype;
1025 GLuint comps;
1026
1027 ASSERT(texObj);
1028 /* XXX choose cube map face here??? */
1029 srcImage = texObj->Image[0][texObj->BaseLevel];
1030 ASSERT(srcImage);
1031
1032 maxLevels = _mesa_max_texture_levels(ctx, texObj->Target);
1033 ASSERT(maxLevels > 0); /* bad target */
1034
1035 /* Find convertFormat - the format that do_row() will process */
1036 if (srcImage->IsCompressed) {
1037 /* setup for compressed textures */
1038 GLuint row;
1039 GLint components, size;
1040 GLchan *dst;
1041
1042 assert(texObj->Target == GL_TEXTURE_2D);
1043
1044 if (srcImage->_BaseFormat == GL_RGB) {
1045 convertFormat = &_mesa_texformat_rgb;
1046 components = 3;
1047 }
1048 else if (srcImage->_BaseFormat == GL_RGBA) {
1049 convertFormat = &_mesa_texformat_rgba;
1050 components = 4;
1051 }
1052 else {
1053 _mesa_problem(ctx, "bad srcImage->_BaseFormat in _mesa_generate_mipmaps");
1054 return;
1055 }
1056
1057 /* allocate storage for uncompressed GL_RGB or GL_RGBA images */
1058 size = _mesa_bytes_per_pixel(srcImage->_BaseFormat, CHAN_TYPE)
1059 * srcImage->Width * srcImage->Height * srcImage->Depth + 20;
1060 /* 20 extra bytes, just be safe when calling last FetchTexel */
1061 srcData = (GLubyte *) _mesa_malloc(size);
1062 if (!srcData) {
1063 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generate mipmaps");
1064 return;
1065 }
1066 dstData = (GLubyte *) _mesa_malloc(size / 2); /* 1/4 would probably be OK */
1067 if (!dstData) {
1068 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generate mipmaps");
1069 _mesa_free((void *) srcData);
1070 return;
1071 }
1072
1073 /* decompress base image here */
1074 dst = (GLchan *) srcData;
1075 for (row = 0; row < srcImage->Height; row++) {
1076 GLuint col;
1077 for (col = 0; col < srcImage->Width; col++) {
1078 srcImage->FetchTexelc(srcImage, col, row, 0, dst);
1079 dst += components;
1080 }
1081 }
1082 }
1083 else {
1084 /* uncompressed */
1085 convertFormat = srcImage->TexFormat;
1086 }
1087
1088 mesa_format_to_type_and_comps(convertFormat, &datatype, &comps);
1089
1090 for (level = texObj->BaseLevel; level < texObj->MaxLevel
1091 && level < maxLevels - 1; level++) {
1092 /* generate image[level+1] from image[level] */
1093 const struct gl_texture_image *srcImage;
1094 struct gl_texture_image *dstImage;
1095 GLint srcWidth, srcHeight, srcDepth;
1096 GLint dstWidth, dstHeight, dstDepth;
1097 GLint border, bytesPerTexel;
1098
1099 /* get src image parameters */
1100 srcImage = _mesa_select_tex_image(ctx, texObj, target, level);
1101 ASSERT(srcImage);
1102 srcWidth = srcImage->Width;
1103 srcHeight = srcImage->Height;
1104 srcDepth = srcImage->Depth;
1105 border = srcImage->Border;
1106
1107 /* compute next (level+1) image size */
1108 if (srcWidth - 2 * border > 1) {
1109 dstWidth = (srcWidth - 2 * border) / 2 + 2 * border;
1110 }
1111 else {
1112 dstWidth = srcWidth; /* can't go smaller */
1113 }
1114 if ((srcHeight - 2 * border > 1) &&
1115 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT)) {
1116 dstHeight = (srcHeight - 2 * border) / 2 + 2 * border;
1117 }
1118 else {
1119 dstHeight = srcHeight; /* can't go smaller */
1120 }
1121 if ((srcDepth - 2 * border > 1) &&
1122 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT)) {
1123 dstDepth = (srcDepth - 2 * border) / 2 + 2 * border;
1124 }
1125 else {
1126 dstDepth = srcDepth; /* can't go smaller */
1127 }
1128
1129 if (dstWidth == srcWidth &&
1130 dstHeight == srcHeight &&
1131 dstDepth == srcDepth) {
1132 /* all done */
1133 if (srcImage->IsCompressed) {
1134 _mesa_free((void *) srcData);
1135 _mesa_free(dstData);
1136 }
1137 return;
1138 }
1139
1140 /* get dest gl_texture_image */
1141 dstImage = _mesa_get_tex_image(ctx, texObj, target, level + 1);
1142 if (!dstImage) {
1143 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
1144 return;
1145 }
1146
1147 if (dstImage->ImageOffsets)
1148 _mesa_free(dstImage->ImageOffsets);
1149
1150 /* Free old image data */
1151 if (dstImage->Data)
1152 ctx->Driver.FreeTexImageData(ctx, dstImage);
1153
1154 /* initialize new image */
1155 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
1156 dstDepth, border, srcImage->InternalFormat);
1157 dstImage->DriverData = NULL;
1158 dstImage->TexFormat = srcImage->TexFormat;
1159 dstImage->FetchTexelc = srcImage->FetchTexelc;
1160 dstImage->FetchTexelf = srcImage->FetchTexelf;
1161 dstImage->IsCompressed = srcImage->IsCompressed;
1162 if (dstImage->IsCompressed) {
1163 dstImage->CompressedSize
1164 = ctx->Driver.CompressedTextureSize(ctx, dstImage->Width,
1165 dstImage->Height,
1166 dstImage->Depth,
1167 dstImage->TexFormat->MesaFormat);
1168 ASSERT(dstImage->CompressedSize > 0);
1169 }
1170
1171 ASSERT(dstImage->TexFormat);
1172 ASSERT(dstImage->FetchTexelc);
1173 ASSERT(dstImage->FetchTexelf);
1174
1175 /* Alloc new teximage data buffer.
1176 * Setup src and dest data pointers.
1177 */
1178 if (dstImage->IsCompressed) {
1179 dstImage->Data = _mesa_alloc_texmemory(dstImage->CompressedSize);
1180 if (!dstImage->Data) {
1181 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
1182 return;
1183 }
1184 /* srcData and dstData are already set */
1185 ASSERT(srcData);
1186 ASSERT(dstData);
1187 }
1188 else {
1189 bytesPerTexel = dstImage->TexFormat->TexelBytes;
1190 ASSERT(dstWidth * dstHeight * dstDepth * bytesPerTexel > 0);
1191 dstImage->Data = _mesa_alloc_texmemory(dstWidth * dstHeight
1192 * dstDepth * bytesPerTexel);
1193 if (!dstImage->Data) {
1194 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
1195 return;
1196 }
1197 srcData = (const GLubyte *) srcImage->Data;
1198 dstData = (GLubyte *) dstImage->Data;
1199 }
1200
1201 /*
1202 * We use simple 2x2 averaging to compute the next mipmap level.
1203 */
1204 switch (target) {
1205 case GL_TEXTURE_1D:
1206 make_1d_mipmap(datatype, comps, border,
1207 srcWidth, srcData,
1208 dstWidth, dstData);
1209 break;
1210 case GL_TEXTURE_2D:
1211 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
1212 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
1213 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
1214 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
1215 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
1216 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
1217 make_2d_mipmap(datatype, comps, border,
1218 srcWidth, srcHeight, srcData,
1219 dstWidth, dstHeight, dstData);
1220 break;
1221 case GL_TEXTURE_3D:
1222 make_3d_mipmap(datatype, comps, border,
1223 srcWidth, srcHeight, srcDepth, srcData,
1224 dstWidth, dstHeight, dstDepth, dstData);
1225 break;
1226 case GL_TEXTURE_1D_ARRAY_EXT:
1227 make_1d_stack_mipmap(datatype, comps, border,
1228 srcWidth, srcData,
1229 dstWidth, dstHeight, dstData);
1230 break;
1231 case GL_TEXTURE_2D_ARRAY_EXT:
1232 make_2d_stack_mipmap(datatype, comps, border,
1233 srcWidth, srcHeight, srcData,
1234 dstWidth, dstHeight, dstDepth, dstData);
1235 break;
1236 case GL_TEXTURE_RECTANGLE_NV:
1237 /* no mipmaps, do nothing */
1238 break;
1239 default:
1240 _mesa_problem(ctx, "bad dimensions in _mesa_generate_mipmaps");
1241 return;
1242 }
1243
1244 if (dstImage->IsCompressed) {
1245 GLubyte *temp;
1246 /* compress image from dstData into dstImage->Data */
1247 const GLenum srcFormat = convertFormat->BaseFormat;
1248 GLint dstRowStride
1249 = _mesa_compressed_row_stride(dstImage->TexFormat->MesaFormat, dstWidth);
1250 ASSERT(srcFormat == GL_RGB || srcFormat == GL_RGBA);
1251 dstImage->TexFormat->StoreImage(ctx, 2, dstImage->_BaseFormat,
1252 dstImage->TexFormat,
1253 dstImage->Data,
1254 0, 0, 0, /* dstX/Y/Zoffset */
1255 dstRowStride, 0, /* strides */
1256 dstWidth, dstHeight, 1, /* size */
1257 srcFormat, CHAN_TYPE,
1258 dstData, /* src data, actually */
1259 &ctx->DefaultPacking);
1260 /* swap src and dest pointers */
1261 temp = (GLubyte *) srcData;
1262 srcData = dstData;
1263 dstData = temp;
1264 }
1265
1266 } /* loop over mipmap levels */
1267 }
1268
1269
1270 /**
1271 * Helper function for drivers which need to rescale texture images to
1272 * certain aspect ratios.
1273 * Nearest filtering only (for broken hardware that can't support
1274 * all aspect ratios). This can be made a lot faster, but I don't
1275 * really care enough...
1276 */
1277 void
1278 _mesa_rescale_teximage2d(GLuint bytesPerPixel,
1279 GLuint srcStrideInPixels,
1280 GLuint dstRowStride,
1281 GLint srcWidth, GLint srcHeight,
1282 GLint dstWidth, GLint dstHeight,
1283 const GLvoid *srcImage, GLvoid *dstImage)
1284 {
1285 GLint row, col;
1286
1287 #define INNER_LOOP( TYPE, HOP, WOP ) \
1288 for ( row = 0 ; row < dstHeight ; row++ ) { \
1289 GLint srcRow = row HOP hScale; \
1290 for ( col = 0 ; col < dstWidth ; col++ ) { \
1291 GLint srcCol = col WOP wScale; \
1292 dst[col] = src[srcRow * srcStrideInPixels + srcCol]; \
1293 } \
1294 dst = (TYPE *) ((GLubyte *) dst + dstRowStride); \
1295 } \
1296
1297 #define RESCALE_IMAGE( TYPE ) \
1298 do { \
1299 const TYPE *src = (const TYPE *)srcImage; \
1300 TYPE *dst = (TYPE *)dstImage; \
1301 \
1302 if ( srcHeight < dstHeight ) { \
1303 const GLint hScale = dstHeight / srcHeight; \
1304 if ( srcWidth < dstWidth ) { \
1305 const GLint wScale = dstWidth / srcWidth; \
1306 INNER_LOOP( TYPE, /, / ); \
1307 } \
1308 else { \
1309 const GLint wScale = srcWidth / dstWidth; \
1310 INNER_LOOP( TYPE, /, * ); \
1311 } \
1312 } \
1313 else { \
1314 const GLint hScale = srcHeight / dstHeight; \
1315 if ( srcWidth < dstWidth ) { \
1316 const GLint wScale = dstWidth / srcWidth; \
1317 INNER_LOOP( TYPE, *, / ); \
1318 } \
1319 else { \
1320 const GLint wScale = srcWidth / dstWidth; \
1321 INNER_LOOP( TYPE, *, * ); \
1322 } \
1323 } \
1324 } while (0)
1325
1326 switch ( bytesPerPixel ) {
1327 case 4:
1328 RESCALE_IMAGE( GLuint );
1329 break;
1330
1331 case 2:
1332 RESCALE_IMAGE( GLushort );
1333 break;
1334
1335 case 1:
1336 RESCALE_IMAGE( GLubyte );
1337 break;
1338 default:
1339 _mesa_problem(NULL,"unexpected bytes/pixel in _mesa_rescale_teximage2d");
1340 }
1341 }
1342
1343
1344 /**
1345 * Upscale an image by replication, not (typical) stretching.
1346 * We use this when the image width or height is less than a
1347 * certain size (4, 8) and we need to upscale an image.
1348 */
1349 void
1350 _mesa_upscale_teximage2d(GLsizei inWidth, GLsizei inHeight,
1351 GLsizei outWidth, GLsizei outHeight,
1352 GLint comps, const GLchan *src, GLint srcRowStride,
1353 GLchan *dest )
1354 {
1355 GLint i, j, k;
1356
1357 ASSERT(outWidth >= inWidth);
1358 ASSERT(outHeight >= inHeight);
1359 #if 0
1360 ASSERT(inWidth == 1 || inWidth == 2 || inHeight == 1 || inHeight == 2);
1361 ASSERT((outWidth & 3) == 0);
1362 ASSERT((outHeight & 3) == 0);
1363 #endif
1364
1365 for (i = 0; i < outHeight; i++) {
1366 const GLint ii = i % inHeight;
1367 for (j = 0; j < outWidth; j++) {
1368 const GLint jj = j % inWidth;
1369 for (k = 0; k < comps; k++) {
1370 dest[(i * outWidth + j) * comps + k]
1371 = src[ii * srcRowStride + jj * comps + k];
1372 }
1373 }
1374 }
1375 }
1376