tif_getimage.c 101 KB

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