#include <stdio.h>
#import <dpsclient/event.h>
#import <appkit/appkit.h>
#import <appkit/NXImage.h>
#import <appkit/NXEPSImageRep.h>
#import <appkit/NXBitmapImageRep.h>
#import <appkit/View.h>
#import <appkit/Window.h>
#import <appkit/graphics.h>
#import <appkit/tiff.h>
#import <streams/streams.h>

// to compile cc -g -o eps2tiff eps2tiff.m -lNeXT_s 

void main(argc, argv)
int	argc;
char	*argv[];
{
NXStream		*fp, *fpz;
id			myRep, myWindow;
NXSize		theSize;
char			filename[80];
extern char   *optarg;
extern int     optind;
int 			scale = 1;
int			c;
int			depth = 24;
//added by GWR, 10/27/92
int			compressionType = NX_TIFF_COMPRESSION_NONE;
int			errflg = 0;
id			myTiffRep; 
float			compressionFactor = 0.0;
optind = 1;

while ((c = getopt(argc, argv, "zj:d:s:")) != EOF) {
	  
	switch (c) {
		case 'd': {
			(void) sscanf (optarg, "%d", &depth);
			break;
			}
		case 's': {
			(void) sscanf (optarg, "%d", &scale);
			break;
			}
		case 'z': {
			compressionType = NX_TIFF_COMPRESSION_LZW;
			break;
			}
		case 'j': {
			compressionType = NX_TIFF_COMPRESSION_JPEG;
			(void) sscanf (optarg, "%f", &compressionFactor);
			break;
			}
		 default: {
                         errflg++;
			 }
		 }
	 if (errflg) {
           fprintf(stderr, "Usage:eps2tiff [-d nn] [-s nn] [-z] [-j nn] infile outfile\n");
           exit(2);
           }

	}

strcpy(filename, argv[optind++]);
fprintf(stderr,"input = %s scale = %d\n", filename, scale);
[Application new];
myWindow = [[Window alloc] init];
[myWindow setDepthLimit: NX_TwentyFourBitRGBDepth]; 
fp = NXOpenMemory(NULL, 0, NX_READWRITE);
myRep = [[NXImage alloc] initFromFile:filename];
[myRep getSize: &theSize];  /* this is where you get the size */
[myRep setScalable: YES];  /* this is where you change it! */
theSize.width *= scale;
theSize.height *= scale;
[myRep setSize: &theSize];
[myRep setBackgroundColor: NX_COLORWHITE];
[myRep setCacheDepthBounded:NO];
if ([myRep isCacheDepthBounded]) fprintf(stderr,"CacheDepth is Bounded\n");
switch (depth) {
	case 2: {
		[myRep useCacheWithDepth: NX_TwoBitGrayDepth];
		break;
		}
	case 8: {
		[myRep useCacheWithDepth: NX_EightBitGrayDepth];
		break;
		}
	case 12: {
		[myRep useCacheWithDepth: NX_TwelveBitRGBDepth];
		break;
		}
	case 24: {
		[myRep useCacheWithDepth: NX_TwentyFourBitRGBDepth];
		break;
		}
	default: {
		fprintf(stderr, "You want a depth of %d but I can only give\
you 2, 8, 12 or 24 bit depth...\nyou will get 24 bits this time\n", depth);
		}
	}
	

[myRep writeTIFF: fp  allRepresentations: NO];

if (((compressionType == NX_TIFF_COMPRESSION_LZW)||(compressionType == NX_TIFF_COMPRESSION_JPEG)) &&(depth==2)){
	fprintf(stderr, "Unable to compress a 2-bit grayscale image: saving as uncompressed\n");
	compressionType = NX_TIFF_COMPRESSION_NONE;
	}

switch (compressionType) {
	
	case NX_TIFF_COMPRESSION_NONE: {
		NXSaveToFile(fp, argv[optind]);
		NXCloseMemory(fp, NX_FREEBUFFER);
		break;
		}
		
	case NX_TIFF_COMPRESSION_JPEG:{
		fpz = NXOpenMemory(NULL, 0, NX_READWRITE);
		if (!(myTiffRep = [[NXBitmapImageRep alloc] initFromStream:fp]))
			fprintf(stderr,"myTiffRep failed initialization\n");
		NXCloseMemory(fp, NX_FREEBUFFER);
		fprintf(stderr, "Compressing file %s as jpeg(%f)... ",filename, compressionFactor);
		[myTiffRep writeTIFF:fpz usingCompression:NX_TIFF_COMPRESSION_JPEG andFactor:compressionFactor];
		fprintf(stderr, "Writing %s \n",argv[optind]);
		NXSaveToFile(fpz, argv[optind]);		
		NXCloseMemory(fpz, NX_FREEBUFFER);
		break;
		}

	case NX_TIFF_COMPRESSION_LZW:{
		fpz = NXOpenMemory(NULL, 0, NX_READWRITE);
		if (!(myTiffRep = [[NXBitmapImageRep alloc] initFromStream:fp]))
			fprintf(stderr,"myTiffRep failed initialization\n");
		NXCloseMemory(fp, NX_FREEBUFFER);
		fprintf(stderr, "Compressing file %s as lzw-tiff... ",filename);
		[myTiffRep writeTIFF:fpz usingCompression:NX_TIFF_COMPRESSION_LZW];
		fprintf(stderr, "Writing %s \n",argv[optind]);
		NXSaveToFile(fpz, argv[optind]);		
		NXCloseMemory(fpz, NX_FREEBUFFER);
		break;
		}
 	}
}