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
|
/* tiff.c - load TIFF image from file
*
* Raster graphics library
*
* Copyright (c) 1997-2003 Alfredo K. Kojima
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <tiff.h>
#include <tiffio.h>
#include <tiffvers.h>
#include "wraster.h"
#include "imgformat.h"
#include "wr_i18n.h"
RImage *RLoadTIFF(const char *file, int index)
{
RImage *image = NULL;
TIFF *tif;
int i, ch;
unsigned char *r, *g, *b, *a;
#if TIFFLIB_VERSION < 20210416
uint16 alpha, amode, extrasamples;
uint16 *sampleinfo;
uint32 width, height;
uint32 *data, *ptr;
#else
uint16_t alpha, amode, extrasamples;;
uint16_t *sampleinfo;
uint32_t width, height;
uint32_t *data, *ptr;
#endif
tif = TIFFOpen(file, "r");
if (!tif)
return NULL;
/* seek index */
i = index;
while (i > 0) {
if (!TIFFReadDirectory(tif)) {
RErrorCode = RERR_BADINDEX;
TIFFClose(tif);
return NULL;
}
i--;
}
/* get info */
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo);
alpha = (extrasamples == 1 &&
((sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA) || (sampleinfo[0] == EXTRASAMPLE_UNASSALPHA)));
amode = (extrasamples == 1 && sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
if (width < 1 || height < 1) {
RErrorCode = RERR_BADIMAGEFILE;
TIFFClose(tif);
return NULL;
}
/* read data */
#if TIFFLIB_VERSION < 20210416
ptr = data = (uint32 *) _TIFFmalloc(width * height * sizeof(uint32));
#else
ptr = data = (uint32_t *) _TIFFmalloc(width * height * sizeof(uint32_t));
#endif
if (!data) {
RErrorCode = RERR_NOMEMORY;
} else {
if (!TIFFReadRGBAImage(tif, width, height, data, 0)) {
RErrorCode = RERR_BADIMAGEFILE;
} else {
/* convert data */
image = RCreateImage(width, height, alpha);
if (alpha)
ch = 4;
else
ch = 3;
if (image) {
int x, y;
r = image->data;
g = image->data + 1;
b = image->data + 2;
a = image->data + 3;
/* data seems to be stored upside down */
data += width * (height - 1);
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
*(r) = (*data) & 0xff;
*(g) = (*data >> 8) & 0xff;
*(b) = (*data >> 16) & 0xff;
if (alpha) {
*(a) = (*data >> 24) & 0xff;
if (amode && (*a > 0)) {
*r = (*r * 255) / *(a);
*g = (*g * 255) / *(a);
*b = (*b * 255) / *(a);
}
a += 4;
}
r += ch;
g += ch;
b += ch;
data++;
}
data -= 2 * width;
}
}
}
_TIFFfree(ptr);
}
TIFFClose(tif);
return image;
}
|