tif_flush.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 TIFFFlush(TIFF *tif)
  29. {
  30. if (tif->tif_mode == O_RDONLY)
  31. return 1;
  32. if (!TIFFFlushData(tif))
  33. return (0);
  34. /* In update (r+) mode we try to detect the case where
  35. only the strip/tile map has been altered, and we try to
  36. rewrite only that portion of the directory without
  37. making any other changes */
  38. if ((tif->tif_flags & TIFF_DIRTYSTRIP) &&
  39. !(tif->tif_flags & TIFF_DIRTYDIRECT) && tif->tif_mode == O_RDWR)
  40. {
  41. if (TIFFForceStrileArrayWriting(tif))
  42. return 1;
  43. }
  44. if ((tif->tif_flags & (TIFF_DIRTYDIRECT | TIFF_DIRTYSTRIP)) &&
  45. !TIFFRewriteDirectory(tif))
  46. return (0);
  47. return (1);
  48. }
  49. /*
  50. * This is an advanced writing function that must be used in a particular
  51. * sequence, and together with TIFFDeferStrileArrayWriting(),
  52. * to make its intended effect. Its aim is to force the writing of
  53. * the [Strip/Tile][Offsets/ByteCounts] arrays at the end of the file, when
  54. * they have not yet been rewritten.
  55. *
  56. * The typical sequence of calls is:
  57. * TIFFOpen()
  58. * [ TIFFCreateDirectory(tif) ]
  59. * Set fields with calls to TIFFSetField(tif, ...)
  60. * TIFFDeferStrileArrayWriting(tif)
  61. * TIFFWriteCheck(tif, ...)
  62. * TIFFWriteDirectory(tif)
  63. * ... potentially create other directories and come back to the above directory
  64. * TIFFForceStrileArrayWriting(tif)
  65. *
  66. * Returns 1 in case of success, 0 otherwise.
  67. */
  68. int TIFFForceStrileArrayWriting(TIFF *tif)
  69. {
  70. static const char module[] = "TIFFForceStrileArrayWriting";
  71. const int isTiled = TIFFIsTiled(tif);
  72. if (tif->tif_mode == O_RDONLY)
  73. {
  74. TIFFErrorExtR(tif, tif->tif_name, "File opened in read-only mode");
  75. return 0;
  76. }
  77. if (tif->tif_diroff == 0)
  78. {
  79. TIFFErrorExtR(tif, module, "Directory has not yet been written");
  80. return 0;
  81. }
  82. if ((tif->tif_flags & TIFF_DIRTYDIRECT) != 0)
  83. {
  84. TIFFErrorExtR(tif, module,
  85. "Directory has changes other than the strile arrays. "
  86. "TIFFRewriteDirectory() should be called instead");
  87. return 0;
  88. }
  89. if (!(tif->tif_flags & TIFF_DIRTYSTRIP))
  90. {
  91. if (!(tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
  92. tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
  93. tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
  94. tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
  95. tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
  96. tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
  97. tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
  98. tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0))
  99. {
  100. TIFFErrorExtR(tif, module,
  101. "Function not called together with "
  102. "TIFFDeferStrileArrayWriting()");
  103. return 0;
  104. }
  105. if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif))
  106. return 0;
  107. }
  108. if (_TIFFRewriteField(tif,
  109. isTiled ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS,
  110. TIFF_LONG8, tif->tif_dir.td_nstrips,
  111. tif->tif_dir.td_stripoffset_p) &&
  112. _TIFFRewriteField(
  113. tif, isTiled ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS,
  114. TIFF_LONG8, tif->tif_dir.td_nstrips,
  115. tif->tif_dir.td_stripbytecount_p))
  116. {
  117. tif->tif_flags &= ~TIFF_DIRTYSTRIP;
  118. tif->tif_flags &= ~TIFF_BEENWRITING;
  119. return 1;
  120. }
  121. return 0;
  122. }
  123. /*
  124. * Flush buffered data to the file.
  125. *
  126. * Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING
  127. * is not set, so that TIFFFlush() will proceed to write out the directory.
  128. * The documentation says returning 1 is an error indicator, but not having
  129. * been writing isn't exactly a an error. Hopefully this doesn't cause
  130. * problems for other people.
  131. */
  132. int TIFFFlushData(TIFF *tif)
  133. {
  134. if ((tif->tif_flags & TIFF_BEENWRITING) == 0)
  135. return (1);
  136. if (tif->tif_flags & TIFF_POSTENCODE)
  137. {
  138. tif->tif_flags &= ~TIFF_POSTENCODE;
  139. if (!(*tif->tif_postencode)(tif))
  140. return (0);
  141. }
  142. return (TIFFFlushData1(tif));
  143. }