00001 #ifndef Impala_Core_Array_ImageArchiveMapi_h
00002 #define Impala_Core_Array_ImageArchiveMapi_h
00003
00004 #include "Core/Array/ImageArchive.h"
00005 #include "Link/Monet/Connection.h"
00006 #include "Basis/Quid.h"
00007 #include "Util/BinHex.h"
00008 #include "Core/Array/ReadImage.h"
00009
00010 namespace Impala
00011 {
00012 namespace Core
00013 {
00014 namespace Array
00015 {
00016
00017
00018 class ImageArchiveMapi : public ImageArchive
00019 {
00020 public:
00021
00022 ImageArchiveMapi(Quid vidQuid, Link::Monet::Connection* conn)
00023 {
00024 Init(vidQuid, conn);
00025 }
00026
00027 virtual
00028 ~ImageArchiveMapi()
00029 {
00030 if (mFrameImageId)
00031 delete mFrameImageId;
00032 }
00033
00034 bool
00035 Valid()
00036 {
00037 return mConn->Valid() && (NrImages() != 0);
00038 }
00039
00040 int
00041 NrImages()
00042 {
00043 return mNrImages;
00044 }
00045
00046 Array2dVec3UInt8*
00047 ReadImage(int idx)
00048 {
00049 if (!Valid())
00050 {
00051 ILOG_ERROR("Cannot ReadImage: archive not valid");
00052 return 0;
00053 }
00054
00055 size_t blockSize = 0;
00056 UInt8* data = GetImageData(idx, blockSize);
00057 Array2dVec3UInt8* im = 0;
00058 ReadImageFromMemory(im, (char*) data, blockSize);
00059 delete data;
00060 return im;
00061 }
00062
00063 UInt8*
00064 GetImageData(int idx, size_t& blockSize)
00065 {
00066 if (!Valid())
00067 {
00068 ILOG_ERROR("Cannot GetImageData: archive not valid");
00069 return 0;
00070 }
00071 if ((idx < 0) || (idx >= mNrImages))
00072 {
00073 ILOG_ERROR("GetImageData: idx out of range");
00074 return 0;
00075 }
00076
00077 String query = "select image from frame_image where frame_image_id = "
00078 + MakeString(mFrameImageId[idx]) + ";";
00079
00080 MapiHdl hdl = mConn->QueryPartStart(query);
00081 if (hdl == 0)
00082 return 0;
00083
00084 std::vector<String> res;
00085 if (!mConn->QueryPartFetchString(hdl, 0, res))
00086 return 0;
00087
00088 mConn->QueryPartEnd(hdl);
00089 if (res.size() != 1)
00090 {
00091 ILOG_ERROR("GetImageData: unexpected number of results");
00092 return 0;
00093 }
00094 return Util::Hex2Bin(res[0], blockSize);
00095 }
00096
00097 private:
00098
00099 void
00100 Init(Quid vidQuid, Link::Monet::Connection* conn)
00101 {
00102 mConn = conn;
00103 mNrImages = 0;
00104 mFrameImageId = 0;
00105
00106 if ((conn == 0) || (!conn->Valid()))
00107 {
00108 ILOG_ERROR("No valid connection given");
00109 return;
00110 }
00111 if (!QuidValid(vidQuid))
00112 {
00113 ILOG_ERROR("Invalid video Quid");
00114 return;
00115 }
00116
00117 String query =
00118 "select fi.frame_image_id \
00119 from frame_image fi, fragment f, media m \
00120 where m.identifier = " + MakeString(vidQuid) + " and \
00121 f.media_id = m.media_id and \
00122 fi.fragment_id = f.fragment_id \
00123 order by f.fragment_start;";
00124
00125 MapiHdl hdl = mConn->QueryPartStart(query);
00126 if (hdl == 0)
00127 return;
00128
00129 if (!mConn->QueryPartFetchInt(hdl, 0, mFrameImageId, mNrImages))
00130 return;
00131
00132 conn->QueryPartEnd(hdl);
00133 }
00134
00135 Link::Monet::Connection* mConn;
00136 int mNrImages;
00137 int* mFrameImageId;
00138
00139 ILOG_VAR_DEC;
00140 };
00141
00142 ILOG_VAR_INIT(ImageArchiveMapi, Impala.Core.Array);
00143
00144 }
00145 }
00146 }
00147
00148 #endif