From: Markus R. <rol...@us...> - 2005-12-05 21:38:33
|
Update of /cvsroot/simspark/simspark/spark/kerosin/imageserver In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19774/imageserver Added Files: .cvsignore image.cpp image.h imageserver.cpp imageserver.h imageserver_c.cpp Log Message: --- NEW FILE: .cvsignore --- *.lo .deps .dirstamp .libs --- NEW FILE: imageserver_c.cpp --- #include "imageserver.h" using namespace boost; using namespace kerosin; using namespace zeitgeist; void CLASS(ImageServer)::DefineClass() { DEFINE_BASECLASS(zeitgeist/Leaf); } --- NEW FILE: image.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: image.h,v 1.1 2005/12/05 21:38:22 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef KEROSIN_IMAGE_H #define KEROSIN_IMAGE_H /* Image - A Wrapper for the DevIL Library NOTE: HISTORY: 11.07.01 - MK - Initial version 28.08.01 - MK - Added support for a palette 29.08.01 - MK - Rewrite for DevIL 03.09.01 - MK - OpenGL texture support TODO: - add RGB access - image creation TOFIX: */ #include <IL/il.h> namespace kerosin { class Image { public: // constructor/destructor Image(); virtual ~Image(); // this makes the image active void Bind(); // image information ILuint Width(); // width ILuint Height(); // height ILuint Depth(); // depth (==1 for 2d images, >1 for 3d images) ILuint BitsPP(); // bits per pixel ILuint BytesPP(); // bytes per pixel ILuint Type(); // format of pixels ILuint Format(); // byte format of image ILubyte*Data(); bool HasAlpha(); // does the format have an alpha channel bool Create(int w, int h, int b, void *data = NULL); // the interface functions ... these *have* to be implemented by derived classes //virtual bool Create() = 0; //virtual void SetPixel(int x, int y, long color) const = 0; //virtual long GetPixel(int x, int y) const = 0; /* virtual long MakeCol(int a, int r, int g, int b) const = 0; virtual void GetCol(long col, int& a, int& r, int& g, int& b) const = 0; virtual int GetA(long col) const = 0; virtual int GetR(long col) const = 0; virtual int GetG(long col) const = 0; virtual int GetB(long col) const = 0; // accessors f_inline void SetWidth (int inWidth) { mWidth = inWidth; } f_inline void SetHeight(int inHeight) { mHeight = inHeight; } */ protected: ILuint mId; // the DevIL ID which this image is bound to }; } // namespace kerosin #endif //KEROSIN_IMAGE_H --- NEW FILE: imageserver.cpp --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2004 RoboCup Soccer Server 3D Maintenance Group $Id: imageserver.cpp,v 1.1 2005/12/05 21:38:22 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "imageserver.h" #include "image.h" #include <salt/fileclasses.h> #include <zeitgeist/fileserver/fileserver.h> #include <zeitgeist/logserver/logserver.h> #include <boost/scoped_ptr.hpp> using namespace boost; using namespace kerosin; using namespace salt; using namespace zeitgeist; using namespace std; shared_ptr<FileServer> gFileServer; //------------------------------------------------------------------------------------------------ // FileServer hooks for DevIL //------------------------------------------------------------------------------------------------ ILHANDLE ILAPIENTRY FSOpen(const ILstring inName) { return (ILHANDLE)(gFileServer->Register(inName)); } ILvoid ILAPIENTRY FSClose(ILHANDLE handle) { gFileServer->Close((FileServer::THandle)handle); } ILboolean ILAPIENTRY FSEof(ILHANDLE handle) { shared_ptr<salt::RFile> file = gFileServer->Get((FileServer::THandle)handle); return file->Eof(); } ILint ILAPIENTRY FSGetc(ILHANDLE handle) { shared_ptr<salt::RFile> file = gFileServer->Get((FileServer::THandle)handle); return file->Getc(); } ILint ILAPIENTRY FSRead(void *buffer, ILuint size, ILuint count, ILHANDLE handle) { shared_ptr<salt::RFile> file = gFileServer->Get((FileServer::THandle)handle); return file->Read(buffer, size, count); } ILint ILAPIENTRY FSSeek(ILHANDLE handle, ILint offset, ILint origin) { shared_ptr<salt::RFile> file = gFileServer->Get((FileServer::THandle)handle); return file->Seek(offset, origin); } ILint ILAPIENTRY FSTell(ILHANDLE handle) { shared_ptr<salt::RFile> file = gFileServer->Get((FileServer::THandle)handle); return file->Tell(); } //------------------------------------------------------------------------------------------------ // ImageServer implementation //------------------------------------------------------------------------------------------------ // constructor ImageServer::ImageServer() { // initialize DevIL ilInit(); // and setup the default behavior // (this might come out of a config file at a later point) ilEnable(IL_FILE_OVERWRITE); ilEnable(IL_ORIGIN_SET); ilOriginFunc(IL_ORIGIN_UPPER_LEFT); // register FileServer hooks for DevIL ilSetRead( FSOpen, FSClose, FSEof, FSGetc, FSRead, FSSeek, FSTell); } // // This function loads the file inName. If inType is IL_TYPE_UNKNOWN, // then the library will try to find a handler by the file extension provided. // This behavior is done automatically by the library! // boost::shared_ptr<Image> ImageServer::Load(const string& inName, ILenum inType) { // create a new image boost::shared_ptr<Image> image(new Image()); // make it active with DevIL image->Bind(); // set the file server gFileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file")); // load the image ilLoad(inType, (ILstring)inName.c_str()); // set the file server to 0 again gFileServer.reset(); // check for errors if(HandleErrors() == true) { // release the image and return return boost::shared_ptr<Image>(); } return image; } bool ImageServer::Save(const boost::shared_ptr<Image> &inImage, const string& inName, ILenum inType) { // make the image active inImage->Bind(); // set the file server gFileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file")); // save the image ilSave(inType, (ILstring)inName.c_str()); // set the file server to 0 again gFileServer.reset(); // check for errors if(HandleErrors() == true) { return false; } return true; } // // This routine checks for DevIL errors and logs them. The function returns // 'true' if an error has occured and 'false' if not. // bool ImageServer::HandleErrors() { bool ret = false; ILenum error; // check if we have any errors and log them accordingly while ((error = ilGetError()) != IL_NO_ERROR) { ret = true; string msg; switch(error) { case IL_INVALID_ENUM : msg = "invalid enum"; break; case IL_OUT_OF_MEMORY : msg = "out of memory"; break; case IL_FORMAT_NOT_SUPPORTED : msg = "format not supported"; break; case IL_INTERNAL_ERROR : msg = "internal error"; break; case IL_INVALID_VALUE : msg = "invalid value"; break; case IL_ILLEGAL_OPERATION : msg = "illegal operation"; break; case IL_ILLEGAL_FILE_VALUE : msg = "illegal file value"; break; case IL_INVALID_FILE_HEADER : msg = "invalid file header"; break; case IL_INVALID_PARAM : msg = "invalid param"; break; case IL_COULD_NOT_OPEN_FILE : msg = "could not open file"; break; case IL_INVALID_EXTENSION : msg = "invalid extension"; break; case IL_FILE_ALREADY_EXISTS : msg = "file already exists"; break; case IL_OUT_FORMAT_SAME : msg = "out format same"; break; case IL_STACK_OVERFLOW : msg ="stack overflow"; break; case IL_STACK_UNDERFLOW : msg ="stack underflow"; break; case IL_INVALID_CONVERSION : msg = "invalid conversion"; break; case IL_BAD_DIMENSIONS : msg = "bad dimensions"; break; case IL_FILE_READ_ERROR : //case IL_FILE_WRITE_ERROR : msg = "file read/write error"; break; case IL_LIB_GIF_ERROR : msg = "lib gif error"; break; case IL_LIB_JPEG_ERROR : msg = "lib jpeg error"; break; case IL_LIB_PNG_ERROR : msg = "lib png error"; break; case IL_LIB_TIFF_ERROR : msg = "lib tiff error"; break; case IL_LIB_MNG_ERROR : msg = "lib mng error"; break; default: msg = "unknown IL error"; break; } GetLog()->Error() << "(ImageServer) ERROR: DevIL returned error " << error << " (" << msg << ")\n"; } return ret; } --- NEW FILE: imageserver.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: imageserver.h,v 1.1 2005/12/05 21:38:22 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef KEROSIN_IMAGESERVER_H #define KEROSIN_IMAGESERVER_H #include <IL/il.h> #include <zeitgeist/class.h> namespace kerosin { class Image; /* ImageServer - Global Interface For All Image-Related Functionality What the ImageServer does: - Load/Save images - Create images with different formats - Conversion between formats NOTE: HISTORY: 14.07.01 - MK - Initial version 29.07.01 - MK - Uses classserver 29.08.01 - MK - Doesn't use classserver anymore :( - Switched to DevIL for image loading needs, since the task of supporting all major formats would have been too time consuming - Cleaned up the interface of the imageserver quite a bit 11.10.01 - MK - Made singleton functionality more secure 02.10.02 - MK - Moved to Kerosin TODO: - Image creation - Image conversion - Pixel-level access TOFIX: */ class ImageServer : public zeitgeist::Leaf { public: ImageServer(); // load/save /** interpret the file with the filter associated with inExt */ boost::shared_ptr<Image> Load(const std::string& inName, ILenum inType = IL_TYPE_UNKNOWN); /** interpret the file with the filter associated with inExt */ bool Save(const boost::shared_ptr<Image> &inImage, const std::string& inName, ILenum inType = IL_TYPE_UNKNOWN); private: /** some internal error checking */ bool HandleErrors(); }; DECLARE_CLASS(ImageServer); } // namespace kerosin #endif //KEROSIN_IMAGESERVER_H --- NEW FILE: image.cpp --- #include "image.h" using namespace kerosin; // constructor Image::Image() { // let's create a DevIL ID for this image ilGenImages(1, &mId); } // destructor Image::~Image() { // free the image with DevIL ilDeleteImages(1, &mId); } void Image::Bind() { ilBindImage(mId); } ILuint Image::Width() { Bind(); return ilGetInteger(IL_IMAGE_WIDTH); } ILuint Image::Height() { Bind(); return ilGetInteger(IL_IMAGE_HEIGHT); } ILuint Image::Depth() { Bind(); return ilGetInteger(IL_IMAGE_DEPTH); } ILuint Image::BitsPP() { Bind(); return ilGetInteger(IL_IMAGE_BITS_PER_PIXEL ); } ILuint Image::BytesPP() { Bind(); return ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL ); } ILuint Image::Type() { Bind(); return ilGetInteger(IL_IMAGE_TYPE); } ILuint Image::Format() { Bind(); return ilGetInteger(IL_IMAGE_FORMAT); } ILubyte* Image::Data() { Bind(); return ilGetData(); } bool Image::HasAlpha() { Bind(); ILuint format = Format(); switch(format) { case IL_RGB: case IL_BGR: return false; break; case IL_RGBA: case IL_BGRA: return true; break; default: return false; } } bool Image::Create(int w, int h, int b, void *data) { Bind(); if(b==3) { ilTexImage(w, h, 1, b, IL_RGB, IL_UNSIGNED_BYTE, data); } else { ilTexImage(w, h, 1, b, IL_RGBA, IL_UNSIGNED_BYTE, data); } return true; } |