typedef int RACIn; typedef int RACOut; class PlaneColorInfo { public: pixel_t min, max; virtual static bool is_simple() { return false; } // @pre: low>=min, high<=max virtual int range_test(int x, int y, pixel_t low, pixel_t high)=0; } class SimplePlaneColorInfo : public PlaneColorInfo { public: static bool is_simple() { return true; } int range_test(int x, int y, pixel_t low, pixel_t high) { if (high= 0); // -1 if the plane does not exist return info[reverse_index[frame][p]]; } void add_plane(plane_semantics p, uint32_t frame = 0, PlaneColorInfo *pci) { info.push_back(MetaData(p, pci)); ImageData(uint16_t h, uint16_t w, std::vector pci, std::vector pt = RGBA) : height(h), width(w) { info.push_back(MetaData(pt[i], pci[i])); // represents transformer instance // transformer objects are maintained during encode/decode for all transformers involved class Transformer { public: // initialization during encode void virtual transform(const ImageData &in, ImageData &out) =0; // serialization during encode void virtual serialize(RACOut &rac) const =0; // deserialization during decode void virtual deserialize(RACIn &rac) =0; // apply transformation on metadata only during decode void virtual transformMeta(const ImageData &in, ImageData &out) const =0; // apply inverse transformation on data and metadata during decode void virtual transformInv(const ImageData &in, ImageData &out) const =0; } class TransformerFactory { public: Transformer* deserialize(RACIn &rac) { } void serialize(RACOut &rac, Transformer* trans) { } } class RGBToBWTransformer : public Transformer { class RGBToBWTransformerData : public TransformerData { } void transform(const ImageData &in, ImageData &out) { out = in; out.remove_plane(RED); out.remove_plane(GREEN); out.remove_plane(BLUE); Plane &RP = in.plane(RED); Plane &GP = in.plane(GREEN); Plane &BP = in.plane(BLUE); MetaData &RM = in.metadata(RED); MetaData &GM = in.metadata(GREEN); MetaData &BM = in.metadata(BLUE); if (RM.info->is_simple() && GM.info->is_simple() && BM.info->is_simple()) { } else { } out.add_plane(Y); Plane &YP = out.plane(Y); MetaData &YM = in.metadata(Y); } } #include #include #include "symbol.h" template class CompoundSymbolChances { public: SymbolChance realChances; std::vector,SymbolChance > > virtChances; uint64_t realSize; std::vector virtSize; }; template class CompoundSymbolCoder { private: RAC& rac; CompoundSymbolChances *chances; std::vector *select; BitChance& virt = (*select)[j] ? chances->virtChances[j].first.bit(type,i) : chances->virtChances[j].second.bit(type,i); virt.estim(bit, chances->virtSize[j]); virt.put(bit); } } public: } return bit; } CompoundSymbolCoder(RAC& racIn) : rac(racIn) { chances = NULL; select = NULL; } int read_int(CompoundSymbolChances& chancesIn, std::vector &selectIn, int min, int max, int(*range_test)(int, int)) { chances = &chancesIn; select = &selectIn; return reader(*this, min, max, range_test); } void write_int(CompoundSymbolChances& chancesIn, std::vector &selectIn, int min, int max, int(*range_test)(int, int), int val) { chances = &chancesIn; select = &selectIn; writer(*this, min, max, range_test, val); } }; // fprintf(stderr,"Checking property %i (val=%i, splitval=%i)\n",inner_node[pos].property,properties[inner_node[pos].property],inner_node[pos].splitval); leaf_node.push_back(CompoundSymbolChances(result)); CompoundSymbolChances &chances2 = find_leaf(properties); return coder.read_int(chances2,selection, min, max, range_test); CompoundSymbolChances &chances2 = find_leaf(properties); coder.write_int(chances2,selection, min, max, range_test, val); #ifndef _RAC_SYMBOL_H_ #define _RAC_SYMBOL_H_ 1 #include #include #include "util.h" #include "chance.h" typedef enum { BIT_ZERO, BIT_SIGN, BIT_EXP, BIT_MANT } SymbolChanceBitType; template class SymbolChance { int bits; std::vector chances; public: BitChance inline &bitZero() { return chances[0]; } BitChance inline &bitSign() { return chances[1]; } // all exp bits 0 -> int(log2(val)) == 0 [ val == 1 ] // exp bits up to i are 1 -> int(log2(val)) == i+1 BitChance inline &bitExp(int i) { assert(i >= 0 && i < bits-1); return chances[2+i]; } BitChance inline &bitMant(int i) { assert(i >= 0 && i < bits); return chances[bits+1+i]; } BitChance inline &bit(SymbolChanceBitType typ, int i = 0) { switch (typ) { case BIT_ZERO: return bitZero(); case BIT_SIGN: return bitSign(); case BIT_EXP: return bitExp(i); case BIT_MANT: return bitMant(i); } } SymbolChance(int bitsin) : bits(bitsin), chances(2*bits+1) { } }; template class SimpleSymbolCoder { private: SymbolChance ctx; RAC *rac; public: SimpleSymbolCoder(RAC& racIn, int nBits) : ctx(nBits) { rac = &racIn; } void inline write(bool bit, SymbolChanceBitType typ, int i = 0) { BitChance& bch = ctx.bit(typ,i); rac->write(bch.get(), bit); bch.put(bit); } bool inline read(SymbolChanceBitType typ, int i = 0) { BitChance& bch = ctx.bit(typ,i); bool bit = rac->read(bch.get()); bch.put(bit); return bit; } }; int default_range_test(int a, int b) { assert(b >= a); return (b-a+1); } static inline int signed_test(int(*range_test)(int,int), int low, int high, int sign) { if (low > high) return 0; if (sign > 0) return range_test(low, high); return range_test(-high, -low); } template int reader(SymbolCoder& coder, int min, int max, int(*range_test)(int, int)) { assert(min<=max); assert(range_test(min,max)); if (min == max) return min; // nodig? if (max >= 0 && min <= 0 && range_test(0,0)) { if (!signed_test(range_test,min,-1,1) && !signed_test(range_test,1,max,1)) return 0; if (coder.read(BIT_ZERO)) return 0; } bool sign = true; if (max > 0 && min < 0 && signed_test(range_test,min,-1,1) && signed_test(range_test,1,max,1)) { sign = coder.read(BIT_SIGN); } else { if (min < 0 || !signed_test(range_test,1,max,1)) sign = false; } if (sign && min <= 0) min = 1; if (!sign && max >= 0) max = -1; while (i < emax) { // if exponent >i is impossible, we are done if (sign && !signed_test(range_test,1<<(i+1),max,1)) break; if (!sign && !signed_test(range_test,min,-(1<<(i+1)),1)) break; // if exponent i is possible, output the exponent bit if (coder.read(BIT_EXP,i)) break; } i++; } int e = i; int have = (1 << e); int left = (1 << e)-1; int bit = 1; int minval = (sign ? have : -(have+left)); int maxval = (sign ? have+left : -have); if (min > minval) minval = min; if (max < maxval) maxval = max; left ^= (1 << (--pos)); if (minval == maxval) return minval; int minval1 = (sign ? have+(1< void writer(SymbolCoder& coder, int min, int max, int(*range_test)(int, int), int &value) { assert(min<=max); assert(value>=min); assert(value<=max); assert(signed_test(range_test,min,max,1)); // avoid doing anything if the value is already known (this line is optional; behavior should be identical if commented out) if (signed_test(range_test,min,max,1) == 1) return; if (value) { // value is nonzero // only output zero bit if value could also have been zero if (max >= 0 && min <= 0 && range_test(0,0)) coder.write(false,BIT_ZERO); if (max > 0 && min < 0) { // only output sign bit if value can be both pos and neg if (range_test(min,-1) && range_test(1,max)) coder.write(sign,BIT_SIGN); } while (i < emax) { // if exponent >i is impossible, we are done coder.write(i==e, BIT_EXP, i); i++; } int have = (1 << e); int left = (1 << e)-1; int bit = 1; int minabs = have, maxabs = have+left; left ^= (1 << (--pos)); if (signed_test(range_test,minabs,maxabs,sign)==1) return; int minabs1 = have + (1<> pos) & 1; coder.write(bit, BIT_MANT, pos); } have |= (bit << pos); } } else { // value is zero // only output zero bit if value could also have been nonzero if (range_test(min,-1) || range_test(1,max)) coder.write(true, BIT_ZERO); } } #endif #include #include #include "symbol.h" #include "rac.h" #include "chance.h" #include "compound.h" std::string text = "The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too."; int main() { { FILE *f = fopen("test.dat","w"); RacOutput40 rac(f); SimpleSymbolCoder coder(rac,8); int diff = curr - prev; int min = 0 - prev; int max = 255 - prev; prev = curr; write_int(coder, min, max, default_range_test, diff); } rac.flush(); fclose(f); } { FILE *f = fopen("test.dat","r"); SimpleSymbolCoder coder(rac,8); int prev = 0; prev = prev + diff; assert(curr == prev); if (curr != prev) printf("Fail: pos %i should be %i, but decoded %i\n",i,curr,prev); } return 0; }