1 /******************************************************************************
2  *
3  * Purpose:  Implementation of Prologue class. Parse the prologue of one
4  *           repeat cycle and keep the interesting info.
5  * Author:   Bas Retsios, retsios@itc.nl
6  *
7  ******************************************************************************
8  * Copyright (c) 2004, ITC
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  ******************************************************************************/
28 
29  #include "cpl_port.h"  // Must be first.
30 
31 #include "prologue.h"
32 
33 CPL_CVSID("$Id: prologue.cpp 327bfdc0f5dd563c3b1c4cbf26d34967c5c9c790 2020-02-28 13:51:40 +0100 Even Rouault $")
34 
35 static
size_SatelliteStatus()36 int size_SatelliteStatus()
37 {
38   int iSizePrimary = 1+4+1+1+4+4+1+1+4+4+1;
39 
40   int iSizeOrbitCoef = 4 + 4 + 8*8 + 8*8 + 8*8 + 8*8 + 8*8 + 8*8;
41   int iSizeOrbit = 4 + 4 + 100*iSizeOrbitCoef;
42   int iSizeAttitudeCoef = 4 + 4 + 8*8 + 8*8 + 8*8;
43   int iSizeAttitude = 4 + 4 + 8 + 100*iSizeAttitudeCoef;
44   int iSizeSpinRateatRCStart = 8;
45   int iSizeUTCCorrelation = 4 + 4 + 4*4*3 + 8 + 8 + 8 + 8 + 8;
46 
47   int iTotalSize = iSizePrimary + iSizeOrbit + iSizeAttitude + iSizeSpinRateatRCStart + iSizeUTCCorrelation;
48 
49   return iTotalSize;
50 }
51 
52 static
size_ImageAcquisition()53 int size_ImageAcquisition()
54 {
55   // up to  DHSSSynchSelection
56   int iSize1 = 8 + 8 + 8 + 12 + 42 + 42*2 + 2 + 2 + 2 + 2 + 1;
57   // up to RefocusingDirection
58   int iSize2 = 42*2 + 42 + 42*2 + 42*2 + 42*2 + 27*2 + 15*2 + 6*2 + 1 + 2 + 1;
59   // to end
60   int iSize3 = 2 + 1 + 2 + 4 + 2 + 2 + 2 + 1 + 4 + 1 + 4 + 4 + 1 + 1 + 2 + 2 + 2 + 2;
61 
62   int iTotalSize = iSize1 + iSize2 + iSize3;
63 
64   return iTotalSize;
65 }
66 
67 static
size_CelestialEvents()68 int size_CelestialEvents()
69 {
70   int iSizeCelestialBodies = 2 + 2 + 4 + 4 + 3*100*(2 + 2 + 8*8 + 8*8) + 100*(20*(2 + 2 + 2 + 8*8 + 8*8));
71 
72   int iSizeRelationToImage = 1 + 2 + 2 + 1 + 1 + 1;
73 
74   int iTotalSize = iSizeCelestialBodies + iSizeRelationToImage;
75 
76   return iTotalSize;
77 }
78 
79 static
size_Correction()80 int size_Correction()
81 {
82   return 19229;
83 }
84 
85 static
iReadDouble(std::ifstream & ifile)86 double iReadDouble(std::ifstream & ifile)
87 {
88     double rVal;
89     ifile.read(reinterpret_cast<char*>(&rVal), 8);
90     CPL_MSBPTR64(&rVal);
91     return rVal;
92 }
93 
94 static
iReadReal(std::ifstream & ifile)95 double iReadReal(std::ifstream & ifile)
96 {
97     float fVal;
98     ifile.read(reinterpret_cast<char*>(&fVal), 4);
99     CPL_MSBPTR32(&fVal);
100     return fVal;
101 }
102 
103 static
iReadInt(std::ifstream & ifile)104 int iReadInt(std::ifstream & ifile)
105 {
106     int iResult;
107     ifile.read(reinterpret_cast<char*>(&iResult), 4);
108     CPL_MSBPTR32(&iResult);
109     return iResult;
110 }
111 
112 static
iReadByte(std::ifstream & ifile)113 unsigned char iReadByte (std::ifstream & ifile)
114 {
115   // will read 1 byte from the file
116     char b;
117 
118     ifile.read(&b, 1);
119 
120     return b;
121 }
122 
ReferenceGridRecord(std::ifstream & ifile)123 ReferenceGridRecord::ReferenceGridRecord(std::ifstream & ifile)
124 {
125     NumberOfLines = iReadInt(ifile);
126     NumberOfColumns = iReadInt(ifile);
127     LineDirGridStep = iReadReal(ifile);
128     ColumnDirGridStep = iReadReal(ifile);
129     GridOrigin = iReadByte(ifile);
130 }
131 
PlannedCoverageVIS_IRRecord(std::ifstream & ifile)132 PlannedCoverageVIS_IRRecord::PlannedCoverageVIS_IRRecord(std::ifstream & ifile)
133 {
134     SouthernLinePlanned = iReadInt(ifile);
135     NorthernLinePlanned = iReadInt(ifile);
136     EasternColumnPlanned = iReadInt(ifile);
137     WesternColumnPlanned = iReadInt(ifile);
138 }
139 
PlannedCoverageHRVRecord(std::ifstream & ifile)140 PlannedCoverageHRVRecord::PlannedCoverageHRVRecord(std::ifstream & ifile)
141 {
142     LowerSouthLinePlanned = iReadInt(ifile);
143     LowerNorthLinePlanned = iReadInt(ifile);
144     LowerEastColumnPlanned = iReadInt(ifile);
145     LowerWestColumnPlanned = iReadInt(ifile);
146     UpperSouthLinePlanned = iReadInt(ifile);
147     UpperNorthLinePlanned = iReadInt(ifile);
148     UpperEastColumnPlanned = iReadInt(ifile);
149     UpperWestColumnPlanned = iReadInt(ifile);
150 }
151 
ImageDescriptionRecord(std::ifstream & ifile)152 ImageDescriptionRecord::ImageDescriptionRecord(std::ifstream & ifile)
153 {
154     TypeOfProjection = iReadByte(ifile);
155     LongitudeOfSSP = iReadReal(ifile);
156     ReferenceGridVIS_IR = new ReferenceGridRecord(ifile);
157     ReferenceGridHRV = new ReferenceGridRecord(ifile);
158     PlannedCoverageVIS_IR = new PlannedCoverageVIS_IRRecord(ifile);
159     PlannedCoverageHRV = new PlannedCoverageHRVRecord(ifile);
160     ImageProcDirection = iReadByte(ifile);
161     PixelGenDirection = iReadByte(ifile);
162     for (int i=0; i < 12; ++i)
163       PlannedChannelProcessing[i] = iReadByte(ifile);
164 }
165 
~ImageDescriptionRecord()166 ImageDescriptionRecord::~ImageDescriptionRecord()
167 {
168     delete ReferenceGridVIS_IR;
169     delete ReferenceGridHRV;
170     delete PlannedCoverageVIS_IR;
171     delete PlannedCoverageHRV;
172 }
173 
RadiometricProcessingRecord(std::ifstream & ifile)174 RadiometricProcessingRecord::RadiometricProcessingRecord(std::ifstream & ifile)
175 {
176   // skip a part that doesn't interest us
177     unsigned char dummy [12];
178     int i;
179     for (i = 0; i < 6; ++i)
180       ifile.read((char*)dummy, 12);
181 
182     for (i = 0; i < 12; ++i)
183     {
184       Cal_Slope[i] = iReadDouble(ifile);
185       Cal_Offset[i] = iReadDouble(ifile);
186     }
187 }
188 
189 //////////////////////////////////////////////////////////////////////
190 // Construction/Destruction
191 //////////////////////////////////////////////////////////////////////
192 
Prologue()193 Prologue::Prologue() :
194     m_idr(nullptr),
195     m_rpr(nullptr)
196 {}
197 
~Prologue()198 Prologue::~Prologue()
199 {
200   if (m_idr)
201     delete m_idr;
202   if (m_rpr)
203     delete m_rpr;
204 }
205 
read(std::ifstream & ifile)206 void Prologue::read(std::ifstream & ifile)
207 {
208   /*unsigned char version = */iReadByte(ifile);
209 
210   int iSkipHeadersSize = size_SatelliteStatus() + size_ImageAcquisition() + size_CelestialEvents() + size_Correction();
211 
212   ifile.seekg(iSkipHeadersSize, std::ios_base::cur);
213 
214   m_idr = new ImageDescriptionRecord(ifile);
215 
216   m_rpr = new RadiometricProcessingRecord(ifile);
217   // TODO: file is not left at the end of the Radiometric Processing Record
218 }
219