home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pgm / bioradtopgm.c < prev    next >
C/C++ Source or Header  |  1994-01-27  |  4KB  |  157 lines

  1. /* bioradtopgm.c - convert a Biorad confocal image into a portable graymap
  2. **
  3. ** Copyright (C) 1993 by Oliver Trepte, oliver@fysik4.kth.se
  4. **
  5. ** Derived from the pbmplus package,
  6. ** Copyright (C) 1989 by Jef Poskanzer.
  7. **
  8. ** Permission to use, copy, modify, and distribute this software and its
  9. ** documentation for any purpose and without fee is hereby granted, provided
  10. ** that the above copyright notice appear in all copies and that both that
  11. ** copyright notice and this permission notice appear in supporting
  12. ** documentation.  This software is provided "as is" without express or
  13. ** implied warranty.
  14. */
  15.  
  16. #include <ctype.h>
  17. #include "pgm.h"
  18.  
  19. #define BYTE unsigned char
  20. #define BIORAD_HEADER_LENGTH 76
  21. #define BYTE_TO_WORD(lsb,msb) (((BYTE) lsb) + (((BYTE) msb) << 8))
  22.  
  23. int
  24. main( argc, argv )
  25.     int argc;
  26.     char* argv[];
  27.     {
  28.     FILE* ifp;
  29.     gray* grayrow;
  30.     register gray* gP;
  31.     int argn, row, i;
  32.     float rowskip, toskip;
  33.     register int col, val, val2;
  34.     int rows=0, cols=0, image_num= -1, image_count, byte_word, check_word;
  35.     int maxval;
  36.     BYTE buf[BIORAD_HEADER_LENGTH];
  37.     char* pos;
  38.     long nread = 0;
  39.     char* usage = "[-image#] [Bioradfile]";
  40.  
  41.  
  42.     pgm_init( &argc, argv );
  43.  
  44.     argn = 1;
  45.  
  46.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  47.     {
  48.     if ( isdigit( argv[argn][1] ))
  49.     {
  50.         image_num = atoi( (argv[argn]+1) );
  51.     }
  52.     else
  53.         pm_usage( usage );
  54.     ++argn;
  55.     }
  56.  
  57.     if ( argn < argc )
  58.     {
  59.     ifp = pm_openr( argv[argn] );
  60.     ++argn;
  61.     }
  62.     else
  63.     ifp = stdin;
  64.  
  65.     if ( argn != argc )
  66.     pm_usage( usage );
  67.  
  68.     for ( i = 0; i < BIORAD_HEADER_LENGTH; ++i )
  69.     {
  70.     val = getc( ifp );
  71.     if ( val == EOF )
  72.         pm_error( "EOF / read error" );
  73.     buf[ i ] = val;
  74.     }
  75.  
  76.     cols = BYTE_TO_WORD(buf[0], buf[1]);
  77.     rows = BYTE_TO_WORD(buf[2], buf[3]);
  78.     image_count = BYTE_TO_WORD(buf[4], buf[5]);
  79.     byte_word = BYTE_TO_WORD(buf[14], buf[15]);
  80.     check_word = BYTE_TO_WORD(buf[54], buf[55]);
  81.  
  82.     if ( check_word != 12345 )
  83.     pm_error( "Not a Biorad file" );
  84.  
  85.     if ( cols <= 0 )
  86.     pm_error( "Strange image size, cols = %d", cols);
  87.  
  88.     if ( rows <= 0 )
  89.     pm_error( "Strange image size, rows = %d", rows);
  90.  
  91.     if ( image_count <= 0 )
  92.     pm_error( "Number of images in file is %d", image_count);
  93.  
  94.     if ( byte_word )
  95.     maxval = 255;
  96.     else
  97.     {
  98.     maxval = 65535;   /* Perhaps this should be something else */
  99.  
  100. #ifndef PGM_BIGGRAYS
  101.     pm_error( "Input image is a word image, PbmPlus is compiled for bytes only" );
  102. #endif
  103.     }
  104.  
  105.     pm_message( "Image size: %d cols, %d rows", cols, rows);
  106.     pm_message( "%s",
  107.            (byte_word) ? "Byte image (8 bits)" : "Word image (16 bits)");
  108.  
  109.     if ( image_num < 0 )
  110.     pm_message( "Input contains %d image%c",
  111.            image_count, (image_count > 1) ? 's' : '\0');
  112.     else
  113.     {
  114.     if ( image_num >= image_count )
  115.         pm_error( "Cannot extract image %d, input contains only %d image%s",
  116.              image_num, image_count, (image_count > 1) ? "s" : "" );
  117.     for ( i = (byte_word) ? image_num : image_num*2 ; i > 0 ; --i ) {
  118.         for ( row = 0; row < rows; ++row)
  119.         for ( col = 0; col < cols; ++col )
  120.         {
  121.             val = getc( ifp );
  122.             if ( val == EOF ) {
  123.             pm_error( "EOF / read error" );
  124.             }
  125.         }
  126.     }
  127.  
  128.     pgm_writepgminit( stdout, cols, rows, (gray) maxval, 0 );
  129.     grayrow = pgm_allocrow( cols );
  130.  
  131.     for ( row = 0; row < rows; ++row)
  132.     {
  133.         for ( col = 0, gP = grayrow; col < cols; ++col )
  134.         {
  135.         val = getc( ifp );
  136.         if ( val == EOF )
  137.             pm_error( "EOF / read error" );
  138.         if (byte_word)
  139.             *gP++ = val;
  140.         else
  141.         {
  142.             val2 = getc( ifp );
  143.             if ( val2 == EOF )
  144.             pm_error( "EOF / read error" );
  145.             *gP++ = BYTE_TO_WORD(val, val2);
  146.         }
  147.         }
  148.         pgm_writepgmrow( stdout, grayrow, cols, (gray) maxval, 0 );
  149.     }
  150.  
  151.     pm_close( ifp );
  152.     pm_close( stdout );
  153.  
  154.     }
  155.     exit( 0 );
  156. }
  157.