~ubuntu-branches/ubuntu/warty/xplanet/warty

« back to all changes in this revision

Viewing changes to src/libimage/tiff.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040824071400-2dr4qnjbjmm8z3ia
Tags: 1.0.6-1ubuntu1
Build-depend: libtiff4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
    tiff.c - read and write tiff images using libtiff routines.
 
3
    Distributed with Xplanet.
 
4
    Copyright (C) 2002 Hari Nair <hari@alumni.caltech.edu>
 
5
 
 
6
    This program is free software; you can redistribute it and/or modify
 
7
    it under the terms of the GNU General Public License as published by
 
8
    the Free Software Foundation; either version 2 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    This program 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
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
****************************************************************************/
 
20
 
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
 
 
24
#include <tiffio.h>
 
25
 
 
26
int
 
27
read_tiff(const char *filename, int *width, int *height, unsigned char **rgb)
 
28
{
 
29
    unsigned char *ptr = NULL;
 
30
    int i, j, istart;
 
31
 
 
32
    TIFF* tif = TIFFOpen(filename, "r");
 
33
 
 
34
    uint32 w, h;
 
35
    uint32* raster;
 
36
  
 
37
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
 
38
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
 
39
 
 
40
    *width = (int) w;
 
41
    *height = (int) h;
 
42
 
 
43
    raster = _TIFFmalloc(w * h * sizeof(uint32));
 
44
    if (raster != NULL) 
 
45
    {
 
46
        rgb[0] = (unsigned char *) realloc(rgb[0], 3 * *width * *height);
 
47
        if (rgb[0] == NULL)
 
48
        {
 
49
            fprintf(stderr, "Can't allocate memory for TIFF file.\n");
 
50
            return(0);
 
51
        }
 
52
 
 
53
        ptr = rgb[0];
 
54
 
 
55
        if (TIFFReadRGBAImage(tif, w, h, raster, 0)) 
 
56
        {
 
57
            for (j = h - 1; j >= 0; j--)
 
58
            {
 
59
                istart = j * w;
 
60
                for (i = 0; i < w; i++)
 
61
                {
 
62
                    *ptr++ = (unsigned char) TIFFGetR(raster[istart + i]);
 
63
                    *ptr++ = (unsigned char) TIFFGetG(raster[istart + i]);
 
64
                    *ptr++ = (unsigned char) TIFFGetB(raster[istart + i]);
 
65
                }
 
66
            }
 
67
        }
 
68
        else
 
69
        {
 
70
            _TIFFfree(raster);
 
71
            return(0);
 
72
        }
 
73
        _TIFFfree(raster);
 
74
    }
 
75
    else 
 
76
        return(0);
 
77
 
 
78
    TIFFClose(tif);
 
79
    return(1);
 
80
}
 
81
 
 
82
int
 
83
write_tiff(const char *filename, int width, int height, unsigned char *rgb)
 
84
{
 
85
    unsigned char *raster;
 
86
    int j;
 
87
  
 
88
    TIFF* tif = TIFFOpen(filename, "w");
 
89
 
 
90
    if (tif == NULL) 
 
91
    {
 
92
        fprintf(stderr, "Can't create TIFF file\n");
 
93
        return(0);
 
94
    }
 
95
 
 
96
    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
 
97
    TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32) width);
 
98
    TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32) height);
 
99
    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
 
100
    TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
 
101
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, -1));
 
102
    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
 
103
    for (j = 0; j < height; j++)
 
104
    {
 
105
        raster = rgb + (j * width * 3);
 
106
        TIFFWriteScanline(tif, raster, j, 0);
 
107
    }
 
108
 
 
109
    TIFFClose(tif);
 
110
    return(1);
 
111
}