
/* STRIPIFY: Add the Spectrum loading stripes to a Windows 95 startup screen
    Copyright (C) 1997,2000  John Elliott

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include	<stdio.h>
#include	<stdlib.h>
#include 	<time.h>

static unsigned char chkhdr[] = 
{
	'B', 'M', 				/* Signature */
	0x36, 0xF8, 0x01, 0x00,	/* Length */
	0x00, 0x00, 0x00, 0x00,	/* Reserved */
	0x36, 0x04, 0x00, 0x00,	/* Address of bitmap data */
	0x28, 0x00, 0x00, 0x00, /* Length of header info */
	0x40, 0x01, 0x00, 0x00, /* Picture width */
	0x90, 0x01, 0x00, 0x00, /* Picture height */
};


void main(int argc, char **argv)
{
	FILE *fp, *fp2;
	long n;
	int byt, c, bpl, cindex, lim1;
	
	if (argc < 4)
	{
		fprintf(stderr,"STRIPIFY: Syntax is STRIPIFY <source> <dest> <colour>\n\n"
					   "<source> is a BMP file in 320x400x256 format\n"
					   "<dest>   is the BMP file to create\n"
					   "<colour> is the index of the first colour to use for stripes\n");
		exit(1);
	}
	fp  = fopen(argv[1], "rb");

	if (!fp)
	{
		perror(argv[1]);
		exit(1);
	}

	fp2 = fopen(argv[2], "wb");
	
	if (!fp2)
	{
		fclose(fp);
		perror(argv[2]);
		exit(1);
	}

	cindex = atoi(argv[3]);
	lim1 = 0x36 + 4*cindex;

	srand(time(NULL));
	for (n = 0; n < sizeof(chkhdr); n++)
	{
		c = fgetc(fp);

		if (c == EOF) 
		{
			fprintf(stderr,"Unexpected EOF\n");
			fclose(fp);
			fclose(fp2);
			exit(1);
		}
		if (c != chkhdr[n])
		{
			fprintf(stderr,"Source file is not a 320x400x256 BMP\n");
			fclose(fp);
			fclose(fp2);
			exit(1);
		}
		fputc(c, fp2);
	}		
	
	for (n = sizeof(chkhdr); n < lim1; n++)
	{
		c = fgetc(fp);

		if (c == EOF) 
		{
			fprintf(stderr,"Unexpected EOF\n");
			fclose(fp);
			fclose(fp2);
			exit(1);
		}
		if (n == 0x32) c = 0x10;
		if (n == 0x33) c = 0x00;
		
		fputc(c, fp2);
	}
	for (n = lim1; n < 0x436; n+=4)
	{
		byt = (rand() & 1);

		fgetc(fp); fgetc(fp); fgetc(fp); fgetc(fp);

		if (feof(fp)) 
		{
			fprintf(stderr,"Unexpected EOF\n");
			fclose(fp);
			fclose(fp2);
			exit(1);
		}

		if (byt)
		{
			fputc(0xFF, fp2);
			fputc(0x00, fp2);
			fputc(0x00, fp2);
			fputc(0x00, fp2);
		}
		else
		{
			fputc(0x00, fp2);
			fputc(0xFF, fp2);
			fputc(0xFF, fp2);
			fputc(0x00, fp2);
		}
	}

	bpl = 0;
	for (n = 0x436; n <= 0x1F835; n++)
	{
		c = fgetc(fp);

		if (!bpl)
		{
			byt = (rand() % (256-cindex)) + cindex;
		}

		if (c == EOF) 
		{
			fprintf(stderr,"Unexpected EOF\n");
			fclose(fp);
			fclose(fp2);
			exit(1);
		}
		if (c >= cindex) fputc(byt, fp2); else fputc(c, fp2);
		++bpl;
		if (bpl == 1280) bpl = 0;
	}
	fclose(fp);
	fclose(fp2);
	exit(0);
}
