home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pbm / pi3topbm.c < prev    next >
C/C++ Source or Header  |  1994-01-31  |  3KB  |  113 lines

  1. /*
  2.  * Convert a ATARI Degas .pi3 file to a portable bitmap file.
  3.  *
  4.  * Author: David Beckemeyer
  5.  *
  6.  * This code was derived from the original gemtopbm program written
  7.  * by Diomidis D. Spinellis.
  8.  *
  9.  * (C) Copyright 1988 David Beckemeyer and Diomidis D. Spinellis.
  10.  *
  11.  * Permission to use, copy, modify, and distribute this software and its
  12.  * documentation for any purpose and without fee is hereby granted,
  13.  * provided that the above copyright notice appear in all copies and that
  14.  * both that copyright notice and this permission notice appear in
  15.  * supporting documentation.
  16.  *
  17.  * This file is provided AS IS with no warranties of any kind.  The author
  18.  * shall have no liability with respect to the infringement of copyrights,
  19.  * trade secrets or any patents by this file or any part thereof.  In no
  20.  * event will the author be liable for any lost revenue or profits or
  21.  * other special, indirect and consequential damages.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include "pbm.h"
  26.  
  27. int
  28. main(argc, argv)
  29.     int             argc;
  30.     char           *argv[];
  31. {
  32.     int             debug = 0;
  33.     FILE           *f;
  34.     int             x;
  35.     int             i, k;
  36.     int             c;
  37.     int        rows, cols;
  38.     bit        *bitrow;
  39.     short res;
  40.     int black, white;
  41.     char *usage = "[-debug] [pi3file]";
  42.     int argn = 1;
  43.  
  44.     pbm_init( &argc, argv );
  45.  
  46.     while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0')
  47.       {
  48.         if (pm_keymatch(argv[1], "-debug", 2))
  49.           debug = 1;
  50.         else
  51.           pm_usage (usage);
  52.         ++argn;
  53.       }
  54.  
  55.     if (argn == argc)
  56.         f = stdin;
  57.     else
  58.       {
  59.         f = pm_openr (argv[argn]);
  60.         ++argn;
  61.       }
  62.  
  63.     if (argn != argc)
  64.       pm_usage (usage);
  65.  
  66.     if (pm_readbigshort (f, &res) == -1)
  67.         pm_error ("EOF / read error");
  68.  
  69.     if (debug)
  70.         pm_message ("resolution is %d", res);
  71.  
  72.     /* only handles hi-rez 640x400 */
  73.     if (res != 2)
  74.         pm_error( "bad resolution" );
  75.  
  76.     pm_readbigshort (f, &res);
  77.     if (res == 0)
  78.       {
  79.         black = PBM_WHITE;
  80.         white = PBM_BLACK;
  81.       }
  82.     else
  83.       {
  84.         black = PBM_BLACK;
  85.         white = PBM_WHITE;
  86.       }
  87.  
  88.     for (i = 1; i < 16; i++)
  89.       if (pm_readbigshort (f, &res) == -1)
  90.         pm_error ("EOF / read error");
  91.  
  92.     cols = 640;
  93.     rows = 400;
  94.     pbm_writepbminit( stdout, cols, rows, 0 );
  95.     bitrow = pbm_allocrow( cols );
  96.  
  97.     for (i = 0; i < rows; ++i) {
  98.         x = 0;
  99.         while (x < cols) {
  100.             if ((c = getc(f)) == EOF)
  101.                 pm_error( "end of file reached" );
  102.             for (k = 0x80; k; k >>= 1) {
  103.                 bitrow[x] = (k & c) ? black : white;
  104.                 ++x;
  105.             }
  106.         }
  107.         pbm_writepbmrow( stdout, bitrow, cols, 0 );
  108.     }
  109.     pm_close( f );
  110.     pm_close( stdout );
  111.     exit(0);
  112. }
  113.