27 using namespace KKLSC;
196 rawPixelRecBuffer (NULL),
197 rawPixelRecBufferSize (0),
198 rawPixelRecBufferLen (0),
202 encodedBuffNext (NULL),
210 curCompStatus (csNull)
213 BuildConversionTables ();
225 rawPixelRecBuffer (NULL),
226 rawPixelRecBufferSize (0),
227 rawPixelRecBufferLen (0),
230 encodedBuffNext (NULL),
237 curCompStatus (csNull)
239 BuildConversionTables ();
240 AllocateEncodedBuff ();
241 AllocateRawStr (_pixelsPerScanLine + 100);
287 delete encodedBuff; encodedBuff = NULL;
288 delete rawPixelRecBuffer; rawPixelRecBuffer = NULL;
289 delete rawStr; rawStr = NULL;
297 delete rawPixelRecBuffer;
299 rawPixelRecBufferSize = size;
300 rawPixelRecBufferLen = 0;
310 kkuint16 bytesToCopy = Min (rawStrLen, size);
311 memcpy (newRawStr, rawStr, bytesToCopy);
314 rawStrLen = bytesToCopy;
321 rawStr =
new uchar[rawStrSize];
330 delete encodedBuff; encodedBuff = NULL;
332 kkint32 frameWidth = Max ((kkuint32)2048, 4 * PixelsPerScanLine ());
334 encodedBuffSize = (
int)(1.2f * (
float)frameWidth);
336 encodedBuff =
new OpRec[encodedBuffSize];
337 encodedBuffNext = encodedBuff;
346 OpRecPtr newEncodedBuff =
new OpRec[newSize];
348 kkuint32 recsToMove = Min (newSize, encodedBuffLen);
350 memcpy (newEncodedBuff, encodedBuff, recsToMove *
sizeof (OpRec));
351 encodedBuffNext = newEncodedBuff + (encodedBuffNext - encodedBuff);
354 encodedBuff = newEncodedBuff;
355 encodedBuffSize = newSize;
363 if (!convTable4BitTo8Bit)
368 convTable4BitTo8Bit =
new uchar[16];
369 convTable8BitTo4Bit =
new uchar[256];
370 for (x = 0; x < 16; ++x)
373 kkint32 next8Bit = (x + 1) * inc;
377 convTable4BitTo8Bit[x] = (
uchar)fourBitTo8BitNum;
378 for (y = this8Bit; y < next8Bit; ++y)
379 convTable8BitTo4Bit[y] = x;
382 compensationTable =
new uchar[256];
383 for (
kkint16 pv = 0; pv < 256; ++pv)
385 uchar encodedValue = convTable8BitTo4Bit[pv];
386 compensationTable[pv] = convTable4BitTo8Bit[encodedValue];
388 atexit (ScannerFile4BitEncoded::ExitCleanUp);
399 if (!compensationTable)
400 BuildConversionTables ();
402 return compensationTable;
412 delete convTable4BitTo8Bit; convTable4BitTo8Bit = NULL;
413 delete convTable8BitTo4Bit; convTable8BitTo4Bit = NULL;
414 delete compensationTable; compensationTable = NULL;
423 ScannerFile::ScanRate (_scanRate);
437 if (feof (file) != 0)
439 memset (frameBuffer, 0, frameBufferSize);
444 kkuint32 numScanLinesReadThisFrameBuffer = 0;
446 while ((feof (file) == 0) && (numScanLinesReadThisFrameBuffer < frameHeight))
448 GetNextScanLine (buffPtr, pixelsPerScanLine);
449 frameBufferLen += pixelsPerScanLine;
450 buffPtr += pixelsPerScanLine;
451 ++numScanLinesReadThisFrameBuffer;
456 return numScanLinesReadThisFrameBuffer;
465 kkuint32 numScanLinesReadThisFrameBuffer = 0;
466 while ((feof (file) == 0) && (numScanLinesReadThisFrameBuffer < frameHeight))
468 GetNextScanLine (scanLine, pixelsPerScanLine);
469 ++numScanLinesReadThisFrameBuffer;
475 if (feof (file) != 0)
478 return osFTELL (file);
487 kkuint32 recsRead = fread (&rec2,
sizeof (rec2), 1, file);
495 char* textMsgPtr =
new char[numTextBytes + 1];
498 recsRead = fread (textMsgPtr, 1, numTextBytes, file);
499 if (recsRead < numTextBytes)
503 textMsgPtr[recsRead] = 0;
504 ReportTextMsg (textMsgPtr, textMsgLen);
506 delete[] textMsgPtr; textMsgPtr = NULL;
518 kkuint32 recsRead = fread (&rec2,
sizeof (rec2), 1, file);
523 recsRead = fread (&rec3,
sizeof (rec3), 1, file);
527 ReportInstrumentDataWord (idNum, rec2.scanLineNum, rec3.word);
541 if (numRawPixelRecs > rawPixelRecBufferSize)
542 AllocateRawPixelRecBuffer (numRawPixelRecs + 30);
544 size_t recsRead = fread (rawPixelRecBuffer,
sizeof (RawPixelRec), numRawPixelRecs, file);
545 if (recsRead < numRawPixelRecs)
551 for (
kkuint32 x = 0; x < numRawPixelRecs; ++x)
553 if (bufferLineLen < lineBuffSize) {lineBuff[bufferLineLen] = convTable4BitTo8Bit[rawPixelRecBuffer[x]
.pix0]; ++bufferLineLen;}
554 if (bufferLineLen < lineBuffSize) {lineBuff[bufferLineLen] = convTable4BitTo8Bit[rawPixelRecBuffer[x]
.pix1]; ++bufferLineLen;}
576 recsRead = fread (&rec,
sizeof (rec), 1, file);
587 else if (bufferLineLen >= lineBuffSize)
592 ungetc (rec.textChar, file);
597 else if (opCode == 1)
598 ProcessTextBlock (rec);
600 else if (opCode == 2)
601 ProcessInstrumentDataWord (rec);
603 else if ((opCode >= 4) && (opCode <= 9))
608 kkuint32 newLineSize = bufferLineLen + runLen;
610 if (newLineSize > lineBuffSize)
612 cerr <<
"ScannerFile4BitEncoded::GetNextScanLine ***ERROR*** Exceeding 'bufferLineLen'; ScanLine[" << nextScanLine <<
"]." << endl;
613 cerr <<
" newLineSize: " << newLineSize << endl;
617 memset (&(lineBuff[bufferLineLen]), runLenChar, runLen);
618 bufferLineLen = newLineSize;
622 else if (opCode == 10)
624 recsRead = fread (&rec2,
sizeof (rec2), 1, file);
630 kkuint32 newLineSize = bufferLineLen + runLen;
631 if (newLineSize > lineBuffSize)
633 cerr <<
"ScannerFile4BitEncoded::GetNextScanLine ***ERROR*** Exceeding 'bufferLineLen'; ScanLine[" << nextScanLine <<
"]." << endl;
638 memset (&(lineBuff[bufferLineLen]), runLenChar, runLen);
639 bufferLineLen = newLineSize;
644 else if (opCode == 11)
646 if (bufferLineLen < lineBuffSize)
653 cerr <<
"ScannerFile4BitEncoded::GetNextScanLine ***ERROR*** Exceeding 'bufferLineLen'; ScanLine[" << nextScanLine <<
"]." << endl;
657 else if (opCode == 12)
660 kkuint16 numRawPixels = numRawRecs * 2;
661 kkuint16 newBufferLineLen = bufferLineLen + numRawPixels;
662 if (newBufferLineLen > lineBuffSize)
664 cerr <<
"ScannerFile4BitEncoded::GetNextScanLine ***ERROR*** Exceeding 'bufferLineLen'; ScanLine[" << nextScanLine <<
"]." << endl;
666 ProcessRawPixelRecs (numRawRecs, lineBuff, lineBuffSize, bufferLineLen);
669 else if (opCode == 13)
671 recsRead = fread (&rec2,
sizeof (rec2), 1, file);
680 kkuint16 numRawPixels = 1 + 2 * numRawRecs;
681 kkuint16 newBufferLineLen = bufferLineLen + numRawPixels;
682 if (newBufferLineLen > lineBuffSize)
684 cerr <<
"ScannerFile4BitEncoded::GetNextScanLine ***ERROR*** Exceeding 'bufferLineLen'; ScanLine[" << nextScanLine <<
"]." << endl;
687 if (bufferLineLen < lineBuffSize)
693 ProcessRawPixelRecs (numRawRecs, lineBuff, lineBuffSize, bufferLineLen);
737 *encodedBuffNext = rec;
746 *encodedBuffNext = rec;
751 else if (runLen < 257)
753 ushort encodedLen = runLen - 1;
757 *encodedBuffNext = rec;
761 *encodedBuffNext = rec;
770 *encodedBuffNext = rec;
774 *encodedBuffNext = rec;
796 *encodedBuffNext = rec;
807 *encodedBuffNext = rec;
814 *encodedBuffNext = rec;
822 kkuint16 lenToProcess = Min ((kkuint16)513, len);
823 if ((lenToProcess % 2) > 0)
829 kkuint16 numRawRecsNeeded = (lenToProcess - 1) / 2;
830 kkuint16 numRawRecsNeededEnc = numRawRecsNeeded - 1;
831 kkuint16 lenHigh = numRawRecsNeededEnc / 16;
832 kkuint16 lenLow = numRawRecsNeededEnc % 16;
836 *encodedBuffNext = rec;
841 *encodedBuffNext = rec;
847 while (lenToProcess > 1)
851 *encodedBuffNext = rec;
868 encodedBuffNext = encodedBuff;
870 kkuint32 encodedBuffLeft = encodedBuffSize - encodedBuffLen;
871 if (bufferLen > (encodedBuffLeft - 10))
873 kkuint32 newEncodedBuffSise = bufferLen + 10;
874 ReSizeEncodedBuff (newEncodedBuffSise);
877 curCompStatus = csNull;
879 for (
kkuint16 x = 0; x < bufferLen; ++x)
881 uchar nextCh = convTable8BitTo4Bit[buffer[x]];
883 switch (curCompStatus)
889 curCompStatus = csRunLen;
895 if (nextCh == runLenChar)
899 else if (runLen == 1)
901 rawStr[0] = runLenChar;
905 curCompStatus = csRaw;
909 AddCurRunLenToOutputBuffer ();
920 rawStr[rawStrLen] = nextCh;
924 else if (rawStr[rawStrLen - 1] != nextCh)
926 rawStr[rawStrLen] = nextCh;
934 AddCurRawStrToOutputBuffer ();
938 curCompStatus = csRunLen;
945 if (curCompStatus == csRunLen)
946 AddCurRunLenToOutputBuffer ();
948 if (curCompStatus == csRaw)
949 AddCurRawStrToOutputBuffer ();
954 *encodedBuffNext = rec;
957 encodedBuffLen = encodedBuffNext - encodedBuff;
958 fwrite (encodedBuff,
sizeof (OpRec), encodedBuffLen, file);
973 if (txtBlockLen > (encodedBuffSize - 10))
975 kkuint32 newEncodedBuffSise = txtBlockLen + 10;
976 ReSizeEncodedBuff (newEncodedBuffSise);
979 encodedBuffNext = encodedBuff;
981 kkint32 charsLeft = txtBlockLen;
982 const uchar* txtBlockPtr = txtBlock;
983 while (charsLeft > 0)
985 kkint32 charsToWrite = Min (2048, charsLeft);
991 *encodedBuffNext = rec;
997 memcpy (encodedBuffNext, txtBlockPtr, charsToWrite);
999 charsLeft -= charsToWrite;
1000 txtBlockPtr += charsToWrite;
1001 encodedBuffNext += charsToWrite;
1004 fwrite (encodedBuff,
sizeof (OpRec), encodedBuffLen, file);
__int16 kkint16
16 bit signed integer.
kkint64 frameBufferFileOffsetLast
kkint64 frameBufferFileOffsetNext
ScannerFile4BitEncoded(const KKStr &_fileName, RunLog &_log)
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.
static const uchar * CompensationTable()
OpRecRun256Len2 run256Len2
OpRecInstrumentDataWord1 instrumentDataWord1
unsigned __int16 kkuint16
16 bit unsigned integer.
OpRecTextBlock1 textBlock1
virtual ~ScannerFile4BitEncoded()
unsigned __int32 kkuint32
virtual void ScanRate(float _scanRate)
OpRecInstrumentDataWord3()
virtual void WriteBufferFrame()
Write the contents of 'frameBuffer' to he end of the scanner file.
OpRecTextBlock2 textBlock2
unsigned char uchar
Unsigned character.
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of 'std::string' strings.
OpRecInstrumentDataWord2()
Contains Classes that are specific to Cameras physical characteristics.
unsigned short ushort
Unsigned short.
OpRecRaw513Pixels2 raw513Pixels2
OpRecEndOfScanLine endOfScanLine
OpRecRun256Len1 run256Len1
kkuint32 ReadBufferFrame()
Writes a 32 bit number into the Scanner File Stream at current location.
OpRecRaw513Pixels1 raw513Pixels1
kkuint32 frameBufferNextLine
Implements a 4 bit Encoded format.
kkuint32 pixelsPerScanLine
Used for logging messages.
ScannerFile4BitEncoded(const KKStr &_fileName, kkuint32 _pixelsPerScanLine, kkuint32 _frameHeight, RunLog &_log)
ScannerFile(const KKStr &_fileName, kkuint32 _pixelsPerScanLine, kkuint32 _frameHeight, RunLog &_log)
OpRecRaw32Pixels raw32Pixels
Maintains one instance of a GoalKeeper object that can be used anywhere in the application.
ScannerFile(const KKStr &_fileName, RunLog &_log)
virtual void WriteTextBlock(const uchar *txtBlock, kkuint32 txtBlockLen)