1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef YUV_STAMPER_H_
6 #define YUV_STAMPER_H_
7 
8 #include "nptypes.h"
9 
10 namespace mozilla {
11 
12 class YuvStamper {
13  public:
14   bool WriteDigits(uint32_t value);
15 
16   template <typename T>
17   static bool Write(uint32_t width, uint32_t height, uint32_t stride,
18                     unsigned char* pYData, const T& value, uint32_t x = 0,
19                     uint32_t y = 0) {
20     YuvStamper stamper(pYData, width, height, stride, x, y,
21                        (sDigitWidth + sInterDigit) * sPixelSize,
22                        (sDigitHeight + sInterLine) * sPixelSize);
23     return stamper.WriteDigits(value);
24   }
25 
26   static bool Encode(uint32_t width, uint32_t height, uint32_t stride,
27                      unsigned char* pYData, unsigned char* pMsg, size_t msg_len,
28                      uint32_t x = 0, uint32_t y = 0);
29 
30   static bool Decode(uint32_t width, uint32_t height, uint32_t stride,
31                      unsigned char* pYData, unsigned char* pMsg, size_t msg_len,
32                      uint32_t x = 0, uint32_t y = 0);
33 
34  private:
35   YuvStamper(unsigned char* pYData, uint32_t width, uint32_t height,
36              uint32_t stride, uint32_t x, uint32_t y,
37              unsigned char symbol_width, unsigned char symbol_height);
38 
39   bool WriteDigit(unsigned char digit);
40   void WritePixel(unsigned char* data, uint32_t x, uint32_t y);
41   uint32_t Capacity();
42   bool AdvanceCursor();
43   bool WriteBit(bool one);
44   bool Write8(unsigned char value);
45   bool ReadBit(unsigned char& value);
46   bool Read8(unsigned char& bit);
47 
48   const static unsigned char sPixelSize = 3;
49   const static unsigned char sDigitWidth = 6;
50   const static unsigned char sDigitHeight = 7;
51   const static unsigned char sInterDigit = 1;
52   const static unsigned char sInterLine = 1;
53   const static uint32_t sBitSize = 4;
54   const static uint32_t sBitThreshold = 60;
55   const static unsigned char sYOn = 0x80;
56   const static unsigned char sYOff = 0;
57   const static unsigned char sLumaThreshold = 96;
58   const static unsigned char sLumaMin = 16;
59   const static unsigned char sLumaMax = 235;
60 
61   unsigned char* pYData;
62   uint32_t mStride;
63   uint32_t mWidth;
64   uint32_t mHeight;
65   unsigned char mSymbolWidth;
66   unsigned char mSymbolHeight;
67 
68   struct Cursor {
CursorCursor69     Cursor(uint32_t x, uint32_t y) : x(x), y(y) {}
70     uint32_t x;
71     uint32_t y;
72   } mCursor;
73 };
74 
75 }  // namespace mozilla
76 
77 #endif
78