From 26289b70d2cd5c442ee4bd8eb76198fe28c3e9a4 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 26 Mar 2010 16:55:17 +0000 Subject: [PATCH] Patch from PolitikerNEU for RGB / RGBA files --- source/shared_lib/include/graphics/pixmap.h | 2 + source/shared_lib/include/graphics/texture.h | 145 ++++++++++++++++++ .../shared_lib/sources/graphics/BMPReader.cpp | 68 +++++--- .../shared_lib/sources/graphics/JPGReader.cpp | 33 ++-- .../shared_lib/sources/graphics/PNGReader.cpp | 1 + .../shared_lib/sources/graphics/TGAReader.cpp | 16 +- source/shared_lib/sources/graphics/pixmap.cpp | 14 ++ .../shared_lib/sources/graphics/texture.cpp | 82 ++++++++++ 8 files changed, 329 insertions(+), 32 deletions(-) create mode 100644 source/shared_lib/include/graphics/texture.h create mode 100644 source/shared_lib/sources/graphics/texture.cpp diff --git a/source/shared_lib/include/graphics/pixmap.h b/source/shared_lib/include/graphics/pixmap.h index 19703fd46..c325d1204 100644 --- a/source/shared_lib/include/graphics/pixmap.h +++ b/source/shared_lib/include/graphics/pixmap.h @@ -217,6 +217,7 @@ public: Pixmap3D(int d, int components); void init(int w, int h, int d, int components); void init(int d, int components); + void init(int components); ~Pixmap3D(); //load & save @@ -253,6 +254,7 @@ protected: public: //init void init(int w, int h, int components); + void init(int components); //load & save void loadFace(const string &path, int face); diff --git a/source/shared_lib/include/graphics/texture.h b/source/shared_lib/include/graphics/texture.h new file mode 100644 index 000000000..ca449ea27 --- /dev/null +++ b/source/shared_lib/include/graphics/texture.h @@ -0,0 +1,145 @@ +// ============================================================== +// This file is part of Glest Shared Library (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo 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 +// ============================================================== + +#ifndef _SHARED_GRAPHICS_TEXTURE_H_ +#define _SHARED_GRAPHICS_TEXTURE_H_ + +#include "types.h" +#include "pixmap.h" + +#include + +using std::string; +using Shared::Platform::uint8; + +namespace Shared{ namespace Graphics{ + +class TextureParams; + +// ===================================================== +// class Texture +// ===================================================== + +class Texture{ +public: + static const int defaultSize; + static const int defaultComponents; + + enum WrapMode{ + wmRepeat, + wmClamp, + wmClampToEdge + }; + + enum Filter{ + fBilinear, + fTrilinear + }; + + enum Format{ + fAuto, + fAlpha, + fLuminance, + fRgb, + fRgba + }; + +protected: + string path; + bool mipmap; + WrapMode wrapMode; + bool pixmapInit; + Format format; + + bool inited; + +public: + Texture(); + virtual ~Texture(){}; + + bool getMipmap() const {return mipmap;} + WrapMode getWrapMode() const {return wrapMode;} + bool getPixmapInit() const {return pixmapInit;} + Format getFormat() const {return format;} + const string getPath() const {return path;} + + void setMipmap(bool mipmap) {this->mipmap= mipmap;} + void setWrapMode(WrapMode wrapMode) {this->wrapMode= wrapMode;} + void setPixmapInit(bool pixmapInit) {this->pixmapInit= pixmapInit;} + void setFormat(Format format) {this->format= format;} + + virtual void init(Filter filter= fBilinear, int maxAnisotropy= 1)=0; + virtual void end()=0; +}; + +// ===================================================== +// class Texture1D +// ===================================================== + +class Texture1D: public Texture{ +protected: + Pixmap1D pixmap; + +public: + void load(const string &path); + + Pixmap1D *getPixmap() {return &pixmap;} + const Pixmap1D *getPixmap() const {return &pixmap;} +}; + +// ===================================================== +// class Texture2D +// ===================================================== + +class Texture2D: public Texture{ +protected: + Pixmap2D pixmap; + +public: + void load(const string &path); + + Pixmap2D *getPixmap() {return &pixmap;} + const Pixmap2D *getPixmap() const {return &pixmap;} +}; + +// ===================================================== +// class Texture3D +// ===================================================== + +class Texture3D: public Texture{ +protected: + Pixmap3D pixmap; + +public: + void loadSlice(const string &path, int slice); + + Pixmap3D *getPixmap() {return &pixmap;} + const Pixmap3D *getPixmap() const {return &pixmap;} +}; + +// ===================================================== +// class TextureCube +// ===================================================== + +class TextureCube: public Texture{ +protected: + PixmapCube pixmap; + +public: + void loadFace(const string &path, int face); + + PixmapCube *getPixmap() {return &pixmap;} + const PixmapCube *getPixmap() const {return &pixmap;} +}; + +}}//end namespace + +#endif diff --git a/source/shared_lib/sources/graphics/BMPReader.cpp b/source/shared_lib/sources/graphics/BMPReader.cpp index ef5cc89d7..a122e4122 100644 --- a/source/shared_lib/sources/graphics/BMPReader.cpp +++ b/source/shared_lib/sources/graphics/BMPReader.cpp @@ -81,34 +81,58 @@ Pixmap2D* BMPReader::read(ifstream& in, const string& path, Pixmap2D* ret) const int h= infoHeader.height; int w= infoHeader.width; int components= (ret->getComponents() == -1)?3:ret->getComponents(); + const int fileComponents = 3; //std::cout << "BMP-Components: Pic: " << components << " old: " << (ret->getComponents()) << " File: " << 3 << std::endl; ret->init(w,h,components); uint8* pixels = ret->getPixels(); - for(int i=0; igetComponents() == -1)?cinfo.num_components:ret->getComponents(); + int picComponents = (ret->getComponents() == -1)?cinfo.num_components:ret->getComponents(); //std::cout << "JPG-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << cinfo.num_components << std::endl; - ret->init(cinfo.image_width, cinfo.image_height, picComponents); - uint8* pixels = ret->getPixels(); + //picComponents = 4; //TODO: Irrlicht has some special CMYK-handling - maybe needed too? /* Start decompression jpeg here */ jpeg_start_decompress( &cinfo ); - + ret->init(cinfo.image_width, cinfo.image_height, picComponents); + uint8* pixels = ret->getPixels(); + //std::cout << "output width and height: " << cinfo.output_width <<" pixels and " << cinfo.output_height <<" pixels." << std::endl; /* now actually read the jpeg into the raw buffer */ row_pointer[0] = new unsigned char[cinfo.output_width*cinfo.num_components]; size_t location = 0; //Current pixel /* read one scan line at a time */ /* Again you need to invert the lines unfortunately*/ - while( cinfo.output_scanline < cinfo.image_height ) + while( cinfo.output_scanline < cinfo.output_height ) { jpeg_read_scanlines( &cinfo, row_pointer, 1 ); - location = (cinfo.image_height - cinfo.output_scanline)*cinfo.image_width*cinfo.num_components; + location = (cinfo.output_height - cinfo.output_scanline)*cinfo.output_width*picComponents; if (picComponents == cinfo.num_components) { - memcpy(pixels+location,row_pointer[0],cinfo.image_width*cinfo.num_components); + memcpy(pixels+location,row_pointer[0],cinfo.output_width*cinfo.num_components); } else { int r,g,b,a,l; - for (int xPic = 0, xFile = 0; xPic < cinfo.image_width*picComponents; xPic+= picComponents, xFile+= cinfo.num_components) { + for (int xPic = 0, xFile = 0; xPic < cinfo.output_width*picComponents; xPic+= picComponents, xFile+= cinfo.num_components) { switch(cinfo.num_components) { case 3: r = row_pointer[0][xFile]; @@ -188,6 +189,20 @@ Pixmap2D* JPGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const } } } + /*for(int i = 0; i < cinfo.image_width*cinfo.image_height*picComponents; ++i) { + if (i%39 == 0) std::cout << std::endl; + int first = pixels[i]/16; + if (first < 10) + std:: cout << first; + else + std::cout << (char)('A'+(first-10)); + first = pixels[i]%16; + if (first < 10) + std:: cout << first; + else + std::cout << (char)('A'+(first-10)); + std::cout << " "; + }*/ /* wrap up decompression, destroy objects, free pointers and close open files */ jpeg_finish_decompress( &cinfo ); jpeg_destroy_decompress( &cinfo ); diff --git a/source/shared_lib/sources/graphics/PNGReader.cpp b/source/shared_lib/sources/graphics/PNGReader.cpp index b4a17e55b..22323a94f 100644 --- a/source/shared_lib/sources/graphics/PNGReader.cpp +++ b/source/shared_lib/sources/graphics/PNGReader.cpp @@ -109,6 +109,7 @@ Pixmap2D* PNGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const int fileComponents = info_ptr->rowbytes/info_ptr->width; int picComponents = (ret->getComponents()==-1)?fileComponents:ret->getComponents(); //std::cout << "PNG-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << fileComponents << std::endl; + //picComponents = 4; //Copy image ret->init(width,height,picComponents); uint8* pixels = ret->getPixels(); diff --git a/source/shared_lib/sources/graphics/TGAReader.cpp b/source/shared_lib/sources/graphics/TGAReader.cpp index e67ae9b68..00b6adde4 100644 --- a/source/shared_lib/sources/graphics/TGAReader.cpp +++ b/source/shared_lib/sources/graphics/TGAReader.cpp @@ -85,7 +85,7 @@ Pixmap2D* TGAReader::read(ifstream& in, const string& path, Pixmap2D* ret) const const int w = fileHeader.width; const int fileComponents= fileHeader.bitsPerPixel/8; const int picComponents = (ret->getComponents()==-1)?fileComponents:ret->getComponents(); - //std::cout << "BMP-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << fileComponents << std::endl; + //std::cout << "TGA-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << fileComponents << std::endl; ret->init(w,h,picComponents); uint8* pixels = ret->getPixels(); //read file @@ -131,6 +131,20 @@ Pixmap2D* TGAReader::read(ifstream& in, const string& path, Pixmap2D* ret) const break; } } + /*for(int i = 0; i < w*h*picComponents; ++i) { + if (i%39 == 0) std::cout << std::endl; + int first = pixels[i]/16; + if (first < 10) + std:: cout << first; + else + std::cout << (char)('A'+(first-10)); + first = pixels[i]%16; + if (first < 10) + std:: cout << first; + else + std::cout << (char)('A'+(first-10)); + std::cout << " "; + }*/ return ret; } diff --git a/source/shared_lib/sources/graphics/pixmap.cpp b/source/shared_lib/sources/graphics/pixmap.cpp index 0c04f7cb0..462f8dd77 100644 --- a/source/shared_lib/sources/graphics/pixmap.cpp +++ b/source/shared_lib/sources/graphics/pixmap.cpp @@ -754,6 +754,14 @@ void Pixmap3D::init(int d, int components){ pixels= NULL; } +void Pixmap3D::init(int components){ + this->w= -1; + this->h= -1; + this->d= -1; + this->components= components; + pixels= NULL; +} + Pixmap3D::~Pixmap3D(){ delete [] pixels; } @@ -821,6 +829,12 @@ void PixmapCube::init(int w, int h, int components){ } } +void PixmapCube::init(int components){ + for(int i=0; i<6; ++i){ + faces[i].init(components); + } +} + //load & save void PixmapCube::loadFace(const string &path, int face){ faces[face].load(path); diff --git a/source/shared_lib/sources/graphics/texture.cpp b/source/shared_lib/sources/graphics/texture.cpp new file mode 100644 index 000000000..968d8f3e5 --- /dev/null +++ b/source/shared_lib/sources/graphics/texture.cpp @@ -0,0 +1,82 @@ +// ============================================================== +// This file is part of Glest Shared Library (www.glest.org) +// +// Copyright (C) 2001-2008 Martiņo 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 "texture.h" + +#include "leak_dumper.h" + +namespace Shared{ namespace Graphics{ + +// ===================================================== +// class Texture +// ===================================================== + +const int Texture::defaultSize= 256; +const int Texture::defaultComponents = 4; + +Texture::Texture(){ + mipmap= true; + pixmapInit= true; + wrapMode= wmRepeat; + format= fAuto; + + inited= false; +} + +// ===================================================== +// class Texture1D +// ===================================================== + +void Texture1D::load(const string &path){ + this->path= path; + if (pixmap.getComponents() == -1) { //TODO: look where you really need that + pixmap.init(defaultComponents); + } + pixmap.load(path); +} + +// ===================================================== +// class Texture2D +// ===================================================== + +void Texture2D::load(const string &path){ + this->path= path; + if (pixmap.getComponents() == -1) { + pixmap.init(defaultComponents); + } + pixmap.load(path); +} + +// ===================================================== +// class Texture3D +// ===================================================== + +void Texture3D::loadSlice(const string &path, int slice){ + this->path= path; + if (pixmap.getComponents() == -1) { + pixmap.init(defaultComponents); + } + pixmap.loadSlice(path, slice); +} + +// ===================================================== +// class TextureCube +// ===================================================== + +void TextureCube::loadFace(const string &path, int face){ + this->path= path; + if (pixmap.getFace(0)->getComponents() == -1) { + pixmap.init(defaultComponents); + } + pixmap.loadFace(path, face); +} + +}}//end namespace