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