Login

Subversion Repositories NedoOS

Rev

Rev 400 | Rev 922 | Go to most recent revision | 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];
BYTE sizeword[4];
BYTE pic[1024][1024];
BYTE pixrow[1024/8][1024+1024];
#define PIXROWSHIFT 1024
BYTE maskrow[1024/8][1024];
//BYTE pixrowshift[1024/8][1024]; //>>4
BYTE attrrow[1024/8];
BYTE pal[64];

#define CONVORDERSZ 1024

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

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

char sprformat;

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);
}

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=='-') {sign = -1; goto skip;};
    if ((c<'0')||(c>'9')) break; //т Єюь ўшёых 0x0a
    num = num*10 + (int)(c-'0');
skip:
        ;
  }while(1);
  num = num*sign;
return num;
}

unsigned int readlabel(FILE * fin, char * s)
{
char c;
unsigned int i;
int iscomment;
  do{
    i = 0;
    iscomment = 0;
    do{
      if (!fread(&c,1,1,fin)) break;
      if (c == ';') iscomment = -1;
      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];

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') {
    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;
    };
  };
  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)
{
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 emitchr_(int xchr, int y, FILE * fout)
{
BYTE b;
int j;
  fputs("\tdb ", fout);
  j = y;
  while (1) {
    b = pixrow[xchr][j];
    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;
    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;

BYTE b;
BYTE bmask;
BYTE b0;

int sprx;
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);
            if (size == 0) break;
            readlabel(fintxt, formatlabelbuf); //format
            sprformat = *formatlabelbuf;
            sprx = readnum(fintxt);
            spry = readnum(fintxt);
            sprwid = readnum(fintxt);
            sprhgt = readnum(fintxt);
            tiles = readnum(fintxt); //юЄёєЄёЄтєхЄ т x
            defaultcolor = (BYTE)tiles; //фы  тёхї, ъЁюьх L

            if (sprformat == 'B') {
              fputs(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);
              fputs(labelbuf, fout);
              fputs("\n", fout);
              rowhgt = 8;
            }else if (sprformat == 'x') { //ёяЁрщЄ 16c
              fputs("\n", fout);
              fputs(labelbuf, fout);
              fputs("=$+4\n", fout);
              fputs("\n", fout);
              emitdb((BYTE)(sprwid>>1), fout);
              emitdb((BYTE)(sprhgt), fout);
              rowhgt = sprhgt;
            }else if (sprformat == 'i') { //ърЁЄшэър 16c яю ёЄюысЎрь
              fputs("\n", fout);
              fputs(labelbuf, fout);
              fputs("\n", fout);
              rowhgt = sprhgt;
            }else if (sprformat == 'L') { //LAND ъръ т ╫┬, фры№°х ёыхфєхЄ ЄрсышЎр - эюьхЁ Єрщыр фы  ърцфющ ъыхЄъш
              fputs("\n", fout);
              fputs(labelbuf, fout);
              fputs("\n", fout);
              rowhgt = sprhgt;
            }else if (sprformat == 'P') { //DDp palette
              fputs("\n", fout);
              fputs(labelbuf, fout);
              fputs("\n", fout);
              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);
              rowhgt = sprhgt;
            }else { //'s'
              fputs(labelbuf, fout);
              fputs("\n", fout);
              emitdb((BYTE)(sprwid>>3), fout);
              emitdb((BYTE)(sprhgt), fout);
              rowhgt = sprhgt;
            };

            y = spry;
            while (y < (spry+sprhgt)) {
              //яхЁхъюфшЁєхь Ё ф чэръюьхёЄ т√ёюЄющ rowhgt
              curink = 0x0f;
              curpaper = 0x08;
              x = sprx;
              while (x < (sprx+sprwid)) {
                findinkpaper(x, y);
                setcurinkpaper(&curink, &curpaper);
              //curink = 0x0f;
              //curpaper = 0x08;
                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++;
                    i++;
                  };
                  pixrow[x/8][j] = b;
                  maskrow[x/8][j] = bmask;
                  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') { //tiles
                x = sprx;
                while (x < (sprx+sprwid)) {
  //                emitchrshift(x/8,y,fout);
                  emitchr(x/8,y,fout);
                  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 == 's') { //sprite
                emitspr(sprx/8,y,sprwid/8,sprhgt,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 == '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;
              };

            };

          }; //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";

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

  resfile(finname, fintxtname, foutname);

  return 0;
}