home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gbmsrc.zip / gbmtifh.c < prev    next >
C/C++ Source or Header  |  1996-04-14  |  16KB  |  775 lines

  1.  
  2. /*
  3.  
  4. gbmtifh.c - Routines to handle TIFF file headers
  5.  
  6. */
  7.  
  8. /*...sincludes:0:*/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stddef.h>
  12. #include <malloc.h>
  13. #include <memory.h>
  14. #include <string.h>
  15. #include "gbm.h"
  16. #include "gbmhelp.h"
  17. #define    _GBMTIFH_
  18. #include "gbmtifh.h"
  19.  
  20. /*...vgbm\46\h:0:*/
  21. /*...vgbmhelp\46\h:0:*/
  22. /*...vgbmtifh\46\h:0:*/
  23. /*...e*/
  24.  
  25. /*...susefull:0:*/
  26. #define    ifd_malloc()    ((IFD *) malloc((size_t) sizeof(IFD)))
  27. #define    ifd_free(ifd)    free((char *) ifd)
  28.  
  29. #define    ifh_malloc()    ((IFH *) malloc((size_t) sizeof(IFH)))
  30. #define    ifh_free(ifh)    free((char *) ifh)
  31.  
  32. static int sizeof_data_type(short data_type)
  33.     {
  34.     switch ( data_type )
  35.         {
  36.         case D_BYTE:
  37.         case D_SBYTE:
  38.         case D_ASCII:
  39.         case D_UNDEFINED:
  40.             return 1;
  41.         case D_SHORT:
  42.         case D_SSHORT:
  43.             return sizeof(short);
  44.         case D_LONG:
  45.         case D_SLONG:
  46.             return sizeof(long);
  47.         case D_RATIONAL:
  48.         case D_SRATIONAL:
  49.             return sizeof(rational);
  50.         case D_FLOAT:
  51.             return 4;
  52.         case D_DOUBLE:
  53.             return 8;
  54.         }
  55.     return 1;
  56.     }
  57.  
  58. static void tag_free(TAG *tag)
  59.     {
  60.     free(tag->value);
  61.     }
  62.  
  63. /*
  64. This finds the slot for the new tag. It returns NULL if the limit of
  65. MAX_TAGS tags is curruntly defined.
  66. */
  67.  
  68. static TAG *get_tag_slot(short type, IFD *ifd)
  69.     {
  70.     int    i;
  71.  
  72.     if ( ifd->n_tags == MAX_TAGS )
  73.         return NULL;
  74.  
  75.     for ( i = ifd->n_tags;
  76.           i > 0 && ifd->tags[i - 1].type >= type;
  77.           i-- )
  78.         memcpy(ifd->tags + i,
  79.                ifd->tags + i - 1,
  80.                sizeof(TAG));
  81.  
  82.     ifd->n_tags++;
  83.  
  84.     /* now i == slot, with greater than elements moved up */
  85.  
  86.     return &(ifd->tags[i]);
  87.     }
  88. /*...e*/
  89.  
  90. /*...sread_ifh_and_ifd:0:*/
  91. /*...sread_long:0:*/
  92. #define    make_long(b1,b2,b3,b4) ( ((long)(b1)<<24) | ((long)(b2)<<16) | ((long)(b3)<<8) | (long)(b4) )
  93.  
  94. static long read_long(int fd, BOOLEAN motorola)
  95.     {
  96.     byte b[4];
  97.  
  98.     gbm_file_read(fd, b, 4);
  99.     return  ( motorola ) ?
  100.         make_long(b[0], b[1], b[2], b[3]) :
  101.         make_long(b[3], b[2], b[1], b[0]) ;
  102.     }
  103. /*...e*/
  104. /*...sread_short:0:*/
  105. #define    make_short(b1,b2) ( ((short)(b1)<<8) | (short)(b2) )
  106.  
  107. static short read_short(int fd, BOOLEAN motorola)
  108.     {
  109.     byte b[2];
  110.  
  111.     gbm_file_read(fd, b, 2);
  112.     return ( motorola ) ?
  113.         make_short(b[0], b[1]) :
  114.         make_short(b[1], b[0]) ;
  115.     }
  116. /*...e*/
  117. /*...sread_rational:0:*/
  118. static void read_rational(int fd, BOOLEAN motorola, rational *r)
  119.     {
  120.     r->numerator   = read_long(fd, motorola);
  121.     r->denominator = read_long(fd, motorola);
  122.     }
  123. /*...e*/
  124. /*...sread_tag:0:*/
  125. static int read_tag(int fd, BOOLEAN motorola, TAG *tag)
  126.     {
  127.     int i, s, n;
  128.     long len;
  129.     long seek_to, old_pos;
  130.  
  131.     tag->type      = read_short(fd, motorola);
  132.     tag->data_type = read_short(fd, motorola);
  133.     tag->length    = read_long(fd, motorola);
  134.  
  135.     if ( tag->type & 0x8000 )
  136.         /* proprietry tag */
  137.         {
  138.         gbm_file_lseek(fd, 4L, SEEK_CUR);    /* skip data */
  139.         return TE_OK;            /* assumed ok */
  140.         }
  141.  
  142.     n   = (int) tag->length;
  143.  
  144. /*...sbugfix for UBU\39\s writing of ColorMap tag:8:*/
  145. /* UBU writes out a length feild of 256 when it should write 768 */
  146.  
  147. if ( tag->type == T_COLORMAP && (n / 3) * 3 != n )
  148.     n *= 3;
  149. /*...e*/
  150.  
  151.     s   = sizeof_data_type(tag->data_type);
  152.     len = s * n;
  153.  
  154.     if ( len > 4 )
  155.         /* will have to seek for data */
  156.         {
  157.         seek_to = read_long(fd, motorola);
  158.         old_pos = gbm_file_lseek(fd, 0L, SEEK_CUR);
  159.         gbm_file_lseek(fd, seek_to, SEEK_SET);
  160.         }
  161.  
  162.     if ( (tag->value = malloc((size_t) len)) == NULL )
  163.         return TE_MEM;
  164.  
  165.     switch ( tag->data_type )
  166.         {
  167.         case D_BYTE:
  168.         case D_SBYTE:
  169.             gbm_file_read(fd, tag->value, n);
  170.             break;
  171.         case D_ASCII:
  172.             gbm_file_read(fd, tag->value, n);
  173.             break;
  174.         case D_SHORT:
  175.         case D_SSHORT:
  176.             {
  177.             short    *short_ptr = (short *) tag->value;
  178.  
  179.             for ( i = 0; i < n; i++ )
  180.                 *short_ptr++ = read_short(fd, motorola);
  181.             }
  182.             break;
  183.         case D_LONG:
  184.         case D_SLONG:
  185.             {
  186.             long    *long_ptr = (long *) tag->value;
  187.  
  188.             for ( i = 0; i < n; i++ )
  189.                 *long_ptr++ = read_long(fd, motorola);
  190.             }
  191.             break;
  192.         case D_RATIONAL:
  193.         case D_SRATIONAL:
  194.             {
  195.             rational *rational_ptr = (rational *) tag->value;
  196.  
  197.             for ( i = 0; i < n; i++ )
  198.                 read_rational(fd, motorola, rational_ptr++);
  199.             }
  200.             break;
  201.         case D_FLOAT:
  202.             /* Skip 4 byte IEEE floating point */
  203.             gbm_file_lseek(fd, 4 * len, SEEK_CUR);
  204.             break;
  205.         case D_DOUBLE:
  206.             /* Skip 8 byte IEEE double precision floating point */
  207.             gbm_file_lseek(fd, 8 * len, SEEK_CUR);
  208.             break;
  209.         default:
  210.             gbm_file_read(fd, tag->value, (int) len);
  211.             break;
  212.         }
  213.  
  214.     if ( len > 4 )
  215.         gbm_file_lseek(fd, old_pos, SEEK_SET);
  216.     else if ( len < 4 )
  217.         gbm_file_lseek(fd, 4L - len, SEEK_CUR);    /* advance past gap */
  218.  
  219.     return TE_OK;
  220.     }
  221. /*...e*/
  222. /*...sread_ifd:0:*/
  223. /*
  224. For the time being we will assume there is only one IFD in
  225. a given TIFF file. When this code was written, the author
  226. knew of no software packages that support multiple IFDs.
  227. */
  228.  
  229. /*...sclean_up_ifd:0:*/
  230. static void clean_up_ifd(IFD *ifd, int n)
  231.     {
  232.     int    i;
  233.     TAG    *tag;
  234.  
  235.     for ( i = 0; i < n; i++ )
  236.         {
  237.         tag = &(ifd->tags[i]);
  238.         if ( !(tag->type & 0x8000) )    /* its not read in */
  239.             tag_free(tag);
  240.         }
  241.     ifd_free(ifd);
  242.     }
  243. /*...e*/
  244.  
  245. static int read_ifd(int fd, BOOLEAN motorola, IFD **ifd_return)
  246.     {
  247.     IFD *ifd;
  248.     int i, ecode;
  249.  
  250.     if ( (ifd = ifd_malloc()) == NULL )
  251.         return TE_MEM;
  252.  
  253.     /* ensure we can handle all the tags */
  254.  
  255.     if ( (ifd->n_tags = read_short(fd, motorola)) > MAX_TAGS )
  256.         {
  257.         ifd_free(ifd); return TE_N_TAGS;
  258.         }
  259.  
  260.     /* get the tags */
  261.  
  262.     for ( i = 0; i < ifd->n_tags; i++ )
  263.         if ( (ecode = read_tag(fd, motorola, &(ifd->tags[i]))) != TE_OK )
  264.             {
  265.             clean_up_ifd(ifd, i);
  266.             return ecode;
  267.             }
  268.  
  269.     *ifd_return = ifd;
  270.  
  271.     return TE_OK;
  272.     }
  273. /*...e*/
  274. /*...sskip_ifd:0:*/
  275. /* Returns TRUE if there is another IFD afterwards */
  276.  
  277. static BOOLEAN skip_ifd(int fd, BOOLEAN motorola)
  278.     {
  279.     short n_tags = read_short(fd, motorola);
  280.     long offset_ifd;
  281.     gbm_file_lseek(fd, 12L * n_tags, SEEK_CUR);
  282.     offset_ifd = read_long(fd, motorola);
  283.     if ( offset_ifd == 0L )
  284.         return FALSE;
  285.     gbm_file_lseek(fd, offset_ifd, SEEK_SET);
  286.     return TRUE;
  287.     }
  288. /*...e*/
  289.  
  290. int read_ifh_and_ifd(int fd, int n_ifds_to_skip, IFH **ifh_return)
  291.     {
  292.     IFH    *ifh;
  293.     long    offset_ifd;
  294.     BOOLEAN    motorola;
  295.     int    ecode;
  296.  
  297.     if ( (ifh = ifh_malloc()) == NULL )
  298.         return TE_MEM;
  299.  
  300.     gbm_file_read(fd, (char *) &(ifh->byte_order), sizeof(short));
  301.     motorola = ( ifh->byte_order == ('M' << 8) + 'M' );
  302.  
  303.     /* Apparently, the following number has great univeral significance! */
  304.     /* See the TIFF 5.0 spec. for details! */
  305.  
  306.     if ( (ifh->version_no = read_short(fd, motorola)) != 42 )
  307.         {
  308.         ifh_free(ifh); return TE_VERSION;
  309.         }
  310.  
  311.     offset_ifd = read_long(fd, motorola);
  312.     gbm_file_lseek(fd, offset_ifd, SEEK_SET);
  313.     while ( n_ifds_to_skip-- > 0 )
  314.         if ( !skip_ifd(fd, motorola) )
  315.             return TE_N_IFD;
  316.  
  317.     if ( (ecode = read_ifd(fd, motorola, &(ifh->ifd))) != TE_OK )
  318.         {
  319.         ifh_free(ifh); return ecode;
  320.         }
  321.  
  322.     *ifh_return = ifh;
  323.  
  324.     return TE_OK;
  325.     }
  326. /*...e*/
  327. /*...slocate_tag:0:*/
  328. TAG *locate_tag(IFD *ifd, short type)
  329.     {
  330.     int i;
  331.  
  332.     for ( i = 0; i < ifd->n_tags; i++ )
  333.         if ( ifd->tags[i].type == type )
  334.             return &(ifd->tags[i]);
  335.     return NULL;
  336.     }
  337. /*...e*/
  338. /*...snumeric_tag:0:*/
  339. BOOLEAN numeric_tag(TAG *tag)
  340.     {
  341.     short t = tag->data_type;
  342.     return t == D_BYTE  ||
  343.            t == D_SHORT || t == D_SSHORT ||
  344.            t == D_LONG  || t == D_SLONG  ;
  345.     }
  346. /*...e*/
  347. /*...svalue_of_tag_n:0:*/
  348. /*
  349. For a numeric tag, return the value of the nth item in it.
  350. Upto the caller to know that tag is signed or unsigned.
  351. */
  352.  
  353. long value_of_tag_n(TAG *tag, int n)
  354.     {
  355.     switch ( tag->data_type )
  356.         {
  357.         case D_BYTE:
  358.             {
  359.             unsigned char *p = (unsigned char *) tag->value;
  360.             return (long) (unsigned long) p[n];
  361.             }
  362.         case D_SBYTE:
  363.             {
  364.             signed char *p = (signed char *) tag->value;
  365.             return (long) p[n];
  366.             }
  367.         case D_SHORT:
  368.             {
  369.             unsigned short *p = (unsigned short *) tag->value;
  370.             return (long) (unsigned long) p[n];
  371.             }
  372.         case D_SSHORT:
  373.             {
  374.             signed short *p = (signed short *) tag->value;
  375.             return (long) p[n];
  376.             }
  377.         case D_LONG:
  378.             {
  379.             unsigned long *p = (unsigned long *) tag->value;
  380.             return (long) p[n];
  381.             }
  382.         case D_SLONG:
  383.             {
  384.             signed long *p = (signed long *) tag->value;
  385.             return (long) p[n];
  386.             }
  387.         }
  388.     return 0L;
  389.     }
  390. /*...e*/
  391. /*...svalue_of_tag:0:*/
  392. /*
  393. For a numeric tag, return the value of the 1st value in it.
  394. This is usefull for tags that typically only have 1 value anyway.
  395. */
  396.  
  397. long value_of_tag(TAG *tag)
  398.     {
  399.     return value_of_tag_n(tag, 0);
  400.     }
  401. /*...e*/
  402. /*...sfree_ifh:0:*/
  403. void free_ifh(IFH *ifh)
  404.     {
  405.     IFD *ifd = ifh->ifd;
  406.     clean_up_ifd(ifd, ifd->n_tags);
  407.     ifh_free(ifh);
  408.     }
  409. /*...e*/
  410. /*...smake_ifh:0:*/
  411. /*
  412. Creates an empty IFH set up for the image.
  413. Also creates an IFD as part of the IFH.
  414. Use add_?_tag() routines to add tags to IFH's IFD.
  415. */
  416.  
  417. IFH *make_ifh(void)
  418.     {
  419.     IFH *ifh;
  420.     IFD *ifd;
  421.  
  422.     if ( (ifh = ifh_malloc()) == NULL )
  423.         return NULL;
  424.  
  425.     if ( (ifh->ifd = ifd = ifd_malloc()) == NULL )
  426.         {
  427.         ifh_free(ifh);
  428.         return NULL;
  429.         }
  430.  
  431.     ifh->byte_order = ('I' << 8) + 'I';
  432.     ifh->version_no = 42;
  433.  
  434.     ifd->n_tags = 0;
  435.  
  436.     return ifh;
  437.     }
  438. /*...e*/
  439. /*...sadd_byte_tag:0:*/
  440. BOOLEAN    add_byte_tag(IFD *ifd, short type, byte *value, int n)
  441.     {
  442.     byte *byte_ptr;
  443.     TAG *tag;
  444.  
  445.     if ( (byte_ptr = (byte *) malloc((size_t) (n * sizeof(byte)))) == NULL )
  446.         return FALSE;
  447.     if ( (tag = get_tag_slot(type, ifd)) == NULL )
  448.         return FALSE;
  449.     tag->type      = type;
  450.     tag->data_type = D_BYTE;
  451.     tag->length    = (long) n;
  452.     if ( value != NULL )
  453.         memcpy(tag->value = (char *) byte_ptr,
  454.                value,
  455.                n * sizeof(byte));
  456.     return TRUE;
  457.     }
  458. /*...e*/
  459. /*...sadd_ascii_tag:0:*/
  460. BOOLEAN    add_ascii_tag(IFD *ifd, short type, char *value)
  461.     {
  462.     char *ascii_ptr;
  463.     TAG *tag;
  464.     int n;
  465.  
  466.     n = strlen(value) + 1;
  467.     if ( (ascii_ptr = (char *) malloc((size_t) n)) == NULL )
  468.         return FALSE;
  469.     if ( (tag = get_tag_slot(type, ifd)) == NULL )
  470.         return FALSE;
  471.     tag->type      = type;
  472.     tag->data_type = D_ASCII;
  473.     tag->length    = (long) n;
  474.     strcpy(tag->value = ascii_ptr, value);
  475.     return TRUE;
  476.     }
  477. /*...e*/
  478. /*...sadd_short_tag:0:*/
  479. BOOLEAN    add_short_tag(IFD *ifd, short type, short *value, int n)
  480.     {
  481.     short *short_ptr;
  482.     TAG *tag;
  483.  
  484.     if ( (short_ptr = (short *) malloc((size_t) (n * sizeof(short)))) == NULL )
  485.         return FALSE;
  486.     if ( (tag = get_tag_slot(type, ifd)) == NULL )
  487.         return FALSE;
  488.     tag->type      = type;
  489.     tag->data_type = D_SHORT;
  490.     tag->length    = (long) n;
  491.     if ( value != NULL )
  492.         memcpy(tag->value = (char *) short_ptr,
  493.                value,
  494.                n * sizeof(short));
  495.     return TRUE;
  496.     }
  497. /*...e*/
  498. /*...sadd_long_tag:0:*/
  499. BOOLEAN    add_long_tag(IFD *ifd, short type, long *value, int n)
  500.     {
  501.     long *long_ptr;
  502.     TAG *tag;
  503.  
  504.     if ( (long_ptr = (long *) malloc((size_t) (n * sizeof(long)))) == NULL )
  505.         return FALSE;
  506.     if ( (tag = get_tag_slot(type, ifd)) == NULL )
  507.         return FALSE;
  508.     tag->type      = type;
  509.     tag->data_type = D_LONG;
  510.     tag->length    = (long) n;
  511.     if ( value != NULL )
  512.         memcpy(tag->value = (char *) long_ptr,
  513.                value,
  514.                n * sizeof(long));
  515.     return TRUE;
  516.     }
  517. /*...e*/
  518. /*...sadd_rational_tag:0:*/
  519. BOOLEAN    add_rational_tag(IFD *ifd, short type, rational *value, int n)
  520.     {
  521.     rational *rational_ptr;
  522.     TAG *tag;
  523.  
  524.     if ( (rational_ptr = (rational *) malloc((size_t) (n * sizeof(rational)))) == NULL )
  525.         return FALSE;
  526.     if ( (tag = get_tag_slot(type, ifd)) == NULL )
  527.         return FALSE;
  528.     tag->type      = type;
  529.     tag->data_type = D_RATIONAL;
  530.     tag->length    = (long) n;
  531.     if ( value != NULL )
  532.         memcpy(tag->value = (char *) rational_ptr,
  533.                value,
  534.                n * sizeof(rational));
  535.     return TRUE;
  536.     }
  537. /*...e*/
  538. /*...swrite_ifh_and_ifd:0:*/
  539. /*...spad:0:*/
  540. static BOOLEAN pad(int fd, int n)
  541.     {
  542.     static char padding[] = { 0, 0, 0, 0 };
  543.  
  544.     return gbm_file_write(fd, padding, n) == n;
  545.     }
  546. /*...e*/
  547. /*...swrite_short:0:*/
  548. static BOOLEAN write_short(int fd, short s)
  549.     {
  550.     byte b[2];
  551.  
  552.     b[0] = (byte)                   (s & 0x00ffU)      ;
  553.     b[1] = (byte) ((unsigned short) (s & 0xff00U) >> 8);
  554.  
  555.     return gbm_file_write(fd, b, 2) == 2;
  556.     }
  557. /*...e*/
  558. /*...swrite_long:0:*/
  559. static BOOLEAN write_long(int fd, long l)
  560.     {
  561.     byte b[4];
  562.  
  563.     b[0] = (byte)  (l & 0x000000ffUL);
  564.     b[1] = (byte) ((l & 0x0000ff00UL) >>  8);
  565.     b[2] = (byte) ((l & 0x00ff0000UL) >> 16);
  566.     b[3] = (byte) ((l & 0xff000000UL) >> 24);
  567.  
  568.     return gbm_file_write(fd, b, 4) == 4;
  569.     }
  570. /*...e*/
  571. /*...swrite_rational:0:*/
  572. static BOOLEAN write_rational(int fd, rational *rational)
  573.     {
  574.     return write_long(fd, rational->numerator  ) &&
  575.            write_long(fd, rational->denominator) ;
  576.     }
  577. /*...e*/
  578. /*...swrite_tag:0:*/
  579. static BOOLEAN write_tag(int fd, TAG *tag, long *offset_upto)
  580.     {
  581.     BOOLEAN    ok;
  582.     int    s, i, n, len;
  583.     long    offset_return_to;
  584.  
  585.     ok = write_short(fd, tag->type) &&
  586.          write_short(fd, tag->data_type) &&
  587.          write_long(fd, tag->length);
  588.  
  589.     if ( !ok )
  590.         return FALSE;
  591.  
  592.     /* if we can fit the tag into 4 bytes, do so */
  593.     /* else we will have to allocate some disc space */
  594.  
  595.     s = sizeof_data_type(tag->data_type);
  596.     n = (int) tag->length;
  597.     len = s * n;
  598.  
  599.     if ( len > 4 )
  600.         {
  601.         if ( !write_long(fd, *offset_upto) )
  602.             return FALSE;
  603.         offset_return_to = gbm_file_lseek(fd, 0L, SEEK_CUR);
  604.         gbm_file_lseek(fd, *offset_upto, SEEK_SET);
  605.         }        
  606.  
  607.     /* actually write the tag */
  608.  
  609.     switch ( tag->data_type )
  610.         {
  611.         case D_BYTE:
  612.         case D_ASCII:
  613.             if  ( gbm_file_write(fd, tag->value, n) != n )
  614.                 return FALSE;
  615.             break;
  616.         case D_SHORT:
  617.             {
  618.             short    *short_ptr = (short *) tag->value;
  619.  
  620.             for ( i = 0; i < n; i++ )
  621.                 if ( !write_short(fd, *short_ptr++) )
  622.                     return FALSE;
  623.             }
  624.             break;
  625.         case D_LONG:
  626.             {
  627.             long    *long_ptr = (long *) tag->value;
  628.  
  629.             for ( i = 0; i < n; i++ )
  630.                 if ( !write_long(fd, *long_ptr++) )
  631.                     return FALSE;
  632.             }
  633.             break;
  634.         case D_RATIONAL:
  635.             {
  636.             rational *rational_ptr = (rational *) tag->value;
  637.  
  638.             for ( i = 0; i < n; i++ )
  639.                 if ( !write_rational(fd, rational_ptr++) )
  640.                     return FALSE;
  641.             }
  642.             break;
  643.         }
  644.  
  645.     if ( len > 4 )
  646.         {
  647.         if ( (*offset_upto = gbm_file_lseek(fd, 0L, SEEK_CUR)) & 1L )
  648.             /* pad to make next offset even */
  649.             {
  650.             if ( !pad(fd, 1) )
  651.                 return FALSE;
  652.             (*offset_upto)++;
  653.             }
  654.         gbm_file_lseek(fd, offset_return_to, SEEK_SET);
  655.         }
  656.     else if ( len < 4 )
  657.         if ( !pad(fd, 4 - len) )
  658.             return FALSE;
  659.     return TRUE;
  660.     }
  661. /*...e*/
  662. /*...swrite_ifd:0:*/
  663. /*
  664. Given an IFD, write it out to disc.
  665. Also patch the IFH (which we know will be at the start of the file).
  666. In writing out a tag we may need some more disc space other than
  667. that for the IFD table. This occurs when a field is larger than
  668. 4 bytes. What we do is to keep a pointer to the next free space
  669. (after the table) and write_tag() will advance it if it uses any
  670. extra space.
  671. */
  672.  
  673. BOOLEAN    write_ifd(int fd, IFD *ifd)
  674.     {
  675.     int i, n;
  676.     long offset_upto;
  677.  
  678.     if ( !write_short(fd, (short) (n = ifd->n_tags)) )
  679.         return FALSE;
  680.  
  681.     /* write out tags */
  682.  
  683.     offset_upto = gbm_file_lseek(fd, 0L, SEEK_CUR) + n * 12L + 4L;
  684.         /* leave space for each tag plus next IFD ptr */
  685.  
  686.     for ( i = 0; i < n; i++ )
  687.         if ( !write_tag(fd, &(ifd->tags[i]), &offset_upto) )
  688.             return FALSE;
  689.  
  690.     /* done writing out the IFD, now put null next IFD pointer */
  691.  
  692.     if ( !write_long(fd, 0L) )
  693.         return FALSE;
  694.     
  695.     gbm_file_lseek(fd, offset_upto, SEEK_SET);
  696.  
  697.     return TRUE;
  698.     }
  699. /*...e*/
  700.  
  701. BOOLEAN    write_ifh_and_ifd(IFH *ifh, int fd)
  702.     {
  703.     return write_short(fd, ifh->byte_order) &&
  704.            write_short(fd, ifh->version_no) &&
  705.            write_long(fd, 8L) &&
  706.            write_ifd(fd, ifh->ifd);
  707.     }
  708. /*...e*/
  709. /*...supdate_byte_tag:0:*/
  710. void    update_byte_tag(IFD *ifd, short type, byte *value)
  711.     {
  712.     TAG    *tag;
  713.     int    n;
  714.  
  715.     tag = locate_tag(ifd, type);
  716.     n = (int) tag->length;
  717.     memcpy(tag->value, value, n * sizeof(byte));
  718.     }
  719. /*...e*/
  720. /*...supdate_ascii_tag:0:*/
  721. void    update_ascii_tag(IFD *ifd, short type, char *value)
  722.     {
  723.     TAG    *tag;
  724.     int    n;
  725.  
  726.     tag = locate_tag(ifd, type);
  727.     n = (int) tag->length;
  728.     memcpy(tag->value, value, n);
  729.     }
  730. /*...e*/
  731. /*...supdate_short_tag:0:*/
  732. void    update_short_tag(IFD *ifd, short type, short *value)
  733.     {
  734.     TAG    *tag;
  735.     int    n;
  736.  
  737.     tag = locate_tag(ifd, type);
  738.     n = (int) tag->length;
  739.     memcpy(tag->value, value, n * sizeof(short));
  740.     }
  741. /*...e*/
  742. /*...supdate_long_tag:0:*/
  743. void    update_long_tag(IFD *ifd, short type, long *value)
  744.     {
  745.     TAG    *tag;
  746.     int    n;
  747.  
  748.     tag = locate_tag(ifd, type);
  749.     n = (int) tag->length;
  750.     memcpy(tag->value, value, n * sizeof(long));
  751.     }
  752. /*...e*/
  753. /*...supdate_rational_tag:0:*/
  754. void    update_rational_tag(IFD *ifd, short type, rational *value)
  755.     {
  756.     TAG    *tag;
  757.     int    n;
  758.  
  759.     tag = locate_tag(ifd, type);
  760.     n = (int) tag->length;
  761.     memcpy(tag->value, value, n * sizeof(rational));
  762.     }
  763. /*...e*/
  764. /*...supdate_ifd:0:*/
  765. /*
  766. Go back to the IFD, and rewrite it.
  767. */
  768.  
  769. BOOLEAN    update_ifd(IFD *ifd, int fd)
  770.     {
  771.     gbm_file_lseek(fd, 8L, SEEK_SET);
  772.     return write_ifd(fd, ifd);
  773.     }
  774. /*...e*/
  775.