Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Purpose: Implementation of the CPCIDSK_BLUT class. 4 : * 5 : ****************************************************************************** 6 : * Copyright (c) 2015 7 : * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada. 8 : * 9 : * Permission is hereby granted, free of charge, to any person obtaining a 10 : * copy of this software and associated documentation files (the "Software"), 11 : * to deal in the Software without restriction, including without limitation 12 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13 : * and/or sell copies of the Software, and to permit persons to whom the 14 : * Software is furnished to do so, subject to the following conditions: 15 : * 16 : * The above copyright notice and this permission notice shall be included 17 : * in all copies or substantial portions of the Software. 18 : * 19 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 : * DEALINGS IN THE SOFTWARE. 26 : ****************************************************************************/ 27 : 28 : #include "pcidsk_exception.h" 29 : #include "segment/cpcidskblut.h" 30 : #include <cassert> 31 : #include <cstring> 32 : #include <sstream> 33 : #include <cmath> 34 : #include <stdlib.h> 35 : 36 : using namespace PCIDSK; 37 : 38 : /************************************************************************/ 39 : /* CPCIDSK_BLUT() */ 40 : /************************************************************************/ 41 0 : CPCIDSK_BLUT::CPCIDSK_BLUT( PCIDSKFile *fileIn, int segmentIn, 42 0 : const char *segment_pointer ) 43 0 : : CPCIDSKSegment( fileIn, segmentIn, segment_pointer ) 44 : 45 : { 46 0 : } 47 : 48 : /************************************************************************/ 49 : /* ~CPCIDSK_BLUT() */ 50 : /************************************************************************/ 51 0 : CPCIDSK_BLUT::~CPCIDSK_BLUT() 52 : 53 : { 54 0 : } 55 : 56 : /************************************************************************/ 57 : /* ReadBLUT() */ 58 : /************************************************************************/ 59 0 : void CPCIDSK_BLUT::ReadBLUT( std::vector<BLUTEntry>& vBLUT ) 60 : 61 : { 62 0 : PCIDSKBuffer seg_data; 63 : 64 0 : seg_data.SetSize( (int) GetContentSize() ); 65 : 66 0 : ReadFromFile( seg_data.buffer, 0, seg_data.buffer_size ); 67 : 68 0 : std::istringstream ss(seg_data.buffer); 69 : 70 0 : vBLUT.clear(); 71 : 72 : // the first token is interpolation type (not used) 73 : std::size_t nInterp; 74 0 : if(!(ss >> nInterp)) 75 0 : throw PCIDSKException("Invalid BLUT segment."); 76 : 77 : // the second token is the number of entries 78 : std::size_t nCount; 79 0 : if(!(ss >> nCount) || nCount > 1024 * 1024 /* arbitrary limit */) 80 0 : throw PCIDSKException("Invalid BLUT segment."); 81 : 82 0 : for(std::size_t n=0; n<nCount; ++n) 83 : { 84 0 : BLUTEntry oEntry; 85 : 86 0 : if(!(ss >> oEntry.first)) 87 0 : throw PCIDSKException("Invalid BLUT segment."); 88 : 89 0 : if(!(ss >> oEntry.second)) 90 0 : throw PCIDSKException("Invalid BLUT segment."); 91 : 92 0 : vBLUT.push_back(oEntry); 93 : 94 : } 95 0 : } 96 : 97 : /************************************************************************/ 98 : /* WriteBLUT() */ 99 : /************************************************************************/ 100 0 : void CPCIDSK_BLUT::WriteBLUT( const std::vector<BLUTEntry>& vBLUT ) 101 : 102 : { 103 0 : std::stringstream oSS; 104 : 105 0 : oSS << INTERP_LINEAR << " " << vBLUT.size(); 106 0 : oSS.precision(15); 107 : 108 0 : for(std::vector<BLUTEntry>::const_iterator it = vBLUT.begin(); 109 0 : it != vBLUT.end(); 110 0 : ++it) 111 : { 112 0 : if(it->first == std::floor(it->first)) 113 0 : oSS << " " << (int) it->first; 114 : else 115 0 : oSS << " " << it->first; 116 : 117 0 : if(it->second == std::floor(it->second)) 118 0 : oSS << " " << (int) it->second; 119 : else 120 0 : oSS << " " << it->second; 121 : } 122 : 123 0 : std::string sData = oSS.str(); 124 0 : WriteToFile( sData.c_str(), 0, sData.size() ); 125 0 : }