2
0

pdf.image_decoders.js 240 KB

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