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