1 #include "evas_common.h"
2 #include "evas_private.h"
8 int evas_image_load_file_head_tiff(Image_Entry *ie, const char *file, const char *key);
9 int evas_image_load_file_data_tiff(Image_Entry *ie, const char *file, const char *key);
11 Evas_Image_Load_Func evas_image_load_tiff_func =
13 evas_image_load_file_head_tiff,
14 evas_image_load_file_data_tiff
17 typedef struct TIFFRGBAImage_Extra TIFFRGBAImage_Extra;
19 struct TIFFRGBAImage_Extra {
21 tileContigRoutine put_contig;
22 tileSeparateRoutine put_separate;
29 static void put_contig_and_raster(TIFFRGBAImage *, uint32 *,
30 uint32, uint32, uint32, uint32, int32,
31 int32, unsigned char *);
32 static void put_separate_and_raster(TIFFRGBAImage *, uint32 *, uint32,
33 uint32, uint32, uint32, int32,
34 int32, unsigned char *,
35 unsigned char *, unsigned char *,
37 static void raster(TIFFRGBAImage_Extra * img, uint32 * raster, uint32 x,
38 uint32 y, uint32 w, uint32 h);
42 put_contig_and_raster(TIFFRGBAImage * img, uint32 * rast,
43 uint32 x, uint32 y, uint32 w, uint32 h,
44 int32 fromskew, int32 toskew, unsigned char *cp)
46 (*(((TIFFRGBAImage_Extra *) img)->put_contig)) (img, rast, x, y, w, h,
47 fromskew, toskew, cp);
48 raster((TIFFRGBAImage_Extra *) img, rast, x, y, w, h);
52 put_separate_and_raster(TIFFRGBAImage * img, uint32 * rast,
53 uint32 x, uint32 y, uint32 w, uint32 h,
54 int32 fromskew, int32 toskew,
55 unsigned char *r, unsigned char *g, unsigned char *b,
58 (*(((TIFFRGBAImage_Extra *) img)->put_separate))
59 (img, rast, x, y, w, h, fromskew, toskew, r, g, b, a);
60 raster((TIFFRGBAImage_Extra *) img, rast, x, y, w, h);
63 /* needs orientation code */
66 raster(TIFFRGBAImage_Extra * img, uint32 * rast,
67 uint32 x, uint32 y, uint32 w, uint32 h)
69 int image_width, image_height;
70 uint32 *pixel, pixel_value;
71 int i, j, dy, rast_offset;
72 DATA32 *buffer_pixel, *buffer = evas_cache_image_pixels(img->image);
75 image_width = img->image->w;
76 image_height = img->image->h;
78 dy = h > y ? -1 : y - h;
80 /* rast seems to point to the beginning of the last strip processed */
81 /* so you need use negative offsets. Bizzare. Someone please check this */
82 /* I don't understand why, but that seems to be what's going on. */
83 /* libtiff needs better docs! */
85 if (img->rgba.alpha == EXTRASAMPLE_UNASSALPHA)
87 for (i = y, rast_offset = 0; i > dy; i--, rast_offset--)
89 pixel = rast + (rast_offset * image_width);
90 buffer_pixel = buffer + ((((image_height - 1) - i) * image_width) + x);
92 for (j = 0; j < w; j++)
96 pixel_value = (*(pixel++));
97 a = TIFFGetA(pixel_value);
98 r = TIFFGetR(pixel_value);
99 g = TIFFGetG(pixel_value);
100 b = TIFFGetB(pixel_value);
101 if (!alpha_premult && (a < 255))
103 r = (r * (a + 1)) >> 8;
104 g = (g * (a + 1)) >> 8;
105 b = (b * (a + 1)) >> 8;
107 (*(buffer_pixel++)) = (a << 24) | (r << 16) | (g << 8) | b;
113 evas_image_load_file_head_tiff(Image_Entry *ie, const char *file, const char *key)
116 TIFFRGBAImage tiff_image;
125 ffile = fopen(file, "rb");
129 if (fread(&magic_number, sizeof(uint16), 1, ffile) != 1)
134 /* Apparently rewind(f) isn't sufficient */
135 fseek(ffile, (long)0, SEEK_SET);
137 if ((magic_number != TIFF_BIGENDIAN) /* Checks if actually tiff file */
138 && (magic_number != TIFF_LITTLEENDIAN))
146 lseek(fd, (long)0, SEEK_SET);
149 tif = TIFFFdOpen(fd, file, "r");
153 strcpy(txt, "Evas Tiff loader: cannot be processed by libtiff");
154 if (!TIFFRGBAImageOK(tif, txt))
159 strcpy(txt, "Evas Tiff loader: cannot begin reading tiff");
160 if (!TIFFRGBAImageBegin(& tiff_image, tif, 1, txt))
166 if (tiff_image.alpha != EXTRASAMPLE_UNSPECIFIED)
168 if ((tiff_image.width < 1) || (tiff_image.height < 1) ||
169 (tiff_image.width > 8192) || (tiff_image.height > 8192))
174 ie->w = tiff_image.width;
175 ie->h = tiff_image.height;
182 evas_image_load_file_data_tiff(Image_Entry *ie, const char *file, const char *key)
185 TIFFRGBAImage_Extra rgba_image;
196 ffile = fopen(file, "rb");
200 fread(&magic_number, sizeof(uint16), 1, ffile);
201 /* Apparently rewind(f) isn't sufficient */
202 fseek(ffile, (long)0, SEEK_SET);
204 if ((magic_number != TIFF_BIGENDIAN) /* Checks if actually tiff file */
205 && (magic_number != TIFF_LITTLEENDIAN))
213 lseek(fd, (long)0, SEEK_SET);
216 tif = TIFFFdOpen(fd, file, "r");
220 strcpy(txt, "Evas Tiff loader: cannot be processed by libtiff");
221 if (!TIFFRGBAImageOK(tif, txt))
226 strcpy(txt, "Evas Tiff loader: cannot begin reading tiff");
227 if (!TIFFRGBAImageBegin((TIFFRGBAImage *) & rgba_image, tif, 0, txt))
232 rgba_image.image = ie;
234 if (rgba_image.rgba.alpha != EXTRASAMPLE_UNSPECIFIED)
236 if ((rgba_image.rgba.width != ie->w) ||
237 (rgba_image.rgba.height != ie->h))
243 evas_cache_image_surface_alloc(ie, rgba_image.rgba.width, rgba_image.rgba.height);
244 if (!evas_cache_image_pixels(ie))
246 TIFFRGBAImageEnd((TIFFRGBAImage *) & rgba_image);
252 rgba_image.num_pixels = num_pixels = ie->w * ie->h;
254 rgba_image.pper = rgba_image.py = 0;
255 rast = (uint32 *) _TIFFmalloc(sizeof(uint32) * num_pixels);
259 fprintf(stderr, "Evas Tiff loader: out of memory\n");
261 TIFFRGBAImageEnd((TIFFRGBAImage *) & rgba_image);
267 if (rgba_image.rgba.put.any == NULL)
269 fprintf(stderr, "Evas Tiff loader: no put function");
272 TIFFRGBAImageEnd((TIFFRGBAImage *) & rgba_image);
279 if (rgba_image.rgba.isContig)
281 rgba_image.put_contig = rgba_image.rgba.put.contig;
282 rgba_image.rgba.put.contig = put_contig_and_raster;
286 rgba_image.put_separate = rgba_image.rgba.put.separate;
287 rgba_image.rgba.put.separate = put_separate_and_raster;
291 /* if (rgba_image.rgba.samplesperpixel == 8)*/
292 if (rgba_image.rgba.bitspersample == 8)
294 if (!TIFFRGBAImageGet((TIFFRGBAImage *) &rgba_image, rast,
295 rgba_image.rgba.width, rgba_image.rgba.height))
298 TIFFRGBAImageEnd((TIFFRGBAImage *) & rgba_image);
305 printf("channel bits == %i\n", (int)rgba_image.rgba.samplesperpixel);
310 TIFFRGBAImageEnd((TIFFRGBAImage *) & rgba_image);
314 evas_common_image_set_alpha_sparse(ie);
319 module_open(Evas_Module *em)
322 em->functions = (void *)(&evas_image_load_tiff_func);
332 EAPI Evas_Module_Api evas_modapi =
334 EVAS_MODULE_API_VERSION,
335 EVAS_MODULE_TYPE_IMAGE_LOADER,