Coverage Report

Created: 2023-06-07 06:50

/src/opencv/3rdparty/libtiff/tif_tile.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1991-1997 Sam Leffler
3
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and 
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 * 
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
16
 * 
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
22
 * OF THIS SOFTWARE.
23
 */
24
25
/*
26
 * TIFF Library.
27
 *
28
 * Tiled Image Support Routines.
29
 */
30
#include "tiffiop.h"
31
32
/*
33
 * Compute which tile an (x,y,z,s) value is in.
34
 */
35
uint32
36
TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
37
0
{
38
0
  TIFFDirectory *td = &tif->tif_dir;
39
0
  uint32 dx = td->td_tilewidth;
40
0
  uint32 dy = td->td_tilelength;
41
0
  uint32 dz = td->td_tiledepth;
42
0
  uint32 tile = 1;
43
44
0
  if (td->td_imagedepth == 1)
45
0
    z = 0;
46
0
  if (dx == (uint32) -1)
47
0
    dx = td->td_imagewidth;
48
0
  if (dy == (uint32) -1)
49
0
    dy = td->td_imagelength;
50
0
  if (dz == (uint32) -1)
51
0
    dz = td->td_imagedepth;
52
0
  if (dx != 0 && dy != 0 && dz != 0) {
53
0
    uint32 xpt = TIFFhowmany_32(td->td_imagewidth, dx);
54
0
    uint32 ypt = TIFFhowmany_32(td->td_imagelength, dy);
55
0
    uint32 zpt = TIFFhowmany_32(td->td_imagedepth, dz);
56
57
0
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE) 
58
0
      tile = (xpt*ypt*zpt)*s +
59
0
           (xpt*ypt)*(z/dz) +
60
0
           xpt*(y/dy) +
61
0
           x/dx;
62
0
    else
63
0
      tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;
64
0
  }
65
0
  return (tile);
66
0
}
67
68
/*
69
 * Check an (x,y,z,s) coordinate
70
 * against the image bounds.
71
 */
72
int
73
TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
74
0
{
75
0
  TIFFDirectory *td = &tif->tif_dir;
76
77
0
  if (x >= td->td_imagewidth) {
78
0
    TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
79
0
           "%lu: Col out of range, max %lu",
80
0
           (unsigned long) x,
81
0
           (unsigned long) (td->td_imagewidth - 1));
82
0
    return (0);
83
0
  }
84
0
  if (y >= td->td_imagelength) {
85
0
    TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
86
0
           "%lu: Row out of range, max %lu",
87
0
           (unsigned long) y,
88
0
           (unsigned long) (td->td_imagelength - 1));
89
0
    return (0);
90
0
  }
91
0
  if (z >= td->td_imagedepth) {
92
0
    TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
93
0
           "%lu: Depth out of range, max %lu",
94
0
           (unsigned long) z,
95
0
           (unsigned long) (td->td_imagedepth - 1));
96
0
    return (0);
97
0
  }
98
0
  if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
99
0
      s >= td->td_samplesperpixel) {
100
0
    TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
101
0
           "%lu: Sample out of range, max %lu",
102
0
           (unsigned long) s,
103
0
           (unsigned long) (td->td_samplesperpixel - 1));
104
0
    return (0);
105
0
  }
106
0
  return (1);
107
0
}
108
109
/*
110
 * Compute how many tiles are in an image.
111
 */
112
uint32
113
TIFFNumberOfTiles(TIFF* tif)
114
0
{
115
0
  TIFFDirectory *td = &tif->tif_dir;
116
0
  uint32 dx = td->td_tilewidth;
117
0
  uint32 dy = td->td_tilelength;
118
0
  uint32 dz = td->td_tiledepth;
119
0
  uint32 ntiles;
120
121
0
  if (dx == (uint32) -1)
122
0
    dx = td->td_imagewidth;
123
0
  if (dy == (uint32) -1)
124
0
    dy = td->td_imagelength;
125
0
  if (dz == (uint32) -1)
126
0
    dz = td->td_imagedepth;
127
0
  ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
128
0
      _TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
129
0
      TIFFhowmany_32(td->td_imagelength, dy),
130
0
      "TIFFNumberOfTiles"),
131
0
      TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
132
0
  if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
133
0
    ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
134
0
        "TIFFNumberOfTiles");
135
0
  return (ntiles);
136
0
}
137
138
/*
139
 * Compute the # bytes in each row of a tile.
140
 */
141
uint64
142
TIFFTileRowSize64(TIFF* tif)
143
0
{
144
0
        static const char module[] = "TIFFTileRowSize64";
145
0
  TIFFDirectory *td = &tif->tif_dir;
146
0
  uint64 rowsize;
147
0
  uint64 tilerowsize;
148
149
0
  if (td->td_tilelength == 0)
150
0
        {
151
0
                TIFFErrorExt(tif->tif_clientdata,module,"Tile length is zero");
152
0
                return 0;
153
0
        }
154
0
        if (td->td_tilewidth == 0)
155
0
        {
156
0
                TIFFErrorExt(tif->tif_clientdata,module,"Tile width is zero");
157
0
    return (0);
158
0
        }
159
0
  rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
160
0
      "TIFFTileRowSize");
161
0
  if (td->td_planarconfig == PLANARCONFIG_CONTIG)
162
0
        {
163
0
                if (td->td_samplesperpixel == 0)
164
0
                {
165
0
                        TIFFErrorExt(tif->tif_clientdata,module,"Samples per pixel is zero");
166
0
                        return 0;
167
0
                }
168
0
    rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
169
0
        "TIFFTileRowSize");
170
0
        }
171
0
        tilerowsize=TIFFhowmany8_64(rowsize);
172
0
        if (tilerowsize == 0)
173
0
        {
174
0
                TIFFErrorExt(tif->tif_clientdata,module,"Computed tile row size is zero");
175
0
                return 0;
176
0
        }
177
0
  return (tilerowsize);
178
0
}
179
tmsize_t
180
TIFFTileRowSize(TIFF* tif)
181
0
{
182
0
  static const char module[] = "TIFFTileRowSize";
183
0
  uint64 m;
184
0
  m=TIFFTileRowSize64(tif);
185
0
  return _TIFFCastUInt64ToSSize(tif, m, module);
186
0
}
187
188
/*
189
 * Compute the # bytes in a variable length, row-aligned tile.
190
 */
191
uint64
192
TIFFVTileSize64(TIFF* tif, uint32 nrows)
193
0
{
194
0
  static const char module[] = "TIFFVTileSize64";
195
0
  TIFFDirectory *td = &tif->tif_dir;
196
0
  if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
197
0
      td->td_tiledepth == 0)
198
0
    return (0);
199
0
  if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
200
0
      (td->td_photometric==PHOTOMETRIC_YCBCR)&&
201
0
      (td->td_samplesperpixel==3)&&
202
0
      (!isUpSampled(tif)))
203
0
  {
204
    /*
205
     * Packed YCbCr data contain one Cb+Cr for every
206
     * HorizontalSampling*VerticalSampling Y values.
207
     * Must also roundup width and height when calculating
208
     * since images that are not a multiple of the
209
     * horizontal/vertical subsampling area include
210
     * YCbCr data for the extended image.
211
     */
212
0
    uint16 ycbcrsubsampling[2];
213
0
    uint16 samplingblock_samples;
214
0
    uint32 samplingblocks_hor;
215
0
    uint32 samplingblocks_ver;
216
0
    uint64 samplingrow_samples;
217
0
    uint64 samplingrow_size;
218
0
    TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
219
0
        ycbcrsubsampling+1);
220
0
    if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
221
0
        ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
222
0
    {
223
0
      TIFFErrorExt(tif->tif_clientdata,module,
224
0
             "Invalid YCbCr subsampling (%dx%d)", 
225
0
             ycbcrsubsampling[0], 
226
0
             ycbcrsubsampling[1] );
227
0
      return 0;
228
0
    }
229
0
    samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
230
0
    samplingblocks_hor=TIFFhowmany_32(td->td_tilewidth,ycbcrsubsampling[0]);
231
0
    samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
232
0
    samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
233
0
    samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
234
0
    return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
235
0
  }
236
0
  else
237
0
    return(_TIFFMultiply64(tif,nrows,TIFFTileRowSize64(tif),module));
238
0
}
239
tmsize_t
240
TIFFVTileSize(TIFF* tif, uint32 nrows)
241
0
{
242
0
  static const char module[] = "TIFFVTileSize";
243
0
  uint64 m;
244
0
  m=TIFFVTileSize64(tif,nrows);
245
0
  return _TIFFCastUInt64ToSSize(tif, m, module);
246
0
}
247
248
/*
249
 * Compute the # bytes in a row-aligned tile.
250
 */
251
uint64
252
TIFFTileSize64(TIFF* tif)
253
0
{
254
0
  return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
255
0
}
256
tmsize_t
257
TIFFTileSize(TIFF* tif)
258
0
{
259
0
  static const char module[] = "TIFFTileSize";
260
0
  uint64 m;
261
0
  m=TIFFTileSize64(tif);
262
0
  return _TIFFCastUInt64ToSSize(tif, m, module);
263
0
}
264
265
/*
266
 * Compute a default tile size based on the image
267
 * characteristics and a requested value.  If a
268
 * request is <1 then we choose a size according
269
 * to certain heuristics.
270
 */
271
void
272
TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
273
0
{
274
0
  (*tif->tif_deftilesize)(tif, tw, th);
275
0
}
276
277
void
278
_TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
279
0
{
280
0
  (void) tif;
281
0
  if (*(int32*) tw < 1)
282
0
    *tw = 256;
283
0
  if (*(int32*) th < 1)
284
0
    *th = 256;
285
  /* roundup to a multiple of 16 per the spec */
286
0
  if (*tw & 0xf)
287
0
    *tw = TIFFroundup_32(*tw, 16);
288
0
  if (*th & 0xf)
289
0
    *th = TIFFroundup_32(*th, 16);
290
0
}
291
292
/* vim: set ts=8 sts=8 sw=8 noet: */
293
/*
294
 * Local Variables:
295
 * mode: c
296
 * c-basic-offset: 8
297
 * fill-column: 78
298
 * End:
299
 */