Line data Source code
1 : /******************************************************************************
2 : *
3 : * Purpose: Block directory API.
4 : *
5 : ******************************************************************************
6 : * Copyright (c) 2011
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 "blockdir/blocktiledir.h"
29 : #include "blockdir/blocktilelayer.h"
30 : #include "blockdir/blockfile.h"
31 : #include "core/pcidsk_utils.h"
32 : #include <cassert>
33 :
34 : using namespace PCIDSK;
35 :
36 : /************************************************************************/
37 : /* BlockTileDir() */
38 : /************************************************************************/
39 :
40 : /**
41 : * Constructor.
42 : *
43 : * @param poFile The associated file object.
44 : * @param nSegment The segment of the block directory.
45 : */
46 24 : BlockTileDir::BlockTileDir(BlockFile * poFile, uint16 nSegment)
47 24 : : BlockDir(poFile, nSegment)
48 : {
49 24 : }
50 :
51 : /************************************************************************/
52 : /* BlockTileDir() */
53 : /************************************************************************/
54 :
55 : /**
56 : * Constructor.
57 : *
58 : * @param poFile The associated file object.
59 : * @param nSegment The segment of the block directory.
60 : * @param nVersion The version of the block directory.
61 : */
62 8 : BlockTileDir::BlockTileDir(BlockFile * poFile, uint16 nSegment, uint16 nVersion)
63 8 : : BlockDir(poFile, nSegment, nVersion)
64 : {
65 8 : }
66 :
67 : /************************************************************************/
68 : /* ~BlockTileDir() */
69 : /************************************************************************/
70 :
71 : /**
72 : * Destructor.
73 : */
74 32 : BlockTileDir::~BlockTileDir(void)
75 : {
76 32 : assert(moLayerInfoList.size() == moTileLayerInfoList.size());
77 :
78 68 : for (auto poIter : moLayerInfoList)
79 36 : delete poIter;
80 :
81 68 : for (auto poIter : moTileLayerInfoList)
82 36 : delete poIter;
83 32 : }
84 :
85 : /************************************************************************/
86 : /* GetTileLayer() */
87 : /************************************************************************/
88 :
89 : /**
90 : * Gets the block tile layer at the specified index.
91 : *
92 : * @param iLayer The index of the block tile layer.
93 : *
94 : * @return The block tile layer at the specified index.
95 : */
96 44 : BlockTileLayer * BlockTileDir::GetTileLayer(uint32 iLayer)
97 : {
98 44 : return dynamic_cast<BlockTileLayer *>(GetLayer(iLayer));
99 : }
100 :
101 : /************************************************************************/
102 : /* */
103 : /************************************************************************/
104 :
105 : /**
106 : * Gets the number of new blocks to create.
107 : *
108 : * @return The number of new blocks to create.
109 : */
110 8 : uint32 BlockTileDir::GetNewBlockCount(void) const
111 : {
112 8 : return (uint32) ((unsigned long long)(mpoFile->GetImageFileSize() / GetBlockSize()) * 0.01);
113 : }
114 :
115 : /************************************************************************/
116 : /* SwapBlockLayer() */
117 : /************************************************************************/
118 :
119 : /**
120 : * Swaps the specified block layer info.
121 : *
122 : * @param psBlockLayer The block layer info to swap.
123 : */
124 40 : void BlockTileDir::SwapBlockLayer(BlockLayerInfo * psBlockLayer)
125 : {
126 40 : if (!mbNeedsSwap)
127 40 : return;
128 :
129 0 : SwapData(&psBlockLayer->nLayerType, 2, 1);
130 0 : SwapData(&psBlockLayer->nStartBlock, 4, 1);
131 0 : SwapData(&psBlockLayer->nBlockCount, 4, 1);
132 0 : SwapData(&psBlockLayer->nLayerSize, 8, 1);
133 : }
134 :
135 : /************************************************************************/
136 : /* SwapTileLayer() */
137 : /************************************************************************/
138 :
139 : /**
140 : * Swaps the specified tile layer info.
141 : *
142 : * @param psTileLayer The tile layer info to swap.
143 : */
144 31 : void BlockTileDir::SwapTileLayer(TileLayerInfo * psTileLayer)
145 : {
146 31 : if (!mbNeedsSwap)
147 31 : return;
148 :
149 0 : SwapData(&psTileLayer->nXSize, 4, 1);
150 0 : SwapData(&psTileLayer->nYSize, 4, 1);
151 0 : SwapData(&psTileLayer->nTileXSize, 4, 1);
152 0 : SwapData(&psTileLayer->nTileYSize, 4, 1);
153 0 : SwapData(&psTileLayer->bNoDataValid, 2, 1);
154 0 : SwapData(&psTileLayer->dfNoDataValue, 8, 1);
155 : }
156 :
157 : /************************************************************************/
158 : /* SwapBlock() */
159 : /************************************************************************/
160 :
161 : /**
162 : * Swaps the specified block info array.
163 : *
164 : * @param psBlock The block info array to swap.
165 : * @param nCount The number of block info.
166 : */
167 19 : void BlockTileDir::SwapBlock(BlockInfo * psBlock, size_t nCount)
168 : {
169 19 : if (!mbNeedsSwap)
170 19 : return;
171 :
172 0 : for (BlockInfo * psEnd = psBlock + nCount;
173 0 : psBlock < psEnd; psBlock++)
174 : {
175 0 : SwapData(&psBlock->nSegment, 2, 1);
176 0 : SwapData(&psBlock->nStartBlock, 4, 1);
177 : }
178 : }
|