Login

Subversion Repositories NedoOS

Rev

Rev 1931 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#include <stdio.h>
#include <stdlib.h>
//#include <mem.h>
#include <string.h>

#define _STRMAX 79

#define BYTE unsigned char
#define UINT unsigned int
#define MAXDEFB 8

#define MASKCOLOR 0x00

BYTE filebuf[65536];
char labelbuf[_STRMAX+1];
char formatlabelbuf[_STRMAX+1];
char commentlabelbuf[_STRMAX+1];
BYTE sizeword[4];
BYTE pic[2048][1024];
BYTE pixrow[2048/8+1][1024+1024];
#define PIXROWSHIFT 1024
BYTE maskrow[2048/8+1][1024];
//BYTE pixrowshift[2048/8][1024]; //>>4
BYTE attrrow[2048/8+1];
BYTE pal[64];

#define CONVORDERSZ 1024

int convorderx[CONVORDERSZ]; //фы  ърцфюую эюьхЁр Єрщыр ъююЁфшэрЄ√
int convordery[CONVORDERSZ]; //фы  ърцфюую эюьхЁр Єрщыр ъююЁфшэрЄ√

int sprcount;
int vertsprcount;

BYTE numok;

BYTE ink;
BYTE paper;
BYTE curink;
BYTE curpaper;
BYTE defaultcolor;

char sprformat;
char globalsprformat;
BYTE invattr;
BYTE forceattr;

int hgt;
int wid;
int bpp;

void skiplf(FILE * fin)
{ //тючьюцэю, 0x0d єцх яЁюўшЄрэ, ЄхяхЁ№ яЁюяєёърхь 0x0a
char c;
  do{
    if (!fread(&c,1,1,fin)) break;
  }while (c!=0x0a);
}

void readcomment(FILE * fin, char * s)
{ //тючьюцэю, 0x0d єцх яЁюўшЄрэ, ЄхяхЁ№ яЁюяєёърхь 0x0a
char c;
unsigned int i;
  i = 0;
  do{
    if (!fread(&c,1,1,fin)) break;
    if (i==_STRMAX) break;
    s[i] = c;
    i++;
  }while (c!=0x0a);
  s[i] = '\0';
}

int readnum(FILE * fin)
{
char c;
int num;
int sign=1;
  num = 0;
  do{
    if (!fread(&c,1,1,fin)) break;
    if (c==' ') goto skip;
    //if (c == 0x0d) goto skip;
    //if (c == 0x0a) break;
    if (c=='!') {forceattr = 0xff; goto skip;};
    if (c=='-') {sign = -1; goto skip;};
    if ((c<'0')||(c>'9')) break; //т Єюь ўшёых 0x0a
    num = num*10 + (int)(c-'0');
    numok = 0xff;
skip:
        ;
  }while(1);
  num = num*sign;
return num;
}

unsigned int readlabel(FILE * fin, char * s)
{
char c;
unsigned int i;
int iscomment;
  sprcount = 0;
  vertsprcount = 0;
  do{
    i = 0;
    iscomment = 0;
    do{
      if (!fread(&c,1,1,fin)) break;
      if (c == ';') iscomment = -1;
      if (c == '~') {invattr = 0xff; continue;}
      if (c == '$') { //фры№°х ўшёыю ёяЁрщЄют тхЁЄшъры№эю
        if (iscomment) continue; //т ъюььхэЄрї ьюцэю $
        while (1) {
          if (!fread(&c,1,1,fin)) return 0;
          if ((c < '0') || (c > '9')) break;
          vertsprcount = vertsprcount*10;
          vertsprcount = vertsprcount + (int)(c-'0');
        };
      };
      if (c == '#') { //фры№°х ўшёыю ёяЁрщЄют
        if (iscomment) continue; //т ъюььхэЄрї ьюцэю #
        while (1) {
          if (!fread(&c,1,1,fin)) return 0;
          if ((c < '0') || (c > '9')) break;
          sprcount = sprcount*10;
          sprcount = sprcount + (int)(c-'0');
        };
      };
      if ((c == '=')||(c == ',')) {
        if (iscomment) continue; //т ъюььхэЄрї ьюцэю =
        break;
      };
      if (c == 0x0d) continue;
      if (c == 0x0a) {
        if (iscomment) break; //ъюььхэЄ√ ъюэўр■Єё  яю ъюэЎє ёЄЁюъш
        continue;
      };
      if (i==_STRMAX) break;
      s[i] = c;
      i++;
    }while(1);
    s[i] = '\0';
  }while(iscomment);
return i;
}

int read4b(FILE * fin)
{
  fread(sizeword, 4, 1, fin);
return sizeword[0] + (sizeword[1]<<8) + (sizeword[2]<<16) + (sizeword[3]<<24);
}

BYTE colstat[256];

char numbuf[256];

UINT maxsprcount;
UINT cursprcount;
UINT maxvertsprcount;
UINT curvertsprcount;

void putlabel(char * labelbuf, FILE * fout)
{
UINT num;
UINT i;
        fputs(labelbuf, fout);
        if (maxvertsprcount != 0) {
          fputc('_', fout);
          fputc('_', fout);
          num = curvertsprcount;

          i = 0;
          do {
            numbuf[i] = (char)((UINT)'0' + (num - (num/10)*10));
            num = num/10;
            i++;
          } while (num != 0);

          do {
            i--;
            fputc(numbuf[i], fout);
          } while (i != 0);
        };
        if (maxsprcount != 0) {
          fputc('_', fout);
          num = cursprcount;

          i = 0;
          do {
            numbuf[i] = (char)((UINT)'0' + (num - (num/10)*10));
            num = num/10;
            i++;
          } while (num != 0);

          do {
            i--;
            fputc(numbuf[i], fout);
          } while (i != 0);
        };
return;
}

void findinkpaper(int x, int y) //ьрёър 0x00 эх ёўшЄрхЄё  ЎтхЄюь
{
//BYTE b;
int i;
int j;
BYTE col1;
BYTE col1stat;
BYTE col2;
BYTE col2stat;
BYTE col3;
BYTE col3stat;
BYTE col4;
BYTE col4stat; //эр ёыєўрщ Єръющ ёЄрЄшёЄшъш: MASKCOLOR, RED, red, anothercolor
  i = 0; do {colstat[i] = 0x00; i++;}while (i!=256);

  //col1 = pic[x][y];
  j = y;
  while (j < (y+8)) {
//    b = 0x00;
    i = x;
    while (i < (x+8)) {
      colstat[pic[i][j]]++;
      //b = pic[i][j];
      //if (b!=col1) col2 = b;
      i++;
    };
    j++;
  };

//ьрёър MASKCOLOR эх ёўшЄрхЄё  ЎтхЄюь
  col1 = MASKCOLOR; col1stat = 0; //fix 27.12.2018
  col2 = MASKCOLOR; col2stat = 0; //fix 27.12.2018
  col3 = MASKCOLOR; col3stat = 0; //fix 27.12.2018
  col4 = MASKCOLOR; col3stat = 0; //fix 27.12.2018
  i = 0; do { //fix 27.12.2018 (эр ёыєўрщ MASKCOLOR!=0)
    if (colstat[i] > col1stat) {
      col4 = col3;
      col4stat = col3stat;
      col3 = col2;
      col3stat = col2stat;
      col2 = col1;
      col2stat = col1stat;
      col1 = (BYTE)i;
      col1stat = colstat[i];
    }else if (colstat[i] > col2stat) {
      col4 = col3;
      col4stat = col3stat;
      col3 = col2;
      col3stat = col2stat;
      col2 = (BYTE)i;
      col2stat = colstat[i];
    }else if (colstat[i] > col3stat) {
      col4 = col3;
      col4stat = col3stat;
      col3 = (BYTE)i;
      col3stat = colstat[i];
    }else if (colstat[i] > col4stat) {
      col4 = (BYTE)i;
      col4stat = colstat[i];
    };
    i++;
  }while (i!=16); //fix 27.12.2018

//MASKCOLOR эхы№ч  ёрь√ь ўрёЄ√ь, ёюЁЄшЁєхь хую т ъюэхЎ
  if (col1==MASKCOLOR) {
    col1 = col2;
    col2 = MASKCOLOR;
  };
  if (col2==MASKCOLOR) {
    col2 = col3;
    col3 = MASKCOLOR;
  };
  if (col3==MASKCOLOR) {
    col3 = col4;
    col4 = MASKCOLOR;
  };

//col1 ьюцхЄ с√Є№ MASKCOLOR Єюы№ъю фы  чэръюьхёЄр, яюыэюёЄ№■ чрышЄюую MASKCOLOR
//ьрёър 0x00 эх ёўшЄрхЄё  ЎтхЄюь, яю¤Єюьє 0x08 яхЁхтюфшЄё  т 0x00 ё фхЇюыЄэющ  ЁъюёЄ№■ (ўЄюс√ эх тыш Є№ эр bright)
  //if ((col1&0x07)==0x00) col1 = (BYTE)(defaultcolor&0x08); //fix 27.12.2018
  //if ((col2&0x07)==0x00) col2 = (BYTE)(defaultcolor&0x08); //fix 27.12.2018
/**  if (col2 == MASKCOLOR) {
    if (col1 == MASKCOLOR) {
      col2 = (BYTE)((col1&0x08) | (defaultcolor&0x07));
    }else {
      col2 = (BYTE)((col1&0x08) | (defaultcolor&0x07));
    };
  };*/
//fix 27.12.2018
  if (((col2&0x07)==(col1&0x07)) /**&& (col2!=0x00)*/) col2 = col3; //same colour with another bright
  ink = col1;
  paper = col2; //Ёхцх
  //paper Ёхцх ш ьюцхЄ с√Є№ MASKCOLOR (эхЄ тЄюЁюую ЎтхЄр, эрфю сЁрЄ№ фхЇюыЄэ√щ)
  //ink ьюцхЄ с√Є№ MASKCOLOR Єюы№ъю фы  чэръюьхёЄр, яюыэюёЄ№■ чрышЄюую MASKCOLOR
}

void setcurinkpaper(BYTE* pcurink, BYTE* pcurpaper)
//paper Ёхцх ш ьюцхЄ с√Є№ MASKCOLOR (эхЄ тЄюЁюую ЎтхЄр, эрфю сЁрЄ№ фхЇюыЄэ√щ)
//ink ьюцхЄ с√Є№ MASKCOLOR Єюы№ъю фы  чэръюьхёЄр, яюыэюёЄ№■ чрышЄюую MASKCOLOR
//*pcurink, *curpaper шчэрўры№эю ёюфхЁцрЄ рЄЁшсєЄ√ яЁхф√фє∙хую чэръюьхёЄр
//тючтЁр∙рхЄ *pcurink, *curpaper, яЁшў╕ь тьхёЄю 0x08 ёЄртшЄ 0x00 (ўЄюс√ эх тыш ыю эр  ЁъюёЄ№)
{
BYTE t;
  if ((sprformat == 's')||(sprformat == 'y')||(sprformat == 'Z')||(sprformat == 'z')||(sprformat == 'W')||(sprformat == 'w')) {
    paper = 0x08;
    ink = 0x0f;
  }; //фы  ёяЁрщЄют Їюэ ў╕Ёэ√щ (р ьрёър 0x00)

//27.02.2019:
//Їюэ фы  чрышЄ√ї чэръюьхёЄ эрўшэр  ё чхы╕эюую ЄхяхЁ№ ў╕Ёэ√щ[шыш эрфю яЁхф√фє∙шщ?]
//Їюэ фы  ў╕Ёэ√ї чэръюьхёЄ схЁ╕Є  ЁъюёЄ№ юЄ defaultcolor, р фы  чрышЄ√ї чэръюьхёЄ ьхэхх чхы╕эюую эх схЁ╕Є
#define MINCOLORWITHBLACKBG 0x04
//fix 27.12.2018:
  if (((ink&0x07)==(*pcurink&0x07)) && (ink!=MASKCOLOR)) { //ink ёююЄтхЄёЄтєхЄ яЁхф√фє∙хьє
    *pcurink = ink;
    if (paper==MASKCOLOR) {
      *pcurpaper = ((ink&0x07)<MINCOLORWITHBLACKBG) ? ((ink&0x07)?((defaultcolor&0x07)|(ink&0x08)):defaultcolor) : 0x00;
    }else {
      *pcurpaper = paper;
    };
  }else if (((paper&0x07)==(*pcurpaper&0x07)) && (paper!=MASKCOLOR)) { //paper ёююЄтхЄёЄтєхЄ яЁхф√фє∙хьє
    *pcurpaper = paper;
    if (ink==MASKCOLOR) {
      *pcurink = ((paper&0x07)<MINCOLORWITHBLACKBG) ? ((paper&0x07)?((defaultcolor&0x07)|(paper&0x08)):defaultcolor) : 0x00;
    }else {
      *pcurink = ink;
    };
  }else if (((paper&0x07)==(*pcurink&0x07)) && (paper!=MASKCOLOR)) { //paper ёююЄтхЄёЄтєхЄ яЁхф√фє∙хьє ink
    *pcurink = paper;
    if (ink==MASKCOLOR) {
      *pcurpaper = ((paper&0x07)<MINCOLORWITHBLACKBG) ? ((paper&0x07)?((defaultcolor&0x07)|(paper&0x08)):defaultcolor) : 0x00;
    }else {
      *pcurpaper = ink;
    };
  }else if (((ink&0x07)==(*pcurpaper&0x07)) && (ink!=MASKCOLOR)) { //ink ёююЄтхЄёЄтєхЄ яЁхф√фє∙хьє paper
    *pcurpaper = ink;
    if (paper==MASKCOLOR) {
      *pcurink = ((ink&0x07)<MINCOLORWITHBLACKBG) ? ((ink&0x07)?((defaultcolor&0x07)|(ink&0x08)):defaultcolor) : 0x00;
    }else {
      *pcurink = paper;
    };
  }else if ((ink==MASKCOLOR)&&(paper==MASKCOLOR)) { //юср ЎтхЄр MASKCOLOR
    ink = defaultcolor;
    paper = 0x08;
  }else { //юср ЎтхЄр эх ёююЄтхЄёЄтє■Є яЁхф√фє∙хьє чэръюьхёЄє, эю эх юср MASKCOLOR
    if (ink == MASKCOLOR) ink = ((paper&0x07)<MINCOLORWITHBLACKBG) ? ((paper&0x07)?((defaultcolor&0x07)|(paper&0x08)):defaultcolor) : 0x00;
    if (paper == MASKCOLOR) paper = ((ink&0x07)<MINCOLORWITHBLACKBG) ? ((ink&0x07)?((defaultcolor&0x07)|(ink&0x08)):defaultcolor) : 0x00;
    if (ink > paper) {
      *pcurink = ink;
      *pcurpaper = paper;
    }else {
      *pcurink = paper;
      *pcurpaper = ink;
    };
  };

    //*pcurink = ink;
    //*pcurpaper = paper;

  if (*pcurpaper==0x08) *pcurpaper = 0x00; //ўЄюс√ эх тыш ыю эр  ЁъюёЄ№ //fix 27.12.2018
  if (*pcurink==0x08) *pcurink = 0x00; //ўЄюс√ эх тыш ыю эр  ЁъюёЄ№ //fix 27.12.2018
  //эр ў╕Ёэюь/ёшэхь Їюэх ink фюыцхэ с√Є№  Ёўх paper'р (юЄфхы№э√щ ёыєўрщ, Є.ъ. юёЄры№э√х, фрцх яєёЄ√х, чэръюьхёЄр ьюуєЄ єўрёЄтютрЄ№ т юЄЁшёютъх ёъЁюыышЁєхь√ї Їюэют ё эхяЁхЁ√тэ√ьш Ўхяюўърьш ink-paper)
  //Єю хёЄ№ эхы№ч  ink=0, р 1 Єюы№ъю т ёыєўрх paper=0
  if (((*pcurink&0x07)<=0x01) && ((*pcurpaper&0x07)>(*pcurink&0x07))) { //ў╕Ёэюх/ёшэхх яєёЄюх чэръюьхёЄю (юЄфхы№э√щ ёыєўрщ, Є.ъ. юёЄры№э√х яєёЄ√х ьюуєЄ єўрёЄтютрЄ№ т юЄЁшёютъх ёъЁюыышЁєхь√ї Їюэют ё эхяЁхЁ√тэ√ьш Ўхяюўърьш ink-paper)
    t = *pcurpaper;
    *pcurpaper = *pcurink;
    *pcurink = t;
  };
}

void emitdb(BYTE b, FILE * fout)
{
  fputs("\tdb ", fout);
  fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  fputs("\n", fout);
}

void emitdw(UINT u, FILE * fout)
{
  fputs("\tdw ", fout);
  fprintf(fout, "0x%x%x%x%x", (u>>12)&0x0f, (u>>8)&0x0f, (u>>4)&0x0f, u&0x0f);
  fputs("\n", fout);
}

void emitnops(BYTE count, FILE * fout)
{
  fputs("\tds ", fout);
  fprintf(fout, "0x%x%x", count>>4, count&0x0f);
  fputs("\n", fout);
}

void emitspr(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
{ //b/w sprites: wid8, hgt, (antimask, antipixels, ...) ;for DizzySE!
BYTE b;
int i;
int j;
  j = y;
  while (1) {
    fputs("\tdb ", fout);
    i = xchr;
    while (1) {
      b = maskrow[i][j];
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      fputs(",", fout);
      b = b^pixrow[i][j];
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      i++;
      if (i >= (xchr+sprwid8)) break;
      fputs(",", fout);
    };
    fputs("\n", fout);
    j++;
    if (j >= (y+sprhgt)) break;
  };
}

void emitcolorspr(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
{ //coloured sprite, one row of attrchrs: (mask, antipixels, ...),attr
BYTE b;
int i;
int j;
  i = xchr;
  while (1) { //chrs in row
    fputs("\tdb ", fout);
    j = y;
    while (1) { //bytes in chr
      b = ~maskrow[i][j];
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      fputs(",", fout);
      b = b ^ pixrow[i][j] ^ invattr;
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      j++;
      if (j >= (y+8)) break;
      fputs(",", fout);
    };
    b = attrrow[xchr]; //0x07;
    if (invattr) b = (b&0xc0) + ((b&0x07)<<3) + ((b&0x38)>>3);
    if (forceattr) b = defaultcolor;
    fputs(",", fout);
    fprintf(fout, "0x%x%x", b>>4, b&0x0f);
    fputs("\n", fout);
    i++;
    if (i >= (xchr+sprwid8)) break;
  };
}

void emitcolorline(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
{ //for coloured line: 8 lines of pixels (no attrs)
BYTE b;
int i;
int j;
  j = y;
  while (1) { //lines
    fputs("\tdb ", fout);
    i = xchr;
    while (1) { //chrs in row
      b = pixrow[i][j] ^ invattr;
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      i++;
      if (i >= (xchr+sprwid8)) break;
      fputs(",", fout);
    };
    fputs("\n", fout);
    j++;
    if (j >= (y+8)) break;
  };
}

void emitattrline(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
{
BYTE b;
int i;
  fputs("\tdb ", fout);
  i = xchr;
  while (1) { //chrs in row
    b = attrrow[i]; //0x07;
    if (invattr) b = (b&0xc0) + ((b&0x07)<<3) + ((b&0x38)>>3);
    if (forceattr) b = defaultcolor;
    fprintf(fout, "0x%x%x", b>>4, b&0x0f);
    i++;
    if (i >= (xchr+sprwid8)) break;
    fputs(",", fout);
  };
  fputs("\n", fout);
}

void emitspry(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
{
BYTE b;
int i;
int j;
  i = xchr;
  while (1) {
    fputs("\tdb ", fout);
    j = y;
    while (1) {
      b = ~maskrow[i][j];
      b = b^pixrow[i][j];
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      fputs(",", fout);
      //b = maskrow[i][j];
      b = pixrow[i][j];
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      j++;
      if (j >= (y+sprhgt)) break;
      fputs(",", fout);
    };
    fputs("\n", fout);
    i++;
    if (i >= (xchr+sprwid8)) break;
  };
}

void emitsprw(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
{ //antipixelsline, antimaskline
BYTE b;
int i;
int j;
  j = y;
  while (1) {
    fputs("\tdb ", fout);
    i = xchr;
    while (1) {
      b = ~maskrow[i][j];
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      i++;
      if (i == xchr+sprwid8) break;
      fputs(",", fout);
    };
    fputs("\n", fout);
    fputs("\tdb ", fout);
    i = xchr;
    while (1) {
      b = ~maskrow[i][j];
      b ^= pixrow[i][j];
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      i++;
      if (i == xchr+sprwid8) break;
      fputs(",", fout);
    };
    fputs("\n", fout);
    j++;
    if (j >= (y+sprhgt)) break;
  };
}

void emitsprwback(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
{ //antipixelsw, antimaskw
BYTE b;
int i;
int j;
  j = y;
  while (1) {
    fputs("\tdb ", fout);
    i = xchr+sprwid8;
    while (1) {
      i--;
      b = ~maskrow[i][j];
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      if (i == xchr) break;
      fputs(",", fout);
    };
    fputs("\n", fout);
    fputs("\tdb ", fout);
    i = xchr+sprwid8;
    while (1) {
      i--;
      b = ~maskrow[i][j];
      b ^= pixrow[i][j];
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      if (i == xchr) break;
      fputs(",", fout);
    };
    fputs("\n", fout);
    j++;
    if (j >= (y+sprhgt)) break;
  };
}

void emitsprwnomask(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
{ //antipixelsw
BYTE b;
int i;
int j;
  j = y;
  while (1) {
    fputs("\tdb ", fout);
    i = xchr;
    while (1) {
      b = ~maskrow[i][j];
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      i++;
      if (i == xchr+sprwid8) break;
      fputs(",", fout);
    };
    fputs("\n", fout);
    j++;
    if (j >= (y+sprhgt)) break;
  };
}

void emitsprwnomaskback(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
{ //antipixelsw
BYTE b;
int i;
int j;
  j = y;
  while (1) {
    fputs("\tdb ", fout);
    i = xchr+sprwid8;
    while (1) {
      i--;
      b = ~maskrow[i][j];
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      if (i == xchr) break;
      fputs(",", fout);
    };
    fputs("\n", fout);
    j++;
    if (j >= (y+sprhgt)) break;
  };
}

void emitimgW(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
{ //by columns
BYTE b;
int i;
int j;
  i = xchr;
  while (1) {
    fputs("\tdb ", fout);
    j = y;
    while (1) {
      b = maskrow[i][j];
                  //fprintf(fout, "0x%x%x ", i>>4, i&0x0f);
                  //fprintf(fout, "0x%x%x ", j>>4, j&0x0f);
      fprintf(fout, "0x%x%x", b>>4, b&0x0f);
      j++;
      if (j >= (y+sprhgt)) break;
      fputs(",", fout);
    };
    fputs("\n", fout);
    i++;
    if (i == (xchr+sprwid8)) break;
  };
}

void emitchr_(int xchr, int y, FILE * fout)
{
BYTE b;
int j;
  fputs("\tdb ", fout);
  j = y;
  while (1) {
    b = pixrow[xchr][j] ^ invattr;
    fprintf(fout, "0x%x%x", b>>4, b&0x0f);
    j++;
    if (j >= (y+8)) break;
    fputs(",", fout);
  };
  if (sprformat < 'a') { //capital letter format => attr used
    b = attrrow[xchr]; //0x07;
    if (invattr) b = (b&0xc0) + ((b&0x07)<<3) + ((b&0x38)>>3);
    if (forceattr) b = defaultcolor;
    fputs(",", fout);
    fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  };
  fputs("\n", fout);
}

void emitchr(int xchr, int y, FILE * fout)
{
  emitchr_(xchr, y, fout);
}

void emitchrshift(int xchr, int y, FILE * fout)
{
  emitchr_(xchr, y+PIXROWSHIFT, fout);
}

//ёфтшурхь Ё ф чэръюьхёЄ >>shiftbits, Ёхчєы№ЄрЄ т pixrow[sprx][y+PIXROWSHIFT]
void shiftrow(int sprx, int y, int sprwid, int rowhgt, int pixrowshift, BYTE shiftbits)
{
int j;
int x;
BYTE b;
BYTE b0;
BYTE shiftmask;
  shiftmask = (BYTE)(0xff>>(0x08-shiftbits)); //хёыш shiftbits==0x01, Єю shiftmask==0x01
  j = y;
  while (j < (y+rowhgt)) {
    b = 0x00;
    x = sprx;
    while (x < (sprx+sprwid+8)) {
      b0 = pixrow[x/8][j];
      pixrow[x/8][j+pixrowshift] = (BYTE)((b<<(0x08-shiftbits)) + (b0>>shiftbits));
      b = (BYTE)(b0&shiftmask/**0x0f*/); //хёыш shiftbits==0x01, Єю shiftmask==0x01
      x = x+8;
    };
    j++;
  };
}

//data: wid8, hgt8, chrgfx, chrgfx...
void resfile(char * finname, char * fintxtname, char * foutname)
{
FILE* fin;
FILE* fintxt;
FILE* fout;
int i;
int j;
int size;
int y;
int x;
int n;
int tiles;
int xi;
int yi;

BYTE b;
BYTE bmask;
BYTE b0;

int sprx;
int startsprx;
int spry;
int sprwid;
int sprhgt;
int rowhgt; //8 for tiles, sprhgt for sprites

UINT color;

  fin = fopen(finname, "rb");
  if (fin) {
    fread(filebuf, 10, 1, fin); //skip to 10 (header size)
    size = read4b(fin); //10 (header size)
    fread(filebuf, 4, 1, fin); //skip to 18
    wid = read4b(fin); //18
    hgt = read4b(fin); //22
    fread(filebuf, 2, 1, fin); //skip to 28
    fread(&bpp, 1, 1, fin); //28
    fread(filebuf, 1, 54-29, fin); //skip to pal
    fread(pal, 1, 64, fin); //ярышЄЁр (B, G, R, 0)
    if (size > (54+64)) {fread(filebuf, 1, size-(54+64), fin);}; //skip to pic
    if ((wid>0)&&(wid<=1024)&&(hgt>0)&&(hgt<=1024)&&((wid&7)==0)&&((hgt&7)==0)) {
      y = hgt;
      while (y>0) {
        y--;
        x = 0;
        while (x<wid) {
          fread(&b, 1, 1, fin);
          if (bpp == 8) {
            pic[x][y] = b;
            x++;
          }else {
            pic[x][y] = (BYTE)((b&0xf0)>>4);
            x++;
            pic[x][y] = (BYTE)(b&0x0f);
            x++;
          };
        };
      };

      fintxt = fopen(fintxtname, "rb");
      if (fintxt) {
        fout = fopen(foutname, "wb");
        if (fout) {
/**          if (labelname[0]!='\0') {
            fputs(labelname, fout);
            fputs("\n", fout);
          };*/

          while (1) {
            size = readlabel(fintxt, labelbuf); //fread(filebuf, 1, MAXDEFB, fin);
           maxvertsprcount = vertsprcount;
           maxsprcount = sprcount;
            if (size == 0) break;
            invattr = 0x00;
            readlabel(fintxt, formatlabelbuf); //format
            sprformat = *formatlabelbuf;
            if (globalsprformat != '\0') sprformat = globalsprformat;
            startsprx = readnum(fintxt);
            spry = readnum(fintxt);
            sprwid = readnum(fintxt);
            sprhgt = readnum(fintxt);
            numok = 0x00;
            forceattr = 0x00;
            tiles = readnum(fintxt); //юЄёєЄёЄтєхЄ т x
            defaultcolor = (BYTE)tiles; //фы  тёхї, ъЁюьх L
            *commentlabelbuf = '\0';
            if ((numok != 0x00) && (sprformat != 'L')) readcomment(fintxt, commentlabelbuf);
           curvertsprcount = 0;
           do { //vertsprcount
           cursprcount = 0;
           sprx = startsprx;
           do { //sprcount
            if ((sprformat == 'B')||(sprformat == 'b')) {
              putlabel(labelbuf, fout);
              fputs("\n", fout);
              //emitdb((BYTE)(sprwid>>3), fout);
              //emitdb((BYTE)(sprhgt>>3), fout);
              rowhgt = 8;
            }else if (sprformat == 'T') { //эрсюЁ Єрщыют
              fputs("\tds (-$)&0xff\n", fout);
              putlabel(labelbuf, fout);
              fputs("\n", fout);
              rowhgt = 8;
            }else if (sprformat == 'x') { //ёяЁрщЄ 16c
              fputs("\n", fout);
              putlabel(labelbuf, fout);
              fputs("=$+4\n", fout);
              fputs("\n", fout);
              emitdb((BYTE)(sprwid>>1), fout);
              emitdb((BYTE)(sprhgt), fout);
              rowhgt = sprhgt;
            }else if ((sprformat == 'i')||(sprformat == 'I')) { //ърЁЄшэър 16c яю ёЄюысЎрь
              fputs("\n", fout);
              putlabel(labelbuf, fout);
              fputs("\n", fout);
              rowhgt = sprhgt;
            }else if (sprformat == 'L') { //LAND ъръ т ╫┬, фры№°х ёыхфєхЄ ЄрсышЎр - эюьхЁ Єрщыр фы  ърцфющ ъыхЄъш
              fputs("\n", fout);
              putlabel(labelbuf, fout);
              fputs("\n", fout);
              rowhgt = sprhgt;
            }else if (sprformat == 'P') { //DDp palette
              fputs("\n", fout);
              putlabel(labelbuf, fout);
              fputs("\n", fout);
              rowhgt = sprhgt;
              i = 0;
              while (i < 64) { //DDp palette: %grbG11RB(low),%grbG11RB(high), шэтхЁёэ√х //color = highlow
                color = 0; //pal = ярышЄЁр (B, G, R, 0)
                if (pal[i]&0x80) color = color | 0x0100;
                if (pal[i]&0x40) color = color | 0x2000;
                if (pal[i]&0x20) color = color | 0x0001;
                if (pal[i]&0x10) color = color | 0x0020;
                i++;
                if (pal[i]&0x80) color = color | 0x1000;
                if (pal[i]&0x40) color = color | 0x8000;
                if (pal[i]&0x20) color = color | 0x0010;
                if (pal[i]&0x10) color = color | 0x0080;
                i++;
                if (pal[i]&0x80) color = color | 0x0200;
                if (pal[i]&0x40) color = color | 0x4000;
                if (pal[i]&0x20) color = color | 0x0002;
                if (pal[i]&0x10) color = color | 0x0040;
                i++;
                emitdw(~color, fout);
                i++;
              };
              fputs("\n", fout);
            }else if ((sprformat == 'w')||(sprformat == 'W')||(sprformat == 'z')||(sprformat == 'Z')||(sprformat == 'y')) {
              putlabel(labelbuf, fout);
              fputs("\n", fout);
              rowhgt = sprhgt;
            }else if (sprformat == 'c') {
              putlabel(labelbuf, fout);
              fputs("\n", fout);
              rowhgt = sprhgt;
            }else if (sprformat == 's') { //for DizzySE
              putlabel(labelbuf, fout);
              fputs("\n", fout);
              emitdb((BYTE)(sprwid>>3), fout);
              emitdb((BYTE)(sprhgt), fout);
              rowhgt = sprhgt;
            }else if (sprformat == 'S') {
              putlabel(labelbuf, fout);
              fputs("\n", fout);
              rowhgt = 8;
            }else if ((sprformat == 'A')||(sprformat == 'a')) {
              putlabel(labelbuf, fout);
              fputs("\n", fout);
              rowhgt = 8;
            };

//copy comment as code line
              fputs(commentlabelbuf, fout);
              //fputs("\n", fout);

            y = spry;
            while (y < (spry+sprhgt)) {
              //яхЁхъюфшЁєхь Ё ф чэръюьхёЄ т√ёюЄющ rowhgt
              curink = 0x0f;
              curpaper = 0x08;
              x = 0;//sprx;
              while (x < (sprx+sprwid)) {
                findinkpaper(x, y);
                setcurinkpaper(&curink, &curpaper);
              //curink = 0x0f;
              //curpaper = 0x08;
//  if (sprformat == 'c') {
//    curpaper = 0;
//    curink = 1;
//  };
                j = y;
                while (j < (y+rowhgt)) {
                  b = 0x00;
                  bmask = 0x00;
                  i = x;
                  while (i < (x+8)) {
                    b = (BYTE)(b<<1);
                    bmask = (BYTE)(bmask<<1);
                    //if (sprformat != 'B') {fprintf(fout, "0x%x%x\n", (pic[i][j])>>4, (pic[i][j])&0x0f);};
                    if (pic[i][j]==curink) b++;
                    if (pic[i][j]!=0x00) bmask++;
                    if (pic[i][j]==0x10) {bmask--; b++;}
                    i++;
                  };
                  pixrow[x/8][j] = b;
                  maskrow[x/8][j] = bmask;
                  //fprintf(fout, "0x%x%x ", x>>4, x&0x0f);
                  //fprintf(fout, "0x%x%x ", j>>4, j&0x0f);
                  //fprintf(fout, "0x%x%x ", b>>4, b&0x0f);
                  //fprintf(fout, "0x%x%x\n", bmask>>4, bmask&0x0f);
                  pixrow[(x/8)+1][j] = 0x00; //ўЄюс√ ёфтшурЄ№
                  j++;
                };
//ьрёър 0x00 эх ёўшЄрхЄё  ЎтхЄюь, яю¤Єюьє 0x08 яхЁхтюфшЄё  т 0x00 (ўЄюс√ эх тыш Є№ эр bright)
                b = 0x00; if (curink!=0x08) b = curink;
                b0 = 0x00; if (curpaper!=0x08) b0 = curpaper;
                attrrow[x/8    ] = (BYTE)( (((b|b0)&0x08)<<3)+((curpaper&0x07)<<3)+(curink&0x07) );
                attrrow[(x/8)+1] = (BYTE)( (((b|b0)&0x08)<<3)+((curpaper&0x07)<<3)+(curink&0x07) ); //ўЄюс√ ёфтшурЄ№
                x = x+8;
              };
  //            shiftrow(sprx, y, sprwid, rowhgt, PIXROWSHIFT, 0x04); //ёфтшурхь Ё ф чэръюьхёЄ >>4, Ёхчєы№ЄрЄ т pixrow[sprx][y+PIXROWSHIFT]

              //т√тюфшь т рёь
              if ((sprformat == 'B')||(sprformat == 'b')) { //tiles or bw tiles
                x = sprx;
                while (x < (sprx+sprwid)) {
  //                emitchrshift(x/8,y,fout);
                  emitchr(x/8,y,fout); //checks for capital letter in sprformat
                  x = x+8;
                };
  //              emitchrshift(x/8,y,fout);
              }else if (sprformat == 'T') {
                x = sprx;
                while (x < (sprx+sprwid)) {
                  emitchr(x/8,y,fout);
                  x = x+8;
                };
                emitnops((BYTE)(0x100-((BYTE)(sprwid>>3)*0x09)),fout);
              }else if (sprformat == '%') { //mirror
                  xi = 0;
                  while (xi < (sprwid/2)) {
                    //fprintf(fout, ";0%x ", sprx+xi);
                    yi = y;
                    while (yi < (y+sprhgt)) {
                      b = pic[sprx+xi][yi]; //L
                      pic[sprx+xi][yi] = pic[sprx+sprwid-1-xi][yi]; //new R
                      pic[sprx+sprwid-1-xi][yi] = b; //new L
                      yi = yi+1;
                    };
                    xi = xi+1;
                  };
                  //fprintf(fout, "\n");
              }else if (sprformat == 'A') { //for coloured lines
                emitcolorline(sprx/8,y,sprwid/8,sprhgt,fout);
              }else if (sprformat == 'a') { //attr lines
                emitattrline(sprx/8,y,sprwid/8,sprhgt,fout);
              }else if (sprformat == 's') { //sprite
                emitspr(sprx/8,y,sprwid/8,sprhgt,fout);
              }else if (sprformat == 'S') { //coloured sprite
                emitcolorspr(sprx/8,y,sprwid/8,sprhgt,fout);
              }else if (sprformat == 'y') { //spritey
                emitspry(sprx/8,y,sprwid/8,sprhgt,fout);
              }else if (sprformat == 'w') { //sprite antipixels16, antimask16 right to left
                emitsprwback(sprx/8,y,sprwid/8,sprhgt,fout);
              }else if (sprformat == 'W') { //sprite antipixels16, antimask16
                emitsprw(sprx/8,y,sprwid/8,sprhgt,fout);
              }else if (sprformat == 'z') { //unmasked sprite right to left
                emitsprwnomaskback(sprx/8,y,sprwid/8,sprhgt,fout);
              }else if (sprformat == 'Z') { //unmasked sprite
                emitsprwnomask(sprx/8,y,sprwid/8,sprhgt,fout);
              }else if (sprformat == 'c') { //b/w image by columns
                emitimgW(sprx/8,y,sprwid/8,sprhgt,fout);
                //emitimgW(0,0,128,128,fout);
              };
              y = y+rowhgt;
            }; //while y

            if (sprformat == 'x') {
              x = sprx;
              while (x < (sprx+sprwid)) {
                y = spry;
                while (y < (spry+sprhgt)) {
                  b = pic[x][y]; //L
                  b0 = pic[x+1][y]; //R
                  bmask = 0; //0x47(L) ш 0xb8(R) т Єхї ьхёЄрї, уфх ЎтхЄ=16:
                  if (b == 16) {bmask = bmask + 0x47; b = 0x00;};
                  if (b0 == 16) {bmask = bmask + 0xb8; b0 = 0x00;};
                  b = ((b&0x08)<<3) + (b&0x07) + ((b0&0x08)<<4) + ((b0&0x07)<<3);
                  fputs("\tdb ", fout);
                  fprintf(fout, "0x%x%x", bmask>>4, bmask&0x0f);
                  fprintf(fout, ",0x%x%x", b>>4, b&0x0f);
                  fputs("\n", fout);
                  y = y+1;
                };
                x = x+2;
                if (x < (sprx+sprwid)) {
                  emitdw(0x4000-((sprhgt-1)*40), fout);
                }else {
                  emitdw(0xffff, fout);
                };
                fputs("\n", fout);
              };
              fputs("\tdw prsprqwid\n", fout);
            };

            if (sprformat == 'i') {
              x = sprx;
              while (x < (sprx+sprwid)) {
                y = spry;
                while (y < (spry+sprhgt)) {
                  b = pic[x][y]; //L
                  b0 = pic[x+1][y]; //R
                  b = ((b&0x08)<<3) + (b&0x07) + ((b0&0x08)<<4) + ((b0&0x07)<<3);
                  fprintf(fout, "\tdb 0x%x%x", b>>4, b&0x0f);
                  fputs("\n", fout);
                  y = y+1;
                };
                x = x+2;
                fputs("\n", fout);
              };
            };

            if (sprformat == 'I') {
              x = sprx;
              while (x < (sprx+sprwid)) {
                BYTE mode = 255;
                BYTE newmode = 0;
                BYTE transp;
                int count = 0;
                int sumhgt = 0;
                y = spry;
                while (1) { //y
                  //fprintf(fout, "\ty=%d\n", y);

                  if (y != (spry+sprhgt)) {
                  //яюёўшЄрЄ№ ўшёыю яЁючЁрўэ√ї яшъёхыхщ
                    transp = 0;
                    xi = x;
                    while (xi < (x+8)) {
                      if (pic[xi][y] == 16) transp = transp+1;
                      xi = xi+1;
                    }
                    if (transp == 0) { newmode = 1; //ld zone
                    }else if (transp == 8) { newmode = 0; //empty zone
                    }else { newmode = 2; //and:or zone
                    }
                    if (mode == 255) { mode = newmode; };
                  };

                  if (((newmode != mode)||(y == (spry+sprhgt)))&&(count != 0)) {
                    //fprintf(fout, "\ty=%d, transp=%d, count=%d, mode=%d, newmode=%d\n", y, transp, count, mode, newmode);
                    fprintf(fout, "\tdb %d,%d\n", mode, count);
                    sumhgt = sumhgt + count;
                    yi = y-count;
                    while (yi != y) {
                      xi = x;
                      if (mode == 1) { //ld zone
                        fprintf(fout, "\tdb ");
                        while (1) {
                          b = pic[xi][yi]; //L
                          b0 = pic[xi+1][yi]; //R
                          b = ((b&0x08)<<3) + (b&0x07) + ((b0&0x08)<<4) + ((b0&0x07)<<3);
                          fprintf(fout, "0x%x%x", b>>4, b&0x0f);
                          xi = xi+2;
                          if (xi == (x+8)) break;
                          fputs(",", fout);
                        }
                        fputs("\n", fout);
                      //}else if (mode == 0) { //empty zone
                      }else if (mode == 2) { //and:or zone
                        fprintf(fout, "\tdb ");
                        while (1) {
                          b = pic[xi][yi]; //L
                          b0 = pic[xi+1][yi]; //R
                          bmask = 0; //0x47(L) ш 0xb8(R) т Єхї ьхёЄрї, уфх ЎтхЄ=16:
                          if (b == 16) {bmask = bmask + 0x47; b = 0x00;};
                          if (b0 == 16) {bmask = bmask + 0xb8; b0 = 0x00;};
                          b = ((b&0x08)<<3) + (b&0x07) + ((b0&0x08)<<4) + ((b0&0x07)<<3);
                          fprintf(fout, "0x%x%x", bmask>>4, bmask&0x0f);
                          fprintf(fout, ",0x%x%x", b>>4, b&0x0f);
                          xi = xi+2;
                          if (xi == (x+8)) break;
                          fputs(",", fout);
                        };
                        fputs("\n", fout);
                      };
                      yi = yi+1;
                    };
                    mode = newmode;
                    count = 0;
                  };
                  count = count+1;

                  if (y >= (spry+sprhgt)) break;

                  y = y+1;
                };
                x = x+8;
                //fprintf(fout, "\tsumhgt=%d\n", sumhgt);
                fputs("\n", fout);
              };
            };

            if (sprformat == 'L') { //фрыхх ЄхъёЄ Єшяр (-1=яЁюяєёъ):
//   -1, -1, -1,114,116,119,121,124,126,-1,-1,-1,-1,-1,-1,-1,
//  113,118,123,115,117,120,122,125,127,-1,-1,-1,-1,-1,-1,-1
//фы  ърцфющ  ўхщъш ърЁЄшэъш єърчрэ эюьхЁ Єрщыр
//р эрь эрфю чряюыэшЄ№ ьрёёшт√ convorderx,y - ъююЁфшэрЄ√ фы  ърцфюую эюьхЁр Єрщыр
//тёх фюыцэ√ с√Є№ т юфэющ ърЁЄшэъх, шэрўх эх яюыєўшЄё  (яхЁхьх°рэ√ эюьхЁр Єрщыют юс∙шх фы  тёхї ыюърЎшщ ш фы  ъюэъЁхЄэющ)
              n = 0;
              while (n < CONVORDERSZ) {
                convorderx[n] = 0;
                convordery[n] = 0;
                n = n+1;
              };

                skiplf(fintxt);
              //tiles = 0;

              y = spry;
              while (y < (spry+sprhgt)) {
                x = sprx;
                while (x < (sprx+sprwid)) {
                  n = readnum(fintxt);
                  if (n != -1) {
                    convorderx[n] = x;
                    convordery[n] = y;
                  };
                  //fprintf(fout, "\tdb %d\n", n);
                  //tiles = tiles + 1;
                  x = x+16;
                };
                skiplf(fintxt);
                //fputs("\n", fout);
                y = y+16;
              };

              n = 0;
              while (n < tiles) {
                x = convorderx[n];
                while (x < (convorderx[n]+16)) {
                  fputs(" db ", fout);
                  y = convordery[n];
                  while (1) {
                    b = pic[x][y]; //L
                    b0 = pic[x+1][y]; //R
                    b = ((b&0x08)<<3) + (b&0x07) + ((b0&0x08)<<4) + ((b0&0x07)<<3);
                    fprintf(fout, "0x%x%x", b>>4, b&0x0f);
                    y = y+1;
                    if (y == (convordery[n]+16)) break;
                    fputs(",", fout);
                  };
                  fputs("\n", fout);
                  x = x+2;
                };
                n = n+1;
              };

            };
            sprx = sprx + sprwid;
            cursprcount++;
           } while (cursprcount<maxsprcount); //while (sprcount)
            spry = spry + sprhgt;
            curvertsprcount++;
           } while (curvertsprcount<maxvertsprcount); //while (vertsprcount)
          }; //while (1)
          fclose(fout);
        }else {printf("can't open %s",foutname);};
        fclose(fintxt);
      }else {printf("can't open %s",fintxtname);};
    };
    fclose(fin);
  }else {printf("can't open %s",finname);};
}

int main(int argc,char* argv[])
{
//  int i;
  char *finname;
  char *fintxtname;
  char *foutname;
  finname = "testpic.bmp";
  fintxtname = "testpic.txt";
  foutname = "testpic.asm";
  globalsprformat = '\0';

  if (argc<4) {
    printf(
      "NedoRes\n"
      "\tnedores.exe file.bmp file.dat(=txt) file.ast(=asm) [-f<format>]\n"
      "4bpp or 8bpp\n"
    );
  }else {
    finname = argv[1];
    fintxtname = argv[2];
    foutname = argv[3];
    if (argc > 4) if (argv[4][0] == '-') if (argv[4][1] == 'f') globalsprformat = argv[4][2];
  };

  resfile(finname, fintxtname, foutname);

  return 0;
}