pdf.image_decoders.js 241 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. (function webpackUniversalModuleDefinition(root, factory) {
  23. if(typeof exports === 'object' && typeof module === 'object')
  24. module.exports = factory();
  25. else if(typeof define === 'function' && define.amd)
  26. define("pdfjs-dist/image_decoders/pdf.image_decoders", [], factory);
  27. else if(typeof exports === 'object')
  28. exports["pdfjs-dist/image_decoders/pdf.image_decoders"] = factory();
  29. else
  30. root["pdfjs-dist/image_decoders/pdf.image_decoders"] = root.pdfjsImageDecoders = factory();
  31. })(globalThis, () => {
  32. return /******/ (() => { // webpackBootstrap
  33. /******/ "use strict";
  34. /******/ var __webpack_modules__ = ([
  35. /* 0 */,
  36. /* 1 */
  37. /***/ ((__unused_webpack_module, exports) => {
  38. Object.defineProperty(exports, "__esModule", ({
  39. value: true
  40. }));
  41. exports.VerbosityLevel = exports.Util = exports.UnknownErrorException = exports.UnexpectedResponseException = exports.UNSUPPORTED_FEATURES = exports.TextRenderingMode = exports.StreamType = exports.RenderingIntentFlag = exports.PermissionFlag = exports.PasswordResponses = exports.PasswordException = exports.PageActionEventType = exports.OPS = exports.MissingPDFException = exports.LINE_FACTOR = exports.LINE_DESCENT_FACTOR = exports.InvalidPDFException = exports.ImageKind = exports.IDENTITY_MATRIX = exports.FormatError = exports.FontType = exports.FeatureTest = exports.FONT_IDENTITY_MATRIX = exports.DocumentActionEventType = exports.CMapCompressionType = exports.BaseException = exports.BASELINE_FACTOR = exports.AnnotationType = exports.AnnotationStateModelType = exports.AnnotationReviewState = exports.AnnotationReplyType = exports.AnnotationMode = exports.AnnotationMarkedState = exports.AnnotationFlag = exports.AnnotationFieldFlag = exports.AnnotationEditorType = exports.AnnotationEditorPrefix = exports.AnnotationEditorParamsType = exports.AnnotationBorderStyleType = exports.AnnotationActionEventType = exports.AbortException = void 0;
  42. exports.arrayByteLength = arrayByteLength;
  43. exports.arraysToBytes = arraysToBytes;
  44. exports.assert = assert;
  45. exports.bytesToString = bytesToString;
  46. exports.createPromiseCapability = createPromiseCapability;
  47. exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
  48. exports.getModificationDate = getModificationDate;
  49. exports.getVerbosityLevel = getVerbosityLevel;
  50. exports.info = info;
  51. exports.isArrayBuffer = isArrayBuffer;
  52. exports.isArrayEqual = isArrayEqual;
  53. exports.objectFromMap = objectFromMap;
  54. exports.objectSize = objectSize;
  55. exports.setVerbosityLevel = setVerbosityLevel;
  56. exports.shadow = shadow;
  57. exports.string32 = string32;
  58. exports.stringToBytes = stringToBytes;
  59. exports.stringToPDFString = stringToPDFString;
  60. exports.stringToUTF8String = stringToUTF8String;
  61. exports.unreachable = unreachable;
  62. exports.utf8StringToString = utf8StringToString;
  63. exports.warn = warn;
  64. ;
  65. const IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
  66. exports.IDENTITY_MATRIX = IDENTITY_MATRIX;
  67. const FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
  68. exports.FONT_IDENTITY_MATRIX = FONT_IDENTITY_MATRIX;
  69. const LINE_FACTOR = 1.35;
  70. exports.LINE_FACTOR = LINE_FACTOR;
  71. const LINE_DESCENT_FACTOR = 0.35;
  72. exports.LINE_DESCENT_FACTOR = LINE_DESCENT_FACTOR;
  73. const BASELINE_FACTOR = LINE_DESCENT_FACTOR / LINE_FACTOR;
  74. exports.BASELINE_FACTOR = BASELINE_FACTOR;
  75. const RenderingIntentFlag = {
  76. ANY: 0x01,
  77. DISPLAY: 0x02,
  78. PRINT: 0x04,
  79. SAVE: 0x08,
  80. ANNOTATIONS_FORMS: 0x10,
  81. ANNOTATIONS_STORAGE: 0x20,
  82. ANNOTATIONS_DISABLE: 0x40,
  83. OPLIST: 0x100
  84. };
  85. exports.RenderingIntentFlag = RenderingIntentFlag;
  86. const AnnotationMode = {
  87. DISABLE: 0,
  88. ENABLE: 1,
  89. ENABLE_FORMS: 2,
  90. ENABLE_STORAGE: 3
  91. };
  92. exports.AnnotationMode = AnnotationMode;
  93. const AnnotationEditorPrefix = "pdfjs_internal_editor_";
  94. exports.AnnotationEditorPrefix = AnnotationEditorPrefix;
  95. const AnnotationEditorType = {
  96. DISABLE: -1,
  97. NONE: 0,
  98. FREETEXT: 3,
  99. INK: 15
  100. };
  101. exports.AnnotationEditorType = AnnotationEditorType;
  102. const AnnotationEditorParamsType = {
  103. FREETEXT_SIZE: 1,
  104. FREETEXT_COLOR: 2,
  105. FREETEXT_OPACITY: 3,
  106. INK_COLOR: 11,
  107. INK_THICKNESS: 12,
  108. INK_OPACITY: 13
  109. };
  110. exports.AnnotationEditorParamsType = AnnotationEditorParamsType;
  111. const PermissionFlag = {
  112. PRINT: 0x04,
  113. MODIFY_CONTENTS: 0x08,
  114. COPY: 0x10,
  115. MODIFY_ANNOTATIONS: 0x20,
  116. FILL_INTERACTIVE_FORMS: 0x100,
  117. COPY_FOR_ACCESSIBILITY: 0x200,
  118. ASSEMBLE: 0x400,
  119. PRINT_HIGH_QUALITY: 0x800
  120. };
  121. exports.PermissionFlag = PermissionFlag;
  122. const TextRenderingMode = {
  123. FILL: 0,
  124. STROKE: 1,
  125. FILL_STROKE: 2,
  126. INVISIBLE: 3,
  127. FILL_ADD_TO_PATH: 4,
  128. STROKE_ADD_TO_PATH: 5,
  129. FILL_STROKE_ADD_TO_PATH: 6,
  130. ADD_TO_PATH: 7,
  131. FILL_STROKE_MASK: 3,
  132. ADD_TO_PATH_FLAG: 4
  133. };
  134. exports.TextRenderingMode = TextRenderingMode;
  135. const ImageKind = {
  136. GRAYSCALE_1BPP: 1,
  137. RGB_24BPP: 2,
  138. RGBA_32BPP: 3
  139. };
  140. exports.ImageKind = ImageKind;
  141. const AnnotationType = {
  142. TEXT: 1,
  143. LINK: 2,
  144. FREETEXT: 3,
  145. LINE: 4,
  146. SQUARE: 5,
  147. CIRCLE: 6,
  148. POLYGON: 7,
  149. POLYLINE: 8,
  150. HIGHLIGHT: 9,
  151. UNDERLINE: 10,
  152. SQUIGGLY: 11,
  153. STRIKEOUT: 12,
  154. STAMP: 13,
  155. CARET: 14,
  156. INK: 15,
  157. POPUP: 16,
  158. FILEATTACHMENT: 17,
  159. SOUND: 18,
  160. MOVIE: 19,
  161. WIDGET: 20,
  162. SCREEN: 21,
  163. PRINTERMARK: 22,
  164. TRAPNET: 23,
  165. WATERMARK: 24,
  166. THREED: 25,
  167. REDACT: 26
  168. };
  169. exports.AnnotationType = AnnotationType;
  170. const AnnotationStateModelType = {
  171. MARKED: "Marked",
  172. REVIEW: "Review"
  173. };
  174. exports.AnnotationStateModelType = AnnotationStateModelType;
  175. const AnnotationMarkedState = {
  176. MARKED: "Marked",
  177. UNMARKED: "Unmarked"
  178. };
  179. exports.AnnotationMarkedState = AnnotationMarkedState;
  180. const AnnotationReviewState = {
  181. ACCEPTED: "Accepted",
  182. REJECTED: "Rejected",
  183. CANCELLED: "Cancelled",
  184. COMPLETED: "Completed",
  185. NONE: "None"
  186. };
  187. exports.AnnotationReviewState = AnnotationReviewState;
  188. const AnnotationReplyType = {
  189. GROUP: "Group",
  190. REPLY: "R"
  191. };
  192. exports.AnnotationReplyType = AnnotationReplyType;
  193. const AnnotationFlag = {
  194. INVISIBLE: 0x01,
  195. HIDDEN: 0x02,
  196. PRINT: 0x04,
  197. NOZOOM: 0x08,
  198. NOROTATE: 0x10,
  199. NOVIEW: 0x20,
  200. READONLY: 0x40,
  201. LOCKED: 0x80,
  202. TOGGLENOVIEW: 0x100,
  203. LOCKEDCONTENTS: 0x200
  204. };
  205. exports.AnnotationFlag = AnnotationFlag;
  206. const AnnotationFieldFlag = {
  207. READONLY: 0x0000001,
  208. REQUIRED: 0x0000002,
  209. NOEXPORT: 0x0000004,
  210. MULTILINE: 0x0001000,
  211. PASSWORD: 0x0002000,
  212. NOTOGGLETOOFF: 0x0004000,
  213. RADIO: 0x0008000,
  214. PUSHBUTTON: 0x0010000,
  215. COMBO: 0x0020000,
  216. EDIT: 0x0040000,
  217. SORT: 0x0080000,
  218. FILESELECT: 0x0100000,
  219. MULTISELECT: 0x0200000,
  220. DONOTSPELLCHECK: 0x0400000,
  221. DONOTSCROLL: 0x0800000,
  222. COMB: 0x1000000,
  223. RICHTEXT: 0x2000000,
  224. RADIOSINUNISON: 0x2000000,
  225. COMMITONSELCHANGE: 0x4000000
  226. };
  227. exports.AnnotationFieldFlag = AnnotationFieldFlag;
  228. const AnnotationBorderStyleType = {
  229. SOLID: 1,
  230. DASHED: 2,
  231. BEVELED: 3,
  232. INSET: 4,
  233. UNDERLINE: 5
  234. };
  235. exports.AnnotationBorderStyleType = AnnotationBorderStyleType;
  236. const AnnotationActionEventType = {
  237. E: "Mouse Enter",
  238. X: "Mouse Exit",
  239. D: "Mouse Down",
  240. U: "Mouse Up",
  241. Fo: "Focus",
  242. Bl: "Blur",
  243. PO: "PageOpen",
  244. PC: "PageClose",
  245. PV: "PageVisible",
  246. PI: "PageInvisible",
  247. K: "Keystroke",
  248. F: "Format",
  249. V: "Validate",
  250. C: "Calculate"
  251. };
  252. exports.AnnotationActionEventType = AnnotationActionEventType;
  253. const DocumentActionEventType = {
  254. WC: "WillClose",
  255. WS: "WillSave",
  256. DS: "DidSave",
  257. WP: "WillPrint",
  258. DP: "DidPrint"
  259. };
  260. exports.DocumentActionEventType = DocumentActionEventType;
  261. const PageActionEventType = {
  262. O: "PageOpen",
  263. C: "PageClose"
  264. };
  265. exports.PageActionEventType = PageActionEventType;
  266. const StreamType = {
  267. UNKNOWN: "UNKNOWN",
  268. FLATE: "FLATE",
  269. LZW: "LZW",
  270. DCT: "DCT",
  271. JPX: "JPX",
  272. JBIG: "JBIG",
  273. A85: "A85",
  274. AHX: "AHX",
  275. CCF: "CCF",
  276. RLX: "RLX"
  277. };
  278. exports.StreamType = StreamType;
  279. const FontType = {
  280. UNKNOWN: "UNKNOWN",
  281. TYPE1: "TYPE1",
  282. TYPE1STANDARD: "TYPE1STANDARD",
  283. TYPE1C: "TYPE1C",
  284. CIDFONTTYPE0: "CIDFONTTYPE0",
  285. CIDFONTTYPE0C: "CIDFONTTYPE0C",
  286. TRUETYPE: "TRUETYPE",
  287. CIDFONTTYPE2: "CIDFONTTYPE2",
  288. TYPE3: "TYPE3",
  289. OPENTYPE: "OPENTYPE",
  290. TYPE0: "TYPE0",
  291. MMTYPE1: "MMTYPE1"
  292. };
  293. exports.FontType = FontType;
  294. const VerbosityLevel = {
  295. ERRORS: 0,
  296. WARNINGS: 1,
  297. INFOS: 5
  298. };
  299. exports.VerbosityLevel = VerbosityLevel;
  300. const CMapCompressionType = {
  301. NONE: 0,
  302. BINARY: 1
  303. };
  304. exports.CMapCompressionType = CMapCompressionType;
  305. const OPS = {
  306. dependency: 1,
  307. setLineWidth: 2,
  308. setLineCap: 3,
  309. setLineJoin: 4,
  310. setMiterLimit: 5,
  311. setDash: 6,
  312. setRenderingIntent: 7,
  313. setFlatness: 8,
  314. setGState: 9,
  315. save: 10,
  316. restore: 11,
  317. transform: 12,
  318. moveTo: 13,
  319. lineTo: 14,
  320. curveTo: 15,
  321. curveTo2: 16,
  322. curveTo3: 17,
  323. closePath: 18,
  324. rectangle: 19,
  325. stroke: 20,
  326. closeStroke: 21,
  327. fill: 22,
  328. eoFill: 23,
  329. fillStroke: 24,
  330. eoFillStroke: 25,
  331. closeFillStroke: 26,
  332. closeEOFillStroke: 27,
  333. endPath: 28,
  334. clip: 29,
  335. eoClip: 30,
  336. beginText: 31,
  337. endText: 32,
  338. setCharSpacing: 33,
  339. setWordSpacing: 34,
  340. setHScale: 35,
  341. setLeading: 36,
  342. setFont: 37,
  343. setTextRenderingMode: 38,
  344. setTextRise: 39,
  345. moveText: 40,
  346. setLeadingMoveText: 41,
  347. setTextMatrix: 42,
  348. nextLine: 43,
  349. showText: 44,
  350. showSpacedText: 45,
  351. nextLineShowText: 46,
  352. nextLineSetSpacingShowText: 47,
  353. setCharWidth: 48,
  354. setCharWidthAndBounds: 49,
  355. setStrokeColorSpace: 50,
  356. setFillColorSpace: 51,
  357. setStrokeColor: 52,
  358. setStrokeColorN: 53,
  359. setFillColor: 54,
  360. setFillColorN: 55,
  361. setStrokeGray: 56,
  362. setFillGray: 57,
  363. setStrokeRGBColor: 58,
  364. setFillRGBColor: 59,
  365. setStrokeCMYKColor: 60,
  366. setFillCMYKColor: 61,
  367. shadingFill: 62,
  368. beginInlineImage: 63,
  369. beginImageData: 64,
  370. endInlineImage: 65,
  371. paintXObject: 66,
  372. markPoint: 67,
  373. markPointProps: 68,
  374. beginMarkedContent: 69,
  375. beginMarkedContentProps: 70,
  376. endMarkedContent: 71,
  377. beginCompat: 72,
  378. endCompat: 73,
  379. paintFormXObjectBegin: 74,
  380. paintFormXObjectEnd: 75,
  381. beginGroup: 76,
  382. endGroup: 77,
  383. beginAnnotation: 80,
  384. endAnnotation: 81,
  385. paintImageMaskXObject: 83,
  386. paintImageMaskXObjectGroup: 84,
  387. paintImageXObject: 85,
  388. paintInlineImageXObject: 86,
  389. paintInlineImageXObjectGroup: 87,
  390. paintImageXObjectRepeat: 88,
  391. paintImageMaskXObjectRepeat: 89,
  392. paintSolidColorImageMask: 90,
  393. constructPath: 91
  394. };
  395. exports.OPS = OPS;
  396. const UNSUPPORTED_FEATURES = {
  397. forms: "forms",
  398. javaScript: "javaScript",
  399. signatures: "signatures",
  400. smask: "smask",
  401. shadingPattern: "shadingPattern",
  402. errorTilingPattern: "errorTilingPattern",
  403. errorExtGState: "errorExtGState",
  404. errorXObject: "errorXObject",
  405. errorFontLoadType3: "errorFontLoadType3",
  406. errorFontState: "errorFontState",
  407. errorFontMissing: "errorFontMissing",
  408. errorFontTranslate: "errorFontTranslate",
  409. errorColorSpace: "errorColorSpace",
  410. errorOperatorList: "errorOperatorList",
  411. errorFontToUnicode: "errorFontToUnicode",
  412. errorFontLoadNative: "errorFontLoadNative",
  413. errorFontBuildPath: "errorFontBuildPath",
  414. errorFontGetPath: "errorFontGetPath",
  415. errorMarkedContent: "errorMarkedContent",
  416. errorContentSubStream: "errorContentSubStream"
  417. };
  418. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  419. const PasswordResponses = {
  420. NEED_PASSWORD: 1,
  421. INCORRECT_PASSWORD: 2
  422. };
  423. exports.PasswordResponses = PasswordResponses;
  424. let verbosity = VerbosityLevel.WARNINGS;
  425. function setVerbosityLevel(level) {
  426. if (Number.isInteger(level)) {
  427. verbosity = level;
  428. }
  429. }
  430. function getVerbosityLevel() {
  431. return verbosity;
  432. }
  433. function info(msg) {
  434. if (verbosity >= VerbosityLevel.INFOS) {
  435. console.log(`Info: ${msg}`);
  436. }
  437. }
  438. function warn(msg) {
  439. if (verbosity >= VerbosityLevel.WARNINGS) {
  440. console.log(`Warning: ${msg}`);
  441. }
  442. }
  443. function unreachable(msg) {
  444. throw new Error(msg);
  445. }
  446. function assert(cond, msg) {
  447. if (!cond) {
  448. unreachable(msg);
  449. }
  450. }
  451. function _isValidProtocol(url) {
  452. if (!url) {
  453. return false;
  454. }
  455. switch (url.protocol) {
  456. case "http:":
  457. case "https:":
  458. case "ftp:":
  459. case "mailto:":
  460. case "tel:":
  461. return true;
  462. default:
  463. return false;
  464. }
  465. }
  466. function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
  467. if (!url) {
  468. return null;
  469. }
  470. try {
  471. if (options && typeof url === "string") {
  472. if (options.addDefaultProtocol && url.startsWith("www.")) {
  473. const dots = url.match(/\./g);
  474. if (dots && dots.length >= 2) {
  475. url = `http://${url}`;
  476. }
  477. }
  478. if (options.tryConvertEncoding) {
  479. try {
  480. url = stringToUTF8String(url);
  481. } catch (ex) {}
  482. }
  483. }
  484. const absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
  485. if (_isValidProtocol(absoluteUrl)) {
  486. return absoluteUrl;
  487. }
  488. } catch (ex) {}
  489. return null;
  490. }
  491. function shadow(obj, prop, value, nonSerializable = false) {
  492. Object.defineProperty(obj, prop, {
  493. value,
  494. enumerable: !nonSerializable,
  495. configurable: true,
  496. writable: false
  497. });
  498. return value;
  499. }
  500. const BaseException = function BaseExceptionClosure() {
  501. function BaseException(message, name) {
  502. if (this.constructor === BaseException) {
  503. unreachable("Cannot initialize BaseException.");
  504. }
  505. this.message = message;
  506. this.name = name;
  507. }
  508. BaseException.prototype = new Error();
  509. BaseException.constructor = BaseException;
  510. return BaseException;
  511. }();
  512. exports.BaseException = BaseException;
  513. class PasswordException extends BaseException {
  514. constructor(msg, code) {
  515. super(msg, "PasswordException");
  516. this.code = code;
  517. }
  518. }
  519. exports.PasswordException = PasswordException;
  520. class UnknownErrorException extends BaseException {
  521. constructor(msg, details) {
  522. super(msg, "UnknownErrorException");
  523. this.details = details;
  524. }
  525. }
  526. exports.UnknownErrorException = UnknownErrorException;
  527. class InvalidPDFException extends BaseException {
  528. constructor(msg) {
  529. super(msg, "InvalidPDFException");
  530. }
  531. }
  532. exports.InvalidPDFException = InvalidPDFException;
  533. class MissingPDFException extends BaseException {
  534. constructor(msg) {
  535. super(msg, "MissingPDFException");
  536. }
  537. }
  538. exports.MissingPDFException = MissingPDFException;
  539. class UnexpectedResponseException extends BaseException {
  540. constructor(msg, status) {
  541. super(msg, "UnexpectedResponseException");
  542. this.status = status;
  543. }
  544. }
  545. exports.UnexpectedResponseException = UnexpectedResponseException;
  546. class FormatError extends BaseException {
  547. constructor(msg) {
  548. super(msg, "FormatError");
  549. }
  550. }
  551. exports.FormatError = FormatError;
  552. class AbortException extends BaseException {
  553. constructor(msg) {
  554. super(msg, "AbortException");
  555. }
  556. }
  557. exports.AbortException = AbortException;
  558. function bytesToString(bytes) {
  559. if (typeof bytes !== "object" || bytes === null || bytes.length === undefined) {
  560. unreachable("Invalid argument for bytesToString");
  561. }
  562. const length = bytes.length;
  563. const MAX_ARGUMENT_COUNT = 8192;
  564. if (length < MAX_ARGUMENT_COUNT) {
  565. return String.fromCharCode.apply(null, bytes);
  566. }
  567. const strBuf = [];
  568. for (let i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  569. const chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  570. const chunk = bytes.subarray(i, chunkEnd);
  571. strBuf.push(String.fromCharCode.apply(null, chunk));
  572. }
  573. return strBuf.join("");
  574. }
  575. function stringToBytes(str) {
  576. if (typeof str !== "string") {
  577. unreachable("Invalid argument for stringToBytes");
  578. }
  579. const length = str.length;
  580. const bytes = new Uint8Array(length);
  581. for (let i = 0; i < length; ++i) {
  582. bytes[i] = str.charCodeAt(i) & 0xff;
  583. }
  584. return bytes;
  585. }
  586. function arrayByteLength(arr) {
  587. if (arr.length !== undefined) {
  588. return arr.length;
  589. }
  590. if (arr.byteLength !== undefined) {
  591. return arr.byteLength;
  592. }
  593. unreachable("Invalid argument for arrayByteLength");
  594. }
  595. function arraysToBytes(arr) {
  596. const length = arr.length;
  597. if (length === 1 && arr[0] instanceof Uint8Array) {
  598. return arr[0];
  599. }
  600. let resultLength = 0;
  601. for (let i = 0; i < length; i++) {
  602. resultLength += arrayByteLength(arr[i]);
  603. }
  604. let pos = 0;
  605. const data = new Uint8Array(resultLength);
  606. for (let i = 0; i < length; i++) {
  607. let item = arr[i];
  608. if (!(item instanceof Uint8Array)) {
  609. if (typeof item === "string") {
  610. item = stringToBytes(item);
  611. } else {
  612. item = new Uint8Array(item);
  613. }
  614. }
  615. const itemLength = item.byteLength;
  616. data.set(item, pos);
  617. pos += itemLength;
  618. }
  619. return data;
  620. }
  621. function string32(value) {
  622. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  623. }
  624. function objectSize(obj) {
  625. return Object.keys(obj).length;
  626. }
  627. function objectFromMap(map) {
  628. const obj = Object.create(null);
  629. for (const [key, value] of map) {
  630. obj[key] = value;
  631. }
  632. return obj;
  633. }
  634. function isLittleEndian() {
  635. const buffer8 = new Uint8Array(4);
  636. buffer8[0] = 1;
  637. const view32 = new Uint32Array(buffer8.buffer, 0, 1);
  638. return view32[0] === 1;
  639. }
  640. function isEvalSupported() {
  641. try {
  642. new Function("");
  643. return true;
  644. } catch (e) {
  645. return false;
  646. }
  647. }
  648. class FeatureTest {
  649. static get isLittleEndian() {
  650. return shadow(this, "isLittleEndian", isLittleEndian());
  651. }
  652. static get isEvalSupported() {
  653. return shadow(this, "isEvalSupported", isEvalSupported());
  654. }
  655. static get isOffscreenCanvasSupported() {
  656. return shadow(this, "isOffscreenCanvasSupported", typeof OffscreenCanvas !== "undefined");
  657. }
  658. }
  659. exports.FeatureTest = FeatureTest;
  660. const hexNumbers = [...Array(256).keys()].map(n => n.toString(16).padStart(2, "0"));
  661. class Util {
  662. static makeHexColor(r, g, b) {
  663. return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`;
  664. }
  665. static scaleMinMax(transform, minMax) {
  666. let temp;
  667. if (transform[0]) {
  668. if (transform[0] < 0) {
  669. temp = minMax[0];
  670. minMax[0] = minMax[1];
  671. minMax[1] = temp;
  672. }
  673. minMax[0] *= transform[0];
  674. minMax[1] *= transform[0];
  675. if (transform[3] < 0) {
  676. temp = minMax[2];
  677. minMax[2] = minMax[3];
  678. minMax[3] = temp;
  679. }
  680. minMax[2] *= transform[3];
  681. minMax[3] *= transform[3];
  682. } else {
  683. temp = minMax[0];
  684. minMax[0] = minMax[2];
  685. minMax[2] = temp;
  686. temp = minMax[1];
  687. minMax[1] = minMax[3];
  688. minMax[3] = temp;
  689. if (transform[1] < 0) {
  690. temp = minMax[2];
  691. minMax[2] = minMax[3];
  692. minMax[3] = temp;
  693. }
  694. minMax[2] *= transform[1];
  695. minMax[3] *= transform[1];
  696. if (transform[2] < 0) {
  697. temp = minMax[0];
  698. minMax[0] = minMax[1];
  699. minMax[1] = temp;
  700. }
  701. minMax[0] *= transform[2];
  702. minMax[1] *= transform[2];
  703. }
  704. minMax[0] += transform[4];
  705. minMax[1] += transform[4];
  706. minMax[2] += transform[5];
  707. minMax[3] += transform[5];
  708. }
  709. static transform(m1, m2) {
  710. return [m1[0] * m2[0] + m1[2] * m2[1], m1[1] * m2[0] + m1[3] * m2[1], m1[0] * m2[2] + m1[2] * m2[3], m1[1] * m2[2] + m1[3] * m2[3], m1[0] * m2[4] + m1[2] * m2[5] + m1[4], m1[1] * m2[4] + m1[3] * m2[5] + m1[5]];
  711. }
  712. static applyTransform(p, m) {
  713. const xt = p[0] * m[0] + p[1] * m[2] + m[4];
  714. const yt = p[0] * m[1] + p[1] * m[3] + m[5];
  715. return [xt, yt];
  716. }
  717. static applyInverseTransform(p, m) {
  718. const d = m[0] * m[3] - m[1] * m[2];
  719. const xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  720. const yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  721. return [xt, yt];
  722. }
  723. static getAxialAlignedBoundingBox(r, m) {
  724. const p1 = Util.applyTransform(r, m);
  725. const p2 = Util.applyTransform(r.slice(2, 4), m);
  726. const p3 = Util.applyTransform([r[0], r[3]], m);
  727. const p4 = Util.applyTransform([r[2], r[1]], m);
  728. return [Math.min(p1[0], p2[0], p3[0], p4[0]), Math.min(p1[1], p2[1], p3[1], p4[1]), Math.max(p1[0], p2[0], p3[0], p4[0]), Math.max(p1[1], p2[1], p3[1], p4[1])];
  729. }
  730. static inverseTransform(m) {
  731. const d = m[0] * m[3] - m[1] * m[2];
  732. return [m[3] / d, -m[1] / d, -m[2] / d, m[0] / d, (m[2] * m[5] - m[4] * m[3]) / d, (m[4] * m[1] - m[5] * m[0]) / d];
  733. }
  734. static singularValueDecompose2dScale(m) {
  735. const transpose = [m[0], m[2], m[1], m[3]];
  736. const a = m[0] * transpose[0] + m[1] * transpose[2];
  737. const b = m[0] * transpose[1] + m[1] * transpose[3];
  738. const c = m[2] * transpose[0] + m[3] * transpose[2];
  739. const d = m[2] * transpose[1] + m[3] * transpose[3];
  740. const first = (a + d) / 2;
  741. const second = Math.sqrt((a + d) ** 2 - 4 * (a * d - c * b)) / 2;
  742. const sx = first + second || 1;
  743. const sy = first - second || 1;
  744. return [Math.sqrt(sx), Math.sqrt(sy)];
  745. }
  746. static normalizeRect(rect) {
  747. const r = rect.slice(0);
  748. if (rect[0] > rect[2]) {
  749. r[0] = rect[2];
  750. r[2] = rect[0];
  751. }
  752. if (rect[1] > rect[3]) {
  753. r[1] = rect[3];
  754. r[3] = rect[1];
  755. }
  756. return r;
  757. }
  758. static intersect(rect1, rect2) {
  759. const xLow = Math.max(Math.min(rect1[0], rect1[2]), Math.min(rect2[0], rect2[2]));
  760. const xHigh = Math.min(Math.max(rect1[0], rect1[2]), Math.max(rect2[0], rect2[2]));
  761. if (xLow > xHigh) {
  762. return null;
  763. }
  764. const yLow = Math.max(Math.min(rect1[1], rect1[3]), Math.min(rect2[1], rect2[3]));
  765. const yHigh = Math.min(Math.max(rect1[1], rect1[3]), Math.max(rect2[1], rect2[3]));
  766. if (yLow > yHigh) {
  767. return null;
  768. }
  769. return [xLow, yLow, xHigh, yHigh];
  770. }
  771. static bezierBoundingBox(x0, y0, x1, y1, x2, y2, x3, y3) {
  772. const tvalues = [],
  773. bounds = [[], []];
  774. let a, b, c, t, t1, t2, b2ac, sqrtb2ac;
  775. for (let i = 0; i < 2; ++i) {
  776. if (i === 0) {
  777. b = 6 * x0 - 12 * x1 + 6 * x2;
  778. a = -3 * x0 + 9 * x1 - 9 * x2 + 3 * x3;
  779. c = 3 * x1 - 3 * x0;
  780. } else {
  781. b = 6 * y0 - 12 * y1 + 6 * y2;
  782. a = -3 * y0 + 9 * y1 - 9 * y2 + 3 * y3;
  783. c = 3 * y1 - 3 * y0;
  784. }
  785. if (Math.abs(a) < 1e-12) {
  786. if (Math.abs(b) < 1e-12) {
  787. continue;
  788. }
  789. t = -c / b;
  790. if (0 < t && t < 1) {
  791. tvalues.push(t);
  792. }
  793. continue;
  794. }
  795. b2ac = b * b - 4 * c * a;
  796. sqrtb2ac = Math.sqrt(b2ac);
  797. if (b2ac < 0) {
  798. continue;
  799. }
  800. t1 = (-b + sqrtb2ac) / (2 * a);
  801. if (0 < t1 && t1 < 1) {
  802. tvalues.push(t1);
  803. }
  804. t2 = (-b - sqrtb2ac) / (2 * a);
  805. if (0 < t2 && t2 < 1) {
  806. tvalues.push(t2);
  807. }
  808. }
  809. let j = tvalues.length,
  810. mt;
  811. const jlen = j;
  812. while (j--) {
  813. t = tvalues[j];
  814. mt = 1 - t;
  815. bounds[0][j] = mt * mt * mt * x0 + 3 * mt * mt * t * x1 + 3 * mt * t * t * x2 + t * t * t * x3;
  816. bounds[1][j] = mt * mt * mt * y0 + 3 * mt * mt * t * y1 + 3 * mt * t * t * y2 + t * t * t * y3;
  817. }
  818. bounds[0][jlen] = x0;
  819. bounds[1][jlen] = y0;
  820. bounds[0][jlen + 1] = x3;
  821. bounds[1][jlen + 1] = y3;
  822. bounds[0].length = bounds[1].length = jlen + 2;
  823. return [Math.min(...bounds[0]), Math.min(...bounds[1]), Math.max(...bounds[0]), Math.max(...bounds[1])];
  824. }
  825. }
  826. exports.Util = Util;
  827. const PDFStringTranslateTable = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2d8, 0x2c7, 0x2c6, 0x2d9, 0x2dd, 0x2db, 0x2da, 0x2dc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x192, 0x2044, 0x2039, 0x203a, 0x2212, 0x2030, 0x201e, 0x201c, 0x201d, 0x2018, 0x2019, 0x201a, 0x2122, 0xfb01, 0xfb02, 0x141, 0x152, 0x160, 0x178, 0x17d, 0x131, 0x142, 0x153, 0x161, 0x17e, 0, 0x20ac];
  828. function stringToPDFString(str) {
  829. if (str[0] >= "\xEF") {
  830. let encoding;
  831. if (str[0] === "\xFE" && str[1] === "\xFF") {
  832. encoding = "utf-16be";
  833. } else if (str[0] === "\xFF" && str[1] === "\xFE") {
  834. encoding = "utf-16le";
  835. } else if (str[0] === "\xEF" && str[1] === "\xBB" && str[2] === "\xBF") {
  836. encoding = "utf-8";
  837. }
  838. if (encoding) {
  839. try {
  840. const decoder = new TextDecoder(encoding, {
  841. fatal: true
  842. });
  843. const buffer = stringToBytes(str);
  844. return decoder.decode(buffer);
  845. } catch (ex) {
  846. warn(`stringToPDFString: "${ex}".`);
  847. }
  848. }
  849. }
  850. const strBuf = [];
  851. for (let i = 0, ii = str.length; i < ii; i++) {
  852. const code = PDFStringTranslateTable[str.charCodeAt(i)];
  853. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  854. }
  855. return strBuf.join("");
  856. }
  857. function stringToUTF8String(str) {
  858. return decodeURIComponent(escape(str));
  859. }
  860. function utf8StringToString(str) {
  861. return unescape(encodeURIComponent(str));
  862. }
  863. function isArrayBuffer(v) {
  864. return typeof v === "object" && v !== null && v.byteLength !== undefined;
  865. }
  866. function isArrayEqual(arr1, arr2) {
  867. if (arr1.length !== arr2.length) {
  868. return false;
  869. }
  870. for (let i = 0, ii = arr1.length; i < ii; i++) {
  871. if (arr1[i] !== arr2[i]) {
  872. return false;
  873. }
  874. }
  875. return true;
  876. }
  877. function getModificationDate(date = new Date()) {
  878. const buffer = [date.getUTCFullYear().toString(), (date.getUTCMonth() + 1).toString().padStart(2, "0"), date.getUTCDate().toString().padStart(2, "0"), date.getUTCHours().toString().padStart(2, "0"), date.getUTCMinutes().toString().padStart(2, "0"), date.getUTCSeconds().toString().padStart(2, "0")];
  879. return buffer.join("");
  880. }
  881. function createPromiseCapability() {
  882. const capability = Object.create(null);
  883. let isSettled = false;
  884. Object.defineProperty(capability, "settled", {
  885. get() {
  886. return isSettled;
  887. }
  888. });
  889. capability.promise = new Promise(function (resolve, reject) {
  890. capability.resolve = function (data) {
  891. isSettled = true;
  892. resolve(data);
  893. };
  894. capability.reject = function (reason) {
  895. isSettled = true;
  896. reject(reason);
  897. };
  898. });
  899. return capability;
  900. }
  901. /***/ }),
  902. /* 2 */
  903. /***/ ((__unused_webpack_module, exports, __w_pdfjs_require__) => {
  904. Object.defineProperty(exports, "__esModule", ({
  905. value: true
  906. }));
  907. exports.Jbig2Image = void 0;
  908. var _util = __w_pdfjs_require__(1);
  909. var _core_utils = __w_pdfjs_require__(3);
  910. var _arithmetic_decoder = __w_pdfjs_require__(6);
  911. var _ccitt = __w_pdfjs_require__(7);
  912. class Jbig2Error extends _util.BaseException {
  913. constructor(msg) {
  914. super(`JBIG2 error: ${msg}`, "Jbig2Error");
  915. }
  916. }
  917. class ContextCache {
  918. getContexts(id) {
  919. if (id in this) {
  920. return this[id];
  921. }
  922. return this[id] = new Int8Array(1 << 16);
  923. }
  924. }
  925. class DecodingContext {
  926. constructor(data, start, end) {
  927. this.data = data;
  928. this.start = start;
  929. this.end = end;
  930. }
  931. get decoder() {
  932. const decoder = new _arithmetic_decoder.ArithmeticDecoder(this.data, this.start, this.end);
  933. return (0, _util.shadow)(this, "decoder", decoder);
  934. }
  935. get contextCache() {
  936. const cache = new ContextCache();
  937. return (0, _util.shadow)(this, "contextCache", cache);
  938. }
  939. }
  940. function decodeInteger(contextCache, procedure, decoder) {
  941. const contexts = contextCache.getContexts(procedure);
  942. let prev = 1;
  943. function readBits(length) {
  944. let v = 0;
  945. for (let i = 0; i < length; i++) {
  946. const bit = decoder.readBit(contexts, prev);
  947. prev = prev < 256 ? prev << 1 | bit : (prev << 1 | bit) & 511 | 256;
  948. v = v << 1 | bit;
  949. }
  950. return v >>> 0;
  951. }
  952. const sign = readBits(1);
  953. const value = readBits(1) ? readBits(1) ? readBits(1) ? readBits(1) ? readBits(1) ? readBits(32) + 4436 : readBits(12) + 340 : readBits(8) + 84 : readBits(6) + 20 : readBits(4) + 4 : readBits(2);
  954. if (sign === 0) {
  955. return value;
  956. } else if (value > 0) {
  957. return -value;
  958. }
  959. return null;
  960. }
  961. function decodeIAID(contextCache, decoder, codeLength) {
  962. const contexts = contextCache.getContexts("IAID");
  963. let prev = 1;
  964. for (let i = 0; i < codeLength; i++) {
  965. const bit = decoder.readBit(contexts, prev);
  966. prev = prev << 1 | bit;
  967. }
  968. if (codeLength < 31) {
  969. return prev & (1 << codeLength) - 1;
  970. }
  971. return prev & 0x7fffffff;
  972. }
  973. const SegmentTypes = ["SymbolDictionary", null, null, null, "IntermediateTextRegion", null, "ImmediateTextRegion", "ImmediateLosslessTextRegion", null, null, null, null, null, null, null, null, "PatternDictionary", null, null, null, "IntermediateHalftoneRegion", null, "ImmediateHalftoneRegion", "ImmediateLosslessHalftoneRegion", null, null, null, null, null, null, null, null, null, null, null, null, "IntermediateGenericRegion", null, "ImmediateGenericRegion", "ImmediateLosslessGenericRegion", "IntermediateGenericRefinementRegion", null, "ImmediateGenericRefinementRegion", "ImmediateLosslessGenericRefinementRegion", null, null, null, null, "PageInformation", "EndOfPage", "EndOfStripe", "EndOfFile", "Profiles", "Tables", null, null, null, null, null, null, null, null, "Extension"];
  974. const CodingTemplates = [[{
  975. x: -1,
  976. y: -2
  977. }, {
  978. x: 0,
  979. y: -2
  980. }, {
  981. x: 1,
  982. y: -2
  983. }, {
  984. x: -2,
  985. y: -1
  986. }, {
  987. x: -1,
  988. y: -1
  989. }, {
  990. x: 0,
  991. y: -1
  992. }, {
  993. x: 1,
  994. y: -1
  995. }, {
  996. x: 2,
  997. y: -1
  998. }, {
  999. x: -4,
  1000. y: 0
  1001. }, {
  1002. x: -3,
  1003. y: 0
  1004. }, {
  1005. x: -2,
  1006. y: 0
  1007. }, {
  1008. x: -1,
  1009. y: 0
  1010. }], [{
  1011. x: -1,
  1012. y: -2
  1013. }, {
  1014. x: 0,
  1015. y: -2
  1016. }, {
  1017. x: 1,
  1018. y: -2
  1019. }, {
  1020. x: 2,
  1021. y: -2
  1022. }, {
  1023. x: -2,
  1024. y: -1
  1025. }, {
  1026. x: -1,
  1027. y: -1
  1028. }, {
  1029. x: 0,
  1030. y: -1
  1031. }, {
  1032. x: 1,
  1033. y: -1
  1034. }, {
  1035. x: 2,
  1036. y: -1
  1037. }, {
  1038. x: -3,
  1039. y: 0
  1040. }, {
  1041. x: -2,
  1042. y: 0
  1043. }, {
  1044. x: -1,
  1045. y: 0
  1046. }], [{
  1047. x: -1,
  1048. y: -2
  1049. }, {
  1050. x: 0,
  1051. y: -2
  1052. }, {
  1053. x: 1,
  1054. y: -2
  1055. }, {
  1056. x: -2,
  1057. y: -1
  1058. }, {
  1059. x: -1,
  1060. y: -1
  1061. }, {
  1062. x: 0,
  1063. y: -1
  1064. }, {
  1065. x: 1,
  1066. y: -1
  1067. }, {
  1068. x: -2,
  1069. y: 0
  1070. }, {
  1071. x: -1,
  1072. y: 0
  1073. }], [{
  1074. x: -3,
  1075. y: -1
  1076. }, {
  1077. x: -2,
  1078. y: -1
  1079. }, {
  1080. x: -1,
  1081. y: -1
  1082. }, {
  1083. x: 0,
  1084. y: -1
  1085. }, {
  1086. x: 1,
  1087. y: -1
  1088. }, {
  1089. x: -4,
  1090. y: 0
  1091. }, {
  1092. x: -3,
  1093. y: 0
  1094. }, {
  1095. x: -2,
  1096. y: 0
  1097. }, {
  1098. x: -1,
  1099. y: 0
  1100. }]];
  1101. const RefinementTemplates = [{
  1102. coding: [{
  1103. x: 0,
  1104. y: -1
  1105. }, {
  1106. x: 1,
  1107. y: -1
  1108. }, {
  1109. x: -1,
  1110. y: 0
  1111. }],
  1112. reference: [{
  1113. x: 0,
  1114. y: -1
  1115. }, {
  1116. x: 1,
  1117. y: -1
  1118. }, {
  1119. x: -1,
  1120. y: 0
  1121. }, {
  1122. x: 0,
  1123. y: 0
  1124. }, {
  1125. x: 1,
  1126. y: 0
  1127. }, {
  1128. x: -1,
  1129. y: 1
  1130. }, {
  1131. x: 0,
  1132. y: 1
  1133. }, {
  1134. x: 1,
  1135. y: 1
  1136. }]
  1137. }, {
  1138. coding: [{
  1139. x: -1,
  1140. y: -1
  1141. }, {
  1142. x: 0,
  1143. y: -1
  1144. }, {
  1145. x: 1,
  1146. y: -1
  1147. }, {
  1148. x: -1,
  1149. y: 0
  1150. }],
  1151. reference: [{
  1152. x: 0,
  1153. y: -1
  1154. }, {
  1155. x: -1,
  1156. y: 0
  1157. }, {
  1158. x: 0,
  1159. y: 0
  1160. }, {
  1161. x: 1,
  1162. y: 0
  1163. }, {
  1164. x: 0,
  1165. y: 1
  1166. }, {
  1167. x: 1,
  1168. y: 1
  1169. }]
  1170. }];
  1171. const ReusedContexts = [0x9b25, 0x0795, 0x00e5, 0x0195];
  1172. const RefinementReusedContexts = [0x0020, 0x0008];
  1173. function decodeBitmapTemplate0(width, height, decodingContext) {
  1174. const decoder = decodingContext.decoder;
  1175. const contexts = decodingContext.contextCache.getContexts("GB");
  1176. const bitmap = [];
  1177. let contextLabel, i, j, pixel, row, row1, row2;
  1178. const OLD_PIXEL_MASK = 0x7bf7;
  1179. for (i = 0; i < height; i++) {
  1180. row = bitmap[i] = new Uint8Array(width);
  1181. row1 = i < 1 ? row : bitmap[i - 1];
  1182. row2 = i < 2 ? row : bitmap[i - 2];
  1183. contextLabel = row2[0] << 13 | row2[1] << 12 | row2[2] << 11 | row1[0] << 7 | row1[1] << 6 | row1[2] << 5 | row1[3] << 4;
  1184. for (j = 0; j < width; j++) {
  1185. row[j] = pixel = decoder.readBit(contexts, contextLabel);
  1186. contextLabel = (contextLabel & OLD_PIXEL_MASK) << 1 | (j + 3 < width ? row2[j + 3] << 11 : 0) | (j + 4 < width ? row1[j + 4] << 4 : 0) | pixel;
  1187. }
  1188. }
  1189. return bitmap;
  1190. }
  1191. function decodeBitmap(mmr, width, height, templateIndex, prediction, skip, at, decodingContext) {
  1192. if (mmr) {
  1193. const input = new Reader(decodingContext.data, decodingContext.start, decodingContext.end);
  1194. return decodeMMRBitmap(input, width, height, false);
  1195. }
  1196. if (templateIndex === 0 && !skip && !prediction && at.length === 4 && at[0].x === 3 && at[0].y === -1 && at[1].x === -3 && at[1].y === -1 && at[2].x === 2 && at[2].y === -2 && at[3].x === -2 && at[3].y === -2) {
  1197. return decodeBitmapTemplate0(width, height, decodingContext);
  1198. }
  1199. const useskip = !!skip;
  1200. const template = CodingTemplates[templateIndex].concat(at);
  1201. template.sort(function (a, b) {
  1202. return a.y - b.y || a.x - b.x;
  1203. });
  1204. const templateLength = template.length;
  1205. const templateX = new Int8Array(templateLength);
  1206. const templateY = new Int8Array(templateLength);
  1207. const changingTemplateEntries = [];
  1208. let reuseMask = 0,
  1209. minX = 0,
  1210. maxX = 0,
  1211. minY = 0;
  1212. let c, k;
  1213. for (k = 0; k < templateLength; k++) {
  1214. templateX[k] = template[k].x;
  1215. templateY[k] = template[k].y;
  1216. minX = Math.min(minX, template[k].x);
  1217. maxX = Math.max(maxX, template[k].x);
  1218. minY = Math.min(minY, template[k].y);
  1219. if (k < templateLength - 1 && template[k].y === template[k + 1].y && template[k].x === template[k + 1].x - 1) {
  1220. reuseMask |= 1 << templateLength - 1 - k;
  1221. } else {
  1222. changingTemplateEntries.push(k);
  1223. }
  1224. }
  1225. const changingEntriesLength = changingTemplateEntries.length;
  1226. const changingTemplateX = new Int8Array(changingEntriesLength);
  1227. const changingTemplateY = new Int8Array(changingEntriesLength);
  1228. const changingTemplateBit = new Uint16Array(changingEntriesLength);
  1229. for (c = 0; c < changingEntriesLength; c++) {
  1230. k = changingTemplateEntries[c];
  1231. changingTemplateX[c] = template[k].x;
  1232. changingTemplateY[c] = template[k].y;
  1233. changingTemplateBit[c] = 1 << templateLength - 1 - k;
  1234. }
  1235. const sbb_left = -minX;
  1236. const sbb_top = -minY;
  1237. const sbb_right = width - maxX;
  1238. const pseudoPixelContext = ReusedContexts[templateIndex];
  1239. let row = new Uint8Array(width);
  1240. const bitmap = [];
  1241. const decoder = decodingContext.decoder;
  1242. const contexts = decodingContext.contextCache.getContexts("GB");
  1243. let ltp = 0,
  1244. j,
  1245. i0,
  1246. j0,
  1247. contextLabel = 0,
  1248. bit,
  1249. shift;
  1250. for (let i = 0; i < height; i++) {
  1251. if (prediction) {
  1252. const sltp = decoder.readBit(contexts, pseudoPixelContext);
  1253. ltp ^= sltp;
  1254. if (ltp) {
  1255. bitmap.push(row);
  1256. continue;
  1257. }
  1258. }
  1259. row = new Uint8Array(row);
  1260. bitmap.push(row);
  1261. for (j = 0; j < width; j++) {
  1262. if (useskip && skip[i][j]) {
  1263. row[j] = 0;
  1264. continue;
  1265. }
  1266. if (j >= sbb_left && j < sbb_right && i >= sbb_top) {
  1267. contextLabel = contextLabel << 1 & reuseMask;
  1268. for (k = 0; k < changingEntriesLength; k++) {
  1269. i0 = i + changingTemplateY[k];
  1270. j0 = j + changingTemplateX[k];
  1271. bit = bitmap[i0][j0];
  1272. if (bit) {
  1273. bit = changingTemplateBit[k];
  1274. contextLabel |= bit;
  1275. }
  1276. }
  1277. } else {
  1278. contextLabel = 0;
  1279. shift = templateLength - 1;
  1280. for (k = 0; k < templateLength; k++, shift--) {
  1281. j0 = j + templateX[k];
  1282. if (j0 >= 0 && j0 < width) {
  1283. i0 = i + templateY[k];
  1284. if (i0 >= 0) {
  1285. bit = bitmap[i0][j0];
  1286. if (bit) {
  1287. contextLabel |= bit << shift;
  1288. }
  1289. }
  1290. }
  1291. }
  1292. }
  1293. const pixel = decoder.readBit(contexts, contextLabel);
  1294. row[j] = pixel;
  1295. }
  1296. }
  1297. return bitmap;
  1298. }
  1299. function decodeRefinement(width, height, templateIndex, referenceBitmap, offsetX, offsetY, prediction, at, decodingContext) {
  1300. let codingTemplate = RefinementTemplates[templateIndex].coding;
  1301. if (templateIndex === 0) {
  1302. codingTemplate = codingTemplate.concat([at[0]]);
  1303. }
  1304. const codingTemplateLength = codingTemplate.length;
  1305. const codingTemplateX = new Int32Array(codingTemplateLength);
  1306. const codingTemplateY = new Int32Array(codingTemplateLength);
  1307. let k;
  1308. for (k = 0; k < codingTemplateLength; k++) {
  1309. codingTemplateX[k] = codingTemplate[k].x;
  1310. codingTemplateY[k] = codingTemplate[k].y;
  1311. }
  1312. let referenceTemplate = RefinementTemplates[templateIndex].reference;
  1313. if (templateIndex === 0) {
  1314. referenceTemplate = referenceTemplate.concat([at[1]]);
  1315. }
  1316. const referenceTemplateLength = referenceTemplate.length;
  1317. const referenceTemplateX = new Int32Array(referenceTemplateLength);
  1318. const referenceTemplateY = new Int32Array(referenceTemplateLength);
  1319. for (k = 0; k < referenceTemplateLength; k++) {
  1320. referenceTemplateX[k] = referenceTemplate[k].x;
  1321. referenceTemplateY[k] = referenceTemplate[k].y;
  1322. }
  1323. const referenceWidth = referenceBitmap[0].length;
  1324. const referenceHeight = referenceBitmap.length;
  1325. const pseudoPixelContext = RefinementReusedContexts[templateIndex];
  1326. const bitmap = [];
  1327. const decoder = decodingContext.decoder;
  1328. const contexts = decodingContext.contextCache.getContexts("GR");
  1329. let ltp = 0;
  1330. for (let i = 0; i < height; i++) {
  1331. if (prediction) {
  1332. const sltp = decoder.readBit(contexts, pseudoPixelContext);
  1333. ltp ^= sltp;
  1334. if (ltp) {
  1335. throw new Jbig2Error("prediction is not supported");
  1336. }
  1337. }
  1338. const row = new Uint8Array(width);
  1339. bitmap.push(row);
  1340. for (let j = 0; j < width; j++) {
  1341. let i0, j0;
  1342. let contextLabel = 0;
  1343. for (k = 0; k < codingTemplateLength; k++) {
  1344. i0 = i + codingTemplateY[k];
  1345. j0 = j + codingTemplateX[k];
  1346. if (i0 < 0 || j0 < 0 || j0 >= width) {
  1347. contextLabel <<= 1;
  1348. } else {
  1349. contextLabel = contextLabel << 1 | bitmap[i0][j0];
  1350. }
  1351. }
  1352. for (k = 0; k < referenceTemplateLength; k++) {
  1353. i0 = i + referenceTemplateY[k] - offsetY;
  1354. j0 = j + referenceTemplateX[k] - offsetX;
  1355. if (i0 < 0 || i0 >= referenceHeight || j0 < 0 || j0 >= referenceWidth) {
  1356. contextLabel <<= 1;
  1357. } else {
  1358. contextLabel = contextLabel << 1 | referenceBitmap[i0][j0];
  1359. }
  1360. }
  1361. const pixel = decoder.readBit(contexts, contextLabel);
  1362. row[j] = pixel;
  1363. }
  1364. }
  1365. return bitmap;
  1366. }
  1367. function decodeSymbolDictionary(huffman, refinement, symbols, numberOfNewSymbols, numberOfExportedSymbols, huffmanTables, templateIndex, at, refinementTemplateIndex, refinementAt, decodingContext, huffmanInput) {
  1368. if (huffman && refinement) {
  1369. throw new Jbig2Error("symbol refinement with Huffman is not supported");
  1370. }
  1371. const newSymbols = [];
  1372. let currentHeight = 0;
  1373. let symbolCodeLength = (0, _core_utils.log2)(symbols.length + numberOfNewSymbols);
  1374. const decoder = decodingContext.decoder;
  1375. const contextCache = decodingContext.contextCache;
  1376. let tableB1, symbolWidths;
  1377. if (huffman) {
  1378. tableB1 = getStandardTable(1);
  1379. symbolWidths = [];
  1380. symbolCodeLength = Math.max(symbolCodeLength, 1);
  1381. }
  1382. while (newSymbols.length < numberOfNewSymbols) {
  1383. const deltaHeight = huffman ? huffmanTables.tableDeltaHeight.decode(huffmanInput) : decodeInteger(contextCache, "IADH", decoder);
  1384. currentHeight += deltaHeight;
  1385. let currentWidth = 0,
  1386. totalWidth = 0;
  1387. const firstSymbol = huffman ? symbolWidths.length : 0;
  1388. while (true) {
  1389. const deltaWidth = huffman ? huffmanTables.tableDeltaWidth.decode(huffmanInput) : decodeInteger(contextCache, "IADW", decoder);
  1390. if (deltaWidth === null) {
  1391. break;
  1392. }
  1393. currentWidth += deltaWidth;
  1394. totalWidth += currentWidth;
  1395. let bitmap;
  1396. if (refinement) {
  1397. const numberOfInstances = decodeInteger(contextCache, "IAAI", decoder);
  1398. if (numberOfInstances > 1) {
  1399. bitmap = decodeTextRegion(huffman, refinement, currentWidth, currentHeight, 0, numberOfInstances, 1, symbols.concat(newSymbols), symbolCodeLength, 0, 0, 1, 0, huffmanTables, refinementTemplateIndex, refinementAt, decodingContext, 0, huffmanInput);
  1400. } else {
  1401. const symbolId = decodeIAID(contextCache, decoder, symbolCodeLength);
  1402. const rdx = decodeInteger(contextCache, "IARDX", decoder);
  1403. const rdy = decodeInteger(contextCache, "IARDY", decoder);
  1404. const symbol = symbolId < symbols.length ? symbols[symbolId] : newSymbols[symbolId - symbols.length];
  1405. bitmap = decodeRefinement(currentWidth, currentHeight, refinementTemplateIndex, symbol, rdx, rdy, false, refinementAt, decodingContext);
  1406. }
  1407. newSymbols.push(bitmap);
  1408. } else if (huffman) {
  1409. symbolWidths.push(currentWidth);
  1410. } else {
  1411. bitmap = decodeBitmap(false, currentWidth, currentHeight, templateIndex, false, null, at, decodingContext);
  1412. newSymbols.push(bitmap);
  1413. }
  1414. }
  1415. if (huffman && !refinement) {
  1416. const bitmapSize = huffmanTables.tableBitmapSize.decode(huffmanInput);
  1417. huffmanInput.byteAlign();
  1418. let collectiveBitmap;
  1419. if (bitmapSize === 0) {
  1420. collectiveBitmap = readUncompressedBitmap(huffmanInput, totalWidth, currentHeight);
  1421. } else {
  1422. const originalEnd = huffmanInput.end;
  1423. const bitmapEnd = huffmanInput.position + bitmapSize;
  1424. huffmanInput.end = bitmapEnd;
  1425. collectiveBitmap = decodeMMRBitmap(huffmanInput, totalWidth, currentHeight, false);
  1426. huffmanInput.end = originalEnd;
  1427. huffmanInput.position = bitmapEnd;
  1428. }
  1429. const numberOfSymbolsDecoded = symbolWidths.length;
  1430. if (firstSymbol === numberOfSymbolsDecoded - 1) {
  1431. newSymbols.push(collectiveBitmap);
  1432. } else {
  1433. let i,
  1434. y,
  1435. xMin = 0,
  1436. xMax,
  1437. bitmapWidth,
  1438. symbolBitmap;
  1439. for (i = firstSymbol; i < numberOfSymbolsDecoded; i++) {
  1440. bitmapWidth = symbolWidths[i];
  1441. xMax = xMin + bitmapWidth;
  1442. symbolBitmap = [];
  1443. for (y = 0; y < currentHeight; y++) {
  1444. symbolBitmap.push(collectiveBitmap[y].subarray(xMin, xMax));
  1445. }
  1446. newSymbols.push(symbolBitmap);
  1447. xMin = xMax;
  1448. }
  1449. }
  1450. }
  1451. }
  1452. const exportedSymbols = [],
  1453. flags = [];
  1454. let currentFlag = false,
  1455. i,
  1456. ii;
  1457. const totalSymbolsLength = symbols.length + numberOfNewSymbols;
  1458. while (flags.length < totalSymbolsLength) {
  1459. let runLength = huffman ? tableB1.decode(huffmanInput) : decodeInteger(contextCache, "IAEX", decoder);
  1460. while (runLength--) {
  1461. flags.push(currentFlag);
  1462. }
  1463. currentFlag = !currentFlag;
  1464. }
  1465. for (i = 0, ii = symbols.length; i < ii; i++) {
  1466. if (flags[i]) {
  1467. exportedSymbols.push(symbols[i]);
  1468. }
  1469. }
  1470. for (let j = 0; j < numberOfNewSymbols; i++, j++) {
  1471. if (flags[i]) {
  1472. exportedSymbols.push(newSymbols[j]);
  1473. }
  1474. }
  1475. return exportedSymbols;
  1476. }
  1477. function decodeTextRegion(huffman, refinement, width, height, defaultPixelValue, numberOfSymbolInstances, stripSize, inputSymbols, symbolCodeLength, transposed, dsOffset, referenceCorner, combinationOperator, huffmanTables, refinementTemplateIndex, refinementAt, decodingContext, logStripSize, huffmanInput) {
  1478. if (huffman && refinement) {
  1479. throw new Jbig2Error("refinement with Huffman is not supported");
  1480. }
  1481. const bitmap = [];
  1482. let i, row;
  1483. for (i = 0; i < height; i++) {
  1484. row = new Uint8Array(width);
  1485. if (defaultPixelValue) {
  1486. for (let j = 0; j < width; j++) {
  1487. row[j] = defaultPixelValue;
  1488. }
  1489. }
  1490. bitmap.push(row);
  1491. }
  1492. const decoder = decodingContext.decoder;
  1493. const contextCache = decodingContext.contextCache;
  1494. let stripT = huffman ? -huffmanTables.tableDeltaT.decode(huffmanInput) : -decodeInteger(contextCache, "IADT", decoder);
  1495. let firstS = 0;
  1496. i = 0;
  1497. while (i < numberOfSymbolInstances) {
  1498. const deltaT = huffman ? huffmanTables.tableDeltaT.decode(huffmanInput) : decodeInteger(contextCache, "IADT", decoder);
  1499. stripT += deltaT;
  1500. const deltaFirstS = huffman ? huffmanTables.tableFirstS.decode(huffmanInput) : decodeInteger(contextCache, "IAFS", decoder);
  1501. firstS += deltaFirstS;
  1502. let currentS = firstS;
  1503. do {
  1504. let currentT = 0;
  1505. if (stripSize > 1) {
  1506. currentT = huffman ? huffmanInput.readBits(logStripSize) : decodeInteger(contextCache, "IAIT", decoder);
  1507. }
  1508. const t = stripSize * stripT + currentT;
  1509. const symbolId = huffman ? huffmanTables.symbolIDTable.decode(huffmanInput) : decodeIAID(contextCache, decoder, symbolCodeLength);
  1510. const applyRefinement = refinement && (huffman ? huffmanInput.readBit() : decodeInteger(contextCache, "IARI", decoder));
  1511. let symbolBitmap = inputSymbols[symbolId];
  1512. let symbolWidth = symbolBitmap[0].length;
  1513. let symbolHeight = symbolBitmap.length;
  1514. if (applyRefinement) {
  1515. const rdw = decodeInteger(contextCache, "IARDW", decoder);
  1516. const rdh = decodeInteger(contextCache, "IARDH", decoder);
  1517. const rdx = decodeInteger(contextCache, "IARDX", decoder);
  1518. const rdy = decodeInteger(contextCache, "IARDY", decoder);
  1519. symbolWidth += rdw;
  1520. symbolHeight += rdh;
  1521. symbolBitmap = decodeRefinement(symbolWidth, symbolHeight, refinementTemplateIndex, symbolBitmap, (rdw >> 1) + rdx, (rdh >> 1) + rdy, false, refinementAt, decodingContext);
  1522. }
  1523. const offsetT = t - (referenceCorner & 1 ? 0 : symbolHeight - 1);
  1524. const offsetS = currentS - (referenceCorner & 2 ? symbolWidth - 1 : 0);
  1525. let s2, t2, symbolRow;
  1526. if (transposed) {
  1527. for (s2 = 0; s2 < symbolHeight; s2++) {
  1528. row = bitmap[offsetS + s2];
  1529. if (!row) {
  1530. continue;
  1531. }
  1532. symbolRow = symbolBitmap[s2];
  1533. const maxWidth = Math.min(width - offsetT, symbolWidth);
  1534. switch (combinationOperator) {
  1535. case 0:
  1536. for (t2 = 0; t2 < maxWidth; t2++) {
  1537. row[offsetT + t2] |= symbolRow[t2];
  1538. }
  1539. break;
  1540. case 2:
  1541. for (t2 = 0; t2 < maxWidth; t2++) {
  1542. row[offsetT + t2] ^= symbolRow[t2];
  1543. }
  1544. break;
  1545. default:
  1546. throw new Jbig2Error(`operator ${combinationOperator} is not supported`);
  1547. }
  1548. }
  1549. currentS += symbolHeight - 1;
  1550. } else {
  1551. for (t2 = 0; t2 < symbolHeight; t2++) {
  1552. row = bitmap[offsetT + t2];
  1553. if (!row) {
  1554. continue;
  1555. }
  1556. symbolRow = symbolBitmap[t2];
  1557. switch (combinationOperator) {
  1558. case 0:
  1559. for (s2 = 0; s2 < symbolWidth; s2++) {
  1560. row[offsetS + s2] |= symbolRow[s2];
  1561. }
  1562. break;
  1563. case 2:
  1564. for (s2 = 0; s2 < symbolWidth; s2++) {
  1565. row[offsetS + s2] ^= symbolRow[s2];
  1566. }
  1567. break;
  1568. default:
  1569. throw new Jbig2Error(`operator ${combinationOperator} is not supported`);
  1570. }
  1571. }
  1572. currentS += symbolWidth - 1;
  1573. }
  1574. i++;
  1575. const deltaS = huffman ? huffmanTables.tableDeltaS.decode(huffmanInput) : decodeInteger(contextCache, "IADS", decoder);
  1576. if (deltaS === null) {
  1577. break;
  1578. }
  1579. currentS += deltaS + dsOffset;
  1580. } while (true);
  1581. }
  1582. return bitmap;
  1583. }
  1584. function decodePatternDictionary(mmr, patternWidth, patternHeight, maxPatternIndex, template, decodingContext) {
  1585. const at = [];
  1586. if (!mmr) {
  1587. at.push({
  1588. x: -patternWidth,
  1589. y: 0
  1590. });
  1591. if (template === 0) {
  1592. at.push({
  1593. x: -3,
  1594. y: -1
  1595. }, {
  1596. x: 2,
  1597. y: -2
  1598. }, {
  1599. x: -2,
  1600. y: -2
  1601. });
  1602. }
  1603. }
  1604. const collectiveWidth = (maxPatternIndex + 1) * patternWidth;
  1605. const collectiveBitmap = decodeBitmap(mmr, collectiveWidth, patternHeight, template, false, null, at, decodingContext);
  1606. const patterns = [];
  1607. for (let i = 0; i <= maxPatternIndex; i++) {
  1608. const patternBitmap = [];
  1609. const xMin = patternWidth * i;
  1610. const xMax = xMin + patternWidth;
  1611. for (let y = 0; y < patternHeight; y++) {
  1612. patternBitmap.push(collectiveBitmap[y].subarray(xMin, xMax));
  1613. }
  1614. patterns.push(patternBitmap);
  1615. }
  1616. return patterns;
  1617. }
  1618. function decodeHalftoneRegion(mmr, patterns, template, regionWidth, regionHeight, defaultPixelValue, enableSkip, combinationOperator, gridWidth, gridHeight, gridOffsetX, gridOffsetY, gridVectorX, gridVectorY, decodingContext) {
  1619. const skip = null;
  1620. if (enableSkip) {
  1621. throw new Jbig2Error("skip is not supported");
  1622. }
  1623. if (combinationOperator !== 0) {
  1624. throw new Jbig2Error(`operator "${combinationOperator}" is not supported in halftone region`);
  1625. }
  1626. const regionBitmap = [];
  1627. let i, j, row;
  1628. for (i = 0; i < regionHeight; i++) {
  1629. row = new Uint8Array(regionWidth);
  1630. if (defaultPixelValue) {
  1631. for (j = 0; j < regionWidth; j++) {
  1632. row[j] = defaultPixelValue;
  1633. }
  1634. }
  1635. regionBitmap.push(row);
  1636. }
  1637. const numberOfPatterns = patterns.length;
  1638. const pattern0 = patterns[0];
  1639. const patternWidth = pattern0[0].length,
  1640. patternHeight = pattern0.length;
  1641. const bitsPerValue = (0, _core_utils.log2)(numberOfPatterns);
  1642. const at = [];
  1643. if (!mmr) {
  1644. at.push({
  1645. x: template <= 1 ? 3 : 2,
  1646. y: -1
  1647. });
  1648. if (template === 0) {
  1649. at.push({
  1650. x: -3,
  1651. y: -1
  1652. }, {
  1653. x: 2,
  1654. y: -2
  1655. }, {
  1656. x: -2,
  1657. y: -2
  1658. });
  1659. }
  1660. }
  1661. const grayScaleBitPlanes = [];
  1662. let mmrInput, bitmap;
  1663. if (mmr) {
  1664. mmrInput = new Reader(decodingContext.data, decodingContext.start, decodingContext.end);
  1665. }
  1666. for (i = bitsPerValue - 1; i >= 0; i--) {
  1667. if (mmr) {
  1668. bitmap = decodeMMRBitmap(mmrInput, gridWidth, gridHeight, true);
  1669. } else {
  1670. bitmap = decodeBitmap(false, gridWidth, gridHeight, template, false, skip, at, decodingContext);
  1671. }
  1672. grayScaleBitPlanes[i] = bitmap;
  1673. }
  1674. let mg, ng, bit, patternIndex, patternBitmap, x, y, patternRow, regionRow;
  1675. for (mg = 0; mg < gridHeight; mg++) {
  1676. for (ng = 0; ng < gridWidth; ng++) {
  1677. bit = 0;
  1678. patternIndex = 0;
  1679. for (j = bitsPerValue - 1; j >= 0; j--) {
  1680. bit ^= grayScaleBitPlanes[j][mg][ng];
  1681. patternIndex |= bit << j;
  1682. }
  1683. patternBitmap = patterns[patternIndex];
  1684. x = gridOffsetX + mg * gridVectorY + ng * gridVectorX >> 8;
  1685. y = gridOffsetY + mg * gridVectorX - ng * gridVectorY >> 8;
  1686. if (x >= 0 && x + patternWidth <= regionWidth && y >= 0 && y + patternHeight <= regionHeight) {
  1687. for (i = 0; i < patternHeight; i++) {
  1688. regionRow = regionBitmap[y + i];
  1689. patternRow = patternBitmap[i];
  1690. for (j = 0; j < patternWidth; j++) {
  1691. regionRow[x + j] |= patternRow[j];
  1692. }
  1693. }
  1694. } else {
  1695. let regionX, regionY;
  1696. for (i = 0; i < patternHeight; i++) {
  1697. regionY = y + i;
  1698. if (regionY < 0 || regionY >= regionHeight) {
  1699. continue;
  1700. }
  1701. regionRow = regionBitmap[regionY];
  1702. patternRow = patternBitmap[i];
  1703. for (j = 0; j < patternWidth; j++) {
  1704. regionX = x + j;
  1705. if (regionX >= 0 && regionX < regionWidth) {
  1706. regionRow[regionX] |= patternRow[j];
  1707. }
  1708. }
  1709. }
  1710. }
  1711. }
  1712. }
  1713. return regionBitmap;
  1714. }
  1715. function readSegmentHeader(data, start) {
  1716. const segmentHeader = {};
  1717. segmentHeader.number = (0, _core_utils.readUint32)(data, start);
  1718. const flags = data[start + 4];
  1719. const segmentType = flags & 0x3f;
  1720. if (!SegmentTypes[segmentType]) {
  1721. throw new Jbig2Error("invalid segment type: " + segmentType);
  1722. }
  1723. segmentHeader.type = segmentType;
  1724. segmentHeader.typeName = SegmentTypes[segmentType];
  1725. segmentHeader.deferredNonRetain = !!(flags & 0x80);
  1726. const pageAssociationFieldSize = !!(flags & 0x40);
  1727. const referredFlags = data[start + 5];
  1728. let referredToCount = referredFlags >> 5 & 7;
  1729. const retainBits = [referredFlags & 31];
  1730. let position = start + 6;
  1731. if (referredFlags === 7) {
  1732. referredToCount = (0, _core_utils.readUint32)(data, position - 1) & 0x1fffffff;
  1733. position += 3;
  1734. let bytes = referredToCount + 7 >> 3;
  1735. retainBits[0] = data[position++];
  1736. while (--bytes > 0) {
  1737. retainBits.push(data[position++]);
  1738. }
  1739. } else if (referredFlags === 5 || referredFlags === 6) {
  1740. throw new Jbig2Error("invalid referred-to flags");
  1741. }
  1742. segmentHeader.retainBits = retainBits;
  1743. let referredToSegmentNumberSize = 4;
  1744. if (segmentHeader.number <= 256) {
  1745. referredToSegmentNumberSize = 1;
  1746. } else if (segmentHeader.number <= 65536) {
  1747. referredToSegmentNumberSize = 2;
  1748. }
  1749. const referredTo = [];
  1750. let i, ii;
  1751. for (i = 0; i < referredToCount; i++) {
  1752. let number;
  1753. if (referredToSegmentNumberSize === 1) {
  1754. number = data[position];
  1755. } else if (referredToSegmentNumberSize === 2) {
  1756. number = (0, _core_utils.readUint16)(data, position);
  1757. } else {
  1758. number = (0, _core_utils.readUint32)(data, position);
  1759. }
  1760. referredTo.push(number);
  1761. position += referredToSegmentNumberSize;
  1762. }
  1763. segmentHeader.referredTo = referredTo;
  1764. if (!pageAssociationFieldSize) {
  1765. segmentHeader.pageAssociation = data[position++];
  1766. } else {
  1767. segmentHeader.pageAssociation = (0, _core_utils.readUint32)(data, position);
  1768. position += 4;
  1769. }
  1770. segmentHeader.length = (0, _core_utils.readUint32)(data, position);
  1771. position += 4;
  1772. if (segmentHeader.length === 0xffffffff) {
  1773. if (segmentType === 38) {
  1774. const genericRegionInfo = readRegionSegmentInformation(data, position);
  1775. const genericRegionSegmentFlags = data[position + RegionSegmentInformationFieldLength];
  1776. const genericRegionMmr = !!(genericRegionSegmentFlags & 1);
  1777. const searchPatternLength = 6;
  1778. const searchPattern = new Uint8Array(searchPatternLength);
  1779. if (!genericRegionMmr) {
  1780. searchPattern[0] = 0xff;
  1781. searchPattern[1] = 0xac;
  1782. }
  1783. searchPattern[2] = genericRegionInfo.height >>> 24 & 0xff;
  1784. searchPattern[3] = genericRegionInfo.height >> 16 & 0xff;
  1785. searchPattern[4] = genericRegionInfo.height >> 8 & 0xff;
  1786. searchPattern[5] = genericRegionInfo.height & 0xff;
  1787. for (i = position, ii = data.length; i < ii; i++) {
  1788. let j = 0;
  1789. while (j < searchPatternLength && searchPattern[j] === data[i + j]) {
  1790. j++;
  1791. }
  1792. if (j === searchPatternLength) {
  1793. segmentHeader.length = i + searchPatternLength;
  1794. break;
  1795. }
  1796. }
  1797. if (segmentHeader.length === 0xffffffff) {
  1798. throw new Jbig2Error("segment end was not found");
  1799. }
  1800. } else {
  1801. throw new Jbig2Error("invalid unknown segment length");
  1802. }
  1803. }
  1804. segmentHeader.headerEnd = position;
  1805. return segmentHeader;
  1806. }
  1807. function readSegments(header, data, start, end) {
  1808. const segments = [];
  1809. let position = start;
  1810. while (position < end) {
  1811. const segmentHeader = readSegmentHeader(data, position);
  1812. position = segmentHeader.headerEnd;
  1813. const segment = {
  1814. header: segmentHeader,
  1815. data
  1816. };
  1817. if (!header.randomAccess) {
  1818. segment.start = position;
  1819. position += segmentHeader.length;
  1820. segment.end = position;
  1821. }
  1822. segments.push(segment);
  1823. if (segmentHeader.type === 51) {
  1824. break;
  1825. }
  1826. }
  1827. if (header.randomAccess) {
  1828. for (let i = 0, ii = segments.length; i < ii; i++) {
  1829. segments[i].start = position;
  1830. position += segments[i].header.length;
  1831. segments[i].end = position;
  1832. }
  1833. }
  1834. return segments;
  1835. }
  1836. function readRegionSegmentInformation(data, start) {
  1837. return {
  1838. width: (0, _core_utils.readUint32)(data, start),
  1839. height: (0, _core_utils.readUint32)(data, start + 4),
  1840. x: (0, _core_utils.readUint32)(data, start + 8),
  1841. y: (0, _core_utils.readUint32)(data, start + 12),
  1842. combinationOperator: data[start + 16] & 7
  1843. };
  1844. }
  1845. const RegionSegmentInformationFieldLength = 17;
  1846. function processSegment(segment, visitor) {
  1847. const header = segment.header;
  1848. const data = segment.data,
  1849. end = segment.end;
  1850. let position = segment.start;
  1851. let args, at, i, atLength;
  1852. switch (header.type) {
  1853. case 0:
  1854. const dictionary = {};
  1855. const dictionaryFlags = (0, _core_utils.readUint16)(data, position);
  1856. dictionary.huffman = !!(dictionaryFlags & 1);
  1857. dictionary.refinement = !!(dictionaryFlags & 2);
  1858. dictionary.huffmanDHSelector = dictionaryFlags >> 2 & 3;
  1859. dictionary.huffmanDWSelector = dictionaryFlags >> 4 & 3;
  1860. dictionary.bitmapSizeSelector = dictionaryFlags >> 6 & 1;
  1861. dictionary.aggregationInstancesSelector = dictionaryFlags >> 7 & 1;
  1862. dictionary.bitmapCodingContextUsed = !!(dictionaryFlags & 256);
  1863. dictionary.bitmapCodingContextRetained = !!(dictionaryFlags & 512);
  1864. dictionary.template = dictionaryFlags >> 10 & 3;
  1865. dictionary.refinementTemplate = dictionaryFlags >> 12 & 1;
  1866. position += 2;
  1867. if (!dictionary.huffman) {
  1868. atLength = dictionary.template === 0 ? 4 : 1;
  1869. at = [];
  1870. for (i = 0; i < atLength; i++) {
  1871. at.push({
  1872. x: (0, _core_utils.readInt8)(data, position),
  1873. y: (0, _core_utils.readInt8)(data, position + 1)
  1874. });
  1875. position += 2;
  1876. }
  1877. dictionary.at = at;
  1878. }
  1879. if (dictionary.refinement && !dictionary.refinementTemplate) {
  1880. at = [];
  1881. for (i = 0; i < 2; i++) {
  1882. at.push({
  1883. x: (0, _core_utils.readInt8)(data, position),
  1884. y: (0, _core_utils.readInt8)(data, position + 1)
  1885. });
  1886. position += 2;
  1887. }
  1888. dictionary.refinementAt = at;
  1889. }
  1890. dictionary.numberOfExportedSymbols = (0, _core_utils.readUint32)(data, position);
  1891. position += 4;
  1892. dictionary.numberOfNewSymbols = (0, _core_utils.readUint32)(data, position);
  1893. position += 4;
  1894. args = [dictionary, header.number, header.referredTo, data, position, end];
  1895. break;
  1896. case 6:
  1897. case 7:
  1898. const textRegion = {};
  1899. textRegion.info = readRegionSegmentInformation(data, position);
  1900. position += RegionSegmentInformationFieldLength;
  1901. const textRegionSegmentFlags = (0, _core_utils.readUint16)(data, position);
  1902. position += 2;
  1903. textRegion.huffman = !!(textRegionSegmentFlags & 1);
  1904. textRegion.refinement = !!(textRegionSegmentFlags & 2);
  1905. textRegion.logStripSize = textRegionSegmentFlags >> 2 & 3;
  1906. textRegion.stripSize = 1 << textRegion.logStripSize;
  1907. textRegion.referenceCorner = textRegionSegmentFlags >> 4 & 3;
  1908. textRegion.transposed = !!(textRegionSegmentFlags & 64);
  1909. textRegion.combinationOperator = textRegionSegmentFlags >> 7 & 3;
  1910. textRegion.defaultPixelValue = textRegionSegmentFlags >> 9 & 1;
  1911. textRegion.dsOffset = textRegionSegmentFlags << 17 >> 27;
  1912. textRegion.refinementTemplate = textRegionSegmentFlags >> 15 & 1;
  1913. if (textRegion.huffman) {
  1914. const textRegionHuffmanFlags = (0, _core_utils.readUint16)(data, position);
  1915. position += 2;
  1916. textRegion.huffmanFS = textRegionHuffmanFlags & 3;
  1917. textRegion.huffmanDS = textRegionHuffmanFlags >> 2 & 3;
  1918. textRegion.huffmanDT = textRegionHuffmanFlags >> 4 & 3;
  1919. textRegion.huffmanRefinementDW = textRegionHuffmanFlags >> 6 & 3;
  1920. textRegion.huffmanRefinementDH = textRegionHuffmanFlags >> 8 & 3;
  1921. textRegion.huffmanRefinementDX = textRegionHuffmanFlags >> 10 & 3;
  1922. textRegion.huffmanRefinementDY = textRegionHuffmanFlags >> 12 & 3;
  1923. textRegion.huffmanRefinementSizeSelector = !!(textRegionHuffmanFlags & 0x4000);
  1924. }
  1925. if (textRegion.refinement && !textRegion.refinementTemplate) {
  1926. at = [];
  1927. for (i = 0; i < 2; i++) {
  1928. at.push({
  1929. x: (0, _core_utils.readInt8)(data, position),
  1930. y: (0, _core_utils.readInt8)(data, position + 1)
  1931. });
  1932. position += 2;
  1933. }
  1934. textRegion.refinementAt = at;
  1935. }
  1936. textRegion.numberOfSymbolInstances = (0, _core_utils.readUint32)(data, position);
  1937. position += 4;
  1938. args = [textRegion, header.referredTo, data, position, end];
  1939. break;
  1940. case 16:
  1941. const patternDictionary = {};
  1942. const patternDictionaryFlags = data[position++];
  1943. patternDictionary.mmr = !!(patternDictionaryFlags & 1);
  1944. patternDictionary.template = patternDictionaryFlags >> 1 & 3;
  1945. patternDictionary.patternWidth = data[position++];
  1946. patternDictionary.patternHeight = data[position++];
  1947. patternDictionary.maxPatternIndex = (0, _core_utils.readUint32)(data, position);
  1948. position += 4;
  1949. args = [patternDictionary, header.number, data, position, end];
  1950. break;
  1951. case 22:
  1952. case 23:
  1953. const halftoneRegion = {};
  1954. halftoneRegion.info = readRegionSegmentInformation(data, position);
  1955. position += RegionSegmentInformationFieldLength;
  1956. const halftoneRegionFlags = data[position++];
  1957. halftoneRegion.mmr = !!(halftoneRegionFlags & 1);
  1958. halftoneRegion.template = halftoneRegionFlags >> 1 & 3;
  1959. halftoneRegion.enableSkip = !!(halftoneRegionFlags & 8);
  1960. halftoneRegion.combinationOperator = halftoneRegionFlags >> 4 & 7;
  1961. halftoneRegion.defaultPixelValue = halftoneRegionFlags >> 7 & 1;
  1962. halftoneRegion.gridWidth = (0, _core_utils.readUint32)(data, position);
  1963. position += 4;
  1964. halftoneRegion.gridHeight = (0, _core_utils.readUint32)(data, position);
  1965. position += 4;
  1966. halftoneRegion.gridOffsetX = (0, _core_utils.readUint32)(data, position) & 0xffffffff;
  1967. position += 4;
  1968. halftoneRegion.gridOffsetY = (0, _core_utils.readUint32)(data, position) & 0xffffffff;
  1969. position += 4;
  1970. halftoneRegion.gridVectorX = (0, _core_utils.readUint16)(data, position);
  1971. position += 2;
  1972. halftoneRegion.gridVectorY = (0, _core_utils.readUint16)(data, position);
  1973. position += 2;
  1974. args = [halftoneRegion, header.referredTo, data, position, end];
  1975. break;
  1976. case 38:
  1977. case 39:
  1978. const genericRegion = {};
  1979. genericRegion.info = readRegionSegmentInformation(data, position);
  1980. position += RegionSegmentInformationFieldLength;
  1981. const genericRegionSegmentFlags = data[position++];
  1982. genericRegion.mmr = !!(genericRegionSegmentFlags & 1);
  1983. genericRegion.template = genericRegionSegmentFlags >> 1 & 3;
  1984. genericRegion.prediction = !!(genericRegionSegmentFlags & 8);
  1985. if (!genericRegion.mmr) {
  1986. atLength = genericRegion.template === 0 ? 4 : 1;
  1987. at = [];
  1988. for (i = 0; i < atLength; i++) {
  1989. at.push({
  1990. x: (0, _core_utils.readInt8)(data, position),
  1991. y: (0, _core_utils.readInt8)(data, position + 1)
  1992. });
  1993. position += 2;
  1994. }
  1995. genericRegion.at = at;
  1996. }
  1997. args = [genericRegion, data, position, end];
  1998. break;
  1999. case 48:
  2000. const pageInfo = {
  2001. width: (0, _core_utils.readUint32)(data, position),
  2002. height: (0, _core_utils.readUint32)(data, position + 4),
  2003. resolutionX: (0, _core_utils.readUint32)(data, position + 8),
  2004. resolutionY: (0, _core_utils.readUint32)(data, position + 12)
  2005. };
  2006. if (pageInfo.height === 0xffffffff) {
  2007. delete pageInfo.height;
  2008. }
  2009. const pageSegmentFlags = data[position + 16];
  2010. (0, _core_utils.readUint16)(data, position + 17);
  2011. pageInfo.lossless = !!(pageSegmentFlags & 1);
  2012. pageInfo.refinement = !!(pageSegmentFlags & 2);
  2013. pageInfo.defaultPixelValue = pageSegmentFlags >> 2 & 1;
  2014. pageInfo.combinationOperator = pageSegmentFlags >> 3 & 3;
  2015. pageInfo.requiresBuffer = !!(pageSegmentFlags & 32);
  2016. pageInfo.combinationOperatorOverride = !!(pageSegmentFlags & 64);
  2017. args = [pageInfo];
  2018. break;
  2019. case 49:
  2020. break;
  2021. case 50:
  2022. break;
  2023. case 51:
  2024. break;
  2025. case 53:
  2026. args = [header.number, data, position, end];
  2027. break;
  2028. case 62:
  2029. break;
  2030. default:
  2031. throw new Jbig2Error(`segment type ${header.typeName}(${header.type}) is not implemented`);
  2032. }
  2033. const callbackName = "on" + header.typeName;
  2034. if (callbackName in visitor) {
  2035. visitor[callbackName].apply(visitor, args);
  2036. }
  2037. }
  2038. function processSegments(segments, visitor) {
  2039. for (let i = 0, ii = segments.length; i < ii; i++) {
  2040. processSegment(segments[i], visitor);
  2041. }
  2042. }
  2043. function parseJbig2Chunks(chunks) {
  2044. const visitor = new SimpleSegmentVisitor();
  2045. for (let i = 0, ii = chunks.length; i < ii; i++) {
  2046. const chunk = chunks[i];
  2047. const segments = readSegments({}, chunk.data, chunk.start, chunk.end);
  2048. processSegments(segments, visitor);
  2049. }
  2050. return visitor.buffer;
  2051. }
  2052. function parseJbig2(data) {
  2053. const end = data.length;
  2054. let position = 0;
  2055. if (data[position] !== 0x97 || data[position + 1] !== 0x4a || data[position + 2] !== 0x42 || data[position + 3] !== 0x32 || data[position + 4] !== 0x0d || data[position + 5] !== 0x0a || data[position + 6] !== 0x1a || data[position + 7] !== 0x0a) {
  2056. throw new Jbig2Error("parseJbig2 - invalid header.");
  2057. }
  2058. const header = Object.create(null);
  2059. position += 8;
  2060. const flags = data[position++];
  2061. header.randomAccess = !(flags & 1);
  2062. if (!(flags & 2)) {
  2063. header.numberOfPages = (0, _core_utils.readUint32)(data, position);
  2064. position += 4;
  2065. }
  2066. const segments = readSegments(header, data, position, end);
  2067. const visitor = new SimpleSegmentVisitor();
  2068. processSegments(segments, visitor);
  2069. const {
  2070. width,
  2071. height
  2072. } = visitor.currentPageInfo;
  2073. const bitPacked = visitor.buffer;
  2074. const imgData = new Uint8ClampedArray(width * height);
  2075. let q = 0,
  2076. k = 0;
  2077. for (let i = 0; i < height; i++) {
  2078. let mask = 0,
  2079. buffer;
  2080. for (let j = 0; j < width; j++) {
  2081. if (!mask) {
  2082. mask = 128;
  2083. buffer = bitPacked[k++];
  2084. }
  2085. imgData[q++] = buffer & mask ? 0 : 255;
  2086. mask >>= 1;
  2087. }
  2088. }
  2089. return {
  2090. imgData,
  2091. width,
  2092. height
  2093. };
  2094. }
  2095. class SimpleSegmentVisitor {
  2096. onPageInformation(info) {
  2097. this.currentPageInfo = info;
  2098. const rowSize = info.width + 7 >> 3;
  2099. const buffer = new Uint8ClampedArray(rowSize * info.height);
  2100. if (info.defaultPixelValue) {
  2101. buffer.fill(0xff);
  2102. }
  2103. this.buffer = buffer;
  2104. }
  2105. drawBitmap(regionInfo, bitmap) {
  2106. const pageInfo = this.currentPageInfo;
  2107. const width = regionInfo.width,
  2108. height = regionInfo.height;
  2109. const rowSize = pageInfo.width + 7 >> 3;
  2110. const combinationOperator = pageInfo.combinationOperatorOverride ? regionInfo.combinationOperator : pageInfo.combinationOperator;
  2111. const buffer = this.buffer;
  2112. const mask0 = 128 >> (regionInfo.x & 7);
  2113. let offset0 = regionInfo.y * rowSize + (regionInfo.x >> 3);
  2114. let i, j, mask, offset;
  2115. switch (combinationOperator) {
  2116. case 0:
  2117. for (i = 0; i < height; i++) {
  2118. mask = mask0;
  2119. offset = offset0;
  2120. for (j = 0; j < width; j++) {
  2121. if (bitmap[i][j]) {
  2122. buffer[offset] |= mask;
  2123. }
  2124. mask >>= 1;
  2125. if (!mask) {
  2126. mask = 128;
  2127. offset++;
  2128. }
  2129. }
  2130. offset0 += rowSize;
  2131. }
  2132. break;
  2133. case 2:
  2134. for (i = 0; i < height; i++) {
  2135. mask = mask0;
  2136. offset = offset0;
  2137. for (j = 0; j < width; j++) {
  2138. if (bitmap[i][j]) {
  2139. buffer[offset] ^= mask;
  2140. }
  2141. mask >>= 1;
  2142. if (!mask) {
  2143. mask = 128;
  2144. offset++;
  2145. }
  2146. }
  2147. offset0 += rowSize;
  2148. }
  2149. break;
  2150. default:
  2151. throw new Jbig2Error(`operator ${combinationOperator} is not supported`);
  2152. }
  2153. }
  2154. onImmediateGenericRegion(region, data, start, end) {
  2155. const regionInfo = region.info;
  2156. const decodingContext = new DecodingContext(data, start, end);
  2157. const bitmap = decodeBitmap(region.mmr, regionInfo.width, regionInfo.height, region.template, region.prediction, null, region.at, decodingContext);
  2158. this.drawBitmap(regionInfo, bitmap);
  2159. }
  2160. onImmediateLosslessGenericRegion() {
  2161. this.onImmediateGenericRegion(...arguments);
  2162. }
  2163. onSymbolDictionary(dictionary, currentSegment, referredSegments, data, start, end) {
  2164. let huffmanTables, huffmanInput;
  2165. if (dictionary.huffman) {
  2166. huffmanTables = getSymbolDictionaryHuffmanTables(dictionary, referredSegments, this.customTables);
  2167. huffmanInput = new Reader(data, start, end);
  2168. }
  2169. let symbols = this.symbols;
  2170. if (!symbols) {
  2171. this.symbols = symbols = {};
  2172. }
  2173. const inputSymbols = [];
  2174. for (const referredSegment of referredSegments) {
  2175. const referredSymbols = symbols[referredSegment];
  2176. if (referredSymbols) {
  2177. inputSymbols.push(...referredSymbols);
  2178. }
  2179. }
  2180. const decodingContext = new DecodingContext(data, start, end);
  2181. symbols[currentSegment] = decodeSymbolDictionary(dictionary.huffman, dictionary.refinement, inputSymbols, dictionary.numberOfNewSymbols, dictionary.numberOfExportedSymbols, huffmanTables, dictionary.template, dictionary.at, dictionary.refinementTemplate, dictionary.refinementAt, decodingContext, huffmanInput);
  2182. }
  2183. onImmediateTextRegion(region, referredSegments, data, start, end) {
  2184. const regionInfo = region.info;
  2185. let huffmanTables, huffmanInput;
  2186. const symbols = this.symbols;
  2187. const inputSymbols = [];
  2188. for (const referredSegment of referredSegments) {
  2189. const referredSymbols = symbols[referredSegment];
  2190. if (referredSymbols) {
  2191. inputSymbols.push(...referredSymbols);
  2192. }
  2193. }
  2194. const symbolCodeLength = (0, _core_utils.log2)(inputSymbols.length);
  2195. if (region.huffman) {
  2196. huffmanInput = new Reader(data, start, end);
  2197. huffmanTables = getTextRegionHuffmanTables(region, referredSegments, this.customTables, inputSymbols.length, huffmanInput);
  2198. }
  2199. const decodingContext = new DecodingContext(data, start, end);
  2200. const bitmap = decodeTextRegion(region.huffman, region.refinement, regionInfo.width, regionInfo.height, region.defaultPixelValue, region.numberOfSymbolInstances, region.stripSize, inputSymbols, symbolCodeLength, region.transposed, region.dsOffset, region.referenceCorner, region.combinationOperator, huffmanTables, region.refinementTemplate, region.refinementAt, decodingContext, region.logStripSize, huffmanInput);
  2201. this.drawBitmap(regionInfo, bitmap);
  2202. }
  2203. onImmediateLosslessTextRegion() {
  2204. this.onImmediateTextRegion(...arguments);
  2205. }
  2206. onPatternDictionary(dictionary, currentSegment, data, start, end) {
  2207. let patterns = this.patterns;
  2208. if (!patterns) {
  2209. this.patterns = patterns = {};
  2210. }
  2211. const decodingContext = new DecodingContext(data, start, end);
  2212. patterns[currentSegment] = decodePatternDictionary(dictionary.mmr, dictionary.patternWidth, dictionary.patternHeight, dictionary.maxPatternIndex, dictionary.template, decodingContext);
  2213. }
  2214. onImmediateHalftoneRegion(region, referredSegments, data, start, end) {
  2215. const patterns = this.patterns[referredSegments[0]];
  2216. const regionInfo = region.info;
  2217. const decodingContext = new DecodingContext(data, start, end);
  2218. const bitmap = decodeHalftoneRegion(region.mmr, patterns, region.template, regionInfo.width, regionInfo.height, region.defaultPixelValue, region.enableSkip, region.combinationOperator, region.gridWidth, region.gridHeight, region.gridOffsetX, region.gridOffsetY, region.gridVectorX, region.gridVectorY, decodingContext);
  2219. this.drawBitmap(regionInfo, bitmap);
  2220. }
  2221. onImmediateLosslessHalftoneRegion() {
  2222. this.onImmediateHalftoneRegion(...arguments);
  2223. }
  2224. onTables(currentSegment, data, start, end) {
  2225. let customTables = this.customTables;
  2226. if (!customTables) {
  2227. this.customTables = customTables = {};
  2228. }
  2229. customTables[currentSegment] = decodeTablesSegment(data, start, end);
  2230. }
  2231. }
  2232. class HuffmanLine {
  2233. constructor(lineData) {
  2234. if (lineData.length === 2) {
  2235. this.isOOB = true;
  2236. this.rangeLow = 0;
  2237. this.prefixLength = lineData[0];
  2238. this.rangeLength = 0;
  2239. this.prefixCode = lineData[1];
  2240. this.isLowerRange = false;
  2241. } else {
  2242. this.isOOB = false;
  2243. this.rangeLow = lineData[0];
  2244. this.prefixLength = lineData[1];
  2245. this.rangeLength = lineData[2];
  2246. this.prefixCode = lineData[3];
  2247. this.isLowerRange = lineData[4] === "lower";
  2248. }
  2249. }
  2250. }
  2251. class HuffmanTreeNode {
  2252. constructor(line) {
  2253. this.children = [];
  2254. if (line) {
  2255. this.isLeaf = true;
  2256. this.rangeLength = line.rangeLength;
  2257. this.rangeLow = line.rangeLow;
  2258. this.isLowerRange = line.isLowerRange;
  2259. this.isOOB = line.isOOB;
  2260. } else {
  2261. this.isLeaf = false;
  2262. }
  2263. }
  2264. buildTree(line, shift) {
  2265. const bit = line.prefixCode >> shift & 1;
  2266. if (shift <= 0) {
  2267. this.children[bit] = new HuffmanTreeNode(line);
  2268. } else {
  2269. let node = this.children[bit];
  2270. if (!node) {
  2271. this.children[bit] = node = new HuffmanTreeNode(null);
  2272. }
  2273. node.buildTree(line, shift - 1);
  2274. }
  2275. }
  2276. decodeNode(reader) {
  2277. if (this.isLeaf) {
  2278. if (this.isOOB) {
  2279. return null;
  2280. }
  2281. const htOffset = reader.readBits(this.rangeLength);
  2282. return this.rangeLow + (this.isLowerRange ? -htOffset : htOffset);
  2283. }
  2284. const node = this.children[reader.readBit()];
  2285. if (!node) {
  2286. throw new Jbig2Error("invalid Huffman data");
  2287. }
  2288. return node.decodeNode(reader);
  2289. }
  2290. }
  2291. class HuffmanTable {
  2292. constructor(lines, prefixCodesDone) {
  2293. if (!prefixCodesDone) {
  2294. this.assignPrefixCodes(lines);
  2295. }
  2296. this.rootNode = new HuffmanTreeNode(null);
  2297. for (let i = 0, ii = lines.length; i < ii; i++) {
  2298. const line = lines[i];
  2299. if (line.prefixLength > 0) {
  2300. this.rootNode.buildTree(line, line.prefixLength - 1);
  2301. }
  2302. }
  2303. }
  2304. decode(reader) {
  2305. return this.rootNode.decodeNode(reader);
  2306. }
  2307. assignPrefixCodes(lines) {
  2308. const linesLength = lines.length;
  2309. let prefixLengthMax = 0;
  2310. for (let i = 0; i < linesLength; i++) {
  2311. prefixLengthMax = Math.max(prefixLengthMax, lines[i].prefixLength);
  2312. }
  2313. const histogram = new Uint32Array(prefixLengthMax + 1);
  2314. for (let i = 0; i < linesLength; i++) {
  2315. histogram[lines[i].prefixLength]++;
  2316. }
  2317. let currentLength = 1,
  2318. firstCode = 0,
  2319. currentCode,
  2320. currentTemp,
  2321. line;
  2322. histogram[0] = 0;
  2323. while (currentLength <= prefixLengthMax) {
  2324. firstCode = firstCode + histogram[currentLength - 1] << 1;
  2325. currentCode = firstCode;
  2326. currentTemp = 0;
  2327. while (currentTemp < linesLength) {
  2328. line = lines[currentTemp];
  2329. if (line.prefixLength === currentLength) {
  2330. line.prefixCode = currentCode;
  2331. currentCode++;
  2332. }
  2333. currentTemp++;
  2334. }
  2335. currentLength++;
  2336. }
  2337. }
  2338. }
  2339. function decodeTablesSegment(data, start, end) {
  2340. const flags = data[start];
  2341. const lowestValue = (0, _core_utils.readUint32)(data, start + 1) & 0xffffffff;
  2342. const highestValue = (0, _core_utils.readUint32)(data, start + 5) & 0xffffffff;
  2343. const reader = new Reader(data, start + 9, end);
  2344. const prefixSizeBits = (flags >> 1 & 7) + 1;
  2345. const rangeSizeBits = (flags >> 4 & 7) + 1;
  2346. const lines = [];
  2347. let prefixLength,
  2348. rangeLength,
  2349. currentRangeLow = lowestValue;
  2350. do {
  2351. prefixLength = reader.readBits(prefixSizeBits);
  2352. rangeLength = reader.readBits(rangeSizeBits);
  2353. lines.push(new HuffmanLine([currentRangeLow, prefixLength, rangeLength, 0]));
  2354. currentRangeLow += 1 << rangeLength;
  2355. } while (currentRangeLow < highestValue);
  2356. prefixLength = reader.readBits(prefixSizeBits);
  2357. lines.push(new HuffmanLine([lowestValue - 1, prefixLength, 32, 0, "lower"]));
  2358. prefixLength = reader.readBits(prefixSizeBits);
  2359. lines.push(new HuffmanLine([highestValue, prefixLength, 32, 0]));
  2360. if (flags & 1) {
  2361. prefixLength = reader.readBits(prefixSizeBits);
  2362. lines.push(new HuffmanLine([prefixLength, 0]));
  2363. }
  2364. return new HuffmanTable(lines, false);
  2365. }
  2366. const standardTablesCache = {};
  2367. function getStandardTable(number) {
  2368. let table = standardTablesCache[number];
  2369. if (table) {
  2370. return table;
  2371. }
  2372. let lines;
  2373. switch (number) {
  2374. case 1:
  2375. lines = [[0, 1, 4, 0x0], [16, 2, 8, 0x2], [272, 3, 16, 0x6], [65808, 3, 32, 0x7]];
  2376. break;
  2377. case 2:
  2378. lines = [[0, 1, 0, 0x0], [1, 2, 0, 0x2], [2, 3, 0, 0x6], [3, 4, 3, 0xe], [11, 5, 6, 0x1e], [75, 6, 32, 0x3e], [6, 0x3f]];
  2379. break;
  2380. case 3:
  2381. lines = [[-256, 8, 8, 0xfe], [0, 1, 0, 0x0], [1, 2, 0, 0x2], [2, 3, 0, 0x6], [3, 4, 3, 0xe], [11, 5, 6, 0x1e], [-257, 8, 32, 0xff, "lower"], [75, 7, 32, 0x7e], [6, 0x3e]];
  2382. break;
  2383. case 4:
  2384. lines = [[1, 1, 0, 0x0], [2, 2, 0, 0x2], [3, 3, 0, 0x6], [4, 4, 3, 0xe], [12, 5, 6, 0x1e], [76, 5, 32, 0x1f]];
  2385. break;
  2386. case 5:
  2387. lines = [[-255, 7, 8, 0x7e], [1, 1, 0, 0x0], [2, 2, 0, 0x2], [3, 3, 0, 0x6], [4, 4, 3, 0xe], [12, 5, 6, 0x1e], [-256, 7, 32, 0x7f, "lower"], [76, 6, 32, 0x3e]];
  2388. break;
  2389. case 6:
  2390. lines = [[-2048, 5, 10, 0x1c], [-1024, 4, 9, 0x8], [-512, 4, 8, 0x9], [-256, 4, 7, 0xa], [-128, 5, 6, 0x1d], [-64, 5, 5, 0x1e], [-32, 4, 5, 0xb], [0, 2, 7, 0x0], [128, 3, 7, 0x2], [256, 3, 8, 0x3], [512, 4, 9, 0xc], [1024, 4, 10, 0xd], [-2049, 6, 32, 0x3e, "lower"], [2048, 6, 32, 0x3f]];
  2391. break;
  2392. case 7:
  2393. lines = [[-1024, 4, 9, 0x8], [-512, 3, 8, 0x0], [-256, 4, 7, 0x9], [-128, 5, 6, 0x1a], [-64, 5, 5, 0x1b], [-32, 4, 5, 0xa], [0, 4, 5, 0xb], [32, 5, 5, 0x1c], [64, 5, 6, 0x1d], [128, 4, 7, 0xc], [256, 3, 8, 0x1], [512, 3, 9, 0x2], [1024, 3, 10, 0x3], [-1025, 5, 32, 0x1e, "lower"], [2048, 5, 32, 0x1f]];
  2394. break;
  2395. case 8:
  2396. lines = [[-15, 8, 3, 0xfc], [-7, 9, 1, 0x1fc], [-5, 8, 1, 0xfd], [-3, 9, 0, 0x1fd], [-2, 7, 0, 0x7c], [-1, 4, 0, 0xa], [0, 2, 1, 0x0], [2, 5, 0, 0x1a], [3, 6, 0, 0x3a], [4, 3, 4, 0x4], [20, 6, 1, 0x3b], [22, 4, 4, 0xb], [38, 4, 5, 0xc], [70, 5, 6, 0x1b], [134, 5, 7, 0x1c], [262, 6, 7, 0x3c], [390, 7, 8, 0x7d], [646, 6, 10, 0x3d], [-16, 9, 32, 0x1fe, "lower"], [1670, 9, 32, 0x1ff], [2, 0x1]];
  2397. break;
  2398. case 9:
  2399. lines = [[-31, 8, 4, 0xfc], [-15, 9, 2, 0x1fc], [-11, 8, 2, 0xfd], [-7, 9, 1, 0x1fd], [-5, 7, 1, 0x7c], [-3, 4, 1, 0xa], [-1, 3, 1, 0x2], [1, 3, 1, 0x3], [3, 5, 1, 0x1a], [5, 6, 1, 0x3a], [7, 3, 5, 0x4], [39, 6, 2, 0x3b], [43, 4, 5, 0xb], [75, 4, 6, 0xc], [139, 5, 7, 0x1b], [267, 5, 8, 0x1c], [523, 6, 8, 0x3c], [779, 7, 9, 0x7d], [1291, 6, 11, 0x3d], [-32, 9, 32, 0x1fe, "lower"], [3339, 9, 32, 0x1ff], [2, 0x0]];
  2400. break;
  2401. case 10:
  2402. lines = [[-21, 7, 4, 0x7a], [-5, 8, 0, 0xfc], [-4, 7, 0, 0x7b], [-3, 5, 0, 0x18], [-2, 2, 2, 0x0], [2, 5, 0, 0x19], [3, 6, 0, 0x36], [4, 7, 0, 0x7c], [5, 8, 0, 0xfd], [6, 2, 6, 0x1], [70, 5, 5, 0x1a], [102, 6, 5, 0x37], [134, 6, 6, 0x38], [198, 6, 7, 0x39], [326, 6, 8, 0x3a], [582, 6, 9, 0x3b], [1094, 6, 10, 0x3c], [2118, 7, 11, 0x7d], [-22, 8, 32, 0xfe, "lower"], [4166, 8, 32, 0xff], [2, 0x2]];
  2403. break;
  2404. case 11:
  2405. lines = [[1, 1, 0, 0x0], [2, 2, 1, 0x2], [4, 4, 0, 0xc], [5, 4, 1, 0xd], [7, 5, 1, 0x1c], [9, 5, 2, 0x1d], [13, 6, 2, 0x3c], [17, 7, 2, 0x7a], [21, 7, 3, 0x7b], [29, 7, 4, 0x7c], [45, 7, 5, 0x7d], [77, 7, 6, 0x7e], [141, 7, 32, 0x7f]];
  2406. break;
  2407. case 12:
  2408. lines = [[1, 1, 0, 0x0], [2, 2, 0, 0x2], [3, 3, 1, 0x6], [5, 5, 0, 0x1c], [6, 5, 1, 0x1d], [8, 6, 1, 0x3c], [10, 7, 0, 0x7a], [11, 7, 1, 0x7b], [13, 7, 2, 0x7c], [17, 7, 3, 0x7d], [25, 7, 4, 0x7e], [41, 8, 5, 0xfe], [73, 8, 32, 0xff]];
  2409. break;
  2410. case 13:
  2411. lines = [[1, 1, 0, 0x0], [2, 3, 0, 0x4], [3, 4, 0, 0xc], [4, 5, 0, 0x1c], [5, 4, 1, 0xd], [7, 3, 3, 0x5], [15, 6, 1, 0x3a], [17, 6, 2, 0x3b], [21, 6, 3, 0x3c], [29, 6, 4, 0x3d], [45, 6, 5, 0x3e], [77, 7, 6, 0x7e], [141, 7, 32, 0x7f]];
  2412. break;
  2413. case 14:
  2414. lines = [[-2, 3, 0, 0x4], [-1, 3, 0, 0x5], [0, 1, 0, 0x0], [1, 3, 0, 0x6], [2, 3, 0, 0x7]];
  2415. break;
  2416. case 15:
  2417. lines = [[-24, 7, 4, 0x7c], [-8, 6, 2, 0x3c], [-4, 5, 1, 0x1c], [-2, 4, 0, 0xc], [-1, 3, 0, 0x4], [0, 1, 0, 0x0], [1, 3, 0, 0x5], [2, 4, 0, 0xd], [3, 5, 1, 0x1d], [5, 6, 2, 0x3d], [9, 7, 4, 0x7d], [-25, 7, 32, 0x7e, "lower"], [25, 7, 32, 0x7f]];
  2418. break;
  2419. default:
  2420. throw new Jbig2Error(`standard table B.${number} does not exist`);
  2421. }
  2422. for (let i = 0, ii = lines.length; i < ii; i++) {
  2423. lines[i] = new HuffmanLine(lines[i]);
  2424. }
  2425. table = new HuffmanTable(lines, true);
  2426. standardTablesCache[number] = table;
  2427. return table;
  2428. }
  2429. class Reader {
  2430. constructor(data, start, end) {
  2431. this.data = data;
  2432. this.start = start;
  2433. this.end = end;
  2434. this.position = start;
  2435. this.shift = -1;
  2436. this.currentByte = 0;
  2437. }
  2438. readBit() {
  2439. if (this.shift < 0) {
  2440. if (this.position >= this.end) {
  2441. throw new Jbig2Error("end of data while reading bit");
  2442. }
  2443. this.currentByte = this.data[this.position++];
  2444. this.shift = 7;
  2445. }
  2446. const bit = this.currentByte >> this.shift & 1;
  2447. this.shift--;
  2448. return bit;
  2449. }
  2450. readBits(numBits) {
  2451. let result = 0,
  2452. i;
  2453. for (i = numBits - 1; i >= 0; i--) {
  2454. result |= this.readBit() << i;
  2455. }
  2456. return result;
  2457. }
  2458. byteAlign() {
  2459. this.shift = -1;
  2460. }
  2461. next() {
  2462. if (this.position >= this.end) {
  2463. return -1;
  2464. }
  2465. return this.data[this.position++];
  2466. }
  2467. }
  2468. function getCustomHuffmanTable(index, referredTo, customTables) {
  2469. let currentIndex = 0;
  2470. for (let i = 0, ii = referredTo.length; i < ii; i++) {
  2471. const table = customTables[referredTo[i]];
  2472. if (table) {
  2473. if (index === currentIndex) {
  2474. return table;
  2475. }
  2476. currentIndex++;
  2477. }
  2478. }
  2479. throw new Jbig2Error("can't find custom Huffman table");
  2480. }
  2481. function getTextRegionHuffmanTables(textRegion, referredTo, customTables, numberOfSymbols, reader) {
  2482. const codes = [];
  2483. for (let i = 0; i <= 34; i++) {
  2484. const codeLength = reader.readBits(4);
  2485. codes.push(new HuffmanLine([i, codeLength, 0, 0]));
  2486. }
  2487. const runCodesTable = new HuffmanTable(codes, false);
  2488. codes.length = 0;
  2489. for (let i = 0; i < numberOfSymbols;) {
  2490. const codeLength = runCodesTable.decode(reader);
  2491. if (codeLength >= 32) {
  2492. let repeatedLength, numberOfRepeats, j;
  2493. switch (codeLength) {
  2494. case 32:
  2495. if (i === 0) {
  2496. throw new Jbig2Error("no previous value in symbol ID table");
  2497. }
  2498. numberOfRepeats = reader.readBits(2) + 3;
  2499. repeatedLength = codes[i - 1].prefixLength;
  2500. break;
  2501. case 33:
  2502. numberOfRepeats = reader.readBits(3) + 3;
  2503. repeatedLength = 0;
  2504. break;
  2505. case 34:
  2506. numberOfRepeats = reader.readBits(7) + 11;
  2507. repeatedLength = 0;
  2508. break;
  2509. default:
  2510. throw new Jbig2Error("invalid code length in symbol ID table");
  2511. }
  2512. for (j = 0; j < numberOfRepeats; j++) {
  2513. codes.push(new HuffmanLine([i, repeatedLength, 0, 0]));
  2514. i++;
  2515. }
  2516. } else {
  2517. codes.push(new HuffmanLine([i, codeLength, 0, 0]));
  2518. i++;
  2519. }
  2520. }
  2521. reader.byteAlign();
  2522. const symbolIDTable = new HuffmanTable(codes, false);
  2523. let customIndex = 0,
  2524. tableFirstS,
  2525. tableDeltaS,
  2526. tableDeltaT;
  2527. switch (textRegion.huffmanFS) {
  2528. case 0:
  2529. case 1:
  2530. tableFirstS = getStandardTable(textRegion.huffmanFS + 6);
  2531. break;
  2532. case 3:
  2533. tableFirstS = getCustomHuffmanTable(customIndex, referredTo, customTables);
  2534. customIndex++;
  2535. break;
  2536. default:
  2537. throw new Jbig2Error("invalid Huffman FS selector");
  2538. }
  2539. switch (textRegion.huffmanDS) {
  2540. case 0:
  2541. case 1:
  2542. case 2:
  2543. tableDeltaS = getStandardTable(textRegion.huffmanDS + 8);
  2544. break;
  2545. case 3:
  2546. tableDeltaS = getCustomHuffmanTable(customIndex, referredTo, customTables);
  2547. customIndex++;
  2548. break;
  2549. default:
  2550. throw new Jbig2Error("invalid Huffman DS selector");
  2551. }
  2552. switch (textRegion.huffmanDT) {
  2553. case 0:
  2554. case 1:
  2555. case 2:
  2556. tableDeltaT = getStandardTable(textRegion.huffmanDT + 11);
  2557. break;
  2558. case 3:
  2559. tableDeltaT = getCustomHuffmanTable(customIndex, referredTo, customTables);
  2560. customIndex++;
  2561. break;
  2562. default:
  2563. throw new Jbig2Error("invalid Huffman DT selector");
  2564. }
  2565. if (textRegion.refinement) {
  2566. throw new Jbig2Error("refinement with Huffman is not supported");
  2567. }
  2568. return {
  2569. symbolIDTable,
  2570. tableFirstS,
  2571. tableDeltaS,
  2572. tableDeltaT
  2573. };
  2574. }
  2575. function getSymbolDictionaryHuffmanTables(dictionary, referredTo, customTables) {
  2576. let customIndex = 0,
  2577. tableDeltaHeight,
  2578. tableDeltaWidth;
  2579. switch (dictionary.huffmanDHSelector) {
  2580. case 0:
  2581. case 1:
  2582. tableDeltaHeight = getStandardTable(dictionary.huffmanDHSelector + 4);
  2583. break;
  2584. case 3:
  2585. tableDeltaHeight = getCustomHuffmanTable(customIndex, referredTo, customTables);
  2586. customIndex++;
  2587. break;
  2588. default:
  2589. throw new Jbig2Error("invalid Huffman DH selector");
  2590. }
  2591. switch (dictionary.huffmanDWSelector) {
  2592. case 0:
  2593. case 1:
  2594. tableDeltaWidth = getStandardTable(dictionary.huffmanDWSelector + 2);
  2595. break;
  2596. case 3:
  2597. tableDeltaWidth = getCustomHuffmanTable(customIndex, referredTo, customTables);
  2598. customIndex++;
  2599. break;
  2600. default:
  2601. throw new Jbig2Error("invalid Huffman DW selector");
  2602. }
  2603. let tableBitmapSize, tableAggregateInstances;
  2604. if (dictionary.bitmapSizeSelector) {
  2605. tableBitmapSize = getCustomHuffmanTable(customIndex, referredTo, customTables);
  2606. customIndex++;
  2607. } else {
  2608. tableBitmapSize = getStandardTable(1);
  2609. }
  2610. if (dictionary.aggregationInstancesSelector) {
  2611. tableAggregateInstances = getCustomHuffmanTable(customIndex, referredTo, customTables);
  2612. } else {
  2613. tableAggregateInstances = getStandardTable(1);
  2614. }
  2615. return {
  2616. tableDeltaHeight,
  2617. tableDeltaWidth,
  2618. tableBitmapSize,
  2619. tableAggregateInstances
  2620. };
  2621. }
  2622. function readUncompressedBitmap(reader, width, height) {
  2623. const bitmap = [];
  2624. for (let y = 0; y < height; y++) {
  2625. const row = new Uint8Array(width);
  2626. bitmap.push(row);
  2627. for (let x = 0; x < width; x++) {
  2628. row[x] = reader.readBit();
  2629. }
  2630. reader.byteAlign();
  2631. }
  2632. return bitmap;
  2633. }
  2634. function decodeMMRBitmap(input, width, height, endOfBlock) {
  2635. const params = {
  2636. K: -1,
  2637. Columns: width,
  2638. Rows: height,
  2639. BlackIs1: true,
  2640. EndOfBlock: endOfBlock
  2641. };
  2642. const decoder = new _ccitt.CCITTFaxDecoder(input, params);
  2643. const bitmap = [];
  2644. let currentByte,
  2645. eof = false;
  2646. for (let y = 0; y < height; y++) {
  2647. const row = new Uint8Array(width);
  2648. bitmap.push(row);
  2649. let shift = -1;
  2650. for (let x = 0; x < width; x++) {
  2651. if (shift < 0) {
  2652. currentByte = decoder.readNextChar();
  2653. if (currentByte === -1) {
  2654. currentByte = 0;
  2655. eof = true;
  2656. }
  2657. shift = 7;
  2658. }
  2659. row[x] = currentByte >> shift & 1;
  2660. shift--;
  2661. }
  2662. }
  2663. if (endOfBlock && !eof) {
  2664. const lookForEOFLimit = 5;
  2665. for (let i = 0; i < lookForEOFLimit; i++) {
  2666. if (decoder.readNextChar() === -1) {
  2667. break;
  2668. }
  2669. }
  2670. }
  2671. return bitmap;
  2672. }
  2673. class Jbig2Image {
  2674. parseChunks(chunks) {
  2675. return parseJbig2Chunks(chunks);
  2676. }
  2677. parse(data) {
  2678. const {
  2679. imgData,
  2680. width,
  2681. height
  2682. } = parseJbig2(data);
  2683. this.width = width;
  2684. this.height = height;
  2685. return imgData;
  2686. }
  2687. }
  2688. exports.Jbig2Image = Jbig2Image;
  2689. /***/ }),
  2690. /* 3 */
  2691. /***/ ((__unused_webpack_module, exports, __w_pdfjs_require__) => {
  2692. Object.defineProperty(exports, "__esModule", ({
  2693. value: true
  2694. }));
  2695. exports.XRefParseException = exports.XRefEntryException = exports.ParserEOFException = exports.PDF_VERSION_REGEXP = exports.MissingDataException = exports.DocStats = void 0;
  2696. exports.collectActions = collectActions;
  2697. exports.encodeToXmlString = encodeToXmlString;
  2698. exports.escapePDFName = escapePDFName;
  2699. exports.escapeString = escapeString;
  2700. exports.getArrayLookupTableFactory = getArrayLookupTableFactory;
  2701. exports.getInheritableProperty = getInheritableProperty;
  2702. exports.getLookupTableFactory = getLookupTableFactory;
  2703. exports.getNewAnnotationsMap = getNewAnnotationsMap;
  2704. exports.getRotationMatrix = getRotationMatrix;
  2705. exports.isAscii = isAscii;
  2706. exports.isWhiteSpace = isWhiteSpace;
  2707. exports.log2 = log2;
  2708. exports.numberToString = numberToString;
  2709. exports.parseXFAPath = parseXFAPath;
  2710. exports.readInt8 = readInt8;
  2711. exports.readUint16 = readUint16;
  2712. exports.readUint32 = readUint32;
  2713. exports.recoverJsURL = recoverJsURL;
  2714. exports.stringToUTF16HexString = stringToUTF16HexString;
  2715. exports.stringToUTF16String = stringToUTF16String;
  2716. exports.toRomanNumerals = toRomanNumerals;
  2717. exports.validateCSSFont = validateCSSFont;
  2718. var _util = __w_pdfjs_require__(1);
  2719. var _primitives = __w_pdfjs_require__(4);
  2720. var _base_stream = __w_pdfjs_require__(5);
  2721. const PDF_VERSION_REGEXP = /^[1-9]\.\d$/;
  2722. exports.PDF_VERSION_REGEXP = PDF_VERSION_REGEXP;
  2723. function getLookupTableFactory(initializer) {
  2724. let lookup;
  2725. return function () {
  2726. if (initializer) {
  2727. lookup = Object.create(null);
  2728. initializer(lookup);
  2729. initializer = null;
  2730. }
  2731. return lookup;
  2732. };
  2733. }
  2734. function getArrayLookupTableFactory(initializer) {
  2735. let lookup;
  2736. return function () {
  2737. if (initializer) {
  2738. let arr = initializer();
  2739. initializer = null;
  2740. lookup = Object.create(null);
  2741. for (let i = 0, ii = arr.length; i < ii; i += 2) {
  2742. lookup[arr[i]] = arr[i + 1];
  2743. }
  2744. arr = null;
  2745. }
  2746. return lookup;
  2747. };
  2748. }
  2749. class MissingDataException extends _util.BaseException {
  2750. constructor(begin, end) {
  2751. super(`Missing data [${begin}, ${end})`, "MissingDataException");
  2752. this.begin = begin;
  2753. this.end = end;
  2754. }
  2755. }
  2756. exports.MissingDataException = MissingDataException;
  2757. class ParserEOFException extends _util.BaseException {
  2758. constructor(msg) {
  2759. super(msg, "ParserEOFException");
  2760. }
  2761. }
  2762. exports.ParserEOFException = ParserEOFException;
  2763. class XRefEntryException extends _util.BaseException {
  2764. constructor(msg) {
  2765. super(msg, "XRefEntryException");
  2766. }
  2767. }
  2768. exports.XRefEntryException = XRefEntryException;
  2769. class XRefParseException extends _util.BaseException {
  2770. constructor(msg) {
  2771. super(msg, "XRefParseException");
  2772. }
  2773. }
  2774. exports.XRefParseException = XRefParseException;
  2775. class DocStats {
  2776. constructor(handler) {
  2777. this._handler = handler;
  2778. this._streamTypes = new Set();
  2779. this._fontTypes = new Set();
  2780. }
  2781. _send() {
  2782. const streamTypes = Object.create(null),
  2783. fontTypes = Object.create(null);
  2784. for (const type of this._streamTypes) {
  2785. streamTypes[type] = true;
  2786. }
  2787. for (const type of this._fontTypes) {
  2788. fontTypes[type] = true;
  2789. }
  2790. this._handler.send("DocStats", {
  2791. streamTypes,
  2792. fontTypes
  2793. });
  2794. }
  2795. addStreamType(type) {
  2796. if (this._streamTypes.has(type)) {
  2797. return;
  2798. }
  2799. this._streamTypes.add(type);
  2800. this._send();
  2801. }
  2802. addFontType(type) {
  2803. if (this._fontTypes.has(type)) {
  2804. return;
  2805. }
  2806. this._fontTypes.add(type);
  2807. this._send();
  2808. }
  2809. }
  2810. exports.DocStats = DocStats;
  2811. function getInheritableProperty({
  2812. dict,
  2813. key,
  2814. getArray = false,
  2815. stopWhenFound = true
  2816. }) {
  2817. let values;
  2818. const visited = new _primitives.RefSet();
  2819. while (dict instanceof _primitives.Dict && !(dict.objId && visited.has(dict.objId))) {
  2820. if (dict.objId) {
  2821. visited.put(dict.objId);
  2822. }
  2823. const value = getArray ? dict.getArray(key) : dict.get(key);
  2824. if (value !== undefined) {
  2825. if (stopWhenFound) {
  2826. return value;
  2827. }
  2828. if (!values) {
  2829. values = [];
  2830. }
  2831. values.push(value);
  2832. }
  2833. dict = dict.get("Parent");
  2834. }
  2835. return values;
  2836. }
  2837. const ROMAN_NUMBER_MAP = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
  2838. function toRomanNumerals(number, lowerCase = false) {
  2839. (0, _util.assert)(Number.isInteger(number) && number > 0, "The number should be a positive integer.");
  2840. const romanBuf = [];
  2841. let pos;
  2842. while (number >= 1000) {
  2843. number -= 1000;
  2844. romanBuf.push("M");
  2845. }
  2846. pos = number / 100 | 0;
  2847. number %= 100;
  2848. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  2849. pos = number / 10 | 0;
  2850. number %= 10;
  2851. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  2852. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  2853. const romanStr = romanBuf.join("");
  2854. return lowerCase ? romanStr.toLowerCase() : romanStr;
  2855. }
  2856. function log2(x) {
  2857. if (x <= 0) {
  2858. return 0;
  2859. }
  2860. return Math.ceil(Math.log2(x));
  2861. }
  2862. function readInt8(data, offset) {
  2863. return data[offset] << 24 >> 24;
  2864. }
  2865. function readUint16(data, offset) {
  2866. return data[offset] << 8 | data[offset + 1];
  2867. }
  2868. function readUint32(data, offset) {
  2869. return (data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]) >>> 0;
  2870. }
  2871. function isWhiteSpace(ch) {
  2872. return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
  2873. }
  2874. function parseXFAPath(path) {
  2875. const positionPattern = /(.+)\[(\d+)\]$/;
  2876. return path.split(".").map(component => {
  2877. const m = component.match(positionPattern);
  2878. if (m) {
  2879. return {
  2880. name: m[1],
  2881. pos: parseInt(m[2], 10)
  2882. };
  2883. }
  2884. return {
  2885. name: component,
  2886. pos: 0
  2887. };
  2888. });
  2889. }
  2890. function escapePDFName(str) {
  2891. const buffer = [];
  2892. let start = 0;
  2893. for (let i = 0, ii = str.length; i < ii; i++) {
  2894. const char = str.charCodeAt(i);
  2895. if (char < 0x21 || char > 0x7e || char === 0x23 || char === 0x28 || char === 0x29 || char === 0x3c || char === 0x3e || char === 0x5b || char === 0x5d || char === 0x7b || char === 0x7d || char === 0x2f || char === 0x25) {
  2896. if (start < i) {
  2897. buffer.push(str.substring(start, i));
  2898. }
  2899. buffer.push(`#${char.toString(16)}`);
  2900. start = i + 1;
  2901. }
  2902. }
  2903. if (buffer.length === 0) {
  2904. return str;
  2905. }
  2906. if (start < str.length) {
  2907. buffer.push(str.substring(start, str.length));
  2908. }
  2909. return buffer.join("");
  2910. }
  2911. function escapeString(str) {
  2912. return str.replace(/([()\\\n\r])/g, match => {
  2913. if (match === "\n") {
  2914. return "\\n";
  2915. } else if (match === "\r") {
  2916. return "\\r";
  2917. }
  2918. return `\\${match}`;
  2919. });
  2920. }
  2921. function _collectJS(entry, xref, list, parents) {
  2922. if (!entry) {
  2923. return;
  2924. }
  2925. let parent = null;
  2926. if (entry instanceof _primitives.Ref) {
  2927. if (parents.has(entry)) {
  2928. return;
  2929. }
  2930. parent = entry;
  2931. parents.put(parent);
  2932. entry = xref.fetch(entry);
  2933. }
  2934. if (Array.isArray(entry)) {
  2935. for (const element of entry) {
  2936. _collectJS(element, xref, list, parents);
  2937. }
  2938. } else if (entry instanceof _primitives.Dict) {
  2939. if ((0, _primitives.isName)(entry.get("S"), "JavaScript")) {
  2940. const js = entry.get("JS");
  2941. let code;
  2942. if (js instanceof _base_stream.BaseStream) {
  2943. code = js.getString();
  2944. } else if (typeof js === "string") {
  2945. code = js;
  2946. }
  2947. code = code && (0, _util.stringToPDFString)(code).replace(/\u0000/g, "");
  2948. if (code) {
  2949. list.push(code);
  2950. }
  2951. }
  2952. _collectJS(entry.getRaw("Next"), xref, list, parents);
  2953. }
  2954. if (parent) {
  2955. parents.remove(parent);
  2956. }
  2957. }
  2958. function collectActions(xref, dict, eventType) {
  2959. const actions = Object.create(null);
  2960. const additionalActionsDicts = getInheritableProperty({
  2961. dict,
  2962. key: "AA",
  2963. stopWhenFound: false
  2964. });
  2965. if (additionalActionsDicts) {
  2966. for (let i = additionalActionsDicts.length - 1; i >= 0; i--) {
  2967. const additionalActions = additionalActionsDicts[i];
  2968. if (!(additionalActions instanceof _primitives.Dict)) {
  2969. continue;
  2970. }
  2971. for (const key of additionalActions.getKeys()) {
  2972. const action = eventType[key];
  2973. if (!action) {
  2974. continue;
  2975. }
  2976. const actionDict = additionalActions.getRaw(key);
  2977. const parents = new _primitives.RefSet();
  2978. const list = [];
  2979. _collectJS(actionDict, xref, list, parents);
  2980. if (list.length > 0) {
  2981. actions[action] = list;
  2982. }
  2983. }
  2984. }
  2985. }
  2986. if (dict.has("A")) {
  2987. const actionDict = dict.get("A");
  2988. const parents = new _primitives.RefSet();
  2989. const list = [];
  2990. _collectJS(actionDict, xref, list, parents);
  2991. if (list.length > 0) {
  2992. actions.Action = list;
  2993. }
  2994. }
  2995. return (0, _util.objectSize)(actions) > 0 ? actions : null;
  2996. }
  2997. const XMLEntities = {
  2998. 0x3c: "&lt;",
  2999. 0x3e: "&gt;",
  3000. 0x26: "&amp;",
  3001. 0x22: "&quot;",
  3002. 0x27: "&apos;"
  3003. };
  3004. function encodeToXmlString(str) {
  3005. const buffer = [];
  3006. let start = 0;
  3007. for (let i = 0, ii = str.length; i < ii; i++) {
  3008. const char = str.codePointAt(i);
  3009. if (0x20 <= char && char <= 0x7e) {
  3010. const entity = XMLEntities[char];
  3011. if (entity) {
  3012. if (start < i) {
  3013. buffer.push(str.substring(start, i));
  3014. }
  3015. buffer.push(entity);
  3016. start = i + 1;
  3017. }
  3018. } else {
  3019. if (start < i) {
  3020. buffer.push(str.substring(start, i));
  3021. }
  3022. buffer.push(`&#x${char.toString(16).toUpperCase()};`);
  3023. if (char > 0xd7ff && (char < 0xe000 || char > 0xfffd)) {
  3024. i++;
  3025. }
  3026. start = i + 1;
  3027. }
  3028. }
  3029. if (buffer.length === 0) {
  3030. return str;
  3031. }
  3032. if (start < str.length) {
  3033. buffer.push(str.substring(start, str.length));
  3034. }
  3035. return buffer.join("");
  3036. }
  3037. function validateCSSFont(cssFontInfo) {
  3038. const DEFAULT_CSS_FONT_OBLIQUE = "14";
  3039. const DEFAULT_CSS_FONT_WEIGHT = "400";
  3040. const CSS_FONT_WEIGHT_VALUES = new Set(["100", "200", "300", "400", "500", "600", "700", "800", "900", "1000", "normal", "bold", "bolder", "lighter"]);
  3041. const {
  3042. fontFamily,
  3043. fontWeight,
  3044. italicAngle
  3045. } = cssFontInfo;
  3046. if (/^".*"$/.test(fontFamily)) {
  3047. if (/[^\\]"/.test(fontFamily.slice(1, fontFamily.length - 1))) {
  3048. (0, _util.warn)(`XFA - FontFamily contains some unescaped ": ${fontFamily}.`);
  3049. return false;
  3050. }
  3051. } else if (/^'.*'$/.test(fontFamily)) {
  3052. if (/[^\\]'/.test(fontFamily.slice(1, fontFamily.length - 1))) {
  3053. (0, _util.warn)(`XFA - FontFamily contains some unescaped ': ${fontFamily}.`);
  3054. return false;
  3055. }
  3056. } else {
  3057. for (const ident of fontFamily.split(/[ \t]+/)) {
  3058. if (/^(\d|(-(\d|-)))/.test(ident) || !/^[\w-\\]+$/.test(ident)) {
  3059. (0, _util.warn)(`XFA - FontFamily contains some invalid <custom-ident>: ${fontFamily}.`);
  3060. return false;
  3061. }
  3062. }
  3063. }
  3064. const weight = fontWeight ? fontWeight.toString() : "";
  3065. cssFontInfo.fontWeight = CSS_FONT_WEIGHT_VALUES.has(weight) ? weight : DEFAULT_CSS_FONT_WEIGHT;
  3066. const angle = parseFloat(italicAngle);
  3067. cssFontInfo.italicAngle = isNaN(angle) || angle < -90 || angle > 90 ? DEFAULT_CSS_FONT_OBLIQUE : italicAngle.toString();
  3068. return true;
  3069. }
  3070. function recoverJsURL(str) {
  3071. const URL_OPEN_METHODS = ["app.launchURL", "window.open", "xfa.host.gotoURL"];
  3072. const regex = new RegExp("^\\s*(" + URL_OPEN_METHODS.join("|").split(".").join("\\.") + ")\\((?:'|\")([^'\"]*)(?:'|\")(?:,\\s*(\\w+)\\)|\\))", "i");
  3073. const jsUrl = regex.exec(str);
  3074. if (jsUrl && jsUrl[2]) {
  3075. const url = jsUrl[2];
  3076. let newWindow = false;
  3077. if (jsUrl[3] === "true" && jsUrl[1] === "app.launchURL") {
  3078. newWindow = true;
  3079. }
  3080. return {
  3081. url,
  3082. newWindow
  3083. };
  3084. }
  3085. return null;
  3086. }
  3087. function numberToString(value) {
  3088. if (Number.isInteger(value)) {
  3089. return value.toString();
  3090. }
  3091. const roundedValue = Math.round(value * 100);
  3092. if (roundedValue % 100 === 0) {
  3093. return (roundedValue / 100).toString();
  3094. }
  3095. if (roundedValue % 10 === 0) {
  3096. return value.toFixed(1);
  3097. }
  3098. return value.toFixed(2);
  3099. }
  3100. function getNewAnnotationsMap(annotationStorage) {
  3101. if (!annotationStorage) {
  3102. return null;
  3103. }
  3104. const newAnnotationsByPage = new Map();
  3105. for (const [key, value] of annotationStorage) {
  3106. if (!key.startsWith(_util.AnnotationEditorPrefix)) {
  3107. continue;
  3108. }
  3109. let annotations = newAnnotationsByPage.get(value.pageIndex);
  3110. if (!annotations) {
  3111. annotations = [];
  3112. newAnnotationsByPage.set(value.pageIndex, annotations);
  3113. }
  3114. annotations.push(value);
  3115. }
  3116. return newAnnotationsByPage.size > 0 ? newAnnotationsByPage : null;
  3117. }
  3118. function isAscii(str) {
  3119. return /^[\x00-\x7F]*$/.test(str);
  3120. }
  3121. function stringToUTF16HexString(str) {
  3122. const buf = [];
  3123. for (let i = 0, ii = str.length; i < ii; i++) {
  3124. const char = str.charCodeAt(i);
  3125. buf.push((char >> 8 & 0xff).toString(16).padStart(2, "0"), (char & 0xff).toString(16).padStart(2, "0"));
  3126. }
  3127. return buf.join("");
  3128. }
  3129. function stringToUTF16String(str, bigEndian = false) {
  3130. const buf = [];
  3131. if (bigEndian) {
  3132. buf.push("\xFE\xFF");
  3133. }
  3134. for (let i = 0, ii = str.length; i < ii; i++) {
  3135. const char = str.charCodeAt(i);
  3136. buf.push(String.fromCharCode(char >> 8 & 0xff), String.fromCharCode(char & 0xff));
  3137. }
  3138. return buf.join("");
  3139. }
  3140. function getRotationMatrix(rotation, width, height) {
  3141. switch (rotation) {
  3142. case 90:
  3143. return [0, 1, -1, 0, width, 0];
  3144. case 180:
  3145. return [-1, 0, 0, -1, width, height];
  3146. case 270:
  3147. return [0, -1, 1, 0, 0, height];
  3148. default:
  3149. throw new Error("Invalid rotation");
  3150. }
  3151. }
  3152. /***/ }),
  3153. /* 4 */
  3154. /***/ ((__unused_webpack_module, exports, __w_pdfjs_require__) => {
  3155. Object.defineProperty(exports, "__esModule", ({
  3156. value: true
  3157. }));
  3158. exports.RefSetCache = exports.RefSet = exports.Ref = exports.Name = exports.EOF = exports.Dict = exports.Cmd = exports.CIRCULAR_REF = void 0;
  3159. exports.clearPrimitiveCaches = clearPrimitiveCaches;
  3160. exports.isCmd = isCmd;
  3161. exports.isDict = isDict;
  3162. exports.isName = isName;
  3163. exports.isRefsEqual = isRefsEqual;
  3164. var _util = __w_pdfjs_require__(1);
  3165. const CIRCULAR_REF = Symbol("CIRCULAR_REF");
  3166. exports.CIRCULAR_REF = CIRCULAR_REF;
  3167. const EOF = Symbol("EOF");
  3168. exports.EOF = EOF;
  3169. const Name = function NameClosure() {
  3170. let nameCache = Object.create(null);
  3171. class Name {
  3172. constructor(name) {
  3173. this.name = name;
  3174. }
  3175. static get(name) {
  3176. return nameCache[name] || (nameCache[name] = new Name(name));
  3177. }
  3178. static _clearCache() {
  3179. nameCache = Object.create(null);
  3180. }
  3181. }
  3182. return Name;
  3183. }();
  3184. exports.Name = Name;
  3185. const Cmd = function CmdClosure() {
  3186. let cmdCache = Object.create(null);
  3187. class Cmd {
  3188. constructor(cmd) {
  3189. this.cmd = cmd;
  3190. }
  3191. static get(cmd) {
  3192. return cmdCache[cmd] || (cmdCache[cmd] = new Cmd(cmd));
  3193. }
  3194. static _clearCache() {
  3195. cmdCache = Object.create(null);
  3196. }
  3197. }
  3198. return Cmd;
  3199. }();
  3200. exports.Cmd = Cmd;
  3201. const nonSerializable = function nonSerializableClosure() {
  3202. return nonSerializable;
  3203. };
  3204. class Dict {
  3205. constructor(xref = null) {
  3206. this._map = Object.create(null);
  3207. this.xref = xref;
  3208. this.objId = null;
  3209. this.suppressEncryption = false;
  3210. this.__nonSerializable__ = nonSerializable;
  3211. }
  3212. assignXref(newXref) {
  3213. this.xref = newXref;
  3214. }
  3215. get size() {
  3216. return Object.keys(this._map).length;
  3217. }
  3218. get(key1, key2, key3) {
  3219. let value = this._map[key1];
  3220. if (value === undefined && key2 !== undefined) {
  3221. value = this._map[key2];
  3222. if (value === undefined && key3 !== undefined) {
  3223. value = this._map[key3];
  3224. }
  3225. }
  3226. if (value instanceof Ref && this.xref) {
  3227. return this.xref.fetch(value, this.suppressEncryption);
  3228. }
  3229. return value;
  3230. }
  3231. async getAsync(key1, key2, key3) {
  3232. let value = this._map[key1];
  3233. if (value === undefined && key2 !== undefined) {
  3234. value = this._map[key2];
  3235. if (value === undefined && key3 !== undefined) {
  3236. value = this._map[key3];
  3237. }
  3238. }
  3239. if (value instanceof Ref && this.xref) {
  3240. return this.xref.fetchAsync(value, this.suppressEncryption);
  3241. }
  3242. return value;
  3243. }
  3244. getArray(key1, key2, key3) {
  3245. let value = this._map[key1];
  3246. if (value === undefined && key2 !== undefined) {
  3247. value = this._map[key2];
  3248. if (value === undefined && key3 !== undefined) {
  3249. value = this._map[key3];
  3250. }
  3251. }
  3252. if (value instanceof Ref && this.xref) {
  3253. value = this.xref.fetch(value, this.suppressEncryption);
  3254. }
  3255. if (Array.isArray(value)) {
  3256. value = value.slice();
  3257. for (let i = 0, ii = value.length; i < ii; i++) {
  3258. if (value[i] instanceof Ref && this.xref) {
  3259. value[i] = this.xref.fetch(value[i], this.suppressEncryption);
  3260. }
  3261. }
  3262. }
  3263. return value;
  3264. }
  3265. getRaw(key) {
  3266. return this._map[key];
  3267. }
  3268. getKeys() {
  3269. return Object.keys(this._map);
  3270. }
  3271. getRawValues() {
  3272. return Object.values(this._map);
  3273. }
  3274. set(key, value) {
  3275. this._map[key] = value;
  3276. }
  3277. has(key) {
  3278. return this._map[key] !== undefined;
  3279. }
  3280. forEach(callback) {
  3281. for (const key in this._map) {
  3282. callback(key, this.get(key));
  3283. }
  3284. }
  3285. static get empty() {
  3286. const emptyDict = new Dict(null);
  3287. emptyDict.set = (key, value) => {
  3288. (0, _util.unreachable)("Should not call `set` on the empty dictionary.");
  3289. };
  3290. return (0, _util.shadow)(this, "empty", emptyDict);
  3291. }
  3292. static merge({
  3293. xref,
  3294. dictArray,
  3295. mergeSubDicts = false
  3296. }) {
  3297. const mergedDict = new Dict(xref),
  3298. properties = new Map();
  3299. for (const dict of dictArray) {
  3300. if (!(dict instanceof Dict)) {
  3301. continue;
  3302. }
  3303. for (const [key, value] of Object.entries(dict._map)) {
  3304. let property = properties.get(key);
  3305. if (property === undefined) {
  3306. property = [];
  3307. properties.set(key, property);
  3308. } else if (!mergeSubDicts || !(value instanceof Dict)) {
  3309. continue;
  3310. }
  3311. property.push(value);
  3312. }
  3313. }
  3314. for (const [name, values] of properties) {
  3315. if (values.length === 1 || !(values[0] instanceof Dict)) {
  3316. mergedDict._map[name] = values[0];
  3317. continue;
  3318. }
  3319. const subDict = new Dict(xref);
  3320. for (const dict of values) {
  3321. for (const [key, value] of Object.entries(dict._map)) {
  3322. if (subDict._map[key] === undefined) {
  3323. subDict._map[key] = value;
  3324. }
  3325. }
  3326. }
  3327. if (subDict.size > 0) {
  3328. mergedDict._map[name] = subDict;
  3329. }
  3330. }
  3331. properties.clear();
  3332. return mergedDict.size > 0 ? mergedDict : Dict.empty;
  3333. }
  3334. }
  3335. exports.Dict = Dict;
  3336. const Ref = function RefClosure() {
  3337. let refCache = Object.create(null);
  3338. class Ref {
  3339. constructor(num, gen) {
  3340. this.num = num;
  3341. this.gen = gen;
  3342. }
  3343. toString() {
  3344. if (this.gen === 0) {
  3345. return `${this.num}R`;
  3346. }
  3347. return `${this.num}R${this.gen}`;
  3348. }
  3349. static get(num, gen) {
  3350. const key = gen === 0 ? `${num}R` : `${num}R${gen}`;
  3351. return refCache[key] || (refCache[key] = new Ref(num, gen));
  3352. }
  3353. static _clearCache() {
  3354. refCache = Object.create(null);
  3355. }
  3356. }
  3357. return Ref;
  3358. }();
  3359. exports.Ref = Ref;
  3360. class RefSet {
  3361. constructor(parent = null) {
  3362. this._set = new Set(parent && parent._set);
  3363. }
  3364. has(ref) {
  3365. return this._set.has(ref.toString());
  3366. }
  3367. put(ref) {
  3368. this._set.add(ref.toString());
  3369. }
  3370. remove(ref) {
  3371. this._set.delete(ref.toString());
  3372. }
  3373. [Symbol.iterator]() {
  3374. return this._set.values();
  3375. }
  3376. clear() {
  3377. this._set.clear();
  3378. }
  3379. }
  3380. exports.RefSet = RefSet;
  3381. class RefSetCache {
  3382. constructor() {
  3383. this._map = new Map();
  3384. }
  3385. get size() {
  3386. return this._map.size;
  3387. }
  3388. get(ref) {
  3389. return this._map.get(ref.toString());
  3390. }
  3391. has(ref) {
  3392. return this._map.has(ref.toString());
  3393. }
  3394. put(ref, obj) {
  3395. this._map.set(ref.toString(), obj);
  3396. }
  3397. putAlias(ref, aliasRef) {
  3398. this._map.set(ref.toString(), this.get(aliasRef));
  3399. }
  3400. [Symbol.iterator]() {
  3401. return this._map.values();
  3402. }
  3403. clear() {
  3404. this._map.clear();
  3405. }
  3406. }
  3407. exports.RefSetCache = RefSetCache;
  3408. function isName(v, name) {
  3409. return v instanceof Name && (name === undefined || v.name === name);
  3410. }
  3411. function isCmd(v, cmd) {
  3412. return v instanceof Cmd && (cmd === undefined || v.cmd === cmd);
  3413. }
  3414. function isDict(v, type) {
  3415. return v instanceof Dict && (type === undefined || isName(v.get("Type"), type));
  3416. }
  3417. function isRefsEqual(v1, v2) {
  3418. return v1.num === v2.num && v1.gen === v2.gen;
  3419. }
  3420. function clearPrimitiveCaches() {
  3421. Cmd._clearCache();
  3422. Name._clearCache();
  3423. Ref._clearCache();
  3424. }
  3425. /***/ }),
  3426. /* 5 */
  3427. /***/ ((__unused_webpack_module, exports, __w_pdfjs_require__) => {
  3428. Object.defineProperty(exports, "__esModule", ({
  3429. value: true
  3430. }));
  3431. exports.BaseStream = void 0;
  3432. var _util = __w_pdfjs_require__(1);
  3433. class BaseStream {
  3434. constructor() {
  3435. if (this.constructor === BaseStream) {
  3436. (0, _util.unreachable)("Cannot initialize BaseStream.");
  3437. }
  3438. }
  3439. get length() {
  3440. (0, _util.unreachable)("Abstract getter `length` accessed");
  3441. }
  3442. get isEmpty() {
  3443. (0, _util.unreachable)("Abstract getter `isEmpty` accessed");
  3444. }
  3445. get isDataLoaded() {
  3446. return (0, _util.shadow)(this, "isDataLoaded", true);
  3447. }
  3448. getByte() {
  3449. (0, _util.unreachable)("Abstract method `getByte` called");
  3450. }
  3451. getBytes(length) {
  3452. (0, _util.unreachable)("Abstract method `getBytes` called");
  3453. }
  3454. peekByte() {
  3455. const peekedByte = this.getByte();
  3456. if (peekedByte !== -1) {
  3457. this.pos--;
  3458. }
  3459. return peekedByte;
  3460. }
  3461. peekBytes(length) {
  3462. const bytes = this.getBytes(length);
  3463. this.pos -= bytes.length;
  3464. return bytes;
  3465. }
  3466. getUint16() {
  3467. const b0 = this.getByte();
  3468. const b1 = this.getByte();
  3469. if (b0 === -1 || b1 === -1) {
  3470. return -1;
  3471. }
  3472. return (b0 << 8) + b1;
  3473. }
  3474. getInt32() {
  3475. const b0 = this.getByte();
  3476. const b1 = this.getByte();
  3477. const b2 = this.getByte();
  3478. const b3 = this.getByte();
  3479. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  3480. }
  3481. getByteRange(begin, end) {
  3482. (0, _util.unreachable)("Abstract method `getByteRange` called");
  3483. }
  3484. getString(length) {
  3485. return (0, _util.bytesToString)(this.getBytes(length));
  3486. }
  3487. skip(n) {
  3488. this.pos += n || 1;
  3489. }
  3490. reset() {
  3491. (0, _util.unreachable)("Abstract method `reset` called");
  3492. }
  3493. moveStart() {
  3494. (0, _util.unreachable)("Abstract method `moveStart` called");
  3495. }
  3496. makeSubStream(start, length, dict = null) {
  3497. (0, _util.unreachable)("Abstract method `makeSubStream` called");
  3498. }
  3499. getBaseStreams() {
  3500. return null;
  3501. }
  3502. }
  3503. exports.BaseStream = BaseStream;
  3504. /***/ }),
  3505. /* 6 */
  3506. /***/ ((__unused_webpack_module, exports) => {
  3507. Object.defineProperty(exports, "__esModule", ({
  3508. value: true
  3509. }));
  3510. exports.ArithmeticDecoder = void 0;
  3511. const QeTable = [{
  3512. qe: 0x5601,
  3513. nmps: 1,
  3514. nlps: 1,
  3515. switchFlag: 1
  3516. }, {
  3517. qe: 0x3401,
  3518. nmps: 2,
  3519. nlps: 6,
  3520. switchFlag: 0
  3521. }, {
  3522. qe: 0x1801,
  3523. nmps: 3,
  3524. nlps: 9,
  3525. switchFlag: 0
  3526. }, {
  3527. qe: 0x0ac1,
  3528. nmps: 4,
  3529. nlps: 12,
  3530. switchFlag: 0
  3531. }, {
  3532. qe: 0x0521,
  3533. nmps: 5,
  3534. nlps: 29,
  3535. switchFlag: 0
  3536. }, {
  3537. qe: 0x0221,
  3538. nmps: 38,
  3539. nlps: 33,
  3540. switchFlag: 0
  3541. }, {
  3542. qe: 0x5601,
  3543. nmps: 7,
  3544. nlps: 6,
  3545. switchFlag: 1
  3546. }, {
  3547. qe: 0x5401,
  3548. nmps: 8,
  3549. nlps: 14,
  3550. switchFlag: 0
  3551. }, {
  3552. qe: 0x4801,
  3553. nmps: 9,
  3554. nlps: 14,
  3555. switchFlag: 0
  3556. }, {
  3557. qe: 0x3801,
  3558. nmps: 10,
  3559. nlps: 14,
  3560. switchFlag: 0
  3561. }, {
  3562. qe: 0x3001,
  3563. nmps: 11,
  3564. nlps: 17,
  3565. switchFlag: 0
  3566. }, {
  3567. qe: 0x2401,
  3568. nmps: 12,
  3569. nlps: 18,
  3570. switchFlag: 0
  3571. }, {
  3572. qe: 0x1c01,
  3573. nmps: 13,
  3574. nlps: 20,
  3575. switchFlag: 0
  3576. }, {
  3577. qe: 0x1601,
  3578. nmps: 29,
  3579. nlps: 21,
  3580. switchFlag: 0
  3581. }, {
  3582. qe: 0x5601,
  3583. nmps: 15,
  3584. nlps: 14,
  3585. switchFlag: 1
  3586. }, {
  3587. qe: 0x5401,
  3588. nmps: 16,
  3589. nlps: 14,
  3590. switchFlag: 0
  3591. }, {
  3592. qe: 0x5101,
  3593. nmps: 17,
  3594. nlps: 15,
  3595. switchFlag: 0
  3596. }, {
  3597. qe: 0x4801,
  3598. nmps: 18,
  3599. nlps: 16,
  3600. switchFlag: 0
  3601. }, {
  3602. qe: 0x3801,
  3603. nmps: 19,
  3604. nlps: 17,
  3605. switchFlag: 0
  3606. }, {
  3607. qe: 0x3401,
  3608. nmps: 20,
  3609. nlps: 18,
  3610. switchFlag: 0
  3611. }, {
  3612. qe: 0x3001,
  3613. nmps: 21,
  3614. nlps: 19,
  3615. switchFlag: 0
  3616. }, {
  3617. qe: 0x2801,
  3618. nmps: 22,
  3619. nlps: 19,
  3620. switchFlag: 0
  3621. }, {
  3622. qe: 0x2401,
  3623. nmps: 23,
  3624. nlps: 20,
  3625. switchFlag: 0
  3626. }, {
  3627. qe: 0x2201,
  3628. nmps: 24,
  3629. nlps: 21,
  3630. switchFlag: 0
  3631. }, {
  3632. qe: 0x1c01,
  3633. nmps: 25,
  3634. nlps: 22,
  3635. switchFlag: 0
  3636. }, {
  3637. qe: 0x1801,
  3638. nmps: 26,
  3639. nlps: 23,
  3640. switchFlag: 0
  3641. }, {
  3642. qe: 0x1601,
  3643. nmps: 27,
  3644. nlps: 24,
  3645. switchFlag: 0
  3646. }, {
  3647. qe: 0x1401,
  3648. nmps: 28,
  3649. nlps: 25,
  3650. switchFlag: 0
  3651. }, {
  3652. qe: 0x1201,
  3653. nmps: 29,
  3654. nlps: 26,
  3655. switchFlag: 0
  3656. }, {
  3657. qe: 0x1101,
  3658. nmps: 30,
  3659. nlps: 27,
  3660. switchFlag: 0
  3661. }, {
  3662. qe: 0x0ac1,
  3663. nmps: 31,
  3664. nlps: 28,
  3665. switchFlag: 0
  3666. }, {
  3667. qe: 0x09c1,
  3668. nmps: 32,
  3669. nlps: 29,
  3670. switchFlag: 0
  3671. }, {
  3672. qe: 0x08a1,
  3673. nmps: 33,
  3674. nlps: 30,
  3675. switchFlag: 0
  3676. }, {
  3677. qe: 0x0521,
  3678. nmps: 34,
  3679. nlps: 31,
  3680. switchFlag: 0
  3681. }, {
  3682. qe: 0x0441,
  3683. nmps: 35,
  3684. nlps: 32,
  3685. switchFlag: 0
  3686. }, {
  3687. qe: 0x02a1,
  3688. nmps: 36,
  3689. nlps: 33,
  3690. switchFlag: 0
  3691. }, {
  3692. qe: 0x0221,
  3693. nmps: 37,
  3694. nlps: 34,
  3695. switchFlag: 0
  3696. }, {
  3697. qe: 0x0141,
  3698. nmps: 38,
  3699. nlps: 35,
  3700. switchFlag: 0
  3701. }, {
  3702. qe: 0x0111,
  3703. nmps: 39,
  3704. nlps: 36,
  3705. switchFlag: 0
  3706. }, {
  3707. qe: 0x0085,
  3708. nmps: 40,
  3709. nlps: 37,
  3710. switchFlag: 0
  3711. }, {
  3712. qe: 0x0049,
  3713. nmps: 41,
  3714. nlps: 38,
  3715. switchFlag: 0
  3716. }, {
  3717. qe: 0x0025,
  3718. nmps: 42,
  3719. nlps: 39,
  3720. switchFlag: 0
  3721. }, {
  3722. qe: 0x0015,
  3723. nmps: 43,
  3724. nlps: 40,
  3725. switchFlag: 0
  3726. }, {
  3727. qe: 0x0009,
  3728. nmps: 44,
  3729. nlps: 41,
  3730. switchFlag: 0
  3731. }, {
  3732. qe: 0x0005,
  3733. nmps: 45,
  3734. nlps: 42,
  3735. switchFlag: 0
  3736. }, {
  3737. qe: 0x0001,
  3738. nmps: 45,
  3739. nlps: 43,
  3740. switchFlag: 0
  3741. }, {
  3742. qe: 0x5601,
  3743. nmps: 46,
  3744. nlps: 46,
  3745. switchFlag: 0
  3746. }];
  3747. class ArithmeticDecoder {
  3748. constructor(data, start, end) {
  3749. this.data = data;
  3750. this.bp = start;
  3751. this.dataEnd = end;
  3752. this.chigh = data[start];
  3753. this.clow = 0;
  3754. this.byteIn();
  3755. this.chigh = this.chigh << 7 & 0xffff | this.clow >> 9 & 0x7f;
  3756. this.clow = this.clow << 7 & 0xffff;
  3757. this.ct -= 7;
  3758. this.a = 0x8000;
  3759. }
  3760. byteIn() {
  3761. const data = this.data;
  3762. let bp = this.bp;
  3763. if (data[bp] === 0xff) {
  3764. if (data[bp + 1] > 0x8f) {
  3765. this.clow += 0xff00;
  3766. this.ct = 8;
  3767. } else {
  3768. bp++;
  3769. this.clow += data[bp] << 9;
  3770. this.ct = 7;
  3771. this.bp = bp;
  3772. }
  3773. } else {
  3774. bp++;
  3775. this.clow += bp < this.dataEnd ? data[bp] << 8 : 0xff00;
  3776. this.ct = 8;
  3777. this.bp = bp;
  3778. }
  3779. if (this.clow > 0xffff) {
  3780. this.chigh += this.clow >> 16;
  3781. this.clow &= 0xffff;
  3782. }
  3783. }
  3784. readBit(contexts, pos) {
  3785. let cx_index = contexts[pos] >> 1,
  3786. cx_mps = contexts[pos] & 1;
  3787. const qeTableIcx = QeTable[cx_index];
  3788. const qeIcx = qeTableIcx.qe;
  3789. let d;
  3790. let a = this.a - qeIcx;
  3791. if (this.chigh < qeIcx) {
  3792. if (a < qeIcx) {
  3793. a = qeIcx;
  3794. d = cx_mps;
  3795. cx_index = qeTableIcx.nmps;
  3796. } else {
  3797. a = qeIcx;
  3798. d = 1 ^ cx_mps;
  3799. if (qeTableIcx.switchFlag === 1) {
  3800. cx_mps = d;
  3801. }
  3802. cx_index = qeTableIcx.nlps;
  3803. }
  3804. } else {
  3805. this.chigh -= qeIcx;
  3806. if ((a & 0x8000) !== 0) {
  3807. this.a = a;
  3808. return cx_mps;
  3809. }
  3810. if (a < qeIcx) {
  3811. d = 1 ^ cx_mps;
  3812. if (qeTableIcx.switchFlag === 1) {
  3813. cx_mps = d;
  3814. }
  3815. cx_index = qeTableIcx.nlps;
  3816. } else {
  3817. d = cx_mps;
  3818. cx_index = qeTableIcx.nmps;
  3819. }
  3820. }
  3821. do {
  3822. if (this.ct === 0) {
  3823. this.byteIn();
  3824. }
  3825. a <<= 1;
  3826. this.chigh = this.chigh << 1 & 0xffff | this.clow >> 15 & 1;
  3827. this.clow = this.clow << 1 & 0xffff;
  3828. this.ct--;
  3829. } while ((a & 0x8000) === 0);
  3830. this.a = a;
  3831. contexts[pos] = cx_index << 1 | cx_mps;
  3832. return d;
  3833. }
  3834. }
  3835. exports.ArithmeticDecoder = ArithmeticDecoder;
  3836. /***/ }),
  3837. /* 7 */
  3838. /***/ ((__unused_webpack_module, exports, __w_pdfjs_require__) => {
  3839. Object.defineProperty(exports, "__esModule", ({
  3840. value: true
  3841. }));
  3842. exports.CCITTFaxDecoder = void 0;
  3843. var _util = __w_pdfjs_require__(1);
  3844. const ccittEOL = -2;
  3845. const ccittEOF = -1;
  3846. const twoDimPass = 0;
  3847. const twoDimHoriz = 1;
  3848. const twoDimVert0 = 2;
  3849. const twoDimVertR1 = 3;
  3850. const twoDimVertL1 = 4;
  3851. const twoDimVertR2 = 5;
  3852. const twoDimVertL2 = 6;
  3853. const twoDimVertR3 = 7;
  3854. const twoDimVertL3 = 8;
  3855. const twoDimTable = [[-1, -1], [-1, -1], [7, twoDimVertL3], [7, twoDimVertR3], [6, twoDimVertL2], [6, twoDimVertL2], [6, twoDimVertR2], [6, twoDimVertR2], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0]];
  3856. const whiteTable1 = [[-1, -1], [12, ccittEOL], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [11, 1792], [11, 1792], [12, 1984], [12, 2048], [12, 2112], [12, 2176], [12, 2240], [12, 2304], [11, 1856], [11, 1856], [11, 1920], [11, 1920], [12, 2368], [12, 2432], [12, 2496], [12, 2560]];
  3857. const whiteTable2 = [[-1, -1], [-1, -1], [-1, -1], [-1, -1], [8, 29], [8, 29], [8, 30], [8, 30], [8, 45], [8, 45], [8, 46], [8, 46], [7, 22], [7, 22], [7, 22], [7, 22], [7, 23], [7, 23], [7, 23], [7, 23], [8, 47], [8, 47], [8, 48], [8, 48], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [7, 20], [7, 20], [7, 20], [7, 20], [8, 33], [8, 33], [8, 34], [8, 34], [8, 35], [8, 35], [8, 36], [8, 36], [8, 37], [8, 37], [8, 38], [8, 38], [7, 19], [7, 19], [7, 19], [7, 19], [8, 31], [8, 31], [8, 32], [8, 32], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [8, 53], [8, 53], [8, 54], [8, 54], [7, 26], [7, 26], [7, 26], [7, 26], [8, 39], [8, 39], [8, 40], [8, 40], [8, 41], [8, 41], [8, 42], [8, 42], [8, 43], [8, 43], [8, 44], [8, 44], [7, 21], [7, 21], [7, 21], [7, 21], [7, 28], [7, 28], [7, 28], [7, 28], [8, 61], [8, 61], [8, 62], [8, 62], [8, 63], [8, 63], [8, 0], [8, 0], [8, 320], [8, 320], [8, 384], [8, 384], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [7, 27], [7, 27], [7, 27], [7, 27], [8, 59], [8, 59], [8, 60], [8, 60], [9, 1472], [9, 1536], [9, 1600], [9, 1728], [7, 18], [7, 18], [7, 18], [7, 18], [7, 24], [7, 24], [7, 24], [7, 24], [8, 49], [8, 49], [8, 50], [8, 50], [8, 51], [8, 51], [8, 52], [8, 52], [7, 25], [7, 25], [7, 25], [7, 25], [8, 55], [8, 55], [8, 56], [8, 56], [8, 57], [8, 57], [8, 58], [8, 58], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [8, 448], [8, 448], [8, 512], [8, 512], [9, 704], [9, 768], [8, 640], [8, 640], [8, 576], [8, 576], [9, 832], [9, 896], [9, 960], [9, 1024], [9, 1088], [9, 1152], [9, 1216], [9, 1280], [9, 1344], [9, 1408], [7, 256], [7, 256], [7, 256], [7, 256], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7]];
  3858. const blackTable1 = [[-1, -1], [-1, -1], [12, ccittEOL], [12, ccittEOL], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [11, 1792], [11, 1792], [11, 1792], [11, 1792], [12, 1984], [12, 1984], [12, 2048], [12, 2048], [12, 2112], [12, 2112], [12, 2176], [12, 2176], [12, 2240], [12, 2240], [12, 2304], [12, 2304], [11, 1856], [11, 1856], [11, 1856], [11, 1856], [11, 1920], [11, 1920], [11, 1920], [11, 1920], [12, 2368], [12, 2368], [12, 2432], [12, 2432], [12, 2496], [12, 2496], [12, 2560], [12, 2560], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [12, 52], [12, 52], [13, 640], [13, 704], [13, 768], [13, 832], [12, 55], [12, 55], [12, 56], [12, 56], [13, 1280], [13, 1344], [13, 1408], [13, 1472], [12, 59], [12, 59], [12, 60], [12, 60], [13, 1536], [13, 1600], [11, 24], [11, 24], [11, 24], [11, 24], [11, 25], [11, 25], [11, 25], [11, 25], [13, 1664], [13, 1728], [12, 320], [12, 320], [12, 384], [12, 384], [12, 448], [12, 448], [13, 512], [13, 576], [12, 53], [12, 53], [12, 54], [12, 54], [13, 896], [13, 960], [13, 1024], [13, 1088], [13, 1152], [13, 1216], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64]];
  3859. const blackTable2 = [[8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [11, 23], [11, 23], [12, 50], [12, 51], [12, 44], [12, 45], [12, 46], [12, 47], [12, 57], [12, 58], [12, 61], [12, 256], [10, 16], [10, 16], [10, 16], [10, 16], [10, 17], [10, 17], [10, 17], [10, 17], [12, 48], [12, 49], [12, 62], [12, 63], [12, 30], [12, 31], [12, 32], [12, 33], [12, 40], [12, 41], [11, 22], [11, 22], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [12, 128], [12, 192], [12, 26], [12, 27], [12, 28], [12, 29], [11, 19], [11, 19], [11, 20], [11, 20], [12, 34], [12, 35], [12, 36], [12, 37], [12, 38], [12, 39], [11, 21], [11, 21], [12, 42], [12, 43], [10, 0], [10, 0], [10, 0], [10, 0], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12]];
  3860. const blackTable3 = [[-1, -1], [-1, -1], [-1, -1], [-1, -1], [6, 9], [6, 8], [5, 7], [5, 7], [4, 6], [4, 6], [4, 6], [4, 6], [4, 5], [4, 5], [4, 5], [4, 5], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2]];
  3861. class CCITTFaxDecoder {
  3862. constructor(source, options = {}) {
  3863. if (!source || typeof source.next !== "function") {
  3864. throw new Error('CCITTFaxDecoder - invalid "source" parameter.');
  3865. }
  3866. this.source = source;
  3867. this.eof = false;
  3868. this.encoding = options.K || 0;
  3869. this.eoline = options.EndOfLine || false;
  3870. this.byteAlign = options.EncodedByteAlign || false;
  3871. this.columns = options.Columns || 1728;
  3872. this.rows = options.Rows || 0;
  3873. let eoblock = options.EndOfBlock;
  3874. if (eoblock === null || eoblock === undefined) {
  3875. eoblock = true;
  3876. }
  3877. this.eoblock = eoblock;
  3878. this.black = options.BlackIs1 || false;
  3879. this.codingLine = new Uint32Array(this.columns + 1);
  3880. this.refLine = new Uint32Array(this.columns + 2);
  3881. this.codingLine[0] = this.columns;
  3882. this.codingPos = 0;
  3883. this.row = 0;
  3884. this.nextLine2D = this.encoding < 0;
  3885. this.inputBits = 0;
  3886. this.inputBuf = 0;
  3887. this.outputBits = 0;
  3888. this.rowsDone = false;
  3889. let code1;
  3890. while ((code1 = this._lookBits(12)) === 0) {
  3891. this._eatBits(1);
  3892. }
  3893. if (code1 === 1) {
  3894. this._eatBits(12);
  3895. }
  3896. if (this.encoding > 0) {
  3897. this.nextLine2D = !this._lookBits(1);
  3898. this._eatBits(1);
  3899. }
  3900. }
  3901. readNextChar() {
  3902. if (this.eof) {
  3903. return -1;
  3904. }
  3905. const refLine = this.refLine;
  3906. const codingLine = this.codingLine;
  3907. const columns = this.columns;
  3908. let refPos, blackPixels, bits, i;
  3909. if (this.outputBits === 0) {
  3910. if (this.rowsDone) {
  3911. this.eof = true;
  3912. }
  3913. if (this.eof) {
  3914. return -1;
  3915. }
  3916. this.err = false;
  3917. let code1, code2, code3;
  3918. if (this.nextLine2D) {
  3919. for (i = 0; codingLine[i] < columns; ++i) {
  3920. refLine[i] = codingLine[i];
  3921. }
  3922. refLine[i++] = columns;
  3923. refLine[i] = columns;
  3924. codingLine[0] = 0;
  3925. this.codingPos = 0;
  3926. refPos = 0;
  3927. blackPixels = 0;
  3928. while (codingLine[this.codingPos] < columns) {
  3929. code1 = this._getTwoDimCode();
  3930. switch (code1) {
  3931. case twoDimPass:
  3932. this._addPixels(refLine[refPos + 1], blackPixels);
  3933. if (refLine[refPos + 1] < columns) {
  3934. refPos += 2;
  3935. }
  3936. break;
  3937. case twoDimHoriz:
  3938. code1 = code2 = 0;
  3939. if (blackPixels) {
  3940. do {
  3941. code1 += code3 = this._getBlackCode();
  3942. } while (code3 >= 64);
  3943. do {
  3944. code2 += code3 = this._getWhiteCode();
  3945. } while (code3 >= 64);
  3946. } else {
  3947. do {
  3948. code1 += code3 = this._getWhiteCode();
  3949. } while (code3 >= 64);
  3950. do {
  3951. code2 += code3 = this._getBlackCode();
  3952. } while (code3 >= 64);
  3953. }
  3954. this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
  3955. if (codingLine[this.codingPos] < columns) {
  3956. this._addPixels(codingLine[this.codingPos] + code2, blackPixels ^ 1);
  3957. }
  3958. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  3959. refPos += 2;
  3960. }
  3961. break;
  3962. case twoDimVertR3:
  3963. this._addPixels(refLine[refPos] + 3, blackPixels);
  3964. blackPixels ^= 1;
  3965. if (codingLine[this.codingPos] < columns) {
  3966. ++refPos;
  3967. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  3968. refPos += 2;
  3969. }
  3970. }
  3971. break;
  3972. case twoDimVertR2:
  3973. this._addPixels(refLine[refPos] + 2, blackPixels);
  3974. blackPixels ^= 1;
  3975. if (codingLine[this.codingPos] < columns) {
  3976. ++refPos;
  3977. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  3978. refPos += 2;
  3979. }
  3980. }
  3981. break;
  3982. case twoDimVertR1:
  3983. this._addPixels(refLine[refPos] + 1, blackPixels);
  3984. blackPixels ^= 1;
  3985. if (codingLine[this.codingPos] < columns) {
  3986. ++refPos;
  3987. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  3988. refPos += 2;
  3989. }
  3990. }
  3991. break;
  3992. case twoDimVert0:
  3993. this._addPixels(refLine[refPos], blackPixels);
  3994. blackPixels ^= 1;
  3995. if (codingLine[this.codingPos] < columns) {
  3996. ++refPos;
  3997. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  3998. refPos += 2;
  3999. }
  4000. }
  4001. break;
  4002. case twoDimVertL3:
  4003. this._addPixelsNeg(refLine[refPos] - 3, blackPixels);
  4004. blackPixels ^= 1;
  4005. if (codingLine[this.codingPos] < columns) {
  4006. if (refPos > 0) {
  4007. --refPos;
  4008. } else {
  4009. ++refPos;
  4010. }
  4011. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  4012. refPos += 2;
  4013. }
  4014. }
  4015. break;
  4016. case twoDimVertL2:
  4017. this._addPixelsNeg(refLine[refPos] - 2, blackPixels);
  4018. blackPixels ^= 1;
  4019. if (codingLine[this.codingPos] < columns) {
  4020. if (refPos > 0) {
  4021. --refPos;
  4022. } else {
  4023. ++refPos;
  4024. }
  4025. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  4026. refPos += 2;
  4027. }
  4028. }
  4029. break;
  4030. case twoDimVertL1:
  4031. this._addPixelsNeg(refLine[refPos] - 1, blackPixels);
  4032. blackPixels ^= 1;
  4033. if (codingLine[this.codingPos] < columns) {
  4034. if (refPos > 0) {
  4035. --refPos;
  4036. } else {
  4037. ++refPos;
  4038. }
  4039. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  4040. refPos += 2;
  4041. }
  4042. }
  4043. break;
  4044. case ccittEOF:
  4045. this._addPixels(columns, 0);
  4046. this.eof = true;
  4047. break;
  4048. default:
  4049. (0, _util.info)("bad 2d code");
  4050. this._addPixels(columns, 0);
  4051. this.err = true;
  4052. }
  4053. }
  4054. } else {
  4055. codingLine[0] = 0;
  4056. this.codingPos = 0;
  4057. blackPixels = 0;
  4058. while (codingLine[this.codingPos] < columns) {
  4059. code1 = 0;
  4060. if (blackPixels) {
  4061. do {
  4062. code1 += code3 = this._getBlackCode();
  4063. } while (code3 >= 64);
  4064. } else {
  4065. do {
  4066. code1 += code3 = this._getWhiteCode();
  4067. } while (code3 >= 64);
  4068. }
  4069. this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
  4070. blackPixels ^= 1;
  4071. }
  4072. }
  4073. let gotEOL = false;
  4074. if (this.byteAlign) {
  4075. this.inputBits &= ~7;
  4076. }
  4077. if (!this.eoblock && this.row === this.rows - 1) {
  4078. this.rowsDone = true;
  4079. } else {
  4080. code1 = this._lookBits(12);
  4081. if (this.eoline) {
  4082. while (code1 !== ccittEOF && code1 !== 1) {
  4083. this._eatBits(1);
  4084. code1 = this._lookBits(12);
  4085. }
  4086. } else {
  4087. while (code1 === 0) {
  4088. this._eatBits(1);
  4089. code1 = this._lookBits(12);
  4090. }
  4091. }
  4092. if (code1 === 1) {
  4093. this._eatBits(12);
  4094. gotEOL = true;
  4095. } else if (code1 === ccittEOF) {
  4096. this.eof = true;
  4097. }
  4098. }
  4099. if (!this.eof && this.encoding > 0 && !this.rowsDone) {
  4100. this.nextLine2D = !this._lookBits(1);
  4101. this._eatBits(1);
  4102. }
  4103. if (this.eoblock && gotEOL && this.byteAlign) {
  4104. code1 = this._lookBits(12);
  4105. if (code1 === 1) {
  4106. this._eatBits(12);
  4107. if (this.encoding > 0) {
  4108. this._lookBits(1);
  4109. this._eatBits(1);
  4110. }
  4111. if (this.encoding >= 0) {
  4112. for (i = 0; i < 4; ++i) {
  4113. code1 = this._lookBits(12);
  4114. if (code1 !== 1) {
  4115. (0, _util.info)("bad rtc code: " + code1);
  4116. }
  4117. this._eatBits(12);
  4118. if (this.encoding > 0) {
  4119. this._lookBits(1);
  4120. this._eatBits(1);
  4121. }
  4122. }
  4123. }
  4124. this.eof = true;
  4125. }
  4126. } else if (this.err && this.eoline) {
  4127. while (true) {
  4128. code1 = this._lookBits(13);
  4129. if (code1 === ccittEOF) {
  4130. this.eof = true;
  4131. return -1;
  4132. }
  4133. if (code1 >> 1 === 1) {
  4134. break;
  4135. }
  4136. this._eatBits(1);
  4137. }
  4138. this._eatBits(12);
  4139. if (this.encoding > 0) {
  4140. this._eatBits(1);
  4141. this.nextLine2D = !(code1 & 1);
  4142. }
  4143. }
  4144. if (codingLine[0] > 0) {
  4145. this.outputBits = codingLine[this.codingPos = 0];
  4146. } else {
  4147. this.outputBits = codingLine[this.codingPos = 1];
  4148. }
  4149. this.row++;
  4150. }
  4151. let c;
  4152. if (this.outputBits >= 8) {
  4153. c = this.codingPos & 1 ? 0 : 0xff;
  4154. this.outputBits -= 8;
  4155. if (this.outputBits === 0 && codingLine[this.codingPos] < columns) {
  4156. this.codingPos++;
  4157. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  4158. }
  4159. } else {
  4160. bits = 8;
  4161. c = 0;
  4162. do {
  4163. if (typeof this.outputBits !== "number") {
  4164. throw new _util.FormatError('Invalid /CCITTFaxDecode data, "outputBits" must be a number.');
  4165. }
  4166. if (this.outputBits > bits) {
  4167. c <<= bits;
  4168. if (!(this.codingPos & 1)) {
  4169. c |= 0xff >> 8 - bits;
  4170. }
  4171. this.outputBits -= bits;
  4172. bits = 0;
  4173. } else {
  4174. c <<= this.outputBits;
  4175. if (!(this.codingPos & 1)) {
  4176. c |= 0xff >> 8 - this.outputBits;
  4177. }
  4178. bits -= this.outputBits;
  4179. this.outputBits = 0;
  4180. if (codingLine[this.codingPos] < columns) {
  4181. this.codingPos++;
  4182. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  4183. } else if (bits > 0) {
  4184. c <<= bits;
  4185. bits = 0;
  4186. }
  4187. }
  4188. } while (bits);
  4189. }
  4190. if (this.black) {
  4191. c ^= 0xff;
  4192. }
  4193. return c;
  4194. }
  4195. _addPixels(a1, blackPixels) {
  4196. const codingLine = this.codingLine;
  4197. let codingPos = this.codingPos;
  4198. if (a1 > codingLine[codingPos]) {
  4199. if (a1 > this.columns) {
  4200. (0, _util.info)("row is wrong length");
  4201. this.err = true;
  4202. a1 = this.columns;
  4203. }
  4204. if (codingPos & 1 ^ blackPixels) {
  4205. ++codingPos;
  4206. }
  4207. codingLine[codingPos] = a1;
  4208. }
  4209. this.codingPos = codingPos;
  4210. }
  4211. _addPixelsNeg(a1, blackPixels) {
  4212. const codingLine = this.codingLine;
  4213. let codingPos = this.codingPos;
  4214. if (a1 > codingLine[codingPos]) {
  4215. if (a1 > this.columns) {
  4216. (0, _util.info)("row is wrong length");
  4217. this.err = true;
  4218. a1 = this.columns;
  4219. }
  4220. if (codingPos & 1 ^ blackPixels) {
  4221. ++codingPos;
  4222. }
  4223. codingLine[codingPos] = a1;
  4224. } else if (a1 < codingLine[codingPos]) {
  4225. if (a1 < 0) {
  4226. (0, _util.info)("invalid code");
  4227. this.err = true;
  4228. a1 = 0;
  4229. }
  4230. while (codingPos > 0 && a1 < codingLine[codingPos - 1]) {
  4231. --codingPos;
  4232. }
  4233. codingLine[codingPos] = a1;
  4234. }
  4235. this.codingPos = codingPos;
  4236. }
  4237. _findTableCode(start, end, table, limit) {
  4238. const limitValue = limit || 0;
  4239. for (let i = start; i <= end; ++i) {
  4240. let code = this._lookBits(i);
  4241. if (code === ccittEOF) {
  4242. return [true, 1, false];
  4243. }
  4244. if (i < end) {
  4245. code <<= end - i;
  4246. }
  4247. if (!limitValue || code >= limitValue) {
  4248. const p = table[code - limitValue];
  4249. if (p[0] === i) {
  4250. this._eatBits(i);
  4251. return [true, p[1], true];
  4252. }
  4253. }
  4254. }
  4255. return [false, 0, false];
  4256. }
  4257. _getTwoDimCode() {
  4258. let code = 0;
  4259. let p;
  4260. if (this.eoblock) {
  4261. code = this._lookBits(7);
  4262. p = twoDimTable[code];
  4263. if (p && p[0] > 0) {
  4264. this._eatBits(p[0]);
  4265. return p[1];
  4266. }
  4267. } else {
  4268. const result = this._findTableCode(1, 7, twoDimTable);
  4269. if (result[0] && result[2]) {
  4270. return result[1];
  4271. }
  4272. }
  4273. (0, _util.info)("Bad two dim code");
  4274. return ccittEOF;
  4275. }
  4276. _getWhiteCode() {
  4277. let code = 0;
  4278. let p;
  4279. if (this.eoblock) {
  4280. code = this._lookBits(12);
  4281. if (code === ccittEOF) {
  4282. return 1;
  4283. }
  4284. if (code >> 5 === 0) {
  4285. p = whiteTable1[code];
  4286. } else {
  4287. p = whiteTable2[code >> 3];
  4288. }
  4289. if (p[0] > 0) {
  4290. this._eatBits(p[0]);
  4291. return p[1];
  4292. }
  4293. } else {
  4294. let result = this._findTableCode(1, 9, whiteTable2);
  4295. if (result[0]) {
  4296. return result[1];
  4297. }
  4298. result = this._findTableCode(11, 12, whiteTable1);
  4299. if (result[0]) {
  4300. return result[1];
  4301. }
  4302. }
  4303. (0, _util.info)("bad white code");
  4304. this._eatBits(1);
  4305. return 1;
  4306. }
  4307. _getBlackCode() {
  4308. let code, p;
  4309. if (this.eoblock) {
  4310. code = this._lookBits(13);
  4311. if (code === ccittEOF) {
  4312. return 1;
  4313. }
  4314. if (code >> 7 === 0) {
  4315. p = blackTable1[code];
  4316. } else if (code >> 9 === 0 && code >> 7 !== 0) {
  4317. p = blackTable2[(code >> 1) - 64];
  4318. } else {
  4319. p = blackTable3[code >> 7];
  4320. }
  4321. if (p[0] > 0) {
  4322. this._eatBits(p[0]);
  4323. return p[1];
  4324. }
  4325. } else {
  4326. let result = this._findTableCode(2, 6, blackTable3);
  4327. if (result[0]) {
  4328. return result[1];
  4329. }
  4330. result = this._findTableCode(7, 12, blackTable2, 64);
  4331. if (result[0]) {
  4332. return result[1];
  4333. }
  4334. result = this._findTableCode(10, 13, blackTable1);
  4335. if (result[0]) {
  4336. return result[1];
  4337. }
  4338. }
  4339. (0, _util.info)("bad black code");
  4340. this._eatBits(1);
  4341. return 1;
  4342. }
  4343. _lookBits(n) {
  4344. let c;
  4345. while (this.inputBits < n) {
  4346. if ((c = this.source.next()) === -1) {
  4347. if (this.inputBits === 0) {
  4348. return ccittEOF;
  4349. }
  4350. return this.inputBuf << n - this.inputBits & 0xffff >> 16 - n;
  4351. }
  4352. this.inputBuf = this.inputBuf << 8 | c;
  4353. this.inputBits += 8;
  4354. }
  4355. return this.inputBuf >> this.inputBits - n & 0xffff >> 16 - n;
  4356. }
  4357. _eatBits(n) {
  4358. if ((this.inputBits -= n) < 0) {
  4359. this.inputBits = 0;
  4360. }
  4361. }
  4362. }
  4363. exports.CCITTFaxDecoder = CCITTFaxDecoder;
  4364. /***/ }),
  4365. /* 8 */
  4366. /***/ ((__unused_webpack_module, exports, __w_pdfjs_require__) => {
  4367. Object.defineProperty(exports, "__esModule", ({
  4368. value: true
  4369. }));
  4370. exports.JpegImage = void 0;
  4371. var _util = __w_pdfjs_require__(1);
  4372. var _core_utils = __w_pdfjs_require__(3);
  4373. class JpegError extends _util.BaseException {
  4374. constructor(msg) {
  4375. super(`JPEG error: ${msg}`, "JpegError");
  4376. }
  4377. }
  4378. class DNLMarkerError extends _util.BaseException {
  4379. constructor(message, scanLines) {
  4380. super(message, "DNLMarkerError");
  4381. this.scanLines = scanLines;
  4382. }
  4383. }
  4384. class EOIMarkerError extends _util.BaseException {
  4385. constructor(msg) {
  4386. super(msg, "EOIMarkerError");
  4387. }
  4388. }
  4389. const dctZigZag = new Uint8Array([0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63]);
  4390. const dctCos1 = 4017;
  4391. const dctSin1 = 799;
  4392. const dctCos3 = 3406;
  4393. const dctSin3 = 2276;
  4394. const dctCos6 = 1567;
  4395. const dctSin6 = 3784;
  4396. const dctSqrt2 = 5793;
  4397. const dctSqrt1d2 = 2896;
  4398. function buildHuffmanTable(codeLengths, values) {
  4399. let k = 0,
  4400. i,
  4401. j,
  4402. length = 16;
  4403. while (length > 0 && !codeLengths[length - 1]) {
  4404. length--;
  4405. }
  4406. const code = [{
  4407. children: [],
  4408. index: 0
  4409. }];
  4410. let p = code[0],
  4411. q;
  4412. for (i = 0; i < length; i++) {
  4413. for (j = 0; j < codeLengths[i]; j++) {
  4414. p = code.pop();
  4415. p.children[p.index] = values[k];
  4416. while (p.index > 0) {
  4417. p = code.pop();
  4418. }
  4419. p.index++;
  4420. code.push(p);
  4421. while (code.length <= i) {
  4422. code.push(q = {
  4423. children: [],
  4424. index: 0
  4425. });
  4426. p.children[p.index] = q.children;
  4427. p = q;
  4428. }
  4429. k++;
  4430. }
  4431. if (i + 1 < length) {
  4432. code.push(q = {
  4433. children: [],
  4434. index: 0
  4435. });
  4436. p.children[p.index] = q.children;
  4437. p = q;
  4438. }
  4439. }
  4440. return code[0].children;
  4441. }
  4442. function getBlockBufferOffset(component, row, col) {
  4443. return 64 * ((component.blocksPerLine + 1) * row + col);
  4444. }
  4445. function decodeScan(data, offset, frame, components, resetInterval, spectralStart, spectralEnd, successivePrev, successive, parseDNLMarker = false) {
  4446. const mcusPerLine = frame.mcusPerLine;
  4447. const progressive = frame.progressive;
  4448. const startOffset = offset;
  4449. let bitsData = 0,
  4450. bitsCount = 0;
  4451. function readBit() {
  4452. if (bitsCount > 0) {
  4453. bitsCount--;
  4454. return bitsData >> bitsCount & 1;
  4455. }
  4456. bitsData = data[offset++];
  4457. if (bitsData === 0xff) {
  4458. const nextByte = data[offset++];
  4459. if (nextByte) {
  4460. if (nextByte === 0xdc && parseDNLMarker) {
  4461. offset += 2;
  4462. const scanLines = (0, _core_utils.readUint16)(data, offset);
  4463. offset += 2;
  4464. if (scanLines > 0 && scanLines !== frame.scanLines) {
  4465. throw new DNLMarkerError("Found DNL marker (0xFFDC) while parsing scan data", scanLines);
  4466. }
  4467. } else if (nextByte === 0xd9) {
  4468. if (parseDNLMarker) {
  4469. const maybeScanLines = blockRow * (frame.precision === 8 ? 8 : 0);
  4470. if (maybeScanLines > 0 && Math.round(frame.scanLines / maybeScanLines) >= 5) {
  4471. throw new DNLMarkerError("Found EOI marker (0xFFD9) while parsing scan data, " + "possibly caused by incorrect `scanLines` parameter", maybeScanLines);
  4472. }
  4473. }
  4474. throw new EOIMarkerError("Found EOI marker (0xFFD9) while parsing scan data");
  4475. }
  4476. throw new JpegError(`unexpected marker ${(bitsData << 8 | nextByte).toString(16)}`);
  4477. }
  4478. }
  4479. bitsCount = 7;
  4480. return bitsData >>> 7;
  4481. }
  4482. function decodeHuffman(tree) {
  4483. let node = tree;
  4484. while (true) {
  4485. node = node[readBit()];
  4486. switch (typeof node) {
  4487. case "number":
  4488. return node;
  4489. case "object":
  4490. continue;
  4491. }
  4492. throw new JpegError("invalid huffman sequence");
  4493. }
  4494. }
  4495. function receive(length) {
  4496. let n = 0;
  4497. while (length > 0) {
  4498. n = n << 1 | readBit();
  4499. length--;
  4500. }
  4501. return n;
  4502. }
  4503. function receiveAndExtend(length) {
  4504. if (length === 1) {
  4505. return readBit() === 1 ? 1 : -1;
  4506. }
  4507. const n = receive(length);
  4508. if (n >= 1 << length - 1) {
  4509. return n;
  4510. }
  4511. return n + (-1 << length) + 1;
  4512. }
  4513. function decodeBaseline(component, blockOffset) {
  4514. const t = decodeHuffman(component.huffmanTableDC);
  4515. const diff = t === 0 ? 0 : receiveAndExtend(t);
  4516. component.blockData[blockOffset] = component.pred += diff;
  4517. let k = 1;
  4518. while (k < 64) {
  4519. const rs = decodeHuffman(component.huffmanTableAC);
  4520. const s = rs & 15,
  4521. r = rs >> 4;
  4522. if (s === 0) {
  4523. if (r < 15) {
  4524. break;
  4525. }
  4526. k += 16;
  4527. continue;
  4528. }
  4529. k += r;
  4530. const z = dctZigZag[k];
  4531. component.blockData[blockOffset + z] = receiveAndExtend(s);
  4532. k++;
  4533. }
  4534. }
  4535. function decodeDCFirst(component, blockOffset) {
  4536. const t = decodeHuffman(component.huffmanTableDC);
  4537. const diff = t === 0 ? 0 : receiveAndExtend(t) << successive;
  4538. component.blockData[blockOffset] = component.pred += diff;
  4539. }
  4540. function decodeDCSuccessive(component, blockOffset) {
  4541. component.blockData[blockOffset] |= readBit() << successive;
  4542. }
  4543. let eobrun = 0;
  4544. function decodeACFirst(component, blockOffset) {
  4545. if (eobrun > 0) {
  4546. eobrun--;
  4547. return;
  4548. }
  4549. let k = spectralStart;
  4550. const e = spectralEnd;
  4551. while (k <= e) {
  4552. const rs = decodeHuffman(component.huffmanTableAC);
  4553. const s = rs & 15,
  4554. r = rs >> 4;
  4555. if (s === 0) {
  4556. if (r < 15) {
  4557. eobrun = receive(r) + (1 << r) - 1;
  4558. break;
  4559. }
  4560. k += 16;
  4561. continue;
  4562. }
  4563. k += r;
  4564. const z = dctZigZag[k];
  4565. component.blockData[blockOffset + z] = receiveAndExtend(s) * (1 << successive);
  4566. k++;
  4567. }
  4568. }
  4569. let successiveACState = 0,
  4570. successiveACNextValue;
  4571. function decodeACSuccessive(component, blockOffset) {
  4572. let k = spectralStart;
  4573. const e = spectralEnd;
  4574. let r = 0;
  4575. let s;
  4576. let rs;
  4577. while (k <= e) {
  4578. const offsetZ = blockOffset + dctZigZag[k];
  4579. const sign = component.blockData[offsetZ] < 0 ? -1 : 1;
  4580. switch (successiveACState) {
  4581. case 0:
  4582. rs = decodeHuffman(component.huffmanTableAC);
  4583. s = rs & 15;
  4584. r = rs >> 4;
  4585. if (s === 0) {
  4586. if (r < 15) {
  4587. eobrun = receive(r) + (1 << r);
  4588. successiveACState = 4;
  4589. } else {
  4590. r = 16;
  4591. successiveACState = 1;
  4592. }
  4593. } else {
  4594. if (s !== 1) {
  4595. throw new JpegError("invalid ACn encoding");
  4596. }
  4597. successiveACNextValue = receiveAndExtend(s);
  4598. successiveACState = r ? 2 : 3;
  4599. }
  4600. continue;
  4601. case 1:
  4602. case 2:
  4603. if (component.blockData[offsetZ]) {
  4604. component.blockData[offsetZ] += sign * (readBit() << successive);
  4605. } else {
  4606. r--;
  4607. if (r === 0) {
  4608. successiveACState = successiveACState === 2 ? 3 : 0;
  4609. }
  4610. }
  4611. break;
  4612. case 3:
  4613. if (component.blockData[offsetZ]) {
  4614. component.blockData[offsetZ] += sign * (readBit() << successive);
  4615. } else {
  4616. component.blockData[offsetZ] = successiveACNextValue << successive;
  4617. successiveACState = 0;
  4618. }
  4619. break;
  4620. case 4:
  4621. if (component.blockData[offsetZ]) {
  4622. component.blockData[offsetZ] += sign * (readBit() << successive);
  4623. }
  4624. break;
  4625. }
  4626. k++;
  4627. }
  4628. if (successiveACState === 4) {
  4629. eobrun--;
  4630. if (eobrun === 0) {
  4631. successiveACState = 0;
  4632. }
  4633. }
  4634. }
  4635. let blockRow = 0;
  4636. function decodeMcu(component, decode, mcu, row, col) {
  4637. const mcuRow = mcu / mcusPerLine | 0;
  4638. const mcuCol = mcu % mcusPerLine;
  4639. blockRow = mcuRow * component.v + row;
  4640. const blockCol = mcuCol * component.h + col;
  4641. const blockOffset = getBlockBufferOffset(component, blockRow, blockCol);
  4642. decode(component, blockOffset);
  4643. }
  4644. function decodeBlock(component, decode, mcu) {
  4645. blockRow = mcu / component.blocksPerLine | 0;
  4646. const blockCol = mcu % component.blocksPerLine;
  4647. const blockOffset = getBlockBufferOffset(component, blockRow, blockCol);
  4648. decode(component, blockOffset);
  4649. }
  4650. const componentsLength = components.length;
  4651. let component, i, j, k, n;
  4652. let decodeFn;
  4653. if (progressive) {
  4654. if (spectralStart === 0) {
  4655. decodeFn = successivePrev === 0 ? decodeDCFirst : decodeDCSuccessive;
  4656. } else {
  4657. decodeFn = successivePrev === 0 ? decodeACFirst : decodeACSuccessive;
  4658. }
  4659. } else {
  4660. decodeFn = decodeBaseline;
  4661. }
  4662. let mcu = 0,
  4663. fileMarker;
  4664. let mcuExpected;
  4665. if (componentsLength === 1) {
  4666. mcuExpected = components[0].blocksPerLine * components[0].blocksPerColumn;
  4667. } else {
  4668. mcuExpected = mcusPerLine * frame.mcusPerColumn;
  4669. }
  4670. let h, v;
  4671. while (mcu <= mcuExpected) {
  4672. const mcuToRead = resetInterval ? Math.min(mcuExpected - mcu, resetInterval) : mcuExpected;
  4673. if (mcuToRead > 0) {
  4674. for (i = 0; i < componentsLength; i++) {
  4675. components[i].pred = 0;
  4676. }
  4677. eobrun = 0;
  4678. if (componentsLength === 1) {
  4679. component = components[0];
  4680. for (n = 0; n < mcuToRead; n++) {
  4681. decodeBlock(component, decodeFn, mcu);
  4682. mcu++;
  4683. }
  4684. } else {
  4685. for (n = 0; n < mcuToRead; n++) {
  4686. for (i = 0; i < componentsLength; i++) {
  4687. component = components[i];
  4688. h = component.h;
  4689. v = component.v;
  4690. for (j = 0; j < v; j++) {
  4691. for (k = 0; k < h; k++) {
  4692. decodeMcu(component, decodeFn, mcu, j, k);
  4693. }
  4694. }
  4695. }
  4696. mcu++;
  4697. }
  4698. }
  4699. }
  4700. bitsCount = 0;
  4701. fileMarker = findNextFileMarker(data, offset);
  4702. if (!fileMarker) {
  4703. break;
  4704. }
  4705. if (fileMarker.invalid) {
  4706. const partialMsg = mcuToRead > 0 ? "unexpected" : "excessive";
  4707. (0, _util.warn)(`decodeScan - ${partialMsg} MCU data, current marker is: ${fileMarker.invalid}`);
  4708. offset = fileMarker.offset;
  4709. }
  4710. if (fileMarker.marker >= 0xffd0 && fileMarker.marker <= 0xffd7) {
  4711. offset += 2;
  4712. } else {
  4713. break;
  4714. }
  4715. }
  4716. return offset - startOffset;
  4717. }
  4718. function quantizeAndInverse(component, blockBufferOffset, p) {
  4719. const qt = component.quantizationTable,
  4720. blockData = component.blockData;
  4721. let v0, v1, v2, v3, v4, v5, v6, v7;
  4722. let p0, p1, p2, p3, p4, p5, p6, p7;
  4723. let t;
  4724. if (!qt) {
  4725. throw new JpegError("missing required Quantization Table.");
  4726. }
  4727. for (let row = 0; row < 64; row += 8) {
  4728. p0 = blockData[blockBufferOffset + row];
  4729. p1 = blockData[blockBufferOffset + row + 1];
  4730. p2 = blockData[blockBufferOffset + row + 2];
  4731. p3 = blockData[blockBufferOffset + row + 3];
  4732. p4 = blockData[blockBufferOffset + row + 4];
  4733. p5 = blockData[blockBufferOffset + row + 5];
  4734. p6 = blockData[blockBufferOffset + row + 6];
  4735. p7 = blockData[blockBufferOffset + row + 7];
  4736. p0 *= qt[row];
  4737. if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {
  4738. t = dctSqrt2 * p0 + 512 >> 10;
  4739. p[row] = t;
  4740. p[row + 1] = t;
  4741. p[row + 2] = t;
  4742. p[row + 3] = t;
  4743. p[row + 4] = t;
  4744. p[row + 5] = t;
  4745. p[row + 6] = t;
  4746. p[row + 7] = t;
  4747. continue;
  4748. }
  4749. p1 *= qt[row + 1];
  4750. p2 *= qt[row + 2];
  4751. p3 *= qt[row + 3];
  4752. p4 *= qt[row + 4];
  4753. p5 *= qt[row + 5];
  4754. p6 *= qt[row + 6];
  4755. p7 *= qt[row + 7];
  4756. v0 = dctSqrt2 * p0 + 128 >> 8;
  4757. v1 = dctSqrt2 * p4 + 128 >> 8;
  4758. v2 = p2;
  4759. v3 = p6;
  4760. v4 = dctSqrt1d2 * (p1 - p7) + 128 >> 8;
  4761. v7 = dctSqrt1d2 * (p1 + p7) + 128 >> 8;
  4762. v5 = p3 << 4;
  4763. v6 = p5 << 4;
  4764. v0 = v0 + v1 + 1 >> 1;
  4765. v1 = v0 - v1;
  4766. t = v2 * dctSin6 + v3 * dctCos6 + 128 >> 8;
  4767. v2 = v2 * dctCos6 - v3 * dctSin6 + 128 >> 8;
  4768. v3 = t;
  4769. v4 = v4 + v6 + 1 >> 1;
  4770. v6 = v4 - v6;
  4771. v7 = v7 + v5 + 1 >> 1;
  4772. v5 = v7 - v5;
  4773. v0 = v0 + v3 + 1 >> 1;
  4774. v3 = v0 - v3;
  4775. v1 = v1 + v2 + 1 >> 1;
  4776. v2 = v1 - v2;
  4777. t = v4 * dctSin3 + v7 * dctCos3 + 2048 >> 12;
  4778. v4 = v4 * dctCos3 - v7 * dctSin3 + 2048 >> 12;
  4779. v7 = t;
  4780. t = v5 * dctSin1 + v6 * dctCos1 + 2048 >> 12;
  4781. v5 = v5 * dctCos1 - v6 * dctSin1 + 2048 >> 12;
  4782. v6 = t;
  4783. p[row] = v0 + v7;
  4784. p[row + 7] = v0 - v7;
  4785. p[row + 1] = v1 + v6;
  4786. p[row + 6] = v1 - v6;
  4787. p[row + 2] = v2 + v5;
  4788. p[row + 5] = v2 - v5;
  4789. p[row + 3] = v3 + v4;
  4790. p[row + 4] = v3 - v4;
  4791. }
  4792. for (let col = 0; col < 8; ++col) {
  4793. p0 = p[col];
  4794. p1 = p[col + 8];
  4795. p2 = p[col + 16];
  4796. p3 = p[col + 24];
  4797. p4 = p[col + 32];
  4798. p5 = p[col + 40];
  4799. p6 = p[col + 48];
  4800. p7 = p[col + 56];
  4801. if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {
  4802. t = dctSqrt2 * p0 + 8192 >> 14;
  4803. if (t < -2040) {
  4804. t = 0;
  4805. } else if (t >= 2024) {
  4806. t = 255;
  4807. } else {
  4808. t = t + 2056 >> 4;
  4809. }
  4810. blockData[blockBufferOffset + col] = t;
  4811. blockData[blockBufferOffset + col + 8] = t;
  4812. blockData[blockBufferOffset + col + 16] = t;
  4813. blockData[blockBufferOffset + col + 24] = t;
  4814. blockData[blockBufferOffset + col + 32] = t;
  4815. blockData[blockBufferOffset + col + 40] = t;
  4816. blockData[blockBufferOffset + col + 48] = t;
  4817. blockData[blockBufferOffset + col + 56] = t;
  4818. continue;
  4819. }
  4820. v0 = dctSqrt2 * p0 + 2048 >> 12;
  4821. v1 = dctSqrt2 * p4 + 2048 >> 12;
  4822. v2 = p2;
  4823. v3 = p6;
  4824. v4 = dctSqrt1d2 * (p1 - p7) + 2048 >> 12;
  4825. v7 = dctSqrt1d2 * (p1 + p7) + 2048 >> 12;
  4826. v5 = p3;
  4827. v6 = p5;
  4828. v0 = (v0 + v1 + 1 >> 1) + 4112;
  4829. v1 = v0 - v1;
  4830. t = v2 * dctSin6 + v3 * dctCos6 + 2048 >> 12;
  4831. v2 = v2 * dctCos6 - v3 * dctSin6 + 2048 >> 12;
  4832. v3 = t;
  4833. v4 = v4 + v6 + 1 >> 1;
  4834. v6 = v4 - v6;
  4835. v7 = v7 + v5 + 1 >> 1;
  4836. v5 = v7 - v5;
  4837. v0 = v0 + v3 + 1 >> 1;
  4838. v3 = v0 - v3;
  4839. v1 = v1 + v2 + 1 >> 1;
  4840. v2 = v1 - v2;
  4841. t = v4 * dctSin3 + v7 * dctCos3 + 2048 >> 12;
  4842. v4 = v4 * dctCos3 - v7 * dctSin3 + 2048 >> 12;
  4843. v7 = t;
  4844. t = v5 * dctSin1 + v6 * dctCos1 + 2048 >> 12;
  4845. v5 = v5 * dctCos1 - v6 * dctSin1 + 2048 >> 12;
  4846. v6 = t;
  4847. p0 = v0 + v7;
  4848. p7 = v0 - v7;
  4849. p1 = v1 + v6;
  4850. p6 = v1 - v6;
  4851. p2 = v2 + v5;
  4852. p5 = v2 - v5;
  4853. p3 = v3 + v4;
  4854. p4 = v3 - v4;
  4855. if (p0 < 16) {
  4856. p0 = 0;
  4857. } else if (p0 >= 4080) {
  4858. p0 = 255;
  4859. } else {
  4860. p0 >>= 4;
  4861. }
  4862. if (p1 < 16) {
  4863. p1 = 0;
  4864. } else if (p1 >= 4080) {
  4865. p1 = 255;
  4866. } else {
  4867. p1 >>= 4;
  4868. }
  4869. if (p2 < 16) {
  4870. p2 = 0;
  4871. } else if (p2 >= 4080) {
  4872. p2 = 255;
  4873. } else {
  4874. p2 >>= 4;
  4875. }
  4876. if (p3 < 16) {
  4877. p3 = 0;
  4878. } else if (p3 >= 4080) {
  4879. p3 = 255;
  4880. } else {
  4881. p3 >>= 4;
  4882. }
  4883. if (p4 < 16) {
  4884. p4 = 0;
  4885. } else if (p4 >= 4080) {
  4886. p4 = 255;
  4887. } else {
  4888. p4 >>= 4;
  4889. }
  4890. if (p5 < 16) {
  4891. p5 = 0;
  4892. } else if (p5 >= 4080) {
  4893. p5 = 255;
  4894. } else {
  4895. p5 >>= 4;
  4896. }
  4897. if (p6 < 16) {
  4898. p6 = 0;
  4899. } else if (p6 >= 4080) {
  4900. p6 = 255;
  4901. } else {
  4902. p6 >>= 4;
  4903. }
  4904. if (p7 < 16) {
  4905. p7 = 0;
  4906. } else if (p7 >= 4080) {
  4907. p7 = 255;
  4908. } else {
  4909. p7 >>= 4;
  4910. }
  4911. blockData[blockBufferOffset + col] = p0;
  4912. blockData[blockBufferOffset + col + 8] = p1;
  4913. blockData[blockBufferOffset + col + 16] = p2;
  4914. blockData[blockBufferOffset + col + 24] = p3;
  4915. blockData[blockBufferOffset + col + 32] = p4;
  4916. blockData[blockBufferOffset + col + 40] = p5;
  4917. blockData[blockBufferOffset + col + 48] = p6;
  4918. blockData[blockBufferOffset + col + 56] = p7;
  4919. }
  4920. }
  4921. function buildComponentData(frame, component) {
  4922. const blocksPerLine = component.blocksPerLine;
  4923. const blocksPerColumn = component.blocksPerColumn;
  4924. const computationBuffer = new Int16Array(64);
  4925. for (let blockRow = 0; blockRow < blocksPerColumn; blockRow++) {
  4926. for (let blockCol = 0; blockCol < blocksPerLine; blockCol++) {
  4927. const offset = getBlockBufferOffset(component, blockRow, blockCol);
  4928. quantizeAndInverse(component, offset, computationBuffer);
  4929. }
  4930. }
  4931. return component.blockData;
  4932. }
  4933. function findNextFileMarker(data, currentPos, startPos = currentPos) {
  4934. const maxPos = data.length - 1;
  4935. let newPos = startPos < currentPos ? startPos : currentPos;
  4936. if (currentPos >= maxPos) {
  4937. return null;
  4938. }
  4939. const currentMarker = (0, _core_utils.readUint16)(data, currentPos);
  4940. if (currentMarker >= 0xffc0 && currentMarker <= 0xfffe) {
  4941. return {
  4942. invalid: null,
  4943. marker: currentMarker,
  4944. offset: currentPos
  4945. };
  4946. }
  4947. let newMarker = (0, _core_utils.readUint16)(data, newPos);
  4948. while (!(newMarker >= 0xffc0 && newMarker <= 0xfffe)) {
  4949. if (++newPos >= maxPos) {
  4950. return null;
  4951. }
  4952. newMarker = (0, _core_utils.readUint16)(data, newPos);
  4953. }
  4954. return {
  4955. invalid: currentMarker.toString(16),
  4956. marker: newMarker,
  4957. offset: newPos
  4958. };
  4959. }
  4960. class JpegImage {
  4961. constructor({
  4962. decodeTransform = null,
  4963. colorTransform = -1
  4964. } = {}) {
  4965. this._decodeTransform = decodeTransform;
  4966. this._colorTransform = colorTransform;
  4967. }
  4968. parse(data, {
  4969. dnlScanLines = null
  4970. } = {}) {
  4971. function readDataBlock() {
  4972. const length = (0, _core_utils.readUint16)(data, offset);
  4973. offset += 2;
  4974. let endOffset = offset + length - 2;
  4975. const fileMarker = findNextFileMarker(data, endOffset, offset);
  4976. if (fileMarker && fileMarker.invalid) {
  4977. (0, _util.warn)("readDataBlock - incorrect length, current marker is: " + fileMarker.invalid);
  4978. endOffset = fileMarker.offset;
  4979. }
  4980. const array = data.subarray(offset, endOffset);
  4981. offset += array.length;
  4982. return array;
  4983. }
  4984. function prepareComponents(frame) {
  4985. const mcusPerLine = Math.ceil(frame.samplesPerLine / 8 / frame.maxH);
  4986. const mcusPerColumn = Math.ceil(frame.scanLines / 8 / frame.maxV);
  4987. for (const component of frame.components) {
  4988. const blocksPerLine = Math.ceil(Math.ceil(frame.samplesPerLine / 8) * component.h / frame.maxH);
  4989. const blocksPerColumn = Math.ceil(Math.ceil(frame.scanLines / 8) * component.v / frame.maxV);
  4990. const blocksPerLineForMcu = mcusPerLine * component.h;
  4991. const blocksPerColumnForMcu = mcusPerColumn * component.v;
  4992. const blocksBufferSize = 64 * blocksPerColumnForMcu * (blocksPerLineForMcu + 1);
  4993. component.blockData = new Int16Array(blocksBufferSize);
  4994. component.blocksPerLine = blocksPerLine;
  4995. component.blocksPerColumn = blocksPerColumn;
  4996. }
  4997. frame.mcusPerLine = mcusPerLine;
  4998. frame.mcusPerColumn = mcusPerColumn;
  4999. }
  5000. let offset = 0;
  5001. let jfif = null;
  5002. let adobe = null;
  5003. let frame, resetInterval;
  5004. let numSOSMarkers = 0;
  5005. const quantizationTables = [];
  5006. const huffmanTablesAC = [],
  5007. huffmanTablesDC = [];
  5008. let fileMarker = (0, _core_utils.readUint16)(data, offset);
  5009. offset += 2;
  5010. if (fileMarker !== 0xffd8) {
  5011. throw new JpegError("SOI not found");
  5012. }
  5013. fileMarker = (0, _core_utils.readUint16)(data, offset);
  5014. offset += 2;
  5015. markerLoop: while (fileMarker !== 0xffd9) {
  5016. let i, j, l;
  5017. switch (fileMarker) {
  5018. case 0xffe0:
  5019. case 0xffe1:
  5020. case 0xffe2:
  5021. case 0xffe3:
  5022. case 0xffe4:
  5023. case 0xffe5:
  5024. case 0xffe6:
  5025. case 0xffe7:
  5026. case 0xffe8:
  5027. case 0xffe9:
  5028. case 0xffea:
  5029. case 0xffeb:
  5030. case 0xffec:
  5031. case 0xffed:
  5032. case 0xffee:
  5033. case 0xffef:
  5034. case 0xfffe:
  5035. const appData = readDataBlock();
  5036. if (fileMarker === 0xffe0) {
  5037. if (appData[0] === 0x4a && appData[1] === 0x46 && appData[2] === 0x49 && appData[3] === 0x46 && appData[4] === 0) {
  5038. jfif = {
  5039. version: {
  5040. major: appData[5],
  5041. minor: appData[6]
  5042. },
  5043. densityUnits: appData[7],
  5044. xDensity: appData[8] << 8 | appData[9],
  5045. yDensity: appData[10] << 8 | appData[11],
  5046. thumbWidth: appData[12],
  5047. thumbHeight: appData[13],
  5048. thumbData: appData.subarray(14, 14 + 3 * appData[12] * appData[13])
  5049. };
  5050. }
  5051. }
  5052. if (fileMarker === 0xffee) {
  5053. if (appData[0] === 0x41 && appData[1] === 0x64 && appData[2] === 0x6f && appData[3] === 0x62 && appData[4] === 0x65) {
  5054. adobe = {
  5055. version: appData[5] << 8 | appData[6],
  5056. flags0: appData[7] << 8 | appData[8],
  5057. flags1: appData[9] << 8 | appData[10],
  5058. transformCode: appData[11]
  5059. };
  5060. }
  5061. }
  5062. break;
  5063. case 0xffdb:
  5064. const quantizationTablesLength = (0, _core_utils.readUint16)(data, offset);
  5065. offset += 2;
  5066. const quantizationTablesEnd = quantizationTablesLength + offset - 2;
  5067. let z;
  5068. while (offset < quantizationTablesEnd) {
  5069. const quantizationTableSpec = data[offset++];
  5070. const tableData = new Uint16Array(64);
  5071. if (quantizationTableSpec >> 4 === 0) {
  5072. for (j = 0; j < 64; j++) {
  5073. z = dctZigZag[j];
  5074. tableData[z] = data[offset++];
  5075. }
  5076. } else if (quantizationTableSpec >> 4 === 1) {
  5077. for (j = 0; j < 64; j++) {
  5078. z = dctZigZag[j];
  5079. tableData[z] = (0, _core_utils.readUint16)(data, offset);
  5080. offset += 2;
  5081. }
  5082. } else {
  5083. throw new JpegError("DQT - invalid table spec");
  5084. }
  5085. quantizationTables[quantizationTableSpec & 15] = tableData;
  5086. }
  5087. break;
  5088. case 0xffc0:
  5089. case 0xffc1:
  5090. case 0xffc2:
  5091. if (frame) {
  5092. throw new JpegError("Only single frame JPEGs supported");
  5093. }
  5094. offset += 2;
  5095. frame = {};
  5096. frame.extended = fileMarker === 0xffc1;
  5097. frame.progressive = fileMarker === 0xffc2;
  5098. frame.precision = data[offset++];
  5099. const sofScanLines = (0, _core_utils.readUint16)(data, offset);
  5100. offset += 2;
  5101. frame.scanLines = dnlScanLines || sofScanLines;
  5102. frame.samplesPerLine = (0, _core_utils.readUint16)(data, offset);
  5103. offset += 2;
  5104. frame.components = [];
  5105. frame.componentIds = {};
  5106. const componentsCount = data[offset++];
  5107. let maxH = 0,
  5108. maxV = 0;
  5109. for (i = 0; i < componentsCount; i++) {
  5110. const componentId = data[offset];
  5111. const h = data[offset + 1] >> 4;
  5112. const v = data[offset + 1] & 15;
  5113. if (maxH < h) {
  5114. maxH = h;
  5115. }
  5116. if (maxV < v) {
  5117. maxV = v;
  5118. }
  5119. const qId = data[offset + 2];
  5120. l = frame.components.push({
  5121. h,
  5122. v,
  5123. quantizationId: qId,
  5124. quantizationTable: null
  5125. });
  5126. frame.componentIds[componentId] = l - 1;
  5127. offset += 3;
  5128. }
  5129. frame.maxH = maxH;
  5130. frame.maxV = maxV;
  5131. prepareComponents(frame);
  5132. break;
  5133. case 0xffc4:
  5134. const huffmanLength = (0, _core_utils.readUint16)(data, offset);
  5135. offset += 2;
  5136. for (i = 2; i < huffmanLength;) {
  5137. const huffmanTableSpec = data[offset++];
  5138. const codeLengths = new Uint8Array(16);
  5139. let codeLengthSum = 0;
  5140. for (j = 0; j < 16; j++, offset++) {
  5141. codeLengthSum += codeLengths[j] = data[offset];
  5142. }
  5143. const huffmanValues = new Uint8Array(codeLengthSum);
  5144. for (j = 0; j < codeLengthSum; j++, offset++) {
  5145. huffmanValues[j] = data[offset];
  5146. }
  5147. i += 17 + codeLengthSum;
  5148. (huffmanTableSpec >> 4 === 0 ? huffmanTablesDC : huffmanTablesAC)[huffmanTableSpec & 15] = buildHuffmanTable(codeLengths, huffmanValues);
  5149. }
  5150. break;
  5151. case 0xffdd:
  5152. offset += 2;
  5153. resetInterval = (0, _core_utils.readUint16)(data, offset);
  5154. offset += 2;
  5155. break;
  5156. case 0xffda:
  5157. const parseDNLMarker = ++numSOSMarkers === 1 && !dnlScanLines;
  5158. offset += 2;
  5159. const selectorsCount = data[offset++],
  5160. components = [];
  5161. for (i = 0; i < selectorsCount; i++) {
  5162. const index = data[offset++];
  5163. const componentIndex = frame.componentIds[index];
  5164. const component = frame.components[componentIndex];
  5165. component.index = index;
  5166. const tableSpec = data[offset++];
  5167. component.huffmanTableDC = huffmanTablesDC[tableSpec >> 4];
  5168. component.huffmanTableAC = huffmanTablesAC[tableSpec & 15];
  5169. components.push(component);
  5170. }
  5171. const spectralStart = data[offset++],
  5172. spectralEnd = data[offset++],
  5173. successiveApproximation = data[offset++];
  5174. try {
  5175. const processed = decodeScan(data, offset, frame, components, resetInterval, spectralStart, spectralEnd, successiveApproximation >> 4, successiveApproximation & 15, parseDNLMarker);
  5176. offset += processed;
  5177. } catch (ex) {
  5178. if (ex instanceof DNLMarkerError) {
  5179. (0, _util.warn)(`${ex.message} -- attempting to re-parse the JPEG image.`);
  5180. return this.parse(data, {
  5181. dnlScanLines: ex.scanLines
  5182. });
  5183. } else if (ex instanceof EOIMarkerError) {
  5184. (0, _util.warn)(`${ex.message} -- ignoring the rest of the image data.`);
  5185. break markerLoop;
  5186. }
  5187. throw ex;
  5188. }
  5189. break;
  5190. case 0xffdc:
  5191. offset += 4;
  5192. break;
  5193. case 0xffff:
  5194. if (data[offset] !== 0xff) {
  5195. offset--;
  5196. }
  5197. break;
  5198. default:
  5199. const nextFileMarker = findNextFileMarker(data, offset - 2, offset - 3);
  5200. if (nextFileMarker && nextFileMarker.invalid) {
  5201. (0, _util.warn)("JpegImage.parse - unexpected data, current marker is: " + nextFileMarker.invalid);
  5202. offset = nextFileMarker.offset;
  5203. break;
  5204. }
  5205. if (!nextFileMarker || offset >= data.length - 1) {
  5206. (0, _util.warn)("JpegImage.parse - reached the end of the image data " + "without finding an EOI marker (0xFFD9).");
  5207. break markerLoop;
  5208. }
  5209. throw new JpegError("JpegImage.parse - unknown marker: " + fileMarker.toString(16));
  5210. }
  5211. fileMarker = (0, _core_utils.readUint16)(data, offset);
  5212. offset += 2;
  5213. }
  5214. this.width = frame.samplesPerLine;
  5215. this.height = frame.scanLines;
  5216. this.jfif = jfif;
  5217. this.adobe = adobe;
  5218. this.components = [];
  5219. for (const component of frame.components) {
  5220. const quantizationTable = quantizationTables[component.quantizationId];
  5221. if (quantizationTable) {
  5222. component.quantizationTable = quantizationTable;
  5223. }
  5224. this.components.push({
  5225. index: component.index,
  5226. output: buildComponentData(frame, component),
  5227. scaleX: component.h / frame.maxH,
  5228. scaleY: component.v / frame.maxV,
  5229. blocksPerLine: component.blocksPerLine,
  5230. blocksPerColumn: component.blocksPerColumn
  5231. });
  5232. }
  5233. this.numComponents = this.components.length;
  5234. return undefined;
  5235. }
  5236. _getLinearizedBlockData(width, height, isSourcePDF = false) {
  5237. const scaleX = this.width / width,
  5238. scaleY = this.height / height;
  5239. let component, componentScaleX, componentScaleY, blocksPerScanline;
  5240. let x, y, i, j, k;
  5241. let index;
  5242. let offset = 0;
  5243. let output;
  5244. const numComponents = this.components.length;
  5245. const dataLength = width * height * numComponents;
  5246. const data = new Uint8ClampedArray(dataLength);
  5247. const xScaleBlockOffset = new Uint32Array(width);
  5248. const mask3LSB = 0xfffffff8;
  5249. let lastComponentScaleX;
  5250. for (i = 0; i < numComponents; i++) {
  5251. component = this.components[i];
  5252. componentScaleX = component.scaleX * scaleX;
  5253. componentScaleY = component.scaleY * scaleY;
  5254. offset = i;
  5255. output = component.output;
  5256. blocksPerScanline = component.blocksPerLine + 1 << 3;
  5257. if (componentScaleX !== lastComponentScaleX) {
  5258. for (x = 0; x < width; x++) {
  5259. j = 0 | x * componentScaleX;
  5260. xScaleBlockOffset[x] = (j & mask3LSB) << 3 | j & 7;
  5261. }
  5262. lastComponentScaleX = componentScaleX;
  5263. }
  5264. for (y = 0; y < height; y++) {
  5265. j = 0 | y * componentScaleY;
  5266. index = blocksPerScanline * (j & mask3LSB) | (j & 7) << 3;
  5267. for (x = 0; x < width; x++) {
  5268. data[offset] = output[index + xScaleBlockOffset[x]];
  5269. offset += numComponents;
  5270. }
  5271. }
  5272. }
  5273. let transform = this._decodeTransform;
  5274. if (!isSourcePDF && numComponents === 4 && !transform) {
  5275. transform = new Int32Array([-256, 255, -256, 255, -256, 255, -256, 255]);
  5276. }
  5277. if (transform) {
  5278. for (i = 0; i < dataLength;) {
  5279. for (j = 0, k = 0; j < numComponents; j++, i++, k += 2) {
  5280. data[i] = (data[i] * transform[k] >> 8) + transform[k + 1];
  5281. }
  5282. }
  5283. }
  5284. return data;
  5285. }
  5286. get _isColorConversionNeeded() {
  5287. if (this.adobe) {
  5288. return !!this.adobe.transformCode;
  5289. }
  5290. if (this.numComponents === 3) {
  5291. if (this._colorTransform === 0) {
  5292. return false;
  5293. } else if (this.components[0].index === 0x52 && this.components[1].index === 0x47 && this.components[2].index === 0x42) {
  5294. return false;
  5295. }
  5296. return true;
  5297. }
  5298. if (this._colorTransform === 1) {
  5299. return true;
  5300. }
  5301. return false;
  5302. }
  5303. _convertYccToRgb(data) {
  5304. let Y, Cb, Cr;
  5305. for (let i = 0, length = data.length; i < length; i += 3) {
  5306. Y = data[i];
  5307. Cb = data[i + 1];
  5308. Cr = data[i + 2];
  5309. data[i] = Y - 179.456 + 1.402 * Cr;
  5310. data[i + 1] = Y + 135.459 - 0.344 * Cb - 0.714 * Cr;
  5311. data[i + 2] = Y - 226.816 + 1.772 * Cb;
  5312. }
  5313. return data;
  5314. }
  5315. _convertYcckToRgb(data) {
  5316. let Y, Cb, Cr, k;
  5317. let offset = 0;
  5318. for (let i = 0, length = data.length; i < length; i += 4) {
  5319. Y = data[i];
  5320. Cb = data[i + 1];
  5321. Cr = data[i + 2];
  5322. k = data[i + 3];
  5323. data[offset++] = -122.67195406894 + Cb * (-6.60635669420364e-5 * Cb + 0.000437130475926232 * Cr - 5.4080610064599e-5 * Y + 0.00048449797120281 * k - 0.154362151871126) + Cr * (-0.000957964378445773 * Cr + 0.000817076911346625 * Y - 0.00477271405408747 * k + 1.53380253221734) + Y * (0.000961250184130688 * Y - 0.00266257332283933 * k + 0.48357088451265) + k * (-0.000336197177618394 * k + 0.484791561490776);
  5324. data[offset++] = 107.268039397724 + Cb * (2.19927104525741e-5 * Cb - 0.000640992018297945 * Cr + 0.000659397001245577 * Y + 0.000426105652938837 * k - 0.176491792462875) + Cr * (-0.000778269941513683 * Cr + 0.00130872261408275 * Y + 0.000770482631801132 * k - 0.151051492775562) + Y * (0.00126935368114843 * Y - 0.00265090189010898 * k + 0.25802910206845) + k * (-0.000318913117588328 * k - 0.213742400323665);
  5325. data[offset++] = -20.810012546947 + Cb * (-0.000570115196973677 * Cb - 2.63409051004589e-5 * Cr + 0.0020741088115012 * Y - 0.00288260236853442 * k + 0.814272968359295) + Cr * (-1.53496057440975e-5 * Cr - 0.000132689043961446 * Y + 0.000560833691242812 * k - 0.195152027534049) + Y * (0.00174418132927582 * Y - 0.00255243321439347 * k + 0.116935020465145) + k * (-0.000343531996510555 * k + 0.24165260232407);
  5326. }
  5327. return data.subarray(0, offset);
  5328. }
  5329. _convertYcckToCmyk(data) {
  5330. let Y, Cb, Cr;
  5331. for (let i = 0, length = data.length; i < length; i += 4) {
  5332. Y = data[i];
  5333. Cb = data[i + 1];
  5334. Cr = data[i + 2];
  5335. data[i] = 434.456 - Y - 1.402 * Cr;
  5336. data[i + 1] = 119.541 - Y + 0.344 * Cb + 0.714 * Cr;
  5337. data[i + 2] = 481.816 - Y - 1.772 * Cb;
  5338. }
  5339. return data;
  5340. }
  5341. _convertCmykToRgb(data) {
  5342. let c, m, y, k;
  5343. let offset = 0;
  5344. for (let i = 0, length = data.length; i < length; i += 4) {
  5345. c = data[i];
  5346. m = data[i + 1];
  5347. y = data[i + 2];
  5348. k = data[i + 3];
  5349. data[offset++] = 255 + c * (-0.00006747147073602441 * c + 0.0008379262121013727 * m + 0.0002894718188643294 * y + 0.003264231057537806 * k - 1.1185611867203937) + m * (0.000026374107616089405 * m - 0.00008626949158638572 * y - 0.0002748769067499491 * k - 0.02155688794978967) + y * (-0.00003878099212869363 * y - 0.0003267808279485286 * k + 0.0686742238595345) - k * (0.0003361971776183937 * k + 0.7430659151342254);
  5350. data[offset++] = 255 + c * (0.00013596372813588848 * c + 0.000924537132573585 * m + 0.00010567359618683593 * y + 0.0004791864687436512 * k - 0.3109689587515875) + m * (-0.00023545346108370344 * m + 0.0002702845253534714 * y + 0.0020200308977307156 * k - 0.7488052167015494) + y * (0.00006834815998235662 * y + 0.00015168452363460973 * k - 0.09751927774728933) - k * (0.0003189131175883281 * k + 0.7364883807733168);
  5351. data[offset++] = 255 + c * (0.000013598650411385307 * c + 0.00012423956175490851 * m + 0.0004751985097583589 * y - 0.0000036729317476630422 * k - 0.05562186980264034) + m * (0.00016141380598724676 * m + 0.0009692239130725186 * y + 0.0007782692450036253 * k - 0.44015232367526463) + y * (5.068882914068769e-7 * y + 0.0017778369011375071 * k - 0.7591454649749609) - k * (0.0003435319965105553 * k + 0.7063770186160144);
  5352. }
  5353. return data.subarray(0, offset);
  5354. }
  5355. getData({
  5356. width,
  5357. height,
  5358. forceRGB = false,
  5359. isSourcePDF = false
  5360. }) {
  5361. if (this.numComponents > 4) {
  5362. throw new JpegError("Unsupported color mode");
  5363. }
  5364. const data = this._getLinearizedBlockData(width, height, isSourcePDF);
  5365. if (this.numComponents === 1 && forceRGB) {
  5366. const rgbData = new Uint8ClampedArray(data.length * 3);
  5367. let offset = 0;
  5368. for (const grayColor of data) {
  5369. rgbData[offset++] = grayColor;
  5370. rgbData[offset++] = grayColor;
  5371. rgbData[offset++] = grayColor;
  5372. }
  5373. return rgbData;
  5374. } else if (this.numComponents === 3 && this._isColorConversionNeeded) {
  5375. return this._convertYccToRgb(data);
  5376. } else if (this.numComponents === 4) {
  5377. if (this._isColorConversionNeeded) {
  5378. if (forceRGB) {
  5379. return this._convertYcckToRgb(data);
  5380. }
  5381. return this._convertYcckToCmyk(data);
  5382. } else if (forceRGB) {
  5383. return this._convertCmykToRgb(data);
  5384. }
  5385. }
  5386. return data;
  5387. }
  5388. }
  5389. exports.JpegImage = JpegImage;
  5390. /***/ }),
  5391. /* 9 */
  5392. /***/ ((__unused_webpack_module, exports, __w_pdfjs_require__) => {
  5393. Object.defineProperty(exports, "__esModule", ({
  5394. value: true
  5395. }));
  5396. exports.JpxImage = void 0;
  5397. var _util = __w_pdfjs_require__(1);
  5398. var _core_utils = __w_pdfjs_require__(3);
  5399. var _arithmetic_decoder = __w_pdfjs_require__(6);
  5400. class JpxError extends _util.BaseException {
  5401. constructor(msg) {
  5402. super(`JPX error: ${msg}`, "JpxError");
  5403. }
  5404. }
  5405. const SubbandsGainLog2 = {
  5406. LL: 0,
  5407. LH: 1,
  5408. HL: 1,
  5409. HH: 2
  5410. };
  5411. class JpxImage {
  5412. constructor() {
  5413. this.failOnCorruptedImage = false;
  5414. }
  5415. parse(data) {
  5416. const head = (0, _core_utils.readUint16)(data, 0);
  5417. if (head === 0xff4f) {
  5418. this.parseCodestream(data, 0, data.length);
  5419. return;
  5420. }
  5421. const length = data.length;
  5422. let position = 0;
  5423. while (position < length) {
  5424. let headerSize = 8;
  5425. let lbox = (0, _core_utils.readUint32)(data, position);
  5426. const tbox = (0, _core_utils.readUint32)(data, position + 4);
  5427. position += headerSize;
  5428. if (lbox === 1) {
  5429. lbox = (0, _core_utils.readUint32)(data, position) * 4294967296 + (0, _core_utils.readUint32)(data, position + 4);
  5430. position += 8;
  5431. headerSize += 8;
  5432. }
  5433. if (lbox === 0) {
  5434. lbox = length - position + headerSize;
  5435. }
  5436. if (lbox < headerSize) {
  5437. throw new JpxError("Invalid box field size");
  5438. }
  5439. const dataLength = lbox - headerSize;
  5440. let jumpDataLength = true;
  5441. switch (tbox) {
  5442. case 0x6a703268:
  5443. jumpDataLength = false;
  5444. break;
  5445. case 0x636f6c72:
  5446. const method = data[position];
  5447. if (method === 1) {
  5448. const colorspace = (0, _core_utils.readUint32)(data, position + 3);
  5449. switch (colorspace) {
  5450. case 16:
  5451. case 17:
  5452. case 18:
  5453. break;
  5454. default:
  5455. (0, _util.warn)("Unknown colorspace " + colorspace);
  5456. break;
  5457. }
  5458. } else if (method === 2) {
  5459. (0, _util.info)("ICC profile not supported");
  5460. }
  5461. break;
  5462. case 0x6a703263:
  5463. this.parseCodestream(data, position, position + dataLength);
  5464. break;
  5465. case 0x6a502020:
  5466. if ((0, _core_utils.readUint32)(data, position) !== 0x0d0a870a) {
  5467. (0, _util.warn)("Invalid JP2 signature");
  5468. }
  5469. break;
  5470. case 0x6a501a1a:
  5471. case 0x66747970:
  5472. case 0x72726571:
  5473. case 0x72657320:
  5474. case 0x69686472:
  5475. break;
  5476. default:
  5477. const headerType = String.fromCharCode(tbox >> 24 & 0xff, tbox >> 16 & 0xff, tbox >> 8 & 0xff, tbox & 0xff);
  5478. (0, _util.warn)(`Unsupported header type ${tbox} (${headerType}).`);
  5479. break;
  5480. }
  5481. if (jumpDataLength) {
  5482. position += dataLength;
  5483. }
  5484. }
  5485. }
  5486. parseImageProperties(stream) {
  5487. let newByte = stream.getByte();
  5488. while (newByte >= 0) {
  5489. const oldByte = newByte;
  5490. newByte = stream.getByte();
  5491. const code = oldByte << 8 | newByte;
  5492. if (code === 0xff51) {
  5493. stream.skip(4);
  5494. const Xsiz = stream.getInt32() >>> 0;
  5495. const Ysiz = stream.getInt32() >>> 0;
  5496. const XOsiz = stream.getInt32() >>> 0;
  5497. const YOsiz = stream.getInt32() >>> 0;
  5498. stream.skip(16);
  5499. const Csiz = stream.getUint16();
  5500. this.width = Xsiz - XOsiz;
  5501. this.height = Ysiz - YOsiz;
  5502. this.componentsCount = Csiz;
  5503. this.bitsPerComponent = 8;
  5504. return;
  5505. }
  5506. }
  5507. throw new JpxError("No size marker found in JPX stream");
  5508. }
  5509. parseCodestream(data, start, end) {
  5510. const context = {};
  5511. let doNotRecover = false;
  5512. try {
  5513. let position = start;
  5514. while (position + 1 < end) {
  5515. const code = (0, _core_utils.readUint16)(data, position);
  5516. position += 2;
  5517. let length = 0,
  5518. j,
  5519. sqcd,
  5520. spqcds,
  5521. spqcdSize,
  5522. scalarExpounded,
  5523. tile;
  5524. switch (code) {
  5525. case 0xff4f:
  5526. context.mainHeader = true;
  5527. break;
  5528. case 0xffd9:
  5529. break;
  5530. case 0xff51:
  5531. length = (0, _core_utils.readUint16)(data, position);
  5532. const siz = {};
  5533. siz.Xsiz = (0, _core_utils.readUint32)(data, position + 4);
  5534. siz.Ysiz = (0, _core_utils.readUint32)(data, position + 8);
  5535. siz.XOsiz = (0, _core_utils.readUint32)(data, position + 12);
  5536. siz.YOsiz = (0, _core_utils.readUint32)(data, position + 16);
  5537. siz.XTsiz = (0, _core_utils.readUint32)(data, position + 20);
  5538. siz.YTsiz = (0, _core_utils.readUint32)(data, position + 24);
  5539. siz.XTOsiz = (0, _core_utils.readUint32)(data, position + 28);
  5540. siz.YTOsiz = (0, _core_utils.readUint32)(data, position + 32);
  5541. const componentsCount = (0, _core_utils.readUint16)(data, position + 36);
  5542. siz.Csiz = componentsCount;
  5543. const components = [];
  5544. j = position + 38;
  5545. for (let i = 0; i < componentsCount; i++) {
  5546. const component = {
  5547. precision: (data[j] & 0x7f) + 1,
  5548. isSigned: !!(data[j] & 0x80),
  5549. XRsiz: data[j + 1],
  5550. YRsiz: data[j + 2]
  5551. };
  5552. j += 3;
  5553. calculateComponentDimensions(component, siz);
  5554. components.push(component);
  5555. }
  5556. context.SIZ = siz;
  5557. context.components = components;
  5558. calculateTileGrids(context, components);
  5559. context.QCC = [];
  5560. context.COC = [];
  5561. break;
  5562. case 0xff5c:
  5563. length = (0, _core_utils.readUint16)(data, position);
  5564. const qcd = {};
  5565. j = position + 2;
  5566. sqcd = data[j++];
  5567. switch (sqcd & 0x1f) {
  5568. case 0:
  5569. spqcdSize = 8;
  5570. scalarExpounded = true;
  5571. break;
  5572. case 1:
  5573. spqcdSize = 16;
  5574. scalarExpounded = false;
  5575. break;
  5576. case 2:
  5577. spqcdSize = 16;
  5578. scalarExpounded = true;
  5579. break;
  5580. default:
  5581. throw new Error("Invalid SQcd value " + sqcd);
  5582. }
  5583. qcd.noQuantization = spqcdSize === 8;
  5584. qcd.scalarExpounded = scalarExpounded;
  5585. qcd.guardBits = sqcd >> 5;
  5586. spqcds = [];
  5587. while (j < length + position) {
  5588. const spqcd = {};
  5589. if (spqcdSize === 8) {
  5590. spqcd.epsilon = data[j++] >> 3;
  5591. spqcd.mu = 0;
  5592. } else {
  5593. spqcd.epsilon = data[j] >> 3;
  5594. spqcd.mu = (data[j] & 0x7) << 8 | data[j + 1];
  5595. j += 2;
  5596. }
  5597. spqcds.push(spqcd);
  5598. }
  5599. qcd.SPqcds = spqcds;
  5600. if (context.mainHeader) {
  5601. context.QCD = qcd;
  5602. } else {
  5603. context.currentTile.QCD = qcd;
  5604. context.currentTile.QCC = [];
  5605. }
  5606. break;
  5607. case 0xff5d:
  5608. length = (0, _core_utils.readUint16)(data, position);
  5609. const qcc = {};
  5610. j = position + 2;
  5611. let cqcc;
  5612. if (context.SIZ.Csiz < 257) {
  5613. cqcc = data[j++];
  5614. } else {
  5615. cqcc = (0, _core_utils.readUint16)(data, j);
  5616. j += 2;
  5617. }
  5618. sqcd = data[j++];
  5619. switch (sqcd & 0x1f) {
  5620. case 0:
  5621. spqcdSize = 8;
  5622. scalarExpounded = true;
  5623. break;
  5624. case 1:
  5625. spqcdSize = 16;
  5626. scalarExpounded = false;
  5627. break;
  5628. case 2:
  5629. spqcdSize = 16;
  5630. scalarExpounded = true;
  5631. break;
  5632. default:
  5633. throw new Error("Invalid SQcd value " + sqcd);
  5634. }
  5635. qcc.noQuantization = spqcdSize === 8;
  5636. qcc.scalarExpounded = scalarExpounded;
  5637. qcc.guardBits = sqcd >> 5;
  5638. spqcds = [];
  5639. while (j < length + position) {
  5640. const spqcd = {};
  5641. if (spqcdSize === 8) {
  5642. spqcd.epsilon = data[j++] >> 3;
  5643. spqcd.mu = 0;
  5644. } else {
  5645. spqcd.epsilon = data[j] >> 3;
  5646. spqcd.mu = (data[j] & 0x7) << 8 | data[j + 1];
  5647. j += 2;
  5648. }
  5649. spqcds.push(spqcd);
  5650. }
  5651. qcc.SPqcds = spqcds;
  5652. if (context.mainHeader) {
  5653. context.QCC[cqcc] = qcc;
  5654. } else {
  5655. context.currentTile.QCC[cqcc] = qcc;
  5656. }
  5657. break;
  5658. case 0xff52:
  5659. length = (0, _core_utils.readUint16)(data, position);
  5660. const cod = {};
  5661. j = position + 2;
  5662. const scod = data[j++];
  5663. cod.entropyCoderWithCustomPrecincts = !!(scod & 1);
  5664. cod.sopMarkerUsed = !!(scod & 2);
  5665. cod.ephMarkerUsed = !!(scod & 4);
  5666. cod.progressionOrder = data[j++];
  5667. cod.layersCount = (0, _core_utils.readUint16)(data, j);
  5668. j += 2;
  5669. cod.multipleComponentTransform = data[j++];
  5670. cod.decompositionLevelsCount = data[j++];
  5671. cod.xcb = (data[j++] & 0xf) + 2;
  5672. cod.ycb = (data[j++] & 0xf) + 2;
  5673. const blockStyle = data[j++];
  5674. cod.selectiveArithmeticCodingBypass = !!(blockStyle & 1);
  5675. cod.resetContextProbabilities = !!(blockStyle & 2);
  5676. cod.terminationOnEachCodingPass = !!(blockStyle & 4);
  5677. cod.verticallyStripe = !!(blockStyle & 8);
  5678. cod.predictableTermination = !!(blockStyle & 16);
  5679. cod.segmentationSymbolUsed = !!(blockStyle & 32);
  5680. cod.reversibleTransformation = data[j++];
  5681. if (cod.entropyCoderWithCustomPrecincts) {
  5682. const precinctsSizes = [];
  5683. while (j < length + position) {
  5684. const precinctsSize = data[j++];
  5685. precinctsSizes.push({
  5686. PPx: precinctsSize & 0xf,
  5687. PPy: precinctsSize >> 4
  5688. });
  5689. }
  5690. cod.precinctsSizes = precinctsSizes;
  5691. }
  5692. const unsupported = [];
  5693. if (cod.selectiveArithmeticCodingBypass) {
  5694. unsupported.push("selectiveArithmeticCodingBypass");
  5695. }
  5696. if (cod.terminationOnEachCodingPass) {
  5697. unsupported.push("terminationOnEachCodingPass");
  5698. }
  5699. if (cod.verticallyStripe) {
  5700. unsupported.push("verticallyStripe");
  5701. }
  5702. if (cod.predictableTermination) {
  5703. unsupported.push("predictableTermination");
  5704. }
  5705. if (unsupported.length > 0) {
  5706. doNotRecover = true;
  5707. (0, _util.warn)(`JPX: Unsupported COD options (${unsupported.join(", ")}).`);
  5708. }
  5709. if (context.mainHeader) {
  5710. context.COD = cod;
  5711. } else {
  5712. context.currentTile.COD = cod;
  5713. context.currentTile.COC = [];
  5714. }
  5715. break;
  5716. case 0xff90:
  5717. length = (0, _core_utils.readUint16)(data, position);
  5718. tile = {};
  5719. tile.index = (0, _core_utils.readUint16)(data, position + 2);
  5720. tile.length = (0, _core_utils.readUint32)(data, position + 4);
  5721. tile.dataEnd = tile.length + position - 2;
  5722. tile.partIndex = data[position + 8];
  5723. tile.partsCount = data[position + 9];
  5724. context.mainHeader = false;
  5725. if (tile.partIndex === 0) {
  5726. tile.COD = context.COD;
  5727. tile.COC = context.COC.slice(0);
  5728. tile.QCD = context.QCD;
  5729. tile.QCC = context.QCC.slice(0);
  5730. }
  5731. context.currentTile = tile;
  5732. break;
  5733. case 0xff93:
  5734. tile = context.currentTile;
  5735. if (tile.partIndex === 0) {
  5736. initializeTile(context, tile.index);
  5737. buildPackets(context);
  5738. }
  5739. length = tile.dataEnd - position;
  5740. parseTilePackets(context, data, position, length);
  5741. break;
  5742. case 0xff53:
  5743. (0, _util.warn)("JPX: Codestream code 0xFF53 (COC) is not implemented.");
  5744. case 0xff55:
  5745. case 0xff57:
  5746. case 0xff58:
  5747. case 0xff64:
  5748. length = (0, _core_utils.readUint16)(data, position);
  5749. break;
  5750. default:
  5751. throw new Error("Unknown codestream code: " + code.toString(16));
  5752. }
  5753. position += length;
  5754. }
  5755. } catch (e) {
  5756. if (doNotRecover || this.failOnCorruptedImage) {
  5757. throw new JpxError(e.message);
  5758. } else {
  5759. (0, _util.warn)(`JPX: Trying to recover from: "${e.message}".`);
  5760. }
  5761. }
  5762. this.tiles = transformComponents(context);
  5763. this.width = context.SIZ.Xsiz - context.SIZ.XOsiz;
  5764. this.height = context.SIZ.Ysiz - context.SIZ.YOsiz;
  5765. this.componentsCount = context.SIZ.Csiz;
  5766. }
  5767. }
  5768. exports.JpxImage = JpxImage;
  5769. function calculateComponentDimensions(component, siz) {
  5770. component.x0 = Math.ceil(siz.XOsiz / component.XRsiz);
  5771. component.x1 = Math.ceil(siz.Xsiz / component.XRsiz);
  5772. component.y0 = Math.ceil(siz.YOsiz / component.YRsiz);
  5773. component.y1 = Math.ceil(siz.Ysiz / component.YRsiz);
  5774. component.width = component.x1 - component.x0;
  5775. component.height = component.y1 - component.y0;
  5776. }
  5777. function calculateTileGrids(context, components) {
  5778. const siz = context.SIZ;
  5779. const tiles = [];
  5780. let tile;
  5781. const numXtiles = Math.ceil((siz.Xsiz - siz.XTOsiz) / siz.XTsiz);
  5782. const numYtiles = Math.ceil((siz.Ysiz - siz.YTOsiz) / siz.YTsiz);
  5783. for (let q = 0; q < numYtiles; q++) {
  5784. for (let p = 0; p < numXtiles; p++) {
  5785. tile = {};
  5786. tile.tx0 = Math.max(siz.XTOsiz + p * siz.XTsiz, siz.XOsiz);
  5787. tile.ty0 = Math.max(siz.YTOsiz + q * siz.YTsiz, siz.YOsiz);
  5788. tile.tx1 = Math.min(siz.XTOsiz + (p + 1) * siz.XTsiz, siz.Xsiz);
  5789. tile.ty1 = Math.min(siz.YTOsiz + (q + 1) * siz.YTsiz, siz.Ysiz);
  5790. tile.width = tile.tx1 - tile.tx0;
  5791. tile.height = tile.ty1 - tile.ty0;
  5792. tile.components = [];
  5793. tiles.push(tile);
  5794. }
  5795. }
  5796. context.tiles = tiles;
  5797. const componentsCount = siz.Csiz;
  5798. for (let i = 0, ii = componentsCount; i < ii; i++) {
  5799. const component = components[i];
  5800. for (let j = 0, jj = tiles.length; j < jj; j++) {
  5801. const tileComponent = {};
  5802. tile = tiles[j];
  5803. tileComponent.tcx0 = Math.ceil(tile.tx0 / component.XRsiz);
  5804. tileComponent.tcy0 = Math.ceil(tile.ty0 / component.YRsiz);
  5805. tileComponent.tcx1 = Math.ceil(tile.tx1 / component.XRsiz);
  5806. tileComponent.tcy1 = Math.ceil(tile.ty1 / component.YRsiz);
  5807. tileComponent.width = tileComponent.tcx1 - tileComponent.tcx0;
  5808. tileComponent.height = tileComponent.tcy1 - tileComponent.tcy0;
  5809. tile.components[i] = tileComponent;
  5810. }
  5811. }
  5812. }
  5813. function getBlocksDimensions(context, component, r) {
  5814. const codOrCoc = component.codingStyleParameters;
  5815. const result = {};
  5816. if (!codOrCoc.entropyCoderWithCustomPrecincts) {
  5817. result.PPx = 15;
  5818. result.PPy = 15;
  5819. } else {
  5820. result.PPx = codOrCoc.precinctsSizes[r].PPx;
  5821. result.PPy = codOrCoc.precinctsSizes[r].PPy;
  5822. }
  5823. result.xcb_ = r > 0 ? Math.min(codOrCoc.xcb, result.PPx - 1) : Math.min(codOrCoc.xcb, result.PPx);
  5824. result.ycb_ = r > 0 ? Math.min(codOrCoc.ycb, result.PPy - 1) : Math.min(codOrCoc.ycb, result.PPy);
  5825. return result;
  5826. }
  5827. function buildPrecincts(context, resolution, dimensions) {
  5828. const precinctWidth = 1 << dimensions.PPx;
  5829. const precinctHeight = 1 << dimensions.PPy;
  5830. const isZeroRes = resolution.resLevel === 0;
  5831. const precinctWidthInSubband = 1 << dimensions.PPx + (isZeroRes ? 0 : -1);
  5832. const precinctHeightInSubband = 1 << dimensions.PPy + (isZeroRes ? 0 : -1);
  5833. const numprecinctswide = resolution.trx1 > resolution.trx0 ? Math.ceil(resolution.trx1 / precinctWidth) - Math.floor(resolution.trx0 / precinctWidth) : 0;
  5834. const numprecinctshigh = resolution.try1 > resolution.try0 ? Math.ceil(resolution.try1 / precinctHeight) - Math.floor(resolution.try0 / precinctHeight) : 0;
  5835. const numprecincts = numprecinctswide * numprecinctshigh;
  5836. resolution.precinctParameters = {
  5837. precinctWidth,
  5838. precinctHeight,
  5839. numprecinctswide,
  5840. numprecinctshigh,
  5841. numprecincts,
  5842. precinctWidthInSubband,
  5843. precinctHeightInSubband
  5844. };
  5845. }
  5846. function buildCodeblocks(context, subband, dimensions) {
  5847. const xcb_ = dimensions.xcb_;
  5848. const ycb_ = dimensions.ycb_;
  5849. const codeblockWidth = 1 << xcb_;
  5850. const codeblockHeight = 1 << ycb_;
  5851. const cbx0 = subband.tbx0 >> xcb_;
  5852. const cby0 = subband.tby0 >> ycb_;
  5853. const cbx1 = subband.tbx1 + codeblockWidth - 1 >> xcb_;
  5854. const cby1 = subband.tby1 + codeblockHeight - 1 >> ycb_;
  5855. const precinctParameters = subband.resolution.precinctParameters;
  5856. const codeblocks = [];
  5857. const precincts = [];
  5858. let i, j, codeblock, precinctNumber;
  5859. for (j = cby0; j < cby1; j++) {
  5860. for (i = cbx0; i < cbx1; i++) {
  5861. codeblock = {
  5862. cbx: i,
  5863. cby: j,
  5864. tbx0: codeblockWidth * i,
  5865. tby0: codeblockHeight * j,
  5866. tbx1: codeblockWidth * (i + 1),
  5867. tby1: codeblockHeight * (j + 1)
  5868. };
  5869. codeblock.tbx0_ = Math.max(subband.tbx0, codeblock.tbx0);
  5870. codeblock.tby0_ = Math.max(subband.tby0, codeblock.tby0);
  5871. codeblock.tbx1_ = Math.min(subband.tbx1, codeblock.tbx1);
  5872. codeblock.tby1_ = Math.min(subband.tby1, codeblock.tby1);
  5873. const pi = Math.floor((codeblock.tbx0_ - subband.tbx0) / precinctParameters.precinctWidthInSubband);
  5874. const pj = Math.floor((codeblock.tby0_ - subband.tby0) / precinctParameters.precinctHeightInSubband);
  5875. precinctNumber = pi + pj * precinctParameters.numprecinctswide;
  5876. codeblock.precinctNumber = precinctNumber;
  5877. codeblock.subbandType = subband.type;
  5878. codeblock.Lblock = 3;
  5879. if (codeblock.tbx1_ <= codeblock.tbx0_ || codeblock.tby1_ <= codeblock.tby0_) {
  5880. continue;
  5881. }
  5882. codeblocks.push(codeblock);
  5883. let precinct = precincts[precinctNumber];
  5884. if (precinct !== undefined) {
  5885. if (i < precinct.cbxMin) {
  5886. precinct.cbxMin = i;
  5887. } else if (i > precinct.cbxMax) {
  5888. precinct.cbxMax = i;
  5889. }
  5890. if (j < precinct.cbyMin) {
  5891. precinct.cbxMin = j;
  5892. } else if (j > precinct.cbyMax) {
  5893. precinct.cbyMax = j;
  5894. }
  5895. } else {
  5896. precincts[precinctNumber] = precinct = {
  5897. cbxMin: i,
  5898. cbyMin: j,
  5899. cbxMax: i,
  5900. cbyMax: j
  5901. };
  5902. }
  5903. codeblock.precinct = precinct;
  5904. }
  5905. }
  5906. subband.codeblockParameters = {
  5907. codeblockWidth: xcb_,
  5908. codeblockHeight: ycb_,
  5909. numcodeblockwide: cbx1 - cbx0 + 1,
  5910. numcodeblockhigh: cby1 - cby0 + 1
  5911. };
  5912. subband.codeblocks = codeblocks;
  5913. subband.precincts = precincts;
  5914. }
  5915. function createPacket(resolution, precinctNumber, layerNumber) {
  5916. const precinctCodeblocks = [];
  5917. const subbands = resolution.subbands;
  5918. for (let i = 0, ii = subbands.length; i < ii; i++) {
  5919. const subband = subbands[i];
  5920. const codeblocks = subband.codeblocks;
  5921. for (let j = 0, jj = codeblocks.length; j < jj; j++) {
  5922. const codeblock = codeblocks[j];
  5923. if (codeblock.precinctNumber !== precinctNumber) {
  5924. continue;
  5925. }
  5926. precinctCodeblocks.push(codeblock);
  5927. }
  5928. }
  5929. return {
  5930. layerNumber,
  5931. codeblocks: precinctCodeblocks
  5932. };
  5933. }
  5934. function LayerResolutionComponentPositionIterator(context) {
  5935. const siz = context.SIZ;
  5936. const tileIndex = context.currentTile.index;
  5937. const tile = context.tiles[tileIndex];
  5938. const layersCount = tile.codingStyleDefaultParameters.layersCount;
  5939. const componentsCount = siz.Csiz;
  5940. let maxDecompositionLevelsCount = 0;
  5941. for (let q = 0; q < componentsCount; q++) {
  5942. maxDecompositionLevelsCount = Math.max(maxDecompositionLevelsCount, tile.components[q].codingStyleParameters.decompositionLevelsCount);
  5943. }
  5944. let l = 0,
  5945. r = 0,
  5946. i = 0,
  5947. k = 0;
  5948. this.nextPacket = function JpxImage_nextPacket() {
  5949. for (; l < layersCount; l++) {
  5950. for (; r <= maxDecompositionLevelsCount; r++) {
  5951. for (; i < componentsCount; i++) {
  5952. const component = tile.components[i];
  5953. if (r > component.codingStyleParameters.decompositionLevelsCount) {
  5954. continue;
  5955. }
  5956. const resolution = component.resolutions[r];
  5957. const numprecincts = resolution.precinctParameters.numprecincts;
  5958. for (; k < numprecincts;) {
  5959. const packet = createPacket(resolution, k, l);
  5960. k++;
  5961. return packet;
  5962. }
  5963. k = 0;
  5964. }
  5965. i = 0;
  5966. }
  5967. r = 0;
  5968. }
  5969. throw new JpxError("Out of packets");
  5970. };
  5971. }
  5972. function ResolutionLayerComponentPositionIterator(context) {
  5973. const siz = context.SIZ;
  5974. const tileIndex = context.currentTile.index;
  5975. const tile = context.tiles[tileIndex];
  5976. const layersCount = tile.codingStyleDefaultParameters.layersCount;
  5977. const componentsCount = siz.Csiz;
  5978. let maxDecompositionLevelsCount = 0;
  5979. for (let q = 0; q < componentsCount; q++) {
  5980. maxDecompositionLevelsCount = Math.max(maxDecompositionLevelsCount, tile.components[q].codingStyleParameters.decompositionLevelsCount);
  5981. }
  5982. let r = 0,
  5983. l = 0,
  5984. i = 0,
  5985. k = 0;
  5986. this.nextPacket = function JpxImage_nextPacket() {
  5987. for (; r <= maxDecompositionLevelsCount; r++) {
  5988. for (; l < layersCount; l++) {
  5989. for (; i < componentsCount; i++) {
  5990. const component = tile.components[i];
  5991. if (r > component.codingStyleParameters.decompositionLevelsCount) {
  5992. continue;
  5993. }
  5994. const resolution = component.resolutions[r];
  5995. const numprecincts = resolution.precinctParameters.numprecincts;
  5996. for (; k < numprecincts;) {
  5997. const packet = createPacket(resolution, k, l);
  5998. k++;
  5999. return packet;
  6000. }
  6001. k = 0;
  6002. }
  6003. i = 0;
  6004. }
  6005. l = 0;
  6006. }
  6007. throw new JpxError("Out of packets");
  6008. };
  6009. }
  6010. function ResolutionPositionComponentLayerIterator(context) {
  6011. const siz = context.SIZ;
  6012. const tileIndex = context.currentTile.index;
  6013. const tile = context.tiles[tileIndex];
  6014. const layersCount = tile.codingStyleDefaultParameters.layersCount;
  6015. const componentsCount = siz.Csiz;
  6016. let l, r, c, p;
  6017. let maxDecompositionLevelsCount = 0;
  6018. for (c = 0; c < componentsCount; c++) {
  6019. const component = tile.components[c];
  6020. maxDecompositionLevelsCount = Math.max(maxDecompositionLevelsCount, component.codingStyleParameters.decompositionLevelsCount);
  6021. }
  6022. const maxNumPrecinctsInLevel = new Int32Array(maxDecompositionLevelsCount + 1);
  6023. for (r = 0; r <= maxDecompositionLevelsCount; ++r) {
  6024. let maxNumPrecincts = 0;
  6025. for (c = 0; c < componentsCount; ++c) {
  6026. const resolutions = tile.components[c].resolutions;
  6027. if (r < resolutions.length) {
  6028. maxNumPrecincts = Math.max(maxNumPrecincts, resolutions[r].precinctParameters.numprecincts);
  6029. }
  6030. }
  6031. maxNumPrecinctsInLevel[r] = maxNumPrecincts;
  6032. }
  6033. l = 0;
  6034. r = 0;
  6035. c = 0;
  6036. p = 0;
  6037. this.nextPacket = function JpxImage_nextPacket() {
  6038. for (; r <= maxDecompositionLevelsCount; r++) {
  6039. for (; p < maxNumPrecinctsInLevel[r]; p++) {
  6040. for (; c < componentsCount; c++) {
  6041. const component = tile.components[c];
  6042. if (r > component.codingStyleParameters.decompositionLevelsCount) {
  6043. continue;
  6044. }
  6045. const resolution = component.resolutions[r];
  6046. const numprecincts = resolution.precinctParameters.numprecincts;
  6047. if (p >= numprecincts) {
  6048. continue;
  6049. }
  6050. for (; l < layersCount;) {
  6051. const packet = createPacket(resolution, p, l);
  6052. l++;
  6053. return packet;
  6054. }
  6055. l = 0;
  6056. }
  6057. c = 0;
  6058. }
  6059. p = 0;
  6060. }
  6061. throw new JpxError("Out of packets");
  6062. };
  6063. }
  6064. function PositionComponentResolutionLayerIterator(context) {
  6065. const siz = context.SIZ;
  6066. const tileIndex = context.currentTile.index;
  6067. const tile = context.tiles[tileIndex];
  6068. const layersCount = tile.codingStyleDefaultParameters.layersCount;
  6069. const componentsCount = siz.Csiz;
  6070. const precinctsSizes = getPrecinctSizesInImageScale(tile);
  6071. const precinctsIterationSizes = precinctsSizes;
  6072. let l = 0,
  6073. r = 0,
  6074. c = 0,
  6075. px = 0,
  6076. py = 0;
  6077. this.nextPacket = function JpxImage_nextPacket() {
  6078. for (; py < precinctsIterationSizes.maxNumHigh; py++) {
  6079. for (; px < precinctsIterationSizes.maxNumWide; px++) {
  6080. for (; c < componentsCount; c++) {
  6081. const component = tile.components[c];
  6082. const decompositionLevelsCount = component.codingStyleParameters.decompositionLevelsCount;
  6083. for (; r <= decompositionLevelsCount; r++) {
  6084. const resolution = component.resolutions[r];
  6085. const sizeInImageScale = precinctsSizes.components[c].resolutions[r];
  6086. const k = getPrecinctIndexIfExist(px, py, sizeInImageScale, precinctsIterationSizes, resolution);
  6087. if (k === null) {
  6088. continue;
  6089. }
  6090. for (; l < layersCount;) {
  6091. const packet = createPacket(resolution, k, l);
  6092. l++;
  6093. return packet;
  6094. }
  6095. l = 0;
  6096. }
  6097. r = 0;
  6098. }
  6099. c = 0;
  6100. }
  6101. px = 0;
  6102. }
  6103. throw new JpxError("Out of packets");
  6104. };
  6105. }
  6106. function ComponentPositionResolutionLayerIterator(context) {
  6107. const siz = context.SIZ;
  6108. const tileIndex = context.currentTile.index;
  6109. const tile = context.tiles[tileIndex];
  6110. const layersCount = tile.codingStyleDefaultParameters.layersCount;
  6111. const componentsCount = siz.Csiz;
  6112. const precinctsSizes = getPrecinctSizesInImageScale(tile);
  6113. let l = 0,
  6114. r = 0,
  6115. c = 0,
  6116. px = 0,
  6117. py = 0;
  6118. this.nextPacket = function JpxImage_nextPacket() {
  6119. for (; c < componentsCount; ++c) {
  6120. const component = tile.components[c];
  6121. const precinctsIterationSizes = precinctsSizes.components[c];
  6122. const decompositionLevelsCount = component.codingStyleParameters.decompositionLevelsCount;
  6123. for (; py < precinctsIterationSizes.maxNumHigh; py++) {
  6124. for (; px < precinctsIterationSizes.maxNumWide; px++) {
  6125. for (; r <= decompositionLevelsCount; r++) {
  6126. const resolution = component.resolutions[r];
  6127. const sizeInImageScale = precinctsIterationSizes.resolutions[r];
  6128. const k = getPrecinctIndexIfExist(px, py, sizeInImageScale, precinctsIterationSizes, resolution);
  6129. if (k === null) {
  6130. continue;
  6131. }
  6132. for (; l < layersCount;) {
  6133. const packet = createPacket(resolution, k, l);
  6134. l++;
  6135. return packet;
  6136. }
  6137. l = 0;
  6138. }
  6139. r = 0;
  6140. }
  6141. px = 0;
  6142. }
  6143. py = 0;
  6144. }
  6145. throw new JpxError("Out of packets");
  6146. };
  6147. }
  6148. function getPrecinctIndexIfExist(pxIndex, pyIndex, sizeInImageScale, precinctIterationSizes, resolution) {
  6149. const posX = pxIndex * precinctIterationSizes.minWidth;
  6150. const posY = pyIndex * precinctIterationSizes.minHeight;
  6151. if (posX % sizeInImageScale.width !== 0 || posY % sizeInImageScale.height !== 0) {
  6152. return null;
  6153. }
  6154. const startPrecinctRowIndex = posY / sizeInImageScale.width * resolution.precinctParameters.numprecinctswide;
  6155. return posX / sizeInImageScale.height + startPrecinctRowIndex;
  6156. }
  6157. function getPrecinctSizesInImageScale(tile) {
  6158. const componentsCount = tile.components.length;
  6159. let minWidth = Number.MAX_VALUE;
  6160. let minHeight = Number.MAX_VALUE;
  6161. let maxNumWide = 0;
  6162. let maxNumHigh = 0;
  6163. const sizePerComponent = new Array(componentsCount);
  6164. for (let c = 0; c < componentsCount; c++) {
  6165. const component = tile.components[c];
  6166. const decompositionLevelsCount = component.codingStyleParameters.decompositionLevelsCount;
  6167. const sizePerResolution = new Array(decompositionLevelsCount + 1);
  6168. let minWidthCurrentComponent = Number.MAX_VALUE;
  6169. let minHeightCurrentComponent = Number.MAX_VALUE;
  6170. let maxNumWideCurrentComponent = 0;
  6171. let maxNumHighCurrentComponent = 0;
  6172. let scale = 1;
  6173. for (let r = decompositionLevelsCount; r >= 0; --r) {
  6174. const resolution = component.resolutions[r];
  6175. const widthCurrentResolution = scale * resolution.precinctParameters.precinctWidth;
  6176. const heightCurrentResolution = scale * resolution.precinctParameters.precinctHeight;
  6177. minWidthCurrentComponent = Math.min(minWidthCurrentComponent, widthCurrentResolution);
  6178. minHeightCurrentComponent = Math.min(minHeightCurrentComponent, heightCurrentResolution);
  6179. maxNumWideCurrentComponent = Math.max(maxNumWideCurrentComponent, resolution.precinctParameters.numprecinctswide);
  6180. maxNumHighCurrentComponent = Math.max(maxNumHighCurrentComponent, resolution.precinctParameters.numprecinctshigh);
  6181. sizePerResolution[r] = {
  6182. width: widthCurrentResolution,
  6183. height: heightCurrentResolution
  6184. };
  6185. scale <<= 1;
  6186. }
  6187. minWidth = Math.min(minWidth, minWidthCurrentComponent);
  6188. minHeight = Math.min(minHeight, minHeightCurrentComponent);
  6189. maxNumWide = Math.max(maxNumWide, maxNumWideCurrentComponent);
  6190. maxNumHigh = Math.max(maxNumHigh, maxNumHighCurrentComponent);
  6191. sizePerComponent[c] = {
  6192. resolutions: sizePerResolution,
  6193. minWidth: minWidthCurrentComponent,
  6194. minHeight: minHeightCurrentComponent,
  6195. maxNumWide: maxNumWideCurrentComponent,
  6196. maxNumHigh: maxNumHighCurrentComponent
  6197. };
  6198. }
  6199. return {
  6200. components: sizePerComponent,
  6201. minWidth,
  6202. minHeight,
  6203. maxNumWide,
  6204. maxNumHigh
  6205. };
  6206. }
  6207. function buildPackets(context) {
  6208. const siz = context.SIZ;
  6209. const tileIndex = context.currentTile.index;
  6210. const tile = context.tiles[tileIndex];
  6211. const componentsCount = siz.Csiz;
  6212. for (let c = 0; c < componentsCount; c++) {
  6213. const component = tile.components[c];
  6214. const decompositionLevelsCount = component.codingStyleParameters.decompositionLevelsCount;
  6215. const resolutions = [];
  6216. const subbands = [];
  6217. for (let r = 0; r <= decompositionLevelsCount; r++) {
  6218. const blocksDimensions = getBlocksDimensions(context, component, r);
  6219. const resolution = {};
  6220. const scale = 1 << decompositionLevelsCount - r;
  6221. resolution.trx0 = Math.ceil(component.tcx0 / scale);
  6222. resolution.try0 = Math.ceil(component.tcy0 / scale);
  6223. resolution.trx1 = Math.ceil(component.tcx1 / scale);
  6224. resolution.try1 = Math.ceil(component.tcy1 / scale);
  6225. resolution.resLevel = r;
  6226. buildPrecincts(context, resolution, blocksDimensions);
  6227. resolutions.push(resolution);
  6228. let subband;
  6229. if (r === 0) {
  6230. subband = {};
  6231. subband.type = "LL";
  6232. subband.tbx0 = Math.ceil(component.tcx0 / scale);
  6233. subband.tby0 = Math.ceil(component.tcy0 / scale);
  6234. subband.tbx1 = Math.ceil(component.tcx1 / scale);
  6235. subband.tby1 = Math.ceil(component.tcy1 / scale);
  6236. subband.resolution = resolution;
  6237. buildCodeblocks(context, subband, blocksDimensions);
  6238. subbands.push(subband);
  6239. resolution.subbands = [subband];
  6240. } else {
  6241. const bscale = 1 << decompositionLevelsCount - r + 1;
  6242. const resolutionSubbands = [];
  6243. subband = {};
  6244. subband.type = "HL";
  6245. subband.tbx0 = Math.ceil(component.tcx0 / bscale - 0.5);
  6246. subband.tby0 = Math.ceil(component.tcy0 / bscale);
  6247. subband.tbx1 = Math.ceil(component.tcx1 / bscale - 0.5);
  6248. subband.tby1 = Math.ceil(component.tcy1 / bscale);
  6249. subband.resolution = resolution;
  6250. buildCodeblocks(context, subband, blocksDimensions);
  6251. subbands.push(subband);
  6252. resolutionSubbands.push(subband);
  6253. subband = {};
  6254. subband.type = "LH";
  6255. subband.tbx0 = Math.ceil(component.tcx0 / bscale);
  6256. subband.tby0 = Math.ceil(component.tcy0 / bscale - 0.5);
  6257. subband.tbx1 = Math.ceil(component.tcx1 / bscale);
  6258. subband.tby1 = Math.ceil(component.tcy1 / bscale - 0.5);
  6259. subband.resolution = resolution;
  6260. buildCodeblocks(context, subband, blocksDimensions);
  6261. subbands.push(subband);
  6262. resolutionSubbands.push(subband);
  6263. subband = {};
  6264. subband.type = "HH";
  6265. subband.tbx0 = Math.ceil(component.tcx0 / bscale - 0.5);
  6266. subband.tby0 = Math.ceil(component.tcy0 / bscale - 0.5);
  6267. subband.tbx1 = Math.ceil(component.tcx1 / bscale - 0.5);
  6268. subband.tby1 = Math.ceil(component.tcy1 / bscale - 0.5);
  6269. subband.resolution = resolution;
  6270. buildCodeblocks(context, subband, blocksDimensions);
  6271. subbands.push(subband);
  6272. resolutionSubbands.push(subband);
  6273. resolution.subbands = resolutionSubbands;
  6274. }
  6275. }
  6276. component.resolutions = resolutions;
  6277. component.subbands = subbands;
  6278. }
  6279. const progressionOrder = tile.codingStyleDefaultParameters.progressionOrder;
  6280. switch (progressionOrder) {
  6281. case 0:
  6282. tile.packetsIterator = new LayerResolutionComponentPositionIterator(context);
  6283. break;
  6284. case 1:
  6285. tile.packetsIterator = new ResolutionLayerComponentPositionIterator(context);
  6286. break;
  6287. case 2:
  6288. tile.packetsIterator = new ResolutionPositionComponentLayerIterator(context);
  6289. break;
  6290. case 3:
  6291. tile.packetsIterator = new PositionComponentResolutionLayerIterator(context);
  6292. break;
  6293. case 4:
  6294. tile.packetsIterator = new ComponentPositionResolutionLayerIterator(context);
  6295. break;
  6296. default:
  6297. throw new JpxError(`Unsupported progression order ${progressionOrder}`);
  6298. }
  6299. }
  6300. function parseTilePackets(context, data, offset, dataLength) {
  6301. let position = 0;
  6302. let buffer,
  6303. bufferSize = 0,
  6304. skipNextBit = false;
  6305. function readBits(count) {
  6306. while (bufferSize < count) {
  6307. const b = data[offset + position];
  6308. position++;
  6309. if (skipNextBit) {
  6310. buffer = buffer << 7 | b;
  6311. bufferSize += 7;
  6312. skipNextBit = false;
  6313. } else {
  6314. buffer = buffer << 8 | b;
  6315. bufferSize += 8;
  6316. }
  6317. if (b === 0xff) {
  6318. skipNextBit = true;
  6319. }
  6320. }
  6321. bufferSize -= count;
  6322. return buffer >>> bufferSize & (1 << count) - 1;
  6323. }
  6324. function skipMarkerIfEqual(value) {
  6325. if (data[offset + position - 1] === 0xff && data[offset + position] === value) {
  6326. skipBytes(1);
  6327. return true;
  6328. } else if (data[offset + position] === 0xff && data[offset + position + 1] === value) {
  6329. skipBytes(2);
  6330. return true;
  6331. }
  6332. return false;
  6333. }
  6334. function skipBytes(count) {
  6335. position += count;
  6336. }
  6337. function alignToByte() {
  6338. bufferSize = 0;
  6339. if (skipNextBit) {
  6340. position++;
  6341. skipNextBit = false;
  6342. }
  6343. }
  6344. function readCodingpasses() {
  6345. if (readBits(1) === 0) {
  6346. return 1;
  6347. }
  6348. if (readBits(1) === 0) {
  6349. return 2;
  6350. }
  6351. let value = readBits(2);
  6352. if (value < 3) {
  6353. return value + 3;
  6354. }
  6355. value = readBits(5);
  6356. if (value < 31) {
  6357. return value + 6;
  6358. }
  6359. value = readBits(7);
  6360. return value + 37;
  6361. }
  6362. const tileIndex = context.currentTile.index;
  6363. const tile = context.tiles[tileIndex];
  6364. const sopMarkerUsed = context.COD.sopMarkerUsed;
  6365. const ephMarkerUsed = context.COD.ephMarkerUsed;
  6366. const packetsIterator = tile.packetsIterator;
  6367. while (position < dataLength) {
  6368. alignToByte();
  6369. if (sopMarkerUsed && skipMarkerIfEqual(0x91)) {
  6370. skipBytes(4);
  6371. }
  6372. const packet = packetsIterator.nextPacket();
  6373. if (!readBits(1)) {
  6374. continue;
  6375. }
  6376. const layerNumber = packet.layerNumber,
  6377. queue = [];
  6378. let codeblock;
  6379. for (let i = 0, ii = packet.codeblocks.length; i < ii; i++) {
  6380. codeblock = packet.codeblocks[i];
  6381. let precinct = codeblock.precinct;
  6382. const codeblockColumn = codeblock.cbx - precinct.cbxMin;
  6383. const codeblockRow = codeblock.cby - precinct.cbyMin;
  6384. let codeblockIncluded = false;
  6385. let firstTimeInclusion = false;
  6386. let valueReady, zeroBitPlanesTree;
  6387. if (codeblock.included !== undefined) {
  6388. codeblockIncluded = !!readBits(1);
  6389. } else {
  6390. precinct = codeblock.precinct;
  6391. let inclusionTree;
  6392. if (precinct.inclusionTree !== undefined) {
  6393. inclusionTree = precinct.inclusionTree;
  6394. } else {
  6395. const width = precinct.cbxMax - precinct.cbxMin + 1;
  6396. const height = precinct.cbyMax - precinct.cbyMin + 1;
  6397. inclusionTree = new InclusionTree(width, height, layerNumber);
  6398. zeroBitPlanesTree = new TagTree(width, height);
  6399. precinct.inclusionTree = inclusionTree;
  6400. precinct.zeroBitPlanesTree = zeroBitPlanesTree;
  6401. for (let l = 0; l < layerNumber; l++) {
  6402. if (readBits(1) !== 0) {
  6403. throw new JpxError("Invalid tag tree");
  6404. }
  6405. }
  6406. }
  6407. if (inclusionTree.reset(codeblockColumn, codeblockRow, layerNumber)) {
  6408. while (true) {
  6409. if (readBits(1)) {
  6410. valueReady = !inclusionTree.nextLevel();
  6411. if (valueReady) {
  6412. codeblock.included = true;
  6413. codeblockIncluded = firstTimeInclusion = true;
  6414. break;
  6415. }
  6416. } else {
  6417. inclusionTree.incrementValue(layerNumber);
  6418. break;
  6419. }
  6420. }
  6421. }
  6422. }
  6423. if (!codeblockIncluded) {
  6424. continue;
  6425. }
  6426. if (firstTimeInclusion) {
  6427. zeroBitPlanesTree = precinct.zeroBitPlanesTree;
  6428. zeroBitPlanesTree.reset(codeblockColumn, codeblockRow);
  6429. while (true) {
  6430. if (readBits(1)) {
  6431. valueReady = !zeroBitPlanesTree.nextLevel();
  6432. if (valueReady) {
  6433. break;
  6434. }
  6435. } else {
  6436. zeroBitPlanesTree.incrementValue();
  6437. }
  6438. }
  6439. codeblock.zeroBitPlanes = zeroBitPlanesTree.value;
  6440. }
  6441. const codingpasses = readCodingpasses();
  6442. while (readBits(1)) {
  6443. codeblock.Lblock++;
  6444. }
  6445. const codingpassesLog2 = (0, _core_utils.log2)(codingpasses);
  6446. const bits = (codingpasses < 1 << codingpassesLog2 ? codingpassesLog2 - 1 : codingpassesLog2) + codeblock.Lblock;
  6447. const codedDataLength = readBits(bits);
  6448. queue.push({
  6449. codeblock,
  6450. codingpasses,
  6451. dataLength: codedDataLength
  6452. });
  6453. }
  6454. alignToByte();
  6455. if (ephMarkerUsed) {
  6456. skipMarkerIfEqual(0x92);
  6457. }
  6458. while (queue.length > 0) {
  6459. const packetItem = queue.shift();
  6460. codeblock = packetItem.codeblock;
  6461. if (codeblock.data === undefined) {
  6462. codeblock.data = [];
  6463. }
  6464. codeblock.data.push({
  6465. data,
  6466. start: offset + position,
  6467. end: offset + position + packetItem.dataLength,
  6468. codingpasses: packetItem.codingpasses
  6469. });
  6470. position += packetItem.dataLength;
  6471. }
  6472. }
  6473. return position;
  6474. }
  6475. function copyCoefficients(coefficients, levelWidth, levelHeight, subband, delta, mb, reversible, segmentationSymbolUsed, resetContextProbabilities) {
  6476. const x0 = subband.tbx0;
  6477. const y0 = subband.tby0;
  6478. const width = subband.tbx1 - subband.tbx0;
  6479. const codeblocks = subband.codeblocks;
  6480. const right = subband.type.charAt(0) === "H" ? 1 : 0;
  6481. const bottom = subband.type.charAt(1) === "H" ? levelWidth : 0;
  6482. for (let i = 0, ii = codeblocks.length; i < ii; ++i) {
  6483. const codeblock = codeblocks[i];
  6484. const blockWidth = codeblock.tbx1_ - codeblock.tbx0_;
  6485. const blockHeight = codeblock.tby1_ - codeblock.tby0_;
  6486. if (blockWidth === 0 || blockHeight === 0) {
  6487. continue;
  6488. }
  6489. if (codeblock.data === undefined) {
  6490. continue;
  6491. }
  6492. const bitModel = new BitModel(blockWidth, blockHeight, codeblock.subbandType, codeblock.zeroBitPlanes, mb);
  6493. let currentCodingpassType = 2;
  6494. const data = codeblock.data;
  6495. let totalLength = 0,
  6496. codingpasses = 0;
  6497. let j, jj, dataItem;
  6498. for (j = 0, jj = data.length; j < jj; j++) {
  6499. dataItem = data[j];
  6500. totalLength += dataItem.end - dataItem.start;
  6501. codingpasses += dataItem.codingpasses;
  6502. }
  6503. const encodedData = new Uint8Array(totalLength);
  6504. let position = 0;
  6505. for (j = 0, jj = data.length; j < jj; j++) {
  6506. dataItem = data[j];
  6507. const chunk = dataItem.data.subarray(dataItem.start, dataItem.end);
  6508. encodedData.set(chunk, position);
  6509. position += chunk.length;
  6510. }
  6511. const decoder = new _arithmetic_decoder.ArithmeticDecoder(encodedData, 0, totalLength);
  6512. bitModel.setDecoder(decoder);
  6513. for (j = 0; j < codingpasses; j++) {
  6514. switch (currentCodingpassType) {
  6515. case 0:
  6516. bitModel.runSignificancePropagationPass();
  6517. break;
  6518. case 1:
  6519. bitModel.runMagnitudeRefinementPass();
  6520. break;
  6521. case 2:
  6522. bitModel.runCleanupPass();
  6523. if (segmentationSymbolUsed) {
  6524. bitModel.checkSegmentationSymbol();
  6525. }
  6526. break;
  6527. }
  6528. if (resetContextProbabilities) {
  6529. bitModel.reset();
  6530. }
  6531. currentCodingpassType = (currentCodingpassType + 1) % 3;
  6532. }
  6533. let offset = codeblock.tbx0_ - x0 + (codeblock.tby0_ - y0) * width;
  6534. const sign = bitModel.coefficentsSign;
  6535. const magnitude = bitModel.coefficentsMagnitude;
  6536. const bitsDecoded = bitModel.bitsDecoded;
  6537. const magnitudeCorrection = reversible ? 0 : 0.5;
  6538. let k, n, nb;
  6539. position = 0;
  6540. const interleave = subband.type !== "LL";
  6541. for (j = 0; j < blockHeight; j++) {
  6542. const row = offset / width | 0;
  6543. const levelOffset = 2 * row * (levelWidth - width) + right + bottom;
  6544. for (k = 0; k < blockWidth; k++) {
  6545. n = magnitude[position];
  6546. if (n !== 0) {
  6547. n = (n + magnitudeCorrection) * delta;
  6548. if (sign[position] !== 0) {
  6549. n = -n;
  6550. }
  6551. nb = bitsDecoded[position];
  6552. const pos = interleave ? levelOffset + (offset << 1) : offset;
  6553. if (reversible && nb >= mb) {
  6554. coefficients[pos] = n;
  6555. } else {
  6556. coefficients[pos] = n * (1 << mb - nb);
  6557. }
  6558. }
  6559. offset++;
  6560. position++;
  6561. }
  6562. offset += width - blockWidth;
  6563. }
  6564. }
  6565. }
  6566. function transformTile(context, tile, c) {
  6567. const component = tile.components[c];
  6568. const codingStyleParameters = component.codingStyleParameters;
  6569. const quantizationParameters = component.quantizationParameters;
  6570. const decompositionLevelsCount = codingStyleParameters.decompositionLevelsCount;
  6571. const spqcds = quantizationParameters.SPqcds;
  6572. const scalarExpounded = quantizationParameters.scalarExpounded;
  6573. const guardBits = quantizationParameters.guardBits;
  6574. const segmentationSymbolUsed = codingStyleParameters.segmentationSymbolUsed;
  6575. const resetContextProbabilities = codingStyleParameters.resetContextProbabilities;
  6576. const precision = context.components[c].precision;
  6577. const reversible = codingStyleParameters.reversibleTransformation;
  6578. const transform = reversible ? new ReversibleTransform() : new IrreversibleTransform();
  6579. const subbandCoefficients = [];
  6580. let b = 0;
  6581. for (let i = 0; i <= decompositionLevelsCount; i++) {
  6582. const resolution = component.resolutions[i];
  6583. const width = resolution.trx1 - resolution.trx0;
  6584. const height = resolution.try1 - resolution.try0;
  6585. const coefficients = new Float32Array(width * height);
  6586. for (let j = 0, jj = resolution.subbands.length; j < jj; j++) {
  6587. let mu, epsilon;
  6588. if (!scalarExpounded) {
  6589. mu = spqcds[0].mu;
  6590. epsilon = spqcds[0].epsilon + (i > 0 ? 1 - i : 0);
  6591. } else {
  6592. mu = spqcds[b].mu;
  6593. epsilon = spqcds[b].epsilon;
  6594. b++;
  6595. }
  6596. const subband = resolution.subbands[j];
  6597. const gainLog2 = SubbandsGainLog2[subband.type];
  6598. const delta = reversible ? 1 : 2 ** (precision + gainLog2 - epsilon) * (1 + mu / 2048);
  6599. const mb = guardBits + epsilon - 1;
  6600. copyCoefficients(coefficients, width, height, subband, delta, mb, reversible, segmentationSymbolUsed, resetContextProbabilities);
  6601. }
  6602. subbandCoefficients.push({
  6603. width,
  6604. height,
  6605. items: coefficients
  6606. });
  6607. }
  6608. const result = transform.calculate(subbandCoefficients, component.tcx0, component.tcy0);
  6609. return {
  6610. left: component.tcx0,
  6611. top: component.tcy0,
  6612. width: result.width,
  6613. height: result.height,
  6614. items: result.items
  6615. };
  6616. }
  6617. function transformComponents(context) {
  6618. const siz = context.SIZ;
  6619. const components = context.components;
  6620. const componentsCount = siz.Csiz;
  6621. const resultImages = [];
  6622. for (let i = 0, ii = context.tiles.length; i < ii; i++) {
  6623. const tile = context.tiles[i];
  6624. const transformedTiles = [];
  6625. for (let c = 0; c < componentsCount; c++) {
  6626. transformedTiles[c] = transformTile(context, tile, c);
  6627. }
  6628. const tile0 = transformedTiles[0];
  6629. const out = new Uint8ClampedArray(tile0.items.length * componentsCount);
  6630. const result = {
  6631. left: tile0.left,
  6632. top: tile0.top,
  6633. width: tile0.width,
  6634. height: tile0.height,
  6635. items: out
  6636. };
  6637. let shift, offset;
  6638. let pos = 0,
  6639. j,
  6640. jj,
  6641. y0,
  6642. y1,
  6643. y2;
  6644. if (tile.codingStyleDefaultParameters.multipleComponentTransform) {
  6645. const fourComponents = componentsCount === 4;
  6646. const y0items = transformedTiles[0].items;
  6647. const y1items = transformedTiles[1].items;
  6648. const y2items = transformedTiles[2].items;
  6649. const y3items = fourComponents ? transformedTiles[3].items : null;
  6650. shift = components[0].precision - 8;
  6651. offset = (128 << shift) + 0.5;
  6652. const component0 = tile.components[0];
  6653. const alpha01 = componentsCount - 3;
  6654. jj = y0items.length;
  6655. if (!component0.codingStyleParameters.reversibleTransformation) {
  6656. for (j = 0; j < jj; j++, pos += alpha01) {
  6657. y0 = y0items[j] + offset;
  6658. y1 = y1items[j];
  6659. y2 = y2items[j];
  6660. out[pos++] = y0 + 1.402 * y2 >> shift;
  6661. out[pos++] = y0 - 0.34413 * y1 - 0.71414 * y2 >> shift;
  6662. out[pos++] = y0 + 1.772 * y1 >> shift;
  6663. }
  6664. } else {
  6665. for (j = 0; j < jj; j++, pos += alpha01) {
  6666. y0 = y0items[j] + offset;
  6667. y1 = y1items[j];
  6668. y2 = y2items[j];
  6669. const g = y0 - (y2 + y1 >> 2);
  6670. out[pos++] = g + y2 >> shift;
  6671. out[pos++] = g >> shift;
  6672. out[pos++] = g + y1 >> shift;
  6673. }
  6674. }
  6675. if (fourComponents) {
  6676. for (j = 0, pos = 3; j < jj; j++, pos += 4) {
  6677. out[pos] = y3items[j] + offset >> shift;
  6678. }
  6679. }
  6680. } else {
  6681. for (let c = 0; c < componentsCount; c++) {
  6682. const items = transformedTiles[c].items;
  6683. shift = components[c].precision - 8;
  6684. offset = (128 << shift) + 0.5;
  6685. for (pos = c, j = 0, jj = items.length; j < jj; j++) {
  6686. out[pos] = items[j] + offset >> shift;
  6687. pos += componentsCount;
  6688. }
  6689. }
  6690. }
  6691. resultImages.push(result);
  6692. }
  6693. return resultImages;
  6694. }
  6695. function initializeTile(context, tileIndex) {
  6696. const siz = context.SIZ;
  6697. const componentsCount = siz.Csiz;
  6698. const tile = context.tiles[tileIndex];
  6699. for (let c = 0; c < componentsCount; c++) {
  6700. const component = tile.components[c];
  6701. const qcdOrQcc = context.currentTile.QCC[c] !== undefined ? context.currentTile.QCC[c] : context.currentTile.QCD;
  6702. component.quantizationParameters = qcdOrQcc;
  6703. const codOrCoc = context.currentTile.COC[c] !== undefined ? context.currentTile.COC[c] : context.currentTile.COD;
  6704. component.codingStyleParameters = codOrCoc;
  6705. }
  6706. tile.codingStyleDefaultParameters = context.currentTile.COD;
  6707. }
  6708. class TagTree {
  6709. constructor(width, height) {
  6710. const levelsLength = (0, _core_utils.log2)(Math.max(width, height)) + 1;
  6711. this.levels = [];
  6712. for (let i = 0; i < levelsLength; i++) {
  6713. const level = {
  6714. width,
  6715. height,
  6716. items: []
  6717. };
  6718. this.levels.push(level);
  6719. width = Math.ceil(width / 2);
  6720. height = Math.ceil(height / 2);
  6721. }
  6722. }
  6723. reset(i, j) {
  6724. let currentLevel = 0,
  6725. value = 0,
  6726. level;
  6727. while (currentLevel < this.levels.length) {
  6728. level = this.levels[currentLevel];
  6729. const index = i + j * level.width;
  6730. if (level.items[index] !== undefined) {
  6731. value = level.items[index];
  6732. break;
  6733. }
  6734. level.index = index;
  6735. i >>= 1;
  6736. j >>= 1;
  6737. currentLevel++;
  6738. }
  6739. currentLevel--;
  6740. level = this.levels[currentLevel];
  6741. level.items[level.index] = value;
  6742. this.currentLevel = currentLevel;
  6743. delete this.value;
  6744. }
  6745. incrementValue() {
  6746. const level = this.levels[this.currentLevel];
  6747. level.items[level.index]++;
  6748. }
  6749. nextLevel() {
  6750. let currentLevel = this.currentLevel;
  6751. let level = this.levels[currentLevel];
  6752. const value = level.items[level.index];
  6753. currentLevel--;
  6754. if (currentLevel < 0) {
  6755. this.value = value;
  6756. return false;
  6757. }
  6758. this.currentLevel = currentLevel;
  6759. level = this.levels[currentLevel];
  6760. level.items[level.index] = value;
  6761. return true;
  6762. }
  6763. }
  6764. class InclusionTree {
  6765. constructor(width, height, defaultValue) {
  6766. const levelsLength = (0, _core_utils.log2)(Math.max(width, height)) + 1;
  6767. this.levels = [];
  6768. for (let i = 0; i < levelsLength; i++) {
  6769. const items = new Uint8Array(width * height);
  6770. for (let j = 0, jj = items.length; j < jj; j++) {
  6771. items[j] = defaultValue;
  6772. }
  6773. const level = {
  6774. width,
  6775. height,
  6776. items
  6777. };
  6778. this.levels.push(level);
  6779. width = Math.ceil(width / 2);
  6780. height = Math.ceil(height / 2);
  6781. }
  6782. }
  6783. reset(i, j, stopValue) {
  6784. let currentLevel = 0;
  6785. while (currentLevel < this.levels.length) {
  6786. const level = this.levels[currentLevel];
  6787. const index = i + j * level.width;
  6788. level.index = index;
  6789. const value = level.items[index];
  6790. if (value === 0xff) {
  6791. break;
  6792. }
  6793. if (value > stopValue) {
  6794. this.currentLevel = currentLevel;
  6795. this.propagateValues();
  6796. return false;
  6797. }
  6798. i >>= 1;
  6799. j >>= 1;
  6800. currentLevel++;
  6801. }
  6802. this.currentLevel = currentLevel - 1;
  6803. return true;
  6804. }
  6805. incrementValue(stopValue) {
  6806. const level = this.levels[this.currentLevel];
  6807. level.items[level.index] = stopValue + 1;
  6808. this.propagateValues();
  6809. }
  6810. propagateValues() {
  6811. let levelIndex = this.currentLevel;
  6812. let level = this.levels[levelIndex];
  6813. const currentValue = level.items[level.index];
  6814. while (--levelIndex >= 0) {
  6815. level = this.levels[levelIndex];
  6816. level.items[level.index] = currentValue;
  6817. }
  6818. }
  6819. nextLevel() {
  6820. let currentLevel = this.currentLevel;
  6821. let level = this.levels[currentLevel];
  6822. const value = level.items[level.index];
  6823. level.items[level.index] = 0xff;
  6824. currentLevel--;
  6825. if (currentLevel < 0) {
  6826. return false;
  6827. }
  6828. this.currentLevel = currentLevel;
  6829. level = this.levels[currentLevel];
  6830. level.items[level.index] = value;
  6831. return true;
  6832. }
  6833. }
  6834. const BitModel = function BitModelClosure() {
  6835. const UNIFORM_CONTEXT = 17;
  6836. const RUNLENGTH_CONTEXT = 18;
  6837. const LLAndLHContextsLabel = new Uint8Array([0, 5, 8, 0, 3, 7, 8, 0, 4, 7, 8, 0, 0, 0, 0, 0, 1, 6, 8, 0, 3, 7, 8, 0, 4, 7, 8, 0, 0, 0, 0, 0, 2, 6, 8, 0, 3, 7, 8, 0, 4, 7, 8, 0, 0, 0, 0, 0, 2, 6, 8, 0, 3, 7, 8, 0, 4, 7, 8, 0, 0, 0, 0, 0, 2, 6, 8, 0, 3, 7, 8, 0, 4, 7, 8]);
  6838. const HLContextLabel = new Uint8Array([0, 3, 4, 0, 5, 7, 7, 0, 8, 8, 8, 0, 0, 0, 0, 0, 1, 3, 4, 0, 6, 7, 7, 0, 8, 8, 8, 0, 0, 0, 0, 0, 2, 3, 4, 0, 6, 7, 7, 0, 8, 8, 8, 0, 0, 0, 0, 0, 2, 3, 4, 0, 6, 7, 7, 0, 8, 8, 8, 0, 0, 0, 0, 0, 2, 3, 4, 0, 6, 7, 7, 0, 8, 8, 8]);
  6839. const HHContextLabel = new Uint8Array([0, 1, 2, 0, 1, 2, 2, 0, 2, 2, 2, 0, 0, 0, 0, 0, 3, 4, 5, 0, 4, 5, 5, 0, 5, 5, 5, 0, 0, 0, 0, 0, 6, 7, 7, 0, 7, 7, 7, 0, 7, 7, 7, 0, 0, 0, 0, 0, 8, 8, 8, 0, 8, 8, 8, 0, 8, 8, 8, 0, 0, 0, 0, 0, 8, 8, 8, 0, 8, 8, 8, 0, 8, 8, 8]);
  6840. class BitModel {
  6841. constructor(width, height, subband, zeroBitPlanes, mb) {
  6842. this.width = width;
  6843. this.height = height;
  6844. let contextLabelTable;
  6845. if (subband === "HH") {
  6846. contextLabelTable = HHContextLabel;
  6847. } else if (subband === "HL") {
  6848. contextLabelTable = HLContextLabel;
  6849. } else {
  6850. contextLabelTable = LLAndLHContextsLabel;
  6851. }
  6852. this.contextLabelTable = contextLabelTable;
  6853. const coefficientCount = width * height;
  6854. this.neighborsSignificance = new Uint8Array(coefficientCount);
  6855. this.coefficentsSign = new Uint8Array(coefficientCount);
  6856. let coefficentsMagnitude;
  6857. if (mb > 14) {
  6858. coefficentsMagnitude = new Uint32Array(coefficientCount);
  6859. } else if (mb > 6) {
  6860. coefficentsMagnitude = new Uint16Array(coefficientCount);
  6861. } else {
  6862. coefficentsMagnitude = new Uint8Array(coefficientCount);
  6863. }
  6864. this.coefficentsMagnitude = coefficentsMagnitude;
  6865. this.processingFlags = new Uint8Array(coefficientCount);
  6866. const bitsDecoded = new Uint8Array(coefficientCount);
  6867. if (zeroBitPlanes !== 0) {
  6868. for (let i = 0; i < coefficientCount; i++) {
  6869. bitsDecoded[i] = zeroBitPlanes;
  6870. }
  6871. }
  6872. this.bitsDecoded = bitsDecoded;
  6873. this.reset();
  6874. }
  6875. setDecoder(decoder) {
  6876. this.decoder = decoder;
  6877. }
  6878. reset() {
  6879. this.contexts = new Int8Array(19);
  6880. this.contexts[0] = 4 << 1 | 0;
  6881. this.contexts[UNIFORM_CONTEXT] = 46 << 1 | 0;
  6882. this.contexts[RUNLENGTH_CONTEXT] = 3 << 1 | 0;
  6883. }
  6884. setNeighborsSignificance(row, column, index) {
  6885. const neighborsSignificance = this.neighborsSignificance;
  6886. const width = this.width,
  6887. height = this.height;
  6888. const left = column > 0;
  6889. const right = column + 1 < width;
  6890. let i;
  6891. if (row > 0) {
  6892. i = index - width;
  6893. if (left) {
  6894. neighborsSignificance[i - 1] += 0x10;
  6895. }
  6896. if (right) {
  6897. neighborsSignificance[i + 1] += 0x10;
  6898. }
  6899. neighborsSignificance[i] += 0x04;
  6900. }
  6901. if (row + 1 < height) {
  6902. i = index + width;
  6903. if (left) {
  6904. neighborsSignificance[i - 1] += 0x10;
  6905. }
  6906. if (right) {
  6907. neighborsSignificance[i + 1] += 0x10;
  6908. }
  6909. neighborsSignificance[i] += 0x04;
  6910. }
  6911. if (left) {
  6912. neighborsSignificance[index - 1] += 0x01;
  6913. }
  6914. if (right) {
  6915. neighborsSignificance[index + 1] += 0x01;
  6916. }
  6917. neighborsSignificance[index] |= 0x80;
  6918. }
  6919. runSignificancePropagationPass() {
  6920. const decoder = this.decoder;
  6921. const width = this.width,
  6922. height = this.height;
  6923. const coefficentsMagnitude = this.coefficentsMagnitude;
  6924. const coefficentsSign = this.coefficentsSign;
  6925. const neighborsSignificance = this.neighborsSignificance;
  6926. const processingFlags = this.processingFlags;
  6927. const contexts = this.contexts;
  6928. const labels = this.contextLabelTable;
  6929. const bitsDecoded = this.bitsDecoded;
  6930. const processedInverseMask = ~1;
  6931. const processedMask = 1;
  6932. const firstMagnitudeBitMask = 2;
  6933. for (let i0 = 0; i0 < height; i0 += 4) {
  6934. for (let j = 0; j < width; j++) {
  6935. let index = i0 * width + j;
  6936. for (let i1 = 0; i1 < 4; i1++, index += width) {
  6937. const i = i0 + i1;
  6938. if (i >= height) {
  6939. break;
  6940. }
  6941. processingFlags[index] &= processedInverseMask;
  6942. if (coefficentsMagnitude[index] || !neighborsSignificance[index]) {
  6943. continue;
  6944. }
  6945. const contextLabel = labels[neighborsSignificance[index]];
  6946. const decision = decoder.readBit(contexts, contextLabel);
  6947. if (decision) {
  6948. const sign = this.decodeSignBit(i, j, index);
  6949. coefficentsSign[index] = sign;
  6950. coefficentsMagnitude[index] = 1;
  6951. this.setNeighborsSignificance(i, j, index);
  6952. processingFlags[index] |= firstMagnitudeBitMask;
  6953. }
  6954. bitsDecoded[index]++;
  6955. processingFlags[index] |= processedMask;
  6956. }
  6957. }
  6958. }
  6959. }
  6960. decodeSignBit(row, column, index) {
  6961. const width = this.width,
  6962. height = this.height;
  6963. const coefficentsMagnitude = this.coefficentsMagnitude;
  6964. const coefficentsSign = this.coefficentsSign;
  6965. let contribution, sign0, sign1, significance1;
  6966. let contextLabel, decoded;
  6967. significance1 = column > 0 && coefficentsMagnitude[index - 1] !== 0;
  6968. if (column + 1 < width && coefficentsMagnitude[index + 1] !== 0) {
  6969. sign1 = coefficentsSign[index + 1];
  6970. if (significance1) {
  6971. sign0 = coefficentsSign[index - 1];
  6972. contribution = 1 - sign1 - sign0;
  6973. } else {
  6974. contribution = 1 - sign1 - sign1;
  6975. }
  6976. } else if (significance1) {
  6977. sign0 = coefficentsSign[index - 1];
  6978. contribution = 1 - sign0 - sign0;
  6979. } else {
  6980. contribution = 0;
  6981. }
  6982. const horizontalContribution = 3 * contribution;
  6983. significance1 = row > 0 && coefficentsMagnitude[index - width] !== 0;
  6984. if (row + 1 < height && coefficentsMagnitude[index + width] !== 0) {
  6985. sign1 = coefficentsSign[index + width];
  6986. if (significance1) {
  6987. sign0 = coefficentsSign[index - width];
  6988. contribution = 1 - sign1 - sign0 + horizontalContribution;
  6989. } else {
  6990. contribution = 1 - sign1 - sign1 + horizontalContribution;
  6991. }
  6992. } else if (significance1) {
  6993. sign0 = coefficentsSign[index - width];
  6994. contribution = 1 - sign0 - sign0 + horizontalContribution;
  6995. } else {
  6996. contribution = horizontalContribution;
  6997. }
  6998. if (contribution >= 0) {
  6999. contextLabel = 9 + contribution;
  7000. decoded = this.decoder.readBit(this.contexts, contextLabel);
  7001. } else {
  7002. contextLabel = 9 - contribution;
  7003. decoded = this.decoder.readBit(this.contexts, contextLabel) ^ 1;
  7004. }
  7005. return decoded;
  7006. }
  7007. runMagnitudeRefinementPass() {
  7008. const decoder = this.decoder;
  7009. const width = this.width,
  7010. height = this.height;
  7011. const coefficentsMagnitude = this.coefficentsMagnitude;
  7012. const neighborsSignificance = this.neighborsSignificance;
  7013. const contexts = this.contexts;
  7014. const bitsDecoded = this.bitsDecoded;
  7015. const processingFlags = this.processingFlags;
  7016. const processedMask = 1;
  7017. const firstMagnitudeBitMask = 2;
  7018. const length = width * height;
  7019. const width4 = width * 4;
  7020. for (let index0 = 0, indexNext; index0 < length; index0 = indexNext) {
  7021. indexNext = Math.min(length, index0 + width4);
  7022. for (let j = 0; j < width; j++) {
  7023. for (let index = index0 + j; index < indexNext; index += width) {
  7024. if (!coefficentsMagnitude[index] || (processingFlags[index] & processedMask) !== 0) {
  7025. continue;
  7026. }
  7027. let contextLabel = 16;
  7028. if ((processingFlags[index] & firstMagnitudeBitMask) !== 0) {
  7029. processingFlags[index] ^= firstMagnitudeBitMask;
  7030. const significance = neighborsSignificance[index] & 127;
  7031. contextLabel = significance === 0 ? 15 : 14;
  7032. }
  7033. const bit = decoder.readBit(contexts, contextLabel);
  7034. coefficentsMagnitude[index] = coefficentsMagnitude[index] << 1 | bit;
  7035. bitsDecoded[index]++;
  7036. processingFlags[index] |= processedMask;
  7037. }
  7038. }
  7039. }
  7040. }
  7041. runCleanupPass() {
  7042. const decoder = this.decoder;
  7043. const width = this.width,
  7044. height = this.height;
  7045. const neighborsSignificance = this.neighborsSignificance;
  7046. const coefficentsMagnitude = this.coefficentsMagnitude;
  7047. const coefficentsSign = this.coefficentsSign;
  7048. const contexts = this.contexts;
  7049. const labels = this.contextLabelTable;
  7050. const bitsDecoded = this.bitsDecoded;
  7051. const processingFlags = this.processingFlags;
  7052. const processedMask = 1;
  7053. const firstMagnitudeBitMask = 2;
  7054. const oneRowDown = width;
  7055. const twoRowsDown = width * 2;
  7056. const threeRowsDown = width * 3;
  7057. let iNext;
  7058. for (let i0 = 0; i0 < height; i0 = iNext) {
  7059. iNext = Math.min(i0 + 4, height);
  7060. const indexBase = i0 * width;
  7061. const checkAllEmpty = i0 + 3 < height;
  7062. for (let j = 0; j < width; j++) {
  7063. const index0 = indexBase + j;
  7064. const allEmpty = checkAllEmpty && processingFlags[index0] === 0 && processingFlags[index0 + oneRowDown] === 0 && processingFlags[index0 + twoRowsDown] === 0 && processingFlags[index0 + threeRowsDown] === 0 && neighborsSignificance[index0] === 0 && neighborsSignificance[index0 + oneRowDown] === 0 && neighborsSignificance[index0 + twoRowsDown] === 0 && neighborsSignificance[index0 + threeRowsDown] === 0;
  7065. let i1 = 0,
  7066. index = index0;
  7067. let i = i0,
  7068. sign;
  7069. if (allEmpty) {
  7070. const hasSignificantCoefficent = decoder.readBit(contexts, RUNLENGTH_CONTEXT);
  7071. if (!hasSignificantCoefficent) {
  7072. bitsDecoded[index0]++;
  7073. bitsDecoded[index0 + oneRowDown]++;
  7074. bitsDecoded[index0 + twoRowsDown]++;
  7075. bitsDecoded[index0 + threeRowsDown]++;
  7076. continue;
  7077. }
  7078. i1 = decoder.readBit(contexts, UNIFORM_CONTEXT) << 1 | decoder.readBit(contexts, UNIFORM_CONTEXT);
  7079. if (i1 !== 0) {
  7080. i = i0 + i1;
  7081. index += i1 * width;
  7082. }
  7083. sign = this.decodeSignBit(i, j, index);
  7084. coefficentsSign[index] = sign;
  7085. coefficentsMagnitude[index] = 1;
  7086. this.setNeighborsSignificance(i, j, index);
  7087. processingFlags[index] |= firstMagnitudeBitMask;
  7088. index = index0;
  7089. for (let i2 = i0; i2 <= i; i2++, index += width) {
  7090. bitsDecoded[index]++;
  7091. }
  7092. i1++;
  7093. }
  7094. for (i = i0 + i1; i < iNext; i++, index += width) {
  7095. if (coefficentsMagnitude[index] || (processingFlags[index] & processedMask) !== 0) {
  7096. continue;
  7097. }
  7098. const contextLabel = labels[neighborsSignificance[index]];
  7099. const decision = decoder.readBit(contexts, contextLabel);
  7100. if (decision === 1) {
  7101. sign = this.decodeSignBit(i, j, index);
  7102. coefficentsSign[index] = sign;
  7103. coefficentsMagnitude[index] = 1;
  7104. this.setNeighborsSignificance(i, j, index);
  7105. processingFlags[index] |= firstMagnitudeBitMask;
  7106. }
  7107. bitsDecoded[index]++;
  7108. }
  7109. }
  7110. }
  7111. }
  7112. checkSegmentationSymbol() {
  7113. const decoder = this.decoder;
  7114. const contexts = this.contexts;
  7115. const symbol = decoder.readBit(contexts, UNIFORM_CONTEXT) << 3 | decoder.readBit(contexts, UNIFORM_CONTEXT) << 2 | decoder.readBit(contexts, UNIFORM_CONTEXT) << 1 | decoder.readBit(contexts, UNIFORM_CONTEXT);
  7116. if (symbol !== 0xa) {
  7117. throw new JpxError("Invalid segmentation symbol");
  7118. }
  7119. }
  7120. }
  7121. return BitModel;
  7122. }();
  7123. class Transform {
  7124. constructor() {
  7125. if (this.constructor === Transform) {
  7126. (0, _util.unreachable)("Cannot initialize Transform.");
  7127. }
  7128. }
  7129. calculate(subbands, u0, v0) {
  7130. let ll = subbands[0];
  7131. for (let i = 1, ii = subbands.length; i < ii; i++) {
  7132. ll = this.iterate(ll, subbands[i], u0, v0);
  7133. }
  7134. return ll;
  7135. }
  7136. extend(buffer, offset, size) {
  7137. let i1 = offset - 1,
  7138. j1 = offset + 1;
  7139. let i2 = offset + size - 2,
  7140. j2 = offset + size;
  7141. buffer[i1--] = buffer[j1++];
  7142. buffer[j2++] = buffer[i2--];
  7143. buffer[i1--] = buffer[j1++];
  7144. buffer[j2++] = buffer[i2--];
  7145. buffer[i1--] = buffer[j1++];
  7146. buffer[j2++] = buffer[i2--];
  7147. buffer[i1] = buffer[j1];
  7148. buffer[j2] = buffer[i2];
  7149. }
  7150. filter(x, offset, length) {
  7151. (0, _util.unreachable)("Abstract method `filter` called");
  7152. }
  7153. iterate(ll, hl_lh_hh, u0, v0) {
  7154. const llWidth = ll.width,
  7155. llHeight = ll.height;
  7156. let llItems = ll.items;
  7157. const width = hl_lh_hh.width;
  7158. const height = hl_lh_hh.height;
  7159. const items = hl_lh_hh.items;
  7160. let i, j, k, l, u, v;
  7161. for (k = 0, i = 0; i < llHeight; i++) {
  7162. l = i * 2 * width;
  7163. for (j = 0; j < llWidth; j++, k++, l += 2) {
  7164. items[l] = llItems[k];
  7165. }
  7166. }
  7167. llItems = ll.items = null;
  7168. const bufferPadding = 4;
  7169. const rowBuffer = new Float32Array(width + 2 * bufferPadding);
  7170. if (width === 1) {
  7171. if ((u0 & 1) !== 0) {
  7172. for (v = 0, k = 0; v < height; v++, k += width) {
  7173. items[k] *= 0.5;
  7174. }
  7175. }
  7176. } else {
  7177. for (v = 0, k = 0; v < height; v++, k += width) {
  7178. rowBuffer.set(items.subarray(k, k + width), bufferPadding);
  7179. this.extend(rowBuffer, bufferPadding, width);
  7180. this.filter(rowBuffer, bufferPadding, width);
  7181. items.set(rowBuffer.subarray(bufferPadding, bufferPadding + width), k);
  7182. }
  7183. }
  7184. let numBuffers = 16;
  7185. const colBuffers = [];
  7186. for (i = 0; i < numBuffers; i++) {
  7187. colBuffers.push(new Float32Array(height + 2 * bufferPadding));
  7188. }
  7189. let b,
  7190. currentBuffer = 0;
  7191. ll = bufferPadding + height;
  7192. if (height === 1) {
  7193. if ((v0 & 1) !== 0) {
  7194. for (u = 0; u < width; u++) {
  7195. items[u] *= 0.5;
  7196. }
  7197. }
  7198. } else {
  7199. for (u = 0; u < width; u++) {
  7200. if (currentBuffer === 0) {
  7201. numBuffers = Math.min(width - u, numBuffers);
  7202. for (k = u, l = bufferPadding; l < ll; k += width, l++) {
  7203. for (b = 0; b < numBuffers; b++) {
  7204. colBuffers[b][l] = items[k + b];
  7205. }
  7206. }
  7207. currentBuffer = numBuffers;
  7208. }
  7209. currentBuffer--;
  7210. const buffer = colBuffers[currentBuffer];
  7211. this.extend(buffer, bufferPadding, height);
  7212. this.filter(buffer, bufferPadding, height);
  7213. if (currentBuffer === 0) {
  7214. k = u - numBuffers + 1;
  7215. for (l = bufferPadding; l < ll; k += width, l++) {
  7216. for (b = 0; b < numBuffers; b++) {
  7217. items[k + b] = colBuffers[b][l];
  7218. }
  7219. }
  7220. }
  7221. }
  7222. }
  7223. return {
  7224. width,
  7225. height,
  7226. items
  7227. };
  7228. }
  7229. }
  7230. class IrreversibleTransform extends Transform {
  7231. filter(x, offset, length) {
  7232. const len = length >> 1;
  7233. offset |= 0;
  7234. let j, n, current, next;
  7235. const alpha = -1.586134342059924;
  7236. const beta = -0.052980118572961;
  7237. const gamma = 0.882911075530934;
  7238. const delta = 0.443506852043971;
  7239. const K = 1.230174104914001;
  7240. const K_ = 1 / K;
  7241. j = offset - 3;
  7242. for (n = len + 4; n--; j += 2) {
  7243. x[j] *= K_;
  7244. }
  7245. j = offset - 2;
  7246. current = delta * x[j - 1];
  7247. for (n = len + 3; n--; j += 2) {
  7248. next = delta * x[j + 1];
  7249. x[j] = K * x[j] - current - next;
  7250. if (n--) {
  7251. j += 2;
  7252. current = delta * x[j + 1];
  7253. x[j] = K * x[j] - current - next;
  7254. } else {
  7255. break;
  7256. }
  7257. }
  7258. j = offset - 1;
  7259. current = gamma * x[j - 1];
  7260. for (n = len + 2; n--; j += 2) {
  7261. next = gamma * x[j + 1];
  7262. x[j] -= current + next;
  7263. if (n--) {
  7264. j += 2;
  7265. current = gamma * x[j + 1];
  7266. x[j] -= current + next;
  7267. } else {
  7268. break;
  7269. }
  7270. }
  7271. j = offset;
  7272. current = beta * x[j - 1];
  7273. for (n = len + 1; n--; j += 2) {
  7274. next = beta * x[j + 1];
  7275. x[j] -= current + next;
  7276. if (n--) {
  7277. j += 2;
  7278. current = beta * x[j + 1];
  7279. x[j] -= current + next;
  7280. } else {
  7281. break;
  7282. }
  7283. }
  7284. if (len !== 0) {
  7285. j = offset + 1;
  7286. current = alpha * x[j - 1];
  7287. for (n = len; n--; j += 2) {
  7288. next = alpha * x[j + 1];
  7289. x[j] -= current + next;
  7290. if (n--) {
  7291. j += 2;
  7292. current = alpha * x[j + 1];
  7293. x[j] -= current + next;
  7294. } else {
  7295. break;
  7296. }
  7297. }
  7298. }
  7299. }
  7300. }
  7301. class ReversibleTransform extends Transform {
  7302. filter(x, offset, length) {
  7303. const len = length >> 1;
  7304. offset |= 0;
  7305. let j, n;
  7306. for (j = offset, n = len + 1; n--; j += 2) {
  7307. x[j] -= x[j - 1] + x[j + 1] + 2 >> 2;
  7308. }
  7309. for (j = offset + 1, n = len; n--; j += 2) {
  7310. x[j] += x[j - 1] + x[j + 1] >> 1;
  7311. }
  7312. }
  7313. }
  7314. /***/ })
  7315. /******/ ]);
  7316. /************************************************************************/
  7317. /******/ // The module cache
  7318. /******/ var __webpack_module_cache__ = {};
  7319. /******/
  7320. /******/ // The require function
  7321. /******/ function __w_pdfjs_require__(moduleId) {
  7322. /******/ // Check if module is in cache
  7323. /******/ var cachedModule = __webpack_module_cache__[moduleId];
  7324. /******/ if (cachedModule !== undefined) {
  7325. /******/ return cachedModule.exports;
  7326. /******/ }
  7327. /******/ // Create a new module (and put it into the cache)
  7328. /******/ var module = __webpack_module_cache__[moduleId] = {
  7329. /******/ // no module.id needed
  7330. /******/ // no module.loaded needed
  7331. /******/ exports: {}
  7332. /******/ };
  7333. /******/
  7334. /******/ // Execute the module function
  7335. /******/ __webpack_modules__[moduleId](module, module.exports, __w_pdfjs_require__);
  7336. /******/
  7337. /******/ // Return the exports of the module
  7338. /******/ return module.exports;
  7339. /******/ }
  7340. /******/
  7341. /************************************************************************/
  7342. var __webpack_exports__ = {};
  7343. // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
  7344. (() => {
  7345. var exports = __webpack_exports__;
  7346. Object.defineProperty(exports, "__esModule", ({
  7347. value: true
  7348. }));
  7349. Object.defineProperty(exports, "Jbig2Image", ({
  7350. enumerable: true,
  7351. get: function () {
  7352. return _jbig.Jbig2Image;
  7353. }
  7354. }));
  7355. Object.defineProperty(exports, "JpegImage", ({
  7356. enumerable: true,
  7357. get: function () {
  7358. return _jpg.JpegImage;
  7359. }
  7360. }));
  7361. Object.defineProperty(exports, "JpxImage", ({
  7362. enumerable: true,
  7363. get: function () {
  7364. return _jpx.JpxImage;
  7365. }
  7366. }));
  7367. Object.defineProperty(exports, "getVerbosityLevel", ({
  7368. enumerable: true,
  7369. get: function () {
  7370. return _util.getVerbosityLevel;
  7371. }
  7372. }));
  7373. Object.defineProperty(exports, "setVerbosityLevel", ({
  7374. enumerable: true,
  7375. get: function () {
  7376. return _util.setVerbosityLevel;
  7377. }
  7378. }));
  7379. var _util = __w_pdfjs_require__(1);
  7380. var _jbig = __w_pdfjs_require__(2);
  7381. var _jpg = __w_pdfjs_require__(8);
  7382. var _jpx = __w_pdfjs_require__(9);
  7383. const pdfjsVersion = '3.1.81';
  7384. const pdfjsBuild = '0766898d5';
  7385. })();
  7386. /******/ return __webpack_exports__;
  7387. /******/ })()
  7388. ;
  7389. });
  7390. //# sourceMappingURL=pdf.image_decoders.js.map