00001 #ifndef Impala_Core_VideoSet_ExportFrames_h
00002 #define Impala_Core_VideoSet_ExportFrames_h
00003
00004 #include "Persistency/ImageArchiveRepository.h"
00005 #include "Persistency/FrameRepository.h"
00006 #include "Core/Array/ImageArchiveMemory.h"
00007 #include "Core/Array/WritePng.h"
00008 #include "Core/Array/WriteJpg.h"
00009 #include "Core/Array/Scale.h"
00010 #include "Core/VideoSet/Reporter.h"
00011
00012 namespace Impala
00013 {
00014 namespace Core
00015 {
00016 namespace VideoSet
00017 {
00018
00019
00020 class ExportFrames : public Listener
00021 {
00022 public:
00023
00024 ExportFrames(Reporter* reporter, CmdOptions& options)
00025 {
00026 mReporter = reporter;
00027 mSplitArchive = false;
00028 if (options.GetNrArg() > 2)
00029 if (options.GetArg(2) == "split")
00030 mSplitArchive = true;
00031 mJpg = false;
00032 if (options.GetNrArg() > 3)
00033 if (options.GetArg(3) == "jpg")
00034 mJpg = true;
00035 mScale = 1.0;
00036 if (options.GetNrArg() > 4)
00037 mScale = atof(options.GetArg(4));
00038 ILOG_INFO("scale = " << mScale);
00039 if (options.GetNrArg() > 5)
00040 Core::Array::gWriteJpgQuality = atol(options.GetArg(5));
00041 mBuf = 0;
00042 if (mSplitArchive)
00043 {
00044 mReporter->SetReportArray(false);
00045 mBufSize = 10000000;
00046 mBuf = new char[mBufSize];
00047 }
00048 }
00049
00050 virtual
00051 ~ExportFrames()
00052 {
00053 if (mBuf)
00054 delete mBuf;
00055 }
00056
00057 virtual void
00058 HandleNewFrame(VideoSet* vs, int fileId, Stream::RgbDataSrc* src)
00059 {
00060 HandleNewFrame(vs, fileId, src, Geometry::Rectangle(), "");
00061 }
00062
00063 virtual void
00064 HandleNewFrame(VideoSet* vs, int fileId, Stream::RgbDataSrc* src,
00065 Geometry::Rectangle rect, String anno)
00066 {
00067 typedef Array::Array2dVec3UInt8 ArrayT;
00068 ArrayT* im = Array::ArrayCreate<ArrayT>
00069 (src->FrameWidth(), src->FrameHeight(), 0, 0, src->DataPtr(), true);
00070 double scale = mScale;
00071 if (mScale > 1)
00072 {
00073 scale = 1.0;
00074 while (scale * im->CW() > mScale)
00075 scale *= 0.5;
00076 }
00077 if (mSplitArchive)
00078 {
00079 size_t nrBytes = 0;
00080 ArrayT* sc = 0;
00081 Array::Scale(sc, im, scale, scale, Geometry::NEAREST, true);
00082 if (mJpg)
00083 {
00084 Array::WriteJpgToMemory(sc, mBuf, mBufSize, &nrBytes);
00085 }
00086 else
00087 {
00088 Array::WritePngToMemory(sc, mBuf, mBufSize, &nrBytes);
00089 }
00090 delete sc;
00091 Array::Array2dScalarUInt8* data =
00092 Array::MakeFromData<Array::Array2dScalarUInt8>((UInt8*) mBuf,
00093 nrBytes, 1);
00094 mImList.push_back(data);
00095 }
00096 else
00097 {
00098 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00099 String fName = vs->GetFilePathFrames(fileId, src->FrameNr(),
00100 ".png", true, false);
00101 if (fName.empty())
00102 {
00103 ILOG_ERROR("HandleNewFrame : Empty filename");
00104 }
00105 else
00106 {
00107 Array::WritePng(im, fName, vs->GetDatabase());
00108 }
00109 #else // REPOSITORY_USED
00110 String container = vs->GetContainerFile(fileId);
00111 Persistency::FrameLocator loc(vs->GetLocator(), container,
00112 src->FrameNr(), ".png");
00113 Persistency::FrameRepository().Add(loc, im);
00114 #endif // REPOSITORY_USED
00115 }
00116 delete im;
00117 }
00118
00119 virtual void
00120 HandleDoneFile(VideoSet* vs, int fileId, Stream::RgbDataSrc* src)
00121 {
00122 if (!mSplitArchive)
00123 return;
00124
00125 String fName = (mJpg) ? "images_jpg.raw" : "images.raw";
00126 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00127 fName = vs->GetFilePathFrames(fileId, fName, true, false);
00128 Array::WriteRawListVar(mImList, fName, vs->GetDatabase(), true, true);
00129 #else // REPOSITORY_USED
00130 String container = vs->GetContainerFile(fileId);
00131 Persistency::ImageArchiveLocator loc(vs->GetLocator(), true, container,
00132 fName, 0);
00133 Array::ImageArchiveMemory wrapper(&mImList);
00134 Persistency::ImageArchiveRepository().Add(loc, &wrapper);
00135 #endif // REPOSITORY_USED
00136 Array::ArrayListDelete(&mImList);
00137 }
00138
00139 private:
00140
00141 Reporter* mReporter;
00142 bool mSplitArchive;
00143 bool mJpg;
00144 double mScale;
00145 int mBufSize;
00146 char* mBuf;
00147 std::vector<Array::Array2dScalarUInt8*> mImList;
00148
00149 ILOG_VAR_DEC;
00150 };
00151
00152 ILOG_VAR_INIT(ExportFrames, Impala.Core.VideoSet);
00153
00154 }
00155 }
00156 }
00157
00158 #endif