1 /*
2  *  keaoverview.cpp
3  *
4  *  Created by Pete Bunting on 01/08/2012.
5  *  Copyright 2012 LibKEA. All rights reserved.
6  *
7  *  This file is part of LibKEA.
8  *
9  *  Permission is hereby granted, free of charge, to any person
10  *  obtaining a copy of this software and associated documentation
11  *  files (the "Software"), to deal in the Software without restriction,
12  *  including without limitation the rights to use, copy, modify,
13  *  merge, publish, distribute, sublicense, and/or sell copies of the
14  *  Software, and to permit persons to whom the Software is furnished
15  *  to do so, subject to the following conditions:
16  *
17  *  The above copyright notice and this permission notice shall be
18  *  included in all copies or substantial portions of the Software.
19  *
20  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22  *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
24  *  ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25  *  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  */
29 
30 #include "keaoverview.h"
31 
32 CPL_CVSID("$Id: keaoverview.cpp 980fee897f6fd8cf10fa0f62936cca216cd76cf7 2020-04-03 17:54:46 +1000 Sam Gillingham $")
33 
34 // constructor
KEAOverview(KEADataset * pDataset,int nSrcBand,GDALAccess eAccessIn,kealib::KEAImageIO * pImageIO,LockedRefCount * pRefCount,int nOverviewIndex,uint64_t nXSize,uint64_t nYSize)35 KEAOverview::KEAOverview(KEADataset *pDataset, int nSrcBand, GDALAccess eAccessIn,
36                 kealib::KEAImageIO *pImageIO, LockedRefCount *pRefCount,
37                 int nOverviewIndex, uint64_t nXSize, uint64_t nYSize)
38  : KEARasterBand( pDataset, nSrcBand, eAccessIn, pImageIO, pRefCount )
39 {
40     this->m_nOverviewIndex = nOverviewIndex;
41     // overridden from the band - not the same size as the band obviously
42     this->nBlockXSize = pImageIO->getOverviewBlockSize(nSrcBand, nOverviewIndex);
43     this->nBlockYSize = pImageIO->getOverviewBlockSize(nSrcBand, nOverviewIndex);
44     this->nRasterXSize = static_cast<int>(nXSize);
45     this->nRasterYSize = static_cast<int>(nYSize);
46 }
47 
~KEAOverview()48 KEAOverview::~KEAOverview() {}
49 
50 // overridden implementation - calls readFromOverview instead
IReadBlock(int nBlockXOff,int nBlockYOff,void * pImage)51 CPLErr KEAOverview::IReadBlock( int nBlockXOff, int nBlockYOff, void * pImage )
52 {
53     try
54     {
55         // GDAL deals in blocks - if we are at the end of a row
56         // we need to adjust the amount read so we don't go over the edge
57         int nxsize = this->nBlockXSize;
58         int nxtotalsize = this->nBlockXSize * (nBlockXOff + 1);
59         if( nxtotalsize > this->nRasterXSize )
60         {
61             nxsize -= (nxtotalsize - this->nRasterXSize);
62         }
63         int nysize = this->nBlockYSize;
64         int nytotalsize = this->nBlockYSize * (nBlockYOff + 1);
65         if( nytotalsize > this->nRasterYSize )
66         {
67             nysize -= (nytotalsize - this->nRasterYSize);
68         }
69         this->m_pImageIO->readFromOverview( this->nBand, this->m_nOverviewIndex,
70                                             pImage, this->nBlockXSize * nBlockXOff,
71                                             this->nBlockYSize * nBlockYOff,
72                                             nxsize, nysize, this->nBlockXSize, this->nBlockYSize,
73                                             this->m_eKEADataType );
74         return CE_None;
75     }
76     catch (kealib::KEAIOException &e)
77     {
78         CPLError( CE_Failure, CPLE_AppDefined,
79                 "Failed to read file: %s", e.what() );
80         return CE_Failure;
81     }
82 }
83 
84 // overridden implementation - calls writeToOverview instead
IWriteBlock(int nBlockXOff,int nBlockYOff,void * pImage)85 CPLErr KEAOverview::IWriteBlock( int nBlockXOff, int nBlockYOff, void * pImage )
86 {
87     try
88     {
89         // GDAL deals in blocks - if we are at the end of a row
90         // we need to adjust the amount written so we don't go over the edge
91         int nxsize = this->nBlockXSize;
92         int nxtotalsize = this->nBlockXSize * (nBlockXOff + 1);
93         if( nxtotalsize > this->nRasterXSize )
94         {
95             nxsize -= (nxtotalsize - this->nRasterXSize);
96         }
97         int nysize = this->nBlockYSize;
98         int nytotalsize = this->nBlockYSize * (nBlockYOff + 1);
99         if( nytotalsize > this->nRasterYSize )
100         {
101             nysize -= (nytotalsize - this->nRasterYSize);
102         }
103 
104         this->m_pImageIO->writeToOverview( this->nBand, this->m_nOverviewIndex,
105                                             pImage, this->nBlockXSize * nBlockXOff,
106                                             this->nBlockYSize * nBlockYOff,
107                                             nxsize, nysize, this->nBlockXSize, this->nBlockYSize,
108                                             this->m_eKEADataType );
109         return CE_None;
110     }
111     catch (kealib::KEAIOException &e)
112     {
113         CPLError( CE_Failure, CPLE_AppDefined,
114                 "Failed to write file: %s", e.what() );
115         return CE_Failure;
116     }
117 }
118 
GetDefaultRAT()119 GDALRasterAttributeTable *KEAOverview::GetDefaultRAT()
120 {
121     // KEARasterBand implements this, but we don't want to
122     return nullptr;
123 }
124 
SetDefaultRAT(CPL_UNUSED const GDALRasterAttributeTable * poRAT)125 CPLErr KEAOverview::SetDefaultRAT(CPL_UNUSED const GDALRasterAttributeTable *poRAT)
126 {
127     // KEARasterBand implements this, but we don't want to
128     return CE_Failure;
129 }
130