- #include <stdio.h> 
- #include <stdlib.h> 
- //#include <mem.h> 
- #include <string.h> 
-   
- #define _STRMAX 79 
-   
- #define BYTE unsigned char 
- #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 ink; 
- BYTE paper; 
- BYTE curink; 
- BYTE curpaper; 
- BYTE defaultcolor; 
-   
- char sprformat; 
-   
- int hgt; 
- int wid; 
- int bpp; 
-   
- int readnum(FILE * fin) 
- { 
- char c; 
- int num; 
-   num = 0; 
-   do{ 
-     if (!fread(&- c ,1,1,- fin )) break;
 
-     if ((c<'0')||(c>'9')) break; //т Єюь ўшёых 0x0a 
-     num = num*10 + (int)(c-'0'); 
-   }while(1); 
- 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) 
- { 
-   fprintf(- fout , "0x%x%x",-  b >>4,-  b &0x0f);
 
- } 
-   
- void emitnops(BYTE count, FILE * fout) 
- { 
-   fprintf(- fout , "0x%x%x",-  count >>4,-  count &0x0f);
 
- } 
-   
- void emitspr(int xchr, int y, int sprwid8, int sprhgt, FILE * fout) 
- { 
- BYTE b; 
- int i; 
- int j; 
-   j = y; 
-   while (1) { 
-     i = xchr; 
-     while (1) { 
-       b = maskrow[i][j]; 
-       fprintf(- fout , "0x%x%x",-  b >>4,-  b &0x0f);
 
-       b = b^pixrow[i][j]; 
-       fprintf(- fout , "0x%x%x",-  b >>4,-  b &0x0f);
 
-       i++; 
-       if (i >= (xchr+sprwid8)) break; 
-     }; 
-     j++; 
-     if (j >= (y+sprhgt)) break; 
-   }; 
- } 
-   
- void emitchr_(int xchr, int y, FILE * fout) 
- { 
- BYTE b; 
- int j; 
-   j = y; 
-   while (1) { 
-     b = pixrow[xchr][j]; 
-     fprintf(- fout , "0x%x%x",-  b >>4,-  b &0x0f);
 
-     j++; 
-     if (j >= (y+8)) break; 
-   }; 
-   if (sprformat < 'a') { //capital letter format => attr used 
-     b = attrrow[xchr]; //0x07; 
-     fprintf(- fout , "0x%x%x",-  b >>4,-  b &0x0f);
 
-   }; 
- } 
-   
- 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; 
-   
- BYTE b; 
- BYTE bmask; 
- BYTE b0; 
-   
- int sprx; 
- int spry; 
- int sprwid; 
- int sprhgt; 
- int rowhgt; //8 for tiles, sprhgt for sprites 
-   
-   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,-  size -29,-  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) { 
-           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); 
-             defaultcolor = (BYTE)readnum(fintxt); 
-   
-             if (sprformat == 'B') { 
-               emitdb((BYTE)(sprwid>>3), fout); 
-               emitdb((BYTE)(sprhgt>>3), fout); 
-               rowhgt = 8; 
-             }else if (sprformat == 'T') { 
-               fputs("\tds (-$)&0xff\n",-  fout );
 
-               rowhgt = 8; 
-             }else { //'s' 
-               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 { //sprite 
-                 emitspr(sprx/8,y,sprwid/8,sprhgt,fout); 
-               }; 
-               y = y+rowhgt; 
-             }; 
-   
-           }; 
-         }else {printf("can't open %s",- foutname );};
 
-       }else {printf("can't open %s",- fintxtname );};
 
-     }; 
-   }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) { 
-       "NedoRes\n" 
-       "\tnedores.exe file.bmp file.txt file.asm\n" 
-     ); 
-   }else { 
-     finname = argv[1]; 
-     fintxtname = argv[2]; 
-     foutname = argv[3]; 
-   }; 
-   
-   resfile(finname, fintxtname, foutname); 
-   
-   return 0; 
- }