Patch from PolitikerNEU for RGB / RGBA files

This commit is contained in:
Mark Vejvoda
2010-03-26 16:55:17 +00:00
parent 1e519fedbe
commit 26289b70d2
8 changed files with 329 additions and 32 deletions

View File

@@ -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);

View File

@@ -0,0 +1,145 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Marti<74>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 <string>
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

View File

@@ -81,10 +81,16 @@ 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; i<h*w*components; i+=components){
char buffer[4];
//BMP is padded to sizes of 4
const int padSize = (4 - (w*fileComponents)%4)%4; //Yeah, could be faster
//std::cout << "Padsize = " << padSize;
for (int y = 0, i = 0; y < h; ++y) {
for (int x = 0; x < w; ++x, i+=components) {
uint8 r, g, b;
in.read((char*)&b, 1);
in.read((char*)&g, 1);
@@ -109,6 +115,24 @@ Pixmap2D* BMPReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
break;
}
}
if (padSize) {
in.read(buffer,padSize);
}
}
/*for(int i = 0; i < w*h*components; ++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;
}

View File

@@ -77,7 +77,6 @@ Pixmap2D* JPGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
if (buffer[0] != 0xFF || buffer[1] != 0xD8) {
std::cout << "0 = [" << std::hex << (int)buffer[0] << "] 1 = [" << std::hex << (int)buffer[1] << "]" << std::endl;
delete[] buffer;
std::cout << "Returning NULL jpeg" << std::endl;
return NULL;
}
@@ -121,30 +120,32 @@ Pixmap2D* JPGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
/*std::cout << "JPEG FILE Information: " << std::endl;
std::cout << "Image width and height: " << cinfo.image_width <<" pixels and " << cinfo.image_height <<" pixels." << std::endl;
std::cout << "Color components per fixel: " << cinfo.num_components << std::endl;
std::cout << "Color space: " << cinfo.jpeg_color_space << std::endl;*/
const int picComponents = (ret->getComponents() == -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 );

View File

@@ -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();

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -0,0 +1,82 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Marti<74>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