00001 #ifndef Impala_Core_ImageSet_ImageSet_h
00002 #define Impala_Core_ImageSet_ImageSet_h
00003
00004 #include "Persistency/ImageSetsRepository.h"
00005 #include "Persistency/ImageRepository.h"
00006 #include "Persistency/ImageArchiveRepository.h"
00007 #include "Basis/Timer.h"
00008 #include "Util/Cache.h"
00009 #include "Core/Array/Arrays.h"
00010 #include "Core/Array/ReadFile.h"
00011 #include "Core/Array/MakeFromData.h"
00012 #include "Core/Array/ImageArchiveFile.h"
00013 #include "Core/Database/RawDataSet.h"
00014
00015 namespace Impala
00016 {
00017 namespace Core
00018 {
00019 namespace ImageSet
00020 {
00021
00022
00023 class ImageSet : public Database::RawDataSet
00024 {
00025 public:
00026
00027 typedef Persistency::ImageSetsRepository ImageSetsRepository;
00028
00029 ImageSet(String setName) :
00030 Database::RawDataSet(setName),
00031 mSetId(-1)
00032 {
00033 CmdOptions& options = CmdOptions::GetInstance();
00034 mUseArchive = false;
00035 mUseFileArchive = false;
00036 mUseSplitArchive = false;
00037 int cacheSize = options.GetInt("imCacheSize", 128);
00038 mArchives.SetSize(cacheSize);
00039 mQuidClass = QUID_CLASS_IMAGE;
00040 ImageSetsRepository::GetInstance().Register(this, GetSetName());
00041 }
00042
00043 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00044 ImageSet(Util::Database* db, String setName, bool readSet) :
00045 Database::RawDataSet
00046 (db, setName,
00047 readSet ? db->GetFilePath("ImageData", setName, false, false) : ""),
00048 mSetId(-1)
00049 {
00050 CmdOptions& options = CmdOptions::GetInstance();
00051 mUseArchive = false;
00052 mUseFileArchive = false;
00053 mUseSplitArchive = false;
00054 int cacheSize = options.GetInt("imCacheSize", 128);
00055 mArchives.SetSize(cacheSize);
00056 mQuidClass = QUID_CLASS_IMAGE;
00057 ImageSetsRepository::GetInstance().Register(this, GetSetName());
00058 }
00059 #endif // REPOSITORY_USED
00060
00061 int
00062 GetSetId()
00063 {
00064 if(mSetId == -1)
00065 mSetId = ImageSetsRepository::GetInstance().GetId(GetSetName());
00066 return mSetId;
00067 }
00068
00069 Persistency::ImageLocator
00070 GetImageLocator(int fileId)
00071 {
00072 return Persistency::ImageLocator
00073 (GetLocator(), GetContainerDirOfFile(fileId), GetFile(fileId));
00074 }
00075
00076 Quid
00077 GetQuidImage(int fileId)
00078 {
00079 return MakeQuidImage(GetSetId(), fileId);
00080 }
00081
00082 virtual String
00083 QuidToString(Quid quid, bool elaborate)
00084 {
00085 QuidObj qObj(quid);
00086 if (!elaborate)
00087 return qObj.ToString();
00088 String setName = "unknown";
00089 if ((qObj.Set() != 0) && (qObj.Set() == GetSetId()))
00090 setName = GetSetName();
00091 return "Q(" + QuidClassToString(qObj.Class()) + "," + setName + "," +
00092 GetFile(qObj.Id()) + ")";
00093 }
00094
00095 void
00096 SetImageSrc(bool useArchive, bool useFileArchive, bool useSplitArchive)
00097 {
00098 mUseArchive = useArchive;
00099 mUseFileArchive = useFileArchive;
00100 mUseSplitArchive = useSplitArchive;
00101 if (mUseFileArchive)
00102 mUseArchive = true;
00103 if (mUseSplitArchive)
00104 {
00105 mUseFileArchive = true;
00106 mUseArchive = true;
00107 }
00108
00109 if (mUseArchive)
00110 {
00111 String prefix("");
00112 if (mUseSplitArchive)
00113 prefix = "split ";
00114 else if (mUseFileArchive)
00115 prefix = "file ";
00116 ILOG_INFO("Reading " << prefix << "archive");
00117 Timer timer(1);
00118 if (mUseSplitArchive)
00119 {
00120 for (int i=0 ; i<NrDirs() ; i++)
00121 {
00122 if (GetArchive(i) == 0)
00123 mUseArchive = false;
00124 }
00125 return;
00126 }
00127 else
00128 {
00129 if (GetArchive(0) == 0)
00130 mUseArchive = false;
00131 }
00132 ILOG_INFO("time: " << timer.SplitTime());
00133 }
00134 }
00135
00136 bool
00137 GetUseArchive()
00138 {
00139 return mUseArchive;
00140 }
00141
00145 Array::Array2dVec3UInt8*
00146 GetImage(int fileId)
00147 {
00148 Array::Array2dVec3UInt8* im = 0;
00149 if (mUseArchive)
00150 {
00151 int dirId;
00152 int imIdx;
00153 if (mUseSplitArchive)
00154 {
00155 dirId = GetDirIdOfFile(fileId);
00156 imIdx = fileId - GetFirstFileId(dirId);
00157 }
00158 else
00159 {
00160 dirId = 0;
00161 imIdx = fileId;
00162 }
00163 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00164 Array::ImageArchiveFile* ar = GetArchive(dirId);
00165 #else // REPOSITORY_USED
00166 Array::ImageArchive* ar = GetArchive(dirId);
00167 #endif // REPOSITORY_USED
00168 if (ar)
00169 im = ar->ReadImage(imIdx);
00170 }
00171 else
00172 {
00173 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00174 String path = GetFilePathImageData(fileId, false, false);
00175 if (!path.empty())
00176 ReadFile(im, path, GetDatabase());
00177 #else // REPOSITORY_USED
00178 Persistency::ImageLocator loc = GetImageLocator(fileId);
00179 im = Persistency::ImageRepository().Get(loc);
00180 #endif // REPOSITORY_USED
00181 }
00182 return im;
00183 }
00184
00187 Array::Array2dScalarUInt8*
00188 GetImageData(int fileId)
00189 {
00190 UInt8* dataPtr=0;
00191 size_t dataSize=0;
00192 if (mUseArchive)
00193 {
00194 int dirId;
00195 int imIdx;
00196 if (mUseSplitArchive)
00197 {
00198 dirId = GetDirIdOfFile(fileId);
00199 imIdx = fileId - GetFirstFileId(dirId);
00200 }
00201 else
00202 {
00203 dirId = 0;
00204 imIdx = fileId;
00205 }
00206 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00207 Array::ImageArchiveFile* ar = GetArchive(dirId);
00208 #else // REPOSITORY_USED
00209 Array::ImageArchive* ar = GetArchive(dirId);
00210 #endif // REPOSITORY_USED
00211 if (ar)
00212 dataPtr = ar->GetImageData(imIdx, dataSize);
00213 }
00214 else
00215 {
00216 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00217 String path = GetFilePathImageData(fileId, false, false);
00218 Util::Database* db = GetDatabase();
00219 Util::IOBuffer* ioBuf = db->GetIOBuffer(path, true, false, "", 0, true);
00220 if (!ioBuf)
00221 {
00222 ILOG_ERROR("Unable to open file " << path);
00223 return 0;
00224 }
00225 #else // REPOSITORY_USED
00226 Persistency::ImageLocator loc = GetImageLocator(fileId);
00227 Util::IOBuffer* ioBuf =
00228 Persistency::ImageRepositoryInFile().GetReadBuffer(loc);
00229 #endif // REPOSITORY_USED
00230 dataSize = ioBuf->Size();
00231 dataPtr = new UInt8[dataSize];
00232 Int64 nrRead = ioBuf->Read(dataPtr, dataSize);
00233 delete ioBuf;
00234 if(nrRead != dataSize)
00235 ILOG_ERROR("not all data read from ioBuf");
00236 }
00237 Array::Array2dScalarUInt8* im =
00238 Array::MakeFromData<Array::Array2dScalarUInt8>(dataPtr, dataSize, 1);
00239 delete dataPtr;
00240 return im;
00241 }
00242
00243 int
00244 Diff(ImageSet* arg)
00245 {
00246 if (NrFiles() != arg->NrFiles())
00247 {
00248 ILOG_ERROR("Diff: nrFiles differs: " << NrFiles() << " vs " <<
00249 arg->NrFiles());
00250 return 1;
00251 }
00252 int nDiff = 0;
00253 for (int i=0 ; i<NrFiles() ; i++)
00254 {
00255 if (GetAsPath(i) != arg->GetAsPath(i))
00256 {
00257 ILOG_DEBUG("File " << i << " differs " << GetAsPath(i) <<
00258 " vs " << arg->GetAsPath(i));
00259 nDiff++;
00260 }
00261 else if (GetQuidImage(i) != arg->GetQuidImage(i))
00262 {
00263 ILOG_DEBUG("Quid " << i << " differs " << GetQuidImage(i)
00264 << " vs " << arg->GetQuidImage(i));
00265 nDiff++;
00266 }
00267 }
00268 if (nDiff > 0)
00269 ILOG_ERROR("Found " << nDiff << " differences");
00270 return nDiff;
00271 }
00272
00273 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00274
00275
00280 String
00281 MakeDirId(String base, String subDir, int fileId, bool makeDbFile)
00282 {
00283 String dbFile = (makeDbFile) ? GetFile(fileId) : ".";
00284 return mDb->MakeDir(base, subDir, GetSectionOfFile(fileId),
00285 GetDirOfFile(fileId), dbFile);
00286 }
00287
00293 String
00294 GetFilePathId(String base, String subDir, int fileId,
00295 bool useDbFileDir, bool useDbFile, String suffix,
00296 bool toWrite)
00297 {
00298 String dbFileDir = (useDbFileDir) ? GetFile(fileId) : "";
00299 String fName = (useDbFile) ? GetFileBase(fileId) : "";
00300 return mDb->GetFilePath(base, subDir, GetSectionOfFile(fileId),
00301 GetDirOfFile(fileId), dbFileDir,
00302 fName + suffix, toWrite, false);
00303 }
00304
00305
00306
00307 void
00308 SaveImageSet()
00309 {
00310 mDb->MakeDir("ImageData", "");
00311 Save(mDb->GetFilePath("ImageData", mSetName, true, false));
00312 }
00313
00314 void
00315 UpdateImageSet()
00316 {
00317 mDb->MakeDir("ImageData", "");
00318 String fName = mDb->GetFilePath("ImageData", mSetName, true, true);
00319 if (fName.empty())
00320 fName = mDb->GetFilePath("ImageData", mSetName, false, false);
00321 Save(fName);
00322 }
00323 #endif // REPOSITORY_USED
00324
00325 private:
00326
00327 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00328 Array::ImageArchiveFile*
00329 GetArchive(int dirId)
00330 {
00331 Array::ImageArchiveFile* res = 0;
00332 if (mArchives.GetElement(dirId, res))
00333 return res;
00334
00335 if (mUseSplitArchive)
00336 {
00337 int fileId = GetFirstFileId(dirId);
00338 String fName = GetFilePathImageArchive(fileId, "images.raw", false,
00339 false);
00340 if (fName.empty())
00341 return 0;
00342 res = new Array::ImageArchiveFile(fName, !mUseFileArchive,
00343 GetDatabase());
00344 mArchives.AddElement(dirId, res);
00345 return res;
00346 }
00347 String fName = GetFilePathImageArchive(GetSetNameBase() + ".raw", false,
00348 false);
00349 if (fName.empty())
00350 return 0;
00351 res = new Array::ImageArchiveFile(fName, !mUseFileArchive, GetDatabase());
00352 mArchives.AddElement(0, res);
00353 return res;
00354 }
00355 #else // REPOSITORY_USED
00356 Array::ImageArchive*
00357 GetArchive(int dirId)
00358 {
00359 Array::ImageArchive* res = 0;
00360 if (mArchives.GetElement(dirId, res))
00361 return res;
00362
00363 if (mUseSplitArchive)
00364 {
00365 String container = GetContainerDir(dirId);
00366 Persistency::ImageArchiveLocator loc(GetLocator(), false,
00367 container, "images.raw", 0);
00368 res = Persistency::ImageArchiveRepository().Get(loc);
00369 mArchives.AddElement(dirId, res);
00370 return res;
00371 }
00372 String arName = GetSetNameBase() + ".raw";
00373 Persistency::ImageArchiveLocator loc(GetLocator(), false, "", arName, 0);
00374 res = Persistency::ImageArchiveRepository().Get(loc);
00375 mArchives.AddElement(0, res);
00376 return res;
00377 }
00378 #endif // REPOSITORY_USED
00379
00380 int mSetId;
00381 bool mUseArchive;
00382 bool mUseFileArchive;
00383 bool mUseSplitArchive;
00384 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00385 Util::Cache<int, Array::ImageArchiveFile*> mArchives;
00386 #else // REPOSITORY_USED
00387 Util::Cache<int, Array::ImageArchive*> mArchives;
00388 #endif // REPOSITORY_USED
00389
00390 ILOG_VAR_DEC;
00391 };
00392
00393 ILOG_VAR_INIT(ImageSet, Impala.Core.ImageSet);
00394
00395 }
00396 }
00397 }
00398
00399 #endif