/***********************************************************
 *
 *  IMAGEfy.c
 *
 *  macht aus einem ILBM-Bild (z. B. einem Brush aus Deluxe
 *  Paint) C-Quelltext für Intuitions Image-Struktur.
 *  Braucht jiff.c mit jiff.h (Public Domain).
 *
 *  Aufruf:
 *  IMAGEfy <Quelle> [<Ziel>]
 *  Wird keine Zieldatei angegeben, so wird <Quelle>.c als
 *  Ziel angenommen.
 *
 *  Peter Gober, 23-Jan-89
 *
 ***********************************************************/

 
#include <exec/types.h>
#include <graphics/gfx.h>
#include <stdio.h>
#include "jiff.h"


struct ILBM_info *info;
extern int Enable_Abort;

main(argc, argv)
int argc;
char *argv[];
{
	char *outname;                  /* Ausgabedateiname */
	FILE *fopen(), *outstr;
	int plane, row;
	register int byte;
	register PLANEPTR adr;
	struct ILBM_info *read_iff();
	char *strcat(), *strcpy(), *malloc();

	printf("IMAGEfy 1.0 23-Jan-89 (C) Peter Gober\n"); 

	/*
	 *  Argumente verarbeiten
	 */

	if (argc < 2 || argc > 3 || *argv[1] == '?')
	{
		printf("Aufruf: IMAGEfy <Quelle> [<Ziel>]\n");
		exit(0);
	}
	else if (argc == 3)
	{
		/*
		 *  Zweites Argument für Ausgabedatei angegeben
		 */

		outname = argv[2];
	}
	else if (argc == 2)
	{
		/*
		 *  Ausgabedateiname selbst erzeugen durch Anhängen von .c
		 */

		if (!(outname = malloc(strlen(argv[1]) + 3)))
			exit(0);
		strcpy(outname, argv[1]);
		strcat(outname, ".c");
	}
 	
	/*
    	 *  Bild lesen
  	 */
 
	Enable_Abort = 0;
	if (!(info = read_iff(argv[1], 0)))
	{
		printf("Kann %s nicht lesen.\n", argv[1]);
		exit(0);
	}
	Enable_Abort = 1;
	
	/*
	 *  Ausgabedatei vorbereiten
         */

	if (!(outstr = fopen(outname, "w")))
	{
		printf("Kann Ausgabedatei %s nicht öffnen.\n", outname);
		free_planes(&info->bitmap);
		exit(0);
	}

	/*
	 *  Ausgabe als C-Quelltext: USHORT imagedata[] = ...
	 */
	
	fprintf(outstr, "/* %s */\n", argv[1]);
	fprintf(outstr, "\n");
	fprintf(outstr, "#include <exec/types.h>\n");
	fprintf(outstr, "#include <intuition/intuition.h>\n");
	fprintf(outstr, "\n");
	fprintf(outstr, "USHORT imagedata[] =\n");
	fprintf(outstr, "{\n");
	for (plane = 0; plane < info->bitmap.Depth; plane++)
	{
		adr = info->bitmap.Planes[plane];
		for (row = 0; row < info->bitmap.Rows; row++)
		{
			fprintf(outstr, "\t");
			for (byte = 0;
			     byte < info->bitmap.BytesPerRow;
		  	     byte += 2)
			{
				fprintf(outstr, "0x%04x, ",
					* (UWORD *) adr);
				adr += 2;
			}
			fprintf(outstr, "\n");
		}
		if (plane != info->bitmap.Depth - 1)
			fprintf(outstr, "/**/\n");
	}
	fprintf(outstr, "};\n");
	fprintf(outstr, "\n");

	/*
	 *  struct Image ...
         */

	fprintf(outstr, "struct Image image =\n");
	fprintf(outstr, "{\n");
	fprintf(outstr, "\t0, 0,      \t\t/* LeftEdge, TopEdge */\n");
	fprintf(outstr, "\t%d, %d, %d,\t\t/* Width, Height, Depth */\n",
		info->header.w, info->header.h, info->bitmap.Depth);
	fprintf(outstr, "\timagedata, \t\t/* ImageData */\n");
	fprintf(outstr, "\t0x00, 0x00,\t\t/* PlanePick, PlaneOnOff */\n");
	fprintf(outstr, "\tNULL       \t\t/* NextImage */\n");
	fprintf(outstr, "};\n");

	/*
   	 *  Alles wieder freigeben
	 */ 

	free_planes(&info->bitmap);
        
	return(0);
}

_abort()
{
	printf("***Abbruch\n");
	free_planes(&info->bitmap);
	exit(0);
}
