home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / PNM / zeisstopnm.c < prev   
C/C++ Source or Header  |  1996-11-18  |  5KB  |  191 lines

  1. /* zeisstopnm.c - convert a Zeiss confocal image into a portable anymap
  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. ** This conversion utility is based on a mail from Keith Bartels to
  17. ** the confocal mail group (confocal@ubvm.cc.buffalo.edu) in June 1993.
  18. **
  19. ** I'm including here a description of the Zeiss confocal image format. I
  20. ** obtained this over the phone on 4-10-1991 form a Zeiss Engineer, and from
  21. ** what I hear, it is probably not correct for the new scopes.
  22. ** Zeiss puts its header information and the end of the file, so I call it
  23. ** a tailer.  This is nice because most conversions programs that work with raw
  24. ** image files will read the image simply ignore the tailer.  The file contains:
  25. **
  26. ** The image data: NxN  1 byte pixels (where N is the number of pixels in a
  27. ** scan line) in a standard raw rastering order.
  28. **
  29. ** The tailer contains:
  30. ** 256 bytes: the blue Look-Up-Table (LUT)
  31. ** 256 bytes: the red LUT
  32. ** 256 bytes: the green LUT
  33. ** 8 bytes: empty
  34. ** 2 bytes: no. of columns in the image. hi-byte, low-byte
  35. ** 2 bytes: no. of rows in the image.
  36. ** 2 bytes: x-position of upper left pixel  (0 for 512x512 images)
  37. ** 2 bytes: y-position of upper left pixel  (0 for 512x512 images)
  38. ** 16 bytes: empty
  39. ** 32 bytes: test from upper right corner of image, ASCII.
  40. ** 128 bytes: text from the bottom two rows of the screen, ASCII.
  41. ** 64 bytes: reserved
  42. ** -----------
  43. ** 1024 bytes TOTAL
  44. **
  45. ** So, image files contain NxN + 1024 bytes.
  46. **
  47. ** Keith Bartels
  48. ** keith@VISION.EE.UTEXAS.EDU
  49. **
  50. */
  51.  
  52. #include "pnm.h"
  53.  
  54. int
  55. main( argc, argv )
  56.     int argc;
  57.     char* argv[];
  58.     {
  59.     FILE* ifp;
  60.     gray* grayrow;
  61.     register gray* gP;
  62.     int argn, row, i;
  63.     register int col, val;
  64.     int rows=0, cols=0;
  65.     int format = 0;
  66.     xel* xelrow;
  67.     register xel* xP;
  68.     char* buf = NULL;
  69.     unsigned char *lutr, *lutg, *lutb;
  70.     long nread = 0;
  71.     unsigned char* data;
  72.     unsigned char* byteP;
  73.     char* usage = "[-pgm|-ppm] [Zeissfile]";
  74.  
  75.  
  76.     pnm_init( &argc, argv );
  77.  
  78.     argn = 1;
  79.  
  80.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  81.     {
  82.     if ( pm_keymatch( argv[argn], "-pgm", 3 ) )
  83.         {
  84.         if ( argn >= argc )
  85.         pm_usage( usage );
  86.         format = PGM_TYPE;
  87.         }
  88.     else if ( pm_keymatch( argv[argn], "-ppm", 3 ) )
  89.         {
  90.         if ( argn >= argc )
  91.         pm_usage( usage );
  92.         format = PPM_TYPE;
  93.         }
  94.     else
  95.         pm_usage( usage );
  96.     ++argn;
  97.     }
  98.  
  99.     if ( argn < argc )
  100.     {
  101.     ifp = pm_openr( argv[argn] );
  102.     ++argn;
  103.     }
  104.     else
  105.     ifp = stdin;
  106.  
  107.     if ( argn != argc )
  108.     pm_usage( usage );
  109.  
  110.     /* Read the image to a buffer */
  111.  
  112.     buf = pm_read_unknown_size( ifp, &nread );
  113.  
  114.     /* Check the format of the file */
  115.  
  116.     if (nread <=1024)
  117.     pm_error( "Input file not in Zeiss format (too small)" );
  118.  
  119.     lutg = (unsigned char *)buf+(nread-1024+512);
  120.     lutr = (unsigned char *)buf+(nread-1024+256);
  121.     lutb = (unsigned char *)buf+(nread-1024);
  122.  
  123.     cols = ((unsigned char) buf[nread-1024+768+8]) +
  124.     (((unsigned char) buf[nread-1024+768+9]) << 8);
  125.     rows = ((unsigned char) buf[nread-1024+768+10]) +
  126.     (((unsigned char) buf[nread-1024+768+11]) << 8);
  127.  
  128.     if ( cols <= 0 )
  129.     pm_error( "invalid cols: %d", cols );
  130.     if ( rows <= 0 )
  131.     pm_error( "invalid rows: %d", rows );
  132.  
  133.     if (cols*rows != nread-1024)
  134.     pm_error( "Hmm, %d rows, %d cols, %d total image size",
  135.          rows, cols, nread-1024);
  136.  
  137.     /* Choose pgm or ppm */
  138.     /* If the LUTs all contain 0,1,2,3,4..255, it is a pgm file */
  139.  
  140.     for (i=0; i<256 && format==0; i++)
  141.     if (lutr[i] != i || lutg[i] != i || lutb[i] != i)
  142.         format = PPM_TYPE;
  143.  
  144.     if (format == 0)
  145.     format = PGM_TYPE;
  146.  
  147.     pnm_writepnminit( stdout, cols, rows, 255, format, 0 );
  148.     xelrow = pnm_allocrow( cols );
  149.     byteP = (unsigned char *) buf;
  150.  
  151.     switch ( PNM_FORMAT_TYPE(format) )
  152.         {
  153.         case PGM_TYPE:
  154.         pm_message( "writing PGM file, %d rows %d columns", rows, cols );
  155.         break;
  156.  
  157.         case PPM_TYPE:
  158.         pm_message( "writing PPM file, %d rows %d columns", rows, cols );
  159.         break;
  160.  
  161.         default:
  162.         pm_error( "shouldn't happen" );
  163.         }
  164.  
  165.     for ( row = 0; row < rows; ++row )
  166.     {
  167.     switch ( PNM_FORMAT_TYPE(format) )
  168.     {
  169.     case PGM_TYPE:
  170.         for ( col = 0, xP = xelrow; col < cols; ++col, ++xP, ++byteP )
  171.         PNM_ASSIGN1( *xP, *byteP );
  172.         break;
  173.  
  174.     case PPM_TYPE:
  175.         for ( col = 0, xP = xelrow; col < cols; ++col, ++xP, ++byteP )
  176.         PPM_ASSIGN( *xP, lutr[*byteP], lutg[*byteP], lutb[*byteP] );
  177.         break;
  178.  
  179.         default:
  180.         pm_error( "shouldn't happen" );
  181.         }
  182.  
  183.     pnm_writepnmrow( stdout, xelrow, cols, 255, format, 0 );
  184.     }
  185.  
  186.     free( buf );
  187.     pm_close( stdout );
  188.  
  189.     exit( 0 );
  190. }
  191.