00001 #ifndef Impala_Core_ImageSet_FixSizes_h
00002 #define Impala_Core_ImageSet_FixSizes_h
00003
00004 #include "Core/ImageSet/Reporter.h"
00005 #include "Core/ImageSet/MakeImageSet.h"
00006 #include "Core/Array/MakeFromData.h"
00007 #include "Core/Array/ArrayListDelete.h"
00008 #include "Core/Array/WriteRaw.h"
00009 #include "Core/Array/Scale.h"
00010 #include "Core/Array/ImageArchiveMemory.h"
00011
00012 namespace Impala
00013 {
00014 namespace Core
00015 {
00016 namespace ImageSet
00017 {
00018
00019
00020 class FixSizes : public Listener
00021 {
00022 public:
00023
00024 FixSizes(Reporter* reporter, CmdOptions& options)
00025 {
00026 mReporter = reporter;
00027 mReporter->SetReportArray(false);
00028 mMaxWidth = 1000;
00029 mMaxHeight = 1000;
00030 if(options.GetNrArg() > 3)
00031 {
00032 mMaxWidth = atol(options.GetArg(2));
00033 mMaxHeight = atol(options.GetArg(3));
00034 }
00035 ILOG_INFO(" resizing images to "<< mMaxWidth <<" by "<< mMaxHeight);
00036 mSplit = options.GetNrArg() > 4;
00037 ILOG_INFO((mSplit?"":"not ") << "using split archives");
00038
00039 mFileNameAddition = options.GetString("addToName");
00040 if(mFileNameAddition != "")
00041 ILOG_INFO(mFileNameAddition << " will be added to filenames");
00042 }
00043
00044 virtual
00045 ~FixSizes()
00046 {
00047 Array::ArrayListDelete(&mImList);
00048 }
00049
00050 void
00051 AddOriginal(ImageSet* is, int fileId)
00052 {
00053 Array::Array2dScalarUInt8* im = is->GetImageData(fileId);
00054 mImList.push_back(im);
00055
00056 }
00057
00058 void
00059 AddScaled(int fileId, Array::Array2dVec3UInt8* im)
00060 {
00061
00062 double scale = 1.;
00063 while(scale*im->CW() > mMaxWidth || scale*im->CH() > mMaxHeight)
00064 scale *= 0.5;
00065 Array::Array2dVec3UInt8* scaled = 0;
00066 Array::Scale(scaled, im, scale, scale, Geometry::NEAREST, true);
00067
00068 size_t bufSize = mMaxHeight*mMaxWidth;
00069 char* buf = new char[bufSize];
00070 size_t jpgSize=0;
00071 WriteJpgToMemory(scaled, buf, bufSize, &jpgSize);
00072 Array::Array2dScalarUInt8* jpg =
00073 Array::MakeFromData<Array::Array2dScalarUInt8>((UInt8*)buf, jpgSize, 1);
00074 delete buf;
00075
00076 mImList.push_back(jpg);
00077 ILOG_DEBUG("fixed im "<< fileId <<" to "<< scaled->CW() <<" by "<<
00078 scaled->CH());
00079 delete scaled;
00080 }
00081
00082 virtual void
00083 HandleNewFile(ImageSet* is, int fileId, Array::Array2dVec3UInt8* im)
00084 {
00085
00086 if(im->CW() <= mMaxWidth && im->CH() <= mMaxHeight)
00087 AddOriginal(is, fileId);
00088 else
00089 AddScaled(fileId, im);
00090 }
00091
00092 virtual void
00093 HandleDoneDir(ImageSet* is, int dirId)
00094 {
00095 if (!mSplit)
00096 return;
00097 String arName = "images" + mFileNameAddition + ".raw";
00098 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00099 int fileId = is->GetFirstFileId(dirId);
00100 String fName = is->GetFilePathImageArchive(fileId, arName, true, false);
00101 ILOG_INFO("writing archive [" << fName << "]");
00102 Array::WriteRawListVar(mImList, fName, is->GetDatabase(), true, true);
00103 #else // REPOSITORY_USED
00104 String container = is->GetContainerDir(dirId);
00105 Persistency::ImageArchiveLocator loc(is->GetLocator(), false,
00106 container, arName, 0);
00107 Array::ImageArchiveMemory wrapper(&mImList);
00108 Persistency::ImageArchiveRepository().Add(loc, &wrapper);
00109 #endif // REPOSITORY_USED
00110 Array::ArrayListDelete(&mImList);
00111 }
00112
00113 virtual void
00114 HandleDoneWalk(ImageSet* is)
00115 {
00116 if (mSplit)
00117 return;
00118
00119 String arName = is->GetSetNameBase() + mFileNameAddition + ".raw";
00120 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00121 String fName = is->GetFilePathImageArchive(arName, true, false);
00122 ILOG_INFO("writing archive [" << fName << "]");
00123 Array::WriteRawListVar(mImList, fName, is->GetDatabase(), true, true);
00124 #else // REPOSITORY_USED
00125 Persistency::ImageArchiveLocator loc(is->GetLocator(), false,
00126 "", arName, 0);
00127 Array::ImageArchiveMemory wrapper(&mImList);
00128 Persistency::ImageArchiveRepository().Add(loc, &wrapper);
00129 #endif // REPOSITORY_USED
00130 }
00131
00132 private:
00133
00134 String mFileNameAddition;
00135 int mMaxWidth;
00136 int mMaxHeight;
00137 bool mSplit;
00138 Reporter* mReporter;
00139 std::vector<Array::Array2dScalarUInt8*> mImList;
00140 ILOG_VAR_DEC;
00141 };
00142
00143 ILOG_VAR_INIT(FixSizes, Impala.Core.ImageSet);
00144
00145 }
00146 }
00147 }
00148
00149 #endif