home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / program / asm / RTGRAF.ZIP / MAP.ZIP / CPAINT.C next >
Encoding:
C/C++ Source or Header  |  1990-06-19  |  8.3 KB  |  204 lines

  1. /*===========================================================================*\
  2. ||  CPAINT                                                 Rip Toren         ||
  3. ||                                                             POB 674       ||
  4. ||                                                         Columbia, MD 21045||
  5. ||                                                                           ||
  6. ||  Translate a file created by Microsoft (Zsoft) PcPaint into binary        ||
  7. ||     image files. These files are used by RTGRAF display system.           ||
  8. ||                                                                           ||
  9. ||  input : argv[1] = file name to process                                   ||
  10. ||  output: file with name of argv[1], but extension '.BMP'                  ||
  11. ||                                                                           ||
  12. \*===========================================================================*/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <malloc.h>
  17.  
  18. typedef unsigned char byte;
  19.  
  20.        /*--------------------------------------------------------------------*\
  21.        |   Structure definition for first 128 bytes of .PCC or .PCX file      |
  22.        \*--------------------------------------------------------------------*/
  23. struct header {
  24.        byte    manuf;             /* manufacturer               */
  25.        byte    version;           /* version number             */
  26.        byte    rllenc;            /* run-length packing turned on       */
  27.        byte    bits_p_pixel;      /* bit per pixel              */
  28.        int     x1,y1,x2,y2;       /* cooridinated to top/left bot/rite  */
  29.        int     horiz_res, vert_res;/* resolution of producing screen     */
  30.        byte    palette[48];       /* beats me ???               */
  31.        byte    vmode;
  32.        byte    planes;            /* number of planes in picture        */
  33.        int     bytes_p_line;      /* packed bytes per scan line         */
  34.        byte    filler [200];
  35.        };
  36.  
  37. FILE *mppic,*mpres;               /* input and output files         */
  38. FILE *res[4];                     /* a file for each possible plane     */
  39. char buff [512];
  40. char drive[_MAX_DRIVE],
  41.      dir  [_MAX_DIR  ],
  42.      name [_MAX_FNAME],
  43.      ext  [_MAX_EXT  ];
  44.  
  45.                    /*     0   1    2    3    4    5    6    7    */
  46. static char mask[8]  ={ 0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe};
  47.                       /* names of potential outputs         */
  48. static char *dfname[] = {"p__1.dat","p__2.dat","p__3.dat","p__4.dat"};
  49. static char null_f[] = "NUL";          /* name for non-used planes           */
  50.  
  51.  
  52. int encget (int *pbyt,FILE *fid);
  53.  
  54.  
  55. /*---------------------------------------------------------------------------*\
  56. |  **************************** M A I N **********************************    |
  57. \*---------------------------------------------------------------------------*/
  58. int
  59. main (argc,argv)
  60. int argc;
  61. char *argv[];
  62.   {
  63.    struct header mp_hdr;
  64.    unsigned char  c, *fb;
  65.    int i, j, k, chr, rows,max_planes,plane,bytes,bits;
  66.    unsigned int rc;
  67.    long byte_cnt;
  68.  
  69.    if (argc < 2)  {
  70.        puts ("I can't read minds, how about a file name!");
  71.        exit(1);
  72.        }
  73.    /*------------------------------------------------------------------------*\
  74.    |   open input file                                                        |
  75.    \*------------------------------------------------------------------------*/
  76.    mppic = fopen (argv[1],"rb");
  77.    if (mppic==NULL)  {
  78.        puts ("open failed on pic file ");
  79.        exit (1);
  80.        }
  81.    puts ("Open good");
  82.    _splitpath (argv[1],drive,dir,name,ext);
  83.  
  84.    /*------------------------------------------------------------------------*\
  85.    |   get and display file header                                            |
  86.    \*------------------------------------------------------------------------*/
  87.    fread ((char*)&mp_hdr, 1, 128, mppic);
  88.    printf ("Manufacturer      = %d\n",(int) mp_hdr.manuf            );
  89.    printf ("Version is        = %d\n",(int) mp_hdr.version          );
  90.    printf ("Run length is     = %d\n",(int) mp_hdr.rllenc           );
  91.    printf ("Bits / pixel      = %d\n",(int) mp_hdr.bits_p_pixel     );
  92.    printf ("Coordinate x1     = %d\n",(int) mp_hdr.x1               );
  93.    printf ("Coordinate y1     = %d\n",(int) mp_hdr.y1               );
  94.    printf ("Coordinate x2     = %d\n",(int) mp_hdr.x2               );
  95.    printf ("Coordinate y2     = %d\n",(int) mp_hdr.y2               );
  96.    printf ("Horizontal res    = %d\n",(int) mp_hdr.horiz_res        );
  97.    printf ("Vertical   res    = %d\n",(int) mp_hdr.vert_res         );
  98.    printf ("Planes            = %d\n",(int) mp_hdr.planes           );
  99.    max_planes                 =       (int) mp_hdr.planes            ;
  100.    printf ("Bytes / line      = %d\n",(int) mp_hdr.bytes_p_line     );
  101.  
  102.                       /* allocate space for one scan line   */
  103.    fb = malloc ((int)mp_hdr.bytes_p_line);
  104.    if (fb==NULL)  {
  105.        puts("malloc failure");
  106.        exit (-1);
  107.        }
  108.  
  109.    /*------------------------------------------------------------------------*\
  110.    |   Prime each of the files                                                |
  111.    \*------------------------------------------------------------------------*/
  112.    for (i=0; i<min(4,max_planes); i++)
  113.        {
  114.        res[i] = fopen (dfname[i],"w+b");
  115.        }
  116.  
  117.    byte_cnt = 0;
  118.                       /* bits per row               */
  119.    bits = (mp_hdr.x2 - mp_hdr.x1) + 1;
  120.                       /* row in image               */
  121.    rows = (mp_hdr.y2 - mp_hdr.y1) + 1;
  122.                       /* number of bytes in row (packed)    */
  123.  
  124.    for (i=0; i<rows; i++)  {
  125.        for (plane=0; plane < mp_hdr.planes; plane++)  {
  126.            k = bits;
  127.            for (bytes=0; bytes < mp_hdr.bytes_p_line; bytes++)  {
  128.                if (EOF != encget (&chr, mppic))  {
  129.                    byte_cnt ++;
  130.                    c = (unsigned char) chr;
  131.                            /* clear last few bits if not in image*/
  132.                    if (k<8) c &= mask[k];
  133.                    *(fb+bytes) = c;    /* store in save array            */
  134.                            /* indicate a non-blank row       */
  135.                    k -= 8;         /* decrem bit count           */
  136.                    if (k<0 ) k=0;
  137.                    }
  138.                }
  139.            for (j=0;j< mp_hdr.bytes_p_line;j++)
  140.                fputc (*(fb+j),res[plane]);
  141.            }
  142.        }
  143.    printf (" BYTE COUNT       = %ld\n",byte_cnt);
  144.    fclose (mpres);
  145.  
  146.    for (i=0; i<min(4,(int)mp_hdr.planes); i++) {
  147.        fseek (res[i],0L,0);  //rewind each
  148.        }
  149.    fclose (mppic);
  150. //
  151. //
  152.    strcpy (ext,".bmp");
  153.    _makepath (buff,drive,dir,name,ext);
  154.  
  155.    if ((mpres=fopen(buff,"wb"))== NULL)  {
  156.        puts ("error on open of result");
  157.        perror ("result file");
  158.        exit (-1);
  159.        }
  160.    fwrite ((char*)&bits, sizeof(int), 1, mpres);
  161.    fwrite ((char*)&rows, sizeof(int), 1, mpres);
  162.  
  163.    for (i=0; i<min(4,(int)mp_hdr.planes); i++) {
  164.        do  {
  165.            rc = fread (buff,1,512,res[i]);  //get some
  166.            if (rc > 0)  {
  167.                fwrite (buff,1,rc,mpres);
  168.                printf ("wrote %u bytes from plane %d\n",rc,i);
  169.                }
  170.            } while ( rc > 0);
  171.        }
  172.    fclose (mpres);
  173.    puts ("Conversion complete");
  174.    return 0;
  175.    }
  176.  
  177. /*===========================================================================*\
  178. || Get the bytes from the file.  If bits 6 & 7 are set, this is a count      ||
  179. || of repetitions of following byte.                                         ||
  180. ||                                                                           ||
  181. \*===========================================================================*/
  182. int
  183. encget (pbyt, fid)
  184. int *pbyt;
  185. FILE *fid;
  186.   {
  187.   static int cnt=0, i;
  188.    if (cnt > 0)  {            /* return the same byte again         */
  189.        cnt --;
  190.        *pbyt = i;
  191.        return (0);
  192.        }
  193.  
  194.                       /* no more characters             */
  195.    if (EOF== (i=fgetc(fid))) return (EOF);
  196.  
  197.    if (0xC0 == (0xC0 & i))  {         /* it is a repeat count           */
  198.        cnt = (0x3F & i)-1;
  199.        if (EOF == (i= fgetc(fid))) return (EOF);
  200.        }
  201.    *pbyt = i;                 /* return the byte read           */
  202.    return (0);
  203.    }
  204.