Commit 7c03f272 authored by Jan Möbius's avatar Jan Möbius

Added imagestore to reuse already active textures



git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@11730 383ad7c9-94d9-4d36-a494-682f7c89f535
parent d75c0e9b
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $LastChangedBy$ *
* $Date$ *
* *
\*===========================================================================*/
#include "ImageStorage.hh"
#include <iostream>
#include <OpenFlipper/common/GlobalOptions.hh>
static ImageStore store;
ImageStore& imageStore() {
return store;
}
// ---------------------------------------------------------------------------
ImageStore::ImageStore():
nextId_(1)
{
}
// ---------------------------------------------------------------------------
int ImageStore::addImageFile( QString _fileName ) {
// Clean error string
errorString_ = "No error";
QString loadFilename;
// Construct complete filename
// if it starts with "/" or "." or contains a ":" it is a global filename and we don't touch it
// otherwise we take a relative path from our texture dir
if ( _fileName.startsWith("/") || _fileName.startsWith(".") || _fileName.contains(":") )
loadFilename = _fileName;
else
loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + _fileName;
// Check if it already exists and return the existing one rather than recreating a new one
if ( filenameMap_.count(loadFilename) > 0 ) {
// Get id of the existing image
int existingId = filenameMap_[loadFilename];
// Update refcount
refCount_[existingId] = refCount_[existingId] + 1;
// Return existing ones id
return existingId;
}
QFile file(loadFilename);
if ( !file.exists() ) {
errorString_ = "addedEmptyObject: Cannot load global texture '"+ loadFilename +"'. File not found!" ;
return -1;
}
QImage image;
//QImage cannot handle tga directly
if ( QFileInfo(loadFilename).suffix().toLower() == "tga" ){
QPixmap pic(loadFilename);
image = pic.toImage();
} else {
//load the image
if ( !image.load( loadFilename ) ){
errorString_ = "Unable to load Image from file " + loadFilename;
return -1;
}
}
// Store the mapping from filename to id
filenameMap_[loadFilename]= nextId_;
reverseFilenameMap_[nextId_] = loadFilename;
// Initialize refcount
refCount_[nextId_] = 1;
// Store image
imageMap_[nextId_] = image;
// Increment for next image
nextId_++;
// return the id of the added one
return (nextId_ - 1);
}
// ---------------------------------------------------------------------------
int ImageStore::addImage( QImage _image ) {
// No filename mapping as it does not have a filename
// Initialize refcount
refCount_[nextId_] = 1;
// Store image
imageMap_[nextId_] = _image;
// Increment for next image
nextId_++;
// return the id of the added one
return (nextId_ - 1);
}
// ---------------------------------------------------------------------------
int ImageStore::getImageID(QString _filename) {
// Check if it already exists and return the existing one rather than recreating a new one
if ( filenameMap_.count(_filename) > 0 ) {
return filenameMap_[_filename];
}
// Clean error string
errorString_ = "getImageID failed. No such image :" + _filename;
return -1;
}
// ---------------------------------------------------------------------------
QImage& ImageStore::getImage(int _id, bool* _ok) {
if ( imageMap_.count(_id) > 0 ) {
if ( _ok)
*_ok = true;
return imageMap_[_id];
} else {
if ( _ok)
*_ok = false;
errorString_ = "getImage: Unknown image id : " + QString::number(_id);
return dummy_;
}
}
// ---------------------------------------------------------------------------
void ImageStore::removeImage(int _id) {
// Check if it exists
if ( refCount_.count(_id) > 0 ) {
if ( refCount_[_id] > 1 ) {
refCount_[_id] = refCount_[_id] - 1;
return;
} else {
// Remove from refCount
refCount_.remove(_id);
imageMap_.remove(_id);
// This image is from a file and not directly added
if ( reverseFilenameMap_.count(_id) > 0 ) {
// Get the filename
QString fileName = reverseFilenameMap_[_id];
reverseFilenameMap_.remove(_id);
filenameMap_.remove(fileName);
}
}
}
}
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $LastChangedBy$ *
* $Date$ *
* *
\*===========================================================================*/
#ifndef TEXTUREIMAGESTORAGE_HH
#define TEXTUREIMAGESTORAGE_HH
#include <QMap>
#include <QImage>
// ---------------------------------------------------------------------------
class ImageStore {
public :
ImageStore();
int addImageFile( QString _filename);
int addImage( QImage _image );
int getImageID(QString _filename);
QImage& getImage(int _id, bool* _ok);
QString error() {return errorString_; };
void removeImage(int _id);
private:
QMap<int,QImage> imageMap_;
QMap<QString,int> filenameMap_;
QMap<int,QString> reverseFilenameMap_;
QMap<int,unsigned int> refCount_;
QString errorString_;
QImage dummy_;
int nextId_;
};
ImageStore& imageStore();
#endif //TEXTUREIMAGESTORAGE_HH
This diff is collapsed.
......@@ -83,6 +83,7 @@ class TextureControlPlugin : public QObject, BaseInterface, BackupInterface, Tex
// BaseInterface
void updateView();
void updatedObject(int _identifier, const UpdateType _type);
// TextureInterface
void updateTexture( QString , int);
......@@ -158,9 +159,6 @@ class TextureControlPlugin : public QObject, BaseInterface, BackupInterface, Tex
bool StringToBool(QString _value);
/// Load an Image from disk ( if filename contains leading "/ " its consedered global otherwise in texturedir)
bool getImage( QString _fileName, QImage& _image );
/// Checks for a correct drawmode and changes if necessary
void switchDrawMode( TextureType _type );
......
......@@ -209,17 +209,17 @@ bool TextureData::addMultiTexture( QString _textureName ) {
}
/// Stores the given image in the texture information
bool TextureData::setImage( QString _textureName , QImage& _image ) {
bool TextureData::setImage( QString _textureName , int _id ) {
int textureid = -1;
textureid = getTextureIndex(_textureName);
if ( textureid == -1) {
std::cerr << "setImage: Unknown Texture!" << std::endl;
return true;
return false;
}
textures_[textureid].textureImage = _image;
return false;
textures_[textureid].textureImageId(_id);
return true;
}
//-----------------------------------------------------------------------------------
......
......@@ -47,7 +47,6 @@
#include <QString>
#include <QStringList>
#include <QImage>
#include <ACG/GL/gl.hh>
#include <vector>
#include <map>
......@@ -103,6 +102,7 @@ class Texture {
: parameters(_tex.parameters),
multiTextureList(_tex.multiTextureList),
name_(_tex.name_),
textureImageId_(_tex.textureImageId_),
visibleName_(_tex.visibleName_),
filename_(_tex.filename_),
id_(_tex.id_),
......@@ -114,8 +114,6 @@ class Texture {
type_(_tex.type_),
indexMappingProperty_(_tex.indexMappingProperty_)
{
// copy QImage ( we need a deep copy! )
textureImage = _tex.textureImage.copy(_tex.textureImage.rect());
}
......@@ -156,6 +154,9 @@ class Texture {
void clean() { dirty_ = false; };
void setDirty() { dirty_ = true; };
void textureImageId( int _id) {textureImageId_ = _id;};
int textureImageId() {return textureImageId_; };
void type( TextureType _type ) { type_ = _type; };
TextureType type( ) { return type_; };
......@@ -168,8 +169,6 @@ class Texture {
/// Parameters of the texture
TexParameters parameters;
/// The image used as the texture
QImage textureImage;
/// If this is a multiTexture, the list will contain all textures for this multi Texture node.
QStringList multiTextureList;
......@@ -178,6 +177,9 @@ class Texture {
/// Texture Name
QString name_;
/// The image used as the texture ( Ids are handled by the ImageStore )
int textureImageId_;
/// Name visible in the gui
QString visibleName_;
......@@ -256,7 +258,7 @@ class TextureData : public PerObjectData
bool addMultiTexture( QString _textureName );
/// Stores the given image in the texture information
bool setImage( QString _textureName , QImage& _image );
bool setImage( QString _textureName , int _id );
/*
/// Delete a given texture
......@@ -308,4 +310,4 @@ class TextureData : public PerObjectData
};
#endif //TEXTUREDATA_HH
\ No newline at end of file
#endif //TEXTUREDATA_HH
......@@ -53,6 +53,7 @@
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>
#include "ImageStorage.hh"
texturePropertiesWidget::texturePropertiesWidget(QWidget *parent)
: QDialog(parent)
......@@ -289,7 +290,13 @@ void texturePropertiesWidget::textureChanged(QTreeWidgetItem* _item, int _column
}
// Show the texture Image
imageLabel->setPixmap(QPixmap::fromImage(texture.textureImage));
bool ok = false;
imageLabel->setPixmap(QPixmap::fromImage( imageStore().getImage(texture.textureImageId(),&ok) ));
if ( !ok ) {
std::cerr<< imageStore().error().toStdString();
}
imageLabel->setScaledContents(true);
if ( texture.filename().startsWith("/") )
......@@ -318,7 +325,12 @@ void texturePropertiesWidget::textureChanged(QTreeWidgetItem* _item, int _column
absBox->isChecked(),
scaleBox->isChecked());
image_ = texture.textureImage;
bool ok = false;
image_ = imageStore().getImage(texture.textureImageId(),&ok);
if ( !ok ) {
std::cerr << imageStore().error().toStdString();
}
functionPlot_->setImage( &image_ );
......@@ -409,8 +421,12 @@ void texturePropertiesWidget::slotButtonBoxClicked(QAbstractButton* _button){
}
if ( texture.filename() != currentImage_ ){
// Set the new filename of the image
texture.filename( currentImage_ );
texture.textureImage = QImage( currentImage_ );
// Add it to the imagestore and set the index in the texture description
texture.textureImageId( imageStore().addImageFile(currentImage_) );
changed = true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment