home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / programs / amiga / pastex / archives / bm2font.lha / bm2font / bm2font.c.orig < prev    next >
Text File  |  1992-09-29  |  103KB  |  4,634 lines

  1. /* Output from p2c, the Pascal-to-C translator */
  2. /* From input file "bm2font.p" */
  3.  
  4.  
  5. /*1:*/
  6.  
  7. #include "p2c.h"
  8. /* p2c: bm2font.p, line 1: Note:
  9.  * Unexpected name "texfile" in program header [262] */
  10. /* p2c: bm2font.p, line 1: Note:
  11.  * Unexpected name "tfmfile" in program header [262] */
  12. /* p2c: bm2font.p, line 1: Note:
  13.  * Unexpected name "pxlfile" in program header [262] */
  14. /* p2c: bm2font.p, line 1: Note:
  15.  * Unexpected name "bitmap" in program header [262] */
  16. /* p2c: bm2font.p, line 1: Note:
  17.  * Unexpected name "tmpfil" in program header [262] */
  18.  
  19.  
  20. /*logfile,*/
  21.  
  22.  
  23. typedef uchar ebts;
  24.  
  25. typedef short halfk;
  26.  
  27. typedef long dynlong[26][129];
  28. typedef unsigned short dynword[26][129];
  29. typedef short korbyte[511];
  30. typedef short spreadbyte[256];
  31. /*55:*/
  32. typedef unsigned short pxlstr;
  33.  
  34. typedef ebts pxla[65522L];
  35. /*:55*/
  36. /*59:*/
  37. typedef short pxlba[32761];
  38. /*:59*/
  39. /*93:*/
  40. typedef uchar dmatrix[7][7];
  41.  
  42. /*:93*/
  43.  
  44.  
  45. #define maxmemsize      32760
  46.  
  47.  
  48. /*94:*/
  49.  
  50. Static dmatrix ditherm = {
  51.   { 1, 3, 6, 10, 18, 26, 38 },
  52.   { 2, 4, 8, 12, 20, 28, 40 },
  53.   { 5, 7, 9, 14, 22, 30, 42 },
  54.   { 11, 13, 15, 16, 24, 32, 44 },
  55.   { 17, 19, 21, 23, 25, 34, 46 },
  56.   { 27, 29, 31, 33, 35, 36, 48 },
  57.   { 37, 39, 41, 43, 45, 47, 49 }
  58. };
  59.  
  60. /*:94*/
  61. /*:1*/
  62. /*2:*/
  63. Static ebts hrep, vrep, xrep, yrep, patswitch, usecol,
  64.         pminval, pmaxval, bufsh, nextbuf, tmpcolor,
  65.         invbyte, invbit, xratio, yratio;
  66. Static unsigned short tpal[256];
  67. Static long bitspsam, rowspstrip;
  68. Static unsigned short photoinf;
  69. Static short colpos;
  70. Static ebts pxllook[256];
  71. Static ebts lutone[256];
  72. Static ebts lutvga[256];
  73. Static ebts lutsl[256];
  74. Static double spreadcol, patref, greyunit;
  75. Static short greykor;
  76. Static short *gkorh, *gkordivp;
  77. Static short *spreadvals;
  78. Static short hktab[256];
  79. Static boolean cspread;
  80. Static unsigned short pxlcolor;
  81. Static boolean greypxl, egastretch, ledprinter, distribute,
  82.            giftemp, pcxtemp;
  83. Static short maxcol;
  84. Static long usepxl, vusepxl, brightness;
  85. Static short greypix;
  86. Static ebts pattern[4][197][7];
  87. Static unsigned short tab[64][3];
  88. Static uchar ccol;
  89. Static Char cref[77];
  90. Static ebts b1[3000];
  91. Static ebts b2[3000];
  92. Static ebts b3[3000];
  93. Static ebts b4[3000];
  94. Static long linepos_, posbit;
  95. Static halfk dbuf;
  96. Static boolean zeroline, zerorow, nameok, fok;
  97. Static Char cmd[256];
  98. Static Char bmname[256];
  99. Static Char font[256];
  100. Static Char tmpname[256];
  101. Static Char fontpre[256];
  102. Static Char fontupc[256];
  103. Static ebts curpat[2];
  104. Static short gbufp, gbufpp;
  105. Static halfk pxlinlen, zeropads;
  106. Static short mapwidth, mapheight, maplines, freelines;
  107. Static long mapinlen, newwidth, newheight;
  108. Static short a, b, f, g, ly, lx, minx, maxx, i, j, v, k, pk,
  109.          pkn;
  110. Static uchar pm, pn;
  111. Static unsigned short pl, pb;
  112. Static uchar c, cb;
  113. Static long fh, sh, fw, sw;
  114. Static unsigned short lf, lh, bc, ec, nw, nh, nd, ni, nl,
  115.               nk, ne, np;
  116. Static union {
  117.   uchar b[4];
  118.   long i;
  119. } cx;
  120. Static long checksum, dsize;
  121. Static long tabdsize[26];
  122. Static long (*width)[129], (*height)[129];
  123. Static unsigned short (*yoffset)[129], (*pwidth)[129],
  124.               (*pheight)[129];
  125. Static long dirpoint[129];
  126. Static pxlstr pkpoint[129];
  127. Static long hres, vres;
  128. Static double truehres, truevres;
  129. Static long noffonts, nofchars, charpf;
  130. /*logfile,*/
  131. Static FILE *texfile;
  132. Static FILE *bitmap, *pxlfile, *tmpfil;
  133. Static short filestat;
  134. Static unsigned short gi;
  135. Static boolean invert;
  136. Static unsigned short cv, dv, lv;
  137. Static Char comment[256];
  138. Static Char aliasname[256];
  139. Static boolean aliasused;
  140. Static double mapdiv;   /*pxlmod:boolean;*/
  141. Static long pkwidth, pkheight;
  142. Static boolean nowhite;
  143. Static pxlstr nextmemfree;
  144. /*25:*/
  145. Static ebts cutrep, outputbyte;
  146. Static halfk cutlen;
  147. Static boolean cutgrey;   /*:25*/
  148. /*53:*/
  149. Static Char tfmstr[256];
  150. Static uchar tfmb;
  151. Static FILE *tfmfile;
  152. /*:53*/
  153. /*56:*/
  154. Static uchar *pxlbm;
  155. Static pxlstr pxlbmi;
  156. /*:56*/
  157. /*58:*/
  158. Static uchar *pkbm;
  159. Static short *repbm;
  160. Static pxlstr pkloc;
  161. /*:58*/
  162. /*62:*/
  163. Static short bitweight;
  164. /*:62*/
  165. /*64:*/
  166. Static ebts car;
  167. /*:64*/
  168. /*69:*/
  169. Static short cxoff, cyoff;
  170. /*:69*/
  171. /*73:*/
  172. Static short power[9];   /*:73*/
  173. /*95:*/
  174. Static double valinc, redval, grx, grxzero;
  175. Static long gradint;
  176. Static uchar *greymem;
  177. Static long pgreymem;
  178. Static boolean randomst, newrandom;
  179. Static Char texfile_NAME[_FNSIZE];
  180. Static Char bitmap_NAME[_FNSIZE];
  181. Static Char pxlfile_NAME[_FNSIZE];
  182. Static Char tmpfil_NAME[_FNSIZE];
  183. Static Char tfmfile_NAME[_FNSIZE];
  184.  
  185.  
  186. /*:95*/
  187. /*:2*/
  188. /*6:*/
  189. Static uchar korrlastb(uchar i, short j)
  190. {
  191.   ebts k;
  192.  
  193.   k = 8 - j;
  194.   if (k > 0) {
  195.     i >>= k;
  196.     i <<= k;
  197.   }
  198.   return i;
  199. }
  200.  
  201.  
  202. /*:6*/
  203. /*7:*/
  204. Static uchar pxladd(uchar i, uchar j, short k)
  205. {
  206.   i = (i << k) & 255;
  207.   j >>= k;
  208.   return (i | j);
  209. }
  210.  
  211.  
  212. /*:7*/
  213. /*8:*/
  214. Static unsigned short getcolor(void)
  215. {
  216.   halfk j, k;
  217.   unsigned short a, o;
  218.   ebts savcol, lcol, rcol, l, r;
  219.  
  220.   if (cutgrey) {
  221.     if ((cutrep & 127) == 0) {
  222.       if (cutrep == 128)
  223.     colpos++;
  224.       cutrep = b1[(halfk)colpos];
  225.       colpos++;
  226.     }
  227.     savcol = b1[(halfk)colpos];
  228.     if (cutrep < 128)
  229.       colpos++;
  230.     cutrep--;
  231.     return savcol;
  232.   }
  233.   a = colpos;
  234.   colpos += bitspsam;
  235.   o = a + bitspsam - 1;
  236.   l = a & 7;
  237.   r = (o + 1) & 7;
  238.   k = o >> 3;
  239.   j = a >> 3;
  240.   lcol = b1[j];
  241.   rcol = b1[k];
  242.   if (k != j) {
  243.     savcol = lcol << l;
  244.     savcol = (savcol >> (l - r)) ^ (rcol >> (8 - r));
  245.     return savcol;
  246.   }
  247.   savcol = lcol << l;
  248.   savcol >>= l;
  249.   if (r > 0)
  250.     savcol >>= 8 - r;
  251.   return savcol;
  252. }
  253.  
  254.  
  255. /*:8*/
  256. /*9:*/
  257. Static Char *numtostr(Char *Result, long i)
  258. {
  259.   Char nstr[256];
  260.  
  261.   sprintf(nstr, "%ld", i);
  262.   return strcpy(Result, nstr);
  263. }
  264.  
  265.  
  266. /*:9*/
  267. /*10:*/
  268. Static void korrfont(void)
  269. {
  270.   uchar i, j;
  271.  
  272.   i = strpos2(font, "/", 1);
  273.   while (i > 0) {
  274.     strdelete((void *)font, 1, i);
  275.     i = strpos2(font, "/", 1);
  276.   }
  277.   j = strlen(font);
  278.   i = strpos2(font, ".", 1);
  279.   if (i != 0)
  280.     strdelete((void *)font, i, j - i + 1);
  281.   if (strlen(font) > 7)
  282.     font[7] = '\0';
  283. }
  284.  
  285.  
  286. /*:10*/
  287. /*11:*/
  288. Static void whatistrue(void)
  289. {
  290.   hres = (long)floor(truehres + 0.5);
  291.   vres = (long)floor(truevres + 0.5);
  292.   hres /= 2;
  293.   vres /= 2;
  294.   hres -= hres & 7;
  295.   if (hres * vres <= 262136L)
  296.     return;
  297.   if (truehres > truevres) {
  298.     hres = (long)sqrt(262136.0) - 31;
  299.     vres = (long)(hres * truevres / truehres);
  300.     return;
  301.   }
  302.   if (truevres > truehres) {
  303.     vres = (long)sqrt(262136.0) - 31;
  304.     hres = (long)(vres * truehres / truevres);
  305.   } else {
  306.     hres = (long)sqrt(262136.0) - 31;
  307.     vres = hres;
  308.   }
  309. }  /*:11*/
  310.  
  311.  
  312. /*12:*/
  313. Static void outfix(long k)
  314. {
  315.   short a;
  316.   long f;
  317.   char j;
  318.   long delta;
  319.   union {
  320.     ebts b[4];
  321.     long i;
  322.   } ax;
  323.   char dig[12];
  324.  
  325.   ax.i = k;
  326. #ifdef INTEL
  327.   a = (long)ax.b[3] * 16 + ax.b[2] / 16;
  328.   f = ((ax.b[2] & 15) * 256 + ax.b[1]) * 256 + ax.b[0];
  329. #else
  330.   a = (long)ax.b[0] * 16 + ax.b[1] / 16;
  331.   f = ((ax.b[1] & 15) * 256 + ax.b[2]) * 256 + ax.b[3];
  332. #endif
  333.   if (a > 2047) {
  334.     putc('-', texfile);
  335.     a = 4096 - a;
  336.     if (f > 0) {
  337.       f = 1048576L - f;
  338.       a--;
  339.     }
  340.   }
  341.   j = 0;
  342.   do {
  343.     dig[j] = a % 10;
  344.     a /= 10;
  345.     j++;
  346.   } while (a != 0);
  347.   do {
  348.     j--;
  349.     fprintf(texfile, "%d", dig[j]);
  350.   } while (j != 0);
  351.   putc('.', texfile);
  352.   f = f * 10 + 5;
  353.   delta = 10;
  354.   do {
  355.     if (delta > 1048576L)
  356.       f += 524288L - delta / 2;
  357.     fprintf(texfile, "%ld", f / 1048576L);
  358.     f = f % 1048576L * 10;
  359. /* p2c: bm2font.p, line 98:
  360.  * Note: Using % for possibly-negative arguments [317] */
  361.     delta *= 10;
  362.   } while (f > delta);
  363. }
  364.  
  365.  
  366. /*:12*/
  367. /*13:*/
  368. Static void fileproblem(void)
  369. {
  370.   printf("file-error %d : ", filestat);
  371.   switch (filestat) {
  372.  
  373.   case 2:
  374.     printf("file not found");
  375.     break;
  376.  
  377.   case 3:
  378.     printf("path not found");
  379.     break;
  380.  
  381.   case 4:
  382.     printf("too many files open");
  383.     break;
  384.  
  385.   case 5:
  386.     printf("file access denied");
  387.     break;
  388.  
  389.   case 6:
  390.     printf("invalid file handle");
  391.     break;
  392.  
  393.   case 12:
  394.     printf("invalid file access mode");
  395.     break;
  396.  
  397.   case 15:
  398.     printf("invalid drive number");
  399.     break;
  400.  
  401.   case 101:
  402.     printf("disk full");
  403.     break;
  404.  
  405.   default:
  406.     printf("ask a tpc guru");
  407.     break;
  408.   }
  409.   printf("!\n");
  410.   if (pcxtemp)
  411. /* p2c: bm2font.p, line 108: 
  412.  * Note: Called procedure Exec [285] */
  413.     Exec("rm", "pcxtemp.tmp");
  414.   if (giftemp)
  415. /* p2c: bm2font.p, line 108: 
  416.  * Note: Called procedure Exec [285] */
  417.     Exec("rm", "giftemp.tmp");
  418.   if (greypxl)
  419. /* p2c: bm2font.p, line 108: 
  420.  * Note: Called procedure Exec [285] */
  421.     Exec("rm", tmpname);
  422. }
  423.  
  424.  
  425. /*:13*/
  426. /*14:*/
  427. Static void opentempfile(void)
  428. {
  429.   strcpy(tmpname, "pcxtemp.tmp");
  430.   strcpy(tmpfil_NAME, "pcxtemp.tmp");
  431.   filestat = P_ioresult;
  432.   if (filestat == 0)
  433.     fok = false;
  434.   else
  435.     fok = true;
  436.   if (fok) {
  437.     printf("tempfile pcxtemp.tmp not accessable, abort\n");
  438.     fileproblem();
  439.     _Escape(0);
  440.   }
  441.   if (tmpfil != NULL)
  442.     tmpfil = freopen(tmpfil_NAME, "wb", tmpfil);
  443.   else
  444.     tmpfil = fopen(tmpfil_NAME, "wb");
  445.   _SETIO(tmpfil != NULL, FileNotFound);
  446.   filestat = P_ioresult;
  447.   if (filestat == 0)
  448.     fok = false;
  449.   else
  450.     fok = true;
  451.   if (!fok)
  452.     return;
  453.   printf("Unable to create pcxtmp.tmp, abort\n");
  454.   fileproblem();
  455.   _Escape(0);
  456. }
  457.  
  458.  
  459. /*:14*/
  460. /*15:*/
  461. Static void picstretch(void)
  462. {
  463.   short FORLIM;
  464.  
  465.   printf("Stretching the picture because of odd aspect ratio\n");
  466.   posbit = 0;
  467.   giftemp = true;
  468.   strcpy(tmpname, "giftemp.tmp");
  469.   strcpy(tmpfil_NAME, "giftemp.tmp");
  470.   filestat = P_ioresult;
  471.   if (filestat == 0)
  472.     fok = false;
  473.   else
  474.     fok = true;
  475.   if (fok) {
  476.     printf("tempfile giftemp.tmp not accessable, abort\n");
  477.     fileproblem();
  478.     _Escape(0);
  479.   }
  480.   if (tmpfil != NULL)
  481.     tmpfil = freopen(tmpfil_NAME, "wb", tmpfil);
  482.   else
  483.     tmpfil = fopen(tmpfil_NAME, "wb");
  484.   if (tmpfil == NULL)
  485.     _EscIO(FileNotFound);
  486.   while (!P_eof(bitmap)) {
  487.     fread(b1, mapinlen, 1, bitmap);
  488.     if (posbit == xratio) {
  489.       if (bitspsam == 8) {
  490.     FORLIM = mapinlen;
  491.     for (gbufp = 0; gbufp < FORLIM; gbufp++)
  492.       b3[gbufp] = ((unsigned)(b3[gbufp] + b1[gbufp])) >> 1;
  493.       } else if (bitspsam == 4) {
  494.     FORLIM = mapinlen;
  495.     for (gbufp = 0; gbufp < FORLIM; gbufp++) {
  496.       b = ((unsigned)
  497.            ((b3[gbufp] >> 4) + (b1[gbufp] >> 4))) >> 1;
  498.       c = ((unsigned)
  499.            ((b3[gbufp] & 15) + (b1[gbufp] & 15))) >> 1;
  500.       b3[gbufp] = (b << 4) + c;
  501.     }
  502.       }
  503.       _SETIO(fwrite(b3, mapinlen, 1, tmpfil) == 1,
  504.          FileWriteError);
  505.       filestat = P_ioresult;
  506.       if (filestat != 0) {
  507.     _SETIO(printf("Error occured when writing file %s!\n",
  508.               tmpname) >= 0, FileWriteError);
  509.     fileproblem();
  510.     _Escape(0);
  511.       }
  512.       posbit = 0;
  513.     }
  514.     posbit++;
  515.     if (posbit == xratio) {
  516.       FORLIM = mapinlen;
  517.       for (gbufp = 0; gbufp < FORLIM; gbufp++)
  518.     b3[gbufp] = b1[gbufp];
  519.     }
  520.     _SETIO(fwrite(b1, mapinlen, 1, tmpfil) == 1,
  521.        FileWriteError);
  522.     filestat = P_ioresult;
  523.     if (filestat != 0) {
  524.       _SETIO(printf("Error occured when writing file %s!\n",
  525.             tmpname) >= 0, FileWriteError);
  526.       fileproblem();
  527.       _Escape(0);
  528.     }
  529.   }
  530.   if (bitmap != NULL)
  531.     fclose(bitmap);
  532.   bitmap = NULL;
  533.   if (tmpfil != NULL)
  534.     fclose(tmpfil);
  535.   tmpfil = NULL;
  536.   strcpy(bitmap_NAME, "giftemp.tmp");
  537.   if (bitmap != NULL)
  538.     bitmap = freopen(bitmap_NAME, "rb", bitmap);
  539.   else
  540.     bitmap = fopen(bitmap_NAME, "rb");
  541.   if (bitmap == NULL)
  542.     _EscIO(FileNotFound);
  543.   posbit = 0;
  544.   cutgrey = false;
  545.   if (pcxtemp) {
  546. /* p2c: bm2font.p, line 136: 
  547.  * Note: Called procedure Exec [285] */
  548.     Exec("rm", "pcxtemp.tmp");
  549.     pcxtemp = false;
  550.   }
  551. }
  552.  
  553.  
  554. /*:15*/
  555. /*16:*/
  556. Static void scaling(void)
  557. {  /*17:*/
  558.   long newh, newv, counth, countv;
  559.   double scaleh, scalev, ledfac, repfac;
  560.   boolean grver, grhor, remlast;
  561.   short FORLIM;
  562.  
  563.   if (ledprinter)
  564.     ledfac = 0.5;
  565.   else
  566.     ledfac = 0.0;
  567.   if (hrep != 1)
  568.     repfac = 2.0;
  569.   else
  570.     repfac = 1.0;
  571.   if (!greypxl) {
  572.     usepxl = 1;
  573.     vusepxl = 1;
  574.     bitspsam = 1;
  575.   }
  576.   if (newwidth != 0)
  577.     newwidth = (long)floor(newwidth * truehres / 25.4 /
  578.                (repfac * (usepxl + ledfac)) + 0.5);
  579.   if (newheight != 0)
  580.     newheight = (long)floor(newheight * truevres / 25.4 /
  581.                 (repfac * (vusepxl + ledfac)) + 0.5);
  582.   if (newheight == 0)
  583.     newheight = (long)floor(
  584.       (double)mapheight / mapwidth * newwidth + 0.5);
  585.   if (newwidth == 0)
  586.     newwidth = (long)floor(
  587.       (double)mapwidth / mapheight * newheight + 0.5);
  588.   printf("Scaling the picture from %dx%d",
  589.      mapwidth, mapheight);
  590.   printf(" to %ldx%ld pixels\n", newwidth, newheight);
  591.   grver = (newheight > mapheight);
  592.   grhor = (newwidth > mapwidth);
  593.   if (grhor)
  594.     scaleh = newwidth / (newwidth - mapwidth + 1.0);
  595.   else
  596.     scaleh = mapwidth / (mapwidth - newwidth + 1.0);
  597.   if (grver)   /*:17*/
  598.     scalev = newheight / (newheight - mapheight + 1.0);
  599.   else
  600.     scalev = mapheight / (mapheight - newheight + 1.0);
  601.   /*18:*/
  602.   if (giftemp)
  603.     strcpy(tmpname, "pcxtemp.tmp");
  604.   else
  605.     strcpy(tmpname, "giftemp.tmp");
  606.   strcpy(tmpfil_NAME, tmpname);
  607.   filestat = P_ioresult;
  608.   if (filestat == 0)
  609.     fok = false;
  610.   else
  611.     fok = true;
  612.   if (fok) {
  613.     printf("tempfile %s not accessable, abort\n", tmpname);
  614.     fileproblem();
  615.     _Escape(0);
  616.   }
  617.   if (tmpfil != NULL)
  618.     tmpfil = freopen(tmpfil_NAME, "wb", tmpfil);
  619.   else
  620.     tmpfil = fopen(tmpfil_NAME, "wb");
  621.   if (tmpfil == NULL)
  622.     _EscIO(FileNotFound);
  623.   /*:18*/
  624.   pxlinlen = (halfk)
  625.       (((unsigned long)(mapwidth * bitspsam + 7)) >> 3);
  626.   greypix = mapwidth;
  627.   if (cutgrey) {
  628.     checksum = mapheight;
  629.     pxlinlen = cutlen;
  630.   } else
  631.     checksum = mapheight;
  632.   pl = 0;
  633.   fseek(bitmap, posbit, SEEK_SET);
  634.   for (pk = 0; pk <= 255; pk++)
  635.     lutsl[pk] = pk << (8 - bitspsam);
  636.   newv = 1;
  637.   countv = 0;
  638.   remlast = false;
  639.   do {
  640.     pxlinlen = fread(b1, 1, pxlinlen, bitmap);
  641.     colpos = 0;
  642.     cutrep = 0;
  643.     gbufpp = (newwidth * bitspsam + 7) / 8;
  644.     memset(b4, invbyte, 3000);
  645.     FORLIM = greypix;
  646.     for (pk = 0; pk < FORLIM; pk++)
  647.       b4[pk] = getcolor();
  648.     if (remlast) {
  649.       FORLIM = greypix;
  650.       for (pk = 0; pk < FORLIM; pk++)
  651.     b4[pk] = (b2[pk] * 2 + b4[pk]) / 3;
  652.       remlast = false;
  653.     }
  654.     gbufp = 0;
  655.     cb = 0;
  656.     memset(b3, invbyte, gbufpp);
  657.     newh = 1;
  658.     counth = 0;
  659.     pk = 0;
  660.     while (pk < greypix) {   /*19:*/
  661.       cb = lutsl[b4[pk]];
  662.       dbuf = ((unsigned)gbufp) >> 3;
  663.       bufsh = gbufp & 7;
  664.       nextbuf = bufsh + bitspsam;
  665.       switch (bufsh) {
  666.  
  667.       case 0:
  668.     b3[dbuf] = cb;
  669.     break;
  670.  
  671.       default:
  672.     b3[dbuf] ^= cb >> bufsh;
  673.     if (nextbuf > 8)
  674.       b3[dbuf + 1] = cb << (8 - bufsh);
  675.     break;
  676.       }
  677.       gbufp += bitspsam;
  678.       pk++;
  679.       counth++;
  680.       if (counth != (long)floor(newh * scaleh + 0.5))
  681.     continue;
  682.       if (grhor) {
  683.     while (counth >= (long)floor(newh * scaleh + 0.5)) {
  684.       if (pk == greypix)
  685.         cb = lutsl[(b4[pk - 1] * 2 + b4[pk - 2]) / 3];
  686.       else
  687.         cb = lutsl[(b4[pk] * 2 + b4[pk - 1]) / 3];
  688.       dbuf = ((unsigned)gbufp) >> 3;
  689.       bufsh = gbufp & 7;
  690.       nextbuf = bufsh + bitspsam;
  691.       switch (bufsh) {
  692.  
  693.       case 0:
  694.         b3[dbuf] = cb;
  695.         break;
  696.  
  697.       default:
  698.         b3[dbuf] ^= cb >> bufsh;
  699.         if (nextbuf > 8)
  700.           b3[dbuf + 1] = cb << (8 - bufsh);
  701.         break;
  702.       }
  703.       gbufp += bitspsam;
  704.       counth++;
  705.       newh++;
  706.     }
  707.     continue;
  708.       }
  709.       while (counth == (long)floor(newh * scaleh + 0.5)) {
  710.     b4[pk + 1] = (b4[pk + 1] * 2 + b4[pk]) / 3;
  711.     pk++;
  712.     newh++;
  713.     counth++;
  714.       }
  715.     }  /*:19*/
  716.     if (bitspsam == 1) {
  717.       dbuf = ((unsigned)gbufp) >> 3;
  718.       bufsh = gbufp & 7;
  719.       if (dbuf < gbufpp) {
  720.     cb = invbyte;
  721.     b3[dbuf] ^= cb >> bufsh;
  722.       }
  723.     }
  724.     fwrite(b3, gbufpp, 1, tmpfil);
  725.     checksum--;   /*20:*/
  726.     countv++;
  727.     if (cutgrey)
  728.       pxlinlen = (halfk)(b1[pxlinlen - 1] * 256 +
  729.              b1[pxlinlen - 2] + 2);
  730.     if (countv == (long)floor(newv * scalev + 0.5)) {
  731.       /*:20*/
  732.         if (grver) {
  733.     while (countv >= (long)floor(newv * scalev + 0.5)) {
  734.       fwrite(b3, gbufpp, 1, tmpfil);
  735.       countv++;
  736.       newv++;
  737.     }
  738.       } else {
  739.     while (countv == (long)floor(newv * scalev + 0.5)) {
  740.       pxlinlen = fread(b1, 1, pxlinlen, bitmap);
  741.       colpos = 0;
  742.       cutrep = 0;
  743.       FORLIM = greypix;
  744.       for (pk = 0; pk < FORLIM; pk++)
  745.         b2[pk] = getcolor();
  746.       remlast = true;
  747.       if (checksum > 0)
  748.         checksum--;
  749.       if (cutgrey)
  750.         pxlinlen = (halfk)(b1[pxlinlen - 1] * 256 +
  751.                    b1[pxlinlen - 2] + 2);
  752.       newv++;
  753.       countv++;
  754.     }
  755.       }
  756.     }
  757.   } while (checksum != 0);
  758.   if (bitmap != NULL)
  759.     fclose(bitmap);
  760.   bitmap = NULL;
  761.   if (tmpfil != NULL)
  762.     fclose(tmpfil);
  763.   tmpfil = NULL;   /*21:*/
  764.   if (giftemp) {   /*:21*/
  765. /* p2c: bm2font.p, line 202: 
  766.  * Note: Called procedure Exec [285] */
  767.     Exec("rm", "giftemp.tmp");
  768.     giftemp = false;
  769.     pcxtemp = true;
  770.   } else {
  771. /* p2c: bm2font.p, line 202: 
  772.  * Note: Called procedure Exec [285] */
  773.     Exec("rm", "pcxtemp.tmp");
  774.     giftemp = true;
  775.     pcxtemp = false;
  776.   }
  777.   strcpy(bitmap_NAME, tmpname);
  778.   if (bitmap != NULL)
  779.     bitmap = freopen(bitmap_NAME, "rb", bitmap);
  780.   else
  781.     bitmap = fopen(bitmap_NAME, "rb");
  782.   if (bitmap == NULL)
  783.     _EscIO(FileNotFound);
  784.   posbit = 0;
  785.   cutgrey = false;
  786.   mapheight = newheight;
  787.   mapwidth = newwidth;
  788.   if (bitspsam == 1)
  789.     pxlinlen = (mapwidth + 7) / 8;
  790.   mapheight = P_maxpos(bitmap) / ((mapwidth * bitspsam + 7) / 8);
  791. }  /*:16*/
  792.  
  793.  
  794. /* Local variables for tifint: */
  795. struct LOC_tifint {
  796.   boolean le;
  797.   uchar tb[3000];
  798. } ;
  799.  
  800. /*:22*/
  801. /*23:*/
  802. Local unsigned short tifword(unsigned short ba,
  803.                  struct LOC_tifint *LINK)
  804. {
  805.   if (LINK->le)
  806.     return ((unsigned short)LINK->tb[ba] +
  807.         (unsigned short)LINK->tb[ba + 1] * 256);
  808.   else
  809.     return ((unsigned short)(LINK->tb[ba] * 256) +
  810.         (unsigned short)LINK->tb[ba + 1]);
  811. }
  812.  
  813. Local long tiflong(unsigned short ba,
  814.            struct LOC_tifint *LINK)
  815. {
  816.   if (LINK->le)
  817.     return ((long)tifword(ba, LINK) +
  818.         (long)tifword(ba + 2, LINK) * 65536L);
  819.   else
  820.     return (LINK->tb[ba] * 16777216 + LINK->tb[ba + 1] * 65536 +
  821.         LINK->tb[ba + 2] * 256 + (long)LINK->tb[ba + 3]);
  822. }
  823.  
  824. Local unsigned short tiftwobyte(unsigned short ba,
  825.                 struct LOC_tifint *LINK)
  826. {
  827.   if (LINK->le)
  828.     return ((unsigned short)LINK->tb[ba] +
  829.         (unsigned short)LINK->tb[ba + 1] * 256);
  830.   else
  831.     return ((unsigned short)(LINK->tb[ba] * 256) +
  832.         (unsigned short)LINK->tb[ba + 1]);
  833. }
  834.  
  835.  
  836. /*22:*/
  837. Static void tifint(void)
  838. {   /*:23*/
  839.   struct LOC_tifint V;
  840.   unsigned short i, j, l, m, tl;
  841.   long ll;
  842.   uchar tp;
  843.   boolean strips;
  844.   unsigned short ttype;
  845.   long tiftemp;
  846.   uchar FORLIM1;
  847.  
  848.   /*24:*/
  849.   strips = false;
  850.   tl = fread(V.tb, 1, 2398, bitmap);
  851.   V.le = (V.tb[1] == 'I');
  852.   ll = tiflong(4, &V);
  853.   if (ll > 900) {
  854.     printf("I can't digest this\n");
  855.     _Escape(0);
  856.   }
  857.   l = ll;
  858.   m = tiftwobyte(l, &V);
  859.   l += 2;
  860.   for (i = 1; i <= m; i++) {
  861.     j = tiftwobyte(l, &V);
  862.     ttype = tiftwobyte(l + 2, &V);
  863.     if (ttype == 3)
  864.       tiftemp = tifword(l + 8, &V);
  865.     else
  866.       tiftemp = tiflong(l + 8, &V);
  867.     switch (j) {
  868.  
  869.     case 256:
  870.       mapwidth = tiftemp;
  871.       break;
  872.  
  873.     case 257:
  874.       mapheight = tiftemp;
  875.       break;
  876.  
  877.     case 258:
  878.       if (tiftemp > 1) {
  879.     greypxl = true;
  880.     bitspsam = tiftemp;
  881.     if (bitspsam > 8) {
  882.       printf("Only up to 256 greyshades supported\n");
  883.       _Escape(0);
  884.     }
  885.     mapdiv = 1.0;
  886.     FORLIM1 = bitspsam;
  887.     for (tp = 1; tp <= FORLIM1; tp++)
  888.       mapdiv *= 2;
  889.     mapdiv /= 16;
  890.       }
  891.       break;
  892.  
  893.     case 259:
  894.       if (tiftemp > 1) {
  895.     printf("No compression supported\n");
  896.     _Escape(0);
  897.       }
  898.       break;
  899.  
  900.     case 262:
  901.       photoinf = tiftemp;
  902.       if (photoinf > 1) {
  903.     printf("No RGB colours supported\n");
  904.     _Escape(0);
  905.       }
  906.       if (photoinf == 0)
  907.     invert = !invert;
  908.       if (invert) {
  909.     invbit = 0;
  910.     invbyte = 0;
  911.       } else {
  912.     invbit = 1;
  913.     invbyte = 255;
  914.       }
  915.       photoinf = 15;
  916.       break;
  917.  
  918.     case 273:
  919.       if (tiflong(l + 4, &V) != 1)
  920.     strips = true;
  921.       posbit = tiftemp;
  922.       break;
  923.  
  924.     case 277:
  925.       if (tiftemp != 1) {
  926.     printf("Only monochrome images supported\n");
  927.     _Escape(0);
  928.       }
  929.       break;
  930.  
  931.     case 278:
  932.       rowspstrip = tiftemp;
  933.       break;
  934.  
  935.     case 290:
  936.       greyunit = 1.0;
  937.       FORLIM1 = tifword(l + 4, &V);
  938.       for (tp = 1; tp <= FORLIM1; tp++)
  939.     greyunit /= 10;
  940.       break;
  941.  
  942.     case 291:
  943.       FORLIM1 = tiflong(l + 4, &V);
  944.       for (tp = 0; tp < FORLIM1; tp++)
  945.     tpal[tp] = V.tb[l + tp + 8];
  946.       break;
  947.     }
  948.     l += 12;
  949.     if (l >= tl) {
  950.       tl = fread(V.tb, 1, 2398, bitmap);
  951.       l = 0;
  952.     }
  953.   }
  954.   if (!strips)
  955.     return;
  956.   fseek(bitmap, posbit, SEEK_SET);
  957.   l = fread(V.tb, 1, 4, bitmap);
  958.   posbit = tiflong(0, &V);
  959. }
  960.  
  961.  
  962. /*:24*/
  963. /*26:*/
  964. Static void cutint(void)
  965. {
  966.   long FORLIM;
  967.   short FORLIM1;
  968.  
  969.   pxlinlen = 8;
  970.   pxlinlen = fread(b1, 1, pxlinlen, bitmap);
  971.   if (pxlinlen != 8) {
  972.     printf("CUT file too small!\n");
  973.     _Escape(0);
  974.   }
  975.   mapheight = b1[3] * 256 + b1[2];
  976.   mapwidth = b1[1] * 256 + b1[0];
  977.   photoinf = 15;
  978.   mapdiv = 16.0;
  979.   posbit = 8;
  980.   cutlen = b1[7] * 256 + b1[6] + 2;
  981.   bitspsam = 8;
  982.   cutgrey = true;
  983.   greypxl = true;
  984.   if (!egastretch)
  985.     return;
  986.   mapinlen = mapwidth;
  987.   printf("Shrinking the CUT picture because of odd aspect ratio\n");
  988.   giftemp = true;
  989.   strcpy(tmpname, "giftemp.tmp");
  990.   strcpy(tmpfil_NAME, "giftemp.tmp");
  991.   filestat = P_ioresult;
  992.   if (filestat == 0)
  993.     fok = false;
  994.   else
  995.     fok = true;
  996.   if (fok) {
  997.     printf("tempfile giftemp.tmp not accessable, abort\n");
  998.     fileproblem();
  999.     _Escape(0);
  1000.   }
  1001.   if (tmpfil != NULL)
  1002.     tmpfil = freopen(tmpfil_NAME, "wb", tmpfil);
  1003.   else
  1004.     tmpfil = fopen(tmpfil_NAME, "wb");
  1005.   if (tmpfil == NULL)
  1006.     _EscIO(FileNotFound);
  1007.   xratio = 2;
  1008.   posbit = 0;
  1009.   pxlinlen = cutlen;
  1010.   FORLIM = mapheight;
  1011.   for (checksum = 1; checksum <= FORLIM; checksum++) {
  1012.     pxlinlen = fread(b1, 1, pxlinlen, bitmap);
  1013.     colpos = 0;
  1014.     cutrep = 0;
  1015.     FORLIM1 = mapinlen;
  1016.     for (pk = 0; pk < FORLIM1; pk++)
  1017.       b2[pk] = getcolor();
  1018.     posbit++;
  1019.     FORLIM1 = mapinlen;
  1020.     for (gbufp = 0; gbufp < FORLIM1; gbufp++)
  1021.       b3[gbufp] = b2[gbufp];
  1022.     if (posbit < xratio) {
  1023.       _SETIO(fwrite(b2, mapinlen, 1, tmpfil) == 1,
  1024.          FileWriteError);
  1025.       filestat = P_ioresult;
  1026.       if (filestat != 0) {
  1027.     _SETIO(printf("Error occured when writing file %s!\n",
  1028.               tmpname) >= 0, FileWriteError);
  1029.     fileproblem();
  1030.     _Escape(0);
  1031.       }
  1032.     }
  1033.     if (posbit > xratio) {
  1034.       FORLIM1 = mapinlen;
  1035.       for (gbufp = 0; gbufp < FORLIM1; gbufp++)
  1036.     b3[gbufp] = ((unsigned)(b3[gbufp] + b2[gbufp])) >> 1;
  1037.       _SETIO(fwrite(b3, mapinlen, 1, tmpfil) == 1,
  1038.          FileWriteError);
  1039.       filestat = P_ioresult;
  1040.       if (filestat != 0) {
  1041.     _SETIO(printf("Error occured when writing file %s!\n",
  1042.               tmpname) >= 0, FileWriteError);
  1043.     fileproblem();
  1044.     _Escape(0);
  1045.       }
  1046.       posbit = 0;
  1047.       if (xratio == 2)
  1048.     xratio = 3;
  1049.       else
  1050.     xratio = 2;
  1051.     }
  1052.     pxlinlen = (halfk)(b1[pxlinlen - 1] * 256 +
  1053.                b1[pxlinlen - 2] + 2);
  1054.   }
  1055.   if (bitmap != NULL)
  1056.     fclose(bitmap);
  1057.   bitmap = NULL;
  1058.   if (tmpfil != NULL)
  1059.     fclose(tmpfil);
  1060.   tmpfil = NULL;
  1061.   strcpy(bitmap_NAME, "giftemp.tmp");
  1062.   if (bitmap != NULL)
  1063.     bitmap = freopen(bitmap_NAME, "rb", bitmap);
  1064.   else
  1065.     bitmap = fopen(bitmap_NAME, "rb");
  1066.   if (bitmap == NULL)
  1067.     _EscIO(FileNotFound);
  1068.   cutgrey = false;
  1069.   posbit = 0;
  1070. }
  1071.  
  1072.  
  1073. typedef unsigned short tabcode[4097];
  1074. typedef uchar bytecod[4097];
  1075. typedef uchar ilaced[4];
  1076. typedef uchar ebts_;
  1077.  
  1078.  
  1079. typedef long gifmask[13];
  1080.  
  1081.  
  1082. /* Local variables for gifint: */
  1083. struct LOC_gifint {
  1084.   ilaced ioff, ijump;
  1085.   long lvr, linepos;
  1086.   ebts_ logvr, codesample, modus, gifshift, nextbuf, bufsh;
  1087.   unsigned short maxcode, hh;
  1088.   short ec, ki;
  1089.   unsigned short hell[256];
  1090.   ebts_ gifbuf[3000];
  1091.   unsigned short codepos, gifinlen, gotinlen;
  1092.   long codesav;
  1093.   unsigned short dbuf, linebytes;
  1094. } ;
  1095.  
  1096. /*:27*/
  1097. /*28:*/
  1098. Local uchar getgif(struct LOC_gifint *LINK)
  1099. {
  1100.   fread(b1, 1, 1, bitmap);
  1101.   return (b1[0]);
  1102. }
  1103.  
  1104. Local void gifout(unsigned short b, struct LOC_gifint *LINK)
  1105. {
  1106.   short i, FORLIM;
  1107.   ebts_ ibits;
  1108.  
  1109.   if (gbufpp < 3000)
  1110.     b2[gbufpp] = LINK->hell[b];
  1111.   gbufpp++;
  1112.   if (gbufpp != LINK->hh)
  1113.     return;
  1114.   LINK->codepos = 0;
  1115.   memset(LINK->gifbuf, 0, LINK->linebytes);
  1116.   FORLIM = LINK->hh;
  1117.   for (i = 0; i < FORLIM; i++) {
  1118.     LINK->dbuf = LINK->codepos / 8;
  1119.     LINK->bufsh = LINK->codepos & 7;
  1120.     LINK->nextbuf = LINK->bufsh + bitspsam;
  1121.     cb = lutsl[b2[i]];
  1122.     if (LINK->bufsh == 0)
  1123.       LINK->gifbuf[LINK->dbuf] = cb;
  1124.     else {
  1125.       LINK->gifbuf[LINK->dbuf] |= cb >> LINK->bufsh;
  1126.       if (LINK->nextbuf > 8)
  1127.     LINK->gifbuf[LINK->dbuf + 1] = cb << (8 - LINK->bufsh);
  1128.     }
  1129.     LINK->codepos += bitspsam;
  1130.   }
  1131.   if (LINK->modus == 1)
  1132.     fseek(tmpfil, LINK->linepos, SEEK_SET);
  1133.   if (bitspsam == 1) {
  1134.     i = 8 - (LINK->hh & 7);
  1135.     if (i != 8) {
  1136.       cb = LINK->gifbuf[LINK->dbuf] >> i;
  1137.       cb <<= i;
  1138.       LINK->gifbuf[LINK->dbuf] = cb;
  1139.       cb = invbit;
  1140.       for (ibits = 1; ibits <= i; ibits++) {
  1141.     LINK->gifbuf[LINK->dbuf] ^= cb;
  1142.     cb <<= 1;
  1143.       }
  1144.     }
  1145.   }
  1146.   _SETIO(fwrite(LINK->gifbuf, LINK->linebytes, 1, tmpfil) ==
  1147.      1, FileWriteError);
  1148.   filestat = P_ioresult;
  1149.   if (filestat != 0) {
  1150.     _SETIO(printf("Error occured when writing file %s!\n",
  1151.           tmpname) >= 0, FileWriteError);
  1152.     fileproblem();
  1153.     _Escape(0);
  1154.   }
  1155.   LINK->lvr++;
  1156.   gbufpp = 0;
  1157.   LINK->linepos += LINK->linebytes * LINK->ijump[LINK->logvr];
  1158.   if (LINK->linepos >= P_maxpos(tmpfil) - 1) {
  1159.     LINK->logvr++;
  1160.     LINK->linepos = LINK->ioff[LINK->logvr] * LINK->linebytes;
  1161.   }
  1162. }
  1163.  
  1164. Local unsigned short gifcode(struct LOC_gifint *LINK)
  1165. {
  1166.   unsigned short Result;
  1167.  
  1168.   static gifmask codemask = {
  1169.     0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095
  1170.   };
  1171.  
  1172.   long codebyte;
  1173.  
  1174.   while (LINK->gifshift < LINK->codesample) {
  1175.     codebyte = b1[gbufp];
  1176.     gbufp++;
  1177.     if (gbufp == LINK->gifinlen) {
  1178.       LINK->gifinlen = getgif(LINK);
  1179.       LINK->gotinlen = fread(b1, 1, LINK->gifinlen, bitmap);
  1180.       gbufp = 0;
  1181.       if (LINK->gifinlen == 0)
  1182.     printf("Code zero, proceeding\n");
  1183.       if (LINK->gotinlen != LINK->gifinlen) {
  1184.     b1[LINK->gotinlen] = LINK->ec;
  1185.     LINK->gifinlen = LINK->gotinlen + 1;
  1186.       }
  1187.     }
  1188.     LINK->codesav |= codebyte << LINK->gifshift;
  1189.     LINK->gifshift += 8;
  1190.   }
  1191.   Result = LINK->codesav & codemask[LINK->codesample];
  1192.   LINK->codesav = ((unsigned long)LINK->codesav) >>
  1193.           LINK->codesample;
  1194.   LINK->gifshift -= LINK->codesample;
  1195.   LINK->ki++;
  1196.   if (LINK->ki > LINK->maxcode && LINK->codesample < 12) {
  1197.     LINK->codesample++;
  1198.     LINK->maxcode = 1 << LINK->codesample;
  1199.   }
  1200.   return Result;
  1201. }
  1202.  
  1203.  
  1204. /*:26*/
  1205. /*27:*/
  1206. Static void gifint(void)
  1207. {   /*:28*/
  1208.   struct LOC_gifint V;
  1209.  
  1210.   static ilaced ioff = {
  1211.     0, 4, 2, 1
  1212.   };
  1213.  
  1214.   static ilaced ijump = {
  1215.     8, 8, 4, 2
  1216.   };
  1217.  
  1218.   unsigned short a, g, hi, vi, vh, hr, vr;
  1219.   short merk, ic, codegif, i;
  1220.   ebts_ colmap, b, col, bpp;
  1221.   ebts_ gpal[256][3];
  1222.   unsigned short *codtab;
  1223.   unsigned short folge, minhell, maxhell;
  1224.   uchar *anf;
  1225.   Char hexbyte[7];
  1226.   short FORLIM;
  1227.  
  1228.   /*29:*/
  1229.   memcpy(V.ioff, ioff, sizeof(ilaced));
  1230.   memcpy(V.ijump, ijump, sizeof(ilaced));
  1231.   printf("Decoding GIF file\n");
  1232.   V.logvr = 0;
  1233.   V.codesav = 0;
  1234.   V.lvr = 0;
  1235.   minhell = 9999;
  1236.   maxhell = 0;
  1237.   codtab = Malloc(sizeof(tabcode));
  1238.   anf = Malloc(sizeof(bytecod));
  1239.   opentempfile();
  1240.   *hexbyte = '\0';
  1241.   for (i = 1; i <= 3; i++) {
  1242.     sprintf(hexbyte + strlen(hexbyte), "%c", getgif(&V));
  1243. /* p2c: bm2font.p, line 309:
  1244.  * Note: Possible string truncation in assignment [145] */
  1245.   }
  1246.   if (strcmp(hexbyte, "GIF")) {
  1247.     printf("This file is NOT a gif file!\n");
  1248.     _Escape(0);
  1249.   }
  1250.   for (i = 1; i <= 3; i++) {
  1251.     sprintf(hexbyte + strlen(hexbyte), "%c", getgif(&V));
  1252. /* p2c: bm2font.p, line 311:
  1253.  * Note: Possible string truncation in assignment [145] */
  1254.   }
  1255.   if (strcmp(hexbyte, "GIF87a") && strcmp(hexbyte, "GIF89a")) {
  1256.     printf("The program only supports version 87a and 89a!\n");
  1257.     _Escape(0);
  1258.   }
  1259.   b = getgif(&V);
  1260.   hr = b;
  1261.   b = getgif(&V);
  1262.   hr += b * 256;
  1263.   b = getgif(&V);
  1264.   vr = b;
  1265.   b = getgif(&V);
  1266.   vr += b * 256;
  1267.   if (hr == 640 && vr == 350) {
  1268.     xratio = 3;
  1269.     yratio = 4;
  1270.   }
  1271.   b = getgif(&V);
  1272.   colmap = b & 128;
  1273.   colmap >>= 7;
  1274.   col = b & 112;
  1275.   col >>= 4;
  1276.   col++;
  1277.   bitspsam = b & 7;
  1278.   bitspsam++;
  1279.   b = getgif(&V);
  1280.   b = getgif(&V);
  1281.   if (colmap == 1) {   /*30:*/
  1282.     g = 1;
  1283.     FORLIM = bitspsam;
  1284.     for (i = 1; i <= FORLIM; i++)
  1285.       g *= 2;
  1286.     for (i = 0; i < g; i++) {
  1287.       for (j = 0; j <= 2; j++)
  1288.     gpal[i][j] = getgif(&V);
  1289.       V.hell[i] = (long)floor(gpal[i][0] * 0.287 + gpal[i]
  1290.                 [1] * 0.589 + gpal[i]
  1291.                 [2] * 0.114 + 0.5);
  1292.       if (V.hell[i] > maxhell)
  1293.     maxhell = V.hell[i];
  1294.     }
  1295.     for (i = 0; i < g; i++)   /*:30*/
  1296.       V.hell[i] = (long)floor(
  1297.         (double)V.hell[i] / maxhell * (g - 1) + 0.5);
  1298.   }
  1299.   b = getgif(&V);
  1300.   if (b != 44) {   /*31:*/
  1301.     while (b != 44) {   /*:31*/
  1302.       b = getgif(&V);
  1303.       switch (b) {
  1304.  
  1305.       case 249:
  1306.     a = getgif(&V);
  1307.     for (g = 0; g <= a; g++)
  1308.       b = getgif(&V);
  1309.     break;
  1310.  
  1311.       case 254:
  1312.     a = getgif(&V);
  1313.     while (a != 0) {
  1314.       for (g = 0; g <= a; g++)
  1315.         b = getgif(&V);
  1316.       a = b;
  1317.       if (P_eof(bitmap)) {
  1318.         printf("file ended while looking for image\n");
  1319.         _Escape(0);
  1320.       }
  1321.     }
  1322.     break;
  1323.  
  1324.       case 1:
  1325.       case 255:
  1326.     a = getgif(&V);
  1327.     for (g = 1; g <= a; g++)
  1328.       b = getgif(&V);
  1329.     a = getgif(&V);
  1330.     while (a != 0) {
  1331.       for (g = 0; g <= a; g++)
  1332.         b = getgif(&V);
  1333.       a = b;
  1334.       if (P_eof(bitmap)) {
  1335.         printf("file ended while looking for image\n");
  1336.         _Escape(0);
  1337.       }
  1338.     }
  1339.     break;
  1340.       }
  1341.       b = getgif(&V);
  1342.       if (P_eof(bitmap)) {
  1343.     printf("file ended while looking for image\n");
  1344.     _Escape(0);
  1345.       }
  1346.     }
  1347.   }
  1348.   b = getgif(&V);
  1349.   hi = b;
  1350.   b = getgif(&V);
  1351.   hi += b * 256;
  1352.   b = getgif(&V);
  1353.   vi = b;
  1354.   b = getgif(&V);
  1355.   vi += b * 256;
  1356.   b = getgif(&V);
  1357.   V.hh = b;
  1358.   b = getgif(&V);
  1359.   V.hh += b * 256;
  1360.   b = getgif(&V);
  1361.   vh = b;
  1362.   b = getgif(&V);
  1363.   vh += b * 256;
  1364.   if (V.hh != hr || vh != vr)
  1365.     printf("No fragments supported!\n");
  1366.   V.linebytes = (V.hh * bitspsam + 7) / 8;
  1367.   if (V.linebytes > 3000) {
  1368.     V.linebytes = 3000;
  1369.     printf("Sorry, had to cut picture to a width of 3000 bytes\n");
  1370.   }
  1371.   b = getgif(&V);
  1372.   col = b & 128;
  1373.   col >>= 7;
  1374.   if (col != 0) {
  1375.     bitspsam = b & 7;
  1376.     bitspsam++;   /*30:*/
  1377.     g = 1;
  1378.     FORLIM = bitspsam;
  1379.     for (i = 1; i <= FORLIM; i++)
  1380.       g *= 2;
  1381.     for (i = 0; i < g; i++) {
  1382.       for (j = 0; j <= 2; j++)
  1383.     gpal[i][j] = getgif(&V);
  1384.       V.hell[i] = (long)floor(gpal[i][0] * 0.287 + gpal[i]
  1385.                 [1] * 0.589 + gpal[i]
  1386.                 [2] * 0.114 + 0.5);
  1387.       if (V.hell[i] > maxhell)
  1388.     maxhell = V.hell[i];
  1389.     }
  1390.     for (i = 0; i < g; i++)   /*:30*/
  1391.       V.hell[i] = (long)floor(
  1392.         (double)V.hell[i] / maxhell * (g - 1) + 0.5);
  1393.   }
  1394.   for (pk = 0; pk <= 255; pk++)
  1395.     lutsl[pk] = pk << (8 - bitspsam);
  1396.   V.modus = b << 1;
  1397.   V.modus >>= 7;
  1398.   if (V.modus == 0)
  1399.     printf("Image is loaded sequentially.\n");
  1400.   else
  1401.     printf(
  1402.       "Interlaced mode, loading the image will take more time.\n");
  1403.   if (V.modus != 0) {
  1404.     memset(V.gifbuf, 0, V.linebytes);
  1405.     for (i = 1; i <= vh; i++) {
  1406.       _SETIO(fwrite(V.gifbuf, V.linebytes, 1, tmpfil) == 1,
  1407.          FileWriteError);
  1408.       filestat = P_ioresult;
  1409.       if (filestat != 0) {
  1410.     _SETIO(printf("Error occured when writing file %s!\n",
  1411.               tmpname) >= 0, FileWriteError);
  1412.     fileproblem();
  1413.     _Escape(0);
  1414.       }
  1415.     }
  1416.     V.linepos = 0;
  1417.     V.logvr = 0;
  1418.     fseek(tmpfil, 0, SEEK_SET);
  1419.   }
  1420.   bpp = b << 5;
  1421.   bpp >>= 5;
  1422.   bpp++;
  1423.   bpp = getgif(&V);
  1424.   g = 1 << bpp;
  1425.   ic = 1 << bpp;
  1426.   V.ec = ic + 1;
  1427.   V.codesample = bpp + 1;
  1428.   V.maxcode = 1 << V.codesample;
  1429.   for (i = 0; i <= 4095; i++)
  1430.     codtab[i] = 4098;
  1431.   V.ki = V.ec + 1;
  1432.   a = 4098;
  1433.   gbufpp = 0;
  1434.   V.gifshift = 0;
  1435.   V.gifinlen = getgif(&V);
  1436.   fread(b1, V.gifinlen, 1, bitmap);
  1437.   gbufp = 0;
  1438.   if (V.gifinlen == 0)
  1439.     _Escape(0);
  1440.   merk = 0;   /*32:*/
  1441.   while (V.lvr < vh) {   /*:32*/
  1442.     codegif = gifcode(&V);
  1443.     if (codegif == V.ec) {
  1444.       printf("End of code before gif image completed!\n");
  1445.       V.lvr = vh;
  1446.       continue;
  1447.     }
  1448.     if (codegif == ic) {
  1449.       for (i = 0; i <= 4095; i++)
  1450.     codtab[i] = 4098;
  1451.       V.ki = V.ec + 1;
  1452.       V.codesample = bpp + 1;
  1453.       V.maxcode = 1 << V.codesample;
  1454.       a = 4098;
  1455.       continue;
  1456.     }
  1457.     if (codegif < ic)
  1458.       gifout(codegif, &V);
  1459.     else {
  1460.       if (codtab[codegif] == 4098) {
  1461.     if (codegif != V.ki - 2) {
  1462.       printf("Bytes are mangled!\n");
  1463.       _Escape(0);
  1464.     }
  1465.     folge = a;
  1466.     merk++;
  1467.     while (folge > ic)
  1468.       folge = codtab[folge];
  1469.     b3[merk] = folge;
  1470.     anf[V.ki - 2] = folge;
  1471.     folge = a;
  1472.       } else
  1473.     folge = codegif;
  1474.       while (folge > ic && folge <= 4095) {
  1475.     merk++;
  1476.     b3[merk] = anf[folge];
  1477.     folge = codtab[folge];
  1478.       }
  1479.       merk++;
  1480.       b3[merk] = folge;
  1481.       while (merk > 0) {
  1482.     gifout(b3[merk], &V);
  1483.     merk--;
  1484.       }
  1485.     }
  1486.     if (a != 4098) {
  1487.       codtab[V.ki - 2] = a;
  1488.       if (codegif == V.ki - 2) {
  1489.     folge = a;
  1490.     while (folge > ic)
  1491.       folge = codtab[folge];
  1492.     anf[V.ki - 2] = folge;
  1493.       } else {
  1494.     folge = codegif;
  1495.     while (folge > ic)
  1496.       folge = codtab[folge];
  1497.     anf[V.ki - 2] = folge;
  1498.       }
  1499.     }
  1500.     a = codegif;
  1501.   }
  1502.   if (bitmap != NULL)
  1503.     fclose(bitmap);
  1504.   bitmap = NULL;
  1505.   if (tmpfil != NULL)
  1506.     fclose(tmpfil);
  1507.   tmpfil = NULL;
  1508.   if (bitspsam > 1)
  1509.     greypxl = true;
  1510.   mapinlen = V.linebytes;
  1511.   strcpy(bitmap_NAME, "pcxtemp.tmp");
  1512.   if (bitmap != NULL)
  1513.     bitmap = freopen(bitmap_NAME, "rb", bitmap);
  1514.   else
  1515.     bitmap = fopen(bitmap_NAME, "rb");
  1516.   if (bitmap == NULL)
  1517.     _EscIO(FileNotFound);
  1518.   pcxtemp = true;
  1519.   if (egastretch) {
  1520.     if (xratio + 1 == yratio)
  1521.       picstretch();
  1522.   }
  1523.   posbit = 0;
  1524.   mapdiv = 1.0;
  1525.   FORLIM = bitspsam;
  1526.   for (i = 1; i <= FORLIM; i++)
  1527.     mapdiv *= 2;
  1528.   mapdiv /= 16;
  1529. }
  1530.  
  1531.  
  1532. /*:29*/
  1533. /*33:*/
  1534. Static uchar getpcx(void)
  1535. {
  1536.   uchar Result;
  1537.  
  1538.   if (gbufp == pxlinlen) {
  1539.     gbufp = 0;
  1540.     pxlinlen = fread(b1, 1, 640, bitmap);
  1541.   }
  1542.   Result = b1[gbufp];
  1543.   gbufp++;
  1544.   return Result;
  1545. }
  1546.  
  1547.  
  1548. /*:33*/
  1549. /*34:*/
  1550. Static long longpcx(void)
  1551. {
  1552.   long bl;
  1553.   ebts pb;
  1554.  
  1555.   pb = getpcx();
  1556.   bl = pb;
  1557.   pb = getpcx();
  1558.   bl += pb * 256;
  1559.   pb = getpcx();
  1560.   bl += pb * 65536;
  1561.   pb = getpcx();
  1562.   bl += pb * 16777216;
  1563.   return bl;
  1564. }
  1565.  
  1566.  
  1567. /*:34*/
  1568. /*35:*/
  1569. Static unsigned short wordpcx(void)
  1570. {
  1571.   unsigned short bl;
  1572.   ebts pb;
  1573.  
  1574.   pb = getpcx();
  1575.   bl = pb;
  1576.   pb = getpcx();
  1577.   bl += pb * 256;
  1578.   return bl;
  1579. }  /*:35*/
  1580.  
  1581.  
  1582. /*36:*/
  1583. Static void pcxint(void)
  1584. {
  1585.   unsigned short i, ki, k, hi, hh, vi, vh, hr, vr, bzf;
  1586.   ebts xb, j, b, a, col, bps, ibits, pcxver, pcxcomp;
  1587.   ebts gpal[256][3];
  1588.   ebts hell[256];
  1589.   unsigned short FORLIM;
  1590.   short FORLIM1;
  1591.  
  1592.   printf("Analysing PCX file %s\n", bmname);
  1593.   gbufp = 640;
  1594.   pxlinlen = 640;
  1595.   b = getpcx();
  1596.   if (b != 10 && b != 205) {
  1597.     printf("What a bad joke!\n");
  1598.     printf("This is NOT a PCX file!\n");
  1599.     _Escape(0);
  1600.   }
  1601.   if (b == 205)
  1602.     printf("First byte 205, file seems to be captured!\n");
  1603.   /*37:*/
  1604.   pcxver = getpcx();
  1605.   pcxcomp = getpcx();
  1606.   bps = getpcx();
  1607.   hi = getpcx();
  1608.   b = getpcx();
  1609.   hi += b * 256;
  1610.   vi = getpcx();
  1611.   b = getpcx();
  1612.   vi += b * 256;
  1613.   hh = getpcx();
  1614.   b = getpcx();
  1615.   hh += b * 256;
  1616.   vh = getpcx();
  1617.   b = getpcx();
  1618.   vh += b * 256;
  1619.   hr = getpcx();
  1620.   b = getpcx();
  1621.   hr += b * 256;
  1622.   vr = getpcx();
  1623.   b = getpcx();
  1624.   vr += b * 256;
  1625.   if (hr == 640 && vr == 350) {
  1626.     xratio = 3;
  1627.     yratio = 4;
  1628.   }
  1629.   if (hr == 320 && vr == 200) {
  1630.     xratio = 5;
  1631.     yratio = 6;
  1632.   }
  1633.   hh += 1 - hi;
  1634.   vh -= vi;
  1635.   mapwidth = hh;
  1636.   for (i = 0; i <= 15; i++) {
  1637.     for (j = 0; j <= 2; j++)
  1638.       gpal[i][j] = getpcx();
  1639.   }
  1640.   b = getpcx();
  1641.   col = getpcx();
  1642.   bzf = getpcx();
  1643.   b = getpcx();
  1644.   bzf += b * 256;
  1645.   hh = bzf;
  1646.   b = getpcx();
  1647.   b = getpcx();
  1648.   for (i = 70; i <= 127; i++)
  1649.     b = getpcx();
  1650.   bitspsam = bps;
  1651.   if (col == 4) {
  1652.     bitspsam = 4;
  1653.     greypxl = true;
  1654.   } else if (bps > 1) {
  1655.     bitspsam = bps;
  1656.     greypxl = true;
  1657.   }
  1658.   mapinlen = ((unsigned long)(mapwidth * bitspsam + 7)) >> 3;
  1659.       /*:37*/
  1660.   /*38:*/
  1661.   for (i = 0; i <= 15; i++)
  1662.     hell[i] = (long)(gpal[i][0] / 16.0 * 0.287 + gpal[i]
  1663.           [1] / 16.0 * 0.589 + gpal[i][2] / 16.0 * 0.114);
  1664.   for (i = 0; i <= 15; i++) {
  1665.     if (hell[i] > 15)
  1666.       hell[i] = 15;
  1667.   }
  1668.   opentempfile();   /*:38*/
  1669.   if (pcxver == 5 && bitspsam > 4) {   /*39:*/
  1670.     gbufp = 640;
  1671.     pxlinlen = 640;
  1672.     posbit = P_maxpos(bitmap);
  1673.     posbit -= 769;
  1674.     fseek(bitmap, posbit, SEEK_SET);
  1675.     b = getpcx();
  1676.     if (b != 12) {
  1677.       printf("No valid information in 256 colors palette\n");
  1678.       printf("Using standard 16 colors palette\n");
  1679.     } else {
  1680.       for (i = 0; i <= 255; i++) {
  1681.     for (j = 0; j <= 2; j++)
  1682.       gpal[i][j] = getpcx();
  1683.     hell[i] = (long)floor(gpal[i][0] * 0.287 + gpal[i]
  1684.                 [1] * 0.589 + gpal[i]
  1685.                 [2] * 0.114 + 0.5);
  1686.       }
  1687.     }
  1688.     fseek(bitmap, 128, SEEK_SET);
  1689.     gbufp = 640;
  1690.     pxlinlen = 640;
  1691.   }
  1692.   /*:39*/
  1693.   /*40:*/
  1694.   a = 0;
  1695.   for (i = 0; i <= vh; i++) {   /*:40*/
  1696.     FORLIM1 = mapinlen;
  1697.     for (gbufpp = 0; gbufpp < FORLIM1; gbufpp++)
  1698.       b3[gbufpp] = 0;
  1699.     for (j = 0; j < col; j++) {
  1700.       gbufpp = 0;
  1701.       for (k = 1; k <= hh; k++) {
  1702.     if (a == 0) {
  1703.       b = getpcx();
  1704.       if ((b & 0xc0) == 0xc0) {
  1705.         a = (b & 0x3f) - 1;
  1706.         b = getpcx();
  1707.       }
  1708.     } else
  1709.       a--;
  1710.     if (col == 4) {
  1711.       for (ibits = 0; ibits <= 7; ibits++) {
  1712.         ki = gbufpp * 4 + (ibits >> 1);
  1713.         xb = b << ibits;
  1714.         xb >>= 7;
  1715.         xb <<= j;
  1716.         xb <<= (1 - (ibits & 1)) << 2;
  1717.         b3[ki] ^= xb;
  1718.       }
  1719.     } else {
  1720.       b3[gbufpp] = b;
  1721.       if (gbufpp == mapinlen - 1 && bitspsam == 1) {
  1722.         ki = 8 - (mapwidth & 7);
  1723.         if (ki != 8) {
  1724.           xb = b3[gbufpp] >> ki;
  1725.           xb <<= ki;
  1726.           b3[gbufpp] = xb;
  1727.           xb = invbit;
  1728.           for (ibits = 1; ibits <= ki; ibits++) {
  1729.         b3[gbufpp] ^= xb;
  1730.         xb <<= 1;
  1731.           }
  1732.         }
  1733.       }
  1734.     }
  1735.     gbufpp++;
  1736.       }
  1737.     }
  1738.     if (col == 4) {
  1739.       gbufpp = 0;
  1740.       hi = (hh - 1) * 4 + 3;
  1741.       for (k = 0; k <= hi; k++) {
  1742.     b3[gbufpp] = (hell[b3[k] >> 4] << 4) + hell[b3[k] & 15];
  1743.     gbufpp++;
  1744.       }
  1745.     } else if (bps == 8 && pcxver == 5) {
  1746.       hi = hh - 1;
  1747.       for (k = 0; k <= hi; k++)
  1748.     b3[k] = hell[b3[k]];
  1749.       gbufpp = hh;
  1750.     }
  1751.     _SETIO(fwrite(b3, mapinlen, 1, tmpfil) == 1,
  1752.        FileWriteError);
  1753.     filestat = P_ioresult;
  1754.     if (filestat != 0) {
  1755.       _SETIO(printf("Error occured when writing file %s!\n",
  1756.             tmpname) >= 0, FileWriteError);
  1757.       fileproblem();
  1758.       _Escape(0);
  1759.     }
  1760.     if (hr == 640 && vr == 200) {
  1761.       _SETIO(fwrite(b3, mapinlen, 1, tmpfil) == 1,
  1762.          FileWriteError);
  1763.       filestat = P_ioresult;
  1764.       if (filestat != 0) {
  1765.     _SETIO(printf("Error occured when writing file %s!\n",
  1766.               tmpname) >= 0, FileWriteError);
  1767.     fileproblem();
  1768.     _Escape(0);
  1769.       }
  1770.     }
  1771.   }
  1772.   if (bitmap != NULL)
  1773.     fclose(bitmap);
  1774.   bitmap = NULL;
  1775.   if (tmpfil != NULL)
  1776.     fclose(tmpfil);
  1777.   tmpfil = NULL;
  1778.   strcpy(bitmap_NAME, "pcxtemp.tmp");
  1779.   if (bitmap != NULL)
  1780.     bitmap = freopen(bitmap_NAME, "rb", bitmap);
  1781.   else
  1782.     bitmap = fopen(bitmap_NAME, "rb");
  1783.   if (bitmap == NULL)
  1784.     _EscIO(FileNotFound);
  1785.   pcxtemp = true;
  1786.   if (egastretch) {
  1787.     if (xratio + 1 == yratio)
  1788.       picstretch();
  1789.   }
  1790.   posbit = 0;
  1791.   mapdiv = 1.0;
  1792.   FORLIM = bitspsam;
  1793.   for (i = 1; i <= FORLIM; i++)
  1794.     mapdiv *= 2;
  1795.   mapdiv /= 16;
  1796. }
  1797.  
  1798.  
  1799. /* Local variables for iffint: */
  1800. struct LOC_iffint {
  1801.   unsigned short hi, hh, vi, vh, compr;
  1802.   uchar a, b, col, bps;
  1803.   uchar gpal[256][3];
  1804.   uchar hell[256];
  1805.   Char bmstr[256];
  1806.   long bmsize, bmoff, bl;
  1807. } ;
  1808.  
  1809. /*:41*/
  1810. /*42:*/
  1811. Local long ifflong(struct LOC_iffint *LINK)
  1812. {
  1813.   ebts k;
  1814.  
  1815.   fread(b1, 4, 1, bitmap);
  1816.   LINK->bl = b1[0];
  1817.   for (k = 1; k <= 3; k++)
  1818.     LINK->bl = LINK->bl * 256 + b1[k];
  1819.   return LINK->bl;
  1820. }
  1821.  
  1822. /*:42*/
  1823. /*43:*/
  1824. Local unsigned short iffword(struct LOC_iffint *LINK)
  1825. {
  1826.   fread(b1, 2, 1, bitmap);
  1827.   return (b1[0] * 256 + b1[1]);
  1828. }
  1829.  
  1830. /*:43*/
  1831. /*44:*/
  1832. Local void junkchunk(struct LOC_iffint *LINK)
  1833. {
  1834.   if (LINK->bmsize > P_maxpos(bitmap)) {
  1835.     printf("File endend while reading junk\n");
  1836.     _Escape(0);
  1837.   }
  1838.   while (LINK->bmsize > 0) {
  1839.     fread(&LINK->b, 1, 1, bitmap);
  1840.     LINK->bmsize--;
  1841.   }
  1842. }  /*:44*/
  1843.  
  1844. /*45:*/
  1845. Local void checkchunk(struct LOC_iffint *LINK)
  1846. {
  1847.   short i, j, FORLIM;
  1848.  
  1849.   if (!strcmp(LINK->bmstr, "BMHD")) {
  1850.     LINK->bmsize = ifflong(LINK);
  1851.     if (LINK->bmsize & 1)
  1852.       LINK->bmsize++;
  1853.     LINK->hh = iffword(LINK);
  1854.     LINK->vh = iffword(LINK);
  1855.     LINK->hi = iffword(LINK);
  1856.     LINK->vi = iffword(LINK);
  1857.     fread(&LINK->bps, 1, 1, bitmap);
  1858.     fread(&LINK->b, 1, 1, bitmap);
  1859.     fread(&LINK->b, 1, 1, bitmap);
  1860.     LINK->compr = LINK->b;
  1861.     for (i = 1; i <= 4; i++)
  1862.       fread(&xratio, 1, 1, bitmap);
  1863.     fread(&yratio, 1, 1, bitmap);
  1864.     LINK->bmsize -= 16;
  1865.     junkchunk(LINK);
  1866.     if (LINK->bps == 1) {
  1867.       LINK->col = 1;
  1868.       return;
  1869.     }
  1870.     if (LINK->bps == 4)
  1871.       LINK->col = 15;
  1872.     else
  1873.       LINK->col = 255;
  1874.     return;
  1875.   }
  1876.   if (!strcmp(LINK->bmstr, "CMAP")) {
  1877.     LINK->bmsize = ifflong(LINK);
  1878.     LINK->col = LINK->bmsize / 3 - 1;
  1879.     FORLIM = LINK->col;
  1880.     for (i = 0; i <= FORLIM; i++) {
  1881.       for (j = 0; j <= 2; j++) {
  1882.     fread(&LINK->b, 1, 1, bitmap);
  1883.     LINK->gpal[i][j] = LINK->b;
  1884.       }
  1885.       LINK->hell[i] = (long)floor(
  1886.         LINK->gpal[i][0] * 0.287 + LINK->gpal[i]
  1887.           [1] * 0.589 + LINK->gpal[i][2] * 0.114 + 0.5);
  1888.     }
  1889.     if (LINK->bmsize & 1)
  1890.       fread(&LINK->b, 1, 1, bitmap);
  1891.     return;
  1892.   }
  1893.   if (!strcmp(LINK->bmstr, "CRNG")) {
  1894.     LINK->bmsize = ifflong(LINK);
  1895.     LINK->bmoff = iffword(LINK);
  1896.     LINK->bmoff = iffword(LINK);
  1897.     LINK->bmoff = iffword(LINK);
  1898.     fread(&LINK->b, 1, 1, bitmap);
  1899.     fread(&LINK->a, 1, 1, bitmap);
  1900.     if (LINK->bps == 4) {
  1901.       if (LINK->b < LINK->a) {
  1902.     FORLIM = LINK->a;
  1903.     for (i = LINK->b; i <= FORLIM; i++)
  1904.       LINK->hell[i] = i;
  1905.       } else if (LINK->b > LINK->a) {
  1906.     FORLIM = LINK->b;
  1907.     for (i = LINK->a; i <= FORLIM; i++)
  1908.       LINK->hell[i] = i;
  1909.       }
  1910.     }
  1911.     if (LINK->bmsize & 1)
  1912.       fread(&LINK->b, 1, 1, bitmap);
  1913.     return;
  1914.   }
  1915.   LINK->bmsize = ifflong(LINK);
  1916.   if (LINK->bmsize & 1)
  1917.     LINK->bmsize++;
  1918.   junkchunk(LINK);
  1919. }
  1920.  
  1921.  
  1922. /*:36*/
  1923. /*41:*/
  1924. Static void iffint(void)
  1925. {   /*:45*/
  1926.   struct LOC_iffint V;
  1927.   uchar cb;
  1928.   short FORLIM, FORLIM1;
  1929.  
  1930.   /*46:*/
  1931.   printf("Analysing IFF file %s\n", bmname);
  1932.   opentempfile();
  1933.   fread(V.bmstr, 4, 1, bitmap);
  1934.   V.bmstr[4] = '\0';
  1935.   if (strcmp(V.bmstr, "FORM")) {
  1936.     printf("Graphic file not stored as Interchange File Format\n");
  1937.     _Escape(0);
  1938.   }
  1939.   V.bmsize = ifflong(&V) + 8;
  1940.   if (V.bmsize != P_maxpos(bitmap)) {
  1941.     printf("File is incomplete, should contain %ld\n",
  1942.        V.bmsize);
  1943.     _Escape(0);
  1944.   }
  1945.   fread(V.bmstr, 4, 1, bitmap);
  1946.   V.bmstr[4] = '\0';
  1947.   if (strcmp(V.bmstr, "ILBM"))
  1948.     printf("Type of file is %s, should be ILBM\n", V.bmstr);
  1949.   fread(V.bmstr, 4, 1, bitmap);
  1950.   V.bmstr[4] = '\0';
  1951.   while (strcmp(V.bmstr, "BODY")) {
  1952.     if (P_eof(bitmap)) {
  1953.       printf("No graphics information available\n");
  1954.       _Escape(0);
  1955.     }
  1956.     checkchunk(&V);
  1957.     fread(V.bmstr, 4, 1, bitmap);
  1958.     V.bmstr[4] = '\0';
  1959.   }
  1960.   V.bmsize = ifflong(&V);
  1961.   V.hh = (V.hh * V.bps + 7) / 8;
  1962.   FORLIM = V.vh;
  1963.   for (i = 1; i <= FORLIM; i++) {
  1964.     if (P_eof(bitmap)) {
  1965.       printf("ende\n");
  1966.       _Escape(0);
  1967.     }
  1968.     if (V.compr == 0)
  1969.       fread(b3, V.hh, 1, bitmap);
  1970.     else {   /*47:*/
  1971.       gbufp = 0;
  1972.       while (gbufp < V.hh) {
  1973.     fread(&V.b, 1, 1, bitmap);
  1974.     V.bmoff++;
  1975.     if (V.b < 128) {
  1976.       V.b++;
  1977.       fread(&b3[gbufp], V.b, 1, bitmap);
  1978.       gbufp += V.b;
  1979.       continue;
  1980.     }
  1981.     if (V.b <= 128)
  1982.       continue;
  1983.     fread(&V.a, 1, 1, bitmap);
  1984.     V.b = 257 - V.b;
  1985.     while (V.b > 0) {
  1986.       b3[gbufp] = V.a;
  1987.       gbufp++;
  1988.       V.b--;
  1989.     }
  1990.       }
  1991.     }  /*:47*/
  1992.     if (V.bps == 4) {   /*48:*/
  1993.       gi = 0;
  1994.       FORLIM1 = V.hh;
  1995.       for (gbufpp = 0; gbufpp < FORLIM1; gbufpp++)
  1996.     b2[gbufpp] = 0;
  1997.       V.a = 0;
  1998.       FORLIM1 = V.hh;
  1999.       for (gbufpp = 0; gbufpp < FORLIM1; gbufpp++) {
  2000.     V.b = b3[gbufpp];
  2001.     for (j = 0; j <= 3; j++) {
  2002.       cb = V.b & 128;
  2003.       cb >>= V.a;
  2004.       b2[gi] ^= cb;
  2005.       V.b <<= 1;
  2006.       cb = V.b & 128;
  2007.       cb >>= V.a + 4;
  2008.       b2[gi] ^= cb;
  2009.       V.b <<= 1;
  2010.       gi++;
  2011.     }
  2012.     if (gi >= V.hh) {
  2013.       gi = 0;
  2014.       V.a++;
  2015.     }
  2016.       }
  2017.       FORLIM1 = V.hh;
  2018.       for (gbufpp = 0; gbufpp < FORLIM1; gbufpp++)
  2019.     b3[gbufpp] = b2[gbufpp];
  2020.       gi = 0;
  2021.       FORLIM1 = V.hh;
  2022.       for (gbufpp = 0; gbufpp < FORLIM1; gbufpp++) {
  2023.     b2[gi] = (V.hell[b3[gbufpp] >> 4] << 4) +
  2024.          V.hell[b3[gbufpp] & 15];
  2025.     gi++;
  2026.       }  /*:48*/
  2027.     } else if (V.bps == 8) {
  2028.       FORLIM1 = V.hh;
  2029.       for (gbufp = 0; gbufp < FORLIM1; gbufp++)
  2030.     b2[gbufp] = V.hell[b3[gbufp]];
  2031.     } else {
  2032.       FORLIM1 = V.hh;
  2033.       for (gbufp = 0; gbufp < FORLIM1; gbufp++)
  2034.     b2[gbufp] = b3[gbufp];
  2035.     }
  2036.     _SETIO(fwrite(b2, V.hh, 1, tmpfil) == 1, FileWriteError);
  2037.     filestat = P_ioresult;
  2038.     if (filestat != 0) {
  2039.       _SETIO(printf("Error occured when writing file %s!\n",
  2040.             tmpname) >= 0, FileWriteError);
  2041.       fileproblem();
  2042.       _Escape(0);
  2043.     }
  2044.   }
  2045.   if (bitmap != NULL)
  2046.     fclose(bitmap);
  2047.   bitmap = NULL;
  2048.   if (tmpfil != NULL)
  2049.     fclose(tmpfil);
  2050.   tmpfil = NULL;
  2051.   mapinlen = V.hh;
  2052.   bitspsam = V.bps;
  2053.   posbit = 0;
  2054.   strcpy(bitmap_NAME, "pcxtemp.tmp");
  2055.   if (bitmap != NULL)
  2056.     bitmap = freopen(bitmap_NAME, "rb", bitmap);
  2057.   else
  2058.     bitmap = fopen(bitmap_NAME, "rb");
  2059.   if (bitmap == NULL)
  2060.     _EscIO(FileNotFound);
  2061.   pcxtemp = true;
  2062.   if (egastretch) {
  2063.     if (xratio + 1 == yratio)
  2064.       picstretch();
  2065.   }
  2066.   posbit = 0;
  2067.   if (bitspsam > 1)
  2068.     greypxl = true;
  2069.   mapdiv = 1.0;
  2070.   FORLIM = bitspsam;
  2071.   for (i = 1; i <= FORLIM; i++)
  2072.     mapdiv *= 2;
  2073.   mapdiv /= 16;
  2074. }  /*:46*/
  2075.  
  2076.  
  2077. /*49:*/
  2078. Static void bmpint(void)
  2079. {
  2080.   unsigned short i, k, hi, hh, hhl, vi, vh, compr, lcol;
  2081.   uchar j, b, a, col, bps;
  2082.   uchar gpal[256][4];
  2083.   uchar hell[256];
  2084.   Char bmstr[256];
  2085.   long bmsize, bmoff;
  2086.   unsigned short FORLIM;
  2087.  
  2088.   printf("Analysing BMP file %s\n", bmname);
  2089.   opentempfile();
  2090.   fread(bmstr, 2, 1, bitmap);
  2091.   bmstr[2] = '\0';
  2092.   if (strcmp(bmstr, "BM")) {
  2093.     printf("No windows bitmap %s\n", bmstr);
  2094.     _Escape(0);
  2095.   }
  2096.   gbufp = 640;
  2097.   pxlinlen = 640;
  2098.   bmsize = longpcx();
  2099.   for (i = 1; i <= 4; i++)
  2100.     b = getpcx();
  2101.   bmoff = longpcx();
  2102.   bmsize = longpcx();
  2103.   bmoff -= bmsize;
  2104.   if (bmsize > 12) {
  2105.     hi = longpcx();
  2106.     vi = longpcx();
  2107.     b = getpcx();
  2108.     b = getpcx();
  2109.     bps = wordpcx();
  2110.     if (bps > 8) {
  2111.       printf("only up to 256 colors supported\n");
  2112.       _Escape(0);
  2113.     }
  2114.     hh = hi;
  2115.     vh = vi;
  2116.     compr = longpcx();
  2117.     for (i = 1; i <= 4; i++)
  2118.       b = getpcx();
  2119.     xratio = longpcx();
  2120.     yratio = longpcx();
  2121.     lcol = longpcx();
  2122.     lcol--;
  2123.     if (col == 0) {
  2124.       if (bps == 1)
  2125.     col = 1;
  2126.       else if (bps == 4)
  2127.     col = 15;
  2128.       else
  2129.     col = 255;
  2130.     }
  2131.     col = longpcx();
  2132.     col--;
  2133.   } else {
  2134.     hi = wordpcx();
  2135.     vi = wordpcx();
  2136.     b = getpcx();
  2137.     b = getpcx();
  2138.     bps = wordpcx();
  2139.     if (bps > 8) {
  2140.       printf("only up to 256 colors supported\n");
  2141.       _Escape(0);
  2142.     }
  2143.     hh = hi;
  2144.     vh = vi;
  2145.     compr = 0;
  2146.     xratio = 1;
  2147.     yratio = 1;
  2148.     col = 0;
  2149.   }
  2150.   if (lcol == 0) {
  2151.     if (bps == 1)
  2152.       col = 1;
  2153.     else if (bps == 4)
  2154.       col = 15;
  2155.     else
  2156.       col = 255;
  2157.   }
  2158.   if (bps == 1)
  2159.     col = 1;
  2160.   else if (bps == 4)
  2161.     col = 15;
  2162.   else
  2163.     col = 255;
  2164.   for (i = 0; i <= col; i++) {
  2165.     if (bmsize > 12) {
  2166.       for (j = 0; j <= 3; j++)
  2167.     gpal[i][j] = getpcx();
  2168.     } else {
  2169.       for (j = 0; j <= 2; j++)
  2170.     gpal[i][j] = getpcx();
  2171.     }
  2172.     hell[i] = (long)((gpal[i][0] * 0.287 + gpal[i]
  2173.                        [1] * 0.589 + gpal[i]
  2174.             [2] * 0.114) / (256.0 / (col + 1)));
  2175.   }
  2176.   if (bmsize > 12)
  2177.     bmoff -= (col + 1) * 4;
  2178.   else
  2179.     bmoff -= (col + 1) * 3;
  2180.   if (compr != 0) {
  2181.     printf("compression not supported yet\n");
  2182.     _Escape(0);
  2183.   }
  2184.   hh = (hh * bps + 7) / 8;
  2185.   hhl = (hh + 3) / 4 * 4 - 1;
  2186.   a = 8 - ((hi * bps) & 7);
  2187.   memset(b3, 0, hhl + 1);
  2188.   for (i = 1; i <= vh; i++) {  /*$I-*/
  2189.     _SETIO(fwrite(b3, hh, 1, tmpfil) == 1, FileWriteError);
  2190.     filestat = P_ioresult;
  2191.     if (filestat != 0) {
  2192.       _SETIO(printf("Error occured when writing file %s!\n",
  2193.             tmpname) >= 0, FileWriteError);
  2194.       fileproblem();
  2195.       _Escape(0);
  2196.     }
  2197.   }
  2198.   if (tmpfil != NULL)
  2199.     fclose(tmpfil);
  2200.   tmpfil = NULL;
  2201.   if (tmpfil != NULL)
  2202.     tmpfil = freopen(tmpfil_NAME, "wb", tmpfil);
  2203.   else
  2204.     tmpfil = fopen(tmpfil_NAME, "wb");
  2205.   if (tmpfil == NULL)
  2206.     _EscIO(FileNotFound);
  2207.   bmoff = (vh - 1) * (long)hh;
  2208.   for (i = 0; i < vh; i++) {
  2209.     for (k = 0; k <= hhl; k++)
  2210.       b3[k] = getpcx();
  2211.     if (bps == 4) {
  2212.       gi = 0;
  2213.       for (k = 0; k < hh; k++) {
  2214.     b2[gi] = (hell[b3[k] / 16] << 4) + hell[b3[k] & 15];
  2215.     gi++;
  2216.       }
  2217.     } else if (bps == 8) {
  2218.       for (k = 0; k < hh; k++)
  2219.     b2[k] = hell[b3[k]];
  2220.     } else {
  2221.       for (k = 0; k < hh; k++)
  2222.     b2[k] = b3[k];
  2223.     }
  2224.     if (a < 8) {
  2225.       for (k = 0; k < a; k++)
  2226.     b2[hh - 1] ^= invbit >> k;
  2227.     }
  2228.     fseek(tmpfil, bmoff, SEEK_SET);
  2229.     bmoff = bmoff - (long)hh;
  2230.     _SETIO(fwrite(b2, hh, 1, tmpfil) == 1, FileWriteError);
  2231.     filestat = P_ioresult;
  2232.     if (filestat != 0) {
  2233.       _SETIO(printf("Error occured when writing file %s!\n",
  2234.             tmpname) >= 0, FileWriteError);
  2235.       fileproblem();
  2236.       _Escape(0);
  2237.     }
  2238.   }
  2239.   if (bitmap != NULL)
  2240.     fclose(bitmap);
  2241.   bitmap = NULL;
  2242.   if (tmpfil != NULL)
  2243.     fclose(tmpfil);
  2244.   tmpfil = NULL;
  2245.   mapinlen = hh;
  2246.   posbit = 0;
  2247.   strcpy(bitmap_NAME, "pcxtemp.tmp");
  2248.   if (bitmap != NULL)
  2249.     bitmap = freopen(bitmap_NAME, "rb", bitmap);
  2250.   else
  2251.     bitmap = fopen(bitmap_NAME, "rb");
  2252.   if (bitmap == NULL)
  2253.     _EscIO(FileNotFound);
  2254.   pcxtemp = true;
  2255.   bitspsam = bps;
  2256.   if (bitspsam > 1)
  2257.     greypxl = true;
  2258.   mapdiv = 1.0;
  2259.   FORLIM = bitspsam;
  2260.   for (i = 1; i <= FORLIM; i++)
  2261.     mapdiv *= 2;
  2262.   mapdiv /= 16;
  2263. }
  2264.  
  2265.  
  2266. /*50:*/
  2267. Static void imgint(void)
  2268. {
  2269.   unsigned short i, ki, hi, hh, bzf, imgpat;
  2270.   short vh;
  2271.   uchar mi, cb, j, b, a, col, bps;
  2272.   uchar hell[16];
  2273.   uchar linerep;
  2274.   unsigned short FORLIM;
  2275.  
  2276.   printf("Analysing IMG file %s\n", bmname);
  2277.   invert = !invert;
  2278.   if (invert) {   /*51:*/
  2279.     invbit = 0;
  2280.     invbyte = 0;
  2281.   } else {
  2282.     invbit = 1;
  2283.     invbyte = 255;
  2284.   }
  2285.   hell[0] = 0;
  2286.   hell[1] = 1;
  2287.   hell[2] = 6;
  2288.   hell[3] = 8;
  2289.   hell[4] = 3;
  2290.   hell[5] = 2;
  2291.   hell[6] = 7;
  2292.   hell[7] = 11;
  2293.   hell[8] = 4;
  2294.   hell[9] = 5;
  2295.   hell[10] = 12;
  2296.   hell[11] = 13;
  2297.   hell[12] = 10;
  2298.   hell[13] = 9;
  2299.   hell[14] = 14;
  2300.   hell[15] = 15;
  2301.   opentempfile();   /*:51*/
  2302.   gbufp = 640;
  2303.   pxlinlen = 640;
  2304.   a = getpcx();
  2305.   b = getpcx();
  2306.   if (a * 256 + b != 1) {   /*52:*/
  2307.     printf("File does not contain GEM Image File Format\n");
  2308.     _Escape(0);
  2309.   }
  2310.   b = getpcx();
  2311.   bzf = b * 256;
  2312.   b = getpcx();
  2313.   bzf += b;
  2314.   b = getpcx();
  2315.   bps = b * 256;
  2316.   b = getpcx();
  2317.   bps += b;
  2318.   b = getpcx();
  2319.   imgpat = b * 256;
  2320.   b = getpcx();
  2321.   imgpat += b;
  2322.   b = getpcx();
  2323.   xratio = b * 256;
  2324.   b = getpcx();
  2325.   xratio += b;
  2326.   b = getpcx();
  2327.   yratio = b * 256;
  2328.   b = getpcx();
  2329.   yratio += b;
  2330.   if (xratio != yratio) {
  2331.     mapdiv = (double)yratio / xratio;
  2332.     xratio = 0;
  2333.     yratio = 0;
  2334.     while (xratio == yratio) {
  2335.       xratio++;
  2336.       yratio = (long)floor(xratio * mapdiv + 0.5);
  2337.     }
  2338.   }
  2339.   b = getpcx();
  2340.   hi = b * 256;
  2341.   b = getpcx();
  2342.   hi += b;
  2343.   b = getpcx();
  2344.   vh = b * 256;
  2345.   b = getpcx();
  2346.   vh += b;   /*:52*/
  2347.   hh = (hi * bps + 7) / 8;
  2348.   if (bzf > 8) {
  2349.     for (i = 1; i <= (bzf - 8) * 2; i++)
  2350.       b = getpcx();
  2351.   }
  2352.   linerep = 0;
  2353.   gi = 0;
  2354.   while (vh > 0) {
  2355.     for (gi = 0; gi <= hh; gi++)
  2356.       b3[gi] = 0;
  2357.     gi = 0;
  2358.     while (gi < hh) {
  2359.       b = getpcx();
  2360.       if (b != 0 && b != 128) {
  2361.     a = b & 127;
  2362.     cb = ((unsigned)(b - a)) >> 7;
  2363.     if (cb == 1)
  2364.       cb = 255;
  2365.     for (ki = 1; ki <= a; ki++) {
  2366.       b3[gi] = cb;
  2367.       gi++;
  2368.     }
  2369.     continue;
  2370.       }
  2371.       if (b == 128) {
  2372.     a = getpcx();
  2373.     for (ki = 1; ki <= a; ki++) {
  2374.       b3[gi] = getpcx();
  2375.       gi++;
  2376.     }
  2377.     continue;
  2378.       }
  2379.       a = getpcx();
  2380.       if (a <= 0) {
  2381.     b = getpcx();
  2382.     linerep = getpcx();
  2383.     continue;
  2384.       }
  2385.       for (ki = 1; ki <= imgpat; ki++)
  2386.     b2[ki] = getpcx();
  2387.       for (ki = 1; ki <= a; ki++) {
  2388.     for (mi = 1; mi <= imgpat; mi++) {
  2389.       b = b2[mi];
  2390.       b3[gi] = b;
  2391.       gi++;
  2392.     }
  2393.       }
  2394.     }
  2395.     if (bps == 4) {   /*48:*/
  2396.       gi = 0;
  2397.       for (gbufpp = 0; gbufpp < hh; gbufpp++)
  2398.     b2[gbufpp] = 0;
  2399.       a = 0;
  2400.       for (gbufpp = 0; gbufpp < hh; gbufpp++) {
  2401.     b = b3[gbufpp];
  2402.     for (j = 0; j <= 3; j++) {
  2403.       cb = b & 128;
  2404.       cb >>= a;
  2405.       b2[gi] ^= cb;
  2406.       b <<= 1;
  2407.       cb = b & 128;
  2408.       cb >>= a + 4;
  2409.       b2[gi] ^= cb;
  2410.       b <<= 1;
  2411.       gi++;
  2412.     }
  2413.     if (gi >= hh) {
  2414.       gi = 0;
  2415.       a++;
  2416.     }
  2417.       }
  2418.       for (gbufpp = 0; gbufpp < hh; gbufpp++)
  2419.     b3[gbufpp] = b2[gbufpp];
  2420.       gi = 0;
  2421.       for (gbufpp = 0; gbufpp < hh; gbufpp++) {
  2422.     b2[gi] = (hell[b3[gbufpp] >> 4] << 4) +
  2423.          hell[b3[gbufpp] & 15];
  2424.     gi++;
  2425.       }  /*:48*/
  2426.     } else {
  2427.       for (ki = 0; ki < hh; ki++)
  2428.     b2[ki] = b3[ki];
  2429.     }
  2430.     _SETIO(fwrite(b2, hh, 1, tmpfil) == 1, FileWriteError);
  2431.     filestat = P_ioresult;
  2432.     if (filestat != 0) {
  2433.       _SETIO(printf("Error occured when writing file %s!\n",
  2434.             tmpname) >= 0, FileWriteError);
  2435.       fileproblem();
  2436.       _Escape(0);
  2437.     }
  2438.     vh--;
  2439.     if (linerep <= 0)
  2440.       continue;
  2441.     for (i = 1; i <= linerep; i++) {  /*$I-*/
  2442.       _SETIO(fwrite(b2, hh, 1, tmpfil) == 1, FileWriteError);
  2443.       filestat = P_ioresult;
  2444.       if (filestat != 0) {
  2445.     _SETIO(printf("Error occured when writing file %s!\n",
  2446.               tmpname) >= 0, FileWriteError);
  2447.     fileproblem();
  2448.     _Escape(0);
  2449.       }
  2450.     }
  2451.     vh += 1 - linerep;
  2452.     linerep = 0;
  2453.   }
  2454.   if (bitmap != NULL)
  2455.     fclose(bitmap);
  2456.   bitmap = NULL;
  2457.   if (tmpfil != NULL)
  2458.     fclose(tmpfil);
  2459.   tmpfil = NULL;
  2460.   if (col == 4) {
  2461.     bitspsam = 4;
  2462.     greypxl = true;
  2463.   } else if (bps > 1) {
  2464.     bitspsam = bps;
  2465.     greypxl = true;
  2466.   }
  2467.   mapinlen = hh;
  2468.   posbit = 0;
  2469.   strcpy(bitmap_NAME, "pcxtemp.tmp");
  2470.   if (bitmap != NULL)
  2471.     bitmap = freopen(bitmap_NAME, "rb", bitmap);
  2472.   else
  2473.     bitmap = fopen(bitmap_NAME, "rb");
  2474.   if (bitmap == NULL)
  2475.     _EscIO(FileNotFound);
  2476.   if (egastretch) {
  2477.     if (xratio + 1 == yratio)
  2478.       picstretch();
  2479.   }
  2480.   posbit = 0;
  2481.   pcxtemp = true;
  2482.   mapdiv = 1.0;
  2483.   FORLIM = bitspsam;
  2484.   for (i = 1; i <= FORLIM; i++)
  2485.     mapdiv *= 2;
  2486.   mapdiv /= 16;
  2487. }
  2488.  
  2489.  
  2490. /*54:*/
  2491. Static void tfmhword(long hword)
  2492. {
  2493.   union {
  2494.     uchar b[4];
  2495.     long i;
  2496.   } ax;
  2497.   short i;
  2498.  
  2499.   ax.i = hword;
  2500. #ifdef INTEL
  2501.   for (i = 1; i >= 0; i--)
  2502.     fwrite(&ax.b[i], sizeof(uchar), 1, tfmfile);
  2503. #else
  2504.   for (i = 2; i <= 3; i++)
  2505.     fwrite(&ax.b[i], sizeof(uchar), 1, tfmfile);
  2506. #endif
  2507. }
  2508.  
  2509.  
  2510. Static void tfmword(long lword)
  2511. {
  2512.   union {
  2513.     uchar b[4];
  2514.     long i;
  2515.   } ax;
  2516.   short i;
  2517.  
  2518.   ax.i = lword;
  2519. #ifdef INTEL
  2520.   for (i = 3; i >= 0; i--)
  2521.     fwrite(&ax.b[i], sizeof(uchar), 1, tfmfile);
  2522. #else
  2523.   for (i = 0; i <= 3; i++)
  2524.     fwrite(&ax.b[i], sizeof(uchar), 1, tfmfile);
  2525. #endif
  2526. }
  2527.  
  2528.  
  2529. /*:54*/
  2530. /*57:*/
  2531. /*procedure pxlhword(hword:word);
  2532. var ax:record case boolean of true:(b:array[0..1]of ebts);
  2533. false:(w:word)end;i:integer;
  2534. [EMBED
  2535. #ifdef INTEL
  2536. ]
  2537. begin ax.w:=hword;pxlbm^[pxlbmi]:=ax.b[1];inc(pxlbmi);
  2538. pxlbm^[pxlbmi]:=ax.b[0];inc(pxlbmi);end;
  2539. [EMBED
  2540. #else
  2541. ]
  2542. begin ax.w:=hword;pxlbm^[pxlbmi]:=ax.b[0];inc(pxlbmi);
  2543. pxlbm^[pxlbmi]:=ax.b[1];inc(pxlbmi);end;
  2544. [EMBED
  2545. #endif
  2546. ]
  2547. */
  2548. Static void pxlmap(long lword)
  2549. {
  2550.   union {
  2551.     ebts b[4];
  2552.     long i;
  2553.   } ax;
  2554.   uchar i;
  2555.  
  2556.   ax.i = lword;
  2557.   for (i = 0; i <= 3; i++) {
  2558.     pxlbm[pxlbmi] = ax.b[i];
  2559.     pxlbmi++;
  2560.   }
  2561. }
  2562.  
  2563.  
  2564. /*procedure pxlword(lword:longint);
  2565. var ax:record case boolean of true:(b:array[0..3]of ebts);
  2566. false:(i:longint)end;i:byte;begin ax.i:=lword;
  2567. [EMBED
  2568. #ifdef INTEL
  2569. ]
  2570. for i:=3 downto 0 do begin pxlbm^[pxlbmi]:=ax.b[i];inc(pxlbmi);end;
  2571. [EMBED
  2572. #else
  2573. ]
  2574. for i:=0 to 3 do begin pxlbm^[pxlbmi]:=ax.b[i];inc(pxlbmi);end;
  2575. [EMBED
  2576. #endif
  2577. ]
  2578. end;*/
  2579. /*:57*/
  2580. /*61:*/
  2581. Static void pkbyte(uchar b)
  2582. {
  2583.   pkbm[pkloc] = b;
  2584.   pkloc++;
  2585. }
  2586.  
  2587.  
  2588. Static void pkhalfword(short a)
  2589. {
  2590.   union {
  2591.     ebts b[2];
  2592.     short w;
  2593.   } ax;
  2594.  
  2595.   ax.w = a;
  2596. #ifdef INTEL
  2597.   pkbyte(ax.b[1]);
  2598.   pkbyte(ax.b[0]);
  2599. #else
  2600.   pkbyte(ax.b[0]);
  2601.   pkbyte(ax.b[1]);
  2602. #endif
  2603. }
  2604.  
  2605.  
  2606. Static void pkthreebytes(long a)
  2607. {
  2608.   union {
  2609.     ebts b[4];
  2610.     long i;
  2611.   } ax;
  2612.  
  2613.   ax.i = a;
  2614. #ifdef INTEL
  2615.   pkbyte(ax.b[2]);
  2616.   pkbyte(ax.b[1]);
  2617.   pkbyte(ax.b[0]);
  2618. #else
  2619.   pkbyte(ax.b[1]);
  2620.   pkbyte(ax.b[2]);
  2621.   pkbyte(ax.b[3]);
  2622. #endif
  2623. }
  2624.  
  2625.  
  2626. Static void pkword(long a)
  2627. {
  2628.   union {
  2629.     ebts b[4];
  2630.     long i;
  2631.   } ax;
  2632.  
  2633.   ax.i = a;
  2634. #ifdef INTEL
  2635.   pkbyte(ax.b[3]);
  2636.   pkbyte(ax.b[2]);
  2637.   pkbyte(ax.b[1]);
  2638.   pkbyte(ax.b[0]);
  2639. #else
  2640.   pkbyte(ax.b[0]);
  2641.   pkbyte(ax.b[1]);
  2642.   pkbyte(ax.b[2]);
  2643.   pkbyte(ax.b[3]);
  2644. #endif
  2645. }
  2646.  
  2647.  
  2648. Static void pknyb(uchar a)
  2649. {
  2650.   if (bitweight == 16) {
  2651.     outputbyte = a << 4;
  2652.     bitweight = 1;
  2653.   } else {
  2654.     pkbyte(outputbyte + a);
  2655.     bitweight = 16;
  2656.   }
  2657. }
  2658.  
  2659.  
  2660. /*:61*/
  2661. /*63:*/
  2662. Static void writepreamble(void)
  2663. {
  2664.   short i;
  2665.   Char nstr[256];
  2666.  
  2667.   sprintf(nstr, "%ld", (long)floor(truehres + 0.5));
  2668.   sprintf(cmd, "%s%c.%spk", font, fontpre[f], nstr);
  2669.   strcpy(pxlfile_NAME, cmd);
  2670.   if (pxlfile != NULL)
  2671.     pxlfile = freopen(pxlfile_NAME, "wb", pxlfile);
  2672.   else
  2673.     pxlfile = fopen(pxlfile_NAME, "wb");
  2674.   _SETIO(pxlfile != NULL, FileNotFound);
  2675.   filestat = P_ioresult;
  2676.   if (filestat == 3 || filestat == 5) {
  2677.     strcpy(cmd, bmname);
  2678.     sprintf(cmd + strlen(cmd), "%c.pk", fontupc[f]);
  2679.     strcpy(pxlfile_NAME, cmd);
  2680.     if (pxlfile != NULL)
  2681.       pxlfile = freopen(pxlfile_NAME, "wb", pxlfile);
  2682.     else
  2683.       pxlfile = fopen(pxlfile_NAME, "wb");
  2684.     _SETIO(pxlfile != NULL, FileNotFound);
  2685.     filestat = P_ioresult;
  2686.   }
  2687.   if (filestat == 0)
  2688.     fok = false;
  2689.   else
  2690.     fok = true;
  2691.   if (fok) {
  2692.     printf("error occurred when ");
  2693.     printf("allocating pkfile ");
  2694.     puts(cmd);
  2695.     fileproblem();
  2696.     _Escape(0);
  2697.   }
  2698.   pkloc = 0;
  2699.   pkbyte(247);
  2700.   pkbyte(89);
  2701.   pkbyte(14);
  2702.   strcpy(comment, "BM2FONT output");
  2703.   for (i = 0; i <= 13; i++)
  2704.     pkbyte(comment[i]);
  2705.   pkword(tabdsize[f]);
  2706.   pkword(checksum);
  2707.   pkword((long)floor(truehres * 65536L / 72.27 + 0.5));
  2708.   pkword((long)floor(truevres * 65536L / 72.27 + 0.5));
  2709.   nextmemfree = 0;
  2710.   printf("Packing font %s\n", cmd);
  2711. }
  2712.  
  2713.  
  2714. /*:63*/
  2715. /*65:*/
  2716. Static void writepostamble(void)
  2717. {
  2718.   pkbyte(245);
  2719.   while ((pkloc & 3) != 0)
  2720.     pkbyte(246);
  2721.   _SETIO(fwrite(pkbm, pkloc, 1, pxlfile) == 1,
  2722.      FileWriteError);
  2723.   filestat = P_ioresult;
  2724.   if (filestat != 0) {
  2725.     _SETIO(printf("Error occured when writing font %s!\n",
  2726.           cmd) >= 0, FileWriteError);
  2727.     fileproblem();
  2728.     _Escape(0);
  2729.   }
  2730.   if (pxlfile != NULL)
  2731.     fclose(pxlfile);
  2732.   pxlfile = NULL;
  2733.   printf(" \n");
  2734.   printf("Font %s at %ld dots per inch written\n",
  2735.      cmd, (long)floor(truehres + 0.5));
  2736.   for (i = 0; i <= 128; i++)
  2737.     pkpoint[i] = 0;
  2738.   pxlbmi = 4;
  2739. }
  2740.  
  2741.  
  2742. /*:65*/
  2743. /*67:*/
  2744. Static boolean equal(pxlstr row1, pxlstr row2)
  2745. {
  2746.   short i;
  2747.   boolean temp;
  2748.  
  2749.   i = pkwidth;
  2750.   temp = true;
  2751.   while (i > 0 && temp) {
  2752.     if (pxlbm[row1] != pxlbm[row2])
  2753.       temp = false;
  2754.     row1++;
  2755.     row2++;
  2756.     i -= 8;
  2757.   }
  2758.   return temp;
  2759. }
  2760.  
  2761.  
  2762. /*:67*/
  2763. /*68:*/
  2764. Static boolean equalzo(pxlstr row1, short row2)
  2765. {
  2766.   short i;
  2767.   boolean temp;
  2768.  
  2769.   i = pkwidth;
  2770.   temp = true;
  2771.   while (i > 0 && temp) {
  2772.     if (pxlbm[row1] != (repbm[row2] & 255))
  2773.       temp = false;
  2774.     row1++;
  2775.     row2++;
  2776.     i -= 8;
  2777.   }
  2778.   return temp;
  2779. }
  2780.  
  2781.  
  2782. /*:68*/
  2783. /*70:*/
  2784. Static void shipcharacter(void)
  2785. {   /*:87*/
  2786.   pxlstr crst;
  2787.   short wwid;
  2788.   long compsize;
  2789.   unsigned short horesc;
  2790.   /*72:*/
  2791.   short l;
  2792.   long i, j, k;
  2793.   short rowzero, onesrow, repptr, bitcounts;
  2794.   ebts fillbytes, longind, mixbyte, restbyte, pkshift,
  2795.        dshift;
  2796.   long longtab[20];
  2797.   /*:72*/
  2798.   /*77:*/
  2799.   long count;
  2800.   short test, curptr, bit, repflag;
  2801.   schar pkkword;
  2802.   short bitptr, bitmod32, currepeat;
  2803.   pxlstr endraster;   /*:77*/
  2804.   /*80:*/
  2805.   short dynf;
  2806.   long deriv[13];
  2807.   long bcompsize;
  2808.   boolean firston;
  2809.   short flagbyte;
  2810.   /*:80*/
  2811.   /*87:*/
  2812.   unsigned short max2;
  2813.   pxlstr predpkloc;
  2814.   short bytelength;
  2815.  
  2816.   printf("%c ", car);
  2817.   horesc = pwidth[f][car];
  2818.   pkwidth = pwidth[f][car];
  2819.   pkheight = pheight[f][car];
  2820.   pkheight -= yoffset[f][car];
  2821.   cxoff = 0;
  2822.   cyoff = pkheight;
  2823.   crst = pkpoint[car];
  2824.   wwid = (pkwidth + 31) / 32 * 4;
  2825.   fillbytes = wwid - (pkwidth + 7) / 8;   /*71:*/
  2826.   rowzero = nextmemfree;
  2827.   onesrow = nextmemfree + wwid;
  2828.   repptr = onesrow + wwid;
  2829.   bitcounts = repptr + pkheight + 1;
  2830.   for (i = rowzero; i < onesrow; i++)
  2831.     repbm[i] = 0;
  2832.   for (i = onesrow; i <= repptr - 2; i++)
  2833.     repbm[i] = -1;
  2834.   j = onesrow + (((unsigned long)(pkwidth + 7)) >> 3);
  2835.   repbm[j - 1] = power[8] - power[7 - ((pkwidth + 7) & 7)];
  2836.   for (i = j; i < repptr; i++)
  2837.     repbm[i] = 0;
  2838.   i = 0;
  2839.   j = pkheight;
  2840.   while (i < j) {
  2841.     if (equalzo(i * wwid + crst, rowzero))
  2842.       repbm[repptr + i] = 0;
  2843.     else if (equalzo(i * wwid + crst, onesrow))
  2844.       repbm[repptr + i] = 0;
  2845.     else if (i + 1 == j)
  2846.       repbm[repptr + i] = 0;
  2847.     else if (equal(i * wwid + crst, (i + 1) * wwid + crst))
  2848.       repbm[repptr + i] = 1;
  2849.     else
  2850.       repbm[repptr + i] = 0;
  2851.     i++;
  2852.   }
  2853.   i = 0;
  2854.   while (i < j) {
  2855.     k = i;
  2856.     while (repbm[repptr + k] == 1)
  2857.       k++;
  2858.     repbm[repptr + i] = k - i;
  2859.     i = k + 1;
  2860.   }
  2861.   repbm[repptr + i] = 0;   /*:71*/
  2862.   /*75:*/
  2863.   repflag = 0;
  2864.   bitptr = pkwidth - 1;
  2865.   currepeat = repptr;
  2866.   endraster = crst + pkheight * wwid;
  2867.   curptr = bitcounts;
  2868.   count = 0;
  2869.   longind = 0;
  2870.   test = 0;
  2871.   do {   /*76:*/
  2872.     bitptr++;
  2873.     if (bitptr == pkwidth) {
  2874.       bitmod32 = 0;
  2875.       bitptr = 0;
  2876.       if (currepeat > repptr)
  2877.     crst += fillbytes;
  2878.       if (repbm[currepeat] > 0) {
  2879.     repflag = repbm[currepeat];
  2880.     currepeat += repflag;
  2881.     crst += wwid * repflag;
  2882.       }
  2883.       currepeat++;
  2884.     }
  2885.     bitmod32--;
  2886.     if (bitmod32 == -1) {
  2887.       bitmod32 = 7;
  2888.       pkkword = pxlbm[crst];
  2889.       crst++;
  2890.     }
  2891.     if (crst > endraster)
  2892.       bit = 2;
  2893.     else if (pkkword < 0)
  2894.       bit = 1;
  2895.     else
  2896.       bit = 0;
  2897.     pkkword <<= 1;   /*:76*/
  2898.     if (bit == test)
  2899.       count++;
  2900.     else {
  2901.       if (count > 32767) {
  2902.     longind++;
  2903.     longtab[longind - 1] = count;
  2904.     count = -longind - 10000;
  2905.       }
  2906.       repbm[curptr] = count;
  2907.       curptr++;
  2908.       if (curptr + 1 > maxmemsize)
  2909.     test = 3;
  2910.       else {
  2911.     count = 1;
  2912.     test = bit;
  2913.     if (repflag > 0) {
  2914.       repbm[curptr] = -repflag;
  2915.       repflag = 0;
  2916.       curptr++;
  2917.     }
  2918.       }
  2919.     }
  2920.   } while (test < 2);
  2921.   repbm[curptr] = 0;
  2922.   repbm[curptr + 1] = 0;   /*:75*/
  2923.   /*78:*/
  2924.   if (test < 3) {   /*81:*/
  2925.     for (i = 0; i <= 12; i++)
  2926.       deriv[i] = 0;
  2927.     firston = (repbm[bitcounts] == 0);
  2928.     if (firston)
  2929.       bitcounts++;
  2930.     i = bitcounts;
  2931.     compsize = 0;
  2932.     while (repbm[i] != 0) {   /*79:*/
  2933.       j = repbm[i];
  2934.       if (j < -10000)
  2935.     j = longtab[-j - 10001];
  2936.       if (j == -1)
  2937.     compsize++;
  2938.       else {
  2939.     if (j < 0) {
  2940.       compsize++;
  2941.       j = -j;
  2942.     }
  2943.     if (j < 209)
  2944.       compsize += 2;
  2945.     else {
  2946.       k = j - 193;
  2947.       while (k >= 16) {
  2948.         k = ((unsigned long)k) >> 4;
  2949.         compsize += 2;
  2950.       }
  2951.       compsize++;
  2952.     }
  2953.     if (j < 14)
  2954.       deriv[j - 1]--;
  2955.     else if (j < 209)
  2956.       deriv[(223 - j) / 15 - 1]++;
  2957.     else {
  2958.       k = 16;
  2959.       while (k <= ((unsigned long)(j + 2)) >> 4) {
  2960.         k <<= 4;
  2961.         if (k == 0) {
  2962.           k = 32767;
  2963.           j += 32767;
  2964.         }
  2965.       }
  2966.       if (j - k <= 192)
  2967.         deriv[(k - j + 207) / 15 - 1] += 2;
  2968.     }
  2969.       }
  2970.       i++;
  2971.     }
  2972.     /*:79*/
  2973.     bcompsize = compsize;
  2974.     dynf = 0;
  2975.     for (i = 1; i <= 13; i++) {
  2976.       compsize += deriv[i - 1];
  2977.       if (compsize <= bcompsize) {
  2978.     bcompsize = compsize;
  2979.     dynf = i;
  2980.       }
  2981.     }
  2982.     compsize = (bcompsize + 1) / 2;
  2983.     if (compsize > (pkheight * pkwidth + 7) / 8 ||
  2984.     pkheight * pkwidth == 0) {
  2985.       compsize = (pkheight * pkwidth + 7) / 8;
  2986.       dynf = 14;
  2987.     }
  2988.   } else {
  2989.     dynf = 14;
  2990.     firston = (repbm[bitcounts] == 0);
  2991.     compsize = (pkheight * pkwidth + 7) / 8;
  2992.   }
  2993.   flagbyte = dynf * 16;
  2994.   if (firston)
  2995.     flagbyte += 8;
  2996.   if (width[f][car] > 16777215L || width[f][car] < 0 ||
  2997.       horesc < 0 || compsize > 196594L ||
  2998.       pkwidth > 65535L || pkheight > 65535L ||
  2999.       cxoff > 32767 || cyoff > 32767 || cxoff < -32768L ||
  3000.       cyoff < -32768L)
  3001.   {   /*82:*/
  3002.     flagbyte += 7;
  3003.     pkbyte(flagbyte);
  3004.     compsize += 28;
  3005.     pkword(compsize);
  3006.     pkword((long)car);
  3007.     predpkloc = pkloc + compsize;
  3008.     pkword(width[f][car]);
  3009.     pkword((long)horesc);
  3010.     pkword(0);
  3011.     pkword(pkwidth);
  3012.     pkword(pkheight);
  3013.     pkword(cxoff);
  3014.     pkword(cyoff);
  3015.   }  /*:82*/
  3016.   else if (horesc > 255 || pkwidth > 255 ||
  3017.        pkheight > 255 || cxoff > 127 || cyoff > 127 ||
  3018.        cxoff < -128 || cyoff < -128 ||
  3019.        compsize > 1015) {
  3020.     compsize += 13;
  3021.     flagbyte += compsize / 65536L + 4;
  3022.     pkbyte(flagbyte);
  3023. /* p2c: bm2font.p, line 824:
  3024.  * Note: Using % for possibly-negative arguments [317] */
  3025.     pkhalfword(compsize % 65536L);
  3026.     pkbyte(car);
  3027.     predpkloc = pkloc + compsize;
  3028.     pkthreebytes(width[f][car]);
  3029.     pkhalfword(horesc);
  3030.     pkhalfword(pkwidth);
  3031.     pkhalfword(pkheight);
  3032.     pkhalfword(cxoff);
  3033.     pkhalfword(cyoff);
  3034.   } else {
  3035.     compsize += 8;
  3036.     flagbyte += compsize / 256;
  3037.     pkbyte(flagbyte);
  3038.     pkbyte(compsize & 255);
  3039.     pkbyte(car);
  3040.     predpkloc = pkloc + compsize;
  3041.     pkthreebytes(width[f][car]);
  3042.     pkbyte(horesc & 255);
  3043.     pkbyte(pkwidth);
  3044.     pkbyte(pkheight);
  3045.     pkbyte(cxoff);
  3046.     pkbyte(cyoff);
  3047.   }
  3048.   if (dynf != 14) {   /*85:*/
  3049.     bitweight = 16;
  3050.     max2 = 208 - dynf * 15;
  3051.     i = bitcounts;
  3052.     while (repbm[i] != 0) {
  3053.       j = repbm[i];
  3054.       if (j < -10000)
  3055.     j = longtab[-j - 10001];
  3056.       if (j == -1)
  3057.     pknyb(15);
  3058.       else {
  3059.     if (j < 0) {
  3060.       pknyb(14);
  3061.       j = -j;
  3062.     }
  3063.     if (j <= dynf)
  3064.       pknyb(j);
  3065.     else if (j <= max2) {
  3066.       j += -dynf - 1;
  3067.       pknyb((((unsigned long)j) >> 4) + dynf + 1);
  3068.       pknyb(j & 15);
  3069.     } else {
  3070.       j += 15 - max2;
  3071.       k = 1;
  3072.       while (k <= ((unsigned long)j) >> 4) {
  3073.         k <<= 4;
  3074.         pknyb(0);
  3075.       }
  3076.       while (k > 0) {
  3077.         pknyb(j / k);
  3078.         j %= k;
  3079. /* p2c: bm2font.p, line 839:
  3080.  * Note: Using % for possibly-negative arguments [317] */
  3081.         k = ((unsigned long)k) >> 4;
  3082.       }
  3083.     }
  3084.       }
  3085.       i++;
  3086.     }
  3087.     if (bitweight != 16)
  3088.       pkbyte(outputbyte);
  3089.   } else  /*86:*/
  3090.   {   /*:86*/
  3091.     crst = pkpoint[car];
  3092.     bytelength = (((unsigned long)(pkwidth + 7)) >> 3) - 1;
  3093.     if ((pkwidth & 7) == 0) {
  3094.       while (crst < endraster) {
  3095.     for (i = 0; i <= bytelength; i++)
  3096.       pkbyte(pxlbm[crst + i]);
  3097.     crst += wwid;
  3098.       }
  3099.     } else {
  3100.       pkshift = 0;
  3101.       dshift = pkwidth & 7;
  3102.       while (crst < endraster) {
  3103.     if (pkshift == 0) {
  3104.       for (i = 0; i < bytelength; i++)
  3105.         pkbyte(pxlbm[crst + i]);
  3106.       mixbyte = pxlbm[crst + bytelength];
  3107.     } else {
  3108.       i = crst;
  3109.       l = pkwidth + pkshift;
  3110.       while (l >= 8) {
  3111.         l -= 8;
  3112.         restbyte = pxlbm[i];
  3113.         i++;
  3114.         pkbyte(mixbyte + (restbyte >> pkshift));
  3115.         mixbyte = restbyte << (8 - pkshift);
  3116.       }
  3117.       if (pkshift < ((pkshift + dshift) & 7))
  3118.         mixbyte += pxlbm[i] >> pkshift;
  3119.     }
  3120.     crst += wwid;
  3121.     pkshift = (pkshift + dshift) & 7;
  3122.       }
  3123.       if (pkshift > 0)
  3124.     pkbyte(mixbyte);
  3125.     }
  3126.   }
  3127.   /*:85*/
  3128.   if (predpkloc == pkloc)   /*:78*/
  3129.     return;
  3130.   printf("Bad predicted character length: character %d\n",
  3131.      car);
  3132.   printf("pred: %uloc: %u\n", predpkloc, pkloc);
  3133.   _Escape(0);
  3134.  
  3135.   /*84:*/
  3136.   /*:84*/
  3137.   /*83:*/
  3138.   /*:83*/
  3139.   /*:81*/
  3140. }  /*:70*/
  3141.  
  3142.  
  3143. /*88:*/
  3144. /*procedure prepxlfile;
  3145. cmd:=concat(font,fontpre[f+1],'.',numtostr(5*truehres),'pxl');
  3146. assign(pxlfile,cmd);[$I-]rewrite(pxlfile,1);filestat:=ioresult;
  3147. if filestat=3 then begin cmd:=bmname;
  3148. cmd:=concat(cmd,fontupc[f+1],'.pxl');assign(pxlfile,cmd);
  3149. rewrite(pxlfile,1);filestat:=ioresult;end;
  3150. if filestat=0 then fok:=false else fok:=true;
  3151. [$I+]if not fok then begin pxlbmi:=0;pxlword(1001);end;end;*/
  3152. /*:88*/
  3153. /*89:*/
  3154. /*procedure restofpxl(fi:integer);var i:integer;ib:pxlstr;begin i:=0;
  3155. ib:=0;
  3156. for i:=0 to 127 do begin pheight^[fi,i]:=pheight^[fi,i]-yoffset^[fi,i];
  3157. yoffset^[fi,i]:=pheight^[fi,i];pxlhword(pwidth^[fi,i]);
  3158. pxlhword(pheight^[fi,i]);pxlhword(0);pxlhword(yoffset^[fi,i]);
  3159. pxlword(dirpoint[i]);pxlword(width^[fi,i]);end;pxlword(checksum);
  3160. pxlword(1000);pxlword(tabdsize[fi]);pxlword((pxlbmi div 4)-515);
  3161. pxlword(1001);begin[$I-]blockwrite(pxlfile,pxlbm^[0],pxlbmi);
  3162. filestat:=ioresult;
  3163. if filestat<>0 then begin writeln('Error occured when writing font ',cmd
  3164. ,'!');fileproblem;halt;end;[$I+]end;close(pxlfile);
  3165. writeln('Font ',cmd,' at ',round(truehres):1,' dots per inch written');
  3166. for i:=0 to 128 do dirpoint[i]:=0;for i:=0 to 128 do pkpoint[i]:=0;end;*/
  3167. /*:89*/
  3168. /*90:*/
  3169. Static void fonteject(void)
  3170. {
  3171.   short FORLIM, FORLIM1, FORLIM2;
  3172.  
  3173.   while (maplines > 0) {
  3174.     FORLIM = maxx;
  3175.     for (k = 1; k <= FORLIM; k++) {
  3176.       i = k - 1;
  3177.       dirpoint[cref[g]] = pxlbmi / 4;
  3178.       pkpoint[cref[g]] = pxlbmi;
  3179.       linepos_ = posbit + i * hres / 8;
  3180.       pxlinlen = (pwidth[f][cref[g]] + 7) / 8;
  3181.       zeroline = true;
  3182.       gi = 0;
  3183.       FORLIM1 = pheight[f][cref[g]];
  3184.       for (v = 1; v <= FORLIM1; v++) {
  3185.     fseek(bitmap, linepos_, SEEK_SET);
  3186.     j = fread(b1, 1, pxlinlen, bitmap);
  3187.     FORLIM2 = pxlinlen;
  3188.     for (lx = 0; lx < FORLIM2; lx++)
  3189.       b2[lx] = b1[lx];
  3190.     if (invert) {
  3191.       FORLIM2 = pxlinlen;
  3192.       for (lx = 0; lx < FORLIM2; lx++)
  3193.         b2[lx] = 255 - b1[lx];
  3194.     }
  3195.     gi += dv;
  3196.     if (gi >= lv)
  3197.       gi -= cv;
  3198.     if (invbyte == 255) {
  3199.       FORLIM2 = pxlinlen;
  3200.       for (lx = 0; lx < FORLIM2; lx++)
  3201.         b1[lx] = invbyte - b1[lx];
  3202.     }
  3203.     lx = 0;
  3204.     zerorow = true;
  3205.     FORLIM2 = pxlinlen;
  3206.     for (j = 0; j < FORLIM2; j++) {
  3207.       if (b1[j] != 0) {
  3208.         zerorow = false;
  3209.         zeroline = false;
  3210.       }
  3211.     }
  3212.     if (zeroline) {
  3213.       yoffset[f][cref[g]]++;
  3214.       if (yoffset[f][cref[g]] == pheight[f][cref[g]]) {
  3215.         yoffset[f][cref[g]]--;
  3216.         zerorow = false;
  3217.         zeroline = false;
  3218.       }
  3219.     }
  3220.     cx.i = 0;
  3221.     if (!zeroline) {
  3222.       FORLIM2 = pxlinlen;
  3223.       for (j = 0; j < FORLIM2; j++) {
  3224.         if (lx > 3) {
  3225.           lx = 0;
  3226.           pxlmap(cx.i);
  3227.           cx.i = 0;
  3228.         }
  3229.         cx.b[lx] = b1[j];
  3230.         lx++;
  3231.       }
  3232.       pxlmap(cx.i);
  3233.     }
  3234.     linepos_ += (mapwidth + 7) / 8 + zeropads;
  3235.       }
  3236.       i += maxx;
  3237.       g++;
  3238.       if (g == charpf) {   /*91:*/
  3239.     writepreamble();
  3240.     for (car = 0; car <= 127; car++) {
  3241.       if (pkpoint[car] != 0)
  3242.         shipcharacter();
  3243.     }
  3244.     writepostamble();
  3245.     f++;
  3246.     g = 0;
  3247.     /*if f<noffonts then if pxlmod then begin prepxlfile;
  3248. if fok then begin write('error occurred when ');
  3249. write('allocating pxlfile ');writeln(cmd);fileproblem;halt;end;end;*/
  3250.       }
  3251.       /*if pxlmod then restofpxl(f)else*/
  3252.       /*:91*/
  3253.     }
  3254.     maplines -= vres;
  3255.     posbit += vres * ((mapwidth + 7) / 8 + zeropads);
  3256.   }
  3257.   if (bitmap != NULL)
  3258.     fclose(bitmap);
  3259.   bitmap = NULL;   /*92:*/
  3260.   if (f >= noffonts)   /*if pxlmod then restofpxl(f)else*/
  3261.     return;
  3262.   /*:92*/
  3263.   writepreamble();
  3264.   for (car = 0; car <= 127; car++) {
  3265.     if (pkpoint[car] != 0)
  3266.       shipcharacter();
  3267.   }
  3268.   writepostamble();
  3269. }
  3270.  
  3271.  
  3272. /*:90*/
  3273. /*105:*/
  3274. main(int argc, Char *argv[])
  3275. {
  3276.   short FORLIM;
  3277.   Char STR1[256];
  3278.   uchar FORLIM1;
  3279.   short FORLIM2, FORLIM3;
  3280.   uchar FORLIM4;
  3281.  
  3282.   PASCAL_MAIN(argc, argv);
  3283.   tfmfile = NULL;
  3284.   tmpfil = NULL;
  3285.   pxlfile = NULL;
  3286.   bitmap = NULL;
  3287.   texfile = NULL;
  3288.   power[0] = 1;
  3289.   for (i = 1; i <= 8; i++)
  3290.     power[i] = power[i - 1] * 2;
  3291.   pxlbm = Malloc(sizeof(pxla));
  3292.   pkbm = Malloc(sizeof(pxla));
  3293.   repbm = Malloc(sizeof(pxlba));
  3294.   gkorh = Malloc(sizeof(korbyte));
  3295.   gkordivp = Malloc(sizeof(korbyte));
  3296.   spreadvals = Malloc(sizeof(spreadbyte));
  3297.   width = Malloc(sizeof(dynlong));
  3298.   height = Malloc(sizeof(dynlong));
  3299.   yoffset = Malloc(sizeof(dynword));
  3300.   pwidth = Malloc(sizeof(dynword));
  3301.   pheight = Malloc(sizeof(dynword));
  3302.   greymem = Malloc(sizeof(pxla));   /*106:*/
  3303.   strcpy(cref, "!()+,-./0123456789:;<=>?");
  3304.   strcat(cref, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  3305. /* p2c: bm2font.p, line 915:
  3306.  * Note: Possible string truncation in assignment [145] */
  3307.   strcat(cref, "abcdefghijklmnopqrstuvwxyz");
  3308. /* p2c: bm2font.p, line 916:
  3309.  * Note: Possible string truncation in assignment [145] */
  3310.   strcpy(fontupc, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  3311.   strcpy(fontpre, "abcdefghijklmnopqrstuvwxyz");
  3312.   truehres = 0.0;
  3313.   truevres = 0.0;
  3314.   invert = false;
  3315.   greypxl = false;
  3316.   pcxtemp = false;
  3317.   giftemp = false;
  3318.   cutgrey = false;
  3319.   aliasused = false;
  3320.   xratio = 0;
  3321.   yratio = 0;   /*pxlmod:=false;*/
  3322.   egastretch = true;
  3323.   ledprinter = false;
  3324.   distribute = true;
  3325.   brightness = 0;
  3326.   bitspsam = 8;
  3327.   nowhite = true;
  3328.   usepxl = 0;
  3329.   vusepxl = 0;
  3330.   hrep = 1;
  3331.   vrep = 1;
  3332.   valinc = 70.0;
  3333.   gradint = 70;
  3334.   newwidth = 0;
  3335.   newheight = 0;
  3336.   printf("This is BitMapTOfont, version 1.1c of june 92, via p2c\n");
  3337.   printf("Converting Bitmap Files to TeX-Fonts\n");
  3338.   /*assign(logfile,'test.log');rewrite(logfile);close(logfile);*/
  3339.   /*109:*/
  3340.   if (P_argc == 1) {
  3341.     printf("usage is BM2FONT filename and parameters\n");
  3342.     printf("-f<name of picture for TeX>     (std filename)\n");
  3343.     printf(
  3344.       "-h<horizontal resolution>       (pixel/inch, std 300)\n");
  3345.     printf(
  3346.       "-v<vertical resolution>         (pixel/inch, std 300)\n");
  3347.     printf(
  3348.       "-l<length of mapline>           (in bytes, only pure bitmaps)\n");
  3349.     printf("-e<stretch EGA pictures>        (y or n, std y)\n");
  3350.     printf("-i<inversion of pixels>         (y or n, std n)\n");
  3351.     printf("-g<greypixels in bitmap>        (y or n, std n)\n");
  3352.     /*writeln('-p<write pixel files>           (y or n, std n)');*/
  3353.     printf("-w<let white be light grey>     (y or n, std y)\n");
  3354.     printf("-d<distribute errors>           (y or n, std y)\n");
  3355.     printf("-s<separation of grey dots>     (y or n, std n)\n");
  3356.     printf("-r<repeat each grey pixel>      (y or n, std n)\n");
  3357.     printf("-u<pixels for grey rectangle>   (less 8)\n");
  3358.     printf("-c<vert. pixels for rectangle>  (less 8)\n");
  3359.     printf("-x<bits per sample>             (0 < x < 9)\n");
  3360.     printf(
  3361.       "-b<reduce halftone colors>      (f.e. by 1, less u*c*4, std 0)\n");
  3362.     printf("-t<gradation value>             (in %%, std 70)\n");
  3363.     printf("-z<area of gradation>           (in %%, std 70)\n");
  3364.     printf("-m<width of picture on paper>   (in mm)\n");
  3365.     printf("-n<height of picture on paper>  (in mm)\n");
  3366.     /*readln(comment);
  3367. if length(comment)=0 then goto 9999;j:=ord(comment[0])+1;
  3368. if j<=127 then move(comment[0],mem[prefixseg:$80],j);*/
  3369.     goto _L9999;
  3370.   }
  3371.   /*:109*/
  3372.   /*110:*/
  3373.   for (j = 1; j < P_argc; j++) {
  3374.     strcpy(comment, P_argv[j]);
  3375.     if (comment[0] != '-') {
  3376.       strcpy(font, comment);
  3377.       if (strlen(font) < 1 || !strcmp(font, " ")) {
  3378.     printf("give the name of your bitmap file next time\n");
  3379.     goto _L9999;
  3380.       }
  3381.     } else {
  3382.       switch (comment[1]) {
  3383.  
  3384.       case 'h':
  3385.       case 'H':
  3386.     strdelete((void *)comment, 1, 2);
  3387.     i = (sscanf(comment, "%lf", &truehres) == 0);
  3388.     break;
  3389.  
  3390.       case 'v':
  3391.       case 'V':
  3392.     strdelete((void *)comment, 1, 2);
  3393.     i = (sscanf(comment, "%lf", &truevres) == 0);
  3394.     break;
  3395.  
  3396.       case 'l':
  3397.       case 'L':
  3398.     strdelete((void *)comment, 1, 2);
  3399.     i = (sscanf(comment, "%ld", &mapinlen) == 0);
  3400.     break;
  3401.  
  3402.       case 'm':
  3403.       case 'M':
  3404.     strdelete((void *)comment, 1, 2);
  3405.     i = (sscanf(comment, "%ld", &newwidth) == 0);
  3406.     break;
  3407.  
  3408.       case 'n':
  3409.       case 'N':
  3410.     strdelete((void *)comment, 1, 2);
  3411.     i = (sscanf(comment, "%ld", &newheight) == 0);
  3412.     break;
  3413.  
  3414.       case 'c':
  3415.       case 'C':
  3416.     strdelete((void *)comment, 1, 2);
  3417.     i = (sscanf(comment, "%ld", &vusepxl) == 0);
  3418.     break;
  3419.  
  3420.       case 'b':
  3421.       case 'B':
  3422.     strdelete((void *)comment, 1, 2);
  3423.     i = (sscanf(comment, "%ld", &brightness) == 0);
  3424.     break;
  3425.  
  3426.       case 'x':
  3427.       case 'X':
  3428.     strdelete((void *)comment, 1, 2);
  3429.     i = (sscanf(comment, "%ld", &bitspsam) == 0);
  3430.     break;
  3431.  
  3432.       case 't':
  3433.       case 'T':
  3434.     strdelete((void *)comment, 1, 2);
  3435.     i = (sscanf(comment, "%lf", &valinc) == 0);
  3436.     break;
  3437.  
  3438.       case 'z':
  3439.       case 'Z':
  3440.     strdelete((void *)comment, 1, 2);
  3441.     i = (sscanf(comment, "%ld", &gradint) == 0);
  3442.     break;
  3443.  
  3444.       case 'g':
  3445.       case 'G':
  3446.     strdelete((void *)comment, 1, 2);
  3447.     greypxl = (!strcmp(comment, "y") ||
  3448.            !strcmp(comment, "Y"));
  3449.     break;
  3450.  
  3451.       case 'e':
  3452.       case 'E':
  3453.     strdelete((void *)comment, 1, 2);
  3454.     egastretch = (!strcmp(comment, "n") ||
  3455.               !strcmp(comment, "N"));
  3456.     egastretch = !egastretch;
  3457.     break;
  3458.  
  3459.       case 'd':
  3460.       case 'D':
  3461.     strdelete((void *)comment, 1, 2);
  3462.     distribute = (!strcmp(comment, "n") ||
  3463.               !strcmp(comment, "N"));
  3464.     distribute = !distribute;
  3465.     break;
  3466.  
  3467.       case 'f':
  3468.       case 'F':
  3469.     strdelete((void *)comment, 1, 2);
  3470.     strcpy(aliasname, comment);
  3471.     aliasused = true;
  3472.     break;
  3473.  
  3474.       case 's':
  3475.       case 'S':
  3476.     strdelete((void *)comment, 1, 2);
  3477.     ledprinter = (!strcmp(comment, "y") ||
  3478.               !strcmp(comment, "Y"));
  3479.     break;
  3480.  
  3481.       /*'p','P':begin delete(comment,1,2);pxlmod:=(comment='y')or(comment='Y');
  3482. end;*/
  3483.       case 'r':
  3484.       case 'R':
  3485.     strdelete((void *)comment, 1, 2);
  3486.     if (!strcmp(comment, "y")) {
  3487.       hrep = 2;
  3488.       vrep = 2;
  3489.     }
  3490.     break;
  3491.  
  3492.       case 'w':
  3493.       case 'W':
  3494.     strdelete((void *)comment, 1, 2);
  3495.     nowhite = (!strcmp(comment, "n") ||
  3496.            !strcmp(comment, "N"));
  3497.     nowhite = !nowhite;
  3498.     break;
  3499.  
  3500.       case 'i':
  3501.       case 'I':
  3502.     strdelete((void *)comment, 1, 2);
  3503.     invert = (!strcmp(comment, "y") ||
  3504.           !strcmp(comment, "Y"));
  3505.     break;
  3506.  
  3507.       case 'u':
  3508.       case 'U':
  3509.     strdelete((void *)comment, 1, 2);
  3510.     i = (sscanf(comment, "%ld", &usepxl) == 0);
  3511.     break;
  3512.       }
  3513.     }
  3514.   }
  3515.   /*:110*/
  3516.   /*111:*/
  3517.   if (bitspsam < 1 || bitspsam > 8)
  3518.     bitspsam = 8;
  3519.   if (invert) {
  3520.     invbit = 0;
  3521.     invbyte = 0;
  3522.   } else {
  3523.     invbit = 1;
  3524.     invbyte = 255;
  3525.   }
  3526.   if (usepxl == 0)
  3527.     usepxl = 2;
  3528.   if (vusepxl == 0)
  3529.     vusepxl = usepxl;
  3530.   if (brightness >= usepxl * vusepxl * 4)
  3531.     brightness = 0;
  3532.   if (brightness != 0 && usepxl < 2)
  3533.     brightness = 0;
  3534.   if (valinc > 99)
  3535.     valinc = 99.0;
  3536.   if (valinc > 0) {
  3537.     if (gradint == 0)
  3538.       gradint = 1;
  3539.   }
  3540.   if (gradint > 99)
  3541.     gradint = 99;
  3542.   if (newheight < 0)
  3543.     newheight = 0;
  3544.   if (newwidth < 0)
  3545.     newwidth = 0;
  3546.   if (newwidth > 200) {
  3547.     printf("Sorry, new width set to 200mm\n");
  3548.     newwidth = 200;
  3549.   }
  3550.   if (newheight > 280) {
  3551.     printf("Sorry, new height set to 280mm\n");
  3552.     newheight = 280;
  3553.   }
  3554.   if (truehres == 0)
  3555.     truehres = 300.0;
  3556.   if (truevres == 0)   /*:111*/
  3557.     truevres = truehres;
  3558.   /*:106*/
  3559.   /*107:*/
  3560.   for (j = 0; j <= 25; j++) {
  3561.     for (i = 0; i <= 128; i++) {
  3562.       pwidth[j][i] = 0;
  3563.       width[j][i] = 0;
  3564.       height[j][i] = 0;
  3565.       pheight[j][i] = 0;
  3566.       yoffset[j][i] = 0;
  3567.     }
  3568.   }
  3569.   for (i = 0; i <= 128; i++)
  3570.     dirpoint[i] = 0;
  3571.   for (i = 0; i <= 128; i++)
  3572.     pkpoint[i] = 0;
  3573.   maxx = 0;
  3574.   minx = 646;
  3575.   mapheight = 0;   /*:107*/
  3576.   fok = true;
  3577.   strcpy(bmname, font);
  3578.   strcpy(bitmap_NAME, bmname);
  3579.   if (bitmap != NULL)
  3580.     bitmap = freopen(bitmap_NAME, "rb", bitmap);
  3581.   else
  3582.     bitmap = fopen(bitmap_NAME, "rb");
  3583.   _SETIO(bitmap != NULL, FileNotFound);
  3584.   filestat = P_ioresult;
  3585.   if (filestat == 0)
  3586.     fok = false;
  3587.   else
  3588.     fok = true;
  3589.   if (fok) {   /*112:*/
  3590.     printf("File %s not found, abort\n", font);
  3591.     fileproblem();
  3592.     goto _L9999;
  3593.   }
  3594.   i = 1;
  3595.   j = 0;
  3596.   strcpy(cmd, " ");
  3597.   while (i <= strlen(bmname)) {
  3598.     if (bmname[i - 1] == '.')
  3599.       j = i;
  3600.     i++;
  3601.   }
  3602.   if (j > 0)
  3603.     strsub(cmd, bmname, j + 1, strlen(bmname) - j + 1);
  3604.   if (!strcmp(cmd, "tif") || !strcmp(cmd, "TIF") ||
  3605.       !strcmp(cmd, "tiff") || !strcmp(cmd, "TIFF"))
  3606.     tifint();
  3607.   else {
  3608.     if (!strcmp(cmd, "pcx") || !strcmp(cmd, "PCX"))
  3609.       pcxint();
  3610.     else if (!strcmp(cmd, "lbm") || !strcmp(cmd, "LBM") ||
  3611.          !strcmp(cmd, "iff") || !strcmp(cmd, "IFF"))
  3612.       iffint();
  3613.     else if (!strcmp(cmd, "gif") || !strcmp(cmd, "GIF"))
  3614.       gifint();
  3615.     else if (!strcmp(cmd, "bmp") || !strcmp(cmd, "BMP"))
  3616.       bmpint();
  3617.     else if (!strcmp(cmd, "img") || !strcmp(cmd, "IMG"))
  3618.       imgint();
  3619.     else if (!strcmp(cmd, "cut") || !strcmp(cmd, "CUT"))
  3620.       cutint();
  3621.     else
  3622.       posbit = 0;
  3623.   }
  3624.   if (aliasused) {
  3625.     strcpy(font, aliasname);
  3626.     strcpy(bmname, aliasname);
  3627.   }
  3628.   FORLIM = strlen(bmname);
  3629.   for (i = 1; i <= FORLIM; i++) {
  3630.     sprintf(STR1, "%c", bmname[i - 1]);
  3631.     j = strpos2(fontpre, STR1, 1);
  3632.     if (j != 0)
  3633.       bmname[i - 1] = fontupc[j - 1];
  3634.   }
  3635.   FORLIM = strlen(font);
  3636.   for (i = 1; i <= FORLIM; i++) {
  3637.     sprintf(STR1, "%c", font[i - 1]);
  3638.     j = strpos2(fontupc, STR1, 1);
  3639.     if (j != 0)
  3640.       font[i - 1] = fontpre[j - 1];
  3641.   }
  3642.   korrfont();
  3643.   nameok = true;
  3644.   FORLIM = strlen(font);
  3645.   for (i = 1; i <= FORLIM; i++) {
  3646.     if (isdigit(font[i - 1]))
  3647.       nameok = false;
  3648.   }
  3649.   if (!nameok) {
  3650.     printf("no digits allowed in name of font\n");
  3651.     goto _L9999;
  3652.   }
  3653.   strcpy(bmname, font);
  3654.   FORLIM = strlen(bmname);
  3655.   for (i = 1; i <= FORLIM; i++) {   /*:112*/
  3656.     sprintf(STR1, "%c", bmname[i - 1]);
  3657.     j = strpos2(fontpre, STR1, 1);
  3658.     if (j != 0)
  3659.       bmname[i - 1] = fontupc[j - 1];
  3660.   }
  3661.   /*113:*/
  3662.   if (truehres <= 0 || truehres > 2000)
  3663.     truehres = 300.0;
  3664.   if (truevres <= 0 || truevres > 2000)
  3665.     truevres = 300.0;
  3666.   whatistrue();
  3667.   if (hres != (long)floor(truehres + 0.5))
  3668.     printf("maximum width of characters is %ld pixel\n",
  3669.        hres);
  3670.   if (vres != (long)floor(truevres + 0.5))
  3671.     printf("maximum height of characters is %ld pixel\n",
  3672.        vres);
  3673.   if (posbit != 0)
  3674.     pxlinlen = (halfk)((mapwidth + 7) / 8);
  3675.   else {
  3676.     pxlinlen = mapinlen;
  3677.     mapwidth = pxlinlen * 8;
  3678.     if (greypxl)   /*115:*/
  3679.       mapwidth /= bitspsam;
  3680.     if (mapwidth < 1) {
  3681.       printf("Width not correct!\n");
  3682.       goto _L9999;
  3683.     }
  3684.     mapheight = P_maxpos(bitmap) / mapinlen;
  3685.     if (P_maxpos(bitmap) % mapinlen > 0) {
  3686.       printf("Remainder > 0 when dividing size of file by width!\n");
  3687.       goto _L9999;
  3688.     }
  3689. /* p2c: bm2font.p, line 1032:
  3690.  * Note: Using % for possibly-negative arguments [317] */
  3691.     if (greypxl) {
  3692.       mapdiv = 1.0;
  3693.       photoinf = 15;
  3694.       FORLIM1 = bitspsam;
  3695.       for (cb = 1; cb <= FORLIM1; cb++)
  3696.     mapdiv *= 2;
  3697.       mapdiv /= 16;
  3698.     }  /*:115*/
  3699.   }
  3700.   if (pxlinlen > 3000) {
  3701.     printf("File not processable, width > 3000 bytes\n");
  3702.     goto _L9999;
  3703.   }  /*:113*/
  3704.   if (newwidth != 0 || newheight != 0)
  3705.     scaling();
  3706.   if (greypxl) {  /*96:*/
  3707.     printf("Halftone picture required\n");
  3708.     pxlinlen = (halfk)
  3709.     (((unsigned long)(mapwidth * bitspsam + 7)) >> 3);
  3710.     greypix = mapwidth;
  3711.     mapwidth *= bitspsam;   /*97:*/
  3712.     sprintf(tmpname, "%s.tmp", font);
  3713.     strcpy(tmpfil_NAME, tmpname);
  3714.     filestat = P_ioresult;
  3715.     if (filestat == 0)
  3716.       fok = false;
  3717.     else
  3718.       fok = true;
  3719.     if (fok) {
  3720.       printf("tempfile %s not accessable, abort\n", tmpname);
  3721.       fileproblem();
  3722.       goto _L9999;
  3723.     }
  3724.     if (tmpfil != NULL)
  3725.       tmpfil = freopen(tmpfil_NAME, "wb", tmpfil);
  3726.     else
  3727.       tmpfil = fopen(tmpfil_NAME, "wb");
  3728.     _SETIO(tmpfil != NULL, FileNotFound);
  3729.     filestat = P_ioresult;
  3730.     if (filestat == 0)
  3731.       fok = false;
  3732.     else
  3733.       fok = true;
  3734.     if (fok) {   /*:97*/
  3735.       printf("Tempfile for font %s not found, abort\n",
  3736.          font);
  3737.       fileproblem();
  3738.       goto _L9999;
  3739.     }
  3740.     /*103:*/
  3741.     k = 0;
  3742.     if (usepxl != vusepxl) {
  3743.       FORLIM = vusepxl;
  3744.       for (i = 0; i < FORLIM; i++) {
  3745.     FORLIM2 = usepxl;
  3746.     for (j = 0; j < FORLIM2; j++) {
  3747.       k++;
  3748.       tab[k - 1][0] = i + 1;
  3749.       tab[k - 1][1] = j + 1;
  3750.       tab[k - 1]
  3751.         [2] = i * i * (usepxl - 1) * (usepxl - 1) +
  3752.           j * j * (vusepxl - 1) * (vusepxl - 1);
  3753.     }
  3754.       }
  3755.       FORLIM = k;
  3756.       for (i = 1; i <= FORLIM; i++) {
  3757.     FORLIM2 = k;
  3758.     for (j = i; j <= FORLIM2; j++) {
  3759.       if (tab[i - 1][2] > tab[j - 1][2]) {
  3760.         lx = tab[i - 1][0];
  3761.         tab[i - 1][0] = tab[j - 1][0];
  3762.         tab[j - 1][0] = lx;
  3763.         lx = tab[i - 1][1];
  3764.         tab[i - 1][1] = tab[j - 1][1];
  3765.         tab[j - 1][1] = lx;
  3766.         lx = tab[i - 1][2];
  3767.         tab[i - 1][2] = tab[j - 1][2];
  3768.         tab[j - 1][2] = lx;
  3769.       }
  3770.     }
  3771.       }
  3772.       FORLIM = k;
  3773.       for (i = 1; i <= FORLIM; i++) {
  3774.     lx = tab[i - 1][0];
  3775.     ly = tab[i - 1][1];
  3776.     ditherm[lx - 1][ly - 1] = i;
  3777.       }
  3778.     }
  3779.     if (usepxl > 7)
  3780.       usepxl = 7;
  3781.     if (vusepxl > 7)
  3782.       vusepxl = 7;
  3783.     maxcol = 1;
  3784.     cb = 0;
  3785.     usecol = usepxl * vusepxl * 4;
  3786.     FORLIM = bitspsam;
  3787.     for (k = 1; k <= FORLIM; k++)
  3788.       maxcol *= 2;
  3789.     maxcol--;
  3790.     for (k = 1; k <= 4; k++) {
  3791.       for (i = 0; i <= 196; i++) {
  3792.     for (j = 1; j <= 7; j++)
  3793.       pattern[k - 1][i][j - 1] = 0;
  3794.       }
  3795.     }
  3796.     FORLIM = vusepxl;
  3797.     for (i = 1; i <= FORLIM; i++) {
  3798.       FORLIM2 = usepxl;
  3799.       for (j = 1; j <= FORLIM2; j++) {
  3800.     if (ditherm[i - 1][j - 1] <= usecol / 4 &&
  3801.         ditherm[i - 1][j - 1] > 0) {
  3802.       v = (ditherm[i - 1][j - 1] - 1) * 4 + 1;
  3803.       for (ccol = 1; ccol <= 4; ccol++) {
  3804.         if (ccol == 1)
  3805.           c = 1;
  3806.         else if (ccol == 2)
  3807.           c = 3;
  3808.         else if (ccol == 3)
  3809.           c = 4;
  3810.         else
  3811.           c = 2;
  3812.         FORLIM3 = usecol;
  3813.         for (k = v; k <= FORLIM3; k++) {
  3814.           switch (c) {
  3815.  
  3816.           case 4:
  3817.         if (usepxl > 2 || vusepxl > 2)
  3818.           pattern[0][k]
  3819.             [j - 1] |= (cb + 2) << (7 - i);
  3820.         else
  3821.           pattern[0][k]
  3822.             [i - 1] |= (cb + 2) << (7 - j);
  3823.         break;
  3824.  
  3825.           case 1:
  3826.         pattern[1][k]
  3827.           [vusepxl - i] |= (cb + 1) << (j - usepxl + 7);
  3828.         break;
  3829.  
  3830.           case 2:
  3831.         if (usepxl > 2 || vusepxl > 2)
  3832.           pattern[2][k]
  3833.             [vusepxl - j] |= (cb + 2) << (7 - i);
  3834.         else
  3835.           pattern[2][k]
  3836.             [vusepxl - i] |= (cb + 2) << (7 - j);
  3837.         break;
  3838.  
  3839.           case 3:
  3840.         pattern[3][k]
  3841.           [i - 1] |= (cb + 1) << (j - usepxl + 7);
  3842.         break;
  3843.           }
  3844.         }
  3845.         v++;
  3846.       }
  3847.     }
  3848.       }
  3849.     }
  3850.     if (nowhite)
  3851.       pattern[1][0][usepxl - 1] = 128 >> (usepxl - 1);
  3852.     usecol -= brightness;   /*:103*/
  3853.     /*[104:]append(logfile);
  3854. for k:=0 to usecol do begin for j:=1 to vusepxl do begin for i:=1 to 4
  3855. do begin cb:=pattern[i,k,j];write(logfile,cb:4,' ');
  3856. for pk:=1 to usepxl do begin if cb>127 then write(logfile,'1')else write
  3857. (logfile,'0');cb:=cb shl 1;end;write(logfile,' ');end;
  3858. writeln(logfile,'.');end;writeln(logfile,'---- ',k:1,' ----');end;
  3859. close(logfile);[:104]*/
  3860.     greypix -= greypix & 1;
  3861.     if (ledprinter)
  3862.       gbufpp = ((long)floor(greypix * (usepxl + 0.5) + 0.5) *
  3863.         hrep + 7) / 8;
  3864.     else
  3865.       gbufpp = (greypix * usepxl * hrep + 7) / 8;
  3866.     if (gbufpp > 1280) {
  3867.       printf("Only up to 1280 bytes in one row allowed!\n");
  3868.       printf("Try again by decreasing param <u> or <r=n>\n");
  3869.       _Escape(0);
  3870.     }  /*98:*/
  3871.     printf("Analysing and optimizing the picture\n");
  3872.     pminval = 255;
  3873.     pmaxval = 0;
  3874.     if (cutgrey) {
  3875.       checksum = mapheight;
  3876.       pxlinlen = cutlen;
  3877.     } else
  3878.       checksum = mapheight;
  3879.     pl = 0;
  3880.     fseek(bitmap, posbit, SEEK_SET);
  3881.     do {
  3882.       pxlinlen = fread(b1, 1, pxlinlen, bitmap);
  3883.       colpos = 0;
  3884.       cutrep = 0;
  3885.       FORLIM = greypix;
  3886.       for (pk = 0; pk < FORLIM; pk++) {
  3887.     tmpcolor = getcolor();
  3888.     if (tmpcolor > pmaxval)
  3889.       pmaxval = tmpcolor;
  3890.     if (tmpcolor < pminval)
  3891.       pminval = tmpcolor;
  3892.       }
  3893.       checksum--;
  3894.       if (cutgrey)
  3895.     pxlinlen = b1[pxlinlen - 1] * 256 + b1[pxlinlen - 2] + 2;
  3896.     } while (checksum != 0);
  3897.     if (pminval == pmaxval) {
  3898.       printf("Only one color (value %d) used by bitmap!\n",
  3899.          pmaxval);
  3900.       printf("It makes no sence to generate a picture!\n");
  3901.       _Escape(0);
  3902.     }  /*:98*/
  3903.     if (brightness == 0) {
  3904.       brightness = (long)floor((maxcol - pmaxval + pminval + 1.0) *
  3905.                    usecol / (maxcol + 1) + 0.5);
  3906.       if (brightness > 0)
  3907.     printf("Best result may be with parameter -b%ld !?\n",
  3908.            brightness);
  3909.     }
  3910.     printf("Dithering the picture\n");
  3911.     memset(greymem, 255, 65522L);
  3912.     pgreymem = 0;
  3913.     if (cutgrey) {
  3914.       checksum = mapheight;
  3915.       pxlinlen = cutlen;
  3916.     } else
  3917.       checksum = mapheight;
  3918.     pl = 0;
  3919.     fseek(bitmap, posbit, SEEK_SET);
  3920.     patswitch = 4;
  3921.     spreadcol = (maxcol + 1.0) / (pmaxval - pminval + 1);
  3922.     patref = (double)usecol / maxcol;   /*99:*/
  3923.     for (k = 0; k <= 255; k++)
  3924.       pxllook[k] = k;
  3925.     if (invert) {
  3926.       for (k = 0; k <= 255; k++)
  3927.     pxllook[k] = 255 - k;
  3928.     }
  3929.     if (invert) {
  3930.       FORLIM = pmaxval;
  3931.       for (k = pminval; k <= FORLIM; k++)
  3932.     pxllook[pmaxval - k + pminval] =
  3933.       (long)floor((k - pminval) * spreadcol + 0.5);
  3934.       invbit = 1;
  3935.       invbyte = 255;
  3936.     } else {
  3937.       FORLIM = pmaxval;
  3938.       for (k = pminval; k <= FORLIM; k++)
  3939.     pxllook[k] = (long)floor((k - pminval) * spreadcol + 0.5);
  3940.     }
  3941.     FORLIM = maxcol;
  3942.     for (k = 0; k <= FORLIM; k++)
  3943.       lutone[k] = (long)floor((maxcol - k) * patref + 0.5);
  3944.     if (valinc > 0) {   /*100:*/
  3945.       k = pminval;
  3946.       pk = 0;
  3947.       while (pk <= gradint && k <= pmaxval) {
  3948.     pk = (long)floor(pxllook[k] * 100.0 / maxcol + 0.5);
  3949.     k++;
  3950.       }
  3951.       pkn = pxllook[k - 1];
  3952.       grxzero = pkn;
  3953.       redval = valinc / 100;
  3954.       FORLIM = pmaxval;
  3955.       for (k = pminval; k <= FORLIM; k++) {
  3956.     /*write(logfile,(pxllook[k]/maxcol):1:5
  3957. );*/
  3958.     grx = pxllook[k];
  3959.     if (grx <= pkn) {
  3960.       if (grx > 0)
  3961.         pxllook[k] =
  3962.           (long)
  3963.           floor(0.5 * grx *
  3964.               (exp(redval * log(grx / grxzero)) +
  3965.                exp(-redval * log(grx / grxzero))) + 0.5);
  3966. /* p2c: bm2font.p, line 1305: Note: Line breaker spent
  3967.  * 1.4+0.40 seconds, 5000 tries on line 3965 [251] */
  3968.     }
  3969.     /*writeln(logfile,' ',(pxllook[k]/maxcol):1:5);*/
  3970.       }  /*close(logfile);;*/
  3971.       /*:100*/
  3972.     }
  3973.     /*append(logfile);*/
  3974.     FORLIM = maxcol;
  3975.     for (pk = 0; pk <= FORLIM; pk++)
  3976.       hktab[pk] = (long)floor((maxcol - pk) * patref * 100 + 0.5) -
  3977.           (long)(lutone[pk] * 100.0);
  3978.     cspread = false;
  3979.     if (usecol - 1 > maxcol) {
  3980.       pk = usecol;
  3981.       for (k = maxcol; k >= 0; k--) {
  3982.     if (invert)
  3983.       pk = usecol - lutone[pxllook[maxcol - k]];
  3984.     else
  3985.       pk = usecol - lutone[pxllook[k]];
  3986.     while (pk >= k) {
  3987.       lutone[pk] = lutone[pxllook[k]];
  3988.       hktab[pk] = (long)floor(lutone[pk] * 100.0 + 0.5) -
  3989.           (long)floor((usecol - pk) * 100.0 + 0.5);
  3990.       lutone[pk] -= hktab[pk] / 100;
  3991.       pk--;
  3992.     }
  3993.       }
  3994.       if (invert) {
  3995.     FORLIM = usecol;
  3996.     for (k = 0; k <= FORLIM; k++)
  3997.       pxllook[k] = lutone[k];
  3998.       } else {
  3999.     FORLIM = usecol;
  4000.     for (k = 0; k <= FORLIM; k++)
  4001.       pxllook[k] = usecol - lutone[k];
  4002.       }
  4003.       maxcol = usecol;
  4004.       cspread = true;
  4005.     }
  4006.     invert = false;
  4007.     mapdiv = (usecol + 1.0) / 16;
  4008.     FORLIM = usecol;
  4009.     for (k = 0; k <= FORLIM; k++)
  4010.       lutvga[k] = (long)((usecol - k) / mapdiv);
  4011.     for (pk = -255; pk <= 255; pk++) {
  4012.       if (cspread)
  4013.     gkorh[pk + 255] = (long)(pk / 2.0);
  4014.       else
  4015.     gkorh[pk + 255] = (long)floor(pk / 2.0 * spreadcol + 0.5);
  4016.       if (cspread)
  4017.     gkordivp[pk + 255] = (long)(pk / 2.0);
  4018.       else
  4019.     gkordivp[pk + 255] = (long)
  4020.         (pk / 2.0 * patref + hktab[abs(pk)] / 100.0);
  4021.     }
  4022.     for (pk = 0; pk <= 255; pk++)   /*:99*/
  4023.       spreadvals[pk] = (long)floor(pk * patref + 0.5);
  4024.     do {
  4025.       pxlinlen = fread(b1, 1, pxlinlen, bitmap);
  4026.       colpos = 0;
  4027.       cutrep = 0;
  4028.       FORLIM = greypix;
  4029.       for (pk = 0; pk < FORLIM; pk++)
  4030.     b4[pk] = getcolor();
  4031.       if (cspread) {
  4032.     FORLIM = greypix;
  4033.     for (pk = 0; pk < FORLIM; pk++)
  4034.       b4[pk] = spreadvals[b4[pk]];
  4035.       }
  4036.       if (distribute) {   /*101:*/
  4037.     pk = 1;
  4038.     pkn = 1;
  4039.     while (pk < greypix) {
  4040.       pkn++;
  4041.       greykor = 0;
  4042.       if (b4[pk] != b4[pkn]) {
  4043.         greykor = gkorh[b4[pk] - b4[pkn] + 255];
  4044.         greykor = gkordivp[greykor + 255];
  4045.       }
  4046.       if (greykor != 0) {
  4047.         tmpcolor = lutone[pxllook[b4[pkn]]];
  4048.         if (abs(tmpcolor + greykor) <= usecol &&
  4049.         abs(tmpcolor + greykor) >= 0)
  4050.           b4[pkn] += greykor;
  4051.       }
  4052.       pk++;
  4053.     }
  4054.     pk = greypix - 1;
  4055.     pkn = pk;
  4056.     while (pk > 0) {   /*:101*/
  4057.       pkn--;
  4058.       greykor = 0;
  4059.       if (b4[pk] != b4[pkn]) {
  4060.         greykor = gkorh[b4[pk] - b4[pkn] + 255];
  4061.         greykor = gkordivp[greykor + 255];
  4062.       }
  4063.       if (greykor != 0) {
  4064.         tmpcolor = lutone[pxllook[b4[pkn]]];
  4065.         if (abs(tmpcolor + greykor) <= usecol &&
  4066.         abs(tmpcolor + greykor) >= 0)
  4067.           b4[pkn] += greykor;
  4068.       }
  4069.       pk--;
  4070.     }
  4071.       }
  4072.       FORLIM = greypix;
  4073.       for (pk = 0; pk < FORLIM; pk++)
  4074.     b4[pk] = lutone[pxllook[b4[pk]]];
  4075.       FORLIM1 = vrep;
  4076.       for (yrep = 1; yrep <= FORLIM1; yrep++) {   /*102:*/
  4077.     FORLIM = vusepxl;
  4078.     for (k = 1; k <= FORLIM; k++) {
  4079.       gbufp = 0;
  4080.       cb = 0;
  4081.       pn = 0;
  4082.       switch (patswitch) {
  4083.  
  4084.       case 1:
  4085.         curpat[0] = 2;
  4086.         curpat[1] = 3;
  4087.         break;
  4088.  
  4089.       case 2:
  4090.         curpat[0] = 4;
  4091.         curpat[1] = 1;
  4092.         break;
  4093.  
  4094.       case 3:
  4095.         curpat[0] = 3;
  4096.         curpat[1] = 2;
  4097.         break;
  4098.  
  4099.       case 4:
  4100.         curpat[0] = 1;
  4101.         curpat[1] = 4;
  4102.         break;
  4103.       }
  4104.       memset(b3, 0, gbufpp);
  4105.       FORLIM2 = greypix;
  4106.       for (pk = 0; pk < FORLIM2; pk++) {
  4107.         FORLIM4 = hrep;
  4108.         for (xrep = 1; xrep <= FORLIM4; xrep++) {
  4109.           cb = pattern[curpat[pn] - 1][b4[pk]][k - 1];
  4110.           dbuf = ((unsigned)gbufp) >> 3;
  4111.           bufsh = gbufp & 7;
  4112.           nextbuf = bufsh + usepxl;
  4113.           switch (bufsh) {
  4114.  
  4115.           case 0:
  4116.         b3[dbuf] = cb;
  4117.         break;
  4118.  
  4119.           default:
  4120.         b3[dbuf] |= cb >> bufsh;
  4121.         if (nextbuf > 8)
  4122.           b3[dbuf + 1] = cb << (8 - bufsh);
  4123.         break;
  4124.           }
  4125.           gbufp += usepxl;
  4126.           if (ledprinter) {
  4127.         if (curpat[pn] == 1 || curpat[pn] == 3)
  4128.           gbufp++;
  4129.           }
  4130.           pn = abs(pn - 1);
  4131.         }
  4132.       }
  4133.       FORLIM2 = gbufpp;
  4134.       for (pk = 0; pk <= FORLIM2; pk++)
  4135.         b3[pk] = ~b3[pk];
  4136.       if (pgreymem + gbufpp > 65522L) {
  4137.         _SETIO(fwrite(greymem, pgreymem, 1, tmpfil) == 1,
  4138.            FileWriteError);
  4139.         filestat = P_ioresult;
  4140.         if (filestat != 0) {
  4141.           _SETIO(printf("Error occured when writing file %s!\n",
  4142.                 tmpname) >= 0, FileWriteError);
  4143.           fileproblem();
  4144.           _Escape(0);
  4145.         }
  4146.         pgreymem = 0;
  4147.       }
  4148.       memmove(&greymem[pgreymem], b3, gbufpp);
  4149.       pgreymem += gbufpp;
  4150.     }
  4151.     memset(b3, 255, gbufpp);
  4152.     if (ledprinter) {
  4153.       if (patswitch == 2 || patswitch == 4) {
  4154.         if (pgreymem + gbufpp > 65522L) {
  4155.           _SETIO(fwrite(greymem, pgreymem, 1, tmpfil) ==
  4156.              1, FileWriteError);
  4157.           filestat = P_ioresult;
  4158.           if (filestat != 0) {
  4159.         _SETIO(
  4160.           printf("Error occured when writing file %s!\n",
  4161.              tmpname) >= 0, FileWriteError);
  4162.         fileproblem();
  4163.         _Escape(0);
  4164.           }
  4165.           pgreymem = 0;
  4166.         }
  4167.         memmove(&greymem[pgreymem], b3, gbufpp);
  4168.         pgreymem += gbufpp;
  4169.       }  /*:102*/
  4170.     }
  4171.     patswitch++;
  4172.     if (patswitch > 4)
  4173.       patswitch = 1;
  4174.       }
  4175.       pm = 128;
  4176.       pb = pl;
  4177.       FORLIM = greypix;
  4178.       for (pk = 0; pk < FORLIM; pk++) {
  4179.     pxlcolor = lutvga[b4[pk]];
  4180.     pm >>= 1;
  4181.     if (pm == 0) {
  4182.       pm = 128;
  4183.       pb++;
  4184.     }
  4185.       }
  4186.       pl += 80;
  4187.       if (pl >= 38400L)
  4188.     pl = 0;
  4189.       checksum--;
  4190.       if (cutgrey)
  4191.     pxlinlen = (halfk)(b1[pxlinlen - 1] * 256 +
  4192.                b1[pxlinlen - 2] + 2);
  4193.     } while (checksum != 0);
  4194.     if (pgreymem != 0) {  /*$I-*/
  4195.       _SETIO(fwrite(greymem, pgreymem, 1, tmpfil) == 1,
  4196.          FileWriteError);
  4197.       filestat = P_ioresult;
  4198.       if (filestat != 0) {
  4199.     _SETIO(printf("Error occured when writing file %s!\n",
  4200.               tmpname) >= 0, FileWriteError);
  4201.     fileproblem();
  4202.     _Escape(0);
  4203.       }
  4204.     }
  4205.     posbit = 0;
  4206.     if (ledprinter)
  4207.       mapwidth =
  4208.     (long)floor(greypix * (usepxl + 0.5) + 0.5) * hrep;
  4209.     else
  4210.       mapwidth = greypix * usepxl * hrep;
  4211.     pxlinlen = (mapwidth + 7) / 8;
  4212.     mapinlen = pxlinlen;
  4213.     if (ledprinter)
  4214.       mapheight =
  4215.     (long)floor(mapheight * (vusepxl + 0.5) + 0.5) * vrep;
  4216.     else
  4217.       mapheight *= vusepxl * vrep;
  4218.     if (tmpfil != NULL)
  4219.       fclose(tmpfil);
  4220.     tmpfil = NULL;
  4221.     if (bitmap != NULL)
  4222.       fclose(bitmap);
  4223.     bitmap = NULL;
  4224.     strcpy(bitmap_NAME, tmpname);
  4225.     if (bitmap != NULL)
  4226.       bitmap = freopen(bitmap_NAME, "rb", bitmap);
  4227.     else
  4228.       bitmap = fopen(bitmap_NAME, "rb");
  4229.     if (bitmap == NULL)
  4230.       _EscIO(FileNotFound);
  4231.   }  /*114:*/
  4232.   checksum = mapheight * ((mapwidth + 7) / 8);
  4233.   mapheight = 0;
  4234.   freelines = 0;
  4235.   fseek(bitmap, posbit, SEEK_SET);
  4236.   while (checksum >= pxlinlen) {
  4237.     fread(b1, pxlinlen, 1, bitmap);
  4238.     mapwidth = pxlinlen * 8;
  4239.     j = pxlinlen - 1;
  4240.     while (b1[j] == invbyte && j >= 0)
  4241.       j--;
  4242.     if (j >= 0) {
  4243.       i = 0;
  4244.       while (b1[i] == invbyte && i <= j)
  4245.     i++;
  4246.       if (i < minx)
  4247.     minx = i;
  4248.       lx = j * 8 + 8;
  4249.       cb = b1[j];
  4250.       for (i = 1; i <= 8; i++) {
  4251.     if ((cb & 1) == invbit) {
  4252.       cb >>= 1;
  4253.       lx--;
  4254.     } else
  4255.       i = 8;
  4256.       }
  4257.       if (lx > maxx)
  4258.     maxx = lx;
  4259.       freelines++;
  4260.       mapheight += freelines;
  4261.       freelines = 0;
  4262.     } else if (mapheight == 0)
  4263.       posbit += pxlinlen;
  4264.     else
  4265.       freelines++;
  4266.     checksum -= pxlinlen;
  4267.   }
  4268.   mapwidth = maxx - minx * 8;
  4269.   zeropads = pxlinlen - (mapwidth + 7) / 8;
  4270.   maplines = mapheight;
  4271.   posbit += minx;   /*:114*/
  4272.   /*116:*/
  4273.   i = (mapwidth + hres - 1) / hres;
  4274.   j = (mapheight + vres - 1) / vres;
  4275.   nofchars = i * j;
  4276.   charpf = 515832L / (vres * ((hres + 31) / 32) * 32);
  4277.   noffonts = (nofchars + charpf - 1) / charpf;
  4278.   if (charpf > 76) {
  4279.     charpf = 76;
  4280.     noffonts = (nofchars + 75) / 76;
  4281.   }
  4282.   if (noffonts > 26) {
  4283.     printf("more than 26 fonts necessary, try a smaller bitmap\n");
  4284.     goto _L9999;
  4285.   }
  4286.   /*:116*/
  4287.   /*120:*/
  4288.   k = 0;
  4289.   c = 0;
  4290.   while (mapheight > 0) {   /*:120*/
  4291.     maxx = mapwidth;
  4292.     while (maxx > 0) {
  4293.       if (maxx > hres)
  4294.     pwidth[k][cref[c]] = hres;
  4295.       else
  4296.     pwidth[k][cref[c]] = maxx;
  4297.       if (mapheight > vres)
  4298.     pheight[k][cref[c]] = vres;
  4299.       else
  4300.     pheight[k][cref[c]] = mapheight;
  4301.       c++;
  4302.       if (c == charpf) {
  4303.     k++;
  4304.     c = 0;
  4305.       }
  4306.       maxx -= hres;
  4307.     }
  4308.     mapheight -= vres;
  4309.   }
  4310.   /*121:*/
  4311.   FORLIM = noffonts;
  4312.   for (a = 0; a < FORLIM; a++) {   /*:121*/
  4313.     lh = 17;
  4314.     bc = cref[0];
  4315.     ec = 127;
  4316.     while (pheight[a][ec] == 0)   /*122:*/
  4317.       ec--;
  4318.     sprintf(cmd, "%s%c.tfm", font, fontpre[a]);
  4319.     strcpy(tfmfile_NAME, cmd);
  4320.     if (tfmfile != NULL)
  4321.       tfmfile = freopen(tfmfile_NAME, "wb", tfmfile);
  4322.     else
  4323.       tfmfile = fopen(tfmfile_NAME, "wb");
  4324.     _SETIO(tfmfile != NULL, FileNotFound);
  4325.     filestat = P_ioresult;
  4326.     if (filestat == 3 || filestat == 5) {
  4327.       strcpy(cmd, bmname);
  4328.       sprintf(cmd + strlen(cmd), "%c.tfm", fontupc[a]);
  4329.       strcpy(tfmfile_NAME, cmd);
  4330.       if (tfmfile != NULL)
  4331.     tfmfile = freopen(tfmfile_NAME, "wb", tfmfile);
  4332.       else
  4333.     tfmfile = fopen(tfmfile_NAME, "wb");
  4334.       _SETIO(tfmfile != NULL, FileNotFound);
  4335.       filestat = P_ioresult;
  4336.     }
  4337.     if (filestat == 0)
  4338.       fok = false;
  4339.     else
  4340.       fok = true;
  4341.     if (fok) {
  4342.       printf("error occurred when allocating tfmfile ");
  4343.       puts(cmd);
  4344.       fileproblem();
  4345.       goto _L9999;
  4346.     }
  4347.     printf("generating tfmfile %s\n", cmd);   /*:122*/
  4348.     if (pheight[a][bc] > pwidth[a][bc])
  4349.       dsize = (long)floor(pheight[a][bc] / truehres *
  4350.               72.27 * 65536L * 16 + 0.5);
  4351.     else
  4352.       dsize = (long)floor(pwidth[a][bc] / truehres * 72.27 *
  4353.               65536L * 16 + 0.5);
  4354.     tabdsize[a] = dsize;
  4355.     FORLIM2 = ec;
  4356.     for (k = bc; k <= FORLIM2; k++) {
  4357.       height[a]
  4358.     [k] = (long)floor(pheight[a][k] / truevres *
  4359.               4736286.7227 / dsize * 16777215L + 0.5);
  4360.       width[a]
  4361.     [k] = (long)floor(pwidth[a][k] / truehres * 4736286.7227 /
  4362.               dsize * 16777215L + 0.5);
  4363.     }
  4364.     fh = height[a][bc];
  4365.     fw = width[a][bc];
  4366.     sh = fh;
  4367.     sw = fw;
  4368.     nw = 2;
  4369.     nh = 2;
  4370.     FORLIM2 = ec;
  4371.     for (k = bc; k <= FORLIM2; k++) {
  4372.       if (height[a][k] != 0 && height[a][k] != fh &&
  4373.       height[a][k] != sh) {
  4374.     sh = height[a][k];
  4375.     nh++;
  4376.       }
  4377.       if (width[a][k] != 0 && width[a][k] != fw &&
  4378.       width[a][k] != sw) {
  4379.     sw = width[a][k];
  4380.     nw++;
  4381.       }
  4382.     }
  4383.     if (nh > 3) {
  4384.       printf("more than 2 heights, should be impossible\n");
  4385.       goto _L9999;
  4386.     }
  4387.     if (nw > 3) {
  4388.       printf("more than 2 widths, should be impossible\n");
  4389.       goto _L9999;
  4390.     }
  4391.     nd = 2;
  4392.     ni = 2;
  4393.     nl = 0;
  4394.     nk = 0;
  4395.     ne = 0;
  4396.     np = 0;
  4397.     lf = lh + ec - bc + nw + nh + nd + ni + nk + nl + ne +
  4398.      np + 7;
  4399.     checksum = 0;
  4400.     tfmhword(lf);
  4401.     tfmhword(lh);
  4402.     tfmhword(bc);
  4403.     tfmhword(ec);
  4404.     tfmhword(nw);
  4405.     tfmhword(nh);
  4406.     tfmhword(nd);
  4407.     tfmhword(ni);
  4408.     tfmhword(nl);
  4409.     tfmhword(nk);
  4410.     tfmhword(ne);
  4411.     tfmhword(np);
  4412.     tfmword(checksum);
  4413.     tfmword(dsize);
  4414.     strcpy(tfmstr, "0Graphic");
  4415.     tfmstr[0] = '\007';
  4416.     FORLIM2 = strlen(tfmstr);
  4417.     for (i = 1; i <= FORLIM2; i++) {
  4418.       tfmb = tfmstr[i - 1];
  4419.       fwrite(&tfmb, sizeof(uchar), 1, tfmfile);
  4420.     }
  4421.     tfmb = 0;
  4422.     for (i = 1; i <= 32; i++)
  4423.       fwrite(&tfmb, sizeof(uchar), 1, tfmfile);
  4424.     strcpy(tfmstr, "0Bitmap");
  4425.     tfmstr[0] = '\006';
  4426.     FORLIM2 = strlen(tfmstr);
  4427.     for (i = 1; i <= FORLIM2; i++) {
  4428.       tfmb = tfmstr[i - 1];
  4429.       fwrite(&tfmb, sizeof(uchar), 1, tfmfile);
  4430.     }
  4431.     tfmb = 0;
  4432.     for (i = 1; i <= 13; i++)
  4433.       fwrite(&tfmb, sizeof(uchar), 1, tfmfile);
  4434.     FORLIM2 = ec;
  4435.     for (i = bc; i <= FORLIM2; i++) {
  4436.       if (width[a][i] == 0) {
  4437.     tfmb = 0;
  4438.     fwrite(&tfmb, sizeof(uchar), 1, tfmfile);
  4439.       } else if (width[a][i] == fw) {
  4440.     tfmb = 1;
  4441.     fwrite(&tfmb, sizeof(uchar), 1, tfmfile);
  4442.       } else {
  4443.     tfmb = 2;
  4444.     fwrite(&tfmb, sizeof(uchar), 1, tfmfile);
  4445.       }
  4446.       if (height[a][i] == 0) {
  4447.     tfmb = 0;
  4448.     fwrite(&tfmb, sizeof(uchar), 1, tfmfile);
  4449.       } else if (height[a][i] == fh) {
  4450.     tfmb = 16;
  4451.     fwrite(&tfmb, sizeof(uchar), 1, tfmfile);
  4452.       } else {
  4453.     tfmb = 32;
  4454.     fwrite(&tfmb, sizeof(uchar), 1, tfmfile);
  4455.       }
  4456.       tfmhword(0);
  4457.     }
  4458.     tfmword(0);
  4459.     tfmword(fw);
  4460.     if (nw > 2)
  4461.       tfmword(sw);
  4462.     tfmword(0);
  4463.     tfmword(fh);
  4464.     if (nh > 2)
  4465.       tfmword(sh);
  4466.     tfmword(0);
  4467.     tfmword(0);
  4468.     tfmword(0);
  4469.     tfmword(0);
  4470.     if (tfmfile != NULL)
  4471.       fclose(tfmfile);
  4472.     tfmfile = NULL;
  4473.   }
  4474.   /*117:*/
  4475.   sprintf(cmd, "%s.tex", font);
  4476.   strcpy(texfile_NAME, cmd);
  4477.   if (texfile != NULL)
  4478.     texfile = freopen(texfile_NAME, "w", texfile);
  4479.   else
  4480.     texfile = fopen(texfile_NAME, "w");
  4481.   _SETIO(texfile != NULL, FileNotFound);
  4482.   filestat = P_ioresult;
  4483.   if (filestat == 0)
  4484.     fok = false;
  4485.   else
  4486.     fok = true;
  4487.   if (filestat == 5) {
  4488.     *cmd = '\0';
  4489.     sprintf(cmd + strlen(cmd), "%s.tex", font);
  4490.     strcpy(texfile_NAME, cmd);
  4491.     if (texfile != NULL)
  4492.       texfile = freopen(texfile_NAME, "w", texfile);
  4493.     else
  4494.       texfile = fopen(texfile_NAME, "w");
  4495.     _SETIO(texfile != NULL, FileNotFound);
  4496.     filestat = P_ioresult;
  4497.     if (filestat == 0)
  4498.       fok = false;
  4499.     else
  4500.       fok = true;
  4501.   }
  4502.   if (fok) {   /*:117*/
  4503.     printf("%s error occured when allocating TeXfile %s\n",
  4504.        cmd, cmd);
  4505.     fileproblem();
  4506.     goto _L9999;
  4507.   }
  4508.   /*118:*/
  4509.   fprintf(texfile, "\\newbox\\%sbox\n", font);
  4510.   fprintf(texfile, "\\newdimen\\%swd\n", font);
  4511.   FORLIM = noffonts;
  4512.   for (a = 1; a <= FORLIM; a++) {
  4513.     fprintf(texfile, "\\font\\%s%c=%s%c at ",
  4514.         font, fontpre[a - 1], font, fontpre[a - 1]);
  4515.     outfix(tabdsize[a - 1]);
  4516.     fprintf(texfile, "truept\n");
  4517.   }
  4518.   fprintf(texfile, "\\setbox\\%sbox=\\vbox{\\hbox{%%\n",
  4519.       font);
  4520.   a = 0;
  4521.   k = 1;
  4522.   c = 1;
  4523.   i = (mapwidth + hres - 1) / hres;
  4524.   FORLIM = i;
  4525.   for (b = 1; b <= FORLIM; b++) {
  4526.     if (a != k) {
  4527.       fprintf(texfile, "\\%s%c ", font, fontpre[k - 1]);
  4528.       a = k;
  4529.     }
  4530.     putc(cref[c - 1], texfile);
  4531.     c++;
  4532.     if (c > charpf) {
  4533.       k++;
  4534.       c = 1;
  4535.     }
  4536.   }
  4537.   fprintf(texfile, "}}\n");   /*:118*/
  4538.   /*119:*/
  4539.   fprintf(texfile, "\\%swd=\\wd\\%sbox\n", font, font);
  4540.   fprintf(texfile,
  4541.       "\\setbox\\%sbox=\\hbox{\\vbox{\\hsize=\\%swd\n",
  4542.       font, font);
  4543.   fprintf(texfile,
  4544.       "\\parskip=0pt\\offinterlineskip\\parindent0pt\n");
  4545.   v = 1;
  4546.   k = 1;
  4547.   c = 1;
  4548.   FORLIM = j;
  4549.   for (a = 0; a < FORLIM; a++) {
  4550.     fprintf(texfile, "\\hbox{\\%s%c ", font, fontpre[k - 1]);
  4551.     v = k;
  4552.     FORLIM2 = i;
  4553.     for (b = 0; b < FORLIM2; b++) {
  4554.       if (v != k) {
  4555.     fprintf(texfile, "\\%s%c ", font, fontpre[k - 1]);
  4556.     v = k;
  4557.       }
  4558.       putc(cref[c - 1], texfile);
  4559.       c++;
  4560.       if (c > charpf) {
  4561.     k++;
  4562.     c = 1;
  4563.       }
  4564.     }
  4565.     if (a < j - 1)
  4566.       fprintf(texfile, "}\n");
  4567.   }
  4568.   fprintf(texfile, "}}}\n");
  4569.   fprintf(texfile, "\\ifx\\parbox\\undefined\n");
  4570.   fprintf(texfile, "    \\def\\set%s{\\box\\%sbox}\n",
  4571.       font, font);
  4572.   fprintf(texfile, "\\else\n");
  4573.   fprintf(texfile, "    \\def\\set%s{\\parbox{\\wd\\%sbox}",
  4574.       font, font);
  4575.   fprintf(texfile, "{\\box\\%sbox}}\n", font);
  4576.   fprintf(texfile, "\\fi\n");
  4577.   if (texfile != NULL)
  4578.     fclose(texfile);
  4579.   texfile = NULL;   /*:119*/
  4580.   printf("TeX Font Metric files written, generating fonts at ");
  4581.   printf("%ld dpi\n", (long)floor(truehres + 0.5));
  4582.   maxx = (mapwidth + hres - 1) / hres;
  4583.   f = 0;
  4584.   g = 0;   /*if pxlmod then prepxlfile else*/
  4585.   pxlbmi = 4;
  4586.   if (fok) {
  4587.     printf("error occurred when allocating pxlfile ");
  4588.     puts(cmd);
  4589.     fileproblem();
  4590.     goto _L9999;
  4591.   }
  4592.   if (bitmap != NULL)
  4593.     fclose(bitmap);
  4594.   bitmap = NULL;
  4595.   if (bitmap != NULL)
  4596.     bitmap = freopen(bitmap_NAME, "rb", bitmap);
  4597.   else
  4598.     bitmap = fopen(bitmap_NAME, "rb");
  4599.   if (bitmap == NULL)
  4600.     _EscIO(FileNotFound);
  4601.   fonteject();   /*108:*/
  4602.   if (pcxtemp)
  4603. /* p2c: bm2font.p, line 1305: 
  4604.  * Note: Called procedure Exec [285] */
  4605.     Exec("rm", "pcxtemp.tmp");
  4606.   if (giftemp)
  4607. /* p2c: bm2font.p, line 1305: 
  4608.  * Note: Called procedure Exec [285] */
  4609.     Exec("rm", "giftemp.tmp");
  4610.   if (greypxl)
  4611. /* p2c: bm2font.p, line 1305: 
  4612.  * Note: Called procedure Exec [285] */
  4613.     Exec("rm", tmpname);
  4614.   printf("input %s.tex into your document", font);
  4615.   printf(" and set the picture with \\set%s\n", font);
  4616.       /*:108*/
  4617. _L9999:
  4618.   if (texfile != NULL)
  4619.     fclose(texfile);
  4620.   if (bitmap != NULL)
  4621.     fclose(bitmap);
  4622.   if (pxlfile != NULL)
  4623.     fclose(pxlfile);
  4624.   if (tmpfil != NULL)
  4625.     fclose(tmpfil);
  4626.   if (tfmfile != NULL)
  4627.     fclose(tfmfile);
  4628.   exit(EXIT_SUCCESS);
  4629. }  /*:105*/
  4630.  
  4631.  
  4632.  
  4633. /* End. */
  4634.