00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
00092
00093
00094
00095 ImageServer::ImageServer()
00096 {
00097
00098 ilInit();
00099
00100
00101
00102 ilEnable(IL_FILE_OVERWRITE);
00103 ilEnable(IL_ORIGIN_SET);
00104 ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
00105
00106
00107 ilSetRead(
00108 FSOpen,
00109 FSClose,
00110 FSEof,
00111 FSGetc,
00112 FSRead,
00113 FSSeek,
00114 FSTell);
00115 }
00116
00117
00118
00119
00120
00121
00122 boost::shared_ptr<Image> ImageServer::Load(const string& inName, ILenum inType)
00123 {
00124
00125 boost::shared_ptr<Image> image(new Image());
00126
00127
00128 image->Bind();
00129
00130
00131 gFileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file"));
00132
00133
00134 ilLoad(inType, (ILstring)inName.c_str());
00135
00136
00137 gFileServer.reset();
00138
00139
00140 if(HandleErrors() == true)
00141 {
00142
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
00152 inImage->Bind();
00153
00154
00155 gFileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file"));
00156
00157
00158 ilSave(inType, (ILstring)inName.c_str());
00159
00160
00161 gFileServer.reset();
00162
00163
00164 if(HandleErrors() == true)
00165 {
00166 return false;
00167 }
00168
00169 return true;
00170 }
00171
00172
00173
00174
00175
00176 bool ImageServer::HandleErrors()
00177 {
00178 bool ret = false;
00179 ILenum error;
00180
00181
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
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 }