Isis 3 Programmer Reference
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 
180  return getDataStartByte() + getChunkIndex(chunk) * getBytesPerChunk();
181  }
182 }
long long int BigInt
Big int.
Definition: Constants.h:65
void clearCache(bool blockForWriteCache=true) const
Free all cube chunks (cached cube data) from memory and write them to disk.
int getChunkIndex(const RawCubeChunk &) const
Given a chunk, what&#39;s its index in the file.
void setRawData(QByteArray rawData)
Sets the chunk&#39;s raw data.
void setChunkSizes(int numSamples, int numLines, int numBands)
This should be called once from the child constructor.
int getSampleCountInChunk() const
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:286
int SizeOf(Isis::PixelType pixelType)
Returns the number of bytes of the specified PixelType.
Definition: PixelType.h:62
Namespace for the standard library.
int findGoodSize(int maxSize, int dimensionSize) const
This is a helper method that tries to compute a good tile size for one of the cube&#39;s dimensions (samp...
BigInt getDataStartByte() const
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:171
bool hasKeyword(const QString &kname, FindOptions opts) const
See if a keyword is in the current PvlObject, or deeper inside other PvlObjects and Pvlgroups within ...
Definition: PvlObject.cpp:207
Handles converting buffers to and from disk.
~CubeTileHandler()
Writes all data from memory to disk.
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
A single keyword-value pair.
Definition: PvlKeyword.h:98
A section of raw data on the disk.
Definition: RawCubeChunk.h:42
virtual void writeRaw(const RawCubeChunk &chunkToWrite)
This needs to write the chunkToWrite directly to disk with no modifications to the data itself...
BigInt getBytesPerChunk() const
Container for cube-like labels.
Definition: Pvl.h:135
BigInt getTileStartByte(const RawCubeChunk &chunk) const
This is a helper method that goes from chunk to file position.
void updateLabels(Pvl &label)
Update the cube labels so that this cube indicates what tile size it used.
Isis exception class.
Definition: IException.h:107
Adds specific functionality to C++ strings.
Definition: IString.h:181
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
virtual void readRaw(RawCubeChunk &chunkToFill)
This needs to populate the chunkToFill with unswapped raw bytes from the disk.
int getByteCount() const
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
QByteArray & getRawData() const
Definition: RawCubeChunk.h:53
int getLineCountInChunk() const
PixelType pixelType() const