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 : * SPDX-License-Identifier: MIT 10 : ****************************************************************************/ 11 : 12 : #include "pcidsk_exception.h" 13 : #include "segment/cpcidskblut.h" 14 : #include <cassert> 15 : #include <cstring> 16 : #include <sstream> 17 : #include <cmath> 18 : #include <stdlib.h> 19 : 20 : using namespace PCIDSK; 21 : 22 : PCIDSK_BLUT::~PCIDSK_BLUT() = default; 23 : 24 : /************************************************************************/ 25 : /* CPCIDSK_BLUT() */ 26 : /************************************************************************/ 27 0 : CPCIDSK_BLUT::CPCIDSK_BLUT( PCIDSKFile *fileIn, int segmentIn, 28 0 : const char *segment_pointer ) 29 0 : : CPCIDSKSegment( fileIn, segmentIn, segment_pointer ) 30 : 31 : { 32 0 : } 33 : 34 : /************************************************************************/ 35 : /* ~CPCIDSK_BLUT() */ 36 : /************************************************************************/ 37 0 : CPCIDSK_BLUT::~CPCIDSK_BLUT() 38 : 39 : { 40 0 : } 41 : 42 : /************************************************************************/ 43 : /* ReadBLUT() */ 44 : /************************************************************************/ 45 0 : void CPCIDSK_BLUT::ReadBLUT( std::vector<BLUTEntry>& vBLUT ) 46 : 47 : { 48 0 : PCIDSKBuffer seg_data; 49 : 50 0 : seg_data.SetSize( (int) GetContentSize() ); 51 : 52 0 : ReadFromFile( seg_data.buffer, 0, seg_data.buffer_size ); 53 : 54 0 : std::istringstream ss(seg_data.buffer); 55 : 56 0 : vBLUT.clear(); 57 : 58 : // the first token is interpolation type (not used) 59 : std::size_t nInterp; 60 0 : if(!(ss >> nInterp)) 61 0 : throw PCIDSKException("Invalid BLUT segment."); 62 : 63 : // the second token is the number of entries 64 : std::size_t nCount; 65 0 : if(!(ss >> nCount) || nCount > 1024 * 1024 /* arbitrary limit */) 66 0 : throw PCIDSKException("Invalid BLUT segment."); 67 : 68 0 : for(std::size_t n=0; n<nCount; ++n) 69 : { 70 0 : BLUTEntry oEntry; 71 : 72 0 : if(!(ss >> oEntry.first)) 73 0 : throw PCIDSKException("Invalid BLUT segment."); 74 : 75 0 : if(!(ss >> oEntry.second)) 76 0 : throw PCIDSKException("Invalid BLUT segment."); 77 : 78 0 : vBLUT.push_back(oEntry); 79 : 80 : } 81 0 : } 82 : 83 : /************************************************************************/ 84 : /* WriteBLUT() */ 85 : /************************************************************************/ 86 0 : void CPCIDSK_BLUT::WriteBLUT( const std::vector<BLUTEntry>& vBLUT ) 87 : 88 : { 89 0 : std::stringstream oSS; 90 : 91 0 : oSS << INTERP_LINEAR << " " << vBLUT.size(); 92 0 : oSS.precision(15); 93 : 94 0 : for(std::vector<BLUTEntry>::const_iterator it = vBLUT.begin(); 95 0 : it != vBLUT.end(); 96 0 : ++it) 97 : { 98 0 : if(it->first == std::floor(it->first)) 99 0 : oSS << " " << (int) it->first; 100 : else 101 0 : oSS << " " << it->first; 102 : 103 0 : if(it->second == std::floor(it->second)) 104 0 : oSS << " " << (int) it->second; 105 : else 106 0 : oSS << " " << it->second; 107 : } 108 : 109 0 : std::string sData = oSS.str(); 110 0 : WriteToFile( sData.c_str(), 0, sData.size() ); 111 0 : }