Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

imageserver.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2004 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: imageserver.cpp,v 1.7 2004/04/18 16:24:21 rollmark Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 #include "imageserver.h"
00023 #include "image.h"
00024 #include <salt/fileclasses.h>
00025 #include <zeitgeist/fileserver/fileserver.h>
00026 #include <zeitgeist/logserver/logserver.h>
00027 #include <boost/scoped_ptr.hpp>
00028 
00029 using namespace boost;
00030 using namespace kerosin;
00031 using namespace salt;
00032 using namespace zeitgeist;
00033 using namespace std;
00034 
00035 shared_ptr<FileServer> gFileServer;
00036 
00037 //------------------------------------------------------------------------------------------------
00038 // FileServer hooks for DevIL
00039 //------------------------------------------------------------------------------------------------
00040 ILHANDLE ILAPIENTRY FSOpen(const ILstring inName)
00041 {
00042     return (ILHANDLE)(gFileServer->Register(inName));
00043 }
00044 
00045 ILvoid ILAPIENTRY FSClose(ILHANDLE handle)
00046 {
00047     gFileServer->Close((FileServer::THandle)handle);
00048 }
00049 
00050 ILboolean ILAPIENTRY FSEof(ILHANDLE handle)
00051 {
00052     shared_ptr<salt::RFile> file =
00053         gFileServer->Get((FileServer::THandle)handle);
00054 
00055     return file->Eof();
00056 }
00057 
00058 ILint ILAPIENTRY FSGetc(ILHANDLE handle)
00059 {
00060     shared_ptr<salt::RFile> file =
00061         gFileServer->Get((FileServer::THandle)handle);
00062 
00063     return file->Getc();
00064 }
00065 
00066 ILint ILAPIENTRY FSRead(void *buffer, ILuint size, ILuint count, ILHANDLE handle)
00067 {
00068     shared_ptr<salt::RFile> file =
00069         gFileServer->Get((FileServer::THandle)handle);
00070 
00071     return file->Read(buffer, size, count);
00072 }
00073 
00074 ILint ILAPIENTRY FSSeek(ILHANDLE handle, ILint offset, ILint origin)
00075 {
00076     shared_ptr<salt::RFile> file =
00077         gFileServer->Get((FileServer::THandle)handle);
00078 
00079     return file->Seek(offset, origin);
00080 }
00081 
00082 ILint ILAPIENTRY FSTell(ILHANDLE handle)
00083 {
00084     shared_ptr<salt::RFile> file =
00085         gFileServer->Get((FileServer::THandle)handle);
00086 
00087     return file->Tell();
00088 }
00089 
00090 //------------------------------------------------------------------------------------------------
00091 // ImageServer implementation
00092 //------------------------------------------------------------------------------------------------
00093 
00094 // constructor
00095 ImageServer::ImageServer()
00096 {
00097     // initialize DevIL
00098     ilInit();
00099 
00100     // and setup the default behavior
00101     // (this might come out of a config file at a later point)
00102     ilEnable(IL_FILE_OVERWRITE);
00103     ilEnable(IL_ORIGIN_SET);
00104     ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
00105 
00106     // register FileServer hooks for DevIL
00107     ilSetRead(
00108               FSOpen,
00109               FSClose,
00110               FSEof,
00111               FSGetc,
00112               FSRead,
00113               FSSeek,
00114               FSTell);
00115 }
00116 
00117 //
00118 // This function loads the file inName. If inType is IL_TYPE_UNKNOWN,
00119 // then the library will try to find a handler by the file extension provided.
00120 // This behavior is done automatically by the library!
00121 //
00122 boost::shared_ptr<Image> ImageServer::Load(const string& inName, ILenum inType)
00123 {
00124     // create a new image
00125     boost::shared_ptr<Image> image(new Image());
00126 
00127     // make it active with DevIL
00128     image->Bind();
00129 
00130     // set the file server
00131     gFileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file"));
00132 
00133     // load the image
00134     ilLoad(inType, (ILstring)inName.c_str());
00135 
00136     // set the file server to 0 again
00137     gFileServer.reset();
00138 
00139     // check for errors
00140     if(HandleErrors() == true)
00141         {
00142             // release the image and return
00143             return boost::shared_ptr<Image>();
00144         }
00145 
00146     return image;
00147 }
00148 
00149 bool ImageServer::Save(const boost::shared_ptr<Image> &inImage, const string& inName, ILenum inType)
00150 {
00151     // make the image active
00152     inImage->Bind();
00153 
00154     // set the file server
00155     gFileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file"));
00156 
00157     // save the image
00158     ilSave(inType, (ILstring)inName.c_str());
00159 
00160     // set the file server to 0 again
00161     gFileServer.reset();
00162 
00163     // check for errors
00164     if(HandleErrors() == true)
00165         {
00166             return false;
00167         }
00168 
00169     return true;
00170 }
00171 
00172 //
00173 //      This routine checks for DevIL errors and logs them. The function returns
00174 //      'true' if an error has occured and 'false' if not.
00175 //
00176 bool ImageServer::HandleErrors()
00177 {
00178     bool ret = false;
00179     ILenum error;
00180 
00181     // check if we have any errors and log them accordingly
00182     while ((error = ilGetError()) != IL_NO_ERROR)
00183         {
00184             ret = true;
00185 
00186             string msg;
00187 
00188             switch(error)
00189                 {
00190                 case IL_INVALID_ENUM :
00191                     msg = "invalid enum";
00192                     break;
00193 
00194                 case IL_OUT_OF_MEMORY :
00195                     msg = "out of memory";
00196                     break;
00197 
00198                 case IL_FORMAT_NOT_SUPPORTED :
00199                     msg = "format not supported";
00200                     break;
00201 
00202                 case IL_INTERNAL_ERROR :
00203                     msg = "internal error";
00204                     break;
00205 
00206                 case IL_INVALID_VALUE :
00207                     msg = "invalid value";
00208                     break;
00209 
00210                 case IL_ILLEGAL_OPERATION :
00211                     msg = "illegal operation";
00212                     break;
00213 
00214                 case IL_ILLEGAL_FILE_VALUE :
00215                     msg  = "illegal file value";
00216                     break;
00217 
00218                 case IL_INVALID_FILE_HEADER :
00219                     msg = "invalid file header";
00220                     break;
00221 
00222                 case IL_INVALID_PARAM :
00223                     msg  = "invalid param";
00224                     break;
00225 
00226                 case IL_COULD_NOT_OPEN_FILE :
00227                     msg = "could not open file";
00228                     break;
00229 
00230                 case IL_INVALID_EXTENSION :
00231                     msg = "invalid extension";
00232                     break;
00233 
00234                 case IL_FILE_ALREADY_EXISTS :
00235                     msg = "file already exists";
00236                     break;
00237 
00238                 case IL_OUT_FORMAT_SAME :
00239                     msg = "out format same";
00240                     break;
00241 
00242                 case IL_STACK_OVERFLOW :
00243                     msg  ="stack overflow";
00244                     break;
00245 
00246                 case IL_STACK_UNDERFLOW :
00247                     msg  ="stack underflow";
00248                     break;
00249 
00250                 case IL_INVALID_CONVERSION :
00251                     msg = "invalid conversion";
00252                     break;
00253 
00254                 case IL_BAD_DIMENSIONS :
00255                     msg = "bad dimensions";
00256                     break;
00257 
00258                 case IL_FILE_READ_ERROR :
00259                     //case IL_FILE_WRITE_ERROR :
00260                     msg = "file read/write error";
00261                     break;
00262 
00263                 case IL_LIB_GIF_ERROR :
00264                     msg = "lib gif error";
00265                     break;
00266 
00267                 case IL_LIB_JPEG_ERROR :
00268                     msg = "lib jpeg error";
00269                     break;
00270 
00271                 case IL_LIB_PNG_ERROR :
00272                     msg = "lib png error";
00273                     break;
00274 
00275                 case IL_LIB_TIFF_ERROR :
00276                     msg = "lib tiff error";
00277                     break;
00278 
00279                 case IL_LIB_MNG_ERROR :
00280                     msg = "lib mng error";
00281                     break;
00282 
00283                 default:
00284                     msg = "unknown IL error";
00285                     break;
00286                 }
00287 
00288             GetLog()->Error() << "(ImageServer) ERROR: DevIL returned error "
00289                               << error << " (" << msg << ")\n";
00290         }
00291 
00292     return ret;
00293 }

Generated on Thu Apr 6 15:25:38 2006 for rcssserver3d by  doxygen 1.4.4