| 1 |
/* $Id: mipmap.c,v 1.2 2000/11/02 00:28:54 mholst Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Mesa 3-D graphics library |
| 5 |
* Version: 2.0 |
| 6 |
* Copyright (C) 1995-1996 Brian Paul |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Library General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Library General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Library General Public |
| 19 |
* License along with this library; if not, write to the Free |
| 20 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <assert.h> |
| 24 |
#include <math.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include "gluP.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
/* |
| 32 |
* Compute ceiling of integer quotient of A divided by B: |
| 33 |
*/ |
| 34 |
#define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 ) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#define EPSILON 0.001 |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
GLint gluScaleImage( GLenum format, |
| 44 |
GLint widthin, GLint heightin, |
| 45 |
GLenum typein, const void *datain, |
| 46 |
GLint widthout, GLint heightout, |
| 47 |
GLenum typeout, void *dataout ) |
| 48 |
{ |
| 49 |
GLuint components, i, j, k; |
| 50 |
GLfloat *tempin, *tempout; |
| 51 |
GLfloat sx, sy; |
| 52 |
GLint unpackrowlength, unpackalignment, unpackskiprows, unpackskippixels; |
| 53 |
GLint packrowlength, packalignment, packskiprows, packskippixels; |
| 54 |
GLint sizein, sizeout; |
| 55 |
GLint rowstride, rowlen; |
| 56 |
|
| 57 |
|
| 58 |
/* Determine number of components per pixel */ |
| 59 |
switch (format) { |
| 60 |
case GL_COLOR_INDEX: |
| 61 |
case GL_STENCIL_INDEX: |
| 62 |
case GL_DEPTH_COMPONENT: |
| 63 |
case GL_RED: |
| 64 |
case GL_GREEN: |
| 65 |
case GL_BLUE: |
| 66 |
case GL_ALPHA: |
| 67 |
case GL_LUMINANCE: |
| 68 |
components = 1; |
| 69 |
break; |
| 70 |
case GL_LUMINANCE_ALPHA: |
| 71 |
components = 2; |
| 72 |
break; |
| 73 |
case GL_RGB: |
| 74 |
components = 3; |
| 75 |
break; |
| 76 |
case GL_RGBA: |
| 77 |
components = 4; |
| 78 |
break; |
| 79 |
default: |
| 80 |
return GLU_INVALID_ENUM; |
| 81 |
} |
| 82 |
|
| 83 |
/* Determine bytes per input datum */ |
| 84 |
switch (typein) { |
| 85 |
case GL_UNSIGNED_BYTE: sizein = sizeof(GLubyte); break; |
| 86 |
case GL_BYTE: sizein = sizeof(GLbyte); break; |
| 87 |
case GL_UNSIGNED_SHORT: sizein = sizeof(GLushort); break; |
| 88 |
case GL_SHORT: sizein = sizeof(GLshort); break; |
| 89 |
case GL_UNSIGNED_INT: sizein = sizeof(GLuint); break; |
| 90 |
case GL_INT: sizein = sizeof(GLint); break; |
| 91 |
case GL_FLOAT: sizein = sizeof(GLfloat); break; |
| 92 |
case GL_BITMAP: |
| 93 |
/* not implemented yet */ |
| 94 |
default: |
| 95 |
return GL_INVALID_ENUM; |
| 96 |
} |
| 97 |
|
| 98 |
/* Determine bytes per output datum */ |
| 99 |
switch (typeout) { |
| 100 |
case GL_UNSIGNED_BYTE: sizeout = sizeof(GLubyte); break; |
| 101 |
case GL_BYTE: sizeout = sizeof(GLbyte); break; |
| 102 |
case GL_UNSIGNED_SHORT: sizeout = sizeof(GLushort); break; |
| 103 |
case GL_SHORT: sizeout = sizeof(GLshort); break; |
| 104 |
case GL_UNSIGNED_INT: sizeout = sizeof(GLuint); break; |
| 105 |
case GL_INT: sizeout = sizeof(GLint); break; |
| 106 |
case GL_FLOAT: sizeout = sizeof(GLfloat); break; |
| 107 |
case GL_BITMAP: |
| 108 |
/* not implemented yet */ |
| 109 |
default: |
| 110 |
return GL_INVALID_ENUM; |
| 111 |
} |
| 112 |
|
| 113 |
/* Get glPixelStore state */ |
| 114 |
glGetIntegerv( GL_UNPACK_ROW_LENGTH, &unpackrowlength ); |
| 115 |
glGetIntegerv( GL_UNPACK_ALIGNMENT, &unpackalignment ); |
| 116 |
glGetIntegerv( GL_UNPACK_SKIP_ROWS, &unpackskiprows ); |
| 117 |
glGetIntegerv( GL_UNPACK_SKIP_PIXELS, &unpackskippixels ); |
| 118 |
glGetIntegerv( GL_PACK_ROW_LENGTH, &packrowlength ); |
| 119 |
glGetIntegerv( GL_PACK_ALIGNMENT, &packalignment ); |
| 120 |
glGetIntegerv( GL_PACK_SKIP_ROWS, &packskiprows ); |
| 121 |
glGetIntegerv( GL_PACK_SKIP_PIXELS, &packskippixels ); |
| 122 |
|
| 123 |
/* Allocate storage for intermediate images */ |
| 124 |
tempin = (GLfloat *) malloc( widthin * heightin |
| 125 |
* components * sizeof(GLfloat) ); |
| 126 |
if (!tempin) { |
| 127 |
return GLU_OUT_OF_MEMORY; |
| 128 |
} |
| 129 |
tempout = (GLfloat *) malloc( widthout * heightout |
| 130 |
* components * sizeof(GLfloat) ); |
| 131 |
if (!tempout) { |
| 132 |
free( tempin ); |
| 133 |
return GLU_OUT_OF_MEMORY; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/* |
| 138 |
* Unpack the pixel data and convert to floating point |
| 139 |
*/ |
| 140 |
|
| 141 |
if (unpackrowlength>0) { |
| 142 |
rowlen = unpackrowlength; |
| 143 |
} |
| 144 |
else { |
| 145 |
rowlen = widthin; |
| 146 |
} |
| 147 |
if (sizein >= unpackalignment) { |
| 148 |
rowstride = components * rowlen; |
| 149 |
} |
| 150 |
else { |
| 151 |
rowstride = unpackalignment/sizein |
| 152 |
* CEILING( components * rowlen * sizein, unpackalignment ); |
| 153 |
} |
| 154 |
|
| 155 |
switch (typein) { |
| 156 |
case GL_UNSIGNED_BYTE: |
| 157 |
k = 0; |
| 158 |
for (i=0;i<heightin;i++) { |
| 159 |
GLubyte *ubptr = (GLubyte *) datain |
| 160 |
+ i * rowstride |
| 161 |
+ unpackskiprows * rowstride |
| 162 |
+ unpackskippixels * components; |
| 163 |
for (j=0;j<widthin*components;j++) { |
| 164 |
tempin[k++] = (GLfloat) *ubptr++; |
| 165 |
} |
| 166 |
} |
| 167 |
break; |
| 168 |
case GL_BYTE: |
| 169 |
k = 0; |
| 170 |
for (i=0;i<heightin;i++) { |
| 171 |
GLbyte *bptr = (GLbyte *) datain |
| 172 |
+ i * rowstride |
| 173 |
+ unpackskiprows * rowstride |
| 174 |
+ unpackskippixels * components; |
| 175 |
for (j=0;j<widthin*components;j++) { |
| 176 |
tempin[k++] = (GLfloat) *bptr++; |
| 177 |
} |
| 178 |
} |
| 179 |
break; |
| 180 |
case GL_UNSIGNED_SHORT: |
| 181 |
k = 0; |
| 182 |
for (i=0;i<heightin;i++) { |
| 183 |
GLushort *usptr = (GLushort *) datain |
| 184 |
+ i * rowstride |
| 185 |
+ unpackskiprows * rowstride |
| 186 |
+ unpackskippixels * components; |
| 187 |
for (j=0;j<widthin*components;j++) { |
| 188 |
tempin[k++] = (GLfloat) *usptr++; |
| 189 |
} |
| 190 |
} |
| 191 |
break; |
| 192 |
case GL_SHORT: |
| 193 |
k = 0; |
| 194 |
for (i=0;i<heightin;i++) { |
| 195 |
GLshort *sptr = (GLshort *) datain |
| 196 |
+ i * rowstride |
| 197 |
+ unpackskiprows * rowstride |
| 198 |
+ unpackskippixels * components; |
| 199 |
for (j=0;j<widthin*components;j++) { |
| 200 |
tempin[k++] = (GLfloat) *sptr++; |
| 201 |
} |
| 202 |
} |
| 203 |
break; |
| 204 |
case GL_UNSIGNED_INT: |
| 205 |
k = 0; |
| 206 |
for (i=0;i<heightin;i++) { |
| 207 |
GLuint *uiptr = (GLuint *) datain |
| 208 |
+ i * rowstride |
| 209 |
+ unpackskiprows * rowstride |
| 210 |
+ unpackskippixels * components; |
| 211 |
for (j=0;j<widthin*components;j++) { |
| 212 |
tempin[k++] = (GLfloat) *uiptr++; |
| 213 |
} |
| 214 |
} |
| 215 |
break; |
| 216 |
case GL_INT: |
| 217 |
k = 0; |
| 218 |
for (i=0;i<heightin;i++) { |
| 219 |
GLint *iptr = (GLint *) datain |
| 220 |
+ i * rowstride |
| 221 |
+ unpackskiprows * rowstride |
| 222 |
+ unpackskippixels * components; |
| 223 |
for (j=0;j<widthin*components;j++) { |
| 224 |
tempin[k++] = (GLfloat) *iptr++; |
| 225 |
} |
| 226 |
} |
| 227 |
break; |
| 228 |
case GL_FLOAT: |
| 229 |
k = 0; |
| 230 |
for (i=0;i<heightin;i++) { |
| 231 |
GLfloat *fptr = (GLfloat *) datain |
| 232 |
+ i * rowstride |
| 233 |
+ unpackskiprows * rowstride |
| 234 |
+ unpackskippixels * components; |
| 235 |
for (j=0;j<widthin*components;j++) { |
| 236 |
tempin[k++] = *fptr++; |
| 237 |
} |
| 238 |
} |
| 239 |
break; |
| 240 |
default: |
| 241 |
return GLU_INVALID_ENUM; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
/* |
| 246 |
* Scale the image! |
| 247 |
*/ |
| 248 |
|
| 249 |
sx = (GLfloat) widthin / (GLfloat) widthout; |
| 250 |
sy = (GLfloat) heightin / (GLfloat) heightout; |
| 251 |
|
| 252 |
/*#define POINT_SAMPLE*/ |
| 253 |
#ifdef POINT_SAMPLE |
| 254 |
for (i=0;i<heightout;i++) { |
| 255 |
GLint ii = i * sy; |
| 256 |
for (j=0;j<widthout;j++) { |
| 257 |
GLint jj = j * sx; |
| 258 |
|
| 259 |
GLfloat *src = tempin + (ii * widthin + jj) * components; |
| 260 |
GLfloat *dst = tempout + (i * widthout + j) * components; |
| 261 |
|
| 262 |
for (k=0;k<components;k++) { |
| 263 |
*dst++ = *src++; |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
#else |
| 268 |
if (sx<1.0 && sy<1.0) { |
| 269 |
/* magnify both width and height: use weighted sample of 4 pixels */ |
| 270 |
GLint i0, i1, j0, j1; |
| 271 |
GLfloat alpha, beta; |
| 272 |
GLfloat *src00, *src01, *src10, *src11; |
| 273 |
GLfloat s1, s2; |
| 274 |
GLfloat *dst; |
| 275 |
|
| 276 |
for (i=0;i<heightout;i++) { |
| 277 |
i0 = i * sy; |
| 278 |
i1 = (i+1) * sy - EPSILON; |
| 279 |
alpha = i*sy - i0; |
| 280 |
for (j=0;j<widthout;j++) { |
| 281 |
j0 = j * sx; |
| 282 |
j1 = (j+1) * sx - EPSILON; |
| 283 |
beta = j*sx - j0; |
| 284 |
|
| 285 |
/* compute weighted average of pixels in rect (i0,j0)-(i1,j1) */ |
| 286 |
src00 = tempin + (i0 * widthin + j0) * components; |
| 287 |
src01 = tempin + (i0 * widthin + j1) * components; |
| 288 |
src10 = tempin + (i1 * widthin + j0) * components; |
| 289 |
src11 = tempin + (i1 * widthin + j1) * components; |
| 290 |
|
| 291 |
dst = tempout + (i * widthout + j) * components; |
| 292 |
|
| 293 |
for (k=0;k<components;k++) { |
| 294 |
s1 = *src00++ * (1.0-beta) + *src01++ * beta; |
| 295 |
s2 = *src10++ * (1.0-beta) + *src11++ * beta; |
| 296 |
*dst++ = s1 * (1.0-alpha) + s2 * alpha; |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
else { |
| 302 |
/* shrink width and/or height: use an unweighted box filter */ |
| 303 |
GLint i0, i1; |
| 304 |
GLint j0, j1; |
| 305 |
GLint ii, jj; |
| 306 |
GLfloat sum, *dst; |
| 307 |
|
| 308 |
for (i=0;i<heightout;i++) { |
| 309 |
i0 = i * sy; |
| 310 |
i1 = (i+1) * sy - EPSILON; |
| 311 |
for (j=0;j<widthout;j++) { |
| 312 |
j0 = j * sx; |
| 313 |
j1 = (j+1) * sx - EPSILON; |
| 314 |
|
| 315 |
dst = tempout + (i * widthout + j) * components; |
| 316 |
|
| 317 |
/* compute average of pixels in the rectangle (i0,j0)-(i1,j1) */ |
| 318 |
for (k=0;k<components;k++) { |
| 319 |
sum = 0.0; |
| 320 |
for (ii=i0;ii<=i1;ii++) { |
| 321 |
for (jj=j0;jj<=j1;jj++) { |
| 322 |
sum += *(tempin + (ii * widthin + jj) * components + k); |
| 323 |
} |
| 324 |
} |
| 325 |
sum /= (j1-j0+1) * (i1-i0+1); |
| 326 |
*dst++ = sum; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
#endif |
| 332 |
|
| 333 |
|
| 334 |
/* |
| 335 |
* Return output image |
| 336 |
*/ |
| 337 |
|
| 338 |
if (packrowlength>0) { |
| 339 |
rowlen = packrowlength; |
| 340 |
} |
| 341 |
else { |
| 342 |
rowlen = widthout; |
| 343 |
} |
| 344 |
if (sizeout >= packalignment) { |
| 345 |
rowstride = components * rowlen; |
| 346 |
} |
| 347 |
else { |
| 348 |
rowstride = packalignment/sizeout |
| 349 |
* CEILING( components * rowlen * sizeout, packalignment ); |
| 350 |
} |
| 351 |
|
| 352 |
switch (typeout) { |
| 353 |
case GL_UNSIGNED_BYTE: |
| 354 |
k = 0; |
| 355 |
for (i=0;i<heightout;i++) { |
| 356 |
GLubyte *ubptr = (GLubyte *) dataout |
| 357 |
+ i * rowstride |
| 358 |
+ packskiprows * rowstride |
| 359 |
+ packskippixels * components; |
| 360 |
for (j=0;j<widthout*components;j++) { |
| 361 |
*ubptr++ = (GLubyte) tempout[k++]; |
| 362 |
} |
| 363 |
} |
| 364 |
break; |
| 365 |
case GL_BYTE: |
| 366 |
k = 0; |
| 367 |
for (i=0;i<heightout;i++) { |
| 368 |
GLbyte *bptr = (GLbyte *) dataout |
| 369 |
+ i * rowstride |
| 370 |
+ packskiprows * rowstride |
| 371 |
+ packskippixels * components; |
| 372 |
for (j=0;j<widthout*components;j++) { |
| 373 |
*bptr++ = (GLbyte) tempout[k++]; |
| 374 |
} |
| 375 |
} |
| 376 |
break; |
| 377 |
case GL_UNSIGNED_SHORT: |
| 378 |
k = 0; |
| 379 |
for (i=0;i<heightout;i++) { |
| 380 |
GLushort *usptr = (GLushort *) dataout |
| 381 |
+ i * rowstride |
| 382 |
+ packskiprows * rowstride |
| 383 |
+ packskippixels * components; |
| 384 |
for (j=0;j<widthout*components;j++) { |
| 385 |
*usptr++ = (GLushort) tempout[k++]; |
| 386 |
} |
| 387 |
} |
| 388 |
break; |
| 389 |
case GL_SHORT: |
| 390 |
k = 0; |
| 391 |
for (i=0;i<heightout;i++) { |
| 392 |
GLshort *sptr = (GLshort *) dataout |
| 393 |
+ i * rowstride |
| 394 |
+ packskiprows * rowstride |
| 395 |
+ packskippixels * components; |
| 396 |
for (j=0;j<widthout*components;j++) { |
| 397 |
*sptr++ = (GLshort) tempout[k++]; |
| 398 |
} |
| 399 |
} |
| 400 |
break; |
| 401 |
case GL_UNSIGNED_INT: |
| 402 |
k = 0; |
| 403 |
for (i=0;i<heightout;i++) { |
| 404 |
GLuint *uiptr = (GLuint *) dataout |
| 405 |
+ i * rowstride |
| 406 |
+ packskiprows * rowstride |
| 407 |
+ packskippixels * components; |
| 408 |
for (j=0;j<widthout*components;j++) { |
| 409 |
*uiptr++ = (GLuint) tempout[k++]; |
| 410 |
} |
| 411 |
} |
| 412 |
break; |
| 413 |
case GL_INT: |
| 414 |
k = 0; |
| 415 |
for (i=0;i<heightout;i++) { |
| 416 |
GLint *iptr = (GLint *) dataout |
| 417 |
+ i * rowstride |
| 418 |
+ packskiprows * rowstride |
| 419 |
+ packskippixels * components; |
| 420 |
for (j=0;j<widthout*components;j++) { |
| 421 |
*iptr++ = (GLint) tempout[k++]; |
| 422 |
} |
| 423 |
} |
| 424 |
break; |
| 425 |
case GL_FLOAT: |
| 426 |
k = 0; |
| 427 |
for (i=0;i<heightout;i++) { |
| 428 |
GLfloat *fptr = (GLfloat *) dataout |
| 429 |
+ i * rowstride |
| 430 |
+ packskiprows * rowstride |
| 431 |
+ packskippixels * components; |
| 432 |
for (j=0;j<widthout*components;j++) { |
| 433 |
*fptr++ = tempout[k++]; |
| 434 |
} |
| 435 |
} |
| 436 |
break; |
| 437 |
default: |
| 438 |
return GLU_INVALID_ENUM; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
/* free temporary image storage */ |
| 443 |
free( tempin ); |
| 444 |
free( tempout ); |
| 445 |
|
| 446 |
return 0; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
/* |
| 452 |
* Return the largest k such that 2^k <= n. |
| 453 |
*/ |
| 454 |
static GLint ilog2( GLint n ) |
| 455 |
{ |
| 456 |
GLint k; |
| 457 |
|
| 458 |
if (n<=0) return 0; |
| 459 |
for (k=0; n>>=1; k++) ; |
| 460 |
return k; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
/* |
| 466 |
* Find the value nearest to n which is also a power of two. |
| 467 |
*/ |
| 468 |
static GLint round2( GLint n ) |
| 469 |
{ |
| 470 |
GLint m; |
| 471 |
|
| 472 |
for (m=1; m<n; m*=2) |
| 473 |
; |
| 474 |
|
| 475 |
/* m>=n */ |
| 476 |
if (m-n <= n-m/2) { |
| 477 |
return m; |
| 478 |
} |
| 479 |
else { |
| 480 |
return m/2; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
/* |
| 486 |
* Given an pixel format and datatype, return the number of bytes to |
| 487 |
* store one pixel. |
| 488 |
*/ |
| 489 |
static GLint bytes_per_pixel( GLenum format, GLenum type ) |
| 490 |
{ |
| 491 |
GLint n, m; |
| 492 |
|
| 493 |
switch (format) { |
| 494 |
case GL_COLOR_INDEX: |
| 495 |
case GL_STENCIL_INDEX: |
| 496 |
case GL_DEPTH_COMPONENT: |
| 497 |
case GL_RED: |
| 498 |
case GL_GREEN: |
| 499 |
case GL_BLUE: |
| 500 |
case GL_ALPHA: |
| 501 |
case GL_LUMINANCE: |
| 502 |
n = 1; |
| 503 |
break; |
| 504 |
case GL_LUMINANCE_ALPHA: |
| 505 |
n = 2; |
| 506 |
break; |
| 507 |
case GL_RGB: |
| 508 |
n = 3; |
| 509 |
break; |
| 510 |
case GL_RGBA: |
| 511 |
n = 4; |
| 512 |
break; |
| 513 |
default: |
| 514 |
n = 0; |
| 515 |
} |
| 516 |
|
| 517 |
switch (type) { |
| 518 |
case GL_UNSIGNED_BYTE: m = sizeof(GLubyte); break; |
| 519 |
case GL_BYTE: m = sizeof(GLbyte); break; |
| 520 |
case GL_BITMAP: m = 1; break; |
| 521 |
case GL_UNSIGNED_SHORT: m = sizeof(GLushort); break; |
| 522 |
case GL_SHORT: m = sizeof(GLshort); break; |
| 523 |
case GL_UNSIGNED_INT: m = sizeof(GLuint); break; |
| 524 |
case GL_INT: m = sizeof(GLint); break; |
| 525 |
case GL_FLOAT: m = sizeof(GLfloat); break; |
| 526 |
default: m = 0; |
| 527 |
} |
| 528 |
|
| 529 |
return n * m; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
/* |
| 535 |
* WARNING: This function isn't finished and has never been tested!!!! |
| 536 |
*/ |
| 537 |
GLint gluBuild1DMipmaps( GLenum target, GLint components, |
| 538 |
GLint width, GLenum format, |
| 539 |
GLenum type, const void *data ) |
| 540 |
{ |
| 541 |
GLubyte *texture; |
| 542 |
GLint levels, max_levels; |
| 543 |
GLint new_width, max_width; |
| 544 |
GLint i, j, k, l; |
| 545 |
|
| 546 |
glGetIntegerv( GL_MAX_TEXTURE_SIZE, &max_width ); |
| 547 |
max_levels = ilog2( max_width ) + 1; |
| 548 |
|
| 549 |
/* Compute how many mipmap images to make */ |
| 550 |
levels = ilog2( width ) + 1; |
| 551 |
if (levels>max_levels) { |
| 552 |
levels = max_levels; |
| 553 |
} |
| 554 |
|
| 555 |
new_width = 1 << (levels-1); |
| 556 |
|
| 557 |
texture = (GLubyte *) malloc( new_width * components ); |
| 558 |
if (!texture) { |
| 559 |
return GLU_OUT_OF_MEMORY; |
| 560 |
} |
| 561 |
|
| 562 |
if (width != new_width) { |
| 563 |
/* initial rescaling */ |
| 564 |
switch (type) { |
| 565 |
case GL_UNSIGNED_BYTE: |
| 566 |
{ |
| 567 |
GLubyte *ub_data = (GLubyte *) data; |
| 568 |
for (i=0;i<new_width;i++) { |
| 569 |
j = i * width / new_width; |
| 570 |
for (k=0;k<components;k++) { |
| 571 |
texture[i*components+k] = ub_data[j*components+k]; |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
break; |
| 576 |
default: |
| 577 |
/* Not implemented */ |
| 578 |
return GLU_ERROR; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
/* generate and load mipmap images */ |
| 583 |
for (l=0;l<levels;l++) { |
| 584 |
glTexImage1D( GL_TEXTURE_1D, l, components, new_width, 0, |
| 585 |
format, GL_UNSIGNED_BYTE, texture ); |
| 586 |
|
| 587 |
/* Scale image down to 1/2 size */ |
| 588 |
new_width = new_width / 2; |
| 589 |
for (i=0;i<new_width;i++) { |
| 590 |
for (k=0;k<components;k++) { |
| 591 |
GLint sample1, sample2; |
| 592 |
sample1 = (GLint) texture[i*2*components+k]; |
| 593 |
sample2 = (GLint) texture[(i*2+1)*components+k]; |
| 594 |
texture[i*components+k] = (GLubyte) ((sample1 + sample2) / 2); |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
free( texture ); |
| 600 |
|
| 601 |
/* make sure remaining mipmap levels are removed */ |
| 602 |
for (l=levels;l<max_levels;l++) { |
| 603 |
glTexImage1D( GL_TEXTURE_1D, l, components, 0, 0, |
| 604 |
format, GL_UNSIGNED_BYTE, NULL ); |
| 605 |
} |
| 606 |
|
| 607 |
return 0; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
GLint gluBuild2DMipmaps( GLenum target, GLint components, |
| 613 |
GLint width, GLint height, GLenum format, |
| 614 |
GLenum type, const void *data ) |
| 615 |
{ |
| 616 |
GLint w, h, maxsize; |
| 617 |
void *image, *newimage; |
| 618 |
GLint neww, newh, level, bpp; |
| 619 |
int error; |
| 620 |
|
| 621 |
glGetIntegerv( GL_MAX_TEXTURE_SIZE, &maxsize ); |
| 622 |
|
| 623 |
w = round2( width ); |
| 624 |
if (w>maxsize) { |
| 625 |
w = maxsize; |
| 626 |
} |
| 627 |
h = round2( height ); |
| 628 |
if (h>maxsize) { |
| 629 |
h = maxsize; |
| 630 |
} |
| 631 |
|
| 632 |
bpp = bytes_per_pixel( format, type ); |
| 633 |
if (bpp==0) { |
| 634 |
/* probably a bad format or type enum */ |
| 635 |
return GLU_INVALID_ENUM; |
| 636 |
} |
| 637 |
|
| 638 |
if (w!=width || h!=height) { |
| 639 |
/* must rescale image to get "top" mipmap texture image */ |
| 640 |
image = malloc( (w+4) * h * bpp ); |
| 641 |
if (!image) { |
| 642 |
return GLU_OUT_OF_MEMORY; |
| 643 |
} |
| 644 |
error = gluScaleImage( format, width, height, type, data, |
| 645 |
w, h, type, image ); |
| 646 |
if (error) { |
| 647 |
return error; |
| 648 |
} |
| 649 |
} |
| 650 |
else { |
| 651 |
image = (void *) data; |
| 652 |
} |
| 653 |
|
| 654 |
level = 0; |
| 655 |
while (1) { |
| 656 |
glTexImage2D( target, level, components, w, h, 0, format, type, image ); |
| 657 |
|
| 658 |
if (w==1 && h==1) break; |
| 659 |
|
| 660 |
neww = (w<2) ? 1 : w/2; |
| 661 |
newh = (h<2) ? 1 : h/2; |
| 662 |
newimage = malloc( (neww+4) * newh * bpp ); |
| 663 |
if (!newimage) { |
| 664 |
return GLU_OUT_OF_MEMORY; |
| 665 |
} |
| 666 |
|
| 667 |
error = gluScaleImage( format, w, h, type, image, |
| 668 |
neww, newh, type, newimage ); |
| 669 |
if (error) { |
| 670 |
return error; |
| 671 |
} |
| 672 |
|
| 673 |
if (image!=data) { |
| 674 |
free( image ); |
| 675 |
} |
| 676 |
image = newimage; |
| 677 |
|
| 678 |
w = neww; |
| 679 |
h = newh; |
| 680 |
level++; |
| 681 |
} |
| 682 |
|
| 683 |
if (image!=data) { |
| 684 |
free( image ); |
| 685 |
} |
| 686 |
|
| 687 |
return 0; |
| 688 |
} |
| 689 |
|