#include <stdio.h>
#include <stdlib.h>

void main(int argc,char *argv[])
{
    FILE *fp;
    char p[0x80][0x80];
    int xp[100];
    int yp[100];
    int chp[100];
    int r;
    int lp1;
    int lp2;
    int rt;
    int x;
    int y;
    int count;
    int max_p;
    int pp;
    int cp;
    int cx;
    int cy;
    int sn;  /*screen no*/
    int rm;
    double xmag;
    double ymag;
    
    rm = 0;
    xmag = 1.0;
    ymag = 1.0;
	
    if (argc < 5 || argc > 8) {
	printf("entry error.\n");
	printf("pcv2 <filename[full name]> <screen no.[0-3]> <center point no.[1-]> <point no> [R] [Xmag(100=100%)] [Ymag]\n");
	exit(1);
    }

    count = atoi(argv[4]);
printf("count=<%d>\n",count);
    
    if (argc >= 5 && *argv[4] == 'R')
	rm = 1;
    
    if (argc >= (6 + rm) )
	xmag = (double)atoi(argv[5+rm]) / 100.0;
	
    if (argc >= (7 + rm) )
	ymag = (double)atoi(argv[6+rm]) / 100.0;
	
    fp = fopen(argv[1], "r");
    if (fp == NULL) {
	printf("file <%s> not found.\n", argv[1]);
	exit(1);
    }
    cp = atoi(argv[3]);
    sn = *argv[2] - 0x30;
    rt = fseek(fp, sn * 128 * 128);
   if ( rt != 0) {
	printf("seek err.\n");
	exit(1);
    }
    
    for (lp1 = 0 ; lp1 < 0x80 * 0x80 ; lp1++) {
	x = (lp1 % 8) + (8 * ((lp1 /64) % 0x10));
	y = ((lp1 / 8) % 8) + (lp1 / 64 / 0x10) * 8;
	if (x < 0 || y < 0 || x > 0x80 || y >  0x80) {
	    printf("point calc err.\n");
	    exit(1);
	}
	fread(&p[x][y], 1, 1, fp);
    }
    fclose(fp);

    /*initial*/
    for (lp1 = 0 ; lp1 < 100 ; lp1++) {
	chp[lp1] = 0x00;
    }
    max_p = 0;
    
    for (lp1 = 0 ; lp1 < 0x80 ; lp1++) {
	for ( lp2 = 0 ; lp2 < 0x080 ; lp2++) {
	    pp = p[lp1][lp2] - 1;
	    if (pp > 0 && pp < 0xe0) {
		if (chp[pp] != 0)
		    printf("[0%02XH]point double!!\n",pp);
		xp[pp] = lp1;
		yp[pp] = lp2;
		chp[pp] = 1;
/*		count++;*/
		if (pp > max_p) {
		    max_p = pp;
		}
	    }
	}
    }

    for (lp1 = 1 ; lp1 <= count ; lp1++) {
	if (chp[lp1] == 0) {
	    if (lp1 == 1 ) {
		printf("begin point not found.\n");
		exit(1);
	    }
	    printf(";point<%2d> = point<%2d>\n",lp1,lp1 -1);
	    xp[lp1] = xp[lp1-1];
	    yp[lp1] = yp[lp1-1];
	}
    }
    cx = xp[cp];
    cy = yp[cp];
    
    for ( lp1 = 1 ; lp1 <= count ; lp1++) {
	xp[lp1] = (double)(xp[lp1] - cx) * xmag;
	yp[lp1] = (double)(yp[lp1] - cy) * ymag;
    }
    printf(";file <%s>\n",argv[1]);
    printf(";screen no.<%d>\n",sn);
    printf(";point kazu <0%02xH>\n",count);
    printf(";xmag=[%06.2f]\n",xmag);
    printf(";ymag=[%06.2f]\n",ymag);
    if (rm == 0) {
	printf(";turn sw off\n");
    }
    else
	printf(";turn sw on\n");
    if (rm == 0) {
	for  ( lp1 = 1 ; lp1 <= count ; lp1++) {
	    if ((lp1 % 4) == 1) {
		printf("\t\tBYTE\t");
	    }
	    printf("0%02XH,0%02XH",(unsigned char)xp[lp1],(unsigned char)yp[lp1]);
	    if ((lp1 % 4) == 0  || lp1 == count) {
		printf("\n");
	    }
	    else {
		printf(", ");
	    }
	}
	printf(";\n");
    }
    else {
	for  ( lp1 = 1 ; lp1 <= count ; lp1++) {
	    if ((lp1 % 4) == 1) {
		printf("\t\tBYTE\t");
	    }
	    printf("0%02XH,0%02XH",(unsigned char)(xp[count-lp1+1] * -1),(unsigned char)(yp[count-lp1+1]));
	    if ((lp1 % 4) == 0  || lp1 == count) {
		printf("\n");
	    }
	    else {
		printf(", ");
	    }
	}
    }
}




