Subversion Repositories NedoOS

Rev

Rev 1479 | Rev 1713 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //#include <mem.h>
  4. #include <string.h>
  5.  
  6. #define _STRMAX 79
  7.  
  8. #define BYTE unsigned char
  9. #define UINT unsigned int
  10. #define MAXDEFB 8
  11.  
  12. #define MASKCOLOR 0x00
  13.  
  14. BYTE filebuf[65536];
  15. char labelbuf[_STRMAX+1];
  16. char formatlabelbuf[_STRMAX+1];
  17. char commentlabelbuf[_STRMAX+1];
  18. BYTE sizeword[4];
  19. BYTE pic[1024][1024];
  20. BYTE pixrow[1024/8+1][1024+1024];
  21. #define PIXROWSHIFT 1024
  22. BYTE maskrow[1024/8+1][1024];
  23. //BYTE pixrowshift[1024/8][1024]; //>>4
  24. BYTE attrrow[1024/8+1];
  25. BYTE pal[64];
  26.  
  27. #define CONVORDERSZ 1024
  28.  
  29. int convorderx[CONVORDERSZ]; //фы  ърцфюую эюьхЁр Єрщыр ъююЁфшэрЄ√
  30. int convordery[CONVORDERSZ]; //фы  ърцфюую эюьхЁр Єрщыр ъююЁфшэрЄ√
  31.  
  32. int sprcount;
  33. int vertsprcount;
  34.  
  35. BYTE numok;
  36.  
  37. BYTE ink;
  38. BYTE paper;
  39. BYTE curink;
  40. BYTE curpaper;
  41. BYTE defaultcolor;
  42.  
  43. char sprformat;
  44.  
  45. int hgt;
  46. int wid;
  47. int bpp;
  48.  
  49. void skiplf(FILE * fin)
  50. { //тючьюцэю, 0x0d єцх яЁюўшЄрэ, ЄхяхЁ№ яЁюяєёърхь 0x0a
  51. char c;
  52.   do{
  53.     if (!fread(&c,1,1,fin)) break;
  54.   }while (c!=0x0a);
  55. }
  56.  
  57. void readcomment(FILE * fin, char * s)
  58. { //тючьюцэю, 0x0d єцх яЁюўшЄрэ, ЄхяхЁ№ яЁюяєёърхь 0x0a
  59. char c;
  60. unsigned int i;
  61.   i = 0;
  62.   do{
  63.     if (!fread(&c,1,1,fin)) break;
  64.     if (i==_STRMAX) break;
  65.     s[i] = c;
  66.     i++;
  67.   }while (c!=0x0a);
  68.   s[i] = '\0';
  69. }
  70.  
  71. int readnum(FILE * fin)
  72. {
  73. char c;
  74. int num;
  75. int sign=1;
  76.   num = 0;
  77.   do{
  78.     if (!fread(&c,1,1,fin)) break;
  79.     if (c==' ') goto skip;
  80.     //if (c == 0x0d) goto skip;
  81.     //if (c == 0x0a) break;
  82.     if (c=='-') {sign = -1; goto skip;};
  83.     if ((c<'0')||(c>'9')) break; //т Єюь ўшёых 0x0a
  84.     num = num*10 + (int)(c-'0');
  85.     numok = 0xff;
  86. skip:
  87.         ;
  88.   }while(1);
  89.   num = num*sign;
  90. return num;
  91. }
  92.  
  93. unsigned int readlabel(FILE * fin, char * s)
  94. {
  95. char c;
  96. unsigned int i;
  97. int iscomment;
  98.   sprcount = 0;
  99.   vertsprcount = 0;
  100.   do{
  101.     i = 0;
  102.     iscomment = 0;
  103.     do{
  104.       if (!fread(&c,1,1,fin)) break;
  105.       if (c == ';') iscomment = -1;
  106.       if (c=='$') { //фры№°х ўшёыю ёяЁрщЄют тхЁЄшъры№эю
  107.         if (iscomment) continue; //т ъюььхэЄрї ьюцэю $
  108.         while (1) {
  109.           if (!fread(&c,1,1,fin)) return 0;
  110.           if ((c < '0') || (c > '9')) break;
  111.           vertsprcount = vertsprcount*10;
  112.           vertsprcount = vertsprcount + (int)(c-'0');
  113.         };
  114.       };
  115.       if (c=='#') { //фры№°х ўшёыю ёяЁрщЄют
  116.         if (iscomment) continue; //т ъюььхэЄрї ьюцэю #
  117.         while (1) {
  118.           if (!fread(&c,1,1,fin)) return 0;
  119.           if ((c < '0') || (c > '9')) break;
  120.           sprcount = sprcount*10;
  121.           sprcount = sprcount + (int)(c-'0');
  122.         };
  123.       };
  124.       if ((c=='=')||(c==',')) {
  125.         if (iscomment) continue; //т ъюььхэЄрї ьюцэю =
  126.         break;
  127.       };
  128.       if (c == 0x0d) continue;
  129.       if (c == 0x0a) {
  130.         if (iscomment) break; //ъюььхэЄ√ ъюэўр■Єё  яю ъюэЎє ёЄЁюъш
  131.         continue;
  132.       };
  133.       if (i==_STRMAX) break;
  134.       s[i] = c;
  135.       i++;
  136.     }while(1);
  137.     s[i] = '\0';
  138.   }while(iscomment);
  139. return i;
  140. }
  141.  
  142. int read4b(FILE * fin)
  143. {
  144.   fread(sizeword, 4, 1, fin);
  145. return sizeword[0] + (sizeword[1]<<8) + (sizeword[2]<<16) + (sizeword[3]<<24);
  146. }
  147.  
  148. BYTE colstat[256];
  149.  
  150. char numbuf[256];
  151.  
  152. UINT maxsprcount;
  153. UINT cursprcount;
  154. UINT maxvertsprcount;
  155. UINT curvertsprcount;
  156.  
  157. void putlabel(char * labelbuf, FILE * fout)
  158. {
  159. UINT num;
  160. UINT i;
  161.         fputs(labelbuf, fout);
  162.         if (maxvertsprcount != 0) {
  163.           fputc('_', fout);
  164.           fputc('_', fout);
  165.           num = curvertsprcount;
  166.  
  167.           i = 0;
  168.           do {
  169.             numbuf[i] = (char)((UINT)'0' + (num - (num/10)*10));
  170.             num = num/10;
  171.             i++;
  172.           } while (num != 0);
  173.  
  174.           do {
  175.             i--;
  176.             fputc(numbuf[i], fout);
  177.           } while (i != 0);
  178.         };
  179.         if (maxsprcount != 0) {
  180.           fputc('_', fout);
  181.           num = cursprcount;
  182.  
  183.           i = 0;
  184.           do {
  185.             numbuf[i] = (char)((UINT)'0' + (num - (num/10)*10));
  186.             num = num/10;
  187.             i++;
  188.           } while (num != 0);
  189.  
  190.           do {
  191.             i--;
  192.             fputc(numbuf[i], fout);
  193.           } while (i != 0);
  194.         };
  195. return;
  196. }
  197.  
  198. void findinkpaper(int x, int y) //ьрёър 0x00 эх ёўшЄрхЄё  ЎтхЄюь
  199. {
  200. //BYTE b;
  201. int i;
  202. int j;
  203. BYTE col1;
  204. BYTE col1stat;
  205. BYTE col2;
  206. BYTE col2stat;
  207. BYTE col3;
  208. BYTE col3stat;
  209. BYTE col4;
  210. BYTE col4stat; //эр ёыєўрщ Єръющ ёЄрЄшёЄшъш: MASKCOLOR, RED, red, anothercolor
  211.   i = 0; do {colstat[i] = 0x00; i++;}while (i!=256);
  212.  
  213.   //col1 = pic[x][y];
  214.   j = y;
  215.   while (j < (y+8)) {
  216. //    b = 0x00;
  217.     i = x;
  218.     while (i < (x+8)) {
  219.       colstat[pic[i][j]]++;
  220.       //b = pic[i][j];
  221.       //if (b!=col1) col2 = b;
  222.       i++;
  223.     };
  224.     j++;
  225.   };
  226.  
  227. //ьрёър MASKCOLOR эх ёўшЄрхЄё  ЎтхЄюь
  228.   col1 = MASKCOLOR; col1stat = 0; //fix 27.12.2018
  229.   col2 = MASKCOLOR; col2stat = 0; //fix 27.12.2018
  230.   col3 = MASKCOLOR; col3stat = 0; //fix 27.12.2018
  231.   col4 = MASKCOLOR; col3stat = 0; //fix 27.12.2018
  232.   i = 0; do { //fix 27.12.2018 (эр ёыєўрщ MASKCOLOR!=0)
  233.     if (colstat[i] > col1stat) {
  234.       col4 = col3;
  235.       col4stat = col3stat;
  236.       col3 = col2;
  237.       col3stat = col2stat;
  238.       col2 = col1;
  239.       col2stat = col1stat;
  240.       col1 = (BYTE)i;
  241.       col1stat = colstat[i];
  242.     }else if (colstat[i] > col2stat) {
  243.       col4 = col3;
  244.       col4stat = col3stat;
  245.       col3 = col2;
  246.       col3stat = col2stat;
  247.       col2 = (BYTE)i;
  248.       col2stat = colstat[i];
  249.     }else if (colstat[i] > col3stat) {
  250.       col4 = col3;
  251.       col4stat = col3stat;
  252.       col3 = (BYTE)i;
  253.       col3stat = colstat[i];
  254.     }else if (colstat[i] > col4stat) {
  255.       col4 = (BYTE)i;
  256.       col4stat = colstat[i];
  257.     };
  258.     i++;
  259.   }while (i!=16); //fix 27.12.2018
  260.  
  261. //MASKCOLOR эхы№ч  ёрь√ь ўрёЄ√ь, ёюЁЄшЁєхь хую т ъюэхЎ
  262.   if (col1==MASKCOLOR) {
  263.     col1 = col2;
  264.     col2 = MASKCOLOR;
  265.   };
  266.   if (col2==MASKCOLOR) {
  267.     col2 = col3;
  268.     col3 = MASKCOLOR;
  269.   };
  270.   if (col3==MASKCOLOR) {
  271.     col3 = col4;
  272.     col4 = MASKCOLOR;
  273.   };
  274.  
  275. //col1 ьюцхЄ с√Є№ MASKCOLOR Єюы№ъю фы  чэръюьхёЄр, яюыэюёЄ№■ чрышЄюую MASKCOLOR
  276. //ьрёър 0x00 эх ёўшЄрхЄё  ЎтхЄюь, яю¤Єюьє 0x08 яхЁхтюфшЄё  т 0x00 ё фхЇюыЄэющ  ЁъюёЄ№■ (ўЄюс√ эх тыш Є№ эр bright)
  277.   //if ((col1&0x07)==0x00) col1 = (BYTE)(defaultcolor&0x08); //fix 27.12.2018
  278.   //if ((col2&0x07)==0x00) col2 = (BYTE)(defaultcolor&0x08); //fix 27.12.2018
  279. /**  if (col2 == MASKCOLOR) {
  280.     if (col1 == MASKCOLOR) {
  281.       col2 = (BYTE)((col1&0x08) | (defaultcolor&0x07));
  282.     }else {
  283.       col2 = (BYTE)((col1&0x08) | (defaultcolor&0x07));
  284.     };
  285.   };*/ //fix 27.12.2018
  286.   if (((col2&0x07)==(col1&0x07)) /**&& (col2!=0x00)*/) col2 = col3; //same colour with another bright
  287.   ink = col1;
  288.   paper = col2; //Ёхцх
  289.   //paper Ёхцх ш ьюцхЄ с√Є№ MASKCOLOR (эхЄ тЄюЁюую ЎтхЄр, эрфю сЁрЄ№ фхЇюыЄэ√щ)
  290.   //ink ьюцхЄ с√Є№ MASKCOLOR Єюы№ъю фы  чэръюьхёЄр, яюыэюёЄ№■ чрышЄюую MASKCOLOR
  291. }
  292.  
  293. void setcurinkpaper(BYTE* pcurink, BYTE* pcurpaper)
  294. //paper Ёхцх ш ьюцхЄ с√Є№ MASKCOLOR (эхЄ тЄюЁюую ЎтхЄр, эрфю сЁрЄ№ фхЇюыЄэ√щ)
  295. //ink ьюцхЄ с√Є№ MASKCOLOR Єюы№ъю фы  чэръюьхёЄр, яюыэюёЄ№■ чрышЄюую MASKCOLOR
  296. //*pcurink, *curpaper шчэрўры№эю ёюфхЁцрЄ рЄЁшсєЄ√ яЁхф√фє∙хую чэръюьхёЄр
  297. //тючтЁр∙рхЄ *pcurink, *curpaper, яЁшў╕ь тьхёЄю 0x08 ёЄртшЄ 0x00 (ўЄюс√ эх тыш ыю эр  ЁъюёЄ№)
  298. {
  299. BYTE t;
  300.   if ((sprformat == 's')||(sprformat == 'W')||(sprformat == 'w')) {
  301.     paper = 0x08;
  302.     ink = 0x0f;
  303.   }; //фы  ёяЁрщЄют Їюэ ў╕Ёэ√щ (р ьрёър 0x00)
  304.  
  305. //27.02.2019:
  306. //Їюэ фы  чрышЄ√ї чэръюьхёЄ эрўшэр  ё чхы╕эюую ЄхяхЁ№ ў╕Ёэ√щ[шыш эрфю яЁхф√фє∙шщ?]
  307. //Їюэ фы  ў╕Ёэ√ї чэръюьхёЄ схЁ╕Є  ЁъюёЄ№ юЄ defaultcolor, р фы  чрышЄ√ї чэръюьхёЄ ьхэхх чхы╕эюую эх схЁ╕Є
  308. #define MINCOLORWITHBLACKBG 0x04
  309. //fix 27.12.2018:
  310.   if (((ink&0x07)==(*pcurink&0x07)) && (ink!=MASKCOLOR)) { //ink ёююЄтхЄёЄтєхЄ яЁхф√фє∙хьє
  311.     *pcurink = ink;
  312.     if (paper==MASKCOLOR) {
  313.       *pcurpaper = ((ink&0x07)<MINCOLORWITHBLACKBG) ? ((ink&0x07)?((defaultcolor&0x07)|(ink&0x08)):defaultcolor) : 0x00;
  314.     }else {
  315.       *pcurpaper = paper;
  316.     };
  317.   }else if (((paper&0x07)==(*pcurpaper&0x07)) && (paper!=MASKCOLOR)) { //paper ёююЄтхЄёЄтєхЄ яЁхф√фє∙хьє
  318.     *pcurpaper = paper;
  319.     if (ink==MASKCOLOR) {
  320.       *pcurink = ((paper&0x07)<MINCOLORWITHBLACKBG) ? ((paper&0x07)?((defaultcolor&0x07)|(paper&0x08)):defaultcolor) : 0x00;
  321.     }else {
  322.       *pcurink = ink;
  323.     };
  324.   }else if (((paper&0x07)==(*pcurink&0x07)) && (paper!=MASKCOLOR)) { //paper ёююЄтхЄёЄтєхЄ яЁхф√фє∙хьє ink
  325.     *pcurink = paper;
  326.     if (ink==MASKCOLOR) {
  327.       *pcurpaper = ((paper&0x07)<MINCOLORWITHBLACKBG) ? ((paper&0x07)?((defaultcolor&0x07)|(paper&0x08)):defaultcolor) : 0x00;
  328.     }else {
  329.       *pcurpaper = ink;
  330.     };
  331.   }else if (((ink&0x07)==(*pcurpaper&0x07)) && (ink!=MASKCOLOR)) { //ink ёююЄтхЄёЄтєхЄ яЁхф√фє∙хьє paper
  332.     *pcurpaper = ink;
  333.     if (paper==MASKCOLOR) {
  334.       *pcurink = ((ink&0x07)<MINCOLORWITHBLACKBG) ? ((ink&0x07)?((defaultcolor&0x07)|(ink&0x08)):defaultcolor) : 0x00;
  335.     }else {
  336.       *pcurink = paper;
  337.     };
  338.   }else if ((ink==MASKCOLOR)&&(paper==MASKCOLOR)) { //юср ЎтхЄр MASKCOLOR
  339.     ink = defaultcolor;
  340.     paper = 0x08;
  341.   }else { //юср ЎтхЄр эх ёююЄтхЄёЄтє■Є яЁхф√фє∙хьє чэръюьхёЄє, эю эх юср MASKCOLOR
  342.     if (ink == MASKCOLOR) ink = ((paper&0x07)<MINCOLORWITHBLACKBG) ? ((paper&0x07)?((defaultcolor&0x07)|(paper&0x08)):defaultcolor) : 0x00;
  343.     if (paper == MASKCOLOR) paper = ((ink&0x07)<MINCOLORWITHBLACKBG) ? ((ink&0x07)?((defaultcolor&0x07)|(ink&0x08)):defaultcolor) : 0x00;
  344.     if (ink > paper) {
  345.       *pcurink = ink;
  346.       *pcurpaper = paper;
  347.     }else {
  348.       *pcurink = paper;
  349.       *pcurpaper = ink;
  350.     };
  351.   };
  352.  
  353.     //*pcurink = ink;
  354.     //*pcurpaper = paper;
  355.  
  356.   if (*pcurpaper==0x08) *pcurpaper = 0x00; //ўЄюс√ эх тыш ыю эр  ЁъюёЄ№ //fix 27.12.2018
  357.   if (*pcurink==0x08) *pcurink = 0x00; //ўЄюс√ эх тыш ыю эр  ЁъюёЄ№ //fix 27.12.2018
  358.   //эр ў╕Ёэюь/ёшэхь Їюэх ink фюыцхэ с√Є№  Ёўх paper'р (юЄфхы№э√щ ёыєўрщ, Є.ъ. юёЄры№э√х, фрцх яєёЄ√х, чэръюьхёЄр ьюуєЄ єўрёЄтютрЄ№ т юЄЁшёютъх ёъЁюыышЁєхь√ї Їюэют ё эхяЁхЁ√тэ√ьш Ўхяюўърьш ink-paper)
  359.   //Єю хёЄ№ эхы№ч  ink=0, р 1 Єюы№ъю т ёыєўрх paper=0
  360.   if (((*pcurink&0x07)<=0x01) && ((*pcurpaper&0x07)>(*pcurink&0x07))) { //ў╕Ёэюх/ёшэхх яєёЄюх чэръюьхёЄю (юЄфхы№э√щ ёыєўрщ, Є.ъ. юёЄры№э√х яєёЄ√х ьюуєЄ єўрёЄтютрЄ№ т юЄЁшёютъх ёъЁюыышЁєхь√ї Їюэют ё эхяЁхЁ√тэ√ьш Ўхяюўърьш ink-paper)
  361.     t = *pcurpaper;
  362.     *pcurpaper = *pcurink;
  363.     *pcurink = t;
  364.   };
  365. }
  366.  
  367. void emitdb(BYTE b, FILE * fout)
  368. {
  369.   fputs("\tdb ", fout);
  370.   fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  371.   fputs("\n", fout);
  372. }
  373.  
  374. void emitdw(UINT u, FILE * fout)
  375. {
  376.   fputs("\tdw ", fout);
  377.   fprintf(fout, "0x%x%x%x%x", (u>>12)&0x0f, (u>>8)&0x0f, (u>>4)&0x0f, u&0x0f);
  378.   fputs("\n", fout);
  379. }
  380.  
  381. void emitnops(BYTE count, FILE * fout)
  382. {
  383.   fputs("\tds ", fout);
  384.   fprintf(fout, "0x%x%x", count>>4, count&0x0f);
  385.   fputs("\n", fout);
  386. }
  387.  
  388. void emitspr(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
  389. {
  390. BYTE b;
  391. int i;
  392. int j;
  393.   j = y;
  394.   while (1) {
  395.     fputs("\tdb ", fout);
  396.     i = xchr;
  397.     while (1) {
  398.       b = maskrow[i][j];
  399.       fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  400.       fputs(",", fout);
  401.       b = b^pixrow[i][j];
  402.       fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  403.       i++;
  404.       if (i >= (xchr+sprwid8)) break;
  405.       fputs(",", fout);
  406.     };
  407.     fputs("\n", fout);
  408.     j++;
  409.     if (j >= (y+sprhgt)) break;
  410.   };
  411. }
  412.  
  413. void emitsprw(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
  414. { //antipixelsline, antimaskline
  415. BYTE b;
  416. int i;
  417. int j;
  418.   j = y;
  419.   while (1) {
  420.     fputs("\tdb ", fout);
  421.     i = xchr;
  422.     while (1) {
  423.       b = ~maskrow[i][j];
  424.       fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  425.       i++;
  426.       if (i == xchr+sprwid8) break;
  427.       fputs(",", fout);
  428.     };
  429.     fputs("\n", fout);
  430.     fputs("\tdb ", fout);
  431.     i = xchr;
  432.     while (1) {
  433.       b = ~maskrow[i][j];
  434.       b ^= pixrow[i][j];
  435.       fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  436.       i++;
  437.       if (i == xchr+sprwid8) break;
  438.       fputs(",", fout);
  439.     };
  440.     fputs("\n", fout);
  441.     j++;
  442.     if (j >= (y+sprhgt)) break;
  443.   };
  444. }
  445.  
  446. void emitsprwback(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
  447. { //antipixelsw, antimaskw
  448. BYTE b;
  449. int i;
  450. int j;
  451.   j = y;
  452.   while (1) {
  453.     fputs("\tdb ", fout);
  454.     i = xchr+sprwid8;
  455.     while (1) {
  456.       i--;
  457.       b = ~maskrow[i][j];
  458.       fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  459.       if (i == xchr) break;
  460.       fputs(",", fout);
  461.     };
  462.     fputs("\n", fout);
  463.     fputs("\tdb ", fout);
  464.     i = xchr+sprwid8;
  465.     while (1) {
  466.       i--;
  467.       b = ~maskrow[i][j];
  468.       b ^= pixrow[i][j];
  469.       fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  470.       if (i == xchr) break;
  471.       fputs(",", fout);
  472.     };
  473.     fputs("\n", fout);
  474.     j++;
  475.     if (j >= (y+sprhgt)) break;
  476.   };
  477. }
  478.  
  479. void emitsprwnomask(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
  480. { //antipixelsw
  481. BYTE b;
  482. int i;
  483. int j;
  484.   j = y;
  485.   while (1) {
  486.     fputs("\tdb ", fout);
  487.     i = xchr;
  488.     while (1) {
  489.       b = ~maskrow[i][j];
  490.       fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  491.       i++;
  492.       if (i == xchr+sprwid8) break;
  493.       fputs(",", fout);
  494.     };
  495.     fputs("\n", fout);
  496.     j++;
  497.     if (j >= (y+sprhgt)) break;
  498.   };
  499. }
  500.  
  501. void emitsprwnomaskback(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
  502. { //antipixelsw
  503. BYTE b;
  504. int i;
  505. int j;
  506.   j = y;
  507.   while (1) {
  508.     fputs("\tdb ", fout);
  509.     i = xchr+sprwid8;
  510.     while (1) {
  511.       i--;
  512.       b = ~maskrow[i][j];
  513.       fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  514.       if (i == xchr) break;
  515.       fputs(",", fout);
  516.     };
  517.     fputs("\n", fout);
  518.     j++;
  519.     if (j >= (y+sprhgt)) break;
  520.   };
  521. }
  522.  
  523. void emitimgW(int xchr, int y, int sprwid8, int sprhgt, FILE * fout)
  524. { //by columns
  525. BYTE b;
  526. int i;
  527. int j;
  528.   i = xchr;
  529.   while (1) {
  530.     fputs("\tdb ", fout);
  531.     j = y;
  532.     while (1) {
  533.       b = maskrow[i][j];
  534.                   //fprintf(fout, "0x%x%x ", i>>4, i&0x0f);
  535.                   //fprintf(fout, "0x%x%x ", j>>4, j&0x0f);
  536.       fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  537.       j++;
  538.       if (j >= (y+sprhgt)) break;
  539.       fputs(",", fout);
  540.     };
  541.     fputs("\n", fout);
  542.     i++;
  543.     if (i == (xchr+sprwid8)) break;
  544.   };
  545. }
  546.  
  547. void emitchr_(int xchr, int y, FILE * fout)
  548. {
  549. BYTE b;
  550. int j;
  551.   fputs("\tdb ", fout);
  552.   j = y;
  553.   while (1) {
  554.     b = pixrow[xchr][j];
  555.     fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  556.     j++;
  557.     if (j >= (y+8)) break;
  558.     fputs(",", fout);
  559.   };
  560.   if (sprformat < 'a') { //capital letter format => attr used
  561.     b = attrrow[xchr]; //0x07;
  562.     fputs(",", fout);
  563.     fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  564.   };
  565.   fputs("\n", fout);
  566. }
  567.  
  568. void emitchr(int xchr, int y, FILE * fout)
  569. {
  570.   emitchr_(xchr, y, fout);
  571. }
  572.  
  573. void emitchrshift(int xchr, int y, FILE * fout)
  574. {
  575.   emitchr_(xchr, y+PIXROWSHIFT, fout);
  576. }
  577.  
  578. //ёфтшурхь Ё ф чэръюьхёЄ >>shiftbits, Ёхчєы№ЄрЄ т pixrow[sprx][y+PIXROWSHIFT]
  579. void shiftrow(int sprx, int y, int sprwid, int rowhgt, int pixrowshift, BYTE shiftbits)
  580. {
  581. int j;
  582. int x;
  583. BYTE b;
  584. BYTE b0;
  585. BYTE shiftmask;
  586.   shiftmask = (BYTE)(0xff>>(0x08-shiftbits)); //хёыш shiftbits==0x01, Єю shiftmask==0x01
  587.   j = y;
  588.   while (j < (y+rowhgt)) {
  589.     b = 0x00;
  590.     x = sprx;
  591.     while (x < (sprx+sprwid+8)) {
  592.       b0 = pixrow[x/8][j];
  593.       pixrow[x/8][j+pixrowshift] = (BYTE)((b<<(0x08-shiftbits)) + (b0>>shiftbits));
  594.       b = (BYTE)(b0&shiftmask/**0x0f*/); //хёыш shiftbits==0x01, Єю shiftmask==0x01
  595.       x = x+8;
  596.     };
  597.     j++;
  598.   };
  599. }
  600.  
  601. //data: wid8, hgt8, chrgfx, chrgfx...
  602. void resfile(char * finname, char * fintxtname, char * foutname)
  603. {
  604. FILE* fin;
  605. FILE* fintxt;
  606. FILE* fout;
  607. int i;
  608. int j;
  609. int size;
  610. int y;
  611. int x;
  612. int n;
  613. int tiles;
  614.  
  615. BYTE b;
  616. BYTE bmask;
  617. BYTE b0;
  618.  
  619. int sprx;
  620. int startsprx;
  621. int spry;
  622. int sprwid;
  623. int sprhgt;
  624. int rowhgt; //8 for tiles, sprhgt for sprites
  625.  
  626. UINT color;
  627.  
  628.   fin = fopen(finname, "rb");
  629.   if (fin) {
  630.     fread(filebuf, 10, 1, fin); //skip to 10 (header size)
  631.     size = read4b(fin); //10 (header size)
  632.     fread(filebuf, 4, 1, fin); //skip to 18
  633.     wid = read4b(fin); //18
  634.     hgt = read4b(fin); //22
  635.     fread(filebuf, 2, 1, fin); //skip to 28
  636.     fread(&bpp, 1, 1, fin); //28
  637.     fread(filebuf, 1, 54-29, fin); //skip to pal
  638.     fread(pal, 1, 64, fin); //ярышЄЁр (B, G, R, 0)
  639.     if (size > (54+64)) {fread(filebuf, 1, size-(54+64), fin);}; //skip to pic
  640.     if ((wid>0)&&(wid<=1024)&&(hgt>0)&&(hgt<=1024)&&((wid&7)==0)&&((hgt&7)==0)) {
  641.       y = hgt;
  642.       while (y>0) {
  643.         y--;
  644.         x = 0;
  645.         while (x<wid) {
  646.           fread(&b, 1, 1, fin);
  647.           if (bpp == 8) {
  648.             pic[x][y] = b;
  649.             x++;
  650.           }else {
  651.             pic[x][y] = (BYTE)((b&0xf0)>>4);
  652.             x++;
  653.             pic[x][y] = (BYTE)(b&0x0f);
  654.             x++;
  655.           };
  656.         };
  657.       };
  658.  
  659.       fintxt = fopen(fintxtname, "rb");
  660.       if (fintxt) {
  661.         fout = fopen(foutname, "wb");
  662.         if (fout) {
  663. /**          if (labelname[0]!='\0') {
  664.             fputs(labelname, fout);
  665.             fputs("\n", fout);
  666.           };*/
  667.           while (1) {
  668.             size = readlabel(fintxt, labelbuf); //fread(filebuf, 1, MAXDEFB, fin);
  669.            maxvertsprcount = vertsprcount;
  670.            maxsprcount = sprcount;
  671.             if (size == 0) break;
  672.             readlabel(fintxt, formatlabelbuf); //format
  673.             sprformat = *formatlabelbuf;
  674.             startsprx = readnum(fintxt);
  675.             spry = readnum(fintxt);
  676.             sprwid = readnum(fintxt);
  677.             sprhgt = readnum(fintxt);
  678.             numok = 0x00;
  679.             tiles = readnum(fintxt); //юЄёєЄёЄтєхЄ т x
  680.             defaultcolor = (BYTE)tiles; //фы  тёхї, ъЁюьх L
  681.             *commentlabelbuf = '\0';
  682.             if ((numok != 0x00) && (sprformat != 'L')) readcomment(fintxt, commentlabelbuf);
  683.            curvertsprcount = 0;
  684.            do { //vertsprcount
  685.            cursprcount = 0;
  686.            sprx = startsprx;
  687.            do { //sprcount
  688.             if ((sprformat == 'B')||(sprformat == 'b')) {
  689.               putlabel(labelbuf, fout);
  690.               fputs("\n", fout);
  691.               //emitdb((BYTE)(sprwid>>3), fout);
  692.               //emitdb((BYTE)(sprhgt>>3), fout);
  693.               rowhgt = 8;
  694.             }else if (sprformat == 'T') { //эрсюЁ Єрщыют
  695.               fputs("\tds (-$)&0xff\n", fout);
  696.               putlabel(labelbuf, fout);
  697.               fputs("\n", fout);
  698.               rowhgt = 8;
  699.             }else if (sprformat == 'x') { //ёяЁрщЄ 16c
  700.               fputs("\n", fout);
  701.               putlabel(labelbuf, fout);
  702.               fputs("=$+4\n", fout);
  703.               fputs("\n", fout);
  704.               emitdb((BYTE)(sprwid>>1), fout);
  705.               emitdb((BYTE)(sprhgt), fout);
  706.               rowhgt = sprhgt;
  707.             }else if (sprformat == 'i') { //ърЁЄшэър 16c яю ёЄюысЎрь
  708.               fputs("\n", fout);
  709.               putlabel(labelbuf, fout);
  710.               fputs("\n", fout);
  711.               rowhgt = sprhgt;
  712.             }else if (sprformat == 'L') { //LAND ъръ т ╫┬, фры№°х ёыхфєхЄ ЄрсышЎр - эюьхЁ Єрщыр фы  ърцфющ ъыхЄъш
  713.               fputs("\n", fout);
  714.               putlabel(labelbuf, fout);
  715.               fputs("\n", fout);
  716.               rowhgt = sprhgt;
  717.             }else if (sprformat == 'P') { //DDp palette
  718.               fputs("\n", fout);
  719.               putlabel(labelbuf, fout);
  720.               fputs("\n", fout);
  721.               i = 0;
  722.               while (i < 64) { //DDp palette: %grbG11RB(low),%grbG11RB(high), шэтхЁёэ√х //color = highlow
  723.                 color = 0; //pal = ярышЄЁр (B, G, R, 0)
  724.                 if (pal[i]&0x80) color = color | 0x0100;
  725.                 if (pal[i]&0x40) color = color | 0x2000;
  726.                 if (pal[i]&0x20) color = color | 0x0001;
  727.                 if (pal[i]&0x10) color = color | 0x0020;
  728.                 i++;
  729.                 if (pal[i]&0x80) color = color | 0x1000;
  730.                 if (pal[i]&0x40) color = color | 0x8000;
  731.                 if (pal[i]&0x20) color = color | 0x0010;
  732.                 if (pal[i]&0x10) color = color | 0x0080;
  733.                 i++;
  734.                 if (pal[i]&0x80) color = color | 0x0200;
  735.                 if (pal[i]&0x40) color = color | 0x4000;
  736.                 if (pal[i]&0x20) color = color | 0x0002;
  737.                 if (pal[i]&0x10) color = color | 0x0040;
  738.                 i++;
  739.                 emitdw(~color, fout);
  740.                 i++;
  741.               };
  742.               fputs("\n", fout);
  743.               rowhgt = sprhgt;
  744.             }else if ((sprformat == 'w')||(sprformat == 'W')||(sprformat == 'z')||(sprformat == 'Z')) {
  745.               putlabel(labelbuf, fout);
  746.               fputs("\n", fout);
  747.               rowhgt = sprhgt;
  748.             }else if (sprformat == 'c') {
  749.               putlabel(labelbuf, fout);
  750.               fputs("\n", fout);
  751.               rowhgt = sprhgt;
  752.             }else { //'s'
  753.               putlabel(labelbuf, fout);
  754.               fputs("\n", fout);
  755.               emitdb((BYTE)(sprwid>>3), fout);
  756.               emitdb((BYTE)(sprhgt), fout);
  757.               rowhgt = sprhgt;
  758.             };
  759.  
  760. //copy comment as code line
  761.               fputs(commentlabelbuf, fout);
  762.               //fputs("\n", fout);
  763.  
  764.             y = spry;
  765.             while (y < (spry+sprhgt)) {
  766.               //яхЁхъюфшЁєхь Ё ф чэръюьхёЄ т√ёюЄющ rowhgt
  767.               curink = 0x0f;
  768.               curpaper = 0x08;
  769.               x = 0;//sprx;
  770.               while (x < (sprx+sprwid)) {
  771.                 findinkpaper(x, y);
  772.                 setcurinkpaper(&curink, &curpaper);
  773.               //curink = 0x0f;
  774.               //curpaper = 0x08;
  775. //  if (sprformat == 'c') {
  776. //    curpaper = 0;
  777. //    curink = 1;
  778. //  };
  779.                 j = y;
  780.                 while (j < (y+rowhgt)) {
  781.                   b = 0x00;
  782.                   bmask = 0x00;
  783.                   i = x;
  784.                   while (i < (x+8)) {
  785.                     b = (BYTE)(b<<1);
  786.                     bmask = (BYTE)(bmask<<1);
  787.                     //if (sprformat != 'B') {fprintf(fout, "0x%x%x\n", (pic[i][j])>>4, (pic[i][j])&0x0f);};
  788.                     if (pic[i][j]==curink) b++;
  789.                     if (pic[i][j]!=0x00) bmask++;
  790.                     i++;
  791.                   };
  792.                   pixrow[x/8][j] = b;
  793.                   maskrow[x/8][j] = bmask;
  794.                   //fprintf(fout, "0x%x%x ", x>>4, x&0x0f);
  795.                   //fprintf(fout, "0x%x%x ", j>>4, j&0x0f);
  796.                   //fprintf(fout, "0x%x%x ", b>>4, b&0x0f);
  797.                   //fprintf(fout, "0x%x%x\n", bmask>>4, bmask&0x0f);
  798.                   pixrow[(x/8)+1][j] = 0x00; //ўЄюс√ ёфтшурЄ№
  799.                   j++;
  800.                 };
  801. //ьрёър 0x00 эх ёўшЄрхЄё  ЎтхЄюь, яю¤Єюьє 0x08 яхЁхтюфшЄё  т 0x00 (ўЄюс√ эх тыш Є№ эр bright)
  802.                 b = 0x00; if (curink!=0x08) b = curink;
  803.                 b0 = 0x00; if (curpaper!=0x08) b0 = curpaper;
  804.                 attrrow[x/8    ] = (BYTE)( (((b|b0)&0x08)<<3)+((curpaper&0x07)<<3)+(curink&0x07) );
  805.                 attrrow[(x/8)+1] = (BYTE)( (((b|b0)&0x08)<<3)+((curpaper&0x07)<<3)+(curink&0x07) ); //ўЄюс√ ёфтшурЄ№
  806.                 x = x+8;
  807.               };
  808.   //            shiftrow(sprx, y, sprwid, rowhgt, PIXROWSHIFT, 0x04); //ёфтшурхь Ё ф чэръюьхёЄ >>4, Ёхчєы№ЄрЄ т pixrow[sprx][y+PIXROWSHIFT]
  809.  
  810.               //т√тюфшь т рёь
  811.               if ((sprformat == 'B')||(sprformat == 'b')) { //tiles or bw tiles
  812.                 x = sprx;
  813.                 while (x < (sprx+sprwid)) {
  814.   //                emitchrshift(x/8,y,fout);
  815.                   emitchr(x/8,y,fout); //checks for capital letter in sprformat
  816.                   x = x+8;
  817.                 };
  818.   //              emitchrshift(x/8,y,fout);
  819.               }else if (sprformat == 'T') {
  820.                 x = sprx;
  821.                 while (x < (sprx+sprwid)) {
  822.                   emitchr(x/8,y,fout);
  823.                   x = x+8;
  824.                 };
  825.                 emitnops((BYTE)(0x100-((BYTE)(sprwid>>3)*0x09)),fout);
  826.               }else if (sprformat == 's') { //sprite
  827.                 emitspr(sprx/8,y,sprwid/8,sprhgt,fout);
  828.               }else if (sprformat == 'w') { //sprite antipixels16, antimask16 right to left
  829.                 emitsprwback(sprx/8,y,sprwid/8,sprhgt,fout);
  830.               }else if (sprformat == 'W') { //sprite antipixels16, antimask16
  831.                 emitsprw(sprx/8,y,sprwid/8,sprhgt,fout);
  832.               }else if (sprformat == 'z') { //unmasked sprite right to left
  833.                 emitsprwnomaskback(sprx/8,y,sprwid/8,sprhgt,fout);
  834.               }else if (sprformat == 'Z') { //unmasked sprite
  835.                 emitsprwnomask(sprx/8,y,sprwid/8,sprhgt,fout);
  836.               }else if (sprformat == 'c') { //b/w image by columns
  837.                 emitimgW(sprx/8,y,sprwid/8,sprhgt,fout);
  838.                 //emitimgW(0,0,128,128,fout);
  839.               };
  840.               y = y+rowhgt;
  841.             }; //while y
  842.  
  843.             if (sprformat == 'x') {
  844.               x = sprx;
  845.               while (x < (sprx+sprwid)) {
  846.                 y = spry;
  847.                 while (y < (spry+sprhgt)) {
  848.                   b = pic[x][y]; //L
  849.                   b0 = pic[x+1][y]; //R
  850.                   bmask = 0; //0x47(L) ш 0xb8(R) т Єхї ьхёЄрї, уфх ЎтхЄ=16:
  851.                   if (b == 16) {bmask = bmask + 0x47; b = 0x00;};
  852.                   if (b0 == 16) {bmask = bmask + 0xb8; b0 = 0x00;};
  853.                   b = ((b&0x08)<<3) + (b&0x07) + ((b0&0x08)<<4) + ((b0&0x07)<<3);
  854.                   fputs("\tdb ", fout);
  855.                   fprintf(fout, "0x%x%x", bmask>>4, bmask&0x0f);
  856.                   fprintf(fout, ",0x%x%x", b>>4, b&0x0f);
  857.                   fputs("\n", fout);
  858.                   y = y+1;
  859.                 };
  860.                 x = x+2;
  861.                 if (x < (sprx+sprwid)) {
  862.                   emitdw(0x4000-((sprhgt-1)*40), fout);
  863.                 }else {
  864.                   emitdw(0xffff, fout);
  865.                 };
  866.                 fputs("\n", fout);
  867.               };
  868.               fputs("\tdw prsprqwid\n", fout);
  869.             };
  870.  
  871.             if (sprformat == 'i') {
  872.               x = sprx;
  873.               while (x < (sprx+sprwid)) {
  874.                 y = spry;
  875.                 while (y < (spry+sprhgt)) {
  876.                   b = pic[x][y]; //L
  877.                   b0 = pic[x+1][y]; //R
  878.                   b = ((b&0x08)<<3) + (b&0x07) + ((b0&0x08)<<4) + ((b0&0x07)<<3);
  879.                   fprintf(fout, "\tdb 0x%x%x", b>>4, b&0x0f);
  880.                   fputs("\n", fout);
  881.                   y = y+1;
  882.                 };
  883.                 x = x+2;
  884.                 fputs("\n", fout);
  885.               };
  886.             };
  887.  
  888.             if (sprformat == 'L') { //фрыхх ЄхъёЄ Єшяр (-1=яЁюяєёъ):
  889. //   -1, -1, -1,114,116,119,121,124,126,-1,-1,-1,-1,-1,-1,-1,
  890. //  113,118,123,115,117,120,122,125,127,-1,-1,-1,-1,-1,-1,-1
  891. //фы  ърцфющ  ўхщъш ърЁЄшэъш єърчрэ эюьхЁ Єрщыр
  892. //р эрь эрфю чряюыэшЄ№ ьрёёшт√ convorderx,y - ъююЁфшэрЄ√ фы  ърцфюую эюьхЁр Єрщыр
  893. //тёх фюыцэ√ с√Є№ т юфэющ ърЁЄшэъх, шэрўх эх яюыєўшЄё  (яхЁхьх°рэ√ эюьхЁр Єрщыют юс∙шх фы  тёхї ыюърЎшщ ш фы  ъюэъЁхЄэющ)
  894.               n = 0;
  895.               while (n < CONVORDERSZ) {
  896.                 convorderx[n] = 0;
  897.                 convordery[n] = 0;
  898.                 n = n+1;
  899.               };
  900.  
  901.                 skiplf(fintxt);
  902.               //tiles = 0;
  903.  
  904.               y = spry;
  905.               while (y < (spry+sprhgt)) {
  906.                 x = sprx;
  907.                 while (x < (sprx+sprwid)) {
  908.                   n = readnum(fintxt);
  909.                   if (n != -1) {
  910.                     convorderx[n] = x;
  911.                     convordery[n] = y;
  912.                   };
  913.                   //fprintf(fout, "\tdb %d\n", n);
  914.                   //tiles = tiles + 1;
  915.                   x = x+16;
  916.                 };
  917.                 skiplf(fintxt);
  918.                 //fputs("\n", fout);
  919.                 y = y+16;
  920.               };
  921.  
  922.               n = 0;
  923.               while (n < tiles) {
  924.                 x = convorderx[n];
  925.                 while (x < (convorderx[n]+16)) {
  926.                   fputs(" db ", fout);
  927.                   y = convordery[n];
  928.                   while (1) {
  929.                     b = pic[x][y]; //L
  930.                     b0 = pic[x+1][y]; //R
  931.                     b = ((b&0x08)<<3) + (b&0x07) + ((b0&0x08)<<4) + ((b0&0x07)<<3);
  932.                     fprintf(fout, "0x%x%x", b>>4, b&0x0f);
  933.                     y = y+1;
  934.                     if (y == (convordery[n]+16)) break;
  935.                     fputs(",", fout);
  936.                   };
  937.                   fputs("\n", fout);
  938.                   x = x+2;
  939.                 };
  940.                 n = n+1;
  941.               };
  942.  
  943.             };
  944.             sprx = sprx + sprwid;
  945.             cursprcount++;
  946.            } while (cursprcount<maxsprcount); //while (sprcount)
  947.             spry = spry + sprhgt;
  948.             curvertsprcount++;
  949.            } while (curvertsprcount<maxvertsprcount); //while (vertsprcount)
  950.           }; //while (1)
  951.           fclose(fout);
  952.         }else {printf("can't open %s",foutname);};
  953.         fclose(fintxt);
  954.       }else {printf("can't open %s",fintxtname);};
  955.     };
  956.     fclose(fin);
  957.   }else {printf("can't open %s",finname);};
  958. }
  959.  
  960. int main(int argc,char* argv[])
  961. {
  962. //  int i;
  963.   char *finname;
  964.   char *fintxtname;
  965.   char *foutname;
  966.   finname = "testpic.bmp";
  967.   fintxtname = "testpic.txt";
  968.   foutname = "testpic.asm";
  969.  
  970.   if (argc<4) {
  971.     printf(
  972.       "NedoRes\n"
  973.       "\tnedores.exe file.bmp file.dat(=txt) file.ast(=asm)\n"
  974.       "4bpp or 8bpp\n"
  975.     );
  976.   }else {
  977.     finname = argv[1];
  978.     fintxtname = argv[2];
  979.     foutname = argv[3];
  980.   };
  981.  
  982.   resfile(finname, fintxtname, foutname);
  983.  
  984.   return 0;
  985. }