KSquare Utilities
ScannerFile2BitEncoded.h
Go to the documentation of this file.
1 #if !defined(_SCANNERFILE2BITENCODED_)
2 #define _SCANNERFILE2BITENCODED_
3 
4 #include "RunLog.h"
5 #include "KKStr.h"
6 using namespace KKB;
7 
8 #include "ScannerFile.h"
9 
10 namespace KKLSC
11 {
12  /**
13  *@class ScannerFile2BitEncoded
14  *@brief Implements a 2 bit Encoded format.
15  *@details This is a very simple implementation of run-length compression.
16  * All pixels will be translated from 8 bit to 2 bit.
17  *@code
18  * All data will be stored in two byte records with the exception of text.
19  * each 2 byte record will start with a 3 bit OpCode and a one bit eol flag.
20  * the rest of the 12 bits will be organized depending on the opcode field.
21  *
22  * Bits
23  * =====
24  * 0 - 2: OP-Code
25  *
26  * 3 - 3: EOL Flag; End of ScanLine '0' = Not end of line, '1' = End of line.
27  *
28  * Op-Code
29  * 0: Text Block
30  * Bits 4 - 15 Length of text block in 4 byte groups. So number in this field
31  * is multiplied by 4 to get the total number of bytes. This allows
32  * for up to (4096 * 4) bytes.
33  *
34  * 1: 3 Raw Bytes
35  * Pix0 Pix1 Pix2
36  * (4-7) (8-11) (12-15)
37  *
38  * 2: Number of White Spaces
39  * Bits 4 - 15 Number of 4 pixels that are background (0). The total length of
40  * this field is this number times 4 allowing for a run-length of up
41  * to (4 * 4096) spaces divisible by 4.
42  *
43  *@endcode
44  */
46  {
47  public:
49 
50  ScannerFile2BitEncoded (const KKStr& _fileName,
51  RunLog& _log
52  );
53 
54  ScannerFile2BitEncoded (const KKStr& _fileName,
55  kkuint32 _pixelsPerScanLine,
56  kkuint32 _frameHeight,
57  RunLog& _log
58  );
59 
60  virtual
62 
63  static const uchar* CompensationTable ();
64 
65  virtual Format FileFormat () const {return Format::sf2BitEncoded;}
66 
67  virtual
68  void ScanRate (float _scanRate);
69 
70 
71  virtual
72  void WriteTextBlock (const uchar* txtBlock,
73  kkuint32 txtBlockLen
74  );
75 
76  protected:
77  /** @brief Read on one Scanner File Frame. */
79 
80  virtual
82 
83  /** @brief Write the contents of 'frameBuffer' to he end of the scanner file. */
84  virtual void WriteBufferFrame ();
85 
86 
87 
88  private:
89  void AllocateEncodedBuff ();
90 
91  static void ExitCleanUp ();
92 
93  void GetNextScanLine (uchar* lineBuff,
94  kkuint32 lineBuffSize
95  );
96 
97  void WriteNextScanLine (const uchar* buffer,
98  kkuint32 bufferLen
99  );
100 
101  /* Methods and variables needed for both reading and writing scanner files. */
102  static uchar* convTable2BitTo8Bit; /**< Lookup table to translate from 2 bit to 8bit pixels. */
103  static uchar* convTable8BitTo2Bit; /**< Lookup table to translate from 8 bit to 2bit pixels. */
104  static uchar* compensationTable; /**< Lookup table 256 long used to translate source pixels from a camera to the *
105  * same values as would be returned if they are written and then reread by this driver.
106  */
107 
108  static void BuildConversionTables ();
109 
110  struct OpRecEndOfScanLine; // 0
111  struct OpRecTextBlock; // 1
112  struct OpRecTextBlock_2; // 1 (Part 2)
113  struct OpRecRunLenPVx; // 4 thru 7
114  struct OpRecRunLen10Bit; // 8
115  struct OpRecRunLen10Bit_2; // 8 (Part 2)
116  struct OpRecRawPixelOne; // 10
117  struct OpRecRawPixelsTwo; // 11
118  struct OpRecRawPixelsVarLen4Bit; // 12
119  struct OpRecRawPixelsVarLen12Bit; // 13
120  struct OpRecRawPixelsVarLen12Bit_2; // 13 (Part 2)
121  struct RawPixelRec;
122 
123  union OpRec;
124  typedef OpRec* OpRecPtr;
125 
126 
127  /** Methods and variables that are required for reading a scanner file. */
128  void ProcessTextBlock (const OpRec& rec);
129  void AllocateRawPixelRecBuffer (kkuint32 size);
130  void ProcessRawPixelRecs (kkuint16 numRawPixels,
131  uchar* lineBuff,
132  kkuint32 lineBuffSize,
133  kkuint32& bufferLineLen
134  );
135 
136  RawPixelRec* rawPixelRecBuffer;
137  kkuint32 rawPixelRecBufferLen;
138  kkuint32 rawPixelRecBufferSize;
139 
140  /** Methods and variables that are required for writing a scanner file. */
141 
142  void AddCurRunLenToOutputBuffer ();
143 
144  void AddCurRawStrToOutputBuffer ();
145 
146  void AddRawStrPixelsToEncodedBuffer (kkuint16& nextCp,
147  kkuint16 len
148  );
149 
150  void AllocateRawStr (kkuint16 size);
151 
152  void ReSizeEncodedBuff (kkuint32 newSize);
153 
154  typedef enum {csNull, csRunLen, csRaw} CompStatus;
155 
156  OpRecPtr encodedBuff; /**< This is where compressed data will be stored before writing to scanner file. */
157  kkuint32 encodedBuffLen; /**< Number of bytes used so far. */
158  OpRecPtr encodedBuffNext; /**< Pointer to next position in encodedBuff to write to. */
159  kkuint32 encodedBuffSize; /**< Size of 'encodedBuff' allocated. */
160 
161  uchar* rawStr;
162  kkuint16 rawStrLen;
163  kkuint16 rawStrSize;
164 
165 
166  kkint32 runLen;
167  uchar runLenChar;
168  CompStatus curCompStatus;
169  }; /* ScannerFile2BitEncoded */
170 }
171 
172 #endif
virtual kkint64 SkipToNextFrame()
Skip to start of next frame returning back byte offset of that frame.
__int32 kkint32
Definition: KKBaseTypes.h:88
virtual Format FileFormat() const
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
Implements a 2 bit Encoded format.
virtual void WriteTextBlock(const uchar *txtBlock, kkuint32 txtBlockLen)
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
__int64 kkint64
Definition: KKBaseTypes.h:90
KKTHread * KKTHreadPtr
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
Contains Classes that are specific to Cameras physical characteristics.
kkuint32 ReadBufferFrame()
Read on one Scanner File Frame.
static const uchar * CompensationTable()
ScannerFile2BitEncoded(const KKStr &_fileName, kkuint32 _pixelsPerScanLine, kkuint32 _frameHeight, RunLog &_log)
ScannerFile2BitEncoded(const KKStr &_fileName, RunLog &_log)
Used for logging messages.
Definition: RunLog.h:49
virtual void ScanRate(float _scanRate)
virtual void WriteBufferFrame()
Write the contents of &#39;frameBuffer&#39; to he end of the scanner file.
ScannerFile2BitEncoded * ScannerFile2BitEncodedPtr