KSquare Utilities
ScannerFileZLib3BitEncoded.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 
7 #include <iostream>
8 #include <fstream>
9 #include <map>
10 #include <vector>
11 
12 #include "MemoryDebug.h"
13 
14 using namespace std;
15 
16 #include "Compressor.h"
17 #include "KKBaseTypes.h"
18 #include "OSservices.h"
19 #include "RunLog.h"
20 #include "KKStr.h"
21 using namespace KKB;
22 
23 
24 #include "ScannerFile.h"
25 
26 #include "Variables.h"
27 
29 
30 using namespace KKLSC;
31 
32 #pragma pack(push)
33 #pragma pack(1)
34 
35 
37 {
38  uint8 intHi;
39  uint8 intLo;
40 };
41 
42 
44 {
45  uint8 intByte0; // Highest Order
46  uint8 intByte1;
47  uint8 intByte2; // Lowest Order
48 };
49 
50 
52 {
53  uint8 intByte0; // Highest Order
54  uint8 intByte1;
55  uint8 intByte2;
56  uint8 intByte3; // Lowest Order
57 };
58 
59 
61 {
62  uint8 opCode;
63  uint8 textBlockLen;
64 };
65 
66 
67 
69 {
70  uint8 opCode;
73 };
74 
75 
77 {
78  uint8 opCode;
81 }; /* OpCodeRec5 */
82 
83 
84 
86 {
87  uint8 opCode;
91 }; /* OpCodeRec6 */
92 
93 
94 
96 {
97  uint8 opCode;
102 }; /* OpCodeRec7 */
103 
104 #pragma pack(pop)
105 
106 
108  RunLog& _log
109  ):
110  ScannerFile (_fileName, _log),
111  compBuffer (NULL),
112  compBufferLen (0),
113  compBufferSize (0)
114 {
115  AllocateBuffers ();
116 }
117 
118 
119 
121  kkuint32 _pixelsPerScanLine,
122  kkuint32 _frameHeight,
123  RunLog& _log
124  ):
125  ScannerFile (_fileName, _pixelsPerScanLine, _frameHeight, _log),
126  compBuffer (NULL),
127  compBufferLen (0),
128  compBufferSize (0)
129 {
130  AllocateBuffers ();
131 }
132 
133 
134 
135 
136 
138 {
139  if (opened)
140  Close ();
141  delete compBuffer;
142  compBuffer = NULL;
143 }
144 
145 
146 
147 void ScannerFileZLib3BitEncoded::AllocateBuffers ()
148 {
149  delete compBuffer; compBuffer = NULL;
150 
151  compBufferSize = (kkuint32)(frameBufferSize * 1.2);
152  compBufferLen = 0;
153  compBuffer = new uchar[compBufferSize];
154 } /* AllocateBuffers */
155 
156 
157 
158 void ScannerFileZLib3BitEncoded::ScanRate (float _scanRate)
159 {
160  ScannerFile::ScanRate (_scanRate);
161 }
162 
163 
164 
166  kkuint32 txtBlockLen
167  )
168 {
169  kkuint32 charsLeft = txtBlockLen;
170  const uchar* txtBlockPtr = txtBlock;
171  while (charsLeft > 0)
172  {
173  if (charsLeft < 256)
174  {
175  OpCodeRec1 rec;
176  rec.opCode = 1;
177  rec.textBlockLen = (uint8)charsLeft;
178  fwrite (&rec, sizeof (rec), 1, file);
179  fwrite (txtBlockPtr, charsLeft, 1, file);
180  txtBlockPtr += charsLeft;
181  charsLeft = 0;
182  }
183 
184  else
185  {
186  kkuint32 bytesToWrite = Min ((kkuint32)65535, charsLeft);
187  OpCodeRec2 rec;
188  rec.opCode = 2;
189  rec.textBlockLenHi = (uint8)(bytesToWrite / 256);
190  rec.textBlockLenLo = (uint8)(bytesToWrite % 256);
191  fwrite (&rec, sizeof (rec), 1, file);
192  fwrite (txtBlockPtr, bytesToWrite, 1, file);
193  txtBlockPtr += bytesToWrite;
194  charsLeft -= bytesToWrite;
195  }
196  }
197 
199 } /* WriteTextBlock */
200 
201 
202 
203 void ScannerFileZLib3BitEncoded::WriteBufferFrame ()
204 {
205  kkuint32 frameBufferSpaceUsed = frameBufferNextLine * pixelsPerScanLine;
206  kkuint32 compressedBuffLen = 0;
207 
208  for (kkuint32 x = 0; x < frameBufferSpaceUsed; ++x)
209  {
210  frameBuffer[x] = frameBuffer[x] & 0xe0;
211  }
212 
213  uchar* compressedData = (uchar*)Compressor::CreateCompressedBuffer (frameBuffer, frameBufferSpaceUsed, compressedBuffLen);
214 
216 
217  if (compressedBuffLen <= 65535)
218  {
219  OpCodeRec5 rec5;
220  rec5.opCode = 5;
221  rec5.compBuffLenLO = compressedBuffLen % 256;
222  rec5.compBuffLenHO = compressedBuffLen /256;
223  fwrite (&rec5, sizeof (rec5), 1, file);
224  }
225 
226  else if (compressedBuffLen <= 16777215)
227  {
228  kkuint32 buffLen = compressedBuffLen;
229  OpCodeRec6 rec6;
230  rec6.opCode = 6;
231  rec6.compBuffLenByte2 = buffLen % 256; buffLen = buffLen / 256;
232  rec6.compBuffLenByte1 = buffLen % 256; buffLen = buffLen / 256;
233  rec6.compBuffLenByte0 = buffLen % 256;
234  fwrite (&rec6, sizeof (rec6), 1, file);
235  }
236 
237  else
238  {
239  kkuint32 buffLen = compressedBuffLen;
240  OpCodeRec7 rec7;
241  rec7.opCode = 7;
242  rec7.compBuffLenByte3 = buffLen % 256; buffLen = buffLen / 256;
243  rec7.compBuffLenByte2 = buffLen % 256; buffLen = buffLen / 256;
244  rec7.compBuffLenByte1 = buffLen % 256; buffLen = buffLen / 256;
245  rec7.compBuffLenByte0 = buffLen;
246  fwrite (&rec7, sizeof (rec7), 1, file);
247  }
248 
249  fwrite (compressedData, compressedBuffLen, 1, file);
250  delete compressedData; compressedData = NULL;
252 
255 } /* WriteBufferFrame */
256 
257 
258 
259 
260 void ScannerFileZLib3BitEncoded::ExpandBuffer (uchar*& buffer,
261  kkuint32& bufferSize,
262  kkuint32 bufferNewSize
263  )
264 {
265  uchar* newBuffer = new uchar[bufferNewSize];
266  if (buffer != NULL)
267  {
268  memcpy (newBuffer, buffer, bufferSize);
269  delete buffer;
270  }
271  buffer = newBuffer;
272  newBuffer = NULL;
273  bufferSize = bufferNewSize;
274  return;
275 } /* ExpandBuffer */
276 
277 
278 
279 void ScannerFileZLib3BitEncoded::ExpandBufferNoCopy (uchar*& buffer,
280  kkuint32& bufferSize,
281  kkuint32 bufferNewSize
282  )
283 {
284  uchar* newBuffer = new uchar[bufferNewSize];
285  if (buffer != NULL)
286  delete buffer;
287 
288  buffer = newBuffer;
289  newBuffer = NULL;
290  bufferSize = bufferNewSize;
291  return;
292 } /* ExpandBufferNoCopy */
293 
294 
295 
296 kkint64 ScannerFileZLib3BitEncoded::SkipToNextFrame ()
297 {
298  bool bufferFrameRead = false;
299 
300  while ((feof (file) == 0) && (!bufferFrameRead))
301  {
302  uint8 opCode;
303  fread (&opCode, sizeof (opCode), 1, file);
304 
305  if (feof (file) != 0)
306  return -1;
307 
308  switch (opCode)
309  {
310  case 1:
311  {
312  uint8 textBlockLen;
313  fread (&textBlockLen, 1, 1, file);
314  if (feof (file) != 0)
315  return -1;
316  SkipBytesForward (textBlockLen);
317  break;
318  }
319 
320  case 2:
321  {
322  TwoByteRec textBlockLen;
323  fread (&textBlockLen, sizeof (textBlockLen), 1, file);
324  if (feof (file) != 0)
325  return -1;
326  kkuint32 len = textBlockLen.intHi * 256 + textBlockLen.intLo;
327  SkipBytesForward (len);
328  break;
329  }
330 
331  case 5:
332  {
333  TwoByteRec textBlockLen;
334  fread (&textBlockLen, sizeof (textBlockLen), 1, file);
335  if (feof (file) != 0)
336  return -1;
337  kkuint32 compBufferLen = textBlockLen.intHi * 256 +
338  textBlockLen.intLo;
339  SkipBytesForward (compBufferLen);
340  bufferFrameRead = true;
341  break;
342  }
343 
344  case 6:
345  {
346  ThreeByteRec textBlockLen;
347  fread (&textBlockLen, sizeof (textBlockLen), 1, file);
348  if (feof (file) != 0)
349  return -1;
350  kkuint32 compBufferLen = textBlockLen.intByte0 * 256 * 256 +
351  textBlockLen.intByte1 * 256 +
352  textBlockLen.intByte2;
353  SkipBytesForward (compBufferLen);
354  bufferFrameRead = true;
355  break;
356  }
357 
358  case 7:
359  {
360  FourByteRec textBlockLen;
361  fread (&textBlockLen, sizeof (textBlockLen), 1, file);
362  if (feof (file) != 0)
363  return -1;
364 
365  kkuint32 compBufferLen = textBlockLen.intByte0 * 256 * 256 * 256 +
366  textBlockLen.intByte1 * 256 * 256 +
367  textBlockLen.intByte2 * 256 +
368  textBlockLen.intByte3;
369  SkipBytesForward (compBufferLen);
370  bufferFrameRead = true;
371  break;
372  }
373  }
374  }
375 
376  if (feof (file) != 0)
377  return -1;
378  else
379  return osFTELL (file);
380 
381 } /* SkipToNextFrame */
382 
383 
384 
385 /** @brief Read in the next frame into the Buffer from the file. */
386 kkuint32 ScannerFileZLib3BitEncoded::ReadBufferFrame ()
387 {
388  frameBufferLen = 0;
390  if (feof (file) != 0)
391  {
392  memset (frameBuffer, 0, frameBufferSize);
393  return 0;
394  }
395 
396  bool bufferFrameRead = false;
397 
399 
400  while ((!feof (file)) && (!bufferFrameRead))
401  {
402  uint8 opCode;
403 
404  fread (&opCode, sizeof (opCode), 1, file);
405 
406  if (feof (file) != 0)
407  {
408  memset (frameBuffer, 0, frameBufferSize);
409  return 0;
410  }
411 
412  switch (opCode)
413  {
414  case 1:
415  {
416  uint8 textBlockLen;
417  fread (&textBlockLen, 1, 1, file);
418  char buff[257];
419  fread (&buff, textBlockLen, 1, file);
420  if (!feof (file))
421  {
422  buff[textBlockLen] = 0;
423  ReportTextMsg (buff, textBlockLen);
424  }
425  break;
426  }
427 
428  case 2:
429  {
430  TwoByteRec textBlockLen;
431  fread (&textBlockLen, sizeof (textBlockLen), 1, file);
432  if (feof (file) == 0)
433  {
434  kkuint32 len = textBlockLen.intHi * 256 + textBlockLen.intLo;
435  fread (compBuffer, len, 1, file);
436  if (!feof (file))
437  ReportTextMsg ((char*)compBuffer, len);
438  }
439  break;
440  }
441 
442  case 5:
443  {
444  TwoByteRec textBlockLen;
445  fread (&textBlockLen, sizeof (textBlockLen), 1, file);
446  if (feof (file) == 0)
447  {
448  kkuint32 compBufferLen = textBlockLen.intHi * 256 +
449  textBlockLen.intLo;
450  if (compBufferLen > compBufferSize)
451  ExpandBufferNoCopy (compBuffer, compBufferSize, compBufferLen);
452 
453  fread (compBuffer, compBufferLen, 1, file);
454  if (feof (file) == 0)
455  {
456  Compressor::Decompress (compBuffer, compBufferLen,
457  frameBuffer, frameBufferSize, frameBufferLen
458  );
459  }
460  }
461  bufferFrameRead = true;
462  break;
463  }
464 
465  case 6:
466  {
467  ThreeByteRec textBlockLen;
468  fread (&textBlockLen, sizeof (textBlockLen), 1, file);
469  if (!feof (file))
470  {
471  kkuint32 compBufferLen = textBlockLen.intByte0 * 256 * 256 +
472  textBlockLen.intByte1 * 256 +
473  textBlockLen.intByte2;
474  if (compBufferLen > compBufferSize)
475  ExpandBufferNoCopy (compBuffer, compBufferSize, compBufferLen);
476 
477  fread (compBuffer, compBufferLen, 1, file);
478  if (feof (file) == 0)
479  {
480  Compressor::Decompress (compBuffer, compBufferLen,
481  frameBuffer, frameBufferSize, frameBufferLen
482  );
483  }
484  }
485  bufferFrameRead = true;
486  break;
487  }
488 
489  case 7:
490  {
491  FourByteRec textBlockLen;
492  fread (&textBlockLen, sizeof (textBlockLen), 1, file);
493  if (!feof (file))
494  {
495  kkuint32 compBufferLen = textBlockLen.intByte0 * 256 * 256 * 256 +
496  textBlockLen.intByte1 * 256 * 256 +
497  textBlockLen.intByte2 * 256 +
498  textBlockLen.intByte3;
499  if (compBufferLen > compBufferSize)
500  ExpandBufferNoCopy (compBuffer, compBufferSize, compBufferLen);
501 
502  fread (compBuffer, compBufferLen, 1, file);
503  if (feof (file) == 0)
504  {
505  Compressor::Decompress (compBuffer, compBufferLen,
506  frameBuffer, frameBufferSize, frameBufferLen
507  );
508  }
509  }
510  bufferFrameRead = true;
511  break;
512  }
513  }
514 
515  frameBufferNextLine = 0;
516  }
517 
519 
521 } /* ReadBufferFrame */
kkint64 frameBufferFileOffsetLast
Definition: ScannerFile.h:427
Simple class that will compress and decompress specified buffers using the routines provided in zlib...
Definition: Compressor.h:17
kkint64 frameBufferFileOffsetNext
Definition: ScannerFile.h:429
kkint64 fileSizeInBytes
Definition: ScannerFile.h:396
ScannerFileZLib3BitEncoded(const KKStr &_fileName, kkuint32 _pixelsPerScanLine, kkuint32 _frameHeight, RunLog &_log)
kkint64 osFTELL(FILE *f)
Calls the appropriate 64 bit function for operating system.
Definition: OSservices.cpp:93
kkuint32 frameBufferLen
Definition: ScannerFile.h:435
kkuint32 frameBufferSize
Definition: ScannerFile.h:447
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
__int64 kkint64
Definition: KKBaseTypes.h:90
static void * CreateCompressedBuffer(void *source, kkuint32 sourceLen, kkuint32 &compressedBuffLen)
Definition: Compressor.cpp:22
KKTHread * KKTHreadPtr
ScannerFileZLib3BitEncoded(const KKStr &_fileName, RunLog &_log)
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 frameBufferNextLine
Definition: ScannerFile.h:441
kkuint32 pixelsPerScanLine
Definition: ScannerFile.h:407
Used for logging messages.
Definition: RunLog.h:49
ScannerFile(const KKStr &_fileName, kkuint32 _pixelsPerScanLine, kkuint32 _frameHeight, RunLog &_log)
virtual void WriteTextBlock(const uchar *txtBlock, kkuint32 txtBlockLen)
ScannerFile(const KKStr &_fileName, RunLog &_log)
Definition: ScannerFile.cpp:40
Implements Zlib compression on 3bit pixel data.