home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / phillips.exe / TIFF.C < prev    next >
Text File  |  1990-08-13  |  8KB  |  292 lines

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