LISTING 3 (rtiff.c)

/*********************************************
* file d:\cips\rtiff.c
*
* Functions: This file contains
*     read_tiff_image
*     read_line
*     seek_to_first line
*     seek_to_end_of_line
*
* Purpose: These functions read a TIFF image
* and insert the data into a ROWSxCOLS array
* of short.
*
* External Calls:
*  mof.c - my_open
*  mrw.c - my_read
*  tiff.c - read_tiff_header
*
* Modifications: created 25 June 1990
*********************************************/

#include "d:\cips\cips.h"

read_tiff_image(image_file_name, array, il, ie, ll, le)
     char   image_file_name[];
     int    il, ie, ll, le;
     short   array[ROWS] [COLS];
{
   char  buffer[100],
       rep[80];
   int  bytes_read,
       closed,
         file_descriptor,
       i;
   float a;
   long line_length, offset;
   unsigned long position;
   struct tiff_header_struct image_header;
   read_tiff_header(image_file_name, &image_header);

/****************************************************
*   Procedure:
*   Seek to the strip offset where the data begins.
*   Seek to the first line you want.
*   Loop over the lines you want to read:
*      Seek to the first element of the line.
*      Read the line.
*      Seek to the end of the data in that line.
****************************************************/

   file_descriptor = my_open(image_file_name);
   position     = lseek(file_descriptor,
                     image_header.strip_offset, 0);
   position     = seek_to_first_line(file_descriptor,
                               &image_header, il);
   
   for(i=0; i<(ll-il); i++){
      offset = (ie-1)/(8/image_header.bits_per_pixel);
      position      = lseek(file_descriptor, offset, 1);
      bytes_read = read_line(file_descriptor, array, i,
                         &image_header, ie, le);
      position      = seek_to_end_of_line(file_descriptor,
                                  le, &image_header);
      position      = lseek(file_descriptor, 1, 1); /*???*/
   }  /* ends loop over i */
   
   closed = close(file_descriptor);

}  /*  ends read_tiff_image */

/*******************************************************
*   read_line(...
*
*   This function reads bytes from the TIFF file into
*   a buffer, extracts the numbers from that buffer,
*   and puts them into a 100x100 array of shorts.
*   The process depends on the number of bits per
*   pixel used in the file (4 or 8).
*******************************************************/

read_line(file_descriptor, array, line_number,
         image_header, ie, le)
   int file_descriptor, ie, le, line_number;
   short array[ROWS][COLS];
   struct tiff_header_struct *image_header;

{
   char buffer[100], first, second;
   float a, b;
   int bytes_read, i;
   unsigned int bytes_to_read;
   union short_char_union scu;
   
   for(i=0; i<100; i++)
      buffer[i] = '\0';

/**********************************************
*   Use the number of bits per pixel to calculate
*   how many bytes to read.
*************************************************/

   bytes_to_read = (le-ie)/(8/image_header->bits_per_pixel);
   bytes_read       = read(file_descriptor, buffer,
                       bytes_to_read);
   
   for(i=0; i<bytes_read; i++){

/**********************************************
*   Use unions defined in cips.h to stuff bytes
*   into shorts.
************************************************/
     
     if(image_header->bits_per_pexil == 8){
      scu.s_num           = 0;
      scu.s_alpha[0]         = buffer[i];
      array[line_number] [i] = scu.s_num;
     }  /* ends if bits_per_pixel == 8 */
     
     if(image_header->bits_per_pixel == 4){
      
      scu.s_num              = 0;
      second                 = buffer[i] & 0X000F;
      scu.s_alpha[0]         = second;
      array[line_number] [i*2+1] = scu.s_num;
      
      scu.s_num              = 0;
      first                  = buffer[i] >> 4;
      first                  = first & 0x000F;
      scu.s_alpha[0]         = first;
      array[line_number] [i*2] = scu.s_num;
     
     }  /* ends if bits_per_pixel == 4 */
   } /*  ends loop over i */
   
   return(bytes_read);
}  /* ends read_line */

/*****************************************
*   seek_to_first_line(...
******************************************/

seek_to_first_line(file_descriptor, image_header, il)
   int file_descriptor, il;
   struct tiff_header_struct *image_header;
{
   long offset;
   unsigned long position;
   
   offset   = (il-1)*image_header->image_width/
            (8/image_header->bits_per_pixel);
      /* seek from current position */
   position = lseek(file_descriptor, offset, 1);
   return(position);
)  /* ends seek_to_first_line */

/*****************************************
*   seek_to_end_of_line(...
*******************************************/

seek_to_end_of_line(file_descriptor, le, image_header)
   int file_descriptor, le;
   struct tiff_header_struct *image_header;
{
   int origin;
   long offset;
   unsigned long position;
   
   offset   = (image_header->image_width-le)/
            (8/image_header->bits_per_pixel);
   origin   = 1;   /* seek from the current position  */
   position = lseek(file_descriptor, offset, origin);
   return(position);
}  /* ends seek_to_end_of_line  */