home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsm / netpbmsca / pgm / c / biorad2pgm next >
Encoding:
Text File  |  1993-10-04  |  3.7 KB  |  158 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 <math.h>
  18. #include "pgm.h"
  19.  
  20. #define BYTE unsigned char
  21. #define BIORAD_HEADER_LENGTH 76
  22. #define BYTE_TO_WORD(lsb,msb) (((BYTE) lsb) + (((BYTE) msb) << 8))
  23.  
  24. int
  25. main( argc, argv )
  26.     int argc;
  27.     char* argv[];
  28.     {
  29.     FILE* ifp;
  30.     gray* grayrow;
  31.     register gray* gP;
  32.     int argn, row, i;
  33.     float rowskip, toskip;
  34.     register int col, val, val2;
  35.     int rows=0, cols=0, image_num= -1, image_count, byte_word, check_word;
  36.     int maxval;
  37.     BYTE buf[BIORAD_HEADER_LENGTH];
  38.     char* pos;
  39.     long nread = 0;
  40.     char* usage = "[-image#] [Bioradfile]";
  41.  
  42.  
  43.     pgm_init( &argc, argv );
  44.  
  45.     argn = 1;
  46.  
  47.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  48.     {
  49.     if ( isdigit( argv[argn][1] ))
  50.     {
  51.         image_num = atoi( (argv[argn]+1) );
  52.     }
  53.     else
  54.         pm_usage( usage );
  55.     ++argn;
  56.     }
  57.  
  58.     if ( argn < argc )
  59.     {
  60.     ifp = pm_openr( argv[argn] );
  61.     ++argn;
  62.     }
  63.     else
  64.     ifp = stdin;
  65.  
  66.     if ( argn != argc )
  67.     pm_usage( usage );
  68.  
  69.     for ( i = 0; i < BIORAD_HEADER_LENGTH; ++i )
  70.     {
  71.     val = getc( ifp );
  72.     if ( val == EOF )
  73.         pm_error( "EOF / read error" );
  74.     buf[ i ] = val;
  75.     }
  76.  
  77.     cols = BYTE_TO_WORD(buf[0], buf[1]);
  78.     rows = BYTE_TO_WORD(buf[2], buf[3]);
  79.     image_count = BYTE_TO_WORD(buf[4], buf[5]);
  80.     byte_word = BYTE_TO_WORD(buf[14], buf[15]);
  81.     check_word = BYTE_TO_WORD(buf[54], buf[55]);
  82.  
  83.     if ( check_word != 12345 )
  84.     pm_error( "Not a Biorad file" );
  85.  
  86.     if ( cols <= 0 )
  87.     pm_error( "Strange image size, cols = %d", cols);
  88.  
  89.     if ( rows <= 0 )
  90.     pm_error( "Strange image size, rows = %d", rows);
  91.  
  92.     if ( image_count <= 0 )
  93.     pm_error( "Number of images in file is %d", image_count);
  94.  
  95.     if ( byte_word )
  96.     maxval = 255;
  97.     else
  98.     {
  99.     maxval = 65535;   /* Perhaps this should be something else */
  100.  
  101. #ifndef PGM_BIGGRAYS
  102.     pm_error( "Input image is a word image, PbmPlus is compiled for bytes only" );
  103. #endif
  104.     }
  105.  
  106.     pm_message( "Image size: %d cols, %d rows", cols, rows);
  107.     pm_message( "%s",
  108.            (byte_word) ? "Byte image (8 bits)" : "Word image (16 bits)");
  109.  
  110.     if ( image_num < 0 )
  111.     pm_message( "Input contains %d image%c",
  112.            image_count, (image_count > 1) ? 's' : '\0');
  113.     else
  114.     {
  115.     if ( image_num >= image_count )
  116.         pm_error( "Cannot extract image %d, input contains only %d image%s",
  117.              image_num, image_count, (image_count > 1) ? "s" : "" );
  118.     for ( i = (byte_word) ? image_num : image_num*2 ; i > 0 ; --i ) {
  119.         for ( row = 0; row < rows; ++row)
  120.         for ( col = 0; col < cols; ++col )
  121.         {
  122.             val = getc( ifp );
  123.             if ( val == EOF ) {
  124.             pm_error( "EOF / read error" );
  125.             }
  126.         }
  127.     }
  128.  
  129.     pgm_writepgminit( stdout, cols, rows, (gray) maxval, 0 );
  130.     grayrow = pgm_allocrow( cols );
  131.  
  132.     for ( row = 0; row < rows; ++row)
  133.     {
  134.         for ( col = 0, gP = grayrow; col < cols; ++col )
  135.         {
  136.         val = getc( ifp );
  137.         if ( val == EOF )
  138.             pm_error( "EOF / read error" );
  139.         if (byte_word)
  140.             *gP++ = val;
  141.         else
  142.         {
  143.             val2 = getc( ifp );
  144.             if ( val2 == EOF )
  145.             pm_error( "EOF / read error" );
  146.             *gP++ = BYTE_TO_WORD(val, val2);
  147.         }
  148.         }
  149.         pgm_writepgmrow( stdout, grayrow, cols, (gray) maxval, 0 );
  150.     }
  151.  
  152.     pm_close( ifp );
  153.     pm_close( stdout );
  154.  
  155.     }
  156.     exit( 0 );
  157. }
  158.