]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-tiff.c
Add pixbuf_duplicate,
[~andy/gtk] / gdk-pixbuf / io-tiff.c
1 /*
2  * io-tiff.c: GdkPixBuf I/O for TIFF files.
3  * Copyright (C) 1999 Mark Crichton
4  * Author: Mark Crichton <crichton@gimp.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
19  *
20  */
21
22 /* Following code (almost) blatantly ripped from Imlib */
23
24 #include <config.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <tiffio.h>
29
30 #include "gdk-pixbuf.h"
31 /*#include "gdk-pixbuf-io.h" */
32
33 GdkPixBuf *image_load(FILE * f)
34 {
35     GdkPixBuf *pixbuf;
36     TIFF *tiff;
37     art_u8 *pixels, *tmppix;
38     gint w, h, x, y, num_pixs, fd;
39     uint32 *rast, *tmp_rast;
40
41     g_return_val_if_fail(f != NULL, NULL);
42
43     fd = fileno(f);
44     tiff = TIFFFdOpen(fd, "libpixbuf-tiff", "r");
45
46     if (!tiff)
47         return NULL;
48
49     TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
50     TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
51     num_pixs = w * h;
52
53     /* Yes, it needs to be _TIFFMalloc... */
54     rast = (uint32 *) _TIFFmalloc(num_pixs * sizeof(uint32));
55
56     if (!rast) {
57         TIFFClose(tiff);
58         return NULL;
59     }
60
61     if (TIFFReadRGBAImage(tiff, w, h, rast, 0)) {
62         pixels = art_alloc(num_pixs * 4);
63         if (!pixels) {
64             _TIFFfree(rast);
65             TIFFClose(tiff);
66             return NULL;
67         }
68         tmppix = pixels;
69
70         for (y = 0; y < h; y++) {
71             /* Unexplainable...are tiffs backwards? */
72             /* Also looking at the GIMP plugin, this
73              * whole reading thing can be a bit more
74              * robust.
75              */
76             tmp_rast = rast + ((h - y - 1) * w);
77             for (x = 0; x < w; x++) {
78                 tmppix[0] = TIFFGetR(*tmp_rast);
79                 tmppix[1] = TIFFGetG(*tmp_rast);
80                 tmppix[2] = TIFFGetB(*tmp_rast);
81                 tmppix[3] = TIFFGetA(*tmp_rast);
82                 tmp_rast++;
83                 tmppix += 4;
84             }
85         }
86     }
87     _TIFFfree(rast);
88     TIFFClose(tiff);
89
90     pixbuf = gdk_pixbuf_new (art_pixbuf_new_rgba (pixels, w, h, (w * 4)),
91                              NULL);
92
93     if (!pixbuf)
94             art_free (pixels);
95
96     return pixbuf;
97 }