32 #define LZMA_API_STATIC
93 #define RET_GEOKEY(TYPE, array, element)\
94 if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
95 key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_name_type_map))\
96 return ff_tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
141 #define RET_GEOKEY_VAL(TYPE, array)\
142 if (val >= TIFF_##TYPE##_OFFSET &&\
143 val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_codes))\
144 return av_strdup(ff_tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
196 snprintf(ap, 14,
"Unknown-%d", val);
204 uint64_t component_len;
205 if (!sep) sep =
", ";
206 component_len = 24LL + strlen(sep);
207 if (count >= (INT_MAX - 1)/component_len)
209 ap =
av_malloc(component_len * count + 1);
214 for (i = 0; i <
count; i++) {
215 unsigned l =
snprintf(ap, component_len,
"%.15g%s", dp[i], sep);
216 if(l >= component_len) {
222 ap0[strlen(ap0) - strlen(sep)] =
'\0';
243 while (--width >= 0) {
244 dst[(width+
offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
245 dst[(width+
offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
246 dst[(width+
offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
247 dst[(width+
offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
248 dst[(width+
offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
249 dst[(width+
offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
250 dst[(width+
offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
251 dst[(width+
offset)*8+0] = (usePtr ? src[width] : c) >> 7;
255 while (--width >= 0) {
256 dst[(width+
offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
257 dst[(width+
offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
258 dst[(width+
offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
259 dst[(width+
offset)*4+0] = (usePtr ? src[width] : c) >> 6;
263 while (--width >= 0) {
264 dst[(width+
offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
265 dst[(width+
offset)*2+0] = (usePtr ? src[width] : c) >> 4;
270 memcpy(dst + offset, src, width);
272 memset(dst + offset, c, width);
284 for (i = 0; i <
size; i++)
298 for (i = 0; i < w; i++) {
307 for (i = 0; i < w; i++) {
322 z_stream zstream = { 0 };
325 zstream.next_in = (
uint8_t *)src;
326 zstream.avail_in =
size;
327 zstream.next_out = dst;
328 zstream.avail_out = *
len;
329 zret = inflateInit(&zstream);
334 zret = inflate(&zstream, Z_SYNC_FLUSH);
335 inflateEnd(&zstream);
336 *len = zstream.total_out;
337 return zret == Z_STREAM_END ? Z_OK : zret;
342 int strip_start,
int is_yuv)
345 unsigned long outlen;
347 outlen = width * lines;
358 ret = tiff_uncompress(zbuf, &outlen, src, size);
361 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
362 (
unsigned long)width * lines, ret);
367 for (line = 0; line < lines; line++) {
371 memcpy(dst, src, width);
386 static int tiff_uncompress_lzma(
uint8_t *dst, uint64_t *len,
const uint8_t *src,
389 lzma_stream stream = LZMA_STREAM_INIT;
392 stream.next_in = (
uint8_t *)src;
393 stream.avail_in =
size;
394 stream.next_out = dst;
395 stream.avail_out = *
len;
396 ret = lzma_stream_decoder(&stream, UINT64_MAX, 0);
397 if (ret != LZMA_OK) {
401 ret = lzma_code(&stream, LZMA_RUN);
403 *len = stream.total_out;
404 return ret == LZMA_STREAM_END ? LZMA_OK :
ret;
408 const uint8_t *src,
int size,
int width,
int lines,
409 int strip_start,
int is_yuv)
411 uint64_t outlen = width * lines;
423 ret = tiff_uncompress_lzma(buf, &outlen, src, size);
424 if (ret != LZMA_OK) {
426 "Uncompressing failed (%"PRIu64
" of %"PRIu64
") with error %d\n", outlen,
427 (uint64_t)width * lines, ret);
432 for (line = 0; line < lines; line++) {
436 memcpy(dst, src, width);
451 const uint8_t *src,
int size,
int width,
int lines)
460 "Error allocating temporary buffer\n");
469 memcpy(src2, src, size);
471 for (i = 0; i <
size; i++)
478 for (line = 0; line < lines; line++) {
487 const uint8_t *src,
int size,
int strip_start,
int lines)
490 int c, line, pixels, code,
ret;
492 int width = ((s->
width * s->
bpp) + 7) >> 3;
523 return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines,
524 strip_start, is_yuv);
527 "zlib support not enabled, "
528 "deflate compression not supported\n");
534 return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines,
535 strip_start, is_yuv);
538 "LZMA support not enabled\n");
548 if (size > 1 && !src[0] && (src[1]&1)) {
555 for (line = 0; line < lines; line++) {
557 if (pixels < width) {
584 for (line = 0; line < lines; line++) {
585 if (src - ssrc > size) {
595 if (ssrc + size - src < width)
600 dst, 1, src, 0, width, 0);
603 for (i = 0; i <
width; i++)
609 for (pixels = 0; pixels <
width;) {
610 if (ssrc + size - src < 2) {
617 if (pixels + code > width ||
618 ssrc + size - src < code) {
620 "Copy went out of bounds\n");
624 dst, 1, src, 0, code, pixels);
627 }
else if (code != -128) {
629 if (pixels + code > width) {
631 "Run went out of bounds\n");
636 dst, 0, NULL, c, code, pixels);
642 for (i = 0; i <
width; i++)
659 int create_gray_palette = 0;
671 create_gray_palette = 1;
730 "This format is not supported (bpp=%d, bppcount=%d)\n",
753 if (!create_gray_palette)
758 uint32_t *pal = (uint32_t *)frame->
f->
data[1];
759 for (i = 0; i < 1<<s->
bpp; i++)
760 pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->
bpp) - 1) * 0x010101;
769 s->
res[offset++] = num;
773 s->
res[2] * (uint64_t)s->
res[1], s->
res[0] * (uint64_t)s->
res[3], INT32_MAX);
820 "This format is not supported (bpp=%d, %d components)\n",
835 for (i = 0; i <
count; i++)
844 "This format is not supported (bpp=%d, %d components)\n",
853 "Samples per pixel requires a single value, many provided\n");
858 "Samples per pixel %d is too large\n", value);
904 if (!value || (type ==
TIFF_LONG && value == UINT_MAX))
932 set_sar(s, tag, value, value2);
963 "PhotometricInterpretation 0x%04X",
975 "Unknown FillOrder value %d, trying default one\n", value);
983 if (count / 3 > 256 ||
987 pal_gb[0] = pal_gb[1] = pal_gb[2] = s->
gb;
992 for (i = 0; i < count / 3; i++) {
993 uint32_t p = 0xFF000000;
994 p |= (
ff_tget(&pal_gb[0], type, s->
le) >> off) << 16;
995 p |= (
ff_tget(&pal_gb[1], type, s->
le) >> off) << 8;
996 p |=
ff_tget(&pal_gb[2], type, s->
le) >> off;
1010 for (i = 0; i <
count; i++)
1021 #define ADD_METADATA(count, name, sep)\
1022 if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1023 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1065 if (count >= INT_MAX /
sizeof(int64_t))
1074 for (i = 0; i <
count; i++)
1155 "Unknown or unsupported tag %d/0X%0X\n",
1172 int le,
ret, plane, planes;
1173 int i, j, entries, stride;
1174 unsigned soff, ssize;
1185 }
else if (off >= UINT_MAX - 14 || avpkt->
size < off + 14) {
1204 for (i = 0; i < entries; i++) {
1258 for (plane = 0; plane < planes; plane++) {
1260 dst = p->
data[plane];
1261 for (i = 0; i < s->
height; i += s->
rps) {
1272 if (soff > avpkt->
size || ssize > avpkt->
size - soff) {
1282 dst += s->
rps * stride;
1289 dst = p->
data[plane];
1293 ssize = s->
width * soff;
1298 for (i = 0; i < s->
height; i++) {
1299 for (j = soff; j < ssize; j += 2)
1307 for (i = 0; i < s->
height; i++) {
1308 for (j = soff; j < ssize; j += 2)
1313 for (i = 0; i < s->
height; i++) {
1314 for (j = soff; j < ssize; j++)
1315 dst[j] += dst[j - soff];
1322 dst = p->
data[plane];
1323 for (i = 0; i < s->
height; i++) {
1324 for (j = 0; j < p->
linesize[plane]; j++)