Source

gd-libgd / src / gd_tiff.c

Full commit
  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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
/*
   TIFF - Tagged Image File Format Encapsulation for GD Library

   gd_tiff.c

   Copyright (C) M. Retallack

   ---------------------------------------------------------------------------

   **
   ** Permission to use, copy, modify, and distribute this software and its
   ** documentation for any purpose and without fee is hereby granted, provided
   ** that the above copyright notice appear in all copies and that both that
   ** copyright notice and this permission notice appear in supporting
   ** documentation.  This software is provided "as is" without express or
   ** implied warranty.
   **
   ---------------------------------------------------------------------------

   Todo:

   If we fail - cleanup
   Writer: Use gd error function, overflow check may not be necessary as
	 we write our own data (check already done)

   (suggestions only)
   Implement 2 color black/white saving using group4 fax compression
   Implement function to specify encoding to use when writing tiff data

   ----------------------------------------------------------------------------
 */
/* $Id$ */

#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include <gd.h>
#include <gdfonts.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>

#ifdef HAVE_LIBTIFF

#include "tiff.h"
#include "tiffio.h"

typedef struct tiff_handle
{
	int size;
	int pos;
	gdIOCtx *ctx;
	int written;
}
tiff_handle;

/*
   Functions for reading, writing and seeking in gdIOCtx
   This allows for non-file i/o operations with no
   explicit use of libtiff fileio wrapper functions

   Note: because libtiff requires random access, but gdIOCtx
         only supports streams, all writes are buffered
         into memory and written out on close, also all
         reads are done from a memory mapped version of the
         tiff (assuming one already exists)
*/

static tiff_handle *new_tiff_handle(tiff_handle *t, gdIOCtx *g)
{
	t = (tiff_handle *) gdMalloc(sizeof(tiff_handle));
	if(!t || !g) {
		return 0;
	}

	t->size = 0;
	t->pos = 0;
	t->ctx = g;
	t->written = 0;

	return t;
}

/* TIFFReadWriteProc tiff_readproc - Will use gdIOCtx procs to read required 
   (previously written) TIFF file content */
static tsize_t tiff_readproc(thandle_t clientdata, tdata_t data, tsize_t size)
{
	tiff_handle *th = (tiff_handle *)clientdata;
	gdIOCtx *ctx = th->ctx;

	size = (ctx->getBuf)(ctx, data, size);

	return size;
}

/* TIFFReadWriteProc tiff_writeproc - Will use gdIOCtx procs to write out 
   TIFF data */
static tsize_t tiff_writeproc(thandle_t clientdata, tdata_t data, tsize_t size)
{
	tiff_handle *th = (tiff_handle *)clientdata;
	gdIOCtx *ctx = th->ctx;

	size = (ctx->putBuf)(ctx, data, size);
	if(size + th->pos>th->size) {
		th->size = size + th->pos;
		th->pos += size;
	}

	return size;
}

/* TIFFSeekProc tiff_seekproc
 * used to move around the partially written TIFF */
static toff_t tiff_seekproc(thandle_t clientdata, toff_t offset, int from)
{
	tiff_handle *th = (tiff_handle *)clientdata;
	gdIOCtx *ctx = th->ctx;
	int result;

	switch(from) {
		default:
		case SEEK_SET:
			/* just use offset */
		break;

		case SEEK_END:
			/* invert offset, so that it is from start, not end as supplied */
			offset = th->size + offset;
		break;

		case SEEK_CUR:
			/* add current position to translate it to 'from start', 
			 * not from durrent as supplied
			 */
			offset += th->pos;
		break;
	}

	/* now, move pos in both io context and buf */
	if((result = (ctx->seek)(ctx, offset))) {
		th->pos = offset;
	}

	return result ? offset : -1;
}

/* TIFFCloseProc tiff_closeproc - used to finally close the TIFF file */
static int tiff_closeproc(thandle_t clientdata)
{
	tiff_handle *th = (tiff_handle *)clientdata;
	gdIOCtx *ctx = th->ctx;

	/*(ctx->gd_free)(ctx);*/

	return 0;
}

/* TIFFSizeProc tiff_sizeproc */
static toff_t tiff_sizeproc(thandle_t clientdata)
{
	tiff_handle *th = (tiff_handle *)clientdata;
	return th->size;
}

/* TIFFMapFileProc tiff_mapproc() */
static int tiff_mapproc(thandle_t h, tdata_t *d, toff_t *o)
{
	return 0;
}

/* TIFFUnmapFileProc tiff_unmapproc */
static void tiff_unmapproc(thandle_t h, tdata_t d, toff_t o)
{

}


/*  tiffWriter 
**  ----------
**  Write the gd image as a tiff file (called by gdImageTiffCtx)
**  Parameters are:
**  image:    gd image structure;
**  out:      the stream where to write
**  bitDepth: depth in bits of each pixel
 */
BGD_DECLARE(void) tiffWriter(gdImagePtr image, gdIOCtx *out, int bitDepth)
{
	int x, y;
	int i;
	int r, g, b, a;
	TIFF *tiff;
	int width;
	int height;
	int colour;
	char *scan;
	int rowsperstrip;
	int samplesPerPixel = 3;
	int bitsPerSample;
	int transparentColourR = -1;
	int transparentColourG = -1;
	int transparentColourB = -1;
	uint16 extraSamples[1];
	uint16 *colorMapRed;
	uint16 *colorMapGreen;
	uint16 *colorMapBlue;

	tiff_handle *th;
	th = new_tiff_handle(th, out);
	extraSamples[0] = EXTRASAMPLE_ASSOCALPHA;

	/* read in the width/height of gd image */
	width = gdImageSX(image);
	height = gdImageSY(image);

	/* reset clip region to whole image */
	gdImageSetClip(image, 0, 0, width, height);

	/* handle old-style single-colour mapping to 100% transparency */
	if(image->transparent != 0xffffffff) {
		/* set our 100% transparent colour value */
		transparentColourR = gdImageRed(image, image->transparent);
		transparentColourG = gdImageGreen(image, image->transparent);
		transparentColourB = gdImageBlue(image, image->transparent);
	}

	/* Open tiff file writing routines, but use special read/write/seek
	 * functions so that tiff lib writes correct bits of tiff content to
	 * correct areas of file opened and modifieable by the gdIOCtx functions
	 */
	tiff = TIFFClientOpen("", "w", th,	tiff_readproc,
										tiff_writeproc,
										tiff_seekproc,
										tiff_closeproc,
										tiff_sizeproc,
										tiff_mapproc,
										tiff_unmapproc);

	TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, width);
	TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, height);
	TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
	TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
	TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC,
					(bitDepth == 24) ? PHOTOMETRIC_RGB : PHOTOMETRIC_PALETTE);

	bitsPerSample = (bitDepth ==24) ? 8 : ((bitDepth == 8) ? 8 : 1);
	TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, bitsPerSample);

	/* build the color map for 8 bit images */
	if(bitDepth != 24) {
		/*TODO: Add checking */
		colorMapRed = (uint16 *) gdMalloc(3 * pow(2, bitsPerSample));
		colorMapGreen = (uint16 *) gdMalloc(3 * pow(2, bitsPerSample));
		colorMapBlue = (uint16 *) gdMalloc(3 * pow(2, bitsPerSample));

		for(i = 0; i < image->colorsTotal; i++) {
			colorMapRed[i] = gdImageRed(image,i) + (gdImageRed(image,i) * 256);
			colorMapGreen[i] =gdImageGreen(image,i)+(gdImageGreen(image,i)*256);
			colorMapBlue[i] = gdImageBlue(image,i) + (gdImageBlue(image,i)*256);
		}

		TIFFSetField(tiff, TIFFTAG_COLORMAP, colorMapRed, colorMapGreen,
															colorMapBlue);
		samplesPerPixel = 1;
	}

	/* here, we check if the 'save alpha' flag is set on the source gd image */
	if(	(bitDepth == 24) &&
		(image->saveAlphaFlag || image->transparent != 0xffffffff)) {
		/* so, we need to store the alpha values too! 
		 * Also, tell TIFF what the extra sample means (associated alpha) */
		samplesPerPixel = 4;
		TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, samplesPerPixel);
		TIFFSetField(tiff, TIFFTAG_EXTRASAMPLES, 1, extraSamples);
	} else {
		TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, samplesPerPixel);
	}

	TIFFSetField(tiff, TIFFTAG_ROWSPERSTRIP, 1);

	if(overflow2(width, samplesPerPixel)) {
		return;
	}

	if(!(scan = (char *)gdMalloc(width * samplesPerPixel))) {
		return;
	}

	/* loop through y-coords, and x-coords */
	for(y = 0; y < height; y++) {
		for(x = 0; x < width; x++) {
			/* generate scan line for writing to tiff */
			colour = gdImageGetPixel(image, x, y);

			a = (127 - gdImageAlpha(image, colour)) * 2;
			a = (a == 0xfe) ? 0xff : a & 0xff;
			b = gdImageBlue(image, colour);
			g = gdImageGreen(image, colour);
			r = gdImageRed(image, colour);

			/* if this pixel has the same RGB as the transparent colour,
			 * then set alpha fully transparent */
			if(	transparentColourR == r &&
				transparentColourG == g &&
				transparentColourB == b
			) {
				a = 0x00;
			}

			if(bitDepth != 24) {
				/* write out 1 or 8 bit value in 1 byte
				 * (currently treats 1bit as 8bit) */
				scan[(x * samplesPerPixel) + 0] = colour;
			} else {
				/* write out 24 bit value in 3 (or 4 if transparent) bytes */
				if(image->saveAlphaFlag || image->transparent != 0xffffffff) {
					scan[(x * samplesPerPixel) + 3] = a;
				}

				scan[(x * samplesPerPixel) + 2] = b;
				scan[(x * samplesPerPixel) + 1] = g;
				scan[(x * samplesPerPixel) + 0] = r;
			}
		}

		/* Write the scan line to the tiff */
		if(TIFFWriteEncodedStrip(tiff, y, scan, width * samplesPerPixel) == -1){
			/* error handler here */
			fprintf(stderr, "Could not create TIFF\n");
			return;
		}
	}

	/* now cloase and free up resources */
	TIFFClose(tiff);
	gdFree(scan);
	gdFree(th);

	if(bitDepth != 24) {
		gdFree(colorMapRed);
		gdFree(colorMapGreen);
		gdFree(colorMapBlue);
	}
}

/*  gdImageTiffCtx
**  --------------
**  Write the gd image as a tiff file
**  Parameters are:
**  image:  gd image structure;
**  out:    the stream where to write
 */
BGD_DECLARE(void) gdImageTiffCtx(gdImagePtr image, gdIOCtx *out)
{
	int clipx1P, clipy1P, clipx2P, clipy2P;
	int bitDepth = 24;

	/* First, switch off clipping, or we'll not get all the image! */
	gdImageGetClip(image, &clipx1P, &clipy1P, &clipx2P, &clipy2P);

	/* use the appropriate routine depending on the bit depth of the image */
	if(image->trueColor) {
		bitDepth = 24;
	} else if(image->colorsTotal == 2) {
		bitDepth = 1;
	} else {
		bitDepth = 8;
	}

	tiffWriter(image, out, bitDepth);

	/* reset clipping area to the gd image's original values */
	gdImageSetClip(image, clipx1P, clipy1P, clipx2P, clipy2P);
}


/* readColorMap
** ------------
** Used by tiff reading routines to generate a map between color indices
** and their actual RGB color values.
** return value is a chunk of memory containing 
 */

typedef struct
{
	uint16 *red;
	uint16 *green;
	uint16 *blue;
}
RgbContext;

static void readColorMap(TIFF *tiff, RgbContext ctx)
{
	uint32 dataSize;

	TIFFGetField(tiff, TIFFTAG_COLORMAP, &ctx.red, &ctx.green, &ctx.blue);
}

/* getRed, getGreen, getBlue
** -------------------------
** gets the red value for specified index from specified color map data 
** from source tiff file 
 */
BGD_DECLARE(uint16) getColor(TIFF *tiff, RgbContext ctx, int index, char color)
{
	uint16 bitsPerSample;
	uint16 val;
	uint32 offset;

	TIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bitsPerSample);
	offset = pow(2, bitsPerSample);

	if(color == 0) {
		val = ctx.red[index];
	}
	if(color == 1) {
		val = ctx.green[index];
	}
	if(color == 2) {
		val = ctx.blue[index];
	}

	val = (val >> 8);

	return val;
}

#define getRed(t,c,i) getColor(t,c,i,0)
#define getGreen(t,c,i) getColor(t,c,i,1)
#define getBlue(t,c,i) getColor(t,c,i,2)

/* createFromTiffCtx1bit
** ------------------------
** Create a gdImage from a TIFF file input from an gdIOCtx
** called by gdImageCreateFromTiffCtx() when bitsPerPixel==1
 */
BGD_DECLARE(gdImagePtr) createFromTiffCtx1bit(TIFF *tiff, int width, int height)
{
	gdImagePtr im = NULL;
	int x, y;
	int tileX, tileY, tileMaxX, tileMaxY;
	int i, j, k;
	int colour;
	char *scan;
	int compression;
	ttile_t numberOfTiles = 0;
	int tileNum;
	uint32 black, white;
	unsigned long long *bitmapAsVal;
	unsigned long long bitToTest;
	unsigned long long bitTestResult;
	unsigned char byteVal;
	unsigned char byteBitToTest;
	unsigned char byteBitTestResult;
	uint32 tileSize;
	RgbContext ctx;

	TIFFGetField(tiff, TIFFTAG_COMPRESSION, &compression);
 
	if(!(im = gdImageCreate(width, height))) {
		TIFFClose(tiff);
		return NULL;
	}

	/* allocate both black and white for this image */
	white = gdImageColorAllocate(im, 0xff, 0xff, 0xff); 
	black = gdImageColorAllocate(im, 0x00, 0x00, 0x00);

	numberOfTiles = TIFFNumberOfTiles(tiff);

	if(TIFFIsTiled(tiff)) {
		TIFFGetField(tiff, TIFFTAG_TILEWIDTH, &tileMaxX);
		TIFFGetField(tiff, TIFFTAG_TILELENGTH, &tileMaxY);

		tileSize = TIFFTileSize(tiff);
		if(!(scan = (char *) gdMalloc(tileSize))) {
			return 0;
		}

		tileX = tileY = 0;

		for(tileNum = 0; tileNum < numberOfTiles; tileNum++) {
			TIFFReadEncodedTile(tiff, tileNum, scan, tileSize); 

			/* setup coords for this pixel */
			x = tileX;
			y = tileY;

			for(i=0; i < tileSize; i += sizeof(unsigned long long)) {
				/* check if the next sizeof(unsigned long long) bytes are zero,
				 * if they are, move along */
				bitmapAsVal = (unsigned long long *)&scan[i];
				if(bitmapAsVal[0] != 0ull) {
					/* if this chunk has some black bits, write them out,
					 * one bit at a time */
					bitToTest = 1ull;

					for(j = 0; j < sizeof(unsigned long long) * 8; j++) {
						bitTestResult = bitmapAsVal[0] & bitToTest;

						/* if the bit at offset j is white, skip it */
						if(bitTestResult != 0ull) {
							gdImageSetPixel(im, x, y, black);
						}

						/* move onto next pixel in tile's gd target area */
						x++;
						if(x >= (tileX + tileMaxX)) {
							x = tileX;
							y++;
						}

						bitToTest *= 2;
					}
				} else {
					/* entire section was blank - move x on by section length */
					x += sizeof(unsigned long long) * 8;
					while(x >= (tileX + tileMaxX)) {
						x -= tileMaxX;
						y++;
					}
				}
			}
      
			/* move to the next tile offset */
			tileX += tileMaxX;
			if(tileX > width) {
				tileX = 0;
				tileY += tileMaxY;
			}
		}

		gdFree(scan);

	} else {
		/* image is not tiled - assume stripped (scanline) format */
		printf("is a 1bit scanline tiff!\n");

		if(!(scan = (char *) gdMalloc(TIFFScanlineSize(tiff)))) {
			return 0;
		}

		y = x = 0;

		for(i = 0; i < height; i++) {
			/* looping on number of scanlines - read source scanline */
			TIFFReadScanline(tiff, scan, i, 0);

			for(j = 0; j < TIFFScanlineSize(tiff); j++) {
				/* now, loop on content of scanline */
				byteVal = scan[j];
				byteBitToTest = 0x01;

				if(byteVal == 0) {
					x += 8;
					continue;
				}

				/* now, loop through this byte, setting all bits that are not
				 * the background color */
				for(k = 0; k < 8; k++) {
					byteBitTestResult = (byteVal & byteBitToTest);

					if(byteBitTestResult) {
						gdImageSetPixel(im, x, y, black);
					}

					x++;
					byteBitToTest *= 2;
				}
			}

			y++;
			x = 0;
		}

		gdFree(scan);
	}

	return im;
}

/* createFromTiffCtx8bit
** ------------------------
** Create a gdImage from a TIFF file input from an gdIOCtx
** called by gdImageCreateFromTiffCtx() when bitsPerPixel==8
 */
BGD_DECLARE(gdImagePtr) createFromTiffCtx8bit(TIFF *tiff, int width, int height)
{
	gdImagePtr im = NULL;
	int x, y;
	int i, j;
	int colorIndex;
	int colour;
	char *scan;
	uint32 numberOfTiles = 0;
	uint32 tileSize;
	int tileX, tileY;
	int tileMaxX, tileMaxY;
	int tileNum;
	uint32 color;
	char *colorMap;
	RgbContext ctx;

	if(!(im = gdImageCreate(width, height))) {
		TIFFClose(tiff);
		return NULL;
	}

	readColorMap(tiff, ctx);

	/* set the background of the image to the first color in the color map */
	gdImageColorAllocate(im, getRed(tiff, ctx, 0),
							getGreen(tiff, ctx, 0),
							getBlue(tiff, ctx, 0));

	numberOfTiles = TIFFNumberOfTiles(tiff);

	if(TIFFIsTiled(tiff)) {
		printf("is a 8bit tiled tiff! (%d tiles)\n",numberOfTiles);
		TIFFGetField(tiff, TIFFTAG_TILEWIDTH, &tileMaxX);
		TIFFGetField(tiff, TIFFTAG_TILELENGTH, &tileMaxY);

		tileSize = TIFFTileSize(tiff);
		if(!(scan = (char*) gdMalloc(tileSize))) {
			return 0;
		}

		tileX = tileY = 0;

		for(tileNum = 0; tileNum < numberOfTiles; tileNum++) {
			TIFFReadEncodedTile(tiff, tileNum, scan, tileSize);

			/* setup coords for this pixel */
			x = tileX;
			y = tileY;

			for(i = 0; i < tileSize; i++) {
				/* check if the next byte is zero */
				/* if it is - background already set so move along */
				colorIndex = scan[i];
				if(colorIndex != 0x0) {
					color = gdImageColorResolve(im,
											getRed(tiff, ctx, colorIndex),
											getGreen(tiff, ctx, colorIndex),
											getBlue(tiff, ctx, colorIndex));

					gdImageSetPixel(im, x, y, color);

					/* move onto next pixel in tile's gd target area */
					x++;
					if(x >= (tileX + tileMaxX)) {
						x = tileX;
						y++;
					}
				} else {
					/* move x on to next pixel offset */
					x++;
					if(x >= (tileX + tileMaxX)) {
						x = tileMaxX;
						y++;
					}
				}
			}

			/* move to the next tile offset */
			tileX += tileMaxX;
			if(tileX > width) {
				tileX = 0;
				tileY += tileMaxY;
			}
		}

		gdFree(scan);

	} else {
		if(!(scan = (char *)gdMalloc(TIFFScanlineSize(tiff)))) {
			return 0;
		}

		y = x = 0;

		/* image is not tiled - assume scanline based images */
		for(i = 0; i < height; i++) {
			/* looping on number of scanlines - read source scanline */
			TIFFReadScanline(tiff, scan, i, 0);

			for(j = 0; j < TIFFScanlineSize(tiff); j++) {
				colorIndex = scan[j];
				color = gdImageColorResolve(im, getRed(tiff, ctx, colorIndex),
												getGreen(tiff, ctx, colorIndex),
												getBlue(tiff, ctx, colorIndex));

				/*printf("%2x  %2x %2x %2x\n",colorIndex,getRed(tiff,colorIndex),getGreen(tiff,colorIndex),getBlue(tiff,colorIndex));*/

				gdImageSetPixel(im,x,y,color);
				x++;
			}
			
			y++;
			x = 0;
		}

		gdFree(scan);
	}

	return im;
}

/* createFromTiffCtx32bit
** ------------------------
** Create a 32 bit gdImage from a TIFF file input from an gdIOCtx
** - called by gdImageCreateFromTiffCtx() when bitsPerPixel>8bit
** This is essentially a fall-back routine; it's very slow but will 
** handle any format of tiff that libtiff supports.
 */
BGD_DECLARE(gdImagePtr) createFromTiffCtx32bit(TIFF *tiff, int width,int height)
{
	gdImagePtr im = NULL;
	int r, g, b, a;
	int x, y;
	int alphaBlendingFlag = 0;
	int colour;
	uint32 *scan;
	uint32 rgba;

	printf("32 bits\n");

	if(!(im = gdImageCreateTrueColor(width, height))) {
		TIFFClose(tiff);
		return NULL;
	}

	/* switch off colour merging on target gd image just while we write out
	 * content - we want to preserve the alpha data until the user chooses
	 * what to do with the image */
	alphaBlendingFlag = im->alphaBlendingFlag;
	gdImageAlphaBlending(im, 0);

	/* loop through y-coords, and x-coords */
	scan = calloc(sizeof(uint32), width * height);
	TIFFReadRGBAImage(tiff, width, height, scan, 0);

	for(y = 0; y < height; y++) {
		for(x = 0; x < width; x++) {
			/* if it doesn't already exist, allocate a new colour,
			 * else use existing one */
			rgba = scan[((y * width) + x)];
			a = (0xff - TIFFGetA(rgba)) / 2;

			b = TIFFGetB(rgba);
			g = TIFFGetG(rgba);
			r = TIFFGetR(rgba);

			colour = gdTrueColorAlpha(r, g, b, a);

			/* set pixel colour to this colour */
			gdImageSetPixel(im, x, height - y - 1, colour);
		}
	}

	gdFree(scan);

	/* now reset colour merge for alpha blending routines */
	gdImageAlphaBlending(im, alphaBlendingFlag);

	return im;
}

/* gdImageCreateFromTiffCtx
** ------------------------
** Create a gdImage from a TIFF file input from an gdIOCtx
 */
BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffCtx(gdIOCtx *infile)
{
	int width;
	int height;
	int compression;
	uint16 samplesPerPixel;
	uint16 bitsPerSample[4];
	int bitsPerPixel;
	TIFF *tiff;
	gdImagePtr im;

	tiff_handle *th;
	th = new_tiff_handle(th, infile);

	tiff = TIFFClientOpen("", "rb", th, tiff_readproc,
										tiff_writeproc,
										tiff_seekproc,
										tiff_closeproc,
										tiff_sizeproc,
										tiff_mapproc,
										tiff_unmapproc);


	TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);
	TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);
	TIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, bitsPerSample);
	TIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel);
	bitsPerPixel = bitsPerSample[0] * samplesPerPixel;

	if(bitsPerPixel == 1) {
		im = createFromTiffCtx1bit(tiff, width, height);
	} else if(bitsPerPixel == 8) {
		im = createFromTiffCtx8bit(tiff, width, height);
	} else {
		im = createFromTiffCtx32bit(tiff, width, height);
	}

	TIFFClose(tiff);
	gdFree(th);

	return im;
}

/* gdImageCreateFromTIFF
** ---------------------
 */
BGD_DECLARE(gdImagePtr) gdImageCreateFromTiff(FILE *inFile)
{
	gdImagePtr im;
	gdIOCtx *in = gdNewFileCtx(inFile);
	im = gdImageCreateFromTiffCtx(in);
	in->gd_free(in);
	return im;
}

BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffPtr(int size, void *data)
{
	gdImagePtr im;
	gdIOCtx *in = gdNewDynamicCtxEx (size, data, 0);
	im = gdImageCreateFromTiffCtx(in);
	in->gd_free(in);
	return im;
}

/* gdImageTIFF
** -----------
 */
BGD_DECLARE(void) gdImageTiff(gdImagePtr im, FILE *outFile)
{
	gdIOCtx *out = gdNewFileCtx(outFile);
	gdImageTiffCtx(im, out); /* what's an fg again? */
	out->gd_free(out);
}

/* gdImageTIFFPtr
** --------------
 */
BGD_DECLARE(void *) gdImageTiffPtr(gdImagePtr im, int *size)
{
	void *rv;
	gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
	gdImageTiffCtx(im, out); /* what's an fg again? */
	rv = gdDPExtractData(out, size);
	out->gd_free(out);
	return rv;
}

#endif