encoder.h
Go to the documentation of this file.
1 #ifndef IZ_ENCODER_H
2 #define IZ_ENCODER_H 1
3 
4 #include <cstring>
5 
6 #include "iz_p.h"
7 
8 namespace IZ {
9 
10 #define encodePixel(predictor) \
11 { \
12  Pixel<> pix, pp; \
13  \
14  pix.readFrom(p); \
15  pp.predict(p, bpp, bpr, predictor::predict); \
16  pix -= pp; \
17  pix.forwardTransform(); \
18  p += bpp; \
19  pix.toUnsigned(); \
20  \
21  int nl = pix.numBits(); \
22  cx = (cx << CONTEXT_BITS) + nl; \
23  this->writeBits(dBits[cx & bitMask(2 * CONTEXT_BITS)], dCount[cx & bitMask(2 * CONTEXT_BITS)]); \
24  pix.writeBits(*this, nl); \
25 }
26 
27 template <
28  int bpp = 3,
29  typename Predictor = Predictor3avgplane<>,
30  typename Code = U32
31 >
32 class ImageEncoder : public BitEncoder<Code>
33 {
34 public:
36  memcpy(dBits, staticdBits, sizeof(dBits));
37  memcpy(dCount, staticdCount, sizeof(dCount));
38  }
39 
40  void encodeImagePixels(const Image<> &im) __attribute__((always_inline)) {
41  const int bpr = im.samplesPerLine();
42  const unsigned char *p = im.data();
43  int size = im.width() * im.height();
44  const unsigned char *pend = p + bpp * size;
45  unsigned int cx = (7 << CONTEXT_BITS) + 7;
46 
47  /* first pixel in first line */
49  /* remaining pixels in first line */
50  const unsigned char *endline = p + bpr - bpp;
51  while (p != endline) {
53  }
54  while (p != pend) {
55  /* first pixel in remaining lines */
57  /* remaining pixels in remaining lines */
58  const unsigned char *endline = p + bpr - bpp;
59  while (p != endline) {
60  encodePixel(Predictor);
61  }
62  }
63  }
64 
65  void encodeImageSize(const Image<> &im) {
66  int w = im.width() - 1;
67  int h = im.height() - 1;
68  int b = IZ::numBits(w | h);
69  this->writeBits(b, 4);
70  this->writeBits(w, b);
71  this->writeBits(h, b);
72  this->flushCache();
73  }
74 
75 private:
76  unsigned int dBits[1 << (2 * CONTEXT_BITS)];
77  unsigned int dCount[1 << (2 * CONTEXT_BITS)];
78 };
79 
80 } // namespace IZ
81 
82 #endif
static unsigned int numBits(unsigned int v)
Definition: intmacros.h:89
int width() const
Definition: image.h:15
void encodeImageSize(const Image<> &im)
Definition: encoder.h:65
Definition: bitcoder.h:6
unsigned int staticdBits[1<< (2 *CONTEXT_BITS)]
Definition: table.cpp:37
int height() const
Definition: image.h:16
unsigned int U32
Definition: bitcoder.h:24
unsigned int dBits[1<< (2 *CONTEXT_BITS)]
Definition: encoder.h:76
void writeBits(unsigned int bits, unsigned int count)
Definition: bitcoder.h:170
const unsigned int staticdCount[1<< (2 *CONTEXT_BITS)]
Definition: table.cpp:9
const int CONTEXT_BITS
Definition: iz_p.h:12
void encodeImagePixels(const Image<> &im) __attribute__((always_inline))
Definition: encoder.h:40
void flushCache()
Definition: bitcoder.h:159
unsigned int dCount[1<< (2 *CONTEXT_BITS)]
Definition: encoder.h:77
#define encodePixel(predictor)
Definition: encoder.h:10


imagezero
Author(s):
autogenerated on Wed Jun 5 2019 22:02:47