summaryrefslogtreecommitdiffstats
path: root/debian/imlib/imlib-1.9.15/gdk_imlib/io-tiff.c
blob: 26790b7522b500078da3123ad7e6d52693d6e1dc (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
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
#include <config.h>
#include <setjmp.h>
#define id _gdk_imlib_data
#include "gdk_imlib.h"
#include "gdk_imlib_private.h"

#ifdef HAVE_LIBTIFF
#include <tiffio.h>

unsigned char      *
loader_tiff(FILE *f, char *file, int *w, int *h, int *t)
{
  TIFF               *tif;
  unsigned char      *data, *ptr, r, g, b, a;
  int                 x, y;
  uint32              ww, hh, *rast, *tptr;
  size_t              npix;
  int                 istransp;
  int                 fd;

  istransp = 0;
  if (!f)
    return NULL;

  fd = fileno(f);
  /* Apparently rewind(f) isn't sufficient */
  lseek(fd, (long) 0, 0);  
  /* So why does libtif need a filename here ??? */
  tif = TIFFFdOpen(fd, file, "r");

  if (!tif)
    return NULL;

  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &ww);
  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &hh);
  npix = ww * hh;
  *w = (int)ww;
  *h = (int)hh;
  if(ww > 32767 || hh > 32767)
    {
      TIFFClose(tif);
      return NULL;
    }
  rast = (uint32 *) _TIFFmalloc(npix * sizeof(uint32));
  if (!rast)
    {
      TIFFClose(tif);
      return NULL;
    }
  data = NULL;
  if (TIFFReadRGBAImage(tif, ww, hh, rast, 0))
    {
      data = (unsigned char *)malloc(*w ** h * 3);
      if (!data)
	{
	  _TIFFfree(rast);
	  TIFFClose(tif);
	  return NULL;
	}
      ptr = data;
      for (y = 0; y < *h; y++)
	{
	  tptr = rast;
	  tptr += ((*h - y - 1) ** w);
	  for (x = 0; x < *w; x++)
	    {
	      a = TIFFGetA(*tptr);
	      b = TIFFGetB(*tptr);
	      g = TIFFGetG(*tptr);
	      r = TIFFGetR(*tptr);
	      tptr++;
	      if (a < 128)
		{
		  *ptr++ = 255;
		  *ptr++ = 0;
		  *ptr++ = 255;
		  istransp = 1;
		}
	      else
		{
		  if ((r == 255) && (g == 0) && (b == 255))
		    r = 254;
		  *ptr++ = r;
		  *ptr++ = g;
		  *ptr++ = b;
		}
	    }
	}
    }
  _TIFFfree(rast);
  TIFFClose(tif);
  *t = istransp;
  return data;
}

gint
saver_tiff (GdkImlibImage * im, char *file, GdkImlibSaveInfo * info)
{
      TIFF               *tif;
      unsigned char      *data;
      int                 y;
      int                 w;

      tif = TIFFOpen(file, "w");
      if (tif)
	{
	  TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, im->rgb_width);
	  TIFFSetField(tif, TIFFTAG_IMAGELENGTH, im->rgb_height);
	  TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
	  TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
	  TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
	  TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
	  {
	    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
	    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
	    w = TIFFScanlineSize(tif);
	    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
			 TIFFDefaultStripSize(tif, -1));
	    for (y = 0; y < im->rgb_height; y++)
	      {
		data = im->rgb_data + (y * im->rgb_width * 3);
		TIFFWriteScanline(tif, data, y, 0);
	      }
	  }
	  TIFFClose(tif);
	  return 1;
	}
      return 0;
}

#else
unsigned char      *
loader_tiff (FILE * f, int *w, int *h, int *t)
{
	return NULL;
}

gint
saver_tiff (GdkImlibImage * im, char *file, GdkImlibSaveInfo * info)
{
	return 0;
}
#endif