1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
/*!
\class SbImage SbImage.h Inventor/SbImage.h
\brief The SbImage class is an abstract datatype for 2D and 3D images.
\ingroup base
\COIN_CLASS_EXTENSION
\since Coin 1.0
*/
// FIXME: this class could be used to handle image reusage, since it's
// quite common that the same image is used several times in a scene
// and for different contexts. The API should stay the same though.
// 20001026 mortene (original comment by pederb).
#include <Inventor/SbImage.h>
#include <cstring>
#include <cstdlib>
#include <Inventor/SbVec2s.h>
#include <Inventor/SbVec3s.h>
#include <Inventor/SbString.h>
#include <Inventor/SoInput.h> // for SoInput::searchForFile()
#include <Inventor/lists/SbStringList.h>
#include <Inventor/lists/SbList.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/C/tidbits.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
#ifdef COIN_THREADSAFE
#include <Inventor/threads/SbRWMutex.h>
#endif // COIN_THREADSAFE
#include "glue/simage_wrapper.h"
#include "coindefs.h"
#ifndef COIN_WORKAROUND_NO_USING_STD_FUNCS
using std::memcmp;
using std::memcpy;
#endif // !COIN_WORKAROUND_NO_USING_STD_FUNCS
class SbImageP {
public:
typedef struct {
SbImageReadImageCB * cb;
void * closure;
} ReadImageCBData;
enum DataType {
INTERNAL_DATA,
SIMAGE_DATA,
SETVALUEPTR_DATA
};
SbImageP(void)
: bytes(NULL),
datatype(SETVALUEPTR_DATA),
size(0,0,0),
bpp(0),
schedulecb(NULL)
#ifdef COIN_THREADSAFE
, rwmutex(SbRWMutex::READ_PRECEDENCE)
#endif // COIN_THREADSAFE
{ }
void freeData(void) {
if (this->bytes) {
switch (this->datatype) {
default:
assert(0 && "unknown data type");
break;
case INTERNAL_DATA:
delete[] this->bytes;
this->bytes = NULL;
break;
case SIMAGE_DATA:
simage_wrapper()->simage_free_image(this->bytes);
this->bytes = NULL;
break;
case SETVALUEPTR_DATA:
this->bytes = NULL;
break;
}
}
this->datatype = SETVALUEPTR_DATA;
}
unsigned char * bytes;
DataType datatype;
SbVec3s size;
int bpp;
SbString schedulename;
SbImageScheduleReadCB * schedulecb;
void * scheduleclosure;
static SbList <ReadImageCBData> * readimagecallbacks;
#ifdef COIN_THREADSAFE
SbRWMutex rwmutex;
void readLock(void) {
// fprintf(stderr,"readlock: %p\n", this);
this->rwmutex.readLock();
//fprintf(stderr,"readlock achieved: %p\n", this);
}
void readUnlock(void) {
//fprintf(stderr,"readUnlock: %p\n", this);
this->rwmutex.readUnlock();
}
void writeLock(void) {
//fprintf(stderr,"writelock: %p\n", this);
this->rwmutex.writeLock();
//fprintf(stderr,"writelock achived: %p\n", this);
}
void writeUnlock(void) {
//fprintf(stderr,"writeUnlock: %p\n", this);
this->rwmutex.writeUnlock();
}
#else // COIN_THREADSAFE
void readLock(void) { }
void readUnlock(void) { }
void writeLock(void) { }
void writeUnlock(void) { }
#endif // ! COIN_THREADSAFE
};
extern "C" {
static void SbImage_cleanup_callback(void) {
delete SbImageP::readimagecallbacks;
SbImageP::readimagecallbacks = NULL;
}
} // extern "C"
SbList <SbImageP::ReadImageCBData> * SbImageP::readimagecallbacks = NULL;
//////////////////////////////////////////////////////////////////////////
#define PRIVATE(image) ((image)->pimpl)
/*!
Default constructor.
*/
SbImage::SbImage(void)
{
PRIVATE(this) = new SbImageP;
}
/*!
Constructor which sets 2D data using setValue().
\sa setValue()
*/
SbImage::SbImage(const unsigned char * bytes,
const SbVec2s & size, const int bytesperpixel)
{
PRIVATE(this) = new SbImageP;
this->setValue(size, bytesperpixel, bytes);
}
/*!
Constructor which sets 3D data using setValue().
\COIN_FUNCTION_EXTENSION
\sa setValue()
\since Coin 2.0
*/
SbImage::SbImage(const unsigned char * bytes,
const SbVec3s & size, const int bytesperpixel)
{
PRIVATE(this) = new SbImageP;
this->setValue(size, bytesperpixel, bytes);
}
/*!
Destructor.
*/
SbImage::~SbImage(void)
{
PRIVATE(this)->freeData();
delete PRIVATE(this);
}
/*!
Apply a read lock on this image. This will make it impossible for
other threads to change the image while this lock is active. Other
threads can do read-only operations on this image, of course.
For the single thread version of Coin, this method does nothing.
\sa readUnlock()
\since Coin 2.0
*/
void
SbImage::readLock(void) const
{
PRIVATE(this)->readLock();
}
/*!
Release a read lock on this image.
For the single thread version of Coin, this method does nothing.
\sa readLock()
\since Coin 2.0
*/
void
SbImage::readUnlock(void) const
{
PRIVATE(this)->readUnlock();
}
/*!
Convenience 2D version of setValuePtr.
\sa setValue()
\since Coin 2.0
*/
void
SbImage::setValuePtr(const SbVec2s & size, const int bytesperpixel,
const unsigned char * bytes)
{
SbVec3s tmpsize(size[0], size[1], 0);
this->setValuePtr(tmpsize, bytesperpixel, bytes);
}
/*!
Sets the image data without copying the data. \a bytes will be used
directly, and the data will not be freed when the image instance is
destructed.
If the depth of the image (size[2]) is zero, the image is considered
a 2D image.
\sa setValue()
\since Coin 2.0
*/
void
SbImage::setValuePtr(const SbVec3s & size, const int bytesperpixel,
const unsigned char * bytes)
{
PRIVATE(this)->writeLock();
PRIVATE(this)->schedulename = "";
PRIVATE(this)->schedulecb = NULL;
PRIVATE(this)->freeData();
PRIVATE(this)->bytes = const_cast<unsigned char *>(bytes);
PRIVATE(this)->datatype = SbImageP::SETVALUEPTR_DATA;
PRIVATE(this)->size = size;
PRIVATE(this)->bpp = bytesperpixel;
PRIVATE(this)->writeUnlock();
}
/*!
Convenience 2D version of setValue.
*/
void
SbImage::setValue(const SbVec2s & size, const int bytesperpixel,
const unsigned char * bytes)
{
SbVec3s tmpsize(size[0], size[1], 0);
this->setValue(tmpsize, bytesperpixel, bytes);
}
/*!
Sets the image to \a size and \a bytesperpixel. If \a bytes !=
NULL, data is copied from \a bytes into this class' image data. If
\a bytes == NULL, the image data is left uninitialized.
The image data will always be allocated in multiples of four. This
means that if you set an image with size == (1,1,1) and bytesperpixel
== 1, four bytes will be allocated to hold the data. This is mainly
done to simplify the export code in SoSFImage and normally you'll
not have to worry about this feature.
If the depth of the image (size[2]) is zero, the image is considered
a 2D image.
\since Coin 2.0
*/
void
SbImage::setValue(const SbVec3s & size, const int bytesperpixel,
const unsigned char * bytes)
{
PRIVATE(this)->writeLock();
PRIVATE(this)->schedulename = "";
PRIVATE(this)->schedulecb = NULL;
if (PRIVATE(this)->bytes && PRIVATE(this)->datatype == SbImageP::INTERNAL_DATA) {
// check for special case where we don't have to reallocate
if (bytes && (size == PRIVATE(this)->size) && (bytesperpixel == PRIVATE(this)->bpp)) {
memcpy(PRIVATE(this)->bytes, bytes,
int(size[0]) * int(size[1]) * int(size[2]==0?1:size[2]) *
bytesperpixel);
PRIVATE(this)->writeUnlock();
return;
}
}
PRIVATE(this)->freeData();
PRIVATE(this)->size = size;
PRIVATE(this)->bpp = bytesperpixel;
int buffersize = int(size[0]) * int(size[1]) * int(size[2]==0?1:size[2]) *
bytesperpixel;
if (buffersize) {
// Align buffers because the binary file format has the data aligned
// (simplifies export code in SoSFImage).
buffersize = ((buffersize + 3) / 4) * 4;
PRIVATE(this)->bytes = new unsigned char[buffersize];
PRIVATE(this)->datatype = SbImageP::INTERNAL_DATA;
if (bytes) {
// Important: don't copy buffersize num bytes here!
(void)memcpy(PRIVATE(this)->bytes, bytes,
int(size[0]) * int(size[1]) * int(size[2]==0?1:size[2]) *
bytesperpixel);
}
}
PRIVATE(this)->writeUnlock();
}
/*!
Returns the 2D image data.
*/
unsigned char *
SbImage::getValue(SbVec2s & size, int & bytesperpixel) const
{
SbVec3s tmpsize;
unsigned char *bytes = this->getValue(tmpsize, bytesperpixel);
size.setValue(tmpsize[0], tmpsize[1]);
return bytes;
}
/*!
Returns the 3D image data.
\since Coin 2.0
*/
unsigned char *
SbImage::getValue(SbVec3s & size, int & bytesperpixel) const
{
PRIVATE(this)->readLock();
if (PRIVATE(this)->schedulecb) {
// start a thread to read the image.
SbBool scheduled = PRIVATE(this)->schedulecb(PRIVATE(this)->schedulename, const_cast<SbImage *>(this),
PRIVATE(this)->scheduleclosure);
if (scheduled) {
PRIVATE(this)->schedulecb = NULL;
}
}
size = PRIVATE(this)->size;
bytesperpixel = PRIVATE(this)->bpp;
unsigned char * bytes = PRIVATE(this)->bytes;
PRIVATE(this)->readUnlock();
return bytes;
}
/*!
Given a \a basename for a file and and array of directories to
search (in \a dirlist, of length \a numdirs), returns the full name
of the file found.
In addition to looking at the root of each directory in \a dirlist,
we also look into the subdirectories \e texture/, \e textures/, \e
images/, \e pics/ and \e pictures/ of each \a dirlist directory.
If no file matching \a basename could be found, returns an empty
string.
*/
SbString
SbImage::searchForFile(const SbString & basename,
const SbString * const * dirlist, const int numdirs)
{
int i;
SbStringList directories;
SbStringList subdirectories;
for (i = 0; i < numdirs; i++) {
directories.append(const_cast<SbString *>(dirlist[i]));
}
subdirectories.append(new SbString("texture"));
subdirectories.append(new SbString("textures"));
subdirectories.append(new SbString("images"));
subdirectories.append(new SbString("pics"));
subdirectories.append(new SbString("pictures"));
SbString ret = SoInput::searchForFile(basename, directories, subdirectories);
for (i = 0; i < subdirectories.getLength(); i++) {
delete subdirectories[i];
}
return ret;
}
/*!
Reads image data from \a filename. In Coin, simage is used to
load image files, and several common file formats are supported.
simage can be downloaded from our webpages. If loading
fails for some reason this method returns FALSE, and the instance
is set to an empty image. If the file is successfully loaded, the
file image data is copied into this class.
If \a numdirectories > 0, this method will search for \a filename
in all directories in \a searchdirectories.
*/
SbBool
SbImage::readFile(const SbString & filename,
const SbString * const * searchdirectories,
const int numdirectories)
{
// FIXME: Add 3D image support when that is added to simage (kintel 20011118)
if (filename.getLength() == 0) {
// This is really an internal error, should perhaps assert. <mortene>.
SoDebugError::post("SbImage::readFile",
"attempted to read file from empty filename.");
return FALSE;
}
SbString finalname = SbImage::searchForFile(filename, searchdirectories,
numdirectories);
if (finalname.getLength() == 0) {
SoDebugError::post("SbImage::readFile",
"couldn't find '%s'.", filename.getString());
return FALSE;
}
// use callback to load the image if it's set
if (SbImageP::readimagecallbacks) {
for (int i = 0; i < SbImageP::readimagecallbacks->getLength(); i++) {
SbImageP::ReadImageCBData cbdata = (*SbImageP::readimagecallbacks)[i];
if (cbdata.cb(finalname, this, cbdata.closure)) return TRUE;
}
if (!simage_wrapper()->available) {
return FALSE;
}
}
// try simage
if (!simage_wrapper()->available) {
SoDebugError::postWarning("SbImage::readFile",
"The simage library is not available, "
"can not import any images from disk.");
return FALSE;
}
assert(simage_wrapper()->simage_read_image);
int w, h, nc;
unsigned char * simagedata =
simage_wrapper()->simage_read_image(finalname.getString(),
&w, &h, &nc);
if (simagedata) {
//FIXME: Add 3'rd dimension (kintel 20011110)
this->setValuePtr(
SbVec3s(static_cast<short>(w),
static_cast<short>(h),
static_cast<short>(0)
),
nc, simagedata);
// NB, this is a trick. We use setValuePtr() to set the size
// and data pointer, and then we change the data type to simage
// peder, 2002-03-22
PRIVATE(this)->datatype = SbImageP::SIMAGE_DATA;
return TRUE;
}
#if COIN_DEBUG
else {
SoDebugError::post("SbImage::readFile", "(%s) %s",
filename.getString(),
// FIXME: "getlasterror" is a crap strategy, as
// it places extra burden on the client to
// lock. Should keep a single entry-lock within
// simage_wrapper() to work around
// this. 20020628 mortene.
simage_wrapper()->simage_get_last_error ?
simage_wrapper()->simage_get_last_error() :
"Unknown error");
}
#endif // COIN_DEBUG
this->setValue(SbVec3s(0,0,0), 0, NULL);
return FALSE;
}
/*!
\fn int SbImage::operator!=(const SbImage & image) const
Compare image of \a image with the image in this class and
return \c FALSE if they are equal.
*/
/*!
Compare image of \a image with the image in this class and
return \c TRUE if they are equal.
*/
int
SbImage::operator==(const SbImage & image) const
{
this->readLock();
int ret = 0;
if (!PRIVATE(this)->schedulecb && !PRIVATE(&image)->schedulecb) {
if (PRIVATE(this)->size != PRIVATE(&image)->size) ret = 0;
else if (PRIVATE(this)->bpp != PRIVATE(&image)->bpp) ret = 0;
else if (PRIVATE(this)->bytes == NULL || PRIVATE(&image)->bytes == NULL) {
ret = (PRIVATE(this)->bytes == PRIVATE(&image)->bytes);
}
else {
ret = memcmp(PRIVATE(this)->bytes, PRIVATE(&image)->bytes,
int(PRIVATE(this)->size[0]) *
int(PRIVATE(this)->size[1]) *
int(PRIVATE(this)->size[2]==0?1:PRIVATE(this)->size[2]) *
PRIVATE(this)->bpp) == 0;
}
}
this->readUnlock();
return ret;
}
/*!
Assignment operator.
*/
SbImage &
SbImage::operator=(const SbImage & image)
{
if (*this != image ) {
PRIVATE(this)->writeLock();
PRIVATE(this)->freeData();
PRIVATE(this)->writeUnlock();
if (PRIVATE(&image)->bytes) {
PRIVATE(&image)->readLock();
switch (PRIVATE(&image)->datatype) {
default:
assert(0 && "unknown data type");
break;
case SbImageP::INTERNAL_DATA:
case SbImageP::SIMAGE_DATA:
// need to copy data both for INTERNAL and SIMAGE data, since
// we can only free the data once when the data is of SIMAGE type.
this->setValue(PRIVATE(&image)->size,
PRIVATE(&image)->bpp,
PRIVATE(&image)->bytes);
break;
case SbImageP::SETVALUEPTR_DATA:
// just set the data ptr
this->setValuePtr(PRIVATE(&image)->size,
PRIVATE(&image)->bpp,
PRIVATE(&image)->bytes);
break;
}
PRIVATE(&image)->readUnlock();
}
}
return *this;
}
/*!
Schedule a file for reading. \a cb will be called the first time
getValue() is called for this image, and the callback should then
start a thread to read the image. Do not read the image in the
callback, as this will lock up the application.
\sa readFile()
\since Coin 2.0
*/
SbBool
SbImage::scheduleReadFile(SbImageScheduleReadCB * cb,
void * closure,
const SbString & filename,
const SbString * const * searchdirectories,
const int numdirectories)
{
this->setValue(SbVec3s(0,0,0), 0, NULL);
PRIVATE(this)->writeLock();
PRIVATE(this)->schedulecb = NULL;
PRIVATE(this)->schedulename =
this->searchForFile(filename, searchdirectories, numdirectories);
int len = PRIVATE(this)->schedulename.getLength();
if (len > 0) {
PRIVATE(this)->schedulecb = cb;
PRIVATE(this)->scheduleclosure = closure;
}
PRIVATE(this)->writeUnlock();
return len > 0;
}
/*!
Returns \a TRUE if the image is not empty. This can be useful, since
getValue() will start loading the image if scheduleReadFile() has
been used to set the image data.
\since Coin 2.0
*/
SbBool
SbImage::hasData(void) const
{
SbBool ret;
this->readLock();
ret = PRIVATE(this)->bytes != NULL;
this->readUnlock();
return ret;
}
/*!
Returns the size of the image. If this is a 2D image, the
z component is zero. If this is a 3D image, the z component is
>= 1.
\since Coin 2.0
*/
SbVec3s
SbImage::getSize(void) const
{
return PRIVATE(this)->size;
}
/*!
Add a callback which will be called whenever Coin wants to read an
image file. The callback should return TRUE if it was able to
successfully read and set the image data, and FALSE otherwise.
The callback(s) will be called before attempting to use simage to
load images.
\sa removeReadImageCB()
\since Coin 3.0
*/
void
SbImage::addReadImageCB(SbImageReadImageCB * cb, void * closure)
{
if (!SbImageP::readimagecallbacks) {
SbImageP::readimagecallbacks = new SbList <SbImageP::ReadImageCBData>;
cc_coin_atexit(static_cast<coin_atexit_f*>(SbImage_cleanup_callback));
}
SbImageP::ReadImageCBData data;
data.cb = cb;
data.closure = closure;
SbImageP::readimagecallbacks->append(data);
}
/*!
Remove a read image callback added with addReadImageCB().
\sa addReadImageCB()
\since Coin 3.0
*/
void
SbImage::removeReadImageCB(SbImageReadImageCB * cb, void * closure)
{
if (SbImageP::readimagecallbacks) {
for (int i = 0; i < SbImageP::readimagecallbacks->getLength(); i++) {
SbImageP::ReadImageCBData data = (*SbImageP::readimagecallbacks)[i];
if (data.cb == cb && data.closure == closure) {
SbImageP::readimagecallbacks->remove(i);
return;
}
}
}
}
#undef PRIVATE
|