/*************************************************************************
*	TIF画像データをテキスト形式に変換する
*************************************************************************/

#include	<stdio.h>
#include	<stdlib.h>

#ifndef	CONST
#	define	CONST	const
#endif
#ifndef	NORMAL
#	define	NORMAL	(0)
#endif
#ifndef	ERR
#	define	ERR		(-1)
#endif

typedef struct
{
	short			tag;
	short			typ;
	long			len;
	unsigned char	p[4];
} TIFTAG_T;

typedef struct
{
	char	format[2];
	short	version;
	long	offset;
} TIFHEAD_T;

int		tif_to_txt( CONST char *fninp, CONST char *fnout );

int		main( int argc, char **argv )
{
	char	*fninp = NULL, *fnout = NULL;

	if ( argc < 2 )
		return (1);	/* error */
	fninp = argv[1];
	if ( argc > 2 )
		fnout = argv[2];
	if ( tif_to_txt(fninp,fnout) )
		return (1);
	return (0);
}

int		getval( long *val, TIFTAG_T *tag )
{
	switch( tag->typ )
	{
		case 1: /* byte */
			*val = tag->p[0];
			break;
		case 2: /* ascii */
			return (ERR);
		case 3: /* short */
			*val = tag->p[0] + (tag->p[1]<<8);
			break;
		case 4: /* long */
			*val = tag->p[0] + (tag->p[1]<<8) + (tag->p[2]<<16) + (tag->p[3]<<24);
			break;
		case 5: /* rational */
			break;
		default:
			return (ERR);
	}
	return (NORMAL);
}

int		tif_to_txt( CONST char *fninp, CONST char *fnout )
{
	FILE	*fpinp, *fpout;

	if ( fninp )
	{
		if ( (fpinp = fopen(fninp,"rb")) == NULL )
			return (ERR);
	} else
		return (ERR);

	if ( fnout )
	{
		if ( (fpout = fopen(fnout,"w")) == NULL )
			return (ERR);
	} else
		fpout = stdout;

	fseek(fpinp,0l,SEEK_SET);
	{
		TIFHEAD_T	tifHead;
		short		i, entry;
		int			xsImg, ysImg;
		long		ofsImg = 0, sizImg = 0;

		fread(&tifHead,sizeof(TIFHEAD_T),1,fpinp);
		fprintf(stderr,"format        %c%c\n", tifHead.format[0], tifHead.format[1] );
		fprintf(stderr,"version    %5d\n"    , tifHead.version);
		fprintf(stderr,"offset  %08lXH\n"    , tifHead.offset);
		fseek( fpinp,tifHead.offset,SEEK_SET);

		fread( &entry, sizeof(short),1,fpinp);
		fprintf(stderr,"entry      %5d\n"    , entry);
		for ( i = 0; i < entry; ++ i )
		{
			TIFTAG_T	tifTag;
			long		v;

			fread( &tifTag, sizeof(TIFTAG_T),1,fpinp);
			if ( getval(&v,&tifTag) )
			{
				fprintf(stderr,"Unknown tag %04X\n", tifTag.tag );
				continue;
			}
			switch ( tifTag.tag )
			{
				case 0xFE:
					break;
				case 0x100:	/* image width */
					xsImg = (int)v;
					fprintf(stderr,"Image width       %4d\n", (int)v );
					break;
				case 0x101:	/* image length */
					ysImg = (int)v;
					fprintf(stderr,"Image length      %4d\n", (int)v );
					break;
				case 0x102:	/* bits per sample */
					fprintf(stderr,"Bits per sample   %4d\n", (int)v );
					break;
				case 0x103:	/* Compression	*/
					fprintf(stderr,"Compression       %4d\n", (int)v );
					break;
				case 0x106:	/* Photo.Interp. */
					fprintf(stderr,"Photo.Interp.     %4d\n", (int)v );
					break;
				case 0x10A:	/* Fill order */
					fprintf(stderr,"Fill order        %4d\n", (int)v );
					break;
				case 0x111:
					ofsImg = v;
					fprintf(stderr,"Strip offset      %4ld\n", v );
					break;
				case 0x115:
					fprintf(stderr,"Samples per pixel %4d\n", (int)v );
					break;
				case 0x116:
					fprintf(stderr,"Row per strip     %4ld\n", v );
					break;
				case 0x117:
					sizImg = v;
					fprintf(stderr,"Strips bytecount  %4ld\n", v );
					break;
				case 0x119:
					fprintf(stderr,"Max sample Value  %4d\n", (int)v );
					break;
				case 0x11A:
					fprintf(stderr,"X resolusion      %4d\n", (int)v );
					break;
				case 0x11B:
					fprintf(stderr,"Y resolusion      %4d\n", (int)v );
					break;
				case 0x11C:
					fprintf(stderr,"Planer config.    %4d\n", (int)v );
					break;
				case 0x140:	/* palette */
					break;
			}
		}
		if ( ofsImg > 0 && sizImg > 0 )
		{
			long		i;

			fprintf(fpout,"/* xs:%3d,ys=%3d,size=%ld */\n",xsImg, ysImg, sizImg );
			fseek(fpinp,ofsImg,SEEK_SET);
			for ( i = 0; i < sizImg; ++i )
			{
				unsigned char	dat;

				fread(&dat,1,1,fpinp);
				fprintf(fpout,"0x%02X,",dat);
				if ( (i & 7) == 7 )
					fputc('\n',fpout);
			}
		}
	}
	fclose(fpinp);
	if ( fnout )
		fclose(fpout);

	return (NORMAL);
}
