tif_dirwrite.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739
  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. * Directory Write Support Routines.
  28. */
  29. #include "tiffiop.h"
  30. #include <float.h> /*--: for Rational2Double */
  31. #include <math.h> /*--: for Rational2Double */
  32. #ifdef HAVE_IEEEFP
  33. #define TIFFCvtNativeToIEEEFloat(tif, n, fp)
  34. #define TIFFCvtNativeToIEEEDouble(tif, n, dp)
  35. #else
  36. extern void TIFFCvtNativeToIEEEFloat(TIFF* tif, uint32_t n, float* fp);
  37. extern void TIFFCvtNativeToIEEEDouble(TIFF* tif, uint32_t n, double* dp);
  38. #endif
  39. static int TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64_t* pdiroff);
  40. static int TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value);
  41. #if 0
  42. static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value);
  43. #endif
  44. static int TIFFWriteDirectoryTagAscii(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, char* value);
  45. static int TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint8_t* value);
  46. #ifdef notdef
  47. static int TIFFWriteDirectoryTagByte(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint8_t value);
  48. #endif
  49. static int TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint8_t* value);
  50. #if 0
  51. static int TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint8_t value);
  52. #endif
  53. #ifdef notdef
  54. static int TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int8_t value);
  55. #endif
  56. static int TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int8_t* value);
  57. #if 0
  58. static int TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int8_t value);
  59. #endif
  60. static int TIFFWriteDirectoryTagShort(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint16_t value);
  61. static int TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint16_t* value);
  62. static int TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint16_t value);
  63. #ifdef notdef
  64. static int TIFFWriteDirectoryTagSshort(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int16_t value);
  65. #endif
  66. static int TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int16_t* value);
  67. #if 0
  68. static int TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int16_t value);
  69. #endif
  70. static int TIFFWriteDirectoryTagLong(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t value);
  71. static int TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint32_t* value);
  72. #if 0
  73. static int TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t value);
  74. #endif
  75. #ifdef notdef
  76. static int TIFFWriteDirectoryTagSlong(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int32_t value);
  77. #endif
  78. static int TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int32_t* value);
  79. #if 0
  80. static int TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int32_t value);
  81. #endif
  82. #ifdef notdef
  83. static int TIFFWriteDirectoryTagLong8(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint64_t value);
  84. #endif
  85. static int TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value);
  86. #ifdef notdef
  87. static int TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int64_t value);
  88. #endif
  89. static int TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int64_t* value);
  90. static int TIFFWriteDirectoryTagRational(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value);
  91. static int TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value);
  92. static int TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value);
  93. #ifdef notdef
  94. static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, float value);
  95. #endif
  96. static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value);
  97. #if 0
  98. static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, float value);
  99. #endif
  100. #ifdef notdef
  101. static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value);
  102. #endif
  103. static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value);
  104. #if 0
  105. static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value);
  106. #endif
  107. static int TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint32_t* value);
  108. #ifdef notdef
  109. static int TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value);
  110. #endif
  111. static int TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t value);
  112. static int TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value);
  113. static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value);
  114. #ifdef notdef
  115. static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value);
  116. #endif
  117. static int TIFFWriteDirectoryTagColormap(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir);
  118. static int TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir);
  119. static int TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir);
  120. static int TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, char* value);
  121. static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint8_t* value);
  122. #ifdef notdef
  123. static int TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint8_t value);
  124. #endif
  125. static int TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint8_t* value);
  126. #ifdef notdef
  127. static int TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int8_t value);
  128. #endif
  129. static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int8_t* value);
  130. static int TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint16_t value);
  131. static int TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint16_t* value);
  132. #ifdef notdef
  133. static int TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int16_t value);
  134. #endif
  135. static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int16_t* value);
  136. static int TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t value);
  137. static int TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint32_t* value);
  138. #ifdef notdef
  139. static int TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int32_t value);
  140. #endif
  141. static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int32_t* value);
  142. #ifdef notdef
  143. static int TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint64_t value);
  144. #endif
  145. static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value);
  146. #ifdef notdef
  147. static int TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int64_t value);
  148. #endif
  149. static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int64_t* value);
  150. static int TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value);
  151. static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value);
  152. static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value);
  153. /*--: Rational2Double: New functions to support true double-precision for custom rational tag types. */
  154. static int TIFFWriteDirectoryTagRationalDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value);
  155. static int TIFFWriteDirectoryTagSrationalDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value);
  156. static int TIFFWriteDirectoryTagCheckedRationalDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value);
  157. static int TIFFWriteDirectoryTagCheckedSrationalDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value);
  158. static void DoubleToRational(double value, uint32_t *num, uint32_t *denom);
  159. static void DoubleToSrational(double value, int32_t *num, int32_t *denom);
  160. #if 0
  161. static void DoubleToRational_direct(double value, unsigned long *num, unsigned long *denom);
  162. static void DoubleToSrational_direct(double value, long *num, long *denom);
  163. #endif
  164. #ifdef notdef
  165. static int TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, float value);
  166. #endif
  167. static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value);
  168. #ifdef notdef
  169. static int TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value);
  170. #endif
  171. static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value);
  172. static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint32_t* value);
  173. static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value);
  174. static int TIFFWriteDirectoryTagData(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint16_t datatype, uint32_t count, uint32_t datalength, void* data);
  175. static int TIFFLinkDirectory(TIFF*);
  176. /*
  177. * Write the contents of the current directory
  178. * to the specified file. This routine doesn't
  179. * handle overwriting a directory with auxiliary
  180. * storage that's been changed.
  181. */
  182. int
  183. TIFFWriteDirectory(TIFF* tif)
  184. {
  185. return TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);
  186. }
  187. /*
  188. * This is an advanced writing function that must be used in a particular
  189. * sequence, and generally together with TIFFForceStrileArrayWriting(),
  190. * to make its intended effect. Its aim is to modify the location
  191. * where the [Strip/Tile][Offsets/ByteCounts] arrays are located in the file.
  192. * More precisely, when TIFFWriteCheck() will be called, the tag entries for
  193. * those arrays will be written with type = count = offset = 0 as a temporary
  194. * value.
  195. *
  196. * Its effect is only valid for the current directory, and before
  197. * TIFFWriteDirectory() is first called, and will be reset when
  198. * changing directory.
  199. *
  200. * The typical sequence of calls is:
  201. * TIFFOpen()
  202. * [ TIFFCreateDirectory(tif) ]
  203. * Set fields with calls to TIFFSetField(tif, ...)
  204. * TIFFDeferStrileArrayWriting(tif)
  205. * TIFFWriteCheck(tif, ...)
  206. * TIFFWriteDirectory(tif)
  207. * ... potentially create other directories and come back to the above directory
  208. * TIFFForceStrileArrayWriting(tif): emit the arrays at the end of file
  209. *
  210. * Returns 1 in case of success, 0 otherwise.
  211. */
  212. int TIFFDeferStrileArrayWriting(TIFF* tif)
  213. {
  214. static const char module[] = "TIFFDeferStrileArrayWriting";
  215. if (tif->tif_mode == O_RDONLY)
  216. {
  217. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  218. "File opened in read-only mode");
  219. return 0;
  220. }
  221. if( tif->tif_diroff != 0 )
  222. {
  223. TIFFErrorExt(tif->tif_clientdata, module,
  224. "Directory has already been written");
  225. return 0;
  226. }
  227. tif->tif_dir.td_deferstrilearraywriting = TRUE;
  228. return 1;
  229. }
  230. /*
  231. * Similar to TIFFWriteDirectory(), writes the directory out
  232. * but leaves all data structures in memory so that it can be
  233. * written again. This will make a partially written TIFF file
  234. * readable before it is successfully completed/closed.
  235. */
  236. int
  237. TIFFCheckpointDirectory(TIFF* tif)
  238. {
  239. int rc;
  240. /* Setup the strips arrays, if they haven't already been. */
  241. if (tif->tif_dir.td_stripoffset_p == NULL)
  242. (void) TIFFSetupStrips(tif);
  243. rc = TIFFWriteDirectorySec(tif,TRUE,FALSE,NULL);
  244. (void) TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END));
  245. return rc;
  246. }
  247. int
  248. TIFFWriteCustomDirectory(TIFF* tif, uint64_t* pdiroff)
  249. {
  250. return TIFFWriteDirectorySec(tif,FALSE,FALSE,pdiroff);
  251. }
  252. /*
  253. * Similar to TIFFWriteDirectory(), but if the directory has already
  254. * been written once, it is relocated to the end of the file, in case it
  255. * has changed in size. Note that this will result in the loss of the
  256. * previously used directory space.
  257. */
  258. int
  259. TIFFRewriteDirectory( TIFF *tif )
  260. {
  261. static const char module[] = "TIFFRewriteDirectory";
  262. /* We don't need to do anything special if it hasn't been written. */
  263. if( tif->tif_diroff == 0 )
  264. return TIFFWriteDirectory( tif );
  265. /*
  266. * Find and zero the pointer to this directory, so that TIFFLinkDirectory
  267. * will cause it to be added after this directories current pre-link.
  268. */
  269. if (!(tif->tif_flags&TIFF_BIGTIFF))
  270. {
  271. if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)
  272. {
  273. tif->tif_header.classic.tiff_diroff = 0;
  274. tif->tif_diroff = 0;
  275. TIFFSeekFile(tif,4,SEEK_SET);
  276. if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff),4))
  277. {
  278. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  279. "Error updating TIFF header");
  280. return (0);
  281. }
  282. }
  283. else
  284. {
  285. uint32_t nextdir;
  286. nextdir = tif->tif_header.classic.tiff_diroff;
  287. while(1) {
  288. uint16_t dircount;
  289. uint32_t nextnextdir;
  290. if (!SeekOK(tif, nextdir) ||
  291. !ReadOK(tif, &dircount, 2)) {
  292. TIFFErrorExt(tif->tif_clientdata, module,
  293. "Error fetching directory count");
  294. return (0);
  295. }
  296. if (tif->tif_flags & TIFF_SWAB)
  297. TIFFSwabShort(&dircount);
  298. (void) TIFFSeekFile(tif,
  299. nextdir+2+dircount*12, SEEK_SET);
  300. if (!ReadOK(tif, &nextnextdir, 4)) {
  301. TIFFErrorExt(tif->tif_clientdata, module,
  302. "Error fetching directory link");
  303. return (0);
  304. }
  305. if (tif->tif_flags & TIFF_SWAB)
  306. TIFFSwabLong(&nextnextdir);
  307. if (nextnextdir==tif->tif_diroff)
  308. {
  309. uint32_t m;
  310. m=0;
  311. (void) TIFFSeekFile(tif,
  312. nextdir+2+dircount*12, SEEK_SET);
  313. if (!WriteOK(tif, &m, 4)) {
  314. TIFFErrorExt(tif->tif_clientdata, module,
  315. "Error writing directory link");
  316. return (0);
  317. }
  318. tif->tif_diroff=0;
  319. break;
  320. }
  321. nextdir=nextnextdir;
  322. }
  323. }
  324. }
  325. else
  326. {
  327. if (tif->tif_header.big.tiff_diroff == tif->tif_diroff)
  328. {
  329. tif->tif_header.big.tiff_diroff = 0;
  330. tif->tif_diroff = 0;
  331. TIFFSeekFile(tif,8,SEEK_SET);
  332. if (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff),8))
  333. {
  334. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  335. "Error updating TIFF header");
  336. return (0);
  337. }
  338. }
  339. else
  340. {
  341. uint64_t nextdir;
  342. nextdir = tif->tif_header.big.tiff_diroff;
  343. while(1) {
  344. uint64_t dircount64;
  345. uint16_t dircount;
  346. uint64_t nextnextdir;
  347. if (!SeekOK(tif, nextdir) ||
  348. !ReadOK(tif, &dircount64, 8)) {
  349. TIFFErrorExt(tif->tif_clientdata, module,
  350. "Error fetching directory count");
  351. return (0);
  352. }
  353. if (tif->tif_flags & TIFF_SWAB)
  354. TIFFSwabLong8(&dircount64);
  355. if (dircount64>0xFFFF)
  356. {
  357. TIFFErrorExt(tif->tif_clientdata, module,
  358. "Sanity check on tag count failed, likely corrupt TIFF");
  359. return (0);
  360. }
  361. dircount=(uint16_t)dircount64;
  362. (void) TIFFSeekFile(tif,
  363. nextdir+8+dircount*20, SEEK_SET);
  364. if (!ReadOK(tif, &nextnextdir, 8)) {
  365. TIFFErrorExt(tif->tif_clientdata, module,
  366. "Error fetching directory link");
  367. return (0);
  368. }
  369. if (tif->tif_flags & TIFF_SWAB)
  370. TIFFSwabLong8(&nextnextdir);
  371. if (nextnextdir==tif->tif_diroff)
  372. {
  373. uint64_t m;
  374. m=0;
  375. (void) TIFFSeekFile(tif,
  376. nextdir+8+dircount*20, SEEK_SET);
  377. if (!WriteOK(tif, &m, 8)) {
  378. TIFFErrorExt(tif->tif_clientdata, module,
  379. "Error writing directory link");
  380. return (0);
  381. }
  382. tif->tif_diroff=0;
  383. break;
  384. }
  385. nextdir=nextnextdir;
  386. }
  387. }
  388. }
  389. /*
  390. * Now use TIFFWriteDirectory() normally.
  391. */
  392. return TIFFWriteDirectory( tif );
  393. }
  394. static int
  395. TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64_t* pdiroff)
  396. {
  397. static const char module[] = "TIFFWriteDirectorySec";
  398. uint32_t ndir;
  399. TIFFDirEntry* dir;
  400. uint32_t dirsize;
  401. void* dirmem;
  402. uint32_t m;
  403. if (tif->tif_mode == O_RDONLY)
  404. return (1);
  405. _TIFFFillStriles( tif );
  406. /*
  407. * Clear write state so that subsequent images with
  408. * different characteristics get the right buffers
  409. * setup for them.
  410. */
  411. if (imagedone)
  412. {
  413. if (tif->tif_flags & TIFF_POSTENCODE)
  414. {
  415. tif->tif_flags &= ~TIFF_POSTENCODE;
  416. if (!(*tif->tif_postencode)(tif))
  417. {
  418. TIFFErrorExt(tif->tif_clientdata,module,
  419. "Error post-encoding before directory write");
  420. return (0);
  421. }
  422. }
  423. (*tif->tif_close)(tif); /* shutdown encoder */
  424. /*
  425. * Flush any data that might have been written
  426. * by the compression close+cleanup routines. But
  427. * be careful not to write stuff if we didn't add data
  428. * in the previous steps as the "rawcc" data may well be
  429. * a previously read tile/strip in mixed read/write mode.
  430. */
  431. if (tif->tif_rawcc > 0
  432. && (tif->tif_flags & TIFF_BEENWRITING) != 0 )
  433. {
  434. if( !TIFFFlushData1(tif) )
  435. {
  436. TIFFErrorExt(tif->tif_clientdata, module,
  437. "Error flushing data before directory write");
  438. return (0);
  439. }
  440. }
  441. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  442. {
  443. _TIFFfree(tif->tif_rawdata);
  444. tif->tif_rawdata = NULL;
  445. tif->tif_rawcc = 0;
  446. tif->tif_rawdatasize = 0;
  447. tif->tif_rawdataoff = 0;
  448. tif->tif_rawdataloaded = 0;
  449. }
  450. tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
  451. }
  452. dir=NULL;
  453. dirmem=NULL;
  454. dirsize=0;
  455. while (1)
  456. {
  457. ndir=0;
  458. if (isimage)
  459. {
  460. if (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
  461. {
  462. if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))
  463. goto bad;
  464. if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))
  465. goto bad;
  466. }
  467. if (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))
  468. {
  469. if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))
  470. goto bad;
  471. if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))
  472. goto bad;
  473. }
  474. if (TIFFFieldSet(tif,FIELD_RESOLUTION))
  475. {
  476. if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))
  477. goto bad;
  478. if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))
  479. goto bad;
  480. }
  481. if (TIFFFieldSet(tif,FIELD_POSITION))
  482. {
  483. if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))
  484. goto bad;
  485. if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))
  486. goto bad;
  487. }
  488. if (TIFFFieldSet(tif,FIELD_SUBFILETYPE))
  489. {
  490. if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))
  491. goto bad;
  492. }
  493. if (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
  494. {
  495. if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))
  496. goto bad;
  497. }
  498. if (TIFFFieldSet(tif,FIELD_COMPRESSION))
  499. {
  500. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))
  501. goto bad;
  502. }
  503. if (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
  504. {
  505. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))
  506. goto bad;
  507. }
  508. if (TIFFFieldSet(tif,FIELD_THRESHHOLDING))
  509. {
  510. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))
  511. goto bad;
  512. }
  513. if (TIFFFieldSet(tif,FIELD_FILLORDER))
  514. {
  515. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))
  516. goto bad;
  517. }
  518. if (TIFFFieldSet(tif,FIELD_ORIENTATION))
  519. {
  520. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))
  521. goto bad;
  522. }
  523. if (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
  524. {
  525. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))
  526. goto bad;
  527. }
  528. if (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))
  529. {
  530. if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))
  531. goto bad;
  532. }
  533. if (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))
  534. {
  535. if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))
  536. goto bad;
  537. }
  538. if (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))
  539. {
  540. if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))
  541. goto bad;
  542. }
  543. if (TIFFFieldSet(tif,FIELD_PLANARCONFIG))
  544. {
  545. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))
  546. goto bad;
  547. }
  548. if (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))
  549. {
  550. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))
  551. goto bad;
  552. }
  553. if (TIFFFieldSet(tif,FIELD_PAGENUMBER))
  554. {
  555. if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))
  556. goto bad;
  557. }
  558. if (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))
  559. {
  560. if (!isTiled(tif))
  561. {
  562. if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount_p))
  563. goto bad;
  564. }
  565. else
  566. {
  567. if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount_p))
  568. goto bad;
  569. }
  570. }
  571. if (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))
  572. {
  573. if (!isTiled(tif))
  574. {
  575. /* td_stripoffset_p might be NULL in an odd OJPEG case. See
  576. * tif_dirread.c around line 3634.
  577. * XXX: OJPEG hack.
  578. * If a) compression is OJPEG, b) it's not a tiled TIFF,
  579. * and c) the number of strips is 1,
  580. * then we tolerate the absence of stripoffsets tag,
  581. * because, presumably, all required data is in the
  582. * JpegInterchangeFormat stream.
  583. * We can get here when using tiffset on such a file.
  584. * See http://bugzilla.maptools.org/show_bug.cgi?id=2500
  585. */
  586. if (tif->tif_dir.td_stripoffset_p != NULL &&
  587. !TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset_p))
  588. goto bad;
  589. }
  590. else
  591. {
  592. if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset_p))
  593. goto bad;
  594. }
  595. }
  596. if (TIFFFieldSet(tif,FIELD_COLORMAP))
  597. {
  598. if (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))
  599. goto bad;
  600. }
  601. if (TIFFFieldSet(tif,FIELD_EXTRASAMPLES))
  602. {
  603. if (tif->tif_dir.td_extrasamples)
  604. {
  605. uint16_t na;
  606. uint16_t* nb;
  607. TIFFGetFieldDefaulted(tif,TIFFTAG_EXTRASAMPLES,&na,&nb);
  608. if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_EXTRASAMPLES,na,nb))
  609. goto bad;
  610. }
  611. }
  612. if (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT))
  613. {
  614. if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_SAMPLEFORMAT,tif->tif_dir.td_sampleformat))
  615. goto bad;
  616. }
  617. if (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))
  618. {
  619. if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_sminsamplevalue))
  620. goto bad;
  621. }
  622. if (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))
  623. {
  624. if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_smaxsamplevalue))
  625. goto bad;
  626. }
  627. if (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))
  628. {
  629. if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_IMAGEDEPTH,tif->tif_dir.td_imagedepth))
  630. goto bad;
  631. }
  632. if (TIFFFieldSet(tif,FIELD_TILEDEPTH))
  633. {
  634. if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_TILEDEPTH,tif->tif_dir.td_tiledepth))
  635. goto bad;
  636. }
  637. if (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))
  638. {
  639. if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_HALFTONEHINTS,2,&tif->tif_dir.td_halftonehints[0]))
  640. goto bad;
  641. }
  642. if (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))
  643. {
  644. if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_YCBCRSUBSAMPLING,2,&tif->tif_dir.td_ycbcrsubsampling[0]))
  645. goto bad;
  646. }
  647. if (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING))
  648. {
  649. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_YCBCRPOSITIONING,tif->tif_dir.td_ycbcrpositioning))
  650. goto bad;
  651. }
  652. if (TIFFFieldSet(tif,FIELD_REFBLACKWHITE))
  653. {
  654. if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,TIFFTAG_REFERENCEBLACKWHITE,6,tif->tif_dir.td_refblackwhite))
  655. goto bad;
  656. }
  657. if (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION))
  658. {
  659. if (!TIFFWriteDirectoryTagTransferfunction(tif,&ndir,dir))
  660. goto bad;
  661. }
  662. if (TIFFFieldSet(tif,FIELD_INKNAMES))
  663. {
  664. if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,TIFFTAG_INKNAMES,tif->tif_dir.td_inknameslen,tif->tif_dir.td_inknames))
  665. goto bad;
  666. }
  667. if (TIFFFieldSet(tif,FIELD_SUBIFD))
  668. {
  669. if (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))
  670. goto bad;
  671. }
  672. {
  673. uint32_t n;
  674. for (n=0; n<tif->tif_nfields; n++) {
  675. const TIFFField* o;
  676. o = tif->tif_fields[n];
  677. if ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))
  678. {
  679. switch (o->get_field_type)
  680. {
  681. case TIFF_SETGET_ASCII:
  682. {
  683. uint32_t pa;
  684. char* pb;
  685. assert(o->field_type==TIFF_ASCII);
  686. assert(o->field_readcount==TIFF_VARIABLE);
  687. assert(o->field_passcount==0);
  688. TIFFGetField(tif,o->field_tag,&pb);
  689. pa=(uint32_t)(strlen(pb));
  690. if (!TIFFWriteDirectoryTagAscii(tif, &ndir, dir, (uint16_t)o->field_tag, pa, pb))
  691. goto bad;
  692. }
  693. break;
  694. case TIFF_SETGET_UINT16:
  695. {
  696. uint16_t p;
  697. assert(o->field_type==TIFF_SHORT);
  698. assert(o->field_readcount==1);
  699. assert(o->field_passcount==0);
  700. TIFFGetField(tif,o->field_tag,&p);
  701. if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir, (uint16_t)o->field_tag, p))
  702. goto bad;
  703. }
  704. break;
  705. case TIFF_SETGET_UINT32:
  706. {
  707. uint32_t p;
  708. assert(o->field_type==TIFF_LONG);
  709. assert(o->field_readcount==1);
  710. assert(o->field_passcount==0);
  711. TIFFGetField(tif,o->field_tag,&p);
  712. if (!TIFFWriteDirectoryTagLong(tif, &ndir, dir, (uint16_t)o->field_tag, p))
  713. goto bad;
  714. }
  715. break;
  716. case TIFF_SETGET_C32_UINT8:
  717. {
  718. uint32_t pa;
  719. void* pb;
  720. assert(o->field_type==TIFF_UNDEFINED);
  721. assert(o->field_readcount==TIFF_VARIABLE2);
  722. assert(o->field_passcount==1);
  723. TIFFGetField(tif,o->field_tag,&pa,&pb);
  724. if (!TIFFWriteDirectoryTagUndefinedArray(tif, &ndir, dir, (uint16_t)o->field_tag, pa, pb))
  725. goto bad;
  726. }
  727. break;
  728. default:
  729. TIFFErrorExt(tif->tif_clientdata,module,
  730. "Cannot write tag %"PRIu32" (%s)",
  731. TIFFFieldTag(o),
  732. o->field_name ? o->field_name : "unknown");
  733. goto bad;
  734. }
  735. }
  736. }
  737. }
  738. }
  739. for (m=0; m<(uint32_t)(tif->tif_dir.td_customValueCount); m++)
  740. {
  741. uint16_t tag = (uint16_t)tif->tif_dir.td_customValues[m].info->field_tag;
  742. uint32_t count = tif->tif_dir.td_customValues[m].count;
  743. switch (tif->tif_dir.td_customValues[m].info->field_type)
  744. {
  745. case TIFF_ASCII:
  746. if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  747. goto bad;
  748. break;
  749. case TIFF_UNDEFINED:
  750. if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  751. goto bad;
  752. break;
  753. case TIFF_BYTE:
  754. if (!TIFFWriteDirectoryTagByteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  755. goto bad;
  756. break;
  757. case TIFF_SBYTE:
  758. if (!TIFFWriteDirectoryTagSbyteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  759. goto bad;
  760. break;
  761. case TIFF_SHORT:
  762. if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  763. goto bad;
  764. break;
  765. case TIFF_SSHORT:
  766. if (!TIFFWriteDirectoryTagSshortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  767. goto bad;
  768. break;
  769. case TIFF_LONG:
  770. if (!TIFFWriteDirectoryTagLongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  771. goto bad;
  772. break;
  773. case TIFF_SLONG:
  774. if (!TIFFWriteDirectoryTagSlongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  775. goto bad;
  776. break;
  777. case TIFF_LONG8:
  778. if (!TIFFWriteDirectoryTagLong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  779. goto bad;
  780. break;
  781. case TIFF_SLONG8:
  782. if (!TIFFWriteDirectoryTagSlong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  783. goto bad;
  784. break;
  785. case TIFF_RATIONAL:
  786. {
  787. /*-- Rational2Double: For Rationals evaluate "set_field_type" to determine internal storage size. */
  788. int tv_size;
  789. tv_size = _TIFFSetGetFieldSize(tif->tif_dir.td_customValues[m].info->set_field_type);
  790. if (tv_size == 8) {
  791. if (!TIFFWriteDirectoryTagRationalDoubleArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  792. goto bad;
  793. } else {
  794. /*-- default should be tv_size == 4 */
  795. if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  796. goto bad;
  797. /*-- ToDo: After Testing, this should be removed and tv_size==4 should be set as default. */
  798. if (tv_size != 4) {
  799. TIFFErrorExt(0,"TIFFLib: _TIFFWriteDirectorySec()", "Rational2Double: .set_field_type in not 4 but %d", tv_size);
  800. }
  801. }
  802. }
  803. break;
  804. case TIFF_SRATIONAL:
  805. {
  806. /*-- Rational2Double: For Rationals evaluate "set_field_type" to determine internal storage size. */
  807. int tv_size;
  808. tv_size = _TIFFSetGetFieldSize(tif->tif_dir.td_customValues[m].info->set_field_type);
  809. if (tv_size == 8) {
  810. if (!TIFFWriteDirectoryTagSrationalDoubleArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  811. goto bad;
  812. } else {
  813. /*-- default should be tv_size == 4 */
  814. if (!TIFFWriteDirectoryTagSrationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  815. goto bad;
  816. /*-- ToDo: After Testing, this should be removed and tv_size==4 should be set as default. */
  817. if (tv_size != 4) {
  818. TIFFErrorExt(0,"TIFFLib: _TIFFWriteDirectorySec()", "Rational2Double: .set_field_type in not 4 but %d", tv_size);
  819. }
  820. }
  821. }
  822. break;
  823. case TIFF_FLOAT:
  824. if (!TIFFWriteDirectoryTagFloatArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  825. goto bad;
  826. break;
  827. case TIFF_DOUBLE:
  828. if (!TIFFWriteDirectoryTagDoubleArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  829. goto bad;
  830. break;
  831. case TIFF_IFD:
  832. if (!TIFFWriteDirectoryTagIfdArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  833. goto bad;
  834. break;
  835. case TIFF_IFD8:
  836. if (!TIFFWriteDirectoryTagIfdIfd8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  837. goto bad;
  838. break;
  839. default:
  840. assert(0); /* we should never get here */
  841. break;
  842. }
  843. }
  844. if (dir!=NULL)
  845. break;
  846. dir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));
  847. if (dir==NULL)
  848. {
  849. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  850. goto bad;
  851. }
  852. if (isimage)
  853. {
  854. if ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))
  855. goto bad;
  856. }
  857. else
  858. tif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~((toff_t)1));
  859. if (pdiroff!=NULL)
  860. *pdiroff=tif->tif_diroff;
  861. if (!(tif->tif_flags&TIFF_BIGTIFF))
  862. dirsize=2+ndir*12+4;
  863. else
  864. dirsize=8+ndir*20+8;
  865. tif->tif_dataoff=tif->tif_diroff+dirsize;
  866. if (!(tif->tif_flags&TIFF_BIGTIFF))
  867. tif->tif_dataoff=(uint32_t)tif->tif_dataoff;
  868. if ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64_t)dirsize))
  869. {
  870. TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
  871. goto bad;
  872. }
  873. if (tif->tif_dataoff&1)
  874. tif->tif_dataoff++;
  875. if (isimage)
  876. tif->tif_curdir++;
  877. }
  878. if (isimage)
  879. {
  880. if (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))
  881. {
  882. uint32_t na;
  883. TIFFDirEntry* nb;
  884. for (na=0, nb=dir; ; na++, nb++)
  885. {
  886. if( na == ndir )
  887. {
  888. TIFFErrorExt(tif->tif_clientdata,module,
  889. "Cannot find SubIFD tag");
  890. goto bad;
  891. }
  892. if (nb->tdir_tag==TIFFTAG_SUBIFD)
  893. break;
  894. }
  895. if (!(tif->tif_flags&TIFF_BIGTIFF))
  896. tif->tif_subifdoff=tif->tif_diroff+2+na*12+8;
  897. else
  898. tif->tif_subifdoff=tif->tif_diroff+8+na*20+12;
  899. }
  900. }
  901. dirmem=_TIFFmalloc(dirsize);
  902. if (dirmem==NULL)
  903. {
  904. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  905. goto bad;
  906. }
  907. if (!(tif->tif_flags&TIFF_BIGTIFF))
  908. {
  909. uint8_t* n;
  910. uint32_t nTmp;
  911. TIFFDirEntry* o;
  912. n=dirmem;
  913. *(uint16_t*)n=(uint16_t)ndir;
  914. if (tif->tif_flags&TIFF_SWAB)
  915. TIFFSwabShort((uint16_t*)n);
  916. n+=2;
  917. o=dir;
  918. for (m=0; m<ndir; m++)
  919. {
  920. *(uint16_t*)n=o->tdir_tag;
  921. if (tif->tif_flags&TIFF_SWAB)
  922. TIFFSwabShort((uint16_t*)n);
  923. n+=2;
  924. *(uint16_t*)n=o->tdir_type;
  925. if (tif->tif_flags&TIFF_SWAB)
  926. TIFFSwabShort((uint16_t*)n);
  927. n+=2;
  928. nTmp = (uint32_t)o->tdir_count;
  929. _TIFFmemcpy(n,&nTmp,4);
  930. if (tif->tif_flags&TIFF_SWAB)
  931. TIFFSwabLong((uint32_t*)n);
  932. n+=4;
  933. /* This is correct. The data has been */
  934. /* swabbed previously in TIFFWriteDirectoryTagData */
  935. _TIFFmemcpy(n,&o->tdir_offset,4);
  936. n+=4;
  937. o++;
  938. }
  939. nTmp = (uint32_t)tif->tif_nextdiroff;
  940. if (tif->tif_flags&TIFF_SWAB)
  941. TIFFSwabLong(&nTmp);
  942. _TIFFmemcpy(n,&nTmp,4);
  943. }
  944. else
  945. {
  946. uint8_t* n;
  947. TIFFDirEntry* o;
  948. n=dirmem;
  949. *(uint64_t*)n=ndir;
  950. if (tif->tif_flags&TIFF_SWAB)
  951. TIFFSwabLong8((uint64_t*)n);
  952. n+=8;
  953. o=dir;
  954. for (m=0; m<ndir; m++)
  955. {
  956. *(uint16_t*)n=o->tdir_tag;
  957. if (tif->tif_flags&TIFF_SWAB)
  958. TIFFSwabShort((uint16_t*)n);
  959. n+=2;
  960. *(uint16_t*)n=o->tdir_type;
  961. if (tif->tif_flags&TIFF_SWAB)
  962. TIFFSwabShort((uint16_t*)n);
  963. n+=2;
  964. _TIFFmemcpy(n,&o->tdir_count,8);
  965. if (tif->tif_flags&TIFF_SWAB)
  966. TIFFSwabLong8((uint64_t*)n);
  967. n+=8;
  968. _TIFFmemcpy(n,&o->tdir_offset,8);
  969. n+=8;
  970. o++;
  971. }
  972. _TIFFmemcpy(n,&tif->tif_nextdiroff,8);
  973. if (tif->tif_flags&TIFF_SWAB)
  974. TIFFSwabLong8((uint64_t*)n);
  975. }
  976. _TIFFfree(dir);
  977. dir=NULL;
  978. if (!SeekOK(tif,tif->tif_diroff))
  979. {
  980. TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
  981. goto bad;
  982. }
  983. if (!WriteOK(tif,dirmem,(tmsize_t)dirsize))
  984. {
  985. TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
  986. goto bad;
  987. }
  988. _TIFFfree(dirmem);
  989. if (imagedone)
  990. {
  991. TIFFFreeDirectory(tif);
  992. tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  993. tif->tif_flags &= ~TIFF_DIRTYSTRIP;
  994. (*tif->tif_cleanup)(tif);
  995. /*
  996. * Reset directory-related state for subsequent
  997. * directories.
  998. */
  999. TIFFCreateDirectory(tif);
  1000. }
  1001. return(1);
  1002. bad:
  1003. if (dir!=NULL)
  1004. _TIFFfree(dir);
  1005. if (dirmem!=NULL)
  1006. _TIFFfree(dirmem);
  1007. return(0);
  1008. }
  1009. static int8_t TIFFClampDoubleToInt8(double val )
  1010. {
  1011. if( val > 127 )
  1012. return 127;
  1013. if( val < -128 || val != val )
  1014. return -128;
  1015. return (int8_t)val;
  1016. }
  1017. static int16_t TIFFClampDoubleToInt16(double val )
  1018. {
  1019. if( val > 32767 )
  1020. return 32767;
  1021. if( val < -32768 || val != val )
  1022. return -32768;
  1023. return (int16_t)val;
  1024. }
  1025. static int32_t TIFFClampDoubleToInt32(double val )
  1026. {
  1027. if( val > 0x7FFFFFFF )
  1028. return 0x7FFFFFFF;
  1029. if( val < -0x7FFFFFFF-1 || val != val )
  1030. return -0x7FFFFFFF-1;
  1031. return (int32_t)val;
  1032. }
  1033. static uint8_t TIFFClampDoubleToUInt8(double val )
  1034. {
  1035. if( val < 0 )
  1036. return 0;
  1037. if( val > 255 || val != val )
  1038. return 255;
  1039. return (uint8_t)val;
  1040. }
  1041. static uint16_t TIFFClampDoubleToUInt16(double val )
  1042. {
  1043. if( val < 0 )
  1044. return 0;
  1045. if( val > 65535 || val != val )
  1046. return 65535;
  1047. return (uint16_t)val;
  1048. }
  1049. static uint32_t TIFFClampDoubleToUInt32(double val )
  1050. {
  1051. if( val < 0 )
  1052. return 0;
  1053. if( val > 0xFFFFFFFFU || val != val )
  1054. return 0xFFFFFFFFU;
  1055. return (uint32_t)val;
  1056. }
  1057. static int
  1058. TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value)
  1059. {
  1060. static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
  1061. void* conv;
  1062. uint32_t i;
  1063. int ok;
  1064. conv = _TIFFmalloc(count*sizeof(double));
  1065. if (conv == NULL)
  1066. {
  1067. TIFFErrorExt(tif->tif_clientdata, module, "Out of memory");
  1068. return (0);
  1069. }
  1070. switch (tif->tif_dir.td_sampleformat)
  1071. {
  1072. case SAMPLEFORMAT_IEEEFP:
  1073. if (tif->tif_dir.td_bitspersample<=32)
  1074. {
  1075. for (i = 0; i < count; ++i)
  1076. ((float*)conv)[i] = _TIFFClampDoubleToFloat(value[i]);
  1077. ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);
  1078. }
  1079. else
  1080. {
  1081. ok = TIFFWriteDirectoryTagDoubleArray(tif,ndir,dir,tag,count,value);
  1082. }
  1083. break;
  1084. case SAMPLEFORMAT_INT:
  1085. if (tif->tif_dir.td_bitspersample<=8)
  1086. {
  1087. for (i = 0; i < count; ++i)
  1088. ((int8_t*)conv)[i] = TIFFClampDoubleToInt8(value[i]);
  1089. ok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8_t*)conv);
  1090. }
  1091. else if (tif->tif_dir.td_bitspersample<=16)
  1092. {
  1093. for (i = 0; i < count; ++i)
  1094. ((int16_t*)conv)[i] = TIFFClampDoubleToInt16(value[i]);
  1095. ok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16_t*)conv);
  1096. }
  1097. else
  1098. {
  1099. for (i = 0; i < count; ++i)
  1100. ((int32_t*)conv)[i] = TIFFClampDoubleToInt32(value[i]);
  1101. ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32_t*)conv);
  1102. }
  1103. break;
  1104. case SAMPLEFORMAT_UINT:
  1105. if (tif->tif_dir.td_bitspersample<=8)
  1106. {
  1107. for (i = 0; i < count; ++i)
  1108. ((uint8_t*)conv)[i] = TIFFClampDoubleToUInt8(value[i]);
  1109. ok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8_t*)conv);
  1110. }
  1111. else if (tif->tif_dir.td_bitspersample<=16)
  1112. {
  1113. for (i = 0; i < count; ++i)
  1114. ((uint16_t*)conv)[i] = TIFFClampDoubleToUInt16(value[i]);
  1115. ok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16_t*)conv);
  1116. }
  1117. else
  1118. {
  1119. for (i = 0; i < count; ++i)
  1120. ((uint32_t*)conv)[i] = TIFFClampDoubleToUInt32(value[i]);
  1121. ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32_t*)conv);
  1122. }
  1123. break;
  1124. default:
  1125. ok = 0;
  1126. }
  1127. _TIFFfree(conv);
  1128. return (ok);
  1129. }
  1130. #if 0
  1131. static int
  1132. TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value)
  1133. {
  1134. switch (tif->tif_dir.td_sampleformat)
  1135. {
  1136. case SAMPLEFORMAT_IEEEFP:
  1137. if (tif->tif_dir.td_bitspersample<=32)
  1138. return(TIFFWriteDirectoryTagFloatPerSample(tif,ndir,dir,tag,(float)value));
  1139. else
  1140. return(TIFFWriteDirectoryTagDoublePerSample(tif,ndir,dir,tag,value));
  1141. case SAMPLEFORMAT_INT:
  1142. if (tif->tif_dir.td_bitspersample<=8)
  1143. return(TIFFWriteDirectoryTagSbytePerSample(tif,ndir,dir,tag,(int8_t)value));
  1144. else if (tif->tif_dir.td_bitspersample<=16)
  1145. return(TIFFWriteDirectoryTagSshortPerSample(tif,ndir,dir,tag,(int16_t)value));
  1146. else
  1147. return(TIFFWriteDirectoryTagSlongPerSample(tif,ndir,dir,tag,(int32_t)value));
  1148. case SAMPLEFORMAT_UINT:
  1149. if (tif->tif_dir.td_bitspersample<=8)
  1150. return(TIFFWriteDirectoryTagBytePerSample(tif,ndir,dir,tag,(uint8_t)value));
  1151. else if (tif->tif_dir.td_bitspersample<=16)
  1152. return(TIFFWriteDirectoryTagShortPerSample(tif,ndir,dir,tag,(uint16_t)value));
  1153. else
  1154. return(TIFFWriteDirectoryTagLongPerSample(tif,ndir,dir,tag,(uint32_t)value));
  1155. default:
  1156. return(1);
  1157. }
  1158. }
  1159. #endif
  1160. static int
  1161. TIFFWriteDirectoryTagAscii(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, char* value)
  1162. {
  1163. if (dir==NULL)
  1164. {
  1165. (*ndir)++;
  1166. return(1);
  1167. }
  1168. return(TIFFWriteDirectoryTagCheckedAscii(tif,ndir,dir,tag,count,value));
  1169. }
  1170. static int
  1171. TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint8_t* value)
  1172. {
  1173. if (dir==NULL)
  1174. {
  1175. (*ndir)++;
  1176. return(1);
  1177. }
  1178. return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif,ndir,dir,tag,count,value));
  1179. }
  1180. #ifdef notdef
  1181. static int
  1182. TIFFWriteDirectoryTagByte(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint8_t value)
  1183. {
  1184. if (dir==NULL)
  1185. {
  1186. (*ndir)++;
  1187. return(1);
  1188. }
  1189. return(TIFFWriteDirectoryTagCheckedByte(tif,ndir,dir,tag,value));
  1190. }
  1191. #endif
  1192. static int
  1193. TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint8_t* value)
  1194. {
  1195. if (dir==NULL)
  1196. {
  1197. (*ndir)++;
  1198. return(1);
  1199. }
  1200. return(TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,count,value));
  1201. }
  1202. #if 0
  1203. static int
  1204. TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint8_t value)
  1205. {
  1206. static const char module[] = "TIFFWriteDirectoryTagBytePerSample";
  1207. uint8_t* m;
  1208. uint8_t* na;
  1209. uint16_t nb;
  1210. int o;
  1211. if (dir==NULL)
  1212. {
  1213. (*ndir)++;
  1214. return(1);
  1215. }
  1216. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint8_t));
  1217. if (m==NULL)
  1218. {
  1219. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1220. return(0);
  1221. }
  1222. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1223. *na=value;
  1224. o=TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1225. _TIFFfree(m);
  1226. return(o);
  1227. }
  1228. #endif
  1229. #ifdef notdef
  1230. static int
  1231. TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int8_t value)
  1232. {
  1233. if (dir==NULL)
  1234. {
  1235. (*ndir)++;
  1236. return(1);
  1237. }
  1238. return(TIFFWriteDirectoryTagCheckedSbyte(tif,ndir,dir,tag,value));
  1239. }
  1240. #endif
  1241. static int
  1242. TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int8_t* value)
  1243. {
  1244. if (dir==NULL)
  1245. {
  1246. (*ndir)++;
  1247. return(1);
  1248. }
  1249. return(TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,count,value));
  1250. }
  1251. #if 0
  1252. static int
  1253. TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int8_t value)
  1254. {
  1255. static const char module[] = "TIFFWriteDirectoryTagSbytePerSample";
  1256. int8_t* m;
  1257. int8_t* na;
  1258. uint16_t nb;
  1259. int o;
  1260. if (dir==NULL)
  1261. {
  1262. (*ndir)++;
  1263. return(1);
  1264. }
  1265. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int8_t));
  1266. if (m==NULL)
  1267. {
  1268. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1269. return(0);
  1270. }
  1271. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1272. *na=value;
  1273. o=TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1274. _TIFFfree(m);
  1275. return(o);
  1276. }
  1277. #endif
  1278. static int
  1279. TIFFWriteDirectoryTagShort(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint16_t value)
  1280. {
  1281. if (dir==NULL)
  1282. {
  1283. (*ndir)++;
  1284. return(1);
  1285. }
  1286. return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,value));
  1287. }
  1288. static int
  1289. TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint16_t* value)
  1290. {
  1291. if (dir==NULL)
  1292. {
  1293. (*ndir)++;
  1294. return(1);
  1295. }
  1296. return(TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,value));
  1297. }
  1298. static int
  1299. TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint16_t value)
  1300. {
  1301. static const char module[] = "TIFFWriteDirectoryTagShortPerSample";
  1302. uint16_t* m;
  1303. uint16_t* na;
  1304. uint16_t nb;
  1305. int o;
  1306. if (dir==NULL)
  1307. {
  1308. (*ndir)++;
  1309. return(1);
  1310. }
  1311. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint16_t));
  1312. if (m==NULL)
  1313. {
  1314. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1315. return(0);
  1316. }
  1317. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1318. *na=value;
  1319. o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1320. _TIFFfree(m);
  1321. return(o);
  1322. }
  1323. #ifdef notdef
  1324. static int
  1325. TIFFWriteDirectoryTagSshort(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int16_t value)
  1326. {
  1327. if (dir==NULL)
  1328. {
  1329. (*ndir)++;
  1330. return(1);
  1331. }
  1332. return(TIFFWriteDirectoryTagCheckedSshort(tif,ndir,dir,tag,value));
  1333. }
  1334. #endif
  1335. static int
  1336. TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int16_t* value)
  1337. {
  1338. if (dir==NULL)
  1339. {
  1340. (*ndir)++;
  1341. return(1);
  1342. }
  1343. return(TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,count,value));
  1344. }
  1345. #if 0
  1346. static int
  1347. TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int16_t value)
  1348. {
  1349. static const char module[] = "TIFFWriteDirectoryTagSshortPerSample";
  1350. int16_t* m;
  1351. int16_t* na;
  1352. uint16_t nb;
  1353. int o;
  1354. if (dir==NULL)
  1355. {
  1356. (*ndir)++;
  1357. return(1);
  1358. }
  1359. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int16_t));
  1360. if (m==NULL)
  1361. {
  1362. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1363. return(0);
  1364. }
  1365. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1366. *na=value;
  1367. o=TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1368. _TIFFfree(m);
  1369. return(o);
  1370. }
  1371. #endif
  1372. static int
  1373. TIFFWriteDirectoryTagLong(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t value)
  1374. {
  1375. if (dir==NULL)
  1376. {
  1377. (*ndir)++;
  1378. return(1);
  1379. }
  1380. return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
  1381. }
  1382. static int
  1383. TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint32_t* value)
  1384. {
  1385. if (dir==NULL)
  1386. {
  1387. (*ndir)++;
  1388. return(1);
  1389. }
  1390. return(TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,value));
  1391. }
  1392. #if 0
  1393. static int
  1394. TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t value)
  1395. {
  1396. static const char module[] = "TIFFWriteDirectoryTagLongPerSample";
  1397. uint32_t* m;
  1398. uint32_t* na;
  1399. uint16_t nb;
  1400. int o;
  1401. if (dir==NULL)
  1402. {
  1403. (*ndir)++;
  1404. return(1);
  1405. }
  1406. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint32_t));
  1407. if (m==NULL)
  1408. {
  1409. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1410. return(0);
  1411. }
  1412. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1413. *na=value;
  1414. o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1415. _TIFFfree(m);
  1416. return(o);
  1417. }
  1418. #endif
  1419. #ifdef notdef
  1420. static int
  1421. TIFFWriteDirectoryTagSlong(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int32_t value)
  1422. {
  1423. if (dir==NULL)
  1424. {
  1425. (*ndir)++;
  1426. return(1);
  1427. }
  1428. return(TIFFWriteDirectoryTagCheckedSlong(tif,ndir,dir,tag,value));
  1429. }
  1430. #endif
  1431. static int
  1432. TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int32_t* value)
  1433. {
  1434. if (dir==NULL)
  1435. {
  1436. (*ndir)++;
  1437. return(1);
  1438. }
  1439. return(TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,count,value));
  1440. }
  1441. #if 0
  1442. static int
  1443. TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int32_t value)
  1444. {
  1445. static const char module[] = "TIFFWriteDirectoryTagSlongPerSample";
  1446. int32_t* m;
  1447. int32_t* na;
  1448. uint16_t nb;
  1449. int o;
  1450. if (dir==NULL)
  1451. {
  1452. (*ndir)++;
  1453. return(1);
  1454. }
  1455. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int32_t));
  1456. if (m==NULL)
  1457. {
  1458. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1459. return(0);
  1460. }
  1461. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1462. *na=value;
  1463. o=TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1464. _TIFFfree(m);
  1465. return(o);
  1466. }
  1467. #endif
  1468. #ifdef notdef
  1469. static int
  1470. TIFFWriteDirectoryTagLong8(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint64_t value)
  1471. {
  1472. if (dir==NULL)
  1473. {
  1474. (*ndir)++;
  1475. return(1);
  1476. }
  1477. return(TIFFWriteDirectoryTagCheckedLong8(tif,ndir,dir,tag,value));
  1478. }
  1479. #endif
  1480. static int
  1481. TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value)
  1482. {
  1483. if (dir==NULL)
  1484. {
  1485. (*ndir)++;
  1486. return(1);
  1487. }
  1488. return(TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value));
  1489. }
  1490. #ifdef notdef
  1491. static int
  1492. TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int64_t value)
  1493. {
  1494. if (dir==NULL)
  1495. {
  1496. (*ndir)++;
  1497. return(1);
  1498. }
  1499. return(TIFFWriteDirectoryTagCheckedSlong8(tif,ndir,dir,tag,value));
  1500. }
  1501. #endif
  1502. static int
  1503. TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int64_t* value)
  1504. {
  1505. if (dir==NULL)
  1506. {
  1507. (*ndir)++;
  1508. return(1);
  1509. }
  1510. return(TIFFWriteDirectoryTagCheckedSlong8Array(tif,ndir,dir,tag,count,value));
  1511. }
  1512. static int
  1513. TIFFWriteDirectoryTagRational(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value)
  1514. {
  1515. if (dir==NULL)
  1516. {
  1517. (*ndir)++;
  1518. return(1);
  1519. }
  1520. return(TIFFWriteDirectoryTagCheckedRational(tif,ndir,dir,tag,value));
  1521. }
  1522. static int
  1523. TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value)
  1524. {
  1525. if (dir==NULL)
  1526. {
  1527. (*ndir)++;
  1528. return(1);
  1529. }
  1530. return(TIFFWriteDirectoryTagCheckedRationalArray(tif,ndir,dir,tag,count,value));
  1531. }
  1532. static int
  1533. TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value)
  1534. {
  1535. if (dir==NULL)
  1536. {
  1537. (*ndir)++;
  1538. return(1);
  1539. }
  1540. return(TIFFWriteDirectoryTagCheckedSrationalArray(tif,ndir,dir,tag,count,value));
  1541. }
  1542. /*-- Rational2Double: additional write functions */
  1543. static int
  1544. TIFFWriteDirectoryTagRationalDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value)
  1545. {
  1546. if (dir==NULL)
  1547. {
  1548. (*ndir)++;
  1549. return(1);
  1550. }
  1551. return(TIFFWriteDirectoryTagCheckedRationalDoubleArray(tif,ndir,dir,tag,count,value));
  1552. }
  1553. static int
  1554. TIFFWriteDirectoryTagSrationalDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value)
  1555. {
  1556. if (dir==NULL)
  1557. {
  1558. (*ndir)++;
  1559. return(1);
  1560. }
  1561. return(TIFFWriteDirectoryTagCheckedSrationalDoubleArray(tif,ndir,dir,tag,count,value));
  1562. }
  1563. #ifdef notdef
  1564. static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, float value)
  1565. {
  1566. if (dir==NULL)
  1567. {
  1568. (*ndir)++;
  1569. return(1);
  1570. }
  1571. return(TIFFWriteDirectoryTagCheckedFloat(tif,ndir,dir,tag,value));
  1572. }
  1573. #endif
  1574. static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value)
  1575. {
  1576. if (dir==NULL)
  1577. {
  1578. (*ndir)++;
  1579. return(1);
  1580. }
  1581. return(TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,count,value));
  1582. }
  1583. #if 0
  1584. static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, float value)
  1585. {
  1586. static const char module[] = "TIFFWriteDirectoryTagFloatPerSample";
  1587. float* m;
  1588. float* na;
  1589. uint16_t nb;
  1590. int o;
  1591. if (dir==NULL)
  1592. {
  1593. (*ndir)++;
  1594. return(1);
  1595. }
  1596. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(float));
  1597. if (m==NULL)
  1598. {
  1599. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1600. return(0);
  1601. }
  1602. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1603. *na=value;
  1604. o=TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1605. _TIFFfree(m);
  1606. return(o);
  1607. }
  1608. #endif
  1609. #ifdef notdef
  1610. static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value)
  1611. {
  1612. if (dir==NULL)
  1613. {
  1614. (*ndir)++;
  1615. return(1);
  1616. }
  1617. return(TIFFWriteDirectoryTagCheckedDouble(tif,ndir,dir,tag,value));
  1618. }
  1619. #endif
  1620. static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value)
  1621. {
  1622. if (dir==NULL)
  1623. {
  1624. (*ndir)++;
  1625. return(1);
  1626. }
  1627. return(TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,count,value));
  1628. }
  1629. #if 0
  1630. static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value)
  1631. {
  1632. static const char module[] = "TIFFWriteDirectoryTagDoublePerSample";
  1633. double* m;
  1634. double* na;
  1635. uint16_t nb;
  1636. int o;
  1637. if (dir==NULL)
  1638. {
  1639. (*ndir)++;
  1640. return(1);
  1641. }
  1642. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(double));
  1643. if (m==NULL)
  1644. {
  1645. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1646. return(0);
  1647. }
  1648. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1649. *na=value;
  1650. o=TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1651. _TIFFfree(m);
  1652. return(o);
  1653. }
  1654. #endif
  1655. static int
  1656. TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint32_t* value)
  1657. {
  1658. if (dir==NULL)
  1659. {
  1660. (*ndir)++;
  1661. return(1);
  1662. }
  1663. return(TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,value));
  1664. }
  1665. #ifdef notdef
  1666. static int
  1667. TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value)
  1668. {
  1669. if (dir==NULL)
  1670. {
  1671. (*ndir)++;
  1672. return(1);
  1673. }
  1674. return(TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,tag,count,value));
  1675. }
  1676. #endif
  1677. static int
  1678. TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t value)
  1679. {
  1680. if (dir==NULL)
  1681. {
  1682. (*ndir)++;
  1683. return(1);
  1684. }
  1685. if (value<=0xFFFF)
  1686. return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,(uint16_t)value));
  1687. else
  1688. return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
  1689. }
  1690. static int _WriteAsType(TIFF* tif, uint64_t strile_size, uint64_t uncompressed_threshold)
  1691. {
  1692. const uint16_t compression = tif->tif_dir.td_compression;
  1693. if ( compression == COMPRESSION_NONE )
  1694. {
  1695. return strile_size > uncompressed_threshold;
  1696. }
  1697. else if ( compression == COMPRESSION_JPEG ||
  1698. compression == COMPRESSION_LZW ||
  1699. compression == COMPRESSION_ADOBE_DEFLATE ||
  1700. compression == COMPRESSION_LZMA ||
  1701. compression == COMPRESSION_LERC ||
  1702. compression == COMPRESSION_ZSTD ||
  1703. compression == COMPRESSION_WEBP )
  1704. {
  1705. /* For a few select compression types, we assume that in the worst */
  1706. /* case the compressed size will be 10 times the uncompressed size */
  1707. /* This is overly pessismistic ! */
  1708. return strile_size >= uncompressed_threshold / 10;
  1709. }
  1710. return 1;
  1711. }
  1712. static int WriteAsLong8(TIFF* tif, uint64_t strile_size)
  1713. {
  1714. return _WriteAsType(tif, strile_size, 0xFFFFFFFFU);
  1715. }
  1716. static int WriteAsLong4(TIFF* tif, uint64_t strile_size)
  1717. {
  1718. return _WriteAsType(tif, strile_size, 0xFFFFU);
  1719. }
  1720. /************************************************************************/
  1721. /* TIFFWriteDirectoryTagLongLong8Array() */
  1722. /* */
  1723. /* Write out LONG8 array and write a SHORT/LONG/LONG8 depending */
  1724. /* on strile size and Classic/BigTIFF mode. */
  1725. /************************************************************************/
  1726. static int
  1727. TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value)
  1728. {
  1729. static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";
  1730. int o;
  1731. int write_aslong4;
  1732. /* is this just a counting pass? */
  1733. if (dir==NULL)
  1734. {
  1735. (*ndir)++;
  1736. return(1);
  1737. }
  1738. if( tif->tif_dir.td_deferstrilearraywriting )
  1739. {
  1740. return TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_NOTYPE, 0, 0, NULL);
  1741. }
  1742. if( tif->tif_flags&TIFF_BIGTIFF )
  1743. {
  1744. int write_aslong8 = 1;
  1745. /* In the case of ByteCounts array, we may be able to write them on */
  1746. /* LONG if the strip/tilesize is not too big. */
  1747. /* Also do that for count > 1 in the case someone would want to create */
  1748. /* a single-strip file with a growing height, in which case using */
  1749. /* LONG8 will be safer. */
  1750. if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
  1751. {
  1752. write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif));
  1753. }
  1754. else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
  1755. {
  1756. write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif));
  1757. }
  1758. if( write_aslong8 )
  1759. {
  1760. return TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,
  1761. tag,count,value);
  1762. }
  1763. }
  1764. write_aslong4 = 1;
  1765. if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
  1766. {
  1767. write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif));
  1768. }
  1769. else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
  1770. {
  1771. write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif));
  1772. }
  1773. if( write_aslong4 )
  1774. {
  1775. /*
  1776. ** For classic tiff we want to verify everything is in range for LONG
  1777. ** and convert to long format.
  1778. */
  1779. uint32_t* p = _TIFFmalloc(count * sizeof(uint32_t));
  1780. uint32_t* q;
  1781. uint64_t* ma;
  1782. uint32_t mb;
  1783. if (p==NULL)
  1784. {
  1785. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1786. return(0);
  1787. }
  1788. for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
  1789. {
  1790. if (*ma>0xFFFFFFFF)
  1791. {
  1792. TIFFErrorExt(tif->tif_clientdata,module,
  1793. "Attempt to write value larger than 0xFFFFFFFF in LONG array.");
  1794. _TIFFfree(p);
  1795. return(0);
  1796. }
  1797. *q= (uint32_t)(*ma);
  1798. }
  1799. o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
  1800. _TIFFfree(p);
  1801. }
  1802. else
  1803. {
  1804. uint16_t* p = _TIFFmalloc(count * sizeof(uint16_t));
  1805. uint16_t* q;
  1806. uint64_t* ma;
  1807. uint32_t mb;
  1808. if (p==NULL)
  1809. {
  1810. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1811. return(0);
  1812. }
  1813. for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
  1814. {
  1815. if (*ma>0xFFFF)
  1816. {
  1817. /* Should not happen normally given the check we did before */
  1818. TIFFErrorExt(tif->tif_clientdata,module,
  1819. "Attempt to write value larger than 0xFFFF in SHORT array.");
  1820. _TIFFfree(p);
  1821. return(0);
  1822. }
  1823. *q= (uint16_t)(*ma);
  1824. }
  1825. o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,p);
  1826. _TIFFfree(p);
  1827. }
  1828. return(o);
  1829. }
  1830. /************************************************************************/
  1831. /* TIFFWriteDirectoryTagIfdIfd8Array() */
  1832. /* */
  1833. /* Write either IFD8 or IFD array depending on file type. */
  1834. /************************************************************************/
  1835. static int
  1836. TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value)
  1837. {
  1838. static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array";
  1839. uint64_t* ma;
  1840. uint32_t mb;
  1841. uint32_t* p;
  1842. uint32_t* q;
  1843. int o;
  1844. /* is this just a counting pass? */
  1845. if (dir==NULL)
  1846. {
  1847. (*ndir)++;
  1848. return(1);
  1849. }
  1850. /* We always write IFD8 for BigTIFF, no checking needed. */
  1851. if( tif->tif_flags&TIFF_BIGTIFF )
  1852. return TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,
  1853. tag,count,value);
  1854. /*
  1855. ** For classic tiff we want to verify everything is in range for IFD
  1856. ** and convert to long format.
  1857. */
  1858. p = _TIFFmalloc(count*sizeof(uint32_t));
  1859. if (p==NULL)
  1860. {
  1861. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1862. return(0);
  1863. }
  1864. for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
  1865. {
  1866. if (*ma>0xFFFFFFFF)
  1867. {
  1868. TIFFErrorExt(tif->tif_clientdata,module,
  1869. "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
  1870. _TIFFfree(p);
  1871. return(0);
  1872. }
  1873. *q= (uint32_t)(*ma);
  1874. }
  1875. o=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,p);
  1876. _TIFFfree(p);
  1877. return(o);
  1878. }
  1879. #ifdef notdef
  1880. static int
  1881. TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value)
  1882. {
  1883. static const char module[] = "TIFFWriteDirectoryTagShortLongLong8Array";
  1884. uint64_t* ma;
  1885. uint32_t mb;
  1886. uint8_t n;
  1887. int o;
  1888. if (dir==NULL)
  1889. {
  1890. (*ndir)++;
  1891. return(1);
  1892. }
  1893. n=0;
  1894. for (ma=value, mb=0; mb<count; ma++, mb++)
  1895. {
  1896. if ((n==0)&&(*ma>0xFFFF))
  1897. n=1;
  1898. if ((n==1)&&(*ma>0xFFFFFFFF))
  1899. {
  1900. n=2;
  1901. break;
  1902. }
  1903. }
  1904. if (n==0)
  1905. {
  1906. uint16_t* p;
  1907. uint16_t* q;
  1908. p=_TIFFmalloc(count*sizeof(uint16_t));
  1909. if (p==NULL)
  1910. {
  1911. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1912. return(0);
  1913. }
  1914. for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
  1915. *q=(uint16_t)(*ma);
  1916. o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,p);
  1917. _TIFFfree(p);
  1918. }
  1919. else if (n==1)
  1920. {
  1921. uint32_t* p;
  1922. uint32_t* q;
  1923. p=_TIFFmalloc(count*sizeof(uint32_t));
  1924. if (p==NULL)
  1925. {
  1926. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1927. return(0);
  1928. }
  1929. for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
  1930. *q=(uint32_t)(*ma);
  1931. o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
  1932. _TIFFfree(p);
  1933. }
  1934. else
  1935. {
  1936. assert(n==2);
  1937. o=TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value);
  1938. }
  1939. return(o);
  1940. }
  1941. #endif
  1942. static int
  1943. TIFFWriteDirectoryTagColormap(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir)
  1944. {
  1945. static const char module[] = "TIFFWriteDirectoryTagColormap";
  1946. uint32_t m;
  1947. uint16_t* n;
  1948. int o;
  1949. if (dir==NULL)
  1950. {
  1951. (*ndir)++;
  1952. return(1);
  1953. }
  1954. m=(1<<tif->tif_dir.td_bitspersample);
  1955. n=_TIFFmalloc(3*m*sizeof(uint16_t));
  1956. if (n==NULL)
  1957. {
  1958. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1959. return(0);
  1960. }
  1961. _TIFFmemcpy(&n[0],tif->tif_dir.td_colormap[0],m*sizeof(uint16_t));
  1962. _TIFFmemcpy(&n[m],tif->tif_dir.td_colormap[1],m*sizeof(uint16_t));
  1963. _TIFFmemcpy(&n[2*m],tif->tif_dir.td_colormap[2],m*sizeof(uint16_t));
  1964. o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_COLORMAP,3*m,n);
  1965. _TIFFfree(n);
  1966. return(o);
  1967. }
  1968. static int
  1969. TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir)
  1970. {
  1971. static const char module[] = "TIFFWriteDirectoryTagTransferfunction";
  1972. uint32_t m;
  1973. uint16_t n;
  1974. uint16_t* o;
  1975. int p;
  1976. if (dir==NULL)
  1977. {
  1978. (*ndir)++;
  1979. return(1);
  1980. }
  1981. m=(1<<tif->tif_dir.td_bitspersample);
  1982. n=tif->tif_dir.td_samplesperpixel-tif->tif_dir.td_extrasamples;
  1983. /*
  1984. * Check if the table can be written as a single column,
  1985. * or if it must be written as 3 columns. Note that we
  1986. * write a 3-column tag if there are 2 samples/pixel and
  1987. * a single column of data won't suffice--hmm.
  1988. */
  1989. if (n>3)
  1990. n=3;
  1991. if (n==3)
  1992. {
  1993. if (tif->tif_dir.td_transferfunction[2] == NULL ||
  1994. !_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16_t)))
  1995. n=2;
  1996. }
  1997. if (n==2)
  1998. {
  1999. if (tif->tif_dir.td_transferfunction[1] == NULL ||
  2000. !_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16_t)))
  2001. n=1;
  2002. }
  2003. if (n==0)
  2004. n=1;
  2005. o=_TIFFmalloc(n*m*sizeof(uint16_t));
  2006. if (o==NULL)
  2007. {
  2008. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  2009. return(0);
  2010. }
  2011. _TIFFmemcpy(&o[0],tif->tif_dir.td_transferfunction[0],m*sizeof(uint16_t));
  2012. if (n>1)
  2013. _TIFFmemcpy(&o[m],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16_t));
  2014. if (n>2)
  2015. _TIFFmemcpy(&o[2*m],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16_t));
  2016. p=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_TRANSFERFUNCTION,n*m,o);
  2017. _TIFFfree(o);
  2018. return(p);
  2019. }
  2020. static int
  2021. TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir)
  2022. {
  2023. static const char module[] = "TIFFWriteDirectoryTagSubifd";
  2024. uint64_t m;
  2025. int n;
  2026. if (tif->tif_dir.td_nsubifd==0)
  2027. return(1);
  2028. if (dir==NULL)
  2029. {
  2030. (*ndir)++;
  2031. return(1);
  2032. }
  2033. m=tif->tif_dataoff;
  2034. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2035. {
  2036. uint32_t* o;
  2037. uint64_t* pa;
  2038. uint32_t* pb;
  2039. uint16_t p;
  2040. o=_TIFFmalloc(tif->tif_dir.td_nsubifd*sizeof(uint32_t));
  2041. if (o==NULL)
  2042. {
  2043. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  2044. return(0);
  2045. }
  2046. pa=tif->tif_dir.td_subifd;
  2047. pb=o;
  2048. for (p=0; p < tif->tif_dir.td_nsubifd; p++)
  2049. {
  2050. assert(pa != 0);
  2051. /* Could happen if an classicTIFF has a SubIFD of type LONG8 (which is illegal) */
  2052. if( *pa > 0xFFFFFFFFUL)
  2053. {
  2054. TIFFErrorExt(tif->tif_clientdata,module,"Illegal value for SubIFD tag");
  2055. _TIFFfree(o);
  2056. return(0);
  2057. }
  2058. *pb++=(uint32_t)(*pa++);
  2059. }
  2060. n=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,o);
  2061. _TIFFfree(o);
  2062. }
  2063. else
  2064. n=TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,tif->tif_dir.td_subifd);
  2065. if (!n)
  2066. return(0);
  2067. /*
  2068. * Total hack: if this directory includes a SubIFD
  2069. * tag then force the next <n> directories to be
  2070. * written as ``sub directories'' of this one. This
  2071. * is used to write things like thumbnails and
  2072. * image masks that one wants to keep out of the
  2073. * normal directory linkage access mechanism.
  2074. */
  2075. tif->tif_flags|=TIFF_INSUBIFD;
  2076. tif->tif_nsubifd=tif->tif_dir.td_nsubifd;
  2077. if (tif->tif_dir.td_nsubifd==1)
  2078. tif->tif_subifdoff=0;
  2079. else
  2080. tif->tif_subifdoff=m;
  2081. return(1);
  2082. }
  2083. static int
  2084. TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, char* value)
  2085. {
  2086. assert(sizeof(char)==1);
  2087. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_ASCII,count,count,value));
  2088. }
  2089. static int
  2090. TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint8_t* value)
  2091. {
  2092. assert(sizeof(uint8_t) == 1);
  2093. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_UNDEFINED,count,count,value));
  2094. }
  2095. #ifdef notdef
  2096. static int
  2097. TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint8_t value)
  2098. {
  2099. assert(sizeof(uint8_t)==1);
  2100. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,1,1,&value));
  2101. }
  2102. #endif
  2103. static int
  2104. TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint8_t* value)
  2105. {
  2106. assert(sizeof(uint8_t) == 1);
  2107. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,count,count,value));
  2108. }
  2109. #ifdef notdef
  2110. static int
  2111. TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int8_t value)
  2112. {
  2113. assert(sizeof(int8_t)==1);
  2114. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,1,1,&value));
  2115. }
  2116. #endif
  2117. static int
  2118. TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int8_t* value)
  2119. {
  2120. assert(sizeof(int8_t) == 1);
  2121. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,count,count,value));
  2122. }
  2123. static int
  2124. TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint16_t value)
  2125. {
  2126. uint16_t m;
  2127. assert(sizeof(uint16_t) == 2);
  2128. m=value;
  2129. if (tif->tif_flags&TIFF_SWAB)
  2130. TIFFSwabShort(&m);
  2131. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,1,2,&m));
  2132. }
  2133. static int
  2134. TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint16_t* value)
  2135. {
  2136. assert(count<0x80000000);
  2137. assert(sizeof(uint16_t) == 2);
  2138. if (tif->tif_flags&TIFF_SWAB)
  2139. TIFFSwabArrayOfShort(value,count);
  2140. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,count,count*2,value));
  2141. }
  2142. #ifdef notdef
  2143. static int
  2144. TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int16_t value)
  2145. {
  2146. int16_t m;
  2147. assert(sizeof(int16_t)==2);
  2148. m=value;
  2149. if (tif->tif_flags&TIFF_SWAB)
  2150. TIFFSwabShort((uint16_t*)(&m));
  2151. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,1,2,&m));
  2152. }
  2153. #endif
  2154. static int
  2155. TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int16_t* value)
  2156. {
  2157. assert(count<0x80000000);
  2158. assert(sizeof(int16_t) == 2);
  2159. if (tif->tif_flags&TIFF_SWAB)
  2160. TIFFSwabArrayOfShort((uint16_t*)value, count);
  2161. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,count,count*2,value));
  2162. }
  2163. static int
  2164. TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t value)
  2165. {
  2166. uint32_t m;
  2167. assert(sizeof(uint32_t) == 4);
  2168. m=value;
  2169. if (tif->tif_flags&TIFF_SWAB)
  2170. TIFFSwabLong(&m);
  2171. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,1,4,&m));
  2172. }
  2173. static int
  2174. TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint32_t* value)
  2175. {
  2176. assert(count<0x40000000);
  2177. assert(sizeof(uint32_t) == 4);
  2178. if (tif->tif_flags&TIFF_SWAB)
  2179. TIFFSwabArrayOfLong(value,count);
  2180. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,count,count*4,value));
  2181. }
  2182. #ifdef notdef
  2183. static int
  2184. TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int32_t value)
  2185. {
  2186. int32_t m;
  2187. assert(sizeof(int32_t)==4);
  2188. m=value;
  2189. if (tif->tif_flags&TIFF_SWAB)
  2190. TIFFSwabLong((uint32_t*)(&m));
  2191. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,1,4,&m));
  2192. }
  2193. #endif
  2194. static int
  2195. TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int32_t* value)
  2196. {
  2197. assert(count<0x40000000);
  2198. assert(sizeof(int32_t) == 4);
  2199. if (tif->tif_flags&TIFF_SWAB)
  2200. TIFFSwabArrayOfLong((uint32_t*)value, count);
  2201. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,count,count*4,value));
  2202. }
  2203. #ifdef notdef
  2204. static int
  2205. TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint64_t value)
  2206. {
  2207. uint64_t m;
  2208. assert(sizeof(uint64_t)==8);
  2209. if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
  2210. TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedLong8","LONG8 not allowed for ClassicTIFF");
  2211. return(0);
  2212. }
  2213. m=value;
  2214. if (tif->tif_flags&TIFF_SWAB)
  2215. TIFFSwabLong8(&m);
  2216. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,1,8,&m));
  2217. }
  2218. #endif
  2219. static int
  2220. TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value)
  2221. {
  2222. assert(count<0x20000000);
  2223. assert(sizeof(uint64_t) == 8);
  2224. if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
  2225. TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedLong8Array","LONG8 not allowed for ClassicTIFF");
  2226. return(0);
  2227. }
  2228. if (tif->tif_flags&TIFF_SWAB)
  2229. TIFFSwabArrayOfLong8(value,count);
  2230. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,count,count*8,value));
  2231. }
  2232. #ifdef notdef
  2233. static int
  2234. TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, int64_t value)
  2235. {
  2236. int64_t m;
  2237. assert(sizeof(int64_t)==8);
  2238. if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
  2239. TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedSlong8","SLONG8 not allowed for ClassicTIFF");
  2240. return(0);
  2241. }
  2242. m=value;
  2243. if (tif->tif_flags&TIFF_SWAB)
  2244. TIFFSwabLong8((uint64_t*)(&m));
  2245. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,1,8,&m));
  2246. }
  2247. #endif
  2248. static int
  2249. TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, int64_t* value)
  2250. {
  2251. assert(count<0x20000000);
  2252. assert(sizeof(int64_t) == 8);
  2253. if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
  2254. TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedSlong8Array","SLONG8 not allowed for ClassicTIFF");
  2255. return(0);
  2256. }
  2257. if (tif->tif_flags&TIFF_SWAB)
  2258. TIFFSwabArrayOfLong8((uint64_t*)value, count);
  2259. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,count,count*8,value));
  2260. }
  2261. static int
  2262. TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value)
  2263. {
  2264. static const char module[] = "TIFFWriteDirectoryTagCheckedRational";
  2265. uint32_t m[2];
  2266. assert(sizeof(uint32_t) == 4);
  2267. if (value < 0)
  2268. {
  2269. TIFFErrorExt(tif->tif_clientdata, module, "Negative value is illegal");
  2270. return 0;
  2271. }
  2272. else if (value != value)
  2273. {
  2274. TIFFErrorExt(tif->tif_clientdata, module, "Not-a-number value is illegal");
  2275. return 0;
  2276. }
  2277. #ifdef not_def
  2278. else if (value==0.0)
  2279. {
  2280. m[0]=0;
  2281. m[1]=1;
  2282. }
  2283. else if (value <= 0xFFFFFFFFU && value==(double)(uint32_t)value)
  2284. {
  2285. m[0]=(uint32_t)value;
  2286. m[1]=1;
  2287. }
  2288. else if (value<1.0)
  2289. {
  2290. m[0]=(uint32_t)(value*0xFFFFFFFF);
  2291. m[1]=0xFFFFFFFF;
  2292. }
  2293. else
  2294. {
  2295. m[0]=0xFFFFFFFF;
  2296. m[1]=(uint32_t)(0xFFFFFFFF/value);
  2297. }
  2298. #else
  2299. /*--Rational2Double: New function also used for non-custom rational tags.
  2300. * However, could be omitted here, because TIFFWriteDirectoryTagCheckedRational() is not used by code for custom tags,
  2301. * only by code for named-tiff-tags like FIELD_RESOLUTION and FIELD_POSITION */
  2302. else {
  2303. DoubleToRational(value, &m[0], &m[1]);
  2304. }
  2305. #endif
  2306. if (tif->tif_flags&TIFF_SWAB)
  2307. {
  2308. TIFFSwabLong(&m[0]);
  2309. TIFFSwabLong(&m[1]);
  2310. }
  2311. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,1,8,&m[0]));
  2312. }
  2313. static int
  2314. TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value)
  2315. {
  2316. static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
  2317. uint32_t* m;
  2318. float* na;
  2319. uint32_t* nb;
  2320. uint32_t nc;
  2321. int o;
  2322. assert(sizeof(uint32_t) == 4);
  2323. m=_TIFFmalloc(count*2*sizeof(uint32_t));
  2324. if (m==NULL)
  2325. {
  2326. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  2327. return(0);
  2328. }
  2329. for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
  2330. {
  2331. #ifdef not_def
  2332. if (*na<=0.0 || *na != *na)
  2333. {
  2334. nb[0]=0;
  2335. nb[1]=1;
  2336. }
  2337. else if (*na >= 0 && *na <= (float)0xFFFFFFFFU &&
  2338. *na==(float)(uint32_t)(*na))
  2339. {
  2340. nb[0]=(uint32_t)(*na);
  2341. nb[1]=1;
  2342. }
  2343. else if (*na<1.0)
  2344. {
  2345. nb[0]=(uint32_t)((double)(*na)*0xFFFFFFFF);
  2346. nb[1]=0xFFFFFFFF;
  2347. }
  2348. else
  2349. {
  2350. nb[0]=0xFFFFFFFF;
  2351. nb[1]=(uint32_t)((double)0xFFFFFFFF/(*na));
  2352. }
  2353. #else
  2354. /*-- Rational2Double: Also for float precision accuracy is sometimes enhanced --*/
  2355. DoubleToRational(*na, &nb[0], &nb[1]);
  2356. #endif
  2357. }
  2358. if (tif->tif_flags&TIFF_SWAB)
  2359. TIFFSwabArrayOfLong(m,count*2);
  2360. o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,count,count*8,&m[0]);
  2361. _TIFFfree(m);
  2362. return(o);
  2363. }
  2364. static int
  2365. TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value)
  2366. {
  2367. static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
  2368. int32_t* m;
  2369. float* na;
  2370. int32_t* nb;
  2371. uint32_t nc;
  2372. int o;
  2373. assert(sizeof(int32_t) == 4);
  2374. m=_TIFFmalloc(count*2*sizeof(int32_t));
  2375. if (m==NULL)
  2376. {
  2377. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  2378. return(0);
  2379. }
  2380. for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
  2381. {
  2382. #ifdef not_def
  2383. if (*na<0.0)
  2384. {
  2385. if (*na==(int32_t)(*na))
  2386. {
  2387. nb[0]=(int32_t)(*na);
  2388. nb[1]=1;
  2389. }
  2390. else if (*na>-1.0)
  2391. {
  2392. nb[0]=-(int32_t)((double)(-*na)*0x7FFFFFFF);
  2393. nb[1]=0x7FFFFFFF;
  2394. }
  2395. else
  2396. {
  2397. nb[0]=-0x7FFFFFFF;
  2398. nb[1]=(int32_t)((double)0x7FFFFFFF/(-*na));
  2399. }
  2400. }
  2401. else
  2402. {
  2403. if (*na==(int32_t)(*na))
  2404. {
  2405. nb[0]=(int32_t)(*na);
  2406. nb[1]=1;
  2407. }
  2408. else if (*na<1.0)
  2409. {
  2410. nb[0]=(int32_t)((double)(*na)*0x7FFFFFFF);
  2411. nb[1]=0x7FFFFFFF;
  2412. }
  2413. else
  2414. {
  2415. nb[0]=0x7FFFFFFF;
  2416. nb[1]=(int32_t)((double)0x7FFFFFFF/(*na));
  2417. }
  2418. }
  2419. #else
  2420. /*-- Rational2Double: Also for float precision accuracy is sometimes enhanced --*/
  2421. DoubleToSrational(*na, &nb[0], &nb[1]);
  2422. #endif
  2423. }
  2424. if (tif->tif_flags&TIFF_SWAB)
  2425. TIFFSwabArrayOfLong((uint32_t*)m, count * 2);
  2426. o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SRATIONAL,count,count*8,&m[0]);
  2427. _TIFFfree(m);
  2428. return(o);
  2429. }
  2430. /*-- Rational2Double: additional write functions for double arrays */
  2431. static int
  2432. TIFFWriteDirectoryTagCheckedRationalDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value)
  2433. {
  2434. static const char module[] = "TIFFWriteDirectoryTagCheckedRationalDoubleArray";
  2435. uint32_t* m;
  2436. double* na;
  2437. uint32_t* nb;
  2438. uint32_t nc;
  2439. int o;
  2440. assert(sizeof(uint32_t) == 4);
  2441. m=_TIFFmalloc(count*2*sizeof(uint32_t));
  2442. if (m==NULL)
  2443. {
  2444. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  2445. return(0);
  2446. }
  2447. for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
  2448. {
  2449. DoubleToRational(*na, &nb[0], &nb[1]);
  2450. }
  2451. if (tif->tif_flags&TIFF_SWAB)
  2452. TIFFSwabArrayOfLong(m,count*2);
  2453. o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,count,count*8,&m[0]);
  2454. _TIFFfree(m);
  2455. return(o);
  2456. } /*-- TIFFWriteDirectoryTagCheckedRationalDoubleArray() ------- */
  2457. static int
  2458. TIFFWriteDirectoryTagCheckedSrationalDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value)
  2459. {
  2460. static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalDoubleArray";
  2461. int32_t* m;
  2462. double* na;
  2463. int32_t* nb;
  2464. uint32_t nc;
  2465. int o;
  2466. assert(sizeof(int32_t) == 4);
  2467. m=_TIFFmalloc(count*2*sizeof(int32_t));
  2468. if (m==NULL)
  2469. {
  2470. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  2471. return(0);
  2472. }
  2473. for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
  2474. {
  2475. DoubleToSrational(*na, &nb[0], &nb[1]);
  2476. }
  2477. if (tif->tif_flags&TIFF_SWAB)
  2478. TIFFSwabArrayOfLong((uint32_t*)m, count * 2);
  2479. o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SRATIONAL,count,count*8,&m[0]);
  2480. _TIFFfree(m);
  2481. return(o);
  2482. } /*--- TIFFWriteDirectoryTagCheckedSrationalDoubleArray() -------- */
  2483. #if 0
  2484. static
  2485. void DoubleToRational_direct(double value, unsigned long *num, unsigned long *denom)
  2486. {
  2487. /*--- OLD Code for debugging and comparison ---- */
  2488. /* code merged from TIFFWriteDirectoryTagCheckedRationalArray() and TIFFWriteDirectoryTagCheckedRational() */
  2489. /* First check for zero and also check for negative numbers (which are illegal for RATIONAL)
  2490. * and also check for "not-a-number". In each case just set this to zero to support also rational-arrays.
  2491. */
  2492. if (value<=0.0 || value != value)
  2493. {
  2494. *num=0;
  2495. *denom=1;
  2496. }
  2497. else if (value <= 0xFFFFFFFFU && (value==(double)(uint32_t)(value))) /* check for integer values */
  2498. {
  2499. *num=(uint32_t)(value);
  2500. *denom=1;
  2501. }
  2502. else if (value<1.0)
  2503. {
  2504. *num = (uint32_t)((value) * (double)0xFFFFFFFFU);
  2505. *denom=0xFFFFFFFFU;
  2506. }
  2507. else
  2508. {
  2509. *num=0xFFFFFFFFU;
  2510. *denom=(uint32_t)((double)0xFFFFFFFFU/(value));
  2511. }
  2512. } /*-- DoubleToRational_direct() -------------- */
  2513. #endif
  2514. #if 0
  2515. static
  2516. void DoubleToSrational_direct(double value, long *num, long *denom)
  2517. {
  2518. /*--- OLD Code for debugging and comparison -- SIGNED-version ----*/
  2519. /* code was amended from original TIFFWriteDirectoryTagCheckedSrationalArray() */
  2520. /* First check for zero and also check for negative numbers (which are illegal for RATIONAL)
  2521. * and also check for "not-a-number". In each case just set this to zero to support also rational-arrays.
  2522. */
  2523. if (value<0.0)
  2524. {
  2525. if (value==(int32_t)(value))
  2526. {
  2527. *num=(int32_t)(value);
  2528. *denom=1;
  2529. }
  2530. else if (value>-1.0)
  2531. {
  2532. *num=-(int32_t)((-value) * (double)0x7FFFFFFF);
  2533. *denom=0x7FFFFFFF;
  2534. }
  2535. else
  2536. {
  2537. *num=-0x7FFFFFFF;
  2538. *denom=(int32_t)((double)0x7FFFFFFF / (-value));
  2539. }
  2540. }
  2541. else
  2542. {
  2543. if (value==(int32_t)(value))
  2544. {
  2545. *num=(int32_t)(value);
  2546. *denom=1;
  2547. }
  2548. else if (value<1.0)
  2549. {
  2550. *num=(int32_t)((value) *(double)0x7FFFFFFF);
  2551. *denom=0x7FFFFFFF;
  2552. }
  2553. else
  2554. {
  2555. *num=0x7FFFFFFF;
  2556. *denom=(int32_t)((double)0x7FFFFFFF / (value));
  2557. }
  2558. }
  2559. } /*-- DoubleToSrational_direct() --------------*/
  2560. #endif
  2561. //#define DOUBLE2RAT_DEBUGOUTPUT
  2562. /** ----- Rational2Double: Double To Rational Conversion ----------------------------------------------------------
  2563. * There is a mathematical theorem to convert real numbers into a rational (integer fraction) number.
  2564. * This is called "continuous fraction" which uses the Euclidean algorithm to find the greatest common divisor (GCD).
  2565. * (ref. e.g. https://de.wikipedia.org/wiki/Kettenbruch or https://en.wikipedia.org/wiki/Continued_fraction
  2566. * https://en.wikipedia.org/wiki/Euclidean_algorithm)
  2567. * The following functions implement the
  2568. * - ToRationalEuclideanGCD() auxiliary function which mainly implements euclidean GCD
  2569. * - DoubleToRational() conversion function for un-signed rationals
  2570. * - DoubleToSrational() conversion function for signed rationals
  2571. ------------------------------------------------------------------------------------------------------------------*/
  2572. /**---- ToRationalEuclideanGCD() -----------------------------------------
  2573. * Calculates the rational fractional of a double input value
  2574. * using the Euclidean algorithm to find the greatest common divisor (GCD)
  2575. ------------------------------------------------------------------------*/
  2576. static
  2577. void ToRationalEuclideanGCD(double value, int blnUseSignedRange, int blnUseSmallRange, uint64_t *ullNum, uint64_t *ullDenom)
  2578. {
  2579. /* Internally, the integer variables can be bigger than the external ones,
  2580. * as long as the result will fit into the external variable size.
  2581. */
  2582. uint64_t numSum[3] = { 0, 1, 0 }, denomSum[3] = { 1, 0, 0 };
  2583. uint64_t aux, bigNum, bigDenom;
  2584. uint64_t returnLimit;
  2585. int i;
  2586. uint64_t nMax;
  2587. double fMax;
  2588. unsigned long maxDenom;
  2589. /*-- nMax and fMax defines the initial accuracy of the starting fractional,
  2590. * or better, the highest used integer numbers used within the starting fractional (bigNum/bigDenom).
  2591. * There are two approaches, which can accidentally lead to different accuracies just depending on the value.
  2592. * Therefore, blnUseSmallRange steers this behavior.
  2593. * For long long nMax = ((9223372036854775807-1)/2); for long nMax = ((2147483647-1)/2);
  2594. */
  2595. if (blnUseSmallRange) {
  2596. nMax = (uint64_t)((2147483647 - 1) / 2); /* for ULONG range */
  2597. }
  2598. else {
  2599. nMax = ((9223372036854775807 - 1) / 2); /* for ULLONG range */
  2600. }
  2601. fMax = (double)nMax;
  2602. /*-- For the Euclidean GCD define the denominator range, so that it stays within size of unsigned long variables.
  2603. * maxDenom should be LONG_MAX for negative values and ULONG_MAX for positive ones.
  2604. * Also the final returned value of ullNum and ullDenom is limited according to signed- or unsigned-range.
  2605. */
  2606. if (blnUseSignedRange) {
  2607. maxDenom = 2147483647UL; /*LONG_MAX = 0x7FFFFFFFUL*/
  2608. returnLimit = maxDenom;
  2609. }
  2610. else {
  2611. maxDenom = 0xFFFFFFFFUL; /*ULONG_MAX = 0xFFFFFFFFUL*/
  2612. returnLimit = maxDenom;
  2613. }
  2614. /*-- First generate a rational fraction (bigNum/bigDenom) which represents the value
  2615. * as a rational number with the highest accuracy. Therefore, uint64_t (uint64_t) is needed.
  2616. * This rational fraction is then reduced using the Euclidean algorithm to find the greatest common divisor (GCD).
  2617. * bigNum = big numinator of value without fraction (or cut residual fraction)
  2618. * bigDenom = big denominator of value
  2619. *-- Break-criteria so that uint64_t cast to "bigNum" introduces no error and bigDenom has no overflow,
  2620. * and stop with enlargement of fraction when the double-value of it reaches an integer number without fractional part.
  2621. */
  2622. bigDenom = 1;
  2623. while ((value != floor(value)) && (value < fMax) && (bigDenom < nMax)) {
  2624. bigDenom <<= 1;
  2625. value *= 2;
  2626. }
  2627. bigNum = (uint64_t)value;
  2628. /*-- Start Euclidean algorithm to find the greatest common divisor (GCD) -- */
  2629. #define MAX_ITERATIONS 64
  2630. for (i = 0; i < MAX_ITERATIONS; i++) {
  2631. uint64_t val;
  2632. /* if bigDenom is not zero, calculate integer part of fraction. */
  2633. if (bigDenom == 0) {
  2634. break;
  2635. }
  2636. val = bigNum / bigDenom;
  2637. /* Set bigDenom to reminder of bigNum/bigDenom and bigNum to previous denominator bigDenom. */
  2638. aux = bigNum;
  2639. bigNum = bigDenom;
  2640. bigDenom = aux % bigDenom;
  2641. /* calculate next denominator and check for its given maximum */
  2642. aux = val;
  2643. if (denomSum[1] * val + denomSum[0] >= maxDenom) {
  2644. aux = (maxDenom - denomSum[0]) / denomSum[1];
  2645. if (aux * 2 >= val || denomSum[1] >= maxDenom)
  2646. i = (MAX_ITERATIONS + 1); /* exit but execute rest of for-loop */
  2647. else
  2648. break;
  2649. }
  2650. /* calculate next numerator to numSum2 and save previous one to numSum0; numSum1 just copy of numSum2. */
  2651. numSum[2] = aux * numSum[1] + numSum[0];
  2652. numSum[0] = numSum[1];
  2653. numSum[1] = numSum[2];
  2654. /* calculate next denominator to denomSum2 and save previous one to denomSum0; denomSum1 just copy of denomSum2. */
  2655. denomSum[2] = aux * denomSum[1] + denomSum[0];
  2656. denomSum[0] = denomSum[1];
  2657. denomSum[1] = denomSum[2];
  2658. }
  2659. /*-- Check and adapt for final variable size and return values; reduces internal accuracy; denominator is kept in ULONG-range with maxDenom -- */
  2660. while (numSum[1] > returnLimit || denomSum[1] > returnLimit) {
  2661. numSum[1] = numSum[1] / 2;
  2662. denomSum[1] = denomSum[1] / 2;
  2663. }
  2664. /* return values */
  2665. *ullNum = numSum[1];
  2666. *ullDenom = denomSum[1];
  2667. } /*-- ToRationalEuclideanGCD() -------------- */
  2668. /**---- DoubleToRational() -----------------------------------------------
  2669. * Calculates the rational fractional of a double input value
  2670. * for UN-SIGNED rationals,
  2671. * using the Euclidean algorithm to find the greatest common divisor (GCD)
  2672. ------------------------------------------------------------------------*/
  2673. static
  2674. void DoubleToRational(double value, uint32_t *num, uint32_t *denom)
  2675. {
  2676. /*---- UN-SIGNED RATIONAL ---- */
  2677. double dblDiff, dblDiff2;
  2678. uint64_t ullNum, ullDenom, ullNum2, ullDenom2;
  2679. /*-- Check for negative values. If so it is an error. */
  2680. /* Test written that way to catch NaN */
  2681. if (!(value >= 0)) {
  2682. *num = *denom = 0;
  2683. TIFFErrorExt(0, "TIFFLib: DoubleToRational()", " Negative Value for Unsigned Rational given.");
  2684. return;
  2685. }
  2686. /*-- Check for too big numbers (> ULONG_MAX) -- */
  2687. if (value > 0xFFFFFFFFUL) {
  2688. *num = 0xFFFFFFFFU;
  2689. *denom = 0;
  2690. return;
  2691. }
  2692. /*-- Check for easy integer numbers -- */
  2693. if (value == (uint32_t)(value)) {
  2694. *num = (uint32_t)value;
  2695. *denom = 1;
  2696. return;
  2697. }
  2698. /*-- Check for too small numbers for "unsigned long" type rationals -- */
  2699. if (value < 1.0 / (double)0xFFFFFFFFUL) {
  2700. *num = 0;
  2701. *denom = 0xFFFFFFFFU;
  2702. return;
  2703. }
  2704. /*-- There are two approaches using the Euclidean algorithm,
  2705. * which can accidentally lead to different accuracies just depending on the value.
  2706. * Try both and define which one was better.
  2707. */
  2708. ToRationalEuclideanGCD(value, FALSE, FALSE, &ullNum, &ullDenom);
  2709. ToRationalEuclideanGCD(value, FALSE, TRUE, &ullNum2, &ullDenom2);
  2710. /*-- Double-Check, that returned values fit into ULONG :*/
  2711. if (ullNum > 0xFFFFFFFFUL || ullDenom > 0xFFFFFFFFUL || ullNum2 > 0xFFFFFFFFUL || ullDenom2 > 0xFFFFFFFFUL) {
  2712. TIFFErrorExt(0, "TIFFLib: DoubleToRational()", " Num or Denom exceeds ULONG: val=%14.6f, num=%12"PRIu64", denom=%12"PRIu64" | num2=%12"PRIu64", denom2=%12"PRIu64"", value, ullNum, ullDenom, ullNum2, ullDenom2);
  2713. assert(0);
  2714. }
  2715. /* Check, which one has higher accuracy and take that. */
  2716. dblDiff = fabs(value - ((double)ullNum / (double)ullDenom));
  2717. dblDiff2 = fabs(value - ((double)ullNum2 / (double)ullDenom2));
  2718. if (dblDiff < dblDiff2) {
  2719. *num = (uint32_t)ullNum;
  2720. *denom = (uint32_t)ullDenom;
  2721. }
  2722. else {
  2723. *num = (uint32_t)ullNum2;
  2724. *denom = (uint32_t)ullDenom2;
  2725. }
  2726. } /*-- DoubleToRational() -------------- */
  2727. /**---- DoubleToSrational() -----------------------------------------------
  2728. * Calculates the rational fractional of a double input value
  2729. * for SIGNED rationals,
  2730. * using the Euclidean algorithm to find the greatest common divisor (GCD)
  2731. ------------------------------------------------------------------------*/
  2732. static
  2733. void DoubleToSrational(double value, int32_t *num, int32_t *denom)
  2734. {
  2735. /*---- SIGNED RATIONAL ----*/
  2736. int neg = 1;
  2737. double dblDiff, dblDiff2;
  2738. uint64_t ullNum, ullDenom, ullNum2, ullDenom2;
  2739. /*-- Check for negative values and use then the positive one for internal calculations, but take the sign into account before returning. */
  2740. if (value < 0) { neg = -1; value = -value; }
  2741. /*-- Check for too big numbers (> LONG_MAX) -- */
  2742. if (value > 0x7FFFFFFFL) {
  2743. *num = 0x7FFFFFFFL;
  2744. *denom = 0;
  2745. return;
  2746. }
  2747. /*-- Check for easy numbers -- */
  2748. if (value == (int32_t)(value)) {
  2749. *num = (int32_t)(neg * value);
  2750. *denom = 1;
  2751. return;
  2752. }
  2753. /*-- Check for too small numbers for "long" type rationals -- */
  2754. if (value < 1.0 / (double)0x7FFFFFFFL) {
  2755. *num = 0;
  2756. *denom = 0x7FFFFFFFL;
  2757. return;
  2758. }
  2759. /*-- There are two approaches using the Euclidean algorithm,
  2760. * which can accidentally lead to different accuracies just depending on the value.
  2761. * Try both and define which one was better.
  2762. * Furthermore, set behavior of ToRationalEuclideanGCD() to the range of signed-long.
  2763. */
  2764. ToRationalEuclideanGCD(value, TRUE, FALSE, &ullNum, &ullDenom);
  2765. ToRationalEuclideanGCD(value, TRUE, TRUE, &ullNum2, &ullDenom2);
  2766. /*-- Double-Check, that returned values fit into LONG :*/
  2767. if (ullNum > 0x7FFFFFFFL || ullDenom > 0x7FFFFFFFL || ullNum2 > 0x7FFFFFFFL || ullDenom2 > 0x7FFFFFFFL) {
  2768. TIFFErrorExt(0, "TIFFLib: DoubleToSrational()", " Num or Denom exceeds LONG: val=%14.6f, num=%12"PRIu64", denom=%12"PRIu64" | num2=%12"PRIu64", denom2=%12"PRIu64"", neg*value, ullNum, ullDenom, ullNum2, ullDenom2);
  2769. assert(0);
  2770. }
  2771. /* Check, which one has higher accuracy and take that. */
  2772. dblDiff = fabs(value - ((double)ullNum / (double)ullDenom));
  2773. dblDiff2 = fabs(value - ((double)ullNum2 / (double)ullDenom2));
  2774. if (dblDiff < dblDiff2) {
  2775. *num = (int32_t)(neg * (long)ullNum);
  2776. *denom = (int32_t)ullDenom;
  2777. }
  2778. else {
  2779. *num = (int32_t)(neg * (long)ullNum2);
  2780. *denom = (int32_t)ullDenom2;
  2781. }
  2782. } /*-- DoubleToSrational() --------------*/
  2783. #ifdef notdef
  2784. static int
  2785. TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, float value)
  2786. {
  2787. float m;
  2788. assert(sizeof(float)==4);
  2789. m=value;
  2790. TIFFCvtNativeToIEEEFloat(tif,1,&m);
  2791. if (tif->tif_flags&TIFF_SWAB)
  2792. TIFFSwabFloat(&m);
  2793. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,1,4,&m));
  2794. }
  2795. #endif
  2796. static int
  2797. TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, float* value)
  2798. {
  2799. assert(count<0x40000000);
  2800. assert(sizeof(float)==4);
  2801. TIFFCvtNativeToIEEEFloat(tif,count,&value);
  2802. if (tif->tif_flags&TIFF_SWAB)
  2803. TIFFSwabArrayOfFloat(value,count);
  2804. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,count,count*4,value));
  2805. }
  2806. #ifdef notdef
  2807. static int
  2808. TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, double value)
  2809. {
  2810. double m;
  2811. assert(sizeof(double)==8);
  2812. m=value;
  2813. TIFFCvtNativeToIEEEDouble(tif,1,&m);
  2814. if (tif->tif_flags&TIFF_SWAB)
  2815. TIFFSwabDouble(&m);
  2816. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,1,8,&m));
  2817. }
  2818. #endif
  2819. static int
  2820. TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, double* value)
  2821. {
  2822. assert(count<0x20000000);
  2823. assert(sizeof(double)==8);
  2824. TIFFCvtNativeToIEEEDouble(tif,count,&value);
  2825. if (tif->tif_flags&TIFF_SWAB)
  2826. TIFFSwabArrayOfDouble(value,count);
  2827. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,count,count*8,value));
  2828. }
  2829. static int
  2830. TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint32_t* value)
  2831. {
  2832. assert(count<0x40000000);
  2833. assert(sizeof(uint32_t) == 4);
  2834. if (tif->tif_flags&TIFF_SWAB)
  2835. TIFFSwabArrayOfLong(value,count);
  2836. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD,count,count*4,value));
  2837. }
  2838. static int
  2839. TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint32_t count, uint64_t* value)
  2840. {
  2841. assert(count<0x20000000);
  2842. assert(sizeof(uint64_t) == 8);
  2843. assert(tif->tif_flags&TIFF_BIGTIFF);
  2844. if (tif->tif_flags&TIFF_SWAB)
  2845. TIFFSwabArrayOfLong8(value,count);
  2846. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD8,count,count*8,value));
  2847. }
  2848. static int
  2849. TIFFWriteDirectoryTagData(TIFF* tif, uint32_t* ndir, TIFFDirEntry* dir, uint16_t tag, uint16_t datatype, uint32_t count, uint32_t datalength, void* data)
  2850. {
  2851. static const char module[] = "TIFFWriteDirectoryTagData";
  2852. uint32_t m;
  2853. m=0;
  2854. while (m<(*ndir))
  2855. {
  2856. assert(dir[m].tdir_tag!=tag);
  2857. if (dir[m].tdir_tag>tag)
  2858. break;
  2859. m++;
  2860. }
  2861. if (m<(*ndir))
  2862. {
  2863. uint32_t n;
  2864. for (n=*ndir; n>m; n--)
  2865. dir[n]=dir[n-1];
  2866. }
  2867. dir[m].tdir_tag=tag;
  2868. dir[m].tdir_type=datatype;
  2869. dir[m].tdir_count=count;
  2870. dir[m].tdir_offset.toff_long8 = 0;
  2871. if (datalength<=((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U))
  2872. {
  2873. if( data && datalength )
  2874. {
  2875. _TIFFmemcpy(&dir[m].tdir_offset,data,datalength);
  2876. }
  2877. }
  2878. else
  2879. {
  2880. uint64_t na,nb;
  2881. na=tif->tif_dataoff;
  2882. nb=na+datalength;
  2883. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2884. nb=(uint32_t)nb;
  2885. if ((nb<na)||(nb<datalength))
  2886. {
  2887. TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
  2888. return(0);
  2889. }
  2890. if (!SeekOK(tif,na))
  2891. {
  2892. TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
  2893. return(0);
  2894. }
  2895. assert(datalength<0x80000000UL);
  2896. if (!WriteOK(tif,data,(tmsize_t)datalength))
  2897. {
  2898. TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
  2899. return(0);
  2900. }
  2901. tif->tif_dataoff=nb;
  2902. if (tif->tif_dataoff&1)
  2903. tif->tif_dataoff++;
  2904. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2905. {
  2906. uint32_t o;
  2907. o=(uint32_t)na;
  2908. if (tif->tif_flags&TIFF_SWAB)
  2909. TIFFSwabLong(&o);
  2910. _TIFFmemcpy(&dir[m].tdir_offset,&o,4);
  2911. }
  2912. else
  2913. {
  2914. dir[m].tdir_offset.toff_long8 = na;
  2915. if (tif->tif_flags&TIFF_SWAB)
  2916. TIFFSwabLong8(&dir[m].tdir_offset.toff_long8);
  2917. }
  2918. }
  2919. (*ndir)++;
  2920. return(1);
  2921. }
  2922. /*
  2923. * Link the current directory into the directory chain for the file.
  2924. */
  2925. static int
  2926. TIFFLinkDirectory(TIFF* tif)
  2927. {
  2928. static const char module[] = "TIFFLinkDirectory";
  2929. tif->tif_diroff = (TIFFSeekFile(tif,0,SEEK_END)+1) & (~((toff_t)1));
  2930. /*
  2931. * Handle SubIFDs
  2932. */
  2933. if (tif->tif_flags & TIFF_INSUBIFD)
  2934. {
  2935. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2936. {
  2937. uint32_t m;
  2938. m = (uint32_t)tif->tif_diroff;
  2939. if (tif->tif_flags & TIFF_SWAB)
  2940. TIFFSwabLong(&m);
  2941. (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
  2942. if (!WriteOK(tif, &m, 4)) {
  2943. TIFFErrorExt(tif->tif_clientdata, module,
  2944. "Error writing SubIFD directory link");
  2945. return (0);
  2946. }
  2947. /*
  2948. * Advance to the next SubIFD or, if this is
  2949. * the last one configured, revert back to the
  2950. * normal directory linkage.
  2951. */
  2952. if (--tif->tif_nsubifd)
  2953. tif->tif_subifdoff += 4;
  2954. else
  2955. tif->tif_flags &= ~TIFF_INSUBIFD;
  2956. return (1);
  2957. }
  2958. else
  2959. {
  2960. uint64_t m;
  2961. m = tif->tif_diroff;
  2962. if (tif->tif_flags & TIFF_SWAB)
  2963. TIFFSwabLong8(&m);
  2964. (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
  2965. if (!WriteOK(tif, &m, 8)) {
  2966. TIFFErrorExt(tif->tif_clientdata, module,
  2967. "Error writing SubIFD directory link");
  2968. return (0);
  2969. }
  2970. /*
  2971. * Advance to the next SubIFD or, if this is
  2972. * the last one configured, revert back to the
  2973. * normal directory linkage.
  2974. */
  2975. if (--tif->tif_nsubifd)
  2976. tif->tif_subifdoff += 8;
  2977. else
  2978. tif->tif_flags &= ~TIFF_INSUBIFD;
  2979. return (1);
  2980. }
  2981. }
  2982. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2983. {
  2984. uint32_t m;
  2985. uint32_t nextdir;
  2986. m = (uint32_t)(tif->tif_diroff);
  2987. if (tif->tif_flags & TIFF_SWAB)
  2988. TIFFSwabLong(&m);
  2989. if (tif->tif_header.classic.tiff_diroff == 0) {
  2990. /*
  2991. * First directory, overwrite offset in header.
  2992. */
  2993. tif->tif_header.classic.tiff_diroff = (uint32_t) tif->tif_diroff;
  2994. (void) TIFFSeekFile(tif,4, SEEK_SET);
  2995. if (!WriteOK(tif, &m, 4)) {
  2996. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  2997. "Error writing TIFF header");
  2998. return (0);
  2999. }
  3000. return (1);
  3001. }
  3002. /*
  3003. * Not the first directory, search to the last and append.
  3004. */
  3005. nextdir = tif->tif_header.classic.tiff_diroff;
  3006. while(1) {
  3007. uint16_t dircount;
  3008. uint32_t nextnextdir;
  3009. if (!SeekOK(tif, nextdir) ||
  3010. !ReadOK(tif, &dircount, 2)) {
  3011. TIFFErrorExt(tif->tif_clientdata, module,
  3012. "Error fetching directory count");
  3013. return (0);
  3014. }
  3015. if (tif->tif_flags & TIFF_SWAB)
  3016. TIFFSwabShort(&dircount);
  3017. (void) TIFFSeekFile(tif,
  3018. nextdir+2+dircount*12, SEEK_SET);
  3019. if (!ReadOK(tif, &nextnextdir, 4)) {
  3020. TIFFErrorExt(tif->tif_clientdata, module,
  3021. "Error fetching directory link");
  3022. return (0);
  3023. }
  3024. if (tif->tif_flags & TIFF_SWAB)
  3025. TIFFSwabLong(&nextnextdir);
  3026. if (nextnextdir==0)
  3027. {
  3028. (void) TIFFSeekFile(tif,
  3029. nextdir+2+dircount*12, SEEK_SET);
  3030. if (!WriteOK(tif, &m, 4)) {
  3031. TIFFErrorExt(tif->tif_clientdata, module,
  3032. "Error writing directory link");
  3033. return (0);
  3034. }
  3035. break;
  3036. }
  3037. nextdir=nextnextdir;
  3038. }
  3039. }
  3040. else
  3041. {
  3042. uint64_t m;
  3043. uint64_t nextdir;
  3044. m = tif->tif_diroff;
  3045. if (tif->tif_flags & TIFF_SWAB)
  3046. TIFFSwabLong8(&m);
  3047. if (tif->tif_header.big.tiff_diroff == 0) {
  3048. /*
  3049. * First directory, overwrite offset in header.
  3050. */
  3051. tif->tif_header.big.tiff_diroff = tif->tif_diroff;
  3052. (void) TIFFSeekFile(tif,8, SEEK_SET);
  3053. if (!WriteOK(tif, &m, 8)) {
  3054. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  3055. "Error writing TIFF header");
  3056. return (0);
  3057. }
  3058. return (1);
  3059. }
  3060. /*
  3061. * Not the first directory, search to the last and append.
  3062. */
  3063. nextdir = tif->tif_header.big.tiff_diroff;
  3064. while(1) {
  3065. uint64_t dircount64;
  3066. uint16_t dircount;
  3067. uint64_t nextnextdir;
  3068. if (!SeekOK(tif, nextdir) ||
  3069. !ReadOK(tif, &dircount64, 8)) {
  3070. TIFFErrorExt(tif->tif_clientdata, module,
  3071. "Error fetching directory count");
  3072. return (0);
  3073. }
  3074. if (tif->tif_flags & TIFF_SWAB)
  3075. TIFFSwabLong8(&dircount64);
  3076. if (dircount64>0xFFFF)
  3077. {
  3078. TIFFErrorExt(tif->tif_clientdata, module,
  3079. "Sanity check on tag count failed, likely corrupt TIFF");
  3080. return (0);
  3081. }
  3082. dircount=(uint16_t)dircount64;
  3083. (void) TIFFSeekFile(tif,
  3084. nextdir+8+dircount*20, SEEK_SET);
  3085. if (!ReadOK(tif, &nextnextdir, 8)) {
  3086. TIFFErrorExt(tif->tif_clientdata, module,
  3087. "Error fetching directory link");
  3088. return (0);
  3089. }
  3090. if (tif->tif_flags & TIFF_SWAB)
  3091. TIFFSwabLong8(&nextnextdir);
  3092. if (nextnextdir==0)
  3093. {
  3094. (void) TIFFSeekFile(tif,
  3095. nextdir+8+dircount*20, SEEK_SET);
  3096. if (!WriteOK(tif, &m, 8)) {
  3097. TIFFErrorExt(tif->tif_clientdata, module,
  3098. "Error writing directory link");
  3099. return (0);
  3100. }
  3101. break;
  3102. }
  3103. nextdir=nextnextdir;
  3104. }
  3105. }
  3106. return (1);
  3107. }
  3108. /************************************************************************/
  3109. /* TIFFRewriteField() */
  3110. /* */
  3111. /* Rewrite a field in the directory on disk without regard to */
  3112. /* updating the TIFF directory structure in memory. Currently */
  3113. /* only supported for field that already exist in the on-disk */
  3114. /* directory. Mainly used for updating stripoffset / */
  3115. /* stripbytecount values after the directory is already on */
  3116. /* disk. */
  3117. /* */
  3118. /* Returns zero on failure, and one on success. */
  3119. /************************************************************************/
  3120. int
  3121. _TIFFRewriteField(TIFF* tif, uint16_t tag, TIFFDataType in_datatype,
  3122. tmsize_t count, void* data)
  3123. {
  3124. static const char module[] = "TIFFResetField";
  3125. /* const TIFFField* fip = NULL; */
  3126. uint16_t dircount;
  3127. tmsize_t dirsize;
  3128. uint8_t direntry_raw[20];
  3129. uint16_t entry_tag = 0;
  3130. uint16_t entry_type = 0;
  3131. uint64_t entry_count = 0;
  3132. uint64_t entry_offset = 0;
  3133. int value_in_entry = 0;
  3134. uint64_t read_offset;
  3135. uint8_t *buf_to_write = NULL;
  3136. TIFFDataType datatype;
  3137. /* -------------------------------------------------------------------- */
  3138. /* Find field definition. */
  3139. /* -------------------------------------------------------------------- */
  3140. /*fip =*/ TIFFFindField(tif, tag, TIFF_ANY);
  3141. /* -------------------------------------------------------------------- */
  3142. /* Do some checking this is a straight forward case. */
  3143. /* -------------------------------------------------------------------- */
  3144. if( isMapped(tif) )
  3145. {
  3146. TIFFErrorExt( tif->tif_clientdata, module,
  3147. "Memory mapped files not currently supported for this operation." );
  3148. return 0;
  3149. }
  3150. if( tif->tif_diroff == 0 )
  3151. {
  3152. TIFFErrorExt( tif->tif_clientdata, module,
  3153. "Attempt to reset field on directory not already on disk." );
  3154. return 0;
  3155. }
  3156. /* -------------------------------------------------------------------- */
  3157. /* Read the directory entry count. */
  3158. /* -------------------------------------------------------------------- */
  3159. if (!SeekOK(tif, tif->tif_diroff)) {
  3160. TIFFErrorExt(tif->tif_clientdata, module,
  3161. "%s: Seek error accessing TIFF directory",
  3162. tif->tif_name);
  3163. return 0;
  3164. }
  3165. read_offset = tif->tif_diroff;
  3166. if (!(tif->tif_flags&TIFF_BIGTIFF))
  3167. {
  3168. if (!ReadOK(tif, &dircount, sizeof (uint16_t))) {
  3169. TIFFErrorExt(tif->tif_clientdata, module,
  3170. "%s: Can not read TIFF directory count",
  3171. tif->tif_name);
  3172. return 0;
  3173. }
  3174. if (tif->tif_flags & TIFF_SWAB)
  3175. TIFFSwabShort(&dircount);
  3176. dirsize = 12;
  3177. read_offset += 2;
  3178. } else {
  3179. uint64_t dircount64;
  3180. if (!ReadOK(tif, &dircount64, sizeof (uint64_t))) {
  3181. TIFFErrorExt(tif->tif_clientdata, module,
  3182. "%s: Can not read TIFF directory count",
  3183. tif->tif_name);
  3184. return 0;
  3185. }
  3186. if (tif->tif_flags & TIFF_SWAB)
  3187. TIFFSwabLong8(&dircount64);
  3188. dircount = (uint16_t)dircount64;
  3189. dirsize = 20;
  3190. read_offset += 8;
  3191. }
  3192. /* -------------------------------------------------------------------- */
  3193. /* Read through directory to find target tag. */
  3194. /* -------------------------------------------------------------------- */
  3195. while( dircount > 0 )
  3196. {
  3197. if (!ReadOK(tif, direntry_raw, dirsize)) {
  3198. TIFFErrorExt(tif->tif_clientdata, module,
  3199. "%s: Can not read TIFF directory entry.",
  3200. tif->tif_name);
  3201. return 0;
  3202. }
  3203. memcpy( &entry_tag, direntry_raw + 0, sizeof(uint16_t) );
  3204. if (tif->tif_flags&TIFF_SWAB)
  3205. TIFFSwabShort( &entry_tag );
  3206. if( entry_tag == tag )
  3207. break;
  3208. read_offset += dirsize;
  3209. }
  3210. if( entry_tag != tag )
  3211. {
  3212. TIFFErrorExt(tif->tif_clientdata, module,
  3213. "%s: Could not find tag %"PRIu16".",
  3214. tif->tif_name, tag );
  3215. return 0;
  3216. }
  3217. /* -------------------------------------------------------------------- */
  3218. /* Extract the type, count and offset for this entry. */
  3219. /* -------------------------------------------------------------------- */
  3220. memcpy( &entry_type, direntry_raw + 2, sizeof(uint16_t) );
  3221. if (tif->tif_flags&TIFF_SWAB)
  3222. TIFFSwabShort( &entry_type );
  3223. if (!(tif->tif_flags&TIFF_BIGTIFF))
  3224. {
  3225. uint32_t value;
  3226. memcpy( &value, direntry_raw + 4, sizeof(uint32_t) );
  3227. if (tif->tif_flags&TIFF_SWAB)
  3228. TIFFSwabLong( &value );
  3229. entry_count = value;
  3230. memcpy( &value, direntry_raw + 8, sizeof(uint32_t) );
  3231. if (tif->tif_flags&TIFF_SWAB)
  3232. TIFFSwabLong( &value );
  3233. entry_offset = value;
  3234. }
  3235. else
  3236. {
  3237. memcpy( &entry_count, direntry_raw + 4, sizeof(uint64_t) );
  3238. if (tif->tif_flags&TIFF_SWAB)
  3239. TIFFSwabLong8( &entry_count );
  3240. memcpy( &entry_offset, direntry_raw + 12, sizeof(uint64_t) );
  3241. if (tif->tif_flags&TIFF_SWAB)
  3242. TIFFSwabLong8( &entry_offset );
  3243. }
  3244. /* -------------------------------------------------------------------- */
  3245. /* When a dummy tag was written due to TIFFDeferStrileArrayWriting() */
  3246. /* -------------------------------------------------------------------- */
  3247. if( entry_offset == 0 && entry_count == 0 && entry_type == 0 )
  3248. {
  3249. if( tag == TIFFTAG_TILEOFFSETS || tag == TIFFTAG_STRIPOFFSETS )
  3250. {
  3251. entry_type = (tif->tif_flags&TIFF_BIGTIFF) ? TIFF_LONG8 : TIFF_LONG;
  3252. }
  3253. else
  3254. {
  3255. int write_aslong8 = 1;
  3256. if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
  3257. {
  3258. write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif));
  3259. }
  3260. else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
  3261. {
  3262. write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif));
  3263. }
  3264. if( write_aslong8 )
  3265. {
  3266. entry_type = TIFF_LONG8;
  3267. }
  3268. else
  3269. {
  3270. int write_aslong4 = 1;
  3271. if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
  3272. {
  3273. write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif));
  3274. }
  3275. else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
  3276. {
  3277. write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif));
  3278. }
  3279. if( write_aslong4 )
  3280. {
  3281. entry_type = TIFF_LONG;
  3282. }
  3283. else
  3284. {
  3285. entry_type = TIFF_SHORT;
  3286. }
  3287. }
  3288. }
  3289. }
  3290. /* -------------------------------------------------------------------- */
  3291. /* What data type do we want to write this as? */
  3292. /* -------------------------------------------------------------------- */
  3293. if( TIFFDataWidth(in_datatype) == 8 && !(tif->tif_flags&TIFF_BIGTIFF) )
  3294. {
  3295. if( in_datatype == TIFF_LONG8 )
  3296. datatype = entry_type == TIFF_SHORT ? TIFF_SHORT : TIFF_LONG;
  3297. else if( in_datatype == TIFF_SLONG8 )
  3298. datatype = TIFF_SLONG;
  3299. else if( in_datatype == TIFF_IFD8 )
  3300. datatype = TIFF_IFD;
  3301. else
  3302. datatype = in_datatype;
  3303. }
  3304. else
  3305. {
  3306. if( in_datatype == TIFF_LONG8 &&
  3307. (entry_type == TIFF_SHORT || entry_type == TIFF_LONG ||
  3308. entry_type == TIFF_LONG8 ) )
  3309. datatype = entry_type;
  3310. else if( in_datatype == TIFF_SLONG8 &&
  3311. (entry_type == TIFF_SLONG || entry_type == TIFF_SLONG8 ) )
  3312. datatype = entry_type;
  3313. else if( in_datatype == TIFF_IFD8 &&
  3314. (entry_type == TIFF_IFD || entry_type == TIFF_IFD8 ) )
  3315. datatype = entry_type;
  3316. else
  3317. datatype = in_datatype;
  3318. }
  3319. /* -------------------------------------------------------------------- */
  3320. /* Prepare buffer of actual data to write. This includes */
  3321. /* swabbing as needed. */
  3322. /* -------------------------------------------------------------------- */
  3323. buf_to_write =
  3324. (uint8_t *)_TIFFCheckMalloc(tif, count, TIFFDataWidth(datatype),
  3325. "for field buffer.");
  3326. if (!buf_to_write)
  3327. return 0;
  3328. if( datatype == in_datatype )
  3329. memcpy( buf_to_write, data, count * TIFFDataWidth(datatype) );
  3330. else if( datatype == TIFF_SLONG && in_datatype == TIFF_SLONG8 )
  3331. {
  3332. tmsize_t i;
  3333. for( i = 0; i < count; i++ )
  3334. {
  3335. ((int32_t *) buf_to_write)[i] =
  3336. (int32_t) ((int64_t *) data)[i];
  3337. if((int64_t) ((int32_t *) buf_to_write)[i] != ((int64_t *) data)[i] )
  3338. {
  3339. _TIFFfree( buf_to_write );
  3340. TIFFErrorExt( tif->tif_clientdata, module,
  3341. "Value exceeds 32bit range of output type." );
  3342. return 0;
  3343. }
  3344. }
  3345. }
  3346. else if( (datatype == TIFF_LONG && in_datatype == TIFF_LONG8)
  3347. || (datatype == TIFF_IFD && in_datatype == TIFF_IFD8) )
  3348. {
  3349. tmsize_t i;
  3350. for( i = 0; i < count; i++ )
  3351. {
  3352. ((uint32_t *) buf_to_write)[i] =
  3353. (uint32_t) ((uint64_t *) data)[i];
  3354. if((uint64_t) ((uint32_t *) buf_to_write)[i] != ((uint64_t *) data)[i] )
  3355. {
  3356. _TIFFfree( buf_to_write );
  3357. TIFFErrorExt( tif->tif_clientdata, module,
  3358. "Value exceeds 32bit range of output type." );
  3359. return 0;
  3360. }
  3361. }
  3362. }
  3363. else if( datatype == TIFF_SHORT && in_datatype == TIFF_LONG8 )
  3364. {
  3365. tmsize_t i;
  3366. for( i = 0; i < count; i++ )
  3367. {
  3368. ((uint16_t *) buf_to_write)[i] =
  3369. (uint16_t) ((uint64_t *) data)[i];
  3370. if((uint64_t) ((uint16_t *) buf_to_write)[i] != ((uint64_t *) data)[i] )
  3371. {
  3372. _TIFFfree( buf_to_write );
  3373. TIFFErrorExt( tif->tif_clientdata, module,
  3374. "Value exceeds 16bit range of output type." );
  3375. return 0;
  3376. }
  3377. }
  3378. }
  3379. else
  3380. {
  3381. TIFFErrorExt( tif->tif_clientdata, module,
  3382. "Unhandled type conversion." );
  3383. return 0;
  3384. }
  3385. if( TIFFDataWidth(datatype) > 1 && (tif->tif_flags&TIFF_SWAB) )
  3386. {
  3387. if( TIFFDataWidth(datatype) == 2 )
  3388. TIFFSwabArrayOfShort((uint16_t *) buf_to_write, count );
  3389. else if( TIFFDataWidth(datatype) == 4 )
  3390. TIFFSwabArrayOfLong((uint32_t *) buf_to_write, count );
  3391. else if( TIFFDataWidth(datatype) == 8 )
  3392. TIFFSwabArrayOfLong8((uint64_t *) buf_to_write, count );
  3393. }
  3394. /* -------------------------------------------------------------------- */
  3395. /* Is this a value that fits into the directory entry? */
  3396. /* -------------------------------------------------------------------- */
  3397. if (!(tif->tif_flags&TIFF_BIGTIFF))
  3398. {
  3399. if( TIFFDataWidth(datatype) * count <= 4 )
  3400. {
  3401. entry_offset = read_offset + 8;
  3402. value_in_entry = 1;
  3403. }
  3404. }
  3405. else
  3406. {
  3407. if( TIFFDataWidth(datatype) * count <= 8 )
  3408. {
  3409. entry_offset = read_offset + 12;
  3410. value_in_entry = 1;
  3411. }
  3412. }
  3413. if( (tag == TIFFTAG_TILEOFFSETS || tag == TIFFTAG_STRIPOFFSETS) &&
  3414. tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
  3415. tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
  3416. tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 )
  3417. {
  3418. tif->tif_dir.td_stripoffset_entry.tdir_type = datatype;
  3419. tif->tif_dir.td_stripoffset_entry.tdir_count = count;
  3420. }
  3421. else if( (tag == TIFFTAG_TILEBYTECOUNTS || tag == TIFFTAG_STRIPBYTECOUNTS) &&
  3422. tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
  3423. tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
  3424. tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0 )
  3425. {
  3426. tif->tif_dir.td_stripbytecount_entry.tdir_type = datatype;
  3427. tif->tif_dir.td_stripbytecount_entry.tdir_count = count;
  3428. }
  3429. /* -------------------------------------------------------------------- */
  3430. /* If the tag type, and count match, then we just write it out */
  3431. /* over the old values without altering the directory entry at */
  3432. /* all. */
  3433. /* -------------------------------------------------------------------- */
  3434. if( entry_count == (uint64_t)count && entry_type == (uint16_t) datatype )
  3435. {
  3436. if (!SeekOK(tif, entry_offset)) {
  3437. _TIFFfree( buf_to_write );
  3438. TIFFErrorExt(tif->tif_clientdata, module,
  3439. "%s: Seek error accessing TIFF directory",
  3440. tif->tif_name);
  3441. return 0;
  3442. }
  3443. if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
  3444. _TIFFfree( buf_to_write );
  3445. TIFFErrorExt(tif->tif_clientdata, module,
  3446. "Error writing directory link");
  3447. return (0);
  3448. }
  3449. _TIFFfree( buf_to_write );
  3450. return 1;
  3451. }
  3452. /* -------------------------------------------------------------------- */
  3453. /* Otherwise, we write the new tag data at the end of the file. */
  3454. /* -------------------------------------------------------------------- */
  3455. if( !value_in_entry )
  3456. {
  3457. entry_offset = TIFFSeekFile(tif,0,SEEK_END);
  3458. if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
  3459. _TIFFfree( buf_to_write );
  3460. TIFFErrorExt(tif->tif_clientdata, module,
  3461. "Error writing directory link");
  3462. return (0);
  3463. }
  3464. }
  3465. else
  3466. {
  3467. memcpy( &entry_offset, buf_to_write, count*TIFFDataWidth(datatype));
  3468. }
  3469. _TIFFfree( buf_to_write );
  3470. buf_to_write = 0;
  3471. /* -------------------------------------------------------------------- */
  3472. /* Adjust the directory entry. */
  3473. /* -------------------------------------------------------------------- */
  3474. entry_type = datatype;
  3475. entry_count = (uint64_t)count;
  3476. memcpy( direntry_raw + 2, &entry_type, sizeof(uint16_t) );
  3477. if (tif->tif_flags&TIFF_SWAB)
  3478. TIFFSwabShort( (uint16_t *) (direntry_raw + 2) );
  3479. if (!(tif->tif_flags&TIFF_BIGTIFF))
  3480. {
  3481. uint32_t value;
  3482. value = (uint32_t) entry_count;
  3483. memcpy( direntry_raw + 4, &value, sizeof(uint32_t) );
  3484. if (tif->tif_flags&TIFF_SWAB)
  3485. TIFFSwabLong( (uint32_t *) (direntry_raw + 4) );
  3486. value = (uint32_t) entry_offset;
  3487. memcpy( direntry_raw + 8, &value, sizeof(uint32_t) );
  3488. if (tif->tif_flags&TIFF_SWAB)
  3489. TIFFSwabLong( (uint32_t *) (direntry_raw + 8) );
  3490. }
  3491. else
  3492. {
  3493. memcpy( direntry_raw + 4, &entry_count, sizeof(uint64_t) );
  3494. if (tif->tif_flags&TIFF_SWAB)
  3495. TIFFSwabLong8( (uint64_t *) (direntry_raw + 4) );
  3496. memcpy( direntry_raw + 12, &entry_offset, sizeof(uint64_t) );
  3497. if (tif->tif_flags&TIFF_SWAB)
  3498. TIFFSwabLong8( (uint64_t *) (direntry_raw + 12) );
  3499. }
  3500. /* -------------------------------------------------------------------- */
  3501. /* Write the directory entry out to disk. */
  3502. /* -------------------------------------------------------------------- */
  3503. if (!SeekOK(tif, read_offset )) {
  3504. TIFFErrorExt(tif->tif_clientdata, module,
  3505. "%s: Seek error accessing TIFF directory",
  3506. tif->tif_name);
  3507. return 0;
  3508. }
  3509. if (!WriteOK(tif, direntry_raw,dirsize))
  3510. {
  3511. TIFFErrorExt(tif->tif_clientdata, module,
  3512. "%s: Can not write TIFF directory entry.",
  3513. tif->tif_name);
  3514. return 0;
  3515. }
  3516. return 1;
  3517. }
  3518. /* vim: set ts=8 sts=8 sw=8 noet: */
  3519. /*
  3520. * Local Variables:
  3521. * mode: c
  3522. * c-basic-offset: 8
  3523. * fill-column: 78
  3524. * End:
  3525. */