home *** CD-ROM | disk | FTP | other *** search
/ Der Mediaplex Sampler - Die 6 von Plex / 6_v_plex.zip / 6_v_plex / DISK5 / WIN_12 / IDEPIX2.ZIP / TIFFACS.ZIP / READTAG.C < prev    next >
C/C++ Source or Header  |  1993-06-23  |  4KB  |  169 lines

  1. /*-------------------------------------------------------
  2.  
  3.     T I F F A C S : Unterstⁿtzungsroutinen fⁿr TIFF-Dateien
  4.             Ursprⁿnglich von Aldus, von mir auf dem
  5.             Walnut-Creek - Source-Code-ROM gefunden.
  6.  
  7.             Der ursprⁿngliche Source ist TIFF 4.0 und
  8.             auch ansonsten ein ziemlicher Mⁿll.
  9.  
  10.             Diese Version hat Funktionsprototypen und
  11.             ein paar Fehler weniger, aber im Grunde ist
  12.             sie auch ein ziemlicher Mⁿll.
  13.  
  14.             Im Mircosoft System Journal 4/93 ist eine ganz
  15.             brauchbare TIF-Routine dabei, aber eben nicht PD
  16.  
  17.             Bernd Herd
  18.             Rudolf Virchow Str. 8
  19.             6842 Bⁿrstadt
  20.             06206/79222
  21.  
  22.        R E A D T A G  : N÷tige Routinen um einen TAG-Eintrag 
  23.             aus einer TIFF-Datei zu lesen
  24.  
  25.   -------------------------------------------------------------*/
  26.  
  27. #include <io.h>
  28. #include "tiff.h"    // Header-Datei zur Verbindung mit Applikation
  29. #include "tiffi.h"    // Interne Header-Datei
  30.  
  31. /*-------------------------------------------------------------
  32.  
  33.     read_tag : Daten eines TIFF-Tags lesen, anzahl der Bytes
  34.            je nach Datenformat...
  35.  
  36.     Parameter: fhandle    : Dateihandle der offenen TIF-Datei
  37.            fdtype    : Subfile-Type
  38.            tagnum    : TAG-Nummer
  39.            buffer    : Ziel-Adresse fⁿr Daten
  40.            max_length    : Gr÷▀e des Puffers in Bytes
  41.     ReturnVal: 0        : Tag nicht gefunden oder falscher Subfile
  42.            sonst    : Anzahl gelesene EintrΣge (nicht : Bytes!)
  43.  
  44.   -------------------------------------------------------------*/
  45. SHORT    read_tag(    SHORT     fhandle,        // readtag.c
  46.             SHORT     fdtype,
  47.             SHORT     tagnum,
  48.             void far *ibuffer,
  49.             SHORT     max_length)
  50. {    LONGPTR buffer = (LONGPTR) ibuffer;
  51.     LONG ifd_offset = get_1st_ifd(fhandle);
  52.     LONG new_offset;
  53.     TIFF_DIR_ENTRY dirent;
  54.     SHORT entry_count;
  55.     LONG  subfile_type;
  56.  
  57.     /* go through IFD's ... stop at null IFD offset */
  58.     while(ifd_offset)
  59.     {
  60.         /* get pertinent IFD data */
  61.         if(access_ifd(fhandle, ifd_offset,
  62.           (TIFF_DIR_ENTRY far *)&dirent, &entry_count, &subfile_type,
  63.           &new_offset))
  64.         {
  65.             return(0);
  66.         }
  67.  
  68.         /* match on subfile type? */
  69.         if(fdtype == subfile_type)
  70.         {
  71.             SHORT n = entry_count - 1;
  72.  
  73.             /* go through each entry */
  74.             while(n-- > 0 && !read_dir(fhandle,
  75.               (TIFF_DIR_ENTRY far *)&dirent))
  76.             {
  77.                 SHORT len;
  78.  
  79.                 /* only do it for the supplied tag number */
  80.                 if(dirent.tag != tagnum)
  81.                 {
  82.                     continue;
  83.                 }
  84.  
  85.                 /* get length of data to be copied */
  86.                 len = type_length(dirent.type);
  87.                 len *= (SHORT)dirent.length;
  88.  
  89.                 /* length overflow? */
  90.                 if(len > max_length)
  91.                 {
  92.                     return(0);
  93.                 }
  94.                 if(len > LONG_SIZE)
  95.                 {
  96.                     /* read data into user's buffer */
  97.                     if(lseek(fhandle, dirent.value_offset,
  98.                       0) == (LONG)(-1))
  99.                     {
  100.                         return(0);
  101.                     }
  102.                     if(bigRead(fhandle, buffer,
  103.                       (LONG)len) != (LONG)len)
  104.                     {
  105.                         return(0);
  106.                     }
  107.                 }
  108.                 else
  109.                 {
  110.                     LONGPTR dp;
  111.                     dp = (LONGPTR)&dirent.value_offset;
  112.                     while(len-- > 0)
  113.                     {
  114.                         *buffer++ = *dp++;
  115.                     }
  116.                 }
  117.  
  118.                 /* we're done after reading one tag's worth */
  119.                 return dirent.length;
  120.             }
  121.  
  122.             /* matched subfile & unmatched tag => error */
  123.             return(0);
  124.         }
  125.         ifd_offset = new_offset;
  126.     }
  127.     return(0);
  128. }
  129.  
  130. SHORT read_dirent(SHORT fhandle, SHORT fdtype, SHORT tagnum, TIFF_DIR_ENTRY far *dirent)
  131. {
  132.     LONG ifd_offset = get_1st_ifd(fhandle);
  133.     LONG new_offset;
  134.     SHORT entry_count;
  135.     LONG subfile_type;
  136.  
  137.     /* go through IFD's ... stop at null IFD offset */
  138.     while(ifd_offset)
  139.     {
  140.         /* get pertinent IFD data */
  141.         if(access_ifd(fhandle, ifd_offset, dirent, &entry_count,
  142.           &subfile_type, &new_offset))
  143.         {
  144.             return(0);
  145.         }
  146.  
  147.         /* match on subfile type? */
  148.         if(fdtype == subfile_type)
  149.         {
  150.             SHORT n = entry_count - 1;
  151.  
  152.             /* go through each entry */
  153.             while(n-- > 0 && !read_dir(fhandle, dirent))
  154.             {
  155.                 /* only do it for the supplied tag number */
  156.                 if(dirent->tag == tagnum)
  157.                 {
  158.                     return(1);
  159.                 }
  160.             }
  161.  
  162.             /* matched subfile & unmatched tag => error */
  163.             return(0);
  164.         }
  165.         ifd_offset = new_offset;
  166.     }
  167.     return(0);
  168. }
  169.