tif_getimage.c 76 KB

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