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 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
|
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Martio Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#include "pixmap.h"
#include <stdexcept>
#include <cstdio>
#include <cassert>
#include "util.h"
#include "math_util.h"
#include "random.h"
#include "leak_dumper.h"
using namespace Shared::Util;
using namespace std;
namespace Shared{ namespace Graphics{
using namespace Util;
// =====================================================
// file structs
// =====================================================
#pragma pack(push, 1)
struct BitmapFileHeader{
uint8 type1;
uint8 type2;
uint32 size;
uint16 reserved1;
uint16 reserved2;
uint32 offsetBits;
};
struct BitmapInfoHeader{
uint32 size;
int32 width;
int32 height;
uint16 planes;
uint16 bitCount;
uint32 compression;
uint32 sizeImage;
int32 xPelsPerMeter;
int32 yPelsPerMeter;
uint32 clrUsed;
uint32 clrImportant;
};
struct TargaFileHeader{
int8 idLength;
int8 colourMapType;
int8 dataTypeCode;
int16 colourMapOrigin;
int16 colourMapLength;
int8 colourMapDepth;
int16 xOrigin;
int16 yOrigin;
int16 width;
int16 height;
int8 bitsPerPixel;
int8 imageDescriptor;
};
#pragma pack(pop)
const int tgaUncompressedRgb= 2;
const int tgaUncompressedBw= 3;
// =====================================================
// class PixmapIoTga
// =====================================================
PixmapIoTga::PixmapIoTga(){
file= NULL;
}
PixmapIoTga::~PixmapIoTga(){
if(file!=NULL){
fclose(file);
}
}
void PixmapIoTga::openRead(const string &path){
file= fopen(path.c_str(),"rb");
if (file==NULL){
throw runtime_error("Can't open TGA file: "+ path);
}
//read header
TargaFileHeader fileHeader;
fread(&fileHeader, sizeof(TargaFileHeader), 1, file);
//check that we can load this tga file
if(fileHeader.idLength!=0){
throw runtime_error(path + ": id field is not 0");
}
if(fileHeader.dataTypeCode!=tgaUncompressedRgb && fileHeader.dataTypeCode!=tgaUncompressedBw){
throw runtime_error(path + ": only uncompressed BW and RGB targa images are supported");
}
//check bits per pixel
if(fileHeader.bitsPerPixel!=8 && fileHeader.bitsPerPixel!=24 && fileHeader.bitsPerPixel!=32){
throw runtime_error(path + ": only 8, 24 and 32 bit targa images are supported");
}
h= fileHeader.height;
w= fileHeader.width;
components= fileHeader.bitsPerPixel/8;
}
void PixmapIoTga::read(uint8 *pixels){
read(pixels, components);
}
void PixmapIoTga::read(uint8 *pixels, int components){
for(int i=0; i<h*w*components; i+=components){
uint8 r, g, b, a, l;
if(this->components==1){
fread(&l, 1, 1, file);
r= l;
g= l;
b= l;
a= 255;
}
else{
fread(&b, 1, 1, file);
fread(&g, 1, 1, file);
fread(&r, 1, 1, file);
if(this->components==4){
fread(&a, 1, 1, file);
}
else{
a= 255;
}
l= (r+g+b)/3;
}
switch(components){
case 1:
pixels[i]= l;
break;
case 3:
pixels[i]= r;
pixels[i+1]= g;
pixels[i+2]= b;
break;
case 4:
pixels[i]= r;
pixels[i+1]= g;
pixels[i+2]= b;
pixels[i+3]= a;
break;
}
}
}
void PixmapIoTga::openWrite(const string &path, int w, int h, int components){
this->w= w;
this->h= h;
this->components= components;
file= fopen(path.c_str(),"wb");
if (file==NULL){
throw runtime_error("Can't open TGA file: "+ path);
}
TargaFileHeader fileHeader;
memset(&fileHeader, 0, sizeof(TargaFileHeader));
fileHeader.dataTypeCode= components==1? tgaUncompressedBw: tgaUncompressedRgb;
fileHeader.bitsPerPixel= components*8;
fileHeader.width= w;
fileHeader.height= h;
fileHeader.imageDescriptor= components==4? 8: 0;
fwrite(&fileHeader, sizeof(TargaFileHeader), 1, file);
}
void PixmapIoTga::write(uint8 *pixels){
if(components==1){
fwrite(pixels, h*w, 1, file);
}
else{
for(int i=0; i<h*w*components; i+=components){
fwrite(&pixels[i+2], 1, 1, file);
fwrite(&pixels[i+1], 1, 1, file);
fwrite(&pixels[i], 1, 1, file);
if(components==4){
fwrite(&pixels[i+3], 1, 1, file);
}
}
}
}
// =====================================================
// class PixmapIoBmp
// =====================================================
PixmapIoBmp::PixmapIoBmp(){
file= NULL;
}
PixmapIoBmp::~PixmapIoBmp(){
if(file!=NULL){
fclose(file);
}
}
void PixmapIoBmp::openRead(const string &path){
file= fopen(path.c_str(),"rb");
if (file==NULL){
throw runtime_error("Can't open BMP file: "+ path);
}
//read file header
BitmapFileHeader fileHeader;
fread(&fileHeader, sizeof(BitmapFileHeader), 1, file);
if(fileHeader.type1!='B' || fileHeader.type2!='M'){
throw runtime_error(path +" is not a bitmap");
}
//read info header
BitmapInfoHeader infoHeader;
fread(&infoHeader, sizeof(BitmapInfoHeader), 1, file);
if(infoHeader.bitCount!=24){
throw runtime_error(path+" is not a 24 bit bitmap");
}
h= infoHeader.height;
w= infoHeader.width;
components= 3;
}
void PixmapIoBmp::read(uint8 *pixels){
read(pixels, 3);
}
void PixmapIoBmp::read(uint8 *pixels, int components){
for(int i=0; i<h*w*components; i+=components){
uint8 r, g, b;
fread(&b, 1, 1, file);
fread(&g, 1, 1, file);
fread(&r, 1, 1, file);
switch(components){
case 1:
pixels[i]= (r+g+b)/3;
break;
case 3:
pixels[i]= r;
pixels[i+1]= g;
pixels[i+2]= b;
break;
case 4:
pixels[i]= r;
pixels[i+1]= g;
pixels[i+2]= b;
pixels[i+3]= 255;
break;
}
}
}
void PixmapIoBmp::openWrite(const string &path, int w, int h, int components){
this->w= w;
this->h= h;
this->components= components;
file= fopen(path.c_str(),"wb");
if (file==NULL){
throw runtime_error("Can't open BMP file for writting: "+ path);
}
BitmapFileHeader fileHeader;
fileHeader.type1='B';
fileHeader.type2='M';
fileHeader.offsetBits=sizeof(BitmapFileHeader)+sizeof(BitmapInfoHeader);
fileHeader.size=sizeof(BitmapFileHeader)+sizeof(BitmapInfoHeader)+3*h*w;
fwrite(&fileHeader, sizeof(BitmapFileHeader), 1, file);
//info header
BitmapInfoHeader infoHeader;
infoHeader.bitCount=24;
infoHeader.clrImportant=0;
infoHeader.clrUsed=0;
infoHeader.compression=0;
infoHeader.height= h;
infoHeader.planes=1;
infoHeader.size=sizeof(BitmapInfoHeader);
infoHeader.sizeImage=0;
infoHeader.width= w;
infoHeader.xPelsPerMeter= 0;
infoHeader.yPelsPerMeter= 0;
fwrite(&infoHeader, sizeof(BitmapInfoHeader), 1, file);
}
void PixmapIoBmp::write(uint8 *pixels){
for (int i=0; i<h*w*components; i+=components){
fwrite(&pixels[i+2], 1, 1, file);
fwrite(&pixels[i+1], 1, 1, file);
fwrite(&pixels[i], 1, 1, file);
}
}
// =====================================================
// class Pixmap1D
// =====================================================
// ===================== PUBLIC ========================
Pixmap1D::Pixmap1D(){
w= -1;
components= -1;
pixels= NULL;
}
Pixmap1D::Pixmap1D(int components){
init(components);
}
Pixmap1D::Pixmap1D(int w, int components){
init(w, components);
}
void Pixmap1D::init(int components){
this->w= -1;
this->components= components;
pixels= NULL;
}
void Pixmap1D::init(int w, int components){
this->w= w;
this->components= components;
pixels= new uint8[w*components];
}
Pixmap1D::~Pixmap1D(){
delete [] pixels;
}
void Pixmap1D::load(const string &path){
string extension= path.substr(path.find_last_of('.')+1);
if(extension=="bmp"){
loadBmp(path);
}
else if(extension=="tga"){
loadTga(path);
}
else{
throw runtime_error("Unknown pixmap extension: "+extension);
}
}
void Pixmap1D::loadBmp(const string &path){
PixmapIoBmp plb;
plb.openRead(path);
//init
if(plb.getH()==1){
w= plb.getW();
}
else if(plb.getW()==1){
w= plb.getH();
}
else{
throw runtime_error("One of the texture dimensions must be 1");
}
if(components==-1){
components= 3;
}
if(pixels==NULL){
pixels= new uint8[w*components];
}
//data
plb.read(pixels, components);
}
void Pixmap1D::loadTga(const string &path){
PixmapIoTga plt;
plt.openRead(path);
//init
if(plt.getH()==1){
w= plt.getW();
}
else if(plt.getW()==1){
w= plt.getH();
}
else{
throw runtime_error("One of the texture dimensions must be 1");
}
int fileComponents= plt.getComponents();
if(components==-1){
components= fileComponents;
}
if(pixels==NULL){
pixels= new uint8[w*components];
}
//read data
plt.read(pixels, components);
}
// =====================================================
// class Pixmap2D
// =====================================================
// ===================== PUBLIC ========================
Pixmap2D::Pixmap2D(){
h= -1;
w= -1;
components= -1;
pixels= NULL;
}
Pixmap2D::Pixmap2D(int components){
init(components);
}
Pixmap2D::Pixmap2D(int w, int h, int components){
init(w, h, components);
}
void Pixmap2D::init(int components){
this->w= -1;
this->h= -1;
this->components= components;
pixels= NULL;
}
void Pixmap2D::init(int w, int h, int components){
this->w= w;
this->h= h;
this->components= components;
pixels= new uint8[h*w*components];
}
Pixmap2D::~Pixmap2D(){
delete [] pixels;
}
void Pixmap2D::load(const string &path){
string extension= path.substr(path.find_last_of('.')+1);
if(extension=="bmp"){
loadBmp(path);
}
else if(extension=="tga"){
loadTga(path);
}
else{
throw runtime_error("Unknown pixmap extension: "+extension);
}
}
void Pixmap2D::loadBmp(const string &path){
PixmapIoBmp plb;
plb.openRead(path);
//init
w= plb.getW();
h= plb.getH();
if(components==-1){
components= 3;
}
if(pixels==NULL){
pixels= new uint8[w*h*components];
}
//data
plb.read(pixels, components);
}
void Pixmap2D::loadTga(const string &path){
PixmapIoTga plt;
plt.openRead(path);
w= plt.getW();
h= plt.getH();
//header
int fileComponents= plt.getComponents();
//init
if(components==-1){
components= fileComponents;
}
if(pixels==NULL){
pixels= new uint8[w*h*components];
}
//read data
plt.read(pixels, components);
}
void Pixmap2D::save(const string &path){
string extension= path.substr(path.find_last_of('.')+1);
if(extension=="bmp"){
saveBmp(path);
}
else if(extension=="tga"){
saveTga(path);
}
else{
throw runtime_error("Unknown pixmap extension: "+extension);
}
}
void Pixmap2D::saveBmp(const string &path){
PixmapIoBmp psb;
psb.openWrite(path, w, h, components);
psb.write(pixels);
}
void Pixmap2D::saveTga(const string &path){
PixmapIoTga pst;
pst.openWrite(path, w, h, components);
pst.write(pixels);
}
void Pixmap2D::getPixel(int x, int y, uint8 *value) const{
for(int i=0; i<components; ++i){
value[i]= pixels[(w*y+x)*components+i];
}
}
void Pixmap2D::getPixel(int x, int y, float32 *value) const{
for(int i=0; i<components; ++i){
value[i]= pixels[(w*y+x)*components+i]/255.f;
}
}
void Pixmap2D::getComponent(int x, int y, int component, uint8 &value) const{
value= pixels[(w*y+x)*components+component];
}
void Pixmap2D::getComponent(int x, int y, int component, float32 &value) const{
value= pixels[(w*y+x)*components+component]/255.f;
}
//vector get
Vec4f Pixmap2D::getPixel4f(int x, int y) const{
Vec4f v(0.f);
for(int i=0; i<components && i<4; ++i){
v.ptr()[i]= pixels[(w*y+x)*components+i]/255.f;
}
return v;
}
Vec3f Pixmap2D::getPixel3f(int x, int y) const{
Vec3f v(0.f);
for(int i=0; i<components && i<3; ++i){
v.ptr()[i]= pixels[(w*y+x)*components+i]/255.f;
}
return v;
}
float Pixmap2D::getPixelf(int x, int y) const{
return pixels[(w*y+x)*components]/255.f;
}
float Pixmap2D::getComponentf(int x, int y, int component) const{
float c;
getComponent(x, y, component, c);
return c;
}
void Pixmap2D::setPixel(int x, int y, const uint8 *value){
for(int i=0; i<components; ++i){
pixels[(w*y+x)*components+i]= value[i];
}
}
void Pixmap2D::setPixel(int x, int y, const float32 *value){
for(int i=0; i<components; ++i){
pixels[(w*y+x)*components+i]= static_cast<uint8>(value[i]*255.f);
}
}
void Pixmap2D::setComponent(int x, int y, int component, uint8 value){
pixels[(w*y+x)*components+component]= value;
}
void Pixmap2D::setComponent(int x, int y, int component, float32 value){
pixels[(w*y+x)*components+component]= static_cast<uint8>(value*255.f);
}
//vector set
void Pixmap2D::setPixel(int x, int y, const Vec3f &p){
for(int i=0; i<components && i<3; ++i){
pixels[(w*y+x)*components+i]= static_cast<uint8>(p.ptr()[i]*255.f);
}
}
void Pixmap2D::setPixel(int x, int y, const Vec4f &p){
for(int i=0; i<components && i<4; ++i){
pixels[(w*y+x)*components+i]= static_cast<uint8>(p.ptr()[i]*255.f);
}
}
void Pixmap2D::setPixel(int x, int y, float p){
pixels[(w*y+x)*components]= static_cast<uint8>(p*255.f);
}
void Pixmap2D::setPixels(const uint8 *value){
for(int i=0; i<w; ++i){
for(int j=0; j<h; ++j){
setPixel(i, j, value);
}
}
}
void Pixmap2D::setPixels(const float32 *value){
for(int i=0; i<w; ++i){
for(int j=0; j<h; ++j){
setPixel(i, j, value);
}
}
}
void Pixmap2D::setComponents(int component, uint8 value){
assert(component<components);
for(int i=0; i<w; ++i){
for(int j=0; j<h; ++j){
setComponent(i, j, component, value);
}
}
}
void Pixmap2D::setComponents(int component, float32 value){
assert(component<components);
for(int i=0; i<w; ++i){
for(int j=0; j<h; ++j){
setComponent(i, j, component, value);
}
}
}
float splatDist(Vec2i a, Vec2i b){
return (max(abs(a.x-b.x),abs(a.y- b.y)) + 3.f*a.dist(b))/4.f;
}
void Pixmap2D::splat(const Pixmap2D *leftUp, const Pixmap2D *rightUp, const Pixmap2D *leftDown, const Pixmap2D *rightDown){
Random random;
assert(components==3 || components==4);
if(
!doDimensionsAgree(leftUp) ||
!doDimensionsAgree(rightUp) ||
!doDimensionsAgree(leftDown) ||
!doDimensionsAgree(rightDown))
{
throw runtime_error("Pixmap2D::splat: pixmap dimensions don't agree");
}
for(int i=0; i<w; ++i){
for(int j=0; j<h; ++j){
float avg= (w+h)/2.f;
float distLu= splatDist(Vec2i(i, j), Vec2i(0, 0));
float distRu= splatDist(Vec2i(i, j), Vec2i(w, 0));
float distLd= splatDist(Vec2i(i, j), Vec2i(0, h));
float distRd= splatDist(Vec2i(i, j), Vec2i(w, h));
const float powFactor= 2.0f;
distLu= pow(distLu, powFactor);
distRu= pow(distRu, powFactor);
distLd= pow(distLd, powFactor);
distRd= pow(distRd, powFactor);
avg= pow(avg, powFactor);
float lu= distLu>avg? 0: ((avg-distLu))*random.randRange(0.5f, 1.0f);
float ru= distRu>avg? 0: ((avg-distRu))*random.randRange(0.5f, 1.0f);
float ld= distLd>avg? 0: ((avg-distLd))*random.randRange(0.5f, 1.0f);
float rd= distRd>avg? 0: ((avg-distRd))*random.randRange(0.5f, 1.0f);
float total= lu+ru+ld+rd;
Vec4f pix= (leftUp->getPixel4f(i, j)*lu+
rightUp->getPixel4f(i, j)*ru+
leftDown->getPixel4f(i, j)*ld+
rightDown->getPixel4f(i, j)*rd)*(1.0f/total);
setPixel(i, j, pix);
}
}
}
void Pixmap2D::lerp(float t, const Pixmap2D *pixmap1, const Pixmap2D *pixmap2){
if(
!doDimensionsAgree(pixmap1) ||
!doDimensionsAgree(pixmap2))
{
throw runtime_error("Pixmap2D::lerp: pixmap dimensions don't agree");
}
for(int i=0; i<w; ++i){
for(int j=0; j<h; ++j){
setPixel(i, j, pixmap1->getPixel4f(i, j).lerp(t, pixmap2->getPixel4f(i, j)));
}
}
}
void Pixmap2D::copy(const Pixmap2D *sourcePixmap){
assert(components==sourcePixmap->getComponents());
if(w!=sourcePixmap->getW() || h!=sourcePixmap->getH()){
throw runtime_error("Pixmap2D::copy() dimensions must agree");
}
memcpy(pixels, sourcePixmap->getPixels(), w*h*sourcePixmap->getComponents());
}
void Pixmap2D::subCopy(int x, int y, const Pixmap2D *sourcePixmap){
assert(components==sourcePixmap->getComponents());
if(w<sourcePixmap->getW() && h<sourcePixmap->getH()){
throw runtime_error("Pixmap2D::subCopy(), bad dimensions");
}
uint8 *pixel= new uint8[components];
for(int i=0; i<sourcePixmap->getW(); ++i){
for(int j=0; j<sourcePixmap->getH(); ++j){
sourcePixmap->getPixel(i, j, pixel);
setPixel(i+x, j+y, pixel);
}
}
delete pixel;
}
bool Pixmap2D::doDimensionsAgree(const Pixmap2D *pixmap){
return pixmap->getW() == w && pixmap->getH() == h;
}
// =====================================================
// class Pixmap3D
// =====================================================
Pixmap3D::Pixmap3D(){
w= -1;
h= -1;
d= -1;
components= -1;
}
Pixmap3D::Pixmap3D(int w, int h, int d, int components){
init(w, h, d, components);
}
Pixmap3D::Pixmap3D(int d, int components){
init(d, components);
}
void Pixmap3D::init(int w, int h, int d, int components){
this->w= w;
this->h= h;
this->d= d;
this->components= components;
pixels= new uint8[h*w*d*components];;
}
void Pixmap3D::init(int d, int components){
this->w= -1;
this->h= -1;
this->d= d;
this->components= components;
pixels= NULL;
}
Pixmap3D::~Pixmap3D(){
delete [] pixels;
}
void Pixmap3D::loadSlice(const string &path, int slice){
string extension= path.substr(path.find_last_of('.')+1);
if(extension=="bmp"){
loadSliceBmp(path, slice);
}
else if(extension=="tga"){
loadSliceTga(path, slice);
}
else{
throw runtime_error("Unknown pixmap extension: "+extension);
}
}
void Pixmap3D::loadSliceBmp(const string &path, int slice){
PixmapIoBmp plb;
plb.openRead(path);
//init
w= plb.getW();
h= plb.getH();
if(components==-1){
components= 3;
}
if(pixels==NULL){
pixels= new uint8[w*h*d*components];
}
//data
plb.read(&pixels[slice*w*h*components], components);
}
void Pixmap3D::loadSliceTga(const string &path, int slice){
PixmapIoTga plt;
plt.openRead(path);
//header
int fileComponents= plt.getComponents();
//init
w= plt.getW();
h= plt.getH();
if(components==-1){
components= fileComponents;
}
if(pixels==NULL){
pixels= new uint8[w*h*d*components];
}
//read data
plt.read(&pixels[slice*w*h*components], components);
}
// =====================================================
// class PixmapCube
// =====================================================
void PixmapCube::init(int w, int h, int components){
for(int i=0; i<6; ++i){
faces[i].init(w, h, components);
}
}
//load & save
void PixmapCube::loadFace(const string &path, int face){
faces[face].load(path);
}
void PixmapCube::loadFaceBmp(const string &path, int face){
faces[face].loadBmp(path);
}
void PixmapCube::loadFaceTga(const string &path, int face){
faces[face].loadTga(path);
}
}}//end namespace
|