00001 #ifndef INTERFACES_HEADER_
00002 #define INTERFACES_HEADER_
00003
00004
00005 struct IRoot;
00006 struct IQuality2SE;
00007 struct IColorTransformer;
00008 struct IShapeTransformer;
00009 struct ISquareEncoder;
00010 struct ISquareRanges;
00011 struct ISquareDomains;
00012 struct IStdEncPredictor;
00013 struct IIntCodec;
00014
00016 namespace MTypes {
00017 typedef double Real;
00018 typedef float SReal;
00019
00020 typedef MatrixSlice<SReal> SMatrix;
00021 typedef SMatrix::Const CSMatrix;
00022 typedef std::vector<SMatrix> MatrixList;
00023
00024 enum DecodeAct { Clear, Iterate };
00025
00026
00027 struct PlaneBlock;
00028
00029 typedef SummedMatrix<Real,SReal> SummedPixels;
00030 }
00031 namespace NOSPACE {
00032 using namespace std;
00033 using namespace MTypes;
00034 }
00035
00036
00037
00043 struct IRoot: public Interface<IRoot> {
00045 enum Mode {
00046 Clear,
00047 Encode,
00048 Decode
00049 };
00050
00051 static const Uint16 Magic= 65535-4063
00052 , SettingsMagic= Magic^12345;
00053
00055 virtual Mode getMode() =0;
00057 virtual QImage toImage() =0;
00058
00060 virtual bool encode
00061 ( const QImage &toEncode, const UpdateInfo &updateInfo=UpdateInfo::none ) =0;
00063 virtual void decodeAct( DecodeAct action, int count=1 ) =0;
00064
00066 virtual bool toStream(std::ostream &file) =0;
00069 virtual bool fromStream(std::istream &file,int zoom=0) =0;
00070
00073 int toFile(const char *fileName) {
00074 std::ofstream file( fileName, ios_base::binary|ios_base::trunc|ios_base::out );
00075 if ( !toStream(file) )
00076 return 0;
00077 else
00078 return file.tellp();
00079 }
00081 bool fromFile(const char *fileName,int zoom=0) {
00082 std::ifstream file( fileName, ios_base::binary|ios_base::in );
00083 return fromStream(file,zoom);
00084 }
00085
00087 bool allSettingsToFile(const char *fileName);
00089 bool allSettingsFromFile(const char *fileName);
00090 };
00091
00092
00093
00094
00097 struct IQuality2SE: public Interface<IQuality2SE> {
00099 virtual float rangeSE(float quality,int pixelCount) =0;
00103 void regularRangeErrors(float quality,int levelEnd,float *squareErrors);
00104 };
00105
00106
00107
00108
00111 struct IColorTransformer: public Interface<IColorTransformer> {
00113 struct PlaneSettings;
00114
00117 struct Plane {
00118 mutable SMatrix pixels;
00119 const PlaneSettings *settings;
00120 };
00121
00123 typedef std::vector<Plane> PlaneList;
00124
00127 virtual PlaneList image2planes(const QImage &toEncode,const PlaneSettings &prototype) =0;
00129 virtual QImage planes2image() =0;
00130
00132 virtual void writeData(std::ostream &file) =0;
00134 virtual PlaneList readData( std::istream &file, const PlaneSettings &prototype ) =0;
00135 };
00136
00137 struct IColorTransformer::PlaneSettings {
00138 int width
00139 , height
00140 , domainCountLog2
00141 , zoom;
00142 SReal quality;
00143 IQuality2SE *moduleQ2SE;
00144 const UpdateInfo &updateInfo;
00145
00147 PlaneSettings( int width_, int height_, int domainCountLog2_, int zoom_
00148 , SReal quality_=numeric_limits<SReal>::quiet_NaN()
00149 , IQuality2SE *moduleQ2SE_=0, const UpdateInfo &updateInfo_=UpdateInfo::none )
00150 : width(width_), height(height_)
00151 , domainCountLog2(domainCountLog2_), zoom(zoom_)
00152 , quality(quality_), moduleQ2SE(moduleQ2SE_), updateInfo(updateInfo_) {}
00153 };
00154
00155
00156
00157
00160 struct IShapeTransformer: public Interface<IShapeTransformer> {
00161 typedef IColorTransformer::PlaneList PlaneList;
00162
00164 virtual int createJobs(const PlaneList &planes) =0;
00166 virtual int jobCount() =0;
00167
00169 virtual void jobEncode(int jobIndex) =0;
00171 virtual void jobDecodeAct( int jobIndex, DecodeAct action, int count=1 ) =0;
00172
00174 virtual void writeSettings(std::ostream &file) =0;
00177 virtual void readSettings(std::istream &file) =0;
00178
00180 virtual int phaseCount() =0;
00183 virtual void writeJobs(std::ostream &file,int phaseBegin,int phaseEnd) =0;
00186 virtual void readJobs(std::istream &file,int phaseBegin,int phaseEnd) =0;
00187
00189 void writeJobs(std::ostream &file,int phaseBegin=0)
00190 { writeJobs( file, phaseBegin, phaseCount() ); }
00192 void readJobs(std::istream &file,int phaseBegin=0)
00193 { readJobs( file, phaseBegin, phaseCount() ); }
00194 };
00195
00196
00198
00199
00200 namespace MTypes {
00205 struct PlaneBlock: public SummedPixels {
00206 typedef IColorTransformer::PlaneSettings PlaneSettings;
00207
00208 const PlaneSettings *settings;
00209 ISquareRanges *ranges;
00210 ISquareDomains *domains;
00211 ISquareEncoder *encoder;
00212
00214 bool isReady() const
00215 { return this && ranges && domains && encoder && pixels.isValid(); }
00216 };
00217 }
00218
00219
00226 struct ISquareRanges: public Interface<ISquareRanges> {
00228 struct RangeNode;
00230 typedef std::vector<RangeNode*> RangeList;
00231
00235 virtual void encode(const PlaneBlock &toEncode) =0;
00237 virtual const RangeList& getRangeList() const =0;
00238
00240 virtual void writeSettings(std::ostream &file) =0;
00242 virtual void readSettings(std::istream &file) =0;
00243
00245 virtual void writeData(std::ostream &file) =0;
00249 virtual void readData_buildRanges( std::istream &file, const PlaneBlock &block ) =0;
00250 };
00251
00252 struct ISquareRanges::RangeNode: public Block {
00254 struct EncoderData {
00255 float bestSE;
00256 };
00257
00259 mutable EncoderData *encoderData;
00261 int level;
00262
00264 bool isRegular() const
00265 { return width()==powers[level] && height()==powers[level]; }
00266 protected:
00268 RangeNode(const Block &block,int level_)
00269 : Block(block), encoderData(0), level(level_) {}
00270
00272 ~RangeNode()
00273 { delete encoderData; }
00274 };
00275
00276
00277
00278
00280 struct ISquareDomains: public Interface<ISquareDomains> {
00282 struct Pool;
00284 typedef std::vector<Pool> PoolList;
00285
00287 virtual void initPools(const PlaneBlock &planeBlock) =0;
00289 virtual void fillPixelsInPools(PlaneBlock &planeBlock) =0;
00290
00292 virtual const PoolList& getPools() const =0;
00295 virtual std::vector<short> getLevelDensities(int level,int stdDomCountLog2) =0;
00296
00298 virtual void writeSettings(std::ostream &file) =0;
00300 virtual void readSettings(std::istream &file) =0;
00301
00303 virtual void writeData(std::ostream &file) =0;
00305 virtual void readData(std::istream &file) =0;
00306 };
00307
00308 struct ISquareDomains::Pool: public SummedPixels {
00309 char type
00310 , level;
00311 float contrFactor;
00312
00315 Pool(short width_,short height_,char type_,char level_,float cFactor,short zoom)
00316 : type(type_), level(level_), contrFactor(cFactor)
00317 { setSize( lShift(width_,zoom), lShift(height_,zoom) ); }
00318 };
00319
00320
00321
00322
00324 struct ISquareEncoder: public Interface<ISquareEncoder> {
00325 typedef IColorTransformer::Plane Plane;
00326 typedef ISquareRanges::RangeNode RangeNode;
00327
00329 struct LevelPoolInfo {
00330 int indexBegin
00331 , density;
00332 };
00335 typedef std::vector< std::vector<LevelPoolInfo> > LevelPoolInfos;
00336
00338 virtual void initialize( IRoot::Mode mode, PlaneBlock &planeBlock ) =0;
00341 virtual float findBestSE(const RangeNode &range,bool allowHigherSE=false) =0;
00343 virtual void finishEncoding() =0;
00345 virtual void decodeAct( DecodeAct action, int count=1 ) =0;
00346
00348 virtual void writeSettings(std::ostream &file) =0;
00350 virtual void readSettings(std::istream &file) =0;
00351
00353 virtual int phaseCount() const =0;
00355 virtual void writeData(std::ostream &file,int phase) =0;
00358 virtual void readData(std::istream &file,int phase) =0;
00359 };
00360
00361
00362
00363
00365 struct IStdEncPredictor: public Interface<IStdEncPredictor> {
00367 struct Prediction {
00368 explicit Prediction( int domainID_=-1, char rotation_=-1 )
00369 : domainID(domainID_), rotation(rotation_) {}
00370
00371 int domainID;
00372 char rotation;
00373 };
00375 typedef std::vector<Prediction> Predictions;
00376
00378 struct IOneRangePredictor {
00380 virtual ~IOneRangePredictor() {}
00382 virtual Predictions& getChunk(float maxPredictedSE,Predictions &store) =0;
00383 };
00384
00386 struct NewPredictorData;
00387
00389 virtual IOneRangePredictor* newPredictor(const NewPredictorData &data) =0;
00391 virtual void cleanUp() =0;
00392 };
00393
00394 struct IStdEncPredictor::NewPredictorData {
00395 const ISquareRanges::RangeNode *rangeBlock;
00396 const SummedPixels *rangePixels;
00397 const ISquareDomains::PoolList *pools;
00398 const ISquareEncoder::LevelPoolInfos::value_type *poolInfos;
00400
00401 bool allowRotations
00402 , quantError
00403 , allowInversion
00404 , isRegular;
00405
00406 Real maxLinCoeff2
00407 , bigScaleCoeff;
00408
00409 Real rSum
00410 , r2Sum
00411 , pixCount
00412 , rnDev2
00413 , rnDev
00414 , qrAvg
00415 , qrDev
00416 , qrDev2;
00417 #ifndef NDEBUG
00418 NewPredictorData()
00419 : rangeBlock(0), rangePixels(), pools(0), poolInfos(0) {}
00420 #endif
00421 };
00422
00423
00424
00425
00427 struct IIntCodec: public Interface<IIntCodec> {
00429 virtual void setPossibilities(int possib) =0;
00431 virtual void encode(std::vector<int> &data,std::ostream &file) =0;
00433 virtual void decode(std::istream &file,int count,std::vector<int> &data) =0;
00434
00436 virtual void writeSettings(std::ostream &file) =0;
00438 virtual void readSettings(std::istream &file) =0;
00439 };
00440
00442
00443 #endif // INTERFACES_HEADER_