Go to the documentation of this file.00001 #ifndef IZ_ENCODER_H
00002 #define IZ_ENCODER_H 1
00003
00004 #include <cstring>
00005
00006 #include "iz_p.h"
00007
00008 namespace IZ {
00009
00010 #define encodePixel(predictor) \
00011 { \
00012 Pixel<> pix, pp; \
00013 \
00014 pix.readFrom(p); \
00015 pp.predict(p, bpp, bpr, predictor::predict); \
00016 pix -= pp; \
00017 pix.forwardTransform(); \
00018 p += bpp; \
00019 pix.toUnsigned(); \
00020 \
00021 int nl = pix.numBits(); \
00022 cx = (cx << CONTEXT_BITS) + nl; \
00023 this->writeBits(dBits[cx & bitMask(2 * CONTEXT_BITS)], dCount[cx & bitMask(2 * CONTEXT_BITS)]); \
00024 pix.writeBits(*this, nl); \
00025 }
00026
00027 template <
00028 int bpp = 3,
00029 typename Predictor = Predictor3avgplane<>,
00030 typename Code = U32
00031 >
00032 class ImageEncoder : public BitEncoder<Code>
00033 {
00034 public:
00035 ImageEncoder() {
00036 memcpy(dBits, staticdBits, sizeof(dBits));
00037 memcpy(dCount, staticdCount, sizeof(dCount));
00038 }
00039
00040 void encodeImagePixels(const Image<> &im) __attribute__((always_inline)) {
00041 const int bpr = im.samplesPerLine();
00042 const unsigned char *p = im.data();
00043 int size = im.width() * im.height();
00044 const unsigned char *pend = p + bpp * size;
00045 unsigned int cx = (7 << CONTEXT_BITS) + 7;
00046
00047
00048 encodePixel(Predictor0<>);
00049
00050 const unsigned char *endline = p + bpr - bpp;
00051 while (p != endline) {
00052 encodePixel(Predictor1x<>);
00053 }
00054 while (p != pend) {
00055
00056 encodePixel(Predictor1y<>);
00057
00058 const unsigned char *endline = p + bpr - bpp;
00059 while (p != endline) {
00060 encodePixel(Predictor);
00061 }
00062 }
00063 }
00064
00065 void encodeImageSize(const Image<> &im) {
00066 int w = im.width() - 1;
00067 int h = im.height() - 1;
00068 int b = IZ::numBits(w | h);
00069 this->writeBits(b, 4);
00070 this->writeBits(w, b);
00071 this->writeBits(h, b);
00072 this->flushCache();
00073 }
00074
00075 private:
00076 unsigned int dBits[1 << (2 * CONTEXT_BITS)];
00077 unsigned int dCount[1 << (2 * CONTEXT_BITS)];
00078 };
00079
00080 }
00081
00082 #endif