-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtif_strip.cs
executable file
·292 lines (251 loc) · 10.2 KB
/
tif_strip.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// tif_strip.cs
//
// Based on LIBTIFF, Version 3.9.4 - 15-Jun-2010
// Copyright (c) 2006-2010 by the Authors
// Copyright (c) 1988-1997 Sam Leffler
// Copyright (c) 1991-1997 Silicon Graphics, Inc.
//
// Permission to use, copy, modify, distribute, and sell this software and
// its documentation for any purpose is hereby granted without fee, provided
// that (i) the above copyright notices and this permission notice appear in
// all copies of the software and related documentation, and (ii) the names of
// Sam Leffler and Silicon Graphics may not be used in any advertising or
// publicity relating to the software without the specific, prior written
// permission of Sam Leffler and Silicon Graphics.
//
// THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
// ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
// LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
// OF THIS SOFTWARE.
// TIFF Library.
//
// Strip-organized Image Support Routines.
using System;
using System.Collections.Generic;
using System.Text;
namespace Free.Ports.LibTiff
{
public static partial class libtiff
{
static uint summarize(TIFF tif, uint summand1, uint summand2, string where)
{
uint bytes=summand1+summand2;
if(bytes-summand1!=summand2)
{
TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "Integer overflow in {0}", where);
bytes=0;
}
return bytes;
}
static uint multiply(TIFF tif, uint nmemb, uint elem_size, string where)
{
if(elem_size==0) return 0;
uint bytes=nmemb*elem_size;
if(bytes/elem_size!=nmemb)
{
TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "Integer overflow in {0}", where);
bytes=0;
}
return bytes;
}
// Compute which strip a (row, sample) value is in.
public static int TIFFComputeStrip(TIFF tif, uint row, ushort sample)
{
TIFFDirectory td=tif.tif_dir;
uint strip;
strip=row/td.td_rowsperstrip;
if(td.td_planarconfig==PLANARCONFIG.SEPARATE)
{
if(sample>=td.td_samplesperpixel)
{
TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "{0}: Sample out of range, max {1}", sample, td.td_samplesperpixel);
return 0;
}
strip+=sample*td.td_stripsperimage;
}
return (int)strip;
}
// Compute how many strips are in an image.
public static int TIFFNumberOfStrips(TIFF tif)
{
TIFFDirectory td=tif.tif_dir;
uint nstrips;
nstrips=(td.td_rowsperstrip==0xffffffff?1:TIFFhowmany(td.td_imagelength, td.td_rowsperstrip));
if(td.td_planarconfig==PLANARCONFIG.SEPARATE) nstrips=multiply(tif, nstrips, td.td_samplesperpixel, "TIFFNumberOfStrips");
return (int)nstrips;
}
// Compute the # bytes in a variable height, row-aligned strip.
public static int TIFFVStripSize(TIFF tif, uint nrows)
{
TIFFDirectory td=tif.tif_dir;
if(nrows==0xffffffff) nrows=td.td_imagelength;
if(td.td_planarconfig==PLANARCONFIG.CONTIG&&td.td_photometric==PHOTOMETRIC.YCBCR&&!isUpSampled(tif))
{
// Packed YCbCr data contain one Cb+Cr for every
// HorizontalSampling*VerticalSampling Y values.
// Must also roundup width and height when calculating
// since images that are not a multiple of the
// horizontal/vertical subsampling area include
// YCbCr data for the extended image.
object[] ap=new object[2];
TIFFGetField(tif, TIFFTAG.YCBCRSUBSAMPLING, ap);
ushort ycbcrsubsampling0=__GetAsUshort(ap, 0);
ushort ycbcrsubsampling1=__GetAsUshort(ap, 1);
uint samplingarea=(uint)(ycbcrsubsampling0*ycbcrsubsampling1);
if(samplingarea==0)
{
TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "Invalid YCbCr subsampling");
return 0;
}
uint w=TIFFroundup(td.td_imagewidth, ycbcrsubsampling0);
uint scanline=TIFFhowmany8(multiply(tif, w, td.td_bitspersample, "TIFFVStripSize"));
nrows=TIFFroundup(nrows, ycbcrsubsampling1);
// NB: don't need TIFFhowmany here 'cuz everything is rounded
scanline=multiply(tif, nrows, scanline, "TIFFVStripSize");
return (int)summarize(tif, scanline, multiply(tif, 2, (scanline/samplingarea), "TIFFVStripSize"), "TIFFVStripSize");
}
return (int)multiply(tif, nrows, (uint)TIFFScanlineSize(tif), "TIFFVStripSize");
}
// Compute the # bytes in a raw strip.
public static int TIFFRawStripSize(TIFF tif, uint strip)
{
TIFFDirectory td=tif.tif_dir;
int bytecount=(int)td.td_stripbytecount[strip];
if(bytecount<=0)
{
TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "{0}: Invalid strip byte count, strip {1}", bytecount, strip);
return -1;
}
return bytecount;
}
// Compute the # bytes in a (row-aligned) strip.
//
// Note that if RowsPerStrip is larger than the
// recorded ImageLength, then the strip size is
// truncated to reflect the actual space required
// to hold the strip.
public static int TIFFStripSize(TIFF tif)
{
TIFFDirectory td=tif.tif_dir;
uint rps=td.td_rowsperstrip;
if(rps>td.td_imagelength) rps=td.td_imagelength;
return TIFFVStripSize(tif, rps);
}
// Compute a default strip size based on the image
// characteristics and a requested value. If the
// request is <1 then we choose a strip size according
// to certain heuristics.
public static uint TIFFDefaultStripSize(TIFF tif, uint request)
{
return tif.tif_defstripsize(tif, request);
}
static uint _TIFFDefaultStripSize(TIFF tif, uint s)
{
if((int)s<1)
{
// If RowsPerStrip is unspecified, try to break the
// image up into strips that are approximately
// STRIP_SIZE_DEFAULT bytes long.
int scanline=TIFFScanlineSize(tif);
s=(uint)(STRIP_SIZE_DEFAULT/(scanline==0?1:scanline));
if(s==0) s=1; // very wide images
}
return s;
}
// Return the number of bytes to read/write in a call to
// one of the scanline-oriented i/o routines. Note that
// this number may be 1/samples-per-pixel if data is
// stored as separate planes.
public static int TIFFScanlineSize(TIFF tif)
{
TIFFDirectory td=tif.tif_dir;
uint scanline;
if(td.td_planarconfig==PLANARCONFIG.CONTIG)
{
if(td.td_photometric==PHOTOMETRIC.YCBCR&&!isUpSampled(tif))
{
object[] ap=new object[2];
TIFFGetField(tif, TIFFTAG.YCBCRSUBSAMPLING, ap);
ushort ycbcrsubsampling0=__GetAsUshort(ap, 0);
//ushort ycbcrsubsampling1=__GetAsUshort(ap, 1); // not needed
if(ycbcrsubsampling0==0)
{
TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "Invalid YCbCr subsampling");
return 0;
}
scanline=TIFFroundup(td.td_imagewidth, ycbcrsubsampling0);
scanline=TIFFhowmany8(multiply(tif, scanline, td.td_bitspersample, "TIFFScanlineSize"));
return (int)summarize(tif, scanline, multiply(tif, 2, scanline/ycbcrsubsampling0, "TIFFVStripSize"), "TIFFVStripSize");
}
scanline=multiply(tif, td.td_imagewidth, td.td_samplesperpixel, "TIFFScanlineSize");
}
else scanline=td.td_imagewidth;
return (int)TIFFhowmany8(multiply(tif, scanline, td.td_bitspersample, "TIFFScanlineSize"));
}
// Some stuff depends on this older version of TIFFScanlineSize
// TODO: resolve this
public static int TIFFOldScanlineSize(TIFF tif)
{
TIFFDirectory td=tif.tif_dir;
uint scanline=multiply(tif, td.td_bitspersample, td.td_imagewidth, "TIFFScanlineSize");
if(td.td_planarconfig==PLANARCONFIG.CONTIG) scanline=multiply(tif, scanline, td.td_samplesperpixel, "TIFFScanlineSize");
return (int)TIFFhowmany8(scanline);
}
// Return the number of bytes to read/write in a call to
// one of the scanline-oriented i/o routines. Note that
// this number may be 1/samples-per-pixel if data is
// stored as separate planes.
// The ScanlineSize in case of YCbCrSubsampling is defined as the
// strip size divided by the strip height, i.e. the size of a pack of vertical
// subsampling lines divided by vertical subsampling. It should thus make
// sense when multiplied by a multiple of vertical subsampling.
// Some stuff depends on this newer version of TIFFScanlineSize
// TODO: resolve this
public static int TIFFNewScanlineSize(TIFF tif)
{
TIFFDirectory td=tif.tif_dir;
uint scanline;
if(td.td_planarconfig==PLANARCONFIG.CONTIG)
{
if(td.td_photometric==PHOTOMETRIC.YCBCR&&!isUpSampled(tif))
{
object[] ap=new object[2];
TIFFGetField(tif, TIFFTAG.YCBCRSUBSAMPLING, ap);
ushort[] ycbcrsubsampling=new ushort[2];
ycbcrsubsampling[0]=__GetAsUshort(ap, 0);
ycbcrsubsampling[1]=__GetAsUshort(ap, 1);
if(ycbcrsubsampling[0]*ycbcrsubsampling[1]==0)
{
TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "Invalid YCbCr subsampling");
return 0;
}
return (int)(((((td.td_imagewidth+ycbcrsubsampling[0]-1)/ycbcrsubsampling[0])*(ycbcrsubsampling[0]*ycbcrsubsampling[1]+2)*td.td_bitspersample+7)/8)/ycbcrsubsampling[1]);
}
else scanline=multiply(tif, td.td_imagewidth, td.td_samplesperpixel, "TIFFScanlineSize");
}
else scanline=td.td_imagewidth;
return (int)TIFFhowmany8(multiply(tif, scanline, td.td_bitspersample, "TIFFScanlineSize"));
}
// Return the number of bytes required to store a complete
// decoded and packed raster scanline (as opposed to the
// I/O size returned by TIFFScanlineSize which may be less
// if data is store as separate planes).
public static int TIFFRasterScanlineSize(TIFF tif)
{
TIFFDirectory td=tif.tif_dir;
uint scanline=multiply(tif, td.td_bitspersample, td.td_imagewidth, "TIFFRasterScanlineSize");
if(td.td_planarconfig==PLANARCONFIG.CONTIG)
{
scanline=multiply(tif, scanline, td.td_samplesperpixel, "TIFFRasterScanlineSize");
return (int)TIFFhowmany8(scanline);
}
return (int)multiply(tif, TIFFhowmany8(scanline), td.td_samplesperpixel, "TIFFRasterScanlineSize");
}
}
}