index.js 1.2 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ZXing = {}));
  5. })(this, (function (exports) { 'use strict';
  6. function fixProto(target, prototype) {
  7. var setPrototypeOf = Object.setPrototypeOf;
  8. setPrototypeOf ? setPrototypeOf(target, prototype) : target.__proto__ = prototype;
  9. }
  10. function fixStack(target, fn) {
  11. if (fn === void 0) {
  12. fn = target.constructor;
  13. }
  14. var captureStackTrace = Error.captureStackTrace;
  15. captureStackTrace && captureStackTrace(target, fn);
  16. }
  17. var __extends = function () {
  18. var _extendStatics = function extendStatics(d, b) {
  19. _extendStatics = Object.setPrototypeOf || {
  20. __proto__: []
  21. } instanceof Array && function (d, b) {
  22. d.__proto__ = b;
  23. } || function (d, b) {
  24. for (var p in b) {
  25. if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
  26. }
  27. };
  28. return _extendStatics(d, b);
  29. };
  30. return function (d, b) {
  31. if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  32. _extendStatics(d, b);
  33. function __() {
  34. this.constructor = d;
  35. }
  36. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  37. };
  38. }();
  39. var CustomError = function (_super) {
  40. __extends(CustomError, _super);
  41. function CustomError(message, options) {
  42. var _newTarget = this.constructor;
  43. var _this = _super.call(this, message, options) || this;
  44. Object.defineProperty(_this, 'name', {
  45. value: _newTarget.name,
  46. enumerable: false,
  47. configurable: true
  48. });
  49. fixProto(_this, _newTarget.prototype);
  50. fixStack(_this);
  51. return _this;
  52. }
  53. return CustomError;
  54. }(Error);
  55. /**
  56. * Custom Error class of type Exception.
  57. */
  58. class Exception extends CustomError {
  59. /**
  60. * Allows Exception to be constructed directly
  61. * with some message and prototype definition.
  62. */
  63. constructor(message = undefined) {
  64. super(message);
  65. this.message = message;
  66. }
  67. getKind() {
  68. const ex = this.constructor;
  69. return ex.kind;
  70. }
  71. }
  72. /**
  73. * It's typed as string so it can be extended and overriden.
  74. */
  75. Exception.kind = 'Exception';
  76. /**
  77. * Custom Error class of type Exception.
  78. */
  79. class ArgumentException extends Exception {
  80. }
  81. ArgumentException.kind = 'ArgumentException';
  82. /**
  83. * Custom Error class of type Exception.
  84. */
  85. class IllegalArgumentException extends Exception {
  86. }
  87. IllegalArgumentException.kind = 'IllegalArgumentException';
  88. /*
  89. * Copyright 2009 ZXing authors
  90. *
  91. * Licensed under the Apache License, Version 2.0 (the "License");
  92. * you may not use this file except in compliance with the License.
  93. * You may obtain a copy of the License at
  94. *
  95. * http://www.apache.org/licenses/LICENSE-2.0
  96. *
  97. * Unless required by applicable law or agreed to in writing, software
  98. * distributed under the License is distributed on an "AS IS" BASIS,
  99. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  100. * See the License for the specific language governing permissions and
  101. * limitations under the License.
  102. */
  103. class BinaryBitmap {
  104. constructor(binarizer) {
  105. this.binarizer = binarizer;
  106. if (binarizer === null) {
  107. throw new IllegalArgumentException('Binarizer must be non-null.');
  108. }
  109. }
  110. /**
  111. * @return The width of the bitmap.
  112. */
  113. getWidth() {
  114. return this.binarizer.getWidth();
  115. }
  116. /**
  117. * @return The height of the bitmap.
  118. */
  119. getHeight() {
  120. return this.binarizer.getHeight();
  121. }
  122. /**
  123. * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
  124. * cached data. Callers should assume this method is expensive and call it as seldom as possible.
  125. * This method is intended for decoding 1D barcodes and may choose to apply sharpening.
  126. *
  127. * @param y The row to fetch, which must be in [0, bitmap height)
  128. * @param row An optional preallocated array. If null or too small, it will be ignored.
  129. * If used, the Binarizer will call BitArray.clear(). Always use the returned object.
  130. * @return The array of bits for this row (true means black).
  131. * @throws NotFoundException if row can't be binarized
  132. */
  133. getBlackRow(y /*int*/, row) {
  134. return this.binarizer.getBlackRow(y, row);
  135. }
  136. /**
  137. * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
  138. * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
  139. * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
  140. * fetched using getBlackRow(), so don't mix and match between them.
  141. *
  142. * @return The 2D array of bits for the image (true means black).
  143. * @throws NotFoundException if image can't be binarized to make a matrix
  144. */
  145. getBlackMatrix() {
  146. // The matrix is created on demand the first time it is requested, then cached. There are two
  147. // reasons for this:
  148. // 1. This work will never be done if the caller only installs 1D Reader objects, or if a
  149. // 1D Reader finds a barcode before the 2D Readers run.
  150. // 2. This work will only be done once even if the caller installs multiple 2D Readers.
  151. if (this.matrix === null || this.matrix === undefined) {
  152. this.matrix = this.binarizer.getBlackMatrix();
  153. }
  154. return this.matrix;
  155. }
  156. /**
  157. * @return Whether this bitmap can be cropped.
  158. */
  159. isCropSupported() {
  160. return this.binarizer.getLuminanceSource().isCropSupported();
  161. }
  162. /**
  163. * Returns a new object with cropped image data. Implementations may keep a reference to the
  164. * original data rather than a copy. Only callable if isCropSupported() is true.
  165. *
  166. * @param left The left coordinate, which must be in [0,getWidth())
  167. * @param top The top coordinate, which must be in [0,getHeight())
  168. * @param width The width of the rectangle to crop.
  169. * @param height The height of the rectangle to crop.
  170. * @return A cropped version of this object.
  171. */
  172. crop(left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  173. const newSource = this.binarizer.getLuminanceSource().crop(left, top, width, height);
  174. return new BinaryBitmap(this.binarizer.createBinarizer(newSource));
  175. }
  176. /**
  177. * @return Whether this bitmap supports counter-clockwise rotation.
  178. */
  179. isRotateSupported() {
  180. return this.binarizer.getLuminanceSource().isRotateSupported();
  181. }
  182. /**
  183. * Returns a new object with rotated image data by 90 degrees counterclockwise.
  184. * Only callable if {@link #isRotateSupported()} is true.
  185. *
  186. * @return A rotated version of this object.
  187. */
  188. rotateCounterClockwise() {
  189. const newSource = this.binarizer.getLuminanceSource().rotateCounterClockwise();
  190. return new BinaryBitmap(this.binarizer.createBinarizer(newSource));
  191. }
  192. /**
  193. * Returns a new object with rotated image data by 45 degrees counterclockwise.
  194. * Only callable if {@link #isRotateSupported()} is true.
  195. *
  196. * @return A rotated version of this object.
  197. */
  198. rotateCounterClockwise45() {
  199. const newSource = this.binarizer.getLuminanceSource().rotateCounterClockwise45();
  200. return new BinaryBitmap(this.binarizer.createBinarizer(newSource));
  201. }
  202. /*@Override*/
  203. toString() {
  204. try {
  205. return this.getBlackMatrix().toString();
  206. }
  207. catch (e /*: NotFoundException*/) {
  208. return '';
  209. }
  210. }
  211. }
  212. /**
  213. * Custom Error class of type Exception.
  214. */
  215. class ChecksumException extends Exception {
  216. static getChecksumInstance() {
  217. return new ChecksumException();
  218. }
  219. }
  220. ChecksumException.kind = 'ChecksumException';
  221. /*
  222. * Copyright 2009 ZXing authors
  223. *
  224. * Licensed under the Apache License, Version 2.0 (the "License");
  225. * you may not use this file except in compliance with the License.
  226. * You may obtain a copy of the License at
  227. *
  228. * http://www.apache.org/licenses/LICENSE-2.0
  229. *
  230. * Unless required by applicable law or agreed to in writing, software
  231. * distributed under the License is distributed on an "AS IS" BASIS,
  232. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  233. * See the License for the specific language governing permissions and
  234. * limitations under the License.
  235. */
  236. /**
  237. * This class hierarchy provides a set of methods to convert luminance data to 1 bit data.
  238. * It allows the algorithm to vary polymorphically, for example allowing a very expensive
  239. * thresholding technique for servers and a fast one for mobile. It also permits the implementation
  240. * to vary, e.g. a JNI version for Android and a Java fallback version for other platforms.
  241. *
  242. * @author dswitkin@google.com (Daniel Switkin)
  243. */
  244. class Binarizer {
  245. constructor(source) {
  246. this.source = source;
  247. }
  248. getLuminanceSource() {
  249. return this.source;
  250. }
  251. getWidth() {
  252. return this.source.getWidth();
  253. }
  254. getHeight() {
  255. return this.source.getHeight();
  256. }
  257. }
  258. class System {
  259. // public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
  260. /**
  261. * Makes a copy of a array.
  262. */
  263. static arraycopy(src, srcPos, dest, destPos, length) {
  264. // TODO: better use split or set?
  265. while (length--) {
  266. dest[destPos++] = src[srcPos++];
  267. }
  268. }
  269. /**
  270. * Returns the current time in milliseconds.
  271. */
  272. static currentTimeMillis() {
  273. return Date.now();
  274. }
  275. }
  276. /**
  277. * Custom Error class of type Exception.
  278. */
  279. class IndexOutOfBoundsException extends Exception {
  280. }
  281. IndexOutOfBoundsException.kind = 'IndexOutOfBoundsException';
  282. /**
  283. * Custom Error class of type Exception.
  284. */
  285. class ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException {
  286. constructor(index = undefined, message = undefined) {
  287. super(message);
  288. this.index = index;
  289. this.message = message;
  290. }
  291. }
  292. ArrayIndexOutOfBoundsException.kind = 'ArrayIndexOutOfBoundsException';
  293. class Arrays {
  294. /**
  295. * Assigns the specified int value to each element of the specified array
  296. * of ints.
  297. *
  298. * @param a the array to be filled
  299. * @param val the value to be stored in all elements of the array
  300. */
  301. static fill(a, val) {
  302. for (let i = 0, len = a.length; i < len; i++)
  303. a[i] = val;
  304. }
  305. /**
  306. * Assigns the specified int value to each element of the specified
  307. * range of the specified array of ints. The range to be filled
  308. * extends from index {@code fromIndex}, inclusive, to index
  309. * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
  310. * range to be filled is empty.)
  311. *
  312. * @param a the array to be filled
  313. * @param fromIndex the index of the first element (inclusive) to be
  314. * filled with the specified value
  315. * @param toIndex the index of the last element (exclusive) to be
  316. * filled with the specified value
  317. * @param val the value to be stored in all elements of the array
  318. * @throws IllegalArgumentException if {@code fromIndex > toIndex}
  319. * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
  320. * {@code toIndex > a.length}
  321. */
  322. static fillWithin(a, fromIndex, toIndex, val) {
  323. Arrays.rangeCheck(a.length, fromIndex, toIndex);
  324. for (let i = fromIndex; i < toIndex; i++)
  325. a[i] = val;
  326. }
  327. /**
  328. * Checks that {@code fromIndex} and {@code toIndex} are in
  329. * the range and throws an exception if they aren't.
  330. */
  331. static rangeCheck(arrayLength, fromIndex, toIndex) {
  332. if (fromIndex > toIndex) {
  333. throw new IllegalArgumentException('fromIndex(' + fromIndex + ') > toIndex(' + toIndex + ')');
  334. }
  335. if (fromIndex < 0) {
  336. throw new ArrayIndexOutOfBoundsException(fromIndex);
  337. }
  338. if (toIndex > arrayLength) {
  339. throw new ArrayIndexOutOfBoundsException(toIndex);
  340. }
  341. }
  342. static asList(...args) {
  343. return args;
  344. }
  345. static create(rows, cols, value) {
  346. let arr = Array.from({ length: rows });
  347. return arr.map(x => Array.from({ length: cols }).fill(value));
  348. }
  349. static createInt32Array(rows, cols, value) {
  350. let arr = Array.from({ length: rows });
  351. return arr.map(x => Int32Array.from({ length: cols }).fill(value));
  352. }
  353. static equals(first, second) {
  354. if (!first) {
  355. return false;
  356. }
  357. if (!second) {
  358. return false;
  359. }
  360. if (!first.length) {
  361. return false;
  362. }
  363. if (!second.length) {
  364. return false;
  365. }
  366. if (first.length !== second.length) {
  367. return false;
  368. }
  369. for (let i = 0, length = first.length; i < length; i++) {
  370. if (first[i] !== second[i]) {
  371. return false;
  372. }
  373. }
  374. return true;
  375. }
  376. static hashCode(a) {
  377. if (a === null) {
  378. return 0;
  379. }
  380. let result = 1;
  381. for (const element of a) {
  382. result = 31 * result + element;
  383. }
  384. return result;
  385. }
  386. static fillUint8Array(a, value) {
  387. for (let i = 0; i !== a.length; i++) {
  388. a[i] = value;
  389. }
  390. }
  391. static copyOf(original, newLength) {
  392. return original.slice(0, newLength);
  393. }
  394. static copyOfUint8Array(original, newLength) {
  395. if (original.length <= newLength) {
  396. const newArray = new Uint8Array(newLength);
  397. newArray.set(original);
  398. return newArray;
  399. }
  400. return original.slice(0, newLength);
  401. }
  402. static copyOfRange(original, from, to) {
  403. const newLength = to - from;
  404. const copy = new Int32Array(newLength);
  405. System.arraycopy(original, from, copy, 0, newLength);
  406. return copy;
  407. }
  408. /*
  409. * Returns the index of of the element in a sorted array or (-n-1) where n is the insertion point
  410. * for the new element.
  411. * Parameters:
  412. * ar - A sorted array
  413. * el - An element to search for
  414. * comparator - A comparator function. The function takes two arguments: (a, b) and returns:
  415. * a negative number if a is less than b;
  416. * 0 if a is equal to b;
  417. * a positive number of a is greater than b.
  418. * The array may contain duplicate elements. If there are more than one equal elements in the array,
  419. * the returned value can be the index of any one of the equal elements.
  420. *
  421. * http://jsfiddle.net/aryzhov/pkfst550/
  422. */
  423. static binarySearch(ar, el, comparator) {
  424. if (undefined === comparator) {
  425. comparator = Arrays.numberComparator;
  426. }
  427. let m = 0;
  428. let n = ar.length - 1;
  429. while (m <= n) {
  430. const k = (n + m) >> 1;
  431. const cmp = comparator(el, ar[k]);
  432. if (cmp > 0) {
  433. m = k + 1;
  434. }
  435. else if (cmp < 0) {
  436. n = k - 1;
  437. }
  438. else {
  439. return k;
  440. }
  441. }
  442. return -m - 1;
  443. }
  444. static numberComparator(a, b) {
  445. return a - b;
  446. }
  447. }
  448. /**
  449. * Ponyfill for Java's Integer class.
  450. */
  451. class Integer {
  452. static numberOfTrailingZeros(i) {
  453. let y;
  454. if (i === 0)
  455. return 32;
  456. let n = 31;
  457. y = i << 16;
  458. if (y !== 0) {
  459. n -= 16;
  460. i = y;
  461. }
  462. y = i << 8;
  463. if (y !== 0) {
  464. n -= 8;
  465. i = y;
  466. }
  467. y = i << 4;
  468. if (y !== 0) {
  469. n -= 4;
  470. i = y;
  471. }
  472. y = i << 2;
  473. if (y !== 0) {
  474. n -= 2;
  475. i = y;
  476. }
  477. return n - ((i << 1) >>> 31);
  478. }
  479. static numberOfLeadingZeros(i) {
  480. // HD, Figure 5-6
  481. if (i === 0) {
  482. return 32;
  483. }
  484. let n = 1;
  485. if (i >>> 16 === 0) {
  486. n += 16;
  487. i <<= 16;
  488. }
  489. if (i >>> 24 === 0) {
  490. n += 8;
  491. i <<= 8;
  492. }
  493. if (i >>> 28 === 0) {
  494. n += 4;
  495. i <<= 4;
  496. }
  497. if (i >>> 30 === 0) {
  498. n += 2;
  499. i <<= 2;
  500. }
  501. n -= i >>> 31;
  502. return n;
  503. }
  504. static toHexString(i) {
  505. return i.toString(16);
  506. }
  507. static toBinaryString(intNumber) {
  508. return String(parseInt(String(intNumber), 2));
  509. }
  510. // Returns the number of one-bits in the two's complement binary representation of the specified int value. This function is sometimes referred to as the population count.
  511. // Returns:
  512. // the number of one-bits in the two's complement binary representation of the specified int value.
  513. static bitCount(i) {
  514. // HD, Figure 5-2
  515. i = i - ((i >>> 1) & 0x55555555);
  516. i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
  517. i = (i + (i >>> 4)) & 0x0f0f0f0f;
  518. i = i + (i >>> 8);
  519. i = i + (i >>> 16);
  520. return i & 0x3f;
  521. }
  522. static truncDivision(dividend, divisor) {
  523. return Math.trunc(dividend / divisor);
  524. }
  525. /**
  526. * Converts A string to an integer.
  527. * @param s A string to convert into a number.
  528. * @param radix A value between 2 and 36 that specifies the base of the number in numString. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.
  529. */
  530. static parseInt(num, radix = undefined) {
  531. return parseInt(num, radix);
  532. }
  533. }
  534. Integer.MIN_VALUE_32_BITS = -2147483648;
  535. Integer.MAX_VALUE = Number.MAX_SAFE_INTEGER;
  536. /*
  537. * Copyright 2007 ZXing authors
  538. *
  539. * Licensed under the Apache License, Version 2.0 (the "License");
  540. * you may not use this file except in compliance with the License.
  541. * You may obtain a copy of the License at
  542. *
  543. * http://www.apache.org/licenses/LICENSE-2.0
  544. *
  545. * Unless required by applicable law or agreed to in writing, software
  546. * distributed under the License is distributed on an "AS IS" BASIS,
  547. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  548. * See the License for the specific language governing permissions and
  549. * limitations under the License.
  550. */
  551. /**
  552. * <p>A simple, fast array of bits, represented compactly by an array of ints internally.</p>
  553. *
  554. * @author Sean Owen
  555. */
  556. class BitArray /*implements Cloneable*/ {
  557. // public constructor() {
  558. // this.size = 0
  559. // this.bits = new Int32Array(1)
  560. // }
  561. // public constructor(size?: number /*int*/) {
  562. // if (undefined === size) {
  563. // this.size = 0
  564. // } else {
  565. // this.size = size
  566. // }
  567. // this.bits = this.makeArray(size)
  568. // }
  569. // For testing only
  570. constructor(size /*int*/, bits) {
  571. if (undefined === size) {
  572. this.size = 0;
  573. this.bits = new Int32Array(1);
  574. }
  575. else {
  576. this.size = size;
  577. if (undefined === bits || null === bits) {
  578. this.bits = BitArray.makeArray(size);
  579. }
  580. else {
  581. this.bits = bits;
  582. }
  583. }
  584. }
  585. getSize() {
  586. return this.size;
  587. }
  588. getSizeInBytes() {
  589. return Math.floor((this.size + 7) / 8);
  590. }
  591. ensureCapacity(size /*int*/) {
  592. if (size > this.bits.length * 32) {
  593. const newBits = BitArray.makeArray(size);
  594. System.arraycopy(this.bits, 0, newBits, 0, this.bits.length);
  595. this.bits = newBits;
  596. }
  597. }
  598. /**
  599. * @param i bit to get
  600. * @return true iff bit i is set
  601. */
  602. get(i /*int*/) {
  603. return (this.bits[Math.floor(i / 32)] & (1 << (i & 0x1F))) !== 0;
  604. }
  605. /**
  606. * Sets bit i.
  607. *
  608. * @param i bit to set
  609. */
  610. set(i /*int*/) {
  611. this.bits[Math.floor(i / 32)] |= 1 << (i & 0x1F);
  612. }
  613. /**
  614. * Flips bit i.
  615. *
  616. * @param i bit to set
  617. */
  618. flip(i /*int*/) {
  619. this.bits[Math.floor(i / 32)] ^= 1 << (i & 0x1F);
  620. }
  621. /**
  622. * @param from first bit to check
  623. * @return index of first bit that is set, starting from the given index, or size if none are set
  624. * at or beyond this given index
  625. * @see #getNextUnset(int)
  626. */
  627. getNextSet(from /*int*/) {
  628. const size = this.size;
  629. if (from >= size) {
  630. return size;
  631. }
  632. const bits = this.bits;
  633. let bitsOffset = Math.floor(from / 32);
  634. let currentBits = bits[bitsOffset];
  635. // mask off lesser bits first
  636. currentBits &= ~((1 << (from & 0x1F)) - 1);
  637. const length = bits.length;
  638. while (currentBits === 0) {
  639. if (++bitsOffset === length) {
  640. return size;
  641. }
  642. currentBits = bits[bitsOffset];
  643. }
  644. const result = (bitsOffset * 32) + Integer.numberOfTrailingZeros(currentBits);
  645. return result > size ? size : result;
  646. }
  647. /**
  648. * @param from index to start looking for unset bit
  649. * @return index of next unset bit, or {@code size} if none are unset until the end
  650. * @see #getNextSet(int)
  651. */
  652. getNextUnset(from /*int*/) {
  653. const size = this.size;
  654. if (from >= size) {
  655. return size;
  656. }
  657. const bits = this.bits;
  658. let bitsOffset = Math.floor(from / 32);
  659. let currentBits = ~bits[bitsOffset];
  660. // mask off lesser bits first
  661. currentBits &= ~((1 << (from & 0x1F)) - 1);
  662. const length = bits.length;
  663. while (currentBits === 0) {
  664. if (++bitsOffset === length) {
  665. return size;
  666. }
  667. currentBits = ~bits[bitsOffset];
  668. }
  669. const result = (bitsOffset * 32) + Integer.numberOfTrailingZeros(currentBits);
  670. return result > size ? size : result;
  671. }
  672. /**
  673. * Sets a block of 32 bits, starting at bit i.
  674. *
  675. * @param i first bit to set
  676. * @param newBits the new value of the next 32 bits. Note again that the least-significant bit
  677. * corresponds to bit i, the next-least-significant to i+1, and so on.
  678. */
  679. setBulk(i /*int*/, newBits /*int*/) {
  680. this.bits[Math.floor(i / 32)] = newBits;
  681. }
  682. /**
  683. * Sets a range of bits.
  684. *
  685. * @param start start of range, inclusive.
  686. * @param end end of range, exclusive
  687. */
  688. setRange(start /*int*/, end /*int*/) {
  689. if (end < start || start < 0 || end > this.size) {
  690. throw new IllegalArgumentException();
  691. }
  692. if (end === start) {
  693. return;
  694. }
  695. end--; // will be easier to treat this as the last actually set bit -- inclusive
  696. const firstInt = Math.floor(start / 32);
  697. const lastInt = Math.floor(end / 32);
  698. const bits = this.bits;
  699. for (let i = firstInt; i <= lastInt; i++) {
  700. const firstBit = i > firstInt ? 0 : start & 0x1F;
  701. const lastBit = i < lastInt ? 31 : end & 0x1F;
  702. // Ones from firstBit to lastBit, inclusive
  703. const mask = (2 << lastBit) - (1 << firstBit);
  704. bits[i] |= mask;
  705. }
  706. }
  707. /**
  708. * Clears all bits (sets to false).
  709. */
  710. clear() {
  711. const max = this.bits.length;
  712. const bits = this.bits;
  713. for (let i = 0; i < max; i++) {
  714. bits[i] = 0;
  715. }
  716. }
  717. /**
  718. * Efficient method to check if a range of bits is set, or not set.
  719. *
  720. * @param start start of range, inclusive.
  721. * @param end end of range, exclusive
  722. * @param value if true, checks that bits in range are set, otherwise checks that they are not set
  723. * @return true iff all bits are set or not set in range, according to value argument
  724. * @throws IllegalArgumentException if end is less than start or the range is not contained in the array
  725. */
  726. isRange(start /*int*/, end /*int*/, value) {
  727. if (end < start || start < 0 || end > this.size) {
  728. throw new IllegalArgumentException();
  729. }
  730. if (end === start) {
  731. return true; // empty range matches
  732. }
  733. end--; // will be easier to treat this as the last actually set bit -- inclusive
  734. const firstInt = Math.floor(start / 32);
  735. const lastInt = Math.floor(end / 32);
  736. const bits = this.bits;
  737. for (let i = firstInt; i <= lastInt; i++) {
  738. const firstBit = i > firstInt ? 0 : start & 0x1F;
  739. const lastBit = i < lastInt ? 31 : end & 0x1F;
  740. // Ones from firstBit to lastBit, inclusive
  741. const mask = (2 << lastBit) - (1 << firstBit) & 0xFFFFFFFF;
  742. // TYPESCRIPTPORT: & 0xFFFFFFFF added to discard anything after 32 bits, as ES has 53 bits
  743. // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (is: that,
  744. // equals the mask, or we're looking for 0s and the masked portion is not all 0s
  745. if ((bits[i] & mask) !== (value ? mask : 0)) {
  746. return false;
  747. }
  748. }
  749. return true;
  750. }
  751. appendBit(bit) {
  752. this.ensureCapacity(this.size + 1);
  753. if (bit) {
  754. this.bits[Math.floor(this.size / 32)] |= 1 << (this.size & 0x1F);
  755. }
  756. this.size++;
  757. }
  758. /**
  759. * Appends the least-significant bits, from value, in order from most-significant to
  760. * least-significant. For example, appending 6 bits from 0x000001E will append the bits
  761. * 0, 1, 1, 1, 1, 0 in that order.
  762. *
  763. * @param value {@code int} containing bits to append
  764. * @param numBits bits from value to append
  765. */
  766. appendBits(value /*int*/, numBits /*int*/) {
  767. if (numBits < 0 || numBits > 32) {
  768. throw new IllegalArgumentException('Num bits must be between 0 and 32');
  769. }
  770. this.ensureCapacity(this.size + numBits);
  771. // const appendBit = this.appendBit;
  772. for (let numBitsLeft = numBits; numBitsLeft > 0; numBitsLeft--) {
  773. this.appendBit(((value >> (numBitsLeft - 1)) & 0x01) === 1);
  774. }
  775. }
  776. appendBitArray(other) {
  777. const otherSize = other.size;
  778. this.ensureCapacity(this.size + otherSize);
  779. // const appendBit = this.appendBit;
  780. for (let i = 0; i < otherSize; i++) {
  781. this.appendBit(other.get(i));
  782. }
  783. }
  784. xor(other) {
  785. if (this.size !== other.size) {
  786. throw new IllegalArgumentException('Sizes don\'t match');
  787. }
  788. const bits = this.bits;
  789. for (let i = 0, length = bits.length; i < length; i++) {
  790. // The last int could be incomplete (i.e. not have 32 bits in
  791. // it) but there is no problem since 0 XOR 0 == 0.
  792. bits[i] ^= other.bits[i];
  793. }
  794. }
  795. /**
  796. *
  797. * @param bitOffset first bit to start writing
  798. * @param array array to write into. Bytes are written most-significant byte first. This is the opposite
  799. * of the internal representation, which is exposed by {@link #getBitArray()}
  800. * @param offset position in array to start writing
  801. * @param numBytes how many bytes to write
  802. */
  803. toBytes(bitOffset /*int*/, array, offset /*int*/, numBytes /*int*/) {
  804. for (let i = 0; i < numBytes; i++) {
  805. let theByte = 0;
  806. for (let j = 0; j < 8; j++) {
  807. if (this.get(bitOffset)) {
  808. theByte |= 1 << (7 - j);
  809. }
  810. bitOffset++;
  811. }
  812. array[offset + i] = /*(byte)*/ theByte;
  813. }
  814. }
  815. /**
  816. * @return underlying array of ints. The first element holds the first 32 bits, and the least
  817. * significant bit is bit 0.
  818. */
  819. getBitArray() {
  820. return this.bits;
  821. }
  822. /**
  823. * Reverses all bits in the array.
  824. */
  825. reverse() {
  826. const newBits = new Int32Array(this.bits.length);
  827. // reverse all int's first
  828. const len = Math.floor((this.size - 1) / 32);
  829. const oldBitsLen = len + 1;
  830. const bits = this.bits;
  831. for (let i = 0; i < oldBitsLen; i++) {
  832. let x = bits[i];
  833. x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
  834. x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
  835. x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
  836. x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
  837. x = ((x >> 16) & 0x0000ffff) | ((x & 0x0000ffff) << 16);
  838. newBits[len - i] = /*(int)*/ x;
  839. }
  840. // now correct the int's if the bit size isn't a multiple of 32
  841. if (this.size !== oldBitsLen * 32) {
  842. const leftOffset = oldBitsLen * 32 - this.size;
  843. let currentInt = newBits[0] >>> leftOffset;
  844. for (let i = 1; i < oldBitsLen; i++) {
  845. const nextInt = newBits[i];
  846. currentInt |= nextInt << (32 - leftOffset);
  847. newBits[i - 1] = currentInt;
  848. currentInt = nextInt >>> leftOffset;
  849. }
  850. newBits[oldBitsLen - 1] = currentInt;
  851. }
  852. this.bits = newBits;
  853. }
  854. static makeArray(size /*int*/) {
  855. return new Int32Array(Math.floor((size + 31) / 32));
  856. }
  857. /*@Override*/
  858. equals(o) {
  859. if (!(o instanceof BitArray)) {
  860. return false;
  861. }
  862. const other = o;
  863. return this.size === other.size && Arrays.equals(this.bits, other.bits);
  864. }
  865. /*@Override*/
  866. hashCode() {
  867. return 31 * this.size + Arrays.hashCode(this.bits);
  868. }
  869. /*@Override*/
  870. toString() {
  871. let result = '';
  872. for (let i = 0, size = this.size; i < size; i++) {
  873. if ((i & 0x07) === 0) {
  874. result += ' ';
  875. }
  876. result += this.get(i) ? 'X' : '.';
  877. }
  878. return result;
  879. }
  880. /*@Override*/
  881. clone() {
  882. return new BitArray(this.size, this.bits.slice());
  883. }
  884. /**
  885. * converts to boolean array.
  886. */
  887. toArray() {
  888. let result = [];
  889. for (let i = 0, size = this.size; i < size; i++) {
  890. result.push(this.get(i));
  891. }
  892. return result;
  893. }
  894. }
  895. /*
  896. * Copyright 2009 ZXing authors
  897. *
  898. * Licensed under the Apache License, Version 2.0 (the "License");
  899. * you may not use this file except in compliance with the License.
  900. * You may obtain a copy of the License at
  901. *
  902. * http://www.apache.org/licenses/LICENSE-2.0
  903. *
  904. * Unless required by applicable law or agreed to in writing, software
  905. * distributed under the License is distributed on an "AS IS" BASIS,
  906. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  907. * See the License for the specific language governing permissions and
  908. * limitations under the License.
  909. */
  910. /*namespace com.google.zxing {*/
  911. /**
  912. * Encapsulates a type of hint that a caller may pass to a barcode reader to help it
  913. * more quickly or accurately decode it. It is up to implementations to decide what,
  914. * if anything, to do with the information that is supplied.
  915. *
  916. * @author Sean Owen
  917. * @author dswitkin@google.com (Daniel Switkin)
  918. * @see Reader#decode(BinaryBitmap,java.util.Map)
  919. */
  920. var DecodeHintType;
  921. (function (DecodeHintType) {
  922. /**
  923. * Unspecified, application-specific hint. Maps to an unspecified {@link Object}.
  924. */
  925. DecodeHintType[DecodeHintType["OTHER"] = 0] = "OTHER"; /*(Object.class)*/
  926. /**
  927. * Image is a pure monochrome image of a barcode. Doesn't matter what it maps to;
  928. * use {@link Boolean#TRUE}.
  929. */
  930. DecodeHintType[DecodeHintType["PURE_BARCODE"] = 1] = "PURE_BARCODE"; /*(Void.class)*/
  931. /**
  932. * Image is known to be of one of a few possible formats.
  933. * Maps to a {@link List} of {@link BarcodeFormat}s.
  934. */
  935. DecodeHintType[DecodeHintType["POSSIBLE_FORMATS"] = 2] = "POSSIBLE_FORMATS"; /*(List.class)*/
  936. /**
  937. * Spend more time to try to find a barcode; optimize for accuracy, not speed.
  938. * Doesn't matter what it maps to; use {@link Boolean#TRUE}.
  939. */
  940. DecodeHintType[DecodeHintType["TRY_HARDER"] = 3] = "TRY_HARDER"; /*(Void.class)*/
  941. /**
  942. * Specifies what character encoding to use when decoding, where applicable (type String)
  943. */
  944. DecodeHintType[DecodeHintType["CHARACTER_SET"] = 4] = "CHARACTER_SET"; /*(String.class)*/
  945. /**
  946. * Allowed lengths of encoded data -- reject anything else. Maps to an {@code Int32Array}.
  947. */
  948. DecodeHintType[DecodeHintType["ALLOWED_LENGTHS"] = 5] = "ALLOWED_LENGTHS"; /*(Int32Array.class)*/
  949. /**
  950. * Assume Code 39 codes employ a check digit. Doesn't matter what it maps to;
  951. * use {@link Boolean#TRUE}.
  952. */
  953. DecodeHintType[DecodeHintType["ASSUME_CODE_39_CHECK_DIGIT"] = 6] = "ASSUME_CODE_39_CHECK_DIGIT"; /*(Void.class)*/
  954. /**
  955. * Enable extended mode for Code 39 codes. Doesn't matter what it maps to;
  956. * use {@link Boolean#TRUE}.
  957. */
  958. DecodeHintType[DecodeHintType["ENABLE_CODE_39_EXTENDED_MODE"] = 7] = "ENABLE_CODE_39_EXTENDED_MODE"; /*(Void.class)*/
  959. /**
  960. * Assume the barcode is being processed as a GS1 barcode, and modify behavior as needed.
  961. * For example this affects FNC1 handling for Code 128 (aka GS1-128). Doesn't matter what it maps to;
  962. * use {@link Boolean#TRUE}.
  963. */
  964. DecodeHintType[DecodeHintType["ASSUME_GS1"] = 8] = "ASSUME_GS1"; /*(Void.class)*/
  965. /**
  966. * If true, return the start and end digits in a Codabar barcode instead of stripping them. They
  967. * are alpha, whereas the rest are numeric. By default, they are stripped, but this causes them
  968. * to not be. Doesn't matter what it maps to; use {@link Boolean#TRUE}.
  969. */
  970. DecodeHintType[DecodeHintType["RETURN_CODABAR_START_END"] = 9] = "RETURN_CODABAR_START_END"; /*(Void.class)*/
  971. /**
  972. * The caller needs to be notified via callback when a possible {@link ResultPoint}
  973. * is found. Maps to a {@link ResultPointCallback}.
  974. */
  975. DecodeHintType[DecodeHintType["NEED_RESULT_POINT_CALLBACK"] = 10] = "NEED_RESULT_POINT_CALLBACK"; /*(ResultPointCallback.class)*/
  976. /**
  977. * Allowed extension lengths for EAN or UPC barcodes. Other formats will ignore this.
  978. * Maps to an {@code Int32Array} of the allowed extension lengths, for example [2], [5], or [2, 5].
  979. * If it is optional to have an extension, do not set this hint. If this is set,
  980. * and a UPC or EAN barcode is found but an extension is not, then no result will be returned
  981. * at all.
  982. */
  983. DecodeHintType[DecodeHintType["ALLOWED_EAN_EXTENSIONS"] = 11] = "ALLOWED_EAN_EXTENSIONS"; /*(Int32Array.class)*/
  984. // End of enumeration values.
  985. /**
  986. * Data type the hint is expecting.
  987. * Among the possible values the {@link Void} stands out as being used for
  988. * hints that do not expect a value to be supplied (flag hints). Such hints
  989. * will possibly have their value ignored, or replaced by a
  990. * {@link Boolean#TRUE}. Hint suppliers should probably use
  991. * {@link Boolean#TRUE} as directed by the actual hint documentation.
  992. */
  993. // private valueType: Class<?>
  994. // DecodeHintType(valueType: Class<?>) {
  995. // this.valueType = valueType
  996. // }
  997. // public getValueType(): Class<?> {
  998. // return valueType
  999. // }
  1000. })(DecodeHintType || (DecodeHintType = {}));
  1001. var DecodeHintType$1 = DecodeHintType;
  1002. /**
  1003. * Custom Error class of type Exception.
  1004. */
  1005. class FormatException extends Exception {
  1006. static getFormatInstance() {
  1007. return new FormatException();
  1008. }
  1009. }
  1010. FormatException.kind = 'FormatException';
  1011. /*
  1012. * Copyright 2008 ZXing authors
  1013. *
  1014. * Licensed under the Apache License, Version 2.0 (the "License");
  1015. * you may not use this file except in compliance with the License.
  1016. * You may obtain a copy of the License at
  1017. *
  1018. * http://www.apache.org/licenses/LICENSE-2.0
  1019. *
  1020. * Unless required by applicable law or agreed to in writing, software
  1021. * distributed under the License is distributed on an "AS IS" BASIS,
  1022. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1023. * See the License for the specific language governing permissions and
  1024. * limitations under the License.
  1025. */
  1026. /*import java.util.HashMap;*/
  1027. /*import java.util.Map;*/
  1028. var CharacterSetValueIdentifiers;
  1029. (function (CharacterSetValueIdentifiers) {
  1030. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["Cp437"] = 0] = "Cp437";
  1031. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_1"] = 1] = "ISO8859_1";
  1032. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_2"] = 2] = "ISO8859_2";
  1033. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_3"] = 3] = "ISO8859_3";
  1034. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_4"] = 4] = "ISO8859_4";
  1035. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_5"] = 5] = "ISO8859_5";
  1036. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_6"] = 6] = "ISO8859_6";
  1037. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_7"] = 7] = "ISO8859_7";
  1038. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_8"] = 8] = "ISO8859_8";
  1039. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_9"] = 9] = "ISO8859_9";
  1040. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_10"] = 10] = "ISO8859_10";
  1041. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_11"] = 11] = "ISO8859_11";
  1042. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_13"] = 12] = "ISO8859_13";
  1043. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_14"] = 13] = "ISO8859_14";
  1044. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_15"] = 14] = "ISO8859_15";
  1045. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ISO8859_16"] = 15] = "ISO8859_16";
  1046. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["SJIS"] = 16] = "SJIS";
  1047. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["Cp1250"] = 17] = "Cp1250";
  1048. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["Cp1251"] = 18] = "Cp1251";
  1049. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["Cp1252"] = 19] = "Cp1252";
  1050. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["Cp1256"] = 20] = "Cp1256";
  1051. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["UnicodeBigUnmarked"] = 21] = "UnicodeBigUnmarked";
  1052. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["UTF8"] = 22] = "UTF8";
  1053. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["ASCII"] = 23] = "ASCII";
  1054. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["Big5"] = 24] = "Big5";
  1055. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["GB18030"] = 25] = "GB18030";
  1056. CharacterSetValueIdentifiers[CharacterSetValueIdentifiers["EUC_KR"] = 26] = "EUC_KR";
  1057. })(CharacterSetValueIdentifiers || (CharacterSetValueIdentifiers = {}));
  1058. /**
  1059. * Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1
  1060. * of ISO 18004.
  1061. *
  1062. * @author Sean Owen
  1063. */
  1064. class CharacterSetECI {
  1065. constructor(valueIdentifier, valuesParam, name, ...otherEncodingNames) {
  1066. this.valueIdentifier = valueIdentifier;
  1067. this.name = name;
  1068. if (typeof valuesParam === 'number') {
  1069. this.values = Int32Array.from([valuesParam]);
  1070. }
  1071. else {
  1072. this.values = valuesParam;
  1073. }
  1074. this.otherEncodingNames = otherEncodingNames;
  1075. CharacterSetECI.VALUE_IDENTIFIER_TO_ECI.set(valueIdentifier, this);
  1076. CharacterSetECI.NAME_TO_ECI.set(name, this);
  1077. const values = this.values;
  1078. for (let i = 0, length = values.length; i !== length; i++) {
  1079. const v = values[i];
  1080. CharacterSetECI.VALUES_TO_ECI.set(v, this);
  1081. }
  1082. for (const otherName of otherEncodingNames) {
  1083. CharacterSetECI.NAME_TO_ECI.set(otherName, this);
  1084. }
  1085. }
  1086. // CharacterSetECI(value: number /*int*/) {
  1087. // this(new Int32Array {value})
  1088. // }
  1089. // CharacterSetECI(value: number /*int*/, String... otherEncodingNames) {
  1090. // this.values = new Int32Array {value}
  1091. // this.otherEncodingNames = otherEncodingNames
  1092. // }
  1093. // CharacterSetECI(values: Int32Array, String... otherEncodingNames) {
  1094. // this.values = values
  1095. // this.otherEncodingNames = otherEncodingNames
  1096. // }
  1097. getValueIdentifier() {
  1098. return this.valueIdentifier;
  1099. }
  1100. getName() {
  1101. return this.name;
  1102. }
  1103. getValue() {
  1104. return this.values[0];
  1105. }
  1106. /**
  1107. * @param value character set ECI value
  1108. * @return {@code CharacterSetECI} representing ECI of given value, or null if it is legal but
  1109. * unsupported
  1110. * @throws FormatException if ECI value is invalid
  1111. */
  1112. static getCharacterSetECIByValue(value /*int*/) {
  1113. if (value < 0 || value >= 900) {
  1114. throw new FormatException('incorect value');
  1115. }
  1116. const characterSet = CharacterSetECI.VALUES_TO_ECI.get(value);
  1117. if (undefined === characterSet) {
  1118. throw new FormatException('incorect value');
  1119. }
  1120. return characterSet;
  1121. }
  1122. /**
  1123. * @param name character set ECI encoding name
  1124. * @return CharacterSetECI representing ECI for character encoding, or null if it is legal
  1125. * but unsupported
  1126. */
  1127. static getCharacterSetECIByName(name) {
  1128. const characterSet = CharacterSetECI.NAME_TO_ECI.get(name);
  1129. if (undefined === characterSet) {
  1130. throw new FormatException('incorect value');
  1131. }
  1132. return characterSet;
  1133. }
  1134. equals(o) {
  1135. if (!(o instanceof CharacterSetECI)) {
  1136. return false;
  1137. }
  1138. const other = o;
  1139. return this.getName() === other.getName();
  1140. }
  1141. }
  1142. CharacterSetECI.VALUE_IDENTIFIER_TO_ECI = new Map();
  1143. CharacterSetECI.VALUES_TO_ECI = new Map();
  1144. CharacterSetECI.NAME_TO_ECI = new Map();
  1145. // Enum name is a Java encoding valid for java.lang and java.io
  1146. // TYPESCRIPTPORT: changed the main label for ISO as the TextEncoder did not recognized them in the form from java
  1147. // (eg ISO8859_1 must be ISO88591 or ISO8859-1 or ISO-8859-1)
  1148. // later on: well, except 16 wich does not work with ISO885916 so used ISO-8859-1 form for default
  1149. CharacterSetECI.Cp437 = new CharacterSetECI(CharacterSetValueIdentifiers.Cp437, Int32Array.from([0, 2]), 'Cp437');
  1150. CharacterSetECI.ISO8859_1 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_1, Int32Array.from([1, 3]), 'ISO-8859-1', 'ISO88591', 'ISO8859_1');
  1151. CharacterSetECI.ISO8859_2 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_2, 4, 'ISO-8859-2', 'ISO88592', 'ISO8859_2');
  1152. CharacterSetECI.ISO8859_3 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_3, 5, 'ISO-8859-3', 'ISO88593', 'ISO8859_3');
  1153. CharacterSetECI.ISO8859_4 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_4, 6, 'ISO-8859-4', 'ISO88594', 'ISO8859_4');
  1154. CharacterSetECI.ISO8859_5 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_5, 7, 'ISO-8859-5', 'ISO88595', 'ISO8859_5');
  1155. CharacterSetECI.ISO8859_6 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_6, 8, 'ISO-8859-6', 'ISO88596', 'ISO8859_6');
  1156. CharacterSetECI.ISO8859_7 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_7, 9, 'ISO-8859-7', 'ISO88597', 'ISO8859_7');
  1157. CharacterSetECI.ISO8859_8 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_8, 10, 'ISO-8859-8', 'ISO88598', 'ISO8859_8');
  1158. CharacterSetECI.ISO8859_9 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_9, 11, 'ISO-8859-9', 'ISO88599', 'ISO8859_9');
  1159. CharacterSetECI.ISO8859_10 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_10, 12, 'ISO-8859-10', 'ISO885910', 'ISO8859_10');
  1160. CharacterSetECI.ISO8859_11 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_11, 13, 'ISO-8859-11', 'ISO885911', 'ISO8859_11');
  1161. CharacterSetECI.ISO8859_13 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_13, 15, 'ISO-8859-13', 'ISO885913', 'ISO8859_13');
  1162. CharacterSetECI.ISO8859_14 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_14, 16, 'ISO-8859-14', 'ISO885914', 'ISO8859_14');
  1163. CharacterSetECI.ISO8859_15 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_15, 17, 'ISO-8859-15', 'ISO885915', 'ISO8859_15');
  1164. CharacterSetECI.ISO8859_16 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_16, 18, 'ISO-8859-16', 'ISO885916', 'ISO8859_16');
  1165. CharacterSetECI.SJIS = new CharacterSetECI(CharacterSetValueIdentifiers.SJIS, 20, 'SJIS', 'Shift_JIS');
  1166. CharacterSetECI.Cp1250 = new CharacterSetECI(CharacterSetValueIdentifiers.Cp1250, 21, 'Cp1250', 'windows-1250');
  1167. CharacterSetECI.Cp1251 = new CharacterSetECI(CharacterSetValueIdentifiers.Cp1251, 22, 'Cp1251', 'windows-1251');
  1168. CharacterSetECI.Cp1252 = new CharacterSetECI(CharacterSetValueIdentifiers.Cp1252, 23, 'Cp1252', 'windows-1252');
  1169. CharacterSetECI.Cp1256 = new CharacterSetECI(CharacterSetValueIdentifiers.Cp1256, 24, 'Cp1256', 'windows-1256');
  1170. CharacterSetECI.UnicodeBigUnmarked = new CharacterSetECI(CharacterSetValueIdentifiers.UnicodeBigUnmarked, 25, 'UnicodeBigUnmarked', 'UTF-16BE', 'UnicodeBig');
  1171. CharacterSetECI.UTF8 = new CharacterSetECI(CharacterSetValueIdentifiers.UTF8, 26, 'UTF8', 'UTF-8');
  1172. CharacterSetECI.ASCII = new CharacterSetECI(CharacterSetValueIdentifiers.ASCII, Int32Array.from([27, 170]), 'ASCII', 'US-ASCII');
  1173. CharacterSetECI.Big5 = new CharacterSetECI(CharacterSetValueIdentifiers.Big5, 28, 'Big5');
  1174. CharacterSetECI.GB18030 = new CharacterSetECI(CharacterSetValueIdentifiers.GB18030, 29, 'GB18030', 'GB2312', 'EUC_CN', 'GBK');
  1175. CharacterSetECI.EUC_KR = new CharacterSetECI(CharacterSetValueIdentifiers.EUC_KR, 30, 'EUC_KR', 'EUC-KR');
  1176. /**
  1177. * Custom Error class of type Exception.
  1178. */
  1179. class UnsupportedOperationException extends Exception {
  1180. }
  1181. UnsupportedOperationException.kind = 'UnsupportedOperationException';
  1182. /**
  1183. * Responsible for en/decoding strings.
  1184. */
  1185. class StringEncoding {
  1186. /**
  1187. * Decodes some Uint8Array to a string format.
  1188. */
  1189. static decode(bytes, encoding) {
  1190. const encodingName = this.encodingName(encoding);
  1191. if (this.customDecoder) {
  1192. return this.customDecoder(bytes, encodingName);
  1193. }
  1194. // Increases browser support.
  1195. if (typeof TextDecoder === 'undefined' || this.shouldDecodeOnFallback(encodingName)) {
  1196. return this.decodeFallback(bytes, encodingName);
  1197. }
  1198. return new TextDecoder(encodingName).decode(bytes);
  1199. }
  1200. /**
  1201. * Checks if the decoding method should use the fallback for decoding
  1202. * once Node TextDecoder doesn't support all encoding formats.
  1203. *
  1204. * @param encodingName
  1205. */
  1206. static shouldDecodeOnFallback(encodingName) {
  1207. return !StringEncoding.isBrowser() && encodingName === 'ISO-8859-1';
  1208. }
  1209. /**
  1210. * Encodes some string into a Uint8Array.
  1211. */
  1212. static encode(s, encoding) {
  1213. const encodingName = this.encodingName(encoding);
  1214. if (this.customEncoder) {
  1215. return this.customEncoder(s, encodingName);
  1216. }
  1217. // Increases browser support.
  1218. if (typeof TextEncoder === 'undefined') {
  1219. return this.encodeFallback(s);
  1220. }
  1221. // TextEncoder only encodes to UTF8 by default as specified by encoding.spec.whatwg.org
  1222. return new TextEncoder().encode(s);
  1223. }
  1224. static isBrowser() {
  1225. return (typeof window !== 'undefined' && {}.toString.call(window) === '[object Window]');
  1226. }
  1227. /**
  1228. * Returns the string value from some encoding character set.
  1229. */
  1230. static encodingName(encoding) {
  1231. return typeof encoding === 'string'
  1232. ? encoding
  1233. : encoding.getName();
  1234. }
  1235. /**
  1236. * Returns character set from some encoding character set.
  1237. */
  1238. static encodingCharacterSet(encoding) {
  1239. if (encoding instanceof CharacterSetECI) {
  1240. return encoding;
  1241. }
  1242. return CharacterSetECI.getCharacterSetECIByName(encoding);
  1243. }
  1244. /**
  1245. * Runs a fallback for the native decoding funcion.
  1246. */
  1247. static decodeFallback(bytes, encoding) {
  1248. const characterSet = this.encodingCharacterSet(encoding);
  1249. if (StringEncoding.isDecodeFallbackSupported(characterSet)) {
  1250. let s = '';
  1251. for (let i = 0, length = bytes.length; i < length; i++) {
  1252. let h = bytes[i].toString(16);
  1253. if (h.length < 2) {
  1254. h = '0' + h;
  1255. }
  1256. s += '%' + h;
  1257. }
  1258. return decodeURIComponent(s);
  1259. }
  1260. if (characterSet.equals(CharacterSetECI.UnicodeBigUnmarked)) {
  1261. return String.fromCharCode.apply(null, new Uint16Array(bytes.buffer));
  1262. }
  1263. throw new UnsupportedOperationException(`Encoding ${this.encodingName(encoding)} not supported by fallback.`);
  1264. }
  1265. static isDecodeFallbackSupported(characterSet) {
  1266. return characterSet.equals(CharacterSetECI.UTF8) ||
  1267. characterSet.equals(CharacterSetECI.ISO8859_1) ||
  1268. characterSet.equals(CharacterSetECI.ASCII);
  1269. }
  1270. /**
  1271. * Runs a fallback for the native encoding funcion.
  1272. *
  1273. * @see https://stackoverflow.com/a/17192845/4367683
  1274. */
  1275. static encodeFallback(s) {
  1276. const encodedURIstring = btoa(unescape(encodeURIComponent(s)));
  1277. const charList = encodedURIstring.split('');
  1278. const uintArray = [];
  1279. for (let i = 0; i < charList.length; i++) {
  1280. uintArray.push(charList[i].charCodeAt(0));
  1281. }
  1282. return new Uint8Array(uintArray);
  1283. }
  1284. }
  1285. /*
  1286. * Copyright (C) 2010 ZXing authors
  1287. *
  1288. * Licensed under the Apache License, Version 2.0 (the "License");
  1289. * you may not use this file except in compliance with the License.
  1290. * You may obtain a copy of the License at
  1291. *
  1292. * http://www.apache.org/licenses/LICENSE-2.0
  1293. *
  1294. * Unless required by applicable law or agreed to in writing, software
  1295. * distributed under the License is distributed on an "AS IS" BASIS,
  1296. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1297. * See the License for the specific language governing permissions and
  1298. * limitations under the License.
  1299. */
  1300. /**
  1301. * Common string-related functions.
  1302. *
  1303. * @author Sean Owen
  1304. * @author Alex Dupre
  1305. */
  1306. class StringUtils {
  1307. // SHIFT_JIS.equalsIgnoreCase(PLATFORM_DEFAULT_ENCODING) ||
  1308. // EUC_JP.equalsIgnoreCase(PLATFORM_DEFAULT_ENCODING);
  1309. static castAsNonUtf8Char(code, encoding = null) {
  1310. // ISO 8859-1 is the Java default as UTF-8 is JavaScripts
  1311. // you can see this method as a Java version of String.fromCharCode
  1312. const e = encoding ? encoding.getName() : this.ISO88591;
  1313. // use passed format (fromCharCode will return UTF8 encoding)
  1314. return StringEncoding.decode(new Uint8Array([code]), e);
  1315. }
  1316. /**
  1317. * @param bytes bytes encoding a string, whose encoding should be guessed
  1318. * @param hints decode hints if applicable
  1319. * @return name of guessed encoding; at the moment will only guess one of:
  1320. * {@link #SHIFT_JIS}, {@link #UTF8}, {@link #ISO88591}, or the platform
  1321. * default encoding if none of these can possibly be correct
  1322. */
  1323. static guessEncoding(bytes, hints) {
  1324. if (hints !== null && hints !== undefined && undefined !== hints.get(DecodeHintType$1.CHARACTER_SET)) {
  1325. return hints.get(DecodeHintType$1.CHARACTER_SET).toString();
  1326. }
  1327. // For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS,
  1328. // which should be by far the most common encodings.
  1329. const length = bytes.length;
  1330. let canBeISO88591 = true;
  1331. let canBeShiftJIS = true;
  1332. let canBeUTF8 = true;
  1333. let utf8BytesLeft = 0;
  1334. // int utf8LowChars = 0
  1335. let utf2BytesChars = 0;
  1336. let utf3BytesChars = 0;
  1337. let utf4BytesChars = 0;
  1338. let sjisBytesLeft = 0;
  1339. // int sjisLowChars = 0
  1340. let sjisKatakanaChars = 0;
  1341. // int sjisDoubleBytesChars = 0
  1342. let sjisCurKatakanaWordLength = 0;
  1343. let sjisCurDoubleBytesWordLength = 0;
  1344. let sjisMaxKatakanaWordLength = 0;
  1345. let sjisMaxDoubleBytesWordLength = 0;
  1346. // int isoLowChars = 0
  1347. // int isoHighChars = 0
  1348. let isoHighOther = 0;
  1349. const utf8bom = bytes.length > 3 &&
  1350. bytes[0] === /*(byte) */ 0xEF &&
  1351. bytes[1] === /*(byte) */ 0xBB &&
  1352. bytes[2] === /*(byte) */ 0xBF;
  1353. for (let i = 0; i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8); i++) {
  1354. const value = bytes[i] & 0xFF;
  1355. // UTF-8 stuff
  1356. if (canBeUTF8) {
  1357. if (utf8BytesLeft > 0) {
  1358. if ((value & 0x80) === 0) {
  1359. canBeUTF8 = false;
  1360. }
  1361. else {
  1362. utf8BytesLeft--;
  1363. }
  1364. }
  1365. else if ((value & 0x80) !== 0) {
  1366. if ((value & 0x40) === 0) {
  1367. canBeUTF8 = false;
  1368. }
  1369. else {
  1370. utf8BytesLeft++;
  1371. if ((value & 0x20) === 0) {
  1372. utf2BytesChars++;
  1373. }
  1374. else {
  1375. utf8BytesLeft++;
  1376. if ((value & 0x10) === 0) {
  1377. utf3BytesChars++;
  1378. }
  1379. else {
  1380. utf8BytesLeft++;
  1381. if ((value & 0x08) === 0) {
  1382. utf4BytesChars++;
  1383. }
  1384. else {
  1385. canBeUTF8 = false;
  1386. }
  1387. }
  1388. }
  1389. }
  1390. } // else {
  1391. // utf8LowChars++
  1392. // }
  1393. }
  1394. // ISO-8859-1 stuff
  1395. if (canBeISO88591) {
  1396. if (value > 0x7F && value < 0xA0) {
  1397. canBeISO88591 = false;
  1398. }
  1399. else if (value > 0x9F) {
  1400. if (value < 0xC0 || value === 0xD7 || value === 0xF7) {
  1401. isoHighOther++;
  1402. } // else {
  1403. // isoHighChars++
  1404. // }
  1405. } // else {
  1406. // isoLowChars++
  1407. // }
  1408. }
  1409. // Shift_JIS stuff
  1410. if (canBeShiftJIS) {
  1411. if (sjisBytesLeft > 0) {
  1412. if (value < 0x40 || value === 0x7F || value > 0xFC) {
  1413. canBeShiftJIS = false;
  1414. }
  1415. else {
  1416. sjisBytesLeft--;
  1417. }
  1418. }
  1419. else if (value === 0x80 || value === 0xA0 || value > 0xEF) {
  1420. canBeShiftJIS = false;
  1421. }
  1422. else if (value > 0xA0 && value < 0xE0) {
  1423. sjisKatakanaChars++;
  1424. sjisCurDoubleBytesWordLength = 0;
  1425. sjisCurKatakanaWordLength++;
  1426. if (sjisCurKatakanaWordLength > sjisMaxKatakanaWordLength) {
  1427. sjisMaxKatakanaWordLength = sjisCurKatakanaWordLength;
  1428. }
  1429. }
  1430. else if (value > 0x7F) {
  1431. sjisBytesLeft++;
  1432. // sjisDoubleBytesChars++
  1433. sjisCurKatakanaWordLength = 0;
  1434. sjisCurDoubleBytesWordLength++;
  1435. if (sjisCurDoubleBytesWordLength > sjisMaxDoubleBytesWordLength) {
  1436. sjisMaxDoubleBytesWordLength = sjisCurDoubleBytesWordLength;
  1437. }
  1438. }
  1439. else {
  1440. // sjisLowChars++
  1441. sjisCurKatakanaWordLength = 0;
  1442. sjisCurDoubleBytesWordLength = 0;
  1443. }
  1444. }
  1445. }
  1446. if (canBeUTF8 && utf8BytesLeft > 0) {
  1447. canBeUTF8 = false;
  1448. }
  1449. if (canBeShiftJIS && sjisBytesLeft > 0) {
  1450. canBeShiftJIS = false;
  1451. }
  1452. // Easy -- if there is BOM or at least 1 valid not-single byte character (and no evidence it can't be UTF-8), done
  1453. if (canBeUTF8 && (utf8bom || utf2BytesChars + utf3BytesChars + utf4BytesChars > 0)) {
  1454. return StringUtils.UTF8;
  1455. }
  1456. // Easy -- if assuming Shift_JIS or at least 3 valid consecutive not-ascii characters (and no evidence it can't be), done
  1457. if (canBeShiftJIS && (StringUtils.ASSUME_SHIFT_JIS || sjisMaxKatakanaWordLength >= 3 || sjisMaxDoubleBytesWordLength >= 3)) {
  1458. return StringUtils.SHIFT_JIS;
  1459. }
  1460. // Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is:
  1461. // - If we saw
  1462. // - only two consecutive katakana chars in the whole text, or
  1463. // - at least 10% of bytes that could be "upper" not-alphanumeric Latin1,
  1464. // - then we conclude Shift_JIS, else ISO-8859-1
  1465. if (canBeISO88591 && canBeShiftJIS) {
  1466. return (sjisMaxKatakanaWordLength === 2 && sjisKatakanaChars === 2) || isoHighOther * 10 >= length
  1467. ? StringUtils.SHIFT_JIS : StringUtils.ISO88591;
  1468. }
  1469. // Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding
  1470. if (canBeISO88591) {
  1471. return StringUtils.ISO88591;
  1472. }
  1473. if (canBeShiftJIS) {
  1474. return StringUtils.SHIFT_JIS;
  1475. }
  1476. if (canBeUTF8) {
  1477. return StringUtils.UTF8;
  1478. }
  1479. // Otherwise, we take a wild guess with platform encoding
  1480. return StringUtils.PLATFORM_DEFAULT_ENCODING;
  1481. }
  1482. /**
  1483. *
  1484. * @see https://stackoverflow.com/a/13439711/4367683
  1485. *
  1486. * @param append The new string to append.
  1487. * @param args Argumets values to be formated.
  1488. */
  1489. static format(append, ...args) {
  1490. let i = -1;
  1491. function callback(exp, p0, p1, p2, p3, p4) {
  1492. if (exp === '%%')
  1493. return '%';
  1494. if (args[++i] === undefined)
  1495. return undefined;
  1496. exp = p2 ? parseInt(p2.substr(1)) : undefined;
  1497. let base = p3 ? parseInt(p3.substr(1)) : undefined;
  1498. let val;
  1499. switch (p4) {
  1500. case 's':
  1501. val = args[i];
  1502. break;
  1503. case 'c':
  1504. val = args[i][0];
  1505. break;
  1506. case 'f':
  1507. val = parseFloat(args[i]).toFixed(exp);
  1508. break;
  1509. case 'p':
  1510. val = parseFloat(args[i]).toPrecision(exp);
  1511. break;
  1512. case 'e':
  1513. val = parseFloat(args[i]).toExponential(exp);
  1514. break;
  1515. case 'x':
  1516. val = parseInt(args[i]).toString(base ? base : 16);
  1517. break;
  1518. case 'd':
  1519. val = parseFloat(parseInt(args[i], base ? base : 10).toPrecision(exp)).toFixed(0);
  1520. break;
  1521. }
  1522. val = typeof val === 'object' ? JSON.stringify(val) : (+val).toString(base);
  1523. let size = parseInt(p1); /* padding size */
  1524. let ch = p1 && (p1[0] + '') === '0' ? '0' : ' '; /* isnull? */
  1525. while (val.length < size)
  1526. val = p0 !== undefined ? val + ch : ch + val; /* isminus? */
  1527. return val;
  1528. }
  1529. let regex = /%(-)?(0?[0-9]+)?([.][0-9]+)?([#][0-9]+)?([scfpexd%])/g;
  1530. return append.replace(regex, callback);
  1531. }
  1532. /**
  1533. *
  1534. */
  1535. static getBytes(str, encoding) {
  1536. return StringEncoding.encode(str, encoding);
  1537. }
  1538. /**
  1539. * Returns the charcode at the specified index or at index zero.
  1540. */
  1541. static getCharCode(str, index = 0) {
  1542. return str.charCodeAt(index);
  1543. }
  1544. /**
  1545. * Returns char for given charcode
  1546. */
  1547. static getCharAt(charCode) {
  1548. return String.fromCharCode(charCode);
  1549. }
  1550. }
  1551. StringUtils.SHIFT_JIS = CharacterSetECI.SJIS.getName(); // "SJIS"
  1552. StringUtils.GB2312 = 'GB2312';
  1553. StringUtils.ISO88591 = CharacterSetECI.ISO8859_1.getName(); // "ISO8859_1"
  1554. StringUtils.EUC_JP = 'EUC_JP';
  1555. StringUtils.UTF8 = CharacterSetECI.UTF8.getName(); // "UTF8"
  1556. StringUtils.PLATFORM_DEFAULT_ENCODING = StringUtils.UTF8; // "UTF8"//Charset.defaultCharset().name()
  1557. StringUtils.ASSUME_SHIFT_JIS = false;
  1558. class StringBuilder {
  1559. constructor(value = '') {
  1560. this.value = value;
  1561. }
  1562. enableDecoding(encoding) {
  1563. this.encoding = encoding;
  1564. return this;
  1565. }
  1566. append(s) {
  1567. if (typeof s === 'string') {
  1568. this.value += s.toString();
  1569. }
  1570. else if (this.encoding) {
  1571. // use passed format (fromCharCode will return UTF8 encoding)
  1572. this.value += StringUtils.castAsNonUtf8Char(s, this.encoding);
  1573. }
  1574. else {
  1575. // correctly converts from UTF-8, but not other encodings
  1576. this.value += String.fromCharCode(s);
  1577. }
  1578. return this;
  1579. }
  1580. appendChars(str, offset, len) {
  1581. for (let i = offset; offset < offset + len; i++) {
  1582. this.append(str[i]);
  1583. }
  1584. return this;
  1585. }
  1586. length() {
  1587. return this.value.length;
  1588. }
  1589. charAt(n) {
  1590. return this.value.charAt(n);
  1591. }
  1592. deleteCharAt(n) {
  1593. this.value = this.value.substr(0, n) + this.value.substring(n + 1);
  1594. }
  1595. setCharAt(n, c) {
  1596. this.value = this.value.substr(0, n) + c + this.value.substr(n + 1);
  1597. }
  1598. substring(start, end) {
  1599. return this.value.substring(start, end);
  1600. }
  1601. /**
  1602. * @note helper method for RSS Expanded
  1603. */
  1604. setLengthToZero() {
  1605. this.value = '';
  1606. }
  1607. toString() {
  1608. return this.value;
  1609. }
  1610. insert(n, c) {
  1611. this.value = this.value.substring(0, n) + c + this.value.substring(n);
  1612. }
  1613. }
  1614. /*
  1615. * Copyright 2007 ZXing authors
  1616. *
  1617. * Licensed under the Apache License, Version 2.0 (the "License");
  1618. * you may not use this file except in compliance with the License.
  1619. * You may obtain a copy of the License at
  1620. *
  1621. * http://www.apache.org/licenses/LICENSE-2.0
  1622. *
  1623. * Unless required by applicable law or agreed to in writing, software
  1624. * distributed under the License is distributed on an "AS IS" BASIS,
  1625. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1626. * See the License for the specific language governing permissions and
  1627. * limitations under the License.
  1628. */
  1629. /**
  1630. * <p>Represents a 2D matrix of bits. In function arguments below, and throughout the common
  1631. * module, x is the column position, and y is the row position. The ordering is always x, y.
  1632. * The origin is at the top-left.</p>
  1633. *
  1634. * <p>Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins
  1635. * with a new int. This is done intentionally so that we can copy out a row into a BitArray very
  1636. * efficiently.</p>
  1637. *
  1638. * <p>The ordering of bits is row-major. Within each int, the least significant bits are used first,
  1639. * meaning they represent lower x values. This is compatible with BitArray's implementation.</p>
  1640. *
  1641. * @author Sean Owen
  1642. * @author dswitkin@google.com (Daniel Switkin)
  1643. */
  1644. class BitMatrix /*implements Cloneable*/ {
  1645. /**
  1646. * Creates an empty square {@link BitMatrix}.
  1647. *
  1648. * @param dimension height and width
  1649. */
  1650. // public constructor(dimension: number /*int*/) {
  1651. // this(dimension, dimension)
  1652. // }
  1653. /**
  1654. * Creates an empty {@link BitMatrix}.
  1655. *
  1656. * @param width bit matrix width
  1657. * @param height bit matrix height
  1658. */
  1659. // public constructor(width: number /*int*/, height: number /*int*/) {
  1660. // if (width < 1 || height < 1) {
  1661. // throw new IllegalArgumentException("Both dimensions must be greater than 0")
  1662. // }
  1663. // this.width = width
  1664. // this.height = height
  1665. // this.rowSize = (width + 31) / 32
  1666. // bits = new int[rowSize * height];
  1667. // }
  1668. constructor(width /*int*/, height /*int*/, rowSize /*int*/, bits) {
  1669. this.width = width;
  1670. this.height = height;
  1671. this.rowSize = rowSize;
  1672. this.bits = bits;
  1673. if (undefined === height || null === height) {
  1674. height = width;
  1675. }
  1676. this.height = height;
  1677. if (width < 1 || height < 1) {
  1678. throw new IllegalArgumentException('Both dimensions must be greater than 0');
  1679. }
  1680. if (undefined === rowSize || null === rowSize) {
  1681. rowSize = Math.floor((width + 31) / 32);
  1682. }
  1683. this.rowSize = rowSize;
  1684. if (undefined === bits || null === bits) {
  1685. this.bits = new Int32Array(this.rowSize * this.height);
  1686. }
  1687. }
  1688. /**
  1689. * Interprets a 2D array of booleans as a {@link BitMatrix}, where "true" means an "on" bit.
  1690. *
  1691. * @function parse
  1692. * @param image bits of the image, as a row-major 2D array. Elements are arrays representing rows
  1693. * @return {@link BitMatrix} representation of image
  1694. */
  1695. static parseFromBooleanArray(image) {
  1696. const height = image.length;
  1697. const width = image[0].length;
  1698. const bits = new BitMatrix(width, height);
  1699. for (let i = 0; i < height; i++) {
  1700. const imageI = image[i];
  1701. for (let j = 0; j < width; j++) {
  1702. if (imageI[j]) {
  1703. bits.set(j, i);
  1704. }
  1705. }
  1706. }
  1707. return bits;
  1708. }
  1709. /**
  1710. *
  1711. * @function parse
  1712. * @param stringRepresentation
  1713. * @param setString
  1714. * @param unsetString
  1715. */
  1716. static parseFromString(stringRepresentation, setString, unsetString) {
  1717. if (stringRepresentation === null) {
  1718. throw new IllegalArgumentException('stringRepresentation cannot be null');
  1719. }
  1720. const bits = new Array(stringRepresentation.length);
  1721. let bitsPos = 0;
  1722. let rowStartPos = 0;
  1723. let rowLength = -1;
  1724. let nRows = 0;
  1725. let pos = 0;
  1726. while (pos < stringRepresentation.length) {
  1727. if (stringRepresentation.charAt(pos) === '\n' ||
  1728. stringRepresentation.charAt(pos) === '\r') {
  1729. if (bitsPos > rowStartPos) {
  1730. if (rowLength === -1) {
  1731. rowLength = bitsPos - rowStartPos;
  1732. }
  1733. else if (bitsPos - rowStartPos !== rowLength) {
  1734. throw new IllegalArgumentException('row lengths do not match');
  1735. }
  1736. rowStartPos = bitsPos;
  1737. nRows++;
  1738. }
  1739. pos++;
  1740. }
  1741. else if (stringRepresentation.substring(pos, pos + setString.length) === setString) {
  1742. pos += setString.length;
  1743. bits[bitsPos] = true;
  1744. bitsPos++;
  1745. }
  1746. else if (stringRepresentation.substring(pos, pos + unsetString.length) === unsetString) {
  1747. pos += unsetString.length;
  1748. bits[bitsPos] = false;
  1749. bitsPos++;
  1750. }
  1751. else {
  1752. throw new IllegalArgumentException('illegal character encountered: ' + stringRepresentation.substring(pos));
  1753. }
  1754. }
  1755. // no EOL at end?
  1756. if (bitsPos > rowStartPos) {
  1757. if (rowLength === -1) {
  1758. rowLength = bitsPos - rowStartPos;
  1759. }
  1760. else if (bitsPos - rowStartPos !== rowLength) {
  1761. throw new IllegalArgumentException('row lengths do not match');
  1762. }
  1763. nRows++;
  1764. }
  1765. const matrix = new BitMatrix(rowLength, nRows);
  1766. for (let i = 0; i < bitsPos; i++) {
  1767. if (bits[i]) {
  1768. matrix.set(Math.floor(i % rowLength), Math.floor(i / rowLength));
  1769. }
  1770. }
  1771. return matrix;
  1772. }
  1773. /**
  1774. * <p>Gets the requested bit, where true means black.</p>
  1775. *
  1776. * @param x The horizontal component (i.e. which column)
  1777. * @param y The vertical component (i.e. which row)
  1778. * @return value of given bit in matrix
  1779. */
  1780. get(x /*int*/, y /*int*/) {
  1781. const offset = y * this.rowSize + Math.floor(x / 32);
  1782. return ((this.bits[offset] >>> (x & 0x1f)) & 1) !== 0;
  1783. }
  1784. /**
  1785. * <p>Sets the given bit to true.</p>
  1786. *
  1787. * @param x The horizontal component (i.e. which column)
  1788. * @param y The vertical component (i.e. which row)
  1789. */
  1790. set(x /*int*/, y /*int*/) {
  1791. const offset = y * this.rowSize + Math.floor(x / 32);
  1792. this.bits[offset] |= (1 << (x & 0x1f)) & 0xFFFFFFFF;
  1793. }
  1794. unset(x /*int*/, y /*int*/) {
  1795. const offset = y * this.rowSize + Math.floor(x / 32);
  1796. this.bits[offset] &= ~((1 << (x & 0x1f)) & 0xFFFFFFFF);
  1797. }
  1798. /**
  1799. * <p>Flips the given bit.</p>
  1800. *
  1801. * @param x The horizontal component (i.e. which column)
  1802. * @param y The vertical component (i.e. which row)
  1803. */
  1804. flip(x /*int*/, y /*int*/) {
  1805. const offset = y * this.rowSize + Math.floor(x / 32);
  1806. this.bits[offset] ^= ((1 << (x & 0x1f)) & 0xFFFFFFFF);
  1807. }
  1808. /**
  1809. * Exclusive-or (XOR): Flip the bit in this {@code BitMatrix} if the corresponding
  1810. * mask bit is set.
  1811. *
  1812. * @param mask XOR mask
  1813. */
  1814. xor(mask) {
  1815. if (this.width !== mask.getWidth() || this.height !== mask.getHeight()
  1816. || this.rowSize !== mask.getRowSize()) {
  1817. throw new IllegalArgumentException('input matrix dimensions do not match');
  1818. }
  1819. const rowArray = new BitArray(Math.floor(this.width / 32) + 1);
  1820. const rowSize = this.rowSize;
  1821. const bits = this.bits;
  1822. for (let y = 0, height = this.height; y < height; y++) {
  1823. const offset = y * rowSize;
  1824. const row = mask.getRow(y, rowArray).getBitArray();
  1825. for (let x = 0; x < rowSize; x++) {
  1826. bits[offset + x] ^= row[x];
  1827. }
  1828. }
  1829. }
  1830. /**
  1831. * Clears all bits (sets to false).
  1832. */
  1833. clear() {
  1834. const bits = this.bits;
  1835. const max = bits.length;
  1836. for (let i = 0; i < max; i++) {
  1837. bits[i] = 0;
  1838. }
  1839. }
  1840. /**
  1841. * <p>Sets a square region of the bit matrix to true.</p>
  1842. *
  1843. * @param left The horizontal position to begin at (inclusive)
  1844. * @param top The vertical position to begin at (inclusive)
  1845. * @param width The width of the region
  1846. * @param height The height of the region
  1847. */
  1848. setRegion(left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  1849. if (top < 0 || left < 0) {
  1850. throw new IllegalArgumentException('Left and top must be nonnegative');
  1851. }
  1852. if (height < 1 || width < 1) {
  1853. throw new IllegalArgumentException('Height and width must be at least 1');
  1854. }
  1855. const right = left + width;
  1856. const bottom = top + height;
  1857. if (bottom > this.height || right > this.width) {
  1858. throw new IllegalArgumentException('The region must fit inside the matrix');
  1859. }
  1860. const rowSize = this.rowSize;
  1861. const bits = this.bits;
  1862. for (let y = top; y < bottom; y++) {
  1863. const offset = y * rowSize;
  1864. for (let x = left; x < right; x++) {
  1865. bits[offset + Math.floor(x / 32)] |= ((1 << (x & 0x1f)) & 0xFFFFFFFF);
  1866. }
  1867. }
  1868. }
  1869. /**
  1870. * A fast method to retrieve one row of data from the matrix as a BitArray.
  1871. *
  1872. * @param y The row to retrieve
  1873. * @param row An optional caller-allocated BitArray, will be allocated if null or too small
  1874. * @return The resulting BitArray - this reference should always be used even when passing
  1875. * your own row
  1876. */
  1877. getRow(y /*int*/, row) {
  1878. if (row === null || row === undefined || row.getSize() < this.width) {
  1879. row = new BitArray(this.width);
  1880. }
  1881. else {
  1882. row.clear();
  1883. }
  1884. const rowSize = this.rowSize;
  1885. const bits = this.bits;
  1886. const offset = y * rowSize;
  1887. for (let x = 0; x < rowSize; x++) {
  1888. row.setBulk(x * 32, bits[offset + x]);
  1889. }
  1890. return row;
  1891. }
  1892. /**
  1893. * @param y row to set
  1894. * @param row {@link BitArray} to copy from
  1895. */
  1896. setRow(y /*int*/, row) {
  1897. System.arraycopy(row.getBitArray(), 0, this.bits, y * this.rowSize, this.rowSize);
  1898. }
  1899. /**
  1900. * Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees
  1901. */
  1902. rotate180() {
  1903. const width = this.getWidth();
  1904. const height = this.getHeight();
  1905. let topRow = new BitArray(width);
  1906. let bottomRow = new BitArray(width);
  1907. for (let i = 0, length = Math.floor((height + 1) / 2); i < length; i++) {
  1908. topRow = this.getRow(i, topRow);
  1909. bottomRow = this.getRow(height - 1 - i, bottomRow);
  1910. topRow.reverse();
  1911. bottomRow.reverse();
  1912. this.setRow(i, bottomRow);
  1913. this.setRow(height - 1 - i, topRow);
  1914. }
  1915. }
  1916. /**
  1917. * This is useful in detecting the enclosing rectangle of a 'pure' barcode.
  1918. *
  1919. * @return {@code left,top,width,height} enclosing rectangle of all 1 bits, or null if it is all white
  1920. */
  1921. getEnclosingRectangle() {
  1922. const width = this.width;
  1923. const height = this.height;
  1924. const rowSize = this.rowSize;
  1925. const bits = this.bits;
  1926. let left = width;
  1927. let top = height;
  1928. let right = -1;
  1929. let bottom = -1;
  1930. for (let y = 0; y < height; y++) {
  1931. for (let x32 = 0; x32 < rowSize; x32++) {
  1932. const theBits = bits[y * rowSize + x32];
  1933. if (theBits !== 0) {
  1934. if (y < top) {
  1935. top = y;
  1936. }
  1937. if (y > bottom) {
  1938. bottom = y;
  1939. }
  1940. if (x32 * 32 < left) {
  1941. let bit = 0;
  1942. while (((theBits << (31 - bit)) & 0xFFFFFFFF) === 0) {
  1943. bit++;
  1944. }
  1945. if ((x32 * 32 + bit) < left) {
  1946. left = x32 * 32 + bit;
  1947. }
  1948. }
  1949. if (x32 * 32 + 31 > right) {
  1950. let bit = 31;
  1951. while ((theBits >>> bit) === 0) {
  1952. bit--;
  1953. }
  1954. if ((x32 * 32 + bit) > right) {
  1955. right = x32 * 32 + bit;
  1956. }
  1957. }
  1958. }
  1959. }
  1960. }
  1961. if (right < left || bottom < top) {
  1962. return null;
  1963. }
  1964. return Int32Array.from([left, top, right - left + 1, bottom - top + 1]);
  1965. }
  1966. /**
  1967. * This is useful in detecting a corner of a 'pure' barcode.
  1968. *
  1969. * @return {@code x,y} coordinate of top-left-most 1 bit, or null if it is all white
  1970. */
  1971. getTopLeftOnBit() {
  1972. const rowSize = this.rowSize;
  1973. const bits = this.bits;
  1974. let bitsOffset = 0;
  1975. while (bitsOffset < bits.length && bits[bitsOffset] === 0) {
  1976. bitsOffset++;
  1977. }
  1978. if (bitsOffset === bits.length) {
  1979. return null;
  1980. }
  1981. const y = bitsOffset / rowSize;
  1982. let x = (bitsOffset % rowSize) * 32;
  1983. const theBits = bits[bitsOffset];
  1984. let bit = 0;
  1985. while (((theBits << (31 - bit)) & 0xFFFFFFFF) === 0) {
  1986. bit++;
  1987. }
  1988. x += bit;
  1989. return Int32Array.from([x, y]);
  1990. }
  1991. getBottomRightOnBit() {
  1992. const rowSize = this.rowSize;
  1993. const bits = this.bits;
  1994. let bitsOffset = bits.length - 1;
  1995. while (bitsOffset >= 0 && bits[bitsOffset] === 0) {
  1996. bitsOffset--;
  1997. }
  1998. if (bitsOffset < 0) {
  1999. return null;
  2000. }
  2001. const y = Math.floor(bitsOffset / rowSize);
  2002. let x = Math.floor(bitsOffset % rowSize) * 32;
  2003. const theBits = bits[bitsOffset];
  2004. let bit = 31;
  2005. while ((theBits >>> bit) === 0) {
  2006. bit--;
  2007. }
  2008. x += bit;
  2009. return Int32Array.from([x, y]);
  2010. }
  2011. /**
  2012. * @return The width of the matrix
  2013. */
  2014. getWidth() {
  2015. return this.width;
  2016. }
  2017. /**
  2018. * @return The height of the matrix
  2019. */
  2020. getHeight() {
  2021. return this.height;
  2022. }
  2023. /**
  2024. * @return The row size of the matrix
  2025. */
  2026. getRowSize() {
  2027. return this.rowSize;
  2028. }
  2029. /*@Override*/
  2030. equals(o) {
  2031. if (!(o instanceof BitMatrix)) {
  2032. return false;
  2033. }
  2034. const other = o;
  2035. return this.width === other.width && this.height === other.height && this.rowSize === other.rowSize &&
  2036. Arrays.equals(this.bits, other.bits);
  2037. }
  2038. /*@Override*/
  2039. hashCode() {
  2040. let hash = this.width;
  2041. hash = 31 * hash + this.width;
  2042. hash = 31 * hash + this.height;
  2043. hash = 31 * hash + this.rowSize;
  2044. hash = 31 * hash + Arrays.hashCode(this.bits);
  2045. return hash;
  2046. }
  2047. /**
  2048. * @return string representation using "X" for set and " " for unset bits
  2049. */
  2050. /*@Override*/
  2051. // public toString(): string {
  2052. // return toString(": "X, " ")
  2053. // }
  2054. /**
  2055. * @param setString representation of a set bit
  2056. * @param unsetString representation of an unset bit
  2057. * @return string representation of entire matrix utilizing given strings
  2058. */
  2059. // public toString(setString: string = "X ", unsetString: string = " "): string {
  2060. // return this.buildToString(setString, unsetString, "\n")
  2061. // }
  2062. /**
  2063. * @param setString representation of a set bit
  2064. * @param unsetString representation of an unset bit
  2065. * @param lineSeparator newline character in string representation
  2066. * @return string representation of entire matrix utilizing given strings and line separator
  2067. * @deprecated call {@link #toString(String,String)} only, which uses \n line separator always
  2068. */
  2069. // @Deprecated
  2070. toString(setString = 'X ', unsetString = ' ', lineSeparator = '\n') {
  2071. return this.buildToString(setString, unsetString, lineSeparator);
  2072. }
  2073. buildToString(setString, unsetString, lineSeparator) {
  2074. let result = new StringBuilder();
  2075. // result.append(lineSeparator);
  2076. for (let y = 0, height = this.height; y < height; y++) {
  2077. for (let x = 0, width = this.width; x < width; x++) {
  2078. result.append(this.get(x, y) ? setString : unsetString);
  2079. }
  2080. result.append(lineSeparator);
  2081. }
  2082. return result.toString();
  2083. }
  2084. /*@Override*/
  2085. clone() {
  2086. return new BitMatrix(this.width, this.height, this.rowSize, this.bits.slice());
  2087. }
  2088. }
  2089. /**
  2090. * Custom Error class of type Exception.
  2091. */
  2092. class NotFoundException extends Exception {
  2093. static getNotFoundInstance() {
  2094. return new NotFoundException();
  2095. }
  2096. }
  2097. NotFoundException.kind = 'NotFoundException';
  2098. /*
  2099. * Copyright 2009 ZXing authors
  2100. *
  2101. * Licensed under the Apache License, Version 2.0 (the "License");
  2102. * you may not use this file except in compliance with the License.
  2103. * You may obtain a copy of the License at
  2104. *
  2105. * http://www.apache.org/licenses/LICENSE-2.0
  2106. *
  2107. * Unless required by applicable law or agreed to in writing, software
  2108. * distributed under the License is distributed on an "AS IS" BASIS,
  2109. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2110. * See the License for the specific language governing permissions and
  2111. * limitations under the License.
  2112. */
  2113. /**
  2114. * This Binarizer implementation uses the old ZXing global histogram approach. It is suitable
  2115. * for low-end mobile devices which don't have enough CPU or memory to use a local thresholding
  2116. * algorithm. However, because it picks a global black point, it cannot handle difficult shadows
  2117. * and gradients.
  2118. *
  2119. * Faster mobile devices and all desktop applications should probably use HybridBinarizer instead.
  2120. *
  2121. * @author dswitkin@google.com (Daniel Switkin)
  2122. * @author Sean Owen
  2123. */
  2124. class GlobalHistogramBinarizer extends Binarizer {
  2125. constructor(source) {
  2126. super(source);
  2127. this.luminances = GlobalHistogramBinarizer.EMPTY;
  2128. this.buckets = new Int32Array(GlobalHistogramBinarizer.LUMINANCE_BUCKETS);
  2129. }
  2130. // Applies simple sharpening to the row data to improve performance of the 1D Readers.
  2131. /*@Override*/
  2132. getBlackRow(y /*int*/, row) {
  2133. const source = this.getLuminanceSource();
  2134. const width = source.getWidth();
  2135. if (row === undefined || row === null || row.getSize() < width) {
  2136. row = new BitArray(width);
  2137. }
  2138. else {
  2139. row.clear();
  2140. }
  2141. this.initArrays(width);
  2142. const localLuminances = source.getRow(y, this.luminances);
  2143. const localBuckets = this.buckets;
  2144. for (let x = 0; x < width; x++) {
  2145. localBuckets[(localLuminances[x] & 0xff) >> GlobalHistogramBinarizer.LUMINANCE_SHIFT]++;
  2146. }
  2147. const blackPoint = GlobalHistogramBinarizer.estimateBlackPoint(localBuckets);
  2148. if (width < 3) {
  2149. // Special case for very small images
  2150. for (let x = 0; x < width; x++) {
  2151. if ((localLuminances[x] & 0xff) < blackPoint) {
  2152. row.set(x);
  2153. }
  2154. }
  2155. }
  2156. else {
  2157. let left = localLuminances[0] & 0xff;
  2158. let center = localLuminances[1] & 0xff;
  2159. for (let x = 1; x < width - 1; x++) {
  2160. const right = localLuminances[x + 1] & 0xff;
  2161. // A simple -1 4 -1 box filter with a weight of 2.
  2162. if (((center * 4) - left - right) / 2 < blackPoint) {
  2163. row.set(x);
  2164. }
  2165. left = center;
  2166. center = right;
  2167. }
  2168. }
  2169. return row;
  2170. }
  2171. // Does not sharpen the data, as this call is intended to only be used by 2D Readers.
  2172. /*@Override*/
  2173. getBlackMatrix() {
  2174. const source = this.getLuminanceSource();
  2175. const width = source.getWidth();
  2176. const height = source.getHeight();
  2177. const matrix = new BitMatrix(width, height);
  2178. // Quickly calculates the histogram by sampling four rows from the image. This proved to be
  2179. // more robust on the blackbox tests than sampling a diagonal as we used to do.
  2180. this.initArrays(width);
  2181. const localBuckets = this.buckets;
  2182. for (let y = 1; y < 5; y++) {
  2183. const row = Math.floor((height * y) / 5);
  2184. const localLuminances = source.getRow(row, this.luminances);
  2185. const right = Math.floor((width * 4) / 5);
  2186. for (let x = Math.floor(width / 5); x < right; x++) {
  2187. const pixel = localLuminances[x] & 0xff;
  2188. localBuckets[pixel >> GlobalHistogramBinarizer.LUMINANCE_SHIFT]++;
  2189. }
  2190. }
  2191. const blackPoint = GlobalHistogramBinarizer.estimateBlackPoint(localBuckets);
  2192. // We delay reading the entire image luminance until the black point estimation succeeds.
  2193. // Although we end up reading four rows twice, it is consistent with our motto of
  2194. // "fail quickly" which is necessary for continuous scanning.
  2195. const localLuminances = source.getMatrix();
  2196. for (let y = 0; y < height; y++) {
  2197. const offset = y * width;
  2198. for (let x = 0; x < width; x++) {
  2199. const pixel = localLuminances[offset + x] & 0xff;
  2200. if (pixel < blackPoint) {
  2201. matrix.set(x, y);
  2202. }
  2203. }
  2204. }
  2205. return matrix;
  2206. }
  2207. /*@Override*/
  2208. createBinarizer(source) {
  2209. return new GlobalHistogramBinarizer(source);
  2210. }
  2211. initArrays(luminanceSize /*int*/) {
  2212. if (this.luminances.length < luminanceSize) {
  2213. this.luminances = new Uint8ClampedArray(luminanceSize);
  2214. }
  2215. const buckets = this.buckets;
  2216. for (let x = 0; x < GlobalHistogramBinarizer.LUMINANCE_BUCKETS; x++) {
  2217. buckets[x] = 0;
  2218. }
  2219. }
  2220. static estimateBlackPoint(buckets) {
  2221. // Find the tallest peak in the histogram.
  2222. const numBuckets = buckets.length;
  2223. let maxBucketCount = 0;
  2224. let firstPeak = 0;
  2225. let firstPeakSize = 0;
  2226. for (let x = 0; x < numBuckets; x++) {
  2227. if (buckets[x] > firstPeakSize) {
  2228. firstPeak = x;
  2229. firstPeakSize = buckets[x];
  2230. }
  2231. if (buckets[x] > maxBucketCount) {
  2232. maxBucketCount = buckets[x];
  2233. }
  2234. }
  2235. // Find the second-tallest peak which is somewhat far from the tallest peak.
  2236. let secondPeak = 0;
  2237. let secondPeakScore = 0;
  2238. for (let x = 0; x < numBuckets; x++) {
  2239. const distanceToBiggest = x - firstPeak;
  2240. // Encourage more distant second peaks by multiplying by square of distance.
  2241. const score = buckets[x] * distanceToBiggest * distanceToBiggest;
  2242. if (score > secondPeakScore) {
  2243. secondPeak = x;
  2244. secondPeakScore = score;
  2245. }
  2246. }
  2247. // Make sure firstPeak corresponds to the black peak.
  2248. if (firstPeak > secondPeak) {
  2249. const temp = firstPeak;
  2250. firstPeak = secondPeak;
  2251. secondPeak = temp;
  2252. }
  2253. // If there is too little contrast in the image to pick a meaningful black point, throw rather
  2254. // than waste time trying to decode the image, and risk false positives.
  2255. if (secondPeak - firstPeak <= numBuckets / 16) {
  2256. throw new NotFoundException();
  2257. }
  2258. // Find a valley between them that is low and closer to the white peak.
  2259. let bestValley = secondPeak - 1;
  2260. let bestValleyScore = -1;
  2261. for (let x = secondPeak - 1; x > firstPeak; x--) {
  2262. const fromFirst = x - firstPeak;
  2263. const score = fromFirst * fromFirst * (secondPeak - x) * (maxBucketCount - buckets[x]);
  2264. if (score > bestValleyScore) {
  2265. bestValley = x;
  2266. bestValleyScore = score;
  2267. }
  2268. }
  2269. return bestValley << GlobalHistogramBinarizer.LUMINANCE_SHIFT;
  2270. }
  2271. }
  2272. GlobalHistogramBinarizer.LUMINANCE_BITS = 5;
  2273. GlobalHistogramBinarizer.LUMINANCE_SHIFT = 8 - GlobalHistogramBinarizer.LUMINANCE_BITS;
  2274. GlobalHistogramBinarizer.LUMINANCE_BUCKETS = 1 << GlobalHistogramBinarizer.LUMINANCE_BITS;
  2275. GlobalHistogramBinarizer.EMPTY = Uint8ClampedArray.from([0]);
  2276. /*
  2277. * Copyright 2009 ZXing authors
  2278. *
  2279. * Licensed under the Apache License, Version 2.0 (the "License");
  2280. * you may not use this file except in compliance with the License.
  2281. * You may obtain a copy of the License at
  2282. *
  2283. * http://www.apache.org/licenses/LICENSE-2.0
  2284. *
  2285. * Unless required by applicable law or agreed to in writing, software
  2286. * distributed under the License is distributed on an "AS IS" BASIS,
  2287. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2288. * See the License for the specific language governing permissions and
  2289. * limitations under the License.
  2290. */
  2291. /**
  2292. * This class implements a local thresholding algorithm, which while slower than the
  2293. * GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for
  2294. * high frequency images of barcodes with black data on white backgrounds. For this application,
  2295. * it does a much better job than a global blackpoint with severe shadows and gradients.
  2296. * However it tends to produce artifacts on lower frequency images and is therefore not
  2297. * a good general purpose binarizer for uses outside ZXing.
  2298. *
  2299. * This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers,
  2300. * and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already
  2301. * inherently local, and only fails for horizontal gradients. We can revisit that problem later,
  2302. * but for now it was not a win to use local blocks for 1D.
  2303. *
  2304. * This Binarizer is the default for the unit tests and the recommended class for library users.
  2305. *
  2306. * @author dswitkin@google.com (Daniel Switkin)
  2307. */
  2308. class HybridBinarizer extends GlobalHistogramBinarizer {
  2309. constructor(source) {
  2310. super(source);
  2311. this.matrix = null;
  2312. }
  2313. /**
  2314. * Calculates the final BitMatrix once for all requests. This could be called once from the
  2315. * constructor instead, but there are some advantages to doing it lazily, such as making
  2316. * profiling easier, and not doing heavy lifting when callers don't expect it.
  2317. */
  2318. /*@Override*/
  2319. getBlackMatrix() {
  2320. if (this.matrix !== null) {
  2321. return this.matrix;
  2322. }
  2323. const source = this.getLuminanceSource();
  2324. const width = source.getWidth();
  2325. const height = source.getHeight();
  2326. if (width >= HybridBinarizer.MINIMUM_DIMENSION && height >= HybridBinarizer.MINIMUM_DIMENSION) {
  2327. const luminances = source.getMatrix();
  2328. let subWidth = width >> HybridBinarizer.BLOCK_SIZE_POWER;
  2329. if ((width & HybridBinarizer.BLOCK_SIZE_MASK) !== 0) {
  2330. subWidth++;
  2331. }
  2332. let subHeight = height >> HybridBinarizer.BLOCK_SIZE_POWER;
  2333. if ((height & HybridBinarizer.BLOCK_SIZE_MASK) !== 0) {
  2334. subHeight++;
  2335. }
  2336. const blackPoints = HybridBinarizer.calculateBlackPoints(luminances, subWidth, subHeight, width, height);
  2337. const newMatrix = new BitMatrix(width, height);
  2338. HybridBinarizer.calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix);
  2339. this.matrix = newMatrix;
  2340. }
  2341. else {
  2342. // If the image is too small, fall back to the global histogram approach.
  2343. this.matrix = super.getBlackMatrix();
  2344. }
  2345. return this.matrix;
  2346. }
  2347. /*@Override*/
  2348. createBinarizer(source) {
  2349. return new HybridBinarizer(source);
  2350. }
  2351. /**
  2352. * For each block in the image, calculate the average black point using a 5x5 grid
  2353. * of the blocks around it. Also handles the corner cases (fractional blocks are computed based
  2354. * on the last pixels in the row/column which are also used in the previous block).
  2355. */
  2356. static calculateThresholdForBlock(luminances, subWidth /*int*/, subHeight /*int*/, width /*int*/, height /*int*/, blackPoints, matrix) {
  2357. const maxYOffset = height - HybridBinarizer.BLOCK_SIZE;
  2358. const maxXOffset = width - HybridBinarizer.BLOCK_SIZE;
  2359. for (let y = 0; y < subHeight; y++) {
  2360. let yoffset = y << HybridBinarizer.BLOCK_SIZE_POWER;
  2361. if (yoffset > maxYOffset) {
  2362. yoffset = maxYOffset;
  2363. }
  2364. const top = HybridBinarizer.cap(y, 2, subHeight - 3);
  2365. for (let x = 0; x < subWidth; x++) {
  2366. let xoffset = x << HybridBinarizer.BLOCK_SIZE_POWER;
  2367. if (xoffset > maxXOffset) {
  2368. xoffset = maxXOffset;
  2369. }
  2370. const left = HybridBinarizer.cap(x, 2, subWidth - 3);
  2371. let sum = 0;
  2372. for (let z = -2; z <= 2; z++) {
  2373. const blackRow = blackPoints[top + z];
  2374. sum += blackRow[left - 2] + blackRow[left - 1] + blackRow[left] + blackRow[left + 1] + blackRow[left + 2];
  2375. }
  2376. const average = sum / 25;
  2377. HybridBinarizer.thresholdBlock(luminances, xoffset, yoffset, average, width, matrix);
  2378. }
  2379. }
  2380. }
  2381. static cap(value /*int*/, min /*int*/, max /*int*/) {
  2382. return value < min ? min : value > max ? max : value;
  2383. }
  2384. /**
  2385. * Applies a single threshold to a block of pixels.
  2386. */
  2387. static thresholdBlock(luminances, xoffset /*int*/, yoffset /*int*/, threshold /*int*/, stride /*int*/, matrix) {
  2388. for (let y = 0, offset = yoffset * stride + xoffset; y < HybridBinarizer.BLOCK_SIZE; y++, offset += stride) {
  2389. for (let x = 0; x < HybridBinarizer.BLOCK_SIZE; x++) {
  2390. // Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0.
  2391. if ((luminances[offset + x] & 0xFF) <= threshold) {
  2392. matrix.set(xoffset + x, yoffset + y);
  2393. }
  2394. }
  2395. }
  2396. }
  2397. /**
  2398. * Calculates a single black point for each block of pixels and saves it away.
  2399. * See the following thread for a discussion of this algorithm:
  2400. * http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
  2401. */
  2402. static calculateBlackPoints(luminances, subWidth /*int*/, subHeight /*int*/, width /*int*/, height /*int*/) {
  2403. const maxYOffset = height - HybridBinarizer.BLOCK_SIZE;
  2404. const maxXOffset = width - HybridBinarizer.BLOCK_SIZE;
  2405. // tslint:disable-next-line:whitespace
  2406. const blackPoints = new Array(subHeight); // subWidth
  2407. for (let y = 0; y < subHeight; y++) {
  2408. blackPoints[y] = new Int32Array(subWidth);
  2409. let yoffset = y << HybridBinarizer.BLOCK_SIZE_POWER;
  2410. if (yoffset > maxYOffset) {
  2411. yoffset = maxYOffset;
  2412. }
  2413. for (let x = 0; x < subWidth; x++) {
  2414. let xoffset = x << HybridBinarizer.BLOCK_SIZE_POWER;
  2415. if (xoffset > maxXOffset) {
  2416. xoffset = maxXOffset;
  2417. }
  2418. let sum = 0;
  2419. let min = 0xFF;
  2420. let max = 0;
  2421. for (let yy = 0, offset = yoffset * width + xoffset; yy < HybridBinarizer.BLOCK_SIZE; yy++, offset += width) {
  2422. for (let xx = 0; xx < HybridBinarizer.BLOCK_SIZE; xx++) {
  2423. const pixel = luminances[offset + xx] & 0xFF;
  2424. sum += pixel;
  2425. // still looking for good contrast
  2426. if (pixel < min) {
  2427. min = pixel;
  2428. }
  2429. if (pixel > max) {
  2430. max = pixel;
  2431. }
  2432. }
  2433. // short-circuit min/max tests once dynamic range is met
  2434. if (max - min > HybridBinarizer.MIN_DYNAMIC_RANGE) {
  2435. // finish the rest of the rows quickly
  2436. for (yy++, offset += width; yy < HybridBinarizer.BLOCK_SIZE; yy++, offset += width) {
  2437. for (let xx = 0; xx < HybridBinarizer.BLOCK_SIZE; xx++) {
  2438. sum += luminances[offset + xx] & 0xFF;
  2439. }
  2440. }
  2441. }
  2442. }
  2443. // The default estimate is the average of the values in the block.
  2444. let average = sum >> (HybridBinarizer.BLOCK_SIZE_POWER * 2);
  2445. if (max - min <= HybridBinarizer.MIN_DYNAMIC_RANGE) {
  2446. // If variation within the block is low, assume this is a block with only light or only
  2447. // dark pixels. In that case we do not want to use the average, as it would divide this
  2448. // low contrast area into black and white pixels, essentially creating data out of noise.
  2449. //
  2450. // The default assumption is that the block is light/background. Since no estimate for
  2451. // the level of dark pixels exists locally, use half the min for the block.
  2452. average = min / 2;
  2453. if (y > 0 && x > 0) {
  2454. // Correct the "white background" assumption for blocks that have neighbors by comparing
  2455. // the pixels in this block to the previously calculated black points. This is based on
  2456. // the fact that dark barcode symbology is always surrounded by some amount of light
  2457. // background for which reasonable black point estimates were made. The bp estimated at
  2458. // the boundaries is used for the interior.
  2459. // The (min < bp) is arbitrary but works better than other heuristics that were tried.
  2460. const averageNeighborBlackPoint = (blackPoints[y - 1][x] + (2 * blackPoints[y][x - 1]) + blackPoints[y - 1][x - 1]) / 4;
  2461. if (min < averageNeighborBlackPoint) {
  2462. average = averageNeighborBlackPoint;
  2463. }
  2464. }
  2465. }
  2466. blackPoints[y][x] = average;
  2467. }
  2468. }
  2469. return blackPoints;
  2470. }
  2471. }
  2472. // This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.
  2473. // So this is the smallest dimension in each axis we can accept.
  2474. HybridBinarizer.BLOCK_SIZE_POWER = 3;
  2475. HybridBinarizer.BLOCK_SIZE = 1 << HybridBinarizer.BLOCK_SIZE_POWER; // ...0100...00
  2476. HybridBinarizer.BLOCK_SIZE_MASK = HybridBinarizer.BLOCK_SIZE - 1; // ...0011...11
  2477. HybridBinarizer.MINIMUM_DIMENSION = HybridBinarizer.BLOCK_SIZE * 5;
  2478. HybridBinarizer.MIN_DYNAMIC_RANGE = 24;
  2479. /*
  2480. * Copyright 2009 ZXing authors
  2481. *
  2482. * Licensed under the Apache License, Version 2.0 (the "License");
  2483. * you may not use this file except in compliance with the License.
  2484. * You may obtain a copy of the License at
  2485. *
  2486. * http://www.apache.org/licenses/LICENSE-2.0
  2487. *
  2488. * Unless required by applicable law or agreed to in writing, software
  2489. * distributed under the License is distributed on an "AS IS" BASIS,
  2490. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2491. * See the License for the specific language governing permissions and
  2492. * limitations under the License.
  2493. */
  2494. /*namespace com.google.zxing {*/
  2495. /**
  2496. * The purpose of this class hierarchy is to abstract different bitmap implementations across
  2497. * platforms into a standard interface for requesting greyscale luminance values. The interface
  2498. * only provides immutable methods; therefore crop and rotation create copies. This is to ensure
  2499. * that one Reader does not modify the original luminance source and leave it in an unknown state
  2500. * for other Readers in the chain.
  2501. *
  2502. * @author dswitkin@google.com (Daniel Switkin)
  2503. */
  2504. class LuminanceSource {
  2505. constructor(width /*int*/, height /*int*/) {
  2506. this.width = width;
  2507. this.height = height;
  2508. }
  2509. /**
  2510. * @return The width of the bitmap.
  2511. */
  2512. getWidth() {
  2513. return this.width;
  2514. }
  2515. /**
  2516. * @return The height of the bitmap.
  2517. */
  2518. getHeight() {
  2519. return this.height;
  2520. }
  2521. /**
  2522. * @return Whether this subclass supports cropping.
  2523. */
  2524. isCropSupported() {
  2525. return false;
  2526. }
  2527. /**
  2528. * Returns a new object with cropped image data. Implementations may keep a reference to the
  2529. * original data rather than a copy. Only callable if isCropSupported() is true.
  2530. *
  2531. * @param left The left coordinate, which must be in [0,getWidth())
  2532. * @param top The top coordinate, which must be in [0,getHeight())
  2533. * @param width The width of the rectangle to crop.
  2534. * @param height The height of the rectangle to crop.
  2535. * @return A cropped version of this object.
  2536. */
  2537. crop(left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  2538. throw new UnsupportedOperationException('This luminance source does not support cropping.');
  2539. }
  2540. /**
  2541. * @return Whether this subclass supports counter-clockwise rotation.
  2542. */
  2543. isRotateSupported() {
  2544. return false;
  2545. }
  2546. /**
  2547. * Returns a new object with rotated image data by 90 degrees counterclockwise.
  2548. * Only callable if {@link #isRotateSupported()} is true.
  2549. *
  2550. * @return A rotated version of this object.
  2551. */
  2552. rotateCounterClockwise() {
  2553. throw new UnsupportedOperationException('This luminance source does not support rotation by 90 degrees.');
  2554. }
  2555. /**
  2556. * Returns a new object with rotated image data by 45 degrees counterclockwise.
  2557. * Only callable if {@link #isRotateSupported()} is true.
  2558. *
  2559. * @return A rotated version of this object.
  2560. */
  2561. rotateCounterClockwise45() {
  2562. throw new UnsupportedOperationException('This luminance source does not support rotation by 45 degrees.');
  2563. }
  2564. /*@Override*/
  2565. toString() {
  2566. const row = new Uint8ClampedArray(this.width);
  2567. let result = new StringBuilder();
  2568. for (let y = 0; y < this.height; y++) {
  2569. const sourceRow = this.getRow(y, row);
  2570. for (let x = 0; x < this.width; x++) {
  2571. const luminance = sourceRow[x] & 0xFF;
  2572. let c;
  2573. if (luminance < 0x40) {
  2574. c = '#';
  2575. }
  2576. else if (luminance < 0x80) {
  2577. c = '+';
  2578. }
  2579. else if (luminance < 0xC0) {
  2580. c = '.';
  2581. }
  2582. else {
  2583. c = ' ';
  2584. }
  2585. result.append(c);
  2586. }
  2587. result.append('\n');
  2588. }
  2589. return result.toString();
  2590. }
  2591. }
  2592. /*
  2593. * Copyright 2009 ZXing authors
  2594. *
  2595. * Licensed under the Apache License, Version 2.0 (the "License");
  2596. * you may not use this file except in compliance with the License.
  2597. * You may obtain a copy of the License at
  2598. *
  2599. * http://www.apache.org/licenses/LICENSE-2.0
  2600. *
  2601. * Unless required by applicable law or agreed to in writing, software
  2602. * distributed under the License is distributed on an "AS IS" BASIS,
  2603. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2604. * See the License for the specific language governing permissions and
  2605. * limitations under the License.
  2606. */
  2607. /*namespace com.google.zxing {*/
  2608. /**
  2609. * A wrapper implementation of {@link LuminanceSource} which inverts the luminances it returns -- black becomes
  2610. * white and vice versa, and each value becomes (255-value).
  2611. *
  2612. * @author Sean Owen
  2613. */
  2614. class InvertedLuminanceSource extends LuminanceSource {
  2615. constructor(delegate) {
  2616. super(delegate.getWidth(), delegate.getHeight());
  2617. this.delegate = delegate;
  2618. }
  2619. /*@Override*/
  2620. getRow(y /*int*/, row) {
  2621. const sourceRow = this.delegate.getRow(y, row);
  2622. const width = this.getWidth();
  2623. for (let i = 0; i < width; i++) {
  2624. sourceRow[i] = /*(byte)*/ (255 - (sourceRow[i] & 0xFF));
  2625. }
  2626. return sourceRow;
  2627. }
  2628. /*@Override*/
  2629. getMatrix() {
  2630. const matrix = this.delegate.getMatrix();
  2631. const length = this.getWidth() * this.getHeight();
  2632. const invertedMatrix = new Uint8ClampedArray(length);
  2633. for (let i = 0; i < length; i++) {
  2634. invertedMatrix[i] = /*(byte)*/ (255 - (matrix[i] & 0xFF));
  2635. }
  2636. return invertedMatrix;
  2637. }
  2638. /*@Override*/
  2639. isCropSupported() {
  2640. return this.delegate.isCropSupported();
  2641. }
  2642. /*@Override*/
  2643. crop(left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  2644. return new InvertedLuminanceSource(this.delegate.crop(left, top, width, height));
  2645. }
  2646. /*@Override*/
  2647. isRotateSupported() {
  2648. return this.delegate.isRotateSupported();
  2649. }
  2650. /**
  2651. * @return original delegate {@link LuminanceSource} since invert undoes itself
  2652. */
  2653. /*@Override*/
  2654. invert() {
  2655. return this.delegate;
  2656. }
  2657. /*@Override*/
  2658. rotateCounterClockwise() {
  2659. return new InvertedLuminanceSource(this.delegate.rotateCounterClockwise());
  2660. }
  2661. /*@Override*/
  2662. rotateCounterClockwise45() {
  2663. return new InvertedLuminanceSource(this.delegate.rotateCounterClockwise45());
  2664. }
  2665. }
  2666. /**
  2667. * @deprecated Moving to @zxing/browser
  2668. */
  2669. class HTMLCanvasElementLuminanceSource extends LuminanceSource {
  2670. constructor(canvas, doAutoInvert = false) {
  2671. super(canvas.width, canvas.height);
  2672. this.canvas = canvas;
  2673. this.tempCanvasElement = null;
  2674. this.buffer = HTMLCanvasElementLuminanceSource.makeBufferFromCanvasImageData(canvas, doAutoInvert);
  2675. }
  2676. static makeBufferFromCanvasImageData(canvas, doAutoInvert = false) {
  2677. const imageData = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height);
  2678. return HTMLCanvasElementLuminanceSource.toGrayscaleBuffer(imageData.data, canvas.width, canvas.height, doAutoInvert);
  2679. }
  2680. static toGrayscaleBuffer(imageBuffer, width, height, doAutoInvert = false) {
  2681. const grayscaleBuffer = new Uint8ClampedArray(width * height);
  2682. HTMLCanvasElementLuminanceSource.FRAME_INDEX = !HTMLCanvasElementLuminanceSource.FRAME_INDEX;
  2683. if (HTMLCanvasElementLuminanceSource.FRAME_INDEX || !doAutoInvert) {
  2684. for (let i = 0, j = 0, length = imageBuffer.length; i < length; i += 4, j++) {
  2685. let gray;
  2686. const alpha = imageBuffer[i + 3];
  2687. // The color of fully-transparent pixels is irrelevant. They are often, technically, fully-transparent
  2688. // black (0 alpha, and then 0 RGB). They are often used, of course as the "white" area in a
  2689. // barcode image. Force any such pixel to be white:
  2690. if (alpha === 0) {
  2691. gray = 0xFF;
  2692. }
  2693. else {
  2694. const pixelR = imageBuffer[i];
  2695. const pixelG = imageBuffer[i + 1];
  2696. const pixelB = imageBuffer[i + 2];
  2697. // .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
  2698. // (306*R) >> 10 is approximately equal to R*0.299, and so on.
  2699. // 0x200 >> 10 is 0.5, it implements rounding.
  2700. gray = (306 * pixelR +
  2701. 601 * pixelG +
  2702. 117 * pixelB +
  2703. 0x200) >> 10;
  2704. }
  2705. grayscaleBuffer[j] = gray;
  2706. }
  2707. }
  2708. else {
  2709. for (let i = 0, j = 0, length = imageBuffer.length; i < length; i += 4, j++) {
  2710. let gray;
  2711. const alpha = imageBuffer[i + 3];
  2712. // The color of fully-transparent pixels is irrelevant. They are often, technically, fully-transparent
  2713. // black (0 alpha, and then 0 RGB). They are often used, of course as the "white" area in a
  2714. // barcode image. Force any such pixel to be white:
  2715. if (alpha === 0) {
  2716. gray = 0xFF;
  2717. }
  2718. else {
  2719. const pixelR = imageBuffer[i];
  2720. const pixelG = imageBuffer[i + 1];
  2721. const pixelB = imageBuffer[i + 2];
  2722. // .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
  2723. // (306*R) >> 10 is approximately equal to R*0.299, and so on.
  2724. // 0x200 >> 10 is 0.5, it implements rounding.
  2725. gray = (306 * pixelR +
  2726. 601 * pixelG +
  2727. 117 * pixelB +
  2728. 0x200) >> 10;
  2729. }
  2730. grayscaleBuffer[j] = 0xFF - gray;
  2731. }
  2732. }
  2733. return grayscaleBuffer;
  2734. }
  2735. getRow(y /*int*/, row) {
  2736. if (y < 0 || y >= this.getHeight()) {
  2737. throw new IllegalArgumentException('Requested row is outside the image: ' + y);
  2738. }
  2739. const width = this.getWidth();
  2740. const start = y * width;
  2741. if (row === null) {
  2742. row = this.buffer.slice(start, start + width);
  2743. }
  2744. else {
  2745. if (row.length < width) {
  2746. row = new Uint8ClampedArray(width);
  2747. }
  2748. // The underlying raster of image consists of bytes with the luminance values
  2749. // TODO: can avoid set/slice?
  2750. row.set(this.buffer.slice(start, start + width));
  2751. }
  2752. return row;
  2753. }
  2754. getMatrix() {
  2755. return this.buffer;
  2756. }
  2757. isCropSupported() {
  2758. return true;
  2759. }
  2760. crop(left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  2761. super.crop(left, top, width, height);
  2762. return this;
  2763. }
  2764. /**
  2765. * This is always true, since the image is a gray-scale image.
  2766. *
  2767. * @return true
  2768. */
  2769. isRotateSupported() {
  2770. return true;
  2771. }
  2772. rotateCounterClockwise() {
  2773. this.rotate(-90);
  2774. return this;
  2775. }
  2776. rotateCounterClockwise45() {
  2777. this.rotate(-45);
  2778. return this;
  2779. }
  2780. getTempCanvasElement() {
  2781. if (null === this.tempCanvasElement) {
  2782. const tempCanvasElement = this.canvas.ownerDocument.createElement('canvas');
  2783. tempCanvasElement.width = this.canvas.width;
  2784. tempCanvasElement.height = this.canvas.height;
  2785. this.tempCanvasElement = tempCanvasElement;
  2786. }
  2787. return this.tempCanvasElement;
  2788. }
  2789. rotate(angle) {
  2790. const tempCanvasElement = this.getTempCanvasElement();
  2791. const tempContext = tempCanvasElement.getContext('2d');
  2792. const angleRadians = angle * HTMLCanvasElementLuminanceSource.DEGREE_TO_RADIANS;
  2793. // Calculate and set new dimensions for temp canvas
  2794. const width = this.canvas.width;
  2795. const height = this.canvas.height;
  2796. const newWidth = Math.ceil(Math.abs(Math.cos(angleRadians)) * width + Math.abs(Math.sin(angleRadians)) * height);
  2797. const newHeight = Math.ceil(Math.abs(Math.sin(angleRadians)) * width + Math.abs(Math.cos(angleRadians)) * height);
  2798. tempCanvasElement.width = newWidth;
  2799. tempCanvasElement.height = newHeight;
  2800. // Draw at center of temp canvas to prevent clipping of image data
  2801. tempContext.translate(newWidth / 2, newHeight / 2);
  2802. tempContext.rotate(angleRadians);
  2803. tempContext.drawImage(this.canvas, width / -2, height / -2);
  2804. this.buffer = HTMLCanvasElementLuminanceSource.makeBufferFromCanvasImageData(tempCanvasElement);
  2805. return this;
  2806. }
  2807. invert() {
  2808. return new InvertedLuminanceSource(this);
  2809. }
  2810. }
  2811. HTMLCanvasElementLuminanceSource.DEGREE_TO_RADIANS = Math.PI / 180;
  2812. HTMLCanvasElementLuminanceSource.FRAME_INDEX = true;
  2813. /**
  2814. * @deprecated Moving to @zxing/browser
  2815. *
  2816. * Video input device metadata containing the id and label of the device if available.
  2817. */
  2818. class VideoInputDevice {
  2819. /**
  2820. * Creates an instance of VideoInputDevice.
  2821. *
  2822. * @param {string} deviceId the video input device id
  2823. * @param {string} label the label of the device if available
  2824. */
  2825. constructor(deviceId, label, groupId) {
  2826. this.deviceId = deviceId;
  2827. this.label = label;
  2828. /** @inheritdoc */
  2829. this.kind = 'videoinput';
  2830. this.groupId = groupId || undefined;
  2831. }
  2832. /** @inheritdoc */
  2833. toJSON() {
  2834. return {
  2835. kind: this.kind,
  2836. groupId: this.groupId,
  2837. deviceId: this.deviceId,
  2838. label: this.label,
  2839. };
  2840. }
  2841. }
  2842. var __awaiter = ((globalThis || global || self || window || undefined) && (globalThis || global || self || window || undefined).__awaiter) || function (thisArg, _arguments, P, generator) {
  2843. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  2844. return new (P || (P = Promise))(function (resolve, reject) {
  2845. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  2846. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  2847. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  2848. step((generator = generator.apply(thisArg, _arguments || [])).next());
  2849. });
  2850. };
  2851. /**
  2852. * @deprecated Moving to @zxing/browser
  2853. *
  2854. * Base class for browser code reader.
  2855. */
  2856. class BrowserCodeReader {
  2857. /**
  2858. * Creates an instance of BrowserCodeReader.
  2859. * @param {Reader} reader The reader instance to decode the barcode
  2860. * @param {number} [timeBetweenScansMillis=500] the time delay between subsequent successful decode tries
  2861. *
  2862. * @memberOf BrowserCodeReader
  2863. */
  2864. constructor(reader, timeBetweenScansMillis = 500, _hints) {
  2865. this.reader = reader;
  2866. this.timeBetweenScansMillis = timeBetweenScansMillis;
  2867. this._hints = _hints;
  2868. /**
  2869. * This will break the loop.
  2870. */
  2871. this._stopContinuousDecode = false;
  2872. /**
  2873. * This will break the loop.
  2874. */
  2875. this._stopAsyncDecode = false;
  2876. /**
  2877. * Delay time between decode attempts made by the scanner.
  2878. */
  2879. this._timeBetweenDecodingAttempts = 0;
  2880. }
  2881. /**
  2882. * If navigator is present.
  2883. */
  2884. get hasNavigator() {
  2885. return typeof navigator !== 'undefined';
  2886. }
  2887. /**
  2888. * If mediaDevices under navigator is supported.
  2889. */
  2890. get isMediaDevicesSuported() {
  2891. return this.hasNavigator && !!navigator.mediaDevices;
  2892. }
  2893. /**
  2894. * If enumerateDevices under navigator is supported.
  2895. */
  2896. get canEnumerateDevices() {
  2897. return !!(this.isMediaDevicesSuported && navigator.mediaDevices.enumerateDevices);
  2898. }
  2899. /** Time between two decoding tries in milli seconds. */
  2900. get timeBetweenDecodingAttempts() {
  2901. return this._timeBetweenDecodingAttempts;
  2902. }
  2903. /**
  2904. * Change the time span the decoder waits between two decoding tries.
  2905. *
  2906. * @param {number} millis Time between two decoding tries in milli seconds.
  2907. */
  2908. set timeBetweenDecodingAttempts(millis) {
  2909. this._timeBetweenDecodingAttempts = millis < 0 ? 0 : millis;
  2910. }
  2911. /**
  2912. * Sets the hints.
  2913. */
  2914. set hints(hints) {
  2915. this._hints = hints || null;
  2916. }
  2917. /**
  2918. * Sets the hints.
  2919. */
  2920. get hints() {
  2921. return this._hints;
  2922. }
  2923. /**
  2924. * Lists all the available video input devices.
  2925. */
  2926. listVideoInputDevices() {
  2927. return __awaiter(this, void 0, void 0, function* () {
  2928. if (!this.hasNavigator) {
  2929. throw new Error("Can't enumerate devices, navigator is not present.");
  2930. }
  2931. if (!this.canEnumerateDevices) {
  2932. throw new Error("Can't enumerate devices, method not supported.");
  2933. }
  2934. const devices = yield navigator.mediaDevices.enumerateDevices();
  2935. const videoDevices = [];
  2936. for (const device of devices) {
  2937. const kind = device.kind === 'video' ? 'videoinput' : device.kind;
  2938. if (kind !== 'videoinput') {
  2939. continue;
  2940. }
  2941. const deviceId = device.deviceId || device.id;
  2942. const label = device.label || `Video device ${videoDevices.length + 1}`;
  2943. const groupId = device.groupId;
  2944. const videoDevice = { deviceId, label, kind, groupId };
  2945. videoDevices.push(videoDevice);
  2946. }
  2947. return videoDevices;
  2948. });
  2949. }
  2950. /**
  2951. * Obtain the list of available devices with type 'videoinput'.
  2952. *
  2953. * @returns {Promise<VideoInputDevice[]>} an array of available video input devices
  2954. *
  2955. * @memberOf BrowserCodeReader
  2956. *
  2957. * @deprecated Use `listVideoInputDevices` instead.
  2958. */
  2959. getVideoInputDevices() {
  2960. return __awaiter(this, void 0, void 0, function* () {
  2961. const devices = yield this.listVideoInputDevices();
  2962. return devices.map(d => new VideoInputDevice(d.deviceId, d.label));
  2963. });
  2964. }
  2965. /**
  2966. * Let's you find a device using it's Id.
  2967. */
  2968. findDeviceById(deviceId) {
  2969. return __awaiter(this, void 0, void 0, function* () {
  2970. const devices = yield this.listVideoInputDevices();
  2971. if (!devices) {
  2972. return null;
  2973. }
  2974. return devices.find(x => x.deviceId === deviceId);
  2975. });
  2976. }
  2977. /**
  2978. * Decodes the barcode from the device specified by deviceId while showing the video in the specified video element.
  2979. *
  2980. * @param deviceId the id of one of the devices obtained after calling getVideoInputDevices. Can be undefined, in this case it will decode from one of the available devices, preffering the main camera (environment facing) if available.
  2981. * @param video the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  2982. * @returns The decoding result.
  2983. *
  2984. * @memberOf BrowserCodeReader
  2985. *
  2986. * @deprecated Use `decodeOnceFromVideoDevice` instead.
  2987. */
  2988. decodeFromInputVideoDevice(deviceId, videoSource) {
  2989. return __awaiter(this, void 0, void 0, function* () {
  2990. return yield this.decodeOnceFromVideoDevice(deviceId, videoSource);
  2991. });
  2992. }
  2993. /**
  2994. * In one attempt, tries to decode the barcode from the device specified by deviceId while showing the video in the specified video element.
  2995. *
  2996. * @param deviceId the id of one of the devices obtained after calling getVideoInputDevices. Can be undefined, in this case it will decode from one of the available devices, preffering the main camera (environment facing) if available.
  2997. * @param video the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  2998. * @returns The decoding result.
  2999. *
  3000. * @memberOf BrowserCodeReader
  3001. */
  3002. decodeOnceFromVideoDevice(deviceId, videoSource) {
  3003. return __awaiter(this, void 0, void 0, function* () {
  3004. this.reset();
  3005. let videoConstraints;
  3006. if (!deviceId) {
  3007. videoConstraints = { facingMode: 'environment' };
  3008. }
  3009. else {
  3010. videoConstraints = { deviceId: { exact: deviceId } };
  3011. }
  3012. const constraints = { video: videoConstraints };
  3013. return yield this.decodeOnceFromConstraints(constraints, videoSource);
  3014. });
  3015. }
  3016. /**
  3017. * In one attempt, tries to decode the barcode from a stream obtained from the given constraints while showing the video in the specified video element.
  3018. *
  3019. * @param constraints the media stream constraints to get s valid media stream to decode from
  3020. * @param video the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  3021. * @returns The decoding result.
  3022. *
  3023. * @memberOf BrowserCodeReader
  3024. */
  3025. decodeOnceFromConstraints(constraints, videoSource) {
  3026. return __awaiter(this, void 0, void 0, function* () {
  3027. const stream = yield navigator.mediaDevices.getUserMedia(constraints);
  3028. return yield this.decodeOnceFromStream(stream, videoSource);
  3029. });
  3030. }
  3031. /**
  3032. * In one attempt, tries to decode the barcode from a stream obtained from the given constraints while showing the video in the specified video element.
  3033. *
  3034. * @param {MediaStream} [constraints] the media stream constraints to get s valid media stream to decode from
  3035. * @param {string|HTMLVideoElement} [video] the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  3036. * @returns {Promise<Result>} The decoding result.
  3037. *
  3038. * @memberOf BrowserCodeReader
  3039. */
  3040. decodeOnceFromStream(stream, videoSource) {
  3041. return __awaiter(this, void 0, void 0, function* () {
  3042. this.reset();
  3043. const video = yield this.attachStreamToVideo(stream, videoSource);
  3044. const result = yield this.decodeOnce(video);
  3045. return result;
  3046. });
  3047. }
  3048. /**
  3049. * Continuously decodes the barcode from the device specified by device while showing the video in the specified video element.
  3050. *
  3051. * @param {string|null} [deviceId] the id of one of the devices obtained after calling getVideoInputDevices. Can be undefined, in this case it will decode from one of the available devices, preffering the main camera (environment facing) if available.
  3052. * @param {string|HTMLVideoElement|null} [video] the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  3053. * @returns {Promise<void>}
  3054. *
  3055. * @memberOf BrowserCodeReader
  3056. *
  3057. * @deprecated Use `decodeFromVideoDevice` instead.
  3058. */
  3059. decodeFromInputVideoDeviceContinuously(deviceId, videoSource, callbackFn) {
  3060. return __awaiter(this, void 0, void 0, function* () {
  3061. return yield this.decodeFromVideoDevice(deviceId, videoSource, callbackFn);
  3062. });
  3063. }
  3064. /**
  3065. * Continuously tries to decode the barcode from the device specified by device while showing the video in the specified video element.
  3066. *
  3067. * @param {string|null} [deviceId] the id of one of the devices obtained after calling getVideoInputDevices. Can be undefined, in this case it will decode from one of the available devices, preffering the main camera (environment facing) if available.
  3068. * @param {string|HTMLVideoElement|null} [video] the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  3069. * @returns {Promise<void>}
  3070. *
  3071. * @memberOf BrowserCodeReader
  3072. */
  3073. decodeFromVideoDevice(deviceId, videoSource, callbackFn) {
  3074. return __awaiter(this, void 0, void 0, function* () {
  3075. let videoConstraints;
  3076. if (!deviceId) {
  3077. videoConstraints = { facingMode: 'environment' };
  3078. }
  3079. else {
  3080. videoConstraints = { deviceId: { exact: deviceId } };
  3081. }
  3082. const constraints = { video: videoConstraints };
  3083. return yield this.decodeFromConstraints(constraints, videoSource, callbackFn);
  3084. });
  3085. }
  3086. /**
  3087. * Continuously tries to decode the barcode from a stream obtained from the given constraints while showing the video in the specified video element.
  3088. *
  3089. * @param {MediaStream} [constraints] the media stream constraints to get s valid media stream to decode from
  3090. * @param {string|HTMLVideoElement} [video] the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  3091. * @returns {Promise<Result>} The decoding result.
  3092. *
  3093. * @memberOf BrowserCodeReader
  3094. */
  3095. decodeFromConstraints(constraints, videoSource, callbackFn) {
  3096. return __awaiter(this, void 0, void 0, function* () {
  3097. const stream = yield navigator.mediaDevices.getUserMedia(constraints);
  3098. return yield this.decodeFromStream(stream, videoSource, callbackFn);
  3099. });
  3100. }
  3101. /**
  3102. * In one attempt, tries to decode the barcode from a stream obtained from the given constraints while showing the video in the specified video element.
  3103. *
  3104. * @param {MediaStream} [constraints] the media stream constraints to get s valid media stream to decode from
  3105. * @param {string|HTMLVideoElement} [video] the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
  3106. * @returns {Promise<Result>} The decoding result.
  3107. *
  3108. * @memberOf BrowserCodeReader
  3109. */
  3110. decodeFromStream(stream, videoSource, callbackFn) {
  3111. return __awaiter(this, void 0, void 0, function* () {
  3112. this.reset();
  3113. const video = yield this.attachStreamToVideo(stream, videoSource);
  3114. return yield this.decodeContinuously(video, callbackFn);
  3115. });
  3116. }
  3117. /**
  3118. * Breaks the decoding loop.
  3119. */
  3120. stopAsyncDecode() {
  3121. this._stopAsyncDecode = true;
  3122. }
  3123. /**
  3124. * Breaks the decoding loop.
  3125. */
  3126. stopContinuousDecode() {
  3127. this._stopContinuousDecode = true;
  3128. }
  3129. /**
  3130. * Sets the new stream and request a new decoding-with-delay.
  3131. *
  3132. * @param stream The stream to be shown in the video element.
  3133. * @param decodeFn A callback for the decode method.
  3134. */
  3135. attachStreamToVideo(stream, videoSource) {
  3136. return __awaiter(this, void 0, void 0, function* () {
  3137. const videoElement = this.prepareVideoElement(videoSource);
  3138. this.addVideoSource(videoElement, stream);
  3139. this.videoElement = videoElement;
  3140. this.stream = stream;
  3141. yield this.playVideoOnLoadAsync(videoElement);
  3142. return videoElement;
  3143. });
  3144. }
  3145. /**
  3146. *
  3147. * @param videoElement
  3148. */
  3149. playVideoOnLoadAsync(videoElement) {
  3150. return new Promise((resolve, reject) => this.playVideoOnLoad(videoElement, () => resolve()));
  3151. }
  3152. /**
  3153. * Binds listeners and callbacks to the videoElement.
  3154. *
  3155. * @param element
  3156. * @param callbackFn
  3157. */
  3158. playVideoOnLoad(element, callbackFn) {
  3159. this.videoEndedListener = () => this.stopStreams();
  3160. this.videoCanPlayListener = () => this.tryPlayVideo(element);
  3161. element.addEventListener('ended', this.videoEndedListener);
  3162. element.addEventListener('canplay', this.videoCanPlayListener);
  3163. element.addEventListener('playing', callbackFn);
  3164. // if canplay was already fired, we won't know when to play, so just give it a try
  3165. this.tryPlayVideo(element);
  3166. }
  3167. /**
  3168. * Checks if the given video element is currently playing.
  3169. */
  3170. isVideoPlaying(video) {
  3171. return (video.currentTime > 0 &&
  3172. !video.paused &&
  3173. !video.ended &&
  3174. video.readyState > 2);
  3175. }
  3176. /**
  3177. * Just tries to play the video and logs any errors.
  3178. * The play call is only made is the video is not already playing.
  3179. */
  3180. tryPlayVideo(videoElement) {
  3181. return __awaiter(this, void 0, void 0, function* () {
  3182. if (this.isVideoPlaying(videoElement)) {
  3183. console.warn('Trying to play video that is already playing.');
  3184. return;
  3185. }
  3186. try {
  3187. yield videoElement.play();
  3188. }
  3189. catch (_a) {
  3190. console.warn('It was not possible to play the video.');
  3191. }
  3192. });
  3193. }
  3194. /**
  3195. * Searches and validates a media element.
  3196. */
  3197. getMediaElement(mediaElementId, type) {
  3198. const mediaElement = document.getElementById(mediaElementId);
  3199. if (!mediaElement) {
  3200. throw new ArgumentException(`element with id '${mediaElementId}' not found`);
  3201. }
  3202. if (mediaElement.nodeName.toLowerCase() !== type.toLowerCase()) {
  3203. throw new ArgumentException(`element with id '${mediaElementId}' must be an ${type} element`);
  3204. }
  3205. return mediaElement;
  3206. }
  3207. /**
  3208. * Decodes the barcode from an image.
  3209. *
  3210. * @param {(string|HTMLImageElement)} [source] The image element that can be either an element id or the element itself. Can be undefined in which case the decoding will be done from the imageUrl parameter.
  3211. * @param {string} [url]
  3212. * @returns {Promise<Result>} The decoding result.
  3213. *
  3214. * @memberOf BrowserCodeReader
  3215. */
  3216. decodeFromImage(source, url) {
  3217. if (!source && !url) {
  3218. throw new ArgumentException('either imageElement with a src set or an url must be provided');
  3219. }
  3220. if (url && !source) {
  3221. return this.decodeFromImageUrl(url);
  3222. }
  3223. return this.decodeFromImageElement(source);
  3224. }
  3225. /**
  3226. * Decodes the barcode from a video.
  3227. *
  3228. * @param {(string|HTMLImageElement)} [source] The image element that can be either an element id or the element itself. Can be undefined in which case the decoding will be done from the imageUrl parameter.
  3229. * @param {string} [url]
  3230. * @returns {Promise<Result>} The decoding result.
  3231. *
  3232. * @memberOf BrowserCodeReader
  3233. */
  3234. decodeFromVideo(source, url) {
  3235. if (!source && !url) {
  3236. throw new ArgumentException('Either an element with a src set or an URL must be provided');
  3237. }
  3238. if (url && !source) {
  3239. return this.decodeFromVideoUrl(url);
  3240. }
  3241. return this.decodeFromVideoElement(source);
  3242. }
  3243. /**
  3244. * Decodes continuously the barcode from a video.
  3245. *
  3246. * @param {(string|HTMLImageElement)} [source] The image element that can be either an element id or the element itself. Can be undefined in which case the decoding will be done from the imageUrl parameter.
  3247. * @param {string} [url]
  3248. * @returns {Promise<Result>} The decoding result.
  3249. *
  3250. * @memberOf BrowserCodeReader
  3251. *
  3252. * @experimental
  3253. */
  3254. decodeFromVideoContinuously(source, url, callbackFn) {
  3255. if (undefined === source && undefined === url) {
  3256. throw new ArgumentException('Either an element with a src set or an URL must be provided');
  3257. }
  3258. if (url && !source) {
  3259. return this.decodeFromVideoUrlContinuously(url, callbackFn);
  3260. }
  3261. return this.decodeFromVideoElementContinuously(source, callbackFn);
  3262. }
  3263. /**
  3264. * Decodes something from an image HTML element.
  3265. */
  3266. decodeFromImageElement(source) {
  3267. if (!source) {
  3268. throw new ArgumentException('An image element must be provided.');
  3269. }
  3270. this.reset();
  3271. const element = this.prepareImageElement(source);
  3272. this.imageElement = element;
  3273. let task;
  3274. if (this.isImageLoaded(element)) {
  3275. task = this.decodeOnce(element, false, true);
  3276. }
  3277. else {
  3278. task = this._decodeOnLoadImage(element);
  3279. }
  3280. return task;
  3281. }
  3282. /**
  3283. * Decodes something from an image HTML element.
  3284. */
  3285. decodeFromVideoElement(source) {
  3286. const element = this._decodeFromVideoElementSetup(source);
  3287. return this._decodeOnLoadVideo(element);
  3288. }
  3289. /**
  3290. * Decodes something from an image HTML element.
  3291. */
  3292. decodeFromVideoElementContinuously(source, callbackFn) {
  3293. const element = this._decodeFromVideoElementSetup(source);
  3294. return this._decodeOnLoadVideoContinuously(element, callbackFn);
  3295. }
  3296. /**
  3297. * Sets up the video source so it can be decoded when loaded.
  3298. *
  3299. * @param source The video source element.
  3300. */
  3301. _decodeFromVideoElementSetup(source) {
  3302. if (!source) {
  3303. throw new ArgumentException('A video element must be provided.');
  3304. }
  3305. this.reset();
  3306. const element = this.prepareVideoElement(source);
  3307. // defines the video element before starts decoding
  3308. this.videoElement = element;
  3309. return element;
  3310. }
  3311. /**
  3312. * Decodes an image from a URL.
  3313. */
  3314. decodeFromImageUrl(url) {
  3315. if (!url) {
  3316. throw new ArgumentException('An URL must be provided.');
  3317. }
  3318. this.reset();
  3319. const element = this.prepareImageElement();
  3320. this.imageElement = element;
  3321. const decodeTask = this._decodeOnLoadImage(element);
  3322. element.src = url;
  3323. return decodeTask;
  3324. }
  3325. /**
  3326. * Decodes an image from a URL.
  3327. */
  3328. decodeFromVideoUrl(url) {
  3329. if (!url) {
  3330. throw new ArgumentException('An URL must be provided.');
  3331. }
  3332. this.reset();
  3333. // creates a new element
  3334. const element = this.prepareVideoElement();
  3335. const decodeTask = this.decodeFromVideoElement(element);
  3336. element.src = url;
  3337. return decodeTask;
  3338. }
  3339. /**
  3340. * Decodes an image from a URL.
  3341. *
  3342. * @experimental
  3343. */
  3344. decodeFromVideoUrlContinuously(url, callbackFn) {
  3345. if (!url) {
  3346. throw new ArgumentException('An URL must be provided.');
  3347. }
  3348. this.reset();
  3349. // creates a new element
  3350. const element = this.prepareVideoElement();
  3351. const decodeTask = this.decodeFromVideoElementContinuously(element, callbackFn);
  3352. element.src = url;
  3353. return decodeTask;
  3354. }
  3355. _decodeOnLoadImage(element) {
  3356. return new Promise((resolve, reject) => {
  3357. this.imageLoadedListener = () => this.decodeOnce(element, false, true).then(resolve, reject);
  3358. element.addEventListener('load', this.imageLoadedListener);
  3359. });
  3360. }
  3361. _decodeOnLoadVideo(videoElement) {
  3362. return __awaiter(this, void 0, void 0, function* () {
  3363. // plays the video
  3364. yield this.playVideoOnLoadAsync(videoElement);
  3365. // starts decoding after played the video
  3366. return yield this.decodeOnce(videoElement);
  3367. });
  3368. }
  3369. _decodeOnLoadVideoContinuously(videoElement, callbackFn) {
  3370. return __awaiter(this, void 0, void 0, function* () {
  3371. // plays the video
  3372. yield this.playVideoOnLoadAsync(videoElement);
  3373. // starts decoding after played the video
  3374. this.decodeContinuously(videoElement, callbackFn);
  3375. });
  3376. }
  3377. isImageLoaded(img) {
  3378. // During the onload event, IE correctly identifies any images that
  3379. // weren’t downloaded as not complete. Others should too. Gecko-based
  3380. // browsers act like NS4 in that they report this incorrectly.
  3381. if (!img.complete) {
  3382. return false;
  3383. }
  3384. // However, they do have two very useful properties: naturalWidth and
  3385. // naturalHeight. These give the true size of the image. If it failed
  3386. // to load, either of these should be zero.
  3387. if (img.naturalWidth === 0) {
  3388. return false;
  3389. }
  3390. // No other way of checking: assume it’s ok.
  3391. return true;
  3392. }
  3393. prepareImageElement(imageSource) {
  3394. let imageElement;
  3395. if (typeof imageSource === 'undefined') {
  3396. imageElement = document.createElement('img');
  3397. imageElement.width = 200;
  3398. imageElement.height = 200;
  3399. }
  3400. if (typeof imageSource === 'string') {
  3401. imageElement = this.getMediaElement(imageSource, 'img');
  3402. }
  3403. if (imageSource instanceof HTMLImageElement) {
  3404. imageElement = imageSource;
  3405. }
  3406. return imageElement;
  3407. }
  3408. /**
  3409. * Sets a HTMLVideoElement for scanning or creates a new one.
  3410. *
  3411. * @param videoSource The HTMLVideoElement to be set.
  3412. */
  3413. prepareVideoElement(videoSource) {
  3414. let videoElement;
  3415. if (!videoSource && typeof document !== 'undefined') {
  3416. videoElement = document.createElement('video');
  3417. videoElement.width = 200;
  3418. videoElement.height = 200;
  3419. }
  3420. if (typeof videoSource === 'string') {
  3421. videoElement = (this.getMediaElement(videoSource, 'video'));
  3422. }
  3423. if (videoSource instanceof HTMLVideoElement) {
  3424. videoElement = videoSource;
  3425. }
  3426. // Needed for iOS 11
  3427. videoElement.setAttribute('autoplay', 'true');
  3428. videoElement.setAttribute('muted', 'true');
  3429. videoElement.setAttribute('playsinline', 'true');
  3430. return videoElement;
  3431. }
  3432. /**
  3433. * Tries to decode from the video input until it finds some value.
  3434. */
  3435. decodeOnce(element, retryIfNotFound = true, retryIfChecksumOrFormatError = true) {
  3436. this._stopAsyncDecode = false;
  3437. const loop = (resolve, reject) => {
  3438. if (this._stopAsyncDecode) {
  3439. reject(new NotFoundException('Video stream has ended before any code could be detected.'));
  3440. this._stopAsyncDecode = undefined;
  3441. return;
  3442. }
  3443. try {
  3444. const result = this.decode(element);
  3445. resolve(result);
  3446. }
  3447. catch (e) {
  3448. const ifNotFound = retryIfNotFound && e instanceof NotFoundException;
  3449. const isChecksumOrFormatError = e instanceof ChecksumException || e instanceof FormatException;
  3450. const ifChecksumOrFormat = isChecksumOrFormatError && retryIfChecksumOrFormatError;
  3451. if (ifNotFound || ifChecksumOrFormat) {
  3452. // trying again
  3453. return setTimeout(loop, this._timeBetweenDecodingAttempts, resolve, reject);
  3454. }
  3455. reject(e);
  3456. }
  3457. };
  3458. return new Promise((resolve, reject) => loop(resolve, reject));
  3459. }
  3460. /**
  3461. * Continuously decodes from video input.
  3462. */
  3463. decodeContinuously(element, callbackFn) {
  3464. this._stopContinuousDecode = false;
  3465. const loop = () => {
  3466. if (this._stopContinuousDecode) {
  3467. this._stopContinuousDecode = undefined;
  3468. return;
  3469. }
  3470. try {
  3471. const result = this.decode(element);
  3472. callbackFn(result, null);
  3473. setTimeout(loop, this.timeBetweenScansMillis);
  3474. }
  3475. catch (e) {
  3476. callbackFn(null, e);
  3477. const isChecksumOrFormatError = e instanceof ChecksumException || e instanceof FormatException;
  3478. const isNotFound = e instanceof NotFoundException;
  3479. if (isChecksumOrFormatError || isNotFound) {
  3480. // trying again
  3481. setTimeout(loop, this._timeBetweenDecodingAttempts);
  3482. }
  3483. }
  3484. };
  3485. loop();
  3486. }
  3487. /**
  3488. * Gets the BinaryBitmap for ya! (and decodes it)
  3489. */
  3490. decode(element) {
  3491. // get binary bitmap for decode function
  3492. const binaryBitmap = this.createBinaryBitmap(element);
  3493. return this.decodeBitmap(binaryBitmap);
  3494. }
  3495. /**
  3496. * Creates a binaryBitmap based in some image source.
  3497. *
  3498. * @param mediaElement HTML element containing drawable image source.
  3499. */
  3500. createBinaryBitmap(mediaElement) {
  3501. this.getCaptureCanvasContext(mediaElement);
  3502. // doing a scan with inverted colors on the second scan should only happen for video elements
  3503. let doAutoInvert = false;
  3504. if (mediaElement instanceof HTMLVideoElement) {
  3505. this.drawFrameOnCanvas(mediaElement);
  3506. doAutoInvert = true;
  3507. }
  3508. else {
  3509. this.drawImageOnCanvas(mediaElement);
  3510. }
  3511. const canvas = this.getCaptureCanvas(mediaElement);
  3512. const luminanceSource = new HTMLCanvasElementLuminanceSource(canvas, doAutoInvert);
  3513. const hybridBinarizer = new HybridBinarizer(luminanceSource);
  3514. return new BinaryBitmap(hybridBinarizer);
  3515. }
  3516. /**
  3517. *
  3518. */
  3519. getCaptureCanvasContext(mediaElement) {
  3520. if (!this.captureCanvasContext) {
  3521. const elem = this.getCaptureCanvas(mediaElement);
  3522. let ctx;
  3523. try {
  3524. ctx = elem.getContext('2d', { willReadFrequently: true });
  3525. }
  3526. catch (e) {
  3527. ctx = elem.getContext('2d');
  3528. }
  3529. this.captureCanvasContext = ctx;
  3530. }
  3531. return this.captureCanvasContext;
  3532. }
  3533. /**
  3534. *
  3535. */
  3536. getCaptureCanvas(mediaElement) {
  3537. if (!this.captureCanvas) {
  3538. const elem = this.createCaptureCanvas(mediaElement);
  3539. this.captureCanvas = elem;
  3540. }
  3541. return this.captureCanvas;
  3542. }
  3543. /**
  3544. * Overwriting this allows you to manipulate the next frame in anyway you want before decode.
  3545. */
  3546. drawFrameOnCanvas(srcElement, dimensions = {
  3547. sx: 0,
  3548. sy: 0,
  3549. sWidth: srcElement.videoWidth,
  3550. sHeight: srcElement.videoHeight,
  3551. dx: 0,
  3552. dy: 0,
  3553. dWidth: srcElement.videoWidth,
  3554. dHeight: srcElement.videoHeight,
  3555. }, canvasElementContext = this.captureCanvasContext) {
  3556. canvasElementContext.drawImage(srcElement, dimensions.sx, dimensions.sy, dimensions.sWidth, dimensions.sHeight, dimensions.dx, dimensions.dy, dimensions.dWidth, dimensions.dHeight);
  3557. }
  3558. /**
  3559. * Ovewriting this allows you to manipulate the snapshot image in anyway you want before decode.
  3560. */
  3561. drawImageOnCanvas(srcElement, dimensions = {
  3562. sx: 0,
  3563. sy: 0,
  3564. sWidth: srcElement.naturalWidth,
  3565. sHeight: srcElement.naturalHeight,
  3566. dx: 0,
  3567. dy: 0,
  3568. dWidth: srcElement.naturalWidth,
  3569. dHeight: srcElement.naturalHeight,
  3570. }, canvasElementContext = this.captureCanvasContext) {
  3571. canvasElementContext.drawImage(srcElement, dimensions.sx, dimensions.sy, dimensions.sWidth, dimensions.sHeight, dimensions.dx, dimensions.dy, dimensions.dWidth, dimensions.dHeight);
  3572. }
  3573. /**
  3574. * Call the encapsulated readers decode
  3575. */
  3576. decodeBitmap(binaryBitmap) {
  3577. return this.reader.decode(binaryBitmap, this._hints);
  3578. }
  3579. /**
  3580. * 🖌 Prepares the canvas for capture and scan frames.
  3581. */
  3582. createCaptureCanvas(mediaElement) {
  3583. if (typeof document === 'undefined') {
  3584. this._destroyCaptureCanvas();
  3585. return null;
  3586. }
  3587. const canvasElement = document.createElement('canvas');
  3588. let width;
  3589. let height;
  3590. if (typeof mediaElement !== 'undefined') {
  3591. if (mediaElement instanceof HTMLVideoElement) {
  3592. width = mediaElement.videoWidth;
  3593. height = mediaElement.videoHeight;
  3594. }
  3595. else if (mediaElement instanceof HTMLImageElement) {
  3596. width = mediaElement.naturalWidth || mediaElement.width;
  3597. height = mediaElement.naturalHeight || mediaElement.height;
  3598. }
  3599. }
  3600. canvasElement.style.width = width + 'px';
  3601. canvasElement.style.height = height + 'px';
  3602. canvasElement.width = width;
  3603. canvasElement.height = height;
  3604. return canvasElement;
  3605. }
  3606. /**
  3607. * Stops the continuous scan and cleans the stream.
  3608. */
  3609. stopStreams() {
  3610. if (this.stream) {
  3611. this.stream.getVideoTracks().forEach(t => t.stop());
  3612. this.stream = undefined;
  3613. }
  3614. if (this._stopAsyncDecode === false) {
  3615. this.stopAsyncDecode();
  3616. }
  3617. if (this._stopContinuousDecode === false) {
  3618. this.stopContinuousDecode();
  3619. }
  3620. }
  3621. /**
  3622. * Resets the code reader to the initial state. Cancels any ongoing barcode scanning from video or camera.
  3623. *
  3624. * @memberOf BrowserCodeReader
  3625. */
  3626. reset() {
  3627. // stops the camera, preview and scan 🔴
  3628. this.stopStreams();
  3629. // clean and forget about HTML elements
  3630. this._destroyVideoElement();
  3631. this._destroyImageElement();
  3632. this._destroyCaptureCanvas();
  3633. }
  3634. _destroyVideoElement() {
  3635. if (!this.videoElement) {
  3636. return;
  3637. }
  3638. // first gives freedon to the element 🕊
  3639. if (typeof this.videoEndedListener !== 'undefined') {
  3640. this.videoElement.removeEventListener('ended', this.videoEndedListener);
  3641. }
  3642. if (typeof this.videoPlayingEventListener !== 'undefined') {
  3643. this.videoElement.removeEventListener('playing', this.videoPlayingEventListener);
  3644. }
  3645. if (typeof this.videoCanPlayListener !== 'undefined') {
  3646. this.videoElement.removeEventListener('loadedmetadata', this.videoCanPlayListener);
  3647. }
  3648. // then forgets about that element 😢
  3649. this.cleanVideoSource(this.videoElement);
  3650. this.videoElement = undefined;
  3651. }
  3652. _destroyImageElement() {
  3653. if (!this.imageElement) {
  3654. return;
  3655. }
  3656. // first gives freedon to the element 🕊
  3657. if (undefined !== this.imageLoadedListener) {
  3658. this.imageElement.removeEventListener('load', this.imageLoadedListener);
  3659. }
  3660. // then forget about that element 😢
  3661. this.imageElement.src = undefined;
  3662. this.imageElement.removeAttribute('src');
  3663. this.imageElement = undefined;
  3664. }
  3665. /**
  3666. * Cleans canvas references 🖌
  3667. */
  3668. _destroyCaptureCanvas() {
  3669. // then forget about that element 😢
  3670. this.captureCanvasContext = undefined;
  3671. this.captureCanvas = undefined;
  3672. }
  3673. /**
  3674. * Defines what the videoElement src will be.
  3675. *
  3676. * @param videoElement
  3677. * @param stream
  3678. */
  3679. addVideoSource(videoElement, stream) {
  3680. // Older browsers may not have `srcObject`
  3681. try {
  3682. // @note Throws Exception if interrupted by a new loaded request
  3683. videoElement.srcObject = stream;
  3684. }
  3685. catch (err) {
  3686. // @note Avoid using this in new browsers, as it is going away.
  3687. // @ts-ignore
  3688. videoElement.src = URL.createObjectURL(stream);
  3689. }
  3690. }
  3691. /**
  3692. * Unbinds a HTML video src property.
  3693. *
  3694. * @param videoElement
  3695. */
  3696. cleanVideoSource(videoElement) {
  3697. try {
  3698. videoElement.srcObject = null;
  3699. }
  3700. catch (err) {
  3701. videoElement.src = '';
  3702. }
  3703. this.videoElement.removeAttribute('src');
  3704. }
  3705. }
  3706. /*
  3707. * Copyright 2007 ZXing authors
  3708. *
  3709. * Licensed under the Apache License, Version 2.0 (the "License");
  3710. * you may not use this file except in compliance with the License.
  3711. * You may obtain a copy of the License at
  3712. *
  3713. * http://www.apache.org/licenses/LICENSE-2.0
  3714. *
  3715. * Unless required by applicable law or agreed to in writing, software
  3716. * distributed under the License is distributed on an "AS IS" BASIS,
  3717. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3718. * See the License for the specific language governing permissions and
  3719. * limitations under the License.
  3720. */
  3721. /**
  3722. * <p>Encapsulates the result of decoding a barcode within an image.</p>
  3723. *
  3724. * @author Sean Owen
  3725. */
  3726. class Result$1 {
  3727. // public constructor(private text: string,
  3728. // Uint8Array rawBytes,
  3729. // ResultPoconst resultPoints: Int32Array,
  3730. // BarcodeFormat format) {
  3731. // this(text, rawBytes, resultPoints, format, System.currentTimeMillis())
  3732. // }
  3733. // public constructor(text: string,
  3734. // Uint8Array rawBytes,
  3735. // ResultPoconst resultPoints: Int32Array,
  3736. // BarcodeFormat format,
  3737. // long timestamp) {
  3738. // this(text, rawBytes, rawBytes == null ? 0 : 8 * rawBytes.length,
  3739. // resultPoints, format, timestamp)
  3740. // }
  3741. constructor(text, rawBytes, numBits = rawBytes == null ? 0 : 8 * rawBytes.length, resultPoints, format, timestamp = System.currentTimeMillis()) {
  3742. this.text = text;
  3743. this.rawBytes = rawBytes;
  3744. this.numBits = numBits;
  3745. this.resultPoints = resultPoints;
  3746. this.format = format;
  3747. this.timestamp = timestamp;
  3748. this.text = text;
  3749. this.rawBytes = rawBytes;
  3750. if (undefined === numBits || null === numBits) {
  3751. this.numBits = (rawBytes === null || rawBytes === undefined) ? 0 : 8 * rawBytes.length;
  3752. }
  3753. else {
  3754. this.numBits = numBits;
  3755. }
  3756. this.resultPoints = resultPoints;
  3757. this.format = format;
  3758. this.resultMetadata = null;
  3759. if (undefined === timestamp || null === timestamp) {
  3760. this.timestamp = System.currentTimeMillis();
  3761. }
  3762. else {
  3763. this.timestamp = timestamp;
  3764. }
  3765. }
  3766. /**
  3767. * @return raw text encoded by the barcode
  3768. */
  3769. getText() {
  3770. return this.text;
  3771. }
  3772. /**
  3773. * @return raw bytes encoded by the barcode, if applicable, otherwise {@code null}
  3774. */
  3775. getRawBytes() {
  3776. return this.rawBytes;
  3777. }
  3778. /**
  3779. * @return how many bits of {@link #getRawBytes()} are valid; typically 8 times its length
  3780. * @since 3.3.0
  3781. */
  3782. getNumBits() {
  3783. return this.numBits;
  3784. }
  3785. /**
  3786. * @return points related to the barcode in the image. These are typically points
  3787. * identifying finder patterns or the corners of the barcode. The exact meaning is
  3788. * specific to the type of barcode that was decoded.
  3789. */
  3790. getResultPoints() {
  3791. return this.resultPoints;
  3792. }
  3793. /**
  3794. * @return {@link BarcodeFormat} representing the format of the barcode that was decoded
  3795. */
  3796. getBarcodeFormat() {
  3797. return this.format;
  3798. }
  3799. /**
  3800. * @return {@link Map} mapping {@link ResultMetadataType} keys to values. May be
  3801. * {@code null}. This contains optional metadata about what was detected about the barcode,
  3802. * like orientation.
  3803. */
  3804. getResultMetadata() {
  3805. return this.resultMetadata;
  3806. }
  3807. putMetadata(type, value) {
  3808. if (this.resultMetadata === null) {
  3809. this.resultMetadata = new Map();
  3810. }
  3811. this.resultMetadata.set(type, value);
  3812. }
  3813. putAllMetadata(metadata) {
  3814. if (metadata !== null) {
  3815. if (this.resultMetadata === null) {
  3816. this.resultMetadata = metadata;
  3817. }
  3818. else {
  3819. this.resultMetadata = new Map(metadata);
  3820. }
  3821. }
  3822. }
  3823. addResultPoints(newPoints) {
  3824. const oldPoints = this.resultPoints;
  3825. if (oldPoints === null) {
  3826. this.resultPoints = newPoints;
  3827. }
  3828. else if (newPoints !== null && newPoints.length > 0) {
  3829. const allPoints = new Array(oldPoints.length + newPoints.length);
  3830. System.arraycopy(oldPoints, 0, allPoints, 0, oldPoints.length);
  3831. System.arraycopy(newPoints, 0, allPoints, oldPoints.length, newPoints.length);
  3832. this.resultPoints = allPoints;
  3833. }
  3834. }
  3835. getTimestamp() {
  3836. return this.timestamp;
  3837. }
  3838. /*@Override*/
  3839. toString() {
  3840. return this.text;
  3841. }
  3842. }
  3843. /*
  3844. * Direct port to TypeScript of ZXing by Adrian Toșcă
  3845. */
  3846. /*
  3847. * Copyright 2009 ZXing authors
  3848. *
  3849. * Licensed under the Apache License, Version 2.0 (the "License");
  3850. * you may not use this file except in compliance with the License.
  3851. * You may obtain a copy of the License at
  3852. *
  3853. * http://www.apache.org/licenses/LICENSE-2.0
  3854. *
  3855. * Unless required by applicable law or agreed to in writing, software
  3856. * distributed under the License is distributed on an "AS IS" BASIS,
  3857. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3858. * See the License for the specific language governing permissions and
  3859. * limitations under the License.
  3860. */
  3861. /*namespace com.google.zxing {*/
  3862. /**
  3863. * Enumerates barcode formats known to this package. Please keep alphabetized.
  3864. *
  3865. * @author Sean Owen
  3866. */
  3867. var BarcodeFormat;
  3868. (function (BarcodeFormat) {
  3869. /** Aztec 2D barcode format. */
  3870. BarcodeFormat[BarcodeFormat["AZTEC"] = 0] = "AZTEC";
  3871. /** CODABAR 1D format. */
  3872. BarcodeFormat[BarcodeFormat["CODABAR"] = 1] = "CODABAR";
  3873. /** Code 39 1D format. */
  3874. BarcodeFormat[BarcodeFormat["CODE_39"] = 2] = "CODE_39";
  3875. /** Code 93 1D format. */
  3876. BarcodeFormat[BarcodeFormat["CODE_93"] = 3] = "CODE_93";
  3877. /** Code 128 1D format. */
  3878. BarcodeFormat[BarcodeFormat["CODE_128"] = 4] = "CODE_128";
  3879. /** Data Matrix 2D barcode format. */
  3880. BarcodeFormat[BarcodeFormat["DATA_MATRIX"] = 5] = "DATA_MATRIX";
  3881. /** EAN-8 1D format. */
  3882. BarcodeFormat[BarcodeFormat["EAN_8"] = 6] = "EAN_8";
  3883. /** EAN-13 1D format. */
  3884. BarcodeFormat[BarcodeFormat["EAN_13"] = 7] = "EAN_13";
  3885. /** ITF (Interleaved Two of Five) 1D format. */
  3886. BarcodeFormat[BarcodeFormat["ITF"] = 8] = "ITF";
  3887. /** MaxiCode 2D barcode format. */
  3888. BarcodeFormat[BarcodeFormat["MAXICODE"] = 9] = "MAXICODE";
  3889. /** PDF417 format. */
  3890. BarcodeFormat[BarcodeFormat["PDF_417"] = 10] = "PDF_417";
  3891. /** QR Code 2D barcode format. */
  3892. BarcodeFormat[BarcodeFormat["QR_CODE"] = 11] = "QR_CODE";
  3893. /** RSS 14 */
  3894. BarcodeFormat[BarcodeFormat["RSS_14"] = 12] = "RSS_14";
  3895. /** RSS EXPANDED */
  3896. BarcodeFormat[BarcodeFormat["RSS_EXPANDED"] = 13] = "RSS_EXPANDED";
  3897. /** UPC-A 1D format. */
  3898. BarcodeFormat[BarcodeFormat["UPC_A"] = 14] = "UPC_A";
  3899. /** UPC-E 1D format. */
  3900. BarcodeFormat[BarcodeFormat["UPC_E"] = 15] = "UPC_E";
  3901. /** UPC/EAN extension format. Not a stand-alone format. */
  3902. BarcodeFormat[BarcodeFormat["UPC_EAN_EXTENSION"] = 16] = "UPC_EAN_EXTENSION";
  3903. })(BarcodeFormat || (BarcodeFormat = {}));
  3904. var BarcodeFormat$1 = BarcodeFormat;
  3905. /*
  3906. * Copyright 2008 ZXing authors
  3907. *
  3908. * Licensed under the Apache License, Version 2.0 (the "License");
  3909. * you may not use this file except in compliance with the License.
  3910. * You may obtain a copy of the License at
  3911. *
  3912. * http://www.apache.org/licenses/LICENSE-2.0
  3913. *
  3914. * Unless required by applicable law or agreed to in writing, software
  3915. * distributed under the License is distributed on an "AS IS" BASIS,
  3916. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3917. * See the License for the specific language governing permissions and
  3918. * limitations under the License.
  3919. */
  3920. /*namespace com.google.zxing {*/
  3921. /**
  3922. * Represents some type of metadata about the result of the decoding that the decoder
  3923. * wishes to communicate back to the caller.
  3924. *
  3925. * @author Sean Owen
  3926. */
  3927. var ResultMetadataType;
  3928. (function (ResultMetadataType) {
  3929. /**
  3930. * Unspecified, application-specific metadata. Maps to an unspecified {@link Object}.
  3931. */
  3932. ResultMetadataType[ResultMetadataType["OTHER"] = 0] = "OTHER";
  3933. /**
  3934. * Denotes the likely approximate orientation of the barcode in the image. This value
  3935. * is given as degrees rotated clockwise from the normal, upright orientation.
  3936. * For example a 1D barcode which was found by reading top-to-bottom would be
  3937. * said to have orientation "90". This key maps to an {@link Integer} whose
  3938. * value is in the range [0,360).
  3939. */
  3940. ResultMetadataType[ResultMetadataType["ORIENTATION"] = 1] = "ORIENTATION";
  3941. /**
  3942. * <p>2D barcode formats typically encode text, but allow for a sort of 'byte mode'
  3943. * which is sometimes used to encode binary data. While {@link Result} makes available
  3944. * the complete raw bytes in the barcode for these formats, it does not offer the bytes
  3945. * from the byte segments alone.</p>
  3946. *
  3947. * <p>This maps to a {@link java.util.List} of byte arrays corresponding to the
  3948. * raw bytes in the byte segments in the barcode, in order.</p>
  3949. */
  3950. ResultMetadataType[ResultMetadataType["BYTE_SEGMENTS"] = 2] = "BYTE_SEGMENTS";
  3951. /**
  3952. * Error correction level used, if applicable. The value type depends on the
  3953. * format, but is typically a String.
  3954. */
  3955. ResultMetadataType[ResultMetadataType["ERROR_CORRECTION_LEVEL"] = 3] = "ERROR_CORRECTION_LEVEL";
  3956. /**
  3957. * For some periodicals, indicates the issue number as an {@link Integer}.
  3958. */
  3959. ResultMetadataType[ResultMetadataType["ISSUE_NUMBER"] = 4] = "ISSUE_NUMBER";
  3960. /**
  3961. * For some products, indicates the suggested retail price in the barcode as a
  3962. * formatted {@link String}.
  3963. */
  3964. ResultMetadataType[ResultMetadataType["SUGGESTED_PRICE"] = 5] = "SUGGESTED_PRICE";
  3965. /**
  3966. * For some products, the possible country of manufacture as a {@link String} denoting the
  3967. * ISO country code. Some map to multiple possible countries, like "US/CA".
  3968. */
  3969. ResultMetadataType[ResultMetadataType["POSSIBLE_COUNTRY"] = 6] = "POSSIBLE_COUNTRY";
  3970. /**
  3971. * For some products, the extension text
  3972. */
  3973. ResultMetadataType[ResultMetadataType["UPC_EAN_EXTENSION"] = 7] = "UPC_EAN_EXTENSION";
  3974. /**
  3975. * PDF417-specific metadata
  3976. */
  3977. ResultMetadataType[ResultMetadataType["PDF417_EXTRA_METADATA"] = 8] = "PDF417_EXTRA_METADATA";
  3978. /**
  3979. * If the code format supports structured append and the current scanned code is part of one then the
  3980. * sequence number is given with it.
  3981. */
  3982. ResultMetadataType[ResultMetadataType["STRUCTURED_APPEND_SEQUENCE"] = 9] = "STRUCTURED_APPEND_SEQUENCE";
  3983. /**
  3984. * If the code format supports structured append and the current scanned code is part of one then the
  3985. * parity is given with it.
  3986. */
  3987. ResultMetadataType[ResultMetadataType["STRUCTURED_APPEND_PARITY"] = 10] = "STRUCTURED_APPEND_PARITY";
  3988. })(ResultMetadataType || (ResultMetadataType = {}));
  3989. var ResultMetadataType$1 = ResultMetadataType;
  3990. /*
  3991. * Copyright 2007 ZXing authors
  3992. *
  3993. * Licensed under the Apache License, Version 2.0 (the "License");
  3994. * you may not use this file except in compliance with the License.
  3995. * You may obtain a copy of the License at
  3996. *
  3997. * http://www.apache.org/licenses/LICENSE-2.0
  3998. *
  3999. * Unless required by applicable law or agreed to in writing, software
  4000. * distributed under the License is distributed on an "AS IS" BASIS,
  4001. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4002. * See the License for the specific language governing permissions and
  4003. * limitations under the License.
  4004. */
  4005. /*namespace com.google.zxing.common {*/
  4006. /*import java.util.List;*/
  4007. /**
  4008. * <p>Encapsulates the result of decoding a matrix of bits. This typically
  4009. * applies to 2D barcode formats. For now it contains the raw bytes obtained,
  4010. * as well as a String interpretation of those bytes, if applicable.</p>
  4011. *
  4012. * @author Sean Owen
  4013. */
  4014. class DecoderResult {
  4015. // public constructor(rawBytes: Uint8Array,
  4016. // text: string,
  4017. // List<Uint8Array> byteSegments,
  4018. // String ecLevel) {
  4019. // this(rawBytes, text, byteSegments, ecLevel, -1, -1)
  4020. // }
  4021. constructor(rawBytes, text, byteSegments, ecLevel, structuredAppendSequenceNumber = -1, structuredAppendParity = -1) {
  4022. this.rawBytes = rawBytes;
  4023. this.text = text;
  4024. this.byteSegments = byteSegments;
  4025. this.ecLevel = ecLevel;
  4026. this.structuredAppendSequenceNumber = structuredAppendSequenceNumber;
  4027. this.structuredAppendParity = structuredAppendParity;
  4028. this.numBits = (rawBytes === undefined || rawBytes === null) ? 0 : 8 * rawBytes.length;
  4029. }
  4030. /**
  4031. * @return raw bytes representing the result, or {@code null} if not applicable
  4032. */
  4033. getRawBytes() {
  4034. return this.rawBytes;
  4035. }
  4036. /**
  4037. * @return how many bits of {@link #getRawBytes()} are valid; typically 8 times its length
  4038. * @since 3.3.0
  4039. */
  4040. getNumBits() {
  4041. return this.numBits;
  4042. }
  4043. /**
  4044. * @param numBits overrides the number of bits that are valid in {@link #getRawBytes()}
  4045. * @since 3.3.0
  4046. */
  4047. setNumBits(numBits /*int*/) {
  4048. this.numBits = numBits;
  4049. }
  4050. /**
  4051. * @return text representation of the result
  4052. */
  4053. getText() {
  4054. return this.text;
  4055. }
  4056. /**
  4057. * @return list of byte segments in the result, or {@code null} if not applicable
  4058. */
  4059. getByteSegments() {
  4060. return this.byteSegments;
  4061. }
  4062. /**
  4063. * @return name of error correction level used, or {@code null} if not applicable
  4064. */
  4065. getECLevel() {
  4066. return this.ecLevel;
  4067. }
  4068. /**
  4069. * @return number of errors corrected, or {@code null} if not applicable
  4070. */
  4071. getErrorsCorrected() {
  4072. return this.errorsCorrected;
  4073. }
  4074. setErrorsCorrected(errorsCorrected /*Integer*/) {
  4075. this.errorsCorrected = errorsCorrected;
  4076. }
  4077. /**
  4078. * @return number of erasures corrected, or {@code null} if not applicable
  4079. */
  4080. getErasures() {
  4081. return this.erasures;
  4082. }
  4083. setErasures(erasures /*Integer*/) {
  4084. this.erasures = erasures;
  4085. }
  4086. /**
  4087. * @return arbitrary additional metadata
  4088. */
  4089. getOther() {
  4090. return this.other;
  4091. }
  4092. setOther(other) {
  4093. this.other = other;
  4094. }
  4095. hasStructuredAppend() {
  4096. return this.structuredAppendParity >= 0 && this.structuredAppendSequenceNumber >= 0;
  4097. }
  4098. getStructuredAppendParity() {
  4099. return this.structuredAppendParity;
  4100. }
  4101. getStructuredAppendSequenceNumber() {
  4102. return this.structuredAppendSequenceNumber;
  4103. }
  4104. }
  4105. /*
  4106. * Copyright 2007 ZXing authors
  4107. *
  4108. * Licensed under the Apache License, Version 2.0 (the "License");
  4109. * you may not use this file except in compliance with the License.
  4110. * You may obtain a copy of the License at
  4111. *
  4112. * http://www.apache.org/licenses/LICENSE-2.0
  4113. *
  4114. * Unless required by applicable law or agreed to in writing, software
  4115. * distributed under the License is distributed on an "AS IS" BASIS,
  4116. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4117. * See the License for the specific language governing permissions and
  4118. * limitations under the License.
  4119. */
  4120. /**
  4121. * <p>This class contains utility methods for performing mathematical operations over
  4122. * the Galois Fields. Operations use a given primitive polynomial in calculations.</p>
  4123. *
  4124. * <p>Throughout this package, elements of the GF are represented as an {@code int}
  4125. * for convenience and speed (but at the cost of memory).
  4126. * </p>
  4127. *
  4128. * @author Sean Owen
  4129. * @author David Olivier
  4130. */
  4131. class AbstractGenericGF {
  4132. /**
  4133. * @return 2 to the power of a in GF(size)
  4134. */
  4135. exp(a) {
  4136. return this.expTable[a];
  4137. }
  4138. /**
  4139. * @return base 2 log of a in GF(size)
  4140. */
  4141. log(a /*int*/) {
  4142. if (a === 0) {
  4143. throw new IllegalArgumentException();
  4144. }
  4145. return this.logTable[a];
  4146. }
  4147. /**
  4148. * Implements both addition and subtraction -- they are the same in GF(size).
  4149. *
  4150. * @return sum/difference of a and b
  4151. */
  4152. static addOrSubtract(a /*int*/, b /*int*/) {
  4153. return a ^ b;
  4154. }
  4155. }
  4156. /*
  4157. * Copyright 2007 ZXing authors
  4158. *
  4159. * Licensed under the Apache License, Version 2.0 (the "License");
  4160. * you may not use this file except in compliance with the License.
  4161. * You may obtain a copy of the License at
  4162. *
  4163. * http://www.apache.org/licenses/LICENSE-2.0
  4164. *
  4165. * Unless required by applicable law or agreed to in writing, software
  4166. * distributed under the License is distributed on an "AS IS" BASIS,
  4167. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4168. * See the License for the specific language governing permissions and
  4169. * limitations under the License.
  4170. */
  4171. /**
  4172. * <p>Represents a polynomial whose coefficients are elements of a GF.
  4173. * Instances of this class are immutable.</p>
  4174. *
  4175. * <p>Much credit is due to William Rucklidge since portions of this code are an indirect
  4176. * port of his C++ Reed-Solomon implementation.</p>
  4177. *
  4178. * @author Sean Owen
  4179. */
  4180. class GenericGFPoly {
  4181. /**
  4182. * @param field the {@link GenericGF} instance representing the field to use
  4183. * to perform computations
  4184. * @param coefficients coefficients as ints representing elements of GF(size), arranged
  4185. * from most significant (highest-power term) coefficient to least significant
  4186. * @throws IllegalArgumentException if argument is null or empty,
  4187. * or if leading coefficient is 0 and this is not a
  4188. * constant polynomial (that is, it is not the monomial "0")
  4189. */
  4190. constructor(field, coefficients) {
  4191. if (coefficients.length === 0) {
  4192. throw new IllegalArgumentException();
  4193. }
  4194. this.field = field;
  4195. const coefficientsLength = coefficients.length;
  4196. if (coefficientsLength > 1 && coefficients[0] === 0) {
  4197. // Leading term must be non-zero for anything except the constant polynomial "0"
  4198. let firstNonZero = 1;
  4199. while (firstNonZero < coefficientsLength && coefficients[firstNonZero] === 0) {
  4200. firstNonZero++;
  4201. }
  4202. if (firstNonZero === coefficientsLength) {
  4203. this.coefficients = Int32Array.from([0]);
  4204. }
  4205. else {
  4206. this.coefficients = new Int32Array(coefficientsLength - firstNonZero);
  4207. System.arraycopy(coefficients, firstNonZero, this.coefficients, 0, this.coefficients.length);
  4208. }
  4209. }
  4210. else {
  4211. this.coefficients = coefficients;
  4212. }
  4213. }
  4214. getCoefficients() {
  4215. return this.coefficients;
  4216. }
  4217. /**
  4218. * @return degree of this polynomial
  4219. */
  4220. getDegree() {
  4221. return this.coefficients.length - 1;
  4222. }
  4223. /**
  4224. * @return true iff this polynomial is the monomial "0"
  4225. */
  4226. isZero() {
  4227. return this.coefficients[0] === 0;
  4228. }
  4229. /**
  4230. * @return coefficient of x^degree term in this polynomial
  4231. */
  4232. getCoefficient(degree /*int*/) {
  4233. return this.coefficients[this.coefficients.length - 1 - degree];
  4234. }
  4235. /**
  4236. * @return evaluation of this polynomial at a given point
  4237. */
  4238. evaluateAt(a /*int*/) {
  4239. if (a === 0) {
  4240. // Just return the x^0 coefficient
  4241. return this.getCoefficient(0);
  4242. }
  4243. const coefficients = this.coefficients;
  4244. let result;
  4245. if (a === 1) {
  4246. // Just the sum of the coefficients
  4247. result = 0;
  4248. for (let i = 0, length = coefficients.length; i !== length; i++) {
  4249. const coefficient = coefficients[i];
  4250. result = AbstractGenericGF.addOrSubtract(result, coefficient);
  4251. }
  4252. return result;
  4253. }
  4254. result = coefficients[0];
  4255. const size = coefficients.length;
  4256. const field = this.field;
  4257. for (let i = 1; i < size; i++) {
  4258. result = AbstractGenericGF.addOrSubtract(field.multiply(a, result), coefficients[i]);
  4259. }
  4260. return result;
  4261. }
  4262. addOrSubtract(other) {
  4263. if (!this.field.equals(other.field)) {
  4264. throw new IllegalArgumentException('GenericGFPolys do not have same GenericGF field');
  4265. }
  4266. if (this.isZero()) {
  4267. return other;
  4268. }
  4269. if (other.isZero()) {
  4270. return this;
  4271. }
  4272. let smallerCoefficients = this.coefficients;
  4273. let largerCoefficients = other.coefficients;
  4274. if (smallerCoefficients.length > largerCoefficients.length) {
  4275. const temp = smallerCoefficients;
  4276. smallerCoefficients = largerCoefficients;
  4277. largerCoefficients = temp;
  4278. }
  4279. let sumDiff = new Int32Array(largerCoefficients.length);
  4280. const lengthDiff = largerCoefficients.length - smallerCoefficients.length;
  4281. // Copy high-order terms only found in higher-degree polynomial's coefficients
  4282. System.arraycopy(largerCoefficients, 0, sumDiff, 0, lengthDiff);
  4283. for (let i = lengthDiff; i < largerCoefficients.length; i++) {
  4284. sumDiff[i] = AbstractGenericGF.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
  4285. }
  4286. return new GenericGFPoly(this.field, sumDiff);
  4287. }
  4288. multiply(other) {
  4289. if (!this.field.equals(other.field)) {
  4290. throw new IllegalArgumentException('GenericGFPolys do not have same GenericGF field');
  4291. }
  4292. if (this.isZero() || other.isZero()) {
  4293. return this.field.getZero();
  4294. }
  4295. const aCoefficients = this.coefficients;
  4296. const aLength = aCoefficients.length;
  4297. const bCoefficients = other.coefficients;
  4298. const bLength = bCoefficients.length;
  4299. const product = new Int32Array(aLength + bLength - 1);
  4300. const field = this.field;
  4301. for (let i = 0; i < aLength; i++) {
  4302. const aCoeff = aCoefficients[i];
  4303. for (let j = 0; j < bLength; j++) {
  4304. product[i + j] = AbstractGenericGF.addOrSubtract(product[i + j], field.multiply(aCoeff, bCoefficients[j]));
  4305. }
  4306. }
  4307. return new GenericGFPoly(field, product);
  4308. }
  4309. multiplyScalar(scalar /*int*/) {
  4310. if (scalar === 0) {
  4311. return this.field.getZero();
  4312. }
  4313. if (scalar === 1) {
  4314. return this;
  4315. }
  4316. const size = this.coefficients.length;
  4317. const field = this.field;
  4318. const product = new Int32Array(size);
  4319. const coefficients = this.coefficients;
  4320. for (let i = 0; i < size; i++) {
  4321. product[i] = field.multiply(coefficients[i], scalar);
  4322. }
  4323. return new GenericGFPoly(field, product);
  4324. }
  4325. multiplyByMonomial(degree /*int*/, coefficient /*int*/) {
  4326. if (degree < 0) {
  4327. throw new IllegalArgumentException();
  4328. }
  4329. if (coefficient === 0) {
  4330. return this.field.getZero();
  4331. }
  4332. const coefficients = this.coefficients;
  4333. const size = coefficients.length;
  4334. const product = new Int32Array(size + degree);
  4335. const field = this.field;
  4336. for (let i = 0; i < size; i++) {
  4337. product[i] = field.multiply(coefficients[i], coefficient);
  4338. }
  4339. return new GenericGFPoly(field, product);
  4340. }
  4341. divide(other) {
  4342. if (!this.field.equals(other.field)) {
  4343. throw new IllegalArgumentException('GenericGFPolys do not have same GenericGF field');
  4344. }
  4345. if (other.isZero()) {
  4346. throw new IllegalArgumentException('Divide by 0');
  4347. }
  4348. const field = this.field;
  4349. let quotient = field.getZero();
  4350. let remainder = this;
  4351. const denominatorLeadingTerm = other.getCoefficient(other.getDegree());
  4352. const inverseDenominatorLeadingTerm = field.inverse(denominatorLeadingTerm);
  4353. while (remainder.getDegree() >= other.getDegree() && !remainder.isZero()) {
  4354. const degreeDifference = remainder.getDegree() - other.getDegree();
  4355. const scale = field.multiply(remainder.getCoefficient(remainder.getDegree()), inverseDenominatorLeadingTerm);
  4356. const term = other.multiplyByMonomial(degreeDifference, scale);
  4357. const iterationQuotient = field.buildMonomial(degreeDifference, scale);
  4358. quotient = quotient.addOrSubtract(iterationQuotient);
  4359. remainder = remainder.addOrSubtract(term);
  4360. }
  4361. return [quotient, remainder];
  4362. }
  4363. /*@Override*/
  4364. toString() {
  4365. let result = '';
  4366. for (let degree = this.getDegree(); degree >= 0; degree--) {
  4367. let coefficient = this.getCoefficient(degree);
  4368. if (coefficient !== 0) {
  4369. if (coefficient < 0) {
  4370. result += ' - ';
  4371. coefficient = -coefficient;
  4372. }
  4373. else {
  4374. if (result.length > 0) {
  4375. result += ' + ';
  4376. }
  4377. }
  4378. if (degree === 0 || coefficient !== 1) {
  4379. const alphaPower = this.field.log(coefficient);
  4380. if (alphaPower === 0) {
  4381. result += '1';
  4382. }
  4383. else if (alphaPower === 1) {
  4384. result += 'a';
  4385. }
  4386. else {
  4387. result += 'a^';
  4388. result += alphaPower;
  4389. }
  4390. }
  4391. if (degree !== 0) {
  4392. if (degree === 1) {
  4393. result += 'x';
  4394. }
  4395. else {
  4396. result += 'x^';
  4397. result += degree;
  4398. }
  4399. }
  4400. }
  4401. }
  4402. return result;
  4403. }
  4404. }
  4405. /**
  4406. * Custom Error class of type Exception.
  4407. */
  4408. class ArithmeticException extends Exception {
  4409. }
  4410. ArithmeticException.kind = 'ArithmeticException';
  4411. /*
  4412. * Copyright 2007 ZXing authors
  4413. *
  4414. * Licensed under the Apache License, Version 2.0 (the "License");
  4415. * you may not use this file except in compliance with the License.
  4416. * You may obtain a copy of the License at
  4417. *
  4418. * http://www.apache.org/licenses/LICENSE-2.0
  4419. *
  4420. * Unless required by applicable law or agreed to in writing, software
  4421. * distributed under the License is distributed on an "AS IS" BASIS,
  4422. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4423. * See the License for the specific language governing permissions and
  4424. * limitations under the License.
  4425. */
  4426. /**
  4427. * <p>This class contains utility methods for performing mathematical operations over
  4428. * the Galois Fields. Operations use a given primitive polynomial in calculations.</p>
  4429. *
  4430. * <p>Throughout this package, elements of the GF are represented as an {@code int}
  4431. * for convenience and speed (but at the cost of memory).
  4432. * </p>
  4433. *
  4434. * @author Sean Owen
  4435. * @author David Olivier
  4436. */
  4437. class GenericGF extends AbstractGenericGF {
  4438. /**
  4439. * Create a representation of GF(size) using the given primitive polynomial.
  4440. *
  4441. * @param primitive irreducible polynomial whose coefficients are represented by
  4442. * the bits of an int, where the least-significant bit represents the constant
  4443. * coefficient
  4444. * @param size the size of the field
  4445. * @param b the factor b in the generator polynomial can be 0- or 1-based
  4446. * (g(x) = (x+a^b)(x+a^(b+1))...(x+a^(b+2t-1))).
  4447. * In most cases it should be 1, but for QR code it is 0.
  4448. */
  4449. constructor(primitive /*int*/, size /*int*/, generatorBase /*int*/) {
  4450. super();
  4451. this.primitive = primitive;
  4452. this.size = size;
  4453. this.generatorBase = generatorBase;
  4454. const expTable = new Int32Array(size);
  4455. let x = 1;
  4456. for (let i = 0; i < size; i++) {
  4457. expTable[i] = x;
  4458. x *= 2; // we're assuming the generator alpha is 2
  4459. if (x >= size) {
  4460. x ^= primitive;
  4461. x &= size - 1;
  4462. }
  4463. }
  4464. this.expTable = expTable;
  4465. const logTable = new Int32Array(size);
  4466. for (let i = 0; i < size - 1; i++) {
  4467. logTable[expTable[i]] = i;
  4468. }
  4469. this.logTable = logTable;
  4470. // logTable[0] == 0 but this should never be used
  4471. this.zero = new GenericGFPoly(this, Int32Array.from([0]));
  4472. this.one = new GenericGFPoly(this, Int32Array.from([1]));
  4473. }
  4474. getZero() {
  4475. return this.zero;
  4476. }
  4477. getOne() {
  4478. return this.one;
  4479. }
  4480. /**
  4481. * @return the monomial representing coefficient * x^degree
  4482. */
  4483. buildMonomial(degree /*int*/, coefficient /*int*/) {
  4484. if (degree < 0) {
  4485. throw new IllegalArgumentException();
  4486. }
  4487. if (coefficient === 0) {
  4488. return this.zero;
  4489. }
  4490. const coefficients = new Int32Array(degree + 1);
  4491. coefficients[0] = coefficient;
  4492. return new GenericGFPoly(this, coefficients);
  4493. }
  4494. /**
  4495. * @return multiplicative inverse of a
  4496. */
  4497. inverse(a /*int*/) {
  4498. if (a === 0) {
  4499. throw new ArithmeticException();
  4500. }
  4501. return this.expTable[this.size - this.logTable[a] - 1];
  4502. }
  4503. /**
  4504. * @return product of a and b in GF(size)
  4505. */
  4506. multiply(a /*int*/, b /*int*/) {
  4507. if (a === 0 || b === 0) {
  4508. return 0;
  4509. }
  4510. return this.expTable[(this.logTable[a] + this.logTable[b]) % (this.size - 1)];
  4511. }
  4512. getSize() {
  4513. return this.size;
  4514. }
  4515. getGeneratorBase() {
  4516. return this.generatorBase;
  4517. }
  4518. /*@Override*/
  4519. toString() {
  4520. return ('GF(0x' + Integer.toHexString(this.primitive) + ',' + this.size + ')');
  4521. }
  4522. equals(o) {
  4523. return o === this;
  4524. }
  4525. }
  4526. GenericGF.AZTEC_DATA_12 = new GenericGF(0x1069, 4096, 1); // x^12 + x^6 + x^5 + x^3 + 1
  4527. GenericGF.AZTEC_DATA_10 = new GenericGF(0x409, 1024, 1); // x^10 + x^3 + 1
  4528. GenericGF.AZTEC_DATA_6 = new GenericGF(0x43, 64, 1); // x^6 + x + 1
  4529. GenericGF.AZTEC_PARAM = new GenericGF(0x13, 16, 1); // x^4 + x + 1
  4530. GenericGF.QR_CODE_FIELD_256 = new GenericGF(0x011d, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1
  4531. GenericGF.DATA_MATRIX_FIELD_256 = new GenericGF(0x012d, 256, 1); // x^8 + x^5 + x^3 + x^2 + 1
  4532. GenericGF.AZTEC_DATA_8 = GenericGF.DATA_MATRIX_FIELD_256;
  4533. GenericGF.MAXICODE_FIELD_64 = GenericGF.AZTEC_DATA_6;
  4534. /**
  4535. * Custom Error class of type Exception.
  4536. */
  4537. class ReedSolomonException extends Exception {
  4538. }
  4539. ReedSolomonException.kind = 'ReedSolomonException';
  4540. /**
  4541. * Custom Error class of type Exception.
  4542. */
  4543. class IllegalStateException extends Exception {
  4544. }
  4545. IllegalStateException.kind = 'IllegalStateException';
  4546. /*
  4547. * Copyright 2007 ZXing authors
  4548. *
  4549. * Licensed under the Apache License, Version 2.0 (the "License");
  4550. * you may not use this file except in compliance with the License.
  4551. * You may obtain a copy of the License at
  4552. *
  4553. * http://www.apache.org/licenses/LICENSE-2.0
  4554. *
  4555. * Unless required by applicable law or agreed to in writing, software
  4556. * distributed under the License is distributed on an "AS IS" BASIS,
  4557. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4558. * See the License for the specific language governing permissions and
  4559. * limitations under the License.
  4560. */
  4561. /**
  4562. * <p>Implements Reed-Solomon decoding, as the name implies.</p>
  4563. *
  4564. * <p>The algorithm will not be explained here, but the following references were helpful
  4565. * in creating this implementation:</p>
  4566. *
  4567. * <ul>
  4568. * <li>Bruce Maggs.
  4569. * <a href="http://www.cs.cmu.edu/afs/cs.cmu.edu/project/pscico-guyb/realworld/www/rs_decode.ps">
  4570. * "Decoding Reed-Solomon Codes"</a> (see discussion of Forney's Formula)</li>
  4571. * <li>J.I. Hall. <a href="www.mth.msu.edu/~jhall/classes/codenotes/GRS.pdf">
  4572. * "Chapter 5. Generalized Reed-Solomon Codes"</a>
  4573. * (see discussion of Euclidean algorithm)</li>
  4574. * </ul>
  4575. *
  4576. * <p>Much credit is due to William Rucklidge since portions of this code are an indirect
  4577. * port of his C++ Reed-Solomon implementation.</p>
  4578. *
  4579. * @author Sean Owen
  4580. * @author William Rucklidge
  4581. * @author sanfordsquires
  4582. */
  4583. class ReedSolomonDecoder {
  4584. constructor(field) {
  4585. this.field = field;
  4586. }
  4587. /**
  4588. * <p>Decodes given set of received codewords, which include both data and error-correction
  4589. * codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place,
  4590. * in the input.</p>
  4591. *
  4592. * @param received data and error-correction codewords
  4593. * @param twoS number of error-correction codewords available
  4594. * @throws ReedSolomonException if decoding fails for any reason
  4595. */
  4596. decode(received, twoS /*int*/) {
  4597. const field = this.field;
  4598. const poly = new GenericGFPoly(field, received);
  4599. const syndromeCoefficients = new Int32Array(twoS);
  4600. let noError = true;
  4601. for (let i = 0; i < twoS; i++) {
  4602. const evalResult = poly.evaluateAt(field.exp(i + field.getGeneratorBase()));
  4603. syndromeCoefficients[syndromeCoefficients.length - 1 - i] = evalResult;
  4604. if (evalResult !== 0) {
  4605. noError = false;
  4606. }
  4607. }
  4608. if (noError) {
  4609. return;
  4610. }
  4611. const syndrome = new GenericGFPoly(field, syndromeCoefficients);
  4612. const sigmaOmega = this.runEuclideanAlgorithm(field.buildMonomial(twoS, 1), syndrome, twoS);
  4613. const sigma = sigmaOmega[0];
  4614. const omega = sigmaOmega[1];
  4615. const errorLocations = this.findErrorLocations(sigma);
  4616. const errorMagnitudes = this.findErrorMagnitudes(omega, errorLocations);
  4617. for (let i = 0; i < errorLocations.length; i++) {
  4618. const position = received.length - 1 - field.log(errorLocations[i]);
  4619. if (position < 0) {
  4620. throw new ReedSolomonException('Bad error location');
  4621. }
  4622. received[position] = GenericGF.addOrSubtract(received[position], errorMagnitudes[i]);
  4623. }
  4624. }
  4625. runEuclideanAlgorithm(a, b, R /*int*/) {
  4626. // Assume a's degree is >= b's
  4627. if (a.getDegree() < b.getDegree()) {
  4628. const temp = a;
  4629. a = b;
  4630. b = temp;
  4631. }
  4632. const field = this.field;
  4633. let rLast = a;
  4634. let r = b;
  4635. let tLast = field.getZero();
  4636. let t = field.getOne();
  4637. // Run Euclidean algorithm until r's degree is less than R/2
  4638. while (r.getDegree() >= (R / 2 | 0)) {
  4639. let rLastLast = rLast;
  4640. let tLastLast = tLast;
  4641. rLast = r;
  4642. tLast = t;
  4643. // Divide rLastLast by rLast, with quotient in q and remainder in r
  4644. if (rLast.isZero()) {
  4645. // Oops, Euclidean algorithm already terminated?
  4646. throw new ReedSolomonException('r_{i-1} was zero');
  4647. }
  4648. r = rLastLast;
  4649. let q = field.getZero();
  4650. const denominatorLeadingTerm = rLast.getCoefficient(rLast.getDegree());
  4651. const dltInverse = field.inverse(denominatorLeadingTerm);
  4652. while (r.getDegree() >= rLast.getDegree() && !r.isZero()) {
  4653. const degreeDiff = r.getDegree() - rLast.getDegree();
  4654. const scale = field.multiply(r.getCoefficient(r.getDegree()), dltInverse);
  4655. q = q.addOrSubtract(field.buildMonomial(degreeDiff, scale));
  4656. r = r.addOrSubtract(rLast.multiplyByMonomial(degreeDiff, scale));
  4657. }
  4658. t = q.multiply(tLast).addOrSubtract(tLastLast);
  4659. if (r.getDegree() >= rLast.getDegree()) {
  4660. throw new IllegalStateException('Division algorithm failed to reduce polynomial?');
  4661. }
  4662. }
  4663. const sigmaTildeAtZero = t.getCoefficient(0);
  4664. if (sigmaTildeAtZero === 0) {
  4665. throw new ReedSolomonException('sigmaTilde(0) was zero');
  4666. }
  4667. const inverse = field.inverse(sigmaTildeAtZero);
  4668. const sigma = t.multiplyScalar(inverse);
  4669. const omega = r.multiplyScalar(inverse);
  4670. return [sigma, omega];
  4671. }
  4672. findErrorLocations(errorLocator) {
  4673. // This is a direct application of Chien's search
  4674. const numErrors = errorLocator.getDegree();
  4675. if (numErrors === 1) { // shortcut
  4676. return Int32Array.from([errorLocator.getCoefficient(1)]);
  4677. }
  4678. const result = new Int32Array(numErrors);
  4679. let e = 0;
  4680. const field = this.field;
  4681. for (let i = 1; i < field.getSize() && e < numErrors; i++) {
  4682. if (errorLocator.evaluateAt(i) === 0) {
  4683. result[e] = field.inverse(i);
  4684. e++;
  4685. }
  4686. }
  4687. if (e !== numErrors) {
  4688. throw new ReedSolomonException('Error locator degree does not match number of roots');
  4689. }
  4690. return result;
  4691. }
  4692. findErrorMagnitudes(errorEvaluator, errorLocations) {
  4693. // This is directly applying Forney's Formula
  4694. const s = errorLocations.length;
  4695. const result = new Int32Array(s);
  4696. const field = this.field;
  4697. for (let i = 0; i < s; i++) {
  4698. const xiInverse = field.inverse(errorLocations[i]);
  4699. let denominator = 1;
  4700. for (let j = 0; j < s; j++) {
  4701. if (i !== j) {
  4702. // denominator = field.multiply(denominator,
  4703. // GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse)))
  4704. // Above should work but fails on some Apple and Linux JDKs due to a Hotspot bug.
  4705. // Below is a funny-looking workaround from Steven Parkes
  4706. const term = field.multiply(errorLocations[j], xiInverse);
  4707. const termPlus1 = (term & 0x1) === 0 ? term | 1 : term & ~1;
  4708. denominator = field.multiply(denominator, termPlus1);
  4709. }
  4710. }
  4711. result[i] = field.multiply(errorEvaluator.evaluateAt(xiInverse), field.inverse(denominator));
  4712. if (field.getGeneratorBase() !== 0) {
  4713. result[i] = field.multiply(result[i], xiInverse);
  4714. }
  4715. }
  4716. return result;
  4717. }
  4718. }
  4719. /*
  4720. * Copyright 2010 ZXing authors
  4721. *
  4722. * Licensed under the Apache License, Version 2.0 (the "License");
  4723. * you may not use this file except in compliance with the License.
  4724. * You may obtain a copy of the License at
  4725. *
  4726. * http://www.apache.org/licenses/LICENSE-2.0
  4727. *
  4728. * Unless required by applicable law or agreed to in writing, software
  4729. * distributed under the License is distributed on an "AS IS" BASIS,
  4730. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4731. * See the License for the specific language governing permissions and
  4732. * limitations under the License.
  4733. */
  4734. // import java.util.Arrays;
  4735. var Table;
  4736. (function (Table) {
  4737. Table[Table["UPPER"] = 0] = "UPPER";
  4738. Table[Table["LOWER"] = 1] = "LOWER";
  4739. Table[Table["MIXED"] = 2] = "MIXED";
  4740. Table[Table["DIGIT"] = 3] = "DIGIT";
  4741. Table[Table["PUNCT"] = 4] = "PUNCT";
  4742. Table[Table["BINARY"] = 5] = "BINARY";
  4743. })(Table || (Table = {}));
  4744. /**
  4745. * <p>The main class which implements Aztec Code decoding -- as opposed to locating and extracting
  4746. * the Aztec Code from an image.</p>
  4747. *
  4748. * @author David Olivier
  4749. */
  4750. class Decoder$2 {
  4751. decode(detectorResult) {
  4752. this.ddata = detectorResult;
  4753. let matrix = detectorResult.getBits();
  4754. let rawbits = this.extractBits(matrix);
  4755. let correctedBits = this.correctBits(rawbits);
  4756. let rawBytes = Decoder$2.convertBoolArrayToByteArray(correctedBits);
  4757. let result = Decoder$2.getEncodedData(correctedBits);
  4758. let decoderResult = new DecoderResult(rawBytes, result, null, null);
  4759. decoderResult.setNumBits(correctedBits.length);
  4760. return decoderResult;
  4761. }
  4762. // This method is used for testing the high-level encoder
  4763. static highLevelDecode(correctedBits) {
  4764. return this.getEncodedData(correctedBits);
  4765. }
  4766. /**
  4767. * Gets the string encoded in the aztec code bits
  4768. *
  4769. * @return the decoded string
  4770. */
  4771. static getEncodedData(correctedBits) {
  4772. let endIndex = correctedBits.length;
  4773. let latchTable = Table.UPPER; // table most recently latched to
  4774. let shiftTable = Table.UPPER; // table to use for the next read
  4775. let result = '';
  4776. let index = 0;
  4777. while (index < endIndex) {
  4778. if (shiftTable === Table.BINARY) {
  4779. if (endIndex - index < 5) {
  4780. break;
  4781. }
  4782. let length = Decoder$2.readCode(correctedBits, index, 5);
  4783. index += 5;
  4784. if (length === 0) {
  4785. if (endIndex - index < 11) {
  4786. break;
  4787. }
  4788. length = Decoder$2.readCode(correctedBits, index, 11) + 31;
  4789. index += 11;
  4790. }
  4791. for (let charCount = 0; charCount < length; charCount++) {
  4792. if (endIndex - index < 8) {
  4793. index = endIndex; // Force outer loop to exit
  4794. break;
  4795. }
  4796. const code = Decoder$2.readCode(correctedBits, index, 8);
  4797. result += /*(char)*/ StringUtils.castAsNonUtf8Char(code);
  4798. index += 8;
  4799. }
  4800. // Go back to whatever mode we had been in
  4801. shiftTable = latchTable;
  4802. }
  4803. else {
  4804. let size = shiftTable === Table.DIGIT ? 4 : 5;
  4805. if (endIndex - index < size) {
  4806. break;
  4807. }
  4808. let code = Decoder$2.readCode(correctedBits, index, size);
  4809. index += size;
  4810. let str = Decoder$2.getCharacter(shiftTable, code);
  4811. if (str.startsWith('CTRL_')) {
  4812. // Table changes
  4813. // ISO/IEC 24778:2008 prescribes ending a shift sequence in the mode from which it was invoked.
  4814. // That's including when that mode is a shift.
  4815. // Our test case dlusbs.png for issue #642 exercises that.
  4816. latchTable = shiftTable; // Latch the current mode, so as to return to Upper after U/S B/S
  4817. shiftTable = Decoder$2.getTable(str.charAt(5));
  4818. if (str.charAt(6) === 'L') {
  4819. latchTable = shiftTable;
  4820. }
  4821. }
  4822. else {
  4823. result += str;
  4824. // Go back to whatever mode we had been in
  4825. shiftTable = latchTable;
  4826. }
  4827. }
  4828. }
  4829. return result;
  4830. }
  4831. /**
  4832. * gets the table corresponding to the char passed
  4833. */
  4834. static getTable(t) {
  4835. switch (t) {
  4836. case 'L':
  4837. return Table.LOWER;
  4838. case 'P':
  4839. return Table.PUNCT;
  4840. case 'M':
  4841. return Table.MIXED;
  4842. case 'D':
  4843. return Table.DIGIT;
  4844. case 'B':
  4845. return Table.BINARY;
  4846. case 'U':
  4847. default:
  4848. return Table.UPPER;
  4849. }
  4850. }
  4851. /**
  4852. * Gets the character (or string) corresponding to the passed code in the given table
  4853. *
  4854. * @param table the table used
  4855. * @param code the code of the character
  4856. */
  4857. static getCharacter(table, code) {
  4858. switch (table) {
  4859. case Table.UPPER:
  4860. return Decoder$2.UPPER_TABLE[code];
  4861. case Table.LOWER:
  4862. return Decoder$2.LOWER_TABLE[code];
  4863. case Table.MIXED:
  4864. return Decoder$2.MIXED_TABLE[code];
  4865. case Table.PUNCT:
  4866. return Decoder$2.PUNCT_TABLE[code];
  4867. case Table.DIGIT:
  4868. return Decoder$2.DIGIT_TABLE[code];
  4869. default:
  4870. // Should not reach here.
  4871. throw new IllegalStateException('Bad table');
  4872. }
  4873. }
  4874. /**
  4875. * <p>Performs RS error correction on an array of bits.</p>
  4876. *
  4877. * @return the corrected array
  4878. * @throws FormatException if the input contains too many errors
  4879. */
  4880. correctBits(rawbits) {
  4881. let gf;
  4882. let codewordSize;
  4883. if (this.ddata.getNbLayers() <= 2) {
  4884. codewordSize = 6;
  4885. gf = GenericGF.AZTEC_DATA_6;
  4886. }
  4887. else if (this.ddata.getNbLayers() <= 8) {
  4888. codewordSize = 8;
  4889. gf = GenericGF.AZTEC_DATA_8;
  4890. }
  4891. else if (this.ddata.getNbLayers() <= 22) {
  4892. codewordSize = 10;
  4893. gf = GenericGF.AZTEC_DATA_10;
  4894. }
  4895. else {
  4896. codewordSize = 12;
  4897. gf = GenericGF.AZTEC_DATA_12;
  4898. }
  4899. let numDataCodewords = this.ddata.getNbDatablocks();
  4900. let numCodewords = rawbits.length / codewordSize;
  4901. if (numCodewords < numDataCodewords) {
  4902. throw new FormatException();
  4903. }
  4904. let offset = rawbits.length % codewordSize;
  4905. let dataWords = new Int32Array(numCodewords);
  4906. for (let i = 0; i < numCodewords; i++, offset += codewordSize) {
  4907. dataWords[i] = Decoder$2.readCode(rawbits, offset, codewordSize);
  4908. }
  4909. try {
  4910. let rsDecoder = new ReedSolomonDecoder(gf);
  4911. rsDecoder.decode(dataWords, numCodewords - numDataCodewords);
  4912. }
  4913. catch (ex) {
  4914. throw new FormatException(ex);
  4915. }
  4916. // Now perform the unstuffing operation.
  4917. // First, count how many bits are going to be thrown out as stuffing
  4918. let mask = (1 << codewordSize) - 1;
  4919. let stuffedBits = 0;
  4920. for (let i = 0; i < numDataCodewords; i++) {
  4921. let dataWord = dataWords[i];
  4922. if (dataWord === 0 || dataWord === mask) {
  4923. throw new FormatException();
  4924. }
  4925. else if (dataWord === 1 || dataWord === mask - 1) {
  4926. stuffedBits++;
  4927. }
  4928. }
  4929. // Now, actually unpack the bits and remove the stuffing
  4930. let correctedBits = new Array(numDataCodewords * codewordSize - stuffedBits);
  4931. let index = 0;
  4932. for (let i = 0; i < numDataCodewords; i++) {
  4933. let dataWord = dataWords[i];
  4934. if (dataWord === 1 || dataWord === mask - 1) {
  4935. // next codewordSize-1 bits are all zeros or all ones
  4936. correctedBits.fill(dataWord > 1, index, index + codewordSize - 1);
  4937. // Arrays.fill(correctedBits, index, index + codewordSize - 1, dataWord > 1);
  4938. index += codewordSize - 1;
  4939. }
  4940. else {
  4941. for (let bit = codewordSize - 1; bit >= 0; --bit) {
  4942. correctedBits[index++] = (dataWord & (1 << bit)) !== 0;
  4943. }
  4944. }
  4945. }
  4946. return correctedBits;
  4947. }
  4948. /**
  4949. * Gets the array of bits from an Aztec Code matrix
  4950. *
  4951. * @return the array of bits
  4952. */
  4953. extractBits(matrix) {
  4954. let compact = this.ddata.isCompact();
  4955. let layers = this.ddata.getNbLayers();
  4956. let baseMatrixSize = (compact ? 11 : 14) + layers * 4; // not including alignment lines
  4957. let alignmentMap = new Int32Array(baseMatrixSize);
  4958. let rawbits = new Array(this.totalBitsInLayer(layers, compact));
  4959. if (compact) {
  4960. for (let i = 0; i < alignmentMap.length; i++) {
  4961. alignmentMap[i] = i;
  4962. }
  4963. }
  4964. else {
  4965. let matrixSize = baseMatrixSize + 1 + 2 * Integer.truncDivision((Integer.truncDivision(baseMatrixSize, 2) - 1), 15);
  4966. let origCenter = baseMatrixSize / 2;
  4967. let center = Integer.truncDivision(matrixSize, 2);
  4968. for (let i = 0; i < origCenter; i++) {
  4969. let newOffset = i + Integer.truncDivision(i, 15);
  4970. alignmentMap[origCenter - i - 1] = center - newOffset - 1;
  4971. alignmentMap[origCenter + i] = center + newOffset + 1;
  4972. }
  4973. }
  4974. for (let i = 0, rowOffset = 0; i < layers; i++) {
  4975. let rowSize = (layers - i) * 4 + (compact ? 9 : 12);
  4976. // The top-left most point of this layer is <low, low> (not including alignment lines)
  4977. let low = i * 2;
  4978. // The bottom-right most point of this layer is <high, high> (not including alignment lines)
  4979. let high = baseMatrixSize - 1 - low;
  4980. // We pull bits from the two 2 x rowSize columns and two rowSize x 2 rows
  4981. for (let j = 0; j < rowSize; j++) {
  4982. let columnOffset = j * 2;
  4983. for (let k = 0; k < 2; k++) {
  4984. // left column
  4985. rawbits[rowOffset + columnOffset + k] =
  4986. matrix.get(alignmentMap[low + k], alignmentMap[low + j]);
  4987. // bottom row
  4988. rawbits[rowOffset + 2 * rowSize + columnOffset + k] =
  4989. matrix.get(alignmentMap[low + j], alignmentMap[high - k]);
  4990. // right column
  4991. rawbits[rowOffset + 4 * rowSize + columnOffset + k] =
  4992. matrix.get(alignmentMap[high - k], alignmentMap[high - j]);
  4993. // top row
  4994. rawbits[rowOffset + 6 * rowSize + columnOffset + k] =
  4995. matrix.get(alignmentMap[high - j], alignmentMap[low + k]);
  4996. }
  4997. }
  4998. rowOffset += rowSize * 8;
  4999. }
  5000. return rawbits;
  5001. }
  5002. /**
  5003. * Reads a code of given length and at given index in an array of bits
  5004. */
  5005. static readCode(rawbits, startIndex, length) {
  5006. let res = 0;
  5007. for (let i = startIndex; i < startIndex + length; i++) {
  5008. res <<= 1;
  5009. if (rawbits[i]) {
  5010. res |= 0x01;
  5011. }
  5012. }
  5013. return res;
  5014. }
  5015. /**
  5016. * Reads a code of length 8 in an array of bits, padding with zeros
  5017. */
  5018. static readByte(rawbits, startIndex) {
  5019. let n = rawbits.length - startIndex;
  5020. if (n >= 8) {
  5021. return Decoder$2.readCode(rawbits, startIndex, 8);
  5022. }
  5023. return Decoder$2.readCode(rawbits, startIndex, n) << (8 - n);
  5024. }
  5025. /**
  5026. * Packs a bit array into bytes, most significant bit first
  5027. */
  5028. static convertBoolArrayToByteArray(boolArr) {
  5029. let byteArr = new Uint8Array((boolArr.length + 7) / 8);
  5030. for (let i = 0; i < byteArr.length; i++) {
  5031. byteArr[i] = Decoder$2.readByte(boolArr, 8 * i);
  5032. }
  5033. return byteArr;
  5034. }
  5035. totalBitsInLayer(layers, compact) {
  5036. return ((compact ? 88 : 112) + 16 * layers) * layers;
  5037. }
  5038. }
  5039. Decoder$2.UPPER_TABLE = [
  5040. 'CTRL_PS', ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  5041. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'CTRL_LL', 'CTRL_ML', 'CTRL_DL', 'CTRL_BS'
  5042. ];
  5043. Decoder$2.LOWER_TABLE = [
  5044. 'CTRL_PS', ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
  5045. 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'CTRL_US', 'CTRL_ML', 'CTRL_DL', 'CTRL_BS'
  5046. ];
  5047. Decoder$2.MIXED_TABLE = [
  5048. 'CTRL_PS', ' ', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\b', '\t', '\n',
  5049. '\x0b', '\f', '\r', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', '@', '\\', '^', '_',
  5050. '`', '|', '~', '\x7f', 'CTRL_LL', 'CTRL_UL', 'CTRL_PL', 'CTRL_BS'
  5051. ];
  5052. Decoder$2.PUNCT_TABLE = [
  5053. '', '\r', '\r\n', '. ', ', ', ': ', '!', '"', '#', '$', '%', '&', '\'', '(', ')',
  5054. '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '[', ']', '{', '}', 'CTRL_UL'
  5055. ];
  5056. Decoder$2.DIGIT_TABLE = [
  5057. 'CTRL_PS', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '.', 'CTRL_UL', 'CTRL_US'
  5058. ];
  5059. /*
  5060. * Copyright 2012 ZXing authors
  5061. *
  5062. * Licensed under the Apache License, Version 2.0 (the "License");
  5063. * you may not use this file except in compliance with the License.
  5064. * You may obtain a copy of the License at
  5065. *
  5066. * http://www.apache.org/licenses/LICENSE-2.0
  5067. *
  5068. * Unless required by applicable law or agreed to in writing, software
  5069. * distributed under the License is distributed on an "AS IS" BASIS,
  5070. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5071. * See the License for the specific language governing permissions and
  5072. * limitations under the License.
  5073. */
  5074. /*namespace com.google.zxing.common.detector {*/
  5075. /**
  5076. * General math-related and numeric utility functions.
  5077. */
  5078. class MathUtils {
  5079. constructor() { }
  5080. /**
  5081. * Ends up being a bit faster than {@link Math#round(float)}. This merely rounds its
  5082. * argument to the nearest int, where x.5 rounds up to x+1. Semantics of this shortcut
  5083. * differ slightly from {@link Math#round(float)} in that half rounds down for negative
  5084. * values. -2.5 rounds to -3, not -2. For purposes here it makes no difference.
  5085. *
  5086. * @param d real value to round
  5087. * @return nearest {@code int}
  5088. */
  5089. static round(d /*float*/) {
  5090. if (isNaN(d))
  5091. return 0;
  5092. if (d <= Number.MIN_SAFE_INTEGER)
  5093. return Number.MIN_SAFE_INTEGER;
  5094. if (d >= Number.MAX_SAFE_INTEGER)
  5095. return Number.MAX_SAFE_INTEGER;
  5096. return /*(int) */ (d + (d < 0.0 ? -0.5 : 0.5)) | 0;
  5097. }
  5098. // TYPESCRIPTPORT: maybe remove round method and call directly Math.round, it looks like it doesn't make sense for js
  5099. /**
  5100. * @param aX point A x coordinate
  5101. * @param aY point A y coordinate
  5102. * @param bX point B x coordinate
  5103. * @param bY point B y coordinate
  5104. * @return Euclidean distance between points A and B
  5105. */
  5106. static distance(aX /*float|int*/, aY /*float|int*/, bX /*float|int*/, bY /*float|int*/) {
  5107. const xDiff = aX - bX;
  5108. const yDiff = aY - bY;
  5109. return /*(float) */ Math.sqrt(xDiff * xDiff + yDiff * yDiff);
  5110. }
  5111. /**
  5112. * @param aX point A x coordinate
  5113. * @param aY point A y coordinate
  5114. * @param bX point B x coordinate
  5115. * @param bY point B y coordinate
  5116. * @return Euclidean distance between points A and B
  5117. */
  5118. // public static distance(aX: number /*int*/, aY: number /*int*/, bX: number /*int*/, bY: number /*int*/): float {
  5119. // const xDiff = aX - bX
  5120. // const yDiff = aY - bY
  5121. // return (float) Math.sqrt(xDiff * xDiff + yDiff * yDiff);
  5122. // }
  5123. /**
  5124. * @param array values to sum
  5125. * @return sum of values in array
  5126. */
  5127. static sum(array) {
  5128. let count = 0;
  5129. for (let i = 0, length = array.length; i !== length; i++) {
  5130. const a = array[i];
  5131. count += a;
  5132. }
  5133. return count;
  5134. }
  5135. }
  5136. /**
  5137. * Ponyfill for Java's Float class.
  5138. */
  5139. class Float {
  5140. /**
  5141. * SincTS has no difference between int and float, there's all numbers,
  5142. * this is used only to polyfill Java code.
  5143. */
  5144. static floatToIntBits(f) {
  5145. return f;
  5146. }
  5147. }
  5148. /**
  5149. * The float max value in JS is the number max value.
  5150. */
  5151. Float.MAX_VALUE = Number.MAX_SAFE_INTEGER;
  5152. /*
  5153. * Copyright 2007 ZXing authors
  5154. *
  5155. * Licensed under the Apache License, Version 2.0 (the "License");
  5156. * you may not use this file except in compliance with the License.
  5157. * You may obtain a copy of the License at
  5158. *
  5159. * http://www.apache.org/licenses/LICENSE-2.0
  5160. *
  5161. * Unless required by applicable law or agreed to in writing, software
  5162. * distributed under the License is distributed on an "AS IS" BASIS,
  5163. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5164. * See the License for the specific language governing permissions and
  5165. * limitations under the License.
  5166. */
  5167. /**
  5168. * <p>Encapsulates a point of interest in an image containing a barcode. Typically, this
  5169. * would be the location of a finder pattern or the corner of the barcode, for example.</p>
  5170. *
  5171. * @author Sean Owen
  5172. */
  5173. class ResultPoint {
  5174. constructor(x, y) {
  5175. this.x = x;
  5176. this.y = y;
  5177. }
  5178. getX() {
  5179. return this.x;
  5180. }
  5181. getY() {
  5182. return this.y;
  5183. }
  5184. /*@Override*/
  5185. equals(other) {
  5186. if (other instanceof ResultPoint) {
  5187. const otherPoint = other;
  5188. return this.x === otherPoint.x && this.y === otherPoint.y;
  5189. }
  5190. return false;
  5191. }
  5192. /*@Override*/
  5193. hashCode() {
  5194. return 31 * Float.floatToIntBits(this.x) + Float.floatToIntBits(this.y);
  5195. }
  5196. /*@Override*/
  5197. toString() {
  5198. return '(' + this.x + ',' + this.y + ')';
  5199. }
  5200. /**
  5201. * Orders an array of three ResultPoints in an order [A,B,C] such that AB is less than AC
  5202. * and BC is less than AC, and the angle between BC and BA is less than 180 degrees.
  5203. *
  5204. * @param patterns array of three {@code ResultPoint} to order
  5205. */
  5206. static orderBestPatterns(patterns) {
  5207. // Find distances between pattern centers
  5208. const zeroOneDistance = this.distance(patterns[0], patterns[1]);
  5209. const oneTwoDistance = this.distance(patterns[1], patterns[2]);
  5210. const zeroTwoDistance = this.distance(patterns[0], patterns[2]);
  5211. let pointA;
  5212. let pointB;
  5213. let pointC;
  5214. // Assume one closest to other two is B; A and C will just be guesses at first
  5215. if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance) {
  5216. pointB = patterns[0];
  5217. pointA = patterns[1];
  5218. pointC = patterns[2];
  5219. }
  5220. else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance) {
  5221. pointB = patterns[1];
  5222. pointA = patterns[0];
  5223. pointC = patterns[2];
  5224. }
  5225. else {
  5226. pointB = patterns[2];
  5227. pointA = patterns[0];
  5228. pointC = patterns[1];
  5229. }
  5230. // Use cross product to figure out whether A and C are correct or flipped.
  5231. // This asks whether BC x BA has a positive z component, which is the arrangement
  5232. // we want for A, B, C. If it's negative, then we've got it flipped around and
  5233. // should swap A and C.
  5234. if (this.crossProductZ(pointA, pointB, pointC) < 0.0) {
  5235. const temp = pointA;
  5236. pointA = pointC;
  5237. pointC = temp;
  5238. }
  5239. patterns[0] = pointA;
  5240. patterns[1] = pointB;
  5241. patterns[2] = pointC;
  5242. }
  5243. /**
  5244. * @param pattern1 first pattern
  5245. * @param pattern2 second pattern
  5246. * @return distance between two points
  5247. */
  5248. static distance(pattern1, pattern2) {
  5249. return MathUtils.distance(pattern1.x, pattern1.y, pattern2.x, pattern2.y);
  5250. }
  5251. /**
  5252. * Returns the z component of the cross product between vectors BC and BA.
  5253. */
  5254. static crossProductZ(pointA, pointB, pointC) {
  5255. const bX = pointB.x;
  5256. const bY = pointB.y;
  5257. return ((pointC.x - bX) * (pointA.y - bY)) - ((pointC.y - bY) * (pointA.x - bX));
  5258. }
  5259. }
  5260. /*
  5261. * Copyright 2007 ZXing authors
  5262. *
  5263. * Licensed under the Apache License, Version 2.0 (the "License");
  5264. * you may not use this file except in compliance with the License.
  5265. * You may obtain a copy of the License at
  5266. *
  5267. * http://www.apache.org/licenses/LICENSE-2.0
  5268. *
  5269. * Unless required by applicable law or agreed to in writing, software
  5270. * distributed under the License is distributed on an "AS IS" BASIS,
  5271. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5272. * See the License for the specific language governing permissions and
  5273. * limitations under the License.
  5274. */
  5275. /**
  5276. * <p>Encapsulates the result of detecting a barcode in an image. This includes the raw
  5277. * matrix of black/white pixels corresponding to the barcode, and possibly points of interest
  5278. * in the image, like the location of finder patterns or corners of the barcode in the image.</p>
  5279. *
  5280. * @author Sean Owen
  5281. */
  5282. class DetectorResult {
  5283. constructor(bits, points) {
  5284. this.bits = bits;
  5285. this.points = points;
  5286. }
  5287. getBits() {
  5288. return this.bits;
  5289. }
  5290. getPoints() {
  5291. return this.points;
  5292. }
  5293. }
  5294. /*
  5295. * Copyright 2010 ZXing authors
  5296. *
  5297. * Licensed under the Apache License, Version 2.0 (the "License");
  5298. * you may not use this file except in compliance with the License.
  5299. * You may obtain a copy of the License at
  5300. *
  5301. * http://www.apache.org/licenses/LICENSE-2.0
  5302. *
  5303. * Unless required by applicable law or agreed to in writing, software
  5304. * distributed under the License is distributed on an "AS IS" BASIS,
  5305. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5306. * See the License for the specific language governing permissions and
  5307. * limitations under the License.
  5308. */
  5309. /**
  5310. * <p>Extends {@link DetectorResult} with more information specific to the Aztec format,
  5311. * like the number of layers and whether it's compact.</p>
  5312. *
  5313. * @author Sean Owen
  5314. */
  5315. class AztecDetectorResult extends DetectorResult {
  5316. constructor(bits, points, compact, nbDatablocks, nbLayers) {
  5317. super(bits, points);
  5318. this.compact = compact;
  5319. this.nbDatablocks = nbDatablocks;
  5320. this.nbLayers = nbLayers;
  5321. }
  5322. getNbLayers() {
  5323. return this.nbLayers;
  5324. }
  5325. getNbDatablocks() {
  5326. return this.nbDatablocks;
  5327. }
  5328. isCompact() {
  5329. return this.compact;
  5330. }
  5331. }
  5332. /*
  5333. * Copyright 2010 ZXing authors
  5334. *
  5335. * Licensed under the Apache License, Version 2.0 (the "License");
  5336. * you may not use this file except in compliance with the License.
  5337. * You may obtain a copy of the License at
  5338. *
  5339. * http://www.apache.org/licenses/LICENSE-2.0
  5340. *
  5341. * Unless required by applicable law or agreed to in writing, software
  5342. * distributed under the License is distributed on an "AS IS" BASIS,
  5343. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5344. * See the License for the specific language governing permissions and
  5345. * limitations under the License.
  5346. */
  5347. /**
  5348. * <p>
  5349. * Detects a candidate barcode-like rectangular region within an image. It
  5350. * starts around the center of the image, increases the size of the candidate
  5351. * region until it finds a white rectangular region. By keeping track of the
  5352. * last black points it encountered, it determines the corners of the barcode.
  5353. * </p>
  5354. *
  5355. * @author David Olivier
  5356. */
  5357. class WhiteRectangleDetector {
  5358. // public constructor(private image: BitMatrix) /*throws NotFoundException*/ {
  5359. // this(image, INIT_SIZE, image.getWidth() / 2, image.getHeight() / 2)
  5360. // }
  5361. /**
  5362. * @param image barcode image to find a rectangle in
  5363. * @param initSize initial size of search area around center
  5364. * @param x x position of search center
  5365. * @param y y position of search center
  5366. * @throws NotFoundException if image is too small to accommodate {@code initSize}
  5367. */
  5368. constructor(image, initSize /*int*/, x /*int*/, y /*int*/) {
  5369. this.image = image;
  5370. this.height = image.getHeight();
  5371. this.width = image.getWidth();
  5372. if (undefined === initSize || null === initSize) {
  5373. initSize = WhiteRectangleDetector.INIT_SIZE;
  5374. }
  5375. if (undefined === x || null === x) {
  5376. x = image.getWidth() / 2 | 0;
  5377. }
  5378. if (undefined === y || null === y) {
  5379. y = image.getHeight() / 2 | 0;
  5380. }
  5381. const halfsize = initSize / 2 | 0;
  5382. this.leftInit = x - halfsize;
  5383. this.rightInit = x + halfsize;
  5384. this.upInit = y - halfsize;
  5385. this.downInit = y + halfsize;
  5386. if (this.upInit < 0 || this.leftInit < 0 || this.downInit >= this.height || this.rightInit >= this.width) {
  5387. throw new NotFoundException();
  5388. }
  5389. }
  5390. /**
  5391. * <p>
  5392. * Detects a candidate barcode-like rectangular region within an image. It
  5393. * starts around the center of the image, increases the size of the candidate
  5394. * region until it finds a white rectangular region.
  5395. * </p>
  5396. *
  5397. * @return {@link ResultPoint}[] describing the corners of the rectangular
  5398. * region. The first and last points are opposed on the diagonal, as
  5399. * are the second and third. The first point will be the topmost
  5400. * point and the last, the bottommost. The second point will be
  5401. * leftmost and the third, the rightmost
  5402. * @throws NotFoundException if no Data Matrix Code can be found
  5403. */
  5404. detect() {
  5405. let left = this.leftInit;
  5406. let right = this.rightInit;
  5407. let up = this.upInit;
  5408. let down = this.downInit;
  5409. let sizeExceeded = false;
  5410. let aBlackPointFoundOnBorder = true;
  5411. let atLeastOneBlackPointFoundOnBorder = false;
  5412. let atLeastOneBlackPointFoundOnRight = false;
  5413. let atLeastOneBlackPointFoundOnBottom = false;
  5414. let atLeastOneBlackPointFoundOnLeft = false;
  5415. let atLeastOneBlackPointFoundOnTop = false;
  5416. const width = this.width;
  5417. const height = this.height;
  5418. while (aBlackPointFoundOnBorder) {
  5419. aBlackPointFoundOnBorder = false;
  5420. // .....
  5421. // . |
  5422. // .....
  5423. let rightBorderNotWhite = true;
  5424. while ((rightBorderNotWhite || !atLeastOneBlackPointFoundOnRight) && right < width) {
  5425. rightBorderNotWhite = this.containsBlackPoint(up, down, right, false);
  5426. if (rightBorderNotWhite) {
  5427. right++;
  5428. aBlackPointFoundOnBorder = true;
  5429. atLeastOneBlackPointFoundOnRight = true;
  5430. }
  5431. else if (!atLeastOneBlackPointFoundOnRight) {
  5432. right++;
  5433. }
  5434. }
  5435. if (right >= width) {
  5436. sizeExceeded = true;
  5437. break;
  5438. }
  5439. // .....
  5440. // . .
  5441. // .___.
  5442. let bottomBorderNotWhite = true;
  5443. while ((bottomBorderNotWhite || !atLeastOneBlackPointFoundOnBottom) && down < height) {
  5444. bottomBorderNotWhite = this.containsBlackPoint(left, right, down, true);
  5445. if (bottomBorderNotWhite) {
  5446. down++;
  5447. aBlackPointFoundOnBorder = true;
  5448. atLeastOneBlackPointFoundOnBottom = true;
  5449. }
  5450. else if (!atLeastOneBlackPointFoundOnBottom) {
  5451. down++;
  5452. }
  5453. }
  5454. if (down >= height) {
  5455. sizeExceeded = true;
  5456. break;
  5457. }
  5458. // .....
  5459. // | .
  5460. // .....
  5461. let leftBorderNotWhite = true;
  5462. while ((leftBorderNotWhite || !atLeastOneBlackPointFoundOnLeft) && left >= 0) {
  5463. leftBorderNotWhite = this.containsBlackPoint(up, down, left, false);
  5464. if (leftBorderNotWhite) {
  5465. left--;
  5466. aBlackPointFoundOnBorder = true;
  5467. atLeastOneBlackPointFoundOnLeft = true;
  5468. }
  5469. else if (!atLeastOneBlackPointFoundOnLeft) {
  5470. left--;
  5471. }
  5472. }
  5473. if (left < 0) {
  5474. sizeExceeded = true;
  5475. break;
  5476. }
  5477. // .___.
  5478. // . .
  5479. // .....
  5480. let topBorderNotWhite = true;
  5481. while ((topBorderNotWhite || !atLeastOneBlackPointFoundOnTop) && up >= 0) {
  5482. topBorderNotWhite = this.containsBlackPoint(left, right, up, true);
  5483. if (topBorderNotWhite) {
  5484. up--;
  5485. aBlackPointFoundOnBorder = true;
  5486. atLeastOneBlackPointFoundOnTop = true;
  5487. }
  5488. else if (!atLeastOneBlackPointFoundOnTop) {
  5489. up--;
  5490. }
  5491. }
  5492. if (up < 0) {
  5493. sizeExceeded = true;
  5494. break;
  5495. }
  5496. if (aBlackPointFoundOnBorder) {
  5497. atLeastOneBlackPointFoundOnBorder = true;
  5498. }
  5499. }
  5500. if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) {
  5501. const maxSize = right - left;
  5502. let z = null;
  5503. for (let i = 1; z === null && i < maxSize; i++) {
  5504. z = this.getBlackPointOnSegment(left, down - i, left + i, down);
  5505. }
  5506. if (z == null) {
  5507. throw new NotFoundException();
  5508. }
  5509. let t = null;
  5510. // go down right
  5511. for (let i = 1; t === null && i < maxSize; i++) {
  5512. t = this.getBlackPointOnSegment(left, up + i, left + i, up);
  5513. }
  5514. if (t == null) {
  5515. throw new NotFoundException();
  5516. }
  5517. let x = null;
  5518. // go down left
  5519. for (let i = 1; x === null && i < maxSize; i++) {
  5520. x = this.getBlackPointOnSegment(right, up + i, right - i, up);
  5521. }
  5522. if (x == null) {
  5523. throw new NotFoundException();
  5524. }
  5525. let y = null;
  5526. // go up left
  5527. for (let i = 1; y === null && i < maxSize; i++) {
  5528. y = this.getBlackPointOnSegment(right, down - i, right - i, down);
  5529. }
  5530. if (y == null) {
  5531. throw new NotFoundException();
  5532. }
  5533. return this.centerEdges(y, z, x, t);
  5534. }
  5535. else {
  5536. throw new NotFoundException();
  5537. }
  5538. }
  5539. getBlackPointOnSegment(aX /*float*/, aY /*float*/, bX /*float*/, bY /*float*/) {
  5540. const dist = MathUtils.round(MathUtils.distance(aX, aY, bX, bY));
  5541. const xStep = (bX - aX) / dist;
  5542. const yStep = (bY - aY) / dist;
  5543. const image = this.image;
  5544. for (let i = 0; i < dist; i++) {
  5545. const x = MathUtils.round(aX + i * xStep);
  5546. const y = MathUtils.round(aY + i * yStep);
  5547. if (image.get(x, y)) {
  5548. return new ResultPoint(x, y);
  5549. }
  5550. }
  5551. return null;
  5552. }
  5553. /**
  5554. * recenters the points of a constant distance towards the center
  5555. *
  5556. * @param y bottom most point
  5557. * @param z left most point
  5558. * @param x right most point
  5559. * @param t top most point
  5560. * @return {@link ResultPoint}[] describing the corners of the rectangular
  5561. * region. The first and last points are opposed on the diagonal, as
  5562. * are the second and third. The first point will be the topmost
  5563. * point and the last, the bottommost. The second point will be
  5564. * leftmost and the third, the rightmost
  5565. */
  5566. centerEdges(y, z, x, t) {
  5567. //
  5568. // t t
  5569. // z x
  5570. // x OR z
  5571. // y y
  5572. //
  5573. const yi = y.getX();
  5574. const yj = y.getY();
  5575. const zi = z.getX();
  5576. const zj = z.getY();
  5577. const xi = x.getX();
  5578. const xj = x.getY();
  5579. const ti = t.getX();
  5580. const tj = t.getY();
  5581. const CORR = WhiteRectangleDetector.CORR;
  5582. if (yi < this.width / 2.0) {
  5583. return [
  5584. new ResultPoint(ti - CORR, tj + CORR),
  5585. new ResultPoint(zi + CORR, zj + CORR),
  5586. new ResultPoint(xi - CORR, xj - CORR),
  5587. new ResultPoint(yi + CORR, yj - CORR)
  5588. ];
  5589. }
  5590. else {
  5591. return [
  5592. new ResultPoint(ti + CORR, tj + CORR),
  5593. new ResultPoint(zi + CORR, zj - CORR),
  5594. new ResultPoint(xi - CORR, xj + CORR),
  5595. new ResultPoint(yi - CORR, yj - CORR)
  5596. ];
  5597. }
  5598. }
  5599. /**
  5600. * Determines whether a segment contains a black point
  5601. *
  5602. * @param a min value of the scanned coordinate
  5603. * @param b max value of the scanned coordinate
  5604. * @param fixed value of fixed coordinate
  5605. * @param horizontal set to true if scan must be horizontal, false if vertical
  5606. * @return true if a black point has been found, else false.
  5607. */
  5608. containsBlackPoint(a /*int*/, b /*int*/, fixed /*int*/, horizontal) {
  5609. const image = this.image;
  5610. if (horizontal) {
  5611. for (let x = a; x <= b; x++) {
  5612. if (image.get(x, fixed)) {
  5613. return true;
  5614. }
  5615. }
  5616. }
  5617. else {
  5618. for (let y = a; y <= b; y++) {
  5619. if (image.get(fixed, y)) {
  5620. return true;
  5621. }
  5622. }
  5623. }
  5624. return false;
  5625. }
  5626. }
  5627. WhiteRectangleDetector.INIT_SIZE = 10;
  5628. WhiteRectangleDetector.CORR = 1;
  5629. /*
  5630. * Copyright 2007 ZXing authors
  5631. *
  5632. * Licensed under the Apache License, Version 2.0 (the "License");
  5633. * you may not use this file except in compliance with the License.
  5634. * You may obtain a copy of the License at
  5635. *
  5636. * http://www.apache.org/licenses/LICENSE-2.0
  5637. *
  5638. * Unless required by applicable law or agreed to in writing, software
  5639. * distributed under the License is distributed on an "AS IS" BASIS,
  5640. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5641. * See the License for the specific language governing permissions and
  5642. * limitations under the License.
  5643. */
  5644. /**
  5645. * Implementations of this class can, given locations of finder patterns for a QR code in an
  5646. * image, sample the right points in the image to reconstruct the QR code, accounting for
  5647. * perspective distortion. It is abstracted since it is relatively expensive and should be allowed
  5648. * to take advantage of platform-specific optimized implementations, like Sun's Java Advanced
  5649. * Imaging library, but which may not be available in other environments such as J2ME, and vice
  5650. * versa.
  5651. *
  5652. * The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)}
  5653. * with an instance of a class which implements this interface.
  5654. *
  5655. * @author Sean Owen
  5656. */
  5657. class GridSampler {
  5658. /**
  5659. * <p>Checks a set of points that have been transformed to sample points on an image against
  5660. * the image's dimensions to see if the point are even within the image.</p>
  5661. *
  5662. * <p>This method will actually "nudge" the endpoints back onto the image if they are found to be
  5663. * barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder
  5664. * patterns in an image where the QR Code runs all the way to the image border.</p>
  5665. *
  5666. * <p>For efficiency, the method will check points from either end of the line until one is found
  5667. * to be within the image. Because the set of points are assumed to be linear, this is valid.</p>
  5668. *
  5669. * @param image image into which the points should map
  5670. * @param points actual points in x1,y1,...,xn,yn form
  5671. * @throws NotFoundException if an endpoint is lies outside the image boundaries
  5672. */
  5673. static checkAndNudgePoints(image, points) {
  5674. const width = image.getWidth();
  5675. const height = image.getHeight();
  5676. // Check and nudge points from start until we see some that are OK:
  5677. let nudged = true;
  5678. for (let offset = 0; offset < points.length && nudged; offset += 2) {
  5679. const x = Math.floor(points[offset]);
  5680. const y = Math.floor(points[offset + 1]);
  5681. if (x < -1 || x > width || y < -1 || y > height) {
  5682. throw new NotFoundException();
  5683. }
  5684. nudged = false;
  5685. if (x === -1) {
  5686. points[offset] = 0.0;
  5687. nudged = true;
  5688. }
  5689. else if (x === width) {
  5690. points[offset] = width - 1;
  5691. nudged = true;
  5692. }
  5693. if (y === -1) {
  5694. points[offset + 1] = 0.0;
  5695. nudged = true;
  5696. }
  5697. else if (y === height) {
  5698. points[offset + 1] = height - 1;
  5699. nudged = true;
  5700. }
  5701. }
  5702. // Check and nudge points from end:
  5703. nudged = true;
  5704. for (let offset = points.length - 2; offset >= 0 && nudged; offset -= 2) {
  5705. const x = Math.floor(points[offset]);
  5706. const y = Math.floor(points[offset + 1]);
  5707. if (x < -1 || x > width || y < -1 || y > height) {
  5708. throw new NotFoundException();
  5709. }
  5710. nudged = false;
  5711. if (x === -1) {
  5712. points[offset] = 0.0;
  5713. nudged = true;
  5714. }
  5715. else if (x === width) {
  5716. points[offset] = width - 1;
  5717. nudged = true;
  5718. }
  5719. if (y === -1) {
  5720. points[offset + 1] = 0.0;
  5721. nudged = true;
  5722. }
  5723. else if (y === height) {
  5724. points[offset + 1] = height - 1;
  5725. nudged = true;
  5726. }
  5727. }
  5728. }
  5729. }
  5730. /*
  5731. * Copyright 2007 ZXing authors
  5732. *
  5733. * Licensed under the Apache License, Version 2.0 (the "License");
  5734. * you may not use this file except in compliance with the License.
  5735. * You may obtain a copy of the License at
  5736. *
  5737. * http://www.apache.org/licenses/LICENSE-2.0
  5738. *
  5739. * Unless required by applicable law or agreed to in writing, software
  5740. * distributed under the License is distributed on an "AS IS" BASIS,
  5741. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5742. * See the License for the specific language governing permissions and
  5743. * limitations under the License.
  5744. */
  5745. /*namespace com.google.zxing.common {*/
  5746. /**
  5747. * <p>This class implements a perspective transform in two dimensions. Given four source and four
  5748. * destination points, it will compute the transformation implied between them. The code is based
  5749. * directly upon section 3.4.2 of George Wolberg's "Digital Image Warping"; see pages 54-56.</p>
  5750. *
  5751. * @author Sean Owen
  5752. */
  5753. class PerspectiveTransform {
  5754. constructor(a11 /*float*/, a21 /*float*/, a31 /*float*/, a12 /*float*/, a22 /*float*/, a32 /*float*/, a13 /*float*/, a23 /*float*/, a33 /*float*/) {
  5755. this.a11 = a11;
  5756. this.a21 = a21;
  5757. this.a31 = a31;
  5758. this.a12 = a12;
  5759. this.a22 = a22;
  5760. this.a32 = a32;
  5761. this.a13 = a13;
  5762. this.a23 = a23;
  5763. this.a33 = a33;
  5764. }
  5765. static quadrilateralToQuadrilateral(x0 /*float*/, y0 /*float*/, x1 /*float*/, y1 /*float*/, x2 /*float*/, y2 /*float*/, x3 /*float*/, y3 /*float*/, x0p /*float*/, y0p /*float*/, x1p /*float*/, y1p /*float*/, x2p /*float*/, y2p /*float*/, x3p /*float*/, y3p /*float*/) {
  5766. const qToS = PerspectiveTransform.quadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3);
  5767. const sToQ = PerspectiveTransform.squareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p);
  5768. return sToQ.times(qToS);
  5769. }
  5770. transformPoints(points) {
  5771. const max = points.length;
  5772. const a11 = this.a11;
  5773. const a12 = this.a12;
  5774. const a13 = this.a13;
  5775. const a21 = this.a21;
  5776. const a22 = this.a22;
  5777. const a23 = this.a23;
  5778. const a31 = this.a31;
  5779. const a32 = this.a32;
  5780. const a33 = this.a33;
  5781. for (let i = 0; i < max; i += 2) {
  5782. const x = points[i];
  5783. const y = points[i + 1];
  5784. const denominator = a13 * x + a23 * y + a33;
  5785. points[i] = (a11 * x + a21 * y + a31) / denominator;
  5786. points[i + 1] = (a12 * x + a22 * y + a32) / denominator;
  5787. }
  5788. }
  5789. transformPointsWithValues(xValues, yValues) {
  5790. const a11 = this.a11;
  5791. const a12 = this.a12;
  5792. const a13 = this.a13;
  5793. const a21 = this.a21;
  5794. const a22 = this.a22;
  5795. const a23 = this.a23;
  5796. const a31 = this.a31;
  5797. const a32 = this.a32;
  5798. const a33 = this.a33;
  5799. const n = xValues.length;
  5800. for (let i = 0; i < n; i++) {
  5801. const x = xValues[i];
  5802. const y = yValues[i];
  5803. const denominator = a13 * x + a23 * y + a33;
  5804. xValues[i] = (a11 * x + a21 * y + a31) / denominator;
  5805. yValues[i] = (a12 * x + a22 * y + a32) / denominator;
  5806. }
  5807. }
  5808. static squareToQuadrilateral(x0 /*float*/, y0 /*float*/, x1 /*float*/, y1 /*float*/, x2 /*float*/, y2 /*float*/, x3 /*float*/, y3 /*float*/) {
  5809. const dx3 = x0 - x1 + x2 - x3;
  5810. const dy3 = y0 - y1 + y2 - y3;
  5811. if (dx3 === 0.0 && dy3 === 0.0) {
  5812. // Affine
  5813. return new PerspectiveTransform(x1 - x0, x2 - x1, x0, y1 - y0, y2 - y1, y0, 0.0, 0.0, 1.0);
  5814. }
  5815. else {
  5816. const dx1 = x1 - x2;
  5817. const dx2 = x3 - x2;
  5818. const dy1 = y1 - y2;
  5819. const dy2 = y3 - y2;
  5820. const denominator = dx1 * dy2 - dx2 * dy1;
  5821. const a13 = (dx3 * dy2 - dx2 * dy3) / denominator;
  5822. const a23 = (dx1 * dy3 - dx3 * dy1) / denominator;
  5823. return new PerspectiveTransform(x1 - x0 + a13 * x1, x3 - x0 + a23 * x3, x0, y1 - y0 + a13 * y1, y3 - y0 + a23 * y3, y0, a13, a23, 1.0);
  5824. }
  5825. }
  5826. static quadrilateralToSquare(x0 /*float*/, y0 /*float*/, x1 /*float*/, y1 /*float*/, x2 /*float*/, y2 /*float*/, x3 /*float*/, y3 /*float*/) {
  5827. // Here, the adjoint serves as the inverse:
  5828. return PerspectiveTransform.squareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3).buildAdjoint();
  5829. }
  5830. buildAdjoint() {
  5831. // Adjoint is the transpose of the cofactor matrix:
  5832. return new PerspectiveTransform(this.a22 * this.a33 - this.a23 * this.a32, this.a23 * this.a31 - this.a21 * this.a33, this.a21 * this.a32 - this.a22 * this.a31, this.a13 * this.a32 - this.a12 * this.a33, this.a11 * this.a33 - this.a13 * this.a31, this.a12 * this.a31 - this.a11 * this.a32, this.a12 * this.a23 - this.a13 * this.a22, this.a13 * this.a21 - this.a11 * this.a23, this.a11 * this.a22 - this.a12 * this.a21);
  5833. }
  5834. times(other) {
  5835. return new PerspectiveTransform(this.a11 * other.a11 + this.a21 * other.a12 + this.a31 * other.a13, this.a11 * other.a21 + this.a21 * other.a22 + this.a31 * other.a23, this.a11 * other.a31 + this.a21 * other.a32 + this.a31 * other.a33, this.a12 * other.a11 + this.a22 * other.a12 + this.a32 * other.a13, this.a12 * other.a21 + this.a22 * other.a22 + this.a32 * other.a23, this.a12 * other.a31 + this.a22 * other.a32 + this.a32 * other.a33, this.a13 * other.a11 + this.a23 * other.a12 + this.a33 * other.a13, this.a13 * other.a21 + this.a23 * other.a22 + this.a33 * other.a23, this.a13 * other.a31 + this.a23 * other.a32 + this.a33 * other.a33);
  5836. }
  5837. }
  5838. /*
  5839. * Copyright 2007 ZXing authors
  5840. *
  5841. * Licensed under the Apache License, Version 2.0 (the "License");
  5842. * you may not use this file except in compliance with the License.
  5843. * You may obtain a copy of the License at
  5844. *
  5845. * http://www.apache.org/licenses/LICENSE-2.0
  5846. *
  5847. * Unless required by applicable law or agreed to in writing, software
  5848. * distributed under the License is distributed on an "AS IS" BASIS,
  5849. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5850. * See the License for the specific language governing permissions and
  5851. * limitations under the License.
  5852. */
  5853. /**
  5854. * @author Sean Owen
  5855. */
  5856. class DefaultGridSampler extends GridSampler {
  5857. /*@Override*/
  5858. sampleGrid(image, dimensionX /*int*/, dimensionY /*int*/, p1ToX /*float*/, p1ToY /*float*/, p2ToX /*float*/, p2ToY /*float*/, p3ToX /*float*/, p3ToY /*float*/, p4ToX /*float*/, p4ToY /*float*/, p1FromX /*float*/, p1FromY /*float*/, p2FromX /*float*/, p2FromY /*float*/, p3FromX /*float*/, p3FromY /*float*/, p4FromX /*float*/, p4FromY /*float*/) {
  5859. const transform = PerspectiveTransform.quadrilateralToQuadrilateral(p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY);
  5860. return this.sampleGridWithTransform(image, dimensionX, dimensionY, transform);
  5861. }
  5862. /*@Override*/
  5863. sampleGridWithTransform(image, dimensionX /*int*/, dimensionY /*int*/, transform) {
  5864. if (dimensionX <= 0 || dimensionY <= 0) {
  5865. throw new NotFoundException();
  5866. }
  5867. const bits = new BitMatrix(dimensionX, dimensionY);
  5868. const points = new Float32Array(2 * dimensionX);
  5869. for (let y = 0; y < dimensionY; y++) {
  5870. const max = points.length;
  5871. const iValue = y + 0.5;
  5872. for (let x = 0; x < max; x += 2) {
  5873. points[x] = (x / 2) + 0.5;
  5874. points[x + 1] = iValue;
  5875. }
  5876. transform.transformPoints(points);
  5877. // Quick check to see if points transformed to something inside the image
  5878. // sufficient to check the endpoints
  5879. GridSampler.checkAndNudgePoints(image, points);
  5880. try {
  5881. for (let x = 0; x < max; x += 2) {
  5882. if (image.get(Math.floor(points[x]), Math.floor(points[x + 1]))) {
  5883. // Black(-ish) pixel
  5884. bits.set(x / 2, y);
  5885. }
  5886. }
  5887. }
  5888. catch (aioobe /*: ArrayIndexOutOfBoundsException*/) {
  5889. // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
  5890. // transform gets "twisted" such that it maps a straight line of points to a set of points
  5891. // whose endpoints are in bounds, but others are not. There is probably some mathematical
  5892. // way to detect this about the transformation that I don't know yet.
  5893. // This results in an ugly runtime exception despite our clever checks above -- can't have
  5894. // that. We could check each point's coordinates but that feels duplicative. We settle for
  5895. // catching and wrapping ArrayIndexOutOfBoundsException.
  5896. throw new NotFoundException();
  5897. }
  5898. }
  5899. return bits;
  5900. }
  5901. }
  5902. class GridSamplerInstance {
  5903. /**
  5904. * Sets the implementation of GridSampler used by the library. One global
  5905. * instance is stored, which may sound problematic. But, the implementation provided
  5906. * ought to be appropriate for the entire platform, and all uses of this library
  5907. * in the whole lifetime of the JVM. For instance, an Android activity can swap in
  5908. * an implementation that takes advantage of native platform libraries.
  5909. *
  5910. * @param newGridSampler The platform-specific object to install.
  5911. */
  5912. static setGridSampler(newGridSampler) {
  5913. GridSamplerInstance.gridSampler = newGridSampler;
  5914. }
  5915. /**
  5916. * @return the current implementation of GridSampler
  5917. */
  5918. static getInstance() {
  5919. return GridSamplerInstance.gridSampler;
  5920. }
  5921. }
  5922. GridSamplerInstance.gridSampler = new DefaultGridSampler();
  5923. /*
  5924. * Copyright 2010 ZXing authors
  5925. *
  5926. * Licensed under the Apache License, Version 2.0 (the "License");
  5927. * you may not use this file except in compliance with the License.
  5928. * You may obtain a copy of the License at
  5929. *
  5930. * http://www.apache.org/licenses/LICENSE-2.0
  5931. *
  5932. * Unless required by applicable law or agreed to in writing, software
  5933. * distributed under the License is distributed on an "AS IS" BASIS,
  5934. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5935. * See the License for the specific language governing permissions and
  5936. * limitations under the License.
  5937. */
  5938. class Point {
  5939. constructor(x, y) {
  5940. this.x = x;
  5941. this.y = y;
  5942. }
  5943. toResultPoint() {
  5944. return new ResultPoint(this.getX(), this.getY());
  5945. }
  5946. getX() {
  5947. return this.x;
  5948. }
  5949. getY() {
  5950. return this.y;
  5951. }
  5952. }
  5953. /**
  5954. * Encapsulates logic that can detect an Aztec Code in an image, even if the Aztec Code
  5955. * is rotated or skewed, or partially obscured.
  5956. *
  5957. * @author David Olivier
  5958. * @author Frank Yellin
  5959. */
  5960. class Detector$3 {
  5961. constructor(image) {
  5962. this.EXPECTED_CORNER_BITS = new Int32Array([
  5963. 0xee0,
  5964. 0x1dc,
  5965. 0x83b,
  5966. 0x707,
  5967. ]);
  5968. this.image = image;
  5969. }
  5970. detect() {
  5971. return this.detectMirror(false);
  5972. }
  5973. /**
  5974. * Detects an Aztec Code in an image.
  5975. *
  5976. * @param isMirror if true, image is a mirror-image of original
  5977. * @return {@link AztecDetectorResult} encapsulating results of detecting an Aztec Code
  5978. * @throws NotFoundException if no Aztec Code can be found
  5979. */
  5980. detectMirror(isMirror) {
  5981. // 1. Get the center of the aztec matrix
  5982. let pCenter = this.getMatrixCenter();
  5983. // 2. Get the center points of the four diagonal points just outside the bull's eye
  5984. // [topRight, bottomRight, bottomLeft, topLeft]
  5985. let bullsEyeCorners = this.getBullsEyeCorners(pCenter);
  5986. if (isMirror) {
  5987. let temp = bullsEyeCorners[0];
  5988. bullsEyeCorners[0] = bullsEyeCorners[2];
  5989. bullsEyeCorners[2] = temp;
  5990. }
  5991. // 3. Get the size of the matrix and other parameters from the bull's eye
  5992. this.extractParameters(bullsEyeCorners);
  5993. // 4. Sample the grid
  5994. let bits = this.sampleGrid(this.image, bullsEyeCorners[this.shift % 4], bullsEyeCorners[(this.shift + 1) % 4], bullsEyeCorners[(this.shift + 2) % 4], bullsEyeCorners[(this.shift + 3) % 4]);
  5995. // 5. Get the corners of the matrix.
  5996. let corners = this.getMatrixCornerPoints(bullsEyeCorners);
  5997. return new AztecDetectorResult(bits, corners, this.compact, this.nbDataBlocks, this.nbLayers);
  5998. }
  5999. /**
  6000. * Extracts the number of data layers and data blocks from the layer around the bull's eye.
  6001. *
  6002. * @param bullsEyeCorners the array of bull's eye corners
  6003. * @throws NotFoundException in case of too many errors or invalid parameters
  6004. */
  6005. extractParameters(bullsEyeCorners) {
  6006. if (!this.isValidPoint(bullsEyeCorners[0]) || !this.isValidPoint(bullsEyeCorners[1]) ||
  6007. !this.isValidPoint(bullsEyeCorners[2]) || !this.isValidPoint(bullsEyeCorners[3])) {
  6008. throw new NotFoundException();
  6009. }
  6010. let length = 2 * this.nbCenterLayers;
  6011. // Get the bits around the bull's eye
  6012. let sides = new Int32Array([
  6013. this.sampleLine(bullsEyeCorners[0], bullsEyeCorners[1], length),
  6014. this.sampleLine(bullsEyeCorners[1], bullsEyeCorners[2], length),
  6015. this.sampleLine(bullsEyeCorners[2], bullsEyeCorners[3], length),
  6016. this.sampleLine(bullsEyeCorners[3], bullsEyeCorners[0], length) // Top
  6017. ]);
  6018. // bullsEyeCorners[shift] is the corner of the bulls'eye that has three
  6019. // orientation marks.
  6020. // sides[shift] is the row/column that goes from the corner with three
  6021. // orientation marks to the corner with two.
  6022. this.shift = this.getRotation(sides, length);
  6023. // Flatten the parameter bits into a single 28- or 40-bit long
  6024. let parameterData = 0;
  6025. for (let i = 0; i < 4; i++) {
  6026. let side = sides[(this.shift + i) % 4];
  6027. if (this.compact) {
  6028. // Each side of the form ..XXXXXXX. where Xs are parameter data
  6029. parameterData <<= 7;
  6030. parameterData += (side >> 1) & 0x7F;
  6031. }
  6032. else {
  6033. // Each side of the form ..XXXXX.XXXXX. where Xs are parameter data
  6034. parameterData <<= 10;
  6035. parameterData += ((side >> 2) & (0x1f << 5)) + ((side >> 1) & 0x1F);
  6036. }
  6037. }
  6038. // Corrects parameter data using RS. Returns just the data portion
  6039. // without the error correction.
  6040. let correctedData = this.getCorrectedParameterData(parameterData, this.compact);
  6041. if (this.compact) {
  6042. // 8 bits: 2 bits layers and 6 bits data blocks
  6043. this.nbLayers = (correctedData >> 6) + 1;
  6044. this.nbDataBlocks = (correctedData & 0x3F) + 1;
  6045. }
  6046. else {
  6047. // 16 bits: 5 bits layers and 11 bits data blocks
  6048. this.nbLayers = (correctedData >> 11) + 1;
  6049. this.nbDataBlocks = (correctedData & 0x7FF) + 1;
  6050. }
  6051. }
  6052. getRotation(sides, length) {
  6053. // In a normal pattern, we expect to See
  6054. // ** .* D A
  6055. // * *
  6056. //
  6057. // . *
  6058. // .. .. C B
  6059. //
  6060. // Grab the 3 bits from each of the sides the form the locator pattern and concatenate
  6061. // into a 12-bit integer. Start with the bit at A
  6062. let cornerBits = 0;
  6063. sides.forEach((side, idx, arr) => {
  6064. // XX......X where X's are orientation marks
  6065. let t = ((side >> (length - 2)) << 1) + (side & 1);
  6066. cornerBits = (cornerBits << 3) + t;
  6067. });
  6068. // for (var side in sides) {
  6069. // // XX......X where X's are orientation marks
  6070. // var t = ((side >> (length - 2)) << 1) + (side & 1);
  6071. // cornerBits = (cornerBits << 3) + t;
  6072. // }
  6073. // Mov the bottom bit to the top, so that the three bits of the locator pattern at A are
  6074. // together. cornerBits is now:
  6075. // 3 orientation bits at A || 3 orientation bits at B || ... || 3 orientation bits at D
  6076. cornerBits = ((cornerBits & 1) << 11) + (cornerBits >> 1);
  6077. // The result shift indicates which element of BullsEyeCorners[] goes into the top-left
  6078. // corner. Since the four rotation values have a Hamming distance of 8, we
  6079. // can easily tolerate two errors.
  6080. for (let shift = 0; shift < 4; shift++) {
  6081. if (Integer.bitCount(cornerBits ^ this.EXPECTED_CORNER_BITS[shift]) <= 2) {
  6082. return shift;
  6083. }
  6084. }
  6085. throw new NotFoundException();
  6086. }
  6087. /**
  6088. * Corrects the parameter bits using Reed-Solomon algorithm.
  6089. *
  6090. * @param parameterData parameter bits
  6091. * @param compact true if this is a compact Aztec code
  6092. * @throws NotFoundException if the array contains too many errors
  6093. */
  6094. getCorrectedParameterData(parameterData, compact) {
  6095. let numCodewords;
  6096. let numDataCodewords;
  6097. if (compact) {
  6098. numCodewords = 7;
  6099. numDataCodewords = 2;
  6100. }
  6101. else {
  6102. numCodewords = 10;
  6103. numDataCodewords = 4;
  6104. }
  6105. let numECCodewords = numCodewords - numDataCodewords;
  6106. let parameterWords = new Int32Array(numCodewords);
  6107. for (let i = numCodewords - 1; i >= 0; --i) {
  6108. parameterWords[i] = parameterData & 0xF;
  6109. parameterData >>= 4;
  6110. }
  6111. try {
  6112. let rsDecoder = new ReedSolomonDecoder(GenericGF.AZTEC_PARAM);
  6113. rsDecoder.decode(parameterWords, numECCodewords);
  6114. }
  6115. catch (ignored) {
  6116. throw new NotFoundException();
  6117. }
  6118. // Toss the error correction. Just return the data as an integer
  6119. let result = 0;
  6120. for (let i = 0; i < numDataCodewords; i++) {
  6121. result = (result << 4) + parameterWords[i];
  6122. }
  6123. return result;
  6124. }
  6125. /**
  6126. * Finds the corners of a bull-eye centered on the passed point.
  6127. * This returns the centers of the diagonal points just outside the bull's eye
  6128. * Returns [topRight, bottomRight, bottomLeft, topLeft]
  6129. *
  6130. * @param pCenter Center point
  6131. * @return The corners of the bull-eye
  6132. * @throws NotFoundException If no valid bull-eye can be found
  6133. */
  6134. getBullsEyeCorners(pCenter) {
  6135. let pina = pCenter;
  6136. let pinb = pCenter;
  6137. let pinc = pCenter;
  6138. let pind = pCenter;
  6139. let color = true;
  6140. for (this.nbCenterLayers = 1; this.nbCenterLayers < 9; this.nbCenterLayers++) {
  6141. let pouta = this.getFirstDifferent(pina, color, 1, -1);
  6142. let poutb = this.getFirstDifferent(pinb, color, 1, 1);
  6143. let poutc = this.getFirstDifferent(pinc, color, -1, 1);
  6144. let poutd = this.getFirstDifferent(pind, color, -1, -1);
  6145. // d a
  6146. //
  6147. // c b
  6148. if (this.nbCenterLayers > 2) {
  6149. let q = (this.distancePoint(poutd, pouta) * this.nbCenterLayers) / (this.distancePoint(pind, pina) * (this.nbCenterLayers + 2));
  6150. if (q < 0.75 || q > 1.25 || !this.isWhiteOrBlackRectangle(pouta, poutb, poutc, poutd)) {
  6151. break;
  6152. }
  6153. }
  6154. pina = pouta;
  6155. pinb = poutb;
  6156. pinc = poutc;
  6157. pind = poutd;
  6158. color = !color;
  6159. }
  6160. if (this.nbCenterLayers !== 5 && this.nbCenterLayers !== 7) {
  6161. throw new NotFoundException();
  6162. }
  6163. this.compact = this.nbCenterLayers === 5;
  6164. // Expand the square by .5 pixel in each direction so that we're on the border
  6165. // between the white square and the black square
  6166. let pinax = new ResultPoint(pina.getX() + 0.5, pina.getY() - 0.5);
  6167. let pinbx = new ResultPoint(pinb.getX() + 0.5, pinb.getY() + 0.5);
  6168. let pincx = new ResultPoint(pinc.getX() - 0.5, pinc.getY() + 0.5);
  6169. let pindx = new ResultPoint(pind.getX() - 0.5, pind.getY() - 0.5);
  6170. // Expand the square so that its corners are the centers of the points
  6171. // just outside the bull's eye.
  6172. return this.expandSquare([pinax, pinbx, pincx, pindx], 2 * this.nbCenterLayers - 3, 2 * this.nbCenterLayers);
  6173. }
  6174. /**
  6175. * Finds a candidate center point of an Aztec code from an image
  6176. *
  6177. * @return the center point
  6178. */
  6179. getMatrixCenter() {
  6180. let pointA;
  6181. let pointB;
  6182. let pointC;
  6183. let pointD;
  6184. // Get a white rectangle that can be the border of the matrix in center bull's eye or
  6185. try {
  6186. let cornerPoints = new WhiteRectangleDetector(this.image).detect();
  6187. pointA = cornerPoints[0];
  6188. pointB = cornerPoints[1];
  6189. pointC = cornerPoints[2];
  6190. pointD = cornerPoints[3];
  6191. }
  6192. catch (e) {
  6193. // This exception can be in case the initial rectangle is white
  6194. // In that case, surely in the bull's eye, we try to expand the rectangle.
  6195. let cx = this.image.getWidth() / 2;
  6196. let cy = this.image.getHeight() / 2;
  6197. pointA = this.getFirstDifferent(new Point(cx + 7, cy - 7), false, 1, -1).toResultPoint();
  6198. pointB = this.getFirstDifferent(new Point(cx + 7, cy + 7), false, 1, 1).toResultPoint();
  6199. pointC = this.getFirstDifferent(new Point(cx - 7, cy + 7), false, -1, 1).toResultPoint();
  6200. pointD = this.getFirstDifferent(new Point(cx - 7, cy - 7), false, -1, -1).toResultPoint();
  6201. }
  6202. // Compute the center of the rectangle
  6203. let cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4.0);
  6204. let cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4.0);
  6205. // Redetermine the white rectangle starting from previously computed center.
  6206. // This will ensure that we end up with a white rectangle in center bull's eye
  6207. // in order to compute a more accurate center.
  6208. try {
  6209. let cornerPoints = new WhiteRectangleDetector(this.image, 15, cx, cy).detect();
  6210. pointA = cornerPoints[0];
  6211. pointB = cornerPoints[1];
  6212. pointC = cornerPoints[2];
  6213. pointD = cornerPoints[3];
  6214. }
  6215. catch (e) {
  6216. // This exception can be in case the initial rectangle is white
  6217. // In that case we try to expand the rectangle.
  6218. pointA = this.getFirstDifferent(new Point(cx + 7, cy - 7), false, 1, -1).toResultPoint();
  6219. pointB = this.getFirstDifferent(new Point(cx + 7, cy + 7), false, 1, 1).toResultPoint();
  6220. pointC = this.getFirstDifferent(new Point(cx - 7, cy + 7), false, -1, 1).toResultPoint();
  6221. pointD = this.getFirstDifferent(new Point(cx - 7, cy - 7), false, -1, -1).toResultPoint();
  6222. }
  6223. // Recompute the center of the rectangle
  6224. cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4.0);
  6225. cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4.0);
  6226. return new Point(cx, cy);
  6227. }
  6228. /**
  6229. * Gets the Aztec code corners from the bull's eye corners and the parameters.
  6230. *
  6231. * @param bullsEyeCorners the array of bull's eye corners
  6232. * @return the array of aztec code corners
  6233. */
  6234. getMatrixCornerPoints(bullsEyeCorners) {
  6235. return this.expandSquare(bullsEyeCorners, 2 * this.nbCenterLayers, this.getDimension());
  6236. }
  6237. /**
  6238. * Creates a BitMatrix by sampling the provided image.
  6239. * topLeft, topRight, bottomRight, and bottomLeft are the centers of the squares on the
  6240. * diagonal just outside the bull's eye.
  6241. */
  6242. sampleGrid(image, topLeft, topRight, bottomRight, bottomLeft) {
  6243. let sampler = GridSamplerInstance.getInstance();
  6244. let dimension = this.getDimension();
  6245. let low = dimension / 2 - this.nbCenterLayers;
  6246. let high = dimension / 2 + this.nbCenterLayers;
  6247. return sampler.sampleGrid(image, dimension, dimension, low, low, // topleft
  6248. high, low, // topright
  6249. high, high, // bottomright
  6250. low, high, // bottomleft
  6251. topLeft.getX(), topLeft.getY(), topRight.getX(), topRight.getY(), bottomRight.getX(), bottomRight.getY(), bottomLeft.getX(), bottomLeft.getY());
  6252. }
  6253. /**
  6254. * Samples a line.
  6255. *
  6256. * @param p1 start point (inclusive)
  6257. * @param p2 end point (exclusive)
  6258. * @param size number of bits
  6259. * @return the array of bits as an int (first bit is high-order bit of result)
  6260. */
  6261. sampleLine(p1, p2, size) {
  6262. let result = 0;
  6263. let d = this.distanceResultPoint(p1, p2);
  6264. let moduleSize = d / size;
  6265. let px = p1.getX();
  6266. let py = p1.getY();
  6267. let dx = moduleSize * (p2.getX() - p1.getX()) / d;
  6268. let dy = moduleSize * (p2.getY() - p1.getY()) / d;
  6269. for (let i = 0; i < size; i++) {
  6270. if (this.image.get(MathUtils.round(px + i * dx), MathUtils.round(py + i * dy))) {
  6271. result |= 1 << (size - i - 1);
  6272. }
  6273. }
  6274. return result;
  6275. }
  6276. /**
  6277. * @return true if the border of the rectangle passed in parameter is compound of white points only
  6278. * or black points only
  6279. */
  6280. isWhiteOrBlackRectangle(p1, p2, p3, p4) {
  6281. let corr = 3;
  6282. p1 = new Point(p1.getX() - corr, p1.getY() + corr);
  6283. p2 = new Point(p2.getX() - corr, p2.getY() - corr);
  6284. p3 = new Point(p3.getX() + corr, p3.getY() - corr);
  6285. p4 = new Point(p4.getX() + corr, p4.getY() + corr);
  6286. let cInit = this.getColor(p4, p1);
  6287. if (cInit === 0) {
  6288. return false;
  6289. }
  6290. let c = this.getColor(p1, p2);
  6291. if (c !== cInit) {
  6292. return false;
  6293. }
  6294. c = this.getColor(p2, p3);
  6295. if (c !== cInit) {
  6296. return false;
  6297. }
  6298. c = this.getColor(p3, p4);
  6299. return c === cInit;
  6300. }
  6301. /**
  6302. * Gets the color of a segment
  6303. *
  6304. * @return 1 if segment more than 90% black, -1 if segment is more than 90% white, 0 else
  6305. */
  6306. getColor(p1, p2) {
  6307. let d = this.distancePoint(p1, p2);
  6308. let dx = (p2.getX() - p1.getX()) / d;
  6309. let dy = (p2.getY() - p1.getY()) / d;
  6310. let error = 0;
  6311. let px = p1.getX();
  6312. let py = p1.getY();
  6313. let colorModel = this.image.get(p1.getX(), p1.getY());
  6314. let iMax = Math.ceil(d);
  6315. for (let i = 0; i < iMax; i++) {
  6316. px += dx;
  6317. py += dy;
  6318. if (this.image.get(MathUtils.round(px), MathUtils.round(py)) !== colorModel) {
  6319. error++;
  6320. }
  6321. }
  6322. let errRatio = error / d;
  6323. if (errRatio > 0.1 && errRatio < 0.9) {
  6324. return 0;
  6325. }
  6326. return (errRatio <= 0.1) === colorModel ? 1 : -1;
  6327. }
  6328. /**
  6329. * Gets the coordinate of the first point with a different color in the given direction
  6330. */
  6331. getFirstDifferent(init, color, dx, dy) {
  6332. let x = init.getX() + dx;
  6333. let y = init.getY() + dy;
  6334. while (this.isValid(x, y) && this.image.get(x, y) === color) {
  6335. x += dx;
  6336. y += dy;
  6337. }
  6338. x -= dx;
  6339. y -= dy;
  6340. while (this.isValid(x, y) && this.image.get(x, y) === color) {
  6341. x += dx;
  6342. }
  6343. x -= dx;
  6344. while (this.isValid(x, y) && this.image.get(x, y) === color) {
  6345. y += dy;
  6346. }
  6347. y -= dy;
  6348. return new Point(x, y);
  6349. }
  6350. /**
  6351. * Expand the square represented by the corner points by pushing out equally in all directions
  6352. *
  6353. * @param cornerPoints the corners of the square, which has the bull's eye at its center
  6354. * @param oldSide the original length of the side of the square in the target bit matrix
  6355. * @param newSide the new length of the size of the square in the target bit matrix
  6356. * @return the corners of the expanded square
  6357. */
  6358. expandSquare(cornerPoints, oldSide, newSide) {
  6359. let ratio = newSide / (2.0 * oldSide);
  6360. let dx = cornerPoints[0].getX() - cornerPoints[2].getX();
  6361. let dy = cornerPoints[0].getY() - cornerPoints[2].getY();
  6362. let centerx = (cornerPoints[0].getX() + cornerPoints[2].getX()) / 2.0;
  6363. let centery = (cornerPoints[0].getY() + cornerPoints[2].getY()) / 2.0;
  6364. let result0 = new ResultPoint(centerx + ratio * dx, centery + ratio * dy);
  6365. let result2 = new ResultPoint(centerx - ratio * dx, centery - ratio * dy);
  6366. dx = cornerPoints[1].getX() - cornerPoints[3].getX();
  6367. dy = cornerPoints[1].getY() - cornerPoints[3].getY();
  6368. centerx = (cornerPoints[1].getX() + cornerPoints[3].getX()) / 2.0;
  6369. centery = (cornerPoints[1].getY() + cornerPoints[3].getY()) / 2.0;
  6370. let result1 = new ResultPoint(centerx + ratio * dx, centery + ratio * dy);
  6371. let result3 = new ResultPoint(centerx - ratio * dx, centery - ratio * dy);
  6372. let results = [result0, result1, result2, result3];
  6373. return results;
  6374. }
  6375. isValid(x, y) {
  6376. return x >= 0 && x < this.image.getWidth() && y > 0 && y < this.image.getHeight();
  6377. }
  6378. isValidPoint(point) {
  6379. let x = MathUtils.round(point.getX());
  6380. let y = MathUtils.round(point.getY());
  6381. return this.isValid(x, y);
  6382. }
  6383. distancePoint(a, b) {
  6384. return MathUtils.distance(a.getX(), a.getY(), b.getX(), b.getY());
  6385. }
  6386. distanceResultPoint(a, b) {
  6387. return MathUtils.distance(a.getX(), a.getY(), b.getX(), b.getY());
  6388. }
  6389. getDimension() {
  6390. if (this.compact) {
  6391. return 4 * this.nbLayers + 11;
  6392. }
  6393. if (this.nbLayers <= 4) {
  6394. return 4 * this.nbLayers + 15;
  6395. }
  6396. return 4 * this.nbLayers + 2 * (Integer.truncDivision((this.nbLayers - 4), 8) + 1) + 15;
  6397. }
  6398. }
  6399. /*
  6400. * Copyright 2010 ZXing authors
  6401. *
  6402. * Licensed under the Apache License, Version 2.0 (the "License");
  6403. * you may not use this file except in compliance with the License.
  6404. * You may obtain a copy of the License at
  6405. *
  6406. * http://www.apache.org/licenses/LICENSE-2.0
  6407. *
  6408. * Unless required by applicable law or agreed to in writing, software
  6409. * distributed under the License is distributed on an "AS IS" BASIS,
  6410. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6411. * See the License for the specific language governing permissions and
  6412. * limitations under the License.
  6413. */
  6414. // import java.util.List;
  6415. // import java.util.Map;
  6416. /**
  6417. * This implementation can detect and decode Aztec codes in an image.
  6418. *
  6419. * @author David Olivier
  6420. */
  6421. class AztecReader {
  6422. /**
  6423. * Locates and decodes a Data Matrix code in an image.
  6424. *
  6425. * @return a String representing the content encoded by the Data Matrix code
  6426. * @throws NotFoundException if a Data Matrix code cannot be found
  6427. * @throws FormatException if a Data Matrix code cannot be decoded
  6428. */
  6429. decode(image, hints = null) {
  6430. let exception = null;
  6431. let detector = new Detector$3(image.getBlackMatrix());
  6432. let points = null;
  6433. let decoderResult = null;
  6434. try {
  6435. let detectorResult = detector.detectMirror(false);
  6436. points = detectorResult.getPoints();
  6437. this.reportFoundResultPoints(hints, points);
  6438. decoderResult = new Decoder$2().decode(detectorResult);
  6439. }
  6440. catch (e) {
  6441. exception = e;
  6442. }
  6443. if (decoderResult == null) {
  6444. try {
  6445. let detectorResult = detector.detectMirror(true);
  6446. points = detectorResult.getPoints();
  6447. this.reportFoundResultPoints(hints, points);
  6448. decoderResult = new Decoder$2().decode(detectorResult);
  6449. }
  6450. catch (e) {
  6451. if (exception != null) {
  6452. throw exception;
  6453. }
  6454. throw e;
  6455. }
  6456. }
  6457. let result = new Result$1(decoderResult.getText(), decoderResult.getRawBytes(), decoderResult.getNumBits(), points, BarcodeFormat$1.AZTEC, System.currentTimeMillis());
  6458. let byteSegments = decoderResult.getByteSegments();
  6459. if (byteSegments != null) {
  6460. result.putMetadata(ResultMetadataType$1.BYTE_SEGMENTS, byteSegments);
  6461. }
  6462. let ecLevel = decoderResult.getECLevel();
  6463. if (ecLevel != null) {
  6464. result.putMetadata(ResultMetadataType$1.ERROR_CORRECTION_LEVEL, ecLevel);
  6465. }
  6466. return result;
  6467. }
  6468. reportFoundResultPoints(hints, points) {
  6469. if (hints != null) {
  6470. let rpcb = hints.get(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK);
  6471. if (rpcb != null) {
  6472. points.forEach((point, idx, arr) => {
  6473. rpcb.foundPossibleResultPoint(point);
  6474. });
  6475. }
  6476. }
  6477. }
  6478. // @Override
  6479. reset() {
  6480. // do nothing
  6481. }
  6482. }
  6483. /**
  6484. * Aztec Code reader to use from browser.
  6485. *
  6486. * @class BrowserAztecCodeReader
  6487. * @extends {BrowserCodeReader}
  6488. */
  6489. class BrowserAztecCodeReader extends BrowserCodeReader {
  6490. /**
  6491. * Creates an instance of BrowserAztecCodeReader.
  6492. * @param {number} [timeBetweenScansMillis=500] the time delay between subsequent decode tries
  6493. *
  6494. * @memberOf BrowserAztecCodeReader
  6495. */
  6496. constructor(timeBetweenScansMillis = 500) {
  6497. super(new AztecReader(), timeBetweenScansMillis);
  6498. }
  6499. }
  6500. /*
  6501. * Copyright 2008 ZXing authors
  6502. *
  6503. * Licensed under the Apache License, Version 2.0 (the "License");
  6504. * you may not use this file except in compliance with the License.
  6505. * You may obtain a copy of the License at
  6506. *
  6507. * http://www.apache.org/licenses/LICENSE-2.0
  6508. *
  6509. * Unless required by applicable law or agreed to in writing, software
  6510. * distributed under the License is distributed on an "AS IS" BASIS,
  6511. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6512. * See the License for the specific language governing permissions and
  6513. * limitations under the License.
  6514. */
  6515. /**
  6516. * Encapsulates functionality and implementation that is common to all families
  6517. * of one-dimensional barcodes.
  6518. *
  6519. * @author dswitkin@google.com (Daniel Switkin)
  6520. * @author Sean Owen
  6521. */
  6522. class OneDReader {
  6523. /*
  6524. @Override
  6525. public Result decode(BinaryBitmap image) throws NotFoundException, FormatException {
  6526. return decode(image, null);
  6527. }
  6528. */
  6529. // Note that we don't try rotation without the try harder flag, even if rotation was supported.
  6530. // @Override
  6531. decode(image, hints) {
  6532. try {
  6533. return this.doDecode(image, hints);
  6534. }
  6535. catch (nfe) {
  6536. const tryHarder = hints && (hints.get(DecodeHintType$1.TRY_HARDER) === true);
  6537. if (tryHarder && image.isRotateSupported()) {
  6538. const rotatedImage = image.rotateCounterClockwise();
  6539. const result = this.doDecode(rotatedImage, hints);
  6540. // Record that we found it rotated 90 degrees CCW / 270 degrees CW
  6541. const metadata = result.getResultMetadata();
  6542. let orientation = 270;
  6543. if (metadata !== null && (metadata.get(ResultMetadataType$1.ORIENTATION) === true)) {
  6544. // But if we found it reversed in doDecode(), add in that result here:
  6545. orientation = (orientation + metadata.get(ResultMetadataType$1.ORIENTATION) % 360);
  6546. }
  6547. result.putMetadata(ResultMetadataType$1.ORIENTATION, orientation);
  6548. // Update result points
  6549. const points = result.getResultPoints();
  6550. if (points !== null) {
  6551. const height = rotatedImage.getHeight();
  6552. for (let i = 0; i < points.length; i++) {
  6553. points[i] = new ResultPoint(height - points[i].getY() - 1, points[i].getX());
  6554. }
  6555. }
  6556. return result;
  6557. }
  6558. else {
  6559. throw new NotFoundException();
  6560. }
  6561. }
  6562. }
  6563. // @Override
  6564. reset() {
  6565. // do nothing
  6566. }
  6567. /**
  6568. * We're going to examine rows from the middle outward, searching alternately above and below the
  6569. * middle, and farther out each time. rowStep is the number of rows between each successive
  6570. * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then
  6571. * middle + rowStep, then middle - (2 * rowStep), etc.
  6572. * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily
  6573. * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the
  6574. * image if "trying harder".
  6575. *
  6576. * @param image The image to decode
  6577. * @param hints Any hints that were requested
  6578. * @return The contents of the decoded barcode
  6579. * @throws NotFoundException Any spontaneous errors which occur
  6580. */
  6581. doDecode(image, hints) {
  6582. const width = image.getWidth();
  6583. const height = image.getHeight();
  6584. let row = new BitArray(width);
  6585. const tryHarder = hints && (hints.get(DecodeHintType$1.TRY_HARDER) === true);
  6586. const rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
  6587. let maxLines;
  6588. if (tryHarder) {
  6589. maxLines = height; // Look at the whole image, not just the center
  6590. }
  6591. else {
  6592. maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image
  6593. }
  6594. const middle = Math.trunc(height / 2);
  6595. for (let x = 0; x < maxLines; x++) {
  6596. // Scanning from the middle out. Determine which row we're looking at next:
  6597. const rowStepsAboveOrBelow = Math.trunc((x + 1) / 2);
  6598. const isAbove = (x & 0x01) === 0; // i.e. is x even?
  6599. const rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
  6600. if (rowNumber < 0 || rowNumber >= height) {
  6601. // Oops, if we run off the top or bottom, stop
  6602. break;
  6603. }
  6604. // Estimate black point for this row and load it:
  6605. try {
  6606. row = image.getBlackRow(rowNumber, row);
  6607. }
  6608. catch (ignored) {
  6609. continue;
  6610. }
  6611. // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
  6612. // handle decoding upside down barcodes.
  6613. for (let attempt = 0; attempt < 2; attempt++) {
  6614. if (attempt === 1) { // trying again?
  6615. row.reverse(); // reverse the row and continue
  6616. // This means we will only ever draw result points *once* in the life of this method
  6617. // since we want to avoid drawing the wrong points after flipping the row, and,
  6618. // don't want to clutter with noise from every single row scan -- just the scans
  6619. // that start on the center line.
  6620. if (hints && (hints.get(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK) === true)) {
  6621. const newHints = new Map();
  6622. hints.forEach((hint, key) => newHints.set(key, hint));
  6623. newHints.delete(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK);
  6624. hints = newHints;
  6625. }
  6626. }
  6627. try {
  6628. // Look for a barcode
  6629. const result = this.decodeRow(rowNumber, row, hints);
  6630. // We found our barcode
  6631. if (attempt === 1) {
  6632. // But it was upside down, so note that
  6633. result.putMetadata(ResultMetadataType$1.ORIENTATION, 180);
  6634. // And remember to flip the result points horizontally.
  6635. const points = result.getResultPoints();
  6636. if (points !== null) {
  6637. points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
  6638. points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
  6639. }
  6640. }
  6641. return result;
  6642. }
  6643. catch (re) {
  6644. // continue -- just couldn't decode this row
  6645. }
  6646. }
  6647. }
  6648. throw new NotFoundException();
  6649. }
  6650. /**
  6651. * Records the size of successive runs of white and black pixels in a row, starting at a given point.
  6652. * The values are recorded in the given array, and the number of runs recorded is equal to the size
  6653. * of the array. If the row starts on a white pixel at the given start point, then the first count
  6654. * recorded is the run of white pixels starting from that point; likewise it is the count of a run
  6655. * of black pixels if the row begin on a black pixels at that point.
  6656. *
  6657. * @param row row to count from
  6658. * @param start offset into row to start at
  6659. * @param counters array into which to record counts
  6660. * @throws NotFoundException if counters cannot be filled entirely from row before running out
  6661. * of pixels
  6662. */
  6663. static recordPattern(row, start, counters) {
  6664. const numCounters = counters.length;
  6665. for (let index = 0; index < numCounters; index++)
  6666. counters[index] = 0;
  6667. const end = row.getSize();
  6668. if (start >= end) {
  6669. throw new NotFoundException();
  6670. }
  6671. let isWhite = !row.get(start);
  6672. let counterPosition = 0;
  6673. let i = start;
  6674. while (i < end) {
  6675. if (row.get(i) !== isWhite) {
  6676. counters[counterPosition]++;
  6677. }
  6678. else {
  6679. if (++counterPosition === numCounters) {
  6680. break;
  6681. }
  6682. else {
  6683. counters[counterPosition] = 1;
  6684. isWhite = !isWhite;
  6685. }
  6686. }
  6687. i++;
  6688. }
  6689. // If we read fully the last section of pixels and filled up our counters -- or filled
  6690. // the last counter but ran off the side of the image, OK. Otherwise, a problem.
  6691. if (!(counterPosition === numCounters || (counterPosition === numCounters - 1 && i === end))) {
  6692. throw new NotFoundException();
  6693. }
  6694. }
  6695. static recordPatternInReverse(row, start, counters) {
  6696. // This could be more efficient I guess
  6697. let numTransitionsLeft = counters.length;
  6698. let last = row.get(start);
  6699. while (start > 0 && numTransitionsLeft >= 0) {
  6700. if (row.get(--start) !== last) {
  6701. numTransitionsLeft--;
  6702. last = !last;
  6703. }
  6704. }
  6705. if (numTransitionsLeft >= 0) {
  6706. throw new NotFoundException();
  6707. }
  6708. OneDReader.recordPattern(row, start + 1, counters);
  6709. }
  6710. /**
  6711. * Determines how closely a set of observed counts of runs of black/white values matches a given
  6712. * target pattern. This is reported as the ratio of the total variance from the expected pattern
  6713. * proportions across all pattern elements, to the length of the pattern.
  6714. *
  6715. * @param counters observed counters
  6716. * @param pattern expected pattern
  6717. * @param maxIndividualVariance The most any counter can differ before we give up
  6718. * @return ratio of total variance between counters and pattern compared to total pattern size
  6719. */
  6720. static patternMatchVariance(counters, pattern, maxIndividualVariance) {
  6721. const numCounters = counters.length;
  6722. let total = 0;
  6723. let patternLength = 0;
  6724. for (let i = 0; i < numCounters; i++) {
  6725. total += counters[i];
  6726. patternLength += pattern[i];
  6727. }
  6728. if (total < patternLength) {
  6729. // If we don't even have one pixel per unit of bar width, assume this is too small
  6730. // to reliably match, so fail:
  6731. return Number.POSITIVE_INFINITY;
  6732. }
  6733. const unitBarWidth = total / patternLength;
  6734. maxIndividualVariance *= unitBarWidth;
  6735. let totalVariance = 0.0;
  6736. for (let x = 0; x < numCounters; x++) {
  6737. const counter = counters[x];
  6738. const scaledPattern = pattern[x] * unitBarWidth;
  6739. const variance = counter > scaledPattern ? counter - scaledPattern : scaledPattern - counter;
  6740. if (variance > maxIndividualVariance) {
  6741. return Number.POSITIVE_INFINITY;
  6742. }
  6743. totalVariance += variance;
  6744. }
  6745. return totalVariance / total;
  6746. }
  6747. }
  6748. /*
  6749. * Copyright 2008 ZXing authors
  6750. *
  6751. * Licensed under the Apache License, Version 2.0 (the "License");
  6752. * you may not use this file except in compliance with the License.
  6753. * You may obtain a copy of the License at
  6754. *
  6755. * http://www.apache.org/licenses/LICENSE-2.0
  6756. *
  6757. * Unless required by applicable law or agreed to in writing, software
  6758. * distributed under the License is distributed on an "AS IS" BASIS,
  6759. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6760. * See the License for the specific language governing permissions and
  6761. * limitations under the License.
  6762. */
  6763. /**
  6764. * <p>Decodes Code 128 barcodes.</p>
  6765. *
  6766. * @author Sean Owen
  6767. */
  6768. class Code128Reader extends OneDReader {
  6769. static findStartPattern(row) {
  6770. const width = row.getSize();
  6771. const rowOffset = row.getNextSet(0);
  6772. let counterPosition = 0;
  6773. let counters = Int32Array.from([0, 0, 0, 0, 0, 0]);
  6774. let patternStart = rowOffset;
  6775. let isWhite = false;
  6776. const patternLength = 6;
  6777. for (let i = rowOffset; i < width; i++) {
  6778. if (row.get(i) !== isWhite) {
  6779. counters[counterPosition]++;
  6780. }
  6781. else {
  6782. if (counterPosition === (patternLength - 1)) {
  6783. let bestVariance = Code128Reader.MAX_AVG_VARIANCE;
  6784. let bestMatch = -1;
  6785. for (let startCode = Code128Reader.CODE_START_A; startCode <= Code128Reader.CODE_START_C; startCode++) {
  6786. const variance = OneDReader.patternMatchVariance(counters, Code128Reader.CODE_PATTERNS[startCode], Code128Reader.MAX_INDIVIDUAL_VARIANCE);
  6787. if (variance < bestVariance) {
  6788. bestVariance = variance;
  6789. bestMatch = startCode;
  6790. }
  6791. }
  6792. // Look for whitespace before start pattern, >= 50% of width of start pattern
  6793. if (bestMatch >= 0 &&
  6794. row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
  6795. return Int32Array.from([patternStart, i, bestMatch]);
  6796. }
  6797. patternStart += counters[0] + counters[1];
  6798. counters = counters.slice(2, counters.length);
  6799. counters[counterPosition - 1] = 0;
  6800. counters[counterPosition] = 0;
  6801. counterPosition--;
  6802. }
  6803. else {
  6804. counterPosition++;
  6805. }
  6806. counters[counterPosition] = 1;
  6807. isWhite = !isWhite;
  6808. }
  6809. }
  6810. throw new NotFoundException();
  6811. }
  6812. static decodeCode(row, counters, rowOffset) {
  6813. OneDReader.recordPattern(row, rowOffset, counters);
  6814. let bestVariance = Code128Reader.MAX_AVG_VARIANCE; // worst variance we'll accept
  6815. let bestMatch = -1;
  6816. for (let d = 0; d < Code128Reader.CODE_PATTERNS.length; d++) {
  6817. const pattern = Code128Reader.CODE_PATTERNS[d];
  6818. const variance = this.patternMatchVariance(counters, pattern, Code128Reader.MAX_INDIVIDUAL_VARIANCE);
  6819. if (variance < bestVariance) {
  6820. bestVariance = variance;
  6821. bestMatch = d;
  6822. }
  6823. }
  6824. // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6.
  6825. if (bestMatch >= 0) {
  6826. return bestMatch;
  6827. }
  6828. else {
  6829. throw new NotFoundException();
  6830. }
  6831. }
  6832. decodeRow(rowNumber, row, hints) {
  6833. const convertFNC1 = hints && (hints.get(DecodeHintType$1.ASSUME_GS1) === true);
  6834. const startPatternInfo = Code128Reader.findStartPattern(row);
  6835. const startCode = startPatternInfo[2];
  6836. let currentRawCodesIndex = 0;
  6837. const rawCodes = new Uint8Array(20);
  6838. rawCodes[currentRawCodesIndex++] = startCode;
  6839. let codeSet;
  6840. switch (startCode) {
  6841. case Code128Reader.CODE_START_A:
  6842. codeSet = Code128Reader.CODE_CODE_A;
  6843. break;
  6844. case Code128Reader.CODE_START_B:
  6845. codeSet = Code128Reader.CODE_CODE_B;
  6846. break;
  6847. case Code128Reader.CODE_START_C:
  6848. codeSet = Code128Reader.CODE_CODE_C;
  6849. break;
  6850. default:
  6851. throw new FormatException();
  6852. }
  6853. let done = false;
  6854. let isNextShifted = false;
  6855. let result = '';
  6856. let lastStart = startPatternInfo[0];
  6857. let nextStart = startPatternInfo[1];
  6858. const counters = Int32Array.from([0, 0, 0, 0, 0, 0]);
  6859. let lastCode = 0;
  6860. let code = 0;
  6861. let checksumTotal = startCode;
  6862. let multiplier = 0;
  6863. let lastCharacterWasPrintable = true;
  6864. let upperMode = false;
  6865. let shiftUpperMode = false;
  6866. while (!done) {
  6867. const unshift = isNextShifted;
  6868. isNextShifted = false;
  6869. // Save off last code
  6870. lastCode = code;
  6871. // Decode another code from image
  6872. code = Code128Reader.decodeCode(row, counters, nextStart);
  6873. rawCodes[currentRawCodesIndex++] = code;
  6874. // Remember whether the last code was printable or not (excluding CODE_STOP)
  6875. if (code !== Code128Reader.CODE_STOP) {
  6876. lastCharacterWasPrintable = true;
  6877. }
  6878. // Add to checksum computation (if not CODE_STOP of course)
  6879. if (code !== Code128Reader.CODE_STOP) {
  6880. multiplier++;
  6881. checksumTotal += multiplier * code;
  6882. }
  6883. // Advance to where the next code will to start
  6884. lastStart = nextStart;
  6885. nextStart += counters.reduce((previous, current) => previous + current, 0);
  6886. // Take care of illegal start codes
  6887. switch (code) {
  6888. case Code128Reader.CODE_START_A:
  6889. case Code128Reader.CODE_START_B:
  6890. case Code128Reader.CODE_START_C:
  6891. throw new FormatException();
  6892. }
  6893. switch (codeSet) {
  6894. case Code128Reader.CODE_CODE_A:
  6895. if (code < 64) {
  6896. if (shiftUpperMode === upperMode) {
  6897. result += String.fromCharCode((' '.charCodeAt(0) + code));
  6898. }
  6899. else {
  6900. result += String.fromCharCode((' '.charCodeAt(0) + code + 128));
  6901. }
  6902. shiftUpperMode = false;
  6903. }
  6904. else if (code < 96) {
  6905. if (shiftUpperMode === upperMode) {
  6906. result += String.fromCharCode((code - 64));
  6907. }
  6908. else {
  6909. result += String.fromCharCode((code + 64));
  6910. }
  6911. shiftUpperMode = false;
  6912. }
  6913. else {
  6914. // Don't let CODE_STOP, which always appears, affect whether whether we think the last
  6915. // code was printable or not.
  6916. if (code !== Code128Reader.CODE_STOP) {
  6917. lastCharacterWasPrintable = false;
  6918. }
  6919. switch (code) {
  6920. case Code128Reader.CODE_FNC_1:
  6921. if (convertFNC1) {
  6922. if (result.length === 0) {
  6923. // GS1 specification 5.4.3.7. and 5.4.6.4. If the first char after the start code
  6924. // is FNC1 then this is GS1-128. We add the symbology identifier.
  6925. result += ']C1';
  6926. }
  6927. else {
  6928. // GS1 specification 5.4.7.5. Every subsequent FNC1 is returned as ASCII 29 (GS)
  6929. result += String.fromCharCode(29);
  6930. }
  6931. }
  6932. break;
  6933. case Code128Reader.CODE_FNC_2:
  6934. case Code128Reader.CODE_FNC_3:
  6935. // do nothing?
  6936. break;
  6937. case Code128Reader.CODE_FNC_4_A:
  6938. if (!upperMode && shiftUpperMode) {
  6939. upperMode = true;
  6940. shiftUpperMode = false;
  6941. }
  6942. else if (upperMode && shiftUpperMode) {
  6943. upperMode = false;
  6944. shiftUpperMode = false;
  6945. }
  6946. else {
  6947. shiftUpperMode = true;
  6948. }
  6949. break;
  6950. case Code128Reader.CODE_SHIFT:
  6951. isNextShifted = true;
  6952. codeSet = Code128Reader.CODE_CODE_B;
  6953. break;
  6954. case Code128Reader.CODE_CODE_B:
  6955. codeSet = Code128Reader.CODE_CODE_B;
  6956. break;
  6957. case Code128Reader.CODE_CODE_C:
  6958. codeSet = Code128Reader.CODE_CODE_C;
  6959. break;
  6960. case Code128Reader.CODE_STOP:
  6961. done = true;
  6962. break;
  6963. }
  6964. }
  6965. break;
  6966. case Code128Reader.CODE_CODE_B:
  6967. if (code < 96) {
  6968. if (shiftUpperMode === upperMode) {
  6969. result += String.fromCharCode((' '.charCodeAt(0) + code));
  6970. }
  6971. else {
  6972. result += String.fromCharCode((' '.charCodeAt(0) + code + 128));
  6973. }
  6974. shiftUpperMode = false;
  6975. }
  6976. else {
  6977. if (code !== Code128Reader.CODE_STOP) {
  6978. lastCharacterWasPrintable = false;
  6979. }
  6980. switch (code) {
  6981. case Code128Reader.CODE_FNC_1:
  6982. if (convertFNC1) {
  6983. if (result.length === 0) {
  6984. // GS1 specification 5.4.3.7. and 5.4.6.4. If the first char after the start code
  6985. // is FNC1 then this is GS1-128. We add the symbology identifier.
  6986. result += ']C1';
  6987. }
  6988. else {
  6989. // GS1 specification 5.4.7.5. Every subsequent FNC1 is returned as ASCII 29 (GS)
  6990. result += String.fromCharCode(29);
  6991. }
  6992. }
  6993. break;
  6994. case Code128Reader.CODE_FNC_2:
  6995. case Code128Reader.CODE_FNC_3:
  6996. // do nothing?
  6997. break;
  6998. case Code128Reader.CODE_FNC_4_B:
  6999. if (!upperMode && shiftUpperMode) {
  7000. upperMode = true;
  7001. shiftUpperMode = false;
  7002. }
  7003. else if (upperMode && shiftUpperMode) {
  7004. upperMode = false;
  7005. shiftUpperMode = false;
  7006. }
  7007. else {
  7008. shiftUpperMode = true;
  7009. }
  7010. break;
  7011. case Code128Reader.CODE_SHIFT:
  7012. isNextShifted = true;
  7013. codeSet = Code128Reader.CODE_CODE_A;
  7014. break;
  7015. case Code128Reader.CODE_CODE_A:
  7016. codeSet = Code128Reader.CODE_CODE_A;
  7017. break;
  7018. case Code128Reader.CODE_CODE_C:
  7019. codeSet = Code128Reader.CODE_CODE_C;
  7020. break;
  7021. case Code128Reader.CODE_STOP:
  7022. done = true;
  7023. break;
  7024. }
  7025. }
  7026. break;
  7027. case Code128Reader.CODE_CODE_C:
  7028. if (code < 100) {
  7029. if (code < 10) {
  7030. result += '0';
  7031. }
  7032. result += code;
  7033. }
  7034. else {
  7035. if (code !== Code128Reader.CODE_STOP) {
  7036. lastCharacterWasPrintable = false;
  7037. }
  7038. switch (code) {
  7039. case Code128Reader.CODE_FNC_1:
  7040. if (convertFNC1) {
  7041. if (result.length === 0) {
  7042. // GS1 specification 5.4.3.7. and 5.4.6.4. If the first char after the start code
  7043. // is FNC1 then this is GS1-128. We add the symbology identifier.
  7044. result += ']C1';
  7045. }
  7046. else {
  7047. // GS1 specification 5.4.7.5. Every subsequent FNC1 is returned as ASCII 29 (GS)
  7048. result += String.fromCharCode(29);
  7049. }
  7050. }
  7051. break;
  7052. case Code128Reader.CODE_CODE_A:
  7053. codeSet = Code128Reader.CODE_CODE_A;
  7054. break;
  7055. case Code128Reader.CODE_CODE_B:
  7056. codeSet = Code128Reader.CODE_CODE_B;
  7057. break;
  7058. case Code128Reader.CODE_STOP:
  7059. done = true;
  7060. break;
  7061. }
  7062. }
  7063. break;
  7064. }
  7065. // Unshift back to another code set if we were shifted
  7066. if (unshift) {
  7067. codeSet = codeSet === Code128Reader.CODE_CODE_A ? Code128Reader.CODE_CODE_B : Code128Reader.CODE_CODE_A;
  7068. }
  7069. }
  7070. const lastPatternSize = nextStart - lastStart;
  7071. // Check for ample whitespace following pattern, but, to do this we first need to remember that
  7072. // we fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left
  7073. // to read off. Would be slightly better to properly read. Here we just skip it:
  7074. nextStart = row.getNextUnset(nextStart);
  7075. if (!row.isRange(nextStart, Math.min(row.getSize(), nextStart + (nextStart - lastStart) / 2), false)) {
  7076. throw new NotFoundException();
  7077. }
  7078. // Pull out from sum the value of the penultimate check code
  7079. checksumTotal -= multiplier * lastCode;
  7080. // lastCode is the checksum then:
  7081. if (checksumTotal % 103 !== lastCode) {
  7082. throw new ChecksumException();
  7083. }
  7084. // Need to pull out the check digits from string
  7085. const resultLength = result.length;
  7086. if (resultLength === 0) {
  7087. // false positive
  7088. throw new NotFoundException();
  7089. }
  7090. // Only bother if the result had at least one character, and if the checksum digit happened to
  7091. // be a printable character. If it was just interpreted as a control code, nothing to remove.
  7092. if (resultLength > 0 && lastCharacterWasPrintable) {
  7093. if (codeSet === Code128Reader.CODE_CODE_C) {
  7094. result = result.substring(0, resultLength - 2);
  7095. }
  7096. else {
  7097. result = result.substring(0, resultLength - 1);
  7098. }
  7099. }
  7100. const left = (startPatternInfo[1] + startPatternInfo[0]) / 2.0;
  7101. const right = lastStart + lastPatternSize / 2.0;
  7102. const rawCodesSize = rawCodes.length;
  7103. const rawBytes = new Uint8Array(rawCodesSize);
  7104. for (let i = 0; i < rawCodesSize; i++) {
  7105. rawBytes[i] = rawCodes[i];
  7106. }
  7107. const points = [new ResultPoint(left, rowNumber), new ResultPoint(right, rowNumber)];
  7108. return new Result$1(result, rawBytes, 0, points, BarcodeFormat$1.CODE_128, new Date().getTime());
  7109. }
  7110. }
  7111. Code128Reader.CODE_PATTERNS = [
  7112. Int32Array.from([2, 1, 2, 2, 2, 2]),
  7113. Int32Array.from([2, 2, 2, 1, 2, 2]),
  7114. Int32Array.from([2, 2, 2, 2, 2, 1]),
  7115. Int32Array.from([1, 2, 1, 2, 2, 3]),
  7116. Int32Array.from([1, 2, 1, 3, 2, 2]),
  7117. Int32Array.from([1, 3, 1, 2, 2, 2]),
  7118. Int32Array.from([1, 2, 2, 2, 1, 3]),
  7119. Int32Array.from([1, 2, 2, 3, 1, 2]),
  7120. Int32Array.from([1, 3, 2, 2, 1, 2]),
  7121. Int32Array.from([2, 2, 1, 2, 1, 3]),
  7122. Int32Array.from([2, 2, 1, 3, 1, 2]),
  7123. Int32Array.from([2, 3, 1, 2, 1, 2]),
  7124. Int32Array.from([1, 1, 2, 2, 3, 2]),
  7125. Int32Array.from([1, 2, 2, 1, 3, 2]),
  7126. Int32Array.from([1, 2, 2, 2, 3, 1]),
  7127. Int32Array.from([1, 1, 3, 2, 2, 2]),
  7128. Int32Array.from([1, 2, 3, 1, 2, 2]),
  7129. Int32Array.from([1, 2, 3, 2, 2, 1]),
  7130. Int32Array.from([2, 2, 3, 2, 1, 1]),
  7131. Int32Array.from([2, 2, 1, 1, 3, 2]),
  7132. Int32Array.from([2, 2, 1, 2, 3, 1]),
  7133. Int32Array.from([2, 1, 3, 2, 1, 2]),
  7134. Int32Array.from([2, 2, 3, 1, 1, 2]),
  7135. Int32Array.from([3, 1, 2, 1, 3, 1]),
  7136. Int32Array.from([3, 1, 1, 2, 2, 2]),
  7137. Int32Array.from([3, 2, 1, 1, 2, 2]),
  7138. Int32Array.from([3, 2, 1, 2, 2, 1]),
  7139. Int32Array.from([3, 1, 2, 2, 1, 2]),
  7140. Int32Array.from([3, 2, 2, 1, 1, 2]),
  7141. Int32Array.from([3, 2, 2, 2, 1, 1]),
  7142. Int32Array.from([2, 1, 2, 1, 2, 3]),
  7143. Int32Array.from([2, 1, 2, 3, 2, 1]),
  7144. Int32Array.from([2, 3, 2, 1, 2, 1]),
  7145. Int32Array.from([1, 1, 1, 3, 2, 3]),
  7146. Int32Array.from([1, 3, 1, 1, 2, 3]),
  7147. Int32Array.from([1, 3, 1, 3, 2, 1]),
  7148. Int32Array.from([1, 1, 2, 3, 1, 3]),
  7149. Int32Array.from([1, 3, 2, 1, 1, 3]),
  7150. Int32Array.from([1, 3, 2, 3, 1, 1]),
  7151. Int32Array.from([2, 1, 1, 3, 1, 3]),
  7152. Int32Array.from([2, 3, 1, 1, 1, 3]),
  7153. Int32Array.from([2, 3, 1, 3, 1, 1]),
  7154. Int32Array.from([1, 1, 2, 1, 3, 3]),
  7155. Int32Array.from([1, 1, 2, 3, 3, 1]),
  7156. Int32Array.from([1, 3, 2, 1, 3, 1]),
  7157. Int32Array.from([1, 1, 3, 1, 2, 3]),
  7158. Int32Array.from([1, 1, 3, 3, 2, 1]),
  7159. Int32Array.from([1, 3, 3, 1, 2, 1]),
  7160. Int32Array.from([3, 1, 3, 1, 2, 1]),
  7161. Int32Array.from([2, 1, 1, 3, 3, 1]),
  7162. Int32Array.from([2, 3, 1, 1, 3, 1]),
  7163. Int32Array.from([2, 1, 3, 1, 1, 3]),
  7164. Int32Array.from([2, 1, 3, 3, 1, 1]),
  7165. Int32Array.from([2, 1, 3, 1, 3, 1]),
  7166. Int32Array.from([3, 1, 1, 1, 2, 3]),
  7167. Int32Array.from([3, 1, 1, 3, 2, 1]),
  7168. Int32Array.from([3, 3, 1, 1, 2, 1]),
  7169. Int32Array.from([3, 1, 2, 1, 1, 3]),
  7170. Int32Array.from([3, 1, 2, 3, 1, 1]),
  7171. Int32Array.from([3, 3, 2, 1, 1, 1]),
  7172. Int32Array.from([3, 1, 4, 1, 1, 1]),
  7173. Int32Array.from([2, 2, 1, 4, 1, 1]),
  7174. Int32Array.from([4, 3, 1, 1, 1, 1]),
  7175. Int32Array.from([1, 1, 1, 2, 2, 4]),
  7176. Int32Array.from([1, 1, 1, 4, 2, 2]),
  7177. Int32Array.from([1, 2, 1, 1, 2, 4]),
  7178. Int32Array.from([1, 2, 1, 4, 2, 1]),
  7179. Int32Array.from([1, 4, 1, 1, 2, 2]),
  7180. Int32Array.from([1, 4, 1, 2, 2, 1]),
  7181. Int32Array.from([1, 1, 2, 2, 1, 4]),
  7182. Int32Array.from([1, 1, 2, 4, 1, 2]),
  7183. Int32Array.from([1, 2, 2, 1, 1, 4]),
  7184. Int32Array.from([1, 2, 2, 4, 1, 1]),
  7185. Int32Array.from([1, 4, 2, 1, 1, 2]),
  7186. Int32Array.from([1, 4, 2, 2, 1, 1]),
  7187. Int32Array.from([2, 4, 1, 2, 1, 1]),
  7188. Int32Array.from([2, 2, 1, 1, 1, 4]),
  7189. Int32Array.from([4, 1, 3, 1, 1, 1]),
  7190. Int32Array.from([2, 4, 1, 1, 1, 2]),
  7191. Int32Array.from([1, 3, 4, 1, 1, 1]),
  7192. Int32Array.from([1, 1, 1, 2, 4, 2]),
  7193. Int32Array.from([1, 2, 1, 1, 4, 2]),
  7194. Int32Array.from([1, 2, 1, 2, 4, 1]),
  7195. Int32Array.from([1, 1, 4, 2, 1, 2]),
  7196. Int32Array.from([1, 2, 4, 1, 1, 2]),
  7197. Int32Array.from([1, 2, 4, 2, 1, 1]),
  7198. Int32Array.from([4, 1, 1, 2, 1, 2]),
  7199. Int32Array.from([4, 2, 1, 1, 1, 2]),
  7200. Int32Array.from([4, 2, 1, 2, 1, 1]),
  7201. Int32Array.from([2, 1, 2, 1, 4, 1]),
  7202. Int32Array.from([2, 1, 4, 1, 2, 1]),
  7203. Int32Array.from([4, 1, 2, 1, 2, 1]),
  7204. Int32Array.from([1, 1, 1, 1, 4, 3]),
  7205. Int32Array.from([1, 1, 1, 3, 4, 1]),
  7206. Int32Array.from([1, 3, 1, 1, 4, 1]),
  7207. Int32Array.from([1, 1, 4, 1, 1, 3]),
  7208. Int32Array.from([1, 1, 4, 3, 1, 1]),
  7209. Int32Array.from([4, 1, 1, 1, 1, 3]),
  7210. Int32Array.from([4, 1, 1, 3, 1, 1]),
  7211. Int32Array.from([1, 1, 3, 1, 4, 1]),
  7212. Int32Array.from([1, 1, 4, 1, 3, 1]),
  7213. Int32Array.from([3, 1, 1, 1, 4, 1]),
  7214. Int32Array.from([4, 1, 1, 1, 3, 1]),
  7215. Int32Array.from([2, 1, 1, 4, 1, 2]),
  7216. Int32Array.from([2, 1, 1, 2, 1, 4]),
  7217. Int32Array.from([2, 1, 1, 2, 3, 2]),
  7218. Int32Array.from([2, 3, 3, 1, 1, 1, 2]),
  7219. ];
  7220. Code128Reader.MAX_AVG_VARIANCE = 0.25;
  7221. Code128Reader.MAX_INDIVIDUAL_VARIANCE = 0.7;
  7222. Code128Reader.CODE_SHIFT = 98;
  7223. Code128Reader.CODE_CODE_C = 99;
  7224. Code128Reader.CODE_CODE_B = 100;
  7225. Code128Reader.CODE_CODE_A = 101;
  7226. Code128Reader.CODE_FNC_1 = 102;
  7227. Code128Reader.CODE_FNC_2 = 97;
  7228. Code128Reader.CODE_FNC_3 = 96;
  7229. Code128Reader.CODE_FNC_4_A = 101;
  7230. Code128Reader.CODE_FNC_4_B = 100;
  7231. Code128Reader.CODE_START_A = 103;
  7232. Code128Reader.CODE_START_B = 104;
  7233. Code128Reader.CODE_START_C = 105;
  7234. Code128Reader.CODE_STOP = 106;
  7235. /*
  7236. * Copyright 2008 ZXing authors
  7237. *
  7238. * Licensed under the Apache License, Version 2.0 (the "License");
  7239. * you may not use this file except in compliance with the License.
  7240. * You may obtain a copy of the License at
  7241. *
  7242. * http://www.apache.org/licenses/LICENSE-2.0
  7243. *
  7244. * Unless required by applicable law or agreed to in writing, software
  7245. * distributed under the License is distributed on an "AS IS" BASIS,
  7246. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7247. * See the License for the specific language governing permissions and
  7248. * limitations under the License.
  7249. */
  7250. /**
  7251. * <p>Decodes Code 39 barcodes. Supports "Full ASCII Code 39" if USE_CODE_39_EXTENDED_MODE is set.</p>
  7252. *
  7253. * @author Sean Owen
  7254. * @see Code93Reader
  7255. */
  7256. class Code39Reader extends OneDReader {
  7257. /**
  7258. * Creates a reader that assumes all encoded data is data, and does not treat the final
  7259. * character as a check digit. It will not decoded "extended Code 39" sequences.
  7260. */
  7261. // public Code39Reader() {
  7262. // this(false);
  7263. // }
  7264. /**
  7265. * Creates a reader that can be configured to check the last character as a check digit.
  7266. * It will not decoded "extended Code 39" sequences.
  7267. *
  7268. * @param usingCheckDigit if true, treat the last data character as a check digit, not
  7269. * data, and verify that the checksum passes.
  7270. */
  7271. // public Code39Reader(boolean usingCheckDigit) {
  7272. // this(usingCheckDigit, false);
  7273. // }
  7274. /**
  7275. * Creates a reader that can be configured to check the last character as a check digit,
  7276. * or optionally attempt to decode "extended Code 39" sequences that are used to encode
  7277. * the full ASCII character set.
  7278. *
  7279. * @param usingCheckDigit if true, treat the last data character as a check digit, not
  7280. * data, and verify that the checksum passes.
  7281. * @param extendedMode if true, will attempt to decode extended Code 39 sequences in the
  7282. * text.
  7283. */
  7284. constructor(usingCheckDigit = false, extendedMode = false) {
  7285. super();
  7286. this.usingCheckDigit = usingCheckDigit;
  7287. this.extendedMode = extendedMode;
  7288. this.decodeRowResult = '';
  7289. this.counters = new Int32Array(9);
  7290. }
  7291. decodeRow(rowNumber, row, hints) {
  7292. let theCounters = this.counters;
  7293. theCounters.fill(0);
  7294. this.decodeRowResult = '';
  7295. let start = Code39Reader.findAsteriskPattern(row, theCounters);
  7296. // Read off white space
  7297. let nextStart = row.getNextSet(start[1]);
  7298. let end = row.getSize();
  7299. let decodedChar;
  7300. let lastStart;
  7301. do {
  7302. Code39Reader.recordPattern(row, nextStart, theCounters);
  7303. let pattern = Code39Reader.toNarrowWidePattern(theCounters);
  7304. if (pattern < 0) {
  7305. throw new NotFoundException();
  7306. }
  7307. decodedChar = Code39Reader.patternToChar(pattern);
  7308. this.decodeRowResult += decodedChar;
  7309. lastStart = nextStart;
  7310. for (let counter of theCounters) {
  7311. nextStart += counter;
  7312. }
  7313. // Read off white space
  7314. nextStart = row.getNextSet(nextStart);
  7315. } while (decodedChar !== '*');
  7316. this.decodeRowResult = this.decodeRowResult.substring(0, this.decodeRowResult.length - 1); // remove asterisk
  7317. // Look for whitespace after pattern:
  7318. let lastPatternSize = 0;
  7319. for (let counter of theCounters) {
  7320. lastPatternSize += counter;
  7321. }
  7322. let whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
  7323. // If 50% of last pattern size, following last pattern, is not whitespace, fail
  7324. // (but if it's whitespace to the very end of the image, that's OK)
  7325. if (nextStart !== end && (whiteSpaceAfterEnd * 2) < lastPatternSize) {
  7326. throw new NotFoundException();
  7327. }
  7328. if (this.usingCheckDigit) {
  7329. let max = this.decodeRowResult.length - 1;
  7330. let total = 0;
  7331. for (let i = 0; i < max; i++) {
  7332. total += Code39Reader.ALPHABET_STRING.indexOf(this.decodeRowResult.charAt(i));
  7333. }
  7334. if (this.decodeRowResult.charAt(max) !== Code39Reader.ALPHABET_STRING.charAt(total % 43)) {
  7335. throw new ChecksumException();
  7336. }
  7337. this.decodeRowResult = this.decodeRowResult.substring(0, max);
  7338. }
  7339. if (this.decodeRowResult.length === 0) {
  7340. // false positive
  7341. throw new NotFoundException();
  7342. }
  7343. let resultString;
  7344. if (this.extendedMode) {
  7345. resultString = Code39Reader.decodeExtended(this.decodeRowResult);
  7346. }
  7347. else {
  7348. resultString = this.decodeRowResult;
  7349. }
  7350. let left = (start[1] + start[0]) / 2.0;
  7351. let right = lastStart + lastPatternSize / 2.0;
  7352. return new Result$1(resultString, null, 0, [new ResultPoint(left, rowNumber), new ResultPoint(right, rowNumber)], BarcodeFormat$1.CODE_39, new Date().getTime());
  7353. }
  7354. static findAsteriskPattern(row, counters) {
  7355. let width = row.getSize();
  7356. let rowOffset = row.getNextSet(0);
  7357. let counterPosition = 0;
  7358. let patternStart = rowOffset;
  7359. let isWhite = false;
  7360. let patternLength = counters.length;
  7361. for (let i = rowOffset; i < width; i++) {
  7362. if (row.get(i) !== isWhite) {
  7363. counters[counterPosition]++;
  7364. }
  7365. else {
  7366. if (counterPosition === patternLength - 1) {
  7367. // Look for whitespace before start pattern, >= 50% of width of start pattern
  7368. if (this.toNarrowWidePattern(counters) === Code39Reader.ASTERISK_ENCODING &&
  7369. row.isRange(Math.max(0, patternStart - Math.floor((i - patternStart) / 2)), patternStart, false)) {
  7370. return [patternStart, i];
  7371. }
  7372. patternStart += counters[0] + counters[1];
  7373. counters.copyWithin(0, 2, 2 + counterPosition - 1);
  7374. counters[counterPosition - 1] = 0;
  7375. counters[counterPosition] = 0;
  7376. counterPosition--;
  7377. }
  7378. else {
  7379. counterPosition++;
  7380. }
  7381. counters[counterPosition] = 1;
  7382. isWhite = !isWhite;
  7383. }
  7384. }
  7385. throw new NotFoundException();
  7386. }
  7387. // For efficiency, returns -1 on failure. Not throwing here saved as many as 700 exceptions
  7388. // per image when using some of our blackbox images.
  7389. static toNarrowWidePattern(counters) {
  7390. let numCounters = counters.length;
  7391. let maxNarrowCounter = 0;
  7392. let wideCounters;
  7393. do {
  7394. let minCounter = 0x7fffffff;
  7395. for (let counter of counters) {
  7396. if (counter < minCounter && counter > maxNarrowCounter) {
  7397. minCounter = counter;
  7398. }
  7399. }
  7400. maxNarrowCounter = minCounter;
  7401. wideCounters = 0;
  7402. let totalWideCountersWidth = 0;
  7403. let pattern = 0;
  7404. for (let i = 0; i < numCounters; i++) {
  7405. let counter = counters[i];
  7406. if (counter > maxNarrowCounter) {
  7407. pattern |= 1 << (numCounters - 1 - i);
  7408. wideCounters++;
  7409. totalWideCountersWidth += counter;
  7410. }
  7411. }
  7412. if (wideCounters === 3) {
  7413. // Found 3 wide counters, but are they close enough in width?
  7414. // We can perform a cheap, conservative check to see if any individual
  7415. // counter is more than 1.5 times the average:
  7416. for (let i = 0; i < numCounters && wideCounters > 0; i++) {
  7417. let counter = counters[i];
  7418. if (counter > maxNarrowCounter) {
  7419. wideCounters--;
  7420. // totalWideCountersWidth = 3 * average, so this checks if counter >= 3/2 * average
  7421. if ((counter * 2) >= totalWideCountersWidth) {
  7422. return -1;
  7423. }
  7424. }
  7425. }
  7426. return pattern;
  7427. }
  7428. } while (wideCounters > 3);
  7429. return -1;
  7430. }
  7431. static patternToChar(pattern) {
  7432. for (let i = 0; i < Code39Reader.CHARACTER_ENCODINGS.length; i++) {
  7433. if (Code39Reader.CHARACTER_ENCODINGS[i] === pattern) {
  7434. return Code39Reader.ALPHABET_STRING.charAt(i);
  7435. }
  7436. }
  7437. if (pattern === Code39Reader.ASTERISK_ENCODING) {
  7438. return '*';
  7439. }
  7440. throw new NotFoundException();
  7441. }
  7442. static decodeExtended(encoded) {
  7443. let length = encoded.length;
  7444. let decoded = '';
  7445. for (let i = 0; i < length; i++) {
  7446. let c = encoded.charAt(i);
  7447. if (c === '+' || c === '$' || c === '%' || c === '/') {
  7448. let next = encoded.charAt(i + 1);
  7449. let decodedChar = '\0';
  7450. switch (c) {
  7451. case '+':
  7452. // +A to +Z map to a to z
  7453. if (next >= 'A' && next <= 'Z') {
  7454. decodedChar = String.fromCharCode(next.charCodeAt(0) + 32);
  7455. }
  7456. else {
  7457. throw new FormatException();
  7458. }
  7459. break;
  7460. case '$':
  7461. // $A to $Z map to control codes SH to SB
  7462. if (next >= 'A' && next <= 'Z') {
  7463. decodedChar = String.fromCharCode(next.charCodeAt(0) - 64);
  7464. }
  7465. else {
  7466. throw new FormatException();
  7467. }
  7468. break;
  7469. case '%':
  7470. // %A to %E map to control codes ESC to US
  7471. if (next >= 'A' && next <= 'E') {
  7472. decodedChar = String.fromCharCode(next.charCodeAt(0) - 38);
  7473. }
  7474. else if (next >= 'F' && next <= 'J') {
  7475. decodedChar = String.fromCharCode(next.charCodeAt(0) - 11);
  7476. }
  7477. else if (next >= 'K' && next <= 'O') {
  7478. decodedChar = String.fromCharCode(next.charCodeAt(0) + 16);
  7479. }
  7480. else if (next >= 'P' && next <= 'T') {
  7481. decodedChar = String.fromCharCode(next.charCodeAt(0) + 43);
  7482. }
  7483. else if (next === 'U') {
  7484. decodedChar = '\0';
  7485. }
  7486. else if (next === 'V') {
  7487. decodedChar = '@';
  7488. }
  7489. else if (next === 'W') {
  7490. decodedChar = '`';
  7491. }
  7492. else if (next === 'X' || next === 'Y' || next === 'Z') {
  7493. decodedChar = '\x7f';
  7494. }
  7495. else {
  7496. throw new FormatException();
  7497. }
  7498. break;
  7499. case '/':
  7500. // /A to /O map to ! to , and /Z maps to :
  7501. if (next >= 'A' && next <= 'O') {
  7502. decodedChar = String.fromCharCode(next.charCodeAt(0) - 32);
  7503. }
  7504. else if (next === 'Z') {
  7505. decodedChar = ':';
  7506. }
  7507. else {
  7508. throw new FormatException();
  7509. }
  7510. break;
  7511. }
  7512. decoded += decodedChar;
  7513. // bump up i again since we read two characters
  7514. i++;
  7515. }
  7516. else {
  7517. decoded += c;
  7518. }
  7519. }
  7520. return decoded;
  7521. }
  7522. }
  7523. Code39Reader.ALPHABET_STRING = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%';
  7524. /**
  7525. * These represent the encodings of characters, as patterns of wide and narrow bars.
  7526. * The 9 least-significant bits of each int correspond to the pattern of wide and narrow,
  7527. * with 1s representing "wide" and 0s representing narrow.
  7528. */
  7529. Code39Reader.CHARACTER_ENCODINGS = [
  7530. 0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064,
  7531. 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C,
  7532. 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016,
  7533. 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, 0x085, 0x184, 0x0C4, 0x0A8,
  7534. 0x0A2, 0x08A, 0x02A // /-%
  7535. ];
  7536. Code39Reader.ASTERISK_ENCODING = 0x094;
  7537. /*
  7538. * Copyright 2010 ZXing authors
  7539. *
  7540. * Licensed under the Apache License, Version 2.0 (the "License");
  7541. * you may not use this file except in compliance with the License.
  7542. * You may obtain a copy of the License at
  7543. *
  7544. * http://www.apache.org/licenses/LICENSE-2.0
  7545. *
  7546. * Unless required by applicable law or agreed to in writing, software
  7547. * distributed under the License is distributed on an "AS IS" BASIS,
  7548. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7549. * See the License for the specific language governing permissions and
  7550. * limitations under the License.
  7551. */
  7552. /**
  7553. * <p>Decodes Code 93 barcodes.</p>
  7554. *
  7555. * @author Sean Owen
  7556. * @see Code39Reader
  7557. */
  7558. class Code93Reader extends OneDReader {
  7559. //public Code93Reader() {
  7560. // decodeRowResult = new StringBuilder(20);
  7561. // counters = new int[6];
  7562. //}
  7563. constructor() {
  7564. super();
  7565. this.decodeRowResult = '';
  7566. this.counters = new Int32Array(6);
  7567. }
  7568. decodeRow(rowNumber, row, hints) {
  7569. let start = this.findAsteriskPattern(row);
  7570. // Read off white space
  7571. let nextStart = row.getNextSet(start[1]);
  7572. let end = row.getSize();
  7573. let theCounters = this.counters;
  7574. theCounters.fill(0);
  7575. this.decodeRowResult = '';
  7576. let decodedChar;
  7577. let lastStart;
  7578. do {
  7579. Code93Reader.recordPattern(row, nextStart, theCounters);
  7580. let pattern = this.toPattern(theCounters);
  7581. if (pattern < 0) {
  7582. throw new NotFoundException();
  7583. }
  7584. decodedChar = this.patternToChar(pattern);
  7585. this.decodeRowResult += decodedChar;
  7586. lastStart = nextStart;
  7587. for (let counter of theCounters) {
  7588. nextStart += counter;
  7589. }
  7590. // Read off white space
  7591. nextStart = row.getNextSet(nextStart);
  7592. } while (decodedChar !== '*');
  7593. this.decodeRowResult = this.decodeRowResult.substring(0, this.decodeRowResult.length - 1); // remove asterisk
  7594. let lastPatternSize = 0;
  7595. for (let counter of theCounters) {
  7596. lastPatternSize += counter;
  7597. }
  7598. // Should be at least one more black module
  7599. if (nextStart === end || !row.get(nextStart)) {
  7600. throw new NotFoundException();
  7601. }
  7602. if (this.decodeRowResult.length < 2) {
  7603. // false positive -- need at least 2 checksum digits
  7604. throw new NotFoundException();
  7605. }
  7606. this.checkChecksums(this.decodeRowResult);
  7607. // Remove checksum digits
  7608. this.decodeRowResult = this.decodeRowResult.substring(0, this.decodeRowResult.length - 2);
  7609. let resultString = this.decodeExtended(this.decodeRowResult);
  7610. let left = (start[1] + start[0]) / 2.0;
  7611. let right = lastStart + lastPatternSize / 2.0;
  7612. return new Result$1(resultString, null, 0, [new ResultPoint(left, rowNumber), new ResultPoint(right, rowNumber)], BarcodeFormat$1.CODE_93, new Date().getTime());
  7613. }
  7614. findAsteriskPattern(row) {
  7615. let width = row.getSize();
  7616. let rowOffset = row.getNextSet(0);
  7617. this.counters.fill(0);
  7618. let theCounters = this.counters;
  7619. let patternStart = rowOffset;
  7620. let isWhite = false;
  7621. let patternLength = theCounters.length;
  7622. let counterPosition = 0;
  7623. for (let i = rowOffset; i < width; i++) {
  7624. if (row.get(i) !== isWhite) {
  7625. theCounters[counterPosition]++;
  7626. }
  7627. else {
  7628. if (counterPosition === patternLength - 1) {
  7629. if (this.toPattern(theCounters) === Code93Reader.ASTERISK_ENCODING) {
  7630. return new Int32Array([patternStart, i]);
  7631. }
  7632. patternStart += theCounters[0] + theCounters[1];
  7633. theCounters.copyWithin(0, 2, 2 + counterPosition - 1);
  7634. theCounters[counterPosition - 1] = 0;
  7635. theCounters[counterPosition] = 0;
  7636. counterPosition--;
  7637. }
  7638. else {
  7639. counterPosition++;
  7640. }
  7641. theCounters[counterPosition] = 1;
  7642. isWhite = !isWhite;
  7643. }
  7644. }
  7645. throw new NotFoundException;
  7646. }
  7647. toPattern(counters) {
  7648. let sum = 0;
  7649. for (const counter of counters) {
  7650. sum += counter;
  7651. }
  7652. let pattern = 0;
  7653. let max = counters.length;
  7654. for (let i = 0; i < max; i++) {
  7655. let scaled = Math.round(counters[i] * 9.0 / sum);
  7656. if (scaled < 1 || scaled > 4) {
  7657. return -1;
  7658. }
  7659. if ((i & 0x01) === 0) {
  7660. for (let j = 0; j < scaled; j++) {
  7661. pattern = (pattern << 1) | 0x01;
  7662. }
  7663. }
  7664. else {
  7665. pattern <<= scaled;
  7666. }
  7667. }
  7668. return pattern;
  7669. }
  7670. patternToChar(pattern) {
  7671. for (let i = 0; i < Code93Reader.CHARACTER_ENCODINGS.length; i++) {
  7672. if (Code93Reader.CHARACTER_ENCODINGS[i] === pattern) {
  7673. return Code93Reader.ALPHABET_STRING.charAt(i);
  7674. }
  7675. }
  7676. throw new NotFoundException();
  7677. }
  7678. decodeExtended(encoded) {
  7679. let length = encoded.length;
  7680. let decoded = '';
  7681. for (let i = 0; i < length; i++) {
  7682. let c = encoded.charAt(i);
  7683. if (c >= 'a' && c <= 'd') {
  7684. if (i >= length - 1) {
  7685. throw new FormatException();
  7686. }
  7687. let next = encoded.charAt(i + 1);
  7688. let decodedChar = '\0';
  7689. switch (c) {
  7690. case 'd':
  7691. // +A to +Z map to a to z
  7692. if (next >= 'A' && next <= 'Z') {
  7693. decodedChar = String.fromCharCode(next.charCodeAt(0) + 32);
  7694. }
  7695. else {
  7696. throw new FormatException();
  7697. }
  7698. break;
  7699. case 'a':
  7700. // $A to $Z map to control codes SH to SB
  7701. if (next >= 'A' && next <= 'Z') {
  7702. decodedChar = String.fromCharCode(next.charCodeAt(0) - 64);
  7703. }
  7704. else {
  7705. throw new FormatException();
  7706. }
  7707. break;
  7708. case 'b':
  7709. if (next >= 'A' && next <= 'E') {
  7710. // %A to %E map to control codes ESC to USep
  7711. decodedChar = String.fromCharCode(next.charCodeAt(0) - 38);
  7712. }
  7713. else if (next >= 'F' && next <= 'J') {
  7714. // %F to %J map to ; < = > ?
  7715. decodedChar = String.fromCharCode(next.charCodeAt(0) - 11);
  7716. }
  7717. else if (next >= 'K' && next <= 'O') {
  7718. // %K to %O map to [ \ ] ^ _
  7719. decodedChar = String.fromCharCode(next.charCodeAt(0) + 16);
  7720. }
  7721. else if (next >= 'P' && next <= 'T') {
  7722. // %P to %T map to { | } ~ DEL
  7723. decodedChar = String.fromCharCode(next.charCodeAt(0) + 43);
  7724. }
  7725. else if (next === 'U') {
  7726. // %U map to NUL
  7727. decodedChar = '\0';
  7728. }
  7729. else if (next === 'V') {
  7730. // %V map to @
  7731. decodedChar = '@';
  7732. }
  7733. else if (next === 'W') {
  7734. // %W map to `
  7735. decodedChar = '`';
  7736. }
  7737. else if (next >= 'X' && next <= 'Z') {
  7738. // %X to %Z all map to DEL (127)
  7739. decodedChar = String.fromCharCode(127);
  7740. }
  7741. else {
  7742. throw new FormatException();
  7743. }
  7744. break;
  7745. case 'c':
  7746. // /A to /O map to ! to , and /Z maps to :
  7747. if (next >= 'A' && next <= 'O') {
  7748. decodedChar = String.fromCharCode(next.charCodeAt(0) - 32);
  7749. }
  7750. else if (next === 'Z') {
  7751. decodedChar = ':';
  7752. }
  7753. else {
  7754. throw new FormatException();
  7755. }
  7756. break;
  7757. }
  7758. decoded += decodedChar;
  7759. // bump up i again since we read two characters
  7760. i++;
  7761. }
  7762. else {
  7763. decoded += c;
  7764. }
  7765. }
  7766. return decoded;
  7767. }
  7768. checkChecksums(result) {
  7769. let length = result.length;
  7770. this.checkOneChecksum(result, length - 2, 20);
  7771. this.checkOneChecksum(result, length - 1, 15);
  7772. }
  7773. checkOneChecksum(result, checkPosition, weightMax) {
  7774. let weight = 1;
  7775. let total = 0;
  7776. for (let i = checkPosition - 1; i >= 0; i--) {
  7777. total += weight * Code93Reader.ALPHABET_STRING.indexOf(result.charAt(i));
  7778. if (++weight > weightMax) {
  7779. weight = 1;
  7780. }
  7781. }
  7782. if (result.charAt(checkPosition) !== Code93Reader.ALPHABET_STRING[total % 47]) {
  7783. throw new ChecksumException;
  7784. }
  7785. }
  7786. }
  7787. // Note that 'abcd' are dummy characters in place of control characters.
  7788. Code93Reader.ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%abcd*";
  7789. /**
  7790. * These represent the encodings of characters, as patterns of wide and narrow bars.
  7791. * The 9 least-significant bits of each int correspond to the pattern of wide and narrow.
  7792. */
  7793. Code93Reader.CHARACTER_ENCODINGS = [
  7794. 0x114, 0x148, 0x144, 0x142, 0x128, 0x124, 0x122, 0x150, 0x112, 0x10A,
  7795. 0x1A8, 0x1A4, 0x1A2, 0x194, 0x192, 0x18A, 0x168, 0x164, 0x162, 0x134,
  7796. 0x11A, 0x158, 0x14C, 0x146, 0x12C, 0x116, 0x1B4, 0x1B2, 0x1AC, 0x1A6,
  7797. 0x196, 0x19A, 0x16C, 0x166, 0x136, 0x13A,
  7798. 0x12E, 0x1D4, 0x1D2, 0x1CA, 0x16E, 0x176, 0x1AE,
  7799. 0x126, 0x1DA, 0x1D6, 0x132, 0x15E,
  7800. ];
  7801. Code93Reader.ASTERISK_ENCODING = Code93Reader.CHARACTER_ENCODINGS[47];
  7802. /*
  7803. * Copyright 2008 ZXing authors
  7804. *
  7805. * Licensed under the Apache License, Version 2.0 (the "License");
  7806. * you may not use this file except in compliance with the License.
  7807. * You may obtain a copy of the License at
  7808. *
  7809. * http://www.apache.org/licenses/LICENSE-2.0
  7810. *
  7811. * Unless required by applicable law or agreed to in writing, software
  7812. * distributed under the License is distributed on an "AS IS" BASIS,
  7813. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7814. * See the License for the specific language governing permissions and
  7815. * limitations under the License.
  7816. */
  7817. /**
  7818. * <p>Decodes ITF barcodes.</p>
  7819. *
  7820. * @author Tjieco
  7821. */
  7822. class ITFReader extends OneDReader {
  7823. constructor() {
  7824. // private static W = 3; // Pixel width of a 3x wide line
  7825. // private static w = 2; // Pixel width of a 2x wide line
  7826. // private static N = 1; // Pixed width of a narrow line
  7827. super(...arguments);
  7828. // Stores the actual narrow line width of the image being decoded.
  7829. this.narrowLineWidth = -1;
  7830. }
  7831. // See ITFWriter.PATTERNS
  7832. /*
  7833. /!**
  7834. * Patterns of Wide / Narrow lines to indicate each digit
  7835. *!/
  7836. */
  7837. decodeRow(rowNumber, row, hints) {
  7838. // Find out where the Middle section (payload) starts & ends
  7839. let startRange = this.decodeStart(row);
  7840. let endRange = this.decodeEnd(row);
  7841. let result = new StringBuilder();
  7842. ITFReader.decodeMiddle(row, startRange[1], endRange[0], result);
  7843. let resultString = result.toString();
  7844. let allowedLengths = null;
  7845. if (hints != null) {
  7846. allowedLengths = hints.get(DecodeHintType$1.ALLOWED_LENGTHS);
  7847. }
  7848. if (allowedLengths == null) {
  7849. allowedLengths = ITFReader.DEFAULT_ALLOWED_LENGTHS;
  7850. }
  7851. // To avoid false positives with 2D barcodes (and other patterns), make
  7852. // an assumption that the decoded string must be a 'standard' length if it's short
  7853. let length = resultString.length;
  7854. let lengthOK = false;
  7855. let maxAllowedLength = 0;
  7856. for (let value of allowedLengths) {
  7857. if (length === value) {
  7858. lengthOK = true;
  7859. break;
  7860. }
  7861. if (value > maxAllowedLength) {
  7862. maxAllowedLength = value;
  7863. }
  7864. }
  7865. if (!lengthOK && length > maxAllowedLength) {
  7866. lengthOK = true;
  7867. }
  7868. if (!lengthOK) {
  7869. throw new FormatException();
  7870. }
  7871. const points = [new ResultPoint(startRange[1], rowNumber), new ResultPoint(endRange[0], rowNumber)];
  7872. let resultReturn = new Result$1(resultString, null, // no natural byte representation for these barcodes
  7873. 0, points, BarcodeFormat$1.ITF, new Date().getTime());
  7874. return resultReturn;
  7875. }
  7876. /*
  7877. /!**
  7878. * @param row row of black/white values to search
  7879. * @param payloadStart offset of start pattern
  7880. * @param resultString {@link StringBuilder} to append decoded chars to
  7881. * @throws NotFoundException if decoding could not complete successfully
  7882. *!/*/
  7883. static decodeMiddle(row, payloadStart, payloadEnd, resultString) {
  7884. // Digits are interleaved in pairs - 5 black lines for one digit, and the
  7885. // 5
  7886. // interleaved white lines for the second digit.
  7887. // Therefore, need to scan 10 lines and then
  7888. // split these into two arrays
  7889. let counterDigitPair = new Int32Array(10); // 10
  7890. let counterBlack = new Int32Array(5); // 5
  7891. let counterWhite = new Int32Array(5); // 5
  7892. counterDigitPair.fill(0);
  7893. counterBlack.fill(0);
  7894. counterWhite.fill(0);
  7895. while (payloadStart < payloadEnd) {
  7896. // Get 10 runs of black/white.
  7897. OneDReader.recordPattern(row, payloadStart, counterDigitPair);
  7898. // Split them into each array
  7899. for (let k = 0; k < 5; k++) {
  7900. let twoK = 2 * k;
  7901. counterBlack[k] = counterDigitPair[twoK];
  7902. counterWhite[k] = counterDigitPair[twoK + 1];
  7903. }
  7904. let bestMatch = ITFReader.decodeDigit(counterBlack);
  7905. resultString.append(bestMatch.toString());
  7906. bestMatch = this.decodeDigit(counterWhite);
  7907. resultString.append(bestMatch.toString());
  7908. counterDigitPair.forEach(function (counterDigit) {
  7909. payloadStart += counterDigit;
  7910. });
  7911. }
  7912. }
  7913. /*/!**
  7914. * Identify where the start of the middle / payload section starts.
  7915. *
  7916. * @param row row of black/white values to search
  7917. * @return Array, containing index of start of 'start block' and end of
  7918. * 'start block'
  7919. *!/*/
  7920. decodeStart(row) {
  7921. let endStart = ITFReader.skipWhiteSpace(row);
  7922. let startPattern = ITFReader.findGuardPattern(row, endStart, ITFReader.START_PATTERN);
  7923. // Determine the width of a narrow line in pixels. We can do this by
  7924. // getting the width of the start pattern and dividing by 4 because its
  7925. // made up of 4 narrow lines.
  7926. this.narrowLineWidth = (startPattern[1] - startPattern[0]) / 4;
  7927. this.validateQuietZone(row, startPattern[0]);
  7928. return startPattern;
  7929. }
  7930. /*/!**
  7931. * The start & end patterns must be pre/post fixed by a quiet zone. This
  7932. * zone must be at least 10 times the width of a narrow line. Scan back until
  7933. * we either get to the start of the barcode or match the necessary number of
  7934. * quiet zone pixels.
  7935. *
  7936. * Note: Its assumed the row is reversed when using this method to find
  7937. * quiet zone after the end pattern.
  7938. *
  7939. * ref: http://www.barcode-1.net/i25code.html
  7940. *
  7941. * @param row bit array representing the scanned barcode.
  7942. * @param startPattern index into row of the start or end pattern.
  7943. * @throws NotFoundException if the quiet zone cannot be found
  7944. *!/*/
  7945. validateQuietZone(row, startPattern) {
  7946. let quietCount = this.narrowLineWidth * 10; // expect to find this many pixels of quiet zone
  7947. // if there are not so many pixel at all let's try as many as possible
  7948. quietCount = quietCount < startPattern ? quietCount : startPattern;
  7949. for (let i = startPattern - 1; quietCount > 0 && i >= 0; i--) {
  7950. if (row.get(i)) {
  7951. break;
  7952. }
  7953. quietCount--;
  7954. }
  7955. if (quietCount !== 0) {
  7956. // Unable to find the necessary number of quiet zone pixels.
  7957. throw new NotFoundException();
  7958. }
  7959. }
  7960. /*
  7961. /!**
  7962. * Skip all whitespace until we get to the first black line.
  7963. *
  7964. * @param row row of black/white values to search
  7965. * @return index of the first black line.
  7966. * @throws NotFoundException Throws exception if no black lines are found in the row
  7967. *!/*/
  7968. static skipWhiteSpace(row) {
  7969. const width = row.getSize();
  7970. const endStart = row.getNextSet(0);
  7971. if (endStart === width) {
  7972. throw new NotFoundException();
  7973. }
  7974. return endStart;
  7975. }
  7976. /*/!**
  7977. * Identify where the end of the middle / payload section ends.
  7978. *
  7979. * @param row row of black/white values to search
  7980. * @return Array, containing index of start of 'end block' and end of 'end
  7981. * block'
  7982. *!/*/
  7983. decodeEnd(row) {
  7984. // For convenience, reverse the row and then
  7985. // search from 'the start' for the end block
  7986. row.reverse();
  7987. try {
  7988. let endStart = ITFReader.skipWhiteSpace(row);
  7989. let endPattern;
  7990. try {
  7991. endPattern = ITFReader.findGuardPattern(row, endStart, ITFReader.END_PATTERN_REVERSED[0]);
  7992. }
  7993. catch (error) {
  7994. if (error instanceof NotFoundException) {
  7995. endPattern = ITFReader.findGuardPattern(row, endStart, ITFReader.END_PATTERN_REVERSED[1]);
  7996. }
  7997. }
  7998. // The start & end patterns must be pre/post fixed by a quiet zone. This
  7999. // zone must be at least 10 times the width of a narrow line.
  8000. // ref: http://www.barcode-1.net/i25code.html
  8001. this.validateQuietZone(row, endPattern[0]);
  8002. // Now recalculate the indices of where the 'endblock' starts & stops to
  8003. // accommodate
  8004. // the reversed nature of the search
  8005. let temp = endPattern[0];
  8006. endPattern[0] = row.getSize() - endPattern[1];
  8007. endPattern[1] = row.getSize() - temp;
  8008. return endPattern;
  8009. }
  8010. finally {
  8011. // Put the row back the right way.
  8012. row.reverse();
  8013. }
  8014. }
  8015. /*
  8016. /!**
  8017. * @param row row of black/white values to search
  8018. * @param rowOffset position to start search
  8019. * @param pattern pattern of counts of number of black and white pixels that are
  8020. * being searched for as a pattern
  8021. * @return start/end horizontal offset of guard pattern, as an array of two
  8022. * ints
  8023. * @throws NotFoundException if pattern is not found
  8024. *!/*/
  8025. static findGuardPattern(row, rowOffset, pattern) {
  8026. let patternLength = pattern.length;
  8027. let counters = new Int32Array(patternLength);
  8028. let width = row.getSize();
  8029. let isWhite = false;
  8030. let counterPosition = 0;
  8031. let patternStart = rowOffset;
  8032. counters.fill(0);
  8033. for (let x = rowOffset; x < width; x++) {
  8034. if (row.get(x) !== isWhite) {
  8035. counters[counterPosition]++;
  8036. }
  8037. else {
  8038. if (counterPosition === patternLength - 1) {
  8039. if (OneDReader.patternMatchVariance(counters, pattern, ITFReader.MAX_INDIVIDUAL_VARIANCE) < ITFReader.MAX_AVG_VARIANCE) {
  8040. return [patternStart, x];
  8041. }
  8042. patternStart += counters[0] + counters[1];
  8043. System.arraycopy(counters, 2, counters, 0, counterPosition - 1);
  8044. counters[counterPosition - 1] = 0;
  8045. counters[counterPosition] = 0;
  8046. counterPosition--;
  8047. }
  8048. else {
  8049. counterPosition++;
  8050. }
  8051. counters[counterPosition] = 1;
  8052. isWhite = !isWhite;
  8053. }
  8054. }
  8055. throw new NotFoundException();
  8056. }
  8057. /*/!**
  8058. * Attempts to decode a sequence of ITF black/white lines into single
  8059. * digit.
  8060. *
  8061. * @param counters the counts of runs of observed black/white/black/... values
  8062. * @return The decoded digit
  8063. * @throws NotFoundException if digit cannot be decoded
  8064. *!/*/
  8065. static decodeDigit(counters) {
  8066. let bestVariance = ITFReader.MAX_AVG_VARIANCE; // worst variance we'll accept
  8067. let bestMatch = -1;
  8068. let max = ITFReader.PATTERNS.length;
  8069. for (let i = 0; i < max; i++) {
  8070. let pattern = ITFReader.PATTERNS[i];
  8071. let variance = OneDReader.patternMatchVariance(counters, pattern, ITFReader.MAX_INDIVIDUAL_VARIANCE);
  8072. if (variance < bestVariance) {
  8073. bestVariance = variance;
  8074. bestMatch = i;
  8075. }
  8076. else if (variance === bestVariance) {
  8077. // if we find a second 'best match' with the same variance, we can not reliably report to have a suitable match
  8078. bestMatch = -1;
  8079. }
  8080. }
  8081. if (bestMatch >= 0) {
  8082. return bestMatch % 10;
  8083. }
  8084. else {
  8085. throw new NotFoundException();
  8086. }
  8087. }
  8088. }
  8089. ITFReader.PATTERNS = [
  8090. Int32Array.from([1, 1, 2, 2, 1]),
  8091. Int32Array.from([2, 1, 1, 1, 2]),
  8092. Int32Array.from([1, 2, 1, 1, 2]),
  8093. Int32Array.from([2, 2, 1, 1, 1]),
  8094. Int32Array.from([1, 1, 2, 1, 2]),
  8095. Int32Array.from([2, 1, 2, 1, 1]),
  8096. Int32Array.from([1, 2, 2, 1, 1]),
  8097. Int32Array.from([1, 1, 1, 2, 2]),
  8098. Int32Array.from([2, 1, 1, 2, 1]),
  8099. Int32Array.from([1, 2, 1, 2, 1]),
  8100. Int32Array.from([1, 1, 3, 3, 1]),
  8101. Int32Array.from([3, 1, 1, 1, 3]),
  8102. Int32Array.from([1, 3, 1, 1, 3]),
  8103. Int32Array.from([3, 3, 1, 1, 1]),
  8104. Int32Array.from([1, 1, 3, 1, 3]),
  8105. Int32Array.from([3, 1, 3, 1, 1]),
  8106. Int32Array.from([1, 3, 3, 1, 1]),
  8107. Int32Array.from([1, 1, 1, 3, 3]),
  8108. Int32Array.from([3, 1, 1, 3, 1]),
  8109. Int32Array.from([1, 3, 1, 3, 1]) // 9
  8110. ];
  8111. ITFReader.MAX_AVG_VARIANCE = 0.38;
  8112. ITFReader.MAX_INDIVIDUAL_VARIANCE = 0.5;
  8113. /* /!** Valid ITF lengths. Anything longer than the largest value is also allowed. *!/*/
  8114. ITFReader.DEFAULT_ALLOWED_LENGTHS = [6, 8, 10, 12, 14];
  8115. /*/!**
  8116. * Start/end guard pattern.
  8117. *
  8118. * Note: The end pattern is reversed because the row is reversed before
  8119. * searching for the END_PATTERN
  8120. *!/*/
  8121. ITFReader.START_PATTERN = Int32Array.from([1, 1, 1, 1]);
  8122. ITFReader.END_PATTERN_REVERSED = [
  8123. Int32Array.from([1, 1, 2]),
  8124. Int32Array.from([1, 1, 3]) // 3x
  8125. ];
  8126. /*
  8127. * Copyright 2008 ZXing authors
  8128. *
  8129. * Licensed under the Apache License, Version 2.0 (the "License");
  8130. * you may not use this file except in compliance with the License.
  8131. * You may obtain a copy of the License at
  8132. *
  8133. * http://www.apache.org/licenses/LICENSE-2.0
  8134. *
  8135. * Unless required by applicable law or agreed to in writing, software
  8136. * distributed under the License is distributed on an "AS IS" BASIS,
  8137. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8138. * See the License for the specific language governing permissions and
  8139. * limitations under the License.
  8140. */
  8141. /**
  8142. * <p>Encapsulates functionality and implementation that is common to UPC and EAN families
  8143. * of one-dimensional barcodes.</p>
  8144. *
  8145. * @author dswitkin@google.com (Daniel Switkin)
  8146. * @author Sean Owen
  8147. * @author alasdair@google.com (Alasdair Mackintosh)
  8148. */
  8149. class AbstractUPCEANReader extends OneDReader {
  8150. constructor() {
  8151. super(...arguments);
  8152. this.decodeRowStringBuffer = '';
  8153. }
  8154. // private final UPCEANExtensionSupport extensionReader;
  8155. // private final EANManufacturerOrgSupport eanManSupport;
  8156. /*
  8157. protected UPCEANReader() {
  8158. decodeRowStringBuffer = new StringBuilder(20);
  8159. extensionReader = new UPCEANExtensionSupport();
  8160. eanManSupport = new EANManufacturerOrgSupport();
  8161. }
  8162. */
  8163. static findStartGuardPattern(row) {
  8164. let foundStart = false;
  8165. let startRange;
  8166. let nextStart = 0;
  8167. let counters = Int32Array.from([0, 0, 0]);
  8168. while (!foundStart) {
  8169. counters = Int32Array.from([0, 0, 0]);
  8170. startRange = AbstractUPCEANReader.findGuardPattern(row, nextStart, false, this.START_END_PATTERN, counters);
  8171. let start = startRange[0];
  8172. nextStart = startRange[1];
  8173. let quietStart = start - (nextStart - start);
  8174. if (quietStart >= 0) {
  8175. foundStart = row.isRange(quietStart, start, false);
  8176. }
  8177. }
  8178. return startRange;
  8179. }
  8180. static checkChecksum(s) {
  8181. return AbstractUPCEANReader.checkStandardUPCEANChecksum(s);
  8182. }
  8183. static checkStandardUPCEANChecksum(s) {
  8184. let length = s.length;
  8185. if (length === 0)
  8186. return false;
  8187. let check = parseInt(s.charAt(length - 1), 10);
  8188. return AbstractUPCEANReader.getStandardUPCEANChecksum(s.substring(0, length - 1)) === check;
  8189. }
  8190. static getStandardUPCEANChecksum(s) {
  8191. let length = s.length;
  8192. let sum = 0;
  8193. for (let i = length - 1; i >= 0; i -= 2) {
  8194. let digit = s.charAt(i).charCodeAt(0) - '0'.charCodeAt(0);
  8195. if (digit < 0 || digit > 9) {
  8196. throw new FormatException();
  8197. }
  8198. sum += digit;
  8199. }
  8200. sum *= 3;
  8201. for (let i = length - 2; i >= 0; i -= 2) {
  8202. let digit = s.charAt(i).charCodeAt(0) - '0'.charCodeAt(0);
  8203. if (digit < 0 || digit > 9) {
  8204. throw new FormatException();
  8205. }
  8206. sum += digit;
  8207. }
  8208. return (1000 - sum) % 10;
  8209. }
  8210. static decodeEnd(row, endStart) {
  8211. return AbstractUPCEANReader.findGuardPattern(row, endStart, false, AbstractUPCEANReader.START_END_PATTERN, new Int32Array(AbstractUPCEANReader.START_END_PATTERN.length).fill(0));
  8212. }
  8213. /**
  8214. * @throws NotFoundException
  8215. */
  8216. static findGuardPatternWithoutCounters(row, rowOffset, whiteFirst, pattern) {
  8217. return this.findGuardPattern(row, rowOffset, whiteFirst, pattern, new Int32Array(pattern.length));
  8218. }
  8219. /**
  8220. * @param row row of black/white values to search
  8221. * @param rowOffset position to start search
  8222. * @param whiteFirst if true, indicates that the pattern specifies white/black/white/...
  8223. * pixel counts, otherwise, it is interpreted as black/white/black/...
  8224. * @param pattern pattern of counts of number of black and white pixels that are being
  8225. * searched for as a pattern
  8226. * @param counters array of counters, as long as pattern, to re-use
  8227. * @return start/end horizontal offset of guard pattern, as an array of two ints
  8228. * @throws NotFoundException if pattern is not found
  8229. */
  8230. static findGuardPattern(row, rowOffset, whiteFirst, pattern, counters) {
  8231. let width = row.getSize();
  8232. rowOffset = whiteFirst ? row.getNextUnset(rowOffset) : row.getNextSet(rowOffset);
  8233. let counterPosition = 0;
  8234. let patternStart = rowOffset;
  8235. let patternLength = pattern.length;
  8236. let isWhite = whiteFirst;
  8237. for (let x = rowOffset; x < width; x++) {
  8238. if (row.get(x) !== isWhite) {
  8239. counters[counterPosition]++;
  8240. }
  8241. else {
  8242. if (counterPosition === patternLength - 1) {
  8243. if (OneDReader.patternMatchVariance(counters, pattern, AbstractUPCEANReader.MAX_INDIVIDUAL_VARIANCE) < AbstractUPCEANReader.MAX_AVG_VARIANCE) {
  8244. return Int32Array.from([patternStart, x]);
  8245. }
  8246. patternStart += counters[0] + counters[1];
  8247. let slice = counters.slice(2, counters.length);
  8248. for (let i = 0; i < counterPosition - 1; i++) {
  8249. counters[i] = slice[i];
  8250. }
  8251. counters[counterPosition - 1] = 0;
  8252. counters[counterPosition] = 0;
  8253. counterPosition--;
  8254. }
  8255. else {
  8256. counterPosition++;
  8257. }
  8258. counters[counterPosition] = 1;
  8259. isWhite = !isWhite;
  8260. }
  8261. }
  8262. throw new NotFoundException();
  8263. }
  8264. static decodeDigit(row, counters, rowOffset, patterns) {
  8265. this.recordPattern(row, rowOffset, counters);
  8266. let bestVariance = this.MAX_AVG_VARIANCE;
  8267. let bestMatch = -1;
  8268. let max = patterns.length;
  8269. for (let i = 0; i < max; i++) {
  8270. let pattern = patterns[i];
  8271. let variance = OneDReader.patternMatchVariance(counters, pattern, AbstractUPCEANReader.MAX_INDIVIDUAL_VARIANCE);
  8272. if (variance < bestVariance) {
  8273. bestVariance = variance;
  8274. bestMatch = i;
  8275. }
  8276. }
  8277. if (bestMatch >= 0) {
  8278. return bestMatch;
  8279. }
  8280. else {
  8281. throw new NotFoundException();
  8282. }
  8283. }
  8284. }
  8285. // These two values are critical for determining how permissive the decoding will be.
  8286. // We've arrived at these values through a lot of trial and error. Setting them any higher
  8287. // lets false positives creep in quickly.
  8288. AbstractUPCEANReader.MAX_AVG_VARIANCE = 0.48;
  8289. AbstractUPCEANReader.MAX_INDIVIDUAL_VARIANCE = 0.7;
  8290. /**
  8291. * Start/end guard pattern.
  8292. */
  8293. AbstractUPCEANReader.START_END_PATTERN = Int32Array.from([1, 1, 1]);
  8294. /**
  8295. * Pattern marking the middle of a UPC/EAN pattern, separating the two halves.
  8296. */
  8297. AbstractUPCEANReader.MIDDLE_PATTERN = Int32Array.from([1, 1, 1, 1, 1]);
  8298. /**
  8299. * end guard pattern.
  8300. */
  8301. AbstractUPCEANReader.END_PATTERN = Int32Array.from([1, 1, 1, 1, 1, 1]);
  8302. /**
  8303. * "Odd", or "L" patterns used to encode UPC/EAN digits.
  8304. */
  8305. AbstractUPCEANReader.L_PATTERNS = [
  8306. Int32Array.from([3, 2, 1, 1]),
  8307. Int32Array.from([2, 2, 2, 1]),
  8308. Int32Array.from([2, 1, 2, 2]),
  8309. Int32Array.from([1, 4, 1, 1]),
  8310. Int32Array.from([1, 1, 3, 2]),
  8311. Int32Array.from([1, 2, 3, 1]),
  8312. Int32Array.from([1, 1, 1, 4]),
  8313. Int32Array.from([1, 3, 1, 2]),
  8314. Int32Array.from([1, 2, 1, 3]),
  8315. Int32Array.from([3, 1, 1, 2]),
  8316. ];
  8317. /*
  8318. * Copyright (C) 2010 ZXing authors
  8319. *
  8320. * Licensed under the Apache License, Version 2.0 (the "License");
  8321. * you may not use this file except in compliance with the License.
  8322. * You may obtain a copy of the License at
  8323. *
  8324. * http://www.apache.org/licenses/LICENSE-2.0
  8325. *
  8326. * Unless required by applicable law or agreed to in writing, software
  8327. * distributed under the License is distributed on an "AS IS" BASIS,
  8328. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8329. * See the License for the specific language governing permissions and
  8330. * limitations under the License.
  8331. */
  8332. /**
  8333. * @see UPCEANExtension2Support
  8334. */
  8335. class UPCEANExtension5Support {
  8336. constructor() {
  8337. this.CHECK_DIGIT_ENCODINGS = [0x18, 0x14, 0x12, 0x11, 0x0C, 0x06, 0x03, 0x0A, 0x09, 0x05];
  8338. this.decodeMiddleCounters = Int32Array.from([0, 0, 0, 0]);
  8339. this.decodeRowStringBuffer = '';
  8340. }
  8341. decodeRow(rowNumber, row, extensionStartRange) {
  8342. let result = this.decodeRowStringBuffer;
  8343. let end = this.decodeMiddle(row, extensionStartRange, result);
  8344. let resultString = result.toString();
  8345. let extensionData = UPCEANExtension5Support.parseExtensionString(resultString);
  8346. let resultPoints = [
  8347. new ResultPoint((extensionStartRange[0] + extensionStartRange[1]) / 2.0, rowNumber),
  8348. new ResultPoint(end, rowNumber)
  8349. ];
  8350. let extensionResult = new Result$1(resultString, null, 0, resultPoints, BarcodeFormat$1.UPC_EAN_EXTENSION, new Date().getTime());
  8351. if (extensionData != null) {
  8352. extensionResult.putAllMetadata(extensionData);
  8353. }
  8354. return extensionResult;
  8355. }
  8356. decodeMiddle(row, startRange, resultString) {
  8357. let counters = this.decodeMiddleCounters;
  8358. counters[0] = 0;
  8359. counters[1] = 0;
  8360. counters[2] = 0;
  8361. counters[3] = 0;
  8362. let end = row.getSize();
  8363. let rowOffset = startRange[1];
  8364. let lgPatternFound = 0;
  8365. for (let x = 0; x < 5 && rowOffset < end; x++) {
  8366. let bestMatch = AbstractUPCEANReader.decodeDigit(row, counters, rowOffset, AbstractUPCEANReader.L_AND_G_PATTERNS);
  8367. resultString += String.fromCharCode(('0'.charCodeAt(0) + bestMatch % 10));
  8368. for (let counter of counters) {
  8369. rowOffset += counter;
  8370. }
  8371. if (bestMatch >= 10) {
  8372. lgPatternFound |= 1 << (4 - x);
  8373. }
  8374. if (x !== 4) {
  8375. // Read off separator if not last
  8376. rowOffset = row.getNextSet(rowOffset);
  8377. rowOffset = row.getNextUnset(rowOffset);
  8378. }
  8379. }
  8380. if (resultString.length !== 5) {
  8381. throw new NotFoundException();
  8382. }
  8383. let checkDigit = this.determineCheckDigit(lgPatternFound);
  8384. if (UPCEANExtension5Support.extensionChecksum(resultString.toString()) !== checkDigit) {
  8385. throw new NotFoundException();
  8386. }
  8387. return rowOffset;
  8388. }
  8389. static extensionChecksum(s) {
  8390. let length = s.length;
  8391. let sum = 0;
  8392. for (let i = length - 2; i >= 0; i -= 2) {
  8393. sum += s.charAt(i).charCodeAt(0) - '0'.charCodeAt(0);
  8394. }
  8395. sum *= 3;
  8396. for (let i = length - 1; i >= 0; i -= 2) {
  8397. sum += s.charAt(i).charCodeAt(0) - '0'.charCodeAt(0);
  8398. }
  8399. sum *= 3;
  8400. return sum % 10;
  8401. }
  8402. determineCheckDigit(lgPatternFound) {
  8403. for (let d = 0; d < 10; d++) {
  8404. if (lgPatternFound === this.CHECK_DIGIT_ENCODINGS[d]) {
  8405. return d;
  8406. }
  8407. }
  8408. throw new NotFoundException();
  8409. }
  8410. static parseExtensionString(raw) {
  8411. if (raw.length !== 5) {
  8412. return null;
  8413. }
  8414. let value = UPCEANExtension5Support.parseExtension5String(raw);
  8415. if (value == null) {
  8416. return null;
  8417. }
  8418. return new Map([[ResultMetadataType$1.SUGGESTED_PRICE, value]]);
  8419. }
  8420. static parseExtension5String(raw) {
  8421. let currency;
  8422. switch (raw.charAt(0)) {
  8423. case '0':
  8424. currency = '£';
  8425. break;
  8426. case '5':
  8427. currency = '$';
  8428. break;
  8429. case '9':
  8430. // Reference: http://www.jollytech.com
  8431. switch (raw) {
  8432. case '90000':
  8433. // No suggested retail price
  8434. return null;
  8435. case '99991':
  8436. // Complementary
  8437. return '0.00';
  8438. case '99990':
  8439. return 'Used';
  8440. }
  8441. // Otherwise... unknown currency?
  8442. currency = '';
  8443. break;
  8444. default:
  8445. currency = '';
  8446. break;
  8447. }
  8448. let rawAmount = parseInt(raw.substring(1));
  8449. let unitsString = (rawAmount / 100).toString();
  8450. let hundredths = rawAmount % 100;
  8451. let hundredthsString = hundredths < 10 ? '0' + hundredths : hundredths.toString(); // fixme
  8452. return currency + unitsString + '.' + hundredthsString;
  8453. }
  8454. }
  8455. /*
  8456. * Copyright (C) 2012 ZXing authors
  8457. *
  8458. * Licensed under the Apache License, Version 2.0 (the "License");
  8459. * you may not use this file except in compliance with the License.
  8460. * You may obtain a copy of the License at
  8461. *
  8462. * http://www.apache.org/licenses/LICENSE-2.0
  8463. *
  8464. * Unless required by applicable law or agreed to in writing, software
  8465. * distributed under the License is distributed on an "AS IS" BASIS,
  8466. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8467. * See the License for the specific language governing permissions and
  8468. * limitations under the License.
  8469. */
  8470. /**
  8471. * @see UPCEANExtension5Support
  8472. */
  8473. class UPCEANExtension2Support {
  8474. constructor() {
  8475. this.decodeMiddleCounters = Int32Array.from([0, 0, 0, 0]);
  8476. this.decodeRowStringBuffer = '';
  8477. }
  8478. decodeRow(rowNumber, row, extensionStartRange) {
  8479. let result = this.decodeRowStringBuffer;
  8480. let end = this.decodeMiddle(row, extensionStartRange, result);
  8481. let resultString = result.toString();
  8482. let extensionData = UPCEANExtension2Support.parseExtensionString(resultString);
  8483. let resultPoints = [
  8484. new ResultPoint((extensionStartRange[0] + extensionStartRange[1]) / 2.0, rowNumber),
  8485. new ResultPoint(end, rowNumber)
  8486. ];
  8487. let extensionResult = new Result$1(resultString, null, 0, resultPoints, BarcodeFormat$1.UPC_EAN_EXTENSION, new Date().getTime());
  8488. if (extensionData != null) {
  8489. extensionResult.putAllMetadata(extensionData);
  8490. }
  8491. return extensionResult;
  8492. }
  8493. decodeMiddle(row, startRange, resultString) {
  8494. let counters = this.decodeMiddleCounters;
  8495. counters[0] = 0;
  8496. counters[1] = 0;
  8497. counters[2] = 0;
  8498. counters[3] = 0;
  8499. let end = row.getSize();
  8500. let rowOffset = startRange[1];
  8501. let checkParity = 0;
  8502. for (let x = 0; x < 2 && rowOffset < end; x++) {
  8503. let bestMatch = AbstractUPCEANReader.decodeDigit(row, counters, rowOffset, AbstractUPCEANReader.L_AND_G_PATTERNS);
  8504. resultString += String.fromCharCode(('0'.charCodeAt(0) + bestMatch % 10));
  8505. for (let counter of counters) {
  8506. rowOffset += counter;
  8507. }
  8508. if (bestMatch >= 10) {
  8509. checkParity |= 1 << (1 - x);
  8510. }
  8511. if (x !== 1) {
  8512. // Read off separator if not last
  8513. rowOffset = row.getNextSet(rowOffset);
  8514. rowOffset = row.getNextUnset(rowOffset);
  8515. }
  8516. }
  8517. if (resultString.length !== 2) {
  8518. throw new NotFoundException();
  8519. }
  8520. if (parseInt(resultString.toString()) % 4 !== checkParity) {
  8521. throw new NotFoundException();
  8522. }
  8523. return rowOffset;
  8524. }
  8525. static parseExtensionString(raw) {
  8526. if (raw.length !== 2) {
  8527. return null;
  8528. }
  8529. return new Map([[ResultMetadataType$1.ISSUE_NUMBER, parseInt(raw)]]);
  8530. }
  8531. }
  8532. /*
  8533. * Copyright (C) 2010 ZXing authors
  8534. *
  8535. * Licensed under the Apache License, Version 2.0 (the "License");
  8536. * you may not use this file except in compliance with the License.
  8537. * You may obtain a copy of the License at
  8538. *
  8539. * http://www.apache.org/licenses/LICENSE-2.0
  8540. *
  8541. * Unless required by applicable law or agreed to in writing, software
  8542. * distributed under the License is distributed on an "AS IS" BASIS,
  8543. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8544. * See the License for the specific language governing permissions and
  8545. * limitations under the License.
  8546. */
  8547. class UPCEANExtensionSupport {
  8548. static decodeRow(rowNumber, row, rowOffset) {
  8549. let extensionStartRange = AbstractUPCEANReader.findGuardPattern(row, rowOffset, false, this.EXTENSION_START_PATTERN, new Int32Array(this.EXTENSION_START_PATTERN.length).fill(0));
  8550. try {
  8551. // return null;
  8552. let fiveSupport = new UPCEANExtension5Support();
  8553. return fiveSupport.decodeRow(rowNumber, row, extensionStartRange);
  8554. }
  8555. catch (err) {
  8556. // return null;
  8557. let twoSupport = new UPCEANExtension2Support();
  8558. return twoSupport.decodeRow(rowNumber, row, extensionStartRange);
  8559. }
  8560. }
  8561. }
  8562. UPCEANExtensionSupport.EXTENSION_START_PATTERN = Int32Array.from([1, 1, 2]);
  8563. /*
  8564. * Copyright 2008 ZXing authors
  8565. *
  8566. * Licensed under the Apache License, Version 2.0 (the "License");
  8567. * you may not use this file except in compliance with the License.
  8568. * You may obtain a copy of the License at
  8569. *
  8570. * http://www.apache.org/licenses/LICENSE-2.0
  8571. *
  8572. * Unless required by applicable law or agreed to in writing, software
  8573. * distributed under the License is distributed on an "AS IS" BASIS,
  8574. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8575. * See the License for the specific language governing permissions and
  8576. * limitations under the License.
  8577. */
  8578. /**
  8579. * <p>Encapsulates functionality and implementation that is common to UPC and EAN families
  8580. * of one-dimensional barcodes.</p>
  8581. *
  8582. * @author dswitkin@google.com (Daniel Switkin)
  8583. * @author Sean Owen
  8584. * @author alasdair@google.com (Alasdair Mackintosh)
  8585. */
  8586. class UPCEANReader extends AbstractUPCEANReader {
  8587. constructor() {
  8588. super();
  8589. this.decodeRowStringBuffer = '';
  8590. UPCEANReader.L_AND_G_PATTERNS = UPCEANReader.L_PATTERNS.map(arr => Int32Array.from(arr));
  8591. for (let i = 10; i < 20; i++) {
  8592. let widths = UPCEANReader.L_PATTERNS[i - 10];
  8593. let reversedWidths = new Int32Array(widths.length);
  8594. for (let j = 0; j < widths.length; j++) {
  8595. reversedWidths[j] = widths[widths.length - j - 1];
  8596. }
  8597. UPCEANReader.L_AND_G_PATTERNS[i] = reversedWidths;
  8598. }
  8599. }
  8600. decodeRow(rowNumber, row, hints) {
  8601. let startGuardRange = UPCEANReader.findStartGuardPattern(row);
  8602. let resultPointCallback = hints == null ? null : hints.get(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK);
  8603. if (resultPointCallback != null) {
  8604. const resultPoint = new ResultPoint((startGuardRange[0] + startGuardRange[1]) / 2.0, rowNumber);
  8605. resultPointCallback.foundPossibleResultPoint(resultPoint);
  8606. }
  8607. let budello = this.decodeMiddle(row, startGuardRange, this.decodeRowStringBuffer);
  8608. let endStart = budello.rowOffset;
  8609. let result = budello.resultString;
  8610. if (resultPointCallback != null) {
  8611. const resultPoint = new ResultPoint(endStart, rowNumber);
  8612. resultPointCallback.foundPossibleResultPoint(resultPoint);
  8613. }
  8614. let endRange = UPCEANReader.decodeEnd(row, endStart);
  8615. if (resultPointCallback != null) {
  8616. const resultPoint = new ResultPoint((endRange[0] + endRange[1]) / 2.0, rowNumber);
  8617. resultPointCallback.foundPossibleResultPoint(resultPoint);
  8618. }
  8619. // Make sure there is a quiet zone at least as big as the end pattern after the barcode. The
  8620. // spec might want more whitespace, but in practice this is the maximum we can count on.
  8621. let end = endRange[1];
  8622. let quietEnd = end + (end - endRange[0]);
  8623. if (quietEnd >= row.getSize() || !row.isRange(end, quietEnd, false)) {
  8624. throw new NotFoundException();
  8625. }
  8626. let resultString = result.toString();
  8627. // UPC/EAN should never be less than 8 chars anyway
  8628. if (resultString.length < 8) {
  8629. throw new FormatException();
  8630. }
  8631. if (!UPCEANReader.checkChecksum(resultString)) {
  8632. throw new ChecksumException();
  8633. }
  8634. let left = (startGuardRange[1] + startGuardRange[0]) / 2.0;
  8635. let right = (endRange[1] + endRange[0]) / 2.0;
  8636. let format = this.getBarcodeFormat();
  8637. let resultPoint = [new ResultPoint(left, rowNumber), new ResultPoint(right, rowNumber)];
  8638. let decodeResult = new Result$1(resultString, null, 0, resultPoint, format, new Date().getTime());
  8639. let extensionLength = 0;
  8640. try {
  8641. let extensionResult = UPCEANExtensionSupport.decodeRow(rowNumber, row, endRange[1]);
  8642. decodeResult.putMetadata(ResultMetadataType$1.UPC_EAN_EXTENSION, extensionResult.getText());
  8643. decodeResult.putAllMetadata(extensionResult.getResultMetadata());
  8644. decodeResult.addResultPoints(extensionResult.getResultPoints());
  8645. extensionLength = extensionResult.getText().length;
  8646. }
  8647. catch (err) {
  8648. }
  8649. let allowedExtensions = hints == null ? null : hints.get(DecodeHintType$1.ALLOWED_EAN_EXTENSIONS);
  8650. if (allowedExtensions != null) {
  8651. let valid = false;
  8652. for (let length in allowedExtensions) {
  8653. if (extensionLength.toString() === length) { // check me
  8654. valid = true;
  8655. break;
  8656. }
  8657. }
  8658. if (!valid) {
  8659. throw new NotFoundException();
  8660. }
  8661. }
  8662. if (format === BarcodeFormat$1.EAN_13 || format === BarcodeFormat$1.UPC_A) ;
  8663. return decodeResult;
  8664. }
  8665. static checkChecksum(s) {
  8666. return UPCEANReader.checkStandardUPCEANChecksum(s);
  8667. }
  8668. static checkStandardUPCEANChecksum(s) {
  8669. let length = s.length;
  8670. if (length === 0)
  8671. return false;
  8672. let check = parseInt(s.charAt(length - 1), 10);
  8673. return UPCEANReader.getStandardUPCEANChecksum(s.substring(0, length - 1)) === check;
  8674. }
  8675. static getStandardUPCEANChecksum(s) {
  8676. let length = s.length;
  8677. let sum = 0;
  8678. for (let i = length - 1; i >= 0; i -= 2) {
  8679. let digit = s.charAt(i).charCodeAt(0) - '0'.charCodeAt(0);
  8680. if (digit < 0 || digit > 9) {
  8681. throw new FormatException();
  8682. }
  8683. sum += digit;
  8684. }
  8685. sum *= 3;
  8686. for (let i = length - 2; i >= 0; i -= 2) {
  8687. let digit = s.charAt(i).charCodeAt(0) - '0'.charCodeAt(0);
  8688. if (digit < 0 || digit > 9) {
  8689. throw new FormatException();
  8690. }
  8691. sum += digit;
  8692. }
  8693. return (1000 - sum) % 10;
  8694. }
  8695. static decodeEnd(row, endStart) {
  8696. return UPCEANReader.findGuardPattern(row, endStart, false, UPCEANReader.START_END_PATTERN, new Int32Array(UPCEANReader.START_END_PATTERN.length).fill(0));
  8697. }
  8698. }
  8699. /*
  8700. * Copyright 2008 ZXing authors
  8701. *
  8702. * Licensed under the Apache License, Version 2.0 (the "License");
  8703. * you may not use this file except in compliance with the License.
  8704. * You may obtain a copy of the License at
  8705. *
  8706. * http://www.apache.org/licenses/LICENSE-2.0
  8707. *
  8708. * Unless required by applicable law or agreed to in writing, software
  8709. * distributed under the License is distributed on an "AS IS" BASIS,
  8710. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8711. * See the License for the specific language governing permissions and
  8712. * limitations under the License.
  8713. */
  8714. /**
  8715. * <p>Implements decoding of the EAN-13 format.</p>
  8716. *
  8717. * @author dswitkin@google.com (Daniel Switkin)
  8718. * @author Sean Owen
  8719. * @author alasdair@google.com (Alasdair Mackintosh)
  8720. */
  8721. class EAN13Reader extends UPCEANReader {
  8722. constructor() {
  8723. super();
  8724. this.decodeMiddleCounters = Int32Array.from([0, 0, 0, 0]);
  8725. }
  8726. decodeMiddle(row, startRange, resultString) {
  8727. let counters = this.decodeMiddleCounters;
  8728. counters[0] = 0;
  8729. counters[1] = 0;
  8730. counters[2] = 0;
  8731. counters[3] = 0;
  8732. let end = row.getSize();
  8733. let rowOffset = startRange[1];
  8734. let lgPatternFound = 0;
  8735. for (let x = 0; x < 6 && rowOffset < end; x++) {
  8736. let bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
  8737. resultString += String.fromCharCode(('0'.charCodeAt(0) + bestMatch % 10));
  8738. for (let counter of counters) {
  8739. rowOffset += counter;
  8740. }
  8741. if (bestMatch >= 10) {
  8742. lgPatternFound |= 1 << (5 - x);
  8743. }
  8744. }
  8745. resultString = EAN13Reader.determineFirstDigit(resultString, lgPatternFound);
  8746. let middleRange = UPCEANReader.findGuardPattern(row, rowOffset, true, UPCEANReader.MIDDLE_PATTERN, new Int32Array(UPCEANReader.MIDDLE_PATTERN.length).fill(0));
  8747. rowOffset = middleRange[1];
  8748. for (let x = 0; x < 6 && rowOffset < end; x++) {
  8749. let bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_PATTERNS);
  8750. resultString += String.fromCharCode(('0'.charCodeAt(0) + bestMatch));
  8751. for (let counter of counters) {
  8752. rowOffset += counter;
  8753. }
  8754. }
  8755. return { rowOffset, resultString };
  8756. }
  8757. getBarcodeFormat() {
  8758. return BarcodeFormat$1.EAN_13;
  8759. }
  8760. static determineFirstDigit(resultString, lgPatternFound) {
  8761. for (let d = 0; d < 10; d++) {
  8762. if (lgPatternFound === this.FIRST_DIGIT_ENCODINGS[d]) {
  8763. resultString = String.fromCharCode(('0'.charCodeAt(0) + d)) + resultString;
  8764. return resultString;
  8765. }
  8766. }
  8767. throw new NotFoundException();
  8768. }
  8769. }
  8770. EAN13Reader.FIRST_DIGIT_ENCODINGS = [0x00, 0x0B, 0x0D, 0xE, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A];
  8771. /*
  8772. * Copyright 2008 ZXing authors
  8773. *
  8774. * Licensed under the Apache License, Version 2.0 (the "License");
  8775. * you may not use this file except in compliance with the License.
  8776. * You may obtain a copy of the License at
  8777. *
  8778. * http://www.apache.org/licenses/LICENSE-2.0
  8779. *
  8780. * Unless required by applicable law or agreed to in writing, software
  8781. * distributed under the License is distributed on an "AS IS" BASIS,
  8782. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8783. * See the License for the specific language governing permissions and
  8784. * limitations under the License.
  8785. */
  8786. /**
  8787. * <p>Implements decoding of the EAN-8 format.</p>
  8788. *
  8789. * @author Sean Owen
  8790. */
  8791. class EAN8Reader extends UPCEANReader {
  8792. constructor() {
  8793. super();
  8794. this.decodeMiddleCounters = Int32Array.from([0, 0, 0, 0]);
  8795. }
  8796. decodeMiddle(row, startRange, resultString) {
  8797. const counters = this.decodeMiddleCounters;
  8798. counters[0] = 0;
  8799. counters[1] = 0;
  8800. counters[2] = 0;
  8801. counters[3] = 0;
  8802. let end = row.getSize();
  8803. let rowOffset = startRange[1];
  8804. for (let x = 0; x < 4 && rowOffset < end; x++) {
  8805. let bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_PATTERNS);
  8806. resultString += String.fromCharCode(('0'.charCodeAt(0) + bestMatch));
  8807. for (let counter of counters) {
  8808. rowOffset += counter;
  8809. }
  8810. }
  8811. let middleRange = UPCEANReader.findGuardPattern(row, rowOffset, true, UPCEANReader.MIDDLE_PATTERN, new Int32Array(UPCEANReader.MIDDLE_PATTERN.length).fill(0));
  8812. rowOffset = middleRange[1];
  8813. for (let x = 0; x < 4 && rowOffset < end; x++) {
  8814. let bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_PATTERNS);
  8815. resultString += String.fromCharCode(('0'.charCodeAt(0) + bestMatch));
  8816. for (let counter of counters) {
  8817. rowOffset += counter;
  8818. }
  8819. }
  8820. return { rowOffset, resultString };
  8821. }
  8822. getBarcodeFormat() {
  8823. return BarcodeFormat$1.EAN_8;
  8824. }
  8825. }
  8826. /*
  8827. * Copyright 2008 ZXing authors
  8828. *
  8829. * Licensed under the Apache License, Version 2.0 (the "License");
  8830. * you may not use this file except in compliance with the License.
  8831. * You may obtain a copy of the License at
  8832. *
  8833. * http://www.apache.org/licenses/LICENSE-2.0
  8834. *
  8835. * Unless required by applicable law or agreed to in writing, software
  8836. * distributed under the License is distributed on an "AS IS" BASIS,
  8837. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8838. * See the License for the specific language governing permissions and
  8839. * limitations under the License.
  8840. */
  8841. /**
  8842. * Encapsulates functionality and implementation that is common to all families
  8843. * of one-dimensional barcodes.
  8844. *
  8845. * @author dswitkin@google.com (Daniel Switkin)
  8846. * @author Sean Owen
  8847. * @author sam2332 (Sam Rudloff)
  8848. *
  8849. * @source https://github.com/zxing/zxing/blob/3c96923276dd5785d58eb970b6ba3f80d36a9505/core/src/main/java/com/google/zxing/oned/UPCAReader.java
  8850. *
  8851. * @experimental
  8852. */
  8853. class UPCAReader extends UPCEANReader {
  8854. constructor() {
  8855. super(...arguments);
  8856. this.ean13Reader = new EAN13Reader();
  8857. }
  8858. // @Override
  8859. getBarcodeFormat() {
  8860. return BarcodeFormat$1.UPC_A;
  8861. }
  8862. // Note that we don't try rotation without the try harder flag, even if rotation was supported.
  8863. // @Override
  8864. decode(image, hints) {
  8865. return this.maybeReturnResult(this.ean13Reader.decode(image));
  8866. }
  8867. // @Override
  8868. decodeRow(rowNumber, row, hints) {
  8869. return this.maybeReturnResult(this.ean13Reader.decodeRow(rowNumber, row, hints));
  8870. }
  8871. // @Override
  8872. decodeMiddle(row, startRange, resultString) {
  8873. return this.ean13Reader.decodeMiddle(row, startRange, resultString);
  8874. }
  8875. maybeReturnResult(result) {
  8876. let text = result.getText();
  8877. if (text.charAt(0) === '0') {
  8878. let upcaResult = new Result$1(text.substring(1), null, null, result.getResultPoints(), BarcodeFormat$1.UPC_A);
  8879. if (result.getResultMetadata() != null) {
  8880. upcaResult.putAllMetadata(result.getResultMetadata());
  8881. }
  8882. return upcaResult;
  8883. }
  8884. else {
  8885. throw new NotFoundException();
  8886. }
  8887. }
  8888. reset() {
  8889. this.ean13Reader.reset();
  8890. }
  8891. }
  8892. /*
  8893. * Copyright 2008 ZXing authors
  8894. *
  8895. * Licensed under the Apache License, Version 2.0 (the "License");
  8896. * you may not use this file except in compliance with the License.
  8897. * You may obtain a copy of the License at
  8898. *
  8899. * http://www.apache.org/licenses/LICENSE-2.0
  8900. *
  8901. * Unless required by applicable law or agreed to in writing, software
  8902. * distributed under the License is distributed on an "AS IS" BASIS,
  8903. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8904. * See the License for the specific language governing permissions and
  8905. * limitations under the License.
  8906. */
  8907. // package com.google.zxing.oned;
  8908. // import com.google.zxing.BarcodeFormat;
  8909. // import com.google.zxing.FormatException;
  8910. // import com.google.zxing.NotFoundException;
  8911. // import com.google.zxing.common.BitArray;
  8912. /**
  8913. * <p>Implements decoding of the UPC-E format.</p>
  8914. * <p><a href="http://www.barcodeisland.com/upce.phtml">This</a> is a great reference for
  8915. * UPC-E information.</p>
  8916. *
  8917. * @author Sean Owen
  8918. *
  8919. * @source https://github.com/zxing/zxing/blob/3c96923276dd5785d58eb970b6ba3f80d36a9505/core/src/main/java/com/google/zxing/oned/UPCEReader.java
  8920. *
  8921. * @experimental
  8922. */
  8923. /* final */ class UPCEReader extends UPCEANReader {
  8924. constructor() {
  8925. super();
  8926. this.decodeMiddleCounters = new Int32Array(4);
  8927. }
  8928. /**
  8929. * @throws NotFoundException
  8930. */
  8931. // @Override
  8932. decodeMiddle(row, startRange, result) {
  8933. const counters = this.decodeMiddleCounters.map(x => x);
  8934. counters[0] = 0;
  8935. counters[1] = 0;
  8936. counters[2] = 0;
  8937. counters[3] = 0;
  8938. const end = row.getSize();
  8939. let rowOffset = startRange[1];
  8940. let lgPatternFound = 0;
  8941. for (let x = 0; x < 6 && rowOffset < end; x++) {
  8942. const bestMatch = UPCEReader.decodeDigit(row, counters, rowOffset, UPCEReader.L_AND_G_PATTERNS);
  8943. result += String.fromCharCode(('0'.charCodeAt(0) + bestMatch % 10));
  8944. for (let counter of counters) {
  8945. rowOffset += counter;
  8946. }
  8947. if (bestMatch >= 10) {
  8948. lgPatternFound |= 1 << (5 - x);
  8949. }
  8950. }
  8951. UPCEReader.determineNumSysAndCheckDigit(new StringBuilder(result), lgPatternFound);
  8952. return rowOffset;
  8953. }
  8954. /**
  8955. * @throws NotFoundException
  8956. */
  8957. // @Override
  8958. decodeEnd(row, endStart) {
  8959. return UPCEReader.findGuardPatternWithoutCounters(row, endStart, true, UPCEReader.MIDDLE_END_PATTERN);
  8960. }
  8961. /**
  8962. * @throws FormatException
  8963. */
  8964. // @Override
  8965. checkChecksum(s) {
  8966. return UPCEANReader.checkChecksum(UPCEReader.convertUPCEtoUPCA(s));
  8967. }
  8968. /**
  8969. * @throws NotFoundException
  8970. */
  8971. static determineNumSysAndCheckDigit(resultString, lgPatternFound) {
  8972. for (let numSys = 0; numSys <= 1; numSys++) {
  8973. for (let d = 0; d < 10; d++) {
  8974. if (lgPatternFound === this.NUMSYS_AND_CHECK_DIGIT_PATTERNS[numSys][d]) {
  8975. resultString.insert(0, /*(char)*/ ('0' + numSys));
  8976. resultString.append(/*(char)*/ ('0' + d));
  8977. return;
  8978. }
  8979. }
  8980. }
  8981. throw NotFoundException.getNotFoundInstance();
  8982. }
  8983. // @Override
  8984. getBarcodeFormat() {
  8985. return BarcodeFormat$1.UPC_E;
  8986. }
  8987. /**
  8988. * Expands a UPC-E value back into its full, equivalent UPC-A code value.
  8989. *
  8990. * @param upce UPC-E code as string of digits
  8991. * @return equivalent UPC-A code as string of digits
  8992. */
  8993. static convertUPCEtoUPCA(upce) {
  8994. // the following line is equivalent to upce.getChars(1, 7, upceChars, 0);
  8995. const upceChars = upce.slice(1, 7).split('').map(x => x.charCodeAt(0));
  8996. const result = new StringBuilder( /*12*/);
  8997. result.append(upce.charAt(0));
  8998. let lastChar = upceChars[5];
  8999. switch (lastChar) {
  9000. case 0:
  9001. case 1:
  9002. case 2:
  9003. result.appendChars(upceChars, 0, 2);
  9004. result.append(lastChar);
  9005. result.append('0000');
  9006. result.appendChars(upceChars, 2, 3);
  9007. break;
  9008. case 3:
  9009. result.appendChars(upceChars, 0, 3);
  9010. result.append('00000');
  9011. result.appendChars(upceChars, 3, 2);
  9012. break;
  9013. case 4:
  9014. result.appendChars(upceChars, 0, 4);
  9015. result.append('00000');
  9016. result.append(upceChars[4]);
  9017. break;
  9018. default:
  9019. result.appendChars(upceChars, 0, 5);
  9020. result.append('0000');
  9021. result.append(lastChar);
  9022. break;
  9023. }
  9024. // Only append check digit in conversion if supplied
  9025. if (upce.length >= 8) {
  9026. result.append(upce.charAt(7));
  9027. }
  9028. return result.toString();
  9029. }
  9030. }
  9031. /**
  9032. * The pattern that marks the middle, and end, of a UPC-E pattern.
  9033. * There is no "second half" to a UPC-E barcode.
  9034. */
  9035. UPCEReader.MIDDLE_END_PATTERN = Int32Array.from([1, 1, 1, 1, 1, 1]);
  9036. // For an UPC-E barcode, the final digit is represented by the parities used
  9037. // to encode the middle six digits, according to the table below.
  9038. //
  9039. // Parity of next 6 digits
  9040. // Digit 0 1 2 3 4 5
  9041. // 0 Even Even Even Odd Odd Odd
  9042. // 1 Even Even Odd Even Odd Odd
  9043. // 2 Even Even Odd Odd Even Odd
  9044. // 3 Even Even Odd Odd Odd Even
  9045. // 4 Even Odd Even Even Odd Odd
  9046. // 5 Even Odd Odd Even Even Odd
  9047. // 6 Even Odd Odd Odd Even Even
  9048. // 7 Even Odd Even Odd Even Odd
  9049. // 8 Even Odd Even Odd Odd Even
  9050. // 9 Even Odd Odd Even Odd Even
  9051. //
  9052. // The encoding is represented by the following array, which is a bit pattern
  9053. // using Odd = 0 and Even = 1. For example, 5 is represented by:
  9054. //
  9055. // Odd Even Even Odd Odd Even
  9056. // in binary:
  9057. // 0 1 1 0 0 1 == 0x19
  9058. //
  9059. /**
  9060. * See {@link #L_AND_G_PATTERNS}; these values similarly represent patterns of
  9061. * even-odd parity encodings of digits that imply both the number system (0 or 1)
  9062. * used, and the check digit.
  9063. */
  9064. UPCEReader.NUMSYS_AND_CHECK_DIGIT_PATTERNS = [
  9065. Int32Array.from([0x38, 0x34, 0x32, 0x31, 0x2C, 0x26, 0x23, 0x2A, 0x29, 0x25]),
  9066. Int32Array.from([0x07, 0x0B, 0x0D, 0x0E, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1]),
  9067. ];
  9068. /*
  9069. * Copyright 2008 ZXing authors
  9070. *
  9071. * Licensed under the Apache License, Version 2.0 (the "License");
  9072. * you may not use this file except in compliance with the License.
  9073. * You may obtain a copy of the License at
  9074. *
  9075. * http://www.apache.org/licenses/LICENSE-2.0
  9076. *
  9077. * Unless required by applicable law or agreed to in writing, software
  9078. * distributed under the License is distributed on an "AS IS" BASIS,
  9079. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9080. * See the License for the specific language governing permissions and
  9081. * limitations under the License.
  9082. */
  9083. /**
  9084. * <p>A reader that can read all available UPC/EAN formats. If a caller wants to try to
  9085. * read all such formats, it is most efficient to use this implementation rather than invoke
  9086. * individual readers.</p>
  9087. *
  9088. * @author Sean Owen
  9089. */
  9090. class MultiFormatUPCEANReader extends OneDReader {
  9091. constructor(hints) {
  9092. super();
  9093. let possibleFormats = hints == null ? null : hints.get(DecodeHintType$1.POSSIBLE_FORMATS);
  9094. let readers = [];
  9095. if (possibleFormats != null) {
  9096. if (possibleFormats.indexOf(BarcodeFormat$1.EAN_13) > -1) {
  9097. readers.push(new EAN13Reader());
  9098. }
  9099. if (possibleFormats.indexOf(BarcodeFormat$1.UPC_A) > -1) {
  9100. readers.push(new UPCAReader());
  9101. }
  9102. if (possibleFormats.indexOf(BarcodeFormat$1.EAN_8) > -1) {
  9103. readers.push(new EAN8Reader());
  9104. }
  9105. if (possibleFormats.indexOf(BarcodeFormat$1.UPC_E) > -1) {
  9106. readers.push(new UPCEReader());
  9107. }
  9108. }
  9109. if (readers.length === 0) {
  9110. readers.push(new EAN13Reader());
  9111. readers.push(new UPCAReader());
  9112. readers.push(new EAN8Reader());
  9113. readers.push(new UPCEReader());
  9114. }
  9115. this.readers = readers;
  9116. }
  9117. decodeRow(rowNumber, row, hints) {
  9118. for (let reader of this.readers) {
  9119. try {
  9120. // const result: Result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);
  9121. const result = reader.decodeRow(rowNumber, row, hints);
  9122. // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
  9123. // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
  9124. // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
  9125. // Individually these are correct and their readers will both read such a code
  9126. // and correctly call it EAN-13, or UPC-A, respectively.
  9127. //
  9128. // In this case, if we've been looking for both types, we'd like to call it
  9129. // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
  9130. // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
  9131. // result if appropriate.
  9132. //
  9133. // But, don't return UPC-A if UPC-A was not a requested format!
  9134. const ean13MayBeUPCA = result.getBarcodeFormat() === BarcodeFormat$1.EAN_13 &&
  9135. result.getText().charAt(0) === '0';
  9136. // @SuppressWarnings("unchecked")
  9137. const possibleFormats = hints == null ? null : hints.get(DecodeHintType$1.POSSIBLE_FORMATS);
  9138. const canReturnUPCA = possibleFormats == null || possibleFormats.includes(BarcodeFormat$1.UPC_A);
  9139. if (ean13MayBeUPCA && canReturnUPCA) {
  9140. const rawBytes = result.getRawBytes();
  9141. // Transfer the metadata across
  9142. const resultUPCA = new Result$1(result.getText().substring(1), rawBytes, (rawBytes ? rawBytes.length : null), result.getResultPoints(), BarcodeFormat$1.UPC_A);
  9143. resultUPCA.putAllMetadata(result.getResultMetadata());
  9144. return resultUPCA;
  9145. }
  9146. return result;
  9147. }
  9148. catch (err) {
  9149. // continue;
  9150. }
  9151. }
  9152. throw new NotFoundException();
  9153. }
  9154. reset() {
  9155. for (let reader of this.readers) {
  9156. reader.reset();
  9157. }
  9158. }
  9159. }
  9160. /*
  9161. * Copyright 2008 ZXing authors
  9162. *
  9163. * Licensed under the Apache License, Version 2.0 (the "License");
  9164. * you may not use this file except in compliance with the License.
  9165. * You may obtain a copy of the License at
  9166. *
  9167. * http://www.apache.org/licenses/LICENSE-2.0
  9168. *
  9169. * Unless required by applicable law or agreed to in writing, software
  9170. * distributed under the License is distributed on an "AS IS" BASIS,
  9171. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9172. * See the License for the specific language governing permissions and
  9173. * limitations under the License.
  9174. */
  9175. /**
  9176. * <p>Decodes CodaBar barcodes. </p>
  9177. *
  9178. * @author Evan @dodobelieve
  9179. * @see CodaBarReader
  9180. */
  9181. class CodaBarReader extends OneDReader {
  9182. constructor() {
  9183. super(...arguments);
  9184. this.CODA_BAR_CHAR_SET = {
  9185. nnnnnww: '0',
  9186. nnnnwwn: '1',
  9187. nnnwnnw: '2',
  9188. wwnnnnn: '3',
  9189. nnwnnwn: '4',
  9190. wnnnnwn: '5',
  9191. nwnnnnw: '6',
  9192. nwnnwnn: '7',
  9193. nwwnnnn: '8',
  9194. wnnwnnn: '9',
  9195. nnnwwnn: '-',
  9196. nnwwnnn: '$',
  9197. wnnnwnw: ':',
  9198. wnwnnnw: '/',
  9199. wnwnwnn: '.',
  9200. nnwwwww: '+',
  9201. nnwwnwn: 'A',
  9202. nwnwnnw: 'B',
  9203. nnnwnww: 'C',
  9204. nnnwwwn: 'D'
  9205. };
  9206. }
  9207. decodeRow(rowNumber, row, hints) {
  9208. let validRowData = this.getValidRowData(row);
  9209. if (!validRowData)
  9210. throw new NotFoundException();
  9211. let retStr = this.codaBarDecodeRow(validRowData.row);
  9212. if (!retStr)
  9213. throw new NotFoundException();
  9214. return new Result$1(retStr, null, 0, [new ResultPoint(validRowData.left, rowNumber), new ResultPoint(validRowData.right, rowNumber)], BarcodeFormat$1.CODABAR, new Date().getTime());
  9215. }
  9216. /**
  9217. * converts bit array to valid data array(lengths of black bits and white bits)
  9218. * @param row bit array to convert
  9219. */
  9220. getValidRowData(row) {
  9221. let booleanArr = row.toArray();
  9222. let startIndex = booleanArr.indexOf(true);
  9223. if (startIndex === -1)
  9224. return null;
  9225. let lastIndex = booleanArr.lastIndexOf(true);
  9226. if (lastIndex <= startIndex)
  9227. return null;
  9228. booleanArr = booleanArr.slice(startIndex, lastIndex + 1);
  9229. let result = [];
  9230. let lastBit = booleanArr[0];
  9231. let bitLength = 1;
  9232. for (let i = 1; i < booleanArr.length; i++) {
  9233. if (booleanArr[i] === lastBit) {
  9234. bitLength++;
  9235. }
  9236. else {
  9237. lastBit = booleanArr[i];
  9238. result.push(bitLength);
  9239. bitLength = 1;
  9240. }
  9241. }
  9242. result.push(bitLength);
  9243. // CodaBar code data valid
  9244. if (result.length < 23 && (result.length + 1) % 8 !== 0)
  9245. return null;
  9246. return { row: result, left: startIndex, right: lastIndex };
  9247. }
  9248. /**
  9249. * decode codabar code
  9250. * @param row row to cecode
  9251. */
  9252. codaBarDecodeRow(row) {
  9253. const code = [];
  9254. const barThreshold = Math.ceil(row.reduce((pre, item) => (pre + item) / 2, 0));
  9255. // Read one encoded character at a time.
  9256. while (row.length > 0) {
  9257. const seg = row.splice(0, 8).splice(0, 7);
  9258. const key = seg.map(len => (len < barThreshold ? 'n' : 'w')).join('');
  9259. if (this.CODA_BAR_CHAR_SET[key] === undefined)
  9260. return null;
  9261. code.push(this.CODA_BAR_CHAR_SET[key]);
  9262. }
  9263. let strCode = code.join('');
  9264. if (this.validCodaBarString(strCode))
  9265. return strCode;
  9266. return null;
  9267. }
  9268. /**
  9269. * check if the string is a CodaBar string
  9270. * @param src string to determine
  9271. */
  9272. validCodaBarString(src) {
  9273. let reg = /^[A-D].{1,}[A-D]$/;
  9274. return reg.test(src);
  9275. }
  9276. }
  9277. // import Integer from '../../util/Integer';
  9278. // import Float from '../../util/Float';
  9279. class AbstractRSSReader extends OneDReader {
  9280. constructor() {
  9281. super();
  9282. this.decodeFinderCounters = new Int32Array(4);
  9283. this.dataCharacterCounters = new Int32Array(8);
  9284. this.oddRoundingErrors = new Array(4);
  9285. this.evenRoundingErrors = new Array(4);
  9286. this.oddCounts = new Array(this.dataCharacterCounters.length / 2);
  9287. this.evenCounts = new Array(this.dataCharacterCounters.length / 2);
  9288. }
  9289. getDecodeFinderCounters() {
  9290. return this.decodeFinderCounters;
  9291. }
  9292. getDataCharacterCounters() {
  9293. return this.dataCharacterCounters;
  9294. }
  9295. getOddRoundingErrors() {
  9296. return this.oddRoundingErrors;
  9297. }
  9298. getEvenRoundingErrors() {
  9299. return this.evenRoundingErrors;
  9300. }
  9301. getOddCounts() {
  9302. return this.oddCounts;
  9303. }
  9304. getEvenCounts() {
  9305. return this.evenCounts;
  9306. }
  9307. parseFinderValue(counters, finderPatterns) {
  9308. for (let value = 0; value < finderPatterns.length; value++) {
  9309. if (OneDReader.patternMatchVariance(counters, finderPatterns[value], AbstractRSSReader.MAX_INDIVIDUAL_VARIANCE) < AbstractRSSReader.MAX_AVG_VARIANCE) {
  9310. return value;
  9311. }
  9312. }
  9313. throw new NotFoundException();
  9314. }
  9315. /**
  9316. * @param array values to sum
  9317. * @return sum of values
  9318. * @deprecated call {@link MathUtils#sum(int[])}
  9319. */
  9320. static count(array) {
  9321. return MathUtils.sum(new Int32Array(array));
  9322. }
  9323. static increment(array, errors) {
  9324. let index = 0;
  9325. let biggestError = errors[0];
  9326. for (let i = 1; i < array.length; i++) {
  9327. if (errors[i] > biggestError) {
  9328. biggestError = errors[i];
  9329. index = i;
  9330. }
  9331. }
  9332. array[index]++;
  9333. }
  9334. static decrement(array, errors) {
  9335. let index = 0;
  9336. let biggestError = errors[0];
  9337. for (let i = 1; i < array.length; i++) {
  9338. if (errors[i] < biggestError) {
  9339. biggestError = errors[i];
  9340. index = i;
  9341. }
  9342. }
  9343. array[index]--;
  9344. }
  9345. static isFinderPattern(counters) {
  9346. let firstTwoSum = counters[0] + counters[1];
  9347. let sum = firstTwoSum + counters[2] + counters[3];
  9348. let ratio = firstTwoSum / sum;
  9349. if (ratio >= AbstractRSSReader.MIN_FINDER_PATTERN_RATIO && ratio <= AbstractRSSReader.MAX_FINDER_PATTERN_RATIO) {
  9350. // passes ratio test in spec, but see if the counts are unreasonable
  9351. let minCounter = Number.MAX_SAFE_INTEGER;
  9352. let maxCounter = Number.MIN_SAFE_INTEGER;
  9353. for (let counter of counters) {
  9354. if (counter > maxCounter) {
  9355. maxCounter = counter;
  9356. }
  9357. if (counter < minCounter) {
  9358. minCounter = counter;
  9359. }
  9360. }
  9361. return maxCounter < 10 * minCounter;
  9362. }
  9363. return false;
  9364. }
  9365. }
  9366. AbstractRSSReader.MAX_AVG_VARIANCE = 0.2;
  9367. AbstractRSSReader.MAX_INDIVIDUAL_VARIANCE = 0.45;
  9368. AbstractRSSReader.MIN_FINDER_PATTERN_RATIO = 9.5 / 12.0;
  9369. AbstractRSSReader.MAX_FINDER_PATTERN_RATIO = 12.5 / 14.0;
  9370. class DataCharacter {
  9371. constructor(value, checksumPortion) {
  9372. this.value = value;
  9373. this.checksumPortion = checksumPortion;
  9374. }
  9375. getValue() {
  9376. return this.value;
  9377. }
  9378. getChecksumPortion() {
  9379. return this.checksumPortion;
  9380. }
  9381. toString() {
  9382. return this.value + '(' + this.checksumPortion + ')';
  9383. }
  9384. equals(o) {
  9385. if (!(o instanceof DataCharacter)) {
  9386. return false;
  9387. }
  9388. const that = o;
  9389. return this.value === that.value && this.checksumPortion === that.checksumPortion;
  9390. }
  9391. hashCode() {
  9392. return this.value ^ this.checksumPortion;
  9393. }
  9394. }
  9395. class FinderPattern$1 {
  9396. constructor(value, startEnd, start, end, rowNumber) {
  9397. this.value = value;
  9398. this.startEnd = startEnd;
  9399. this.value = value;
  9400. this.startEnd = startEnd;
  9401. this.resultPoints = new Array();
  9402. this.resultPoints.push(new ResultPoint(start, rowNumber));
  9403. this.resultPoints.push(new ResultPoint(end, rowNumber));
  9404. }
  9405. getValue() {
  9406. return this.value;
  9407. }
  9408. getStartEnd() {
  9409. return this.startEnd;
  9410. }
  9411. getResultPoints() {
  9412. return this.resultPoints;
  9413. }
  9414. equals(o) {
  9415. if (!(o instanceof FinderPattern$1)) {
  9416. return false;
  9417. }
  9418. const that = o;
  9419. return this.value === that.value;
  9420. }
  9421. hashCode() {
  9422. return this.value;
  9423. }
  9424. }
  9425. /**
  9426. * RSS util functions.
  9427. */
  9428. class RSSUtils {
  9429. constructor() { }
  9430. static getRSSvalue(widths, maxWidth, noNarrow) {
  9431. let n = 0;
  9432. for (let width of widths) {
  9433. n += width;
  9434. }
  9435. let val = 0;
  9436. let narrowMask = 0;
  9437. let elements = widths.length;
  9438. for (let bar = 0; bar < elements - 1; bar++) {
  9439. let elmWidth;
  9440. for (elmWidth = 1, narrowMask |= 1 << bar; elmWidth < widths[bar]; elmWidth++, narrowMask &= ~(1 << bar)) {
  9441. let subVal = RSSUtils.combins(n - elmWidth - 1, elements - bar - 2);
  9442. if (noNarrow && (narrowMask === 0) && (n - elmWidth - (elements - bar - 1) >= elements - bar - 1)) {
  9443. subVal -= RSSUtils.combins(n - elmWidth - (elements - bar), elements - bar - 2);
  9444. }
  9445. if (elements - bar - 1 > 1) {
  9446. let lessVal = 0;
  9447. for (let mxwElement = n - elmWidth - (elements - bar - 2); mxwElement > maxWidth; mxwElement--) {
  9448. lessVal += RSSUtils.combins(n - elmWidth - mxwElement - 1, elements - bar - 3);
  9449. }
  9450. subVal -= lessVal * (elements - 1 - bar);
  9451. }
  9452. else if (n - elmWidth > maxWidth) {
  9453. subVal--;
  9454. }
  9455. val += subVal;
  9456. }
  9457. n -= elmWidth;
  9458. }
  9459. return val;
  9460. }
  9461. static combins(n, r) {
  9462. let maxDenom;
  9463. let minDenom;
  9464. if (n - r > r) {
  9465. minDenom = r;
  9466. maxDenom = n - r;
  9467. }
  9468. else {
  9469. minDenom = n - r;
  9470. maxDenom = r;
  9471. }
  9472. let val = 1;
  9473. let j = 1;
  9474. for (let i = n; i > maxDenom; i--) {
  9475. val *= i;
  9476. if (j <= minDenom) {
  9477. val /= j;
  9478. j++;
  9479. }
  9480. }
  9481. while ((j <= minDenom)) {
  9482. val /= j;
  9483. j++;
  9484. }
  9485. return val;
  9486. }
  9487. }
  9488. class BitArrayBuilder {
  9489. static buildBitArray(pairs) {
  9490. let charNumber = pairs.length * 2 - 1;
  9491. if (pairs[pairs.length - 1].getRightChar() == null) {
  9492. charNumber -= 1;
  9493. }
  9494. let size = 12 * charNumber;
  9495. let binary = new BitArray(size);
  9496. let accPos = 0;
  9497. let firstPair = pairs[0];
  9498. let firstValue = firstPair.getRightChar().getValue();
  9499. for (let i = 11; i >= 0; --i) {
  9500. if ((firstValue & (1 << i)) !== 0) {
  9501. binary.set(accPos);
  9502. }
  9503. accPos++;
  9504. }
  9505. for (let i = 1; i < pairs.length; ++i) {
  9506. let currentPair = pairs[i];
  9507. let leftValue = currentPair.getLeftChar().getValue();
  9508. for (let j = 11; j >= 0; --j) {
  9509. if ((leftValue & (1 << j)) !== 0) {
  9510. binary.set(accPos);
  9511. }
  9512. accPos++;
  9513. }
  9514. if (currentPair.getRightChar() !== null) {
  9515. let rightValue = currentPair.getRightChar().getValue();
  9516. for (let j = 11; j >= 0; --j) {
  9517. if ((rightValue & (1 << j)) !== 0) {
  9518. binary.set(accPos);
  9519. }
  9520. accPos++;
  9521. }
  9522. }
  9523. }
  9524. return binary;
  9525. }
  9526. }
  9527. class BlockParsedResult {
  9528. constructor(finished, decodedInformation) {
  9529. if (decodedInformation) {
  9530. this.decodedInformation = null;
  9531. }
  9532. else {
  9533. this.finished = finished;
  9534. this.decodedInformation = decodedInformation;
  9535. }
  9536. }
  9537. getDecodedInformation() {
  9538. return this.decodedInformation;
  9539. }
  9540. isFinished() {
  9541. return this.finished;
  9542. }
  9543. }
  9544. class DecodedObject {
  9545. constructor(newPosition) {
  9546. this.newPosition = newPosition;
  9547. }
  9548. getNewPosition() {
  9549. return this.newPosition;
  9550. }
  9551. }
  9552. class DecodedChar extends DecodedObject {
  9553. constructor(newPosition, value) {
  9554. super(newPosition);
  9555. this.value = value;
  9556. }
  9557. getValue() {
  9558. return this.value;
  9559. }
  9560. isFNC1() {
  9561. return this.value === DecodedChar.FNC1;
  9562. }
  9563. }
  9564. DecodedChar.FNC1 = '$';
  9565. class DecodedInformation extends DecodedObject {
  9566. constructor(newPosition, newString, remainingValue) {
  9567. super(newPosition);
  9568. if (remainingValue) {
  9569. this.remaining = true;
  9570. this.remainingValue = this.remainingValue;
  9571. }
  9572. else {
  9573. this.remaining = false;
  9574. this.remainingValue = 0;
  9575. }
  9576. this.newString = newString;
  9577. }
  9578. getNewString() {
  9579. return this.newString;
  9580. }
  9581. isRemaining() {
  9582. return this.remaining;
  9583. }
  9584. getRemainingValue() {
  9585. return this.remainingValue;
  9586. }
  9587. }
  9588. class DecodedNumeric extends DecodedObject {
  9589. constructor(newPosition, firstDigit, secondDigit) {
  9590. super(newPosition);
  9591. if (firstDigit < 0 || firstDigit > 10 || secondDigit < 0 || secondDigit > 10) {
  9592. throw new FormatException();
  9593. }
  9594. this.firstDigit = firstDigit;
  9595. this.secondDigit = secondDigit;
  9596. }
  9597. getFirstDigit() {
  9598. return this.firstDigit;
  9599. }
  9600. getSecondDigit() {
  9601. return this.secondDigit;
  9602. }
  9603. getValue() {
  9604. return this.firstDigit * 10 + this.secondDigit;
  9605. }
  9606. isFirstDigitFNC1() {
  9607. return this.firstDigit === DecodedNumeric.FNC1;
  9608. }
  9609. isSecondDigitFNC1() {
  9610. return this.secondDigit === DecodedNumeric.FNC1;
  9611. }
  9612. isAnyFNC1() {
  9613. return this.firstDigit === DecodedNumeric.FNC1 || this.secondDigit === DecodedNumeric.FNC1;
  9614. }
  9615. }
  9616. DecodedNumeric.FNC1 = 10;
  9617. class FieldParser {
  9618. constructor() {
  9619. }
  9620. static parseFieldsInGeneralPurpose(rawInformation) {
  9621. if (!rawInformation) {
  9622. return null;
  9623. }
  9624. // Processing 2-digit AIs
  9625. if (rawInformation.length < 2) {
  9626. throw new NotFoundException();
  9627. }
  9628. let firstTwoDigits = rawInformation.substring(0, 2);
  9629. for (let dataLength of FieldParser.TWO_DIGIT_DATA_LENGTH) {
  9630. if (dataLength[0] === firstTwoDigits) {
  9631. if (dataLength[1] === FieldParser.VARIABLE_LENGTH) {
  9632. return FieldParser.processVariableAI(2, dataLength[2], rawInformation);
  9633. }
  9634. return FieldParser.processFixedAI(2, dataLength[1], rawInformation);
  9635. }
  9636. }
  9637. if (rawInformation.length < 3) {
  9638. throw new NotFoundException();
  9639. }
  9640. let firstThreeDigits = rawInformation.substring(0, 3);
  9641. for (let dataLength of FieldParser.THREE_DIGIT_DATA_LENGTH) {
  9642. if (dataLength[0] === firstThreeDigits) {
  9643. if (dataLength[1] === FieldParser.VARIABLE_LENGTH) {
  9644. return FieldParser.processVariableAI(3, dataLength[2], rawInformation);
  9645. }
  9646. return FieldParser.processFixedAI(3, dataLength[1], rawInformation);
  9647. }
  9648. }
  9649. for (let dataLength of FieldParser.THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH) {
  9650. if (dataLength[0] === firstThreeDigits) {
  9651. if (dataLength[1] === FieldParser.VARIABLE_LENGTH) {
  9652. return FieldParser.processVariableAI(4, dataLength[2], rawInformation);
  9653. }
  9654. return FieldParser.processFixedAI(4, dataLength[1], rawInformation);
  9655. }
  9656. }
  9657. if (rawInformation.length < 4) {
  9658. throw new NotFoundException();
  9659. }
  9660. let firstFourDigits = rawInformation.substring(0, 4);
  9661. for (let dataLength of FieldParser.FOUR_DIGIT_DATA_LENGTH) {
  9662. if (dataLength[0] === firstFourDigits) {
  9663. if (dataLength[1] === FieldParser.VARIABLE_LENGTH) {
  9664. return FieldParser.processVariableAI(4, dataLength[2], rawInformation);
  9665. }
  9666. return FieldParser.processFixedAI(4, dataLength[1], rawInformation);
  9667. }
  9668. }
  9669. throw new NotFoundException();
  9670. }
  9671. static processFixedAI(aiSize, fieldSize, rawInformation) {
  9672. if (rawInformation.length < aiSize) {
  9673. throw new NotFoundException();
  9674. }
  9675. let ai = rawInformation.substring(0, aiSize);
  9676. if (rawInformation.length < aiSize + fieldSize) {
  9677. throw new NotFoundException();
  9678. }
  9679. let field = rawInformation.substring(aiSize, aiSize + fieldSize);
  9680. let remaining = rawInformation.substring(aiSize + fieldSize);
  9681. let result = '(' + ai + ')' + field;
  9682. let parsedAI = FieldParser.parseFieldsInGeneralPurpose(remaining);
  9683. return parsedAI == null ? result : result + parsedAI;
  9684. }
  9685. static processVariableAI(aiSize, variableFieldSize, rawInformation) {
  9686. let ai = rawInformation.substring(0, aiSize);
  9687. let maxSize;
  9688. if (rawInformation.length < aiSize + variableFieldSize) {
  9689. maxSize = rawInformation.length;
  9690. }
  9691. else {
  9692. maxSize = aiSize + variableFieldSize;
  9693. }
  9694. let field = rawInformation.substring(aiSize, maxSize);
  9695. let remaining = rawInformation.substring(maxSize);
  9696. let result = '(' + ai + ')' + field;
  9697. let parsedAI = FieldParser.parseFieldsInGeneralPurpose(remaining);
  9698. return parsedAI == null ? result : result + parsedAI;
  9699. }
  9700. }
  9701. FieldParser.VARIABLE_LENGTH = [];
  9702. FieldParser.TWO_DIGIT_DATA_LENGTH = [
  9703. ['00', 18],
  9704. ['01', 14],
  9705. ['02', 14],
  9706. ['10', FieldParser.VARIABLE_LENGTH, 20],
  9707. ['11', 6],
  9708. ['12', 6],
  9709. ['13', 6],
  9710. ['15', 6],
  9711. ['17', 6],
  9712. ['20', 2],
  9713. ['21', FieldParser.VARIABLE_LENGTH, 20],
  9714. ['22', FieldParser.VARIABLE_LENGTH, 29],
  9715. ['30', FieldParser.VARIABLE_LENGTH, 8],
  9716. ['37', FieldParser.VARIABLE_LENGTH, 8],
  9717. // internal company codes
  9718. ['90', FieldParser.VARIABLE_LENGTH, 30],
  9719. ['91', FieldParser.VARIABLE_LENGTH, 30],
  9720. ['92', FieldParser.VARIABLE_LENGTH, 30],
  9721. ['93', FieldParser.VARIABLE_LENGTH, 30],
  9722. ['94', FieldParser.VARIABLE_LENGTH, 30],
  9723. ['95', FieldParser.VARIABLE_LENGTH, 30],
  9724. ['96', FieldParser.VARIABLE_LENGTH, 30],
  9725. ['97', FieldParser.VARIABLE_LENGTH, 3],
  9726. ['98', FieldParser.VARIABLE_LENGTH, 30],
  9727. ['99', FieldParser.VARIABLE_LENGTH, 30],
  9728. ];
  9729. FieldParser.THREE_DIGIT_DATA_LENGTH = [
  9730. // Same format as above
  9731. ['240', FieldParser.VARIABLE_LENGTH, 30],
  9732. ['241', FieldParser.VARIABLE_LENGTH, 30],
  9733. ['242', FieldParser.VARIABLE_LENGTH, 6],
  9734. ['250', FieldParser.VARIABLE_LENGTH, 30],
  9735. ['251', FieldParser.VARIABLE_LENGTH, 30],
  9736. ['253', FieldParser.VARIABLE_LENGTH, 17],
  9737. ['254', FieldParser.VARIABLE_LENGTH, 20],
  9738. ['400', FieldParser.VARIABLE_LENGTH, 30],
  9739. ['401', FieldParser.VARIABLE_LENGTH, 30],
  9740. ['402', 17],
  9741. ['403', FieldParser.VARIABLE_LENGTH, 30],
  9742. ['410', 13],
  9743. ['411', 13],
  9744. ['412', 13],
  9745. ['413', 13],
  9746. ['414', 13],
  9747. ['420', FieldParser.VARIABLE_LENGTH, 20],
  9748. ['421', FieldParser.VARIABLE_LENGTH, 15],
  9749. ['422', 3],
  9750. ['423', FieldParser.VARIABLE_LENGTH, 15],
  9751. ['424', 3],
  9752. ['425', 3],
  9753. ['426', 3],
  9754. ];
  9755. FieldParser.THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH = [
  9756. // Same format as above
  9757. ['310', 6],
  9758. ['311', 6],
  9759. ['312', 6],
  9760. ['313', 6],
  9761. ['314', 6],
  9762. ['315', 6],
  9763. ['316', 6],
  9764. ['320', 6],
  9765. ['321', 6],
  9766. ['322', 6],
  9767. ['323', 6],
  9768. ['324', 6],
  9769. ['325', 6],
  9770. ['326', 6],
  9771. ['327', 6],
  9772. ['328', 6],
  9773. ['329', 6],
  9774. ['330', 6],
  9775. ['331', 6],
  9776. ['332', 6],
  9777. ['333', 6],
  9778. ['334', 6],
  9779. ['335', 6],
  9780. ['336', 6],
  9781. ['340', 6],
  9782. ['341', 6],
  9783. ['342', 6],
  9784. ['343', 6],
  9785. ['344', 6],
  9786. ['345', 6],
  9787. ['346', 6],
  9788. ['347', 6],
  9789. ['348', 6],
  9790. ['349', 6],
  9791. ['350', 6],
  9792. ['351', 6],
  9793. ['352', 6],
  9794. ['353', 6],
  9795. ['354', 6],
  9796. ['355', 6],
  9797. ['356', 6],
  9798. ['357', 6],
  9799. ['360', 6],
  9800. ['361', 6],
  9801. ['362', 6],
  9802. ['363', 6],
  9803. ['364', 6],
  9804. ['365', 6],
  9805. ['366', 6],
  9806. ['367', 6],
  9807. ['368', 6],
  9808. ['369', 6],
  9809. ['390', FieldParser.VARIABLE_LENGTH, 15],
  9810. ['391', FieldParser.VARIABLE_LENGTH, 18],
  9811. ['392', FieldParser.VARIABLE_LENGTH, 15],
  9812. ['393', FieldParser.VARIABLE_LENGTH, 18],
  9813. ['703', FieldParser.VARIABLE_LENGTH, 30],
  9814. ];
  9815. FieldParser.FOUR_DIGIT_DATA_LENGTH = [
  9816. // Same format as above
  9817. ['7001', 13],
  9818. ['7002', FieldParser.VARIABLE_LENGTH, 30],
  9819. ['7003', 10],
  9820. ['8001', 14],
  9821. ['8002', FieldParser.VARIABLE_LENGTH, 20],
  9822. ['8003', FieldParser.VARIABLE_LENGTH, 30],
  9823. ['8004', FieldParser.VARIABLE_LENGTH, 30],
  9824. ['8005', 6],
  9825. ['8006', 18],
  9826. ['8007', FieldParser.VARIABLE_LENGTH, 30],
  9827. ['8008', FieldParser.VARIABLE_LENGTH, 12],
  9828. ['8018', 18],
  9829. ['8020', FieldParser.VARIABLE_LENGTH, 25],
  9830. ['8100', 6],
  9831. ['8101', 10],
  9832. ['8102', 2],
  9833. ['8110', FieldParser.VARIABLE_LENGTH, 70],
  9834. ['8200', FieldParser.VARIABLE_LENGTH, 70],
  9835. ];
  9836. class GeneralAppIdDecoder {
  9837. constructor(information) {
  9838. this.buffer = new StringBuilder();
  9839. this.information = information;
  9840. }
  9841. decodeAllCodes(buff, initialPosition) {
  9842. let currentPosition = initialPosition;
  9843. let remaining = null;
  9844. do {
  9845. let info = this.decodeGeneralPurposeField(currentPosition, remaining);
  9846. let parsedFields = FieldParser.parseFieldsInGeneralPurpose(info.getNewString());
  9847. if (parsedFields != null) {
  9848. buff.append(parsedFields);
  9849. }
  9850. if (info.isRemaining()) {
  9851. remaining = '' + info.getRemainingValue();
  9852. }
  9853. else {
  9854. remaining = null;
  9855. }
  9856. if (currentPosition === info.getNewPosition()) { // No step forward!
  9857. break;
  9858. }
  9859. currentPosition = info.getNewPosition();
  9860. } while (true);
  9861. return buff.toString();
  9862. }
  9863. isStillNumeric(pos) {
  9864. // It's numeric if it still has 7 positions
  9865. // and one of the first 4 bits is "1".
  9866. if (pos + 7 > this.information.getSize()) {
  9867. return pos + 4 <= this.information.getSize();
  9868. }
  9869. for (let i = pos; i < pos + 3; ++i) {
  9870. if (this.information.get(i)) {
  9871. return true;
  9872. }
  9873. }
  9874. return this.information.get(pos + 3);
  9875. }
  9876. decodeNumeric(pos) {
  9877. if (pos + 7 > this.information.getSize()) {
  9878. let numeric = this.extractNumericValueFromBitArray(pos, 4);
  9879. if (numeric === 0) {
  9880. return new DecodedNumeric(this.information.getSize(), DecodedNumeric.FNC1, DecodedNumeric.FNC1);
  9881. }
  9882. return new DecodedNumeric(this.information.getSize(), numeric - 1, DecodedNumeric.FNC1);
  9883. }
  9884. let numeric = this.extractNumericValueFromBitArray(pos, 7);
  9885. let digit1 = (numeric - 8) / 11;
  9886. let digit2 = (numeric - 8) % 11;
  9887. return new DecodedNumeric(pos + 7, digit1, digit2);
  9888. }
  9889. extractNumericValueFromBitArray(pos, bits) {
  9890. return GeneralAppIdDecoder.extractNumericValueFromBitArray(this.information, pos, bits);
  9891. }
  9892. static extractNumericValueFromBitArray(information, pos, bits) {
  9893. let value = 0;
  9894. for (let i = 0; i < bits; ++i) {
  9895. if (information.get(pos + i)) {
  9896. value |= 1 << (bits - i - 1);
  9897. }
  9898. }
  9899. return value;
  9900. }
  9901. decodeGeneralPurposeField(pos, remaining) {
  9902. // this.buffer.setLength(0);
  9903. this.buffer.setLengthToZero();
  9904. if (remaining != null) {
  9905. this.buffer.append(remaining);
  9906. }
  9907. this.current.setPosition(pos);
  9908. let lastDecoded = this.parseBlocks();
  9909. if (lastDecoded != null && lastDecoded.isRemaining()) {
  9910. return new DecodedInformation(this.current.getPosition(), this.buffer.toString(), lastDecoded.getRemainingValue());
  9911. }
  9912. return new DecodedInformation(this.current.getPosition(), this.buffer.toString());
  9913. }
  9914. parseBlocks() {
  9915. let isFinished;
  9916. let result;
  9917. do {
  9918. let initialPosition = this.current.getPosition();
  9919. if (this.current.isAlpha()) {
  9920. result = this.parseAlphaBlock();
  9921. isFinished = result.isFinished();
  9922. }
  9923. else if (this.current.isIsoIec646()) {
  9924. result = this.parseIsoIec646Block();
  9925. isFinished = result.isFinished();
  9926. }
  9927. else { // it must be numeric
  9928. result = this.parseNumericBlock();
  9929. isFinished = result.isFinished();
  9930. }
  9931. let positionChanged = initialPosition !== this.current.getPosition();
  9932. if (!positionChanged && !isFinished) {
  9933. break;
  9934. }
  9935. } while (!isFinished);
  9936. return result.getDecodedInformation();
  9937. }
  9938. parseNumericBlock() {
  9939. while (this.isStillNumeric(this.current.getPosition())) {
  9940. let numeric = this.decodeNumeric(this.current.getPosition());
  9941. this.current.setPosition(numeric.getNewPosition());
  9942. if (numeric.isFirstDigitFNC1()) {
  9943. let information;
  9944. if (numeric.isSecondDigitFNC1()) {
  9945. information = new DecodedInformation(this.current.getPosition(), this.buffer.toString());
  9946. }
  9947. else {
  9948. information = new DecodedInformation(this.current.getPosition(), this.buffer.toString(), numeric.getSecondDigit());
  9949. }
  9950. return new BlockParsedResult(true, information);
  9951. }
  9952. this.buffer.append(numeric.getFirstDigit());
  9953. if (numeric.isSecondDigitFNC1()) {
  9954. let information = new DecodedInformation(this.current.getPosition(), this.buffer.toString());
  9955. return new BlockParsedResult(true, information);
  9956. }
  9957. this.buffer.append(numeric.getSecondDigit());
  9958. }
  9959. if (this.isNumericToAlphaNumericLatch(this.current.getPosition())) {
  9960. this.current.setAlpha();
  9961. this.current.incrementPosition(4);
  9962. }
  9963. return new BlockParsedResult(false);
  9964. }
  9965. parseIsoIec646Block() {
  9966. while (this.isStillIsoIec646(this.current.getPosition())) {
  9967. let iso = this.decodeIsoIec646(this.current.getPosition());
  9968. this.current.setPosition(iso.getNewPosition());
  9969. if (iso.isFNC1()) {
  9970. let information = new DecodedInformation(this.current.getPosition(), this.buffer.toString());
  9971. return new BlockParsedResult(true, information);
  9972. }
  9973. this.buffer.append(iso.getValue());
  9974. }
  9975. if (this.isAlphaOr646ToNumericLatch(this.current.getPosition())) {
  9976. this.current.incrementPosition(3);
  9977. this.current.setNumeric();
  9978. }
  9979. else if (this.isAlphaTo646ToAlphaLatch(this.current.getPosition())) {
  9980. if (this.current.getPosition() + 5 < this.information.getSize()) {
  9981. this.current.incrementPosition(5);
  9982. }
  9983. else {
  9984. this.current.setPosition(this.information.getSize());
  9985. }
  9986. this.current.setAlpha();
  9987. }
  9988. return new BlockParsedResult(false);
  9989. }
  9990. parseAlphaBlock() {
  9991. while (this.isStillAlpha(this.current.getPosition())) {
  9992. let alpha = this.decodeAlphanumeric(this.current.getPosition());
  9993. this.current.setPosition(alpha.getNewPosition());
  9994. if (alpha.isFNC1()) {
  9995. let information = new DecodedInformation(this.current.getPosition(), this.buffer.toString());
  9996. return new BlockParsedResult(true, information); // end of the char block
  9997. }
  9998. this.buffer.append(alpha.getValue());
  9999. }
  10000. if (this.isAlphaOr646ToNumericLatch(this.current.getPosition())) {
  10001. this.current.incrementPosition(3);
  10002. this.current.setNumeric();
  10003. }
  10004. else if (this.isAlphaTo646ToAlphaLatch(this.current.getPosition())) {
  10005. if (this.current.getPosition() + 5 < this.information.getSize()) {
  10006. this.current.incrementPosition(5);
  10007. }
  10008. else {
  10009. this.current.setPosition(this.information.getSize());
  10010. }
  10011. this.current.setIsoIec646();
  10012. }
  10013. return new BlockParsedResult(false);
  10014. }
  10015. isStillIsoIec646(pos) {
  10016. if (pos + 5 > this.information.getSize()) {
  10017. return false;
  10018. }
  10019. let fiveBitValue = this.extractNumericValueFromBitArray(pos, 5);
  10020. if (fiveBitValue >= 5 && fiveBitValue < 16) {
  10021. return true;
  10022. }
  10023. if (pos + 7 > this.information.getSize()) {
  10024. return false;
  10025. }
  10026. let sevenBitValue = this.extractNumericValueFromBitArray(pos, 7);
  10027. if (sevenBitValue >= 64 && sevenBitValue < 116) {
  10028. return true;
  10029. }
  10030. if (pos + 8 > this.information.getSize()) {
  10031. return false;
  10032. }
  10033. let eightBitValue = this.extractNumericValueFromBitArray(pos, 8);
  10034. return eightBitValue >= 232 && eightBitValue < 253;
  10035. }
  10036. decodeIsoIec646(pos) {
  10037. let fiveBitValue = this.extractNumericValueFromBitArray(pos, 5);
  10038. if (fiveBitValue === 15) {
  10039. return new DecodedChar(pos + 5, DecodedChar.FNC1);
  10040. }
  10041. if (fiveBitValue >= 5 && fiveBitValue < 15) {
  10042. return new DecodedChar(pos + 5, ('0' + (fiveBitValue - 5)));
  10043. }
  10044. let sevenBitValue = this.extractNumericValueFromBitArray(pos, 7);
  10045. if (sevenBitValue >= 64 && sevenBitValue < 90) {
  10046. return new DecodedChar(pos + 7, ('' + (sevenBitValue + 1)));
  10047. }
  10048. if (sevenBitValue >= 90 && sevenBitValue < 116) {
  10049. return new DecodedChar(pos + 7, ('' + (sevenBitValue + 7)));
  10050. }
  10051. let eightBitValue = this.extractNumericValueFromBitArray(pos, 8);
  10052. let c;
  10053. switch (eightBitValue) {
  10054. case 232:
  10055. c = '!';
  10056. break;
  10057. case 233:
  10058. c = '"';
  10059. break;
  10060. case 234:
  10061. c = '%';
  10062. break;
  10063. case 235:
  10064. c = '&';
  10065. break;
  10066. case 236:
  10067. c = '\'';
  10068. break;
  10069. case 237:
  10070. c = '(';
  10071. break;
  10072. case 238:
  10073. c = ')';
  10074. break;
  10075. case 239:
  10076. c = '*';
  10077. break;
  10078. case 240:
  10079. c = '+';
  10080. break;
  10081. case 241:
  10082. c = ',';
  10083. break;
  10084. case 242:
  10085. c = '-';
  10086. break;
  10087. case 243:
  10088. c = '.';
  10089. break;
  10090. case 244:
  10091. c = '/';
  10092. break;
  10093. case 245:
  10094. c = ':';
  10095. break;
  10096. case 246:
  10097. c = ';';
  10098. break;
  10099. case 247:
  10100. c = '<';
  10101. break;
  10102. case 248:
  10103. c = '=';
  10104. break;
  10105. case 249:
  10106. c = '>';
  10107. break;
  10108. case 250:
  10109. c = '?';
  10110. break;
  10111. case 251:
  10112. c = '_';
  10113. break;
  10114. case 252:
  10115. c = ' ';
  10116. break;
  10117. default:
  10118. throw new FormatException();
  10119. }
  10120. return new DecodedChar(pos + 8, c);
  10121. }
  10122. isStillAlpha(pos) {
  10123. if (pos + 5 > this.information.getSize()) {
  10124. return false;
  10125. }
  10126. // We now check if it's a valid 5-bit value (0..9 and FNC1)
  10127. let fiveBitValue = this.extractNumericValueFromBitArray(pos, 5);
  10128. if (fiveBitValue >= 5 && fiveBitValue < 16) {
  10129. return true;
  10130. }
  10131. if (pos + 6 > this.information.getSize()) {
  10132. return false;
  10133. }
  10134. let sixBitValue = this.extractNumericValueFromBitArray(pos, 6);
  10135. return sixBitValue >= 16 && sixBitValue < 63; // 63 not included
  10136. }
  10137. decodeAlphanumeric(pos) {
  10138. let fiveBitValue = this.extractNumericValueFromBitArray(pos, 5);
  10139. if (fiveBitValue === 15) {
  10140. return new DecodedChar(pos + 5, DecodedChar.FNC1);
  10141. }
  10142. if (fiveBitValue >= 5 && fiveBitValue < 15) {
  10143. return new DecodedChar(pos + 5, ('0' + (fiveBitValue - 5)));
  10144. }
  10145. let sixBitValue = this.extractNumericValueFromBitArray(pos, 6);
  10146. if (sixBitValue >= 32 && sixBitValue < 58) {
  10147. return new DecodedChar(pos + 6, ('' + (sixBitValue + 33)));
  10148. }
  10149. let c;
  10150. switch (sixBitValue) {
  10151. case 58:
  10152. c = '*';
  10153. break;
  10154. case 59:
  10155. c = ',';
  10156. break;
  10157. case 60:
  10158. c = '-';
  10159. break;
  10160. case 61:
  10161. c = '.';
  10162. break;
  10163. case 62:
  10164. c = '/';
  10165. break;
  10166. default:
  10167. throw new IllegalStateException('Decoding invalid alphanumeric value: ' + sixBitValue);
  10168. }
  10169. return new DecodedChar(pos + 6, c);
  10170. }
  10171. isAlphaTo646ToAlphaLatch(pos) {
  10172. if (pos + 1 > this.information.getSize()) {
  10173. return false;
  10174. }
  10175. for (let i = 0; i < 5 && i + pos < this.information.getSize(); ++i) {
  10176. if (i === 2) {
  10177. if (!this.information.get(pos + 2)) {
  10178. return false;
  10179. }
  10180. }
  10181. else if (this.information.get(pos + i)) {
  10182. return false;
  10183. }
  10184. }
  10185. return true;
  10186. }
  10187. isAlphaOr646ToNumericLatch(pos) {
  10188. // Next is alphanumeric if there are 3 positions and they are all zeros
  10189. if (pos + 3 > this.information.getSize()) {
  10190. return false;
  10191. }
  10192. for (let i = pos; i < pos + 3; ++i) {
  10193. if (this.information.get(i)) {
  10194. return false;
  10195. }
  10196. }
  10197. return true;
  10198. }
  10199. isNumericToAlphaNumericLatch(pos) {
  10200. // Next is alphanumeric if there are 4 positions and they are all zeros, or
  10201. // if there is a subset of this just before the end of the symbol
  10202. if (pos + 1 > this.information.getSize()) {
  10203. return false;
  10204. }
  10205. for (let i = 0; i < 4 && i + pos < this.information.getSize(); ++i) {
  10206. if (this.information.get(pos + i)) {
  10207. return false;
  10208. }
  10209. }
  10210. return true;
  10211. }
  10212. }
  10213. class AbstractExpandedDecoder {
  10214. constructor(information) {
  10215. this.information = information;
  10216. this.generalDecoder = new GeneralAppIdDecoder(information);
  10217. }
  10218. getInformation() {
  10219. return this.information;
  10220. }
  10221. getGeneralDecoder() {
  10222. return this.generalDecoder;
  10223. }
  10224. }
  10225. class AI01decoder extends AbstractExpandedDecoder {
  10226. constructor(information) {
  10227. super(information);
  10228. }
  10229. encodeCompressedGtin(buf, currentPos) {
  10230. buf.append('(01)');
  10231. let initialPosition = buf.length();
  10232. buf.append('9');
  10233. this.encodeCompressedGtinWithoutAI(buf, currentPos, initialPosition);
  10234. }
  10235. encodeCompressedGtinWithoutAI(buf, currentPos, initialBufferPosition) {
  10236. for (let i = 0; i < 4; ++i) {
  10237. let currentBlock = this.getGeneralDecoder().extractNumericValueFromBitArray(currentPos + 10 * i, 10);
  10238. if (currentBlock / 100 === 0) {
  10239. buf.append('0');
  10240. }
  10241. if (currentBlock / 10 === 0) {
  10242. buf.append('0');
  10243. }
  10244. buf.append(currentBlock);
  10245. }
  10246. AI01decoder.appendCheckDigit(buf, initialBufferPosition);
  10247. }
  10248. static appendCheckDigit(buf, currentPos) {
  10249. let checkDigit = 0;
  10250. for (let i = 0; i < 13; i++) {
  10251. // let digit = buf.charAt(i + currentPos) - '0';
  10252. // To be checked
  10253. let digit = buf.charAt(i + currentPos).charCodeAt(0) - '0'.charCodeAt(0);
  10254. checkDigit += (i & 0x01) === 0 ? 3 * digit : digit;
  10255. }
  10256. checkDigit = 10 - (checkDigit % 10);
  10257. if (checkDigit === 10) {
  10258. checkDigit = 0;
  10259. }
  10260. buf.append(checkDigit);
  10261. }
  10262. }
  10263. AI01decoder.GTIN_SIZE = 40;
  10264. class AI01AndOtherAIs extends AI01decoder {
  10265. // the second one is the encodation method, and the other two are for the variable length
  10266. constructor(information) {
  10267. super(information);
  10268. }
  10269. parseInformation() {
  10270. let buff = new StringBuilder();
  10271. buff.append('(01)');
  10272. let initialGtinPosition = buff.length();
  10273. let firstGtinDigit = this.getGeneralDecoder().extractNumericValueFromBitArray(AI01AndOtherAIs.HEADER_SIZE, 4);
  10274. buff.append(firstGtinDigit);
  10275. this.encodeCompressedGtinWithoutAI(buff, AI01AndOtherAIs.HEADER_SIZE + 4, initialGtinPosition);
  10276. return this.getGeneralDecoder().decodeAllCodes(buff, AI01AndOtherAIs.HEADER_SIZE + 44);
  10277. }
  10278. }
  10279. AI01AndOtherAIs.HEADER_SIZE = 1 + 1 + 2; // first bit encodes the linkage flag,
  10280. class AnyAIDecoder extends AbstractExpandedDecoder {
  10281. constructor(information) {
  10282. super(information);
  10283. }
  10284. parseInformation() {
  10285. let buf = new StringBuilder();
  10286. return this.getGeneralDecoder().decodeAllCodes(buf, AnyAIDecoder.HEADER_SIZE);
  10287. }
  10288. }
  10289. AnyAIDecoder.HEADER_SIZE = 2 + 1 + 2;
  10290. class AI01weightDecoder extends AI01decoder {
  10291. constructor(information) {
  10292. super(information);
  10293. }
  10294. encodeCompressedWeight(buf, currentPos, weightSize) {
  10295. let originalWeightNumeric = this.getGeneralDecoder().extractNumericValueFromBitArray(currentPos, weightSize);
  10296. this.addWeightCode(buf, originalWeightNumeric);
  10297. let weightNumeric = this.checkWeight(originalWeightNumeric);
  10298. let currentDivisor = 100000;
  10299. for (let i = 0; i < 5; ++i) {
  10300. if (weightNumeric / currentDivisor === 0) {
  10301. buf.append('0');
  10302. }
  10303. currentDivisor /= 10;
  10304. }
  10305. buf.append(weightNumeric);
  10306. }
  10307. }
  10308. class AI013x0xDecoder extends AI01weightDecoder {
  10309. constructor(information) {
  10310. super(information);
  10311. }
  10312. parseInformation() {
  10313. if (this.getInformation().getSize() !==
  10314. AI013x0xDecoder.HEADER_SIZE +
  10315. AI01weightDecoder.GTIN_SIZE +
  10316. AI013x0xDecoder.WEIGHT_SIZE) {
  10317. throw new NotFoundException();
  10318. }
  10319. let buf = new StringBuilder();
  10320. this.encodeCompressedGtin(buf, AI013x0xDecoder.HEADER_SIZE);
  10321. this.encodeCompressedWeight(buf, AI013x0xDecoder.HEADER_SIZE + AI01weightDecoder.GTIN_SIZE, AI013x0xDecoder.WEIGHT_SIZE);
  10322. return buf.toString();
  10323. }
  10324. }
  10325. AI013x0xDecoder.HEADER_SIZE = 4 + 1;
  10326. AI013x0xDecoder.WEIGHT_SIZE = 15;
  10327. class AI013103decoder extends AI013x0xDecoder {
  10328. constructor(information) {
  10329. super(information);
  10330. }
  10331. addWeightCode(buf, weight) {
  10332. buf.append('(3103)');
  10333. }
  10334. checkWeight(weight) {
  10335. return weight;
  10336. }
  10337. }
  10338. class AI01320xDecoder extends AI013x0xDecoder {
  10339. constructor(information) {
  10340. super(information);
  10341. }
  10342. addWeightCode(buf, weight) {
  10343. if (weight < 10000) {
  10344. buf.append('(3202)');
  10345. }
  10346. else {
  10347. buf.append('(3203)');
  10348. }
  10349. }
  10350. checkWeight(weight) {
  10351. if (weight < 10000) {
  10352. return weight;
  10353. }
  10354. return weight - 10000;
  10355. }
  10356. }
  10357. class AI01392xDecoder extends AI01decoder {
  10358. constructor(information) {
  10359. super(information);
  10360. }
  10361. parseInformation() {
  10362. if (this.getInformation().getSize() < AI01392xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE) {
  10363. throw new NotFoundException();
  10364. }
  10365. let buf = new StringBuilder();
  10366. this.encodeCompressedGtin(buf, AI01392xDecoder.HEADER_SIZE);
  10367. let lastAIdigit = this.getGeneralDecoder().extractNumericValueFromBitArray(AI01392xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE, AI01392xDecoder.LAST_DIGIT_SIZE);
  10368. buf.append('(392');
  10369. buf.append(lastAIdigit);
  10370. buf.append(')');
  10371. let decodedInformation = this.getGeneralDecoder().decodeGeneralPurposeField(AI01392xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE + AI01392xDecoder.LAST_DIGIT_SIZE, null);
  10372. buf.append(decodedInformation.getNewString());
  10373. return buf.toString();
  10374. }
  10375. }
  10376. AI01392xDecoder.HEADER_SIZE = 5 + 1 + 2;
  10377. AI01392xDecoder.LAST_DIGIT_SIZE = 2;
  10378. class AI01393xDecoder extends AI01decoder {
  10379. constructor(information) {
  10380. super(information);
  10381. }
  10382. parseInformation() {
  10383. if (this.getInformation().getSize() <
  10384. AI01393xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE) {
  10385. throw new NotFoundException();
  10386. }
  10387. let buf = new StringBuilder();
  10388. this.encodeCompressedGtin(buf, AI01393xDecoder.HEADER_SIZE);
  10389. let lastAIdigit = this.getGeneralDecoder().extractNumericValueFromBitArray(AI01393xDecoder.HEADER_SIZE + AI01decoder.GTIN_SIZE, AI01393xDecoder.LAST_DIGIT_SIZE);
  10390. buf.append('(393');
  10391. buf.append(lastAIdigit);
  10392. buf.append(')');
  10393. let firstThreeDigits = this.getGeneralDecoder().extractNumericValueFromBitArray(AI01393xDecoder.HEADER_SIZE +
  10394. AI01decoder.GTIN_SIZE +
  10395. AI01393xDecoder.LAST_DIGIT_SIZE, AI01393xDecoder.FIRST_THREE_DIGITS_SIZE);
  10396. if (firstThreeDigits / 100 === 0) {
  10397. buf.append('0');
  10398. }
  10399. if (firstThreeDigits / 10 === 0) {
  10400. buf.append('0');
  10401. }
  10402. buf.append(firstThreeDigits);
  10403. let generalInformation = this.getGeneralDecoder().decodeGeneralPurposeField(AI01393xDecoder.HEADER_SIZE +
  10404. AI01decoder.GTIN_SIZE +
  10405. AI01393xDecoder.LAST_DIGIT_SIZE +
  10406. AI01393xDecoder.FIRST_THREE_DIGITS_SIZE, null);
  10407. buf.append(generalInformation.getNewString());
  10408. return buf.toString();
  10409. }
  10410. }
  10411. AI01393xDecoder.HEADER_SIZE = 5 + 1 + 2;
  10412. AI01393xDecoder.LAST_DIGIT_SIZE = 2;
  10413. AI01393xDecoder.FIRST_THREE_DIGITS_SIZE = 10;
  10414. class AI013x0x1xDecoder extends AI01weightDecoder {
  10415. constructor(information, firstAIdigits, dateCode) {
  10416. super(information);
  10417. this.dateCode = dateCode;
  10418. this.firstAIdigits = firstAIdigits;
  10419. }
  10420. parseInformation() {
  10421. if (this.getInformation().getSize() !==
  10422. AI013x0x1xDecoder.HEADER_SIZE +
  10423. AI013x0x1xDecoder.GTIN_SIZE +
  10424. AI013x0x1xDecoder.WEIGHT_SIZE +
  10425. AI013x0x1xDecoder.DATE_SIZE) {
  10426. throw new NotFoundException();
  10427. }
  10428. let buf = new StringBuilder();
  10429. this.encodeCompressedGtin(buf, AI013x0x1xDecoder.HEADER_SIZE);
  10430. this.encodeCompressedWeight(buf, AI013x0x1xDecoder.HEADER_SIZE + AI013x0x1xDecoder.GTIN_SIZE, AI013x0x1xDecoder.WEIGHT_SIZE);
  10431. this.encodeCompressedDate(buf, AI013x0x1xDecoder.HEADER_SIZE +
  10432. AI013x0x1xDecoder.GTIN_SIZE +
  10433. AI013x0x1xDecoder.WEIGHT_SIZE);
  10434. return buf.toString();
  10435. }
  10436. encodeCompressedDate(buf, currentPos) {
  10437. let numericDate = this.getGeneralDecoder().extractNumericValueFromBitArray(currentPos, AI013x0x1xDecoder.DATE_SIZE);
  10438. if (numericDate === 38400) {
  10439. return;
  10440. }
  10441. buf.append('(');
  10442. buf.append(this.dateCode);
  10443. buf.append(')');
  10444. let day = numericDate % 32;
  10445. numericDate /= 32;
  10446. let month = (numericDate % 12) + 1;
  10447. numericDate /= 12;
  10448. let year = numericDate;
  10449. if (year / 10 === 0) {
  10450. buf.append('0');
  10451. }
  10452. buf.append(year);
  10453. if (month / 10 === 0) {
  10454. buf.append('0');
  10455. }
  10456. buf.append(month);
  10457. if (day / 10 === 0) {
  10458. buf.append('0');
  10459. }
  10460. buf.append(day);
  10461. }
  10462. addWeightCode(buf, weight) {
  10463. buf.append('(');
  10464. buf.append(this.firstAIdigits);
  10465. buf.append(weight / 100000);
  10466. buf.append(')');
  10467. }
  10468. checkWeight(weight) {
  10469. return weight % 100000;
  10470. }
  10471. }
  10472. AI013x0x1xDecoder.HEADER_SIZE = 7 + 1;
  10473. AI013x0x1xDecoder.WEIGHT_SIZE = 20;
  10474. AI013x0x1xDecoder.DATE_SIZE = 16;
  10475. function createDecoder(information) {
  10476. try {
  10477. if (information.get(1)) {
  10478. return new AI01AndOtherAIs(information);
  10479. }
  10480. if (!information.get(2)) {
  10481. return new AnyAIDecoder(information);
  10482. }
  10483. let fourBitEncodationMethod = GeneralAppIdDecoder.extractNumericValueFromBitArray(information, 1, 4);
  10484. switch (fourBitEncodationMethod) {
  10485. case 4: return new AI013103decoder(information);
  10486. case 5: return new AI01320xDecoder(information);
  10487. }
  10488. let fiveBitEncodationMethod = GeneralAppIdDecoder.extractNumericValueFromBitArray(information, 1, 5);
  10489. switch (fiveBitEncodationMethod) {
  10490. case 12: return new AI01392xDecoder(information);
  10491. case 13: return new AI01393xDecoder(information);
  10492. }
  10493. let sevenBitEncodationMethod = GeneralAppIdDecoder.extractNumericValueFromBitArray(information, 1, 7);
  10494. switch (sevenBitEncodationMethod) {
  10495. case 56: return new AI013x0x1xDecoder(information, '310', '11');
  10496. case 57: return new AI013x0x1xDecoder(information, '320', '11');
  10497. case 58: return new AI013x0x1xDecoder(information, '310', '13');
  10498. case 59: return new AI013x0x1xDecoder(information, '320', '13');
  10499. case 60: return new AI013x0x1xDecoder(information, '310', '15');
  10500. case 61: return new AI013x0x1xDecoder(information, '320', '15');
  10501. case 62: return new AI013x0x1xDecoder(information, '310', '17');
  10502. case 63: return new AI013x0x1xDecoder(information, '320', '17');
  10503. }
  10504. }
  10505. catch (e) {
  10506. console.log(e);
  10507. throw new IllegalStateException('unknown decoder: ' + information);
  10508. }
  10509. }
  10510. class ExpandedPair {
  10511. constructor(leftChar, rightChar, finderPatter, mayBeLast) {
  10512. this.leftchar = leftChar;
  10513. this.rightchar = rightChar;
  10514. this.finderpattern = finderPatter;
  10515. this.maybeLast = mayBeLast;
  10516. }
  10517. mayBeLast() {
  10518. return this.maybeLast;
  10519. }
  10520. getLeftChar() {
  10521. return this.leftchar;
  10522. }
  10523. getRightChar() {
  10524. return this.rightchar;
  10525. }
  10526. getFinderPattern() {
  10527. return this.finderpattern;
  10528. }
  10529. mustBeLast() {
  10530. return this.rightchar == null;
  10531. }
  10532. toString() {
  10533. return '[ ' + this.leftchar + ', ' + this.rightchar + ' : ' + (this.finderpattern == null ? 'null' : this.finderpattern.getValue()) + ' ]';
  10534. }
  10535. static equals(o1, o2) {
  10536. if (!(o1 instanceof ExpandedPair)) {
  10537. return false;
  10538. }
  10539. return ExpandedPair.equalsOrNull(o1.leftchar, o2.leftchar) &&
  10540. ExpandedPair.equalsOrNull(o1.rightchar, o2.rightchar) &&
  10541. ExpandedPair.equalsOrNull(o1.finderpattern, o2.finderpattern);
  10542. }
  10543. static equalsOrNull(o1, o2) {
  10544. return o1 === null ? o2 === null : ExpandedPair.equals(o1, o2);
  10545. }
  10546. hashCode() {
  10547. // return ExpandedPair.hashNotNull(leftChar) ^ hashNotNull(rightChar) ^ hashNotNull(finderPattern);
  10548. let value = this.leftchar.getValue() ^ this.rightchar.getValue() ^ this.finderpattern.getValue();
  10549. return value;
  10550. }
  10551. }
  10552. class ExpandedRow {
  10553. constructor(pairs, rowNumber, wasReversed) {
  10554. this.pairs = pairs;
  10555. this.rowNumber = rowNumber;
  10556. this.wasReversed = wasReversed;
  10557. }
  10558. getPairs() {
  10559. return this.pairs;
  10560. }
  10561. getRowNumber() {
  10562. return this.rowNumber;
  10563. }
  10564. isReversed() {
  10565. return this.wasReversed;
  10566. }
  10567. // check implementation
  10568. isEquivalent(otherPairs) {
  10569. return this.checkEqualitity(this, otherPairs);
  10570. }
  10571. // @Override
  10572. toString() {
  10573. return '{ ' + this.pairs + ' }';
  10574. }
  10575. /**
  10576. * Two rows are equal if they contain the same pairs in the same order.
  10577. */
  10578. // @Override
  10579. // check implementation
  10580. equals(o1, o2) {
  10581. if (!(o1 instanceof ExpandedRow)) {
  10582. return false;
  10583. }
  10584. return this.checkEqualitity(o1, o2) && o1.wasReversed === o2.wasReversed;
  10585. }
  10586. checkEqualitity(pair1, pair2) {
  10587. if (!pair1 || !pair2)
  10588. return;
  10589. let result;
  10590. pair1.forEach((e1, i) => {
  10591. pair2.forEach(e2 => {
  10592. if (e1.getLeftChar().getValue() === e2.getLeftChar().getValue() && e1.getRightChar().getValue() === e2.getRightChar().getValue() && e1.getFinderPatter().getValue() === e2.getFinderPatter().getValue()) {
  10593. result = true;
  10594. }
  10595. });
  10596. });
  10597. return result;
  10598. }
  10599. }
  10600. // import java.util.ArrayList;
  10601. // import java.util.Iterator;
  10602. // import java.util.List;
  10603. // import java.util.Map;
  10604. // import java.util.Collections;
  10605. /** @experimental */
  10606. class RSSExpandedReader extends AbstractRSSReader {
  10607. constructor() {
  10608. super(...arguments);
  10609. this.pairs = new Array(RSSExpandedReader.MAX_PAIRS);
  10610. this.rows = new Array();
  10611. this.startEnd = [2];
  10612. }
  10613. decodeRow(rowNumber, row, hints) {
  10614. // Rows can start with even pattern in case in prev rows there where odd number of patters.
  10615. // So lets try twice
  10616. // this.pairs.clear();
  10617. this.pairs.length = 0;
  10618. this.startFromEven = false;
  10619. try {
  10620. return RSSExpandedReader.constructResult(this.decodeRow2pairs(rowNumber, row));
  10621. }
  10622. catch (e) {
  10623. // OK
  10624. // console.log(e);
  10625. }
  10626. this.pairs.length = 0;
  10627. this.startFromEven = true;
  10628. return RSSExpandedReader.constructResult(this.decodeRow2pairs(rowNumber, row));
  10629. }
  10630. reset() {
  10631. this.pairs.length = 0;
  10632. this.rows.length = 0;
  10633. }
  10634. // Not private for testing
  10635. decodeRow2pairs(rowNumber, row) {
  10636. let done = false;
  10637. while (!done) {
  10638. try {
  10639. this.pairs.push(this.retrieveNextPair(row, this.pairs, rowNumber));
  10640. }
  10641. catch (error) {
  10642. if (error instanceof NotFoundException) {
  10643. if (!this.pairs.length) {
  10644. throw new NotFoundException();
  10645. }
  10646. // exit this loop when retrieveNextPair() fails and throws
  10647. done = true;
  10648. }
  10649. }
  10650. }
  10651. // TODO: verify sequence of finder patterns as in checkPairSequence()
  10652. if (this.checkChecksum()) {
  10653. return this.pairs;
  10654. }
  10655. let tryStackedDecode;
  10656. if (this.rows.length) {
  10657. tryStackedDecode = true;
  10658. }
  10659. else {
  10660. tryStackedDecode = false;
  10661. }
  10662. // let tryStackedDecode = !this.rows.isEmpty();
  10663. this.storeRow(rowNumber, false); // TODO: deal with reversed rows
  10664. if (tryStackedDecode) {
  10665. // When the image is 180-rotated, then rows are sorted in wrong direction.
  10666. // Try twice with both the directions.
  10667. let ps = this.checkRowsBoolean(false);
  10668. if (ps != null) {
  10669. return ps;
  10670. }
  10671. ps = this.checkRowsBoolean(true);
  10672. if (ps != null) {
  10673. return ps;
  10674. }
  10675. }
  10676. throw new NotFoundException();
  10677. }
  10678. // Need to Verify
  10679. checkRowsBoolean(reverse) {
  10680. // Limit number of rows we are checking
  10681. // We use recursive algorithm with pure complexity and don't want it to take forever
  10682. // Stacked barcode can have up to 11 rows, so 25 seems reasonable enough
  10683. if (this.rows.length > 25) {
  10684. this.rows.length = 0; // We will never have a chance to get result, so clear it
  10685. return null;
  10686. }
  10687. this.pairs.length = 0;
  10688. if (reverse) {
  10689. this.rows = this.rows.reverse();
  10690. // Collections.reverse(this.rows);
  10691. }
  10692. let ps = null;
  10693. try {
  10694. ps = this.checkRows(new Array(), 0);
  10695. }
  10696. catch (e) {
  10697. // OK
  10698. console.log(e);
  10699. }
  10700. if (reverse) {
  10701. this.rows = this.rows.reverse();
  10702. // Collections.reverse(this.rows);
  10703. }
  10704. return ps;
  10705. }
  10706. // Try to construct a valid rows sequence
  10707. // Recursion is used to implement backtracking
  10708. checkRows(collectedRows, currentRow) {
  10709. for (let i = currentRow; i < this.rows.length; i++) {
  10710. let row = this.rows[i];
  10711. this.pairs.length = 0;
  10712. for (let collectedRow of collectedRows) {
  10713. this.pairs.push(collectedRow.getPairs());
  10714. }
  10715. this.pairs.push(row.getPairs());
  10716. if (!RSSExpandedReader.isValidSequence(this.pairs)) {
  10717. continue;
  10718. }
  10719. if (this.checkChecksum()) {
  10720. return this.pairs;
  10721. }
  10722. let rs = new Array(collectedRows);
  10723. rs.push(row);
  10724. try {
  10725. // Recursion: try to add more rows
  10726. return this.checkRows(rs, i + 1);
  10727. }
  10728. catch (e) {
  10729. // We failed, try the next candidate
  10730. console.log(e);
  10731. }
  10732. }
  10733. throw new NotFoundException();
  10734. }
  10735. // Whether the pairs form a valid find pattern sequence,
  10736. // either complete or a prefix
  10737. static isValidSequence(pairs) {
  10738. for (let sequence of RSSExpandedReader.FINDER_PATTERN_SEQUENCES) {
  10739. if (pairs.length > sequence.length) {
  10740. continue;
  10741. }
  10742. let stop = true;
  10743. for (let j = 0; j < pairs.length; j++) {
  10744. if (pairs[j].getFinderPattern().getValue() !== sequence[j]) {
  10745. stop = false;
  10746. break;
  10747. }
  10748. }
  10749. if (stop) {
  10750. return true;
  10751. }
  10752. }
  10753. return false;
  10754. }
  10755. storeRow(rowNumber, wasReversed) {
  10756. // Discard if duplicate above or below; otherwise insert in order by row number.
  10757. let insertPos = 0;
  10758. let prevIsSame = false;
  10759. let nextIsSame = false;
  10760. while (insertPos < this.rows.length) {
  10761. let erow = this.rows[insertPos];
  10762. if (erow.getRowNumber() > rowNumber) {
  10763. nextIsSame = erow.isEquivalent(this.pairs);
  10764. break;
  10765. }
  10766. prevIsSame = erow.isEquivalent(this.pairs);
  10767. insertPos++;
  10768. }
  10769. if (nextIsSame || prevIsSame) {
  10770. return;
  10771. }
  10772. // When the row was partially decoded (e.g. 2 pairs found instead of 3),
  10773. // it will prevent us from detecting the barcode.
  10774. // Try to merge partial rows
  10775. // Check whether the row is part of an allready detected row
  10776. if (RSSExpandedReader.isPartialRow(this.pairs, this.rows)) {
  10777. return;
  10778. }
  10779. this.rows.push(insertPos, new ExpandedRow(this.pairs, rowNumber, wasReversed));
  10780. this.removePartialRows(this.pairs, this.rows);
  10781. }
  10782. // Remove all the rows that contains only specified pairs
  10783. removePartialRows(pairs, rows) {
  10784. // for (Iterator<ExpandedRow> iterator = rows.iterator(); iterator.hasNext();) {
  10785. // ExpandedRow r = iterator.next();
  10786. // if (r.getPairs().size() == pairs.size()) {
  10787. // continue;
  10788. // }
  10789. // boolean allFound = true;
  10790. // for (ExpandedPair p : r.getPairs()) {
  10791. // boolean found = false;
  10792. // for (ExpandedPair pp : pairs) {
  10793. // if (p.equals(pp)) {
  10794. // found = true;
  10795. // break;
  10796. // }
  10797. // }
  10798. // if (!found) {
  10799. // allFound = false;
  10800. // break;
  10801. // }
  10802. // }
  10803. // if (allFound) {
  10804. // // 'pairs' contains all the pairs from the row 'r'
  10805. // iterator.remove();
  10806. // }
  10807. // }
  10808. for (let row of rows) {
  10809. if (row.getPairs().length === pairs.length) {
  10810. continue;
  10811. }
  10812. for (let p of row.getPairs()) {
  10813. for (let pp of pairs) {
  10814. if (ExpandedPair.equals(p, pp)) {
  10815. break;
  10816. }
  10817. }
  10818. }
  10819. }
  10820. }
  10821. // Returns true when one of the rows already contains all the pairs
  10822. static isPartialRow(pairs, rows) {
  10823. for (let r of rows) {
  10824. let allFound = true;
  10825. for (let p of pairs) {
  10826. let found = false;
  10827. for (let pp of r.getPairs()) {
  10828. if (p.equals(pp)) {
  10829. found = true;
  10830. break;
  10831. }
  10832. }
  10833. if (!found) {
  10834. allFound = false;
  10835. break;
  10836. }
  10837. }
  10838. if (allFound) {
  10839. // the row 'r' contain all the pairs from 'pairs'
  10840. return true;
  10841. }
  10842. }
  10843. return false;
  10844. }
  10845. // Only used for unit testing
  10846. getRows() {
  10847. return this.rows;
  10848. }
  10849. // Not private for unit testing
  10850. static constructResult(pairs) {
  10851. let binary = BitArrayBuilder.buildBitArray(pairs);
  10852. let decoder = createDecoder(binary);
  10853. let resultingString = decoder.parseInformation();
  10854. let firstPoints = pairs[0].getFinderPattern().getResultPoints();
  10855. let lastPoints = pairs[pairs.length - 1]
  10856. .getFinderPattern()
  10857. .getResultPoints();
  10858. let points = [firstPoints[0], firstPoints[1], lastPoints[0], lastPoints[1]];
  10859. return new Result$1(resultingString, null, null, points, BarcodeFormat$1.RSS_EXPANDED, null);
  10860. }
  10861. checkChecksum() {
  10862. let firstPair = this.pairs.get(0);
  10863. let checkCharacter = firstPair.getLeftChar();
  10864. let firstCharacter = firstPair.getRightChar();
  10865. if (firstCharacter === null) {
  10866. return false;
  10867. }
  10868. let checksum = firstCharacter.getChecksumPortion();
  10869. let s = 2;
  10870. for (let i = 1; i < this.pairs.size(); ++i) {
  10871. let currentPair = this.pairs.get(i);
  10872. checksum += currentPair.getLeftChar().getChecksumPortion();
  10873. s++;
  10874. let currentRightChar = currentPair.getRightChar();
  10875. if (currentRightChar != null) {
  10876. checksum += currentRightChar.getChecksumPortion();
  10877. s++;
  10878. }
  10879. }
  10880. checksum %= 211;
  10881. let checkCharacterValue = 211 * (s - 4) + checksum;
  10882. return checkCharacterValue === checkCharacter.getValue();
  10883. }
  10884. static getNextSecondBar(row, initialPos) {
  10885. let currentPos;
  10886. if (row.get(initialPos)) {
  10887. currentPos = row.getNextUnset(initialPos);
  10888. currentPos = row.getNextSet(currentPos);
  10889. }
  10890. else {
  10891. currentPos = row.getNextSet(initialPos);
  10892. currentPos = row.getNextUnset(currentPos);
  10893. }
  10894. return currentPos;
  10895. }
  10896. // not private for testing
  10897. retrieveNextPair(row, previousPairs, rowNumber) {
  10898. let isOddPattern = previousPairs.length % 2 === 0;
  10899. if (this.startFromEven) {
  10900. isOddPattern = !isOddPattern;
  10901. }
  10902. let pattern;
  10903. let keepFinding = true;
  10904. let forcedOffset = -1;
  10905. do {
  10906. this.findNextPair(row, previousPairs, forcedOffset);
  10907. pattern = this.parseFoundFinderPattern(row, rowNumber, isOddPattern);
  10908. if (pattern === null) {
  10909. forcedOffset = RSSExpandedReader.getNextSecondBar(row, this.startEnd[0]);
  10910. }
  10911. else {
  10912. keepFinding = false;
  10913. }
  10914. } while (keepFinding);
  10915. // When stacked symbol is split over multiple rows, there's no way to guess if this pair can be last or not.
  10916. // boolean mayBeLast = checkPairSequence(previousPairs, pattern);
  10917. let leftChar = this.decodeDataCharacter(row, pattern, isOddPattern, true);
  10918. if (!this.isEmptyPair(previousPairs) &&
  10919. previousPairs[previousPairs.length - 1].mustBeLast()) {
  10920. throw new NotFoundException();
  10921. }
  10922. let rightChar;
  10923. try {
  10924. rightChar = this.decodeDataCharacter(row, pattern, isOddPattern, false);
  10925. }
  10926. catch (e) {
  10927. rightChar = null;
  10928. console.log(e);
  10929. }
  10930. return new ExpandedPair(leftChar, rightChar, pattern, true);
  10931. }
  10932. isEmptyPair(pairs) {
  10933. if (pairs.length === 0) {
  10934. return true;
  10935. }
  10936. return false;
  10937. }
  10938. findNextPair(row, previousPairs, forcedOffset) {
  10939. let counters = this.getDecodeFinderCounters();
  10940. counters[0] = 0;
  10941. counters[1] = 0;
  10942. counters[2] = 0;
  10943. counters[3] = 0;
  10944. let width = row.getSize();
  10945. let rowOffset;
  10946. if (forcedOffset >= 0) {
  10947. rowOffset = forcedOffset;
  10948. }
  10949. else if (this.isEmptyPair(previousPairs)) {
  10950. rowOffset = 0;
  10951. }
  10952. else {
  10953. let lastPair = previousPairs[previousPairs.length - 1];
  10954. rowOffset = lastPair.getFinderPattern().getStartEnd()[1];
  10955. }
  10956. let searchingEvenPair = previousPairs.length % 2 !== 0;
  10957. if (this.startFromEven) {
  10958. searchingEvenPair = !searchingEvenPair;
  10959. }
  10960. let isWhite = false;
  10961. while (rowOffset < width) {
  10962. isWhite = !row.get(rowOffset);
  10963. if (!isWhite) {
  10964. break;
  10965. }
  10966. rowOffset++;
  10967. }
  10968. let counterPosition = 0;
  10969. let patternStart = rowOffset;
  10970. for (let x = rowOffset; x < width; x++) {
  10971. if (row.get(x) !== isWhite) {
  10972. counters[counterPosition]++;
  10973. }
  10974. else {
  10975. if (counterPosition === 3) {
  10976. if (searchingEvenPair) {
  10977. RSSExpandedReader.reverseCounters(counters);
  10978. }
  10979. if (RSSExpandedReader.isFinderPattern(counters)) {
  10980. this.startEnd[0] = patternStart;
  10981. this.startEnd[1] = x;
  10982. return;
  10983. }
  10984. if (searchingEvenPair) {
  10985. RSSExpandedReader.reverseCounters(counters);
  10986. }
  10987. patternStart += counters[0] + counters[1];
  10988. counters[0] = counters[2];
  10989. counters[1] = counters[3];
  10990. counters[2] = 0;
  10991. counters[3] = 0;
  10992. counterPosition--;
  10993. }
  10994. else {
  10995. counterPosition++;
  10996. }
  10997. counters[counterPosition] = 1;
  10998. isWhite = !isWhite;
  10999. }
  11000. }
  11001. throw new NotFoundException();
  11002. }
  11003. static reverseCounters(counters) {
  11004. let length = counters.length;
  11005. for (let i = 0; i < length / 2; ++i) {
  11006. let tmp = counters[i];
  11007. counters[i] = counters[length - i - 1];
  11008. counters[length - i - 1] = tmp;
  11009. }
  11010. }
  11011. parseFoundFinderPattern(row, rowNumber, oddPattern) {
  11012. // Actually we found elements 2-5.
  11013. let firstCounter;
  11014. let start;
  11015. let end;
  11016. if (oddPattern) {
  11017. // If pattern number is odd, we need to locate element 1 *before* the current block.
  11018. let firstElementStart = this.startEnd[0] - 1;
  11019. // Locate element 1
  11020. while (firstElementStart >= 0 && !row.get(firstElementStart)) {
  11021. firstElementStart--;
  11022. }
  11023. firstElementStart++;
  11024. firstCounter = this.startEnd[0] - firstElementStart;
  11025. start = firstElementStart;
  11026. end = this.startEnd[1];
  11027. }
  11028. else {
  11029. // If pattern number is even, the pattern is reversed, so we need to locate element 1 *after* the current block.
  11030. start = this.startEnd[0];
  11031. end = row.getNextUnset(this.startEnd[1] + 1);
  11032. firstCounter = end - this.startEnd[1];
  11033. }
  11034. // Make 'counters' hold 1-4
  11035. let counters = this.getDecodeFinderCounters();
  11036. System.arraycopy(counters, 0, counters, 1, counters.length - 1);
  11037. counters[0] = firstCounter;
  11038. let value;
  11039. try {
  11040. value = this.parseFinderValue(counters, RSSExpandedReader.FINDER_PATTERNS);
  11041. }
  11042. catch (e) {
  11043. return null;
  11044. }
  11045. // return new FinderPattern(value, new int[] { start, end }, start, end, rowNumber});
  11046. return new FinderPattern$1(value, [start, end], start, end, rowNumber);
  11047. }
  11048. decodeDataCharacter(row, pattern, isOddPattern, leftChar) {
  11049. let counters = this.getDataCharacterCounters();
  11050. for (let x = 0; x < counters.length; x++) {
  11051. counters[x] = 0;
  11052. }
  11053. if (leftChar) {
  11054. RSSExpandedReader.recordPatternInReverse(row, pattern.getStartEnd()[0], counters);
  11055. }
  11056. else {
  11057. RSSExpandedReader.recordPattern(row, pattern.getStartEnd()[1], counters);
  11058. // reverse it
  11059. for (let i = 0, j = counters.length - 1; i < j; i++, j--) {
  11060. let temp = counters[i];
  11061. counters[i] = counters[j];
  11062. counters[j] = temp;
  11063. }
  11064. } // counters[] has the pixels of the module
  11065. let numModules = 17; // left and right data characters have all the same length
  11066. let elementWidth = MathUtils.sum(new Int32Array(counters)) / numModules;
  11067. // Sanity check: element width for pattern and the character should match
  11068. let expectedElementWidth = (pattern.getStartEnd()[1] - pattern.getStartEnd()[0]) / 15.0;
  11069. if (Math.abs(elementWidth - expectedElementWidth) / expectedElementWidth >
  11070. 0.3) {
  11071. throw new NotFoundException();
  11072. }
  11073. let oddCounts = this.getOddCounts();
  11074. let evenCounts = this.getEvenCounts();
  11075. let oddRoundingErrors = this.getOddRoundingErrors();
  11076. let evenRoundingErrors = this.getEvenRoundingErrors();
  11077. for (let i = 0; i < counters.length; i++) {
  11078. let value = (1.0 * counters[i]) / elementWidth;
  11079. let count = value + 0.5; // Round
  11080. if (count < 1) {
  11081. if (value < 0.3) {
  11082. throw new NotFoundException();
  11083. }
  11084. count = 1;
  11085. }
  11086. else if (count > 8) {
  11087. if (value > 8.7) {
  11088. throw new NotFoundException();
  11089. }
  11090. count = 8;
  11091. }
  11092. let offset = i / 2;
  11093. if ((i & 0x01) === 0) {
  11094. oddCounts[offset] = count;
  11095. oddRoundingErrors[offset] = value - count;
  11096. }
  11097. else {
  11098. evenCounts[offset] = count;
  11099. evenRoundingErrors[offset] = value - count;
  11100. }
  11101. }
  11102. this.adjustOddEvenCounts(numModules);
  11103. let weightRowNumber = 4 * pattern.getValue() + (isOddPattern ? 0 : 2) + (leftChar ? 0 : 1) - 1;
  11104. let oddSum = 0;
  11105. let oddChecksumPortion = 0;
  11106. for (let i = oddCounts.length - 1; i >= 0; i--) {
  11107. if (RSSExpandedReader.isNotA1left(pattern, isOddPattern, leftChar)) {
  11108. let weight = RSSExpandedReader.WEIGHTS[weightRowNumber][2 * i];
  11109. oddChecksumPortion += oddCounts[i] * weight;
  11110. }
  11111. oddSum += oddCounts[i];
  11112. }
  11113. let evenChecksumPortion = 0;
  11114. // int evenSum = 0;
  11115. for (let i = evenCounts.length - 1; i >= 0; i--) {
  11116. if (RSSExpandedReader.isNotA1left(pattern, isOddPattern, leftChar)) {
  11117. let weight = RSSExpandedReader.WEIGHTS[weightRowNumber][2 * i + 1];
  11118. evenChecksumPortion += evenCounts[i] * weight;
  11119. }
  11120. // evenSum += evenCounts[i];
  11121. }
  11122. let checksumPortion = oddChecksumPortion + evenChecksumPortion;
  11123. if ((oddSum & 0x01) !== 0 || oddSum > 13 || oddSum < 4) {
  11124. throw new NotFoundException();
  11125. }
  11126. let group = (13 - oddSum) / 2;
  11127. let oddWidest = RSSExpandedReader.SYMBOL_WIDEST[group];
  11128. let evenWidest = 9 - oddWidest;
  11129. let vOdd = RSSUtils.getRSSvalue(oddCounts, oddWidest, true);
  11130. let vEven = RSSUtils.getRSSvalue(evenCounts, evenWidest, false);
  11131. let tEven = RSSExpandedReader.EVEN_TOTAL_SUBSET[group];
  11132. let gSum = RSSExpandedReader.GSUM[group];
  11133. let value = vOdd * tEven + vEven + gSum;
  11134. return new DataCharacter(value, checksumPortion);
  11135. }
  11136. static isNotA1left(pattern, isOddPattern, leftChar) {
  11137. // A1: pattern.getValue is 0 (A), and it's an oddPattern, and it is a left char
  11138. return !(pattern.getValue() === 0 && isOddPattern && leftChar);
  11139. }
  11140. adjustOddEvenCounts(numModules) {
  11141. let oddSum = MathUtils.sum(new Int32Array(this.getOddCounts()));
  11142. let evenSum = MathUtils.sum(new Int32Array(this.getEvenCounts()));
  11143. let incrementOdd = false;
  11144. let decrementOdd = false;
  11145. if (oddSum > 13) {
  11146. decrementOdd = true;
  11147. }
  11148. else if (oddSum < 4) {
  11149. incrementOdd = true;
  11150. }
  11151. let incrementEven = false;
  11152. let decrementEven = false;
  11153. if (evenSum > 13) {
  11154. decrementEven = true;
  11155. }
  11156. else if (evenSum < 4) {
  11157. incrementEven = true;
  11158. }
  11159. let mismatch = oddSum + evenSum - numModules;
  11160. let oddParityBad = (oddSum & 0x01) === 1;
  11161. let evenParityBad = (evenSum & 0x01) === 0;
  11162. if (mismatch === 1) {
  11163. if (oddParityBad) {
  11164. if (evenParityBad) {
  11165. throw new NotFoundException();
  11166. }
  11167. decrementOdd = true;
  11168. }
  11169. else {
  11170. if (!evenParityBad) {
  11171. throw new NotFoundException();
  11172. }
  11173. decrementEven = true;
  11174. }
  11175. }
  11176. else if (mismatch === -1) {
  11177. if (oddParityBad) {
  11178. if (evenParityBad) {
  11179. throw new NotFoundException();
  11180. }
  11181. incrementOdd = true;
  11182. }
  11183. else {
  11184. if (!evenParityBad) {
  11185. throw new NotFoundException();
  11186. }
  11187. incrementEven = true;
  11188. }
  11189. }
  11190. else if (mismatch === 0) {
  11191. if (oddParityBad) {
  11192. if (!evenParityBad) {
  11193. throw new NotFoundException();
  11194. }
  11195. // Both bad
  11196. if (oddSum < evenSum) {
  11197. incrementOdd = true;
  11198. decrementEven = true;
  11199. }
  11200. else {
  11201. decrementOdd = true;
  11202. incrementEven = true;
  11203. }
  11204. }
  11205. else {
  11206. if (evenParityBad) {
  11207. throw new NotFoundException();
  11208. }
  11209. // Nothing to do!
  11210. }
  11211. }
  11212. else {
  11213. throw new NotFoundException();
  11214. }
  11215. if (incrementOdd) {
  11216. if (decrementOdd) {
  11217. throw new NotFoundException();
  11218. }
  11219. RSSExpandedReader.increment(this.getOddCounts(), this.getOddRoundingErrors());
  11220. }
  11221. if (decrementOdd) {
  11222. RSSExpandedReader.decrement(this.getOddCounts(), this.getOddRoundingErrors());
  11223. }
  11224. if (incrementEven) {
  11225. if (decrementEven) {
  11226. throw new NotFoundException();
  11227. }
  11228. RSSExpandedReader.increment(this.getEvenCounts(), this.getOddRoundingErrors());
  11229. }
  11230. if (decrementEven) {
  11231. RSSExpandedReader.decrement(this.getEvenCounts(), this.getEvenRoundingErrors());
  11232. }
  11233. }
  11234. }
  11235. RSSExpandedReader.SYMBOL_WIDEST = [7, 5, 4, 3, 1];
  11236. RSSExpandedReader.EVEN_TOTAL_SUBSET = [4, 20, 52, 104, 204];
  11237. RSSExpandedReader.GSUM = [0, 348, 1388, 2948, 3988];
  11238. RSSExpandedReader.FINDER_PATTERNS = [
  11239. Int32Array.from([1, 8, 4, 1]),
  11240. Int32Array.from([3, 6, 4, 1]),
  11241. Int32Array.from([3, 4, 6, 1]),
  11242. Int32Array.from([3, 2, 8, 1]),
  11243. Int32Array.from([2, 6, 5, 1]),
  11244. Int32Array.from([2, 2, 9, 1]),
  11245. ];
  11246. RSSExpandedReader.WEIGHTS = [
  11247. [1, 3, 9, 27, 81, 32, 96, 77],
  11248. [20, 60, 180, 118, 143, 7, 21, 63],
  11249. [189, 145, 13, 39, 117, 140, 209, 205],
  11250. [193, 157, 49, 147, 19, 57, 171, 91],
  11251. [62, 186, 136, 197, 169, 85, 44, 132],
  11252. [185, 133, 188, 142, 4, 12, 36, 108],
  11253. [113, 128, 173, 97, 80, 29, 87, 50],
  11254. [150, 28, 84, 41, 123, 158, 52, 156],
  11255. [46, 138, 203, 187, 139, 206, 196, 166],
  11256. [76, 17, 51, 153, 37, 111, 122, 155],
  11257. [43, 129, 176, 106, 107, 110, 119, 146],
  11258. [16, 48, 144, 10, 30, 90, 59, 177],
  11259. [109, 116, 137, 200, 178, 112, 125, 164],
  11260. [70, 210, 208, 202, 184, 130, 179, 115],
  11261. [134, 191, 151, 31, 93, 68, 204, 190],
  11262. [148, 22, 66, 198, 172, 94, 71, 2],
  11263. [6, 18, 54, 162, 64, 192, 154, 40],
  11264. [120, 149, 25, 75, 14, 42, 126, 167],
  11265. [79, 26, 78, 23, 69, 207, 199, 175],
  11266. [103, 98, 83, 38, 114, 131, 182, 124],
  11267. [161, 61, 183, 127, 170, 88, 53, 159],
  11268. [55, 165, 73, 8, 24, 72, 5, 15],
  11269. [45, 135, 194, 160, 58, 174, 100, 89],
  11270. ];
  11271. RSSExpandedReader.FINDER_PAT_A = 0;
  11272. RSSExpandedReader.FINDER_PAT_B = 1;
  11273. RSSExpandedReader.FINDER_PAT_C = 2;
  11274. RSSExpandedReader.FINDER_PAT_D = 3;
  11275. RSSExpandedReader.FINDER_PAT_E = 4;
  11276. RSSExpandedReader.FINDER_PAT_F = 5;
  11277. RSSExpandedReader.FINDER_PATTERN_SEQUENCES = [
  11278. [RSSExpandedReader.FINDER_PAT_A, RSSExpandedReader.FINDER_PAT_A],
  11279. [
  11280. RSSExpandedReader.FINDER_PAT_A,
  11281. RSSExpandedReader.FINDER_PAT_B,
  11282. RSSExpandedReader.FINDER_PAT_B,
  11283. ],
  11284. [
  11285. RSSExpandedReader.FINDER_PAT_A,
  11286. RSSExpandedReader.FINDER_PAT_C,
  11287. RSSExpandedReader.FINDER_PAT_B,
  11288. RSSExpandedReader.FINDER_PAT_D,
  11289. ],
  11290. [
  11291. RSSExpandedReader.FINDER_PAT_A,
  11292. RSSExpandedReader.FINDER_PAT_E,
  11293. RSSExpandedReader.FINDER_PAT_B,
  11294. RSSExpandedReader.FINDER_PAT_D,
  11295. RSSExpandedReader.FINDER_PAT_C,
  11296. ],
  11297. [
  11298. RSSExpandedReader.FINDER_PAT_A,
  11299. RSSExpandedReader.FINDER_PAT_E,
  11300. RSSExpandedReader.FINDER_PAT_B,
  11301. RSSExpandedReader.FINDER_PAT_D,
  11302. RSSExpandedReader.FINDER_PAT_D,
  11303. RSSExpandedReader.FINDER_PAT_F,
  11304. ],
  11305. [
  11306. RSSExpandedReader.FINDER_PAT_A,
  11307. RSSExpandedReader.FINDER_PAT_E,
  11308. RSSExpandedReader.FINDER_PAT_B,
  11309. RSSExpandedReader.FINDER_PAT_D,
  11310. RSSExpandedReader.FINDER_PAT_E,
  11311. RSSExpandedReader.FINDER_PAT_F,
  11312. RSSExpandedReader.FINDER_PAT_F,
  11313. ],
  11314. [
  11315. RSSExpandedReader.FINDER_PAT_A,
  11316. RSSExpandedReader.FINDER_PAT_A,
  11317. RSSExpandedReader.FINDER_PAT_B,
  11318. RSSExpandedReader.FINDER_PAT_B,
  11319. RSSExpandedReader.FINDER_PAT_C,
  11320. RSSExpandedReader.FINDER_PAT_C,
  11321. RSSExpandedReader.FINDER_PAT_D,
  11322. RSSExpandedReader.FINDER_PAT_D,
  11323. ],
  11324. [
  11325. RSSExpandedReader.FINDER_PAT_A,
  11326. RSSExpandedReader.FINDER_PAT_A,
  11327. RSSExpandedReader.FINDER_PAT_B,
  11328. RSSExpandedReader.FINDER_PAT_B,
  11329. RSSExpandedReader.FINDER_PAT_C,
  11330. RSSExpandedReader.FINDER_PAT_C,
  11331. RSSExpandedReader.FINDER_PAT_D,
  11332. RSSExpandedReader.FINDER_PAT_E,
  11333. RSSExpandedReader.FINDER_PAT_E,
  11334. ],
  11335. [
  11336. RSSExpandedReader.FINDER_PAT_A,
  11337. RSSExpandedReader.FINDER_PAT_A,
  11338. RSSExpandedReader.FINDER_PAT_B,
  11339. RSSExpandedReader.FINDER_PAT_B,
  11340. RSSExpandedReader.FINDER_PAT_C,
  11341. RSSExpandedReader.FINDER_PAT_C,
  11342. RSSExpandedReader.FINDER_PAT_D,
  11343. RSSExpandedReader.FINDER_PAT_E,
  11344. RSSExpandedReader.FINDER_PAT_F,
  11345. RSSExpandedReader.FINDER_PAT_F,
  11346. ],
  11347. [
  11348. RSSExpandedReader.FINDER_PAT_A,
  11349. RSSExpandedReader.FINDER_PAT_A,
  11350. RSSExpandedReader.FINDER_PAT_B,
  11351. RSSExpandedReader.FINDER_PAT_B,
  11352. RSSExpandedReader.FINDER_PAT_C,
  11353. RSSExpandedReader.FINDER_PAT_D,
  11354. RSSExpandedReader.FINDER_PAT_D,
  11355. RSSExpandedReader.FINDER_PAT_E,
  11356. RSSExpandedReader.FINDER_PAT_E,
  11357. RSSExpandedReader.FINDER_PAT_F,
  11358. RSSExpandedReader.FINDER_PAT_F,
  11359. ],
  11360. ];
  11361. RSSExpandedReader.MAX_PAIRS = 11;
  11362. class Pair extends DataCharacter {
  11363. constructor(value, checksumPortion, finderPattern) {
  11364. super(value, checksumPortion);
  11365. this.count = 0;
  11366. this.finderPattern = finderPattern;
  11367. }
  11368. getFinderPattern() {
  11369. return this.finderPattern;
  11370. }
  11371. getCount() {
  11372. return this.count;
  11373. }
  11374. incrementCount() {
  11375. this.count++;
  11376. }
  11377. }
  11378. class RSS14Reader extends AbstractRSSReader {
  11379. constructor() {
  11380. super(...arguments);
  11381. this.possibleLeftPairs = [];
  11382. this.possibleRightPairs = [];
  11383. }
  11384. decodeRow(rowNumber, row, hints) {
  11385. const leftPair = this.decodePair(row, false, rowNumber, hints);
  11386. RSS14Reader.addOrTally(this.possibleLeftPairs, leftPair);
  11387. row.reverse();
  11388. let rightPair = this.decodePair(row, true, rowNumber, hints);
  11389. RSS14Reader.addOrTally(this.possibleRightPairs, rightPair);
  11390. row.reverse();
  11391. for (let left of this.possibleLeftPairs) {
  11392. if (left.getCount() > 1) {
  11393. for (let right of this.possibleRightPairs) {
  11394. if (right.getCount() > 1 && RSS14Reader.checkChecksum(left, right)) {
  11395. return RSS14Reader.constructResult(left, right);
  11396. }
  11397. }
  11398. }
  11399. }
  11400. throw new NotFoundException();
  11401. }
  11402. static addOrTally(possiblePairs, pair) {
  11403. if (pair == null) {
  11404. return;
  11405. }
  11406. let found = false;
  11407. for (let other of possiblePairs) {
  11408. if (other.getValue() === pair.getValue()) {
  11409. other.incrementCount();
  11410. found = true;
  11411. break;
  11412. }
  11413. }
  11414. if (!found) {
  11415. possiblePairs.push(pair);
  11416. }
  11417. }
  11418. reset() {
  11419. this.possibleLeftPairs.length = 0;
  11420. this.possibleRightPairs.length = 0;
  11421. }
  11422. static constructResult(leftPair, rightPair) {
  11423. let symbolValue = 4537077 * leftPair.getValue() + rightPair.getValue();
  11424. let text = new String(symbolValue).toString();
  11425. let buffer = new StringBuilder();
  11426. for (let i = 13 - text.length; i > 0; i--) {
  11427. buffer.append('0');
  11428. }
  11429. buffer.append(text);
  11430. let checkDigit = 0;
  11431. for (let i = 0; i < 13; i++) {
  11432. let digit = buffer.charAt(i).charCodeAt(0) - '0'.charCodeAt(0);
  11433. checkDigit += ((i & 0x01) === 0) ? 3 * digit : digit;
  11434. }
  11435. checkDigit = 10 - (checkDigit % 10);
  11436. if (checkDigit === 10) {
  11437. checkDigit = 0;
  11438. }
  11439. buffer.append(checkDigit.toString());
  11440. let leftPoints = leftPair.getFinderPattern().getResultPoints();
  11441. let rightPoints = rightPair.getFinderPattern().getResultPoints();
  11442. return new Result$1(buffer.toString(), null, 0, [leftPoints[0], leftPoints[1], rightPoints[0], rightPoints[1]], BarcodeFormat$1.RSS_14, new Date().getTime());
  11443. }
  11444. static checkChecksum(leftPair, rightPair) {
  11445. let checkValue = (leftPair.getChecksumPortion() + 16 * rightPair.getChecksumPortion()) % 79;
  11446. let targetCheckValue = 9 * leftPair.getFinderPattern().getValue() + rightPair.getFinderPattern().getValue();
  11447. if (targetCheckValue > 72) {
  11448. targetCheckValue--;
  11449. }
  11450. if (targetCheckValue > 8) {
  11451. targetCheckValue--;
  11452. }
  11453. return checkValue === targetCheckValue;
  11454. }
  11455. decodePair(row, right, rowNumber, hints) {
  11456. try {
  11457. let startEnd = this.findFinderPattern(row, right);
  11458. let pattern = this.parseFoundFinderPattern(row, rowNumber, right, startEnd);
  11459. let resultPointCallback = hints == null ? null : hints.get(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK);
  11460. if (resultPointCallback != null) {
  11461. let center = (startEnd[0] + startEnd[1]) / 2.0;
  11462. if (right) {
  11463. // row is actually reversed
  11464. center = row.getSize() - 1 - center;
  11465. }
  11466. resultPointCallback.foundPossibleResultPoint(new ResultPoint(center, rowNumber));
  11467. }
  11468. let outside = this.decodeDataCharacter(row, pattern, true);
  11469. let inside = this.decodeDataCharacter(row, pattern, false);
  11470. return new Pair(1597 * outside.getValue() + inside.getValue(), outside.getChecksumPortion() + 4 * inside.getChecksumPortion(), pattern);
  11471. }
  11472. catch (err) {
  11473. return null;
  11474. }
  11475. }
  11476. decodeDataCharacter(row, pattern, outsideChar) {
  11477. let counters = this.getDataCharacterCounters();
  11478. for (let x = 0; x < counters.length; x++) {
  11479. counters[x] = 0;
  11480. }
  11481. if (outsideChar) {
  11482. OneDReader.recordPatternInReverse(row, pattern.getStartEnd()[0], counters);
  11483. }
  11484. else {
  11485. OneDReader.recordPattern(row, pattern.getStartEnd()[1] + 1, counters);
  11486. // reverse it
  11487. for (let i = 0, j = counters.length - 1; i < j; i++, j--) {
  11488. let temp = counters[i];
  11489. counters[i] = counters[j];
  11490. counters[j] = temp;
  11491. }
  11492. }
  11493. let numModules = outsideChar ? 16 : 15;
  11494. let elementWidth = MathUtils.sum(new Int32Array(counters)) / numModules;
  11495. let oddCounts = this.getOddCounts();
  11496. let evenCounts = this.getEvenCounts();
  11497. let oddRoundingErrors = this.getOddRoundingErrors();
  11498. let evenRoundingErrors = this.getEvenRoundingErrors();
  11499. for (let i = 0; i < counters.length; i++) {
  11500. let value = counters[i] / elementWidth;
  11501. let count = Math.floor(value + 0.5);
  11502. if (count < 1) {
  11503. count = 1;
  11504. }
  11505. else if (count > 8) {
  11506. count = 8;
  11507. }
  11508. let offset = Math.floor(i / 2);
  11509. if ((i & 0x01) === 0) {
  11510. oddCounts[offset] = count;
  11511. oddRoundingErrors[offset] = value - count;
  11512. }
  11513. else {
  11514. evenCounts[offset] = count;
  11515. evenRoundingErrors[offset] = value - count;
  11516. }
  11517. }
  11518. this.adjustOddEvenCounts(outsideChar, numModules);
  11519. let oddSum = 0;
  11520. let oddChecksumPortion = 0;
  11521. for (let i = oddCounts.length - 1; i >= 0; i--) {
  11522. oddChecksumPortion *= 9;
  11523. oddChecksumPortion += oddCounts[i];
  11524. oddSum += oddCounts[i];
  11525. }
  11526. let evenChecksumPortion = 0;
  11527. let evenSum = 0;
  11528. for (let i = evenCounts.length - 1; i >= 0; i--) {
  11529. evenChecksumPortion *= 9;
  11530. evenChecksumPortion += evenCounts[i];
  11531. evenSum += evenCounts[i];
  11532. }
  11533. let checksumPortion = oddChecksumPortion + 3 * evenChecksumPortion;
  11534. if (outsideChar) {
  11535. if ((oddSum & 0x01) !== 0 || oddSum > 12 || oddSum < 4) {
  11536. throw new NotFoundException();
  11537. }
  11538. let group = (12 - oddSum) / 2;
  11539. let oddWidest = RSS14Reader.OUTSIDE_ODD_WIDEST[group];
  11540. let evenWidest = 9 - oddWidest;
  11541. let vOdd = RSSUtils.getRSSvalue(oddCounts, oddWidest, false);
  11542. let vEven = RSSUtils.getRSSvalue(evenCounts, evenWidest, true);
  11543. let tEven = RSS14Reader.OUTSIDE_EVEN_TOTAL_SUBSET[group];
  11544. let gSum = RSS14Reader.OUTSIDE_GSUM[group];
  11545. return new DataCharacter(vOdd * tEven + vEven + gSum, checksumPortion);
  11546. }
  11547. else {
  11548. if ((evenSum & 0x01) !== 0 || evenSum > 10 || evenSum < 4) {
  11549. throw new NotFoundException();
  11550. }
  11551. let group = (10 - evenSum) / 2;
  11552. let oddWidest = RSS14Reader.INSIDE_ODD_WIDEST[group];
  11553. let evenWidest = 9 - oddWidest;
  11554. let vOdd = RSSUtils.getRSSvalue(oddCounts, oddWidest, true);
  11555. let vEven = RSSUtils.getRSSvalue(evenCounts, evenWidest, false);
  11556. let tOdd = RSS14Reader.INSIDE_ODD_TOTAL_SUBSET[group];
  11557. let gSum = RSS14Reader.INSIDE_GSUM[group];
  11558. return new DataCharacter(vEven * tOdd + vOdd + gSum, checksumPortion);
  11559. }
  11560. }
  11561. findFinderPattern(row, rightFinderPattern) {
  11562. let counters = this.getDecodeFinderCounters();
  11563. counters[0] = 0;
  11564. counters[1] = 0;
  11565. counters[2] = 0;
  11566. counters[3] = 0;
  11567. let width = row.getSize();
  11568. let isWhite = false;
  11569. let rowOffset = 0;
  11570. while (rowOffset < width) {
  11571. isWhite = !row.get(rowOffset);
  11572. if (rightFinderPattern === isWhite) {
  11573. // Will encounter white first when searching for right finder pattern
  11574. break;
  11575. }
  11576. rowOffset++;
  11577. }
  11578. let counterPosition = 0;
  11579. let patternStart = rowOffset;
  11580. for (let x = rowOffset; x < width; x++) {
  11581. if (row.get(x) !== isWhite) {
  11582. counters[counterPosition]++;
  11583. }
  11584. else {
  11585. if (counterPosition === 3) {
  11586. if (AbstractRSSReader.isFinderPattern(counters)) {
  11587. return [patternStart, x];
  11588. }
  11589. patternStart += counters[0] + counters[1];
  11590. counters[0] = counters[2];
  11591. counters[1] = counters[3];
  11592. counters[2] = 0;
  11593. counters[3] = 0;
  11594. counterPosition--;
  11595. }
  11596. else {
  11597. counterPosition++;
  11598. }
  11599. counters[counterPosition] = 1;
  11600. isWhite = !isWhite;
  11601. }
  11602. }
  11603. throw new NotFoundException();
  11604. }
  11605. parseFoundFinderPattern(row, rowNumber, right, startEnd) {
  11606. // Actually we found elements 2-5
  11607. let firstIsBlack = row.get(startEnd[0]);
  11608. let firstElementStart = startEnd[0] - 1;
  11609. // Locate element 1
  11610. while (firstElementStart >= 0 && firstIsBlack !== row.get(firstElementStart)) {
  11611. firstElementStart--;
  11612. }
  11613. firstElementStart++;
  11614. const firstCounter = startEnd[0] - firstElementStart;
  11615. // Make 'counters' hold 1-4
  11616. const counters = this.getDecodeFinderCounters();
  11617. const copy = new Int32Array(counters.length);
  11618. System.arraycopy(counters, 0, copy, 1, counters.length - 1);
  11619. copy[0] = firstCounter;
  11620. const value = this.parseFinderValue(copy, RSS14Reader.FINDER_PATTERNS);
  11621. let start = firstElementStart;
  11622. let end = startEnd[1];
  11623. if (right) {
  11624. // row is actually reversed
  11625. start = row.getSize() - 1 - start;
  11626. end = row.getSize() - 1 - end;
  11627. }
  11628. return new FinderPattern$1(value, [firstElementStart, startEnd[1]], start, end, rowNumber);
  11629. }
  11630. adjustOddEvenCounts(outsideChar, numModules) {
  11631. let oddSum = MathUtils.sum(new Int32Array(this.getOddCounts()));
  11632. let evenSum = MathUtils.sum(new Int32Array(this.getEvenCounts()));
  11633. let incrementOdd = false;
  11634. let decrementOdd = false;
  11635. let incrementEven = false;
  11636. let decrementEven = false;
  11637. if (outsideChar) {
  11638. if (oddSum > 12) {
  11639. decrementOdd = true;
  11640. }
  11641. else if (oddSum < 4) {
  11642. incrementOdd = true;
  11643. }
  11644. if (evenSum > 12) {
  11645. decrementEven = true;
  11646. }
  11647. else if (evenSum < 4) {
  11648. incrementEven = true;
  11649. }
  11650. }
  11651. else {
  11652. if (oddSum > 11) {
  11653. decrementOdd = true;
  11654. }
  11655. else if (oddSum < 5) {
  11656. incrementOdd = true;
  11657. }
  11658. if (evenSum > 10) {
  11659. decrementEven = true;
  11660. }
  11661. else if (evenSum < 4) {
  11662. incrementEven = true;
  11663. }
  11664. }
  11665. let mismatch = oddSum + evenSum - numModules;
  11666. let oddParityBad = (oddSum & 0x01) === (outsideChar ? 1 : 0);
  11667. let evenParityBad = (evenSum & 0x01) === 1;
  11668. if (mismatch === 1) {
  11669. if (oddParityBad) {
  11670. if (evenParityBad) {
  11671. throw new NotFoundException();
  11672. }
  11673. decrementOdd = true;
  11674. }
  11675. else {
  11676. if (!evenParityBad) {
  11677. throw new NotFoundException();
  11678. }
  11679. decrementEven = true;
  11680. }
  11681. }
  11682. else if (mismatch === -1) {
  11683. if (oddParityBad) {
  11684. if (evenParityBad) {
  11685. throw new NotFoundException();
  11686. }
  11687. incrementOdd = true;
  11688. }
  11689. else {
  11690. if (!evenParityBad) {
  11691. throw new NotFoundException();
  11692. }
  11693. incrementEven = true;
  11694. }
  11695. }
  11696. else if (mismatch === 0) {
  11697. if (oddParityBad) {
  11698. if (!evenParityBad) {
  11699. throw new NotFoundException();
  11700. }
  11701. // Both bad
  11702. if (oddSum < evenSum) {
  11703. incrementOdd = true;
  11704. decrementEven = true;
  11705. }
  11706. else {
  11707. decrementOdd = true;
  11708. incrementEven = true;
  11709. }
  11710. }
  11711. else {
  11712. if (evenParityBad) {
  11713. throw new NotFoundException();
  11714. }
  11715. // Nothing to do!
  11716. }
  11717. }
  11718. else {
  11719. throw new NotFoundException();
  11720. }
  11721. if (incrementOdd) {
  11722. if (decrementOdd) {
  11723. throw new NotFoundException();
  11724. }
  11725. AbstractRSSReader.increment(this.getOddCounts(), this.getOddRoundingErrors());
  11726. }
  11727. if (decrementOdd) {
  11728. AbstractRSSReader.decrement(this.getOddCounts(), this.getOddRoundingErrors());
  11729. }
  11730. if (incrementEven) {
  11731. if (decrementEven) {
  11732. throw new NotFoundException();
  11733. }
  11734. AbstractRSSReader.increment(this.getEvenCounts(), this.getOddRoundingErrors());
  11735. }
  11736. if (decrementEven) {
  11737. AbstractRSSReader.decrement(this.getEvenCounts(), this.getEvenRoundingErrors());
  11738. }
  11739. }
  11740. }
  11741. RSS14Reader.OUTSIDE_EVEN_TOTAL_SUBSET = [1, 10, 34, 70, 126];
  11742. RSS14Reader.INSIDE_ODD_TOTAL_SUBSET = [4, 20, 48, 81];
  11743. RSS14Reader.OUTSIDE_GSUM = [0, 161, 961, 2015, 2715];
  11744. RSS14Reader.INSIDE_GSUM = [0, 336, 1036, 1516];
  11745. RSS14Reader.OUTSIDE_ODD_WIDEST = [8, 6, 4, 3, 1];
  11746. RSS14Reader.INSIDE_ODD_WIDEST = [2, 4, 6, 8];
  11747. RSS14Reader.FINDER_PATTERNS = [
  11748. Int32Array.from([3, 8, 2, 1]),
  11749. Int32Array.from([3, 5, 5, 1]),
  11750. Int32Array.from([3, 3, 7, 1]),
  11751. Int32Array.from([3, 1, 9, 1]),
  11752. Int32Array.from([2, 7, 4, 1]),
  11753. Int32Array.from([2, 5, 6, 1]),
  11754. Int32Array.from([2, 3, 8, 1]),
  11755. Int32Array.from([1, 5, 7, 1]),
  11756. Int32Array.from([1, 3, 9, 1]),
  11757. ];
  11758. /*
  11759. * Copyright 2008 ZXing authors
  11760. *
  11761. * Licensed under the Apache License, Version 2.0 (the "License");
  11762. * you may not use this file except in compliance with the License.
  11763. * You may obtain a copy of the License at
  11764. *
  11765. * http://www.apache.org/licenses/LICENSE-2.0
  11766. *
  11767. * Unless required by applicable law or agreed to in writing, software
  11768. * distributed under the License is distributed on an "AS IS" BASIS,
  11769. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11770. * See the License for the specific language governing permissions and
  11771. * limitations under the License.
  11772. */
  11773. /**
  11774. * @author Daniel Switkin <dswitkin@google.com>
  11775. * @author Sean Owen
  11776. */
  11777. class MultiFormatOneDReader extends OneDReader {
  11778. constructor(hints) {
  11779. super();
  11780. this.readers = [];
  11781. const possibleFormats = !hints ? null : hints.get(DecodeHintType$1.POSSIBLE_FORMATS);
  11782. const useCode39CheckDigit = hints && hints.get(DecodeHintType$1.ASSUME_CODE_39_CHECK_DIGIT) !== undefined;
  11783. const useCode39ExtendedMode = hints && hints.get(DecodeHintType$1.ENABLE_CODE_39_EXTENDED_MODE) !== undefined;
  11784. if (possibleFormats) {
  11785. if (possibleFormats.includes(BarcodeFormat$1.EAN_13) ||
  11786. possibleFormats.includes(BarcodeFormat$1.UPC_A) ||
  11787. possibleFormats.includes(BarcodeFormat$1.EAN_8) ||
  11788. possibleFormats.includes(BarcodeFormat$1.UPC_E)) {
  11789. this.readers.push(new MultiFormatUPCEANReader(hints));
  11790. }
  11791. if (possibleFormats.includes(BarcodeFormat$1.CODE_39)) {
  11792. this.readers.push(new Code39Reader(useCode39CheckDigit, useCode39ExtendedMode));
  11793. }
  11794. if (possibleFormats.includes(BarcodeFormat$1.CODE_93)) {
  11795. this.readers.push(new Code93Reader());
  11796. }
  11797. if (possibleFormats.includes(BarcodeFormat$1.CODE_128)) {
  11798. this.readers.push(new Code128Reader());
  11799. }
  11800. if (possibleFormats.includes(BarcodeFormat$1.ITF)) {
  11801. this.readers.push(new ITFReader());
  11802. }
  11803. if (possibleFormats.includes(BarcodeFormat$1.CODABAR)) {
  11804. this.readers.push(new CodaBarReader());
  11805. }
  11806. if (possibleFormats.includes(BarcodeFormat$1.RSS_14)) {
  11807. this.readers.push(new RSS14Reader());
  11808. }
  11809. if (possibleFormats.includes(BarcodeFormat$1.RSS_EXPANDED)) {
  11810. console.warn('RSS Expanded reader IS NOT ready for production yet! use at your own risk.');
  11811. this.readers.push(new RSSExpandedReader());
  11812. }
  11813. }
  11814. if (this.readers.length === 0) {
  11815. this.readers.push(new MultiFormatUPCEANReader(hints));
  11816. this.readers.push(new Code39Reader());
  11817. // this.readers.push(new CodaBarReader());
  11818. this.readers.push(new Code93Reader());
  11819. this.readers.push(new MultiFormatUPCEANReader(hints));
  11820. this.readers.push(new Code128Reader());
  11821. this.readers.push(new ITFReader());
  11822. this.readers.push(new RSS14Reader());
  11823. // this.readers.push(new RSSExpandedReader());
  11824. }
  11825. }
  11826. // @Override
  11827. decodeRow(rowNumber, row, hints) {
  11828. for (let i = 0; i < this.readers.length; i++) {
  11829. try {
  11830. return this.readers[i].decodeRow(rowNumber, row, hints);
  11831. }
  11832. catch (re) {
  11833. // continue
  11834. }
  11835. }
  11836. throw new NotFoundException();
  11837. }
  11838. // @Override
  11839. reset() {
  11840. this.readers.forEach(reader => reader.reset());
  11841. }
  11842. }
  11843. /**
  11844. * @deprecated Moving to @zxing/browser
  11845. *
  11846. * Barcode reader reader to use from browser.
  11847. */
  11848. class BrowserBarcodeReader extends BrowserCodeReader {
  11849. /**
  11850. * Creates an instance of BrowserBarcodeReader.
  11851. * @param {number} [timeBetweenScansMillis=500] the time delay between subsequent decode tries
  11852. * @param {Map<DecodeHintType, any>} hints
  11853. */
  11854. constructor(timeBetweenScansMillis = 500, hints) {
  11855. super(new MultiFormatOneDReader(hints), timeBetweenScansMillis, hints);
  11856. }
  11857. }
  11858. /*
  11859. * Copyright 2007 ZXing authors
  11860. *
  11861. * Licensed under the Apache License, Version 2.0 (the "License");
  11862. * you may not use this file except in compliance with the License.
  11863. * You may obtain a copy of the License at
  11864. *
  11865. * http://www.apache.org/licenses/LICENSE-2.0
  11866. *
  11867. * Unless required by applicable law or agreed to in writing, software
  11868. * distributed under the License is distributed on an "AS IS" BASIS,
  11869. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11870. * See the License for the specific language governing permissions and
  11871. * limitations under the License.
  11872. */
  11873. /**
  11874. * <p>Encapsulates a set of error-correction blocks in one symbol version. Most versions will
  11875. * use blocks of differing sizes within one version, so, this encapsulates the parameters for
  11876. * each set of blocks. It also holds the number of error-correction codewords per block since it
  11877. * will be the same across all blocks within one version.</p>
  11878. */
  11879. class ECBlocks$1 {
  11880. constructor(ecCodewords, ecBlocks1, ecBlocks2) {
  11881. this.ecCodewords = ecCodewords;
  11882. this.ecBlocks = [ecBlocks1];
  11883. ecBlocks2 && this.ecBlocks.push(ecBlocks2);
  11884. }
  11885. getECCodewords() {
  11886. return this.ecCodewords;
  11887. }
  11888. getECBlocks() {
  11889. return this.ecBlocks;
  11890. }
  11891. }
  11892. /**
  11893. * <p>Encapsulates the parameters for one error-correction block in one symbol version.
  11894. * This includes the number of data codewords, and the number of times a block with these
  11895. * parameters is used consecutively in the Data Matrix code version's format.</p>
  11896. */
  11897. class ECB$1 {
  11898. constructor(count, dataCodewords) {
  11899. this.count = count;
  11900. this.dataCodewords = dataCodewords;
  11901. }
  11902. getCount() {
  11903. return this.count;
  11904. }
  11905. getDataCodewords() {
  11906. return this.dataCodewords;
  11907. }
  11908. }
  11909. /**
  11910. * The Version object encapsulates attributes about a particular
  11911. * size Data Matrix Code.
  11912. *
  11913. * @author bbrown@google.com (Brian Brown)
  11914. */
  11915. class Version$1 {
  11916. constructor(versionNumber, symbolSizeRows, symbolSizeColumns, dataRegionSizeRows, dataRegionSizeColumns, ecBlocks) {
  11917. this.versionNumber = versionNumber;
  11918. this.symbolSizeRows = symbolSizeRows;
  11919. this.symbolSizeColumns = symbolSizeColumns;
  11920. this.dataRegionSizeRows = dataRegionSizeRows;
  11921. this.dataRegionSizeColumns = dataRegionSizeColumns;
  11922. this.ecBlocks = ecBlocks;
  11923. // Calculate the total number of codewords
  11924. let total = 0;
  11925. const ecCodewords = ecBlocks.getECCodewords();
  11926. const ecbArray = ecBlocks.getECBlocks();
  11927. for (let ecBlock of ecbArray) {
  11928. total += ecBlock.getCount() * (ecBlock.getDataCodewords() + ecCodewords);
  11929. }
  11930. this.totalCodewords = total;
  11931. }
  11932. getVersionNumber() {
  11933. return this.versionNumber;
  11934. }
  11935. getSymbolSizeRows() {
  11936. return this.symbolSizeRows;
  11937. }
  11938. getSymbolSizeColumns() {
  11939. return this.symbolSizeColumns;
  11940. }
  11941. getDataRegionSizeRows() {
  11942. return this.dataRegionSizeRows;
  11943. }
  11944. getDataRegionSizeColumns() {
  11945. return this.dataRegionSizeColumns;
  11946. }
  11947. getTotalCodewords() {
  11948. return this.totalCodewords;
  11949. }
  11950. getECBlocks() {
  11951. return this.ecBlocks;
  11952. }
  11953. /**
  11954. * <p>Deduces version information from Data Matrix dimensions.</p>
  11955. *
  11956. * @param numRows Number of rows in modules
  11957. * @param numColumns Number of columns in modules
  11958. * @return Version for a Data Matrix Code of those dimensions
  11959. * @throws FormatException if dimensions do correspond to a valid Data Matrix size
  11960. */
  11961. static getVersionForDimensions(numRows, numColumns) {
  11962. if ((numRows & 0x01) !== 0 || (numColumns & 0x01) !== 0) {
  11963. throw new FormatException();
  11964. }
  11965. for (let version of Version$1.VERSIONS) {
  11966. if (version.symbolSizeRows === numRows && version.symbolSizeColumns === numColumns) {
  11967. return version;
  11968. }
  11969. }
  11970. throw new FormatException();
  11971. }
  11972. // @Override
  11973. toString() {
  11974. return '' + this.versionNumber;
  11975. }
  11976. /**
  11977. * See ISO 16022:2006 5.5.1 Table 7
  11978. */
  11979. static buildVersions() {
  11980. return [
  11981. new Version$1(1, 10, 10, 8, 8, new ECBlocks$1(5, new ECB$1(1, 3))),
  11982. new Version$1(2, 12, 12, 10, 10, new ECBlocks$1(7, new ECB$1(1, 5))),
  11983. new Version$1(3, 14, 14, 12, 12, new ECBlocks$1(10, new ECB$1(1, 8))),
  11984. new Version$1(4, 16, 16, 14, 14, new ECBlocks$1(12, new ECB$1(1, 12))),
  11985. new Version$1(5, 18, 18, 16, 16, new ECBlocks$1(14, new ECB$1(1, 18))),
  11986. new Version$1(6, 20, 20, 18, 18, new ECBlocks$1(18, new ECB$1(1, 22))),
  11987. new Version$1(7, 22, 22, 20, 20, new ECBlocks$1(20, new ECB$1(1, 30))),
  11988. new Version$1(8, 24, 24, 22, 22, new ECBlocks$1(24, new ECB$1(1, 36))),
  11989. new Version$1(9, 26, 26, 24, 24, new ECBlocks$1(28, new ECB$1(1, 44))),
  11990. new Version$1(10, 32, 32, 14, 14, new ECBlocks$1(36, new ECB$1(1, 62))),
  11991. new Version$1(11, 36, 36, 16, 16, new ECBlocks$1(42, new ECB$1(1, 86))),
  11992. new Version$1(12, 40, 40, 18, 18, new ECBlocks$1(48, new ECB$1(1, 114))),
  11993. new Version$1(13, 44, 44, 20, 20, new ECBlocks$1(56, new ECB$1(1, 144))),
  11994. new Version$1(14, 48, 48, 22, 22, new ECBlocks$1(68, new ECB$1(1, 174))),
  11995. new Version$1(15, 52, 52, 24, 24, new ECBlocks$1(42, new ECB$1(2, 102))),
  11996. new Version$1(16, 64, 64, 14, 14, new ECBlocks$1(56, new ECB$1(2, 140))),
  11997. new Version$1(17, 72, 72, 16, 16, new ECBlocks$1(36, new ECB$1(4, 92))),
  11998. new Version$1(18, 80, 80, 18, 18, new ECBlocks$1(48, new ECB$1(4, 114))),
  11999. new Version$1(19, 88, 88, 20, 20, new ECBlocks$1(56, new ECB$1(4, 144))),
  12000. new Version$1(20, 96, 96, 22, 22, new ECBlocks$1(68, new ECB$1(4, 174))),
  12001. new Version$1(21, 104, 104, 24, 24, new ECBlocks$1(56, new ECB$1(6, 136))),
  12002. new Version$1(22, 120, 120, 18, 18, new ECBlocks$1(68, new ECB$1(6, 175))),
  12003. new Version$1(23, 132, 132, 20, 20, new ECBlocks$1(62, new ECB$1(8, 163))),
  12004. new Version$1(24, 144, 144, 22, 22, new ECBlocks$1(62, new ECB$1(8, 156), new ECB$1(2, 155))),
  12005. new Version$1(25, 8, 18, 6, 16, new ECBlocks$1(7, new ECB$1(1, 5))),
  12006. new Version$1(26, 8, 32, 6, 14, new ECBlocks$1(11, new ECB$1(1, 10))),
  12007. new Version$1(27, 12, 26, 10, 24, new ECBlocks$1(14, new ECB$1(1, 16))),
  12008. new Version$1(28, 12, 36, 10, 16, new ECBlocks$1(18, new ECB$1(1, 22))),
  12009. new Version$1(29, 16, 36, 14, 16, new ECBlocks$1(24, new ECB$1(1, 32))),
  12010. new Version$1(30, 16, 48, 14, 22, new ECBlocks$1(28, new ECB$1(1, 49)))
  12011. ];
  12012. }
  12013. }
  12014. Version$1.VERSIONS = Version$1.buildVersions();
  12015. /*
  12016. * Copyright 2007 ZXing authors
  12017. *
  12018. * Licensed under the Apache License, Version 2.0 (the "License");
  12019. * you may not use this file except in compliance with the License.
  12020. * You may obtain a copy of the License at
  12021. *
  12022. * http://www.apache.org/licenses/LICENSE-2.0
  12023. *
  12024. * Unless required by applicable law or agreed to in writing, software
  12025. * distributed under the License is distributed on an "AS IS" BASIS,
  12026. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12027. * See the License for the specific language governing permissions and
  12028. * limitations under the License.
  12029. */
  12030. /**
  12031. * @author bbrown@google.com (Brian Brown)
  12032. */
  12033. class BitMatrixParser$1 {
  12034. /**
  12035. * @param bitMatrix {@link BitMatrix} to parse
  12036. * @throws FormatException if dimension is < 8 or > 144 or not 0 mod 2
  12037. */
  12038. constructor(bitMatrix) {
  12039. const dimension = bitMatrix.getHeight();
  12040. if (dimension < 8 || dimension > 144 || (dimension & 0x01) !== 0) {
  12041. throw new FormatException();
  12042. }
  12043. this.version = BitMatrixParser$1.readVersion(bitMatrix);
  12044. this.mappingBitMatrix = this.extractDataRegion(bitMatrix);
  12045. this.readMappingMatrix = new BitMatrix(this.mappingBitMatrix.getWidth(), this.mappingBitMatrix.getHeight());
  12046. }
  12047. getVersion() {
  12048. return this.version;
  12049. }
  12050. /**
  12051. * <p>Creates the version object based on the dimension of the original bit matrix from
  12052. * the datamatrix code.</p>
  12053. *
  12054. * <p>See ISO 16022:2006 Table 7 - ECC 200 symbol attributes</p>
  12055. *
  12056. * @param bitMatrix Original {@link BitMatrix} including alignment patterns
  12057. * @return {@link Version} encapsulating the Data Matrix Code's "version"
  12058. * @throws FormatException if the dimensions of the mapping matrix are not valid
  12059. * Data Matrix dimensions.
  12060. */
  12061. static readVersion(bitMatrix) {
  12062. const numRows = bitMatrix.getHeight();
  12063. const numColumns = bitMatrix.getWidth();
  12064. return Version$1.getVersionForDimensions(numRows, numColumns);
  12065. }
  12066. /**
  12067. * <p>Reads the bits in the {@link BitMatrix} representing the mapping matrix (No alignment patterns)
  12068. * in the correct order in order to reconstitute the codewords bytes contained within the
  12069. * Data Matrix Code.</p>
  12070. *
  12071. * @return bytes encoded within the Data Matrix Code
  12072. * @throws FormatException if the exact number of bytes expected is not read
  12073. */
  12074. readCodewords() {
  12075. const result = new Int8Array(this.version.getTotalCodewords());
  12076. let resultOffset = 0;
  12077. let row = 4;
  12078. let column = 0;
  12079. const numRows = this.mappingBitMatrix.getHeight();
  12080. const numColumns = this.mappingBitMatrix.getWidth();
  12081. let corner1Read = false;
  12082. let corner2Read = false;
  12083. let corner3Read = false;
  12084. let corner4Read = false;
  12085. // Read all of the codewords
  12086. do {
  12087. // Check the four corner cases
  12088. if ((row === numRows) && (column === 0) && !corner1Read) {
  12089. result[resultOffset++] = this.readCorner1(numRows, numColumns) & 0xff;
  12090. row -= 2;
  12091. column += 2;
  12092. corner1Read = true;
  12093. }
  12094. else if ((row === numRows - 2) && (column === 0) && ((numColumns & 0x03) !== 0) && !corner2Read) {
  12095. result[resultOffset++] = this.readCorner2(numRows, numColumns) & 0xff;
  12096. row -= 2;
  12097. column += 2;
  12098. corner2Read = true;
  12099. }
  12100. else if ((row === numRows + 4) && (column === 2) && ((numColumns & 0x07) === 0) && !corner3Read) {
  12101. result[resultOffset++] = this.readCorner3(numRows, numColumns) & 0xff;
  12102. row -= 2;
  12103. column += 2;
  12104. corner3Read = true;
  12105. }
  12106. else if ((row === numRows - 2) && (column === 0) && ((numColumns & 0x07) === 4) && !corner4Read) {
  12107. result[resultOffset++] = this.readCorner4(numRows, numColumns) & 0xff;
  12108. row -= 2;
  12109. column += 2;
  12110. corner4Read = true;
  12111. }
  12112. else {
  12113. // Sweep upward diagonally to the right
  12114. do {
  12115. if ((row < numRows) && (column >= 0) && !this.readMappingMatrix.get(column, row)) {
  12116. result[resultOffset++] = this.readUtah(row, column, numRows, numColumns) & 0xff;
  12117. }
  12118. row -= 2;
  12119. column += 2;
  12120. } while ((row >= 0) && (column < numColumns));
  12121. row += 1;
  12122. column += 3;
  12123. // Sweep downward diagonally to the left
  12124. do {
  12125. if ((row >= 0) && (column < numColumns) && !this.readMappingMatrix.get(column, row)) {
  12126. result[resultOffset++] = this.readUtah(row, column, numRows, numColumns) & 0xff;
  12127. }
  12128. row += 2;
  12129. column -= 2;
  12130. } while ((row < numRows) && (column >= 0));
  12131. row += 3;
  12132. column += 1;
  12133. }
  12134. } while ((row < numRows) || (column < numColumns));
  12135. if (resultOffset !== this.version.getTotalCodewords()) {
  12136. throw new FormatException();
  12137. }
  12138. return result;
  12139. }
  12140. /**
  12141. * <p>Reads a bit of the mapping matrix accounting for boundary wrapping.</p>
  12142. *
  12143. * @param row Row to read in the mapping matrix
  12144. * @param column Column to read in the mapping matrix
  12145. * @param numRows Number of rows in the mapping matrix
  12146. * @param numColumns Number of columns in the mapping matrix
  12147. * @return value of the given bit in the mapping matrix
  12148. */
  12149. readModule(row, column, numRows, numColumns) {
  12150. // Adjust the row and column indices based on boundary wrapping
  12151. if (row < 0) {
  12152. row += numRows;
  12153. column += 4 - ((numRows + 4) & 0x07);
  12154. }
  12155. if (column < 0) {
  12156. column += numColumns;
  12157. row += 4 - ((numColumns + 4) & 0x07);
  12158. }
  12159. this.readMappingMatrix.set(column, row);
  12160. return this.mappingBitMatrix.get(column, row);
  12161. }
  12162. /**
  12163. * <p>Reads the 8 bits of the standard Utah-shaped pattern.</p>
  12164. *
  12165. * <p>See ISO 16022:2006, 5.8.1 Figure 6</p>
  12166. *
  12167. * @param row Current row in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
  12168. * @param column Current column in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
  12169. * @param numRows Number of rows in the mapping matrix
  12170. * @param numColumns Number of columns in the mapping matrix
  12171. * @return byte from the utah shape
  12172. */
  12173. readUtah(row, column, numRows, numColumns) {
  12174. let currentByte = 0;
  12175. if (this.readModule(row - 2, column - 2, numRows, numColumns)) {
  12176. currentByte |= 1;
  12177. }
  12178. currentByte <<= 1;
  12179. if (this.readModule(row - 2, column - 1, numRows, numColumns)) {
  12180. currentByte |= 1;
  12181. }
  12182. currentByte <<= 1;
  12183. if (this.readModule(row - 1, column - 2, numRows, numColumns)) {
  12184. currentByte |= 1;
  12185. }
  12186. currentByte <<= 1;
  12187. if (this.readModule(row - 1, column - 1, numRows, numColumns)) {
  12188. currentByte |= 1;
  12189. }
  12190. currentByte <<= 1;
  12191. if (this.readModule(row - 1, column, numRows, numColumns)) {
  12192. currentByte |= 1;
  12193. }
  12194. currentByte <<= 1;
  12195. if (this.readModule(row, column - 2, numRows, numColumns)) {
  12196. currentByte |= 1;
  12197. }
  12198. currentByte <<= 1;
  12199. if (this.readModule(row, column - 1, numRows, numColumns)) {
  12200. currentByte |= 1;
  12201. }
  12202. currentByte <<= 1;
  12203. if (this.readModule(row, column, numRows, numColumns)) {
  12204. currentByte |= 1;
  12205. }
  12206. return currentByte;
  12207. }
  12208. /**
  12209. * <p>Reads the 8 bits of the special corner condition 1.</p>
  12210. *
  12211. * <p>See ISO 16022:2006, Figure F.3</p>
  12212. *
  12213. * @param numRows Number of rows in the mapping matrix
  12214. * @param numColumns Number of columns in the mapping matrix
  12215. * @return byte from the Corner condition 1
  12216. */
  12217. readCorner1(numRows, numColumns) {
  12218. let currentByte = 0;
  12219. if (this.readModule(numRows - 1, 0, numRows, numColumns)) {
  12220. currentByte |= 1;
  12221. }
  12222. currentByte <<= 1;
  12223. if (this.readModule(numRows - 1, 1, numRows, numColumns)) {
  12224. currentByte |= 1;
  12225. }
  12226. currentByte <<= 1;
  12227. if (this.readModule(numRows - 1, 2, numRows, numColumns)) {
  12228. currentByte |= 1;
  12229. }
  12230. currentByte <<= 1;
  12231. if (this.readModule(0, numColumns - 2, numRows, numColumns)) {
  12232. currentByte |= 1;
  12233. }
  12234. currentByte <<= 1;
  12235. if (this.readModule(0, numColumns - 1, numRows, numColumns)) {
  12236. currentByte |= 1;
  12237. }
  12238. currentByte <<= 1;
  12239. if (this.readModule(1, numColumns - 1, numRows, numColumns)) {
  12240. currentByte |= 1;
  12241. }
  12242. currentByte <<= 1;
  12243. if (this.readModule(2, numColumns - 1, numRows, numColumns)) {
  12244. currentByte |= 1;
  12245. }
  12246. currentByte <<= 1;
  12247. if (this.readModule(3, numColumns - 1, numRows, numColumns)) {
  12248. currentByte |= 1;
  12249. }
  12250. return currentByte;
  12251. }
  12252. /**
  12253. * <p>Reads the 8 bits of the special corner condition 2.</p>
  12254. *
  12255. * <p>See ISO 16022:2006, Figure F.4</p>
  12256. *
  12257. * @param numRows Number of rows in the mapping matrix
  12258. * @param numColumns Number of columns in the mapping matrix
  12259. * @return byte from the Corner condition 2
  12260. */
  12261. readCorner2(numRows, numColumns) {
  12262. let currentByte = 0;
  12263. if (this.readModule(numRows - 3, 0, numRows, numColumns)) {
  12264. currentByte |= 1;
  12265. }
  12266. currentByte <<= 1;
  12267. if (this.readModule(numRows - 2, 0, numRows, numColumns)) {
  12268. currentByte |= 1;
  12269. }
  12270. currentByte <<= 1;
  12271. if (this.readModule(numRows - 1, 0, numRows, numColumns)) {
  12272. currentByte |= 1;
  12273. }
  12274. currentByte <<= 1;
  12275. if (this.readModule(0, numColumns - 4, numRows, numColumns)) {
  12276. currentByte |= 1;
  12277. }
  12278. currentByte <<= 1;
  12279. if (this.readModule(0, numColumns - 3, numRows, numColumns)) {
  12280. currentByte |= 1;
  12281. }
  12282. currentByte <<= 1;
  12283. if (this.readModule(0, numColumns - 2, numRows, numColumns)) {
  12284. currentByte |= 1;
  12285. }
  12286. currentByte <<= 1;
  12287. if (this.readModule(0, numColumns - 1, numRows, numColumns)) {
  12288. currentByte |= 1;
  12289. }
  12290. currentByte <<= 1;
  12291. if (this.readModule(1, numColumns - 1, numRows, numColumns)) {
  12292. currentByte |= 1;
  12293. }
  12294. return currentByte;
  12295. }
  12296. /**
  12297. * <p>Reads the 8 bits of the special corner condition 3.</p>
  12298. *
  12299. * <p>See ISO 16022:2006, Figure F.5</p>
  12300. *
  12301. * @param numRows Number of rows in the mapping matrix
  12302. * @param numColumns Number of columns in the mapping matrix
  12303. * @return byte from the Corner condition 3
  12304. */
  12305. readCorner3(numRows, numColumns) {
  12306. let currentByte = 0;
  12307. if (this.readModule(numRows - 1, 0, numRows, numColumns)) {
  12308. currentByte |= 1;
  12309. }
  12310. currentByte <<= 1;
  12311. if (this.readModule(numRows - 1, numColumns - 1, numRows, numColumns)) {
  12312. currentByte |= 1;
  12313. }
  12314. currentByte <<= 1;
  12315. if (this.readModule(0, numColumns - 3, numRows, numColumns)) {
  12316. currentByte |= 1;
  12317. }
  12318. currentByte <<= 1;
  12319. if (this.readModule(0, numColumns - 2, numRows, numColumns)) {
  12320. currentByte |= 1;
  12321. }
  12322. currentByte <<= 1;
  12323. if (this.readModule(0, numColumns - 1, numRows, numColumns)) {
  12324. currentByte |= 1;
  12325. }
  12326. currentByte <<= 1;
  12327. if (this.readModule(1, numColumns - 3, numRows, numColumns)) {
  12328. currentByte |= 1;
  12329. }
  12330. currentByte <<= 1;
  12331. if (this.readModule(1, numColumns - 2, numRows, numColumns)) {
  12332. currentByte |= 1;
  12333. }
  12334. currentByte <<= 1;
  12335. if (this.readModule(1, numColumns - 1, numRows, numColumns)) {
  12336. currentByte |= 1;
  12337. }
  12338. return currentByte;
  12339. }
  12340. /**
  12341. * <p>Reads the 8 bits of the special corner condition 4.</p>
  12342. *
  12343. * <p>See ISO 16022:2006, Figure F.6</p>
  12344. *
  12345. * @param numRows Number of rows in the mapping matrix
  12346. * @param numColumns Number of columns in the mapping matrix
  12347. * @return byte from the Corner condition 4
  12348. */
  12349. readCorner4(numRows, numColumns) {
  12350. let currentByte = 0;
  12351. if (this.readModule(numRows - 3, 0, numRows, numColumns)) {
  12352. currentByte |= 1;
  12353. }
  12354. currentByte <<= 1;
  12355. if (this.readModule(numRows - 2, 0, numRows, numColumns)) {
  12356. currentByte |= 1;
  12357. }
  12358. currentByte <<= 1;
  12359. if (this.readModule(numRows - 1, 0, numRows, numColumns)) {
  12360. currentByte |= 1;
  12361. }
  12362. currentByte <<= 1;
  12363. if (this.readModule(0, numColumns - 2, numRows, numColumns)) {
  12364. currentByte |= 1;
  12365. }
  12366. currentByte <<= 1;
  12367. if (this.readModule(0, numColumns - 1, numRows, numColumns)) {
  12368. currentByte |= 1;
  12369. }
  12370. currentByte <<= 1;
  12371. if (this.readModule(1, numColumns - 1, numRows, numColumns)) {
  12372. currentByte |= 1;
  12373. }
  12374. currentByte <<= 1;
  12375. if (this.readModule(2, numColumns - 1, numRows, numColumns)) {
  12376. currentByte |= 1;
  12377. }
  12378. currentByte <<= 1;
  12379. if (this.readModule(3, numColumns - 1, numRows, numColumns)) {
  12380. currentByte |= 1;
  12381. }
  12382. return currentByte;
  12383. }
  12384. /**
  12385. * <p>Extracts the data region from a {@link BitMatrix} that contains
  12386. * alignment patterns.</p>
  12387. *
  12388. * @param bitMatrix Original {@link BitMatrix} with alignment patterns
  12389. * @return BitMatrix that has the alignment patterns removed
  12390. */
  12391. extractDataRegion(bitMatrix) {
  12392. const symbolSizeRows = this.version.getSymbolSizeRows();
  12393. const symbolSizeColumns = this.version.getSymbolSizeColumns();
  12394. if (bitMatrix.getHeight() !== symbolSizeRows) {
  12395. throw new IllegalArgumentException('Dimension of bitMatrix must match the version size');
  12396. }
  12397. const dataRegionSizeRows = this.version.getDataRegionSizeRows();
  12398. const dataRegionSizeColumns = this.version.getDataRegionSizeColumns();
  12399. const numDataRegionsRow = symbolSizeRows / dataRegionSizeRows | 0;
  12400. const numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns | 0;
  12401. const sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows;
  12402. const sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns;
  12403. const bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionColumn, sizeDataRegionRow);
  12404. for (let dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) {
  12405. const dataRegionRowOffset = dataRegionRow * dataRegionSizeRows;
  12406. for (let dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++dataRegionColumn) {
  12407. const dataRegionColumnOffset = dataRegionColumn * dataRegionSizeColumns;
  12408. for (let i = 0; i < dataRegionSizeRows; ++i) {
  12409. const readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i;
  12410. const writeRowOffset = dataRegionRowOffset + i;
  12411. for (let j = 0; j < dataRegionSizeColumns; ++j) {
  12412. const readColumnOffset = dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j;
  12413. if (bitMatrix.get(readColumnOffset, readRowOffset)) {
  12414. const writeColumnOffset = dataRegionColumnOffset + j;
  12415. bitMatrixWithoutAlignment.set(writeColumnOffset, writeRowOffset);
  12416. }
  12417. }
  12418. }
  12419. }
  12420. }
  12421. return bitMatrixWithoutAlignment;
  12422. }
  12423. }
  12424. /*
  12425. * Copyright 2008 ZXing authors
  12426. *
  12427. * Licensed under the Apache License, Version 2.0 (the "License");
  12428. * you may not use this file except in compliance with the License.
  12429. * You may obtain a copy of the License at
  12430. *
  12431. * http://www.apache.org/licenses/LICENSE-2.0
  12432. *
  12433. * Unless required by applicable law or agreed to in writing, software
  12434. * distributed under the License is distributed on an "AS IS" BASIS,
  12435. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12436. * See the License for the specific language governing permissions and
  12437. * limitations under the License.
  12438. */
  12439. /**
  12440. * <p>Encapsulates a block of data within a Data Matrix Code. Data Matrix Codes may split their data into
  12441. * multiple blocks, each of which is a unit of data and error-correction codewords. Each
  12442. * is represented by an instance of this class.</p>
  12443. *
  12444. * @author bbrown@google.com (Brian Brown)
  12445. */
  12446. class DataBlock$1 {
  12447. constructor(numDataCodewords, codewords) {
  12448. this.numDataCodewords = numDataCodewords;
  12449. this.codewords = codewords;
  12450. }
  12451. /**
  12452. * <p>When Data Matrix Codes use multiple data blocks, they actually interleave the bytes of each of them.
  12453. * That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This
  12454. * method will separate the data into original blocks.</p>
  12455. *
  12456. * @param rawCodewords bytes as read directly from the Data Matrix Code
  12457. * @param version version of the Data Matrix Code
  12458. * @return DataBlocks containing original bytes, "de-interleaved" from representation in the
  12459. * Data Matrix Code
  12460. */
  12461. static getDataBlocks(rawCodewords, version) {
  12462. // Figure out the number and size of data blocks used by this version
  12463. const ecBlocks = version.getECBlocks();
  12464. // First count the total number of data blocks
  12465. let totalBlocks = 0;
  12466. const ecBlockArray = ecBlocks.getECBlocks();
  12467. for (let ecBlock of ecBlockArray) {
  12468. totalBlocks += ecBlock.getCount();
  12469. }
  12470. // Now establish DataBlocks of the appropriate size and number of data codewords
  12471. const result = new Array(totalBlocks);
  12472. let numResultBlocks = 0;
  12473. for (let ecBlock of ecBlockArray) {
  12474. for (let i = 0; i < ecBlock.getCount(); i++) {
  12475. const numDataCodewords = ecBlock.getDataCodewords();
  12476. const numBlockCodewords = ecBlocks.getECCodewords() + numDataCodewords;
  12477. result[numResultBlocks++] = new DataBlock$1(numDataCodewords, new Uint8Array(numBlockCodewords));
  12478. }
  12479. }
  12480. // All blocks have the same amount of data, except that the last n
  12481. // (where n may be 0) have 1 less byte. Figure out where these start.
  12482. // TODO(bbrown): There is only one case where there is a difference for Data Matrix for size 144
  12483. const longerBlocksTotalCodewords = result[0].codewords.length;
  12484. // int shorterBlocksTotalCodewords = longerBlocksTotalCodewords - 1;
  12485. const longerBlocksNumDataCodewords = longerBlocksTotalCodewords - ecBlocks.getECCodewords();
  12486. const shorterBlocksNumDataCodewords = longerBlocksNumDataCodewords - 1;
  12487. // The last elements of result may be 1 element shorter for 144 matrix
  12488. // first fill out as many elements as all of them have minus 1
  12489. let rawCodewordsOffset = 0;
  12490. for (let i = 0; i < shorterBlocksNumDataCodewords; i++) {
  12491. for (let j = 0; j < numResultBlocks; j++) {
  12492. result[j].codewords[i] = rawCodewords[rawCodewordsOffset++];
  12493. }
  12494. }
  12495. // Fill out the last data block in the longer ones
  12496. const specialVersion = version.getVersionNumber() === 24;
  12497. const numLongerBlocks = specialVersion ? 8 : numResultBlocks;
  12498. for (let j = 0; j < numLongerBlocks; j++) {
  12499. result[j].codewords[longerBlocksNumDataCodewords - 1] = rawCodewords[rawCodewordsOffset++];
  12500. }
  12501. // Now add in error correction blocks
  12502. const max = result[0].codewords.length;
  12503. for (let i = longerBlocksNumDataCodewords; i < max; i++) {
  12504. for (let j = 0; j < numResultBlocks; j++) {
  12505. const jOffset = specialVersion ? (j + 8) % numResultBlocks : j;
  12506. const iOffset = specialVersion && jOffset > 7 ? i - 1 : i;
  12507. result[jOffset].codewords[iOffset] = rawCodewords[rawCodewordsOffset++];
  12508. }
  12509. }
  12510. if (rawCodewordsOffset !== rawCodewords.length) {
  12511. throw new IllegalArgumentException();
  12512. }
  12513. return result;
  12514. }
  12515. getNumDataCodewords() {
  12516. return this.numDataCodewords;
  12517. }
  12518. getCodewords() {
  12519. return this.codewords;
  12520. }
  12521. }
  12522. /*
  12523. * Copyright 2007 ZXing authors
  12524. *
  12525. * Licensed under the Apache License, Version 2.0 (the "License");
  12526. * you may not use this file except in compliance with the License.
  12527. * You may obtain a copy of the License at
  12528. *
  12529. * http://www.apache.org/licenses/LICENSE-2.0
  12530. *
  12531. * Unless required by applicable law or agreed to in writing, software
  12532. * distributed under the License is distributed on an "AS IS" BASIS,
  12533. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12534. * See the License for the specific language governing permissions and
  12535. * limitations under the License.
  12536. */
  12537. /**
  12538. * <p>This provides an easy abstraction to read bits at a time from a sequence of bytes, where the
  12539. * number of bits read is not often a multiple of 8.</p>
  12540. *
  12541. * <p>This class is thread-safe but not reentrant -- unless the caller modifies the bytes array
  12542. * it passed in, in which case all bets are off.</p>
  12543. *
  12544. * @author Sean Owen
  12545. */
  12546. class BitSource {
  12547. /**
  12548. * @param bytes bytes from which this will read bits. Bits will be read from the first byte first.
  12549. * Bits are read within a byte from most-significant to least-significant bit.
  12550. */
  12551. constructor(bytes) {
  12552. this.bytes = bytes;
  12553. this.byteOffset = 0;
  12554. this.bitOffset = 0;
  12555. }
  12556. /**
  12557. * @return index of next bit in current byte which would be read by the next call to {@link #readBits(int)}.
  12558. */
  12559. getBitOffset() {
  12560. return this.bitOffset;
  12561. }
  12562. /**
  12563. * @return index of next byte in input byte array which would be read by the next call to {@link #readBits(int)}.
  12564. */
  12565. getByteOffset() {
  12566. return this.byteOffset;
  12567. }
  12568. /**
  12569. * @param numBits number of bits to read
  12570. * @return int representing the bits read. The bits will appear as the least-significant
  12571. * bits of the int
  12572. * @throws IllegalArgumentException if numBits isn't in [1,32] or more than is available
  12573. */
  12574. readBits(numBits /*int*/) {
  12575. if (numBits < 1 || numBits > 32 || numBits > this.available()) {
  12576. throw new IllegalArgumentException('' + numBits);
  12577. }
  12578. let result = 0;
  12579. let bitOffset = this.bitOffset;
  12580. let byteOffset = this.byteOffset;
  12581. const bytes = this.bytes;
  12582. // First, read remainder from current byte
  12583. if (bitOffset > 0) {
  12584. const bitsLeft = 8 - bitOffset;
  12585. const toRead = numBits < bitsLeft ? numBits : bitsLeft;
  12586. const bitsToNotRead = bitsLeft - toRead;
  12587. const mask = (0xFF >> (8 - toRead)) << bitsToNotRead;
  12588. result = (bytes[byteOffset] & mask) >> bitsToNotRead;
  12589. numBits -= toRead;
  12590. bitOffset += toRead;
  12591. if (bitOffset === 8) {
  12592. bitOffset = 0;
  12593. byteOffset++;
  12594. }
  12595. }
  12596. // Next read whole bytes
  12597. if (numBits > 0) {
  12598. while (numBits >= 8) {
  12599. result = (result << 8) | (bytes[byteOffset] & 0xFF);
  12600. byteOffset++;
  12601. numBits -= 8;
  12602. }
  12603. // Finally read a partial byte
  12604. if (numBits > 0) {
  12605. const bitsToNotRead = 8 - numBits;
  12606. const mask = (0xFF >> bitsToNotRead) << bitsToNotRead;
  12607. result = (result << numBits) | ((bytes[byteOffset] & mask) >> bitsToNotRead);
  12608. bitOffset += numBits;
  12609. }
  12610. }
  12611. this.bitOffset = bitOffset;
  12612. this.byteOffset = byteOffset;
  12613. return result;
  12614. }
  12615. /**
  12616. * @return number of bits that can be read successfully
  12617. */
  12618. available() {
  12619. return 8 * (this.bytes.length - this.byteOffset) - this.bitOffset;
  12620. }
  12621. }
  12622. /*
  12623. * Copyright 2008 ZXing authors
  12624. *
  12625. * Licensed under the Apache License, Version 2.0 (the "License");
  12626. * you may not use this file except in compliance with the License.
  12627. * You may obtain a copy of the License at
  12628. *
  12629. * http://www.apache.org/licenses/LICENSE-2.0
  12630. *
  12631. * Unless required by applicable law or agreed to in writing, software
  12632. * distributed under the License is distributed on an "AS IS" BASIS,
  12633. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12634. * See the License for the specific language governing permissions and
  12635. * limitations under the License.
  12636. */
  12637. var Mode$3;
  12638. (function (Mode) {
  12639. Mode[Mode["PAD_ENCODE"] = 0] = "PAD_ENCODE";
  12640. Mode[Mode["ASCII_ENCODE"] = 1] = "ASCII_ENCODE";
  12641. Mode[Mode["C40_ENCODE"] = 2] = "C40_ENCODE";
  12642. Mode[Mode["TEXT_ENCODE"] = 3] = "TEXT_ENCODE";
  12643. Mode[Mode["ANSIX12_ENCODE"] = 4] = "ANSIX12_ENCODE";
  12644. Mode[Mode["EDIFACT_ENCODE"] = 5] = "EDIFACT_ENCODE";
  12645. Mode[Mode["BASE256_ENCODE"] = 6] = "BASE256_ENCODE";
  12646. })(Mode$3 || (Mode$3 = {}));
  12647. /**
  12648. * <p>Data Matrix Codes can encode text as bits in one of several modes, and can use multiple modes
  12649. * in one Data Matrix Code. This class decodes the bits back into text.</p>
  12650. *
  12651. * <p>See ISO 16022:2006, 5.2.1 - 5.2.9.2</p>
  12652. *
  12653. * @author bbrown@google.com (Brian Brown)
  12654. * @author Sean Owen
  12655. */
  12656. class DecodedBitStreamParser$2 {
  12657. static decode(bytes) {
  12658. const bits = new BitSource(bytes);
  12659. const result = new StringBuilder();
  12660. const resultTrailer = new StringBuilder();
  12661. const byteSegments = new Array();
  12662. let mode = Mode$3.ASCII_ENCODE;
  12663. do {
  12664. if (mode === Mode$3.ASCII_ENCODE) {
  12665. mode = this.decodeAsciiSegment(bits, result, resultTrailer);
  12666. }
  12667. else {
  12668. switch (mode) {
  12669. case Mode$3.C40_ENCODE:
  12670. this.decodeC40Segment(bits, result);
  12671. break;
  12672. case Mode$3.TEXT_ENCODE:
  12673. this.decodeTextSegment(bits, result);
  12674. break;
  12675. case Mode$3.ANSIX12_ENCODE:
  12676. this.decodeAnsiX12Segment(bits, result);
  12677. break;
  12678. case Mode$3.EDIFACT_ENCODE:
  12679. this.decodeEdifactSegment(bits, result);
  12680. break;
  12681. case Mode$3.BASE256_ENCODE:
  12682. this.decodeBase256Segment(bits, result, byteSegments);
  12683. break;
  12684. default:
  12685. throw new FormatException();
  12686. }
  12687. mode = Mode$3.ASCII_ENCODE;
  12688. }
  12689. } while (mode !== Mode$3.PAD_ENCODE && bits.available() > 0);
  12690. if (resultTrailer.length() > 0) {
  12691. result.append(resultTrailer.toString());
  12692. }
  12693. return new DecoderResult(bytes, result.toString(), byteSegments.length === 0 ? null : byteSegments, null);
  12694. }
  12695. /**
  12696. * See ISO 16022:2006, 5.2.3 and Annex C, Table C.2
  12697. */
  12698. static decodeAsciiSegment(bits, result, resultTrailer) {
  12699. let upperShift = false;
  12700. do {
  12701. let oneByte = bits.readBits(8);
  12702. if (oneByte === 0) {
  12703. throw new FormatException();
  12704. }
  12705. else if (oneByte <= 128) { // ASCII data (ASCII value + 1)
  12706. if (upperShift) {
  12707. oneByte += 128;
  12708. // upperShift = false;
  12709. }
  12710. result.append(String.fromCharCode(oneByte - 1));
  12711. return Mode$3.ASCII_ENCODE;
  12712. }
  12713. else if (oneByte === 129) { // Pad
  12714. return Mode$3.PAD_ENCODE;
  12715. }
  12716. else if (oneByte <= 229) { // 2-digit data 00-99 (Numeric Value + 130)
  12717. const value = oneByte - 130;
  12718. if (value < 10) { // pad with '0' for single digit values
  12719. result.append('0');
  12720. }
  12721. result.append('' + value);
  12722. }
  12723. else {
  12724. switch (oneByte) {
  12725. case 230: // Latch to C40 encodation
  12726. return Mode$3.C40_ENCODE;
  12727. case 231: // Latch to Base 256 encodation
  12728. return Mode$3.BASE256_ENCODE;
  12729. case 232: // FNC1
  12730. result.append(String.fromCharCode(29)); // translate as ASCII 29
  12731. break;
  12732. case 233: // Structured Append
  12733. case 234: // Reader Programming
  12734. // Ignore these symbols for now
  12735. // throw ReaderException.getInstance();
  12736. break;
  12737. case 235: // Upper Shift (shift to Extended ASCII)
  12738. upperShift = true;
  12739. break;
  12740. case 236: // 05 Macro
  12741. result.append('[)>\u001E05\u001D');
  12742. resultTrailer.insert(0, '\u001E\u0004');
  12743. break;
  12744. case 237: // 06 Macro
  12745. result.append('[)>\u001E06\u001D');
  12746. resultTrailer.insert(0, '\u001E\u0004');
  12747. break;
  12748. case 238: // Latch to ANSI X12 encodation
  12749. return Mode$3.ANSIX12_ENCODE;
  12750. case 239: // Latch to Text encodation
  12751. return Mode$3.TEXT_ENCODE;
  12752. case 240: // Latch to EDIFACT encodation
  12753. return Mode$3.EDIFACT_ENCODE;
  12754. case 241: // ECI Character
  12755. // TODO(bbrown): I think we need to support ECI
  12756. // throw ReaderException.getInstance();
  12757. // Ignore this symbol for now
  12758. break;
  12759. default:
  12760. // Not to be used in ASCII encodation
  12761. // but work around encoders that end with 254, latch back to ASCII
  12762. if (oneByte !== 254 || bits.available() !== 0) {
  12763. throw new FormatException();
  12764. }
  12765. break;
  12766. }
  12767. }
  12768. } while (bits.available() > 0);
  12769. return Mode$3.ASCII_ENCODE;
  12770. }
  12771. /**
  12772. * See ISO 16022:2006, 5.2.5 and Annex C, Table C.1
  12773. */
  12774. static decodeC40Segment(bits, result) {
  12775. // Three C40 values are encoded in a 16-bit value as
  12776. // (1600 * C1) + (40 * C2) + C3 + 1
  12777. // TODO(bbrown): The Upper Shift with C40 doesn't work in the 4 value scenario all the time
  12778. let upperShift = false;
  12779. const cValues = [];
  12780. let shift = 0;
  12781. do {
  12782. // If there is only one byte left then it will be encoded as ASCII
  12783. if (bits.available() === 8) {
  12784. return;
  12785. }
  12786. const firstByte = bits.readBits(8);
  12787. if (firstByte === 254) { // Unlatch codeword
  12788. return;
  12789. }
  12790. this.parseTwoBytes(firstByte, bits.readBits(8), cValues);
  12791. for (let i = 0; i < 3; i++) {
  12792. const cValue = cValues[i];
  12793. switch (shift) {
  12794. case 0:
  12795. if (cValue < 3) {
  12796. shift = cValue + 1;
  12797. }
  12798. else if (cValue < this.C40_BASIC_SET_CHARS.length) {
  12799. const c40char = this.C40_BASIC_SET_CHARS[cValue];
  12800. if (upperShift) {
  12801. result.append(String.fromCharCode(c40char.charCodeAt(0) + 128));
  12802. upperShift = false;
  12803. }
  12804. else {
  12805. result.append(c40char);
  12806. }
  12807. }
  12808. else {
  12809. throw new FormatException();
  12810. }
  12811. break;
  12812. case 1:
  12813. if (upperShift) {
  12814. result.append(String.fromCharCode(cValue + 128));
  12815. upperShift = false;
  12816. }
  12817. else {
  12818. result.append(String.fromCharCode(cValue));
  12819. }
  12820. shift = 0;
  12821. break;
  12822. case 2:
  12823. if (cValue < this.C40_SHIFT2_SET_CHARS.length) {
  12824. const c40char = this.C40_SHIFT2_SET_CHARS[cValue];
  12825. if (upperShift) {
  12826. result.append(String.fromCharCode(c40char.charCodeAt(0) + 128));
  12827. upperShift = false;
  12828. }
  12829. else {
  12830. result.append(c40char);
  12831. }
  12832. }
  12833. else {
  12834. switch (cValue) {
  12835. case 27: // FNC1
  12836. result.append(String.fromCharCode(29)); // translate as ASCII 29
  12837. break;
  12838. case 30: // Upper Shift
  12839. upperShift = true;
  12840. break;
  12841. default:
  12842. throw new FormatException();
  12843. }
  12844. }
  12845. shift = 0;
  12846. break;
  12847. case 3:
  12848. if (upperShift) {
  12849. result.append(String.fromCharCode(cValue + 224));
  12850. upperShift = false;
  12851. }
  12852. else {
  12853. result.append(String.fromCharCode(cValue + 96));
  12854. }
  12855. shift = 0;
  12856. break;
  12857. default:
  12858. throw new FormatException();
  12859. }
  12860. }
  12861. } while (bits.available() > 0);
  12862. }
  12863. /**
  12864. * See ISO 16022:2006, 5.2.6 and Annex C, Table C.2
  12865. */
  12866. static decodeTextSegment(bits, result) {
  12867. // Three Text values are encoded in a 16-bit value as
  12868. // (1600 * C1) + (40 * C2) + C3 + 1
  12869. // TODO(bbrown): The Upper Shift with Text doesn't work in the 4 value scenario all the time
  12870. let upperShift = false;
  12871. let cValues = [];
  12872. let shift = 0;
  12873. do {
  12874. // If there is only one byte left then it will be encoded as ASCII
  12875. if (bits.available() === 8) {
  12876. return;
  12877. }
  12878. const firstByte = bits.readBits(8);
  12879. if (firstByte === 254) { // Unlatch codeword
  12880. return;
  12881. }
  12882. this.parseTwoBytes(firstByte, bits.readBits(8), cValues);
  12883. for (let i = 0; i < 3; i++) {
  12884. const cValue = cValues[i];
  12885. switch (shift) {
  12886. case 0:
  12887. if (cValue < 3) {
  12888. shift = cValue + 1;
  12889. }
  12890. else if (cValue < this.TEXT_BASIC_SET_CHARS.length) {
  12891. const textChar = this.TEXT_BASIC_SET_CHARS[cValue];
  12892. if (upperShift) {
  12893. result.append(String.fromCharCode(textChar.charCodeAt(0) + 128));
  12894. upperShift = false;
  12895. }
  12896. else {
  12897. result.append(textChar);
  12898. }
  12899. }
  12900. else {
  12901. throw new FormatException();
  12902. }
  12903. break;
  12904. case 1:
  12905. if (upperShift) {
  12906. result.append(String.fromCharCode(cValue + 128));
  12907. upperShift = false;
  12908. }
  12909. else {
  12910. result.append(String.fromCharCode(cValue));
  12911. }
  12912. shift = 0;
  12913. break;
  12914. case 2:
  12915. // Shift 2 for Text is the same encoding as C40
  12916. if (cValue < this.TEXT_SHIFT2_SET_CHARS.length) {
  12917. const textChar = this.TEXT_SHIFT2_SET_CHARS[cValue];
  12918. if (upperShift) {
  12919. result.append(String.fromCharCode(textChar.charCodeAt(0) + 128));
  12920. upperShift = false;
  12921. }
  12922. else {
  12923. result.append(textChar);
  12924. }
  12925. }
  12926. else {
  12927. switch (cValue) {
  12928. case 27: // FNC1
  12929. result.append(String.fromCharCode(29)); // translate as ASCII 29
  12930. break;
  12931. case 30: // Upper Shift
  12932. upperShift = true;
  12933. break;
  12934. default:
  12935. throw new FormatException();
  12936. }
  12937. }
  12938. shift = 0;
  12939. break;
  12940. case 3:
  12941. if (cValue < this.TEXT_SHIFT3_SET_CHARS.length) {
  12942. const textChar = this.TEXT_SHIFT3_SET_CHARS[cValue];
  12943. if (upperShift) {
  12944. result.append(String.fromCharCode(textChar.charCodeAt(0) + 128));
  12945. upperShift = false;
  12946. }
  12947. else {
  12948. result.append(textChar);
  12949. }
  12950. shift = 0;
  12951. }
  12952. else {
  12953. throw new FormatException();
  12954. }
  12955. break;
  12956. default:
  12957. throw new FormatException();
  12958. }
  12959. }
  12960. } while (bits.available() > 0);
  12961. }
  12962. /**
  12963. * See ISO 16022:2006, 5.2.7
  12964. */
  12965. static decodeAnsiX12Segment(bits, result) {
  12966. // Three ANSI X12 values are encoded in a 16-bit value as
  12967. // (1600 * C1) + (40 * C2) + C3 + 1
  12968. const cValues = [];
  12969. do {
  12970. // If there is only one byte left then it will be encoded as ASCII
  12971. if (bits.available() === 8) {
  12972. return;
  12973. }
  12974. const firstByte = bits.readBits(8);
  12975. if (firstByte === 254) { // Unlatch codeword
  12976. return;
  12977. }
  12978. this.parseTwoBytes(firstByte, bits.readBits(8), cValues);
  12979. for (let i = 0; i < 3; i++) {
  12980. const cValue = cValues[i];
  12981. switch (cValue) {
  12982. case 0: // X12 segment terminator <CR>
  12983. result.append('\r');
  12984. break;
  12985. case 1: // X12 segment separator *
  12986. result.append('*');
  12987. break;
  12988. case 2: // X12 sub-element separator >
  12989. result.append('>');
  12990. break;
  12991. case 3: // space
  12992. result.append(' ');
  12993. break;
  12994. default:
  12995. if (cValue < 14) { // 0 - 9
  12996. result.append(String.fromCharCode(cValue + 44));
  12997. }
  12998. else if (cValue < 40) { // A - Z
  12999. result.append(String.fromCharCode(cValue + 51));
  13000. }
  13001. else {
  13002. throw new FormatException();
  13003. }
  13004. break;
  13005. }
  13006. }
  13007. } while (bits.available() > 0);
  13008. }
  13009. static parseTwoBytes(firstByte, secondByte, result) {
  13010. let fullBitValue = (firstByte << 8) + secondByte - 1;
  13011. let temp = Math.floor(fullBitValue / 1600);
  13012. result[0] = temp;
  13013. fullBitValue -= temp * 1600;
  13014. temp = Math.floor(fullBitValue / 40);
  13015. result[1] = temp;
  13016. result[2] = fullBitValue - temp * 40;
  13017. }
  13018. /**
  13019. * See ISO 16022:2006, 5.2.8 and Annex C Table C.3
  13020. */
  13021. static decodeEdifactSegment(bits, result) {
  13022. do {
  13023. // If there is only two or less bytes left then it will be encoded as ASCII
  13024. if (bits.available() <= 16) {
  13025. return;
  13026. }
  13027. for (let i = 0; i < 4; i++) {
  13028. let edifactValue = bits.readBits(6);
  13029. // Check for the unlatch character
  13030. if (edifactValue === 0x1F) { // 011111
  13031. // Read rest of byte, which should be 0, and stop
  13032. const bitsLeft = 8 - bits.getBitOffset();
  13033. if (bitsLeft !== 8) {
  13034. bits.readBits(bitsLeft);
  13035. }
  13036. return;
  13037. }
  13038. if ((edifactValue & 0x20) === 0) { // no 1 in the leading (6th) bit
  13039. edifactValue |= 0x40; // Add a leading 01 to the 6 bit binary value
  13040. }
  13041. result.append(String.fromCharCode(edifactValue));
  13042. }
  13043. } while (bits.available() > 0);
  13044. }
  13045. /**
  13046. * See ISO 16022:2006, 5.2.9 and Annex B, B.2
  13047. */
  13048. static decodeBase256Segment(bits, result, byteSegments) {
  13049. // Figure out how long the Base 256 Segment is.
  13050. let codewordPosition = 1 + bits.getByteOffset(); // position is 1-indexed
  13051. const d1 = this.unrandomize255State(bits.readBits(8), codewordPosition++);
  13052. let count;
  13053. if (d1 === 0) { // Read the remainder of the symbol
  13054. count = bits.available() / 8 | 0;
  13055. }
  13056. else if (d1 < 250) {
  13057. count = d1;
  13058. }
  13059. else {
  13060. count = 250 * (d1 - 249) + this.unrandomize255State(bits.readBits(8), codewordPosition++);
  13061. }
  13062. // We're seeing NegativeArraySizeException errors from users.
  13063. if (count < 0) {
  13064. throw new FormatException();
  13065. }
  13066. const bytes = new Uint8Array(count);
  13067. for (let i = 0; i < count; i++) {
  13068. // Have seen this particular error in the wild, such as at
  13069. // http://www.bcgen.com/demo/IDAutomationStreamingDataMatrix.aspx?MODE=3&D=Fred&PFMT=3&PT=F&X=0.3&O=0&LM=0.2
  13070. if (bits.available() < 8) {
  13071. throw new FormatException();
  13072. }
  13073. bytes[i] = this.unrandomize255State(bits.readBits(8), codewordPosition++);
  13074. }
  13075. byteSegments.push(bytes);
  13076. try {
  13077. result.append(StringEncoding.decode(bytes, StringUtils.ISO88591));
  13078. }
  13079. catch (uee) {
  13080. throw new IllegalStateException('Platform does not support required encoding: ' + uee.message);
  13081. }
  13082. }
  13083. /**
  13084. * See ISO 16022:2006, Annex B, B.2
  13085. */
  13086. static unrandomize255State(randomizedBase256Codeword, base256CodewordPosition) {
  13087. const pseudoRandomNumber = ((149 * base256CodewordPosition) % 255) + 1;
  13088. const tempVariable = randomizedBase256Codeword - pseudoRandomNumber;
  13089. return tempVariable >= 0 ? tempVariable : tempVariable + 256;
  13090. }
  13091. }
  13092. /**
  13093. * See ISO 16022:2006, Annex C Table C.1
  13094. * The C40 Basic Character Set (*'s used for placeholders for the shift values)
  13095. */
  13096. DecodedBitStreamParser$2.C40_BASIC_SET_CHARS = [
  13097. '*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  13098. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  13099. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
  13100. ];
  13101. DecodedBitStreamParser$2.C40_SHIFT2_SET_CHARS = [
  13102. '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.',
  13103. '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_'
  13104. ];
  13105. /**
  13106. * See ISO 16022:2006, Annex C Table C.2
  13107. * The Text Basic Character Set (*'s used for placeholders for the shift values)
  13108. */
  13109. DecodedBitStreamParser$2.TEXT_BASIC_SET_CHARS = [
  13110. '*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  13111. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  13112. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
  13113. ];
  13114. // Shift 2 for Text is the same encoding as C40
  13115. DecodedBitStreamParser$2.TEXT_SHIFT2_SET_CHARS = DecodedBitStreamParser$2.C40_SHIFT2_SET_CHARS;
  13116. DecodedBitStreamParser$2.TEXT_SHIFT3_SET_CHARS = [
  13117. '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  13118. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', String.fromCharCode(127)
  13119. ];
  13120. /*
  13121. * Copyright 2007 ZXing authors
  13122. *
  13123. * Licensed under the Apache License, Version 2.0 (the "License");
  13124. * you may not use this file except in compliance with the License.
  13125. * You may obtain a copy of the License at
  13126. *
  13127. * http://www.apache.org/licenses/LICENSE-2.0
  13128. *
  13129. * Unless required by applicable law or agreed to in writing, software
  13130. * distributed under the License is distributed on an "AS IS" BASIS,
  13131. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13132. * See the License for the specific language governing permissions and
  13133. * limitations under the License.
  13134. */
  13135. /**
  13136. * <p>The main class which implements Data Matrix Code decoding -- as opposed to locating and extracting
  13137. * the Data Matrix Code from an image.</p>
  13138. *
  13139. * @author bbrown@google.com (Brian Brown)
  13140. */
  13141. class Decoder$1 {
  13142. constructor() {
  13143. this.rsDecoder = new ReedSolomonDecoder(GenericGF.DATA_MATRIX_FIELD_256);
  13144. }
  13145. /**
  13146. * <p>Decodes a Data Matrix Code represented as a {@link BitMatrix}. A 1 or "true" is taken
  13147. * to mean a black module.</p>
  13148. *
  13149. * @param bits booleans representing white/black Data Matrix Code modules
  13150. * @return text and bytes encoded within the Data Matrix Code
  13151. * @throws FormatException if the Data Matrix Code cannot be decoded
  13152. * @throws ChecksumException if error correction fails
  13153. */
  13154. decode(bits) {
  13155. // Construct a parser and read version, error-correction level
  13156. const parser = new BitMatrixParser$1(bits);
  13157. const version = parser.getVersion();
  13158. // Read codewords
  13159. const codewords = parser.readCodewords();
  13160. // Separate into data blocks
  13161. const dataBlocks = DataBlock$1.getDataBlocks(codewords, version);
  13162. // Count total number of data bytes
  13163. let totalBytes = 0;
  13164. for (let db of dataBlocks) {
  13165. totalBytes += db.getNumDataCodewords();
  13166. }
  13167. const resultBytes = new Uint8Array(totalBytes);
  13168. const dataBlocksCount = dataBlocks.length;
  13169. // Error-correct and copy data blocks together into a stream of bytes
  13170. for (let j = 0; j < dataBlocksCount; j++) {
  13171. const dataBlock = dataBlocks[j];
  13172. const codewordBytes = dataBlock.getCodewords();
  13173. const numDataCodewords = dataBlock.getNumDataCodewords();
  13174. this.correctErrors(codewordBytes, numDataCodewords);
  13175. for (let i = 0; i < numDataCodewords; i++) {
  13176. // De-interlace data blocks.
  13177. resultBytes[i * dataBlocksCount + j] = codewordBytes[i];
  13178. }
  13179. }
  13180. // Decode the contents of that stream of bytes
  13181. return DecodedBitStreamParser$2.decode(resultBytes);
  13182. }
  13183. /**
  13184. * <p>Given data and error-correction codewords received, possibly corrupted by errors, attempts to
  13185. * correct the errors in-place using Reed-Solomon error correction.</p>
  13186. *
  13187. * @param codewordBytes data and error correction codewords
  13188. * @param numDataCodewords number of codewords that are data bytes
  13189. * @throws ChecksumException if error correction fails
  13190. */
  13191. correctErrors(codewordBytes, numDataCodewords) {
  13192. // const numCodewords = codewordBytes.length;
  13193. // First read into an array of ints
  13194. const codewordsInts = new Int32Array(codewordBytes);
  13195. // for (let i = 0; i < numCodewords; i++) {
  13196. // codewordsInts[i] = codewordBytes[i] & 0xFF;
  13197. // }
  13198. try {
  13199. this.rsDecoder.decode(codewordsInts, codewordBytes.length - numDataCodewords);
  13200. }
  13201. catch (ignored /* ReedSolomonException */) {
  13202. throw new ChecksumException();
  13203. }
  13204. // Copy back into array of bytes -- only need to worry about the bytes that were data
  13205. // We don't care about errors in the error-correction codewords
  13206. for (let i = 0; i < numDataCodewords; i++) {
  13207. codewordBytes[i] = codewordsInts[i];
  13208. }
  13209. }
  13210. }
  13211. /*
  13212. * Copyright 2008 ZXing authors
  13213. *
  13214. * Licensed under the Apache License, Version 2.0 (the "License");
  13215. * you may not use this file except in compliance with the License.
  13216. * You may obtain a copy of the License at
  13217. *
  13218. * http://www.apache.org/licenses/LICENSE-2.0
  13219. *
  13220. * Unless required by applicable law or agreed to in writing, software
  13221. * distributed under the License is distributed on an "AS IS" BASIS,
  13222. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13223. * See the License for the specific language governing permissions and
  13224. * limitations under the License.
  13225. */
  13226. /**
  13227. * <p>Encapsulates logic that can detect a Data Matrix Code in an image, even if the Data Matrix Code
  13228. * is rotated or skewed, or partially obscured.</p>
  13229. *
  13230. * @author Sean Owen
  13231. */
  13232. class Detector$2 {
  13233. constructor(image) {
  13234. this.image = image;
  13235. this.rectangleDetector = new WhiteRectangleDetector(this.image);
  13236. }
  13237. /**
  13238. * <p>Detects a Data Matrix Code in an image.</p>
  13239. *
  13240. * @return {@link DetectorResult} encapsulating results of detecting a Data Matrix Code
  13241. * @throws NotFoundException if no Data Matrix Code can be found
  13242. */
  13243. detect() {
  13244. const cornerPoints = this.rectangleDetector.detect();
  13245. let points = this.detectSolid1(cornerPoints);
  13246. points = this.detectSolid2(points);
  13247. points[3] = this.correctTopRight(points);
  13248. if (!points[3]) {
  13249. throw new NotFoundException();
  13250. }
  13251. points = this.shiftToModuleCenter(points);
  13252. const topLeft = points[0];
  13253. const bottomLeft = points[1];
  13254. const bottomRight = points[2];
  13255. const topRight = points[3];
  13256. let dimensionTop = this.transitionsBetween(topLeft, topRight) + 1;
  13257. let dimensionRight = this.transitionsBetween(bottomRight, topRight) + 1;
  13258. if ((dimensionTop & 0x01) === 1) {
  13259. dimensionTop += 1;
  13260. }
  13261. if ((dimensionRight & 0x01) === 1) {
  13262. dimensionRight += 1;
  13263. }
  13264. if (4 * dimensionTop < 7 * dimensionRight && 4 * dimensionRight < 7 * dimensionTop) {
  13265. // The matrix is square
  13266. dimensionTop = dimensionRight = Math.max(dimensionTop, dimensionRight);
  13267. }
  13268. let bits = Detector$2.sampleGrid(this.image, topLeft, bottomLeft, bottomRight, topRight, dimensionTop, dimensionRight);
  13269. return new DetectorResult(bits, [topLeft, bottomLeft, bottomRight, topRight]);
  13270. }
  13271. static shiftPoint(point, to, div) {
  13272. let x = (to.getX() - point.getX()) / (div + 1);
  13273. let y = (to.getY() - point.getY()) / (div + 1);
  13274. return new ResultPoint(point.getX() + x, point.getY() + y);
  13275. }
  13276. static moveAway(point, fromX, fromY) {
  13277. let x = point.getX();
  13278. let y = point.getY();
  13279. if (x < fromX) {
  13280. x -= 1;
  13281. }
  13282. else {
  13283. x += 1;
  13284. }
  13285. if (y < fromY) {
  13286. y -= 1;
  13287. }
  13288. else {
  13289. y += 1;
  13290. }
  13291. return new ResultPoint(x, y);
  13292. }
  13293. /**
  13294. * Detect a solid side which has minimum transition.
  13295. */
  13296. detectSolid1(cornerPoints) {
  13297. // 0 2
  13298. // 1 3
  13299. let pointA = cornerPoints[0];
  13300. let pointB = cornerPoints[1];
  13301. let pointC = cornerPoints[3];
  13302. let pointD = cornerPoints[2];
  13303. let trAB = this.transitionsBetween(pointA, pointB);
  13304. let trBC = this.transitionsBetween(pointB, pointC);
  13305. let trCD = this.transitionsBetween(pointC, pointD);
  13306. let trDA = this.transitionsBetween(pointD, pointA);
  13307. // 0..3
  13308. // : :
  13309. // 1--2
  13310. let min = trAB;
  13311. let points = [pointD, pointA, pointB, pointC];
  13312. if (min > trBC) {
  13313. min = trBC;
  13314. points[0] = pointA;
  13315. points[1] = pointB;
  13316. points[2] = pointC;
  13317. points[3] = pointD;
  13318. }
  13319. if (min > trCD) {
  13320. min = trCD;
  13321. points[0] = pointB;
  13322. points[1] = pointC;
  13323. points[2] = pointD;
  13324. points[3] = pointA;
  13325. }
  13326. if (min > trDA) {
  13327. points[0] = pointC;
  13328. points[1] = pointD;
  13329. points[2] = pointA;
  13330. points[3] = pointB;
  13331. }
  13332. return points;
  13333. }
  13334. /**
  13335. * Detect a second solid side next to first solid side.
  13336. */
  13337. detectSolid2(points) {
  13338. // A..D
  13339. // : :
  13340. // B--C
  13341. let pointA = points[0];
  13342. let pointB = points[1];
  13343. let pointC = points[2];
  13344. let pointD = points[3];
  13345. // Transition detection on the edge is not stable.
  13346. // To safely detect, shift the points to the module center.
  13347. let tr = this.transitionsBetween(pointA, pointD);
  13348. let pointBs = Detector$2.shiftPoint(pointB, pointC, (tr + 1) * 4);
  13349. let pointCs = Detector$2.shiftPoint(pointC, pointB, (tr + 1) * 4);
  13350. let trBA = this.transitionsBetween(pointBs, pointA);
  13351. let trCD = this.transitionsBetween(pointCs, pointD);
  13352. // 0..3
  13353. // | :
  13354. // 1--2
  13355. if (trBA < trCD) {
  13356. // solid sides: A-B-C
  13357. points[0] = pointA;
  13358. points[1] = pointB;
  13359. points[2] = pointC;
  13360. points[3] = pointD;
  13361. }
  13362. else {
  13363. // solid sides: B-C-D
  13364. points[0] = pointB;
  13365. points[1] = pointC;
  13366. points[2] = pointD;
  13367. points[3] = pointA;
  13368. }
  13369. return points;
  13370. }
  13371. /**
  13372. * Calculates the corner position of the white top right module.
  13373. */
  13374. correctTopRight(points) {
  13375. // A..D
  13376. // | :
  13377. // B--C
  13378. let pointA = points[0];
  13379. let pointB = points[1];
  13380. let pointC = points[2];
  13381. let pointD = points[3];
  13382. // shift points for safe transition detection.
  13383. let trTop = this.transitionsBetween(pointA, pointD);
  13384. let trRight = this.transitionsBetween(pointB, pointD);
  13385. let pointAs = Detector$2.shiftPoint(pointA, pointB, (trRight + 1) * 4);
  13386. let pointCs = Detector$2.shiftPoint(pointC, pointB, (trTop + 1) * 4);
  13387. trTop = this.transitionsBetween(pointAs, pointD);
  13388. trRight = this.transitionsBetween(pointCs, pointD);
  13389. let candidate1 = new ResultPoint(pointD.getX() + (pointC.getX() - pointB.getX()) / (trTop + 1), pointD.getY() + (pointC.getY() - pointB.getY()) / (trTop + 1));
  13390. let candidate2 = new ResultPoint(pointD.getX() + (pointA.getX() - pointB.getX()) / (trRight + 1), pointD.getY() + (pointA.getY() - pointB.getY()) / (trRight + 1));
  13391. if (!this.isValid(candidate1)) {
  13392. if (this.isValid(candidate2)) {
  13393. return candidate2;
  13394. }
  13395. return null;
  13396. }
  13397. if (!this.isValid(candidate2)) {
  13398. return candidate1;
  13399. }
  13400. let sumc1 = this.transitionsBetween(pointAs, candidate1) + this.transitionsBetween(pointCs, candidate1);
  13401. let sumc2 = this.transitionsBetween(pointAs, candidate2) + this.transitionsBetween(pointCs, candidate2);
  13402. if (sumc1 > sumc2) {
  13403. return candidate1;
  13404. }
  13405. else {
  13406. return candidate2;
  13407. }
  13408. }
  13409. /**
  13410. * Shift the edge points to the module center.
  13411. */
  13412. shiftToModuleCenter(points) {
  13413. // A..D
  13414. // | :
  13415. // B--C
  13416. let pointA = points[0];
  13417. let pointB = points[1];
  13418. let pointC = points[2];
  13419. let pointD = points[3];
  13420. // calculate pseudo dimensions
  13421. let dimH = this.transitionsBetween(pointA, pointD) + 1;
  13422. let dimV = this.transitionsBetween(pointC, pointD) + 1;
  13423. // shift points for safe dimension detection
  13424. let pointAs = Detector$2.shiftPoint(pointA, pointB, dimV * 4);
  13425. let pointCs = Detector$2.shiftPoint(pointC, pointB, dimH * 4);
  13426. // calculate more precise dimensions
  13427. dimH = this.transitionsBetween(pointAs, pointD) + 1;
  13428. dimV = this.transitionsBetween(pointCs, pointD) + 1;
  13429. if ((dimH & 0x01) === 1) {
  13430. dimH += 1;
  13431. }
  13432. if ((dimV & 0x01) === 1) {
  13433. dimV += 1;
  13434. }
  13435. // WhiteRectangleDetector returns points inside of the rectangle.
  13436. // I want points on the edges.
  13437. let centerX = (pointA.getX() + pointB.getX() + pointC.getX() + pointD.getX()) / 4;
  13438. let centerY = (pointA.getY() + pointB.getY() + pointC.getY() + pointD.getY()) / 4;
  13439. pointA = Detector$2.moveAway(pointA, centerX, centerY);
  13440. pointB = Detector$2.moveAway(pointB, centerX, centerY);
  13441. pointC = Detector$2.moveAway(pointC, centerX, centerY);
  13442. pointD = Detector$2.moveAway(pointD, centerX, centerY);
  13443. let pointBs;
  13444. let pointDs;
  13445. // shift points to the center of each modules
  13446. pointAs = Detector$2.shiftPoint(pointA, pointB, dimV * 4);
  13447. pointAs = Detector$2.shiftPoint(pointAs, pointD, dimH * 4);
  13448. pointBs = Detector$2.shiftPoint(pointB, pointA, dimV * 4);
  13449. pointBs = Detector$2.shiftPoint(pointBs, pointC, dimH * 4);
  13450. pointCs = Detector$2.shiftPoint(pointC, pointD, dimV * 4);
  13451. pointCs = Detector$2.shiftPoint(pointCs, pointB, dimH * 4);
  13452. pointDs = Detector$2.shiftPoint(pointD, pointC, dimV * 4);
  13453. pointDs = Detector$2.shiftPoint(pointDs, pointA, dimH * 4);
  13454. return [pointAs, pointBs, pointCs, pointDs];
  13455. }
  13456. isValid(p) {
  13457. return p.getX() >= 0 && p.getX() < this.image.getWidth() && p.getY() > 0 && p.getY() < this.image.getHeight();
  13458. }
  13459. static sampleGrid(image, topLeft, bottomLeft, bottomRight, topRight, dimensionX, dimensionY) {
  13460. const sampler = GridSamplerInstance.getInstance();
  13461. return sampler.sampleGrid(image, dimensionX, dimensionY, 0.5, 0.5, dimensionX - 0.5, 0.5, dimensionX - 0.5, dimensionY - 0.5, 0.5, dimensionY - 0.5, topLeft.getX(), topLeft.getY(), topRight.getX(), topRight.getY(), bottomRight.getX(), bottomRight.getY(), bottomLeft.getX(), bottomLeft.getY());
  13462. }
  13463. /**
  13464. * Counts the number of black/white transitions between two points, using something like Bresenham's algorithm.
  13465. */
  13466. transitionsBetween(from, to) {
  13467. // See QR Code Detector, sizeOfBlackWhiteBlackRun()
  13468. let fromX = Math.trunc(from.getX());
  13469. let fromY = Math.trunc(from.getY());
  13470. let toX = Math.trunc(to.getX());
  13471. let toY = Math.trunc(to.getY());
  13472. let steep = Math.abs(toY - fromY) > Math.abs(toX - fromX);
  13473. if (steep) {
  13474. let temp = fromX;
  13475. fromX = fromY;
  13476. fromY = temp;
  13477. temp = toX;
  13478. toX = toY;
  13479. toY = temp;
  13480. }
  13481. let dx = Math.abs(toX - fromX);
  13482. let dy = Math.abs(toY - fromY);
  13483. let error = -dx / 2;
  13484. let ystep = fromY < toY ? 1 : -1;
  13485. let xstep = fromX < toX ? 1 : -1;
  13486. let transitions = 0;
  13487. let inBlack = this.image.get(steep ? fromY : fromX, steep ? fromX : fromY);
  13488. for (let x = fromX, y = fromY; x !== toX; x += xstep) {
  13489. let isBlack = this.image.get(steep ? y : x, steep ? x : y);
  13490. if (isBlack !== inBlack) {
  13491. transitions++;
  13492. inBlack = isBlack;
  13493. }
  13494. error += dy;
  13495. if (error > 0) {
  13496. if (y === toY) {
  13497. break;
  13498. }
  13499. y += ystep;
  13500. error -= dx;
  13501. }
  13502. }
  13503. return transitions;
  13504. }
  13505. }
  13506. /*
  13507. * Copyright 2007 ZXing authors
  13508. *
  13509. * Licensed under the Apache License, Version 2.0 (the "License");
  13510. * you may not use this file except in compliance with the License.
  13511. * You may obtain a copy of the License at
  13512. *
  13513. * http://www.apache.org/licenses/LICENSE-2.0
  13514. *
  13515. * Unless required by applicable law or agreed to in writing, software
  13516. * distributed under the License is distributed on an "AS IS" BASIS,
  13517. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13518. * See the License for the specific language governing permissions and
  13519. * limitations under the License.
  13520. */
  13521. /**
  13522. * This implementation can detect and decode Data Matrix codes in an image.
  13523. *
  13524. * @author bbrown@google.com (Brian Brown)
  13525. */
  13526. class DataMatrixReader {
  13527. constructor() {
  13528. this.decoder = new Decoder$1();
  13529. }
  13530. /**
  13531. * Locates and decodes a Data Matrix code in an image.
  13532. *
  13533. * @return a String representing the content encoded by the Data Matrix code
  13534. * @throws NotFoundException if a Data Matrix code cannot be found
  13535. * @throws FormatException if a Data Matrix code cannot be decoded
  13536. * @throws ChecksumException if error correction fails
  13537. */
  13538. // @Override
  13539. // public Result decode(BinaryBitmap image) throws NotFoundException, ChecksumException, FormatException {
  13540. // return decode(image, null);
  13541. // }
  13542. // @Override
  13543. decode(image, hints = null) {
  13544. let decoderResult;
  13545. let points;
  13546. if (hints != null && hints.has(DecodeHintType$1.PURE_BARCODE)) {
  13547. const bits = DataMatrixReader.extractPureBits(image.getBlackMatrix());
  13548. decoderResult = this.decoder.decode(bits);
  13549. points = DataMatrixReader.NO_POINTS;
  13550. }
  13551. else {
  13552. const detectorResult = new Detector$2(image.getBlackMatrix()).detect();
  13553. decoderResult = this.decoder.decode(detectorResult.getBits());
  13554. points = detectorResult.getPoints();
  13555. }
  13556. const rawBytes = decoderResult.getRawBytes();
  13557. const result = new Result$1(decoderResult.getText(), rawBytes, 8 * rawBytes.length, points, BarcodeFormat$1.DATA_MATRIX, System.currentTimeMillis());
  13558. const byteSegments = decoderResult.getByteSegments();
  13559. if (byteSegments != null) {
  13560. result.putMetadata(ResultMetadataType$1.BYTE_SEGMENTS, byteSegments);
  13561. }
  13562. const ecLevel = decoderResult.getECLevel();
  13563. if (ecLevel != null) {
  13564. result.putMetadata(ResultMetadataType$1.ERROR_CORRECTION_LEVEL, ecLevel);
  13565. }
  13566. return result;
  13567. }
  13568. // @Override
  13569. reset() {
  13570. // do nothing
  13571. }
  13572. /**
  13573. * This method detects a code in a "pure" image -- that is, pure monochrome image
  13574. * which contains only an unrotated, unskewed, image of a code, with some white border
  13575. * around it. This is a specialized method that works exceptionally fast in this special
  13576. * case.
  13577. *
  13578. * @see com.google.zxing.qrcode.QRCodeReader#extractPureBits(BitMatrix)
  13579. */
  13580. static extractPureBits(image) {
  13581. const leftTopBlack = image.getTopLeftOnBit();
  13582. const rightBottomBlack = image.getBottomRightOnBit();
  13583. if (leftTopBlack == null || rightBottomBlack == null) {
  13584. throw new NotFoundException();
  13585. }
  13586. const moduleSize = this.moduleSize(leftTopBlack, image);
  13587. let top = leftTopBlack[1];
  13588. const bottom = rightBottomBlack[1];
  13589. let left = leftTopBlack[0];
  13590. const right = rightBottomBlack[0];
  13591. const matrixWidth = (right - left + 1) / moduleSize;
  13592. const matrixHeight = (bottom - top + 1) / moduleSize;
  13593. if (matrixWidth <= 0 || matrixHeight <= 0) {
  13594. throw new NotFoundException();
  13595. }
  13596. // Push in the "border" by half the module width so that we start
  13597. // sampling in the middle of the module. Just in case the image is a
  13598. // little off, this will help recover.
  13599. const nudge = moduleSize / 2;
  13600. top += nudge;
  13601. left += nudge;
  13602. // Now just read off the bits
  13603. const bits = new BitMatrix(matrixWidth, matrixHeight);
  13604. for (let y = 0; y < matrixHeight; y++) {
  13605. const iOffset = top + y * moduleSize;
  13606. for (let x = 0; x < matrixWidth; x++) {
  13607. if (image.get(left + x * moduleSize, iOffset)) {
  13608. bits.set(x, y);
  13609. }
  13610. }
  13611. }
  13612. return bits;
  13613. }
  13614. static moduleSize(leftTopBlack, image) {
  13615. const width = image.getWidth();
  13616. let x = leftTopBlack[0];
  13617. const y = leftTopBlack[1];
  13618. while (x < width && image.get(x, y)) {
  13619. x++;
  13620. }
  13621. if (x === width) {
  13622. throw new NotFoundException();
  13623. }
  13624. const moduleSize = x - leftTopBlack[0];
  13625. if (moduleSize === 0) {
  13626. throw new NotFoundException();
  13627. }
  13628. return moduleSize;
  13629. }
  13630. }
  13631. DataMatrixReader.NO_POINTS = [];
  13632. /**
  13633. * @deprecated Moving to @zxing/browser
  13634. *
  13635. * QR Code reader to use from browser.
  13636. */
  13637. class BrowserDatamatrixCodeReader extends BrowserCodeReader {
  13638. /**
  13639. * Creates an instance of BrowserQRCodeReader.
  13640. * @param {number} [timeBetweenScansMillis=500] the time delay between subsequent decode tries
  13641. */
  13642. constructor(timeBetweenScansMillis = 500) {
  13643. super(new DataMatrixReader(), timeBetweenScansMillis);
  13644. }
  13645. }
  13646. /*
  13647. * Copyright 2007 ZXing authors
  13648. *
  13649. * Licensed under the Apache License, Version 2.0 (the "License");
  13650. * you may not use this file except in compliance with the License.
  13651. * You may obtain a copy of the License at
  13652. *
  13653. * http://www.apache.org/licenses/LICENSE-2.0
  13654. *
  13655. * Unless required by applicable law or agreed to in writing, software
  13656. * distributed under the License is distributed on an "AS IS" BASIS,
  13657. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13658. * See the License for the specific language governing permissions and
  13659. * limitations under the License.
  13660. */
  13661. var ErrorCorrectionLevelValues;
  13662. (function (ErrorCorrectionLevelValues) {
  13663. ErrorCorrectionLevelValues[ErrorCorrectionLevelValues["L"] = 0] = "L";
  13664. ErrorCorrectionLevelValues[ErrorCorrectionLevelValues["M"] = 1] = "M";
  13665. ErrorCorrectionLevelValues[ErrorCorrectionLevelValues["Q"] = 2] = "Q";
  13666. ErrorCorrectionLevelValues[ErrorCorrectionLevelValues["H"] = 3] = "H";
  13667. })(ErrorCorrectionLevelValues || (ErrorCorrectionLevelValues = {}));
  13668. /**
  13669. * <p>See ISO 18004:2006, 6.5.1. This enum encapsulates the four error correction levels
  13670. * defined by the QR code standard.</p>
  13671. *
  13672. * @author Sean Owen
  13673. */
  13674. class ErrorCorrectionLevel {
  13675. constructor(value, stringValue, bits /*int*/) {
  13676. this.value = value;
  13677. this.stringValue = stringValue;
  13678. this.bits = bits;
  13679. ErrorCorrectionLevel.FOR_BITS.set(bits, this);
  13680. ErrorCorrectionLevel.FOR_VALUE.set(value, this);
  13681. }
  13682. getValue() {
  13683. return this.value;
  13684. }
  13685. getBits() {
  13686. return this.bits;
  13687. }
  13688. static fromString(s) {
  13689. switch (s) {
  13690. case 'L': return ErrorCorrectionLevel.L;
  13691. case 'M': return ErrorCorrectionLevel.M;
  13692. case 'Q': return ErrorCorrectionLevel.Q;
  13693. case 'H': return ErrorCorrectionLevel.H;
  13694. default: throw new ArgumentException(s + 'not available');
  13695. }
  13696. }
  13697. toString() {
  13698. return this.stringValue;
  13699. }
  13700. equals(o) {
  13701. if (!(o instanceof ErrorCorrectionLevel)) {
  13702. return false;
  13703. }
  13704. const other = o;
  13705. return this.value === other.value;
  13706. }
  13707. /**
  13708. * @param bits int containing the two bits encoding a QR Code's error correction level
  13709. * @return ErrorCorrectionLevel representing the encoded error correction level
  13710. */
  13711. static forBits(bits /*int*/) {
  13712. if (bits < 0 || bits >= ErrorCorrectionLevel.FOR_BITS.size) {
  13713. throw new IllegalArgumentException();
  13714. }
  13715. return ErrorCorrectionLevel.FOR_BITS.get(bits);
  13716. }
  13717. }
  13718. ErrorCorrectionLevel.FOR_BITS = new Map();
  13719. ErrorCorrectionLevel.FOR_VALUE = new Map();
  13720. /** L = ~7% correction */
  13721. ErrorCorrectionLevel.L = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.L, 'L', 0x01);
  13722. /** M = ~15% correction */
  13723. ErrorCorrectionLevel.M = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.M, 'M', 0x00);
  13724. /** Q = ~25% correction */
  13725. ErrorCorrectionLevel.Q = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.Q, 'Q', 0x03);
  13726. /** H = ~30% correction */
  13727. ErrorCorrectionLevel.H = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.H, 'H', 0x02);
  13728. /*
  13729. * Copyright 2007 ZXing authors
  13730. *
  13731. * Licensed under the Apache License, Version 2.0 (the "License");
  13732. * you may not use this file except in compliance with the License.
  13733. * You may obtain a copy of the License at
  13734. *
  13735. * http://www.apache.org/licenses/LICENSE-2.0
  13736. *
  13737. * Unless required by applicable law or agreed to in writing, software
  13738. * distributed under the License is distributed on an "AS IS" BASIS,
  13739. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13740. * See the License for the specific language governing permissions and
  13741. * limitations under the License.
  13742. */
  13743. /**
  13744. * <p>Encapsulates a QR Code's format information, including the data mask used and
  13745. * error correction level.</p>
  13746. *
  13747. * @author Sean Owen
  13748. * @see DataMask
  13749. * @see ErrorCorrectionLevel
  13750. */
  13751. class FormatInformation {
  13752. constructor(formatInfo /*int*/) {
  13753. // Bits 3,4
  13754. this.errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
  13755. // Bottom 3 bits
  13756. this.dataMask = /*(byte) */ (formatInfo & 0x07);
  13757. }
  13758. static numBitsDiffering(a /*int*/, b /*int*/) {
  13759. return Integer.bitCount(a ^ b);
  13760. }
  13761. /**
  13762. * @param maskedFormatInfo1 format info indicator, with mask still applied
  13763. * @param maskedFormatInfo2 second copy of same info; both are checked at the same time
  13764. * to establish best match
  13765. * @return information about the format it specifies, or {@code null}
  13766. * if doesn't seem to match any known pattern
  13767. */
  13768. static decodeFormatInformation(maskedFormatInfo1 /*int*/, maskedFormatInfo2 /*int*/) {
  13769. const formatInfo = FormatInformation.doDecodeFormatInformation(maskedFormatInfo1, maskedFormatInfo2);
  13770. if (formatInfo !== null) {
  13771. return formatInfo;
  13772. }
  13773. // Should return null, but, some QR codes apparently
  13774. // do not mask this info. Try again by actually masking the pattern
  13775. // first
  13776. return FormatInformation.doDecodeFormatInformation(maskedFormatInfo1 ^ FormatInformation.FORMAT_INFO_MASK_QR, maskedFormatInfo2 ^ FormatInformation.FORMAT_INFO_MASK_QR);
  13777. }
  13778. static doDecodeFormatInformation(maskedFormatInfo1 /*int*/, maskedFormatInfo2 /*int*/) {
  13779. // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
  13780. let bestDifference = Number.MAX_SAFE_INTEGER;
  13781. let bestFormatInfo = 0;
  13782. for (const decodeInfo of FormatInformation.FORMAT_INFO_DECODE_LOOKUP) {
  13783. const targetInfo = decodeInfo[0];
  13784. if (targetInfo === maskedFormatInfo1 || targetInfo === maskedFormatInfo2) {
  13785. // Found an exact match
  13786. return new FormatInformation(decodeInfo[1]);
  13787. }
  13788. let bitsDifference = FormatInformation.numBitsDiffering(maskedFormatInfo1, targetInfo);
  13789. if (bitsDifference < bestDifference) {
  13790. bestFormatInfo = decodeInfo[1];
  13791. bestDifference = bitsDifference;
  13792. }
  13793. if (maskedFormatInfo1 !== maskedFormatInfo2) {
  13794. // also try the other option
  13795. bitsDifference = FormatInformation.numBitsDiffering(maskedFormatInfo2, targetInfo);
  13796. if (bitsDifference < bestDifference) {
  13797. bestFormatInfo = decodeInfo[1];
  13798. bestDifference = bitsDifference;
  13799. }
  13800. }
  13801. }
  13802. // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits
  13803. // differing means we found a match
  13804. if (bestDifference <= 3) {
  13805. return new FormatInformation(bestFormatInfo);
  13806. }
  13807. return null;
  13808. }
  13809. getErrorCorrectionLevel() {
  13810. return this.errorCorrectionLevel;
  13811. }
  13812. getDataMask() {
  13813. return this.dataMask;
  13814. }
  13815. /*@Override*/
  13816. hashCode() {
  13817. return (this.errorCorrectionLevel.getBits() << 3) | this.dataMask;
  13818. }
  13819. /*@Override*/
  13820. equals(o) {
  13821. if (!(o instanceof FormatInformation)) {
  13822. return false;
  13823. }
  13824. const other = o;
  13825. return this.errorCorrectionLevel === other.errorCorrectionLevel &&
  13826. this.dataMask === other.dataMask;
  13827. }
  13828. }
  13829. FormatInformation.FORMAT_INFO_MASK_QR = 0x5412;
  13830. /**
  13831. * See ISO 18004:2006, Annex C, Table C.1
  13832. */
  13833. FormatInformation.FORMAT_INFO_DECODE_LOOKUP = [
  13834. Int32Array.from([0x5412, 0x00]),
  13835. Int32Array.from([0x5125, 0x01]),
  13836. Int32Array.from([0x5E7C, 0x02]),
  13837. Int32Array.from([0x5B4B, 0x03]),
  13838. Int32Array.from([0x45F9, 0x04]),
  13839. Int32Array.from([0x40CE, 0x05]),
  13840. Int32Array.from([0x4F97, 0x06]),
  13841. Int32Array.from([0x4AA0, 0x07]),
  13842. Int32Array.from([0x77C4, 0x08]),
  13843. Int32Array.from([0x72F3, 0x09]),
  13844. Int32Array.from([0x7DAA, 0x0A]),
  13845. Int32Array.from([0x789D, 0x0B]),
  13846. Int32Array.from([0x662F, 0x0C]),
  13847. Int32Array.from([0x6318, 0x0D]),
  13848. Int32Array.from([0x6C41, 0x0E]),
  13849. Int32Array.from([0x6976, 0x0F]),
  13850. Int32Array.from([0x1689, 0x10]),
  13851. Int32Array.from([0x13BE, 0x11]),
  13852. Int32Array.from([0x1CE7, 0x12]),
  13853. Int32Array.from([0x19D0, 0x13]),
  13854. Int32Array.from([0x0762, 0x14]),
  13855. Int32Array.from([0x0255, 0x15]),
  13856. Int32Array.from([0x0D0C, 0x16]),
  13857. Int32Array.from([0x083B, 0x17]),
  13858. Int32Array.from([0x355F, 0x18]),
  13859. Int32Array.from([0x3068, 0x19]),
  13860. Int32Array.from([0x3F31, 0x1A]),
  13861. Int32Array.from([0x3A06, 0x1B]),
  13862. Int32Array.from([0x24B4, 0x1C]),
  13863. Int32Array.from([0x2183, 0x1D]),
  13864. Int32Array.from([0x2EDA, 0x1E]),
  13865. Int32Array.from([0x2BED, 0x1F]),
  13866. ];
  13867. /**
  13868. * <p>Encapsulates a set of error-correction blocks in one symbol version. Most versions will
  13869. * use blocks of differing sizes within one version, so, this encapsulates the parameters for
  13870. * each set of blocks. It also holds the number of error-correction codewords per block since it
  13871. * will be the same across all blocks within one version.</p>
  13872. */
  13873. class ECBlocks {
  13874. constructor(ecCodewordsPerBlock /*int*/, ...ecBlocks) {
  13875. this.ecCodewordsPerBlock = ecCodewordsPerBlock;
  13876. this.ecBlocks = ecBlocks;
  13877. }
  13878. getECCodewordsPerBlock() {
  13879. return this.ecCodewordsPerBlock;
  13880. }
  13881. getNumBlocks() {
  13882. let total = 0;
  13883. const ecBlocks = this.ecBlocks;
  13884. for (const ecBlock of ecBlocks) {
  13885. total += ecBlock.getCount();
  13886. }
  13887. return total;
  13888. }
  13889. getTotalECCodewords() {
  13890. return this.ecCodewordsPerBlock * this.getNumBlocks();
  13891. }
  13892. getECBlocks() {
  13893. return this.ecBlocks;
  13894. }
  13895. }
  13896. /**
  13897. * <p>Encapsulates the parameters for one error-correction block in one symbol version.
  13898. * This includes the number of data codewords, and the number of times a block with these
  13899. * parameters is used consecutively in the QR code version's format.</p>
  13900. */
  13901. class ECB {
  13902. constructor(count /*int*/, dataCodewords /*int*/) {
  13903. this.count = count;
  13904. this.dataCodewords = dataCodewords;
  13905. }
  13906. getCount() {
  13907. return this.count;
  13908. }
  13909. getDataCodewords() {
  13910. return this.dataCodewords;
  13911. }
  13912. }
  13913. /*
  13914. * Copyright 2007 ZXing authors
  13915. *
  13916. * Licensed under the Apache License, Version 2.0 (the "License");
  13917. * you may not use this file except in compliance with the License.
  13918. * You may obtain a copy of the License at
  13919. *
  13920. * http://www.apache.org/licenses/LICENSE-2.0
  13921. *
  13922. * Unless required by applicable law or agreed to in writing, software
  13923. * distributed under the License is distributed on an "AS IS" BASIS,
  13924. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13925. * See the License for the specific language governing permissions and
  13926. * limitations under the License.
  13927. */
  13928. /**
  13929. * See ISO 18004:2006 Annex D
  13930. *
  13931. * @author Sean Owen
  13932. */
  13933. class Version {
  13934. constructor(versionNumber /*int*/, alignmentPatternCenters, ...ecBlocks) {
  13935. this.versionNumber = versionNumber;
  13936. this.alignmentPatternCenters = alignmentPatternCenters;
  13937. this.ecBlocks = ecBlocks;
  13938. let total = 0;
  13939. const ecCodewords = ecBlocks[0].getECCodewordsPerBlock();
  13940. const ecbArray = ecBlocks[0].getECBlocks();
  13941. for (const ecBlock of ecbArray) {
  13942. total += ecBlock.getCount() * (ecBlock.getDataCodewords() + ecCodewords);
  13943. }
  13944. this.totalCodewords = total;
  13945. }
  13946. getVersionNumber() {
  13947. return this.versionNumber;
  13948. }
  13949. getAlignmentPatternCenters() {
  13950. return this.alignmentPatternCenters;
  13951. }
  13952. getTotalCodewords() {
  13953. return this.totalCodewords;
  13954. }
  13955. getDimensionForVersion() {
  13956. return 17 + 4 * this.versionNumber;
  13957. }
  13958. getECBlocksForLevel(ecLevel) {
  13959. return this.ecBlocks[ecLevel.getValue()];
  13960. // TYPESCRIPTPORT: original was using ordinal, and using the order of levels as defined in ErrorCorrectionLevel enum (LMQH)
  13961. // I will use the direct value from ErrorCorrectionLevelValues enum which in typescript goes to a number
  13962. }
  13963. /**
  13964. * <p>Deduces version information purely from QR Code dimensions.</p>
  13965. *
  13966. * @param dimension dimension in modules
  13967. * @return Version for a QR Code of that dimension
  13968. * @throws FormatException if dimension is not 1 mod 4
  13969. */
  13970. static getProvisionalVersionForDimension(dimension /*int*/) {
  13971. if (dimension % 4 !== 1) {
  13972. throw new FormatException();
  13973. }
  13974. try {
  13975. return this.getVersionForNumber((dimension - 17) / 4);
  13976. }
  13977. catch (ignored /*: IllegalArgumentException*/) {
  13978. throw new FormatException();
  13979. }
  13980. }
  13981. static getVersionForNumber(versionNumber /*int*/) {
  13982. if (versionNumber < 1 || versionNumber > 40) {
  13983. throw new IllegalArgumentException();
  13984. }
  13985. return Version.VERSIONS[versionNumber - 1];
  13986. }
  13987. static decodeVersionInformation(versionBits /*int*/) {
  13988. let bestDifference = Number.MAX_SAFE_INTEGER;
  13989. let bestVersion = 0;
  13990. for (let i = 0; i < Version.VERSION_DECODE_INFO.length; i++) {
  13991. const targetVersion = Version.VERSION_DECODE_INFO[i];
  13992. // Do the version info bits match exactly? done.
  13993. if (targetVersion === versionBits) {
  13994. return Version.getVersionForNumber(i + 7);
  13995. }
  13996. // Otherwise see if this is the closest to a real version info bit string
  13997. // we have seen so far
  13998. const bitsDifference = FormatInformation.numBitsDiffering(versionBits, targetVersion);
  13999. if (bitsDifference < bestDifference) {
  14000. bestVersion = i + 7;
  14001. bestDifference = bitsDifference;
  14002. }
  14003. }
  14004. // We can tolerate up to 3 bits of error since no two version info codewords will
  14005. // differ in less than 8 bits.
  14006. if (bestDifference <= 3) {
  14007. return Version.getVersionForNumber(bestVersion);
  14008. }
  14009. // If we didn't find a close enough match, fail
  14010. return null;
  14011. }
  14012. /**
  14013. * See ISO 18004:2006 Annex E
  14014. */
  14015. buildFunctionPattern() {
  14016. const dimension = this.getDimensionForVersion();
  14017. const bitMatrix = new BitMatrix(dimension);
  14018. // Top left finder pattern + separator + format
  14019. bitMatrix.setRegion(0, 0, 9, 9);
  14020. // Top right finder pattern + separator + format
  14021. bitMatrix.setRegion(dimension - 8, 0, 8, 9);
  14022. // Bottom left finder pattern + separator + format
  14023. bitMatrix.setRegion(0, dimension - 8, 9, 8);
  14024. // Alignment patterns
  14025. const max = this.alignmentPatternCenters.length;
  14026. for (let x = 0; x < max; x++) {
  14027. const i = this.alignmentPatternCenters[x] - 2;
  14028. for (let y = 0; y < max; y++) {
  14029. if ((x === 0 && (y === 0 || y === max - 1)) || (x === max - 1 && y === 0)) {
  14030. // No alignment patterns near the three finder patterns
  14031. continue;
  14032. }
  14033. bitMatrix.setRegion(this.alignmentPatternCenters[y] - 2, i, 5, 5);
  14034. }
  14035. }
  14036. // Vertical timing pattern
  14037. bitMatrix.setRegion(6, 9, 1, dimension - 17);
  14038. // Horizontal timing pattern
  14039. bitMatrix.setRegion(9, 6, dimension - 17, 1);
  14040. if (this.versionNumber > 6) {
  14041. // Version info, top right
  14042. bitMatrix.setRegion(dimension - 11, 0, 3, 6);
  14043. // Version info, bottom left
  14044. bitMatrix.setRegion(0, dimension - 11, 6, 3);
  14045. }
  14046. return bitMatrix;
  14047. }
  14048. /*@Override*/
  14049. toString() {
  14050. return '' + this.versionNumber;
  14051. }
  14052. }
  14053. /**
  14054. * See ISO 18004:2006 Annex D.
  14055. * Element i represents the raw version bits that specify version i + 7
  14056. */
  14057. Version.VERSION_DECODE_INFO = Int32Array.from([
  14058. 0x07C94, 0x085BC, 0x09A99, 0x0A4D3, 0x0BBF6,
  14059. 0x0C762, 0x0D847, 0x0E60D, 0x0F928, 0x10B78,
  14060. 0x1145D, 0x12A17, 0x13532, 0x149A6, 0x15683,
  14061. 0x168C9, 0x177EC, 0x18EC4, 0x191E1, 0x1AFAB,
  14062. 0x1B08E, 0x1CC1A, 0x1D33F, 0x1ED75, 0x1F250,
  14063. 0x209D5, 0x216F0, 0x228BA, 0x2379F, 0x24B0B,
  14064. 0x2542E, 0x26A64, 0x27541, 0x28C69
  14065. ]);
  14066. /**
  14067. * See ISO 18004:2006 6.5.1 Table 9
  14068. */
  14069. Version.VERSIONS = [
  14070. new Version(1, new Int32Array(0), new ECBlocks(7, new ECB(1, 19)), new ECBlocks(10, new ECB(1, 16)), new ECBlocks(13, new ECB(1, 13)), new ECBlocks(17, new ECB(1, 9))),
  14071. new Version(2, Int32Array.from([6, 18]), new ECBlocks(10, new ECB(1, 34)), new ECBlocks(16, new ECB(1, 28)), new ECBlocks(22, new ECB(1, 22)), new ECBlocks(28, new ECB(1, 16))),
  14072. new Version(3, Int32Array.from([6, 22]), new ECBlocks(15, new ECB(1, 55)), new ECBlocks(26, new ECB(1, 44)), new ECBlocks(18, new ECB(2, 17)), new ECBlocks(22, new ECB(2, 13))),
  14073. new Version(4, Int32Array.from([6, 26]), new ECBlocks(20, new ECB(1, 80)), new ECBlocks(18, new ECB(2, 32)), new ECBlocks(26, new ECB(2, 24)), new ECBlocks(16, new ECB(4, 9))),
  14074. new Version(5, Int32Array.from([6, 30]), new ECBlocks(26, new ECB(1, 108)), new ECBlocks(24, new ECB(2, 43)), new ECBlocks(18, new ECB(2, 15), new ECB(2, 16)), new ECBlocks(22, new ECB(2, 11), new ECB(2, 12))),
  14075. new Version(6, Int32Array.from([6, 34]), new ECBlocks(18, new ECB(2, 68)), new ECBlocks(16, new ECB(4, 27)), new ECBlocks(24, new ECB(4, 19)), new ECBlocks(28, new ECB(4, 15))),
  14076. new Version(7, Int32Array.from([6, 22, 38]), new ECBlocks(20, new ECB(2, 78)), new ECBlocks(18, new ECB(4, 31)), new ECBlocks(18, new ECB(2, 14), new ECB(4, 15)), new ECBlocks(26, new ECB(4, 13), new ECB(1, 14))),
  14077. new Version(8, Int32Array.from([6, 24, 42]), new ECBlocks(24, new ECB(2, 97)), new ECBlocks(22, new ECB(2, 38), new ECB(2, 39)), new ECBlocks(22, new ECB(4, 18), new ECB(2, 19)), new ECBlocks(26, new ECB(4, 14), new ECB(2, 15))),
  14078. new Version(9, Int32Array.from([6, 26, 46]), new ECBlocks(30, new ECB(2, 116)), new ECBlocks(22, new ECB(3, 36), new ECB(2, 37)), new ECBlocks(20, new ECB(4, 16), new ECB(4, 17)), new ECBlocks(24, new ECB(4, 12), new ECB(4, 13))),
  14079. new Version(10, Int32Array.from([6, 28, 50]), new ECBlocks(18, new ECB(2, 68), new ECB(2, 69)), new ECBlocks(26, new ECB(4, 43), new ECB(1, 44)), new ECBlocks(24, new ECB(6, 19), new ECB(2, 20)), new ECBlocks(28, new ECB(6, 15), new ECB(2, 16))),
  14080. new Version(11, Int32Array.from([6, 30, 54]), new ECBlocks(20, new ECB(4, 81)), new ECBlocks(30, new ECB(1, 50), new ECB(4, 51)), new ECBlocks(28, new ECB(4, 22), new ECB(4, 23)), new ECBlocks(24, new ECB(3, 12), new ECB(8, 13))),
  14081. new Version(12, Int32Array.from([6, 32, 58]), new ECBlocks(24, new ECB(2, 92), new ECB(2, 93)), new ECBlocks(22, new ECB(6, 36), new ECB(2, 37)), new ECBlocks(26, new ECB(4, 20), new ECB(6, 21)), new ECBlocks(28, new ECB(7, 14), new ECB(4, 15))),
  14082. new Version(13, Int32Array.from([6, 34, 62]), new ECBlocks(26, new ECB(4, 107)), new ECBlocks(22, new ECB(8, 37), new ECB(1, 38)), new ECBlocks(24, new ECB(8, 20), new ECB(4, 21)), new ECBlocks(22, new ECB(12, 11), new ECB(4, 12))),
  14083. new Version(14, Int32Array.from([6, 26, 46, 66]), new ECBlocks(30, new ECB(3, 115), new ECB(1, 116)), new ECBlocks(24, new ECB(4, 40), new ECB(5, 41)), new ECBlocks(20, new ECB(11, 16), new ECB(5, 17)), new ECBlocks(24, new ECB(11, 12), new ECB(5, 13))),
  14084. new Version(15, Int32Array.from([6, 26, 48, 70]), new ECBlocks(22, new ECB(5, 87), new ECB(1, 88)), new ECBlocks(24, new ECB(5, 41), new ECB(5, 42)), new ECBlocks(30, new ECB(5, 24), new ECB(7, 25)), new ECBlocks(24, new ECB(11, 12), new ECB(7, 13))),
  14085. new Version(16, Int32Array.from([6, 26, 50, 74]), new ECBlocks(24, new ECB(5, 98), new ECB(1, 99)), new ECBlocks(28, new ECB(7, 45), new ECB(3, 46)), new ECBlocks(24, new ECB(15, 19), new ECB(2, 20)), new ECBlocks(30, new ECB(3, 15), new ECB(13, 16))),
  14086. new Version(17, Int32Array.from([6, 30, 54, 78]), new ECBlocks(28, new ECB(1, 107), new ECB(5, 108)), new ECBlocks(28, new ECB(10, 46), new ECB(1, 47)), new ECBlocks(28, new ECB(1, 22), new ECB(15, 23)), new ECBlocks(28, new ECB(2, 14), new ECB(17, 15))),
  14087. new Version(18, Int32Array.from([6, 30, 56, 82]), new ECBlocks(30, new ECB(5, 120), new ECB(1, 121)), new ECBlocks(26, new ECB(9, 43), new ECB(4, 44)), new ECBlocks(28, new ECB(17, 22), new ECB(1, 23)), new ECBlocks(28, new ECB(2, 14), new ECB(19, 15))),
  14088. new Version(19, Int32Array.from([6, 30, 58, 86]), new ECBlocks(28, new ECB(3, 113), new ECB(4, 114)), new ECBlocks(26, new ECB(3, 44), new ECB(11, 45)), new ECBlocks(26, new ECB(17, 21), new ECB(4, 22)), new ECBlocks(26, new ECB(9, 13), new ECB(16, 14))),
  14089. new Version(20, Int32Array.from([6, 34, 62, 90]), new ECBlocks(28, new ECB(3, 107), new ECB(5, 108)), new ECBlocks(26, new ECB(3, 41), new ECB(13, 42)), new ECBlocks(30, new ECB(15, 24), new ECB(5, 25)), new ECBlocks(28, new ECB(15, 15), new ECB(10, 16))),
  14090. new Version(21, Int32Array.from([6, 28, 50, 72, 94]), new ECBlocks(28, new ECB(4, 116), new ECB(4, 117)), new ECBlocks(26, new ECB(17, 42)), new ECBlocks(28, new ECB(17, 22), new ECB(6, 23)), new ECBlocks(30, new ECB(19, 16), new ECB(6, 17))),
  14091. new Version(22, Int32Array.from([6, 26, 50, 74, 98]), new ECBlocks(28, new ECB(2, 111), new ECB(7, 112)), new ECBlocks(28, new ECB(17, 46)), new ECBlocks(30, new ECB(7, 24), new ECB(16, 25)), new ECBlocks(24, new ECB(34, 13))),
  14092. new Version(23, Int32Array.from([6, 30, 54, 78, 102]), new ECBlocks(30, new ECB(4, 121), new ECB(5, 122)), new ECBlocks(28, new ECB(4, 47), new ECB(14, 48)), new ECBlocks(30, new ECB(11, 24), new ECB(14, 25)), new ECBlocks(30, new ECB(16, 15), new ECB(14, 16))),
  14093. new Version(24, Int32Array.from([6, 28, 54, 80, 106]), new ECBlocks(30, new ECB(6, 117), new ECB(4, 118)), new ECBlocks(28, new ECB(6, 45), new ECB(14, 46)), new ECBlocks(30, new ECB(11, 24), new ECB(16, 25)), new ECBlocks(30, new ECB(30, 16), new ECB(2, 17))),
  14094. new Version(25, Int32Array.from([6, 32, 58, 84, 110]), new ECBlocks(26, new ECB(8, 106), new ECB(4, 107)), new ECBlocks(28, new ECB(8, 47), new ECB(13, 48)), new ECBlocks(30, new ECB(7, 24), new ECB(22, 25)), new ECBlocks(30, new ECB(22, 15), new ECB(13, 16))),
  14095. new Version(26, Int32Array.from([6, 30, 58, 86, 114]), new ECBlocks(28, new ECB(10, 114), new ECB(2, 115)), new ECBlocks(28, new ECB(19, 46), new ECB(4, 47)), new ECBlocks(28, new ECB(28, 22), new ECB(6, 23)), new ECBlocks(30, new ECB(33, 16), new ECB(4, 17))),
  14096. new Version(27, Int32Array.from([6, 34, 62, 90, 118]), new ECBlocks(30, new ECB(8, 122), new ECB(4, 123)), new ECBlocks(28, new ECB(22, 45), new ECB(3, 46)), new ECBlocks(30, new ECB(8, 23), new ECB(26, 24)), new ECBlocks(30, new ECB(12, 15), new ECB(28, 16))),
  14097. new Version(28, Int32Array.from([6, 26, 50, 74, 98, 122]), new ECBlocks(30, new ECB(3, 117), new ECB(10, 118)), new ECBlocks(28, new ECB(3, 45), new ECB(23, 46)), new ECBlocks(30, new ECB(4, 24), new ECB(31, 25)), new ECBlocks(30, new ECB(11, 15), new ECB(31, 16))),
  14098. new Version(29, Int32Array.from([6, 30, 54, 78, 102, 126]), new ECBlocks(30, new ECB(7, 116), new ECB(7, 117)), new ECBlocks(28, new ECB(21, 45), new ECB(7, 46)), new ECBlocks(30, new ECB(1, 23), new ECB(37, 24)), new ECBlocks(30, new ECB(19, 15), new ECB(26, 16))),
  14099. new Version(30, Int32Array.from([6, 26, 52, 78, 104, 130]), new ECBlocks(30, new ECB(5, 115), new ECB(10, 116)), new ECBlocks(28, new ECB(19, 47), new ECB(10, 48)), new ECBlocks(30, new ECB(15, 24), new ECB(25, 25)), new ECBlocks(30, new ECB(23, 15), new ECB(25, 16))),
  14100. new Version(31, Int32Array.from([6, 30, 56, 82, 108, 134]), new ECBlocks(30, new ECB(13, 115), new ECB(3, 116)), new ECBlocks(28, new ECB(2, 46), new ECB(29, 47)), new ECBlocks(30, new ECB(42, 24), new ECB(1, 25)), new ECBlocks(30, new ECB(23, 15), new ECB(28, 16))),
  14101. new Version(32, Int32Array.from([6, 34, 60, 86, 112, 138]), new ECBlocks(30, new ECB(17, 115)), new ECBlocks(28, new ECB(10, 46), new ECB(23, 47)), new ECBlocks(30, new ECB(10, 24), new ECB(35, 25)), new ECBlocks(30, new ECB(19, 15), new ECB(35, 16))),
  14102. new Version(33, Int32Array.from([6, 30, 58, 86, 114, 142]), new ECBlocks(30, new ECB(17, 115), new ECB(1, 116)), new ECBlocks(28, new ECB(14, 46), new ECB(21, 47)), new ECBlocks(30, new ECB(29, 24), new ECB(19, 25)), new ECBlocks(30, new ECB(11, 15), new ECB(46, 16))),
  14103. new Version(34, Int32Array.from([6, 34, 62, 90, 118, 146]), new ECBlocks(30, new ECB(13, 115), new ECB(6, 116)), new ECBlocks(28, new ECB(14, 46), new ECB(23, 47)), new ECBlocks(30, new ECB(44, 24), new ECB(7, 25)), new ECBlocks(30, new ECB(59, 16), new ECB(1, 17))),
  14104. new Version(35, Int32Array.from([6, 30, 54, 78, 102, 126, 150]), new ECBlocks(30, new ECB(12, 121), new ECB(7, 122)), new ECBlocks(28, new ECB(12, 47), new ECB(26, 48)), new ECBlocks(30, new ECB(39, 24), new ECB(14, 25)), new ECBlocks(30, new ECB(22, 15), new ECB(41, 16))),
  14105. new Version(36, Int32Array.from([6, 24, 50, 76, 102, 128, 154]), new ECBlocks(30, new ECB(6, 121), new ECB(14, 122)), new ECBlocks(28, new ECB(6, 47), new ECB(34, 48)), new ECBlocks(30, new ECB(46, 24), new ECB(10, 25)), new ECBlocks(30, new ECB(2, 15), new ECB(64, 16))),
  14106. new Version(37, Int32Array.from([6, 28, 54, 80, 106, 132, 158]), new ECBlocks(30, new ECB(17, 122), new ECB(4, 123)), new ECBlocks(28, new ECB(29, 46), new ECB(14, 47)), new ECBlocks(30, new ECB(49, 24), new ECB(10, 25)), new ECBlocks(30, new ECB(24, 15), new ECB(46, 16))),
  14107. new Version(38, Int32Array.from([6, 32, 58, 84, 110, 136, 162]), new ECBlocks(30, new ECB(4, 122), new ECB(18, 123)), new ECBlocks(28, new ECB(13, 46), new ECB(32, 47)), new ECBlocks(30, new ECB(48, 24), new ECB(14, 25)), new ECBlocks(30, new ECB(42, 15), new ECB(32, 16))),
  14108. new Version(39, Int32Array.from([6, 26, 54, 82, 110, 138, 166]), new ECBlocks(30, new ECB(20, 117), new ECB(4, 118)), new ECBlocks(28, new ECB(40, 47), new ECB(7, 48)), new ECBlocks(30, new ECB(43, 24), new ECB(22, 25)), new ECBlocks(30, new ECB(10, 15), new ECB(67, 16))),
  14109. new Version(40, Int32Array.from([6, 30, 58, 86, 114, 142, 170]), new ECBlocks(30, new ECB(19, 118), new ECB(6, 119)), new ECBlocks(28, new ECB(18, 47), new ECB(31, 48)), new ECBlocks(30, new ECB(34, 24), new ECB(34, 25)), new ECBlocks(30, new ECB(20, 15), new ECB(61, 16)))
  14110. ];
  14111. /*
  14112. * Copyright 2007 ZXing authors
  14113. *
  14114. * Licensed under the Apache License, Version 2.0 (the "License");
  14115. * you may not use this file except in compliance with the License.
  14116. * You may obtain a copy of the License at
  14117. *
  14118. * http://www.apache.org/licenses/LICENSE-2.0
  14119. *
  14120. * Unless required by applicable law or agreed to in writing, software
  14121. * distributed under the License is distributed on an "AS IS" BASIS,
  14122. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14123. * See the License for the specific language governing permissions and
  14124. * limitations under the License.
  14125. */
  14126. var DataMaskValues;
  14127. (function (DataMaskValues) {
  14128. DataMaskValues[DataMaskValues["DATA_MASK_000"] = 0] = "DATA_MASK_000";
  14129. DataMaskValues[DataMaskValues["DATA_MASK_001"] = 1] = "DATA_MASK_001";
  14130. DataMaskValues[DataMaskValues["DATA_MASK_010"] = 2] = "DATA_MASK_010";
  14131. DataMaskValues[DataMaskValues["DATA_MASK_011"] = 3] = "DATA_MASK_011";
  14132. DataMaskValues[DataMaskValues["DATA_MASK_100"] = 4] = "DATA_MASK_100";
  14133. DataMaskValues[DataMaskValues["DATA_MASK_101"] = 5] = "DATA_MASK_101";
  14134. DataMaskValues[DataMaskValues["DATA_MASK_110"] = 6] = "DATA_MASK_110";
  14135. DataMaskValues[DataMaskValues["DATA_MASK_111"] = 7] = "DATA_MASK_111";
  14136. })(DataMaskValues || (DataMaskValues = {}));
  14137. /**
  14138. * <p>Encapsulates data masks for the data bits in a QR code, per ISO 18004:2006 6.8. Implementations
  14139. * of this class can un-mask a raw BitMatrix. For simplicity, they will unmask the entire BitMatrix,
  14140. * including areas used for finder patterns, timing patterns, etc. These areas should be unused
  14141. * after the point they are unmasked anyway.</p>
  14142. *
  14143. * <p>Note that the diagram in section 6.8.1 is misleading since it indicates that i is column position
  14144. * and j is row position. In fact, as the text says, i is row position and j is column position.</p>
  14145. *
  14146. * @author Sean Owen
  14147. */
  14148. class DataMask {
  14149. // See ISO 18004:2006 6.8.1
  14150. constructor(value, isMasked) {
  14151. this.value = value;
  14152. this.isMasked = isMasked;
  14153. }
  14154. // End of enum constants.
  14155. /**
  14156. * <p>Implementations of this method reverse the data masking process applied to a QR Code and
  14157. * make its bits ready to read.</p>
  14158. *
  14159. * @param bits representation of QR Code bits
  14160. * @param dimension dimension of QR Code, represented by bits, being unmasked
  14161. */
  14162. unmaskBitMatrix(bits, dimension /*int*/) {
  14163. for (let i = 0; i < dimension; i++) {
  14164. for (let j = 0; j < dimension; j++) {
  14165. if (this.isMasked(i, j)) {
  14166. bits.flip(j, i);
  14167. }
  14168. }
  14169. }
  14170. }
  14171. }
  14172. DataMask.values = new Map([
  14173. /**
  14174. * 000: mask bits for which (x + y) mod 2 == 0
  14175. */
  14176. [DataMaskValues.DATA_MASK_000, new DataMask(DataMaskValues.DATA_MASK_000, (i /*int*/, j /*int*/) => { return ((i + j) & 0x01) === 0; })],
  14177. /**
  14178. * 001: mask bits for which x mod 2 == 0
  14179. */
  14180. [DataMaskValues.DATA_MASK_001, new DataMask(DataMaskValues.DATA_MASK_001, (i /*int*/, j /*int*/) => { return (i & 0x01) === 0; })],
  14181. /**
  14182. * 010: mask bits for which y mod 3 == 0
  14183. */
  14184. [DataMaskValues.DATA_MASK_010, new DataMask(DataMaskValues.DATA_MASK_010, (i /*int*/, j /*int*/) => { return j % 3 === 0; })],
  14185. /**
  14186. * 011: mask bits for which (x + y) mod 3 == 0
  14187. */
  14188. [DataMaskValues.DATA_MASK_011, new DataMask(DataMaskValues.DATA_MASK_011, (i /*int*/, j /*int*/) => { return (i + j) % 3 === 0; })],
  14189. /**
  14190. * 100: mask bits for which (x/2 + y/3) mod 2 == 0
  14191. */
  14192. [DataMaskValues.DATA_MASK_100, new DataMask(DataMaskValues.DATA_MASK_100, (i /*int*/, j /*int*/) => { return ((Math.floor(i / 2) + Math.floor(j / 3)) & 0x01) === 0; })],
  14193. /**
  14194. * 101: mask bits for which xy mod 2 + xy mod 3 == 0
  14195. * equivalently, such that xy mod 6 == 0
  14196. */
  14197. [DataMaskValues.DATA_MASK_101, new DataMask(DataMaskValues.DATA_MASK_101, (i /*int*/, j /*int*/) => { return (i * j) % 6 === 0; })],
  14198. /**
  14199. * 110: mask bits for which (xy mod 2 + xy mod 3) mod 2 == 0
  14200. * equivalently, such that xy mod 6 < 3
  14201. */
  14202. [DataMaskValues.DATA_MASK_110, new DataMask(DataMaskValues.DATA_MASK_110, (i /*int*/, j /*int*/) => { return ((i * j) % 6) < 3; })],
  14203. /**
  14204. * 111: mask bits for which ((x+y)mod 2 + xy mod 3) mod 2 == 0
  14205. * equivalently, such that (x + y + xy mod 3) mod 2 == 0
  14206. */
  14207. [DataMaskValues.DATA_MASK_111, new DataMask(DataMaskValues.DATA_MASK_111, (i /*int*/, j /*int*/) => { return ((i + j + ((i * j) % 3)) & 0x01) === 0; })],
  14208. ]);
  14209. /*
  14210. * Copyright 2007 ZXing authors
  14211. *
  14212. * Licensed under the Apache License, Version 2.0 (the "License");
  14213. * you may not use this file except in compliance with the License.
  14214. * You may obtain a copy of the License at
  14215. *
  14216. * http://www.apache.org/licenses/LICENSE-2.0
  14217. *
  14218. * Unless required by applicable law or agreed to in writing, software
  14219. * distributed under the License is distributed on an "AS IS" BASIS,
  14220. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14221. * See the License for the specific language governing permissions and
  14222. * limitations under the License.
  14223. */
  14224. /**
  14225. * @author Sean Owen
  14226. */
  14227. class BitMatrixParser {
  14228. /**
  14229. * @param bitMatrix {@link BitMatrix} to parse
  14230. * @throws FormatException if dimension is not >= 21 and 1 mod 4
  14231. */
  14232. constructor(bitMatrix) {
  14233. const dimension = bitMatrix.getHeight();
  14234. if (dimension < 21 || (dimension & 0x03) !== 1) {
  14235. throw new FormatException();
  14236. }
  14237. this.bitMatrix = bitMatrix;
  14238. }
  14239. /**
  14240. * <p>Reads format information from one of its two locations within the QR Code.</p>
  14241. *
  14242. * @return {@link FormatInformation} encapsulating the QR Code's format info
  14243. * @throws FormatException if both format information locations cannot be parsed as
  14244. * the valid encoding of format information
  14245. */
  14246. readFormatInformation() {
  14247. if (this.parsedFormatInfo !== null && this.parsedFormatInfo !== undefined) {
  14248. return this.parsedFormatInfo;
  14249. }
  14250. // Read top-left format info bits
  14251. let formatInfoBits1 = 0;
  14252. for (let i = 0; i < 6; i++) {
  14253. formatInfoBits1 = this.copyBit(i, 8, formatInfoBits1);
  14254. }
  14255. // .. and skip a bit in the timing pattern ...
  14256. formatInfoBits1 = this.copyBit(7, 8, formatInfoBits1);
  14257. formatInfoBits1 = this.copyBit(8, 8, formatInfoBits1);
  14258. formatInfoBits1 = this.copyBit(8, 7, formatInfoBits1);
  14259. // .. and skip a bit in the timing pattern ...
  14260. for (let j = 5; j >= 0; j--) {
  14261. formatInfoBits1 = this.copyBit(8, j, formatInfoBits1);
  14262. }
  14263. // Read the top-right/bottom-left pattern too
  14264. const dimension = this.bitMatrix.getHeight();
  14265. let formatInfoBits2 = 0;
  14266. const jMin = dimension - 7;
  14267. for (let j = dimension - 1; j >= jMin; j--) {
  14268. formatInfoBits2 = this.copyBit(8, j, formatInfoBits2);
  14269. }
  14270. for (let i = dimension - 8; i < dimension; i++) {
  14271. formatInfoBits2 = this.copyBit(i, 8, formatInfoBits2);
  14272. }
  14273. this.parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits1, formatInfoBits2);
  14274. if (this.parsedFormatInfo !== null) {
  14275. return this.parsedFormatInfo;
  14276. }
  14277. throw new FormatException();
  14278. }
  14279. /**
  14280. * <p>Reads version information from one of its two locations within the QR Code.</p>
  14281. *
  14282. * @return {@link Version} encapsulating the QR Code's version
  14283. * @throws FormatException if both version information locations cannot be parsed as
  14284. * the valid encoding of version information
  14285. */
  14286. readVersion() {
  14287. if (this.parsedVersion !== null && this.parsedVersion !== undefined) {
  14288. return this.parsedVersion;
  14289. }
  14290. const dimension = this.bitMatrix.getHeight();
  14291. const provisionalVersion = Math.floor((dimension - 17) / 4);
  14292. if (provisionalVersion <= 6) {
  14293. return Version.getVersionForNumber(provisionalVersion);
  14294. }
  14295. // Read top-right version info: 3 wide by 6 tall
  14296. let versionBits = 0;
  14297. const ijMin = dimension - 11;
  14298. for (let j = 5; j >= 0; j--) {
  14299. for (let i = dimension - 9; i >= ijMin; i--) {
  14300. versionBits = this.copyBit(i, j, versionBits);
  14301. }
  14302. }
  14303. let theParsedVersion = Version.decodeVersionInformation(versionBits);
  14304. if (theParsedVersion !== null && theParsedVersion.getDimensionForVersion() === dimension) {
  14305. this.parsedVersion = theParsedVersion;
  14306. return theParsedVersion;
  14307. }
  14308. // Hmm, failed. Try bottom left: 6 wide by 3 tall
  14309. versionBits = 0;
  14310. for (let i = 5; i >= 0; i--) {
  14311. for (let j = dimension - 9; j >= ijMin; j--) {
  14312. versionBits = this.copyBit(i, j, versionBits);
  14313. }
  14314. }
  14315. theParsedVersion = Version.decodeVersionInformation(versionBits);
  14316. if (theParsedVersion !== null && theParsedVersion.getDimensionForVersion() === dimension) {
  14317. this.parsedVersion = theParsedVersion;
  14318. return theParsedVersion;
  14319. }
  14320. throw new FormatException();
  14321. }
  14322. copyBit(i /*int*/, j /*int*/, versionBits /*int*/) {
  14323. const bit = this.isMirror ? this.bitMatrix.get(j, i) : this.bitMatrix.get(i, j);
  14324. return bit ? (versionBits << 1) | 0x1 : versionBits << 1;
  14325. }
  14326. /**
  14327. * <p>Reads the bits in the {@link BitMatrix} representing the finder pattern in the
  14328. * correct order in order to reconstruct the codewords bytes contained within the
  14329. * QR Code.</p>
  14330. *
  14331. * @return bytes encoded within the QR Code
  14332. * @throws FormatException if the exact number of bytes expected is not read
  14333. */
  14334. readCodewords() {
  14335. const formatInfo = this.readFormatInformation();
  14336. const version = this.readVersion();
  14337. // Get the data mask for the format used in this QR Code. This will exclude
  14338. // some bits from reading as we wind through the bit matrix.
  14339. const dataMask = DataMask.values.get(formatInfo.getDataMask());
  14340. const dimension = this.bitMatrix.getHeight();
  14341. dataMask.unmaskBitMatrix(this.bitMatrix, dimension);
  14342. const functionPattern = version.buildFunctionPattern();
  14343. let readingUp = true;
  14344. const result = new Uint8Array(version.getTotalCodewords());
  14345. let resultOffset = 0;
  14346. let currentByte = 0;
  14347. let bitsRead = 0;
  14348. // Read columns in pairs, from right to left
  14349. for (let j = dimension - 1; j > 0; j -= 2) {
  14350. if (j === 6) {
  14351. // Skip whole column with vertical alignment pattern
  14352. // saves time and makes the other code proceed more cleanly
  14353. j--;
  14354. }
  14355. // Read alternatingly from bottom to top then top to bottom
  14356. for (let count = 0; count < dimension; count++) {
  14357. const i = readingUp ? dimension - 1 - count : count;
  14358. for (let col = 0; col < 2; col++) {
  14359. // Ignore bits covered by the function pattern
  14360. if (!functionPattern.get(j - col, i)) {
  14361. // Read a bit
  14362. bitsRead++;
  14363. currentByte <<= 1;
  14364. if (this.bitMatrix.get(j - col, i)) {
  14365. currentByte |= 1;
  14366. }
  14367. // If we've made a whole byte, save it off
  14368. if (bitsRead === 8) {
  14369. result[resultOffset++] = /*(byte) */ currentByte;
  14370. bitsRead = 0;
  14371. currentByte = 0;
  14372. }
  14373. }
  14374. }
  14375. }
  14376. readingUp = !readingUp; // readingUp ^= true; // readingUp = !readingUp; // switch directions
  14377. }
  14378. if (resultOffset !== version.getTotalCodewords()) {
  14379. throw new FormatException();
  14380. }
  14381. return result;
  14382. }
  14383. /**
  14384. * Revert the mask removal done while reading the code words. The bit matrix should revert to its original state.
  14385. */
  14386. remask() {
  14387. if (this.parsedFormatInfo === null) {
  14388. return; // We have no format information, and have no data mask
  14389. }
  14390. const dataMask = DataMask.values.get(this.parsedFormatInfo.getDataMask());
  14391. const dimension = this.bitMatrix.getHeight();
  14392. dataMask.unmaskBitMatrix(this.bitMatrix, dimension);
  14393. }
  14394. /**
  14395. * Prepare the parser for a mirrored operation.
  14396. * This flag has effect only on the {@link #readFormatInformation()} and the
  14397. * {@link #readVersion()}. Before proceeding with {@link #readCodewords()} the
  14398. * {@link #mirror()} method should be called.
  14399. *
  14400. * @param mirror Whether to read version and format information mirrored.
  14401. */
  14402. setMirror(isMirror) {
  14403. this.parsedVersion = null;
  14404. this.parsedFormatInfo = null;
  14405. this.isMirror = isMirror;
  14406. }
  14407. /** Mirror the bit matrix in order to attempt a second reading. */
  14408. mirror() {
  14409. const bitMatrix = this.bitMatrix;
  14410. for (let x = 0, width = bitMatrix.getWidth(); x < width; x++) {
  14411. for (let y = x + 1, height = bitMatrix.getHeight(); y < height; y++) {
  14412. if (bitMatrix.get(x, y) !== bitMatrix.get(y, x)) {
  14413. bitMatrix.flip(y, x);
  14414. bitMatrix.flip(x, y);
  14415. }
  14416. }
  14417. }
  14418. }
  14419. }
  14420. /*
  14421. * Copyright 2007 ZXing authors
  14422. *
  14423. * Licensed under the Apache License, Version 2.0 (the "License");
  14424. * you may not use this file except in compliance with the License.
  14425. * You may obtain a copy of the License at
  14426. *
  14427. * http://www.apache.org/licenses/LICENSE-2.0
  14428. *
  14429. * Unless required by applicable law or agreed to in writing, software
  14430. * distributed under the License is distributed on an "AS IS" BASIS,
  14431. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14432. * See the License for the specific language governing permissions and
  14433. * limitations under the License.
  14434. */
  14435. /**
  14436. * <p>Encapsulates a block of data within a QR Code. QR Codes may split their data into
  14437. * multiple blocks, each of which is a unit of data and error-correction codewords. Each
  14438. * is represented by an instance of this class.</p>
  14439. *
  14440. * @author Sean Owen
  14441. */
  14442. class DataBlock {
  14443. constructor(numDataCodewords /*int*/, codewords) {
  14444. this.numDataCodewords = numDataCodewords;
  14445. this.codewords = codewords;
  14446. }
  14447. /**
  14448. * <p>When QR Codes use multiple data blocks, they are actually interleaved.
  14449. * That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This
  14450. * method will separate the data into original blocks.</p>
  14451. *
  14452. * @param rawCodewords bytes as read directly from the QR Code
  14453. * @param version version of the QR Code
  14454. * @param ecLevel error-correction level of the QR Code
  14455. * @return DataBlocks containing original bytes, "de-interleaved" from representation in the
  14456. * QR Code
  14457. */
  14458. static getDataBlocks(rawCodewords, version, ecLevel) {
  14459. if (rawCodewords.length !== version.getTotalCodewords()) {
  14460. throw new IllegalArgumentException();
  14461. }
  14462. // Figure out the number and size of data blocks used by this version and
  14463. // error correction level
  14464. const ecBlocks = version.getECBlocksForLevel(ecLevel);
  14465. // First count the total number of data blocks
  14466. let totalBlocks = 0;
  14467. const ecBlockArray = ecBlocks.getECBlocks();
  14468. for (const ecBlock of ecBlockArray) {
  14469. totalBlocks += ecBlock.getCount();
  14470. }
  14471. // Now establish DataBlocks of the appropriate size and number of data codewords
  14472. const result = new Array(totalBlocks);
  14473. let numResultBlocks = 0;
  14474. for (const ecBlock of ecBlockArray) {
  14475. for (let i = 0; i < ecBlock.getCount(); i++) {
  14476. const numDataCodewords = ecBlock.getDataCodewords();
  14477. const numBlockCodewords = ecBlocks.getECCodewordsPerBlock() + numDataCodewords;
  14478. result[numResultBlocks++] = new DataBlock(numDataCodewords, new Uint8Array(numBlockCodewords));
  14479. }
  14480. }
  14481. // All blocks have the same amount of data, except that the last n
  14482. // (where n may be 0) have 1 more byte. Figure out where these start.
  14483. const shorterBlocksTotalCodewords = result[0].codewords.length;
  14484. let longerBlocksStartAt = result.length - 1;
  14485. // TYPESCRIPTPORT: check length is correct here
  14486. while (longerBlocksStartAt >= 0) {
  14487. const numCodewords = result[longerBlocksStartAt].codewords.length;
  14488. if (numCodewords === shorterBlocksTotalCodewords) {
  14489. break;
  14490. }
  14491. longerBlocksStartAt--;
  14492. }
  14493. longerBlocksStartAt++;
  14494. const shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.getECCodewordsPerBlock();
  14495. // The last elements of result may be 1 element longer
  14496. // first fill out as many elements as all of them have
  14497. let rawCodewordsOffset = 0;
  14498. for (let i = 0; i < shorterBlocksNumDataCodewords; i++) {
  14499. for (let j = 0; j < numResultBlocks; j++) {
  14500. result[j].codewords[i] = rawCodewords[rawCodewordsOffset++];
  14501. }
  14502. }
  14503. // Fill out the last data block in the longer ones
  14504. for (let j = longerBlocksStartAt; j < numResultBlocks; j++) {
  14505. result[j].codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset++];
  14506. }
  14507. // Now add in error correction blocks
  14508. const max = result[0].codewords.length;
  14509. for (let i = shorterBlocksNumDataCodewords; i < max; i++) {
  14510. for (let j = 0; j < numResultBlocks; j++) {
  14511. const iOffset = j < longerBlocksStartAt ? i : i + 1;
  14512. result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset++];
  14513. }
  14514. }
  14515. return result;
  14516. }
  14517. getNumDataCodewords() {
  14518. return this.numDataCodewords;
  14519. }
  14520. getCodewords() {
  14521. return this.codewords;
  14522. }
  14523. }
  14524. /*
  14525. * Copyright 2007 ZXing authors
  14526. *
  14527. * Licensed under the Apache License, Version 2.0 (the "License");
  14528. * you may not use this file except in compliance with the License.
  14529. * You may obtain a copy of the License at
  14530. *
  14531. * http://www.apache.org/licenses/LICENSE-2.0
  14532. *
  14533. * Unless required by applicable law or agreed to in writing, software
  14534. * distributed under the License is distributed on an "AS IS" BASIS,
  14535. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14536. * See the License for the specific language governing permissions and
  14537. * limitations under the License.
  14538. */
  14539. var ModeValues;
  14540. (function (ModeValues) {
  14541. ModeValues[ModeValues["TERMINATOR"] = 0] = "TERMINATOR";
  14542. ModeValues[ModeValues["NUMERIC"] = 1] = "NUMERIC";
  14543. ModeValues[ModeValues["ALPHANUMERIC"] = 2] = "ALPHANUMERIC";
  14544. ModeValues[ModeValues["STRUCTURED_APPEND"] = 3] = "STRUCTURED_APPEND";
  14545. ModeValues[ModeValues["BYTE"] = 4] = "BYTE";
  14546. ModeValues[ModeValues["ECI"] = 5] = "ECI";
  14547. ModeValues[ModeValues["KANJI"] = 6] = "KANJI";
  14548. ModeValues[ModeValues["FNC1_FIRST_POSITION"] = 7] = "FNC1_FIRST_POSITION";
  14549. ModeValues[ModeValues["FNC1_SECOND_POSITION"] = 8] = "FNC1_SECOND_POSITION";
  14550. /** See GBT 18284-2000; "Hanzi" is a transliteration of this mode name. */
  14551. ModeValues[ModeValues["HANZI"] = 9] = "HANZI";
  14552. })(ModeValues || (ModeValues = {}));
  14553. /**
  14554. * <p>See ISO 18004:2006, 6.4.1, Tables 2 and 3. This enum encapsulates the various modes in which
  14555. * data can be encoded to bits in the QR code standard.</p>
  14556. *
  14557. * @author Sean Owen
  14558. */
  14559. class Mode$2 {
  14560. constructor(value, stringValue, characterCountBitsForVersions, bits /*int*/) {
  14561. this.value = value;
  14562. this.stringValue = stringValue;
  14563. this.characterCountBitsForVersions = characterCountBitsForVersions;
  14564. this.bits = bits;
  14565. Mode$2.FOR_BITS.set(bits, this);
  14566. Mode$2.FOR_VALUE.set(value, this);
  14567. }
  14568. /**
  14569. * @param bits four bits encoding a QR Code data mode
  14570. * @return Mode encoded by these bits
  14571. * @throws IllegalArgumentException if bits do not correspond to a known mode
  14572. */
  14573. static forBits(bits /*int*/) {
  14574. const mode = Mode$2.FOR_BITS.get(bits);
  14575. if (undefined === mode) {
  14576. throw new IllegalArgumentException();
  14577. }
  14578. return mode;
  14579. }
  14580. /**
  14581. * @param version version in question
  14582. * @return number of bits used, in this QR Code symbol {@link Version}, to encode the
  14583. * count of characters that will follow encoded in this Mode
  14584. */
  14585. getCharacterCountBits(version) {
  14586. const versionNumber = version.getVersionNumber();
  14587. let offset;
  14588. if (versionNumber <= 9) {
  14589. offset = 0;
  14590. }
  14591. else if (versionNumber <= 26) {
  14592. offset = 1;
  14593. }
  14594. else {
  14595. offset = 2;
  14596. }
  14597. return this.characterCountBitsForVersions[offset];
  14598. }
  14599. getValue() {
  14600. return this.value;
  14601. }
  14602. getBits() {
  14603. return this.bits;
  14604. }
  14605. equals(o) {
  14606. if (!(o instanceof Mode$2)) {
  14607. return false;
  14608. }
  14609. const other = o;
  14610. return this.value === other.value;
  14611. }
  14612. toString() {
  14613. return this.stringValue;
  14614. }
  14615. }
  14616. Mode$2.FOR_BITS = new Map();
  14617. Mode$2.FOR_VALUE = new Map();
  14618. Mode$2.TERMINATOR = new Mode$2(ModeValues.TERMINATOR, 'TERMINATOR', Int32Array.from([0, 0, 0]), 0x00); // Not really a mode...
  14619. Mode$2.NUMERIC = new Mode$2(ModeValues.NUMERIC, 'NUMERIC', Int32Array.from([10, 12, 14]), 0x01);
  14620. Mode$2.ALPHANUMERIC = new Mode$2(ModeValues.ALPHANUMERIC, 'ALPHANUMERIC', Int32Array.from([9, 11, 13]), 0x02);
  14621. Mode$2.STRUCTURED_APPEND = new Mode$2(ModeValues.STRUCTURED_APPEND, 'STRUCTURED_APPEND', Int32Array.from([0, 0, 0]), 0x03); // Not supported
  14622. Mode$2.BYTE = new Mode$2(ModeValues.BYTE, 'BYTE', Int32Array.from([8, 16, 16]), 0x04);
  14623. Mode$2.ECI = new Mode$2(ModeValues.ECI, 'ECI', Int32Array.from([0, 0, 0]), 0x07); // character counts don't apply
  14624. Mode$2.KANJI = new Mode$2(ModeValues.KANJI, 'KANJI', Int32Array.from([8, 10, 12]), 0x08);
  14625. Mode$2.FNC1_FIRST_POSITION = new Mode$2(ModeValues.FNC1_FIRST_POSITION, 'FNC1_FIRST_POSITION', Int32Array.from([0, 0, 0]), 0x05);
  14626. Mode$2.FNC1_SECOND_POSITION = new Mode$2(ModeValues.FNC1_SECOND_POSITION, 'FNC1_SECOND_POSITION', Int32Array.from([0, 0, 0]), 0x09);
  14627. /** See GBT 18284-2000; "Hanzi" is a transliteration of this mode name. */
  14628. Mode$2.HANZI = new Mode$2(ModeValues.HANZI, 'HANZI', Int32Array.from([8, 10, 12]), 0x0D);
  14629. /*
  14630. * Copyright 2007 ZXing authors
  14631. *
  14632. * Licensed under the Apache License, Version 2.0 (the "License");
  14633. * you may not use this file except in compliance with the License.
  14634. * You may obtain a copy of the License at
  14635. *
  14636. * http://www.apache.org/licenses/LICENSE-2.0
  14637. *
  14638. * Unless required by applicable law or agreed to in writing, software
  14639. * distributed under the License is distributed on an "AS IS" BASIS,
  14640. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14641. * See the License for the specific language governing permissions and
  14642. * limitations under the License.
  14643. */
  14644. /*import java.io.UnsupportedEncodingException;*/
  14645. /*import java.util.ArrayList;*/
  14646. /*import java.util.Collection;*/
  14647. /*import java.util.List;*/
  14648. /*import java.util.Map;*/
  14649. /**
  14650. * <p>QR Codes can encode text as bits in one of several modes, and can use multiple modes
  14651. * in one QR Code. This class decodes the bits back into text.</p>
  14652. *
  14653. * <p>See ISO 18004:2006, 6.4.3 - 6.4.7</p>
  14654. *
  14655. * @author Sean Owen
  14656. */
  14657. class DecodedBitStreamParser$1 {
  14658. static decode(bytes, version, ecLevel, hints) {
  14659. const bits = new BitSource(bytes);
  14660. let result = new StringBuilder();
  14661. const byteSegments = new Array(); // 1
  14662. // TYPESCRIPTPORT: I do not use constructor with size 1 as in original Java means capacity and the array length is checked below
  14663. let symbolSequence = -1;
  14664. let parityData = -1;
  14665. try {
  14666. let currentCharacterSetECI = null;
  14667. let fc1InEffect = false;
  14668. let mode;
  14669. do {
  14670. // While still another segment to read...
  14671. if (bits.available() < 4) {
  14672. // OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
  14673. mode = Mode$2.TERMINATOR;
  14674. }
  14675. else {
  14676. const modeBits = bits.readBits(4);
  14677. mode = Mode$2.forBits(modeBits); // mode is encoded by 4 bits
  14678. }
  14679. switch (mode) {
  14680. case Mode$2.TERMINATOR:
  14681. break;
  14682. case Mode$2.FNC1_FIRST_POSITION:
  14683. case Mode$2.FNC1_SECOND_POSITION:
  14684. // We do little with FNC1 except alter the parsed result a bit according to the spec
  14685. fc1InEffect = true;
  14686. break;
  14687. case Mode$2.STRUCTURED_APPEND:
  14688. if (bits.available() < 16) {
  14689. throw new FormatException();
  14690. }
  14691. // sequence number and parity is added later to the result metadata
  14692. // Read next 8 bits (symbol sequence #) and 8 bits (data: parity), then continue
  14693. symbolSequence = bits.readBits(8);
  14694. parityData = bits.readBits(8);
  14695. break;
  14696. case Mode$2.ECI:
  14697. // Count doesn't apply to ECI
  14698. const value = DecodedBitStreamParser$1.parseECIValue(bits);
  14699. currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value);
  14700. if (currentCharacterSetECI === null) {
  14701. throw new FormatException();
  14702. }
  14703. break;
  14704. case Mode$2.HANZI:
  14705. // First handle Hanzi mode which does not start with character count
  14706. // Chinese mode contains a sub set indicator right after mode indicator
  14707. const subset = bits.readBits(4);
  14708. const countHanzi = bits.readBits(mode.getCharacterCountBits(version));
  14709. if (subset === DecodedBitStreamParser$1.GB2312_SUBSET) {
  14710. DecodedBitStreamParser$1.decodeHanziSegment(bits, result, countHanzi);
  14711. }
  14712. break;
  14713. default:
  14714. // "Normal" QR code modes:
  14715. // How many characters will follow, encoded in this mode?
  14716. const count = bits.readBits(mode.getCharacterCountBits(version));
  14717. switch (mode) {
  14718. case Mode$2.NUMERIC:
  14719. DecodedBitStreamParser$1.decodeNumericSegment(bits, result, count);
  14720. break;
  14721. case Mode$2.ALPHANUMERIC:
  14722. DecodedBitStreamParser$1.decodeAlphanumericSegment(bits, result, count, fc1InEffect);
  14723. break;
  14724. case Mode$2.BYTE:
  14725. DecodedBitStreamParser$1.decodeByteSegment(bits, result, count, currentCharacterSetECI, byteSegments, hints);
  14726. break;
  14727. case Mode$2.KANJI:
  14728. DecodedBitStreamParser$1.decodeKanjiSegment(bits, result, count);
  14729. break;
  14730. default:
  14731. throw new FormatException();
  14732. }
  14733. break;
  14734. }
  14735. } while (mode !== Mode$2.TERMINATOR);
  14736. }
  14737. catch (iae /*: IllegalArgumentException*/) {
  14738. // from readBits() calls
  14739. throw new FormatException();
  14740. }
  14741. return new DecoderResult(bytes, result.toString(), byteSegments.length === 0 ? null : byteSegments, ecLevel === null ? null : ecLevel.toString(), symbolSequence, parityData);
  14742. }
  14743. /**
  14744. * See specification GBT 18284-2000
  14745. */
  14746. static decodeHanziSegment(bits, result, count /*int*/) {
  14747. // Don't crash trying to read more bits than we have available.
  14748. if (count * 13 > bits.available()) {
  14749. throw new FormatException();
  14750. }
  14751. // Each character will require 2 bytes. Read the characters as 2-byte pairs
  14752. // and decode as GB2312 afterwards
  14753. const buffer = new Uint8Array(2 * count);
  14754. let offset = 0;
  14755. while (count > 0) {
  14756. // Each 13 bits encodes a 2-byte character
  14757. const twoBytes = bits.readBits(13);
  14758. let assembledTwoBytes = (((twoBytes / 0x060) << 8) & 0xFFFFFFFF) | (twoBytes % 0x060);
  14759. if (assembledTwoBytes < 0x003BF) {
  14760. // In the 0xA1A1 to 0xAAFE range
  14761. assembledTwoBytes += 0x0A1A1;
  14762. }
  14763. else {
  14764. // In the 0xB0A1 to 0xFAFE range
  14765. assembledTwoBytes += 0x0A6A1;
  14766. }
  14767. buffer[offset] = /*(byte) */ ((assembledTwoBytes >> 8) & 0xFF);
  14768. buffer[offset + 1] = /*(byte) */ (assembledTwoBytes & 0xFF);
  14769. offset += 2;
  14770. count--;
  14771. }
  14772. try {
  14773. result.append(StringEncoding.decode(buffer, StringUtils.GB2312));
  14774. // TYPESCRIPTPORT: TODO: implement GB2312 decode. StringView from MDN could be a starting point
  14775. }
  14776. catch (ignored /*: UnsupportedEncodingException*/) {
  14777. throw new FormatException(ignored);
  14778. }
  14779. }
  14780. static decodeKanjiSegment(bits, result, count /*int*/) {
  14781. // Don't crash trying to read more bits than we have available.
  14782. if (count * 13 > bits.available()) {
  14783. throw new FormatException();
  14784. }
  14785. // Each character will require 2 bytes. Read the characters as 2-byte pairs
  14786. // and decode as Shift_JIS afterwards
  14787. const buffer = new Uint8Array(2 * count);
  14788. let offset = 0;
  14789. while (count > 0) {
  14790. // Each 13 bits encodes a 2-byte character
  14791. const twoBytes = bits.readBits(13);
  14792. let assembledTwoBytes = (((twoBytes / 0x0C0) << 8) & 0xFFFFFFFF) | (twoBytes % 0x0C0);
  14793. if (assembledTwoBytes < 0x01F00) {
  14794. // In the 0x8140 to 0x9FFC range
  14795. assembledTwoBytes += 0x08140;
  14796. }
  14797. else {
  14798. // In the 0xE040 to 0xEBBF range
  14799. assembledTwoBytes += 0x0C140;
  14800. }
  14801. buffer[offset] = /*(byte) */ (assembledTwoBytes >> 8);
  14802. buffer[offset + 1] = /*(byte) */ assembledTwoBytes;
  14803. offset += 2;
  14804. count--;
  14805. }
  14806. // Shift_JIS may not be supported in some environments:
  14807. try {
  14808. result.append(StringEncoding.decode(buffer, StringUtils.SHIFT_JIS));
  14809. // TYPESCRIPTPORT: TODO: implement SHIFT_JIS decode. StringView from MDN could be a starting point
  14810. }
  14811. catch (ignored /*: UnsupportedEncodingException*/) {
  14812. throw new FormatException(ignored);
  14813. }
  14814. }
  14815. static decodeByteSegment(bits, result, count /*int*/, currentCharacterSetECI, byteSegments, hints) {
  14816. // Don't crash trying to read more bits than we have available.
  14817. if (8 * count > bits.available()) {
  14818. throw new FormatException();
  14819. }
  14820. const readBytes = new Uint8Array(count);
  14821. for (let i = 0; i < count; i++) {
  14822. readBytes[i] = /*(byte) */ bits.readBits(8);
  14823. }
  14824. let encoding;
  14825. if (currentCharacterSetECI === null) {
  14826. // The spec isn't clear on this mode; see
  14827. // section 6.4.5: t does not say which encoding to assuming
  14828. // upon decoding. I have seen ISO-8859-1 used as well as
  14829. // Shift_JIS -- without anything like an ECI designator to
  14830. // give a hint.
  14831. encoding = StringUtils.guessEncoding(readBytes, hints);
  14832. }
  14833. else {
  14834. encoding = currentCharacterSetECI.getName();
  14835. }
  14836. try {
  14837. result.append(StringEncoding.decode(readBytes, encoding));
  14838. }
  14839. catch (ignored /*: UnsupportedEncodingException*/) {
  14840. throw new FormatException(ignored);
  14841. }
  14842. byteSegments.push(readBytes);
  14843. }
  14844. static toAlphaNumericChar(value /*int*/) {
  14845. if (value >= DecodedBitStreamParser$1.ALPHANUMERIC_CHARS.length) {
  14846. throw new FormatException();
  14847. }
  14848. return DecodedBitStreamParser$1.ALPHANUMERIC_CHARS[value];
  14849. }
  14850. static decodeAlphanumericSegment(bits, result, count /*int*/, fc1InEffect) {
  14851. // Read two characters at a time
  14852. const start = result.length();
  14853. while (count > 1) {
  14854. if (bits.available() < 11) {
  14855. throw new FormatException();
  14856. }
  14857. const nextTwoCharsBits = bits.readBits(11);
  14858. result.append(DecodedBitStreamParser$1.toAlphaNumericChar(Math.floor(nextTwoCharsBits / 45)));
  14859. result.append(DecodedBitStreamParser$1.toAlphaNumericChar(nextTwoCharsBits % 45));
  14860. count -= 2;
  14861. }
  14862. if (count === 1) {
  14863. // special case: one character left
  14864. if (bits.available() < 6) {
  14865. throw new FormatException();
  14866. }
  14867. result.append(DecodedBitStreamParser$1.toAlphaNumericChar(bits.readBits(6)));
  14868. }
  14869. // See section 6.4.8.1, 6.4.8.2
  14870. if (fc1InEffect) {
  14871. // We need to massage the result a bit if in an FNC1 mode:
  14872. for (let i = start; i < result.length(); i++) {
  14873. if (result.charAt(i) === '%') {
  14874. if (i < result.length() - 1 && result.charAt(i + 1) === '%') {
  14875. // %% is rendered as %
  14876. result.deleteCharAt(i + 1);
  14877. }
  14878. else {
  14879. // In alpha mode, % should be converted to FNC1 separator 0x1D
  14880. result.setCharAt(i, String.fromCharCode(0x1D));
  14881. }
  14882. }
  14883. }
  14884. }
  14885. }
  14886. static decodeNumericSegment(bits, result, count /*int*/) {
  14887. // Read three digits at a time
  14888. while (count >= 3) {
  14889. // Each 10 bits encodes three digits
  14890. if (bits.available() < 10) {
  14891. throw new FormatException();
  14892. }
  14893. const threeDigitsBits = bits.readBits(10);
  14894. if (threeDigitsBits >= 1000) {
  14895. throw new FormatException();
  14896. }
  14897. result.append(DecodedBitStreamParser$1.toAlphaNumericChar(Math.floor(threeDigitsBits / 100)));
  14898. result.append(DecodedBitStreamParser$1.toAlphaNumericChar(Math.floor(threeDigitsBits / 10) % 10));
  14899. result.append(DecodedBitStreamParser$1.toAlphaNumericChar(threeDigitsBits % 10));
  14900. count -= 3;
  14901. }
  14902. if (count === 2) {
  14903. // Two digits left over to read, encoded in 7 bits
  14904. if (bits.available() < 7) {
  14905. throw new FormatException();
  14906. }
  14907. const twoDigitsBits = bits.readBits(7);
  14908. if (twoDigitsBits >= 100) {
  14909. throw new FormatException();
  14910. }
  14911. result.append(DecodedBitStreamParser$1.toAlphaNumericChar(Math.floor(twoDigitsBits / 10)));
  14912. result.append(DecodedBitStreamParser$1.toAlphaNumericChar(twoDigitsBits % 10));
  14913. }
  14914. else if (count === 1) {
  14915. // One digit left over to read
  14916. if (bits.available() < 4) {
  14917. throw new FormatException();
  14918. }
  14919. const digitBits = bits.readBits(4);
  14920. if (digitBits >= 10) {
  14921. throw new FormatException();
  14922. }
  14923. result.append(DecodedBitStreamParser$1.toAlphaNumericChar(digitBits));
  14924. }
  14925. }
  14926. static parseECIValue(bits) {
  14927. const firstByte = bits.readBits(8);
  14928. if ((firstByte & 0x80) === 0) {
  14929. // just one byte
  14930. return firstByte & 0x7F;
  14931. }
  14932. if ((firstByte & 0xC0) === 0x80) {
  14933. // two bytes
  14934. const secondByte = bits.readBits(8);
  14935. return (((firstByte & 0x3F) << 8) & 0xFFFFFFFF) | secondByte;
  14936. }
  14937. if ((firstByte & 0xE0) === 0xC0) {
  14938. // three bytes
  14939. const secondThirdBytes = bits.readBits(16);
  14940. return (((firstByte & 0x1F) << 16) & 0xFFFFFFFF) | secondThirdBytes;
  14941. }
  14942. throw new FormatException();
  14943. }
  14944. }
  14945. /**
  14946. * See ISO 18004:2006, 6.4.4 Table 5
  14947. */
  14948. DecodedBitStreamParser$1.ALPHANUMERIC_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:';
  14949. DecodedBitStreamParser$1.GB2312_SUBSET = 1;
  14950. // function Uint8ArrayToString(a: Uint8Array): string {
  14951. // const CHUNK_SZ = 0x8000;
  14952. // const c = new StringBuilder();
  14953. // for (let i = 0, length = a.length; i < length; i += CHUNK_SZ) {
  14954. // c.append(String.fromCharCode.apply(null, a.subarray(i, i + CHUNK_SZ)));
  14955. // }
  14956. // return c.toString();
  14957. // }
  14958. /*
  14959. * Copyright 2013 ZXing authors
  14960. *
  14961. * Licensed under the Apache License, Version 2.0 (the "License");
  14962. * you may not use this file except in compliance with the License.
  14963. * You may obtain a copy of the License at
  14964. *
  14965. * http://www.apache.org/licenses/LICENSE-2.0
  14966. *
  14967. * Unless required by applicable law or agreed to in writing, software
  14968. * distributed under the License is distributed on an "AS IS" BASIS,
  14969. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14970. * See the License for the specific language governing permissions and
  14971. * limitations under the License.
  14972. */
  14973. /**
  14974. * Meta-data container for QR Code decoding. Instances of this class may be used to convey information back to the
  14975. * decoding caller. Callers are expected to process this.
  14976. *
  14977. * @see com.google.zxing.common.DecoderResult#getOther()
  14978. */
  14979. class QRCodeDecoderMetaData {
  14980. constructor(mirrored) {
  14981. this.mirrored = mirrored;
  14982. }
  14983. /**
  14984. * @return true if the QR Code was mirrored.
  14985. */
  14986. isMirrored() {
  14987. return this.mirrored;
  14988. }
  14989. /**
  14990. * Apply the result points' order correction due to mirroring.
  14991. *
  14992. * @param points Array of points to apply mirror correction to.
  14993. */
  14994. applyMirroredCorrection(points) {
  14995. if (!this.mirrored || points === null || points.length < 3) {
  14996. return;
  14997. }
  14998. const bottomLeft = points[0];
  14999. points[0] = points[2];
  15000. points[2] = bottomLeft;
  15001. // No need to 'fix' top-left and alignment pattern.
  15002. }
  15003. }
  15004. /*
  15005. * Copyright 2007 ZXing authors
  15006. *
  15007. * Licensed under the Apache License, Version 2.0 (the "License");
  15008. * you may not use this file except in compliance with the License.
  15009. * You may obtain a copy of the License at
  15010. *
  15011. * http://www.apache.org/licenses/LICENSE-2.0
  15012. *
  15013. * Unless required by applicable law or agreed to in writing, software
  15014. * distributed under the License is distributed on an "AS IS" BASIS,
  15015. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15016. * See the License for the specific language governing permissions and
  15017. * limitations under the License.
  15018. */
  15019. /*import java.util.Map;*/
  15020. /**
  15021. * <p>The main class which implements QR Code decoding -- as opposed to locating and extracting
  15022. * the QR Code from an image.</p>
  15023. *
  15024. * @author Sean Owen
  15025. */
  15026. class Decoder {
  15027. constructor() {
  15028. this.rsDecoder = new ReedSolomonDecoder(GenericGF.QR_CODE_FIELD_256);
  15029. }
  15030. // public decode(image: boolean[][]): DecoderResult /*throws ChecksumException, FormatException*/ {
  15031. // return decode(image, null)
  15032. // }
  15033. /**
  15034. * <p>Convenience method that can decode a QR Code represented as a 2D array of booleans.
  15035. * "true" is taken to mean a black module.</p>
  15036. *
  15037. * @param image booleans representing white/black QR Code modules
  15038. * @param hints decoding hints that should be used to influence decoding
  15039. * @return text and bytes encoded within the QR Code
  15040. * @throws FormatException if the QR Code cannot be decoded
  15041. * @throws ChecksumException if error correction fails
  15042. */
  15043. decodeBooleanArray(image, hints) {
  15044. return this.decodeBitMatrix(BitMatrix.parseFromBooleanArray(image), hints);
  15045. }
  15046. // public decodeBitMatrix(bits: BitMatrix): DecoderResult /*throws ChecksumException, FormatException*/ {
  15047. // return decode(bits, null)
  15048. // }
  15049. /**
  15050. * <p>Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.</p>
  15051. *
  15052. * @param bits booleans representing white/black QR Code modules
  15053. * @param hints decoding hints that should be used to influence decoding
  15054. * @return text and bytes encoded within the QR Code
  15055. * @throws FormatException if the QR Code cannot be decoded
  15056. * @throws ChecksumException if error correction fails
  15057. */
  15058. decodeBitMatrix(bits, hints) {
  15059. // Construct a parser and read version, error-correction level
  15060. const parser = new BitMatrixParser(bits);
  15061. let ex = null;
  15062. try {
  15063. return this.decodeBitMatrixParser(parser, hints);
  15064. }
  15065. catch (e /*: FormatException, ChecksumException*/) {
  15066. ex = e;
  15067. }
  15068. try {
  15069. // Revert the bit matrix
  15070. parser.remask();
  15071. // Will be attempting a mirrored reading of the version and format info.
  15072. parser.setMirror(true);
  15073. // Preemptively read the version.
  15074. parser.readVersion();
  15075. // Preemptively read the format information.
  15076. parser.readFormatInformation();
  15077. /*
  15078. * Since we're here, this means we have successfully detected some kind
  15079. * of version and format information when mirrored. This is a good sign,
  15080. * that the QR code may be mirrored, and we should try once more with a
  15081. * mirrored content.
  15082. */
  15083. // Prepare for a mirrored reading.
  15084. parser.mirror();
  15085. const result = this.decodeBitMatrixParser(parser, hints);
  15086. // Success! Notify the caller that the code was mirrored.
  15087. result.setOther(new QRCodeDecoderMetaData(true));
  15088. return result;
  15089. }
  15090. catch (e /*FormatException | ChecksumException*/) {
  15091. // Throw the exception from the original reading
  15092. if (ex !== null) {
  15093. throw ex;
  15094. }
  15095. throw e;
  15096. }
  15097. }
  15098. decodeBitMatrixParser(parser, hints) {
  15099. const version = parser.readVersion();
  15100. const ecLevel = parser.readFormatInformation().getErrorCorrectionLevel();
  15101. // Read codewords
  15102. const codewords = parser.readCodewords();
  15103. // Separate into data blocks
  15104. const dataBlocks = DataBlock.getDataBlocks(codewords, version, ecLevel);
  15105. // Count total number of data bytes
  15106. let totalBytes = 0;
  15107. for (const dataBlock of dataBlocks) {
  15108. totalBytes += dataBlock.getNumDataCodewords();
  15109. }
  15110. const resultBytes = new Uint8Array(totalBytes);
  15111. let resultOffset = 0;
  15112. // Error-correct and copy data blocks together into a stream of bytes
  15113. for (const dataBlock of dataBlocks) {
  15114. const codewordBytes = dataBlock.getCodewords();
  15115. const numDataCodewords = dataBlock.getNumDataCodewords();
  15116. this.correctErrors(codewordBytes, numDataCodewords);
  15117. for (let i = 0; i < numDataCodewords; i++) {
  15118. resultBytes[resultOffset++] = codewordBytes[i];
  15119. }
  15120. }
  15121. // Decode the contents of that stream of bytes
  15122. return DecodedBitStreamParser$1.decode(resultBytes, version, ecLevel, hints);
  15123. }
  15124. /**
  15125. * <p>Given data and error-correction codewords received, possibly corrupted by errors, attempts to
  15126. * correct the errors in-place using Reed-Solomon error correction.</p>
  15127. *
  15128. * @param codewordBytes data and error correction codewords
  15129. * @param numDataCodewords number of codewords that are data bytes
  15130. * @throws ChecksumException if error correction fails
  15131. */
  15132. correctErrors(codewordBytes, numDataCodewords /*int*/) {
  15133. // const numCodewords = codewordBytes.length;
  15134. // First read into an array of ints
  15135. const codewordsInts = new Int32Array(codewordBytes);
  15136. // TYPESCRIPTPORT: not realy necessary to transform to ints? could redesign everything to work with unsigned bytes?
  15137. // const codewordsInts = new Int32Array(numCodewords)
  15138. // for (let i = 0; i < numCodewords; i++) {
  15139. // codewordsInts[i] = codewordBytes[i] & 0xFF
  15140. // }
  15141. try {
  15142. this.rsDecoder.decode(codewordsInts, codewordBytes.length - numDataCodewords);
  15143. }
  15144. catch (ignored /*: ReedSolomonException*/) {
  15145. throw new ChecksumException();
  15146. }
  15147. // Copy back into array of bytes -- only need to worry about the bytes that were data
  15148. // We don't care about errors in the error-correction codewords
  15149. for (let i = 0; i < numDataCodewords; i++) {
  15150. codewordBytes[i] = /*(byte) */ codewordsInts[i];
  15151. }
  15152. }
  15153. }
  15154. /*
  15155. * Copyright 2007 ZXing authors
  15156. *
  15157. * Licensed under the Apache License, Version 2.0 (the "License");
  15158. * you may not use this file except in compliance with the License.
  15159. * You may obtain a copy of the License at
  15160. *
  15161. * http://www.apache.org/licenses/LICENSE-2.0
  15162. *
  15163. * Unless required by applicable law or agreed to in writing, software
  15164. * distributed under the License is distributed on an "AS IS" BASIS,
  15165. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15166. * See the License for the specific language governing permissions and
  15167. * limitations under the License.
  15168. */
  15169. /**
  15170. * <p>Encapsulates an alignment pattern, which are the smaller square patterns found in
  15171. * all but the simplest QR Codes.</p>
  15172. *
  15173. * @author Sean Owen
  15174. */
  15175. class AlignmentPattern extends ResultPoint {
  15176. constructor(posX /*float*/, posY /*float*/, estimatedModuleSize /*float*/) {
  15177. super(posX, posY);
  15178. this.estimatedModuleSize = estimatedModuleSize;
  15179. }
  15180. /**
  15181. * <p>Determines if this alignment pattern "about equals" an alignment pattern at the stated
  15182. * position and size -- meaning, it is at nearly the same center with nearly the same size.</p>
  15183. */
  15184. aboutEquals(moduleSize /*float*/, i /*float*/, j /*float*/) {
  15185. if (Math.abs(i - this.getY()) <= moduleSize && Math.abs(j - this.getX()) <= moduleSize) {
  15186. const moduleSizeDiff = Math.abs(moduleSize - this.estimatedModuleSize);
  15187. return moduleSizeDiff <= 1.0 || moduleSizeDiff <= this.estimatedModuleSize;
  15188. }
  15189. return false;
  15190. }
  15191. /**
  15192. * Combines this object's current estimate of a finder pattern position and module size
  15193. * with a new estimate. It returns a new {@code FinderPattern} containing an average of the two.
  15194. */
  15195. combineEstimate(i /*float*/, j /*float*/, newModuleSize /*float*/) {
  15196. const combinedX = (this.getX() + j) / 2.0;
  15197. const combinedY = (this.getY() + i) / 2.0;
  15198. const combinedModuleSize = (this.estimatedModuleSize + newModuleSize) / 2.0;
  15199. return new AlignmentPattern(combinedX, combinedY, combinedModuleSize);
  15200. }
  15201. }
  15202. /*
  15203. * Copyright 2007 ZXing authors
  15204. *
  15205. * Licensed under the Apache License, Version 2.0 (the "License");
  15206. * you may not use this file except in compliance with the License.
  15207. * You may obtain a copy of the License at
  15208. *
  15209. * http://www.apache.org/licenses/LICENSE-2.0
  15210. *
  15211. * Unless required by applicable law or agreed to in writing, software
  15212. * distributed under the License is distributed on an "AS IS" BASIS,
  15213. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15214. * See the License for the specific language governing permissions and
  15215. * limitations under the License.
  15216. */
  15217. /*import java.util.ArrayList;*/
  15218. /*import java.util.List;*/
  15219. /**
  15220. * <p>This class attempts to find alignment patterns in a QR Code. Alignment patterns look like finder
  15221. * patterns but are smaller and appear at regular intervals throughout the image.</p>
  15222. *
  15223. * <p>At the moment this only looks for the bottom-right alignment pattern.</p>
  15224. *
  15225. * <p>This is mostly a simplified copy of {@link FinderPatternFinder}. It is copied,
  15226. * pasted and stripped down here for maximum performance but does unfortunately duplicate
  15227. * some code.</p>
  15228. *
  15229. * <p>This class is thread-safe but not reentrant. Each thread must allocate its own object.</p>
  15230. *
  15231. * @author Sean Owen
  15232. */
  15233. class AlignmentPatternFinder {
  15234. /**
  15235. * <p>Creates a finder that will look in a portion of the whole image.</p>
  15236. *
  15237. * @param image image to search
  15238. * @param startX left column from which to start searching
  15239. * @param startY top row from which to start searching
  15240. * @param width width of region to search
  15241. * @param height height of region to search
  15242. * @param moduleSize estimated module size so far
  15243. */
  15244. constructor(image, startX /*int*/, startY /*int*/, width /*int*/, height /*int*/, moduleSize /*float*/, resultPointCallback) {
  15245. this.image = image;
  15246. this.startX = startX;
  15247. this.startY = startY;
  15248. this.width = width;
  15249. this.height = height;
  15250. this.moduleSize = moduleSize;
  15251. this.resultPointCallback = resultPointCallback;
  15252. this.possibleCenters = []; // new Array<any>(5))
  15253. // TYPESCRIPTPORT: array initialization without size as the length is checked below
  15254. this.crossCheckStateCount = new Int32Array(3);
  15255. }
  15256. /**
  15257. * <p>This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since
  15258. * it's pretty performance-critical and so is written to be fast foremost.</p>
  15259. *
  15260. * @return {@link AlignmentPattern} if found
  15261. * @throws NotFoundException if not found
  15262. */
  15263. find() {
  15264. const startX = this.startX;
  15265. const height = this.height;
  15266. const width = this.width;
  15267. const maxJ = startX + width;
  15268. const middleI = this.startY + (height / 2);
  15269. // We are looking for black/white/black modules in 1:1:1 ratio
  15270. // this tracks the number of black/white/black modules seen so far
  15271. const stateCount = new Int32Array(3);
  15272. const image = this.image;
  15273. for (let iGen = 0; iGen < height; iGen++) {
  15274. // Search from middle outwards
  15275. const i = middleI + ((iGen & 0x01) === 0 ? Math.floor((iGen + 1) / 2) : -Math.floor((iGen + 1) / 2));
  15276. stateCount[0] = 0;
  15277. stateCount[1] = 0;
  15278. stateCount[2] = 0;
  15279. let j = startX;
  15280. // Burn off leading white pixels before anything else; if we start in the middle of
  15281. // a white run, it doesn't make sense to count its length, since we don't know if the
  15282. // white run continued to the left of the start point
  15283. while (j < maxJ && !image.get(j, i)) {
  15284. j++;
  15285. }
  15286. let currentState = 0;
  15287. while (j < maxJ) {
  15288. if (image.get(j, i)) {
  15289. // Black pixel
  15290. if (currentState === 1) { // Counting black pixels
  15291. stateCount[1]++;
  15292. }
  15293. else { // Counting white pixels
  15294. if (currentState === 2) { // A winner?
  15295. if (this.foundPatternCross(stateCount)) { // Yes
  15296. const confirmed = this.handlePossibleCenter(stateCount, i, j);
  15297. if (confirmed !== null) {
  15298. return confirmed;
  15299. }
  15300. }
  15301. stateCount[0] = stateCount[2];
  15302. stateCount[1] = 1;
  15303. stateCount[2] = 0;
  15304. currentState = 1;
  15305. }
  15306. else {
  15307. stateCount[++currentState]++;
  15308. }
  15309. }
  15310. }
  15311. else { // White pixel
  15312. if (currentState === 1) { // Counting black pixels
  15313. currentState++;
  15314. }
  15315. stateCount[currentState]++;
  15316. }
  15317. j++;
  15318. }
  15319. if (this.foundPatternCross(stateCount)) {
  15320. const confirmed = this.handlePossibleCenter(stateCount, i, maxJ);
  15321. if (confirmed !== null) {
  15322. return confirmed;
  15323. }
  15324. }
  15325. }
  15326. // Hmm, nothing we saw was observed and confirmed twice. If we had
  15327. // any guess at all, return it.
  15328. if (this.possibleCenters.length !== 0) {
  15329. return this.possibleCenters[0];
  15330. }
  15331. throw new NotFoundException();
  15332. }
  15333. /**
  15334. * Given a count of black/white/black pixels just seen and an end position,
  15335. * figures the location of the center of this black/white/black run.
  15336. */
  15337. static centerFromEnd(stateCount, end /*int*/) {
  15338. return (end - stateCount[2]) - stateCount[1] / 2.0;
  15339. }
  15340. /**
  15341. * @param stateCount count of black/white/black pixels just read
  15342. * @return true iff the proportions of the counts is close enough to the 1/1/1 ratios
  15343. * used by alignment patterns to be considered a match
  15344. */
  15345. foundPatternCross(stateCount) {
  15346. const moduleSize = this.moduleSize;
  15347. const maxVariance = moduleSize / 2.0;
  15348. for (let i = 0; i < 3; i++) {
  15349. if (Math.abs(moduleSize - stateCount[i]) >= maxVariance) {
  15350. return false;
  15351. }
  15352. }
  15353. return true;
  15354. }
  15355. /**
  15356. * <p>After a horizontal scan finds a potential alignment pattern, this method
  15357. * "cross-checks" by scanning down vertically through the center of the possible
  15358. * alignment pattern to see if the same proportion is detected.</p>
  15359. *
  15360. * @param startI row where an alignment pattern was detected
  15361. * @param centerJ center of the section that appears to cross an alignment pattern
  15362. * @param maxCount maximum reasonable number of modules that should be
  15363. * observed in any reading state, based on the results of the horizontal scan
  15364. * @return vertical center of alignment pattern, or {@link Float#NaN} if not found
  15365. */
  15366. crossCheckVertical(startI /*int*/, centerJ /*int*/, maxCount /*int*/, originalStateCountTotal /*int*/) {
  15367. const image = this.image;
  15368. const maxI = image.getHeight();
  15369. const stateCount = this.crossCheckStateCount;
  15370. stateCount[0] = 0;
  15371. stateCount[1] = 0;
  15372. stateCount[2] = 0;
  15373. // Start counting up from center
  15374. let i = startI;
  15375. while (i >= 0 && image.get(centerJ, i) && stateCount[1] <= maxCount) {
  15376. stateCount[1]++;
  15377. i--;
  15378. }
  15379. // If already too many modules in this state or ran off the edge:
  15380. if (i < 0 || stateCount[1] > maxCount) {
  15381. return NaN;
  15382. }
  15383. while (i >= 0 && !image.get(centerJ, i) && stateCount[0] <= maxCount) {
  15384. stateCount[0]++;
  15385. i--;
  15386. }
  15387. if (stateCount[0] > maxCount) {
  15388. return NaN;
  15389. }
  15390. // Now also count down from center
  15391. i = startI + 1;
  15392. while (i < maxI && image.get(centerJ, i) && stateCount[1] <= maxCount) {
  15393. stateCount[1]++;
  15394. i++;
  15395. }
  15396. if (i === maxI || stateCount[1] > maxCount) {
  15397. return NaN;
  15398. }
  15399. while (i < maxI && !image.get(centerJ, i) && stateCount[2] <= maxCount) {
  15400. stateCount[2]++;
  15401. i++;
  15402. }
  15403. if (stateCount[2] > maxCount) {
  15404. return NaN;
  15405. }
  15406. const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];
  15407. if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) {
  15408. return NaN;
  15409. }
  15410. return this.foundPatternCross(stateCount) ? AlignmentPatternFinder.centerFromEnd(stateCount, i) : NaN;
  15411. }
  15412. /**
  15413. * <p>This is called when a horizontal scan finds a possible alignment pattern. It will
  15414. * cross check with a vertical scan, and if successful, will see if this pattern had been
  15415. * found on a previous horizontal scan. If so, we consider it confirmed and conclude we have
  15416. * found the alignment pattern.</p>
  15417. *
  15418. * @param stateCount reading state module counts from horizontal scan
  15419. * @param i row where alignment pattern may be found
  15420. * @param j end of possible alignment pattern in row
  15421. * @return {@link AlignmentPattern} if we have found the same pattern twice, or null if not
  15422. */
  15423. handlePossibleCenter(stateCount, i /*int*/, j /*int*/) {
  15424. const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];
  15425. const centerJ = AlignmentPatternFinder.centerFromEnd(stateCount, j);
  15426. const centerI = this.crossCheckVertical(i, /*(int) */ centerJ, 2 * stateCount[1], stateCountTotal);
  15427. if (!isNaN(centerI)) {
  15428. const estimatedModuleSize = (stateCount[0] + stateCount[1] + stateCount[2]) / 3.0;
  15429. for (const center of this.possibleCenters) {
  15430. // Look for about the same center and module size:
  15431. if (center.aboutEquals(estimatedModuleSize, centerI, centerJ)) {
  15432. return center.combineEstimate(centerI, centerJ, estimatedModuleSize);
  15433. }
  15434. }
  15435. // Hadn't found this before; save it
  15436. const point = new AlignmentPattern(centerJ, centerI, estimatedModuleSize);
  15437. this.possibleCenters.push(point);
  15438. if (this.resultPointCallback !== null && this.resultPointCallback !== undefined) {
  15439. this.resultPointCallback.foundPossibleResultPoint(point);
  15440. }
  15441. }
  15442. return null;
  15443. }
  15444. }
  15445. /*
  15446. * Copyright 2007 ZXing authors
  15447. *
  15448. * Licensed under the Apache License, Version 2.0 (the "License");
  15449. * you may not use this file except in compliance with the License.
  15450. * You may obtain a copy of the License at
  15451. *
  15452. * http://www.apache.org/licenses/LICENSE-2.0
  15453. *
  15454. * Unless required by applicable law or agreed to in writing, software
  15455. * distributed under the License is distributed on an "AS IS" BASIS,
  15456. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15457. * See the License for the specific language governing permissions and
  15458. * limitations under the License.
  15459. */
  15460. /**
  15461. * <p>Encapsulates a finder pattern, which are the three square patterns found in
  15462. * the corners of QR Codes. It also encapsulates a count of similar finder patterns,
  15463. * as a convenience to the finder's bookkeeping.</p>
  15464. *
  15465. * @author Sean Owen
  15466. */
  15467. class FinderPattern extends ResultPoint {
  15468. // FinderPattern(posX: number/*float*/, posY: number/*float*/, estimatedModuleSize: number/*float*/) {
  15469. // this(posX, posY, estimatedModuleSize, 1)
  15470. // }
  15471. constructor(posX /*float*/, posY /*float*/, estimatedModuleSize /*float*/, count /*int*/) {
  15472. super(posX, posY);
  15473. this.estimatedModuleSize = estimatedModuleSize;
  15474. this.count = count;
  15475. if (undefined === count) {
  15476. this.count = 1;
  15477. }
  15478. }
  15479. getEstimatedModuleSize() {
  15480. return this.estimatedModuleSize;
  15481. }
  15482. getCount() {
  15483. return this.count;
  15484. }
  15485. /*
  15486. void incrementCount() {
  15487. this.count++
  15488. }
  15489. */
  15490. /**
  15491. * <p>Determines if this finder pattern "about equals" a finder pattern at the stated
  15492. * position and size -- meaning, it is at nearly the same center with nearly the same size.</p>
  15493. */
  15494. aboutEquals(moduleSize /*float*/, i /*float*/, j /*float*/) {
  15495. if (Math.abs(i - this.getY()) <= moduleSize && Math.abs(j - this.getX()) <= moduleSize) {
  15496. const moduleSizeDiff = Math.abs(moduleSize - this.estimatedModuleSize);
  15497. return moduleSizeDiff <= 1.0 || moduleSizeDiff <= this.estimatedModuleSize;
  15498. }
  15499. return false;
  15500. }
  15501. /**
  15502. * Combines this object's current estimate of a finder pattern position and module size
  15503. * with a new estimate. It returns a new {@code FinderPattern} containing a weighted average
  15504. * based on count.
  15505. */
  15506. combineEstimate(i /*float*/, j /*float*/, newModuleSize /*float*/) {
  15507. const combinedCount = this.count + 1;
  15508. const combinedX = (this.count * this.getX() + j) / combinedCount;
  15509. const combinedY = (this.count * this.getY() + i) / combinedCount;
  15510. const combinedModuleSize = (this.count * this.estimatedModuleSize + newModuleSize) / combinedCount;
  15511. return new FinderPattern(combinedX, combinedY, combinedModuleSize, combinedCount);
  15512. }
  15513. }
  15514. /*
  15515. * Copyright 2007 ZXing authors
  15516. *
  15517. * Licensed under the Apache License, Version 2.0 (the "License");
  15518. * you may not use this file except in compliance with the License.
  15519. * You may obtain a copy of the License at
  15520. *
  15521. * http://www.apache.org/licenses/LICENSE-2.0
  15522. *
  15523. * Unless required by applicable law or agreed to in writing, software
  15524. * distributed under the License is distributed on an "AS IS" BASIS,
  15525. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15526. * See the License for the specific language governing permissions and
  15527. * limitations under the License.
  15528. */
  15529. /**
  15530. * <p>Encapsulates information about finder patterns in an image, including the location of
  15531. * the three finder patterns, and their estimated module size.</p>
  15532. *
  15533. * @author Sean Owen
  15534. */
  15535. class FinderPatternInfo {
  15536. constructor(patternCenters) {
  15537. this.bottomLeft = patternCenters[0];
  15538. this.topLeft = patternCenters[1];
  15539. this.topRight = patternCenters[2];
  15540. }
  15541. getBottomLeft() {
  15542. return this.bottomLeft;
  15543. }
  15544. getTopLeft() {
  15545. return this.topLeft;
  15546. }
  15547. getTopRight() {
  15548. return this.topRight;
  15549. }
  15550. }
  15551. /*
  15552. * Copyright 2007 ZXing authors
  15553. *
  15554. * Licensed under the Apache License, Version 2.0 (the "License");
  15555. * you may not use this file except in compliance with the License.
  15556. * You may obtain a copy of the License at
  15557. *
  15558. * http://www.apache.org/licenses/LICENSE-2.0
  15559. *
  15560. * Unless required by applicable law or agreed to in writing, software
  15561. * distributed under the License is distributed on an "AS IS" BASIS,
  15562. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15563. * See the License for the specific language governing permissions and
  15564. * limitations under the License.
  15565. */
  15566. /*import java.io.Serializable;*/
  15567. /*import java.util.ArrayList;*/
  15568. /*import java.util.Collections;*/
  15569. /*import java.util.Comparator;*/
  15570. /*import java.util.List;*/
  15571. /*import java.util.Map;*/
  15572. /**
  15573. * <p>This class attempts to find finder patterns in a QR Code. Finder patterns are the square
  15574. * markers at three corners of a QR Code.</p>
  15575. *
  15576. * <p>This class is thread-safe but not reentrant. Each thread must allocate its own object.
  15577. *
  15578. * @author Sean Owen
  15579. */
  15580. class FinderPatternFinder {
  15581. /**
  15582. * <p>Creates a finder that will search the image for three finder patterns.</p>
  15583. *
  15584. * @param image image to search
  15585. */
  15586. // public constructor(image: BitMatrix) {
  15587. // this(image, null)
  15588. // }
  15589. constructor(image, resultPointCallback) {
  15590. this.image = image;
  15591. this.resultPointCallback = resultPointCallback;
  15592. this.possibleCenters = [];
  15593. this.crossCheckStateCount = new Int32Array(5);
  15594. this.resultPointCallback = resultPointCallback;
  15595. }
  15596. getImage() {
  15597. return this.image;
  15598. }
  15599. getPossibleCenters() {
  15600. return this.possibleCenters;
  15601. }
  15602. find(hints) {
  15603. const tryHarder = (hints !== null && hints !== undefined) && undefined !== hints.get(DecodeHintType$1.TRY_HARDER);
  15604. const pureBarcode = (hints !== null && hints !== undefined) && undefined !== hints.get(DecodeHintType$1.PURE_BARCODE);
  15605. const image = this.image;
  15606. const maxI = image.getHeight();
  15607. const maxJ = image.getWidth();
  15608. // We are looking for black/white/black/white/black modules in
  15609. // 1:1:3:1:1 ratio; this tracks the number of such modules seen so far
  15610. // Let's assume that the maximum version QR Code we support takes up 1/4 the height of the
  15611. // image, and then account for the center being 3 modules in size. This gives the smallest
  15612. // number of pixels the center could be, so skip this often. When trying harder, look for all
  15613. // QR versions regardless of how dense they are.
  15614. let iSkip = Math.floor((3 * maxI) / (4 * FinderPatternFinder.MAX_MODULES));
  15615. if (iSkip < FinderPatternFinder.MIN_SKIP || tryHarder) {
  15616. iSkip = FinderPatternFinder.MIN_SKIP;
  15617. }
  15618. let done = false;
  15619. const stateCount = new Int32Array(5);
  15620. for (let i = iSkip - 1; i < maxI && !done; i += iSkip) {
  15621. // Get a row of black/white values
  15622. stateCount[0] = 0;
  15623. stateCount[1] = 0;
  15624. stateCount[2] = 0;
  15625. stateCount[3] = 0;
  15626. stateCount[4] = 0;
  15627. let currentState = 0;
  15628. for (let j = 0; j < maxJ; j++) {
  15629. if (image.get(j, i)) {
  15630. // Black pixel
  15631. if ((currentState & 1) === 1) { // Counting white pixels
  15632. currentState++;
  15633. }
  15634. stateCount[currentState]++;
  15635. }
  15636. else { // White pixel
  15637. if ((currentState & 1) === 0) { // Counting black pixels
  15638. if (currentState === 4) { // A winner?
  15639. if (FinderPatternFinder.foundPatternCross(stateCount)) { // Yes
  15640. const confirmed = this.handlePossibleCenter(stateCount, i, j, pureBarcode);
  15641. if (confirmed === true) {
  15642. // Start examining every other line. Checking each line turned out to be too
  15643. // expensive and didn't improve performance.
  15644. iSkip = 2;
  15645. if (this.hasSkipped === true) {
  15646. done = this.haveMultiplyConfirmedCenters();
  15647. }
  15648. else {
  15649. const rowSkip = this.findRowSkip();
  15650. if (rowSkip > stateCount[2]) {
  15651. // Skip rows between row of lower confirmed center
  15652. // and top of presumed third confirmed center
  15653. // but back up a bit to get a full chance of detecting
  15654. // it, entire width of center of finder pattern
  15655. // Skip by rowSkip, but back off by stateCount[2] (size of last center
  15656. // of pattern we saw) to be conservative, and also back off by iSkip which
  15657. // is about to be re-added
  15658. i += rowSkip - stateCount[2] - iSkip;
  15659. j = maxJ - 1;
  15660. }
  15661. }
  15662. }
  15663. else {
  15664. stateCount[0] = stateCount[2];
  15665. stateCount[1] = stateCount[3];
  15666. stateCount[2] = stateCount[4];
  15667. stateCount[3] = 1;
  15668. stateCount[4] = 0;
  15669. currentState = 3;
  15670. continue;
  15671. }
  15672. // Clear state to start looking again
  15673. currentState = 0;
  15674. stateCount[0] = 0;
  15675. stateCount[1] = 0;
  15676. stateCount[2] = 0;
  15677. stateCount[3] = 0;
  15678. stateCount[4] = 0;
  15679. }
  15680. else { // No, shift counts back by two
  15681. stateCount[0] = stateCount[2];
  15682. stateCount[1] = stateCount[3];
  15683. stateCount[2] = stateCount[4];
  15684. stateCount[3] = 1;
  15685. stateCount[4] = 0;
  15686. currentState = 3;
  15687. }
  15688. }
  15689. else {
  15690. stateCount[++currentState]++;
  15691. }
  15692. }
  15693. else { // Counting white pixels
  15694. stateCount[currentState]++;
  15695. }
  15696. }
  15697. }
  15698. if (FinderPatternFinder.foundPatternCross(stateCount)) {
  15699. const confirmed = this.handlePossibleCenter(stateCount, i, maxJ, pureBarcode);
  15700. if (confirmed === true) {
  15701. iSkip = stateCount[0];
  15702. if (this.hasSkipped) {
  15703. // Found a third one
  15704. done = this.haveMultiplyConfirmedCenters();
  15705. }
  15706. }
  15707. }
  15708. }
  15709. const patternInfo = this.selectBestPatterns();
  15710. ResultPoint.orderBestPatterns(patternInfo);
  15711. return new FinderPatternInfo(patternInfo);
  15712. }
  15713. /**
  15714. * Given a count of black/white/black/white/black pixels just seen and an end position,
  15715. * figures the location of the center of this run.
  15716. */
  15717. static centerFromEnd(stateCount, end /*int*/) {
  15718. return (end - stateCount[4] - stateCount[3]) - stateCount[2] / 2.0;
  15719. }
  15720. /**
  15721. * @param stateCount count of black/white/black/white/black pixels just read
  15722. * @return true iff the proportions of the counts is close enough to the 1/1/3/1/1 ratios
  15723. * used by finder patterns to be considered a match
  15724. */
  15725. static foundPatternCross(stateCount) {
  15726. let totalModuleSize = 0;
  15727. for (let i = 0; i < 5; i++) {
  15728. const count = stateCount[i];
  15729. if (count === 0) {
  15730. return false;
  15731. }
  15732. totalModuleSize += count;
  15733. }
  15734. if (totalModuleSize < 7) {
  15735. return false;
  15736. }
  15737. const moduleSize = totalModuleSize / 7.0;
  15738. const maxVariance = moduleSize / 2.0;
  15739. // Allow less than 50% variance from 1-1-3-1-1 proportions
  15740. return Math.abs(moduleSize - stateCount[0]) < maxVariance &&
  15741. Math.abs(moduleSize - stateCount[1]) < maxVariance &&
  15742. Math.abs(3.0 * moduleSize - stateCount[2]) < 3 * maxVariance &&
  15743. Math.abs(moduleSize - stateCount[3]) < maxVariance &&
  15744. Math.abs(moduleSize - stateCount[4]) < maxVariance;
  15745. }
  15746. getCrossCheckStateCount() {
  15747. const crossCheckStateCount = this.crossCheckStateCount;
  15748. crossCheckStateCount[0] = 0;
  15749. crossCheckStateCount[1] = 0;
  15750. crossCheckStateCount[2] = 0;
  15751. crossCheckStateCount[3] = 0;
  15752. crossCheckStateCount[4] = 0;
  15753. return crossCheckStateCount;
  15754. }
  15755. /**
  15756. * After a vertical and horizontal scan finds a potential finder pattern, this method
  15757. * "cross-cross-cross-checks" by scanning down diagonally through the center of the possible
  15758. * finder pattern to see if the same proportion is detected.
  15759. *
  15760. * @param startI row where a finder pattern was detected
  15761. * @param centerJ center of the section that appears to cross a finder pattern
  15762. * @param maxCount maximum reasonable number of modules that should be
  15763. * observed in any reading state, based on the results of the horizontal scan
  15764. * @param originalStateCountTotal The original state count total.
  15765. * @return true if proportions are withing expected limits
  15766. */
  15767. crossCheckDiagonal(startI /*int*/, centerJ /*int*/, maxCount /*int*/, originalStateCountTotal /*int*/) {
  15768. const stateCount = this.getCrossCheckStateCount();
  15769. // Start counting up, left from center finding black center mass
  15770. let i = 0;
  15771. const image = this.image;
  15772. while (startI >= i && centerJ >= i && image.get(centerJ - i, startI - i)) {
  15773. stateCount[2]++;
  15774. i++;
  15775. }
  15776. if (startI < i || centerJ < i) {
  15777. return false;
  15778. }
  15779. // Continue up, left finding white space
  15780. while (startI >= i && centerJ >= i && !image.get(centerJ - i, startI - i) &&
  15781. stateCount[1] <= maxCount) {
  15782. stateCount[1]++;
  15783. i++;
  15784. }
  15785. // If already too many modules in this state or ran off the edge:
  15786. if (startI < i || centerJ < i || stateCount[1] > maxCount) {
  15787. return false;
  15788. }
  15789. // Continue up, left finding black border
  15790. while (startI >= i && centerJ >= i && image.get(centerJ - i, startI - i) &&
  15791. stateCount[0] <= maxCount) {
  15792. stateCount[0]++;
  15793. i++;
  15794. }
  15795. if (stateCount[0] > maxCount) {
  15796. return false;
  15797. }
  15798. const maxI = image.getHeight();
  15799. const maxJ = image.getWidth();
  15800. // Now also count down, right from center
  15801. i = 1;
  15802. while (startI + i < maxI && centerJ + i < maxJ && image.get(centerJ + i, startI + i)) {
  15803. stateCount[2]++;
  15804. i++;
  15805. }
  15806. // Ran off the edge?
  15807. if (startI + i >= maxI || centerJ + i >= maxJ) {
  15808. return false;
  15809. }
  15810. while (startI + i < maxI && centerJ + i < maxJ && !image.get(centerJ + i, startI + i) &&
  15811. stateCount[3] < maxCount) {
  15812. stateCount[3]++;
  15813. i++;
  15814. }
  15815. if (startI + i >= maxI || centerJ + i >= maxJ || stateCount[3] >= maxCount) {
  15816. return false;
  15817. }
  15818. while (startI + i < maxI && centerJ + i < maxJ && image.get(centerJ + i, startI + i) &&
  15819. stateCount[4] < maxCount) {
  15820. stateCount[4]++;
  15821. i++;
  15822. }
  15823. if (stateCount[4] >= maxCount) {
  15824. return false;
  15825. }
  15826. // If we found a finder-pattern-like section, but its size is more than 100% different than
  15827. // the original, assume it's a false positive
  15828. const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4];
  15829. return Math.abs(stateCountTotal - originalStateCountTotal) < 2 * originalStateCountTotal &&
  15830. FinderPatternFinder.foundPatternCross(stateCount);
  15831. }
  15832. /**
  15833. * <p>After a horizontal scan finds a potential finder pattern, this method
  15834. * "cross-checks" by scanning down vertically through the center of the possible
  15835. * finder pattern to see if the same proportion is detected.</p>
  15836. *
  15837. * @param startI row where a finder pattern was detected
  15838. * @param centerJ center of the section that appears to cross a finder pattern
  15839. * @param maxCount maximum reasonable number of modules that should be
  15840. * observed in any reading state, based on the results of the horizontal scan
  15841. * @return vertical center of finder pattern, or {@link Float#NaN} if not found
  15842. */
  15843. crossCheckVertical(startI /*int*/, centerJ /*int*/, maxCount /*int*/, originalStateCountTotal /*int*/) {
  15844. const image = this.image;
  15845. const maxI = image.getHeight();
  15846. const stateCount = this.getCrossCheckStateCount();
  15847. // Start counting up from center
  15848. let i = startI;
  15849. while (i >= 0 && image.get(centerJ, i)) {
  15850. stateCount[2]++;
  15851. i--;
  15852. }
  15853. if (i < 0) {
  15854. return NaN;
  15855. }
  15856. while (i >= 0 && !image.get(centerJ, i) && stateCount[1] <= maxCount) {
  15857. stateCount[1]++;
  15858. i--;
  15859. }
  15860. // If already too many modules in this state or ran off the edge:
  15861. if (i < 0 || stateCount[1] > maxCount) {
  15862. return NaN;
  15863. }
  15864. while (i >= 0 && image.get(centerJ, i) && stateCount[0] <= maxCount) {
  15865. stateCount[0]++;
  15866. i--;
  15867. }
  15868. if (stateCount[0] > maxCount) {
  15869. return NaN;
  15870. }
  15871. // Now also count down from center
  15872. i = startI + 1;
  15873. while (i < maxI && image.get(centerJ, i)) {
  15874. stateCount[2]++;
  15875. i++;
  15876. }
  15877. if (i === maxI) {
  15878. return NaN;
  15879. }
  15880. while (i < maxI && !image.get(centerJ, i) && stateCount[3] < maxCount) {
  15881. stateCount[3]++;
  15882. i++;
  15883. }
  15884. if (i === maxI || stateCount[3] >= maxCount) {
  15885. return NaN;
  15886. }
  15887. while (i < maxI && image.get(centerJ, i) && stateCount[4] < maxCount) {
  15888. stateCount[4]++;
  15889. i++;
  15890. }
  15891. if (stateCount[4] >= maxCount) {
  15892. return NaN;
  15893. }
  15894. // If we found a finder-pattern-like section, but its size is more than 40% different than
  15895. // the original, assume it's a false positive
  15896. const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +
  15897. stateCount[4];
  15898. if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) {
  15899. return NaN;
  15900. }
  15901. return FinderPatternFinder.foundPatternCross(stateCount) ? FinderPatternFinder.centerFromEnd(stateCount, i) : NaN;
  15902. }
  15903. /**
  15904. * <p>Like {@link #crossCheckVertical(int, int, int, int)}, and in fact is basically identical,
  15905. * except it reads horizontally instead of vertically. This is used to cross-cross
  15906. * check a vertical cross check and locate the real center of the alignment pattern.</p>
  15907. */
  15908. crossCheckHorizontal(startJ /*int*/, centerI /*int*/, maxCount /*int*/, originalStateCountTotal /*int*/) {
  15909. const image = this.image;
  15910. const maxJ = image.getWidth();
  15911. const stateCount = this.getCrossCheckStateCount();
  15912. let j = startJ;
  15913. while (j >= 0 && image.get(j, centerI)) {
  15914. stateCount[2]++;
  15915. j--;
  15916. }
  15917. if (j < 0) {
  15918. return NaN;
  15919. }
  15920. while (j >= 0 && !image.get(j, centerI) && stateCount[1] <= maxCount) {
  15921. stateCount[1]++;
  15922. j--;
  15923. }
  15924. if (j < 0 || stateCount[1] > maxCount) {
  15925. return NaN;
  15926. }
  15927. while (j >= 0 && image.get(j, centerI) && stateCount[0] <= maxCount) {
  15928. stateCount[0]++;
  15929. j--;
  15930. }
  15931. if (stateCount[0] > maxCount) {
  15932. return NaN;
  15933. }
  15934. j = startJ + 1;
  15935. while (j < maxJ && image.get(j, centerI)) {
  15936. stateCount[2]++;
  15937. j++;
  15938. }
  15939. if (j === maxJ) {
  15940. return NaN;
  15941. }
  15942. while (j < maxJ && !image.get(j, centerI) && stateCount[3] < maxCount) {
  15943. stateCount[3]++;
  15944. j++;
  15945. }
  15946. if (j === maxJ || stateCount[3] >= maxCount) {
  15947. return NaN;
  15948. }
  15949. while (j < maxJ && image.get(j, centerI) && stateCount[4] < maxCount) {
  15950. stateCount[4]++;
  15951. j++;
  15952. }
  15953. if (stateCount[4] >= maxCount) {
  15954. return NaN;
  15955. }
  15956. // If we found a finder-pattern-like section, but its size is significantly different than
  15957. // the original, assume it's a false positive
  15958. const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +
  15959. stateCount[4];
  15960. if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= originalStateCountTotal) {
  15961. return NaN;
  15962. }
  15963. return FinderPatternFinder.foundPatternCross(stateCount) ? FinderPatternFinder.centerFromEnd(stateCount, j) : NaN;
  15964. }
  15965. /**
  15966. * <p>This is called when a horizontal scan finds a possible alignment pattern. It will
  15967. * cross check with a vertical scan, and if successful, will, ah, cross-cross-check
  15968. * with another horizontal scan. This is needed primarily to locate the real horizontal
  15969. * center of the pattern in cases of extreme skew.
  15970. * And then we cross-cross-cross check with another diagonal scan.</p>
  15971. *
  15972. * <p>If that succeeds the finder pattern location is added to a list that tracks
  15973. * the number of times each location has been nearly-matched as a finder pattern.
  15974. * Each additional find is more evidence that the location is in fact a finder
  15975. * pattern center
  15976. *
  15977. * @param stateCount reading state module counts from horizontal scan
  15978. * @param i row where finder pattern may be found
  15979. * @param j end of possible finder pattern in row
  15980. * @param pureBarcode true if in "pure barcode" mode
  15981. * @return true if a finder pattern candidate was found this time
  15982. */
  15983. handlePossibleCenter(stateCount, i /*int*/, j /*int*/, pureBarcode) {
  15984. const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +
  15985. stateCount[4];
  15986. let centerJ = FinderPatternFinder.centerFromEnd(stateCount, j);
  15987. let centerI = this.crossCheckVertical(i, /*(int) */ Math.floor(centerJ), stateCount[2], stateCountTotal);
  15988. if (!isNaN(centerI)) {
  15989. // Re-cross check
  15990. centerJ = this.crossCheckHorizontal(/*(int) */ Math.floor(centerJ), /*(int) */ Math.floor(centerI), stateCount[2], stateCountTotal);
  15991. if (!isNaN(centerJ) &&
  15992. (!pureBarcode || this.crossCheckDiagonal(/*(int) */ Math.floor(centerI), /*(int) */ Math.floor(centerJ), stateCount[2], stateCountTotal))) {
  15993. const estimatedModuleSize = stateCountTotal / 7.0;
  15994. let found = false;
  15995. const possibleCenters = this.possibleCenters;
  15996. for (let index = 0, length = possibleCenters.length; index < length; index++) {
  15997. const center = possibleCenters[index];
  15998. // Look for about the same center and module size:
  15999. if (center.aboutEquals(estimatedModuleSize, centerI, centerJ)) {
  16000. possibleCenters[index] = center.combineEstimate(centerI, centerJ, estimatedModuleSize);
  16001. found = true;
  16002. break;
  16003. }
  16004. }
  16005. if (!found) {
  16006. const point = new FinderPattern(centerJ, centerI, estimatedModuleSize);
  16007. possibleCenters.push(point);
  16008. if (this.resultPointCallback !== null && this.resultPointCallback !== undefined) {
  16009. this.resultPointCallback.foundPossibleResultPoint(point);
  16010. }
  16011. }
  16012. return true;
  16013. }
  16014. }
  16015. return false;
  16016. }
  16017. /**
  16018. * @return number of rows we could safely skip during scanning, based on the first
  16019. * two finder patterns that have been located. In some cases their position will
  16020. * allow us to infer that the third pattern must lie below a certain point farther
  16021. * down in the image.
  16022. */
  16023. findRowSkip() {
  16024. const max = this.possibleCenters.length;
  16025. if (max <= 1) {
  16026. return 0;
  16027. }
  16028. let firstConfirmedCenter = null;
  16029. for (const center of this.possibleCenters) {
  16030. if (center.getCount() >= FinderPatternFinder.CENTER_QUORUM) {
  16031. if (firstConfirmedCenter == null) {
  16032. firstConfirmedCenter = center;
  16033. }
  16034. else {
  16035. // We have two confirmed centers
  16036. // How far down can we skip before resuming looking for the next
  16037. // pattern? In the worst case, only the difference between the
  16038. // difference in the x / y coordinates of the two centers.
  16039. // This is the case where you find top left last.
  16040. this.hasSkipped = true;
  16041. return /*(int) */ Math.floor((Math.abs(firstConfirmedCenter.getX() - center.getX()) -
  16042. Math.abs(firstConfirmedCenter.getY() - center.getY())) / 2);
  16043. }
  16044. }
  16045. }
  16046. return 0;
  16047. }
  16048. /**
  16049. * @return true iff we have found at least 3 finder patterns that have been detected
  16050. * at least {@link #CENTER_QUORUM} times each, and, the estimated module size of the
  16051. * candidates is "pretty similar"
  16052. */
  16053. haveMultiplyConfirmedCenters() {
  16054. let confirmedCount = 0;
  16055. let totalModuleSize = 0.0;
  16056. const max = this.possibleCenters.length;
  16057. for (const pattern of this.possibleCenters) {
  16058. if (pattern.getCount() >= FinderPatternFinder.CENTER_QUORUM) {
  16059. confirmedCount++;
  16060. totalModuleSize += pattern.getEstimatedModuleSize();
  16061. }
  16062. }
  16063. if (confirmedCount < 3) {
  16064. return false;
  16065. }
  16066. // OK, we have at least 3 confirmed centers, but, it's possible that one is a "false positive"
  16067. // and that we need to keep looking. We detect this by asking if the estimated module sizes
  16068. // vary too much. We arbitrarily say that when the total deviation from average exceeds
  16069. // 5% of the total module size estimates, it's too much.
  16070. const average = totalModuleSize / max;
  16071. let totalDeviation = 0.0;
  16072. for (const pattern of this.possibleCenters) {
  16073. totalDeviation += Math.abs(pattern.getEstimatedModuleSize() - average);
  16074. }
  16075. return totalDeviation <= 0.05 * totalModuleSize;
  16076. }
  16077. /**
  16078. * @return the 3 best {@link FinderPattern}s from our list of candidates. The "best" are
  16079. * those that have been detected at least {@link #CENTER_QUORUM} times, and whose module
  16080. * size differs from the average among those patterns the least
  16081. * @throws NotFoundException if 3 such finder patterns do not exist
  16082. */
  16083. selectBestPatterns() {
  16084. const startSize = this.possibleCenters.length;
  16085. if (startSize < 3) {
  16086. // Couldn't find enough finder patterns
  16087. throw new NotFoundException();
  16088. }
  16089. const possibleCenters = this.possibleCenters;
  16090. let average;
  16091. // Filter outlier possibilities whose module size is too different
  16092. if (startSize > 3) {
  16093. // But we can only afford to do so if we have at least 4 possibilities to choose from
  16094. let totalModuleSize = 0.0;
  16095. let square = 0.0;
  16096. for (const center of this.possibleCenters) {
  16097. const size = center.getEstimatedModuleSize();
  16098. totalModuleSize += size;
  16099. square += size * size;
  16100. }
  16101. average = totalModuleSize / startSize;
  16102. let stdDev = Math.sqrt(square / startSize - average * average);
  16103. possibleCenters.sort(
  16104. /**
  16105. * <p>Orders by furthest from average</p>
  16106. */
  16107. // FurthestFromAverageComparator implements Comparator<FinderPattern>
  16108. (center1, center2) => {
  16109. const dA = Math.abs(center2.getEstimatedModuleSize() - average);
  16110. const dB = Math.abs(center1.getEstimatedModuleSize() - average);
  16111. return dA < dB ? -1 : dA > dB ? 1 : 0;
  16112. });
  16113. const limit = Math.max(0.2 * average, stdDev);
  16114. for (let i = 0; i < possibleCenters.length && possibleCenters.length > 3; i++) {
  16115. const pattern = possibleCenters[i];
  16116. if (Math.abs(pattern.getEstimatedModuleSize() - average) > limit) {
  16117. possibleCenters.splice(i, 1);
  16118. i--;
  16119. }
  16120. }
  16121. }
  16122. if (possibleCenters.length > 3) {
  16123. // Throw away all but those first size candidate points we found.
  16124. let totalModuleSize = 0.0;
  16125. for (const possibleCenter of possibleCenters) {
  16126. totalModuleSize += possibleCenter.getEstimatedModuleSize();
  16127. }
  16128. average = totalModuleSize / possibleCenters.length;
  16129. possibleCenters.sort(
  16130. /**
  16131. * <p>Orders by {@link FinderPattern#getCount()}, descending.</p>
  16132. */
  16133. // CenterComparator implements Comparator<FinderPattern>
  16134. (center1, center2) => {
  16135. if (center2.getCount() === center1.getCount()) {
  16136. const dA = Math.abs(center2.getEstimatedModuleSize() - average);
  16137. const dB = Math.abs(center1.getEstimatedModuleSize() - average);
  16138. return dA < dB ? 1 : dA > dB ? -1 : 0;
  16139. }
  16140. else {
  16141. return center2.getCount() - center1.getCount();
  16142. }
  16143. });
  16144. possibleCenters.splice(3); // this is not realy necessary as we only return first 3 anyway
  16145. }
  16146. return [
  16147. possibleCenters[0],
  16148. possibleCenters[1],
  16149. possibleCenters[2]
  16150. ];
  16151. }
  16152. }
  16153. FinderPatternFinder.CENTER_QUORUM = 2;
  16154. FinderPatternFinder.MIN_SKIP = 3; // 1 pixel/module times 3 modules/center
  16155. FinderPatternFinder.MAX_MODULES = 57; // support up to version 10 for mobile clients
  16156. /*
  16157. * Copyright 2007 ZXing authors
  16158. *
  16159. * Licensed under the Apache License, Version 2.0 (the "License");
  16160. * you may not use this file except in compliance with the License.
  16161. * You may obtain a copy of the License at
  16162. *
  16163. * http://www.apache.org/licenses/LICENSE-2.0
  16164. *
  16165. * Unless required by applicable law or agreed to in writing, software
  16166. * distributed under the License is distributed on an "AS IS" BASIS,
  16167. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16168. * See the License for the specific language governing permissions and
  16169. * limitations under the License.
  16170. */
  16171. /*import java.util.Map;*/
  16172. /**
  16173. * <p>Encapsulates logic that can detect a QR Code in an image, even if the QR Code
  16174. * is rotated or skewed, or partially obscured.</p>
  16175. *
  16176. * @author Sean Owen
  16177. */
  16178. class Detector$1 {
  16179. constructor(image) {
  16180. this.image = image;
  16181. }
  16182. getImage() {
  16183. return this.image;
  16184. }
  16185. getResultPointCallback() {
  16186. return this.resultPointCallback;
  16187. }
  16188. /**
  16189. * <p>Detects a QR Code in an image.</p>
  16190. *
  16191. * @return {@link DetectorResult} encapsulating results of detecting a QR Code
  16192. * @throws NotFoundException if QR Code cannot be found
  16193. * @throws FormatException if a QR Code cannot be decoded
  16194. */
  16195. // public detect(): DetectorResult /*throws NotFoundException, FormatException*/ {
  16196. // return detect(null)
  16197. // }
  16198. /**
  16199. * <p>Detects a QR Code in an image.</p>
  16200. *
  16201. * @param hints optional hints to detector
  16202. * @return {@link DetectorResult} encapsulating results of detecting a QR Code
  16203. * @throws NotFoundException if QR Code cannot be found
  16204. * @throws FormatException if a QR Code cannot be decoded
  16205. */
  16206. detect(hints) {
  16207. this.resultPointCallback = (hints === null || hints === undefined) ? null :
  16208. /*(ResultPointCallback) */ hints.get(DecodeHintType$1.NEED_RESULT_POINT_CALLBACK);
  16209. const finder = new FinderPatternFinder(this.image, this.resultPointCallback);
  16210. const info = finder.find(hints);
  16211. return this.processFinderPatternInfo(info);
  16212. }
  16213. processFinderPatternInfo(info) {
  16214. const topLeft = info.getTopLeft();
  16215. const topRight = info.getTopRight();
  16216. const bottomLeft = info.getBottomLeft();
  16217. const moduleSize = this.calculateModuleSize(topLeft, topRight, bottomLeft);
  16218. if (moduleSize < 1.0) {
  16219. throw new NotFoundException('No pattern found in proccess finder.');
  16220. }
  16221. const dimension = Detector$1.computeDimension(topLeft, topRight, bottomLeft, moduleSize);
  16222. const provisionalVersion = Version.getProvisionalVersionForDimension(dimension);
  16223. const modulesBetweenFPCenters = provisionalVersion.getDimensionForVersion() - 7;
  16224. let alignmentPattern = null;
  16225. // Anything above version 1 has an alignment pattern
  16226. if (provisionalVersion.getAlignmentPatternCenters().length > 0) {
  16227. // Guess where a "bottom right" finder pattern would have been
  16228. const bottomRightX = topRight.getX() - topLeft.getX() + bottomLeft.getX();
  16229. const bottomRightY = topRight.getY() - topLeft.getY() + bottomLeft.getY();
  16230. // Estimate that alignment pattern is closer by 3 modules
  16231. // from "bottom right" to known top left location
  16232. const correctionToTopLeft = 1.0 - 3.0 / modulesBetweenFPCenters;
  16233. const estAlignmentX = /*(int) */ Math.floor(topLeft.getX() + correctionToTopLeft * (bottomRightX - topLeft.getX()));
  16234. const estAlignmentY = /*(int) */ Math.floor(topLeft.getY() + correctionToTopLeft * (bottomRightY - topLeft.getY()));
  16235. // Kind of arbitrary -- expand search radius before giving up
  16236. for (let i = 4; i <= 16; i <<= 1) {
  16237. try {
  16238. alignmentPattern = this.findAlignmentInRegion(moduleSize, estAlignmentX, estAlignmentY, i);
  16239. break;
  16240. }
  16241. catch (re /*NotFoundException*/) {
  16242. if (!(re instanceof NotFoundException)) {
  16243. throw re;
  16244. }
  16245. // try next round
  16246. }
  16247. }
  16248. // If we didn't find alignment pattern... well try anyway without it
  16249. }
  16250. const transform = Detector$1.createTransform(topLeft, topRight, bottomLeft, alignmentPattern, dimension);
  16251. const bits = Detector$1.sampleGrid(this.image, transform, dimension);
  16252. let points;
  16253. if (alignmentPattern === null) {
  16254. points = [bottomLeft, topLeft, topRight];
  16255. }
  16256. else {
  16257. points = [bottomLeft, topLeft, topRight, alignmentPattern];
  16258. }
  16259. return new DetectorResult(bits, points);
  16260. }
  16261. static createTransform(topLeft, topRight, bottomLeft, alignmentPattern, dimension /*int*/) {
  16262. const dimMinusThree = dimension - 3.5;
  16263. let bottomRightX; /*float*/
  16264. let bottomRightY; /*float*/
  16265. let sourceBottomRightX; /*float*/
  16266. let sourceBottomRightY; /*float*/
  16267. if (alignmentPattern !== null) {
  16268. bottomRightX = alignmentPattern.getX();
  16269. bottomRightY = alignmentPattern.getY();
  16270. sourceBottomRightX = dimMinusThree - 3.0;
  16271. sourceBottomRightY = sourceBottomRightX;
  16272. }
  16273. else {
  16274. // Don't have an alignment pattern, just make up the bottom-right point
  16275. bottomRightX = (topRight.getX() - topLeft.getX()) + bottomLeft.getX();
  16276. bottomRightY = (topRight.getY() - topLeft.getY()) + bottomLeft.getY();
  16277. sourceBottomRightX = dimMinusThree;
  16278. sourceBottomRightY = dimMinusThree;
  16279. }
  16280. return PerspectiveTransform.quadrilateralToQuadrilateral(3.5, 3.5, dimMinusThree, 3.5, sourceBottomRightX, sourceBottomRightY, 3.5, dimMinusThree, topLeft.getX(), topLeft.getY(), topRight.getX(), topRight.getY(), bottomRightX, bottomRightY, bottomLeft.getX(), bottomLeft.getY());
  16281. }
  16282. static sampleGrid(image, transform, dimension /*int*/) {
  16283. const sampler = GridSamplerInstance.getInstance();
  16284. return sampler.sampleGridWithTransform(image, dimension, dimension, transform);
  16285. }
  16286. /**
  16287. * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
  16288. * of the finder patterns and estimated module size.</p>
  16289. */
  16290. static computeDimension(topLeft, topRight, bottomLeft, moduleSize /*float*/) {
  16291. const tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
  16292. const tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
  16293. let dimension = Math.floor((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
  16294. switch (dimension & 0x03) { // mod 4
  16295. case 0:
  16296. dimension++;
  16297. break;
  16298. // 1? do nothing
  16299. case 2:
  16300. dimension--;
  16301. break;
  16302. case 3:
  16303. throw new NotFoundException('Dimensions could be not found.');
  16304. }
  16305. return dimension;
  16306. }
  16307. /**
  16308. * <p>Computes an average estimated module size based on estimated derived from the positions
  16309. * of the three finder patterns.</p>
  16310. *
  16311. * @param topLeft detected top-left finder pattern center
  16312. * @param topRight detected top-right finder pattern center
  16313. * @param bottomLeft detected bottom-left finder pattern center
  16314. * @return estimated module size
  16315. */
  16316. calculateModuleSize(topLeft, topRight, bottomLeft) {
  16317. // Take the average
  16318. return (this.calculateModuleSizeOneWay(topLeft, topRight) +
  16319. this.calculateModuleSizeOneWay(topLeft, bottomLeft)) / 2.0;
  16320. }
  16321. /**
  16322. * <p>Estimates module size based on two finder patterns -- it uses
  16323. * {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the
  16324. * width of each, measuring along the axis between their centers.</p>
  16325. */
  16326. calculateModuleSizeOneWay(pattern, otherPattern) {
  16327. const moduleSizeEst1 = this.sizeOfBlackWhiteBlackRunBothWays(/*(int) */ Math.floor(pattern.getX()),
  16328. /*(int) */ Math.floor(pattern.getY()),
  16329. /*(int) */ Math.floor(otherPattern.getX()),
  16330. /*(int) */ Math.floor(otherPattern.getY()));
  16331. const moduleSizeEst2 = this.sizeOfBlackWhiteBlackRunBothWays(/*(int) */ Math.floor(otherPattern.getX()),
  16332. /*(int) */ Math.floor(otherPattern.getY()),
  16333. /*(int) */ Math.floor(pattern.getX()),
  16334. /*(int) */ Math.floor(pattern.getY()));
  16335. if (isNaN(moduleSizeEst1)) {
  16336. return moduleSizeEst2 / 7.0;
  16337. }
  16338. if (isNaN(moduleSizeEst2)) {
  16339. return moduleSizeEst1 / 7.0;
  16340. }
  16341. // Average them, and divide by 7 since we've counted the width of 3 black modules,
  16342. // and 1 white and 1 black module on either side. Ergo, divide sum by 14.
  16343. return (moduleSizeEst1 + moduleSizeEst2) / 14.0;
  16344. }
  16345. /**
  16346. * See {@link #sizeOfBlackWhiteBlackRun(int, int, int, int)}; computes the total width of
  16347. * a finder pattern by looking for a black-white-black run from the center in the direction
  16348. * of another point (another finder pattern center), and in the opposite direction too.
  16349. */
  16350. sizeOfBlackWhiteBlackRunBothWays(fromX /*int*/, fromY /*int*/, toX /*int*/, toY /*int*/) {
  16351. let result = this.sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY);
  16352. // Now count other way -- don't run off image though of course
  16353. let scale = 1.0;
  16354. let otherToX = fromX - (toX - fromX);
  16355. if (otherToX < 0) {
  16356. scale = fromX / /*(float) */ (fromX - otherToX);
  16357. otherToX = 0;
  16358. }
  16359. else if (otherToX >= this.image.getWidth()) {
  16360. scale = (this.image.getWidth() - 1 - fromX) / /*(float) */ (otherToX - fromX);
  16361. otherToX = this.image.getWidth() - 1;
  16362. }
  16363. let otherToY = /*(int) */ Math.floor(fromY - (toY - fromY) * scale);
  16364. scale = 1.0;
  16365. if (otherToY < 0) {
  16366. scale = fromY / /*(float) */ (fromY - otherToY);
  16367. otherToY = 0;
  16368. }
  16369. else if (otherToY >= this.image.getHeight()) {
  16370. scale = (this.image.getHeight() - 1 - fromY) / /*(float) */ (otherToY - fromY);
  16371. otherToY = this.image.getHeight() - 1;
  16372. }
  16373. otherToX = /*(int) */ Math.floor(fromX + (otherToX - fromX) * scale);
  16374. result += this.sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);
  16375. // Middle pixel is double-counted this way; subtract 1
  16376. return result - 1.0;
  16377. }
  16378. /**
  16379. * <p>This method traces a line from a point in the image, in the direction towards another point.
  16380. * It begins in a black region, and keeps going until it finds white, then black, then white again.
  16381. * It reports the distance from the start to this point.</p>
  16382. *
  16383. * <p>This is used when figuring out how wide a finder pattern is, when the finder pattern
  16384. * may be skewed or rotated.</p>
  16385. */
  16386. sizeOfBlackWhiteBlackRun(fromX /*int*/, fromY /*int*/, toX /*int*/, toY /*int*/) {
  16387. // Mild variant of Bresenham's algorithm
  16388. // see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  16389. const steep = Math.abs(toY - fromY) > Math.abs(toX - fromX);
  16390. if (steep) {
  16391. let temp = fromX;
  16392. fromX = fromY;
  16393. fromY = temp;
  16394. temp = toX;
  16395. toX = toY;
  16396. toY = temp;
  16397. }
  16398. const dx = Math.abs(toX - fromX);
  16399. const dy = Math.abs(toY - fromY);
  16400. let error = -dx / 2;
  16401. const xstep = fromX < toX ? 1 : -1;
  16402. const ystep = fromY < toY ? 1 : -1;
  16403. // In black pixels, looking for white, first or second time.
  16404. let state = 0;
  16405. // Loop up until x == toX, but not beyond
  16406. const xLimit = toX + xstep;
  16407. for (let x = fromX, y = fromY; x !== xLimit; x += xstep) {
  16408. const realX = steep ? y : x;
  16409. const realY = steep ? x : y;
  16410. // Does current pixel mean we have moved white to black or vice versa?
  16411. // Scanning black in state 0,2 and white in state 1, so if we find the wrong
  16412. // color, advance to next state or end if we are in state 2 already
  16413. if ((state === 1) === this.image.get(realX, realY)) {
  16414. if (state === 2) {
  16415. return MathUtils.distance(x, y, fromX, fromY);
  16416. }
  16417. state++;
  16418. }
  16419. error += dy;
  16420. if (error > 0) {
  16421. if (y === toY) {
  16422. break;
  16423. }
  16424. y += ystep;
  16425. error -= dx;
  16426. }
  16427. }
  16428. // Found black-white-black; give the benefit of the doubt that the next pixel outside the image
  16429. // is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a
  16430. // small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
  16431. if (state === 2) {
  16432. return MathUtils.distance(toX + xstep, toY, fromX, fromY);
  16433. }
  16434. // else we didn't find even black-white-black; no estimate is really possible
  16435. return NaN;
  16436. }
  16437. /**
  16438. * <p>Attempts to locate an alignment pattern in a limited region of the image, which is
  16439. * guessed to contain it. This method uses {@link AlignmentPattern}.</p>
  16440. *
  16441. * @param overallEstModuleSize estimated module size so far
  16442. * @param estAlignmentX x coordinate of center of area probably containing alignment pattern
  16443. * @param estAlignmentY y coordinate of above
  16444. * @param allowanceFactor number of pixels in all directions to search from the center
  16445. * @return {@link AlignmentPattern} if found, or null otherwise
  16446. * @throws NotFoundException if an unexpected error occurs during detection
  16447. */
  16448. findAlignmentInRegion(overallEstModuleSize /*float*/, estAlignmentX /*int*/, estAlignmentY /*int*/, allowanceFactor /*float*/) {
  16449. // Look for an alignment pattern (3 modules in size) around where it
  16450. // should be
  16451. const allowance = /*(int) */ Math.floor(allowanceFactor * overallEstModuleSize);
  16452. const alignmentAreaLeftX = Math.max(0, estAlignmentX - allowance);
  16453. const alignmentAreaRightX = Math.min(this.image.getWidth() - 1, estAlignmentX + allowance);
  16454. if (alignmentAreaRightX - alignmentAreaLeftX < overallEstModuleSize * 3) {
  16455. throw new NotFoundException('Alignment top exceeds estimated module size.');
  16456. }
  16457. const alignmentAreaTopY = Math.max(0, estAlignmentY - allowance);
  16458. const alignmentAreaBottomY = Math.min(this.image.getHeight() - 1, estAlignmentY + allowance);
  16459. if (alignmentAreaBottomY - alignmentAreaTopY < overallEstModuleSize * 3) {
  16460. throw new NotFoundException('Alignment bottom exceeds estimated module size.');
  16461. }
  16462. const alignmentFinder = new AlignmentPatternFinder(this.image, alignmentAreaLeftX, alignmentAreaTopY, alignmentAreaRightX - alignmentAreaLeftX, alignmentAreaBottomY - alignmentAreaTopY, overallEstModuleSize, this.resultPointCallback);
  16463. return alignmentFinder.find();
  16464. }
  16465. }
  16466. /*
  16467. * Copyright 2007 ZXing authors
  16468. *
  16469. * Licensed under the Apache License, Version 2.0 (the "License");
  16470. * you may not use this file except in compliance with the License.
  16471. * You may obtain a copy of the License at
  16472. *
  16473. * http://www.apache.org/licenses/LICENSE-2.0
  16474. *
  16475. * Unless required by applicable law or agreed to in writing, software
  16476. * distributed under the License is distributed on an "AS IS" BASIS,
  16477. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16478. * See the License for the specific language governing permissions and
  16479. * limitations under the License.
  16480. */
  16481. /*import java.util.List;*/
  16482. /*import java.util.Map;*/
  16483. /**
  16484. * This implementation can detect and decode QR Codes in an image.
  16485. *
  16486. * @author Sean Owen
  16487. */
  16488. class QRCodeReader {
  16489. constructor() {
  16490. this.decoder = new Decoder();
  16491. }
  16492. getDecoder() {
  16493. return this.decoder;
  16494. }
  16495. /**
  16496. * Locates and decodes a QR code in an image.
  16497. *
  16498. * @return a representing: string the content encoded by the QR code
  16499. * @throws NotFoundException if a QR code cannot be found
  16500. * @throws FormatException if a QR code cannot be decoded
  16501. * @throws ChecksumException if error correction fails
  16502. */
  16503. /*@Override*/
  16504. // public decode(image: BinaryBitmap): Result /*throws NotFoundException, ChecksumException, FormatException */ {
  16505. // return this.decode(image, null)
  16506. // }
  16507. /*@Override*/
  16508. decode(image, hints) {
  16509. let decoderResult;
  16510. let points;
  16511. if (hints !== undefined && hints !== null && undefined !== hints.get(DecodeHintType$1.PURE_BARCODE)) {
  16512. const bits = QRCodeReader.extractPureBits(image.getBlackMatrix());
  16513. decoderResult = this.decoder.decodeBitMatrix(bits, hints);
  16514. points = QRCodeReader.NO_POINTS;
  16515. }
  16516. else {
  16517. const detectorResult = new Detector$1(image.getBlackMatrix()).detect(hints);
  16518. decoderResult = this.decoder.decodeBitMatrix(detectorResult.getBits(), hints);
  16519. points = detectorResult.getPoints();
  16520. }
  16521. // If the code was mirrored: swap the bottom-left and the top-right points.
  16522. if (decoderResult.getOther() instanceof QRCodeDecoderMetaData) {
  16523. decoderResult.getOther().applyMirroredCorrection(points);
  16524. }
  16525. const result = new Result$1(decoderResult.getText(), decoderResult.getRawBytes(), undefined, points, BarcodeFormat$1.QR_CODE, undefined);
  16526. const byteSegments = decoderResult.getByteSegments();
  16527. if (byteSegments !== null) {
  16528. result.putMetadata(ResultMetadataType$1.BYTE_SEGMENTS, byteSegments);
  16529. }
  16530. const ecLevel = decoderResult.getECLevel();
  16531. if (ecLevel !== null) {
  16532. result.putMetadata(ResultMetadataType$1.ERROR_CORRECTION_LEVEL, ecLevel);
  16533. }
  16534. if (decoderResult.hasStructuredAppend()) {
  16535. result.putMetadata(ResultMetadataType$1.STRUCTURED_APPEND_SEQUENCE, decoderResult.getStructuredAppendSequenceNumber());
  16536. result.putMetadata(ResultMetadataType$1.STRUCTURED_APPEND_PARITY, decoderResult.getStructuredAppendParity());
  16537. }
  16538. return result;
  16539. }
  16540. /*@Override*/
  16541. reset() {
  16542. // do nothing
  16543. }
  16544. /**
  16545. * This method detects a code in a "pure" image -- that is, pure monochrome image
  16546. * which contains only an unrotated, unskewed, image of a code, with some white border
  16547. * around it. This is a specialized method that works exceptionally fast in this special
  16548. * case.
  16549. *
  16550. * @see com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix)
  16551. */
  16552. static extractPureBits(image) {
  16553. const leftTopBlack = image.getTopLeftOnBit();
  16554. const rightBottomBlack = image.getBottomRightOnBit();
  16555. if (leftTopBlack === null || rightBottomBlack === null) {
  16556. throw new NotFoundException();
  16557. }
  16558. const moduleSize = this.moduleSize(leftTopBlack, image);
  16559. let top = leftTopBlack[1];
  16560. let bottom = rightBottomBlack[1];
  16561. let left = leftTopBlack[0];
  16562. let right = rightBottomBlack[0];
  16563. // Sanity check!
  16564. if (left >= right || top >= bottom) {
  16565. throw new NotFoundException();
  16566. }
  16567. if (bottom - top !== right - left) {
  16568. // Special case, where bottom-right module wasn't black so we found something else in the last row
  16569. // Assume it's a square, so use height as the width
  16570. right = left + (bottom - top);
  16571. if (right >= image.getWidth()) {
  16572. // Abort if that would not make sense -- off image
  16573. throw new NotFoundException();
  16574. }
  16575. }
  16576. const matrixWidth = Math.round((right - left + 1) / moduleSize);
  16577. const matrixHeight = Math.round((bottom - top + 1) / moduleSize);
  16578. if (matrixWidth <= 0 || matrixHeight <= 0) {
  16579. throw new NotFoundException();
  16580. }
  16581. if (matrixHeight !== matrixWidth) {
  16582. // Only possibly decode square regions
  16583. throw new NotFoundException();
  16584. }
  16585. // Push in the "border" by half the module width so that we start
  16586. // sampling in the middle of the module. Just in case the image is a
  16587. // little off, this will help recover.
  16588. const nudge = /*(int) */ Math.floor(moduleSize / 2.0);
  16589. top += nudge;
  16590. left += nudge;
  16591. // But careful that this does not sample off the edge
  16592. // "right" is the farthest-right valid pixel location -- right+1 is not necessarily
  16593. // This is positive by how much the inner x loop below would be too large
  16594. const nudgedTooFarRight = left + /*(int) */ Math.floor((matrixWidth - 1) * moduleSize) - right;
  16595. if (nudgedTooFarRight > 0) {
  16596. if (nudgedTooFarRight > nudge) {
  16597. // Neither way fits; abort
  16598. throw new NotFoundException();
  16599. }
  16600. left -= nudgedTooFarRight;
  16601. }
  16602. // See logic above
  16603. const nudgedTooFarDown = top + /*(int) */ Math.floor((matrixHeight - 1) * moduleSize) - bottom;
  16604. if (nudgedTooFarDown > 0) {
  16605. if (nudgedTooFarDown > nudge) {
  16606. // Neither way fits; abort
  16607. throw new NotFoundException();
  16608. }
  16609. top -= nudgedTooFarDown;
  16610. }
  16611. // Now just read off the bits
  16612. const bits = new BitMatrix(matrixWidth, matrixHeight);
  16613. for (let y = 0; y < matrixHeight; y++) {
  16614. const iOffset = top + /*(int) */ Math.floor(y * moduleSize);
  16615. for (let x = 0; x < matrixWidth; x++) {
  16616. if (image.get(left + /*(int) */ Math.floor(x * moduleSize), iOffset)) {
  16617. bits.set(x, y);
  16618. }
  16619. }
  16620. }
  16621. return bits;
  16622. }
  16623. static moduleSize(leftTopBlack, image) {
  16624. const height = image.getHeight();
  16625. const width = image.getWidth();
  16626. let x = leftTopBlack[0];
  16627. let y = leftTopBlack[1];
  16628. let inBlack = true;
  16629. let transitions = 0;
  16630. while (x < width && y < height) {
  16631. if (inBlack !== image.get(x, y)) {
  16632. if (++transitions === 5) {
  16633. break;
  16634. }
  16635. inBlack = !inBlack;
  16636. }
  16637. x++;
  16638. y++;
  16639. }
  16640. if (x === width || y === height) {
  16641. throw new NotFoundException();
  16642. }
  16643. return (x - leftTopBlack[0]) / 7.0;
  16644. }
  16645. }
  16646. QRCodeReader.NO_POINTS = new Array();
  16647. /*
  16648. * Copyright 2009 ZXing authors
  16649. *
  16650. * Licensed under the Apache License, Version 2.0 (the "License");
  16651. * you may not use this file except in compliance with the License.
  16652. * You may obtain a copy of the License at
  16653. *
  16654. * http://www.apache.org/licenses/LICENSE-2.0
  16655. *
  16656. * Unless required by applicable law or agreed to in writing, software
  16657. * distributed under the License is distributed on an "AS IS" BASIS,
  16658. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16659. * See the License for the specific language governing permissions and
  16660. * limitations under the License.
  16661. */
  16662. /**
  16663. * @author SITA Lab (kevin.osullivan@sita.aero)
  16664. * @author Guenther Grau
  16665. */
  16666. /*public final*/ class PDF417Common {
  16667. PDF417Common() {
  16668. }
  16669. /**
  16670. * @param moduleBitCount values to sum
  16671. * @return sum of values
  16672. * @deprecated call {@link MathUtils#sum(int[])}
  16673. */
  16674. // @Deprecated
  16675. static getBitCountSum(moduleBitCount) {
  16676. return MathUtils.sum(moduleBitCount);
  16677. }
  16678. static toIntArray(list) {
  16679. if (list == null || !list.length) {
  16680. return PDF417Common.EMPTY_INT_ARRAY;
  16681. }
  16682. const result = new Int32Array(list.length);
  16683. let i = 0;
  16684. for (const integer of list) {
  16685. result[i++] = integer;
  16686. }
  16687. return result;
  16688. }
  16689. /**
  16690. * @param symbol encoded symbol to translate to a codeword
  16691. * @return the codeword corresponding to the symbol.
  16692. */
  16693. static getCodeword(symbol /*int*/) {
  16694. const i = Arrays.binarySearch(PDF417Common.SYMBOL_TABLE, symbol & 0x3FFFF);
  16695. if (i < 0) {
  16696. return -1;
  16697. }
  16698. return (PDF417Common.CODEWORD_TABLE[i] - 1) % PDF417Common.NUMBER_OF_CODEWORDS;
  16699. }
  16700. }
  16701. PDF417Common.NUMBER_OF_CODEWORDS = 929;
  16702. // Maximum Codewords (Data + Error).
  16703. PDF417Common.MAX_CODEWORDS_IN_BARCODE = PDF417Common.NUMBER_OF_CODEWORDS - 1;
  16704. PDF417Common.MIN_ROWS_IN_BARCODE = 3;
  16705. PDF417Common.MAX_ROWS_IN_BARCODE = 90;
  16706. // One left row indication column + max 30 data columns + one right row indicator column
  16707. // public static /*final*/ MAX_CODEWORDS_IN_ROW: /*int*/ number = 32;
  16708. PDF417Common.MODULES_IN_CODEWORD = 17;
  16709. PDF417Common.MODULES_IN_STOP_PATTERN = 18;
  16710. PDF417Common.BARS_IN_MODULE = 8;
  16711. PDF417Common.EMPTY_INT_ARRAY = new Int32Array([]);
  16712. /**
  16713. * The sorted table of all possible symbols. Extracted from the PDF417
  16714. * specification. The index of a symbol in this table corresponds to the
  16715. * index into the codeword table.
  16716. */
  16717. PDF417Common.SYMBOL_TABLE = Int32Array.from([
  16718. 0x1025e, 0x1027a, 0x1029e, 0x102bc, 0x102f2, 0x102f4, 0x1032e, 0x1034e, 0x1035c, 0x10396, 0x103a6, 0x103ac,
  16719. 0x10422, 0x10428, 0x10436, 0x10442, 0x10444, 0x10448, 0x10450, 0x1045e, 0x10466, 0x1046c, 0x1047a, 0x10482,
  16720. 0x1049e, 0x104a0, 0x104bc, 0x104c6, 0x104d8, 0x104ee, 0x104f2, 0x104f4, 0x10504, 0x10508, 0x10510, 0x1051e,
  16721. 0x10520, 0x1053c, 0x10540, 0x10578, 0x10586, 0x1058c, 0x10598, 0x105b0, 0x105be, 0x105ce, 0x105dc, 0x105e2,
  16722. 0x105e4, 0x105e8, 0x105f6, 0x1062e, 0x1064e, 0x1065c, 0x1068e, 0x1069c, 0x106b8, 0x106de, 0x106fa, 0x10716,
  16723. 0x10726, 0x1072c, 0x10746, 0x1074c, 0x10758, 0x1076e, 0x10792, 0x10794, 0x107a2, 0x107a4, 0x107a8, 0x107b6,
  16724. 0x10822, 0x10828, 0x10842, 0x10848, 0x10850, 0x1085e, 0x10866, 0x1086c, 0x1087a, 0x10882, 0x10884, 0x10890,
  16725. 0x1089e, 0x108a0, 0x108bc, 0x108c6, 0x108cc, 0x108d8, 0x108ee, 0x108f2, 0x108f4, 0x10902, 0x10908, 0x1091e,
  16726. 0x10920, 0x1093c, 0x10940, 0x10978, 0x10986, 0x10998, 0x109b0, 0x109be, 0x109ce, 0x109dc, 0x109e2, 0x109e4,
  16727. 0x109e8, 0x109f6, 0x10a08, 0x10a10, 0x10a1e, 0x10a20, 0x10a3c, 0x10a40, 0x10a78, 0x10af0, 0x10b06, 0x10b0c,
  16728. 0x10b18, 0x10b30, 0x10b3e, 0x10b60, 0x10b7c, 0x10b8e, 0x10b9c, 0x10bb8, 0x10bc2, 0x10bc4, 0x10bc8, 0x10bd0,
  16729. 0x10bde, 0x10be6, 0x10bec, 0x10c2e, 0x10c4e, 0x10c5c, 0x10c62, 0x10c64, 0x10c68, 0x10c76, 0x10c8e, 0x10c9c,
  16730. 0x10cb8, 0x10cc2, 0x10cc4, 0x10cc8, 0x10cd0, 0x10cde, 0x10ce6, 0x10cec, 0x10cfa, 0x10d0e, 0x10d1c, 0x10d38,
  16731. 0x10d70, 0x10d7e, 0x10d82, 0x10d84, 0x10d88, 0x10d90, 0x10d9e, 0x10da0, 0x10dbc, 0x10dc6, 0x10dcc, 0x10dd8,
  16732. 0x10dee, 0x10df2, 0x10df4, 0x10e16, 0x10e26, 0x10e2c, 0x10e46, 0x10e58, 0x10e6e, 0x10e86, 0x10e8c, 0x10e98,
  16733. 0x10eb0, 0x10ebe, 0x10ece, 0x10edc, 0x10f0a, 0x10f12, 0x10f14, 0x10f22, 0x10f28, 0x10f36, 0x10f42, 0x10f44,
  16734. 0x10f48, 0x10f50, 0x10f5e, 0x10f66, 0x10f6c, 0x10fb2, 0x10fb4, 0x11022, 0x11028, 0x11042, 0x11048, 0x11050,
  16735. 0x1105e, 0x1107a, 0x11082, 0x11084, 0x11090, 0x1109e, 0x110a0, 0x110bc, 0x110c6, 0x110cc, 0x110d8, 0x110ee,
  16736. 0x110f2, 0x110f4, 0x11102, 0x1111e, 0x11120, 0x1113c, 0x11140, 0x11178, 0x11186, 0x11198, 0x111b0, 0x111be,
  16737. 0x111ce, 0x111dc, 0x111e2, 0x111e4, 0x111e8, 0x111f6, 0x11208, 0x1121e, 0x11220, 0x11278, 0x112f0, 0x1130c,
  16738. 0x11330, 0x1133e, 0x11360, 0x1137c, 0x1138e, 0x1139c, 0x113b8, 0x113c2, 0x113c8, 0x113d0, 0x113de, 0x113e6,
  16739. 0x113ec, 0x11408, 0x11410, 0x1141e, 0x11420, 0x1143c, 0x11440, 0x11478, 0x114f0, 0x115e0, 0x1160c, 0x11618,
  16740. 0x11630, 0x1163e, 0x11660, 0x1167c, 0x116c0, 0x116f8, 0x1171c, 0x11738, 0x11770, 0x1177e, 0x11782, 0x11784,
  16741. 0x11788, 0x11790, 0x1179e, 0x117a0, 0x117bc, 0x117c6, 0x117cc, 0x117d8, 0x117ee, 0x1182e, 0x11834, 0x1184e,
  16742. 0x1185c, 0x11862, 0x11864, 0x11868, 0x11876, 0x1188e, 0x1189c, 0x118b8, 0x118c2, 0x118c8, 0x118d0, 0x118de,
  16743. 0x118e6, 0x118ec, 0x118fa, 0x1190e, 0x1191c, 0x11938, 0x11970, 0x1197e, 0x11982, 0x11984, 0x11990, 0x1199e,
  16744. 0x119a0, 0x119bc, 0x119c6, 0x119cc, 0x119d8, 0x119ee, 0x119f2, 0x119f4, 0x11a0e, 0x11a1c, 0x11a38, 0x11a70,
  16745. 0x11a7e, 0x11ae0, 0x11afc, 0x11b08, 0x11b10, 0x11b1e, 0x11b20, 0x11b3c, 0x11b40, 0x11b78, 0x11b8c, 0x11b98,
  16746. 0x11bb0, 0x11bbe, 0x11bce, 0x11bdc, 0x11be2, 0x11be4, 0x11be8, 0x11bf6, 0x11c16, 0x11c26, 0x11c2c, 0x11c46,
  16747. 0x11c4c, 0x11c58, 0x11c6e, 0x11c86, 0x11c98, 0x11cb0, 0x11cbe, 0x11cce, 0x11cdc, 0x11ce2, 0x11ce4, 0x11ce8,
  16748. 0x11cf6, 0x11d06, 0x11d0c, 0x11d18, 0x11d30, 0x11d3e, 0x11d60, 0x11d7c, 0x11d8e, 0x11d9c, 0x11db8, 0x11dc4,
  16749. 0x11dc8, 0x11dd0, 0x11dde, 0x11de6, 0x11dec, 0x11dfa, 0x11e0a, 0x11e12, 0x11e14, 0x11e22, 0x11e24, 0x11e28,
  16750. 0x11e36, 0x11e42, 0x11e44, 0x11e50, 0x11e5e, 0x11e66, 0x11e6c, 0x11e82, 0x11e84, 0x11e88, 0x11e90, 0x11e9e,
  16751. 0x11ea0, 0x11ebc, 0x11ec6, 0x11ecc, 0x11ed8, 0x11eee, 0x11f1a, 0x11f2e, 0x11f32, 0x11f34, 0x11f4e, 0x11f5c,
  16752. 0x11f62, 0x11f64, 0x11f68, 0x11f76, 0x12048, 0x1205e, 0x12082, 0x12084, 0x12090, 0x1209e, 0x120a0, 0x120bc,
  16753. 0x120d8, 0x120f2, 0x120f4, 0x12108, 0x1211e, 0x12120, 0x1213c, 0x12140, 0x12178, 0x12186, 0x12198, 0x121b0,
  16754. 0x121be, 0x121e2, 0x121e4, 0x121e8, 0x121f6, 0x12204, 0x12210, 0x1221e, 0x12220, 0x12278, 0x122f0, 0x12306,
  16755. 0x1230c, 0x12330, 0x1233e, 0x12360, 0x1237c, 0x1238e, 0x1239c, 0x123b8, 0x123c2, 0x123c8, 0x123d0, 0x123e6,
  16756. 0x123ec, 0x1241e, 0x12420, 0x1243c, 0x124f0, 0x125e0, 0x12618, 0x1263e, 0x12660, 0x1267c, 0x126c0, 0x126f8,
  16757. 0x12738, 0x12770, 0x1277e, 0x12782, 0x12784, 0x12790, 0x1279e, 0x127a0, 0x127bc, 0x127c6, 0x127cc, 0x127d8,
  16758. 0x127ee, 0x12820, 0x1283c, 0x12840, 0x12878, 0x128f0, 0x129e0, 0x12bc0, 0x12c18, 0x12c30, 0x12c3e, 0x12c60,
  16759. 0x12c7c, 0x12cc0, 0x12cf8, 0x12df0, 0x12e1c, 0x12e38, 0x12e70, 0x12e7e, 0x12ee0, 0x12efc, 0x12f04, 0x12f08,
  16760. 0x12f10, 0x12f20, 0x12f3c, 0x12f40, 0x12f78, 0x12f86, 0x12f8c, 0x12f98, 0x12fb0, 0x12fbe, 0x12fce, 0x12fdc,
  16761. 0x1302e, 0x1304e, 0x1305c, 0x13062, 0x13068, 0x1308e, 0x1309c, 0x130b8, 0x130c2, 0x130c8, 0x130d0, 0x130de,
  16762. 0x130ec, 0x130fa, 0x1310e, 0x13138, 0x13170, 0x1317e, 0x13182, 0x13184, 0x13190, 0x1319e, 0x131a0, 0x131bc,
  16763. 0x131c6, 0x131cc, 0x131d8, 0x131f2, 0x131f4, 0x1320e, 0x1321c, 0x13270, 0x1327e, 0x132e0, 0x132fc, 0x13308,
  16764. 0x1331e, 0x13320, 0x1333c, 0x13340, 0x13378, 0x13386, 0x13398, 0x133b0, 0x133be, 0x133ce, 0x133dc, 0x133e2,
  16765. 0x133e4, 0x133e8, 0x133f6, 0x1340e, 0x1341c, 0x13438, 0x13470, 0x1347e, 0x134e0, 0x134fc, 0x135c0, 0x135f8,
  16766. 0x13608, 0x13610, 0x1361e, 0x13620, 0x1363c, 0x13640, 0x13678, 0x136f0, 0x1370c, 0x13718, 0x13730, 0x1373e,
  16767. 0x13760, 0x1377c, 0x1379c, 0x137b8, 0x137c2, 0x137c4, 0x137c8, 0x137d0, 0x137de, 0x137e6, 0x137ec, 0x13816,
  16768. 0x13826, 0x1382c, 0x13846, 0x1384c, 0x13858, 0x1386e, 0x13874, 0x13886, 0x13898, 0x138b0, 0x138be, 0x138ce,
  16769. 0x138dc, 0x138e2, 0x138e4, 0x138e8, 0x13906, 0x1390c, 0x13930, 0x1393e, 0x13960, 0x1397c, 0x1398e, 0x1399c,
  16770. 0x139b8, 0x139c8, 0x139d0, 0x139de, 0x139e6, 0x139ec, 0x139fa, 0x13a06, 0x13a0c, 0x13a18, 0x13a30, 0x13a3e,
  16771. 0x13a60, 0x13a7c, 0x13ac0, 0x13af8, 0x13b0e, 0x13b1c, 0x13b38, 0x13b70, 0x13b7e, 0x13b88, 0x13b90, 0x13b9e,
  16772. 0x13ba0, 0x13bbc, 0x13bcc, 0x13bd8, 0x13bee, 0x13bf2, 0x13bf4, 0x13c12, 0x13c14, 0x13c22, 0x13c24, 0x13c28,
  16773. 0x13c36, 0x13c42, 0x13c48, 0x13c50, 0x13c5e, 0x13c66, 0x13c6c, 0x13c82, 0x13c84, 0x13c90, 0x13c9e, 0x13ca0,
  16774. 0x13cbc, 0x13cc6, 0x13ccc, 0x13cd8, 0x13cee, 0x13d02, 0x13d04, 0x13d08, 0x13d10, 0x13d1e, 0x13d20, 0x13d3c,
  16775. 0x13d40, 0x13d78, 0x13d86, 0x13d8c, 0x13d98, 0x13db0, 0x13dbe, 0x13dce, 0x13ddc, 0x13de4, 0x13de8, 0x13df6,
  16776. 0x13e1a, 0x13e2e, 0x13e32, 0x13e34, 0x13e4e, 0x13e5c, 0x13e62, 0x13e64, 0x13e68, 0x13e76, 0x13e8e, 0x13e9c,
  16777. 0x13eb8, 0x13ec2, 0x13ec4, 0x13ec8, 0x13ed0, 0x13ede, 0x13ee6, 0x13eec, 0x13f26, 0x13f2c, 0x13f3a, 0x13f46,
  16778. 0x13f4c, 0x13f58, 0x13f6e, 0x13f72, 0x13f74, 0x14082, 0x1409e, 0x140a0, 0x140bc, 0x14104, 0x14108, 0x14110,
  16779. 0x1411e, 0x14120, 0x1413c, 0x14140, 0x14178, 0x1418c, 0x14198, 0x141b0, 0x141be, 0x141e2, 0x141e4, 0x141e8,
  16780. 0x14208, 0x14210, 0x1421e, 0x14220, 0x1423c, 0x14240, 0x14278, 0x142f0, 0x14306, 0x1430c, 0x14318, 0x14330,
  16781. 0x1433e, 0x14360, 0x1437c, 0x1438e, 0x143c2, 0x143c4, 0x143c8, 0x143d0, 0x143e6, 0x143ec, 0x14408, 0x14410,
  16782. 0x1441e, 0x14420, 0x1443c, 0x14440, 0x14478, 0x144f0, 0x145e0, 0x1460c, 0x14618, 0x14630, 0x1463e, 0x14660,
  16783. 0x1467c, 0x146c0, 0x146f8, 0x1471c, 0x14738, 0x14770, 0x1477e, 0x14782, 0x14784, 0x14788, 0x14790, 0x147a0,
  16784. 0x147bc, 0x147c6, 0x147cc, 0x147d8, 0x147ee, 0x14810, 0x14820, 0x1483c, 0x14840, 0x14878, 0x148f0, 0x149e0,
  16785. 0x14bc0, 0x14c30, 0x14c3e, 0x14c60, 0x14c7c, 0x14cc0, 0x14cf8, 0x14df0, 0x14e38, 0x14e70, 0x14e7e, 0x14ee0,
  16786. 0x14efc, 0x14f04, 0x14f08, 0x14f10, 0x14f1e, 0x14f20, 0x14f3c, 0x14f40, 0x14f78, 0x14f86, 0x14f8c, 0x14f98,
  16787. 0x14fb0, 0x14fce, 0x14fdc, 0x15020, 0x15040, 0x15078, 0x150f0, 0x151e0, 0x153c0, 0x15860, 0x1587c, 0x158c0,
  16788. 0x158f8, 0x159f0, 0x15be0, 0x15c70, 0x15c7e, 0x15ce0, 0x15cfc, 0x15dc0, 0x15df8, 0x15e08, 0x15e10, 0x15e20,
  16789. 0x15e40, 0x15e78, 0x15ef0, 0x15f0c, 0x15f18, 0x15f30, 0x15f60, 0x15f7c, 0x15f8e, 0x15f9c, 0x15fb8, 0x1604e,
  16790. 0x1605c, 0x1608e, 0x1609c, 0x160b8, 0x160c2, 0x160c4, 0x160c8, 0x160de, 0x1610e, 0x1611c, 0x16138, 0x16170,
  16791. 0x1617e, 0x16184, 0x16188, 0x16190, 0x1619e, 0x161a0, 0x161bc, 0x161c6, 0x161cc, 0x161d8, 0x161f2, 0x161f4,
  16792. 0x1620e, 0x1621c, 0x16238, 0x16270, 0x1627e, 0x162e0, 0x162fc, 0x16304, 0x16308, 0x16310, 0x1631e, 0x16320,
  16793. 0x1633c, 0x16340, 0x16378, 0x16386, 0x1638c, 0x16398, 0x163b0, 0x163be, 0x163ce, 0x163dc, 0x163e2, 0x163e4,
  16794. 0x163e8, 0x163f6, 0x1640e, 0x1641c, 0x16438, 0x16470, 0x1647e, 0x164e0, 0x164fc, 0x165c0, 0x165f8, 0x16610,
  16795. 0x1661e, 0x16620, 0x1663c, 0x16640, 0x16678, 0x166f0, 0x16718, 0x16730, 0x1673e, 0x16760, 0x1677c, 0x1678e,
  16796. 0x1679c, 0x167b8, 0x167c2, 0x167c4, 0x167c8, 0x167d0, 0x167de, 0x167e6, 0x167ec, 0x1681c, 0x16838, 0x16870,
  16797. 0x168e0, 0x168fc, 0x169c0, 0x169f8, 0x16bf0, 0x16c10, 0x16c1e, 0x16c20, 0x16c3c, 0x16c40, 0x16c78, 0x16cf0,
  16798. 0x16de0, 0x16e18, 0x16e30, 0x16e3e, 0x16e60, 0x16e7c, 0x16ec0, 0x16ef8, 0x16f1c, 0x16f38, 0x16f70, 0x16f7e,
  16799. 0x16f84, 0x16f88, 0x16f90, 0x16f9e, 0x16fa0, 0x16fbc, 0x16fc6, 0x16fcc, 0x16fd8, 0x17026, 0x1702c, 0x17046,
  16800. 0x1704c, 0x17058, 0x1706e, 0x17086, 0x1708c, 0x17098, 0x170b0, 0x170be, 0x170ce, 0x170dc, 0x170e8, 0x17106,
  16801. 0x1710c, 0x17118, 0x17130, 0x1713e, 0x17160, 0x1717c, 0x1718e, 0x1719c, 0x171b8, 0x171c2, 0x171c4, 0x171c8,
  16802. 0x171d0, 0x171de, 0x171e6, 0x171ec, 0x171fa, 0x17206, 0x1720c, 0x17218, 0x17230, 0x1723e, 0x17260, 0x1727c,
  16803. 0x172c0, 0x172f8, 0x1730e, 0x1731c, 0x17338, 0x17370, 0x1737e, 0x17388, 0x17390, 0x1739e, 0x173a0, 0x173bc,
  16804. 0x173cc, 0x173d8, 0x173ee, 0x173f2, 0x173f4, 0x1740c, 0x17418, 0x17430, 0x1743e, 0x17460, 0x1747c, 0x174c0,
  16805. 0x174f8, 0x175f0, 0x1760e, 0x1761c, 0x17638, 0x17670, 0x1767e, 0x176e0, 0x176fc, 0x17708, 0x17710, 0x1771e,
  16806. 0x17720, 0x1773c, 0x17740, 0x17778, 0x17798, 0x177b0, 0x177be, 0x177dc, 0x177e2, 0x177e4, 0x177e8, 0x17822,
  16807. 0x17824, 0x17828, 0x17836, 0x17842, 0x17844, 0x17848, 0x17850, 0x1785e, 0x17866, 0x1786c, 0x17882, 0x17884,
  16808. 0x17888, 0x17890, 0x1789e, 0x178a0, 0x178bc, 0x178c6, 0x178cc, 0x178d8, 0x178ee, 0x178f2, 0x178f4, 0x17902,
  16809. 0x17904, 0x17908, 0x17910, 0x1791e, 0x17920, 0x1793c, 0x17940, 0x17978, 0x17986, 0x1798c, 0x17998, 0x179b0,
  16810. 0x179be, 0x179ce, 0x179dc, 0x179e2, 0x179e4, 0x179e8, 0x179f6, 0x17a04, 0x17a08, 0x17a10, 0x17a1e, 0x17a20,
  16811. 0x17a3c, 0x17a40, 0x17a78, 0x17af0, 0x17b06, 0x17b0c, 0x17b18, 0x17b30, 0x17b3e, 0x17b60, 0x17b7c, 0x17b8e,
  16812. 0x17b9c, 0x17bb8, 0x17bc4, 0x17bc8, 0x17bd0, 0x17bde, 0x17be6, 0x17bec, 0x17c2e, 0x17c32, 0x17c34, 0x17c4e,
  16813. 0x17c5c, 0x17c62, 0x17c64, 0x17c68, 0x17c76, 0x17c8e, 0x17c9c, 0x17cb8, 0x17cc2, 0x17cc4, 0x17cc8, 0x17cd0,
  16814. 0x17cde, 0x17ce6, 0x17cec, 0x17d0e, 0x17d1c, 0x17d38, 0x17d70, 0x17d82, 0x17d84, 0x17d88, 0x17d90, 0x17d9e,
  16815. 0x17da0, 0x17dbc, 0x17dc6, 0x17dcc, 0x17dd8, 0x17dee, 0x17e26, 0x17e2c, 0x17e3a, 0x17e46, 0x17e4c, 0x17e58,
  16816. 0x17e6e, 0x17e72, 0x17e74, 0x17e86, 0x17e8c, 0x17e98, 0x17eb0, 0x17ece, 0x17edc, 0x17ee2, 0x17ee4, 0x17ee8,
  16817. 0x17ef6, 0x1813a, 0x18172, 0x18174, 0x18216, 0x18226, 0x1823a, 0x1824c, 0x18258, 0x1826e, 0x18272, 0x18274,
  16818. 0x18298, 0x182be, 0x182e2, 0x182e4, 0x182e8, 0x182f6, 0x1835e, 0x1837a, 0x183ae, 0x183d6, 0x18416, 0x18426,
  16819. 0x1842c, 0x1843a, 0x18446, 0x18458, 0x1846e, 0x18472, 0x18474, 0x18486, 0x184b0, 0x184be, 0x184ce, 0x184dc,
  16820. 0x184e2, 0x184e4, 0x184e8, 0x184f6, 0x18506, 0x1850c, 0x18518, 0x18530, 0x1853e, 0x18560, 0x1857c, 0x1858e,
  16821. 0x1859c, 0x185b8, 0x185c2, 0x185c4, 0x185c8, 0x185d0, 0x185de, 0x185e6, 0x185ec, 0x185fa, 0x18612, 0x18614,
  16822. 0x18622, 0x18628, 0x18636, 0x18642, 0x18650, 0x1865e, 0x1867a, 0x18682, 0x18684, 0x18688, 0x18690, 0x1869e,
  16823. 0x186a0, 0x186bc, 0x186c6, 0x186cc, 0x186d8, 0x186ee, 0x186f2, 0x186f4, 0x1872e, 0x1874e, 0x1875c, 0x18796,
  16824. 0x187a6, 0x187ac, 0x187d2, 0x187d4, 0x18826, 0x1882c, 0x1883a, 0x18846, 0x1884c, 0x18858, 0x1886e, 0x18872,
  16825. 0x18874, 0x18886, 0x18898, 0x188b0, 0x188be, 0x188ce, 0x188dc, 0x188e2, 0x188e4, 0x188e8, 0x188f6, 0x1890c,
  16826. 0x18930, 0x1893e, 0x18960, 0x1897c, 0x1898e, 0x189b8, 0x189c2, 0x189c8, 0x189d0, 0x189de, 0x189e6, 0x189ec,
  16827. 0x189fa, 0x18a18, 0x18a30, 0x18a3e, 0x18a60, 0x18a7c, 0x18ac0, 0x18af8, 0x18b1c, 0x18b38, 0x18b70, 0x18b7e,
  16828. 0x18b82, 0x18b84, 0x18b88, 0x18b90, 0x18b9e, 0x18ba0, 0x18bbc, 0x18bc6, 0x18bcc, 0x18bd8, 0x18bee, 0x18bf2,
  16829. 0x18bf4, 0x18c22, 0x18c24, 0x18c28, 0x18c36, 0x18c42, 0x18c48, 0x18c50, 0x18c5e, 0x18c66, 0x18c7a, 0x18c82,
  16830. 0x18c84, 0x18c90, 0x18c9e, 0x18ca0, 0x18cbc, 0x18ccc, 0x18cf2, 0x18cf4, 0x18d04, 0x18d08, 0x18d10, 0x18d1e,
  16831. 0x18d20, 0x18d3c, 0x18d40, 0x18d78, 0x18d86, 0x18d98, 0x18dce, 0x18de2, 0x18de4, 0x18de8, 0x18e2e, 0x18e32,
  16832. 0x18e34, 0x18e4e, 0x18e5c, 0x18e62, 0x18e64, 0x18e68, 0x18e8e, 0x18e9c, 0x18eb8, 0x18ec2, 0x18ec4, 0x18ec8,
  16833. 0x18ed0, 0x18efa, 0x18f16, 0x18f26, 0x18f2c, 0x18f46, 0x18f4c, 0x18f58, 0x18f6e, 0x18f8a, 0x18f92, 0x18f94,
  16834. 0x18fa2, 0x18fa4, 0x18fa8, 0x18fb6, 0x1902c, 0x1903a, 0x19046, 0x1904c, 0x19058, 0x19072, 0x19074, 0x19086,
  16835. 0x19098, 0x190b0, 0x190be, 0x190ce, 0x190dc, 0x190e2, 0x190e8, 0x190f6, 0x19106, 0x1910c, 0x19130, 0x1913e,
  16836. 0x19160, 0x1917c, 0x1918e, 0x1919c, 0x191b8, 0x191c2, 0x191c8, 0x191d0, 0x191de, 0x191e6, 0x191ec, 0x191fa,
  16837. 0x19218, 0x1923e, 0x19260, 0x1927c, 0x192c0, 0x192f8, 0x19338, 0x19370, 0x1937e, 0x19382, 0x19384, 0x19390,
  16838. 0x1939e, 0x193a0, 0x193bc, 0x193c6, 0x193cc, 0x193d8, 0x193ee, 0x193f2, 0x193f4, 0x19430, 0x1943e, 0x19460,
  16839. 0x1947c, 0x194c0, 0x194f8, 0x195f0, 0x19638, 0x19670, 0x1967e, 0x196e0, 0x196fc, 0x19702, 0x19704, 0x19708,
  16840. 0x19710, 0x19720, 0x1973c, 0x19740, 0x19778, 0x19786, 0x1978c, 0x19798, 0x197b0, 0x197be, 0x197ce, 0x197dc,
  16841. 0x197e2, 0x197e4, 0x197e8, 0x19822, 0x19824, 0x19842, 0x19848, 0x19850, 0x1985e, 0x19866, 0x1987a, 0x19882,
  16842. 0x19884, 0x19890, 0x1989e, 0x198a0, 0x198bc, 0x198cc, 0x198f2, 0x198f4, 0x19902, 0x19908, 0x1991e, 0x19920,
  16843. 0x1993c, 0x19940, 0x19978, 0x19986, 0x19998, 0x199ce, 0x199e2, 0x199e4, 0x199e8, 0x19a08, 0x19a10, 0x19a1e,
  16844. 0x19a20, 0x19a3c, 0x19a40, 0x19a78, 0x19af0, 0x19b18, 0x19b3e, 0x19b60, 0x19b9c, 0x19bc2, 0x19bc4, 0x19bc8,
  16845. 0x19bd0, 0x19be6, 0x19c2e, 0x19c34, 0x19c4e, 0x19c5c, 0x19c62, 0x19c64, 0x19c68, 0x19c8e, 0x19c9c, 0x19cb8,
  16846. 0x19cc2, 0x19cc8, 0x19cd0, 0x19ce6, 0x19cfa, 0x19d0e, 0x19d1c, 0x19d38, 0x19d70, 0x19d7e, 0x19d82, 0x19d84,
  16847. 0x19d88, 0x19d90, 0x19da0, 0x19dcc, 0x19df2, 0x19df4, 0x19e16, 0x19e26, 0x19e2c, 0x19e46, 0x19e4c, 0x19e58,
  16848. 0x19e74, 0x19e86, 0x19e8c, 0x19e98, 0x19eb0, 0x19ebe, 0x19ece, 0x19ee2, 0x19ee4, 0x19ee8, 0x19f0a, 0x19f12,
  16849. 0x19f14, 0x19f22, 0x19f24, 0x19f28, 0x19f42, 0x19f44, 0x19f48, 0x19f50, 0x19f5e, 0x19f6c, 0x19f9a, 0x19fae,
  16850. 0x19fb2, 0x19fb4, 0x1a046, 0x1a04c, 0x1a072, 0x1a074, 0x1a086, 0x1a08c, 0x1a098, 0x1a0b0, 0x1a0be, 0x1a0e2,
  16851. 0x1a0e4, 0x1a0e8, 0x1a0f6, 0x1a106, 0x1a10c, 0x1a118, 0x1a130, 0x1a13e, 0x1a160, 0x1a17c, 0x1a18e, 0x1a19c,
  16852. 0x1a1b8, 0x1a1c2, 0x1a1c4, 0x1a1c8, 0x1a1d0, 0x1a1de, 0x1a1e6, 0x1a1ec, 0x1a218, 0x1a230, 0x1a23e, 0x1a260,
  16853. 0x1a27c, 0x1a2c0, 0x1a2f8, 0x1a31c, 0x1a338, 0x1a370, 0x1a37e, 0x1a382, 0x1a384, 0x1a388, 0x1a390, 0x1a39e,
  16854. 0x1a3a0, 0x1a3bc, 0x1a3c6, 0x1a3cc, 0x1a3d8, 0x1a3ee, 0x1a3f2, 0x1a3f4, 0x1a418, 0x1a430, 0x1a43e, 0x1a460,
  16855. 0x1a47c, 0x1a4c0, 0x1a4f8, 0x1a5f0, 0x1a61c, 0x1a638, 0x1a670, 0x1a67e, 0x1a6e0, 0x1a6fc, 0x1a702, 0x1a704,
  16856. 0x1a708, 0x1a710, 0x1a71e, 0x1a720, 0x1a73c, 0x1a740, 0x1a778, 0x1a786, 0x1a78c, 0x1a798, 0x1a7b0, 0x1a7be,
  16857. 0x1a7ce, 0x1a7dc, 0x1a7e2, 0x1a7e4, 0x1a7e8, 0x1a830, 0x1a860, 0x1a87c, 0x1a8c0, 0x1a8f8, 0x1a9f0, 0x1abe0,
  16858. 0x1ac70, 0x1ac7e, 0x1ace0, 0x1acfc, 0x1adc0, 0x1adf8, 0x1ae04, 0x1ae08, 0x1ae10, 0x1ae20, 0x1ae3c, 0x1ae40,
  16859. 0x1ae78, 0x1aef0, 0x1af06, 0x1af0c, 0x1af18, 0x1af30, 0x1af3e, 0x1af60, 0x1af7c, 0x1af8e, 0x1af9c, 0x1afb8,
  16860. 0x1afc4, 0x1afc8, 0x1afd0, 0x1afde, 0x1b042, 0x1b05e, 0x1b07a, 0x1b082, 0x1b084, 0x1b088, 0x1b090, 0x1b09e,
  16861. 0x1b0a0, 0x1b0bc, 0x1b0cc, 0x1b0f2, 0x1b0f4, 0x1b102, 0x1b104, 0x1b108, 0x1b110, 0x1b11e, 0x1b120, 0x1b13c,
  16862. 0x1b140, 0x1b178, 0x1b186, 0x1b198, 0x1b1ce, 0x1b1e2, 0x1b1e4, 0x1b1e8, 0x1b204, 0x1b208, 0x1b210, 0x1b21e,
  16863. 0x1b220, 0x1b23c, 0x1b240, 0x1b278, 0x1b2f0, 0x1b30c, 0x1b33e, 0x1b360, 0x1b39c, 0x1b3c2, 0x1b3c4, 0x1b3c8,
  16864. 0x1b3d0, 0x1b3e6, 0x1b410, 0x1b41e, 0x1b420, 0x1b43c, 0x1b440, 0x1b478, 0x1b4f0, 0x1b5e0, 0x1b618, 0x1b660,
  16865. 0x1b67c, 0x1b6c0, 0x1b738, 0x1b782, 0x1b784, 0x1b788, 0x1b790, 0x1b79e, 0x1b7a0, 0x1b7cc, 0x1b82e, 0x1b84e,
  16866. 0x1b85c, 0x1b88e, 0x1b89c, 0x1b8b8, 0x1b8c2, 0x1b8c4, 0x1b8c8, 0x1b8d0, 0x1b8e6, 0x1b8fa, 0x1b90e, 0x1b91c,
  16867. 0x1b938, 0x1b970, 0x1b97e, 0x1b982, 0x1b984, 0x1b988, 0x1b990, 0x1b99e, 0x1b9a0, 0x1b9cc, 0x1b9f2, 0x1b9f4,
  16868. 0x1ba0e, 0x1ba1c, 0x1ba38, 0x1ba70, 0x1ba7e, 0x1bae0, 0x1bafc, 0x1bb08, 0x1bb10, 0x1bb20, 0x1bb3c, 0x1bb40,
  16869. 0x1bb98, 0x1bbce, 0x1bbe2, 0x1bbe4, 0x1bbe8, 0x1bc16, 0x1bc26, 0x1bc2c, 0x1bc46, 0x1bc4c, 0x1bc58, 0x1bc72,
  16870. 0x1bc74, 0x1bc86, 0x1bc8c, 0x1bc98, 0x1bcb0, 0x1bcbe, 0x1bcce, 0x1bce2, 0x1bce4, 0x1bce8, 0x1bd06, 0x1bd0c,
  16871. 0x1bd18, 0x1bd30, 0x1bd3e, 0x1bd60, 0x1bd7c, 0x1bd9c, 0x1bdc2, 0x1bdc4, 0x1bdc8, 0x1bdd0, 0x1bde6, 0x1bdfa,
  16872. 0x1be12, 0x1be14, 0x1be22, 0x1be24, 0x1be28, 0x1be42, 0x1be44, 0x1be48, 0x1be50, 0x1be5e, 0x1be66, 0x1be82,
  16873. 0x1be84, 0x1be88, 0x1be90, 0x1be9e, 0x1bea0, 0x1bebc, 0x1becc, 0x1bef4, 0x1bf1a, 0x1bf2e, 0x1bf32, 0x1bf34,
  16874. 0x1bf4e, 0x1bf5c, 0x1bf62, 0x1bf64, 0x1bf68, 0x1c09a, 0x1c0b2, 0x1c0b4, 0x1c11a, 0x1c132, 0x1c134, 0x1c162,
  16875. 0x1c164, 0x1c168, 0x1c176, 0x1c1ba, 0x1c21a, 0x1c232, 0x1c234, 0x1c24e, 0x1c25c, 0x1c262, 0x1c264, 0x1c268,
  16876. 0x1c276, 0x1c28e, 0x1c2c2, 0x1c2c4, 0x1c2c8, 0x1c2d0, 0x1c2de, 0x1c2e6, 0x1c2ec, 0x1c2fa, 0x1c316, 0x1c326,
  16877. 0x1c33a, 0x1c346, 0x1c34c, 0x1c372, 0x1c374, 0x1c41a, 0x1c42e, 0x1c432, 0x1c434, 0x1c44e, 0x1c45c, 0x1c462,
  16878. 0x1c464, 0x1c468, 0x1c476, 0x1c48e, 0x1c49c, 0x1c4b8, 0x1c4c2, 0x1c4c8, 0x1c4d0, 0x1c4de, 0x1c4e6, 0x1c4ec,
  16879. 0x1c4fa, 0x1c51c, 0x1c538, 0x1c570, 0x1c57e, 0x1c582, 0x1c584, 0x1c588, 0x1c590, 0x1c59e, 0x1c5a0, 0x1c5bc,
  16880. 0x1c5c6, 0x1c5cc, 0x1c5d8, 0x1c5ee, 0x1c5f2, 0x1c5f4, 0x1c616, 0x1c626, 0x1c62c, 0x1c63a, 0x1c646, 0x1c64c,
  16881. 0x1c658, 0x1c66e, 0x1c672, 0x1c674, 0x1c686, 0x1c68c, 0x1c698, 0x1c6b0, 0x1c6be, 0x1c6ce, 0x1c6dc, 0x1c6e2,
  16882. 0x1c6e4, 0x1c6e8, 0x1c712, 0x1c714, 0x1c722, 0x1c728, 0x1c736, 0x1c742, 0x1c744, 0x1c748, 0x1c750, 0x1c75e,
  16883. 0x1c766, 0x1c76c, 0x1c77a, 0x1c7ae, 0x1c7d6, 0x1c7ea, 0x1c81a, 0x1c82e, 0x1c832, 0x1c834, 0x1c84e, 0x1c85c,
  16884. 0x1c862, 0x1c864, 0x1c868, 0x1c876, 0x1c88e, 0x1c89c, 0x1c8b8, 0x1c8c2, 0x1c8c8, 0x1c8d0, 0x1c8de, 0x1c8e6,
  16885. 0x1c8ec, 0x1c8fa, 0x1c90e, 0x1c938, 0x1c970, 0x1c97e, 0x1c982, 0x1c984, 0x1c990, 0x1c99e, 0x1c9a0, 0x1c9bc,
  16886. 0x1c9c6, 0x1c9cc, 0x1c9d8, 0x1c9ee, 0x1c9f2, 0x1c9f4, 0x1ca38, 0x1ca70, 0x1ca7e, 0x1cae0, 0x1cafc, 0x1cb02,
  16887. 0x1cb04, 0x1cb08, 0x1cb10, 0x1cb20, 0x1cb3c, 0x1cb40, 0x1cb78, 0x1cb86, 0x1cb8c, 0x1cb98, 0x1cbb0, 0x1cbbe,
  16888. 0x1cbce, 0x1cbdc, 0x1cbe2, 0x1cbe4, 0x1cbe8, 0x1cbf6, 0x1cc16, 0x1cc26, 0x1cc2c, 0x1cc3a, 0x1cc46, 0x1cc58,
  16889. 0x1cc72, 0x1cc74, 0x1cc86, 0x1ccb0, 0x1ccbe, 0x1ccce, 0x1cce2, 0x1cce4, 0x1cce8, 0x1cd06, 0x1cd0c, 0x1cd18,
  16890. 0x1cd30, 0x1cd3e, 0x1cd60, 0x1cd7c, 0x1cd9c, 0x1cdc2, 0x1cdc4, 0x1cdc8, 0x1cdd0, 0x1cdde, 0x1cde6, 0x1cdfa,
  16891. 0x1ce22, 0x1ce28, 0x1ce42, 0x1ce50, 0x1ce5e, 0x1ce66, 0x1ce7a, 0x1ce82, 0x1ce84, 0x1ce88, 0x1ce90, 0x1ce9e,
  16892. 0x1cea0, 0x1cebc, 0x1cecc, 0x1cef2, 0x1cef4, 0x1cf2e, 0x1cf32, 0x1cf34, 0x1cf4e, 0x1cf5c, 0x1cf62, 0x1cf64,
  16893. 0x1cf68, 0x1cf96, 0x1cfa6, 0x1cfac, 0x1cfca, 0x1cfd2, 0x1cfd4, 0x1d02e, 0x1d032, 0x1d034, 0x1d04e, 0x1d05c,
  16894. 0x1d062, 0x1d064, 0x1d068, 0x1d076, 0x1d08e, 0x1d09c, 0x1d0b8, 0x1d0c2, 0x1d0c4, 0x1d0c8, 0x1d0d0, 0x1d0de,
  16895. 0x1d0e6, 0x1d0ec, 0x1d0fa, 0x1d11c, 0x1d138, 0x1d170, 0x1d17e, 0x1d182, 0x1d184, 0x1d188, 0x1d190, 0x1d19e,
  16896. 0x1d1a0, 0x1d1bc, 0x1d1c6, 0x1d1cc, 0x1d1d8, 0x1d1ee, 0x1d1f2, 0x1d1f4, 0x1d21c, 0x1d238, 0x1d270, 0x1d27e,
  16897. 0x1d2e0, 0x1d2fc, 0x1d302, 0x1d304, 0x1d308, 0x1d310, 0x1d31e, 0x1d320, 0x1d33c, 0x1d340, 0x1d378, 0x1d386,
  16898. 0x1d38c, 0x1d398, 0x1d3b0, 0x1d3be, 0x1d3ce, 0x1d3dc, 0x1d3e2, 0x1d3e4, 0x1d3e8, 0x1d3f6, 0x1d470, 0x1d47e,
  16899. 0x1d4e0, 0x1d4fc, 0x1d5c0, 0x1d5f8, 0x1d604, 0x1d608, 0x1d610, 0x1d620, 0x1d640, 0x1d678, 0x1d6f0, 0x1d706,
  16900. 0x1d70c, 0x1d718, 0x1d730, 0x1d73e, 0x1d760, 0x1d77c, 0x1d78e, 0x1d79c, 0x1d7b8, 0x1d7c2, 0x1d7c4, 0x1d7c8,
  16901. 0x1d7d0, 0x1d7de, 0x1d7e6, 0x1d7ec, 0x1d826, 0x1d82c, 0x1d83a, 0x1d846, 0x1d84c, 0x1d858, 0x1d872, 0x1d874,
  16902. 0x1d886, 0x1d88c, 0x1d898, 0x1d8b0, 0x1d8be, 0x1d8ce, 0x1d8e2, 0x1d8e4, 0x1d8e8, 0x1d8f6, 0x1d90c, 0x1d918,
  16903. 0x1d930, 0x1d93e, 0x1d960, 0x1d97c, 0x1d99c, 0x1d9c2, 0x1d9c4, 0x1d9c8, 0x1d9d0, 0x1d9e6, 0x1d9fa, 0x1da0c,
  16904. 0x1da18, 0x1da30, 0x1da3e, 0x1da60, 0x1da7c, 0x1dac0, 0x1daf8, 0x1db38, 0x1db82, 0x1db84, 0x1db88, 0x1db90,
  16905. 0x1db9e, 0x1dba0, 0x1dbcc, 0x1dbf2, 0x1dbf4, 0x1dc22, 0x1dc42, 0x1dc44, 0x1dc48, 0x1dc50, 0x1dc5e, 0x1dc66,
  16906. 0x1dc7a, 0x1dc82, 0x1dc84, 0x1dc88, 0x1dc90, 0x1dc9e, 0x1dca0, 0x1dcbc, 0x1dccc, 0x1dcf2, 0x1dcf4, 0x1dd04,
  16907. 0x1dd08, 0x1dd10, 0x1dd1e, 0x1dd20, 0x1dd3c, 0x1dd40, 0x1dd78, 0x1dd86, 0x1dd98, 0x1ddce, 0x1dde2, 0x1dde4,
  16908. 0x1dde8, 0x1de2e, 0x1de32, 0x1de34, 0x1de4e, 0x1de5c, 0x1de62, 0x1de64, 0x1de68, 0x1de8e, 0x1de9c, 0x1deb8,
  16909. 0x1dec2, 0x1dec4, 0x1dec8, 0x1ded0, 0x1dee6, 0x1defa, 0x1df16, 0x1df26, 0x1df2c, 0x1df46, 0x1df4c, 0x1df58,
  16910. 0x1df72, 0x1df74, 0x1df8a, 0x1df92, 0x1df94, 0x1dfa2, 0x1dfa4, 0x1dfa8, 0x1e08a, 0x1e092, 0x1e094, 0x1e0a2,
  16911. 0x1e0a4, 0x1e0a8, 0x1e0b6, 0x1e0da, 0x1e10a, 0x1e112, 0x1e114, 0x1e122, 0x1e124, 0x1e128, 0x1e136, 0x1e142,
  16912. 0x1e144, 0x1e148, 0x1e150, 0x1e166, 0x1e16c, 0x1e17a, 0x1e19a, 0x1e1b2, 0x1e1b4, 0x1e20a, 0x1e212, 0x1e214,
  16913. 0x1e222, 0x1e224, 0x1e228, 0x1e236, 0x1e242, 0x1e248, 0x1e250, 0x1e25e, 0x1e266, 0x1e26c, 0x1e27a, 0x1e282,
  16914. 0x1e284, 0x1e288, 0x1e290, 0x1e2a0, 0x1e2bc, 0x1e2c6, 0x1e2cc, 0x1e2d8, 0x1e2ee, 0x1e2f2, 0x1e2f4, 0x1e31a,
  16915. 0x1e332, 0x1e334, 0x1e35c, 0x1e362, 0x1e364, 0x1e368, 0x1e3ba, 0x1e40a, 0x1e412, 0x1e414, 0x1e422, 0x1e428,
  16916. 0x1e436, 0x1e442, 0x1e448, 0x1e450, 0x1e45e, 0x1e466, 0x1e46c, 0x1e47a, 0x1e482, 0x1e484, 0x1e490, 0x1e49e,
  16917. 0x1e4a0, 0x1e4bc, 0x1e4c6, 0x1e4cc, 0x1e4d8, 0x1e4ee, 0x1e4f2, 0x1e4f4, 0x1e502, 0x1e504, 0x1e508, 0x1e510,
  16918. 0x1e51e, 0x1e520, 0x1e53c, 0x1e540, 0x1e578, 0x1e586, 0x1e58c, 0x1e598, 0x1e5b0, 0x1e5be, 0x1e5ce, 0x1e5dc,
  16919. 0x1e5e2, 0x1e5e4, 0x1e5e8, 0x1e5f6, 0x1e61a, 0x1e62e, 0x1e632, 0x1e634, 0x1e64e, 0x1e65c, 0x1e662, 0x1e668,
  16920. 0x1e68e, 0x1e69c, 0x1e6b8, 0x1e6c2, 0x1e6c4, 0x1e6c8, 0x1e6d0, 0x1e6e6, 0x1e6fa, 0x1e716, 0x1e726, 0x1e72c,
  16921. 0x1e73a, 0x1e746, 0x1e74c, 0x1e758, 0x1e772, 0x1e774, 0x1e792, 0x1e794, 0x1e7a2, 0x1e7a4, 0x1e7a8, 0x1e7b6,
  16922. 0x1e812, 0x1e814, 0x1e822, 0x1e824, 0x1e828, 0x1e836, 0x1e842, 0x1e844, 0x1e848, 0x1e850, 0x1e85e, 0x1e866,
  16923. 0x1e86c, 0x1e87a, 0x1e882, 0x1e884, 0x1e888, 0x1e890, 0x1e89e, 0x1e8a0, 0x1e8bc, 0x1e8c6, 0x1e8cc, 0x1e8d8,
  16924. 0x1e8ee, 0x1e8f2, 0x1e8f4, 0x1e902, 0x1e904, 0x1e908, 0x1e910, 0x1e920, 0x1e93c, 0x1e940, 0x1e978, 0x1e986,
  16925. 0x1e98c, 0x1e998, 0x1e9b0, 0x1e9be, 0x1e9ce, 0x1e9dc, 0x1e9e2, 0x1e9e4, 0x1e9e8, 0x1e9f6, 0x1ea04, 0x1ea08,
  16926. 0x1ea10, 0x1ea20, 0x1ea40, 0x1ea78, 0x1eaf0, 0x1eb06, 0x1eb0c, 0x1eb18, 0x1eb30, 0x1eb3e, 0x1eb60, 0x1eb7c,
  16927. 0x1eb8e, 0x1eb9c, 0x1ebb8, 0x1ebc2, 0x1ebc4, 0x1ebc8, 0x1ebd0, 0x1ebde, 0x1ebe6, 0x1ebec, 0x1ec1a, 0x1ec2e,
  16928. 0x1ec32, 0x1ec34, 0x1ec4e, 0x1ec5c, 0x1ec62, 0x1ec64, 0x1ec68, 0x1ec8e, 0x1ec9c, 0x1ecb8, 0x1ecc2, 0x1ecc4,
  16929. 0x1ecc8, 0x1ecd0, 0x1ece6, 0x1ecfa, 0x1ed0e, 0x1ed1c, 0x1ed38, 0x1ed70, 0x1ed7e, 0x1ed82, 0x1ed84, 0x1ed88,
  16930. 0x1ed90, 0x1ed9e, 0x1eda0, 0x1edcc, 0x1edf2, 0x1edf4, 0x1ee16, 0x1ee26, 0x1ee2c, 0x1ee3a, 0x1ee46, 0x1ee4c,
  16931. 0x1ee58, 0x1ee6e, 0x1ee72, 0x1ee74, 0x1ee86, 0x1ee8c, 0x1ee98, 0x1eeb0, 0x1eebe, 0x1eece, 0x1eedc, 0x1eee2,
  16932. 0x1eee4, 0x1eee8, 0x1ef12, 0x1ef22, 0x1ef24, 0x1ef28, 0x1ef36, 0x1ef42, 0x1ef44, 0x1ef48, 0x1ef50, 0x1ef5e,
  16933. 0x1ef66, 0x1ef6c, 0x1ef7a, 0x1efae, 0x1efb2, 0x1efb4, 0x1efd6, 0x1f096, 0x1f0a6, 0x1f0ac, 0x1f0ba, 0x1f0ca,
  16934. 0x1f0d2, 0x1f0d4, 0x1f116, 0x1f126, 0x1f12c, 0x1f13a, 0x1f146, 0x1f14c, 0x1f158, 0x1f16e, 0x1f172, 0x1f174,
  16935. 0x1f18a, 0x1f192, 0x1f194, 0x1f1a2, 0x1f1a4, 0x1f1a8, 0x1f1da, 0x1f216, 0x1f226, 0x1f22c, 0x1f23a, 0x1f246,
  16936. 0x1f258, 0x1f26e, 0x1f272, 0x1f274, 0x1f286, 0x1f28c, 0x1f298, 0x1f2b0, 0x1f2be, 0x1f2ce, 0x1f2dc, 0x1f2e2,
  16937. 0x1f2e4, 0x1f2e8, 0x1f2f6, 0x1f30a, 0x1f312, 0x1f314, 0x1f322, 0x1f328, 0x1f342, 0x1f344, 0x1f348, 0x1f350,
  16938. 0x1f35e, 0x1f366, 0x1f37a, 0x1f39a, 0x1f3ae, 0x1f3b2, 0x1f3b4, 0x1f416, 0x1f426, 0x1f42c, 0x1f43a, 0x1f446,
  16939. 0x1f44c, 0x1f458, 0x1f46e, 0x1f472, 0x1f474, 0x1f486, 0x1f48c, 0x1f498, 0x1f4b0, 0x1f4be, 0x1f4ce, 0x1f4dc,
  16940. 0x1f4e2, 0x1f4e4, 0x1f4e8, 0x1f4f6, 0x1f506, 0x1f50c, 0x1f518, 0x1f530, 0x1f53e, 0x1f560, 0x1f57c, 0x1f58e,
  16941. 0x1f59c, 0x1f5b8, 0x1f5c2, 0x1f5c4, 0x1f5c8, 0x1f5d0, 0x1f5de, 0x1f5e6, 0x1f5ec, 0x1f5fa, 0x1f60a, 0x1f612,
  16942. 0x1f614, 0x1f622, 0x1f624, 0x1f628, 0x1f636, 0x1f642, 0x1f644, 0x1f648, 0x1f650, 0x1f65e, 0x1f666, 0x1f67a,
  16943. 0x1f682, 0x1f684, 0x1f688, 0x1f690, 0x1f69e, 0x1f6a0, 0x1f6bc, 0x1f6cc, 0x1f6f2, 0x1f6f4, 0x1f71a, 0x1f72e,
  16944. 0x1f732, 0x1f734, 0x1f74e, 0x1f75c, 0x1f762, 0x1f764, 0x1f768, 0x1f776, 0x1f796, 0x1f7a6, 0x1f7ac, 0x1f7ba,
  16945. 0x1f7d2, 0x1f7d4, 0x1f89a, 0x1f8ae, 0x1f8b2, 0x1f8b4, 0x1f8d6, 0x1f8ea, 0x1f91a, 0x1f92e, 0x1f932, 0x1f934,
  16946. 0x1f94e, 0x1f95c, 0x1f962, 0x1f964, 0x1f968, 0x1f976, 0x1f996, 0x1f9a6, 0x1f9ac, 0x1f9ba, 0x1f9ca, 0x1f9d2,
  16947. 0x1f9d4, 0x1fa1a, 0x1fa2e, 0x1fa32, 0x1fa34, 0x1fa4e, 0x1fa5c, 0x1fa62, 0x1fa64, 0x1fa68, 0x1fa76, 0x1fa8e,
  16948. 0x1fa9c, 0x1fab8, 0x1fac2, 0x1fac4, 0x1fac8, 0x1fad0, 0x1fade, 0x1fae6, 0x1faec, 0x1fb16, 0x1fb26, 0x1fb2c,
  16949. 0x1fb3a, 0x1fb46, 0x1fb4c, 0x1fb58, 0x1fb6e, 0x1fb72, 0x1fb74, 0x1fb8a, 0x1fb92, 0x1fb94, 0x1fba2, 0x1fba4,
  16950. 0x1fba8, 0x1fbb6, 0x1fbda
  16951. ]);
  16952. /**
  16953. * This table contains to codewords for all symbols.
  16954. */
  16955. PDF417Common.CODEWORD_TABLE = Int32Array.from([
  16956. 2627, 1819, 2622, 2621, 1813, 1812, 2729, 2724, 2723, 2779, 2774, 2773, 902, 896, 908, 868, 865, 861, 859, 2511,
  16957. 873, 871, 1780, 835, 2493, 825, 2491, 842, 837, 844, 1764, 1762, 811, 810, 809, 2483, 807, 2482, 806, 2480, 815,
  16958. 814, 813, 812, 2484, 817, 816, 1745, 1744, 1742, 1746, 2655, 2637, 2635, 2626, 2625, 2623, 2628, 1820, 2752,
  16959. 2739, 2737, 2728, 2727, 2725, 2730, 2785, 2783, 2778, 2777, 2775, 2780, 787, 781, 747, 739, 736, 2413, 754, 752,
  16960. 1719, 692, 689, 681, 2371, 678, 2369, 700, 697, 694, 703, 1688, 1686, 642, 638, 2343, 631, 2341, 627, 2338, 651,
  16961. 646, 643, 2345, 654, 652, 1652, 1650, 1647, 1654, 601, 599, 2322, 596, 2321, 594, 2319, 2317, 611, 610, 608, 606,
  16962. 2324, 603, 2323, 615, 614, 612, 1617, 1616, 1614, 1612, 616, 1619, 1618, 2575, 2538, 2536, 905, 901, 898, 909,
  16963. 2509, 2507, 2504, 870, 867, 864, 860, 2512, 875, 872, 1781, 2490, 2489, 2487, 2485, 1748, 836, 834, 832, 830,
  16964. 2494, 827, 2492, 843, 841, 839, 845, 1765, 1763, 2701, 2676, 2674, 2653, 2648, 2656, 2634, 2633, 2631, 2629,
  16965. 1821, 2638, 2636, 2770, 2763, 2761, 2750, 2745, 2753, 2736, 2735, 2733, 2731, 1848, 2740, 2738, 2786, 2784, 591,
  16966. 588, 576, 569, 566, 2296, 1590, 537, 534, 526, 2276, 522, 2274, 545, 542, 539, 548, 1572, 1570, 481, 2245, 466,
  16967. 2242, 462, 2239, 492, 485, 482, 2249, 496, 494, 1534, 1531, 1528, 1538, 413, 2196, 406, 2191, 2188, 425, 419,
  16968. 2202, 415, 2199, 432, 430, 427, 1472, 1467, 1464, 433, 1476, 1474, 368, 367, 2160, 365, 2159, 362, 2157, 2155,
  16969. 2152, 378, 377, 375, 2166, 372, 2165, 369, 2162, 383, 381, 379, 2168, 1419, 1418, 1416, 1414, 385, 1411, 384,
  16970. 1423, 1422, 1420, 1424, 2461, 802, 2441, 2439, 790, 786, 783, 794, 2409, 2406, 2403, 750, 742, 738, 2414, 756,
  16971. 753, 1720, 2367, 2365, 2362, 2359, 1663, 693, 691, 684, 2373, 680, 2370, 702, 699, 696, 704, 1690, 1687, 2337,
  16972. 2336, 2334, 2332, 1624, 2329, 1622, 640, 637, 2344, 634, 2342, 630, 2340, 650, 648, 645, 2346, 655, 653, 1653,
  16973. 1651, 1649, 1655, 2612, 2597, 2595, 2571, 2568, 2565, 2576, 2534, 2529, 2526, 1787, 2540, 2537, 907, 904, 900,
  16974. 910, 2503, 2502, 2500, 2498, 1768, 2495, 1767, 2510, 2508, 2506, 869, 866, 863, 2513, 876, 874, 1782, 2720, 2713,
  16975. 2711, 2697, 2694, 2691, 2702, 2672, 2670, 2664, 1828, 2678, 2675, 2647, 2646, 2644, 2642, 1823, 2639, 1822, 2654,
  16976. 2652, 2650, 2657, 2771, 1855, 2765, 2762, 1850, 1849, 2751, 2749, 2747, 2754, 353, 2148, 344, 342, 336, 2142,
  16977. 332, 2140, 345, 1375, 1373, 306, 2130, 299, 2128, 295, 2125, 319, 314, 311, 2132, 1354, 1352, 1349, 1356, 262,
  16978. 257, 2101, 253, 2096, 2093, 274, 273, 267, 2107, 263, 2104, 280, 278, 275, 1316, 1311, 1308, 1320, 1318, 2052,
  16979. 202, 2050, 2044, 2040, 219, 2063, 212, 2060, 208, 2055, 224, 221, 2066, 1260, 1258, 1252, 231, 1248, 229, 1266,
  16980. 1264, 1261, 1268, 155, 1998, 153, 1996, 1994, 1991, 1988, 165, 164, 2007, 162, 2006, 159, 2003, 2000, 172, 171,
  16981. 169, 2012, 166, 2010, 1186, 1184, 1182, 1179, 175, 1176, 173, 1192, 1191, 1189, 1187, 176, 1194, 1193, 2313,
  16982. 2307, 2305, 592, 589, 2294, 2292, 2289, 578, 572, 568, 2297, 580, 1591, 2272, 2267, 2264, 1547, 538, 536, 529,
  16983. 2278, 525, 2275, 547, 544, 541, 1574, 1571, 2237, 2235, 2229, 1493, 2225, 1489, 478, 2247, 470, 2244, 465, 2241,
  16984. 493, 488, 484, 2250, 498, 495, 1536, 1533, 1530, 1539, 2187, 2186, 2184, 2182, 1432, 2179, 1430, 2176, 1427, 414,
  16985. 412, 2197, 409, 2195, 405, 2193, 2190, 426, 424, 421, 2203, 418, 2201, 431, 429, 1473, 1471, 1469, 1466, 434,
  16986. 1477, 1475, 2478, 2472, 2470, 2459, 2457, 2454, 2462, 803, 2437, 2432, 2429, 1726, 2443, 2440, 792, 789, 785,
  16987. 2401, 2399, 2393, 1702, 2389, 1699, 2411, 2408, 2405, 745, 741, 2415, 758, 755, 1721, 2358, 2357, 2355, 2353,
  16988. 1661, 2350, 1660, 2347, 1657, 2368, 2366, 2364, 2361, 1666, 690, 687, 2374, 683, 2372, 701, 698, 705, 1691, 1689,
  16989. 2619, 2617, 2610, 2608, 2605, 2613, 2593, 2588, 2585, 1803, 2599, 2596, 2563, 2561, 2555, 1797, 2551, 1795, 2573,
  16990. 2570, 2567, 2577, 2525, 2524, 2522, 2520, 1786, 2517, 1785, 2514, 1783, 2535, 2533, 2531, 2528, 1788, 2541, 2539,
  16991. 906, 903, 911, 2721, 1844, 2715, 2712, 1838, 1836, 2699, 2696, 2693, 2703, 1827, 1826, 1824, 2673, 2671, 2669,
  16992. 2666, 1829, 2679, 2677, 1858, 1857, 2772, 1854, 1853, 1851, 1856, 2766, 2764, 143, 1987, 139, 1986, 135, 133,
  16993. 131, 1984, 128, 1983, 125, 1981, 138, 137, 136, 1985, 1133, 1132, 1130, 112, 110, 1974, 107, 1973, 104, 1971,
  16994. 1969, 122, 121, 119, 117, 1977, 114, 1976, 124, 1115, 1114, 1112, 1110, 1117, 1116, 84, 83, 1953, 81, 1952, 78,
  16995. 1950, 1948, 1945, 94, 93, 91, 1959, 88, 1958, 85, 1955, 99, 97, 95, 1961, 1086, 1085, 1083, 1081, 1078, 100,
  16996. 1090, 1089, 1087, 1091, 49, 47, 1917, 44, 1915, 1913, 1910, 1907, 59, 1926, 56, 1925, 53, 1922, 1919, 66, 64,
  16997. 1931, 61, 1929, 1042, 1040, 1038, 71, 1035, 70, 1032, 68, 1048, 1047, 1045, 1043, 1050, 1049, 12, 10, 1869, 1867,
  16998. 1864, 1861, 21, 1880, 19, 1877, 1874, 1871, 28, 1888, 25, 1886, 22, 1883, 982, 980, 977, 974, 32, 30, 991, 989,
  16999. 987, 984, 34, 995, 994, 992, 2151, 2150, 2147, 2146, 2144, 356, 355, 354, 2149, 2139, 2138, 2136, 2134, 1359,
  17000. 343, 341, 338, 2143, 335, 2141, 348, 347, 346, 1376, 1374, 2124, 2123, 2121, 2119, 1326, 2116, 1324, 310, 308,
  17001. 305, 2131, 302, 2129, 298, 2127, 320, 318, 316, 313, 2133, 322, 321, 1355, 1353, 1351, 1357, 2092, 2091, 2089,
  17002. 2087, 1276, 2084, 1274, 2081, 1271, 259, 2102, 256, 2100, 252, 2098, 2095, 272, 269, 2108, 266, 2106, 281, 279,
  17003. 277, 1317, 1315, 1313, 1310, 282, 1321, 1319, 2039, 2037, 2035, 2032, 1203, 2029, 1200, 1197, 207, 2053, 205,
  17004. 2051, 201, 2049, 2046, 2043, 220, 218, 2064, 215, 2062, 211, 2059, 228, 226, 223, 2069, 1259, 1257, 1254, 232,
  17005. 1251, 230, 1267, 1265, 1263, 2316, 2315, 2312, 2311, 2309, 2314, 2304, 2303, 2301, 2299, 1593, 2308, 2306, 590,
  17006. 2288, 2287, 2285, 2283, 1578, 2280, 1577, 2295, 2293, 2291, 579, 577, 574, 571, 2298, 582, 581, 1592, 2263, 2262,
  17007. 2260, 2258, 1545, 2255, 1544, 2252, 1541, 2273, 2271, 2269, 2266, 1550, 535, 532, 2279, 528, 2277, 546, 543, 549,
  17008. 1575, 1573, 2224, 2222, 2220, 1486, 2217, 1485, 2214, 1482, 1479, 2238, 2236, 2234, 2231, 1496, 2228, 1492, 480,
  17009. 477, 2248, 473, 2246, 469, 2243, 490, 487, 2251, 497, 1537, 1535, 1532, 2477, 2476, 2474, 2479, 2469, 2468, 2466,
  17010. 2464, 1730, 2473, 2471, 2453, 2452, 2450, 2448, 1729, 2445, 1728, 2460, 2458, 2456, 2463, 805, 804, 2428, 2427,
  17011. 2425, 2423, 1725, 2420, 1724, 2417, 1722, 2438, 2436, 2434, 2431, 1727, 2444, 2442, 793, 791, 788, 795, 2388,
  17012. 2386, 2384, 1697, 2381, 1696, 2378, 1694, 1692, 2402, 2400, 2398, 2395, 1703, 2392, 1701, 2412, 2410, 2407, 751,
  17013. 748, 744, 2416, 759, 757, 1807, 2620, 2618, 1806, 1805, 2611, 2609, 2607, 2614, 1802, 1801, 1799, 2594, 2592,
  17014. 2590, 2587, 1804, 2600, 2598, 1794, 1793, 1791, 1789, 2564, 2562, 2560, 2557, 1798, 2554, 1796, 2574, 2572, 2569,
  17015. 2578, 1847, 1846, 2722, 1843, 1842, 1840, 1845, 2716, 2714, 1835, 1834, 1832, 1830, 1839, 1837, 2700, 2698, 2695,
  17016. 2704, 1817, 1811, 1810, 897, 862, 1777, 829, 826, 838, 1760, 1758, 808, 2481, 1741, 1740, 1738, 1743, 2624, 1818,
  17017. 2726, 2776, 782, 740, 737, 1715, 686, 679, 695, 1682, 1680, 639, 628, 2339, 647, 644, 1645, 1643, 1640, 1648,
  17018. 602, 600, 597, 595, 2320, 593, 2318, 609, 607, 604, 1611, 1610, 1608, 1606, 613, 1615, 1613, 2328, 926, 924, 892,
  17019. 886, 899, 857, 850, 2505, 1778, 824, 823, 821, 819, 2488, 818, 2486, 833, 831, 828, 840, 1761, 1759, 2649, 2632,
  17020. 2630, 2746, 2734, 2732, 2782, 2781, 570, 567, 1587, 531, 527, 523, 540, 1566, 1564, 476, 467, 463, 2240, 486,
  17021. 483, 1524, 1521, 1518, 1529, 411, 403, 2192, 399, 2189, 423, 416, 1462, 1457, 1454, 428, 1468, 1465, 2210, 366,
  17022. 363, 2158, 360, 2156, 357, 2153, 376, 373, 370, 2163, 1410, 1409, 1407, 1405, 382, 1402, 380, 1417, 1415, 1412,
  17023. 1421, 2175, 2174, 777, 774, 771, 784, 732, 725, 722, 2404, 743, 1716, 676, 674, 668, 2363, 665, 2360, 685, 1684,
  17024. 1681, 626, 624, 622, 2335, 620, 2333, 617, 2330, 641, 635, 649, 1646, 1644, 1642, 2566, 928, 925, 2530, 2527,
  17025. 894, 891, 888, 2501, 2499, 2496, 858, 856, 854, 851, 1779, 2692, 2668, 2665, 2645, 2643, 2640, 2651, 2768, 2759,
  17026. 2757, 2744, 2743, 2741, 2748, 352, 1382, 340, 337, 333, 1371, 1369, 307, 300, 296, 2126, 315, 312, 1347, 1342,
  17027. 1350, 261, 258, 250, 2097, 246, 2094, 271, 268, 264, 1306, 1301, 1298, 276, 1312, 1309, 2115, 203, 2048, 195,
  17028. 2045, 191, 2041, 213, 209, 2056, 1246, 1244, 1238, 225, 1234, 222, 1256, 1253, 1249, 1262, 2080, 2079, 154, 1997,
  17029. 150, 1995, 147, 1992, 1989, 163, 160, 2004, 156, 2001, 1175, 1174, 1172, 1170, 1167, 170, 1164, 167, 1185, 1183,
  17030. 1180, 1177, 174, 1190, 1188, 2025, 2024, 2022, 587, 586, 564, 559, 556, 2290, 573, 1588, 520, 518, 512, 2268,
  17031. 508, 2265, 530, 1568, 1565, 461, 457, 2233, 450, 2230, 446, 2226, 479, 471, 489, 1526, 1523, 1520, 397, 395,
  17032. 2185, 392, 2183, 389, 2180, 2177, 410, 2194, 402, 422, 1463, 1461, 1459, 1456, 1470, 2455, 799, 2433, 2430, 779,
  17033. 776, 773, 2397, 2394, 2390, 734, 728, 724, 746, 1717, 2356, 2354, 2351, 2348, 1658, 677, 675, 673, 670, 667, 688,
  17034. 1685, 1683, 2606, 2589, 2586, 2559, 2556, 2552, 927, 2523, 2521, 2518, 2515, 1784, 2532, 895, 893, 890, 2718,
  17035. 2709, 2707, 2689, 2687, 2684, 2663, 2662, 2660, 2658, 1825, 2667, 2769, 1852, 2760, 2758, 142, 141, 1139, 1138,
  17036. 134, 132, 129, 126, 1982, 1129, 1128, 1126, 1131, 113, 111, 108, 105, 1972, 101, 1970, 120, 118, 115, 1109, 1108,
  17037. 1106, 1104, 123, 1113, 1111, 82, 79, 1951, 75, 1949, 72, 1946, 92, 89, 86, 1956, 1077, 1076, 1074, 1072, 98,
  17038. 1069, 96, 1084, 1082, 1079, 1088, 1968, 1967, 48, 45, 1916, 42, 1914, 39, 1911, 1908, 60, 57, 54, 1923, 50, 1920,
  17039. 1031, 1030, 1028, 1026, 67, 1023, 65, 1020, 62, 1041, 1039, 1036, 1033, 69, 1046, 1044, 1944, 1943, 1941, 11, 9,
  17040. 1868, 7, 1865, 1862, 1859, 20, 1878, 16, 1875, 13, 1872, 970, 968, 966, 963, 29, 960, 26, 23, 983, 981, 978, 975,
  17041. 33, 971, 31, 990, 988, 985, 1906, 1904, 1902, 993, 351, 2145, 1383, 331, 330, 328, 326, 2137, 323, 2135, 339,
  17042. 1372, 1370, 294, 293, 291, 289, 2122, 286, 2120, 283, 2117, 309, 303, 317, 1348, 1346, 1344, 245, 244, 242, 2090,
  17043. 239, 2088, 236, 2085, 2082, 260, 2099, 249, 270, 1307, 1305, 1303, 1300, 1314, 189, 2038, 186, 2036, 183, 2033,
  17044. 2030, 2026, 206, 198, 2047, 194, 216, 1247, 1245, 1243, 1240, 227, 1237, 1255, 2310, 2302, 2300, 2286, 2284,
  17045. 2281, 565, 563, 561, 558, 575, 1589, 2261, 2259, 2256, 2253, 1542, 521, 519, 517, 514, 2270, 511, 533, 1569,
  17046. 1567, 2223, 2221, 2218, 2215, 1483, 2211, 1480, 459, 456, 453, 2232, 449, 474, 491, 1527, 1525, 1522, 2475, 2467,
  17047. 2465, 2451, 2449, 2446, 801, 800, 2426, 2424, 2421, 2418, 1723, 2435, 780, 778, 775, 2387, 2385, 2382, 2379,
  17048. 1695, 2375, 1693, 2396, 735, 733, 730, 727, 749, 1718, 2616, 2615, 2604, 2603, 2601, 2584, 2583, 2581, 2579,
  17049. 1800, 2591, 2550, 2549, 2547, 2545, 1792, 2542, 1790, 2558, 929, 2719, 1841, 2710, 2708, 1833, 1831, 2690, 2688,
  17050. 2686, 1815, 1809, 1808, 1774, 1756, 1754, 1737, 1736, 1734, 1739, 1816, 1711, 1676, 1674, 633, 629, 1638, 1636,
  17051. 1633, 1641, 598, 1605, 1604, 1602, 1600, 605, 1609, 1607, 2327, 887, 853, 1775, 822, 820, 1757, 1755, 1584, 524,
  17052. 1560, 1558, 468, 464, 1514, 1511, 1508, 1519, 408, 404, 400, 1452, 1447, 1444, 417, 1458, 1455, 2208, 364, 361,
  17053. 358, 2154, 1401, 1400, 1398, 1396, 374, 1393, 371, 1408, 1406, 1403, 1413, 2173, 2172, 772, 726, 723, 1712, 672,
  17054. 669, 666, 682, 1678, 1675, 625, 623, 621, 618, 2331, 636, 632, 1639, 1637, 1635, 920, 918, 884, 880, 889, 849,
  17055. 848, 847, 846, 2497, 855, 852, 1776, 2641, 2742, 2787, 1380, 334, 1367, 1365, 301, 297, 1340, 1338, 1335, 1343,
  17056. 255, 251, 247, 1296, 1291, 1288, 265, 1302, 1299, 2113, 204, 196, 192, 2042, 1232, 1230, 1224, 214, 1220, 210,
  17057. 1242, 1239, 1235, 1250, 2077, 2075, 151, 148, 1993, 144, 1990, 1163, 1162, 1160, 1158, 1155, 161, 1152, 157,
  17058. 1173, 1171, 1168, 1165, 168, 1181, 1178, 2021, 2020, 2018, 2023, 585, 560, 557, 1585, 516, 509, 1562, 1559, 458,
  17059. 447, 2227, 472, 1516, 1513, 1510, 398, 396, 393, 390, 2181, 386, 2178, 407, 1453, 1451, 1449, 1446, 420, 1460,
  17060. 2209, 769, 764, 720, 712, 2391, 729, 1713, 664, 663, 661, 659, 2352, 656, 2349, 671, 1679, 1677, 2553, 922, 919,
  17061. 2519, 2516, 885, 883, 881, 2685, 2661, 2659, 2767, 2756, 2755, 140, 1137, 1136, 130, 127, 1125, 1124, 1122, 1127,
  17062. 109, 106, 102, 1103, 1102, 1100, 1098, 116, 1107, 1105, 1980, 80, 76, 73, 1947, 1068, 1067, 1065, 1063, 90, 1060,
  17063. 87, 1075, 1073, 1070, 1080, 1966, 1965, 46, 43, 40, 1912, 36, 1909, 1019, 1018, 1016, 1014, 58, 1011, 55, 1008,
  17064. 51, 1029, 1027, 1024, 1021, 63, 1037, 1034, 1940, 1939, 1937, 1942, 8, 1866, 4, 1863, 1, 1860, 956, 954, 952,
  17065. 949, 946, 17, 14, 969, 967, 964, 961, 27, 957, 24, 979, 976, 972, 1901, 1900, 1898, 1896, 986, 1905, 1903, 350,
  17066. 349, 1381, 329, 327, 324, 1368, 1366, 292, 290, 287, 284, 2118, 304, 1341, 1339, 1337, 1345, 243, 240, 237, 2086,
  17067. 233, 2083, 254, 1297, 1295, 1293, 1290, 1304, 2114, 190, 187, 184, 2034, 180, 2031, 177, 2027, 199, 1233, 1231,
  17068. 1229, 1226, 217, 1223, 1241, 2078, 2076, 584, 555, 554, 552, 550, 2282, 562, 1586, 507, 506, 504, 502, 2257, 499,
  17069. 2254, 515, 1563, 1561, 445, 443, 441, 2219, 438, 2216, 435, 2212, 460, 454, 475, 1517, 1515, 1512, 2447, 798,
  17070. 797, 2422, 2419, 770, 768, 766, 2383, 2380, 2376, 721, 719, 717, 714, 731, 1714, 2602, 2582, 2580, 2548, 2546,
  17071. 2543, 923, 921, 2717, 2706, 2705, 2683, 2682, 2680, 1771, 1752, 1750, 1733, 1732, 1731, 1735, 1814, 1707, 1670,
  17072. 1668, 1631, 1629, 1626, 1634, 1599, 1598, 1596, 1594, 1603, 1601, 2326, 1772, 1753, 1751, 1581, 1554, 1552, 1504,
  17073. 1501, 1498, 1509, 1442, 1437, 1434, 401, 1448, 1445, 2206, 1392, 1391, 1389, 1387, 1384, 359, 1399, 1397, 1394,
  17074. 1404, 2171, 2170, 1708, 1672, 1669, 619, 1632, 1630, 1628, 1773, 1378, 1363, 1361, 1333, 1328, 1336, 1286, 1281,
  17075. 1278, 248, 1292, 1289, 2111, 1218, 1216, 1210, 197, 1206, 193, 1228, 1225, 1221, 1236, 2073, 2071, 1151, 1150,
  17076. 1148, 1146, 152, 1143, 149, 1140, 145, 1161, 1159, 1156, 1153, 158, 1169, 1166, 2017, 2016, 2014, 2019, 1582,
  17077. 510, 1556, 1553, 452, 448, 1506, 1500, 394, 391, 387, 1443, 1441, 1439, 1436, 1450, 2207, 765, 716, 713, 1709,
  17078. 662, 660, 657, 1673, 1671, 916, 914, 879, 878, 877, 882, 1135, 1134, 1121, 1120, 1118, 1123, 1097, 1096, 1094,
  17079. 1092, 103, 1101, 1099, 1979, 1059, 1058, 1056, 1054, 77, 1051, 74, 1066, 1064, 1061, 1071, 1964, 1963, 1007,
  17080. 1006, 1004, 1002, 999, 41, 996, 37, 1017, 1015, 1012, 1009, 52, 1025, 1022, 1936, 1935, 1933, 1938, 942, 940,
  17081. 938, 935, 932, 5, 2, 955, 953, 950, 947, 18, 943, 15, 965, 962, 958, 1895, 1894, 1892, 1890, 973, 1899, 1897,
  17082. 1379, 325, 1364, 1362, 288, 285, 1334, 1332, 1330, 241, 238, 234, 1287, 1285, 1283, 1280, 1294, 2112, 188, 185,
  17083. 181, 178, 2028, 1219, 1217, 1215, 1212, 200, 1209, 1227, 2074, 2072, 583, 553, 551, 1583, 505, 503, 500, 513,
  17084. 1557, 1555, 444, 442, 439, 436, 2213, 455, 451, 1507, 1505, 1502, 796, 763, 762, 760, 767, 711, 710, 708, 706,
  17085. 2377, 718, 715, 1710, 2544, 917, 915, 2681, 1627, 1597, 1595, 2325, 1769, 1749, 1747, 1499, 1438, 1435, 2204,
  17086. 1390, 1388, 1385, 1395, 2169, 2167, 1704, 1665, 1662, 1625, 1623, 1620, 1770, 1329, 1282, 1279, 2109, 1214, 1207,
  17087. 1222, 2068, 2065, 1149, 1147, 1144, 1141, 146, 1157, 1154, 2013, 2011, 2008, 2015, 1579, 1549, 1546, 1495, 1487,
  17088. 1433, 1431, 1428, 1425, 388, 1440, 2205, 1705, 658, 1667, 1664, 1119, 1095, 1093, 1978, 1057, 1055, 1052, 1062,
  17089. 1962, 1960, 1005, 1003, 1000, 997, 38, 1013, 1010, 1932, 1930, 1927, 1934, 941, 939, 936, 933, 6, 930, 3, 951,
  17090. 948, 944, 1889, 1887, 1884, 1881, 959, 1893, 1891, 35, 1377, 1360, 1358, 1327, 1325, 1322, 1331, 1277, 1275,
  17091. 1272, 1269, 235, 1284, 2110, 1205, 1204, 1201, 1198, 182, 1195, 179, 1213, 2070, 2067, 1580, 501, 1551, 1548,
  17092. 440, 437, 1497, 1494, 1490, 1503, 761, 709, 707, 1706, 913, 912, 2198, 1386, 2164, 2161, 1621, 1766, 2103, 1208,
  17093. 2058, 2054, 1145, 1142, 2005, 2002, 1999, 2009, 1488, 1429, 1426, 2200, 1698, 1659, 1656, 1975, 1053, 1957, 1954,
  17094. 1001, 998, 1924, 1921, 1918, 1928, 937, 934, 931, 1879, 1876, 1873, 1870, 945, 1885, 1882, 1323, 1273, 1270,
  17095. 2105, 1202, 1199, 1196, 1211, 2061, 2057, 1576, 1543, 1540, 1484, 1481, 1478, 1491, 1700
  17096. ]);
  17097. /*
  17098. * Copyright 2007 ZXing authors
  17099. *
  17100. * Licensed under the Apache License, Version 2.0 (the "License");
  17101. * you may not use this file except in compliance with the License.
  17102. * You may obtain a copy of the License at
  17103. *
  17104. * http://www.apache.org/licenses/LICENSE-2.0
  17105. *
  17106. * Unless required by applicable law or agreed to in writing, software
  17107. * distributed under the License is distributed on an "AS IS" BASIS,
  17108. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17109. * See the License for the specific language governing permissions and
  17110. * limitations under the License.
  17111. */
  17112. // import java.util.List;
  17113. /**
  17114. * @author Guenther Grau
  17115. */
  17116. /*public final*/ class PDF417DetectorResult {
  17117. constructor(bits, points) {
  17118. this.bits = bits;
  17119. this.points = points;
  17120. }
  17121. getBits() {
  17122. return this.bits;
  17123. }
  17124. getPoints() {
  17125. return this.points;
  17126. }
  17127. }
  17128. /*
  17129. * Copyright 2009 ZXing authors
  17130. *
  17131. * Licensed under the Apache License, Version 2.0 (the "License");
  17132. * you may not use this file except in compliance with the License.
  17133. * You may obtain a copy of the License at
  17134. *
  17135. * http://www.apache.org/licenses/LICENSE-2.0
  17136. *
  17137. * Unless required by applicable law or agreed to in writing, software
  17138. * distributed under the License is distributed on an "AS IS" BASIS,
  17139. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17140. * See the License for the specific language governing permissions and
  17141. * limitations under the License.
  17142. */
  17143. // import java.util.ArrayList;
  17144. // import java.util.Arrays;
  17145. // import java.util.List;
  17146. // import java.util.Map;
  17147. /**
  17148. * <p>Encapsulates logic that can detect a PDF417 Code in an image, even if the
  17149. * PDF417 Code is rotated or skewed, or partially obscured.</p>
  17150. *
  17151. * @author SITA Lab (kevin.osullivan@sita.aero)
  17152. * @author dswitkin@google.com (Daniel Switkin)
  17153. * @author Guenther Grau
  17154. */
  17155. /*public*/ /*final*/ class Detector {
  17156. /**
  17157. * <p>Detects a PDF417 Code in an image. Only checks 0 and 180 degree rotations.</p>
  17158. *
  17159. * @param image barcode image to decode
  17160. * @param hints optional hints to detector
  17161. * @param multiple if true, then the image is searched for multiple codes. If false, then at most one code will
  17162. * be found and returned
  17163. * @return {@link PDF417DetectorResult} encapsulating results of detecting a PDF417 code
  17164. * @throws NotFoundException if no PDF417 Code can be found
  17165. */
  17166. static detectMultiple(image, hints, multiple) {
  17167. // TODO detection improvement, tryHarder could try several different luminance thresholds/blackpoints or even
  17168. // different binarizers
  17169. // boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
  17170. let bitMatrix = image.getBlackMatrix();
  17171. let barcodeCoordinates = Detector.detect(multiple, bitMatrix);
  17172. if (!barcodeCoordinates.length) {
  17173. bitMatrix = bitMatrix.clone();
  17174. bitMatrix.rotate180();
  17175. barcodeCoordinates = Detector.detect(multiple, bitMatrix);
  17176. }
  17177. return new PDF417DetectorResult(bitMatrix, barcodeCoordinates);
  17178. }
  17179. /**
  17180. * Detects PDF417 codes in an image. Only checks 0 degree rotation
  17181. * @param multiple if true, then the image is searched for multiple codes. If false, then at most one code will
  17182. * be found and returned
  17183. * @param bitMatrix bit matrix to detect barcodes in
  17184. * @return List of ResultPoint arrays containing the coordinates of found barcodes
  17185. */
  17186. static detect(multiple, bitMatrix) {
  17187. const barcodeCoordinates = new Array();
  17188. let row = 0;
  17189. let column = 0;
  17190. let foundBarcodeInRow = false;
  17191. while (row < bitMatrix.getHeight()) {
  17192. const vertices = Detector.findVertices(bitMatrix, row, column);
  17193. if (vertices[0] == null && vertices[3] == null) {
  17194. if (!foundBarcodeInRow) {
  17195. // we didn't find any barcode so that's the end of searching
  17196. break;
  17197. }
  17198. // we didn't find a barcode starting at the given column and row. Try again from the first column and slightly
  17199. // below the lowest barcode we found so far.
  17200. foundBarcodeInRow = false;
  17201. column = 0;
  17202. for (const barcodeCoordinate of barcodeCoordinates) {
  17203. if (barcodeCoordinate[1] != null) {
  17204. row = Math.trunc(Math.max(row, barcodeCoordinate[1].getY()));
  17205. }
  17206. if (barcodeCoordinate[3] != null) {
  17207. row = Math.max(row, Math.trunc(barcodeCoordinate[3].getY()));
  17208. }
  17209. }
  17210. row += Detector.ROW_STEP;
  17211. continue;
  17212. }
  17213. foundBarcodeInRow = true;
  17214. barcodeCoordinates.push(vertices);
  17215. if (!multiple) {
  17216. break;
  17217. }
  17218. // if we didn't find a right row indicator column, then continue the search for the next barcode after the
  17219. // start pattern of the barcode just found.
  17220. if (vertices[2] != null) {
  17221. column = Math.trunc(vertices[2].getX());
  17222. row = Math.trunc(vertices[2].getY());
  17223. }
  17224. else {
  17225. column = Math.trunc(vertices[4].getX());
  17226. row = Math.trunc(vertices[4].getY());
  17227. }
  17228. }
  17229. return barcodeCoordinates;
  17230. }
  17231. /**
  17232. * Locate the vertices and the codewords area of a black blob using the Start
  17233. * and Stop patterns as locators.
  17234. *
  17235. * @param matrix the scanned barcode image.
  17236. * @return an array containing the vertices:
  17237. * vertices[0] x, y top left barcode
  17238. * vertices[1] x, y bottom left barcode
  17239. * vertices[2] x, y top right barcode
  17240. * vertices[3] x, y bottom right barcode
  17241. * vertices[4] x, y top left codeword area
  17242. * vertices[5] x, y bottom left codeword area
  17243. * vertices[6] x, y top right codeword area
  17244. * vertices[7] x, y bottom right codeword area
  17245. */
  17246. static findVertices(matrix, startRow, startColumn) {
  17247. const height = matrix.getHeight();
  17248. const width = matrix.getWidth();
  17249. // const result = new ResultPoint[8];
  17250. const result = new Array(8);
  17251. Detector.copyToResult(result, Detector.findRowsWithPattern(matrix, height, width, startRow, startColumn, Detector.START_PATTERN), Detector.INDEXES_START_PATTERN);
  17252. if (result[4] != null) {
  17253. startColumn = Math.trunc(result[4].getX());
  17254. startRow = Math.trunc(result[4].getY());
  17255. }
  17256. Detector.copyToResult(result, Detector.findRowsWithPattern(matrix, height, width, startRow, startColumn, Detector.STOP_PATTERN), Detector.INDEXES_STOP_PATTERN);
  17257. return result;
  17258. }
  17259. static copyToResult(result, tmpResult, destinationIndexes) {
  17260. for (let i = 0; i < destinationIndexes.length; i++) {
  17261. result[destinationIndexes[i]] = tmpResult[i];
  17262. }
  17263. }
  17264. static findRowsWithPattern(matrix, height, width, startRow, startColumn, pattern) {
  17265. // const result = new ResultPoint[4];
  17266. const result = new Array(4);
  17267. let found = false;
  17268. const counters = new Int32Array(pattern.length);
  17269. for (; startRow < height; startRow += Detector.ROW_STEP) {
  17270. let loc = Detector.findGuardPattern(matrix, startColumn, startRow, width, false, pattern, counters);
  17271. if (loc != null) {
  17272. while (startRow > 0) {
  17273. const previousRowLoc = Detector.findGuardPattern(matrix, startColumn, --startRow, width, false, pattern, counters);
  17274. if (previousRowLoc != null) {
  17275. loc = previousRowLoc;
  17276. }
  17277. else {
  17278. startRow++;
  17279. break;
  17280. }
  17281. }
  17282. result[0] = new ResultPoint(loc[0], startRow);
  17283. result[1] = new ResultPoint(loc[1], startRow);
  17284. found = true;
  17285. break;
  17286. }
  17287. }
  17288. let stopRow = startRow + 1;
  17289. // Last row of the current symbol that contains pattern
  17290. if (found) {
  17291. let skippedRowCount = 0;
  17292. let previousRowLoc = Int32Array.from([Math.trunc(result[0].getX()), Math.trunc(result[1].getX())]);
  17293. for (; stopRow < height; stopRow++) {
  17294. const loc = Detector.findGuardPattern(matrix, previousRowLoc[0], stopRow, width, false, pattern, counters);
  17295. // a found pattern is only considered to belong to the same barcode if the start and end positions
  17296. // don't differ too much. Pattern drift should be not bigger than two for consecutive rows. With
  17297. // a higher number of skipped rows drift could be larger. To keep it simple for now, we allow a slightly
  17298. // larger drift and don't check for skipped rows.
  17299. if (loc != null &&
  17300. Math.abs(previousRowLoc[0] - loc[0]) < Detector.MAX_PATTERN_DRIFT &&
  17301. Math.abs(previousRowLoc[1] - loc[1]) < Detector.MAX_PATTERN_DRIFT) {
  17302. previousRowLoc = loc;
  17303. skippedRowCount = 0;
  17304. }
  17305. else {
  17306. if (skippedRowCount > Detector.SKIPPED_ROW_COUNT_MAX) {
  17307. break;
  17308. }
  17309. else {
  17310. skippedRowCount++;
  17311. }
  17312. }
  17313. }
  17314. stopRow -= skippedRowCount + 1;
  17315. result[2] = new ResultPoint(previousRowLoc[0], stopRow);
  17316. result[3] = new ResultPoint(previousRowLoc[1], stopRow);
  17317. }
  17318. if (stopRow - startRow < Detector.BARCODE_MIN_HEIGHT) {
  17319. Arrays.fill(result, null);
  17320. }
  17321. return result;
  17322. }
  17323. /**
  17324. * @param matrix row of black/white values to search
  17325. * @param column x position to start search
  17326. * @param row y position to start search
  17327. * @param width the number of pixels to search on this row
  17328. * @param pattern pattern of counts of number of black and white pixels that are
  17329. * being searched for as a pattern
  17330. * @param counters array of counters, as long as pattern, to re-use
  17331. * @return start/end horizontal offset of guard pattern, as an array of two ints.
  17332. */
  17333. static findGuardPattern(matrix, column, row, width, whiteFirst, pattern, counters) {
  17334. Arrays.fillWithin(counters, 0, counters.length, 0);
  17335. let patternStart = column;
  17336. let pixelDrift = 0;
  17337. // if there are black pixels left of the current pixel shift to the left, but only for MAX_PIXEL_DRIFT pixels
  17338. while (matrix.get(patternStart, row) && patternStart > 0 && pixelDrift++ < Detector.MAX_PIXEL_DRIFT) {
  17339. patternStart--;
  17340. }
  17341. let x = patternStart;
  17342. let counterPosition = 0;
  17343. let patternLength = pattern.length;
  17344. for (let isWhite = whiteFirst; x < width; x++) {
  17345. let pixel = matrix.get(x, row);
  17346. if (pixel !== isWhite) {
  17347. counters[counterPosition]++;
  17348. }
  17349. else {
  17350. if (counterPosition === patternLength - 1) {
  17351. if (Detector.patternMatchVariance(counters, pattern, Detector.MAX_INDIVIDUAL_VARIANCE) < Detector.MAX_AVG_VARIANCE) {
  17352. return new Int32Array([patternStart, x]);
  17353. }
  17354. patternStart += counters[0] + counters[1];
  17355. System.arraycopy(counters, 2, counters, 0, counterPosition - 1);
  17356. counters[counterPosition - 1] = 0;
  17357. counters[counterPosition] = 0;
  17358. counterPosition--;
  17359. }
  17360. else {
  17361. counterPosition++;
  17362. }
  17363. counters[counterPosition] = 1;
  17364. isWhite = !isWhite;
  17365. }
  17366. }
  17367. if (counterPosition === patternLength - 1 &&
  17368. Detector.patternMatchVariance(counters, pattern, Detector.MAX_INDIVIDUAL_VARIANCE) < Detector.MAX_AVG_VARIANCE) {
  17369. return new Int32Array([patternStart, x - 1]);
  17370. }
  17371. return null;
  17372. }
  17373. /**
  17374. * Determines how closely a set of observed counts of runs of black/white
  17375. * values matches a given target pattern. This is reported as the ratio of
  17376. * the total variance from the expected pattern proportions across all
  17377. * pattern elements, to the length of the pattern.
  17378. *
  17379. * @param counters observed counters
  17380. * @param pattern expected pattern
  17381. * @param maxIndividualVariance The most any counter can differ before we give up
  17382. * @return ratio of total variance between counters and pattern compared to total pattern size
  17383. */
  17384. static patternMatchVariance(counters, pattern, maxIndividualVariance) {
  17385. let numCounters = counters.length;
  17386. let total = 0;
  17387. let patternLength = 0;
  17388. for (let i = 0; i < numCounters; i++) {
  17389. total += counters[i];
  17390. patternLength += pattern[i];
  17391. }
  17392. if (total < patternLength) {
  17393. // If we don't even have one pixel per unit of bar width, assume this
  17394. // is too small to reliably match, so fail:
  17395. return /*Float.POSITIVE_INFINITY*/ Infinity;
  17396. }
  17397. // We're going to fake floating-point math in integers. We just need to use more bits.
  17398. // Scale up patternLength so that intermediate values below like scaledCounter will have
  17399. // more "significant digits".
  17400. let unitBarWidth = total / patternLength;
  17401. maxIndividualVariance *= unitBarWidth;
  17402. let totalVariance = 0.0;
  17403. for (let x = 0; x < numCounters; x++) {
  17404. let counter = counters[x];
  17405. let scaledPattern = pattern[x] * unitBarWidth;
  17406. let variance = counter > scaledPattern ? counter - scaledPattern : scaledPattern - counter;
  17407. if (variance > maxIndividualVariance) {
  17408. return /*Float.POSITIVE_INFINITY*/ Infinity;
  17409. }
  17410. totalVariance += variance;
  17411. }
  17412. return totalVariance / total;
  17413. }
  17414. }
  17415. Detector.INDEXES_START_PATTERN = Int32Array.from([0, 4, 1, 5]);
  17416. Detector.INDEXES_STOP_PATTERN = Int32Array.from([6, 2, 7, 3]);
  17417. Detector.MAX_AVG_VARIANCE = 0.42;
  17418. Detector.MAX_INDIVIDUAL_VARIANCE = 0.8;
  17419. // B S B S B S B S Bar/Space pattern
  17420. // 11111111 0 1 0 1 0 1 000
  17421. Detector.START_PATTERN = Int32Array.from([8, 1, 1, 1, 1, 1, 1, 3]);
  17422. // 1111111 0 1 000 1 0 1 00 1
  17423. Detector.STOP_PATTERN = Int32Array.from([7, 1, 1, 3, 1, 1, 1, 2, 1]);
  17424. Detector.MAX_PIXEL_DRIFT = 3;
  17425. Detector.MAX_PATTERN_DRIFT = 5;
  17426. // if we set the value too low, then we don't detect the correct height of the bar if the start patterns are damaged.
  17427. // if we set the value too high, then we might detect the start pattern from a neighbor barcode.
  17428. Detector.SKIPPED_ROW_COUNT_MAX = 25;
  17429. // A PDF471 barcode should have at least 3 rows, with each row being >= 3 times the module width. Therefore it should be at least
  17430. // 9 pixels tall. To be conservative, we use about half the size to ensure we don't miss it.
  17431. Detector.ROW_STEP = 5;
  17432. Detector.BARCODE_MIN_HEIGHT = 10;
  17433. /*
  17434. * Copyright 2012 ZXing authors
  17435. *
  17436. * Licensed under the Apache License, Version 2.0 (the "License");
  17437. * you may not use this file except in compliance with the License.
  17438. * You may obtain a copy of the License at
  17439. *
  17440. * http://www.apache.org/licenses/LICENSE-2.0
  17441. *
  17442. * Unless required by applicable law or agreed to in writing, software
  17443. * distributed under the License is distributed on an "AS IS" BASIS,
  17444. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17445. * See the License for the specific language governing permissions and
  17446. * limitations under the License.
  17447. */
  17448. /**
  17449. * @author Sean Owen
  17450. * @see com.google.zxing.common.reedsolomon.GenericGFPoly
  17451. */
  17452. /*final*/ class ModulusPoly {
  17453. constructor(field, coefficients) {
  17454. if (coefficients.length === 0) {
  17455. throw new IllegalArgumentException();
  17456. }
  17457. this.field = field;
  17458. let coefficientsLength = /*int*/ coefficients.length;
  17459. if (coefficientsLength > 1 && coefficients[0] === 0) {
  17460. // Leading term must be non-zero for anything except the constant polynomial "0"
  17461. let firstNonZero = /*int*/ 1;
  17462. while (firstNonZero < coefficientsLength && coefficients[firstNonZero] === 0) {
  17463. firstNonZero++;
  17464. }
  17465. if (firstNonZero === coefficientsLength) {
  17466. this.coefficients = new Int32Array([0]);
  17467. }
  17468. else {
  17469. this.coefficients = new Int32Array(coefficientsLength - firstNonZero);
  17470. System.arraycopy(coefficients, firstNonZero, this.coefficients, 0, this.coefficients.length);
  17471. }
  17472. }
  17473. else {
  17474. this.coefficients = coefficients;
  17475. }
  17476. }
  17477. getCoefficients() {
  17478. return this.coefficients;
  17479. }
  17480. /**
  17481. * @return degree of this polynomial
  17482. */
  17483. getDegree() {
  17484. return this.coefficients.length - 1;
  17485. }
  17486. /**
  17487. * @return true iff this polynomial is the monomial "0"
  17488. */
  17489. isZero() {
  17490. return this.coefficients[0] === 0;
  17491. }
  17492. /**
  17493. * @return coefficient of x^degree term in this polynomial
  17494. */
  17495. getCoefficient(degree) {
  17496. return this.coefficients[this.coefficients.length - 1 - degree];
  17497. }
  17498. /**
  17499. * @return evaluation of this polynomial at a given point
  17500. */
  17501. evaluateAt(a) {
  17502. if (a === 0) {
  17503. // Just return the x^0 coefficient
  17504. return this.getCoefficient(0);
  17505. }
  17506. if (a === 1) {
  17507. // Just the sum of the coefficients
  17508. let sum = /*int*/ 0;
  17509. for (let coefficient /*int*/ of this.coefficients) {
  17510. sum = this.field.add(sum, coefficient);
  17511. }
  17512. return sum;
  17513. }
  17514. let result = /*int*/ this.coefficients[0];
  17515. let size = /*int*/ this.coefficients.length;
  17516. for (let i /*int*/ = 1; i < size; i++) {
  17517. result = this.field.add(this.field.multiply(a, result), this.coefficients[i]);
  17518. }
  17519. return result;
  17520. }
  17521. add(other) {
  17522. if (!this.field.equals(other.field)) {
  17523. throw new IllegalArgumentException('ModulusPolys do not have same ModulusGF field');
  17524. }
  17525. if (this.isZero()) {
  17526. return other;
  17527. }
  17528. if (other.isZero()) {
  17529. return this;
  17530. }
  17531. let smallerCoefficients = this.coefficients;
  17532. let largerCoefficients = other.coefficients;
  17533. if (smallerCoefficients.length > largerCoefficients.length) {
  17534. let temp = smallerCoefficients;
  17535. smallerCoefficients = largerCoefficients;
  17536. largerCoefficients = temp;
  17537. }
  17538. let sumDiff = new Int32Array(largerCoefficients.length);
  17539. let lengthDiff = /*int*/ largerCoefficients.length - smallerCoefficients.length;
  17540. // Copy high-order terms only found in higher-degree polynomial's coefficients
  17541. System.arraycopy(largerCoefficients, 0, sumDiff, 0, lengthDiff);
  17542. for (let i /*int*/ = lengthDiff; i < largerCoefficients.length; i++) {
  17543. sumDiff[i] = this.field.add(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
  17544. }
  17545. return new ModulusPoly(this.field, sumDiff);
  17546. }
  17547. subtract(other) {
  17548. if (!this.field.equals(other.field)) {
  17549. throw new IllegalArgumentException('ModulusPolys do not have same ModulusGF field');
  17550. }
  17551. if (other.isZero()) {
  17552. return this;
  17553. }
  17554. return this.add(other.negative());
  17555. }
  17556. multiply(other) {
  17557. if (other instanceof ModulusPoly) {
  17558. return this.multiplyOther(other);
  17559. }
  17560. return this.multiplyScalar(other);
  17561. }
  17562. multiplyOther(other) {
  17563. if (!this.field.equals(other.field)) {
  17564. throw new IllegalArgumentException('ModulusPolys do not have same ModulusGF field');
  17565. }
  17566. if (this.isZero() || other.isZero()) {
  17567. // return this.field.getZero();
  17568. return new ModulusPoly(this.field, new Int32Array([0]));
  17569. }
  17570. let aCoefficients = this.coefficients;
  17571. let aLength = /*int*/ aCoefficients.length;
  17572. let bCoefficients = other.coefficients;
  17573. let bLength = /*int*/ bCoefficients.length;
  17574. let product = new Int32Array(aLength + bLength - 1);
  17575. for (let i /*int*/ = 0; i < aLength; i++) {
  17576. let aCoeff = /*int*/ aCoefficients[i];
  17577. for (let j /*int*/ = 0; j < bLength; j++) {
  17578. product[i + j] = this.field.add(product[i + j], this.field.multiply(aCoeff, bCoefficients[j]));
  17579. }
  17580. }
  17581. return new ModulusPoly(this.field, product);
  17582. }
  17583. negative() {
  17584. let size = /*int*/ this.coefficients.length;
  17585. let negativeCoefficients = new Int32Array(size);
  17586. for (let i /*int*/ = 0; i < size; i++) {
  17587. negativeCoefficients[i] = this.field.subtract(0, this.coefficients[i]);
  17588. }
  17589. return new ModulusPoly(this.field, negativeCoefficients);
  17590. }
  17591. multiplyScalar(scalar) {
  17592. if (scalar === 0) {
  17593. return new ModulusPoly(this.field, new Int32Array([0]));
  17594. }
  17595. if (scalar === 1) {
  17596. return this;
  17597. }
  17598. let size = /*int*/ this.coefficients.length;
  17599. let product = new Int32Array(size);
  17600. for (let i /*int*/ = 0; i < size; i++) {
  17601. product[i] = this.field.multiply(this.coefficients[i], scalar);
  17602. }
  17603. return new ModulusPoly(this.field, product);
  17604. }
  17605. multiplyByMonomial(degree, coefficient) {
  17606. if (degree < 0) {
  17607. throw new IllegalArgumentException();
  17608. }
  17609. if (coefficient === 0) {
  17610. return new ModulusPoly(this.field, new Int32Array([0]));
  17611. }
  17612. let size = /*int*/ this.coefficients.length;
  17613. let product = new Int32Array(size + degree);
  17614. for (let i /*int*/ = 0; i < size; i++) {
  17615. product[i] = this.field.multiply(this.coefficients[i], coefficient);
  17616. }
  17617. return new ModulusPoly(this.field, product);
  17618. }
  17619. /*
  17620. ModulusPoly[] divide(other: ModulusPoly) {
  17621. if (!field.equals(other.field)) {
  17622. throw new IllegalArgumentException("ModulusPolys do not have same ModulusGF field");
  17623. }
  17624. if (other.isZero()) {
  17625. throw new IllegalArgumentException("Divide by 0");
  17626. }
  17627. let quotient: ModulusPoly = field.getZero();
  17628. let remainder: ModulusPoly = this;
  17629. let denominatorLeadingTerm: /*int/ number = other.getCoefficient(other.getDegree());
  17630. let inverseDenominatorLeadingTerm: /*int/ number = field.inverse(denominatorLeadingTerm);
  17631. while (remainder.getDegree() >= other.getDegree() && !remainder.isZero()) {
  17632. let degreeDifference: /*int/ number = remainder.getDegree() - other.getDegree();
  17633. let scale: /*int/ number = field.multiply(remainder.getCoefficient(remainder.getDegree()), inverseDenominatorLeadingTerm);
  17634. let term: ModulusPoly = other.multiplyByMonomial(degreeDifference, scale);
  17635. let iterationQuotient: ModulusPoly = field.buildMonomial(degreeDifference, scale);
  17636. quotient = quotient.add(iterationQuotient);
  17637. remainder = remainder.subtract(term);
  17638. }
  17639. return new ModulusPoly[] { quotient, remainder };
  17640. }
  17641. */
  17642. // @Override
  17643. toString() {
  17644. let result = new StringBuilder( /*8 * this.getDegree()*/); // dynamic string size in JS
  17645. for (let degree /*int*/ = this.getDegree(); degree >= 0; degree--) {
  17646. let coefficient = /*int*/ this.getCoefficient(degree);
  17647. if (coefficient !== 0) {
  17648. if (coefficient < 0) {
  17649. result.append(' - ');
  17650. coefficient = -coefficient;
  17651. }
  17652. else {
  17653. if (result.length() > 0) {
  17654. result.append(' + ');
  17655. }
  17656. }
  17657. if (degree === 0 || coefficient !== 1) {
  17658. result.append(coefficient);
  17659. }
  17660. if (degree !== 0) {
  17661. if (degree === 1) {
  17662. result.append('x');
  17663. }
  17664. else {
  17665. result.append('x^');
  17666. result.append(degree);
  17667. }
  17668. }
  17669. }
  17670. }
  17671. return result.toString();
  17672. }
  17673. }
  17674. class ModulusBase {
  17675. add(a, b) {
  17676. return (a + b) % this.modulus;
  17677. }
  17678. subtract(a, b) {
  17679. return (this.modulus + a - b) % this.modulus;
  17680. }
  17681. exp(a) {
  17682. return this.expTable[a];
  17683. }
  17684. log(a) {
  17685. if (a === 0) {
  17686. throw new IllegalArgumentException();
  17687. }
  17688. return this.logTable[a];
  17689. }
  17690. inverse(a) {
  17691. if (a === 0) {
  17692. throw new ArithmeticException();
  17693. }
  17694. return this.expTable[this.modulus - this.logTable[a] - 1];
  17695. }
  17696. multiply(a, b) {
  17697. if (a === 0 || b === 0) {
  17698. return 0;
  17699. }
  17700. return this.expTable[(this.logTable[a] + this.logTable[b]) % (this.modulus - 1)];
  17701. }
  17702. getSize() {
  17703. return this.modulus;
  17704. }
  17705. equals(o) {
  17706. return o === this;
  17707. }
  17708. }
  17709. /*
  17710. * Copyright 2012 ZXing authors
  17711. *
  17712. * Licensed under the Apache License, Version 2.0 (the "License");
  17713. * you may not use this file except in compliance with the License.
  17714. * You may obtain a copy of the License at
  17715. *
  17716. * http://www.apache.org/licenses/LICENSE-2.0
  17717. *
  17718. * Unless required by applicable law or agreed to in writing, software
  17719. * distributed under the License is distributed on an "AS IS" BASIS,
  17720. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17721. * See the License for the specific language governing permissions and
  17722. * limitations under the License.
  17723. */
  17724. /**
  17725. * <p>A field based on powers of a generator integer, modulo some modulus.</p>
  17726. *
  17727. * @author Sean Owen
  17728. * @see com.google.zxing.common.reedsolomon.GenericGF
  17729. */
  17730. /*public final*/ class ModulusGF extends ModulusBase {
  17731. // private /*final*/ modulus: /*int*/ number;
  17732. constructor(modulus, generator) {
  17733. super();
  17734. this.modulus = modulus;
  17735. this.expTable = new Int32Array(modulus);
  17736. this.logTable = new Int32Array(modulus);
  17737. let x = /*int*/ 1;
  17738. for (let i /*int*/ = 0; i < modulus; i++) {
  17739. this.expTable[i] = x;
  17740. x = (x * generator) % modulus;
  17741. }
  17742. for (let i /*int*/ = 0; i < modulus - 1; i++) {
  17743. this.logTable[this.expTable[i]] = i;
  17744. }
  17745. // logTable[0] == 0 but this should never be used
  17746. this.zero = new ModulusPoly(this, new Int32Array([0]));
  17747. this.one = new ModulusPoly(this, new Int32Array([1]));
  17748. }
  17749. getZero() {
  17750. return this.zero;
  17751. }
  17752. getOne() {
  17753. return this.one;
  17754. }
  17755. buildMonomial(degree, coefficient) {
  17756. if (degree < 0) {
  17757. throw new IllegalArgumentException();
  17758. }
  17759. if (coefficient === 0) {
  17760. return this.zero;
  17761. }
  17762. let coefficients = new Int32Array(degree + 1);
  17763. coefficients[0] = coefficient;
  17764. return new ModulusPoly(this, coefficients);
  17765. }
  17766. }
  17767. ModulusGF.PDF417_GF = new ModulusGF(PDF417Common.NUMBER_OF_CODEWORDS, 3);
  17768. /*
  17769. * Copyright 2012 ZXing authors
  17770. *
  17771. * Licensed under the Apache License, Version 2.0 (the "License");
  17772. * you may not use this file except in compliance with the License.
  17773. * You may obtain a copy of the License at
  17774. *
  17775. * http://www.apache.org/licenses/LICENSE-2.0
  17776. *
  17777. * Unless required by applicable law or agreed to in writing, software
  17778. * distributed under the License is distributed on an "AS IS" BASIS,
  17779. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17780. * See the License for the specific language governing permissions and
  17781. * limitations under the License.
  17782. */
  17783. /**
  17784. * <p>PDF417 error correction implementation.</p>
  17785. *
  17786. * <p>This <a href="http://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction#Example">example</a>
  17787. * is quite useful in understanding the algorithm.</p>
  17788. *
  17789. * @author Sean Owen
  17790. * @see com.google.zxing.common.reedsolomon.ReedSolomonDecoder
  17791. */
  17792. /*public final*/ class ErrorCorrection$1 {
  17793. constructor() {
  17794. this.field = ModulusGF.PDF417_GF;
  17795. }
  17796. /**
  17797. * @param received received codewords
  17798. * @param numECCodewords number of those codewords used for EC
  17799. * @param erasures location of erasures
  17800. * @return number of errors
  17801. * @throws ChecksumException if errors cannot be corrected, maybe because of too many errors
  17802. */
  17803. decode(received, numECCodewords, erasures) {
  17804. let poly = new ModulusPoly(this.field, received);
  17805. let S = new Int32Array(numECCodewords);
  17806. let error = false;
  17807. for (let i /*int*/ = numECCodewords; i > 0; i--) {
  17808. let evaluation = poly.evaluateAt(this.field.exp(i));
  17809. S[numECCodewords - i] = evaluation;
  17810. if (evaluation !== 0) {
  17811. error = true;
  17812. }
  17813. }
  17814. if (!error) {
  17815. return 0;
  17816. }
  17817. let knownErrors = this.field.getOne();
  17818. if (erasures != null) {
  17819. for (const erasure of erasures) {
  17820. let b = this.field.exp(received.length - 1 - erasure);
  17821. // Add (1 - bx) term:
  17822. let term = new ModulusPoly(this.field, new Int32Array([this.field.subtract(0, b), 1]));
  17823. knownErrors = knownErrors.multiply(term);
  17824. }
  17825. }
  17826. let syndrome = new ModulusPoly(this.field, S);
  17827. // syndrome = syndrome.multiply(knownErrors);
  17828. let sigmaOmega = this.runEuclideanAlgorithm(this.field.buildMonomial(numECCodewords, 1), syndrome, numECCodewords);
  17829. let sigma = sigmaOmega[0];
  17830. let omega = sigmaOmega[1];
  17831. // sigma = sigma.multiply(knownErrors);
  17832. let errorLocations = this.findErrorLocations(sigma);
  17833. let errorMagnitudes = this.findErrorMagnitudes(omega, sigma, errorLocations);
  17834. for (let i /*int*/ = 0; i < errorLocations.length; i++) {
  17835. let position = received.length - 1 - this.field.log(errorLocations[i]);
  17836. if (position < 0) {
  17837. throw ChecksumException.getChecksumInstance();
  17838. }
  17839. received[position] = this.field.subtract(received[position], errorMagnitudes[i]);
  17840. }
  17841. return errorLocations.length;
  17842. }
  17843. /**
  17844. *
  17845. * @param ModulusPoly
  17846. * @param a
  17847. * @param ModulusPoly
  17848. * @param b
  17849. * @param int
  17850. * @param R
  17851. * @throws ChecksumException
  17852. */
  17853. runEuclideanAlgorithm(a, b, R) {
  17854. // Assume a's degree is >= b's
  17855. if (a.getDegree() < b.getDegree()) {
  17856. let temp = a;
  17857. a = b;
  17858. b = temp;
  17859. }
  17860. let rLast = a;
  17861. let r = b;
  17862. let tLast = this.field.getZero();
  17863. let t = this.field.getOne();
  17864. // Run Euclidean algorithm until r's degree is less than R/2
  17865. while (r.getDegree() >= Math.round(R / 2)) {
  17866. let rLastLast = rLast;
  17867. let tLastLast = tLast;
  17868. rLast = r;
  17869. tLast = t;
  17870. // Divide rLastLast by rLast, with quotient in q and remainder in r
  17871. if (rLast.isZero()) {
  17872. // Oops, Euclidean algorithm already terminated?
  17873. throw ChecksumException.getChecksumInstance();
  17874. }
  17875. r = rLastLast;
  17876. let q = this.field.getZero();
  17877. let denominatorLeadingTerm = rLast.getCoefficient(rLast.getDegree());
  17878. let dltInverse = this.field.inverse(denominatorLeadingTerm);
  17879. while (r.getDegree() >= rLast.getDegree() && !r.isZero()) {
  17880. let degreeDiff = r.getDegree() - rLast.getDegree();
  17881. let scale = this.field.multiply(r.getCoefficient(r.getDegree()), dltInverse);
  17882. q = q.add(this.field.buildMonomial(degreeDiff, scale));
  17883. r = r.subtract(rLast.multiplyByMonomial(degreeDiff, scale));
  17884. }
  17885. t = q.multiply(tLast).subtract(tLastLast).negative();
  17886. }
  17887. let sigmaTildeAtZero = t.getCoefficient(0);
  17888. if (sigmaTildeAtZero === 0) {
  17889. throw ChecksumException.getChecksumInstance();
  17890. }
  17891. let inverse = this.field.inverse(sigmaTildeAtZero);
  17892. let sigma = t.multiply(inverse);
  17893. let omega = r.multiply(inverse);
  17894. return [sigma, omega];
  17895. }
  17896. /**
  17897. *
  17898. * @param errorLocator
  17899. * @throws ChecksumException
  17900. */
  17901. findErrorLocations(errorLocator) {
  17902. // This is a direct application of Chien's search
  17903. let numErrors = errorLocator.getDegree();
  17904. let result = new Int32Array(numErrors);
  17905. let e = 0;
  17906. for (let i /*int*/ = 1; i < this.field.getSize() && e < numErrors; i++) {
  17907. if (errorLocator.evaluateAt(i) === 0) {
  17908. result[e] = this.field.inverse(i);
  17909. e++;
  17910. }
  17911. }
  17912. if (e !== numErrors) {
  17913. throw ChecksumException.getChecksumInstance();
  17914. }
  17915. return result;
  17916. }
  17917. findErrorMagnitudes(errorEvaluator, errorLocator, errorLocations) {
  17918. let errorLocatorDegree = errorLocator.getDegree();
  17919. let formalDerivativeCoefficients = new Int32Array(errorLocatorDegree);
  17920. for (let i /*int*/ = 1; i <= errorLocatorDegree; i++) {
  17921. formalDerivativeCoefficients[errorLocatorDegree - i] =
  17922. this.field.multiply(i, errorLocator.getCoefficient(i));
  17923. }
  17924. let formalDerivative = new ModulusPoly(this.field, formalDerivativeCoefficients);
  17925. // This is directly applying Forney's Formula
  17926. let s = errorLocations.length;
  17927. let result = new Int32Array(s);
  17928. for (let i /*int*/ = 0; i < s; i++) {
  17929. let xiInverse = this.field.inverse(errorLocations[i]);
  17930. let numerator = this.field.subtract(0, errorEvaluator.evaluateAt(xiInverse));
  17931. let denominator = this.field.inverse(formalDerivative.evaluateAt(xiInverse));
  17932. result[i] = this.field.multiply(numerator, denominator);
  17933. }
  17934. return result;
  17935. }
  17936. }
  17937. /*
  17938. * Copyright 2013 ZXing authors
  17939. *
  17940. * Licensed under the Apache License, Version 2.0 (the "License");
  17941. * you may not use this file except in compliance with the License.
  17942. * You may obtain a copy of the License at
  17943. *
  17944. * http://www.apache.org/licenses/LICENSE-2.0
  17945. *
  17946. * Unless required by applicable law or agreed to in writing, software
  17947. * distributed under the License is distributed on an "AS IS" BASIS,
  17948. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17949. * See the License for the specific language governing permissions and
  17950. * limitations under the License.
  17951. */
  17952. /**
  17953. * @author Guenther Grau
  17954. */
  17955. /*final*/ class BoundingBox {
  17956. constructor(image, topLeft, bottomLeft, topRight, bottomRight) {
  17957. if (image instanceof BoundingBox) {
  17958. this.constructor_2(image);
  17959. }
  17960. else {
  17961. this.constructor_1(image, topLeft, bottomLeft, topRight, bottomRight);
  17962. }
  17963. }
  17964. /**
  17965. *
  17966. * @param image
  17967. * @param topLeft
  17968. * @param bottomLeft
  17969. * @param topRight
  17970. * @param bottomRight
  17971. *
  17972. * @throws NotFoundException
  17973. */
  17974. constructor_1(image, topLeft, bottomLeft, topRight, bottomRight) {
  17975. const leftUnspecified = topLeft == null || bottomLeft == null;
  17976. const rightUnspecified = topRight == null || bottomRight == null;
  17977. if (leftUnspecified && rightUnspecified) {
  17978. throw new NotFoundException();
  17979. }
  17980. if (leftUnspecified) {
  17981. topLeft = new ResultPoint(0, topRight.getY());
  17982. bottomLeft = new ResultPoint(0, bottomRight.getY());
  17983. }
  17984. else if (rightUnspecified) {
  17985. topRight = new ResultPoint(image.getWidth() - 1, topLeft.getY());
  17986. bottomRight = new ResultPoint(image.getWidth() - 1, bottomLeft.getY());
  17987. }
  17988. this.image = image;
  17989. this.topLeft = topLeft;
  17990. this.bottomLeft = bottomLeft;
  17991. this.topRight = topRight;
  17992. this.bottomRight = bottomRight;
  17993. this.minX = Math.trunc(Math.min(topLeft.getX(), bottomLeft.getX()));
  17994. this.maxX = Math.trunc(Math.max(topRight.getX(), bottomRight.getX()));
  17995. this.minY = Math.trunc(Math.min(topLeft.getY(), topRight.getY()));
  17996. this.maxY = Math.trunc(Math.max(bottomLeft.getY(), bottomRight.getY()));
  17997. }
  17998. constructor_2(boundingBox) {
  17999. this.image = boundingBox.image;
  18000. this.topLeft = boundingBox.getTopLeft();
  18001. this.bottomLeft = boundingBox.getBottomLeft();
  18002. this.topRight = boundingBox.getTopRight();
  18003. this.bottomRight = boundingBox.getBottomRight();
  18004. this.minX = boundingBox.getMinX();
  18005. this.maxX = boundingBox.getMaxX();
  18006. this.minY = boundingBox.getMinY();
  18007. this.maxY = boundingBox.getMaxY();
  18008. }
  18009. /**
  18010. * @throws NotFoundException
  18011. */
  18012. static merge(leftBox, rightBox) {
  18013. if (leftBox == null) {
  18014. return rightBox;
  18015. }
  18016. if (rightBox == null) {
  18017. return leftBox;
  18018. }
  18019. return new BoundingBox(leftBox.image, leftBox.topLeft, leftBox.bottomLeft, rightBox.topRight, rightBox.bottomRight);
  18020. }
  18021. /**
  18022. * @throws NotFoundException
  18023. */
  18024. addMissingRows(missingStartRows, missingEndRows, isLeft) {
  18025. let newTopLeft = this.topLeft;
  18026. let newBottomLeft = this.bottomLeft;
  18027. let newTopRight = this.topRight;
  18028. let newBottomRight = this.bottomRight;
  18029. if (missingStartRows > 0) {
  18030. let top = isLeft ? this.topLeft : this.topRight;
  18031. let newMinY = Math.trunc(top.getY() - missingStartRows);
  18032. if (newMinY < 0) {
  18033. newMinY = 0;
  18034. }
  18035. let newTop = new ResultPoint(top.getX(), newMinY);
  18036. if (isLeft) {
  18037. newTopLeft = newTop;
  18038. }
  18039. else {
  18040. newTopRight = newTop;
  18041. }
  18042. }
  18043. if (missingEndRows > 0) {
  18044. let bottom = isLeft ? this.bottomLeft : this.bottomRight;
  18045. let newMaxY = Math.trunc(bottom.getY() + missingEndRows);
  18046. if (newMaxY >= this.image.getHeight()) {
  18047. newMaxY = this.image.getHeight() - 1;
  18048. }
  18049. let newBottom = new ResultPoint(bottom.getX(), newMaxY);
  18050. if (isLeft) {
  18051. newBottomLeft = newBottom;
  18052. }
  18053. else {
  18054. newBottomRight = newBottom;
  18055. }
  18056. }
  18057. return new BoundingBox(this.image, newTopLeft, newBottomLeft, newTopRight, newBottomRight);
  18058. }
  18059. getMinX() {
  18060. return this.minX;
  18061. }
  18062. getMaxX() {
  18063. return this.maxX;
  18064. }
  18065. getMinY() {
  18066. return this.minY;
  18067. }
  18068. getMaxY() {
  18069. return this.maxY;
  18070. }
  18071. getTopLeft() {
  18072. return this.topLeft;
  18073. }
  18074. getTopRight() {
  18075. return this.topRight;
  18076. }
  18077. getBottomLeft() {
  18078. return this.bottomLeft;
  18079. }
  18080. getBottomRight() {
  18081. return this.bottomRight;
  18082. }
  18083. }
  18084. /*
  18085. * Copyright 2013 ZXing authors
  18086. *
  18087. * Licensed under the Apache License, Version 2.0 (the "License");
  18088. * you may not use this file except in compliance with the License.
  18089. * You may obtain a copy of the License at
  18090. *
  18091. * http://www.apache.org/licenses/LICENSE-2.0
  18092. *
  18093. * Unless required by applicable law or agreed to in writing, software
  18094. * distributed under the License is distributed on an "AS IS" BASIS,
  18095. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18096. * See the License for the specific language governing permissions and
  18097. * limitations under the License.
  18098. */
  18099. // package com.google.zxing.pdf417.decoder;
  18100. /**
  18101. * @author Guenther Grau
  18102. */
  18103. /*final*/ class BarcodeMetadata {
  18104. constructor(columnCount, rowCountUpperPart, rowCountLowerPart, errorCorrectionLevel) {
  18105. this.columnCount = columnCount;
  18106. this.errorCorrectionLevel = errorCorrectionLevel;
  18107. this.rowCountUpperPart = rowCountUpperPart;
  18108. this.rowCountLowerPart = rowCountLowerPart;
  18109. this.rowCount = rowCountUpperPart + rowCountLowerPart;
  18110. }
  18111. getColumnCount() {
  18112. return this.columnCount;
  18113. }
  18114. getErrorCorrectionLevel() {
  18115. return this.errorCorrectionLevel;
  18116. }
  18117. getRowCount() {
  18118. return this.rowCount;
  18119. }
  18120. getRowCountUpperPart() {
  18121. return this.rowCountUpperPart;
  18122. }
  18123. getRowCountLowerPart() {
  18124. return this.rowCountLowerPart;
  18125. }
  18126. }
  18127. /**
  18128. * Java Formatter class polyfill that works in the JS way.
  18129. */
  18130. class Formatter {
  18131. constructor() {
  18132. this.buffer = '';
  18133. }
  18134. /**
  18135. *
  18136. * @see https://stackoverflow.com/a/13439711/4367683
  18137. *
  18138. * @param str
  18139. * @param arr
  18140. */
  18141. static form(str, arr) {
  18142. let i = -1;
  18143. function callback(exp, p0, p1, p2, p3, p4) {
  18144. if (exp === '%%')
  18145. return '%';
  18146. if (arr[++i] === undefined)
  18147. return undefined;
  18148. exp = p2 ? parseInt(p2.substr(1)) : undefined;
  18149. let base = p3 ? parseInt(p3.substr(1)) : undefined;
  18150. let val;
  18151. switch (p4) {
  18152. case 's':
  18153. val = arr[i];
  18154. break;
  18155. case 'c':
  18156. val = arr[i][0];
  18157. break;
  18158. case 'f':
  18159. val = parseFloat(arr[i]).toFixed(exp);
  18160. break;
  18161. case 'p':
  18162. val = parseFloat(arr[i]).toPrecision(exp);
  18163. break;
  18164. case 'e':
  18165. val = parseFloat(arr[i]).toExponential(exp);
  18166. break;
  18167. case 'x':
  18168. val = parseInt(arr[i]).toString(base ? base : 16);
  18169. break;
  18170. case 'd':
  18171. val = parseFloat(parseInt(arr[i], base ? base : 10).toPrecision(exp)).toFixed(0);
  18172. break;
  18173. }
  18174. val = typeof val === 'object' ? JSON.stringify(val) : (+val).toString(base);
  18175. let size = parseInt(p1); /* padding size */
  18176. let ch = p1 && (p1[0] + '') === '0' ? '0' : ' '; /* isnull? */
  18177. while (val.length < size)
  18178. val = p0 !== undefined ? val + ch : ch + val; /* isminus? */
  18179. return val;
  18180. }
  18181. let regex = /%(-)?(0?[0-9]+)?([.][0-9]+)?([#][0-9]+)?([scfpexd%])/g;
  18182. return str.replace(regex, callback);
  18183. }
  18184. /**
  18185. *
  18186. * @param append The new string to append.
  18187. * @param args Argumets values to be formated.
  18188. */
  18189. format(append, ...args) {
  18190. this.buffer += Formatter.form(append, args);
  18191. }
  18192. /**
  18193. * Returns the Formatter string value.
  18194. */
  18195. toString() {
  18196. return this.buffer;
  18197. }
  18198. }
  18199. /*
  18200. * Copyright 2013 ZXing authors
  18201. *
  18202. * Licensed under the Apache License, Version 2.0 (the "License");
  18203. * you may not use this file except in compliance with the License.
  18204. * You may obtain a copy of the License at
  18205. *
  18206. * http://www.apache.org/licenses/LICENSE-2.0
  18207. *
  18208. * Unless required by applicable law or agreed to in writing, software
  18209. * distributed under the License is distributed on an "AS IS" BASIS,
  18210. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18211. * See the License for the specific language governing permissions and
  18212. * limitations under the License.
  18213. */
  18214. /**
  18215. * @author Guenther Grau
  18216. */
  18217. class DetectionResultColumn {
  18218. constructor(boundingBox) {
  18219. this.boundingBox = new BoundingBox(boundingBox);
  18220. // this.codewords = new Codeword[boundingBox.getMaxY() - boundingBox.getMinY() + 1];
  18221. this.codewords = new Array(boundingBox.getMaxY() - boundingBox.getMinY() + 1);
  18222. }
  18223. /*final*/ getCodewordNearby(imageRow) {
  18224. let codeword = this.getCodeword(imageRow);
  18225. if (codeword != null) {
  18226. return codeword;
  18227. }
  18228. for (let i = 1; i < DetectionResultColumn.MAX_NEARBY_DISTANCE; i++) {
  18229. let nearImageRow = this.imageRowToCodewordIndex(imageRow) - i;
  18230. if (nearImageRow >= 0) {
  18231. codeword = this.codewords[nearImageRow];
  18232. if (codeword != null) {
  18233. return codeword;
  18234. }
  18235. }
  18236. nearImageRow = this.imageRowToCodewordIndex(imageRow) + i;
  18237. if (nearImageRow < this.codewords.length) {
  18238. codeword = this.codewords[nearImageRow];
  18239. if (codeword != null) {
  18240. return codeword;
  18241. }
  18242. }
  18243. }
  18244. return null;
  18245. }
  18246. /*final int*/ imageRowToCodewordIndex(imageRow) {
  18247. return imageRow - this.boundingBox.getMinY();
  18248. }
  18249. /*final void*/ setCodeword(imageRow, codeword) {
  18250. this.codewords[this.imageRowToCodewordIndex(imageRow)] = codeword;
  18251. }
  18252. /*final*/ getCodeword(imageRow) {
  18253. return this.codewords[this.imageRowToCodewordIndex(imageRow)];
  18254. }
  18255. /*final*/ getBoundingBox() {
  18256. return this.boundingBox;
  18257. }
  18258. /*final*/ getCodewords() {
  18259. return this.codewords;
  18260. }
  18261. // @Override
  18262. toString() {
  18263. const formatter = new Formatter();
  18264. let row = 0;
  18265. for (const codeword of this.codewords) {
  18266. if (codeword == null) {
  18267. formatter.format('%3d: | %n', row++);
  18268. continue;
  18269. }
  18270. formatter.format('%3d: %3d|%3d%n', row++, codeword.getRowNumber(), codeword.getValue());
  18271. }
  18272. return formatter.toString();
  18273. }
  18274. }
  18275. DetectionResultColumn.MAX_NEARBY_DISTANCE = 5;
  18276. /*
  18277. * Copyright 2013 ZXing authors
  18278. *
  18279. * Licensed under the Apache License, Version 2.0 (the "License");
  18280. * you may not use this file except in compliance with the License.
  18281. * You may obtain a copy of the License at
  18282. *
  18283. * http://www.apache.org/licenses/LICENSE-2.0
  18284. *
  18285. * Unless required by applicable law or agreed to in writing, software
  18286. * distributed under the License is distributed on an "AS IS" BASIS,
  18287. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18288. * See the License for the specific language governing permissions and
  18289. * limitations under the License.
  18290. */
  18291. // import java.util.ArrayList;
  18292. // import java.util.Collection;
  18293. // import java.util.HashMap;
  18294. // import java.util.Map;
  18295. // import java.util.Map.Entry;
  18296. /**
  18297. * @author Guenther Grau
  18298. */
  18299. /*final*/ class BarcodeValue {
  18300. constructor() {
  18301. this.values = new Map();
  18302. }
  18303. /**
  18304. * Add an occurrence of a value
  18305. */
  18306. setValue(value) {
  18307. value = Math.trunc(value);
  18308. let confidence = this.values.get(value);
  18309. if (confidence == null) {
  18310. confidence = 0;
  18311. }
  18312. confidence++;
  18313. this.values.set(value, confidence);
  18314. }
  18315. /**
  18316. * Determines the maximum occurrence of a set value and returns all values which were set with this occurrence.
  18317. * @return an array of int, containing the values with the highest occurrence, or null, if no value was set
  18318. */
  18319. getValue() {
  18320. let maxConfidence = -1;
  18321. let result = new Array();
  18322. for (const [key, value] of this.values.entries()) {
  18323. const entry = {
  18324. getKey: () => key,
  18325. getValue: () => value,
  18326. };
  18327. if (entry.getValue() > maxConfidence) {
  18328. maxConfidence = entry.getValue();
  18329. result = [];
  18330. result.push(entry.getKey());
  18331. }
  18332. else if (entry.getValue() === maxConfidence) {
  18333. result.push(entry.getKey());
  18334. }
  18335. }
  18336. return PDF417Common.toIntArray(result);
  18337. }
  18338. getConfidence(value) {
  18339. return this.values.get(value);
  18340. }
  18341. }
  18342. /*
  18343. * Copyright 2013 ZXing authors
  18344. *
  18345. * Licensed under the Apache License, Version 2.0 (the "License");
  18346. * you may not use this file except in compliance with the License.
  18347. * You may obtain a copy of the License at
  18348. *
  18349. * http://www.apache.org/licenses/LICENSE-2.0
  18350. *
  18351. * Unless required by applicable law or agreed to in writing, software
  18352. * distributed under the License is distributed on an "AS IS" BASIS,
  18353. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18354. * See the License for the specific language governing permissions and
  18355. * limitations under the License.
  18356. */
  18357. /**
  18358. * @author Guenther Grau
  18359. */
  18360. /*final*/ class DetectionResultRowIndicatorColumn extends DetectionResultColumn {
  18361. constructor(boundingBox, isLeft) {
  18362. super(boundingBox);
  18363. this._isLeft = isLeft;
  18364. }
  18365. setRowNumbers() {
  18366. for (let codeword /*Codeword*/ of this.getCodewords()) {
  18367. if (codeword != null) {
  18368. codeword.setRowNumberAsRowIndicatorColumn();
  18369. }
  18370. }
  18371. }
  18372. // TODO implement properly
  18373. // TODO maybe we should add missing codewords to store the correct row number to make
  18374. // finding row numbers for other columns easier
  18375. // use row height count to make detection of invalid row numbers more reliable
  18376. adjustCompleteIndicatorColumnRowNumbers(barcodeMetadata) {
  18377. let codewords = this.getCodewords();
  18378. this.setRowNumbers();
  18379. this.removeIncorrectCodewords(codewords, barcodeMetadata);
  18380. let boundingBox = this.getBoundingBox();
  18381. let top = this._isLeft ? boundingBox.getTopLeft() : boundingBox.getTopRight();
  18382. let bottom = this._isLeft ? boundingBox.getBottomLeft() : boundingBox.getBottomRight();
  18383. let firstRow = this.imageRowToCodewordIndex(Math.trunc(top.getY()));
  18384. let lastRow = this.imageRowToCodewordIndex(Math.trunc(bottom.getY()));
  18385. // We need to be careful using the average row height. Barcode could be skewed so that we have smaller and
  18386. // taller rows
  18387. // float averageRowHeight = (lastRow - firstRow) / /*(float)*/ barcodeMetadata.getRowCount();
  18388. let barcodeRow = -1;
  18389. let maxRowHeight = 1;
  18390. let currentRowHeight = 0;
  18391. for (let codewordsRow /*int*/ = firstRow; codewordsRow < lastRow; codewordsRow++) {
  18392. if (codewords[codewordsRow] == null) {
  18393. continue;
  18394. }
  18395. let codeword = codewords[codewordsRow];
  18396. // float expectedRowNumber = (codewordsRow - firstRow) / averageRowHeight;
  18397. // if (Math.abs(codeword.getRowNumber() - expectedRowNumber) > 2) {
  18398. // SimpleLog.log(LEVEL.WARNING,
  18399. // "Removing codeword, rowNumberSkew too high, codeword[" + codewordsRow + "]: Expected Row: " +
  18400. // expectedRowNumber + ", RealRow: " + codeword.getRowNumber() + ", value: " + codeword.getValue());
  18401. // codewords[codewordsRow] = null;
  18402. // }
  18403. let rowDifference = codeword.getRowNumber() - barcodeRow;
  18404. // TODO improve handling with case where first row indicator doesn't start with 0
  18405. if (rowDifference === 0) {
  18406. currentRowHeight++;
  18407. }
  18408. else if (rowDifference === 1) {
  18409. maxRowHeight = Math.max(maxRowHeight, currentRowHeight);
  18410. currentRowHeight = 1;
  18411. barcodeRow = codeword.getRowNumber();
  18412. }
  18413. else if (rowDifference < 0 ||
  18414. codeword.getRowNumber() >= barcodeMetadata.getRowCount() ||
  18415. rowDifference > codewordsRow) {
  18416. codewords[codewordsRow] = null;
  18417. }
  18418. else {
  18419. let checkedRows;
  18420. if (maxRowHeight > 2) {
  18421. checkedRows = (maxRowHeight - 2) * rowDifference;
  18422. }
  18423. else {
  18424. checkedRows = rowDifference;
  18425. }
  18426. let closePreviousCodewordFound = checkedRows >= codewordsRow;
  18427. for (let i /*int*/ = 1; i <= checkedRows && !closePreviousCodewordFound; i++) {
  18428. // there must be (height * rowDifference) number of codewords missing. For now we assume height = 1.
  18429. // This should hopefully get rid of most problems already.
  18430. closePreviousCodewordFound = codewords[codewordsRow - i] != null;
  18431. }
  18432. if (closePreviousCodewordFound) {
  18433. codewords[codewordsRow] = null;
  18434. }
  18435. else {
  18436. barcodeRow = codeword.getRowNumber();
  18437. currentRowHeight = 1;
  18438. }
  18439. }
  18440. }
  18441. // return (int) (averageRowHeight + 0.5);
  18442. }
  18443. getRowHeights() {
  18444. let barcodeMetadata = this.getBarcodeMetadata();
  18445. if (barcodeMetadata == null) {
  18446. return null;
  18447. }
  18448. this.adjustIncompleteIndicatorColumnRowNumbers(barcodeMetadata);
  18449. let result = new Int32Array(barcodeMetadata.getRowCount());
  18450. for (let codeword /*Codeword*/ of this.getCodewords()) {
  18451. if (codeword != null) {
  18452. let rowNumber = codeword.getRowNumber();
  18453. if (rowNumber >= result.length) {
  18454. // We have more rows than the barcode metadata allows for, ignore them.
  18455. continue;
  18456. }
  18457. result[rowNumber]++;
  18458. } // else throw exception?
  18459. }
  18460. return result;
  18461. }
  18462. // TODO maybe we should add missing codewords to store the correct row number to make
  18463. // finding row numbers for other columns easier
  18464. // use row height count to make detection of invalid row numbers more reliable
  18465. adjustIncompleteIndicatorColumnRowNumbers(barcodeMetadata) {
  18466. let boundingBox = this.getBoundingBox();
  18467. let top = this._isLeft ? boundingBox.getTopLeft() : boundingBox.getTopRight();
  18468. let bottom = this._isLeft ? boundingBox.getBottomLeft() : boundingBox.getBottomRight();
  18469. let firstRow = this.imageRowToCodewordIndex(Math.trunc(top.getY()));
  18470. let lastRow = this.imageRowToCodewordIndex(Math.trunc(bottom.getY()));
  18471. // float averageRowHeight = (lastRow - firstRow) / /*(float)*/ barcodeMetadata.getRowCount();
  18472. let codewords = this.getCodewords();
  18473. let barcodeRow = -1;
  18474. for (let codewordsRow /*int*/ = firstRow; codewordsRow < lastRow; codewordsRow++) {
  18475. if (codewords[codewordsRow] == null) {
  18476. continue;
  18477. }
  18478. let codeword = codewords[codewordsRow];
  18479. codeword.setRowNumberAsRowIndicatorColumn();
  18480. let rowDifference = codeword.getRowNumber() - barcodeRow;
  18481. // TODO improve handling with case where first row indicator doesn't start with 0
  18482. if (rowDifference === 0) ;
  18483. else if (rowDifference === 1) {
  18484. barcodeRow = codeword.getRowNumber();
  18485. }
  18486. else if (codeword.getRowNumber() >= barcodeMetadata.getRowCount()) {
  18487. codewords[codewordsRow] = null;
  18488. }
  18489. else {
  18490. barcodeRow = codeword.getRowNumber();
  18491. }
  18492. }
  18493. // return (int) (averageRowHeight + 0.5);
  18494. }
  18495. getBarcodeMetadata() {
  18496. let codewords = this.getCodewords();
  18497. let barcodeColumnCount = new BarcodeValue();
  18498. let barcodeRowCountUpperPart = new BarcodeValue();
  18499. let barcodeRowCountLowerPart = new BarcodeValue();
  18500. let barcodeECLevel = new BarcodeValue();
  18501. for (let codeword /*Codeword*/ of codewords) {
  18502. if (codeword == null) {
  18503. continue;
  18504. }
  18505. codeword.setRowNumberAsRowIndicatorColumn();
  18506. let rowIndicatorValue = codeword.getValue() % 30;
  18507. let codewordRowNumber = codeword.getRowNumber();
  18508. if (!this._isLeft) {
  18509. codewordRowNumber += 2;
  18510. }
  18511. switch (codewordRowNumber % 3) {
  18512. case 0:
  18513. barcodeRowCountUpperPart.setValue(rowIndicatorValue * 3 + 1);
  18514. break;
  18515. case 1:
  18516. barcodeECLevel.setValue(rowIndicatorValue / 3);
  18517. barcodeRowCountLowerPart.setValue(rowIndicatorValue % 3);
  18518. break;
  18519. case 2:
  18520. barcodeColumnCount.setValue(rowIndicatorValue + 1);
  18521. break;
  18522. }
  18523. }
  18524. // Maybe we should check if we have ambiguous values?
  18525. if ((barcodeColumnCount.getValue().length === 0) ||
  18526. (barcodeRowCountUpperPart.getValue().length === 0) ||
  18527. (barcodeRowCountLowerPart.getValue().length === 0) ||
  18528. (barcodeECLevel.getValue().length === 0) ||
  18529. barcodeColumnCount.getValue()[0] < 1 ||
  18530. barcodeRowCountUpperPart.getValue()[0] + barcodeRowCountLowerPart.getValue()[0] < PDF417Common.MIN_ROWS_IN_BARCODE ||
  18531. barcodeRowCountUpperPart.getValue()[0] + barcodeRowCountLowerPart.getValue()[0] > PDF417Common.MAX_ROWS_IN_BARCODE) {
  18532. return null;
  18533. }
  18534. let barcodeMetadata = new BarcodeMetadata(barcodeColumnCount.getValue()[0], barcodeRowCountUpperPart.getValue()[0], barcodeRowCountLowerPart.getValue()[0], barcodeECLevel.getValue()[0]);
  18535. this.removeIncorrectCodewords(codewords, barcodeMetadata);
  18536. return barcodeMetadata;
  18537. }
  18538. removeIncorrectCodewords(codewords, barcodeMetadata) {
  18539. // Remove codewords which do not match the metadata
  18540. // TODO Maybe we should keep the incorrect codewords for the start and end positions?
  18541. for (let codewordRow /*int*/ = 0; codewordRow < codewords.length; codewordRow++) {
  18542. let codeword = codewords[codewordRow];
  18543. if (codewords[codewordRow] == null) {
  18544. continue;
  18545. }
  18546. let rowIndicatorValue = codeword.getValue() % 30;
  18547. let codewordRowNumber = codeword.getRowNumber();
  18548. if (codewordRowNumber > barcodeMetadata.getRowCount()) {
  18549. codewords[codewordRow] = null;
  18550. continue;
  18551. }
  18552. if (!this._isLeft) {
  18553. codewordRowNumber += 2;
  18554. }
  18555. switch (codewordRowNumber % 3) {
  18556. case 0:
  18557. if (rowIndicatorValue * 3 + 1 !== barcodeMetadata.getRowCountUpperPart()) {
  18558. codewords[codewordRow] = null;
  18559. }
  18560. break;
  18561. case 1:
  18562. if (Math.trunc(rowIndicatorValue / 3) !== barcodeMetadata.getErrorCorrectionLevel() ||
  18563. rowIndicatorValue % 3 !== barcodeMetadata.getRowCountLowerPart()) {
  18564. codewords[codewordRow] = null;
  18565. }
  18566. break;
  18567. case 2:
  18568. if (rowIndicatorValue + 1 !== barcodeMetadata.getColumnCount()) {
  18569. codewords[codewordRow] = null;
  18570. }
  18571. break;
  18572. }
  18573. }
  18574. }
  18575. isLeft() {
  18576. return this._isLeft;
  18577. }
  18578. // @Override
  18579. toString() {
  18580. return 'IsLeft: ' + this._isLeft + '\n' + super.toString();
  18581. }
  18582. }
  18583. /*
  18584. * Copyright 2013 ZXing authors
  18585. *
  18586. * Licensed under the Apache License, Version 2.0 (the "License");
  18587. * you may not use this file except in compliance with the License.
  18588. * You may obtain a copy of the License at
  18589. *
  18590. * http://www.apache.org/licenses/LICENSE-2.0
  18591. *
  18592. * Unless required by applicable law or agreed to in writing, software
  18593. * distributed under the License is distributed on an "AS IS" BASIS,
  18594. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18595. * See the License for the specific language governing permissions and
  18596. * limitations under the License.
  18597. */
  18598. /**
  18599. * @author Guenther Grau
  18600. */
  18601. /*final*/ class DetectionResult {
  18602. constructor(barcodeMetadata, boundingBox) {
  18603. /*final*/ this.ADJUST_ROW_NUMBER_SKIP = 2;
  18604. this.barcodeMetadata = barcodeMetadata;
  18605. this.barcodeColumnCount = barcodeMetadata.getColumnCount();
  18606. this.boundingBox = boundingBox;
  18607. // this.detectionResultColumns = new DetectionResultColumn[this.barcodeColumnCount + 2];
  18608. this.detectionResultColumns = new Array(this.barcodeColumnCount + 2);
  18609. }
  18610. getDetectionResultColumns() {
  18611. this.adjustIndicatorColumnRowNumbers(this.detectionResultColumns[0]);
  18612. this.adjustIndicatorColumnRowNumbers(this.detectionResultColumns[this.barcodeColumnCount + 1]);
  18613. let unadjustedCodewordCount = PDF417Common.MAX_CODEWORDS_IN_BARCODE;
  18614. let previousUnadjustedCount;
  18615. do {
  18616. previousUnadjustedCount = unadjustedCodewordCount;
  18617. unadjustedCodewordCount = this.adjustRowNumbersAndGetCount();
  18618. } while (unadjustedCodewordCount > 0 && unadjustedCodewordCount < previousUnadjustedCount);
  18619. return this.detectionResultColumns;
  18620. }
  18621. adjustIndicatorColumnRowNumbers(detectionResultColumn) {
  18622. if (detectionResultColumn != null) {
  18623. detectionResultColumn
  18624. .adjustCompleteIndicatorColumnRowNumbers(this.barcodeMetadata);
  18625. }
  18626. }
  18627. // TODO ensure that no detected codewords with unknown row number are left
  18628. // we should be able to estimate the row height and use it as a hint for the row number
  18629. // we should also fill the rows top to bottom and bottom to top
  18630. /**
  18631. * @return number of codewords which don't have a valid row number. Note that the count is not accurate as codewords
  18632. * will be counted several times. It just serves as an indicator to see when we can stop adjusting row numbers
  18633. */
  18634. adjustRowNumbersAndGetCount() {
  18635. let unadjustedCount = this.adjustRowNumbersByRow();
  18636. if (unadjustedCount === 0) {
  18637. return 0;
  18638. }
  18639. for (let barcodeColumn /*int*/ = 1; barcodeColumn < this.barcodeColumnCount + 1; barcodeColumn++) {
  18640. let codewords = this.detectionResultColumns[barcodeColumn].getCodewords();
  18641. for (let codewordsRow /*int*/ = 0; codewordsRow < codewords.length; codewordsRow++) {
  18642. if (codewords[codewordsRow] == null) {
  18643. continue;
  18644. }
  18645. if (!codewords[codewordsRow].hasValidRowNumber()) {
  18646. this.adjustRowNumbers(barcodeColumn, codewordsRow, codewords);
  18647. }
  18648. }
  18649. }
  18650. return unadjustedCount;
  18651. }
  18652. adjustRowNumbersByRow() {
  18653. this.adjustRowNumbersFromBothRI();
  18654. // TODO we should only do full row adjustments if row numbers of left and right row indicator column match.
  18655. // Maybe it's even better to calculated the height (rows: d) and divide it by the number of barcode
  18656. // rows. This, together with the LRI and RRI row numbers should allow us to get a good estimate where a row
  18657. // number starts and ends.
  18658. let unadjustedCount = this.adjustRowNumbersFromLRI();
  18659. return unadjustedCount + this.adjustRowNumbersFromRRI();
  18660. }
  18661. adjustRowNumbersFromBothRI() {
  18662. if (this.detectionResultColumns[0] == null || this.detectionResultColumns[this.barcodeColumnCount + 1] == null) {
  18663. return;
  18664. }
  18665. let LRIcodewords = this.detectionResultColumns[0].getCodewords();
  18666. let RRIcodewords = this.detectionResultColumns[this.barcodeColumnCount + 1].getCodewords();
  18667. for (let codewordsRow /*int*/ = 0; codewordsRow < LRIcodewords.length; codewordsRow++) {
  18668. if (LRIcodewords[codewordsRow] != null &&
  18669. RRIcodewords[codewordsRow] != null &&
  18670. LRIcodewords[codewordsRow].getRowNumber() === RRIcodewords[codewordsRow].getRowNumber()) {
  18671. for (let barcodeColumn /*int*/ = 1; barcodeColumn <= this.barcodeColumnCount; barcodeColumn++) {
  18672. let codeword = this.detectionResultColumns[barcodeColumn].getCodewords()[codewordsRow];
  18673. if (codeword == null) {
  18674. continue;
  18675. }
  18676. codeword.setRowNumber(LRIcodewords[codewordsRow].getRowNumber());
  18677. if (!codeword.hasValidRowNumber()) {
  18678. this.detectionResultColumns[barcodeColumn].getCodewords()[codewordsRow] = null;
  18679. }
  18680. }
  18681. }
  18682. }
  18683. }
  18684. adjustRowNumbersFromRRI() {
  18685. if (this.detectionResultColumns[this.barcodeColumnCount + 1] == null) {
  18686. return 0;
  18687. }
  18688. let unadjustedCount = 0;
  18689. let codewords = this.detectionResultColumns[this.barcodeColumnCount + 1].getCodewords();
  18690. for (let codewordsRow /*int*/ = 0; codewordsRow < codewords.length; codewordsRow++) {
  18691. if (codewords[codewordsRow] == null) {
  18692. continue;
  18693. }
  18694. let rowIndicatorRowNumber = codewords[codewordsRow].getRowNumber();
  18695. let invalidRowCounts = 0;
  18696. for (let barcodeColumn /*int*/ = this.barcodeColumnCount + 1; barcodeColumn > 0 && invalidRowCounts < this.ADJUST_ROW_NUMBER_SKIP; barcodeColumn--) {
  18697. let codeword = this.detectionResultColumns[barcodeColumn].getCodewords()[codewordsRow];
  18698. if (codeword != null) {
  18699. invalidRowCounts = DetectionResult.adjustRowNumberIfValid(rowIndicatorRowNumber, invalidRowCounts, codeword);
  18700. if (!codeword.hasValidRowNumber()) {
  18701. unadjustedCount++;
  18702. }
  18703. }
  18704. }
  18705. }
  18706. return unadjustedCount;
  18707. }
  18708. adjustRowNumbersFromLRI() {
  18709. if (this.detectionResultColumns[0] == null) {
  18710. return 0;
  18711. }
  18712. let unadjustedCount = 0;
  18713. let codewords = this.detectionResultColumns[0].getCodewords();
  18714. for (let codewordsRow /*int*/ = 0; codewordsRow < codewords.length; codewordsRow++) {
  18715. if (codewords[codewordsRow] == null) {
  18716. continue;
  18717. }
  18718. let rowIndicatorRowNumber = codewords[codewordsRow].getRowNumber();
  18719. let invalidRowCounts = 0;
  18720. for (let barcodeColumn /*int*/ = 1; barcodeColumn < this.barcodeColumnCount + 1 && invalidRowCounts < this.ADJUST_ROW_NUMBER_SKIP; barcodeColumn++) {
  18721. let codeword = this.detectionResultColumns[barcodeColumn].getCodewords()[codewordsRow];
  18722. if (codeword != null) {
  18723. invalidRowCounts = DetectionResult.adjustRowNumberIfValid(rowIndicatorRowNumber, invalidRowCounts, codeword);
  18724. if (!codeword.hasValidRowNumber()) {
  18725. unadjustedCount++;
  18726. }
  18727. }
  18728. }
  18729. }
  18730. return unadjustedCount;
  18731. }
  18732. static adjustRowNumberIfValid(rowIndicatorRowNumber, invalidRowCounts, codeword) {
  18733. if (codeword == null) {
  18734. return invalidRowCounts;
  18735. }
  18736. if (!codeword.hasValidRowNumber()) {
  18737. if (codeword.isValidRowNumber(rowIndicatorRowNumber)) {
  18738. codeword.setRowNumber(rowIndicatorRowNumber);
  18739. invalidRowCounts = 0;
  18740. }
  18741. else {
  18742. ++invalidRowCounts;
  18743. }
  18744. }
  18745. return invalidRowCounts;
  18746. }
  18747. adjustRowNumbers(barcodeColumn, codewordsRow, codewords) {
  18748. if (this.detectionResultColumns[barcodeColumn - 1] == null) {
  18749. return;
  18750. }
  18751. let codeword = codewords[codewordsRow];
  18752. let previousColumnCodewords = this.detectionResultColumns[barcodeColumn - 1].getCodewords();
  18753. let nextColumnCodewords = previousColumnCodewords;
  18754. if (this.detectionResultColumns[barcodeColumn + 1] != null) {
  18755. nextColumnCodewords = this.detectionResultColumns[barcodeColumn + 1].getCodewords();
  18756. }
  18757. // let otherCodewords: Codeword[] = new Codeword[14];
  18758. let otherCodewords = new Array(14);
  18759. otherCodewords[2] = previousColumnCodewords[codewordsRow];
  18760. otherCodewords[3] = nextColumnCodewords[codewordsRow];
  18761. if (codewordsRow > 0) {
  18762. otherCodewords[0] = codewords[codewordsRow - 1];
  18763. otherCodewords[4] = previousColumnCodewords[codewordsRow - 1];
  18764. otherCodewords[5] = nextColumnCodewords[codewordsRow - 1];
  18765. }
  18766. if (codewordsRow > 1) {
  18767. otherCodewords[8] = codewords[codewordsRow - 2];
  18768. otherCodewords[10] = previousColumnCodewords[codewordsRow - 2];
  18769. otherCodewords[11] = nextColumnCodewords[codewordsRow - 2];
  18770. }
  18771. if (codewordsRow < codewords.length - 1) {
  18772. otherCodewords[1] = codewords[codewordsRow + 1];
  18773. otherCodewords[6] = previousColumnCodewords[codewordsRow + 1];
  18774. otherCodewords[7] = nextColumnCodewords[codewordsRow + 1];
  18775. }
  18776. if (codewordsRow < codewords.length - 2) {
  18777. otherCodewords[9] = codewords[codewordsRow + 2];
  18778. otherCodewords[12] = previousColumnCodewords[codewordsRow + 2];
  18779. otherCodewords[13] = nextColumnCodewords[codewordsRow + 2];
  18780. }
  18781. for (let otherCodeword of otherCodewords) {
  18782. if (DetectionResult.adjustRowNumber(codeword, otherCodeword)) {
  18783. return;
  18784. }
  18785. }
  18786. }
  18787. /**
  18788. * @return true, if row number was adjusted, false otherwise
  18789. */
  18790. static adjustRowNumber(codeword, otherCodeword) {
  18791. if (otherCodeword == null) {
  18792. return false;
  18793. }
  18794. if (otherCodeword.hasValidRowNumber() && otherCodeword.getBucket() === codeword.getBucket()) {
  18795. codeword.setRowNumber(otherCodeword.getRowNumber());
  18796. return true;
  18797. }
  18798. return false;
  18799. }
  18800. getBarcodeColumnCount() {
  18801. return this.barcodeColumnCount;
  18802. }
  18803. getBarcodeRowCount() {
  18804. return this.barcodeMetadata.getRowCount();
  18805. }
  18806. getBarcodeECLevel() {
  18807. return this.barcodeMetadata.getErrorCorrectionLevel();
  18808. }
  18809. setBoundingBox(boundingBox) {
  18810. this.boundingBox = boundingBox;
  18811. }
  18812. getBoundingBox() {
  18813. return this.boundingBox;
  18814. }
  18815. setDetectionResultColumn(barcodeColumn, detectionResultColumn) {
  18816. this.detectionResultColumns[barcodeColumn] = detectionResultColumn;
  18817. }
  18818. getDetectionResultColumn(barcodeColumn) {
  18819. return this.detectionResultColumns[barcodeColumn];
  18820. }
  18821. // @Override
  18822. toString() {
  18823. let rowIndicatorColumn = this.detectionResultColumns[0];
  18824. if (rowIndicatorColumn == null) {
  18825. rowIndicatorColumn = this.detectionResultColumns[this.barcodeColumnCount + 1];
  18826. }
  18827. // try (
  18828. let formatter = new Formatter();
  18829. // ) {
  18830. for (let codewordsRow /*int*/ = 0; codewordsRow < rowIndicatorColumn.getCodewords().length; codewordsRow++) {
  18831. formatter.format('CW %3d:', codewordsRow);
  18832. for (let barcodeColumn /*int*/ = 0; barcodeColumn < this.barcodeColumnCount + 2; barcodeColumn++) {
  18833. if (this.detectionResultColumns[barcodeColumn] == null) {
  18834. formatter.format(' | ');
  18835. continue;
  18836. }
  18837. let codeword = this.detectionResultColumns[barcodeColumn].getCodewords()[codewordsRow];
  18838. if (codeword == null) {
  18839. formatter.format(' | ');
  18840. continue;
  18841. }
  18842. formatter.format(' %3d|%3d', codeword.getRowNumber(), codeword.getValue());
  18843. }
  18844. formatter.format('%n');
  18845. }
  18846. return formatter.toString();
  18847. // }
  18848. }
  18849. }
  18850. /*
  18851. * Copyright 2013 ZXing authors
  18852. *
  18853. * Licensed under the Apache License, Version 2.0 (the "License");
  18854. * you may not use this file except in compliance with the License.
  18855. * You may obtain a copy of the License at
  18856. *
  18857. * http://www.apache.org/licenses/LICENSE-2.0
  18858. *
  18859. * Unless required by applicable law or agreed to in writing, software
  18860. * distributed under the License is distributed on an "AS IS" BASIS,
  18861. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18862. * See the License for the specific language governing permissions and
  18863. * limitations under the License.
  18864. */
  18865. // package com.google.zxing.pdf417.decoder;
  18866. /**
  18867. * @author Guenther Grau
  18868. */
  18869. /*final*/ class Codeword {
  18870. constructor(startX, endX, bucket, value) {
  18871. this.rowNumber = Codeword.BARCODE_ROW_UNKNOWN;
  18872. this.startX = Math.trunc(startX);
  18873. this.endX = Math.trunc(endX);
  18874. this.bucket = Math.trunc(bucket);
  18875. this.value = Math.trunc(value);
  18876. }
  18877. hasValidRowNumber() {
  18878. return this.isValidRowNumber(this.rowNumber);
  18879. }
  18880. isValidRowNumber(rowNumber) {
  18881. return rowNumber !== Codeword.BARCODE_ROW_UNKNOWN && this.bucket === (rowNumber % 3) * 3;
  18882. }
  18883. setRowNumberAsRowIndicatorColumn() {
  18884. this.rowNumber = Math.trunc((Math.trunc(this.value / 30)) * 3 + Math.trunc(this.bucket / 3));
  18885. }
  18886. getWidth() {
  18887. return this.endX - this.startX;
  18888. }
  18889. getStartX() {
  18890. return this.startX;
  18891. }
  18892. getEndX() {
  18893. return this.endX;
  18894. }
  18895. getBucket() {
  18896. return this.bucket;
  18897. }
  18898. getValue() {
  18899. return this.value;
  18900. }
  18901. getRowNumber() {
  18902. return this.rowNumber;
  18903. }
  18904. setRowNumber(rowNumber) {
  18905. this.rowNumber = rowNumber;
  18906. }
  18907. // @Override
  18908. toString() {
  18909. return this.rowNumber + '|' + this.value;
  18910. }
  18911. }
  18912. Codeword.BARCODE_ROW_UNKNOWN = -1;
  18913. /*
  18914. * Copyright 2013 ZXing authors
  18915. *
  18916. * Licensed under the Apache License, Version 2.0 (the "License");
  18917. * you may not use this file except in compliance with the License.
  18918. * You may obtain a copy of the License at
  18919. *
  18920. * http://www.apache.org/licenses/LICENSE-2.0
  18921. *
  18922. * Unless required by applicable law or agreed to in writing, software
  18923. * distributed under the License is distributed on an "AS IS" BASIS,
  18924. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18925. * See the License for the specific language governing permissions and
  18926. * limitations under the License.
  18927. */
  18928. /**
  18929. * @author Guenther Grau
  18930. * @author creatale GmbH (christoph.schulz@creatale.de)
  18931. */
  18932. /*final*/ class PDF417CodewordDecoder {
  18933. /* @note
  18934. * this action have to be performed before first use of class
  18935. * - static constructor
  18936. * working with 32bit float (based from Java logic)
  18937. */
  18938. static initialize() {
  18939. // Pre-computes the symbol ratio table.
  18940. for ( /*int*/let i = 0; i < PDF417Common.SYMBOL_TABLE.length; i++) {
  18941. let currentSymbol = PDF417Common.SYMBOL_TABLE[i];
  18942. let currentBit = currentSymbol & 0x1;
  18943. for ( /*int*/let j = 0; j < PDF417Common.BARS_IN_MODULE; j++) {
  18944. let size = 0.0;
  18945. while ((currentSymbol & 0x1) === currentBit) {
  18946. size += 1.0;
  18947. currentSymbol >>= 1;
  18948. }
  18949. currentBit = currentSymbol & 0x1;
  18950. if (!PDF417CodewordDecoder.RATIOS_TABLE[i]) {
  18951. PDF417CodewordDecoder.RATIOS_TABLE[i] = new Array(PDF417Common.BARS_IN_MODULE);
  18952. }
  18953. PDF417CodewordDecoder.RATIOS_TABLE[i][PDF417Common.BARS_IN_MODULE - j - 1] = Math.fround(size / PDF417Common.MODULES_IN_CODEWORD);
  18954. }
  18955. }
  18956. this.bSymbolTableReady = true;
  18957. }
  18958. static getDecodedValue(moduleBitCount) {
  18959. let decodedValue = PDF417CodewordDecoder.getDecodedCodewordValue(PDF417CodewordDecoder.sampleBitCounts(moduleBitCount));
  18960. if (decodedValue !== -1) {
  18961. return decodedValue;
  18962. }
  18963. return PDF417CodewordDecoder.getClosestDecodedValue(moduleBitCount);
  18964. }
  18965. static sampleBitCounts(moduleBitCount) {
  18966. let bitCountSum = MathUtils.sum(moduleBitCount);
  18967. let result = new Int32Array(PDF417Common.BARS_IN_MODULE);
  18968. let bitCountIndex = 0;
  18969. let sumPreviousBits = 0;
  18970. for ( /*int*/let i = 0; i < PDF417Common.MODULES_IN_CODEWORD; i++) {
  18971. let sampleIndex = bitCountSum / (2 * PDF417Common.MODULES_IN_CODEWORD) +
  18972. (i * bitCountSum) / PDF417Common.MODULES_IN_CODEWORD;
  18973. if (sumPreviousBits + moduleBitCount[bitCountIndex] <= sampleIndex) {
  18974. sumPreviousBits += moduleBitCount[bitCountIndex];
  18975. bitCountIndex++;
  18976. }
  18977. result[bitCountIndex]++;
  18978. }
  18979. return result;
  18980. }
  18981. static getDecodedCodewordValue(moduleBitCount) {
  18982. let decodedValue = PDF417CodewordDecoder.getBitValue(moduleBitCount);
  18983. return PDF417Common.getCodeword(decodedValue) === -1 ? -1 : decodedValue;
  18984. }
  18985. static getBitValue(moduleBitCount) {
  18986. let result = /*long*/ 0;
  18987. for (let /*int*/ i = 0; i < moduleBitCount.length; i++) {
  18988. for ( /*int*/let bit = 0; bit < moduleBitCount[i]; bit++) {
  18989. result = (result << 1) | (i % 2 === 0 ? 1 : 0);
  18990. }
  18991. }
  18992. return Math.trunc(result);
  18993. }
  18994. // working with 32bit float (as in Java)
  18995. static getClosestDecodedValue(moduleBitCount) {
  18996. let bitCountSum = MathUtils.sum(moduleBitCount);
  18997. let bitCountRatios = new Array(PDF417Common.BARS_IN_MODULE);
  18998. if (bitCountSum > 1) {
  18999. for (let /*int*/ i = 0; i < bitCountRatios.length; i++) {
  19000. bitCountRatios[i] = Math.fround(moduleBitCount[i] / bitCountSum);
  19001. }
  19002. }
  19003. let bestMatchError = Float.MAX_VALUE;
  19004. let bestMatch = -1;
  19005. if (!this.bSymbolTableReady) {
  19006. PDF417CodewordDecoder.initialize();
  19007. }
  19008. for ( /*int*/let j = 0; j < PDF417CodewordDecoder.RATIOS_TABLE.length; j++) {
  19009. let error = 0.0;
  19010. let ratioTableRow = PDF417CodewordDecoder.RATIOS_TABLE[j];
  19011. for ( /*int*/let k = 0; k < PDF417Common.BARS_IN_MODULE; k++) {
  19012. let diff = Math.fround(ratioTableRow[k] - bitCountRatios[k]);
  19013. error += Math.fround(diff * diff);
  19014. if (error >= bestMatchError) {
  19015. break;
  19016. }
  19017. }
  19018. if (error < bestMatchError) {
  19019. bestMatchError = error;
  19020. bestMatch = PDF417Common.SYMBOL_TABLE[j];
  19021. }
  19022. }
  19023. return bestMatch;
  19024. }
  19025. }
  19026. // flag that the table is ready for use
  19027. PDF417CodewordDecoder.bSymbolTableReady = false;
  19028. PDF417CodewordDecoder.RATIOS_TABLE = new Array(PDF417Common.SYMBOL_TABLE.length).map(x => new Array(PDF417Common.BARS_IN_MODULE));
  19029. /*
  19030. * Copyright 2013 ZXing authors
  19031. *
  19032. * Licensed under the Apache License, Version 2.0 (the "License");
  19033. * you may not use this file except in compliance with the License.
  19034. * You may obtain a copy of the License at
  19035. *
  19036. * http://www.apache.org/licenses/LICENSE-2.0
  19037. *
  19038. * Unless required by applicable law or agreed to in writing, software
  19039. * distributed under the License is distributed on an "AS IS" BASIS,
  19040. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19041. * See the License for the specific language governing permissions and
  19042. * limitations under the License.
  19043. */
  19044. // package com.google.zxing.pdf417;
  19045. /**
  19046. * @author Guenther Grau
  19047. */
  19048. /*public final*/ class PDF417ResultMetadata {
  19049. constructor() {
  19050. this.segmentCount = -1;
  19051. this.fileSize = -1;
  19052. this.timestamp = -1;
  19053. this.checksum = -1;
  19054. }
  19055. /**
  19056. * The Segment ID represents the segment of the whole file distributed over different symbols.
  19057. *
  19058. * @return File segment index
  19059. */
  19060. getSegmentIndex() {
  19061. return this.segmentIndex;
  19062. }
  19063. setSegmentIndex(segmentIndex) {
  19064. this.segmentIndex = segmentIndex;
  19065. }
  19066. /**
  19067. * Is the same for each related PDF417 symbol
  19068. *
  19069. * @return File ID
  19070. */
  19071. getFileId() {
  19072. return this.fileId;
  19073. }
  19074. setFileId(fileId) {
  19075. this.fileId = fileId;
  19076. }
  19077. /**
  19078. * @return always null
  19079. * @deprecated use dedicated already parsed fields
  19080. */
  19081. // @Deprecated
  19082. getOptionalData() {
  19083. return this.optionalData;
  19084. }
  19085. /**
  19086. * @param optionalData old optional data format as int array
  19087. * @deprecated parse and use new fields
  19088. */
  19089. // @Deprecated
  19090. setOptionalData(optionalData) {
  19091. this.optionalData = optionalData;
  19092. }
  19093. /**
  19094. * @return true if it is the last segment
  19095. */
  19096. isLastSegment() {
  19097. return this.lastSegment;
  19098. }
  19099. setLastSegment(lastSegment) {
  19100. this.lastSegment = lastSegment;
  19101. }
  19102. /**
  19103. * @return count of segments, -1 if not set
  19104. */
  19105. getSegmentCount() {
  19106. return this.segmentCount;
  19107. }
  19108. setSegmentCount(segmentCount /*int*/) {
  19109. this.segmentCount = segmentCount;
  19110. }
  19111. getSender() {
  19112. return this.sender || null;
  19113. }
  19114. setSender(sender) {
  19115. this.sender = sender;
  19116. }
  19117. getAddressee() {
  19118. return this.addressee || null;
  19119. }
  19120. setAddressee(addressee) {
  19121. this.addressee = addressee;
  19122. }
  19123. /**
  19124. * Filename of the encoded file
  19125. *
  19126. * @return filename
  19127. */
  19128. getFileName() {
  19129. return this.fileName;
  19130. }
  19131. setFileName(fileName) {
  19132. this.fileName = fileName;
  19133. }
  19134. /**
  19135. * filesize in bytes of the encoded file
  19136. *
  19137. * @return filesize in bytes, -1 if not set
  19138. */
  19139. getFileSize() {
  19140. return this.fileSize;
  19141. }
  19142. setFileSize(fileSize /*long*/) {
  19143. this.fileSize = fileSize;
  19144. }
  19145. /**
  19146. * 16-bit CRC checksum using CCITT-16
  19147. *
  19148. * @return crc checksum, -1 if not set
  19149. */
  19150. getChecksum() {
  19151. return this.checksum;
  19152. }
  19153. setChecksum(checksum /*int*/) {
  19154. this.checksum = checksum;
  19155. }
  19156. /**
  19157. * unix epock timestamp, elapsed seconds since 1970-01-01
  19158. *
  19159. * @return elapsed seconds, -1 if not set
  19160. */
  19161. getTimestamp() {
  19162. return this.timestamp;
  19163. }
  19164. setTimestamp(timestamp /*long*/) {
  19165. this.timestamp = timestamp;
  19166. }
  19167. }
  19168. /**
  19169. * Ponyfill for Java's Long class.
  19170. */
  19171. class Long {
  19172. /**
  19173. * Parses a string to a number, since JS has no really Int64.
  19174. *
  19175. * @param num Numeric string.
  19176. * @param radix Destination radix.
  19177. */
  19178. static parseLong(num, radix = undefined) {
  19179. return parseInt(num, radix);
  19180. }
  19181. }
  19182. /**
  19183. * Custom Error class of type Exception.
  19184. */
  19185. class NullPointerException extends Exception {
  19186. }
  19187. NullPointerException.kind = 'NullPointerException';
  19188. /*
  19189. * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.
  19190. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  19191. *
  19192. * This code is free software; you can redistribute it and/or modify it
  19193. * under the terms of the GNU General Public License version 2 only, as
  19194. * published by the Free Software Foundation. Oracle designates this
  19195. * particular file as subject to the "Classpath" exception as provided
  19196. * by Oracle in the LICENSE file that accompanied this code.
  19197. *
  19198. * This code is distributed in the hope that it will be useful, but WITHOUT
  19199. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19200. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  19201. * version 2 for more details (a copy is included in the LICENSE file that
  19202. * accompanied this code).
  19203. *
  19204. * You should have received a copy of the GNU General Public License version
  19205. * 2 along with this work; if not, write to the Free Software Foundation,
  19206. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19207. *
  19208. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19209. * or visit www.oracle.com if you need additional information or have any
  19210. * questions.
  19211. */
  19212. // package java.io;
  19213. /**
  19214. * This abstract class is the superclass of all classes representing
  19215. * an output stream of bytes. An output stream accepts output bytes
  19216. * and sends them to some sink.
  19217. * <p>
  19218. * Applications that need to define a subclass of
  19219. * <code>OutputStream</code> must always provide at least a method
  19220. * that writes one byte of output.
  19221. *
  19222. * @author Arthur van Hoff
  19223. * @see java.io.BufferedOutputStream
  19224. * @see java.io.ByteArrayOutputStream
  19225. * @see java.io.DataOutputStream
  19226. * @see java.io.FilterOutputStream
  19227. * @see java.io.InputStream
  19228. * @see java.io.OutputStream#write(int)
  19229. * @since JDK1.0
  19230. */
  19231. /*public*/ class OutputStream /*implements Closeable, Flushable*/ {
  19232. /**
  19233. * Writes <code>b.length</code> bytes from the specified byte array
  19234. * to this output stream. The general contract for <code>write(b)</code>
  19235. * is that it should have exactly the same effect as the call
  19236. * <code>write(b, 0, b.length)</code>.
  19237. *
  19238. * @param b the data.
  19239. * @exception IOException if an I/O error occurs.
  19240. * @see java.io.OutputStream#write(byte[], int, int)
  19241. */
  19242. writeBytes(b) {
  19243. this.writeBytesOffset(b, 0, b.length);
  19244. }
  19245. /**
  19246. * Writes <code>len</code> bytes from the specified byte array
  19247. * starting at offset <code>off</code> to this output stream.
  19248. * The general contract for <code>write(b, off, len)</code> is that
  19249. * some of the bytes in the array <code>b</code> are written to the
  19250. * output stream in order; element <code>b[off]</code> is the first
  19251. * byte written and <code>b[off+len-1]</code> is the last byte written
  19252. * by this operation.
  19253. * <p>
  19254. * The <code>write</code> method of <code>OutputStream</code> calls
  19255. * the write method of one argument on each of the bytes to be
  19256. * written out. Subclasses are encouraged to override this method and
  19257. * provide a more efficient implementation.
  19258. * <p>
  19259. * If <code>b</code> is <code>null</code>, a
  19260. * <code>NullPointerException</code> is thrown.
  19261. * <p>
  19262. * If <code>off</code> is negative, or <code>len</code> is negative, or
  19263. * <code>off+len</code> is greater than the length of the array
  19264. * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
  19265. *
  19266. * @param b the data.
  19267. * @param off the start offset in the data.
  19268. * @param len the number of bytes to write.
  19269. * @exception IOException if an I/O error occurs. In particular,
  19270. * an <code>IOException</code> is thrown if the output
  19271. * stream is closed.
  19272. */
  19273. writeBytesOffset(b, off, len) {
  19274. if (b == null) {
  19275. throw new NullPointerException();
  19276. }
  19277. else if ((off < 0) || (off > b.length) || (len < 0) ||
  19278. ((off + len) > b.length) || ((off + len) < 0)) {
  19279. throw new IndexOutOfBoundsException();
  19280. }
  19281. else if (len === 0) {
  19282. return;
  19283. }
  19284. for (let i = 0; i < len; i++) {
  19285. this.write(b[off + i]);
  19286. }
  19287. }
  19288. /**
  19289. * Flushes this output stream and forces any buffered output bytes
  19290. * to be written out. The general contract of <code>flush</code> is
  19291. * that calling it is an indication that, if any bytes previously
  19292. * written have been buffered by the implementation of the output
  19293. * stream, such bytes should immediately be written to their
  19294. * intended destination.
  19295. * <p>
  19296. * If the intended destination of this stream is an abstraction provided by
  19297. * the underlying operating system, for example a file, then flushing the
  19298. * stream guarantees only that bytes previously written to the stream are
  19299. * passed to the operating system for writing; it does not guarantee that
  19300. * they are actually written to a physical device such as a disk drive.
  19301. * <p>
  19302. * The <code>flush</code> method of <code>OutputStream</code> does nothing.
  19303. *
  19304. * @exception IOException if an I/O error occurs.
  19305. */
  19306. flush() {
  19307. }
  19308. /**
  19309. * Closes this output stream and releases any system resources
  19310. * associated with this stream. The general contract of <code>close</code>
  19311. * is that it closes the output stream. A closed stream cannot perform
  19312. * output operations and cannot be reopened.
  19313. * <p>
  19314. * The <code>close</code> method of <code>OutputStream</code> does nothing.
  19315. *
  19316. * @exception IOException if an I/O error occurs.
  19317. */
  19318. close() {
  19319. }
  19320. }
  19321. /**
  19322. * Custom Error class of type Exception.
  19323. */
  19324. class OutOfMemoryError extends Exception {
  19325. }
  19326. /*
  19327. * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
  19328. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  19329. *
  19330. * This code is free software; you can redistribute it and/or modify it
  19331. * under the terms of the GNU General Public License version 2 only, as
  19332. * published by the Free Software Foundation. Oracle designates this
  19333. * particular file as subject to the "Classpath" exception as provided
  19334. * by Oracle in the LICENSE file that accompanied this code.
  19335. *
  19336. * This code is distributed in the hope that it will be useful, but WITHOUT
  19337. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19338. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  19339. * version 2 for more details (a copy is included in the LICENSE file that
  19340. * accompanied this code).
  19341. *
  19342. * You should have received a copy of the GNU General Public License version
  19343. * 2 along with this work; if not, write to the Free Software Foundation,
  19344. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19345. *
  19346. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19347. * or visit www.oracle.com if you need additional information or have any
  19348. * questions.
  19349. */
  19350. /**
  19351. * This class implements an output stream in which the data is
  19352. * written into a byte array. The buffer automatically grows as data
  19353. * is written to it.
  19354. * The data can be retrieved using <code>toByteArray()</code> and
  19355. * <code>toString()</code>.
  19356. * <p>
  19357. * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
  19358. * this class can be called after the stream has been closed without
  19359. * generating an <tt>IOException</tt>.
  19360. *
  19361. * @author Arthur van Hoff
  19362. * @since JDK1.0
  19363. */
  19364. /*public*/ class ByteArrayOutputStream extends OutputStream {
  19365. /**
  19366. * Creates a new byte array output stream. The buffer capacity is
  19367. * initially 32 bytes, though its size increases if necessary.
  19368. */
  19369. // public constructor() {
  19370. // this(32);
  19371. // }
  19372. /**
  19373. * Creates a new byte array output stream, with a buffer capacity of
  19374. * the specified size, in bytes.
  19375. *
  19376. * @param size the initial size.
  19377. * @exception IllegalArgumentException if size is negative.
  19378. */
  19379. constructor(size = 32) {
  19380. super();
  19381. /**
  19382. * The number of valid bytes in the buffer.
  19383. */
  19384. this.count = 0;
  19385. if (size < 0) {
  19386. throw new IllegalArgumentException('Negative initial size: '
  19387. + size);
  19388. }
  19389. this.buf = new Uint8Array(size);
  19390. }
  19391. /**
  19392. * Increases the capacity if necessary to ensure that it can hold
  19393. * at least the number of elements specified by the minimum
  19394. * capacity argument.
  19395. *
  19396. * @param minCapacity the desired minimum capacity
  19397. * @throws OutOfMemoryError if {@code minCapacity < 0}. This is
  19398. * interpreted as a request for the unsatisfiably large capacity
  19399. * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
  19400. */
  19401. ensureCapacity(minCapacity) {
  19402. // overflow-conscious code
  19403. if (minCapacity - this.buf.length > 0)
  19404. this.grow(minCapacity);
  19405. }
  19406. /**
  19407. * Increases the capacity to ensure that it can hold at least the
  19408. * number of elements specified by the minimum capacity argument.
  19409. *
  19410. * @param minCapacity the desired minimum capacity
  19411. */
  19412. grow(minCapacity) {
  19413. // overflow-conscious code
  19414. let oldCapacity = this.buf.length;
  19415. let newCapacity = oldCapacity << 1;
  19416. if (newCapacity - minCapacity < 0)
  19417. newCapacity = minCapacity;
  19418. if (newCapacity < 0) {
  19419. if (minCapacity < 0) // overflow
  19420. throw new OutOfMemoryError();
  19421. newCapacity = Integer.MAX_VALUE;
  19422. }
  19423. this.buf = Arrays.copyOfUint8Array(this.buf, newCapacity);
  19424. }
  19425. /**
  19426. * Writes the specified byte to this byte array output stream.
  19427. *
  19428. * @param b the byte to be written.
  19429. */
  19430. write(b) {
  19431. this.ensureCapacity(this.count + 1);
  19432. this.buf[this.count] = /*(byte)*/ b;
  19433. this.count += 1;
  19434. }
  19435. /**
  19436. * Writes <code>len</code> bytes from the specified byte array
  19437. * starting at offset <code>off</code> to this byte array output stream.
  19438. *
  19439. * @param b the data.
  19440. * @param off the start offset in the data.
  19441. * @param len the number of bytes to write.
  19442. */
  19443. writeBytesOffset(b, off, len) {
  19444. if ((off < 0) || (off > b.length) || (len < 0) ||
  19445. ((off + len) - b.length > 0)) {
  19446. throw new IndexOutOfBoundsException();
  19447. }
  19448. this.ensureCapacity(this.count + len);
  19449. System.arraycopy(b, off, this.buf, this.count, len);
  19450. this.count += len;
  19451. }
  19452. /**
  19453. * Writes the complete contents of this byte array output stream to
  19454. * the specified output stream argument, as if by calling the output
  19455. * stream's write method using <code>out.write(buf, 0, count)</code>.
  19456. *
  19457. * @param out the output stream to which to write the data.
  19458. * @exception IOException if an I/O error occurs.
  19459. */
  19460. writeTo(out) {
  19461. out.writeBytesOffset(this.buf, 0, this.count);
  19462. }
  19463. /**
  19464. * Resets the <code>count</code> field of this byte array output
  19465. * stream to zero, so that all currently accumulated output in the
  19466. * output stream is discarded. The output stream can be used again,
  19467. * reusing the already allocated buffer space.
  19468. *
  19469. * @see java.io.ByteArrayInputStream#count
  19470. */
  19471. reset() {
  19472. this.count = 0;
  19473. }
  19474. /**
  19475. * Creates a newly allocated byte array. Its size is the current
  19476. * size of this output stream and the valid contents of the buffer
  19477. * have been copied into it.
  19478. *
  19479. * @return the current contents of this output stream, as a byte array.
  19480. * @see java.io.ByteArrayOutputStream#size()
  19481. */
  19482. toByteArray() {
  19483. return Arrays.copyOfUint8Array(this.buf, this.count);
  19484. }
  19485. /**
  19486. * Returns the current size of the buffer.
  19487. *
  19488. * @return the value of the <code>count</code> field, which is the number
  19489. * of valid bytes in this output stream.
  19490. * @see java.io.ByteArrayOutputStream#count
  19491. */
  19492. size() {
  19493. return this.count;
  19494. }
  19495. toString(param) {
  19496. if (!param) {
  19497. return this.toString_void();
  19498. }
  19499. if (typeof param === 'string') {
  19500. return this.toString_string(param);
  19501. }
  19502. return this.toString_number(param);
  19503. }
  19504. /**
  19505. * Converts the buffer's contents into a string decoding bytes using the
  19506. * platform's default character set. The length of the new <tt>String</tt>
  19507. * is a function of the character set, and hence may not be equal to the
  19508. * size of the buffer.
  19509. *
  19510. * <p> This method always replaces malformed-input and unmappable-character
  19511. * sequences with the default replacement string for the platform's
  19512. * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
  19513. * class should be used when more control over the decoding process is
  19514. * required.
  19515. *
  19516. * @return String decoded from the buffer's contents.
  19517. * @since JDK1.1
  19518. */
  19519. toString_void() {
  19520. return new String(this.buf /*, 0, this.count*/).toString();
  19521. }
  19522. /**
  19523. * Converts the buffer's contents into a string by decoding the bytes using
  19524. * the specified {@link java.nio.charset.Charset charsetName}. The length of
  19525. * the new <tt>String</tt> is a function of the charset, and hence may not be
  19526. * equal to the length of the byte array.
  19527. *
  19528. * <p> This method always replaces malformed-input and unmappable-character
  19529. * sequences with this charset's default replacement string. The {@link
  19530. * java.nio.charset.CharsetDecoder} class should be used when more control
  19531. * over the decoding process is required.
  19532. *
  19533. * @param charsetName the name of a supported
  19534. * {@linkplain java.nio.charset.Charset </code>charset<code>}
  19535. * @return String decoded from the buffer's contents.
  19536. * @exception UnsupportedEncodingException
  19537. * If the named charset is not supported
  19538. * @since JDK1.1
  19539. */
  19540. toString_string(charsetName) {
  19541. return new String(this.buf /*, 0, this.count, charsetName*/).toString();
  19542. }
  19543. /**
  19544. * Creates a newly allocated string. Its size is the current size of
  19545. * the output stream and the valid contents of the buffer have been
  19546. * copied into it. Each character <i>c</i> in the resulting string is
  19547. * constructed from the corresponding element <i>b</i> in the byte
  19548. * array such that:
  19549. * <blockquote><pre>
  19550. * c == (char)(((hibyte &amp; 0xff) &lt;&lt; 8) | (b &amp; 0xff))
  19551. * </pre></blockquote>
  19552. *
  19553. * @deprecated This method does not properly convert bytes into characters.
  19554. * As of JDK&nbsp;1.1, the preferred way to do this is via the
  19555. * <code>toString(String enc)</code> method, which takes an encoding-name
  19556. * argument, or the <code>toString()</code> method, which uses the
  19557. * platform's default character encoding.
  19558. *
  19559. * @param hibyte the high byte of each resulting Unicode character.
  19560. * @return the current contents of the output stream, as a string.
  19561. * @see java.io.ByteArrayOutputStream#size()
  19562. * @see java.io.ByteArrayOutputStream#toString(String)
  19563. * @see java.io.ByteArrayOutputStream#toString()
  19564. */
  19565. // @Deprecated
  19566. toString_number(hibyte) {
  19567. return new String(this.buf /*, hibyte, 0, this.count*/).toString();
  19568. }
  19569. /**
  19570. * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
  19571. * this class can be called after the stream has been closed without
  19572. * generating an <tt>IOException</tt>.
  19573. * <p>
  19574. *
  19575. * @throws IOException
  19576. */
  19577. close() {
  19578. }
  19579. }
  19580. /*
  19581. * Copyright 2009 ZXing authors
  19582. *
  19583. * Licensed under the Apache License, Version 2.0 (the "License");
  19584. * you may not use this file except in compliance with the License.
  19585. * You may obtain a copy of the License at
  19586. *
  19587. * http://www.apache.org/licenses/LICENSE-2.0
  19588. *
  19589. * Unless required by applicable law or agreed to in writing, software
  19590. * distributed under the License is distributed on an "AS IS" BASIS,
  19591. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19592. * See the License for the specific language governing permissions and
  19593. * limitations under the License.
  19594. */
  19595. /*private*/ var Mode$1;
  19596. (function (Mode) {
  19597. Mode[Mode["ALPHA"] = 0] = "ALPHA";
  19598. Mode[Mode["LOWER"] = 1] = "LOWER";
  19599. Mode[Mode["MIXED"] = 2] = "MIXED";
  19600. Mode[Mode["PUNCT"] = 3] = "PUNCT";
  19601. Mode[Mode["ALPHA_SHIFT"] = 4] = "ALPHA_SHIFT";
  19602. Mode[Mode["PUNCT_SHIFT"] = 5] = "PUNCT_SHIFT";
  19603. })(Mode$1 || (Mode$1 = {}));
  19604. /**
  19605. * Indirectly access the global BigInt constructor, it
  19606. * allows browsers that doesn't support BigInt to run
  19607. * the library without breaking due to "undefined BigInt"
  19608. * errors.
  19609. */
  19610. function getBigIntConstructor() {
  19611. if (typeof window !== 'undefined') {
  19612. return window['BigInt'] || null;
  19613. }
  19614. if (typeof global !== 'undefined') {
  19615. return global['BigInt'] || null;
  19616. }
  19617. if (typeof self !== 'undefined') {
  19618. return self['BigInt'] || null;
  19619. }
  19620. throw new Error('Can\'t search globals for BigInt!');
  19621. }
  19622. /**
  19623. * Used to store the BigInt constructor.
  19624. */
  19625. let BigInteger;
  19626. /**
  19627. * This function creates a bigint value. It allows browsers
  19628. * that doesn't support BigInt to run the rest of the library
  19629. * by not directly accessing the BigInt constructor.
  19630. */
  19631. function createBigInt(num) {
  19632. if (typeof BigInteger === 'undefined') {
  19633. BigInteger = getBigIntConstructor();
  19634. }
  19635. if (BigInteger === null) {
  19636. throw new Error('BigInt is not supported!');
  19637. }
  19638. return BigInteger(num);
  19639. }
  19640. function getEXP900() {
  19641. // in Java - array with length = 16
  19642. let EXP900 = [];
  19643. EXP900[0] = createBigInt(1);
  19644. let nineHundred = createBigInt(900);
  19645. EXP900[1] = nineHundred;
  19646. // in Java - array with length = 16
  19647. for (let i /*int*/ = 2; i < 16; i++) {
  19648. EXP900[i] = EXP900[i - 1] * nineHundred;
  19649. }
  19650. return EXP900;
  19651. }
  19652. /**
  19653. * <p>This class contains the methods for decoding the PDF417 codewords.</p>
  19654. *
  19655. * @author SITA Lab (kevin.osullivan@sita.aero)
  19656. * @author Guenther Grau
  19657. */
  19658. /*final*/ class DecodedBitStreamParser {
  19659. // private DecodedBitStreamParser() {
  19660. // }
  19661. /**
  19662. *
  19663. * @param codewords
  19664. * @param ecLevel
  19665. *
  19666. * @throws FormatException
  19667. */
  19668. static decode(codewords, ecLevel) {
  19669. // pass encoding to result (will be used for decode symbols in byte mode)
  19670. let result = new StringBuilder('');
  19671. // let encoding: Charset = StandardCharsets.ISO_8859_1;
  19672. let encoding = CharacterSetECI.ISO8859_1;
  19673. /**
  19674. * @note the next command is specific from this TypeScript library
  19675. * because TS can't properly cast some values to char and
  19676. * convert it to string later correctly due to encoding
  19677. * differences from Java version. As reported here:
  19678. * https://github.com/zxing-js/library/pull/264/files#r382831593
  19679. */
  19680. result.enableDecoding(encoding);
  19681. // Get compaction mode
  19682. let codeIndex = 1;
  19683. let code = codewords[codeIndex++];
  19684. let resultMetadata = new PDF417ResultMetadata();
  19685. while (codeIndex < codewords[0]) {
  19686. switch (code) {
  19687. case DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH:
  19688. codeIndex = DecodedBitStreamParser.textCompaction(codewords, codeIndex, result);
  19689. break;
  19690. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH:
  19691. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH_6:
  19692. codeIndex = DecodedBitStreamParser.byteCompaction(code, codewords, encoding, codeIndex, result);
  19693. break;
  19694. case DecodedBitStreamParser.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
  19695. result.append(/*(char)*/ codewords[codeIndex++]);
  19696. break;
  19697. case DecodedBitStreamParser.NUMERIC_COMPACTION_MODE_LATCH:
  19698. codeIndex = DecodedBitStreamParser.numericCompaction(codewords, codeIndex, result);
  19699. break;
  19700. case DecodedBitStreamParser.ECI_CHARSET:
  19701. CharacterSetECI.getCharacterSetECIByValue(codewords[codeIndex++]);
  19702. // encoding = Charset.forName(charsetECI.getName());
  19703. break;
  19704. case DecodedBitStreamParser.ECI_GENERAL_PURPOSE:
  19705. // Can't do anything with generic ECI; skip its 2 characters
  19706. codeIndex += 2;
  19707. break;
  19708. case DecodedBitStreamParser.ECI_USER_DEFINED:
  19709. // Can't do anything with user ECI; skip its 1 character
  19710. codeIndex++;
  19711. break;
  19712. case DecodedBitStreamParser.BEGIN_MACRO_PDF417_CONTROL_BLOCK:
  19713. codeIndex = DecodedBitStreamParser.decodeMacroBlock(codewords, codeIndex, resultMetadata);
  19714. break;
  19715. case DecodedBitStreamParser.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
  19716. case DecodedBitStreamParser.MACRO_PDF417_TERMINATOR:
  19717. // Should not see these outside a macro block
  19718. throw new FormatException();
  19719. default:
  19720. // Default to text compaction. During testing numerous barcodes
  19721. // appeared to be missing the starting mode. In these cases defaulting
  19722. // to text compaction seems to work.
  19723. codeIndex--;
  19724. codeIndex = DecodedBitStreamParser.textCompaction(codewords, codeIndex, result);
  19725. break;
  19726. }
  19727. if (codeIndex < codewords.length) {
  19728. code = codewords[codeIndex++];
  19729. }
  19730. else {
  19731. throw FormatException.getFormatInstance();
  19732. }
  19733. }
  19734. if (result.length() === 0) {
  19735. throw FormatException.getFormatInstance();
  19736. }
  19737. let decoderResult = new DecoderResult(null, result.toString(), null, ecLevel);
  19738. decoderResult.setOther(resultMetadata);
  19739. return decoderResult;
  19740. }
  19741. /**
  19742. *
  19743. * @param int
  19744. * @param param1
  19745. * @param codewords
  19746. * @param int
  19747. * @param codeIndex
  19748. * @param PDF417ResultMetadata
  19749. * @param resultMetadata
  19750. *
  19751. * @throws FormatException
  19752. */
  19753. // @SuppressWarnings("deprecation")
  19754. static decodeMacroBlock(codewords, codeIndex, resultMetadata) {
  19755. if (codeIndex + DecodedBitStreamParser.NUMBER_OF_SEQUENCE_CODEWORDS > codewords[0]) {
  19756. // we must have at least two bytes left for the segment index
  19757. throw FormatException.getFormatInstance();
  19758. }
  19759. let segmentIndexArray = new Int32Array(DecodedBitStreamParser.NUMBER_OF_SEQUENCE_CODEWORDS);
  19760. for (let i /*int*/ = 0; i < DecodedBitStreamParser.NUMBER_OF_SEQUENCE_CODEWORDS; i++, codeIndex++) {
  19761. segmentIndexArray[i] = codewords[codeIndex];
  19762. }
  19763. resultMetadata.setSegmentIndex(Integer.parseInt(DecodedBitStreamParser.decodeBase900toBase10(segmentIndexArray, DecodedBitStreamParser.NUMBER_OF_SEQUENCE_CODEWORDS)));
  19764. let fileId = new StringBuilder();
  19765. codeIndex = DecodedBitStreamParser.textCompaction(codewords, codeIndex, fileId);
  19766. resultMetadata.setFileId(fileId.toString());
  19767. let optionalFieldsStart = -1;
  19768. if (codewords[codeIndex] === DecodedBitStreamParser.BEGIN_MACRO_PDF417_OPTIONAL_FIELD) {
  19769. optionalFieldsStart = codeIndex + 1;
  19770. }
  19771. while (codeIndex < codewords[0]) {
  19772. switch (codewords[codeIndex]) {
  19773. case DecodedBitStreamParser.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
  19774. codeIndex++;
  19775. switch (codewords[codeIndex]) {
  19776. case DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_FILE_NAME:
  19777. let fileName = new StringBuilder();
  19778. codeIndex = DecodedBitStreamParser.textCompaction(codewords, codeIndex + 1, fileName);
  19779. resultMetadata.setFileName(fileName.toString());
  19780. break;
  19781. case DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_SENDER:
  19782. let sender = new StringBuilder();
  19783. codeIndex = DecodedBitStreamParser.textCompaction(codewords, codeIndex + 1, sender);
  19784. resultMetadata.setSender(sender.toString());
  19785. break;
  19786. case DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_ADDRESSEE:
  19787. let addressee = new StringBuilder();
  19788. codeIndex = DecodedBitStreamParser.textCompaction(codewords, codeIndex + 1, addressee);
  19789. resultMetadata.setAddressee(addressee.toString());
  19790. break;
  19791. case DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_SEGMENT_COUNT:
  19792. let segmentCount = new StringBuilder();
  19793. codeIndex = DecodedBitStreamParser.numericCompaction(codewords, codeIndex + 1, segmentCount);
  19794. resultMetadata.setSegmentCount(Integer.parseInt(segmentCount.toString()));
  19795. break;
  19796. case DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_TIME_STAMP:
  19797. let timestamp = new StringBuilder();
  19798. codeIndex = DecodedBitStreamParser.numericCompaction(codewords, codeIndex + 1, timestamp);
  19799. resultMetadata.setTimestamp(Long.parseLong(timestamp.toString()));
  19800. break;
  19801. case DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_CHECKSUM:
  19802. let checksum = new StringBuilder();
  19803. codeIndex = DecodedBitStreamParser.numericCompaction(codewords, codeIndex + 1, checksum);
  19804. resultMetadata.setChecksum(Integer.parseInt(checksum.toString()));
  19805. break;
  19806. case DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_FILE_SIZE:
  19807. let fileSize = new StringBuilder();
  19808. codeIndex = DecodedBitStreamParser.numericCompaction(codewords, codeIndex + 1, fileSize);
  19809. resultMetadata.setFileSize(Long.parseLong(fileSize.toString()));
  19810. break;
  19811. default:
  19812. throw FormatException.getFormatInstance();
  19813. }
  19814. break;
  19815. case DecodedBitStreamParser.MACRO_PDF417_TERMINATOR:
  19816. codeIndex++;
  19817. resultMetadata.setLastSegment(true);
  19818. break;
  19819. default:
  19820. throw FormatException.getFormatInstance();
  19821. }
  19822. }
  19823. // copy optional fields to additional options
  19824. if (optionalFieldsStart !== -1) {
  19825. let optionalFieldsLength = codeIndex - optionalFieldsStart;
  19826. if (resultMetadata.isLastSegment()) {
  19827. // do not include terminator
  19828. optionalFieldsLength--;
  19829. }
  19830. resultMetadata.setOptionalData(Arrays.copyOfRange(codewords, optionalFieldsStart, optionalFieldsStart + optionalFieldsLength));
  19831. }
  19832. return codeIndex;
  19833. }
  19834. /**
  19835. * Text Compaction mode (see 5.4.1.5) permits all printable ASCII characters to be
  19836. * encoded, i.e. values 32 - 126 inclusive in accordance with ISO/IEC 646 (IRV), as
  19837. * well as selected control characters.
  19838. *
  19839. * @param codewords The array of codewords (data + error)
  19840. * @param codeIndex The current index into the codeword array.
  19841. * @param result The decoded data is appended to the result.
  19842. * @return The next index into the codeword array.
  19843. */
  19844. static textCompaction(codewords, codeIndex, result) {
  19845. // 2 character per codeword
  19846. let textCompactionData = new Int32Array((codewords[0] - codeIndex) * 2);
  19847. // Used to hold the byte compaction value if there is a mode shift
  19848. let byteCompactionData = new Int32Array((codewords[0] - codeIndex) * 2);
  19849. let index = 0;
  19850. let end = false;
  19851. while ((codeIndex < codewords[0]) && !end) {
  19852. let code = codewords[codeIndex++];
  19853. if (code < DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH) {
  19854. textCompactionData[index] = code / 30;
  19855. textCompactionData[index + 1] = code % 30;
  19856. index += 2;
  19857. }
  19858. else {
  19859. switch (code) {
  19860. case DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH:
  19861. // reinitialize text compaction mode to alpha sub mode
  19862. textCompactionData[index++] = DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH;
  19863. break;
  19864. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH:
  19865. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH_6:
  19866. case DecodedBitStreamParser.NUMERIC_COMPACTION_MODE_LATCH:
  19867. case DecodedBitStreamParser.BEGIN_MACRO_PDF417_CONTROL_BLOCK:
  19868. case DecodedBitStreamParser.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
  19869. case DecodedBitStreamParser.MACRO_PDF417_TERMINATOR:
  19870. codeIndex--;
  19871. end = true;
  19872. break;
  19873. case DecodedBitStreamParser.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
  19874. // The Mode Shift codeword 913 shall cause a temporary
  19875. // switch from Text Compaction mode to Byte Compaction mode.
  19876. // This switch shall be in effect for only the next codeword,
  19877. // after which the mode shall revert to the prevailing sub-mode
  19878. // of the Text Compaction mode. Codeword 913 is only available
  19879. // in Text Compaction mode; its use is described in 5.4.2.4.
  19880. textCompactionData[index] = DecodedBitStreamParser.MODE_SHIFT_TO_BYTE_COMPACTION_MODE;
  19881. code = codewords[codeIndex++];
  19882. byteCompactionData[index] = code;
  19883. index++;
  19884. break;
  19885. }
  19886. }
  19887. }
  19888. DecodedBitStreamParser.decodeTextCompaction(textCompactionData, byteCompactionData, index, result);
  19889. return codeIndex;
  19890. }
  19891. /**
  19892. * The Text Compaction mode includes all the printable ASCII characters
  19893. * (i.e. values from 32 to 126) and three ASCII control characters: HT or tab
  19894. * (9: e), LF or line feed (10: e), and CR or carriage
  19895. * return (13: e). The Text Compaction mode also includes various latch
  19896. * and shift characters which are used exclusively within the mode. The Text
  19897. * Compaction mode encodes up to 2 characters per codeword. The compaction rules
  19898. * for converting data into PDF417 codewords are defined in 5.4.2.2. The sub-mode
  19899. * switches are defined in 5.4.2.3.
  19900. *
  19901. * @param textCompactionData The text compaction data.
  19902. * @param byteCompactionData The byte compaction data if there
  19903. * was a mode shift.
  19904. * @param length The size of the text compaction and byte compaction data.
  19905. * @param result The decoded data is appended to the result.
  19906. */
  19907. static decodeTextCompaction(textCompactionData, byteCompactionData, length, result) {
  19908. // Beginning from an initial state of the Alpha sub-mode
  19909. // The default compaction mode for PDF417 in effect at the start of each symbol shall always be Text
  19910. // Compaction mode Alpha sub-mode (alphabetic: uppercase). A latch codeword from another mode to the Text
  19911. // Compaction mode shall always switch to the Text Compaction Alpha sub-mode.
  19912. let subMode = Mode$1.ALPHA;
  19913. let priorToShiftMode = Mode$1.ALPHA;
  19914. let i = 0;
  19915. while (i < length) {
  19916. let subModeCh = textCompactionData[i];
  19917. let ch = /*char*/ '';
  19918. switch (subMode) {
  19919. case Mode$1.ALPHA:
  19920. // Alpha (alphabetic: uppercase)
  19921. if (subModeCh < 26) {
  19922. // Upper case Alpha Character
  19923. // Note: 65 = 'A' ASCII -> there is byte code of symbol
  19924. ch = /*(char)('A' + subModeCh) */ String.fromCharCode(65 + subModeCh);
  19925. }
  19926. else {
  19927. switch (subModeCh) {
  19928. case 26:
  19929. ch = ' ';
  19930. break;
  19931. case DecodedBitStreamParser.LL:
  19932. subMode = Mode$1.LOWER;
  19933. break;
  19934. case DecodedBitStreamParser.ML:
  19935. subMode = Mode$1.MIXED;
  19936. break;
  19937. case DecodedBitStreamParser.PS:
  19938. // Shift to punctuation
  19939. priorToShiftMode = subMode;
  19940. subMode = Mode$1.PUNCT_SHIFT;
  19941. break;
  19942. case DecodedBitStreamParser.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
  19943. result.append(/*(char)*/ byteCompactionData[i]);
  19944. break;
  19945. case DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH:
  19946. subMode = Mode$1.ALPHA;
  19947. break;
  19948. }
  19949. }
  19950. break;
  19951. case Mode$1.LOWER:
  19952. // Lower (alphabetic: lowercase)
  19953. if (subModeCh < 26) {
  19954. ch = /*(char)('a' + subModeCh)*/ String.fromCharCode(97 + subModeCh);
  19955. }
  19956. else {
  19957. switch (subModeCh) {
  19958. case 26:
  19959. ch = ' ';
  19960. break;
  19961. case DecodedBitStreamParser.AS:
  19962. // Shift to alpha
  19963. priorToShiftMode = subMode;
  19964. subMode = Mode$1.ALPHA_SHIFT;
  19965. break;
  19966. case DecodedBitStreamParser.ML:
  19967. subMode = Mode$1.MIXED;
  19968. break;
  19969. case DecodedBitStreamParser.PS:
  19970. // Shift to punctuation
  19971. priorToShiftMode = subMode;
  19972. subMode = Mode$1.PUNCT_SHIFT;
  19973. break;
  19974. case DecodedBitStreamParser.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
  19975. // TODO Does this need to use the current character encoding? See other occurrences below
  19976. result.append(/*(char)*/ byteCompactionData[i]);
  19977. break;
  19978. case DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH:
  19979. subMode = Mode$1.ALPHA;
  19980. break;
  19981. }
  19982. }
  19983. break;
  19984. case Mode$1.MIXED:
  19985. // Mixed (punctuation: e)
  19986. if (subModeCh < DecodedBitStreamParser.PL) {
  19987. ch = DecodedBitStreamParser.MIXED_CHARS[subModeCh];
  19988. }
  19989. else {
  19990. switch (subModeCh) {
  19991. case DecodedBitStreamParser.PL:
  19992. subMode = Mode$1.PUNCT;
  19993. break;
  19994. case 26:
  19995. ch = ' ';
  19996. break;
  19997. case DecodedBitStreamParser.LL:
  19998. subMode = Mode$1.LOWER;
  19999. break;
  20000. case DecodedBitStreamParser.AL:
  20001. subMode = Mode$1.ALPHA;
  20002. break;
  20003. case DecodedBitStreamParser.PS:
  20004. // Shift to punctuation
  20005. priorToShiftMode = subMode;
  20006. subMode = Mode$1.PUNCT_SHIFT;
  20007. break;
  20008. case DecodedBitStreamParser.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
  20009. result.append(/*(char)*/ byteCompactionData[i]);
  20010. break;
  20011. case DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH:
  20012. subMode = Mode$1.ALPHA;
  20013. break;
  20014. }
  20015. }
  20016. break;
  20017. case Mode$1.PUNCT:
  20018. // Punctuation
  20019. if (subModeCh < DecodedBitStreamParser.PAL) {
  20020. ch = DecodedBitStreamParser.PUNCT_CHARS[subModeCh];
  20021. }
  20022. else {
  20023. switch (subModeCh) {
  20024. case DecodedBitStreamParser.PAL:
  20025. subMode = Mode$1.ALPHA;
  20026. break;
  20027. case DecodedBitStreamParser.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
  20028. result.append(/*(char)*/ byteCompactionData[i]);
  20029. break;
  20030. case DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH:
  20031. subMode = Mode$1.ALPHA;
  20032. break;
  20033. }
  20034. }
  20035. break;
  20036. case Mode$1.ALPHA_SHIFT:
  20037. // Restore sub-mode
  20038. subMode = priorToShiftMode;
  20039. if (subModeCh < 26) {
  20040. ch = /*(char)('A' + subModeCh)*/ String.fromCharCode(65 + subModeCh);
  20041. }
  20042. else {
  20043. switch (subModeCh) {
  20044. case 26:
  20045. ch = ' ';
  20046. break;
  20047. case DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH:
  20048. subMode = Mode$1.ALPHA;
  20049. break;
  20050. }
  20051. }
  20052. break;
  20053. case Mode$1.PUNCT_SHIFT:
  20054. // Restore sub-mode
  20055. subMode = priorToShiftMode;
  20056. if (subModeCh < DecodedBitStreamParser.PAL) {
  20057. ch = DecodedBitStreamParser.PUNCT_CHARS[subModeCh];
  20058. }
  20059. else {
  20060. switch (subModeCh) {
  20061. case DecodedBitStreamParser.PAL:
  20062. subMode = Mode$1.ALPHA;
  20063. break;
  20064. case DecodedBitStreamParser.MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
  20065. // PS before Shift-to-Byte is used as a padding character,
  20066. // see 5.4.2.4 of the specification
  20067. result.append(/*(char)*/ byteCompactionData[i]);
  20068. break;
  20069. case DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH:
  20070. subMode = Mode$1.ALPHA;
  20071. break;
  20072. }
  20073. }
  20074. break;
  20075. }
  20076. // if (ch !== 0) {
  20077. if (ch !== '') {
  20078. // Append decoded character to result
  20079. result.append(ch);
  20080. }
  20081. i++;
  20082. }
  20083. }
  20084. /**
  20085. * Byte Compaction mode (see 5.4.3) permits all 256 possible 8-bit byte values to be encoded.
  20086. * This includes all ASCII characters value 0 to 127 inclusive and provides for international
  20087. * character set support.
  20088. *
  20089. * @param mode The byte compaction mode i.e. 901 or 924
  20090. * @param codewords The array of codewords (data + error)
  20091. * @param encoding Currently active character encoding
  20092. * @param codeIndex The current index into the codeword array.
  20093. * @param result The decoded data is appended to the result.
  20094. * @return The next index into the codeword array.
  20095. */
  20096. static /*int*/ byteCompaction(mode, codewords, encoding, codeIndex, result) {
  20097. let decodedBytes = new ByteArrayOutputStream();
  20098. let count = 0;
  20099. let value = /*long*/ 0;
  20100. let end = false;
  20101. switch (mode) {
  20102. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH:
  20103. // Total number of Byte Compaction characters to be encoded
  20104. // is not a multiple of 6
  20105. let byteCompactedCodewords = new Int32Array(6);
  20106. let nextCode = codewords[codeIndex++];
  20107. while ((codeIndex < codewords[0]) && !end) {
  20108. byteCompactedCodewords[count++] = nextCode;
  20109. // Base 900
  20110. value = 900 * value + nextCode;
  20111. nextCode = codewords[codeIndex++];
  20112. // perhaps it should be ok to check only nextCode >= TEXT_COMPACTION_MODE_LATCH
  20113. switch (nextCode) {
  20114. case DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH:
  20115. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH:
  20116. case DecodedBitStreamParser.NUMERIC_COMPACTION_MODE_LATCH:
  20117. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH_6:
  20118. case DecodedBitStreamParser.BEGIN_MACRO_PDF417_CONTROL_BLOCK:
  20119. case DecodedBitStreamParser.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
  20120. case DecodedBitStreamParser.MACRO_PDF417_TERMINATOR:
  20121. codeIndex--;
  20122. end = true;
  20123. break;
  20124. default:
  20125. if ((count % 5 === 0) && (count > 0)) {
  20126. // Decode every 5 codewords
  20127. // Convert to Base 256
  20128. for (let j /*int*/ = 0; j < 6; ++j) {
  20129. /* @note
  20130. * JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32 bits binary numbers.
  20131. * So the next bitwise operation could not be done with simple numbers
  20132. */
  20133. decodedBytes.write(/*(byte)*/ Number(createBigInt(value) >> createBigInt(8 * (5 - j))));
  20134. }
  20135. value = 0;
  20136. count = 0;
  20137. }
  20138. break;
  20139. }
  20140. }
  20141. // if the end of all codewords is reached the last codeword needs to be added
  20142. if (codeIndex === codewords[0] && nextCode < DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH) {
  20143. byteCompactedCodewords[count++] = nextCode;
  20144. }
  20145. // If Byte Compaction mode is invoked with codeword 901,
  20146. // the last group of codewords is interpreted directly
  20147. // as one byte per codeword, without compaction.
  20148. for (let i /*int*/ = 0; i < count; i++) {
  20149. decodedBytes.write(/*(byte)*/ byteCompactedCodewords[i]);
  20150. }
  20151. break;
  20152. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH_6:
  20153. // Total number of Byte Compaction characters to be encoded
  20154. // is an integer multiple of 6
  20155. while (codeIndex < codewords[0] && !end) {
  20156. let code = codewords[codeIndex++];
  20157. if (code < DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH) {
  20158. count++;
  20159. // Base 900
  20160. value = 900 * value + code;
  20161. }
  20162. else {
  20163. switch (code) {
  20164. case DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH:
  20165. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH:
  20166. case DecodedBitStreamParser.NUMERIC_COMPACTION_MODE_LATCH:
  20167. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH_6:
  20168. case DecodedBitStreamParser.BEGIN_MACRO_PDF417_CONTROL_BLOCK:
  20169. case DecodedBitStreamParser.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
  20170. case DecodedBitStreamParser.MACRO_PDF417_TERMINATOR:
  20171. codeIndex--;
  20172. end = true;
  20173. break;
  20174. }
  20175. }
  20176. if ((count % 5 === 0) && (count > 0)) {
  20177. // Decode every 5 codewords
  20178. // Convert to Base 256
  20179. /* @note
  20180. * JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32 bits binary numbers.
  20181. * So the next bitwise operation could not be done with simple numbers
  20182. */
  20183. for (let j /*int*/ = 0; j < 6; ++j) {
  20184. decodedBytes.write(/*(byte)*/ Number(createBigInt(value) >> createBigInt(8 * (5 - j))));
  20185. }
  20186. value = 0;
  20187. count = 0;
  20188. }
  20189. }
  20190. break;
  20191. }
  20192. result.append(StringEncoding.decode(decodedBytes.toByteArray(), encoding));
  20193. return codeIndex;
  20194. }
  20195. /**
  20196. * Numeric Compaction mode (see 5.4.4) permits efficient encoding of numeric data strings.
  20197. *
  20198. * @param codewords The array of codewords (data + error)
  20199. * @param codeIndex The current index into the codeword array.
  20200. * @param result The decoded data is appended to the result.
  20201. * @return The next index into the codeword array.
  20202. *
  20203. * @throws FormatException
  20204. */
  20205. static numericCompaction(codewords, codeIndex /*int*/, result) {
  20206. let count = 0;
  20207. let end = false;
  20208. let numericCodewords = new Int32Array(DecodedBitStreamParser.MAX_NUMERIC_CODEWORDS);
  20209. while (codeIndex < codewords[0] && !end) {
  20210. let code = codewords[codeIndex++];
  20211. if (codeIndex === codewords[0]) {
  20212. end = true;
  20213. }
  20214. if (code < DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH) {
  20215. numericCodewords[count] = code;
  20216. count++;
  20217. }
  20218. else {
  20219. switch (code) {
  20220. case DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH:
  20221. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH:
  20222. case DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH_6:
  20223. case DecodedBitStreamParser.BEGIN_MACRO_PDF417_CONTROL_BLOCK:
  20224. case DecodedBitStreamParser.BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
  20225. case DecodedBitStreamParser.MACRO_PDF417_TERMINATOR:
  20226. codeIndex--;
  20227. end = true;
  20228. break;
  20229. }
  20230. }
  20231. if ((count % DecodedBitStreamParser.MAX_NUMERIC_CODEWORDS === 0 || code === DecodedBitStreamParser.NUMERIC_COMPACTION_MODE_LATCH || end) && count > 0) {
  20232. // Re-invoking Numeric Compaction mode (by using codeword 902
  20233. // while in Numeric Compaction mode) serves to terminate the
  20234. // current Numeric Compaction mode grouping as described in 5.4.4.2,
  20235. // and then to start a new one grouping.
  20236. result.append(DecodedBitStreamParser.decodeBase900toBase10(numericCodewords, count));
  20237. count = 0;
  20238. }
  20239. }
  20240. return codeIndex;
  20241. }
  20242. /**
  20243. * Convert a list of Numeric Compacted codewords from Base 900 to Base 10.
  20244. *
  20245. * @param codewords The array of codewords
  20246. * @param count The number of codewords
  20247. * @return The decoded string representing the Numeric data.
  20248. *
  20249. * EXAMPLE
  20250. * Encode the fifteen digit numeric string 000213298174000
  20251. * Prefix the numeric string with a 1 and set the initial value of
  20252. * t = 1 000 213 298 174 000
  20253. * Calculate codeword 0
  20254. * d0 = 1 000 213 298 174 000 mod 900 = 200
  20255. *
  20256. * t = 1 000 213 298 174 000 div 900 = 1 111 348 109 082
  20257. * Calculate codeword 1
  20258. * d1 = 1 111 348 109 082 mod 900 = 282
  20259. *
  20260. * t = 1 111 348 109 082 div 900 = 1 234 831 232
  20261. * Calculate codeword 2
  20262. * d2 = 1 234 831 232 mod 900 = 632
  20263. *
  20264. * t = 1 234 831 232 div 900 = 1 372 034
  20265. * Calculate codeword 3
  20266. * d3 = 1 372 034 mod 900 = 434
  20267. *
  20268. * t = 1 372 034 div 900 = 1 524
  20269. * Calculate codeword 4
  20270. * d4 = 1 524 mod 900 = 624
  20271. *
  20272. * t = 1 524 div 900 = 1
  20273. * Calculate codeword 5
  20274. * d5 = 1 mod 900 = 1
  20275. * t = 1 div 900 = 0
  20276. * Codeword sequence is: 1, 624, 434, 632, 282, 200
  20277. *
  20278. * Decode the above codewords involves
  20279. * 1 x 900 power of 5 + 624 x 900 power of 4 + 434 x 900 power of 3 +
  20280. * 632 x 900 power of 2 + 282 x 900 power of 1 + 200 x 900 power of 0 = 1000213298174000
  20281. *
  20282. * Remove leading 1 => Result is 000213298174000
  20283. *
  20284. * @throws FormatException
  20285. */
  20286. static decodeBase900toBase10(codewords, count) {
  20287. let result = createBigInt(0);
  20288. for (let i /*int*/ = 0; i < count; i++) {
  20289. result += DecodedBitStreamParser.EXP900[count - i - 1] * createBigInt(codewords[i]);
  20290. }
  20291. let resultString = result.toString();
  20292. if (resultString.charAt(0) !== '1') {
  20293. throw new FormatException();
  20294. }
  20295. return resultString.substring(1);
  20296. }
  20297. }
  20298. DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH = 900;
  20299. DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH = 901;
  20300. DecodedBitStreamParser.NUMERIC_COMPACTION_MODE_LATCH = 902;
  20301. DecodedBitStreamParser.BYTE_COMPACTION_MODE_LATCH_6 = 924;
  20302. DecodedBitStreamParser.ECI_USER_DEFINED = 925;
  20303. DecodedBitStreamParser.ECI_GENERAL_PURPOSE = 926;
  20304. DecodedBitStreamParser.ECI_CHARSET = 927;
  20305. DecodedBitStreamParser.BEGIN_MACRO_PDF417_CONTROL_BLOCK = 928;
  20306. DecodedBitStreamParser.BEGIN_MACRO_PDF417_OPTIONAL_FIELD = 923;
  20307. DecodedBitStreamParser.MACRO_PDF417_TERMINATOR = 922;
  20308. DecodedBitStreamParser.MODE_SHIFT_TO_BYTE_COMPACTION_MODE = 913;
  20309. DecodedBitStreamParser.MAX_NUMERIC_CODEWORDS = 15;
  20310. DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_FILE_NAME = 0;
  20311. DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_SEGMENT_COUNT = 1;
  20312. DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_TIME_STAMP = 2;
  20313. DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_SENDER = 3;
  20314. DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_ADDRESSEE = 4;
  20315. DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_FILE_SIZE = 5;
  20316. DecodedBitStreamParser.MACRO_PDF417_OPTIONAL_FIELD_CHECKSUM = 6;
  20317. DecodedBitStreamParser.PL = 25;
  20318. DecodedBitStreamParser.LL = 27;
  20319. DecodedBitStreamParser.AS = 27;
  20320. DecodedBitStreamParser.ML = 28;
  20321. DecodedBitStreamParser.AL = 28;
  20322. DecodedBitStreamParser.PS = 29;
  20323. DecodedBitStreamParser.PAL = 29;
  20324. DecodedBitStreamParser.PUNCT_CHARS = ';<>@[\\]_`~!\r\t,:\n-.$/"|*()?{}\'';
  20325. DecodedBitStreamParser.MIXED_CHARS = '0123456789&\r\t,:#-.$/+%*=^';
  20326. /**
  20327. * Table containing values for the exponent of 900.
  20328. * This is used in the numeric compaction decode algorithm.
  20329. */
  20330. DecodedBitStreamParser.EXP900 = getBigIntConstructor() ? getEXP900() : [];
  20331. DecodedBitStreamParser.NUMBER_OF_SEQUENCE_CODEWORDS = 2;
  20332. /*
  20333. * Copyright 2013 ZXing authors
  20334. *
  20335. * Licensed under the Apache License, Version 2.0 (the "License");
  20336. * you may not use this file except in compliance with the License.
  20337. * You may obtain a copy of the License at
  20338. *
  20339. * http://www.apache.org/licenses/LICENSE-2.0
  20340. *
  20341. * Unless required by applicable law or agreed to in writing, software
  20342. * distributed under the License is distributed on an "AS IS" BASIS,
  20343. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20344. * See the License for the specific language governing permissions and
  20345. * limitations under the License.
  20346. */
  20347. // import java.util.ArrayList;
  20348. // import java.util.Collection;
  20349. // import java.util.Formatter;
  20350. // import java.util.List;
  20351. /**
  20352. * @author Guenther Grau
  20353. */
  20354. /*public final*/ class PDF417ScanningDecoder {
  20355. constructor() { }
  20356. /**
  20357. * @TODO don't pass in minCodewordWidth and maxCodewordWidth, pass in barcode columns for start and stop pattern
  20358. *
  20359. * columns. That way width can be deducted from the pattern column.
  20360. * This approach also allows to detect more details about the barcode, e.g. if a bar type (white or black) is wider
  20361. * than it should be. This can happen if the scanner used a bad blackpoint.
  20362. *
  20363. * @param BitMatrix
  20364. * @param image
  20365. * @param ResultPoint
  20366. * @param imageTopLeft
  20367. * @param ResultPoint
  20368. * @param imageBottomLeft
  20369. * @param ResultPoint
  20370. * @param imageTopRight
  20371. * @param ResultPoint
  20372. * @param imageBottomRight
  20373. * @param int
  20374. * @param minCodewordWidth
  20375. * @param int
  20376. * @param maxCodewordWidth
  20377. *
  20378. * @throws NotFoundException
  20379. * @throws FormatException
  20380. * @throws ChecksumException
  20381. */
  20382. static decode(image, imageTopLeft, imageBottomLeft, imageTopRight, imageBottomRight, minCodewordWidth, maxCodewordWidth) {
  20383. let boundingBox = new BoundingBox(image, imageTopLeft, imageBottomLeft, imageTopRight, imageBottomRight);
  20384. let leftRowIndicatorColumn = null;
  20385. let rightRowIndicatorColumn = null;
  20386. let detectionResult;
  20387. for (let firstPass /*boolean*/ = true;; firstPass = false) {
  20388. if (imageTopLeft != null) {
  20389. leftRowIndicatorColumn = PDF417ScanningDecoder.getRowIndicatorColumn(image, boundingBox, imageTopLeft, true, minCodewordWidth, maxCodewordWidth);
  20390. }
  20391. if (imageTopRight != null) {
  20392. rightRowIndicatorColumn = PDF417ScanningDecoder.getRowIndicatorColumn(image, boundingBox, imageTopRight, false, minCodewordWidth, maxCodewordWidth);
  20393. }
  20394. detectionResult = PDF417ScanningDecoder.merge(leftRowIndicatorColumn, rightRowIndicatorColumn);
  20395. if (detectionResult == null) {
  20396. throw NotFoundException.getNotFoundInstance();
  20397. }
  20398. let resultBox = detectionResult.getBoundingBox();
  20399. if (firstPass && resultBox != null &&
  20400. (resultBox.getMinY() < boundingBox.getMinY() || resultBox.getMaxY() > boundingBox.getMaxY())) {
  20401. boundingBox = resultBox;
  20402. }
  20403. else {
  20404. break;
  20405. }
  20406. }
  20407. detectionResult.setBoundingBox(boundingBox);
  20408. let maxBarcodeColumn = detectionResult.getBarcodeColumnCount() + 1;
  20409. detectionResult.setDetectionResultColumn(0, leftRowIndicatorColumn);
  20410. detectionResult.setDetectionResultColumn(maxBarcodeColumn, rightRowIndicatorColumn);
  20411. let leftToRight = leftRowIndicatorColumn != null;
  20412. for (let barcodeColumnCount /*int*/ = 1; barcodeColumnCount <= maxBarcodeColumn; barcodeColumnCount++) {
  20413. let barcodeColumn = leftToRight ? barcodeColumnCount : maxBarcodeColumn - barcodeColumnCount;
  20414. if (detectionResult.getDetectionResultColumn(barcodeColumn) !== /* null */ undefined) {
  20415. // This will be the case for the opposite row indicator column, which doesn't need to be decoded again.
  20416. continue;
  20417. }
  20418. let detectionResultColumn;
  20419. if (barcodeColumn === 0 || barcodeColumn === maxBarcodeColumn) {
  20420. detectionResultColumn = new DetectionResultRowIndicatorColumn(boundingBox, barcodeColumn === 0);
  20421. }
  20422. else {
  20423. detectionResultColumn = new DetectionResultColumn(boundingBox);
  20424. }
  20425. detectionResult.setDetectionResultColumn(barcodeColumn, detectionResultColumn);
  20426. let startColumn = -1;
  20427. let previousStartColumn = startColumn;
  20428. // TODO start at a row for which we know the start position, then detect upwards and downwards from there.
  20429. for (let imageRow /*int*/ = boundingBox.getMinY(); imageRow <= boundingBox.getMaxY(); imageRow++) {
  20430. startColumn = PDF417ScanningDecoder.getStartColumn(detectionResult, barcodeColumn, imageRow, leftToRight);
  20431. if (startColumn < 0 || startColumn > boundingBox.getMaxX()) {
  20432. if (previousStartColumn === -1) {
  20433. continue;
  20434. }
  20435. startColumn = previousStartColumn;
  20436. }
  20437. let codeword = PDF417ScanningDecoder.detectCodeword(image, boundingBox.getMinX(), boundingBox.getMaxX(), leftToRight, startColumn, imageRow, minCodewordWidth, maxCodewordWidth);
  20438. if (codeword != null) {
  20439. detectionResultColumn.setCodeword(imageRow, codeword);
  20440. previousStartColumn = startColumn;
  20441. minCodewordWidth = Math.min(minCodewordWidth, codeword.getWidth());
  20442. maxCodewordWidth = Math.max(maxCodewordWidth, codeword.getWidth());
  20443. }
  20444. }
  20445. }
  20446. return PDF417ScanningDecoder.createDecoderResult(detectionResult);
  20447. }
  20448. /**
  20449. *
  20450. * @param leftRowIndicatorColumn
  20451. * @param rightRowIndicatorColumn
  20452. *
  20453. * @throws NotFoundException
  20454. */
  20455. static merge(leftRowIndicatorColumn, rightRowIndicatorColumn) {
  20456. if (leftRowIndicatorColumn == null && rightRowIndicatorColumn == null) {
  20457. return null;
  20458. }
  20459. let barcodeMetadata = PDF417ScanningDecoder.getBarcodeMetadata(leftRowIndicatorColumn, rightRowIndicatorColumn);
  20460. if (barcodeMetadata == null) {
  20461. return null;
  20462. }
  20463. let boundingBox = BoundingBox.merge(PDF417ScanningDecoder.adjustBoundingBox(leftRowIndicatorColumn), PDF417ScanningDecoder.adjustBoundingBox(rightRowIndicatorColumn));
  20464. return new DetectionResult(barcodeMetadata, boundingBox);
  20465. }
  20466. /**
  20467. *
  20468. * @param rowIndicatorColumn
  20469. *
  20470. * @throws NotFoundException
  20471. */
  20472. static adjustBoundingBox(rowIndicatorColumn) {
  20473. if (rowIndicatorColumn == null) {
  20474. return null;
  20475. }
  20476. let rowHeights = rowIndicatorColumn.getRowHeights();
  20477. if (rowHeights == null) {
  20478. return null;
  20479. }
  20480. let maxRowHeight = PDF417ScanningDecoder.getMax(rowHeights);
  20481. let missingStartRows = 0;
  20482. for (let rowHeight /*int*/ of rowHeights) {
  20483. missingStartRows += maxRowHeight - rowHeight;
  20484. if (rowHeight > 0) {
  20485. break;
  20486. }
  20487. }
  20488. let codewords = rowIndicatorColumn.getCodewords();
  20489. for (let row /*int*/ = 0; missingStartRows > 0 && codewords[row] == null; row++) {
  20490. missingStartRows--;
  20491. }
  20492. let missingEndRows = 0;
  20493. for (let row /*int*/ = rowHeights.length - 1; row >= 0; row--) {
  20494. missingEndRows += maxRowHeight - rowHeights[row];
  20495. if (rowHeights[row] > 0) {
  20496. break;
  20497. }
  20498. }
  20499. for (let row /*int*/ = codewords.length - 1; missingEndRows > 0 && codewords[row] == null; row--) {
  20500. missingEndRows--;
  20501. }
  20502. return rowIndicatorColumn.getBoundingBox().addMissingRows(missingStartRows, missingEndRows, rowIndicatorColumn.isLeft());
  20503. }
  20504. static getMax(values) {
  20505. let maxValue = -1;
  20506. for (let value /*int*/ of values) {
  20507. maxValue = Math.max(maxValue, value);
  20508. }
  20509. return maxValue;
  20510. }
  20511. static getBarcodeMetadata(leftRowIndicatorColumn, rightRowIndicatorColumn) {
  20512. let leftBarcodeMetadata;
  20513. if (leftRowIndicatorColumn == null ||
  20514. (leftBarcodeMetadata = leftRowIndicatorColumn.getBarcodeMetadata()) == null) {
  20515. return rightRowIndicatorColumn == null ? null : rightRowIndicatorColumn.getBarcodeMetadata();
  20516. }
  20517. let rightBarcodeMetadata;
  20518. if (rightRowIndicatorColumn == null ||
  20519. (rightBarcodeMetadata = rightRowIndicatorColumn.getBarcodeMetadata()) == null) {
  20520. return leftBarcodeMetadata;
  20521. }
  20522. if (leftBarcodeMetadata.getColumnCount() !== rightBarcodeMetadata.getColumnCount() &&
  20523. leftBarcodeMetadata.getErrorCorrectionLevel() !== rightBarcodeMetadata.getErrorCorrectionLevel() &&
  20524. leftBarcodeMetadata.getRowCount() !== rightBarcodeMetadata.getRowCount()) {
  20525. return null;
  20526. }
  20527. return leftBarcodeMetadata;
  20528. }
  20529. static getRowIndicatorColumn(image, boundingBox, startPoint, leftToRight, minCodewordWidth, maxCodewordWidth) {
  20530. let rowIndicatorColumn = new DetectionResultRowIndicatorColumn(boundingBox, leftToRight);
  20531. for (let i /*int*/ = 0; i < 2; i++) {
  20532. let increment = i === 0 ? 1 : -1;
  20533. let startColumn = Math.trunc(Math.trunc(startPoint.getX()));
  20534. for (let imageRow /*int*/ = Math.trunc(Math.trunc(startPoint.getY())); imageRow <= boundingBox.getMaxY() &&
  20535. imageRow >= boundingBox.getMinY(); imageRow += increment) {
  20536. let codeword = PDF417ScanningDecoder.detectCodeword(image, 0, image.getWidth(), leftToRight, startColumn, imageRow, minCodewordWidth, maxCodewordWidth);
  20537. if (codeword != null) {
  20538. rowIndicatorColumn.setCodeword(imageRow, codeword);
  20539. if (leftToRight) {
  20540. startColumn = codeword.getStartX();
  20541. }
  20542. else {
  20543. startColumn = codeword.getEndX();
  20544. }
  20545. }
  20546. }
  20547. }
  20548. return rowIndicatorColumn;
  20549. }
  20550. /**
  20551. *
  20552. * @param detectionResult
  20553. * @param BarcodeValue
  20554. * @param param2
  20555. * @param param3
  20556. * @param barcodeMatrix
  20557. *
  20558. * @throws NotFoundException
  20559. */
  20560. static adjustCodewordCount(detectionResult, barcodeMatrix) {
  20561. let barcodeMatrix01 = barcodeMatrix[0][1];
  20562. let numberOfCodewords = barcodeMatrix01.getValue();
  20563. let calculatedNumberOfCodewords = detectionResult.getBarcodeColumnCount() *
  20564. detectionResult.getBarcodeRowCount() -
  20565. PDF417ScanningDecoder.getNumberOfECCodeWords(detectionResult.getBarcodeECLevel());
  20566. if (numberOfCodewords.length === 0) {
  20567. if (calculatedNumberOfCodewords < 1 || calculatedNumberOfCodewords > PDF417Common.MAX_CODEWORDS_IN_BARCODE) {
  20568. throw NotFoundException.getNotFoundInstance();
  20569. }
  20570. barcodeMatrix01.setValue(calculatedNumberOfCodewords);
  20571. }
  20572. else if (numberOfCodewords[0] !== calculatedNumberOfCodewords) {
  20573. // The calculated one is more reliable as it is derived from the row indicator columns
  20574. barcodeMatrix01.setValue(calculatedNumberOfCodewords);
  20575. }
  20576. }
  20577. /**
  20578. *
  20579. * @param detectionResult
  20580. *
  20581. * @throws FormatException
  20582. * @throws ChecksumException
  20583. * @throws NotFoundException
  20584. */
  20585. static createDecoderResult(detectionResult) {
  20586. let barcodeMatrix = PDF417ScanningDecoder.createBarcodeMatrix(detectionResult);
  20587. PDF417ScanningDecoder.adjustCodewordCount(detectionResult, barcodeMatrix);
  20588. let erasures /*Collection<Integer>*/ = new Array();
  20589. let codewords = new Int32Array(detectionResult.getBarcodeRowCount() * detectionResult.getBarcodeColumnCount());
  20590. let ambiguousIndexValuesList = /*List<int[]>*/ [];
  20591. let ambiguousIndexesList = /*Collection<Integer>*/ new Array();
  20592. for (let row /*int*/ = 0; row < detectionResult.getBarcodeRowCount(); row++) {
  20593. for (let column /*int*/ = 0; column < detectionResult.getBarcodeColumnCount(); column++) {
  20594. let values = barcodeMatrix[row][column + 1].getValue();
  20595. let codewordIndex = row * detectionResult.getBarcodeColumnCount() + column;
  20596. if (values.length === 0) {
  20597. erasures.push(codewordIndex);
  20598. }
  20599. else if (values.length === 1) {
  20600. codewords[codewordIndex] = values[0];
  20601. }
  20602. else {
  20603. ambiguousIndexesList.push(codewordIndex);
  20604. ambiguousIndexValuesList.push(values);
  20605. }
  20606. }
  20607. }
  20608. let ambiguousIndexValues = new Array(ambiguousIndexValuesList.length);
  20609. for (let i /*int*/ = 0; i < ambiguousIndexValues.length; i++) {
  20610. ambiguousIndexValues[i] = ambiguousIndexValuesList[i];
  20611. }
  20612. return PDF417ScanningDecoder.createDecoderResultFromAmbiguousValues(detectionResult.getBarcodeECLevel(), codewords, PDF417Common.toIntArray(erasures), PDF417Common.toIntArray(ambiguousIndexesList), ambiguousIndexValues);
  20613. }
  20614. /**
  20615. * This method deals with the fact, that the decoding process doesn't always yield a single most likely value. The
  20616. * current error correction implementation doesn't deal with erasures very well, so it's better to provide a value
  20617. * for these ambiguous codewords instead of treating it as an erasure. The problem is that we don't know which of
  20618. * the ambiguous values to choose. We try decode using the first value, and if that fails, we use another of the
  20619. * ambiguous values and try to decode again. This usually only happens on very hard to read and decode barcodes,
  20620. * so decoding the normal barcodes is not affected by this.
  20621. *
  20622. * @param erasureArray contains the indexes of erasures
  20623. * @param ambiguousIndexes array with the indexes that have more than one most likely value
  20624. * @param ambiguousIndexValues two dimensional array that contains the ambiguous values. The first dimension must
  20625. * be the same length as the ambiguousIndexes array
  20626. *
  20627. * @throws FormatException
  20628. * @throws ChecksumException
  20629. */
  20630. static createDecoderResultFromAmbiguousValues(ecLevel, codewords, erasureArray, ambiguousIndexes, ambiguousIndexValues) {
  20631. let ambiguousIndexCount = new Int32Array(ambiguousIndexes.length);
  20632. let tries = 100;
  20633. while (tries-- > 0) {
  20634. for (let i /*int*/ = 0; i < ambiguousIndexCount.length; i++) {
  20635. codewords[ambiguousIndexes[i]] = ambiguousIndexValues[i][ambiguousIndexCount[i]];
  20636. }
  20637. try {
  20638. return PDF417ScanningDecoder.decodeCodewords(codewords, ecLevel, erasureArray);
  20639. }
  20640. catch (err) {
  20641. let ignored = err instanceof ChecksumException;
  20642. if (!ignored) {
  20643. throw err;
  20644. }
  20645. }
  20646. if (ambiguousIndexCount.length === 0) {
  20647. throw ChecksumException.getChecksumInstance();
  20648. }
  20649. for (let i /*int*/ = 0; i < ambiguousIndexCount.length; i++) {
  20650. if (ambiguousIndexCount[i] < ambiguousIndexValues[i].length - 1) {
  20651. ambiguousIndexCount[i]++;
  20652. break;
  20653. }
  20654. else {
  20655. ambiguousIndexCount[i] = 0;
  20656. if (i === ambiguousIndexCount.length - 1) {
  20657. throw ChecksumException.getChecksumInstance();
  20658. }
  20659. }
  20660. }
  20661. }
  20662. throw ChecksumException.getChecksumInstance();
  20663. }
  20664. static createBarcodeMatrix(detectionResult) {
  20665. // let barcodeMatrix: BarcodeValue[][] =
  20666. // new BarcodeValue[detectionResult.getBarcodeRowCount()][detectionResult.getBarcodeColumnCount() + 2];
  20667. let barcodeMatrix = Array.from({ length: detectionResult.getBarcodeRowCount() }, () => new Array(detectionResult.getBarcodeColumnCount() + 2));
  20668. for (let row /*int*/ = 0; row < barcodeMatrix.length; row++) {
  20669. for (let column /*int*/ = 0; column < barcodeMatrix[row].length; column++) {
  20670. barcodeMatrix[row][column] = new BarcodeValue();
  20671. }
  20672. }
  20673. let column = 0;
  20674. for (let detectionResultColumn /*DetectionResultColumn*/ of detectionResult.getDetectionResultColumns()) {
  20675. if (detectionResultColumn != null) {
  20676. for (let codeword /*Codeword*/ of detectionResultColumn.getCodewords()) {
  20677. if (codeword != null) {
  20678. let rowNumber = codeword.getRowNumber();
  20679. if (rowNumber >= 0) {
  20680. if (rowNumber >= barcodeMatrix.length) {
  20681. // We have more rows than the barcode metadata allows for, ignore them.
  20682. continue;
  20683. }
  20684. barcodeMatrix[rowNumber][column].setValue(codeword.getValue());
  20685. }
  20686. }
  20687. }
  20688. }
  20689. column++;
  20690. }
  20691. return barcodeMatrix;
  20692. }
  20693. static isValidBarcodeColumn(detectionResult, barcodeColumn) {
  20694. return barcodeColumn >= 0 && barcodeColumn <= detectionResult.getBarcodeColumnCount() + 1;
  20695. }
  20696. static getStartColumn(detectionResult, barcodeColumn, imageRow, leftToRight) {
  20697. let offset = leftToRight ? 1 : -1;
  20698. let codeword = null;
  20699. if (PDF417ScanningDecoder.isValidBarcodeColumn(detectionResult, barcodeColumn - offset)) {
  20700. codeword = detectionResult.getDetectionResultColumn(barcodeColumn - offset).getCodeword(imageRow);
  20701. }
  20702. if (codeword != null) {
  20703. return leftToRight ? codeword.getEndX() : codeword.getStartX();
  20704. }
  20705. codeword = detectionResult.getDetectionResultColumn(barcodeColumn).getCodewordNearby(imageRow);
  20706. if (codeword != null) {
  20707. return leftToRight ? codeword.getStartX() : codeword.getEndX();
  20708. }
  20709. if (PDF417ScanningDecoder.isValidBarcodeColumn(detectionResult, barcodeColumn - offset)) {
  20710. codeword = detectionResult.getDetectionResultColumn(barcodeColumn - offset).getCodewordNearby(imageRow);
  20711. }
  20712. if (codeword != null) {
  20713. return leftToRight ? codeword.getEndX() : codeword.getStartX();
  20714. }
  20715. let skippedColumns = 0;
  20716. while (PDF417ScanningDecoder.isValidBarcodeColumn(detectionResult, barcodeColumn - offset)) {
  20717. barcodeColumn -= offset;
  20718. for (let previousRowCodeword /*Codeword*/ of detectionResult.getDetectionResultColumn(barcodeColumn).getCodewords()) {
  20719. if (previousRowCodeword != null) {
  20720. return (leftToRight ? previousRowCodeword.getEndX() : previousRowCodeword.getStartX()) +
  20721. offset *
  20722. skippedColumns *
  20723. (previousRowCodeword.getEndX() - previousRowCodeword.getStartX());
  20724. }
  20725. }
  20726. skippedColumns++;
  20727. }
  20728. return leftToRight ? detectionResult.getBoundingBox().getMinX() : detectionResult.getBoundingBox().getMaxX();
  20729. }
  20730. static detectCodeword(image, minColumn, maxColumn, leftToRight, startColumn, imageRow, minCodewordWidth, maxCodewordWidth) {
  20731. startColumn = PDF417ScanningDecoder.adjustCodewordStartColumn(image, minColumn, maxColumn, leftToRight, startColumn, imageRow);
  20732. // we usually know fairly exact now how long a codeword is. We should provide minimum and maximum expected length
  20733. // and try to adjust the read pixels, e.g. remove single pixel errors or try to cut off exceeding pixels.
  20734. // min and maxCodewordWidth should not be used as they are calculated for the whole barcode an can be inaccurate
  20735. // for the current position
  20736. let moduleBitCount = PDF417ScanningDecoder.getModuleBitCount(image, minColumn, maxColumn, leftToRight, startColumn, imageRow);
  20737. if (moduleBitCount == null) {
  20738. return null;
  20739. }
  20740. let endColumn;
  20741. let codewordBitCount = MathUtils.sum(moduleBitCount);
  20742. if (leftToRight) {
  20743. endColumn = startColumn + codewordBitCount;
  20744. }
  20745. else {
  20746. for (let i /*int*/ = 0; i < moduleBitCount.length / 2; i++) {
  20747. let tmpCount = moduleBitCount[i];
  20748. moduleBitCount[i] = moduleBitCount[moduleBitCount.length - 1 - i];
  20749. moduleBitCount[moduleBitCount.length - 1 - i] = tmpCount;
  20750. }
  20751. endColumn = startColumn;
  20752. startColumn = endColumn - codewordBitCount;
  20753. }
  20754. // TODO implement check for width and correction of black and white bars
  20755. // use start (and maybe stop pattern) to determine if black bars are wider than white bars. If so, adjust.
  20756. // should probably done only for codewords with a lot more than 17 bits.
  20757. // The following fixes 10-1.png, which has wide black bars and small white bars
  20758. // for (let i /*int*/ = 0; i < moduleBitCount.length; i++) {
  20759. // if (i % 2 === 0) {
  20760. // moduleBitCount[i]--;
  20761. // } else {
  20762. // moduleBitCount[i]++;
  20763. // }
  20764. // }
  20765. // We could also use the width of surrounding codewords for more accurate results, but this seems
  20766. // sufficient for now
  20767. if (!PDF417ScanningDecoder.checkCodewordSkew(codewordBitCount, minCodewordWidth, maxCodewordWidth)) {
  20768. // We could try to use the startX and endX position of the codeword in the same column in the previous row,
  20769. // create the bit count from it and normalize it to 8. This would help with single pixel errors.
  20770. return null;
  20771. }
  20772. let decodedValue = PDF417CodewordDecoder.getDecodedValue(moduleBitCount);
  20773. let codeword = PDF417Common.getCodeword(decodedValue);
  20774. if (codeword === -1) {
  20775. return null;
  20776. }
  20777. return new Codeword(startColumn, endColumn, PDF417ScanningDecoder.getCodewordBucketNumber(decodedValue), codeword);
  20778. }
  20779. static getModuleBitCount(image, minColumn, maxColumn, leftToRight, startColumn, imageRow) {
  20780. let imageColumn = startColumn;
  20781. let moduleBitCount = new Int32Array(8);
  20782. let moduleNumber = 0;
  20783. let increment = leftToRight ? 1 : -1;
  20784. let previousPixelValue = leftToRight;
  20785. while ((leftToRight ? imageColumn < maxColumn : imageColumn >= minColumn) &&
  20786. moduleNumber < moduleBitCount.length) {
  20787. if (image.get(imageColumn, imageRow) === previousPixelValue) {
  20788. moduleBitCount[moduleNumber]++;
  20789. imageColumn += increment;
  20790. }
  20791. else {
  20792. moduleNumber++;
  20793. previousPixelValue = !previousPixelValue;
  20794. }
  20795. }
  20796. if (moduleNumber === moduleBitCount.length ||
  20797. ((imageColumn === (leftToRight ? maxColumn : minColumn)) &&
  20798. moduleNumber === moduleBitCount.length - 1)) {
  20799. return moduleBitCount;
  20800. }
  20801. return null;
  20802. }
  20803. static getNumberOfECCodeWords(barcodeECLevel) {
  20804. return 2 << barcodeECLevel;
  20805. }
  20806. static adjustCodewordStartColumn(image, minColumn, maxColumn, leftToRight, codewordStartColumn, imageRow) {
  20807. let correctedStartColumn = codewordStartColumn;
  20808. let increment = leftToRight ? -1 : 1;
  20809. // there should be no black pixels before the start column. If there are, then we need to start earlier.
  20810. for (let i /*int*/ = 0; i < 2; i++) {
  20811. while ((leftToRight ? correctedStartColumn >= minColumn : correctedStartColumn < maxColumn) &&
  20812. leftToRight === image.get(correctedStartColumn, imageRow)) {
  20813. if (Math.abs(codewordStartColumn - correctedStartColumn) > PDF417ScanningDecoder.CODEWORD_SKEW_SIZE) {
  20814. return codewordStartColumn;
  20815. }
  20816. correctedStartColumn += increment;
  20817. }
  20818. increment = -increment;
  20819. leftToRight = !leftToRight;
  20820. }
  20821. return correctedStartColumn;
  20822. }
  20823. static checkCodewordSkew(codewordSize, minCodewordWidth, maxCodewordWidth) {
  20824. return minCodewordWidth - PDF417ScanningDecoder.CODEWORD_SKEW_SIZE <= codewordSize &&
  20825. codewordSize <= maxCodewordWidth + PDF417ScanningDecoder.CODEWORD_SKEW_SIZE;
  20826. }
  20827. /**
  20828. * @throws FormatException,
  20829. * @throws ChecksumException
  20830. */
  20831. static decodeCodewords(codewords, ecLevel, erasures) {
  20832. if (codewords.length === 0) {
  20833. throw FormatException.getFormatInstance();
  20834. }
  20835. let numECCodewords = 1 << (ecLevel + 1);
  20836. let correctedErrorsCount = PDF417ScanningDecoder.correctErrors(codewords, erasures, numECCodewords);
  20837. PDF417ScanningDecoder.verifyCodewordCount(codewords, numECCodewords);
  20838. // Decode the codewords
  20839. let decoderResult = DecodedBitStreamParser.decode(codewords, '' + ecLevel);
  20840. decoderResult.setErrorsCorrected(correctedErrorsCount);
  20841. decoderResult.setErasures(erasures.length);
  20842. return decoderResult;
  20843. }
  20844. /**
  20845. * <p>Given data and error-correction codewords received, possibly corrupted by errors, attempts to
  20846. * correct the errors in-place.</p>
  20847. *
  20848. * @param codewords data and error correction codewords
  20849. * @param erasures positions of any known erasures
  20850. * @param numECCodewords number of error correction codewords that are available in codewords
  20851. * @throws ChecksumException if error correction fails
  20852. */
  20853. static correctErrors(codewords, erasures, numECCodewords) {
  20854. if (erasures != null &&
  20855. erasures.length > numECCodewords / 2 + PDF417ScanningDecoder.MAX_ERRORS ||
  20856. numECCodewords < 0 ||
  20857. numECCodewords > PDF417ScanningDecoder.MAX_EC_CODEWORDS) {
  20858. // Too many errors or EC Codewords is corrupted
  20859. throw ChecksumException.getChecksumInstance();
  20860. }
  20861. return PDF417ScanningDecoder.errorCorrection.decode(codewords, numECCodewords, erasures);
  20862. }
  20863. /**
  20864. * Verify that all is OK with the codeword array.
  20865. * @throws FormatException
  20866. */
  20867. static verifyCodewordCount(codewords, numECCodewords) {
  20868. if (codewords.length < 4) {
  20869. // Codeword array size should be at least 4 allowing for
  20870. // Count CW, At least one Data CW, Error Correction CW, Error Correction CW
  20871. throw FormatException.getFormatInstance();
  20872. }
  20873. // The first codeword, the Symbol Length Descriptor, shall always encode the total number of data
  20874. // codewords in the symbol, including the Symbol Length Descriptor itself, data codewords and pad
  20875. // codewords, but excluding the number of error correction codewords.
  20876. let numberOfCodewords = codewords[0];
  20877. if (numberOfCodewords > codewords.length) {
  20878. throw FormatException.getFormatInstance();
  20879. }
  20880. if (numberOfCodewords === 0) {
  20881. // Reset to the length of the array - 8 (Allow for at least level 3 Error Correction (8 Error Codewords)
  20882. if (numECCodewords < codewords.length) {
  20883. codewords[0] = codewords.length - numECCodewords;
  20884. }
  20885. else {
  20886. throw FormatException.getFormatInstance();
  20887. }
  20888. }
  20889. }
  20890. static getBitCountForCodeword(codeword) {
  20891. let result = new Int32Array(8);
  20892. let previousValue = 0;
  20893. let i = result.length - 1;
  20894. while (true) {
  20895. if ((codeword & 0x1) !== previousValue) {
  20896. previousValue = codeword & 0x1;
  20897. i--;
  20898. if (i < 0) {
  20899. break;
  20900. }
  20901. }
  20902. result[i]++;
  20903. codeword >>= 1;
  20904. }
  20905. return result;
  20906. }
  20907. static getCodewordBucketNumber(codeword) {
  20908. if (codeword instanceof Int32Array) {
  20909. return this.getCodewordBucketNumber_Int32Array(codeword);
  20910. }
  20911. return this.getCodewordBucketNumber_number(codeword);
  20912. }
  20913. static getCodewordBucketNumber_number(codeword) {
  20914. return PDF417ScanningDecoder.getCodewordBucketNumber(PDF417ScanningDecoder.getBitCountForCodeword(codeword));
  20915. }
  20916. static getCodewordBucketNumber_Int32Array(moduleBitCount) {
  20917. return (moduleBitCount[0] - moduleBitCount[2] + moduleBitCount[4] - moduleBitCount[6] + 9) % 9;
  20918. }
  20919. static toString(barcodeMatrix) {
  20920. let formatter = new Formatter();
  20921. // try (let formatter = new Formatter()) {
  20922. for (let row /*int*/ = 0; row < barcodeMatrix.length; row++) {
  20923. formatter.format('Row %2d: ', row);
  20924. for (let column /*int*/ = 0; column < barcodeMatrix[row].length; column++) {
  20925. let barcodeValue = barcodeMatrix[row][column];
  20926. if (barcodeValue.getValue().length === 0) {
  20927. formatter.format(' ', null);
  20928. }
  20929. else {
  20930. formatter.format('%4d(%2d)', barcodeValue.getValue()[0], barcodeValue.getConfidence(barcodeValue.getValue()[0]));
  20931. }
  20932. }
  20933. formatter.format('%n');
  20934. }
  20935. return formatter.toString();
  20936. // }
  20937. }
  20938. }
  20939. /*final*/ PDF417ScanningDecoder.CODEWORD_SKEW_SIZE = 2;
  20940. /*final*/ PDF417ScanningDecoder.MAX_ERRORS = 3;
  20941. /*final*/ PDF417ScanningDecoder.MAX_EC_CODEWORDS = 512;
  20942. /*final*/ PDF417ScanningDecoder.errorCorrection = new ErrorCorrection$1();
  20943. /*
  20944. * Copyright 2009 ZXing authors
  20945. *
  20946. * Licensed under the Apache License, Version 2.0 (the "License");
  20947. * you may not use this file except in compliance with the License.
  20948. * You may obtain a copy of the License at
  20949. *
  20950. * http://www.apache.org/licenses/LICENSE-2.0
  20951. *
  20952. * Unless required by applicable law or agreed to in writing, software
  20953. * distributed under the License is distributed on an "AS IS" BASIS,
  20954. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20955. * See the License for the specific language governing permissions and
  20956. * limitations under the License.
  20957. */
  20958. // import java.util.ArrayList;
  20959. // import java.util.List;
  20960. // import java.util.Map;
  20961. /**
  20962. * This implementation can detect and decode PDF417 codes in an image.
  20963. *
  20964. * @author Guenther Grau
  20965. */
  20966. /*public final*/ class PDF417Reader {
  20967. // private static /*final Result[]*/ EMPTY_RESULT_ARRAY: Result[] = new Result([0]);
  20968. /**
  20969. * Locates and decodes a PDF417 code in an image.
  20970. *
  20971. * @return a String representing the content encoded by the PDF417 code
  20972. * @throws NotFoundException if a PDF417 code cannot be found,
  20973. * @throws FormatException if a PDF417 cannot be decoded
  20974. * @throws ChecksumException
  20975. */
  20976. // @Override
  20977. decode(image, hints = null) {
  20978. let result = PDF417Reader.decode(image, hints, false);
  20979. if (result == null || result.length === 0 || result[0] == null) {
  20980. throw NotFoundException.getNotFoundInstance();
  20981. }
  20982. return result[0];
  20983. }
  20984. /**
  20985. *
  20986. * @param BinaryBitmap
  20987. * @param image
  20988. * @throws NotFoundException
  20989. */
  20990. // @Override
  20991. decodeMultiple(image, hints = null) {
  20992. try {
  20993. return PDF417Reader.decode(image, hints, true);
  20994. }
  20995. catch (ignored) {
  20996. if (ignored instanceof FormatException || ignored instanceof ChecksumException) {
  20997. throw NotFoundException.getNotFoundInstance();
  20998. }
  20999. throw ignored;
  21000. }
  21001. }
  21002. /**
  21003. *
  21004. * @param image
  21005. * @param hints
  21006. * @param multiple
  21007. *
  21008. * @throws NotFoundException
  21009. * @throws FormatExceptionß
  21010. * @throws ChecksumException
  21011. */
  21012. static decode(image, hints, multiple) {
  21013. const results = new Array();
  21014. const detectorResult = Detector.detectMultiple(image, hints, multiple);
  21015. for (const points of detectorResult.getPoints()) {
  21016. const decoderResult = PDF417ScanningDecoder.decode(detectorResult.getBits(), points[4], points[5], points[6], points[7], PDF417Reader.getMinCodewordWidth(points), PDF417Reader.getMaxCodewordWidth(points));
  21017. const result = new Result$1(decoderResult.getText(), decoderResult.getRawBytes(), undefined, points, BarcodeFormat$1.PDF_417);
  21018. result.putMetadata(ResultMetadataType$1.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel());
  21019. const pdf417ResultMetadata = decoderResult.getOther();
  21020. if (pdf417ResultMetadata != null) {
  21021. result.putMetadata(ResultMetadataType$1.PDF417_EXTRA_METADATA, pdf417ResultMetadata);
  21022. }
  21023. results.push(result);
  21024. }
  21025. return results.map(x => x);
  21026. }
  21027. static getMaxWidth(p1, p2) {
  21028. if (p1 == null || p2 == null) {
  21029. return 0;
  21030. }
  21031. return Math.trunc(Math.abs(p1.getX() - p2.getX()));
  21032. }
  21033. static getMinWidth(p1, p2) {
  21034. if (p1 == null || p2 == null) {
  21035. return Integer.MAX_VALUE;
  21036. }
  21037. return Math.trunc(Math.abs(p1.getX() - p2.getX()));
  21038. }
  21039. static getMaxCodewordWidth(p) {
  21040. return Math.floor(Math.max(Math.max(PDF417Reader.getMaxWidth(p[0], p[4]), PDF417Reader.getMaxWidth(p[6], p[2]) * PDF417Common.MODULES_IN_CODEWORD /
  21041. PDF417Common.MODULES_IN_STOP_PATTERN), Math.max(PDF417Reader.getMaxWidth(p[1], p[5]), PDF417Reader.getMaxWidth(p[7], p[3]) * PDF417Common.MODULES_IN_CODEWORD /
  21042. PDF417Common.MODULES_IN_STOP_PATTERN)));
  21043. }
  21044. static getMinCodewordWidth(p) {
  21045. return Math.floor(Math.min(Math.min(PDF417Reader.getMinWidth(p[0], p[4]), PDF417Reader.getMinWidth(p[6], p[2]) * PDF417Common.MODULES_IN_CODEWORD /
  21046. PDF417Common.MODULES_IN_STOP_PATTERN), Math.min(PDF417Reader.getMinWidth(p[1], p[5]), PDF417Reader.getMinWidth(p[7], p[3]) * PDF417Common.MODULES_IN_CODEWORD /
  21047. PDF417Common.MODULES_IN_STOP_PATTERN)));
  21048. }
  21049. // @Override
  21050. reset() {
  21051. // nothing needs to be reset
  21052. }
  21053. }
  21054. /**
  21055. * Custom Error class of type Exception.
  21056. */
  21057. class ReaderException extends Exception {
  21058. }
  21059. ReaderException.kind = 'ReaderException';
  21060. /*
  21061. * Copyright 2009 ZXing authors
  21062. *
  21063. * Licensed under the Apache License, Version 2.0 (the "License");
  21064. * you may not use this file except in compliance with the License.
  21065. * You may obtain a copy of the License at
  21066. *
  21067. * http://www.apache.org/licenses/LICENSE-2.0
  21068. *
  21069. * Unless required by applicable law or agreed to in writing, software
  21070. * distributed under the License is distributed on an "AS IS" BASIS,
  21071. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21072. * See the License for the specific language governing permissions and
  21073. * limitations under the License.
  21074. */
  21075. /*namespace com.google.zxing {*/
  21076. /**
  21077. * MultiFormatReader is a convenience class and the main entry point into the library for most uses.
  21078. * By default it attempts to decode all barcode formats that the library supports. Optionally, you
  21079. * can provide a hints object to request different behavior, for example only decoding QR codes.
  21080. *
  21081. * @author Sean Owen
  21082. * @author dswitkin@google.com (Daniel Switkin)
  21083. */
  21084. class MultiFormatReader {
  21085. /**
  21086. * This version of decode honors the intent of Reader.decode(BinaryBitmap) in that it
  21087. * passes null as a hint to the decoders. However, that makes it inefficient to call repeatedly.
  21088. * Use setHints() followed by decodeWithState() for continuous scan applications.
  21089. *
  21090. * @param image The pixel data to decode
  21091. * @return The contents of the image
  21092. *
  21093. * @throws NotFoundException Any errors which occurred
  21094. */
  21095. /*@Override*/
  21096. // public decode(image: BinaryBitmap): Result {
  21097. // setHints(null)
  21098. // return decodeInternal(image)
  21099. // }
  21100. /**
  21101. * Decode an image using the hints provided. Does not honor existing state.
  21102. *
  21103. * @param image The pixel data to decode
  21104. * @param hints The hints to use, clearing the previous state.
  21105. * @return The contents of the image
  21106. *
  21107. * @throws NotFoundException Any errors which occurred
  21108. */
  21109. /*@Override*/
  21110. decode(image, hints) {
  21111. this.setHints(hints);
  21112. return this.decodeInternal(image);
  21113. }
  21114. /**
  21115. * Decode an image using the state set up by calling setHints() previously. Continuous scan
  21116. * clients will get a <b>large</b> speed increase by using this instead of decode().
  21117. *
  21118. * @param image The pixel data to decode
  21119. * @return The contents of the image
  21120. *
  21121. * @throws NotFoundException Any errors which occurred
  21122. */
  21123. decodeWithState(image) {
  21124. // Make sure to set up the default state so we don't crash
  21125. if (this.readers === null || this.readers === undefined) {
  21126. this.setHints(null);
  21127. }
  21128. return this.decodeInternal(image);
  21129. }
  21130. /**
  21131. * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls
  21132. * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This
  21133. * is important for performance in continuous scan clients.
  21134. *
  21135. * @param hints The set of hints to use for subsequent calls to decode(image)
  21136. */
  21137. setHints(hints) {
  21138. this.hints = hints;
  21139. const tryHarder = hints !== null && hints !== undefined && undefined !== hints.get(DecodeHintType$1.TRY_HARDER);
  21140. /*@SuppressWarnings("unchecked")*/
  21141. const formats = hints === null || hints === undefined ? null : hints.get(DecodeHintType$1.POSSIBLE_FORMATS);
  21142. const readers = new Array();
  21143. if (formats !== null && formats !== undefined) {
  21144. const addOneDReader = formats.some(f => f === BarcodeFormat$1.UPC_A ||
  21145. f === BarcodeFormat$1.UPC_E ||
  21146. f === BarcodeFormat$1.EAN_13 ||
  21147. f === BarcodeFormat$1.EAN_8 ||
  21148. f === BarcodeFormat$1.CODABAR ||
  21149. f === BarcodeFormat$1.CODE_39 ||
  21150. f === BarcodeFormat$1.CODE_93 ||
  21151. f === BarcodeFormat$1.CODE_128 ||
  21152. f === BarcodeFormat$1.ITF ||
  21153. f === BarcodeFormat$1.RSS_14 ||
  21154. f === BarcodeFormat$1.RSS_EXPANDED);
  21155. // Put 1D readers upfront in "normal" mode
  21156. // TYPESCRIPTPORT: TODO: uncomment below as they are ported
  21157. if (addOneDReader && !tryHarder) {
  21158. readers.push(new MultiFormatOneDReader(hints));
  21159. }
  21160. if (formats.includes(BarcodeFormat$1.QR_CODE)) {
  21161. readers.push(new QRCodeReader());
  21162. }
  21163. if (formats.includes(BarcodeFormat$1.DATA_MATRIX)) {
  21164. readers.push(new DataMatrixReader());
  21165. }
  21166. if (formats.includes(BarcodeFormat$1.AZTEC)) {
  21167. readers.push(new AztecReader());
  21168. }
  21169. if (formats.includes(BarcodeFormat$1.PDF_417)) {
  21170. readers.push(new PDF417Reader());
  21171. }
  21172. // if (formats.includes(BarcodeFormat.MAXICODE)) {
  21173. // readers.push(new MaxiCodeReader())
  21174. // }
  21175. // At end in "try harder" mode
  21176. if (addOneDReader && tryHarder) {
  21177. readers.push(new MultiFormatOneDReader(hints));
  21178. }
  21179. }
  21180. if (readers.length === 0) {
  21181. if (!tryHarder) {
  21182. readers.push(new MultiFormatOneDReader(hints));
  21183. }
  21184. readers.push(new QRCodeReader());
  21185. readers.push(new DataMatrixReader());
  21186. readers.push(new AztecReader());
  21187. readers.push(new PDF417Reader());
  21188. // readers.push(new MaxiCodeReader())
  21189. if (tryHarder) {
  21190. readers.push(new MultiFormatOneDReader(hints));
  21191. }
  21192. }
  21193. this.readers = readers; // .toArray(new Reader[readers.size()])
  21194. }
  21195. /*@Override*/
  21196. reset() {
  21197. if (this.readers !== null) {
  21198. for (const reader of this.readers) {
  21199. reader.reset();
  21200. }
  21201. }
  21202. }
  21203. /**
  21204. * @throws NotFoundException
  21205. */
  21206. decodeInternal(image) {
  21207. if (this.readers === null) {
  21208. throw new ReaderException('No readers where selected, nothing can be read.');
  21209. }
  21210. for (const reader of this.readers) {
  21211. // Trying to decode with ${reader} reader.
  21212. try {
  21213. return reader.decode(image, this.hints);
  21214. }
  21215. catch (ex) {
  21216. if (ex instanceof ReaderException) {
  21217. continue;
  21218. }
  21219. // Bad Exception.
  21220. }
  21221. }
  21222. throw new NotFoundException('No MultiFormat Readers were able to detect the code.');
  21223. }
  21224. }
  21225. class BrowserMultiFormatReader extends BrowserCodeReader {
  21226. constructor(hints = null, timeBetweenScansMillis = 500) {
  21227. const reader = new MultiFormatReader();
  21228. reader.setHints(hints);
  21229. super(reader, timeBetweenScansMillis);
  21230. }
  21231. /**
  21232. * Overwrite decodeBitmap to call decodeWithState, which will pay
  21233. * attention to the hints set in the constructor function
  21234. */
  21235. decodeBitmap(binaryBitmap) {
  21236. return this.reader.decodeWithState(binaryBitmap);
  21237. }
  21238. }
  21239. /**
  21240. * @deprecated Moving to @zxing/browser
  21241. *
  21242. * QR Code reader to use from browser.
  21243. */
  21244. class BrowserPDF417Reader extends BrowserCodeReader {
  21245. /**
  21246. * Creates an instance of BrowserPDF417Reader.
  21247. * @param {number} [timeBetweenScansMillis=500] the time delay between subsequent decode tries
  21248. */
  21249. constructor(timeBetweenScansMillis = 500) {
  21250. super(new PDF417Reader(), timeBetweenScansMillis);
  21251. }
  21252. }
  21253. /**
  21254. * @deprecated Moving to @zxing/browser
  21255. *
  21256. * QR Code reader to use from browser.
  21257. */
  21258. class BrowserQRCodeReader extends BrowserCodeReader {
  21259. /**
  21260. * Creates an instance of BrowserQRCodeReader.
  21261. * @param {number} [timeBetweenScansMillis=500] the time delay between subsequent decode tries
  21262. */
  21263. constructor(timeBetweenScansMillis = 500) {
  21264. super(new QRCodeReader(), timeBetweenScansMillis);
  21265. }
  21266. }
  21267. /*
  21268. * Copyright 2009 ZXing authors
  21269. *
  21270. * Licensed under the Apache License, Version 2.0 (the "License");
  21271. * you may not use this file except in compliance with the License.
  21272. * You may obtain a copy of the License at
  21273. *
  21274. * http://www.apache.org/licenses/LICENSE-2.0
  21275. *
  21276. * Unless required by applicable law or agreed to in writing, software
  21277. * distributed under the License is distributed on an "AS IS" BASIS,
  21278. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21279. * See the License for the specific language governing permissions and
  21280. * limitations under the License.
  21281. */
  21282. /*namespace com.google.zxing {*/
  21283. /**
  21284. * These are a set of hints that you may pass to Writers to specify their behavior.
  21285. *
  21286. * @author dswitkin@google.com (Daniel Switkin)
  21287. */
  21288. var EncodeHintType;
  21289. (function (EncodeHintType) {
  21290. /**
  21291. * Specifies what degree of error correction to use, for example in QR Codes.
  21292. * Type depends on the encoder. For example for QR codes it's type
  21293. * {@link com.google.zxing.qrcode.decoder.ErrorCorrectionLevel ErrorCorrectionLevel}.
  21294. * For Aztec it is of type {@link Integer}, representing the minimal percentage of error correction words.
  21295. * For PDF417 it is of type {@link Integer}, valid values being 0 to 8.
  21296. * In all cases, it can also be a {@link String} representation of the desired value as well.
  21297. * Note: an Aztec symbol should have a minimum of 25% EC words.
  21298. */
  21299. EncodeHintType[EncodeHintType["ERROR_CORRECTION"] = 0] = "ERROR_CORRECTION";
  21300. /**
  21301. * Specifies what character encoding to use where applicable (type {@link String})
  21302. */
  21303. EncodeHintType[EncodeHintType["CHARACTER_SET"] = 1] = "CHARACTER_SET";
  21304. /**
  21305. * Specifies the matrix shape for Data Matrix (type {@link com.google.zxing.datamatrix.encoder.SymbolShapeHint})
  21306. */
  21307. EncodeHintType[EncodeHintType["DATA_MATRIX_SHAPE"] = 2] = "DATA_MATRIX_SHAPE";
  21308. /**
  21309. * Specifies whether to use compact mode for Data Matrix (type {@link Boolean}, or "true" or "false"
  21310. * {@link String } value).
  21311. * The compact encoding mode also supports the encoding of characters that are not in the ISO-8859-1
  21312. * character set via ECIs.
  21313. * Please note that in that case, the most compact character encoding is chosen for characters in
  21314. * the input that are not in the ISO-8859-1 character set. Based on experience, some scanners do not
  21315. * support encodings like cp-1256 (Arabic). In such cases the encoding can be forced to UTF-8 by
  21316. * means of the {@link #CHARACTER_SET} encoding hint.
  21317. * Compact encoding also provides GS1-FNC1 support when {@link #GS1_FORMAT} is selected. In this case
  21318. * group-separator character (ASCII 29 decimal) can be used to encode the positions of FNC1 codewords
  21319. * for the purpose of delimiting AIs.
  21320. * This option and {@link #FORCE_C40} are mutually exclusive.
  21321. */
  21322. EncodeHintType[EncodeHintType["DATA_MATRIX_COMPACT"] = 3] = "DATA_MATRIX_COMPACT";
  21323. /**
  21324. * Specifies a minimum barcode size (type {@link Dimension}). Only applicable to Data Matrix now.
  21325. *
  21326. * @deprecated use width/height params in
  21327. * {@link com.google.zxing.datamatrix.DataMatrixWriter#encode(String, BarcodeFormat, int, int)}
  21328. */
  21329. /*@Deprecated*/
  21330. EncodeHintType[EncodeHintType["MIN_SIZE"] = 4] = "MIN_SIZE";
  21331. /**
  21332. * Specifies a maximum barcode size (type {@link Dimension}). Only applicable to Data Matrix now.
  21333. *
  21334. * @deprecated without replacement
  21335. */
  21336. /*@Deprecated*/
  21337. EncodeHintType[EncodeHintType["MAX_SIZE"] = 5] = "MAX_SIZE";
  21338. /**
  21339. * Specifies margin, in pixels, to use when generating the barcode. The meaning can vary
  21340. * by format; for example it controls margin before and after the barcode horizontally for
  21341. * most 1D formats. (Type {@link Integer}, or {@link String} representation of the integer value).
  21342. */
  21343. EncodeHintType[EncodeHintType["MARGIN"] = 6] = "MARGIN";
  21344. /**
  21345. * Specifies whether to use compact mode for PDF417 (type {@link Boolean}, or "true" or "false"
  21346. * {@link String} value).
  21347. */
  21348. EncodeHintType[EncodeHintType["PDF417_COMPACT"] = 7] = "PDF417_COMPACT";
  21349. /**
  21350. * Specifies what compaction mode to use for PDF417 (type
  21351. * {@link com.google.zxing.pdf417.encoder.Compaction Compaction} or {@link String} value of one of its
  21352. * enum values).
  21353. */
  21354. EncodeHintType[EncodeHintType["PDF417_COMPACTION"] = 8] = "PDF417_COMPACTION";
  21355. /**
  21356. * Specifies the minimum and maximum number of rows and columns for PDF417 (type
  21357. * {@link com.google.zxing.pdf417.encoder.Dimensions Dimensions}).
  21358. */
  21359. EncodeHintType[EncodeHintType["PDF417_DIMENSIONS"] = 9] = "PDF417_DIMENSIONS";
  21360. /**
  21361. * Specifies the required number of layers for an Aztec code.
  21362. * A negative number (-1, -2, -3, -4) specifies a compact Aztec code.
  21363. * 0 indicates to use the minimum number of layers (the default).
  21364. * A positive number (1, 2, .. 32) specifies a normal (non-compact) Aztec code.
  21365. * (Type {@link Integer}, or {@link String} representation of the integer value).
  21366. */
  21367. EncodeHintType[EncodeHintType["AZTEC_LAYERS"] = 10] = "AZTEC_LAYERS";
  21368. /**
  21369. * Specifies the exact version of QR code to be encoded.
  21370. * (Type {@link Integer}, or {@link String} representation of the integer value).
  21371. */
  21372. EncodeHintType[EncodeHintType["QR_VERSION"] = 11] = "QR_VERSION";
  21373. /**
  21374. * Specifies whether the data should be encoded to the GS1 standard (type {@link Boolean}, or "true" or "false"
  21375. * {@link String } value).
  21376. */
  21377. EncodeHintType[EncodeHintType["GS1_FORMAT"] = 12] = "GS1_FORMAT";
  21378. /**
  21379. * Forces C40 encoding for data-matrix (type {@link Boolean}, or "true" or "false") {@link String } value). This
  21380. * option and {@link #DATA_MATRIX_COMPACT} are mutually exclusive.
  21381. */
  21382. EncodeHintType[EncodeHintType["FORCE_C40"] = 13] = "FORCE_C40";
  21383. })(EncodeHintType || (EncodeHintType = {}));
  21384. var EncodeHintType$1 = EncodeHintType;
  21385. /*
  21386. * Copyright 2008 ZXing authors
  21387. *
  21388. * Licensed under the Apache License, Version 2.0 (the "License");
  21389. * you may not use this file except in compliance with the License.
  21390. * You may obtain a copy of the License at
  21391. *
  21392. * http://www.apache.org/licenses/LICENSE-2.0
  21393. *
  21394. * Unless required by applicable law or agreed to in writing, software
  21395. * distributed under the License is distributed on an "AS IS" BASIS,
  21396. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21397. * See the License for the specific language governing permissions and
  21398. * limitations under the License.
  21399. */
  21400. /**
  21401. * <p>Implements Reed-Solomon encoding, as the name implies.</p>
  21402. *
  21403. * @author Sean Owen
  21404. * @author William Rucklidge
  21405. */
  21406. class ReedSolomonEncoder {
  21407. /**
  21408. * A reed solomon error-correcting encoding constructor is created by
  21409. * passing as Galois Field with of size equal to the number of code
  21410. * words (symbols) in the alphabet (the number of values in each
  21411. * element of arrays that are encoded/decoded).
  21412. * @param field A galois field with a number of elements equal to the size
  21413. * of the alphabet of symbols to encode.
  21414. */
  21415. constructor(field) {
  21416. this.field = field;
  21417. this.cachedGenerators = [];
  21418. this.cachedGenerators.push(new GenericGFPoly(field, Int32Array.from([1])));
  21419. }
  21420. buildGenerator(degree /*int*/) {
  21421. const cachedGenerators = this.cachedGenerators;
  21422. if (degree >= cachedGenerators.length) {
  21423. let lastGenerator = cachedGenerators[cachedGenerators.length - 1];
  21424. const field = this.field;
  21425. for (let d = cachedGenerators.length; d <= degree; d++) {
  21426. const nextGenerator = lastGenerator.multiply(new GenericGFPoly(field, Int32Array.from([1, field.exp(d - 1 + field.getGeneratorBase())])));
  21427. cachedGenerators.push(nextGenerator);
  21428. lastGenerator = nextGenerator;
  21429. }
  21430. }
  21431. return cachedGenerators[degree];
  21432. }
  21433. /**
  21434. * <p>Encode a sequence of code words (symbols) using Reed-Solomon to allow decoders
  21435. * to detect and correct errors that may have been introduced when the resulting
  21436. * data is stored or transmitted.</p>
  21437. *
  21438. * @param toEncode array used for both and output. Caller initializes the array with
  21439. * the code words (symbols) to be encoded followed by empty elements allocated to make
  21440. * space for error-correction code words in the encoded output. The array contains
  21441. * the encdoded output when encode returns. Code words are encoded as numbers from
  21442. * 0 to n-1, where n is the number of possible code words (symbols), as determined
  21443. * by the size of the Galois Field passed in the constructor of this object.
  21444. * @param ecBytes the number of elements reserved in the array (first parameter)
  21445. * to store error-correction code words. Thus, the number of code words (symbols)
  21446. * to encode in the first parameter is thus toEncode.length - ecBytes.
  21447. * Note, the use of "bytes" in the name of this parameter is misleading, as there may
  21448. * be more or fewer than 256 symbols being encoded, as determined by the number of
  21449. * elements in the Galois Field passed as a constructor to this object.
  21450. * @throws IllegalArgumentException thrown in response to validation errros.
  21451. */
  21452. encode(toEncode, ecBytes /*int*/) {
  21453. if (ecBytes === 0) {
  21454. throw new IllegalArgumentException('No error correction bytes');
  21455. }
  21456. const dataBytes = toEncode.length - ecBytes;
  21457. if (dataBytes <= 0) {
  21458. throw new IllegalArgumentException('No data bytes provided');
  21459. }
  21460. const generator = this.buildGenerator(ecBytes);
  21461. const infoCoefficients = new Int32Array(dataBytes);
  21462. System.arraycopy(toEncode, 0, infoCoefficients, 0, dataBytes);
  21463. let info = new GenericGFPoly(this.field, infoCoefficients);
  21464. info = info.multiplyByMonomial(ecBytes, 1);
  21465. const remainder = info.divide(generator)[1];
  21466. const coefficients = remainder.getCoefficients();
  21467. const numZeroCoefficients = ecBytes - coefficients.length;
  21468. for (let i = 0; i < numZeroCoefficients; i++) {
  21469. toEncode[dataBytes + i] = 0;
  21470. }
  21471. System.arraycopy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.length);
  21472. }
  21473. }
  21474. /*
  21475. * Copyright 2008 ZXing authors
  21476. *
  21477. * Licensed under the Apache License, Version 2.0 (the "License");
  21478. * you may not use this file except in compliance with the License.
  21479. * You may obtain a copy of the License at
  21480. *
  21481. * http://www.apache.org/licenses/LICENSE-2.0
  21482. *
  21483. * Unless required by applicable law or agreed to in writing, software
  21484. * distributed under the License is distributed on an "AS IS" BASIS,
  21485. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21486. * See the License for the specific language governing permissions and
  21487. * limitations under the License.
  21488. */
  21489. /**
  21490. * @author Satoru Takabayashi
  21491. * @author Daniel Switkin
  21492. * @author Sean Owen
  21493. */
  21494. class MaskUtil {
  21495. constructor() {
  21496. // do nothing
  21497. }
  21498. /**
  21499. * Apply mask penalty rule 1 and return the penalty. Find repetitive cells with the same color and
  21500. * give penalty to them. Example: 00000 or 11111.
  21501. */
  21502. static applyMaskPenaltyRule1(matrix) {
  21503. return MaskUtil.applyMaskPenaltyRule1Internal(matrix, true) + MaskUtil.applyMaskPenaltyRule1Internal(matrix, false);
  21504. }
  21505. /**
  21506. * Apply mask penalty rule 2 and return the penalty. Find 2x2 blocks with the same color and give
  21507. * penalty to them. This is actually equivalent to the spec's rule, which is to find MxN blocks and give a
  21508. * penalty proportional to (M-1)x(N-1), because this is the number of 2x2 blocks inside such a block.
  21509. */
  21510. static applyMaskPenaltyRule2(matrix) {
  21511. let penalty = 0;
  21512. const array = matrix.getArray();
  21513. const width = matrix.getWidth();
  21514. const height = matrix.getHeight();
  21515. for (let y = 0; y < height - 1; y++) {
  21516. const arrayY = array[y];
  21517. for (let x = 0; x < width - 1; x++) {
  21518. const value = arrayY[x];
  21519. if (value === arrayY[x + 1] && value === array[y + 1][x] && value === array[y + 1][x + 1]) {
  21520. penalty++;
  21521. }
  21522. }
  21523. }
  21524. return MaskUtil.N2 * penalty;
  21525. }
  21526. /**
  21527. * Apply mask penalty rule 3 and return the penalty. Find consecutive runs of 1:1:3:1:1:4
  21528. * starting with black, or 4:1:1:3:1:1 starting with white, and give penalty to them. If we
  21529. * find patterns like 000010111010000, we give penalty once.
  21530. */
  21531. static applyMaskPenaltyRule3(matrix) {
  21532. let numPenalties = 0;
  21533. const array = matrix.getArray();
  21534. const width = matrix.getWidth();
  21535. const height = matrix.getHeight();
  21536. for (let y = 0; y < height; y++) {
  21537. for (let x = 0; x < width; x++) {
  21538. const arrayY = array[y]; // We can at least optimize this access
  21539. if (x + 6 < width &&
  21540. arrayY[x] === 1 &&
  21541. arrayY[x + 1] === 0 &&
  21542. arrayY[x + 2] === 1 &&
  21543. arrayY[x + 3] === 1 &&
  21544. arrayY[x + 4] === 1 &&
  21545. arrayY[x + 5] === 0 &&
  21546. arrayY[x + 6] === 1 &&
  21547. (MaskUtil.isWhiteHorizontal(arrayY, x - 4, x) || MaskUtil.isWhiteHorizontal(arrayY, x + 7, x + 11))) {
  21548. numPenalties++;
  21549. }
  21550. if (y + 6 < height &&
  21551. array[y][x] === 1 &&
  21552. array[y + 1][x] === 0 &&
  21553. array[y + 2][x] === 1 &&
  21554. array[y + 3][x] === 1 &&
  21555. array[y + 4][x] === 1 &&
  21556. array[y + 5][x] === 0 &&
  21557. array[y + 6][x] === 1 &&
  21558. (MaskUtil.isWhiteVertical(array, x, y - 4, y) || MaskUtil.isWhiteVertical(array, x, y + 7, y + 11))) {
  21559. numPenalties++;
  21560. }
  21561. }
  21562. }
  21563. return numPenalties * MaskUtil.N3;
  21564. }
  21565. static isWhiteHorizontal(rowArray, from /*int*/, to /*int*/) {
  21566. from = Math.max(from, 0);
  21567. to = Math.min(to, rowArray.length);
  21568. for (let i = from; i < to; i++) {
  21569. if (rowArray[i] === 1) {
  21570. return false;
  21571. }
  21572. }
  21573. return true;
  21574. }
  21575. static isWhiteVertical(array, col /*int*/, from /*int*/, to /*int*/) {
  21576. from = Math.max(from, 0);
  21577. to = Math.min(to, array.length);
  21578. for (let i = from; i < to; i++) {
  21579. if (array[i][col] === 1) {
  21580. return false;
  21581. }
  21582. }
  21583. return true;
  21584. }
  21585. /**
  21586. * Apply mask penalty rule 4 and return the penalty. Calculate the ratio of dark cells and give
  21587. * penalty if the ratio is far from 50%. It gives 10 penalty for 5% distance.
  21588. */
  21589. static applyMaskPenaltyRule4(matrix) {
  21590. let numDarkCells = 0;
  21591. const array = matrix.getArray();
  21592. const width = matrix.getWidth();
  21593. const height = matrix.getHeight();
  21594. for (let y = 0; y < height; y++) {
  21595. const arrayY = array[y];
  21596. for (let x = 0; x < width; x++) {
  21597. if (arrayY[x] === 1) {
  21598. numDarkCells++;
  21599. }
  21600. }
  21601. }
  21602. const numTotalCells = matrix.getHeight() * matrix.getWidth();
  21603. const fivePercentVariances = Math.floor(Math.abs(numDarkCells * 2 - numTotalCells) * 10 / numTotalCells);
  21604. return fivePercentVariances * MaskUtil.N4;
  21605. }
  21606. /**
  21607. * Return the mask bit for "getMaskPattern" at "x" and "y". See 8.8 of JISX0510:2004 for mask
  21608. * pattern conditions.
  21609. */
  21610. static getDataMaskBit(maskPattern /*int*/, x /*int*/, y /*int*/) {
  21611. let intermediate; /*int*/
  21612. let temp; /*int*/
  21613. switch (maskPattern) {
  21614. case 0:
  21615. intermediate = (y + x) & 0x1;
  21616. break;
  21617. case 1:
  21618. intermediate = y & 0x1;
  21619. break;
  21620. case 2:
  21621. intermediate = x % 3;
  21622. break;
  21623. case 3:
  21624. intermediate = (y + x) % 3;
  21625. break;
  21626. case 4:
  21627. intermediate = (Math.floor(y / 2) + Math.floor(x / 3)) & 0x1;
  21628. break;
  21629. case 5:
  21630. temp = y * x;
  21631. intermediate = (temp & 0x1) + (temp % 3);
  21632. break;
  21633. case 6:
  21634. temp = y * x;
  21635. intermediate = ((temp & 0x1) + (temp % 3)) & 0x1;
  21636. break;
  21637. case 7:
  21638. temp = y * x;
  21639. intermediate = ((temp % 3) + ((y + x) & 0x1)) & 0x1;
  21640. break;
  21641. default:
  21642. throw new IllegalArgumentException('Invalid mask pattern: ' + maskPattern);
  21643. }
  21644. return intermediate === 0;
  21645. }
  21646. /**
  21647. * Helper function for applyMaskPenaltyRule1. We need this for doing this calculation in both
  21648. * vertical and horizontal orders respectively.
  21649. */
  21650. static applyMaskPenaltyRule1Internal(matrix, isHorizontal) {
  21651. let penalty = 0;
  21652. const iLimit = isHorizontal ? matrix.getHeight() : matrix.getWidth();
  21653. const jLimit = isHorizontal ? matrix.getWidth() : matrix.getHeight();
  21654. const array = matrix.getArray();
  21655. for (let i = 0; i < iLimit; i++) {
  21656. let numSameBitCells = 0;
  21657. let prevBit = -1;
  21658. for (let j = 0; j < jLimit; j++) {
  21659. const bit = isHorizontal ? array[i][j] : array[j][i];
  21660. if (bit === prevBit) {
  21661. numSameBitCells++;
  21662. }
  21663. else {
  21664. if (numSameBitCells >= 5) {
  21665. penalty += MaskUtil.N1 + (numSameBitCells - 5);
  21666. }
  21667. numSameBitCells = 1; // Include the cell itself.
  21668. prevBit = bit;
  21669. }
  21670. }
  21671. if (numSameBitCells >= 5) {
  21672. penalty += MaskUtil.N1 + (numSameBitCells - 5);
  21673. }
  21674. }
  21675. return penalty;
  21676. }
  21677. }
  21678. // Penalty weights from section 6.8.2.1
  21679. MaskUtil.N1 = 3;
  21680. MaskUtil.N2 = 3;
  21681. MaskUtil.N3 = 40;
  21682. MaskUtil.N4 = 10;
  21683. /*
  21684. * Copyright 2008 ZXing authors
  21685. *
  21686. * Licensed under the Apache License, Version 2.0 (the "License");
  21687. * you may not use this file except in compliance with the License.
  21688. * You may obtain a copy of the License at
  21689. *
  21690. * http://www.apache.org/licenses/LICENSE-2.0
  21691. *
  21692. * Unless required by applicable law or agreed to in writing, software
  21693. * distributed under the License is distributed on an "AS IS" BASIS,
  21694. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21695. * See the License for the specific language governing permissions and
  21696. * limitations under the License.
  21697. */
  21698. /**
  21699. * JAVAPORT: The original code was a 2D array of ints, but since it only ever gets assigned
  21700. * -1, 0, and 1, I'm going to use less memory and go with bytes.
  21701. *
  21702. * @author dswitkin@google.com (Daniel Switkin)
  21703. */
  21704. class ByteMatrix {
  21705. constructor(width /*int*/, height /*int*/) {
  21706. this.width = width;
  21707. this.height = height;
  21708. const bytes = new Array(height); // [height][width]
  21709. for (let i = 0; i !== height; i++) {
  21710. bytes[i] = new Uint8Array(width);
  21711. }
  21712. this.bytes = bytes;
  21713. }
  21714. getHeight() {
  21715. return this.height;
  21716. }
  21717. getWidth() {
  21718. return this.width;
  21719. }
  21720. get(x /*int*/, y /*int*/) {
  21721. return this.bytes[y][x];
  21722. }
  21723. /**
  21724. * @return an internal representation as bytes, in row-major order. array[y][x] represents point (x,y)
  21725. */
  21726. getArray() {
  21727. return this.bytes;
  21728. }
  21729. // TYPESCRIPTPORT: preffer to let two methods instead of override to avoid type comparison inside
  21730. setNumber(x /*int*/, y /*int*/, value /*byte|int*/) {
  21731. this.bytes[y][x] = value;
  21732. }
  21733. // public set(x: number /*int*/, y: number /*int*/, value: number /*int*/): void {
  21734. // bytes[y][x] = (byte) value
  21735. // }
  21736. setBoolean(x /*int*/, y /*int*/, value) {
  21737. this.bytes[y][x] = /*(byte) */ (value ? 1 : 0);
  21738. }
  21739. clear(value /*byte*/) {
  21740. for (const aByte of this.bytes) {
  21741. Arrays.fill(aByte, value);
  21742. }
  21743. }
  21744. equals(o) {
  21745. if (!(o instanceof ByteMatrix)) {
  21746. return false;
  21747. }
  21748. const other = o;
  21749. if (this.width !== other.width) {
  21750. return false;
  21751. }
  21752. if (this.height !== other.height) {
  21753. return false;
  21754. }
  21755. for (let y = 0, height = this.height; y < height; ++y) {
  21756. const bytesY = this.bytes[y];
  21757. const otherBytesY = other.bytes[y];
  21758. for (let x = 0, width = this.width; x < width; ++x) {
  21759. if (bytesY[x] !== otherBytesY[x]) {
  21760. return false;
  21761. }
  21762. }
  21763. }
  21764. return true;
  21765. }
  21766. /*@Override*/
  21767. toString() {
  21768. const result = new StringBuilder(); // (2 * width * height + 2)
  21769. for (let y = 0, height = this.height; y < height; ++y) {
  21770. const bytesY = this.bytes[y];
  21771. for (let x = 0, width = this.width; x < width; ++x) {
  21772. switch (bytesY[x]) {
  21773. case 0:
  21774. result.append(' 0');
  21775. break;
  21776. case 1:
  21777. result.append(' 1');
  21778. break;
  21779. default:
  21780. result.append(' ');
  21781. break;
  21782. }
  21783. }
  21784. result.append('\n');
  21785. }
  21786. return result.toString();
  21787. }
  21788. }
  21789. /*
  21790. * Copyright 2008 ZXing authors
  21791. *
  21792. * Licensed under the Apache License, Version 2.0 (the "License");
  21793. * you may not use this file except in compliance with the License.
  21794. * You may obtain a copy of the License at
  21795. *
  21796. * http://www.apache.org/licenses/LICENSE-2.0
  21797. *
  21798. * Unless required by applicable law or agreed to in writing, software
  21799. * distributed under the License is distributed on an "AS IS" BASIS,
  21800. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21801. * See the License for the specific language governing permissions and
  21802. * limitations under the License.
  21803. */
  21804. /**
  21805. * @author satorux@google.com (Satoru Takabayashi) - creator
  21806. * @author dswitkin@google.com (Daniel Switkin) - ported from C++
  21807. */
  21808. class QRCode {
  21809. constructor() {
  21810. this.maskPattern = -1;
  21811. }
  21812. getMode() {
  21813. return this.mode;
  21814. }
  21815. getECLevel() {
  21816. return this.ecLevel;
  21817. }
  21818. getVersion() {
  21819. return this.version;
  21820. }
  21821. getMaskPattern() {
  21822. return this.maskPattern;
  21823. }
  21824. getMatrix() {
  21825. return this.matrix;
  21826. }
  21827. /*@Override*/
  21828. toString() {
  21829. const result = new StringBuilder(); // (200)
  21830. result.append('<<\n');
  21831. result.append(' mode: ');
  21832. result.append(this.mode ? this.mode.toString() : 'null');
  21833. result.append('\n ecLevel: ');
  21834. result.append(this.ecLevel ? this.ecLevel.toString() : 'null');
  21835. result.append('\n version: ');
  21836. result.append(this.version ? this.version.toString() : 'null');
  21837. result.append('\n maskPattern: ');
  21838. result.append(this.maskPattern.toString());
  21839. if (this.matrix) {
  21840. result.append('\n matrix:\n');
  21841. result.append(this.matrix.toString());
  21842. }
  21843. else {
  21844. result.append('\n matrix: null\n');
  21845. }
  21846. result.append('>>\n');
  21847. return result.toString();
  21848. }
  21849. setMode(value) {
  21850. this.mode = value;
  21851. }
  21852. setECLevel(value) {
  21853. this.ecLevel = value;
  21854. }
  21855. setVersion(version) {
  21856. this.version = version;
  21857. }
  21858. setMaskPattern(value /*int*/) {
  21859. this.maskPattern = value;
  21860. }
  21861. setMatrix(value) {
  21862. this.matrix = value;
  21863. }
  21864. // Check if "mask_pattern" is valid.
  21865. static isValidMaskPattern(maskPattern /*int*/) {
  21866. return maskPattern >= 0 && maskPattern < QRCode.NUM_MASK_PATTERNS;
  21867. }
  21868. }
  21869. QRCode.NUM_MASK_PATTERNS = 8;
  21870. /**
  21871. * Custom Error class of type Exception.
  21872. */
  21873. class WriterException extends Exception {
  21874. }
  21875. WriterException.kind = 'WriterException';
  21876. /*
  21877. * Copyright 2008 ZXing authors
  21878. *
  21879. * Licensed under the Apache License, Version 2.0 (the "License");
  21880. * you may not use this file except in compliance with the License.
  21881. * You may obtain a copy of the License at
  21882. *
  21883. * http://www.apache.org/licenses/LICENSE-2.0
  21884. *
  21885. * Unless required by applicable law or agreed to in writing, software
  21886. * distributed under the License is distributed on an "AS IS" BASIS,
  21887. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21888. * See the License for the specific language governing permissions and
  21889. * limitations under the License.
  21890. */
  21891. /**
  21892. * @author satorux@google.com (Satoru Takabayashi) - creator
  21893. * @author dswitkin@google.com (Daniel Switkin) - ported from C++
  21894. */
  21895. class MatrixUtil {
  21896. constructor() {
  21897. // do nothing
  21898. }
  21899. // Set all cells to -1 (TYPESCRIPTPORT: 255). -1 (TYPESCRIPTPORT: 255) means that the cell is empty (not set yet).
  21900. //
  21901. // JAVAPORT: We shouldn't need to do this at all. The code should be rewritten to begin encoding
  21902. // with the ByteMatrix initialized all to zero.
  21903. static clearMatrix(matrix) {
  21904. // TYPESCRIPTPORT: we use UintArray se changed here from -1 to 255
  21905. matrix.clear(/*(byte) */ /*-1*/ 255);
  21906. }
  21907. // Build 2D matrix of QR Code from "dataBits" with "ecLevel", "version" and "getMaskPattern". On
  21908. // success, store the result in "matrix" and return true.
  21909. static buildMatrix(dataBits, ecLevel, version, maskPattern /*int*/, matrix) {
  21910. MatrixUtil.clearMatrix(matrix);
  21911. MatrixUtil.embedBasicPatterns(version, matrix);
  21912. // Type information appear with any version.
  21913. MatrixUtil.embedTypeInfo(ecLevel, maskPattern, matrix);
  21914. // Version info appear if version >= 7.
  21915. MatrixUtil.maybeEmbedVersionInfo(version, matrix);
  21916. // Data should be embedded at end.
  21917. MatrixUtil.embedDataBits(dataBits, maskPattern, matrix);
  21918. }
  21919. // Embed basic patterns. On success, modify the matrix and return true.
  21920. // The basic patterns are:
  21921. // - Position detection patterns
  21922. // - Timing patterns
  21923. // - Dark dot at the left bottom corner
  21924. // - Position adjustment patterns, if need be
  21925. static embedBasicPatterns(version, matrix) {
  21926. // Let's get started with embedding big squares at corners.
  21927. MatrixUtil.embedPositionDetectionPatternsAndSeparators(matrix);
  21928. // Then, embed the dark dot at the left bottom corner.
  21929. MatrixUtil.embedDarkDotAtLeftBottomCorner(matrix);
  21930. // Position adjustment patterns appear if version >= 2.
  21931. MatrixUtil.maybeEmbedPositionAdjustmentPatterns(version, matrix);
  21932. // Timing patterns should be embedded after position adj. patterns.
  21933. MatrixUtil.embedTimingPatterns(matrix);
  21934. }
  21935. // Embed type information. On success, modify the matrix.
  21936. static embedTypeInfo(ecLevel, maskPattern /*int*/, matrix) {
  21937. const typeInfoBits = new BitArray();
  21938. MatrixUtil.makeTypeInfoBits(ecLevel, maskPattern, typeInfoBits);
  21939. for (let i = 0, size = typeInfoBits.getSize(); i < size; ++i) {
  21940. // Place bits in LSB to MSB order. LSB (least significant bit) is the last value in
  21941. // "typeInfoBits".
  21942. const bit = typeInfoBits.get(typeInfoBits.getSize() - 1 - i);
  21943. // Type info bits at the left top corner. See 8.9 of JISX0510:2004 (p.46).
  21944. const coordinates = MatrixUtil.TYPE_INFO_COORDINATES[i];
  21945. const x1 = coordinates[0];
  21946. const y1 = coordinates[1];
  21947. matrix.setBoolean(x1, y1, bit);
  21948. if (i < 8) {
  21949. // Right top corner.
  21950. const x2 = matrix.getWidth() - i - 1;
  21951. const y2 = 8;
  21952. matrix.setBoolean(x2, y2, bit);
  21953. }
  21954. else {
  21955. // Left bottom corner.
  21956. const x2 = 8;
  21957. const y2 = matrix.getHeight() - 7 + (i - 8);
  21958. matrix.setBoolean(x2, y2, bit);
  21959. }
  21960. }
  21961. }
  21962. // Embed version information if need be. On success, modify the matrix and return true.
  21963. // See 8.10 of JISX0510:2004 (p.47) for how to embed version information.
  21964. static maybeEmbedVersionInfo(version, matrix) {
  21965. if (version.getVersionNumber() < 7) { // Version info is necessary if version >= 7.
  21966. return; // Don't need version info.
  21967. }
  21968. const versionInfoBits = new BitArray();
  21969. MatrixUtil.makeVersionInfoBits(version, versionInfoBits);
  21970. let bitIndex = 6 * 3 - 1; // It will decrease from 17 to 0.
  21971. for (let i = 0; i < 6; ++i) {
  21972. for (let j = 0; j < 3; ++j) {
  21973. // Place bits in LSB (least significant bit) to MSB order.
  21974. const bit = versionInfoBits.get(bitIndex);
  21975. bitIndex--;
  21976. // Left bottom corner.
  21977. matrix.setBoolean(i, matrix.getHeight() - 11 + j, bit);
  21978. // Right bottom corner.
  21979. matrix.setBoolean(matrix.getHeight() - 11 + j, i, bit);
  21980. }
  21981. }
  21982. }
  21983. // Embed "dataBits" using "getMaskPattern". On success, modify the matrix and return true.
  21984. // For debugging purposes, it skips masking process if "getMaskPattern" is -1(TYPESCRIPTPORT: 255).
  21985. // See 8.7 of JISX0510:2004 (p.38) for how to embed data bits.
  21986. static embedDataBits(dataBits, maskPattern /*int*/, matrix) {
  21987. let bitIndex = 0;
  21988. let direction = -1;
  21989. // Start from the right bottom cell.
  21990. let x = matrix.getWidth() - 1;
  21991. let y = matrix.getHeight() - 1;
  21992. while (x > 0) {
  21993. // Skip the vertical timing pattern.
  21994. if (x === 6) {
  21995. x -= 1;
  21996. }
  21997. while (y >= 0 && y < matrix.getHeight()) {
  21998. for (let i = 0; i < 2; ++i) {
  21999. const xx = x - i;
  22000. // Skip the cell if it's not empty.
  22001. if (!MatrixUtil.isEmpty(matrix.get(xx, y))) {
  22002. continue;
  22003. }
  22004. let bit;
  22005. if (bitIndex < dataBits.getSize()) {
  22006. bit = dataBits.get(bitIndex);
  22007. ++bitIndex;
  22008. }
  22009. else {
  22010. // Padding bit. If there is no bit left, we'll fill the left cells with 0, as described
  22011. // in 8.4.9 of JISX0510:2004 (p. 24).
  22012. bit = false;
  22013. }
  22014. // Skip masking if mask_pattern is -1 (TYPESCRIPTPORT: 255).
  22015. if (maskPattern !== 255 && MaskUtil.getDataMaskBit(maskPattern, xx, y)) {
  22016. bit = !bit;
  22017. }
  22018. matrix.setBoolean(xx, y, bit);
  22019. }
  22020. y += direction;
  22021. }
  22022. direction = -direction; // Reverse the direction.
  22023. y += direction;
  22024. x -= 2; // Move to the left.
  22025. }
  22026. // All bits should be consumed.
  22027. if (bitIndex !== dataBits.getSize()) {
  22028. throw new WriterException('Not all bits consumed: ' + bitIndex + '/' + dataBits.getSize());
  22029. }
  22030. }
  22031. // Return the position of the most significant bit set (one: to) in the "value". The most
  22032. // significant bit is position 32. If there is no bit set, return 0. Examples:
  22033. // - findMSBSet(0) => 0
  22034. // - findMSBSet(1) => 1
  22035. // - findMSBSet(255) => 8
  22036. static findMSBSet(value /*int*/) {
  22037. return 32 - Integer.numberOfLeadingZeros(value);
  22038. }
  22039. // Calculate BCH (Bose-Chaudhuri-Hocquenghem) code for "value" using polynomial "poly". The BCH
  22040. // code is used for encoding type information and version information.
  22041. // Example: Calculation of version information of 7.
  22042. // f(x) is created from 7.
  22043. // - 7 = 000111 in 6 bits
  22044. // - f(x) = x^2 + x^1 + x^0
  22045. // g(x) is given by the standard (p. 67)
  22046. // - g(x) = x^12 + x^11 + x^10 + x^9 + x^8 + x^5 + x^2 + 1
  22047. // Multiply f(x) by x^(18 - 6)
  22048. // - f'(x) = f(x) * x^(18 - 6)
  22049. // - f'(x) = x^14 + x^13 + x^12
  22050. // Calculate the remainder of f'(x) / g(x)
  22051. // x^2
  22052. // __________________________________________________
  22053. // g(x) )x^14 + x^13 + x^12
  22054. // x^14 + x^13 + x^12 + x^11 + x^10 + x^7 + x^4 + x^2
  22055. // --------------------------------------------------
  22056. // x^11 + x^10 + x^7 + x^4 + x^2
  22057. //
  22058. // The remainder is x^11 + x^10 + x^7 + x^4 + x^2
  22059. // Encode it in binary: 110010010100
  22060. // The return value is 0xc94 (1100 1001 0100)
  22061. //
  22062. // Since all coefficients in the polynomials are 1 or 0, we can do the calculation by bit
  22063. // operations. We don't care if coefficients are positive or negative.
  22064. static calculateBCHCode(value /*int*/, poly /*int*/) {
  22065. if (poly === 0) {
  22066. throw new IllegalArgumentException('0 polynomial');
  22067. }
  22068. // If poly is "1 1111 0010 0101" (version info poly), msbSetInPoly is 13. We'll subtract 1
  22069. // from 13 to make it 12.
  22070. const msbSetInPoly = MatrixUtil.findMSBSet(poly);
  22071. value <<= msbSetInPoly - 1;
  22072. // Do the division business using exclusive-or operations.
  22073. while (MatrixUtil.findMSBSet(value) >= msbSetInPoly) {
  22074. value ^= poly << (MatrixUtil.findMSBSet(value) - msbSetInPoly);
  22075. }
  22076. // Now the "value" is the remainder (i.e. the BCH code)
  22077. return value;
  22078. }
  22079. // Make bit vector of type information. On success, store the result in "bits" and return true.
  22080. // Encode error correction level and mask pattern. See 8.9 of
  22081. // JISX0510:2004 (p.45) for details.
  22082. static makeTypeInfoBits(ecLevel, maskPattern /*int*/, bits) {
  22083. if (!QRCode.isValidMaskPattern(maskPattern)) {
  22084. throw new WriterException('Invalid mask pattern');
  22085. }
  22086. const typeInfo = (ecLevel.getBits() << 3) | maskPattern;
  22087. bits.appendBits(typeInfo, 5);
  22088. const bchCode = MatrixUtil.calculateBCHCode(typeInfo, MatrixUtil.TYPE_INFO_POLY);
  22089. bits.appendBits(bchCode, 10);
  22090. const maskBits = new BitArray();
  22091. maskBits.appendBits(MatrixUtil.TYPE_INFO_MASK_PATTERN, 15);
  22092. bits.xor(maskBits);
  22093. if (bits.getSize() !== 15) { // Just in case.
  22094. throw new WriterException('should not happen but we got: ' + bits.getSize());
  22095. }
  22096. }
  22097. // Make bit vector of version information. On success, store the result in "bits" and return true.
  22098. // See 8.10 of JISX0510:2004 (p.45) for details.
  22099. static makeVersionInfoBits(version, bits) {
  22100. bits.appendBits(version.getVersionNumber(), 6);
  22101. const bchCode = MatrixUtil.calculateBCHCode(version.getVersionNumber(), MatrixUtil.VERSION_INFO_POLY);
  22102. bits.appendBits(bchCode, 12);
  22103. if (bits.getSize() !== 18) { // Just in case.
  22104. throw new WriterException('should not happen but we got: ' + bits.getSize());
  22105. }
  22106. }
  22107. // Check if "value" is empty.
  22108. static isEmpty(value /*int*/) {
  22109. return value === 255; // -1
  22110. }
  22111. static embedTimingPatterns(matrix) {
  22112. // -8 is for skipping position detection patterns (7: size), and two horizontal/vertical
  22113. // separation patterns (1: size). Thus, 8 = 7 + 1.
  22114. for (let i = 8; i < matrix.getWidth() - 8; ++i) {
  22115. const bit = (i + 1) % 2;
  22116. // Horizontal line.
  22117. if (MatrixUtil.isEmpty(matrix.get(i, 6))) {
  22118. matrix.setNumber(i, 6, bit);
  22119. }
  22120. // Vertical line.
  22121. if (MatrixUtil.isEmpty(matrix.get(6, i))) {
  22122. matrix.setNumber(6, i, bit);
  22123. }
  22124. }
  22125. }
  22126. // Embed the lonely dark dot at left bottom corner. JISX0510:2004 (p.46)
  22127. static embedDarkDotAtLeftBottomCorner(matrix) {
  22128. if (matrix.get(8, matrix.getHeight() - 8) === 0) {
  22129. throw new WriterException();
  22130. }
  22131. matrix.setNumber(8, matrix.getHeight() - 8, 1);
  22132. }
  22133. static embedHorizontalSeparationPattern(xStart /*int*/, yStart /*int*/, matrix) {
  22134. for (let x = 0; x < 8; ++x) {
  22135. if (!MatrixUtil.isEmpty(matrix.get(xStart + x, yStart))) {
  22136. throw new WriterException();
  22137. }
  22138. matrix.setNumber(xStart + x, yStart, 0);
  22139. }
  22140. }
  22141. static embedVerticalSeparationPattern(xStart /*int*/, yStart /*int*/, matrix) {
  22142. for (let y = 0; y < 7; ++y) {
  22143. if (!MatrixUtil.isEmpty(matrix.get(xStart, yStart + y))) {
  22144. throw new WriterException();
  22145. }
  22146. matrix.setNumber(xStart, yStart + y, 0);
  22147. }
  22148. }
  22149. static embedPositionAdjustmentPattern(xStart /*int*/, yStart /*int*/, matrix) {
  22150. for (let y = 0; y < 5; ++y) {
  22151. const patternY = MatrixUtil.POSITION_ADJUSTMENT_PATTERN[y];
  22152. for (let x = 0; x < 5; ++x) {
  22153. matrix.setNumber(xStart + x, yStart + y, patternY[x]);
  22154. }
  22155. }
  22156. }
  22157. static embedPositionDetectionPattern(xStart /*int*/, yStart /*int*/, matrix) {
  22158. for (let y = 0; y < 7; ++y) {
  22159. const patternY = MatrixUtil.POSITION_DETECTION_PATTERN[y];
  22160. for (let x = 0; x < 7; ++x) {
  22161. matrix.setNumber(xStart + x, yStart + y, patternY[x]);
  22162. }
  22163. }
  22164. }
  22165. // Embed position detection patterns and surrounding vertical/horizontal separators.
  22166. static embedPositionDetectionPatternsAndSeparators(matrix) {
  22167. // Embed three big squares at corners.
  22168. const pdpWidth = MatrixUtil.POSITION_DETECTION_PATTERN[0].length;
  22169. // Left top corner.
  22170. MatrixUtil.embedPositionDetectionPattern(0, 0, matrix);
  22171. // Right top corner.
  22172. MatrixUtil.embedPositionDetectionPattern(matrix.getWidth() - pdpWidth, 0, matrix);
  22173. // Left bottom corner.
  22174. MatrixUtil.embedPositionDetectionPattern(0, matrix.getWidth() - pdpWidth, matrix);
  22175. // Embed horizontal separation patterns around the squares.
  22176. const hspWidth = 8;
  22177. // Left top corner.
  22178. MatrixUtil.embedHorizontalSeparationPattern(0, hspWidth - 1, matrix);
  22179. // Right top corner.
  22180. MatrixUtil.embedHorizontalSeparationPattern(matrix.getWidth() - hspWidth, hspWidth - 1, matrix);
  22181. // Left bottom corner.
  22182. MatrixUtil.embedHorizontalSeparationPattern(0, matrix.getWidth() - hspWidth, matrix);
  22183. // Embed vertical separation patterns around the squares.
  22184. const vspSize = 7;
  22185. // Left top corner.
  22186. MatrixUtil.embedVerticalSeparationPattern(vspSize, 0, matrix);
  22187. // Right top corner.
  22188. MatrixUtil.embedVerticalSeparationPattern(matrix.getHeight() - vspSize - 1, 0, matrix);
  22189. // Left bottom corner.
  22190. MatrixUtil.embedVerticalSeparationPattern(vspSize, matrix.getHeight() - vspSize, matrix);
  22191. }
  22192. // Embed position adjustment patterns if need be.
  22193. static maybeEmbedPositionAdjustmentPatterns(version, matrix) {
  22194. if (version.getVersionNumber() < 2) { // The patterns appear if version >= 2
  22195. return;
  22196. }
  22197. const index = version.getVersionNumber() - 1;
  22198. const coordinates = MatrixUtil.POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index];
  22199. for (let i = 0, length = coordinates.length; i !== length; i++) {
  22200. const y = coordinates[i];
  22201. if (y >= 0) {
  22202. for (let j = 0; j !== length; j++) {
  22203. const x = coordinates[j];
  22204. if (x >= 0 && MatrixUtil.isEmpty(matrix.get(x, y))) {
  22205. // If the cell is unset, we embed the position adjustment pattern here.
  22206. // -2 is necessary since the x/y coordinates point to the center of the pattern, not the
  22207. // left top corner.
  22208. MatrixUtil.embedPositionAdjustmentPattern(x - 2, y - 2, matrix);
  22209. }
  22210. }
  22211. }
  22212. }
  22213. }
  22214. }
  22215. MatrixUtil.POSITION_DETECTION_PATTERN = Array.from([
  22216. Int32Array.from([1, 1, 1, 1, 1, 1, 1]),
  22217. Int32Array.from([1, 0, 0, 0, 0, 0, 1]),
  22218. Int32Array.from([1, 0, 1, 1, 1, 0, 1]),
  22219. Int32Array.from([1, 0, 1, 1, 1, 0, 1]),
  22220. Int32Array.from([1, 0, 1, 1, 1, 0, 1]),
  22221. Int32Array.from([1, 0, 0, 0, 0, 0, 1]),
  22222. Int32Array.from([1, 1, 1, 1, 1, 1, 1]),
  22223. ]);
  22224. MatrixUtil.POSITION_ADJUSTMENT_PATTERN = Array.from([
  22225. Int32Array.from([1, 1, 1, 1, 1]),
  22226. Int32Array.from([1, 0, 0, 0, 1]),
  22227. Int32Array.from([1, 0, 1, 0, 1]),
  22228. Int32Array.from([1, 0, 0, 0, 1]),
  22229. Int32Array.from([1, 1, 1, 1, 1]),
  22230. ]);
  22231. // From Appendix E. Table 1, JIS0510X:2004 (71: p). The table was double-checked by komatsu.
  22232. MatrixUtil.POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE = Array.from([
  22233. Int32Array.from([-1, -1, -1, -1, -1, -1, -1]),
  22234. Int32Array.from([6, 18, -1, -1, -1, -1, -1]),
  22235. Int32Array.from([6, 22, -1, -1, -1, -1, -1]),
  22236. Int32Array.from([6, 26, -1, -1, -1, -1, -1]),
  22237. Int32Array.from([6, 30, -1, -1, -1, -1, -1]),
  22238. Int32Array.from([6, 34, -1, -1, -1, -1, -1]),
  22239. Int32Array.from([6, 22, 38, -1, -1, -1, -1]),
  22240. Int32Array.from([6, 24, 42, -1, -1, -1, -1]),
  22241. Int32Array.from([6, 26, 46, -1, -1, -1, -1]),
  22242. Int32Array.from([6, 28, 50, -1, -1, -1, -1]),
  22243. Int32Array.from([6, 30, 54, -1, -1, -1, -1]),
  22244. Int32Array.from([6, 32, 58, -1, -1, -1, -1]),
  22245. Int32Array.from([6, 34, 62, -1, -1, -1, -1]),
  22246. Int32Array.from([6, 26, 46, 66, -1, -1, -1]),
  22247. Int32Array.from([6, 26, 48, 70, -1, -1, -1]),
  22248. Int32Array.from([6, 26, 50, 74, -1, -1, -1]),
  22249. Int32Array.from([6, 30, 54, 78, -1, -1, -1]),
  22250. Int32Array.from([6, 30, 56, 82, -1, -1, -1]),
  22251. Int32Array.from([6, 30, 58, 86, -1, -1, -1]),
  22252. Int32Array.from([6, 34, 62, 90, -1, -1, -1]),
  22253. Int32Array.from([6, 28, 50, 72, 94, -1, -1]),
  22254. Int32Array.from([6, 26, 50, 74, 98, -1, -1]),
  22255. Int32Array.from([6, 30, 54, 78, 102, -1, -1]),
  22256. Int32Array.from([6, 28, 54, 80, 106, -1, -1]),
  22257. Int32Array.from([6, 32, 58, 84, 110, -1, -1]),
  22258. Int32Array.from([6, 30, 58, 86, 114, -1, -1]),
  22259. Int32Array.from([6, 34, 62, 90, 118, -1, -1]),
  22260. Int32Array.from([6, 26, 50, 74, 98, 122, -1]),
  22261. Int32Array.from([6, 30, 54, 78, 102, 126, -1]),
  22262. Int32Array.from([6, 26, 52, 78, 104, 130, -1]),
  22263. Int32Array.from([6, 30, 56, 82, 108, 134, -1]),
  22264. Int32Array.from([6, 34, 60, 86, 112, 138, -1]),
  22265. Int32Array.from([6, 30, 58, 86, 114, 142, -1]),
  22266. Int32Array.from([6, 34, 62, 90, 118, 146, -1]),
  22267. Int32Array.from([6, 30, 54, 78, 102, 126, 150]),
  22268. Int32Array.from([6, 24, 50, 76, 102, 128, 154]),
  22269. Int32Array.from([6, 28, 54, 80, 106, 132, 158]),
  22270. Int32Array.from([6, 32, 58, 84, 110, 136, 162]),
  22271. Int32Array.from([6, 26, 54, 82, 110, 138, 166]),
  22272. Int32Array.from([6, 30, 58, 86, 114, 142, 170]),
  22273. ]);
  22274. // Type info cells at the left top corner.
  22275. MatrixUtil.TYPE_INFO_COORDINATES = Array.from([
  22276. Int32Array.from([8, 0]),
  22277. Int32Array.from([8, 1]),
  22278. Int32Array.from([8, 2]),
  22279. Int32Array.from([8, 3]),
  22280. Int32Array.from([8, 4]),
  22281. Int32Array.from([8, 5]),
  22282. Int32Array.from([8, 7]),
  22283. Int32Array.from([8, 8]),
  22284. Int32Array.from([7, 8]),
  22285. Int32Array.from([5, 8]),
  22286. Int32Array.from([4, 8]),
  22287. Int32Array.from([3, 8]),
  22288. Int32Array.from([2, 8]),
  22289. Int32Array.from([1, 8]),
  22290. Int32Array.from([0, 8]),
  22291. ]);
  22292. // From Appendix D in JISX0510:2004 (p. 67)
  22293. MatrixUtil.VERSION_INFO_POLY = 0x1f25; // 1 1111 0010 0101
  22294. // From Appendix C in JISX0510:2004 (p.65).
  22295. MatrixUtil.TYPE_INFO_POLY = 0x537;
  22296. MatrixUtil.TYPE_INFO_MASK_PATTERN = 0x5412;
  22297. /*
  22298. * Copyright 2008 ZXing authors
  22299. *
  22300. * Licensed under the Apache License, Version 2.0 (the "License");
  22301. * you may not use this file except in compliance with the License.
  22302. * You may obtain a copy of the License at
  22303. *
  22304. * http://www.apache.org/licenses/LICENSE-2.0
  22305. *
  22306. * Unless required by applicable law or agreed to in writing, software
  22307. * distributed under the License is distributed on an "AS IS" BASIS,
  22308. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22309. * See the License for the specific language governing permissions and
  22310. * limitations under the License.
  22311. */
  22312. /*namespace com.google.zxing.qrcode.encoder {*/
  22313. class BlockPair {
  22314. constructor(dataBytes, errorCorrectionBytes) {
  22315. this.dataBytes = dataBytes;
  22316. this.errorCorrectionBytes = errorCorrectionBytes;
  22317. }
  22318. getDataBytes() {
  22319. return this.dataBytes;
  22320. }
  22321. getErrorCorrectionBytes() {
  22322. return this.errorCorrectionBytes;
  22323. }
  22324. }
  22325. /*
  22326. * Copyright 2008 ZXing authors
  22327. *
  22328. * Licensed under the Apache License, Version 2.0 (the "License");
  22329. * you may not use this file except in compliance with the License.
  22330. * You may obtain a copy of the License at
  22331. *
  22332. * http://www.apache.org/licenses/LICENSE-2.0
  22333. *
  22334. * Unless required by applicable law or agreed to in writing, software
  22335. * distributed under the License is distributed on an "AS IS" BASIS,
  22336. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22337. * See the License for the specific language governing permissions and
  22338. * limitations under the License.
  22339. */
  22340. /*import java.io.UnsupportedEncodingException;*/
  22341. /*import java.util.ArrayList;*/
  22342. /*import java.util.Collection;*/
  22343. /*import java.util.Map;*/
  22344. /**
  22345. * @author satorux@google.com (Satoru Takabayashi) - creator
  22346. * @author dswitkin@google.com (Daniel Switkin) - ported from C++
  22347. */
  22348. class Encoder$1 {
  22349. // TYPESCRIPTPORT: changed to UTF8, the default for js
  22350. constructor() { }
  22351. // The mask penalty calculation is complicated. See Table 21 of JISX0510:2004 (p.45) for details.
  22352. // Basically it applies four rules and summate all penalties.
  22353. static calculateMaskPenalty(matrix) {
  22354. return MaskUtil.applyMaskPenaltyRule1(matrix)
  22355. + MaskUtil.applyMaskPenaltyRule2(matrix)
  22356. + MaskUtil.applyMaskPenaltyRule3(matrix)
  22357. + MaskUtil.applyMaskPenaltyRule4(matrix);
  22358. }
  22359. /**
  22360. * @param content text to encode
  22361. * @param ecLevel error correction level to use
  22362. * @return {@link QRCode} representing the encoded QR code
  22363. * @throws WriterException if encoding can't succeed, because of for example invalid content
  22364. * or configuration
  22365. */
  22366. // public static encode(content: string, ecLevel: ErrorCorrectionLevel): QRCode /*throws WriterException*/ {
  22367. // return encode(content, ecLevel, null)
  22368. // }
  22369. static encode(content, ecLevel, hints = null) {
  22370. // Determine what character encoding has been specified by the caller, if any
  22371. let encoding = Encoder$1.DEFAULT_BYTE_MODE_ENCODING;
  22372. const hasEncodingHint = hints !== null && undefined !== hints.get(EncodeHintType$1.CHARACTER_SET);
  22373. if (hasEncodingHint) {
  22374. encoding = hints.get(EncodeHintType$1.CHARACTER_SET).toString();
  22375. }
  22376. // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
  22377. // multiple modes / segments even if that were more efficient. Twould be nice.
  22378. const mode = this.chooseMode(content, encoding);
  22379. // This will store the header information, like mode and
  22380. // length, as well as "header" segments like an ECI segment.
  22381. const headerBits = new BitArray();
  22382. // Append ECI segment if applicable
  22383. if (mode === Mode$2.BYTE && (hasEncodingHint || Encoder$1.DEFAULT_BYTE_MODE_ENCODING !== encoding)) {
  22384. const eci = CharacterSetECI.getCharacterSetECIByName(encoding);
  22385. if (eci !== undefined) {
  22386. this.appendECI(eci, headerBits);
  22387. }
  22388. }
  22389. // (With ECI in place,) Write the mode marker
  22390. this.appendModeInfo(mode, headerBits);
  22391. // Collect data within the main segment, separately, to count its size if needed. Don't add it to
  22392. // main payload yet.
  22393. const dataBits = new BitArray();
  22394. this.appendBytes(content, mode, dataBits, encoding);
  22395. let version;
  22396. if (hints !== null && undefined !== hints.get(EncodeHintType$1.QR_VERSION)) {
  22397. const versionNumber = Number.parseInt(hints.get(EncodeHintType$1.QR_VERSION).toString(), 10);
  22398. version = Version.getVersionForNumber(versionNumber);
  22399. const bitsNeeded = this.calculateBitsNeeded(mode, headerBits, dataBits, version);
  22400. if (!this.willFit(bitsNeeded, version, ecLevel)) {
  22401. throw new WriterException('Data too big for requested version');
  22402. }
  22403. }
  22404. else {
  22405. version = this.recommendVersion(ecLevel, mode, headerBits, dataBits);
  22406. }
  22407. const headerAndDataBits = new BitArray();
  22408. headerAndDataBits.appendBitArray(headerBits);
  22409. // Find "length" of main segment and write it
  22410. const numLetters = mode === Mode$2.BYTE ? dataBits.getSizeInBytes() : content.length;
  22411. this.appendLengthInfo(numLetters, version, mode, headerAndDataBits);
  22412. // Put data together into the overall payload
  22413. headerAndDataBits.appendBitArray(dataBits);
  22414. const ecBlocks = version.getECBlocksForLevel(ecLevel);
  22415. const numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();
  22416. // Terminate the bits properly.
  22417. this.terminateBits(numDataBytes, headerAndDataBits);
  22418. // Interleave data bits with error correction code.
  22419. const finalBits = this.interleaveWithECBytes(headerAndDataBits, version.getTotalCodewords(), numDataBytes, ecBlocks.getNumBlocks());
  22420. const qrCode = new QRCode();
  22421. qrCode.setECLevel(ecLevel);
  22422. qrCode.setMode(mode);
  22423. qrCode.setVersion(version);
  22424. // Choose the mask pattern and set to "qrCode".
  22425. const dimension = version.getDimensionForVersion();
  22426. const matrix = new ByteMatrix(dimension, dimension);
  22427. const maskPattern = this.chooseMaskPattern(finalBits, ecLevel, version, matrix);
  22428. qrCode.setMaskPattern(maskPattern);
  22429. // Build the matrix and set it to "qrCode".
  22430. MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
  22431. qrCode.setMatrix(matrix);
  22432. return qrCode;
  22433. }
  22434. /**
  22435. * Decides the smallest version of QR code that will contain all of the provided data.
  22436. *
  22437. * @throws WriterException if the data cannot fit in any version
  22438. */
  22439. static recommendVersion(ecLevel, mode, headerBits, dataBits) {
  22440. // Hard part: need to know version to know how many bits length takes. But need to know how many
  22441. // bits it takes to know version. First we take a guess at version by assuming version will be
  22442. // the minimum, 1:
  22443. const provisionalBitsNeeded = this.calculateBitsNeeded(mode, headerBits, dataBits, Version.getVersionForNumber(1));
  22444. const provisionalVersion = this.chooseVersion(provisionalBitsNeeded, ecLevel);
  22445. // Use that guess to calculate the right version. I am still not sure this works in 100% of cases.
  22446. const bitsNeeded = this.calculateBitsNeeded(mode, headerBits, dataBits, provisionalVersion);
  22447. return this.chooseVersion(bitsNeeded, ecLevel);
  22448. }
  22449. static calculateBitsNeeded(mode, headerBits, dataBits, version) {
  22450. return headerBits.getSize() + mode.getCharacterCountBits(version) + dataBits.getSize();
  22451. }
  22452. /**
  22453. * @return the code point of the table used in alphanumeric mode or
  22454. * -1 if there is no corresponding code in the table.
  22455. */
  22456. static getAlphanumericCode(code /*int*/) {
  22457. if (code < Encoder$1.ALPHANUMERIC_TABLE.length) {
  22458. return Encoder$1.ALPHANUMERIC_TABLE[code];
  22459. }
  22460. return -1;
  22461. }
  22462. // public static chooseMode(content: string): Mode {
  22463. // return chooseMode(content, null);
  22464. // }
  22465. /**
  22466. * Choose the best mode by examining the content. Note that 'encoding' is used as a hint;
  22467. * if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}.
  22468. */
  22469. static chooseMode(content, encoding = null) {
  22470. if (CharacterSetECI.SJIS.getName() === encoding && this.isOnlyDoubleByteKanji(content)) {
  22471. // Choose Kanji mode if all input are double-byte characters
  22472. return Mode$2.KANJI;
  22473. }
  22474. let hasNumeric = false;
  22475. let hasAlphanumeric = false;
  22476. for (let i = 0, length = content.length; i < length; ++i) {
  22477. const c = content.charAt(i);
  22478. if (Encoder$1.isDigit(c)) {
  22479. hasNumeric = true;
  22480. }
  22481. else if (this.getAlphanumericCode(c.charCodeAt(0)) !== -1) {
  22482. hasAlphanumeric = true;
  22483. }
  22484. else {
  22485. return Mode$2.BYTE;
  22486. }
  22487. }
  22488. if (hasAlphanumeric) {
  22489. return Mode$2.ALPHANUMERIC;
  22490. }
  22491. if (hasNumeric) {
  22492. return Mode$2.NUMERIC;
  22493. }
  22494. return Mode$2.BYTE;
  22495. }
  22496. static isOnlyDoubleByteKanji(content) {
  22497. let bytes;
  22498. try {
  22499. bytes = StringEncoding.encode(content, CharacterSetECI.SJIS); // content.getBytes("Shift_JIS"))
  22500. }
  22501. catch (ignored /*: UnsupportedEncodingException*/) {
  22502. return false;
  22503. }
  22504. const length = bytes.length;
  22505. if (length % 2 !== 0) {
  22506. return false;
  22507. }
  22508. for (let i = 0; i < length; i += 2) {
  22509. const byte1 = bytes[i] & 0xFF;
  22510. if ((byte1 < 0x81 || byte1 > 0x9F) && (byte1 < 0xE0 || byte1 > 0xEB)) {
  22511. return false;
  22512. }
  22513. }
  22514. return true;
  22515. }
  22516. static chooseMaskPattern(bits, ecLevel, version, matrix) {
  22517. let minPenalty = Number.MAX_SAFE_INTEGER; // Lower penalty is better.
  22518. let bestMaskPattern = -1;
  22519. // We try all mask patterns to choose the best one.
  22520. for (let maskPattern = 0; maskPattern < QRCode.NUM_MASK_PATTERNS; maskPattern++) {
  22521. MatrixUtil.buildMatrix(bits, ecLevel, version, maskPattern, matrix);
  22522. let penalty = this.calculateMaskPenalty(matrix);
  22523. if (penalty < minPenalty) {
  22524. minPenalty = penalty;
  22525. bestMaskPattern = maskPattern;
  22526. }
  22527. }
  22528. return bestMaskPattern;
  22529. }
  22530. static chooseVersion(numInputBits /*int*/, ecLevel) {
  22531. for (let versionNum = 1; versionNum <= 40; versionNum++) {
  22532. const version = Version.getVersionForNumber(versionNum);
  22533. if (Encoder$1.willFit(numInputBits, version, ecLevel)) {
  22534. return version;
  22535. }
  22536. }
  22537. throw new WriterException('Data too big');
  22538. }
  22539. /**
  22540. * @return true if the number of input bits will fit in a code with the specified version and
  22541. * error correction level.
  22542. */
  22543. static willFit(numInputBits /*int*/, version, ecLevel) {
  22544. // In the following comments, we use numbers of Version 7-H.
  22545. // numBytes = 196
  22546. const numBytes = version.getTotalCodewords();
  22547. // getNumECBytes = 130
  22548. const ecBlocks = version.getECBlocksForLevel(ecLevel);
  22549. const numEcBytes = ecBlocks.getTotalECCodewords();
  22550. // getNumDataBytes = 196 - 130 = 66
  22551. const numDataBytes = numBytes - numEcBytes;
  22552. const totalInputBytes = (numInputBits + 7) / 8;
  22553. return numDataBytes >= totalInputBytes;
  22554. }
  22555. /**
  22556. * Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).
  22557. */
  22558. static terminateBits(numDataBytes /*int*/, bits) {
  22559. const capacity = numDataBytes * 8;
  22560. if (bits.getSize() > capacity) {
  22561. throw new WriterException('data bits cannot fit in the QR Code' + bits.getSize() + ' > ' +
  22562. capacity);
  22563. }
  22564. for (let i = 0; i < 4 && bits.getSize() < capacity; ++i) {
  22565. bits.appendBit(false);
  22566. }
  22567. // Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details.
  22568. // If the last byte isn't 8-bit aligned, we'll add padding bits.
  22569. const numBitsInLastByte = bits.getSize() & 0x07;
  22570. if (numBitsInLastByte > 0) {
  22571. for (let i = numBitsInLastByte; i < 8; i++) {
  22572. bits.appendBit(false);
  22573. }
  22574. }
  22575. // If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24).
  22576. const numPaddingBytes = numDataBytes - bits.getSizeInBytes();
  22577. for (let i = 0; i < numPaddingBytes; ++i) {
  22578. bits.appendBits((i & 0x01) === 0 ? 0xEC : 0x11, 8);
  22579. }
  22580. if (bits.getSize() !== capacity) {
  22581. throw new WriterException('Bits size does not equal capacity');
  22582. }
  22583. }
  22584. /**
  22585. * Get number of data bytes and number of error correction bytes for block id "blockID". Store
  22586. * the result in "numDataBytesInBlock", and "numECBytesInBlock". See table 12 in 8.5.1 of
  22587. * JISX0510:2004 (p.30)
  22588. */
  22589. static getNumDataBytesAndNumECBytesForBlockID(numTotalBytes /*int*/, numDataBytes /*int*/, numRSBlocks /*int*/, blockID /*int*/, numDataBytesInBlock, numECBytesInBlock) {
  22590. if (blockID >= numRSBlocks) {
  22591. throw new WriterException('Block ID too large');
  22592. }
  22593. // numRsBlocksInGroup2 = 196 % 5 = 1
  22594. const numRsBlocksInGroup2 = numTotalBytes % numRSBlocks;
  22595. // numRsBlocksInGroup1 = 5 - 1 = 4
  22596. const numRsBlocksInGroup1 = numRSBlocks - numRsBlocksInGroup2;
  22597. // numTotalBytesInGroup1 = 196 / 5 = 39
  22598. const numTotalBytesInGroup1 = Math.floor(numTotalBytes / numRSBlocks);
  22599. // numTotalBytesInGroup2 = 39 + 1 = 40
  22600. const numTotalBytesInGroup2 = numTotalBytesInGroup1 + 1;
  22601. // numDataBytesInGroup1 = 66 / 5 = 13
  22602. const numDataBytesInGroup1 = Math.floor(numDataBytes / numRSBlocks);
  22603. // numDataBytesInGroup2 = 13 + 1 = 14
  22604. const numDataBytesInGroup2 = numDataBytesInGroup1 + 1;
  22605. // numEcBytesInGroup1 = 39 - 13 = 26
  22606. const numEcBytesInGroup1 = numTotalBytesInGroup1 - numDataBytesInGroup1;
  22607. // numEcBytesInGroup2 = 40 - 14 = 26
  22608. const numEcBytesInGroup2 = numTotalBytesInGroup2 - numDataBytesInGroup2;
  22609. // Sanity checks.
  22610. // 26 = 26
  22611. if (numEcBytesInGroup1 !== numEcBytesInGroup2) {
  22612. throw new WriterException('EC bytes mismatch');
  22613. }
  22614. // 5 = 4 + 1.
  22615. if (numRSBlocks !== numRsBlocksInGroup1 + numRsBlocksInGroup2) {
  22616. throw new WriterException('RS blocks mismatch');
  22617. }
  22618. // 196 = (13 + 26) * 4 + (14 + 26) * 1
  22619. if (numTotalBytes !==
  22620. ((numDataBytesInGroup1 + numEcBytesInGroup1) *
  22621. numRsBlocksInGroup1) +
  22622. ((numDataBytesInGroup2 + numEcBytesInGroup2) *
  22623. numRsBlocksInGroup2)) {
  22624. throw new WriterException('Total bytes mismatch');
  22625. }
  22626. if (blockID < numRsBlocksInGroup1) {
  22627. numDataBytesInBlock[0] = numDataBytesInGroup1;
  22628. numECBytesInBlock[0] = numEcBytesInGroup1;
  22629. }
  22630. else {
  22631. numDataBytesInBlock[0] = numDataBytesInGroup2;
  22632. numECBytesInBlock[0] = numEcBytesInGroup2;
  22633. }
  22634. }
  22635. /**
  22636. * Interleave "bits" with corresponding error correction bytes. On success, store the result in
  22637. * "result". The interleave rule is complicated. See 8.6 of JISX0510:2004 (p.37) for details.
  22638. */
  22639. static interleaveWithECBytes(bits, numTotalBytes /*int*/, numDataBytes /*int*/, numRSBlocks /*int*/) {
  22640. // "bits" must have "getNumDataBytes" bytes of data.
  22641. if (bits.getSizeInBytes() !== numDataBytes) {
  22642. throw new WriterException('Number of bits and data bytes does not match');
  22643. }
  22644. // Step 1. Divide data bytes into blocks and generate error correction bytes for them. We'll
  22645. // store the divided data bytes blocks and error correction bytes blocks into "blocks".
  22646. let dataBytesOffset = 0;
  22647. let maxNumDataBytes = 0;
  22648. let maxNumEcBytes = 0;
  22649. // Since, we know the number of reedsolmon blocks, we can initialize the vector with the number.
  22650. const blocks = new Array(); // new Array<BlockPair>(numRSBlocks)
  22651. for (let i = 0; i < numRSBlocks; ++i) {
  22652. const numDataBytesInBlock = new Int32Array(1);
  22653. const numEcBytesInBlock = new Int32Array(1);
  22654. Encoder$1.getNumDataBytesAndNumECBytesForBlockID(numTotalBytes, numDataBytes, numRSBlocks, i, numDataBytesInBlock, numEcBytesInBlock);
  22655. const size = numDataBytesInBlock[0];
  22656. const dataBytes = new Uint8Array(size);
  22657. bits.toBytes(8 * dataBytesOffset, dataBytes, 0, size);
  22658. const ecBytes = Encoder$1.generateECBytes(dataBytes, numEcBytesInBlock[0]);
  22659. blocks.push(new BlockPair(dataBytes, ecBytes));
  22660. maxNumDataBytes = Math.max(maxNumDataBytes, size);
  22661. maxNumEcBytes = Math.max(maxNumEcBytes, ecBytes.length);
  22662. dataBytesOffset += numDataBytesInBlock[0];
  22663. }
  22664. if (numDataBytes !== dataBytesOffset) {
  22665. throw new WriterException('Data bytes does not match offset');
  22666. }
  22667. const result = new BitArray();
  22668. // First, place data blocks.
  22669. for (let i = 0; i < maxNumDataBytes; ++i) {
  22670. for (const block of blocks) {
  22671. const dataBytes = block.getDataBytes();
  22672. if (i < dataBytes.length) {
  22673. result.appendBits(dataBytes[i], 8);
  22674. }
  22675. }
  22676. }
  22677. // Then, place error correction blocks.
  22678. for (let i = 0; i < maxNumEcBytes; ++i) {
  22679. for (const block of blocks) {
  22680. const ecBytes = block.getErrorCorrectionBytes();
  22681. if (i < ecBytes.length) {
  22682. result.appendBits(ecBytes[i], 8);
  22683. }
  22684. }
  22685. }
  22686. if (numTotalBytes !== result.getSizeInBytes()) { // Should be same.
  22687. throw new WriterException('Interleaving error: ' + numTotalBytes + ' and ' +
  22688. result.getSizeInBytes() + ' differ.');
  22689. }
  22690. return result;
  22691. }
  22692. static generateECBytes(dataBytes, numEcBytesInBlock /*int*/) {
  22693. const numDataBytes = dataBytes.length;
  22694. const toEncode = new Int32Array(numDataBytes + numEcBytesInBlock); // int[numDataBytes + numEcBytesInBlock]
  22695. for (let i = 0; i < numDataBytes; i++) {
  22696. toEncode[i] = dataBytes[i] & 0xFF;
  22697. }
  22698. new ReedSolomonEncoder(GenericGF.QR_CODE_FIELD_256).encode(toEncode, numEcBytesInBlock);
  22699. const ecBytes = new Uint8Array(numEcBytesInBlock);
  22700. for (let i = 0; i < numEcBytesInBlock; i++) {
  22701. ecBytes[i] = /*(byte) */ toEncode[numDataBytes + i];
  22702. }
  22703. return ecBytes;
  22704. }
  22705. /**
  22706. * Append mode info. On success, store the result in "bits".
  22707. */
  22708. static appendModeInfo(mode, bits) {
  22709. bits.appendBits(mode.getBits(), 4);
  22710. }
  22711. /**
  22712. * Append length info. On success, store the result in "bits".
  22713. */
  22714. static appendLengthInfo(numLetters /*int*/, version, mode, bits) {
  22715. const numBits = mode.getCharacterCountBits(version);
  22716. if (numLetters >= (1 << numBits)) {
  22717. throw new WriterException(numLetters + ' is bigger than ' + ((1 << numBits) - 1));
  22718. }
  22719. bits.appendBits(numLetters, numBits);
  22720. }
  22721. /**
  22722. * Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits".
  22723. */
  22724. static appendBytes(content, mode, bits, encoding) {
  22725. switch (mode) {
  22726. case Mode$2.NUMERIC:
  22727. Encoder$1.appendNumericBytes(content, bits);
  22728. break;
  22729. case Mode$2.ALPHANUMERIC:
  22730. Encoder$1.appendAlphanumericBytes(content, bits);
  22731. break;
  22732. case Mode$2.BYTE:
  22733. Encoder$1.append8BitBytes(content, bits, encoding);
  22734. break;
  22735. case Mode$2.KANJI:
  22736. Encoder$1.appendKanjiBytes(content, bits);
  22737. break;
  22738. default:
  22739. throw new WriterException('Invalid mode: ' + mode);
  22740. }
  22741. }
  22742. static getDigit(singleCharacter) {
  22743. return singleCharacter.charCodeAt(0) - 48;
  22744. }
  22745. static isDigit(singleCharacter) {
  22746. const cn = Encoder$1.getDigit(singleCharacter);
  22747. return cn >= 0 && cn <= 9;
  22748. }
  22749. static appendNumericBytes(content, bits) {
  22750. const length = content.length;
  22751. let i = 0;
  22752. while (i < length) {
  22753. const num1 = Encoder$1.getDigit(content.charAt(i));
  22754. if (i + 2 < length) {
  22755. // Encode three numeric letters in ten bits.
  22756. const num2 = Encoder$1.getDigit(content.charAt(i + 1));
  22757. const num3 = Encoder$1.getDigit(content.charAt(i + 2));
  22758. bits.appendBits(num1 * 100 + num2 * 10 + num3, 10);
  22759. i += 3;
  22760. }
  22761. else if (i + 1 < length) {
  22762. // Encode two numeric letters in seven bits.
  22763. const num2 = Encoder$1.getDigit(content.charAt(i + 1));
  22764. bits.appendBits(num1 * 10 + num2, 7);
  22765. i += 2;
  22766. }
  22767. else {
  22768. // Encode one numeric letter in four bits.
  22769. bits.appendBits(num1, 4);
  22770. i++;
  22771. }
  22772. }
  22773. }
  22774. static appendAlphanumericBytes(content, bits) {
  22775. const length = content.length;
  22776. let i = 0;
  22777. while (i < length) {
  22778. const code1 = Encoder$1.getAlphanumericCode(content.charCodeAt(i));
  22779. if (code1 === -1) {
  22780. throw new WriterException();
  22781. }
  22782. if (i + 1 < length) {
  22783. const code2 = Encoder$1.getAlphanumericCode(content.charCodeAt(i + 1));
  22784. if (code2 === -1) {
  22785. throw new WriterException();
  22786. }
  22787. // Encode two alphanumeric letters in 11 bits.
  22788. bits.appendBits(code1 * 45 + code2, 11);
  22789. i += 2;
  22790. }
  22791. else {
  22792. // Encode one alphanumeric letter in six bits.
  22793. bits.appendBits(code1, 6);
  22794. i++;
  22795. }
  22796. }
  22797. }
  22798. static append8BitBytes(content, bits, encoding) {
  22799. let bytes;
  22800. try {
  22801. bytes = StringEncoding.encode(content, encoding);
  22802. }
  22803. catch (uee /*: UnsupportedEncodingException*/) {
  22804. throw new WriterException(uee);
  22805. }
  22806. for (let i = 0, length = bytes.length; i !== length; i++) {
  22807. const b = bytes[i];
  22808. bits.appendBits(b, 8);
  22809. }
  22810. }
  22811. /**
  22812. * @throws WriterException
  22813. */
  22814. static appendKanjiBytes(content, bits) {
  22815. let bytes;
  22816. try {
  22817. bytes = StringEncoding.encode(content, CharacterSetECI.SJIS);
  22818. }
  22819. catch (uee /*: UnsupportedEncodingException*/) {
  22820. throw new WriterException(uee);
  22821. }
  22822. const length = bytes.length;
  22823. for (let i = 0; i < length; i += 2) {
  22824. const byte1 = bytes[i] & 0xFF;
  22825. const byte2 = bytes[i + 1] & 0xFF;
  22826. const code = ((byte1 << 8) & 0xFFFFFFFF) | byte2;
  22827. let subtracted = -1;
  22828. if (code >= 0x8140 && code <= 0x9ffc) {
  22829. subtracted = code - 0x8140;
  22830. }
  22831. else if (code >= 0xe040 && code <= 0xebbf) {
  22832. subtracted = code - 0xc140;
  22833. }
  22834. if (subtracted === -1) {
  22835. throw new WriterException('Invalid byte sequence');
  22836. }
  22837. const encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
  22838. bits.appendBits(encoded, 13);
  22839. }
  22840. }
  22841. static appendECI(eci, bits) {
  22842. bits.appendBits(Mode$2.ECI.getBits(), 4);
  22843. // This is correct for values up to 127, which is all we need now.
  22844. bits.appendBits(eci.getValue(), 8);
  22845. }
  22846. }
  22847. // The original table is defined in the table 5 of JISX0510:2004 (p.19).
  22848. Encoder$1.ALPHANUMERIC_TABLE = Int32Array.from([
  22849. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  22850. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  22851. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
  22852. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
  22853. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  22854. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
  22855. ]);
  22856. Encoder$1.DEFAULT_BYTE_MODE_ENCODING = CharacterSetECI.UTF8.getName(); // "ISO-8859-1"
  22857. /**
  22858. * @deprecated Moving to @zxing/browser
  22859. */
  22860. class BrowserQRCodeSvgWriter {
  22861. /**
  22862. * Writes and renders a QRCode SVG element.
  22863. *
  22864. * @param contents
  22865. * @param width
  22866. * @param height
  22867. * @param hints
  22868. */
  22869. write(contents, width, height, hints = null) {
  22870. if (contents.length === 0) {
  22871. throw new IllegalArgumentException('Found empty contents');
  22872. }
  22873. // if (format != BarcodeFormat.QR_CODE) {
  22874. // throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format)
  22875. // }
  22876. if (width < 0 || height < 0) {
  22877. throw new IllegalArgumentException('Requested dimensions are too small: ' + width + 'x' + height);
  22878. }
  22879. let errorCorrectionLevel = ErrorCorrectionLevel.L;
  22880. let quietZone = BrowserQRCodeSvgWriter.QUIET_ZONE_SIZE;
  22881. if (hints !== null) {
  22882. if (undefined !== hints.get(EncodeHintType$1.ERROR_CORRECTION)) {
  22883. errorCorrectionLevel = ErrorCorrectionLevel.fromString(hints.get(EncodeHintType$1.ERROR_CORRECTION).toString());
  22884. }
  22885. if (undefined !== hints.get(EncodeHintType$1.MARGIN)) {
  22886. quietZone = Number.parseInt(hints.get(EncodeHintType$1.MARGIN).toString(), 10);
  22887. }
  22888. }
  22889. const code = Encoder$1.encode(contents, errorCorrectionLevel, hints);
  22890. return this.renderResult(code, width, height, quietZone);
  22891. }
  22892. /**
  22893. * Renders the result and then appends it to the DOM.
  22894. */
  22895. writeToDom(containerElement, contents, width, height, hints = null) {
  22896. if (typeof containerElement === 'string') {
  22897. containerElement = document.querySelector(containerElement);
  22898. }
  22899. const svgElement = this.write(contents, width, height, hints);
  22900. if (containerElement)
  22901. containerElement.appendChild(svgElement);
  22902. }
  22903. /**
  22904. * Note that the input matrix uses 0 == white, 1 == black.
  22905. * The output matrix uses 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
  22906. */
  22907. renderResult(code, width /*int*/, height /*int*/, quietZone /*int*/) {
  22908. const input = code.getMatrix();
  22909. if (input === null) {
  22910. throw new IllegalStateException();
  22911. }
  22912. const inputWidth = input.getWidth();
  22913. const inputHeight = input.getHeight();
  22914. const qrWidth = inputWidth + (quietZone * 2);
  22915. const qrHeight = inputHeight + (quietZone * 2);
  22916. const outputWidth = Math.max(width, qrWidth);
  22917. const outputHeight = Math.max(height, qrHeight);
  22918. const multiple = Math.min(Math.floor(outputWidth / qrWidth), Math.floor(outputHeight / qrHeight));
  22919. // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  22920. // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  22921. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  22922. // handle all the padding from 100x100 (the actual QR) up to 200x160.
  22923. const leftPadding = Math.floor((outputWidth - (inputWidth * multiple)) / 2);
  22924. const topPadding = Math.floor((outputHeight - (inputHeight * multiple)) / 2);
  22925. const svgElement = this.createSVGElement(outputWidth, outputHeight);
  22926. for (let inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
  22927. // Write the contents of this row of the barcode
  22928. for (let inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
  22929. if (input.get(inputX, inputY) === 1) {
  22930. const svgRectElement = this.createSvgRectElement(outputX, outputY, multiple, multiple);
  22931. svgElement.appendChild(svgRectElement);
  22932. }
  22933. }
  22934. }
  22935. return svgElement;
  22936. }
  22937. /**
  22938. * Creates a SVG element.
  22939. *
  22940. * @param w SVG's width attribute
  22941. * @param h SVG's height attribute
  22942. */
  22943. createSVGElement(w, h) {
  22944. const svgElement = document.createElementNS(BrowserQRCodeSvgWriter.SVG_NS, 'svg');
  22945. svgElement.setAttributeNS(null, 'height', w.toString());
  22946. svgElement.setAttributeNS(null, 'width', h.toString());
  22947. return svgElement;
  22948. }
  22949. /**
  22950. * Creates a SVG rect element.
  22951. *
  22952. * @param x Element's x coordinate
  22953. * @param y Element's y coordinate
  22954. * @param w Element's width attribute
  22955. * @param h Element's height attribute
  22956. */
  22957. createSvgRectElement(x, y, w, h) {
  22958. const rect = document.createElementNS(BrowserQRCodeSvgWriter.SVG_NS, 'rect');
  22959. rect.setAttributeNS(null, 'x', x.toString());
  22960. rect.setAttributeNS(null, 'y', y.toString());
  22961. rect.setAttributeNS(null, 'height', w.toString());
  22962. rect.setAttributeNS(null, 'width', h.toString());
  22963. rect.setAttributeNS(null, 'fill', '#000000');
  22964. return rect;
  22965. }
  22966. }
  22967. BrowserQRCodeSvgWriter.QUIET_ZONE_SIZE = 4;
  22968. /**
  22969. * SVG markup NameSpace
  22970. */
  22971. BrowserQRCodeSvgWriter.SVG_NS = 'http://www.w3.org/2000/svg';
  22972. /*
  22973. * Copyright 2008 ZXing authors
  22974. *
  22975. * Licensed under the Apache License, Version 2.0 (the "License");
  22976. * you may not use this file except in compliance with the License.
  22977. * You may obtain a copy of the License at
  22978. *
  22979. * http://www.apache.org/licenses/LICENSE-2.0
  22980. *
  22981. * Unless required by applicable law or agreed to in writing, software
  22982. * distributed under the License is distributed on an "AS IS" BASIS,
  22983. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22984. * See the License for the specific language governing permissions and
  22985. * limitations under the License.
  22986. */
  22987. /*import java.util.Map;*/
  22988. /**
  22989. * This object renders a QR Code as a BitMatrix 2D array of greyscale values.
  22990. *
  22991. * @author dswitkin@google.com (Daniel Switkin)
  22992. */
  22993. class QRCodeWriter {
  22994. /*@Override*/
  22995. // public encode(contents: string, format: BarcodeFormat, width: number /*int*/, height: number /*int*/): BitMatrix
  22996. // /*throws WriterException */ {
  22997. // return encode(contents, format, width, height, null)
  22998. // }
  22999. /*@Override*/
  23000. encode(contents, format, width /*int*/, height /*int*/, hints) {
  23001. if (contents.length === 0) {
  23002. throw new IllegalArgumentException('Found empty contents');
  23003. }
  23004. if (format !== BarcodeFormat$1.QR_CODE) {
  23005. throw new IllegalArgumentException('Can only encode QR_CODE, but got ' + format);
  23006. }
  23007. if (width < 0 || height < 0) {
  23008. throw new IllegalArgumentException(`Requested dimensions are too small: ${width}x${height}`);
  23009. }
  23010. let errorCorrectionLevel = ErrorCorrectionLevel.L;
  23011. let quietZone = QRCodeWriter.QUIET_ZONE_SIZE;
  23012. if (hints !== null) {
  23013. if (undefined !== hints.get(EncodeHintType$1.ERROR_CORRECTION)) {
  23014. errorCorrectionLevel = ErrorCorrectionLevel.fromString(hints.get(EncodeHintType$1.ERROR_CORRECTION).toString());
  23015. }
  23016. if (undefined !== hints.get(EncodeHintType$1.MARGIN)) {
  23017. quietZone = Number.parseInt(hints.get(EncodeHintType$1.MARGIN).toString(), 10);
  23018. }
  23019. }
  23020. const code = Encoder$1.encode(contents, errorCorrectionLevel, hints);
  23021. return QRCodeWriter.renderResult(code, width, height, quietZone);
  23022. }
  23023. // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
  23024. // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
  23025. static renderResult(code, width /*int*/, height /*int*/, quietZone /*int*/) {
  23026. const input = code.getMatrix();
  23027. if (input === null) {
  23028. throw new IllegalStateException();
  23029. }
  23030. const inputWidth = input.getWidth();
  23031. const inputHeight = input.getHeight();
  23032. const qrWidth = inputWidth + (quietZone * 2);
  23033. const qrHeight = inputHeight + (quietZone * 2);
  23034. const outputWidth = Math.max(width, qrWidth);
  23035. const outputHeight = Math.max(height, qrHeight);
  23036. const multiple = Math.min(Math.floor(outputWidth / qrWidth), Math.floor(outputHeight / qrHeight));
  23037. // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  23038. // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  23039. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  23040. // handle all the padding from 100x100 (the actual QR) up to 200x160.
  23041. const leftPadding = Math.floor((outputWidth - (inputWidth * multiple)) / 2);
  23042. const topPadding = Math.floor((outputHeight - (inputHeight * multiple)) / 2);
  23043. const output = new BitMatrix(outputWidth, outputHeight);
  23044. for (let inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
  23045. // Write the contents of this row of the barcode
  23046. for (let inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
  23047. if (input.get(inputX, inputY) === 1) {
  23048. output.setRegion(outputX, outputY, multiple, multiple);
  23049. }
  23050. }
  23051. }
  23052. return output;
  23053. }
  23054. }
  23055. QRCodeWriter.QUIET_ZONE_SIZE = 4;
  23056. /*
  23057. * Copyright 2008 ZXing authors
  23058. *
  23059. * Licensed under the Apache License, Version 2.0 (the "License");
  23060. * you may not use this file except in compliance with the License.
  23061. * You may obtain a copy of the License at
  23062. *
  23063. * http://www.apache.org/licenses/LICENSE-2.0
  23064. *
  23065. * Unless required by applicable law or agreed to in writing, software
  23066. * distributed under the License is distributed on an "AS IS" BASIS,
  23067. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23068. * See the License for the specific language governing permissions and
  23069. * limitations under the License.
  23070. */
  23071. /*import java.util.Map;*/
  23072. /**
  23073. * This is a factory class which finds the appropriate Writer subclass for the BarcodeFormat
  23074. * requested and encodes the barcode with the supplied contents.
  23075. *
  23076. * @author dswitkin@google.com (Daniel Switkin)
  23077. */
  23078. class MultiFormatWriter {
  23079. /*@Override*/
  23080. // public encode(contents: string,
  23081. // format: BarcodeFormat,
  23082. // width: number /*int*/,
  23083. // height: number /*int*/): BitMatrix /*throws WriterException */ {
  23084. // return encode(contents, format, width, height, null)
  23085. // }
  23086. /*@Override*/
  23087. encode(contents, format, width /*int*/, height /*int*/, hints) {
  23088. let writer;
  23089. switch (format) {
  23090. // case BarcodeFormat.EAN_8:
  23091. // writer = new EAN8Writer()
  23092. // break
  23093. // case BarcodeFormat.UPC_E:
  23094. // writer = new UPCEWriter()
  23095. // break
  23096. // case BarcodeFormat.EAN_13:
  23097. // writer = new EAN13Writer()
  23098. // break
  23099. // case BarcodeFormat.UPC_A:
  23100. // writer = new UPCAWriter()
  23101. // break
  23102. case BarcodeFormat$1.QR_CODE:
  23103. writer = new QRCodeWriter();
  23104. break;
  23105. // case BarcodeFormat.CODE_39:
  23106. // writer = new Code39Writer()
  23107. // break
  23108. // case BarcodeFormat.CODE_93:
  23109. // writer = new Code93Writer()
  23110. // break
  23111. // case BarcodeFormat.CODE_128:
  23112. // writer = new Code128Writer()
  23113. // break
  23114. // case BarcodeFormat.ITF:
  23115. // writer = new ITFWriter()
  23116. // break
  23117. // case BarcodeFormat.PDF_417:
  23118. // writer = new PDF417Writer()
  23119. // break
  23120. // case BarcodeFormat.CODABAR:
  23121. // writer = new CodaBarWriter()
  23122. // break
  23123. // case BarcodeFormat.DATA_MATRIX:
  23124. // writer = new DataMatrixWriter()
  23125. // break
  23126. // case BarcodeFormat.AZTEC:
  23127. // writer = new AztecWriter()
  23128. // break
  23129. default:
  23130. throw new IllegalArgumentException('No encoder available for format ' + format);
  23131. }
  23132. return writer.encode(contents, format, width, height, hints);
  23133. }
  23134. }
  23135. /*
  23136. * Copyright 2009 ZXing authors
  23137. *
  23138. * Licensed under the Apache License, Version 2.0 (the "License");
  23139. * you may not use this file except in compliance with the License.
  23140. * You may obtain a copy of the License at
  23141. *
  23142. * http://www.apache.org/licenses/LICENSE-2.0
  23143. *
  23144. * Unless required by applicable law or agreed to in writing, software
  23145. * distributed under the License is distributed on an "AS IS" BASIS,
  23146. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23147. * See the License for the specific language governing permissions and
  23148. * limitations under the License.
  23149. */
  23150. /**
  23151. * This object extends LuminanceSource around an array of YUV data returned from the camera driver,
  23152. * with the option to crop to a rectangle within the full data. This can be used to exclude
  23153. * superfluous pixels around the perimeter and speed up decoding.
  23154. *
  23155. * It works for any pixel format where the Y channel is planar and appears first, including
  23156. * YCbCr_420_SP and YCbCr_422_SP.
  23157. *
  23158. * @author dswitkin@google.com (Daniel Switkin)
  23159. */
  23160. class PlanarYUVLuminanceSource extends LuminanceSource {
  23161. constructor(yuvData, dataWidth /*int*/, dataHeight /*int*/, left /*int*/, top /*int*/, width /*int*/, height /*int*/, reverseHorizontal) {
  23162. super(width, height);
  23163. this.yuvData = yuvData;
  23164. this.dataWidth = dataWidth;
  23165. this.dataHeight = dataHeight;
  23166. this.left = left;
  23167. this.top = top;
  23168. if (left + width > dataWidth || top + height > dataHeight) {
  23169. throw new IllegalArgumentException('Crop rectangle does not fit within image data.');
  23170. }
  23171. if (reverseHorizontal) {
  23172. this.reverseHorizontal(width, height);
  23173. }
  23174. }
  23175. /*@Override*/
  23176. getRow(y /*int*/, row) {
  23177. if (y < 0 || y >= this.getHeight()) {
  23178. throw new IllegalArgumentException('Requested row is outside the image: ' + y);
  23179. }
  23180. const width = this.getWidth();
  23181. if (row === null || row === undefined || row.length < width) {
  23182. row = new Uint8ClampedArray(width);
  23183. }
  23184. const offset = (y + this.top) * this.dataWidth + this.left;
  23185. System.arraycopy(this.yuvData, offset, row, 0, width);
  23186. return row;
  23187. }
  23188. /*@Override*/
  23189. getMatrix() {
  23190. const width = this.getWidth();
  23191. const height = this.getHeight();
  23192. // If the caller asks for the entire underlying image, save the copy and give them the
  23193. // original data. The docs specifically warn that result.length must be ignored.
  23194. if (width === this.dataWidth && height === this.dataHeight) {
  23195. return this.yuvData;
  23196. }
  23197. const area = width * height;
  23198. const matrix = new Uint8ClampedArray(area);
  23199. let inputOffset = this.top * this.dataWidth + this.left;
  23200. // If the width matches the full width of the underlying data, perform a single copy.
  23201. if (width === this.dataWidth) {
  23202. System.arraycopy(this.yuvData, inputOffset, matrix, 0, area);
  23203. return matrix;
  23204. }
  23205. // Otherwise copy one cropped row at a time.
  23206. for (let y = 0; y < height; y++) {
  23207. const outputOffset = y * width;
  23208. System.arraycopy(this.yuvData, inputOffset, matrix, outputOffset, width);
  23209. inputOffset += this.dataWidth;
  23210. }
  23211. return matrix;
  23212. }
  23213. /*@Override*/
  23214. isCropSupported() {
  23215. return true;
  23216. }
  23217. /*@Override*/
  23218. crop(left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  23219. return new PlanarYUVLuminanceSource(this.yuvData, this.dataWidth, this.dataHeight, this.left + left, this.top + top, width, height, false);
  23220. }
  23221. renderThumbnail() {
  23222. const width = this.getWidth() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
  23223. const height = this.getHeight() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
  23224. const pixels = new Int32Array(width * height);
  23225. const yuv = this.yuvData;
  23226. let inputOffset = this.top * this.dataWidth + this.left;
  23227. for (let y = 0; y < height; y++) {
  23228. const outputOffset = y * width;
  23229. for (let x = 0; x < width; x++) {
  23230. const grey = yuv[inputOffset + x * PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR] & 0xff;
  23231. pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
  23232. }
  23233. inputOffset += this.dataWidth * PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
  23234. }
  23235. return pixels;
  23236. }
  23237. /**
  23238. * @return width of image from {@link #renderThumbnail()}
  23239. */
  23240. getThumbnailWidth() {
  23241. return this.getWidth() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
  23242. }
  23243. /**
  23244. * @return height of image from {@link #renderThumbnail()}
  23245. */
  23246. getThumbnailHeight() {
  23247. return this.getHeight() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
  23248. }
  23249. reverseHorizontal(width /*int*/, height /*int*/) {
  23250. const yuvData = this.yuvData;
  23251. for (let y = 0, rowStart = this.top * this.dataWidth + this.left; y < height; y++, rowStart += this.dataWidth) {
  23252. const middle = rowStart + width / 2;
  23253. for (let x1 = rowStart, x2 = rowStart + width - 1; x1 < middle; x1++, x2--) {
  23254. const temp = yuvData[x1];
  23255. yuvData[x1] = yuvData[x2];
  23256. yuvData[x2] = temp;
  23257. }
  23258. }
  23259. }
  23260. invert() {
  23261. return new InvertedLuminanceSource(this);
  23262. }
  23263. }
  23264. PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR = 2;
  23265. /*
  23266. * Copyright 2009 ZXing authors
  23267. *
  23268. * Licensed under the Apache License, Version 2.0 (the "License");
  23269. * you may not use this file except in compliance with the License.
  23270. * You may obtain a copy of the License at
  23271. *
  23272. * http://www.apache.org/licenses/LICENSE-2.0
  23273. *
  23274. * Unless required by applicable law or agreed to in writing, software
  23275. * distributed under the License is distributed on an "AS IS" BASIS,
  23276. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23277. * See the License for the specific language governing permissions and
  23278. * limitations under the License.
  23279. */
  23280. /**
  23281. * This class is used to help decode images from files which arrive as RGB data from
  23282. * an ARGB pixel array. It does not support rotation.
  23283. *
  23284. * @author dswitkin@google.com (Daniel Switkin)
  23285. * @author Betaminos
  23286. */
  23287. class RGBLuminanceSource extends LuminanceSource {
  23288. constructor(luminances, width /*int*/, height /*int*/, dataWidth /*int*/, dataHeight /*int*/, left /*int*/, top /*int*/) {
  23289. super(width, height);
  23290. this.dataWidth = dataWidth;
  23291. this.dataHeight = dataHeight;
  23292. this.left = left;
  23293. this.top = top;
  23294. if (luminances.BYTES_PER_ELEMENT === 4) { // Int32Array
  23295. const size = width * height;
  23296. const luminancesUint8Array = new Uint8ClampedArray(size);
  23297. for (let offset = 0; offset < size; offset++) {
  23298. const pixel = luminances[offset];
  23299. const r = (pixel >> 16) & 0xff; // red
  23300. const g2 = (pixel >> 7) & 0x1fe; // 2 * green
  23301. const b = pixel & 0xff; // blue
  23302. // Calculate green-favouring average cheaply
  23303. luminancesUint8Array[offset] = /*(byte) */ ((r + g2 + b) / 4) & 0xFF;
  23304. }
  23305. this.luminances = luminancesUint8Array;
  23306. }
  23307. else {
  23308. this.luminances = luminances;
  23309. }
  23310. if (undefined === dataWidth) {
  23311. this.dataWidth = width;
  23312. }
  23313. if (undefined === dataHeight) {
  23314. this.dataHeight = height;
  23315. }
  23316. if (undefined === left) {
  23317. this.left = 0;
  23318. }
  23319. if (undefined === top) {
  23320. this.top = 0;
  23321. }
  23322. if (this.left + width > this.dataWidth || this.top + height > this.dataHeight) {
  23323. throw new IllegalArgumentException('Crop rectangle does not fit within image data.');
  23324. }
  23325. }
  23326. /*@Override*/
  23327. getRow(y /*int*/, row) {
  23328. if (y < 0 || y >= this.getHeight()) {
  23329. throw new IllegalArgumentException('Requested row is outside the image: ' + y);
  23330. }
  23331. const width = this.getWidth();
  23332. if (row === null || row === undefined || row.length < width) {
  23333. row = new Uint8ClampedArray(width);
  23334. }
  23335. const offset = (y + this.top) * this.dataWidth + this.left;
  23336. System.arraycopy(this.luminances, offset, row, 0, width);
  23337. return row;
  23338. }
  23339. /*@Override*/
  23340. getMatrix() {
  23341. const width = this.getWidth();
  23342. const height = this.getHeight();
  23343. // If the caller asks for the entire underlying image, save the copy and give them the
  23344. // original data. The docs specifically warn that result.length must be ignored.
  23345. if (width === this.dataWidth && height === this.dataHeight) {
  23346. return this.luminances;
  23347. }
  23348. const area = width * height;
  23349. const matrix = new Uint8ClampedArray(area);
  23350. let inputOffset = this.top * this.dataWidth + this.left;
  23351. // If the width matches the full width of the underlying data, perform a single copy.
  23352. if (width === this.dataWidth) {
  23353. System.arraycopy(this.luminances, inputOffset, matrix, 0, area);
  23354. return matrix;
  23355. }
  23356. // Otherwise copy one cropped row at a time.
  23357. for (let y = 0; y < height; y++) {
  23358. const outputOffset = y * width;
  23359. System.arraycopy(this.luminances, inputOffset, matrix, outputOffset, width);
  23360. inputOffset += this.dataWidth;
  23361. }
  23362. return matrix;
  23363. }
  23364. /*@Override*/
  23365. isCropSupported() {
  23366. return true;
  23367. }
  23368. /*@Override*/
  23369. crop(left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  23370. return new RGBLuminanceSource(this.luminances, width, height, this.dataWidth, this.dataHeight, this.left + left, this.top + top);
  23371. }
  23372. invert() {
  23373. return new InvertedLuminanceSource(this);
  23374. }
  23375. }
  23376. /**
  23377. * Just to make a shortcut between Java code and TS code.
  23378. */
  23379. class Charset extends CharacterSetECI {
  23380. static forName(name) {
  23381. return this.getCharacterSetECIByName(name);
  23382. }
  23383. }
  23384. /**
  23385. * Just to make a shortcut between Java code and TS code.
  23386. */
  23387. class StandardCharsets {
  23388. }
  23389. StandardCharsets.ISO_8859_1 = CharacterSetECI.ISO8859_1;
  23390. /**
  23391. * Symbol Character Placement Program. Adapted from Annex M.1 in ISO/IEC 16022:2000(E).
  23392. */
  23393. class DefaultPlacement {
  23394. /**
  23395. * Main constructor
  23396. *
  23397. * @param codewords the codewords to place
  23398. * @param numcols the number of columns
  23399. * @param numrows the number of rows
  23400. */
  23401. constructor(codewords, numcols, numrows) {
  23402. this.codewords = codewords;
  23403. this.numcols = numcols;
  23404. this.numrows = numrows;
  23405. this.bits = new Uint8Array(numcols * numrows);
  23406. Arrays.fill(this.bits, 2); // Initialize with "not set" value
  23407. }
  23408. getNumrows() {
  23409. return this.numrows;
  23410. }
  23411. getNumcols() {
  23412. return this.numcols;
  23413. }
  23414. getBits() {
  23415. return this.bits;
  23416. }
  23417. getBit(col, row) {
  23418. return this.bits[row * this.numcols + col] === 1;
  23419. }
  23420. setBit(col, row, bit) {
  23421. this.bits[row * this.numcols + col] = bit ? 1 : 0;
  23422. }
  23423. noBit(col, row) {
  23424. return this.bits[row * this.numcols + col] === 2;
  23425. }
  23426. place() {
  23427. let pos = 0;
  23428. let row = 4;
  23429. let col = 0;
  23430. do {
  23431. // repeatedly first check for one of the special corner cases, then...
  23432. if (row === this.numrows && col === 0) {
  23433. this.corner1(pos++);
  23434. }
  23435. if (row === this.numrows - 2 && col === 0 && this.numcols % 4 !== 0) {
  23436. this.corner2(pos++);
  23437. }
  23438. if (row === this.numrows - 2 && col === 0 && this.numcols % 8 === 4) {
  23439. this.corner3(pos++);
  23440. }
  23441. if (row === this.numrows + 4 && col === 2 && this.numcols % 8 === 0) {
  23442. this.corner4(pos++);
  23443. }
  23444. // sweep upward diagonally, inserting successive characters...
  23445. do {
  23446. if (row < this.numrows && col >= 0 && this.noBit(col, row)) {
  23447. this.utah(row, col, pos++);
  23448. }
  23449. row -= 2;
  23450. col += 2;
  23451. } while (row >= 0 && col < this.numcols);
  23452. row++;
  23453. col += 3;
  23454. // and then sweep downward diagonally, inserting successive characters, ...
  23455. do {
  23456. if (row >= 0 && col < this.numcols && this.noBit(col, row)) {
  23457. this.utah(row, col, pos++);
  23458. }
  23459. row += 2;
  23460. col -= 2;
  23461. } while (row < this.numrows && col >= 0);
  23462. row += 3;
  23463. col++;
  23464. // ...until the entire array is scanned
  23465. } while (row < this.numrows || col < this.numcols);
  23466. // Lastly, if the lower right-hand corner is untouched, fill in fixed pattern
  23467. if (this.noBit(this.numcols - 1, this.numrows - 1)) {
  23468. this.setBit(this.numcols - 1, this.numrows - 1, true);
  23469. this.setBit(this.numcols - 2, this.numrows - 2, true);
  23470. }
  23471. }
  23472. module(row, col, pos, bit) {
  23473. if (row < 0) {
  23474. row += this.numrows;
  23475. col += 4 - ((this.numrows + 4) % 8);
  23476. }
  23477. if (col < 0) {
  23478. col += this.numcols;
  23479. row += 4 - ((this.numcols + 4) % 8);
  23480. }
  23481. // Note the conversion:
  23482. let v = this.codewords.charCodeAt(pos);
  23483. v &= 1 << (8 - bit);
  23484. this.setBit(col, row, v !== 0);
  23485. }
  23486. /**
  23487. * Places the 8 bits of a utah-shaped symbol character in ECC200.
  23488. *
  23489. * @param row the row
  23490. * @param col the column
  23491. * @param pos character position
  23492. */
  23493. utah(row, col, pos) {
  23494. this.module(row - 2, col - 2, pos, 1);
  23495. this.module(row - 2, col - 1, pos, 2);
  23496. this.module(row - 1, col - 2, pos, 3);
  23497. this.module(row - 1, col - 1, pos, 4);
  23498. this.module(row - 1, col, pos, 5);
  23499. this.module(row, col - 2, pos, 6);
  23500. this.module(row, col - 1, pos, 7);
  23501. this.module(row, col, pos, 8);
  23502. }
  23503. corner1(pos) {
  23504. this.module(this.numrows - 1, 0, pos, 1);
  23505. this.module(this.numrows - 1, 1, pos, 2);
  23506. this.module(this.numrows - 1, 2, pos, 3);
  23507. this.module(0, this.numcols - 2, pos, 4);
  23508. this.module(0, this.numcols - 1, pos, 5);
  23509. this.module(1, this.numcols - 1, pos, 6);
  23510. this.module(2, this.numcols - 1, pos, 7);
  23511. this.module(3, this.numcols - 1, pos, 8);
  23512. }
  23513. corner2(pos) {
  23514. this.module(this.numrows - 3, 0, pos, 1);
  23515. this.module(this.numrows - 2, 0, pos, 2);
  23516. this.module(this.numrows - 1, 0, pos, 3);
  23517. this.module(0, this.numcols - 4, pos, 4);
  23518. this.module(0, this.numcols - 3, pos, 5);
  23519. this.module(0, this.numcols - 2, pos, 6);
  23520. this.module(0, this.numcols - 1, pos, 7);
  23521. this.module(1, this.numcols - 1, pos, 8);
  23522. }
  23523. corner3(pos) {
  23524. this.module(this.numrows - 3, 0, pos, 1);
  23525. this.module(this.numrows - 2, 0, pos, 2);
  23526. this.module(this.numrows - 1, 0, pos, 3);
  23527. this.module(0, this.numcols - 2, pos, 4);
  23528. this.module(0, this.numcols - 1, pos, 5);
  23529. this.module(1, this.numcols - 1, pos, 6);
  23530. this.module(2, this.numcols - 1, pos, 7);
  23531. this.module(3, this.numcols - 1, pos, 8);
  23532. }
  23533. corner4(pos) {
  23534. this.module(this.numrows - 1, 0, pos, 1);
  23535. this.module(this.numrows - 1, this.numcols - 1, pos, 2);
  23536. this.module(0, this.numcols - 3, pos, 3);
  23537. this.module(0, this.numcols - 2, pos, 4);
  23538. this.module(0, this.numcols - 1, pos, 5);
  23539. this.module(1, this.numcols - 3, pos, 6);
  23540. this.module(1, this.numcols - 2, pos, 7);
  23541. this.module(1, this.numcols - 1, pos, 8);
  23542. }
  23543. }
  23544. /**
  23545. * Lookup table which factors to use for which number of error correction codewords.
  23546. * See FACTORS.
  23547. */
  23548. const FACTOR_SETS = [
  23549. 5, 7, 10, 11, 12, 14, 18, 20, 24, 28, 36, 42, 48, 56, 62, 68,
  23550. ];
  23551. /**
  23552. * Precomputed polynomial factors for ECC 200.
  23553. */
  23554. const FACTORS = [
  23555. [228, 48, 15, 111, 62],
  23556. [23, 68, 144, 134, 240, 92, 254],
  23557. [28, 24, 185, 166, 223, 248, 116, 255, 110, 61],
  23558. [175, 138, 205, 12, 194, 168, 39, 245, 60, 97, 120],
  23559. [41, 153, 158, 91, 61, 42, 142, 213, 97, 178, 100, 242],
  23560. [156, 97, 192, 252, 95, 9, 157, 119, 138, 45, 18, 186, 83, 185],
  23561. [
  23562. 83, 195, 100, 39, 188, 75, 66, 61, 241, 213, 109, 129, 94, 254, 225, 48, 90,
  23563. 188,
  23564. ],
  23565. [
  23566. 15, 195, 244, 9, 233, 71, 168, 2, 188, 160, 153, 145, 253, 79, 108, 82, 27,
  23567. 174, 186, 172,
  23568. ],
  23569. [
  23570. 52, 190, 88, 205, 109, 39, 176, 21, 155, 197, 251, 223, 155, 21, 5, 172,
  23571. 254, 124, 12, 181, 184, 96, 50, 193,
  23572. ],
  23573. [
  23574. 211, 231, 43, 97, 71, 96, 103, 174, 37, 151, 170, 53, 75, 34, 249, 121, 17,
  23575. 138, 110, 213, 141, 136, 120, 151, 233, 168, 93, 255,
  23576. ],
  23577. [
  23578. 245, 127, 242, 218, 130, 250, 162, 181, 102, 120, 84, 179, 220, 251, 80,
  23579. 182, 229, 18, 2, 4, 68, 33, 101, 137, 95, 119, 115, 44, 175, 184, 59, 25,
  23580. 225, 98, 81, 112,
  23581. ],
  23582. [
  23583. 77, 193, 137, 31, 19, 38, 22, 153, 247, 105, 122, 2, 245, 133, 242, 8, 175,
  23584. 95, 100, 9, 167, 105, 214, 111, 57, 121, 21, 1, 253, 57, 54, 101, 248, 202,
  23585. 69, 50, 150, 177, 226, 5, 9, 5,
  23586. ],
  23587. [
  23588. 245, 132, 172, 223, 96, 32, 117, 22, 238, 133, 238, 231, 205, 188, 237, 87,
  23589. 191, 106, 16, 147, 118, 23, 37, 90, 170, 205, 131, 88, 120, 100, 66, 138,
  23590. 186, 240, 82, 44, 176, 87, 187, 147, 160, 175, 69, 213, 92, 253, 225, 19,
  23591. ],
  23592. [
  23593. 175, 9, 223, 238, 12, 17, 220, 208, 100, 29, 175, 170, 230, 192, 215, 235,
  23594. 150, 159, 36, 223, 38, 200, 132, 54, 228, 146, 218, 234, 117, 203, 29, 232,
  23595. 144, 238, 22, 150, 201, 117, 62, 207, 164, 13, 137, 245, 127, 67, 247, 28,
  23596. 155, 43, 203, 107, 233, 53, 143, 46,
  23597. ],
  23598. [
  23599. 242, 93, 169, 50, 144, 210, 39, 118, 202, 188, 201, 189, 143, 108, 196, 37,
  23600. 185, 112, 134, 230, 245, 63, 197, 190, 250, 106, 185, 221, 175, 64, 114, 71,
  23601. 161, 44, 147, 6, 27, 218, 51, 63, 87, 10, 40, 130, 188, 17, 163, 31, 176,
  23602. 170, 4, 107, 232, 7, 94, 166, 224, 124, 86, 47, 11, 204,
  23603. ],
  23604. [
  23605. 220, 228, 173, 89, 251, 149, 159, 56, 89, 33, 147, 244, 154, 36, 73, 127,
  23606. 213, 136, 248, 180, 234, 197, 158, 177, 68, 122, 93, 213, 15, 160, 227, 236,
  23607. 66, 139, 153, 185, 202, 167, 179, 25, 220, 232, 96, 210, 231, 136, 223, 239,
  23608. 181, 241, 59, 52, 172, 25, 49, 232, 211, 189, 64, 54, 108, 153, 132, 63, 96,
  23609. 103, 82, 186,
  23610. ],
  23611. ];
  23612. const /*final*/ MODULO_VALUE = 0x12d;
  23613. const static_LOG = (LOG, ALOG) => {
  23614. let p = 1;
  23615. for (let i = 0; i < 255; i++) {
  23616. ALOG[i] = p;
  23617. LOG[p] = i;
  23618. p *= 2;
  23619. if (p >= 256) {
  23620. p ^= MODULO_VALUE;
  23621. }
  23622. }
  23623. return {
  23624. LOG,
  23625. ALOG,
  23626. };
  23627. };
  23628. const { LOG, ALOG } = static_LOG([], []);
  23629. exports.DataMatrixSymbolShapeHint = void 0;
  23630. (function (SymbolShapeHint) {
  23631. SymbolShapeHint[SymbolShapeHint["FORCE_NONE"] = 0] = "FORCE_NONE";
  23632. SymbolShapeHint[SymbolShapeHint["FORCE_SQUARE"] = 1] = "FORCE_SQUARE";
  23633. SymbolShapeHint[SymbolShapeHint["FORCE_RECTANGLE"] = 2] = "FORCE_RECTANGLE";
  23634. })(exports.DataMatrixSymbolShapeHint || (exports.DataMatrixSymbolShapeHint = {}));
  23635. /**
  23636. * Padding character
  23637. */
  23638. const PAD = 129;
  23639. /**
  23640. * mode latch to C40 encodation mode
  23641. */
  23642. const LATCH_TO_C40 = 230;
  23643. /**
  23644. * mode latch to Base 256 encodation mode
  23645. */
  23646. const LATCH_TO_BASE256 = 231;
  23647. /**
  23648. * FNC1 Codeword
  23649. */
  23650. // private static FNC1 = 232;
  23651. /**
  23652. * Structured Append Codeword
  23653. */
  23654. // private static STRUCTURED_APPEND = 233;
  23655. /**
  23656. * Reader Programming
  23657. */
  23658. // private static READER_PROGRAMMING = 234;
  23659. /**
  23660. * Upper Shift
  23661. */
  23662. const UPPER_SHIFT = 235;
  23663. /**
  23664. * 05 Macro
  23665. */
  23666. const MACRO_05 = 236;
  23667. /**
  23668. * 06 Macro
  23669. */
  23670. const MACRO_06 = 237;
  23671. /**
  23672. * mode latch to ANSI X.12 encodation mode
  23673. */
  23674. const LATCH_TO_ANSIX12 = 238;
  23675. /**
  23676. * mode latch to Text encodation mode
  23677. */
  23678. const LATCH_TO_TEXT = 239;
  23679. /**
  23680. * mode latch to EDIFACT encodation mode
  23681. */
  23682. const LATCH_TO_EDIFACT = 240;
  23683. /**
  23684. * ECI character (Extended Channel Interpretation)
  23685. */
  23686. // private export const ECI = 241;
  23687. /**
  23688. * Unlatch from C40 encodation
  23689. */
  23690. const C40_UNLATCH = 254;
  23691. /**
  23692. * Unlatch from X12 encodation
  23693. */
  23694. const X12_UNLATCH = 254;
  23695. /**
  23696. * 05 Macro header
  23697. */
  23698. const MACRO_05_HEADER = '[)>\u001E05\u001D';
  23699. /**
  23700. * 06 Macro header
  23701. */
  23702. const MACRO_06_HEADER = '[)>\u001E06\u001D';
  23703. /**
  23704. * Macro trailer
  23705. */
  23706. const MACRO_TRAILER = '\u001E\u0004';
  23707. const ASCII_ENCODATION = 0;
  23708. const C40_ENCODATION = 1;
  23709. const TEXT_ENCODATION = 2;
  23710. const X12_ENCODATION = 3;
  23711. const EDIFACT_ENCODATION = 4;
  23712. const BASE256_ENCODATION = 5;
  23713. /**
  23714. * Error Correction Code for ECC200.
  23715. */
  23716. class ErrorCorrection {
  23717. /**
  23718. * Creates the ECC200 error correction for an encoded message.
  23719. *
  23720. * @param codewords the codewords
  23721. * @param symbolInfo information about the symbol to be encoded
  23722. * @return the codewords with interleaved error correction.
  23723. */
  23724. static encodeECC200(codewords, symbolInfo) {
  23725. if (codewords.length !== symbolInfo.getDataCapacity()) {
  23726. throw new Error('The number of codewords does not match the selected symbol');
  23727. }
  23728. const sb = new StringBuilder();
  23729. sb.append(codewords);
  23730. const blockCount = symbolInfo.getInterleavedBlockCount();
  23731. if (blockCount === 1) {
  23732. const ecc = this.createECCBlock(codewords, symbolInfo.getErrorCodewords());
  23733. sb.append(ecc);
  23734. }
  23735. else {
  23736. const errorSizes = [];
  23737. for (let i = 0; i < blockCount; i++) {
  23738. symbolInfo.getDataLengthForInterleavedBlock(i + 1);
  23739. errorSizes[i] = symbolInfo.getErrorLengthForInterleavedBlock(i + 1);
  23740. }
  23741. for (let block = 0; block < blockCount; block++) {
  23742. const temp = new StringBuilder();
  23743. for (let d = block; d < symbolInfo.getDataCapacity(); d += blockCount) {
  23744. temp.append(codewords.charAt(d));
  23745. }
  23746. const ecc = this.createECCBlock(temp.toString(), errorSizes[block]);
  23747. let pos = 0;
  23748. for (let e = block; e < errorSizes[block] * blockCount; e += blockCount) {
  23749. sb.setCharAt(symbolInfo.getDataCapacity() + e, ecc.charAt(pos++));
  23750. }
  23751. }
  23752. }
  23753. return sb.toString();
  23754. }
  23755. static createECCBlock(codewords, numECWords) {
  23756. let table = -1;
  23757. for (let i = 0; i < FACTOR_SETS.length; i++) {
  23758. if (FACTOR_SETS[i] === numECWords) {
  23759. table = i;
  23760. break;
  23761. }
  23762. }
  23763. if (table < 0) {
  23764. throw new Error('Illegal number of error correction codewords specified: ' + numECWords);
  23765. }
  23766. const poly = FACTORS[table];
  23767. const ecc = [];
  23768. for (let i = 0; i < numECWords; i++) {
  23769. ecc[i] = 0;
  23770. }
  23771. for (let i = 0; i < codewords.length; i++) {
  23772. let m = ecc[numECWords - 1] ^ codewords.charAt(i).charCodeAt(0);
  23773. for (let k = numECWords - 1; k > 0; k--) {
  23774. if (m !== 0 && poly[k] !== 0) {
  23775. ecc[k] = ecc[k - 1] ^ ALOG[(LOG[m] + LOG[poly[k]]) % 255];
  23776. }
  23777. else {
  23778. ecc[k] = ecc[k - 1];
  23779. }
  23780. }
  23781. if (m !== 0 && poly[0] !== 0) {
  23782. ecc[0] = ALOG[(LOG[m] + LOG[poly[0]]) % 255];
  23783. }
  23784. else {
  23785. ecc[0] = 0;
  23786. }
  23787. }
  23788. const eccReversed = [];
  23789. for (let i = 0; i < numECWords; i++) {
  23790. eccReversed[i] = ecc[numECWords - i - 1];
  23791. }
  23792. return eccReversed.map(c => String.fromCharCode(c)).join('');
  23793. }
  23794. }
  23795. class ASCIIEncoder {
  23796. getEncodingMode() {
  23797. return ASCII_ENCODATION;
  23798. }
  23799. encode(context) {
  23800. // step B
  23801. const n = HighLevelEncoder$1.determineConsecutiveDigitCount(context.getMessage(), context.pos);
  23802. if (n >= 2) {
  23803. context.writeCodeword(this.encodeASCIIDigits(context.getMessage().charCodeAt(context.pos), context.getMessage().charCodeAt(context.pos + 1)));
  23804. context.pos += 2;
  23805. }
  23806. else {
  23807. const c = context.getCurrentChar();
  23808. const newMode = HighLevelEncoder$1.lookAheadTest(context.getMessage(), context.pos, this.getEncodingMode());
  23809. if (newMode !== this.getEncodingMode()) {
  23810. switch (newMode) {
  23811. case BASE256_ENCODATION:
  23812. context.writeCodeword(LATCH_TO_BASE256);
  23813. context.signalEncoderChange(BASE256_ENCODATION);
  23814. return;
  23815. case C40_ENCODATION:
  23816. context.writeCodeword(LATCH_TO_C40);
  23817. context.signalEncoderChange(C40_ENCODATION);
  23818. return;
  23819. case X12_ENCODATION:
  23820. context.writeCodeword(LATCH_TO_ANSIX12);
  23821. context.signalEncoderChange(X12_ENCODATION);
  23822. break;
  23823. case TEXT_ENCODATION:
  23824. context.writeCodeword(LATCH_TO_TEXT);
  23825. context.signalEncoderChange(TEXT_ENCODATION);
  23826. break;
  23827. case EDIFACT_ENCODATION:
  23828. context.writeCodeword(LATCH_TO_EDIFACT);
  23829. context.signalEncoderChange(EDIFACT_ENCODATION);
  23830. break;
  23831. default:
  23832. throw new Error('Illegal mode: ' + newMode);
  23833. }
  23834. }
  23835. else if (HighLevelEncoder$1.isExtendedASCII(c)) {
  23836. context.writeCodeword(UPPER_SHIFT);
  23837. context.writeCodeword(c - 128 + 1);
  23838. context.pos++;
  23839. }
  23840. else {
  23841. context.writeCodeword(c + 1);
  23842. context.pos++;
  23843. }
  23844. }
  23845. }
  23846. encodeASCIIDigits(digit1, digit2) {
  23847. if (HighLevelEncoder$1.isDigit(digit1) && HighLevelEncoder$1.isDigit(digit2)) {
  23848. const num = (digit1 - 48) * 10 + (digit2 - 48);
  23849. return num + 130;
  23850. }
  23851. throw new Error('not digits: ' + digit1 + digit2);
  23852. }
  23853. }
  23854. class Base256Encoder {
  23855. getEncodingMode() {
  23856. return BASE256_ENCODATION;
  23857. }
  23858. encode(context) {
  23859. const buffer = new StringBuilder();
  23860. buffer.append(0o0); // Initialize length field
  23861. while (context.hasMoreCharacters()) {
  23862. const c = context.getCurrentChar();
  23863. buffer.append(c);
  23864. context.pos++;
  23865. const newMode = HighLevelEncoder$1.lookAheadTest(context.getMessage(), context.pos, this.getEncodingMode());
  23866. if (newMode !== this.getEncodingMode()) {
  23867. // Return to ASCII encodation, which will actually handle latch to new mode
  23868. context.signalEncoderChange(ASCII_ENCODATION);
  23869. break;
  23870. }
  23871. }
  23872. const dataCount = buffer.length() - 1;
  23873. const lengthFieldSize = 1;
  23874. const currentSize = context.getCodewordCount() + dataCount + lengthFieldSize;
  23875. context.updateSymbolInfo(currentSize);
  23876. const mustPad = context.getSymbolInfo().getDataCapacity() - currentSize > 0;
  23877. if (context.hasMoreCharacters() || mustPad) {
  23878. if (dataCount <= 249) {
  23879. buffer.setCharAt(0, StringUtils.getCharAt(dataCount));
  23880. }
  23881. else if (dataCount <= 1555) {
  23882. buffer.setCharAt(0, StringUtils.getCharAt(Math.floor(dataCount / 250) + 249));
  23883. buffer.insert(1, StringUtils.getCharAt(dataCount % 250));
  23884. }
  23885. else {
  23886. throw new Error('Message length not in valid ranges: ' + dataCount);
  23887. }
  23888. }
  23889. for (let i = 0, c = buffer.length(); i < c; i++) {
  23890. context.writeCodeword(this.randomize255State(buffer.charAt(i).charCodeAt(0), context.getCodewordCount() + 1));
  23891. }
  23892. }
  23893. randomize255State(ch, codewordPosition) {
  23894. const pseudoRandom = ((149 * codewordPosition) % 255) + 1;
  23895. const tempVariable = ch + pseudoRandom;
  23896. if (tempVariable <= 255) {
  23897. return tempVariable;
  23898. }
  23899. else {
  23900. return tempVariable - 256;
  23901. }
  23902. }
  23903. }
  23904. class C40Encoder {
  23905. getEncodingMode() {
  23906. return C40_ENCODATION;
  23907. }
  23908. encodeMaximal(context) {
  23909. const buffer = new StringBuilder();
  23910. let lastCharSize = 0;
  23911. let backtrackStartPosition = context.pos;
  23912. let backtrackBufferLength = 0;
  23913. while (context.hasMoreCharacters()) {
  23914. const c = context.getCurrentChar();
  23915. context.pos++;
  23916. lastCharSize = this.encodeChar(c, buffer);
  23917. if (buffer.length() % 3 === 0) {
  23918. backtrackStartPosition = context.pos;
  23919. backtrackBufferLength = buffer.length();
  23920. }
  23921. }
  23922. if (backtrackBufferLength !== buffer.length()) {
  23923. const unwritten = Math.floor((buffer.length() / 3) * 2);
  23924. const curCodewordCount = Math.floor(context.getCodewordCount() + unwritten + 1); // +1 for the latch to C40
  23925. context.updateSymbolInfo(curCodewordCount);
  23926. const available = context.getSymbolInfo().getDataCapacity() - curCodewordCount;
  23927. const rest = Math.floor(buffer.length() % 3);
  23928. if ((rest === 2 && available !== 2) ||
  23929. (rest === 1 && (lastCharSize > 3 || available !== 1))) {
  23930. // buffer.setLength(backtrackBufferLength);
  23931. context.pos = backtrackStartPosition;
  23932. }
  23933. }
  23934. if (buffer.length() > 0) {
  23935. context.writeCodeword(LATCH_TO_C40);
  23936. }
  23937. this.handleEOD(context, buffer);
  23938. }
  23939. encode(context) {
  23940. // step C
  23941. const buffer = new StringBuilder();
  23942. while (context.hasMoreCharacters()) {
  23943. const c = context.getCurrentChar();
  23944. context.pos++;
  23945. let lastCharSize = this.encodeChar(c, buffer);
  23946. const unwritten = Math.floor(buffer.length() / 3) * 2;
  23947. const curCodewordCount = context.getCodewordCount() + unwritten;
  23948. context.updateSymbolInfo(curCodewordCount);
  23949. const available = context.getSymbolInfo().getDataCapacity() - curCodewordCount;
  23950. if (!context.hasMoreCharacters()) {
  23951. // Avoid having a single C40 value in the last triplet
  23952. const removed = new StringBuilder();
  23953. if (buffer.length() % 3 === 2 && available !== 2) {
  23954. lastCharSize = this.backtrackOneCharacter(context, buffer, removed, lastCharSize);
  23955. }
  23956. while (buffer.length() % 3 === 1 &&
  23957. (lastCharSize > 3 || available !== 1)) {
  23958. lastCharSize = this.backtrackOneCharacter(context, buffer, removed, lastCharSize);
  23959. }
  23960. break;
  23961. }
  23962. const count = buffer.length();
  23963. if (count % 3 === 0) {
  23964. const newMode = HighLevelEncoder$1.lookAheadTest(context.getMessage(), context.pos, this.getEncodingMode());
  23965. if (newMode !== this.getEncodingMode()) {
  23966. // Return to ASCII encodation, which will actually handle latch to new mode
  23967. context.signalEncoderChange(ASCII_ENCODATION);
  23968. break;
  23969. }
  23970. }
  23971. }
  23972. this.handleEOD(context, buffer);
  23973. }
  23974. backtrackOneCharacter(context, buffer, removed, lastCharSize) {
  23975. const count = buffer.length();
  23976. const test = buffer.toString().substring(0, count - lastCharSize);
  23977. buffer.setLengthToZero();
  23978. buffer.append(test);
  23979. // buffer.delete(count - lastCharSize, count);
  23980. /*for (let i = count - lastCharSize; i < count; i++) {
  23981. buffer.deleteCharAt(i);
  23982. }*/
  23983. context.pos--;
  23984. const c = context.getCurrentChar();
  23985. lastCharSize = this.encodeChar(c, removed);
  23986. context.resetSymbolInfo(); // Deal with possible reduction in symbol size
  23987. return lastCharSize;
  23988. }
  23989. writeNextTriplet(context, buffer) {
  23990. context.writeCodewords(this.encodeToCodewords(buffer.toString()));
  23991. const test = buffer.toString().substring(3);
  23992. buffer.setLengthToZero();
  23993. buffer.append(test);
  23994. // buffer.delete(0, 3);
  23995. /*for (let i = 0; i < 3; i++) {
  23996. buffer.deleteCharAt(i);
  23997. }*/
  23998. }
  23999. /**
  24000. * Handle "end of data" situations
  24001. *
  24002. * @param context the encoder context
  24003. * @param buffer the buffer with the remaining encoded characters
  24004. */
  24005. handleEOD(context, buffer) {
  24006. const unwritten = Math.floor((buffer.length() / 3) * 2);
  24007. const rest = buffer.length() % 3;
  24008. const curCodewordCount = context.getCodewordCount() + unwritten;
  24009. context.updateSymbolInfo(curCodewordCount);
  24010. const available = context.getSymbolInfo().getDataCapacity() - curCodewordCount;
  24011. if (rest === 2) {
  24012. buffer.append('\0'); // Shift 1
  24013. while (buffer.length() >= 3) {
  24014. this.writeNextTriplet(context, buffer);
  24015. }
  24016. if (context.hasMoreCharacters()) {
  24017. context.writeCodeword(C40_UNLATCH);
  24018. }
  24019. }
  24020. else if (available === 1 && rest === 1) {
  24021. while (buffer.length() >= 3) {
  24022. this.writeNextTriplet(context, buffer);
  24023. }
  24024. if (context.hasMoreCharacters()) {
  24025. context.writeCodeword(C40_UNLATCH);
  24026. }
  24027. // else no unlatch
  24028. context.pos--;
  24029. }
  24030. else if (rest === 0) {
  24031. while (buffer.length() >= 3) {
  24032. this.writeNextTriplet(context, buffer);
  24033. }
  24034. if (available > 0 || context.hasMoreCharacters()) {
  24035. context.writeCodeword(C40_UNLATCH);
  24036. }
  24037. }
  24038. else {
  24039. throw new Error('Unexpected case. Please report!');
  24040. }
  24041. context.signalEncoderChange(ASCII_ENCODATION);
  24042. }
  24043. encodeChar(c, sb) {
  24044. if (c === ' '.charCodeAt(0)) {
  24045. sb.append(0o3);
  24046. return 1;
  24047. }
  24048. if (c >= '0'.charCodeAt(0) && c <= '9'.charCodeAt(0)) {
  24049. sb.append(c - 48 + 4);
  24050. return 1;
  24051. }
  24052. if (c >= 'A'.charCodeAt(0) && c <= 'Z'.charCodeAt(0)) {
  24053. sb.append(c - 65 + 14);
  24054. return 1;
  24055. }
  24056. if (c < ' '.charCodeAt(0)) {
  24057. sb.append(0o0); // Shift 1 Set
  24058. sb.append(c);
  24059. return 2;
  24060. }
  24061. if (c <= '/'.charCodeAt(0)) {
  24062. sb.append(0o1); // Shift 2 Set
  24063. sb.append(c - 33);
  24064. return 2;
  24065. }
  24066. if (c <= '@'.charCodeAt(0)) {
  24067. sb.append(0o1); // Shift 2 Set
  24068. sb.append(c - 58 + 15);
  24069. return 2;
  24070. }
  24071. if (c <= '_'.charCodeAt(0)) {
  24072. sb.append(0o1); // Shift 2 Set
  24073. sb.append(c - 91 + 22);
  24074. return 2;
  24075. }
  24076. if (c <= 127) {
  24077. sb.append(0o2); // Shift 3 Set
  24078. sb.append(c - 96);
  24079. return 2;
  24080. }
  24081. sb.append(`${0o1}\u001e`); // Shift 2, Upper Shift
  24082. let len = 2;
  24083. len += this.encodeChar(c - 128, sb);
  24084. return len;
  24085. }
  24086. encodeToCodewords(sb) {
  24087. const v = 1600 * sb.charCodeAt(0) + 40 * sb.charCodeAt(1) + sb.charCodeAt(2) + 1;
  24088. const cw1 = v / 256;
  24089. const cw2 = v % 256;
  24090. const result = new StringBuilder();
  24091. result.append(cw1);
  24092. result.append(cw2);
  24093. return result.toString();
  24094. }
  24095. }
  24096. class EdifactEncoder {
  24097. getEncodingMode() {
  24098. return EDIFACT_ENCODATION;
  24099. }
  24100. encode(context) {
  24101. // step F
  24102. const buffer = new StringBuilder();
  24103. while (context.hasMoreCharacters()) {
  24104. const c = context.getCurrentChar();
  24105. this.encodeChar(c, buffer);
  24106. context.pos++;
  24107. const count = buffer.length();
  24108. if (count >= 4) {
  24109. context.writeCodewords(this.encodeToCodewords(buffer.toString()));
  24110. const test = buffer.toString().substring(4);
  24111. buffer.setLengthToZero();
  24112. buffer.append(test);
  24113. // buffer.delete(0, 4);
  24114. // for (let i = 0; i < 4; i++) {
  24115. // buffer.deleteCharAt(i);
  24116. // }
  24117. const newMode = HighLevelEncoder$1.lookAheadTest(context.getMessage(), context.pos, this.getEncodingMode());
  24118. if (newMode !== this.getEncodingMode()) {
  24119. // Return to ASCII encodation, which will actually handle latch to new mode
  24120. context.signalEncoderChange(ASCII_ENCODATION);
  24121. break;
  24122. }
  24123. }
  24124. }
  24125. buffer.append(StringUtils.getCharAt(31)); // Unlatch
  24126. this.handleEOD(context, buffer);
  24127. }
  24128. /**
  24129. * Handle "end of data" situations
  24130. *
  24131. * @param context the encoder context
  24132. * @param buffer the buffer with the remaining encoded characters
  24133. */
  24134. handleEOD(context, buffer) {
  24135. try {
  24136. const count = buffer.length();
  24137. if (count === 0) {
  24138. return; // Already finished
  24139. }
  24140. if (count === 1) {
  24141. // Only an unlatch at the end
  24142. context.updateSymbolInfo();
  24143. let available = context.getSymbolInfo().getDataCapacity() -
  24144. context.getCodewordCount();
  24145. const remaining = context.getRemainingCharacters();
  24146. // The following two lines are a hack inspired by the 'fix' from https://sourceforge.net/p/barcode4j/svn/221/
  24147. if (remaining > available) {
  24148. context.updateSymbolInfo(context.getCodewordCount() + 1);
  24149. available =
  24150. context.getSymbolInfo().getDataCapacity() -
  24151. context.getCodewordCount();
  24152. }
  24153. if (remaining <= available && available <= 2) {
  24154. return; // No unlatch
  24155. }
  24156. }
  24157. if (count > 4) {
  24158. throw new Error('Count must not exceed 4');
  24159. }
  24160. const restChars = count - 1;
  24161. const encoded = this.encodeToCodewords(buffer.toString());
  24162. const endOfSymbolReached = !context.hasMoreCharacters();
  24163. let restInAscii = endOfSymbolReached && restChars <= 2;
  24164. if (restChars <= 2) {
  24165. context.updateSymbolInfo(context.getCodewordCount() + restChars);
  24166. const available = context.getSymbolInfo().getDataCapacity() -
  24167. context.getCodewordCount();
  24168. if (available >= 3) {
  24169. restInAscii = false;
  24170. context.updateSymbolInfo(context.getCodewordCount() + encoded.length);
  24171. // available = context.symbolInfo.dataCapacity - context.getCodewordCount();
  24172. }
  24173. }
  24174. if (restInAscii) {
  24175. context.resetSymbolInfo();
  24176. context.pos -= restChars;
  24177. }
  24178. else {
  24179. context.writeCodewords(encoded);
  24180. }
  24181. }
  24182. finally {
  24183. context.signalEncoderChange(ASCII_ENCODATION);
  24184. }
  24185. }
  24186. encodeChar(c, sb) {
  24187. if (c >= ' '.charCodeAt(0) && c <= '?'.charCodeAt(0)) {
  24188. sb.append(c);
  24189. }
  24190. else if (c >= '@'.charCodeAt(0) && c <= '^'.charCodeAt(0)) {
  24191. sb.append(StringUtils.getCharAt(c - 64));
  24192. }
  24193. else {
  24194. HighLevelEncoder$1.illegalCharacter(StringUtils.getCharAt(c));
  24195. }
  24196. }
  24197. encodeToCodewords(sb) {
  24198. const len = sb.length;
  24199. if (len === 0) {
  24200. throw new Error('StringBuilder must not be empty');
  24201. }
  24202. const c1 = sb.charAt(0).charCodeAt(0);
  24203. const c2 = len >= 2 ? sb.charAt(1).charCodeAt(0) : 0;
  24204. const c3 = len >= 3 ? sb.charAt(2).charCodeAt(0) : 0;
  24205. const c4 = len >= 4 ? sb.charAt(3).charCodeAt(0) : 0;
  24206. const v = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4;
  24207. const cw1 = (v >> 16) & 255;
  24208. const cw2 = (v >> 8) & 255;
  24209. const cw3 = v & 255;
  24210. const res = new StringBuilder();
  24211. res.append(cw1);
  24212. if (len >= 2) {
  24213. res.append(cw2);
  24214. }
  24215. if (len >= 3) {
  24216. res.append(cw3);
  24217. }
  24218. return res.toString();
  24219. }
  24220. }
  24221. /**
  24222. * Symbol info table for DataMatrix.
  24223. */
  24224. class SymbolInfo {
  24225. constructor(rectangular, dataCapacity, errorCodewords, matrixWidth, matrixHeight, dataRegions, rsBlockData = 0, rsBlockError = 0) {
  24226. this.rectangular = rectangular;
  24227. this.dataCapacity = dataCapacity;
  24228. this.errorCodewords = errorCodewords;
  24229. this.matrixWidth = matrixWidth;
  24230. this.matrixHeight = matrixHeight;
  24231. this.dataRegions = dataRegions;
  24232. this.rsBlockData = rsBlockData;
  24233. this.rsBlockError = rsBlockError;
  24234. }
  24235. static lookup(dataCodewords, shape = 0 /* FORCE_NONE */, minSize = null, maxSize = null, fail = true) {
  24236. for (const symbol of PROD_SYMBOLS) {
  24237. if (shape === 1 /* FORCE_SQUARE */ && symbol.rectangular) {
  24238. continue;
  24239. }
  24240. if (shape === 2 /* FORCE_RECTANGLE */ && !symbol.rectangular) {
  24241. continue;
  24242. }
  24243. if (minSize != null &&
  24244. (symbol.getSymbolWidth() < minSize.getWidth() ||
  24245. symbol.getSymbolHeight() < minSize.getHeight())) {
  24246. continue;
  24247. }
  24248. if (maxSize != null &&
  24249. (symbol.getSymbolWidth() > maxSize.getWidth() ||
  24250. symbol.getSymbolHeight() > maxSize.getHeight())) {
  24251. continue;
  24252. }
  24253. if (dataCodewords <= symbol.dataCapacity) {
  24254. return symbol;
  24255. }
  24256. }
  24257. if (fail) {
  24258. throw new Error("Can't find a symbol arrangement that matches the message. Data codewords: " +
  24259. dataCodewords);
  24260. }
  24261. return null;
  24262. }
  24263. getHorizontalDataRegions() {
  24264. switch (this.dataRegions) {
  24265. case 1:
  24266. return 1;
  24267. case 2:
  24268. case 4:
  24269. return 2;
  24270. case 16:
  24271. return 4;
  24272. case 36:
  24273. return 6;
  24274. default:
  24275. throw new Error('Cannot handle this number of data regions');
  24276. }
  24277. }
  24278. getVerticalDataRegions() {
  24279. switch (this.dataRegions) {
  24280. case 1:
  24281. case 2:
  24282. return 1;
  24283. case 4:
  24284. return 2;
  24285. case 16:
  24286. return 4;
  24287. case 36:
  24288. return 6;
  24289. default:
  24290. throw new Error('Cannot handle this number of data regions');
  24291. }
  24292. }
  24293. getSymbolDataWidth() {
  24294. return this.getHorizontalDataRegions() * this.matrixWidth;
  24295. }
  24296. getSymbolDataHeight() {
  24297. return this.getVerticalDataRegions() * this.matrixHeight;
  24298. }
  24299. getSymbolWidth() {
  24300. return this.getSymbolDataWidth() + this.getHorizontalDataRegions() * 2;
  24301. }
  24302. getSymbolHeight() {
  24303. return this.getSymbolDataHeight() + this.getVerticalDataRegions() * 2;
  24304. }
  24305. getCodewordCount() {
  24306. return this.dataCapacity + this.errorCodewords;
  24307. }
  24308. getInterleavedBlockCount() {
  24309. if (!this.rsBlockData)
  24310. return 1;
  24311. return this.dataCapacity / this.rsBlockData;
  24312. }
  24313. getDataCapacity() {
  24314. return this.dataCapacity;
  24315. }
  24316. getErrorCodewords() {
  24317. return this.errorCodewords;
  24318. }
  24319. getDataLengthForInterleavedBlock(index) {
  24320. return this.rsBlockData;
  24321. }
  24322. getErrorLengthForInterleavedBlock(index) {
  24323. return this.rsBlockError;
  24324. }
  24325. }
  24326. class DataMatrixSymbolInfo144 extends SymbolInfo {
  24327. constructor() {
  24328. super(false, 1558, 620, 22, 22, 36, -1, 62);
  24329. }
  24330. getInterleavedBlockCount() {
  24331. return 10;
  24332. }
  24333. getDataLengthForInterleavedBlock(index) {
  24334. return index <= 8 ? 156 : 155;
  24335. }
  24336. }
  24337. const PROD_SYMBOLS = [
  24338. new SymbolInfo(false, 3, 5, 8, 8, 1),
  24339. new SymbolInfo(false, 5, 7, 10, 10, 1),
  24340. /*rect*/ new SymbolInfo(true, 5, 7, 16, 6, 1),
  24341. new SymbolInfo(false, 8, 10, 12, 12, 1),
  24342. /*rect*/ new SymbolInfo(true, 10, 11, 14, 6, 2),
  24343. new SymbolInfo(false, 12, 12, 14, 14, 1),
  24344. /*rect*/ new SymbolInfo(true, 16, 14, 24, 10, 1),
  24345. new SymbolInfo(false, 18, 14, 16, 16, 1),
  24346. new SymbolInfo(false, 22, 18, 18, 18, 1),
  24347. /*rect*/ new SymbolInfo(true, 22, 18, 16, 10, 2),
  24348. new SymbolInfo(false, 30, 20, 20, 20, 1),
  24349. /*rect*/ new SymbolInfo(true, 32, 24, 16, 14, 2),
  24350. new SymbolInfo(false, 36, 24, 22, 22, 1),
  24351. new SymbolInfo(false, 44, 28, 24, 24, 1),
  24352. /*rect*/ new SymbolInfo(true, 49, 28, 22, 14, 2),
  24353. new SymbolInfo(false, 62, 36, 14, 14, 4),
  24354. new SymbolInfo(false, 86, 42, 16, 16, 4),
  24355. new SymbolInfo(false, 114, 48, 18, 18, 4),
  24356. new SymbolInfo(false, 144, 56, 20, 20, 4),
  24357. new SymbolInfo(false, 174, 68, 22, 22, 4),
  24358. new SymbolInfo(false, 204, 84, 24, 24, 4, 102, 42),
  24359. new SymbolInfo(false, 280, 112, 14, 14, 16, 140, 56),
  24360. new SymbolInfo(false, 368, 144, 16, 16, 16, 92, 36),
  24361. new SymbolInfo(false, 456, 192, 18, 18, 16, 114, 48),
  24362. new SymbolInfo(false, 576, 224, 20, 20, 16, 144, 56),
  24363. new SymbolInfo(false, 696, 272, 22, 22, 16, 174, 68),
  24364. new SymbolInfo(false, 816, 336, 24, 24, 16, 136, 56),
  24365. new SymbolInfo(false, 1050, 408, 18, 18, 36, 175, 68),
  24366. new SymbolInfo(false, 1304, 496, 20, 20, 36, 163, 62),
  24367. new DataMatrixSymbolInfo144(),
  24368. ];
  24369. class EncoderContext {
  24370. constructor(msg) {
  24371. this.msg = msg;
  24372. this.pos = 0;
  24373. this.skipAtEnd = 0;
  24374. // From this point on Strings are not Unicode anymore!
  24375. const msgBinary = msg.split('').map(c => c.charCodeAt(0));
  24376. const sb = new StringBuilder();
  24377. for (let i = 0, c = msgBinary.length; i < c; i++) {
  24378. const ch = String.fromCharCode(msgBinary[i] & 0xff);
  24379. if (ch === '?' && msg.charAt(i) !== '?') {
  24380. throw new Error('Message contains characters outside ISO-8859-1 encoding.');
  24381. }
  24382. sb.append(ch);
  24383. }
  24384. this.msg = sb.toString(); // Not Unicode here!
  24385. this.shape = 0 /* FORCE_NONE */;
  24386. this.codewords = new StringBuilder();
  24387. this.newEncoding = -1;
  24388. }
  24389. setSymbolShape(shape) {
  24390. this.shape = shape;
  24391. }
  24392. setSizeConstraints(minSize, maxSize) {
  24393. this.minSize = minSize;
  24394. this.maxSize = maxSize;
  24395. }
  24396. getMessage() {
  24397. return this.msg;
  24398. }
  24399. setSkipAtEnd(count) {
  24400. this.skipAtEnd = count;
  24401. }
  24402. getCurrentChar() {
  24403. return this.msg.charCodeAt(this.pos);
  24404. }
  24405. getCurrent() {
  24406. return this.msg.charCodeAt(this.pos);
  24407. }
  24408. getCodewords() {
  24409. return this.codewords;
  24410. }
  24411. writeCodewords(codewords) {
  24412. this.codewords.append(codewords);
  24413. }
  24414. writeCodeword(codeword) {
  24415. this.codewords.append(codeword);
  24416. }
  24417. getCodewordCount() {
  24418. return this.codewords.length();
  24419. }
  24420. getNewEncoding() {
  24421. return this.newEncoding;
  24422. }
  24423. signalEncoderChange(encoding) {
  24424. this.newEncoding = encoding;
  24425. }
  24426. resetEncoderSignal() {
  24427. this.newEncoding = -1;
  24428. }
  24429. hasMoreCharacters() {
  24430. return this.pos < this.getTotalMessageCharCount();
  24431. }
  24432. getTotalMessageCharCount() {
  24433. return this.msg.length - this.skipAtEnd;
  24434. }
  24435. getRemainingCharacters() {
  24436. return this.getTotalMessageCharCount() - this.pos;
  24437. }
  24438. getSymbolInfo() {
  24439. return this.symbolInfo;
  24440. }
  24441. updateSymbolInfo(len = this.getCodewordCount()) {
  24442. if (this.symbolInfo == null || len > this.symbolInfo.getDataCapacity()) {
  24443. this.symbolInfo = SymbolInfo.lookup(len, this.shape, this.minSize, this.maxSize, true);
  24444. }
  24445. }
  24446. resetSymbolInfo() {
  24447. this.symbolInfo = null;
  24448. }
  24449. }
  24450. class X12Encoder extends C40Encoder {
  24451. getEncodingMode() {
  24452. return X12_ENCODATION;
  24453. }
  24454. encode(context) {
  24455. // step C
  24456. const buffer = new StringBuilder();
  24457. while (context.hasMoreCharacters()) {
  24458. const c = context.getCurrentChar();
  24459. context.pos++;
  24460. this.encodeChar(c, buffer);
  24461. const count = buffer.length();
  24462. if (count % 3 === 0) {
  24463. this.writeNextTriplet(context, buffer);
  24464. const newMode = HighLevelEncoder$1.lookAheadTest(context.getMessage(), context.pos, this.getEncodingMode());
  24465. if (newMode !== this.getEncodingMode()) {
  24466. // Return to ASCII encodation, which will actually handle latch to new mode
  24467. context.signalEncoderChange(ASCII_ENCODATION);
  24468. break;
  24469. }
  24470. }
  24471. }
  24472. this.handleEOD(context, buffer);
  24473. }
  24474. encodeChar(c, sb) {
  24475. switch (c) {
  24476. case 13: // CR (Carriage return)
  24477. sb.append(0o0);
  24478. break;
  24479. case '*'.charCodeAt(0):
  24480. sb.append(0o1);
  24481. break;
  24482. case '>'.charCodeAt(0):
  24483. sb.append(0o2);
  24484. break;
  24485. case ' '.charCodeAt(0):
  24486. sb.append(0o3);
  24487. break;
  24488. default:
  24489. if (c >= '0'.charCodeAt(0) && c <= '9'.charCodeAt(0)) {
  24490. sb.append(c - 48 + 4);
  24491. }
  24492. else if (c >= 'A'.charCodeAt(0) && c <= 'Z'.charCodeAt(0)) {
  24493. sb.append(c - 65 + 14);
  24494. }
  24495. else {
  24496. HighLevelEncoder$1.illegalCharacter(StringUtils.getCharAt(c));
  24497. }
  24498. break;
  24499. }
  24500. return 1;
  24501. }
  24502. handleEOD(context, buffer) {
  24503. context.updateSymbolInfo();
  24504. const available = context.getSymbolInfo().getDataCapacity() - context.getCodewordCount();
  24505. const count = buffer.length();
  24506. context.pos -= count;
  24507. if (context.getRemainingCharacters() > 1 ||
  24508. available > 1 ||
  24509. context.getRemainingCharacters() !== available) {
  24510. context.writeCodeword(X12_UNLATCH);
  24511. }
  24512. if (context.getNewEncoding() < 0) {
  24513. context.signalEncoderChange(ASCII_ENCODATION);
  24514. }
  24515. }
  24516. }
  24517. class TextEncoder$1 extends C40Encoder {
  24518. getEncodingMode() {
  24519. return TEXT_ENCODATION;
  24520. }
  24521. encodeChar(c, sb) {
  24522. if (c === ' '.charCodeAt(0)) {
  24523. sb.append(0o3);
  24524. return 1;
  24525. }
  24526. if (c >= '0'.charCodeAt(0) && c <= '9'.charCodeAt(0)) {
  24527. sb.append(c - 48 + 4);
  24528. return 1;
  24529. }
  24530. if (c >= 'a'.charCodeAt(0) && c <= 'z'.charCodeAt(0)) {
  24531. sb.append(c - 97 + 14);
  24532. return 1;
  24533. }
  24534. if (c < ' '.charCodeAt(0)) {
  24535. sb.append(0o0); // Shift 1 Set
  24536. sb.append(c);
  24537. return 2;
  24538. }
  24539. if (c <= '/'.charCodeAt(0)) {
  24540. sb.append(0o1); // Shift 2 Set
  24541. sb.append(c - 33);
  24542. return 2;
  24543. }
  24544. if (c <= '@'.charCodeAt(0)) {
  24545. sb.append(0o1); // Shift 2 Set
  24546. sb.append(c - 58 + 15);
  24547. return 2;
  24548. }
  24549. if (c >= '['.charCodeAt(0) && c <= '_'.charCodeAt(0)) {
  24550. sb.append(0o1); // Shift 2 Set
  24551. sb.append(c - 91 + 22);
  24552. return 2;
  24553. }
  24554. if (c === '`'.charCodeAt(0)) {
  24555. sb.append(0o2); // Shift 3 Set
  24556. sb.append(0); // '`' - 96 == 0
  24557. return 2;
  24558. }
  24559. if (c <= 'Z'.charCodeAt(0)) {
  24560. sb.append(0o2); // Shift 3 Set
  24561. sb.append(c - 65 + 1);
  24562. return 2;
  24563. }
  24564. if (c <= 127) {
  24565. sb.append(0o2); // Shift 3 Set
  24566. sb.append(c - 123 + 27);
  24567. return 2;
  24568. }
  24569. sb.append(`${0o1}\u001e`); // Shift 2, Upper Shift
  24570. let len = 2;
  24571. len += this.encodeChar(c - 128, sb);
  24572. return len;
  24573. }
  24574. }
  24575. // tslint:disable-next-line:no-circular-imports
  24576. /**
  24577. * DataMatrix ECC 200 data encoder following the algorithm described in ISO/IEC 16022:200(E) in
  24578. * annex S.
  24579. */
  24580. class HighLevelEncoder$1 {
  24581. static randomize253State(codewordPosition) {
  24582. const pseudoRandom = ((149 * codewordPosition) % 253) + 1;
  24583. const tempVariable = PAD + pseudoRandom;
  24584. return tempVariable <= 254 ? tempVariable : tempVariable - 254;
  24585. }
  24586. /**
  24587. * Performs message encoding of a DataMatrix message using the algorithm described in annex P
  24588. * of ISO/IEC 16022:2000(E).
  24589. *
  24590. * @param msg the message
  24591. * @param shape requested shape. May be {@code SymbolShapeHint.FORCE_NONE},
  24592. * {@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}.
  24593. * @param minSize the minimum symbol size constraint or null for no constraint
  24594. * @param maxSize the maximum symbol size constraint or null for no constraint
  24595. * @param forceC40 enforce C40 encoding
  24596. * @return the encoded message (the char values range from 0 to 255)
  24597. */
  24598. static encodeHighLevel(msg, shape = 0 /* FORCE_NONE */, minSize = null, maxSize = null, forceC40 = false) {
  24599. // the codewords 0..255 are encoded as Unicode characters
  24600. const c40Encoder = new C40Encoder();
  24601. const encoders = [
  24602. new ASCIIEncoder(),
  24603. c40Encoder,
  24604. new TextEncoder$1(),
  24605. new X12Encoder(),
  24606. new EdifactEncoder(),
  24607. new Base256Encoder(),
  24608. ];
  24609. const context = new EncoderContext(msg);
  24610. context.setSymbolShape(shape);
  24611. context.setSizeConstraints(minSize, maxSize);
  24612. if (msg.startsWith(MACRO_05_HEADER) && msg.endsWith(MACRO_TRAILER)) {
  24613. context.writeCodeword(MACRO_05);
  24614. context.setSkipAtEnd(2);
  24615. context.pos += MACRO_05_HEADER.length;
  24616. }
  24617. else if (msg.startsWith(MACRO_06_HEADER) && msg.endsWith(MACRO_TRAILER)) {
  24618. context.writeCodeword(MACRO_06);
  24619. context.setSkipAtEnd(2);
  24620. context.pos += MACRO_06_HEADER.length;
  24621. }
  24622. let encodingMode = ASCII_ENCODATION; // Default mode
  24623. if (forceC40) {
  24624. c40Encoder.encodeMaximal(context);
  24625. encodingMode = context.getNewEncoding();
  24626. context.resetEncoderSignal();
  24627. }
  24628. while (context.hasMoreCharacters()) {
  24629. encoders[encodingMode].encode(context);
  24630. if (context.getNewEncoding() >= 0) {
  24631. encodingMode = context.getNewEncoding();
  24632. context.resetEncoderSignal();
  24633. }
  24634. }
  24635. const len = context.getCodewordCount();
  24636. context.updateSymbolInfo();
  24637. const capacity = context.getSymbolInfo().getDataCapacity();
  24638. if (len < capacity &&
  24639. encodingMode !== ASCII_ENCODATION &&
  24640. encodingMode !== BASE256_ENCODATION &&
  24641. encodingMode !== EDIFACT_ENCODATION) {
  24642. context.writeCodeword('\u00fe'); // Unlatch (254)
  24643. }
  24644. // Padding
  24645. const codewords = context.getCodewords();
  24646. if (codewords.length() < capacity) {
  24647. codewords.append(PAD);
  24648. }
  24649. while (codewords.length() < capacity) {
  24650. codewords.append(this.randomize253State(codewords.length() + 1));
  24651. }
  24652. return context.getCodewords().toString();
  24653. }
  24654. static lookAheadTest(msg, startpos, currentMode) {
  24655. const newMode = this.lookAheadTestIntern(msg, startpos, currentMode);
  24656. if (currentMode === X12_ENCODATION && newMode === X12_ENCODATION) {
  24657. const endpos = Math.min(startpos + 3, msg.length);
  24658. for (let i = startpos; i < endpos; i++) {
  24659. if (!this.isNativeX12(msg.charCodeAt(i))) {
  24660. return ASCII_ENCODATION;
  24661. }
  24662. }
  24663. }
  24664. else if (currentMode === EDIFACT_ENCODATION &&
  24665. newMode === EDIFACT_ENCODATION) {
  24666. const endpos = Math.min(startpos + 4, msg.length);
  24667. for (let i = startpos; i < endpos; i++) {
  24668. if (!this.isNativeEDIFACT(msg.charCodeAt(i))) {
  24669. return ASCII_ENCODATION;
  24670. }
  24671. }
  24672. }
  24673. return newMode;
  24674. }
  24675. static lookAheadTestIntern(msg, startpos, currentMode) {
  24676. if (startpos >= msg.length) {
  24677. return currentMode;
  24678. }
  24679. let charCounts;
  24680. // step J
  24681. if (currentMode === ASCII_ENCODATION) {
  24682. charCounts = [0, 1, 1, 1, 1, 1.25];
  24683. }
  24684. else {
  24685. charCounts = [1, 2, 2, 2, 2, 2.25];
  24686. charCounts[currentMode] = 0;
  24687. }
  24688. let charsProcessed = 0;
  24689. const mins = new Uint8Array(6);
  24690. const intCharCounts = [];
  24691. while (true) {
  24692. // step K
  24693. if (startpos + charsProcessed === msg.length) {
  24694. Arrays.fill(mins, 0);
  24695. Arrays.fill(intCharCounts, 0);
  24696. const min = this.findMinimums(charCounts, intCharCounts, Integer.MAX_VALUE, mins);
  24697. const minCount = this.getMinimumCount(mins);
  24698. if (intCharCounts[ASCII_ENCODATION] === min) {
  24699. return ASCII_ENCODATION;
  24700. }
  24701. if (minCount === 1) {
  24702. if (mins[BASE256_ENCODATION] > 0) {
  24703. return BASE256_ENCODATION;
  24704. }
  24705. if (mins[EDIFACT_ENCODATION] > 0) {
  24706. return EDIFACT_ENCODATION;
  24707. }
  24708. if (mins[TEXT_ENCODATION] > 0) {
  24709. return TEXT_ENCODATION;
  24710. }
  24711. if (mins[X12_ENCODATION] > 0) {
  24712. return X12_ENCODATION;
  24713. }
  24714. }
  24715. return C40_ENCODATION;
  24716. }
  24717. const c = msg.charCodeAt(startpos + charsProcessed);
  24718. charsProcessed++;
  24719. // step L
  24720. if (this.isDigit(c)) {
  24721. charCounts[ASCII_ENCODATION] += 0.5;
  24722. }
  24723. else if (this.isExtendedASCII(c)) {
  24724. charCounts[ASCII_ENCODATION] = Math.ceil(charCounts[ASCII_ENCODATION]);
  24725. charCounts[ASCII_ENCODATION] += 2.0;
  24726. }
  24727. else {
  24728. charCounts[ASCII_ENCODATION] = Math.ceil(charCounts[ASCII_ENCODATION]);
  24729. charCounts[ASCII_ENCODATION]++;
  24730. }
  24731. // step M
  24732. if (this.isNativeC40(c)) {
  24733. charCounts[C40_ENCODATION] += 2.0 / 3.0;
  24734. }
  24735. else if (this.isExtendedASCII(c)) {
  24736. charCounts[C40_ENCODATION] += 8.0 / 3.0;
  24737. }
  24738. else {
  24739. charCounts[C40_ENCODATION] += 4.0 / 3.0;
  24740. }
  24741. // step N
  24742. if (this.isNativeText(c)) {
  24743. charCounts[TEXT_ENCODATION] += 2.0 / 3.0;
  24744. }
  24745. else if (this.isExtendedASCII(c)) {
  24746. charCounts[TEXT_ENCODATION] += 8.0 / 3.0;
  24747. }
  24748. else {
  24749. charCounts[TEXT_ENCODATION] += 4.0 / 3.0;
  24750. }
  24751. // step O
  24752. if (this.isNativeX12(c)) {
  24753. charCounts[X12_ENCODATION] += 2.0 / 3.0;
  24754. }
  24755. else if (this.isExtendedASCII(c)) {
  24756. charCounts[X12_ENCODATION] += 13.0 / 3.0;
  24757. }
  24758. else {
  24759. charCounts[X12_ENCODATION] += 10.0 / 3.0;
  24760. }
  24761. // step P
  24762. if (this.isNativeEDIFACT(c)) {
  24763. charCounts[EDIFACT_ENCODATION] += 3.0 / 4.0;
  24764. }
  24765. else if (this.isExtendedASCII(c)) {
  24766. charCounts[EDIFACT_ENCODATION] += 17.0 / 4.0;
  24767. }
  24768. else {
  24769. charCounts[EDIFACT_ENCODATION] += 13.0 / 4.0;
  24770. }
  24771. // step Q
  24772. if (this.isSpecialB256(c)) {
  24773. charCounts[BASE256_ENCODATION] += 4.0;
  24774. }
  24775. else {
  24776. charCounts[BASE256_ENCODATION]++;
  24777. }
  24778. // step R
  24779. if (charsProcessed >= 4) {
  24780. Arrays.fill(mins, 0);
  24781. Arrays.fill(intCharCounts, 0);
  24782. this.findMinimums(charCounts, intCharCounts, Integer.MAX_VALUE, mins);
  24783. if (intCharCounts[ASCII_ENCODATION] <
  24784. this.min(intCharCounts[BASE256_ENCODATION], intCharCounts[C40_ENCODATION], intCharCounts[TEXT_ENCODATION], intCharCounts[X12_ENCODATION], intCharCounts[EDIFACT_ENCODATION])) {
  24785. return ASCII_ENCODATION;
  24786. }
  24787. if (intCharCounts[BASE256_ENCODATION] < intCharCounts[ASCII_ENCODATION] ||
  24788. intCharCounts[BASE256_ENCODATION] + 1 <
  24789. this.min(intCharCounts[C40_ENCODATION], intCharCounts[TEXT_ENCODATION], intCharCounts[X12_ENCODATION], intCharCounts[EDIFACT_ENCODATION])) {
  24790. return BASE256_ENCODATION;
  24791. }
  24792. if (intCharCounts[EDIFACT_ENCODATION] + 1 <
  24793. this.min(intCharCounts[BASE256_ENCODATION], intCharCounts[C40_ENCODATION], intCharCounts[TEXT_ENCODATION], intCharCounts[X12_ENCODATION], intCharCounts[ASCII_ENCODATION])) {
  24794. return EDIFACT_ENCODATION;
  24795. }
  24796. if (intCharCounts[TEXT_ENCODATION] + 1 <
  24797. this.min(intCharCounts[BASE256_ENCODATION], intCharCounts[C40_ENCODATION], intCharCounts[EDIFACT_ENCODATION], intCharCounts[X12_ENCODATION], intCharCounts[ASCII_ENCODATION])) {
  24798. return TEXT_ENCODATION;
  24799. }
  24800. if (intCharCounts[X12_ENCODATION] + 1 <
  24801. this.min(intCharCounts[BASE256_ENCODATION], intCharCounts[C40_ENCODATION], intCharCounts[EDIFACT_ENCODATION], intCharCounts[TEXT_ENCODATION], intCharCounts[ASCII_ENCODATION])) {
  24802. return X12_ENCODATION;
  24803. }
  24804. if (intCharCounts[C40_ENCODATION] + 1 <
  24805. this.min(intCharCounts[ASCII_ENCODATION], intCharCounts[BASE256_ENCODATION], intCharCounts[EDIFACT_ENCODATION], intCharCounts[TEXT_ENCODATION])) {
  24806. if (intCharCounts[C40_ENCODATION] < intCharCounts[X12_ENCODATION]) {
  24807. return C40_ENCODATION;
  24808. }
  24809. if (intCharCounts[C40_ENCODATION] === intCharCounts[X12_ENCODATION]) {
  24810. let p = startpos + charsProcessed + 1;
  24811. while (p < msg.length) {
  24812. const tc = msg.charCodeAt(p);
  24813. if (this.isX12TermSep(tc)) {
  24814. return X12_ENCODATION;
  24815. }
  24816. if (!this.isNativeX12(tc)) {
  24817. break;
  24818. }
  24819. p++;
  24820. }
  24821. return C40_ENCODATION;
  24822. }
  24823. }
  24824. }
  24825. }
  24826. }
  24827. static min(f1, f2, f3, f4, f5) {
  24828. const val = Math.min(f1, Math.min(f2, Math.min(f3, f4)));
  24829. if (f5 === undefined) {
  24830. return val;
  24831. }
  24832. else {
  24833. return Math.min(val, f5);
  24834. }
  24835. }
  24836. static findMinimums(charCounts, intCharCounts, min, mins) {
  24837. for (let i = 0; i < 6; i++) {
  24838. const current = (intCharCounts[i] = Math.ceil(charCounts[i]));
  24839. if (min > current) {
  24840. min = current;
  24841. Arrays.fill(mins, 0);
  24842. }
  24843. if (min === current) {
  24844. mins[i] = mins[i] + 1;
  24845. }
  24846. }
  24847. return min;
  24848. }
  24849. static getMinimumCount(mins) {
  24850. let minCount = 0;
  24851. for (let i = 0; i < 6; i++) {
  24852. minCount += mins[i];
  24853. }
  24854. return minCount || 0;
  24855. }
  24856. static isDigit(ch) {
  24857. return ch >= '0'.charCodeAt(0) && ch <= '9'.charCodeAt(0);
  24858. }
  24859. static isExtendedASCII(ch) {
  24860. return ch >= 128 && ch <= 255;
  24861. }
  24862. static isNativeC40(ch) {
  24863. return (ch === ' '.charCodeAt(0) ||
  24864. (ch >= '0'.charCodeAt(0) && ch <= '9'.charCodeAt(0)) ||
  24865. (ch >= 'A'.charCodeAt(0) && ch <= 'Z'.charCodeAt(0)));
  24866. }
  24867. static isNativeText(ch) {
  24868. return (ch === ' '.charCodeAt(0) ||
  24869. (ch >= '0'.charCodeAt(0) && ch <= '9'.charCodeAt(0)) ||
  24870. (ch >= 'a'.charCodeAt(0) && ch <= 'z'.charCodeAt(0)));
  24871. }
  24872. static isNativeX12(ch) {
  24873. return (this.isX12TermSep(ch) ||
  24874. ch === ' '.charCodeAt(0) ||
  24875. (ch >= '0'.charCodeAt(0) && ch <= '9'.charCodeAt(0)) ||
  24876. (ch >= 'A'.charCodeAt(0) && ch <= 'Z'.charCodeAt(0)));
  24877. }
  24878. static isX12TermSep(ch) {
  24879. return (ch === 13 || // CR
  24880. ch === '*'.charCodeAt(0) ||
  24881. ch === '>'.charCodeAt(0));
  24882. }
  24883. static isNativeEDIFACT(ch) {
  24884. return ch >= ' '.charCodeAt(0) && ch <= '^'.charCodeAt(0);
  24885. }
  24886. static isSpecialB256(ch) {
  24887. return false; // TODO NOT IMPLEMENTED YET!!!
  24888. }
  24889. /**
  24890. * Determines the number of consecutive characters that are encodable using numeric compaction.
  24891. *
  24892. * @param msg the message
  24893. * @param startpos the start position within the message
  24894. * @return the requested character count
  24895. */
  24896. static determineConsecutiveDigitCount(msg, startpos = 0) {
  24897. const len = msg.length;
  24898. let idx = startpos;
  24899. while (idx < len && this.isDigit(msg.charCodeAt(idx))) {
  24900. idx++;
  24901. }
  24902. return idx - startpos;
  24903. }
  24904. static illegalCharacter(singleCharacter) {
  24905. let hex = Integer.toHexString(singleCharacter.charCodeAt(0));
  24906. hex = '0000'.substring(0, 4 - hex.length) + hex;
  24907. throw new Error('Illegal character: ' + singleCharacter + ' (0x' + hex + ')');
  24908. }
  24909. }
  24910. /**
  24911. * Set of CharsetEncoders for a given input string
  24912. *
  24913. * Invariants:
  24914. * - The list contains only encoders from CharacterSetECI (list is shorter then the list of encoders available on
  24915. * the platform for which ECI values are defined).
  24916. * - The list contains encoders at least one encoder for every character in the input.
  24917. * - The first encoder in the list is always the ISO-8859-1 encoder even of no character in the input can be encoded
  24918. * by it.
  24919. * - If the input contains a character that is not in ISO-8859-1 then the last two entries in the list will be the
  24920. * UTF-8 encoder and the UTF-16BE encoder.
  24921. *
  24922. * @author Alex Geller
  24923. */
  24924. class CharsetEncoder {
  24925. constructor(charset) {
  24926. this.charset = charset;
  24927. this.name = charset.name;
  24928. }
  24929. canEncode(c) {
  24930. try {
  24931. return StringEncoding.encode(c, this.charset) != null;
  24932. }
  24933. catch (ex) {
  24934. return false;
  24935. }
  24936. }
  24937. }
  24938. class ECIEncoderSet {
  24939. /**
  24940. * Constructs an encoder set
  24941. *
  24942. * @param stringToEncode the string that needs to be encoded
  24943. * @param priorityCharset The preferred {@link Charset} or null.
  24944. * @param fnc1 fnc1 denotes the character in the input that represents the FNC1 character or -1 for a non-GS1 bar
  24945. * code. When specified, it is considered an error to pass it as argument to the methods canEncode() or encode().
  24946. */
  24947. constructor(stringToEncode, priorityCharset, fnc1) {
  24948. this.ENCODERS = [
  24949. 'IBM437',
  24950. 'ISO-8859-2',
  24951. 'ISO-8859-3',
  24952. 'ISO-8859-4',
  24953. 'ISO-8859-5',
  24954. 'ISO-8859-6',
  24955. 'ISO-8859-7',
  24956. 'ISO-8859-8',
  24957. 'ISO-8859-9',
  24958. 'ISO-8859-10',
  24959. 'ISO-8859-11',
  24960. 'ISO-8859-13',
  24961. 'ISO-8859-14',
  24962. 'ISO-8859-15',
  24963. 'ISO-8859-16',
  24964. 'windows-1250',
  24965. 'windows-1251',
  24966. 'windows-1252',
  24967. 'windows-1256',
  24968. 'Shift_JIS',
  24969. ].map(name => new CharsetEncoder(Charset.forName(name)));
  24970. this.encoders = [];
  24971. const neededEncoders = [];
  24972. // we always need the ISO-8859-1 encoder. It is the default encoding
  24973. neededEncoders.push(new CharsetEncoder(StandardCharsets.ISO_8859_1));
  24974. let needUnicodeEncoder = priorityCharset != null && priorityCharset.name.startsWith('UTF');
  24975. // Walk over the input string and see if all characters can be encoded with the list of encoders
  24976. for (let i = 0; i < stringToEncode.length; i++) {
  24977. let canEncode = false;
  24978. for (const encoder of neededEncoders) {
  24979. const singleCharacter = stringToEncode.charAt(i);
  24980. const c = singleCharacter.charCodeAt(0);
  24981. if (c === fnc1 || encoder.canEncode(singleCharacter)) {
  24982. canEncode = true;
  24983. break;
  24984. }
  24985. }
  24986. if (!canEncode) {
  24987. // for the character at position i we don't yet have an encoder in the list
  24988. for (const encoder of this.ENCODERS) {
  24989. if (encoder.canEncode(stringToEncode.charAt(i))) {
  24990. // Good, we found an encoder that can encode the character. We add him to the list and continue scanning
  24991. // the input
  24992. neededEncoders.push(encoder);
  24993. canEncode = true;
  24994. break;
  24995. }
  24996. }
  24997. }
  24998. if (!canEncode) {
  24999. // The character is not encodeable by any of the single byte encoders so we remember that we will need a
  25000. // Unicode encoder.
  25001. needUnicodeEncoder = true;
  25002. }
  25003. }
  25004. if (neededEncoders.length === 1 && !needUnicodeEncoder) {
  25005. // the entire input can be encoded by the ISO-8859-1 encoder
  25006. this.encoders = [neededEncoders[0]];
  25007. }
  25008. else {
  25009. // we need more than one single byte encoder or we need a Unicode encoder.
  25010. // In this case we append a UTF-8 and UTF-16 encoder to the list
  25011. this.encoders = [];
  25012. let index = 0;
  25013. for (const encoder of neededEncoders) {
  25014. this.encoders[index++] = encoder;
  25015. }
  25016. // this.encoders[index] = new CharsetEncoder(StandardCharsets.UTF_8);
  25017. // this.encoders[index + 1] = new CharsetEncoder(StandardCharsets.UTF_16BE);
  25018. }
  25019. // Compute priorityEncoderIndex by looking up priorityCharset in encoders
  25020. let priorityEncoderIndexValue = -1;
  25021. if (priorityCharset != null) {
  25022. for (let i = 0; i < this.encoders.length; i++) {
  25023. if (this.encoders[i] != null &&
  25024. priorityCharset.name === this.encoders[i].name) {
  25025. priorityEncoderIndexValue = i;
  25026. break;
  25027. }
  25028. }
  25029. }
  25030. this.priorityEncoderIndex = priorityEncoderIndexValue;
  25031. // invariants
  25032. // if(this?.encoders?.[0].name !== StandardCharsets.ISO_8859_1)){
  25033. // throw new Error("ISO-8859-1 must be the first encoder");
  25034. // }
  25035. }
  25036. length() {
  25037. return this.encoders.length;
  25038. }
  25039. getCharsetName(index) {
  25040. if (!(index < this.length())) {
  25041. throw new Error('index must be less than length');
  25042. }
  25043. return this.encoders[index].name;
  25044. }
  25045. getCharset(index) {
  25046. if (!(index < this.length())) {
  25047. throw new Error('index must be less than length');
  25048. }
  25049. return this.encoders[index].charset;
  25050. }
  25051. getECIValue(encoderIndex) {
  25052. return this.encoders[encoderIndex].charset.getValueIdentifier();
  25053. }
  25054. /*
  25055. * returns -1 if no priority charset was defined
  25056. */
  25057. getPriorityEncoderIndex() {
  25058. return this.priorityEncoderIndex;
  25059. }
  25060. canEncode(c, encoderIndex) {
  25061. if (!(encoderIndex < this.length())) {
  25062. throw new Error('index must be less than length');
  25063. }
  25064. return true;
  25065. }
  25066. encode(c, encoderIndex) {
  25067. if (!(encoderIndex < this.length())) {
  25068. throw new Error('index must be less than length');
  25069. }
  25070. return StringEncoding.encode(StringUtils.getCharAt(c), this.encoders[encoderIndex].name);
  25071. }
  25072. }
  25073. const COST_PER_ECI = 3; // approximated (latch + 2 codewords)
  25074. class MinimalECIInput {
  25075. /**
  25076. * Constructs a minimal input
  25077. *
  25078. * @param stringToEncode the character string to encode
  25079. * @param priorityCharset The preferred {@link Charset}. When the value of the argument is null, the algorithm
  25080. * chooses charsets that leads to a minimal representation. Otherwise the algorithm will use the priority
  25081. * charset to encode any character in the input that can be encoded by it if the charset is among the
  25082. * supported charsets.
  25083. * @param fnc1 denotes the character in the input that represents the FNC1 character or -1 if this is not GS1
  25084. * input.
  25085. */
  25086. constructor(stringToEncode, priorityCharset, fnc1) {
  25087. this.fnc1 = fnc1;
  25088. const encoderSet = new ECIEncoderSet(stringToEncode, priorityCharset, fnc1);
  25089. if (encoderSet.length() === 1) {
  25090. // optimization for the case when all can be encoded without ECI in ISO-8859-1
  25091. for (let i = 0; i < this.bytes.length; i++) {
  25092. const c = stringToEncode.charAt(i).charCodeAt(0);
  25093. this.bytes[i] = c === fnc1 ? 1000 : c;
  25094. }
  25095. }
  25096. else {
  25097. this.bytes = this.encodeMinimally(stringToEncode, encoderSet, fnc1);
  25098. }
  25099. }
  25100. getFNC1Character() {
  25101. return this.fnc1;
  25102. }
  25103. /**
  25104. * Returns the length of this input. The length is the number
  25105. * of {@code byte}s, FNC1 characters or ECIs in the sequence.
  25106. *
  25107. * @return the number of {@code char}s in this sequence
  25108. */
  25109. length() {
  25110. return this.bytes.length;
  25111. }
  25112. haveNCharacters(index, n) {
  25113. if (index + n - 1 >= this.bytes.length) {
  25114. return false;
  25115. }
  25116. for (let i = 0; i < n; i++) {
  25117. if (this.isECI(index + i)) {
  25118. return false;
  25119. }
  25120. }
  25121. return true;
  25122. }
  25123. /**
  25124. * Returns the {@code byte} value at the specified index. An index ranges from zero
  25125. * to {@code length() - 1}. The first {@code byte} value of the sequence is at
  25126. * index zero, the next at index one, and so on, as for array
  25127. * indexing.
  25128. *
  25129. * @param index the index of the {@code byte} value to be returned
  25130. *
  25131. * @return the specified {@code byte} value as character or the FNC1 character
  25132. *
  25133. * @throws IndexOutOfBoundsException
  25134. * if the {@code index} argument is negative or not less than
  25135. * {@code length()}
  25136. * @throws IllegalArgumentException
  25137. * if the value at the {@code index} argument is an ECI (@see #isECI)
  25138. */
  25139. charAt(index) {
  25140. if (index < 0 || index >= this.length()) {
  25141. throw new Error('' + index);
  25142. }
  25143. if (this.isECI(index)) {
  25144. throw new Error('value at ' + index + ' is not a character but an ECI');
  25145. }
  25146. return this.isFNC1(index) ? this.fnc1 : this.bytes[index];
  25147. }
  25148. /**
  25149. * Returns a {@code CharSequence} that is a subsequence of this sequence.
  25150. * The subsequence starts with the {@code char} value at the specified index and
  25151. * ends with the {@code char} value at index {@code end - 1}. The length
  25152. * (in {@code char}s) of the
  25153. * returned sequence is {@code end - start}, so if {@code start == end}
  25154. * then an empty sequence is returned.
  25155. *
  25156. * @param start the start index, inclusive
  25157. * @param end the end index, exclusive
  25158. *
  25159. * @return the specified subsequence
  25160. *
  25161. * @throws IndexOutOfBoundsException
  25162. * if {@code start} or {@code end} are negative,
  25163. * if {@code end} is greater than {@code length()},
  25164. * or if {@code start} is greater than {@code end}
  25165. * @throws IllegalArgumentException
  25166. * if a value in the range {@code start}-{@code end} is an ECI (@see #isECI)
  25167. */
  25168. subSequence(start, end) {
  25169. if (start < 0 || start > end || end > this.length()) {
  25170. throw new Error('' + start);
  25171. }
  25172. const result = new StringBuilder();
  25173. for (let i = start; i < end; i++) {
  25174. if (this.isECI(i)) {
  25175. throw new Error('value at ' + i + ' is not a character but an ECI');
  25176. }
  25177. result.append(this.charAt(i));
  25178. }
  25179. return result.toString();
  25180. }
  25181. /**
  25182. * Determines if a value is an ECI
  25183. *
  25184. * @param index the index of the value
  25185. *
  25186. * @return true if the value at position {@code index} is an ECI
  25187. *
  25188. * @throws IndexOutOfBoundsException
  25189. * if the {@code index} argument is negative or not less than
  25190. * {@code length()}
  25191. */
  25192. isECI(index) {
  25193. if (index < 0 || index >= this.length()) {
  25194. throw new Error('' + index);
  25195. }
  25196. return this.bytes[index] > 255 && this.bytes[index] <= 999;
  25197. }
  25198. /**
  25199. * Determines if a value is the FNC1 character
  25200. *
  25201. * @param index the index of the value
  25202. *
  25203. * @return true if the value at position {@code index} is the FNC1 character
  25204. *
  25205. * @throws IndexOutOfBoundsException
  25206. * if the {@code index} argument is negative or not less than
  25207. * {@code length()}
  25208. */
  25209. isFNC1(index) {
  25210. if (index < 0 || index >= this.length()) {
  25211. throw new Error('' + index);
  25212. }
  25213. return this.bytes[index] === 1000;
  25214. }
  25215. /**
  25216. * Returns the {@code int} ECI value at the specified index. An index ranges from zero
  25217. * to {@code length() - 1}. The first {@code byte} value of the sequence is at
  25218. * index zero, the next at index one, and so on, as for array
  25219. * indexing.
  25220. *
  25221. * @param index the index of the {@code int} value to be returned
  25222. *
  25223. * @return the specified {@code int} ECI value.
  25224. * The ECI specified the encoding of all bytes with a higher index until the
  25225. * next ECI or until the end of the input if no other ECI follows.
  25226. *
  25227. * @throws IndexOutOfBoundsException
  25228. * if the {@code index} argument is negative or not less than
  25229. * {@code length()}
  25230. * @throws IllegalArgumentException
  25231. * if the value at the {@code index} argument is not an ECI (@see #isECI)
  25232. */
  25233. getECIValue(index) {
  25234. if (index < 0 || index >= this.length()) {
  25235. throw new Error('' + index);
  25236. }
  25237. if (!this.isECI(index)) {
  25238. throw new Error('value at ' + index + ' is not an ECI but a character');
  25239. }
  25240. return this.bytes[index] - 256;
  25241. }
  25242. addEdge(edges, to, edge) {
  25243. if (edges[to][edge.encoderIndex] == null ||
  25244. edges[to][edge.encoderIndex].cachedTotalSize > edge.cachedTotalSize) {
  25245. edges[to][edge.encoderIndex] = edge;
  25246. }
  25247. }
  25248. addEdges(stringToEncode, encoderSet, edges, from, previous, fnc1) {
  25249. const ch = stringToEncode.charAt(from).charCodeAt(0);
  25250. let start = 0;
  25251. let end = encoderSet.length();
  25252. if (encoderSet.getPriorityEncoderIndex() >= 0 &&
  25253. (ch === fnc1 ||
  25254. encoderSet.canEncode(ch, encoderSet.getPriorityEncoderIndex()))) {
  25255. start = encoderSet.getPriorityEncoderIndex();
  25256. end = start + 1;
  25257. }
  25258. for (let i = start; i < end; i++) {
  25259. if (ch === fnc1 || encoderSet.canEncode(ch, i)) {
  25260. this.addEdge(edges, from + 1, new InputEdge(ch, encoderSet, i, previous, fnc1));
  25261. }
  25262. }
  25263. }
  25264. encodeMinimally(stringToEncode, encoderSet, fnc1) {
  25265. const inputLength = stringToEncode.length;
  25266. // Array that represents vertices. There is a vertex for every character and encoding.
  25267. const edges = new InputEdge[inputLength + 1][encoderSet.length()]();
  25268. this.addEdges(stringToEncode, encoderSet, edges, 0, null, fnc1);
  25269. for (let i = 1; i <= inputLength; i++) {
  25270. for (let j = 0; j < encoderSet.length(); j++) {
  25271. if (edges[i][j] != null && i < inputLength) {
  25272. this.addEdges(stringToEncode, encoderSet, edges, i, edges[i][j], fnc1);
  25273. }
  25274. }
  25275. // optimize memory by removing edges that have been passed.
  25276. for (let j = 0; j < encoderSet.length(); j++) {
  25277. edges[i - 1][j] = null;
  25278. }
  25279. }
  25280. let minimalJ = -1;
  25281. let minimalSize = Integer.MAX_VALUE;
  25282. for (let j = 0; j < encoderSet.length(); j++) {
  25283. if (edges[inputLength][j] != null) {
  25284. const edge = edges[inputLength][j];
  25285. if (edge.cachedTotalSize < minimalSize) {
  25286. minimalSize = edge.cachedTotalSize;
  25287. minimalJ = j;
  25288. }
  25289. }
  25290. }
  25291. if (minimalJ < 0) {
  25292. throw new Error('Failed to encode "' + stringToEncode + '"');
  25293. }
  25294. const intsAL = [];
  25295. let current = edges[inputLength][minimalJ];
  25296. while (current != null) {
  25297. if (current.isFNC1()) {
  25298. intsAL.unshift(1000);
  25299. }
  25300. else {
  25301. const bytes = encoderSet.encode(current.c, current.encoderIndex);
  25302. for (let i = bytes.length - 1; i >= 0; i--) {
  25303. intsAL.unshift(bytes[i] & 0xff);
  25304. }
  25305. }
  25306. const previousEncoderIndex = current.previous === null ? 0 : current.previous.encoderIndex;
  25307. if (previousEncoderIndex !== current.encoderIndex) {
  25308. intsAL.unshift(256 + encoderSet.getECIValue(current.encoderIndex));
  25309. }
  25310. current = current.previous;
  25311. }
  25312. const ints = [];
  25313. for (let i = 0; i < ints.length; i++) {
  25314. ints[i] = intsAL[i];
  25315. }
  25316. return ints;
  25317. }
  25318. }
  25319. class InputEdge {
  25320. constructor(c, encoderSet, encoderIndex, previous, fnc1) {
  25321. this.c = c;
  25322. this.encoderSet = encoderSet;
  25323. this.encoderIndex = encoderIndex;
  25324. this.previous = previous;
  25325. this.fnc1 = fnc1;
  25326. this.c = c === fnc1 ? 1000 : c;
  25327. let size = this.isFNC1() ? 1 : encoderSet.encode(c, encoderIndex).length;
  25328. const previousEncoderIndex = previous === null ? 0 : previous.encoderIndex;
  25329. if (previousEncoderIndex !== encoderIndex) {
  25330. size += COST_PER_ECI;
  25331. }
  25332. if (previous != null) {
  25333. size += previous.cachedTotalSize;
  25334. }
  25335. this.cachedTotalSize = size;
  25336. }
  25337. isFNC1() {
  25338. return this.c === 1000;
  25339. }
  25340. }
  25341. var Mode;
  25342. (function (Mode) {
  25343. Mode[Mode["ASCII"] = 0] = "ASCII";
  25344. Mode[Mode["C40"] = 1] = "C40";
  25345. Mode[Mode["TEXT"] = 2] = "TEXT";
  25346. Mode[Mode["X12"] = 3] = "X12";
  25347. Mode[Mode["EDF"] = 4] = "EDF";
  25348. Mode[Mode["B256"] = 5] = "B256";
  25349. })(Mode || (Mode = {}));
  25350. const C40_SHIFT2_CHARS = [
  25351. '!',
  25352. '"',
  25353. '#',
  25354. '$',
  25355. '%',
  25356. '&',
  25357. "'",
  25358. '(',
  25359. ')',
  25360. '*',
  25361. '+',
  25362. ',',
  25363. '-',
  25364. '.',
  25365. '/',
  25366. ':',
  25367. ';',
  25368. '<',
  25369. '=',
  25370. '>',
  25371. '?',
  25372. '@',
  25373. '[',
  25374. '\\',
  25375. ']',
  25376. '^',
  25377. '_',
  25378. ];
  25379. class MinimalEncoder {
  25380. static isExtendedASCII(ch, fnc1) {
  25381. return ch !== fnc1 && ch >= 128 && ch <= 255;
  25382. }
  25383. static isInC40Shift1Set(ch) {
  25384. return ch <= 31;
  25385. }
  25386. static isInC40Shift2Set(ch, fnc1) {
  25387. for (const c40Shift2Char of C40_SHIFT2_CHARS) {
  25388. if (c40Shift2Char.charCodeAt(0) === ch) {
  25389. return true;
  25390. }
  25391. }
  25392. return ch === fnc1;
  25393. }
  25394. static isInTextShift1Set(ch) {
  25395. return this.isInC40Shift1Set(ch);
  25396. }
  25397. static isInTextShift2Set(ch, fnc1) {
  25398. return this.isInC40Shift2Set(ch, fnc1);
  25399. }
  25400. /**
  25401. * Performs message encoding of a DataMatrix message
  25402. *
  25403. * @param msg the message
  25404. * @param priorityCharset The preferred {@link Charset}. When the value of the argument is null, the algorithm
  25405. * chooses charsets that leads to a minimal representation. Otherwise the algorithm will use the priority
  25406. * charset to encode any character in the input that can be encoded by it if the charset is among the
  25407. * supported charsets.
  25408. * @param fnc1 denotes the character in the input that represents the FNC1 character or -1 if this is not a GS1
  25409. * bar code. If the value is not -1 then a FNC1 is also prepended.
  25410. * @param shape requested shape.
  25411. * @return the encoded message (the char values range from 0 to 255)
  25412. */
  25413. static encodeHighLevel(msg, priorityCharset = null, fnc1 = -1, shape = 0 /* FORCE_NONE */) {
  25414. let macroId = 0;
  25415. if (msg.startsWith(MACRO_05_HEADER) && msg.endsWith(MACRO_TRAILER)) {
  25416. macroId = 5;
  25417. msg = msg.substring(MACRO_05_HEADER.length, msg.length - 2);
  25418. }
  25419. else if (msg.startsWith(MACRO_06_HEADER) && msg.endsWith(MACRO_TRAILER)) {
  25420. macroId = 6;
  25421. msg = msg.substring(MACRO_06_HEADER.length, msg.length - 2);
  25422. }
  25423. return decodeURIComponent(escape(String.fromCharCode(...this.encode(msg, priorityCharset, fnc1, shape, macroId))));
  25424. }
  25425. /**
  25426. * Encodes input minimally and returns an array of the codewords
  25427. *
  25428. * @param input The string to encode
  25429. * @param priorityCharset The preferred {@link Charset}. When the value of the argument is null, the algorithm
  25430. * chooses charsets that leads to a minimal representation. Otherwise the algorithm will use the priority
  25431. * charset to encode any character in the input that can be encoded by it if the charset is among the
  25432. * supported charsets.
  25433. * @param fnc1 denotes the character in the input that represents the FNC1 character or -1 if this is not a GS1
  25434. * bar code. If the value is not -1 then a FNC1 is also prepended.
  25435. * @param shape requested shape.
  25436. * @param macroId Prepends the specified macro function in case that a value of 5 or 6 is specified.
  25437. * @return An array of bytes representing the codewords of a minimal encoding.
  25438. */
  25439. static encode(input, priorityCharset, fnc1, shape, macroId) {
  25440. return this.encodeMinimally(new Input(input, priorityCharset, fnc1, shape, macroId)).getBytes();
  25441. }
  25442. static addEdge(edges, edge) {
  25443. const vertexIndex = edge.fromPosition + edge.characterLength;
  25444. if (edges[vertexIndex][edge.getEndMode()] === null ||
  25445. edges[vertexIndex][edge.getEndMode()].cachedTotalSize >
  25446. edge.cachedTotalSize) {
  25447. edges[vertexIndex][edge.getEndMode()] = edge;
  25448. }
  25449. }
  25450. /** @return the number of words in which the string starting at from can be encoded in c40 or text mode.
  25451. * The number of characters encoded is returned in characterLength.
  25452. * The number of characters encoded is also minimal in the sense that the algorithm stops as soon
  25453. * as a character encoding fills a C40 word competely (three C40 values). An exception is at the
  25454. * end of the string where two C40 values are allowed (according to the spec the third c40 value
  25455. * is filled with 0 (Shift 1) in this case).
  25456. */
  25457. static getNumberOfC40Words(input, from, c40, characterLength) {
  25458. let thirdsCount = 0;
  25459. for (let i = from; i < input.length(); i++) {
  25460. if (input.isECI(i)) {
  25461. characterLength[0] = 0;
  25462. return 0;
  25463. }
  25464. const ci = input.charAt(i);
  25465. if ((c40 && HighLevelEncoder$1.isNativeC40(ci)) ||
  25466. (!c40 && HighLevelEncoder$1.isNativeText(ci))) {
  25467. thirdsCount++; // native
  25468. }
  25469. else if (!MinimalEncoder.isExtendedASCII(ci, input.getFNC1Character())) {
  25470. thirdsCount += 2; // shift
  25471. }
  25472. else {
  25473. const asciiValue = ci & 0xff;
  25474. if (asciiValue >= 128 &&
  25475. ((c40 && HighLevelEncoder$1.isNativeC40(asciiValue - 128)) ||
  25476. (!c40 && HighLevelEncoder$1.isNativeText(asciiValue - 128)))) {
  25477. thirdsCount += 3; // shift, Upper shift
  25478. }
  25479. else {
  25480. thirdsCount += 4; // shift, Upper shift, shift
  25481. }
  25482. }
  25483. if (thirdsCount % 3 === 0 ||
  25484. ((thirdsCount - 2) % 3 === 0 && i + 1 === input.length())) {
  25485. characterLength[0] = i - from + 1;
  25486. return Math.ceil(thirdsCount / 3.0);
  25487. }
  25488. }
  25489. characterLength[0] = 0;
  25490. return 0;
  25491. }
  25492. static addEdges(input, edges, from, previous) {
  25493. if (input.isECI(from)) {
  25494. this.addEdge(edges, new Edge(input, Mode.ASCII, from, 1, previous));
  25495. return;
  25496. }
  25497. const ch = input.charAt(from);
  25498. if (previous === null || previous.getEndMode() !== Mode.EDF) {
  25499. // not possible to unlatch a full EDF edge to something
  25500. // else
  25501. if (HighLevelEncoder$1.isDigit(ch) &&
  25502. input.haveNCharacters(from, 2) &&
  25503. HighLevelEncoder$1.isDigit(input.charAt(from + 1))) {
  25504. // two digits ASCII encoded
  25505. this.addEdge(edges, new Edge(input, Mode.ASCII, from, 2, previous));
  25506. }
  25507. else {
  25508. // one ASCII encoded character or an extended character via Upper Shift
  25509. this.addEdge(edges, new Edge(input, Mode.ASCII, from, 1, previous));
  25510. }
  25511. const modes = [Mode.C40, Mode.TEXT];
  25512. for (const mode of modes) {
  25513. const characterLength = [];
  25514. if (MinimalEncoder.getNumberOfC40Words(input, from, mode === Mode.C40, characterLength) > 0) {
  25515. this.addEdge(edges, new Edge(input, mode, from, characterLength[0], previous));
  25516. }
  25517. }
  25518. if (input.haveNCharacters(from, 3) &&
  25519. HighLevelEncoder$1.isNativeX12(input.charAt(from)) &&
  25520. HighLevelEncoder$1.isNativeX12(input.charAt(from + 1)) &&
  25521. HighLevelEncoder$1.isNativeX12(input.charAt(from + 2))) {
  25522. this.addEdge(edges, new Edge(input, Mode.X12, from, 3, previous));
  25523. }
  25524. this.addEdge(edges, new Edge(input, Mode.B256, from, 1, previous));
  25525. }
  25526. // We create 4 EDF edges, with 1, 2 3 or 4 characters length. The fourth normally doesn't have a latch to ASCII
  25527. // unless it is 2 characters away from the end of the input.
  25528. let i;
  25529. for (i = 0; i < 3; i++) {
  25530. const pos = from + i;
  25531. if (input.haveNCharacters(pos, 1) &&
  25532. HighLevelEncoder$1.isNativeEDIFACT(input.charAt(pos))) {
  25533. this.addEdge(edges, new Edge(input, Mode.EDF, from, i + 1, previous));
  25534. }
  25535. else {
  25536. break;
  25537. }
  25538. }
  25539. if (i === 3 &&
  25540. input.haveNCharacters(from, 4) &&
  25541. HighLevelEncoder$1.isNativeEDIFACT(input.charAt(from + 3))) {
  25542. this.addEdge(edges, new Edge(input, Mode.EDF, from, 4, previous));
  25543. }
  25544. }
  25545. static encodeMinimally(input) {
  25546. /* The minimal encoding is computed by Dijkstra. The acyclic graph is modeled as follows:
  25547. * A vertex represents a combination of a position in the input and an encoding mode where position 0
  25548. * denotes the position left of the first character, 1 the position left of the second character and so on.
  25549. * Likewise the end vertices are located after the last character at position input.length().
  25550. * For any position there might be up to six vertices, one for each of the encoding types ASCII, C40, TEXT, X12,
  25551. * EDF and B256.
  25552. *
  25553. * As an example consider the input string "ABC123" then at position 0 there is only one vertex with the default
  25554. * ASCII encodation. At position 3 there might be vertices for the types ASCII, C40, X12, EDF and B256.
  25555. *
  25556. * An edge leading to such a vertex encodes one or more of the characters left of the position that the vertex
  25557. * represents. It encodes the characters in the encoding mode of the vertex that it ends on. In other words,
  25558. * all edges leading to a particular vertex encode the same characters (the length of the suffix can vary) using the same
  25559. * encoding mode.
  25560. * As an example consider the input string "ABC123" and the vertex (4,EDF). Possible edges leading to this vertex
  25561. * are:
  25562. * (0,ASCII) --EDF(ABC1)--> (4,EDF)
  25563. * (1,ASCII) --EDF(BC1)--> (4,EDF)
  25564. * (1,B256) --EDF(BC1)--> (4,EDF)
  25565. * (1,EDF) --EDF(BC1)--> (4,EDF)
  25566. * (2,ASCII) --EDF(C1)--> (4,EDF)
  25567. * (2,B256) --EDF(C1)--> (4,EDF)
  25568. * (2,EDF) --EDF(C1)--> (4,EDF)
  25569. * (3,ASCII) --EDF(1)--> (4,EDF)
  25570. * (3,B256) --EDF(1)--> (4,EDF)
  25571. * (3,EDF) --EDF(1)--> (4,EDF)
  25572. * (3,C40) --EDF(1)--> (4,EDF)
  25573. * (3,X12) --EDF(1)--> (4,EDF)
  25574. *
  25575. * The edges leading to a vertex are stored in such a way that there is a fast way to enumerate the edges ending
  25576. * on a particular vertex.
  25577. *
  25578. * The algorithm processes the vertices in order of their position thereby performing the following:
  25579. *
  25580. * For every vertex at position i the algorithm enumerates the edges ending on the vertex and removes all but the
  25581. * shortest from that list.
  25582. * Then it processes the vertices for the position i+1. If i+1 == input.length() then the algorithm ends
  25583. * and chooses the the edge with the smallest size from any of the edges leading to vertices at this position.
  25584. * Otherwise the algorithm computes all possible outgoing edges for the vertices at the position i+1
  25585. *
  25586. * Examples:
  25587. * The process is illustrated by showing the graph (edges) after each iteration from left to right over the input:
  25588. * An edge is drawn as follows "(" + fromVertex + ") -- " + encodingMode + "(" + encodedInput + ") (" +
  25589. * accumulatedSize + ") --> (" + toVertex + ")"
  25590. *
  25591. * Example 1 encoding the string "ABCDEFG":
  25592. *
  25593. *
  25594. * Situation after adding edges to the start vertex (0,ASCII)
  25595. * (0,ASCII) ASCII(A) (1) --> (1,ASCII)
  25596. * (0,ASCII) B256(A) (3) --> (1,B256)
  25597. * (0,ASCII) EDF(AB) (4) --> (2,EDF)
  25598. * (0,ASCII) C40(ABC) (3) --> (3,C40)
  25599. * (0,ASCII) TEXT(ABC) (5) --> (3,TEXT)
  25600. * (0,ASCII) X12(ABC) (3) --> (3,X12)
  25601. * (0,ASCII) EDF(ABC) (4) --> (3,EDF)
  25602. * (0,ASCII) EDF(ABCD) (4) --> (4,EDF)
  25603. *
  25604. * Situation after adding edges to vertices at position 1
  25605. * (0,ASCII) ASCII(A) (1) --> (1,ASCII)
  25606. * (0,ASCII) B256(A) (3) --> (1,B256)
  25607. * (0,ASCII) EDF(AB) (4) --> (2,EDF)
  25608. * (0,ASCII) C40(ABC) (3) --> (3,C40)
  25609. * (0,ASCII) TEXT(ABC) (5) --> (3,TEXT)
  25610. * (0,ASCII) X12(ABC) (3) --> (3,X12)
  25611. * (0,ASCII) EDF(ABC) (4) --> (3,EDF)
  25612. * (0,ASCII) EDF(ABCD) (4) --> (4,EDF)
  25613. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII)
  25614. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) B256(B) (4) --> (2,B256)
  25615. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) EDF(BC) (5) --> (3,EDF)
  25616. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) C40(BCD) (4) --> (4,C40)
  25617. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) TEXT(BCD) (6) --> (4,TEXT)
  25618. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) X12(BCD) (4) --> (4,X12)
  25619. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) EDF(BCD) (5) --> (4,EDF)
  25620. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) EDF(BCDE) (5) --> (5,EDF)
  25621. * (0,ASCII) B256(A) (3) --> (1,B256) ASCII(B) (4) --> (2,ASCII)
  25622. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256)
  25623. * (0,ASCII) B256(A) (3) --> (1,B256) EDF(BC) (6) --> (3,EDF)
  25624. * (0,ASCII) B256(A) (3) --> (1,B256) C40(BCD) (5) --> (4,C40)
  25625. * (0,ASCII) B256(A) (3) --> (1,B256) TEXT(BCD) (7) --> (4,TEXT)
  25626. * (0,ASCII) B256(A) (3) --> (1,B256) X12(BCD) (5) --> (4,X12)
  25627. * (0,ASCII) B256(A) (3) --> (1,B256) EDF(BCD) (6) --> (4,EDF)
  25628. * (0,ASCII) B256(A) (3) --> (1,B256) EDF(BCDE) (6) --> (5,EDF)
  25629. *
  25630. * Edge "(1,ASCII) ASCII(B) (2) --> (2,ASCII)" is minimal for the vertex (2,ASCII) so that edge "(1,B256) ASCII(B) (4) --> (2,ASCII)" is removed.
  25631. * Edge "(1,B256) B256(B) (3) --> (2,B256)" is minimal for the vertext (2,B256) so that the edge "(1,ASCII) B256(B) (4) --> (2,B256)" is removed.
  25632. *
  25633. * Situation after adding edges to vertices at position 2
  25634. * (0,ASCII) ASCII(A) (1) --> (1,ASCII)
  25635. * (0,ASCII) B256(A) (3) --> (1,B256)
  25636. * (0,ASCII) EDF(AB) (4) --> (2,EDF)
  25637. * (0,ASCII) C40(ABC) (3) --> (3,C40)
  25638. * (0,ASCII) TEXT(ABC) (5) --> (3,TEXT)
  25639. * (0,ASCII) X12(ABC) (3) --> (3,X12)
  25640. * (0,ASCII) EDF(ABC) (4) --> (3,EDF)
  25641. * (0,ASCII) EDF(ABCD) (4) --> (4,EDF)
  25642. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII)
  25643. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) EDF(BC) (5) --> (3,EDF)
  25644. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) C40(BCD) (4) --> (4,C40)
  25645. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) TEXT(BCD) (6) --> (4,TEXT)
  25646. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) X12(BCD) (4) --> (4,X12)
  25647. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) EDF(BCD) (5) --> (4,EDF)
  25648. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) EDF(BCDE) (5) --> (5,EDF)
  25649. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256)
  25650. * (0,ASCII) B256(A) (3) --> (1,B256) EDF(BC) (6) --> (3,EDF)
  25651. * (0,ASCII) B256(A) (3) --> (1,B256) C40(BCD) (5) --> (4,C40)
  25652. * (0,ASCII) B256(A) (3) --> (1,B256) TEXT(BCD) (7) --> (4,TEXT)
  25653. * (0,ASCII) B256(A) (3) --> (1,B256) X12(BCD) (5) --> (4,X12)
  25654. * (0,ASCII) B256(A) (3) --> (1,B256) EDF(BCD) (6) --> (4,EDF)
  25655. * (0,ASCII) B256(A) (3) --> (1,B256) EDF(BCDE) (6) --> (5,EDF)
  25656. * (0,ASCII) EDF(AB) (4) --> (2,EDF) ASCII(C) (5) --> (3,ASCII)
  25657. * (0,ASCII) EDF(AB) (4) --> (2,EDF) B256(C) (6) --> (3,B256)
  25658. * (0,ASCII) EDF(AB) (4) --> (2,EDF) EDF(CD) (7) --> (4,EDF)
  25659. * (0,ASCII) EDF(AB) (4) --> (2,EDF) C40(CDE) (6) --> (5,C40)
  25660. * (0,ASCII) EDF(AB) (4) --> (2,EDF) TEXT(CDE) (8) --> (5,TEXT)
  25661. * (0,ASCII) EDF(AB) (4) --> (2,EDF) X12(CDE) (6) --> (5,X12)
  25662. * (0,ASCII) EDF(AB) (4) --> (2,EDF) EDF(CDE) (7) --> (5,EDF)
  25663. * (0,ASCII) EDF(AB) (4) --> (2,EDF) EDF(CDEF) (7) --> (6,EDF)
  25664. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) ASCII(C) (3) --> (3,ASCII)
  25665. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) B256(C) (5) --> (3,B256)
  25666. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) EDF(CD) (6) --> (4,EDF)
  25667. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) C40(CDE) (5) --> (5,C40)
  25668. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) TEXT(CDE) (7) --> (5,TEXT)
  25669. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) X12(CDE) (5) --> (5,X12)
  25670. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) EDF(CDE) (6) --> (5,EDF)
  25671. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) EDF(CDEF) (6) --> (6,EDF)
  25672. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) ASCII(C) (4) --> (3,ASCII)
  25673. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) B256(C) (4) --> (3,B256)
  25674. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) EDF(CD) (6) --> (4,EDF)
  25675. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) C40(CDE) (5) --> (5,C40)
  25676. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) TEXT(CDE) (7) --> (5,TEXT)
  25677. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) X12(CDE) (5) --> (5,X12)
  25678. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) EDF(CDE) (6) --> (5,EDF)
  25679. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) EDF(CDEF) (6) --> (6,EDF)
  25680. *
  25681. * Edge "(2,ASCII) ASCII(C) (3) --> (3,ASCII)" is minimal for the vertex (3,ASCII) so that edges "(2,EDF) ASCII(C) (5) --> (3,ASCII)"
  25682. * and "(2,B256) ASCII(C) (4) --> (3,ASCII)" can be removed.
  25683. * Edge "(0,ASCII) EDF(ABC) (4) --> (3,EDF)" is minimal for the vertex (3,EDF) so that edges "(1,ASCII) EDF(BC) (5) --> (3,EDF)"
  25684. * and "(1,B256) EDF(BC) (6) --> (3,EDF)" can be removed.
  25685. * Edge "(2,B256) B256(C) (4) --> (3,B256)" is minimal for the vertex (3,B256) so that edges "(2,ASCII) B256(C) (5) --> (3,B256)"
  25686. * and "(2,EDF) B256(C) (6) --> (3,B256)" can be removed.
  25687. *
  25688. * This continues for vertices 3 thru 7
  25689. *
  25690. * Situation after adding edges to vertices at position 7
  25691. * (0,ASCII) ASCII(A) (1) --> (1,ASCII)
  25692. * (0,ASCII) B256(A) (3) --> (1,B256)
  25693. * (0,ASCII) EDF(AB) (4) --> (2,EDF)
  25694. * (0,ASCII) C40(ABC) (3) --> (3,C40)
  25695. * (0,ASCII) TEXT(ABC) (5) --> (3,TEXT)
  25696. * (0,ASCII) X12(ABC) (3) --> (3,X12)
  25697. * (0,ASCII) EDF(ABC) (4) --> (3,EDF)
  25698. * (0,ASCII) EDF(ABCD) (4) --> (4,EDF)
  25699. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII)
  25700. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) C40(BCD) (4) --> (4,C40)
  25701. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) TEXT(BCD) (6) --> (4,TEXT)
  25702. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) X12(BCD) (4) --> (4,X12)
  25703. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) EDF(BCDE) (5) --> (5,EDF)
  25704. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256)
  25705. * (0,ASCII) C40(ABC) (3) --> (3,C40) C40(DEF) (5) --> (6,C40)
  25706. * (0,ASCII) X12(ABC) (3) --> (3,X12) X12(DEF) (5) --> (6,X12)
  25707. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) ASCII(C) (3) --> (3,ASCII)
  25708. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) C40(CDE) (5) --> (5,C40)
  25709. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) TEXT(CDE) (7) --> (5,TEXT)
  25710. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) X12(CDE) (5) --> (5,X12)
  25711. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) EDF(CDEF) (6) --> (6,EDF)
  25712. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) C40(BCD) (4) --> (4,C40) C40(EFG) (6) --> (7,C40) //Solution 1
  25713. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) X12(BCD) (4) --> (4,X12) X12(EFG) (6) --> (7,X12) //Solution 2
  25714. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) B256(C) (4) --> (3,B256)
  25715. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) ASCII(C) (3) --> (3,ASCII) ASCII(D) (4) --> (4,ASCII)
  25716. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) ASCII(C) (3) --> (3,ASCII) TEXT(DEF) (8) --> (6,TEXT)
  25717. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) ASCII(C) (3) --> (3,ASCII) EDF(DEFG) (7) --> (7,EDF)
  25718. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) B256(C) (4) --> (3,B256) B256(D) (5) --> (4,B256)
  25719. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) ASCII(C) (3) --> (3,ASCII) ASCII(D) (4) --> (4,ASCII) ASCII(E) (5) --> (5,ASCII)
  25720. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) ASCII(C) (3) --> (3,ASCII) ASCII(D) (4) --> (4,ASCII) TEXT(EFG) (9) --> (7,TEXT)
  25721. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) B256(C) (4) --> (3,B256) B256(D) (5) --> (4,B256) B256(E) (6) --> (5,B256)
  25722. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) ASCII(C) (3) --> (3,ASCII) ASCII(D) (4) --> (4,ASCII) ASCII(E) (5) --> (5,ASCII) ASCII(F) (6) --> (6,ASCII)
  25723. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) B256(C) (4) --> (3,B256) B256(D) (5) --> (4,B256) B256(E) (6) --> (5,B256) B256(F) (7) --> (6,B256)
  25724. * (0,ASCII) ASCII(A) (1) --> (1,ASCII) ASCII(B) (2) --> (2,ASCII) ASCII(C) (3) --> (3,ASCII) ASCII(D) (4) --> (4,ASCII) ASCII(E) (5) --> (5,ASCII) ASCII(F) (6) --> (6,ASCII) ASCII(G) (7) --> (7,ASCII)
  25725. * (0,ASCII) B256(A) (3) --> (1,B256) B256(B) (3) --> (2,B256) B256(C) (4) --> (3,B256) B256(D) (5) --> (4,B256) B256(E) (6) --> (5,B256) B256(F) (7) --> (6,B256) B256(G) (8) --> (7,B256)
  25726. *
  25727. * Hence a minimal encoding of "ABCDEFG" is either ASCII(A),C40(BCDEFG) or ASCII(A), X12(BCDEFG) with a size of 5 bytes.
  25728. */
  25729. const inputLength = input.length();
  25730. // Array that represents vertices. There is a vertex for every character and mode.
  25731. // The last dimension in the array below encodes the 6 modes ASCII, C40, TEXT, X12, EDF and B256
  25732. const edges = Array(inputLength + 1)
  25733. .fill(null)
  25734. .map(() => Array(6).fill(0));
  25735. this.addEdges(input, edges, 0, null);
  25736. for (let i = 1; i <= inputLength; i++) {
  25737. for (let j = 0; j < 6; j++) {
  25738. if (edges[i][j] !== null && i < inputLength) {
  25739. this.addEdges(input, edges, i, edges[i][j]);
  25740. }
  25741. }
  25742. // optimize memory by removing edges that have been passed.
  25743. for (let j = 0; j < 6; j++) {
  25744. edges[i - 1][j] = null;
  25745. }
  25746. }
  25747. let minimalJ = -1;
  25748. let minimalSize = Integer.MAX_VALUE;
  25749. for (let j = 0; j < 6; j++) {
  25750. if (edges[inputLength][j] !== null) {
  25751. const edge = edges[inputLength][j];
  25752. const size = j >= 1 && j <= 3 ? edge.cachedTotalSize + 1 : edge.cachedTotalSize; // C40, TEXT and X12 need an
  25753. // extra unlatch at the end
  25754. if (size < minimalSize) {
  25755. minimalSize = size;
  25756. minimalJ = j;
  25757. }
  25758. }
  25759. }
  25760. if (minimalJ < 0) {
  25761. throw new Error('Failed to encode "' + input + '"');
  25762. }
  25763. return new Result(edges[inputLength][minimalJ]);
  25764. }
  25765. }
  25766. class Result {
  25767. constructor(solution) {
  25768. const input = solution.input;
  25769. let size = 0;
  25770. let bytesAL = [];
  25771. const randomizePostfixLength = [];
  25772. const randomizeLengths = [];
  25773. if ((solution.mode === Mode.C40 ||
  25774. solution.mode === Mode.TEXT ||
  25775. solution.mode === Mode.X12) &&
  25776. solution.getEndMode() !== Mode.ASCII) {
  25777. size += this.prepend(Edge.getBytes(254), bytesAL);
  25778. }
  25779. let current = solution;
  25780. while (current !== null) {
  25781. size += this.prepend(current.getDataBytes(), bytesAL);
  25782. if (current.previous === null ||
  25783. current.getPreviousStartMode() !== current.getMode()) {
  25784. if (current.getMode() === Mode.B256) {
  25785. if (size <= 249) {
  25786. bytesAL.unshift(size);
  25787. size++;
  25788. }
  25789. else {
  25790. bytesAL.unshift(size % 250);
  25791. bytesAL.unshift(size / 250 + 249);
  25792. size += 2;
  25793. }
  25794. randomizePostfixLength.push(bytesAL.length);
  25795. randomizeLengths.push(size);
  25796. }
  25797. this.prepend(current.getLatchBytes(), bytesAL);
  25798. size = 0;
  25799. }
  25800. current = current.previous;
  25801. }
  25802. if (input.getMacroId() === 5) {
  25803. size += this.prepend(Edge.getBytes(236), bytesAL);
  25804. }
  25805. else if (input.getMacroId() === 6) {
  25806. size += this.prepend(Edge.getBytes(237), bytesAL);
  25807. }
  25808. if (input.getFNC1Character() > 0) {
  25809. size += this.prepend(Edge.getBytes(232), bytesAL);
  25810. }
  25811. for (let i = 0; i < randomizePostfixLength.length; i++) {
  25812. this.applyRandomPattern(bytesAL, bytesAL.length - randomizePostfixLength[i], randomizeLengths[i]);
  25813. }
  25814. // add padding
  25815. const capacity = solution.getMinSymbolSize(bytesAL.length);
  25816. if (bytesAL.length < capacity) {
  25817. bytesAL.push(129);
  25818. }
  25819. while (bytesAL.length < capacity) {
  25820. bytesAL.push(this.randomize253State(bytesAL.length + 1));
  25821. }
  25822. this.bytes = new Uint8Array(bytesAL.length);
  25823. for (let i = 0; i < this.bytes.length; i++) {
  25824. this.bytes[i] = bytesAL[i];
  25825. }
  25826. }
  25827. prepend(bytes, into) {
  25828. for (let i = bytes.length - 1; i >= 0; i--) {
  25829. into.unshift(bytes[i]);
  25830. }
  25831. return bytes.length;
  25832. }
  25833. randomize253State(codewordPosition) {
  25834. const pseudoRandom = ((149 * codewordPosition) % 253) + 1;
  25835. const tempVariable = 129 + pseudoRandom;
  25836. return tempVariable <= 254 ? tempVariable : tempVariable - 254;
  25837. }
  25838. applyRandomPattern(bytesAL, startPosition, length) {
  25839. for (let i = 0; i < length; i++) {
  25840. // See "B.1 253-state algorithm
  25841. const Pad_codeword_position = startPosition + i;
  25842. const Pad_codeword_value = bytesAL[Pad_codeword_position] & 0xff;
  25843. const pseudo_random_number = ((149 * (Pad_codeword_position + 1)) % 255) + 1;
  25844. const temp_variable = Pad_codeword_value + pseudo_random_number;
  25845. bytesAL[Pad_codeword_position] =
  25846. temp_variable <= 255 ? temp_variable : temp_variable - 256;
  25847. }
  25848. }
  25849. getBytes() {
  25850. return this.bytes;
  25851. }
  25852. }
  25853. class Edge {
  25854. constructor(input, mode, fromPosition, characterLength, previous) {
  25855. this.input = input;
  25856. this.mode = mode;
  25857. this.fromPosition = fromPosition;
  25858. this.characterLength = characterLength;
  25859. this.previous = previous;
  25860. this.allCodewordCapacities = [
  25861. 3, 5, 8, 10, 12, 16, 18, 22, 30, 32, 36, 44, 49, 62, 86, 114, 144, 174, 204,
  25862. 280, 368, 456, 576, 696, 816, 1050, 1304, 1558,
  25863. ];
  25864. this.squareCodewordCapacities = [
  25865. 3, 5, 8, 12, 18, 22, 30, 36, 44, 62, 86, 114, 144, 174, 204, 280, 368, 456,
  25866. 576, 696, 816, 1050, 1304, 1558,
  25867. ];
  25868. this.rectangularCodewordCapacities = [5, 10, 16, 33, 32, 49];
  25869. if (!(fromPosition + characterLength <= input.length())) {
  25870. throw new Error('Invalid edge');
  25871. }
  25872. let size = previous !== null ? previous.cachedTotalSize : 0;
  25873. const previousMode = this.getPreviousMode();
  25874. /*
  25875. * Switching modes
  25876. * ASCII -> C40: latch 230
  25877. * ASCII -> TEXT: latch 239
  25878. * ASCII -> X12: latch 238
  25879. * ASCII -> EDF: latch 240
  25880. * ASCII -> B256: latch 231
  25881. * C40 -> ASCII: word(c1,c2,c3), 254
  25882. * TEXT -> ASCII: word(c1,c2,c3), 254
  25883. * X12 -> ASCII: word(c1,c2,c3), 254
  25884. * EDIFACT -> ASCII: Unlatch character,0,0,0 or c1,Unlatch character,0,0 or c1,c2,Unlatch character,0 or
  25885. * c1,c2,c3,Unlatch character
  25886. * B256 -> ASCII: without latch after n bytes
  25887. */
  25888. switch (mode) {
  25889. case Mode.ASCII:
  25890. size++;
  25891. if (input.isECI(fromPosition) ||
  25892. MinimalEncoder.isExtendedASCII(input.charAt(fromPosition), input.getFNC1Character())) {
  25893. size++;
  25894. }
  25895. if (previousMode === Mode.C40 ||
  25896. previousMode === Mode.TEXT ||
  25897. previousMode === Mode.X12) {
  25898. size++; // unlatch 254 to ASCII
  25899. }
  25900. break;
  25901. case Mode.B256:
  25902. size++;
  25903. if (previousMode !== Mode.B256) {
  25904. size++; // byte count
  25905. }
  25906. else if (this.getB256Size() === 250) {
  25907. size++; // extra byte count
  25908. }
  25909. if (previousMode === Mode.ASCII) {
  25910. size++; // latch to B256
  25911. }
  25912. else if (previousMode === Mode.C40 ||
  25913. previousMode === Mode.TEXT ||
  25914. previousMode === Mode.X12) {
  25915. size += 2; // unlatch to ASCII, latch to B256
  25916. }
  25917. break;
  25918. case Mode.C40:
  25919. case Mode.TEXT:
  25920. case Mode.X12:
  25921. if (mode === Mode.X12) {
  25922. size += 2;
  25923. }
  25924. else {
  25925. let charLen = [];
  25926. size +=
  25927. MinimalEncoder.getNumberOfC40Words(input, fromPosition, mode === Mode.C40, charLen) * 2;
  25928. }
  25929. if (previousMode === Mode.ASCII || previousMode === Mode.B256) {
  25930. size++; // additional byte for latch from ASCII to this mode
  25931. }
  25932. else if (previousMode !== mode &&
  25933. (previousMode === Mode.C40 ||
  25934. previousMode === Mode.TEXT ||
  25935. previousMode === Mode.X12)) {
  25936. size += 2; // unlatch 254 to ASCII followed by latch to this mode
  25937. }
  25938. break;
  25939. case Mode.EDF:
  25940. size += 3;
  25941. if (previousMode === Mode.ASCII || previousMode === Mode.B256) {
  25942. size++; // additional byte for latch from ASCII to this mode
  25943. }
  25944. else if (previousMode === Mode.C40 ||
  25945. previousMode === Mode.TEXT ||
  25946. previousMode === Mode.X12) {
  25947. size += 2; // unlatch 254 to ASCII followed by latch to this mode
  25948. }
  25949. break;
  25950. }
  25951. this.cachedTotalSize = size;
  25952. }
  25953. // does not count beyond 250
  25954. getB256Size() {
  25955. let cnt = 0;
  25956. let current = this;
  25957. while (current !== null && current.mode === Mode.B256 && cnt <= 250) {
  25958. cnt++;
  25959. current = current.previous;
  25960. }
  25961. return cnt;
  25962. }
  25963. getPreviousStartMode() {
  25964. return this.previous === null ? Mode.ASCII : this.previous.mode;
  25965. }
  25966. getPreviousMode() {
  25967. return this.previous === null ? Mode.ASCII : this.previous.getEndMode();
  25968. }
  25969. /** Returns Mode.ASCII in case that:
  25970. * - Mode is EDIFACT and characterLength is less than 4 or the remaining characters can be encoded in at most 2
  25971. * ASCII bytes.
  25972. * - Mode is C40, TEXT or X12 and the remaining characters can be encoded in at most 1 ASCII byte.
  25973. * Returns mode in all other cases.
  25974. * */
  25975. getEndMode() {
  25976. if (this.mode === Mode.EDF) {
  25977. if (this.characterLength < 4) {
  25978. return Mode.ASCII;
  25979. }
  25980. const lastASCII = this.getLastASCII(); // see 5.2.8.2 EDIFACT encodation Rules
  25981. if (lastASCII > 0 &&
  25982. this.getCodewordsRemaining(this.cachedTotalSize + lastASCII) <=
  25983. 2 - lastASCII) {
  25984. return Mode.ASCII;
  25985. }
  25986. }
  25987. if (this.mode === Mode.C40 ||
  25988. this.mode === Mode.TEXT ||
  25989. this.mode === Mode.X12) {
  25990. // see 5.2.5.2 C40 encodation rules and 5.2.7.2 ANSI X12 encodation rules
  25991. if (this.fromPosition + this.characterLength >= this.input.length() &&
  25992. this.getCodewordsRemaining(this.cachedTotalSize) === 0) {
  25993. return Mode.ASCII;
  25994. }
  25995. const lastASCII = this.getLastASCII();
  25996. if (lastASCII === 1 &&
  25997. this.getCodewordsRemaining(this.cachedTotalSize + 1) === 0) {
  25998. return Mode.ASCII;
  25999. }
  26000. }
  26001. return this.mode;
  26002. }
  26003. getMode() {
  26004. return this.mode;
  26005. }
  26006. /** Peeks ahead and returns 1 if the postfix consists of exactly two digits, 2 if the postfix consists of exactly
  26007. * two consecutive digits and a non extended character or of 4 digits.
  26008. * Returns 0 in any other case
  26009. **/
  26010. getLastASCII() {
  26011. const length = this.input.length();
  26012. const from = this.fromPosition + this.characterLength;
  26013. if (length - from > 4 || from >= length) {
  26014. return 0;
  26015. }
  26016. if (length - from === 1) {
  26017. if (MinimalEncoder.isExtendedASCII(this.input.charAt(from), this.input.getFNC1Character())) {
  26018. return 0;
  26019. }
  26020. return 1;
  26021. }
  26022. if (length - from === 2) {
  26023. if (MinimalEncoder.isExtendedASCII(this.input.charAt(from), this.input.getFNC1Character()) ||
  26024. MinimalEncoder.isExtendedASCII(this.input.charAt(from + 1), this.input.getFNC1Character())) {
  26025. return 0;
  26026. }
  26027. if (HighLevelEncoder$1.isDigit(this.input.charAt(from)) &&
  26028. HighLevelEncoder$1.isDigit(this.input.charAt(from + 1))) {
  26029. return 1;
  26030. }
  26031. return 2;
  26032. }
  26033. if (length - from === 3) {
  26034. if (HighLevelEncoder$1.isDigit(this.input.charAt(from)) &&
  26035. HighLevelEncoder$1.isDigit(this.input.charAt(from + 1)) &&
  26036. !MinimalEncoder.isExtendedASCII(this.input.charAt(from + 2), this.input.getFNC1Character())) {
  26037. return 2;
  26038. }
  26039. if (HighLevelEncoder$1.isDigit(this.input.charAt(from + 1)) &&
  26040. HighLevelEncoder$1.isDigit(this.input.charAt(from + 2)) &&
  26041. !MinimalEncoder.isExtendedASCII(this.input.charAt(from), this.input.getFNC1Character())) {
  26042. return 2;
  26043. }
  26044. return 0;
  26045. }
  26046. if (HighLevelEncoder$1.isDigit(this.input.charAt(from)) &&
  26047. HighLevelEncoder$1.isDigit(this.input.charAt(from + 1)) &&
  26048. HighLevelEncoder$1.isDigit(this.input.charAt(from + 2)) &&
  26049. HighLevelEncoder$1.isDigit(this.input.charAt(from + 3))) {
  26050. return 2;
  26051. }
  26052. return 0;
  26053. }
  26054. /** Returns the capacity in codewords of the smallest symbol that has enough capacity to fit the given minimal
  26055. * number of codewords.
  26056. **/
  26057. getMinSymbolSize(minimum) {
  26058. switch (this.input.getShapeHint()) {
  26059. case 1 /* FORCE_SQUARE */:
  26060. for (const capacity of this.squareCodewordCapacities) {
  26061. if (capacity >= minimum) {
  26062. return capacity;
  26063. }
  26064. }
  26065. break;
  26066. case 2 /* FORCE_RECTANGLE */:
  26067. for (const capacity of this.rectangularCodewordCapacities) {
  26068. if (capacity >= minimum) {
  26069. return capacity;
  26070. }
  26071. }
  26072. break;
  26073. }
  26074. for (const capacity of this.allCodewordCapacities) {
  26075. if (capacity >= minimum) {
  26076. return capacity;
  26077. }
  26078. }
  26079. return this.allCodewordCapacities[this.allCodewordCapacities.length - 1];
  26080. }
  26081. /** Returns the remaining capacity in codewords of the smallest symbol that has enough capacity to fit the given
  26082. * minimal number of codewords.
  26083. **/
  26084. getCodewordsRemaining(minimum) {
  26085. return this.getMinSymbolSize(minimum) - minimum;
  26086. }
  26087. static getBytes(c1, c2) {
  26088. const result = new Uint8Array(c2 ? 2 : 1);
  26089. result[0] = c1;
  26090. if (c2) {
  26091. result[1] = c2;
  26092. }
  26093. return result;
  26094. }
  26095. setC40Word(bytes, offset, c1, c2, c3) {
  26096. const val16 = 1600 * (c1 & 0xff) + 40 * (c2 & 0xff) + (c3 & 0xff) + 1;
  26097. bytes[offset] = val16 / 256;
  26098. bytes[offset + 1] = val16 % 256;
  26099. }
  26100. getX12Value(c) {
  26101. return c === 13
  26102. ? 0
  26103. : c === 42
  26104. ? 1
  26105. : c === 62
  26106. ? 2
  26107. : c === 32
  26108. ? 3
  26109. : c >= 48 && c <= 57
  26110. ? c - 44
  26111. : c >= 65 && c <= 90
  26112. ? c - 51
  26113. : c;
  26114. }
  26115. getX12Words() {
  26116. if (!(this.characterLength % 3 === 0)) {
  26117. throw new Error('X12 words must be a multiple of 3');
  26118. }
  26119. const result = new Uint8Array((this.characterLength / 3) * 2);
  26120. for (let i = 0; i < result.length; i += 2) {
  26121. this.setC40Word(result, i, this.getX12Value(this.input.charAt(this.fromPosition + (i / 2) * 3)), this.getX12Value(this.input.charAt(this.fromPosition + (i / 2) * 3 + 1)), this.getX12Value(this.input.charAt(this.fromPosition + (i / 2) * 3 + 2)));
  26122. }
  26123. return result;
  26124. }
  26125. getShiftValue(c, c40, fnc1) {
  26126. return (c40 && MinimalEncoder.isInC40Shift1Set(c)) ||
  26127. (!c40 && MinimalEncoder.isInTextShift1Set(c))
  26128. ? 0
  26129. : (c40 && MinimalEncoder.isInC40Shift2Set(c, fnc1)) ||
  26130. (!c40 && MinimalEncoder.isInTextShift2Set(c, fnc1))
  26131. ? 1
  26132. : 2;
  26133. }
  26134. getC40Value(c40, setIndex, c, fnc1) {
  26135. if (c === fnc1) {
  26136. if (!(setIndex === 2)) {
  26137. throw new Error('FNC1 cannot be used in C40 shift 2');
  26138. }
  26139. return 27;
  26140. }
  26141. if (c40) {
  26142. return c <= 31
  26143. ? c
  26144. : c === 32
  26145. ? 3
  26146. : c <= 47
  26147. ? c - 33
  26148. : c <= 57
  26149. ? c - 44
  26150. : c <= 64
  26151. ? c - 43
  26152. : c <= 90
  26153. ? c - 51
  26154. : c <= 95
  26155. ? c - 69
  26156. : c <= 127
  26157. ? c - 96
  26158. : c;
  26159. }
  26160. else {
  26161. return c === 0
  26162. ? 0
  26163. : setIndex === 0 && c <= 3
  26164. ? c - 1 // is this a bug in the spec?
  26165. : setIndex === 1 && c <= 31
  26166. ? c
  26167. : c === 32
  26168. ? 3
  26169. : c >= 33 && c <= 47
  26170. ? c - 33
  26171. : c >= 48 && c <= 57
  26172. ? c - 44
  26173. : c >= 58 && c <= 64
  26174. ? c - 43
  26175. : c >= 65 && c <= 90
  26176. ? c - 64
  26177. : c >= 91 && c <= 95
  26178. ? c - 69
  26179. : c === 96
  26180. ? 0
  26181. : c >= 97 && c <= 122
  26182. ? c - 83
  26183. : c >= 123 && c <= 127
  26184. ? c - 96
  26185. : c;
  26186. }
  26187. }
  26188. getC40Words(c40, fnc1) {
  26189. const c40Values = [];
  26190. for (let i = 0; i < this.characterLength; i++) {
  26191. const ci = this.input.charAt(this.fromPosition + i);
  26192. if ((c40 && HighLevelEncoder$1.isNativeC40(ci)) ||
  26193. (!c40 && HighLevelEncoder$1.isNativeText(ci))) {
  26194. c40Values.push(this.getC40Value(c40, 0, ci, fnc1));
  26195. }
  26196. else if (!MinimalEncoder.isExtendedASCII(ci, fnc1)) {
  26197. const shiftValue = this.getShiftValue(ci, c40, fnc1);
  26198. c40Values.push(shiftValue); // Shift[123]
  26199. c40Values.push(this.getC40Value(c40, shiftValue, ci, fnc1));
  26200. }
  26201. else {
  26202. const asciiValue = (ci & 0xff) - 128;
  26203. if ((c40 && HighLevelEncoder$1.isNativeC40(asciiValue)) ||
  26204. (!c40 && HighLevelEncoder$1.isNativeText(asciiValue))) {
  26205. c40Values.push(1); // Shift 2
  26206. c40Values.push(30); // Upper Shift
  26207. c40Values.push(this.getC40Value(c40, 0, asciiValue, fnc1));
  26208. }
  26209. else {
  26210. c40Values.push(1); // Shift 2
  26211. c40Values.push(30); // Upper Shift
  26212. const shiftValue = this.getShiftValue(asciiValue, c40, fnc1);
  26213. c40Values.push(shiftValue); // Shift[123]
  26214. c40Values.push(this.getC40Value(c40, shiftValue, asciiValue, fnc1));
  26215. }
  26216. }
  26217. }
  26218. if (c40Values.length % 3 !== 0) {
  26219. if (!((c40Values.length - 2) % 3 === 0 &&
  26220. this.fromPosition + this.characterLength === this.input.length())) {
  26221. throw new Error('C40 words must be a multiple of 3');
  26222. }
  26223. c40Values.push(0); // pad with 0 (Shift 1)
  26224. }
  26225. const result = new Uint8Array((c40Values.length / 3) * 2);
  26226. let byteIndex = 0;
  26227. for (let i = 0; i < c40Values.length; i += 3) {
  26228. this.setC40Word(result, byteIndex, c40Values[i] & 0xff, c40Values[i + 1] & 0xff, c40Values[i + 2] & 0xff);
  26229. byteIndex += 2;
  26230. }
  26231. return result;
  26232. }
  26233. getEDFBytes() {
  26234. const numberOfThirds = Math.ceil(this.characterLength / 4.0);
  26235. const result = new Uint8Array(numberOfThirds * 3);
  26236. let pos = this.fromPosition;
  26237. const endPos = Math.min(this.fromPosition + this.characterLength - 1, this.input.length() - 1);
  26238. for (let i = 0; i < numberOfThirds; i += 3) {
  26239. const edfValues = [];
  26240. for (let j = 0; j < 4; j++) {
  26241. if (pos <= endPos) {
  26242. edfValues[j] = this.input.charAt(pos++) & 0x3f;
  26243. }
  26244. else {
  26245. edfValues[j] = pos === endPos + 1 ? 0x1f : 0;
  26246. }
  26247. }
  26248. let val24 = edfValues[0] << 18;
  26249. val24 |= edfValues[1] << 12;
  26250. val24 |= edfValues[2] << 6;
  26251. val24 |= edfValues[3];
  26252. result[i] = (val24 >> 16) & 0xff;
  26253. result[i + 1] = (val24 >> 8) & 0xff;
  26254. result[i + 2] = val24 & 0xff;
  26255. }
  26256. return result;
  26257. }
  26258. getLatchBytes() {
  26259. switch (this.getPreviousMode()) {
  26260. case Mode.ASCII:
  26261. case Mode.B256: // after B256 ends (via length) we are back to ASCII
  26262. switch (this.mode) {
  26263. case Mode.B256:
  26264. return Edge.getBytes(231);
  26265. case Mode.C40:
  26266. return Edge.getBytes(230);
  26267. case Mode.TEXT:
  26268. return Edge.getBytes(239);
  26269. case Mode.X12:
  26270. return Edge.getBytes(238);
  26271. case Mode.EDF:
  26272. return Edge.getBytes(240);
  26273. }
  26274. break;
  26275. case Mode.C40:
  26276. case Mode.TEXT:
  26277. case Mode.X12:
  26278. if (this.mode !== this.getPreviousMode()) {
  26279. switch (this.mode) {
  26280. case Mode.ASCII:
  26281. return Edge.getBytes(254);
  26282. case Mode.B256:
  26283. return Edge.getBytes(254, 231);
  26284. case Mode.C40:
  26285. return Edge.getBytes(254, 230);
  26286. case Mode.TEXT:
  26287. return Edge.getBytes(254, 239);
  26288. case Mode.X12:
  26289. return Edge.getBytes(254, 238);
  26290. case Mode.EDF:
  26291. return Edge.getBytes(254, 240);
  26292. }
  26293. }
  26294. break;
  26295. case Mode.EDF:
  26296. // The rightmost EDIFACT edge always contains an unlatch character
  26297. if (this.mode !== Mode.EDF) {
  26298. throw new Error('Cannot switch from EDF to ' + this.mode);
  26299. }
  26300. break;
  26301. }
  26302. return new Uint8Array(0);
  26303. }
  26304. // Important: The function does not return the length bytes (one or two) in case of B256 encoding
  26305. getDataBytes() {
  26306. switch (this.mode) {
  26307. case Mode.ASCII:
  26308. if (this.input.isECI(this.fromPosition)) {
  26309. return Edge.getBytes(241, this.input.getECIValue(this.fromPosition) + 1);
  26310. }
  26311. else if (MinimalEncoder.isExtendedASCII(this.input.charAt(this.fromPosition), this.input.getFNC1Character())) {
  26312. return Edge.getBytes(235, this.input.charAt(this.fromPosition) - 127);
  26313. }
  26314. else if (this.characterLength === 2) {
  26315. return Edge.getBytes(this.input.charAt(this.fromPosition) * 10 +
  26316. this.input.charAt(this.fromPosition + 1) +
  26317. 130);
  26318. }
  26319. else if (this.input.isFNC1(this.fromPosition)) {
  26320. return Edge.getBytes(232);
  26321. }
  26322. else {
  26323. return Edge.getBytes(this.input.charAt(this.fromPosition) + 1);
  26324. }
  26325. case Mode.B256:
  26326. return Edge.getBytes(this.input.charAt(this.fromPosition));
  26327. case Mode.C40:
  26328. return this.getC40Words(true, this.input.getFNC1Character());
  26329. case Mode.TEXT:
  26330. return this.getC40Words(false, this.input.getFNC1Character());
  26331. case Mode.X12:
  26332. return this.getX12Words();
  26333. case Mode.EDF:
  26334. return this.getEDFBytes();
  26335. }
  26336. }
  26337. }
  26338. class Input extends MinimalECIInput {
  26339. constructor(stringToEncode, priorityCharset, fnc1, shape, macroId) {
  26340. super(stringToEncode, priorityCharset, fnc1);
  26341. this.shape = shape;
  26342. this.macroId = macroId;
  26343. }
  26344. getMacroId() {
  26345. return this.macroId;
  26346. }
  26347. getShapeHint() {
  26348. return this.shape;
  26349. }
  26350. }
  26351. class DataMatrixWriter {
  26352. encode(contents, format, width, height, hints = null) {
  26353. if (contents.trim() === '') {
  26354. throw new Error('Found empty contents');
  26355. }
  26356. if (format !== BarcodeFormat$1.DATA_MATRIX) {
  26357. throw new Error('Can only encode DATA_MATRIX, but got ' + format);
  26358. }
  26359. if (width < 0 || height < 0) {
  26360. throw new Error('Requested dimensions can\'t be negative: ' + width + 'x' + height);
  26361. }
  26362. // Try to get force shape & min / max size
  26363. let shape = 0 /* FORCE_NONE */;
  26364. let minSize = null;
  26365. let maxSize = null;
  26366. if (hints != null) {
  26367. const requestedShape = hints.get(EncodeHintType$1.DATA_MATRIX_SHAPE);
  26368. if (requestedShape != null) {
  26369. shape = requestedShape;
  26370. }
  26371. const requestedMinSize = hints.get(EncodeHintType$1.MIN_SIZE);
  26372. if (requestedMinSize != null) {
  26373. minSize = requestedMinSize;
  26374. }
  26375. const requestedMaxSize = hints.get(EncodeHintType$1.MAX_SIZE);
  26376. if (requestedMaxSize != null) {
  26377. maxSize = requestedMaxSize;
  26378. }
  26379. }
  26380. // 1. step: Data encodation
  26381. let encoded;
  26382. const hasCompactionHint = hints != null &&
  26383. hints.has(EncodeHintType$1.DATA_MATRIX_COMPACT) &&
  26384. Boolean(hints.get(EncodeHintType$1.DATA_MATRIX_COMPACT).toString());
  26385. if (hasCompactionHint) {
  26386. const hasGS1FormatHint = hints.has(EncodeHintType$1.GS1_FORMAT) &&
  26387. Boolean(hints.get(EncodeHintType$1.GS1_FORMAT).toString());
  26388. let charset = null;
  26389. const hasEncodingHint = hints.has(EncodeHintType$1.CHARACTER_SET);
  26390. if (hasEncodingHint) {
  26391. charset = Charset.forName(hints.get(EncodeHintType$1.CHARACTER_SET).toString());
  26392. }
  26393. encoded = MinimalEncoder.encodeHighLevel(contents, charset, hasGS1FormatHint ? 0x1d : -1, shape);
  26394. }
  26395. else {
  26396. const hasForceC40Hint = hints != null &&
  26397. hints.has(EncodeHintType$1.FORCE_C40) &&
  26398. Boolean(hints.get(EncodeHintType$1.FORCE_C40).toString());
  26399. encoded = HighLevelEncoder$1.encodeHighLevel(contents, shape, minSize, maxSize, hasForceC40Hint);
  26400. }
  26401. const symbolInfo = SymbolInfo.lookup(encoded.length, shape, minSize, maxSize, true);
  26402. // 2. step: ECC generation
  26403. const codewords = ErrorCorrection.encodeECC200(encoded, symbolInfo);
  26404. // 3. step: Module placement in Matrix
  26405. const placement = new DefaultPlacement(codewords, symbolInfo.getSymbolDataWidth(), symbolInfo.getSymbolDataHeight());
  26406. placement.place();
  26407. // 4. step: low-level encoding
  26408. return this.encodeLowLevel(placement, symbolInfo, width, height);
  26409. }
  26410. /**
  26411. * Encode the given symbol info to a bit matrix.
  26412. *
  26413. * @param placement The DataMatrix placement.
  26414. * @param symbolInfo The symbol info to encode.
  26415. * @return The bit matrix generated.
  26416. */
  26417. encodeLowLevel(placement, symbolInfo, width, height) {
  26418. const symbolWidth = symbolInfo.getSymbolDataWidth();
  26419. const symbolHeight = symbolInfo.getSymbolDataHeight();
  26420. const matrix = new ByteMatrix(symbolInfo.getSymbolWidth(), symbolInfo.getSymbolHeight());
  26421. let matrixY = 0;
  26422. for (let y = 0; y < symbolHeight; y++) {
  26423. // Fill the top edge with alternate 0 / 1
  26424. let matrixX;
  26425. if (y % symbolInfo.matrixHeight === 0) {
  26426. matrixX = 0;
  26427. for (let x = 0; x < symbolInfo.getSymbolWidth(); x++) {
  26428. matrix.setBoolean(matrixX, matrixY, x % 2 === 0);
  26429. matrixX++;
  26430. }
  26431. matrixY++;
  26432. }
  26433. matrixX = 0;
  26434. for (let x = 0; x < symbolWidth; x++) {
  26435. // Fill the right edge with full 1
  26436. if (x % symbolInfo.matrixWidth === 0) {
  26437. matrix.setBoolean(matrixX, matrixY, true);
  26438. matrixX++;
  26439. }
  26440. matrix.setBoolean(matrixX, matrixY, placement.getBit(x, y));
  26441. matrixX++;
  26442. // Fill the right edge with alternate 0 / 1
  26443. if (x % symbolInfo.matrixWidth === symbolInfo.matrixWidth - 1) {
  26444. matrix.setBoolean(matrixX, matrixY, y % 2 === 0);
  26445. matrixX++;
  26446. }
  26447. }
  26448. matrixY++;
  26449. // Fill the bottom edge with full 1
  26450. if (y % symbolInfo.matrixHeight === symbolInfo.matrixHeight - 1) {
  26451. matrixX = 0;
  26452. for (let x = 0; x < symbolInfo.getSymbolWidth(); x++) {
  26453. matrix.setBoolean(matrixX, matrixY, true);
  26454. matrixX++;
  26455. }
  26456. matrixY++;
  26457. }
  26458. }
  26459. return this.convertByteMatrixToBitMatrix(matrix, width, height);
  26460. }
  26461. /**
  26462. * Convert the ByteMatrix to BitMatrix.
  26463. *
  26464. * @param reqHeight The requested height of the image (in pixels) with the Datamatrix code
  26465. * @param reqWidth The requested width of the image (in pixels) with the Datamatrix code
  26466. * @param matrix The input matrix.
  26467. * @return The output matrix.
  26468. */
  26469. convertByteMatrixToBitMatrix(matrix, reqWidth, reqHeight) {
  26470. const matrixWidth = matrix.getWidth();
  26471. const matrixHeight = matrix.getHeight();
  26472. const outputWidth = Math.max(reqWidth, matrixWidth);
  26473. const outputHeight = Math.max(reqHeight, matrixHeight);
  26474. const multiple = Math.min(outputWidth / matrixWidth, outputHeight / matrixHeight);
  26475. let leftPadding = (outputWidth - matrixWidth * multiple) / 2;
  26476. let topPadding = (outputHeight - matrixHeight * multiple) / 2;
  26477. let output;
  26478. // remove padding if requested width and height are too small
  26479. if (reqHeight < matrixHeight || reqWidth < matrixWidth) {
  26480. leftPadding = 0;
  26481. topPadding = 0;
  26482. output = new BitMatrix(matrixWidth, matrixHeight);
  26483. }
  26484. else {
  26485. output = new BitMatrix(reqWidth, reqHeight);
  26486. }
  26487. output.clear();
  26488. for (let inputY = 0, outputY = topPadding; inputY < matrixHeight; inputY++, outputY += multiple) {
  26489. // Write the contents of this row of the bytematrix
  26490. for (let inputX = 0, outputX = leftPadding; inputX < matrixWidth; inputX++, outputX += multiple) {
  26491. if (matrix.get(inputX, inputY) === 1) {
  26492. output.setRegion(outputX, outputY, multiple, multiple);
  26493. }
  26494. }
  26495. }
  26496. return output;
  26497. }
  26498. }
  26499. /*
  26500. * Copyright 2013 ZXing authors
  26501. *
  26502. * Licensed under the Apache License, Version 2.0 (the "License");
  26503. * you may not use this file except in compliance with the License.
  26504. * You may obtain a copy of the License at
  26505. *
  26506. * http://www.apache.org/licenses/LICENSE-2.0
  26507. *
  26508. * Unless required by applicable law or agreed to in writing, software
  26509. * distributed under the License is distributed on an "AS IS" BASIS,
  26510. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26511. * See the License for the specific language governing permissions and
  26512. * limitations under the License.
  26513. */
  26514. /**
  26515. * Aztec 2D code representation
  26516. *
  26517. * @author Rustam Abdullaev
  26518. */
  26519. /*public final*/ class AztecCode {
  26520. /**
  26521. * @return {@code true} if compact instead of full mode
  26522. */
  26523. isCompact() {
  26524. return this.compact;
  26525. }
  26526. setCompact(compact) {
  26527. this.compact = compact;
  26528. }
  26529. /**
  26530. * @return size in pixels (width and height)
  26531. */
  26532. getSize() {
  26533. return this.size;
  26534. }
  26535. setSize(size) {
  26536. this.size = size;
  26537. }
  26538. /**
  26539. * @return number of levels
  26540. */
  26541. getLayers() {
  26542. return this.layers;
  26543. }
  26544. setLayers(layers) {
  26545. this.layers = layers;
  26546. }
  26547. /**
  26548. * @return number of data codewords
  26549. */
  26550. getCodeWords() {
  26551. return this.codeWords;
  26552. }
  26553. setCodeWords(codeWords) {
  26554. this.codeWords = codeWords;
  26555. }
  26556. /**
  26557. * @return the symbol image
  26558. */
  26559. getMatrix() {
  26560. return this.matrix;
  26561. }
  26562. setMatrix(matrix) {
  26563. this.matrix = matrix;
  26564. }
  26565. }
  26566. class Collections {
  26567. /**
  26568. * The singletonList(T) method is used to return an immutable list containing only the specified object.
  26569. */
  26570. static singletonList(item) {
  26571. return [item];
  26572. }
  26573. /**
  26574. * The min(Collection<? extends T>, Comparator<? super T>) method is used to return the minimum element of the given collection, according to the order induced by the specified comparator.
  26575. */
  26576. static min(collection, comparator) {
  26577. return collection.sort(comparator)[0];
  26578. }
  26579. }
  26580. /*
  26581. * Copyright 2013 ZXing authors
  26582. *
  26583. * Licensed under the Apache License, Version 2.0 (the "License");
  26584. * you may not use this file except in compliance with the License.
  26585. * You may obtain a copy of the License at
  26586. *
  26587. * http://www.apache.org/licenses/LICENSE-2.0
  26588. *
  26589. * Unless required by applicable law or agreed to in writing, software
  26590. * distributed under the License is distributed on an "AS IS" BASIS,
  26591. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26592. * See the License for the specific language governing permissions and
  26593. * limitations under the License.
  26594. */
  26595. class Token {
  26596. constructor(previous) {
  26597. this.previous = previous;
  26598. }
  26599. getPrevious() {
  26600. return this.previous;
  26601. }
  26602. }
  26603. /*
  26604. * Copyright 2013 ZXing authors
  26605. *
  26606. * Licensed under the Apache License, Version 2.0 (the "License");
  26607. * you may not use this file except in compliance with the License.
  26608. * You may obtain a copy of the License at
  26609. *
  26610. * http://www.apache.org/licenses/LICENSE-2.0
  26611. *
  26612. * Unless required by applicable law or agreed to in writing, software
  26613. * distributed under the License is distributed on an "AS IS" BASIS,
  26614. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26615. * See the License for the specific language governing permissions and
  26616. * limitations under the License.
  26617. */
  26618. /*final*/ class SimpleToken extends Token {
  26619. constructor(previous, value, bitCount) {
  26620. super(previous);
  26621. this.value = value;
  26622. this.bitCount = bitCount;
  26623. }
  26624. /**
  26625. * @Override
  26626. */
  26627. appendTo(bitArray, text) {
  26628. bitArray.appendBits(this.value, this.bitCount);
  26629. }
  26630. add(value, bitCount) {
  26631. return new SimpleToken(this, value, bitCount);
  26632. }
  26633. addBinaryShift(start, byteCount) {
  26634. // no-op can't binary shift a simple token
  26635. console.warn('addBinaryShift on SimpleToken, this simply returns a copy of this token');
  26636. return new SimpleToken(this, start, byteCount);
  26637. }
  26638. /**
  26639. * @Override
  26640. */
  26641. toString() {
  26642. let value = this.value & ((1 << this.bitCount) - 1);
  26643. value |= 1 << this.bitCount;
  26644. return '<' + Integer.toBinaryString(value | (1 << this.bitCount)).substring(1) + '>';
  26645. }
  26646. }
  26647. /*
  26648. * Copyright 2013 ZXing authors
  26649. *
  26650. * Licensed under the Apache License, Version 2.0 (the "License");
  26651. * you may not use this file except in compliance with the License.
  26652. * You may obtain a copy of the License at
  26653. *
  26654. * http://www.apache.org/licenses/LICENSE-2.0
  26655. *
  26656. * Unless required by applicable law or agreed to in writing, software
  26657. * distributed under the License is distributed on an "AS IS" BASIS,
  26658. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26659. * See the License for the specific language governing permissions and
  26660. * limitations under the License.
  26661. */
  26662. /*final*/ class BinaryShiftToken extends SimpleToken {
  26663. constructor(previous, binaryShiftStart, binaryShiftByteCount) {
  26664. super(previous, 0, 0);
  26665. this.binaryShiftStart = binaryShiftStart;
  26666. this.binaryShiftByteCount = binaryShiftByteCount;
  26667. }
  26668. /**
  26669. * @Override
  26670. */
  26671. appendTo(bitArray, text) {
  26672. for (let i = 0; i < this.binaryShiftByteCount; i++) {
  26673. if (i === 0 || (i === 31 && this.binaryShiftByteCount <= 62)) {
  26674. // We need a header before the first character, and before
  26675. // character 31 when the total byte code is <= 62
  26676. bitArray.appendBits(31, 5); // BINARY_SHIFT
  26677. if (this.binaryShiftByteCount > 62) {
  26678. bitArray.appendBits(this.binaryShiftByteCount - 31, 16);
  26679. }
  26680. else if (i === 0) {
  26681. // 1 <= binaryShiftByteCode <= 62
  26682. bitArray.appendBits(Math.min(this.binaryShiftByteCount, 31), 5);
  26683. }
  26684. else {
  26685. // 32 <= binaryShiftCount <= 62 and i == 31
  26686. bitArray.appendBits(this.binaryShiftByteCount - 31, 5);
  26687. }
  26688. }
  26689. bitArray.appendBits(text[this.binaryShiftStart + i], 8);
  26690. }
  26691. }
  26692. addBinaryShift(start, byteCount) {
  26693. // int bitCount = (byteCount * 8) + (byteCount <= 31 ? 10 : byteCount <= 62 ? 20 : 21);
  26694. return new BinaryShiftToken(this, start, byteCount);
  26695. }
  26696. /**
  26697. * @Override
  26698. */
  26699. toString() {
  26700. return '<' + this.binaryShiftStart + '::' + (this.binaryShiftStart + this.binaryShiftByteCount - 1) + '>';
  26701. }
  26702. }
  26703. function addBinaryShift(token, start, byteCount) {
  26704. // int bitCount = (byteCount * 8) + (byteCount <= 31 ? 10 : byteCount <= 62 ? 20 : 21);
  26705. return new BinaryShiftToken(token, start, byteCount);
  26706. }
  26707. function add(token, value, bitCount) {
  26708. return new SimpleToken(token, value, bitCount);
  26709. }
  26710. const /*final*/ MODE_NAMES = [
  26711. 'UPPER',
  26712. 'LOWER',
  26713. 'DIGIT',
  26714. 'MIXED',
  26715. 'PUNCT'
  26716. ];
  26717. const /*final*/ MODE_UPPER = 0; // 5 bits
  26718. const /*final*/ MODE_LOWER = 1; // 5 bits
  26719. const /*final*/ MODE_DIGIT = 2; // 4 bits
  26720. const /*final*/ MODE_MIXED = 3; // 5 bits
  26721. const /*final*/ MODE_PUNCT = 4; // 5 bits
  26722. const EMPTY_TOKEN = new SimpleToken(null, 0, 0);
  26723. // The Latch Table shows, for each pair of Modes, the optimal method for
  26724. // getting from one mode to another. In the worst possible case, this can
  26725. // be up to 14 bits. In the best possible case, we are already there!
  26726. // The high half-word of each entry gives the number of bits.
  26727. // The low half-word of each entry are the actual bits necessary to change
  26728. const LATCH_TABLE = [
  26729. Int32Array.from([
  26730. 0,
  26731. (5 << 16) + 28,
  26732. (5 << 16) + 30,
  26733. (5 << 16) + 29,
  26734. (10 << 16) + (29 << 5) + 30 // UPPER -> MIXED -> PUNCT
  26735. ]),
  26736. Int32Array.from([
  26737. (9 << 16) + (30 << 4) + 14,
  26738. 0,
  26739. (5 << 16) + 30,
  26740. (5 << 16) + 29,
  26741. (10 << 16) + (29 << 5) + 30 // LOWER -> MIXED -> PUNCT
  26742. ]),
  26743. Int32Array.from([
  26744. (4 << 16) + 14,
  26745. (9 << 16) + (14 << 5) + 28,
  26746. 0,
  26747. (9 << 16) + (14 << 5) + 29,
  26748. (14 << 16) + (14 << 10) + (29 << 5) + 30
  26749. // DIGIT -> UPPER -> MIXED -> PUNCT
  26750. ]),
  26751. Int32Array.from([
  26752. (5 << 16) + 29,
  26753. (5 << 16) + 28,
  26754. (10 << 16) + (29 << 5) + 30,
  26755. 0,
  26756. (5 << 16) + 30 // MIXED -> PUNCT
  26757. ]),
  26758. Int32Array.from([
  26759. (5 << 16) + 31,
  26760. (10 << 16) + (31 << 5) + 28,
  26761. (10 << 16) + (31 << 5) + 30,
  26762. (10 << 16) + (31 << 5) + 29,
  26763. 0
  26764. ])
  26765. ];
  26766. function static_SHIFT_TABLE(SHIFT_TABLE) {
  26767. for (let table /*Int32Array*/ of SHIFT_TABLE) {
  26768. Arrays.fill(table, -1);
  26769. }
  26770. SHIFT_TABLE[MODE_UPPER][MODE_PUNCT] = 0;
  26771. SHIFT_TABLE[MODE_LOWER][MODE_PUNCT] = 0;
  26772. SHIFT_TABLE[MODE_LOWER][MODE_UPPER] = 28;
  26773. SHIFT_TABLE[MODE_MIXED][MODE_PUNCT] = 0;
  26774. SHIFT_TABLE[MODE_DIGIT][MODE_PUNCT] = 0;
  26775. SHIFT_TABLE[MODE_DIGIT][MODE_UPPER] = 15;
  26776. return SHIFT_TABLE;
  26777. }
  26778. const /*final*/ SHIFT_TABLE = static_SHIFT_TABLE(Arrays.createInt32Array(6, 6)); // mode shift codes, per table
  26779. /*
  26780. * Copyright 2013 ZXing authors
  26781. *
  26782. * Licensed under the Apache License, Version 2.0 (the "License");
  26783. * you may not use this file except in compliance with the License.
  26784. * You may obtain a copy of the License at
  26785. *
  26786. * http://www.apache.org/licenses/LICENSE-2.0
  26787. *
  26788. * Unless required by applicable law or agreed to in writing, software
  26789. * distributed under the License is distributed on an "AS IS" BASIS,
  26790. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26791. * See the License for the specific language governing permissions and
  26792. * limitations under the License.
  26793. */
  26794. /**
  26795. * State represents all information about a sequence necessary to generate the current output.
  26796. * Note that a state is immutable.
  26797. */
  26798. /*final*/ class State {
  26799. constructor(token, mode, binaryBytes, bitCount) {
  26800. this.token = token;
  26801. this.mode = mode;
  26802. this.binaryShiftByteCount = binaryBytes;
  26803. this.bitCount = bitCount;
  26804. // Make sure we match the token
  26805. // int binaryShiftBitCount = (binaryShiftByteCount * 8) +
  26806. // (binaryShiftByteCount === 0 ? 0 :
  26807. // binaryShiftByteCount <= 31 ? 10 :
  26808. // binaryShiftByteCount <= 62 ? 20 : 21);
  26809. // assert this.bitCount === token.getTotalBitCount() + binaryShiftBitCount;
  26810. }
  26811. getMode() {
  26812. return this.mode;
  26813. }
  26814. getToken() {
  26815. return this.token;
  26816. }
  26817. getBinaryShiftByteCount() {
  26818. return this.binaryShiftByteCount;
  26819. }
  26820. getBitCount() {
  26821. return this.bitCount;
  26822. }
  26823. // Create a new state representing this state with a latch to a (not
  26824. // necessary different) mode, and then a code.
  26825. latchAndAppend(mode, value) {
  26826. // assert binaryShiftByteCount === 0;
  26827. let bitCount = this.bitCount;
  26828. let token = this.token;
  26829. if (mode !== this.mode) {
  26830. let latch = LATCH_TABLE[this.mode][mode];
  26831. token = add(token, latch & 0xffff, latch >> 16);
  26832. bitCount += latch >> 16;
  26833. }
  26834. let latchModeBitCount = mode === MODE_DIGIT ? 4 : 5;
  26835. token = add(token, value, latchModeBitCount);
  26836. return new State(token, mode, 0, bitCount + latchModeBitCount);
  26837. }
  26838. // Create a new state representing this state, with a temporary shift
  26839. // to a different mode to output a single value.
  26840. shiftAndAppend(mode, value) {
  26841. // assert binaryShiftByteCount === 0 && this.mode !== mode;
  26842. let token = this.token;
  26843. let thisModeBitCount = this.mode === MODE_DIGIT ? 4 : 5;
  26844. // Shifts exist only to UPPER and PUNCT, both with tokens size 5.
  26845. token = add(token, SHIFT_TABLE[this.mode][mode], thisModeBitCount);
  26846. token = add(token, value, 5);
  26847. return new State(token, this.mode, 0, this.bitCount + thisModeBitCount + 5);
  26848. }
  26849. // Create a new state representing this state, but an additional character
  26850. // output in Binary Shift mode.
  26851. addBinaryShiftChar(index) {
  26852. let token = this.token;
  26853. let mode = this.mode;
  26854. let bitCount = this.bitCount;
  26855. if (this.mode === MODE_PUNCT || this.mode === MODE_DIGIT) {
  26856. // assert binaryShiftByteCount === 0;
  26857. let latch = LATCH_TABLE[mode][MODE_UPPER];
  26858. token = add(token, latch & 0xffff, latch >> 16);
  26859. bitCount += latch >> 16;
  26860. mode = MODE_UPPER;
  26861. }
  26862. let deltaBitCount = this.binaryShiftByteCount === 0 || this.binaryShiftByteCount === 31
  26863. ? 18
  26864. : this.binaryShiftByteCount === 62
  26865. ? 9
  26866. : 8;
  26867. let result = new State(token, mode, this.binaryShiftByteCount + 1, bitCount + deltaBitCount);
  26868. if (result.binaryShiftByteCount === 2047 + 31) {
  26869. // The string is as long as it's allowed to be. We should end it.
  26870. result = result.endBinaryShift(index + 1);
  26871. }
  26872. return result;
  26873. }
  26874. // Create the state identical to this one, but we are no longer in
  26875. // Binary Shift mode.
  26876. endBinaryShift(index) {
  26877. if (this.binaryShiftByteCount === 0) {
  26878. return this;
  26879. }
  26880. let token = this.token;
  26881. token = addBinaryShift(token, index - this.binaryShiftByteCount, this.binaryShiftByteCount);
  26882. // assert token.getTotalBitCount() === this.bitCount;
  26883. return new State(token, this.mode, 0, this.bitCount);
  26884. }
  26885. // Returns true if "this" state is better (equal: or) to be in than "that"
  26886. // state under all possible circumstances.
  26887. isBetterThanOrEqualTo(other) {
  26888. let newModeBitCount = this.bitCount + (LATCH_TABLE[this.mode][other.mode] >> 16);
  26889. if (this.binaryShiftByteCount < other.binaryShiftByteCount) {
  26890. // add additional B/S encoding cost of other, if any
  26891. newModeBitCount +=
  26892. State.calculateBinaryShiftCost(other) -
  26893. State.calculateBinaryShiftCost(this);
  26894. }
  26895. else if (this.binaryShiftByteCount > other.binaryShiftByteCount &&
  26896. other.binaryShiftByteCount > 0) {
  26897. // maximum possible additional cost (it: h)
  26898. newModeBitCount += 10;
  26899. }
  26900. return newModeBitCount <= other.bitCount;
  26901. }
  26902. toBitArray(text) {
  26903. // Reverse the tokens, so that they are in the order that they should
  26904. // be output
  26905. let symbols = [];
  26906. for (let token = this.endBinaryShift(text.length).token; token !== null; token = token.getPrevious()) {
  26907. symbols.unshift(token);
  26908. }
  26909. let bitArray = new BitArray();
  26910. // Add each token to the result.
  26911. for (const symbol of symbols) {
  26912. symbol.appendTo(bitArray, text);
  26913. }
  26914. // assert bitArray.getSize() === this.bitCount;
  26915. return bitArray;
  26916. }
  26917. /**
  26918. * @Override
  26919. */
  26920. toString() {
  26921. return StringUtils.format('%s bits=%d bytes=%d', MODE_NAMES[this.mode], this.bitCount, this.binaryShiftByteCount);
  26922. }
  26923. static calculateBinaryShiftCost(state) {
  26924. if (state.binaryShiftByteCount > 62) {
  26925. return 21; // B/S with extended length
  26926. }
  26927. if (state.binaryShiftByteCount > 31) {
  26928. return 20; // two B/S
  26929. }
  26930. if (state.binaryShiftByteCount > 0) {
  26931. return 10; // one B/S
  26932. }
  26933. return 0;
  26934. }
  26935. }
  26936. State.INITIAL_STATE = new State(EMPTY_TOKEN, MODE_UPPER, 0, 0);
  26937. function static_CHAR_MAP(CHAR_MAP) {
  26938. const spaceCharCode = StringUtils.getCharCode(' ');
  26939. const pointCharCode = StringUtils.getCharCode('.');
  26940. const commaCharCode = StringUtils.getCharCode(',');
  26941. CHAR_MAP[MODE_UPPER][spaceCharCode] = 1;
  26942. const zUpperCharCode = StringUtils.getCharCode('Z');
  26943. const aUpperCharCode = StringUtils.getCharCode('A');
  26944. for (let c = aUpperCharCode; c <= zUpperCharCode; c++) {
  26945. CHAR_MAP[MODE_UPPER][c] = c - aUpperCharCode + 2;
  26946. }
  26947. CHAR_MAP[MODE_LOWER][spaceCharCode] = 1;
  26948. const zLowerCharCode = StringUtils.getCharCode('z');
  26949. const aLowerCharCode = StringUtils.getCharCode('a');
  26950. for (let c = aLowerCharCode; c <= zLowerCharCode; c++) {
  26951. CHAR_MAP[MODE_LOWER][c] = c - aLowerCharCode + 2;
  26952. }
  26953. CHAR_MAP[MODE_DIGIT][spaceCharCode] = 1;
  26954. const nineCharCode = StringUtils.getCharCode('9');
  26955. const zeroCharCode = StringUtils.getCharCode('0');
  26956. for (let c = zeroCharCode; c <= nineCharCode; c++) {
  26957. CHAR_MAP[MODE_DIGIT][c] = c - zeroCharCode + 2;
  26958. }
  26959. CHAR_MAP[MODE_DIGIT][commaCharCode] = 12;
  26960. CHAR_MAP[MODE_DIGIT][pointCharCode] = 13;
  26961. const mixedTable = [
  26962. '\x00',
  26963. ' ',
  26964. '\x01',
  26965. '\x02',
  26966. '\x03',
  26967. '\x04',
  26968. '\x05',
  26969. '\x06',
  26970. '\x07',
  26971. '\b',
  26972. '\t',
  26973. '\n',
  26974. '\x0b',
  26975. '\f',
  26976. '\r',
  26977. '\x1b',
  26978. '\x1c',
  26979. '\x1d',
  26980. '\x1e',
  26981. '\x1f',
  26982. '@',
  26983. '\\',
  26984. '^',
  26985. '_',
  26986. '`',
  26987. '|',
  26988. '~',
  26989. '\x7f'
  26990. ];
  26991. for (let i = 0; i < mixedTable.length; i++) {
  26992. CHAR_MAP[MODE_MIXED][StringUtils.getCharCode(mixedTable[i])] = i;
  26993. }
  26994. const punctTable = [
  26995. '\x00',
  26996. '\r',
  26997. '\x00',
  26998. '\x00',
  26999. '\x00',
  27000. '\x00',
  27001. '!',
  27002. '\'',
  27003. '#',
  27004. '$',
  27005. '%',
  27006. '&',
  27007. '\'',
  27008. '(',
  27009. ')',
  27010. '*',
  27011. '+',
  27012. ',',
  27013. '-',
  27014. '.',
  27015. '/',
  27016. ':',
  27017. ';',
  27018. '<',
  27019. '=',
  27020. '>',
  27021. '?',
  27022. '[',
  27023. ']',
  27024. '{',
  27025. '}'
  27026. ];
  27027. for (let i = 0; i < punctTable.length; i++) {
  27028. if (StringUtils.getCharCode(punctTable[i]) > 0) {
  27029. CHAR_MAP[MODE_PUNCT][StringUtils.getCharCode(punctTable[i])] = i;
  27030. }
  27031. }
  27032. return CHAR_MAP;
  27033. }
  27034. const CHAR_MAP = static_CHAR_MAP(Arrays.createInt32Array(5, 256));
  27035. /*
  27036. * Copyright 2013 ZXing authors
  27037. *
  27038. * Licensed under the Apache License, Version 2.0 (the "License");
  27039. * you may not use this file except in compliance with the License.
  27040. * You may obtain a copy of the License at
  27041. *
  27042. * http://www.apache.org/licenses/LICENSE-2.0
  27043. *
  27044. * Unless required by applicable law or agreed to in writing, software
  27045. * distributed under the License is distributed on an "AS IS" BASIS,
  27046. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27047. * See the License for the specific language governing permissions and
  27048. * limitations under the License.
  27049. */
  27050. /**
  27051. * This produces nearly optimal encodings of text into the first-level of
  27052. * encoding used by Aztec code.
  27053. *
  27054. * It uses a dynamic algorithm. For each prefix of the string, it determines
  27055. * a set of encodings that could lead to this prefix. We repeatedly add a
  27056. * character and generate a new set of optimal encodings until we have read
  27057. * through the entire input.
  27058. *
  27059. * @author Frank Yellin
  27060. * @author Rustam Abdullaev
  27061. */
  27062. /*public final*/ class HighLevelEncoder {
  27063. constructor(text) {
  27064. this.text = text;
  27065. }
  27066. /**
  27067. * @return text represented by this encoder encoded as a {@link BitArray}
  27068. */
  27069. encode() {
  27070. const spaceCharCode = StringUtils.getCharCode(' ');
  27071. const lineBreakCharCode = StringUtils.getCharCode('\n');
  27072. let states = Collections.singletonList(State.INITIAL_STATE);
  27073. for (let index = 0; index < this.text.length; index++) {
  27074. let pairCode;
  27075. let nextChar = index + 1 < this.text.length ? this.text[index + 1] : 0;
  27076. switch (this.text[index]) {
  27077. case StringUtils.getCharCode('\r'):
  27078. pairCode = nextChar === lineBreakCharCode ? 2 : 0;
  27079. break;
  27080. case StringUtils.getCharCode('.'):
  27081. pairCode = nextChar === spaceCharCode ? 3 : 0;
  27082. break;
  27083. case StringUtils.getCharCode(','):
  27084. pairCode = nextChar === spaceCharCode ? 4 : 0;
  27085. break;
  27086. case StringUtils.getCharCode(':'):
  27087. pairCode = nextChar === spaceCharCode ? 5 : 0;
  27088. break;
  27089. default:
  27090. pairCode = 0;
  27091. }
  27092. if (pairCode > 0) {
  27093. // We have one of the four special PUNCT pairs. Treat them specially.
  27094. // Get a new set of states for the two new characters.
  27095. states = HighLevelEncoder.updateStateListForPair(states, index, pairCode);
  27096. index++;
  27097. }
  27098. else {
  27099. // Get a new set of states for the new character.
  27100. states = this.updateStateListForChar(states, index);
  27101. }
  27102. }
  27103. // We are left with a set of states. Find the shortest one.
  27104. const minState = Collections.min(states, (a, b) => {
  27105. return a.getBitCount() - b.getBitCount();
  27106. });
  27107. // Convert it to a bit array, and return.
  27108. return minState.toBitArray(this.text);
  27109. }
  27110. // We update a set of states for a new character by updating each state
  27111. // for the new character, merging the results, and then removing the
  27112. // non-optimal states.
  27113. updateStateListForChar(states, index) {
  27114. const result = [];
  27115. for (let state /*State*/ of states) {
  27116. this.updateStateForChar(state, index, result);
  27117. }
  27118. return HighLevelEncoder.simplifyStates(result);
  27119. }
  27120. // Return a set of states that represent the possible ways of updating this
  27121. // state for the next character. The resulting set of states are added to
  27122. // the "result" list.
  27123. updateStateForChar(state, index, result) {
  27124. let ch = (this.text[index] & 0xff);
  27125. let charInCurrentTable = CHAR_MAP[state.getMode()][ch] > 0;
  27126. let stateNoBinary = null;
  27127. for (let mode /*int*/ = 0; mode <= MODE_PUNCT; mode++) {
  27128. let charInMode = CHAR_MAP[mode][ch];
  27129. if (charInMode > 0) {
  27130. if (stateNoBinary == null) {
  27131. // Only create stateNoBinary the first time it's required.
  27132. stateNoBinary = state.endBinaryShift(index);
  27133. }
  27134. // Try generating the character by latching to its mode
  27135. if (!charInCurrentTable ||
  27136. mode === state.getMode() ||
  27137. mode === MODE_DIGIT) {
  27138. // If the character is in the current table, we don't want to latch to
  27139. // any other mode except possibly digit (which uses only 4 bits). Any
  27140. // other latch would be equally successful *after* this character, and
  27141. // so wouldn't save any bits.
  27142. const latchState = stateNoBinary.latchAndAppend(mode, charInMode);
  27143. result.push(latchState);
  27144. }
  27145. // Try generating the character by switching to its mode.
  27146. if (!charInCurrentTable &&
  27147. SHIFT_TABLE[state.getMode()][mode] >= 0) {
  27148. // It never makes sense to temporarily shift to another mode if the
  27149. // character exists in the current mode. That can never save bits.
  27150. const shiftState = stateNoBinary.shiftAndAppend(mode, charInMode);
  27151. result.push(shiftState);
  27152. }
  27153. }
  27154. }
  27155. if (state.getBinaryShiftByteCount() > 0 ||
  27156. CHAR_MAP[state.getMode()][ch] === 0) {
  27157. // It's never worthwhile to go into binary shift mode if you're not already
  27158. // in binary shift mode, and the character exists in your current mode.
  27159. // That can never save bits over just outputting the char in the current mode.
  27160. let binaryState = state.addBinaryShiftChar(index);
  27161. result.push(binaryState);
  27162. }
  27163. }
  27164. static updateStateListForPair(states, index, pairCode) {
  27165. const result = [];
  27166. for (let state /*State*/ of states) {
  27167. this.updateStateForPair(state, index, pairCode, result);
  27168. }
  27169. return this.simplifyStates(result);
  27170. }
  27171. static updateStateForPair(state, index, pairCode, result) {
  27172. let stateNoBinary = state.endBinaryShift(index);
  27173. // Possibility 1. Latch to C.MODE_PUNCT, and then append this code
  27174. result.push(stateNoBinary.latchAndAppend(MODE_PUNCT, pairCode));
  27175. if (state.getMode() !== MODE_PUNCT) {
  27176. // Possibility 2. Shift to C.MODE_PUNCT, and then append this code.
  27177. // Every state except C.MODE_PUNCT (handled above) can shift
  27178. result.push(stateNoBinary.shiftAndAppend(MODE_PUNCT, pairCode));
  27179. }
  27180. if (pairCode === 3 || pairCode === 4) {
  27181. // both characters are in DIGITS. Sometimes better to just add two digits
  27182. let digitState = stateNoBinary
  27183. .latchAndAppend(MODE_DIGIT, 16 - pairCode) // period or comma in DIGIT
  27184. .latchAndAppend(MODE_DIGIT, 1); // space in DIGIT
  27185. result.push(digitState);
  27186. }
  27187. if (state.getBinaryShiftByteCount() > 0) {
  27188. // It only makes sense to do the characters as binary if we're already
  27189. // in binary mode.
  27190. let binaryState = state
  27191. .addBinaryShiftChar(index)
  27192. .addBinaryShiftChar(index + 1);
  27193. result.push(binaryState);
  27194. }
  27195. }
  27196. static simplifyStates(states) {
  27197. let result = [];
  27198. for (const newState of states) {
  27199. let add = true;
  27200. for (const oldState of result) {
  27201. if (oldState.isBetterThanOrEqualTo(newState)) {
  27202. add = false;
  27203. break;
  27204. }
  27205. if (newState.isBetterThanOrEqualTo(oldState)) {
  27206. // iterator.remove();
  27207. result = result.filter(x => x !== oldState); // remove old state
  27208. }
  27209. }
  27210. if (add) {
  27211. result.push(newState);
  27212. }
  27213. }
  27214. return result;
  27215. }
  27216. }
  27217. /*
  27218. * Copyright 2013 ZXing authors
  27219. *
  27220. * Licensed under the Apache License, Version 2.0 (the "License");
  27221. * you may not use this file except in compliance with the License.
  27222. * You may obtain a copy of the License at
  27223. *
  27224. * http://www.apache.org/licenses/LICENSE-2.0
  27225. *
  27226. * Unless required by applicable law or agreed to in writing, software
  27227. * distributed under the License is distributed on an "AS IS" BASIS,
  27228. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27229. * See the License for the specific language governing permissions and
  27230. * limitations under the License.
  27231. */
  27232. // package com.google.zxing.aztec.encoder;
  27233. // import com.google.zxing.common.BitArray;
  27234. // import com.google.zxing.common.BitMatrix;
  27235. // import com.google.zxing.common.reedsolomon.GenericGF;
  27236. // import com.google.zxing.common.reedsolomon.ReedSolomonEncoder;
  27237. /**
  27238. * Generates Aztec 2D barcodes.
  27239. *
  27240. * @author Rustam Abdullaev
  27241. */
  27242. /*public final*/ class Encoder {
  27243. constructor() {
  27244. }
  27245. /**
  27246. * Encodes the given binary content as an Aztec symbol
  27247. *
  27248. * @param data input data string
  27249. * @return Aztec symbol matrix with metadata
  27250. */
  27251. static encodeBytes(data) {
  27252. return Encoder.encode(data, Encoder.DEFAULT_EC_PERCENT, Encoder.DEFAULT_AZTEC_LAYERS);
  27253. }
  27254. /**
  27255. * Encodes the given binary content as an Aztec symbol
  27256. *
  27257. * @param data input data string
  27258. * @param minECCPercent minimal percentage of error check words (According to ISO/IEC 24778:2008,
  27259. * a minimum of 23% + 3 words is recommended)
  27260. * @param userSpecifiedLayers if non-zero, a user-specified value for the number of layers
  27261. * @return Aztec symbol matrix with metadata
  27262. */
  27263. static encode(data, minECCPercent, userSpecifiedLayers) {
  27264. // High-level encode
  27265. let bits = new HighLevelEncoder(data).encode();
  27266. // stuff bits and choose symbol size
  27267. let eccBits = Integer.truncDivision((bits.getSize() * minECCPercent), 100) + 11;
  27268. let totalSizeBits = bits.getSize() + eccBits;
  27269. let compact;
  27270. let layers;
  27271. let totalBitsInLayer;
  27272. let wordSize;
  27273. let stuffedBits;
  27274. if (userSpecifiedLayers !== Encoder.DEFAULT_AZTEC_LAYERS) {
  27275. compact = userSpecifiedLayers < 0;
  27276. layers = Math.abs(userSpecifiedLayers);
  27277. if (layers > (compact ? Encoder.MAX_NB_BITS_COMPACT : Encoder.MAX_NB_BITS)) {
  27278. throw new IllegalArgumentException(StringUtils.format('Illegal value %s for layers', userSpecifiedLayers));
  27279. }
  27280. totalBitsInLayer = Encoder.totalBitsInLayer(layers, compact);
  27281. wordSize = Encoder.WORD_SIZE[layers];
  27282. let usableBitsInLayers = totalBitsInLayer - (totalBitsInLayer % wordSize);
  27283. stuffedBits = Encoder.stuffBits(bits, wordSize);
  27284. if (stuffedBits.getSize() + eccBits > usableBitsInLayers) {
  27285. throw new IllegalArgumentException('Data to large for user specified layer');
  27286. }
  27287. if (compact && stuffedBits.getSize() > wordSize * 64) {
  27288. // Compact format only allows 64 data words, though C4 can hold more words than that
  27289. throw new IllegalArgumentException('Data to large for user specified layer');
  27290. }
  27291. }
  27292. else {
  27293. wordSize = 0;
  27294. stuffedBits = null;
  27295. // We look at the possible table sizes in the order Compact1, Compact2, Compact3,
  27296. // Compact4, Normal4,... Normal(i) for i < 4 isn't typically used since Compact(i+1)
  27297. // is the same size, but has more data.
  27298. for (let i /*int*/ = 0;; i++) {
  27299. if (i > Encoder.MAX_NB_BITS) {
  27300. throw new IllegalArgumentException('Data too large for an Aztec code');
  27301. }
  27302. compact = i <= 3;
  27303. layers = compact ? i + 1 : i;
  27304. totalBitsInLayer = Encoder.totalBitsInLayer(layers, compact);
  27305. if (totalSizeBits > totalBitsInLayer) {
  27306. continue;
  27307. }
  27308. // [Re]stuff the bits if this is the first opportunity, or if the
  27309. // wordSize has changed
  27310. if (stuffedBits == null || wordSize !== Encoder.WORD_SIZE[layers]) {
  27311. wordSize = Encoder.WORD_SIZE[layers];
  27312. stuffedBits = Encoder.stuffBits(bits, wordSize);
  27313. }
  27314. let usableBitsInLayers = totalBitsInLayer - (totalBitsInLayer % wordSize);
  27315. if (compact && stuffedBits.getSize() > wordSize * 64) {
  27316. // Compact format only allows 64 data words, though C4 can hold more words than that
  27317. continue;
  27318. }
  27319. if (stuffedBits.getSize() + eccBits <= usableBitsInLayers) {
  27320. break;
  27321. }
  27322. }
  27323. }
  27324. let messageBits = Encoder.generateCheckWords(stuffedBits, totalBitsInLayer, wordSize);
  27325. // generate mode message
  27326. let messageSizeInWords = stuffedBits.getSize() / wordSize;
  27327. let modeMessage = Encoder.generateModeMessage(compact, layers, messageSizeInWords);
  27328. // allocate symbol
  27329. let baseMatrixSize = (compact ? 11 : 14) + layers * 4; // not including alignment lines
  27330. let alignmentMap = new Int32Array(baseMatrixSize);
  27331. let matrixSize;
  27332. if (compact) {
  27333. // no alignment marks in compact mode, alignmentMap is a no-op
  27334. matrixSize = baseMatrixSize;
  27335. for (let i /*int*/ = 0; i < alignmentMap.length; i++) {
  27336. alignmentMap[i] = i;
  27337. }
  27338. }
  27339. else {
  27340. matrixSize = baseMatrixSize + 1 + 2 * Integer.truncDivision((Integer.truncDivision(baseMatrixSize, 2) - 1), 15);
  27341. let origCenter = Integer.truncDivision(baseMatrixSize, 2);
  27342. let center = Integer.truncDivision(matrixSize, 2);
  27343. for (let i /*int*/ = 0; i < origCenter; i++) {
  27344. let newOffset = i + Integer.truncDivision(i, 15);
  27345. alignmentMap[origCenter - i - 1] = center - newOffset - 1;
  27346. alignmentMap[origCenter + i] = center + newOffset + 1;
  27347. }
  27348. }
  27349. let matrix = new BitMatrix(matrixSize);
  27350. // draw data bits
  27351. for (let i /*int*/ = 0, rowOffset = 0; i < layers; i++) {
  27352. let rowSize = (layers - i) * 4 + (compact ? 9 : 12);
  27353. for (let j /*int*/ = 0; j < rowSize; j++) {
  27354. let columnOffset = j * 2;
  27355. for (let k /*int*/ = 0; k < 2; k++) {
  27356. if (messageBits.get(rowOffset + columnOffset + k)) {
  27357. matrix.set(alignmentMap[i * 2 + k], alignmentMap[i * 2 + j]);
  27358. }
  27359. if (messageBits.get(rowOffset + rowSize * 2 + columnOffset + k)) {
  27360. matrix.set(alignmentMap[i * 2 + j], alignmentMap[baseMatrixSize - 1 - i * 2 - k]);
  27361. }
  27362. if (messageBits.get(rowOffset + rowSize * 4 + columnOffset + k)) {
  27363. matrix.set(alignmentMap[baseMatrixSize - 1 - i * 2 - k], alignmentMap[baseMatrixSize - 1 - i * 2 - j]);
  27364. }
  27365. if (messageBits.get(rowOffset + rowSize * 6 + columnOffset + k)) {
  27366. matrix.set(alignmentMap[baseMatrixSize - 1 - i * 2 - j], alignmentMap[i * 2 + k]);
  27367. }
  27368. }
  27369. }
  27370. rowOffset += rowSize * 8;
  27371. }
  27372. // draw mode message
  27373. Encoder.drawModeMessage(matrix, compact, matrixSize, modeMessage);
  27374. // draw alignment marks
  27375. if (compact) {
  27376. Encoder.drawBullsEye(matrix, Integer.truncDivision(matrixSize, 2), 5);
  27377. }
  27378. else {
  27379. Encoder.drawBullsEye(matrix, Integer.truncDivision(matrixSize, 2), 7);
  27380. for (let i /*int*/ = 0, j = 0; i < Integer.truncDivision(baseMatrixSize, 2) - 1; i += 15, j += 16) {
  27381. for (let k /*int*/ = Integer.truncDivision(matrixSize, 2) & 1; k < matrixSize; k += 2) {
  27382. matrix.set(Integer.truncDivision(matrixSize, 2) - j, k);
  27383. matrix.set(Integer.truncDivision(matrixSize, 2) + j, k);
  27384. matrix.set(k, Integer.truncDivision(matrixSize, 2) - j);
  27385. matrix.set(k, Integer.truncDivision(matrixSize, 2) + j);
  27386. }
  27387. }
  27388. }
  27389. let aztec = new AztecCode();
  27390. aztec.setCompact(compact);
  27391. aztec.setSize(matrixSize);
  27392. aztec.setLayers(layers);
  27393. aztec.setCodeWords(messageSizeInWords);
  27394. aztec.setMatrix(matrix);
  27395. return aztec;
  27396. }
  27397. static drawBullsEye(matrix, center, size) {
  27398. for (let i /*int*/ = 0; i < size; i += 2) {
  27399. for (let j /*int*/ = center - i; j <= center + i; j++) {
  27400. matrix.set(j, center - i);
  27401. matrix.set(j, center + i);
  27402. matrix.set(center - i, j);
  27403. matrix.set(center + i, j);
  27404. }
  27405. }
  27406. matrix.set(center - size, center - size);
  27407. matrix.set(center - size + 1, center - size);
  27408. matrix.set(center - size, center - size + 1);
  27409. matrix.set(center + size, center - size);
  27410. matrix.set(center + size, center - size + 1);
  27411. matrix.set(center + size, center + size - 1);
  27412. }
  27413. static generateModeMessage(compact, layers, messageSizeInWords) {
  27414. let modeMessage = new BitArray();
  27415. if (compact) {
  27416. modeMessage.appendBits(layers - 1, 2);
  27417. modeMessage.appendBits(messageSizeInWords - 1, 6);
  27418. modeMessage = Encoder.generateCheckWords(modeMessage, 28, 4);
  27419. }
  27420. else {
  27421. modeMessage.appendBits(layers - 1, 5);
  27422. modeMessage.appendBits(messageSizeInWords - 1, 11);
  27423. modeMessage = Encoder.generateCheckWords(modeMessage, 40, 4);
  27424. }
  27425. return modeMessage;
  27426. }
  27427. static drawModeMessage(matrix, compact, matrixSize, modeMessage) {
  27428. let center = Integer.truncDivision(matrixSize, 2);
  27429. if (compact) {
  27430. for (let i /*int*/ = 0; i < 7; i++) {
  27431. let offset = center - 3 + i;
  27432. if (modeMessage.get(i)) {
  27433. matrix.set(offset, center - 5);
  27434. }
  27435. if (modeMessage.get(i + 7)) {
  27436. matrix.set(center + 5, offset);
  27437. }
  27438. if (modeMessage.get(20 - i)) {
  27439. matrix.set(offset, center + 5);
  27440. }
  27441. if (modeMessage.get(27 - i)) {
  27442. matrix.set(center - 5, offset);
  27443. }
  27444. }
  27445. }
  27446. else {
  27447. for (let i /*int*/ = 0; i < 10; i++) {
  27448. let offset = center - 5 + i + Integer.truncDivision(i, 5);
  27449. if (modeMessage.get(i)) {
  27450. matrix.set(offset, center - 7);
  27451. }
  27452. if (modeMessage.get(i + 10)) {
  27453. matrix.set(center + 7, offset);
  27454. }
  27455. if (modeMessage.get(29 - i)) {
  27456. matrix.set(offset, center + 7);
  27457. }
  27458. if (modeMessage.get(39 - i)) {
  27459. matrix.set(center - 7, offset);
  27460. }
  27461. }
  27462. }
  27463. }
  27464. static generateCheckWords(bitArray, totalBits, wordSize) {
  27465. // bitArray is guaranteed to be a multiple of the wordSize, so no padding needed
  27466. let messageSizeInWords = bitArray.getSize() / wordSize;
  27467. let rs = new ReedSolomonEncoder(Encoder.getGF(wordSize));
  27468. let totalWords = Integer.truncDivision(totalBits, wordSize);
  27469. let messageWords = Encoder.bitsToWords(bitArray, wordSize, totalWords);
  27470. rs.encode(messageWords, totalWords - messageSizeInWords);
  27471. let startPad = totalBits % wordSize;
  27472. let messageBits = new BitArray();
  27473. messageBits.appendBits(0, startPad);
  27474. for (const messageWord /*: int*/ of Array.from(messageWords)) {
  27475. messageBits.appendBits(messageWord, wordSize);
  27476. }
  27477. return messageBits;
  27478. }
  27479. static bitsToWords(stuffedBits, wordSize, totalWords) {
  27480. let message = new Int32Array(totalWords);
  27481. let i;
  27482. let n;
  27483. for (i = 0, n = stuffedBits.getSize() / wordSize; i < n; i++) {
  27484. let value = 0;
  27485. for (let j /*int*/ = 0; j < wordSize; j++) {
  27486. value |= stuffedBits.get(i * wordSize + j) ? (1 << wordSize - j - 1) : 0;
  27487. }
  27488. message[i] = value;
  27489. }
  27490. return message;
  27491. }
  27492. static getGF(wordSize) {
  27493. switch (wordSize) {
  27494. case 4:
  27495. return GenericGF.AZTEC_PARAM;
  27496. case 6:
  27497. return GenericGF.AZTEC_DATA_6;
  27498. case 8:
  27499. return GenericGF.AZTEC_DATA_8;
  27500. case 10:
  27501. return GenericGF.AZTEC_DATA_10;
  27502. case 12:
  27503. return GenericGF.AZTEC_DATA_12;
  27504. default:
  27505. throw new IllegalArgumentException('Unsupported word size ' + wordSize);
  27506. }
  27507. }
  27508. static stuffBits(bits, wordSize) {
  27509. let out = new BitArray();
  27510. let n = bits.getSize();
  27511. let mask = (1 << wordSize) - 2;
  27512. for (let i /*int*/ = 0; i < n; i += wordSize) {
  27513. let word = 0;
  27514. for (let j /*int*/ = 0; j < wordSize; j++) {
  27515. if (i + j >= n || bits.get(i + j)) {
  27516. word |= 1 << (wordSize - 1 - j);
  27517. }
  27518. }
  27519. if ((word & mask) === mask) {
  27520. out.appendBits(word & mask, wordSize);
  27521. i--;
  27522. }
  27523. else if ((word & mask) === 0) {
  27524. out.appendBits(word | 1, wordSize);
  27525. i--;
  27526. }
  27527. else {
  27528. out.appendBits(word, wordSize);
  27529. }
  27530. }
  27531. return out;
  27532. }
  27533. static totalBitsInLayer(layers, compact) {
  27534. return ((compact ? 88 : 112) + 16 * layers) * layers;
  27535. }
  27536. }
  27537. Encoder.DEFAULT_EC_PERCENT = 33; // default minimal percentage of error check words
  27538. Encoder.DEFAULT_AZTEC_LAYERS = 0;
  27539. Encoder.MAX_NB_BITS = 32;
  27540. Encoder.MAX_NB_BITS_COMPACT = 4;
  27541. Encoder.WORD_SIZE = Int32Array.from([
  27542. 4, 6, 6, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  27543. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12
  27544. ]);
  27545. /*
  27546. * Copyright 2013 ZXing authors
  27547. *
  27548. * Licensed under the Apache License, Version 2.0 (the "License");
  27549. * you may not use this file except in compliance with the License.
  27550. * You may obtain a copy of the License at
  27551. *
  27552. * http://www.apache.org/licenses/LICENSE-2.0
  27553. *
  27554. * Unless required by applicable law or agreed to in writing, software
  27555. * distributed under the License is distributed on an "AS IS" BASIS,
  27556. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27557. * See the License for the specific language governing permissions and
  27558. * limitations under the License.
  27559. */
  27560. /**
  27561. * Renders an Aztec code as a {@link BitMatrix}.
  27562. */
  27563. /*public final*/ class AztecWriter {
  27564. // @Override
  27565. encode(contents, format, width, height) {
  27566. return this.encodeWithHints(contents, format, width, height, null);
  27567. }
  27568. // @Override
  27569. encodeWithHints(contents, format, width, height, hints) {
  27570. let charset = StandardCharsets.ISO_8859_1;
  27571. let eccPercent = Encoder.DEFAULT_EC_PERCENT;
  27572. let layers = Encoder.DEFAULT_AZTEC_LAYERS;
  27573. if (hints != null) {
  27574. if (hints.has(EncodeHintType$1.CHARACTER_SET)) {
  27575. charset = Charset.forName(hints.get(EncodeHintType$1.CHARACTER_SET).toString());
  27576. }
  27577. if (hints.has(EncodeHintType$1.ERROR_CORRECTION)) {
  27578. eccPercent = Integer.parseInt(hints.get(EncodeHintType$1.ERROR_CORRECTION).toString());
  27579. }
  27580. if (hints.has(EncodeHintType$1.AZTEC_LAYERS)) {
  27581. layers = Integer.parseInt(hints.get(EncodeHintType$1.AZTEC_LAYERS).toString());
  27582. }
  27583. }
  27584. return AztecWriter.encodeLayers(contents, format, width, height, charset, eccPercent, layers);
  27585. }
  27586. static encodeLayers(contents, format, width, height, charset, eccPercent, layers) {
  27587. if (format !== BarcodeFormat$1.AZTEC) {
  27588. throw new IllegalArgumentException('Can only encode AZTEC, but got ' + format);
  27589. }
  27590. let aztec = Encoder.encode(StringUtils.getBytes(contents, charset), eccPercent, layers);
  27591. return AztecWriter.renderResult(aztec, width, height);
  27592. }
  27593. static renderResult(code, width, height) {
  27594. let input = code.getMatrix();
  27595. if (input == null) {
  27596. throw new IllegalStateException();
  27597. }
  27598. let inputWidth = input.getWidth();
  27599. let inputHeight = input.getHeight();
  27600. let outputWidth = Math.max(width, inputWidth);
  27601. let outputHeight = Math.max(height, inputHeight);
  27602. let multiple = Math.min(outputWidth / inputWidth, outputHeight / inputHeight);
  27603. let leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  27604. let topPadding = (outputHeight - (inputHeight * multiple)) / 2;
  27605. let output = new BitMatrix(outputWidth, outputHeight);
  27606. for (let inputY /*int*/ = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
  27607. // Write the contents of this row of the barcode
  27608. for (let inputX /*int*/ = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
  27609. if (input.get(inputX, inputY)) {
  27610. output.setRegion(outputX, outputY, multiple, multiple);
  27611. }
  27612. }
  27613. }
  27614. return output;
  27615. }
  27616. }
  27617. exports.AbstractExpandedDecoder = AbstractExpandedDecoder;
  27618. exports.ArgumentException = ArgumentException;
  27619. exports.ArithmeticException = ArithmeticException;
  27620. exports.AztecCode = AztecCode;
  27621. exports.AztecCodeReader = AztecReader;
  27622. exports.AztecCodeWriter = AztecWriter;
  27623. exports.AztecDecoder = Decoder$2;
  27624. exports.AztecDetector = Detector$3;
  27625. exports.AztecDetectorResult = AztecDetectorResult;
  27626. exports.AztecEncoder = Encoder;
  27627. exports.AztecHighLevelEncoder = HighLevelEncoder;
  27628. exports.AztecPoint = Point;
  27629. exports.BarcodeFormat = BarcodeFormat$1;
  27630. exports.Binarizer = Binarizer;
  27631. exports.BinaryBitmap = BinaryBitmap;
  27632. exports.BitArray = BitArray;
  27633. exports.BitMatrix = BitMatrix;
  27634. exports.BitSource = BitSource;
  27635. exports.BrowserAztecCodeReader = BrowserAztecCodeReader;
  27636. exports.BrowserBarcodeReader = BrowserBarcodeReader;
  27637. exports.BrowserCodeReader = BrowserCodeReader;
  27638. exports.BrowserDatamatrixCodeReader = BrowserDatamatrixCodeReader;
  27639. exports.BrowserMultiFormatReader = BrowserMultiFormatReader;
  27640. exports.BrowserPDF417Reader = BrowserPDF417Reader;
  27641. exports.BrowserQRCodeReader = BrowserQRCodeReader;
  27642. exports.BrowserQRCodeSvgWriter = BrowserQRCodeSvgWriter;
  27643. exports.CharacterSetECI = CharacterSetECI;
  27644. exports.ChecksumException = ChecksumException;
  27645. exports.CodaBarReader = CodaBarReader;
  27646. exports.Code128Reader = Code128Reader;
  27647. exports.Code39Reader = Code39Reader;
  27648. exports.Code93Reader = Code93Reader;
  27649. exports.DataMatrixDecodedBitStreamParser = DecodedBitStreamParser$2;
  27650. exports.DataMatrixDefaultPlacement = DefaultPlacement;
  27651. exports.DataMatrixErrorCorrection = ErrorCorrection;
  27652. exports.DataMatrixHighLevelEncoder = HighLevelEncoder$1;
  27653. exports.DataMatrixReader = DataMatrixReader;
  27654. exports.DataMatrixSymbolInfo = SymbolInfo;
  27655. exports.DataMatrixWriter = DataMatrixWriter;
  27656. exports.DecodeHintType = DecodeHintType$1;
  27657. exports.DecoderResult = DecoderResult;
  27658. exports.DefaultGridSampler = DefaultGridSampler;
  27659. exports.DetectorResult = DetectorResult;
  27660. exports.EAN13Reader = EAN13Reader;
  27661. exports.EncodeHintType = EncodeHintType$1;
  27662. exports.Exception = Exception;
  27663. exports.FormatException = FormatException;
  27664. exports.GenericGF = GenericGF;
  27665. exports.GenericGFPoly = GenericGFPoly;
  27666. exports.GlobalHistogramBinarizer = GlobalHistogramBinarizer;
  27667. exports.GridSampler = GridSampler;
  27668. exports.GridSamplerInstance = GridSamplerInstance;
  27669. exports.HTMLCanvasElementLuminanceSource = HTMLCanvasElementLuminanceSource;
  27670. exports.HybridBinarizer = HybridBinarizer;
  27671. exports.ITFReader = ITFReader;
  27672. exports.IllegalArgumentException = IllegalArgumentException;
  27673. exports.IllegalStateException = IllegalStateException;
  27674. exports.InvertedLuminanceSource = InvertedLuminanceSource;
  27675. exports.LuminanceSource = LuminanceSource;
  27676. exports.MathUtils = MathUtils;
  27677. exports.MultiFormatOneDReader = MultiFormatOneDReader;
  27678. exports.MultiFormatReader = MultiFormatReader;
  27679. exports.MultiFormatWriter = MultiFormatWriter;
  27680. exports.NotFoundException = NotFoundException;
  27681. exports.OneDReader = OneDReader;
  27682. exports.PDF417DecodedBitStreamParser = DecodedBitStreamParser;
  27683. exports.PDF417DecoderErrorCorrection = ErrorCorrection$1;
  27684. exports.PDF417Reader = PDF417Reader;
  27685. exports.PDF417ResultMetadata = PDF417ResultMetadata;
  27686. exports.PerspectiveTransform = PerspectiveTransform;
  27687. exports.PlanarYUVLuminanceSource = PlanarYUVLuminanceSource;
  27688. exports.QRCodeByteMatrix = ByteMatrix;
  27689. exports.QRCodeDataMask = DataMask;
  27690. exports.QRCodeDecodedBitStreamParser = DecodedBitStreamParser$1;
  27691. exports.QRCodeDecoderErrorCorrectionLevel = ErrorCorrectionLevel;
  27692. exports.QRCodeDecoderFormatInformation = FormatInformation;
  27693. exports.QRCodeEncoder = Encoder$1;
  27694. exports.QRCodeEncoderQRCode = QRCode;
  27695. exports.QRCodeMaskUtil = MaskUtil;
  27696. exports.QRCodeMatrixUtil = MatrixUtil;
  27697. exports.QRCodeMode = Mode$2;
  27698. exports.QRCodeReader = QRCodeReader;
  27699. exports.QRCodeVersion = Version;
  27700. exports.QRCodeWriter = QRCodeWriter;
  27701. exports.RGBLuminanceSource = RGBLuminanceSource;
  27702. exports.RSS14Reader = RSS14Reader;
  27703. exports.RSSExpandedReader = RSSExpandedReader;
  27704. exports.ReaderException = ReaderException;
  27705. exports.ReedSolomonDecoder = ReedSolomonDecoder;
  27706. exports.ReedSolomonEncoder = ReedSolomonEncoder;
  27707. exports.ReedSolomonException = ReedSolomonException;
  27708. exports.Result = Result$1;
  27709. exports.ResultMetadataType = ResultMetadataType$1;
  27710. exports.ResultPoint = ResultPoint;
  27711. exports.StringUtils = StringUtils;
  27712. exports.UnsupportedOperationException = UnsupportedOperationException;
  27713. exports.VideoInputDevice = VideoInputDevice;
  27714. exports.WhiteRectangleDetector = WhiteRectangleDetector;
  27715. exports.WriterException = WriterException;
  27716. exports.ZXingArrays = Arrays;
  27717. exports.ZXingCharset = Charset;
  27718. exports.ZXingInteger = Integer;
  27719. exports.ZXingStandardCharsets = StandardCharsets;
  27720. exports.ZXingStringBuilder = StringBuilder;
  27721. exports.ZXingStringEncoding = StringEncoding;
  27722. exports.ZXingSystem = System;
  27723. exports.createAbstractExpandedDecoder = createDecoder;
  27724. Object.defineProperty(exports, '__esModule', { value: true });
  27725. }));
  27726. //# sourceMappingURL=index.js.map