1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/******************************************************************************
*
* Purpose: Implementation of XRITHeaderParser class. Parse the header
* of the combined XRIT header/data files.
* Author: Bas Retsios, retsios@itc.nl
*
******************************************************************************
* Copyright (c) 2007, ITC
*
* SPDX-License-Identifier: MIT
******************************************************************************/
#include "cpl_port.h" // Must be first.
#include "xritheaderparser.h"
#include <cstdlib> // malloc, free
#include <cstring> // memcpy
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//
// Upon successful parsing of a header in ifile, isValid() returns true
// and ifile is sought to the beginning of the image data
//////////////////////////////////////////////////////////////////////
XRITHeaderParser::XRITHeaderParser(std::ifstream &ifile)
: m_isValid(false), m_isPrologue(false), m_dataSize(0), m_nrBitsPerPixel(0),
m_nrColumns(0), m_nrRows(0), m_scanNorth(false)
{
const unsigned int probeSize = 8;
unsigned char probeBuf[probeSize];
ifile.read((char *)probeBuf,
probeSize); // Probe file by reading first 8 bytes
if (probeBuf[0] == 0 && probeBuf[1] == 0 &&
probeBuf[2] == 16) // Check for primary header record
{
long totalHeaderLength = parseInt32(&probeBuf[4]);
if ((totalHeaderLength >= 10) &&
(totalHeaderLength <= 10000)) // Check for valid header length
{
unsigned char *buf =
(unsigned char *)std::malloc(totalHeaderLength);
if (buf)
{
std::memcpy(
buf, probeBuf,
probeSize); // save what we have already read when probing
ifile.read(
(char *)buf + probeSize,
totalHeaderLength -
probeSize); // read the rest of the header section
parseHeader(buf, totalHeaderLength);
std::free(buf);
m_isValid = true;
}
}
}
if (!m_isValid) // seek back to original position
{
ifile.seekg(-static_cast<int>(probeSize), std::ios_base::cur);
}
}
XRITHeaderParser::~XRITHeaderParser()
{
}
int XRITHeaderParser::parseInt16(unsigned char *num)
{
return (num[0] << 8) | num[1];
}
long XRITHeaderParser::parseInt32(unsigned char *num)
{
int i;
memcpy(&i, num, 4);
CPL_MSBPTR32(&i);
return i;
}
void XRITHeaderParser::parseHeader(unsigned char *buf, long totalHeaderLength)
{
int remainingHeaderLength = static_cast<int>(totalHeaderLength);
while (remainingHeaderLength > 0)
{
int headerType = buf[0];
int headerRecordLength = parseInt16(&buf[1]);
if (headerRecordLength > remainingHeaderLength)
break;
switch (headerType)
{
case 0: // primary header
{
int fileTypeCode =
buf[3]; // 0 = image data file, 128 = prologue
if (fileTypeCode == 128)
m_isPrologue = true;
long dataFieldLengthH = parseInt32(
&buf[8]); // length of data field in bits (High DWORD)
long dataFieldLengthL = parseInt32(
&buf[12]); // length of data field in bits (Low DWORD)
m_dataSize = (dataFieldLengthH << 5) +
(dataFieldLengthL >>
3); // combine and convert bits to bytes
}
break;
case 1: // image structure
m_nrBitsPerPixel = buf[3]; // NB, number of bits per pixel
m_nrColumns = parseInt16(&buf[4]); // NC, number of columns
m_nrRows = parseInt16(&buf[6]); // NL, number of lines
break;
case 2: // image navigation
{
#if 0
/*long cfac =*/ parseInt32(&buf[35]); // column scaling factor
#endif
long lfac = parseInt32(&buf[39]); // line scaling factor
#if 0
/*long coff =*/ parseInt32(&buf[43]); // column offset
/*long loff =*/ parseInt32(&buf[47]); // line offset
#endif
if (lfac >= 0)
m_scanNorth = true;
else
m_scanNorth = false;
}
break;
case 3: // image data function
case 4: // annotation
case 5: // time stamp
case 6: // ancillary text
case 7: // key header
case 128: // image segment identification
case 129: // encryption key message header
case 130: // image compensation information header
case 131: // image observation time header
case 132: // image quality information header
break;
default: // ignore unknown header type
break;
}
buf += headerRecordLength;
remainingHeaderLength -= headerRecordLength;
}
}
|