00001 #ifndef MediaTable_TableDataSourceImageDir_h
00002 #define MediaTable_TableDataSourceImageDir_h
00003
00004 #include "TableDataSource.h"
00005 #include "Core/Array/ReadFile.h"
00006 #include "Core/Stream/RgbDataSrcRaw.h"
00007 #include "Util/DirImageFileNames.h"
00008
00009 namespace Impala {
00010 namespace Application {
00011 namespace MediaTable {
00012
00013 class TableDataSourceImageDir : public TableDataSource
00014 {
00015 public:
00016 typedef Impala::Core::Stream::RgbDataSrcRaw RgbDataSrcRaw;
00017
00018 TableDataSourceImageDir(CString directory):
00019 TableDataSource()
00020 {
00021 Init();
00022 OpenDir(directory);
00023
00024
00025 }
00026
00027 ~TableDataSourceImageDir()
00028 {
00029 ClearFiles();
00030 }
00031
00032 int GetTotalRows() {
00033 return mTotalRows;
00034 }
00035
00036 void OpenDir(CString dirName)
00037 {
00038 ClearFiles();
00039
00040 mDirName = dirName;
00041 String ext = FileNameExt(dirName,true);
00042
00043 if (ext == "raw")
00044 mRgbDataSrcRaw = new RgbDataSrcRaw(dirName);
00045 else
00046 Util::DirImageFileNames(dirName, mFileNames);
00047
00048 if (mRgbDataSrcRaw)
00049 mTotalRows = mRgbDataSrcRaw->LastFrame();
00050 else
00051 mTotalRows = mFileNames.size();
00052 }
00053
00054 void ClearFiles()
00055 {
00056 if (mRgbDataSrcRaw)
00057 {
00058 delete mRgbDataSrcRaw;
00059 mRgbDataSrcRaw = 0;
00060 }
00061 for (unsigned int i=0 ; i<mFileNames.size() ; i++)
00062 delete mFileNames[i];
00063 mFileNames.clear();
00064 }
00065
00066
00067 virtual String
00068 GetTextDataByID(String column, int row)
00069 {
00070 if (column == "filename")
00071 {
00072 if (!mRgbDataSrcRaw)
00073 return *mFileNames[row];
00074 return String("raw:") + MakeString(row);
00075 }
00076 if (column == "id")
00077 return MakeString(row);
00078 return "N/A";
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 virtual double
00088 GetNormalizedDataByID(String column, int row)
00089 {
00090 double v = row / (double)mTotalRows;
00091 if (column == "red")
00092 return v;
00093 if (column == "green")
00094 return 1.0 - v;
00095 if (column == "blue") {
00096 return (row % 10) / 10.0;
00097 }
00098 return 0.0;
00099 }
00100
00101 virtual Array2dVec3UInt8*
00102 GetImageDataByID(String colum, int row)
00103 {
00104 Array2dVec3UInt8* ar = 0;
00105 if (mRgbDataSrcRaw)
00106 {
00107 mRgbDataSrcRaw->GotoFrame(row);
00108 int imW = mRgbDataSrcRaw->FrameWidth();
00109 int imH = mRgbDataSrcRaw->FrameHeight();
00110 ar = new Array2dVec3UInt8(imW, imH, 0, 0, 0, false);
00111 memcpy((void *)ar->CPB(0, 0), mRgbDataSrcRaw->DataPtr(), imW*imH*3);
00112 }
00113 else
00114 {
00115 String fName = mDirName + "/" + *mFileNames[row];
00116 Core::Array::ReadFile(ar, fName, &Util::Database::GetInstance());
00117 if (ar == 0)
00118 ILOG_WARN("Could not read file: " << fName);
00119 }
00120 return ar;
00121 }
00122
00123 private:
00124
00125 void Init()
00126 {
00127 mRgbDataSrcRaw = 0;
00128
00129 AddStaticColumn("id", TYPE_TEXT, 50);
00130 AddStaticColumn("image", TYPE_IMAGE, 120);
00131 AddStaticColumn("filename", TYPE_TEXT, 300);
00132
00133
00134
00135
00136
00137
00138 }
00139
00140 String mDirName;
00141 RgbDataSrcRaw* mRgbDataSrcRaw;
00142 std::vector<String*> mFileNames;
00143 int mTotalRows;
00144
00145 ILOG_VAR_DEC;
00146 };
00147
00148 ILOG_VAR_INIT(TableDataSourceImageDir, Application.MediaTable);
00149
00150 }
00151 }
00152 }
00153
00154 #endif // TableDataSourceImageDir_h