| 1 | /* |
| 2 | * pngxrtif.c - libpng external I/O: TIFF reader. |
| 3 | * Copyright (C) 2006-2012 Cosmin Truta. |
| 4 | */ |
| 5 | |
| 6 | #include "pngxtern.h"cxx . . $ `<file>/src/pngxtern/pngxtern.h`/ |
| 7 | #include "pngxutil.h"cxx . . $ `<file>/src/pngxtern/pngxutil.h`/ |
| 8 | #include "minitiff.h"cxx . . $ `<file>/third_party/minitiff/minitiff.h`/ |
| 9 | #include <stdio.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #define PNGX_INTERNALcxx . . $ `src/pngxtern/pngxrtif.c:12:9`! |
| 13 | #include "pngxpriv.h"cxx . . $ `<file>/src/pngxtern/pngxpriv.h`/ |
| 14 | |
| 15 | |
| 16 | /* |
| 17 | * The TIFF format name. |
| 18 | */ |
| 19 | static const char tiff_fmt_namecxx . . $ tiff_fmt_name.[] = "TIFF"; |
| 20 | static const char tiff_fmt_long_namecxx . . $ tiff_fmt_long_name.[] = "Tagged Image File Format"; |
| 21 | |
| 22 | |
| 23 | int /* PRIVATE */ |
| 24 | pngx_sig_is_tiffcxx . . $ pngx_sig_is_tiff(6351eb9d066e7d6).(png_bytepcxx . . $ png_bytep# sig, size_tcxx . . $ size_t# sig_size, |
| 25 | png_const_charppcxx . . $ png_const_charpp# fmt_name_ptr, |
| 26 | png_const_charppcxx . . $ png_const_charpp# fmt_long_name_ptr) |
| 27 | { |
| 28 | /* Require at least the TIFF signature. */ |
| 29 | if (sig_size < 8) |
| 30 | return -1; /* insufficient data */ |
| 31 | if (memcmpcxx . . $ memcmp(36f949851efa3055).(sig, minitiff_sig_mcxx . . $ minitiff_sig_m., 4) != 0 && |
| 32 | memcmpcxx . . $ memcmp(36f949851efa3055).(sig, minitiff_sig_icxx . . $ minitiff_sig_i., 4) != 0) |
| 33 | return 0; /* not TIFF */ |
| 34 | |
| 35 | /* Store the format name. */ |
| 36 | if (fmt_name_ptr != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 37 | *fmt_name_ptr = tiff_fmt_namecxx . . $ tiff_fmt_name.; |
| 38 | if (fmt_long_name_ptr != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 39 | *fmt_long_name_ptr = tiff_fmt_long_namecxx . . $ tiff_fmt_long_name.; |
| 40 | return 1; /* TIFF */ |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /* FIXME: Not thread-safe. */ |
| 45 | static png_structpcxx . . $ png_structp# err_png_ptrcxx . . $ err_png_ptr. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 46 | static unsigned int num_extra_imagescxx . . $ num_extra_images.; |
| 47 | |
| 48 | static void pngx_tiff_errorcxx . . $ pngx_tiff_error(85c52e162fed56f9).(const char *msg) |
| 49 | { |
| 50 | png_errorcxx . . $ png_error(38b9ebb04943db50).(err_png_ptrcxx . . $ err_png_ptr., msg); |
| 51 | } |
| 52 | |
| 53 | static void pngx_tiff_warningcxx . . $ pngx_tiff_warning(85c52e162fed56f9).(const char *msg) |
| 54 | { |
| 55 | /* FIXME: |
| 56 | * Inspection of warning messages is fragile, but is |
| 57 | * required by the limitations of minitiff version 0.1. |
| 58 | */ |
| 59 | if (strstrcxx . . $ strstr(a713fe7b9a57cf96).(msg, "multi-image") != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 60 | ++num_extra_imagescxx . . $ num_extra_images.; |
| 61 | |
| 62 | #if 0 |
| 63 | /* Metadata is not imported, so warnings need not be shown. */ |
| 64 | png_warning(err_png_ptr, msg); |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | |
| 69 | int /* PRIVATE */ |
| 70 | pngx_read_tiffcxx . . $ pngx_read_tiff(7580bb69e5ece70a).(png_structpcxx . . $ png_structp# png_ptr, png_infopcxx . . $ png_infop# info_ptr, FILEcxx . . $ FILE# *stream) |
| 71 | { |
| 72 | struct minitiff_infocxx . . $ minitiff_info# tiff_info; |
| 73 | unsigned int width, height, pixel_size, sample_depth, sample_max; |
| 74 | int color_type; |
| 75 | int sample_overflow; |
| 76 | png_byteppcxx . . $ png_bytepp# row_pointers; |
| 77 | png_bytepcxx . . $ png_bytep# row; |
| 78 | unsigned int i, j, k; |
| 79 | |
| 80 | err_png_ptrcxx . . $ err_png_ptr. = png_ptr; |
| 81 | num_extra_imagescxx . . $ num_extra_images. = 0; |
| 82 | minitiff_init_infocxx . . $ minitiff_init_info(807577ea17fd968e).(&tiff_info); |
| 83 | tiff_info.error_handlercxx . . $ minitiff_info#error_handler. = pngx_tiff_errorcxx . . $ pngx_tiff_error(85c52e162fed56f9).; |
| 84 | tiff_info.warning_handlercxx . . $ minitiff_info#warning_handler. = pngx_tiff_warningcxx . . $ pngx_tiff_warning(85c52e162fed56f9).; |
| 85 | minitiff_read_infocxx . . $ minitiff_read_info(4c8462452f6d7fd5).(&tiff_info, stream); |
| 86 | minitiff_validate_infocxx . . $ minitiff_validate_info(ac839349c062fb85).(&tiff_info); |
| 87 | |
| 88 | width = (unsigned int)tiff_info.widthcxx . . $ minitiff_info#width.; |
| 89 | height = (unsigned int)tiff_info.heightcxx . . $ minitiff_info#height.; |
| 90 | pixel_size = tiff_info.samples_per_pixelcxx . . $ minitiff_info#samples_per_pixel.; |
| 91 | sample_depth = tiff_info.bits_per_samplecxx . . $ minitiff_info#bits_per_sample.; |
| 92 | switch (pixel_size) |
| 93 | { |
| 94 | case 1: |
| 95 | color_type = PNG_COLOR_TYPE_GRAYcxx . . $ `<external>/449245770443910636/png.h:666:9`!; |
| 96 | break; |
| 97 | case 2: |
| 98 | color_type = PNG_COLOR_TYPE_GRAY_ALPHAcxx . . $ `<external>/449245770443910636/png.h:670:9`!; |
| 99 | break; |
| 100 | case 3: |
| 101 | color_type = PNG_COLOR_TYPE_RGBcxx . . $ `<external>/449245770443910636/png.h:668:9`!; |
| 102 | break; |
| 103 | case 4: |
| 104 | color_type = PNG_COLOR_TYPE_RGB_ALPHAcxx . . $ `<external>/449245770443910636/png.h:669:9`!; |
| 105 | break; |
| 106 | default: |
| 107 | png_errorcxx . . $ png_error(38b9ebb04943db50).(png_ptr, "Unsupported TIFF color space"); |
| 108 | /* NOTREACHED */ |
| 109 | return 0; /* avoid "uninitialized color_type" warning */ |
| 110 | } |
| 111 | if (sample_depth > 16) |
| 112 | png_errorcxx . . $ png_error(38b9ebb04943db50).(png_ptr, "Unsupported TIFF sample depth"); |
| 113 | sample_max = (1 << sample_depth) - 1; |
| 114 | sample_overflow = 0; |
| 115 | |
| 116 | png_set_IHDRcxx . . $ png_set_IHDR(ee2be53053d03500).(png_ptr, info_ptr, width, height, |
| 117 | (sample_depth <= 8) ? 8 : 16, |
| 118 | color_type, |
| 119 | PNG_INTERLACE_NONEcxx . . $ `<external>/449245770443910636/png.h:685:9`!, PNG_COMPRESSION_TYPE_BASEcxx . . $ `<external>/449245770443910636/png.h:676:9`!, PNG_FILTER_TYPE_BASEcxx . . $ `<external>/449245770443910636/png.h:680:9`!); |
| 120 | row_pointers = pngx_malloc_rowscxx . . $ pngx_malloc_rows(450b4c9ceabcd76c).(png_ptr, info_ptr, 0); |
| 121 | |
| 122 | if (sample_depth <= 8) |
| 123 | { |
| 124 | for (i = 0; i < height; ++i) |
| 125 | { |
| 126 | row = row_pointers[i]; |
| 127 | minitiff_read_rowcxx . . $ minitiff_read_row(5bb88cb70a9a20ea).(&tiff_info, row, i, stream); |
| 128 | if (sample_depth < 8) |
| 129 | { |
| 130 | for (j = 0; j < pixel_size * width; ++j) |
| 131 | { |
| 132 | unsigned int b = row[j]; |
| 133 | if (b > sample_max) |
| 134 | { |
| 135 | b = sample_max; |
| 136 | sample_overflow = 1; |
| 137 | } |
| 138 | row[j] = (png_bytecxx . . $ png_byte#)((b * 255 + sample_max / 2) / sample_max); |
| 139 | } |
| 140 | } |
| 141 | if (tiff_info.photometriccxx . . $ minitiff_info#photometric. == 0) |
| 142 | { |
| 143 | for (j = 0; j < pixel_size * width; ++j) |
| 144 | row[j] = (png_bytecxx . . $ png_byte#)(255 - row[j]); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | for (i = 0; i < height; ++i) |
| 151 | { |
| 152 | row = row_pointers[i]; |
| 153 | minitiff_read_rowcxx . . $ minitiff_read_row(5bb88cb70a9a20ea).(&tiff_info, row, i, stream); |
| 154 | if (tiff_info.byte_ordercxx . . $ minitiff_info#byte_order. == 'I') |
| 155 | { |
| 156 | /* "Intel" byte order => swap row bytes */ |
| 157 | for (j = k = 0; j < pixel_size * width; ++j, k+=2) |
| 158 | { |
| 159 | png_bytecxx . . $ png_byte# b = row[k]; |
| 160 | row[k] = row[k + 1]; |
| 161 | row[k + 1] = b; |
| 162 | } |
| 163 | } |
| 164 | if (sample_depth < 16) |
| 165 | { |
| 166 | for (j = k = 0; k < pixel_size * width; ++j, k+=2) |
| 167 | { |
| 168 | unsigned int b = (row[k] << 8) + row[k + 1]; |
| 169 | if (b > sample_max) |
| 170 | { |
| 171 | b = sample_max; |
| 172 | sample_overflow = 1; |
| 173 | } |
| 174 | b = (b * 65535U + sample_max / 2) / sample_max; |
| 175 | row[k] = (png_bytecxx . . $ png_byte#)(b >> 8); |
| 176 | row[k + 1] = (png_bytecxx . . $ png_byte#)(b & 255); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (sample_overflow) |
| 183 | png_warningcxx . . $ png_warning(b0c459b02b32e6a2).(png_ptr, "Overflow in TIFF samples"); |
| 184 | |
| 185 | minitiff_destroy_infocxx . . $ minitiff_destroy_info(807577ea17fd968e).(&tiff_info); |
| 186 | return 1 + num_extra_imagescxx . . $ num_extra_images.; |
| 187 | } |