| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Copyright (C) 1990,91 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.4 $ |
| 44 |
| |
| 45 |
| Classes: |
| 46 |
| SoTexture2 |
| 47 |
| |
| 48 |
| Author(s) : Gavin Bell |
| 49 |
| |
| 50 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 |
_______________________________________________________________________ |
| 52 |
*/ |
| 53 |
|
| 54 |
#include <Inventor/actions/SoCallbackAction.h> |
| 55 |
#include <Inventor/actions/SoGLRenderAction.h> |
| 56 |
#include <Inventor/elements/SoGLCacheContextElement.h> |
| 57 |
#include <Inventor/elements/SoGLTextureEnabledElement.h> |
| 58 |
#include <Inventor/elements/SoGLTextureImageElement.h> |
| 59 |
#include <Inventor/elements/SoTextureQualityElement.h> |
| 60 |
#include <Inventor/errors/SoDebugError.h> |
| 61 |
#include <Inventor/errors/SoReadError.h> |
| 62 |
#include <Inventor/nodes/SoTexture2.h> |
| 63 |
#include <Inventor/sensors/SoFieldSensor.h> |
| 64 |
|
| 65 |
#include <cstdio> |
| 66 |
#include <cstdlib> |
| 67 |
|
| 68 |
SO_NODE_SOURCE(SoTexture2); |
| 69 |
|
| 70 |
//////////////////////////////////////////////////////////////////////// |
| 71 |
// |
| 72 |
// Description: |
| 73 |
// Constructor |
| 74 |
// |
| 75 |
// Use: public |
| 76 |
|
| 77 |
SoTexture2::SoTexture2() |
| 78 |
// |
| 79 |
//////////////////////////////////////////////////////////////////////// |
| 80 |
{ |
| 81 |
SO_NODE_CONSTRUCTOR(SoTexture2); |
| 82 |
|
| 83 |
SO_NODE_ADD_FIELD(filename, ("")); |
| 84 |
SO_NODE_ADD_FIELD(image, (SbVec2s(0, 0), 0, 0)); |
| 85 |
SO_NODE_ADD_FIELD(wrapS, (REPEAT)); |
| 86 |
SO_NODE_ADD_FIELD(wrapT, (REPEAT)); |
| 87 |
SO_NODE_ADD_FIELD(model, (MODULATE)); |
| 88 |
SO_NODE_ADD_FIELD(blendColor, (SbColor(0,0,0))); |
| 89 |
|
| 90 |
// Set up enumerations for texture model |
| 91 |
SO_NODE_DEFINE_ENUM_VALUE(Model, MODULATE); |
| 92 |
SO_NODE_DEFINE_ENUM_VALUE(Model, DECAL); |
| 93 |
SO_NODE_DEFINE_ENUM_VALUE(Model, BLEND); |
| 94 |
|
| 95 |
SO_NODE_DEFINE_ENUM_VALUE(Wrap, REPEAT); |
| 96 |
SO_NODE_DEFINE_ENUM_VALUE(Wrap, CLAMP); |
| 97 |
|
| 98 |
// Set up info in enumerated type field |
| 99 |
SO_NODE_SET_SF_ENUM_TYPE(model, Model); |
| 100 |
SO_NODE_SET_SF_ENUM_TYPE(wrapS, Wrap); |
| 101 |
SO_NODE_SET_SF_ENUM_TYPE(wrapT, Wrap); |
| 102 |
|
| 103 |
// Set up sensors to keep the image/filename fields agreeing. |
| 104 |
// Sensors are used instead of field to field connections or raw |
| 105 |
// notification so that the fields can still be attached to/from |
| 106 |
// other fields. |
| 107 |
imageSensor = new SoFieldSensor(imageChangedCB, this); |
| 108 |
imageSensor->setPriority(0); |
| 109 |
imageSensor->attach(&image); |
| 110 |
filenameSensor = new SoFieldSensor(filenameChangedCB, this); |
| 111 |
filenameSensor->setPriority(0); |
| 112 |
filenameSensor->attach(&filename); |
| 113 |
|
| 114 |
renderList = NULL; // Display list used for rendering |
| 115 |
|
| 116 |
isBuiltIn = TRUE; |
| 117 |
} |
| 118 |
|
| 119 |
//////////////////////////////////////////////////////////////////////// |
| 120 |
// |
| 121 |
// Description: |
| 122 |
// Destructor |
| 123 |
// |
| 124 |
// Use: private |
| 125 |
|
| 126 |
SoTexture2::~SoTexture2() |
| 127 |
// |
| 128 |
//////////////////////////////////////////////////////////////////////// |
| 129 |
{ |
| 130 |
if (renderList) { |
| 131 |
renderList->unref(); |
| 132 |
renderList = NULL; |
| 133 |
} |
| 134 |
delete imageSensor; |
| 135 |
delete filenameSensor; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
//////////////////////////////////////////////////////////////////////// |
| 140 |
// |
| 141 |
// Description: |
| 142 |
// Reads into instance of SoTexture2. Returns FALSE on error. |
| 143 |
// |
| 144 |
// Use: protected |
| 145 |
|
| 146 |
SbBool |
| 147 |
SoTexture2::readInstance(SoInput *in, unsigned short flags) |
| 148 |
// |
| 149 |
//////////////////////////////////////////////////////////////////////// |
| 150 |
{ |
| 151 |
// Detach filename sensor temporarily |
| 152 |
filenameSensor->detach(); |
| 153 |
|
| 154 |
// Read field info as usual. |
| 155 |
SbBool readOK = SoNode::readInstance(in, flags); |
| 156 |
|
| 157 |
if (readOK && !filename.isDefault()) { |
| 158 |
// See the comment in SoFile::readInstance for why we do this |
| 159 |
// and don't just let the FieldSensor take care of reading in |
| 160 |
// the image. |
| 161 |
setReadStatus(readOK); |
| 162 |
(*(filenameSensor->getFunction()))(filenameSensor->getData(), NULL); |
| 163 |
|
| 164 |
// Don't set readOK, because not being able to read the image |
| 165 |
// isn't a fatal error. But do issue a read error: |
| 166 |
if (getReadStatus() == FALSE) |
| 167 |
SoReadError::post(in, "Could not read texture file %s", |
| 168 |
filename.getValue().getString()); |
| 169 |
} |
| 170 |
|
| 171 |
// Reattach sensor |
| 172 |
filenameSensor->attach(&filename); |
| 173 |
|
| 174 |
return readOK; |
| 175 |
} |
| 176 |
|
| 177 |
//////////////////////////////////////////////////////////////////////// |
| 178 |
// |
| 179 |
// Description: |
| 180 |
// Callback that sets the filename to "" whenever the image field |
| 181 |
// is changed. |
| 182 |
// |
| 183 |
// Use: static, internal |
| 184 |
|
| 185 |
void |
| 186 |
SoTexture2::imageChangedCB(void *data, SoSensor *) |
| 187 |
// |
| 188 |
//////////////////////////////////////////////////////////////////////// |
| 189 |
{ |
| 190 |
SoTexture2 *tex = (SoTexture2 *)data; |
| 191 |
|
| 192 |
if (tex->image.isIgnored()) return; |
| 193 |
|
| 194 |
tex->filenameSensor->detach(); |
| 195 |
tex->filename.setValue(""); |
| 196 |
tex->filename.setDefault(TRUE); |
| 197 |
tex->filenameSensor->attach(&tex->filename); |
| 198 |
|
| 199 |
if (tex->renderList) { |
| 200 |
tex->renderList->unref(); |
| 201 |
tex->renderList = NULL; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
//////////////////////////////////////////////////////////////////////// |
| 206 |
// |
| 207 |
// Description: |
| 208 |
// Callback that reads in the given file and stuffs it into the |
| 209 |
// image field whenever the filename field changes. |
| 210 |
// |
| 211 |
// Use: static, internal |
| 212 |
|
| 213 |
void |
| 214 |
SoTexture2::filenameChangedCB(void *data, SoSensor *) |
| 215 |
// |
| 216 |
//////////////////////////////////////////////////////////////////////// |
| 217 |
{ |
| 218 |
SoTexture2 *tex = (SoTexture2 *)data; |
| 219 |
|
| 220 |
if (tex->filename.isIgnored()) { |
| 221 |
tex->setReadStatus(FALSE); |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
// Read in image file right away... |
| 226 |
int nx, ny, nc; |
| 227 |
unsigned char *bytes; |
| 228 |
SbBool result = readImage(tex->filename.getValue(), nx, ny, nc, bytes); |
| 229 |
if (!result) { |
| 230 |
// Read error is taken care of by readImage() call |
| 231 |
nx = ny = nc = 0; |
| 232 |
bytes = NULL; |
| 233 |
} |
| 234 |
// Detach the image sensor temporarily... |
| 235 |
tex->imageSensor->detach(); |
| 236 |
|
| 237 |
// Set the image to the right value: |
| 238 |
tex->image.setValue(SbVec2s(nx, ny), nc, bytes); |
| 239 |
|
| 240 |
// And set its default bit so it isn't written out |
| 241 |
tex->image.setDefault(TRUE); |
| 242 |
|
| 243 |
if (bytes != NULL) delete [] bytes; |
| 244 |
|
| 245 |
if (tex->renderList) { |
| 246 |
tex->renderList->unref(); |
| 247 |
tex->renderList = NULL; |
| 248 |
} |
| 249 |
tex->imageSensor->attach(&tex->image); |
| 250 |
|
| 251 |
tex->setReadStatus(result); |
| 252 |
} |
| 253 |
|
| 254 |
//////////////////////////////////////////////////////////////////////// |
| 255 |
// |
| 256 |
// Description: |
| 257 |
// Performs typical action |
| 258 |
// |
| 259 |
// Use: extender |
| 260 |
|
| 261 |
void |
| 262 |
SoTexture2::doAction(SoAction *action) |
| 263 |
// |
| 264 |
//////////////////////////////////////////////////////////////////////// |
| 265 |
{ |
| 266 |
SoState * state = action->getState(); |
| 267 |
|
| 268 |
if (image.isIgnored() || |
| 269 |
SoTextureOverrideElement::getImageOverride(state)) |
| 270 |
return; // Texture being overriden or this node ignored |
| 271 |
if (isOverride()) { |
| 272 |
SoTextureOverrideElement::setImageOverride(state, TRUE); |
| 273 |
} |
| 274 |
|
| 275 |
SbVec2s size; |
| 276 |
int nc; |
| 277 |
const unsigned char *bytes = image.getValue(size, nc); |
| 278 |
|
| 279 |
SoTextureImageElement::set(state, this, size, nc, bytes, |
| 280 |
wrapS.getValue(), wrapT.getValue(), |
| 281 |
model.getValue(), blendColor.getValue()); |
| 282 |
} |
| 283 |
|
| 284 |
//////////////////////////////////////////////////////////////////////// |
| 285 |
// |
| 286 |
// Description: |
| 287 |
// Performs callback action |
| 288 |
// |
| 289 |
// Use: extender |
| 290 |
|
| 291 |
void |
| 292 |
SoTexture2::callback(SoCallbackAction *action) |
| 293 |
// |
| 294 |
//////////////////////////////////////////////////////////////////////// |
| 295 |
{ |
| 296 |
SoTexture2::doAction(action); |
| 297 |
} |
| 298 |
|
| 299 |
//////////////////////////////////////////////////////////////////////// |
| 300 |
// |
| 301 |
// Description: |
| 302 |
// Performs GL rendering on a texture node. |
| 303 |
// |
| 304 |
// Use: extender |
| 305 |
|
| 306 |
void |
| 307 |
SoTexture2::GLRender(SoGLRenderAction *action) |
| 308 |
// |
| 309 |
//////////////////////////////////////////////////////////////////////// |
| 310 |
{ |
| 311 |
SoState * state = action->getState(); |
| 312 |
|
| 313 |
if (image.isIgnored() || |
| 314 |
SoTextureOverrideElement::getImageOverride(state)) |
| 315 |
return; // Texture being overriden or this node ignored |
| 316 |
if (isOverride()) { |
| 317 |
SoTextureOverrideElement::setImageOverride(state, TRUE); |
| 318 |
} |
| 319 |
|
| 320 |
SbVec2s size; |
| 321 |
int nc; |
| 322 |
const unsigned char *bytes = image.getValue(size, nc); |
| 323 |
int numBytes = size[0]*size[1]*nc; |
| 324 |
|
| 325 |
float texQuality = SoTextureQualityElement::get(state); |
| 326 |
if (texQuality == 0 || numBytes == 0 || image.isIgnored()) { |
| 327 |
SoGLTextureEnabledElement::set(state, FALSE); |
| 328 |
return; |
| 329 |
} |
| 330 |
else { |
| 331 |
SoGLTextureEnabledElement::set(state, TRUE); |
| 332 |
} |
| 333 |
|
| 334 |
// Check for special cases of 1/2 component texture and model |
| 335 |
// DECAL or 3/4 component texture and model BLEND; print out |
| 336 |
// errors in these cases: |
| 337 |
|
| 338 |
int m = model.getValue(); |
| 339 |
if (nc < 3 && m == DECAL) { |
| 340 |
#ifdef DEBUG |
| 341 |
SoDebugError::post("SoTexture2::GLRender", |
| 342 |
"Texture model is DECAL, but texture image" |
| 343 |
" has only %d components (must be 3 or 4). " |
| 344 |
"Use imgcopy to convert the image.", nc); |
| 345 |
#endif |
| 346 |
SoGLTextureEnabledElement::set(state, FALSE); |
| 347 |
} |
| 348 |
else if (nc > 2 && m == BLEND) { |
| 349 |
#ifdef DEBUG |
| 350 |
SoDebugError::post("SoTexture2::GLRender", |
| 351 |
"Texture model is BLEND, but texture image" |
| 352 |
" has %d components (must be 1 or 2). " |
| 353 |
"Use imgcopy to convert the image.", nc); |
| 354 |
#endif |
| 355 |
SoGLTextureEnabledElement::set(state, FALSE); |
| 356 |
} else { |
| 357 |
// This is kind of weird-- the element builds and uses the |
| 358 |
// display list (which is why we pass it in and assign |
| 359 |
// it) because it sends the GL calls, and needs to know |
| 360 |
// the list if the state is popped. But this node must |
| 361 |
// manage storage and deletion of the display list, since |
| 362 |
// the list must go away if the node is deleted or the |
| 363 |
// image is changed. |
| 364 |
|
| 365 |
// See if renderList is valid (in the right context, with |
| 366 |
// the right texture quality): |
| 367 |
int context = SoGLCacheContextElement::get(state); |
| 368 |
if (renderList && renderList->getContext() == context && |
| 369 |
texQuality == renderListQuality) { |
| 370 |
SoGLTextureImageElement::set( |
| 371 |
state, this, size, nc, bytes, texQuality, |
| 372 |
wrapS.getValue(), wrapT.getValue(), |
| 373 |
m, blendColor.getValue(), renderList); |
| 374 |
} // Not valid, try to build |
| 375 |
else { |
| 376 |
// Free up old list, if necessary: |
| 377 |
if (renderList) { |
| 378 |
renderList->unref(state); |
| 379 |
renderList = NULL; |
| 380 |
} |
| 381 |
renderList = SoGLTextureImageElement::set( |
| 382 |
state, this, size, nc, bytes, texQuality, |
| 383 |
wrapS.getValue(), wrapT.getValue(), |
| 384 |
m, blendColor.getValue(), NULL); |
| 385 |
if (renderList) |
| 386 |
renderList->ref(); |
| 387 |
renderListQuality = texQuality; |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
// gross stuff to read images, grabbed from image.h and the image |
| 393 |
// library. Had to grab it since the real .h file is not c++ compatible. |
| 394 |
extern "C" { |
| 395 |
|
| 396 |
#include <setjmp.h> |
| 397 |
|
| 398 |
#include "readGIF.h" |
| 399 |
#include <jpeglib.h> |
| 400 |
|
| 401 |
typedef struct { |
| 402 |
unsigned short imagic; /* stuff saved on disk . . */ |
| 403 |
unsigned short type; |
| 404 |
unsigned short dim; |
| 405 |
unsigned short xsize; |
| 406 |
unsigned short ysize; |
| 407 |
unsigned short zsize; |
| 408 |
uint32_t min; |
| 409 |
uint32_t max; |
| 410 |
uint32_t wastebytes; |
| 411 |
char name[80]; |
| 412 |
uint32_t colormap; |
| 413 |
} IMAGE; |
| 414 |
extern IMAGE *fiopen(int fd, const char *mode); |
| 415 |
extern int getrow(IMAGE *, short *, int, int); |
| 416 |
extern void iclose(IMAGE *); |
| 417 |
extern void i_seterror(void (*func)(char *, int, int, int, int)); |
| 418 |
}; /* end of extern "C" */ |
| 419 |
|
| 420 |
void errfunc(char *, int, int, int, int) { } |
| 421 |
|
| 422 |
SbBool ReadSGIImage(const SoInput& in, int &w, int &h, int &nc, |
| 423 |
unsigned char *&bytes) { |
| 424 |
|
| 425 |
i_seterror(errfunc); |
| 426 |
|
| 427 |
IMAGE *image_in; |
| 428 |
int i, j, row; |
| 429 |
|
| 430 |
if ( (image_in = fiopen(fileno(in.getCurFile()), "r")) == NULL) |
| 431 |
return FALSE; |
| 432 |
|
| 433 |
w = image_in->xsize; |
| 434 |
h = image_in->ysize; |
| 435 |
nc = image_in->zsize; |
| 436 |
|
| 437 |
bytes = new unsigned char[w*h*nc]; |
| 438 |
|
| 439 |
short *rbuf = new short[w]; |
| 440 |
|
| 441 |
int readOK = TRUE; |
| 442 |
|
| 443 |
for (row = 0; row < h; row++) { |
| 444 |
for (i = 0; i < nc; i++) { |
| 445 |
if (getrow(image_in, rbuf, row, i) < 0) { |
| 446 |
row = h; // Don't read any more rows |
| 447 |
readOK = FALSE; |
| 448 |
break; |
| 449 |
} |
| 450 |
for (j = 0; j < w; j++) { |
| 451 |
bytes[row*w*nc + j*nc + i] = |
| 452 |
(unsigned char) rbuf[j]; |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
delete [] rbuf; |
| 457 |
|
| 458 |
iclose(image_in); |
| 459 |
|
| 460 |
return TRUE; |
| 461 |
} |
| 462 |
|
| 463 |
SbBool ReadGIFImage(const SoInput& in, int &w, int &h, int &nc, |
| 464 |
unsigned char *&bytes) { |
| 465 |
|
| 466 |
int ncolors; |
| 467 |
int bgIndex; |
| 468 |
int errCode; |
| 469 |
XColor *colors = (XColor*) malloc(GIF_MAXCOLORMAPSIZE * sizeof(XColor)); |
| 470 |
FILE *fp = in.getCurFile(); |
| 471 |
fseek(fp, 0, SEEK_SET); |
| 472 |
|
| 473 |
if (fp == NULL) return FALSE; |
| 474 |
|
| 475 |
unsigned char *array = readGIF(fp, |
| 476 |
&w, &h, colors, &ncolors, &bgIndex, &errCode); |
| 477 |
if (errCode != GIF_NO_ERROR) { |
| 478 |
free(colors); |
| 479 |
return FALSE; |
| 480 |
} |
| 481 |
|
| 482 |
nc = 3; |
| 483 |
|
| 484 |
// convert color map image to rgb |
| 485 |
// gif files go top down and we need bottom up. Switch it. |
| 486 |
bytes = new unsigned char[w*h*nc]; |
| 487 |
|
| 488 |
int i, j; |
| 489 |
|
| 490 |
for (j = 0; j < h; ++j) |
| 491 |
for (i = 0; i < w; ++i) { |
| 492 |
int c = array[j*w+i]; |
| 493 |
int index = ((h-j-1)*w+i)*nc; |
| 494 |
|
| 495 |
if (c < 0 || c >= ncolors) |
| 496 |
// store black if out of range |
| 497 |
bytes[index] = bytes[index+1] = bytes[index+2] = 0; |
| 498 |
else { |
| 499 |
bytes[index] = colors[c].red / 256; |
| 500 |
bytes[index+1] = colors[c].green / 256; |
| 501 |
bytes[index+2] = colors[c].blue / 256; |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
free(colors); |
| 506 |
free(array); |
| 507 |
|
| 508 |
return TRUE; |
| 509 |
} |
| 510 |
|
| 511 |
// JPEG error handler |
| 512 |
|
| 513 |
struct my_error_mgr { |
| 514 |
struct jpeg_error_mgr pub; /* "public" fields */ |
| 515 |
|
| 516 |
jmp_buf setjmp_buffer; /* for return to caller */ |
| 517 |
}; |
| 518 |
|
| 519 |
typedef struct my_error_mgr * my_error_ptr; |
| 520 |
|
| 521 |
/* |
| 522 |
* Here's the routine that will replace the standard error_exit method: |
| 523 |
*/ |
| 524 |
|
| 525 |
METHODDEF(void) |
| 526 |
my_error_exit (j_common_ptr cinfo) |
| 527 |
{ |
| 528 |
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */ |
| 529 |
my_error_ptr myerr = (my_error_ptr) cinfo->err; |
| 530 |
|
| 531 |
/* Always display the message. */ |
| 532 |
/* We could postpone this until after returning, if we chose. */ |
| 533 |
(*cinfo->err->output_message) (cinfo); |
| 534 |
|
| 535 |
/* Return control to the setjmp point */ |
| 536 |
longjmp(myerr->setjmp_buffer, 1); |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
SbBool ReadJPEGImage(const SoInput& in, int &w, int &h, int &nc, |
| 541 |
unsigned char *&bytes) { |
| 542 |
|
| 543 |
/* This struct contains the JPEG decompression parameters and pointers to |
| 544 |
* working space (which is allocated as needed by the JPEG library). |
| 545 |
*/ |
| 546 |
struct jpeg_decompress_struct cinfo; |
| 547 |
/* We use our private extension JPEG error handler. */ |
| 548 |
struct my_error_mgr jerr; |
| 549 |
|
| 550 |
/* More stuff */ |
| 551 |
FILE * infile; /* source file */ |
| 552 |
JSAMPARRAY buffer; /* Output row buffer */ |
| 553 |
int row_stride; /* physical row width in output buffer */ |
| 554 |
|
| 555 |
infile = in.getCurFile(); |
| 556 |
fseek(infile, 0, SEEK_SET); |
| 557 |
|
| 558 |
/* Step 1: allocate and initialize JPEG decompression object */ |
| 559 |
|
| 560 |
/* We set up the normal JPEG error routines, then override error_exit. */ |
| 561 |
cinfo.err = jpeg_std_error(&jerr.pub); |
| 562 |
jerr.pub.error_exit = my_error_exit; |
| 563 |
|
| 564 |
/* Establish the setjmp return context for my_error_exit to use. */ |
| 565 |
if (setjmp(jerr.setjmp_buffer)) { |
| 566 |
/* If we get here, the JPEG code has signaled an error. |
| 567 |
* We need to clean up the JPEG object, close the input file, and return. |
| 568 |
*/ |
| 569 |
jpeg_destroy_decompress(&cinfo); |
| 570 |
return FALSE; |
| 571 |
} |
| 572 |
|
| 573 |
/* Now we can initialize the JPEG decompression object. */ |
| 574 |
jpeg_create_decompress(&cinfo); |
| 575 |
|
| 576 |
/* Step 2: specify data source (eg, a file) */ |
| 577 |
|
| 578 |
jpeg_stdio_src(&cinfo, infile); |
| 579 |
|
| 580 |
/* Step 3: read file parameters with jpeg_read_header() */ |
| 581 |
|
| 582 |
jpeg_read_header(&cinfo, TRUE); |
| 583 |
|
| 584 |
/* Step 5: Start decompressor */ |
| 585 |
|
| 586 |
jpeg_start_decompress(&cinfo); |
| 587 |
|
| 588 |
// make buffer for data to be put into |
| 589 |
w = cinfo.output_width; |
| 590 |
h = cinfo.output_height; |
| 591 |
nc = cinfo.output_components; |
| 592 |
bytes = new unsigned char[w*h*nc]; |
| 593 |
|
| 594 |
row_stride = w*nc; |
| 595 |
/* Make a one-row-high sample array that will go away when done with image */ |
| 596 |
buffer = (*cinfo.mem->alloc_sarray) |
| 597 |
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); |
| 598 |
|
| 599 |
unsigned char *curline = bytes+(h-1)*w*nc; |
| 600 |
|
| 601 |
while (cinfo.output_scanline < cinfo.output_height) { |
| 602 |
jpeg_read_scanlines(&cinfo, buffer, 1); |
| 603 |
|
| 604 |
// put the data from the sample buffer into the output buffer |
| 605 |
for (int i = 0; i < row_stride; ++i) |
| 606 |
curline[i] = (unsigned char) (buffer[0][i]); |
| 607 |
curline -= w*nc; |
| 608 |
} |
| 609 |
|
| 610 |
/* Step 7: Finish decompression */ |
| 611 |
|
| 612 |
jpeg_finish_decompress(&cinfo); |
| 613 |
|
| 614 |
/* Step 8: Release JPEG decompression object */ |
| 615 |
|
| 616 |
/* This is an important step since it will release a good deal of memory. */ |
| 617 |
jpeg_destroy_decompress(&cinfo); |
| 618 |
|
| 619 |
/* At this point you may want to check to see whether any corrupt-data |
| 620 |
* warnings occurred (test whether jerr.pub.num_warnings is nonzero). |
| 621 |
*/ |
| 622 |
|
| 623 |
return TRUE; |
| 624 |
} |
| 625 |
|
| 626 |
//////////////////////////////////////////////////////////////////////// |
| 627 |
// |
| 628 |
// Description: |
| 629 |
// read passed image file |
| 630 |
// |
| 631 |
// Use: static, protected |
| 632 |
|
| 633 |
SbBool |
| 634 |
SoTexture2::readImage(const SbString& fname, int &w, int &h, int &nc, |
| 635 |
unsigned char *&bytes) |
| 636 |
// |
| 637 |
//////////////////////////////////////////////////////////////////////// |
| 638 |
{ |
| 639 |
w = h = nc = 0; |
| 640 |
bytes = NULL; |
| 641 |
|
| 642 |
// Empty file means an empty image... |
| 643 |
if (fname.getString()[0] == '\0') |
| 644 |
return TRUE; |
| 645 |
|
| 646 |
SoInput in; |
| 647 |
if (!in.openFile(fname.getString(), TRUE)) { |
| 648 |
return FALSE; |
| 649 |
} |
| 650 |
|
| 651 |
#ifdef DEBUG |
| 652 |
SoDebugError::postInfo("SoTexture2::readImage", |
| 653 |
"Reading texture image %s", |
| 654 |
fname.getString()); |
| 655 |
#endif |
| 656 |
|
| 657 |
if (ReadSGIImage(in, w, h, nc, bytes)) |
| 658 |
return TRUE; |
| 659 |
|
| 660 |
// fiopen() closes the file even if it can't read the data, so |
| 661 |
// reopen it |
| 662 |
in.closeFile(); |
| 663 |
if (!in.openFile(fname.getString(), TRUE)) |
| 664 |
return FALSE; |
| 665 |
|
| 666 |
if (ReadGIFImage(in, w, h, nc, bytes)) |
| 667 |
return TRUE; |
| 668 |
|
| 669 |
if (ReadJPEGImage(in, w, h, nc, bytes)) |
| 670 |
return TRUE; |
| 671 |
|
| 672 |
return FALSE; |
| 673 |
} |