KSquare Utilities
ScannerFile4BitEncoded.h
Go to the documentation of this file.
1 #if !defined(_SCANNERFILE4BITENCODED_)
2 #define _SCANNERFILE4BITENCODED_
3 
4 #include "ScannerFile.h"
5 
6 namespace KKLSC
7 {
8  /**
9  *@class ScannerFile4BitEncoded
10  *@brief Implements a 4 bit Encoded format.
11  *@details This is a very simple implementation of run-length compression
12  * only background pixels (0) will be compressed by groups of 4.
13  * All pixels will be translated from 8 bit to 3 bit.
14  *@code
15  * 1) Records are or varying length.
16  * 2) Each record starts with a 4 bit Op-Code.
17  * 3) Records contain either Text or Imagery
18  * 4) Imagery records can be either Run-Length (Same Pixel repeating) or Raw-String(A sequence of pixels).
19  *
20  * Summary of Op-Codes:
21  * --------------------------------------------------------------------
22  * 0: End-Of-Line: Indicates that end of Scan-Line reached.
23  * 1: Text-Block:
24  * 2: InsrumentData
25  * 3: Unused
26  * 4: Run-Length (2 Pixels).
27  * 5: Run-Length (3 Pixels).
28  * 6: Run-Length (4 Pixels).
29  * 7: Run-Length (5 Pixels).
30  * 8: Run-Length (6 Pixels).
31  * 9: Run-Length (7 Pixels).
32  * 10: Run-Length (1 thru 256 Pixels).
33  * 11: Raw-String (1 Pixel Length).
34  * 12: Raw-String (Even length 2 thru 32).
35  * 13: Raw-String (Odd Length 1 thru 513).
36  * 14: Unused.
37  * 15: Unused.
38  *
39  *
40  * Bits
41  * =====
42  * 0 - 3: OP-Code (Values 0 thru 15)
43  *
44  *
45  *----------------------------------------------------------
46  * Op-Code = 0 end of line
47  * 4 - 7: Ignore
48  *
49  *
50  *----------------------------------------------------------
51  * Op-Code 1: Test Block.
52  * 4 - 4: End of text block.
53  * 5 - 7: TextBlock Len HighEnd.
54  * 8 - 15: TextBlock Len LowEnd
55  * Text-Block-Len = HighEnd * 256 + LowEnd.
56  * Followed by 'Text-Block-Len' bytes.
57  *
58  *
59  *----------------------------------------------------------
60  * Op-Code 2: InstrumentData
61  * 4 - 7: InstrumentId
62  * 8 - 39: Scan-Line Number.
63  * 40 - 71: Instrument Data.
64  *
65  *
66  *----------------------------------------------------------
67  * Op-Code = 4 thru 9: Short-Run-Len
68  * RunLen = OpCode - 2
69  * 4 - 7: Pixel Value being repeated.
70  *
71  *
72  *----------------------------------------------------------
73  * Op-Code = 10: Run-length of range "1" thru "256"
74  * Since there are no run-lengths of '0'; '0' will decode to Run-Length = 1;
75  * 4 - 7: Pixel Value being repeated.
76  * 8 - 15: Run-length 0=1, 1=2, 2=3,... 254=255, 255=256
77  *
78  *
79  *----------------------------------------------------------
80  * Op-Code = 11: One Pixel Raw String.
81  * 4 - 7: PixelValue.
82  *
83  *
84  *----------------------------------------------------------
85  * Op-Code = 12 Raw Pixel String of even length ranging from 2 to 32
86  * because each byte can fit two pixels it is best that Raw String lengths
87  * always be multiples of 2.
88  *
89  * 4 - 7: Len; Num-Raw-Pixels = (Len + 1) * 2
90  * Len 0=(2 raw Pixels), 1=(4 raw pixels), 2=(6 raw pixels),,, 15=(32 raw pixels)
91  * Number of following bytes = (Len + 1).
92  *
93  *
94  *----------------------------------------------------------
95  * Op-Code = 13 Odd number of raw pixels up to length of 513 pixels.
96  * We use the other 4 bits in the 1st byte to represent the high order bits of the length
97  * and the 1st 4 bits in the second byte for the low order; that leaves room for one
98  * pixel in the second byte. This is why odd length stings are supported.
99  *
100  * 8 - 7: LenHigh String Length = 1 + 2 * (LenHigh * 16 + LenLow);
101  * 8 - 11: LenLow
102  * 12 - 15: First Raw Pixel. Will be followed by ((LenHigh * 16 + LenLow) bytes with two pixels
103  * in each one.
104  *
105  * Translation Tables
106  * ======================================
107  * 4 8 4
108  * Bit Bit 8-Bit Range Bit
109  * === === ============ ===
110  * 0 0 ( 0 - 15): 0
111  * 1 17 ( 16 - 31): 1
112  * 2 34 ( 32 - 47): 2
113  * 3 51 ( 48 - 63): 3
114  * 4 68 ( 64 - 79): 4
115  * 5 85 ( 80 - 95): 5
116  * 6 102 ( 96 - 111): 6
117  * 7 119 (112 - 127): 7
118  * 8 136 (128 - 143): 8
119  * 9 153 (144 - 159): 9
120  * 10 170 (160 - 175): 10
121  * 11 187 (176 - 191): 11
122  * 12 204 (192 - 207): 12
123  * 13 221 (208 - 223): 13
124  * 14 238 (224 - 239): 14
125  * 15 255 (240 - 255): 15
126  *
127  *@endcode
128  */
130  {
131  public:
133 
134  ScannerFile4BitEncoded (const KKStr& _fileName,
135  RunLog& _log
136  );
137 
138  ScannerFile4BitEncoded (const KKStr& _fileName,
139  kkuint32 _pixelsPerScanLine,
140  kkuint32 _frameHeight,
141  RunLog& _log
142  );
143 
144  virtual
146 
147  static const uchar* CompensationTable ();
148 
149  virtual Format FileFormat () const {return Format::sf4BitEncoded;}
150 
151  virtual
152  void ScanRate (float _scanRate);
153 
154  virtual
155  void WriteTextBlock (const uchar* txtBlock,
156  kkuint32 txtBlockLen
157  );
158 
159  /**
160  *@brief Writes a 32 bit number into the Scanner File Stream at current location.
161  *@param[in] idNum nNumber that identifies Instrument data, ex: 0 is reserved for Flow Meter Count.
162  *@param[in] dataWord 32 bit number being written.
163  */
164 // virtual
165 // void WriteInstrumentDataWord (uchar idNum,
166 // kkuint32 scanLineNum,
167 // WordFormat32Bits dataWord
168 // );
169 
170  protected:
171  /** @brief Read in one Scanner File Frame. */
173 
174  virtual kkint64 SkipToNextFrame ();
175 
176  /** @brief Write the contents of 'frameBuffer' to he end of the scanner file. */
177  virtual void WriteBufferFrame ();
178 
179 
180 
181  private:
182  void AllocateEncodedBuff ();
183 
184  static void ExitCleanUp ();
185 
186  void GetNextScanLine (uchar* lineBuff,
187  kkuint32 lineBuffSize
188  );
189 
190  void WriteNextScanLine (const uchar* buffer,
191  kkuint32 bufferLen
192  );
193 
194  /* Methods and variables needed for both reading and writing scanner files. */
195  static uchar* convTable4BitTo8Bit; /**< Lookup table to translate from 3 bit to 8bit pixels. */
196  static uchar* convTable8BitTo4Bit; /**< Lookup table to translate from 8 bit to 3bit pixels. */
197  static uchar* compensationTable; /**< Lookup table 256 long used to translate source pixels from a camera to the
198  * same values as would be returned if they are written and then reread by this driver.
199  */
200 
201  static void BuildConversionTables ();
202 
203  struct OpRecEndOfScanLine; /**< OpCode 0 */
204  struct OpRecTextBlock1; /**< OpCode 1 */
205  struct OpRecTextBlock2;
206 
207  struct OpRecInstrumentDataWord1; /**< OpCode 2 */
208  struct OpRecInstrumentDataWord2; /**< OpCode 2 */
209  struct OpRecInstrumentDataWord3; /**< OpCode 2 */
210 
211  struct OpRecRunLen; /**< OpCodes 4 thru 9 */
212 
213  struct OpRecRun256Len1; /**< OpCode 8 */
214  struct OpRecRun256Len2;
215 
216  struct OpRecRaw1Pixel; /**< OpCode 11 */
217 
218  struct OpRecRaw32Pixels; /**< OpCode 12 */
219 
220  struct OpRecRaw513Pixels1; /**< OpCode 13 */
221  struct OpRecRaw513Pixels2;
222 
223  struct RawPixelRec;
224 
225  union OpRec;
226  typedef OpRec* OpRecPtr;
227 
228 
229  void PrintSizeInfo ();
230 
231 
232  /** Methods and variables that are required for reading a scanner file. */
233  void ProcessTextBlock (const OpRec& rec);
234  void ProcessInstrumentDataWord (const OpRec& rec);
235  void AllocateRawPixelRecBuffer (kkuint32 size);
236  void ProcessRawPixelRecs (kkuint16 numRawPixelRecs,
237  uchar* lineBuff,
238  kkuint32 lineBuffSize,
239  kkuint32& bufferLineLen
240  );
241 
242  RawPixelRec* rawPixelRecBuffer;
243  kkuint32 rawPixelRecBufferSize;
244  kkuint32 rawPixelRecBufferLen;
245 
246  /** Methods and variables that are required for writing a scanner file. */
247 
248  void AddCurRunLenToOutputBuffer ();
249  void AddCurRawStrToOutputBuffer ();
250  void AllocateRawStr (kkuint16 size);
251  void ReSizeEncodedBuff (kkuint32 newSize);
252 
253  typedef enum {csNull, csRunLen, csRaw} CompStatus;
254 
255  OpRecPtr encodedBuff; /**< This is where compressed data will be stored before writing to scanner file. */
256  kkuint32 encodedBuffLen; /**< Number of bytes used so far. */
257  OpRecPtr encodedBuffNext; /**< Pointer to next position in encodedBuff to write to. */
258  kkuint32 encodedBuffSize; /**< Size of 'encodedBuff' allocated. */
259 
260  uchar* rawStr;
261  kkuint16 rawStrLen;
262  kkuint16 rawStrSize;
263  //uint16 rawStrNumSameInARow;
264 
265  kkint32 runLen;
266  uchar runLenChar;
267  CompStatus curCompStatus;
268  }; /* ScannerFile4BitEncoded */
269 }
270 
271 #endif
ScannerFile4BitEncoded * ScannerFile4BitEncodedPtr
ScannerFile4BitEncoded(const KKStr &_fileName, RunLog &_log)
__int32 kkint32
Definition: KKBaseTypes.h:88
virtual kkint64 SkipToNextFrame()
Skip to start of next frame returning back byte offset of that frame.
static const uchar * CompensationTable()
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
virtual void ScanRate(float _scanRate)
__int64 kkint64
Definition: KKBaseTypes.h:90
virtual void WriteBufferFrame()
Write the contents of &#39;frameBuffer&#39; to he end of the scanner file.
virtual Format FileFormat() const
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()
Writes a 32 bit number into the Scanner File Stream at current location.
Implements a 4 bit Encoded format.
Used for logging messages.
Definition: RunLog.h:49
ScannerFile4BitEncoded(const KKStr &_fileName, kkuint32 _pixelsPerScanLine, kkuint32 _frameHeight, RunLog &_log)
virtual void WriteTextBlock(const uchar *txtBlock, kkuint32 txtBlockLen)