home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_03 / 9n03100a < prev    next >
Text File  |  1991-01-09  |  5KB  |  176 lines

  1. /*********************************************
  2. * file d:\cips\rtiff.c
  3. *
  4. * Functions: This file contains
  5. *     read_tiff_image
  6. *     read_line
  7. *     seek_to_first_line
  8. *     seek_to_end_of_line
  9. *
  10. *  Purpose: These functions read a TIFF image 
  11. *  and insert the data into a ROWSxCOLS array 
  12. *  of short.
  13. *
  14. *  External Calls:
  15. *   mof.c - my_open
  16. *   mrw.c - my_read
  17. *   tiff.c - read_tiff_header
  18. *
  19. *  Modifications: created 25 June 1990 
  20. ***********************************************/
  21.  
  22. #include "d:\cips\cips.h"
  23.  
  24. read_tiff_image(image_file_name, array, il, ie, ll, le)
  25.       char   image_file_name[];
  26.       int    il, ie, ll, le;
  27.       short   array[ROWS][COLS];
  28. {
  29.    char  buffer[100],
  30.        rep[80];
  31.    int bytes_read,
  32.        closed,
  33.          file_descriptor,
  34.        i;
  35.    float a;
  36.    long  line_length, offset;
  37.    unsigned long position;
  38.    struct tiff_header_struct image_header;
  39.    read_tiff_header(image_file_name, &image_header);
  40.  
  41. /****************************************************
  42. *   Procedure:
  43. *   Seek to the strip offset where the data begins.
  44. *   Seek to the first line you want.
  45. *   Loop over the lines you want to read:
  46. *      Seek to the first element of the line.
  47. *      Read the line.
  48. *      Seek to the end of the data in that line.
  49. ****************************************************/
  50.  
  51.    file_descriptor = my_open(image_file_name);
  52.    position     = lseek(file_descriptor, 
  53.                         image_header.strip_offset, 0);
  54.    position     = seek_to_first_line(file_descriptor, 
  55.                                     &image_header, il);
  56.  
  57.    for(i=0; i<(ll-il); i++){
  58.       offset  = (ie-1)/(8/image_header.bits_per_pixel);
  59.       position      = lseek(file_descriptor, offset, 1);
  60.       bytes_read = read_line(file_descriptor, array, i, 
  61.                              &image_header, ie, le);
  62.       position      = seek_to_end_of_line(file_descriptor, 
  63.                                        le, &image_header);
  64.       position      = lseek(file_descriptor, 1, 1); /*???*/
  65.    }  /* ends loop over i  */
  66.  
  67.    closed = close(file_descriptor);
  68.  
  69. }  /*  ends read_tiff_image */
  70.  
  71. /*******************************************************
  72. *   read_line(...
  73. *
  74. *   This function reads bytes from the TIFF file into
  75. *   a buffer, extracts the numbers from that buffer,
  76. *   and puts them into a 100x100 array of shorts.
  77. *   The process depends on the number of bits per
  78. *   pixel used in the file (4 or 8).
  79. *******************************************************/
  80.  
  81. read_line(file_descriptor, array, line_number, 
  82.            image_header, ie, le)
  83.    int  file_descriptor, ie, le, line_number;
  84.    short  array[ROWS][COLS];
  85.    struct tiff_header_struct *image_header;
  86. {
  87.    char  buffer[100], first, second;
  88.    float a, b;
  89.    int bytes_read, i;
  90.    unsigned int bytes_to_read;
  91.    union short_char_union scu;
  92.  
  93.    for(i=0; i<100; i++)
  94.       buffer[i] = '\0';
  95.  
  96. /**********************************************
  97. *   Use the number of bits per pixel to calculate
  98. *   how many bytes to read.
  99. *************************************************/
  100.  
  101.    bytes_to_read = (le-ie)/(8/image_header->bits_per_pixel);
  102.    bytes_read       = read(file_descriptor, buffer, 
  103.                            bytes_to_read);
  104.  
  105.    for(i=0; i<bytes_read; i++){
  106.  
  107. /**********************************************
  108. *   Use unions defined in cips.h to stuff bytes  
  109. *   into shorts.                   
  110. ************************************************/
  111.  
  112.       if(image_header->bits_per_pixel == 8){
  113.        scu.s_num          = 0;
  114.        scu.s_alpha[0]        = buffer[i];
  115.        array[line_number][i] = scu.s_num;
  116.       }  /* ends if bits_per_pixel == 8 */
  117.  
  118.       if(image_header->bits_per_pixel == 4){
  119.  
  120.        scu.s_num             = 0;
  121.        second                = buffer[i] & 0X000F;
  122.        scu.s_alpha[0]        = second;
  123.        array[line_number][i*2+1] = scu.s_num;
  124.  
  125.        scu.s_num             = 0;
  126.        first                 = buffer[i] >> 4;
  127.        first                 = first & 0x000F;
  128.        scu.s_alpha[0]        = first;
  129.        array[line_number][i*2] = scu.s_num;
  130.  
  131.       }  /* ends if bits_per_pixel == 4 */
  132.    }  /*  ends loop over i  */
  133.  
  134.    return(bytes_read);
  135. }  /* ends read_line  */
  136.  
  137. /*****************************************
  138. *   seek_to_first_line(...
  139. ******************************************/
  140.  
  141. seek_to_first_line(file_descriptor, image_header, il)
  142.    int  file_descriptor, il;
  143.    struct tiff_header_struct *image_header;
  144. {
  145.    long offset;
  146.    unsigned long position;
  147.  
  148.    offset   = (il-1)*image_header->image_width/
  149.              (8/image_header->bits_per_pixel);
  150.       /* seek from current position */
  151.    position = lseek(file_descriptor, offset, 1);
  152.    return(position);
  153. }  /* ends seek_to_first_line */
  154.  
  155. /*****************************************
  156. *   seek_to_end_of_line(...
  157. *******************************************/
  158.  
  159. seek_to_end_of_line(file_descriptor, le, image_header)
  160.    int file_descriptor, le;
  161.    struct tiff_header_struct *image_header;
  162. {
  163.    int origin;
  164.    long  offset;
  165.    unsigned long position;
  166.  
  167.    offset   = (image_header->image_width-le)/
  168.              (8/image_header->bits_per_pixel);
  169.    origin   = 1;   /* seek from the current position  */
  170.    position = lseek(file_descriptor, offset, origin);
  171.    return(position);
  172. }  /* ends seek_to_end_of_line  */
  173.  
  174.  
  175.  
  176.