KSquare Utilities
ScannerFile3BitEncoded.h
Go to the documentation of this file.
1 #if !defined(_SCANNERFILE3BITENCODED_)
2 #define _SCANNERFILE3BITENCODED_
3 
4 #include "ScannerFile.h"
5 
6 namespace KKLSC
7 {
8  /**
9  *@class ScannerFile3BitEncoded
10  *@brief Implements a 3 bit Encoded format.
11  *@details This is a very simple implementation of run-length compression only background
12  * pixels (0) and Foreground Pixels(7/255) will be compressed by groups of 4.
13  * All pixels will be translated from 8 bit to 3 bit.
14  *<p>
15  * All data will be stored in two byte records with the exception of text.
16  * each 2 byte record will start with a 3 bit OpCode and a one bit eol flag.
17  * the rest of the 12 bits will be organized depending on the opcode field.
18  *<p>
19  *@code
20  * Bits
21  * ======
22  * 0 - 2: OP-Code
23  *
24  * 3 - 3: EOL Flag; End of ScanLine '0' = Not end of line, '1' = End of line.
25  *
26  * Op-Code
27  * 0: Text Block
28  * Bits 4 - 15 Length of text block in 4 byte groups. So number in this field
29  * is multiplied by 4 to get the total number of bytes. This allows
30  * for up to (4096 * 4) bytes.
31  *
32  * 1: 4 Raw Bytes
33  * Pix0 Pix1 Pix2 Pix
34  * (4-6) (7-9) (10-12) (13-15)
35  *
36  * 2: Number of White Spaces
37  * Bits 4 - 15 Number of 4 pixels that are background (0). The total length of
38  * this field is this number times 4 allowing for a run-length of up
39  * to (4 * 4096) spaces divisible by 4.
40  *
41  * 3: Number of Black Spaces
42  * Bits 4 - 15 Number of 4 pixels that are foreground (7/255). The total length of
43  * this field is this number times 4 allowing for a run-length of up
44  * to (4 * 4096) Foreground pixels (7/255) divisible by 4.
45  *
46  * 4: Instrument Data
47  * Bits 3 - 6
48  *
49  * 3-Bit to 8-bit Translation
50  * 0: 0
51  * 1: 36
52  * 2: 73
53  * 3: 109
54  * 4: 145
55  * 5: 181
56  * 6: 218
57  * 7: 255
58  *
59  * 8-Bit to 3-bit Translation
60  * 0 -> 31: 0
61  * 32 -> 63: 1
62  * 64 -> 95: 2
63  * 96 -> 127: 3
64  * 128 -> 158: 4
65  * 159 -> 190: 5
66  * 191 -> 222: 6
67  * 223 -> 255; 7
68  *@endcode
69  **/
71  {
72  public:
74 
75  ScannerFile3BitEncoded (const KKStr& _fileName,
76  RunLog& _log
77  );
78 
79  ScannerFile3BitEncoded (const KKStr& _fileName,
80  kkuint32 _pixelsPerScanLine,
81  kkuint32 _frameHeight,
82  RunLog& _log
83  );
84 
85  virtual
87 
88  static
89  const uchar* CompensationTable ();
90 
91  virtual Format FileFormat () const {return Format::sf3BitEncoded;}
92 
93  virtual
94  void ScanRate (float _scanRate);
95 
96 
97  virtual
98  void WriteTextBlock (const uchar* txtBlock,
99  kkuint32 txtBlockLen
100  );
101 
102  protected:
103  /** @brief Read on one Scanner File Frame. */
105 
106  virtual kkint64 SkipToNextFrame ();
107 
108  /** @brief Write the contents of 'frameBuffer' to he end of the scanner file. */
109  virtual void WriteBufferFrame ();
110 
111 
112 
113  private:
114  void AllocateWorkLineAndOutputBuf ();
115 
116  static
117  void BuildConversionTables ();
118 
119  static
120  void ExitCleanUp ();
121 
122  void GetNextScanLine (uchar* lineBuff,
123  kkuint32 lineBuffSize
124  );
125 
126  void WriteNextScanLine (const uchar* buffer,
127  kkuint32 bufferLen
128  );
129 
130  void WriteNextScanLine2 (const uchar* buffer,
131  kkuint32 bufferLen
132  );
133 
134  struct OpRecTextBlock;
135  struct OpRec4RawPixels;
136  struct OpRecSpaces;
137  struct OpRecBlackOuts;
138  struct OpRecRunLen;
139  struct OpRecRaw;
140  union OpRec;
141  typedef OpRec* OpRecPtr;
142 
143  inline
144  void Write4Spaces (OpRecPtr& outputBuffPtr,
145  kkint32& outputBuffUsed,
146  kkint32& num4SpacesInARow,
147  ushort eol
148  );
149 
150  inline
151  void Write4BlackOuts (OpRecPtr& outputBuffPtr,
152  kkint32& outputBuffUsed,
153  kkint32& num4BlackOutsInARow,
154  ushort eol
155  );
156 
157  static
158  uchar* convTable3BitTo8Bit; /**< Lookup table to translate from 3 bit to 8bit pixels. */
159  static
160  uchar* convTable8BitTo3Bit; /**< Lookup table to translate from 8 bit to 3bit pixels. */
161 
162  static
163  uchar* compensationTable; /**< Lookup table 256 long used to translate source pixels from a camera to the *
164  * same values as would be returned if they are written and then reread by this driver.
165  */
166 
167  uchar fourSpaces[4]; /**< Used to quickly check for 4 blanks in a row. */
168  uchar fourBlackOuts[4];
169  OpRecPtr outputBuff; /**< This is where compressed data will be stored before writing to scanner file. */
170  kkuint32 outputBuffLen; /**< Size of 'outputBuff' allocated. */
171  uchar* workLine; /**< Where 4bit translated characters will be stored before compression. */
172  kkuint32 workLineLen; /**< length of the current 'workLine' buffer. */
173 
174  typedef enum {csNull, csRunLen, csRaw} CompStatus;
175  CompStatus curCompStatus;
176 
177  }; /* ScannerFile3BitEncoded */
178 }
179 
180 #endif
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
__int64 kkint64
Definition: KKBaseTypes.h:90
virtual void ScanRate(float _scanRate)
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
virtual kkint64 SkipToNextFrame()
Skip to start of next frame returning back byte offset of that frame.
Contains Classes that are specific to Cameras physical characteristics.
kkuint32 ReadBufferFrame()
Read on one Scanner File Frame.
unsigned short ushort
Unsigned short.
Definition: KKBaseTypes.h:79
ScannerFile3BitEncoded(const KKStr &_fileName, kkuint32 _pixelsPerScanLine, kkuint32 _frameHeight, RunLog &_log)
Implements a 3 bit Encoded format.
static const uchar * CompensationTable()
Used for logging messages.
Definition: RunLog.h:49
virtual Format FileFormat() const
virtual void WriteTextBlock(const uchar *txtBlock, kkuint32 txtBlockLen)
virtual void WriteBufferFrame()
Write the contents of &#39;frameBuffer&#39; to he end of the scanner file.
ScannerFile3BitEncoded * ScannerFile3BitEncodedPtr
ScannerFile3BitEncoded(const KKStr &_fileName, RunLog &_log)