tif_write.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. * Scanline-oriented Write Support
  28. */
  29. #include "tiffiop.h"
  30. #include <stdio.h>
  31. #define STRIPINCR 20 /* expansion factor on strip array */
  32. #define WRITECHECKSTRIPS(tif, module) \
  33. (((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),0,module))
  34. #define WRITECHECKTILES(tif, module) \
  35. (((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),1,module))
  36. #define BUFFERCHECK(tif) \
  37. ((((tif)->tif_flags & TIFF_BUFFERSETUP) && tif->tif_rawdata) || \
  38. TIFFWriteBufferSetup((tif), NULL, (tmsize_t) -1))
  39. static int TIFFGrowStrips(TIFF* tif, uint32 delta, const char* module);
  40. static int TIFFAppendToStrip(TIFF* tif, uint32 strip, uint8* data, tmsize_t cc);
  41. int
  42. TIFFWriteScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
  43. {
  44. static const char module[] = "TIFFWriteScanline";
  45. register TIFFDirectory *td;
  46. int status, imagegrew = 0;
  47. uint32 strip;
  48. if (!WRITECHECKSTRIPS(tif, module))
  49. return (-1);
  50. /*
  51. * Handle delayed allocation of data buffer. This
  52. * permits it to be sized more intelligently (using
  53. * directory information).
  54. */
  55. if (!BUFFERCHECK(tif))
  56. return (-1);
  57. tif->tif_flags |= TIFF_BUF4WRITE; /* not strictly sure this is right*/
  58. td = &tif->tif_dir;
  59. /*
  60. * Extend image length if needed
  61. * (but only for PlanarConfig=1).
  62. */
  63. if (row >= td->td_imagelength) { /* extend image */
  64. if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  65. TIFFErrorExt(tif->tif_clientdata, module,
  66. "Can not change \"ImageLength\" when using separate planes");
  67. return (-1);
  68. }
  69. td->td_imagelength = row+1;
  70. imagegrew = 1;
  71. }
  72. /*
  73. * Calculate strip and check for crossings.
  74. */
  75. if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  76. if (sample >= td->td_samplesperpixel) {
  77. TIFFErrorExt(tif->tif_clientdata, module,
  78. "%lu: Sample out of range, max %lu",
  79. (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
  80. return (-1);
  81. }
  82. strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
  83. } else
  84. strip = row / td->td_rowsperstrip;
  85. /*
  86. * Check strip array to make sure there's space. We don't support
  87. * dynamically growing files that have data organized in separate
  88. * bitplanes because it's too painful. In that case we require that
  89. * the imagelength be set properly before the first write (so that the
  90. * strips array will be fully allocated above).
  91. */
  92. if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module))
  93. return (-1);
  94. if (strip != tif->tif_curstrip) {
  95. /*
  96. * Changing strips -- flush any data present.
  97. */
  98. if (!TIFFFlushData(tif))
  99. return (-1);
  100. tif->tif_curstrip = strip;
  101. /*
  102. * Watch out for a growing image. The value of strips/image
  103. * will initially be 1 (since it can't be deduced until the
  104. * imagelength is known).
  105. */
  106. if (strip >= td->td_stripsperimage && imagegrew)
  107. td->td_stripsperimage =
  108. TIFFhowmany_32(td->td_imagelength,td->td_rowsperstrip);
  109. if (td->td_stripsperimage == 0) {
  110. TIFFErrorExt(tif->tif_clientdata, module, "Zero strips per image");
  111. return (-1);
  112. }
  113. tif->tif_row =
  114. (strip % td->td_stripsperimage) * td->td_rowsperstrip;
  115. if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  116. if (!(*tif->tif_setupencode)(tif))
  117. return (-1);
  118. tif->tif_flags |= TIFF_CODERSETUP;
  119. }
  120. tif->tif_rawcc = 0;
  121. tif->tif_rawcp = tif->tif_rawdata;
  122. if( td->td_stripbytecount_p[strip] > 0 )
  123. {
  124. /* if we are writing over existing tiles, zero length */
  125. td->td_stripbytecount_p[strip] = 0;
  126. /* this forces TIFFAppendToStrip() to do a seek */
  127. tif->tif_curoff = 0;
  128. }
  129. if (!(*tif->tif_preencode)(tif, sample))
  130. return (-1);
  131. tif->tif_flags |= TIFF_POSTENCODE;
  132. }
  133. /*
  134. * Ensure the write is either sequential or at the
  135. * beginning of a strip (or that we can randomly
  136. * access the data -- i.e. no encoding).
  137. */
  138. if (row != tif->tif_row) {
  139. if (row < tif->tif_row) {
  140. /*
  141. * Moving backwards within the same strip:
  142. * backup to the start and then decode
  143. * forward (below).
  144. */
  145. tif->tif_row = (strip % td->td_stripsperimage) *
  146. td->td_rowsperstrip;
  147. tif->tif_rawcp = tif->tif_rawdata;
  148. }
  149. /*
  150. * Seek forward to the desired row.
  151. */
  152. if (!(*tif->tif_seek)(tif, row - tif->tif_row))
  153. return (-1);
  154. tif->tif_row = row;
  155. }
  156. /* swab if needed - note that source buffer will be altered */
  157. tif->tif_postdecode( tif, (uint8*) buf, tif->tif_scanlinesize );
  158. status = (*tif->tif_encoderow)(tif, (uint8*) buf,
  159. tif->tif_scanlinesize, sample);
  160. /* we are now poised at the beginning of the next row */
  161. tif->tif_row = row + 1;
  162. return (status);
  163. }
  164. /* Make sure that at the first attempt of rewriting a tile/strip, we will have */
  165. /* more bytes available in the output buffer than the previous byte count, */
  166. /* so that TIFFAppendToStrip() will detect the overflow when it is called the first */
  167. /* time if the new compressed tile is bigger than the older one. (GDAL #4771) */
  168. static int _TIFFReserveLargeEnoughWriteBuffer(TIFF* tif, uint32 strip_or_tile)
  169. {
  170. TIFFDirectory *td = &tif->tif_dir;
  171. if( td->td_stripbytecount_p[strip_or_tile] > 0 )
  172. {
  173. /* The +1 is to ensure at least one extra bytes */
  174. /* The +4 is because the LZW encoder flushes 4 bytes before the limit */
  175. uint64 safe_buffer_size = (uint64)(td->td_stripbytecount_p[strip_or_tile] + 1 + 4);
  176. if( tif->tif_rawdatasize <= (tmsize_t)safe_buffer_size )
  177. {
  178. if( !(TIFFWriteBufferSetup(tif, NULL,
  179. (tmsize_t)TIFFroundup_64(safe_buffer_size, 1024))) )
  180. return 0;
  181. }
  182. /* Force TIFFAppendToStrip() to consider placing data at end
  183. of file. */
  184. tif->tif_curoff = 0;
  185. }
  186. return 1;
  187. }
  188. /*
  189. * Encode the supplied data and write it to the
  190. * specified strip.
  191. *
  192. * NB: Image length must be setup before writing.
  193. */
  194. tmsize_t
  195. TIFFWriteEncodedStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc)
  196. {
  197. static const char module[] = "TIFFWriteEncodedStrip";
  198. TIFFDirectory *td = &tif->tif_dir;
  199. uint16 sample;
  200. if (!WRITECHECKSTRIPS(tif, module))
  201. return ((tmsize_t) -1);
  202. /*
  203. * Check strip array to make sure there's space.
  204. * We don't support dynamically growing files that
  205. * have data organized in separate bitplanes because
  206. * it's too painful. In that case we require that
  207. * the imagelength be set properly before the first
  208. * write (so that the strips array will be fully
  209. * allocated above).
  210. */
  211. if (strip >= td->td_nstrips) {
  212. if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  213. TIFFErrorExt(tif->tif_clientdata, module,
  214. "Can not grow image by strips when using separate planes");
  215. return ((tmsize_t) -1);
  216. }
  217. if (!TIFFGrowStrips(tif, 1, module))
  218. return ((tmsize_t) -1);
  219. td->td_stripsperimage =
  220. TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip);
  221. }
  222. /*
  223. * Handle delayed allocation of data buffer. This
  224. * permits it to be sized according to the directory
  225. * info.
  226. */
  227. if (!BUFFERCHECK(tif))
  228. return ((tmsize_t) -1);
  229. tif->tif_flags |= TIFF_BUF4WRITE;
  230. tif->tif_curstrip = strip;
  231. if( !_TIFFReserveLargeEnoughWriteBuffer(tif, strip) ) {
  232. return ((tmsize_t)(-1));
  233. }
  234. tif->tif_rawcc = 0;
  235. tif->tif_rawcp = tif->tif_rawdata;
  236. if (td->td_stripsperimage == 0) {
  237. TIFFErrorExt(tif->tif_clientdata, module, "Zero strips per image");
  238. return ((tmsize_t) -1);
  239. }
  240. tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
  241. if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  242. if (!(*tif->tif_setupencode)(tif))
  243. return ((tmsize_t) -1);
  244. tif->tif_flags |= TIFF_CODERSETUP;
  245. }
  246. tif->tif_flags &= ~TIFF_POSTENCODE;
  247. /* shortcut to avoid an extra memcpy() */
  248. if( td->td_compression == COMPRESSION_NONE )
  249. {
  250. /* swab if needed - note that source buffer will be altered */
  251. tif->tif_postdecode( tif, (uint8*) data, cc );
  252. if (!isFillOrder(tif, td->td_fillorder) &&
  253. (tif->tif_flags & TIFF_NOBITREV) == 0)
  254. TIFFReverseBits((uint8*) data, cc);
  255. if (cc > 0 &&
  256. !TIFFAppendToStrip(tif, strip, (uint8*) data, cc))
  257. return ((tmsize_t) -1);
  258. return (cc);
  259. }
  260. sample = (uint16)(strip / td->td_stripsperimage);
  261. if (!(*tif->tif_preencode)(tif, sample))
  262. return ((tmsize_t) -1);
  263. /* swab if needed - note that source buffer will be altered */
  264. tif->tif_postdecode( tif, (uint8*) data, cc );
  265. if (!(*tif->tif_encodestrip)(tif, (uint8*) data, cc, sample))
  266. return ((tmsize_t) -1);
  267. if (!(*tif->tif_postencode)(tif))
  268. return ((tmsize_t) -1);
  269. if (!isFillOrder(tif, td->td_fillorder) &&
  270. (tif->tif_flags & TIFF_NOBITREV) == 0)
  271. TIFFReverseBits(tif->tif_rawdata, tif->tif_rawcc);
  272. if (tif->tif_rawcc > 0 &&
  273. !TIFFAppendToStrip(tif, strip, tif->tif_rawdata, tif->tif_rawcc))
  274. return ((tmsize_t) -1);
  275. tif->tif_rawcc = 0;
  276. tif->tif_rawcp = tif->tif_rawdata;
  277. return (cc);
  278. }
  279. /*
  280. * Write the supplied data to the specified strip.
  281. *
  282. * NB: Image length must be setup before writing.
  283. */
  284. tmsize_t
  285. TIFFWriteRawStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc)
  286. {
  287. static const char module[] = "TIFFWriteRawStrip";
  288. TIFFDirectory *td = &tif->tif_dir;
  289. if (!WRITECHECKSTRIPS(tif, module))
  290. return ((tmsize_t) -1);
  291. /*
  292. * Check strip array to make sure there's space.
  293. * We don't support dynamically growing files that
  294. * have data organized in separate bitplanes because
  295. * it's too painful. In that case we require that
  296. * the imagelength be set properly before the first
  297. * write (so that the strips array will be fully
  298. * allocated above).
  299. */
  300. if (strip >= td->td_nstrips) {
  301. if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  302. TIFFErrorExt(tif->tif_clientdata, module,
  303. "Can not grow image by strips when using separate planes");
  304. return ((tmsize_t) -1);
  305. }
  306. /*
  307. * Watch out for a growing image. The value of
  308. * strips/image will initially be 1 (since it
  309. * can't be deduced until the imagelength is known).
  310. */
  311. if (strip >= td->td_stripsperimage)
  312. td->td_stripsperimage =
  313. TIFFhowmany_32(td->td_imagelength,td->td_rowsperstrip);
  314. if (!TIFFGrowStrips(tif, 1, module))
  315. return ((tmsize_t) -1);
  316. }
  317. tif->tif_curstrip = strip;
  318. if (td->td_stripsperimage == 0) {
  319. TIFFErrorExt(tif->tif_clientdata, module,"Zero strips per image");
  320. return ((tmsize_t) -1);
  321. }
  322. tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
  323. return (TIFFAppendToStrip(tif, strip, (uint8*) data, cc) ?
  324. cc : (tmsize_t) -1);
  325. }
  326. /*
  327. * Write and compress a tile of data. The
  328. * tile is selected by the (x,y,z,s) coordinates.
  329. */
  330. tmsize_t
  331. TIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
  332. {
  333. if (!TIFFCheckTile(tif, x, y, z, s))
  334. return ((tmsize_t)(-1));
  335. /*
  336. * NB: A tile size of -1 is used instead of tif_tilesize knowing
  337. * that TIFFWriteEncodedTile will clamp this to the tile size.
  338. * This is done because the tile size may not be defined until
  339. * after the output buffer is setup in TIFFWriteBufferSetup.
  340. */
  341. return (TIFFWriteEncodedTile(tif,
  342. TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
  343. }
  344. /*
  345. * Encode the supplied data and write it to the
  346. * specified tile. There must be space for the
  347. * data. The function clamps individual writes
  348. * to a tile to the tile size, but does not (and
  349. * can not) check that multiple writes to the same
  350. * tile do not write more than tile size data.
  351. *
  352. * NB: Image length must be setup before writing; this
  353. * interface does not support automatically growing
  354. * the image on each write (as TIFFWriteScanline does).
  355. */
  356. tmsize_t
  357. TIFFWriteEncodedTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)
  358. {
  359. static const char module[] = "TIFFWriteEncodedTile";
  360. TIFFDirectory *td;
  361. uint16 sample;
  362. uint32 howmany32;
  363. if (!WRITECHECKTILES(tif, module))
  364. return ((tmsize_t)(-1));
  365. td = &tif->tif_dir;
  366. if (tile >= td->td_nstrips) {
  367. TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",
  368. (unsigned long) tile, (unsigned long) td->td_nstrips);
  369. return ((tmsize_t)(-1));
  370. }
  371. /*
  372. * Handle delayed allocation of data buffer. This
  373. * permits it to be sized more intelligently (using
  374. * directory information).
  375. */
  376. if (!BUFFERCHECK(tif))
  377. return ((tmsize_t)(-1));
  378. tif->tif_flags |= TIFF_BUF4WRITE;
  379. tif->tif_curtile = tile;
  380. if( !_TIFFReserveLargeEnoughWriteBuffer(tif, tile) ) {
  381. return ((tmsize_t)(-1));
  382. }
  383. tif->tif_rawcc = 0;
  384. tif->tif_rawcp = tif->tif_rawdata;
  385. /*
  386. * Compute tiles per row & per column to compute
  387. * current row and column
  388. */
  389. howmany32=TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
  390. if (howmany32 == 0) {
  391. TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
  392. return ((tmsize_t)(-1));
  393. }
  394. tif->tif_row = (tile % howmany32) * td->td_tilelength;
  395. howmany32=TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
  396. if (howmany32 == 0) {
  397. TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
  398. return ((tmsize_t)(-1));
  399. }
  400. tif->tif_col = (tile % howmany32) * td->td_tilewidth;
  401. if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  402. if (!(*tif->tif_setupencode)(tif))
  403. return ((tmsize_t)(-1));
  404. tif->tif_flags |= TIFF_CODERSETUP;
  405. }
  406. tif->tif_flags &= ~TIFF_POSTENCODE;
  407. /*
  408. * Clamp write amount to the tile size. This is mostly
  409. * done so that callers can pass in some large number
  410. * (e.g. -1) and have the tile size used instead.
  411. */
  412. if ( cc < 1 || cc > tif->tif_tilesize)
  413. cc = tif->tif_tilesize;
  414. /* shortcut to avoid an extra memcpy() */
  415. if( td->td_compression == COMPRESSION_NONE )
  416. {
  417. /* swab if needed - note that source buffer will be altered */
  418. tif->tif_postdecode( tif, (uint8*) data, cc );
  419. if (!isFillOrder(tif, td->td_fillorder) &&
  420. (tif->tif_flags & TIFF_NOBITREV) == 0)
  421. TIFFReverseBits((uint8*) data, cc);
  422. if (cc > 0 &&
  423. !TIFFAppendToStrip(tif, tile, (uint8*) data, cc))
  424. return ((tmsize_t) -1);
  425. return (cc);
  426. }
  427. sample = (uint16)(tile/td->td_stripsperimage);
  428. if (!(*tif->tif_preencode)(tif, sample))
  429. return ((tmsize_t)(-1));
  430. /* swab if needed - note that source buffer will be altered */
  431. tif->tif_postdecode( tif, (uint8*) data, cc );
  432. if (!(*tif->tif_encodetile)(tif, (uint8*) data, cc, sample))
  433. return ((tmsize_t) -1);
  434. if (!(*tif->tif_postencode)(tif))
  435. return ((tmsize_t)(-1));
  436. if (!isFillOrder(tif, td->td_fillorder) &&
  437. (tif->tif_flags & TIFF_NOBITREV) == 0)
  438. TIFFReverseBits((uint8*)tif->tif_rawdata, tif->tif_rawcc);
  439. if (tif->tif_rawcc > 0 && !TIFFAppendToStrip(tif, tile,
  440. tif->tif_rawdata, tif->tif_rawcc))
  441. return ((tmsize_t)(-1));
  442. tif->tif_rawcc = 0;
  443. tif->tif_rawcp = tif->tif_rawdata;
  444. return (cc);
  445. }
  446. /*
  447. * Write the supplied data to the specified strip.
  448. * There must be space for the data; we don't check
  449. * if strips overlap!
  450. *
  451. * NB: Image length must be setup before writing; this
  452. * interface does not support automatically growing
  453. * the image on each write (as TIFFWriteScanline does).
  454. */
  455. tmsize_t
  456. TIFFWriteRawTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)
  457. {
  458. static const char module[] = "TIFFWriteRawTile";
  459. if (!WRITECHECKTILES(tif, module))
  460. return ((tmsize_t)(-1));
  461. if (tile >= tif->tif_dir.td_nstrips) {
  462. TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",
  463. (unsigned long) tile,
  464. (unsigned long) tif->tif_dir.td_nstrips);
  465. return ((tmsize_t)(-1));
  466. }
  467. return (TIFFAppendToStrip(tif, tile, (uint8*) data, cc) ?
  468. cc : (tmsize_t)(-1));
  469. }
  470. #define isUnspecified(tif, f) \
  471. (TIFFFieldSet(tif,f) && (tif)->tif_dir.td_imagelength == 0)
  472. int
  473. TIFFSetupStrips(TIFF* tif)
  474. {
  475. TIFFDirectory* td = &tif->tif_dir;
  476. if (isTiled(tif))
  477. td->td_stripsperimage =
  478. isUnspecified(tif, FIELD_TILEDIMENSIONS) ?
  479. td->td_samplesperpixel : TIFFNumberOfTiles(tif);
  480. else
  481. td->td_stripsperimage =
  482. isUnspecified(tif, FIELD_ROWSPERSTRIP) ?
  483. td->td_samplesperpixel : TIFFNumberOfStrips(tif);
  484. td->td_nstrips = td->td_stripsperimage;
  485. /* TIFFWriteDirectoryTagData has a limitation to 0x80000000U bytes */
  486. if( td->td_nstrips >= 0x80000000U / ((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U) )
  487. {
  488. TIFFErrorExt(tif->tif_clientdata, "TIFFSetupStrips",
  489. "Too large Strip/Tile Offsets/ByteCounts arrays");
  490. return 0;
  491. }
  492. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  493. td->td_stripsperimage /= td->td_samplesperpixel;
  494. td->td_stripoffset_p = (uint64 *)
  495. _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
  496. "for \"StripOffsets\" array");
  497. td->td_stripbytecount_p = (uint64 *)
  498. _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
  499. "for \"StripByteCounts\" array");
  500. if (td->td_stripoffset_p == NULL || td->td_stripbytecount_p == NULL)
  501. return (0);
  502. /*
  503. * Place data at the end-of-file
  504. * (by setting offsets to zero).
  505. */
  506. _TIFFmemset(td->td_stripoffset_p, 0, td->td_nstrips*sizeof (uint64));
  507. _TIFFmemset(td->td_stripbytecount_p, 0, td->td_nstrips*sizeof (uint64));
  508. TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
  509. TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
  510. return (1);
  511. }
  512. #undef isUnspecified
  513. /*
  514. * Verify file is writable and that the directory
  515. * information is setup properly. In doing the latter
  516. * we also "freeze" the state of the directory so
  517. * that important information is not changed.
  518. */
  519. int
  520. TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
  521. {
  522. if (tif->tif_mode == O_RDONLY) {
  523. TIFFErrorExt(tif->tif_clientdata, module, "File not open for writing");
  524. return (0);
  525. }
  526. if (tiles ^ isTiled(tif)) {
  527. TIFFErrorExt(tif->tif_clientdata, module, tiles ?
  528. "Can not write tiles to a striped image" :
  529. "Can not write scanlines to a tiled image");
  530. return (0);
  531. }
  532. _TIFFFillStriles( tif );
  533. /*
  534. * On the first write verify all the required information
  535. * has been setup and initialize any data structures that
  536. * had to wait until directory information was set.
  537. * Note that a lot of our work is assumed to remain valid
  538. * because we disallow any of the important parameters
  539. * from changing after we start writing (i.e. once
  540. * TIFF_BEENWRITING is set, TIFFSetField will only allow
  541. * the image's length to be changed).
  542. */
  543. if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
  544. TIFFErrorExt(tif->tif_clientdata, module,
  545. "Must set \"ImageWidth\" before writing data");
  546. return (0);
  547. }
  548. if (tif->tif_dir.td_samplesperpixel == 1) {
  549. /*
  550. * Planarconfiguration is irrelevant in case of single band
  551. * images and need not be included. We will set it anyway,
  552. * because this field is used in other parts of library even
  553. * in the single band case.
  554. */
  555. if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG))
  556. tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
  557. } else {
  558. if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
  559. TIFFErrorExt(tif->tif_clientdata, module,
  560. "Must set \"PlanarConfiguration\" before writing data");
  561. return (0);
  562. }
  563. }
  564. if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif)) {
  565. tif->tif_dir.td_nstrips = 0;
  566. TIFFErrorExt(tif->tif_clientdata, module, "No space for %s arrays",
  567. isTiled(tif) ? "tile" : "strip");
  568. return (0);
  569. }
  570. if (isTiled(tif))
  571. {
  572. tif->tif_tilesize = TIFFTileSize(tif);
  573. if (tif->tif_tilesize == 0)
  574. return (0);
  575. }
  576. else
  577. tif->tif_tilesize = (tmsize_t)(-1);
  578. tif->tif_scanlinesize = TIFFScanlineSize(tif);
  579. if (tif->tif_scanlinesize == 0)
  580. return (0);
  581. tif->tif_flags |= TIFF_BEENWRITING;
  582. if( tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
  583. tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
  584. tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
  585. tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
  586. tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
  587. tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
  588. tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
  589. tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0 &&
  590. !(tif->tif_flags & TIFF_DIRTYDIRECT) )
  591. {
  592. TIFFForceStrileArrayWriting(tif);
  593. }
  594. return (1);
  595. }
  596. /*
  597. * Setup the raw data buffer used for encoding.
  598. */
  599. int
  600. TIFFWriteBufferSetup(TIFF* tif, void* bp, tmsize_t size)
  601. {
  602. static const char module[] = "TIFFWriteBufferSetup";
  603. if (tif->tif_rawdata) {
  604. if (tif->tif_flags & TIFF_MYBUFFER) {
  605. _TIFFfree(tif->tif_rawdata);
  606. tif->tif_flags &= ~TIFF_MYBUFFER;
  607. }
  608. tif->tif_rawdata = NULL;
  609. }
  610. if (size == (tmsize_t)(-1)) {
  611. size = (isTiled(tif) ?
  612. tif->tif_tilesize : TIFFStripSize(tif));
  613. /* Adds 10% margin for cases where compression would expand a bit */
  614. if( size < TIFF_TMSIZE_T_MAX - size / 10 )
  615. size += size / 10;
  616. /*
  617. * Make raw data buffer at least 8K
  618. */
  619. if (size < 8*1024)
  620. size = 8*1024;
  621. bp = NULL; /* NB: force malloc */
  622. }
  623. if (bp == NULL) {
  624. bp = _TIFFmalloc(size);
  625. if (bp == NULL) {
  626. TIFFErrorExt(tif->tif_clientdata, module, "No space for output buffer");
  627. return (0);
  628. }
  629. tif->tif_flags |= TIFF_MYBUFFER;
  630. } else
  631. tif->tif_flags &= ~TIFF_MYBUFFER;
  632. tif->tif_rawdata = (uint8*) bp;
  633. tif->tif_rawdatasize = size;
  634. tif->tif_rawcc = 0;
  635. tif->tif_rawcp = tif->tif_rawdata;
  636. tif->tif_flags |= TIFF_BUFFERSETUP;
  637. return (1);
  638. }
  639. /*
  640. * Grow the strip data structures by delta strips.
  641. */
  642. static int
  643. TIFFGrowStrips(TIFF* tif, uint32 delta, const char* module)
  644. {
  645. TIFFDirectory *td = &tif->tif_dir;
  646. uint64* new_stripoffset;
  647. uint64* new_stripbytecount;
  648. assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
  649. new_stripoffset = (uint64*)_TIFFrealloc(td->td_stripoffset_p,
  650. (td->td_nstrips + delta) * sizeof (uint64));
  651. new_stripbytecount = (uint64*)_TIFFrealloc(td->td_stripbytecount_p,
  652. (td->td_nstrips + delta) * sizeof (uint64));
  653. if (new_stripoffset == NULL || new_stripbytecount == NULL) {
  654. if (new_stripoffset)
  655. _TIFFfree(new_stripoffset);
  656. if (new_stripbytecount)
  657. _TIFFfree(new_stripbytecount);
  658. td->td_nstrips = 0;
  659. TIFFErrorExt(tif->tif_clientdata, module, "No space to expand strip arrays");
  660. return (0);
  661. }
  662. td->td_stripoffset_p = new_stripoffset;
  663. td->td_stripbytecount_p = new_stripbytecount;
  664. _TIFFmemset(td->td_stripoffset_p + td->td_nstrips,
  665. 0, delta*sizeof (uint64));
  666. _TIFFmemset(td->td_stripbytecount_p + td->td_nstrips,
  667. 0, delta*sizeof (uint64));
  668. td->td_nstrips += delta;
  669. tif->tif_flags |= TIFF_DIRTYDIRECT;
  670. return (1);
  671. }
  672. /*
  673. * Append the data to the specified strip.
  674. */
  675. static int
  676. TIFFAppendToStrip(TIFF* tif, uint32 strip, uint8* data, tmsize_t cc)
  677. {
  678. static const char module[] = "TIFFAppendToStrip";
  679. TIFFDirectory *td = &tif->tif_dir;
  680. uint64 m;
  681. int64 old_byte_count = -1;
  682. if (td->td_stripoffset_p[strip] == 0 || tif->tif_curoff == 0) {
  683. assert(td->td_nstrips > 0);
  684. if( td->td_stripbytecount_p[strip] != 0
  685. && td->td_stripoffset_p[strip] != 0
  686. && td->td_stripbytecount_p[strip] >= (uint64) cc )
  687. {
  688. /*
  689. * There is already tile data on disk, and the new tile
  690. * data we have will fit in the same space. The only
  691. * aspect of this that is risky is that there could be
  692. * more data to append to this strip before we are done
  693. * depending on how we are getting called.
  694. */
  695. if (!SeekOK(tif, td->td_stripoffset_p[strip])) {
  696. TIFFErrorExt(tif->tif_clientdata, module,
  697. "Seek error at scanline %lu",
  698. (unsigned long)tif->tif_row);
  699. return (0);
  700. }
  701. }
  702. else
  703. {
  704. /*
  705. * Seek to end of file, and set that as our location to
  706. * write this strip.
  707. */
  708. td->td_stripoffset_p[strip] = TIFFSeekFile(tif, 0, SEEK_END);
  709. tif->tif_flags |= TIFF_DIRTYSTRIP;
  710. }
  711. tif->tif_curoff = td->td_stripoffset_p[strip];
  712. /*
  713. * We are starting a fresh strip/tile, so set the size to zero.
  714. */
  715. old_byte_count = td->td_stripbytecount_p[strip];
  716. td->td_stripbytecount_p[strip] = 0;
  717. }
  718. m = tif->tif_curoff+cc;
  719. if (!(tif->tif_flags&TIFF_BIGTIFF))
  720. m = (uint32)m;
  721. if ((m<tif->tif_curoff)||(m<(uint64)cc))
  722. {
  723. TIFFErrorExt(tif->tif_clientdata, module, "Maximum TIFF file size exceeded");
  724. return (0);
  725. }
  726. if (!WriteOK(tif, data, cc)) {
  727. TIFFErrorExt(tif->tif_clientdata, module, "Write error at scanline %lu",
  728. (unsigned long) tif->tif_row);
  729. return (0);
  730. }
  731. tif->tif_curoff = m;
  732. td->td_stripbytecount_p[strip] += cc;
  733. if( (int64) td->td_stripbytecount_p[strip] != old_byte_count )
  734. tif->tif_flags |= TIFF_DIRTYSTRIP;
  735. return (1);
  736. }
  737. /*
  738. * Internal version of TIFFFlushData that can be
  739. * called by ``encodestrip routines'' w/o concern
  740. * for infinite recursion.
  741. */
  742. int
  743. TIFFFlushData1(TIFF* tif)
  744. {
  745. if (tif->tif_rawcc > 0 && tif->tif_flags & TIFF_BUF4WRITE ) {
  746. if (!isFillOrder(tif, tif->tif_dir.td_fillorder) &&
  747. (tif->tif_flags & TIFF_NOBITREV) == 0)
  748. TIFFReverseBits((uint8*)tif->tif_rawdata,
  749. tif->tif_rawcc);
  750. if (!TIFFAppendToStrip(tif,
  751. isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip,
  752. tif->tif_rawdata, tif->tif_rawcc))
  753. {
  754. /* We update those variables even in case of error since there's */
  755. /* code that doesn't really check the return code of this */
  756. /* function */
  757. tif->tif_rawcc = 0;
  758. tif->tif_rawcp = tif->tif_rawdata;
  759. return (0);
  760. }
  761. tif->tif_rawcc = 0;
  762. tif->tif_rawcp = tif->tif_rawdata;
  763. }
  764. return (1);
  765. }
  766. /*
  767. * Set the current write offset. This should only be
  768. * used to set the offset to a known previous location
  769. * (very carefully), or to 0 so that the next write gets
  770. * appended to the end of the file.
  771. */
  772. void
  773. TIFFSetWriteOffset(TIFF* tif, toff_t off)
  774. {
  775. tif->tif_curoff = off;
  776. }
  777. /* vim: set ts=8 sts=8 sw=8 noet: */
  778. /*
  779. * Local Variables:
  780. * mode: c
  781. * c-basic-offset: 8
  782. * fill-column: 78
  783. * End:
  784. */