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_SHORT
&& comps
== 4) {
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;
250 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 3) {
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;
262 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 1) {
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;
272 else if (datatype
== GL_UNSIGNED_SHORT
&& comps
== 2) {
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;
283 else if (datatype
== GL_UNSIGNED_INT
&& comps
== 1) {
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;
293 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 4) {
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;
306 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 3) {
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;
318 else if (datatype
== GL_UNSIGNED_SHORT_5_6_5
&& comps
== 3) {
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
;
343 else if (datatype
== GL_UNSIGNED_SHORT_4_4_4_4
&& comps
== 4) {
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
;
373 else if (datatype
== GL_UNSIGNED_SHORT_1_5_5_5_REV
&& comps
== 4) {
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
;
403 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 2) {
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;
414 else if (datatype
== GL_UNSIGNED_BYTE_3_3_2
&& comps
== 3) {
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
;
439 else if (datatype
== GL_UNSIGNED_BYTE
&& comps
== 1) {
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;
449 else if (datatype
== GL_FLOAT
&& comps
== 4) {
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
;
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
);
483 else if (datatype
== GL_FLOAT
&& comps
== 3) {
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
;
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
);
515 else if (datatype
== GL_FLOAT
&& comps
== 2) {
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
;
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
);
545 else if (datatype
== GL_FLOAT
&& comps
== 1) {
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
;
555 else if (datatype
== GL_HALF_FLOAT_ARB
&& comps
== 1) {
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
);
571 _mesa_problem(NULL
, "bad format in do_row()");
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.
583 make_1d_mipmap(GLenum datatype
, GLuint comps
, GLint border
,
584 GLint srcWidth
, const GLubyte
*srcPtr
,
585 GLint dstWidth
, GLubyte
*dstPtr
)
587 const GLint bpt
= bytes_per_pixel(datatype
, comps
);
591 /* skip the border pixel, if any */
592 src
= srcPtr
+ border
* bpt
;
593 dst
= dstPtr
+ border
* bpt
;
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
);
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
,
611 * XXX need to use the tex image's row stride!
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
)
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
;
628 /* Compute src and dst pointers, skipping any border */
629 srcA
= srcPtr
+ border
* ((srcWidth
+ 1) * bpt
);
631 srcB
= srcA
+ srcRowStride
;
634 dst
= dstPtr
+ border
* ((dstWidth
+ 1) * bpt
);
636 for (row
= 0; row
< dstHeightNB
; row
++) {
637 do_row(datatype
, comps
, srcWidthNB
, srcA
, srcB
,
639 srcA
+= 2 * srcRowStride
;
640 srcB
+= 2 * srcRowStride
;
644 /* This is ugly but probably won't be used much */
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
);
659 do_row(datatype
, comps
, srcWidthNB
,
662 dstWidthNB
, dstPtr
+ bpt
);
664 do_row(datatype
, comps
, srcWidthNB
,
665 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
666 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
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
);
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
);
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
,
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
;
711 GLint bytesPerSrcImage
, bytesPerDstImage
;
712 GLint bytesPerSrcRow
, bytesPerDstRow
;
713 GLint srcImageOffset
, srcRowOffset
;
715 (void) srcDepthNB
; /* silence warnings */
717 /* Need two temporary row buffers */
718 tmpRowA
= _mesa_malloc(srcWidth
* bpt
);
721 tmpRowB
= _mesa_malloc(srcWidth
* bpt
);
727 bytesPerSrcImage
= srcWidth
* srcHeight
* bpt
;
728 bytesPerDstImage
= dstWidth
* dstHeight
* bpt
;
730 bytesPerSrcRow
= srcWidth
* bpt
;
731 bytesPerDstRow
= dstWidth
* bpt
;
733 /* Offset between adjacent src images to be averaged together */
734 srcImageOffset
= (srcDepth
== dstDepth
) ? 0 : bytesPerSrcImage
;
736 /* Offset between adjacent src rows to be averaged together */
737 srcRowOffset
= (srcHeight
== dstHeight
) ? 0 : srcWidth
* bpt
;
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.
748 _mesa_printf("mip3d %d x %d x %d -> %d x %d x %d\n",
749 srcWidth, srcHeight, srcDepth, dstWidth, dstHeight, dstDepth);
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
;
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
;
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
;
793 /* Luckily we can leverage the make_2d_mipmap() function here! */
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),
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
++) {
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
);
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
);
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
);
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
);
838 /* average border pixels from adjacent src image pairs */
839 ASSERT(srcDepthNB
== 2 * dstDepthNB
);
840 for (img
= 0; img
< dstDepthNB
; img
++) {
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
);
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
);
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
);
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
);
876 make_1d_stack_mipmap(GLenum datatype
, GLuint comps
, GLint border
,
877 GLint srcWidth
, const GLubyte
*srcPtr
,
878 GLint dstWidth
, GLint dstHeight
, GLubyte
*dstPtr
)
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
;
890 /* Compute src and dst pointers, skipping any border */
891 src
= srcPtr
+ border
* ((srcWidth
+ 1) * bpt
);
892 dst
= dstPtr
+ border
* ((dstWidth
+ 1) * bpt
);
894 for (row
= 0; row
< dstHeightNB
; row
++) {
895 do_row(datatype
, comps
, srcWidthNB
, src
, src
,
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
,
914 * There is quite a bit of refactoring that could be done with this function
915 * and \c make_2d_mipmap.
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
,
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
;
935 /* Compute src and dst pointers, skipping any border */
936 srcA
= srcPtr
+ border
* ((srcWidth
+ 1) * bpt
);
938 srcB
= srcA
+ srcRowStride
;
941 dst
= dstPtr
+ border
* ((dstWidth
+ 1) * bpt
);
943 for (layer
= 0; layer
< dstDepthNB
; layer
++) {
944 for (row
= 0; row
< dstHeightNB
; row
++) {
945 do_row(datatype
, comps
, srcWidthNB
, srcA
, srcB
,
947 srcA
+= 2 * srcRowStride
;
948 srcB
+= 2 * srcRowStride
;
952 /* This is ugly but probably won't be used much */
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
);
967 do_row(datatype
, comps
, srcWidthNB
,
970 dstWidthNB
, dstPtr
+ bpt
);
972 do_row(datatype
, comps
, srcWidthNB
,
973 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
974 srcPtr
+ (srcWidth
* (srcHeight
- 1) + 1) * bpt
,
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
);
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
);
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.
1011 _mesa_generate_mipmap(GLcontext
*ctx
, GLenum target
,
1012 struct gl_texture_object
*texObj
)
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
;
1023 /* XXX choose cube map face here??? */
1024 srcImage
= texObj
->Image
[0][texObj
->BaseLevel
];
1027 maxLevels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
1028 ASSERT(maxLevels
> 0); /* bad target */
1030 /* Find convertFormat - the format that do_row() will process */
1031 if (srcImage
->IsCompressed
) {
1032 /* setup for compressed textures */
1034 GLint components
, size
;
1037 assert(texObj
->Target
== GL_TEXTURE_2D
);
1039 if (srcImage
->_BaseFormat
== GL_RGB
) {
1040 convertFormat
= &_mesa_texformat_rgb
;
1043 else if (srcImage
->_BaseFormat
== GL_RGBA
) {
1044 convertFormat
= &_mesa_texformat_rgba
;
1048 _mesa_problem(ctx
, "bad srcImage->_BaseFormat in _mesa_generate_mipmaps");
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
);
1058 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
1061 dstData
= (GLubyte
*) _mesa_malloc(size
/ 2); /* 1/4 would probably be OK */
1063 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generate mipmaps");
1064 _mesa_free((void *) srcData
);
1068 /* decompress base image here */
1069 dst
= (GLchan
*) srcData
;
1070 for (row
= 0; row
< srcImage
->Height
; row
++) {
1072 for (col
= 0; col
< srcImage
->Width
; col
++) {
1073 srcImage
->FetchTexelc(srcImage
, col
, row
, 0, dst
);
1080 convertFormat
= srcImage
->TexFormat
;
1083 mesa_format_to_type_and_comps(convertFormat
, &datatype
, &comps
);
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
;
1094 /* get src image parameters */
1095 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1097 srcWidth
= srcImage
->Width
;
1098 srcHeight
= srcImage
->Height
;
1099 srcDepth
= srcImage
->Depth
;
1100 border
= srcImage
->Border
;
1102 /* compute next (level+1) image size */
1103 if (srcWidth
- 2 * border
> 1) {
1104 dstWidth
= (srcWidth
- 2 * border
) / 2 + 2 * border
;
1107 dstWidth
= srcWidth
; /* can't go smaller */
1109 if ((srcHeight
- 2 * border
> 1) &&
1110 (texObj
->Target
!= GL_TEXTURE_1D_ARRAY_EXT
)) {
1111 dstHeight
= (srcHeight
- 2 * border
) / 2 + 2 * border
;
1114 dstHeight
= srcHeight
; /* can't go smaller */
1116 if ((srcDepth
- 2 * border
> 1) &&
1117 (texObj
->Target
!= GL_TEXTURE_2D_ARRAY_EXT
)) {
1118 dstDepth
= (srcDepth
- 2 * border
) / 2 + 2 * border
;
1121 dstDepth
= srcDepth
; /* can't go smaller */
1124 if (dstWidth
== srcWidth
&&
1125 dstHeight
== srcHeight
&&
1126 dstDepth
== srcDepth
) {
1128 if (srcImage
->IsCompressed
) {
1129 _mesa_free((void *) srcData
);
1130 _mesa_free(dstData
);
1135 /* get dest gl_texture_image */
1136 dstImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
+ 1);
1138 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
1142 if (dstImage
->ImageOffsets
)
1143 _mesa_free(dstImage
->ImageOffsets
);
1145 /* Free old image data */
1147 ctx
->Driver
.FreeTexImageData(ctx
, dstImage
);
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
,
1162 dstImage
->TexFormat
->MesaFormat
);
1163 ASSERT(dstImage
->CompressedSize
> 0);
1166 ASSERT(dstImage
->TexFormat
);
1167 ASSERT(dstImage
->FetchTexelc
);
1168 ASSERT(dstImage
->FetchTexelf
);
1170 /* Alloc new teximage data buffer.
1171 * Setup src and dest data pointers.
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");
1179 /* srcData and dstData are already set */
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");
1192 srcData
= (const GLubyte
*) srcImage
->Data
;
1193 dstData
= (GLubyte
*) dstImage
->Data
;
1197 * We use simple 2x2 averaging to compute the next mipmap level.
1201 make_1d_mipmap(datatype
, comps
, border
,
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
);
1217 make_3d_mipmap(datatype
, comps
, border
,
1218 srcWidth
, srcHeight
, srcDepth
, srcData
,
1219 dstWidth
, dstHeight
, dstDepth
, dstData
);
1221 case GL_TEXTURE_1D_ARRAY_EXT
:
1222 make_1d_stack_mipmap(datatype
, comps
, border
,
1224 dstWidth
, dstHeight
, dstData
);
1226 case GL_TEXTURE_2D_ARRAY_EXT
:
1227 make_2d_stack_mipmap(datatype
, comps
, border
,
1228 srcWidth
, srcHeight
, srcData
,
1229 dstWidth
, dstHeight
, dstDepth
, dstData
);
1231 case GL_TEXTURE_RECTANGLE_NV
:
1232 /* no mipmaps, do nothing */
1235 _mesa_problem(ctx
, "bad dimensions in _mesa_generate_mipmaps");
1239 if (dstImage
->IsCompressed
) {
1241 /* compress image from dstData into dstImage->Data */
1242 const GLenum srcFormat
= convertFormat
->BaseFormat
;
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
,
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
;
1261 } /* loop over mipmap levels */
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...
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
)
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]; \
1289 dst = (TYPE *) ((GLubyte *) dst + dstRowStride); \
1292 #define RESCALE_IMAGE( TYPE ) \
1294 const TYPE *src = (const TYPE *)srcImage; \
1295 TYPE *dst = (TYPE *)dstImage; \
1297 if ( srcHeight < dstHeight ) { \
1298 const GLint hScale = dstHeight / srcHeight; \
1299 if ( srcWidth < dstWidth ) { \
1300 const GLint wScale = dstWidth / srcWidth; \
1301 INNER_LOOP( TYPE, /, / ); \
1304 const GLint wScale = srcWidth / dstWidth; \
1305 INNER_LOOP( TYPE, /, * ); \
1309 const GLint hScale = srcHeight / dstHeight; \
1310 if ( srcWidth < dstWidth ) { \
1311 const GLint wScale = dstWidth / srcWidth; \
1312 INNER_LOOP( TYPE, *, / ); \
1315 const GLint wScale = srcWidth / dstWidth; \
1316 INNER_LOOP( TYPE, *, * ); \
1321 switch ( bytesPerPixel
) {
1323 RESCALE_IMAGE( GLuint
);
1327 RESCALE_IMAGE( GLushort
);
1331 RESCALE_IMAGE( GLubyte
);
1334 _mesa_problem(NULL
,"unexpected bytes/pixel in _mesa_rescale_teximage2d");
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.
1345 _mesa_upscale_teximage2d(GLsizei inWidth
, GLsizei inHeight
,
1346 GLsizei outWidth
, GLsizei outHeight
,
1347 GLint comps
, const GLchan
*src
, GLint srcRowStride
,
1352 ASSERT(outWidth
>= inWidth
);
1353 ASSERT(outHeight
>= inHeight
);
1355 ASSERT(inWidth
== 1 || inWidth
== 2 || inHeight
== 1 || inHeight
== 2);
1356 ASSERT((outWidth
& 3) == 0);
1357 ASSERT((outHeight
& 3) == 0);
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
];