Isis 3 Programmer Reference
CubeTileHandler.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include "CubeTileHandler.h"
9 
10 #include <QFile>
11 
12 #include "IException.h"
13 #include "Pvl.h"
14 #include "PvlObject.h"
15 #include "PvlKeyword.h"
16 #include "RawCubeChunk.h"
17 
18 using namespace std;
19 
20 namespace Isis {
32  CubeTileHandler::CubeTileHandler(QFile * dataFile,
33  const QList<int> *virtualBandList, const Pvl &labels, bool alreadyOnDisk)
34  : CubeIoHandler(dataFile, virtualBandList, labels, alreadyOnDisk) {
35 
36  const PvlObject &core = labels.findObject("IsisCube").findObject("Core");
37 
38  if(core.hasKeyword("Format")) {
39  setChunkSizes(core["TileSamples"], core["TileLines"], 1);
40  }
41  else {
42  // up to 1MB chunks
43  int sampleChunkSize =
44  findGoodSize(512 * 4 / SizeOf(pixelType()), sampleCount());
45  int lineChunkSize =
46  findGoodSize(512 * 4 / SizeOf(pixelType()), lineCount());
47 
48  setChunkSizes(sampleChunkSize, lineChunkSize, 1);
49  }
50  }
51 
52 
57  clearCache();
58  }
59 
60 
67  PvlObject &core = labels.findObject("IsisCube").findObject("Core");
68  core.addKeyword(PvlKeyword("Format", "Tile"),
69  PvlContainer::Replace);
70  core.addKeyword(PvlKeyword("TileSamples", toString(getSampleCountInChunk())),
71  PvlContainer::Replace);
72  core.addKeyword(PvlKeyword("TileLines", toString(getLineCountInChunk())),
73  PvlContainer::Replace);
74  }
75 
76 
78  BigInt startByte = getTileStartByte(chunkToFill);
79 
80  bool success = false;
81 
82  QFile * dataFile = getDataFile();
83  if(dataFile->seek(startByte)) {
84  QByteArray binaryData = dataFile->read(chunkToFill.getByteCount());
85 
86  if(binaryData.size() == chunkToFill.getByteCount()) {
87  chunkToFill.setRawData(binaryData);
88  success = true;
89  }
90  }
91 
92  if(!success) {
93  IString msg = "Reading from the file [" + dataFile->fileName() + "] "
94  "failed with reading [" +
95  QString::number(chunkToFill.getByteCount()) +
96  "] bytes at position [" + QString::number(startByte) + "]";
97  throw IException(IException::Io, msg, _FILEINFO_);
98  }
99  }
100 
101 
102  void CubeTileHandler::writeRaw(const RawCubeChunk &chunkToWrite) {
103  BigInt startByte = getTileStartByte(chunkToWrite);
104  bool success = false;
105 
106  QFile * dataFile = getDataFile();
107  if(dataFile->seek(startByte)) {
108  BigInt dataWritten = dataFile->write(chunkToWrite.getRawData());
109 
110  if(dataWritten == chunkToWrite.getByteCount()) {
111  success = true;
112  }
113  }
114 
115  if(!success) {
116  IString msg = "Writing to the file [" + dataFile->fileName() + "] "
117  "failed with writing [" +
118  QString::number(chunkToWrite.getByteCount()) +
119  "] bytes at position [" + QString::number(startByte) + "]";
120  throw IException(IException::Io, msg, _FILEINFO_);
121  }
122  }
123 
124 
135  int CubeTileHandler::findGoodSize(int maxSize, int dimensionSize) const {
136  int ideal = 128;
137 
138  if(dimensionSize <= maxSize) {
139  ideal = dimensionSize;
140  }
141  else {
142  int greatestDividend = maxSize;
143 
144  while(greatestDividend > ideal) {
145  if(dimensionSize % greatestDividend == 0) {
146  ideal = greatestDividend;
147  }
148 
149  greatestDividend --;
150  }
151  }
152 
153  return ideal;
154  }
155 
156 
164  return getDataStartByte() + getChunkIndex(chunk) * getBytesPerChunk();
165  }
166 }
Isis::SizeOf
int SizeOf(Isis::PixelType pixelType)
Returns the number of bytes of the specified PixelType.
Definition: PixelType.h:46
Isis::RawCubeChunk
A section of raw data on the disk.
Definition: RawCubeChunk.h:27
Isis::IException::Io
@ Io
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:155
Isis::PvlObject
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:61
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
QList< int >
Isis::CubeIoHandler::sampleCount
int sampleCount() const
Definition: CubeIoHandler.cpp:596
Isis::CubeIoHandler::pixelType
PixelType pixelType() const
Definition: CubeIoHandler.cpp:587
Isis::PvlContainer::addKeyword
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
Definition: PvlContainer.cpp:202
Isis::CubeIoHandler::getBytesPerChunk
BigInt getBytesPerChunk() const
Definition: CubeIoHandler.cpp:486
Isis::Pvl
Container for cube-like labels.
Definition: Pvl.h:119
Isis::RawCubeChunk::getRawData
QByteArray & getRawData() const
Definition: RawCubeChunk.h:38
Isis::CubeTileHandler::~CubeTileHandler
~CubeTileHandler()
Writes all data from memory to disk.
Definition: CubeTileHandler.cpp:56
Isis::CubeIoHandler::getDataFile
QFile * getDataFile()
Definition: CubeIoHandler.cpp:562
Isis::CubeIoHandler::getSampleCountInChunk
int getSampleCountInChunk() const
Definition: CubeIoHandler.cpp:604
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::CubeTileHandler::getTileStartByte
BigInt getTileStartByte(const RawCubeChunk &chunk) const
This is a helper method that goes from chunk to file position.
Definition: CubeTileHandler.cpp:163
Isis::RawCubeChunk::getByteCount
int getByteCount() const
Definition: RawCubeChunk.cpp:154
Isis::CubeIoHandler::lineCount
int lineCount() const
Definition: CubeIoHandler.cpp:571
Isis::CubeIoHandler::getDataStartByte
BigInt getDataStartByte() const
Definition: CubeIoHandler.cpp:551
Isis::PvlObject::hasKeyword
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:236
Isis::CubeIoHandler
Handles converting buffers to and from disk.
Definition: CubeIoHandler.h:109
Isis::BigInt
long long int BigInt
Big int.
Definition: Constants.h:49
Isis::PvlObject::findObject
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:274
Isis::CubeTileHandler::updateLabels
void updateLabels(Pvl &label)
Update the cube labels so that this cube indicates what tile size it used.
Definition: CubeTileHandler.cpp:66
Isis::CubeIoHandler::getChunkIndex
int getChunkIndex(const RawCubeChunk &) const
Given a chunk, what's its index in the file.
Definition: CubeIoHandler.cpp:530
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::RawCubeChunk::setRawData
void setRawData(QByteArray rawData)
Sets the chunk's raw data.
Definition: RawCubeChunk.cpp:101
Isis::CubeTileHandler::readRaw
virtual void readRaw(RawCubeChunk &chunkToFill)
This needs to populate the chunkToFill with unswapped raw bytes from the disk.
Definition: CubeTileHandler.cpp:77
std
Namespace for the standard library.
Isis::CubeIoHandler::clearCache
void clearCache(bool blockForWriteCache=true) const
Free all cube chunks (cached cube data) from memory and write them to disk.
Definition: CubeIoHandler.cpp:383
Isis::IString
Adds specific functionality to C++ strings.
Definition: IString.h:165
Isis::CubeIoHandler::setChunkSizes
void setChunkSizes(int numSamples, int numLines, int numBands)
This should be called once from the child constructor.
Definition: CubeIoHandler.cpp:621
Isis::CubeTileHandler::writeRaw
virtual void writeRaw(const RawCubeChunk &chunkToWrite)
This needs to write the chunkToWrite directly to disk with no modifications to the data itself.
Definition: CubeTileHandler.cpp:102
Isis::CubeIoHandler::getLineCountInChunk
int getLineCountInChunk() const
Definition: CubeIoHandler.cpp:579
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::CubeTileHandler::findGoodSize
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's dimensions (samp...
Definition: CubeTileHandler.cpp:135