pdf.image_decoders.js 241 KB

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