Debian Codegraph @

src/image-io/tiffload.c

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * tiffload.c - loading TIFF bitmaps
3 */
4
5 #include "../base/mdjvucfg.h"
6
•7 #ifdef HAVE_LIBTIFF
8 #include <tiffio.h>
9 #define MDJVU_USE_TIFFIO
10 #endif
11 #include <minidjvu/minidjvu.h>
12
13 #include <stdlib.h>
14 #include <string.h>
15
•16 #ifdef HAVE_LIBTIFF
17
18 static mdjvu_bitmap_t load_tiff(const char *path, int32 *presolution, mdjvu_error_t *perr, uint32 idx)
19 {
20 uint16 photometric;
21 uint32 w, h;
22 uint16 bits_per_sample = 0, samples_per_pixel = 0;
23 float dpi;
24 mdjvu_bitmap_t result;
25 tsize_t scanline_size;
26 unsigned char *scanline;
27 uint32 i;
28
29 TIFF *tiff = TIFFOpen(path, "r");
30 for ( i=0; tiff != NULL && i<idx; i++ )
31 {
32 if (!TIFFReadDirectory(tiff))
33 break;
34 }
35
36 *perr = NULL;
37 if (!tiff || i<idx)
38 {
39 *perr = mdjvu_get_error(mdjvu_error_fopen_read);
40 return NULL;
41 }
42
43 /* test if bitonal */
44 TIFFGetFieldDefaulted(tiff, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
45 TIFFGetFieldDefaulted(tiff, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
46 if (bits_per_sample != 1 || samples_per_pixel != 1)
47 {
48 *perr = mdjvu_get_error(mdjvu_error_corrupted_tiff);
49 TIFFClose(tiff);
50 return NULL;
51 }
52
53 /* photometric */
54 photometric = PHOTOMETRIC_MINISWHITE;
55 TIFFGetFieldDefaulted(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
56
57 /* image size */
58 if (!TIFFGetFieldDefaulted(tiff, TIFFTAG_IMAGEWIDTH, &w)
59 || !TIFFGetFieldDefaulted(tiff, TIFFTAG_IMAGELENGTH, &h))
60 {
61 *perr = mdjvu_get_error(mdjvu_error_corrupted_tiff);
62 TIFFClose(tiff);
63 return NULL;
64 }
65
66 /* get the resolution */
67 if (presolution && TIFFGetFieldDefaulted(tiff, TIFFTAG_XRESOLUTION, &dpi))
68 {
69 *presolution = (int32) dpi;
70 }
71
72 result = mdjvu_bitmap_create(w, h);
73
74 scanline_size = TIFFScanlineSize(tiff);
75
76 if (scanline_size < mdjvu_bitmap_get_packed_row_size(result))
77 {
78 *perr = mdjvu_get_error(mdjvu_error_corrupted_tiff);
79 TIFFClose(tiff);
80 mdjvu_bitmap_destroy(result);
81 return NULL;
82 }
83
84 scanline = (unsigned char *) malloc(scanline_size);
85
86 for (i = 0; i < h; i++)
87 {
88 if (TIFFReadScanline(tiff, (tdata_t)scanline, i, 0) < 0)
89 {
90 *perr = mdjvu_get_error(mdjvu_error_corrupted_tiff);
91 TIFFClose(tiff);
92 free(scanline);
93 mdjvu_bitmap_destroy(result);
94 return NULL;
95 }
96
97 if (photometric != PHOTOMETRIC_MINISWHITE)
98 {
99 /* invert the row */
100 int32 k;
101 int32 s = (int32) scanline_size;
102 for (k = 0; k < s; k++)
103 scanline[k] = ~scanline[k];
104 }
105
106 /* clear the padding bits */
•107 if (w & 7)
108 scanline[scanline_size - 1] &= ~(0xFF >> (w & 7));
109
110 memcpy(mdjvu_bitmap_access_packed_row(result, i),
111 scanline,
112 mdjvu_bitmap_get_packed_row_size(result));
113 }
114
115 free(scanline);
116
117 TIFFClose(tiff);
118 return result;
119 }
120
121 MDJVU_IMPLEMENT uint32 mdjvu_get_tiff_page_count(const char *path)
122 {
123 int dircount = 0;
124 TIFF* tif = TIFFOpen(path, "r");
125
126 /* a "directory" is a page in a multipage tiff */
127
128 if ( tif ) {
129 do {
130 dircount++;
131 } while ( TIFFReadDirectory(tif) );
132 TIFFClose(tif);
133 }
134 return dircount;
135 }
136
•137 #endif /* HAVE_LIBTIFF */
138
139 MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_load_tiff(const char *path, int32 *presolution, mdjvu_error_t *perr, uint32 idx)
140 {
•141 #ifdef HAVE_LIBTIFF
142 return load_tiff(path, presolution, perr, idx);
143 #else
144 *perr = mdjvu_get_error(mdjvu_error_tiff_support_disabled);
145 return NULL;
146 #endif
147 }