File: xritheaderparser.cpp

package info (click to toggle)
gdal 1.10.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,320 kB
  • ctags: 74,726
  • sloc: cpp: 677,199; ansic: 162,820; python: 13,816; cs: 11,163; sh: 10,446; java: 5,279; perl: 4,429; php: 2,971; xml: 1,500; yacc: 934; makefile: 494; sql: 112
file content (154 lines) | stat: -rw-r--r-- 5,579 bytes parent folder | download | duplicates (3)
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
154
/******************************************************************************
 * $Id: xritheaderparser.cpp 15066 2008-07-28 20:21:59Z mloskot $
 *
 * 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
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ******************************************************************************/

#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 seeked 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);
      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
  {
#if _MSC_VER > 1000 && _MSC_VER < 1300  
    ifile.seekg(-probeSize, std::ios_base::seekdir::cur);
#else
    ifile.seekg(-probeSize, std::ios_base::cur);
#endif
  }
}

XRITHeaderParser::~XRITHeaderParser()
{

}

int XRITHeaderParser::parseInt16(unsigned char * num)
{
  return (num[0]<<8) | num[1];
}

long XRITHeaderParser::parseInt32(unsigned char * num)
{
  return (num[0]<<24) | (num[1]<<16) | (num[2]<<8) | num[3];
}

void XRITHeaderParser::parseHeader(unsigned char * buf, long totalHeaderLength)
{
  int remainingHeaderLength = 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
        {
          long cfac = parseInt32(&buf[35]); // column scaling factor
          long lfac = parseInt32(&buf[39]); // line scaling factor
          long coff = parseInt32(&buf[43]); // column offset
          long loff = parseInt32(&buf[47]); // line offset
          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;
  }
}