tif_flush.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 1988-1997 Sam Leffler
  3. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. /*
  25. * TIFF Library.
  26. */
  27. #include "tiffiop.h"
  28. int
  29. TIFFFlush(TIFF* tif)
  30. {
  31. if( tif->tif_mode == O_RDONLY )
  32. return 1;
  33. if (!TIFFFlushData(tif))
  34. return (0);
  35. /* In update (r+) mode we try to detect the case where
  36. only the strip/tile map has been altered, and we try to
  37. rewrite only that portion of the directory without
  38. making any other changes */
  39. if( (tif->tif_flags & TIFF_DIRTYSTRIP)
  40. && !(tif->tif_flags & TIFF_DIRTYDIRECT)
  41. && tif->tif_mode == O_RDWR )
  42. {
  43. if( TIFFForceStrileArrayWriting(tif) )
  44. return 1;
  45. }
  46. if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP))
  47. && !TIFFRewriteDirectory(tif))
  48. return (0);
  49. return (1);
  50. }
  51. /*
  52. * This is an advanced writing function that must be used in a particular
  53. * sequence, and together with TIFFDeferStrileArrayWriting(),
  54. * to make its intended effect. Its aim is to force the writing of
  55. * the [Strip/Tile][Offsets/ByteCounts] arrays at the end of the file, when
  56. * they have not yet been rewritten.
  57. *
  58. * The typical sequence of calls is:
  59. * TIFFOpen()
  60. * [ TIFFCreateDirectory(tif) ]
  61. * Set fields with calls to TIFFSetField(tif, ...)
  62. * TIFFDeferStrileArrayWriting(tif)
  63. * TIFFWriteCheck(tif, ...)
  64. * TIFFWriteDirectory(tif)
  65. * ... potentially create other directories and come back to the above directory
  66. * TIFFForceStrileArrayWriting(tif)
  67. *
  68. * Returns 1 in case of success, 0 otherwise.
  69. */
  70. int TIFFForceStrileArrayWriting(TIFF* tif)
  71. {
  72. static const char module[] = "TIFFForceStrileArrayWriting";
  73. const int isTiled = TIFFIsTiled(tif);
  74. if (tif->tif_mode == O_RDONLY)
  75. {
  76. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  77. "File opened in read-only mode");
  78. return 0;
  79. }
  80. if( tif->tif_diroff == 0 )
  81. {
  82. TIFFErrorExt(tif->tif_clientdata, module,
  83. "Directory has not yet been written");
  84. return 0;
  85. }
  86. if( (tif->tif_flags & TIFF_DIRTYDIRECT) != 0 )
  87. {
  88. TIFFErrorExt(tif->tif_clientdata, module,
  89. "Directory has changes other than the strile arrays. "
  90. "TIFFRewriteDirectory() should be called instead");
  91. return 0;
  92. }
  93. if( !(tif->tif_flags & TIFF_DIRTYSTRIP) )
  94. {
  95. if( !(tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
  96. tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
  97. tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
  98. tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
  99. tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
  100. tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
  101. tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
  102. tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0) )
  103. {
  104. TIFFErrorExt(tif->tif_clientdata, module,
  105. "Function not called together with "
  106. "TIFFDeferStrileArrayWriting()");
  107. return 0;
  108. }
  109. if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif))
  110. return 0;
  111. }
  112. if( _TIFFRewriteField( tif,
  113. isTiled ? TIFFTAG_TILEOFFSETS :
  114. TIFFTAG_STRIPOFFSETS,
  115. TIFF_LONG8,
  116. tif->tif_dir.td_nstrips,
  117. tif->tif_dir.td_stripoffset_p )
  118. && _TIFFRewriteField( tif,
  119. isTiled ? TIFFTAG_TILEBYTECOUNTS :
  120. TIFFTAG_STRIPBYTECOUNTS,
  121. TIFF_LONG8,
  122. tif->tif_dir.td_nstrips,
  123. tif->tif_dir.td_stripbytecount_p ) )
  124. {
  125. tif->tif_flags &= ~TIFF_DIRTYSTRIP;
  126. tif->tif_flags &= ~TIFF_BEENWRITING;
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. /*
  132. * Flush buffered data to the file.
  133. *
  134. * Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING
  135. * is not set, so that TIFFFlush() will proceed to write out the directory.
  136. * The documentation says returning 1 is an error indicator, but not having
  137. * been writing isn't exactly a an error. Hopefully this doesn't cause
  138. * problems for other people.
  139. */
  140. int
  141. TIFFFlushData(TIFF* tif)
  142. {
  143. if ((tif->tif_flags & TIFF_BEENWRITING) == 0)
  144. return (1);
  145. if (tif->tif_flags & TIFF_POSTENCODE) {
  146. tif->tif_flags &= ~TIFF_POSTENCODE;
  147. if (!(*tif->tif_postencode)(tif))
  148. return (0);
  149. }
  150. return (TIFFFlushData1(tif));
  151. }
  152. /* vim: set ts=8 sts=8 sw=8 noet: */
  153. /*
  154. * Local Variables:
  155. * mode: c
  156. * c-basic-offset: 8
  157. * fill-column: 78
  158. * End:
  159. */