tif_write.c 26 KB

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