E57 Foundation API v1.1.312
Aug. 10, 2011
|
example: creating SourceDestBuffers of numbers More...
Functions | |
int | main (int, char **) |
Example use of numeric SourceDestBuffers. |
example: creating SourceDestBuffers of numbers
Also see listing at end of this page for source without line numbers (to cut&paste from).
00001 /*** SourceDestBufferIntegerCreate.cpp example: creating SourceDestBuffers of numbers */ 00004 #include <iostream> 00005 #include "E57Foundation.h" 00006 using namespace e57; 00007 using namespace std; 00008 00010 int main(int /*argc*/, char** /*argv*/) { 00011 const unsigned N = 4; 00012 00013 try { 00014 ImageFile imf("temp._e57", "w"); 00015 StructureNode root = imf.root(); 00016 00017 StructureNode prototype(imf); 00018 prototype.set("i8", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); 00019 prototype.set("u8", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); 00020 prototype.set("i16", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); 00021 prototype.set("u16", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); 00022 prototype.set("i32", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); 00023 prototype.set("u32", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); 00024 prototype.set("i64", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); 00025 prototype.set("i64Scaled", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); 00026 prototype.set("f", FloatNode(imf, 0.0, E57_SINGLE)); 00027 prototype.set("d", FloatNode(imf, 0.0, E57_DOUBLE)); 00028 00029 VectorNode codecs(imf, true); 00030 00031 CompressedVectorNode cv(imf, prototype, codecs); 00032 root.set("cv1", cv); 00033 00034 static int8_t i8[N] = {1,2,3,4}; 00035 static uint8_t u8[N] = {2,3,4,5}; 00036 static int16_t i16[N] = {3,4,5,6}; 00037 static uint16_t u16[N] = {4,5,6,7}; 00038 static int32_t i32[N] = {5,6,7,8}; 00039 static uint32_t u32[N] = {6,7,8,9}; 00040 static int64_t i64[N] = {7,8,9,10}; 00041 static float f[N] = {8.1F,9.1F,10.1F,11.1F}; 00042 static double d[N] = {9.1,10.1,11.1,12.1}; 00043 vector<SourceDestBuffer> sbufs; 00044 sbufs.push_back(SourceDestBuffer(imf, "i8", i8, N)); 00045 sbufs.push_back(SourceDestBuffer(imf, "u8", u8, N)); 00046 sbufs.push_back(SourceDestBuffer(imf, "i16", i16, N)); 00047 sbufs.push_back(SourceDestBuffer(imf, "u16", u16, N)); 00048 sbufs.push_back(SourceDestBuffer(imf, "i32", i32, N)); 00049 sbufs.push_back(SourceDestBuffer(imf, "u32", u32, N)); 00050 sbufs.push_back(SourceDestBuffer(imf, "i64", i64, N)); 00051 sbufs.push_back(SourceDestBuffer(imf, "i64Scaled", i64, N, false, true)); 00052 sbufs.push_back(SourceDestBuffer(imf, "f", f, N)); 00053 sbufs.push_back(SourceDestBuffer(imf, "d", d, N)); 00054 00055 { 00056 CompressedVectorWriter writer = cv.writer(sbufs); 00057 cout << "Writing " << N << " records to " << writer.compressedVectorNode().pathName() << endl; 00058 writer.write(N); 00059 writer.close(); // don't forget to explicitly close the CompressedVectorWriter 00060 } 00061 00062 imf.close(); // don't forget to explicitly close the ImageFile 00063 } catch(E57Exception& ex) { 00064 ex.report(__FILE__, __LINE__, __FUNCTION__); 00065 return(-1); 00066 } 00067 00068 try { 00069 ImageFile imf("temp._e57", "r"); 00070 StructureNode root = imf.root(); 00071 00072 CompressedVectorNode cv = static_cast<CompressedVectorNode> (root.get("cv1")); 00073 00074 cout << cv.pathName() << " has " << cv.childCount() << " child elements" << endl; 00075 00076 if (cv.prototype().type() != E57_STRUCTURE) 00077 return(-1); 00078 StructureNode prototype = static_cast<StructureNode>(cv.prototype()); 00079 cout << "The prototype has " << prototype.childCount() << " child elements" << endl; 00080 00081 VectorNode codecs(cv.codecs()); 00082 cout << "The codecs has " << codecs.childCount() << " child elements" << endl; 00083 00084 for (int i = 0; i < prototype.childCount(); i++) { 00085 Node n = prototype.get(i); 00086 00087 cout << "reading " << n.pathName() << ":" << endl; 00088 try { 00089 static double rawValue[N]; 00090 vector<SourceDestBuffer> dbufs1; 00091 dbufs1.push_back(SourceDestBuffer(imf, n.pathName(), rawValue, N, true, false)); 00092 00093 CompressedVectorReader reader = cv.reader(dbufs1); 00094 if (reader.read() != N) 00095 return(-1); 00096 cout << " read " << N << " records from " << reader.compressedVectorNode().pathName() << endl; 00097 00098 for (unsigned j = 0; j < N; j++) 00099 cout << " rawValue[" << j << "] = " << rawValue[j] << endl; 00100 reader.close(); // don't forget to explicitly close the CompressedVectorReader 00101 } catch(E57Exception& ex) { 00102 ex.report(__FILE__, __LINE__, __FUNCTION__); 00103 cout << "**** Reading " << n.pathName() << " failed" << endl; 00104 } 00105 00106 try { 00107 static double scaledValue[N]; 00108 vector<SourceDestBuffer> dbufs2; 00109 dbufs2.push_back(SourceDestBuffer(imf, n.pathName(), scaledValue, N, true, true)); 00110 00111 CompressedVectorReader reader = cv.reader(dbufs2); 00112 if (reader.read() != N) 00113 return(-1); 00114 for (unsigned j = 0; j < N; j++) 00115 cout << " scaledValue[" << j << "] = " << scaledValue[j] << endl; 00116 reader.close(); // don't forget to explicitly close the CompressedVectorReader 00117 } catch(E57Exception& /*ex*/) { 00118 cout << "**** Reading " << n.pathName() << " failed" << endl; 00119 } 00120 } 00121 00122 imf.close(); // don't forget to explicitly close the ImageFile 00123 } catch(E57Exception& ex) { 00124 ex.report(__FILE__, __LINE__, __FUNCTION__); 00125 return(-1); 00126 } 00127 return(0); 00128 }
This example program illustrates the writing and reading of numbers (IntegerNode, ScaledIntegerNode, and FloatNode) in a CompressedVectorNode. See the HelloWorld.cpp example for discussion of the use of include files, constructing an ImageFile, and the try/catch block to handle exceptions. See CompressedVectorCreate.cpp example for discussion about the writing/reading a CompressedVectorNode.
Source lines 18-27 create a CompressedVectorNode prototype with eight ScaledIntegerNodes and two FloatNodes. The ScaledIntegerNode rawValues are constrained to be in interval [-10000, 10000], and are scaled by a factor of 1/1000 with no offset. One FloatNode is a double precision and the other is single precision, both with no constrain on the value.
Source lines 34-53 prepare a vector of eight SourceDestBuffers holding integer of various numbers of bytes and signed/unsigned combinations, as well as two SourceDestBuffers of single and double precision floating point numbers. Note that the buffer declared in source line 40, i64
, is read twice: once with no scaling requested (the default value on source line 50), and once with scaling requested (on source line 51).
The CompressedVectorNode is written in source lines 56-59. The file is then reopened on source line 69. The CompressedVectorNode is then fetched by name (with no checking) in source line 72. In source line 78, the prototype is fetched from the CompressedVectorNode (with a little checking on source lines 76-77).
A for loop on source line 84 iterates through the prototype children, and reads each field twice, printing the results. The first read is with no scaling requested in source line 93. The second read is with scaling requested in source line 111. Note on both reads, doConversion
is true, which requests that the file representation be converted automatically to the memory buffer representation. This is necessary, since the read memory buffer is a double
, and eight of the ten children are ScaledIntegerNodes. Requesting conversion is not necessary in the case of the two FloatNode children, but doesn't hurt.
The following observations can be made about the results printed in the output listing below:
The seven ScaledIntegerNodes that were written without any scaling all work about the same (in output lines 5-60). The raw value is stored in the file, and is read back the same. The scaled read produces a scaled value that is multiplied by 0.001.
The ScaledIntegerNode that was written with scaling requested, has a raw value that is 1000 times larger than the number stored in the memory buffer (in output lines 77-80). This is because "scaling" for a writer means dividing by the scale
factor before storing the result. Note that the raw value for the last point of the buffer where write scaling was requested had a value of 10000, which was just at the edge of the maximum allowed value. If a slightly larger value was written with scaling requested in this example, an E57Exception with an errorCode of E57_ERROR_VALUE_OUT_OF_BOUNDS would have been thrown. Whether you perform the scaling yourself, or request the E57 Foundation to do it, you should make sure that result is within the declared bounds. This is necessary because the coding schemes that store the representation in the file cannot (and will not) reliably store any value outside of the declared [minimum, maximum] interval.
The scaling options have no effect on the FloatNodes, as reflected by the output lines 79-93.
The following console output is produced:
The XML section of the temp._e57
E57 file produced by this example program is as follows:
Here is the source code without line numbers to cut&paste from:
/*** SourceDestBufferIntegerCreate.cpp example: creating SourceDestBuffers of numbers */ #include <iostream> #include "E57Foundation.h" using namespace e57; using namespace std; int main(int /*argc*/, char** /*argv*/) { const unsigned N = 4; try { ImageFile imf("temp._e57", "w"); StructureNode root = imf.root(); StructureNode prototype(imf); prototype.set("i8", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); prototype.set("u8", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); prototype.set("i16", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); prototype.set("u16", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); prototype.set("i32", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); prototype.set("u32", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); prototype.set("i64", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); prototype.set("i64Scaled", ScaledIntegerNode(imf, 0, -10000, 10000, 0.001, 0.0)); prototype.set("f", FloatNode(imf, 0.0, E57_SINGLE)); prototype.set("d", FloatNode(imf, 0.0, E57_DOUBLE)); VectorNode codecs(imf, true); CompressedVectorNode cv(imf, prototype, codecs); root.set("cv1", cv); static int8_t i8[N] = {1,2,3,4}; static uint8_t u8[N] = {2,3,4,5}; static int16_t i16[N] = {3,4,5,6}; static uint16_t u16[N] = {4,5,6,7}; static int32_t i32[N] = {5,6,7,8}; static uint32_t u32[N] = {6,7,8,9}; static int64_t i64[N] = {7,8,9,10}; static float f[N] = {8.1F,9.1F,10.1F,11.1F}; static double d[N] = {9.1,10.1,11.1,12.1}; vector<SourceDestBuffer> sbufs; sbufs.push_back(SourceDestBuffer(imf, "i8", i8, N)); sbufs.push_back(SourceDestBuffer(imf, "u8", u8, N)); sbufs.push_back(SourceDestBuffer(imf, "i16", i16, N)); sbufs.push_back(SourceDestBuffer(imf, "u16", u16, N)); sbufs.push_back(SourceDestBuffer(imf, "i32", i32, N)); sbufs.push_back(SourceDestBuffer(imf, "u32", u32, N)); sbufs.push_back(SourceDestBuffer(imf, "i64", i64, N)); sbufs.push_back(SourceDestBuffer(imf, "i64Scaled", i64, N, false, true)); sbufs.push_back(SourceDestBuffer(imf, "f", f, N)); sbufs.push_back(SourceDestBuffer(imf, "d", d, N)); { CompressedVectorWriter writer = cv.writer(sbufs); cout << "Writing " << N << " records to " << writer.compressedVectorNode().pathName() << endl; writer.write(N); writer.close(); // don't forget to explicitly close the CompressedVectorWriter } imf.close(); // don't forget to explicitly close the ImageFile } catch(E57Exception& ex) { ex.report(__FILE__, __LINE__, __FUNCTION__); return(-1); } try { ImageFile imf("temp._e57", "r"); StructureNode root = imf.root(); CompressedVectorNode cv = static_cast<CompressedVectorNode> (root.get("cv1")); cout << cv.pathName() << " has " << cv.childCount() << " child elements" << endl; if (cv.prototype().type() != E57_STRUCTURE) return(-1); StructureNode prototype = static_cast<StructureNode>(cv.prototype()); cout << "The prototype has " << prototype.childCount() << " child elements" << endl; VectorNode codecs(cv.codecs()); cout << "The codecs has " << codecs.childCount() << " child elements" << endl; for (int i = 0; i < prototype.childCount(); i++) { Node n = prototype.get(i); cout << "reading " << n.pathName() << ":" << endl; try { static double rawValue[N]; vector<SourceDestBuffer> dbufs1; dbufs1.push_back(SourceDestBuffer(imf, n.pathName(), rawValue, N, true, false)); CompressedVectorReader reader = cv.reader(dbufs1); if (reader.read() != N) return(-1); cout << " read " << N << " records from " << reader.compressedVectorNode().pathName() << endl; for (unsigned j = 0; j < N; j++) cout << " rawValue[" << j << "] = " << rawValue[j] << endl; reader.close(); // don't forget to explicitly close the CompressedVectorReader } catch(E57Exception& ex) { ex.report(__FILE__, __LINE__, __FUNCTION__); cout << "**** Reading " << n.pathName() << " failed" << endl; } try { static double scaledValue[N]; vector<SourceDestBuffer> dbufs2; dbufs2.push_back(SourceDestBuffer(imf, n.pathName(), scaledValue, N, true, true)); CompressedVectorReader reader = cv.reader(dbufs2); if (reader.read() != N) return(-1); for (unsigned j = 0; j < N; j++) cout << " scaledValue[" << j << "] = " << scaledValue[j] << endl; reader.close(); // don't forget to explicitly close the CompressedVectorReader } catch(E57Exception& /*ex*/) { cout << "**** Reading " << n.pathName() << " failed" << endl; } } imf.close(); // don't forget to explicitly close the ImageFile } catch(E57Exception& ex) { ex.report(__FILE__, __LINE__, __FUNCTION__); return(-1); } return(0); }