| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085 |
- //// etcpack v2.74
- ////
- //// NO WARRANTY
- ////
- //// BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE THE PROGRAM IS PROVIDED
- //// "AS IS". ERICSSON MAKES NO REPRESENTATIONS OF ANY KIND, EXTENDS NO
- //// WARRANTIES OR CONDITIONS OF ANY KIND; EITHER EXPRESS, IMPLIED OR
- //// STATUTORY; INCLUDING, BUT NOT LIMITED TO, EXPRESS, IMPLIED OR
- //// STATUTORY WARRANTIES OR CONDITIONS OF TITLE, MERCHANTABILITY,
- //// SATISFACTORY QUALITY, SUITABILITY AND FITNESS FOR A PARTICULAR
- //// PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
- //// PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
- //// THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. ERICSSON
- //// MAKES NO WARRANTY THAT THE MANUFACTURE, SALE, OFFERING FOR SALE,
- //// DISTRIBUTION, LEASE, USE OR IMPORTATION UNDER THE LICENSE WILL BE FREE
- //// FROM INFRINGEMENT OF PATENTS, COPYRIGHTS OR OTHER INTELLECTUAL
- //// PROPERTY RIGHTS OF OTHERS, AND THE VALIDITY OF THE LICENSE IS SUBJECT
- //// TO YOUR SOLE RESPONSIBILITY TO MAKE SUCH DETERMINATION AND ACQUIRE
- //// SUCH LICENSES AS MAY BE NECESSARY WITH RESPECT TO PATENTS, COPYRIGHT
- //// AND OTHER INTELLECTUAL PROPERTY OF THIRD PARTIES.
- ////
- //// FOR THE AVOIDANCE OF DOUBT THE PROGRAM (I) IS NOT LICENSED FOR; (II)
- //// IS NOT DESIGNED FOR OR INTENDED FOR; AND (III) MAY NOT BE USED FOR;
- //// ANY MISSION CRITICAL APPLICATIONS SUCH AS, BUT NOT LIMITED TO
- //// OPERATION OF NUCLEAR OR HEALTHCARE COMPUTER SYSTEMS AND/OR NETWORKS,
- //// AIRCRAFT OR TRAIN CONTROL AND/OR COMMUNICATION SYSTEMS OR ANY OTHER
- //// COMPUTER SYSTEMS AND/OR NETWORKS OR CONTROL AND/OR COMMUNICATION
- //// SYSTEMS ALL IN WHICH CASE THE FAILURE OF THE PROGRAM COULD LEAD TO
- //// DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL, MATERIAL OR ENVIRONMENTAL
- //// DAMAGE. YOUR RIGHTS UNDER THIS LICENSE WILL TERMINATE AUTOMATICALLY
- //// AND IMMEDIATELY WITHOUT NOTICE IF YOU FAIL TO COMPLY WITH THIS
- //// PARAGRAPH.
- ////
- //// IN NO EVENT WILL ERICSSON, BE LIABLE FOR ANY DAMAGES WHATSOEVER,
- //// INCLUDING BUT NOT LIMITED TO PERSONAL INJURY, ANY GENERAL, SPECIAL,
- //// INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR IN
- //// CONNECTION WITH THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
- //// NOT LIMITED TO LOSS OF PROFITS, BUSINESS INTERUPTIONS, OR ANY OTHER
- //// COMMERCIAL DAMAGES OR LOSSES, LOSS OF DATA OR DATA BEING RENDERED
- //// INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF
- //// THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) REGARDLESS OF THE
- //// THEORY OF LIABILITY (CONTRACT, TORT OR OTHERWISE), EVEN IF SUCH HOLDER
- //// OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
- ////
- //// (C) Ericsson AB 2005-2013. All Rights Reserved.
- ////
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <time.h>
- #include <sys/timeb.h>
- #include "image.h"
- // Typedefs
- typedef unsigned char uint8;
- typedef unsigned short uint16;
- typedef short int16;
- // Functions needed for decrompession ---- in etcdec.cxx
- void read_big_endian_2byte_word(unsigned short *blockadr, FILE *f);
- void read_big_endian_4byte_word(unsigned int *blockadr, FILE *f);
- void unstuff57bits(unsigned int planar_word1, unsigned int planar_word2, unsigned int &planar57_word1, unsigned int &planar57_word2);
- void unstuff59bits(unsigned int thumbT_word1, unsigned int thumbT_word2, unsigned int &thumbT59_word1, unsigned int &thumbT59_word2);
- void unstuff58bits(unsigned int thumbH_word1, unsigned int thumbH_word2, unsigned int &thumbH58_word1, unsigned int &thumbH58_word2);
- void decompressColor(int R_B, int G_B, int B_B, uint8 (colors_RGB444)[2][3], uint8 (colors)[2][3]);
- void calculatePaintColors59T(uint8 d, uint8 p, uint8 (colors)[2][3], uint8 (possible_colors)[4][3]);
- void calculatePaintColors58H(uint8 d, uint8 p, uint8 (colors)[2][3], uint8 (possible_colors)[4][3]);
- void decompressBlockTHUMB59T(unsigned int block_part1, unsigned int block_part2, uint8 *img,int width,int height,int startx,int starty);
- void decompressBlockTHUMB58H(unsigned int block_part1, unsigned int block_part2, uint8 *img,int width,int height,int startx,int starty);
- void decompressBlockPlanar57(unsigned int compressed57_1, unsigned int compressed57_2, uint8 *img,int width,int height,int startx,int starty);
- void decompressBlockDiffFlip(unsigned int block_part1, unsigned int block_part2, uint8 *img,int width,int height,int startx,int starty);
- void decompressBlockETC2(unsigned int block_part1, unsigned int block_part2, uint8 *img,int width,int height,int startx,int starty);
- void decompressBlockDifferentialWithAlpha(unsigned int block_part1,unsigned int block_part2, uint8* img, uint8* alpha, int width, int height, int startx, int starty);
- void decompressBlockETC21BitAlpha(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alphaimg, int width,int height,int startx,int starty);
- void decompressBlockTHUMB58HAlpha(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alpha,int width,int height,int startx,int starty);
- void decompressBlockTHUMB59TAlpha(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alpha,int width,int height,int startx,int starty);
- uint8 getbit(uint8 input, int frompos, int topos);
- int clamp(int val);
- void decompressBlockAlpha(uint8* data,uint8* img,int width,int height,int ix,int iy);
- uint16 get16bits11bits(int base, int table, int mul, int index);
- void decompressBlockAlpha16bit(uint8* data,uint8* img,int width,int height,int ix,int iy);
- int16 get16bits11signed(int base, int table, int mul, int index);
- void setupAlphaTable();
- // This source code is quite long. You can make it shorter by not including the
- // code doing the exhaustive code. Then the -slow modes will not work, but the
- // code will be approximately half the number of lines of code.
- // Then the lines between "exhaustive code starts here" and "exhaustive code ends here"
- // can then be removed.
- #define EXHAUSTIVE_CODE_ACTIVE 1
- // Remove warnings for unsafe functions such as strcpy
- #pragma warning(disable : 4996)
- // Remove warnings for conversions between different time variables
- #pragma warning(disable : 4244)
- // Remove warnings for negative or too big shifts
- //#pragma warning(disable : 4293)
-
- #define CLAMP(ll,x,ul) (((x)<(ll)) ? (ll) : (((x)>(ul)) ? (ul) : (x)))
- // The below code works as CLAMP(0, x, 255) if x < 255
- #define CLAMP_LEFT_ZERO(x) ((~(((int)(x))>>31))&(x))
- // The below code works as CLAMP(0, x, 255) if x is in [0,511]
- #define CLAMP_RIGHT_255(x) (((( ((((int)(x))<<23)>>31) ))|(x))&0x000000ff)
- #define SQUARE(x) ((x)*(x))
- #define JAS_ROUND(x) (((x) < 0.0 ) ? ((int)((x)-0.5)) : ((int)((x)+0.5)))
- #define JAS_MIN(a,b) ((a) < (b) ? (a) : (b))
- #define JAS_MAX(a,b) ((a) > (b) ? (a) : (b))
- // The error metric Wr Wg Wb should be definied so that Wr^2 + Wg^2 + Wb^2 = 1.
- // Hence it is easier to first define the squared values and derive the weights
- // as their square-roots.
- #define PERCEPTUAL_WEIGHT_R_SQUARED 0.299
- #define PERCEPTUAL_WEIGHT_G_SQUARED 0.587
- #define PERCEPTUAL_WEIGHT_B_SQUARED 0.114
- #define PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000 299
- #define PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000 587
- #define PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000 114
- #define RED(img,width,x,y) img[3*(y*width+x)+0]
- #define GREEN(img,width,x,y) img[3*(y*width+x)+1]
- #define BLUE(img,width,x,y) img[3*(y*width+x)+2]
- #define SHIFT(size,startpos) ((startpos)-(size)+1)
- #define MASK(size, startpos) (((2<<(size-1))-1) << SHIFT(size,startpos))
- #define PUTBITS( dest, data, size, startpos) dest = ((dest & ~MASK(size, startpos)) | ((data << SHIFT(size, startpos)) & MASK(size,startpos)))
- #define SHIFTHIGH(size, startpos) (((startpos)-32)-(size)+1)
- #define MASKHIGH(size, startpos) (((1<<(size))-1) << SHIFTHIGH(size,startpos))
- #define PUTBITSHIGH(dest, data, size, startpos) dest = ((dest & ~MASKHIGH(size, startpos)) | ((data << SHIFTHIGH(size, startpos)) & MASKHIGH(size,startpos)))
- #define GETBITS(source, size, startpos) (( (source) >> ((startpos)-(size)+1) ) & ((1<<(size)) -1))
- #define GETBITSHIGH(source, size, startpos) (( (source) >> (((startpos)-32)-(size)+1) ) & ((1<<(size)) -1))
- // Thumb macros and definitions
- #define R_BITS59T 4
- #define G_BITS59T 4
- #define B_BITS59T 4
- #define R_BITS58H 4
- #define G_BITS58H 4
- #define B_BITS58H 4
- #define MAXIMUM_ERROR (255*255*16*1000)
- #define R 0
- #define G 1
- #define B 2
- #define BLOCKHEIGHT 4
- #define BLOCKWIDTH 4
- #define BINPOW(power) (1<<(power))
- //#define RADIUS 2
- #define TABLE_BITS_59T 3
- #define TABLE_BITS_58H 3
- // Global tables
- static uint8 table59T[8] = {3,6,11,16,23,32,41,64}; // 3-bit table for the 59 bit T-mode
- static uint8 table58H[8] = {3,6,11,16,23,32,41,64}; // 3-bit table for the 58 bit H-mode
- uint8 weight[3] = {1,1,1}; // Color weight
- // Enums
- static enum{PATTERN_H = 0,
- PATTERN_T = 1};
- static enum{MODE_ETC1, MODE_THUMB_T, MODE_THUMB_H, MODE_PLANAR};
- // The ETC2 package of codecs includes the following codecs:
- //
- // codec enum
- // --------------------------------------------------------
- // GL_COMPRESSED_R11_EAC 0x9270
- // GL_COMPRESSED_SIGNED_R11_EAC 0x9271
- // GL_COMPRESSED_RG11_EAC 0x9272
- // GL_COMPRESSED_SIGNED_RG11_EAC 0x9273
- // GL_COMPRESSED_RGB8_ETC2 0x9274
- // GL_COMPRESSED_SRGB8_ETC2 0x9275
- // GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
- // GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
- // GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278
- // GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
- //
- // The older codec ETC1 is not included in the package
- // GL_ETC1_RGB8_OES 0x8d64
- // but since ETC2 is backwards compatible an ETC1 texture can
- // be decoded using the RGB8_ETC2 enum (0x9274)
- //
- // In a PKM-file, the codecs are stored using the following identifiers
- //
- // identifier value codec
- // --------------------------------------------------------------------
- // ETC1_RGB_NO_MIPMAPS 0 GL_ETC1_RGB8_OES
- // ETC2PACKAGE_RGB_NO_MIPMAPS 1 GL_COMPRESSED_RGB8_ETC2
- // ETC2PACKAGE_RGBA_NO_MIPMAPS_OLD 2, not used -
- // ETC2PACKAGE_RGBA_NO_MIPMAPS 3 GL_COMPRESSED_RGBA8_ETC2_EAC
- // ETC2PACKAGE_RGBA1_NO_MIPMAPS 4 GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
- // ETC2PACKAGE_R_NO_MIPMAPS 5 GL_COMPRESSED_R11_EAC
- // ETC2PACKAGE_RG_NO_MIPMAPS 6 GL_COMPRESSED_RG11_EAC
- // ETC2PACKAGE_R_SIGNED_NO_MIPMAPS 7 GL_COMPRESSED_SIGNED_R11_EAC
- // ETC2PACKAGE_RG_SIGNED_NO_MIPMAPS 8 GL_COMPRESSED_SIGNED_RG11_EAC
- //
- // In the code, the identifiers are not always used strictly. For instance, the
- // identifier ETC2PACKAGE_R_NO_MIPMAPS is sometimes used for both the unsigned
- // (GL_COMPRESSED_R11_EAC) and signed (GL_COMPRESSED_SIGNED_R11_EAC) version of
- // the codec.
- //
- static enum{ETC1_RGB_NO_MIPMAPS,ETC2PACKAGE_RGB_NO_MIPMAPS,ETC2PACKAGE_RGBA_NO_MIPMAPS_OLD,ETC2PACKAGE_RGBA_NO_MIPMAPS,ETC2PACKAGE_RGBA1_NO_MIPMAPS,ETC2PACKAGE_R_NO_MIPMAPS,ETC2PACKAGE_RG_NO_MIPMAPS,ETC2PACKAGE_R_SIGNED_NO_MIPMAPS,ETC2PACKAGE_RG_SIGNED_NO_MIPMAPS,ETC2PACKAGE_sRGB_NO_MIPMAPS,ETC2PACKAGE_sRGBA_NO_MIPMAPS,ETC2PACKAGE_sRGBA1_NO_MIPMAPS};
- static enum {MODE_COMPRESS, MODE_UNCOMPRESS, MODE_PSNR};
- static enum {SPEED_SLOW, SPEED_FAST, SPEED_MEDIUM};
- static enum {METRIC_PERCEPTUAL, METRIC_NONPERCEPTUAL};
- static enum {CODEC_ETC, CODEC_ETC2};
- int mode = MODE_COMPRESS;
- int speed = SPEED_FAST;
- int metric = METRIC_PERCEPTUAL;
- int codec = CODEC_ETC2;
- int format = ETC2PACKAGE_RGB_NO_MIPMAPS;
- int verbose = true;
- extern int formatSigned;
- int ktxFile=0;
- bool first_time_message = true;
- static int scramble[4] = {3, 2, 0, 1};
- static int unscramble[4] = {2, 3, 1, 0};
- typedef struct KTX_header_t
- {
- uint8 identifier[12];
- unsigned int endianness;
- unsigned int glType;
- unsigned int glTypeSize;
- unsigned int glFormat;
- unsigned int glInternalFormat;
- unsigned int glBaseInternalFormat;
- unsigned int pixelWidth;
- unsigned int pixelHeight;
- unsigned int pixelDepth;
- unsigned int numberOfArrayElements;
- unsigned int numberOfFaces;
- unsigned int numberOfMipmapLevels;
- unsigned int bytesOfKeyValueData;
- }
- KTX_header;
- #define KTX_IDENTIFIER_REF { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A }
- #define KTX_ENDIAN_REF (0x04030201)
- #define KTX_ENDIAN_REF_REV (0x01020304)
- static enum {GL_R=0x1903,GL_RG=0x8227,GL_RGB=0x1907,GL_RGBA=0x1908};
- #define GL_SRGB 0x8C40
- #define GL_SRGB8 0x8C41
- #define GL_SRGB8_ALPHA8 0x8C43
- #define GL_ETC1_RGB8_OES 0x8d64
- #define GL_COMPRESSED_R11_EAC 0x9270
- #define GL_COMPRESSED_SIGNED_R11_EAC 0x9271
- #define GL_COMPRESSED_RG11_EAC 0x9272
- #define GL_COMPRESSED_SIGNED_RG11_EAC 0x9273
- #define GL_COMPRESSED_RGB8_ETC2 0x9274
- #define GL_COMPRESSED_SRGB8_ETC2 0x9275
- #define GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
- #define GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
- #define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278
- #define GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
- int ktx_identifier[] = KTX_IDENTIFIER_REF;
- //converts indices from |a0|a1|e0|e1|i0|i1|m0|m1|b0|b1|f0|f1|j0|j1|n0|n1|c0|c1|g0|g1|k0|k1|o0|o1|d0|d1|h0|h1|l0|l1|p0|p1| previously used by T- and H-modes
- // into |p0|o0|n0|m0|l0|k0|j0|i0|h0|g0|f0|e0|d0|c0|b0|a0|p1|o1|n1|m1|l1|k1|j1|i1|h1|g1|f1|e1|d1|c1|b1|a1| which should be used for all modes.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int indexConversion(int pixelIndices)
- {
- int correctIndices = 0;
- int LSB[4][4];
- int MSB[4][4];
- int shift=0;
- for(int y=3; y>=0; y--)
- {
- for(int x=3; x>=0; x--)
- {
- LSB[x][y] = (pixelIndices>>shift)&1;
- shift++;
- MSB[x][y] = (pixelIndices>>shift)&1;
- shift++;
- }
- }
- shift=0;
- for(int x=0; x<4; x++)
- {
- for(int y=0; y<4; y++)
- {
- correctIndices|=(LSB[x][y]<<shift);
- correctIndices|=(MSB[x][y]<<(16+shift));
- shift++;
- }
- }
- return correctIndices;
- }
- // Tests if a file exists.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- bool fileExist(char *filename)
- {
- FILE *f=NULL;
- if((f=fopen(filename,"rb"))!=NULL)
- {
- fclose(f);
- return true;
- }
- return false;
- }
- // Expand source image so that it is divisible by a factor of four in the x-dimension.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- bool expandToWidthDivByFour(uint8 *&img, int width, int height, int &expandedwidth, int &expandedheight, int bitrate)
- {
- int wdiv4;
- int xx, yy;
- uint8 *newimg;
- wdiv4 = width /4;
- if( !(wdiv4 *4 == width) )
- {
- expandedwidth = (wdiv4 + 1)*4;
- expandedheight = height;
- newimg=(uint8*) malloc(3*expandedwidth*expandedheight*bitrate/8);
- if(!newimg)
- {
- printf("Could not allocate memory to expand width\n");
- return false;
- }
- // First copy image
- for(yy = 0; yy<height; yy++)
- {
- for(xx = 0; xx < width; xx++)
- {
- //we have 3*bitrate/8 bytes for each pixel..
- for(int i=0; i<3*bitrate/8; i++)
- {
- newimg[(yy * expandedwidth+ xx)*3*bitrate/8 + i] = img[(yy * width+xx)*3*bitrate/8 + i];
- }
- }
- }
- // Then make the last column of pixels the same as the previous column.
- for(yy = 0; yy< height; yy++)
- {
- for(xx = width; xx < expandedwidth; xx++)
- {
- for(int i=0; i<3*bitrate/8; i++)
- {
- newimg[(yy * expandedwidth+xx)*3*bitrate/8 + i] = img[(yy * width+(width-1))*3*bitrate/8 + i];
- }
- }
- }
- // Now free the old image
- free(img);
- // Use the new image
- img = newimg;
- return true;
- }
- else
- {
- printf("Image already of even width\n");
- expandedwidth = width;
- expandedheight = height;
- return false;
- }
- }
- // Expand source image so that it is divisible by a factor of four in the y-dimension.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- bool expandToHeightDivByFour(uint8 *&img, int width, int height, int &expandedwidth, int &expandedheight, int bitrate)
- {
- int hdiv4;
- int xx, yy;
- int numlinesmissing;
- uint8 *newimg;
- hdiv4 = height/4;
- if( !(hdiv4 * 4 == height) )
- {
- expandedwidth = width;
- expandedheight = (hdiv4 + 1) * 4;
- numlinesmissing = expandedheight - height;
- newimg=(uint8*)malloc(3*expandedwidth*expandedheight*bitrate/8);
- if(!newimg)
- {
- printf("Could not allocate memory to expand height\n");
- return false;
- }
-
- // First copy image. No need to reformat data.
- for(xx = 0; xx<3*width*height*bitrate/8; xx++)
- newimg[xx] = img[xx];
- // Then copy up to three lines.
- for(yy = height; yy < height + numlinesmissing; yy++)
- {
- for(xx = 0; xx<width; xx++)
- {
- for(int i=0; i<3*bitrate/8; i++)
- {
- newimg[(yy*width+xx)*3*bitrate/8 + i] = img[((height-1)*width+xx)*3*bitrate/8 + i];
- }
- }
- }
- // Now free the old image;
- free(img);
- // Use the new image:
- img = newimg;
- return true;
- }
- else
- {
- printf("Image height already divisible by four.\n");
- expandedwidth = width;
- expandedheight = height;
- return true;
- }
- }
- // Find the position of a file extension such as .ppm or .pkm
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int find_pos_of_extension(char *src)
- {
- int q=strlen(src);
- while(q>=0) // find file name extension
- {
- if(src[q]=='.') break;
- q--;
- }
- if(q<0)
- return -1;
- else
- return q;
- }
- // Read source file. Does conversion if file format is not .ppm.
- // Will expand file to be divisible by four in the x- and y- dimension.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- bool readSrcFile(char *filename,uint8 *&img,int &width,int &height, int &expandedwidth, int &expandedheight)
- {
- int w1,h1;
- int wdiv4, hdiv4;
- char str[255];
- // Delete temp file if it exists.
- if(fileExist("tmp.ppm"))
- {
- sprintf(str, "del tmp.ppm\n");
- system(str);
- }
- int q = find_pos_of_extension(filename);
- if(!strcmp(&filename[q],".ppm"))
- {
- // Already a .ppm file. Just copy.
- sprintf(str,"copy %s tmp.ppm \n", filename);
- printf("Copying source file to tmp.ppm\n", filename);
- }
- else
- {
- // Converting from other format to .ppm
- //
- // Use your favorite command line image converter program,
- // for instance Image Magick. Just make sure the syntax can
- // be written as below:
- //
- // C:\magick convert source.jpg dest.ppm
- //
- sprintf(str,"magick convert %s tmp.ppm\n", filename);
- printf("Converting source file from %s to .ppm\n", filename);
- }
- // Execute system call
- system(str);
- int bitrate=8;
- if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- bitrate=16;
- if(fReadPPM("tmp.ppm",w1,h1,img,bitrate))
- {
- width=w1;
- height=h1;
- system("del tmp.ppm");
- // Width must be divisible by 4 and height must be
- // divisible by 4. Otherwise, we will expand the image
- wdiv4 = width / 4;
- hdiv4 = height / 4;
- expandedwidth = width;
- expandedheight = height;
- if( !(wdiv4 * 4 == width) )
- {
- printf(" Width = %d is not divisible by four... ", width);
- printf(" expanding image in x-dir... ");
- if(expandToWidthDivByFour(img, width, height, expandedwidth, expandedheight,bitrate))
- {
- printf("OK.\n");
- }
- else
- {
- printf("\n Error: could not expand image\n");
- return false;
- }
- }
- if( !(hdiv4 * 4 == height))
- {
- printf(" Height = %d is not divisible by four... ", height);
- printf(" expanding image in y-dir...");
- if(expandToHeightDivByFour(img, expandedwidth, height, expandedwidth, expandedheight,bitrate))
- {
- printf("OK.\n");
- }
- else
- {
- printf("\n Error: could not expand image\n");
- return false;
- }
- }
- if(!(expandedwidth == width && expandedheight == height))
- printf("Active pixels: %dx%d. Expanded image: %dx%d\n",width,height,expandedwidth,expandedheight);
- return true;
- }
- else
- {
- printf("Could not read tmp.ppm file\n");
- exit(1);
- }
- return false;
- }
- // Reads a file without expanding it to be divisible by 4.
- // Is used when doing PSNR calculation between two files.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- bool readSrcFileNoExpand(char *filename,uint8 *&img,int &width,int &height)
- {
- int w1,h1;
- char str[255];
- // Delete temp file if it exists.
- if(fileExist("tmp.ppm"))
- {
- sprintf(str, "del tmp.ppm\n");
- system(str);
- }
- int q = find_pos_of_extension(filename);
- if(!strcmp(&filename[q],".ppm"))
- {
- // Already a .ppm file. Just copy.
- sprintf(str,"copy %s tmp.ppm \n", filename);
- printf("Copying source file to tmp.ppm\n", filename);
- }
- else
- {
- // Converting from other format to .ppm
- //
- // Use your favorite command line image converter program,
- // for instance Image Magick. Just make sure the syntax can
- // be written as below:
- //
- // C:\magick convert source.jpg dest.ppm
- //
- sprintf(str,"magick convert %s tmp.ppm\n", filename);
- // printf("Converting source file from %s to .ppm\n", filename);
- }
- // Execute system call
- system(str);
- if(fReadPPM("tmp.ppm",w1,h1,img,8))
- {
- width=w1;
- height=h1;
- system("del tmp.ppm");
- return true;
- }
- return false;
- }
- // Parses the arguments from the command line.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void readArguments(int argc,char *argv[],char* src,char *dst)
- {
- int q;
- //new code!! do this in a more nicer way!
- bool srcfound=false,dstfound=false;
- for(int i=1; i<argc; i++)
- {
- //loop through the arguments!
- //first check for flags..
- if(argv[i][0]=='-')
- {
- if(i==argc-1)
- {
- printf("flag missing argument: %s!\n");
- exit(1);
- }
- //handle speed flag
- if(!strcmp(argv[i],"-s"))
- {
- // We have argument -s. Now check for slow, medium or fast.
- if(!strcmp(argv[i+1],"slow"))
- speed = SPEED_SLOW;
- else if(!strcmp(argv[i+1],"medium"))
- speed = SPEED_MEDIUM;
- else if(!strcmp(argv[i+1],"fast"))
- speed = SPEED_FAST;
- else
- {
- printf("Error: %s not part of flag %s\n",argv[i+1], argv[i]);
- exit(1);
- }
- }
- //handle verbose flag
- else if(!strcmp(argv[i],"-v"))
- {
- // We have argument -s. Now check for slow, medium or fast.
- if(!strcmp(argv[i+1],"off"))
- verbose = false;
- else if(!strcmp(argv[i+1],"on"))
- verbose = true;
- else
- {
- printf("Error: %s not part of flag %s\n",argv[i+1], argv[i]);
- exit(1);
- }
- }
- //error metric flag
- else if(!strcmp(argv[i],"-e"))
- {
- // We have argument -e. Now check for perceptual or nonperceptual
- if(!strcmp(argv[i+1],"perceptual"))
- metric = METRIC_PERCEPTUAL;
- else if(!strcmp(argv[i+1],"nonperceptual"))
- metric = METRIC_NONPERCEPTUAL;
- else
- {
- printf("Error: %s not part of flag %s\n",argv[i+1], argv[i]);
- exit(1);
- }
- }
- //codec flag
- else if(!strcmp(argv[i],"-c"))
- {
- // We have argument -c. Now check for perceptual or nonperceptual
- if(!strcmp(argv[i+1],"etc") || !strcmp(argv[i+1],"etc1"))
- codec = CODEC_ETC;
- else if(!strcmp(argv[i+1],"etc2"))
- codec = CODEC_ETC2;
- else
- {
- printf("Error: %s not part of flag %s\n",argv[i+1], argv[i]);
- exit(1);
- }
- }
- //format flag
- else if(!strcmp(argv[i],"-f"))
- {
- if(!strcmp(argv[i+1],"R"))
- format=ETC2PACKAGE_R_NO_MIPMAPS;
- else if(!strcmp(argv[i+1],"RG"))
- format=ETC2PACKAGE_RG_NO_MIPMAPS;
- else if(!strcmp(argv[i+1],"R_signed"))
- {
- format=ETC2PACKAGE_R_NO_MIPMAPS;
- formatSigned=1;
- }
- else if(!strcmp(argv[i+1],"RG_signed"))
- {
- format=ETC2PACKAGE_RG_NO_MIPMAPS;
- formatSigned=1;
- }
- else if(!strcmp(argv[i+1],"RGB"))
- format=ETC2PACKAGE_RGB_NO_MIPMAPS;
- else if(!strcmp(argv[i+1],"sRGB"))
- format=ETC2PACKAGE_sRGB_NO_MIPMAPS;
- else if(!strcmp(argv[i+1],"RGBA")||!strcmp(argv[i+1],"RGBA8"))
- format=ETC2PACKAGE_RGBA_NO_MIPMAPS;
- else if(!strcmp(argv[i+1],"sRGBA")||!strcmp(argv[i+1],"sRGBA8"))
- format=ETC2PACKAGE_sRGBA_NO_MIPMAPS;
- else if(!strcmp(argv[i+1],"RGBA1"))
- format=ETC2PACKAGE_RGBA1_NO_MIPMAPS;
- else if(!strcmp(argv[i+1],"sRGBA1"))
- format=ETC2PACKAGE_sRGBA1_NO_MIPMAPS;
- else
- {
- printf("Error: %s not part of flag %s\n",argv[i+1], argv[i]);
- exit(1);
- }
- }
- else if(!strcmp(argv[i],"-p"))
- {
- mode=MODE_PSNR;
- i--; //ugly way of negating the increment of i done later because -p doesn't have an argument.
- }
- else
- {
- printf("Error: cannot interpret flag %s %s\n",argv[i], argv[i+1]);
- exit(1);
- }
- //don't read the flag argument next iteration..
- i++;
- }
- //this isn't a flag, so must be src or dst
- else
- {
- if(srcfound&&dstfound)
- {
- printf("too many arguments! expecting src, dst; found %s, %s, %s\n",src,dst,argv[i]);
- exit(1);
- }
- else if(srcfound)
- {
- strcpy(dst,argv[i]);
- dstfound=true;
- }
- else
- {
- strcpy(src,argv[i]);
- srcfound=true;
- }
- }
- }
- if(!srcfound&&dstfound)
- {
- printf("too few arguments! expecting src, dst\n");
- exit(1);
- }
- if(mode==MODE_PSNR)
- return;
- //check source/destination.. is this compression or decompression?
- q = find_pos_of_extension(src);
- if(q<0)
- {
- printf("invalid source file: %s\n",src);
- exit(1);
- }
- // If we have etcpack img.pkm img.any
- if(!strncmp(&src[q],".pkm",4))
- {
- // First argument is .pkm. Decompress.
- mode = MODE_UNCOMPRESS; // uncompress from binary file format .pkm
- }
- else if(!strncmp(&src[q],".ktx",4))
- {
- // First argument is .ktx. Decompress.
- mode = MODE_UNCOMPRESS; // uncompress from binary file format .pkm
- ktxFile=true;
- printf("decompressing ktx\n");
- }
- else
- {
- // The first argument was not .pkm. The second argument must then be .pkm.
- q = find_pos_of_extension(dst);
- if(q<0)
- {
- printf("invalid destination file: %s\n",src);
- exit(1);
- }
- if(!strncmp(&dst[q],".pkm",4))
- {
- // Second argument is .pkm. Compress.
- mode = MODE_COMPRESS; // compress to binary file format .pkm
- }
- else if(!strncmp(&dst[q],".ktx",4))
- {
- // Second argument is .ktx. Compress.
- ktxFile=true;
- mode = MODE_COMPRESS; // compress to binary file format .pkm
- printf("compressing to ktx\n");
- }
- else
- {
- printf("source or destination must be a .pkm or .ktx file\n");
- exit(1);
- }
- }
- //do some sanity check stuff..
- if(codec==CODEC_ETC&&format!=ETC2PACKAGE_RGB_NO_MIPMAPS)
- {
- printf("ETC1 codec only supports RGB format\n");
- exit(1);
- }
- else if(codec==CODEC_ETC)
- format=ETC1_RGB_NO_MIPMAPS;
- }
- static int compressParams[16][4];
- const int compressParamsFast[32] = { -8, -2, 2, 8,
- -17, -5, 5, 17,
- -29, -9, 9, 29,
- -42, -13, 13, 42,
- -60, -18, 18, 60,
- -80, -24, 24, 80,
- -106, -33, 33, 106,
- -183, -47, 47, 183};
- bool readCompressParams(void)
- {
- compressParams[0][0] = -8; compressParams[0][1] = -2; compressParams[0][2] = 2; compressParams[0][3] = 8;
- compressParams[1][0] = -8; compressParams[1][1] = -2; compressParams[1][2] = 2; compressParams[1][3] = 8;
- compressParams[2][0] = -17; compressParams[2][1] = -5; compressParams[2][2] = 5; compressParams[2][3] = 17;
- compressParams[3][0] = -17; compressParams[3][1] = -5; compressParams[3][2] = 5; compressParams[3][3] = 17;
- compressParams[4][0] = -29; compressParams[4][1] = -9; compressParams[4][2] = 9; compressParams[4][3] = 29;
- compressParams[5][0] = -29; compressParams[5][1] = -9; compressParams[5][2] = 9; compressParams[5][3] = 29;
- compressParams[6][0] = -42; compressParams[6][1] = -13; compressParams[6][2] = 13; compressParams[6][3] = 42;
- compressParams[7][0] = -42; compressParams[7][1] = -13; compressParams[7][2] = 13; compressParams[7][3] = 42;
- compressParams[8][0] = -60; compressParams[8][1] = -18; compressParams[8][2] = 18; compressParams[8][3] = 60;
- compressParams[9][0] = -60; compressParams[9][1] = -18; compressParams[9][2] = 18; compressParams[9][3] = 60;
- compressParams[10][0] = -80; compressParams[10][1] = -24; compressParams[10][2] = 24; compressParams[10][3] = 80;
- compressParams[11][0] = -80; compressParams[11][1] = -24; compressParams[11][2] = 24; compressParams[11][3] = 80;
- compressParams[12][0] =-106; compressParams[12][1] = -33; compressParams[12][2] = 33; compressParams[12][3] = 106;
- compressParams[13][0] =-106; compressParams[13][1] = -33; compressParams[13][2] = 33; compressParams[13][3] = 106;
- compressParams[14][0] =-183; compressParams[14][1] = -47; compressParams[14][2] = 47; compressParams[14][3] = 183;
- compressParams[15][0] =-183; compressParams[15][1] = -47; compressParams[15][2] = 47; compressParams[15][3] = 183;
-
- return true;
- }
- // Computes the average color in a 2x4 area and returns the average color as a float.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void computeAverageColor2x4noQuantFloat(uint8 *img,int width,int height,int startx,int starty,float *avg_color)
- {
- int r=0,g=0,b=0;
- for(int y=starty; y<starty+4; y++)
- {
- for(int x=startx; x<startx+2; x++)
- {
- r+=RED(img,width,x,y);
- g+=GREEN(img,width,x,y);
- b+=BLUE(img,width,x,y);
- }
- }
- avg_color[0]=(float)(r/8.0);
- avg_color[1]=(float)(g/8.0);
- avg_color[2]=(float)(b/8.0);
- }
- // Computes the average color in a 4x2 area and returns the average color as a float.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void computeAverageColor4x2noQuantFloat(uint8 *img,int width,int height,int startx,int starty,float *avg_color)
- {
- int r=0,g=0,b=0;
- for(int y=starty; y<starty+2; y++)
- {
- for(int x=startx; x<startx+4; x++)
- {
- r+=RED(img,width,x,y);
- g+=GREEN(img,width,x,y);
- b+=BLUE(img,width,x,y);
- }
- }
- avg_color[0]=(float)(r/8.0);
- avg_color[1]=(float)(g/8.0);
- avg_color[2]=(float)(b/8.0);
- }
- // Finds all pixel indices for a 2x4 block.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int compressBlockWithTable2x4(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color,int table,unsigned int *pixel_indices_MSBp, unsigned int *pixel_indices_LSBp)
- {
- uint8 orig[3],approx[3];
- unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
- int sum_error=0;
- int q, i;
- i = 0;
- for(int x=startx; x<startx+2; x++)
- {
- for(int y=starty; y<starty+4; y++)
- {
- int err;
- int best=0;
- int min_error=255*255*3*16;
- orig[0]=RED(img,width,x,y);
- orig[1]=GREEN(img,width,x,y);
- orig[2]=BLUE(img,width,x,y);
- for(q=0;q<4;q++)
- {
- approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
- approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
- approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
- // Here we just use equal weights to R, G and B. Although this will
- // give visually worse results, it will give a better PSNR score.
- err=SQUARE(approx[0]-orig[0]) + SQUARE(approx[1]-orig[1]) + SQUARE(approx[2]-orig[2]);
- if(err<min_error)
- {
- min_error=err;
- best=q;
- }
- }
- pixel_indices = scramble[best];
- PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
- PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
- i++;
- // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
- // so that first bit is sign bit and the other bit is size bit (4 or 12).
- // This means that we have to scramble the bits before storing them.
- sum_error+=min_error;
- }
- }
- *pixel_indices_MSBp = pixel_indices_MSB;
- *pixel_indices_LSBp = pixel_indices_LSB;
- return sum_error;
- }
- #define MAXERR1000 1000*255*255*16
- // Finds all pixel indices for a 2x4 block using perceptual weighting of error.
- // Done using fixed poinit arithmetics where weights are multiplied by 1000.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockWithTable2x4percep1000(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color,int table,unsigned int *pixel_indices_MSBp, unsigned int *pixel_indices_LSBp)
- {
- uint8 orig[3],approx[3];
- unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
- unsigned int sum_error=0;
- int q, i;
- i = 0;
- for(int x=startx; x<startx+2; x++)
- {
- for(int y=starty; y<starty+4; y++)
- {
- unsigned int err;
- int best=0;
- unsigned int min_error=MAXERR1000;
- orig[0]=RED(img,width,x,y);
- orig[1]=GREEN(img,width,x,y);
- orig[2]=BLUE(img,width,x,y);
- for(q=0;q<4;q++)
- {
- approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
- approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
- approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
- // Here we just use equal weights to R, G and B. Although this will
- // give visually worse results, it will give a better PSNR score.
- err = (PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE((approx[0]-orig[0]))
- + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE((approx[1]-orig[1]))
- + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*SQUARE((approx[2]-orig[2])));
- if(err<min_error)
- {
- min_error=err;
- best=q;
- }
- }
- pixel_indices = scramble[best];
- PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
- PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
- i++;
- // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
- // so that first bit is sign bit and the other bit is size bit (4 or 12).
- // This means that we have to scramble the bits before storing them.
-
- sum_error+=min_error;
- }
- }
- *pixel_indices_MSBp = pixel_indices_MSB;
- *pixel_indices_LSBp = pixel_indices_LSB;
- return sum_error;
- }
- // Finds all pixel indices for a 2x4 block using perceptual weighting of error.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- float compressBlockWithTable2x4percep(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color,int table,unsigned int *pixel_indices_MSBp, unsigned int *pixel_indices_LSBp)
- {
- uint8 orig[3],approx[3];
- unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
- float sum_error=0;
- int q, i;
- double wR2 = PERCEPTUAL_WEIGHT_R_SQUARED;
- double wG2 = PERCEPTUAL_WEIGHT_G_SQUARED;
- double wB2 = PERCEPTUAL_WEIGHT_B_SQUARED;
- i = 0;
- for(int x=startx; x<startx+2; x++)
- {
- for(int y=starty; y<starty+4; y++)
- {
- float err;
- int best=0;
- float min_error=255*255*3*16;
- orig[0]=RED(img,width,x,y);
- orig[1]=GREEN(img,width,x,y);
- orig[2]=BLUE(img,width,x,y);
- for(q=0;q<4;q++)
- {
- approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
- approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
- approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
- // Here we just use equal weights to R, G and B. Although this will
- // give visually worse results, it will give a better PSNR score.
- err=(float)(wR2*SQUARE((approx[0]-orig[0])) + (float)wG2*SQUARE((approx[1]-orig[1])) + (float)wB2*SQUARE((approx[2]-orig[2])));
- if(err<min_error)
- {
- min_error=err;
- best=q;
- }
- }
- pixel_indices = scramble[best];
- PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
- PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
- i++;
- // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
- // so that first bit is sign bit and the other bit is size bit (4 or 12).
- // This means that we have to scramble the bits before storing them.
-
- sum_error+=min_error;
- }
- }
- *pixel_indices_MSBp = pixel_indices_MSB;
- *pixel_indices_LSBp = pixel_indices_LSB;
- return sum_error;
- }
- // Finds all pixel indices for a 4x2 block.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int compressBlockWithTable4x2(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color,int table,unsigned int *pixel_indices_MSBp, unsigned int *pixel_indices_LSBp)
- {
- uint8 orig[3],approx[3];
- unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
- int sum_error=0;
- int q;
- int i;
- i = 0;
- for(int x=startx; x<startx+4; x++)
- {
- for(int y=starty; y<starty+2; y++)
- {
- int err;
- int best=0;
- int min_error=255*255*3*16;
- orig[0]=RED(img,width,x,y);
- orig[1]=GREEN(img,width,x,y);
- orig[2]=BLUE(img,width,x,y);
- for(q=0;q<4;q++)
- {
- approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
- approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
- approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
- // Here we just use equal weights to R, G and B. Although this will
- // give visually worse results, it will give a better PSNR score.
- err=SQUARE(approx[0]-orig[0]) + SQUARE(approx[1]-orig[1]) + SQUARE(approx[2]-orig[2]);
- if(err<min_error)
- {
- min_error=err;
- best=q;
- }
- }
- pixel_indices = scramble[best];
- PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
- PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
- i++;
- // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
- // so that first bit is sign bit and the other bit is size bit (4 or 12).
- // This means that we have to scramble the bits before storing them.
- sum_error+=min_error;
- }
- i+=2;
- }
- *pixel_indices_MSBp = pixel_indices_MSB;
- *pixel_indices_LSBp = pixel_indices_LSB;
- return sum_error;
- }
- // Finds all pixel indices for a 4x2 block using perceptual weighting of error.
- // Done using fixed point arithmetics where 1000 corresponds to 1.0.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockWithTable4x2percep1000(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color,int table,unsigned int *pixel_indices_MSBp, unsigned int *pixel_indices_LSBp)
- {
- uint8 orig[3],approx[3];
- unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
- unsigned int sum_error=0;
- int q;
- int i;
- i = 0;
- for(int x=startx; x<startx+4; x++)
- {
- for(int y=starty; y<starty+2; y++)
- {
- unsigned int err;
- int best=0;
- unsigned int min_error=MAXERR1000;
- orig[0]=RED(img,width,x,y);
- orig[1]=GREEN(img,width,x,y);
- orig[2]=BLUE(img,width,x,y);
- for(q=0;q<4;q++)
- {
- approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
- approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
- approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
- // Here we just use equal weights to R, G and B. Although this will
- // give visually worse results, it will give a better PSNR score.
- err = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(approx[0]-orig[0])
- + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(approx[1]-orig[1])
- + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*SQUARE(approx[2]-orig[2]);
- if(err<min_error)
- {
- min_error=err;
- best=q;
- }
- }
- pixel_indices = scramble[best];
- PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
- PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
- i++;
- // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
- // so that first bit is sign bit and the other bit is size bit (4 or 12).
- // This means that we have to scramble the bits before storing them.
- sum_error+=min_error;
- }
- i+=2;
- }
- *pixel_indices_MSBp = pixel_indices_MSB;
- *pixel_indices_LSBp = pixel_indices_LSB;
- return sum_error;
- }
- // Finds all pixel indices for a 4x2 block using perceptual weighting of error.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- float compressBlockWithTable4x2percep(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color,int table,unsigned int *pixel_indices_MSBp, unsigned int *pixel_indices_LSBp)
- {
- uint8 orig[3],approx[3];
- unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
- float sum_error=0;
- int q;
- int i;
- float wR2 = (float) PERCEPTUAL_WEIGHT_R_SQUARED;
- float wG2 = (float) PERCEPTUAL_WEIGHT_G_SQUARED;
- float wB2 = (float) PERCEPTUAL_WEIGHT_B_SQUARED;
- i = 0;
- for(int x=startx; x<startx+4; x++)
- {
- for(int y=starty; y<starty+2; y++)
- {
- float err;
- int best=0;
- float min_error=255*255*3*16;
- orig[0]=RED(img,width,x,y);
- orig[1]=GREEN(img,width,x,y);
- orig[2]=BLUE(img,width,x,y);
- for(q=0;q<4;q++)
- {
- approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
- approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
- approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
- // Here we just use equal weights to R, G and B. Although this will
- // give visually worse results, it will give a better PSNR score.
- err=(float) wR2*SQUARE(approx[0]-orig[0]) + (float)wG2*SQUARE(approx[1]-orig[1]) + (float)wB2*SQUARE(approx[2]-orig[2]);
- if(err<min_error)
- {
- min_error=err;
- best=q;
- }
- }
- pixel_indices = scramble[best];
- PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
- PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
- i++;
- // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
- // so that first bit is sign bit and the other bit is size bit (4 or 12).
- // This means that we have to scramble the bits before storing them.
- sum_error+=min_error;
- }
- i+=2;
- }
- *pixel_indices_MSBp = pixel_indices_MSB;
- *pixel_indices_LSBp = pixel_indices_LSB;
- return sum_error;
- }
- // Table for fast implementation of clamping to the interval [0,255] followed by addition of 255.
- const int clamp_table_plus_255[768] = {0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255, 0+255,
- 0+255, 1+255, 2+255, 3+255, 4+255, 5+255, 6+255, 7+255, 8+255, 9+255, 10+255, 11+255, 12+255, 13+255, 14+255, 15+255, 16+255, 17+255, 18+255, 19+255, 20+255, 21+255, 22+255, 23+255, 24+255, 25+255, 26+255, 27+255, 28+255, 29+255, 30+255, 31+255, 32+255, 33+255, 34+255, 35+255, 36+255, 37+255, 38+255, 39+255, 40+255, 41+255, 42+255, 43+255, 44+255, 45+255, 46+255, 47+255, 48+255, 49+255, 50+255, 51+255, 52+255, 53+255, 54+255, 55+255, 56+255, 57+255, 58+255, 59+255, 60+255, 61+255, 62+255, 63+255, 64+255, 65+255, 66+255, 67+255, 68+255, 69+255, 70+255, 71+255, 72+255, 73+255, 74+255, 75+255, 76+255, 77+255, 78+255, 79+255, 80+255, 81+255, 82+255, 83+255, 84+255, 85+255, 86+255, 87+255, 88+255, 89+255, 90+255, 91+255, 92+255, 93+255, 94+255, 95+255, 96+255, 97+255, 98+255, 99+255, 100+255, 101+255, 102+255, 103+255, 104+255, 105+255, 106+255, 107+255, 108+255, 109+255, 110+255, 111+255, 112+255, 113+255, 114+255, 115+255, 116+255, 117+255, 118+255, 119+255, 120+255, 121+255, 122+255, 123+255, 124+255, 125+255, 126+255, 127+255, 128+255, 129+255, 130+255, 131+255, 132+255, 133+255, 134+255, 135+255, 136+255, 137+255, 138+255, 139+255, 140+255, 141+255, 142+255, 143+255, 144+255, 145+255, 146+255, 147+255, 148+255, 149+255, 150+255, 151+255, 152+255, 153+255, 154+255, 155+255, 156+255, 157+255, 158+255, 159+255, 160+255, 161+255, 162+255, 163+255, 164+255, 165+255, 166+255, 167+255, 168+255, 169+255, 170+255, 171+255, 172+255, 173+255, 174+255, 175+255, 176+255, 177+255, 178+255, 179+255, 180+255, 181+255, 182+255, 183+255, 184+255, 185+255, 186+255, 187+255, 188+255, 189+255, 190+255, 191+255, 192+255, 193+255, 194+255, 195+255, 196+255, 197+255, 198+255, 199+255, 200+255, 201+255, 202+255, 203+255, 204+255, 205+255, 206+255, 207+255, 208+255, 209+255, 210+255, 211+255,
- 212+255, 213+255, 214+255, 215+255, 216+255, 217+255, 218+255, 219+255, 220+255, 221+255, 222+255, 223+255, 224+255, 225+255, 226+255, 227+255, 228+255, 229+255, 230+255, 231+255, 232+255, 233+255, 234+255, 235+255, 236+255, 237+255, 238+255, 239+255, 240+255, 241+255, 242+255, 243+255, 244+255, 245+255, 246+255, 247+255, 248+255, 249+255, 250+255, 251+255, 252+255, 253+255, 254+255, 255+255,
- 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255,
- 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255};
- // Table for fast implementationi of clamping to the interval [0,255]
- const int clamp_table[768] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
- // Table for fast implementation of squaring for numbers in the interval [-255, 255]
- const unsigned int square_table[511] = {65025, 64516, 64009, 63504, 63001, 62500, 62001, 61504, 61009, 60516, 60025, 59536, 59049, 58564, 58081, 57600,
- 57121, 56644, 56169, 55696, 55225, 54756, 54289, 53824, 53361, 52900, 52441, 51984, 51529, 51076, 50625, 50176,
- 49729, 49284, 48841, 48400, 47961, 47524, 47089, 46656, 46225, 45796, 45369, 44944, 44521, 44100, 43681, 43264,
- 42849, 42436, 42025, 41616, 41209, 40804, 40401, 40000, 39601, 39204, 38809, 38416, 38025, 37636, 37249, 36864,
- 36481, 36100, 35721, 35344, 34969, 34596, 34225, 33856, 33489, 33124, 32761, 32400, 32041, 31684, 31329, 30976,
- 30625, 30276, 29929, 29584, 29241, 28900, 28561, 28224, 27889, 27556, 27225, 26896, 26569, 26244, 25921, 25600,
- 25281, 24964, 24649, 24336, 24025, 23716, 23409, 23104, 22801, 22500, 22201, 21904, 21609, 21316, 21025, 20736,
- 20449, 20164, 19881, 19600, 19321, 19044, 18769, 18496, 18225, 17956, 17689, 17424, 17161, 16900, 16641, 16384,
- 16129, 15876, 15625, 15376, 15129, 14884, 14641, 14400, 14161, 13924, 13689, 13456, 13225, 12996, 12769, 12544,
- 12321, 12100, 11881, 11664, 11449, 11236, 11025, 10816, 10609, 10404, 10201, 10000, 9801, 9604, 9409, 9216,
- 9025, 8836, 8649, 8464, 8281, 8100, 7921, 7744, 7569, 7396, 7225, 7056, 6889, 6724, 6561, 6400,
- 6241, 6084, 5929, 5776, 5625, 5476, 5329, 5184, 5041, 4900, 4761, 4624, 4489, 4356, 4225, 4096,
- 3969, 3844, 3721, 3600, 3481, 3364, 3249, 3136, 3025, 2916, 2809, 2704, 2601, 2500, 2401, 2304,
- 2209, 2116, 2025, 1936, 1849, 1764, 1681, 1600, 1521, 1444, 1369, 1296, 1225, 1156, 1089, 1024,
- 961, 900, 841, 784, 729, 676, 625, 576, 529, 484, 441, 400, 361, 324, 289, 256,
- 225, 196, 169, 144, 121, 100, 81, 64, 49, 36, 25, 16, 9, 4, 1,
- 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225,
- 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961,
- 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209,
- 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969,
- 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241,
- 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025,
- 9216, 9409, 9604, 9801, 10000, 10201, 10404, 10609, 10816, 11025, 11236, 11449, 11664, 11881, 12100, 12321,
- 12544, 12769, 12996, 13225, 13456, 13689, 13924, 14161, 14400, 14641, 14884, 15129, 15376, 15625, 15876, 16129,
- 16384, 16641, 16900, 17161, 17424, 17689, 17956, 18225, 18496, 18769, 19044, 19321, 19600, 19881, 20164, 20449,
- 20736, 21025, 21316, 21609, 21904, 22201, 22500, 22801, 23104, 23409, 23716, 24025, 24336, 24649, 24964, 25281,
- 25600, 25921, 26244, 26569, 26896, 27225, 27556, 27889, 28224, 28561, 28900, 29241, 29584, 29929, 30276, 30625,
- 30976, 31329, 31684, 32041, 32400, 32761, 33124, 33489, 33856, 34225, 34596, 34969, 35344, 35721, 36100, 36481,
- 36864, 37249, 37636, 38025, 38416, 38809, 39204, 39601, 40000, 40401, 40804, 41209, 41616, 42025, 42436, 42849,
- 43264, 43681, 44100, 44521, 44944, 45369, 45796, 46225, 46656, 47089, 47524, 47961, 48400, 48841, 49284, 49729,
- 50176, 50625, 51076, 51529, 51984, 52441, 52900, 53361, 53824, 54289, 54756, 55225, 55696, 56169, 56644, 57121,
- 57600, 58081, 58564, 59049, 59536, 60025, 60516, 61009, 61504, 62001, 62500, 63001, 63504, 64009, 64516, 65025};
- // Abbreviated variable names to make below tables smaller in source code size
- #define KR PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000
- #define KG PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000
- #define KB PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000
- // Table for fast implementation of squaring for numbers in the interval [-255, 255] multiplied by the perceptual weight for red.
- const unsigned int square_table_percep_red[511] = {
- 65025*KR, 64516*KR, 64009*KR, 63504*KR, 63001*KR, 62500*KR, 62001*KR, 61504*KR, 61009*KR, 60516*KR, 60025*KR, 59536*KR, 59049*KR, 58564*KR, 58081*KR, 57600*KR,
- 57121*KR, 56644*KR, 56169*KR, 55696*KR, 55225*KR, 54756*KR, 54289*KR, 53824*KR, 53361*KR, 52900*KR, 52441*KR, 51984*KR, 51529*KR, 51076*KR, 50625*KR, 50176*KR,
- 49729*KR, 49284*KR, 48841*KR, 48400*KR, 47961*KR, 47524*KR, 47089*KR, 46656*KR, 46225*KR, 45796*KR, 45369*KR, 44944*KR, 44521*KR, 44100*KR, 43681*KR, 43264*KR,
- 42849*KR, 42436*KR, 42025*KR, 41616*KR, 41209*KR, 40804*KR, 40401*KR, 40000*KR, 39601*KR, 39204*KR, 38809*KR, 38416*KR, 38025*KR, 37636*KR, 37249*KR, 36864*KR,
- 36481*KR, 36100*KR, 35721*KR, 35344*KR, 34969*KR, 34596*KR, 34225*KR, 33856*KR, 33489*KR, 33124*KR, 32761*KR, 32400*KR, 32041*KR, 31684*KR, 31329*KR, 30976*KR,
- 30625*KR, 30276*KR, 29929*KR, 29584*KR, 29241*KR, 28900*KR, 28561*KR, 28224*KR, 27889*KR, 27556*KR, 27225*KR, 26896*KR, 26569*KR, 26244*KR, 25921*KR, 25600*KR,
- 25281*KR, 24964*KR, 24649*KR, 24336*KR, 24025*KR, 23716*KR, 23409*KR, 23104*KR, 22801*KR, 22500*KR, 22201*KR, 21904*KR, 21609*KR, 21316*KR, 21025*KR, 20736*KR,
- 20449*KR, 20164*KR, 19881*KR, 19600*KR, 19321*KR, 19044*KR, 18769*KR, 18496*KR, 18225*KR, 17956*KR, 17689*KR, 17424*KR, 17161*KR, 16900*KR, 16641*KR, 16384*KR,
- 16129*KR, 15876*KR, 15625*KR, 15376*KR, 15129*KR, 14884*KR, 14641*KR, 14400*KR, 14161*KR, 13924*KR, 13689*KR, 13456*KR, 13225*KR, 12996*KR, 12769*KR, 12544*KR,
- 12321*KR, 12100*KR, 11881*KR, 11664*KR, 11449*KR, 11236*KR, 11025*KR, 10816*KR, 10609*KR, 10404*KR, 10201*KR, 10000*KR, 9801*KR, 9604*KR, 9409*KR, 9216*KR,
- 9025*KR, 8836*KR, 8649*KR, 8464*KR, 8281*KR, 8100*KR, 7921*KR, 7744*KR, 7569*KR, 7396*KR, 7225*KR, 7056*KR, 6889*KR, 6724*KR, 6561*KR, 6400*KR,
- 6241*KR, 6084*KR, 5929*KR, 5776*KR, 5625*KR, 5476*KR, 5329*KR, 5184*KR, 5041*KR, 4900*KR, 4761*KR, 4624*KR, 4489*KR, 4356*KR, 4225*KR, 4096*KR,
- 3969*KR, 3844*KR, 3721*KR, 3600*KR, 3481*KR, 3364*KR, 3249*KR, 3136*KR, 3025*KR, 2916*KR, 2809*KR, 2704*KR, 2601*KR, 2500*KR, 2401*KR, 2304*KR,
- 2209*KR, 2116*KR, 2025*KR, 1936*KR, 1849*KR, 1764*KR, 1681*KR, 1600*KR, 1521*KR, 1444*KR, 1369*KR, 1296*KR, 1225*KR, 1156*KR, 1089*KR, 1024*KR,
- 961*KR, 900*KR, 841*KR, 784*KR, 729*KR, 676*KR, 625*KR, 576*KR, 529*KR, 484*KR, 441*KR, 400*KR, 361*KR, 324*KR, 289*KR, 256*KR,
- 225*KR, 196*KR, 169*KR, 144*KR, 121*KR, 100*KR, 81*KR, 64*KR, 49*KR, 36*KR, 25*KR, 16*KR, 9*KR, 4*KR, 1*KR,
- 0*KR, 1*KR, 4*KR, 9*KR, 16*KR, 25*KR, 36*KR, 49*KR, 64*KR, 81*KR, 100*KR, 121*KR, 144*KR, 169*KR, 196*KR, 225*KR,
- 256*KR, 289*KR, 324*KR, 361*KR, 400*KR, 441*KR, 484*KR, 529*KR, 576*KR, 625*KR, 676*KR, 729*KR, 784*KR, 841*KR, 900*KR, 961*KR,
- 1024*KR, 1089*KR, 1156*KR, 1225*KR, 1296*KR, 1369*KR, 1444*KR, 1521*KR, 1600*KR, 1681*KR, 1764*KR, 1849*KR, 1936*KR, 2025*KR, 2116*KR, 2209*KR,
- 2304*KR, 2401*KR, 2500*KR, 2601*KR, 2704*KR, 2809*KR, 2916*KR, 3025*KR, 3136*KR, 3249*KR, 3364*KR, 3481*KR, 3600*KR, 3721*KR, 3844*KR, 3969*KR,
- 4096*KR, 4225*KR, 4356*KR, 4489*KR, 4624*KR, 4761*KR, 4900*KR, 5041*KR, 5184*KR, 5329*KR, 5476*KR, 5625*KR, 5776*KR, 5929*KR, 6084*KR, 6241*KR,
- 6400*KR, 6561*KR, 6724*KR, 6889*KR, 7056*KR, 7225*KR, 7396*KR, 7569*KR, 7744*KR, 7921*KR, 8100*KR, 8281*KR, 8464*KR, 8649*KR, 8836*KR, 9025*KR,
- 9216*KR, 9409*KR, 9604*KR, 9801*KR, 10000*KR, 10201*KR, 10404*KR, 10609*KR, 10816*KR, 11025*KR, 11236*KR, 11449*KR, 11664*KR, 11881*KR, 12100*KR, 12321*KR,
- 12544*KR, 12769*KR, 12996*KR, 13225*KR, 13456*KR, 13689*KR, 13924*KR, 14161*KR, 14400*KR, 14641*KR, 14884*KR, 15129*KR, 15376*KR, 15625*KR, 15876*KR, 16129*KR,
- 16384*KR, 16641*KR, 16900*KR, 17161*KR, 17424*KR, 17689*KR, 17956*KR, 18225*KR, 18496*KR, 18769*KR, 19044*KR, 19321*KR, 19600*KR, 19881*KR, 20164*KR, 20449*KR,
- 20736*KR, 21025*KR, 21316*KR, 21609*KR, 21904*KR, 22201*KR, 22500*KR, 22801*KR, 23104*KR, 23409*KR, 23716*KR, 24025*KR, 24336*KR, 24649*KR, 24964*KR, 25281*KR,
- 25600*KR, 25921*KR, 26244*KR, 26569*KR, 26896*KR, 27225*KR, 27556*KR, 27889*KR, 28224*KR, 28561*KR, 28900*KR, 29241*KR, 29584*KR, 29929*KR, 30276*KR, 30625*KR,
- 30976*KR, 31329*KR, 31684*KR, 32041*KR, 32400*KR, 32761*KR, 33124*KR, 33489*KR, 33856*KR, 34225*KR, 34596*KR, 34969*KR, 35344*KR, 35721*KR, 36100*KR, 36481*KR,
- 36864*KR, 37249*KR, 37636*KR, 38025*KR, 38416*KR, 38809*KR, 39204*KR, 39601*KR, 40000*KR, 40401*KR, 40804*KR, 41209*KR, 41616*KR, 42025*KR, 42436*KR, 42849*KR,
- 43264*KR, 43681*KR, 44100*KR, 44521*KR, 44944*KR, 45369*KR, 45796*KR, 46225*KR, 46656*KR, 47089*KR, 47524*KR, 47961*KR, 48400*KR, 48841*KR, 49284*KR, 49729*KR,
- 50176*KR, 50625*KR, 51076*KR, 51529*KR, 51984*KR, 52441*KR, 52900*KR, 53361*KR, 53824*KR, 54289*KR, 54756*KR, 55225*KR, 55696*KR, 56169*KR, 56644*KR, 57121*KR,
- 57600*KR, 58081*KR, 58564*KR, 59049*KR, 59536*KR, 60025*KR, 60516*KR, 61009*KR, 61504*KR, 62001*KR, 62500*KR, 63001*KR, 63504*KR, 64009*KR, 64516*KR, 65025*KR};
- // Table for fast implementation of squaring for numbers in the interval [-255, 255] multiplied by the perceptual weight for green.
- const unsigned int square_table_percep_green[511] = {
- 65025*KG, 64516*KG, 64009*KG, 63504*KG, 63001*KG, 62500*KG, 62001*KG, 61504*KG, 61009*KG, 60516*KG, 60025*KG, 59536*KG, 59049*KG, 58564*KG, 58081*KG, 57600*KG,
- 57121*KG, 56644*KG, 56169*KG, 55696*KG, 55225*KG, 54756*KG, 54289*KG, 53824*KG, 53361*KG, 52900*KG, 52441*KG, 51984*KG, 51529*KG, 51076*KG, 50625*KG, 50176*KG,
- 49729*KG, 49284*KG, 48841*KG, 48400*KG, 47961*KG, 47524*KG, 47089*KG, 46656*KG, 46225*KG, 45796*KG, 45369*KG, 44944*KG, 44521*KG, 44100*KG, 43681*KG, 43264*KG,
- 42849*KG, 42436*KG, 42025*KG, 41616*KG, 41209*KG, 40804*KG, 40401*KG, 40000*KG, 39601*KG, 39204*KG, 38809*KG, 38416*KG, 38025*KG, 37636*KG, 37249*KG, 36864*KG,
- 36481*KG, 36100*KG, 35721*KG, 35344*KG, 34969*KG, 34596*KG, 34225*KG, 33856*KG, 33489*KG, 33124*KG, 32761*KG, 32400*KG, 32041*KG, 31684*KG, 31329*KG, 30976*KG,
- 30625*KG, 30276*KG, 29929*KG, 29584*KG, 29241*KG, 28900*KG, 28561*KG, 28224*KG, 27889*KG, 27556*KG, 27225*KG, 26896*KG, 26569*KG, 26244*KG, 25921*KG, 25600*KG,
- 25281*KG, 24964*KG, 24649*KG, 24336*KG, 24025*KG, 23716*KG, 23409*KG, 23104*KG, 22801*KG, 22500*KG, 22201*KG, 21904*KG, 21609*KG, 21316*KG, 21025*KG, 20736*KG,
- 20449*KG, 20164*KG, 19881*KG, 19600*KG, 19321*KG, 19044*KG, 18769*KG, 18496*KG, 18225*KG, 17956*KG, 17689*KG, 17424*KG, 17161*KG, 16900*KG, 16641*KG, 16384*KG,
- 16129*KG, 15876*KG, 15625*KG, 15376*KG, 15129*KG, 14884*KG, 14641*KG, 14400*KG, 14161*KG, 13924*KG, 13689*KG, 13456*KG, 13225*KG, 12996*KG, 12769*KG, 12544*KG,
- 12321*KG, 12100*KG, 11881*KG, 11664*KG, 11449*KG, 11236*KG, 11025*KG, 10816*KG, 10609*KG, 10404*KG, 10201*KG, 10000*KG, 9801*KG, 9604*KG, 9409*KG, 9216*KG,
- 9025*KG, 8836*KG, 8649*KG, 8464*KG, 8281*KG, 8100*KG, 7921*KG, 7744*KG, 7569*KG, 7396*KG, 7225*KG, 7056*KG, 6889*KG, 6724*KG, 6561*KG, 6400*KG,
- 6241*KG, 6084*KG, 5929*KG, 5776*KG, 5625*KG, 5476*KG, 5329*KG, 5184*KG, 5041*KG, 4900*KG, 4761*KG, 4624*KG, 4489*KG, 4356*KG, 4225*KG, 4096*KG,
- 3969*KG, 3844*KG, 3721*KG, 3600*KG, 3481*KG, 3364*KG, 3249*KG, 3136*KG, 3025*KG, 2916*KG, 2809*KG, 2704*KG, 2601*KG, 2500*KG, 2401*KG, 2304*KG,
- 2209*KG, 2116*KG, 2025*KG, 1936*KG, 1849*KG, 1764*KG, 1681*KG, 1600*KG, 1521*KG, 1444*KG, 1369*KG, 1296*KG, 1225*KG, 1156*KG, 1089*KG, 1024*KG,
- 961*KG, 900*KG, 841*KG, 784*KG, 729*KG, 676*KG, 625*KG, 576*KG, 529*KG, 484*KG, 441*KG, 400*KG, 361*KG, 324*KG, 289*KG, 256*KG,
- 225*KG, 196*KG, 169*KG, 144*KG, 121*KG, 100*KG, 81*KG, 64*KG, 49*KG, 36*KG, 25*KG, 16*KG, 9*KG, 4*KG, 1*KG,
- 0*KG, 1*KG, 4*KG, 9*KG, 16*KG, 25*KG, 36*KG, 49*KG, 64*KG, 81*KG, 100*KG, 121*KG, 144*KG, 169*KG, 196*KG, 225*KG,
- 256*KG, 289*KG, 324*KG, 361*KG, 400*KG, 441*KG, 484*KG, 529*KG, 576*KG, 625*KG, 676*KG, 729*KG, 784*KG, 841*KG, 900*KG, 961*KG,
- 1024*KG, 1089*KG, 1156*KG, 1225*KG, 1296*KG, 1369*KG, 1444*KG, 1521*KG, 1600*KG, 1681*KG, 1764*KG, 1849*KG, 1936*KG, 2025*KG, 2116*KG, 2209*KG,
- 2304*KG, 2401*KG, 2500*KG, 2601*KG, 2704*KG, 2809*KG, 2916*KG, 3025*KG, 3136*KG, 3249*KG, 3364*KG, 3481*KG, 3600*KG, 3721*KG, 3844*KG, 3969*KG,
- 4096*KG, 4225*KG, 4356*KG, 4489*KG, 4624*KG, 4761*KG, 4900*KG, 5041*KG, 5184*KG, 5329*KG, 5476*KG, 5625*KG, 5776*KG, 5929*KG, 6084*KG, 6241*KG,
- 6400*KG, 6561*KG, 6724*KG, 6889*KG, 7056*KG, 7225*KG, 7396*KG, 7569*KG, 7744*KG, 7921*KG, 8100*KG, 8281*KG, 8464*KG, 8649*KG, 8836*KG, 9025*KG,
- 9216*KG, 9409*KG, 9604*KG, 9801*KG, 10000*KG, 10201*KG, 10404*KG, 10609*KG, 10816*KG, 11025*KG, 11236*KG, 11449*KG, 11664*KG, 11881*KG, 12100*KG, 12321*KG,
- 12544*KG, 12769*KG, 12996*KG, 13225*KG, 13456*KG, 13689*KG, 13924*KG, 14161*KG, 14400*KG, 14641*KG, 14884*KG, 15129*KG, 15376*KG, 15625*KG, 15876*KG, 16129*KG,
- 16384*KG, 16641*KG, 16900*KG, 17161*KG, 17424*KG, 17689*KG, 17956*KG, 18225*KG, 18496*KG, 18769*KG, 19044*KG, 19321*KG, 19600*KG, 19881*KG, 20164*KG, 20449*KG,
- 20736*KG, 21025*KG, 21316*KG, 21609*KG, 21904*KG, 22201*KG, 22500*KG, 22801*KG, 23104*KG, 23409*KG, 23716*KG, 24025*KG, 24336*KG, 24649*KG, 24964*KG, 25281*KG,
- 25600*KG, 25921*KG, 26244*KG, 26569*KG, 26896*KG, 27225*KG, 27556*KG, 27889*KG, 28224*KG, 28561*KG, 28900*KG, 29241*KG, 29584*KG, 29929*KG, 30276*KG, 30625*KG,
- 30976*KG, 31329*KG, 31684*KG, 32041*KG, 32400*KG, 32761*KG, 33124*KG, 33489*KG, 33856*KG, 34225*KG, 34596*KG, 34969*KG, 35344*KG, 35721*KG, 36100*KG, 36481*KG,
- 36864*KG, 37249*KG, 37636*KG, 38025*KG, 38416*KG, 38809*KG, 39204*KG, 39601*KG, 40000*KG, 40401*KG, 40804*KG, 41209*KG, 41616*KG, 42025*KG, 42436*KG, 42849*KG,
- 43264*KG, 43681*KG, 44100*KG, 44521*KG, 44944*KG, 45369*KG, 45796*KG, 46225*KG, 46656*KG, 47089*KG, 47524*KG, 47961*KG, 48400*KG, 48841*KG, 49284*KG, 49729*KG,
- 50176*KG, 50625*KG, 51076*KG, 51529*KG, 51984*KG, 52441*KG, 52900*KG, 53361*KG, 53824*KG, 54289*KG, 54756*KG, 55225*KG, 55696*KG, 56169*KG, 56644*KG, 57121*KG,
- 57600*KG, 58081*KG, 58564*KG, 59049*KG, 59536*KG, 60025*KG, 60516*KG, 61009*KG, 61504*KG, 62001*KG, 62500*KG, 63001*KG, 63504*KG, 64009*KG, 64516*KG, 65025*KG};
- // Table for fast implementation of squaring for numbers in the interval [-255, 255] multiplied by the perceptual weight for blue.
- const unsigned int square_table_percep_blue[511] = {
- 65025*KB, 64516*KB, 64009*KB, 63504*KB, 63001*KB, 62500*KB, 62001*KB, 61504*KB, 61009*KB, 60516*KB, 60025*KB, 59536*KB, 59049*KB, 58564*KB, 58081*KB, 57600*KB,
- 57121*KB, 56644*KB, 56169*KB, 55696*KB, 55225*KB, 54756*KB, 54289*KB, 53824*KB, 53361*KB, 52900*KB, 52441*KB, 51984*KB, 51529*KB, 51076*KB, 50625*KB, 50176*KB,
- 49729*KB, 49284*KB, 48841*KB, 48400*KB, 47961*KB, 47524*KB, 47089*KB, 46656*KB, 46225*KB, 45796*KB, 45369*KB, 44944*KB, 44521*KB, 44100*KB, 43681*KB, 43264*KB,
- 42849*KB, 42436*KB, 42025*KB, 41616*KB, 41209*KB, 40804*KB, 40401*KB, 40000*KB, 39601*KB, 39204*KB, 38809*KB, 38416*KB, 38025*KB, 37636*KB, 37249*KB, 36864*KB,
- 36481*KB, 36100*KB, 35721*KB, 35344*KB, 34969*KB, 34596*KB, 34225*KB, 33856*KB, 33489*KB, 33124*KB, 32761*KB, 32400*KB, 32041*KB, 31684*KB, 31329*KB, 30976*KB,
- 30625*KB, 30276*KB, 29929*KB, 29584*KB, 29241*KB, 28900*KB, 28561*KB, 28224*KB, 27889*KB, 27556*KB, 27225*KB, 26896*KB, 26569*KB, 26244*KB, 25921*KB, 25600*KB,
- 25281*KB, 24964*KB, 24649*KB, 24336*KB, 24025*KB, 23716*KB, 23409*KB, 23104*KB, 22801*KB, 22500*KB, 22201*KB, 21904*KB, 21609*KB, 21316*KB, 21025*KB, 20736*KB,
- 20449*KB, 20164*KB, 19881*KB, 19600*KB, 19321*KB, 19044*KB, 18769*KB, 18496*KB, 18225*KB, 17956*KB, 17689*KB, 17424*KB, 17161*KB, 16900*KB, 16641*KB, 16384*KB,
- 16129*KB, 15876*KB, 15625*KB, 15376*KB, 15129*KB, 14884*KB, 14641*KB, 14400*KB, 14161*KB, 13924*KB, 13689*KB, 13456*KB, 13225*KB, 12996*KB, 12769*KB, 12544*KB,
- 12321*KB, 12100*KB, 11881*KB, 11664*KB, 11449*KB, 11236*KB, 11025*KB, 10816*KB, 10609*KB, 10404*KB, 10201*KB, 10000*KB, 9801*KB, 9604*KB, 9409*KB, 9216*KB,
- 9025*KB, 8836*KB, 8649*KB, 8464*KB, 8281*KB, 8100*KB, 7921*KB, 7744*KB, 7569*KB, 7396*KB, 7225*KB, 7056*KB, 6889*KB, 6724*KB, 6561*KB, 6400*KB,
- 6241*KB, 6084*KB, 5929*KB, 5776*KB, 5625*KB, 5476*KB, 5329*KB, 5184*KB, 5041*KB, 4900*KB, 4761*KB, 4624*KB, 4489*KB, 4356*KB, 4225*KB, 4096*KB,
- 3969*KB, 3844*KB, 3721*KB, 3600*KB, 3481*KB, 3364*KB, 3249*KB, 3136*KB, 3025*KB, 2916*KB, 2809*KB, 2704*KB, 2601*KB, 2500*KB, 2401*KB, 2304*KB,
- 2209*KB, 2116*KB, 2025*KB, 1936*KB, 1849*KB, 1764*KB, 1681*KB, 1600*KB, 1521*KB, 1444*KB, 1369*KB, 1296*KB, 1225*KB, 1156*KB, 1089*KB, 1024*KB,
- 961*KB, 900*KB, 841*KB, 784*KB, 729*KB, 676*KB, 625*KB, 576*KB, 529*KB, 484*KB, 441*KB, 400*KB, 361*KB, 324*KB, 289*KB, 256*KB,
- 225*KB, 196*KB, 169*KB, 144*KB, 121*KB, 100*KB, 81*KB, 64*KB, 49*KB, 36*KB, 25*KB, 16*KB, 9*KB, 4*KB, 1*KB,
- 0*KB, 1*KB, 4*KB, 9*KB, 16*KB, 25*KB, 36*KB, 49*KB, 64*KB, 81*KB, 100*KB, 121*KB, 144*KB, 169*KB, 196*KB, 225*KB,
- 256*KB, 289*KB, 324*KB, 361*KB, 400*KB, 441*KB, 484*KB, 529*KB, 576*KB, 625*KB, 676*KB, 729*KB, 784*KB, 841*KB, 900*KB, 961*KB,
- 1024*KB, 1089*KB, 1156*KB, 1225*KB, 1296*KB, 1369*KB, 1444*KB, 1521*KB, 1600*KB, 1681*KB, 1764*KB, 1849*KB, 1936*KB, 2025*KB, 2116*KB, 2209*KB,
- 2304*KB, 2401*KB, 2500*KB, 2601*KB, 2704*KB, 2809*KB, 2916*KB, 3025*KB, 3136*KB, 3249*KB, 3364*KB, 3481*KB, 3600*KB, 3721*KB, 3844*KB, 3969*KB,
- 4096*KB, 4225*KB, 4356*KB, 4489*KB, 4624*KB, 4761*KB, 4900*KB, 5041*KB, 5184*KB, 5329*KB, 5476*KB, 5625*KB, 5776*KB, 5929*KB, 6084*KB, 6241*KB,
- 6400*KB, 6561*KB, 6724*KB, 6889*KB, 7056*KB, 7225*KB, 7396*KB, 7569*KB, 7744*KB, 7921*KB, 8100*KB, 8281*KB, 8464*KB, 8649*KB, 8836*KB, 9025*KB,
- 9216*KB, 9409*KB, 9604*KB, 9801*KB, 10000*KB, 10201*KB, 10404*KB, 10609*KB, 10816*KB, 11025*KB, 11236*KB, 11449*KB, 11664*KB, 11881*KB, 12100*KB, 12321*KB,
- 12544*KB, 12769*KB, 12996*KB, 13225*KB, 13456*KB, 13689*KB, 13924*KB, 14161*KB, 14400*KB, 14641*KB, 14884*KB, 15129*KB, 15376*KB, 15625*KB, 15876*KB, 16129*KB,
- 16384*KB, 16641*KB, 16900*KB, 17161*KB, 17424*KB, 17689*KB, 17956*KB, 18225*KB, 18496*KB, 18769*KB, 19044*KB, 19321*KB, 19600*KB, 19881*KB, 20164*KB, 20449*KB,
- 20736*KB, 21025*KB, 21316*KB, 21609*KB, 21904*KB, 22201*KB, 22500*KB, 22801*KB, 23104*KB, 23409*KB, 23716*KB, 24025*KB, 24336*KB, 24649*KB, 24964*KB, 25281*KB,
- 25600*KB, 25921*KB, 26244*KB, 26569*KB, 26896*KB, 27225*KB, 27556*KB, 27889*KB, 28224*KB, 28561*KB, 28900*KB, 29241*KB, 29584*KB, 29929*KB, 30276*KB, 30625*KB,
- 30976*KB, 31329*KB, 31684*KB, 32041*KB, 32400*KB, 32761*KB, 33124*KB, 33489*KB, 33856*KB, 34225*KB, 34596*KB, 34969*KB, 35344*KB, 35721*KB, 36100*KB, 36481*KB,
- 36864*KB, 37249*KB, 37636*KB, 38025*KB, 38416*KB, 38809*KB, 39204*KB, 39601*KB, 40000*KB, 40401*KB, 40804*KB, 41209*KB, 41616*KB, 42025*KB, 42436*KB, 42849*KB,
- 43264*KB, 43681*KB, 44100*KB, 44521*KB, 44944*KB, 45369*KB, 45796*KB, 46225*KB, 46656*KB, 47089*KB, 47524*KB, 47961*KB, 48400*KB, 48841*KB, 49284*KB, 49729*KB,
- 50176*KB, 50625*KB, 51076*KB, 51529*KB, 51984*KB, 52441*KB, 52900*KB, 53361*KB, 53824*KB, 54289*KB, 54756*KB, 55225*KB, 55696*KB, 56169*KB, 56644*KB, 57121*KB,
- 57600*KB, 58081*KB, 58564*KB, 59049*KB, 59536*KB, 60025*KB, 60516*KB, 61009*KB, 61504*KB, 62001*KB, 62500*KB, 63001*KB, 63504*KB, 64009*KB, 64516*KB, 65025*KB};
- // Find the best table to use for a 2x4 area by testing all.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int tryalltables_3bittable2x4(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color, unsigned int &best_table,unsigned int &best_pixel_indices_MSB, unsigned int &best_pixel_indices_LSB)
- {
- int min_error = 3*255*255*16;
- int q;
- int err;
- unsigned int pixel_indices_MSB, pixel_indices_LSB;
- for(q=0;q<16;q+=2) // try all the 8 tables.
- {
- err=compressBlockWithTable2x4(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
- if(err<min_error)
- {
- min_error=err;
- best_pixel_indices_MSB = pixel_indices_MSB;
- best_pixel_indices_LSB = pixel_indices_LSB;
- best_table=q >> 1;
- }
- }
- return min_error;
- }
- // Find the best table to use for a 2x4 area by testing all.
- // Uses perceptual weighting.
- // Uses fixed point implementation where 1000 equals 1.0
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int tryalltables_3bittable2x4percep1000(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color, unsigned int &best_table,unsigned int &best_pixel_indices_MSB, unsigned int &best_pixel_indices_LSB)
- {
- unsigned int min_error = MAXERR1000;
- int q;
- unsigned int err;
- unsigned int pixel_indices_MSB, pixel_indices_LSB;
- for(q=0;q<16;q+=2) // try all the 8 tables.
- {
- err=compressBlockWithTable2x4percep1000(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
- if(err<min_error)
- {
- min_error=err;
- best_pixel_indices_MSB = pixel_indices_MSB;
- best_pixel_indices_LSB = pixel_indices_LSB;
- best_table=q >> 1;
- }
- }
- return min_error;
- }
- // Find the best table to use for a 2x4 area by testing all.
- // Uses perceptual weighting.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int tryalltables_3bittable2x4percep(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color, unsigned int &best_table,unsigned int &best_pixel_indices_MSB, unsigned int &best_pixel_indices_LSB)
- {
- float min_error = 3*255*255*16;
- int q;
- float err;
- unsigned int pixel_indices_MSB, pixel_indices_LSB;
- for(q=0;q<16;q+=2) // try all the 8 tables.
- {
- err=compressBlockWithTable2x4percep(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
- if(err<min_error)
- {
- min_error=err;
- best_pixel_indices_MSB = pixel_indices_MSB;
- best_pixel_indices_LSB = pixel_indices_LSB;
- best_table=q >> 1;
- }
- }
- return (int) min_error;
- }
- // Find the best table to use for a 4x2 area by testing all.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int tryalltables_3bittable4x2(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color, unsigned int &best_table,unsigned int &best_pixel_indices_MSB, unsigned int &best_pixel_indices_LSB)
- {
- int min_error = 3*255*255*16;
- int q;
- int err;
- unsigned int pixel_indices_MSB, pixel_indices_LSB;
- for(q=0;q<16;q+=2) // try all the 8 tables.
- {
- err=compressBlockWithTable4x2(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
- if(err<min_error)
- {
- min_error=err;
- best_pixel_indices_MSB = pixel_indices_MSB;
- best_pixel_indices_LSB = pixel_indices_LSB;
- best_table=q >> 1;
- }
- }
- return min_error;
- }
- // Find the best table to use for a 4x2 area by testing all.
- // Uses perceptual weighting.
- // Uses fixed point implementation where 1000 equals 1.0
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int tryalltables_3bittable4x2percep1000(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color, unsigned int &best_table,unsigned int &best_pixel_indices_MSB, unsigned int &best_pixel_indices_LSB)
- {
- unsigned int min_error = MAXERR1000;
- int q;
- unsigned int err;
- unsigned int pixel_indices_MSB, pixel_indices_LSB;
- for(q=0;q<16;q+=2) // try all the 8 tables.
- {
- err=compressBlockWithTable4x2percep1000(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
- if(err<min_error)
- {
- min_error=err;
- best_pixel_indices_MSB = pixel_indices_MSB;
- best_pixel_indices_LSB = pixel_indices_LSB;
- best_table=q >> 1;
- }
- }
- return min_error;
- }
- // Find the best table to use for a 4x2 area by testing all.
- // Uses perceptual weighting.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int tryalltables_3bittable4x2percep(uint8 *img,int width,int height,int startx,int starty,uint8 *avg_color, unsigned int &best_table,unsigned int &best_pixel_indices_MSB, unsigned int &best_pixel_indices_LSB)
- {
- float min_error = 3*255*255*16;
- int q;
- float err;
- unsigned int pixel_indices_MSB, pixel_indices_LSB;
- for(q=0;q<16;q+=2) // try all the 8 tables.
- {
- err=compressBlockWithTable4x2percep(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
- if(err<min_error)
- {
- min_error=err;
- best_pixel_indices_MSB = pixel_indices_MSB;
- best_pixel_indices_LSB = pixel_indices_LSB;
- best_table=q >> 1;
- }
- }
- return (int) min_error;
- }
- // The below code quantizes a float RGB value to RGB444.
- //
- // The format often allows a pixel to completely compensate an intensity error of the base
- // color. Hence the closest RGB444 point may not be the best, and the code below uses
- // this fact to find a better RGB444 color as the base color.
- //
- // (See the presentation http://www.jacobstrom.com/publications/PACKMAN.ppt for more info.)
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void quantize444ColorCombined(float *avg_col_in, int *enc_color, uint8 *avg_color)
- {
- float dr, dg, db;
- float kr, kg, kb;
- float wR2, wG2, wB2;
- uint8 low_color[3];
- uint8 high_color[3];
- float min_error=255*255*8*3;
- float lowhightable[8];
- unsigned int best_table=0;
- unsigned int best_index=0;
- int q;
- float kval = (float) (255.0/15.0);
- // These are the values that we want to have:
- float red_average, green_average, blue_average;
- int red_4bit_low, green_4bit_low, blue_4bit_low;
- int red_4bit_high, green_4bit_high, blue_4bit_high;
-
- // These are the values that we approximate with:
- int red_low, green_low, blue_low;
- int red_high, green_high, blue_high;
- red_average = avg_col_in[0];
- green_average = avg_col_in[1];
- blue_average = avg_col_in[2];
- // Find the 5-bit reconstruction levels red_low, red_high
- // so that red_average is in interval [red_low, red_high].
- // (The same with green and blue.)
- red_4bit_low = (int) (red_average/kval);
- green_4bit_low = (int) (green_average/kval);
- blue_4bit_low = (int) (blue_average/kval);
- red_4bit_high = CLAMP(0, red_4bit_low + 1, 15);
- green_4bit_high = CLAMP(0, green_4bit_low + 1, 15);
- blue_4bit_high = CLAMP(0, blue_4bit_low + 1, 15);
- red_low = (red_4bit_low << 4) | (red_4bit_low >> 0);
- green_low = (green_4bit_low << 4) | (green_4bit_low >> 0);
- blue_low = (blue_4bit_low << 4) | (blue_4bit_low >> 0);
- red_high = (red_4bit_high << 4) | (red_4bit_high >> 0);
- green_high = (green_4bit_high << 4) | (green_4bit_high >> 0);
- blue_high = (blue_4bit_high << 4) | (blue_4bit_high >> 0);
- kr = (float)red_high - (float)red_low;
- kg = (float)green_high - (float)green_low;
- kb = (float)blue_high - (float)blue_low;
- // Note that dr, dg, and db are all negative.
- dr = red_low - red_average;
- dg = green_low - green_average;
- db = blue_low - blue_average;
- // Use straight (nonperceptive) weights.
- wR2 = (float) 1.0;
- wG2 = (float) 1.0;
- wB2 = (float) 1.0;
- lowhightable[0] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
- lowhightable[1] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
- lowhightable[2] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
- lowhightable[3] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
- lowhightable[4] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
- lowhightable[5] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
- lowhightable[6] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
- lowhightable[7] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
- float min_value = lowhightable[0];
- int min_index = 0;
- for(q = 1; q<8; q++)
- {
- if(lowhightable[q] < min_value)
- {
- min_value = lowhightable[q];
- min_index = q;
- }
- }
- float drh = red_high-red_average;
- float dgh = green_high-green_average;
- float dbh = blue_high-blue_average;
- low_color[0] = red_4bit_low;
- low_color[1] = green_4bit_low;
- low_color[2] = blue_4bit_low;
- high_color[0] = red_4bit_high;
- high_color[1] = green_4bit_high;
- high_color[2] = blue_4bit_high;
- switch(min_index)
- {
- case 0:
- // Since the step size is always 17 in RGB444 format (15*17=255),
- // kr = kg = kb = 17, which means that case 0 and case 7 will
- // always have equal projected error. Choose the one that is
- // closer to the desired color.
- if(dr*dr + dg*dg + db*db > 3*8*8)
- {
- enc_color[0] = high_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = high_color[2];
- }
- else
- {
- enc_color[0] = low_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = low_color[2];
- }
- break;
- case 1:
- enc_color[0] = high_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = low_color[2];
- break;
- case 2:
- enc_color[0] = low_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = low_color[2];
- break;
- case 3:
- enc_color[0] = low_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = high_color[2];
- break;
- case 4:
- enc_color[0] = high_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = low_color[2];
- break;
- case 5:
- enc_color[0] = high_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = high_color[2];
- break;
- case 6:
- enc_color[0] = low_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = high_color[2];
- break;
- case 7:
- if(dr*dr + dg*dg + db*db > 3*8*8)
- {
- enc_color[0] = high_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = high_color[2];
- }
- else
- {
- enc_color[0] = low_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = low_color[2];
- }
- break;
- }
- // Expand 5-bit encoded color to 8-bit color
- avg_color[0] = (enc_color[0] << 3) | (enc_color[0] >> 2);
- avg_color[1] = (enc_color[1] << 3) | (enc_color[1] >> 2);
- avg_color[2] = (enc_color[2] << 3) | (enc_color[2] >> 2);
- }
- // The below code quantizes a float RGB value to RGB555.
- //
- // The format often allows a pixel to completely compensate an intensity error of the base
- // color. Hence the closest RGB555 point may not be the best, and the code below uses
- // this fact to find a better RGB555 color as the base color.
- //
- // (See the presentation http://www.jacobstrom.com/publications/PACKMAN.ppt for more info.)
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void quantize555ColorCombined(float *avg_col_in, int *enc_color, uint8 *avg_color)
- {
- float dr, dg, db;
- float kr, kg, kb;
- float wR2, wG2, wB2;
- uint8 low_color[3];
- uint8 high_color[3];
- float min_error=255*255*8*3;
- float lowhightable[8];
- unsigned int best_table=0;
- unsigned int best_index=0;
- int q;
- float kval = (float) (255.0/31.0);
- // These are the values that we want to have:
- float red_average, green_average, blue_average;
- int red_5bit_low, green_5bit_low, blue_5bit_low;
- int red_5bit_high, green_5bit_high, blue_5bit_high;
-
- // These are the values that we approximate with:
- int red_low, green_low, blue_low;
- int red_high, green_high, blue_high;
- red_average = avg_col_in[0];
- green_average = avg_col_in[1];
- blue_average = avg_col_in[2];
- // Find the 5-bit reconstruction levels red_low, red_high
- // so that red_average is in interval [red_low, red_high].
- // (The same with green and blue.)
- red_5bit_low = (int) (red_average/kval);
- green_5bit_low = (int) (green_average/kval);
- blue_5bit_low = (int) (blue_average/kval);
- red_5bit_high = CLAMP(0, red_5bit_low + 1, 31);
- green_5bit_high = CLAMP(0, green_5bit_low + 1, 31);
- blue_5bit_high = CLAMP(0, blue_5bit_low + 1, 31);
- red_low = (red_5bit_low << 3) | (red_5bit_low >> 2);
- green_low = (green_5bit_low << 3) | (green_5bit_low >> 2);
- blue_low = (blue_5bit_low << 3) | (blue_5bit_low >> 2);
- red_high = (red_5bit_high << 3) | (red_5bit_high >> 2);
- green_high = (green_5bit_high << 3) | (green_5bit_high >> 2);
- blue_high = (blue_5bit_high << 3) | (blue_5bit_high >> 2);
- kr = (float)red_high - (float)red_low;
- kg = (float)green_high - (float)green_low;
- kb = (float)blue_high - (float)blue_low;
- // Note that dr, dg, and db are all negative.
- dr = red_low - red_average;
- dg = green_low - green_average;
- db = blue_low - blue_average;
- // Use straight (nonperceptive) weights.
- wR2 = (float) 1.0;
- wG2 = (float) 1.0;
- wB2 = (float) 1.0;
- lowhightable[0] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
- lowhightable[1] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
- lowhightable[2] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
- lowhightable[3] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
- lowhightable[4] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
- lowhightable[5] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
- lowhightable[6] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
- lowhightable[7] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
- float min_value = lowhightable[0];
- int min_index = 0;
- for(q = 1; q<8; q++)
- {
- if(lowhightable[q] < min_value)
- {
- min_value = lowhightable[q];
- min_index = q;
- }
- }
- float drh = red_high-red_average;
- float dgh = green_high-green_average;
- float dbh = blue_high-blue_average;
- low_color[0] = red_5bit_low;
- low_color[1] = green_5bit_low;
- low_color[2] = blue_5bit_low;
- high_color[0] = red_5bit_high;
- high_color[1] = green_5bit_high;
- high_color[2] = blue_5bit_high;
- switch(min_index)
- {
- case 0:
- enc_color[0] = low_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = low_color[2];
- break;
- case 1:
- enc_color[0] = high_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = low_color[2];
- break;
- case 2:
- enc_color[0] = low_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = low_color[2];
- break;
- case 3:
- enc_color[0] = low_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = high_color[2];
- break;
- case 4:
- enc_color[0] = high_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = low_color[2];
- break;
- case 5:
- enc_color[0] = high_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = high_color[2];
- break;
- case 6:
- enc_color[0] = low_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = high_color[2];
- break;
- case 7:
- enc_color[0] = high_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = high_color[2];
- break;
- }
- // Expand 5-bit encoded color to 8-bit color
- avg_color[0] = (enc_color[0] << 3) | (enc_color[0] >> 2);
- avg_color[1] = (enc_color[1] << 3) | (enc_color[1] >> 2);
- avg_color[2] = (enc_color[2] << 3) | (enc_color[2] >> 2);
-
- }
- // The below code quantizes a float RGB value to RGB444.
- //
- // The format often allows a pixel to completely compensate an intensity error of the base
- // color. Hence the closest RGB444 point may not be the best, and the code below uses
- // this fact to find a better RGB444 color as the base color.
- //
- // (See the presentation http://www.jacobstrom.com/publications/PACKMAN.ppt for more info.)
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void quantize444ColorCombinedPerceptual(float *avg_col_in, int *enc_color, uint8 *avg_color)
- {
- float dr, dg, db;
- float kr, kg, kb;
- float wR2, wG2, wB2;
- uint8 low_color[3];
- uint8 high_color[3];
- float min_error=255*255*8*3;
- float lowhightable[8];
- unsigned int best_table=0;
- unsigned int best_index=0;
- int q;
- float kval = (float) (255.0/15.0);
- // These are the values that we want to have:
- float red_average, green_average, blue_average;
- int red_4bit_low, green_4bit_low, blue_4bit_low;
- int red_4bit_high, green_4bit_high, blue_4bit_high;
-
- // These are the values that we approximate with:
- int red_low, green_low, blue_low;
- int red_high, green_high, blue_high;
- red_average = avg_col_in[0];
- green_average = avg_col_in[1];
- blue_average = avg_col_in[2];
- // Find the 5-bit reconstruction levels red_low, red_high
- // so that red_average is in interval [red_low, red_high].
- // (The same with green and blue.)
- red_4bit_low = (int) (red_average/kval);
- green_4bit_low = (int) (green_average/kval);
- blue_4bit_low = (int) (blue_average/kval);
- red_4bit_high = CLAMP(0, red_4bit_low + 1, 15);
- green_4bit_high = CLAMP(0, green_4bit_low + 1, 15);
- blue_4bit_high = CLAMP(0, blue_4bit_low + 1, 15);
- red_low = (red_4bit_low << 4) | (red_4bit_low >> 0);
- green_low = (green_4bit_low << 4) | (green_4bit_low >> 0);
- blue_low = (blue_4bit_low << 4) | (blue_4bit_low >> 0);
- red_high = (red_4bit_high << 4) | (red_4bit_high >> 0);
- green_high = (green_4bit_high << 4) | (green_4bit_high >> 0);
- blue_high = (blue_4bit_high << 4) | (blue_4bit_high >> 0);
- low_color[0] = red_4bit_low;
- low_color[1] = green_4bit_low;
- low_color[2] = blue_4bit_low;
- high_color[0] = red_4bit_high;
- high_color[1] = green_4bit_high;
- high_color[2] = blue_4bit_high;
- kr = (float)red_high - (float)red_low;
- kg = (float)green_high - (float)green_low;
- kb = (float)blue_high- (float)blue_low;
- // Note that dr, dg, and db are all negative.
- dr = red_low - red_average;
- dg = green_low - green_average;
- db = blue_low - blue_average;
- // Perceptual weights to use
- wR2 = (float) PERCEPTUAL_WEIGHT_R_SQUARED;
- wG2 = (float) PERCEPTUAL_WEIGHT_G_SQUARED;
- wB2 = (float) PERCEPTUAL_WEIGHT_B_SQUARED;
- lowhightable[0] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
- lowhightable[1] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
- lowhightable[2] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
- lowhightable[3] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
- lowhightable[4] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
- lowhightable[5] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
- lowhightable[6] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
- lowhightable[7] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
- float min_value = lowhightable[0];
- int min_index = 0;
- for(q = 1; q<8; q++)
- {
- if(lowhightable[q] < min_value)
- {
- min_value = lowhightable[q];
- min_index = q;
- }
- }
- float drh = red_high-red_average;
- float dgh = green_high-green_average;
- float dbh = blue_high-blue_average;
- switch(min_index)
- {
- case 0:
- enc_color[0] = low_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = low_color[2];
- break;
- case 1:
- enc_color[0] = high_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = low_color[2];
- break;
- case 2:
- enc_color[0] = low_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = low_color[2];
- break;
- case 3:
- enc_color[0] = low_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = high_color[2];
- break;
- case 4:
- enc_color[0] = high_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = low_color[2];
- break;
- case 5:
- enc_color[0] = high_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = high_color[2];
- break;
- case 6:
- enc_color[0] = low_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = high_color[2];
- break;
- case 7:
- enc_color[0] = high_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = high_color[2];
- break;
- }
- // Expand encoded color to eight bits
- avg_color[0] = (enc_color[0] << 4) | enc_color[0];
- avg_color[1] = (enc_color[1] << 4) | enc_color[1];
- avg_color[2] = (enc_color[2] << 4) | enc_color[2];
- }
- // The below code quantizes a float RGB value to RGB555.
- //
- // The format often allows a pixel to completely compensate an intensity error of the base
- // color. Hence the closest RGB555 point may not be the best, and the code below uses
- // this fact to find a better RGB555 color as the base color.
- //
- // (See the presentation http://www.jacobstrom.com/publications/PACKMAN.ppt for more info.)
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void quantize555ColorCombinedPerceptual(float *avg_col_in, int *enc_color, uint8 *avg_color)
- {
- float dr, dg, db;
- float kr, kg, kb;
- float wR2, wG2, wB2;
- uint8 low_color[3];
- uint8 high_color[3];
- float min_error=255*255*8*3;
- float lowhightable[8];
- unsigned int best_table=0;
- unsigned int best_index=0;
- int q;
- float kval = (float) (255.0/31.0);
- // These are the values that we want to have:
- float red_average, green_average, blue_average;
- int red_5bit_low, green_5bit_low, blue_5bit_low;
- int red_5bit_high, green_5bit_high, blue_5bit_high;
-
- // These are the values that we approximate with:
- int red_low, green_low, blue_low;
- int red_high, green_high, blue_high;
- red_average = avg_col_in[0];
- green_average = avg_col_in[1];
- blue_average = avg_col_in[2];
- // Find the 5-bit reconstruction levels red_low, red_high
- // so that red_average is in interval [red_low, red_high].
- // (The same with green and blue.)
- red_5bit_low = (int) (red_average/kval);
- green_5bit_low = (int) (green_average/kval);
- blue_5bit_low = (int) (blue_average/kval);
- red_5bit_high = CLAMP(0, red_5bit_low + 1, 31);
- green_5bit_high = CLAMP(0, green_5bit_low + 1, 31);
- blue_5bit_high = CLAMP(0, blue_5bit_low + 1, 31);
- red_low = (red_5bit_low << 3) | (red_5bit_low >> 2);
- green_low = (green_5bit_low << 3) | (green_5bit_low >> 2);
- blue_low = (blue_5bit_low << 3) | (blue_5bit_low >> 2);
- red_high = (red_5bit_high << 3) | (red_5bit_high >> 2);
- green_high = (green_5bit_high << 3) | (green_5bit_high >> 2);
- blue_high = (blue_5bit_high << 3) | (blue_5bit_high >> 2);
- low_color[0] = red_5bit_low;
- low_color[1] = green_5bit_low;
- low_color[2] = blue_5bit_low;
- high_color[0] = red_5bit_high;
- high_color[1] = green_5bit_high;
- high_color[2] = blue_5bit_high;
- kr = (float)red_high - (float)red_low;
- kg = (float)green_high - (float)green_low;
- kb = (float)blue_high - (float)blue_low;
- // Note that dr, dg, and db are all negative.
- dr = red_low - red_average;
- dg = green_low - green_average;
- db = blue_low - blue_average;
- // Perceptual weights to use
- wR2 = (float) PERCEPTUAL_WEIGHT_R_SQUARED;
- wG2 = (float) PERCEPTUAL_WEIGHT_G_SQUARED;
- wB2 = (float) PERCEPTUAL_WEIGHT_B_SQUARED;
- lowhightable[0] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
- lowhightable[1] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
- lowhightable[2] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
- lowhightable[3] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
- lowhightable[4] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
- lowhightable[5] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
- lowhightable[6] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
- lowhightable[7] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
- float min_value = lowhightable[0];
- int min_index = 0;
- for(q = 1; q<8; q++)
- {
- if(lowhightable[q] < min_value)
- {
- min_value = lowhightable[q];
- min_index = q;
- }
- }
- float drh = red_high-red_average;
- float dgh = green_high-green_average;
- float dbh = blue_high-blue_average;
- switch(min_index)
- {
- case 0:
- enc_color[0] = low_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = low_color[2];
- break;
- case 1:
- enc_color[0] = high_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = low_color[2];
- break;
- case 2:
- enc_color[0] = low_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = low_color[2];
- break;
- case 3:
- enc_color[0] = low_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = high_color[2];
- break;
- case 4:
- enc_color[0] = high_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = low_color[2];
- break;
- case 5:
- enc_color[0] = high_color[0];
- enc_color[1] = low_color[1];
- enc_color[2] = high_color[2];
- break;
- case 6:
- enc_color[0] = low_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = high_color[2];
- break;
- case 7:
- enc_color[0] = high_color[0];
- enc_color[1] = high_color[1];
- enc_color[2] = high_color[2];
- break;
- }
- // Expand 5-bit encoded color to 8-bit color
- avg_color[0] = (enc_color[0] << 3) | (enc_color[0] >> 2);
- avg_color[1] = (enc_color[1] << 3) | (enc_color[1] >> 2);
- avg_color[2] = (enc_color[2] << 3) | (enc_color[2] >> 2);
- }
- // Compresses the block using only the individual mode in ETC1/ETC2 using the average color as the base color.
- // Uses a perceptual error metric.
- // Uses fixed point arithmetics where 1000 equals 1.0
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockOnlyIndividualAveragePerceptual1000(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, int *best_enc_color1, int*best_enc_color2, int &best_flip, unsigned int &best_err_upper, unsigned int &best_err_lower, unsigned int &best_err_left, unsigned int &best_err_right, int *best_color_upper, int *best_color_lower, int *best_color_left, int *best_color_right)
- {
- unsigned int compressed1_norm, compressed2_norm;
- unsigned int compressed1_flip, compressed2_flip;
- uint8 avg_color_quant1[3], avg_color_quant2[3];
- float avg_color_float1[3],avg_color_float2[3];
- int enc_color1[3], enc_color2[3];
- unsigned int best_table_indices1=0, best_table_indices2=0;
- unsigned int best_table1=0, best_table2=0;
- int diffbit;
- unsigned int norm_err=0;
- unsigned int flip_err=0;
- unsigned int best_err;
- // First try normal blocks 2x4:
- computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
- enc_color1[0] = int( JAS_ROUND(15.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(15.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(15.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(15.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(15.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(15.0*avg_color_float2[2]/255.0) );
- diffbit = 0;
- avg_color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0] );
- avg_color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1] );
- avg_color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2] );
- avg_color_quant2[0] = enc_color2[0] << 4 | (enc_color2[0] );
- avg_color_quant2[1] = enc_color2[1] << 4 | (enc_color2[1] );
- avg_color_quant2[2] = enc_color2[2] << 4 | (enc_color2[2] );
- // Pack bits into the first word.
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 43);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
-
- best_color_left[0] = enc_color1[0];
- best_color_left[1] = enc_color1[1];
- best_color_left[2] = enc_color1[2];
- best_color_right[0] = enc_color2[0];
- best_color_right[1] = enc_color2[1];
- best_color_right[2] = enc_color2[2];
- norm_err = 0;
- // left part of block
- best_err_left = tryalltables_3bittable2x4percep1000(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- norm_err = best_err_left;
- // right part of block
- best_err_right = tryalltables_3bittable2x4percep1000(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- norm_err += best_err_right;
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- // Now try flipped blocks 4x2:
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- enc_color1[0] = int( JAS_ROUND(15.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(15.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(15.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(15.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(15.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(15.0*avg_color_float2[2]/255.0) );
- best_color_upper[0] = enc_color1[0];
- best_color_upper[1] = enc_color1[1];
- best_color_upper[2] = enc_color1[2];
- best_color_lower[0] = enc_color2[0];
- best_color_lower[1] = enc_color2[1];
- best_color_lower[2] = enc_color2[2];
- diffbit = 0;
- avg_color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0] );
- avg_color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1] );
- avg_color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2] );
- avg_color_quant2[0] = enc_color2[0] << 4 | (enc_color2[0] );
- avg_color_quant2[1] = enc_color2[1] << 4 | (enc_color2[1] );
- avg_color_quant2[2] = enc_color2[2] << 4 | (enc_color2[2] );
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 49);
- PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
- // upper part of block
- best_err_upper = tryalltables_3bittable4x2percep1000(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- flip_err = best_err_upper;
- // lower part of block
- best_err_lower = tryalltables_3bittable4x2percep1000(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- flip_err += best_err_lower;
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- // Now lets see which is the best table to use. Only 8 tables are possible.
- if(norm_err <= flip_err)
- {
- compressed1 = compressed1_norm | 0;
- compressed2 = compressed2_norm;
- best_err = norm_err;
- best_flip = 0;
- }
- else
- {
- compressed1 = compressed1_flip | 1;
- compressed2 = compressed2_flip;
- best_err = flip_err;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- best_flip = 1;
- }
- return best_err;
- }
- // Compresses the block using only the individual mode in ETC1/ETC2 using the average color as the base color.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int compressBlockOnlyIndividualAverage(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, int *best_enc_color1, int*best_enc_color2, int &best_flip, unsigned int &best_err_upper, unsigned int &best_err_lower, unsigned int &best_err_left, unsigned int &best_err_right, int *best_color_upper, int *best_color_lower, int *best_color_left, int *best_color_right)
- {
- unsigned int compressed1_norm, compressed2_norm;
- unsigned int compressed1_flip, compressed2_flip;
- uint8 avg_color_quant1[3], avg_color_quant2[3];
- float avg_color_float1[3],avg_color_float2[3];
- int enc_color1[3], enc_color2[3];
- int min_error=255*255*8*3;
- unsigned int best_table_indices1=0, best_table_indices2=0;
- unsigned int best_table1=0, best_table2=0;
- int diffbit;
- int norm_err=0;
- int flip_err=0;
- int best_err;
- // First try normal blocks 2x4:
- computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
- enc_color1[0] = int( JAS_ROUND(15.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(15.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(15.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(15.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(15.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(15.0*avg_color_float2[2]/255.0) );
- diffbit = 0;
- avg_color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0] );
- avg_color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1] );
- avg_color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2] );
- avg_color_quant2[0] = enc_color2[0] << 4 | (enc_color2[0] );
- avg_color_quant2[1] = enc_color2[1] << 4 | (enc_color2[1] );
- avg_color_quant2[2] = enc_color2[2] << 4 | (enc_color2[2] );
- // Pack bits into the first word.
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 43);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- best_color_left[0] = enc_color1[0];
- best_color_left[1] = enc_color1[1];
- best_color_left[2] = enc_color1[2];
- best_color_right[0] = enc_color2[0];
- best_color_right[1] = enc_color2[1];
- best_color_right[2] = enc_color2[2];
- norm_err = 0;
- // left part of block
- best_err_left = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- norm_err = best_err_left;
- // right part of block
- best_err_right = tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- norm_err += best_err_right;
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- // Now try flipped blocks 4x2:
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- enc_color1[0] = int( JAS_ROUND(15.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(15.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(15.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(15.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(15.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(15.0*avg_color_float2[2]/255.0) );
- best_color_upper[0] = enc_color1[0];
- best_color_upper[1] = enc_color1[1];
- best_color_upper[2] = enc_color1[2];
- best_color_lower[0] = enc_color2[0];
- best_color_lower[1] = enc_color2[1];
- best_color_lower[2] = enc_color2[2];
- diffbit = 0;
- avg_color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0] );
- avg_color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1] );
- avg_color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2] );
- avg_color_quant2[0] = enc_color2[0] << 4 | (enc_color2[0] );
- avg_color_quant2[1] = enc_color2[1] << 4 | (enc_color2[1] );
- avg_color_quant2[2] = enc_color2[2] << 4 | (enc_color2[2] );
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 49);
- PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
- // upper part of block
- best_err_upper = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- flip_err = best_err_upper;
- // lower part of block
- best_err_lower = tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- flip_err += best_err_lower;
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- // Now lets see which is the best table to use. Only 8 tables are possible.
- if(norm_err <= flip_err)
- {
- compressed1 = compressed1_norm | 0;
- compressed2 = compressed2_norm;
- best_err = norm_err;
- best_flip = 0;
- }
- else
- {
- compressed1 = compressed1_flip | 1;
- compressed2 = compressed2_flip;
- best_err = flip_err;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- best_flip = 1;
- }
- return best_err;
- }
- // Compresses the block using either the individual or differential mode in ETC1/ETC2
- // Uses the average color as the base color in each half-block.
- // Tries both flipped and unflipped.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockDiffFlipAverage(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int compressed1_norm, compressed2_norm;
- unsigned int compressed1_flip, compressed2_flip;
- uint8 avg_color_quant1[3], avg_color_quant2[3];
- float avg_color_float1[3],avg_color_float2[3];
- int enc_color1[3], enc_color2[3], diff[3];
- int min_error=255*255*8*3;
- unsigned int best_table_indices1=0, best_table_indices2=0;
- unsigned int best_table1=0, best_table2=0;
- int diffbit;
- int norm_err=0;
- int flip_err=0;
- // First try normal blocks 2x4:
- computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- float eps;
- enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
- {
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- norm_err = 0;
- // left part of block
- norm_err = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // right part of block
- norm_err += tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- }
- else
- {
- diffbit = 0;
- // The difference is bigger than what fits in 555 plus delta-333, so we will have
- // to deal with 444 444.
- eps = (float) 0.0001;
- enc_color1[0] = int( ((float) avg_color_float1[0] / (17.0)) +0.5 + eps);
- enc_color1[1] = int( ((float) avg_color_float1[1] / (17.0)) +0.5 + eps);
- enc_color1[2] = int( ((float) avg_color_float1[2] / (17.0)) +0.5 + eps);
- enc_color2[0] = int( ((float) avg_color_float2[0] / (17.0)) +0.5 + eps);
- enc_color2[1] = int( ((float) avg_color_float2[1] / (17.0)) +0.5 + eps);
- enc_color2[2] = int( ((float) avg_color_float2[2] / (17.0)) +0.5 + eps);
- avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
- avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
- avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
- avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
- avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
- avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
- // Pack bits into the first word.
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_norm, enc_color2[2], 4, 43);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
-
- // left part of block
- norm_err = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // right part of block
- norm_err += tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- }
- // Now try flipped blocks 4x2:
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
- {
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- // upper part of block
- flip_err = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // lower part of block
- flip_err += tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- else
- {
- diffbit = 0;
- // The difference is bigger than what fits in 555 plus delta-333, so we will have
- // to deal with 444 444.
- eps = (float) 0.0001;
- enc_color1[0] = int( ((float) avg_color_float1[0] / (17.0)) +0.5 + eps);
- enc_color1[1] = int( ((float) avg_color_float1[1] / (17.0)) +0.5 + eps);
- enc_color1[2] = int( ((float) avg_color_float1[2] / (17.0)) +0.5 + eps);
- enc_color2[0] = int( ((float) avg_color_float2[0] / (17.0)) +0.5 + eps);
- enc_color2[1] = int( ((float) avg_color_float2[1] / (17.0)) +0.5 + eps);
- enc_color2[2] = int( ((float) avg_color_float2[2] / (17.0)) +0.5 + eps);
- avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
- avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
- avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
- avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
- avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
- avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- // upper part of block
- flip_err = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // lower part of block
- flip_err += tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- // Now lets see which is the best table to use. Only 8 tables are possible.
- if(norm_err <= flip_err)
- {
- compressed1 = compressed1_norm | 0;
- compressed2 = compressed2_norm;
- }
- else
- {
- compressed1 = compressed1_flip | 1;
- compressed2 = compressed2_flip;
- }
- }
- // Compresses the block using only the differential mode in ETC1/ETC2
- // Uses the average color as the base color in each half-block.
- // If average colors are too different, use the average color of the entire block in both half-blocks.
- // Tries both flipped and unflipped.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int compressBlockOnlyDiffFlipAverage(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, int *best_enc_color1, int*best_enc_color2, int &best_flip)
- {
- unsigned int compressed1_norm, compressed2_norm;
- unsigned int compressed1_flip, compressed2_flip;
- uint8 avg_color_quant1[3], avg_color_quant2[3];
- float avg_color_float1[3],avg_color_float2[3];
- int enc_color1[3], enc_color2[3], diff[3];
- int min_error=255*255*8*3;
- unsigned int best_table_indices1=0, best_table_indices2=0;
- unsigned int best_table1=0, best_table2=0;
- int diffbit;
- int norm_err=0;
- int flip_err=0;
- int best_err;
- // First try normal blocks 2x4:
- computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( !((diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3)) )
- {
- // The colors are too different. Use the same color in both blocks.
- enc_color1[0] = int( JAS_ROUND(31.0*((avg_color_float1[0]+avg_color_float2[0])/2.0)/255.0) );
- enc_color1[1] = int( JAS_ROUND(31.0*((avg_color_float1[1]+avg_color_float2[1])/2.0)/255.0) );
- enc_color1[2] = int( JAS_ROUND(31.0*((avg_color_float1[2]+avg_color_float2[2])/2.0)/255.0) );
- enc_color2[0] = enc_color1[0];
- enc_color2[1] = enc_color1[1];
- enc_color2[2] = enc_color1[2];
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- }
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- norm_err = 0;
- // left part of block
- norm_err = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // right part of block
- norm_err += tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- // Now try flipped blocks 4x2:
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( !((diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3)) )
- {
- // The colors are too different. Use the same color in both blocks.
- enc_color1[0] = int( JAS_ROUND(31.0*((avg_color_float1[0]+avg_color_float2[0])/2.0)/255.0) );
- enc_color1[1] = int( JAS_ROUND(31.0*((avg_color_float1[1]+avg_color_float2[1])/2.0)/255.0) );
- enc_color1[2] = int( JAS_ROUND(31.0*((avg_color_float1[2]+avg_color_float2[2])/2.0)/255.0) );
- enc_color2[0] = enc_color1[0];
- enc_color2[1] = enc_color1[1];
- enc_color2[2] = enc_color1[2];
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- }
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
- // upper part of block
- flip_err = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // lower part of block
- flip_err += tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- // Now lets see which is the best table to use. Only 8 tables are possible.
- if(norm_err <= flip_err)
- {
- compressed1 = compressed1_norm | 0;
- compressed2 = compressed2_norm;
- best_err = norm_err;
- best_flip = 0;
- }
- else
- {
- compressed1 = compressed1_flip | 1;
- compressed2 = compressed2_flip;
- best_err = flip_err;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- best_flip = 1;
- }
- return best_err;
- }
- // Compresses the block using only the differential mode in ETC1/ETC2
- // Uses the average color as the base color in each half-block.
- // If average colors are too different, use the average color of the entire block in both half-blocks.
- // Tries both flipped and unflipped.
- // Uses fixed point arithmetics where 1000 represents 1.0.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockOnlyDiffFlipAveragePerceptual1000(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int compressed1_norm, compressed2_norm;
- unsigned int compressed1_flip, compressed2_flip;
- uint8 avg_color_quant1[3], avg_color_quant2[3];
- float avg_color_float1[3],avg_color_float2[3];
- int enc_color1[3], enc_color2[3], diff[3];
- unsigned int min_error=MAXERR1000;
- unsigned int best_table_indices1=0, best_table_indices2=0;
- unsigned int best_table1=0, best_table2=0;
- int diffbit;
- int norm_err=0;
- int flip_err=0;
- // First try normal blocks 2x4:
- computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( !((diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3)) )
- {
- enc_color1[0] = (enc_color1[0] + enc_color2[0]) >> 1;
- enc_color1[1] = (enc_color1[1] + enc_color2[1]) >> 1;
- enc_color1[2] = (enc_color1[2] + enc_color2[2]) >> 1;
- enc_color2[0] = enc_color1[0];
- enc_color2[1] = enc_color1[1];
- enc_color2[2] = enc_color1[2];
- }
- {
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- norm_err = 0;
- // left part of block
- norm_err = tryalltables_3bittable2x4percep1000(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // right part of block
- norm_err += tryalltables_3bittable2x4percep1000(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- }
- // Now try flipped blocks 4x2:
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( !((diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3)) )
- {
- enc_color1[0] = (enc_color1[0] + enc_color2[0]) >> 1;
- enc_color1[1] = (enc_color1[1] + enc_color2[1]) >> 1;
- enc_color1[2] = (enc_color1[2] + enc_color2[2]) >> 1;
- enc_color2[0] = enc_color1[0];
- enc_color2[1] = enc_color1[1];
- enc_color2[2] = enc_color1[2];
- }
- {
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- // upper part of block
- flip_err = tryalltables_3bittable4x2percep1000(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // lower part of block
- flip_err += tryalltables_3bittable4x2percep1000(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- unsigned int best_err;
- if(norm_err <= flip_err)
- {
- compressed1 = compressed1_norm | 0;
- compressed2 = compressed2_norm;
- best_err = norm_err;
- }
- else
- {
- compressed1 = compressed1_flip | 1;
- compressed2 = compressed2_flip;
- best_err = flip_err;
- }
- return best_err;
- }
- // Compresses the block using both the individual and the differential mode in ETC1/ETC2
- // Uses the average color as the base color in each half-block.
- // Uses a perceptual error metric.
- // Tries both flipped and unflipped.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double compressBlockDiffFlipAveragePerceptual(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int compressed1_norm, compressed2_norm;
- unsigned int compressed1_flip, compressed2_flip;
- uint8 avg_color_quant1[3], avg_color_quant2[3];
- float avg_color_float1[3],avg_color_float2[3];
- int enc_color1[3], enc_color2[3], diff[3];
- int min_error=255*255*8*3;
- unsigned int best_table_indices1=0, best_table_indices2=0;
- unsigned int best_table1=0, best_table2=0;
- int diffbit;
- int norm_err=0;
- int flip_err=0;
- // First try normal blocks 2x4:
- computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- float eps;
- enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
- {
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- norm_err = 0;
- // left part of block
- norm_err = tryalltables_3bittable2x4percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // right part of block
- norm_err += tryalltables_3bittable2x4percep(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- }
- else
- {
- diffbit = 0;
- // The difference is bigger than what fits in 555 plus delta-333, so we will have
- // to deal with 444 444.
- eps = (float) 0.0001;
- enc_color1[0] = int( ((float) avg_color_float1[0] / (17.0)) +0.5 + eps);
- enc_color1[1] = int( ((float) avg_color_float1[1] / (17.0)) +0.5 + eps);
- enc_color1[2] = int( ((float) avg_color_float1[2] / (17.0)) +0.5 + eps);
- enc_color2[0] = int( ((float) avg_color_float2[0] / (17.0)) +0.5 + eps);
- enc_color2[1] = int( ((float) avg_color_float2[1] / (17.0)) +0.5 + eps);
- enc_color2[2] = int( ((float) avg_color_float2[2] / (17.0)) +0.5 + eps);
- avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
- avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
- avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
- avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
- avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
- avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
-
- // Pack bits into the first word.
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_norm, enc_color2[2], 4, 43);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
-
- // left part of block
- norm_err = tryalltables_3bittable2x4percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // right part of block
- norm_err += tryalltables_3bittable2x4percep(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- }
- // Now try flipped blocks 4x2:
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
- enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
- enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
- enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
- enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
- enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
- {
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- // upper part of block
- flip_err = tryalltables_3bittable4x2percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // lower part of block
- flip_err += tryalltables_3bittable4x2percep(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- else
- {
- diffbit = 0;
- // The difference is bigger than what fits in 555 plus delta-333, so we will have
- // to deal with 444 444.
- eps = (float) 0.0001;
- enc_color1[0] = int( ((float) avg_color_float1[0] / (17.0)) +0.5 + eps);
- enc_color1[1] = int( ((float) avg_color_float1[1] / (17.0)) +0.5 + eps);
- enc_color1[2] = int( ((float) avg_color_float1[2] / (17.0)) +0.5 + eps);
- enc_color2[0] = int( ((float) avg_color_float2[0] / (17.0)) +0.5 + eps);
- enc_color2[1] = int( ((float) avg_color_float2[1] / (17.0)) +0.5 + eps);
- enc_color2[2] = int( ((float) avg_color_float2[2] / (17.0)) +0.5 + eps);
- avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
- avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
- avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
- avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
- avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
- avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- // upper part of block
- flip_err = tryalltables_3bittable4x2percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // lower part of block
- flip_err += tryalltables_3bittable4x2percep(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- // Now lets see which is the best table to use. Only 8 tables are possible.
-
- double best_err;
- if(norm_err <= flip_err)
- {
- compressed1 = compressed1_norm | 0;
- compressed2 = compressed2_norm;
- best_err = norm_err;
- }
- else
- {
- compressed1 = compressed1_flip | 1;
- compressed2 = compressed2_flip;
- best_err = flip_err;
- }
- return best_err;
- }
- // This is our structure for matrix data
- struct dMatrix
- {
- int width; // The number of coloumns in the matrix
- int height; // The number of rows in the matrix
- double *data; // The matrix data in row order
- };
- // Multiplies two matrices
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- dMatrix *multiplyMatrices( dMatrix *Amat, dMatrix *Bmat)
- {
- int xx,yy, q;
- dMatrix *resmatrix;
- if(Amat->width != Bmat->height)
- {
- printf("Cannot multiply matrices -- dimensions do not agree.\n");
- exit(1);
- }
- // Allocate space for result
- resmatrix = (dMatrix*) malloc(sizeof(dMatrix));
- resmatrix->width = Bmat->width;
- resmatrix->height = Amat->height;
- resmatrix->data = (double*) malloc(sizeof(double)*(resmatrix->width)*(resmatrix->height));
- for(yy = 0; yy<resmatrix->height; yy++)
- for(xx = 0; xx<resmatrix->width; xx++)
- for(q=0, resmatrix->data[yy*resmatrix->width+xx] = 0.0; q<Amat->width; q++)
- resmatrix->data[yy*resmatrix->width+xx] += Amat->data[yy*Amat->width + q] * Bmat->data[q*Bmat->width+xx];
- return(resmatrix);
- }
- // Transposes a matrix
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void transposeMatrix( dMatrix *mat)
- {
- int xx, yy, zz;
- double *temp;
- int newwidth, newheight;
- temp = (double*) malloc (sizeof(double)*(mat->width)*(mat->height));
- for(zz = 0; zz<((mat->width)*(mat->height)); zz++)
- temp[zz] = mat->data[zz];
- newwidth = mat->height;
- newheight= mat->width;
- for(yy = 0; yy<newheight; yy++)
- for(xx = 0; xx<newwidth; xx++)
- mat->data[yy*newwidth+xx] = temp[xx*(mat->width)+yy];
- mat->height = newheight;
- mat->width = newwidth;
- free(temp);
- }
- // In the planar mode in ETC2, the block can be partitioned as follows:
- //
- // O A A A H
- // B D1 D3 C3
- // B D2 C2 D5
- // B C1 D4 D6
- // V
- // Here A-pixels, B-pixels and C-pixels only depend on two values. For instance, B-pixels only depend on O and V.
- // This can be used to quickly rule out combinations of colors.
- // Here we calculate the minimum error for the block if we know the red component for O and V.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcBBBred(uint8 *block, int colorO, int colorV)
- {
- colorO = (colorO << 2) | (colorO >> 4);
- colorV = (colorV << 2) | (colorV >> 4);
- unsigned int error = 0;
-
- // Now first column: B B B
- /* unroll loop for( yy=0; (yy<4) && (error <= best_error_sofar); yy++)*/
- {
- error = error + square_table[(block[4*4 + 0] - clamp_table[ ((((colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*2 + 0] - clamp_table[ (((((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*3 + 0] - clamp_table[ (((3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- return error;
- }
- // Calculating the minimum error for the block if we know the red component for H and V.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcCCCred(uint8 *block, int colorH, int colorV)
- {
- colorH = (colorH << 2) | (colorH >> 4);
- colorV = (colorV << 2) | (colorV >> 4);
- unsigned int error=0;
- error = error + square_table[(block[4*4*3 + 4 + 0] - clamp_table[ (((colorH + 3*colorV)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*2 + 4*2 + 0] - clamp_table[ (((2*colorH + 2*colorV)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4 + 4*3 + 0] - clamp_table[ (((3*colorH + colorV)+2)>>2) + 255])+255];
-
- return error;
- }
- // Calculating the minimum error for the block if we know the red component for O and H.
- // Uses perceptual error metric.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcLowestPossibleRedOHperceptual(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
- {
- colorO = (colorO << 2) | (colorO >> 4);
- colorH = (colorH << 2) | (colorH >> 4);
- unsigned int error;
- error = square_table_percep_red[(block[0] - colorO) + 255];
- error = error + square_table_percep_red[(block[4] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table_percep_red[(block[4*2] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table_percep_red[(block[4*3] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
-
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the red component for O and H.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcLowestPossibleRedOH(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
- {
- colorO = (colorO << 2) | (colorO >> 4);
- colorH = (colorH << 2) | (colorH >> 4);
- unsigned int error;
- error = square_table[(block[0] - colorO) + 255];
- error = error + square_table[(block[4] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table[(block[4*2] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*3] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
-
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the red component for O and H and V.
- // Uses perceptual error metric.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcErrorPlanarOnlyRedPerceptual(uint8 *block, int colorO, int colorH, int colorV, unsigned int lowest_possible_error, unsigned int BBBvalue, unsigned int CCCvalue, unsigned int best_error_sofar)
- {
- colorO = (colorO << 2) | (colorO >> 4);
- colorH = (colorH << 2) | (colorH >> 4);
- colorV = (colorV << 2) | (colorV >> 4);
- unsigned int error;
- // The block can be partitioned into: O A A A
- // B D1 D3 C3
- // B D2 C2 D5
- // B C1 D4 D6
- int xpart_times_4;
- // The first part: O A A A. It equals lowest_possible_error previously calculated.
- // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
- error = lowest_possible_error + BBBvalue + CCCvalue;
- // The remaining pixels to cover are D1 through D6.
- if(error <= best_error_sofar)
- {
- // Second column: D1 D2 but not C1
- xpart_times_4 = (colorH-colorO);
- error = error + square_table_percep_red[(block[4*4 + 4 + 0] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table_percep_red[(block[4*4*2 + 4 + 0] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- // Third column: D3 notC2 D4
- xpart_times_4 = (colorH-colorO) << 1;
- error = error + square_table_percep_red[(block[4*4 + 4*2 + 0] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table_percep_red[(block[4*4*3 + 4*2 + 0] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- // Forth column: notC3 D5 D6
- xpart_times_4 = 3*(colorH-colorO);
- error = error + square_table_percep_red[(block[4*4*2 + 4*3 + 0] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table_percep_red[(block[4*4*3 + 4*3 + 0] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- }
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the red component for O and H and V.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcErrorPlanarOnlyRed(uint8 *block, int colorO, int colorH, int colorV, unsigned int lowest_possible_error, unsigned int BBBvalue, unsigned int CCCvalue, unsigned int best_error_sofar)
- {
- colorO = (colorO << 2) | (colorO >> 4);
- colorH = (colorH << 2) | (colorH >> 4);
- colorV = (colorV << 2) | (colorV >> 4);
- unsigned int error;
- // The block can be partitioned into: O A A A
- // B D1 D3 C3
- // B D2 C2 D5
- // B C1 D4 D6
- int xpart_times_4;
- // The first part: O A A A. It equals lowest_possible_error previously calculated.
- // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
- error = lowest_possible_error + BBBvalue + CCCvalue;
- // The remaining pixels to cover are D1 through D6.
- if(error <= best_error_sofar)
- {
- // Second column: D1 D2 but not C1
- xpart_times_4 = (colorH-colorO);
- error = error + square_table[(block[4*4 + 4 + 0] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*2 + 4 + 0] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- // Third column: D3 notC2 D4
- xpart_times_4 = (colorH-colorO) << 1;
- error = error + square_table[(block[4*4 + 4*2 + 0] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table[(block[4*4*3 + 4*2 + 0] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- // Forth column: notC3 D5 D6
- xpart_times_4 = 3*(colorH-colorO);
- error = error + square_table[(block[4*4*2 + 4*3 + 0] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*3 + 4*3 + 0] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- }
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the red component for O and H.
- // Uses perceptual error metrics.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcLowestPossibleGreenOHperceptual(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
- {
- colorO = (colorO << 1) | (colorO >> 6);
- colorH = (colorH << 1) | (colorH >> 6);
- unsigned int error;
- error = square_table_percep_green[(block[1] - colorO) + 255];
- error = error + square_table_percep_green[(block[4 + 1] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table_percep_green[(block[4*2 + 1] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table_percep_green[(block[4*3 + 1] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the red component for O and H.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcLowestPossibleGreenOH(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
- {
- colorO = (colorO << 1) | (colorO >> 6);
- colorH = (colorH << 1) | (colorH >> 6);
- unsigned int error;
- error = square_table[(block[1] - colorO) + 255];
- error = error + square_table[(block[4 + 1] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table[(block[4*2 + 1] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*3 + 1] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the green component for O and V.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcBBBgreen(uint8 *block, int colorO, int colorV)
- {
- colorO = (colorO << 1) | (colorO >> 6);
- colorV = (colorV << 1) | (colorV >> 6);
- unsigned int error = 0;
-
- // Now first column: B B B
- /* unroll loop for( yy=0; (yy<4) && (error <= best_error_sofar); yy++)*/
- {
- error = error + square_table[(block[4*4 + 1] - clamp_table[ ((((colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*2 + 1] - clamp_table[ (((((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*3 + 1] - clamp_table[ (((3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the green component for H and V.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcCCCgreen(uint8 *block, int colorH, int colorV)
- {
- colorH = (colorH << 1) | (colorH >> 6);
- colorV = (colorV << 1) | (colorV >> 6);
- unsigned int error=0;
- error = error + square_table[(block[4*4*3 + 4 + 1] - clamp_table[ (((colorH + 3*colorV)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*2 + 4*2 + 1] - clamp_table[ (((2*colorH + 2*colorV)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4 + 4*3 + 1] - clamp_table[ (((3*colorH + colorV)+2)>>2) + 255])+255];
-
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the green component for H V and O.
- // Uses perceptual error metric.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcErrorPlanarOnlyGreenPerceptual(uint8 *block, int colorO, int colorH, int colorV, unsigned int lowest_possible_error, unsigned int BBBvalue, unsigned int CCCvalue, unsigned int best_error_sofar)
- {
- colorO = (colorO << 1) | (colorO >> 6);
- colorH = (colorH << 1) | (colorH >> 6);
- colorV = (colorV << 1) | (colorV >> 6);
- unsigned int error;
- // The block can be partitioned into: O A A A
- // B D1 D3 C3
- // B D2 C2 D5
- // B C1 D4 D6
- int xpart_times_4;
- // The first part: O A A A. It equals lowest_possible_error previously calculated.
- // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
- error = lowest_possible_error + BBBvalue + CCCvalue;
- // The remaining pixels to cover are D1 through D6.
- if(error <= best_error_sofar)
- {
- // Second column: D1 D2 but not C1
- xpart_times_4 = (colorH-colorO);
- error = error + square_table_percep_green[(block[4*4 + 4 + 1] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table_percep_green[(block[4*4*2 + 4 + 1] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- // Third column: D3 notC2 D4
- xpart_times_4 = (colorH-colorO) << 1;
- error = error + square_table_percep_green[(block[4*4 + 4*2 + 1] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table_percep_green[(block[4*4*3 + 4*2 + 1] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- // Forth column: notC3 D5 D6
- xpart_times_4 = 3*(colorH-colorO);
- error = error + square_table_percep_green[(block[4*4*2 + 4*3 + 1] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table_percep_green[(block[4*4*3 + 4*3 + 1] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- }
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the green component for H V and O.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcErrorPlanarOnlyGreen(uint8 *block, int colorO, int colorH, int colorV, unsigned int lowest_possible_error, unsigned int BBBvalue, unsigned int CCCvalue, unsigned int best_error_sofar)
- {
- colorO = (colorO << 1) | (colorO >> 6);
- colorH = (colorH << 1) | (colorH >> 6);
- colorV = (colorV << 1) | (colorV >> 6);
- unsigned int error;
- // The block can be partitioned into: O A A A
- // B D1 D3 C3
- // B D2 C2 D5
- // B C1 D4 D6
- int xpart_times_4;
- // The first part: O A A A. It equals lowest_possible_error previously calculated.
- // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
- error = lowest_possible_error + BBBvalue + CCCvalue;
- // The remaining pixels to cover are D1 through D6.
- if(error <= best_error_sofar)
- {
- // Second column: D1 D2 but not C1
- xpart_times_4 = (colorH-colorO);
- error = error + square_table[(block[4*4 + 4 + 1] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*2 + 4 + 1] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- // Third column: D3 notC2 D4
- xpart_times_4 = (colorH-colorO) << 1;
- error = error + square_table[(block[4*4 + 4*2 + 1] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table[(block[4*4*3 + 4*2 + 1] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- // Forth column: notC3 D5 D6
- xpart_times_4 = 3*(colorH-colorO);
- error = error + square_table[(block[4*4*2 + 4*3 + 1] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*3 + 4*3 + 1] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- }
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the blue component for O and V.
- // Uses perceptual error metric.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcBBBbluePerceptual(uint8 *block, int colorO, int colorV)
- {
- colorO = (colorO << 2) | (colorO >> 4);
- colorV = (colorV << 2) | (colorV >> 4);
- unsigned int error = 0;
-
- // Now first column: B B B
- /* unroll loop for( yy=0; (yy<4) && (error <= best_error_sofar); yy++)*/
- {
- error = error + square_table_percep_blue[(block[4*4 + 2] - clamp_table[ ((((colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table_percep_blue[(block[4*4*2 + 2] - clamp_table[ (((((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table_percep_blue[(block[4*4*3 + 2] - clamp_table[ (((3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the blue component for O and V.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcBBBblue(uint8 *block, int colorO, int colorV)
- {
- colorO = (colorO << 2) | (colorO >> 4);
- colorV = (colorV << 2) | (colorV >> 4);
- unsigned int error = 0;
-
- // Now first column: B B B
- /* unroll loop for( yy=0; (yy<4) && (error <= best_error_sofar); yy++)*/
- {
- error = error + square_table[(block[4*4 + 2] - clamp_table[ ((((colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*2 + 2] - clamp_table[ (((((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*3 + 2] - clamp_table[ (((3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the blue component for H and V.
- // Uses perceptual error metric.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcCCCbluePerceptual(uint8 *block, int colorH, int colorV)
- {
- colorH = (colorH << 2) | (colorH >> 4);
- colorV = (colorV << 2) | (colorV >> 4);
- unsigned int error=0;
- error = error + square_table_percep_blue[(block[4*4*3 + 4 + 2] - clamp_table[ (((colorH + 3*colorV)+2)>>2) + 255])+255];
- error = error + square_table_percep_blue[(block[4*4*2 + 4*2 + 2] - clamp_table[ (((2*colorH + 2*colorV)+2)>>2) + 255])+255];
- error = error + square_table_percep_blue[(block[4*4 + 4*3 + 2] - clamp_table[ (((3*colorH + colorV)+2)>>2) + 255])+255];
-
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the blue component for O and V.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcCCCblue(uint8 *block, int colorH, int colorV)
- {
- colorH = (colorH << 2) | (colorH >> 4);
- colorV = (colorV << 2) | (colorV >> 4);
- unsigned int error=0;
- error = error + square_table[(block[4*4*3 + 4 + 2] - clamp_table[ (((colorH + 3*colorV)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*2 + 4*2 + 2] - clamp_table[ (((2*colorH + 2*colorV)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4 + 4*3 + 2] - clamp_table[ (((3*colorH + colorV)+2)>>2) + 255])+255];
-
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the blue component for O and H.
- // Uses perceptual error metric.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcLowestPossibleBlueOHperceptual(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
- {
- colorO = (colorO << 2) | (colorO >> 4);
- colorH = (colorH << 2) | (colorH >> 4);
- unsigned int error;
- error = square_table_percep_blue[(block[2] - colorO) + 255];
- error = error + square_table_percep_blue[(block[4+2] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table_percep_blue[(block[4*2+2] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table_percep_blue[(block[4*3+2] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
-
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the blue component for O and H.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcLowestPossibleBlueOH(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
- {
- colorO = (colorO << 2) | (colorO >> 4);
- colorH = (colorH << 2) | (colorH >> 4);
- unsigned int error;
- error = square_table[(block[2] - colorO) + 255];
- error = error + square_table[(block[4+2] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table[(block[4*2+2] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*3+2] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
-
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the blue component for O, V and H.
- // Uses perceptual error metric.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcErrorPlanarOnlyBluePerceptual(uint8 *block, int colorO, int colorH, int colorV, unsigned int lowest_possible_error, unsigned int BBBvalue, unsigned int CCCvalue, unsigned int best_error_sofar)
- {
- colorO = (colorO << 2) | (colorO >> 4);
- colorH = (colorH << 2) | (colorH >> 4);
- colorV = (colorV << 2) | (colorV >> 4);
- unsigned int error;
- // The block can be partitioned into: O A A A
- // B D1 D3 C3
- // B D2 C2 D5
- // B C1 D4 D6
- int xpart_times_4;
- // The first part: O A A A. It equals lowest_possible_error previously calculated.
- // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
- error = lowest_possible_error + BBBvalue + CCCvalue;
- // The remaining pixels to cover are D1 through D6.
- if(error <= best_error_sofar)
- {
- // Second column: D1 D2 but not C1
- xpart_times_4 = (colorH-colorO);
- error = error + square_table_percep_blue[(block[4*4 + 4 + 2] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table_percep_blue[(block[4*4*2 + 4 + 2] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- // Third column: D3 notC2 D4
- xpart_times_4 = (colorH-colorO) << 1;
- error = error + square_table_percep_blue[(block[4*4 + 4*2 + 2] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table_percep_blue[(block[4*4*3 + 4*2 + 2] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- // Forth column: notC3 D5 D6
- xpart_times_4 = 3*(colorH-colorO);
- error = error + square_table_percep_blue[(block[4*4*2 + 4*3 + 2] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table_percep_blue[(block[4*4*3 + 4*3 + 2] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- }
-
- return error;
- }
- // Calculating the minimum error for the block (in planar mode) if we know the blue component for O, V and H.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calcErrorPlanarOnlyBlue(uint8 *block, int colorO, int colorH, int colorV, unsigned int lowest_possible_error, unsigned int BBBvalue, unsigned int CCCvalue, unsigned int best_error_sofar)
- {
- colorO = (colorO << 2) | (colorO >> 4);
- colorH = (colorH << 2) | (colorH >> 4);
- colorV = (colorV << 2) | (colorV >> 4);
- unsigned int error;
- // The block can be partitioned into: O A A A
- // B D1 D3 C3
- // B D2 C2 D5
- // B C1 D4 D6
- int xpart_times_4;
- // The first part: O A A A. It equals lowest_possible_error previously calculated.
- // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
- error = lowest_possible_error + BBBvalue + CCCvalue;
- // The remaining pixels to cover are D1 through D6.
- if(error <= best_error_sofar)
- {
- // Second column: D1 D2 but not C1
- xpart_times_4 = (colorH-colorO);
- error = error + square_table[(block[4*4 + 4 + 2] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*2 + 4 + 2] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- // Third column: D3 notC2 D4
- xpart_times_4 = (colorH-colorO) << 1;
- error = error + square_table[(block[4*4 + 4*2 + 2] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- if(error <= best_error_sofar)
- {
- error = error + square_table[(block[4*4*3 + 4*2 + 2] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- // Forth column: notC3 D5 D6
- xpart_times_4 = 3*(colorH-colorO);
- error = error + square_table[(block[4*4*2 + 4*3 + 2] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
- error = error + square_table[(block[4*4*3 + 4*3 + 2] - clamp_table[ (((xpart_times_4 + 3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
- }
- }
- return error;
- }
- // This function uses least squares in order to determine the best values of the plane.
- // This is close to optimal, but not quite, due to nonlinearities in the expantion from 6 and 7 bits to 8, and
- // in the clamping to a number between 0 and the maximum.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockPlanar57(uint8 *img, int width,int height,int startx,int starty, unsigned int &compressed57_1, unsigned int &compressed57_2)
- {
- // Use least squares to find the solution with the smallest error.
- // That is, find the vector x so that |Ax-b|^2 is minimized, where
- // x = [Ro Rr Rv]';
- // A = [1 3/4 2/4 1/4 3/4 2/4 1/4 0 2/4 1/4 0 -1/4 1/4 0 -1/4 -2/4 ;
- // 0 1/4 2/4 3/4 0 1/4 2/4 3/4 0 1/4 2/4 3/4 0 1/4 2/4 3/4 ;
- // 0 0 0 0 1/4 1/4 1/4 1/4 2/4 2/4 2/4 2/4; 3/4 3/4 3/4 3/4]';
- // b = [r11 r12 r13 r14 r21 r22 r23 r24 r31 r32 r33 r34 r41 r42 r43 r44];
- //
- // That is, find solution x = inv(A' * A) * A' * b
- // = C * A' * b;
- // C is always the same, so we have calculated it off-line here.
- // = C * D
- int xx,yy, cc;
- double coeffsA[48]= { 1.00, 0.00, 0.00,
- 0.75, 0.25, 0.00,
- 0.50, 0.50, 0.00,
- 0.25, 0.75, 0.00,
- 0.75, 0.00, 0.25,
- 0.50, 0.25, 0.25,
- 0.25, 0.50, 0.25,
- 0.00, 0.75, 0.25,
- 0.50, 0.00, 0.50,
- 0.25, 0.25, 0.50,
- 0.00, 0.50, 0.50,
- -0.25, 0.75, 0.50,
- 0.25, 0.00, 0.75,
- 0.00, 0.25, 0.75,
- -0.25, 0.50, 0.75,
- -0.50, 0.75, 0.75};
- double coeffsC[9] = {0.2875, -0.0125, -0.0125, -0.0125, 0.4875, -0.3125, -0.0125, -0.3125, 0.4875};
- double colorO[3], colorH[3], colorV[3];
- uint8 colorO8[3], colorH8[3], colorV8[3];
-
- dMatrix *D_matrix;
- dMatrix *x_vector;
- dMatrix A_matrix; A_matrix.width = 3; A_matrix.height = 16;
- A_matrix.data = coeffsA;
- dMatrix C_matrix; C_matrix.width = 3; C_matrix.height = 3;
- C_matrix.data = coeffsC;
- dMatrix b_vector; b_vector.width = 1; b_vector.height = 16;
- b_vector.data = (double*) malloc(sizeof(double)*b_vector.width*b_vector.height);
- transposeMatrix(&A_matrix);
- // Red component
- // Load color data into vector b:
- for(cc = 0, yy = 0; yy<4; yy++)
- for(xx = 0; xx<4; xx++)
- b_vector.data[cc++] = img[3*width*(starty+yy) + 3*(startx+xx) + 0];
- D_matrix = multiplyMatrices(&A_matrix, &b_vector);
- x_vector = multiplyMatrices(&C_matrix, D_matrix);
- colorO[0] = CLAMP(0.0, x_vector->data[0], 255.0);
- colorH[0] = CLAMP(0.0, x_vector->data[1], 255.0);
- colorV[0] = CLAMP(0.0, x_vector->data[2], 255.0);
- free(D_matrix->data); free(D_matrix);
- free(x_vector->data); free(x_vector);
- // Green component
- // Load color data into vector b:
- for(cc = 0, yy = 0; yy<4; yy++)
- for(xx = 0; xx<4; xx++)
- b_vector.data[cc++] = img[3*width*(starty+yy) + 3*(startx+xx) + 1];
- D_matrix = multiplyMatrices(&A_matrix, &b_vector);
- x_vector = multiplyMatrices(&C_matrix, D_matrix);
- colorO[1] = CLAMP(0.0, x_vector->data[0], 255.0);
- colorH[1] = CLAMP(0.0, x_vector->data[1], 255.0);
- colorV[1] = CLAMP(0.0, x_vector->data[2], 255.0);
- free(D_matrix->data); free(D_matrix);
- free(x_vector->data); free(x_vector);
- // Blue component
- // Load color data into vector b:
- for(cc = 0, yy = 0; yy<4; yy++)
- for(xx = 0; xx<4; xx++)
- b_vector.data[cc++] = img[3*width*(starty+yy) + 3*(startx+xx) + 2];
- D_matrix = multiplyMatrices(&A_matrix, &b_vector);
- x_vector = multiplyMatrices(&C_matrix, D_matrix);
- colorO[2] = CLAMP(0.0, x_vector->data[0], 255.0);
- colorH[2] = CLAMP(0.0, x_vector->data[1], 255.0);
- colorV[2] = CLAMP(0.0, x_vector->data[2], 255.0);
- free(D_matrix->data); free(D_matrix);
- free(x_vector->data); free(x_vector);
- // Quantize to 6 bits
- double D = 255*(1.0/((1<<6)-1.0) );
- colorO8[0] = JAS_ROUND((1.0*colorO[0])/D);
- colorO8[2] = JAS_ROUND((1.0*colorO[2])/D);
- colorH8[0] = JAS_ROUND((1.0*colorH[0])/D);
- colorH8[2] = JAS_ROUND((1.0*colorH[2])/D);
- colorV8[0] = JAS_ROUND((1.0*colorV[0])/D);
- colorV8[2] = JAS_ROUND((1.0*colorV[2])/D);
- // Quantize to 7 bits
- D = 255*(1.0/((1<<7)-1.0) );
- colorO8[1] = JAS_ROUND((1.0*colorO[1])/D);
- colorH8[1] = JAS_ROUND((1.0*colorH[1])/D);
- colorV8[1] = JAS_ROUND((1.0*colorV[1])/D);
- // Pack bits in 57 bits
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ------------------------------------------------------------------------------------------------
- // | R0 | G0 | B0 | RH | GH |
- // ------------------------------------------------------------------------------------------------
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // ------------------------------------------------------------------------------------------------
- // | BH | RV | GV | BV | not used |
- // ------------------------------------------------------------------------------------------------
- compressed57_1 = 0;
- compressed57_2 = 0;
- PUTBITSHIGH( compressed57_1, colorO8[0], 6, 63);
- PUTBITSHIGH( compressed57_1, colorO8[1], 7, 57);
- PUTBITSHIGH( compressed57_1, colorO8[2], 6, 50);
- PUTBITSHIGH( compressed57_1, colorH8[0], 6, 44);
- PUTBITSHIGH( compressed57_1, colorH8[1], 7, 38);
- PUTBITS( compressed57_2, colorH8[2], 6, 31);
- PUTBITS( compressed57_2, colorV8[0], 6, 25);
- PUTBITS( compressed57_2, colorV8[1], 7, 19);
- PUTBITS( compressed57_2, colorV8[2], 6, 12);
- }
- // During search it is not convenient to store the bits the way they are stored in the
- // file format. Hence, after search, it is converted to this format.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void stuff57bits(unsigned int planar57_word1, unsigned int planar57_word2, unsigned int &planar_word1, unsigned int &planar_word2)
- {
- // Put bits in twotimer configuration for 57 bits (red and green dont overflow, green does)
- //
- // Go from this bit layout:
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // -----------------------------------------------------------------------------------------------
- // |R0 |G01G02 |B01B02 ;B03 |RH1 |RH2|GH |
- // -----------------------------------------------------------------------------------------------
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // -----------------------------------------------------------------------------------------------
- // |BH |RV |GV |BV | not used |
- // -----------------------------------------------------------------------------------------------
- //
- // To this:
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ------------------------------------------------------------------------------------------------
- // |//|R0 |G01|/|G02 |B01|/ // //|B02 |//|B03 |RH1 |df|RH2|
- // ------------------------------------------------------------------------------------------------
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // -----------------------------------------------------------------------------------------------
- // |GH |BH |RV |GV |BV |
- // -----------------------------------------------------------------------------------------------
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- uint8 RO, GO1, GO2, BO1, BO2, BO3, RH1, RH2, GH, BH, RV, GV, BV;
- uint8 bit, a, b, c, d, bits;
- RO = GETBITSHIGH( planar57_word1, 6, 63);
- GO1= GETBITSHIGH( planar57_word1, 1, 57);
- GO2= GETBITSHIGH( planar57_word1, 6, 56);
- BO1= GETBITSHIGH( planar57_word1, 1, 50);
- BO2= GETBITSHIGH( planar57_word1, 2, 49);
- BO3= GETBITSHIGH( planar57_word1, 3, 47);
- RH1= GETBITSHIGH( planar57_word1, 5, 44);
- RH2= GETBITSHIGH( planar57_word1, 1, 39);
- GH = GETBITSHIGH( planar57_word1, 7, 38);
- BH = GETBITS( planar57_word2, 6, 31);
- RV = GETBITS( planar57_word2, 6, 25);
- GV = GETBITS( planar57_word2, 7, 19);
- BV = GETBITS( planar57_word2, 6, 12);
- planar_word1 = 0; planar_word2 = 0;
- PUTBITSHIGH( planar_word1, RO, 6, 62);
- PUTBITSHIGH( planar_word1, GO1, 1, 56);
- PUTBITSHIGH( planar_word1, GO2, 6, 54);
- PUTBITSHIGH( planar_word1, BO1, 1, 48);
- PUTBITSHIGH( planar_word1, BO2, 2, 44);
- PUTBITSHIGH( planar_word1, BO3, 3, 41);
- PUTBITSHIGH( planar_word1, RH1, 5, 38);
- PUTBITSHIGH( planar_word1, RH2, 1, 32);
- PUTBITS( planar_word2, GH, 7, 31);
- PUTBITS( planar_word2, BH, 6, 24);
- PUTBITS( planar_word2, RV, 6, 18);
- PUTBITS( planar_word2, GV, 7, 12);
- PUTBITS( planar_word2, BV, 6, 5);
- // Make sure that red does not overflow:
- bit = GETBITSHIGH( planar_word1, 1, 62);
- PUTBITSHIGH( planar_word1, !bit, 1, 63);
- // Make sure that green does not overflow:
- bit = GETBITSHIGH( planar_word1, 1, 54);
- PUTBITSHIGH( planar_word1, !bit, 1, 55);
- // Make sure that blue overflows:
- a = GETBITSHIGH( planar_word1, 1, 44);
- b = GETBITSHIGH( planar_word1, 1, 43);
- c = GETBITSHIGH( planar_word1, 1, 41);
- d = GETBITSHIGH( planar_word1, 1, 40);
- // The following bit abcd bit sequences should be padded with ones: 0111, 1010, 1011, 1101, 1110, 1111
- // The following logical expression checks for the presence of any of those:
- bit = (a & c) | (!a & b & c & d) | (a & b & !c & d);
- bits = 0xf*bit;
- PUTBITSHIGH( planar_word1, bits, 3, 47);
- PUTBITSHIGH( planar_word1, !bit, 1, 42);
- // Set diffbit
- PUTBITSHIGH( planar_word1, 1, 1, 33);
- }
- // During search it is not convenient to store the bits the way they are stored in the
- // file format. Hence, after search, it is converted to this format.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void stuff58bits(unsigned int thumbH58_word1, unsigned int thumbH58_word2, unsigned int &thumbH_word1, unsigned int &thumbH_word2)
- {
- // Put bits in twotimer configuration for 58 (red doesn't overflow, green does)
- //
- // Go from this bit layout:
- //
- //
- // |63 62 61 60 59 58|57 56 55 54|53 52 51 50|49 48 47 46|45 44 43 42|41 40 39 38|37 36 35 34|33 32|
- // |-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
- //
- // |31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- // |---------------------------------------index bits----------------------------------------------|
- //
- // To this:
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // -----------------------------------------------------------------------------------------------
- // |//|R0 |G0 |// // //|G0|B0|//|B0b |R1 |G1 |B0 |d2|df|d1|
- // -----------------------------------------------------------------------------------------------
- //
- // |31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- // |---------------------------------------index bits----------------------------------------------|
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // -----------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |df|fp|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bt|bt|
- // -----------------------------------------------------------------------------------------------
- //
- //
- // Thus, what we are really doing is going from this bit layout:
- //
- //
- // |63 62 61 60 59 58|57 56 55 54 53 52 51|50 49|48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33|32 |
- // |-------empty-----|part0---------------|part1|part2------------------------------------------|part3|
- //
- // To this:
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // --------------------------------------------------------------------------------------------------|
- // |//|part0 |// // //|part1|//|part2 |df|part3|
- // --------------------------------------------------------------------------------------------------|
- unsigned int part0, part1, part2, part3;
- uint8 bit, a, b, c, d, bits;
- // move parts
- part0 = GETBITSHIGH( thumbH58_word1, 7, 57);
- part1 = GETBITSHIGH( thumbH58_word1, 2, 50);
- part2 = GETBITSHIGH( thumbH58_word1,16, 48);
- part3 = GETBITSHIGH( thumbH58_word1, 1, 32);
- thumbH_word1 = 0;
- PUTBITSHIGH( thumbH_word1, part0, 7, 62);
- PUTBITSHIGH( thumbH_word1, part1, 2, 52);
- PUTBITSHIGH( thumbH_word1, part2, 16, 49);
- PUTBITSHIGH( thumbH_word1, part3, 1, 32);
- // Make sure that red does not overflow:
- bit = GETBITSHIGH( thumbH_word1, 1, 62);
- PUTBITSHIGH( thumbH_word1, !bit, 1, 63);
- // Make sure that green overflows:
- a = GETBITSHIGH( thumbH_word1, 1, 52);
- b = GETBITSHIGH( thumbH_word1, 1, 51);
- c = GETBITSHIGH( thumbH_word1, 1, 49);
- d = GETBITSHIGH( thumbH_word1, 1, 48);
- // The following bit abcd bit sequences should be padded with ones: 0111, 1010, 1011, 1101, 1110, 1111
- // The following logical expression checks for the presence of any of those:
- bit = (a & c) | (!a & b & c & d) | (a & b & !c & d);
- bits = 0xf*bit;
- PUTBITSHIGH( thumbH_word1, bits, 3, 55);
- PUTBITSHIGH( thumbH_word1, !bit, 1, 50);
- // Set diffbit
- PUTBITSHIGH( thumbH_word1, 1, 1, 33);
- thumbH_word2 = thumbH58_word2;
- }
- // copy of above, but diffbit is 0
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void stuff58bitsDiffFalse(unsigned int thumbH58_word1, unsigned int thumbH58_word2, unsigned int &thumbH_word1, unsigned int &thumbH_word2)
- {
- unsigned int part0, part1, part2, part3;
- uint8 bit, a, b, c, d, bits;
- // move parts
- part0 = GETBITSHIGH( thumbH58_word1, 7, 57);
- part1 = GETBITSHIGH( thumbH58_word1, 2, 50);
- part2 = GETBITSHIGH( thumbH58_word1,16, 48);
- part3 = GETBITSHIGH( thumbH58_word1, 1, 32);
- thumbH_word1 = 0;
- PUTBITSHIGH( thumbH_word1, part0, 7, 62);
- PUTBITSHIGH( thumbH_word1, part1, 2, 52);
- PUTBITSHIGH( thumbH_word1, part2, 16, 49);
- PUTBITSHIGH( thumbH_word1, part3, 1, 32);
- // Make sure that red does not overflow:
- bit = GETBITSHIGH( thumbH_word1, 1, 62);
- PUTBITSHIGH( thumbH_word1, !bit, 1, 63);
- // Make sure that green overflows:
- a = GETBITSHIGH( thumbH_word1, 1, 52);
- b = GETBITSHIGH( thumbH_word1, 1, 51);
- c = GETBITSHIGH( thumbH_word1, 1, 49);
- d = GETBITSHIGH( thumbH_word1, 1, 48);
- // The following bit abcd bit sequences should be padded with ones: 0111, 1010, 1011, 1101, 1110, 1111
- // The following logical expression checks for the presence of any of those:
- bit = (a & c) | (!a & b & c & d) | (a & b & !c & d);
- bits = 0xf*bit;
- PUTBITSHIGH( thumbH_word1, bits, 3, 55);
- PUTBITSHIGH( thumbH_word1, !bit, 1, 50);
- // Set diffbit
- PUTBITSHIGH( thumbH_word1, 0, 1, 33);
- thumbH_word2 = thumbH58_word2;
- }
- // During search it is not convenient to store the bits the way they are stored in the
- // file format. Hence, after search, it is converted to this format.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void stuff59bits(unsigned int thumbT59_word1, unsigned int thumbT59_word2, unsigned int &thumbT_word1, unsigned int &thumbT_word2)
- {
- // Put bits in twotimer configuration for 59 (red overflows)
- //
- // Go from this bit layout:
- //
- // |63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
- // |----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
- //
- // |31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- // |----------------------------------------index bits---------------------------------------------|
- //
- //
- // To this:
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // -----------------------------------------------------------------------------------------------
- // |// // //|R0a |//|R0b |G0 |B0 |R1 |G1 |B1 |da |df|db|
- // -----------------------------------------------------------------------------------------------
- //
- // |31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- // |----------------------------------------index bits---------------------------------------------|
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // -----------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |df|fp|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bt|bt|
- // ------------------------------------------------------------------------------------------------
- uint8 R0a;
- uint8 bit, a, b, c, d, bits;
- R0a = GETBITSHIGH( thumbT59_word1, 2, 58);
- // Fix middle part
- thumbT_word1 = thumbT59_word1 << 1;
- // Fix R0a (top two bits of R0)
- PUTBITSHIGH( thumbT_word1, R0a, 2, 60);
- // Fix db (lowest bit of d)
- PUTBITSHIGH( thumbT_word1, thumbT59_word1, 1, 32);
- //
- // Make sure that red overflows:
- a = GETBITSHIGH( thumbT_word1, 1, 60);
- b = GETBITSHIGH( thumbT_word1, 1, 59);
- c = GETBITSHIGH( thumbT_word1, 1, 57);
- d = GETBITSHIGH( thumbT_word1, 1, 56);
- // The following bit abcd bit sequences should be padded with ones: 0111, 1010, 1011, 1101, 1110, 1111
- // The following logical expression checks for the presence of any of those:
- bit = (a & c) | (!a & b & c & d) | (a & b & !c & d);
- bits = 0xf*bit;
- PUTBITSHIGH( thumbT_word1, bits, 3, 63);
- PUTBITSHIGH( thumbT_word1, !bit, 1, 58);
- // Set diffbit
- PUTBITSHIGH( thumbT_word1, 1, 1, 33);
- thumbT_word2 = thumbT59_word2;
- }
- // Decompress the planar mode and calculate the error per component compared to original image.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void decompressBlockPlanar57errorPerComponent(unsigned int compressed57_1, unsigned int compressed57_2, uint8 *img,int width,int height,int startx,int starty, uint8 *srcimg, unsigned int &error_red, unsigned int &error_green, unsigned int &error_blue)
- {
- uint8 colorO[3], colorH[3], colorV[3];
- colorO[0] = GETBITSHIGH( compressed57_1, 6, 63);
- colorO[1] = GETBITSHIGH( compressed57_1, 7, 57);
- colorO[2] = GETBITSHIGH( compressed57_1, 6, 50);
- colorH[0] = GETBITSHIGH( compressed57_1, 6, 44);
- colorH[1] = GETBITSHIGH( compressed57_1, 7, 38);
- colorH[2] = GETBITS( compressed57_2, 6, 31);
- colorV[0] = GETBITS( compressed57_2, 6, 25);
- colorV[1] = GETBITS( compressed57_2, 7, 19);
- colorV[2] = GETBITS( compressed57_2, 6, 12);
- colorO[0] = (colorO[0] << 2) | (colorO[0] >> 4);
- colorO[1] = (colorO[1] << 1) | (colorO[1] >> 6);
- colorO[2] = (colorO[2] << 2) | (colorO[2] >> 4);
- colorH[0] = (colorH[0] << 2) | (colorH[0] >> 4);
- colorH[1] = (colorH[1] << 1) | (colorH[1] >> 6);
- colorH[2] = (colorH[2] << 2) | (colorH[2] >> 4);
- colorV[0] = (colorV[0] << 2) | (colorV[0] >> 4);
- colorV[1] = (colorV[1] << 1) | (colorV[1] >> 6);
- colorV[2] = (colorV[2] << 2) | (colorV[2] >> 4);
-
- int xx, yy;
- for( xx=0; xx<4; xx++)
- {
- for( yy=0; yy<4; yy++)
- {
- img[3*width*(starty+yy) + 3*(startx+xx) + 0] = (int)CLAMP(0, JAS_ROUND((xx*(colorH[0]-colorO[0])/4.0 + yy*(colorV[0]-colorO[0])/4.0 + colorO[0])), 255);
- img[3*width*(starty+yy) + 3*(startx+xx) + 1] = (int)CLAMP(0, JAS_ROUND((xx*(colorH[1]-colorO[1])/4.0 + yy*(colorV[1]-colorO[1])/4.0 + colorO[1])), 255);
- img[3*width*(starty+yy) + 3*(startx+xx) + 2] = (int)CLAMP(0, JAS_ROUND((xx*(colorH[2]-colorO[2])/4.0 + yy*(colorV[2]-colorO[2])/4.0 + colorO[2])), 255);
- }
- }
- error_red = 0;
- error_green= 0;
- error_blue = 0;
- for( xx=0; xx<4; xx++)
- {
- for( yy=0; yy<4; yy++)
- {
- error_red = error_red + SQUARE(srcimg[3*width*(starty+yy) + 3*(startx+xx) + 0] - img[3*width*(starty+yy) + 3*(startx+xx) + 0]);
- error_green = error_green + SQUARE(srcimg[3*width*(starty+yy) + 3*(startx+xx) + 1] - img[3*width*(starty+yy) + 3*(startx+xx) + 1]);
- error_blue = error_blue + SQUARE(srcimg[3*width*(starty+yy) + 3*(startx+xx) + 2] - img[3*width*(starty+yy) + 3*(startx+xx) + 2]);
- }
- }
- }
- // Compress using both individual and differential mode in ETC1/ETC2 using combined color
- // quantization. Both flip modes are tried.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockDiffFlipCombined(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int compressed1_norm, compressed2_norm;
- unsigned int compressed1_flip, compressed2_flip;
- uint8 avg_color_quant1[3], avg_color_quant2[3];
- float avg_color_float1[3],avg_color_float2[3];
- int enc_color1[3], enc_color2[3], diff[3];
- int min_error=255*255*8*3;
- unsigned int best_table_indices1=0, best_table_indices2=0;
- unsigned int best_table1=0, best_table2=0;
- int diffbit;
- int norm_err=0;
- int flip_err=0;
- // First try normal blocks 2x4:
- computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- float eps;
- uint8 dummy[3];
- quantize555ColorCombined(avg_color_float1, enc_color1, dummy);
- quantize555ColorCombined(avg_color_float2, enc_color2, dummy);
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
- {
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- norm_err = 0;
- // left part of block
- norm_err = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // right part of block
- norm_err += tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- }
- else
- {
- diffbit = 0;
- // The difference is bigger than what fits in 555 plus delta-333, so we will have
- // to deal with 444 444.
- eps = (float) 0.0001;
- uint8 dummy[3];
- quantize444ColorCombined(avg_color_float1, enc_color1, dummy);
- quantize444ColorCombined(avg_color_float2, enc_color2, dummy);
- avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
- avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
- avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
- avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
- avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
- avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
-
- // Pack bits into the first word.
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_norm, enc_color2[2], 4, 43);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- // left part of block
- norm_err = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // right part of block
- norm_err += tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- }
- // Now try flipped blocks 4x2:
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- quantize555ColorCombined(avg_color_float1, enc_color1, dummy);
- quantize555ColorCombined(avg_color_float2, enc_color2, dummy);
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
- {
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- // upper part of block
- flip_err = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // lower part of block
- flip_err += tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- else
- {
- diffbit = 0;
- // The difference is bigger than what fits in 555 plus delta-333, so we will have
- // to deal with 444 444.
- eps = (float) 0.0001;
- uint8 dummy[3];
- quantize444ColorCombined(avg_color_float1, enc_color1, dummy);
- quantize444ColorCombined(avg_color_float2, enc_color2, dummy);
- avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
- avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
- avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
- avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
- avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
- avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- // upper part of block
- flip_err = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // lower part of block
- flip_err += tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- // Now lets see which is the best table to use. Only 8 tables are possible.
- if(norm_err <= flip_err)
- {
- compressed1 = compressed1_norm | 0;
- compressed2 = compressed2_norm;
- }
- else
- {
- compressed1 = compressed1_flip | 1;
- compressed2 = compressed2_flip;
- }
- }
- // Calculation of the two block colors using the LBG-algorithm
- // The following method scales down the intensity, since this can be compensated for anyway by both the H and T mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void computeColorLBGHalfIntensityFast(uint8 *img,int width,int startx,int starty, uint8 (LBG_colors)[2][3])
- {
- uint8 block_mask[4][4];
- // reset rand so that we get predictable output per block
- srand(10000);
- //LBG-algorithm
- double D = 0, oldD, bestD = MAXIMUM_ERROR, eps = 0.0000000001;
- double error_a, error_b;
- int number_of_iterations = 10;
- double t_color[2][3];
- double original_colors[4][4][3];
- double current_colors[2][3];
- double best_colors[2][3];
- double max_v[3];
- double min_v[3];
- int x,y,i;
- double red, green, blue;
- bool continue_seeding;
- int maximum_number_of_seedings = 10;
- int seeding;
- bool continue_iterate;
- max_v[R] = -512.0; max_v[G] = -512.0; max_v[B] = -512.0;
- min_v[R] = 512.0; min_v[G] = 512.0; min_v[B] = 512.0;
- // resolve trainingdata
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (x = 0; x < BLOCKWIDTH; ++x)
- {
- red = img[3*((starty+y)*width+startx+x)+R];
- green = img[3*((starty+y)*width+startx+x)+G];
- blue = img[3*((starty+y)*width+startx+x)+B];
- // Use qrs representation instead of rgb
- // qrs = Q * rgb where Q = [a a a ; b -b 0 ; c c -2c]; a = 1/sqrt(3), b= 1/sqrt(2), c = 1/sqrt(6);
- // rgb = inv(Q)*qrs = Q' * qrs where ' denotes transpose.
- // The q variable holds intensity. r and s hold chrominance.
- // q = [0, sqrt(3)*255], r = [-255/sqrt(2), 255/sqrt(2)], s = [-2*255/sqrt(6), 2*255/sqrt(6)];
- //
- // The LGB algorithm will only act on the r and s variables and not on q.
- //
- original_colors[x][y][R] = (1.0/sqrt(1.0*3))*red + (1.0/sqrt(1.0*3))*green + (1.0/sqrt(1.0*3))*blue;
- original_colors[x][y][G] = (1.0/sqrt(1.0*2))*red - (1.0/sqrt(1.0*2))*green;
- original_colors[x][y][B] = (1.0/sqrt(1.0*6))*red + (1.0/sqrt(1.0*6))*green - (2.0/sqrt(1.0*6))*blue;
-
- // find max
- if (original_colors[x][y][R] > max_v[R]) max_v[R] = original_colors[x][y][R];
- if (original_colors[x][y][G] > max_v[G]) max_v[G] = original_colors[x][y][G];
- if (original_colors[x][y][B] > max_v[B]) max_v[B] = original_colors[x][y][B];
- // find min
- if (original_colors[x][y][R] < min_v[R]) min_v[R] = original_colors[x][y][R];
- if (original_colors[x][y][G] < min_v[G]) min_v[G] = original_colors[x][y][G];
- if (original_colors[x][y][B] < min_v[B]) min_v[B] = original_colors[x][y][B];
- }
- }
- D = 512*512*3*16.0;
- bestD = 512*512*3*16.0;
- continue_seeding = true;
- // loop seeds
- for (seeding = 0; (seeding < maximum_number_of_seedings) && continue_seeding; seeding++)
- {
- // hopefully we will not need more seedings:
- continue_seeding = false;
- // calculate seeds
- for (uint8 s = 0; s < 2; ++s)
- {
- for (uint8 c = 0; c < 3; ++c)
- {
- current_colors[s][c] = double((double(rand())/RAND_MAX)*(max_v[c]-min_v[c])) + min_v[c];
- }
- }
-
- // divide into two quantization sets and calculate distortion
- continue_iterate = true;
- for(i = 0; (i < number_of_iterations) && continue_iterate; i++)
- {
- oldD = D;
- D = 0;
- int n = 0;
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (int x = 0; x < BLOCKWIDTH; ++x)
- {
- error_a = 0.5*SQUARE(original_colors[x][y][R] - current_colors[0][R]) +
- SQUARE(original_colors[x][y][G] - current_colors[0][G]) +
- SQUARE(original_colors[x][y][B] - current_colors[0][B]);
- error_b = 0.5*SQUARE(original_colors[x][y][R] - current_colors[1][R]) +
- SQUARE(original_colors[x][y][G] - current_colors[1][G]) +
- SQUARE(original_colors[x][y][B] - current_colors[1][B]);
- if (error_a < error_b)
- {
- block_mask[x][y] = 0;
- D += error_a;
- ++n;
- }
- else
- {
- block_mask[x][y] = 1;
- D += error_b;
- }
- }
- }
- // compare with old distortion
- if (D == 0)
- {
- // Perfect score -- we dont need to go further iterations.
- continue_iterate = false;
- continue_seeding = false;
- }
- if (D == oldD)
- {
- // Same score as last round -- no need to go for further iterations.
- continue_iterate = false;
- continue_seeding = false;
- }
- if (D < bestD)
- {
- bestD = D;
- for(uint8 s = 0; s < 2; ++s)
- {
- for(uint8 c = 0; c < 3; ++c)
- {
- best_colors[s][c] = current_colors[s][c];
- }
- }
- }
- if (n == 0 || n == BLOCKWIDTH*BLOCKHEIGHT)
- {
- // All colors end up in the same voroni region. We need to reseed.
- continue_iterate = false;
- continue_seeding = true;
- }
- else
- {
- // Calculate new reconstruction points using the centroids
- // Find new construction values from average
- t_color[0][R] = 0;
- t_color[0][G] = 0;
- t_color[0][B] = 0;
- t_color[1][R] = 0;
- t_color[1][G] = 0;
- t_color[1][B] = 0;
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (int x = 0; x < BLOCKWIDTH; ++x)
- {
- // use dummy value for q-parameter
- t_color[block_mask[x][y]][R] += original_colors[x][y][R];
- t_color[block_mask[x][y]][G] += original_colors[x][y][G];
- t_color[block_mask[x][y]][B] += original_colors[x][y][B];
- }
- }
- current_colors[0][R] = t_color[0][R] / n;
- current_colors[1][R] = t_color[1][R] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- current_colors[0][G] = t_color[0][G] / n;
- current_colors[1][G] = t_color[1][G] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- current_colors[0][B] = t_color[0][B] / n;
- current_colors[1][B] = t_color[1][B] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- }
- }
- }
- for(x=0;x<2;x++)
- {
- double qq, rr, ss;
- qq = best_colors[x][0];
- rr = best_colors[x][1];
- ss = best_colors[x][2];
- current_colors[x][0] = CLAMP(0, (1.0/sqrt(1.0*3))*qq + (1.0/sqrt(1.0*2))*rr + (1.0/sqrt(1.0*6))*ss, 255);
- current_colors[x][1] = CLAMP(0, (1.0/sqrt(1.0*3))*qq - (1.0/sqrt(1.0*2))*rr + (1.0/sqrt(1.0*6))*ss, 255);
- current_colors[x][2] = CLAMP(0, (1.0/sqrt(1.0*3))*qq + (0.0 )*rr - (2.0/sqrt(1.0*6))*ss, 255);
- }
- for(x=0;x<2;x++)
- for(y=0;y<3;y++)
- LBG_colors[x][y] = JAS_ROUND(current_colors[x][y]);
- }
- // Calculation of the two block colors using the LBG-algorithm
- // The following method scales down the intensity, since this can be compensated for anyway by both the H and T mode.
- // Faster version
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void computeColorLBGNotIntensityFast(uint8 *img,int width,int startx,int starty, uint8 (LBG_colors)[2][3])
- {
- uint8 block_mask[4][4];
- // reset rand so that we get predictable output per block
- srand(10000);
- //LBG-algorithm
- double D = 0, oldD, bestD = MAXIMUM_ERROR, eps = 0.0000000001;
- double error_a, error_b;
- int number_of_iterations = 10;
- double t_color[2][3];
- double original_colors[4][4][3];
- double current_colors[2][3];
- double best_colors[2][3];
- double max_v[3];
- double min_v[3];
- int x,y,i;
- double red, green, blue;
- bool continue_seeding;
- int maximum_number_of_seedings = 10;
- int seeding;
- bool continue_iterate;
- max_v[R] = -512.0; max_v[G] = -512.0; max_v[B] = -512.0;
- min_v[R] = 512.0; min_v[G] = 512.0; min_v[B] = 512.0;
- // resolve trainingdata
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (x = 0; x < BLOCKWIDTH; ++x)
- {
- red = img[3*((starty+y)*width+startx+x)+R];
- green = img[3*((starty+y)*width+startx+x)+G];
- blue = img[3*((starty+y)*width+startx+x)+B];
- // Use qrs representation instead of rgb
- // qrs = Q * rgb where Q = [a a a ; b -b 0 ; c c -2c]; a = 1/sqrt(1.0*3), b= 1/sqrt(1.0*2), c = 1/sqrt(1.0*6);
- // rgb = inv(Q)*qrs = Q' * qrs where ' denotes transpose.
- // The q variable holds intensity. r and s hold chrominance.
- // q = [0, sqrt(1.0*3)*255], r = [-255/sqrt(1.0*2), 255/sqrt(1.0*2)], s = [-2*255/sqrt(1.0*6), 2*255/sqrt(1.0*6)];
- //
- // The LGB algorithm will only act on the r and s variables and not on q.
- //
- original_colors[x][y][R] = (1.0/sqrt(1.0*3))*red + (1.0/sqrt(1.0*3))*green + (1.0/sqrt(1.0*3))*blue;
- original_colors[x][y][G] = (1.0/sqrt(1.0*2))*red - (1.0/sqrt(1.0*2))*green;
- original_colors[x][y][B] = (1.0/sqrt(1.0*6))*red + (1.0/sqrt(1.0*6))*green - (2.0/sqrt(1.0*6))*blue;
-
- // find max
- if (original_colors[x][y][R] > max_v[R]) max_v[R] = original_colors[x][y][R];
- if (original_colors[x][y][G] > max_v[G]) max_v[G] = original_colors[x][y][G];
- if (original_colors[x][y][B] > max_v[B]) max_v[B] = original_colors[x][y][B];
- // find min
- if (original_colors[x][y][R] < min_v[R]) min_v[R] = original_colors[x][y][R];
- if (original_colors[x][y][G] < min_v[G]) min_v[G] = original_colors[x][y][G];
- if (original_colors[x][y][B] < min_v[B]) min_v[B] = original_colors[x][y][B];
- }
- }
- D = 512*512*3*16.0;
- bestD = 512*512*3*16.0;
- continue_seeding = true;
- // loop seeds
- for (seeding = 0; (seeding < maximum_number_of_seedings) && continue_seeding; seeding++)
- {
- // hopefully we will not need more seedings:
- continue_seeding = false;
- // calculate seeds
- for (uint8 s = 0; s < 2; ++s)
- {
- for (uint8 c = 0; c < 3; ++c)
- {
- current_colors[s][c] = double((double(rand())/RAND_MAX)*(max_v[c]-min_v[c])) + min_v[c];
- }
- }
- // divide into two quantization sets and calculate distortion
- continue_iterate = true;
- for(i = 0; (i < number_of_iterations) && continue_iterate; i++)
- {
- oldD = D;
- D = 0;
- int n = 0;
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (int x = 0; x < BLOCKWIDTH; ++x)
- {
- error_a = 0.0*SQUARE(original_colors[x][y][R] - current_colors[0][R]) +
- SQUARE(original_colors[x][y][G] - current_colors[0][G]) +
- SQUARE(original_colors[x][y][B] - current_colors[0][B]);
- error_b = 0.0*SQUARE(original_colors[x][y][R] - current_colors[1][R]) +
- SQUARE(original_colors[x][y][G] - current_colors[1][G]) +
- SQUARE(original_colors[x][y][B] - current_colors[1][B]);
- if (error_a < error_b)
- {
- block_mask[x][y] = 0;
- D += error_a;
- ++n;
- }
- else
- {
- block_mask[x][y] = 1;
- D += error_b;
- }
- }
- }
- // compare with old distortion
- if (D == 0)
- {
- // Perfect score -- we dont need to go further iterations.
- continue_iterate = false;
- continue_seeding = false;
- }
- if (D == oldD)
- {
- // Same score as last round -- no need to go for further iterations.
- continue_iterate = false;
- continue_seeding = false;
- }
- if (D < bestD)
- {
- bestD = D;
- for(uint8 s = 0; s < 2; ++s)
- {
- for(uint8 c = 0; c < 3; ++c)
- {
- best_colors[s][c] = current_colors[s][c];
- }
- }
- }
- if (n == 0 || n == BLOCKWIDTH*BLOCKHEIGHT)
- {
- // All colors end up in the same voroni region. We need to reseed.
- continue_iterate = false;
- continue_seeding = true;
- }
- else
- {
- // Calculate new reconstruction points using the centroids
- // Find new construction values from average
- t_color[0][R] = 0;
- t_color[0][G] = 0;
- t_color[0][B] = 0;
- t_color[1][R] = 0;
- t_color[1][G] = 0;
- t_color[1][B] = 0;
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (int x = 0; x < BLOCKWIDTH; ++x)
- {
- // use dummy value for q-parameter
- t_color[block_mask[x][y]][R] += original_colors[x][y][R];
- t_color[block_mask[x][y]][G] += original_colors[x][y][G];
- t_color[block_mask[x][y]][B] += original_colors[x][y][B];
- }
- }
- current_colors[0][R] = t_color[0][R] / n;
- current_colors[1][R] = t_color[1][R] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- current_colors[0][G] = t_color[0][G] / n;
- current_colors[1][G] = t_color[1][G] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- current_colors[0][B] = t_color[0][B] / n;
- current_colors[1][B] = t_color[1][B] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- }
- }
- }
- for(x=0;x<2;x++)
- {
- double qq, rr, ss;
- qq = best_colors[x][0];
- rr = best_colors[x][1];
- ss = best_colors[x][2];
- current_colors[x][0] = CLAMP(0, (1.0/sqrt(1.0*3))*qq + (1.0/sqrt(1.0*2))*rr + (1.0/sqrt(1.0*6))*ss, 255);
- current_colors[x][1] = CLAMP(0, (1.0/sqrt(1.0*3))*qq - (1.0/sqrt(1.0*2))*rr + (1.0/sqrt(1.0*6))*ss, 255);
- current_colors[x][2] = CLAMP(0, (1.0/sqrt(1.0*3))*qq + (0.0 )*rr - (2.0/sqrt(1.0*6))*ss, 255);
- }
- for(x=0;x<2;x++)
- for(y=0;y<3;y++)
- LBG_colors[x][y] = JAS_ROUND(current_colors[x][y]);
- }
- // Calculation of the two block colors using the LBG-algorithm
- // The following method completely ignores the intensity, since this can be compensated for anyway by both the H and T mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void computeColorLBGNotIntensity(uint8 *img,int width,int startx,int starty, uint8 (LBG_colors)[2][3])
- {
- uint8 block_mask[4][4];
- // reset rand so that we get predictable output per block
- srand(10000);
- //LBG-algorithm
- double D = 0, oldD, bestD = MAXIMUM_ERROR, eps = 0.0000000001;
- double error_a, error_b;
- int number_of_iterations = 10;
- double t_color[2][3];
- double original_colors[4][4][3];
- double current_colors[2][3];
- double best_colors[2][3];
- double max_v[3];
- double min_v[3];
- int x,y,i;
- double red, green, blue;
- bool continue_seeding;
- int maximum_number_of_seedings = 10;
- int seeding;
- bool continue_iterate;
- max_v[R] = -512.0; max_v[G] = -512.0; max_v[B] = -512.0;
- min_v[R] = 512.0; min_v[G] = 512.0; min_v[B] = 512.0;
- // resolve trainingdata
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (x = 0; x < BLOCKWIDTH; ++x)
- {
- red = img[3*((starty+y)*width+startx+x)+R];
- green = img[3*((starty+y)*width+startx+x)+G];
- blue = img[3*((starty+y)*width+startx+x)+B];
- // Use qrs representation instead of rgb
- // qrs = Q * rgb where Q = [a a a ; b -b 0 ; c c -2c]; a = 1/sqrt(1.0*3), b= 1/sqrt(1.0*2), c = 1/sqrt(1.0*6);
- // rgb = inv(Q)*qrs = Q' * qrs where ' denotes transpose.
- // The q variable holds intensity. r and s hold chrominance.
- // q = [0, sqrt(1.0*3)*255], r = [-255/sqrt(1.0*2), 255/sqrt(1.0*2)], s = [-2*255/sqrt(1.0*6), 2*255/sqrt(1.0*6)];
- //
- // The LGB algorithm will only act on the r and s variables and not on q.
- //
- original_colors[x][y][R] = (1.0/sqrt(1.0*3))*red + (1.0/sqrt(1.0*3))*green + (1.0/sqrt(1.0*3))*blue;
- original_colors[x][y][G] = (1.0/sqrt(1.0*2))*red - (1.0/sqrt(1.0*2))*green;
- original_colors[x][y][B] = (1.0/sqrt(1.0*6))*red + (1.0/sqrt(1.0*6))*green - (2.0/sqrt(1.0*6))*blue;
- // find max
- if (original_colors[x][y][R] > max_v[R]) max_v[R] = original_colors[x][y][R];
- if (original_colors[x][y][G] > max_v[G]) max_v[G] = original_colors[x][y][G];
- if (original_colors[x][y][B] > max_v[B]) max_v[B] = original_colors[x][y][B];
- // find min
- if (original_colors[x][y][R] < min_v[R]) min_v[R] = original_colors[x][y][R];
- if (original_colors[x][y][G] < min_v[G]) min_v[G] = original_colors[x][y][G];
- if (original_colors[x][y][B] < min_v[B]) min_v[B] = original_colors[x][y][B];
- }
- }
- D = 512*512*3*16.0;
- bestD = 512*512*3*16.0;
- continue_seeding = true;
- // loop seeds
- for (seeding = 0; (seeding < maximum_number_of_seedings) && continue_seeding; seeding++)
- {
- // hopefully we will not need more seedings:
- continue_seeding = false;
- // calculate seeds
- for (uint8 s = 0; s < 2; ++s)
- {
- for (uint8 c = 0; c < 3; ++c)
- {
- current_colors[s][c] = double((double(rand())/RAND_MAX)*(max_v[c]-min_v[c])) + min_v[c];
- }
- }
-
- // divide into two quantization sets and calculate distortion
- continue_iterate = true;
- for(i = 0; (i < number_of_iterations) && continue_iterate; i++)
- {
- oldD = D;
- D = 0;
- int n = 0;
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (int x = 0; x < BLOCKWIDTH; ++x)
- {
- error_a = 0.0*SQUARE(original_colors[x][y][R] - current_colors[0][R]) +
- SQUARE(original_colors[x][y][G] - current_colors[0][G]) +
- SQUARE(original_colors[x][y][B] - current_colors[0][B]);
- error_b = 0.0*SQUARE(original_colors[x][y][R] - current_colors[1][R]) +
- SQUARE(original_colors[x][y][G] - current_colors[1][G]) +
- SQUARE(original_colors[x][y][B] - current_colors[1][B]);
- if (error_a < error_b)
- {
- block_mask[x][y] = 0;
- D += error_a;
- ++n;
- }
- else
- {
- block_mask[x][y] = 1;
- D += error_b;
- }
- }
- }
- // compare with old distortion
- if (D == 0)
- {
- // Perfect score -- we dont need to go further iterations.
- continue_iterate = false;
- continue_seeding = false;
- }
- if (D == oldD)
- {
- // Same score as last round -- no need to go for further iterations.
- continue_iterate = false;
- continue_seeding = true;
- }
- if (D < bestD)
- {
- bestD = D;
- for(uint8 s = 0; s < 2; ++s)
- {
- for(uint8 c = 0; c < 3; ++c)
- {
- best_colors[s][c] = current_colors[s][c];
- }
- }
- }
- if (n == 0 || n == BLOCKWIDTH*BLOCKHEIGHT)
- {
- // All colors end up in the same voroni region. We need to reseed.
- continue_iterate = false;
- continue_seeding = true;
- }
- else
- {
- // Calculate new reconstruction points using the centroids
- // Find new construction values from average
- t_color[0][R] = 0;
- t_color[0][G] = 0;
- t_color[0][B] = 0;
- t_color[1][R] = 0;
- t_color[1][G] = 0;
- t_color[1][B] = 0;
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (int x = 0; x < BLOCKWIDTH; ++x)
- {
- // use dummy value for q-parameter
- t_color[block_mask[x][y]][R] += original_colors[x][y][R];
- t_color[block_mask[x][y]][G] += original_colors[x][y][G];
- t_color[block_mask[x][y]][B] += original_colors[x][y][B];
- }
- }
- current_colors[0][R] = t_color[0][R] / n;
- current_colors[1][R] = t_color[1][R] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- current_colors[0][G] = t_color[0][G] / n;
- current_colors[1][G] = t_color[1][G] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- current_colors[0][B] = t_color[0][B] / n;
- current_colors[1][B] = t_color[1][B] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- }
- }
- }
- for(x=0;x<2;x++)
- {
- double qq, rr, ss;
- qq = best_colors[x][0];
- rr = best_colors[x][1];
- ss = best_colors[x][2];
- current_colors[x][0] = CLAMP(0, (1.0/sqrt(1.0*3))*qq + (1.0/sqrt(1.0*2))*rr + (1.0/sqrt(1.0*6))*ss, 255);
- current_colors[x][1] = CLAMP(0, (1.0/sqrt(1.0*3))*qq - (1.0/sqrt(1.0*2))*rr + (1.0/sqrt(1.0*6))*ss, 255);
- current_colors[x][2] = CLAMP(0, (1.0/sqrt(1.0*3))*qq + (0.0 )*rr - (2.0/sqrt(1.0*6))*ss, 255);
- }
- for(x=0;x<2;x++)
- for(y=0;y<3;y++)
- LBG_colors[x][y] = JAS_ROUND(current_colors[x][y]);
- }
- // Calculation of the two block colors using the LBG-algorithm
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void computeColorLBG(uint8 *img,int width,int startx,int starty, uint8 (LBG_colors)[2][3])
- {
- uint8 block_mask[4][4];
- // reset rand so that we get predictable output per block
- srand(10000);
- //LBG-algorithm
- double D = 0, oldD, bestD = MAXIMUM_ERROR, eps = 0.0000000001;
- double error_a, error_b;
- int number_of_iterations = 10;
- double t_color[2][3];
- double original_colors[4][4][3];
- double current_colors[2][3];
- double best_colors[2][3];
- double max_v[3];
- double min_v[3];
- int x,y,i;
- double red, green, blue;
- bool continue_seeding;
- int maximum_number_of_seedings = 10;
- int seeding;
- bool continue_iterate;
- max_v[R] = -512.0; max_v[G] = -512.0; max_v[B] = -512.0;
- min_v[R] = 512.0; min_v[G] = 512.0; min_v[B] = 512.0;
- // resolve trainingdata
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (x = 0; x < BLOCKWIDTH; ++x)
- {
- red = img[3*((starty+y)*width+startx+x)+R];
- green = img[3*((starty+y)*width+startx+x)+G];
- blue = img[3*((starty+y)*width+startx+x)+B];
- original_colors[x][y][R] = red;
- original_colors[x][y][G] = green;
- original_colors[x][y][B] = blue;
- // find max
- if (original_colors[x][y][R] > max_v[R]) max_v[R] = original_colors[x][y][R];
- if (original_colors[x][y][G] > max_v[G]) max_v[G] = original_colors[x][y][G];
- if (original_colors[x][y][B] > max_v[B]) max_v[B] = original_colors[x][y][B];
- // find min
- if (original_colors[x][y][R] < min_v[R]) min_v[R] = original_colors[x][y][R];
- if (original_colors[x][y][G] < min_v[G]) min_v[G] = original_colors[x][y][G];
- if (original_colors[x][y][B] < min_v[B]) min_v[B] = original_colors[x][y][B];
- }
- }
- D = 512*512*3*16.0;
- bestD = 512*512*3*16.0;
- continue_seeding = true;
- // loop seeds
- for (seeding = 0; (seeding < maximum_number_of_seedings) && continue_seeding; seeding++)
- {
- // hopefully we will not need more seedings:
- continue_seeding = false;
- // calculate seeds
- for (uint8 s = 0; s < 2; ++s)
- {
- for (uint8 c = 0; c < 3; ++c)
- {
- current_colors[s][c] = double((double(rand())/RAND_MAX)*(max_v[c]-min_v[c])) + min_v[c];
- }
- }
-
- // divide into two quantization sets and calculate distortion
- continue_iterate = true;
- for(i = 0; (i < number_of_iterations) && continue_iterate; i++)
- {
- oldD = D;
- D = 0;
- int n = 0;
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (int x = 0; x < BLOCKWIDTH; ++x)
- {
- error_a = SQUARE(original_colors[x][y][R] - JAS_ROUND(current_colors[0][R])) +
- SQUARE(original_colors[x][y][G] - JAS_ROUND(current_colors[0][G])) +
- SQUARE(original_colors[x][y][B] - JAS_ROUND(current_colors[0][B]));
- error_b = SQUARE(original_colors[x][y][R] - JAS_ROUND(current_colors[1][R])) +
- SQUARE(original_colors[x][y][G] - JAS_ROUND(current_colors[1][G])) +
- SQUARE(original_colors[x][y][B] - JAS_ROUND(current_colors[1][B]));
- if (error_a < error_b)
- {
- block_mask[x][y] = 0;
- D += error_a;
- ++n;
- }
- else
- {
- block_mask[x][y] = 1;
- D += error_b;
- }
- }
- }
- // compare with old distortion
- if (D == 0)
- {
- // Perfect score -- we dont need to go further iterations.
- continue_iterate = false;
- continue_seeding = false;
- }
- if (D == oldD)
- {
- // Same score as last round -- no need to go for further iterations.
- continue_iterate = false;
- continue_seeding = true;
- }
- if (D < bestD)
- {
- bestD = D;
- for(uint8 s = 0; s < 2; ++s)
- {
- for(uint8 c = 0; c < 3; ++c)
- {
- best_colors[s][c] = current_colors[s][c];
- }
- }
- }
- if (n == 0 || n == BLOCKWIDTH*BLOCKHEIGHT)
- {
- // All colors end up in the same voroni region. We need to reseed.
- continue_iterate = false;
- continue_seeding = true;
- }
- else
- {
- // Calculate new reconstruction points using the centroids
- // Find new construction values from average
- t_color[0][R] = 0;
- t_color[0][G] = 0;
- t_color[0][B] = 0;
- t_color[1][R] = 0;
- t_color[1][G] = 0;
- t_color[1][B] = 0;
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (int x = 0; x < BLOCKWIDTH; ++x)
- {
- // use dummy value for q-parameter
- t_color[block_mask[x][y]][R] += original_colors[x][y][R];
- t_color[block_mask[x][y]][G] += original_colors[x][y][G];
- t_color[block_mask[x][y]][B] += original_colors[x][y][B];
- }
- }
- current_colors[0][R] = t_color[0][R] / n;
- current_colors[1][R] = t_color[1][R] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- current_colors[0][G] = t_color[0][G] / n;
- current_colors[1][G] = t_color[1][G] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- current_colors[0][B] = t_color[0][B] / n;
- current_colors[1][B] = t_color[1][B] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- }
- }
- }
- // Set the best colors as the final block colors
- for(int s = 0; s < 2; ++s)
- {
- for(uint8 c = 0; c < 3; ++c)
- {
- current_colors[s][c] = best_colors[s][c];
- }
- }
- for(x=0;x<2;x++)
- for(y=0;y<3;y++)
- LBG_colors[x][y] = JAS_ROUND(current_colors[x][y]);
- }
- // Calculation of the two block colors using the LBG-algorithm
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void computeColorLBGfast(uint8 *img,int width,int startx,int starty, uint8 (LBG_colors)[2][3])
- {
- uint8 block_mask[4][4];
- // reset rand so that we get predictable output per block
- srand(10000);
- //LBG-algorithm
- double D = 0, oldD, bestD = MAXIMUM_ERROR, eps = 0.0000000001;
- double error_a, error_b;
- int number_of_iterations = 10;
- double t_color[2][3];
- uint8 original_colors[4][4][3];
- double current_colors[2][3];
- double best_colors[2][3];
- double max_v[3];
- double min_v[3];
- int x,y,i;
- bool continue_seeding;
- int maximum_number_of_seedings = 10;
- int seeding;
- bool continue_iterate;
- max_v[R] = -512.0; max_v[G] = -512.0; max_v[B] = -512.0;
- min_v[R] = 512.0; min_v[G] = 512.0; min_v[B] = 512.0;
- // resolve trainingdata
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (x = 0; x < BLOCKWIDTH; ++x)
- {
- original_colors[x][y][R] = img[3*((starty+y)*width+startx+x)+R];
- original_colors[x][y][G] = img[3*((starty+y)*width+startx+x)+G];
- original_colors[x][y][B] = img[3*((starty+y)*width+startx+x)+B];
-
- // find max
- if (original_colors[x][y][R] > max_v[R]) max_v[R] = original_colors[x][y][R];
- if (original_colors[x][y][G] > max_v[G]) max_v[G] = original_colors[x][y][G];
- if (original_colors[x][y][B] > max_v[B]) max_v[B] = original_colors[x][y][B];
- // find min
- if (original_colors[x][y][R] < min_v[R]) min_v[R] = original_colors[x][y][R];
- if (original_colors[x][y][G] < min_v[G]) min_v[G] = original_colors[x][y][G];
- if (original_colors[x][y][B] < min_v[B]) min_v[B] = original_colors[x][y][B];
- }
- }
- D = 512*512*3*16.0;
- bestD = 512*512*3*16.0;
- continue_seeding = true;
- // loop seeds
- for (seeding = 0; (seeding < maximum_number_of_seedings) && continue_seeding; seeding++)
- {
- // hopefully we will not need more seedings:
- continue_seeding = false;
- // calculate seeds
- for (uint8 s = 0; s < 2; ++s)
- {
- for (uint8 c = 0; c < 3; ++c)
- {
- current_colors[s][c] = double((double(rand())/RAND_MAX)*(max_v[c]-min_v[c])) + min_v[c];
- }
- }
-
- // divide into two quantization sets and calculate distortion
- continue_iterate = true;
- for(i = 0; (i < number_of_iterations) && continue_iterate; i++)
- {
- oldD = D;
- D = 0;
- int n = 0;
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (int x = 0; x < BLOCKWIDTH; ++x)
- {
- error_a = SQUARE(original_colors[x][y][R] - JAS_ROUND(current_colors[0][R])) +
- SQUARE(original_colors[x][y][G] - JAS_ROUND(current_colors[0][G])) +
- SQUARE(original_colors[x][y][B] - JAS_ROUND(current_colors[0][B]));
- error_b = SQUARE(original_colors[x][y][R] - JAS_ROUND(current_colors[1][R])) +
- SQUARE(original_colors[x][y][G] - JAS_ROUND(current_colors[1][G])) +
- SQUARE(original_colors[x][y][B] - JAS_ROUND(current_colors[1][B]));
- if (error_a < error_b)
- {
- block_mask[x][y] = 0;
- D += error_a;
- ++n;
- }
- else
- {
- block_mask[x][y] = 1;
- D += error_b;
- }
- }
- }
- // compare with old distortion
- if (D == 0)
- {
- // Perfect score -- we dont need to go further iterations.
- continue_iterate = false;
- continue_seeding = false;
- }
- if (D == oldD)
- {
- // Same score as last round -- no need to go for further iterations.
- continue_iterate = false;
- continue_seeding = false;
- }
- if (D < bestD)
- {
- bestD = D;
- for(uint8 s = 0; s < 2; ++s)
- {
- for(uint8 c = 0; c < 3; ++c)
- {
- best_colors[s][c] = current_colors[s][c];
- }
- }
- }
- if (n == 0 || n == BLOCKWIDTH*BLOCKHEIGHT)
- {
- // All colors end up in the same voroni region. We need to reseed.
- continue_iterate = false;
- continue_seeding = true;
- }
- else
- {
- // Calculate new reconstruction points using the centroids
- // Find new construction values from average
- t_color[0][R] = 0;
- t_color[0][G] = 0;
- t_color[0][B] = 0;
- t_color[1][R] = 0;
- t_color[1][G] = 0;
- t_color[1][B] = 0;
- for (y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (int x = 0; x < BLOCKWIDTH; ++x)
- {
- // use dummy value for q-parameter
- t_color[block_mask[x][y]][R] += original_colors[x][y][R];
- t_color[block_mask[x][y]][G] += original_colors[x][y][G];
- t_color[block_mask[x][y]][B] += original_colors[x][y][B];
- }
- }
- current_colors[0][R] = t_color[0][R] / n;
- current_colors[1][R] = t_color[1][R] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- current_colors[0][G] = t_color[0][G] / n;
- current_colors[1][G] = t_color[1][G] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- current_colors[0][B] = t_color[0][B] / n;
- current_colors[1][B] = t_color[1][B] / (BLOCKWIDTH*BLOCKHEIGHT - n);
- }
- }
- }
- // Set the best colors as the final block colors
- for(int s = 0; s < 2; ++s)
- {
- for(uint8 c = 0; c < 3; ++c)
- {
- current_colors[s][c] = best_colors[s][c];
- }
- }
- for(x=0;x<2;x++)
- for(y=0;y<3;y++)
- LBG_colors[x][y] = JAS_ROUND(current_colors[x][y]);
- }
- // Each color component is compressed to fit in its specified number of bits
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressColor(int R_B, int G_B, int B_B, uint8 (current_color)[2][3], uint8 (quantized_color)[2][3])
- {
- //
- // The color is calculated as:
- //
- // c = (c + (2^(8-b))/2) / (255 / (2^b - 1)) where b is the number of bits
- // to code color c with
- // For instance, if b = 3:
- //
- // c = (c + 16) / (255 / 7) = 7 * (c + 16) / 255
- //
- quantized_color[0][R] = CLAMP(0,(BINPOW(R_B)-1) * (current_color[0][R] + BINPOW(8-R_B-1)) / 255,255);
- quantized_color[0][G] = CLAMP(0,(BINPOW(G_B)-1) * (current_color[0][G] + BINPOW(8-G_B-1)) / 255,255);
- quantized_color[0][B] = CLAMP(0,(BINPOW(B_B)-1) * (current_color[0][B] + BINPOW(8-B_B-1)) / 255,255);
- quantized_color[1][R] = CLAMP(0,(BINPOW(R_B)-1) * (current_color[1][R] + BINPOW(8-R_B-1)) / 255,255);
- quantized_color[1][G] = CLAMP(0,(BINPOW(G_B)-1) * (current_color[1][G] + BINPOW(8-G_B-1)) / 255,255);
- quantized_color[1][B] = CLAMP(0,(BINPOW(B_B)-1) * (current_color[1][B] + BINPOW(8-B_B-1)) / 255,255);
- }
- // Swapping two RGB-colors
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void swapColors(uint8 (colors)[2][3])
- {
- uint8 temp = colors[0][R];
- colors[0][R] = colors[1][R];
- colors[1][R] = temp;
- temp = colors[0][G];
- colors[0][G] = colors[1][G];
- colors[1][G] = temp;
- temp = colors[0][B];
- colors[0][B] = colors[1][B];
- colors[1][B] = temp;
- }
- // Calculate the paint colors from the block colors
- // using a distance d and one of the H- or T-patterns.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- // Calculate the error for the block at position (startx,starty)
- // The parameters needed for reconstruction are calculated as well
- //
- // Please note that the function can change the order between the two colors in colorsRGB444
- //
- // In the 59T bit mode, we only have pattern T.
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateError59Tperceptual1000(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
- {
- unsigned int block_error = 0,
- best_block_error = MAXERR1000,
- pixel_error,
- best_pixel_error;
- int diff[3];
- uint8 best_sw;
- unsigned int pixel_colors;
- uint8 colors[2][3];
- uint8 possible_colors[4][3];
- // First use the colors as they are, then swap them
- for (uint8 sw = 0; sw <2; ++sw)
- {
- if (sw == 1)
- {
- swapColors(colorsRGB444);
- }
- decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_59T); ++d)
- {
- calculatePaintColors59T(d,PATTERN_T, colors, possible_colors);
-
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- best_pixel_error = MAXERR1000;
- pixel_colors <<=2; // Make room for next value
- // Loop possible block colors
- for (uint8 c = 0; c < 4; ++c)
- {
-
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
- diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) +
- PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]) +
- PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*SQUARE(diff[B]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
- pixel_colors |= c;
- }
- }
- block_error += best_pixel_error;
- }
- }
- if (block_error < best_block_error)
- {
- best_block_error = block_error;
- distance = d;
- pixel_indices = pixel_colors;
- best_sw = sw;
- }
- }
-
- if (sw == 1 && best_sw == 0)
- {
- swapColors(colorsRGB444);
- }
- decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
- }
- return best_block_error;
- }
- // Calculate the error for the block at position (startx,starty)
- // The parameters needed for reconstruction is calculated as well
- //
- // Please note that the function can change the order between the two colors in colorsRGB444
- //
- // In the 59T bit mode, we only have pattern T.
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calculateError59T(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
- {
- double block_error = 0,
- best_block_error = MAXIMUM_ERROR,
- pixel_error,
- best_pixel_error;
- int diff[3];
- uint8 best_sw;
- unsigned int pixel_colors;
- uint8 colors[2][3];
- uint8 possible_colors[4][3];
- // First use the colors as they are, then swap them
- for (uint8 sw = 0; sw <2; ++sw)
- {
- if (sw == 1)
- {
- swapColors(colorsRGB444);
- }
- decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_59T); ++d)
- {
- calculatePaintColors59T(d,PATTERN_T, colors, possible_colors);
-
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- best_pixel_error = MAXIMUM_ERROR;
- pixel_colors <<=2; // Make room for next value
- // Loop possible block colors
- for (uint8 c = 0; c < 4; ++c)
- {
-
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
- diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
- pixel_error = weight[R]*SQUARE(diff[R]) +
- weight[G]*SQUARE(diff[G]) +
- weight[B]*SQUARE(diff[B]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
- pixel_colors |= c;
- }
- }
- block_error += best_pixel_error;
- }
- }
- if (block_error < best_block_error)
- {
- best_block_error = block_error;
- distance = d;
- pixel_indices = pixel_colors;
- best_sw = sw;
- }
- }
-
- if (sw == 1 && best_sw == 0)
- {
- swapColors(colorsRGB444);
- }
- decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
- }
- return best_block_error;
- }
- // Calculate the error for the block at position (startx,starty)
- // The parameters needed for reconstruction is calculated as well
- //
- // In the 59T bit mode, we only have pattern T.
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateError59TnoSwapPerceptual1000(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
- {
- unsigned int block_error = 0,
- best_block_error = MAXERR1000,
- pixel_error,
- best_pixel_error;
- int diff[3];
- unsigned int pixel_colors;
- uint8 colors[2][3];
- uint8 possible_colors[4][3];
- int thebestintheworld;
- // First use the colors as they are, then swap them
- decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_59T); ++d)
- {
- calculatePaintColors59T(d,PATTERN_T, colors, possible_colors);
-
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- best_pixel_error = MAXERR1000;
- pixel_colors <<=2; // Make room for next value
- // Loop possible block colors
- for (uint8 c = 0; c < 4; ++c)
- {
-
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
- diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) +
- PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]) +
- PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*SQUARE(diff[B]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
- pixel_colors |= c;
- thebestintheworld = c;
- }
- }
- block_error += best_pixel_error;
- }
- }
- if (block_error < best_block_error)
- {
- best_block_error = block_error;
- distance = d;
- pixel_indices = pixel_colors;
- }
- }
-
- decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
- return best_block_error;
- }
- // Calculate the error for the block at position (startx,starty)
- // The parameters needed for reconstruction is calculated as well
- //
- // In the 59T bit mode, we only have pattern T.
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calculateError59TnoSwap(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
- {
- double block_error = 0,
- best_block_error = MAXIMUM_ERROR,
- pixel_error,
- best_pixel_error;
- int diff[3];
- unsigned int pixel_colors;
- uint8 colors[2][3];
- uint8 possible_colors[4][3];
- int thebestintheworld;
- // First use the colors as they are, then swap them
- decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_59T); ++d)
- {
- calculatePaintColors59T(d,PATTERN_T, colors, possible_colors);
-
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- best_pixel_error = MAXIMUM_ERROR;
- pixel_colors <<=2; // Make room for next value
- // Loop possible block colors
- for (uint8 c = 0; c < 4; ++c)
- {
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
- diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
- pixel_error = weight[R]*SQUARE(diff[R]) +
- weight[G]*SQUARE(diff[G]) +
- weight[B]*SQUARE(diff[B]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
- pixel_colors |= c;
- thebestintheworld = c;
- }
- }
- block_error += best_pixel_error;
- }
- }
- if (block_error < best_block_error)
- {
- best_block_error = block_error;
- distance = d;
- pixel_indices = pixel_colors;
- }
- }
-
- decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
- return best_block_error;
- }
- // Put the compress params into the compression block
- //
- //
- //|63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
- //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void packBlock59T(uint8 (colors)[2][3], uint8 d, unsigned int pixel_indices, unsigned int &compressed1, unsigned int &compressed2)
- {
-
- compressed1 = 0;
- PUTBITSHIGH( compressed1, colors[0][R], 4, 58);
- PUTBITSHIGH( compressed1, colors[0][G], 4, 54);
- PUTBITSHIGH( compressed1, colors[0][B], 4, 50);
- PUTBITSHIGH( compressed1, colors[1][R], 4, 46);
- PUTBITSHIGH( compressed1, colors[1][G], 4, 42);
- PUTBITSHIGH( compressed1, colors[1][B], 4, 38);
- PUTBITSHIGH( compressed1, d, TABLE_BITS_59T, 34);
- pixel_indices=indexConversion(pixel_indices);
- compressed2 = 0;
- PUTBITS( compressed2, pixel_indices, 32, 31);
- }
- // Copy colors from source to dest
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void copyColors(uint8 (source)[2][3], uint8 (dest)[2][3])
- {
- int x,y;
- for (x=0; x<2; x++)
- for (y=0; y<3; y++)
- dest[x][y] = source[x][y];
- }
- // The below code should compress the block to 59 bits.
- //
- //|63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
- //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockTHUMB59TFastestOnlyColorPerceptual1000(uint8 *img,int width,int height,int startx,int starty, int (best_colorsRGB444_packed)[2])
- {
- unsigned int best_error = MAXERR1000;
- unsigned int best_pixel_indices;
- uint8 best_distance;
- unsigned int error_no_i;
- uint8 colorsRGB444_no_i[2][3];
- unsigned int pixel_indices_no_i;
- uint8 distance_no_i;
- uint8 colors[2][3];
- // Calculate average color using the LBG-algorithm
- computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
- compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
- // Determine the parameters for the lowest error
- error_no_i = calculateError59Tperceptual1000(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
- best_error = error_no_i;
- best_distance = distance_no_i;
- best_pixel_indices = pixel_indices_no_i;
- best_colorsRGB444_packed[0] = (colorsRGB444_no_i[0][0] << 8) + (colorsRGB444_no_i[0][1] << 4) + (colorsRGB444_no_i[0][2] << 0);
- best_colorsRGB444_packed[1] = (colorsRGB444_no_i[1][0] << 8) + (colorsRGB444_no_i[1][1] << 4) + (colorsRGB444_no_i[1][2] << 0);
- return best_error;
- }
- // The below code should compress the block to 59 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- //
- //|63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
- //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double compressBlockTHUMB59TFastestOnlyColor(uint8 *img,int width,int height,int startx,int starty, int (best_colorsRGB444_packed)[2])
- {
- double best_error = MAXIMUM_ERROR;
- unsigned int best_pixel_indices;
- uint8 best_distance;
- double error_no_i;
- uint8 colorsRGB444_no_i[2][3];
- unsigned int pixel_indices_no_i;
- uint8 distance_no_i;
- uint8 colors[2][3];
- // Calculate average color using the LBG-algorithm
- computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
- compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
- // Determine the parameters for the lowest error
- error_no_i = calculateError59T(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
- best_error = error_no_i;
- best_distance = distance_no_i;
- best_pixel_indices = pixel_indices_no_i;
- best_colorsRGB444_packed[0] = (colorsRGB444_no_i[0][0] << 8) + (colorsRGB444_no_i[0][1] << 4) + (colorsRGB444_no_i[0][2] << 0);
- best_colorsRGB444_packed[1] = (colorsRGB444_no_i[1][0] << 8) + (colorsRGB444_no_i[1][1] << 4) + (colorsRGB444_no_i[1][2] << 0);
- return best_error;
- }
- // The below code should compress the block to 59 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- //
- //|63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
- //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double compressBlockTHUMB59TFastestPerceptual1000(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- double best_error = MAXIMUM_ERROR;
- uint8 best_colorsRGB444[2][3];
- unsigned int best_pixel_indices;
- uint8 best_distance;
- double error_no_i;
- uint8 colorsRGB444_no_i[2][3];
- unsigned int pixel_indices_no_i;
- uint8 distance_no_i;
- uint8 colors[2][3];
- // Calculate average color using the LBG-algorithm
- computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
- compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
- // Determine the parameters for the lowest error
- error_no_i = calculateError59Tperceptual1000(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
- best_error = error_no_i;
- best_distance = distance_no_i;
- best_pixel_indices = pixel_indices_no_i;
- copyColors(colorsRGB444_no_i, best_colorsRGB444);
- // Put the compress params into the compression block
- packBlock59T(best_colorsRGB444, best_distance, best_pixel_indices, compressed1, compressed2);
- return best_error;
- }
- // The below code should compress the block to 59 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- //
- //|63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
- //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double compressBlockTHUMB59TFastest(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- double best_error = MAXIMUM_ERROR;
- uint8 best_colorsRGB444[2][3];
- unsigned int best_pixel_indices;
- uint8 best_distance;
- double error_no_i;
- uint8 colorsRGB444_no_i[2][3];
- unsigned int pixel_indices_no_i;
- uint8 distance_no_i;
- uint8 colors[2][3];
- // Calculate average color using the LBG-algorithm
- computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
- compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
- // Determine the parameters for the lowest error
- error_no_i = calculateError59T(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
- best_error = error_no_i;
- best_distance = distance_no_i;
- best_pixel_indices = pixel_indices_no_i;
- copyColors(colorsRGB444_no_i, best_colorsRGB444);
- // Put the compress params into the compression block
- packBlock59T(best_colorsRGB444, best_distance, best_pixel_indices, compressed1, compressed2);
- return best_error;
- }
- // The below code should compress the block to 59 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- //
- //|63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
- //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double compressBlockTHUMB59TFast(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- double best_error = MAXIMUM_ERROR;
- uint8 best_colorsRGB444[2][3];
- unsigned int best_pixel_indices;
- uint8 best_distance;
- double error_no_i;
- uint8 colorsRGB444_no_i[2][3];
- unsigned int pixel_indices_no_i;
- uint8 distance_no_i;
- double error_half_i;
- uint8 colorsRGB444_half_i[2][3];
- unsigned int pixel_indices_half_i;
- uint8 distance_half_i;
-
- double error;
- uint8 colorsRGB444[2][3];
- unsigned int pixel_indices;
- uint8 distance;
- uint8 colors[2][3];
- // Calculate average color using the LBG-algorithm
- computeColorLBGNotIntensityFast(img,width,startx,starty, colors);
- compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
- // Determine the parameters for the lowest error
- error_no_i = calculateError59T(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
- // Calculate average color using the LBG-algorithm
- computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
- compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_half_i);
- // Determine the parameters for the lowest error
- error_half_i = calculateError59T(img, width, startx, starty, colorsRGB444_half_i, distance_half_i, pixel_indices_half_i);
- // Calculate average color using the LBG-algorithm
- computeColorLBGfast(img,width,startx,starty, colors);
- compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444);
- // Determine the parameters for the lowest error
- error = calculateError59T(img, width, startx, starty, colorsRGB444, distance, pixel_indices);
-
- best_error = error_no_i;
- best_distance = distance_no_i;
- best_pixel_indices = pixel_indices_no_i;
- copyColors(colorsRGB444_no_i, best_colorsRGB444);
- if(error_half_i < best_error)
- {
- best_error = error_half_i;
- best_distance = distance_half_i;
- best_pixel_indices = pixel_indices_half_i;
- copyColors (colorsRGB444_half_i, best_colorsRGB444);
- }
- if(error < best_error)
- {
- best_error = error;
- best_distance = distance;
- best_pixel_indices = pixel_indices;
- copyColors (colorsRGB444, best_colorsRGB444);
- }
- // Put the compress params into the compression block
- packBlock59T(best_colorsRGB444, best_distance, best_pixel_indices, compressed1, compressed2);
- return best_error;
- }
- // Calculate the error for the block at position (startx,starty)
- // The parameters needed for reconstruction is calculated as well
- //
- // In the 58H bit mode, we only have pattern H.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateErrorAndCompress58Hperceptual1000(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
- {
- unsigned int block_error = 0,
- best_block_error = MAXERR1000,
- pixel_error,
- best_pixel_error;
- int diff[3];
- unsigned int pixel_colors;
- uint8 possible_colors[4][3];
- uint8 colors[2][3];
- decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
- {
- calculatePaintColors58H(d, PATTERN_H, colors, possible_colors);
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- best_pixel_error = MAXERR1000;
- pixel_colors <<=2; // Make room for next value
- // Loop possible block colors
- for (uint8 c = 0; c < 4; ++c)
- {
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
- diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) +
- PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]) +
- PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*SQUARE(diff[B]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
- pixel_colors |= c;
- }
- }
- block_error += best_pixel_error;
- }
- }
-
- if (block_error < best_block_error)
- {
- best_block_error = block_error;
- distance = d;
- pixel_indices = pixel_colors;
- }
- }
- return best_block_error;
- }
- // The H-mode but with punchthrough alpha
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calculateErrorAndCompress58HAlpha(uint8* srcimg, uint8* alphaimg,int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
- {
- double block_error = 0,
- best_block_error = MAXIMUM_ERROR,
- pixel_error,
- best_pixel_error;
- int diff[3];
- unsigned int pixel_colors;
- uint8 possible_colors[4][3];
- uint8 colors[2][3];
- int alphaindex;
- int colorsRGB444_packed[2];
- colorsRGB444_packed[0] = (colorsRGB444[0][R] << 8) + (colorsRGB444[0][G] << 4) + colorsRGB444[0][B];
- colorsRGB444_packed[1] = (colorsRGB444[1][R] << 8) + (colorsRGB444[1][G] << 4) + colorsRGB444[1][B];
-
- decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
-
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
- {
- alphaindex=2;
- if( (colorsRGB444_packed[0] >= colorsRGB444_packed[1]) ^ ((d & 1)==1) )
- {
- //we're going to have to swap the colors to be able to choose this distance.. that means
- //that the indices will be swapped as well, so C1 will be the one with alpha instead of C3..
- alphaindex=0;
- }
- calculatePaintColors58H(d, PATTERN_H, colors, possible_colors);
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- int alpha=0;
- if(alphaimg[((starty+y)*width+startx+x)]>0)
- alpha=1;
- if(alphaimg[((starty+y)*width+startx+x)]>0&&alphaimg[((starty+y)*width+startx+x)]<255)
- printf("INVALID ALPHA DATA!!\n");
- best_pixel_error = MAXIMUM_ERROR;
- pixel_colors <<=2; // Make room for next value
- // Loop possible block colors
- for (uint8 c = 0; c < 4; ++c)
- {
- if(c==alphaindex&&alpha)
- {
- pixel_error=0;
- }
- else if(c==alphaindex||alpha)
- {
- pixel_error=MAXIMUM_ERROR;
- }
- else
- {
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
- diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
- pixel_error = weight[R]*SQUARE(diff[R]) +
- weight[G]*SQUARE(diff[G]) +
- weight[B]*SQUARE(diff[B]);
- }
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
- pixel_colors |= c;
- }
- }
- block_error += best_pixel_error;
- }
- }
- if (block_error < best_block_error)
- {
- best_block_error = block_error;
- distance = d;
- pixel_indices = pixel_colors;
- }
- }
- return best_block_error;
- }
- // Calculate the error for the block at position (startx,starty)
- // The parameters needed for reconstruction is calculated as well
- //
- // In the 58H bit mode, we only have pattern H.
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calculateErrorAndCompress58H(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
- {
- double block_error = 0,
- best_block_error = MAXIMUM_ERROR,
- pixel_error,
- best_pixel_error;
- int diff[3];
- unsigned int pixel_colors;
- uint8 possible_colors[4][3];
- uint8 colors[2][3];
-
- decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
- {
- calculatePaintColors58H(d, PATTERN_H, colors, possible_colors);
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- best_pixel_error = MAXIMUM_ERROR;
- pixel_colors <<=2; // Make room for next value
- // Loop possible block colors
- for (uint8 c = 0; c < 4; ++c)
- {
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
- diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
- pixel_error = weight[R]*SQUARE(diff[R]) +
- weight[G]*SQUARE(diff[G]) +
- weight[B]*SQUARE(diff[B]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
- pixel_colors |= c;
- }
- }
- block_error += best_pixel_error;
- }
- }
-
- if (block_error < best_block_error)
- {
- best_block_error = block_error;
- distance = d;
- pixel_indices = pixel_colors;
- }
- }
-
- return best_block_error;
- }
- // Makes sure that col0 < col1;
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void sortColorsRGB444(uint8 (colorsRGB444)[2][3])
- {
- unsigned int col0, col1, tcol;
- // sort colors
- col0 = 16*16*colorsRGB444[0][R] + 16*colorsRGB444[0][G] + colorsRGB444[0][B];
- col1 = 16*16*colorsRGB444[1][R] + 16*colorsRGB444[1][G] + colorsRGB444[1][B];
- // After this, col0 should be smaller than col1 (col0 < col1)
- if( col0 > col1)
- {
- tcol = col0;
- col0 = col1;
- col1 = tcol;
- }
- else
- {
- if(col0 == col1)
- {
- // Both colors are the same. That is useless. If they are both black,
- // col1 can just as well be (0,0,1). Else, col0 can be col1 - 1.
- if(col0 == 0)
- col1 = col0+1;
- else
- col0 = col1-1;
- }
- }
-
- colorsRGB444[0][R] = GETBITS(col0, 4, 11);
- colorsRGB444[0][G] = GETBITS(col0, 4, 7);
- colorsRGB444[0][B] = GETBITS(col0, 4, 3);
- colorsRGB444[1][R] = GETBITS(col1, 4, 11);
- colorsRGB444[1][G] = GETBITS(col1, 4, 7);
- colorsRGB444[1][B] = GETBITS(col1, 4, 3);
- }
- // The below code should compress the block to 58 bits.
- // The bit layout is thought to be:
- //
- //|63 62 61 60 59 58|57 56 55 54|53 52 51 50|49 48 47 46|45 44 43 42|41 40 39 38|37 36 35 34|33 32|
- //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
- // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
- // Else, it is assumed to be 1.
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockTHUMB58HFastestPerceptual1000(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int best_error = MAXERR1000;
- uint8 best_colorsRGB444[2][3];
- unsigned int best_pixel_indices;
- uint8 best_distance;
- unsigned int error_no_i;
- uint8 colorsRGB444_no_i[2][3];
- unsigned int pixel_indices_no_i;
- uint8 distance_no_i;
- uint8 colors[2][3];
-
- // Calculate average color using the LBG-algorithm but discarding the intensity in the error function
- computeColorLBGHalfIntensityFast(img, width, startx, starty, colors);
- compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444_no_i);
- sortColorsRGB444(colorsRGB444_no_i);
- error_no_i = calculateErrorAndCompress58Hperceptual1000(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
- best_error = error_no_i;
- best_distance = distance_no_i;
- best_pixel_indices = pixel_indices_no_i;
- copyColors(colorsRGB444_no_i, best_colorsRGB444);
- // | col0 >= col1 col0 < col1
- //------------------------------------------------------
- // (dist & 1) = 1 | no need to swap | need to swap
- // |-----------------+----------------
- // (dist & 1) = 0 | need to swap | no need to swap
- //
- // This can be done with an xor test.
- int best_colorsRGB444_packed[2];
- best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
- best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
- if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
- {
- swapColors(best_colorsRGB444);
- // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
- best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
- }
-
- // Put the compress params into the compression block
- compressed1 = 0;
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
- PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
- compressed2 = 0;
- best_pixel_indices=indexConversion(best_pixel_indices);
- PUTBITS( compressed2, best_pixel_indices, 32, 31);
- return best_error;
- }
- // The below code should compress the block to 58 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- // The bit layout is thought to be:
- //
- //|63 62 61 60 59 58|57 56 55 54|53 52 51 50|49 48 47 46|45 44 43 42|41 40 39 38|37 36 35 34|33 32|
- //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
- // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
- // Else, it is assumed to be 1.
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double compressBlockTHUMB58HFastest(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- double best_error = MAXIMUM_ERROR;
- uint8 best_colorsRGB444[2][3];
- unsigned int best_pixel_indices;
- uint8 best_distance;
- double error_no_i;
- uint8 colorsRGB444_no_i[2][3];
- unsigned int pixel_indices_no_i;
- uint8 distance_no_i;
- uint8 colors[2][3];
-
- // Calculate average color using the LBG-algorithm but discarding the intensity in the error function
- computeColorLBGHalfIntensityFast(img, width, startx, starty, colors);
- compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444_no_i);
- sortColorsRGB444(colorsRGB444_no_i);
- error_no_i = calculateErrorAndCompress58H(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
- best_error = error_no_i;
- best_distance = distance_no_i;
- best_pixel_indices = pixel_indices_no_i;
- copyColors(colorsRGB444_no_i, best_colorsRGB444);
- // | col0 >= col1 col0 < col1
- //------------------------------------------------------
- // (dist & 1) = 1 | no need to swap | need to swap
- // |-----------------+----------------
- // (dist & 1) = 0 | need to swap | no need to swap
- //
- // This can be done with an xor test.
- int best_colorsRGB444_packed[2];
- best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
- best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
- if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
- {
- swapColors(best_colorsRGB444);
- // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
- best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
- }
- // Put the compress params into the compression block
- compressed1 = 0;
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
- PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
- best_pixel_indices=indexConversion(best_pixel_indices);
- compressed2 = 0;
- PUTBITS( compressed2, best_pixel_indices, 32, 31);
- return best_error;
- }
- //same as above, but with 1-bit alpha
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double compressBlockTHUMB58HAlpha(uint8 *img, uint8* alphaimg, int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- double best_error = MAXIMUM_ERROR;
- uint8 best_colorsRGB444[2][3];
- unsigned int best_pixel_indices;
- uint8 best_distance;
- double error_no_i;
- uint8 colorsRGB444_no_i[2][3];
- unsigned int pixel_indices_no_i;
- uint8 distance_no_i;
- uint8 colors[2][3];
-
- // Calculate average color using the LBG-algorithm but discarding the intensity in the error function
- computeColorLBGHalfIntensityFast(img, width, startx, starty, colors);
- compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444_no_i);
- sortColorsRGB444(colorsRGB444_no_i);
- error_no_i = calculateErrorAndCompress58HAlpha(img, alphaimg,width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
- best_error = error_no_i;
- best_distance = distance_no_i;
- best_pixel_indices = pixel_indices_no_i;
- copyColors(colorsRGB444_no_i, best_colorsRGB444);
- // | col0 >= col1 col0 < col1
- //------------------------------------------------------
- // (dist & 1) = 1 | no need to swap | need to swap
- // |-----------------+----------------
- // (dist & 1) = 0 | need to swap | no need to swap
- //
- // This can be done with an xor test.
- int best_colorsRGB444_packed[2];
- best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
- best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
- if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
- {
- swapColors(best_colorsRGB444);
- // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
- best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
- }
- // Put the compress params into the compression block
- compressed1 = 0;
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
- PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
- best_pixel_indices=indexConversion(best_pixel_indices);
- compressed2 = 0;
- PUTBITS( compressed2, best_pixel_indices, 32, 31);
- return best_error;
- }
- // The below code should compress the block to 58 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- // The bit layout is thought to be:
- //
- //|63 62 61 60 59 58|57 56 55 54|53 52 51 50|49 48 47 46|45 44 43 42|41 40 39 38|37 36 35 34|33 32|
- //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
- // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
- // Else, it is assumed to be 1.
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double compressBlockTHUMB58HFast(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- double best_error = MAXIMUM_ERROR;
- uint8 best_colorsRGB444[2][3];
- unsigned int best_pixel_indices;
- uint8 best_distance;
- double error_no_i;
- uint8 colorsRGB444_no_i[2][3];
- unsigned int pixel_indices_no_i;
- uint8 distance_no_i;
- double error_half_i;
- uint8 colorsRGB444_half_i[2][3];
- unsigned int pixel_indices_half_i;
- uint8 distance_half_i;
- double error;
- uint8 colorsRGB444[2][3];
- unsigned int pixel_indices;
- uint8 distance;
- uint8 colors[2][3];
-
- // Calculate average color using the LBG-algorithm but discarding the intensity in the error function
- computeColorLBGNotIntensity(img, width, startx, starty, colors);
- compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444_no_i);
- sortColorsRGB444(colorsRGB444_no_i);
- error_no_i = calculateErrorAndCompress58H(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
- // Calculate average color using the LBG-algorithm but halfing the influence of the intensity in the error function
- computeColorLBGNotIntensity(img, width, startx, starty, colors);
- compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444_half_i);
- sortColorsRGB444(colorsRGB444_half_i);
- error_half_i = calculateErrorAndCompress58H(img, width, startx, starty, colorsRGB444_half_i, distance_half_i, pixel_indices_half_i);
- // Calculate average color using the LBG-algorithm
- computeColorLBG(img, width, startx, starty, colors);
- compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444);
- sortColorsRGB444(colorsRGB444);
- error = calculateErrorAndCompress58H(img, width, startx, starty, colorsRGB444, distance, pixel_indices);
- best_error = error_no_i;
- best_distance = distance_no_i;
- best_pixel_indices = pixel_indices_no_i;
- copyColors(colorsRGB444_no_i, best_colorsRGB444);
- if(error_half_i < best_error)
- {
- best_error = error_half_i;
- best_distance = distance_half_i;
- best_pixel_indices = pixel_indices_half_i;
- copyColors(colorsRGB444_half_i, best_colorsRGB444);
- }
- if(error < best_error)
- {
- best_error = error;
- best_distance = distance;
- best_pixel_indices = pixel_indices;
- copyColors(colorsRGB444, best_colorsRGB444);
- }
- // | col0 >= col1 col0 < col1
- //------------------------------------------------------
- // (dist & 1) = 1 | no need to swap | need to swap
- // |-----------------+----------------
- // (dist & 1) = 0 | need to swap | no need to swap
- //
- // This can be done with an xor test.
- int best_colorsRGB444_packed[2];
- best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
- best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
- if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
- {
- swapColors(best_colorsRGB444);
- // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
- best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
- }
- // Put the compress params into the compression block
- compressed1 = 0;
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
- PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
- best_pixel_indices=indexConversion(best_pixel_indices);
- compressed2 = 0;
- PUTBITS( compressed2, best_pixel_indices, 32, 31);
- return best_error;
- }
- // Compress block testing both individual and differential mode.
- // Perceptual error metric.
- // Combined quantization for colors.
- // Both flipped and unflipped tested.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockDiffFlipCombinedPerceptual(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int compressed1_norm, compressed2_norm;
- unsigned int compressed1_flip, compressed2_flip;
- uint8 avg_color_quant1[3], avg_color_quant2[3];
- float avg_color_float1[3],avg_color_float2[3];
- int enc_color1[3], enc_color2[3], diff[3];
- int min_error=255*255*8*3;
- unsigned int best_table_indices1=0, best_table_indices2=0;
- unsigned int best_table1=0, best_table2=0;
- int diffbit;
- int norm_err=0;
- int flip_err=0;
- // First try normal blocks 2x4:
- computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- float eps;
- uint8 dummy[3];
- quantize555ColorCombinedPerceptual(avg_color_float1, enc_color1, dummy);
- quantize555ColorCombinedPerceptual(avg_color_float2, enc_color2, dummy);
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
- {
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- norm_err = 0;
- // left part of block
- norm_err = tryalltables_3bittable2x4percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // right part of block
- norm_err += tryalltables_3bittable2x4percep(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- }
- else
- {
- diffbit = 0;
- // The difference is bigger than what fits in 555 plus delta-333, so we will have
- // to deal with 444 444.
- eps = (float) 0.0001;
- quantize444ColorCombinedPerceptual(avg_color_float1, enc_color1, dummy);
- quantize444ColorCombinedPerceptual(avg_color_float2, enc_color2, dummy);
- avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
- avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
- avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
- avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
- avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
- avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
- // Pack bits into the first word.
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- compressed1_norm = 0;
- PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_norm, enc_color2[2], 4, 43);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
-
- // left part of block
- norm_err = tryalltables_3bittable2x4percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // right part of block
- norm_err += tryalltables_3bittable2x4percep(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_norm, 0, 1, 32);
- compressed2_norm = 0;
- PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
- }
- // Now try flipped blocks 4x2:
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
- computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
- // First test if avg_color1 is similar enough to avg_color2 so that
- // we can use differential coding of colors.
- quantize555ColorCombinedPerceptual(avg_color_float1, enc_color1, dummy);
- quantize555ColorCombinedPerceptual(avg_color_float2, enc_color2, dummy);
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
- {
- diffbit = 1;
- // The difference to be coded:
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- // upper part of block
- flip_err = tryalltables_3bittable4x2percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // lower part of block
- flip_err += tryalltables_3bittable4x2percep(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- else
- {
- diffbit = 0;
- // The difference is bigger than what fits in 555 plus delta-333, so we will have
- // to deal with 444 444.
- eps = (float) 0.0001;
- quantize444ColorCombinedPerceptual(avg_color_float1, enc_color1, dummy);
- quantize444ColorCombinedPerceptual(avg_color_float2, enc_color2, dummy);
- avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
- avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
- avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
- avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
- avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
- avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- // Pack bits into the first word.
- compressed1_flip = 0;
- PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
- PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
- unsigned int best_pixel_indices1_MSB;
- unsigned int best_pixel_indices1_LSB;
- unsigned int best_pixel_indices2_MSB;
- unsigned int best_pixel_indices2_LSB;
- // upper part of block
- flip_err = tryalltables_3bittable4x2percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- // lower part of block
- flip_err += tryalltables_3bittable4x2percep(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
- PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
- PUTBITSHIGH( compressed1_flip, 1, 1, 32);
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
-
- compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- // Now lets see which is the best table to use. Only 8 tables are possible.
- if(norm_err <= flip_err)
- {
- compressed1 = compressed1_norm | 0;
- compressed2 = compressed2_norm;
- }
- else
- {
- compressed1 = compressed1_flip | 1;
- compressed2 = compressed2_flip;
- }
- }
- // Calculate the error of a block
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calcBlockErrorRGB(uint8 *img, uint8 *imgdec, int width, int height, int startx, int starty)
- {
- int xx,yy;
- double err;
- err = 0;
- for(xx = startx; xx< startx+4; xx++)
- {
- for(yy = starty; yy<starty+4; yy++)
- {
- err += SQUARE(1.0*RED(img,width,xx,yy) - 1.0*RED(imgdec, width, xx,yy));
- err += SQUARE(1.0*GREEN(img,width,xx,yy)- 1.0*GREEN(imgdec, width, xx,yy));
- err += SQUARE(1.0*BLUE(img,width,xx,yy) - 1.0*BLUE(imgdec, width, xx,yy));
- }
- }
- return err;
- }
- // Calculate the perceptually weighted error of a block
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calcBlockPerceptualErrorRGB(uint8 *img, uint8 *imgdec, int width, int height, int startx, int starty)
- {
- int xx,yy;
- double err;
- err = 0;
- for(xx = startx; xx< startx+4; xx++)
- {
- for(yy = starty; yy<starty+4; yy++)
- {
- err += PERCEPTUAL_WEIGHT_R_SQUARED*SQUARE(1.0*RED(img,width,xx,yy) - 1.0*RED(imgdec, width, xx,yy));
- err += PERCEPTUAL_WEIGHT_G_SQUARED*SQUARE(1.0*GREEN(img,width,xx,yy)- 1.0*GREEN(imgdec, width, xx,yy));
- err += PERCEPTUAL_WEIGHT_B_SQUARED*SQUARE(1.0*BLUE(img,width,xx,yy) - 1.0*BLUE(imgdec, width, xx,yy));
- }
- }
- return err;
- }
- // Compress an ETC1 block (or the individual and differential modes of an ETC2 block)
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double compressBlockDiffFlipFast(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int average_block1;
- unsigned int average_block2;
- double error_average;
- unsigned int combined_block1;
- unsigned int combined_block2;
- double error_combined;
- double best_error;
- // First quantize the average color to the nearest neighbor.
- compressBlockDiffFlipAverage(img, width, height, startx, starty, average_block1, average_block2);
- decompressBlockDiffFlip(average_block1, average_block2, imgdec, width, height, startx, starty);
- error_average = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- // Then quantize the average color taking into consideration that intensity can change
- compressBlockDiffFlipCombined(img, width, height, startx, starty, combined_block1, combined_block2);
- decompressBlockDiffFlip(combined_block1, combined_block2, imgdec, width, height, startx, starty);
- error_combined = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- if(error_combined < error_average)
- {
- compressed1 = combined_block1;
- compressed2 = combined_block2;
- best_error = error_combined;
- }
- else
- {
- compressed1 = average_block1;
- compressed2 = average_block2;
- best_error = error_average;
- }
- return best_error;
- }
- // Compress an ETC1 block (or the individual and differential modes of an ETC2 block)
- // Uses perceptual error metric.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockDiffFlipFastPerceptual(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int average_block1;
- unsigned int average_block2;
- double error_average;
- unsigned int combined_block1;
- unsigned int combined_block2;
- double error_combined;
- // First quantize the average color to the nearest neighbor.
- compressBlockDiffFlipAveragePerceptual(img, width, height, startx, starty, average_block1, average_block2);
- decompressBlockDiffFlip(average_block1, average_block2, imgdec, width, height, startx, starty);
- error_average = calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
- // Then quantize the average color taking into consideration that intensity can change
- compressBlockDiffFlipCombinedPerceptual(img, width, height, startx, starty, combined_block1, combined_block2);
- decompressBlockDiffFlip(combined_block1, combined_block2, imgdec, width, height, startx, starty);
- error_combined = calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
- if(error_combined < error_average)
- {
- compressed1 = combined_block1;
- compressed2 = combined_block2;
- }
- else
- {
- compressed1 = average_block1;
- compressed2 = average_block2;
- }
- }
- // Compresses the differential mode of an ETC2 block with punchthrough alpha
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int compressBlockDifferentialWithAlpha(bool isTransparent, uint8* img, uint8* alphaimg, uint8* imgdec, int width, int height, int startx, int starty, unsigned int &etc1_word1, unsigned int &etc1_word2)
- {
- unsigned int compressed1_norm, compressed2_norm;
- unsigned int compressed1_flip, compressed2_flip;
- unsigned int compressed1_temp, compressed2_temp;
- uint8 avg_color_quant1[3], avg_color_quant2[3];
- float avg_color_float1[3],avg_color_float2[3];
- int enc_color1[3], enc_color2[3], diff[3];
- int min_error=255*255*8*3;
-
- int norm_err=0;
- int flip_err=0;
- int temp_err=0;
- for(int flipbit=0; flipbit<2; flipbit++)
- {
- //compute average color for each half.
- for(int c=0; c<3; c++)
- {
- avg_color_float1[c]=0;
- avg_color_float2[c]=0;
- float sum1=0;
- float sum2=0;
- for(int x=0; x<4; x++)
- {
- for(int y=0; y<4; y++)
- {
- float fac=1;
- int index = x+startx+(y+starty)*width;
- //transparent pixels are only barely figured into the average. This ensures that they DO matter if we have only
- //transparent pixels in one half of the block, and not otherwise. A bit ugly perhaps.
- if(alphaimg[index]<128)
- fac=0.0001f;
- float col = fac*img[index*3+c];
- if( (flipbit==0&&x<2) || (flipbit==1&&y<2) )
- {
- sum1+=fac;
- avg_color_float1[c]+=col;
- }
- else
- {
- sum2+=fac;
- avg_color_float2[c]+=col;
- }
- }
- }
- avg_color_float1[c]/=sum1;
- avg_color_float2[c]/=sum2;
- }
- uint8 dummy[3];
- quantize555ColorCombined(avg_color_float1, enc_color1, dummy);
- quantize555ColorCombined(avg_color_float2, enc_color2, dummy);
- diff[0] = enc_color2[0]-enc_color1[0];
- diff[1] = enc_color2[1]-enc_color1[1];
- diff[2] = enc_color2[2]-enc_color1[2];
- //make sure diff is small enough for diff-coding
- for(int c=0; c<3; c++)
- {
- if(diff[c]<-4)
- diff[c]=-4;
- if(diff[c]>3)
- diff[c]=3;
- enc_color2[c]=enc_color1[c]+diff[c];
- }
- avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
- avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
- avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
- // Pack bits into the first word.
- // see regular compressblockdiffflipfast for details
- compressed1_temp = 0;
- PUTBITSHIGH( compressed1_temp, !isTransparent, 1, 33);
- PUTBITSHIGH( compressed1_temp, enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1_temp, enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1_temp, enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1_temp, diff[0], 3, 58);
- PUTBITSHIGH( compressed1_temp, diff[1], 3, 50);
- PUTBITSHIGH( compressed1_temp, diff[2], 3, 42);
- temp_err = 0;
-
- int besterror[2];
- besterror[0]=255*255*3*16;
- besterror[1]=255*255*3*16;
- int besttable[2];
- int best_indices_LSB[16];
- int best_indices_MSB[16];
- //for each table, we're going to compute the indices required to get minimum error in each half.
- //then we'll check if this was the best table for either half, and set besterror/besttable accordingly.
- for(int table=0; table<8; table++)
- {
- int taberror[2];//count will be sort of an index of each pixel within a half, determining where the index will be placed in the bitstream.
-
- int pixel_indices_LSB[16],pixel_indices_MSB[16];
-
- for(int i=0; i<2; i++)
- {
- taberror[i]=0;
- }
- for(int x=0; x<4; x++)
- {
- for(int y=0; y<4; y++)
- {
- int index = x+startx+(y+starty)*width;
- uint8 basecol[3];
- bool transparentPixel=alphaimg[index]<128;
- //determine which half of the block this pixel is in, based on the flipbit.
- int half=0;
- if( (flipbit==0&&x<2) || (flipbit&&y<2) )
- {
- basecol[0]=avg_color_quant1[0];
- basecol[1]=avg_color_quant1[1];
- basecol[2]=avg_color_quant1[2];
- }
- else
- {
- half=1;
- basecol[0]=avg_color_quant2[0];
- basecol[1]=avg_color_quant2[1];
- basecol[2]=avg_color_quant2[2];
- }
- int besterri=255*255*3*2;
- int besti=0;
- int erri;
- for(int i=0; i<4; i++)
- {
- if(i==1&&isTransparent)
- continue;
- erri=0;
- for(int c=0; c<3; c++)
- {
- int col=CLAMP(0,((int)basecol[c])+compressParams[table*2][i],255);
- if(i==2&&isTransparent)
- {
- col=(int)basecol[c];
- }
- int errcol=col-((int)(img[index*3+c]));
- erri=erri+(errcol*errcol);
- }
- if(erri<besterri)
- {
- besterri=erri;
- besti=i;
- }
- }
- if(transparentPixel)
- {
- besterri=0;
- besti=1;
- }
- //the best index for this pixel using this table for its half is known.
- //add its error to error for this table and half.
- taberror[half]+=besterri;
- //store the index! we might toss it later though if this was a bad table.
- int pixel_index = scramble[besti];
- pixel_indices_MSB[x*4+y]=(pixel_index >> 1);
- pixel_indices_LSB[x*4+y]=(pixel_index & 1);
- }
- }
- for(int half=0; half<2; half++)
- {
- if(taberror[half]<besterror[half])
- {
- besterror[half]=taberror[half];
- besttable[half]=table;
- for(int i=0; i<16; i++)
- {
- int thishalf=0;
- int y=i%4;
- int x=i/4;
- if( !(flipbit==0&&x<2) && !(flipbit&&y<2) )
- thishalf=1;
- if(half!=thishalf) //this pixel is not in given half, don't update best index!
- continue;
- best_indices_MSB[i]=pixel_indices_MSB[i];
- best_indices_LSB[i]=pixel_indices_LSB[i];
- }
- }
- }
- }
- PUTBITSHIGH( compressed1_temp, besttable[0], 3, 39);
- PUTBITSHIGH( compressed1_temp, besttable[1], 3, 36);
- PUTBITSHIGH( compressed1_temp, 0, 1, 32);
- compressed2_temp = 0;
- for(int i=0; i<16; i++)
- {
- PUTBITS( compressed2_temp, (best_indices_MSB[i] ), 1, 16+i);
- PUTBITS( compressed2_temp, (best_indices_LSB[i] ), 1, i);
- }
-
- if(flipbit)
- {
- flip_err=besterror[0]+besterror[1];
- compressed1_flip=compressed1_temp;
- compressed2_flip=compressed2_temp;
- }
- else
- {
- norm_err=besterror[0]+besterror[1];
- compressed1_norm=compressed1_temp;
- compressed2_norm=compressed2_temp;
- }
- }
- // Now to find out if flipping was a good idea or not
- if(norm_err <= flip_err)
- {
- etc1_word1 = compressed1_norm | 0;
- etc1_word2 = compressed2_norm;
- return norm_err;
- }
- else
- {
- etc1_word1 = compressed1_flip | 1;
- etc1_word2 = compressed2_flip;
- return flip_err;
- }
- }
- // Calculate RGBA error --- only count non-transparent pixels (alpha > 128)
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calcBlockErrorRGBA(uint8 *img, uint8 *imgdec, uint8* alpha, int width, int height, int startx, int starty)
- {
- int xx,yy;
- double err;
- err = 0;
- for(xx = startx; xx< startx+4; xx++)
- {
- for(yy = starty; yy<starty+4; yy++)
- {
- //only count non-transparent pixels.
- if(alpha[yy*width+xx]>128)
- {
- err += SQUARE(1.0*RED(img,width,xx,yy) - 1.0*RED(imgdec, width, xx,yy));
- err += SQUARE(1.0*GREEN(img,width,xx,yy)- 1.0*GREEN(imgdec, width, xx,yy));
- err += SQUARE(1.0*BLUE(img,width,xx,yy) - 1.0*BLUE(imgdec, width, xx,yy));
- }
- }
- }
- return err;
- }
- //calculates the error for a block using the given colors, and the paremeters required to obtain the error. This version uses 1-bit punch-through alpha.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calculateError59TAlpha(uint8* srcimg, uint8* alpha,int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
- {
- double block_error = 0,
- best_block_error = MAXIMUM_ERROR,
- pixel_error,
- best_pixel_error;
- int diff[3];
- uint8 best_sw;
- unsigned int pixel_colors;
- uint8 colors[2][3];
- uint8 possible_colors[4][3];
- // First use the colors as they are, then swap them
- for (uint8 sw = 0; sw <2; ++sw)
- {
- if (sw == 1)
- {
- swapColors(colorsRGB444);
- }
- decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_59T); ++d)
- {
- calculatePaintColors59T(d,PATTERN_T, colors, possible_colors);
-
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- best_pixel_error = MAXIMUM_ERROR;
- pixel_colors <<=2; // Make room for next value
- // Loop possible block colors
- if(alpha[x+startx+(y+starty)*width]==0)
- {
- best_pixel_error=0;
- pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
- pixel_colors |= 2; //insert the index for this pixel, two meaning transparent.
- }
- else
- {
- for (uint8 c = 0; c < 4; ++c)
- {
-
- if(c==2)
- continue; //don't use this, because we don't have alpha here and index 2 means transparent.
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
- diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
- pixel_error = weight[R]*SQUARE(diff[R]) +
- weight[G]*SQUARE(diff[G]) +
- weight[B]*SQUARE(diff[B]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
- pixel_colors |= c; //insert the index for this pixel
- }
- }
- }
- block_error += best_pixel_error;
- }
- }
- if (block_error < best_block_error)
- {
- best_block_error = block_error;
- distance = d;
- pixel_indices = pixel_colors;
- best_sw = sw;
- }
- }
-
- if (sw == 1 && best_sw == 0)
- {
- swapColors(colorsRGB444);
- }
- decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
- }
- return best_block_error;
- }
- // same as fastest t-mode compressor above, but here one of the colors (the central one in the T) is used to also signal that the pixel is transparent.
- // the only difference is that calculateError has been swapped out to one that considers alpha.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double compressBlockTHUMB59TAlpha(uint8 *img, uint8* alpha, int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- double best_error = MAXIMUM_ERROR;
- uint8 best_colorsRGB444[2][3];
- unsigned int best_pixel_indices;
- uint8 best_distance;
- double error_no_i;
- uint8 colorsRGB444_no_i[2][3];
- unsigned int pixel_indices_no_i;
- uint8 distance_no_i;
- uint8 colors[2][3];
- // Calculate average color using the LBG-algorithm
- computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
- compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
- // Determine the parameters for the lowest error
- error_no_i = calculateError59TAlpha(img, alpha, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
- best_error = error_no_i;
- best_distance = distance_no_i;
- best_pixel_indices = pixel_indices_no_i;
- copyColors(colorsRGB444_no_i, best_colorsRGB444);
- // Put the compress params into the compression block
- packBlock59T(best_colorsRGB444, best_distance, best_pixel_indices, compressed1, compressed2);
- return best_error;
- }
- // Put bits in order for the format.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void stuff59bitsDiffFalse(unsigned int thumbT59_word1, unsigned int thumbT59_word2, unsigned int &thumbT_word1, unsigned int &thumbT_word2)
- {
- // Put bits in twotimer configuration for 59 (red overflows)
- //
- // Go from this bit layout:
- //
- // |63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
- // |----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
- //
- // |31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- // |----------------------------------------index bits---------------------------------------------|
- //
- //
- // To this:
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // -----------------------------------------------------------------------------------------------
- // |// // //|R0a |//|R0b |G0 |B0 |R1 |G1 |B1 |da |df|db|
- // -----------------------------------------------------------------------------------------------
- //
- // |31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- // |----------------------------------------index bits---------------------------------------------|
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // -----------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |df|fp|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bt|bt|
- // ------------------------------------------------------------------------------------------------
- uint8 R0a;
- uint8 bit, a, b, c, d, bits;
- R0a = GETBITSHIGH( thumbT59_word1, 2, 58);
- // Fix middle part
- thumbT_word1 = thumbT59_word1 << 1;
- // Fix R0a (top two bits of R0)
- PUTBITSHIGH( thumbT_word1, R0a, 2, 60);
- // Fix db (lowest bit of d)
- PUTBITSHIGH( thumbT_word1, thumbT59_word1, 1, 32);
- //
- // Make sure that red overflows:
- a = GETBITSHIGH( thumbT_word1, 1, 60);
- b = GETBITSHIGH( thumbT_word1, 1, 59);
- c = GETBITSHIGH( thumbT_word1, 1, 57);
- d = GETBITSHIGH( thumbT_word1, 1, 56);
- // The following bit abcd bit sequences should be padded with ones: 0111, 1010, 1011, 1101, 1110, 1111
- // The following logical expression checks for the presence of any of those:
- bit = (a & c) | (!a & b & c & d) | (a & b & !c & d);
- bits = 0xf*bit;
- PUTBITSHIGH( thumbT_word1, bits, 3, 63);
- PUTBITSHIGH( thumbT_word1, !bit, 1, 58);
- // Set diffbit
- PUTBITSHIGH( thumbT_word1, 0, 1, 33);
- thumbT_word2 = thumbT59_word2;
- }
- // Tests if there is at least one pixel in the image which would get alpha = 0 in punchtrough mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- bool hasAlpha(uint8* alphaimg, int ix, int iy, int width)
- {
- for(int x=ix; x<ix+4; x++)
- {
- for(int y=iy; y<iy+4; y++)
- {
- int index = x+y*width;
- if(alphaimg[index]<128)
- {
- return true;
- }
- }
- }
- return false;
- }
- // Compress a block with ETC2 RGB
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockETC2Fast(uint8 *img, uint8* alphaimg, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int etc1_word1;
- unsigned int etc1_word2;
- double error_etc1;
- unsigned int planar57_word1;
- unsigned int planar57_word2;
- unsigned int planar_word1;
- unsigned int planar_word2;
- double error_planar;
- unsigned int thumbT59_word1;
- unsigned int thumbT59_word2;
- unsigned int thumbT_word1;
- unsigned int thumbT_word2;
- double error_thumbT;
-
- unsigned int thumbH58_word1;
- unsigned int thumbH58_word2;
- unsigned int thumbH_word1;
- unsigned int thumbH_word2;
- double error_thumbH;
- double error_best;
- signed char best_char;
- int best_mode;
-
- if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- /* if we have one-bit alpha, we never use the individual mode,
- instead that bit flags that one of our four offsets will instead
- mean transparent (with 0 offset for color channels) */
- /* the regular ETC individual mode is disabled, but the old T, H and planar modes
- are kept unchanged and may be used for blocks without transparency.
- Introduced are old ETC with only differential coding,
- ETC differential but with 3 offsets and transparent,
- and T-mode with 3 colors plus transparent.*/
- /* in a fairly hackish manner, error_etc1, etc1_word1 and etc1_word2 will
- represent the best out of the three introduced modes, to be compared
- with the three kept modes in the old code*/
- unsigned int tempword1, tempword2;
- double temperror;
- //try regular differential transparent mode
- int testerr= compressBlockDifferentialWithAlpha(true,img,alphaimg, imgdec,width,height,startx,starty,etc1_word1,etc1_word2);
- uint8* alphadec = new uint8[width*height];
- decompressBlockDifferentialWithAlpha(etc1_word1, etc1_word2, imgdec, alphadec,width, height, startx, starty);
- error_etc1 = calcBlockErrorRGBA(img, imgdec, alphaimg,width, height, startx, starty);
- if(error_etc1!=testerr)
- {
- printf("testerr: %d, etcerr: %lf\n",testerr,error_etc1);
- }
- //try T-mode with transparencies
- //for now, skip this...
- compressBlockTHUMB59TAlpha(img,alphaimg,width,height,startx,starty,tempword1,tempword2);
- decompressBlockTHUMB59TAlpha(tempword1,tempword2,imgdec, alphadec, width,height,startx,starty);
- temperror=calcBlockErrorRGBA(img, imgdec, alphaimg, width, height, startx, starty);
- if(temperror<error_etc1)
- {
- error_etc1=temperror;
- stuff59bitsDiffFalse(tempword1,tempword2,etc1_word1,etc1_word2);
- }
- compressBlockTHUMB58HAlpha(img,alphaimg,width,height,startx,starty,tempword1,tempword2);
- decompressBlockTHUMB58HAlpha(tempword1,tempword2,imgdec, alphadec, width,height,startx,starty);
- temperror=calcBlockErrorRGBA(img, imgdec, alphaimg, width, height, startx, starty);
- if(temperror<error_etc1)
- {
- error_etc1=temperror;
- stuff58bitsDiffFalse(tempword1,tempword2,etc1_word1,etc1_word2);
- }
- //if we have transparency in this pixel, we know that one of these two modes was best..
- if(hasAlpha(alphaimg,startx,starty,width))
- {
- compressed1=etc1_word1;
- compressed2=etc1_word2;
- delete[] alphadec;
- return;
- }
- //error_etc1=255*255*1000;
- //otherwise, they MIGHT have been the best, although that's unlikely.. anyway, try old differential mode now
-
- compressBlockDifferentialWithAlpha(false,img,alphaimg,imgdec,width,height,startx,starty,tempword1,tempword2);
- decompressBlockDiffFlip(tempword1, tempword2, imgdec, width, height, startx, starty);
- temperror = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- decompressBlockDifferentialWithAlpha(tempword1,tempword2,imgdec,alphadec,width,height,startx,starty);
- if(temperror<error_etc1)
- {
- error_etc1=temperror;
- etc1_word1=tempword1;
- etc1_word2=tempword2;
- }
- delete[] alphadec;
- //drop out of this if, and test old T, H and planar modes (we have already returned if there are transparent pixels in this block)
- }
- else
- {
- //this includes individual mode, and therefore doesn't apply in case of punch-through alpha.
- compressBlockDiffFlipFast(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
- decompressBlockDiffFlip(etc1_word1, etc1_word2, imgdec, width, height, startx, starty);
- error_etc1 = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- }
- //these modes apply regardless of whether we want punch-through alpha or not.
- //error etc_1 and etc1_word1/etc1_word2 contain previous best candidate.
- compressBlockPlanar57(img, width, height, startx, starty, planar57_word1, planar57_word2);
- decompressBlockPlanar57(planar57_word1, planar57_word2, imgdec, width, height, startx, starty);
- error_planar = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
- compressBlockTHUMB59TFastest(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
- decompressBlockTHUMB59T(thumbT59_word1, thumbT59_word2, imgdec, width, height, startx, starty);
- error_thumbT = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
- compressBlockTHUMB58HFastest(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
- decompressBlockTHUMB58H(thumbH58_word1, thumbH58_word2, imgdec, width, height, startx, starty);
- error_thumbH = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
- error_best = error_etc1;
- compressed1 = etc1_word1;
- compressed2 = etc1_word2;
- best_char = '.';
- best_mode = MODE_ETC1;
- if(error_planar < error_best)
- {
- compressed1 = planar_word1;
- compressed2 = planar_word2;
- best_char = 'p';
- error_best = error_planar;
- best_mode = MODE_PLANAR;
- }
- if(error_thumbT < error_best)
- {
- compressed1 = thumbT_word1;
- compressed2 = thumbT_word2;
- best_char = 'T';
- error_best = error_thumbT;
- best_mode = MODE_THUMB_T;
- }
- if(error_thumbH < error_best)
- {
- compressed1 = thumbH_word1;
- compressed2 = thumbH_word2;
- best_char = 'H';
- error_best = error_thumbH;
- best_mode = MODE_THUMB_H;
- }
-
- switch(best_mode)
- {
- // Now see which mode won and compress that a little bit harder
- case MODE_THUMB_T:
- compressBlockTHUMB59TFast(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
- decompressBlockTHUMB59T(thumbT59_word1, thumbT59_word2, imgdec, width, height, startx, starty);
- error_thumbT = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
- if(error_thumbT < error_best)
- {
- compressed1 = thumbT_word1;
- compressed2 = thumbT_word2;
- }
- break;
- case MODE_THUMB_H:
- compressBlockTHUMB58HFast(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
- decompressBlockTHUMB58H(thumbH58_word1, thumbH58_word2, imgdec, width, height, startx, starty);
- error_thumbH = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
- if(error_thumbH < error_best)
- {
- compressed1 = thumbH_word1;
- compressed2 = thumbH_word2;
- }
- break;
- default:
- break;
- }
- }
- // Compress an ETC2 RGB block using perceptual error metric
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockETC2FastPerceptual(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int etc1_word1;
- unsigned int etc1_word2;
- double error_etc1;
- unsigned int planar57_word1;
- unsigned int planar57_word2;
- unsigned int planar_word1;
- unsigned int planar_word2;
- double error_planar;
- unsigned int thumbT59_word1;
- unsigned int thumbT59_word2;
- unsigned int thumbT_word1;
- unsigned int thumbT_word2;
- double error_thumbT;
-
- unsigned int thumbH58_word1;
- unsigned int thumbH58_word2;
- unsigned int thumbH_word1;
- unsigned int thumbH_word2;
- double error_thumbH;
- double error_best;
- signed char best_char;
- int best_mode;
- compressBlockDiffFlipFastPerceptual(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
- decompressBlockDiffFlip(etc1_word1, etc1_word2, imgdec, width, height, startx, starty);
- error_etc1 = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
- compressBlockPlanar57(img, width, height, startx, starty, planar57_word1, planar57_word2);
- decompressBlockPlanar57(planar57_word1, planar57_word2, imgdec, width, height, startx, starty);
- error_planar = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
- stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
- compressBlockTHUMB59TFastestPerceptual1000(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
- decompressBlockTHUMB59T(thumbT59_word1, thumbT59_word2, imgdec, width, height, startx, starty);
- error_thumbT = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
- stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
- compressBlockTHUMB58HFastestPerceptual1000(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
- decompressBlockTHUMB58H(thumbH58_word1, thumbH58_word2, imgdec, width, height, startx, starty);
- error_thumbH = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
- stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
- error_best = error_etc1;
- compressed1 = etc1_word1;
- compressed2 = etc1_word2;
- best_char = '.';
- best_mode = MODE_ETC1;
- if(error_planar < error_best)
- {
- compressed1 = planar_word1;
- compressed2 = planar_word2;
- best_char = 'p';
- error_best = error_planar;
- best_mode = MODE_PLANAR;
- }
- if(error_thumbT < error_best)
- {
- compressed1 = thumbT_word1;
- compressed2 = thumbT_word2;
- best_char = 'T';
- error_best = error_thumbT;
- best_mode = MODE_THUMB_T;
- }
- if(error_thumbH < error_best)
- {
- compressed1 = thumbH_word1;
- compressed2 = thumbH_word2;
- best_char = 'H';
- error_best = error_thumbH;
- best_mode = MODE_THUMB_H;
- }
-
- switch(best_mode)
- {
- // Now see which mode won and compress that a little bit harder
- case MODE_THUMB_T:
- compressBlockTHUMB59TFast(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
- decompressBlockTHUMB59T(thumbT59_word1, thumbT59_word2, imgdec, width, height, startx, starty);
- error_thumbT = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
- if(error_thumbT < error_best)
- {
- compressed1 = thumbT_word1;
- compressed2 = thumbT_word2;
- }
- break;
- case MODE_THUMB_H:
- compressBlockTHUMB58HFast(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
- decompressBlockTHUMB58H(thumbH58_word1, thumbH58_word2, imgdec, width, height, startx, starty);
- error_thumbH = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
- if(error_thumbH < error_best)
- {
- compressed1 = thumbH_word1;
- compressed2 = thumbH_word2;
- }
- break;
- default:
- break;
- }
- }
- // Write a word in big endian style
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void write_big_endian_2byte_word(unsigned short *blockadr, FILE *f)
- {
- uint8 bytes[2];
- unsigned short block;
- block = blockadr[0];
- bytes[0] = (block >> 8) & 0xff;
- bytes[1] = (block >> 0) & 0xff;
- fwrite(&bytes[0],1,1,f);
- fwrite(&bytes[1],1,1,f);
- }
- // Write a word in big endian style
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void write_big_endian_4byte_word(unsigned int *blockadr, FILE *f)
- {
- uint8 bytes[4];
- unsigned int block;
- block = blockadr[0];
- bytes[0] = (block >> 24) & 0xff;
- bytes[1] = (block >> 16) & 0xff;
- bytes[2] = (block >> 8) & 0xff;
- bytes[3] = (block >> 0) & 0xff;
- fwrite(&bytes[0],1,1,f);
- fwrite(&bytes[1],1,1,f);
- fwrite(&bytes[2],1,1,f);
- fwrite(&bytes[3],1,1,f);
- }
- extern int alphaTable[256][8];
- extern int alphaBase[16][4];
- // valtab holds precalculated data used for compressing using EAC2.
- // Note that valtab is constructed using get16bits11bits, which means
- // that it already is expanded to 16 bits.
- // Note also that it its contents will depend on the value of formatSigned.
- int *valtab;
- void setupAlphaTableAndValtab()
- {
- setupAlphaTable();
- //fix precomputation table..!
- valtab = new int[1024*512];
- int16 val16;
- int count=0;
- for(int base=0; base<256; base++)
- {
- for(int tab=0; tab<16; tab++)
- {
- for(int mul=0; mul<16; mul++)
- {
- for(int index=0; index<8; index++)
- {
- if(formatSigned)
- {
- val16=get16bits11signed(base,tab,mul,index);
- valtab[count] = val16 + 256*128;
- }
- else
- valtab[count]=get16bits11bits(base,tab,mul,index);
- count++;
- }
- }
- }
- }
- }
- // Reads alpha data
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void readAlpha(uint8* &data, int &width, int &height, int &extendedwidth, int &extendedheight)
- {
- //width and height are already known..?
- uint8* tempdata;
- int wantedBitDepth;
- if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- wantedBitDepth=8;
- }
- else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
- {
- wantedBitDepth=16;
- }
- else
- {
- printf("invalid format for alpha reading!\n");
- exit(1);
- }
- fReadPGM("alpha.pgm",width,height,tempdata,wantedBitDepth);
- extendedwidth=4*((width+3)/4);
- extendedheight=4*((height+3)/4);
- if(width==extendedwidth&&height==extendedheight)
- {
- data=tempdata;
- }
- else
- {
- data = (uint8*)malloc(extendedwidth*extendedheight*wantedBitDepth/8);
- uint8 last=0;
- uint8 lastlast=0;
- for(int x=0; x<extendedwidth; x++)
- {
- for(int y=0; y<extendedheight; y++)
- {
- if(wantedBitDepth==8)
- {
- if(x<width&&y<height)
- {
- last = tempdata[x+y*width];
- }
- data[x+y*extendedwidth]=last;
- }
- else
- {
- if(x<width&&y<height)
- {
- last = tempdata[(x+y*width)*2];
- lastlast = tempdata[(x+y*width)*2+1];
- }
- data[(x+y*extendedwidth)*2]=last;
- data[(x+y*extendedwidth)*2+1]=lastlast;
- }
- }
- }
- }
- if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- for(int x=0; x<extendedwidth; x++)
- {
- for(int y=0; y<extendedheight; y++)
- {
- if(data[x+y*extendedwidth]<128)
- data[x+y*extendedwidth]=0;
- else
- data[x+y*extendedwidth]=255;
- }
- }
- }
- }
- // Compresses the alpha part of a GL_COMPRESSED_RGBA8_ETC2_EAC block.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockAlphaFast(uint8 * data, int ix, int iy, int width, int height, uint8* returnData)
- {
- int alphasum=0;
- int maxdist=-2;
- for(int x=0; x<4; x++)
- {
- for(int y=0; y<4; y++)
- {
- alphasum+=data[ix+x+(iy+y)*width];
- }
- }
- int alpha = (int)( ((float)alphasum)/16.0f+0.5f); //average pixel value, used as guess for base value.
- for(int x=0; x<4; x++)
- {
- for(int y=0; y<4; y++)
- {
- if(abs(alpha-data[ix+x+(iy+y)*width])>maxdist)
- maxdist=abs(alpha-data[ix+x+(iy+y)*width]); //maximum distance from average
- }
- }
- int approxPos = (maxdist*255)/160-4; //experimentally derived formula for calculating approximate table position given a max distance from average
- if(approxPos>255)
- approxPos=255;
- int startTable=approxPos-15; //first table to be tested
- if(startTable<0)
- startTable=0;
- int endTable=clamp(approxPos+15); //last table to be tested
- int bestsum=1000000000;
- int besttable=-3;
- int bestalpha=128;
- int prevalpha=alpha;
- //main loop: determine best base alpha value and offset table to use for compression
- //try some different alpha tables.
- for(int table = startTable; table<endTable&&bestsum>0; table++)
- {
- int tablealpha=prevalpha;
- int tablebestsum=1000000000;
- //test some different alpha values, trying to find the best one for the given table.
- for(int alphascale=16; alphascale>0; alphascale/=4)
- {
- int startalpha;
- int endalpha;
- if(alphascale==16)
- {
- startalpha = clamp(tablealpha-alphascale*4);
- endalpha = clamp(tablealpha+alphascale*4);
- }
- else
- {
- startalpha = clamp(tablealpha-alphascale*2);
- endalpha = clamp(tablealpha+alphascale*2);
- }
- for(alpha=startalpha; alpha<=endalpha; alpha+=alphascale)
- {
- int sum=0;
- int val,diff,bestdiff=10000000,index;
- for(int x=0; x<4; x++)
- {
- for(int y=0; y<4; y++)
- {
- //compute best offset here, add square difference to sum..
- val=data[ix+x+(iy+y)*width];
- bestdiff=1000000000;
- //the values are always ordered from small to large, with the first 4 being negative and the last 4 positive
- //search is therefore made in the order 0-1-2-3 or 7-6-5-4, stopping when error increases compared to the previous entry tested.
- if(val>alpha)
- {
- for(index=7; index>3; index--)
- {
- diff=clamp_table[alpha+(int)(alphaTable[table][index])+255]-val;
- diff*=diff;
- if(diff<=bestdiff)
- {
- bestdiff=diff;
- }
- else
- break;
- }
- }
- else
- {
- for(index=0; index<4; index++)
- {
- diff=clamp_table[alpha+(int)(alphaTable[table][index])+255]-val;
- diff*=diff;
- if(diff<bestdiff)
- {
- bestdiff=diff;
- }
- else
- break;
- }
- }
- //best diff here is bestdiff, add it to sum!
- sum+=bestdiff;
- //if the sum here is worse than previously best already, there's no use in continuing the count..
- //note that tablebestsum could be used for more precise estimation, but the speedup gained here is deemed more important.
- if(sum>bestsum)
- {
- x=9999; //just to make it large and get out of the x<4 loop
- break;
- }
- }
- }
- if(sum<tablebestsum)
- {
- tablebestsum=sum;
- tablealpha=alpha;
- }
- if(sum<bestsum)
- {
- bestsum=sum;
- besttable=table;
- bestalpha=alpha;
- }
- }
- if(alphascale<=2)
- alphascale=0;
- }
- }
- alpha=bestalpha;
- //"good" alpha value and table are known!
- //store them, then loop through the pixels again and print indices.
- returnData[0]=alpha;
- returnData[1]=besttable;
- for(int pos=2; pos<8; pos++)
- {
- returnData[pos]=0;
- }
- int byte=2;
- int bit=0;
- for(int x=0; x<4; x++)
- {
- for(int y=0; y<4; y++)
- {
- //find correct index
- int besterror=1000000;
- int bestindex=99;
- for(int index=0; index<8; index++) //no clever ordering this time, as this loop is only run once per block anyway
- {
- int error= (clamp(alpha +(int)(alphaTable[besttable][index]))-data[ix+x+(iy+y)*width])*(clamp(alpha +(int)(alphaTable[besttable][index]))-data[ix+x+(iy+y)*width]);
- if(error<besterror)
- {
- besterror=error;
- bestindex=index;
- }
- }
- //best table index has been determined.
- //pack 3-bit index into compressed data, one bit at a time
- for(int numbit=0; numbit<3; numbit++)
- {
- returnData[byte]|=getbit(bestindex,2-numbit,7-bit);
- bit++;
- if(bit>7)
- {
- bit=0;
- byte++;
- }
- }
- }
- }
- }
- // Helper function for the below function
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int getPremulIndex(int base, int tab, int mul, int index)
- {
- return (base<<11)+(tab<<7)+(mul<<3)+index;
- }
- // Calculates the error used in compressBlockAlpha16()
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calcError(uint8* data, int ix, int iy, int width, int height, int base, int tab, int mul, double prevbest)
- {
- int offset = getPremulIndex(base,tab,mul,0);
- double error=0;
- for (int y=0; y<4; y++)
- {
- for(int x=0; x<4; x++)
- {
- double besthere = (1<<20);
- besthere*=besthere;
- uint8 byte1 = data[2*(x+ix+(y+iy)*width)];
- uint8 byte2 = data[2*(x+ix+(y+iy)*width)+1];
- int alpha = (byte1<<8)+byte2;
- for(int index=0; index<8; index++)
- {
- double indexError;
- indexError = alpha-valtab[offset+index];
- indexError*=indexError;
- if(indexError<besthere)
- besthere=indexError;
- }
- error+=besthere;
- if(error>=prevbest)
- return prevbest+(1<<30);
- }
- }
- return error;
- }
- // compressBlockAlpha16
- //
- // Compresses a block using the 11-bit EAC formats.
- // Depends on the global variable formatSigned.
- //
- // COMPRESSED_R11_EAC (if formatSigned = 0)
- // This is an 11-bit unsigned format. Since we do not have a good 11-bit file format, we use 16-bit pgm instead.
- // Here we assume that, in the input 16-bit pgm file, 0 represents 0.0 and 65535 represents 1.0. The function compressBlockAlpha16
- // will find the compressed block which best matches the data. In detail, it will find the compressed block, which
- // if decompressed, will generate an 11-bit block that after bit replication to 16-bits will generate the closest
- // block to the original 16-bit pgm block.
- //
- // COMPRESSED_SIGNED_R11_EAC (if formatSigned = 1)
- // This is an 11-bit signed format. Since we do not have any signed file formats, we use unsigned 16-bit pgm instead.
- // Hence we assume that, in the input 16-bit pgm file, 1 represents -1.0, 32768 represents 0.0 and 65535 represents 1.0.
- // The function compresseBlockAlpha16 will find the compressed block, which if decompressed, will generate a signed
- // 11-bit block that after bit replication to 16-bits and conversion to unsigned (1 equals -1.0, 32768 equals 0.0 and
- // 65535 equals 1.0) will generate the closest block to the original 16-bit pgm block.
- //
- // COMPRESSED_RG11_EAC is compressed by calling the function twice, dito for COMPRESSED_SIGNED_RG11_EAC.
- //
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockAlpha16(uint8* data, int ix, int iy, int width, int height, uint8* returnData)
- {
- unsigned int bestbase, besttable, bestmul;
- double besterror;
- besterror=1<<20;
- besterror*=besterror;
- for(int base=0; base<256; base++)
- {
- for(int table=0; table<16; table++)
- {
- for(int mul=0; mul<16; mul++)
- {
- double e = calcError(data, ix, iy, width, height,base,table,mul,besterror);
- if(e<besterror)
- {
- bestbase=base;
- besttable=table;
- bestmul=mul;
- besterror=e;
- }
- }
- }
- }
- returnData[0]=bestbase;
- returnData[1]=(bestmul<<4)+besttable;
- if(formatSigned)
- {
- //if we have a signed format, the base value should be given as a signed byte.
- signed char signedbase = bestbase-128;
- returnData[0]=*((uint8*)(&signedbase));
- }
-
- for(int i=2; i<8; i++)
- {
- returnData[i]=0;
- }
- int byte=2;
- int bit=0;
- for (int x=0; x<4; x++)
- {
- for(int y=0; y<4; y++)
- {
- double besterror=255*255;
- besterror*=besterror;
- int bestindex=99;
- uint8 byte1 = data[2*(x+ix+(y+iy)*width)];
- uint8 byte2 = data[2*(x+ix+(y+iy)*width)+1];
- int alpha = (byte1<<8)+byte2;
- for(unsigned int index=0; index<8; index++)
- {
- double indexError;
- if(formatSigned)
- {
- int16 val16;
- int val;
- val16 = get16bits11signed(bestbase,besttable,bestmul,index);
- val = val16 + 256*128;
- indexError = alpha-val;
- }
- else
- indexError = alpha-get16bits11bits(bestbase,besttable,bestmul,index);
- indexError*=indexError;
- if(indexError<besterror)
- {
- besterror=indexError;
- bestindex=index;
- }
- }
-
- for(int numbit=0; numbit<3; numbit++)
- {
- returnData[byte]|=getbit(bestindex,2-numbit,7-bit);
- bit++;
- if(bit>7)
- {
- bit=0;
- byte++;
- }
- }
- }
- }
- }
- // Exhaustive compression of alpha compression in a GL_COMPRESSED_RGB8_ETC2 block
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockAlphaSlow(uint8* data, int ix, int iy, int width, int height, uint8* returnData)
- {
- //determine the best table and base alpha value for this block using MSE
- int alphasum=0;
- int maxdist=-2;
- for(int x=0; x<4; x++)
- {
- for(int y=0; y<4; y++)
- {
- alphasum+=data[ix+x+(iy+y)*width];
- }
- }
- int alpha = (int)( ((float)alphasum)/16.0f+0.5f); //average pixel value, used as guess for base value.
- int bestsum=1000000000;
- int besttable=-3;
- int bestalpha=128;
- int prevalpha=alpha;
- //main loop: determine best base alpha value and offset table to use for compression
- //try some different alpha tables.
- for(int table = 0; table<256&&bestsum>0; table++)
- {
- int tablealpha=prevalpha;
- int tablebestsum=1000000000;
- //test some different alpha values, trying to find the best one for the given table.
- for(int alphascale=32; alphascale>0; alphascale/=8)
- {
-
- int startalpha = clamp(tablealpha-alphascale*4);
- int endalpha = clamp(tablealpha+alphascale*4);
-
- for(alpha=startalpha; alpha<=endalpha; alpha+=alphascale) {
- int sum=0;
- int val,diff,bestdiff=10000000,index;
- for(int x=0; x<4; x++)
- {
- for(int y=0; y<4; y++)
- {
- //compute best offset here, add square difference to sum..
- val=data[ix+x+(iy+y)*width];
- bestdiff=1000000000;
- //the values are always ordered from small to large, with the first 4 being negative and the last 4 positive
- //search is therefore made in the order 0-1-2-3 or 7-6-5-4, stopping when error increases compared to the previous entry tested.
- if(val>alpha)
- {
- for(index=7; index>3; index--)
- {
- diff=clamp_table[alpha+(alphaTable[table][index])+255]-val;
- diff*=diff;
- if(diff<=bestdiff)
- {
- bestdiff=diff;
- }
- else
- break;
- }
- }
- else
- {
- for(index=0; index<5; index++)
- {
- diff=clamp_table[alpha+(alphaTable[table][index])+255]-val;
- diff*=diff;
- if(diff<bestdiff)
- {
- bestdiff=diff;
- }
- else
- break;
- }
- }
- //best diff here is bestdiff, add it to sum!
- sum+=bestdiff;
- //if the sum here is worse than previously best already, there's no use in continuing the count..
- if(sum>tablebestsum)
- {
- x=9999; //just to make it large and get out of the x<4 loop
- break;
- }
- }
- }
- if(sum<tablebestsum)
- {
- tablebestsum=sum;
- tablealpha=alpha;
- }
- if(sum<bestsum)
- {
- bestsum=sum;
- besttable=table;
- bestalpha=alpha;
- }
- }
- if(alphascale==4)
- alphascale=8;
- }
- }
- alpha=bestalpha;
- //the best alpha value and table are known!
- //store them, then loop through the pixels again and print indices.
- returnData[0]=alpha;
- returnData[1]=besttable;
- for(int pos=2; pos<8; pos++)
- {
- returnData[pos]=0;
- }
- int byte=2;
- int bit=0;
- for(int x=0; x<4; x++)
- {
- for(int y=0; y<4; y++)
- {
- //find correct index
- int besterror=1000000;
- int bestindex=99;
- for(int index=0; index<8; index++) //no clever ordering this time, as this loop is only run once per block anyway
- {
- int error= (clamp(alpha +(int)(alphaTable[besttable][index]))-data[ix+x+(iy+y)*width])*(clamp(alpha +(int)(alphaTable[besttable][index]))-data[ix+x+(iy+y)*width]);
- if(error<besterror)
- {
- besterror=error;
- bestindex=index;
- }
- }
- //best table index has been determined.
- //pack 3-bit index into compressed data, one bit at a time
- for(int numbit=0; numbit<3; numbit++)
- {
- returnData[byte]|=getbit(bestindex,2-numbit,7-bit);
- bit++;
- if(bit>7)
- {
- bit=0;
- byte++;
- }
- }
- }
- }
- }
- // Calculate weighted PSNR
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calculateWeightedPSNR(uint8 *lossyimg, uint8 *origimg, int width, int height, double w1, double w2, double w3)
- {
- // Note: This calculation of PSNR uses the formula
- //
- // PSNR = 10 * log_10 ( 255^2 / wMSE )
- //
- // where the wMSE is calculated as
- //
- // 1/(N*M) * sum ( ( w1*(R' - R)^2 + w2*(G' - G)^2 + w3*(B' - B)^2) )
- //
- // typical weights are 0.299, 0.587, 0.114 for perceptually weighted PSNR and
- // 1.0/3.0, 1.0/3.0, 1.0/3.0 for nonweighted PSNR
- int x,y;
- double wMSE;
- double PSNR;
- double err;
- wMSE = 0;
- for(y=0;y<height;y++)
- {
- for(x=0;x<width;x++)
- {
- err = lossyimg[y*width*3+x*3+0] - origimg[y*width*3+x*3+0];
- wMSE = wMSE + (w1*(err * err));
- err = lossyimg[y*width*3+x*3+1] - origimg[y*width*3+x*3+1];
- wMSE = wMSE + (w2*(err * err));
- err = lossyimg[y*width*3+x*3+2] - origimg[y*width*3+x*3+2];
- wMSE = wMSE + (w3*(err * err));
- }
- }
- wMSE = wMSE / (width * height);
- if(wMSE == 0)
- {
- printf("********************************************************************\n");
- printf("There is no difference at all between image files --- infinite PSNR.\n");
- printf("********************************************************************\n");
- }
- PSNR = 10*log((1.0*255*255)/wMSE)/log(10.0);
- return PSNR;
- }
- // Calculate unweighted PSNR (weights are (1,1,1))
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calculatePSNR(uint8 *lossyimg, uint8 *origimg, int width, int height)
- {
- // Note: This calculation of PSNR uses the formula
- //
- // PSNR = 10 * log_10 ( 255^2 / MSE )
- //
- // where the MSE is calculated as
- //
- // 1/(N*M) * sum ( 1/3 * ((R' - R)^2 + (G' - G)^2 + (B' - B)^2) )
- //
- // The reason for having the 1/3 factor is the following:
- // Presume we have a grayscale image, that is acutally just the red component
- // of a color image.. The squared error is then (R' - R)^2.
- // Assume that we have a certain signal to noise ratio, say 30 dB. If we add
- // another two components (say green and blue) with the same signal to noise
- // ratio, we want the total signal to noise ratio be the same. For the
- // squared error to remain constant we must divide by three after adding
- // together the squared errors of the components.
- return calculateWeightedPSNR(lossyimg, origimg, width, height, (1.0/3.0), (1.0/3.0), (1.0/3.0));
- }
- // Decompresses a file
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void uncompressFile(char *srcfile, uint8* &img, uint8 *&alphaimg, int& active_width, int& active_height)
- {
- FILE *f;
- int width,height;
- unsigned int block_part1, block_part2;
- uint8 *newimg, *newalphaimg, *alphaimg2;
- unsigned short w, h;
- int xx, yy;
- unsigned char magic[4];
- unsigned char version[2];
- unsigned short texture_type;
- if(f=fopen(srcfile,"rb"))
- {
- // Load table
- readCompressParams();
- if(ktxFile)
- {
- //read ktx header..
- KTX_header header;
- fread(&header,sizeof(KTX_header),1,f);
- //read size parameter, which we don't actually need..
- unsigned int bitsize;
- fread(&bitsize,sizeof(unsigned int),1,f);
-
- active_width=header.pixelWidth;
- active_height = header.pixelHeight;
- w = ((active_width+3)/4)*4;
- h = ((active_height+3)/4)*4;
- width=w;
- height=h;
- if(header.glInternalFormat==GL_COMPRESSED_SIGNED_R11_EAC)
- {
- format=ETC2PACKAGE_R_NO_MIPMAPS;
- formatSigned=1;
- }
- else if(header.glInternalFormat==GL_COMPRESSED_R11_EAC)
- {
- format=ETC2PACKAGE_R_NO_MIPMAPS;
- }
- else if(header.glInternalFormat==GL_COMPRESSED_SIGNED_RG11_EAC)
- {
- format=ETC2PACKAGE_RG_NO_MIPMAPS;
- formatSigned=1;
- }
- else if(header.glInternalFormat==GL_COMPRESSED_RG11_EAC)
- {
- format=ETC2PACKAGE_RG_NO_MIPMAPS;
- }
- else if(header.glInternalFormat==GL_COMPRESSED_RGB8_ETC2)
- {
- format=ETC2PACKAGE_RGB_NO_MIPMAPS;
- }
- else if(header.glInternalFormat==GL_COMPRESSED_SRGB8_ETC2)
- {
- format=ETC2PACKAGE_sRGB_NO_MIPMAPS;
- }
- else if(header.glInternalFormat==GL_COMPRESSED_RGBA8_ETC2_EAC)
- {
- format=ETC2PACKAGE_RGBA_NO_MIPMAPS;
- }
- else if(header.glInternalFormat==GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC)
- {
- format=ETC2PACKAGE_sRGBA_NO_MIPMAPS;
- }
- else if(header.glInternalFormat==GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2)
- {
- format=ETC2PACKAGE_RGBA1_NO_MIPMAPS;
- }
- else if(header.glInternalFormat==GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2)
- {
- format=ETC2PACKAGE_sRGBA1_NO_MIPMAPS;
- }
- else if(header.glInternalFormat==GL_ETC1_RGB8_OES)
- {
- format=ETC1_RGB_NO_MIPMAPS;
- codec=CODEC_ETC;
- }
- else
- {
- printf("ktx file has unknown glInternalFormat (not etc compressed)!\n");
- exit(1);
- }
- }
- else
- {
- // Read magic nunmber
- fread(&magic[0], sizeof(unsigned char), 1, f);
- fread(&magic[1], sizeof(unsigned char), 1, f);
- fread(&magic[2], sizeof(unsigned char), 1, f);
- fread(&magic[3], sizeof(unsigned char), 1, f);
- if(!(magic[0] == 'P' && magic[1] == 'K' && magic[2] == 'M' && magic[3] == ' '))
- {
- printf("\n\n The file %s is not a .pkm file.\n",srcfile);
- exit(1);
- }
-
- // Read version
- fread(&version[0], sizeof(unsigned char), 1, f);
- fread(&version[1], sizeof(unsigned char), 1, f);
- if( version[0] == '1' && version[1] == '0' )
- {
- // Read texture type
- read_big_endian_2byte_word(&texture_type, f);
- if(!(texture_type == ETC1_RGB_NO_MIPMAPS))
- {
- printf("\n\n The file %s (of version %c.%c) does not contain a texture of known format.\n", srcfile, version[0],version[1]);
- printf("Known formats: ETC1_RGB_NO_MIPMAPS.\n", srcfile);
- exit(1);
- }
- }
- else if( version[0] == '2' && version[1] == '0' )
- {
- // Read texture type
- read_big_endian_2byte_word(&texture_type, f);
- if(texture_type==ETC2PACKAGE_RG_SIGNED_NO_MIPMAPS)
- {
- texture_type=ETC2PACKAGE_RG_NO_MIPMAPS;
- formatSigned=1;
- //printf("Decompressing 2-channel signed data\n");
- }
- if(texture_type==ETC2PACKAGE_R_SIGNED_NO_MIPMAPS)
- {
- texture_type=ETC2PACKAGE_R_NO_MIPMAPS;
- formatSigned=1;
- //printf("Decompressing 1-channel signed data\n");
- }
- if(texture_type==ETC2PACKAGE_sRGB_NO_MIPMAPS)
- {
- // The SRGB formats are decoded just as RGB formats -- use RGB format for decompression.
- texture_type=ETC2PACKAGE_RGB_NO_MIPMAPS;
- }
- if(texture_type==ETC2PACKAGE_sRGBA_NO_MIPMAPS)
- {
- // The SRGB formats are decoded just as RGB formats -- use RGB format for decompression.
- texture_type=ETC2PACKAGE_RGBA_NO_MIPMAPS;
- }
- if(texture_type==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- // The SRGB formats are decoded just as RGB formats -- use RGB format for decompression.
- texture_type=ETC2PACKAGE_sRGBA1_NO_MIPMAPS;
- }
- if(texture_type==ETC2PACKAGE_RGBA_NO_MIPMAPS_OLD)
- {
- printf("\n\nThe file %s contains a compressed texture created using an old version of ETCPACK.\n",srcfile);
- printf("decompression is not supported in this version.\n");
- exit(1);
- }
- if(!(texture_type==ETC2PACKAGE_RGB_NO_MIPMAPS||texture_type==ETC2PACKAGE_sRGB_NO_MIPMAPS||texture_type==ETC2PACKAGE_RGBA_NO_MIPMAPS||texture_type==ETC2PACKAGE_sRGBA_NO_MIPMAPS||texture_type==ETC2PACKAGE_R_NO_MIPMAPS||texture_type==ETC2PACKAGE_RG_NO_MIPMAPS||texture_type==ETC2PACKAGE_RGBA1_NO_MIPMAPS||texture_type==ETC2PACKAGE_sRGBA1_NO_MIPMAPS))
- {
- printf("\n\n The file %s does not contain a texture of known format.\n", srcfile);
- printf("Known formats: ETC2PACKAGE_RGB_NO_MIPMAPS.\n", srcfile);
- exit(1);
- }
- }
- else
- {
- printf("\n\n The file %s is not of version 1.0 or 2.0 but of version %c.%c.\n",srcfile, version[0], version[1]);
- printf("Aborting.\n");
- exit(1);
- }
- format=texture_type;
- printf("textype: %d\n",texture_type);
- // ETC2 is backwards compatible, which means that an ETC2-capable decompressor can also handle
- // old ETC1 textures without any problems. Thus a version 1.0 file with ETC1_RGB_NO_MIPMAPS and a
- // version 2.0 file with ETC2PACKAGE_RGB_NO_MIPMAPS can be handled by the same ETC2-capable decompressor
- // Read how many pixels the blocks make up
- read_big_endian_2byte_word(&w, f);
- read_big_endian_2byte_word(&h, f);
- width = w;
- height = h;
- // Read how many pixels contain active data (the rest are just
- // for making sure we have a 4*a x 4*b size).
- read_big_endian_2byte_word(&w, f);
- read_big_endian_2byte_word(&h, f);
- active_width = w;
- active_height = h;
- }
- printf("Width = %d, Height = %d\n",width, height);
- printf("active pixel area: top left %d x %d area.\n",active_width, active_height);
- if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- img=(uint8*)malloc(3*width*height*2);
- else
- img=(uint8*)malloc(3*width*height);
- if(!img)
- {
- printf("Error: could not allocate memory\n");
- exit(0);
- }
- if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_R_NO_MIPMAPS||format==ETC2PACKAGE_RG_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- //printf("alpha channel decompression\n");
- alphaimg=(uint8*)malloc(width*height*2);
- setupAlphaTableAndValtab();
- if(!alphaimg)
- {
- printf("Error: could not allocate memory for alpha\n");
- exit(0);
- }
- }
- if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- alphaimg2=(uint8*)malloc(width*height*2);
- if(!alphaimg2)
- {
- printf("Error: could not allocate memory\n");
- exit(0);
- }
- }
- for(int y=0;y<height/4;y++)
- {
- for(int x=0;x<width/4;x++)
- {
- //decode alpha channel for RGBA
- if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS)
- {
- uint8 alphablock[8];
- fread(alphablock,1,8,f);
- decompressBlockAlpha(alphablock,alphaimg,width,height,4*x,4*y);
- }
- //color channels for most normal modes
- if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- //we have normal ETC2 color channels, decompress these
- read_big_endian_4byte_word(&block_part1,f);
- read_big_endian_4byte_word(&block_part2,f);
- if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- decompressBlockETC21BitAlpha(block_part1, block_part2,img,alphaimg,width,height,4*x,4*y);
- else
- decompressBlockETC2(block_part1, block_part2,img,width,height,4*x,4*y);
- }
- //one or two 11-bit alpha channels for R or RG.
- if(format==ETC2PACKAGE_R_NO_MIPMAPS||format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- uint8 alphablock[8];
- fread(alphablock,1,8,f);
- decompressBlockAlpha16bit(alphablock,alphaimg,width,height,4*x,4*y);
- }
- if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- uint8 alphablock[8];
- fread(alphablock,1,8,f);
- decompressBlockAlpha16bit(alphablock,alphaimg2,width,height,4*x,4*y);
- }
- }
- }
- if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- for(int y=0;y<height;y++)
- {
- for(int x=0;x<width;x++)
- {
- img[6*(y*width+x)]=alphaimg[2*(y*width+x)];
- img[6*(y*width+x)+1]=alphaimg[2*(y*width+x)+1];
- img[6*(y*width+x)+2]=alphaimg2[2*(y*width+x)];
- img[6*(y*width+x)+3]=alphaimg2[2*(y*width+x)+1];
- img[6*(y*width+x)+4]=0;
- img[6*(y*width+x)+5]=0;
- }
- }
- }
- // Ok, and now only write out the active pixels to the .ppm file.
- // (But only if the active pixels differ from the total pixels)
- if( !(height == active_height && width == active_width) )
- {
- if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- newimg=(uint8*)malloc(3*active_width*active_height*2);
- else
- newimg=(uint8*)malloc(3*active_width*active_height);
-
- if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_R_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- newalphaimg = (uint8*)malloc(active_width*active_height*2);
- }
- if(!newimg)
- {
- printf("Error: could not allocate memory\n");
- exit(0);
- }
-
- // Convert from total area to active area:
- for(yy = 0; yy<active_height; yy++)
- {
- for(xx = 0; xx< active_width; xx++)
- {
- if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- newimg[ (yy*active_width)*3 + xx*3 + 0 ] = img[ (yy*width)*3 + xx*3 + 0];
- newimg[ (yy*active_width)*3 + xx*3 + 1 ] = img[ (yy*width)*3 + xx*3 + 1];
- newimg[ (yy*active_width)*3 + xx*3 + 2 ] = img[ (yy*width)*3 + xx*3 + 2];
- }
- else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- newimg[ (yy*active_width)*6 + xx*6 + 0 ] = img[ (yy*width)*6 + xx*6 + 0];
- newimg[ (yy*active_width)*6 + xx*6 + 1 ] = img[ (yy*width)*6 + xx*6 + 1];
- newimg[ (yy*active_width)*6 + xx*6 + 2 ] = img[ (yy*width)*6 + xx*6 + 2];
- newimg[ (yy*active_width)*6 + xx*6 + 3 ] = img[ (yy*width)*6 + xx*6 + 3];
- newimg[ (yy*active_width)*6 + xx*6 + 4 ] = img[ (yy*width)*6 + xx*6 + 4];
- newimg[ (yy*active_width)*6 + xx*6 + 5 ] = img[ (yy*width)*6 + xx*6 + 5];
- }
- if(format==ETC2PACKAGE_R_NO_MIPMAPS)
- {
- newalphaimg[ ((yy*active_width) + xx)*2] = alphaimg[2*((yy*width) + xx)];
- newalphaimg[ ((yy*active_width) + xx)*2+1] = alphaimg[2*((yy*width) + xx)+1];
- }
- if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- newalphaimg[ ((yy*active_width) + xx)] = alphaimg[((yy*width) + xx)];
- }
- }
- }
- free(img);
- img = newimg;
- if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_R_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- free(alphaimg);
- alphaimg=newalphaimg;
- }
- if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- free(alphaimg);
- free(alphaimg2);
- alphaimg = NULL;
- alphaimg2 = NULL;
- }
- }
- }
- else
- {
- printf("Error: could not open <%s>.\n",srcfile);
- exit(1);
- }
- height=active_height;
- width=active_width;
- fclose(f);
- }
- // Writes output file
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void writeOutputFile(char *dstfile, uint8* img, uint8* alphaimg, int width, int height)
- {
- char str[300];
- if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- fWritePPM("tmp.ppm",width,height,img,8,false);
- printf("Saved file tmp.ppm \n\n");
- }
- else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- fWritePPM("tmp.ppm",width,height,img,16,false);
- }
- if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- fWritePGM("alphaout.pgm",width,height,alphaimg,false,8);
- if(format==ETC2PACKAGE_R_NO_MIPMAPS)
- fWritePGM("alphaout.pgm",width,height,alphaimg,false,16);
- // Delete destination file if it exists
- if(fileExist(dstfile))
- {
- sprintf(str, "del %s\n",dstfile);
- system(str);
- }
- int q = find_pos_of_extension(dstfile);
- if(!strcmp(&dstfile[q],".ppm")&&format!=ETC2PACKAGE_R_NO_MIPMAPS)
- {
- // Already a .ppm file. Just rename.
- sprintf(str,"move tmp.ppm %s\n",dstfile);
- printf("Renaming destination file to %s\n",dstfile);
- }
- else
- {
- // Converting from .ppm to other file format
- //
- // Use your favorite command line image converter program,
- // for instance Image Magick. Just make sure the syntax can
- // be written as below:
- //
- // C:\magick convert source.ppm dest.jpg
- //
- if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- // Somewhere after version 6.7.1-2 of ImageMagick the following command gives the wrong result due to a bug.
- // sprintf(str,"composite -compose CopyOpacity alphaout.pgm tmp.ppm %s\n",dstfile);
- // Instead we read the file and write a tga.
- printf("Converting destination file from .ppm/.pgm to %s with alpha\n",dstfile);
- int rw, rh;
- unsigned char *pixelsRGB;
- unsigned char *pixelsA;
- fReadPPM("tmp.ppm", rw, rh, pixelsRGB, 8);
- fReadPGM("alphaout.pgm", rw, rh, pixelsA, 8);
- fWriteTGAfromRGBandA(dstfile, rw, rh, pixelsRGB, pixelsA, true);
- free(pixelsRGB);
- free(pixelsA);
- sprintf(str,""); // Nothing to execute.
- }
- else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
- {
- sprintf(str,"magick convert alphaout.pgm %s\n",dstfile);
- printf("Converting destination file from .pgm to %s\n",dstfile);
- }
- else
- {
- sprintf(str,"magick convert tmp.ppm %s\n",dstfile);
- printf("Converting destination file from .ppm to %s\n",dstfile);
- }
- }
- // Execute system call
- system(str);
-
- free(img);
- if(alphaimg!=NULL)
- free(alphaimg);
- }
- // Calculates the PSNR between two files
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calculatePSNRfile(char *srcfile, uint8 *origimg, uint8* origalpha)
- {
- uint8 *alphaimg, *img;
- int active_width, active_height;
- uncompressFile(srcfile,img,alphaimg,active_width,active_height);
- // calculate Mean Square Error (MSE)
- double MSER=0,MSEG=0,MSEB=0,MSEA, PSNRR,PSNRG,PSNRA;
- double MSE;
- double wMSE;
- double PSNR=0;
- double wPSNR;
- double err;
- MSE = 0;
- MSEA=0;
- wMSE = 0;
- int width=((active_width+3)/4)*4;
- int height=((active_height+3)/4)*4;
- int numpixels = 0;
- for(int y=0;y<active_height;y++)
- {
- for(int x=0;x<active_width;x++)
- {
- if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- //we have regular color channels..
- if((format != ETC2PACKAGE_RGBA1_NO_MIPMAPS && format != ETC2PACKAGE_sRGBA1_NO_MIPMAPS) || alphaimg[y*width + x] > 0)
- {
- err = img[y*active_width*3+x*3+0] - origimg[y*width*3+x*3+0];
- MSE += ((err * err)/3.0);
- wMSE += PERCEPTUAL_WEIGHT_R_SQUARED * (err*err);
- err = img[y*active_width*3+x*3+1] - origimg[y*width*3+x*3+1];
- MSE += ((err * err)/3.0);
- wMSE += PERCEPTUAL_WEIGHT_G_SQUARED * (err*err);
- err = img[y*active_width*3+x*3+2] - origimg[y*width*3+x*3+2];
- MSE += ((err * err)/3.0);
- wMSE += PERCEPTUAL_WEIGHT_B_SQUARED * (err*err);
- numpixels++;
- }
- }
- else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- int rorig = (origimg[6*(y*width+x)+0]<<8)+origimg[6*(y*width+x)+1];
- int rnew = ( img[6*(y*active_width+x)+0]<<8)+ img[6*(y*active_width+x)+1];
- int gorig = (origimg[6*(y*width+x)+2]<<8)+origimg[6*(y*width+x)+3];
- int gnew = ( img[6*(y*active_width+x)+2]<<8)+ img[6*(y*active_width+x)+3];
- err=rorig-rnew;
- MSER+=(err*err);
- err=gorig-gnew;
- MSEG+=(err*err);
- }
- else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
- {
- int aorig = (((int)origalpha[2*(y*width+x)+0])<<8)+origalpha[2*(y*width+x)+1];
- int anew = (((int)alphaimg[2*(y*active_width+x)+0])<<8)+alphaimg[2*(y*active_width+x)+1];
- err=aorig-anew;
- MSEA+=(err*err);
- }
- }
- }
- if(format == ETC2PACKAGE_RGBA1_NO_MIPMAPS || format == ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- MSE = MSE / (1.0 * numpixels);
- wMSE = wMSE / (1.0 * numpixels);
- PSNR = 10*log((1.0*255*255)/MSE)/log(10.0);
- wPSNR = 10*log((1.0*255*255)/wMSE)/log(10.0);
- printf("PSNR only calculated on pixels where compressed alpha > 0\n");
- printf("color PSNR: %lf\nweighted PSNR: %lf\n",PSNR,wPSNR);
- }
- else if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- MSE = MSE / (active_width * active_height);
- wMSE = wMSE / (active_width * active_height);
- PSNR = 10*log((1.0*255*255)/MSE)/log(10.0);
- wPSNR = 10*log((1.0*255*255)/wMSE)/log(10.0);
- if(format == ETC2PACKAGE_RGBA_NO_MIPMAPS || format == ETC2PACKAGE_sRGBA_NO_MIPMAPS)
- printf("PSNR only calculated on RGB, not on alpha\n");
- printf("color PSNR: %lf\nweighted PSNR: %lf\n",PSNR,wPSNR);
- }
- else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- MSER = MSER / (active_width * active_height);
- MSEG = MSEG / (active_width * active_height);
- PSNRR = 10*log((1.0*65535*65535)/MSER)/log(10.0);
- PSNRG = 10*log((1.0*65535*65535)/MSEG)/log(10.0);
- printf("red PSNR: %lf\ngreen PSNR: %lf\n",PSNRR,PSNRG);
- }
- else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
- {
- MSEA = MSEA / (active_width * active_height);
- PSNRA = 10*log((1.0*65535.0*65535.0)/MSEA)/log(10.0);
- printf("PSNR: %lf\n",PSNRA);
- }
- free(img);
- return PSNR;
- }
- //// Exhaustive code starts here.
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precomutes a table that is used when compressing a block exhaustively
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- inline unsigned int precompute_3bittable_all_subblocksRG_withtest_perceptual1000(uint8 *block,uint8 *avg_color, unsigned int *precalc_err_UL_R, unsigned int *precalc_err_UR_R, unsigned int *precalc_err_LL_R, unsigned int *precalc_err_LR_R,unsigned int *precalc_err_UL_RG, unsigned int *precalc_err_UR_RG, unsigned int *precalc_err_LL_RG, unsigned int *precalc_err_LR_RG, unsigned int best_err)
- {
- int table;
- int index;
- int orig[3],approx[3][4];
- int x;
- int intensity_modifier;
- const int *table_indices;
- int good_enough_to_test;
- unsigned int err[4];
- unsigned int err_this_table_upper;
- unsigned int err_this_table_lower;
- unsigned int err_this_table_left;
- unsigned int err_this_table_right;
- // If the error in the red and green component is already larger than best_err for all 8 tables in
- // all of upper, lower, left and right, this combination of red and green will never be used in
- // the optimal color configuration. Therefore we can avoid testing all the blue colors for this
- // combination.
- good_enough_to_test = false;
- for(table=0;table<8;table++) // try all the 8 tables.
- {
- table_indices = &compressParamsFast[table*4];
- intensity_modifier = table_indices[0];
- approx[1][0]=CLAMP(0, avg_color[1]+intensity_modifier,255);
- intensity_modifier = table_indices[1];
- approx[1][1]=CLAMP(0, avg_color[1]+intensity_modifier,255);
- intensity_modifier = table_indices[2];
- approx[1][2]=CLAMP(0, avg_color[1]+intensity_modifier,255);
- intensity_modifier = table_indices[3];
- approx[1][3]=CLAMP(0, avg_color[1]+intensity_modifier,255);
- err_this_table_upper = 0;
- err_this_table_lower = 0;
- err_this_table_left = 0;
- err_this_table_right = 0;
- for(x=0; x<4; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index] = precalc_err_UL_R[table*4*4+x*4+index]
- + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000 * SQUARE(approx[1][index]-orig[1]);
- precalc_err_UL_RG[table*4*4+x*4+index] = err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_upper+=err[0];
- err_this_table_left+=err[0];
- }
- for(x=4; x<8; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index] = precalc_err_UR_R[table*4*4+(x-4)*4+index]
- + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000 * SQUARE(approx[1][index]-orig[1]);
- precalc_err_UR_RG[table*4*4+(x-4)*4+index] = err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_upper+=err[0];
- err_this_table_right+=err[0];
- }
- for(x=8; x<12; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index] = precalc_err_LL_R[table*4*4+(x-8)*4+index]
- + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000 * SQUARE(approx[1][index]-orig[1]);
- precalc_err_LL_RG[table*4*4+(x-8)*4+index] = err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_lower+=err[0];
- err_this_table_left+=err[0];
- }
- for(x=12; x<16; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index] = precalc_err_LR_R[table*4*4+(x-12)*4+index]
- + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000 * SQUARE(approx[1][index]-orig[1]);
- precalc_err_LR_RG[table*4*4+(x-12)*4+index] = err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_lower+=err[0];
- err_this_table_right+=err[0];
- }
- if(err_this_table_upper < best_err)
- good_enough_to_test = true;
- if(err_this_table_lower < best_err)
- good_enough_to_test = true;
- if(err_this_table_left < best_err)
- good_enough_to_test = true;
- if(err_this_table_right < best_err)
- good_enough_to_test = true;
- }
- return good_enough_to_test;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precomutes a table that is used when compressing a block exhaustively
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- inline int precompute_3bittable_all_subblocksRG_withtest(uint8 *block,uint8 *avg_color, unsigned int *precalc_err_UL_R, unsigned int *precalc_err_UR_R, unsigned int *precalc_err_LL_R, unsigned int *precalc_err_LR_R,unsigned int *precalc_err_UL_RG, unsigned int *precalc_err_UR_RG, unsigned int *precalc_err_LL_RG, unsigned int *precalc_err_LR_RG, unsigned int best_err)
- {
- int table;
- int index;
- int orig[3],approx[3][4];
- int x;
- int intensity_modifier;
- const int *table_indices;
- int good_enough_to_test;
- unsigned int err[4];
- unsigned int err_this_table_upper;
- unsigned int err_this_table_lower;
- unsigned int err_this_table_left;
- unsigned int err_this_table_right;
- // If the error in the red and green component is already larger than best_err for all 8 tables in
- // all of upper, lower, left and right, this combination of red and green will never be used in
- // the optimal color configuration. Therefore we can avoid testing all the blue colors for this
- // combination.
- good_enough_to_test = false;
- for(table=0;table<8;table++) // try all the 8 tables.
- {
- table_indices = &compressParamsFast[table*4];
- intensity_modifier = table_indices[0];
- approx[1][0]=CLAMP(0, avg_color[1]+intensity_modifier,255);
- intensity_modifier = table_indices[1];
- approx[1][1]=CLAMP(0, avg_color[1]+intensity_modifier,255);
- intensity_modifier = table_indices[2];
- approx[1][2]=CLAMP(0, avg_color[1]+intensity_modifier,255);
- intensity_modifier = table_indices[3];
- approx[1][3]=CLAMP(0, avg_color[1]+intensity_modifier,255);
- err_this_table_upper = 0;
- err_this_table_lower = 0;
- err_this_table_left = 0;
- err_this_table_right = 0;
- for(x=0; x<4; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index] = precalc_err_UL_R[table*4*4+x*4+index]+SQUARE(approx[1][index]-orig[1]);
- precalc_err_UL_RG[table*4*4+x*4+index] = err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_upper+=err[0];
- err_this_table_left+=err[0];
- }
- for(x=4; x<8; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index] = precalc_err_UR_R[table*4*4+(x-4)*4+index]+SQUARE(approx[1][index]-orig[1]);
- precalc_err_UR_RG[table*4*4+(x-4)*4+index] = err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_upper+=err[0];
- err_this_table_right+=err[0];
- }
- for(x=8; x<12; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index] = precalc_err_LL_R[table*4*4+(x-8)*4+index]+SQUARE(approx[1][index]-orig[1]);
- precalc_err_LL_RG[table*4*4+(x-8)*4+index] = err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_lower+=err[0];
- err_this_table_left+=err[0];
- }
- for(x=12; x<16; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index] = precalc_err_LR_R[table*4*4+(x-12)*4+index]+SQUARE(approx[1][index]-orig[1]);
- precalc_err_LR_RG[table*4*4+(x-12)*4+index] = err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_lower+=err[0];
- err_this_table_right+=err[0];
- }
- if(err_this_table_upper < best_err)
- good_enough_to_test = true;
- if(err_this_table_lower < best_err)
- good_enough_to_test = true;
- if(err_this_table_left < best_err)
- good_enough_to_test = true;
- if(err_this_table_right < best_err)
- good_enough_to_test = true;
- }
- return good_enough_to_test;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precomutes a table that is used when compressing a block exhaustively
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- inline unsigned int precompute_3bittable_all_subblocksR_with_test_perceptual1000(uint8 *block,uint8 *avg_color, unsigned int *precalc_err_UL_R, unsigned int *precalc_err_UR_R, unsigned int *precalc_err_LL_R, unsigned int *precalc_err_LR_R, unsigned int best_err)
- {
- int table;
- int index;
- int orig[3],approx[3][4];
- int x;
- int intensity_modifier;
- const int *table_indices;
- unsigned int err[4];
- unsigned int err_this_table_upper;
- unsigned int err_this_table_lower;
- unsigned int err_this_table_left;
- unsigned int err_this_table_right;
- int good_enough_to_test;
- good_enough_to_test = false;
- for(table=0;table<8;table++) // try all the 8 tables.
- {
- err_this_table_upper = 0;
- err_this_table_lower = 0;
- err_this_table_left = 0;
- err_this_table_right = 0;
- table_indices = &compressParamsFast[table*4];
- intensity_modifier = table_indices[0];
- approx[0][0]=CLAMP(0, avg_color[0]+intensity_modifier,255);
- intensity_modifier = table_indices[1];
- approx[0][1]=CLAMP(0, avg_color[0]+intensity_modifier,255);
- intensity_modifier = table_indices[2];
- approx[0][2]=CLAMP(0, avg_color[0]+intensity_modifier,255);
- intensity_modifier = table_indices[3];
- approx[0][3]=CLAMP(0, avg_color[0]+intensity_modifier,255);
- for(x=0; x<4; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index]=PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(approx[0][index]-orig[0]);
- precalc_err_UL_R[table*4*4+x*4+index]=err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_upper+=err[0];
- err_this_table_left+=err[0];
- }
- for(x=4; x<8; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index]=PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(approx[0][index]-orig[0]);
- precalc_err_UR_R[table*4*4+(x-4)*4+index]=err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_upper+=err[0];
- err_this_table_right+=err[0];
- }
- for(x=8; x<12; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index]=PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(approx[0][index]-orig[0]);
- precalc_err_LL_R[table*4*4+(x-8)*4+index]=err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_lower+=err[0];
- err_this_table_left+=err[0];
-
- }
- for(x=12; x<16; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index]=PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(approx[0][index]-orig[0]);
- precalc_err_LR_R[table*4*4+(x-12)*4+index]=err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_lower+=err[0];
- err_this_table_right+=err[0];
- }
- if(err_this_table_upper < best_err)
- good_enough_to_test = true;
- if(err_this_table_lower < best_err)
- good_enough_to_test = true;
- if(err_this_table_left < best_err)
- good_enough_to_test = true;
- if(err_this_table_right < best_err)
- good_enough_to_test = true;
- }
- return good_enough_to_test;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precomutes a table that is used when compressing a block exhaustively
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- inline int precompute_3bittable_all_subblocksR_with_test(uint8 *block,uint8 *avg_color, unsigned int *precalc_err_UL_R, unsigned int *precalc_err_UR_R, unsigned int *precalc_err_LL_R, unsigned int *precalc_err_LR_R, unsigned int best_err)
- {
- int table;
- int index;
- int orig[3],approx[3][4];
- int x;
- int intensity_modifier;
- const int *table_indices;
- unsigned int err[4];
- unsigned int err_this_table_upper;
- unsigned int err_this_table_lower;
- unsigned int err_this_table_left;
- unsigned int err_this_table_right;
- int good_enough_to_test;
- good_enough_to_test = false;
- for(table=0;table<8;table++) // try all the 8 tables.
- {
- err_this_table_upper = 0;
- err_this_table_lower = 0;
- err_this_table_left = 0;
- err_this_table_right = 0;
- table_indices = &compressParamsFast[table*4];
- intensity_modifier = table_indices[0];
- approx[0][0]=CLAMP(0, avg_color[0]+intensity_modifier,255);
- intensity_modifier = table_indices[1];
- approx[0][1]=CLAMP(0, avg_color[0]+intensity_modifier,255);
- intensity_modifier = table_indices[2];
- approx[0][2]=CLAMP(0, avg_color[0]+intensity_modifier,255);
- intensity_modifier = table_indices[3];
- approx[0][3]=CLAMP(0, avg_color[0]+intensity_modifier,255);
- for(x=0; x<4; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index]=SQUARE(approx[0][index]-orig[0]);
- precalc_err_UL_R[table*4*4+x*4+index]=err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_upper+=err[0];
- err_this_table_left+=err[0];
- }
- for(x=4; x<8; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index]=SQUARE(approx[0][index]-orig[0]);
- precalc_err_UR_R[table*4*4+(x-4)*4+index]=err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_upper+=err[0];
- err_this_table_right+=err[0];
- }
- for(x=8; x<12; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index]=SQUARE(approx[0][index]-orig[0]);
- precalc_err_LL_R[table*4*4+(x-8)*4+index]=err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_lower+=err[0];
- err_this_table_left+=err[0];
-
- }
- for(x=12; x<16; x++)
- {
- orig[0]=block[x*4];
- orig[1]=block[x*4+1];
- orig[2]=block[x*4+2];
- for(index=0;index<4;index++)
- {
- err[index]=SQUARE(approx[0][index]-orig[0]);
- precalc_err_LR_R[table*4*4+(x-12)*4+index]=err[index];
- }
- if(err[0] > err[1])
- err[0] = err[1];
- if(err[2] > err[3])
- err[2] = err[3];
- if(err[0] > err[2])
- err[0] = err[2];
- err_this_table_lower+=err[0];
- err_this_table_right+=err[0];
- }
- if(err_this_table_upper < best_err)
- good_enough_to_test = true;
- if(err_this_table_lower < best_err)
- good_enough_to_test = true;
- if(err_this_table_left < best_err)
- good_enough_to_test = true;
- if(err_this_table_right < best_err)
- good_enough_to_test = true;
- }
- return good_enough_to_test;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Tries all index-tables, used when compressing a block exhaustively
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- inline void tryalltables_3bittable_all_subblocks_using_precalc(uint8 *block_2x2,uint8 *color_quant1, unsigned int *precalc_err_UL_RG, unsigned int *precalc_err_UR_RG, unsigned int *precalc_err_LL_RG, unsigned int *precalc_err_LR_RG, unsigned int &err_upper, unsigned int &err_lower, unsigned int &err_left, unsigned int &err_right, unsigned int best_err)
- {
- unsigned int err_this_table_upper;
- unsigned int err_this_table_lower;
- unsigned int err_this_table_left;
- unsigned int err_this_table_right;
- int orig[3],approx[4];
- int err[4];
- err_upper = 3*255*255*16;
- err_lower = 3*255*255*16;
- err_left = 3*255*255*16;
- err_right = 3*255*255*16;
- #define ONE_PIXEL_UL(table_nbr,xx)\
- orig[0]=block_2x2[xx*4];\
- orig[1]=block_2x2[xx*4+1];\
- orig[2]=block_2x2[xx*4+2];\
- /* unrolled loop for(index=0;index<4;index++)*/\
- err[0]=precalc_err_UL_RG[table_nbr*4*4+xx*4+0] + square_table[approx[0]-orig[2]];\
- err[1]=precalc_err_UL_RG[table_nbr*4*4+xx*4+1] + square_table[approx[1]-orig[2]];\
- err[2]=precalc_err_UL_RG[table_nbr*4*4+xx*4+2] + square_table[approx[2]-orig[2]];\
- err[3]=precalc_err_UL_RG[table_nbr*4*4+xx*4+3] + square_table[approx[3]-orig[2]];\
- /* end unrolled loop*/\
- if(err[0] > err[1])\
- err[0] = err[1];\
- if(err[2] > err[3])\
- err[2] = err[3];\
- if(err[0] > err[2])\
- err[0] = err[2];\
- err_this_table_upper+=err[0];\
- err_this_table_left+=err[0];\
- #define ONE_PIXEL_UR(table_nbr,xx)\
- orig[0]=block_2x2[xx*4];\
- orig[1]=block_2x2[xx*4+1];\
- orig[2]=block_2x2[xx*4+2];\
- /* unrolled loop for(index=0;index<4;index++)*/\
- err[0]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+0] + square_table[approx[0]-orig[2]];\
- err[1]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+1] + square_table[approx[1]-orig[2]];\
- err[2]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+2] + square_table[approx[2]-orig[2]];\
- err[3]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+3] + square_table[approx[3]-orig[2]];\
- /* end unrolled loop */\
- if(err[0] > err[1])\
- err[0] = err[1];\
- if(err[2] > err[3])\
- err[2] = err[3];\
- if(err[0] > err[2])\
- err[0] = err[2];\
- err_this_table_upper+=err[0];\
- err_this_table_right+=err[0];
- #define ONE_PIXEL_LL(table_nbr,xx)\
- orig[0]=block_2x2[xx*4];\
- orig[1]=block_2x2[xx*4+1];\
- orig[2]=block_2x2[xx*4+2];\
- /* unrolled loop for(index=0;index<4;index++)*/\
- err[0]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+0] + square_table[approx[0]-orig[2]];\
- err[1]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+1] + square_table[approx[1]-orig[2]];\
- err[2]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+2] + square_table[approx[2]-orig[2]];\
- err[3]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+3] + square_table[approx[3]-orig[2]];\
- /* end unrolled loop*/\
- if(err[0] > err[1])\
- err[0] = err[1];\
- if(err[2] > err[3])\
- err[2] = err[3];\
- if(err[0] > err[2])\
- err[0] = err[2];\
- err_this_table_lower+=err[0];\
- err_this_table_left+=err[0];\
- #define ONE_PIXEL_LR(table_nbr,xx)\
- orig[0]=block_2x2[xx*4];\
- orig[1]=block_2x2[xx*4+1];\
- orig[2]=block_2x2[xx*4+2];\
- /* unrolled loop for(index=0;index<4;index++)*/\
- err[0]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+0] + square_table[approx[0]-orig[2]];\
- err[1]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+1] + square_table[approx[1]-orig[2]];\
- err[2]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+2] + square_table[approx[2]-orig[2]];\
- err[3]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+3] + square_table[approx[3]-orig[2]];\
- /* end unrolled loop*/\
- if(err[0] > err[1])\
- err[0] = err[1];\
- if(err[2] > err[3])\
- err[2] = err[3];\
- if(err[0] > err[2])\
- err[0] = err[2];\
- err_this_table_lower+=err[0];\
- err_this_table_right+=err[0];\
- #define ONE_TABLE_3(table_nbr)\
- err_this_table_upper = 0;\
- err_this_table_lower = 0;\
- err_this_table_left = 0;\
- err_this_table_right = 0;\
- approx[0]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+0]+255];\
- approx[1]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+1]+255];\
- approx[2]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+2]+255];\
- approx[3]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+3]+255];\
- /* unroll loop for(xx=0; xx<4; xx++) */\
- ONE_PIXEL_UL(table_nbr,0)\
- ONE_PIXEL_UL(table_nbr,1)\
- ONE_PIXEL_UL(table_nbr,2)\
- ONE_PIXEL_UL(table_nbr,3)\
- /* end unroll loop */\
- /* unroll loop for(xx=4; xx<8; xx++) */\
- ONE_PIXEL_LR(table_nbr,12)\
- ONE_PIXEL_LR(table_nbr,13)\
- ONE_PIXEL_LR(table_nbr,14)\
- ONE_PIXEL_LR(table_nbr,15)\
- /* end unroll loop */\
- /* If error in the top left 2x2 pixel area is already larger than the best error, and */\
- /* The same is true for the bottom right 2x2 pixel area, this combination of table and color */\
- /* can never be part of an optimal solution and therefore we do not need to test the other */\
- /* two 2x2 pixel areas */\
- if((err_this_table_upper<best_err)||(err_this_table_lower<best_err))\
- {\
- /* unroll loop for(xx=4; xx<8; xx++) */\
- ONE_PIXEL_UR(table_nbr,4)\
- ONE_PIXEL_UR(table_nbr,5)\
- ONE_PIXEL_UR(table_nbr,6)\
- ONE_PIXEL_UR(table_nbr,7)\
- /* end unroll loop */\
- /* unroll loop for(xx=4; xx<8; xx++) */\
- ONE_PIXEL_LL(table_nbr,8)\
- ONE_PIXEL_LL(table_nbr,9)\
- ONE_PIXEL_LL(table_nbr,10)\
- ONE_PIXEL_LL(table_nbr,11)\
- /* end unroll loop */\
- if(err_this_table_upper<err_upper)\
- err_upper = err_this_table_upper;\
- if(err_this_table_lower<err_lower)\
- err_lower = err_this_table_lower;\
- if(err_this_table_left<err_left)\
- err_left = err_this_table_left;\
- if(err_this_table_right<err_right)\
- err_right = err_this_table_right;\
- }\
- /*unroll loop for(table_nbr=0;table_nbr<8;table_nbr++)*/
- ONE_TABLE_3(0);
- ONE_TABLE_3(1);
- ONE_TABLE_3(2);
- ONE_TABLE_3(3);
- ONE_TABLE_3(4);
- ONE_TABLE_3(5);
- ONE_TABLE_3(6);
- ONE_TABLE_3(7);
- /*end unroll loop*/
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Tries all index-tables, used when compressing a block exhaustively using perceptual error measure
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- inline void tryalltables_3bittable_all_subblocks_using_precalc_perceptual1000(uint8 *block_2x2,uint8 *color_quant1, unsigned int *precalc_err_UL_RG, unsigned int *precalc_err_UR_RG, unsigned int *precalc_err_LL_RG, unsigned int *precalc_err_LR_RG, unsigned int &err_upper, unsigned int &err_lower, unsigned int &err_left, unsigned int &err_right, unsigned int best_err)
- {
- unsigned int err_this_table_upper;
- unsigned int err_this_table_lower;
- unsigned int err_this_table_left;
- unsigned int err_this_table_right;
- int orig[3],approx[4];
- int err[4];
- err_upper = MAXERR1000;
- err_lower = MAXERR1000;
- err_left = MAXERR1000;
- err_right =MAXERR1000;
- #define ONE_PIXEL_UL_PERCEP(table_nbr,xx)\
- orig[0]=block_2x2[xx*4];\
- orig[1]=block_2x2[xx*4+1];\
- orig[2]=block_2x2[xx*4+2];\
- /* unrolled loop for(index=0;index<4;index++)*/\
- err[0]=precalc_err_UL_RG[table_nbr*4*4+xx*4+0] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[0]-orig[2]];\
- err[1]=precalc_err_UL_RG[table_nbr*4*4+xx*4+1] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[1]-orig[2]];\
- err[2]=precalc_err_UL_RG[table_nbr*4*4+xx*4+2] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[2]-orig[2]];\
- err[3]=precalc_err_UL_RG[table_nbr*4*4+xx*4+3] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[3]-orig[2]];\
- /* end unrolled loop*/\
- if(err[0] > err[1])\
- err[0] = err[1];\
- if(err[2] > err[3])\
- err[2] = err[3];\
- if(err[0] > err[2])\
- err[0] = err[2];\
- err_this_table_upper+=err[0];\
- err_this_table_left+=err[0];\
- #define ONE_PIXEL_UR_PERCEP(table_nbr,xx)\
- orig[0]=block_2x2[xx*4];\
- orig[1]=block_2x2[xx*4+1];\
- orig[2]=block_2x2[xx*4+2];\
- /* unrolled loop for(index=0;index<4;index++)*/\
- err[0]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+0] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[0]-orig[2]];\
- err[1]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+1] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[1]-orig[2]];\
- err[2]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+2] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[2]-orig[2]];\
- err[3]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+3] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[3]-orig[2]];\
- /* end unrolled loop */\
- if(err[0] > err[1])\
- err[0] = err[1];\
- if(err[2] > err[3])\
- err[2] = err[3];\
- if(err[0] > err[2])\
- err[0] = err[2];\
- err_this_table_upper+=err[0];\
- err_this_table_right+=err[0];
- #define ONE_PIXEL_LL_PERCEP(table_nbr,xx)\
- orig[0]=block_2x2[xx*4];\
- orig[1]=block_2x2[xx*4+1];\
- orig[2]=block_2x2[xx*4+2];\
- /* unrolled loop for(index=0;index<4;index++)*/\
- err[0]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+0] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[0]-orig[2]];\
- err[1]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+1] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[1]-orig[2]];\
- err[2]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+2] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[2]-orig[2]];\
- err[3]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+3] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[3]-orig[2]];\
- /* end unrolled loop*/\
- if(err[0] > err[1])\
- err[0] = err[1];\
- if(err[2] > err[3])\
- err[2] = err[3];\
- if(err[0] > err[2])\
- err[0] = err[2];\
- err_this_table_lower+=err[0];\
- err_this_table_left+=err[0];\
- #define ONE_PIXEL_LR_PERCEP(table_nbr,xx)\
- orig[0]=block_2x2[xx*4];\
- orig[1]=block_2x2[xx*4+1];\
- orig[2]=block_2x2[xx*4+2];\
- /* unrolled loop for(index=0;index<4;index++)*/\
- err[0]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+0] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[0]-orig[2]];\
- err[1]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+1] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[1]-orig[2]];\
- err[2]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+2] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[2]-orig[2]];\
- err[3]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+3] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[3]-orig[2]];\
- /* end unrolled loop*/\
- if(err[0] > err[1])\
- err[0] = err[1];\
- if(err[2] > err[3])\
- err[2] = err[3];\
- if(err[0] > err[2])\
- err[0] = err[2];\
- err_this_table_lower+=err[0];\
- err_this_table_right+=err[0];\
- #define ONE_TABLE_3_PERCEP(table_nbr)\
- err_this_table_upper = 0;\
- err_this_table_lower = 0;\
- err_this_table_left = 0;\
- err_this_table_right = 0;\
- approx[0]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+0]+255];\
- approx[1]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+1]+255];\
- approx[2]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+2]+255];\
- approx[3]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+3]+255];\
- /* unroll loop for(xx=0; xx<4; xx++) */\
- ONE_PIXEL_UL_PERCEP(table_nbr,0)\
- ONE_PIXEL_UL_PERCEP(table_nbr,1)\
- ONE_PIXEL_UL_PERCEP(table_nbr,2)\
- ONE_PIXEL_UL_PERCEP(table_nbr,3)\
- /* end unroll loop */\
- /* unroll loop for(xx=4; xx<8; xx++) */\
- ONE_PIXEL_LR_PERCEP(table_nbr,12)\
- ONE_PIXEL_LR_PERCEP(table_nbr,13)\
- ONE_PIXEL_LR_PERCEP(table_nbr,14)\
- ONE_PIXEL_LR_PERCEP(table_nbr,15)\
- /* end unroll loop */\
- /* If error in the top left 2x2 pixel area is already larger than the best error, and */\
- /* The same is true for the bottom right 2x2 pixel area, this combination of table and color */\
- /* can never be part of an optimal solution and therefore we do not need to test the other */\
- /* two 2x2 pixel areas */\
- if((err_this_table_upper<best_err)||(err_this_table_lower<best_err))\
- {\
- /* unroll loop for(xx=4; xx<8; xx++) */\
- ONE_PIXEL_UR_PERCEP(table_nbr,4)\
- ONE_PIXEL_UR_PERCEP(table_nbr,5)\
- ONE_PIXEL_UR_PERCEP(table_nbr,6)\
- ONE_PIXEL_UR_PERCEP(table_nbr,7)\
- /* end unroll loop */\
- /* unroll loop for(xx=4; xx<8; xx++) */\
- ONE_PIXEL_LL_PERCEP(table_nbr,8)\
- ONE_PIXEL_LL_PERCEP(table_nbr,9)\
- ONE_PIXEL_LL_PERCEP(table_nbr,10)\
- ONE_PIXEL_LL_PERCEP(table_nbr,11)\
- /* end unroll loop */\
- if(err_this_table_upper<err_upper)\
- err_upper = err_this_table_upper;\
- if(err_this_table_lower<err_lower)\
- err_lower = err_this_table_lower;\
- if(err_this_table_left<err_left)\
- err_left = err_this_table_left;\
- if(err_this_table_right<err_right)\
- err_right = err_this_table_right;\
- }\
- /*unroll loop for(table_nbr=0;table_nbr<8;table_nbr++)*/
- ONE_TABLE_3_PERCEP(0);
- ONE_TABLE_3_PERCEP(1);
- ONE_TABLE_3_PERCEP(2);
- ONE_TABLE_3_PERCEP(3);
- ONE_TABLE_3_PERCEP(4);
- ONE_TABLE_3_PERCEP(5);
- ONE_TABLE_3_PERCEP(6);
- ONE_TABLE_3_PERCEP(7);
- /*end unroll loop*/
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Compresses the individual mode exhaustively (perecptual error metric).
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockIndividualExhaustivePerceptual(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, unsigned int total_best_err)
- {
- unsigned int best_err_norm_diff = MAXERR1000;
- unsigned int best_err_norm_444 = MAXERR1000;
- unsigned int best_err_flip_diff = MAXERR1000;
- unsigned int best_err_flip_444 = MAXERR1000;
- uint8 color_quant1[3], color_quant2[3];
- int enc_color1[3];
- int best_enc_color1[3], best_enc_color2[3];
-
- int min_error=MAXERR1000;
- unsigned int best_pixel_indices1_MSB=0;
- unsigned int best_pixel_indices1_LSB=0;
- unsigned int best_pixel_indices2_MSB=0;
- unsigned int best_pixel_indices2_LSB=0;
- unsigned int pixel_indices1_MSB=0;
- unsigned int pixel_indices1_LSB=0;
- unsigned int pixel_indices2_MSB=0;
- unsigned int err_upper, err_lower;
- unsigned int err_left, err_right;
- unsigned int pixel_indices2_LSB=0;
- unsigned int best_err_upper = MAXERR1000;
- unsigned int best_err_lower = MAXERR1000;
- unsigned int best_err_left = MAXERR1000;
- unsigned int best_err_right = MAXERR1000;
- int best_upper_col[3];
- int best_lower_col[3];
- int best_left_col[3];
- int best_right_col[3];
- unsigned int table1=0, table2=0;
- unsigned int best_table1=0, best_table2=0;
- unsigned int precalc_err_UL_R[8*4*4];
- unsigned int precalc_err_UR_R[8*4*4];
- unsigned int precalc_err_LL_R[8*4*4];
- unsigned int precalc_err_LR_R[8*4*4];
- unsigned int precalc_err_UL_RG[8*4*4];
- unsigned int precalc_err_UR_RG[8*4*4];
- unsigned int precalc_err_LL_RG[8*4*4];
- unsigned int precalc_err_LR_RG[8*4*4];
- int diffbit;
- uint8 block_2x2[4*4*4];
- unsigned int best_err;
- int best_flip;
- int xx,yy,count = 0;
- // Reshuffle pixels so that the top left 2x2 pixels arrive first, then the top right 2x2 pixels etc. Also put use 4 bytes per pixel to make it 32-word aligned.
- for(xx = 0; xx<2; xx++)
- {
- for(yy=0; yy<2; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 2; xx<4; xx++)
- {
- for(yy=0; yy<2; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 0; xx<2; xx++)
- {
- for(yy=2; yy<4; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 2; xx<4; xx++)
- {
- for(yy=2; yy<4; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- unsigned int test1, test2;
- best_err = (unsigned int)compressBlockOnlyIndividualAveragePerceptual1000(img, width, height, startx, starty, test1, test2, best_enc_color1, best_enc_color2, best_flip, best_err_upper, best_err_lower, best_err_left, best_err_right, best_upper_col, best_lower_col, best_left_col, best_right_col);
- if(best_err < total_best_err)
- total_best_err = best_err;
- unsigned int tryblocks = 0;
- unsigned int allblocks = 0;
- int needtest;
- for(enc_color1[0]=0; enc_color1[0]<16; enc_color1[0]++)
- {
- color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0]);
- if(precompute_3bittable_all_subblocksR_with_test_perceptual1000(block_2x2, color_quant1, precalc_err_UL_R, precalc_err_UR_R, precalc_err_LL_R, precalc_err_LR_R, total_best_err))
- {
- for(enc_color1[1]=0; enc_color1[1]<16; enc_color1[1]++)
- {
- color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1]);
- if(precompute_3bittable_all_subblocksRG_withtest_perceptual1000(block_2x2, color_quant1, precalc_err_UL_R, precalc_err_UR_R, precalc_err_LL_R, precalc_err_LR_R, precalc_err_UL_RG, precalc_err_UR_RG, precalc_err_LL_RG, precalc_err_LR_RG, total_best_err))
- {
- needtest = false;
- for(enc_color1[2]=0; enc_color1[2]<16; enc_color1[2]++)
- {
- color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2]);
- tryalltables_3bittable_all_subblocks_using_precalc_perceptual1000(block_2x2, color_quant1, precalc_err_UL_RG, precalc_err_UR_RG, precalc_err_LL_RG, precalc_err_LR_RG, err_upper, err_lower, err_left, err_right, total_best_err);
- if(err_upper<best_err_upper)
- {
- best_err_upper = err_upper;
- best_upper_col[0] = enc_color1[0];
- best_upper_col[1] = enc_color1[1];
- best_upper_col[2] = enc_color1[2];
- needtest = true;
- }
- if(err_lower<best_err_lower)
- {
- best_err_lower = err_lower;
- best_lower_col[0] = enc_color1[0];
- best_lower_col[1] = enc_color1[1];
- best_lower_col[2] = enc_color1[2];
- needtest=true;
- }
- if(err_left<best_err_left)
- {
- best_err_left = err_left;
- best_left_col[0] = enc_color1[0];
- best_left_col[1] = enc_color1[1];
- best_left_col[2] = enc_color1[2];
- needtest=true;
- }
- if(err_right<best_err_right)
- {
- best_err_right = err_right;
- best_right_col[0] = enc_color1[0];
- best_right_col[1] = enc_color1[1];
- best_right_col[2] = enc_color1[2];
- needtest = true;
- }
- }
- if(needtest)
- {
- if(best_err_upper+best_err_lower < best_err_left+best_err_right)
- {
- best_err = best_err_upper+best_err_lower;
- if(best_err < total_best_err)
- total_best_err = best_err;
- }
- else
- {
- best_err = best_err_left+best_err_right;
- if(best_err < total_best_err)
- total_best_err = best_err;
- }
- }
- }
- }
- }
- }
-
- if(best_err_upper+best_err_lower < best_err_left+best_err_right)
- {
- best_flip = 1;
- best_enc_color1[0] = best_upper_col[0];
- best_enc_color1[1] = best_upper_col[1];
- best_enc_color1[2] = best_upper_col[2];
- best_enc_color2[0] = best_lower_col[0];
- best_enc_color2[1] = best_lower_col[1];
- best_enc_color2[2] = best_lower_col[2];
- best_err = best_err_upper+best_err_lower;
- if(best_err < total_best_err)
- total_best_err = best_err;
- }
- else
- {
- best_flip = 0;
- best_enc_color1[0] = best_left_col[0];
- best_enc_color1[1] = best_left_col[1];
- best_enc_color1[2] = best_left_col[2];
- best_enc_color2[0] = best_right_col[0];
- best_enc_color2[1] = best_right_col[1];
- best_enc_color2[2] = best_right_col[2];
- best_err = best_err_left+best_err_right;
- if(best_err < total_best_err)
- total_best_err = best_err;
- }
- color_quant1[0] = best_enc_color1[0] << 4 | (best_enc_color1[0]);
- color_quant1[1] = best_enc_color1[1] << 4 | (best_enc_color1[1]);
- color_quant1[2] = best_enc_color1[2] << 4 | (best_enc_color1[2]);
- if(best_flip == 0)
- tryalltables_3bittable2x4percep1000(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- else
- tryalltables_3bittable4x2percep1000(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- color_quant2[0] = best_enc_color2[0] << 4 | (best_enc_color2[0]);
- color_quant2[1] = best_enc_color2[1] << 4 | (best_enc_color2[1]);
- color_quant2[2] = best_enc_color2[2] << 4 | (best_enc_color2[2]);
- if(best_flip == 0)
- tryalltables_3bittable2x4percep1000(img,width,height,startx+2,starty,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- else
- tryalltables_3bittable4x2percep1000(img,width,height,startx,starty+2,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- diffbit = 1;
- compressed1 = 0;
- PUTBITSHIGH( compressed1, diffbit, 0, 33);
- PUTBITSHIGH( compressed1, best_enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1, best_enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1, best_enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1, best_enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1, best_enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1, best_enc_color2[2], 4, 43);
- PUTBITSHIGH( compressed1, best_table1, 3, 39);
- PUTBITSHIGH( compressed1, best_table2, 3, 36);
- PUTBITSHIGH( compressed1, best_flip, 1, 32);
- if(best_flip == 0)
- {
- compressed2 = 0;
- PUTBITS( compressed2, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2, (best_pixel_indices2_LSB ), 8, 15);
- }
- else
- {
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
- compressed2 = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- return best_err;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Compresses the individual mode exhaustively.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockIndividualExhaustive(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, unsigned int total_best_err)
- {
- unsigned int best_err_norm_diff = 255*255*16*3;
- unsigned int best_err_norm_444 = 255*255*16*3;
- unsigned int best_err_flip_diff = 255*255*16*3;
- unsigned int best_err_flip_444 = 255*255*16*3;
- uint8 color_quant1[3], color_quant2[3];
- int enc_color1[3];
- int best_enc_color1[3], best_enc_color2[3];
-
- int min_error=255*255*8*3;
- unsigned int best_pixel_indices1_MSB=0;
- unsigned int best_pixel_indices1_LSB=0;
- unsigned int best_pixel_indices2_MSB=0;
- unsigned int best_pixel_indices2_LSB=0;
- unsigned int pixel_indices1_MSB=0;
- unsigned int pixel_indices1_LSB=0;
- unsigned int pixel_indices2_MSB=0;
- unsigned int err_upper, err_lower;
- unsigned int err_left, err_right;
- unsigned int pixel_indices2_LSB=0;
- unsigned int best_err_upper = 255*255*16*3;
- unsigned int best_err_lower = 255*255*16*3;
- unsigned int best_err_left = 255*255*16*3;
- unsigned int best_err_right = 255*255*16*3;
- int best_upper_col[3];
- int best_lower_col[3];
- int best_left_col[3];
- int best_right_col[3];
- unsigned int table1=0, table2=0;
- unsigned int best_table1=0, best_table2=0;
- unsigned int precalc_err_UL_R[8*4*4];
- unsigned int precalc_err_UR_R[8*4*4];
- unsigned int precalc_err_LL_R[8*4*4];
- unsigned int precalc_err_LR_R[8*4*4];
- unsigned int precalc_err_UL_RG[8*4*4];
- unsigned int precalc_err_UR_RG[8*4*4];
- unsigned int precalc_err_LL_RG[8*4*4];
- unsigned int precalc_err_LR_RG[8*4*4];
- int diffbit;
- uint8 block_2x2[4*4*4];
- unsigned int best_err;
- int best_flip;
- int xx,yy,count = 0;
- // Reshuffle pixels so that the top left 2x2 pixels arrive first, then the top right 2x2 pixels etc. Also put use 4 bytes per pixel to make it 32-word aligned.
- for(xx = 0; xx<2; xx++)
- {
- for(yy=0; yy<2; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 2; xx<4; xx++)
- {
- for(yy=0; yy<2; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 0; xx<2; xx++)
- {
- for(yy=2; yy<4; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 2; xx<4; xx++)
- {
- for(yy=2; yy<4; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- unsigned int test1, test2;
- best_err = (unsigned int)compressBlockOnlyIndividualAverage(img, width, height, startx, starty, test1, test2, best_enc_color1, best_enc_color2, best_flip, best_err_upper, best_err_lower, best_err_left, best_err_right, best_upper_col, best_lower_col, best_left_col, best_right_col);
- if(best_err < total_best_err)
- total_best_err = best_err;
- unsigned int tryblocks = 0;
- unsigned int allblocks = 0;
- int needtest;
- for(enc_color1[0]=0; enc_color1[0]<16; enc_color1[0]++)
- {
- color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0]);
- if(precompute_3bittable_all_subblocksR_with_test(block_2x2, color_quant1, precalc_err_UL_R, precalc_err_UR_R, precalc_err_LL_R, precalc_err_LR_R, total_best_err))
- {
- for(enc_color1[1]=0; enc_color1[1]<16; enc_color1[1]++)
- {
- color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1]);
- if(precompute_3bittable_all_subblocksRG_withtest(block_2x2, color_quant1, precalc_err_UL_R, precalc_err_UR_R, precalc_err_LL_R, precalc_err_LR_R, precalc_err_UL_RG, precalc_err_UR_RG, precalc_err_LL_RG, precalc_err_LR_RG, total_best_err))
- {
- needtest = false;
- for(enc_color1[2]=0; enc_color1[2]<16; enc_color1[2]++)
- {
- color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2]);
- tryalltables_3bittable_all_subblocks_using_precalc(block_2x2, color_quant1, precalc_err_UL_RG, precalc_err_UR_RG, precalc_err_LL_RG, precalc_err_LR_RG, err_upper, err_lower, err_left, err_right, total_best_err);
- if(err_upper<best_err_upper)
- {
- best_err_upper = err_upper;
- best_upper_col[0] = enc_color1[0];
- best_upper_col[1] = enc_color1[1];
- best_upper_col[2] = enc_color1[2];
- needtest = true;
- }
- if(err_lower<best_err_lower)
- {
- best_err_lower = err_lower;
- best_lower_col[0] = enc_color1[0];
- best_lower_col[1] = enc_color1[1];
- best_lower_col[2] = enc_color1[2];
- needtest=true;
- }
- if(err_left<best_err_left)
- {
- best_err_left = err_left;
- best_left_col[0] = enc_color1[0];
- best_left_col[1] = enc_color1[1];
- best_left_col[2] = enc_color1[2];
- needtest=true;
- }
- if(err_right<best_err_right)
- {
- best_err_right = err_right;
- best_right_col[0] = enc_color1[0];
- best_right_col[1] = enc_color1[1];
- best_right_col[2] = enc_color1[2];
- needtest = true;
- }
- }
- if(needtest)
- {
- if(best_err_upper+best_err_lower < best_err_left+best_err_right)
- {
- best_err = best_err_upper+best_err_lower;
- if(best_err < total_best_err)
- total_best_err = best_err;
- }
- else
- {
- best_err = best_err_left+best_err_right;
- if(best_err < total_best_err)
- total_best_err = best_err;
- }
- }
- }
- }
- }
- }
- if(best_err_upper+best_err_lower < best_err_left+best_err_right)
- {
- best_flip = 1;
- best_enc_color1[0] = best_upper_col[0];
- best_enc_color1[1] = best_upper_col[1];
- best_enc_color1[2] = best_upper_col[2];
- best_enc_color2[0] = best_lower_col[0];
- best_enc_color2[1] = best_lower_col[1];
- best_enc_color2[2] = best_lower_col[2];
- best_err = best_err_upper+best_err_lower;
- if(best_err < total_best_err)
- total_best_err = best_err;
- }
- else
- {
- best_flip = 0;
- best_enc_color1[0] = best_left_col[0];
- best_enc_color1[1] = best_left_col[1];
- best_enc_color1[2] = best_left_col[2];
- best_enc_color2[0] = best_right_col[0];
- best_enc_color2[1] = best_right_col[1];
- best_enc_color2[2] = best_right_col[2];
- best_err = best_err_left+best_err_right;
- if(best_err < total_best_err)
- total_best_err = best_err;
- }
- color_quant1[0] = best_enc_color1[0] << 4 | (best_enc_color1[0]);
- color_quant1[1] = best_enc_color1[1] << 4 | (best_enc_color1[1]);
- color_quant1[2] = best_enc_color1[2] << 4 | (best_enc_color1[2]);
- if(best_flip == 0)
- tryalltables_3bittable2x4(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- else
- tryalltables_3bittable4x2(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- color_quant2[0] = best_enc_color2[0] << 4 | (best_enc_color2[0]);
- color_quant2[1] = best_enc_color2[1] << 4 | (best_enc_color2[1]);
- color_quant2[2] = best_enc_color2[2] << 4 | (best_enc_color2[2]);
- if(best_flip == 0)
- tryalltables_3bittable2x4(img,width,height,startx+2,starty,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- else
- tryalltables_3bittable4x2(img,width,height,startx,starty+2,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- diffbit = 1;
- compressed1 = 0;
- PUTBITSHIGH( compressed1, diffbit, 0, 33);
- PUTBITSHIGH( compressed1, best_enc_color1[0], 4, 63);
- PUTBITSHIGH( compressed1, best_enc_color1[1], 4, 55);
- PUTBITSHIGH( compressed1, best_enc_color1[2], 4, 47);
- PUTBITSHIGH( compressed1, best_enc_color2[0], 4, 59);
- PUTBITSHIGH( compressed1, best_enc_color2[1], 4, 51);
- PUTBITSHIGH( compressed1, best_enc_color2[2], 4, 43);
- PUTBITSHIGH( compressed1, best_table1, 3, 39);
- PUTBITSHIGH( compressed1, best_table2, 3, 36);
- PUTBITSHIGH( compressed1, best_flip, 1, 32);
- if(best_flip == 0)
- {
- compressed2 = 0;
- PUTBITS( compressed2, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2, (best_pixel_indices2_LSB ), 8, 15);
- }
- else
- {
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
- compressed2 = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- return best_err;
- }
- #endif
-
- #if EXHAUSTIVE_CODE_ACTIVE
- // Compresses the differential mode exhaustively (perecptual error metric).
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockDifferentialExhaustivePerceptual(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, unsigned int best_error_so_far)
- {
- unsigned int best_err_norm_diff = MAXERR1000;
- unsigned int best_err_norm_444 = MAXERR1000;
- unsigned int best_err_flip_diff = MAXERR1000;
- unsigned int best_err_flip_444 = MAXERR1000;
- uint8 color_quant1[3], color_quant2[3];
- int enc_color1[3], enc_color2[3], diff[3];
- int best_enc_color1[3], best_enc_color2[3];
- signed char bytediff[3];
-
- unsigned int best_pixel_indices1_MSB=0;
- unsigned int best_pixel_indices1_LSB=0;
- unsigned int best_pixel_indices2_MSB=0;
- unsigned int best_pixel_indices2_LSB=0;
- unsigned int pixel_indices1_MSB=0;
- unsigned int pixel_indices1_LSB=0;
- unsigned int pixel_indices2_MSB=0;
- unsigned int *err_upper, *err_lower;
- unsigned int *err_left, *err_right;
- unsigned int pixel_indices2_LSB=0;
- unsigned int table1=0, table2=0;
- unsigned int best_table1=0, best_table2=0;
- unsigned int precalc_err_UL_R[8*4*4];
- unsigned int precalc_err_UR_R[8*4*4];
- unsigned int precalc_err_LL_R[8*4*4];
- unsigned int precalc_err_LR_R[8*4*4];
- unsigned int precalc_err_UL_RG[8*4*4];
- unsigned int precalc_err_UR_RG[8*4*4];
- unsigned int precalc_err_LL_RG[8*4*4];
- unsigned int precalc_err_LR_RG[8*4*4];
- unsigned int best_error_using_diff_mode;
- int diffbit;
- uint8 block_2x2[4*4*4];
- unsigned int error, error_lying, error_standing;
- unsigned int *err_lower_adr;
- int best_flip;
- unsigned int *err_right_adr;
- int xx,yy,count = 0;
- // Reshuffle pixels so that the top left 2x2 pixels arrive first, then the top right 2x2 pixels etc. Also put use 4 bytes per pixel to make it 32-word aligned.
- for(xx = 0; xx<2; xx++)
- {
- for(yy=0; yy<2; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 2; xx<4; xx++)
- {
- for(yy=0; yy<2; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 0; xx<2; xx++)
- {
- for(yy=2; yy<4; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 2; xx<4; xx++)
- {
- for(yy=2; yy<4; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- unsigned int test1, test2;
- best_error_using_diff_mode = compressBlockOnlyDiffFlipAveragePerceptual1000(img, width, height, startx, starty, test1, test2);
- if(best_error_using_diff_mode < best_error_so_far)
- best_error_so_far = best_error_using_diff_mode;
- // Decode the parameters so that we have a worst case color pair and a flip status
- best_flip = test1 & 1;
- best_enc_color1[0] = GETBITSHIGH( test1, 5, 63);
- best_enc_color1[1] = GETBITSHIGH( test1, 5, 55);
- best_enc_color1[2] = GETBITSHIGH( test1, 5, 47);
- bytediff[0] = GETBITSHIGH( test1, 3, 58);
- bytediff[1] = GETBITSHIGH( test1, 3, 50);
- bytediff[2] = GETBITSHIGH( test1, 3, 42);
- bytediff[0] = (bytediff[0] << 5);
- bytediff[1] = (bytediff[1] << 5);
- bytediff[2] = (bytediff[2] << 5);
- bytediff[0] = bytediff[0] >> 5;
- bytediff[1] = bytediff[1] >> 5;
- bytediff[2] = bytediff[2] >> 5;
- best_enc_color2[0]= best_enc_color1[0] + bytediff[0];
- best_enc_color2[1]= best_enc_color1[1] + bytediff[1];
- best_enc_color2[2]= best_enc_color1[2] + bytediff[2];
- // allocate memory for errors:
- err_upper = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
- if(!err_upper){printf("Out of memory allocating \n");exit(1);}
- err_lower = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
- if(!err_lower){printf("Out of memory allocating \n");exit(1);}
- err_left = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
- if(!err_left){printf("Out of memory allocating \n");exit(1);}
- err_right = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
- if(!err_right){printf("Out of memory allocating \n");exit(1);}
- int q;
- // Calculate all errors
- for(enc_color1[0]=0; enc_color1[0]<32; enc_color1[0]++)
- {
- color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- if(precompute_3bittable_all_subblocksR_with_test_perceptual1000(block_2x2, color_quant1, precalc_err_UL_R, precalc_err_UR_R, precalc_err_LL_R, precalc_err_LR_R, best_error_so_far))
- {
- for(enc_color1[1]=0; enc_color1[1]<32; enc_color1[1]++)
- {
- color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- if(precompute_3bittable_all_subblocksRG_withtest_perceptual1000(block_2x2, color_quant1, precalc_err_UL_R, precalc_err_UR_R, precalc_err_LL_R, precalc_err_LR_R, precalc_err_UL_RG, precalc_err_UR_RG, precalc_err_LL_RG, precalc_err_LR_RG, best_error_so_far))
- {
- for(enc_color1[2]=0; enc_color1[2]<32; enc_color1[2]++)
- {
- color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- tryalltables_3bittable_all_subblocks_using_precalc_perceptual1000(block_2x2, color_quant1, precalc_err_UL_RG, precalc_err_UR_RG, precalc_err_LL_RG, precalc_err_LR_RG, err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]], err_lower[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]], err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]], err_right[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]], best_error_so_far);
- }
- }
- else
- {
- for(q=0;q<32;q++)
- {
- err_upper[32*32*enc_color1[0]+32*enc_color1[1]+q] = MAXERR1000;
- err_lower[32*32*enc_color1[0]+32*enc_color1[1]+q] = MAXERR1000;
- err_left[32*32*enc_color1[0]+32*enc_color1[1]+q] = MAXERR1000;
- err_right[32*32*enc_color1[0]+32*enc_color1[1]+q] = MAXERR1000;
- }
- }
- }
- }
- else
- {
- for(q=0;q<32*32;q++)
- {
- err_upper[32*32*enc_color1[0]+q] = MAXERR1000;
- err_lower[32*32*enc_color1[0]+q] = MAXERR1000;
- err_left[32*32*enc_color1[0]+q] = MAXERR1000;
- err_right[32*32*enc_color1[0]+q] = MAXERR1000;
- }
- }
- }
- for(enc_color1[0]=0; enc_color1[0]<32; enc_color1[0]++)
- {
- for(enc_color1[1]=0; enc_color1[1]<32; enc_color1[1]++)
- {
- for(enc_color1[2]=0; enc_color1[2]<4; enc_color1[2]++)
- {
- error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- if(error_lying < best_error_so_far || error_standing < best_error_so_far)
- {
- for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
- {
- for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
- {
- err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
- err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
- for(enc_color2[2]=JAS_MAX(0,enc_color1[2]-4); enc_color2[2]<JAS_MIN(enc_color1[2]+4,32); enc_color2[2]++)
- {
- error = error_lying+err_lower_adr[enc_color2[2]];
- if(error<best_error_so_far)
- {
- best_flip = 1;
- best_error_so_far = error;
- best_error_using_diff_mode = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- error = error_standing+err_right_adr[enc_color2[2]];
- if(error<best_error_so_far)
- {
- best_flip = 0;
- best_error_so_far = error;
- best_error_using_diff_mode = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- }
- }
- }
- }
- }
- for(enc_color1[2]=4; enc_color1[2]<28; enc_color1[2]++)
- {
- error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- if(error_lying < best_error_so_far || error_standing < best_error_so_far)
- {
- for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
- {
- for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
- {
- err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
- err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
- // since enc_color[2] is between 4 and 29 we do not need to clamp the loop on the next line
- for(enc_color2[2]=enc_color1[2]-4; enc_color2[2]<enc_color1[2]+4; enc_color2[2]++)
- {
- error = error_lying+err_lower_adr[enc_color2[2]];
- if(error<best_error_so_far)
- {
- best_flip = 1;
- best_error_so_far = error;
- best_error_using_diff_mode = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- error = error_standing+err_right_adr[enc_color2[2]];
- if(error<best_error_so_far)
- {
- best_flip = 0;
- best_error_so_far = error;
- best_error_using_diff_mode = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- }
- }
- }
- }
- }
- for(enc_color1[2]=28; enc_color1[2]<32; enc_color1[2]++)
- {
- error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- if(error_lying < best_error_so_far || error_standing < best_error_so_far)
- {
- for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
- {
- for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
- {
- err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
- err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
- for(enc_color2[2]=JAS_MAX(0,enc_color1[2]-4); enc_color2[2]<JAS_MIN(enc_color1[2]+4,32); enc_color2[2]++)
- {
- error = error_lying+err_lower_adr[enc_color2[2]];
- if(error<best_error_so_far)
- {
- best_flip = 1;
- best_error_so_far = error;
- best_error_using_diff_mode = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- error = error_standing+err_right_adr[enc_color2[2]];
- if(error<best_error_so_far)
- {
- best_flip = 0;
- best_error_so_far = error;
- best_error_using_diff_mode = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- }
- }
- }
- }
- }
- }
- }
- free(err_upper);
- free(err_lower);
- free(err_left);
- free(err_right);
- color_quant1[0] = best_enc_color1[0] << 3 | (best_enc_color1[0] >> 2);
- color_quant1[1] = best_enc_color1[1] << 3 | (best_enc_color1[1] >> 2);
- color_quant1[2] = best_enc_color1[2] << 3 | (best_enc_color1[2] >> 2);
- if(best_flip == 0)
- tryalltables_3bittable2x4percep1000(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- else
- tryalltables_3bittable4x2percep1000(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- color_quant2[0] = best_enc_color2[0] << 3 | (best_enc_color2[0] >> 2);
- color_quant2[1] = best_enc_color2[1] << 3 | (best_enc_color2[1] >> 2);
- color_quant2[2] = best_enc_color2[2] << 3 | (best_enc_color2[2] >> 2);
- if(best_flip == 0)
- tryalltables_3bittable2x4percep1000(img,width,height,startx+2,starty,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- else
- tryalltables_3bittable4x2percep1000(img,width,height,startx,starty+2,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- diff[0] = best_enc_color2[0]-best_enc_color1[0];
- diff[1] = best_enc_color2[1]-best_enc_color1[1];
- diff[2] = best_enc_color2[2]-best_enc_color1[2];
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- diffbit = 1;
- compressed1 = 0;
- PUTBITSHIGH( compressed1, diffbit, 1, 33);
- PUTBITSHIGH( compressed1, best_enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1, best_enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1, best_enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1, diff[0], 3, 58);
- PUTBITSHIGH( compressed1, diff[1], 3, 50);
- PUTBITSHIGH( compressed1, diff[2], 3, 42);
- PUTBITSHIGH( compressed1, best_table1, 3, 39);
- PUTBITSHIGH( compressed1, best_table2, 3, 36);
- PUTBITSHIGH( compressed1, best_flip, 1, 32);
- if(best_flip == 0)
- {
- compressed2 = 0;
- PUTBITS( compressed2, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2, (best_pixel_indices2_LSB ), 8, 15);
- }
- else
- {
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
- compressed2 = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- return best_error_using_diff_mode;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Compresses the differential mode exhaustively.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockDifferentialExhaustive(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, unsigned int previous_best_err)
- {
- unsigned int best_err_norm_diff = 255*255*16*3;
- unsigned int best_err_norm_444 = 255*255*16*3;
- unsigned int best_err_flip_diff = 255*255*16*3;
- unsigned int best_err_flip_444 = 255*255*16*3;
- uint8 color_quant1[3], color_quant2[3];
- int enc_color1[3], enc_color2[3], diff[3];
- int best_enc_color1[3], best_enc_color2[3];
-
- int min_error=255*255*8*3;
- unsigned int best_pixel_indices1_MSB=0;
- unsigned int best_pixel_indices1_LSB=0;
- unsigned int best_pixel_indices2_MSB=0;
- unsigned int best_pixel_indices2_LSB=0;
- unsigned int pixel_indices1_MSB=0;
- unsigned int pixel_indices1_LSB=0;
- unsigned int pixel_indices2_MSB=0;
- unsigned int *err_upper, *err_lower;
- unsigned int *err_left, *err_right;
- unsigned int pixel_indices2_LSB=0;
- unsigned int table1=0, table2=0;
- unsigned int best_table1=0, best_table2=0;
- unsigned int precalc_err_UL_R[8*4*4];
- unsigned int precalc_err_UR_R[8*4*4];
- unsigned int precalc_err_LL_R[8*4*4];
- unsigned int precalc_err_LR_R[8*4*4];
- unsigned int precalc_err_UL_RG[8*4*4];
- unsigned int precalc_err_UR_RG[8*4*4];
- unsigned int precalc_err_LL_RG[8*4*4];
- unsigned int precalc_err_LR_RG[8*4*4];
- int diffbit;
- uint8 block_2x2[4*4*4];
- unsigned int error, error_lying, error_standing, best_err, total_best_err;
- unsigned int *err_lower_adr;
- int best_flip;
- unsigned int *err_right_adr;
- int xx,yy,count = 0;
- // Reshuffle pixels so that the top left 2x2 pixels arrive first, then the top right 2x2 pixels etc. Also put use 4 bytes per pixel to make it 32-word aligned.
- for(xx = 0; xx<2; xx++)
- {
- for(yy=0; yy<2; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 2; xx<4; xx++)
- {
- for(yy=0; yy<2; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 0; xx<2; xx++)
- {
- for(yy=2; yy<4; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- for(xx = 2; xx<4; xx++)
- {
- for(yy=2; yy<4; yy++)
- {
- block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block_2x2[(count)*4+3] = 0;
- count++;
- }
- }
- unsigned int test1, test2;
- best_err = (unsigned int)compressBlockOnlyDiffFlipAverage(img, width, height, startx, starty, test1, test2, best_enc_color1, best_enc_color2, best_flip);
- if(previous_best_err < best_err)
- total_best_err = previous_best_err;
- else
- total_best_err = best_err;
- // allocate memory for errors:
- err_upper = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
- if(!err_upper){printf("Out of memory allocating \n");exit(1);}
- err_lower = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
- if(!err_lower){printf("Out of memory allocating \n");exit(1);}
- err_left = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
- if(!err_left){printf("Out of memory allocating \n");exit(1);}
- err_right = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
- if(!err_right){printf("Out of memory allocating \n");exit(1);}
- int q;
- // Calculate all errors
- for(enc_color1[0]=0; enc_color1[0]<32; enc_color1[0]++)
- {
- color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
- if(precompute_3bittable_all_subblocksR_with_test(block_2x2, color_quant1, precalc_err_UL_R, precalc_err_UR_R, precalc_err_LL_R, precalc_err_LR_R, total_best_err))
- {
- for(enc_color1[1]=0; enc_color1[1]<32; enc_color1[1]++)
- {
- color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
- if(precompute_3bittable_all_subblocksRG_withtest(block_2x2, color_quant1, precalc_err_UL_R, precalc_err_UR_R, precalc_err_LL_R, precalc_err_LR_R, precalc_err_UL_RG, precalc_err_UR_RG, precalc_err_LL_RG, precalc_err_LR_RG, total_best_err))
- {
- for(enc_color1[2]=0; enc_color1[2]<32; enc_color1[2]++)
- {
- color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
- tryalltables_3bittable_all_subblocks_using_precalc(block_2x2, color_quant1, precalc_err_UL_RG, precalc_err_UR_RG, precalc_err_LL_RG, precalc_err_LR_RG, err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]], err_lower[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]], err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]], err_right[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]], total_best_err);
- }
- }
- else
- {
- for(q=0;q<32;q++)
- {
- err_upper[32*32*enc_color1[0]+32*enc_color1[1]+q] = 255*255*16*3;
- err_lower[32*32*enc_color1[0]+32*enc_color1[1]+q] = 255*255*16*3;
- err_left[32*32*enc_color1[0]+32*enc_color1[1]+q] = 255*255*16*3;
- err_right[32*32*enc_color1[0]+32*enc_color1[1]+q] = 255*255*16*3;
- }
- }
- }
- }
- else
- {
- for(q=0;q<32*32;q++)
- {
- err_upper[32*32*enc_color1[0]+q] = 255*255*16*3;
- err_lower[32*32*enc_color1[0]+q] = 255*255*16*3;
- err_left[32*32*enc_color1[0]+q] = 255*255*16*3;
- err_right[32*32*enc_color1[0]+q] = 255*255*16*3;
- }
- }
- }
- for(enc_color1[0]=0; enc_color1[0]<32; enc_color1[0]++)
- {
- for(enc_color1[1]=0; enc_color1[1]<32; enc_color1[1]++)
- {
- for(enc_color1[2]=0; enc_color1[2]<4; enc_color1[2]++)
- {
- error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- if(error_lying < total_best_err || error_standing < total_best_err)
- {
- for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
- {
- for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
- {
- err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
- err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
- for(enc_color2[2]=JAS_MAX(0,enc_color1[2]-4); enc_color2[2]<JAS_MIN(enc_color1[2]+4,32); enc_color2[2]++)
- {
- error = error_lying+err_lower_adr[enc_color2[2]];
- if(error<best_err)
- {
- best_flip = 1;
- best_err = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- error = error_standing+err_right_adr[enc_color2[2]];
- if(error<best_err)
- {
- best_flip = 0;
- best_err = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- }
- }
- }
- if(best_err < total_best_err)
- total_best_err = best_err;
- }
- }
- for(enc_color1[2]=4; enc_color1[2]<28; enc_color1[2]++)
- {
- error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- if(error_lying < total_best_err || error_standing < total_best_err)
- {
- for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
- {
- for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
- {
- err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
- err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
- // since enc_color[2] is between 4 and 29 we do not need to clamp the loop on the next line
- for(enc_color2[2]=enc_color1[2]-4; enc_color2[2]<enc_color1[2]+4; enc_color2[2]++)
- {
- error = error_lying+err_lower_adr[enc_color2[2]];
- if(error<best_err)
- {
- best_flip = 1;
- best_err = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- error = error_standing+err_right_adr[enc_color2[2]];
- if(error<best_err)
- {
- best_flip = 0;
- best_err = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- }
- }
- }
- if(best_err < total_best_err)
- total_best_err = best_err;
- }
- }
- for(enc_color1[2]=28; enc_color1[2]<32; enc_color1[2]++)
- {
- error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
- if(error_lying < total_best_err || error_standing < total_best_err)
- {
- for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
- {
- for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
- {
- err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
- err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
- for(enc_color2[2]=JAS_MAX(0,enc_color1[2]-4); enc_color2[2]<JAS_MIN(enc_color1[2]+4,32); enc_color2[2]++)
- {
- error = error_lying+err_lower_adr[enc_color2[2]];
- if(error<best_err)
- {
- best_flip = 1;
- best_err = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- error = error_standing+err_right_adr[enc_color2[2]];
- if(error<best_err)
- {
- best_flip = 0;
- best_err = error;
- best_enc_color1[0] = enc_color1[0];
- best_enc_color1[1] = enc_color1[1];
- best_enc_color1[2] = enc_color1[2];
- best_enc_color2[0] = enc_color2[0];
- best_enc_color2[1] = enc_color2[1];
- best_enc_color2[2] = enc_color2[2];
- }
- }
- }
- }
- if(best_err < total_best_err)
- total_best_err = best_err;
- }
- }
- }
- }
- free(err_upper);
- free(err_lower);
- free(err_left);
- free(err_right);
- color_quant1[0] = best_enc_color1[0] << 3 | (best_enc_color1[0] >> 2);
- color_quant1[1] = best_enc_color1[1] << 3 | (best_enc_color1[1] >> 2);
- color_quant1[2] = best_enc_color1[2] << 3 | (best_enc_color1[2] >> 2);
- if(best_flip == 0)
- tryalltables_3bittable2x4(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- else
- tryalltables_3bittable4x2(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
- color_quant2[0] = best_enc_color2[0] << 3 | (best_enc_color2[0] >> 2);
- color_quant2[1] = best_enc_color2[1] << 3 | (best_enc_color2[1] >> 2);
- color_quant2[2] = best_enc_color2[2] << 3 | (best_enc_color2[2] >> 2);
- if(best_flip == 0)
- tryalltables_3bittable2x4(img,width,height,startx+2,starty,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- else
- tryalltables_3bittable4x2(img,width,height,startx,starty+2,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
- diff[0] = best_enc_color2[0]-best_enc_color1[0];
- diff[1] = best_enc_color2[1]-best_enc_color1[1];
- diff[2] = best_enc_color2[2]-best_enc_color1[2];
- // ETC1_RGB8_OES:
- //
- // a) bit layout in bits 63 through 32 if diffbit = 0
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
- // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // b) bit layout in bits 63 through 32 if diffbit = 1
- //
- // 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
- // ---------------------------------------------------------------------------------------------------
- // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
- // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
- // ---------------------------------------------------------------------------------------------------
- //
- // c) bit layout in bits 31 through 0 (in both cases)
- //
- // 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- // --------------------------------------------------------------------------------------------------
- // | most significant pixel index bits | least significant pixel index bits |
- // | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
- // --------------------------------------------------------------------------------------------------
- diffbit = 1;
- compressed1 = 0;
- PUTBITSHIGH( compressed1, diffbit, 1, 33);
- PUTBITSHIGH( compressed1, best_enc_color1[0], 5, 63);
- PUTBITSHIGH( compressed1, best_enc_color1[1], 5, 55);
- PUTBITSHIGH( compressed1, best_enc_color1[2], 5, 47);
- PUTBITSHIGH( compressed1, diff[0], 3, 58);
- PUTBITSHIGH( compressed1, diff[1], 3, 50);
- PUTBITSHIGH( compressed1, diff[2], 3, 42);
- PUTBITSHIGH( compressed1, best_table1, 3, 39);
- PUTBITSHIGH( compressed1, best_table2, 3, 36);
- PUTBITSHIGH( compressed1, best_flip, 1, 32);
- if(best_flip == 0)
- {
- compressed2 = 0;
- PUTBITS( compressed2, (best_pixel_indices1_MSB ), 8, 23);
- PUTBITS( compressed2, (best_pixel_indices2_MSB ), 8, 31);
- PUTBITS( compressed2, (best_pixel_indices1_LSB ), 8, 7);
- PUTBITS( compressed2, (best_pixel_indices2_LSB ), 8, 15);
- }
- else
- {
- best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
- best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
- compressed2 = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
- }
- return best_err;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // This function uses real exhaustive search for the planar mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockPlanar57ExhaustivePerceptual(uint8 *img, int width,int height,int startx,int starty, unsigned int &compressed57_1, unsigned int &compressed57_2, unsigned int best_error_sofar, unsigned int best_error_planar_red, unsigned int best_error_planar_green, unsigned int best_error_planar_blue)
- {
- int colorO_enc[3], colorH_enc[3], colorV_enc[3];
- int best_colorO_enc[3], best_colorH_enc[3], best_colorV_enc[3];
- unsigned int error;
- unsigned int best_error;
- unsigned int lowest_possible_error;
- unsigned int best_error_red_sofar;
- unsigned int best_error_green_sofar;
- unsigned int best_error_blue_sofar;
- unsigned int BBBtable[128*128];
- unsigned int CCCtable[128*128];
- uint8 block[4*4*4];
- // Use 4 bytes per pixel to make it 32-word aligned.
- int count = 0;
- int xx, yy;
- for(yy=0; yy<4; yy++)
- {
- for(xx = 0; xx<4; xx++)
- {
- block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block[(count)*4+3] = 0;
- count++;
- }
- }
- // The task is to calculate the sum of the error over the entire area of the block.
- //
- // The block can be partitioned into: O A A A
- // B D D C
- // B D C D
- // B C D D
- // where the error in
- // O only depends on colorO
- // A only depends on colorO and colorH
- // B only depends on colorO and colorV
- // C only depends on colorH and colorV
- // D depends on all three (colorO, colorH and colorV)
- //
- // Note that B can be precalculated for all combinations of colorO and colorV
- // and the precalculated values can be used instead of calculating it in the inner loop.
- // The same applies to C.
- //
- // In the code below, the squared error over O A A A is calculated and stored in lowest_possible_error
- // Precalc BBB errors
- for(colorO_enc[0] = 0; colorO_enc[0]<64; colorO_enc[0]++)
- {
- for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
- {
- BBBtable[colorO_enc[0]*64+colorV_enc[0]] = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*calcBBBred(block, colorO_enc[0], colorV_enc[0]);
- }
- }
- // Precalc CCC errors
- for(colorH_enc[0] = 0; colorH_enc[0]<64; colorH_enc[0]++)
- {
- for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
- {
- CCCtable[colorH_enc[0]*64+colorV_enc[0]] = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*calcCCCred(block, colorH_enc[0], colorV_enc[0]);
- }
- }
- best_error = MAXERR1000;
- best_error_red_sofar = JAS_MIN(best_error_planar_red, best_error_sofar);
- for(colorO_enc[0] = 0; colorO_enc[0]<64; colorO_enc[0]++)
- {
- for(colorH_enc[0] = 0; colorH_enc[0]<64; colorH_enc[0]++)
- {
- lowest_possible_error = calcLowestPossibleRedOHperceptual(block, colorO_enc[0], colorH_enc[0], best_error_red_sofar);
- if(lowest_possible_error <= best_error_red_sofar)
- {
- for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
- {
- error = calcErrorPlanarOnlyRedPerceptual(block, colorO_enc[0], colorH_enc[0], colorV_enc[0], lowest_possible_error, BBBtable[colorO_enc[0]*64+colorV_enc[0]], CCCtable[colorH_enc[0]*64+colorV_enc[0]], best_error_red_sofar);
- if(error < best_error)
- {
- best_error = error;
- best_colorO_enc[0] = colorO_enc[0];
- best_colorH_enc[0] = colorH_enc[0];
- best_colorV_enc[0] = colorV_enc[0];
- }
- }
- }
- }
- }
- if(best_error < best_error_planar_red)
- best_error_planar_red = best_error;
- if(best_error_planar_red > best_error_sofar)
- {
- // The red component in itself is already bigger than the previously best value ---- we can give up.
- // use the dummy color black for all colors and report that the errors for the different color components are infinite
- best_error_planar_green = MAXERR1000;
- best_error_planar_blue = MAXERR1000;
- compressed57_1 = 0;
- compressed57_2 = 0;
- return;
- }
- // The task is to calculate the sum of the error over the entire area of the block.
- //
- // The block can be partitioned into: O A A A
- // B D D C
- // B D C D
- // B C D D
- // where the error in
- // O only depends on colorO
- // A only depends on colorO and colorH
- // B only depends on colorO and colorV
- // C only depends on colorH and colorV
- // D depends on all three (colorO, colorH and colorV)
- //
- // Note that B can be precalculated for all combinations of colorO and colorV
- // and the precalculated values can be used instead of calculating it in the inner loop.
- // The same applies to C.
- //
- // In the code below, the squared error over O A A A is calculated and store in lowest_possible_error
- // Precalc BBB errors
- for(colorO_enc[1] = 0; colorO_enc[1]<128; colorO_enc[1]++)
- {
- for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
- {
- BBBtable[colorO_enc[1]*128+colorV_enc[1]] = PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*calcBBBgreen(block, colorO_enc[1], colorV_enc[1]);
- }
- }
- // Precalc CCC errors
- for(colorH_enc[1] = 0; colorH_enc[1]<128; colorH_enc[1]++)
- {
- for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
- {
- CCCtable[colorH_enc[1]*128+colorV_enc[1]] = PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*calcCCCgreen(block, colorH_enc[1], colorV_enc[1]);
- }
- }
- best_error = MAXERR1000;
- best_error_green_sofar = JAS_MIN(best_error_planar_green, best_error_sofar);
- for(colorO_enc[1] = 0; colorO_enc[1]<128; colorO_enc[1]++)
- {
- for(colorH_enc[1] = 0; colorH_enc[1]<128; colorH_enc[1]++)
- {
- lowest_possible_error = calcLowestPossibleGreenOHperceptual(block, colorO_enc[1], colorH_enc[1], best_error_green_sofar);
- if(lowest_possible_error <= best_error_green_sofar)
- {
- for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
- {
- error = calcErrorPlanarOnlyGreenPerceptual(block, colorO_enc[1], colorH_enc[1], colorV_enc[1], lowest_possible_error, BBBtable[colorO_enc[1]*128+colorV_enc[1]], CCCtable[colorH_enc[1]*128+colorV_enc[1]], best_error_green_sofar);
- if(error < best_error)
- {
- best_error = error;
- best_colorO_enc[1] = colorO_enc[1];
- best_colorH_enc[1] = colorH_enc[1];
- best_colorV_enc[1] = colorV_enc[1];
- }
- }
- }
- }
- }
- if(best_error < best_error_planar_green)
- best_error_planar_green = best_error;
- if(best_error_planar_red + best_error_planar_green > best_error_sofar)
- {
- // The red component in itself is already bigger than the previously best value ---- we can give up.
- // use the dummy color black for all colors and report that the errors for the different color components are infinite
- best_error_planar_blue = MAXERR1000;
- compressed57_1 = 0;
- compressed57_2 = 0;
- return;
- }
- // The task is to calculate the sum of the error over the entire area of the block.
- //
- // The block can be partitioned into: O A A A
- // B D D C
- // B D C D
- // B C D D
- // where the error in
- // O only depends on colorO
- // A only depends on colorO and colorH
- // B only depends on colorO and colorV
- // C only depends on colorH and colorV
- // D depends on all three (colorO, colorH and colorV)
- //
- // Note that B can be precalculated for all combinations of colorO and colorV
- // and the precalculated values can be used instead of calculating it in the inner loop.
- // The same applies to C.
- //
- // In the code below, the squared error over O A A A is calculated and store in lowest_possible_error
- // Precalc BBB errors
- for(colorO_enc[2] = 0; colorO_enc[2]<64; colorO_enc[2]++)
- {
- for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
- {
- BBBtable[colorO_enc[2]*64+colorV_enc[2]] = calcBBBbluePerceptual(block, colorO_enc[2], colorV_enc[2]);
- }
- }
- // Precalc CCC errors
- for(colorH_enc[2] = 0; colorH_enc[2]<64; colorH_enc[2]++)
- {
- for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
- {
- CCCtable[colorH_enc[2]*64+colorV_enc[2]] = calcCCCbluePerceptual(block, colorH_enc[2], colorV_enc[2]);
- }
- }
- best_error = MAXERR1000;
- best_error_blue_sofar = JAS_MIN(best_error_planar_blue, best_error_sofar);
- for(colorO_enc[2] = 0; colorO_enc[2]<64; colorO_enc[2]++)
- {
- for(colorH_enc[2] = 0; colorH_enc[2]<64; colorH_enc[2]++)
- {
- lowest_possible_error = calcLowestPossibleBlueOHperceptual(block, colorO_enc[2], colorH_enc[2], best_error_blue_sofar);
- if(lowest_possible_error <= best_error_blue_sofar)
- {
- for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
- {
- error = calcErrorPlanarOnlyBluePerceptual(block, colorO_enc[2], colorH_enc[2], colorV_enc[2], lowest_possible_error, BBBtable[colorO_enc[2]*64+colorV_enc[2]], CCCtable[colorH_enc[2]*64+colorV_enc[2]], best_error_blue_sofar);
- if(error < best_error)
- {
- best_error = error;
- best_colorO_enc[2] = colorO_enc[2];
- best_colorH_enc[2] = colorH_enc[2];
- best_colorV_enc[2] = colorV_enc[2];
- }
- }
- }
- }
- }
- if(best_error < best_error_planar_blue)
- best_error_planar_blue = best_error;
- compressed57_1 = 0;
- compressed57_2 = 0;
- PUTBITSHIGH( compressed57_1, best_colorO_enc[0], 6, 63);
- PUTBITSHIGH( compressed57_1, best_colorO_enc[1], 7, 57);
- PUTBITSHIGH( compressed57_1, best_colorO_enc[2], 6, 50);
- PUTBITSHIGH( compressed57_1, best_colorH_enc[0], 6, 44);
- PUTBITSHIGH( compressed57_1, best_colorH_enc[1], 7, 38);
- PUTBITS( compressed57_2, best_colorH_enc[2], 6, 31);
- PUTBITS( compressed57_2, best_colorV_enc[0], 6, 25);
- PUTBITS( compressed57_2, best_colorV_enc[1], 7, 19);
- PUTBITS( compressed57_2, best_colorV_enc[2], 6, 12);
-
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // This function uses real exhaustive search for the planar mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockPlanar57Exhaustive(uint8 *img, int width,int height,int startx,int starty, unsigned int &compressed57_1, unsigned int &compressed57_2, unsigned int best_error_sofar, unsigned int best_error_red, unsigned int best_error_green, unsigned int best_error_blue)
- {
- int colorO_enc[3], colorH_enc[3], colorV_enc[3];
- int best_colorO_enc[3], best_colorH_enc[3], best_colorV_enc[3];
- unsigned int error;
- unsigned int best_error;
- unsigned int lowest_possible_error;
- unsigned int best_error_red_sofar;
- unsigned int best_error_green_sofar;
- unsigned int best_error_blue_sofar;
- unsigned int BBBtable[128*128];
- unsigned int CCCtable[128*128];
- uint8 block[4*4*4];
- // Use 4 bytes per pixel to make it 32-word aligned.
- int count = 0;
- int xx, yy;
- for(yy=0; yy<4; yy++)
- {
- for(xx = 0; xx<4; xx++)
- {
- block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block[(count)*4+3] = 0;
- count++;
- }
- }
- // The task is to calculate the sum of the error over the entire area of the block.
- //
- // The block can be partitioned into: O A A A
- // B D D C
- // B D C D
- // B C D D
- // where the error in
- // O only depends on colorO
- // A only depends on colorO and colorH
- // B only depends on colorO and colorV
- // C only depends on colorH and colorV
- // D depends on all three (colorO, colorH and colorV)
- //
- // Note that B can be precalculated for all combinations of colorO and colorV
- // and the precalculated values can be used instead of calculating it in the inner loop.
- // The same applies to C.
- //
- // In the code below, the squared error over O A A A is calculated and store in lowest_possible_error
- // Precalc BBB errors
- for(colorO_enc[0] = 0; colorO_enc[0]<64; colorO_enc[0]++)
- {
- for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
- {
- BBBtable[colorO_enc[0]*64+colorV_enc[0]] = calcBBBred(block, colorO_enc[0], colorV_enc[0]);
- }
- }
- // Precalc CCC errors
- for(colorH_enc[0] = 0; colorH_enc[0]<64; colorH_enc[0]++)
- {
- for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
- {
- CCCtable[colorH_enc[0]*64+colorV_enc[0]] = calcCCCred(block, colorH_enc[0], colorV_enc[0]);
- }
- }
- best_error = MAXERR1000;
- best_error_red_sofar = JAS_MIN(best_error_red, best_error_sofar);
- for(colorO_enc[0] = 0; colorO_enc[0]<64; colorO_enc[0]++)
- {
- for(colorH_enc[0] = 0; colorH_enc[0]<64; colorH_enc[0]++)
- {
- lowest_possible_error = calcLowestPossibleRedOH(block, colorO_enc[0], colorH_enc[0], best_error_red_sofar);
- if(lowest_possible_error <= best_error_red_sofar)
- {
- for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
- {
- error = calcErrorPlanarOnlyRed(block, colorO_enc[0], colorH_enc[0], colorV_enc[0], lowest_possible_error, BBBtable[colorO_enc[0]*64+colorV_enc[0]], CCCtable[colorH_enc[0]*64+colorV_enc[0]], best_error_red_sofar);
- if(error < best_error)
- {
- best_error = error;
- best_colorO_enc[0] = colorO_enc[0];
- best_colorH_enc[0] = colorH_enc[0];
- best_colorV_enc[0] = colorV_enc[0];
- }
- }
- }
- }
- }
- // The task is to calculate the sum of the error over the entire area of the block.
- //
- // The block can be partitioned into: O A A A
- // B D D C
- // B D C D
- // B C D D
- // where the error in
- // O only depends on colorO
- // A only depends on colorO and colorH
- // B only depends on colorO and colorV
- // C only depends on colorH and colorV
- // D depends on all three (colorO, colorH and colorV)
- //
- // Note that B can be precalculated for all combinations of colorO and colorV
- // and the precalculated values can be used instead of calculating it in the inner loop.
- // The same applies to C.
- //
- // In the code below, the squared error over O A A A is calculated and store in lowest_possible_error
- // Precalc BBB errors
- for(colorO_enc[1] = 0; colorO_enc[1]<128; colorO_enc[1]++)
- {
- for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
- {
- BBBtable[colorO_enc[1]*128+colorV_enc[1]] = calcBBBgreen(block, colorO_enc[1], colorV_enc[1]);
- }
- }
- // Precalc CCC errors
- for(colorH_enc[1] = 0; colorH_enc[1]<128; colorH_enc[1]++)
- {
- for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
- {
- CCCtable[colorH_enc[1]*128+colorV_enc[1]] = calcCCCgreen(block, colorH_enc[1], colorV_enc[1]);
- }
- }
- best_error = MAXERR1000;
- best_error_green_sofar = JAS_MIN(best_error_green, best_error_sofar);
- for(colorO_enc[1] = 0; colorO_enc[1]<128; colorO_enc[1]++)
- {
- for(colorH_enc[1] = 0; colorH_enc[1]<128; colorH_enc[1]++)
- {
- lowest_possible_error = calcLowestPossibleGreenOH(block, colorO_enc[1], colorH_enc[1], best_error_green_sofar);
- if(lowest_possible_error <= best_error_green_sofar)
- {
- for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
- {
- error = calcErrorPlanarOnlyGreen(block, colorO_enc[1], colorH_enc[1], colorV_enc[1], lowest_possible_error, BBBtable[colorO_enc[1]*128+colorV_enc[1]], CCCtable[colorH_enc[1]*128+colorV_enc[1]], best_error_green_sofar);
- if(error < best_error)
- {
- best_error = error;
- best_colorO_enc[1] = colorO_enc[1];
- best_colorH_enc[1] = colorH_enc[1];
- best_colorV_enc[1] = colorV_enc[1];
- }
- }
- }
- }
- }
- // The task is to calculate the sum of the error over the entire area of the block.
- //
- // The block can be partitioned into: O A A A
- // B D D C
- // B D C D
- // B C D D
- // where the error in
- // O only depends on colorO
- // A only depends on colorO and colorH
- // B only depends on colorO and colorV
- // C only depends on colorH and colorV
- // D depends on all three (colorO, colorH and colorV)
- //
- // Note that B can be precalculated for all combinations of colorO and colorV
- // and the precalculated values can be used instead of calculating it in the inner loop.
- // The same applies to C.
- //
- // In the code below, the squared error over O A A A is calculated and store in lowest_possible_error
- // Precalc BBB errors
- for(colorO_enc[2] = 0; colorO_enc[2]<64; colorO_enc[2]++)
- {
- for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
- {
- BBBtable[colorO_enc[2]*64+colorV_enc[2]] = calcBBBblue(block, colorO_enc[2], colorV_enc[2]);
- }
- }
- // Precalc CCC errors
- for(colorH_enc[2] = 0; colorH_enc[2]<64; colorH_enc[2]++)
- {
- for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
- {
- CCCtable[colorH_enc[2]*64+colorV_enc[2]] = calcCCCblue(block, colorH_enc[2], colorV_enc[2]);
- }
- }
- best_error = MAXERR1000;
- best_error_blue_sofar = JAS_MIN(best_error_blue, best_error_sofar);
- for(colorO_enc[2] = 0; colorO_enc[2]<64; colorO_enc[2]++)
- {
- for(colorH_enc[2] = 0; colorH_enc[2]<64; colorH_enc[2]++)
- {
- lowest_possible_error = calcLowestPossibleBlueOH(block, colorO_enc[2], colorH_enc[2], best_error_blue_sofar);
- if(lowest_possible_error <= best_error_blue_sofar)
- {
- for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
- {
- error = calcErrorPlanarOnlyBlue(block, colorO_enc[2], colorH_enc[2], colorV_enc[2], lowest_possible_error, BBBtable[colorO_enc[2]*64+colorV_enc[2]], CCCtable[colorH_enc[2]*64+colorV_enc[2]], best_error_blue_sofar);
- if(error < best_error)
- {
- best_error = error;
- best_colorO_enc[2] = colorO_enc[2];
- best_colorH_enc[2] = colorH_enc[2];
- best_colorV_enc[2] = colorV_enc[2];
- }
- }
- }
- }
- }
- compressed57_1 = 0;
- compressed57_2 = 0;
- PUTBITSHIGH( compressed57_1, best_colorO_enc[0], 6, 63);
- PUTBITSHIGH( compressed57_1, best_colorO_enc[1], 7, 57);
- PUTBITSHIGH( compressed57_1, best_colorO_enc[2], 6, 50);
- PUTBITSHIGH( compressed57_1, best_colorH_enc[0], 6, 44);
- PUTBITSHIGH( compressed57_1, best_colorH_enc[1], 7, 38);
- PUTBITS( compressed57_2, best_colorH_enc[2], 6, 31);
- PUTBITS( compressed57_2, best_colorV_enc[0], 6, 25);
- PUTBITS( compressed57_2, best_colorV_enc[1], 7, 19);
- PUTBITS( compressed57_2, best_colorV_enc[2], 6, 12);
-
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in exhaustive compression of the T-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError59T_col0_Rpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_R)
- {
- unsigned int block_error = 0,
- best_block_error = MAXERR1000,
- pixel_error,
- best_pixel_error;
- int diff;
- uint8 color;
- uint8 possible_colors[3];
- color = ((colorRGB444_packed >> 8) & 0xf)*17;
- // Test all distances
- for (uint8 d = 0; d < 8; d++)
- {
- possible_colors[0] = CLAMP(0,color - table59T[d],255);
- possible_colors[1] = CLAMP(0,color,255);
- possible_colors[2] = CLAMP(0,color + table59T[d],255);
- // Loop block
- for (int x = 0; x < 16; x++)
- {
- best_pixel_error = MAXERR1000;
- // Loop possible block colors
- for (uint8 c = 0; c < 3; c++)
- {
-
- diff = block[4*x + R] - CLAMP(0,possible_colors[c],255);
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff);
- // Choose best error
- if (pixel_error < best_pixel_error)
- best_pixel_error = pixel_error;
- }
- precalc_err_col0_R[((colorRGB444_packed>>8)*8 + d)*16 + x] = (unsigned int) best_pixel_error;
- }
- }
-
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in exhaustive compression of the T-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError59T_col0_R(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_R)
- {
- unsigned int block_error = 0,
- best_block_error = MAXIMUM_ERROR,
- pixel_error,
- best_pixel_error;
- int diff;
- uint8 color;
- uint8 possible_colors[3];
- color = ((colorRGB444_packed >> 8) & 0xf)*17;
- // Test all distances
- for (uint8 d = 0; d < 8; d++)
- {
- possible_colors[0] = CLAMP(0,color - table59T[d],255);
- possible_colors[1] = CLAMP(0,color,255);
- possible_colors[2] = CLAMP(0,color + table59T[d],255);
- // Loop block
- for (int x = 0; x < 16; x++)
- {
- best_pixel_error = MAXIMUM_ERROR;
- // Loop possible block colors
- for (uint8 c = 0; c < 3; c++)
- {
-
- diff = block[4*x + R] - CLAMP(0,possible_colors[c],255);
- pixel_error = SQUARE(diff);
- // Choose best error
- if (pixel_error < best_pixel_error)
- best_pixel_error = pixel_error;
- }
- precalc_err_col0_R[((colorRGB444_packed>>8)*8 + d)*16 + x] = (unsigned int) best_pixel_error;
- }
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in exhaustive compression of the T-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError59T_col0_RGpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_RG)
- {
- unsigned int block_error = 0,
- best_block_error = MAXERR1000,
- pixel_error,
- best_pixel_error;
- int diff[3];
- uint8 color[3];
- uint8 possible_colors[3][2];
- color[R] = ((colorRGB444_packed >> 8) & 0xf)*17;
- color[G] = ((colorRGB444_packed >> 4) & 0xf)*17;
- // Test all distances
- for (uint8 d = 0; d < 8; d++)
- {
- possible_colors[0][R] = CLAMP(0,color[R] - table59T[d],255);
- possible_colors[0][G] = CLAMP(0,color[G] - table59T[d],255);
- possible_colors[1][R] = CLAMP(0,color[R],255);
- possible_colors[1][G] = CLAMP(0,color[G],255);
- possible_colors[2][R] = CLAMP(0,color[R] + table59T[d],255);
- possible_colors[2][G] = CLAMP(0,color[G] + table59T[d],255);
-
- // Loop block
- for (int x = 0; x < 16; x++)
- {
- best_pixel_error = MAXERR1000;
- // Loop possible block colors
- for (uint8 c = 0; c < 3; c++)
- {
-
- diff[R] = block[4*x + R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = block[4*x + G] - CLAMP(0,possible_colors[c][G],255);
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- best_pixel_error = pixel_error;
- }
- precalc_err_col0_RG[((colorRGB444_packed>>4)*8 + d)*16 + x] = (unsigned int) best_pixel_error;
- }
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in exhaustive compression of the T-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError59T_col0_RG(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_RG)
- {
- unsigned int block_error = 0,
- best_block_error = MAXIMUM_ERROR,
- pixel_error,
- best_pixel_error;
- int diff[3];
- uint8 color[3];
- uint8 possible_colors[3][2];
- color[R] = ((colorRGB444_packed >> 8) & 0xf)*17;
- color[G] = ((colorRGB444_packed >> 4) & 0xf)*17;
- // Test all distances
- for (uint8 d = 0; d < 8; d++)
- {
- possible_colors[0][R] = CLAMP(0,color[R] - table59T[d],255);
- possible_colors[0][G] = CLAMP(0,color[G] - table59T[d],255);
- possible_colors[1][R] = CLAMP(0,color[R],255);
- possible_colors[1][G] = CLAMP(0,color[G],255);
- possible_colors[2][R] = CLAMP(0,color[R] + table59T[d],255);
- possible_colors[2][G] = CLAMP(0,color[G] + table59T[d],255);
- // Loop block
- for (int x = 0; x < 16; x++)
- {
- best_pixel_error = MAXIMUM_ERROR;
- // Loop possible block colors
- for (uint8 c = 0; c < 3; c++)
- {
- diff[R] = block[4*x + R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = block[4*x + G] - CLAMP(0,possible_colors[c][G],255);
- pixel_error = SQUARE(diff[R]) + SQUARE(diff[G]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- best_pixel_error = pixel_error;
- }
- precalc_err_col0_RG[((colorRGB444_packed>>4)*8 + d)*16 + x] = (unsigned int) best_pixel_error;
- }
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in exhaustive compression of the T-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError59T_col1_Rpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_R)
- {
- unsigned int pixel_error;
- int diff;
- uint8 color;
- color = ((colorRGB444_packed >> 8) & 0xf)*17;
- // Loop block
- for (int x = 0; x < 16; x++)
- {
- diff = block[4*x + R] - color;
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff);
- precalc_err_col1_R[((colorRGB444_packed>>8))*16 + x] = (unsigned int) pixel_error;
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- /**
- * Calculate the error for the block at position (startx,starty)
- * The parameters needed for reconstruction is calculated as well
- *
- * In the 59T bit mode, we only have pattern T.
- */
- void precalcError59T_col1_R(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_R)
- {
- unsigned int pixel_error;
- int diff;
- uint8 color;
- color = ((colorRGB444_packed >> 8) & 0xf)*17;
- // Loop block
- for (int x = 0; x < 16; x++)
- {
- diff = block[4*x + R] - color;
- pixel_error = SQUARE(diff);
- precalc_err_col1_R[((colorRGB444_packed>>8))*16 + x] = (unsigned int) pixel_error;
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in exhaustive compression of the T-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError59T_col1_RGpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_RG)
- {
- unsigned int pixel_error;
- int diff[3];
- uint8 color[2];
- color[R] = ((colorRGB444_packed >> 8) & 0xf)*17;
- color[G] = ((colorRGB444_packed >> 4) & 0xf)*17;
- // Loop block
- for (int x = 0; x < 16; x++)
- {
- diff[R] = block[4*x + R] - color[R];
- diff[G] = block[4*x + G] - color[G];
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]);
- precalc_err_col1_RG[((colorRGB444_packed>>4))*16 + x] = (unsigned int) pixel_error;
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in exhaustive compression of the T-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError59T_col1_RG(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_RG)
- {
- unsigned int pixel_error;
- int diff[3];
- uint8 color[2];
- color[R] = ((colorRGB444_packed >> 8) & 0xf)*17;
- color[G] = ((colorRGB444_packed >> 4) & 0xf)*17;
- // Loop block
- for (int x = 0; x < 16; x++)
- {
- diff[R] = block[4*x + R] - color[R];
- diff[G] = block[4*x + G] - color[G];
- pixel_error = SQUARE(diff[R]) + SQUARE(diff[G]);
- precalc_err_col1_RG[((colorRGB444_packed>>4))*16 + x] = (unsigned int) pixel_error;
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in exhaustive compression of the T-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError59T_col0_RGBpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_RGB)
- {
- unsigned int block_error = 0,
- best_block_error = MAXERR1000,
- pixel_error,
- best_pixel_error;
- uint8 color[3];
- int possible_colors[3][3];
- unsigned int *precalc_err_col0_RGB_adr;
- #define ONEPOINT59RGB_PERCEP(xval) \
- /* Loop possible block colors */\
- /* unroll loop for (uint8 c = 0; c < 3; c++) */\
- {\
- best_pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*square_table[block[4*xval + R] - possible_colors[0][R]]\
- + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*square_table[block[4*xval + G] - possible_colors[0][G]] \
- + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[block[4*xval + B] - possible_colors[0][B]];\
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*square_table[block[4*xval + R] - possible_colors[1][R]]\
- + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*square_table[block[4*xval + G] - possible_colors[1][G]]\
- + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[block[4*xval + B] - possible_colors[1][B]];\
- if (pixel_error < best_pixel_error)\
- best_pixel_error = pixel_error;\
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*square_table[block[4*xval + R] - possible_colors[2][R]]\
- + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*square_table[block[4*xval + G] - possible_colors[2][G]]\
- + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[block[4*xval + B] - possible_colors[2][B]];\
- if (pixel_error < best_pixel_error)\
- best_pixel_error = pixel_error;\
- }\
- precalc_err_col0_RGB_adr[xval] = (unsigned int) best_pixel_error;\
- #define ONETABLE59RGB_PERCEP(dval) \
- possible_colors[0][R] = clamp_table[color[R] - table59T[dval]+255]-255;\
- possible_colors[0][G] = clamp_table[color[G] - table59T[dval]+255]-255;\
- possible_colors[0][B] = clamp_table[color[B] - table59T[dval]+255]-255;\
- possible_colors[1][R] = color[R]-255;\
- possible_colors[1][G] = color[G]-255;\
- possible_colors[1][B] = color[B]-255;\
- possible_colors[2][R] = clamp_table[color[R] + table59T[dval]+255]-255;\
- possible_colors[2][G] = clamp_table[color[G] + table59T[dval]+255]-255;\
- possible_colors[2][B] = clamp_table[color[B] + table59T[dval]+255]-255;\
- precalc_err_col0_RGB_adr = &precalc_err_col0_RGB[(colorRGB444_packed*8 + dval)*16];\
- /* Loop block */\
- /* unroll loop for (int x = 0; x < 16; x++) */\
- {\
- ONEPOINT59RGB_PERCEP(0)\
- ONEPOINT59RGB_PERCEP(1)\
- ONEPOINT59RGB_PERCEP(2)\
- ONEPOINT59RGB_PERCEP(3)\
- ONEPOINT59RGB_PERCEP(4)\
- ONEPOINT59RGB_PERCEP(5)\
- ONEPOINT59RGB_PERCEP(6)\
- ONEPOINT59RGB_PERCEP(7)\
- ONEPOINT59RGB_PERCEP(8)\
- ONEPOINT59RGB_PERCEP(9)\
- ONEPOINT59RGB_PERCEP(10)\
- ONEPOINT59RGB_PERCEP(11)\
- ONEPOINT59RGB_PERCEP(12)\
- ONEPOINT59RGB_PERCEP(13)\
- ONEPOINT59RGB_PERCEP(14)\
- ONEPOINT59RGB_PERCEP(15)\
- }\
- color[R] = (((colorRGB444_packed >> 8) ) << 4) | ((colorRGB444_packed >> 8) ) ;
- color[G] = (((colorRGB444_packed >> 4) & 0xf) << 4) | ((colorRGB444_packed >> 4) & 0xf) ;
- color[B] = (((colorRGB444_packed) & 0xf) << 4) | ((colorRGB444_packed) & 0xf) ;
-
- /* Test all distances */
- /* unroll loop for (uint8 d = 0; d < 8; ++d) */
- {
- ONETABLE59RGB_PERCEP(0)
- ONETABLE59RGB_PERCEP(1)
- ONETABLE59RGB_PERCEP(2)
- ONETABLE59RGB_PERCEP(3)
- ONETABLE59RGB_PERCEP(4)
- ONETABLE59RGB_PERCEP(5)
- ONETABLE59RGB_PERCEP(6)
- ONETABLE59RGB_PERCEP(7)
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in exhaustive compression of the T-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError59T_col0_RGB(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_RGB)
- {
- unsigned int block_error = 0,
- best_block_error = MAXIMUM_ERROR,
- pixel_error,
- best_pixel_error;
- uint8 color[3];
- int possible_colors[3][3];
- unsigned int *precalc_err_col0_RGB_adr;
- #define ONEPOINT59RGB(xval) \
- /* Loop possible block colors */\
- /* unroll loop for (uint8 c = 0; c < 3; c++) */\
- {\
- best_pixel_error = square_table[block[4*xval + R] - possible_colors[0][R]]\
- + square_table[block[4*xval + G] - possible_colors[0][G]] \
- + square_table[block[4*xval + B] - possible_colors[0][B]];\
- pixel_error = square_table[block[4*xval + R] - possible_colors[1][R]]\
- + square_table[block[4*xval + G] - possible_colors[1][G]]\
- + square_table[block[4*xval + B] - possible_colors[1][B]];\
- if (pixel_error < best_pixel_error)\
- best_pixel_error = pixel_error;\
- pixel_error = square_table[block[4*xval + R] - possible_colors[2][R]]\
- + square_table[block[4*xval + G] - possible_colors[2][G]]\
- + square_table[block[4*xval + B] - possible_colors[2][B]];\
- if (pixel_error < best_pixel_error)\
- best_pixel_error = pixel_error;\
- }\
- precalc_err_col0_RGB_adr[xval] = (unsigned int) best_pixel_error;\
- #define ONETABLE59RGB(dval) \
- possible_colors[0][R] = clamp_table[color[R] - table59T[dval]+255]-255;\
- possible_colors[0][G] = clamp_table[color[G] - table59T[dval]+255]-255;\
- possible_colors[0][B] = clamp_table[color[B] - table59T[dval]+255]-255;\
- possible_colors[1][R] = color[R]-255;\
- possible_colors[1][G] = color[G]-255;\
- possible_colors[1][B] = color[B]-255;\
- possible_colors[2][R] = clamp_table[color[R] + table59T[dval]+255]-255;\
- possible_colors[2][G] = clamp_table[color[G] + table59T[dval]+255]-255;\
- possible_colors[2][B] = clamp_table[color[B] + table59T[dval]+255]-255;\
- precalc_err_col0_RGB_adr = &precalc_err_col0_RGB[(colorRGB444_packed*8 + dval)*16];\
- /* Loop block */\
- /* unroll loop for (int x = 0; x < 16; x++) */\
- {\
- ONEPOINT59RGB(0)\
- ONEPOINT59RGB(1)\
- ONEPOINT59RGB(2)\
- ONEPOINT59RGB(3)\
- ONEPOINT59RGB(4)\
- ONEPOINT59RGB(5)\
- ONEPOINT59RGB(6)\
- ONEPOINT59RGB(7)\
- ONEPOINT59RGB(8)\
- ONEPOINT59RGB(9)\
- ONEPOINT59RGB(10)\
- ONEPOINT59RGB(11)\
- ONEPOINT59RGB(12)\
- ONEPOINT59RGB(13)\
- ONEPOINT59RGB(14)\
- ONEPOINT59RGB(15)\
- }\
- color[R] = (((colorRGB444_packed >> 8) ) << 4) | ((colorRGB444_packed >> 8) ) ;
- color[G] = (((colorRGB444_packed >> 4) & 0xf) << 4) | ((colorRGB444_packed >> 4) & 0xf) ;
- color[B] = (((colorRGB444_packed) & 0xf) << 4) | ((colorRGB444_packed) & 0xf) ;
- /* Test all distances */
- /* unroll loop for (uint8 d = 0; d < 8; ++d) */
- {
- ONETABLE59RGB(0)
- ONETABLE59RGB(1)
- ONETABLE59RGB(2)
- ONETABLE59RGB(3)
- ONETABLE59RGB(4)
- ONETABLE59RGB(5)
- ONETABLE59RGB(6)
- ONETABLE59RGB(7)
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in exhaustive compression of the T-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError59T_col1_RGBpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_RGB)
- {
- unsigned int pixel_error;
- int diff[3];
- uint8 colorRGB[3];
- colorRGB[0] = ((colorRGB444_packed >> 8) & 0xf)*17;
- colorRGB[1] = ((colorRGB444_packed >> 4) & 0xf)*17;
- colorRGB[2] = ((colorRGB444_packed >> 0) & 0xf)*17;
- // Loop block
- for (int x = 0; x < 16; x++)
- {
- diff[R] = block[4*x + R] - colorRGB[R];
- diff[G] = block[4*x + G] - colorRGB[G];
- diff[B] = block[4*x + B] - colorRGB[B];
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]) + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*SQUARE(diff[B]);
-
- precalc_err_col1_RGB[(colorRGB444_packed)*16 + x] = (unsigned int) pixel_error;
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in exhaustive compression of the T-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError59T_col1_RGB(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_RGB)
- {
- unsigned int pixel_error;
- int diff[3];
- uint8 colorRGB[3];
- colorRGB[0] = ((colorRGB444_packed >> 8) & 0xf)*17;
- colorRGB[1] = ((colorRGB444_packed >> 4) & 0xf)*17;
- colorRGB[2] = ((colorRGB444_packed >> 0) & 0xf)*17;
- // Loop block
- for (int x = 0; x < 16; x++)
- {
- diff[R] = block[4*x + R] - colorRGB[R];
- diff[G] = block[4*x + G] - colorRGB[G];
- diff[B] = block[4*x + B] - colorRGB[B];
- pixel_error = SQUARE(diff[R]) + SQUARE(diff[G]) + SQUARE(diff[B]);
- precalc_err_col1_RGB[(colorRGB444_packed)*16 + x] = (unsigned int) pixel_error;
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimal error for the T-mode when compressing exhaustively.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateError59TusingPrecalcRperceptual1000(uint8* block, int *colorsRGB444_packed, unsigned int *precalc_err_col0_R, unsigned int *precalc_err_col1_R, unsigned int best_error_so_far)
- {
- unsigned int block_error = 0,
- best_block_error = MAXERR1000;
- unsigned int *pixel_error_col0_base_adr;
- unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
- #define FIRSTCHOICE59R_PERCEP\
- if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
- block_error = *pixel_error_col0_adr;\
- else\
- block_error = *pixel_error_col1_adr;\
- #define CHOICE59R_PERCEP(xval)\
- if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
- block_error += pixel_error_col0_adr[xval];\
- else\
- block_error += pixel_error_col1_adr[xval];\
- #define ONETABLE59R_PERCEP(dval) \
- pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
- /* unroll loop for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
- {\
- FIRSTCHOICE59R_PERCEP\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R_PERCEP(1)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R_PERCEP(2)\
- CHOICE59R_PERCEP(3)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R_PERCEP(4)\
- CHOICE59R_PERCEP(5)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R_PERCEP(6)\
- CHOICE59R_PERCEP(7)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R_PERCEP(8)\
- CHOICE59R_PERCEP(9)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R_PERCEP(10)\
- CHOICE59R_PERCEP(11)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R_PERCEP(12)\
- CHOICE59R_PERCEP(13)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R_PERCEP(14)\
- CHOICE59R_PERCEP(15)\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- if (block_error < best_block_error)\
- best_block_error = block_error;\
-
- pixel_error_col0_base_adr = &precalc_err_col0_R[((colorsRGB444_packed[0]>>8)*8)*16];
- pixel_error_col1_adr = &precalc_err_col1_R[((colorsRGB444_packed[1]>>8))*16];
- // Test all distances
- /* unroll loop for (uint8 d = 0; d < 8; d++) */
- {
- ONETABLE59R_PERCEP(0)
- ONETABLE59R_PERCEP(1)
- ONETABLE59R_PERCEP(2)
- ONETABLE59R_PERCEP(3)
- ONETABLE59R_PERCEP(4)
- ONETABLE59R_PERCEP(5)
- ONETABLE59R_PERCEP(6)
- ONETABLE59R_PERCEP(7)
- }
- return best_block_error;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimal error for the T-mode when compressing exhaustively.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateError59TusingPrecalcR(uint8* block, int *colorsRGB444_packed, unsigned int *precalc_err_col0_R, unsigned int *precalc_err_col1_R, unsigned int best_error_so_far)
- {
- unsigned int block_error = 0,
- best_block_error = MAXIMUM_ERROR;
- unsigned int *pixel_error_col0_base_adr;
- unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
- #define FIRSTCHOICE59R\
- if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
- block_error = *pixel_error_col0_adr;\
- else\
- block_error = *pixel_error_col1_adr;\
- #define CHOICE59R(xval)\
- if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
- block_error += pixel_error_col0_adr[xval];\
- else\
- block_error += pixel_error_col1_adr[xval];\
- #define ONETABLE59R(dval) \
- pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
- /* unroll loop for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
- {\
- FIRSTCHOICE59R\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R(1)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R(2)\
- CHOICE59R(3)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R(4)\
- CHOICE59R(5)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R(6)\
- CHOICE59R(7)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R(8)\
- CHOICE59R(9)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R(10)\
- CHOICE59R(11)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R(12)\
- CHOICE59R(13)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59R(14)\
- CHOICE59R(15)\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- if (block_error < best_block_error)\
- best_block_error = block_error;\
-
- pixel_error_col0_base_adr = &precalc_err_col0_R[((colorsRGB444_packed[0]>>8)*8)*16];
- pixel_error_col1_adr = &precalc_err_col1_R[((colorsRGB444_packed[1]>>8))*16];
- // Test all distances
- /* unroll loop for (uint8 d = 0; d < 8; d++) */
- {
- ONETABLE59R(0)
- ONETABLE59R(1)
- ONETABLE59R(2)
- ONETABLE59R(3)
- ONETABLE59R(4)
- ONETABLE59R(5)
- ONETABLE59R(6)
- ONETABLE59R(7)
- }
-
- return best_block_error;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimal error for the T-mode when compressing exhaustively.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateError59TusingPrecalcRGperceptual1000(uint8* block, int *colorsRGB444_packed, unsigned int *precalc_err_col0_RG, unsigned int *precalc_err_col1_RG, unsigned int best_error_so_far)
- {
- unsigned int block_error = 0,
- best_block_error = MAXERR1000;
- unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
- unsigned int *pixel_error_col0_base_adr;
- #define FIRSTCHOICE59RG_PERCEP \
- if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
- block_error = *pixel_error_col0_adr;\
- else\
- block_error = *pixel_error_col1_adr;\
- #define CHOICE59RG_PERCEP(xval) \
- if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
- block_error += pixel_error_col0_adr[xval];\
- else\
- block_error += pixel_error_col1_adr[xval];\
- #define ONETABLE59RG_PERCEP(dval)\
- pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
- /* unroll loop for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
- {\
- FIRSTCHOICE59RG_PERCEP\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG_PERCEP(1)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG_PERCEP(2)\
- CHOICE59RG_PERCEP(3)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG_PERCEP(4)\
- CHOICE59RG_PERCEP(5)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG_PERCEP(6)\
- CHOICE59RG_PERCEP(7)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG_PERCEP(8)\
- CHOICE59RG_PERCEP(9)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG_PERCEP(10)\
- CHOICE59RG_PERCEP(11)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG_PERCEP(12)\
- CHOICE59RG_PERCEP(13)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG_PERCEP(14)\
- CHOICE59RG_PERCEP(15)\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- if (block_error < best_block_error)\
- best_block_error = block_error;\
- pixel_error_col0_base_adr = &precalc_err_col0_RG[((colorsRGB444_packed[0]>>4)*8)*16];
- pixel_error_col1_adr = &precalc_err_col1_RG[((colorsRGB444_packed[1]>>4))*16];
- // Test all distances
- /* unroll loop for (uint8 d = 0; d < 8; d++) */
- {
- ONETABLE59RG_PERCEP(0)
- ONETABLE59RG_PERCEP(1)
- ONETABLE59RG_PERCEP(2)
- ONETABLE59RG_PERCEP(3)
- ONETABLE59RG_PERCEP(4)
- ONETABLE59RG_PERCEP(5)
- ONETABLE59RG_PERCEP(6)
- ONETABLE59RG_PERCEP(7)
- }
- return best_block_error;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimal error for the T-mode when compressing exhaustively.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateError59TusingPrecalcRG(uint8* block, int *colorsRGB444_packed, unsigned int *precalc_err_col0_RG, unsigned int *precalc_err_col1_RG, unsigned int best_error_so_far)
- {
- unsigned int block_error = 0,
- best_block_error = MAXIMUM_ERROR;
- unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
- unsigned int *pixel_error_col0_base_adr;
- #define FIRSTCHOICE59RG \
- if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
- block_error = *pixel_error_col0_adr;\
- else\
- block_error = *pixel_error_col1_adr;\
- #define CHOICE59RG(xval) \
- if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
- block_error += pixel_error_col0_adr[xval];\
- else\
- block_error += pixel_error_col1_adr[xval];\
- #define ONETABLE59RG(dval)\
- pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
- /* unroll loop for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
- {\
- FIRSTCHOICE59RG\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG(1)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG(2)\
- CHOICE59RG(3)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG(4)\
- CHOICE59RG(5)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG(6)\
- CHOICE59RG(7)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG(8)\
- CHOICE59RG(9)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG(10)\
- CHOICE59RG(11)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG(12)\
- CHOICE59RG(13)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59RG(14)\
- CHOICE59RG(15)\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- if (block_error < best_block_error)\
- best_block_error = block_error;\
- pixel_error_col0_base_adr = &precalc_err_col0_RG[((colorsRGB444_packed[0]>>4)*8)*16];
- pixel_error_col1_adr = &precalc_err_col1_RG[((colorsRGB444_packed[1]>>4))*16];
- // Test all distances
- /* unroll loop for (uint8 d = 0; d < 8; d++) */
- {
- ONETABLE59RG(0)
- ONETABLE59RG(1)
- ONETABLE59RG(2)
- ONETABLE59RG(3)
- ONETABLE59RG(4)
- ONETABLE59RG(5)
- ONETABLE59RG(6)
- ONETABLE59RG(7)
- }
- return best_block_error;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimal error for the T-mode when compressing exhaustively.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateError59TusingPrecalcRGBperceptual1000(uint8* block, int *colorsRGB444_packed, unsigned int *precalc_err_col0_RGB, unsigned int *precalc_err_col1_RGB, unsigned int best_error_so_far)
- {
- unsigned int block_error = 0,
- best_block_error = MAXERR1000;
- unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
- unsigned int *pixel_error_col0_base_adr;
- #define FIRSTCHOICE59_PERCEP \
- if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
- block_error = *pixel_error_col0_adr;\
- else\
- block_error = *pixel_error_col1_adr;\
- #define CHOICE59_PERCEP(xval) \
- if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
- block_error += pixel_error_col0_adr[xval];\
- else\
- block_error += pixel_error_col1_adr[xval];\
- #define ONETABLE59T_PERCEP(dval)\
- pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
- /* unroll for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
- {\
- FIRSTCHOICE59_PERCEP\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59_PERCEP(1)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59_PERCEP(2)\
- CHOICE59_PERCEP(3)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59_PERCEP(4)\
- CHOICE59_PERCEP(5)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59_PERCEP(6)\
- CHOICE59_PERCEP(7)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59_PERCEP(8)\
- CHOICE59_PERCEP(9)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59_PERCEP(10)\
- CHOICE59_PERCEP(11)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59_PERCEP(12)\
- CHOICE59_PERCEP(13)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59_PERCEP(14)\
- CHOICE59_PERCEP(15)\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- if (block_error < best_block_error)\
- best_block_error = block_error;\
- pixel_error_col1_adr = &precalc_err_col1_RGB[(colorsRGB444_packed[1])*16];
- pixel_error_col0_base_adr = &precalc_err_col0_RGB[(colorsRGB444_packed[0]*8)*16];
- // Test all distances
- /* unroll loop for (uint8 d = 0; d < 8; d++)*/
- {
- ONETABLE59T_PERCEP(0)
- ONETABLE59T_PERCEP(1)
- ONETABLE59T_PERCEP(2)
- ONETABLE59T_PERCEP(3)
- ONETABLE59T_PERCEP(4)
- ONETABLE59T_PERCEP(5)
- ONETABLE59T_PERCEP(6)
- ONETABLE59T_PERCEP(7)
- }
- return best_block_error;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimal error for the T-mode when compressing exhaustively.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateError59TusingPrecalcRGB(uint8* block, int *colorsRGB444_packed, unsigned int *precalc_err_col0_RGB, unsigned int *precalc_err_col1_RGB, unsigned int best_error_so_far)
- {
- unsigned int block_error = 0,
- best_block_error = MAXIMUM_ERROR;
- unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
- unsigned int *pixel_error_col0_base_adr;
- #define FIRSTCHOICE59 \
- if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
- block_error = *pixel_error_col0_adr;\
- else\
- block_error = *pixel_error_col1_adr;\
- #define CHOICE59(xval) \
- if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
- block_error += pixel_error_col0_adr[xval];\
- else\
- block_error += pixel_error_col1_adr[xval];\
- #define ONETABLE59T(dval)\
- pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
- /* unroll for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
- {\
- FIRSTCHOICE59\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59(1)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59(2)\
- CHOICE59(3)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59(4)\
- CHOICE59(5)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59(6)\
- CHOICE59(7)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59(8)\
- CHOICE59(9)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59(10)\
- CHOICE59(11)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59(12)\
- CHOICE59(13)\
- if( block_error < best_error_so_far)\
- {\
- CHOICE59(14)\
- CHOICE59(15)\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- if (block_error < best_block_error)\
- best_block_error = block_error;\
- pixel_error_col1_adr = &precalc_err_col1_RGB[(colorsRGB444_packed[1])*16];
- pixel_error_col0_base_adr = &precalc_err_col0_RGB[(colorsRGB444_packed[0]*8)*16];
- // Test all distances
- /* unroll loop for (uint8 d = 0; d < 8; d++)*/
- {
- ONETABLE59T(0)
- ONETABLE59T(1)
- ONETABLE59T(2)
- ONETABLE59T(3)
- ONETABLE59T(4)
- ONETABLE59T(5)
- ONETABLE59T(6)
- ONETABLE59T(7)
- }
- return best_block_error;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // The below code should compress the block to 59 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- //
- //|63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
- //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // Note that this method might not return the best possible compression for the T-mode. It will only do so if the best possible T-representation
- // is less than best_error_so_far. To guarantee that the best possible T-representation is found, the function should be called using
- // best_error_so_far = 255*255*3*16, which is the maximum error for a block.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockTHUMB59TExhaustivePerceptual(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, unsigned int best_error_so_far)
- {
- uint8 colorsRGB444[2][3];
- unsigned int pixel_indices;
- uint8 distance;
- uint8 block[4*4*4];
- unsigned int *precalc_err_col0_RGB;
- unsigned int *precalc_err_col1_RGB;
- unsigned int *precalc_err_col0_RG;
- unsigned int *precalc_err_col1_RG;
- unsigned int *precalc_err_col0_R;
- unsigned int *precalc_err_col1_R;
- int colorRGB444_packed;
- int colorsRGB444_packed[2];
- int best_colorsRGB444_packed[2];
- unsigned int best_error_using_Tmode;
- // First compress block quickly to a resonable quality so that we can
- // rule out all blocks that are of worse quality than that.
- best_error_using_Tmode = (unsigned int) compressBlockTHUMB59TFastestOnlyColorPerceptual1000(img, width, height, startx, starty, best_colorsRGB444_packed);
- if(best_error_using_Tmode < best_error_so_far)
- best_error_so_far = best_error_using_Tmode;
- // Color numbering is reversed between the above function and the precalc functions below; swap colors.
- int temp = best_colorsRGB444_packed[0];
- best_colorsRGB444_packed[0] = best_colorsRGB444_packed[1];
- best_colorsRGB444_packed[1] = temp;
- int xx,yy,count = 0;
- // Use 4 bytes per pixel to make it 32-word aligned.
- for(xx = 0; xx<4; xx++)
- {
- for(yy=0; yy<4; yy++)
- {
- block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block[(count)*4+3] = 0;
- count++;
- }
- }
- // Precalculate error for color 0 (which produces the upper half of the T)
- precalc_err_col0_RGB = (unsigned int*) malloc(4096*8*16*sizeof(unsigned int));
- if(!precalc_err_col0_RGB){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
- {
- precalcError59T_col0_RGBpercep1000(block, colorRGB444_packed, precalc_err_col0_RGB);
- }
- // Precalculate error for color 1 (which produces the lower half of the T -- the lone color)
- precalc_err_col1_RGB = (unsigned int*) malloc(4096*16*sizeof(unsigned int));
- if(!precalc_err_col1_RGB){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
- {
- precalcError59T_col1_RGBpercep1000(block, colorRGB444_packed, precalc_err_col1_RGB);
- }
- precalc_err_col0_RG = (unsigned int*) malloc(16*16*8*16*sizeof(unsigned int));
- if(!precalc_err_col0_RG){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
- {
- precalcError59T_col0_RGpercep1000(block, colorRGB444_packed, precalc_err_col0_RG);
- }
- precalc_err_col1_RG = (unsigned int*) malloc(16*16*16*sizeof(unsigned int));
- if(!precalc_err_col1_RG){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
- {
- precalcError59T_col1_RGpercep1000(block, colorRGB444_packed, precalc_err_col1_RG);
- }
- precalc_err_col0_R = (unsigned int*) malloc(16*8*16*sizeof(unsigned int));
- if(!precalc_err_col0_R){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
- {
- precalcError59T_col0_Rpercep1000(block, colorRGB444_packed, precalc_err_col0_R);
- }
- precalc_err_col1_R = (unsigned int*) malloc(16*16*sizeof(unsigned int));
- if(!precalc_err_col1_R){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
- {
- precalcError59T_col1_Rpercep1000(block, colorRGB444_packed, precalc_err_col1_R);
- }
- unsigned int error;
- unsigned int avoided = 0;
- unsigned int notavoided = 0;
- for(colorsRGB444[0][0] = 0; colorsRGB444[0][0] < 16; colorsRGB444[0][0]++)
- {
- for(colorsRGB444[1][0] = 0; colorsRGB444[1][0] < 16; colorsRGB444[1][0]++)
- {
- colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8);
- colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8);
- error = calculateError59TusingPrecalcRperceptual1000(block, colorsRGB444_packed, precalc_err_col0_R, precalc_err_col1_R, best_error_so_far);
- if(error < best_error_so_far)
- {
- notavoided = notavoided + 1;
- for(colorsRGB444[0][1] = 0; colorsRGB444[0][1] < 16; colorsRGB444[0][1]++)
- {
- colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8) + (colorsRGB444[0][1] <<4);
- for(colorsRGB444[1][1] = 0; colorsRGB444[1][1] < 16; colorsRGB444[1][1]++)
- {
- colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8) + (colorsRGB444[1][1] <<4);
- error = calculateError59TusingPrecalcRGperceptual1000(block, colorsRGB444_packed, precalc_err_col0_RG, precalc_err_col1_RG, best_error_so_far);
- if(error < best_error_so_far)
- {
- for(colorsRGB444[0][2] = 0; colorsRGB444[0][2] < 16; colorsRGB444[0][2]++)
- {
- colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8) + (colorsRGB444[0][1] <<4) + colorsRGB444[0][2];
- for(colorsRGB444[1][2] = 0; colorsRGB444[1][2] < 16; colorsRGB444[1][2]++)
- {
- colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8) + (colorsRGB444[1][1] <<4) + colorsRGB444[1][2];
- error = calculateError59TusingPrecalcRGBperceptual1000(block, colorsRGB444_packed, precalc_err_col0_RGB, precalc_err_col1_RGB, best_error_so_far);
- if(error < best_error_so_far)
- {
- best_error_so_far = error;
- best_error_using_Tmode = error;
- best_colorsRGB444_packed[0] = colorsRGB444_packed[0];
- best_colorsRGB444_packed[1] = colorsRGB444_packed[1];
- }
- }
- }
- }
- }
- }
- }
- }
- }
- free(precalc_err_col0_RGB);
- free(precalc_err_col1_RGB);
- free(precalc_err_col0_RG);
- free(precalc_err_col1_RG);
- free(precalc_err_col0_R);
- free(precalc_err_col1_R);
- // We have got the two best colors. Now find the best distance and pixel indices.
- // Color numbering are reversed between precalc and noSwap
- colorsRGB444[0][0] = (best_colorsRGB444_packed[1] >> 8) & 0xf;
- colorsRGB444[0][1] = (best_colorsRGB444_packed[1] >> 4) & 0xf;
- colorsRGB444[0][2] = (best_colorsRGB444_packed[1] >> 0) & 0xf;
-
- colorsRGB444[1][0] = (best_colorsRGB444_packed[0] >> 8) & 0xf;
- colorsRGB444[1][1] = (best_colorsRGB444_packed[0] >> 4) & 0xf;
- colorsRGB444[1][2] = (best_colorsRGB444_packed[0] >> 0) & 0xf;
- calculateError59TnoSwapPerceptual1000(img, width, startx, starty, colorsRGB444, distance, pixel_indices);
- // Put the compress params into the compression block
- packBlock59T(colorsRGB444, distance, pixel_indices, compressed1, compressed2);
- return best_error_using_Tmode;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // The below code should compress the block to 59 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- //
- //|63 62 61 60 59|58 57 56 55|54 53 52 51|50 49 48 47|46 45 44 43|42 41 40 39|38 37 36 35|34 33 32|
- //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // Note that this method might not return the best possible compression for the T-mode. It will only do so if the best possible T-representation
- // is less than best_error_so_far. To guarantee that the best possible T-representation is found, the function should be called using
- // best_error_so_far = 255*255*3*16, which is the maximum error for a block.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockTHUMB59TExhaustive(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, unsigned int best_error_so_far)
- {
- uint8 colorsRGB444[2][3];
- unsigned int pixel_indices;
- uint8 distance;
- uint8 block[4*4*4];
- unsigned int *precalc_err_col0_RGB;
- unsigned int *precalc_err_col1_RGB;
- unsigned int *precalc_err_col0_RG;
- unsigned int *precalc_err_col1_RG;
- unsigned int *precalc_err_col0_R;
- unsigned int *precalc_err_col1_R;
- int colorRGB444_packed;
- int colorsRGB444_packed[2];
- int best_colorsRGB444_packed[2];
- unsigned int best_error_using_Tmode;
- // First compress block quickly to a resonable quality so that we can
- // rule out all blocks that are of worse quality than that.
- best_error_using_Tmode = (unsigned int) compressBlockTHUMB59TFastestOnlyColor(img, width, height, startx, starty, best_colorsRGB444_packed);
- if(best_error_using_Tmode < best_error_so_far)
- best_error_so_far = best_error_using_Tmode;
- // Colors numbering is reversed between the above function and the precalc below:
- int temp = best_colorsRGB444_packed[0];
- best_colorsRGB444_packed[0] = best_colorsRGB444_packed[1];
- best_colorsRGB444_packed[1] = temp;
- int xx,yy,count = 0;
- // Use 4 bytes per pixel to make it 32-word aligned.
- for(xx = 0; xx<4; xx++)
- {
- for(yy=0; yy<4; yy++)
- {
- block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block[(count)*4+3] = 0;
- count++;
- }
- }
- // Precalculate error for color 0 (which produces the upper half of the T)
- precalc_err_col0_RGB = (unsigned int*) malloc(4096*8*16*sizeof(unsigned int));
- if(!precalc_err_col0_RGB){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
- {
- precalcError59T_col0_RGB(block, colorRGB444_packed, precalc_err_col0_RGB);
- }
- // Precalculate error for color 1 (which produces the lower half of the T -- the lone color)
- precalc_err_col1_RGB = (unsigned int*) malloc(4096*16*sizeof(unsigned int));
- if(!precalc_err_col1_RGB){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
- {
- precalcError59T_col1_RGB(block, colorRGB444_packed, precalc_err_col1_RGB);
- }
- precalc_err_col0_RG = (unsigned int*) malloc(16*16*8*16*sizeof(unsigned int));
- if(!precalc_err_col0_RG){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
- {
- precalcError59T_col0_RG(block, colorRGB444_packed, precalc_err_col0_RG);
- }
- precalc_err_col1_RG = (unsigned int*) malloc(16*16*16*sizeof(unsigned int));
- if(!precalc_err_col1_RG){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
- {
- precalcError59T_col1_RG(block, colorRGB444_packed, precalc_err_col1_RG);
- }
- precalc_err_col0_R = (unsigned int*) malloc(16*8*16*sizeof(unsigned int));
- if(!precalc_err_col0_R){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
- {
- precalcError59T_col0_R(block, colorRGB444_packed, precalc_err_col0_R);
- }
- precalc_err_col1_R = (unsigned int*) malloc(16*16*sizeof(unsigned int));
- if(!precalc_err_col1_R){printf("Out of memory allocating \n");exit(1);}
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
- {
- precalcError59T_col1_R(block, colorRGB444_packed, precalc_err_col1_R);
- }
- unsigned int error;
- unsigned int avoided = 0;
- unsigned int notavoided = 0;
- for(colorsRGB444[0][0] = 0; colorsRGB444[0][0] < 16; colorsRGB444[0][0]++)
- {
- for(colorsRGB444[1][0] = 0; colorsRGB444[1][0] < 16; colorsRGB444[1][0]++)
- {
- colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8);
- colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8);
- error = calculateError59TusingPrecalcR(block, colorsRGB444_packed, precalc_err_col0_R, precalc_err_col1_R, best_error_so_far);
- if(error < best_error_so_far)
- {
- notavoided = notavoided + 1;
- for(colorsRGB444[0][1] = 0; colorsRGB444[0][1] < 16; colorsRGB444[0][1]++)
- {
- colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8) + (colorsRGB444[0][1] <<4);
- for(colorsRGB444[1][1] = 0; colorsRGB444[1][1] < 16; colorsRGB444[1][1]++)
- {
- colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8) + (colorsRGB444[1][1] <<4);
- error = calculateError59TusingPrecalcRG(block, colorsRGB444_packed, precalc_err_col0_RG, precalc_err_col1_RG, best_error_so_far);
- if(error < best_error_so_far)
- {
- for(colorsRGB444[0][2] = 0; colorsRGB444[0][2] < 16; colorsRGB444[0][2]++)
- {
- colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8) + (colorsRGB444[0][1] <<4) + colorsRGB444[0][2];
- for(colorsRGB444[1][2] = 0; colorsRGB444[1][2] < 16; colorsRGB444[1][2]++)
- {
- colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8) + (colorsRGB444[1][1] <<4) + colorsRGB444[1][2];
- error = calculateError59TusingPrecalcRGB(block, colorsRGB444_packed, precalc_err_col0_RGB, precalc_err_col1_RGB, best_error_so_far);
- if(error < best_error_so_far)
- {
- best_error_so_far = error;
- best_error_using_Tmode = error;
- best_colorsRGB444_packed[0] = colorsRGB444_packed[0];
- best_colorsRGB444_packed[1] = colorsRGB444_packed[1];
- }
- }
- }
- }
- }
- }
- }
- }
- }
- free(precalc_err_col0_RGB);
- free(precalc_err_col1_RGB);
- free(precalc_err_col0_RG);
- free(precalc_err_col1_RG);
- free(precalc_err_col0_R);
- free(precalc_err_col1_R);
- // We have got the two best colors. Now find the best distance and pixel indices.
- // Color numbering are reversed between precalc and noSwap
- colorsRGB444[0][0] = (best_colorsRGB444_packed[1] >> 8) & 0xf;
- colorsRGB444[0][1] = (best_colorsRGB444_packed[1] >> 4) & 0xf;
- colorsRGB444[0][2] = (best_colorsRGB444_packed[1] >> 0) & 0xf;
-
- colorsRGB444[1][0] = (best_colorsRGB444_packed[0] >> 8) & 0xf;
- colorsRGB444[1][1] = (best_colorsRGB444_packed[0] >> 4) & 0xf;
- colorsRGB444[1][2] = (best_colorsRGB444_packed[0] >> 0) & 0xf;
- calculateError59TnoSwap(img, width, startx, starty, colorsRGB444, distance, pixel_indices);
- // Put the compress params into the compression block
- packBlock59T(colorsRGB444, distance, pixel_indices, compressed1, compressed2);
- return best_error_using_Tmode;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates tables used in the exhaustive compression of the H-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcErrorR_58Hperceptual1000(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_errR)
- {
- unsigned int block_error = 0,
- best_block_error = MAXERR1000,
- pixel_error,
- best_pixel_error;
- int diff[3];
- unsigned int pixel_colors;
- uint8 possible_colors[2][3];
- uint8 colors[2][3];
-
- decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
- {
- possible_colors[0][R] = CLAMP(0,colors[0][R] - table58H[d],255);
- possible_colors[1][R] = CLAMP(0,colors[0][R] + table58H[d],255);
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- best_pixel_error = MAXERR1000;
- // Loop possible block colors
- for (uint8 c = 0; c < 2; ++c)
- {
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- }
- }
- precalc_errR[((colorRGB444_packed>>8)*8 + d)*16 + (y*4)+x] = (unsigned int) best_pixel_error;
- }
- }
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates tables used in the exhaustive compression of the H-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcErrorR_58H(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_errR)
- {
- double block_error = 0,
- best_block_error = MAXIMUM_ERROR,
- pixel_error,
- best_pixel_error;
- int diff[3];
- unsigned int pixel_colors;
- uint8 possible_colors[2][3];
- uint8 colors[2][3];
-
- decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
- {
- possible_colors[0][R] = CLAMP(0,colors[0][R] - table58H[d],255);
- possible_colors[1][R] = CLAMP(0,colors[0][R] + table58H[d],255);
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- best_pixel_error = MAXIMUM_ERROR;
- // Loop possible block colors
- for (uint8 c = 0; c < 2; ++c)
- {
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- pixel_error = weight[R]*SQUARE(diff[R]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- }
- }
- precalc_errR[((colorRGB444_packed>>8)*8 + d)*16 + (y*4)+x] = (unsigned int) best_pixel_error;
- }
- }
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates tables used in the exhaustive compression of the H-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcErrorRG_58Hperceptual1000(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_errRG)
- {
- unsigned int block_error = 0,
- best_block_error = MAXERR1000,
- pixel_error,
- best_pixel_error;
- int diff[3];
- unsigned int pixel_colors;
- uint8 possible_colors[2][3];
- uint8 colors[2][3];
-
- decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
- {
- possible_colors[0][R] = CLAMP(0,colors[0][R] - table58H[d],255);
- possible_colors[0][G] = CLAMP(0,colors[0][G] - table58H[d],255);
- possible_colors[1][R] = CLAMP(0,colors[0][R] + table58H[d],255);
- possible_colors[1][G] = CLAMP(0,colors[0][G] + table58H[d],255);
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- best_pixel_error = MAXERR1000;
- // Loop possible block colors
- for (uint8 c = 0; c < 2; ++c)
- {
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) +
- PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- }
- }
- precalc_errRG[((colorRGB444_packed>>4)*8 + d)*16 + (y*4)+x] = (unsigned int) best_pixel_error;
- }
- }
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates tables used in the exhaustive compression of the H-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcErrorRG_58H(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_errRG)
- {
- double block_error = 0,
- best_block_error = MAXIMUM_ERROR,
- pixel_error,
- best_pixel_error;
- int diff[3];
- unsigned int pixel_colors;
- uint8 possible_colors[2][3];
- uint8 colors[2][3];
-
- decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
- // Test all distances
- for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
- {
- possible_colors[0][R] = CLAMP(0,colors[0][R] - table58H[d],255);
- possible_colors[0][G] = CLAMP(0,colors[0][G] - table58H[d],255);
- possible_colors[1][R] = CLAMP(0,colors[0][R] + table58H[d],255);
- possible_colors[1][G] = CLAMP(0,colors[0][G] + table58H[d],255);
- block_error = 0;
- pixel_colors = 0;
- // Loop block
- for (size_t y = 0; y < BLOCKHEIGHT; ++y)
- {
- for (size_t x = 0; x < BLOCKWIDTH; ++x)
- {
- best_pixel_error = MAXIMUM_ERROR;
- // Loop possible block colors
- for (uint8 c = 0; c < 2; ++c)
- {
- diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
- diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
- pixel_error = weight[R]*SQUARE(diff[R]) +
- weight[G]*SQUARE(diff[G]);
- // Choose best error
- if (pixel_error < best_pixel_error)
- {
- best_pixel_error = pixel_error;
- }
- }
- precalc_errRG[((colorRGB444_packed>>4)*8 + d)*16 + (y*4)+x] = (unsigned int) best_pixel_error;
- }
- }
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in the exhaustive compression of the H-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError58Hperceptual1000(uint8* block, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_err)
- {
- unsigned int pixel_error,
- best_pixel_error;
- int possible_colors[2][3];
- uint8 colors[2][3];
- unsigned int *precalc_err_tab;
- int red_original;
- int green_original;
- int blue_original;
- #define PRECALC_ONE_58H_PERCEP(qvalue)\
- red_original = block[qvalue*4];\
- green_original = block[qvalue*4+1];\
- blue_original = block[qvalue*4+2];\
- /* unroll loop for (color = 0; color< 2; color++) */\
- best_pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*square_table[(possible_colors[0][R] - red_original)] \
- + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*square_table[(possible_colors[0][G] - green_original)]\
- + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[(possible_colors[0][B] - blue_original)];\
- pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*square_table[(possible_colors[1][R] - red_original)]\
- + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*square_table[(possible_colors[1][G] - green_original)]\
- + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[(possible_colors[1][B] - blue_original)];\
- if (pixel_error < best_pixel_error)\
- best_pixel_error = pixel_error;\
- /* end unroll loop */\
- precalc_err_tab[qvalue] = best_pixel_error;\
- #define PRECALC_ONE_TABLE_58H_PERCEP(dvalue)\
- precalc_err_tab = &precalc_err[((colorRGB444_packed*8)+dvalue)*16];\
- possible_colors[0][R] = CLAMP_LEFT_ZERO(colors[0][R] - table58H[dvalue])+255;\
- possible_colors[0][G] = CLAMP_LEFT_ZERO(colors[0][G] - table58H[dvalue])+255;\
- possible_colors[0][B] = CLAMP_LEFT_ZERO(colors[0][B] - table58H[dvalue])+255;\
- possible_colors[1][R] = CLAMP_RIGHT_255(colors[0][R] + table58H[dvalue])+255;\
- possible_colors[1][G] = CLAMP_RIGHT_255(colors[0][G] + table58H[dvalue])+255;\
- possible_colors[1][B] = CLAMP_RIGHT_255(colors[0][B] + table58H[dvalue])+255;\
- /* unrolled loop for(q = 0; q<16; q++)*/\
- PRECALC_ONE_58H_PERCEP(0)\
- PRECALC_ONE_58H_PERCEP(1)\
- PRECALC_ONE_58H_PERCEP(2)\
- PRECALC_ONE_58H_PERCEP(3)\
- PRECALC_ONE_58H_PERCEP(4)\
- PRECALC_ONE_58H_PERCEP(5)\
- PRECALC_ONE_58H_PERCEP(6)\
- PRECALC_ONE_58H_PERCEP(7)\
- PRECALC_ONE_58H_PERCEP(8)\
- PRECALC_ONE_58H_PERCEP(9)\
- PRECALC_ONE_58H_PERCEP(10)\
- PRECALC_ONE_58H_PERCEP(11)\
- PRECALC_ONE_58H_PERCEP(12)\
- PRECALC_ONE_58H_PERCEP(13)\
- PRECALC_ONE_58H_PERCEP(14)\
- PRECALC_ONE_58H_PERCEP(15)\
- /* end unroll loop */\
- colors[0][R] = (colorsRGB444[0][R] << 4) | colorsRGB444[0][R];
- colors[0][G] = (colorsRGB444[0][G] << 4) | colorsRGB444[0][G];
- colors[0][B] = (colorsRGB444[0][B] << 4) | colorsRGB444[0][B];
- // Test all distances
- /* unroll loop for (uint8 d = 0; d < 8; ++d) */
- PRECALC_ONE_TABLE_58H_PERCEP(0)
- PRECALC_ONE_TABLE_58H_PERCEP(1)
- PRECALC_ONE_TABLE_58H_PERCEP(2)
- PRECALC_ONE_TABLE_58H_PERCEP(3)
- PRECALC_ONE_TABLE_58H_PERCEP(4)
- PRECALC_ONE_TABLE_58H_PERCEP(5)
- PRECALC_ONE_TABLE_58H_PERCEP(6)
- PRECALC_ONE_TABLE_58H_PERCEP(7)
- /* end unroll loop */
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Precalculates a table used in the exhaustive compression of the H-mode.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void precalcError58H(uint8* block, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_err)
- {
- unsigned int pixel_error,
- best_pixel_error;
- int possible_colors[2][3];
- uint8 colors[2][3];
- unsigned int *precalc_err_tab;
- int red_original;
- int green_original;
- int blue_original;
- #define PRECALC_ONE_58H(qvalue)\
- red_original = block[qvalue*4];\
- green_original = block[qvalue*4+1];\
- blue_original = block[qvalue*4+2];\
- /* unroll loop for (color = 0; color< 2; color++) */\
- best_pixel_error = square_table[(possible_colors[0][R] - red_original)] + square_table[(possible_colors[0][G] - green_original)] + square_table[(possible_colors[0][B] - blue_original)];\
- pixel_error = square_table[(possible_colors[1][R] - red_original)] + square_table[(possible_colors[1][G] - green_original)] + square_table[(possible_colors[1][B] - blue_original)];\
- if (pixel_error < best_pixel_error)\
- best_pixel_error = pixel_error;\
- /* end unroll loop */\
- precalc_err_tab[qvalue] = best_pixel_error;\
- #define PRECALC_ONE_TABLE_58H(dvalue)\
- precalc_err_tab = &precalc_err[((colorRGB444_packed*8)+dvalue)*16];\
- possible_colors[0][R] = CLAMP_LEFT_ZERO(colors[0][R] - table58H[dvalue])+255;\
- possible_colors[0][G] = CLAMP_LEFT_ZERO(colors[0][G] - table58H[dvalue])+255;\
- possible_colors[0][B] = CLAMP_LEFT_ZERO(colors[0][B] - table58H[dvalue])+255;\
- possible_colors[1][R] = CLAMP_RIGHT_255(colors[0][R] + table58H[dvalue])+255;\
- possible_colors[1][G] = CLAMP_RIGHT_255(colors[0][G] + table58H[dvalue])+255;\
- possible_colors[1][B] = CLAMP_RIGHT_255(colors[0][B] + table58H[dvalue])+255;\
- /* unrolled loop for(q = 0; q<16; q++)*/\
- PRECALC_ONE_58H(0)\
- PRECALC_ONE_58H(1)\
- PRECALC_ONE_58H(2)\
- PRECALC_ONE_58H(3)\
- PRECALC_ONE_58H(4)\
- PRECALC_ONE_58H(5)\
- PRECALC_ONE_58H(6)\
- PRECALC_ONE_58H(7)\
- PRECALC_ONE_58H(8)\
- PRECALC_ONE_58H(9)\
- PRECALC_ONE_58H(10)\
- PRECALC_ONE_58H(11)\
- PRECALC_ONE_58H(12)\
- PRECALC_ONE_58H(13)\
- PRECALC_ONE_58H(14)\
- PRECALC_ONE_58H(15)\
- /* end unroll loop */\
- colors[0][R] = (colorsRGB444[0][R] << 4) | colorsRGB444[0][R];
- colors[0][G] = (colorsRGB444[0][G] << 4) | colorsRGB444[0][G];
- colors[0][B] = (colorsRGB444[0][B] << 4) | colorsRGB444[0][B];
- // Test all distances
- /* unroll loop for (uint8 d = 0; d < 8; ++d) */
- PRECALC_ONE_TABLE_58H(0)
- PRECALC_ONE_TABLE_58H(1)
- PRECALC_ONE_TABLE_58H(2)
- PRECALC_ONE_TABLE_58H(3)
- PRECALC_ONE_TABLE_58H(4)
- PRECALC_ONE_TABLE_58H(5)
- PRECALC_ONE_TABLE_58H(6)
- PRECALC_ONE_TABLE_58H(7)
- /* end unroll loop */
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimum error for the H-mode when doing exhaustive compression.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateErrorFromPrecalcR58Hperceptual1000(int *colorsRGB444_packed, unsigned int *precalc_errR, unsigned int best_err_so_far)
- {
- unsigned int block_error = 0;
- unsigned int best_block_error = MAXERR1000;
- unsigned int *precalc_col1, *precalc_col2;
- unsigned int *precalc_col1tab, *precalc_col2tab;
- precalc_col1 = &precalc_errR[(colorsRGB444_packed[0]>>8)*8*16];
- precalc_col2 = &precalc_errR[(colorsRGB444_packed[1]>>8)*8*16];
- #define CHOICE_R58H_PERCEP(value)\
- if(precalc_col1tab[value] < precalc_col2tab[value])\
- block_error += precalc_col1tab[value];\
- else\
- block_error += precalc_col2tab[value];\
- // Test all distances
- for (uint8 d = 0; d < 8; ++d)
- {
- block_error = 0;
- precalc_col1tab = &precalc_col1[d*16];
- precalc_col2tab = &precalc_col2[d*16];
- // Loop block
- /* unroll loop for(q = 0; q<16 && block_error < best_err_so_far; q++) */
- CHOICE_R58H_PERCEP(0)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(1)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(2)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(3)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(4)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(5)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(6)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(7)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(8)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(9)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(10)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(11)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(12)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(13)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(14)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H_PERCEP(15)
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- /* end unroll loop */
- if (block_error < best_block_error)
- best_block_error = block_error;
- }
- return best_block_error;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimum error for the H-mode when doing exhaustive compression.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateErrorFromPrecalcR58H(int *colorsRGB444_packed, unsigned int *precalc_errR, unsigned int best_err_so_far)
- {
- unsigned int block_error = 0;
- unsigned int best_block_error = MAXIMUM_ERROR;
- unsigned int *precalc_col1, *precalc_col2;
- unsigned int *precalc_col1tab, *precalc_col2tab;
- precalc_col1 = &precalc_errR[(colorsRGB444_packed[0]>>8)*8*16];
- precalc_col2 = &precalc_errR[(colorsRGB444_packed[1]>>8)*8*16];
- #define CHOICE_R58H(value)\
- if(precalc_col1tab[value] < precalc_col2tab[value])\
- block_error += precalc_col1tab[value];\
- else\
- block_error += precalc_col2tab[value];\
- // Test all distances
- for (uint8 d = 0; d < 8; ++d)
- {
- block_error = 0;
- precalc_col1tab = &precalc_col1[d*16];
- precalc_col2tab = &precalc_col2[d*16];
- // Loop block
- /* unroll loop for(q = 0; q<16 && block_error < best_err_so_far; q++) */
- CHOICE_R58H(0)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(1)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(2)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(3)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(4)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(5)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(6)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(7)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(8)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(9)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(10)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(11)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(12)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(13)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(14)
- if( block_error < best_err_so_far )
- {
- CHOICE_R58H(15)
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- /* end unroll loop */
- if (block_error < best_block_error)
- best_block_error = block_error;
- }
- return best_block_error;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimum error for the H-mode when doing exhaustive compression.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateErrorFromPrecalcRG58Hperceptual1000(int *colorsRGB444_packed, unsigned int *precalc_errRG, unsigned int best_err_so_far)
- {
- unsigned int block_error = 0;
- unsigned int best_block_error = MAXIMUM_ERROR;
- unsigned int *precalc_col1, *precalc_col2;
- unsigned int *precalc_col1tab, *precalc_col2tab;
- precalc_col1 = &precalc_errRG[(colorsRGB444_packed[0]>>4)*8*16];
- precalc_col2 = &precalc_errRG[(colorsRGB444_packed[1]>>4)*8*16];
- #define CHOICE_RG58H_PERCEP(value)\
- if(precalc_col1tab[value] < precalc_col2tab[value])\
- block_error += precalc_col1tab[value];\
- else\
- block_error += precalc_col2tab[value];\
- // Test all distances
- for (uint8 d = 0; d < 8; ++d)
- {
- block_error = 0;
- precalc_col1tab = &precalc_col1[d*16];
- precalc_col2tab = &precalc_col2[d*16];
- // Loop block
- /* unroll loop for(q = 0; q<16 && block_error < best_err_so_far; q++) */
- CHOICE_RG58H_PERCEP(0)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(1)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(2)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(3)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(4)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(5)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(6)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(7)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(8)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(9)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(10)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(11)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(12)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(13)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(14)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H_PERCEP(15)
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- /* end unroll loop */
- if (block_error < best_block_error)
- best_block_error = block_error;
- }
- return best_block_error;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimum error for the H-mode when doing exhaustive compression.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateErrorFromPrecalcRG58H(int *colorsRGB444_packed, unsigned int *precalc_errRG, unsigned int best_err_so_far)
- {
- unsigned int block_error = 0;
- unsigned int best_block_error = MAXIMUM_ERROR;
- unsigned int *precalc_col1, *precalc_col2;
- unsigned int *precalc_col1tab, *precalc_col2tab;
- precalc_col1 = &precalc_errRG[(colorsRGB444_packed[0]>>4)*8*16];
- precalc_col2 = &precalc_errRG[(colorsRGB444_packed[1]>>4)*8*16];
- #define CHOICE_RG58H(value)\
- if(precalc_col1tab[value] < precalc_col2tab[value])\
- block_error += precalc_col1tab[value];\
- else\
- block_error += precalc_col2tab[value];\
- // Test all distances
- for (uint8 d = 0; d < 8; ++d)
- {
- block_error = 0;
- precalc_col1tab = &precalc_col1[d*16];
- precalc_col2tab = &precalc_col2[d*16];
- // Loop block
- /* unroll loop for(q = 0; q<16 && block_error < best_err_so_far; q++) */
- CHOICE_RG58H(0)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(1)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(2)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(3)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(4)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(5)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(6)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(7)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(8)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(9)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(10)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(11)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(12)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(13)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(14)
- if( block_error < best_err_so_far )
- {
- CHOICE_RG58H(15)
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- /* end unroll loop */
- if (block_error < best_block_error)
- best_block_error = block_error;
- }
- return best_block_error;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimum error for the H-mode when doing exhaustive compression.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateErrorFromPrecalc58Hperceptual1000(int *colorsRGB444_packed, unsigned int *precalc_err, unsigned int total_best_err)
- {
- unsigned int block_error;\
- unsigned int *precalc_col1, *precalc_col2;\
- unsigned int *precalc_col1tab, *precalc_col2tab;\
- unsigned int error;
- #define FIRSTCHOICE_RGB58H_PERCEP(value)\
- if(precalc_col1tab[value] < precalc_col2tab[value])\
- block_error = precalc_col1tab[value];\
- else\
- block_error = precalc_col2tab[value];\
- #define CHOICE_RGB58H_PERCEP(value)\
- if(precalc_col1tab[value] < precalc_col2tab[value])\
- block_error += precalc_col1tab[value];\
- else\
- block_error += precalc_col2tab[value];\
- #define ONETABLE_RGB58H_PERCEP(distance)\
- precalc_col1tab = &precalc_col1[distance*16];\
- precalc_col2tab = &precalc_col2[distance*16];\
- /* unroll loop for(q = 0; q<16 && block_error < total_best_err; q++) */\
- FIRSTCHOICE_RGB58H_PERCEP(0)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H_PERCEP(1)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H_PERCEP(2)\
- CHOICE_RGB58H_PERCEP(3)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H_PERCEP(4)\
- CHOICE_RGB58H_PERCEP(5)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H_PERCEP(6)\
- CHOICE_RGB58H_PERCEP(7)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H_PERCEP(8)\
- CHOICE_RGB58H_PERCEP(9)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H_PERCEP(10)\
- CHOICE_RGB58H_PERCEP(11)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H_PERCEP(12)\
- CHOICE_RGB58H_PERCEP(13)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H_PERCEP(14)\
- CHOICE_RGB58H_PERCEP(15)\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- /* end unroll loop */\
- if (block_error < error)\
- error = block_error;\
- #define CALCULATE_ERROR_FROM_PRECALC_RGB58H_PERCEP\
- error = MAXERR1000;\
- precalc_col1 = &precalc_err[colorsRGB444_packed[0]*8*16];\
- precalc_col2 = &precalc_err[colorsRGB444_packed[1]*8*16];\
- /* Test all distances*/\
- /* unroll loop for (uint8 d = 0; d < 8; ++d) */\
- ONETABLE_RGB58H_PERCEP(0)\
- ONETABLE_RGB58H_PERCEP(1)\
- ONETABLE_RGB58H_PERCEP(2)\
- ONETABLE_RGB58H_PERCEP(3)\
- ONETABLE_RGB58H_PERCEP(4)\
- ONETABLE_RGB58H_PERCEP(5)\
- ONETABLE_RGB58H_PERCEP(6)\
- ONETABLE_RGB58H_PERCEP(7)\
- /* end unroll loop */\
- CALCULATE_ERROR_FROM_PRECALC_RGB58H_PERCEP
- return error;\
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Calculate a minimum error for the H-mode when doing exhaustive compression.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int calculateErrorFromPrecalc58H(int *colorsRGB444_packed, unsigned int *precalc_err, unsigned int total_best_err)
- {
- unsigned int block_error;\
- unsigned int *precalc_col1, *precalc_col2;\
- unsigned int *precalc_col1tab, *precalc_col2tab;\
- unsigned int error;
- #define FIRSTCHOICE_RGB58H(value)\
- if(precalc_col1tab[value] < precalc_col2tab[value])\
- block_error = precalc_col1tab[value];\
- else\
- block_error = precalc_col2tab[value];\
- #define CHOICE_RGB58H(value)\
- if(precalc_col1tab[value] < precalc_col2tab[value])\
- block_error += precalc_col1tab[value];\
- else\
- block_error += precalc_col2tab[value];\
- #define ONETABLE_RGB58H(distance)\
- precalc_col1tab = &precalc_col1[distance*16];\
- precalc_col2tab = &precalc_col2[distance*16];\
- /* unroll loop for(q = 0; q<16 && block_error < total_best_err; q++) */\
- FIRSTCHOICE_RGB58H(0)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H(1)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H(2)\
- CHOICE_RGB58H(3)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H(4)\
- CHOICE_RGB58H(5)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H(6)\
- CHOICE_RGB58H(7)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H(8)\
- CHOICE_RGB58H(9)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H(10)\
- CHOICE_RGB58H(11)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H(12)\
- CHOICE_RGB58H(13)\
- if( block_error < total_best_err)\
- {\
- CHOICE_RGB58H(14)\
- CHOICE_RGB58H(15)\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- }\
- /* end unroll loop */\
- if (block_error < error)\
- error = block_error;\
- #define CALCULATE_ERROR_FROM_PRECALC_RGB58H\
- error = MAXIMUM_ERROR;\
- precalc_col1 = &precalc_err[colorsRGB444_packed[0]*8*16];\
- precalc_col2 = &precalc_err[colorsRGB444_packed[1]*8*16];\
- /* Test all distances*/\
- /* unroll loop for (uint8 d = 0; d < 8; ++d) */\
- ONETABLE_RGB58H(0)\
- ONETABLE_RGB58H(1)\
- ONETABLE_RGB58H(2)\
- ONETABLE_RGB58H(3)\
- ONETABLE_RGB58H(4)\
- ONETABLE_RGB58H(5)\
- ONETABLE_RGB58H(6)\
- ONETABLE_RGB58H(7)\
- /* end unroll loop */\
- CALCULATE_ERROR_FROM_PRECALC_RGB58H
- return error;\
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // The below code should compress the block to 58 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- // The bit layout is thought to be:
- //
- //|63 62 61 60 59 58|57 56 55 54|53 52 51 50|49 48 47 46|45 44 43 42|41 40 39 38|37 36 35 34|33 32|
- //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
- // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
- // Else, it is assumed to be 1.
- // The below code should compress the block to 58 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- // The bit layout is thought to be:
- //
- //|63 62 61 60 59 58|57 56 55 54|53 52 51 50|49 48 47 46|45 44 43 42|41 40 39 38|37 36 35 34|33 32|
- //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
- // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
- // Else, it is assumed to be 1.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockTHUMB58HExhaustivePerceptual(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, unsigned int best_error_so_far)
- {
- unsigned int best_error_using_Hmode;
- uint8 best_colorsRGB444[2][3];
- unsigned int best_pixel_indices;
- uint8 best_distance;
- unsigned int error;
- uint8 colorsRGB444[2][3];
- int colorsRGB444_packed[2];
- int best_colorsRGB444_packed[2];
- int colorRGB444_packed;
- unsigned int pixel_indices;
- uint8 distance;
- unsigned int *precalc_err; // smallest error per color, table and pixel
- unsigned int *precalc_err_RG; // smallest pixel error for an entire table
- unsigned int *precalc_err_R; // smallest pixel error for an entire table
- uint8 block[4*4*4];
- best_error_using_Hmode = MAXERR1000;
- precalc_err = (unsigned int*) malloc(4096*8*16*sizeof(unsigned int));
- if(!precalc_err){printf("Out of memory allocating \n");exit(1);}
- precalc_err_RG = (unsigned int*) malloc(16*16*8*16*sizeof(unsigned int));
- if(!precalc_err_RG){printf("Out of memory allocating \n");exit(1);}
- precalc_err_R = (unsigned int*) malloc(16*8*16*sizeof(unsigned int));
- if(!precalc_err_R){printf("Out of memory allocating \n");exit(1);}
- unsigned int test1, test2;
- best_error_using_Hmode = (unsigned int)compressBlockTHUMB58HFastestPerceptual1000(img,width, height, startx, starty, test1, test2);
- best_colorsRGB444_packed[0] = 0;
- best_colorsRGB444_packed[0] = GETBITSHIGH(test1, 12, 57);
- best_colorsRGB444_packed[1] = 0;
- best_colorsRGB444_packed[1] = GETBITSHIGH(test1, 12, 45);
- if(best_error_using_Hmode < best_error_so_far)
- best_error_so_far = best_error_using_Hmode;
- int xx,yy,count = 0;
- // Use 4 bytes per pixel to make it 32-word aligned.
- for(xx = 0; xx<4; xx++)
- {
- for(yy=0; yy<4; yy++)
- {
- block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block[(count)*4+3] = 0;
- count++;
- }
- }
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
- {
- colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
- colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
- colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
- precalcError58Hperceptual1000(block, colorsRGB444, colorRGB444_packed, precalc_err);
- }
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
- {
- colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
- colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
- colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
- precalcErrorRG_58Hperceptual1000(img, width, startx, starty, colorsRGB444, colorRGB444_packed, precalc_err_RG);
- }
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
- {
- colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
- colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
- colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
- precalcErrorR_58Hperceptual1000(img, width, startx, starty, colorsRGB444, colorRGB444_packed, precalc_err_R);
- }
- int trycols = 0;
- int allcols = 0;
- for( colorsRGB444[0][0] = 0; colorsRGB444[0][0] <16; colorsRGB444[0][0]++)
- {
- colorsRGB444_packed[0] = colorsRGB444[0][0]*256;
- for( colorsRGB444[1][0] = 0; colorsRGB444[1][0] <16; colorsRGB444[1][0]++)
- {
- colorsRGB444_packed[1] = colorsRGB444[1][0]*256;
- if(colorsRGB444_packed[0] <= colorsRGB444_packed[1])
- {
- error = calculateErrorFromPrecalcR58Hperceptual1000(colorsRGB444_packed, precalc_err_R, best_error_so_far);
- if(error < best_error_so_far)
- {
- for( colorsRGB444[0][1] = 0; colorsRGB444[0][1] <16; colorsRGB444[0][1]++)
- {
- colorsRGB444_packed[0] = colorsRGB444[0][0]*256 + colorsRGB444[0][1]*16;
- for( colorsRGB444[1][1] = 0; colorsRGB444[1][1] <16; colorsRGB444[1][1]++)
- {
- colorsRGB444_packed[1] = colorsRGB444[1][0]*256 + colorsRGB444[1][1]*16;
- if(colorsRGB444_packed[0] <= colorsRGB444_packed[1])
- {
- error = calculateErrorFromPrecalcRG58Hperceptual1000(colorsRGB444_packed, precalc_err_RG, best_error_so_far);
- if(error < best_error_so_far)
- {
- for( colorsRGB444[0][2] = 0; colorsRGB444[0][2] <16; colorsRGB444[0][2]++)
- {
- colorsRGB444_packed[0] = colorsRGB444[0][0]*256 + colorsRGB444[0][1]*16 + colorsRGB444[0][2];
- for( colorsRGB444[1][2] = 0; colorsRGB444[1][2] <16; colorsRGB444[1][2]++)
- {
- colorsRGB444_packed[1] = colorsRGB444[1][0]*256 + colorsRGB444[1][1]*16 + colorsRGB444[1][2];
- if(colorsRGB444_packed[0] < colorsRGB444_packed[1])
- {
- error = calculateErrorFromPrecalc58Hperceptual1000(colorsRGB444_packed, precalc_err, best_error_so_far);
- if(error < best_error_so_far)
- {
- best_error_so_far = error;
- best_error_using_Hmode = error;
- best_colorsRGB444_packed[0] = colorsRGB444_packed[0];
- best_colorsRGB444_packed[1] = colorsRGB444_packed[1];
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- best_colorsRGB444[0][0] = (best_colorsRGB444_packed[0] >> 8) & 0xf;
- best_colorsRGB444[0][1] = (best_colorsRGB444_packed[0] >> 4) & 0xf;
- best_colorsRGB444[0][2] = (best_colorsRGB444_packed[0]) & 0xf;
- best_colorsRGB444[1][0] = (best_colorsRGB444_packed[1] >> 8) & 0xf;
- best_colorsRGB444[1][1] = (best_colorsRGB444_packed[1] >> 4) & 0xf;
- best_colorsRGB444[1][2] = (best_colorsRGB444_packed[1]) & 0xf;
- free(precalc_err);
- free(precalc_err_RG);
- free(precalc_err_R);
- error = (unsigned int) calculateErrorAndCompress58Hperceptual1000(img, width, startx, starty, best_colorsRGB444, distance, pixel_indices);
- best_distance = distance;
- best_pixel_indices = pixel_indices;
- // | col0 >= col1 col0 < col1
- //------------------------------------------------------
- // (dist & 1) = 1 | no need to swap | need to swap
- // |-----------------+----------------
- // (dist & 1) = 0 | need to swap | no need to swap
- //
- // This can be done with an xor test.
- best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
- best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
- if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
- {
- swapColors(best_colorsRGB444);
- // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
- best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
- }
- // Put the compress params into the compression block
- compressed1 = 0;
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
- PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
- best_pixel_indices=indexConversion(best_pixel_indices);
- compressed2 = 0;
- PUTBITS( compressed2, best_pixel_indices, 32, 31);
- return best_error_using_Hmode;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // The below code should compress the block to 58 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- // The bit layout is thought to be:
- //
- //|63 62 61 60 59 58|57 56 55 54|53 52 51 50|49 48 47 46|45 44 43 42|41 40 39 38|37 36 35 34|33 32|
- //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
- // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
- // Else, it is assumed to be 1.
- // The below code should compress the block to 58 bits.
- // This is supposed to match the first of the three modes in TWOTIMER.
- // The bit layout is thought to be:
- //
- //|63 62 61 60 59 58|57 56 55 54|53 52 51 50|49 48 47 46|45 44 43 42|41 40 39 38|37 36 35 34|33 32|
- //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
- //
- //|31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00|
- //|----------------------------------------index bits---------------------------------------------|
- //
- // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
- // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
- // Else, it is assumed to be 1.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- unsigned int compressBlockTHUMB58HExhaustive(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, unsigned int best_error_so_far)
- {
- unsigned int best_error_using_Hmode;
- uint8 best_colorsRGB444[2][3];
- unsigned int best_pixel_indices;
- uint8 best_distance;
- unsigned int error;
- uint8 colorsRGB444[2][3];
- int colorsRGB444_packed[2];
- int best_colorsRGB444_packed[2];
- int colorRGB444_packed;
- unsigned int pixel_indices;
- uint8 distance;
- unsigned int *precalc_err; // smallest error per color, table and pixel
- unsigned int *precalc_err_RG; // smallest pixel error for an entire table
- unsigned int *precalc_err_R; // smallest pixel error for an entire table
- uint8 block[4*4*4];
- best_error_using_Hmode = MAXIMUM_ERROR;
- precalc_err = (unsigned int*) malloc(4096*8*16*sizeof(unsigned int));
- if(!precalc_err){printf("Out of memory allocating \n");exit(1);}
- precalc_err_RG = (unsigned int*) malloc(16*16*8*16*sizeof(unsigned int));
- if(!precalc_err_RG){printf("Out of memory allocating \n");exit(1);}
- precalc_err_R = (unsigned int*) malloc(16*8*16*sizeof(unsigned int));
- if(!precalc_err_R){printf("Out of memory allocating \n");exit(1);}
- unsigned int test1, test2;
- best_error_using_Hmode = (unsigned int)compressBlockTHUMB58HFastest(img,width, height, startx, starty, test1, test2);
- best_colorsRGB444_packed[0] = 0;
- best_colorsRGB444_packed[0] = GETBITSHIGH(test1, 12, 57);
- best_colorsRGB444_packed[1] = 0;
- best_colorsRGB444_packed[1] = GETBITSHIGH(test1, 12, 45);
- if(best_error_using_Hmode < best_error_so_far)
- best_error_so_far = best_error_using_Hmode;
- int xx,yy,count = 0;
- // Reshuffle pixels so that the top left 2x2 pixels arrive first, then the top right 2x2 pixels etc. Also put use 4 bytes per pixel to make it 32-word aligned.
- for(xx = 0; xx<4; xx++)
- {
- for(yy=0; yy<4; yy++)
- {
- block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
- block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
- block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
- block[(count)*4+3] = 0;
- count++;
- }
- }
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
- {
- colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
- colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
- colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
- precalcError58H(block, colorsRGB444, colorRGB444_packed, precalc_err);
- }
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
- {
- colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
- colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
- colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
- precalcErrorRG_58H(img, width, startx, starty, colorsRGB444, colorRGB444_packed, precalc_err_RG);
- }
- for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
- {
- colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
- colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
- colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
- precalcErrorR_58H(img, width, startx, starty, colorsRGB444, colorRGB444_packed, precalc_err_R);
- }
- int trycols = 0;
- int allcols = 0;
- for( colorsRGB444[0][0] = 0; colorsRGB444[0][0] <16; colorsRGB444[0][0]++)
- {
- colorsRGB444_packed[0] = colorsRGB444[0][0]*256;
- for( colorsRGB444[1][0] = 0; colorsRGB444[1][0] <16; colorsRGB444[1][0]++)
- {
- colorsRGB444_packed[1] = colorsRGB444[1][0]*256;
- if(colorsRGB444_packed[0] <= colorsRGB444_packed[1])
- {
- error = calculateErrorFromPrecalcR58H(colorsRGB444_packed, precalc_err_R, best_error_so_far);
- if(error < best_error_so_far)
- {
- for( colorsRGB444[0][1] = 0; colorsRGB444[0][1] <16; colorsRGB444[0][1]++)
- {
- colorsRGB444_packed[0] = colorsRGB444[0][0]*256 + colorsRGB444[0][1]*16;
- for( colorsRGB444[1][1] = 0; colorsRGB444[1][1] <16; colorsRGB444[1][1]++)
- {
- colorsRGB444_packed[1] = colorsRGB444[1][0]*256 + colorsRGB444[1][1]*16;
- if(colorsRGB444_packed[0] <= colorsRGB444_packed[1])
- {
- error = calculateErrorFromPrecalcRG58H(colorsRGB444_packed, precalc_err_RG, best_error_so_far);
- if(error < best_error_so_far)
- {
- for( colorsRGB444[0][2] = 0; colorsRGB444[0][2] <16; colorsRGB444[0][2]++)
- {
- colorsRGB444_packed[0] = colorsRGB444[0][0]*256 + colorsRGB444[0][1]*16 + colorsRGB444[0][2];
- for( colorsRGB444[1][2] = 0; colorsRGB444[1][2] <16; colorsRGB444[1][2]++)
- {
- colorsRGB444_packed[1] = colorsRGB444[1][0]*256 + colorsRGB444[1][1]*16 + colorsRGB444[1][2];
- if(colorsRGB444_packed[0] < colorsRGB444_packed[1])
- {
- error = calculateErrorFromPrecalc58H(colorsRGB444_packed, precalc_err, best_error_so_far);
- if(error < best_error_so_far)
- {
- best_error_so_far = error;
- best_error_using_Hmode = error;
- best_colorsRGB444_packed[0] = colorsRGB444_packed[0];
- best_colorsRGB444_packed[1] = colorsRGB444_packed[1];
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- best_colorsRGB444[0][0] = (best_colorsRGB444_packed[0] >> 8) & 0xf;
- best_colorsRGB444[0][1] = (best_colorsRGB444_packed[0] >> 4) & 0xf;
- best_colorsRGB444[0][2] = (best_colorsRGB444_packed[0]) & 0xf;
- best_colorsRGB444[1][0] = (best_colorsRGB444_packed[1] >> 8) & 0xf;
- best_colorsRGB444[1][1] = (best_colorsRGB444_packed[1] >> 4) & 0xf;
- best_colorsRGB444[1][2] = (best_colorsRGB444_packed[1]) & 0xf;
- free(precalc_err);
- free(precalc_err_RG);
- free(precalc_err_R);
- error = (unsigned int) calculateErrorAndCompress58H(img, width, startx, starty, best_colorsRGB444, distance, pixel_indices);
- best_distance = distance;
- best_pixel_indices = pixel_indices;
- // | col0 >= col1 col0 < col1
- //------------------------------------------------------
- // (dist & 1) = 1 | no need to swap | need to swap
- // |-----------------+----------------
- // (dist & 1) = 0 | need to swap | no need to swap
- //
- // This can be done with an xor test.
- best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
- best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
- if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
- {
- swapColors(best_colorsRGB444);
- // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
- best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
- }
- // Put the compress params into the compression block
- compressed1 = 0;
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
- PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
- PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
- PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
- best_pixel_indices=indexConversion(best_pixel_indices);
- compressed2 = 0;
- PUTBITS( compressed2, best_pixel_indices, 32, 31);
- return best_error_using_Hmode;
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Compress a block exhaustively for the ETC1 codec.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockETC1Exhaustive(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int error_currently_best;
- unsigned int etc1_differential_word1;
- unsigned int etc1_differential_word2;
- unsigned int error_etc1_differential;
- unsigned int etc1_individual_word1;
- unsigned int etc1_individual_word2;
- unsigned int error_etc1_individual;
-
- unsigned int error_best;
- signed char best_char;
- int best_mode;
- error_currently_best = 255*255*16*3;
- // First pass -- quickly find a low error so that we can later cull away a lot of
- // calculations later that are guaranteed to be higher than that error.
- unsigned int error_etc1;
- unsigned int etc1_word1;
- unsigned int etc1_word2;
- error_etc1 = (unsigned int) compressBlockDiffFlipFast(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
- if(error_etc1 < error_currently_best)
- error_currently_best = error_etc1;
- error_etc1_individual = compressBlockIndividualExhaustive(img, width, height, startx, starty, etc1_individual_word1, etc1_individual_word2, error_currently_best);
- if(error_etc1_individual < error_currently_best)
- error_currently_best = error_etc1_individual;
- error_etc1_differential = compressBlockDifferentialExhaustive(img, width, height, startx, starty, etc1_differential_word1, etc1_differential_word2, error_currently_best);
- if(error_etc1_differential < error_currently_best)
- error_currently_best = error_etc1_differential;
- error_best = error_etc1_differential;
- compressed1 = etc1_differential_word1;
- compressed2 = etc1_differential_word2;
- best_char = '.';
- best_mode = MODE_ETC1;
- if(error_etc1_individual < error_best)
- {
- compressed1 = etc1_individual_word1;
- compressed2 = etc1_individual_word2;
- best_char = ',';
- error_best = error_etc1_individual;
- best_mode = MODE_ETC1;
- }
- if(error_etc1 < error_best)
- {
- compressed1 = etc1_word1;
- compressed2 = etc1_word2;
- best_char = '.';
- error_best = error_etc1;
- best_mode = MODE_ETC1;
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Compress a block exhaustively for the ETC1 codec using perceptual error measure.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockETC1ExhaustivePerceptual(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int error_currently_best;
-
- unsigned int etc1_differential_word1;
- unsigned int etc1_differential_word2;
- unsigned int error_etc1_differential;
- unsigned int etc1_individual_word1;
- unsigned int etc1_individual_word2;
- unsigned int error_etc1_individual;
- unsigned int error_best;
- signed char best_char;
- int best_mode;
- error_currently_best = 255*255*16*1000;
- // First pass -- quickly find a low error so that we can later cull away a lot of
- // calculations later that are guaranteed to be higher than that error.
- unsigned int error_etc1;
- unsigned int etc1_word1;
- unsigned int etc1_word2;
- compressBlockDiffFlipFastPerceptual(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
- decompressBlockDiffFlip(etc1_word1, etc1_word2, imgdec, width, height, startx, starty);
- error_etc1 = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
- if(error_etc1 < error_currently_best)
- error_currently_best = error_etc1;
- // Second pass --- now find the lowest error, but only if it is lower than error_currently_best
- error_etc1_differential = compressBlockDifferentialExhaustivePerceptual(img, width, height, startx, starty, etc1_differential_word1, etc1_differential_word2, error_currently_best);
- if(error_etc1_differential < error_currently_best)
- error_currently_best = error_etc1_differential;
- error_etc1_individual = compressBlockIndividualExhaustivePerceptual(img, width, height, startx, starty, etc1_individual_word1, etc1_individual_word2, error_currently_best);
- if(error_etc1_individual < error_currently_best)
- error_currently_best = error_etc1_individual;
- // Now find the best error.
- error_best = error_etc1;
- compressed1 = etc1_word1;
- compressed2 = etc1_word2;
- best_char = '.';
- best_mode = MODE_ETC1;
- if(error_etc1_differential < error_best)
- {
- error_best = error_etc1_differential;
- compressed1 = etc1_differential_word1;
- compressed2 = etc1_differential_word2;
- best_char = '.';
- best_mode = MODE_ETC1;
- }
- if(error_etc1_individual < error_best)
- {
- compressed1 = etc1_individual_word1;
- compressed2 = etc1_individual_word2;
- best_char = ',';
- error_best = error_etc1_individual;
- best_mode = MODE_ETC1;
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Compress a block exhaustively for the ETC2 RGB codec using perceptual error measure.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockETC2ExhaustivePerceptual(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int error_currently_best;
-
- unsigned int etc1_differential_word1;
- unsigned int etc1_differential_word2;
- unsigned int error_etc1_differential;
- unsigned int etc1_individual_word1;
- unsigned int etc1_individual_word2;
- unsigned int error_etc1_individual;
- unsigned int planar57_word1;
- unsigned int planar57_word2;
- unsigned int planar_word1;
- unsigned int planar_word2;
- double error_planar;
- unsigned int error_planar_red, error_planar_green, error_planar_blue;
- unsigned int thumbH58_word1;
- unsigned int thumbH58_word2;
- unsigned int thumbH_word1;
- unsigned int thumbH_word2;
- unsigned int error_thumbH;
-
- unsigned int thumbT59_word1;
- unsigned int thumbT59_word2;
- unsigned int thumbT_word1;
- unsigned int thumbT_word2;
- unsigned int error_thumbT;
-
- unsigned int error_best;
- signed char best_char;
- int best_mode;
- error_currently_best = 255*255*16*1000;
- // First pass -- quickly find a low error so that we can later cull away a lot of
- // calculations later that are guaranteed to be higher than that error.
- unsigned int error_etc1;
- unsigned int etc1_word1;
- unsigned int etc1_word2;
- compressBlockDiffFlipFastPerceptual(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
- decompressBlockDiffFlip(etc1_word1, etc1_word2, imgdec, width, height, startx, starty);
- error_etc1 = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
- if(error_etc1 < error_currently_best)
- error_currently_best = error_etc1;
- // The planar mode treats every channel independently and should not be affected by the weights in the error measure.
- // We can hence use the nonperceptual version of the encoder also to find the best perceptual description of the block.
- compressBlockPlanar57(img, width, height, startx, starty, planar57_word1, planar57_word2);
- decompressBlockPlanar57errorPerComponent(planar57_word1, planar57_word2, imgdec, width, height, startx, starty, img, error_planar_red, error_planar_green, error_planar_blue);
- error_planar = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
- stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
- if(error_planar < error_currently_best)
- error_currently_best = (unsigned int) error_planar;
- error_thumbT = (unsigned int) compressBlockTHUMB59TFastestPerceptual1000(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
- stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
- if(error_thumbT < error_currently_best)
- error_currently_best = error_thumbT;
- error_thumbH = (unsigned int) compressBlockTHUMB58HFastestPerceptual1000(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
- stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
- if(error_thumbH < error_currently_best)
- error_currently_best = error_thumbH;
-
- // Second pass --- now find the lowest error, but only if it is lower than error_currently_best
- // Correct the individual errors for the different planes so that they sum to 1000 instead of 1.
- error_planar_red *=PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000;
- error_planar_green *=PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000;
- error_planar_blue *=PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000;
- compressBlockPlanar57ExhaustivePerceptual(img, width, height, startx, starty, planar57_word1, planar57_word2, error_currently_best, error_planar_red, error_planar_green, error_planar_blue);
- decompressBlockPlanar57(planar57_word1, planar57_word2, imgdec, width, height, startx, starty);
- error_planar = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
- stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
- if(error_planar < error_currently_best)
- error_currently_best = (unsigned int) error_planar;
-
- error_etc1_differential = compressBlockDifferentialExhaustivePerceptual(img, width, height, startx, starty, etc1_differential_word1, etc1_differential_word2, error_currently_best);
- if(error_etc1_differential < error_currently_best)
- error_currently_best = error_etc1_differential;
- error_etc1_individual = compressBlockIndividualExhaustivePerceptual(img, width, height, startx, starty, etc1_individual_word1, etc1_individual_word2, error_currently_best);
- if(error_etc1_individual < error_currently_best)
- error_currently_best = error_etc1_individual;
- error_thumbH = compressBlockTHUMB58HExhaustivePerceptual(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2, error_currently_best);
- stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
- if( error_thumbH < error_currently_best)
- error_currently_best = error_thumbH;
- error_thumbT = compressBlockTHUMB59TExhaustivePerceptual(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2, error_currently_best);
- stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
- if(error_thumbT < error_currently_best)
- error_currently_best = error_thumbT;
- // Now find the best error.
- error_best = error_etc1;
- compressed1 = etc1_word1;
- compressed2 = etc1_word2;
- best_char = '.';
- best_mode = MODE_ETC1;
- if(error_etc1_differential < error_best)
- {
- error_best = error_etc1_differential;
- compressed1 = etc1_differential_word1;
- compressed2 = etc1_differential_word2;
- best_char = '.';
- best_mode = MODE_ETC1;
- }
- if(error_etc1_individual < error_best)
- {
- compressed1 = etc1_individual_word1;
- compressed2 = etc1_individual_word2;
- best_char = ',';
- error_best = error_etc1_individual;
- best_mode = MODE_ETC1;
- }
- if(error_planar < error_best)
- {
- compressed1 = planar_word1;
- compressed2 = planar_word2;
- best_char = 'p';
- error_best = (unsigned int) error_planar;
- best_mode = MODE_PLANAR;
- }
- if(error_thumbH < error_best)
- {
- compressed1 = thumbH_word1;
- compressed2 = thumbH_word2;
- best_char = 'H';
- error_best = error_thumbH;
- best_mode = MODE_THUMB_H;
- }
- if(error_thumbT < error_best)
- {
- compressed1 = thumbT_word1;
- compressed2 = thumbT_word2;
- best_char = 'T';
- error_best = error_thumbT;
- best_mode = MODE_THUMB_T;
- }
- }
- #endif
- #if EXHAUSTIVE_CODE_ACTIVE
- // Compress a block exhaustively for the ETC2 RGB codec.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressBlockETC2Exhaustive(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
- {
- unsigned int error_currently_best;
- unsigned int etc1_differential_word1;
- unsigned int etc1_differential_word2;
- unsigned int error_etc1_differential;
- unsigned int etc1_individual_word1;
- unsigned int etc1_individual_word2;
- unsigned int error_etc1_individual;
- unsigned int planar57_word1;
- unsigned int planar57_word2;
- unsigned int planar_word1;
- unsigned int planar_word2;
- double error_planar;
- unsigned int error_planar_red;
- unsigned int error_planar_green;
- unsigned int error_planar_blue;
- unsigned int thumbH58_word1;
- unsigned int thumbH58_word2;
- unsigned int thumbH_word1;
- unsigned int thumbH_word2;
- unsigned int error_thumbH;
-
- unsigned int thumbT59_word1;
- unsigned int thumbT59_word2;
- unsigned int thumbT_word1;
- unsigned int thumbT_word2;
- unsigned int error_thumbT;
-
- unsigned int error_best;
- signed char best_char;
- int best_mode;
- error_currently_best = 255*255*16*3;
- // First pass -- quickly find a low error so that we can later cull away a lot of
- // calculations later that are guaranteed to be higher than that error.
- unsigned int error_etc1;
- unsigned int etc1_word1;
- unsigned int etc1_word2;
- error_etc1 = (unsigned int) compressBlockDiffFlipFast(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
- if(error_etc1 < error_currently_best)
- error_currently_best = error_etc1;
- compressBlockPlanar57(img, width, height, startx, starty, planar57_word1, planar57_word2);
- decompressBlockPlanar57errorPerComponent(planar57_word1, planar57_word2, imgdec, width, height, startx, starty, img, error_planar_red, error_planar_green, error_planar_blue);
- error_planar = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
- if(error_planar < error_currently_best)
- error_currently_best = (unsigned int) error_planar;
- error_thumbT = (unsigned int) compressBlockTHUMB59TFastest(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
- stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
- if(error_thumbT < error_currently_best)
- error_currently_best = error_thumbT;
- error_thumbH = (unsigned int) compressBlockTHUMB58HFastest(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
- stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
- if(error_thumbH < error_currently_best)
- error_currently_best = error_thumbH;
- // Second pass --- now find the lowest error, but only if it is lower than error_currently_best
- error_etc1_differential = compressBlockDifferentialExhaustive(img, width, height, startx, starty, etc1_differential_word1, etc1_differential_word2, error_currently_best);
- if(error_etc1_differential < error_currently_best)
- error_currently_best = error_etc1_differential;
- compressBlockPlanar57Exhaustive(img, width, height, startx, starty, planar57_word1, planar57_word2, error_currently_best, error_planar_red, error_planar_green, error_planar_blue);
- decompressBlockPlanar57(planar57_word1, planar57_word2, imgdec, width, height, startx, starty);
- error_planar = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
- stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
- if(error_planar < error_currently_best)
- error_currently_best = (unsigned int) error_planar;
-
- error_etc1_individual = compressBlockIndividualExhaustive(img, width, height, startx, starty, etc1_individual_word1, etc1_individual_word2, error_currently_best);
- if(error_etc1_individual < error_currently_best)
- error_currently_best = error_etc1_individual;
- error_thumbH = compressBlockTHUMB58HExhaustive(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2, error_currently_best);
- if( error_thumbH < error_currently_best)
- error_currently_best = error_thumbH;
- stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
- error_thumbT = compressBlockTHUMB59TExhaustive(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2, error_currently_best);
- if(error_thumbT < error_currently_best)
- error_currently_best = error_thumbT;
- stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
- error_best = 255*255*3*16;
- // Now find the best error.
- error_best = error_etc1;
- compressed1 = etc1_word1;
- compressed2 = etc1_word2;
- best_char = '.';
- best_mode = MODE_ETC1;
- if(error_etc1_differential < error_best)
- {
- error_best = error_etc1_differential;
- compressed1 = etc1_differential_word1;
- compressed2 = etc1_differential_word2;
- best_char = '.';
- best_mode = MODE_ETC1;
- }
- if(error_etc1_individual < error_best)
- {
- compressed1 = etc1_individual_word1;
- compressed2 = etc1_individual_word2;
- best_char = ',';
- error_best = error_etc1_individual;
- best_mode = MODE_ETC1;
- }
- if(error_planar < error_best)
- {
- compressed1 = planar_word1;
- compressed2 = planar_word2;
- best_char = 'p';
- error_best = (unsigned int) error_planar;
- best_mode = MODE_PLANAR;
- }
- if(error_thumbH < error_best)
- {
- compressed1 = thumbH_word1;
- compressed2 = thumbH_word2;
- best_char = 'H';
- error_best = error_thumbH;
- best_mode = MODE_THUMB_H;
- }
- if(error_thumbT < error_best)
- {
- compressed1 = thumbT_word1;
- compressed2 = thumbT_word2;
- best_char = 'T';
- error_best = error_thumbT;
- best_mode = MODE_THUMB_T;
- }
- }
- #endif
- //// Exhaustive code ends here.
- // Compress an image file.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressImageFile(uint8 *img, uint8 *alphaimg,int width,int height,char *dstfile, int expandedwidth, int expandedheight)
- {
- FILE *f;
- int x,y,w,h;
- unsigned int block1, block2;
- unsigned short wi, hi;
- unsigned char magic[4];
- unsigned char version[2];
- unsigned short texture_type=format;
- uint8 *imgdec;
- uint8* alphaimg2;
- imgdec = (unsigned char*) malloc(expandedwidth*expandedheight*3);
- if(!imgdec)
- {
- printf("Could not allocate decompression buffer --- exiting\n");
- }
- magic[0] = 'P'; magic[1] = 'K'; magic[2] = 'M'; magic[3] = ' ';
- if(codec==CODEC_ETC2)
- {
- version[0] = '2'; version[1] = '0';
- }
- else
- {
- version[0] = '1'; version[1] = '0';
- }
- if(f=fopen(dstfile,"wb"))
- {
- w=expandedwidth/4; w*=4;
- h=expandedheight/4; h*=4;
- wi = w;
- hi = h;
- if(ktxFile)
- {
- //.ktx file: KTX header followed by compressed binary data.
- KTX_header header;
- //identifier
- for(int i=0; i<12; i++)
- {
- header.identifier[i]=ktx_identifier[i];
- }
- //endianess int.. if this comes out reversed, all of the other ints will too.
- header.endianness=KTX_ENDIAN_REF;
-
- //these values are always 0/1 for compressed textures.
- header.glType=0;
- header.glTypeSize=1;
- header.glFormat=0;
- header.pixelWidth=width;
- header.pixelHeight=height;
- header.pixelDepth=0;
- //we only support single non-mipmapped non-cubemap textures..
- header.numberOfArrayElements=0;
- header.numberOfFaces=1;
- header.numberOfMipmapLevels=1;
- //and no metadata..
- header.bytesOfKeyValueData=0;
-
- int halfbytes=1;
- //header.glInternalFormat=?
- //header.glBaseInternalFormat=?
- if(format==ETC2PACKAGE_R_NO_MIPMAPS)
- {
- header.glBaseInternalFormat=GL_R;
- if(formatSigned)
- header.glInternalFormat=GL_COMPRESSED_SIGNED_R11_EAC;
- else
- header.glInternalFormat=GL_COMPRESSED_R11_EAC;
- }
- else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- halfbytes=2;
- header.glBaseInternalFormat=GL_RG;
- if(formatSigned)
- header.glInternalFormat=GL_COMPRESSED_SIGNED_RG11_EAC;
- else
- header.glInternalFormat=GL_COMPRESSED_RG11_EAC;
- }
- else if(format==ETC2PACKAGE_RGB_NO_MIPMAPS)
- {
- header.glBaseInternalFormat=GL_RGB;
- header.glInternalFormat=GL_COMPRESSED_RGB8_ETC2;
- }
- else if(format==ETC2PACKAGE_sRGB_NO_MIPMAPS)
- {
- header.glBaseInternalFormat=GL_SRGB;
- header.glInternalFormat=GL_COMPRESSED_SRGB8_ETC2;
- }
- else if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS)
- {
- halfbytes=2;
- header.glBaseInternalFormat=GL_RGBA;
- header.glInternalFormat=GL_COMPRESSED_RGBA8_ETC2_EAC;
- }
- else if(format==ETC2PACKAGE_sRGBA_NO_MIPMAPS)
- {
- halfbytes=2;
- header.glBaseInternalFormat=GL_SRGB8_ALPHA8;
- header.glInternalFormat=GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
- }
- else if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS)
- {
- header.glBaseInternalFormat=GL_RGBA;
- header.glInternalFormat=GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
- }
- else if(format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- header.glBaseInternalFormat=GL_SRGB8_ALPHA8;
- header.glInternalFormat=GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
- }
- else if(format==ETC1_RGB_NO_MIPMAPS)
- {
- header.glBaseInternalFormat=GL_RGB;
- header.glInternalFormat=GL_ETC1_RGB8_OES;
- }
- else
- {
- printf("internal error: bad format!\n");
- exit(1);
- }
- //write header
- fwrite(&header,sizeof(KTX_header),1,f);
-
- //write size of compressed data.. which depend on the expanded size..
- unsigned int imagesize=(w*h*halfbytes)/2;
- fwrite(&imagesize,sizeof(int),1,f);
- }
- else
- {
- //.pkm file, contains small header..
- // Write magic number
- fwrite(&magic[0], sizeof(unsigned char), 1, f);
- fwrite(&magic[1], sizeof(unsigned char), 1, f);
- fwrite(&magic[2], sizeof(unsigned char), 1, f);
- fwrite(&magic[3], sizeof(unsigned char), 1, f);
-
- // Write version
- fwrite(&version[0], sizeof(unsigned char), 1, f);
- fwrite(&version[1], sizeof(unsigned char), 1, f);
- // Write texture type
- if(texture_type==ETC2PACKAGE_RG_NO_MIPMAPS&&formatSigned)
- {
- unsigned short temp = ETC2PACKAGE_RG_SIGNED_NO_MIPMAPS;
- write_big_endian_2byte_word(&temp,f);
- }
- else if(texture_type==ETC2PACKAGE_R_NO_MIPMAPS&&formatSigned)
- {
- unsigned short temp = ETC2PACKAGE_R_SIGNED_NO_MIPMAPS;
- write_big_endian_2byte_word(&temp,f);
- }
- else
- write_big_endian_2byte_word(&texture_type, f);
- // Write binary header: the width and height as unsigned 16-bit words
- write_big_endian_2byte_word(&wi, f);
- write_big_endian_2byte_word(&hi, f);
- // Also write the active pixels. For instance, if we want to compress
- // a 128 x 129 image, we have to extend it to 128 x 132 pixels.
- // Then the wi and hi written above will be 128 and 132, but the
- // additional information that we write below will be 128 and 129,
- // to indicate that it is only the top 129 lines of data in the
- // decompressed image that will be valid data, and the rest will
- // be just garbage.
- unsigned short activew, activeh;
- activew = width;
- activeh = height;
- write_big_endian_2byte_word(&activew, f);
- write_big_endian_2byte_word(&activeh, f);
- }
- int totblocks = expandedheight/4 * expandedwidth/4;
- int countblocks = 0;
- double percentageblocks=-1.0;
- double oldpercentageblocks;
-
- if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- //extract data from red and green channel into two alpha channels.
- //note that the image will be 16-bit per channel in this case.
- alphaimg= (unsigned char*)malloc(expandedwidth*expandedheight*2);
- alphaimg2=(unsigned char*)malloc(expandedwidth*expandedheight*2);
- setupAlphaTableAndValtab();
- if(!alphaimg||!alphaimg2)
- {
- printf("failed allocating space for alpha buffers!\n");
- exit(1);
- }
- for(y=0;y<expandedheight;y++)
- {
- for(x=0;x<expandedwidth;x++)
- {
- alphaimg[2*(y*expandedwidth+x)]=img[6*(y*expandedwidth+x)];
- alphaimg[2*(y*expandedwidth+x)+1]=img[6*(y*expandedwidth+x)+1];
- alphaimg2[2*(y*expandedwidth+x)]=img[6*(y*expandedwidth+x)+2];
- alphaimg2[2*(y*expandedwidth+x)+1]=img[6*(y*expandedwidth+x)+3];
- }
- }
- }
- for(y=0;y<expandedheight/4;y++)
- {
- for(x=0;x<expandedwidth/4;x++)
- {
- countblocks++;
- oldpercentageblocks = percentageblocks;
- percentageblocks = 100.0*countblocks/(1.0*totblocks);
- //compress color channels
- if(codec==CODEC_ETC)
- {
- if(metric==METRIC_NONPERCEPTUAL)
- {
- if(speed==SPEED_FAST)
- compressBlockDiffFlipFast(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
- else
- #if EXHAUSTIVE_CODE_ACTIVE
- compressBlockETC1Exhaustive(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
- #else
- printf("Not implemented in this version\n");
- #endif
- }
- else
- {
- if(speed==SPEED_FAST)
- compressBlockDiffFlipFastPerceptual(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
- else
- #if EXHAUSTIVE_CODE_ACTIVE
- compressBlockETC1ExhaustivePerceptual(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
- #else
- printf("Not implemented in this version\n");
- #endif
- }
- }
- else
- {
- if(format==ETC2PACKAGE_R_NO_MIPMAPS||format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- //don't compress color
- }
- else if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- //this is only available for fast/nonperceptual
- if(speed == SPEED_SLOW && first_time_message)
- {
- printf("Slow codec not implemented for RGBA1 --- using fast codec instead.\n");
- first_time_message = false;
- }
- compressBlockETC2Fast(img, alphaimg,imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
- }
- else if(metric==METRIC_NONPERCEPTUAL)
- {
- if(speed==SPEED_FAST)
- compressBlockETC2Fast(img, alphaimg,imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
- else
- #if EXHAUSTIVE_CODE_ACTIVE
- compressBlockETC2Exhaustive(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
- #else
- printf("Not implemented in this version\n");
- #endif
- }
- else
- {
- if(speed==SPEED_FAST)
- compressBlockETC2FastPerceptual(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
- else
- #if EXHAUSTIVE_CODE_ACTIVE
- compressBlockETC2ExhaustivePerceptual(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
- #else
- printf("Not implemented in this version\n");
- #endif
- }
- }
-
- //compression of alpha channel in case of 4-bit alpha. Uses 8-bit alpha channel as input, and has 8-bit precision.
- if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS)
- {
- uint8 alphadata[8];
- if(speed==SPEED_SLOW)
- compressBlockAlphaSlow(alphaimg,4*x,4*y,expandedwidth,expandedheight,alphadata);
- else
- compressBlockAlphaFast(alphaimg,4*x,4*y,expandedwidth,expandedheight,alphadata);
- //write the 8 bytes of alphadata into f.
- fwrite(alphadata,1,8,f);
- }
- //store compressed color channels
- if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- write_big_endian_4byte_word(&block1, f);
- write_big_endian_4byte_word(&block2, f);
- }
- //1-channel or 2-channel alpha compression: uses 16-bit data as input, and has 11-bit precision
- if(format==ETC2PACKAGE_R_NO_MIPMAPS||format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- uint8 alphadata[8];
- compressBlockAlpha16(alphaimg,4*x,4*y,expandedwidth,expandedheight,alphadata);
- fwrite(alphadata,1,8,f);
- }
- //compression of second alpha channel in RG-compression
- if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- {
- uint8 alphadata[8];
- compressBlockAlpha16(alphaimg2,4*x,4*y,expandedwidth,expandedheight,alphadata);
- fwrite(alphadata,1,8,f);
- }
- #if 1
- if(verbose)
- {
- if(speed==SPEED_FAST)
- {
- if( ((int)(percentageblocks) != (int)(oldpercentageblocks) ) || percentageblocks == 100.0)
- printf("Compressed %d of %d blocks, %.0f%% finished.\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", countblocks, totblocks, 100.0*countblocks/(1.0*totblocks));
- }
- else
- printf("Compressed %d of %d blocks, %.0f%% finished.\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", countblocks, totblocks, 100.0*countblocks/(1.0*totblocks));
- }
- #endif
- }
- }
- printf("\n");
- fclose(f);
- printf("Saved file <%s>.\n",dstfile);
- }
- }
- // Compress an file.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- void compressFile(char *srcfile,char *dstfile)
- {
- uint8 *srcimg;
- int width,height;
- int extendedwidth, extendedheight;
- struct _timeb tstruct;
- int tstart;
- int tstop;
- // 0: compress from .any to .pkm with SPEED_FAST, METRIC_NONPERCEPTUAL, ETC
- // 1: compress from .any to .pkm with SPEED_MEDIUM, METRIC_NONPERCEPTUAL, ETC
- // 2: compress from .any to .pkm with SPEED_SLOW, METRIC_NONPERCEPTUAL, ETC
- // 3: compress from .any to .pkm with SPEED_FAST, METRIC_PERCEPTUAL, ETC
- // 4: compress from .any to .pkm with SPEED_MEDIUM, METRIC_PERCEPTUAL, ETC
- // 5: compress from .any to .pkm with SPEED_SLOW, METRIC_PERCEPTUAL, ETC
- // 6: decompress from .pkm to .any
- // 7: calculate PSNR between .any and .any
- // 8: compress from .any to .pkm with SPEED_FAST, METRIC_NONPERCEPTUAL, ETC2
- // 9: compress from .any to .pkm with SPEED_MEDIUM, METRIC_NONPERCEPTUAL, ETC2
- //10: compress from .any to .pkm with SPEED_SLOW, METRIC_NONPERCEPTUAL, ETC2
- //11: compress from .any to .pkm with SPEED_FAST, METRIC_PERCEPTUAL, ETC2
- //12: compress from .any to .pkm with SPEED_MEDIUM, METRIC_PERCEPTUAL, ETC2
- //13: compress from .any to .pkm with SPEED_SLOW, METRIC_PERCEPTUAL, ETC2
- printf("\n");
- if(codec==CODEC_ETC)
- printf("ETC codec, ");
- else
- printf("ETC2 codec, ");
- if(speed==SPEED_FAST)
- printf("using FAST compression mode and ");
- else if(speed==SPEED_MEDIUM)
- printf("using MEDIUM compression mode and ");
- else
- printf("using SLOW compression mode and ");
- if(metric==METRIC_PERCEPTUAL)
- printf("PERCEPTUAL error metric, ");
- else
- printf("NONPERCEPTUAL error metric, ");
- if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS)
- printf("in RGBA format");
- else if(format==ETC2PACKAGE_sRGBA_NO_MIPMAPS)
- printf("in sRGBA format");
- else if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS)
- printf("in RGB + punch-through alpha format");
- else if(format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- printf("in sRGB + punch-through alpha format");
- else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
- printf("in R format");
- else if(format==ETC2PACKAGE_RGB_NO_MIPMAPS||format==ETC1_RGB_NO_MIPMAPS)
- printf("in RGB format");
- else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
- printf("in RG format");
- else
- printf("in OTHER format");
- printf("\n");
- if(readCompressParams())
- {
- if(format==ETC2PACKAGE_R_NO_MIPMAPS||readSrcFile(srcfile,srcimg,width,height,extendedwidth, extendedheight))
- {
- //make sure that alphasrcimg contains the alpha channel or is null here, and pass it to compressimagefile
- uint8* alphaimg=NULL;
- if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
- {
- char str[300];
- //printf("reading alpha channel....");
- sprintf(str,"magick convert %s -alpha extract alpha.pgm\n",srcfile);
- system(str);
- readAlpha(alphaimg,width,height,extendedwidth,extendedheight);
- printf("ok!\n");
- setupAlphaTableAndValtab();
- }
- else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
- {
- char str[300];
- sprintf(str,"magick convert %s alpha.pgm\n",srcfile);
- system(str);
- readAlpha(alphaimg,width,height,extendedwidth,extendedheight);
- printf("read alpha ok, size is %d,%d (%d,%d)",width,height,extendedwidth,extendedheight);
- setupAlphaTableAndValtab();
- }
- printf("Compressing...\n");
- tstart=time(NULL);
- _ftime( &tstruct );
- tstart=tstart*1000+tstruct.millitm;
- compressImageFile(srcimg,alphaimg,width,height,dstfile,extendedwidth, extendedheight);
- tstop = time(NULL);
- _ftime( &tstruct );
- tstop = tstop*1000+tstruct.millitm;
- printf( "It took %u milliseconds to compress:\n", tstop - tstart);
- calculatePSNRfile(dstfile,srcimg,alphaimg);
- }
- }
- }
- // Calculates the PSNR between two files.
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- double calculatePSNRTwoFiles(char *srcfile1,char *srcfile2)
- {
- uint8 *srcimg1;
- uint8 *srcimg2;
- int width1, height1;
- int width2, height2;
- double PSNR;
- double perceptually_weighted_PSNR;
- if(readSrcFileNoExpand(srcfile1,srcimg1,width1,height1))
- {
- if(readSrcFileNoExpand(srcfile2,srcimg2,width2,height2))
- {
- if((width1 == width2) && (height1 == height2))
- {
- PSNR = calculatePSNR(srcimg1, srcimg2, width1, height1);
- printf("%f\n",PSNR);
- perceptually_weighted_PSNR = calculateWeightedPSNR(srcimg1, srcimg2, width1, height1, 0.299, 0.587, 0.114);
- }
- else
- {
- printf("\n Width and height do no not match for image: width, height = (%d, %d) and (%d, %d)\n",width1,height1, width2, height2);
- }
- }
- else
- {
- printf("Couldn't open file %s.\n",srcfile2);
- }
- }
- else
- {
- printf("Couldn't open file %s.\n",srcfile1);
- }
- return PSNR;
- }
- // Main function
- // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
- int main(int argc,char *argv[])
- {
- if(argc==3 || argc==4 || argc == 5 || argc == 7 || argc == 9 || argc == 11 || argc == 13)
- {
- // The source file is always the second last one.
- char srcfile[200];
- char dstfile[200];
- readArguments(argc,argv,srcfile,dstfile);
-
- int q = find_pos_of_extension(srcfile);
- int q2 = find_pos_of_extension(dstfile);
-
- if(!fileExist(srcfile))
- {
- printf("Error: file <%s> does not exist.\n",srcfile);
- exit(0);
- }
-
- if(mode==MODE_UNCOMPRESS)
- {
- printf("Decompressing .pkm/.ktx file ...\n");
- uint8* alphaimg=NULL, *img;
- int w, h;
- uncompressFile(srcfile,img,alphaimg,w,h);
- writeOutputFile(dstfile,img,alphaimg,w,h);
- }
- else if(mode==MODE_PSNR)
- {
- calculatePSNRTwoFiles(srcfile,dstfile);
- }
- else
- {
- compressFile(srcfile, dstfile);
- }
- }
- else
- {
- printf("ETCPACK v2.74 For ETC and ETC2\n");
- printf("Compresses and decompresses images using the Ericsson Texture Compression (ETC) version 1.0 and 2.0.\n\nUsage: etcpack srcfile dstfile\n\n");
- printf(" -s {fast|slow} Compression speed. Slow = exhaustive \n");
- printf(" search for optimal quality\n");
- printf(" (default: fast)\n");
- printf(" -e {perceptual|nonperceptual} Error metric: Perceptual (nicest) or \n");
- printf(" nonperceptual (highest PSNR)\n");
- printf(" (default: perceptual)\n");
- printf(" -c {etc1|etc2} Codec: etc1 (most compatible) or \n");
- printf(" etc2 (highest quality)\n");
- printf(" (default: etc2)\n");
- printf(" -f {R|R_signed|RG|RG_signed| Format: one, two, three or four \n");
- printf(" RGB|RGBA1|RGBA8| channels, and 1 or 8 bits for alpha\n");
- printf(" sRGB|sRGBA1|sRGBA8|} RGB or sRGB.\n");
- printf(" (1 equals punchthrough)\n");
- printf(" (default: RGB)\n");
- printf(" -v {on|off} Detailed progress info. (default on)\n");
- printf(" \n");
- printf("Examples: \n");
- printf(" etcpack img.ppm img.pkm Compresses img.ppm to img.pkm in\n");
- printf(" ETC2 RGB format\n");
- printf(" etcpack img.ppm img.ktx Compresses img.ppm to img.ktx in\n");
- printf(" ETC2 RGB format\n");
- printf(" etcpack img.pkm img_copy.ppm Decompresses img.pkm to img_copy.ppm\n");
- printf(" etcpack -s slow img.ppm img.pkm Compress using the slow mode.\n");
- printf(" etcpack -p orig.ppm copy.ppm Calculate PSNR between orig and copy\n");
- printf(" etcpack -f RGBA8 img.tga img.pkm Compresses img.tga to img.pkm, using \n");
- printf(" etc2 + alpha.\n");
- printf(" etcpack -f RG img.ppm img.pkm Compresses red and green channels of\n");
- printf(" img.ppm\n");
- }
- return 0;
- }
|