2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * \file mipmap.c mipmap generation and teximage resizing functions.
32 #include "texcompress.h"
33 #include "texformat.h"
40 bytes_per_pixel(GLenum datatype
, GLuint comps
)
42 GLint b
= _mesa_sizeof_packed_type(datatype
);
49 mesa_format_to_type_and_comps(const struct gl_texture_format
*format
,
50 GLenum
*datatype
, GLuint
*comps
)
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
;
60 case MESA_FORMAT_RGB888
:
61 case MESA_FORMAT_BGR888
:
62 *datatype
= GL_UNSIGNED_BYTE
;
65 case MESA_FORMAT_RGB565
:
66 case MESA_FORMAT_RGB565_REV
:
67 *datatype
= GL_UNSIGNED_SHORT_5_6_5
;
71 case MESA_FORMAT_ARGB4444
:
72 case MESA_FORMAT_ARGB4444_REV
:
73 *datatype
= GL_UNSIGNED_SHORT_4_4_4_4
;
77 case MESA_FORMAT_ARGB1555
:
78 case MESA_FORMAT_ARGB1555_REV
:
79 *datatype
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
83 case MESA_FORMAT_AL88
:
84 case MESA_FORMAT_AL88_REV
:
85 *datatype
= GL_UNSIGNED_BYTE
;
88 case MESA_FORMAT_RGB332
:
89 *datatype
= GL_UNSIGNED_BYTE_3_3_2
;
97 *datatype
= GL_UNSIGNED_BYTE
;
101 case MESA_FORMAT_YCBCR
:
102 case MESA_FORMAT_YCBCR_REV
:
103 *datatype
= GL_UNSIGNED_SHORT
;
107 case MESA_FORMAT_Z24_S8
:
108 *datatype
= GL_UNSIGNED_INT
;
109 *comps
= 1; /* XXX OK? */
112 case MESA_FORMAT_Z16
:
113 *datatype
= GL_UNSIGNED_SHORT
;
117 case MESA_FORMAT_Z32
:
118 *datatype
= GL_UNSIGNED_INT
;
122 case MESA_FORMAT_SRGB8
:
123 *datatype
= GL_UNSIGNED_BYTE
;
126 case MESA_FORMAT_SRGBA8
:
127 *datatype
= GL_UNSIGNED_BYTE
;
130 case MESA_FORMAT_SL8
:
131 *datatype
= GL_UNSIGNED_BYTE
;
134 case MESA_FORMAT_SLA8
:
135 *datatype
= GL_UNSIGNED_BYTE
;
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
;
150 case MESA_FORMAT_RGBA
:
151 *datatype
= CHAN_TYPE
;
154 case MESA_FORMAT_RGB
:
155 *datatype
= CHAN_TYPE
;
158 case MESA_FORMAT_LUMINANCE_ALPHA
:
159 *datatype
= CHAN_TYPE
;
162 case MESA_FORMAT_ALPHA
:
163 case MESA_FORMAT_LUMINANCE
:
164 case MESA_FORMAT_INTENSITY
:
165 *datatype
= CHAN_TYPE
;
169 case MESA_FORMAT_RGBA_FLOAT32
:
170 *datatype
= GL_FLOAT
;
173 case MESA_FORMAT_RGBA_FLOAT16
:
174 *datatype
= GL_HALF_FLOAT_ARB
;
177 case MESA_FORMAT_RGB_FLOAT32
:
178 *datatype
= GL_FLOAT
;
181 case MESA_FORMAT_RGB_FLOAT16
:
182 *datatype
= GL_HALF_FLOAT_ARB
;
185 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32
:
186 *datatype
= GL_FLOAT
;
189 case MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16
:
190 *datatype
= GL_HALF_FLOAT_ARB
;
193 case MESA_FORMAT_ALPHA_FLOAT32
:
194 case MESA_FORMAT_LUMINANCE_FLOAT32
:
195 case MESA_FORMAT_INTENSITY_FLOAT32
:
196 *datatype
= GL_FLOAT
;
199 case MESA_FORMAT_ALPHA_FLOAT16
:
200 case MESA_FORMAT_LUMINANCE_FLOAT16
:
201 case MESA_FORMAT_INTENSITY_FLOAT16
:
202 *datatype
= GL_HALF_FLOAT_ARB
;
207 _mesa_problem(NULL
, "bad texture format in mesa_format_to_type_and_comps");
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)
223 do_row(GLenum datatype
, GLuint comps
, GLint srcWidth
,
224 const GLvoid
*srcRowA
, const GLvoid
*srcRowB
,
225 GLint dstWidth
, GLvoid
*dstRow
)
227 const GLuint k0
= (srcWidth
== dstWidth
) ? 0 : 1;
228 const GLuint colStride
= (srcWidth
== dstWidth
) ? 1 : 2;
233 /* This assertion is no longer valid with non-power-of-2 textures
234 assert(srcWidth == dstWidth || srcWidth == 2 * dstWidth);
237 if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 4) {
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;
250 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 3) {
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;
262 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 2) {
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;
273 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 1) {
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;
284 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 4) {
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;
297 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 3) {
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;
309 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 2) {
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;
320 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 1) {
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;
331 else if (datatype
== GL_FLOAT
&& comps
== 4) {
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
;
348 else if (datatype
== GL_FLOAT
&& comps
== 3) {
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
;
363 else if (datatype
== GL_FLOAT
&& comps
== 2) {
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
;
376 else if (datatype
== GL_FLOAT
&& comps
== 1) {
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
;
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
);
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
);
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
);
438 else if (datatype
== GL_HALF_FLOAT_ARB
&& comps
== 1) {
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
);
454 else if (datatype
== GL_UNSIGNED_INT
&& comps
== 1) {
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;
465 else if (datatype
== GL_UNSIGNED_SHORT_5_6_5
&& comps
== 3) {
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
;
490 else if (datatype
== GL_UNSIGNED_SHORT_4_4_4_4
&& comps
== 4) {
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
;
520 else if (datatype
== GL_UNSIGNED_SHORT_1_5_5_5_REV
&& comps
== 4) {
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
;
550 else if (datatype
== GL_UNSIGNED_BYTE_3_3_2
&& comps
== 3) {
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
;
576 _mesa_problem(NULL
, "bad format in do_row()");
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.
588 make_1d_mipmap(GLenum datatype
, GLuint comps
, GLint border
,
589 GLint srcWidth
, const GLubyte
*srcPtr
,
590 GLint dstWidth
, GLubyte
*dstPtr
)
592 const GLint bpt
= bytes_per_pixel(datatype
, comps
);
596 /* skip the border pixel, if any */
597 src
= srcPtr
+ border
* bpt
;
598 dst
= dstPtr
+ border
* bpt
;
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
);
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
,
616 * XXX need to use the tex image's row stride!
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
)
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
;
633 /* Compute src and dst pointers, skipping any border */
634 srcA
= srcPtr
+ border
* ((srcWidth
+ 1) * bpt
);
636 srcB
= srcA
+ srcRowStride
;
639 dst
= dstPtr
+ border
* ((dstWidth
+ 1) * bpt
);
641 for (row
= 0; row
< dstHeightNB
; row
++) {
642 do_row(datatype
, comps
, srcWidthNB
, srcA
, srcB
,
644 srcA
+= 2 * srcRowStride
;
645 srcB
+= 2 * srcRowStride
;
649 /* This is ugly but probably won't be used much */
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
);
664 do_row(datatype
, comps
, srcWidthNB
,
667 dstWidthNB
, dstPtr
+ bpt
);
669 do_row(datatype
, comps
, srcWidthNB
,
670 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
671 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
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
);
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
);
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
,
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
;
716 GLint bytesPerSrcImage
, bytesPerDstImage
;
717 GLint bytesPerSrcRow
, bytesPerDstRow
;
718 GLint srcImageOffset
, srcRowOffset
;
720 (void) srcDepthNB
; /* silence warnings */
722 /* Need two temporary row buffers */
723 tmpRowA
= _mesa_malloc(srcWidth
* bpt
);
726 tmpRowB
= _mesa_malloc(srcWidth
* bpt
);
732 bytesPerSrcImage
= srcWidth
* srcHeight
* bpt
;
733 bytesPerDstImage
= dstWidth
* dstHeight
* bpt
;
735 bytesPerSrcRow
= srcWidth
* bpt
;
736 bytesPerDstRow
= dstWidth
* bpt
;
738 /* Offset between adjacent src images to be averaged together */
739 srcImageOffset
= (srcDepth
== dstDepth
) ? 0 : bytesPerSrcImage
;
741 /* Offset between adjacent src rows to be averaged together */
742 srcRowOffset
= (srcHeight
== dstHeight
) ? 0 : srcWidth
* bpt
;
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.
753 _mesa_printf("mip3d %d x %d x %d -> %d x %d x %d\n",
754 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
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
;
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
;
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
;
798 /* Luckily we can leverage the make_2d_mipmap() function here! */
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),
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
++) {
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
);
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
);
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
);
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
);
843 /* average border pixels from adjacent src image pairs */
844 ASSERT(srcDepthNB
== 2 * dstDepthNB
);
845 for (img
= 0; img
< dstDepthNB
; img
++) {
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
);
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
);
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
);
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
);
881 make_1d_stack_mipmap(GLenum datatype
, GLuint comps
, GLint border
,
882 GLint srcWidth
, const GLubyte
*srcPtr
,
883 GLint dstWidth
, GLint dstHeight
, GLubyte
*dstPtr
)
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
;
895 /* Compute src and dst pointers, skipping any border */
896 src
= srcPtr
+ border
* ((srcWidth
+ 1) * bpt
);
897 dst
= dstPtr
+ border
* ((dstWidth
+ 1) * bpt
);
899 for (row
= 0; row
< dstHeightNB
; row
++) {
900 do_row(datatype
, comps
, srcWidthNB
, src
, src
,
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
,
919 * There is quite a bit of refactoring that could be done with this function
920 * and \c make_2d_mipmap.
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
,
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
;
940 /* Compute src and dst pointers, skipping any border */
941 srcA
= srcPtr
+ border
* ((srcWidth
+ 1) * bpt
);
943 srcB
= srcA
+ srcRowStride
;
946 dst
= dstPtr
+ border
* ((dstWidth
+ 1) * bpt
);
948 for (layer
= 0; layer
< dstDepthNB
; layer
++) {
949 for (row
= 0; row
< dstHeightNB
; row
++) {
950 do_row(datatype
, comps
, srcWidthNB
, srcA
, srcB
,
952 srcA
+= 2 * srcRowStride
;
953 srcB
+= 2 * srcRowStride
;
957 /* This is ugly but probably won't be used much */
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
);
972 do_row(datatype
, comps
, srcWidthNB
,
975 dstWidthNB
, dstPtr
+ bpt
);
977 do_row(datatype
, comps
, srcWidthNB
,
978 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
979 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
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
);
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
);
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.
1016 _mesa_generate_mipmap(GLcontext
*ctx
, GLenum target
,
1017 struct gl_texture_object
*texObj
)
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
;
1028 /* XXX choose cube map face here??? */
1029 srcImage
= texObj
->Image
[0][texObj
->BaseLevel
];
1032 maxLevels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
1033 ASSERT(maxLevels
> 0); /* bad target */
1035 /* Find convertFormat - the format that do_row() will process */
1036 if (srcImage
->IsCompressed
) {
1037 /* setup for compressed textures */
1039 GLint components
, size
;
1042 assert(texObj
->Target
== GL_TEXTURE_2D
);
1044 if (srcImage
->_BaseFormat
== GL_RGB
) {
1045 convertFormat
= &_mesa_texformat_rgb
;
1048 else if (srcImage
->_BaseFormat
== GL_RGBA
) {
1049 convertFormat
= &_mesa_texformat_rgba
;
1053 _mesa_problem(ctx
, "bad srcImage->_BaseFormat in _mesa_generate_mipmaps");
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
);
1063 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
1066 dstData
= (GLubyte
*) _mesa_malloc(size
/ 2); /* 1/4 would probably be OK */
1068 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
1069 _mesa_free((void *) srcData
);
1073 /* decompress base image here */
1074 dst
= (GLchan
*) srcData
;
1075 for (row
= 0; row
< srcImage
->Height
; row
++) {
1077 for (col
= 0; col
< srcImage
->Width
; col
++) {
1078 srcImage
->FetchTexelc(srcImage
, col
, row
, 0, dst
);
1085 convertFormat
= srcImage
->TexFormat
;
1088 mesa_format_to_type_and_comps(convertFormat
, &datatype
, &comps
);
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
;
1099 /* get src image parameters */
1100 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1102 srcWidth
= srcImage
->Width
;
1103 srcHeight
= srcImage
->Height
;
1104 srcDepth
= srcImage
->Depth
;
1105 border
= srcImage
->Border
;
1107 /* compute next (level+1) image size */
1108 if (srcWidth
- 2 * border
> 1) {
1109 dstWidth
= (srcWidth
- 2 * border
) / 2 + 2 * border
;
1112 dstWidth
= srcWidth
; /* can't go smaller */
1114 if ((srcHeight
- 2 * border
> 1) &&
1115 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
)) {
1116 dstHeight
= (srcHeight
- 2 * border
) / 2 + 2 * border
;
1119 dstHeight
= srcHeight
; /* can't go smaller */
1121 if ((srcDepth
- 2 * border
> 1) &&
1122 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
)) {
1123 dstDepth
= (srcDepth
- 2 * border
) / 2 + 2 * border
;
1126 dstDepth
= srcDepth
; /* can't go smaller */
1129 if (dstWidth
== srcWidth
&&
1130 dstHeight
== srcHeight
&&
1131 dstDepth
== srcDepth
) {
1133 if (srcImage
->IsCompressed
) {
1134 _mesa_free((void *) srcData
);
1135 _mesa_free(dstData
);
1140 /* get dest gl_texture_image */
1141 dstImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
+ 1);
1143 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
1147 if (dstImage
->ImageOffsets
)
1148 _mesa_free(dstImage
->ImageOffsets
);
1150 /* Free old image data */
1152 ctx
->Driver
.FreeTexImageData(ctx
, dstImage
);
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
,
1167 dstImage
->TexFormat
->MesaFormat
);
1168 ASSERT(dstImage
->CompressedSize
> 0);
1171 ASSERT(dstImage
->TexFormat
);
1172 ASSERT(dstImage
->FetchTexelc
);
1173 ASSERT(dstImage
->FetchTexelf
);
1175 /* Alloc new teximage data buffer.
1176 * Setup src and dest data pointers.
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");
1184 /* srcData and dstData are already set */
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");
1197 srcData
= (const GLubyte
*) srcImage
->Data
;
1198 dstData
= (GLubyte
*) dstImage
->Data
;
1202 * We use simple 2x2 averaging to compute the next mipmap level.
1206 make_1d_mipmap(datatype
, comps
, border
,
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
);
1222 make_3d_mipmap(datatype
, comps
, border
,
1223 srcWidth
, srcHeight
, srcDepth
, srcData
,
1224 dstWidth
, dstHeight
, dstDepth
, dstData
);
1226 case GL_TEXTURE_1D_ARRAY_EXT
:
1227 make_1d_stack_mipmap(datatype
, comps
, border
,
1229 dstWidth
, dstHeight
, dstData
);
1231 case GL_TEXTURE_2D_ARRAY_EXT
:
1232 make_2d_stack_mipmap(datatype
, comps
, border
,
1233 srcWidth
, srcHeight
, srcData
,
1234 dstWidth
, dstHeight
, dstDepth
, dstData
);
1236 case GL_TEXTURE_RECTANGLE_NV
:
1237 /* no mipmaps, do nothing */
1240 _mesa_problem(ctx
, "bad dimensions in _mesa_generate_mipmaps");
1244 if (dstImage
->IsCompressed
) {
1246 /* compress image from dstData into dstImage->Data */
1247 const GLenum srcFormat
= convertFormat
->BaseFormat
;
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
,
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
;
1266 } /* loop over mipmap levels */
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...
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
)
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]; \
1294 dst = (TYPE *) ((GLubyte *) dst + dstRowStride); \
1297 #define RESCALE_IMAGE( TYPE ) \
1299 const TYPE *src = (const TYPE *)srcImage; \
1300 TYPE *dst = (TYPE *)dstImage; \
1302 if ( srcHeight < dstHeight ) { \
1303 const GLint hScale = dstHeight / srcHeight; \
1304 if ( srcWidth < dstWidth ) { \
1305 const GLint wScale = dstWidth / srcWidth; \
1306 INNER_LOOP( TYPE, /, / ); \
1309 const GLint wScale = srcWidth / dstWidth; \
1310 INNER_LOOP( TYPE, /, * ); \
1314 const GLint hScale = srcHeight / dstHeight; \
1315 if ( srcWidth < dstWidth ) { \
1316 const GLint wScale = dstWidth / srcWidth; \
1317 INNER_LOOP( TYPE, *, / ); \
1320 const GLint wScale = srcWidth / dstWidth; \
1321 INNER_LOOP( TYPE, *, * ); \
1326 switch ( bytesPerPixel
) {
1328 RESCALE_IMAGE( GLuint
);
1332 RESCALE_IMAGE( GLushort
);
1336 RESCALE_IMAGE( GLubyte
);
1339 _mesa_problem(NULL
,"unexpected bytes/pixel in _mesa_rescale_teximage2d");
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.
1350 _mesa_upscale_teximage2d(GLsizei inWidth
, GLsizei inHeight
,
1351 GLsizei outWidth
, GLsizei outHeight
,
1352 GLint comps
, const GLchan
*src
, GLint srcRowStride
,
1357 ASSERT(outWidth
>= inWidth
);
1358 ASSERT(outHeight
>= inHeight
);
1360 ASSERT(inWidth
== 1 || inWidth
== 2 || inHeight
== 1 || inHeight
== 2);
1361 ASSERT((outWidth
& 3) == 0);
1362 ASSERT((outHeight
& 3) == 0);
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
];