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