home *** CD-ROM | disk | FTP | other *** search
/ The Equalizer BBS / equalizer-bbs-collection_2004.zip / equalizer-bbs-collection / DEMOSCENE-STUFF / BKISSSRC.ZIP / PCX2FNT.C < prev    next >
C/C++ Source or Header  |  1994-01-30  |  12KB  |  312 lines

  1. /**************************************************************************
  2.  PCX2FNT - by Lee Hamel (Patch), hamell@cx.pdx.edu, *Avalanche* coder
  3.  July 14th, 1993
  4. **************************************************************************/
  5. #if defined(__SMALL__) || defined(__TINY__)
  6. #error Cannot be compiled as small or tiny
  7. #endif
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <malloc.h>
  13.  
  14. struct PCXHEAD {
  15.     char    manufacturer;
  16.     char    version;
  17.     char    encoding;
  18.     char    bits_per_pixel;
  19.     int     xmin,ymin;
  20.     int     xmax,ymax;
  21.     int     hres;
  22.     int     vres;
  23.     char    palette[48];
  24.     char    reserved;
  25.     char    colour_planes;
  26.     int     bytes_per_line;
  27.     int     palette_type;
  28.     char    filler[58];
  29. } header;
  30. unsigned int width, depth, bytes, i, i1, i2, i3, charcount = 0;
  31. unsigned char palette[768];
  32. unsigned char charrows, charsinrow[20];
  33. unsigned char buffer[256], charorder[256], xysizes[512], showflag;
  34. FILE *infile, *outfile;
  35. unsigned char *tempptr, *charptr, *rowptr, *picture, *fonts[256];
  36. char pcxfile[20],cfgfile[20],fntfile[20];
  37. /****************************************************************************/
  38. void Read_PCX_Line(unsigned int vidoffset)
  39. {
  40.     unsigned char c, run;
  41.     unsigned int n = 0;
  42.  
  43.     asm cld
  44.     asm mov         di,[vidoffset]
  45.  
  46.     do {
  47.         c = fgetc(infile) & 0xff;
  48.         if ((c & 0xc0) == 0xc0) {           /* if it's a run of bytes field */
  49.  
  50.             run = c & 0x3f;                   /* and off the high bits */
  51.             c = fgetc(infile);                /* get the run byte */
  52.             n += run;                         /* run the byte */
  53.             buffer[c] = 1;
  54.             for (i2 = 0; i2 < run; i2++) *picture++ = c;
  55.  
  56.             if (showflag) {
  57.                 asm mov     ax,0a000h
  58.                 asm mov     es,ax
  59.                 asm mov     al,[c]
  60.                 asm xor     ch,ch
  61.                 asm mov     cl,[run]
  62.                 asm rep     stosb
  63.  
  64.             }
  65.         } else {
  66.             n++;
  67.             buffer[c] = 1;
  68.             *picture++ = c;
  69.  
  70.             if (showflag) {
  71.                 asm mov     ax,0a000h
  72.                 asm mov     es,ax
  73.                 asm mov     al,[c]
  74.                 asm stosb
  75.             }
  76.         }
  77.     }
  78.     while (n < bytes);
  79. }
  80. /****************************************************************************/
  81. void Unpack_PCX_File(void)
  82. {
  83.     if ((infile = fopen(pcxfile,"rb")) != NULL) {
  84.         if (fread((char *)&header,1,sizeof(struct PCXHEAD),infile) == sizeof(struct PCXHEAD)) {
  85.             if (header.manufacturer == 0x0a && header.version == 5) {
  86.                 if (!fseek(infile,-769L,SEEK_END)) {
  87.                     if (fgetc(infile) == 0x0c && fread(palette,1,768,infile) == 768) {
  88.                         fseek(infile,128L,SEEK_SET);
  89.                         width = header.xmax - header.xmin + 1;
  90.                         depth = header.ymax - header.ymin + 1;
  91.                         bytes = header.bytes_per_line;
  92.  
  93.                         for (i = 0; i < 768; i++) palette[i] = palette[i] >> 2;
  94.  
  95.                         if (showflag) {
  96.                             asm mov     ax,0013h
  97.                             asm int     10h
  98.                             asm mov     ax,1012h
  99.                             asm xor     bx,bx
  100.                             asm mov     cx,256
  101.                             asm mov     dx,offset palette
  102.                             asm int     10h
  103.                         }
  104.  
  105.                         if ((rowptr = picture = (unsigned char *) malloc(64000)) == NULL) {
  106.                             printf("Insufficient memory\n");
  107.                             exit(0);
  108.                         }
  109.  
  110.                         for (i = 0; i < depth; i++) Read_PCX_Line(i * 320);
  111.  
  112.                         if (showflag) {
  113.                             asm xor     ax,ax
  114.                             asm int     16h
  115.                             asm mov     ax,0003h
  116.                             asm int     10h
  117.                         }
  118.                     } else {printf("Error reading palette\n"); exit(0);}
  119.                 } else {printf("Error seeking to palette\n"); exit(0);}
  120.             } else {printf("Not a 256 color PCX file\n"); exit(0);}
  121.         } else {printf("Error reading %s\n",pcxfile); exit(0);}
  122.         fclose(infile);
  123.     } else {printf("Error opening %s\n",pcxfile); exit(0);}
  124. }
  125. /****************************************************************************/
  126. void Get_CFG_File(char *cfgfile)
  127. {
  128.     char tempstr[80],temp1[5],temp2[5],num[5];
  129.     FILE *fp;
  130.  
  131.     for (i = 0; i < 512; i++) xysizes[i] = 0;
  132.  
  133.     i = 0;
  134.     if ((fp = fopen(cfgfile,"rt")) != NULL) {
  135.         do {
  136.             fgets(tempstr,80,fp);
  137.         } while (tempstr[0] == ';' || tempstr[0] == '\n');
  138.  
  139.         sscanf(tempstr,"%s = %d",tempptr,& charrows);
  140.         fgets(tempstr,80,fp);
  141.         tempptr = strchr(tempstr,'=');
  142.         tempptr += 2;
  143.  
  144.         for (i = 0; i < charrows; i++) {
  145.             sscanf(tempptr,"%d",& charsinrow[i]);
  146.  
  147.             while (*tempptr != ' ') tempptr++;
  148.             tempptr++;
  149.         }
  150.  
  151.         i = 0;
  152.         while (fgets(tempstr,80,fp) != NULL) {
  153.             if (tempstr[0] != ';' && tempstr[0] != '\n') {
  154.                 if (sscanf(tempstr,"%s = %s x %s",num,temp1,temp2) != -1) {
  155.                     charorder[i++] = atoi(num);
  156.                     charcount++;
  157.                     xysizes[2*atoi(num) + 0] = atoi(temp1);
  158.                     xysizes[2*atoi(num) + 1] = atoi(temp2);
  159.                 }
  160.             }
  161.         }
  162.         fclose(fp);
  163.     } else {
  164.         fcloseall();
  165.         printf("Could not open file %s\n",cfgfile);
  166.         exit(1);
  167.     }
  168. }
  169. /****************************************************************************/
  170. void Dump_Fonts(void)
  171. {
  172.     unsigned int ord = 0, xsize, ysize, highesty;
  173.  
  174.     for (i = 0; i < charrows; i++) {
  175.         /* reset pointer to UL corner of character row */
  176.         /* +320 to skip over blank row */
  177.  
  178.         highesty = 0;                       /* longest font initialize */
  179.         rowptr += 320;
  180.         picture = rowptr;
  181.  
  182.         for (i3 = 0; i3 < charsinrow[i]; i3++) {
  183.             /* UL corner of char */
  184.             charptr = picture;
  185.             xsize = xysizes[charorder[ord]*2];
  186.             ysize = xysizes[(charorder[ord]*2)+1];
  187.  
  188.             if (ysize > highesty) highesty = ysize;
  189.  
  190.             tempptr = fonts[charorder[ord]] = \
  191.                     (unsigned char *) malloc((size_t) xsize * ysize);
  192.  
  193.             for (i1 = 0; i1 < xsize; i1++)    /* X value */ {
  194.                 for (i2 = 0; i2 < ysize; i2++)  /* Y value */ {
  195.                     *tempptr++ = *picture;
  196.                     picture += 320;
  197.                 }
  198.                 picture++;                      /* next column over */
  199.                 picture -= 320*ysize;           /* back to top of column */
  200.             }
  201.  
  202.             ord++;                            /* next char in order */
  203.             picture = charptr + xsize + 1;    /* jump to next UL corner */
  204.         }
  205.  
  206.         /* Jump to start of next char row */
  207.         rowptr += (320 * highesty);
  208.     }
  209.  
  210.     i1 = 515;
  211.     for (i = 0; i < 256; i++) {
  212.         xsize = xysizes[i*2];
  213.         ysize = xysizes[(i*2)+1];
  214.         fwrite(&i1, 2, 1, outfile);
  215.         i1 += (xysizes[i * 2] * xysizes[(i * 2) + 1]) + 3;
  216.     }
  217.  
  218.     for (i = 0; i < 256; i++) {
  219.         xsize = xysizes[i*2];
  220.         ysize = xysizes[(i*2)+1];
  221.         if (xsize == 0 && ysize == 0) {
  222.             fprintf(outfile,"%c%c%c",i,0,0);
  223.         } else {
  224.             fprintf(outfile,"%c%c%c",i,xsize,ysize);
  225.             for (i1 = 0, charptr = fonts[i]; i1 < xsize * ysize; i1++) \
  226.                     fprintf(outfile,"%c",*charptr++);
  227.         }
  228.     }
  229.  
  230.     for (i = 0; i < charcount; i++) free((unsigned char *) fonts[i]);
  231. }
  232. /****************************************************************************/
  233. void Help(char helptype)
  234. {
  235.     if (helptype == 1) {
  236.         printf("FNT format\n");
  237.         printf("──────────\n");
  238.         printf("byte 0   - 2  : string signature 'JLF' (for error checking)\n");
  239.         printf("word 3   - 514: offset of all 256 ASCII chars, relative to byte 0\n");
  240.         printf("byte 515 -    : data for the fonts\n");
  241.         printf("                1st byte is the char in the ASCII table\n");
  242.         printf("                2nd byte is the X size (width)\n");
  243.         printf("                3rd byte is the Y size (height)\n");
  244.         printf("                X*Y bytes of the font - saved column by column\n");
  245.         printf("                       top to bottom, left to right\n\n");
  246.         printf("Fonts are expected to be in the following format:\n");
  247.         printf("┌─┬─┬─┬─┬─┬──┬────  Note the 0s along the top row and far right column.\n");
  248.         printf("0 0 0 0 0 0 ─┤      This is an example of the pixel placement of an A.\n");
  249.         printf("0 1 1 1 0 0 ─┤      The 1s represent where the different colors are at.\n");
  250.         printf("1 1 1 1 1 0 ─┤      When drawing the fonts, make sure each lettter has a\n");
  251.         printf("1 1 0 1 1 0 ─┤      border along the top side, otherwise the conversion\n");
  252.         printf("1 1 1 1 1 0 ─┤      algorithm will mess up.  Just stack all of your fonts\n");
  253.         printf("1 1 0 1 1 0 ─┤      on top of each other and side by side, save the picture\n");
  254.         printf("1 1 0 1 1 0 ─┤      as a PCX, and you're done!\n");
  255.     } else {
  256.         printf("Usage: pcx2fnt [-FNT] PCXFILE [-SHOW]\n");
  257.         printf("where: -FNT       - show the FNT format\n");
  258.         printf("       PCXFILE    - the PCX file to read (no extension)\n");
  259.         printf("       -SHOW      - show the PCX on the screen\n\n");
  260.         printf("Example call: pcx2fnt fontfile -show\n");
  261.         printf("- This will read the file FONTFILE, show it to the screen, and\n");
  262.         printf("  will read the configuration information from FONTFILE.CFG.\n\n");
  263.         printf("Example FONTFILE.CFG:\n");
  264.         printf("charrows = 4\n");
  265.         printf("charsinrow = 3 4 5 6\n");
  266.         printf("065 = 10 x 10\n\n");
  267.         printf("The program expects 4 rows of fonts with 3 fonts in the 1st row, 4 in the 2nd,\n");
  268.         printf("5 in the 3rd, and 6 in the 4th.  065 represents ASCII char A, and it is a\n");
  269.         printf("10 x 10 bitmap.  The file FONTFILE.FNT will be created.  Use a paint\n");
  270.         printf("program to figure out the pixel count.  The blank border along the top and\n");
  271.         printf("right of each font DOES NOT count as part of the height or width!\n");
  272.     }
  273.     exit(1);
  274. }
  275. /****************************************************************************/
  276. void main(int argc, char *argv[])
  277. {
  278.     printf("PCX2FNT - Converts a PCX pic to FNT format\n");
  279.     printf("    by Patch (hamell@rigel.cs.pdx.edu)    \n");
  280.     printf("──────────────────────────────────────────\n");
  281.  
  282.     if (stricmp("-FNT",argv[1]) == 0) Help(1);
  283.     if (argc == 1) Help(0);
  284.     strcpy(pcxfile,argv[1]);
  285.     strcpy(cfgfile,argv[1]);
  286.     strcpy(fntfile,argv[1]);
  287.     strcat(pcxfile,".pcx");
  288.     strcat(cfgfile,".cfg");
  289.     strcat(fntfile,".jlf");
  290.     showflag = (stricmp("-SHOW",argv[2]) == 0);
  291.     printf("PCX file:  %s\n",strupr(pcxfile));
  292.     printf("CFG file:  %s\n",strupr(cfgfile));
  293.     printf("JLF file:  %s\n",strupr(fntfile));
  294.     printf("Show PCX:  %s\n\n",(showflag ? "yes" : "no"));
  295.  
  296.     printf("Reading config file...\n");
  297.     Get_CFG_File(cfgfile);
  298.  
  299.     printf("Loading PCX file...\n");
  300.     Unpack_PCX_File();
  301.  
  302.     printf("Converting...\n");
  303.     if ((outfile = fopen(fntfile,"wb")) == NULL) {printf("Error opening output file.\n"); exit(0); }
  304.     fprintf(outfile,"JLF");
  305.     Dump_Fonts();
  306.     fclose(outfile);
  307.  
  308.     free((unsigned char *) picture);
  309.     exit(0);
  310. }
  311. /****************************************************************************/
  312.