tif_getimage.c 72 KB

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