12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867 |
- /* $Id: tif_getimage.c,v 1.82 2012-06-06 00:17:49 fwarmerdam Exp $ */
- /*
- * Copyright (c) 1991-1997 Sam Leffler
- * Copyright (c) 1991-1997 Silicon Graphics, Inc.
- *
- * Permission to use, copy, modify, distribute, and sell this software and
- * its documentation for any purpose is hereby granted without fee, provided
- * that (i) the above copyright notices and this permission notice appear in
- * all copies of the software and related documentation, and (ii) the names of
- * Sam Leffler and Silicon Graphics may not be used in any advertising or
- * publicity relating to the software without the specific, prior written
- * permission of Sam Leffler and Silicon Graphics.
- *
- * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
- * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
- *
- * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
- * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
- * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
- * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
- * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
- * OF THIS SOFTWARE.
- */
- /*
- * TIFF Library
- *
- * Read and return a packed RGBA image.
- */
- #include "tiffiop.h"
- #include <stdio.h>
- static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
- static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
- static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
- static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
- static int PickContigCase(TIFFRGBAImage*);
- static int PickSeparateCase(TIFFRGBAImage*);
- static int BuildMapUaToAa(TIFFRGBAImage* img);
- static int BuildMapBitdepth16To8(TIFFRGBAImage* img);
- static const char photoTag[] = "PhotometricInterpretation";
- /*
- * Helper constants used in Orientation tag handling
- */
- #define FLIP_VERTICALLY 0x01
- #define FLIP_HORIZONTALLY 0x02
- /*
- * Color conversion constants. We will define display types here.
- */
- static const TIFFDisplay display_sRGB = {
- { /* XYZ -> luminance matrix */
- { 3.2410F, -1.5374F, -0.4986F },
- { -0.9692F, 1.8760F, 0.0416F },
- { 0.0556F, -0.2040F, 1.0570F }
- },
- 100.0F, 100.0F, 100.0F, /* Light o/p for reference white */
- 255, 255, 255, /* Pixel values for ref. white */
- 1.0F, 1.0F, 1.0F, /* Residual light o/p for black pixel */
- 2.4F, 2.4F, 2.4F, /* Gamma values for the three guns */
- };
- /*
- * Check the image to see if TIFFReadRGBAImage can deal with it.
- * 1/0 is returned according to whether or not the image can
- * be handled. If 0 is returned, emsg contains the reason
- * why it is being rejected.
- */
- int
- TIFFRGBAImageOK(TIFF* tif, char emsg[1024])
- {
- TIFFDirectory* td = &tif->tif_dir;
- uint16 photometric;
- int colorchannels;
- if (!tif->tif_decodestatus) {
- sprintf(emsg, "Sorry, requested compression method is not configured");
- return (0);
- }
- switch (td->td_bitspersample) {
- case 1:
- case 2:
- case 4:
- case 8:
- case 16:
- break;
- default:
- sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
- td->td_bitspersample);
- return (0);
- }
- colorchannels = td->td_samplesperpixel - td->td_extrasamples;
- if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
- switch (colorchannels) {
- case 1:
- photometric = PHOTOMETRIC_MINISBLACK;
- break;
- case 3:
- photometric = PHOTOMETRIC_RGB;
- break;
- default:
- sprintf(emsg, "Missing needed %s tag", photoTag);
- return (0);
- }
- }
- switch (photometric) {
- case PHOTOMETRIC_MINISWHITE:
- case PHOTOMETRIC_MINISBLACK:
- case PHOTOMETRIC_PALETTE:
- if (td->td_planarconfig == PLANARCONFIG_CONTIG
- && td->td_samplesperpixel != 1
- && td->td_bitspersample < 8 ) {
- sprintf(emsg,
- "Sorry, can not handle contiguous data with %s=%d, "
- "and %s=%d and Bits/Sample=%d",
- photoTag, photometric,
- "Samples/pixel", td->td_samplesperpixel,
- td->td_bitspersample);
- return (0);
- }
- /*
- * We should likely validate that any extra samples are either
- * to be ignored, or are alpha, and if alpha we should try to use
- * them. But for now we won't bother with this.
- */
- break;
- case PHOTOMETRIC_YCBCR:
- /*
- * TODO: if at all meaningful and useful, make more complete
- * support check here, or better still, refactor to let supporting
- * code decide whether there is support and what meaningfull
- * error to return
- */
- break;
- case PHOTOMETRIC_RGB:
- if (colorchannels < 3) {
- sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
- "Color channels", colorchannels);
- return (0);
- }
- break;
- case PHOTOMETRIC_SEPARATED:
- {
- uint16 inkset;
- TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
- if (inkset != INKSET_CMYK) {
- sprintf(emsg,
- "Sorry, can not handle separated image with %s=%d",
- "InkSet", inkset);
- return 0;
- }
- if (td->td_samplesperpixel < 4) {
- sprintf(emsg,
- "Sorry, can not handle separated image with %s=%d",
- "Samples/pixel", td->td_samplesperpixel);
- return 0;
- }
- break;
- }
- case PHOTOMETRIC_LOGL:
- if (td->td_compression != COMPRESSION_SGILOG) {
- sprintf(emsg, "Sorry, LogL data must have %s=%d",
- "Compression", COMPRESSION_SGILOG);
- return (0);
- }
- break;
- case PHOTOMETRIC_LOGLUV:
- if (td->td_compression != COMPRESSION_SGILOG &&
- td->td_compression != COMPRESSION_SGILOG24) {
- sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
- "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
- return (0);
- }
- if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
- sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
- "Planarconfiguration", td->td_planarconfig);
- return (0);
- }
- break;
- case PHOTOMETRIC_CIELAB:
- break;
- default:
- sprintf(emsg, "Sorry, can not handle image with %s=%d",
- photoTag, photometric);
- return (0);
- }
- return (1);
- }
- void
- TIFFRGBAImageEnd(TIFFRGBAImage* img)
- {
- if (img->Map)
- _TIFFfree(img->Map), img->Map = NULL;
- if (img->BWmap)
- _TIFFfree(img->BWmap), img->BWmap = NULL;
- if (img->PALmap)
- _TIFFfree(img->PALmap), img->PALmap = NULL;
- if (img->ycbcr)
- _TIFFfree(img->ycbcr), img->ycbcr = NULL;
- if (img->cielab)
- _TIFFfree(img->cielab), img->cielab = NULL;
- if (img->UaToAa)
- _TIFFfree(img->UaToAa), img->UaToAa = NULL;
- if (img->Bitdepth16To8)
- _TIFFfree(img->Bitdepth16To8), img->Bitdepth16To8 = NULL;
- if( img->redcmap ) {
- _TIFFfree( img->redcmap );
- _TIFFfree( img->greencmap );
- _TIFFfree( img->bluecmap );
- img->redcmap = img->greencmap = img->bluecmap = NULL;
- }
- }
- static int
- isCCITTCompression(TIFF* tif)
- {
- uint16 compress;
- TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
- return (compress == COMPRESSION_CCITTFAX3 ||
- compress == COMPRESSION_CCITTFAX4 ||
- compress == COMPRESSION_CCITTRLE ||
- compress == COMPRESSION_CCITTRLEW);
- }
- int
- TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
- {
- uint16* sampleinfo;
- uint16 extrasamples;
- uint16 planarconfig;
- uint16 compress;
- int colorchannels;
- uint16 *red_orig, *green_orig, *blue_orig;
- int n_color;
- /* Initialize to normal values */
- img->row_offset = 0;
- img->col_offset = 0;
- img->redcmap = NULL;
- img->greencmap = NULL;
- img->bluecmap = NULL;
- img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
- img->tif = tif;
- img->stoponerr = stop;
- TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
- switch (img->bitspersample) {
- case 1:
- case 2:
- case 4:
- case 8:
- case 16:
- break;
- default:
- sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
- img->bitspersample);
- goto fail_return;
- }
- img->alpha = 0;
- TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
- TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
- &extrasamples, &sampleinfo);
- if (extrasamples >= 1)
- {
- switch (sampleinfo[0]) {
- case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without */
- if (img->samplesperpixel > 3) /* correct info about alpha channel */
- img->alpha = EXTRASAMPLE_ASSOCALPHA;
- break;
- case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
- case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
- img->alpha = sampleinfo[0];
- break;
- }
- }
- #ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
- if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
- img->photometric = PHOTOMETRIC_MINISWHITE;
- if( extrasamples == 0
- && img->samplesperpixel == 4
- && img->photometric == PHOTOMETRIC_RGB )
- {
- img->alpha = EXTRASAMPLE_ASSOCALPHA;
- extrasamples = 1;
- }
- #endif
- colorchannels = img->samplesperpixel - extrasamples;
- TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
- TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
- if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
- switch (colorchannels) {
- case 1:
- if (isCCITTCompression(tif))
- img->photometric = PHOTOMETRIC_MINISWHITE;
- else
- img->photometric = PHOTOMETRIC_MINISBLACK;
- break;
- case 3:
- img->photometric = PHOTOMETRIC_RGB;
- break;
- default:
- sprintf(emsg, "Missing needed %s tag", photoTag);
- goto fail_return;
- }
- }
- switch (img->photometric) {
- case PHOTOMETRIC_PALETTE:
- if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
- &red_orig, &green_orig, &blue_orig)) {
- sprintf(emsg, "Missing required \"Colormap\" tag");
- goto fail_return;
- }
- /* copy the colormaps so we can modify them */
- n_color = (1L << img->bitspersample);
- img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
- img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
- img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
- if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
- sprintf(emsg, "Out of memory for colormap copy");
- goto fail_return;
- }
- _TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
- _TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
- _TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
- /* fall thru... */
- case PHOTOMETRIC_MINISWHITE:
- case PHOTOMETRIC_MINISBLACK:
- if (planarconfig == PLANARCONFIG_CONTIG
- && img->samplesperpixel != 1
- && img->bitspersample < 8 ) {
- sprintf(emsg,
- "Sorry, can not handle contiguous data with %s=%d, "
- "and %s=%d and Bits/Sample=%d",
- photoTag, img->photometric,
- "Samples/pixel", img->samplesperpixel,
- img->bitspersample);
- goto fail_return;
- }
- break;
- case PHOTOMETRIC_YCBCR:
- /* It would probably be nice to have a reality check here. */
- if (planarconfig == PLANARCONFIG_CONTIG)
- /* can rely on libjpeg to convert to RGB */
- /* XXX should restore current state on exit */
- switch (compress) {
- case COMPRESSION_JPEG:
- /*
- * TODO: when complete tests verify complete desubsampling
- * and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
- * favor of tif_getimage.c native handling
- */
- TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
- img->photometric = PHOTOMETRIC_RGB;
- break;
- default:
- /* do nothing */;
- break;
- }
- /*
- * TODO: if at all meaningful and useful, make more complete
- * support check here, or better still, refactor to let supporting
- * code decide whether there is support and what meaningfull
- * error to return
- */
- break;
- case PHOTOMETRIC_RGB:
- if (colorchannels < 3) {
- sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
- "Color channels", colorchannels);
- goto fail_return;
- }
- break;
- case PHOTOMETRIC_SEPARATED:
- {
- uint16 inkset;
- TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
- if (inkset != INKSET_CMYK) {
- sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
- "InkSet", inkset);
- goto fail_return;
- }
- if (img->samplesperpixel < 4) {
- sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
- "Samples/pixel", img->samplesperpixel);
- goto fail_return;
- }
- }
- break;
- case PHOTOMETRIC_LOGL:
- if (compress != COMPRESSION_SGILOG) {
- sprintf(emsg, "Sorry, LogL data must have %s=%d",
- "Compression", COMPRESSION_SGILOG);
- goto fail_return;
- }
- TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
- img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
- img->bitspersample = 8;
- break;
- case PHOTOMETRIC_LOGLUV:
- if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
- sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
- "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
- goto fail_return;
- }
- if (planarconfig != PLANARCONFIG_CONTIG) {
- sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
- "Planarconfiguration", planarconfig);
- return (0);
- }
- TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
- img->photometric = PHOTOMETRIC_RGB; /* little white lie */
- img->bitspersample = 8;
- break;
- case PHOTOMETRIC_CIELAB:
- break;
- default:
- sprintf(emsg, "Sorry, can not handle image with %s=%d",
- photoTag, img->photometric);
- goto fail_return;
- }
- img->Map = NULL;
- img->BWmap = NULL;
- img->PALmap = NULL;
- img->ycbcr = NULL;
- img->cielab = NULL;
- img->UaToAa = NULL;
- img->Bitdepth16To8 = NULL;
- TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
- TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
- TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
- img->isContig =
- !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
- if (img->isContig) {
- if (!PickContigCase(img)) {
- sprintf(emsg, "Sorry, can not handle image");
- goto fail_return;
- }
- } else {
- if (!PickSeparateCase(img)) {
- sprintf(emsg, "Sorry, can not handle image");
- goto fail_return;
- }
- }
- return 1;
- fail_return:
- _TIFFfree( img->redcmap );
- _TIFFfree( img->greencmap );
- _TIFFfree( img->bluecmap );
- img->redcmap = img->greencmap = img->bluecmap = NULL;
- return 0;
- }
- int
- TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
- {
- if (img->get == NULL) {
- TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
- return (0);
- }
- if (img->put.any == NULL) {
- TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
- "No \"put\" routine setupl; probably can not handle image format");
- return (0);
- }
- return (*img->get)(img, raster, w, h);
- }
- /*
- * Read the specified image into an ABGR-format rastertaking in account
- * specified orientation.
- */
- int
- TIFFReadRGBAImageOriented(TIFF* tif,
- uint32 rwidth, uint32 rheight, uint32* raster,
- int orientation, int stop)
- {
- char emsg[1024] = "";
- TIFFRGBAImage img;
- int ok;
- if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
- img.req_orientation = orientation;
- /* XXX verify rwidth and rheight against width and height */
- ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
- rwidth, img.height);
- TIFFRGBAImageEnd(&img);
- } else {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
- ok = 0;
- }
- return (ok);
- }
- /*
- * Read the specified image into an ABGR-format raster. Use bottom left
- * origin for raster by default.
- */
- int
- TIFFReadRGBAImage(TIFF* tif,
- uint32 rwidth, uint32 rheight, uint32* raster, int stop)
- {
- return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
- ORIENTATION_BOTLEFT, stop);
- }
- static int
- setorientation(TIFFRGBAImage* img)
- {
- switch (img->orientation) {
- case ORIENTATION_TOPLEFT:
- case ORIENTATION_LEFTTOP:
- if (img->req_orientation == ORIENTATION_TOPRIGHT ||
- img->req_orientation == ORIENTATION_RIGHTTOP)
- return FLIP_HORIZONTALLY;
- else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
- img->req_orientation == ORIENTATION_RIGHTBOT)
- return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
- else if (img->req_orientation == ORIENTATION_BOTLEFT ||
- img->req_orientation == ORIENTATION_LEFTBOT)
- return FLIP_VERTICALLY;
- else
- return 0;
- case ORIENTATION_TOPRIGHT:
- case ORIENTATION_RIGHTTOP:
- if (img->req_orientation == ORIENTATION_TOPLEFT ||
- img->req_orientation == ORIENTATION_LEFTTOP)
- return FLIP_HORIZONTALLY;
- else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
- img->req_orientation == ORIENTATION_RIGHTBOT)
- return FLIP_VERTICALLY;
- else if (img->req_orientation == ORIENTATION_BOTLEFT ||
- img->req_orientation == ORIENTATION_LEFTBOT)
- return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
- else
- return 0;
- case ORIENTATION_BOTRIGHT:
- case ORIENTATION_RIGHTBOT:
- if (img->req_orientation == ORIENTATION_TOPLEFT ||
- img->req_orientation == ORIENTATION_LEFTTOP)
- return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
- else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
- img->req_orientation == ORIENTATION_RIGHTTOP)
- return FLIP_VERTICALLY;
- else if (img->req_orientation == ORIENTATION_BOTLEFT ||
- img->req_orientation == ORIENTATION_LEFTBOT)
- return FLIP_HORIZONTALLY;
- else
- return 0;
- case ORIENTATION_BOTLEFT:
- case ORIENTATION_LEFTBOT:
- if (img->req_orientation == ORIENTATION_TOPLEFT ||
- img->req_orientation == ORIENTATION_LEFTTOP)
- return FLIP_VERTICALLY;
- else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
- img->req_orientation == ORIENTATION_RIGHTTOP)
- return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
- else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
- img->req_orientation == ORIENTATION_RIGHTBOT)
- return FLIP_HORIZONTALLY;
- else
- return 0;
- default: /* NOTREACHED */
- return 0;
- }
- }
- /*
- * Get an tile-organized image that has
- * PlanarConfiguration contiguous if SamplesPerPixel > 1
- * or
- * SamplesPerPixel == 1
- */
- static int
- gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
- {
- TIFF* tif = img->tif;
- tileContigRoutine put = img->put.contig;
- uint32 col, row, y, rowstoread;
- tmsize_t pos;
- uint32 tw, th;
- unsigned char* buf;
- int32 fromskew, toskew;
- uint32 nrow;
- int ret = 1, flip;
- buf = (unsigned char*) _TIFFmalloc(TIFFTileSize(tif));
- if (buf == 0) {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
- return (0);
- }
- _TIFFmemset(buf, 0, TIFFTileSize(tif));
- TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
- TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
- flip = setorientation(img);
- if (flip & FLIP_VERTICALLY) {
- y = h - 1;
- toskew = -(int32)(tw + w);
- }
- else {
- y = 0;
- toskew = -(int32)(tw - w);
- }
-
- for (row = 0; row < h; row += nrow)
- {
- rowstoread = th - (row + img->row_offset) % th;
- nrow = (row + rowstoread > h ? h - row : rowstoread);
- for (col = 0; col < w; col += tw)
- {
- if (TIFFReadTile(tif, buf, col+img->col_offset,
- row+img->row_offset, 0, 0)==(tmsize_t)(-1) && img->stoponerr)
- {
- ret = 0;
- break;
- }
-
- pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);
- if (col + tw > w)
- {
- /*
- * Tile is clipped horizontally. Calculate
- * visible portion and skewing factors.
- */
- uint32 npix = w - col;
- fromskew = tw - npix;
- (*put)(img, raster+y*w+col, col, y,
- npix, nrow, fromskew, toskew + fromskew, buf + pos);
- }
- else
- {
- (*put)(img, raster+y*w+col, col, y, tw, nrow, 0, toskew, buf + pos);
- }
- }
- y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
- }
- _TIFFfree(buf);
- if (flip & FLIP_HORIZONTALLY) {
- uint32 line;
- for (line = 0; line < h; line++) {
- uint32 *left = raster + (line * w);
- uint32 *right = left + w - 1;
-
- while ( left < right ) {
- uint32 temp = *left;
- *left = *right;
- *right = temp;
- left++, right--;
- }
- }
- }
- return (ret);
- }
- /*
- * Get an tile-organized image that has
- * SamplesPerPixel > 1
- * PlanarConfiguration separated
- * We assume that all such images are RGB.
- */
- static int
- gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
- {
- TIFF* tif = img->tif;
- tileSeparateRoutine put = img->put.separate;
- uint32 col, row, y, rowstoread;
- tmsize_t pos;
- uint32 tw, th;
- unsigned char* buf;
- unsigned char* p0;
- unsigned char* p1;
- unsigned char* p2;
- unsigned char* pa;
- tmsize_t tilesize;
- tmsize_t bufsize;
- int32 fromskew, toskew;
- int alpha = img->alpha;
- uint32 nrow;
- int ret = 1, flip;
- int colorchannels;
- tilesize = TIFFTileSize(tif);
- bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,tilesize);
- if (bufsize == 0) {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtTileSeparate");
- return (0);
- }
- buf = (unsigned char*) _TIFFmalloc(bufsize);
- if (buf == 0) {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
- return (0);
- }
- _TIFFmemset(buf, 0, bufsize);
- p0 = buf;
- p1 = p0 + tilesize;
- p2 = p1 + tilesize;
- pa = (alpha?(p2+tilesize):NULL);
- TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
- TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
- flip = setorientation(img);
- if (flip & FLIP_VERTICALLY) {
- y = h - 1;
- toskew = -(int32)(tw + w);
- }
- else {
- y = 0;
- toskew = -(int32)(tw - w);
- }
- switch( img->photometric )
- {
- case PHOTOMETRIC_MINISWHITE:
- case PHOTOMETRIC_MINISBLACK:
- case PHOTOMETRIC_PALETTE:
- colorchannels = 1;
- p2 = p1 = p0;
- break;
- default:
- colorchannels = 3;
- break;
- }
- for (row = 0; row < h; row += nrow)
- {
- rowstoread = th - (row + img->row_offset) % th;
- nrow = (row + rowstoread > h ? h - row : rowstoread);
- for (col = 0; col < w; col += tw)
- {
- if (TIFFReadTile(tif, p0, col+img->col_offset,
- row+img->row_offset,0,0)==(tmsize_t)(-1) && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (colorchannels > 1
- && TIFFReadTile(tif, p1, col+img->col_offset,
- row+img->row_offset,0,1) == (tmsize_t)(-1)
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (colorchannels > 1
- && TIFFReadTile(tif, p2, col+img->col_offset,
- row+img->row_offset,0,2) == (tmsize_t)(-1)
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (alpha
- && TIFFReadTile(tif,pa,col+img->col_offset,
- row+img->row_offset,0,colorchannels) == (tmsize_t)(-1)
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
- pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);
- if (col + tw > w)
- {
- /*
- * Tile is clipped horizontally. Calculate
- * visible portion and skewing factors.
- */
- uint32 npix = w - col;
- fromskew = tw - npix;
- (*put)(img, raster+y*w+col, col, y,
- npix, nrow, fromskew, toskew + fromskew,
- p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
- } else {
- (*put)(img, raster+y*w+col, col, y,
- tw, nrow, 0, toskew, p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
- }
- }
- y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
- }
- if (flip & FLIP_HORIZONTALLY) {
- uint32 line;
- for (line = 0; line < h; line++) {
- uint32 *left = raster + (line * w);
- uint32 *right = left + w - 1;
- while ( left < right ) {
- uint32 temp = *left;
- *left = *right;
- *right = temp;
- left++, right--;
- }
- }
- }
- _TIFFfree(buf);
- return (ret);
- }
- /*
- * Get a strip-organized image that has
- * PlanarConfiguration contiguous if SamplesPerPixel > 1
- * or
- * SamplesPerPixel == 1
- */
- static int
- gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
- {
- TIFF* tif = img->tif;
- tileContigRoutine put = img->put.contig;
- uint32 row, y, nrow, nrowsub, rowstoread;
- tmsize_t pos;
- unsigned char* buf;
- uint32 rowsperstrip;
- uint16 subsamplinghor,subsamplingver;
- uint32 imagewidth = img->width;
- tmsize_t scanline;
- int32 fromskew, toskew;
- int ret = 1, flip;
- buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
- if (buf == 0) {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
- return (0);
- }
- _TIFFmemset(buf, 0, TIFFStripSize(tif));
- flip = setorientation(img);
- if (flip & FLIP_VERTICALLY) {
- y = h - 1;
- toskew = -(int32)(w + w);
- } else {
- y = 0;
- toskew = -(int32)(w - w);
- }
- TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
- TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
- scanline = TIFFScanlineSize(tif);
- fromskew = (w < imagewidth ? imagewidth - w : 0);
- for (row = 0; row < h; row += nrow)
- {
- rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
- nrow = (row + rowstoread > h ? h - row : rowstoread);
- nrowsub = nrow;
- if ((nrowsub%subsamplingver)!=0)
- nrowsub+=subsamplingver-nrowsub%subsamplingver;
- if (TIFFReadEncodedStrip(tif,
- TIFFComputeStrip(tif,row+img->row_offset, 0),
- buf,
- ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline)==(tmsize_t)(-1)
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
- pos = ((row + img->row_offset) % rowsperstrip) * scanline;
- (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
- y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
- }
- if (flip & FLIP_HORIZONTALLY) {
- uint32 line;
- for (line = 0; line < h; line++) {
- uint32 *left = raster + (line * w);
- uint32 *right = left + w - 1;
- while ( left < right ) {
- uint32 temp = *left;
- *left = *right;
- *right = temp;
- left++, right--;
- }
- }
- }
- _TIFFfree(buf);
- return (ret);
- }
- /*
- * Get a strip-organized image with
- * SamplesPerPixel > 1
- * PlanarConfiguration separated
- * We assume that all such images are RGB.
- */
- static int
- gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
- {
- TIFF* tif = img->tif;
- tileSeparateRoutine put = img->put.separate;
- unsigned char *buf;
- unsigned char *p0, *p1, *p2, *pa;
- uint32 row, y, nrow, rowstoread;
- tmsize_t pos;
- tmsize_t scanline;
- uint32 rowsperstrip, offset_row;
- uint32 imagewidth = img->width;
- tmsize_t stripsize;
- tmsize_t bufsize;
- int32 fromskew, toskew;
- int alpha = img->alpha;
- int ret = 1, flip, colorchannels;
- stripsize = TIFFStripSize(tif);
- bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,stripsize);
- if (bufsize == 0) {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtStripSeparate");
- return (0);
- }
- p0 = buf = (unsigned char *)_TIFFmalloc(bufsize);
- if (buf == 0) {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
- return (0);
- }
- _TIFFmemset(buf, 0, bufsize);
- p1 = p0 + stripsize;
- p2 = p1 + stripsize;
- pa = (alpha?(p2+stripsize):NULL);
- flip = setorientation(img);
- if (flip & FLIP_VERTICALLY) {
- y = h - 1;
- toskew = -(int32)(w + w);
- }
- else {
- y = 0;
- toskew = -(int32)(w - w);
- }
- switch( img->photometric )
- {
- case PHOTOMETRIC_MINISWHITE:
- case PHOTOMETRIC_MINISBLACK:
- case PHOTOMETRIC_PALETTE:
- colorchannels = 1;
- p2 = p1 = p0;
- break;
- default:
- colorchannels = 3;
- break;
- }
- TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
- scanline = TIFFScanlineSize(tif);
- fromskew = (w < imagewidth ? imagewidth - w : 0);
- for (row = 0; row < h; row += nrow)
- {
- rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
- nrow = (row + rowstoread > h ? h - row : rowstoread);
- offset_row = row + img->row_offset;
- if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
- p0, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (colorchannels > 1
- && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
- p1, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (colorchannels > 1
- && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
- p2, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
- if (alpha)
- {
- if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, colorchannels),
- pa, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
- && img->stoponerr)
- {
- ret = 0;
- break;
- }
- }
- pos = ((row + img->row_offset) % rowsperstrip) * scanline;
- (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, p0 + pos, p1 + pos,
- p2 + pos, (alpha?(pa+pos):NULL));
- y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
- }
- if (flip & FLIP_HORIZONTALLY) {
- uint32 line;
- for (line = 0; line < h; line++) {
- uint32 *left = raster + (line * w);
- uint32 *right = left + w - 1;
- while ( left < right ) {
- uint32 temp = *left;
- *left = *right;
- *right = temp;
- left++, right--;
- }
- }
- }
- _TIFFfree(buf);
- return (ret);
- }
- /*
- * The following routines move decoded data returned
- * from the TIFF library into rasters filled with packed
- * ABGR pixels (i.e. suitable for passing to lrecwrite.)
- *
- * The routines have been created according to the most
- * important cases and optimized. PickContigCase and
- * PickSeparateCase analyze the parameters and select
- * the appropriate "get" and "put" routine to use.
- */
- #define REPEAT8(op) REPEAT4(op); REPEAT4(op)
- #define REPEAT4(op) REPEAT2(op); REPEAT2(op)
- #define REPEAT2(op) op; op
- #define CASE8(x,op) \
- switch (x) { \
- case 7: op; case 6: op; case 5: op; \
- case 4: op; case 3: op; case 2: op; \
- case 1: op; \
- }
- #define CASE4(x,op) switch (x) { case 3: op; case 2: op; case 1: op; }
- #define NOP
- #define UNROLL8(w, op1, op2) { \
- uint32 _x; \
- for (_x = w; _x >= 8; _x -= 8) { \
- op1; \
- REPEAT8(op2); \
- } \
- if (_x > 0) { \
- op1; \
- CASE8(_x,op2); \
- } \
- }
- #define UNROLL4(w, op1, op2) { \
- uint32 _x; \
- for (_x = w; _x >= 4; _x -= 4) { \
- op1; \
- REPEAT4(op2); \
- } \
- if (_x > 0) { \
- op1; \
- CASE4(_x,op2); \
- } \
- }
- #define UNROLL2(w, op1, op2) { \
- uint32 _x; \
- for (_x = w; _x >= 2; _x -= 2) { \
- op1; \
- REPEAT2(op2); \
- } \
- if (_x) { \
- op1; \
- op2; \
- } \
- }
-
- #define SKEW(r,g,b,skew) { r += skew; g += skew; b += skew; }
- #define SKEW4(r,g,b,a,skew) { r += skew; g += skew; b += skew; a+= skew; }
- #define A1 (((uint32)0xffL)<<24)
- #define PACK(r,g,b) \
- ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
- #define PACK4(r,g,b,a) \
- ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
- #define W2B(v) (((v)>>8)&0xff)
- /* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
- #define PACKW(r,g,b) \
- ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
- #define PACKW4(r,g,b,a) \
- ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
- #define DECLAREContigPutFunc(name) \
- static void name(\
- TIFFRGBAImage* img, \
- uint32* cp, \
- uint32 x, uint32 y, \
- uint32 w, uint32 h, \
- int32 fromskew, int32 toskew, \
- unsigned char* pp \
- )
- /*
- * 8-bit palette => colormap/RGB
- */
- DECLAREContigPutFunc(put8bitcmaptile)
- {
- uint32** PALmap = img->PALmap;
- int samplesperpixel = img->samplesperpixel;
- (void) y;
- while (h-- > 0) {
- for (x = w; x-- > 0;)
- {
- *cp++ = PALmap[*pp][0];
- pp += samplesperpixel;
- }
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 4-bit palette => colormap/RGB
- */
- DECLAREContigPutFunc(put4bitcmaptile)
- {
- uint32** PALmap = img->PALmap;
- (void) x; (void) y;
- fromskew /= 2;
- while (h-- > 0) {
- uint32* bw;
- UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 2-bit palette => colormap/RGB
- */
- DECLAREContigPutFunc(put2bitcmaptile)
- {
- uint32** PALmap = img->PALmap;
- (void) x; (void) y;
- fromskew /= 4;
- while (h-- > 0) {
- uint32* bw;
- UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 1-bit palette => colormap/RGB
- */
- DECLAREContigPutFunc(put1bitcmaptile)
- {
- uint32** PALmap = img->PALmap;
- (void) x; (void) y;
- fromskew /= 8;
- while (h-- > 0) {
- uint32* bw;
- UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 8-bit greyscale => colormap/RGB
- */
- DECLAREContigPutFunc(putgreytile)
- {
- int samplesperpixel = img->samplesperpixel;
- uint32** BWmap = img->BWmap;
- (void) y;
- while (h-- > 0) {
- for (x = w; x-- > 0;)
- {
- *cp++ = BWmap[*pp][0];
- pp += samplesperpixel;
- }
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 8-bit greyscale with associated alpha => colormap/RGBA
- */
- DECLAREContigPutFunc(putagreytile)
- {
- int samplesperpixel = img->samplesperpixel;
- uint32** BWmap = img->BWmap;
- (void) y;
- while (h-- > 0) {
- for (x = w; x-- > 0;)
- {
- *cp++ = BWmap[*pp][0] & (*(pp+1) << 24 | ~A1);
- pp += samplesperpixel;
- }
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 16-bit greyscale => colormap/RGB
- */
- DECLAREContigPutFunc(put16bitbwtile)
- {
- int samplesperpixel = img->samplesperpixel;
- uint32** BWmap = img->BWmap;
- (void) y;
- while (h-- > 0) {
- uint16 *wp = (uint16 *) pp;
- for (x = w; x-- > 0;)
- {
- /* use high order byte of 16bit value */
- *cp++ = BWmap[*wp >> 8][0];
- pp += 2 * samplesperpixel;
- wp += samplesperpixel;
- }
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 1-bit bilevel => colormap/RGB
- */
- DECLAREContigPutFunc(put1bitbwtile)
- {
- uint32** BWmap = img->BWmap;
- (void) x; (void) y;
- fromskew /= 8;
- while (h-- > 0) {
- uint32* bw;
- UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 2-bit greyscale => colormap/RGB
- */
- DECLAREContigPutFunc(put2bitbwtile)
- {
- uint32** BWmap = img->BWmap;
- (void) x; (void) y;
- fromskew /= 4;
- while (h-- > 0) {
- uint32* bw;
- UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 4-bit greyscale => colormap/RGB
- */
- DECLAREContigPutFunc(put4bitbwtile)
- {
- uint32** BWmap = img->BWmap;
- (void) x; (void) y;
- fromskew /= 2;
- while (h-- > 0) {
- uint32* bw;
- UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 8-bit packed samples, no Map => RGB
- */
- DECLAREContigPutFunc(putRGBcontig8bittile)
- {
- int samplesperpixel = img->samplesperpixel;
- (void) x; (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- UNROLL8(w, NOP,
- *cp++ = PACK(pp[0], pp[1], pp[2]);
- pp += samplesperpixel);
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 8-bit packed samples => RGBA w/ associated alpha
- * (known to have Map == NULL)
- */
- DECLAREContigPutFunc(putRGBAAcontig8bittile)
- {
- int samplesperpixel = img->samplesperpixel;
- (void) x; (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- UNROLL8(w, NOP,
- *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
- pp += samplesperpixel);
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 8-bit packed samples => RGBA w/ unassociated alpha
- * (known to have Map == NULL)
- */
- DECLAREContigPutFunc(putRGBUAcontig8bittile)
- {
- int samplesperpixel = img->samplesperpixel;
- (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- uint32 r, g, b, a;
- uint8* m;
- for (x = w; x-- > 0;) {
- a = pp[3];
- m = img->UaToAa+(a<<8);
- r = m[pp[0]];
- g = m[pp[1]];
- b = m[pp[2]];
- *cp++ = PACK4(r,g,b,a);
- pp += samplesperpixel;
- }
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 16-bit packed samples => RGB
- */
- DECLAREContigPutFunc(putRGBcontig16bittile)
- {
- int samplesperpixel = img->samplesperpixel;
- uint16 *wp = (uint16 *)pp;
- (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- for (x = w; x-- > 0;) {
- *cp++ = PACK(img->Bitdepth16To8[wp[0]],
- img->Bitdepth16To8[wp[1]],
- img->Bitdepth16To8[wp[2]]);
- wp += samplesperpixel;
- }
- cp += toskew;
- wp += fromskew;
- }
- }
- /*
- * 16-bit packed samples => RGBA w/ associated alpha
- * (known to have Map == NULL)
- */
- DECLAREContigPutFunc(putRGBAAcontig16bittile)
- {
- int samplesperpixel = img->samplesperpixel;
- uint16 *wp = (uint16 *)pp;
- (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- for (x = w; x-- > 0;) {
- *cp++ = PACK4(img->Bitdepth16To8[wp[0]],
- img->Bitdepth16To8[wp[1]],
- img->Bitdepth16To8[wp[2]],
- img->Bitdepth16To8[wp[3]]);
- wp += samplesperpixel;
- }
- cp += toskew;
- wp += fromskew;
- }
- }
- /*
- * 16-bit packed samples => RGBA w/ unassociated alpha
- * (known to have Map == NULL)
- */
- DECLAREContigPutFunc(putRGBUAcontig16bittile)
- {
- int samplesperpixel = img->samplesperpixel;
- uint16 *wp = (uint16 *)pp;
- (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- uint32 r,g,b,a;
- uint8* m;
- for (x = w; x-- > 0;) {
- a = img->Bitdepth16To8[wp[3]];
- m = img->UaToAa+(a<<8);
- r = m[img->Bitdepth16To8[wp[0]]];
- g = m[img->Bitdepth16To8[wp[1]]];
- b = m[img->Bitdepth16To8[wp[2]]];
- *cp++ = PACK4(r,g,b,a);
- wp += samplesperpixel;
- }
- cp += toskew;
- wp += fromskew;
- }
- }
- /*
- * 8-bit packed CMYK samples w/o Map => RGB
- *
- * NB: The conversion of CMYK->RGB is *very* crude.
- */
- DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
- {
- int samplesperpixel = img->samplesperpixel;
- uint16 r, g, b, k;
- (void) x; (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- UNROLL8(w, NOP,
- k = 255 - pp[3];
- r = (k*(255-pp[0]))/255;
- g = (k*(255-pp[1]))/255;
- b = (k*(255-pp[2]))/255;
- *cp++ = PACK(r, g, b);
- pp += samplesperpixel);
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * 8-bit packed CMYK samples w/Map => RGB
- *
- * NB: The conversion of CMYK->RGB is *very* crude.
- */
- DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
- {
- int samplesperpixel = img->samplesperpixel;
- TIFFRGBValue* Map = img->Map;
- uint16 r, g, b, k;
- (void) y;
- fromskew *= samplesperpixel;
- while (h-- > 0) {
- for (x = w; x-- > 0;) {
- k = 255 - pp[3];
- r = (k*(255-pp[0]))/255;
- g = (k*(255-pp[1]))/255;
- b = (k*(255-pp[2]))/255;
- *cp++ = PACK(Map[r], Map[g], Map[b]);
- pp += samplesperpixel;
- }
- pp += fromskew;
- cp += toskew;
- }
- }
- #define DECLARESepPutFunc(name) \
- static void name(\
- TIFFRGBAImage* img,\
- uint32* cp,\
- uint32 x, uint32 y, \
- uint32 w, uint32 h,\
- int32 fromskew, int32 toskew,\
- unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a\
- )
- /*
- * 8-bit unpacked samples => RGB
- */
- DECLARESepPutFunc(putRGBseparate8bittile)
- {
- (void) img; (void) x; (void) y; (void) a;
- while (h-- > 0) {
- UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
- SKEW(r, g, b, fromskew);
- cp += toskew;
- }
- }
- /*
- * 8-bit unpacked samples => RGBA w/ associated alpha
- */
- DECLARESepPutFunc(putRGBAAseparate8bittile)
- {
- (void) img; (void) x; (void) y;
- while (h-- > 0) {
- UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
- SKEW4(r, g, b, a, fromskew);
- cp += toskew;
- }
- }
- /*
- * 8-bit unpacked CMYK samples => RGBA
- */
- DECLARESepPutFunc(putCMYKseparate8bittile)
- {
- (void) img; (void) y;
- while (h-- > 0) {
- uint32 rv, gv, bv, kv;
- for (x = w; x-- > 0;) {
- kv = 255 - *a++;
- rv = (kv*(255-*r++))/255;
- gv = (kv*(255-*g++))/255;
- bv = (kv*(255-*b++))/255;
- *cp++ = PACK4(rv,gv,bv,255);
- }
- SKEW4(r, g, b, a, fromskew);
- cp += toskew;
- }
- }
- /*
- * 8-bit unpacked samples => RGBA w/ unassociated alpha
- */
- DECLARESepPutFunc(putRGBUAseparate8bittile)
- {
- (void) img; (void) y;
- while (h-- > 0) {
- uint32 rv, gv, bv, av;
- uint8* m;
- for (x = w; x-- > 0;) {
- av = *a++;
- m = img->UaToAa+(av<<8);
- rv = m[*r++];
- gv = m[*g++];
- bv = m[*b++];
- *cp++ = PACK4(rv,gv,bv,av);
- }
- SKEW4(r, g, b, a, fromskew);
- cp += toskew;
- }
- }
- /*
- * 16-bit unpacked samples => RGB
- */
- DECLARESepPutFunc(putRGBseparate16bittile)
- {
- uint16 *wr = (uint16*) r;
- uint16 *wg = (uint16*) g;
- uint16 *wb = (uint16*) b;
- (void) img; (void) y; (void) a;
- while (h-- > 0) {
- for (x = 0; x < w; x++)
- *cp++ = PACK(img->Bitdepth16To8[*wr++],
- img->Bitdepth16To8[*wg++],
- img->Bitdepth16To8[*wb++]);
- SKEW(wr, wg, wb, fromskew);
- cp += toskew;
- }
- }
- /*
- * 16-bit unpacked samples => RGBA w/ associated alpha
- */
- DECLARESepPutFunc(putRGBAAseparate16bittile)
- {
- uint16 *wr = (uint16*) r;
- uint16 *wg = (uint16*) g;
- uint16 *wb = (uint16*) b;
- uint16 *wa = (uint16*) a;
- (void) img; (void) y;
- while (h-- > 0) {
- for (x = 0; x < w; x++)
- *cp++ = PACK4(img->Bitdepth16To8[*wr++],
- img->Bitdepth16To8[*wg++],
- img->Bitdepth16To8[*wb++],
- img->Bitdepth16To8[*wa++]);
- SKEW4(wr, wg, wb, wa, fromskew);
- cp += toskew;
- }
- }
- /*
- * 16-bit unpacked samples => RGBA w/ unassociated alpha
- */
- DECLARESepPutFunc(putRGBUAseparate16bittile)
- {
- uint16 *wr = (uint16*) r;
- uint16 *wg = (uint16*) g;
- uint16 *wb = (uint16*) b;
- uint16 *wa = (uint16*) a;
- (void) img; (void) y;
- while (h-- > 0) {
- uint32 r,g,b,a;
- uint8* m;
- for (x = w; x-- > 0;) {
- a = img->Bitdepth16To8[*wa++];
- m = img->UaToAa+(a<<8);
- r = m[img->Bitdepth16To8[*wr++]];
- g = m[img->Bitdepth16To8[*wg++]];
- b = m[img->Bitdepth16To8[*wb++]];
- *cp++ = PACK4(r,g,b,a);
- }
- SKEW4(wr, wg, wb, wa, fromskew);
- cp += toskew;
- }
- }
- /*
- * 8-bit packed CIE L*a*b 1976 samples => RGB
- */
- DECLAREContigPutFunc(putcontig8bitCIELab)
- {
- float X, Y, Z;
- uint32 r, g, b;
- (void) y;
- fromskew *= 3;
- while (h-- > 0) {
- for (x = w; x-- > 0;) {
- TIFFCIELabToXYZ(img->cielab,
- (unsigned char)pp[0],
- (signed char)pp[1],
- (signed char)pp[2],
- &X, &Y, &Z);
- TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
- *cp++ = PACK(r, g, b);
- pp += 3;
- }
- cp += toskew;
- pp += fromskew;
- }
- }
- /*
- * YCbCr -> RGB conversion and packing routines.
- */
- #define YCbCrtoRGB(dst, Y) { \
- uint32 r, g, b; \
- TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b); \
- dst = PACK(r, g, b); \
- }
- /*
- * 8-bit packed YCbCr samples => RGB
- * This function is generic for different sampling sizes,
- * and can handle blocks sizes that aren't multiples of the
- * sampling size. However, it is substantially less optimized
- * than the specific sampling cases. It is used as a fallback
- * for difficult blocks.
- */
- #ifdef notdef
- static void putcontig8bitYCbCrGenericTile(
- TIFFRGBAImage* img,
- uint32* cp,
- uint32 x, uint32 y,
- uint32 w, uint32 h,
- int32 fromskew, int32 toskew,
- unsigned char* pp,
- int h_group,
- int v_group )
- {
- uint32* cp1 = cp+w+toskew;
- uint32* cp2 = cp1+w+toskew;
- uint32* cp3 = cp2+w+toskew;
- int32 incr = 3*w+4*toskew;
- int32 Cb, Cr;
- int group_size = v_group * h_group + 2;
- (void) y;
- fromskew = (fromskew * group_size) / h_group;
- for( yy = 0; yy < h; yy++ )
- {
- unsigned char *pp_line;
- int y_line_group = yy / v_group;
- int y_remainder = yy - y_line_group * v_group;
- pp_line = pp + v_line_group *
-
- for( xx = 0; xx < w; xx++ )
- {
- Cb = pp
- }
- }
- for (; h >= 4; h -= 4) {
- x = w>>2;
- do {
- Cb = pp[16];
- Cr = pp[17];
- YCbCrtoRGB(cp [0], pp[ 0]);
- YCbCrtoRGB(cp [1], pp[ 1]);
- YCbCrtoRGB(cp [2], pp[ 2]);
- YCbCrtoRGB(cp [3], pp[ 3]);
- YCbCrtoRGB(cp1[0], pp[ 4]);
- YCbCrtoRGB(cp1[1], pp[ 5]);
- YCbCrtoRGB(cp1[2], pp[ 6]);
- YCbCrtoRGB(cp1[3], pp[ 7]);
- YCbCrtoRGB(cp2[0], pp[ 8]);
- YCbCrtoRGB(cp2[1], pp[ 9]);
- YCbCrtoRGB(cp2[2], pp[10]);
- YCbCrtoRGB(cp2[3], pp[11]);
- YCbCrtoRGB(cp3[0], pp[12]);
- YCbCrtoRGB(cp3[1], pp[13]);
- YCbCrtoRGB(cp3[2], pp[14]);
- YCbCrtoRGB(cp3[3], pp[15]);
- cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
- pp += 18;
- } while (--x);
- cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
- pp += fromskew;
- }
- }
- #endif
- /*
- * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
- */
- DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
- {
- uint32* cp1 = cp+w+toskew;
- uint32* cp2 = cp1+w+toskew;
- uint32* cp3 = cp2+w+toskew;
- int32 incr = 3*w+4*toskew;
- (void) y;
- /* adjust fromskew */
- fromskew = (fromskew * 18) / 4;
- if ((h & 3) == 0 && (w & 3) == 0) {
- for (; h >= 4; h -= 4) {
- x = w>>2;
- do {
- int32 Cb = pp[16];
- int32 Cr = pp[17];
- YCbCrtoRGB(cp [0], pp[ 0]);
- YCbCrtoRGB(cp [1], pp[ 1]);
- YCbCrtoRGB(cp [2], pp[ 2]);
- YCbCrtoRGB(cp [3], pp[ 3]);
- YCbCrtoRGB(cp1[0], pp[ 4]);
- YCbCrtoRGB(cp1[1], pp[ 5]);
- YCbCrtoRGB(cp1[2], pp[ 6]);
- YCbCrtoRGB(cp1[3], pp[ 7]);
- YCbCrtoRGB(cp2[0], pp[ 8]);
- YCbCrtoRGB(cp2[1], pp[ 9]);
- YCbCrtoRGB(cp2[2], pp[10]);
- YCbCrtoRGB(cp2[3], pp[11]);
- YCbCrtoRGB(cp3[0], pp[12]);
- YCbCrtoRGB(cp3[1], pp[13]);
- YCbCrtoRGB(cp3[2], pp[14]);
- YCbCrtoRGB(cp3[3], pp[15]);
- cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
- pp += 18;
- } while (--x);
- cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
- pp += fromskew;
- }
- } else {
- while (h > 0) {
- for (x = w; x > 0;) {
- int32 Cb = pp[16];
- int32 Cr = pp[17];
- switch (x) {
- default:
- switch (h) {
- default: YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
- case 3: YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
- case 2: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
- case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
- } /* FALLTHROUGH */
- case 3:
- switch (h) {
- default: YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
- case 3: YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
- case 2: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
- case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
- } /* FALLTHROUGH */
- case 2:
- switch (h) {
- default: YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
- case 3: YCbCrtoRGB(cp2[1], pp[ 9]); /* FALLTHROUGH */
- case 2: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
- case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
- } /* FALLTHROUGH */
- case 1:
- switch (h) {
- default: YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
- case 3: YCbCrtoRGB(cp2[0], pp[ 8]); /* FALLTHROUGH */
- case 2: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
- case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
- } /* FALLTHROUGH */
- }
- if (x < 4) {
- cp += x; cp1 += x; cp2 += x; cp3 += x;
- x = 0;
- }
- else {
- cp += 4; cp1 += 4; cp2 += 4; cp3 += 4;
- x -= 4;
- }
- pp += 18;
- }
- if (h <= 4)
- break;
- h -= 4;
- cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
- pp += fromskew;
- }
- }
- }
- /*
- * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
- */
- DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
- {
- uint32* cp1 = cp+w+toskew;
- int32 incr = 2*toskew+w;
- (void) y;
- fromskew = (fromskew * 10) / 4;
- if ((h & 3) == 0 && (w & 1) == 0) {
- for (; h >= 2; h -= 2) {
- x = w>>2;
- do {
- int32 Cb = pp[8];
- int32 Cr = pp[9];
-
- YCbCrtoRGB(cp [0], pp[0]);
- YCbCrtoRGB(cp [1], pp[1]);
- YCbCrtoRGB(cp [2], pp[2]);
- YCbCrtoRGB(cp [3], pp[3]);
- YCbCrtoRGB(cp1[0], pp[4]);
- YCbCrtoRGB(cp1[1], pp[5]);
- YCbCrtoRGB(cp1[2], pp[6]);
- YCbCrtoRGB(cp1[3], pp[7]);
-
- cp += 4, cp1 += 4;
- pp += 10;
- } while (--x);
- cp += incr, cp1 += incr;
- pp += fromskew;
- }
- } else {
- while (h > 0) {
- for (x = w; x > 0;) {
- int32 Cb = pp[8];
- int32 Cr = pp[9];
- switch (x) {
- default:
- switch (h) {
- default: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
- case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
- } /* FALLTHROUGH */
- case 3:
- switch (h) {
- default: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
- case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
- } /* FALLTHROUGH */
- case 2:
- switch (h) {
- default: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
- case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
- } /* FALLTHROUGH */
- case 1:
- switch (h) {
- default: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
- case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
- } /* FALLTHROUGH */
- }
- if (x < 4) {
- cp += x; cp1 += x;
- x = 0;
- }
- else {
- cp += 4; cp1 += 4;
- x -= 4;
- }
- pp += 10;
- }
- if (h <= 2)
- break;
- h -= 2;
- cp += incr, cp1 += incr;
- pp += fromskew;
- }
- }
- }
- /*
- * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
- */
- DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
- {
- (void) y;
- /* XXX adjust fromskew */
- do {
- x = w>>2;
- do {
- int32 Cb = pp[4];
- int32 Cr = pp[5];
- YCbCrtoRGB(cp [0], pp[0]);
- YCbCrtoRGB(cp [1], pp[1]);
- YCbCrtoRGB(cp [2], pp[2]);
- YCbCrtoRGB(cp [3], pp[3]);
- cp += 4;
- pp += 6;
- } while (--x);
- if( (w&3) != 0 )
- {
- int32 Cb = pp[4];
- int32 Cr = pp[5];
- switch( (w&3) ) {
- case 3: YCbCrtoRGB(cp [2], pp[2]);
- case 2: YCbCrtoRGB(cp [1], pp[1]);
- case 1: YCbCrtoRGB(cp [0], pp[0]);
- case 0: break;
- }
- cp += (w&3);
- pp += 6;
- }
- cp += toskew;
- pp += fromskew;
- } while (--h);
- }
- /*
- * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
- */
- DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
- {
- uint32* cp2;
- int32 incr = 2*toskew+w;
- (void) y;
- fromskew = (fromskew / 2) * 6;
- cp2 = cp+w+toskew;
- while (h>=2) {
- x = w;
- while (x>=2) {
- uint32 Cb = pp[4];
- uint32 Cr = pp[5];
- YCbCrtoRGB(cp[0], pp[0]);
- YCbCrtoRGB(cp[1], pp[1]);
- YCbCrtoRGB(cp2[0], pp[2]);
- YCbCrtoRGB(cp2[1], pp[3]);
- cp += 2;
- cp2 += 2;
- pp += 6;
- x -= 2;
- }
- if (x==1) {
- uint32 Cb = pp[4];
- uint32 Cr = pp[5];
- YCbCrtoRGB(cp[0], pp[0]);
- YCbCrtoRGB(cp2[0], pp[2]);
- cp ++ ;
- cp2 ++ ;
- pp += 6;
- }
- cp += incr;
- cp2 += incr;
- pp += fromskew;
- h-=2;
- }
- if (h==1) {
- x = w;
- while (x>=2) {
- uint32 Cb = pp[4];
- uint32 Cr = pp[5];
- YCbCrtoRGB(cp[0], pp[0]);
- YCbCrtoRGB(cp[1], pp[1]);
- cp += 2;
- cp2 += 2;
- pp += 6;
- x -= 2;
- }
- if (x==1) {
- uint32 Cb = pp[4];
- uint32 Cr = pp[5];
- YCbCrtoRGB(cp[0], pp[0]);
- }
- }
- }
- /*
- * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
- */
- DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
- {
- (void) y;
- fromskew = (fromskew * 4) / 2;
- do {
- x = w>>1;
- do {
- int32 Cb = pp[2];
- int32 Cr = pp[3];
- YCbCrtoRGB(cp[0], pp[0]);
- YCbCrtoRGB(cp[1], pp[1]);
- cp += 2;
- pp += 4;
- } while (--x);
- if( (w&1) != 0 )
- {
- int32 Cb = pp[2];
- int32 Cr = pp[3];
- YCbCrtoRGB(cp[0], pp[0]);
- cp += 1;
- pp += 4;
- }
- cp += toskew;
- pp += fromskew;
- } while (--h);
- }
- /*
- * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
- */
- DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
- {
- uint32* cp2;
- int32 incr = 2*toskew+w;
- (void) y;
- fromskew = (fromskew / 2) * 4;
- cp2 = cp+w+toskew;
- while (h>=2) {
- x = w;
- do {
- uint32 Cb = pp[2];
- uint32 Cr = pp[3];
- YCbCrtoRGB(cp[0], pp[0]);
- YCbCrtoRGB(cp2[0], pp[1]);
- cp ++;
- cp2 ++;
- pp += 4;
- } while (--x);
- cp += incr;
- cp2 += incr;
- pp += fromskew;
- h-=2;
- }
- if (h==1) {
- x = w;
- do {
- uint32 Cb = pp[2];
- uint32 Cr = pp[3];
- YCbCrtoRGB(cp[0], pp[0]);
- cp ++;
- pp += 4;
- } while (--x);
- }
- }
- /*
- * 8-bit packed YCbCr samples w/ no subsampling => RGB
- */
- DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
- {
- (void) y;
- fromskew *= 3;
- do {
- x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
- do {
- int32 Cb = pp[1];
- int32 Cr = pp[2];
- YCbCrtoRGB(*cp++, pp[0]);
- pp += 3;
- } while (--x);
- cp += toskew;
- pp += fromskew;
- } while (--h);
- }
- /*
- * 8-bit packed YCbCr samples w/ no subsampling => RGB
- */
- DECLARESepPutFunc(putseparate8bitYCbCr11tile)
- {
- (void) y;
- (void) a;
- /* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
- while (h-- > 0) {
- x = w;
- do {
- uint32 dr, dg, db;
- TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
- *cp++ = PACK(dr,dg,db);
- } while (--x);
- SKEW(r, g, b, fromskew);
- cp += toskew;
- }
- }
- #undef YCbCrtoRGB
- static int
- initYCbCrConversion(TIFFRGBAImage* img)
- {
- static const char module[] = "initYCbCrConversion";
- float *luma, *refBlackWhite;
- if (img->ycbcr == NULL) {
- img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
- TIFFroundup_32(sizeof (TIFFYCbCrToRGB), sizeof (long))
- + 4*256*sizeof (TIFFRGBValue)
- + 2*256*sizeof (int)
- + 3*256*sizeof (int32)
- );
- if (img->ycbcr == NULL) {
- TIFFErrorExt(img->tif->tif_clientdata, module,
- "No space for YCbCr->RGB conversion state");
- return (0);
- }
- }
- TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
- TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
- &refBlackWhite);
- if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
- return(0);
- return (1);
- }
- static tileContigRoutine
- initCIELabConversion(TIFFRGBAImage* img)
- {
- static const char module[] = "initCIELabConversion";
- float *whitePoint;
- float refWhite[3];
- if (!img->cielab) {
- img->cielab = (TIFFCIELabToRGB *)
- _TIFFmalloc(sizeof(TIFFCIELabToRGB));
- if (!img->cielab) {
- TIFFErrorExt(img->tif->tif_clientdata, module,
- "No space for CIE L*a*b*->RGB conversion state.");
- return NULL;
- }
- }
- TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
- refWhite[1] = 100.0F;
- refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
- refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
- / whitePoint[1] * refWhite[1];
- if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {
- TIFFErrorExt(img->tif->tif_clientdata, module,
- "Failed to initialize CIE L*a*b*->RGB conversion state.");
- _TIFFfree(img->cielab);
- return NULL;
- }
- return putcontig8bitCIELab;
- }
- /*
- * Greyscale images with less than 8 bits/sample are handled
- * with a table to avoid lots of shifts and masks. The table
- * is setup so that put*bwtile (below) can retrieve 8/bitspersample
- * pixel values simply by indexing into the table with one
- * number.
- */
- static int
- makebwmap(TIFFRGBAImage* img)
- {
- TIFFRGBValue* Map = img->Map;
- int bitspersample = img->bitspersample;
- int nsamples = 8 / bitspersample;
- int i;
- uint32* p;
- if( nsamples == 0 )
- nsamples = 1;
- img->BWmap = (uint32**) _TIFFmalloc(
- 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
- if (img->BWmap == NULL) {
- TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");
- return (0);
- }
- p = (uint32*)(img->BWmap + 256);
- for (i = 0; i < 256; i++) {
- TIFFRGBValue c;
- img->BWmap[i] = p;
- switch (bitspersample) {
- #define GREY(x) c = Map[x]; *p++ = PACK(c,c,c);
- case 1:
- GREY(i>>7);
- GREY((i>>6)&1);
- GREY((i>>5)&1);
- GREY((i>>4)&1);
- GREY((i>>3)&1);
- GREY((i>>2)&1);
- GREY((i>>1)&1);
- GREY(i&1);
- break;
- case 2:
- GREY(i>>6);
- GREY((i>>4)&3);
- GREY((i>>2)&3);
- GREY(i&3);
- break;
- case 4:
- GREY(i>>4);
- GREY(i&0xf);
- break;
- case 8:
- case 16:
- GREY(i);
- break;
- }
- #undef GREY
- }
- return (1);
- }
- /*
- * Construct a mapping table to convert from the range
- * of the data samples to [0,255] --for display. This
- * process also handles inverting B&W images when needed.
- */
- static int
- setupMap(TIFFRGBAImage* img)
- {
- int32 x, range;
- range = (int32)((1L<<img->bitspersample)-1);
-
- /* treat 16 bit the same as eight bit */
- if( img->bitspersample == 16 )
- range = (int32) 255;
- img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
- if (img->Map == NULL) {
- TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
- "No space for photometric conversion table");
- return (0);
- }
- if (img->photometric == PHOTOMETRIC_MINISWHITE) {
- for (x = 0; x <= range; x++)
- img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);
- } else {
- for (x = 0; x <= range; x++)
- img->Map[x] = (TIFFRGBValue) ((x * 255) / range);
- }
- if (img->bitspersample <= 16 &&
- (img->photometric == PHOTOMETRIC_MINISBLACK ||
- img->photometric == PHOTOMETRIC_MINISWHITE)) {
- /*
- * Use photometric mapping table to construct
- * unpacking tables for samples <= 8 bits.
- */
- if (!makebwmap(img))
- return (0);
- /* no longer need Map, free it */
- _TIFFfree(img->Map), img->Map = NULL;
- }
- return (1);
- }
- static int
- checkcmap(TIFFRGBAImage* img)
- {
- uint16* r = img->redcmap;
- uint16* g = img->greencmap;
- uint16* b = img->bluecmap;
- long n = 1L<<img->bitspersample;
- while (n-- > 0)
- if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
- return (16);
- return (8);
- }
- static void
- cvtcmap(TIFFRGBAImage* img)
- {
- uint16* r = img->redcmap;
- uint16* g = img->greencmap;
- uint16* b = img->bluecmap;
- long i;
- for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {
- #define CVT(x) ((uint16)((x)>>8))
- r[i] = CVT(r[i]);
- g[i] = CVT(g[i]);
- b[i] = CVT(b[i]);
- #undef CVT
- }
- }
- /*
- * Palette images with <= 8 bits/sample are handled
- * with a table to avoid lots of shifts and masks. The table
- * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
- * pixel values simply by indexing into the table with one
- * number.
- */
- static int
- makecmap(TIFFRGBAImage* img)
- {
- int bitspersample = img->bitspersample;
- int nsamples = 8 / bitspersample;
- uint16* r = img->redcmap;
- uint16* g = img->greencmap;
- uint16* b = img->bluecmap;
- uint32 *p;
- int i;
- img->PALmap = (uint32**) _TIFFmalloc(
- 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
- if (img->PALmap == NULL) {
- TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");
- return (0);
- }
- p = (uint32*)(img->PALmap + 256);
- for (i = 0; i < 256; i++) {
- TIFFRGBValue c;
- img->PALmap[i] = p;
- #define CMAP(x) c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
- switch (bitspersample) {
- case 1:
- CMAP(i>>7);
- CMAP((i>>6)&1);
- CMAP((i>>5)&1);
- CMAP((i>>4)&1);
- CMAP((i>>3)&1);
- CMAP((i>>2)&1);
- CMAP((i>>1)&1);
- CMAP(i&1);
- break;
- case 2:
- CMAP(i>>6);
- CMAP((i>>4)&3);
- CMAP((i>>2)&3);
- CMAP(i&3);
- break;
- case 4:
- CMAP(i>>4);
- CMAP(i&0xf);
- break;
- case 8:
- CMAP(i);
- break;
- }
- #undef CMAP
- }
- return (1);
- }
- /*
- * Construct any mapping table used
- * by the associated put routine.
- */
- static int
- buildMap(TIFFRGBAImage* img)
- {
- switch (img->photometric) {
- case PHOTOMETRIC_RGB:
- case PHOTOMETRIC_YCBCR:
- case PHOTOMETRIC_SEPARATED:
- if (img->bitspersample == 8)
- break;
- /* fall thru... */
- case PHOTOMETRIC_MINISBLACK:
- case PHOTOMETRIC_MINISWHITE:
- if (!setupMap(img))
- return (0);
- break;
- case PHOTOMETRIC_PALETTE:
- /*
- * Convert 16-bit colormap to 8-bit (unless it looks
- * like an old-style 8-bit colormap).
- */
- if (checkcmap(img) == 16)
- cvtcmap(img);
- else
- TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");
- /*
- * Use mapping table and colormap to construct
- * unpacking tables for samples < 8 bits.
- */
- if (img->bitspersample <= 8 && !makecmap(img))
- return (0);
- break;
- }
- return (1);
- }
- /*
- * Select the appropriate conversion routine for packed data.
- */
- static int
- PickContigCase(TIFFRGBAImage* img)
- {
- img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
- img->put.contig = NULL;
- switch (img->photometric) {
- case PHOTOMETRIC_RGB:
- switch (img->bitspersample) {
- case 8:
- if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
- img->put.contig = putRGBAAcontig8bittile;
- else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
- {
- if (BuildMapUaToAa(img))
- img->put.contig = putRGBUAcontig8bittile;
- }
- else
- img->put.contig = putRGBcontig8bittile;
- break;
- case 16:
- if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
- {
- if (BuildMapBitdepth16To8(img))
- img->put.contig = putRGBAAcontig16bittile;
- }
- else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
- {
- if (BuildMapBitdepth16To8(img) &&
- BuildMapUaToAa(img))
- img->put.contig = putRGBUAcontig16bittile;
- }
- else
- {
- if (BuildMapBitdepth16To8(img))
- img->put.contig = putRGBcontig16bittile;
- }
- break;
- }
- break;
- case PHOTOMETRIC_SEPARATED:
- if (buildMap(img)) {
- if (img->bitspersample == 8) {
- if (!img->Map)
- img->put.contig = putRGBcontig8bitCMYKtile;
- else
- img->put.contig = putRGBcontig8bitCMYKMaptile;
- }
- }
- break;
- case PHOTOMETRIC_PALETTE:
- if (buildMap(img)) {
- switch (img->bitspersample) {
- case 8:
- img->put.contig = put8bitcmaptile;
- break;
- case 4:
- img->put.contig = put4bitcmaptile;
- break;
- case 2:
- img->put.contig = put2bitcmaptile;
- break;
- case 1:
- img->put.contig = put1bitcmaptile;
- break;
- }
- }
- break;
- case PHOTOMETRIC_MINISWHITE:
- case PHOTOMETRIC_MINISBLACK:
- if (buildMap(img)) {
- switch (img->bitspersample) {
- case 16:
- img->put.contig = put16bitbwtile;
- break;
- case 8:
- if (img->alpha && img->samplesperpixel == 2)
- img->put.contig = putagreytile;
- else
- img->put.contig = putgreytile;
- break;
- case 4:
- img->put.contig = put4bitbwtile;
- break;
- case 2:
- img->put.contig = put2bitbwtile;
- break;
- case 1:
- img->put.contig = put1bitbwtile;
- break;
- }
- }
- break;
- case PHOTOMETRIC_YCBCR:
- if ((img->bitspersample==8) && (img->samplesperpixel==3))
- {
- if (initYCbCrConversion(img)!=0)
- {
- /*
- * The 6.0 spec says that subsampling must be
- * one of 1, 2, or 4, and that vertical subsampling
- * must always be <= horizontal subsampling; so
- * there are only a few possibilities and we just
- * enumerate the cases.
- * Joris: added support for the [1,2] case, nonetheless, to accomodate
- * some OJPEG files
- */
- uint16 SubsamplingHor;
- uint16 SubsamplingVer;
- TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
- switch ((SubsamplingHor<<4)|SubsamplingVer) {
- case 0x44:
- img->put.contig = putcontig8bitYCbCr44tile;
- break;
- case 0x42:
- img->put.contig = putcontig8bitYCbCr42tile;
- break;
- case 0x41:
- img->put.contig = putcontig8bitYCbCr41tile;
- break;
- case 0x22:
- img->put.contig = putcontig8bitYCbCr22tile;
- break;
- case 0x21:
- img->put.contig = putcontig8bitYCbCr21tile;
- break;
- case 0x12:
- img->put.contig = putcontig8bitYCbCr12tile;
- break;
- case 0x11:
- img->put.contig = putcontig8bitYCbCr11tile;
- break;
- }
- }
- }
- break;
- case PHOTOMETRIC_CIELAB:
- if (buildMap(img)) {
- if (img->bitspersample == 8)
- img->put.contig = initCIELabConversion(img);
- break;
- }
- }
- return ((img->get!=NULL) && (img->put.contig!=NULL));
- }
- /*
- * Select the appropriate conversion routine for unpacked data.
- *
- * NB: we assume that unpacked single channel data is directed
- * to the "packed routines.
- */
- static int
- PickSeparateCase(TIFFRGBAImage* img)
- {
- img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
- img->put.separate = NULL;
- switch (img->photometric) {
- case PHOTOMETRIC_MINISWHITE:
- case PHOTOMETRIC_MINISBLACK:
- /* greyscale images processed pretty much as RGB by gtTileSeparate */
- case PHOTOMETRIC_RGB:
- switch (img->bitspersample) {
- case 8:
- if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
- img->put.separate = putRGBAAseparate8bittile;
- else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
- {
- if (BuildMapUaToAa(img))
- img->put.separate = putRGBUAseparate8bittile;
- }
- else
- img->put.separate = putRGBseparate8bittile;
- break;
- case 16:
- if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
- {
- if (BuildMapBitdepth16To8(img))
- img->put.separate = putRGBAAseparate16bittile;
- }
- else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
- {
- if (BuildMapBitdepth16To8(img) &&
- BuildMapUaToAa(img))
- img->put.separate = putRGBUAseparate16bittile;
- }
- else
- {
- if (BuildMapBitdepth16To8(img))
- img->put.separate = putRGBseparate16bittile;
- }
- break;
- }
- break;
- case PHOTOMETRIC_SEPARATED:
- if (img->bitspersample == 8 && img->samplesperpixel == 4)
- {
- img->alpha = 1; // Not alpha, but seems like the only way to get 4th band
- img->put.separate = putCMYKseparate8bittile;
- }
- break;
- case PHOTOMETRIC_YCBCR:
- if ((img->bitspersample==8) && (img->samplesperpixel==3))
- {
- if (initYCbCrConversion(img)!=0)
- {
- uint16 hs, vs;
- TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
- switch ((hs<<4)|vs) {
- case 0x11:
- img->put.separate = putseparate8bitYCbCr11tile;
- break;
- /* TODO: add other cases here */
- }
- }
- }
- break;
- }
- return ((img->get!=NULL) && (img->put.separate!=NULL));
- }
- static int
- BuildMapUaToAa(TIFFRGBAImage* img)
- {
- static const char module[]="BuildMapUaToAa";
- uint8* m;
- uint16 na,nv;
- assert(img->UaToAa==NULL);
- img->UaToAa=_TIFFmalloc(65536);
- if (img->UaToAa==NULL)
- {
- TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
- return(0);
- }
- m=img->UaToAa;
- for (na=0; na<256; na++)
- {
- for (nv=0; nv<256; nv++)
- *m++=(nv*na+127)/255;
- }
- return(1);
- }
- static int
- BuildMapBitdepth16To8(TIFFRGBAImage* img)
- {
- static const char module[]="BuildMapBitdepth16To8";
- uint8* m;
- uint32 n;
- assert(img->Bitdepth16To8==NULL);
- img->Bitdepth16To8=_TIFFmalloc(65536);
- if (img->Bitdepth16To8==NULL)
- {
- TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
- return(0);
- }
- m=img->Bitdepth16To8;
- for (n=0; n<65536; n++)
- *m++=(n+128)/257;
- return(1);
- }
- /*
- * Read a whole strip off data from the file, and convert to RGBA form.
- * If this is the last strip, then it will only contain the portion of
- * the strip that is actually within the image space. The result is
- * organized in bottom to top form.
- */
- int
- TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
- {
- char emsg[1024] = "";
- TIFFRGBAImage img;
- int ok;
- uint32 rowsperstrip, rows_to_read;
- if( TIFFIsTiled( tif ) )
- {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
- "Can't use TIFFReadRGBAStrip() with tiled file.");
- return (0);
- }
-
- TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
- if( (row % rowsperstrip) != 0 )
- {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
- "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
- return (0);
- }
- if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
- img.row_offset = row;
- img.col_offset = 0;
- if( row + rowsperstrip > img.height )
- rows_to_read = img.height - row;
- else
- rows_to_read = rowsperstrip;
-
- ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read );
-
- TIFFRGBAImageEnd(&img);
- } else {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
- ok = 0;
- }
-
- return (ok);
- }
- /*
- * Read a whole tile off data from the file, and convert to RGBA form.
- * The returned RGBA data is organized from bottom to top of tile,
- * and may include zeroed areas if the tile extends off the image.
- */
- int
- TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
- {
- char emsg[1024] = "";
- TIFFRGBAImage img;
- int ok;
- uint32 tile_xsize, tile_ysize;
- uint32 read_xsize, read_ysize;
- uint32 i_row;
- /*
- * Verify that our request is legal - on a tile file, and on a
- * tile boundary.
- */
-
- if( !TIFFIsTiled( tif ) )
- {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
- "Can't use TIFFReadRGBATile() with stripped file.");
- return (0);
- }
-
- TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
- TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
- if( (col % tile_xsize) != 0 || (row % tile_ysize) != 0 )
- {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
- "Row/col passed to TIFFReadRGBATile() must be top"
- "left corner of a tile.");
- return (0);
- }
- /*
- * Setup the RGBA reader.
- */
-
- if (!TIFFRGBAImageOK(tif, emsg)
- || !TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
- TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
- return( 0 );
- }
- /*
- * The TIFFRGBAImageGet() function doesn't allow us to get off the
- * edge of the image, even to fill an otherwise valid tile. So we
- * figure out how much we can read, and fix up the tile buffer to
- * a full tile configuration afterwards.
- */
- if( row + tile_ysize > img.height )
- read_ysize = img.height - row;
- else
- read_ysize = tile_ysize;
-
- if( col + tile_xsize > img.width )
- read_xsize = img.width - col;
- else
- read_xsize = tile_xsize;
- /*
- * Read the chunk of imagery.
- */
-
- img.row_offset = row;
- img.col_offset = col;
- ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize );
-
- TIFFRGBAImageEnd(&img);
- /*
- * If our read was incomplete we will need to fix up the tile by
- * shifting the data around as if a full tile of data is being returned.
- *
- * This is all the more complicated because the image is organized in
- * bottom to top format.
- */
- if( read_xsize == tile_xsize && read_ysize == tile_ysize )
- return( ok );
- for( i_row = 0; i_row < read_ysize; i_row++ ) {
- memmove( raster + (tile_ysize - i_row - 1) * tile_xsize,
- raster + (read_ysize - i_row - 1) * read_xsize,
- read_xsize * sizeof(uint32) );
- _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize+read_xsize,
- 0, sizeof(uint32) * (tile_xsize - read_xsize) );
- }
- for( i_row = read_ysize; i_row < tile_ysize; i_row++ ) {
- _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize,
- 0, sizeof(uint32) * tile_xsize );
- }
- return (ok);
- }
- /* vim: set ts=8 sts=8 sw=8 noet: */
- /*
- * Local Variables:
- * mode: c
- * c-basic-offset: 8
- * fill-column: 78
- * End:
- */
|