"Fossies" - the Fresh Open Source Software Archive

Member "netpbm-10.73.29/converter/pgm/bioradtopgm.c" (19 Aug 2006, 3643 Bytes) of package /linux/misc/netpbm-10.73.29.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "bioradtopgm.c" see the Fossies "Dox" file reference documentation.

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