pdf.image_decoders.js 242 KB

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