Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Purpose: Implementation of the CPCIDSK_BPCT 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/cpcidskbpct.h" 30 : #include <cassert> 31 : #include <cstring> 32 : #include <sstream> 33 : #include <cmath> 34 : #include <stdlib.h> 35 : #include "core/pcidsk_utils.h" 36 : 37 : using namespace PCIDSK; 38 : 39 : /************************************************************************/ 40 : /* CPCIDSK_BPCT() */ 41 : /************************************************************************/ 42 0 : CPCIDSK_BPCT::CPCIDSK_BPCT( PCIDSKFile *fileIn, int segmentIn, 43 0 : const char *segment_pointer ) 44 0 : : CPCIDSKSegment( fileIn, segmentIn, segment_pointer ) 45 : 46 : { 47 0 : } 48 : 49 : /************************************************************************/ 50 : /* ~CPCIDSK_BPCT() */ 51 : /************************************************************************/ 52 0 : CPCIDSK_BPCT::~CPCIDSK_BPCT() 53 : 54 : { 55 0 : } 56 : 57 : /************************************************************************/ 58 : /* ReadBPCT() */ 59 : /************************************************************************/ 60 0 : void CPCIDSK_BPCT::ReadBPCT( std::vector<BPCTEntry>& vBPCT ) 61 : 62 : { 63 0 : PCIDSKBuffer seg_data; 64 : 65 0 : seg_data.SetSize( (int) GetContentSize() ); 66 : 67 0 : ReadFromFile( seg_data.buffer, 0, seg_data.buffer_size ); 68 : 69 0 : std::istringstream ss(seg_data.buffer); 70 : 71 0 : vBPCT.clear(); 72 : 73 : // the first token is interpolation type (not used) 74 : std::size_t nInterp; 75 0 : if(!(ss >> nInterp)) 76 0 : throw PCIDSKException("Invalid BPCT segment."); 77 : 78 : // the second token is the number of entries 79 : std::size_t nCount; 80 0 : if(!(ss >> nCount)|| nCount > 1024 * 1024 /* arbitrary limit */) 81 0 : throw PCIDSKException("Invalid BPCT segment."); 82 : 83 : 84 0 : for(std::size_t n=0; n<nCount; ++n) 85 : { 86 0 : BPCTEntry oEntry; 87 : 88 0 : if(!(ss >> oEntry.boundary)) 89 0 : throw PCIDSKException("Invalid BPCT segment."); 90 : 91 : int nTemp; 92 0 : if(!(ss >> nTemp) || nTemp < 0 || nTemp > 255) 93 0 : throw PCIDSKException("Invalid BPCT segment."); 94 0 : oEntry.red = (unsigned char) nTemp; 95 : 96 0 : if(!(ss >> nTemp) || nTemp < 0 || nTemp > 255) 97 0 : throw PCIDSKException("Invalid BPCT segment."); 98 0 : oEntry.green = (unsigned char) nTemp; 99 : 100 0 : if(!(ss >> nTemp) || nTemp < 0 || nTemp > 255) 101 0 : throw PCIDSKException("Invalid BPCT segment."); 102 0 : oEntry.blue = (unsigned char) nTemp; 103 : 104 0 : vBPCT.push_back(oEntry); 105 : 106 : } 107 0 : } 108 : 109 : /************************************************************************/ 110 : /* WriteBPCT() */ 111 : /************************************************************************/ 112 0 : void CPCIDSK_BPCT::WriteBPCT( const std::vector<BPCTEntry>& vBPCT ) 113 : 114 : { 115 0 : std::stringstream oSS; 116 : 117 0 : oSS << INTERP_LINEAR << " " << vBPCT.size(); 118 0 : oSS.precision(15); 119 : 120 0 : for(std::vector<BPCTEntry>::const_iterator it = vBPCT.begin(); 121 0 : it != vBPCT.end(); 122 0 : ++it) 123 : { 124 0 : if(it->boundary == std::floor(it->boundary)) 125 0 : oSS << " " << (int) it->boundary; 126 : else 127 0 : oSS << " " << it->boundary; 128 0 : oSS << " " << (unsigned int) it->red; 129 0 : oSS << " " << (unsigned int) it->green; 130 0 : oSS << " " << (unsigned int) it->blue; 131 : } 132 : 133 0 : std::string sData = oSS.str(); 134 0 : WriteToFile( sData.c_str(), 0, sData.size() ); 135 0 : }