summaryrefslogtreecommitdiff
path: root/gdk-pixbuf/io-tiff.c
blob: 1ade9ce296118ab346d4df3d1da594aa1b41f2fc (plain)
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
/*
 * io-tiff.c: GdkPixBuf I/O for TIFF files.
 * Copyright (C) 1999 Mark Crichton
 * Author: Mark Crichton <crichton@gimp.org>
 *
 * 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., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
 *
 */

/* Following code (almost) blatantly ripped from Imlib */

#include <config.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <tiffio.h>

#include "gdk-pixbuf.h"
/*#include "gdk-pixbuf-io.h" */

GdkPixBuf *image_load(FILE * f)
{
    GdkPixBuf *pixbuf;
    TIFF *tiff;
    art_u8 *pixels, *tmppix;
    gint w, h, x, y, num_pixs, fd;
    uint32 *rast, *tmp_rast;

    g_return_val_if_fail(f != NULL, NULL);

    fd = fileno(f);
    tiff = TIFFFdOpen(fd, "libpixbuf-tiff", "r");

    if (!tiff)
	return NULL;

    TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
    TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
    num_pixs = w * h;

    /* Yes, it needs to be _TIFFMalloc... */
    rast = (uint32 *) _TIFFmalloc(num_pixs * sizeof(uint32));

    if (!rast) {
	TIFFClose(tiff);
	return NULL;
    }

    if (TIFFReadRGBAImage(tiff, w, h, rast, 0)) {
	pixels = art_alloc(num_pixs * 4);
	if (!pixels) {
	    _TIFFfree(rast);
	    TIFFClose(tiff);
	    return NULL;
	}
	tmppix = pixels;

	for (y = 0; y < h; y++) {
	    /* Unexplainable...are tiffs backwards? */
	    /* Also looking at the GIMP plugin, this
	     * whole reading thing can be a bit more
	     * robust.
	     */
	    tmp_rast = rast + ((h - y - 1) * w);
	    for (x = 0; x < w; x++) {
		tmppix[0] = TIFFGetR(*tmp_rast);
		tmppix[1] = TIFFGetG(*tmp_rast);
		tmppix[2] = TIFFGetB(*tmp_rast);
		tmppix[3] = TIFFGetA(*tmp_rast);
		tmp_rast++;
		tmppix += 4;
	    }
	}
    }
    _TIFFfree(rast);
    TIFFClose(tiff);

    pixbuf = gdk_pixbuf_new (art_pixbuf_new_rgba (pixels, w, h, (w * 4)),
			     NULL);

    if (!pixbuf)
	    art_free (pixels);

    return pixbuf;
}