/* BMP2SPEC version 1.0 */
/* By the infamous BLOOD! */
/* (C)1996 Cheese Freak Software */
/* This prog is most definitely FREEWARE! */
/* But please don't distribute updated versions without asking first...! */

#include <stdio.h>
#include <stdlib.h>
#include "bmp2spec.h"

void main(int argc, char *argv[])
{
    FILE *fp1, *fp2;
    BYTE header[24],checkbit=255,data[6144], screen[6144];
    int count,count2,ptr,ptr2,ptr3,x,y,z,result;

    getheader(header);

    printf("\n\nBMP2SPEC version 1.0\n");
    printf("Written by the infamous BLOOD!\naka l.d.tonks@bra0202.wins.icl.co.uk\n");
    printf("(C)1996 Cheese Freak Software\n\n");
    printf("This program is FREEWARE! Don't pay more than 2p for it!\n\n");
    
    if(argc!=3)
    {
        printf("Usage : BMP2SPEC <bitmap file input> <tape file output>\n");
        printf("Example : BMP2SPEC jetset.bmp jetset.tap\n\n");
        exit(0);
    }
    else
    {
        printf("Converting %s to %s....\n",argv[1],argv[2]);
        fp1=fopen(argv[1],"rb");
        if(fp1==NULL)
        {
            printf("Error opening %s...!\n",argv[1]);
            exit(1);
        };
        fp2=fopen(argv[2],"wb");
        if(fp2==NULL)
        {
            printf("Error opening %s...!\n",argv[2]);
            exit(1);
        };

        result=fwrite(header,24,1,fp2);
        if(result!=1)
        {
            printf("Can't write to %s...!\n",argv[2]);
            exit(1);
        };
        
        result=fread(data,62,1,fp1);
        if(result!=1)
        {
            printf("Can't read from %s...!\n",argv[1]);
            exit(1);
        };

        printf("Reading bitmap data...\n");
        result=fread(screen,6144,1,fp1);
        if(result!=1)
        {
            printf("Read error in %s...!\n",argv[1]);
            exit(1);
        };
        
        printf("Sorting bitmap data...\n");
        ptr=0;
        ptr2=6144;
        ptr3=0;
        for(count=0;count<192;count++)
        {
            ptr2=ptr2-32;
            ptr3=ptr2;
            for(count2=0;count2<32;count2++)
            {
                data[ptr]=screen[ptr3];
                ptr++;
                ptr3++;
            };
        };

        printf("Creating Spectrum Screen...\n");        
        x=0;
        y=0;
        z=0;
        
        for(count=0;count<2048;count++)
        {
            screen[count]=data[x+(y*32)];
            screen[count]=screen[count]^255;
            checkbit = (checkbit ^ screen[count]);
            x++;
            if(x>=32)
            {
                x=0;
                y=y+8;
                if(y>=64)
                {
                    z++;
                    y=z;
                };
            };
        };

        x=0;
        y=64;
        z=64;
        
        for(count=2048;count<4096;count++)
        {
            screen[count]=data[x+(y*32)];
            screen[count]=screen[count]^255;
            checkbit = (checkbit ^ screen[count]);
            x++;
            if(x>=32)
            {
                x=0;
                y=y+8;
                if(y>=128)
                {
                    z++;
                    y=z;
                };
            };
        };

        x=0;
        y=128;
        z=128;
        
        for(count=4096;count<6144;count++)
        {
            screen[count]=data[x+(y*32)];
            screen[count]=screen[count]^255;
            checkbit = (checkbit ^ screen[count]);
            x++;
            if(x>=32)
            {
                x=0;
                y=y+8;
                if(y>=192)
                {
                    z++;
                    y=z;
                };
            };
        };

        printf("Writing TAP file...\n");
        result=fwrite(screen,6144,1,fp2);
        if(result!=1)
        {
            printf("Error writing to %s...!\n",argv[2]);
            exit(1);
        };

        data[0]=56;
        for(count=0;count<768;count++)
        {
            result=fwrite(data,1,1,fp2);
            if(result!=1)
            {
                printf("Error writing to %s...!\n",argv[2]);
                exit(1);
            };

        };
        
        result=fwrite(&checkbit,1,1,fp2);
        if(result!=1)
        {
            printf("Error writing to %s...!\n",argv[2]);
            exit(1);
        };
        
        printf("\nConversion done...!\n");

        fclose(fp1);
        fclose(fp2);
    };
}


void getheader(BYTE header[24])
{
    header[0]=19;    
    header[1]=0;    
    header[2]=0;    
    header[3]=3;    
    header[4]=115;    
    header[5]=99;    
    header[6]=114;    
    header[7]=101;    
    header[8]=101;    
    header[9]=110;    
    header[10]=32;    
    header[11]=32;    
    header[12]=32;    
    header[13]=32;    
    header[14]=0;    
    header[15]=27;    
    header[16]=0;    
    header[17]=64;    
    header[18]=0;    
    header[19]=128;    
    header[20]=212;    
    header[21]=2;    
    header[22]=27;    
    header[23]=255;    
}
