tif_getimage.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412
  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. return (*img->get)(img, raster, w, h);
  579. }
  580. /*
  581. * Read the specified image into an ABGR-format rastertaking in account
  582. * specified orientation.
  583. */
  584. int TIFFReadRGBAImageOriented(TIFF *tif, uint32_t rwidth, uint32_t rheight,
  585. uint32_t *raster, int orientation, int stop)
  586. {
  587. char emsg[EMSG_BUF_SIZE] = "";
  588. TIFFRGBAImage img;
  589. int ok;
  590. if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop, emsg))
  591. {
  592. img.req_orientation = (uint16_t)orientation;
  593. /* XXX verify rwidth and rheight against width and height */
  594. ok = TIFFRGBAImageGet(&img, raster + (rheight - img.height) * rwidth,
  595. rwidth, img.height);
  596. TIFFRGBAImageEnd(&img);
  597. }
  598. else
  599. {
  600. TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
  601. ok = 0;
  602. }
  603. return (ok);
  604. }
  605. /*
  606. * Read the specified image into an ABGR-format raster. Use bottom left
  607. * origin for raster by default.
  608. */
  609. int TIFFReadRGBAImage(TIFF *tif, uint32_t rwidth, uint32_t rheight,
  610. uint32_t *raster, int stop)
  611. {
  612. return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
  613. ORIENTATION_BOTLEFT, stop);
  614. }
  615. static int setorientation(TIFFRGBAImage *img)
  616. {
  617. switch (img->orientation)
  618. {
  619. case ORIENTATION_TOPLEFT:
  620. case ORIENTATION_LEFTTOP:
  621. if (img->req_orientation == ORIENTATION_TOPRIGHT ||
  622. img->req_orientation == ORIENTATION_RIGHTTOP)
  623. return FLIP_HORIZONTALLY;
  624. else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
  625. img->req_orientation == ORIENTATION_RIGHTBOT)
  626. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  627. else if (img->req_orientation == ORIENTATION_BOTLEFT ||
  628. img->req_orientation == ORIENTATION_LEFTBOT)
  629. return FLIP_VERTICALLY;
  630. else
  631. return 0;
  632. case ORIENTATION_TOPRIGHT:
  633. case ORIENTATION_RIGHTTOP:
  634. if (img->req_orientation == ORIENTATION_TOPLEFT ||
  635. img->req_orientation == ORIENTATION_LEFTTOP)
  636. return FLIP_HORIZONTALLY;
  637. else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
  638. img->req_orientation == ORIENTATION_RIGHTBOT)
  639. return FLIP_VERTICALLY;
  640. else if (img->req_orientation == ORIENTATION_BOTLEFT ||
  641. img->req_orientation == ORIENTATION_LEFTBOT)
  642. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  643. else
  644. return 0;
  645. case ORIENTATION_BOTRIGHT:
  646. case ORIENTATION_RIGHTBOT:
  647. if (img->req_orientation == ORIENTATION_TOPLEFT ||
  648. img->req_orientation == ORIENTATION_LEFTTOP)
  649. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  650. else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
  651. img->req_orientation == ORIENTATION_RIGHTTOP)
  652. return FLIP_VERTICALLY;
  653. else if (img->req_orientation == ORIENTATION_BOTLEFT ||
  654. img->req_orientation == ORIENTATION_LEFTBOT)
  655. return FLIP_HORIZONTALLY;
  656. else
  657. return 0;
  658. case ORIENTATION_BOTLEFT:
  659. case ORIENTATION_LEFTBOT:
  660. if (img->req_orientation == ORIENTATION_TOPLEFT ||
  661. img->req_orientation == ORIENTATION_LEFTTOP)
  662. return FLIP_VERTICALLY;
  663. else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
  664. img->req_orientation == ORIENTATION_RIGHTTOP)
  665. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  666. else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
  667. img->req_orientation == ORIENTATION_RIGHTBOT)
  668. return FLIP_HORIZONTALLY;
  669. else
  670. return 0;
  671. default: /* NOTREACHED */
  672. return 0;
  673. }
  674. }
  675. /*
  676. * Get an tile-organized image that has
  677. * PlanarConfiguration contiguous if SamplesPerPixel > 1
  678. * or
  679. * SamplesPerPixel == 1
  680. */
  681. static int gtTileContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
  682. uint32_t h)
  683. {
  684. TIFF *tif = img->tif;
  685. tileContigRoutine put = img->put.contig;
  686. uint32_t col, row, y, rowstoread;
  687. tmsize_t pos;
  688. uint32_t tw, th;
  689. unsigned char *buf = NULL;
  690. int32_t fromskew, toskew;
  691. uint32_t nrow;
  692. int ret = 1, flip;
  693. uint32_t this_tw, tocol;
  694. int32_t this_toskew, leftmost_toskew;
  695. int32_t leftmost_fromskew;
  696. uint32_t leftmost_tw;
  697. tmsize_t bufsize;
  698. bufsize = TIFFTileSize(tif);
  699. if (bufsize == 0)
  700. {
  701. TIFFErrorExtR(tif, TIFFFileName(tif), "%s", "No space for tile buffer");
  702. return (0);
  703. }
  704. TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  705. TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  706. flip = setorientation(img);
  707. if (flip & FLIP_VERTICALLY)
  708. {
  709. if ((tw + w) > INT_MAX)
  710. {
  711. TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
  712. "unsupported tile size (too wide)");
  713. return (0);
  714. }
  715. y = h - 1;
  716. toskew = -(int32_t)(tw + w);
  717. }
  718. else
  719. {
  720. if (tw > (INT_MAX + w))
  721. {
  722. TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
  723. "unsupported tile size (too wide)");
  724. return (0);
  725. }
  726. y = 0;
  727. toskew = -(int32_t)(tw - w);
  728. }
  729. if (tw == 0 || th == 0)
  730. {
  731. TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");
  732. return (0);
  733. }
  734. /*
  735. * Leftmost tile is clipped on left side if col_offset > 0.
  736. */
  737. leftmost_fromskew = img->col_offset % tw;
  738. leftmost_tw = tw - leftmost_fromskew;
  739. leftmost_toskew = toskew + leftmost_fromskew;
  740. for (row = 0; ret != 0 && row < h; row += nrow)
  741. {
  742. rowstoread = th - (row + img->row_offset) % th;
  743. nrow = (row + rowstoread > h ? h - row : rowstoread);
  744. fromskew = leftmost_fromskew;
  745. this_tw = leftmost_tw;
  746. this_toskew = leftmost_toskew;
  747. tocol = 0;
  748. col = img->col_offset;
  749. while (tocol < w)
  750. {
  751. if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize, col,
  752. row + img->row_offset, 0,
  753. 0) == (tmsize_t)(-1) &&
  754. (buf == NULL || img->stoponerr))
  755. {
  756. ret = 0;
  757. break;
  758. }
  759. pos = ((row + img->row_offset) % th) * TIFFTileRowSize(tif) +
  760. ((tmsize_t)fromskew * img->samplesperpixel);
  761. if (tocol + this_tw > w)
  762. {
  763. /*
  764. * Rightmost tile is clipped on right side.
  765. */
  766. fromskew = tw - (w - tocol);
  767. this_tw = tw - fromskew;
  768. this_toskew = toskew + fromskew;
  769. }
  770. tmsize_t roffset = (tmsize_t)y * w + tocol;
  771. (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew,
  772. this_toskew, buf + pos);
  773. tocol += this_tw;
  774. col += this_tw;
  775. /*
  776. * After the leftmost tile, tiles are no longer clipped on left
  777. * side.
  778. */
  779. fromskew = 0;
  780. this_tw = tw;
  781. this_toskew = toskew;
  782. }
  783. y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);
  784. }
  785. _TIFFfreeExt(img->tif, buf);
  786. if (flip & FLIP_HORIZONTALLY)
  787. {
  788. uint32_t line;
  789. for (line = 0; line < h; line++)
  790. {
  791. uint32_t *left = raster + (line * w);
  792. uint32_t *right = left + w - 1;
  793. while (left < right)
  794. {
  795. uint32_t temp = *left;
  796. *left = *right;
  797. *right = temp;
  798. left++;
  799. right--;
  800. }
  801. }
  802. }
  803. return (ret);
  804. }
  805. /*
  806. * Get an tile-organized image that has
  807. * SamplesPerPixel > 1
  808. * PlanarConfiguration separated
  809. * We assume that all such images are RGB.
  810. */
  811. static int gtTileSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
  812. uint32_t h)
  813. {
  814. TIFF *tif = img->tif;
  815. tileSeparateRoutine put = img->put.separate;
  816. uint32_t col, row, y, rowstoread;
  817. tmsize_t pos;
  818. uint32_t tw, th;
  819. unsigned char *buf = NULL;
  820. unsigned char *p0 = NULL;
  821. unsigned char *p1 = NULL;
  822. unsigned char *p2 = NULL;
  823. unsigned char *pa = NULL;
  824. tmsize_t tilesize;
  825. tmsize_t bufsize;
  826. int32_t fromskew, toskew;
  827. int alpha = img->alpha;
  828. uint32_t nrow;
  829. int ret = 1, flip;
  830. uint16_t colorchannels;
  831. uint32_t this_tw, tocol;
  832. int32_t this_toskew, leftmost_toskew;
  833. int32_t leftmost_fromskew;
  834. uint32_t leftmost_tw;
  835. tilesize = TIFFTileSize(tif);
  836. bufsize =
  837. _TIFFMultiplySSize(tif, alpha ? 4 : 3, tilesize, "gtTileSeparate");
  838. if (bufsize == 0)
  839. {
  840. return (0);
  841. }
  842. TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  843. TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  844. flip = setorientation(img);
  845. if (flip & FLIP_VERTICALLY)
  846. {
  847. if ((tw + w) > INT_MAX)
  848. {
  849. TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
  850. "unsupported tile size (too wide)");
  851. return (0);
  852. }
  853. y = h - 1;
  854. toskew = -(int32_t)(tw + w);
  855. }
  856. else
  857. {
  858. if (tw > (INT_MAX + w))
  859. {
  860. TIFFErrorExtR(tif, TIFFFileName(tif), "%s",
  861. "unsupported tile size (too wide)");
  862. return (0);
  863. }
  864. y = 0;
  865. toskew = -(int32_t)(tw - w);
  866. }
  867. switch (img->photometric)
  868. {
  869. case PHOTOMETRIC_MINISWHITE:
  870. case PHOTOMETRIC_MINISBLACK:
  871. case PHOTOMETRIC_PALETTE:
  872. colorchannels = 1;
  873. break;
  874. default:
  875. colorchannels = 3;
  876. break;
  877. }
  878. if (tw == 0 || th == 0)
  879. {
  880. TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");
  881. return (0);
  882. }
  883. /*
  884. * Leftmost tile is clipped on left side if col_offset > 0.
  885. */
  886. leftmost_fromskew = img->col_offset % tw;
  887. leftmost_tw = tw - leftmost_fromskew;
  888. leftmost_toskew = toskew + leftmost_fromskew;
  889. for (row = 0; ret != 0 && row < h; row += nrow)
  890. {
  891. rowstoread = th - (row + img->row_offset) % th;
  892. nrow = (row + rowstoread > h ? h - row : rowstoread);
  893. fromskew = leftmost_fromskew;
  894. this_tw = leftmost_tw;
  895. this_toskew = leftmost_toskew;
  896. tocol = 0;
  897. col = img->col_offset;
  898. while (tocol < w)
  899. {
  900. if (buf == NULL)
  901. {
  902. if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize,
  903. col, row + img->row_offset, 0,
  904. 0) == (tmsize_t)(-1) &&
  905. (buf == NULL || img->stoponerr))
  906. {
  907. ret = 0;
  908. break;
  909. }
  910. p0 = buf;
  911. if (colorchannels == 1)
  912. {
  913. p2 = p1 = p0;
  914. pa = (alpha ? (p0 + 3 * tilesize) : NULL);
  915. }
  916. else
  917. {
  918. p1 = p0 + tilesize;
  919. p2 = p1 + tilesize;
  920. pa = (alpha ? (p2 + tilesize) : NULL);
  921. }
  922. }
  923. else if (TIFFReadTile(tif, p0, col, row + img->row_offset, 0, 0) ==
  924. (tmsize_t)(-1) &&
  925. img->stoponerr)
  926. {
  927. ret = 0;
  928. break;
  929. }
  930. if (colorchannels > 1 &&
  931. TIFFReadTile(tif, p1, col, row + img->row_offset, 0, 1) ==
  932. (tmsize_t)(-1) &&
  933. img->stoponerr)
  934. {
  935. ret = 0;
  936. break;
  937. }
  938. if (colorchannels > 1 &&
  939. TIFFReadTile(tif, p2, col, row + img->row_offset, 0, 2) ==
  940. (tmsize_t)(-1) &&
  941. img->stoponerr)
  942. {
  943. ret = 0;
  944. break;
  945. }
  946. if (alpha &&
  947. TIFFReadTile(tif, pa, col, row + img->row_offset, 0,
  948. colorchannels) == (tmsize_t)(-1) &&
  949. img->stoponerr)
  950. {
  951. ret = 0;
  952. break;
  953. }
  954. pos = ((row + img->row_offset) % th) * TIFFTileRowSize(tif) +
  955. ((tmsize_t)fromskew * img->samplesperpixel);
  956. if (tocol + this_tw > w)
  957. {
  958. /*
  959. * Rightmost tile is clipped on right side.
  960. */
  961. fromskew = tw - (w - tocol);
  962. this_tw = tw - fromskew;
  963. this_toskew = toskew + fromskew;
  964. }
  965. tmsize_t roffset = (tmsize_t)y * w + tocol;
  966. (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew,
  967. this_toskew, p0 + pos, p1 + pos, p2 + pos,
  968. (alpha ? (pa + pos) : NULL));
  969. tocol += this_tw;
  970. col += this_tw;
  971. /*
  972. * After the leftmost tile, tiles are no longer clipped on left
  973. * side.
  974. */
  975. fromskew = 0;
  976. this_tw = tw;
  977. this_toskew = toskew;
  978. }
  979. y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);
  980. }
  981. if (flip & FLIP_HORIZONTALLY)
  982. {
  983. uint32_t line;
  984. for (line = 0; line < h; line++)
  985. {
  986. uint32_t *left = raster + (line * w);
  987. uint32_t *right = left + w - 1;
  988. while (left < right)
  989. {
  990. uint32_t temp = *left;
  991. *left = *right;
  992. *right = temp;
  993. left++;
  994. right--;
  995. }
  996. }
  997. }
  998. _TIFFfreeExt(img->tif, buf);
  999. return (ret);
  1000. }
  1001. /*
  1002. * Get a strip-organized image that has
  1003. * PlanarConfiguration contiguous if SamplesPerPixel > 1
  1004. * or
  1005. * SamplesPerPixel == 1
  1006. */
  1007. static int gtStripContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
  1008. uint32_t h)
  1009. {
  1010. TIFF *tif = img->tif;
  1011. tileContigRoutine put = img->put.contig;
  1012. uint32_t row, y, nrow, nrowsub, rowstoread;
  1013. tmsize_t pos;
  1014. unsigned char *buf = NULL;
  1015. uint32_t rowsperstrip;
  1016. uint16_t subsamplinghor, subsamplingver;
  1017. uint32_t imagewidth = img->width;
  1018. tmsize_t scanline;
  1019. int32_t fromskew, toskew;
  1020. int ret = 1, flip;
  1021. tmsize_t maxstripsize;
  1022. TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor,
  1023. &subsamplingver);
  1024. if (subsamplingver == 0)
  1025. {
  1026. TIFFErrorExtR(tif, TIFFFileName(tif),
  1027. "Invalid vertical YCbCr subsampling");
  1028. return (0);
  1029. }
  1030. maxstripsize = TIFFStripSize(tif);
  1031. flip = setorientation(img);
  1032. if (flip & FLIP_VERTICALLY)
  1033. {
  1034. if (w > INT_MAX)
  1035. {
  1036. TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow");
  1037. return (0);
  1038. }
  1039. y = h - 1;
  1040. toskew = -(int32_t)(w + w);
  1041. }
  1042. else
  1043. {
  1044. y = 0;
  1045. toskew = -(int32_t)(w - w);
  1046. }
  1047. TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  1048. if (rowsperstrip == 0)
  1049. {
  1050. TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
  1051. return (0);
  1052. }
  1053. scanline = TIFFScanlineSize(tif);
  1054. fromskew = (w < imagewidth ? imagewidth - w : 0);
  1055. for (row = 0; row < h; row += nrow)
  1056. {
  1057. uint32_t temp;
  1058. rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
  1059. nrow = (row + rowstoread > h ? h - row : rowstoread);
  1060. nrowsub = nrow;
  1061. if ((nrowsub % subsamplingver) != 0)
  1062. nrowsub += subsamplingver - nrowsub % subsamplingver;
  1063. temp = (row + img->row_offset) % rowsperstrip + nrowsub;
  1064. if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline))
  1065. {
  1066. TIFFErrorExtR(tif, TIFFFileName(tif),
  1067. "Integer overflow in gtStripContig");
  1068. return 0;
  1069. }
  1070. if (_TIFFReadEncodedStripAndAllocBuffer(
  1071. tif, TIFFComputeStrip(tif, row + img->row_offset, 0),
  1072. (void **)(&buf), maxstripsize,
  1073. temp * scanline) == (tmsize_t)(-1) &&
  1074. (buf == NULL || img->stoponerr))
  1075. {
  1076. ret = 0;
  1077. break;
  1078. }
  1079. pos = ((row + img->row_offset) % rowsperstrip) * scanline +
  1080. ((tmsize_t)img->col_offset * img->samplesperpixel);
  1081. tmsize_t roffset = (tmsize_t)y * w;
  1082. (*put)(img, raster + roffset, 0, y, w, nrow, fromskew, toskew,
  1083. buf + pos);
  1084. y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);
  1085. }
  1086. if (flip & FLIP_HORIZONTALLY)
  1087. {
  1088. uint32_t line;
  1089. for (line = 0; line < h; line++)
  1090. {
  1091. uint32_t *left = raster + (line * w);
  1092. uint32_t *right = left + w - 1;
  1093. while (left < right)
  1094. {
  1095. uint32_t temp = *left;
  1096. *left = *right;
  1097. *right = temp;
  1098. left++;
  1099. right--;
  1100. }
  1101. }
  1102. }
  1103. _TIFFfreeExt(img->tif, buf);
  1104. return (ret);
  1105. }
  1106. /*
  1107. * Get a strip-organized image with
  1108. * SamplesPerPixel > 1
  1109. * PlanarConfiguration separated
  1110. * We assume that all such images are RGB.
  1111. */
  1112. static int gtStripSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
  1113. uint32_t h)
  1114. {
  1115. TIFF *tif = img->tif;
  1116. tileSeparateRoutine put = img->put.separate;
  1117. unsigned char *buf = NULL;
  1118. unsigned char *p0 = NULL, *p1 = NULL, *p2 = NULL, *pa = NULL;
  1119. uint32_t row, y, nrow, rowstoread;
  1120. tmsize_t pos;
  1121. tmsize_t scanline;
  1122. uint32_t rowsperstrip, offset_row;
  1123. uint32_t imagewidth = img->width;
  1124. tmsize_t stripsize;
  1125. tmsize_t bufsize;
  1126. int32_t fromskew, toskew;
  1127. int alpha = img->alpha;
  1128. int ret = 1, flip;
  1129. uint16_t colorchannels;
  1130. stripsize = TIFFStripSize(tif);
  1131. bufsize =
  1132. _TIFFMultiplySSize(tif, alpha ? 4 : 3, stripsize, "gtStripSeparate");
  1133. if (bufsize == 0)
  1134. {
  1135. return (0);
  1136. }
  1137. flip = setorientation(img);
  1138. if (flip & FLIP_VERTICALLY)
  1139. {
  1140. if (w > INT_MAX)
  1141. {
  1142. TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow");
  1143. return (0);
  1144. }
  1145. y = h - 1;
  1146. toskew = -(int32_t)(w + w);
  1147. }
  1148. else
  1149. {
  1150. y = 0;
  1151. toskew = -(int32_t)(w - w);
  1152. }
  1153. switch (img->photometric)
  1154. {
  1155. case PHOTOMETRIC_MINISWHITE:
  1156. case PHOTOMETRIC_MINISBLACK:
  1157. case PHOTOMETRIC_PALETTE:
  1158. colorchannels = 1;
  1159. break;
  1160. default:
  1161. colorchannels = 3;
  1162. break;
  1163. }
  1164. TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  1165. if (rowsperstrip == 0)
  1166. {
  1167. TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
  1168. return (0);
  1169. }
  1170. scanline = TIFFScanlineSize(tif);
  1171. fromskew = (w < imagewidth ? imagewidth - w : 0);
  1172. for (row = 0; row < h; row += nrow)
  1173. {
  1174. uint32_t temp;
  1175. rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
  1176. nrow = (row + rowstoread > h ? h - row : rowstoread);
  1177. offset_row = row + img->row_offset;
  1178. temp = (row + img->row_offset) % rowsperstrip + nrow;
  1179. if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline))
  1180. {
  1181. TIFFErrorExtR(tif, TIFFFileName(tif),
  1182. "Integer overflow in gtStripSeparate");
  1183. return 0;
  1184. }
  1185. if (buf == NULL)
  1186. {
  1187. if (_TIFFReadEncodedStripAndAllocBuffer(
  1188. tif, TIFFComputeStrip(tif, offset_row, 0), (void **)&buf,
  1189. bufsize, temp * scanline) == (tmsize_t)(-1) &&
  1190. (buf == NULL || img->stoponerr))
  1191. {
  1192. ret = 0;
  1193. break;
  1194. }
  1195. p0 = buf;
  1196. if (colorchannels == 1)
  1197. {
  1198. p2 = p1 = p0;
  1199. pa = (alpha ? (p0 + 3 * stripsize) : NULL);
  1200. }
  1201. else
  1202. {
  1203. p1 = p0 + stripsize;
  1204. p2 = p1 + stripsize;
  1205. pa = (alpha ? (p2 + stripsize) : NULL);
  1206. }
  1207. }
  1208. else if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
  1209. p0, temp * scanline) == (tmsize_t)(-1) &&
  1210. img->stoponerr)
  1211. {
  1212. ret = 0;
  1213. break;
  1214. }
  1215. if (colorchannels > 1 &&
  1216. TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1), p1,
  1217. temp * scanline) == (tmsize_t)(-1) &&
  1218. img->stoponerr)
  1219. {
  1220. ret = 0;
  1221. break;
  1222. }
  1223. if (colorchannels > 1 &&
  1224. TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2), p2,
  1225. temp * scanline) == (tmsize_t)(-1) &&
  1226. img->stoponerr)
  1227. {
  1228. ret = 0;
  1229. break;
  1230. }
  1231. if (alpha)
  1232. {
  1233. if (TIFFReadEncodedStrip(
  1234. tif, TIFFComputeStrip(tif, offset_row, colorchannels), pa,
  1235. temp * scanline) == (tmsize_t)(-1) &&
  1236. img->stoponerr)
  1237. {
  1238. ret = 0;
  1239. break;
  1240. }
  1241. }
  1242. pos = ((row + img->row_offset) % rowsperstrip) * scanline +
  1243. ((tmsize_t)img->col_offset * img->samplesperpixel);
  1244. tmsize_t roffset = (tmsize_t)y * w;
  1245. (*put)(img, raster + roffset, 0, y, w, nrow, fromskew, toskew, p0 + pos,
  1246. p1 + pos, p2 + pos, (alpha ? (pa + pos) : NULL));
  1247. y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);
  1248. }
  1249. if (flip & FLIP_HORIZONTALLY)
  1250. {
  1251. uint32_t line;
  1252. for (line = 0; line < h; line++)
  1253. {
  1254. uint32_t *left = raster + (line * w);
  1255. uint32_t *right = left + w - 1;
  1256. while (left < right)
  1257. {
  1258. uint32_t temp = *left;
  1259. *left = *right;
  1260. *right = temp;
  1261. left++;
  1262. right--;
  1263. }
  1264. }
  1265. }
  1266. _TIFFfreeExt(img->tif, buf);
  1267. return (ret);
  1268. }
  1269. /*
  1270. * The following routines move decoded data returned
  1271. * from the TIFF library into rasters filled with packed
  1272. * ABGR pixels (i.e. suitable for passing to lrecwrite.)
  1273. *
  1274. * The routines have been created according to the most
  1275. * important cases and optimized. PickContigCase and
  1276. * PickSeparateCase analyze the parameters and select
  1277. * the appropriate "get" and "put" routine to use.
  1278. */
  1279. #define REPEAT8(op) \
  1280. REPEAT4(op); \
  1281. REPEAT4(op)
  1282. #define REPEAT4(op) \
  1283. REPEAT2(op); \
  1284. REPEAT2(op)
  1285. #define REPEAT2(op) \
  1286. op; \
  1287. op
  1288. #define CASE8(x, op) \
  1289. switch (x) \
  1290. { \
  1291. case 7: \
  1292. op; /*-fallthrough*/ \
  1293. case 6: \
  1294. op; /*-fallthrough*/ \
  1295. case 5: \
  1296. op; /*-fallthrough*/ \
  1297. case 4: \
  1298. op; /*-fallthrough*/ \
  1299. case 3: \
  1300. op; /*-fallthrough*/ \
  1301. case 2: \
  1302. op; /*-fallthrough*/ \
  1303. case 1: \
  1304. op; \
  1305. }
  1306. #define CASE4(x, op) \
  1307. switch (x) \
  1308. { \
  1309. case 3: \
  1310. op; /*-fallthrough*/ \
  1311. case 2: \
  1312. op; /*-fallthrough*/ \
  1313. case 1: \
  1314. op; \
  1315. }
  1316. #define NOP
  1317. #define UNROLL8(w, op1, op2) \
  1318. { \
  1319. uint32_t _x; \
  1320. for (_x = w; _x >= 8; _x -= 8) \
  1321. { \
  1322. op1; \
  1323. REPEAT8(op2); \
  1324. } \
  1325. if (_x > 0) \
  1326. { \
  1327. op1; \
  1328. CASE8(_x, op2); \
  1329. } \
  1330. }
  1331. #define UNROLL4(w, op1, op2) \
  1332. { \
  1333. uint32_t _x; \
  1334. for (_x = w; _x >= 4; _x -= 4) \
  1335. { \
  1336. op1; \
  1337. REPEAT4(op2); \
  1338. } \
  1339. if (_x > 0) \
  1340. { \
  1341. op1; \
  1342. CASE4(_x, op2); \
  1343. } \
  1344. }
  1345. #define UNROLL2(w, op1, op2) \
  1346. { \
  1347. uint32_t _x; \
  1348. for (_x = w; _x >= 2; _x -= 2) \
  1349. { \
  1350. op1; \
  1351. REPEAT2(op2); \
  1352. } \
  1353. if (_x) \
  1354. { \
  1355. op1; \
  1356. op2; \
  1357. } \
  1358. }
  1359. #define SKEW(r, g, b, skew) \
  1360. { \
  1361. r += skew; \
  1362. g += skew; \
  1363. b += skew; \
  1364. }
  1365. #define SKEW4(r, g, b, a, skew) \
  1366. { \
  1367. r += skew; \
  1368. g += skew; \
  1369. b += skew; \
  1370. a += skew; \
  1371. }
  1372. #define A1 (((uint32_t)0xffL) << 24)
  1373. #define PACK(r, g, b) \
  1374. ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | A1)
  1375. #define PACK4(r, g, b, a) \
  1376. ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | \
  1377. ((uint32_t)(a) << 24))
  1378. #define W2B(v) (((v) >> 8) & 0xff)
  1379. /* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
  1380. #define PACKW(r, g, b) \
  1381. ((uint32_t)W2B(r) | ((uint32_t)W2B(g) << 8) | ((uint32_t)W2B(b) << 16) | A1)
  1382. #define PACKW4(r, g, b, a) \
  1383. ((uint32_t)W2B(r) | ((uint32_t)W2B(g) << 8) | ((uint32_t)W2B(b) << 16) | \
  1384. ((uint32_t)W2B(a) << 24))
  1385. #define DECLAREContigPutFunc(name) \
  1386. static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \
  1387. uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \
  1388. unsigned char *pp)
  1389. /*
  1390. * 8-bit palette => colormap/RGB
  1391. */
  1392. DECLAREContigPutFunc(put8bitcmaptile)
  1393. {
  1394. uint32_t **PALmap = img->PALmap;
  1395. int samplesperpixel = img->samplesperpixel;
  1396. (void)y;
  1397. for (; h > 0; --h)
  1398. {
  1399. for (x = w; x > 0; --x)
  1400. {
  1401. *cp++ = PALmap[*pp][0];
  1402. pp += samplesperpixel;
  1403. }
  1404. cp += toskew;
  1405. pp += fromskew;
  1406. }
  1407. }
  1408. /*
  1409. * 4-bit palette => colormap/RGB
  1410. */
  1411. DECLAREContigPutFunc(put4bitcmaptile)
  1412. {
  1413. uint32_t **PALmap = img->PALmap;
  1414. (void)x;
  1415. (void)y;
  1416. fromskew /= 2;
  1417. for (; h > 0; --h)
  1418. {
  1419. uint32_t *bw;
  1420. UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
  1421. cp += toskew;
  1422. pp += fromskew;
  1423. }
  1424. }
  1425. /*
  1426. * 2-bit palette => colormap/RGB
  1427. */
  1428. DECLAREContigPutFunc(put2bitcmaptile)
  1429. {
  1430. uint32_t **PALmap = img->PALmap;
  1431. (void)x;
  1432. (void)y;
  1433. fromskew /= 4;
  1434. for (; h > 0; --h)
  1435. {
  1436. uint32_t *bw;
  1437. UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
  1438. cp += toskew;
  1439. pp += fromskew;
  1440. }
  1441. }
  1442. /*
  1443. * 1-bit palette => colormap/RGB
  1444. */
  1445. DECLAREContigPutFunc(put1bitcmaptile)
  1446. {
  1447. uint32_t **PALmap = img->PALmap;
  1448. (void)x;
  1449. (void)y;
  1450. fromskew /= 8;
  1451. for (; h > 0; --h)
  1452. {
  1453. uint32_t *bw;
  1454. UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
  1455. cp += toskew;
  1456. pp += fromskew;
  1457. }
  1458. }
  1459. /*
  1460. * 8-bit greyscale => colormap/RGB
  1461. */
  1462. DECLAREContigPutFunc(putgreytile)
  1463. {
  1464. int samplesperpixel = img->samplesperpixel;
  1465. uint32_t **BWmap = img->BWmap;
  1466. (void)y;
  1467. for (; h > 0; --h)
  1468. {
  1469. for (x = w; x > 0; --x)
  1470. {
  1471. *cp++ = BWmap[*pp][0];
  1472. pp += samplesperpixel;
  1473. }
  1474. cp += toskew;
  1475. pp += fromskew;
  1476. }
  1477. }
  1478. /*
  1479. * 8-bit greyscale with associated alpha => colormap/RGBA
  1480. */
  1481. DECLAREContigPutFunc(putagreytile)
  1482. {
  1483. int samplesperpixel = img->samplesperpixel;
  1484. uint32_t **BWmap = img->BWmap;
  1485. (void)y;
  1486. for (; h > 0; --h)
  1487. {
  1488. for (x = w; x > 0; --x)
  1489. {
  1490. *cp++ = BWmap[*pp][0] & ((uint32_t) * (pp + 1) << 24 | ~A1);
  1491. pp += samplesperpixel;
  1492. }
  1493. cp += toskew;
  1494. pp += fromskew;
  1495. }
  1496. }
  1497. /*
  1498. * 16-bit greyscale => colormap/RGB
  1499. */
  1500. DECLAREContigPutFunc(put16bitbwtile)
  1501. {
  1502. int samplesperpixel = img->samplesperpixel;
  1503. uint32_t **BWmap = img->BWmap;
  1504. (void)y;
  1505. for (; h > 0; --h)
  1506. {
  1507. uint16_t *wp = (uint16_t *)pp;
  1508. for (x = w; x > 0; --x)
  1509. {
  1510. /* use high order byte of 16bit value */
  1511. *cp++ = BWmap[*wp >> 8][0];
  1512. pp += 2 * samplesperpixel;
  1513. wp += samplesperpixel;
  1514. }
  1515. cp += toskew;
  1516. pp += fromskew;
  1517. }
  1518. }
  1519. /*
  1520. * 1-bit bilevel => colormap/RGB
  1521. */
  1522. DECLAREContigPutFunc(put1bitbwtile)
  1523. {
  1524. uint32_t **BWmap = img->BWmap;
  1525. (void)x;
  1526. (void)y;
  1527. fromskew /= 8;
  1528. for (; h > 0; --h)
  1529. {
  1530. uint32_t *bw;
  1531. UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
  1532. cp += toskew;
  1533. pp += fromskew;
  1534. }
  1535. }
  1536. /*
  1537. * 2-bit greyscale => colormap/RGB
  1538. */
  1539. DECLAREContigPutFunc(put2bitbwtile)
  1540. {
  1541. uint32_t **BWmap = img->BWmap;
  1542. (void)x;
  1543. (void)y;
  1544. fromskew /= 4;
  1545. for (; h > 0; --h)
  1546. {
  1547. uint32_t *bw;
  1548. UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
  1549. cp += toskew;
  1550. pp += fromskew;
  1551. }
  1552. }
  1553. /*
  1554. * 4-bit greyscale => colormap/RGB
  1555. */
  1556. DECLAREContigPutFunc(put4bitbwtile)
  1557. {
  1558. uint32_t **BWmap = img->BWmap;
  1559. (void)x;
  1560. (void)y;
  1561. fromskew /= 2;
  1562. for (; h > 0; --h)
  1563. {
  1564. uint32_t *bw;
  1565. UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
  1566. cp += toskew;
  1567. pp += fromskew;
  1568. }
  1569. }
  1570. /*
  1571. * 8-bit packed samples, no Map => RGB
  1572. */
  1573. DECLAREContigPutFunc(putRGBcontig8bittile)
  1574. {
  1575. int samplesperpixel = img->samplesperpixel;
  1576. (void)x;
  1577. (void)y;
  1578. fromskew *= samplesperpixel;
  1579. for (; h > 0; --h)
  1580. {
  1581. UNROLL8(w, NOP, *cp++ = PACK(pp[0], pp[1], pp[2]);
  1582. pp += samplesperpixel);
  1583. cp += toskew;
  1584. pp += fromskew;
  1585. }
  1586. }
  1587. /*
  1588. * 8-bit packed samples => RGBA w/ associated alpha
  1589. * (known to have Map == NULL)
  1590. */
  1591. DECLAREContigPutFunc(putRGBAAcontig8bittile)
  1592. {
  1593. int samplesperpixel = img->samplesperpixel;
  1594. (void)x;
  1595. (void)y;
  1596. fromskew *= samplesperpixel;
  1597. for (; h > 0; --h)
  1598. {
  1599. UNROLL8(w, NOP, *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
  1600. pp += samplesperpixel);
  1601. cp += toskew;
  1602. pp += fromskew;
  1603. }
  1604. }
  1605. /*
  1606. * 8-bit packed samples => RGBA w/ unassociated alpha
  1607. * (known to have Map == NULL)
  1608. */
  1609. DECLAREContigPutFunc(putRGBUAcontig8bittile)
  1610. {
  1611. int samplesperpixel = img->samplesperpixel;
  1612. (void)y;
  1613. fromskew *= samplesperpixel;
  1614. for (; h > 0; --h)
  1615. {
  1616. uint32_t r, g, b, a;
  1617. uint8_t *m;
  1618. for (x = w; x > 0; --x)
  1619. {
  1620. a = pp[3];
  1621. m = img->UaToAa + ((size_t)a << 8);
  1622. r = m[pp[0]];
  1623. g = m[pp[1]];
  1624. b = m[pp[2]];
  1625. *cp++ = PACK4(r, g, b, a);
  1626. pp += samplesperpixel;
  1627. }
  1628. cp += toskew;
  1629. pp += fromskew;
  1630. }
  1631. }
  1632. /*
  1633. * 16-bit packed samples => RGB
  1634. */
  1635. DECLAREContigPutFunc(putRGBcontig16bittile)
  1636. {
  1637. int samplesperpixel = img->samplesperpixel;
  1638. uint16_t *wp = (uint16_t *)pp;
  1639. (void)y;
  1640. fromskew *= samplesperpixel;
  1641. for (; h > 0; --h)
  1642. {
  1643. for (x = w; x > 0; --x)
  1644. {
  1645. *cp++ = PACK(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],
  1646. img->Bitdepth16To8[wp[2]]);
  1647. wp += samplesperpixel;
  1648. }
  1649. cp += toskew;
  1650. wp += fromskew;
  1651. }
  1652. }
  1653. /*
  1654. * 16-bit packed samples => RGBA w/ associated alpha
  1655. * (known to have Map == NULL)
  1656. */
  1657. DECLAREContigPutFunc(putRGBAAcontig16bittile)
  1658. {
  1659. int samplesperpixel = img->samplesperpixel;
  1660. uint16_t *wp = (uint16_t *)pp;
  1661. (void)y;
  1662. fromskew *= samplesperpixel;
  1663. for (; h > 0; --h)
  1664. {
  1665. for (x = w; x > 0; --x)
  1666. {
  1667. *cp++ = PACK4(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],
  1668. img->Bitdepth16To8[wp[2]], img->Bitdepth16To8[wp[3]]);
  1669. wp += samplesperpixel;
  1670. }
  1671. cp += toskew;
  1672. wp += fromskew;
  1673. }
  1674. }
  1675. /*
  1676. * 16-bit packed samples => RGBA w/ unassociated alpha
  1677. * (known to have Map == NULL)
  1678. */
  1679. DECLAREContigPutFunc(putRGBUAcontig16bittile)
  1680. {
  1681. int samplesperpixel = img->samplesperpixel;
  1682. uint16_t *wp = (uint16_t *)pp;
  1683. (void)y;
  1684. fromskew *= samplesperpixel;
  1685. for (; h > 0; --h)
  1686. {
  1687. uint32_t r, g, b, a;
  1688. uint8_t *m;
  1689. for (x = w; x > 0; --x)
  1690. {
  1691. a = img->Bitdepth16To8[wp[3]];
  1692. m = img->UaToAa + ((size_t)a << 8);
  1693. r = m[img->Bitdepth16To8[wp[0]]];
  1694. g = m[img->Bitdepth16To8[wp[1]]];
  1695. b = m[img->Bitdepth16To8[wp[2]]];
  1696. *cp++ = PACK4(r, g, b, a);
  1697. wp += samplesperpixel;
  1698. }
  1699. cp += toskew;
  1700. wp += fromskew;
  1701. }
  1702. }
  1703. /*
  1704. * 8-bit packed CMYK samples w/o Map => RGB
  1705. *
  1706. * NB: The conversion of CMYK->RGB is *very* crude.
  1707. */
  1708. DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
  1709. {
  1710. int samplesperpixel = img->samplesperpixel;
  1711. uint16_t r, g, b, k;
  1712. (void)x;
  1713. (void)y;
  1714. fromskew *= samplesperpixel;
  1715. for (; h > 0; --h)
  1716. {
  1717. UNROLL8(w, NOP, k = 255 - pp[3]; r = (k * (255 - pp[0])) / 255;
  1718. g = (k * (255 - pp[1])) / 255; b = (k * (255 - pp[2])) / 255;
  1719. *cp++ = PACK(r, g, b); pp += samplesperpixel);
  1720. cp += toskew;
  1721. pp += fromskew;
  1722. }
  1723. }
  1724. /*
  1725. * 8-bit packed CMYK samples w/Map => RGB
  1726. *
  1727. * NB: The conversion of CMYK->RGB is *very* crude.
  1728. */
  1729. DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
  1730. {
  1731. int samplesperpixel = img->samplesperpixel;
  1732. TIFFRGBValue *Map = img->Map;
  1733. uint16_t r, g, b, k;
  1734. (void)y;
  1735. fromskew *= samplesperpixel;
  1736. for (; h > 0; --h)
  1737. {
  1738. for (x = w; x > 0; --x)
  1739. {
  1740. k = 255 - pp[3];
  1741. r = (k * (255 - pp[0])) / 255;
  1742. g = (k * (255 - pp[1])) / 255;
  1743. b = (k * (255 - pp[2])) / 255;
  1744. *cp++ = PACK(Map[r], Map[g], Map[b]);
  1745. pp += samplesperpixel;
  1746. }
  1747. pp += fromskew;
  1748. cp += toskew;
  1749. }
  1750. }
  1751. #define DECLARESepPutFunc(name) \
  1752. static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \
  1753. uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \
  1754. unsigned char *r, unsigned char *g, unsigned char *b, \
  1755. unsigned char *a)
  1756. /*
  1757. * 8-bit unpacked samples => RGB
  1758. */
  1759. DECLARESepPutFunc(putRGBseparate8bittile)
  1760. {
  1761. (void)img;
  1762. (void)x;
  1763. (void)y;
  1764. (void)a;
  1765. for (; h > 0; --h)
  1766. {
  1767. UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
  1768. SKEW(r, g, b, fromskew);
  1769. cp += toskew;
  1770. }
  1771. }
  1772. /*
  1773. * 8-bit unpacked samples => RGBA w/ associated alpha
  1774. */
  1775. DECLARESepPutFunc(putRGBAAseparate8bittile)
  1776. {
  1777. (void)img;
  1778. (void)x;
  1779. (void)y;
  1780. for (; h > 0; --h)
  1781. {
  1782. UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
  1783. SKEW4(r, g, b, a, fromskew);
  1784. cp += toskew;
  1785. }
  1786. }
  1787. /*
  1788. * 8-bit unpacked CMYK samples => RGBA
  1789. */
  1790. DECLARESepPutFunc(putCMYKseparate8bittile)
  1791. {
  1792. (void)img;
  1793. (void)y;
  1794. for (; h > 0; --h)
  1795. {
  1796. uint32_t rv, gv, bv, kv;
  1797. for (x = w; x > 0; --x)
  1798. {
  1799. kv = 255 - *a++;
  1800. rv = (kv * (255 - *r++)) / 255;
  1801. gv = (kv * (255 - *g++)) / 255;
  1802. bv = (kv * (255 - *b++)) / 255;
  1803. *cp++ = PACK4(rv, gv, bv, 255);
  1804. }
  1805. SKEW4(r, g, b, a, fromskew);
  1806. cp += toskew;
  1807. }
  1808. }
  1809. /*
  1810. * 8-bit unpacked samples => RGBA w/ unassociated alpha
  1811. */
  1812. DECLARESepPutFunc(putRGBUAseparate8bittile)
  1813. {
  1814. (void)img;
  1815. (void)y;
  1816. for (; h > 0; --h)
  1817. {
  1818. uint32_t rv, gv, bv, av;
  1819. uint8_t *m;
  1820. for (x = w; x > 0; --x)
  1821. {
  1822. av = *a++;
  1823. m = img->UaToAa + ((size_t)av << 8);
  1824. rv = m[*r++];
  1825. gv = m[*g++];
  1826. bv = m[*b++];
  1827. *cp++ = PACK4(rv, gv, bv, av);
  1828. }
  1829. SKEW4(r, g, b, a, fromskew);
  1830. cp += toskew;
  1831. }
  1832. }
  1833. /*
  1834. * 16-bit unpacked samples => RGB
  1835. */
  1836. DECLARESepPutFunc(putRGBseparate16bittile)
  1837. {
  1838. uint16_t *wr = (uint16_t *)r;
  1839. uint16_t *wg = (uint16_t *)g;
  1840. uint16_t *wb = (uint16_t *)b;
  1841. (void)img;
  1842. (void)y;
  1843. (void)a;
  1844. for (; h > 0; --h)
  1845. {
  1846. for (x = 0; x < w; x++)
  1847. *cp++ = PACK(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],
  1848. img->Bitdepth16To8[*wb++]);
  1849. SKEW(wr, wg, wb, fromskew);
  1850. cp += toskew;
  1851. }
  1852. }
  1853. /*
  1854. * 16-bit unpacked samples => RGBA w/ associated alpha
  1855. */
  1856. DECLARESepPutFunc(putRGBAAseparate16bittile)
  1857. {
  1858. uint16_t *wr = (uint16_t *)r;
  1859. uint16_t *wg = (uint16_t *)g;
  1860. uint16_t *wb = (uint16_t *)b;
  1861. uint16_t *wa = (uint16_t *)a;
  1862. (void)img;
  1863. (void)y;
  1864. for (; h > 0; --h)
  1865. {
  1866. for (x = 0; x < w; x++)
  1867. *cp++ = PACK4(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],
  1868. img->Bitdepth16To8[*wb++], img->Bitdepth16To8[*wa++]);
  1869. SKEW4(wr, wg, wb, wa, fromskew);
  1870. cp += toskew;
  1871. }
  1872. }
  1873. /*
  1874. * 16-bit unpacked samples => RGBA w/ unassociated alpha
  1875. */
  1876. DECLARESepPutFunc(putRGBUAseparate16bittile)
  1877. {
  1878. uint16_t *wr = (uint16_t *)r;
  1879. uint16_t *wg = (uint16_t *)g;
  1880. uint16_t *wb = (uint16_t *)b;
  1881. uint16_t *wa = (uint16_t *)a;
  1882. (void)img;
  1883. (void)y;
  1884. for (; h > 0; --h)
  1885. {
  1886. uint32_t r2, g2, b2, a2;
  1887. uint8_t *m;
  1888. for (x = w; x > 0; --x)
  1889. {
  1890. a2 = img->Bitdepth16To8[*wa++];
  1891. m = img->UaToAa + ((size_t)a2 << 8);
  1892. r2 = m[img->Bitdepth16To8[*wr++]];
  1893. g2 = m[img->Bitdepth16To8[*wg++]];
  1894. b2 = m[img->Bitdepth16To8[*wb++]];
  1895. *cp++ = PACK4(r2, g2, b2, a2);
  1896. }
  1897. SKEW4(wr, wg, wb, wa, fromskew);
  1898. cp += toskew;
  1899. }
  1900. }
  1901. /*
  1902. * 8-bit packed CIE L*a*b 1976 samples => RGB
  1903. */
  1904. DECLAREContigPutFunc(putcontig8bitCIELab8)
  1905. {
  1906. float X, Y, Z;
  1907. uint32_t r, g, b;
  1908. (void)y;
  1909. fromskew *= 3;
  1910. for (; h > 0; --h)
  1911. {
  1912. for (x = w; x > 0; --x)
  1913. {
  1914. TIFFCIELabToXYZ(img->cielab, (unsigned char)pp[0],
  1915. (signed char)pp[1], (signed char)pp[2], &X, &Y, &Z);
  1916. TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
  1917. *cp++ = PACK(r, g, b);
  1918. pp += 3;
  1919. }
  1920. cp += toskew;
  1921. pp += fromskew;
  1922. }
  1923. }
  1924. /*
  1925. * 16-bit packed CIE L*a*b 1976 samples => RGB
  1926. */
  1927. DECLAREContigPutFunc(putcontig8bitCIELab16)
  1928. {
  1929. float X, Y, Z;
  1930. uint32_t r, g, b;
  1931. uint16_t *wp = (uint16_t *)pp;
  1932. (void)y;
  1933. fromskew *= 3;
  1934. for (; h > 0; --h)
  1935. {
  1936. for (x = w; x > 0; --x)
  1937. {
  1938. TIFFCIELab16ToXYZ(img->cielab, (uint16_t)wp[0], (int16_t)wp[1],
  1939. (int16_t)wp[2], &X, &Y, &Z);
  1940. TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
  1941. *cp++ = PACK(r, g, b);
  1942. wp += 3;
  1943. }
  1944. cp += toskew;
  1945. wp += fromskew;
  1946. }
  1947. }
  1948. /*
  1949. * YCbCr -> RGB conversion and packing routines.
  1950. */
  1951. #define YCbCrtoRGB(dst, Y) \
  1952. { \
  1953. uint32_t r, g, b; \
  1954. TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b); \
  1955. dst = PACK(r, g, b); \
  1956. }
  1957. /*
  1958. * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
  1959. */
  1960. DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
  1961. {
  1962. uint32_t *cp1 = cp + w + toskew;
  1963. uint32_t *cp2 = cp1 + w + toskew;
  1964. uint32_t *cp3 = cp2 + w + toskew;
  1965. int32_t incr = 3 * w + 4 * toskew;
  1966. (void)y;
  1967. /* adjust fromskew */
  1968. fromskew = (fromskew / 4) * (4 * 2 + 2);
  1969. if ((h & 3) == 0 && (w & 3) == 0)
  1970. {
  1971. for (; h >= 4; h -= 4)
  1972. {
  1973. x = w >> 2;
  1974. do
  1975. {
  1976. int32_t Cb = pp[16];
  1977. int32_t Cr = pp[17];
  1978. YCbCrtoRGB(cp[0], pp[0]);
  1979. YCbCrtoRGB(cp[1], pp[1]);
  1980. YCbCrtoRGB(cp[2], pp[2]);
  1981. YCbCrtoRGB(cp[3], pp[3]);
  1982. YCbCrtoRGB(cp1[0], pp[4]);
  1983. YCbCrtoRGB(cp1[1], pp[5]);
  1984. YCbCrtoRGB(cp1[2], pp[6]);
  1985. YCbCrtoRGB(cp1[3], pp[7]);
  1986. YCbCrtoRGB(cp2[0], pp[8]);
  1987. YCbCrtoRGB(cp2[1], pp[9]);
  1988. YCbCrtoRGB(cp2[2], pp[10]);
  1989. YCbCrtoRGB(cp2[3], pp[11]);
  1990. YCbCrtoRGB(cp3[0], pp[12]);
  1991. YCbCrtoRGB(cp3[1], pp[13]);
  1992. YCbCrtoRGB(cp3[2], pp[14]);
  1993. YCbCrtoRGB(cp3[3], pp[15]);
  1994. cp += 4;
  1995. cp1 += 4;
  1996. cp2 += 4;
  1997. cp3 += 4;
  1998. pp += 18;
  1999. } while (--x);
  2000. cp += incr;
  2001. cp1 += incr;
  2002. cp2 += incr;
  2003. cp3 += incr;
  2004. pp += fromskew;
  2005. }
  2006. }
  2007. else
  2008. {
  2009. while (h > 0)
  2010. {
  2011. for (x = w; x > 0;)
  2012. {
  2013. int32_t Cb = pp[16];
  2014. int32_t Cr = pp[17];
  2015. switch (x)
  2016. {
  2017. default:
  2018. switch (h)
  2019. {
  2020. default:
  2021. YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
  2022. case 3:
  2023. YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
  2024. case 2:
  2025. YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
  2026. case 1:
  2027. YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
  2028. } /* FALLTHROUGH */
  2029. case 3:
  2030. switch (h)
  2031. {
  2032. default:
  2033. YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
  2034. case 3:
  2035. YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
  2036. case 2:
  2037. YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
  2038. case 1:
  2039. YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
  2040. } /* FALLTHROUGH */
  2041. case 2:
  2042. switch (h)
  2043. {
  2044. default:
  2045. YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
  2046. case 3:
  2047. YCbCrtoRGB(cp2[1], pp[9]); /* FALLTHROUGH */
  2048. case 2:
  2049. YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
  2050. case 1:
  2051. YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
  2052. } /* FALLTHROUGH */
  2053. case 1:
  2054. switch (h)
  2055. {
  2056. default:
  2057. YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
  2058. case 3:
  2059. YCbCrtoRGB(cp2[0], pp[8]); /* FALLTHROUGH */
  2060. case 2:
  2061. YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
  2062. case 1:
  2063. YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
  2064. } /* FALLTHROUGH */
  2065. }
  2066. if (x < 4)
  2067. {
  2068. cp += x;
  2069. cp1 += x;
  2070. cp2 += x;
  2071. cp3 += x;
  2072. x = 0;
  2073. }
  2074. else
  2075. {
  2076. cp += 4;
  2077. cp1 += 4;
  2078. cp2 += 4;
  2079. cp3 += 4;
  2080. x -= 4;
  2081. }
  2082. pp += 18;
  2083. }
  2084. if (h <= 4)
  2085. break;
  2086. h -= 4;
  2087. cp += incr;
  2088. cp1 += incr;
  2089. cp2 += incr;
  2090. cp3 += incr;
  2091. pp += fromskew;
  2092. }
  2093. }
  2094. }
  2095. /*
  2096. * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
  2097. */
  2098. DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
  2099. {
  2100. uint32_t *cp1 = cp + w + toskew;
  2101. int32_t incr = 2 * toskew + w;
  2102. (void)y;
  2103. fromskew = (fromskew / 4) * (4 * 2 + 2);
  2104. if ((w & 3) == 0 && (h & 1) == 0)
  2105. {
  2106. for (; h >= 2; h -= 2)
  2107. {
  2108. x = w >> 2;
  2109. do
  2110. {
  2111. int32_t Cb = pp[8];
  2112. int32_t Cr = pp[9];
  2113. YCbCrtoRGB(cp[0], pp[0]);
  2114. YCbCrtoRGB(cp[1], pp[1]);
  2115. YCbCrtoRGB(cp[2], pp[2]);
  2116. YCbCrtoRGB(cp[3], pp[3]);
  2117. YCbCrtoRGB(cp1[0], pp[4]);
  2118. YCbCrtoRGB(cp1[1], pp[5]);
  2119. YCbCrtoRGB(cp1[2], pp[6]);
  2120. YCbCrtoRGB(cp1[3], pp[7]);
  2121. cp += 4;
  2122. cp1 += 4;
  2123. pp += 10;
  2124. } while (--x);
  2125. cp += incr;
  2126. cp1 += incr;
  2127. pp += fromskew;
  2128. }
  2129. }
  2130. else
  2131. {
  2132. while (h > 0)
  2133. {
  2134. for (x = w; x > 0;)
  2135. {
  2136. int32_t Cb = pp[8];
  2137. int32_t Cr = pp[9];
  2138. switch (x)
  2139. {
  2140. default:
  2141. switch (h)
  2142. {
  2143. default:
  2144. YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */
  2145. case 1:
  2146. YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */
  2147. } /* FALLTHROUGH */
  2148. case 3:
  2149. switch (h)
  2150. {
  2151. default:
  2152. YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */
  2153. case 1:
  2154. YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */
  2155. } /* FALLTHROUGH */
  2156. case 2:
  2157. switch (h)
  2158. {
  2159. default:
  2160. YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */
  2161. case 1:
  2162. YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */
  2163. } /* FALLTHROUGH */
  2164. case 1:
  2165. switch (h)
  2166. {
  2167. default:
  2168. YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */
  2169. case 1:
  2170. YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */
  2171. } /* FALLTHROUGH */
  2172. }
  2173. if (x < 4)
  2174. {
  2175. cp += x;
  2176. cp1 += x;
  2177. x = 0;
  2178. }
  2179. else
  2180. {
  2181. cp += 4;
  2182. cp1 += 4;
  2183. x -= 4;
  2184. }
  2185. pp += 10;
  2186. }
  2187. if (h <= 2)
  2188. break;
  2189. h -= 2;
  2190. cp += incr;
  2191. cp1 += incr;
  2192. pp += fromskew;
  2193. }
  2194. }
  2195. }
  2196. /*
  2197. * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
  2198. */
  2199. DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
  2200. {
  2201. (void)y;
  2202. fromskew = (fromskew / 4) * (4 * 1 + 2);
  2203. do
  2204. {
  2205. x = w >> 2;
  2206. while (x > 0)
  2207. {
  2208. int32_t Cb = pp[4];
  2209. int32_t Cr = pp[5];
  2210. YCbCrtoRGB(cp[0], pp[0]);
  2211. YCbCrtoRGB(cp[1], pp[1]);
  2212. YCbCrtoRGB(cp[2], pp[2]);
  2213. YCbCrtoRGB(cp[3], pp[3]);
  2214. cp += 4;
  2215. pp += 6;
  2216. x--;
  2217. }
  2218. if ((w & 3) != 0)
  2219. {
  2220. int32_t Cb = pp[4];
  2221. int32_t Cr = pp[5];
  2222. switch ((w & 3))
  2223. {
  2224. case 3:
  2225. YCbCrtoRGB(cp[2], pp[2]); /*-fallthrough*/
  2226. case 2:
  2227. YCbCrtoRGB(cp[1], pp[1]); /*-fallthrough*/
  2228. case 1:
  2229. YCbCrtoRGB(cp[0], pp[0]); /*-fallthrough*/
  2230. case 0:
  2231. break;
  2232. }
  2233. cp += (w & 3);
  2234. pp += 6;
  2235. }
  2236. cp += toskew;
  2237. pp += fromskew;
  2238. } while (--h);
  2239. }
  2240. /*
  2241. * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
  2242. */
  2243. DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
  2244. {
  2245. uint32_t *cp2;
  2246. int32_t incr = 2 * toskew + w;
  2247. (void)y;
  2248. fromskew = (fromskew / 2) * (2 * 2 + 2);
  2249. cp2 = cp + w + toskew;
  2250. while (h >= 2)
  2251. {
  2252. x = w;
  2253. while (x >= 2)
  2254. {
  2255. uint32_t Cb = pp[4];
  2256. uint32_t Cr = pp[5];
  2257. YCbCrtoRGB(cp[0], pp[0]);
  2258. YCbCrtoRGB(cp[1], pp[1]);
  2259. YCbCrtoRGB(cp2[0], pp[2]);
  2260. YCbCrtoRGB(cp2[1], pp[3]);
  2261. cp += 2;
  2262. cp2 += 2;
  2263. pp += 6;
  2264. x -= 2;
  2265. }
  2266. if (x == 1)
  2267. {
  2268. uint32_t Cb = pp[4];
  2269. uint32_t Cr = pp[5];
  2270. YCbCrtoRGB(cp[0], pp[0]);
  2271. YCbCrtoRGB(cp2[0], pp[2]);
  2272. cp++;
  2273. cp2++;
  2274. pp += 6;
  2275. }
  2276. cp += incr;
  2277. cp2 += incr;
  2278. pp += fromskew;
  2279. h -= 2;
  2280. }
  2281. if (h == 1)
  2282. {
  2283. x = w;
  2284. while (x >= 2)
  2285. {
  2286. uint32_t Cb = pp[4];
  2287. uint32_t Cr = pp[5];
  2288. YCbCrtoRGB(cp[0], pp[0]);
  2289. YCbCrtoRGB(cp[1], pp[1]);
  2290. cp += 2;
  2291. cp2 += 2;
  2292. pp += 6;
  2293. x -= 2;
  2294. }
  2295. if (x == 1)
  2296. {
  2297. uint32_t Cb = pp[4];
  2298. uint32_t Cr = pp[5];
  2299. YCbCrtoRGB(cp[0], pp[0]);
  2300. }
  2301. }
  2302. }
  2303. /*
  2304. * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
  2305. */
  2306. DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
  2307. {
  2308. (void)y;
  2309. fromskew = (fromskew / 2) * (2 * 1 + 2);
  2310. do
  2311. {
  2312. x = w >> 1;
  2313. while (x > 0)
  2314. {
  2315. int32_t Cb = pp[2];
  2316. int32_t Cr = pp[3];
  2317. YCbCrtoRGB(cp[0], pp[0]);
  2318. YCbCrtoRGB(cp[1], pp[1]);
  2319. cp += 2;
  2320. pp += 4;
  2321. x--;
  2322. }
  2323. if ((w & 1) != 0)
  2324. {
  2325. int32_t Cb = pp[2];
  2326. int32_t Cr = pp[3];
  2327. YCbCrtoRGB(cp[0], pp[0]);
  2328. cp += 1;
  2329. pp += 4;
  2330. }
  2331. cp += toskew;
  2332. pp += fromskew;
  2333. } while (--h);
  2334. }
  2335. /*
  2336. * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
  2337. */
  2338. DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
  2339. {
  2340. uint32_t *cp2;
  2341. int32_t incr = 2 * toskew + w;
  2342. (void)y;
  2343. fromskew = (fromskew / 1) * (1 * 2 + 2);
  2344. cp2 = cp + w + toskew;
  2345. while (h >= 2)
  2346. {
  2347. x = w;
  2348. do
  2349. {
  2350. uint32_t Cb = pp[2];
  2351. uint32_t Cr = pp[3];
  2352. YCbCrtoRGB(cp[0], pp[0]);
  2353. YCbCrtoRGB(cp2[0], pp[1]);
  2354. cp++;
  2355. cp2++;
  2356. pp += 4;
  2357. } while (--x);
  2358. cp += incr;
  2359. cp2 += incr;
  2360. pp += fromskew;
  2361. h -= 2;
  2362. }
  2363. if (h == 1)
  2364. {
  2365. x = w;
  2366. do
  2367. {
  2368. uint32_t Cb = pp[2];
  2369. uint32_t Cr = pp[3];
  2370. YCbCrtoRGB(cp[0], pp[0]);
  2371. cp++;
  2372. pp += 4;
  2373. } while (--x);
  2374. }
  2375. }
  2376. /*
  2377. * 8-bit packed YCbCr samples w/ no subsampling => RGB
  2378. */
  2379. DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
  2380. {
  2381. (void)y;
  2382. fromskew = (fromskew / 1) * (1 * 1 + 2);
  2383. do
  2384. {
  2385. x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
  2386. do
  2387. {
  2388. int32_t Cb = pp[1];
  2389. int32_t Cr = pp[2];
  2390. YCbCrtoRGB(*cp++, pp[0]);
  2391. pp += 3;
  2392. } while (--x);
  2393. cp += toskew;
  2394. pp += fromskew;
  2395. } while (--h);
  2396. }
  2397. /*
  2398. * 8-bit packed YCbCr samples w/ no subsampling => RGB
  2399. */
  2400. DECLARESepPutFunc(putseparate8bitYCbCr11tile)
  2401. {
  2402. (void)y;
  2403. (void)a;
  2404. /* TODO: naming of input vars is still off, change obfuscating declaration
  2405. * inside define, or resolve obfuscation */
  2406. for (; h > 0; --h)
  2407. {
  2408. x = w;
  2409. do
  2410. {
  2411. uint32_t dr, dg, db;
  2412. TIFFYCbCrtoRGB(img->ycbcr, *r++, *g++, *b++, &dr, &dg, &db);
  2413. *cp++ = PACK(dr, dg, db);
  2414. } while (--x);
  2415. SKEW(r, g, b, fromskew);
  2416. cp += toskew;
  2417. }
  2418. }
  2419. #undef YCbCrtoRGB
  2420. static int isInRefBlackWhiteRange(float f)
  2421. {
  2422. return f > (float)(-0x7FFFFFFF + 128) && f < (float)0x7FFFFFFF;
  2423. }
  2424. static int initYCbCrConversion(TIFFRGBAImage *img)
  2425. {
  2426. static const char module[] = "initYCbCrConversion";
  2427. float *luma, *refBlackWhite;
  2428. if (img->ycbcr == NULL)
  2429. {
  2430. img->ycbcr = (TIFFYCbCrToRGB *)_TIFFmallocExt(
  2431. img->tif, TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long)) +
  2432. 4 * 256 * sizeof(TIFFRGBValue) +
  2433. 2 * 256 * sizeof(int) + 3 * 256 * sizeof(int32_t));
  2434. if (img->ycbcr == NULL)
  2435. {
  2436. TIFFErrorExtR(img->tif, module,
  2437. "No space for YCbCr->RGB conversion state");
  2438. return (0);
  2439. }
  2440. }
  2441. TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
  2442. TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
  2443. &refBlackWhite);
  2444. /* Do some validation to avoid later issues. Detect NaN for now */
  2445. /* and also if lumaGreen is zero since we divide by it later */
  2446. if (luma[0] != luma[0] || luma[1] != luma[1] || luma[1] == 0.0 ||
  2447. luma[2] != luma[2])
  2448. {
  2449. TIFFErrorExtR(img->tif, module,
  2450. "Invalid values for YCbCrCoefficients tag");
  2451. return (0);
  2452. }
  2453. if (!isInRefBlackWhiteRange(refBlackWhite[0]) ||
  2454. !isInRefBlackWhiteRange(refBlackWhite[1]) ||
  2455. !isInRefBlackWhiteRange(refBlackWhite[2]) ||
  2456. !isInRefBlackWhiteRange(refBlackWhite[3]) ||
  2457. !isInRefBlackWhiteRange(refBlackWhite[4]) ||
  2458. !isInRefBlackWhiteRange(refBlackWhite[5]))
  2459. {
  2460. TIFFErrorExtR(img->tif, module,
  2461. "Invalid values for ReferenceBlackWhite tag");
  2462. return (0);
  2463. }
  2464. if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
  2465. return (0);
  2466. return (1);
  2467. }
  2468. static tileContigRoutine initCIELabConversion(TIFFRGBAImage *img)
  2469. {
  2470. static const char module[] = "initCIELabConversion";
  2471. float *whitePoint;
  2472. float refWhite[3];
  2473. TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
  2474. if (whitePoint[1] == 0.0f)
  2475. {
  2476. TIFFErrorExtR(img->tif, module, "Invalid value for WhitePoint tag.");
  2477. return NULL;
  2478. }
  2479. if (!img->cielab)
  2480. {
  2481. img->cielab = (TIFFCIELabToRGB *)_TIFFmallocExt(
  2482. img->tif, sizeof(TIFFCIELabToRGB));
  2483. if (!img->cielab)
  2484. {
  2485. TIFFErrorExtR(img->tif, module,
  2486. "No space for CIE L*a*b*->RGB conversion state.");
  2487. return NULL;
  2488. }
  2489. }
  2490. refWhite[1] = 100.0F;
  2491. refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
  2492. refWhite[2] =
  2493. (1.0F - whitePoint[0] - whitePoint[1]) / whitePoint[1] * refWhite[1];
  2494. if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0)
  2495. {
  2496. TIFFErrorExtR(img->tif, module,
  2497. "Failed to initialize CIE L*a*b*->RGB conversion state.");
  2498. _TIFFfreeExt(img->tif, img->cielab);
  2499. return NULL;
  2500. }
  2501. if (img->bitspersample == 8)
  2502. return putcontig8bitCIELab8;
  2503. else if (img->bitspersample == 16)
  2504. return putcontig8bitCIELab16;
  2505. return NULL;
  2506. }
  2507. /*
  2508. * Greyscale images with less than 8 bits/sample are handled
  2509. * with a table to avoid lots of shifts and masks. The table
  2510. * is setup so that put*bwtile (below) can retrieve 8/bitspersample
  2511. * pixel values simply by indexing into the table with one
  2512. * number.
  2513. */
  2514. static int makebwmap(TIFFRGBAImage *img)
  2515. {
  2516. TIFFRGBValue *Map = img->Map;
  2517. int bitspersample = img->bitspersample;
  2518. int nsamples = 8 / bitspersample;
  2519. int i;
  2520. uint32_t *p;
  2521. if (nsamples == 0)
  2522. nsamples = 1;
  2523. img->BWmap = (uint32_t **)_TIFFmallocExt(
  2524. img->tif,
  2525. 256 * sizeof(uint32_t *) + (256 * nsamples * sizeof(uint32_t)));
  2526. if (img->BWmap == NULL)
  2527. {
  2528. TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
  2529. "No space for B&W mapping table");
  2530. return (0);
  2531. }
  2532. p = (uint32_t *)(img->BWmap + 256);
  2533. for (i = 0; i < 256; i++)
  2534. {
  2535. TIFFRGBValue c;
  2536. img->BWmap[i] = p;
  2537. switch (bitspersample)
  2538. {
  2539. #define GREY(x) \
  2540. c = Map[x]; \
  2541. *p++ = PACK(c, c, c);
  2542. case 1:
  2543. GREY(i >> 7);
  2544. GREY((i >> 6) & 1);
  2545. GREY((i >> 5) & 1);
  2546. GREY((i >> 4) & 1);
  2547. GREY((i >> 3) & 1);
  2548. GREY((i >> 2) & 1);
  2549. GREY((i >> 1) & 1);
  2550. GREY(i & 1);
  2551. break;
  2552. case 2:
  2553. GREY(i >> 6);
  2554. GREY((i >> 4) & 3);
  2555. GREY((i >> 2) & 3);
  2556. GREY(i & 3);
  2557. break;
  2558. case 4:
  2559. GREY(i >> 4);
  2560. GREY(i & 0xf);
  2561. break;
  2562. case 8:
  2563. case 16:
  2564. GREY(i);
  2565. break;
  2566. }
  2567. #undef GREY
  2568. }
  2569. return (1);
  2570. }
  2571. /*
  2572. * Construct a mapping table to convert from the range
  2573. * of the data samples to [0,255] --for display. This
  2574. * process also handles inverting B&W images when needed.
  2575. */
  2576. static int setupMap(TIFFRGBAImage *img)
  2577. {
  2578. int32_t x, range;
  2579. range = (int32_t)((1L << img->bitspersample) - 1);
  2580. /* treat 16 bit the same as eight bit */
  2581. if (img->bitspersample == 16)
  2582. range = (int32_t)255;
  2583. img->Map = (TIFFRGBValue *)_TIFFmallocExt(
  2584. img->tif, (range + 1) * sizeof(TIFFRGBValue));
  2585. if (img->Map == NULL)
  2586. {
  2587. TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
  2588. "No space for photometric conversion table");
  2589. return (0);
  2590. }
  2591. if (img->photometric == PHOTOMETRIC_MINISWHITE)
  2592. {
  2593. for (x = 0; x <= range; x++)
  2594. img->Map[x] = (TIFFRGBValue)(((range - x) * 255) / range);
  2595. }
  2596. else
  2597. {
  2598. for (x = 0; x <= range; x++)
  2599. img->Map[x] = (TIFFRGBValue)((x * 255) / range);
  2600. }
  2601. if (img->bitspersample <= 16 &&
  2602. (img->photometric == PHOTOMETRIC_MINISBLACK ||
  2603. img->photometric == PHOTOMETRIC_MINISWHITE))
  2604. {
  2605. /*
  2606. * Use photometric mapping table to construct
  2607. * unpacking tables for samples <= 8 bits.
  2608. */
  2609. if (!makebwmap(img))
  2610. return (0);
  2611. /* no longer need Map, free it */
  2612. _TIFFfreeExt(img->tif, img->Map);
  2613. img->Map = NULL;
  2614. }
  2615. return (1);
  2616. }
  2617. static int checkcmap(TIFFRGBAImage *img)
  2618. {
  2619. uint16_t *r = img->redcmap;
  2620. uint16_t *g = img->greencmap;
  2621. uint16_t *b = img->bluecmap;
  2622. long n = 1L << img->bitspersample;
  2623. while (n-- > 0)
  2624. if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
  2625. return (16);
  2626. return (8);
  2627. }
  2628. static void cvtcmap(TIFFRGBAImage *img)
  2629. {
  2630. uint16_t *r = img->redcmap;
  2631. uint16_t *g = img->greencmap;
  2632. uint16_t *b = img->bluecmap;
  2633. long i;
  2634. for (i = (1L << img->bitspersample) - 1; i >= 0; i--)
  2635. {
  2636. #define CVT(x) ((uint16_t)((x) >> 8))
  2637. r[i] = CVT(r[i]);
  2638. g[i] = CVT(g[i]);
  2639. b[i] = CVT(b[i]);
  2640. #undef CVT
  2641. }
  2642. }
  2643. /*
  2644. * Palette images with <= 8 bits/sample are handled
  2645. * with a table to avoid lots of shifts and masks. The table
  2646. * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
  2647. * pixel values simply by indexing into the table with one
  2648. * number.
  2649. */
  2650. static int makecmap(TIFFRGBAImage *img)
  2651. {
  2652. int bitspersample = img->bitspersample;
  2653. int nsamples = 8 / bitspersample;
  2654. uint16_t *r = img->redcmap;
  2655. uint16_t *g = img->greencmap;
  2656. uint16_t *b = img->bluecmap;
  2657. uint32_t *p;
  2658. int i;
  2659. img->PALmap = (uint32_t **)_TIFFmallocExt(
  2660. img->tif,
  2661. 256 * sizeof(uint32_t *) + (256 * nsamples * sizeof(uint32_t)));
  2662. if (img->PALmap == NULL)
  2663. {
  2664. TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
  2665. "No space for Palette mapping table");
  2666. return (0);
  2667. }
  2668. p = (uint32_t *)(img->PALmap + 256);
  2669. for (i = 0; i < 256; i++)
  2670. {
  2671. TIFFRGBValue c;
  2672. img->PALmap[i] = p;
  2673. #define CMAP(x) \
  2674. c = (TIFFRGBValue)x; \
  2675. *p++ = PACK(r[c] & 0xff, g[c] & 0xff, b[c] & 0xff);
  2676. switch (bitspersample)
  2677. {
  2678. case 1:
  2679. CMAP(i >> 7);
  2680. CMAP((i >> 6) & 1);
  2681. CMAP((i >> 5) & 1);
  2682. CMAP((i >> 4) & 1);
  2683. CMAP((i >> 3) & 1);
  2684. CMAP((i >> 2) & 1);
  2685. CMAP((i >> 1) & 1);
  2686. CMAP(i & 1);
  2687. break;
  2688. case 2:
  2689. CMAP(i >> 6);
  2690. CMAP((i >> 4) & 3);
  2691. CMAP((i >> 2) & 3);
  2692. CMAP(i & 3);
  2693. break;
  2694. case 4:
  2695. CMAP(i >> 4);
  2696. CMAP(i & 0xf);
  2697. break;
  2698. case 8:
  2699. CMAP(i);
  2700. break;
  2701. }
  2702. #undef CMAP
  2703. }
  2704. return (1);
  2705. }
  2706. /*
  2707. * Construct any mapping table used
  2708. * by the associated put routine.
  2709. */
  2710. static int buildMap(TIFFRGBAImage *img)
  2711. {
  2712. switch (img->photometric)
  2713. {
  2714. case PHOTOMETRIC_RGB:
  2715. case PHOTOMETRIC_YCBCR:
  2716. case PHOTOMETRIC_SEPARATED:
  2717. if (img->bitspersample == 8)
  2718. break;
  2719. /* fall through... */
  2720. case PHOTOMETRIC_MINISBLACK:
  2721. case PHOTOMETRIC_MINISWHITE:
  2722. if (!setupMap(img))
  2723. return (0);
  2724. break;
  2725. case PHOTOMETRIC_PALETTE:
  2726. /*
  2727. * Convert 16-bit colormap to 8-bit (unless it looks
  2728. * like an old-style 8-bit colormap).
  2729. */
  2730. if (checkcmap(img) == 16)
  2731. cvtcmap(img);
  2732. else
  2733. TIFFWarningExtR(img->tif, TIFFFileName(img->tif),
  2734. "Assuming 8-bit colormap");
  2735. /*
  2736. * Use mapping table and colormap to construct
  2737. * unpacking tables for samples < 8 bits.
  2738. */
  2739. if (img->bitspersample <= 8 && !makecmap(img))
  2740. return (0);
  2741. break;
  2742. }
  2743. return (1);
  2744. }
  2745. /*
  2746. * Select the appropriate conversion routine for packed data.
  2747. */
  2748. static int PickContigCase(TIFFRGBAImage *img)
  2749. {
  2750. img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
  2751. img->put.contig = NULL;
  2752. switch (img->photometric)
  2753. {
  2754. case PHOTOMETRIC_RGB:
  2755. switch (img->bitspersample)
  2756. {
  2757. case 8:
  2758. if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
  2759. img->samplesperpixel >= 4)
  2760. img->put.contig = putRGBAAcontig8bittile;
  2761. else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
  2762. img->samplesperpixel >= 4)
  2763. {
  2764. if (BuildMapUaToAa(img))
  2765. img->put.contig = putRGBUAcontig8bittile;
  2766. }
  2767. else if (img->samplesperpixel >= 3)
  2768. img->put.contig = putRGBcontig8bittile;
  2769. break;
  2770. case 16:
  2771. if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&
  2772. img->samplesperpixel >= 4)
  2773. {
  2774. if (BuildMapBitdepth16To8(img))
  2775. img->put.contig = putRGBAAcontig16bittile;
  2776. }
  2777. else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&
  2778. img->samplesperpixel >= 4)
  2779. {
  2780. if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
  2781. img->put.contig = putRGBUAcontig16bittile;
  2782. }
  2783. else if (img->samplesperpixel >= 3)
  2784. {
  2785. if (BuildMapBitdepth16To8(img))
  2786. img->put.contig = putRGBcontig16bittile;
  2787. }
  2788. break;
  2789. }
  2790. break;
  2791. case PHOTOMETRIC_SEPARATED:
  2792. if (img->samplesperpixel >= 4 && buildMap(img))
  2793. {
  2794. if (img->bitspersample == 8)
  2795. {
  2796. if (!img->Map)
  2797. img->put.contig = putRGBcontig8bitCMYKtile;
  2798. else
  2799. img->put.contig = putRGBcontig8bitCMYKMaptile;
  2800. }
  2801. }
  2802. break;
  2803. case PHOTOMETRIC_PALETTE:
  2804. if (buildMap(img))
  2805. {
  2806. switch (img->bitspersample)
  2807. {
  2808. case 8:
  2809. img->put.contig = put8bitcmaptile;
  2810. break;
  2811. case 4:
  2812. img->put.contig = put4bitcmaptile;
  2813. break;
  2814. case 2:
  2815. img->put.contig = put2bitcmaptile;
  2816. break;
  2817. case 1:
  2818. img->put.contig = put1bitcmaptile;
  2819. break;
  2820. }
  2821. }
  2822. break;
  2823. case PHOTOMETRIC_MINISWHITE:
  2824. case PHOTOMETRIC_MINISBLACK:
  2825. if (buildMap(img))
  2826. {
  2827. switch (img->bitspersample)
  2828. {
  2829. case 16:
  2830. img->put.contig = put16bitbwtile;
  2831. break;
  2832. case 8:
  2833. if (img->alpha && img->samplesperpixel == 2)
  2834. img->put.contig = putagreytile;
  2835. else
  2836. img->put.contig = putgreytile;
  2837. break;
  2838. case 4:
  2839. img->put.contig = put4bitbwtile;
  2840. break;
  2841. case 2:
  2842. img->put.contig = put2bitbwtile;
  2843. break;
  2844. case 1:
  2845. img->put.contig = put1bitbwtile;
  2846. break;
  2847. }
  2848. }
  2849. break;
  2850. case PHOTOMETRIC_YCBCR:
  2851. if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
  2852. {
  2853. if (initYCbCrConversion(img) != 0)
  2854. {
  2855. /*
  2856. * The 6.0 spec says that subsampling must be
  2857. * one of 1, 2, or 4, and that vertical subsampling
  2858. * must always be <= horizontal subsampling; so
  2859. * there are only a few possibilities and we just
  2860. * enumerate the cases.
  2861. * Joris: added support for the [1,2] case, nonetheless, to
  2862. * accommodate some OJPEG files
  2863. */
  2864. uint16_t SubsamplingHor;
  2865. uint16_t SubsamplingVer;
  2866. TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,
  2867. &SubsamplingHor, &SubsamplingVer);
  2868. switch ((SubsamplingHor << 4) | SubsamplingVer)
  2869. {
  2870. case 0x44:
  2871. img->put.contig = putcontig8bitYCbCr44tile;
  2872. break;
  2873. case 0x42:
  2874. img->put.contig = putcontig8bitYCbCr42tile;
  2875. break;
  2876. case 0x41:
  2877. img->put.contig = putcontig8bitYCbCr41tile;
  2878. break;
  2879. case 0x22:
  2880. img->put.contig = putcontig8bitYCbCr22tile;
  2881. break;
  2882. case 0x21:
  2883. img->put.contig = putcontig8bitYCbCr21tile;
  2884. break;
  2885. case 0x12:
  2886. img->put.contig = putcontig8bitYCbCr12tile;
  2887. break;
  2888. case 0x11:
  2889. img->put.contig = putcontig8bitYCbCr11tile;
  2890. break;
  2891. }
  2892. }
  2893. }
  2894. break;
  2895. case PHOTOMETRIC_CIELAB:
  2896. if (img->samplesperpixel == 3 && buildMap(img))
  2897. {
  2898. if (img->bitspersample == 8 || img->bitspersample == 16)
  2899. img->put.contig = initCIELabConversion(img);
  2900. break;
  2901. }
  2902. }
  2903. return ((img->get != NULL) && (img->put.contig != NULL));
  2904. }
  2905. /*
  2906. * Select the appropriate conversion routine for unpacked data.
  2907. *
  2908. * NB: we assume that unpacked single channel data is directed
  2909. * to the "packed routines.
  2910. */
  2911. static int PickSeparateCase(TIFFRGBAImage *img)
  2912. {
  2913. img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
  2914. img->put.separate = NULL;
  2915. switch (img->photometric)
  2916. {
  2917. case PHOTOMETRIC_MINISWHITE:
  2918. case PHOTOMETRIC_MINISBLACK:
  2919. /* greyscale images processed pretty much as RGB by gtTileSeparate
  2920. */
  2921. case PHOTOMETRIC_RGB:
  2922. switch (img->bitspersample)
  2923. {
  2924. case 8:
  2925. if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  2926. img->put.separate = putRGBAAseparate8bittile;
  2927. else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  2928. {
  2929. if (BuildMapUaToAa(img))
  2930. img->put.separate = putRGBUAseparate8bittile;
  2931. }
  2932. else
  2933. img->put.separate = putRGBseparate8bittile;
  2934. break;
  2935. case 16:
  2936. if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  2937. {
  2938. if (BuildMapBitdepth16To8(img))
  2939. img->put.separate = putRGBAAseparate16bittile;
  2940. }
  2941. else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  2942. {
  2943. if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))
  2944. img->put.separate = putRGBUAseparate16bittile;
  2945. }
  2946. else
  2947. {
  2948. if (BuildMapBitdepth16To8(img))
  2949. img->put.separate = putRGBseparate16bittile;
  2950. }
  2951. break;
  2952. }
  2953. break;
  2954. case PHOTOMETRIC_SEPARATED:
  2955. if (img->bitspersample == 8 && img->samplesperpixel == 4)
  2956. {
  2957. img->alpha =
  2958. 1; // Not alpha, but seems like the only way to get 4th band
  2959. img->put.separate = putCMYKseparate8bittile;
  2960. }
  2961. break;
  2962. case PHOTOMETRIC_YCBCR:
  2963. if ((img->bitspersample == 8) && (img->samplesperpixel == 3))
  2964. {
  2965. if (initYCbCrConversion(img) != 0)
  2966. {
  2967. uint16_t hs, vs;
  2968. TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,
  2969. &hs, &vs);
  2970. switch ((hs << 4) | vs)
  2971. {
  2972. case 0x11:
  2973. img->put.separate = putseparate8bitYCbCr11tile;
  2974. break;
  2975. /* TODO: add other cases here */
  2976. }
  2977. }
  2978. }
  2979. break;
  2980. }
  2981. return ((img->get != NULL) && (img->put.separate != NULL));
  2982. }
  2983. static int BuildMapUaToAa(TIFFRGBAImage *img)
  2984. {
  2985. static const char module[] = "BuildMapUaToAa";
  2986. uint8_t *m;
  2987. uint16_t na, nv;
  2988. assert(img->UaToAa == NULL);
  2989. img->UaToAa = _TIFFmallocExt(img->tif, 65536);
  2990. if (img->UaToAa == NULL)
  2991. {
  2992. TIFFErrorExtR(img->tif, module, "Out of memory");
  2993. return (0);
  2994. }
  2995. m = img->UaToAa;
  2996. for (na = 0; na < 256; na++)
  2997. {
  2998. for (nv = 0; nv < 256; nv++)
  2999. *m++ = (uint8_t)((nv * na + 127) / 255);
  3000. }
  3001. return (1);
  3002. }
  3003. static int BuildMapBitdepth16To8(TIFFRGBAImage *img)
  3004. {
  3005. static const char module[] = "BuildMapBitdepth16To8";
  3006. uint8_t *m;
  3007. uint32_t n;
  3008. assert(img->Bitdepth16To8 == NULL);
  3009. img->Bitdepth16To8 = _TIFFmallocExt(img->tif, 65536);
  3010. if (img->Bitdepth16To8 == NULL)
  3011. {
  3012. TIFFErrorExtR(img->tif, module, "Out of memory");
  3013. return (0);
  3014. }
  3015. m = img->Bitdepth16To8;
  3016. for (n = 0; n < 65536; n++)
  3017. *m++ = (uint8_t)((n + 128) / 257);
  3018. return (1);
  3019. }
  3020. /*
  3021. * Read a whole strip off data from the file, and convert to RGBA form.
  3022. * If this is the last strip, then it will only contain the portion of
  3023. * the strip that is actually within the image space. The result is
  3024. * organized in bottom to top form.
  3025. */
  3026. int TIFFReadRGBAStrip(TIFF *tif, uint32_t row, uint32_t *raster)
  3027. {
  3028. return TIFFReadRGBAStripExt(tif, row, raster, 0);
  3029. }
  3030. int TIFFReadRGBAStripExt(TIFF *tif, uint32_t row, uint32_t *raster,
  3031. int stop_on_error)
  3032. {
  3033. char emsg[EMSG_BUF_SIZE] = "";
  3034. TIFFRGBAImage img;
  3035. int ok;
  3036. uint32_t rowsperstrip, rows_to_read;
  3037. if (TIFFIsTiled(tif))
  3038. {
  3039. TIFFErrorExtR(tif, TIFFFileName(tif),
  3040. "Can't use TIFFReadRGBAStrip() with tiled file.");
  3041. return (0);
  3042. }
  3043. TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  3044. if (rowsperstrip == 0)
  3045. {
  3046. TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
  3047. return (0);
  3048. }
  3049. if ((row % rowsperstrip) != 0)
  3050. {
  3051. TIFFErrorExtR(
  3052. tif, TIFFFileName(tif),
  3053. "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
  3054. return (0);
  3055. }
  3056. if (TIFFRGBAImageOK(tif, emsg) &&
  3057. TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))
  3058. {
  3059. if (row >= img.height)
  3060. {
  3061. TIFFErrorExtR(tif, TIFFFileName(tif),
  3062. "Invalid row passed to TIFFReadRGBAStrip().");
  3063. TIFFRGBAImageEnd(&img);
  3064. return (0);
  3065. }
  3066. img.row_offset = row;
  3067. img.col_offset = 0;
  3068. if (row + rowsperstrip > img.height)
  3069. rows_to_read = img.height - row;
  3070. else
  3071. rows_to_read = rowsperstrip;
  3072. ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read);
  3073. TIFFRGBAImageEnd(&img);
  3074. }
  3075. else
  3076. {
  3077. TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
  3078. ok = 0;
  3079. }
  3080. return (ok);
  3081. }
  3082. /*
  3083. * Read a whole tile off data from the file, and convert to RGBA form.
  3084. * The returned RGBA data is organized from bottom to top of tile,
  3085. * and may include zeroed areas if the tile extends off the image.
  3086. */
  3087. int TIFFReadRGBATile(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster)
  3088. {
  3089. return TIFFReadRGBATileExt(tif, col, row, raster, 0);
  3090. }
  3091. int TIFFReadRGBATileExt(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster,
  3092. int stop_on_error)
  3093. {
  3094. char emsg[EMSG_BUF_SIZE] = "";
  3095. TIFFRGBAImage img;
  3096. int ok;
  3097. uint32_t tile_xsize, tile_ysize;
  3098. uint32_t read_xsize, read_ysize;
  3099. uint32_t i_row;
  3100. /*
  3101. * Verify that our request is legal - on a tile file, and on a
  3102. * tile boundary.
  3103. */
  3104. if (!TIFFIsTiled(tif))
  3105. {
  3106. TIFFErrorExtR(tif, TIFFFileName(tif),
  3107. "Can't use TIFFReadRGBATile() with striped file.");
  3108. return (0);
  3109. }
  3110. TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
  3111. TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
  3112. if (tile_xsize == 0 || tile_ysize == 0)
  3113. {
  3114. TIFFErrorExtR(tif, TIFFFileName(tif),
  3115. "tile_xsize or tile_ysize is zero");
  3116. return (0);
  3117. }
  3118. if ((col % tile_xsize) != 0 || (row % tile_ysize) != 0)
  3119. {
  3120. TIFFErrorExtR(tif, TIFFFileName(tif),
  3121. "Row/col passed to TIFFReadRGBATile() must be top"
  3122. "left corner of a tile.");
  3123. return (0);
  3124. }
  3125. /*
  3126. * Setup the RGBA reader.
  3127. */
  3128. if (!TIFFRGBAImageOK(tif, emsg) ||
  3129. !TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))
  3130. {
  3131. TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);
  3132. return (0);
  3133. }
  3134. if (col >= img.width || row >= img.height)
  3135. {
  3136. TIFFErrorExtR(tif, TIFFFileName(tif),
  3137. "Invalid row/col passed to TIFFReadRGBATile().");
  3138. TIFFRGBAImageEnd(&img);
  3139. return (0);
  3140. }
  3141. /*
  3142. * The TIFFRGBAImageGet() function doesn't allow us to get off the
  3143. * edge of the image, even to fill an otherwise valid tile. So we
  3144. * figure out how much we can read, and fix up the tile buffer to
  3145. * a full tile configuration afterwards.
  3146. */
  3147. if (row + tile_ysize > img.height)
  3148. read_ysize = img.height - row;
  3149. else
  3150. read_ysize = tile_ysize;
  3151. if (col + tile_xsize > img.width)
  3152. read_xsize = img.width - col;
  3153. else
  3154. read_xsize = tile_xsize;
  3155. /*
  3156. * Read the chunk of imagery.
  3157. */
  3158. img.row_offset = row;
  3159. img.col_offset = col;
  3160. ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize);
  3161. TIFFRGBAImageEnd(&img);
  3162. /*
  3163. * If our read was incomplete we will need to fix up the tile by
  3164. * shifting the data around as if a full tile of data is being returned.
  3165. *
  3166. * This is all the more complicated because the image is organized in
  3167. * bottom to top format.
  3168. */
  3169. if (read_xsize == tile_xsize && read_ysize == tile_ysize)
  3170. return (ok);
  3171. for (i_row = 0; i_row < read_ysize; i_row++)
  3172. {
  3173. memmove(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize,
  3174. raster + (size_t)(read_ysize - i_row - 1) * read_xsize,
  3175. read_xsize * sizeof(uint32_t));
  3176. _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize +
  3177. read_xsize,
  3178. 0, sizeof(uint32_t) * (tile_xsize - read_xsize));
  3179. }
  3180. for (i_row = read_ysize; i_row < tile_ysize; i_row++)
  3181. {
  3182. _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize, 0,
  3183. sizeof(uint32_t) * tile_xsize);
  3184. }
  3185. return (ok);
  3186. }