home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / ppm / xvminitoppm.c < prev    next >
C/C++ Source or Header  |  1986-01-28  |  3KB  |  98 lines

  1. /* xvminitoppm - convert XV thumbnail picture to PPM
  2. **
  3. ** Copyright (C) 1993 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "ppm.h"
  14. #define BUFSIZE 256
  15.  
  16. static void getline ARGS((FILE *fp, char *buf));
  17.  
  18. int 
  19. main(argc, argv)
  20.     int argc;
  21.     char *argv[];
  22. {
  23.     unsigned int brow[256], rrow[256], grow[256];
  24.     char buf[BUFSIZE];
  25.     int r, g, b, i;
  26.     pixel *pixrow;
  27.     FILE *ifp;
  28.     char *usage = "[xvminipic]";
  29.     int cols, rows, maxval, col, row;
  30.  
  31.     ppm_init(&argc, argv);
  32.  
  33.     if( argc > 2 ) 
  34.         pm_usage(usage);
  35.     if( argc == 2 ) 
  36.         ifp = pm_openr(argv[1]);
  37.     else
  38.         ifp = stdin;
  39.  
  40.     i = 0;
  41.     for( r = 0; r < 8; r++ ) 
  42.         for( g = 0; g < 8; g++ )
  43.             for( b = 0; b < 4; b++ ) {
  44.                 rrow[i] = (r*255)/7;
  45.                 grow[i] = (g*255)/7;
  46.                 brow[i] = (b*255)/3;
  47.                 i++;
  48.             }
  49.     
  50.     getline(ifp, buf);
  51.     if( strncmp(buf, "P7 332", 6) != 0 )
  52.         pm_error("bad magic number - not a XV thumbnail picture");
  53.  
  54.     while(1) {
  55.         getline(ifp, buf);
  56.         if( strncmp(buf, "#END_OF_COMMENTS", 16)==0 )
  57.             break;
  58.         if( strncmp(buf, "#BUILTIN", 8)==0 )
  59.             pm_error("cannot convert builtin XV thumbnail pictures");
  60.     }
  61.     getline(ifp, buf);
  62.     if( sscanf(buf, "%d %d %d", &cols, &rows, &maxval) != 3 ) 
  63.         pm_error("error parsing dimension info");
  64.     if( maxval != 255 )
  65.         pm_error("bogus XV thumbnail maxval");
  66.  
  67.     pixrow = ppm_allocrow(cols);
  68.     ppm_writeppminit(stdout, cols, rows, (pixval)maxval, 0);
  69.  
  70.     for( row = 0; row < rows; row++ ) {
  71.         for( col = 0; col < cols; col++ ) {
  72.             int byte;
  73.             byte = fgetc(ifp);
  74.             if( byte == EOF )
  75.                 pm_error("unexpected EOF");
  76.  
  77.                 PPM_ASSIGN(pixrow[col], rrow[byte], grow[byte], brow[byte]);
  78.     }
  79.         ppm_writeppmrow(stdout, pixrow, cols, (pixval)maxval, 0);
  80.     }
  81.     pm_close(ifp);
  82.     exit(0);
  83. }
  84.  
  85.  
  86. static void
  87. getline(fp, buf)
  88.     FILE *fp;
  89.     char *buf;
  90. {
  91.     if( fgets(buf, BUFSIZE, fp) == NULL ) {
  92.         if( ferror(fp) )
  93.             pm_perror("read error");
  94.         else
  95.             pm_error("unexpected EOF");
  96.     }
  97. }
  98.