home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TIFF / TACS40.ZIP / READTAG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-27  |  2.7 KB  |  141 lines

  1. #include <tiff.h>
  2.  
  3. extern LONG bigRead();
  4. extern LONG get_1st_ifd();
  5. extern SHORT type_length();
  6. extern BOOL access_ifd();
  7. extern BOOL read_dir();
  8. extern LONG lseek();
  9.  
  10. SHORT
  11. read_tag(fhandle, fdtype, tagnum, buffer, max_length)
  12. SHORT fhandle;
  13. SHORT fdtype;
  14. SHORT tagnum;
  15. LONGPTR buffer;
  16. SHORT max_length;
  17. {
  18.     LONG ifd_offset = get_1st_ifd(fhandle);
  19.     LONG new_offset;
  20.     TIFF_DIR_ENTRY dirent;
  21.     SHORT entry_count;
  22.     SHORT subfile_type;
  23.  
  24.     /* go through IFD's ... stop at null IFD offset */
  25.     while(ifd_offset)
  26.     {
  27.         /* get pertinent IFD data */
  28.         if(access_ifd(fhandle, ifd_offset,
  29.           (TIFF_DIR_ENTRY far *)&dirent, &entry_count, &subfile_type,
  30.           &new_offset))
  31.         {
  32.             return(0);
  33.         }
  34.  
  35.         /* match on subfile type? */
  36.         if(fdtype == subfile_type)
  37.         {
  38.             SHORT n = entry_count - 1;
  39.  
  40.             /* go through each entry */
  41.             while(n-- > 0 && !read_dir(fhandle,
  42.               (TIFF_DIR_ENTRY far *)&dirent))
  43.             {
  44.                 SHORT len;
  45.  
  46.                 /* only do it for the supplied tag number */
  47.                 if(dirent.tag != tagnum)
  48.                 {
  49.                     continue;
  50.                 }
  51.  
  52.                 /* get length of data to be copied */
  53.                 len = type_length(dirent.type);
  54.                 len *= (SHORT)dirent.length;
  55.  
  56.                 /* length overflow? */
  57.                 if(len > max_length)
  58.                 {
  59.                     return(0);
  60.                 }
  61.                 if(len > LONG_SIZE)
  62.                 {
  63.                     /* read data into user's buffer */
  64.                     if(lseek(fhandle, dirent.value_offset,
  65.                       0) == (LONG)(-1))
  66.                     {
  67.                         return(0);
  68.                     }
  69.                     if(bigRead(fhandle, buffer,
  70.                       (LONG)len) != (LONG)len)
  71.                     {
  72.                         return(0);
  73.                     }
  74.                 }
  75.                 else
  76.                 {
  77.                     LONGPTR dp;
  78.                     dp = (LONGPTR)&dirent.value_offset;
  79.                     while(len-- > 0)
  80.                     {
  81.                         *buffer++ = *dp++;
  82.                     }
  83.                 }
  84.  
  85.                 /* we're done after reading one tag's worth */
  86.                 return(1);
  87.             }
  88.  
  89.             /* matched subfile & unmatched tag => error */
  90.             return(0);
  91.         }
  92.         ifd_offset = new_offset;
  93.     }
  94.     return(0);
  95. }
  96.  
  97. SHORT
  98. read_dirent(fhandle, fdtype, tagnum, dirent)
  99. SHORT fhandle;
  100. SHORT fdtype;
  101. SHORT tagnum;
  102. TIFF_DIR_ENTRY far *dirent;
  103. {
  104.     LONG ifd_offset = get_1st_ifd(fhandle);
  105.     LONG new_offset;
  106.     SHORT entry_count;
  107.     SHORT subfile_type;
  108.  
  109.     /* go through IFD's ... stop at null IFD offset */
  110.     while(ifd_offset)
  111.     {
  112.         /* get pertinent IFD data */
  113.         if(access_ifd(fhandle, ifd_offset, dirent, &entry_count,
  114.           &subfile_type, &new_offset))
  115.         {
  116.             return(0);
  117.         }
  118.  
  119.         /* match on subfile type? */
  120.         if(fdtype == subfile_type)
  121.         {
  122.             SHORT n = entry_count - 1;
  123.  
  124.             /* go through each entry */
  125.             while(n-- > 0 && !read_dir(fhandle, dirent))
  126.             {
  127.                 /* only do it for the supplied tag number */
  128.                 if(dirent->tag == tagnum)
  129.                 {
  130.                     return(1);
  131.                 }
  132.             }
  133.  
  134.             /* matched subfile & unmatched tag => error */
  135.             return(0);
  136.         }
  137.         ifd_offset = new_offset;
  138.     }
  139.     return(0);
  140. }
  141.