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

  1. /***********************************************
  2. *  file d:\cips\tiff.c
  3. *
  4. *  Functions: This file contains
  5. *  read_tiff_header
  6. *  extract_long_from_buffer
  7. *  extract_short_from_buffer
  8. *
  9. *  Purpose: This file contains the subroutines 
  10. *  that read the tiff files header information.
  11. *
  12. *  External Calls:
  13. *   mof.c - my_open_file
  14. *   mrw.c - my_read
  15. *
  16. *  Modifications: created 23 June 1990 
  17. ***********************************************/
  18.  
  19. #include "d:\cips\cips.h"
  20.  
  21. read_tiff_header(file_name, image_header)
  22.    char file_name[];
  23.    struct tiff_header_struct *image_header;
  24. {
  25.    char buffer[12];
  26.  
  27.    int      bytes_read,
  28.       closed,
  29.       file_desc,
  30.       i,
  31.       j,
  32.       lsb,
  33.       not_finished;
  34.  
  35.    long bits_per_pixel,
  36.       image_length,
  37.       image_width,
  38.       length_of_field,
  39.       offset_to_ifd,
  40.       position,
  41.       strip_offset,
  42.       subfile,
  43.       value;
  44.  
  45.    short entry_count,
  46.        field_type,
  47.        s_bits_per_pixel,
  48.        s_image_length,
  49.        s_image_width,
  50.        s_strip_offset,
  51.        tag_type;
  52.  
  53.    file_desc = my_open(file_name);
  54.  
  55. /*************************************
  56. *   Determine if the file uses MSB
  57. *   first or LSB first
  58. *************************************/
  59.  
  60.    bytes_read = my_read(file_desc, buffer, 8);
  61.  
  62.    if(buffer[0] == 0x49)
  63.       lsb = 1;
  64.    else
  65.       lsb = 0;
  66.  
  67. /*************************************
  68. *   Read the offset to the IFD    
  69. *************************************/
  70.  
  71.    extract_long_from_buffer(buffer, lsb, 4, 
  72.          &offset_to_ifd);
  73.  
  74.    not_finished = 1;
  75.    while(not_finished){
  76.  
  77. /*************************************
  78. *   Seek to the IFD and read the 
  79. *   entry_count, i.e. the number of
  80. *   entries in the IFD.
  81. *************************************/
  82.  
  83.       position = lseek(file_desc, offset_to_ifd, 0);
  84.       bytes_read = my_read(file_desc, buffer, 2);
  85.       extract_short_from_buffer(buffer, lsb, 0, 
  86.             &entry_count);
  87.  
  88. /***************************************
  89. *   Now loop over the directory entries.
  90. *   Look only for the tags we need.  These are:
  91. *     ImageLength 
  92. *     ImageWidth
  93. *     BitsPerPixel(BitsPerSample)
  94. *     StripOffset
  95. *****************************************/
  96.  
  97.       for(i=0; i<entry_count; i++){
  98.        bytes_read = my_read(file_desc, buffer, 12);
  99.        extract_short_from_buffer(buffer, lsb, 0, 
  100.              &tag_type);
  101.  
  102.        switch(tag_type){
  103.           case 255: /* Subfile Type */
  104.              extract_short_from_buffer(buffer, lsb, 2, 
  105.                     &field_type);
  106.              extract_short_from_buffer(buffer, lsb, 4,
  107.                     &length_of_field);
  108.              extract_long_from_buffer(buffer, lsb, 8, 
  109.                     &subfile);
  110.              break;
  111.  
  112.           case 256: /* ImageWidth */
  113.              extract_short_from_buffer(buffer, lsb, 2, 
  114.                     &field_type);
  115.              extract_short_from_buffer(buffer, lsb, 4,
  116.                     &length_of_field);
  117.              if(field_type == 3){
  118.               extract_short_from_buffer(buffer, lsb, 8,
  119.                      &s_image_width);
  120.               image_width = s_image_width;
  121.              }
  122.              else
  123.               extract_long_from_buffer(buffer, lsb, 8, 
  124.                      &image_width);
  125.              break;
  126.  
  127.           case 257: /* ImageLength */
  128.              extract_short_from_buffer(buffer, lsb, 2, 
  129.                      &field_type);
  130.              extract_short_from_buffer(buffer, lsb, 4,
  131.                      &length_of_field);
  132.              if(field_type == 3){
  133.               extract_short_from_buffer(buffer, lsb, 8,
  134.                      &s_image_length);
  135.               image_length = s_image_length;
  136.              }
  137.              else
  138.               extract_long_from_buffer(buffer, lsb, 8,
  139.                     &image_length);
  140.              break;
  141.  
  142.           case 258: /* BitsPerPixel */
  143.              extract_short_from_buffer(buffer, lsb, 2, 
  144.                     &field_type);
  145.              extract_short_from_buffer(buffer, lsb, 4,
  146.                     &length_of_field);
  147.              if(field_type == 3){
  148.               extract_short_from_buffer(buffer, lsb, 8,
  149.                     &s_bits_per_pixel);
  150.               bits_per_pixel = s_bits_per_pixel;
  151.              }
  152.              else
  153.               extract_long_from_buffer(buffer, lsb, 8,
  154.                     &bits_per_pixel);
  155.              break;
  156.  
  157.           case 273: /* StripOffset */
  158.              extract_short_from_buffer(buffer, lsb, 2, 
  159.                     &field_type);
  160.              extract_short_from_buffer(buffer, lsb, 4,
  161.                     &length_of_field);
  162.              if(field_type == 3){
  163.               extract_short_from_buffer(buffer, lsb, 8,
  164.                     &s_strip_offset);
  165.               strip_offset = s_strip_offset;
  166.              }
  167.              else
  168.               extract_long_from_buffer(buffer, lsb, 8,
  169.                     &strip_offset);
  170.              break;
  171.  
  172.           default:
  173.              break;
  174.  
  175.        }  /* ends switch tag_type */
  176.       }  /* ends loop over i directory entries */
  177.  
  178.       bytes_read = my_read(file_desc, buffer, 4);
  179.       extract_long_from_buffer(buffer, lsb, 0, 
  180.             &offset_to_ifd);
  181.       if(offset_to_ifd == 0) not_finished = 0;
  182.  
  183.    }  /* ends while not_finished */
  184.  
  185.    image_header->lsb                = lsb;
  186.    image_header->bits_per_pixel = bits_per_pixel;
  187.    image_header->image_length       = image_length;
  188.    image_header->image_width        = image_width;
  189.    image_header->strip_offset       = strip_offset;
  190.  
  191.    closed = close(file_desc);
  192.  
  193. }  /* ends read_tiff_header */
  194.  
  195. /****************************************
  196. *   extract_long_from_buffer(...
  197. *
  198. *   This takes a four byte long out of a
  199. *   buffer of characters. It is important 
  200. *   to know the byte order LSB or MSB.
  201. ****************************************/
  202.  
  203. extract_long_from_buffer(buffer, lsb, start, number)
  204.    char  buffer[];
  205.    int       lsb, start;
  206.    long  *number;
  207. {
  208.    int i;
  209.    union long_char_union lcu;
  210.  
  211.    if(lsb == 1){
  212.       lcu.l_alpha[0] = buffer[start+0];
  213.       lcu.l_alpha[1] = buffer[start+1];
  214.       lcu.l_alpha[2] = buffer[start+2];
  215.       lcu.l_alpha[3] = buffer[start+3];
  216.    }  /* ends if lsb = 1 */
  217.  
  218.    if(lsb == 0){
  219.       lcu.l_alpha[0] = buffer[start+3];
  220.       lcu.l_alpha[1] = buffer[start+2];
  221.       lcu.l_alpha[2] = buffer[start+1];
  222.       lcu.l_alpha[3] = buffer[start+0];
  223.    }  /* ends if lsb = 0      */
  224.  
  225.    *number = lcu.l_num;
  226.  
  227. }  /* ends extract_long_from_buffer */
  228.  
  229. /****************************************
  230. *   extract_short_from_buffer(...
  231. *
  232. *   This takes a two byte short out of a
  233. *   buffer of characters. It is important 
  234. *   to know the byte order LSB or MSB.
  235. ****************************************/
  236.  
  237. extract_short_from_buffer(buffer, lsb, start, number)
  238.    char  buffer[];
  239.    int       lsb, start;
  240.    short *number;
  241. {
  242.  
  243.    int i;
  244.    union short_char_union lcu;
  245.  
  246.    if(lsb == 1){
  247.       lcu.s_alpha[0] = buffer[start+0];
  248.       lcu.s_alpha[1] = buffer[start+1];
  249.    }  /* ends if lsb = 1 */
  250.  
  251.    if(lsb == 0){
  252.       lcu.s_alpha[0] = buffer[start+1];
  253.       lcu.s_alpha[1] = buffer[start+0];
  254.    }  /* ends if lsb = 0      */
  255.  
  256.    *number = lcu.s_num;
  257.  
  258. }  /* ends extract_short_from_buffer */
  259.