tif_getimage.c 79 KB

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