pdf.image_decoders.js 242 KB

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