Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/contrib/win_dib/tiff2dib.c

raw
1 /*************************************************************************
2 *
3 * Source file for Windows 95/Win32.
4 *
5 * The function LoadTIFFinDIB in this source file let you load
6 * a TIFF file and build a memory DIB with it and return the
7 * HANDLE (HDIB) of the memory block containing the DIB.
8 *
9 * Example :
10 *
11 * HDIB hDIB;
12 * hDIB = LoadTIFFinDIB("sample.tif");
13 *
14 *
15 * To build this source file you must include the TIFF library
16 * in your project.
17 *
18 * 4/12/95 Philippe Tenenhaus [email protected]
19 *
20 ************************************************************************/
21
22 #include "tiffio.h"
23
24 #define HDIB HANDLE
25 #define IS_WIN30_DIB(lpbi) ((*(LPDWORD)(lpbi)) == sizeof(BITMAPINFOHEADER))
26 #define CVT(x) (((x)*255L) / ((1L << 16) - 1))
27
28 static HDIB CreateDIB(DWORD dwWidth, DWORD dwHeight, WORD wBitCount);
29 static LPSTR FindDIBBits(LPSTR lpDIB);
30 static WORD PaletteSize(LPSTR lpDIB);
31 static WORD DIBNumColors(LPSTR lpDIB);
32 static int checkcmap(int n, uint16_t *r, uint16_t *g, uint16_t *b);
33
34 /*************************************************************************
35 *
36 * HDIB LoadTIFFinDIB(LPSTR lpFileName)
37 *
38 * Parameter:
39 *
40 * LPSTR lpDIB - File name of a tiff imag
41 *
42 * Return Value:
43 *
44 * LPSTR - HANDLE of a DIB
45 *
46 * Description:
47 *
48 * This function load a TIFF file and build a memory DIB with it
49 * and return the HANDLE (HDIB) of the memory block containing
50 * the DIB.
51 *
52 * 4/12/95 Philippe Tenenhaus [email protected]
53 *
54 ************************************************************************/
55
56 HDIB LoadTIFFinDIB(LPSTR lpFileName)
57 {
58 TIFF *tif;
59 unsigned long imageLength;
60 unsigned long imageWidth;
61 unsigned int BitsPerSample;
62 unsigned long LineSize;
63 unsigned int SamplePerPixel;
64 unsigned long RowsPerStrip;
65 int PhotometricInterpretation;
66 long nrow;
67 unsigned long row;
68 char *buf;
69 LPBITMAPINFOHEADER lpDIB;
70 HDIB hDIB;
71 char *lpBits;
72 HGLOBAL hStrip;
73 int i, l;
74 int Align;
75
76 tif = TIFFOpen(lpFileName, "r");
77
78 if (!tif)
79 goto TiffOpenError;
80
81 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &imageWidth);
82 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imageLength);
83 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &BitsPerSample);
84 TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &RowsPerStrip);
85 TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &RowsPerStrip);
86 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &PhotometricInterpretation);
87
88 LineSize = TIFFScanlineSize(tif); // Number of byte in ine line
89
90 SamplePerPixel = (int)(LineSize / imageWidth);
91
92 // Align = Number of byte to add at the end of each line of the DIB
93 Align = 4 - (LineSize % 4);
94 if (Align == 4)
95 Align = 0;
96
97 // Create a new DIB
98 hDIB = CreateDIB((DWORD)imageWidth, (DWORD)imageLength,
99 (WORD)(BitsPerSample * SamplePerPixel));
100 lpDIB = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
101 if (!lpDIB)
102 goto OutOfDIBMemory;
103
104 if (lpDIB)
105 lpBits = FindDIBBits((LPSTR)lpDIB);
106 else
107 lpBits = NULL;
108
109 // In the tiff file the lines are save from up to down
110 // In a DIB the lines must be save from down to up
111 if (lpBits)
112 {
113 lpBits = FindDIBBits((LPSTR)lpDIB);
114 lpBits += ((imageWidth * SamplePerPixel) + Align) * (imageLength - 1);
115 // now lpBits pointe on the bottom line
116
117 hStrip = GlobalAlloc(GHND, TIFFStripSize(tif));
118 buf = GlobalLock(hStrip);
119
120 if (!buf)
121 goto OutOfBufMemory;
122
123 // PhotometricInterpretation = 2 image is RGB
124 // PhotometricInterpretation = 3 image have a color palette
125 if (PhotometricInterpretation == 3)
126 {
127 uint16_t *red;
128 uint16_t *green;
129 uint16_t *blue;
130 int16_t i;
131 LPBITMAPINFO lpbmi;
132 int Palette16Bits;
133
134 TIFFGetField(tif, TIFFTAG_COLORMAP, &red, &green, &blue);
135
136 // Is the palette 16 or 8 bits ?
137 if (checkcmap(1 << BitsPerSample, red, green, blue) == 16)
138 Palette16Bits = TRUE;
139 else
140 Palette16Bits = FALSE;
141
142 lpbmi = (LPBITMAPINFO)lpDIB;
143
144 // load the palette in the DIB
145 for (i = (1 << BitsPerSample) - 1; i >= 0; i--)
146 {
147 if (Palette16Bits)
148 {
149 lpbmi->bmiColors[i].rgbRed = (BYTE)CVT(red[i]);
150 lpbmi->bmiColors[i].rgbGreen = (BYTE)CVT(green[i]);
151 lpbmi->bmiColors[i].rgbBlue = (BYTE)CVT(blue[i]);
152 }
153 else
154 {
155 lpbmi->bmiColors[i].rgbRed = (BYTE)red[i];
156 lpbmi->bmiColors[i].rgbGreen = (BYTE)green[i];
157 lpbmi->bmiColors[i].rgbBlue = (BYTE)blue[i];
158 }
159 }
160 }
161
162 // read the tiff lines and save them in the DIB
163 // with RGB mode, we have to change the order of the 3 samples RGB
164 <=> BGR for (row = 0; row < imageLength; row += RowsPerStrip)
165 {
166 nrow = (row + RowsPerStrip > imageLength ? imageLength - row
167 : RowsPerStrip);
168 if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0), buf,
169 nrow * LineSize) == -1)
170 {
171 goto TiffReadError;
172 }
173 else
174 {
175 for (l = 0; l < nrow; l++)
176 {
177 if (SamplePerPixel == 3)
178 for (i = 0; i < (int)(imageWidth); i++)
179 {
180 lpBits[i * SamplePerPixel + 0] =
181 buf[l * LineSize + i * Sample PerPixel + 2];
182 lpBits[i * SamplePerPixel + 1] =
183 buf[l * LineSize + i * Sample PerPixel + 1];
184 lpBits[i * SamplePerPixel + 2] =
185 buf[l * LineSize + i * Sample PerPixel + 0];
186 }
187 else
188 memcpy(lpBits, &buf[(int)(l * LineSize)],
189 (int)imageWidth * SamplePerPixel);
190
191 lpBits -= imageWidth * SamplePerPixel + Align;
192 }
193 }
194 }
195 GlobalUnlock(hStrip);
196 GlobalFree(hStrip);
197 GlobalUnlock(hDIB);
198 TIFFClose(tif);
199 }
200
201 return hDIB;
202
203 OutOfBufMemory:
204
205 TiffReadError:
206 GlobalUnlock(hDIB);
207 GlobalFree(hStrip);
208 OutOfDIBMemory:
209 TIFFClose(tif);
210 TiffOpenError:
211 return (HANDLE)0;
212 }
213
214 static int checkcmap(int n, uint16_t *r, uint16_t *g, uint16_t *b)
215 {
216 while (n-- > 0)
217 if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
218 return (16);
219
220 return (8);
221 }
222
223 /*************************************************************************
224 * All the following functions were created by microsoft, they are
225 * parts of the sample project "wincap" given with the SDK Win32.
226 *
227 * Microsoft says that :
228 *
229 * You have a royalty-free right to use, modify, reproduce and
230 * distribute the Sample Files (and/or any modified version) in
231 * any way you find useful, provided that you agree that
232 * Microsoft has no warranty obligations or liability for any
233 * Sample Application Files which are modified.
234 *
235 ************************************************************************/
236
237 HDIB CreateDIB(DWORD dwWidth, DWORD dwHeight, WORD wBitCount)
238 {
239 BITMAPINFOHEADER bi; // bitmap header
240 LPBITMAPINFOHEADER lpbi; // pointer to BITMAPINFOHEADER
241 DWORD dwLen; // size of memory block
242 HDIB hDIB;
243 DWORD dwBytesPerLine; // Number of bytes per scanline
244
245 // Make sure bits per pixel is valid
246 if (wBitCount <= 1)
247 wBitCount = 1;
248 else if (wBitCount <= 4)
249 wBitCount = 4;
250 else if (wBitCount <= 8)
251 wBitCount = 8;
252 else if (wBitCount <= 24)
253 wBitCount = 24;
254 else
255 wBitCount = 4; // set default value to 4 if parameter is bogus
256
257 // initialize BITMAPINFOHEADER
258 bi.biSize = sizeof(BITMAPINFOHEADER);
259 bi.biWidth = dwWidth; // fill in width from parameter
260 bi.biHeight = dwHeight; // fill in height from parameter
261 bi.biPlanes = 1; // must be 1
262 bi.biBitCount = wBitCount; // from parameter
263 bi.biCompression = BI_RGB;
264 bi.biSizeImage =
265 (dwWidth * dwHeight * wBitCount) / 8; // 0; // 0's here
266 mean "default" bi.biXPelsPerMeter = 2834; // 0;
267 bi.biYPelsPerMeter = 2834; // 0;
268 bi.biClrUsed = 0;
269 bi.biClrImportant = 0;
270
271 // calculate size of memory block required to store the DIB. This
272 // block should be big enough to hold the BITMAPINFOHEADER, the color
273 // table, and the bits
274
275 dwBytesPerLine = (((wBitCount * dwWidth) + 31) / 32 * 4);
276 dwLen = bi.biSize + PaletteSize((LPSTR)&bi) + (dwBytesPerLine * dwHeight);
277
278 // alloc memory block to store our bitmap
279 hDIB = GlobalAlloc(GHND, dwLen);
280
281 // major bummer if we couldn't get memory block
282 if (!hDIB)
283 {
284 return NULL;
285 }
286
287 // lock memory and get pointer to it
288 lpbi = (VOID FAR *)GlobalLock(hDIB);
289
290 // use our bitmap info structure to fill in first part of
291 // our DIB with the BITMAPINFOHEADER
292 *lpbi = bi;
293
294 // Since we don't know what the colortable and bits should contain,
295 // just leave these blank. Unlock the DIB and return the HDIB.
296
297 GlobalUnlock(hDIB);
298
299 /* return handle to the DIB */
300 return hDIB;
301 }
302
303 LPSTR FAR FindDIBBits(LPSTR lpDIB)
304 {
305 return (lpDIB + *(LPDWORD)lpDIB + PaletteSize(lpDIB));
306 }
307
308 WORD FAR PaletteSize(LPSTR lpDIB)
309 {
310 /* calculate the size required by the palette */
311 if (IS_WIN30_DIB(lpDIB))
312 return (DIBNumColors(lpDIB) * sizeof(RGBQUAD));
313 else
314 return (DIBNumColors(lpDIB) * sizeof(RGBTRIPLE));
315 }
316
317 WORD DIBNumColors(LPSTR lpDIB)
318 {
319 WORD wBitCount; // DIB bit count
320
321 /* If this is a Windows-style DIB, the number of colors in the
322 * color table can be less than the number of bits per pixel
323 * allows for (i.e. lpbi->biClrUsed can be set to some value).
324 * If this is the case, return the appropriate value.
325 */
326
327 if (IS_WIN30_DIB(lpDIB))
328 {
329 DWORD dwClrUsed;
330
331 dwClrUsed = ((LPBITMAPINFOHEADER)lpDIB)->biClrUsed;
332 if (dwClrUsed)
333 return (WORD)dwClrUsed;
334 }
335
336 /* Calculate the number of colors in the color table based on
337 * the number of bits per pixel for the DIB.
338 */
339 if (IS_WIN30_DIB(lpDIB))
340 wBitCount = ((LPBITMAPINFOHEADER)lpDIB)->biBitCount;
341 else
342 wBitCount = ((LPBITMAPCOREHEADER)lpDIB)->bcBitCount;
343
344 /* return number of colors based on bits per pixel */
345 switch (wBitCount)
346 {
347 case 1:
348 return 2;
349
350 case 4:
351 return 16;
352
353 case 8:
354 return 256;
355
356 default:
357 return 0;
358 }
359 }