USGS

Isis 3.0 Object Programmers' Reference

Home

CubeTileHandler.cpp
Go to the documentation of this file.
1 
24 #include "CubeTileHandler.h"
25 
26 #include <QFile>
27 
28 #include "IException.h"
29 #include "Pvl.h"
30 #include "PvlObject.h"
31 #include "PvlKeyword.h"
32 #include "RawCubeChunk.h"
33 
34 using namespace std;
35 
36 namespace Isis {
48  CubeTileHandler::CubeTileHandler(QFile * dataFile,
49  const QList<int> *virtualBandList, const Pvl &labels, bool alreadyOnDisk)
50  : CubeIoHandler(dataFile, virtualBandList, labels, alreadyOnDisk) {
51 
52  const PvlObject &core = labels.findObject("IsisCube").findObject("Core");
53 
54  if(core.hasKeyword("Format")) {
55  setChunkSizes(core["TileSamples"], core["TileLines"], 1);
56  }
57  else {
58  // up to 1MB chunks
59  int sampleChunkSize =
60  findGoodSize(512 * 4 / SizeOf(pixelType()), sampleCount());
61  int lineChunkSize =
62  findGoodSize(512 * 4 / SizeOf(pixelType()), lineCount());
63 
64  setChunkSizes(sampleChunkSize, lineChunkSize, 1);
65  }
66  }
67 
68 
73  clearCache();
74  }
75 
76 
83  PvlObject &core = labels.findObject("IsisCube").findObject("Core");
84  core.addKeyword(PvlKeyword("Format", "Tile"),
85  PvlContainer::Replace);
86  core.addKeyword(PvlKeyword("TileSamples", toString(getSampleCountInChunk())),
87  PvlContainer::Replace);
88  core.addKeyword(PvlKeyword("TileLines", toString(getLineCountInChunk())),
89  PvlContainer::Replace);
90  }
91 
92 
94  BigInt startByte = getTileStartByte(chunkToFill);
95 
96  bool success = false;
97 
98  QFile * dataFile = getDataFile();
99  if(dataFile->seek(startByte)) {
100  QByteArray binaryData = dataFile->read(chunkToFill.getByteCount());
101 
102  if(binaryData.size() == chunkToFill.getByteCount()) {
103  chunkToFill.setRawData(binaryData);
104  success = true;
105  }
106  }
107 
108  if(!success) {
109  IString msg = "Reading from the file [" + dataFile->fileName() + "] "
110  "failed with reading [" +
111  QString::number(chunkToFill.getByteCount()) +
112  "] bytes at position [" + QString::number(startByte) + "]";
113  throw IException(IException::Io, msg, _FILEINFO_);
114  }
115  }
116 
117 
118  void CubeTileHandler::writeRaw(const RawCubeChunk &chunkToWrite) {
119  BigInt startByte = getTileStartByte(chunkToWrite);
120  bool success = false;
121 
122  QFile * dataFile = getDataFile();
123  if(dataFile->seek(startByte)) {
124  BigInt dataWritten = dataFile->write(chunkToWrite.getRawData());
125 
126  if(dataWritten == chunkToWrite.getByteCount()) {
127  success = true;
128  }
129  }
130 
131  if(!success) {
132  IString msg = "Writing to the file [" + dataFile->fileName() + "] "
133  "failed with writing [" +
134  QString::number(chunkToWrite.getByteCount()) +
135  "] bytes at position [" + QString::number(startByte) + "]";
136  throw IException(IException::Io, msg, _FILEINFO_);
137  }
138  }
139 
140 
151  int CubeTileHandler::findGoodSize(int maxSize, int dimensionSize) const {
152  int ideal = 128;
153 
154  if(dimensionSize <= maxSize) {
155  ideal = dimensionSize;
156  }
157  else {
158  int greatestDividend = maxSize;
159 
160  while(greatestDividend > ideal) {
161  if(dimensionSize % greatestDividend == 0) {
162  ideal = greatestDividend;
163  }
164 
165  greatestDividend --;
166  }
167  }
168 
169  return ideal;
170  }
171 
172 
179  BigInt CubeTileHandler::getTileStartByte(const RawCubeChunk &chunk) const {
180  return getDataStartByte() + getChunkIndex(chunk) * getBytesPerChunk();
181  }
182 }