KSquare Utilities
ScannerFile2BitEncoded.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdio.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <iostream>
7 #include <fstream>
8 #include <map>
9 #include <vector>
10 #include "MemoryDebug.h"
11 using namespace std;
12 
13 #include "GlobalGoalKeeper.h"
14 #include "KKBaseTypes.h"
15 #include "OSservices.h"
16 #include "RunLog.h"
17 #include "KKStr.h"
18 using namespace KKB;
19 
20 
21 #include "ScannerFile.h"
22 
23 #include "Variables.h"
24 
26 
27 using namespace KKLSC;
28 
29 #pragma pack(push,1)
30 //#pragma pack(show)
32 {
33  ushort opCode: 4; /** 0 = End of Scan Line. */
35 };
36 
37 
38 
40 {
41  /** Will handle text blocks in one or more sections with 2^11 characters in each *
42  * The last section will have 'endOfText' == '1' *
43  */
44 
45  uchar opCode: 4; /**< 1 = Text Block. */
46  uchar endOfText: 1; /**< 0 = More Text blocks to follow, 1 = last text block. */
47  uchar lenHigh: 3; /**< Length = 256 * lenHigh + lenLow */
48 }; /* OpRecTextBlock */
49 
50 
51 
53 {
54  uchar lenLow :8; /**< Add this value to the prev rec 'lenHighBits' field to get the length of the text block. */
55 };
56 
57 
58 
60 {
61  uchar opCode :4; /**< Op Codes (4 thru 7) 4=PV0, 5=PV1, 6=PV2, 7=PV3 */
62  uchar len :4; /**< (len + 3), 0=3, 1=4, ... 15=18, White(0) pixels in a row. */
63 }; /* OpRecRunLenPVx */
64 
65 
66 
68 {
69  uchar opCode :4; /**< 8 Variable Run Length range (0 - 1023). */
70  uchar pix :2; /**< Pixel */
71  uchar lenHigh :2; /**< length = lenHigh * 256 + lenLow */
72 }; /* OpRecRunLen10Bit */
73 
74 
75 
77 {
78  uchar lenLow :8; /**< High bits of run-length */
79 }; /* OpRecRunLen10Bit_2 */
80 
81 
82 
84 {
85  uchar opCode :4; /**< 10 = One raw pixel */
86  uchar pix1 :2;
88 }; /* OpRecRawPixelOne */
89 
90 
91 
93 {
94  uchar opCode :4; /**< 11 = Two raw pixel */
95  uchar pix1 :2;
96  uchar pix2 :2;
97 }; /* OpRecRawPixelsTwo */
98 
99 
100 
102 {
103  uchar opCode :4; /**< 12 = Raw Pixels, Length 3 thru 18 */
104  uchar len :4; /**< 0 = 3, 1 = 4,,,, 15 = 18 */
105 }; /* OpRecRawPixelsVarLen4Bit */
106 
107 
108 
110 {
111  uchar opCode :4; /**< 13 = Raw Pixels, Length 0 thru 4095 */
112  uchar lenHigh :4; /**< RawStrLen = lenHigh * 256 + lenLow */
113 }; /* OpRecRawPixelsVarLen12Bit */
114 
115 
116 
118 {
119  uchar lenLow :8; /**< RawStrLen = lenHigh * 256 + lenLow */
120 }; /* OpRecRawPixelsVarLen12Bit_2 */
121 
122 
123 
124 
126 {
131 }; /* RawPixelRec */
132 
133 
134 
135 
137 {
151 }; /* OpRec */
152 
153 
154 #pragma pack(pop)
155 //#pragma pack(show)
156 
157 
158 uchar* ScannerFile2BitEncoded::convTable2BitTo8Bit = NULL;
159 uchar* ScannerFile2BitEncoded::convTable8BitTo2Bit = NULL;
160 uchar* ScannerFile2BitEncoded::compensationTable = NULL;
161 
162 
164  RunLog& _log
165  ):
166  ScannerFile (_fileName, _log),
167  rawPixelRecBuffer (NULL),
168  rawPixelRecBufferLen (0),
169  rawPixelRecBufferSize (0),
170  encodedBuff (NULL),
171  encodedBuffLen (0),
172  encodedBuffNext (NULL),
173  encodedBuffSize (0),
174  rawStr (NULL),
175  rawStrLen (0),
176  rawStrSize (0),
177  runLen (0),
178  runLenChar (0),
179  curCompStatus (csNull)
180 {
181  BuildConversionTables ();
182 }
183 
184 
185 
186 
187 
189  kkuint32 _pixelsPerScanLine,
190  kkuint32 _frameHeight,
191  RunLog& _log
192  ):
193  ScannerFile (_fileName, _pixelsPerScanLine, _frameHeight, _log),
194  rawPixelRecBuffer (NULL),
195  rawPixelRecBufferLen (0),
196  rawPixelRecBufferSize (0),
197  encodedBuff (NULL),
198  encodedBuffLen (0),
199  encodedBuffNext (NULL),
200  encodedBuffSize (0),
201  rawStr (NULL),
202  rawStrLen (0),
203  rawStrSize (0),
204  runLen (0),
205  runLenChar (0),
206  curCompStatus (csNull)
207 {
208  BuildConversionTables ();
209  AllocateEncodedBuff ();
210  AllocateRawStr (_pixelsPerScanLine + 100);
211 }
212 
213 
214 
216 {
217  if (opened)
218  Close ();
219 
220  delete encodedBuff; encodedBuff = NULL;
221  delete rawPixelRecBuffer; rawPixelRecBuffer = NULL;
222  delete rawStr; rawStr = NULL;
223 }
224 
225 
226 void ScannerFile2BitEncoded::AllocateRawPixelRecBuffer (kkuint32 size)
227 {
228  delete rawPixelRecBuffer;
229  rawPixelRecBuffer = new RawPixelRec[size];
230  rawPixelRecBufferSize = size;
231  rawPixelRecBufferLen = 0;
232 } /* AllocateRawPixelRecBuffer */
233 
234 
235 
236 void ScannerFile2BitEncoded::AllocateRawStr (kkuint16 size)
237 {
238  if (rawStr)
239  {
240  uchar* newRawStr = new uchar[size];
241  kkuint16 bytesToCopy = Min (rawStrLen, size);
242  memcpy (newRawStr, rawStr, bytesToCopy);
243  delete rawStr;
244  rawStr = newRawStr;
245  rawStrLen = bytesToCopy;
246  rawStrSize = size;
247  }
248  else
249  {
250  rawStrSize = size;
251  rawStrLen = 0;
252  rawStr = new uchar[rawStrSize];
253  }
254 } /* AllocateRawStr */
255 
256 
257 
258 
259 void ScannerFile2BitEncoded::AllocateEncodedBuff ()
260 {
261  delete encodedBuff; encodedBuff = NULL;
262 
263  kkint32 frameWidth = Max ((kkuint32)2048, 2 * PixelsPerScanLine ());
264 
265  encodedBuffSize = (int)(1.2f * (float)frameWidth);
266 
267  encodedBuff = new OpRec[encodedBuffSize];
268  encodedBuffNext = encodedBuff;
269  encodedBuffLen = 0;
270 } /* AllocateEncodedBuff */
271 
272 
273 
274 void ScannerFile2BitEncoded::ReSizeEncodedBuff (kkuint32 newSize)
275 {
276  OpRecPtr newEncodedBuff = new OpRec[newSize];
277 
278  kkuint32 recsToMove = Min (newSize, encodedBuffLen);
279 
280  memcpy (newEncodedBuff, encodedBuff, recsToMove * sizeof (OpRec));
281  encodedBuffNext = newEncodedBuff + (encodedBuffNext - encodedBuff);
282 
283  delete encodedBuff;
284  encodedBuff = newEncodedBuff;
285  encodedBuffSize = newSize;
286 } /* ReSizeEncodedBuff */
287 
288 
289 
290 void ScannerFile2BitEncoded::BuildConversionTables ()
291 {
293 
294  if (!convTable2BitTo8Bit)
295  {
296  kkint32 x = 0;
297 
298  convTable2BitTo8Bit = new uchar[4];
299  convTable2BitTo8Bit[0] = 0;
300  convTable2BitTo8Bit[1] = 85;
301  convTable2BitTo8Bit[2] = 170;
302  convTable2BitTo8Bit[3] = 255;
303 
304  convTable8BitTo2Bit = new uchar[256];
305  for (x = 0; x <= 31; ++x)
306  convTable8BitTo2Bit[x] = 0;
307 
308  for (x = 32; x <= 105; ++x)
309  convTable8BitTo2Bit[x] = 1;
310 
311  for (x = 106; x <= 179; ++x)
312  convTable8BitTo2Bit[x] = 2;
313 
314  for (x = 180; x <= 255; ++x)
315  convTable8BitTo2Bit[x] = 3;
316 
317  compensationTable = new uchar[256];
318  for (kkint32 pv = 0; pv < 256; ++pv)
319  {
320  uchar encodedValue = convTable8BitTo2Bit[pv];
321  compensationTable[pv] = convTable2BitTo8Bit[encodedValue];
322  }
323  atexit (ScannerFile2BitEncoded::ExitCleanUp);
324  }
325 
327 } /* BuildConversionTables */
328 
329 
330 
332 {
334  if (!compensationTable)
335  BuildConversionTables ();
337  return compensationTable;
338 } /* CompensationTable */
339 
340 
341 
342 
343 
344 void ScannerFile2BitEncoded::ExitCleanUp ()
345 {
347  delete convTable2BitTo8Bit; convTable2BitTo8Bit = NULL;
348  delete convTable8BitTo2Bit; convTable8BitTo2Bit = NULL;
349  delete compensationTable; compensationTable = NULL;
351 }
352 
353 
354 
355 
356 void ScannerFile2BitEncoded::ScanRate (float _scanRate)
357 {
358  ScannerFile::ScanRate (_scanRate);
359 }
360 
361 
362 
363 /*****************************************************************************/
364 /* Following routines are used for READING Scanner Files. */
365 /*****************************************************************************/
366 
367 
369 {
370  frameBufferLen = 0;
372  if (feof (file) != 0)
373  {
374  memset (frameBuffer, 0, frameBufferSize);
375  return 0;
376  }
377 
379  kkuint32 numScanLinesReadThisFrameBuffer = 0;
380  uchar* buffPtr = frameBuffer;
381  while ((feof (file) == 0) && (numScanLinesReadThisFrameBuffer < frameHeight))
382  {
383  GetNextScanLine (buffPtr, pixelsPerScanLine);
384  frameBufferLen += pixelsPerScanLine;
385  buffPtr += pixelsPerScanLine;
386  ++numScanLinesReadThisFrameBuffer;
387  }
388 
391  return numScanLinesReadThisFrameBuffer;
392 } /* ReadBufferFrame */
393 
394 
395 
397 {
398  uchar* scanLine = new uchar[pixelsPerScanLine];
399 
400  kkuint32 numScanLinesReadThisFrameBuffer = 0;
401  while ((feof (file) == 0) && (numScanLinesReadThisFrameBuffer < frameHeight))
402  {
403  GetNextScanLine (scanLine, pixelsPerScanLine);
404  ++numScanLinesReadThisFrameBuffer;
405  }
406 
407  if (feof (file) != 0)
408  return -1;
409  else
410  return osFTELL (file);
411 } /* SkipToNextFrame */
412 
413 
414 
415 void ScannerFile2BitEncoded::ProcessTextBlock (const OpRec& rec)
416 {
417  OpRec rec2;
418 
419  kkuint32 recsRead = fread (&rec2, sizeof (rec2), 1, file);
420  if (recsRead < 1)
421  {
422  eof = true;
423  return;
424  }
425 
426  kkuint32 numTextBytes = 256 * rec.textBlock.lenHigh + rec2.textBlock_2.lenLow;
427  char* textMsgPtr = new char[numTextBytes];
428  kkuint32 textMsgLen = numTextBytes;
429 
430  recsRead = fread (textMsgPtr, 1, numTextBytes, file);
431  if (recsRead < numTextBytes)
432  eof = true;
433  textMsgPtr[recsRead] = 0;
434  ReportTextMsg (textMsgPtr, textMsgLen);
435  delete[] textMsgPtr; textMsgPtr = NULL;
436  textMsgLen = 0;
437 } /* ProcessTextBlock */
438 
439 
440 
441 void ScannerFile2BitEncoded::ProcessRawPixelRecs (kkuint16 numRawPixels,
442  uchar* lineBuff,
443  kkuint32 lineBuffSize,
444  kkuint32& bufferLineLen
445  )
446 {
447  kkuint32 numRawPixelRecsToRead = numRawPixels / 4;
448  kkuint32 numFullOccupiedRawRecs = numRawPixelRecsToRead;
449  kkuint32 numOverFlowPixels = numRawPixels % 4;
450  if (numOverFlowPixels > 0)
451  ++numRawPixelRecsToRead;
452 
453  if (numRawPixelRecsToRead > rawPixelRecBufferSize)
454  AllocateRawPixelRecBuffer (numRawPixelRecsToRead + 30);
455 
456  size_t recsRead = fread (rawPixelRecBuffer, sizeof (RawPixelRec), numRawPixelRecsToRead, file);
457  if (recsRead < numRawPixelRecsToRead)
458  {
459  eof = true;
460  return;
461  }
462 
463  kkuint32 newBufferLineLen = bufferLineLen + numRawPixels;
464  if (newBufferLineLen > lineBuffSize)
465  {
466  cerr << "ScannerFile2BitEncoded::ProcessRawPixelRecs *** Line Length Exceeded ***" << endl;
467  return;
468  }
469 
470  kkuint32 x = 0;
471  for (x = 0; x < numFullOccupiedRawRecs; ++x)
472  {
473  lineBuff[bufferLineLen] = convTable2BitTo8Bit[rawPixelRecBuffer[x].pix0]; ++bufferLineLen;
474  lineBuff[bufferLineLen] = convTable2BitTo8Bit[rawPixelRecBuffer[x].pix1]; ++bufferLineLen;
475  lineBuff[bufferLineLen] = convTable2BitTo8Bit[rawPixelRecBuffer[x].pix2]; ++bufferLineLen;
476  lineBuff[bufferLineLen] = convTable2BitTo8Bit[rawPixelRecBuffer[x].pix3]; ++bufferLineLen;
477  }
478 
479  if (numOverFlowPixels > 0)
480  {
481  lineBuff[bufferLineLen] = convTable2BitTo8Bit[rawPixelRecBuffer[x].pix0]; ++bufferLineLen;
482  }
483 
484  if (numOverFlowPixels > 1)
485  {
486  lineBuff[bufferLineLen] = convTable2BitTo8Bit[rawPixelRecBuffer[x].pix0]; ++bufferLineLen;
487  }
488 
489  if (numOverFlowPixels > 2)
490  {
491  lineBuff[bufferLineLen] = convTable2BitTo8Bit[rawPixelRecBuffer[x].pix0]; ++bufferLineLen;
492  }
493 
494  return;
495 } /* ProcessRawPixelRecs */
496 
497 
498 
499 void ScannerFile2BitEncoded::GetNextScanLine (uchar* lineBuff,
500  kkuint32 lineBuffSize
501  )
502 {
503  bool eol = false;
504  uchar opCode = 0;
505  OpRec rec;
506  OpRec rec2;
507  kkuint32 recsRead = 0;
508 
509  kkuint32 bufferLineLen = 0;
510 
511  do
512  {
513  recsRead = fread (&rec, sizeof (rec), 1, file);
514  if (recsRead == 0)
515  {
516  break;
517  }
518 
519  opCode = rec.textBlock.opCode;
520 
521  if (opCode == 0)
522  eol = true;
523 
524  else if (bufferLineLen >= lineBuffSize)
525  {
526  // Something has gone wrong, we should have encountered a eol op code before this point; that is the
527  // length of the encoded line is exceeding the scan line length.
528  eol = true;
529  ungetc (rec.textChar, file);
530  break;
531  }
532 
533 
534  else if (opCode == 1)
535  ProcessTextBlock (rec);
536 
537  else if ((opCode >= 4) && (opCode <= 7))
538  {
539  // OpCode determines PixelValue OpCode(4)=PV(0), OpCode(5)=PV(1), OpCode(6)=PV(2), OpCode(7)=PV(3),
540  kkuint16 encodedChar = opCode - 4;
541  runLenChar = convTable2BitTo8Bit [encodedChar];
542  runLen = rec.runLenPVx.len + 3;
543  kkuint32 newLineSize = bufferLineLen + runLen;
544 
545  if (newLineSize > lineBuffSize)
546  {
547  cerr << "ScannerFile2BitEncoded::GetNextScanLine *** Line Length Exceeded ****" << endl;
548  }
549  else
550  {
551  memset (&(lineBuff[bufferLineLen]), runLenChar, runLen);
552  bufferLineLen = newLineSize;
553  }
554  }
555 
556  else if (opCode == 8)
557  {
558  // Variable Run Length 0 thru 1023
559  recsRead = fread (&rec2, sizeof (rec2), 1, file);
560  if (recsRead < 1)
561  {
562  eol = true;
563  }
564  else
565  {
567  kkuint32 newLineSize = bufferLineLen + runLen;
568  if (newLineSize > lineBuffSize)
569  {
570  cerr << "ScannerFile2BitEncoded::GetNextScanLine *** Line Length Exceeded ****" << endl;
571  runLen = lineBuffSize - bufferLineLen;
572  newLineSize = bufferLineLen;
573  }
574 
575  uchar runLenChar = convTable2BitTo8Bit [rec.runLen10Bit.pix];
576  memset (&(lineBuff[bufferLineLen]), runLenChar, runLen);
577  bufferLineLen = newLineSize;
578  }
579  }
580 
581 
582  else if (opCode == 10)
583  {
584  // One Raw Pixel
585  lineBuff[bufferLineLen] = convTable2BitTo8Bit [rec.rawPixelOne.pix1];
586  ++bufferLineLen;
587  }
588 
589 
590  else if (opCode == 11)
591  {
592  // Two Raw Pixels.
593  lineBuff[bufferLineLen] = convTable2BitTo8Bit [rec.rawPixelsTwo.pix1]; ++bufferLineLen;
594  lineBuff[bufferLineLen] = convTable2BitTo8Bit [rec.rawPixelsTwo.pix2]; ++bufferLineLen;
595  }
596 
597 
598  else if (opCode == 12)
599  {
600  // Variable Length Raw Pixels where string length = (3 thru 18)
601  kkuint32 numRawPixels = rec.rawPixelsVarLen4Bit.len + 3;
602  ProcessRawPixelRecs (numRawPixels, lineBuff, lineBuffSize, bufferLineLen);
603  }
604 
605 
606  else if (opCode == 13)
607  {
608  // Variable Length Raw Pixels where string length = (0 thru 4095)
609 
610  // Variable Run Length 0 thru 1023
611  recsRead = fread (&rec2, sizeof (rec2), 1, file);
612  if (recsRead < 1)
613  {
614  eol = true;
615  }
616  else
617  {
619  ProcessRawPixelRecs (numRawPixels, lineBuff, lineBuffSize, bufferLineLen);
620  }
621  }
622 
623  else
624  {
625  cerr << "ScannerFile2BitEncoded::GetNextScanLine *** Invalid OpCode[" << opCode<< "] Encountered." << endl;
626  }
627 
628  } while (!eol);
629 
630 } /* GetNextScanLine */
631 
632 
633 
634 /*****************************************************************************/
635 /* Following routines are used for writing Scanner Files. */
636 /*****************************************************************************/
637 
639 {
641  //nextScanLineOffset = osFTELL (file);
642 
643  kkuint32 frameRow = 0;
644  uchar* framePtr = frameBuffer;
645  while (frameRow < frameBufferNextLine)
646  {
647  WriteNextScanLine (framePtr, pixelsPerScanLine);
648  framePtr += pixelsPerScanLine;
649  ++frameRow;
650  }
651 
654 } /* WriteBufferFrame*/
655 
656 
657 
658 void ScannerFile2BitEncoded::AddCurRunLenToOutputBuffer ()
659 {
660  OpRec rec;
661 
662  while (runLen > 0)
663  {
664  if (runLen == 1)
665  {
666  curCompStatus = csRaw;
667  rawStr[rawStrLen] = runLenChar;
668  ++rawStrLen;
669  runLen = 0;
670  }
671 
672  else if (runLen < 2)
673  {
674  rec.rawPixelOne.opCode = 10;
675  rec.rawPixelOne.pix1 = runLenChar;
676  *encodedBuffNext = rec;
677  ++encodedBuffNext;
678  runLen = 0;
679  }
680 
681  else if (runLen < 3)
682  {
683  rec.rawPixelsTwo.opCode = 11;
684  rec.rawPixelsTwo.pix1 = runLenChar;
685  rec.rawPixelsTwo.pix2 = runLenChar;
686  *encodedBuffNext = rec;
687  ++encodedBuffNext;
688  runLen = 0;
689  }
690 
691  else if (runLen < 19)
692  {
693  rec.runLenPVx.opCode = 4 + runLenChar;
694  rec.runLenPVx.len = runLen - 3;
695  *encodedBuffNext = rec;
696  ++encodedBuffNext;
697  runLen = 0;
698  }
699 
700  else
701  {
702  kkint32 runLenThisLoop = Min ((kkint32)1023, runLen);
703 
704  rec.runLen10Bit.opCode = 8;
705  rec.runLen10Bit.pix = runLenChar;
706  rec.runLen10Bit.lenHigh = runLenThisLoop / 256;
707  *encodedBuffNext = rec;
708  ++encodedBuffNext;
709 
710  rec.runLen10Bit_2.lenLow = runLenThisLoop % 256;
711  *encodedBuffNext = rec;
712  ++encodedBuffNext;
713  runLen = runLen - runLenThisLoop;
714  }
715  }
716 } /* AddCurRunLenToOutputBuffer */
717 
718 
719 
720 
721 void ScannerFile2BitEncoded::AddRawStrPixelsToEncodedBuffer (kkuint16& nextCp,
722  kkuint16 len
723  )
724 {
725  OpRec rec;
726 
727  while (len > 0)
728  {
729  if (len > 3)
730  {
731  rec.rawPixelRec.pix0 = rawStr[nextCp]; ++nextCp;
732  rec.rawPixelRec.pix1 = rawStr[nextCp]; ++nextCp;
733  rec.rawPixelRec.pix2 = rawStr[nextCp]; ++nextCp;
734  rec.rawPixelRec.pix3 = rawStr[nextCp]; ++nextCp;
735  len -= 4;
736  }
737  else
738  {
739  rec.rawPixelRec.pix0 = rawStr[nextCp]; ++rawStr;
740  if (len > 1) {rec.rawPixelRec.pix1 = rawStr[nextCp]; ++nextCp;}
741  if (len > 2) {rec.rawPixelRec.pix2 = rawStr[nextCp]; ++nextCp;}
742  if (len > 3) {rec.rawPixelRec.pix3 = rawStr[nextCp]; ++nextCp;}
743  len = 0;
744  }
745 
746  // KAK 2014-May-06 Was missing the next two lines; I need to verify if this is correct.
747  *encodedBuffNext = rec;
748  ++encodedBuffNext;
749  }
750 } /* AddRawStrPixelsToEncodedBuffer */
751 
752 
753 
754 void ScannerFile2BitEncoded::AddCurRawStrToOutputBuffer ()
755 {
756  OpRec rec;
757 
758  kkuint16 nextCp = 0;
759  kkuint16 len = rawStrLen;
760 
761  while (len > 0)
762  {
763  if (len < 2)
764  {
765  rec.rawPixelOne.opCode = 10;
766  rec.rawPixelOne.pix1 = rawStr[nextCp];
767  *encodedBuffNext = rec;
768  ++encodedBuffNext;
769  len = 0;
770  ++nextCp;
771  }
772 
773  else if (len < 3)
774  {
775  rec.rawPixelsTwo.opCode = 11;
776  rec.rawPixelsTwo.pix1 = rawStr[nextCp]; ++nextCp;
777  rec.rawPixelsTwo.pix2 = rawStr[nextCp]; ++nextCp;
778  *encodedBuffNext = rec;
779  ++encodedBuffNext;
780  len = 0;
781  }
782 
783 
784  else if (len < 19)
785  {
787  rec.rawPixelsVarLen4Bit.len = len - 3;
788  *encodedBuffNext = rec;
789  ++encodedBuffNext;
790  AddRawStrPixelsToEncodedBuffer (nextCp, len);
791  len = 0;
792  }
793 
794  else
795  {
796  kkuint16 rawPixelsThisLoop = Min (len, (kkuint16)4095);
797  kkuint16 lenHigh = rawPixelsThisLoop / 256;
798  kkuint16 lenLow = rawPixelsThisLoop % 256;
799 
801  rec.rawPixelsVarLen12Bit.lenHigh = (uchar)lenHigh;
802  *encodedBuffNext = rec;
803  ++encodedBuffNext;
804 
806  *encodedBuffNext = rec;
807  ++encodedBuffNext;
808 
809  AddRawStrPixelsToEncodedBuffer (nextCp, rawPixelsThisLoop);
810  len = len - rawPixelsThisLoop;
811  }
812  }
813 
814  rawStrLen = 0;
815 } /* AddCurRawStrToOutputBuffer */
816 
817 
818 
819 void ScannerFile2BitEncoded::WriteNextScanLine (const uchar* buffer,
820  kkuint32 bufferLen
821  )
822 {
823  encodedBuffNext = encodedBuff;
824  encodedBuffLen = 0;
825  kkuint32 encodedBuffLeft = encodedBuffSize - encodedBuffLen;
826  if (bufferLen > (encodedBuffLeft - 10))
827  {
828  kkuint32 newEncodedBuffSise = bufferLen + 10;
829  ReSizeEncodedBuff (newEncodedBuffSise);
830  }
831 
832  curCompStatus = csNull;
833 
834  for (kkuint16 x = 0; x < bufferLen; ++x)
835  {
836  uchar nextCh = convTable8BitTo2Bit[buffer[x]];
837 
838  switch (curCompStatus)
839  {
840  case csNull:
841  {
842  runLen = 1;
843  runLenChar = nextCh;
844  curCompStatus = csRunLen;
845  }
846  break;
847 
848  case csRunLen:
849  {
850  if (nextCh == runLenChar)
851  {
852  ++runLen;
853  }
854  else if (runLen == 1)
855  {
856  rawStr[0] = runLenChar;
857  rawStr[1] = nextCh;
858  rawStrLen = 2;
859  runLen = 0;
860  curCompStatus = csRaw;
861  }
862  else
863  {
864  AddCurRunLenToOutputBuffer ();
865  runLenChar = nextCh;
866  runLen = 1;
867  }
868  }
869  break;
870 
871  case csRaw:
872  {
873  if (rawStrLen == 0)
874  {
875  rawStr[rawStrLen] = nextCh;
876  ++rawStrLen;
877  }
878 
879  if (rawStr[rawStrLen - 1] != nextCh)
880  {
881  rawStr[rawStrLen] = nextCh;
882  ++rawStrLen;
883  }
884  else
885  {
886  --rawStrLen;
887  if (rawStrLen > 0)
888  AddCurRawStrToOutputBuffer ();
889 
890  runLen = 2;
891  runLenChar = nextCh;
892  curCompStatus = csRunLen;
893  }
894  }
895  break;
896  }
897  }
898 
899  if (curCompStatus == csRunLen)
900  AddCurRunLenToOutputBuffer ();
901 
902  if (curCompStatus == csRaw)
903  AddCurRawStrToOutputBuffer ();
904 
905  OpRec rec;
906  rec.endOfScanLine.opCode = 0;
907  rec.endOfScanLine.filler = 0;
908  *encodedBuffNext = rec;
909  ++encodedBuffNext;
910 
911  encodedBuffLen = encodedBuffNext - encodedBuff;
912  fwrite (encodedBuff, sizeof (OpRec), encodedBuffLen, file);
913 
914 
915  /**@todo Write out OutputBuffer; */
916 
917 } /* WriteNextScanLine */
918 
919 
920 
921 
922 
924  kkuint32 txtBlockLen
925  )
926 {
927  kkint32 charsLeft = txtBlockLen;
928  const uchar* txtBlockPtr = txtBlock;
929  while (charsLeft > 0)
930  {
931  kkint32 charsToWrite = Min ((kkint32)2048, charsLeft);
932  OpRec rec;
933 
934  rec.textBlock.opCode = 1;
935  rec.textBlock.endOfText = (charsToWrite < charsLeft) ? 0 : 1;
936  rec.textBlock.lenHigh = charsToWrite / 256;
937  *encodedBuffNext = rec;
938  ++encodedBuffNext;
939 
940  encodedBuffNext->textBlock_2.lenLow = charsToWrite % 256;
941  ++encodedBuffNext;
942 
943  memcpy (encodedBuffNext, txtBlockPtr, charsToWrite);
944 
945  charsLeft -= charsToWrite;
946  txtBlockPtr += charsToWrite;
947  encodedBuffNext += charsToWrite;
948  }
949 
950  //nextScanLineOffset = osFTELL (file);
952 } /* WriteTextBlock */
kkint64 frameBufferFileOffsetLast
Definition: ScannerFile.h:427
kkint64 frameBufferFileOffsetNext
Definition: ScannerFile.h:429
virtual kkint64 SkipToNextFrame()
Skip to start of next frame returning back byte offset of that frame.
kkint64 fileSizeInBytes
Definition: ScannerFile.h:396
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint64 osFTELL(FILE *f)
Calls the appropriate 64 bit function for operating system.
Definition: OSservices.cpp:93
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
Implements a 2 bit Encoded format.
OpRecRawPixelsVarLen12Bit_2 rawPixelsVarLen12Bit_2
kkuint32 frameBufferLen
Definition: ScannerFile.h:435
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.
unsigned short ushort
Unsigned short.
Definition: KKBaseTypes.h:79
static const uchar * CompensationTable()
kkuint32 frameBufferNextLine
Definition: ScannerFile.h:441
ScannerFile2BitEncoded(const KKStr &_fileName, kkuint32 _pixelsPerScanLine, kkuint32 _frameHeight, RunLog &_log)
ScannerFile2BitEncoded(const KKStr &_fileName, RunLog &_log)
kkuint32 pixelsPerScanLine
Definition: ScannerFile.h:407
Used for logging messages.
Definition: RunLog.h:49
ScannerFile(const KKStr &_fileName, kkuint32 _pixelsPerScanLine, kkuint32 _frameHeight, RunLog &_log)
Maintains one instance of a GoalKeeper object that can be used anywhere in the application.
ScannerFile(const KKStr &_fileName, RunLog &_log)
Definition: ScannerFile.cpp:40
virtual void ScanRate(float _scanRate)
virtual void WriteBufferFrame()
Write the contents of &#39;frameBuffer&#39; to he end of the scanner file.