tif_flush.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* $Id: tif_flush.c,v 1.9 2010-03-31 06:40:10 fwarmerdam Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library.
  27. */
  28. #include "tiffiop.h"
  29. int
  30. TIFFFlush(TIFF* tif)
  31. {
  32. if( tif->tif_mode == O_RDONLY )
  33. return 1;
  34. if (!TIFFFlushData(tif))
  35. return (0);
  36. /* In update (r+) mode we try to detect the case where
  37. only the strip/tile map has been altered, and we try to
  38. rewrite only that portion of the directory without
  39. making any other changes */
  40. if( (tif->tif_flags & TIFF_DIRTYSTRIP)
  41. && !(tif->tif_flags & TIFF_DIRTYDIRECT)
  42. && tif->tif_mode == O_RDWR )
  43. {
  44. uint64 *offsets=NULL, *sizes=NULL;
  45. if( TIFFIsTiled(tif) )
  46. {
  47. if( TIFFGetField( tif, TIFFTAG_TILEOFFSETS, &offsets )
  48. && TIFFGetField( tif, TIFFTAG_TILEBYTECOUNTS, &sizes )
  49. && _TIFFRewriteField( tif, TIFFTAG_TILEOFFSETS, TIFF_LONG8,
  50. tif->tif_dir.td_nstrips, offsets )
  51. && _TIFFRewriteField( tif, TIFFTAG_TILEBYTECOUNTS, TIFF_LONG8,
  52. tif->tif_dir.td_nstrips, sizes ) )
  53. {
  54. tif->tif_flags &= ~TIFF_DIRTYSTRIP;
  55. tif->tif_flags &= ~TIFF_BEENWRITING;
  56. return 1;
  57. }
  58. }
  59. else
  60. {
  61. if( TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &offsets )
  62. && TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &sizes )
  63. && _TIFFRewriteField( tif, TIFFTAG_STRIPOFFSETS, TIFF_LONG8,
  64. tif->tif_dir.td_nstrips, offsets )
  65. && _TIFFRewriteField( tif, TIFFTAG_STRIPBYTECOUNTS, TIFF_LONG8,
  66. tif->tif_dir.td_nstrips, sizes ) )
  67. {
  68. tif->tif_flags &= ~TIFF_DIRTYSTRIP;
  69. tif->tif_flags &= ~TIFF_BEENWRITING;
  70. return 1;
  71. }
  72. }
  73. }
  74. if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP))
  75. && !TIFFRewriteDirectory(tif))
  76. return (0);
  77. return (1);
  78. }
  79. /*
  80. * Flush buffered data to the file.
  81. *
  82. * Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING
  83. * is not set, so that TIFFFlush() will proceed to write out the directory.
  84. * The documentation says returning 1 is an error indicator, but not having
  85. * been writing isn't exactly a an error. Hopefully this doesn't cause
  86. * problems for other people.
  87. */
  88. int
  89. TIFFFlushData(TIFF* tif)
  90. {
  91. if ((tif->tif_flags & TIFF_BEENWRITING) == 0)
  92. return (1);
  93. if (tif->tif_flags & TIFF_POSTENCODE) {
  94. tif->tif_flags &= ~TIFF_POSTENCODE;
  95. if (!(*tif->tif_postencode)(tif))
  96. return (0);
  97. }
  98. return (TIFFFlushData1(tif));
  99. }
  100. /* vim: set ts=8 sts=8 sw=8 noet: */
  101. /*
  102. * Local Variables:
  103. * mode: c
  104. * c-basic-offset: 8
  105. * fill-column: 78
  106. * End:
  107. */