tif_getimage.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535
  1. /*
  2. * Copyright (c) 1991-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. * Read and return a packed RGBA image.
  28. */
  29. #include "tiffiop.h"
  30. #include <limits.h>
  31. #include <stdio.h>
  32. static int gtTileContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
  33. static int gtTileSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
  34. static int gtStripContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
  35. static int gtStripSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);
  36. static int PickContigCase(TIFFRGBAImage *);
  37. static int PickSeparateCase(TIFFRGBAImage *);
  38. static int BuildMapUaToAa(TIFFRGBAImage *img);
  39. static int BuildMapBitdepth16To8(TIFFRGBAImage *img);
  40. static const char photoTag[] = "PhotometricInterpretation";
  41. /*
  42. * Helper constants used in Orientation tag handling
  43. */
  44. #define FLIP_VERTICALLY 0x01
  45. #define FLIP_HORIZONTALLY 0x02
  46. #define EMSG_BUF_SIZE 1024
  47. /*
  48. * Color conversion constants. We will define display types here.
  49. */
  50. static const TIFFDisplay display_sRGB = {
  51. {/* XYZ -> luminance matrix */
  52. {3.2410F, -1.5374F, -0.4986F},
  53. {-0.9692F, 1.8760F, 0.0416F},
  54. {0.0556F, -0.2040F, 1.0570F}},
  55. 100.0F,
  56. 100.0F,
  57. 100.0F, /* Light o/p for reference white */
  58. 255,
  59. 255,
  60. 255, /* Pixel values for ref. white */
  61. 1.0F,
  62. 1.0F,
  63. 1.0F, /* Residual light o/p for black pixel */
  64. 2.4F,
  65. 2.4F,
  66. 2.4F, /* Gamma values for the three guns */
  67. };
  68. /*
  69. * Check the image to see if TIFFReadRGBAImage can deal with it.
  70. * 1/0 is returned according to whether or not the image can
  71. * be handled. If 0 is returned, emsg contains the reason
  72. * why it is being rejected.
  73. */
  74. int TIFFRGBAImageOK(TIFF *tif, char emsg[EMSG_BUF_SIZE])
  75. {
  76. TIFFDirectory *td = &tif->tif_dir;
  77. uint16_t photometric;
  78. int colorchannels;
  79. if (!tif->tif_decodestatus)
  80. {
  81. snprintf(emsg, EMSG_BUF_SIZE,
  82. "Sorry, requested compression method is not configured");
  83. return (0);
  84. }
  85. switch (td->td_bitspersample)
  86. {
  87. case 1:
  88. case 2:
  89. case 4:
  90. case 8:
  91. case 16:
  92. break;
  93. default:
  94. snprintf(emsg, EMSG_BUF_SIZE,
  95. "Sorry, can not handle images with %" PRIu16
  96. "-bit samples",
  97. td->td_bitspersample);
  98. return (0);
  99. }
  100. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP)
  101. {
  102. snprintf(
  103. emsg, EMSG_BUF_SIZE,
  104. "Sorry, can not handle images with IEEE floating-point samples");
  105. return (0);
  106. }
  107. colorchannels = td->td_samplesperpixel - td->td_extrasamples;
  108. if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric))
  109. {
  110. switch (colorchannels)
  111. {
  112. case 1:
  113. photometric = PHOTOMETRIC_MINISBLACK;
  114. break;
  115. case 3:
  116. photometric = PHOTOMETRIC_RGB;
  117. break;
  118. default:
  119. snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag",
  120. photoTag);
  121. return (0);
  122. }
  123. }
  124. switch (photometric)
  125. {
  126. case PHOTOMETRIC_MINISWHITE:
  127. case PHOTOMETRIC_MINISBLACK:
  128. case PHOTOMETRIC_PALETTE:
  129. if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
  130. td->td_samplesperpixel != 1 && td->td_bitspersample < 8)
  131. {
  132. snprintf(
  133. emsg, EMSG_BUF_SIZE,
  134. "Sorry, can not handle contiguous data with %s=%" PRIu16
  135. ", "
  136. "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16 "",
  137. photoTag, photometric, "Samples/pixel",
  138. td->td_samplesperpixel, td->td_bitspersample);
  139. return (0);
  140. }
  141. /*
  142. * We should likely validate that any extra samples are either
  143. * to be ignored, or are alpha, and if alpha we should try to use
  144. * them. But for now we won't bother with this.
  145. */
  146. break;
  147. case PHOTOMETRIC_YCBCR:
  148. /*
  149. * TODO: if at all meaningful and useful, make more complete
  150. * support check here, or better still, refactor to let supporting
  151. * code decide whether there is support and what meaningful
  152. * error to return
  153. */
  154. break;
  155. case PHOTOMETRIC_RGB:
  156. if (colorchannels < 3)
  157. {
  158. snprintf(emsg, EMSG_BUF_SIZE,
  159. "Sorry, can not handle RGB image with %s=%d",
  160. "Color channels", colorchannels);
  161. return (0);
  162. }
  163. break;
  164. case PHOTOMETRIC_SEPARATED:
  165. {
  166. uint16_t inkset;
  167. TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
  168. if (inkset != INKSET_CMYK)
  169. {
  170. snprintf(emsg, EMSG_BUF_SIZE,
  171. "Sorry, can not handle separated image with %s=%d",
  172. "InkSet", inkset);
  173. return 0;
  174. }
  175. if (td->td_samplesperpixel < 4)
  176. {
  177. snprintf(
  178. emsg, EMSG_BUF_SIZE,
  179. "Sorry, can not handle separated image with %s=%" PRIu16,
  180. "Samples/pixel", td->td_samplesperpixel);
  181. return 0;
  182. }
  183. break;
  184. }
  185. case PHOTOMETRIC_LOGL:
  186. if (td->td_compression != COMPRESSION_SGILOG)
  187. {
  188. snprintf(emsg, EMSG_BUF_SIZE,
  189. "Sorry, LogL data must have %s=%d", "Compression",
  190. COMPRESSION_SGILOG);
  191. return (0);
  192. }
  193. break;
  194. case PHOTOMETRIC_LOGLUV:
  195. if (td->td_compression != COMPRESSION_SGILOG &&
  196. td->td_compression != COMPRESSION_SGILOG24)
  197. {
  198. snprintf(emsg, EMSG_BUF_SIZE,
  199. "Sorry, LogLuv data must have %s=%d or %d",
  200. "Compression", COMPRESSION_SGILOG,
  201. COMPRESSION_SGILOG24);
  202. return (0);
  203. }
  204. if (td->td_planarconfig != PLANARCONFIG_CONTIG)
  205. {
  206. snprintf(emsg, EMSG_BUF_SIZE,
  207. "Sorry, can not handle LogLuv images with %s=%" PRIu16,
  208. "Planarconfiguration", td->td_planarconfig);
  209. return (0);
  210. }
  211. if (td->td_samplesperpixel != 3 || colorchannels != 3)
  212. {
  213. snprintf(emsg, EMSG_BUF_SIZE,
  214. "Sorry, can not handle image with %s=%" PRIu16
  215. ", %s=%d",
  216. "Samples/pixel", td->td_samplesperpixel,
  217. "colorchannels", colorchannels);
  218. return 0;
  219. }
  220. break;
  221. case PHOTOMETRIC_CIELAB:
  222. if (td->td_samplesperpixel != 3 || colorchannels != 3 ||
  223. (td->td_bitspersample != 8 && td->td_bitspersample != 16))
  224. {
  225. snprintf(emsg, EMSG_BUF_SIZE,
  226. "Sorry, can not handle image with %s=%" PRIu16
  227. ", %s=%d and %s=%" PRIu16,
  228. "Samples/pixel", td->td_samplesperpixel,
  229. "colorchannels", colorchannels, "Bits/sample",
  230. td->td_bitspersample);
  231. return 0;
  232. }
  233. break;
  234. default:
  235. snprintf(emsg, EMSG_BUF_SIZE,
  236. "Sorry, can not handle image with %s=%" PRIu16, photoTag,
  237. photometric);
  238. return (0);
  239. }
  240. return (1);
  241. }
  242. void TIFFRGBAImageEnd(TIFFRGBAImage *img)
  243. {
  244. if (img->Map)
  245. {
  246. _TIFFfreeExt(img->tif, img->Map);
  247. img->Map = NULL;
  248. }
  249. if (img->BWmap)
  250. {
  251. _TIFFfreeExt(img->tif, img->BWmap);
  252. img->BWmap = NULL;
  253. }
  254. if (img->PALmap)
  255. {
  256. _TIFFfreeExt(img->tif, img->PALmap);
  257. img->PALmap = NULL;
  258. }
  259. if (img->ycbcr)
  260. {
  261. _TIFFfreeExt(img->tif, img->ycbcr);
  262. img->ycbcr = NULL;
  263. }
  264. if (img->cielab)
  265. {
  266. _TIFFfreeExt(img->tif, img->cielab);
  267. img->cielab = NULL;
  268. }
  269. if (img->UaToAa)
  270. {
  271. _TIFFfreeExt(img->tif, img->UaToAa);
  272. img->UaToAa = NULL;
  273. }
  274. if (img->Bitdepth16To8)
  275. {
  276. _TIFFfreeExt(img->tif, img->Bitdepth16To8);
  277. img->Bitdepth16To8 = NULL;
  278. }
  279. if (img->redcmap)
  280. {
  281. _TIFFfreeExt(img->tif, img->redcmap);
  282. _TIFFfreeExt(img->tif, img->greencmap);
  283. _TIFFfreeExt(img->tif, img->bluecmap);
  284. img->redcmap = img->greencmap = img->bluecmap = NULL;
  285. }
  286. }
  287. static int isCCITTCompression(TIFF *tif)
  288. {
  289. uint16_t compress;
  290. TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
  291. return (compress == COMPRESSION_CCITTFAX3 ||
  292. compress == COMPRESSION_CCITTFAX4 ||
  293. compress == COMPRESSION_CCITTRLE ||
  294. compress == COMPRESSION_CCITTRLEW);
  295. }
  296. int TIFFRGBAImageBegin(TIFFRGBAImage *img, TIFF *tif, int stop,
  297. char emsg[EMSG_BUF_SIZE])
  298. {
  299. uint16_t *sampleinfo;
  300. uint16_t extrasamples;
  301. uint16_t planarconfig;
  302. uint16_t compress;
  303. int colorchannels;
  304. uint16_t *red_orig, *green_orig, *blue_orig;
  305. int n_color;
  306. if (!TIFFRGBAImageOK(tif, emsg))
  307. return 0;
  308. /* Initialize to normal values */
  309. img->row_offset = 0;
  310. img->col_offset = 0;
  311. img->redcmap = NULL;
  312. img->greencmap = NULL;
  313. img->bluecmap = NULL;
  314. img->Map = NULL;
  315. img->BWmap = NULL;
  316. img->PALmap = NULL;
  317. img->ycbcr = NULL;
  318. img->cielab = NULL;
  319. img->UaToAa = NULL;
  320. img->Bitdepth16To8 = NULL;
  321. img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
  322. img->tif = tif;
  323. img->stoponerr = stop;
  324. TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
  325. switch (img->bitspersample)
  326. {
  327. case 1:
  328. case 2:
  329. case 4:
  330. case 8:
  331. case 16:
  332. break;
  333. default:
  334. snprintf(emsg, EMSG_BUF_SIZE,
  335. "Sorry, can not handle images with %" PRIu16
  336. "-bit samples",
  337. img->bitspersample);
  338. goto fail_return;
  339. }
  340. img->alpha = 0;
  341. TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
  342. TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples,
  343. &sampleinfo);
  344. if (extrasamples >= 1)
  345. {
  346. switch (sampleinfo[0])
  347. {
  348. case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without
  349. */
  350. if (img->samplesperpixel >
  351. 3) /* correct info about alpha channel */
  352. img->alpha = EXTRASAMPLE_ASSOCALPHA;
  353. break;
  354. case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
  355. case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
  356. img->alpha = sampleinfo[0];
  357. break;
  358. }
  359. }
  360. #ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
  361. if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
  362. img->photometric = PHOTOMETRIC_MINISWHITE;
  363. if (extrasamples == 0 && img->samplesperpixel == 4 &&
  364. img->photometric == PHOTOMETRIC_RGB)
  365. {
  366. img->alpha = EXTRASAMPLE_ASSOCALPHA;
  367. extrasamples = 1;
  368. }
  369. #endif
  370. colorchannels = img->samplesperpixel - extrasamples;
  371. TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
  372. TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
  373. if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
  374. {
  375. switch (colorchannels)
  376. {
  377. case 1:
  378. if (isCCITTCompression(tif))
  379. img->photometric = PHOTOMETRIC_MINISWHITE;
  380. else
  381. img->photometric = PHOTOMETRIC_MINISBLACK;
  382. break;
  383. case 3:
  384. img->photometric = PHOTOMETRIC_RGB;
  385. break;
  386. default:
  387. snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag",
  388. photoTag);
  389. goto fail_return;
  390. }
  391. }
  392. switch (img->photometric)
  393. {
  394. case PHOTOMETRIC_PALETTE:
  395. if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &red_orig, &green_orig,
  396. &blue_orig))
  397. {
  398. snprintf(emsg, EMSG_BUF_SIZE,
  399. "Missing required \"Colormap\" tag");
  400. goto fail_return;
  401. }
  402. /* copy the colormaps so we can modify them */
  403. n_color = (1U << img->bitspersample);
  404. img->redcmap =
  405. (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color);
  406. img->greencmap =
  407. (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color);
  408. img->bluecmap =
  409. (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color);
  410. if (!img->redcmap || !img->greencmap || !img->bluecmap)
  411. {
  412. snprintf(emsg, EMSG_BUF_SIZE,
  413. "Out of memory for colormap copy");
  414. goto fail_return;
  415. }
  416. _TIFFmemcpy(img->redcmap, red_orig, n_color * 2);
  417. _TIFFmemcpy(img->greencmap, green_orig, n_color * 2);
  418. _TIFFmemcpy(img->bluecmap, blue_orig, n_color * 2);
  419. /* fall through... */
  420. case PHOTOMETRIC_MINISWHITE:
  421. case PHOTOMETRIC_MINISBLACK:
  422. if (planarconfig == PLANARCONFIG_CONTIG &&
  423. img->samplesperpixel != 1 && img->bitspersample < 8)
  424. {
  425. snprintf(
  426. emsg, EMSG_BUF_SIZE,
  427. "Sorry, can not handle contiguous data with %s=%" PRIu16
  428. ", "
  429. "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16,
  430. photoTag, img->photometric, "Samples/pixel",
  431. img->samplesperpixel, img->bitspersample);
  432. goto fail_return;
  433. }
  434. break;
  435. case PHOTOMETRIC_YCBCR:
  436. /* It would probably be nice to have a reality check here. */
  437. if (planarconfig == PLANARCONFIG_CONTIG)
  438. /* can rely on libjpeg to convert to RGB */
  439. /* XXX should restore current state on exit */
  440. switch (compress)
  441. {
  442. case COMPRESSION_JPEG:
  443. /*
  444. * TODO: when complete tests verify complete
  445. * desubsampling and YCbCr handling, remove use of
  446. * TIFFTAG_JPEGCOLORMODE in favor of tif_getimage.c
  447. * native handling
  448. */
  449. TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE,
  450. JPEGCOLORMODE_RGB);
  451. img->photometric = PHOTOMETRIC_RGB;
  452. break;
  453. default:
  454. /* do nothing */;
  455. break;
  456. }
  457. /*
  458. * TODO: if at all meaningful and useful, make more complete
  459. * support check here, or better still, refactor to let supporting
  460. * code decide whether there is support and what meaningful
  461. * error to return
  462. */
  463. break;
  464. case PHOTOMETRIC_RGB:
  465. if (colorchannels < 3)
  466. {
  467. snprintf(emsg, EMSG_BUF_SIZE,
  468. "Sorry, can not handle RGB image with %s=%d",
  469. "Color channels", colorchannels);
  470. goto fail_return;
  471. }
  472. break;
  473. case PHOTOMETRIC_SEPARATED:
  474. {
  475. uint16_t inkset;
  476. TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
  477. if (inkset != INKSET_CMYK)
  478. {
  479. snprintf(
  480. emsg, EMSG_BUF_SIZE,
  481. "Sorry, can not handle separated image with %s=%" PRIu16,
  482. "InkSet", inkset);
  483. goto fail_return;
  484. }
  485. if (img->samplesperpixel < 4)
  486. {
  487. snprintf(
  488. emsg, EMSG_BUF_SIZE,
  489. "Sorry, can not handle separated image with %s=%" PRIu16,
  490. "Samples/pixel", img->samplesperpixel);
  491. goto fail_return;
  492. }
  493. }
  494. break;
  495. case PHOTOMETRIC_LOGL:
  496. if (compress != COMPRESSION_SGILOG)
  497. {
  498. snprintf(emsg, EMSG_BUF_SIZE,
  499. "Sorry, LogL data must have %s=%d", "Compression",
  500. COMPRESSION_SGILOG);
  501. goto fail_return;
  502. }
  503. TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
  504. img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
  505. img->bitspersample = 8;
  506. break;
  507. case PHOTOMETRIC_LOGLUV:
  508. if (compress != COMPRESSION_SGILOG &&
  509. compress != COMPRESSION_SGILOG24)
  510. {
  511. snprintf(emsg, EMSG_BUF_SIZE,
  512. "Sorry, LogLuv data must have %s=%d or %d",
  513. "Compression", COMPRESSION_SGILOG,
  514. COMPRESSION_SGILOG24);
  515. goto fail_return;
  516. }
  517. if (planarconfig != PLANARCONFIG_CONTIG)
  518. {
  519. snprintf(emsg, EMSG_BUF_SIZE,
  520. "Sorry, can not handle LogLuv images with %s=%" PRIu16,
  521. "Planarconfiguration", planarconfig);
  522. return (0);
  523. }
  524. TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
  525. img->photometric = PHOTOMETRIC_RGB; /* little white lie */
  526. img->bitspersample = 8;
  527. break;
  528. case PHOTOMETRIC_CIELAB:
  529. break;
  530. default:
  531. snprintf(emsg, EMSG_BUF_SIZE,
  532. "Sorry, can not handle image with %s=%" PRIu16, photoTag,
  533. img->photometric);
  534. goto fail_return;
  535. }
  536. TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
  537. TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
  538. TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
  539. img->isContig =
  540. !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
  541. if (img->isContig)
  542. {
  543. if (!PickContigCase(img))
  544. {
  545. snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");
  546. goto fail_return;
  547. }
  548. }
  549. else
  550. {
  551. if (!PickSeparateCase(img))
  552. {
  553. snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");
  554. goto fail_return;
  555. }
  556. }
  557. return 1;
  558. fail_return:
  559. TIFFRGBAImageEnd(img);
  560. return 0;
  561. }
  562. int TIFFRGBAImageGet(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
  563. uint32_t h)
  564. {
  565. if (img->get == NULL)
  566. {
  567. TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
  568. "No \"get\" routine setup");
  569. return (0);
  570. }
  571. if (img->put.any == NULL)
  572. {
  573. TIFFErrorExtR(
  574. img->tif, TIFFFileName(img->tif),
  575. "No \"put\" routine setupl; probably can not handle image format");
  576. return (0);
  577. }
  578. /* Verify raster height against image height.
  579. * Width is checked in img->get() function individually. */
  580. if (0 <= img->row_offset && (uint32_t)img->row_offset < img->height)
  581. {
  582. uint32_t hx = img->height - img->row_offset;
  583. if (h > hx)
  584. {
  585. /* Adapt parameters to read only available lines and put image
  586. * at the bottom of the raster. */
  587. raster += (size_t)(h - hx) * w;
  588. h = hx;
  589. }
  590. }
  591. else
  592. {
  593. TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
  594. "Error in TIFFRGBAImageGet: row offset %d exceeds "
  595. "image height %d",
  596. img->row_offset, img->height);
  597. return 0;
  598. }
  599. return (*img->get)(img, raster, w, h);
  600. }
  601. /*
  602. * Read the specified image into an ABGR-format rastertaking in account
  603. * specified orientation.
  604. */
  605. int TIFFReadRGBAImageOriented(TIFF *tif, uint32_t rwidth, uint32_t rheight,
  606. uint32_t *raster, int orientation, int stop)
  607. {
  608. char emsg[EMSG_BUF_SIZE] = "";
  609. TIFFRGBAImage img;
  610. int ok;
  611. if (TIFFRGBAImageBegin(&img, tif, stop, emsg))
  612. {
  613. img.req_orientation = (uint16_t)orientation;
  614. ok = TIFFRGBAImageGet(&img, raster, rwidth, rheight);
  615. TIFFRGBAImageEnd(&img);
  616. }
  617. else
  618. {
  619. TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
  620. ok = 0;
  621. }
  622. return (ok);
  623. }
  624. /*
  625. * Read the specified image into an ABGR-format raster. Use bottom left
  626. * origin for raster by default.
  627. */
  628. int TIFFReadRGBAImage(TIFF *tif, uint32_t rwidth, uint32_t rheight,
  629. uint32_t *raster, int stop)
  630. {
  631. return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
  632. ORIENTATION_BOTLEFT, stop);
  633. }
  634. static int setorientation(TIFFRGBAImage *img)
  635. {
  636. switch (img->orientation)
  637. {
  638. case ORIENTATION_TOPLEFT:
  639. case ORIENTATION_LEFTTOP:
  640. if (img->req_orientation == ORIENTATION_TOPRIGHT ||
  641. img->req_orientation == ORIENTATION_RIGHTTOP)
  642. return FLIP_HORIZONTALLY;
  643. else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
  644. img->req_orientation == ORIENTATION_RIGHTBOT)
  645. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  646. else if (img->req_orientation == ORIENTATION_BOTLEFT ||
  647. img->req_orientation == ORIENTATION_LEFTBOT)
  648. return FLIP_VERTICALLY;
  649. else
  650. return 0;
  651. case ORIENTATION_TOPRIGHT:
  652. case ORIENTATION_RIGHTTOP:
  653. if (img->req_orientation == ORIENTATION_TOPLEFT ||
  654. img->req_orientation == ORIENTATION_LEFTTOP)
  655. return FLIP_HORIZONTALLY;
  656. else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
  657. img->req_orientation == ORIENTATION_RIGHTBOT)
  658. return FLIP_VERTICALLY;
  659. else if (img->req_orientation == ORIENTATION_BOTLEFT ||
  660. img->req_orientation == ORIENTATION_LEFTBOT)
  661. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  662. else
  663. return 0;
  664. case ORIENTATION_BOTRIGHT:
  665. case ORIENTATION_RIGHTBOT:
  666. if (img->req_orientation == ORIENTATION_TOPLEFT ||
  667. img->req_orientation == ORIENTATION_LEFTTOP)
  668. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  669. else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
  670. img->req_orientation == ORIENTATION_RIGHTTOP)
  671. return FLIP_VERTICALLY;
  672. else if (img->req_orientation == ORIENTATION_BOTLEFT ||
  673. img->req_orientation == ORIENTATION_LEFTBOT)
  674. return FLIP_HORIZONTALLY;
  675. else
  676. return 0;
  677. case ORIENTATION_BOTLEFT:
  678. case ORIENTATION_LEFTBOT:
  679. if (img->req_orientation == ORIENTATION_TOPLEFT ||
  680. img->req_orientation == ORIENTATION_LEFTTOP)
  681. return FLIP_VERTICALLY;
  682. else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
  683. img->req_orientation == ORIENTATION_RIGHTTOP)
  684. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  685. else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
  686. img->req_orientation == ORIENTATION_RIGHTBOT)
  687. return FLIP_HORIZONTALLY;
  688. else
  689. return 0;
  690. default: /* NOTREACHED */
  691. return 0;
  692. }
  693. }
  694. /*
  695. * Get an tile-organized image that has
  696. * PlanarConfiguration contiguous if SamplesPerPixel > 1
  697. * or
  698. * SamplesPerPixel == 1
  699. */
  700. static int gtTileContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
  701. uint32_t h)
  702. {
  703. TIFF *tif = img->tif;
  704. tileContigRoutine put = img->put.contig;
  705. uint32_t col, row, y, rowstoread;
  706. tmsize_t pos;
  707. uint32_t tw, th;
  708. unsigned char *buf = NULL;
  709. int32_t fromskew, toskew;
  710. uint32_t nrow;
  711. int ret = 1, flip;
  712. uint32_t this_tw, tocol;
  713. int32_t this_toskew, leftmost_toskew;
  714. int32_t leftmost_fromskew;
  715. uint32_t leftmost_tw;
  716. tmsize_t bufsize;
  717. /* If the raster is smaller than the image,
  718. * or if there is a col_offset, adapt the samples to be copied per row. */
  719. uint32_t wmin;
  720. if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width)
  721. {
  722. wmin = TIFFmin(w, img->width - img->col_offset);
  723. }
  724. else
  725. {
  726. TIFFErrorExtR(tif, TIFFFileName(tif),
  727. "Error in gtTileContig: column offset %d exceeds "
  728. "image width %d",
  729. img->col_offset, img->width);
  730. return 0;
  731. }
  732. bufsize = TIFFTileSize(tif);
  733. if (bufsize == 0)
  734. {
  735. TIFFErrorExtR(tif, TIFFFileName(tif), "%s", "No space for tile buffer");
  736. return (0);
  737. }
  738. TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  739. TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  740. flip = setorientation(img);
  741. if (flip & FLIP_VERTICALLY)
  742. {
  743. if (((int64_t)tw + w) > INT_MAX)
  744. {
  745. TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
  746. "unsupported tile size (too wide)");
  747. return (0);
  748. }
  749. y = h - 1;
  750. toskew = -(int32_t)(tw + w);
  751. }
  752. else
  753. {
  754. if (tw > ((int64_t)INT_MAX + w))
  755. {
  756. TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
  757. "unsupported tile size (too wide)");
  758. return (0);
  759. }
  760. y = 0;
  761. toskew = -(int32_t)(tw - w);
  762. }
  763. if (tw == 0 || th == 0)
  764. {
  765. TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");
  766. return (0);
  767. }
  768. /*
  769. * Leftmost tile is clipped on left side if col_offset > 0.
  770. */
  771. leftmost_fromskew = img->col_offset % tw;
  772. leftmost_tw = tw - leftmost_fromskew;
  773. int64_t skew_i64 = (int64_t)toskew + leftmost_fromskew;
  774. if (skew_i64 > INT_MAX || skew_i64 < INT_MIN)
  775. {
  776. TIFFErrorExtR(tif, TIFFFileName(tif), "%s %" PRId64, "Invalid skew",
  777. skew_i64);
  778. return (0);
  779. }
  780. leftmost_toskew = (int32_t)skew_i64;
  781. for (row = 0; ret != 0 && row < h; row += nrow)
  782. {
  783. rowstoread = th - (row + img->row_offset) % th;
  784. nrow = (row + rowstoread > h ? h - row : rowstoread);
  785. fromskew = leftmost_fromskew;
  786. this_tw = leftmost_tw;
  787. this_toskew = leftmost_toskew;
  788. tocol = 0;
  789. col = img->col_offset;
  790. /* wmin: only write imagewidth if raster is bigger. */
  791. while (tocol < wmin)
  792. {
  793. if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize, col,
  794. row + img->row_offset, 0,
  795. 0) == (tmsize_t)(-1) &&
  796. (buf == NULL || img->stoponerr))
  797. {
  798. ret = 0;
  799. break;
  800. }
  801. pos = ((row + img->row_offset) % th) * TIFFTileRowSize(tif) +
  802. ((tmsize_t)fromskew * img->samplesperpixel);
  803. if (tocol + this_tw > wmin)
  804. {
  805. /*
  806. * Rightmost tile is clipped on right side.
  807. */
  808. fromskew = tw - (wmin - tocol);
  809. this_tw = tw - fromskew;
  810. this_toskew = toskew + fromskew;
  811. }
  812. tmsize_t roffset = (tmsize_t)y * w + tocol;
  813. (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew,
  814. this_toskew, buf + pos);
  815. tocol += this_tw;
  816. col += this_tw;
  817. /*
  818. * After the leftmost tile, tiles are no longer clipped on left
  819. * side.
  820. */
  821. fromskew = 0;
  822. this_tw = tw;
  823. this_toskew = toskew;
  824. }
  825. y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);
  826. }
  827. _TIFFfreeExt(img->tif, buf);
  828. if (flip & FLIP_HORIZONTALLY)
  829. {
  830. uint32_t line;
  831. for (line = 0; line < h; line++)
  832. {
  833. uint32_t *left = raster + (line * w);
  834. /* Use wmin to only flip horizontally data in place and not complete
  835. * raster-row. */
  836. uint32_t *right = left + wmin - 1;
  837. while (left < right)
  838. {
  839. uint32_t temp = *left;
  840. *left = *right;
  841. *right = temp;
  842. left++;
  843. right--;
  844. }
  845. }
  846. }
  847. return (ret);
  848. }
  849. /*
  850. * Get an tile-organized image that has
  851. * SamplesPerPixel > 1
  852. * PlanarConfiguration separated
  853. * We assume that all such images are RGB.
  854. */
  855. static int gtTileSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
  856. uint32_t h)
  857. {
  858. TIFF *tif = img->tif;
  859. tileSeparateRoutine put = img->put.separate;
  860. uint32_t col, row, y, rowstoread;
  861. tmsize_t pos;
  862. uint32_t tw, th;
  863. unsigned char *buf = NULL;
  864. unsigned char *p0 = NULL;
  865. unsigned char *p1 = NULL;
  866. unsigned char *p2 = NULL;
  867. unsigned char *pa = NULL;
  868. tmsize_t tilesize;
  869. tmsize_t bufsize;
  870. int32_t fromskew, toskew;
  871. int alpha = img->alpha;
  872. uint32_t nrow;
  873. int ret = 1, flip;
  874. uint16_t colorchannels;
  875. uint32_t this_tw, tocol;
  876. int32_t this_toskew, leftmost_toskew;
  877. int32_t leftmost_fromskew;
  878. uint32_t leftmost_tw;
  879. /* If the raster is smaller than the image,
  880. * or if there is a col_offset, adapt the samples to be copied per row. */
  881. uint32_t wmin;
  882. if (0 <= img->col_offset && (uint32_t)img->col_offset < img->width)
  883. {
  884. wmin = TIFFmin(w, img->width - img->col_offset);
  885. }
  886. else
  887. {
  888. TIFFErrorExtR(tif, TIFFFileName(tif),
  889. "Error in gtTileSeparate: column offset %d exceeds "
  890. "image width %d",
  891. img->col_offset, img->width);
  892. return 0;
  893. }
  894. tilesize = TIFFTileSize(tif);
  895. bufsize =
  896. _TIFFMultiplySSize(tif, alpha ? 4 : 3, tilesize, "gtTileSeparate");
  897. if (bufsize == 0)
  898. {
  899. return (0);
  900. }
  901. TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  902. TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  903. flip = setorientation(img);
  904. if (flip & FLIP_VERTICALLY)
  905. {
  906. if (((int64_t)tw + w) > INT_MAX)
  907. {
  908. TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
  909. "unsupported tile size (too wide)");
  910. return (0);
  911. }
  912. y = h - 1;
  913. toskew = -(int32_t)(tw + w);
  914. }
  915. else
  916. {
  917. if (tw > ((int64_t)INT_MAX + w))
  918. {
  919. TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
  920. "unsupported tile size (too wide)");
  921. return (0);
  922. }
  923. y = 0;
  924. toskew = -(int32_t)(tw - w);
  925. }
  926. switch (img->photometric)
  927. {
  928. case PHOTOMETRIC_MINISWHITE:
  929. case PHOTOMETRIC_MINISBLACK:
  930. case PHOTOMETRIC_PALETTE:
  931. colorchannels = 1;
  932. break;
  933. default:
  934. colorchannels = 3;
  935. break;
  936. }
  937. if (tw == 0 || th == 0)
  938. {
  939. TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");
  940. return (0);
  941. }
  942. /*
  943. * Leftmost tile is clipped on left side if col_offset > 0.
  944. */
  945. leftmost_fromskew = img->col_offset % tw;
  946. leftmost_tw = tw - leftmost_fromskew;
  947. int64_t skew_i64 = (int64_t)toskew + leftmost_fromskew;
  948. if (skew_i64 > INT_MAX || skew_i64 < INT_MIN)
  949. {
  950. TIFFErrorExtR(tif, TIFFFileName(tif), "%s %" PRId64, "Invalid skew",
  951. skew_i64);
  952. return (0);
  953. }
  954. leftmost_toskew = (int32_t)skew_i64;
  955. for (row = 0; ret != 0 && row < h; row += nrow)
  956. {
  957. rowstoread = th - (row + img->row_offset) % th;
  958. nrow = (row + rowstoread > h ? h - row : rowstoread);
  959. fromskew = leftmost_fromskew;
  960. this_tw = leftmost_tw;
  961. this_toskew = leftmost_toskew;
  962. tocol = 0;
  963. col = img->col_offset;
  964. /* wmin: only write imagewidth if raster is bigger. */
  965. while (tocol < wmin)
  966. {
  967. if (buf == NULL)
  968. {
  969. if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize,
  970. col, row + img->row_offset, 0,
  971. 0) == (tmsize_t)(-1) &&
  972. (buf == NULL || img->stoponerr))
  973. {
  974. ret = 0;
  975. break;
  976. }
  977. p0 = buf;
  978. if (colorchannels == 1)
  979. {
  980. p2 = p1 = p0;
  981. pa = (alpha ? (p0 + 3 * tilesize) : NULL);
  982. }
  983. else
  984. {
  985. p1 = p0 + tilesize;
  986. p2 = p1 + tilesize;
  987. pa = (alpha ? (p2 + tilesize) : NULL);
  988. }
  989. }
  990. else if (TIFFReadTile(tif, p0, col, row + img->row_offset, 0, 0) ==
  991. (tmsize_t)(-1) &&
  992. img->stoponerr)
  993. {
  994. ret = 0;
  995. break;
  996. }
  997. if (colorchannels > 1 &&
  998. TIFFReadTile(tif, p1, col, row + img->row_offset, 0, 1) ==
  999. (tmsize_t)(-1) &&
  1000. img->stoponerr)
  1001. {
  1002. ret = 0;
  1003. break;
  1004. }
  1005. if (colorchannels > 1 &&
  1006. TIFFReadTile(tif, p2, col, row + img->row_offset, 0, 2) ==
  1007. (tmsize_t)(-1) &&
  1008. img->stoponerr)
  1009. {
  1010. ret = 0;
  1011. break;
  1012. }
  1013. if (alpha &&
  1014. TIFFReadTile(tif, pa, col, row + img->row_offset, 0,
  1015. colorchannels) == (tmsize_t)(-1) &&
  1016. img->stoponerr)
  1017. {
  1018. ret = 0;
  1019. break;
  1020. }
  1021. /* For SEPARATE the pos-offset is per sample and should not be
  1022. * multiplied by img->samplesperpixel. */
  1023. pos = ((row + img->row_offset) % th) * TIFFTileRowSize(tif) +
  1024. (tmsize_t)fromskew;
  1025. if (tocol + this_tw > wmin)
  1026. {
  1027. /*
  1028. * Rightmost tile is clipped on right side.
  1029. */
  1030. fromskew = tw - (wmin - tocol);
  1031. this_tw = tw - fromskew;
  1032. this_toskew = toskew + fromskew;
  1033. }
  1034. tmsize_t roffset = (tmsize_t)y * w + tocol;
  1035. (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew,
  1036. this_toskew, p0 + pos, p1 + pos, p2 + pos,
  1037. (alpha ? (pa + pos) : NULL));
  1038. tocol += this_tw;
  1039. col += this_tw;
  1040. /*
  1041. * After the leftmost tile, tiles are no longer clipped on left
  1042. * side.
  1043. */
  1044. fromskew = 0;
  1045. this_tw = tw;
  1046. this_toskew = toskew;
  1047. }
  1048. y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);
  1049. }
  1050. if (flip & FLIP_HORIZONTALLY)
  1051. {
  1052. uint32_t line;
  1053. for (line = 0; line < h; line++)
  1054. {
  1055. uint32_t *left = raster + (line * w);
  1056. /* Use wmin to only flip horizontally data in place and not complete
  1057. * raster-row. */
  1058. uint32_t *right = left + wmin - 1;
  1059. while (left < right)
  1060. {
  1061. uint32_t temp = *left;
  1062. *left = *right;
  1063. *right = temp;
  1064. left++;
  1065. right--;
  1066. }
  1067. }
  1068. }
  1069. _TIFFfreeExt(img->tif, buf);
  1070. return (ret);
  1071. }
  1072. /*
  1073. * Get a strip-organized image that has
  1074. * PlanarConfiguration contiguous if SamplesPerPixel > 1
  1075. * or
  1076. * SamplesPerPixel == 1
  1077. */
  1078. static int gtStripContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
  1079. uint32_t h)
  1080. {
  1081. TIFF *tif = img->tif;
  1082. tileContigRoutine put = img->put.contig;
  1083. uint32_t row, y, nrow, nrowsub, rowstoread;
  1084. tmsize_t pos;
  1085. unsigned char *buf = NULL;
  1086. uint32_t rowsperstrip;
  1087. uint16_t subsamplinghor, subsamplingver;
  1088. uint32_t imagewidth = img->width;
  1089. tmsize_t scanline;
  1090. /* fromskew, toskew are the increments within the input image or the raster
  1091. * from the end of a line to the start of the next line to read or write. */
  1092. int32_t fromskew, toskew;
  1093. int ret = 1, flip;
  1094. tmsize_t maxstripsize;
  1095. /* If the raster is smaller than the image,
  1096. * or if there is a col_offset, adapt the samples to be copied per row. */
  1097. uint32_t wmin;
  1098. if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth)
  1099. {
  1100. wmin = TIFFmin(w, imagewidth - img->col_offset);
  1101. }
  1102. else
  1103. {
  1104. TIFFErrorExtR(tif, TIFFFileName(tif),
  1105. "Error in gtStripContig: column offset %d exceeds "
  1106. "image width %d",
  1107. img->col_offset, imagewidth);
  1108. return 0;
  1109. }
  1110. TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor,
  1111. &subsamplingver);
  1112. if (subsamplingver == 0)
  1113. {
  1114. TIFFErrorExtR(tif, TIFFFileName(tif),
  1115. "Invalid vertical YCbCr subsampling");
  1116. return (0);
  1117. }
  1118. maxstripsize = TIFFStripSize(tif);
  1119. flip = setorientation(img);
  1120. if (flip & FLIP_VERTICALLY)
  1121. {
  1122. if (w > INT_MAX / 2)
  1123. {
  1124. TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow");
  1125. return (0);
  1126. }
  1127. y = h - 1;
  1128. /* Skew back to the raster row before the currently written row
  1129. * -> one raster width plus copied image pixels. */
  1130. toskew = -(int32_t)(w + wmin);
  1131. }
  1132. else
  1133. {
  1134. y = 0;
  1135. /* Skew forward to the end of the raster width of the row currently
  1136. * copied. */
  1137. toskew = w - wmin;
  1138. }
  1139. TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  1140. if (rowsperstrip == 0)
  1141. {
  1142. TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
  1143. return (0);
  1144. }
  1145. scanline = TIFFScanlineSize(tif);
  1146. fromskew = (w < imagewidth ? imagewidth - w : 0);
  1147. for (row = 0; row < h; row += nrow)
  1148. {
  1149. uint32_t temp;
  1150. rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
  1151. nrow = (row + rowstoread > h ? h - row : rowstoread);
  1152. nrowsub = nrow;
  1153. if ((nrowsub % subsamplingver) != 0)
  1154. nrowsub += subsamplingver - nrowsub % subsamplingver;
  1155. temp = (row + img->row_offset) % rowsperstrip + nrowsub;
  1156. if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline))
  1157. {
  1158. TIFFErrorExtR(tif, TIFFFileName(tif),
  1159. "Integer overflow in gtStripContig");
  1160. return 0;
  1161. }
  1162. if (_TIFFReadEncodedStripAndAllocBuffer(
  1163. tif, TIFFComputeStrip(tif, row + img->row_offset, 0),
  1164. (void **)(&buf), maxstripsize,
  1165. temp * scanline) == (tmsize_t)(-1) &&
  1166. (buf == NULL || img->stoponerr))
  1167. {
  1168. ret = 0;
  1169. break;
  1170. }
  1171. pos = ((row + img->row_offset) % rowsperstrip) * scanline +
  1172. ((tmsize_t)img->col_offset * img->samplesperpixel);
  1173. tmsize_t roffset = (tmsize_t)y * w;
  1174. (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew,
  1175. buf + pos);
  1176. y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);
  1177. }
  1178. if (flip & FLIP_HORIZONTALLY)
  1179. {
  1180. /* Flips the complete raster matrix horizontally. If raster width is
  1181. * larger than image width, data are moved horizontally to the right
  1182. * side.
  1183. * Use wmin to only flip data in place. */
  1184. uint32_t line;
  1185. for (line = 0; line < h; line++)
  1186. {
  1187. uint32_t *left = raster + (line * w);
  1188. /* Use wmin to only flip horizontally data in place and not complete
  1189. * raster-row. */
  1190. uint32_t *right = left + wmin - 1;
  1191. while (left < right)
  1192. {
  1193. uint32_t temp = *left;
  1194. *left = *right;
  1195. *right = temp;
  1196. left++;
  1197. right--;
  1198. }
  1199. }
  1200. }
  1201. _TIFFfreeExt(img->tif, buf);
  1202. return (ret);
  1203. }
  1204. /*
  1205. * Get a strip-organized image with
  1206. * SamplesPerPixel > 1
  1207. * PlanarConfiguration separated
  1208. * We assume that all such images are RGB.
  1209. */
  1210. static int gtStripSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
  1211. uint32_t h)
  1212. {
  1213. TIFF *tif = img->tif;
  1214. tileSeparateRoutine put = img->put.separate;
  1215. unsigned char *buf = NULL;
  1216. unsigned char *p0 = NULL, *p1 = NULL, *p2 = NULL, *pa = NULL;
  1217. uint32_t row, y, nrow, rowstoread;
  1218. tmsize_t pos;
  1219. tmsize_t scanline;
  1220. uint32_t rowsperstrip, offset_row;
  1221. uint32_t imagewidth = img->width;
  1222. tmsize_t stripsize;
  1223. tmsize_t bufsize;
  1224. int32_t fromskew, toskew;
  1225. int alpha = img->alpha;
  1226. int ret = 1, flip;
  1227. uint16_t colorchannels;
  1228. /* If the raster is smaller than the image,
  1229. * or if there is a col_offset, adapt the samples to be copied per row. */
  1230. uint32_t wmin;
  1231. if (0 <= img->col_offset && (uint32_t)img->col_offset < imagewidth)
  1232. {
  1233. wmin = TIFFmin(w, imagewidth - img->col_offset);
  1234. }
  1235. else
  1236. {
  1237. TIFFErrorExtR(tif, TIFFFileName(tif),
  1238. "Error in gtStripSeparate: column offset %d exceeds "
  1239. "image width %d",
  1240. img->col_offset, imagewidth);
  1241. return 0;
  1242. }
  1243. stripsize = TIFFStripSize(tif);
  1244. bufsize =
  1245. _TIFFMultiplySSize(tif, alpha ? 4 : 3, stripsize, "gtStripSeparate");
  1246. if (bufsize == 0)
  1247. {
  1248. return (0);
  1249. }
  1250. flip = setorientation(img);
  1251. if (flip & FLIP_VERTICALLY)
  1252. {
  1253. if (w > INT_MAX / 2)
  1254. {
  1255. TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow");
  1256. return (0);
  1257. }
  1258. y = h - 1;
  1259. /* Skew back to the raster row before the currently written row
  1260. * -> one raster width plus one image width. */
  1261. toskew = -(int32_t)(w + wmin);
  1262. }
  1263. else
  1264. {
  1265. y = 0;
  1266. /* Skew forward to the end of the raster width of the row currently
  1267. * written. */
  1268. toskew = w - wmin;
  1269. }
  1270. switch (img->photometric)
  1271. {
  1272. case PHOTOMETRIC_MINISWHITE:
  1273. case PHOTOMETRIC_MINISBLACK:
  1274. case PHOTOMETRIC_PALETTE:
  1275. colorchannels = 1;
  1276. break;
  1277. default:
  1278. colorchannels = 3;
  1279. break;
  1280. }
  1281. TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  1282. if (rowsperstrip == 0)
  1283. {
  1284. TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
  1285. return (0);
  1286. }
  1287. scanline = TIFFScanlineSize(tif);
  1288. fromskew = (w < imagewidth ? imagewidth - w : 0);
  1289. for (row = 0; row < h; row += nrow)
  1290. {
  1291. uint32_t temp;
  1292. rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
  1293. nrow = (row + rowstoread > h ? h - row : rowstoread);
  1294. offset_row = row + img->row_offset;
  1295. temp = (row + img->row_offset) % rowsperstrip + nrow;
  1296. if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline))
  1297. {
  1298. TIFFErrorExtR(tif, TIFFFileName(tif),
  1299. "Integer overflow in gtStripSeparate");
  1300. return 0;
  1301. }
  1302. if (buf == NULL)
  1303. {
  1304. if (_TIFFReadEncodedStripAndAllocBuffer(
  1305. tif, TIFFComputeStrip(tif, offset_row, 0), (void **)&buf,
  1306. bufsize, temp * scanline) == (tmsize_t)(-1) &&
  1307. (buf == NULL || img->stoponerr))
  1308. {
  1309. ret = 0;
  1310. break;
  1311. }
  1312. p0 = buf;
  1313. if (colorchannels == 1)
  1314. {
  1315. p2 = p1 = p0;
  1316. pa = (alpha ? (p0 + 3 * stripsize) : NULL);
  1317. }
  1318. else
  1319. {
  1320. p1 = p0 + stripsize;
  1321. p2 = p1 + stripsize;
  1322. pa = (alpha ? (p2 + stripsize) : NULL);
  1323. }
  1324. }
  1325. else if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
  1326. p0, temp * scanline) == (tmsize_t)(-1) &&
  1327. img->stoponerr)
  1328. {
  1329. ret = 0;
  1330. break;
  1331. }
  1332. if (colorchannels > 1 &&
  1333. TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1), p1,
  1334. temp * scanline) == (tmsize_t)(-1) &&
  1335. img->stoponerr)
  1336. {
  1337. ret = 0;
  1338. break;
  1339. }
  1340. if (colorchannels > 1 &&
  1341. TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2), p2,
  1342. temp * scanline) == (tmsize_t)(-1) &&
  1343. img->stoponerr)
  1344. {
  1345. ret = 0;
  1346. break;
  1347. }
  1348. if (alpha)
  1349. {
  1350. if (TIFFReadEncodedStrip(
  1351. tif, TIFFComputeStrip(tif, offset_row, colorchannels), pa,
  1352. temp * scanline) == (tmsize_t)(-1) &&
  1353. img->stoponerr)
  1354. {
  1355. ret = 0;
  1356. break;
  1357. }
  1358. }
  1359. /* For SEPARATE the pos-offset is per sample and should not be
  1360. * multiplied by img->samplesperpixel. */
  1361. pos = ((row + img->row_offset) % rowsperstrip) * scanline +
  1362. (tmsize_t)img->col_offset;
  1363. tmsize_t roffset = (tmsize_t)y * w;
  1364. (*put)(img, raster + roffset, 0, y, wmin, nrow, fromskew, toskew,
  1365. p0 + pos, p1 + pos, p2 + pos, (alpha ? (pa + pos) : NULL));
  1366. y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);
  1367. }
  1368. if (flip & FLIP_HORIZONTALLY)
  1369. {
  1370. uint32_t line;
  1371. for (line = 0; line < h; line++)
  1372. {
  1373. uint32_t *left = raster + (line * w);
  1374. /* Use wmin to only flip horizontally data in place and not complete
  1375. * raster-row. */
  1376. uint32_t *right = left + wmin - 1;
  1377. while (left < right)
  1378. {
  1379. uint32_t temp = *left;
  1380. *left = *right;
  1381. *right = temp;
  1382. left++;
  1383. right--;
  1384. }
  1385. }
  1386. }
  1387. _TIFFfreeExt(img->tif, buf);
  1388. return (ret);
  1389. }
  1390. /*
  1391. * The following routines move decoded data returned
  1392. * from the TIFF library into rasters filled with packed
  1393. * ABGR pixels (i.e. suitable for passing to lrecwrite.)
  1394. *
  1395. * The routines have been created according to the most
  1396. * important cases and optimized. PickContigCase and
  1397. * PickSeparateCase analyze the parameters and select
  1398. * the appropriate "get" and "put" routine to use.
  1399. */
  1400. #define REPEAT8(op) \
  1401. REPEAT4(op); \
  1402. REPEAT4(op)
  1403. #define REPEAT4(op) \
  1404. REPEAT2(op); \
  1405. REPEAT2(op)
  1406. #define REPEAT2(op) \
  1407. op; \
  1408. op
  1409. #define CASE8(x, op) \
  1410. switch (x) \
  1411. { \
  1412. case 7: \
  1413. op; /*-fallthrough*/ \
  1414. case 6: \
  1415. op; /*-fallthrough*/ \
  1416. case 5: \
  1417. op; /*-fallthrough*/ \
  1418. case 4: \
  1419. op; /*-fallthrough*/ \
  1420. case 3: \
  1421. op; /*-fallthrough*/ \
  1422. case 2: \
  1423. op; /*-fallthrough*/ \
  1424. case 1: \
  1425. op; \
  1426. }
  1427. #define CASE4(x, op) \
  1428. switch (x) \
  1429. { \
  1430. case 3: \
  1431. op; /*-fallthrough*/ \
  1432. case 2: \
  1433. op; /*-fallthrough*/ \
  1434. case 1: \
  1435. op; \
  1436. }
  1437. #define NOP
  1438. #define UNROLL8(w, op1, op2) \
  1439. { \
  1440. uint32_t _x; \
  1441. for (_x = w; _x >= 8; _x -= 8) \
  1442. { \
  1443. op1; \
  1444. REPEAT8(op2); \
  1445. } \
  1446. if (_x > 0) \
  1447. { \
  1448. op1; \
  1449. CASE8(_x, op2); \
  1450. } \
  1451. }
  1452. #define UNROLL4(w, op1, op2) \
  1453. { \
  1454. uint32_t _x; \
  1455. for (_x = w; _x >= 4; _x -= 4) \
  1456. { \
  1457. op1; \
  1458. REPEAT4(op2); \
  1459. } \
  1460. if (_x > 0) \
  1461. { \
  1462. op1; \
  1463. CASE4(_x, op2); \
  1464. } \
  1465. }
  1466. #define UNROLL2(w, op1, op2) \
  1467. { \
  1468. uint32_t _x; \
  1469. for (_x = w; _x >= 2; _x -= 2) \
  1470. { \
  1471. op1; \
  1472. REPEAT2(op2); \
  1473. } \
  1474. if (_x) \
  1475. { \
  1476. op1; \
  1477. op2; \
  1478. } \
  1479. }
  1480. #define SKEW(r, g, b, skew) \
  1481. { \
  1482. r += skew; \
  1483. g += skew; \
  1484. b += skew; \
  1485. }
  1486. #define SKEW4(r, g, b, a, skew) \
  1487. { \
  1488. r += skew; \
  1489. g += skew; \
  1490. b += skew; \
  1491. a += skew; \
  1492. }
  1493. #define A1 (((uint32_t)0xffL) << 24)
  1494. #define PACK(r, g, b) \
  1495. ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | A1)
  1496. #define PACK4(r, g, b, a) \
  1497. ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | \
  1498. ((uint32_t)(a) << 24))
  1499. #define W2B(v) (((v) >> 8) & 0xff)
  1500. /* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
  1501. #define PACKW(r, g, b) \
  1502. ((uint32_t)W2B(r) | ((uint32_t)W2B(g) << 8) | ((uint32_t)W2B(b) << 16) | A1)
  1503. #define PACKW4(r, g, b, a) \
  1504. ((uint32_t)W2B(r) | ((uint32_t)W2B(g) << 8) | ((uint32_t)W2B(b) << 16) | \
  1505. ((uint32_t)W2B(a) << 24))
  1506. #define DECLAREContigPutFunc(name) \
  1507. static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \
  1508. uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \
  1509. unsigned char *pp)
  1510. /*
  1511. * 8-bit palette => colormap/RGB
  1512. */
  1513. DECLAREContigPutFunc(put8bitcmaptile)
  1514. {
  1515. uint32_t **PALmap = img->PALmap;
  1516. int samplesperpixel = img->samplesperpixel;
  1517. (void)y;
  1518. for (; h > 0; --h)
  1519. {
  1520. for (x = w; x > 0; --x)
  1521. {
  1522. *cp++ = PALmap[*pp][0];
  1523. pp += samplesperpixel;
  1524. }
  1525. cp += toskew;
  1526. pp += fromskew;
  1527. }
  1528. }
  1529. /*
  1530. * 4-bit palette => colormap/RGB
  1531. */
  1532. DECLAREContigPutFunc(put4bitcmaptile)
  1533. {
  1534. uint32_t **PALmap = img->PALmap;
  1535. (void)x;
  1536. (void)y;
  1537. fromskew /= 2;
  1538. for (; h > 0; --h)
  1539. {
  1540. uint32_t *bw;
  1541. UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
  1542. cp += toskew;
  1543. pp += fromskew;
  1544. }
  1545. }
  1546. /*
  1547. * 2-bit palette => colormap/RGB
  1548. */
  1549. DECLAREContigPutFunc(put2bitcmaptile)
  1550. {
  1551. uint32_t **PALmap = img->PALmap;
  1552. (void)x;
  1553. (void)y;
  1554. fromskew /= 4;
  1555. for (; h > 0; --h)
  1556. {
  1557. uint32_t *bw;
  1558. UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
  1559. cp += toskew;
  1560. pp += fromskew;
  1561. }
  1562. }
  1563. /*
  1564. * 1-bit palette => colormap/RGB
  1565. */
  1566. DECLAREContigPutFunc(put1bitcmaptile)
  1567. {
  1568. uint32_t **PALmap = img->PALmap;
  1569. (void)x;
  1570. (void)y;
  1571. fromskew /= 8;
  1572. for (; h > 0; --h)
  1573. {
  1574. uint32_t *bw;
  1575. UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
  1576. cp += toskew;
  1577. pp += fromskew;
  1578. }
  1579. }
  1580. /*
  1581. * 8-bit greyscale => colormap/RGB
  1582. */
  1583. DECLAREContigPutFunc(putgreytile)
  1584. {
  1585. int samplesperpixel = img->samplesperpixel;
  1586. uint32_t **BWmap = img->BWmap;
  1587. (void)y;
  1588. for (; h > 0; --h)
  1589. {
  1590. for (x = w; x > 0; --x)
  1591. {
  1592. *cp++ = BWmap[*pp][0];
  1593. pp += samplesperpixel;
  1594. }
  1595. cp += toskew;
  1596. pp += fromskew;
  1597. }
  1598. }
  1599. /*
  1600. * 8-bit greyscale with associated alpha => colormap/RGBA
  1601. */
  1602. DECLAREContigPutFunc(putagreytile)
  1603. {
  1604. int samplesperpixel = img->samplesperpixel;
  1605. uint32_t **BWmap = img->BWmap;
  1606. (void)y;
  1607. for (; h > 0; --h)
  1608. {
  1609. for (x = w; x > 0; --x)
  1610. {
  1611. *cp++ = BWmap[*pp][0] & ((uint32_t) * (pp + 1) << 24 | ~A1);
  1612. pp += samplesperpixel;
  1613. }
  1614. cp += toskew;
  1615. pp += fromskew;
  1616. }
  1617. }
  1618. /*
  1619. * 16-bit greyscale => colormap/RGB
  1620. */
  1621. DECLAREContigPutFunc(put16bitbwtile)
  1622. {
  1623. int samplesperpixel = img->samplesperpixel;
  1624. uint32_t **BWmap = img->BWmap;
  1625. (void)y;
  1626. for (; h > 0; --h)
  1627. {
  1628. uint16_t *wp = (uint16_t *)pp;
  1629. for (x = w; x > 0; --x)
  1630. {
  1631. /* use high order byte of 16bit value */
  1632. *cp++ = BWmap[*wp >> 8][0];
  1633. pp += 2 * samplesperpixel;
  1634. wp += samplesperpixel;
  1635. }
  1636. cp += toskew;
  1637. pp += fromskew;
  1638. }
  1639. }
  1640. /*
  1641. * 1-bit bilevel => colormap/RGB
  1642. */
  1643. DECLAREContigPutFunc(put1bitbwtile)
  1644. {
  1645. uint32_t **BWmap = img->BWmap;
  1646. (void)x;
  1647. (void)y;
  1648. fromskew /= 8;
  1649. for (; h > 0; --h)
  1650. {
  1651. uint32_t *bw;
  1652. UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
  1653. cp += toskew;
  1654. pp += fromskew;
  1655. }
  1656. }
  1657. /*
  1658. * 2-bit greyscale => colormap/RGB
  1659. */
  1660. DECLAREContigPutFunc(put2bitbwtile)
  1661. {
  1662. uint32_t **BWmap = img->BWmap;
  1663. (void)x;
  1664. (void)y;
  1665. fromskew /= 4;
  1666. for (; h > 0; --h)
  1667. {
  1668. uint32_t *bw;
  1669. UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
  1670. cp += toskew;
  1671. pp += fromskew;
  1672. }
  1673. }
  1674. /*
  1675. * 4-bit greyscale => colormap/RGB
  1676. */
  1677. DECLAREContigPutFunc(put4bitbwtile)
  1678. {
  1679. uint32_t **BWmap = img->BWmap;
  1680. (void)x;
  1681. (void)y;
  1682. fromskew /= 2;
  1683. for (; h > 0; --h)
  1684. {
  1685. uint32_t *bw;
  1686. UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
  1687. cp += toskew;
  1688. pp += fromskew;
  1689. }
  1690. }
  1691. /*
  1692. * 8-bit packed samples, no Map => RGB
  1693. */
  1694. DECLAREContigPutFunc(putRGBcontig8bittile)
  1695. {
  1696. int samplesperpixel = img->samplesperpixel;
  1697. (void)x;
  1698. (void)y;
  1699. fromskew *= samplesperpixel;
  1700. for (; h > 0; --h)
  1701. {
  1702. UNROLL8(w, NOP, *cp++ = PACK(pp[0], pp[1], pp[2]);
  1703. pp += samplesperpixel);
  1704. cp += toskew;
  1705. pp += fromskew;
  1706. }
  1707. }
  1708. /*
  1709. * 8-bit packed samples => RGBA w/ associated alpha
  1710. * (known to have Map == NULL)
  1711. */
  1712. DECLAREContigPutFunc(putRGBAAcontig8bittile)
  1713. {
  1714. int samplesperpixel = img->samplesperpixel;
  1715. (void)x;
  1716. (void)y;
  1717. fromskew *= samplesperpixel;
  1718. for (; h > 0; --h)
  1719. {
  1720. UNROLL8(w, NOP, *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
  1721. pp += samplesperpixel);
  1722. cp += toskew;
  1723. pp += fromskew;
  1724. }
  1725. }
  1726. /*
  1727. * 8-bit packed samples => RGBA w/ unassociated alpha
  1728. * (known to have Map == NULL)
  1729. */
  1730. DECLAREContigPutFunc(putRGBUAcontig8bittile)
  1731. {
  1732. int samplesperpixel = img->samplesperpixel;
  1733. (void)y;
  1734. fromskew *= samplesperpixel;
  1735. for (; h > 0; --h)
  1736. {
  1737. uint32_t r, g, b, a;
  1738. uint8_t *m;
  1739. for (x = w; x > 0; --x)
  1740. {
  1741. a = pp[3];
  1742. m = img->UaToAa + ((size_t)a << 8);
  1743. r = m[pp[0]];
  1744. g = m[pp[1]];
  1745. b = m[pp[2]];
  1746. *cp++ = PACK4(r, g, b, a);
  1747. pp += samplesperpixel;
  1748. }
  1749. cp += toskew;
  1750. pp += fromskew;
  1751. }
  1752. }
  1753. /*
  1754. * 16-bit packed samples => RGB
  1755. */
  1756. DECLAREContigPutFunc(putRGBcontig16bittile)
  1757. {
  1758. int samplesperpixel = img->samplesperpixel;
  1759. uint16_t *wp = (uint16_t *)pp;
  1760. (void)y;
  1761. fromskew *= samplesperpixel;
  1762. for (; h > 0; --h)
  1763. {
  1764. for (x = w; x > 0; --x)
  1765. {
  1766. *cp++ = PACK(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],
  1767. img->Bitdepth16To8[wp[2]]);
  1768. wp += samplesperpixel;
  1769. }
  1770. cp += toskew;
  1771. wp += fromskew;
  1772. }
  1773. }
  1774. /*
  1775. * 16-bit packed samples => RGBA w/ associated alpha
  1776. * (known to have Map == NULL)
  1777. */
  1778. DECLAREContigPutFunc(putRGBAAcontig16bittile)
  1779. {
  1780. int samplesperpixel = img->samplesperpixel;
  1781. uint16_t *wp = (uint16_t *)pp;
  1782. (void)y;
  1783. fromskew *= samplesperpixel;
  1784. for (; h > 0; --h)
  1785. {
  1786. for (x = w; x > 0; --x)
  1787. {
  1788. *cp++ = PACK4(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],
  1789. img->Bitdepth16To8[wp[2]], img->Bitdepth16To8[wp[3]]);
  1790. wp += samplesperpixel;
  1791. }
  1792. cp += toskew;
  1793. wp += fromskew;
  1794. }
  1795. }
  1796. /*
  1797. * 16-bit packed samples => RGBA w/ unassociated alpha
  1798. * (known to have Map == NULL)
  1799. */
  1800. DECLAREContigPutFunc(putRGBUAcontig16bittile)
  1801. {
  1802. int samplesperpixel = img->samplesperpixel;
  1803. uint16_t *wp = (uint16_t *)pp;
  1804. (void)y;
  1805. fromskew *= samplesperpixel;
  1806. for (; h > 0; --h)
  1807. {
  1808. uint32_t r, g, b, a;
  1809. uint8_t *m;
  1810. for (x = w; x > 0; --x)
  1811. {
  1812. a = img->Bitdepth16To8[wp[3]];
  1813. m = img->UaToAa + ((size_t)a << 8);
  1814. r = m[img->Bitdepth16To8[wp[0]]];
  1815. g = m[img->Bitdepth16To8[wp[1]]];
  1816. b = m[img->Bitdepth16To8[wp[2]]];
  1817. *cp++ = PACK4(r, g, b, a);
  1818. wp += samplesperpixel;
  1819. }
  1820. cp += toskew;
  1821. wp += fromskew;
  1822. }
  1823. }
  1824. /*
  1825. * 8-bit packed CMYK samples w/o Map => RGB
  1826. *
  1827. * NB: The conversion of CMYK->RGB is *very* crude.
  1828. */
  1829. DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
  1830. {
  1831. int samplesperpixel = img->samplesperpixel;
  1832. uint16_t r, g, b, k;
  1833. (void)x;
  1834. (void)y;
  1835. fromskew *= samplesperpixel;
  1836. for (; h > 0; --h)
  1837. {
  1838. UNROLL8(w, NOP, k = 255 - pp[3]; r = (k * (255 - pp[0])) / 255;
  1839. g = (k * (255 - pp[1])) / 255; b = (k * (255 - pp[2])) / 255;
  1840. *cp++ = PACK(r, g, b); pp += samplesperpixel);
  1841. cp += toskew;
  1842. pp += fromskew;
  1843. }
  1844. }
  1845. /*
  1846. * 8-bit packed CMYK samples w/Map => RGB
  1847. *
  1848. * NB: The conversion of CMYK->RGB is *very* crude.
  1849. */
  1850. DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
  1851. {
  1852. int samplesperpixel = img->samplesperpixel;
  1853. TIFFRGBValue *Map = img->Map;
  1854. uint16_t r, g, b, k;
  1855. (void)y;
  1856. fromskew *= samplesperpixel;
  1857. for (; h > 0; --h)
  1858. {
  1859. for (x = w; x > 0; --x)
  1860. {
  1861. k = 255 - pp[3];
  1862. r = (k * (255 - pp[0])) / 255;
  1863. g = (k * (255 - pp[1])) / 255;
  1864. b = (k * (255 - pp[2])) / 255;
  1865. *cp++ = PACK(Map[r], Map[g], Map[b]);
  1866. pp += samplesperpixel;
  1867. }
  1868. pp += fromskew;
  1869. cp += toskew;
  1870. }
  1871. }
  1872. #define DECLARESepPutFunc(name) \
  1873. static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \
  1874. uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \
  1875. unsigned char *r, unsigned char *g, unsigned char *b, \
  1876. unsigned char *a)
  1877. /*
  1878. * 8-bit unpacked samples => RGB
  1879. */
  1880. DECLARESepPutFunc(putRGBseparate8bittile)
  1881. {
  1882. (void)img;
  1883. (void)x;
  1884. (void)y;
  1885. (void)a;
  1886. for (; h > 0; --h)
  1887. {
  1888. UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
  1889. SKEW(r, g, b, fromskew);
  1890. cp += toskew;
  1891. }
  1892. }
  1893. /*
  1894. * 8-bit unpacked samples => RGBA w/ associated alpha
  1895. */
  1896. DECLARESepPutFunc(putRGBAAseparate8bittile)
  1897. {
  1898. (void)img;
  1899. (void)x;
  1900. (void)y;
  1901. for (; h > 0; --h)
  1902. {
  1903. UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
  1904. SKEW4(r, g, b, a, fromskew);
  1905. cp += toskew;
  1906. }
  1907. }
  1908. /*
  1909. * 8-bit unpacked CMYK samples => RGBA
  1910. */
  1911. DECLARESepPutFunc(putCMYKseparate8bittile)
  1912. {
  1913. (void)img;
  1914. (void)y;
  1915. for (; h > 0; --h)
  1916. {
  1917. uint32_t rv, gv, bv, kv;
  1918. for (x = w; x > 0; --x)
  1919. {
  1920. kv = 255 - *a++;
  1921. rv = (kv * (255 - *r++)) / 255;
  1922. gv = (kv * (255 - *g++)) / 255;
  1923. bv = (kv * (255 - *b++)) / 255;
  1924. *cp++ = PACK4(rv, gv, bv, 255);
  1925. }
  1926. SKEW4(r, g, b, a, fromskew);
  1927. cp += toskew;
  1928. }
  1929. }
  1930. /*
  1931. * 8-bit unpacked samples => RGBA w/ unassociated alpha
  1932. */
  1933. DECLARESepPutFunc(putRGBUAseparate8bittile)
  1934. {
  1935. (void)img;
  1936. (void)y;
  1937. for (; h > 0; --h)
  1938. {
  1939. uint32_t rv, gv, bv, av;
  1940. uint8_t *m;
  1941. for (x = w; x > 0; --x)
  1942. {
  1943. av = *a++;
  1944. m = img->UaToAa + ((size_t)av << 8);
  1945. rv = m[*r++];
  1946. gv = m[*g++];
  1947. bv = m[*b++];
  1948. *cp++ = PACK4(rv, gv, bv, av);
  1949. }
  1950. SKEW4(r, g, b, a, fromskew);
  1951. cp += toskew;
  1952. }
  1953. }
  1954. /*
  1955. * 16-bit unpacked samples => RGB
  1956. */
  1957. DECLARESepPutFunc(putRGBseparate16bittile)
  1958. {
  1959. uint16_t *wr = (uint16_t *)r;
  1960. uint16_t *wg = (uint16_t *)g;
  1961. uint16_t *wb = (uint16_t *)b;
  1962. (void)img;
  1963. (void)y;
  1964. (void)a;
  1965. for (; h > 0; --h)
  1966. {
  1967. for (x = 0; x < w; x++)
  1968. *cp++ = PACK(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],
  1969. img->Bitdepth16To8[*wb++]);
  1970. SKEW(wr, wg, wb, fromskew);
  1971. cp += toskew;
  1972. }
  1973. }
  1974. /*
  1975. * 16-bit unpacked samples => RGBA w/ associated alpha
  1976. */
  1977. DECLARESepPutFunc(putRGBAAseparate16bittile)
  1978. {
  1979. uint16_t *wr = (uint16_t *)r;
  1980. uint16_t *wg = (uint16_t *)g;
  1981. uint16_t *wb = (uint16_t *)b;
  1982. uint16_t *wa = (uint16_t *)a;
  1983. (void)img;
  1984. (void)y;
  1985. for (; h > 0; --h)
  1986. {
  1987. for (x = 0; x < w; x++)
  1988. *cp++ = PACK4(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],
  1989. img->Bitdepth16To8[*wb++], img->Bitdepth16To8[*wa++]);
  1990. SKEW4(wr, wg, wb, wa, fromskew);
  1991. cp += toskew;
  1992. }
  1993. }
  1994. /*
  1995. * 16-bit unpacked samples => RGBA w/ unassociated alpha
  1996. */
  1997. DECLARESepPutFunc(putRGBUAseparate16bittile)
  1998. {
  1999. uint16_t *wr = (uint16_t *)r;
  2000. uint16_t *wg = (uint16_t *)g;
  2001. uint16_t *wb = (uint16_t *)b;
  2002. uint16_t *wa = (uint16_t *)a;
  2003. (void)img;
  2004. (void)y;
  2005. for (; h > 0; --h)
  2006. {
  2007. uint32_t r2, g2, b2, a2;
  2008. uint8_t *m;
  2009. for (x = w; x > 0; --x)
  2010. {
  2011. a2 = img->Bitdepth16To8[*wa++];
  2012. m = img->UaToAa + ((size_t)a2 << 8);
  2013. r2 = m[img->Bitdepth16To8[*wr++]];
  2014. g2 = m[img->Bitdepth16To8[*wg++]];
  2015. b2 = m[img->Bitdepth16To8[*wb++]];
  2016. *cp++ = PACK4(r2, g2, b2, a2);
  2017. }
  2018. SKEW4(wr, wg, wb, wa, fromskew);
  2019. cp += toskew;
  2020. }
  2021. }
  2022. /*
  2023. * 8-bit packed CIE L*a*b 1976 samples => RGB
  2024. */
  2025. DECLAREContigPutFunc(putcontig8bitCIELab8)
  2026. {
  2027. float X, Y, Z;
  2028. uint32_t r, g, b;
  2029. (void)y;
  2030. fromskew *= 3;
  2031. for (; h > 0; --h)
  2032. {
  2033. for (x = w; x > 0; --x)
  2034. {
  2035. TIFFCIELabToXYZ(img->cielab, (unsigned char)pp[0],
  2036. (signed char)pp[1], (signed char)pp[2], &X, &Y, &Z);
  2037. TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
  2038. *cp++ = PACK(r, g, b);
  2039. pp += 3;
  2040. }
  2041. cp += toskew;
  2042. pp += fromskew;
  2043. }
  2044. }
  2045. /*
  2046. * 16-bit packed CIE L*a*b 1976 samples => RGB
  2047. */
  2048. DECLAREContigPutFunc(putcontig8bitCIELab16)
  2049. {
  2050. float X, Y, Z;
  2051. uint32_t r, g, b;
  2052. uint16_t *wp = (uint16_t *)pp;
  2053. (void)y;
  2054. fromskew *= 3;
  2055. for (; h > 0; --h)
  2056. {
  2057. for (x = w; x > 0; --x)
  2058. {
  2059. TIFFCIELab16ToXYZ(img->cielab, (uint16_t)wp[0], (int16_t)wp[1],
  2060. (int16_t)wp[2], &X, &Y, &Z);
  2061. TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
  2062. *cp++ = PACK(r, g, b);
  2063. wp += 3;
  2064. }
  2065. cp += toskew;
  2066. wp += fromskew;
  2067. }
  2068. }
  2069. /*
  2070. * YCbCr -> RGB conversion and packing routines.
  2071. */
  2072. #define YCbCrtoRGB(dst, Y) \
  2073. { \
  2074. uint32_t r, g, b; \
  2075. TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b); \
  2076. dst = PACK(r, g, b); \
  2077. }
  2078. /*
  2079. * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
  2080. */
  2081. DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
  2082. {
  2083. uint32_t *cp1 = cp + w + toskew;
  2084. uint32_t *cp2 = cp1 + w + toskew;
  2085. uint32_t *cp3 = cp2 + w + toskew;
  2086. int32_t incr = 3 * w + 4 * toskew;
  2087. (void)y;
  2088. /* adjust fromskew */
  2089. fromskew = (fromskew / 4) * (4 * 2 + 2);
  2090. if ((h & 3) == 0 && (w & 3) == 0)
  2091. {
  2092. for (; h >= 4; h -= 4)
  2093. {
  2094. x = w >> 2;
  2095. do
  2096. {
  2097. int32_t Cb = pp[16];
  2098. int32_t Cr = pp[17];
  2099. YCbCrtoRGB(cp[0], pp[0]);
  2100. YCbCrtoRGB(cp[1], pp[1]);
  2101. YCbCrtoRGB(cp[2], pp[2]);
  2102. YCbCrtoRGB(cp[3], pp[3]);
  2103. YCbCrtoRGB(cp1[0], pp[4]);
  2104. YCbCrtoRGB(cp1[1], pp[5]);
  2105. YCbCrtoRGB(cp1[2], pp[6]);
  2106. YCbCrtoRGB(cp1[3], pp[7]);
  2107. YCbCrtoRGB(cp2[0], pp[8]);
  2108. YCbCrtoRGB(cp2[1], pp[9]);
  2109. YCbCrtoRGB(cp2[2], pp[10]);
  2110. YCbCrtoRGB(cp2[3], pp[11]);
  2111. YCbCrtoRGB(cp3[0], pp[12]);
  2112. YCbCrtoRGB(cp3[1], pp[13]);
  2113. YCbCrtoRGB(cp3[2], pp[14]);
  2114. YCbCrtoRGB(cp3[3], pp[15]);
  2115. cp += 4;
  2116. cp1 += 4;
  2117. cp2 += 4;
  2118. cp3 += 4;
  2119. pp += 18;
  2120. } while (--x);
  2121. cp += incr;
  2122. cp1 += incr;
  2123. cp2 += incr;
  2124. cp3 += incr;
  2125. pp += fromskew;
  2126. }
  2127. }
  2128. else
  2129. {
  2130. while (h > 0)
  2131. {
  2132. for (x = w; x > 0;)
  2133. {
  2134. int32_t Cb = pp[16];
  2135. int32_t Cr = pp[17];
  2136. switch (x)
  2137. {
  2138. default:
  2139. switch (h)
  2140. {
  2141. default:
  2142. YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
  2143. case 3:
  2144. YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
  2145. case 2:
  2146. YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
  2147. case 1:
  2148. YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
  2149. } /* FALLTHROUGH */
  2150. case 3:
  2151. switch (h)
  2152. {
  2153. default:
  2154. YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
  2155. case 3:
  2156. YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
  2157. case 2:
  2158. YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
  2159. case 1:
  2160. YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
  2161. } /* FALLTHROUGH */
  2162. case 2:
  2163. switch (h)
  2164. {
  2165. default:
  2166. YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
  2167. case 3:
  2168. YCbCrtoRGB(cp2[1], pp[9]); /* FALLTHROUGH */
  2169. case 2:
  2170. YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
  2171. case 1:
  2172. YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
  2173. } /* FALLTHROUGH */
  2174. case 1:
  2175. switch (h)
  2176. {
  2177. default:
  2178. YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
  2179. case 3:
  2180. YCbCrtoRGB(cp2[0], pp[8]); /* FALLTHROUGH */
  2181. case 2:
  2182. YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
  2183. case 1:
  2184. YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
  2185. } /* FALLTHROUGH */
  2186. }
  2187. if (x < 4)
  2188. {
  2189. cp += x;
  2190. cp1 += x;
  2191. cp2 += x;
  2192. cp3 += x;
  2193. x = 0;
  2194. }
  2195. else
  2196. {
  2197. cp += 4;
  2198. cp1 += 4;
  2199. cp2 += 4;
  2200. cp3 += 4;
  2201. x -= 4;
  2202. }
  2203. pp += 18;
  2204. }
  2205. if (h <= 4)
  2206. break;
  2207. h -= 4;
  2208. cp += incr;
  2209. cp1 += incr;
  2210. cp2 += incr;
  2211. cp3 += incr;
  2212. pp += fromskew;
  2213. }
  2214. }
  2215. }
  2216. /*
  2217. * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
  2218. */
  2219. DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
  2220. {
  2221. uint32_t *cp1 = cp + w + toskew;
  2222. int32_t incr = 2 * toskew + w;
  2223. (void)y;
  2224. fromskew = (fromskew / 4) * (4 * 2 + 2);
  2225. if ((w & 3) == 0 && (h & 1) == 0)
  2226. {
  2227. for (; h >= 2; h -= 2)
  2228. {
  2229. x = w >> 2;
  2230. do
  2231. {
  2232. int32_t Cb = pp[8];
  2233. int32_t Cr = pp[9];
  2234. YCbCrtoRGB(cp[0], pp[0]);
  2235. YCbCrtoRGB(cp[1], pp[1]);
  2236. YCbCrtoRGB(cp[2], pp[2]);
  2237. YCbCrtoRGB(cp[3], pp[3]);
  2238. YCbCrtoRGB(cp1[0], pp[4]);
  2239. YCbCrtoRGB(cp1[1], pp[5]);
  2240. YCbCrtoRGB(cp1[2], pp[6]);
  2241. YCbCrtoRGB(cp1[3], pp[7]);
  2242. cp += 4;
  2243. cp1 += 4;
  2244. pp += 10;
  2245. } while (--x);
  2246. cp += incr;
  2247. cp1 += incr;
  2248. pp += fromskew;
  2249. }
  2250. }
  2251. else
  2252. {
  2253. while (h > 0)
  2254. {
  2255. for (x = w; x > 0;)
  2256. {
  2257. int32_t Cb = pp[8];
  2258. int32_t Cr = pp[9];
  2259. switch (x)
  2260. {
  2261. default:
  2262. switch (h)
  2263. {
  2264. default:
  2265. YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
  2266. case 1:
  2267. YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
  2268. } /* FALLTHROUGH */
  2269. case 3:
  2270. switch (h)
  2271. {
  2272. default:
  2273. YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
  2274. case 1:
  2275. YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
  2276. } /* FALLTHROUGH */
  2277. case 2:
  2278. switch (h)
  2279. {
  2280. default:
  2281. YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
  2282. case 1:
  2283. YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
  2284. } /* FALLTHROUGH */
  2285. case 1:
  2286. switch (h)
  2287. {
  2288. default:
  2289. YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
  2290. case 1:
  2291. YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
  2292. } /* FALLTHROUGH */
  2293. }
  2294. if (x < 4)
  2295. {
  2296. cp += x;
  2297. cp1 += x;
  2298. x = 0;
  2299. }
  2300. else
  2301. {
  2302. cp += 4;
  2303. cp1 += 4;
  2304. x -= 4;
  2305. }
  2306. pp += 10;
  2307. }
  2308. if (h <= 2)
  2309. break;
  2310. h -= 2;
  2311. cp += incr;
  2312. cp1 += incr;
  2313. pp += fromskew;
  2314. }
  2315. }
  2316. }
  2317. /*
  2318. * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
  2319. */
  2320. DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
  2321. {
  2322. (void)y;
  2323. fromskew = (fromskew / 4) * (4 * 1 + 2);
  2324. do
  2325. {
  2326. x = w >> 2;
  2327. while (x > 0)
  2328. {
  2329. int32_t Cb = pp[4];
  2330. int32_t Cr = pp[5];
  2331. YCbCrtoRGB(cp[0], pp[0]);
  2332. YCbCrtoRGB(cp[1], pp[1]);
  2333. YCbCrtoRGB(cp[2], pp[2]);
  2334. YCbCrtoRGB(cp[3], pp[3]);
  2335. cp += 4;
  2336. pp += 6;
  2337. x--;
  2338. }
  2339. if ((w & 3) != 0)
  2340. {
  2341. int32_t Cb = pp[4];
  2342. int32_t Cr = pp[5];
  2343. switch ((w & 3))
  2344. {
  2345. case 3:
  2346. YCbCrtoRGB(cp[2], pp[2]); /*-fallthrough*/
  2347. case 2:
  2348. YCbCrtoRGB(cp[1], pp[1]); /*-fallthrough*/
  2349. case 1:
  2350. YCbCrtoRGB(cp[0], pp[0]); /*-fallthrough*/
  2351. case 0:
  2352. break;
  2353. }
  2354. cp += (w & 3);
  2355. pp += 6;
  2356. }
  2357. cp += toskew;
  2358. pp += fromskew;
  2359. } while (--h);
  2360. }
  2361. /*
  2362. * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
  2363. */
  2364. DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
  2365. {
  2366. uint32_t *cp2;
  2367. int32_t incr = 2 * toskew + w;
  2368. (void)y;
  2369. fromskew = (fromskew / 2) * (2 * 2 + 2);
  2370. cp2 = cp + w + toskew;
  2371. while (h >= 2)
  2372. {
  2373. x = w;
  2374. while (x >= 2)
  2375. {
  2376. uint32_t Cb = pp[4];
  2377. uint32_t Cr = pp[5];
  2378. YCbCrtoRGB(cp[0], pp[0]);
  2379. YCbCrtoRGB(cp[1], pp[1]);
  2380. YCbCrtoRGB(cp2[0], pp[2]);
  2381. YCbCrtoRGB(cp2[1], pp[3]);
  2382. cp += 2;
  2383. cp2 += 2;
  2384. pp += 6;
  2385. x -= 2;
  2386. }
  2387. if (x == 1)
  2388. {
  2389. uint32_t Cb = pp[4];
  2390. uint32_t Cr = pp[5];
  2391. YCbCrtoRGB(cp[0], pp[0]);
  2392. YCbCrtoRGB(cp2[0], pp[2]);
  2393. cp++;
  2394. cp2++;
  2395. pp += 6;
  2396. }
  2397. cp += incr;
  2398. cp2 += incr;
  2399. pp += fromskew;
  2400. h -= 2;
  2401. }
  2402. if (h == 1)
  2403. {
  2404. x = w;
  2405. while (x >= 2)
  2406. {
  2407. uint32_t Cb = pp[4];
  2408. uint32_t Cr = pp[5];
  2409. YCbCrtoRGB(cp[0], pp[0]);
  2410. YCbCrtoRGB(cp[1], pp[1]);
  2411. cp += 2;
  2412. cp2 += 2;
  2413. pp += 6;
  2414. x -= 2;
  2415. }
  2416. if (x == 1)
  2417. {
  2418. uint32_t Cb = pp[4];
  2419. uint32_t Cr = pp[5];
  2420. YCbCrtoRGB(cp[0], pp[0]);
  2421. }
  2422. }
  2423. }
  2424. /*
  2425. * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
  2426. */
  2427. DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
  2428. {
  2429. (void)y;
  2430. fromskew = (fromskew / 2) * (2 * 1 + 2);
  2431. do
  2432. {
  2433. x = w >> 1;
  2434. while (x > 0)
  2435. {
  2436. int32_t Cb = pp[2];
  2437. int32_t Cr = pp[3];
  2438. YCbCrtoRGB(cp[0], pp[0]);
  2439. YCbCrtoRGB(cp[1], pp[1]);
  2440. cp += 2;
  2441. pp += 4;
  2442. x--;
  2443. }
  2444. if ((w & 1) != 0)
  2445. {
  2446. int32_t Cb = pp[2];
  2447. int32_t Cr = pp[3];
  2448. YCbCrtoRGB(cp[0], pp[0]);
  2449. cp += 1;
  2450. pp += 4;
  2451. }
  2452. cp += toskew;
  2453. pp += fromskew;
  2454. } while (--h);
  2455. }
  2456. /*
  2457. * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
  2458. */
  2459. DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
  2460. {
  2461. uint32_t *cp2;
  2462. int32_t incr = 2 * toskew + w;
  2463. (void)y;
  2464. fromskew = (fromskew / 1) * (1 * 2 + 2);
  2465. cp2 = cp + w + toskew;
  2466. while (h >= 2)
  2467. {
  2468. x = w;
  2469. do
  2470. {
  2471. uint32_t Cb = pp[2];
  2472. uint32_t Cr = pp[3];
  2473. YCbCrtoRGB(cp[0], pp[0]);
  2474. YCbCrtoRGB(cp2[0], pp[1]);
  2475. cp++;
  2476. cp2++;
  2477. pp += 4;
  2478. } while (--x);
  2479. cp += incr;
  2480. cp2 += incr;
  2481. pp += fromskew;
  2482. h -= 2;
  2483. }
  2484. if (h == 1)
  2485. {
  2486. x = w;
  2487. do
  2488. {
  2489. uint32_t Cb = pp[2];
  2490. uint32_t Cr = pp[3];
  2491. YCbCrtoRGB(cp[0], pp[0]);
  2492. cp++;
  2493. pp += 4;
  2494. } while (--x);
  2495. }
  2496. }
  2497. /*
  2498. * 8-bit packed YCbCr samples w/ no subsampling => RGB
  2499. */
  2500. DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
  2501. {
  2502. (void)y;
  2503. fromskew = (fromskew / 1) * (1 * 1 + 2);
  2504. do
  2505. {
  2506. x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
  2507. do
  2508. {
  2509. int32_t Cb = pp[1];
  2510. int32_t Cr = pp[2];
  2511. YCbCrtoRGB(*cp++, pp[0]);
  2512. pp += 3;
  2513. } while (--x);
  2514. cp += toskew;
  2515. pp += fromskew;
  2516. } while (--h);
  2517. }
  2518. /*
  2519. * 8-bit packed YCbCr samples w/ no subsampling => RGB
  2520. */
  2521. DECLARESepPutFunc(putseparate8bitYCbCr11tile)
  2522. {
  2523. (void)y;
  2524. (void)a;
  2525. /* TODO: naming of input vars is still off, change obfuscating declaration
  2526. * inside define, or resolve obfuscation */
  2527. for (; h > 0; --h)
  2528. {
  2529. x = w;
  2530. do
  2531. {
  2532. uint32_t dr, dg, db;
  2533. TIFFYCbCrtoRGB(img->ycbcr, *r++, *g++, *b++, &dr, &dg, &db);
  2534. *cp++ = PACK(dr, dg, db);
  2535. } while (--x);
  2536. SKEW(r, g, b, fromskew);
  2537. cp += toskew;
  2538. }
  2539. }
  2540. #undef YCbCrtoRGB
  2541. static int isInRefBlackWhiteRange(float f)
  2542. {
  2543. return f > (float)(-0x7FFFFFFF + 128) && f < (float)0x7FFFFFFF;
  2544. }
  2545. static int initYCbCrConversion(TIFFRGBAImage *img)
  2546. {
  2547. static const char module[] = "initYCbCrConversion";
  2548. float *luma, *refBlackWhite;
  2549. if (img->ycbcr == NULL)
  2550. {
  2551. img->ycbcr = (TIFFYCbCrToRGB *)_TIFFmallocExt(
  2552. img->tif, TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long)) +
  2553. 4 * 256 * sizeof(TIFFRGBValue) +
  2554. 2 * 256 * sizeof(int) + 3 * 256 * sizeof(int32_t));
  2555. if (img->ycbcr == NULL)
  2556. {
  2557. TIFFErrorExtR(img->tif, module,
  2558. "No space for YCbCr->RGB conversion state");
  2559. return (0);
  2560. }
  2561. }
  2562. TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
  2563. TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
  2564. &refBlackWhite);
  2565. /* Do some validation to avoid later issues. Detect NaN for now */
  2566. /* and also if lumaGreen is zero since we divide by it later */
  2567. if (luma[0] != luma[0] || luma[1] != luma[1] || luma[1] == 0.0 ||
  2568. luma[2] != luma[2])
  2569. {
  2570. TIFFErrorExtR(img->tif, module,
  2571. "Invalid values for YCbCrCoefficients tag");
  2572. return (0);
  2573. }
  2574. if (!isInRefBlackWhiteRange(refBlackWhite[0]) ||
  2575. !isInRefBlackWhiteRange(refBlackWhite[1]) ||
  2576. !isInRefBlackWhiteRange(refBlackWhite[2]) ||
  2577. !isInRefBlackWhiteRange(refBlackWhite[3]) ||
  2578. !isInRefBlackWhiteRange(refBlackWhite[4]) ||
  2579. !isInRefBlackWhiteRange(refBlackWhite[5]))
  2580. {
  2581. TIFFErrorExtR(img->tif, module,
  2582. "Invalid values for ReferenceBlackWhite tag");
  2583. return (0);
  2584. }
  2585. if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
  2586. return (0);
  2587. return (1);
  2588. }
  2589. static tileContigRoutine initCIELabConversion(TIFFRGBAImage *img)
  2590. {
  2591. static const char module[] = "initCIELabConversion";
  2592. float *whitePoint;
  2593. float refWhite[3];
  2594. TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
  2595. if (whitePoint[1] == 0.0f)
  2596. {
  2597. TIFFErrorExtR(img->tif, module, "Invalid value for WhitePoint tag.");
  2598. return NULL;
  2599. }
  2600. if (!img->cielab)
  2601. {
  2602. img->cielab = (TIFFCIELabToRGB *)_TIFFmallocExt(
  2603. img->tif, sizeof(TIFFCIELabToRGB));
  2604. if (!img->cielab)
  2605. {
  2606. TIFFErrorExtR(img->tif, module,
  2607. "No space for CIE L*a*b*->RGB conversion state.");
  2608. return NULL;
  2609. }
  2610. }
  2611. refWhite[1] = 100.0F;
  2612. refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
  2613. refWhite[2] =
  2614. (1.0F - whitePoint[0] - whitePoint[1]) / whitePoint[1] * refWhite[1];
  2615. if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0)
  2616. {
  2617. TIFFErrorExtR(img->tif, module,
  2618. "Failed to initialize CIE L*a*b*->RGB conversion state.");
  2619. _TIFFfreeExt(img->tif, img->cielab);
  2620. return NULL;
  2621. }
  2622. if (img->bitspersample == 8)
  2623. return putcontig8bitCIELab8;
  2624. else if (img->bitspersample == 16)
  2625. return putcontig8bitCIELab16;
  2626. return NULL;
  2627. }
  2628. /*
  2629. * Greyscale images with less than 8 bits/sample are handled
  2630. * with a table to avoid lots of shifts and masks. The table
  2631. * is setup so that put*bwtile (below) can retrieve 8/bitspersample
  2632. * pixel values simply by indexing into the table with one
  2633. * number.
  2634. */
  2635. static int makebwmap(TIFFRGBAImage *img)
  2636. {
  2637. TIFFRGBValue *Map = img->Map;
  2638. int bitspersample = img->bitspersample;
  2639. int nsamples = 8 / bitspersample;
  2640. int i;
  2641. uint32_t *p;
  2642. if (nsamples == 0)
  2643. nsamples = 1;
  2644. img->BWmap = (uint32_t **)_TIFFmallocExt(
  2645. img->tif,
  2646. 256 * sizeof(uint32_t *) + (256 * nsamples * sizeof(uint32_t)));
  2647. if (img->BWmap == NULL)
  2648. {
  2649. TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
  2650. "No space for B&W mapping table");
  2651. return (0);
  2652. }
  2653. p = (uint32_t *)(img->BWmap + 256);
  2654. for (i = 0; i < 256; i++)
  2655. {
  2656. TIFFRGBValue c;
  2657. img->BWmap[i] = p;
  2658. switch (bitspersample)
  2659. {
  2660. #define GREY(x) \
  2661. c = Map[x]; \
  2662. *p++ = PACK(c, c, c);
  2663. case 1:
  2664. GREY(i >> 7);
  2665. GREY((i >> 6) & 1);
  2666. GREY((i >> 5) & 1);
  2667. GREY((i >> 4) & 1);
  2668. GREY((i >> 3) & 1);
  2669. GREY((i >> 2) & 1);
  2670. GREY((i >> 1) & 1);
  2671. GREY(i & 1);
  2672. break;
  2673. case 2:
  2674. GREY(i >> 6);
  2675. GREY((i >> 4) & 3);
  2676. GREY((i >> 2) & 3);
  2677. GREY(i & 3);
  2678. break;
  2679. case 4:
  2680. GREY(i >> 4);
  2681. GREY(i & 0xf);
  2682. break;
  2683. case 8:
  2684. case 16:
  2685. GREY(i);
  2686. break;
  2687. }
  2688. #undef GREY
  2689. }
  2690. return (1);
  2691. }
  2692. /*
  2693. * Construct a mapping table to convert from the range
  2694. * of the data samples to [0,255] --for display. This
  2695. * process also handles inverting B&W images when needed.
  2696. */
  2697. static int setupMap(TIFFRGBAImage *img)
  2698. {
  2699. int32_t x, range;
  2700. range = (int32_t)((1L << img->bitspersample) - 1);
  2701. /* treat 16 bit the same as eight bit */
  2702. if (img->bitspersample == 16)
  2703. range = (int32_t)255;
  2704. img->Map = (TIFFRGBValue *)_TIFFmallocExt(
  2705. img->tif, (range + 1) * sizeof(TIFFRGBValue));
  2706. if (img->Map == NULL)
  2707. {
  2708. TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
  2709. "No space for photometric conversion table");
  2710. return (0);
  2711. }
  2712. if (img->photometric == PHOTOMETRIC_MINISWHITE)
  2713. {
  2714. for (x = 0; x <= range; x++)
  2715. img->Map[x] = (TIFFRGBValue)(((range - x) * 255) / range);
  2716. }
  2717. else
  2718. {
  2719. for (x = 0; x <= range; x++)
  2720. img->Map[x] = (TIFFRGBValue)((x * 255) / range);
  2721. }
  2722. if (img->bitspersample <= 16 &&
  2723. (img->photometric == PHOTOMETRIC_MINISBLACK ||
  2724. img->photometric == PHOTOMETRIC_MINISWHITE))
  2725. {
  2726. /*
  2727. * Use photometric mapping table to construct
  2728. * unpacking tables for samples <= 8 bits.
  2729. */
  2730. if (!makebwmap(img))
  2731. return (0);
  2732. /* no longer need Map, free it */
  2733. _TIFFfreeExt(img->tif, img->Map);
  2734. img->Map = NULL;
  2735. }
  2736. return (1);
  2737. }
  2738. static int checkcmap(TIFFRGBAImage *img)
  2739. {
  2740. uint16_t *r = img->redcmap;
  2741. uint16_t *g = img->greencmap;
  2742. uint16_t *b = img->bluecmap;
  2743. long n = 1L << img->bitspersample;
  2744. while (n-- > 0)
  2745. if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
  2746. return (16);
  2747. return (8);
  2748. }
  2749. static void cvtcmap(TIFFRGBAImage *img)
  2750. {
  2751. uint16_t *r = img->redcmap;
  2752. uint16_t *g = img->greencmap;
  2753. uint16_t *b = img->bluecmap;
  2754. long i;
  2755. for (i = (1L << img->bitspersample) - 1; i >= 0; i--)
  2756. {
  2757. #define CVT(x) ((uint16_t)((x) >> 8))
  2758. r[i] = CVT(r[i]);
  2759. g[i] = CVT(g[i]);
  2760. b[i] = CVT(b[i]);
  2761. #undef CVT
  2762. }
  2763. }
  2764. /*
  2765. * Palette images with <= 8 bits/sample are handled
  2766. * with a table to avoid lots of shifts and masks. The table
  2767. * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
  2768. * pixel values simply by indexing into the table with one
  2769. * number.
  2770. */
  2771. static int makecmap(TIFFRGBAImage *img)
  2772. {
  2773. int bitspersample = img->bitspersample;
  2774. int nsamples = 8 / bitspersample;
  2775. uint16_t *r = img->redcmap;
  2776. uint16_t *g = img->greencmap;
  2777. uint16_t *b = img->bluecmap;
  2778. uint32_t *p;
  2779. int i;
  2780. img->PALmap = (uint32_t **)_TIFFmallocExt(
  2781. img->tif,
  2782. 256 * sizeof(uint32_t *) + (256 * nsamples * sizeof(uint32_t)));
  2783. if (img->PALmap == NULL)
  2784. {
  2785. TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
  2786. "No space for Palette mapping table");
  2787. return (0);
  2788. }
  2789. p = (uint32_t *)(img->PALmap + 256);
  2790. for (i = 0; i < 256; i++)
  2791. {
  2792. TIFFRGBValue c;
  2793. img->PALmap[i] = p;
  2794. #define CMAP(x) \
  2795. c = (TIFFRGBValue)x; \
  2796. *p++ = PACK(r[c] & 0xff, g[c] & 0xff, b[c] & 0xff);
  2797. switch (bitspersample)
  2798. {
  2799. case 1:
  2800. CMAP(i >> 7);
  2801. CMAP((i >> 6) & 1);
  2802. CMAP((i >> 5) & 1);
  2803. CMAP((i >> 4) & 1);
  2804. CMAP((i >> 3) & 1);
  2805. CMAP((i >> 2) & 1);
  2806. CMAP((i >> 1) & 1);
  2807. CMAP(i & 1);
  2808. break;
  2809. case 2:
  2810. CMAP(i >> 6);
  2811. CMAP((i >> 4) & 3);
  2812. CMAP((i >> 2) & 3);
  2813. CMAP(i & 3);
  2814. break;
  2815. case 4:
  2816. CMAP(i >> 4);
  2817. CMAP(i & 0xf);
  2818. break;
  2819. case 8:
  2820. CMAP(i);
  2821. break;
  2822. }
  2823. #undef CMAP
  2824. }
  2825. return (1);
  2826. }
  2827. /*
  2828. * Construct any mapping table used
  2829. * by the associated put routine.
  2830. */
  2831. static int buildMap(TIFFRGBAImage *img)
  2832. {
  2833. switch (img->photometric)
  2834. {
  2835. case PHOTOMETRIC_RGB:
  2836. case PHOTOMETRIC_YCBCR:
  2837. case PHOTOMETRIC_SEPARATED:
  2838. if (img->bitspersample == 8)
  2839. break;
  2840. /* fall through... */
  2841. case PHOTOMETRIC_MINISBLACK:
  2842. case PHOTOMETRIC_MINISWHITE:
  2843. if (!setupMap(img))
  2844. return (0);
  2845. break;
  2846. case PHOTOMETRIC_PALETTE:
  2847. /*
  2848. * Convert 16-bit colormap to 8-bit (unless it looks
  2849. * like an old-style 8-bit colormap).
  2850. */
  2851. if (checkcmap(img) == 16)
  2852. cvtcmap(img);
  2853. else
  2854. TIFFWarningExtR(img->tif, TIFFFileName(img->tif),
  2855. "Assuming 8-bit colormap");
  2856. /*
  2857. * Use mapping table and colormap to construct
  2858. * unpacking tables for samples < 8 bits.
  2859. */
  2860. if (img->bitspersample <= 8 && !makecmap(img))
  2861. return (0);
  2862. break;
  2863. }
  2864. return (1);
  2865. }
  2866. /*
  2867. * Select the appropriate conversion routine for packed data.
  2868. */
  2869. static int PickContigCase(TIFFRGBAImage *img)
  2870. {
  2871. img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
  2872. img->put.contig = NULL;
  2873. switch (img->photometric)
  2874. {
  2875. case PHOTOMETRIC_RGB:
  2876. switch (img->bitspersample)
  2877. {
  2878. case 8:
  2879. if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
  2880. img->samplesperpixel >= 4)
  2881. img->put.contig = putRGBAAcontig8bittile;
  2882. else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
  2883. img->samplesperpixel >= 4)
  2884. {
  2885. if (BuildMapUaToAa(img))
  2886. img->put.contig = putRGBUAcontig8bittile;
  2887. }
  2888. else if (img->samplesperpixel >= 3)
  2889. img->put.contig = putRGBcontig8bittile;
  2890. break;
  2891. case 16:
  2892. if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
  2893. img->samplesperpixel >= 4)
  2894. {
  2895. if (BuildMapBitdepth16To8(img))
  2896. img->put.contig = putRGBAAcontig16bittile;
  2897. }
  2898. else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
  2899. img->samplesperpixel >= 4)
  2900. {
  2901. if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
  2902. img->put.contig = putRGBUAcontig16bittile;
  2903. }
  2904. else if (img->samplesperpixel >= 3)
  2905. {
  2906. if (BuildMapBitdepth16To8(img))
  2907. img->put.contig = putRGBcontig16bittile;
  2908. }
  2909. break;
  2910. }
  2911. break;
  2912. case PHOTOMETRIC_SEPARATED:
  2913. if (img->samplesperpixel >= 4 && buildMap(img))
  2914. {
  2915. if (img->bitspersample == 8)
  2916. {
  2917. if (!img->Map)
  2918. img->put.contig = putRGBcontig8bitCMYKtile;
  2919. else
  2920. img->put.contig = putRGBcontig8bitCMYKMaptile;
  2921. }
  2922. }
  2923. break;
  2924. case PHOTOMETRIC_PALETTE:
  2925. if (buildMap(img))
  2926. {
  2927. switch (img->bitspersample)
  2928. {
  2929. case 8:
  2930. img->put.contig = put8bitcmaptile;
  2931. break;
  2932. case 4:
  2933. img->put.contig = put4bitcmaptile;
  2934. break;
  2935. case 2:
  2936. img->put.contig = put2bitcmaptile;
  2937. break;
  2938. case 1:
  2939. img->put.contig = put1bitcmaptile;
  2940. break;
  2941. }
  2942. }
  2943. break;
  2944. case PHOTOMETRIC_MINISWHITE:
  2945. case PHOTOMETRIC_MINISBLACK:
  2946. if (buildMap(img))
  2947. {
  2948. switch (img->bitspersample)
  2949. {
  2950. case 16:
  2951. img->put.contig = put16bitbwtile;
  2952. break;
  2953. case 8:
  2954. if (img->alpha && img->samplesperpixel == 2)
  2955. img->put.contig = putagreytile;
  2956. else
  2957. img->put.contig = putgreytile;
  2958. break;
  2959. case 4:
  2960. img->put.contig = put4bitbwtile;
  2961. break;
  2962. case 2:
  2963. img->put.contig = put2bitbwtile;
  2964. break;
  2965. case 1:
  2966. img->put.contig = put1bitbwtile;
  2967. break;
  2968. }
  2969. }
  2970. break;
  2971. case PHOTOMETRIC_YCBCR:
  2972. if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
  2973. {
  2974. if (initYCbCrConversion(img) != 0)
  2975. {
  2976. /*
  2977. * The 6.0 spec says that subsampling must be
  2978. * one of 1, 2, or 4, and that vertical subsampling
  2979. * must always be <= horizontal subsampling; so
  2980. * there are only a few possibilities and we just
  2981. * enumerate the cases.
  2982. * Joris: added support for the [1,2] case, nonetheless, to
  2983. * accommodate some OJPEG files
  2984. */
  2985. uint16_t SubsamplingHor;
  2986. uint16_t SubsamplingVer;
  2987. TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,
  2988. &SubsamplingHor, &SubsamplingVer);
  2989. switch ((SubsamplingHor << 4) | SubsamplingVer)
  2990. {
  2991. case 0x44:
  2992. img->put.contig = putcontig8bitYCbCr44tile;
  2993. break;
  2994. case 0x42:
  2995. img->put.contig = putcontig8bitYCbCr42tile;
  2996. break;
  2997. case 0x41:
  2998. img->put.contig = putcontig8bitYCbCr41tile;
  2999. break;
  3000. case 0x22:
  3001. img->put.contig = putcontig8bitYCbCr22tile;
  3002. break;
  3003. case 0x21:
  3004. img->put.contig = putcontig8bitYCbCr21tile;
  3005. break;
  3006. case 0x12:
  3007. img->put.contig = putcontig8bitYCbCr12tile;
  3008. break;
  3009. case 0x11:
  3010. img->put.contig = putcontig8bitYCbCr11tile;
  3011. break;
  3012. }
  3013. }
  3014. }
  3015. break;
  3016. case PHOTOMETRIC_CIELAB:
  3017. if (img->samplesperpixel == 3 && buildMap(img))
  3018. {
  3019. if (img->bitspersample == 8 || img->bitspersample == 16)
  3020. img->put.contig = initCIELabConversion(img);
  3021. break;
  3022. }
  3023. }
  3024. return ((img->get != NULL) && (img->put.contig != NULL));
  3025. }
  3026. /*
  3027. * Select the appropriate conversion routine for unpacked data.
  3028. *
  3029. * NB: we assume that unpacked single channel data is directed
  3030. * to the "packed routines.
  3031. */
  3032. static int PickSeparateCase(TIFFRGBAImage *img)
  3033. {
  3034. img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
  3035. img->put.separate = NULL;
  3036. switch (img->photometric)
  3037. {
  3038. case PHOTOMETRIC_MINISWHITE:
  3039. case PHOTOMETRIC_MINISBLACK:
  3040. /* greyscale images processed pretty much as RGB by gtTileSeparate
  3041. */
  3042. case PHOTOMETRIC_RGB:
  3043. switch (img->bitspersample)
  3044. {
  3045. case 8:
  3046. if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  3047. img->put.separate = putRGBAAseparate8bittile;
  3048. else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  3049. {
  3050. if (BuildMapUaToAa(img))
  3051. img->put.separate = putRGBUAseparate8bittile;
  3052. }
  3053. else
  3054. img->put.separate = putRGBseparate8bittile;
  3055. break;
  3056. case 16:
  3057. if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  3058. {
  3059. if (BuildMapBitdepth16To8(img))
  3060. img->put.separate = putRGBAAseparate16bittile;
  3061. }
  3062. else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  3063. {
  3064. if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
  3065. img->put.separate = putRGBUAseparate16bittile;
  3066. }
  3067. else
  3068. {
  3069. if (BuildMapBitdepth16To8(img))
  3070. img->put.separate = putRGBseparate16bittile;
  3071. }
  3072. break;
  3073. }
  3074. break;
  3075. case PHOTOMETRIC_SEPARATED:
  3076. if (img->bitspersample == 8 && img->samplesperpixel == 4)
  3077. {
  3078. /* Not alpha, but seems like the only way to get 4th band */
  3079. img->alpha = 1;
  3080. img->put.separate = putCMYKseparate8bittile;
  3081. }
  3082. break;
  3083. case PHOTOMETRIC_YCBCR:
  3084. if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
  3085. {
  3086. if (initYCbCrConversion(img) != 0)
  3087. {
  3088. uint16_t hs, vs;
  3089. TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,
  3090. &hs, &vs);
  3091. switch ((hs << 4) | vs)
  3092. {
  3093. case 0x11:
  3094. img->put.separate = putseparate8bitYCbCr11tile;
  3095. break;
  3096. /* TODO: add other cases here */
  3097. }
  3098. }
  3099. }
  3100. break;
  3101. }
  3102. return ((img->get != NULL) && (img->put.separate != NULL));
  3103. }
  3104. static int BuildMapUaToAa(TIFFRGBAImage *img)
  3105. {
  3106. static const char module[] = "BuildMapUaToAa";
  3107. uint8_t *m;
  3108. uint16_t na, nv;
  3109. assert(img->UaToAa == NULL);
  3110. img->UaToAa = _TIFFmallocExt(img->tif, 65536);
  3111. if (img->UaToAa == NULL)
  3112. {
  3113. TIFFErrorExtR(img->tif, module, "Out of memory");
  3114. return (0);
  3115. }
  3116. m = img->UaToAa;
  3117. for (na = 0; na < 256; na++)
  3118. {
  3119. for (nv = 0; nv < 256; nv++)
  3120. *m++ = (uint8_t)((nv * na + 127) / 255);
  3121. }
  3122. return (1);
  3123. }
  3124. static int BuildMapBitdepth16To8(TIFFRGBAImage *img)
  3125. {
  3126. static const char module[] = "BuildMapBitdepth16To8";
  3127. uint8_t *m;
  3128. uint32_t n;
  3129. assert(img->Bitdepth16To8 == NULL);
  3130. img->Bitdepth16To8 = _TIFFmallocExt(img->tif, 65536);
  3131. if (img->Bitdepth16To8 == NULL)
  3132. {
  3133. TIFFErrorExtR(img->tif, module, "Out of memory");
  3134. return (0);
  3135. }
  3136. m = img->Bitdepth16To8;
  3137. for (n = 0; n < 65536; n++)
  3138. *m++ = (uint8_t)((n + 128) / 257);
  3139. return (1);
  3140. }
  3141. /*
  3142. * Read a whole strip off data from the file, and convert to RGBA form.
  3143. * If this is the last strip, then it will only contain the portion of
  3144. * the strip that is actually within the image space. The result is
  3145. * organized in bottom to top form.
  3146. */
  3147. int TIFFReadRGBAStrip(TIFF *tif, uint32_t row, uint32_t *raster)
  3148. {
  3149. return TIFFReadRGBAStripExt(tif, row, raster, 0);
  3150. }
  3151. int TIFFReadRGBAStripExt(TIFF *tif, uint32_t row, uint32_t *raster,
  3152. int stop_on_error)
  3153. {
  3154. char emsg[EMSG_BUF_SIZE] = "";
  3155. TIFFRGBAImage img;
  3156. int ok;
  3157. uint32_t rowsperstrip, rows_to_read;
  3158. if (TIFFIsTiled(tif))
  3159. {
  3160. TIFFErrorExtR(tif, TIFFFileName(tif),
  3161. "Can't use TIFFReadRGBAStrip() with tiled file.");
  3162. return (0);
  3163. }
  3164. TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  3165. if (rowsperstrip == 0)
  3166. {
  3167. TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
  3168. return (0);
  3169. }
  3170. if ((row % rowsperstrip) != 0)
  3171. {
  3172. TIFFErrorExtR(
  3173. tif, TIFFFileName(tif),
  3174. "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
  3175. return (0);
  3176. }
  3177. if (TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))
  3178. {
  3179. if (row >= img.height)
  3180. {
  3181. TIFFErrorExtR(tif, TIFFFileName(tif),
  3182. "Invalid row passed to TIFFReadRGBAStrip().");
  3183. TIFFRGBAImageEnd(&img);
  3184. return (0);
  3185. }
  3186. img.row_offset = row;
  3187. img.col_offset = 0;
  3188. if (row + rowsperstrip > img.height)
  3189. rows_to_read = img.height - row;
  3190. else
  3191. rows_to_read = rowsperstrip;
  3192. ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read);
  3193. TIFFRGBAImageEnd(&img);
  3194. }
  3195. else
  3196. {
  3197. TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
  3198. ok = 0;
  3199. }
  3200. return (ok);
  3201. }
  3202. /*
  3203. * Read a whole tile off data from the file, and convert to RGBA form.
  3204. * The returned RGBA data is organized from bottom to top of tile,
  3205. * and may include zeroed areas if the tile extends off the image.
  3206. */
  3207. int TIFFReadRGBATile(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster)
  3208. {
  3209. return TIFFReadRGBATileExt(tif, col, row, raster, 0);
  3210. }
  3211. int TIFFReadRGBATileExt(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster,
  3212. int stop_on_error)
  3213. {
  3214. char emsg[EMSG_BUF_SIZE] = "";
  3215. TIFFRGBAImage img;
  3216. int ok;
  3217. uint32_t tile_xsize, tile_ysize;
  3218. uint32_t read_xsize, read_ysize;
  3219. uint32_t i_row;
  3220. /*
  3221. * Verify that our request is legal - on a tile file, and on a
  3222. * tile boundary.
  3223. */
  3224. if (!TIFFIsTiled(tif))
  3225. {
  3226. TIFFErrorExtR(tif, TIFFFileName(tif),
  3227. "Can't use TIFFReadRGBATile() with striped file.");
  3228. return (0);
  3229. }
  3230. TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
  3231. TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
  3232. if (tile_xsize == 0 || tile_ysize == 0)
  3233. {
  3234. TIFFErrorExtR(tif, TIFFFileName(tif),
  3235. "tile_xsize or tile_ysize is zero");
  3236. return (0);
  3237. }
  3238. if ((col % tile_xsize) != 0 || (row % tile_ysize) != 0)
  3239. {
  3240. TIFFErrorExtR(tif, TIFFFileName(tif),
  3241. "Row/col passed to TIFFReadRGBATile() must be top"
  3242. "left corner of a tile.");
  3243. return (0);
  3244. }
  3245. /*
  3246. * Setup the RGBA reader.
  3247. */
  3248. if (!TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))
  3249. {
  3250. TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
  3251. return (0);
  3252. }
  3253. if (col >= img.width || row >= img.height)
  3254. {
  3255. TIFFErrorExtR(tif, TIFFFileName(tif),
  3256. "Invalid row/col passed to TIFFReadRGBATile().");
  3257. TIFFRGBAImageEnd(&img);
  3258. return (0);
  3259. }
  3260. /*
  3261. * The TIFFRGBAImageGet() function doesn't allow us to get off the
  3262. * edge of the image, even to fill an otherwise valid tile. So we
  3263. * figure out how much we can read, and fix up the tile buffer to
  3264. * a full tile configuration afterwards.
  3265. */
  3266. if (row + tile_ysize > img.height)
  3267. read_ysize = img.height - row;
  3268. else
  3269. read_ysize = tile_ysize;
  3270. if (col + tile_xsize > img.width)
  3271. read_xsize = img.width - col;
  3272. else
  3273. read_xsize = tile_xsize;
  3274. /*
  3275. * Read the chunk of imagery.
  3276. */
  3277. img.row_offset = row;
  3278. img.col_offset = col;
  3279. ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize);
  3280. TIFFRGBAImageEnd(&img);
  3281. /*
  3282. * If our read was incomplete we will need to fix up the tile by
  3283. * shifting the data around as if a full tile of data is being returned.
  3284. *
  3285. * This is all the more complicated because the image is organized in
  3286. * bottom to top format.
  3287. */
  3288. if (read_xsize == tile_xsize && read_ysize == tile_ysize)
  3289. return (ok);
  3290. for (i_row = 0; i_row < read_ysize; i_row++)
  3291. {
  3292. memmove(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize,
  3293. raster + (size_t)(read_ysize - i_row - 1) * read_xsize,
  3294. read_xsize * sizeof(uint32_t));
  3295. _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize +
  3296. read_xsize,
  3297. 0, sizeof(uint32_t) * (tile_xsize - read_xsize));
  3298. }
  3299. for (i_row = read_ysize; i_row < tile_ysize; i_row++)
  3300. {
  3301. _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize, 0,
  3302. sizeof(uint32_t) * tile_xsize);
  3303. }
  3304. return (ok);
  3305. }