Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GeoTIFF Driver
4 : * Purpose: GDAL GeoTIFF support.
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 1998, 2002, Frank Warmerdam <warmerdam@pobox.com>
9 : * Copyright (c) 2007-2015, Even Rouault <even dot rouault at spatialys dot com>
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #ifndef FETCHBUFFERDIRECTIO_H_INCLUDED
31 : #define FETCHBUFFERDIRECTIO_H_INCLUDED
32 :
33 : #include "cpl_error.h"
34 : #include "cpl_vsi.h"
35 : #include "gdal.h"
36 :
37 : /************************************************************************/
38 : /* FetchBufferDirectIO */
39 : /************************************************************************/
40 :
41 : class FetchBufferDirectIO final
42 : {
43 : VSILFILE *fp;
44 : GByte *pTempBuffer;
45 : size_t nTempBufferSize;
46 :
47 : public:
48 613 : FetchBufferDirectIO(VSILFILE *fpIn, GByte *pTempBufferIn,
49 : size_t nTempBufferSizeIn)
50 613 : : fp(fpIn), pTempBuffer(pTempBufferIn),
51 613 : nTempBufferSize(nTempBufferSizeIn)
52 : {
53 613 : }
54 :
55 9595 : const GByte *FetchBytes(vsi_l_offset nOffset, int nPixels, int nDTSize,
56 : bool bIsByteSwapped, bool bIsComplex, int nBlockId)
57 : {
58 9595 : if (!FetchBytes(pTempBuffer, nOffset, nPixels, nDTSize, bIsByteSwapped,
59 : bIsComplex, nBlockId))
60 : {
61 18 : return nullptr;
62 : }
63 9577 : return pTempBuffer;
64 : }
65 :
66 9595 : bool FetchBytes(GByte *pabyDstBuffer, vsi_l_offset nOffset, int nPixels,
67 : int nDTSize, bool bIsByteSwapped, bool bIsComplex,
68 : int nBlockId)
69 : {
70 9595 : vsi_l_offset nSeekForward = 0;
71 13801 : if (nOffset <= VSIFTellL(fp) ||
72 4206 : (nSeekForward = nOffset - VSIFTellL(fp)) > nTempBufferSize)
73 : {
74 7344 : if (VSIFSeekL(fp, nOffset, SEEK_SET) != 0)
75 : {
76 0 : CPLError(CE_Failure, CPLE_FileIO, "Cannot seek to block %d",
77 : nBlockId);
78 0 : return false;
79 : }
80 : }
81 : else
82 : {
83 4502 : while (nSeekForward > 0)
84 : {
85 2251 : vsi_l_offset nToRead = nSeekForward;
86 2251 : if (nToRead > nTempBufferSize)
87 0 : nToRead = nTempBufferSize;
88 2251 : if (VSIFReadL(pTempBuffer, static_cast<size_t>(nToRead), 1,
89 2251 : fp) != 1)
90 : {
91 0 : CPLError(CE_Failure, CPLE_FileIO, "Cannot seek to block %d",
92 : nBlockId);
93 0 : return false;
94 : }
95 2251 : nSeekForward -= nToRead;
96 : }
97 : }
98 9595 : if (VSIFReadL(pabyDstBuffer, static_cast<size_t>(nPixels) * nDTSize, 1,
99 9595 : fp) != 1)
100 : {
101 18 : CPLError(CE_Failure, CPLE_FileIO, "Missing data for block %d",
102 : nBlockId);
103 18 : return false;
104 : }
105 :
106 9577 : if (bIsByteSwapped)
107 : {
108 2318 : if (bIsComplex)
109 1213 : GDALSwapWords(pabyDstBuffer, nDTSize / 2, 2 * nPixels,
110 : nDTSize / 2);
111 : else
112 1105 : GDALSwapWords(pabyDstBuffer, nDTSize, nPixels, nDTSize);
113 : }
114 9577 : return true;
115 : }
116 :
117 : static const bool bMinimizeIO = true;
118 : };
119 :
120 : #endif // FETCHBUFFERDIRECTIO_H_INCLUDED
|