27 using namespace KKLSC;
167 rawPixelRecBuffer (NULL),
168 rawPixelRecBufferLen (0),
169 rawPixelRecBufferSize (0),
172 encodedBuffNext (NULL),
179 curCompStatus (csNull)
181 BuildConversionTables ();
194 rawPixelRecBuffer (NULL),
195 rawPixelRecBufferLen (0),
196 rawPixelRecBufferSize (0),
199 encodedBuffNext (NULL),
206 curCompStatus (csNull)
208 BuildConversionTables ();
209 AllocateEncodedBuff ();
210 AllocateRawStr (_pixelsPerScanLine + 100);
220 delete encodedBuff; encodedBuff = NULL;
221 delete rawPixelRecBuffer; rawPixelRecBuffer = NULL;
222 delete rawStr; rawStr = NULL;
228 delete rawPixelRecBuffer;
230 rawPixelRecBufferSize = size;
231 rawPixelRecBufferLen = 0;
241 kkuint16 bytesToCopy = Min (rawStrLen, size);
242 memcpy (newRawStr, rawStr, bytesToCopy);
245 rawStrLen = bytesToCopy;
252 rawStr =
new uchar[rawStrSize];
261 delete encodedBuff; encodedBuff = NULL;
263 kkint32 frameWidth = Max ((kkuint32)2048, 2 * PixelsPerScanLine ());
265 encodedBuffSize = (
int)(1.2f * (
float)frameWidth);
267 encodedBuff =
new OpRec[encodedBuffSize];
268 encodedBuffNext = encodedBuff;
276 OpRecPtr newEncodedBuff =
new OpRec[newSize];
278 kkuint32 recsToMove = Min (newSize, encodedBuffLen);
280 memcpy (newEncodedBuff, encodedBuff, recsToMove *
sizeof (OpRec));
281 encodedBuffNext = newEncodedBuff + (encodedBuffNext - encodedBuff);
284 encodedBuff = newEncodedBuff;
285 encodedBuffSize = newSize;
294 if (!convTable2BitTo8Bit)
298 convTable2BitTo8Bit =
new uchar[4];
299 convTable2BitTo8Bit[0] = 0;
300 convTable2BitTo8Bit[1] = 85;
301 convTable2BitTo8Bit[2] = 170;
302 convTable2BitTo8Bit[3] = 255;
304 convTable8BitTo2Bit =
new uchar[256];
305 for (x = 0; x <= 31; ++x)
306 convTable8BitTo2Bit[x] = 0;
308 for (x = 32; x <= 105; ++x)
309 convTable8BitTo2Bit[x] = 1;
311 for (x = 106; x <= 179; ++x)
312 convTable8BitTo2Bit[x] = 2;
314 for (x = 180; x <= 255; ++x)
315 convTable8BitTo2Bit[x] = 3;
317 compensationTable =
new uchar[256];
318 for (
kkint32 pv = 0; pv < 256; ++pv)
320 uchar encodedValue = convTable8BitTo2Bit[pv];
321 compensationTable[pv] = convTable2BitTo8Bit[encodedValue];
323 atexit (ScannerFile2BitEncoded::ExitCleanUp);
334 if (!compensationTable)
335 BuildConversionTables ();
337 return compensationTable;
347 delete convTable2BitTo8Bit; convTable2BitTo8Bit = NULL;
348 delete convTable8BitTo2Bit; convTable8BitTo2Bit = NULL;
349 delete compensationTable; compensationTable = NULL;
358 ScannerFile::ScanRate (_scanRate);
372 if (feof (file) != 0)
374 memset (frameBuffer, 0, frameBufferSize);
379 kkuint32 numScanLinesReadThisFrameBuffer = 0;
381 while ((feof (file) == 0) && (numScanLinesReadThisFrameBuffer < frameHeight))
383 GetNextScanLine (buffPtr, pixelsPerScanLine);
384 frameBufferLen += pixelsPerScanLine;
385 buffPtr += pixelsPerScanLine;
386 ++numScanLinesReadThisFrameBuffer;
391 return numScanLinesReadThisFrameBuffer;
400 kkuint32 numScanLinesReadThisFrameBuffer = 0;
401 while ((feof (file) == 0) && (numScanLinesReadThisFrameBuffer < frameHeight))
403 GetNextScanLine (scanLine, pixelsPerScanLine);
404 ++numScanLinesReadThisFrameBuffer;
407 if (feof (file) != 0)
410 return osFTELL (file);
419 kkuint32 recsRead = fread (&rec2,
sizeof (rec2), 1, file);
427 char* textMsgPtr =
new char[numTextBytes];
430 recsRead = fread (textMsgPtr, 1, numTextBytes, file);
431 if (recsRead < numTextBytes)
433 textMsgPtr[recsRead] = 0;
434 ReportTextMsg (textMsgPtr, textMsgLen);
435 delete[] textMsgPtr; textMsgPtr = NULL;
447 kkuint32 numRawPixelRecsToRead = numRawPixels / 4;
448 kkuint32 numFullOccupiedRawRecs = numRawPixelRecsToRead;
449 kkuint32 numOverFlowPixels = numRawPixels % 4;
450 if (numOverFlowPixels > 0)
451 ++numRawPixelRecsToRead;
453 if (numRawPixelRecsToRead > rawPixelRecBufferSize)
454 AllocateRawPixelRecBuffer (numRawPixelRecsToRead + 30);
456 size_t recsRead = fread (rawPixelRecBuffer,
sizeof (RawPixelRec), numRawPixelRecsToRead, file);
457 if (recsRead < numRawPixelRecsToRead)
463 kkuint32 newBufferLineLen = bufferLineLen + numRawPixels;
464 if (newBufferLineLen > lineBuffSize)
466 cerr <<
"ScannerFile2BitEncoded::ProcessRawPixelRecs *** Line Length Exceeded ***" << endl;
471 for (x = 0; x < numFullOccupiedRawRecs; ++x)
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;
479 if (numOverFlowPixels > 0)
481 lineBuff[bufferLineLen] = convTable2BitTo8Bit[rawPixelRecBuffer[x]
.pix0]; ++bufferLineLen;
484 if (numOverFlowPixels > 1)
486 lineBuff[bufferLineLen] = convTable2BitTo8Bit[rawPixelRecBuffer[x]
.pix0]; ++bufferLineLen;
489 if (numOverFlowPixels > 2)
491 lineBuff[bufferLineLen] = convTable2BitTo8Bit[rawPixelRecBuffer[x]
.pix0]; ++bufferLineLen;
513 recsRead = fread (&rec,
sizeof (rec), 1, file);
524 else if (bufferLineLen >= lineBuffSize)
529 ungetc (rec.textChar, file);
534 else if (opCode == 1)
535 ProcessTextBlock (rec);
537 else if ((opCode >= 4) && (opCode <= 7))
541 runLenChar = convTable2BitTo8Bit [encodedChar];
543 kkuint32 newLineSize = bufferLineLen + runLen;
545 if (newLineSize > lineBuffSize)
547 cerr <<
"ScannerFile2BitEncoded::GetNextScanLine *** Line Length Exceeded ****" << endl;
551 memset (&(lineBuff[bufferLineLen]), runLenChar, runLen);
552 bufferLineLen = newLineSize;
556 else if (opCode == 8)
559 recsRead = fread (&rec2,
sizeof (rec2), 1, file);
567 kkuint32 newLineSize = bufferLineLen + runLen;
568 if (newLineSize > lineBuffSize)
570 cerr <<
"ScannerFile2BitEncoded::GetNextScanLine *** Line Length Exceeded ****" << endl;
571 runLen = lineBuffSize - bufferLineLen;
572 newLineSize = bufferLineLen;
576 memset (&(lineBuff[bufferLineLen]), runLenChar, runLen);
577 bufferLineLen = newLineSize;
582 else if (opCode == 10)
590 else if (opCode == 11)
593 lineBuff[bufferLineLen] = convTable2BitTo8Bit [rec
.rawPixelsTwo.pix1]; ++bufferLineLen;
594 lineBuff[bufferLineLen] = convTable2BitTo8Bit [rec
.rawPixelsTwo.pix2]; ++bufferLineLen;
598 else if (opCode == 12)
602 ProcessRawPixelRecs (numRawPixels, lineBuff, lineBuffSize, bufferLineLen);
606 else if (opCode == 13)
611 recsRead = fread (&rec2,
sizeof (rec2), 1, file);
619 ProcessRawPixelRecs (numRawPixels, lineBuff, lineBuffSize, bufferLineLen);
625 cerr <<
"ScannerFile2BitEncoded::GetNextScanLine *** Invalid OpCode[" << opCode<<
"] Encountered." << endl;
666 curCompStatus = csRaw;
667 rawStr[rawStrLen] = runLenChar;
676 *encodedBuffNext = rec;
686 *encodedBuffNext = rec;
691 else if (runLen < 19)
695 *encodedBuffNext = rec;
702 kkint32 runLenThisLoop = Min ((kkint32)1023, runLen);
707 *encodedBuffNext = rec;
711 *encodedBuffNext = rec;
713 runLen = runLen - runLenThisLoop;
747 *encodedBuffNext = rec;
767 *encodedBuffNext = rec;
778 *encodedBuffNext = rec;
788 *encodedBuffNext = rec;
790 AddRawStrPixelsToEncodedBuffer (nextCp, len);
796 kkuint16 rawPixelsThisLoop = Min (len, (kkuint16)4095);
797 kkuint16 lenHigh = rawPixelsThisLoop / 256;
798 kkuint16 lenLow = rawPixelsThisLoop % 256;
802 *encodedBuffNext = rec;
806 *encodedBuffNext = rec;
809 AddRawStrPixelsToEncodedBuffer (nextCp, rawPixelsThisLoop);
810 len = len - rawPixelsThisLoop;
823 encodedBuffNext = encodedBuff;
825 kkuint32 encodedBuffLeft = encodedBuffSize - encodedBuffLen;
826 if (bufferLen > (encodedBuffLeft - 10))
828 kkuint32 newEncodedBuffSise = bufferLen + 10;
829 ReSizeEncodedBuff (newEncodedBuffSise);
832 curCompStatus = csNull;
834 for (
kkuint16 x = 0; x < bufferLen; ++x)
836 uchar nextCh = convTable8BitTo2Bit[buffer[x]];
838 switch (curCompStatus)
844 curCompStatus = csRunLen;
850 if (nextCh == runLenChar)
854 else if (runLen == 1)
856 rawStr[0] = runLenChar;
860 curCompStatus = csRaw;
864 AddCurRunLenToOutputBuffer ();
875 rawStr[rawStrLen] = nextCh;
879 if (rawStr[rawStrLen - 1] != nextCh)
881 rawStr[rawStrLen] = nextCh;
888 AddCurRawStrToOutputBuffer ();
892 curCompStatus = csRunLen;
899 if (curCompStatus == csRunLen)
900 AddCurRunLenToOutputBuffer ();
902 if (curCompStatus == csRaw)
903 AddCurRawStrToOutputBuffer ();
908 *encodedBuffNext = rec;
911 encodedBuffLen = encodedBuffNext - encodedBuff;
912 fwrite (encodedBuff,
sizeof (OpRec), encodedBuffLen, file);
927 kkint32 charsLeft = txtBlockLen;
928 const uchar* txtBlockPtr = txtBlock;
929 while (charsLeft > 0)
931 kkint32 charsToWrite = Min ((kkint32)2048, charsLeft);
937 *encodedBuffNext = rec;
943 memcpy (encodedBuffNext, txtBlockPtr, charsToWrite);
945 charsLeft -= charsToWrite;
946 txtBlockPtr += charsToWrite;
947 encodedBuffNext += charsToWrite;
kkint64 frameBufferFileOffsetLast
OpRecRawPixelsTwo rawPixelsTwo
kkint64 frameBufferFileOffsetNext
OpRecRunLen10Bit_2 runLen10Bit_2
virtual kkint64 SkipToNextFrame()
Skip to start of next frame returning back byte offset of that frame.
kkint64 osFTELL(FILE *f)
Calls the appropriate 64 bit function for operating system.
virtual ~ScannerFile2BitEncoded()
OpRecRunLen10Bit runLen10Bit
OpRecRawPixelsVarLen4Bit rawPixelsVarLen4Bit
unsigned __int16 kkuint16
16 bit unsigned integer.
Implements a 2 bit Encoded format.
OpRecRawPixelsVarLen12Bit_2 rawPixelsVarLen12Bit_2
virtual void WriteTextBlock(const uchar *txtBlock, kkuint32 txtBlockLen)
unsigned __int32 kkuint32
unsigned char uchar
Unsigned character.
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of 'std::string' strings.
Contains Classes that are specific to Cameras physical characteristics.
kkuint32 ReadBufferFrame()
Read on one Scanner File Frame.
unsigned short ushort
Unsigned short.
static const uchar * CompensationTable()
OpRecRawPixelOne rawPixelOne
kkuint32 frameBufferNextLine
ScannerFile2BitEncoded(const KKStr &_fileName, kkuint32 _pixelsPerScanLine, kkuint32 _frameHeight, RunLog &_log)
OpRecTextBlock_2 textBlock_2
ScannerFile2BitEncoded(const KKStr &_fileName, RunLog &_log)
kkuint32 pixelsPerScanLine
Used for logging messages.
OpRecEndOfScanLine endOfScanLine
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)
virtual void ScanRate(float _scanRate)
virtual void WriteBufferFrame()
Write the contents of 'frameBuffer' to he end of the scanner file.
OpRecRawPixelsVarLen12Bit rawPixelsVarLen12Bit