home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsm / netpbmsca / pbm / c / pi3topbm < prev    next >
Encoding:
Text File  |  1993-10-04  |  2.2 KB  |  96 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. /*
  28.  * File header structure
  29.  */
  30. struct header {
  31.     short        res;        /* resolution */
  32.     short        pal[16];    /* palette */
  33. };
  34.  
  35. int
  36. main(argc, argv)
  37.     int             argc;
  38.     char           *argv[];
  39. {
  40.     int             debug = 0;
  41.     FILE           *f;
  42.     struct header   hd;
  43.     int             x;
  44.     int             i, k;
  45.     int             c;
  46.     int        rows, cols;
  47.     bit        *bitrow;
  48.  
  49.     pbm_init( &argc, argv );
  50.  
  51.     if (argc > 1 && !strcmp(argv[1], "-d")) {
  52.         --argc;
  53.         argv[1] = argv[0];
  54.         ++argv;
  55.         debug = 1;
  56.     }
  57.  
  58.     if (argc == 1)
  59.         f = stdin;
  60.     else if (argc == 2)
  61.         f = pm_openr( argv[1] );
  62.     else
  63.         pm_usage("[-d] [pi3file]");
  64.  
  65.     if (fread(&hd, sizeof hd, 1, f) != 1)
  66.         pm_perror( "read" );
  67.  
  68.     if (debug)
  69.         pm_message( "resolution is %d", hd.res );
  70.  
  71.     /* only handles hi-rez 640x400 */
  72.     if (hd.res != 2)
  73.         pm_error( "bad resolution" );
  74.  
  75.     cols = 640;
  76.     rows = 400;
  77.     pbm_writepbminit( stdout, cols, rows, 0 );
  78.     bitrow = pbm_allocrow( cols );
  79.  
  80.     for (i = 0; i < rows; ++i) {
  81.         x = 0;
  82.         while (x < cols) {
  83.             if ((c = getc(f)) == EOF)
  84.                 pm_error( "end of file reached" );
  85.             for (k = 0x80; k; k >>= 1) {
  86.                 bitrow[x] = (k & c) ? PBM_BLACK : PBM_WHITE;
  87.                 ++x;
  88.             }
  89.         }
  90.         pbm_writepbmrow( stdout, bitrow, cols, 0 );
  91.     }
  92.     pm_close( f );
  93.     pm_close( stdout );
  94.     exit(0);
  95. }
  96.