etcpack.cxx 604 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085
  1. //// etcpack v2.74
  2. ////
  3. //// NO WARRANTY
  4. ////
  5. //// BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE THE PROGRAM IS PROVIDED
  6. //// "AS IS". ERICSSON MAKES NO REPRESENTATIONS OF ANY KIND, EXTENDS NO
  7. //// WARRANTIES OR CONDITIONS OF ANY KIND; EITHER EXPRESS, IMPLIED OR
  8. //// STATUTORY; INCLUDING, BUT NOT LIMITED TO, EXPRESS, IMPLIED OR
  9. //// STATUTORY WARRANTIES OR CONDITIONS OF TITLE, MERCHANTABILITY,
  10. //// SATISFACTORY QUALITY, SUITABILITY AND FITNESS FOR A PARTICULAR
  11. //// PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
  12. //// PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
  13. //// THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. ERICSSON
  14. //// MAKES NO WARRANTY THAT THE MANUFACTURE, SALE, OFFERING FOR SALE,
  15. //// DISTRIBUTION, LEASE, USE OR IMPORTATION UNDER THE LICENSE WILL BE FREE
  16. //// FROM INFRINGEMENT OF PATENTS, COPYRIGHTS OR OTHER INTELLECTUAL
  17. //// PROPERTY RIGHTS OF OTHERS, AND THE VALIDITY OF THE LICENSE IS SUBJECT
  18. //// TO YOUR SOLE RESPONSIBILITY TO MAKE SUCH DETERMINATION AND ACQUIRE
  19. //// SUCH LICENSES AS MAY BE NECESSARY WITH RESPECT TO PATENTS, COPYRIGHT
  20. //// AND OTHER INTELLECTUAL PROPERTY OF THIRD PARTIES.
  21. ////
  22. //// FOR THE AVOIDANCE OF DOUBT THE PROGRAM (I) IS NOT LICENSED FOR; (II)
  23. //// IS NOT DESIGNED FOR OR INTENDED FOR; AND (III) MAY NOT BE USED FOR;
  24. //// ANY MISSION CRITICAL APPLICATIONS SUCH AS, BUT NOT LIMITED TO
  25. //// OPERATION OF NUCLEAR OR HEALTHCARE COMPUTER SYSTEMS AND/OR NETWORKS,
  26. //// AIRCRAFT OR TRAIN CONTROL AND/OR COMMUNICATION SYSTEMS OR ANY OTHER
  27. //// COMPUTER SYSTEMS AND/OR NETWORKS OR CONTROL AND/OR COMMUNICATION
  28. //// SYSTEMS ALL IN WHICH CASE THE FAILURE OF THE PROGRAM COULD LEAD TO
  29. //// DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL, MATERIAL OR ENVIRONMENTAL
  30. //// DAMAGE. YOUR RIGHTS UNDER THIS LICENSE WILL TERMINATE AUTOMATICALLY
  31. //// AND IMMEDIATELY WITHOUT NOTICE IF YOU FAIL TO COMPLY WITH THIS
  32. //// PARAGRAPH.
  33. ////
  34. //// IN NO EVENT WILL ERICSSON, BE LIABLE FOR ANY DAMAGES WHATSOEVER,
  35. //// INCLUDING BUT NOT LIMITED TO PERSONAL INJURY, ANY GENERAL, SPECIAL,
  36. //// INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR IN
  37. //// CONNECTION WITH THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
  38. //// NOT LIMITED TO LOSS OF PROFITS, BUSINESS INTERUPTIONS, OR ANY OTHER
  39. //// COMMERCIAL DAMAGES OR LOSSES, LOSS OF DATA OR DATA BEING RENDERED
  40. //// INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF
  41. //// THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) REGARDLESS OF THE
  42. //// THEORY OF LIABILITY (CONTRACT, TORT OR OTHERWISE), EVEN IF SUCH HOLDER
  43. //// OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  44. ////
  45. //// (C) Ericsson AB 2005-2013. All Rights Reserved.
  46. ////
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <math.h>
  51. #include <time.h>
  52. #include <sys/timeb.h>
  53. #include "image.h"
  54. // Typedefs
  55. typedef unsigned char uint8;
  56. typedef unsigned short uint16;
  57. typedef short int16;
  58. // Functions needed for decrompession ---- in etcdec.cxx
  59. void read_big_endian_2byte_word(unsigned short *blockadr, FILE *f);
  60. void read_big_endian_4byte_word(unsigned int *blockadr, FILE *f);
  61. void unstuff57bits(unsigned int planar_word1, unsigned int planar_word2, unsigned int &planar57_word1, unsigned int &planar57_word2);
  62. void unstuff59bits(unsigned int thumbT_word1, unsigned int thumbT_word2, unsigned int &thumbT59_word1, unsigned int &thumbT59_word2);
  63. void unstuff58bits(unsigned int thumbH_word1, unsigned int thumbH_word2, unsigned int &thumbH58_word1, unsigned int &thumbH58_word2);
  64. void decompressColor(int R_B, int G_B, int B_B, uint8 (colors_RGB444)[2][3], uint8 (colors)[2][3]);
  65. void calculatePaintColors59T(uint8 d, uint8 p, uint8 (colors)[2][3], uint8 (possible_colors)[4][3]);
  66. void calculatePaintColors58H(uint8 d, uint8 p, uint8 (colors)[2][3], uint8 (possible_colors)[4][3]);
  67. void decompressBlockTHUMB59T(unsigned int block_part1, unsigned int block_part2, uint8 *img,int width,int height,int startx,int starty);
  68. void decompressBlockTHUMB58H(unsigned int block_part1, unsigned int block_part2, uint8 *img,int width,int height,int startx,int starty);
  69. void decompressBlockPlanar57(unsigned int compressed57_1, unsigned int compressed57_2, uint8 *img,int width,int height,int startx,int starty);
  70. void decompressBlockDiffFlip(unsigned int block_part1, unsigned int block_part2, uint8 *img,int width,int height,int startx,int starty);
  71. void decompressBlockETC2(unsigned int block_part1, unsigned int block_part2, uint8 *img,int width,int height,int startx,int starty);
  72. void decompressBlockDifferentialWithAlpha(unsigned int block_part1,unsigned int block_part2, uint8* img, uint8* alpha, int width, int height, int startx, int starty);
  73. void decompressBlockETC21BitAlpha(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alphaimg, int width,int height,int startx,int starty);
  74. void decompressBlockTHUMB58HAlpha(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alpha,int width,int height,int startx,int starty);
  75. void decompressBlockTHUMB59TAlpha(unsigned int block_part1, unsigned int block_part2, uint8 *img, uint8* alpha,int width,int height,int startx,int starty);
  76. uint8 getbit(uint8 input, int frompos, int topos);
  77. int clamp(int val);
  78. void decompressBlockAlpha(uint8* data,uint8* img,int width,int height,int ix,int iy);
  79. uint16 get16bits11bits(int base, int table, int mul, int index);
  80. void decompressBlockAlpha16bit(uint8* data,uint8* img,int width,int height,int ix,int iy);
  81. int16 get16bits11signed(int base, int table, int mul, int index);
  82. void setupAlphaTable();
  83. // This source code is quite long. You can make it shorter by not including the
  84. // code doing the exhaustive code. Then the -slow modes will not work, but the
  85. // code will be approximately half the number of lines of code.
  86. // Then the lines between "exhaustive code starts here" and "exhaustive code ends here"
  87. // can then be removed.
  88. #define EXHAUSTIVE_CODE_ACTIVE 1
  89. // Remove warnings for unsafe functions such as strcpy
  90. #pragma warning(disable : 4996)
  91. // Remove warnings for conversions between different time variables
  92. #pragma warning(disable : 4244)
  93. // Remove warnings for negative or too big shifts
  94. //#pragma warning(disable : 4293)
  95. #define CLAMP(ll,x,ul) (((x)<(ll)) ? (ll) : (((x)>(ul)) ? (ul) : (x)))
  96. // The below code works as CLAMP(0, x, 255) if x < 255
  97. #define CLAMP_LEFT_ZERO(x) ((~(((int)(x))>>31))&(x))
  98. // The below code works as CLAMP(0, x, 255) if x is in [0,511]
  99. #define CLAMP_RIGHT_255(x) (((( ((((int)(x))<<23)>>31) ))|(x))&0x000000ff)
  100. #define SQUARE(x) ((x)*(x))
  101. #define JAS_ROUND(x) (((x) < 0.0 ) ? ((int)((x)-0.5)) : ((int)((x)+0.5)))
  102. #define JAS_MIN(a,b) ((a) < (b) ? (a) : (b))
  103. #define JAS_MAX(a,b) ((a) > (b) ? (a) : (b))
  104. // The error metric Wr Wg Wb should be definied so that Wr^2 + Wg^2 + Wb^2 = 1.
  105. // Hence it is easier to first define the squared values and derive the weights
  106. // as their square-roots.
  107. #define PERCEPTUAL_WEIGHT_R_SQUARED 0.299
  108. #define PERCEPTUAL_WEIGHT_G_SQUARED 0.587
  109. #define PERCEPTUAL_WEIGHT_B_SQUARED 0.114
  110. #define PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000 299
  111. #define PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000 587
  112. #define PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000 114
  113. #define RED(img,width,x,y) img[3*(y*width+x)+0]
  114. #define GREEN(img,width,x,y) img[3*(y*width+x)+1]
  115. #define BLUE(img,width,x,y) img[3*(y*width+x)+2]
  116. #define SHIFT(size,startpos) ((startpos)-(size)+1)
  117. #define MASK(size, startpos) (((2<<(size-1))-1) << SHIFT(size,startpos))
  118. #define PUTBITS( dest, data, size, startpos) dest = ((dest & ~MASK(size, startpos)) | ((data << SHIFT(size, startpos)) & MASK(size,startpos)))
  119. #define SHIFTHIGH(size, startpos) (((startpos)-32)-(size)+1)
  120. #define MASKHIGH(size, startpos) (((1<<(size))-1) << SHIFTHIGH(size,startpos))
  121. #define PUTBITSHIGH(dest, data, size, startpos) dest = ((dest & ~MASKHIGH(size, startpos)) | ((data << SHIFTHIGH(size, startpos)) & MASKHIGH(size,startpos)))
  122. #define GETBITS(source, size, startpos) (( (source) >> ((startpos)-(size)+1) ) & ((1<<(size)) -1))
  123. #define GETBITSHIGH(source, size, startpos) (( (source) >> (((startpos)-32)-(size)+1) ) & ((1<<(size)) -1))
  124. // Thumb macros and definitions
  125. #define R_BITS59T 4
  126. #define G_BITS59T 4
  127. #define B_BITS59T 4
  128. #define R_BITS58H 4
  129. #define G_BITS58H 4
  130. #define B_BITS58H 4
  131. #define MAXIMUM_ERROR (255*255*16*1000)
  132. #define R 0
  133. #define G 1
  134. #define B 2
  135. #define BLOCKHEIGHT 4
  136. #define BLOCKWIDTH 4
  137. #define BINPOW(power) (1<<(power))
  138. //#define RADIUS 2
  139. #define TABLE_BITS_59T 3
  140. #define TABLE_BITS_58H 3
  141. // Global tables
  142. static uint8 table59T[8] = {3,6,11,16,23,32,41,64}; // 3-bit table for the 59 bit T-mode
  143. static uint8 table58H[8] = {3,6,11,16,23,32,41,64}; // 3-bit table for the 58 bit H-mode
  144. uint8 weight[3] = {1,1,1}; // Color weight
  145. // Enums
  146. static enum{PATTERN_H = 0,
  147. PATTERN_T = 1};
  148. static enum{MODE_ETC1, MODE_THUMB_T, MODE_THUMB_H, MODE_PLANAR};
  149. // The ETC2 package of codecs includes the following codecs:
  150. //
  151. // codec enum
  152. // --------------------------------------------------------
  153. // GL_COMPRESSED_R11_EAC 0x9270
  154. // GL_COMPRESSED_SIGNED_R11_EAC 0x9271
  155. // GL_COMPRESSED_RG11_EAC 0x9272
  156. // GL_COMPRESSED_SIGNED_RG11_EAC 0x9273
  157. // GL_COMPRESSED_RGB8_ETC2 0x9274
  158. // GL_COMPRESSED_SRGB8_ETC2 0x9275
  159. // GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  160. // GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  161. // GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  162. // GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  163. //
  164. // The older codec ETC1 is not included in the package
  165. // GL_ETC1_RGB8_OES 0x8d64
  166. // but since ETC2 is backwards compatible an ETC1 texture can
  167. // be decoded using the RGB8_ETC2 enum (0x9274)
  168. //
  169. // In a PKM-file, the codecs are stored using the following identifiers
  170. //
  171. // identifier value codec
  172. // --------------------------------------------------------------------
  173. // ETC1_RGB_NO_MIPMAPS 0 GL_ETC1_RGB8_OES
  174. // ETC2PACKAGE_RGB_NO_MIPMAPS 1 GL_COMPRESSED_RGB8_ETC2
  175. // ETC2PACKAGE_RGBA_NO_MIPMAPS_OLD 2, not used -
  176. // ETC2PACKAGE_RGBA_NO_MIPMAPS 3 GL_COMPRESSED_RGBA8_ETC2_EAC
  177. // ETC2PACKAGE_RGBA1_NO_MIPMAPS 4 GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
  178. // ETC2PACKAGE_R_NO_MIPMAPS 5 GL_COMPRESSED_R11_EAC
  179. // ETC2PACKAGE_RG_NO_MIPMAPS 6 GL_COMPRESSED_RG11_EAC
  180. // ETC2PACKAGE_R_SIGNED_NO_MIPMAPS 7 GL_COMPRESSED_SIGNED_R11_EAC
  181. // ETC2PACKAGE_RG_SIGNED_NO_MIPMAPS 8 GL_COMPRESSED_SIGNED_RG11_EAC
  182. //
  183. // In the code, the identifiers are not always used strictly. For instance, the
  184. // identifier ETC2PACKAGE_R_NO_MIPMAPS is sometimes used for both the unsigned
  185. // (GL_COMPRESSED_R11_EAC) and signed (GL_COMPRESSED_SIGNED_R11_EAC) version of
  186. // the codec.
  187. //
  188. 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};
  189. static enum {MODE_COMPRESS, MODE_UNCOMPRESS, MODE_PSNR};
  190. static enum {SPEED_SLOW, SPEED_FAST, SPEED_MEDIUM};
  191. static enum {METRIC_PERCEPTUAL, METRIC_NONPERCEPTUAL};
  192. static enum {CODEC_ETC, CODEC_ETC2};
  193. int mode = MODE_COMPRESS;
  194. int speed = SPEED_FAST;
  195. int metric = METRIC_PERCEPTUAL;
  196. int codec = CODEC_ETC2;
  197. int format = ETC2PACKAGE_RGB_NO_MIPMAPS;
  198. int verbose = true;
  199. extern int formatSigned;
  200. int ktxFile=0;
  201. bool first_time_message = true;
  202. static int scramble[4] = {3, 2, 0, 1};
  203. static int unscramble[4] = {2, 3, 1, 0};
  204. typedef struct KTX_header_t
  205. {
  206. uint8 identifier[12];
  207. unsigned int endianness;
  208. unsigned int glType;
  209. unsigned int glTypeSize;
  210. unsigned int glFormat;
  211. unsigned int glInternalFormat;
  212. unsigned int glBaseInternalFormat;
  213. unsigned int pixelWidth;
  214. unsigned int pixelHeight;
  215. unsigned int pixelDepth;
  216. unsigned int numberOfArrayElements;
  217. unsigned int numberOfFaces;
  218. unsigned int numberOfMipmapLevels;
  219. unsigned int bytesOfKeyValueData;
  220. }
  221. KTX_header;
  222. #define KTX_IDENTIFIER_REF { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A }
  223. #define KTX_ENDIAN_REF (0x04030201)
  224. #define KTX_ENDIAN_REF_REV (0x01020304)
  225. static enum {GL_R=0x1903,GL_RG=0x8227,GL_RGB=0x1907,GL_RGBA=0x1908};
  226. #define GL_SRGB 0x8C40
  227. #define GL_SRGB8 0x8C41
  228. #define GL_SRGB8_ALPHA8 0x8C43
  229. #define GL_ETC1_RGB8_OES 0x8d64
  230. #define GL_COMPRESSED_R11_EAC 0x9270
  231. #define GL_COMPRESSED_SIGNED_R11_EAC 0x9271
  232. #define GL_COMPRESSED_RG11_EAC 0x9272
  233. #define GL_COMPRESSED_SIGNED_RG11_EAC 0x9273
  234. #define GL_COMPRESSED_RGB8_ETC2 0x9274
  235. #define GL_COMPRESSED_SRGB8_ETC2 0x9275
  236. #define GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  237. #define GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  238. #define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  239. #define GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  240. int ktx_identifier[] = KTX_IDENTIFIER_REF;
  241. //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
  242. // 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.
  243. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  244. int indexConversion(int pixelIndices)
  245. {
  246. int correctIndices = 0;
  247. int LSB[4][4];
  248. int MSB[4][4];
  249. int shift=0;
  250. for(int y=3; y>=0; y--)
  251. {
  252. for(int x=3; x>=0; x--)
  253. {
  254. LSB[x][y] = (pixelIndices>>shift)&1;
  255. shift++;
  256. MSB[x][y] = (pixelIndices>>shift)&1;
  257. shift++;
  258. }
  259. }
  260. shift=0;
  261. for(int x=0; x<4; x++)
  262. {
  263. for(int y=0; y<4; y++)
  264. {
  265. correctIndices|=(LSB[x][y]<<shift);
  266. correctIndices|=(MSB[x][y]<<(16+shift));
  267. shift++;
  268. }
  269. }
  270. return correctIndices;
  271. }
  272. // Tests if a file exists.
  273. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  274. bool fileExist(char *filename)
  275. {
  276. FILE *f=NULL;
  277. if((f=fopen(filename,"rb"))!=NULL)
  278. {
  279. fclose(f);
  280. return true;
  281. }
  282. return false;
  283. }
  284. // Expand source image so that it is divisible by a factor of four in the x-dimension.
  285. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  286. bool expandToWidthDivByFour(uint8 *&img, int width, int height, int &expandedwidth, int &expandedheight, int bitrate)
  287. {
  288. int wdiv4;
  289. int xx, yy;
  290. uint8 *newimg;
  291. wdiv4 = width /4;
  292. if( !(wdiv4 *4 == width) )
  293. {
  294. expandedwidth = (wdiv4 + 1)*4;
  295. expandedheight = height;
  296. newimg=(uint8*) malloc(3*expandedwidth*expandedheight*bitrate/8);
  297. if(!newimg)
  298. {
  299. printf("Could not allocate memory to expand width\n");
  300. return false;
  301. }
  302. // First copy image
  303. for(yy = 0; yy<height; yy++)
  304. {
  305. for(xx = 0; xx < width; xx++)
  306. {
  307. //we have 3*bitrate/8 bytes for each pixel..
  308. for(int i=0; i<3*bitrate/8; i++)
  309. {
  310. newimg[(yy * expandedwidth+ xx)*3*bitrate/8 + i] = img[(yy * width+xx)*3*bitrate/8 + i];
  311. }
  312. }
  313. }
  314. // Then make the last column of pixels the same as the previous column.
  315. for(yy = 0; yy< height; yy++)
  316. {
  317. for(xx = width; xx < expandedwidth; xx++)
  318. {
  319. for(int i=0; i<3*bitrate/8; i++)
  320. {
  321. newimg[(yy * expandedwidth+xx)*3*bitrate/8 + i] = img[(yy * width+(width-1))*3*bitrate/8 + i];
  322. }
  323. }
  324. }
  325. // Now free the old image
  326. free(img);
  327. // Use the new image
  328. img = newimg;
  329. return true;
  330. }
  331. else
  332. {
  333. printf("Image already of even width\n");
  334. expandedwidth = width;
  335. expandedheight = height;
  336. return false;
  337. }
  338. }
  339. // Expand source image so that it is divisible by a factor of four in the y-dimension.
  340. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  341. bool expandToHeightDivByFour(uint8 *&img, int width, int height, int &expandedwidth, int &expandedheight, int bitrate)
  342. {
  343. int hdiv4;
  344. int xx, yy;
  345. int numlinesmissing;
  346. uint8 *newimg;
  347. hdiv4 = height/4;
  348. if( !(hdiv4 * 4 == height) )
  349. {
  350. expandedwidth = width;
  351. expandedheight = (hdiv4 + 1) * 4;
  352. numlinesmissing = expandedheight - height;
  353. newimg=(uint8*)malloc(3*expandedwidth*expandedheight*bitrate/8);
  354. if(!newimg)
  355. {
  356. printf("Could not allocate memory to expand height\n");
  357. return false;
  358. }
  359. // First copy image. No need to reformat data.
  360. for(xx = 0; xx<3*width*height*bitrate/8; xx++)
  361. newimg[xx] = img[xx];
  362. // Then copy up to three lines.
  363. for(yy = height; yy < height + numlinesmissing; yy++)
  364. {
  365. for(xx = 0; xx<width; xx++)
  366. {
  367. for(int i=0; i<3*bitrate/8; i++)
  368. {
  369. newimg[(yy*width+xx)*3*bitrate/8 + i] = img[((height-1)*width+xx)*3*bitrate/8 + i];
  370. }
  371. }
  372. }
  373. // Now free the old image;
  374. free(img);
  375. // Use the new image:
  376. img = newimg;
  377. return true;
  378. }
  379. else
  380. {
  381. printf("Image height already divisible by four.\n");
  382. expandedwidth = width;
  383. expandedheight = height;
  384. return true;
  385. }
  386. }
  387. // Find the position of a file extension such as .ppm or .pkm
  388. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  389. int find_pos_of_extension(char *src)
  390. {
  391. int q=strlen(src);
  392. while(q>=0) // find file name extension
  393. {
  394. if(src[q]=='.') break;
  395. q--;
  396. }
  397. if(q<0)
  398. return -1;
  399. else
  400. return q;
  401. }
  402. // Read source file. Does conversion if file format is not .ppm.
  403. // Will expand file to be divisible by four in the x- and y- dimension.
  404. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  405. bool readSrcFile(char *filename,uint8 *&img,int &width,int &height, int &expandedwidth, int &expandedheight)
  406. {
  407. int w1,h1;
  408. int wdiv4, hdiv4;
  409. char str[255];
  410. // Delete temp file if it exists.
  411. if(fileExist("tmp.ppm"))
  412. {
  413. sprintf(str, "del tmp.ppm\n");
  414. system(str);
  415. }
  416. int q = find_pos_of_extension(filename);
  417. if(!strcmp(&filename[q],".ppm"))
  418. {
  419. // Already a .ppm file. Just copy.
  420. sprintf(str,"copy %s tmp.ppm \n", filename);
  421. printf("Copying source file to tmp.ppm\n", filename);
  422. }
  423. else
  424. {
  425. // Converting from other format to .ppm
  426. //
  427. // Use your favorite command line image converter program,
  428. // for instance Image Magick. Just make sure the syntax can
  429. // be written as below:
  430. //
  431. // C:\magick convert source.jpg dest.ppm
  432. //
  433. sprintf(str,"magick convert %s tmp.ppm\n", filename);
  434. printf("Converting source file from %s to .ppm\n", filename);
  435. }
  436. // Execute system call
  437. system(str);
  438. int bitrate=8;
  439. if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  440. bitrate=16;
  441. if(fReadPPM("tmp.ppm",w1,h1,img,bitrate))
  442. {
  443. width=w1;
  444. height=h1;
  445. system("del tmp.ppm");
  446. // Width must be divisible by 4 and height must be
  447. // divisible by 4. Otherwise, we will expand the image
  448. wdiv4 = width / 4;
  449. hdiv4 = height / 4;
  450. expandedwidth = width;
  451. expandedheight = height;
  452. if( !(wdiv4 * 4 == width) )
  453. {
  454. printf(" Width = %d is not divisible by four... ", width);
  455. printf(" expanding image in x-dir... ");
  456. if(expandToWidthDivByFour(img, width, height, expandedwidth, expandedheight,bitrate))
  457. {
  458. printf("OK.\n");
  459. }
  460. else
  461. {
  462. printf("\n Error: could not expand image\n");
  463. return false;
  464. }
  465. }
  466. if( !(hdiv4 * 4 == height))
  467. {
  468. printf(" Height = %d is not divisible by four... ", height);
  469. printf(" expanding image in y-dir...");
  470. if(expandToHeightDivByFour(img, expandedwidth, height, expandedwidth, expandedheight,bitrate))
  471. {
  472. printf("OK.\n");
  473. }
  474. else
  475. {
  476. printf("\n Error: could not expand image\n");
  477. return false;
  478. }
  479. }
  480. if(!(expandedwidth == width && expandedheight == height))
  481. printf("Active pixels: %dx%d. Expanded image: %dx%d\n",width,height,expandedwidth,expandedheight);
  482. return true;
  483. }
  484. else
  485. {
  486. printf("Could not read tmp.ppm file\n");
  487. exit(1);
  488. }
  489. return false;
  490. }
  491. // Reads a file without expanding it to be divisible by 4.
  492. // Is used when doing PSNR calculation between two files.
  493. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  494. bool readSrcFileNoExpand(char *filename,uint8 *&img,int &width,int &height)
  495. {
  496. int w1,h1;
  497. char str[255];
  498. // Delete temp file if it exists.
  499. if(fileExist("tmp.ppm"))
  500. {
  501. sprintf(str, "del tmp.ppm\n");
  502. system(str);
  503. }
  504. int q = find_pos_of_extension(filename);
  505. if(!strcmp(&filename[q],".ppm"))
  506. {
  507. // Already a .ppm file. Just copy.
  508. sprintf(str,"copy %s tmp.ppm \n", filename);
  509. printf("Copying source file to tmp.ppm\n", filename);
  510. }
  511. else
  512. {
  513. // Converting from other format to .ppm
  514. //
  515. // Use your favorite command line image converter program,
  516. // for instance Image Magick. Just make sure the syntax can
  517. // be written as below:
  518. //
  519. // C:\magick convert source.jpg dest.ppm
  520. //
  521. sprintf(str,"magick convert %s tmp.ppm\n", filename);
  522. // printf("Converting source file from %s to .ppm\n", filename);
  523. }
  524. // Execute system call
  525. system(str);
  526. if(fReadPPM("tmp.ppm",w1,h1,img,8))
  527. {
  528. width=w1;
  529. height=h1;
  530. system("del tmp.ppm");
  531. return true;
  532. }
  533. return false;
  534. }
  535. // Parses the arguments from the command line.
  536. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  537. void readArguments(int argc,char *argv[],char* src,char *dst)
  538. {
  539. int q;
  540. //new code!! do this in a more nicer way!
  541. bool srcfound=false,dstfound=false;
  542. for(int i=1; i<argc; i++)
  543. {
  544. //loop through the arguments!
  545. //first check for flags..
  546. if(argv[i][0]=='-')
  547. {
  548. if(i==argc-1)
  549. {
  550. printf("flag missing argument: %s!\n");
  551. exit(1);
  552. }
  553. //handle speed flag
  554. if(!strcmp(argv[i],"-s"))
  555. {
  556. // We have argument -s. Now check for slow, medium or fast.
  557. if(!strcmp(argv[i+1],"slow"))
  558. speed = SPEED_SLOW;
  559. else if(!strcmp(argv[i+1],"medium"))
  560. speed = SPEED_MEDIUM;
  561. else if(!strcmp(argv[i+1],"fast"))
  562. speed = SPEED_FAST;
  563. else
  564. {
  565. printf("Error: %s not part of flag %s\n",argv[i+1], argv[i]);
  566. exit(1);
  567. }
  568. }
  569. //handle verbose flag
  570. else if(!strcmp(argv[i],"-v"))
  571. {
  572. // We have argument -s. Now check for slow, medium or fast.
  573. if(!strcmp(argv[i+1],"off"))
  574. verbose = false;
  575. else if(!strcmp(argv[i+1],"on"))
  576. verbose = true;
  577. else
  578. {
  579. printf("Error: %s not part of flag %s\n",argv[i+1], argv[i]);
  580. exit(1);
  581. }
  582. }
  583. //error metric flag
  584. else if(!strcmp(argv[i],"-e"))
  585. {
  586. // We have argument -e. Now check for perceptual or nonperceptual
  587. if(!strcmp(argv[i+1],"perceptual"))
  588. metric = METRIC_PERCEPTUAL;
  589. else if(!strcmp(argv[i+1],"nonperceptual"))
  590. metric = METRIC_NONPERCEPTUAL;
  591. else
  592. {
  593. printf("Error: %s not part of flag %s\n",argv[i+1], argv[i]);
  594. exit(1);
  595. }
  596. }
  597. //codec flag
  598. else if(!strcmp(argv[i],"-c"))
  599. {
  600. // We have argument -c. Now check for perceptual or nonperceptual
  601. if(!strcmp(argv[i+1],"etc") || !strcmp(argv[i+1],"etc1"))
  602. codec = CODEC_ETC;
  603. else if(!strcmp(argv[i+1],"etc2"))
  604. codec = CODEC_ETC2;
  605. else
  606. {
  607. printf("Error: %s not part of flag %s\n",argv[i+1], argv[i]);
  608. exit(1);
  609. }
  610. }
  611. //format flag
  612. else if(!strcmp(argv[i],"-f"))
  613. {
  614. if(!strcmp(argv[i+1],"R"))
  615. format=ETC2PACKAGE_R_NO_MIPMAPS;
  616. else if(!strcmp(argv[i+1],"RG"))
  617. format=ETC2PACKAGE_RG_NO_MIPMAPS;
  618. else if(!strcmp(argv[i+1],"R_signed"))
  619. {
  620. format=ETC2PACKAGE_R_NO_MIPMAPS;
  621. formatSigned=1;
  622. }
  623. else if(!strcmp(argv[i+1],"RG_signed"))
  624. {
  625. format=ETC2PACKAGE_RG_NO_MIPMAPS;
  626. formatSigned=1;
  627. }
  628. else if(!strcmp(argv[i+1],"RGB"))
  629. format=ETC2PACKAGE_RGB_NO_MIPMAPS;
  630. else if(!strcmp(argv[i+1],"sRGB"))
  631. format=ETC2PACKAGE_sRGB_NO_MIPMAPS;
  632. else if(!strcmp(argv[i+1],"RGBA")||!strcmp(argv[i+1],"RGBA8"))
  633. format=ETC2PACKAGE_RGBA_NO_MIPMAPS;
  634. else if(!strcmp(argv[i+1],"sRGBA")||!strcmp(argv[i+1],"sRGBA8"))
  635. format=ETC2PACKAGE_sRGBA_NO_MIPMAPS;
  636. else if(!strcmp(argv[i+1],"RGBA1"))
  637. format=ETC2PACKAGE_RGBA1_NO_MIPMAPS;
  638. else if(!strcmp(argv[i+1],"sRGBA1"))
  639. format=ETC2PACKAGE_sRGBA1_NO_MIPMAPS;
  640. else
  641. {
  642. printf("Error: %s not part of flag %s\n",argv[i+1], argv[i]);
  643. exit(1);
  644. }
  645. }
  646. else if(!strcmp(argv[i],"-p"))
  647. {
  648. mode=MODE_PSNR;
  649. i--; //ugly way of negating the increment of i done later because -p doesn't have an argument.
  650. }
  651. else
  652. {
  653. printf("Error: cannot interpret flag %s %s\n",argv[i], argv[i+1]);
  654. exit(1);
  655. }
  656. //don't read the flag argument next iteration..
  657. i++;
  658. }
  659. //this isn't a flag, so must be src or dst
  660. else
  661. {
  662. if(srcfound&&dstfound)
  663. {
  664. printf("too many arguments! expecting src, dst; found %s, %s, %s\n",src,dst,argv[i]);
  665. exit(1);
  666. }
  667. else if(srcfound)
  668. {
  669. strcpy(dst,argv[i]);
  670. dstfound=true;
  671. }
  672. else
  673. {
  674. strcpy(src,argv[i]);
  675. srcfound=true;
  676. }
  677. }
  678. }
  679. if(!srcfound&&dstfound)
  680. {
  681. printf("too few arguments! expecting src, dst\n");
  682. exit(1);
  683. }
  684. if(mode==MODE_PSNR)
  685. return;
  686. //check source/destination.. is this compression or decompression?
  687. q = find_pos_of_extension(src);
  688. if(q<0)
  689. {
  690. printf("invalid source file: %s\n",src);
  691. exit(1);
  692. }
  693. // If we have etcpack img.pkm img.any
  694. if(!strncmp(&src[q],".pkm",4))
  695. {
  696. // First argument is .pkm. Decompress.
  697. mode = MODE_UNCOMPRESS; // uncompress from binary file format .pkm
  698. }
  699. else if(!strncmp(&src[q],".ktx",4))
  700. {
  701. // First argument is .ktx. Decompress.
  702. mode = MODE_UNCOMPRESS; // uncompress from binary file format .pkm
  703. ktxFile=true;
  704. printf("decompressing ktx\n");
  705. }
  706. else
  707. {
  708. // The first argument was not .pkm. The second argument must then be .pkm.
  709. q = find_pos_of_extension(dst);
  710. if(q<0)
  711. {
  712. printf("invalid destination file: %s\n",src);
  713. exit(1);
  714. }
  715. if(!strncmp(&dst[q],".pkm",4))
  716. {
  717. // Second argument is .pkm. Compress.
  718. mode = MODE_COMPRESS; // compress to binary file format .pkm
  719. }
  720. else if(!strncmp(&dst[q],".ktx",4))
  721. {
  722. // Second argument is .ktx. Compress.
  723. ktxFile=true;
  724. mode = MODE_COMPRESS; // compress to binary file format .pkm
  725. printf("compressing to ktx\n");
  726. }
  727. else
  728. {
  729. printf("source or destination must be a .pkm or .ktx file\n");
  730. exit(1);
  731. }
  732. }
  733. //do some sanity check stuff..
  734. if(codec==CODEC_ETC&&format!=ETC2PACKAGE_RGB_NO_MIPMAPS)
  735. {
  736. printf("ETC1 codec only supports RGB format\n");
  737. exit(1);
  738. }
  739. else if(codec==CODEC_ETC)
  740. format=ETC1_RGB_NO_MIPMAPS;
  741. }
  742. static int compressParams[16][4];
  743. const int compressParamsFast[32] = { -8, -2, 2, 8,
  744. -17, -5, 5, 17,
  745. -29, -9, 9, 29,
  746. -42, -13, 13, 42,
  747. -60, -18, 18, 60,
  748. -80, -24, 24, 80,
  749. -106, -33, 33, 106,
  750. -183, -47, 47, 183};
  751. bool readCompressParams(void)
  752. {
  753. compressParams[0][0] = -8; compressParams[0][1] = -2; compressParams[0][2] = 2; compressParams[0][3] = 8;
  754. compressParams[1][0] = -8; compressParams[1][1] = -2; compressParams[1][2] = 2; compressParams[1][3] = 8;
  755. compressParams[2][0] = -17; compressParams[2][1] = -5; compressParams[2][2] = 5; compressParams[2][3] = 17;
  756. compressParams[3][0] = -17; compressParams[3][1] = -5; compressParams[3][2] = 5; compressParams[3][3] = 17;
  757. compressParams[4][0] = -29; compressParams[4][1] = -9; compressParams[4][2] = 9; compressParams[4][3] = 29;
  758. compressParams[5][0] = -29; compressParams[5][1] = -9; compressParams[5][2] = 9; compressParams[5][3] = 29;
  759. compressParams[6][0] = -42; compressParams[6][1] = -13; compressParams[6][2] = 13; compressParams[6][3] = 42;
  760. compressParams[7][0] = -42; compressParams[7][1] = -13; compressParams[7][2] = 13; compressParams[7][3] = 42;
  761. compressParams[8][0] = -60; compressParams[8][1] = -18; compressParams[8][2] = 18; compressParams[8][3] = 60;
  762. compressParams[9][0] = -60; compressParams[9][1] = -18; compressParams[9][2] = 18; compressParams[9][3] = 60;
  763. compressParams[10][0] = -80; compressParams[10][1] = -24; compressParams[10][2] = 24; compressParams[10][3] = 80;
  764. compressParams[11][0] = -80; compressParams[11][1] = -24; compressParams[11][2] = 24; compressParams[11][3] = 80;
  765. compressParams[12][0] =-106; compressParams[12][1] = -33; compressParams[12][2] = 33; compressParams[12][3] = 106;
  766. compressParams[13][0] =-106; compressParams[13][1] = -33; compressParams[13][2] = 33; compressParams[13][3] = 106;
  767. compressParams[14][0] =-183; compressParams[14][1] = -47; compressParams[14][2] = 47; compressParams[14][3] = 183;
  768. compressParams[15][0] =-183; compressParams[15][1] = -47; compressParams[15][2] = 47; compressParams[15][3] = 183;
  769. return true;
  770. }
  771. // Computes the average color in a 2x4 area and returns the average color as a float.
  772. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  773. void computeAverageColor2x4noQuantFloat(uint8 *img,int width,int height,int startx,int starty,float *avg_color)
  774. {
  775. int r=0,g=0,b=0;
  776. for(int y=starty; y<starty+4; y++)
  777. {
  778. for(int x=startx; x<startx+2; x++)
  779. {
  780. r+=RED(img,width,x,y);
  781. g+=GREEN(img,width,x,y);
  782. b+=BLUE(img,width,x,y);
  783. }
  784. }
  785. avg_color[0]=(float)(r/8.0);
  786. avg_color[1]=(float)(g/8.0);
  787. avg_color[2]=(float)(b/8.0);
  788. }
  789. // Computes the average color in a 4x2 area and returns the average color as a float.
  790. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  791. void computeAverageColor4x2noQuantFloat(uint8 *img,int width,int height,int startx,int starty,float *avg_color)
  792. {
  793. int r=0,g=0,b=0;
  794. for(int y=starty; y<starty+2; y++)
  795. {
  796. for(int x=startx; x<startx+4; x++)
  797. {
  798. r+=RED(img,width,x,y);
  799. g+=GREEN(img,width,x,y);
  800. b+=BLUE(img,width,x,y);
  801. }
  802. }
  803. avg_color[0]=(float)(r/8.0);
  804. avg_color[1]=(float)(g/8.0);
  805. avg_color[2]=(float)(b/8.0);
  806. }
  807. // Finds all pixel indices for a 2x4 block.
  808. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  809. 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)
  810. {
  811. uint8 orig[3],approx[3];
  812. unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
  813. int sum_error=0;
  814. int q, i;
  815. i = 0;
  816. for(int x=startx; x<startx+2; x++)
  817. {
  818. for(int y=starty; y<starty+4; y++)
  819. {
  820. int err;
  821. int best=0;
  822. int min_error=255*255*3*16;
  823. orig[0]=RED(img,width,x,y);
  824. orig[1]=GREEN(img,width,x,y);
  825. orig[2]=BLUE(img,width,x,y);
  826. for(q=0;q<4;q++)
  827. {
  828. approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
  829. approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
  830. approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
  831. // Here we just use equal weights to R, G and B. Although this will
  832. // give visually worse results, it will give a better PSNR score.
  833. err=SQUARE(approx[0]-orig[0]) + SQUARE(approx[1]-orig[1]) + SQUARE(approx[2]-orig[2]);
  834. if(err<min_error)
  835. {
  836. min_error=err;
  837. best=q;
  838. }
  839. }
  840. pixel_indices = scramble[best];
  841. PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
  842. PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
  843. i++;
  844. // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
  845. // so that first bit is sign bit and the other bit is size bit (4 or 12).
  846. // This means that we have to scramble the bits before storing them.
  847. sum_error+=min_error;
  848. }
  849. }
  850. *pixel_indices_MSBp = pixel_indices_MSB;
  851. *pixel_indices_LSBp = pixel_indices_LSB;
  852. return sum_error;
  853. }
  854. #define MAXERR1000 1000*255*255*16
  855. // Finds all pixel indices for a 2x4 block using perceptual weighting of error.
  856. // Done using fixed poinit arithmetics where weights are multiplied by 1000.
  857. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  858. 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)
  859. {
  860. uint8 orig[3],approx[3];
  861. unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
  862. unsigned int sum_error=0;
  863. int q, i;
  864. i = 0;
  865. for(int x=startx; x<startx+2; x++)
  866. {
  867. for(int y=starty; y<starty+4; y++)
  868. {
  869. unsigned int err;
  870. int best=0;
  871. unsigned int min_error=MAXERR1000;
  872. orig[0]=RED(img,width,x,y);
  873. orig[1]=GREEN(img,width,x,y);
  874. orig[2]=BLUE(img,width,x,y);
  875. for(q=0;q<4;q++)
  876. {
  877. approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
  878. approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
  879. approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
  880. // Here we just use equal weights to R, G and B. Although this will
  881. // give visually worse results, it will give a better PSNR score.
  882. err = (PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE((approx[0]-orig[0]))
  883. + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE((approx[1]-orig[1]))
  884. + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*SQUARE((approx[2]-orig[2])));
  885. if(err<min_error)
  886. {
  887. min_error=err;
  888. best=q;
  889. }
  890. }
  891. pixel_indices = scramble[best];
  892. PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
  893. PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
  894. i++;
  895. // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
  896. // so that first bit is sign bit and the other bit is size bit (4 or 12).
  897. // This means that we have to scramble the bits before storing them.
  898. sum_error+=min_error;
  899. }
  900. }
  901. *pixel_indices_MSBp = pixel_indices_MSB;
  902. *pixel_indices_LSBp = pixel_indices_LSB;
  903. return sum_error;
  904. }
  905. // Finds all pixel indices for a 2x4 block using perceptual weighting of error.
  906. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  907. 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)
  908. {
  909. uint8 orig[3],approx[3];
  910. unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
  911. float sum_error=0;
  912. int q, i;
  913. double wR2 = PERCEPTUAL_WEIGHT_R_SQUARED;
  914. double wG2 = PERCEPTUAL_WEIGHT_G_SQUARED;
  915. double wB2 = PERCEPTUAL_WEIGHT_B_SQUARED;
  916. i = 0;
  917. for(int x=startx; x<startx+2; x++)
  918. {
  919. for(int y=starty; y<starty+4; y++)
  920. {
  921. float err;
  922. int best=0;
  923. float min_error=255*255*3*16;
  924. orig[0]=RED(img,width,x,y);
  925. orig[1]=GREEN(img,width,x,y);
  926. orig[2]=BLUE(img,width,x,y);
  927. for(q=0;q<4;q++)
  928. {
  929. approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
  930. approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
  931. approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
  932. // Here we just use equal weights to R, G and B. Although this will
  933. // give visually worse results, it will give a better PSNR score.
  934. err=(float)(wR2*SQUARE((approx[0]-orig[0])) + (float)wG2*SQUARE((approx[1]-orig[1])) + (float)wB2*SQUARE((approx[2]-orig[2])));
  935. if(err<min_error)
  936. {
  937. min_error=err;
  938. best=q;
  939. }
  940. }
  941. pixel_indices = scramble[best];
  942. PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
  943. PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
  944. i++;
  945. // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
  946. // so that first bit is sign bit and the other bit is size bit (4 or 12).
  947. // This means that we have to scramble the bits before storing them.
  948. sum_error+=min_error;
  949. }
  950. }
  951. *pixel_indices_MSBp = pixel_indices_MSB;
  952. *pixel_indices_LSBp = pixel_indices_LSB;
  953. return sum_error;
  954. }
  955. // Finds all pixel indices for a 4x2 block.
  956. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  957. 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)
  958. {
  959. uint8 orig[3],approx[3];
  960. unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
  961. int sum_error=0;
  962. int q;
  963. int i;
  964. i = 0;
  965. for(int x=startx; x<startx+4; x++)
  966. {
  967. for(int y=starty; y<starty+2; y++)
  968. {
  969. int err;
  970. int best=0;
  971. int min_error=255*255*3*16;
  972. orig[0]=RED(img,width,x,y);
  973. orig[1]=GREEN(img,width,x,y);
  974. orig[2]=BLUE(img,width,x,y);
  975. for(q=0;q<4;q++)
  976. {
  977. approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
  978. approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
  979. approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
  980. // Here we just use equal weights to R, G and B. Although this will
  981. // give visually worse results, it will give a better PSNR score.
  982. err=SQUARE(approx[0]-orig[0]) + SQUARE(approx[1]-orig[1]) + SQUARE(approx[2]-orig[2]);
  983. if(err<min_error)
  984. {
  985. min_error=err;
  986. best=q;
  987. }
  988. }
  989. pixel_indices = scramble[best];
  990. PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
  991. PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
  992. i++;
  993. // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
  994. // so that first bit is sign bit and the other bit is size bit (4 or 12).
  995. // This means that we have to scramble the bits before storing them.
  996. sum_error+=min_error;
  997. }
  998. i+=2;
  999. }
  1000. *pixel_indices_MSBp = pixel_indices_MSB;
  1001. *pixel_indices_LSBp = pixel_indices_LSB;
  1002. return sum_error;
  1003. }
  1004. // Finds all pixel indices for a 4x2 block using perceptual weighting of error.
  1005. // Done using fixed point arithmetics where 1000 corresponds to 1.0.
  1006. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1007. 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)
  1008. {
  1009. uint8 orig[3],approx[3];
  1010. unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
  1011. unsigned int sum_error=0;
  1012. int q;
  1013. int i;
  1014. i = 0;
  1015. for(int x=startx; x<startx+4; x++)
  1016. {
  1017. for(int y=starty; y<starty+2; y++)
  1018. {
  1019. unsigned int err;
  1020. int best=0;
  1021. unsigned int min_error=MAXERR1000;
  1022. orig[0]=RED(img,width,x,y);
  1023. orig[1]=GREEN(img,width,x,y);
  1024. orig[2]=BLUE(img,width,x,y);
  1025. for(q=0;q<4;q++)
  1026. {
  1027. approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
  1028. approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
  1029. approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
  1030. // Here we just use equal weights to R, G and B. Although this will
  1031. // give visually worse results, it will give a better PSNR score.
  1032. err = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(approx[0]-orig[0])
  1033. + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(approx[1]-orig[1])
  1034. + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*SQUARE(approx[2]-orig[2]);
  1035. if(err<min_error)
  1036. {
  1037. min_error=err;
  1038. best=q;
  1039. }
  1040. }
  1041. pixel_indices = scramble[best];
  1042. PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
  1043. PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
  1044. i++;
  1045. // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
  1046. // so that first bit is sign bit and the other bit is size bit (4 or 12).
  1047. // This means that we have to scramble the bits before storing them.
  1048. sum_error+=min_error;
  1049. }
  1050. i+=2;
  1051. }
  1052. *pixel_indices_MSBp = pixel_indices_MSB;
  1053. *pixel_indices_LSBp = pixel_indices_LSB;
  1054. return sum_error;
  1055. }
  1056. // Finds all pixel indices for a 4x2 block using perceptual weighting of error.
  1057. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1058. 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)
  1059. {
  1060. uint8 orig[3],approx[3];
  1061. unsigned int pixel_indices_MSB=0, pixel_indices_LSB=0, pixel_indices = 0;
  1062. float sum_error=0;
  1063. int q;
  1064. int i;
  1065. float wR2 = (float) PERCEPTUAL_WEIGHT_R_SQUARED;
  1066. float wG2 = (float) PERCEPTUAL_WEIGHT_G_SQUARED;
  1067. float wB2 = (float) PERCEPTUAL_WEIGHT_B_SQUARED;
  1068. i = 0;
  1069. for(int x=startx; x<startx+4; x++)
  1070. {
  1071. for(int y=starty; y<starty+2; y++)
  1072. {
  1073. float err;
  1074. int best=0;
  1075. float min_error=255*255*3*16;
  1076. orig[0]=RED(img,width,x,y);
  1077. orig[1]=GREEN(img,width,x,y);
  1078. orig[2]=BLUE(img,width,x,y);
  1079. for(q=0;q<4;q++)
  1080. {
  1081. approx[0]=CLAMP(0, avg_color[0]+compressParams[table][q],255);
  1082. approx[1]=CLAMP(0, avg_color[1]+compressParams[table][q],255);
  1083. approx[2]=CLAMP(0, avg_color[2]+compressParams[table][q],255);
  1084. // Here we just use equal weights to R, G and B. Although this will
  1085. // give visually worse results, it will give a better PSNR score.
  1086. err=(float) wR2*SQUARE(approx[0]-orig[0]) + (float)wG2*SQUARE(approx[1]-orig[1]) + (float)wB2*SQUARE(approx[2]-orig[2]);
  1087. if(err<min_error)
  1088. {
  1089. min_error=err;
  1090. best=q;
  1091. }
  1092. }
  1093. pixel_indices = scramble[best];
  1094. PUTBITS( pixel_indices_MSB, (pixel_indices >> 1), 1, i);
  1095. PUTBITS( pixel_indices_LSB, (pixel_indices & 1) , 1, i);
  1096. i++;
  1097. // In order to simplify hardware, the table {-12, -4, 4, 12} is indexed {11, 10, 00, 01}
  1098. // so that first bit is sign bit and the other bit is size bit (4 or 12).
  1099. // This means that we have to scramble the bits before storing them.
  1100. sum_error+=min_error;
  1101. }
  1102. i+=2;
  1103. }
  1104. *pixel_indices_MSBp = pixel_indices_MSB;
  1105. *pixel_indices_LSBp = pixel_indices_LSB;
  1106. return sum_error;
  1107. }
  1108. // Table for fast implementation of clamping to the interval [0,255] followed by addition of 255.
  1109. 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,
  1110. 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,
  1111. 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,
  1112. 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255,
  1113. 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255, 255+255};
  1114. // Table for fast implementationi of clamping to the interval [0,255]
  1115. 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,
  1116. 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,
  1117. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
  1118. // Table for fast implementation of squaring for numbers in the interval [-255, 255]
  1119. const unsigned int square_table[511] = {65025, 64516, 64009, 63504, 63001, 62500, 62001, 61504, 61009, 60516, 60025, 59536, 59049, 58564, 58081, 57600,
  1120. 57121, 56644, 56169, 55696, 55225, 54756, 54289, 53824, 53361, 52900, 52441, 51984, 51529, 51076, 50625, 50176,
  1121. 49729, 49284, 48841, 48400, 47961, 47524, 47089, 46656, 46225, 45796, 45369, 44944, 44521, 44100, 43681, 43264,
  1122. 42849, 42436, 42025, 41616, 41209, 40804, 40401, 40000, 39601, 39204, 38809, 38416, 38025, 37636, 37249, 36864,
  1123. 36481, 36100, 35721, 35344, 34969, 34596, 34225, 33856, 33489, 33124, 32761, 32400, 32041, 31684, 31329, 30976,
  1124. 30625, 30276, 29929, 29584, 29241, 28900, 28561, 28224, 27889, 27556, 27225, 26896, 26569, 26244, 25921, 25600,
  1125. 25281, 24964, 24649, 24336, 24025, 23716, 23409, 23104, 22801, 22500, 22201, 21904, 21609, 21316, 21025, 20736,
  1126. 20449, 20164, 19881, 19600, 19321, 19044, 18769, 18496, 18225, 17956, 17689, 17424, 17161, 16900, 16641, 16384,
  1127. 16129, 15876, 15625, 15376, 15129, 14884, 14641, 14400, 14161, 13924, 13689, 13456, 13225, 12996, 12769, 12544,
  1128. 12321, 12100, 11881, 11664, 11449, 11236, 11025, 10816, 10609, 10404, 10201, 10000, 9801, 9604, 9409, 9216,
  1129. 9025, 8836, 8649, 8464, 8281, 8100, 7921, 7744, 7569, 7396, 7225, 7056, 6889, 6724, 6561, 6400,
  1130. 6241, 6084, 5929, 5776, 5625, 5476, 5329, 5184, 5041, 4900, 4761, 4624, 4489, 4356, 4225, 4096,
  1131. 3969, 3844, 3721, 3600, 3481, 3364, 3249, 3136, 3025, 2916, 2809, 2704, 2601, 2500, 2401, 2304,
  1132. 2209, 2116, 2025, 1936, 1849, 1764, 1681, 1600, 1521, 1444, 1369, 1296, 1225, 1156, 1089, 1024,
  1133. 961, 900, 841, 784, 729, 676, 625, 576, 529, 484, 441, 400, 361, 324, 289, 256,
  1134. 225, 196, 169, 144, 121, 100, 81, 64, 49, 36, 25, 16, 9, 4, 1,
  1135. 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225,
  1136. 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961,
  1137. 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209,
  1138. 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969,
  1139. 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241,
  1140. 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025,
  1141. 9216, 9409, 9604, 9801, 10000, 10201, 10404, 10609, 10816, 11025, 11236, 11449, 11664, 11881, 12100, 12321,
  1142. 12544, 12769, 12996, 13225, 13456, 13689, 13924, 14161, 14400, 14641, 14884, 15129, 15376, 15625, 15876, 16129,
  1143. 16384, 16641, 16900, 17161, 17424, 17689, 17956, 18225, 18496, 18769, 19044, 19321, 19600, 19881, 20164, 20449,
  1144. 20736, 21025, 21316, 21609, 21904, 22201, 22500, 22801, 23104, 23409, 23716, 24025, 24336, 24649, 24964, 25281,
  1145. 25600, 25921, 26244, 26569, 26896, 27225, 27556, 27889, 28224, 28561, 28900, 29241, 29584, 29929, 30276, 30625,
  1146. 30976, 31329, 31684, 32041, 32400, 32761, 33124, 33489, 33856, 34225, 34596, 34969, 35344, 35721, 36100, 36481,
  1147. 36864, 37249, 37636, 38025, 38416, 38809, 39204, 39601, 40000, 40401, 40804, 41209, 41616, 42025, 42436, 42849,
  1148. 43264, 43681, 44100, 44521, 44944, 45369, 45796, 46225, 46656, 47089, 47524, 47961, 48400, 48841, 49284, 49729,
  1149. 50176, 50625, 51076, 51529, 51984, 52441, 52900, 53361, 53824, 54289, 54756, 55225, 55696, 56169, 56644, 57121,
  1150. 57600, 58081, 58564, 59049, 59536, 60025, 60516, 61009, 61504, 62001, 62500, 63001, 63504, 64009, 64516, 65025};
  1151. // Abbreviated variable names to make below tables smaller in source code size
  1152. #define KR PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000
  1153. #define KG PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000
  1154. #define KB PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000
  1155. // Table for fast implementation of squaring for numbers in the interval [-255, 255] multiplied by the perceptual weight for red.
  1156. const unsigned int square_table_percep_red[511] = {
  1157. 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,
  1158. 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,
  1159. 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,
  1160. 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,
  1161. 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,
  1162. 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,
  1163. 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,
  1164. 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,
  1165. 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,
  1166. 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,
  1167. 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,
  1168. 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,
  1169. 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,
  1170. 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,
  1171. 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,
  1172. 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,
  1173. 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,
  1174. 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,
  1175. 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,
  1176. 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,
  1177. 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,
  1178. 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,
  1179. 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,
  1180. 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,
  1181. 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,
  1182. 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,
  1183. 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,
  1184. 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,
  1185. 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,
  1186. 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,
  1187. 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,
  1188. 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};
  1189. // Table for fast implementation of squaring for numbers in the interval [-255, 255] multiplied by the perceptual weight for green.
  1190. const unsigned int square_table_percep_green[511] = {
  1191. 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,
  1192. 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,
  1193. 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,
  1194. 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,
  1195. 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,
  1196. 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,
  1197. 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,
  1198. 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,
  1199. 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,
  1200. 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,
  1201. 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,
  1202. 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,
  1203. 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,
  1204. 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,
  1205. 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,
  1206. 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,
  1207. 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,
  1208. 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,
  1209. 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,
  1210. 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,
  1211. 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,
  1212. 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,
  1213. 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,
  1214. 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,
  1215. 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,
  1216. 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,
  1217. 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,
  1218. 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,
  1219. 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,
  1220. 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,
  1221. 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,
  1222. 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};
  1223. // Table for fast implementation of squaring for numbers in the interval [-255, 255] multiplied by the perceptual weight for blue.
  1224. const unsigned int square_table_percep_blue[511] = {
  1225. 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,
  1226. 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,
  1227. 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,
  1228. 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,
  1229. 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,
  1230. 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,
  1231. 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,
  1232. 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,
  1233. 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,
  1234. 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,
  1235. 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,
  1236. 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,
  1237. 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,
  1238. 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,
  1239. 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,
  1240. 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,
  1241. 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,
  1242. 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,
  1243. 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,
  1244. 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,
  1245. 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,
  1246. 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,
  1247. 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,
  1248. 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,
  1249. 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,
  1250. 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,
  1251. 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,
  1252. 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,
  1253. 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,
  1254. 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,
  1255. 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,
  1256. 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};
  1257. // Find the best table to use for a 2x4 area by testing all.
  1258. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1259. 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)
  1260. {
  1261. int min_error = 3*255*255*16;
  1262. int q;
  1263. int err;
  1264. unsigned int pixel_indices_MSB, pixel_indices_LSB;
  1265. for(q=0;q<16;q+=2) // try all the 8 tables.
  1266. {
  1267. err=compressBlockWithTable2x4(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
  1268. if(err<min_error)
  1269. {
  1270. min_error=err;
  1271. best_pixel_indices_MSB = pixel_indices_MSB;
  1272. best_pixel_indices_LSB = pixel_indices_LSB;
  1273. best_table=q >> 1;
  1274. }
  1275. }
  1276. return min_error;
  1277. }
  1278. // Find the best table to use for a 2x4 area by testing all.
  1279. // Uses perceptual weighting.
  1280. // Uses fixed point implementation where 1000 equals 1.0
  1281. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1282. 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)
  1283. {
  1284. unsigned int min_error = MAXERR1000;
  1285. int q;
  1286. unsigned int err;
  1287. unsigned int pixel_indices_MSB, pixel_indices_LSB;
  1288. for(q=0;q<16;q+=2) // try all the 8 tables.
  1289. {
  1290. err=compressBlockWithTable2x4percep1000(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
  1291. if(err<min_error)
  1292. {
  1293. min_error=err;
  1294. best_pixel_indices_MSB = pixel_indices_MSB;
  1295. best_pixel_indices_LSB = pixel_indices_LSB;
  1296. best_table=q >> 1;
  1297. }
  1298. }
  1299. return min_error;
  1300. }
  1301. // Find the best table to use for a 2x4 area by testing all.
  1302. // Uses perceptual weighting.
  1303. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1304. 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)
  1305. {
  1306. float min_error = 3*255*255*16;
  1307. int q;
  1308. float err;
  1309. unsigned int pixel_indices_MSB, pixel_indices_LSB;
  1310. for(q=0;q<16;q+=2) // try all the 8 tables.
  1311. {
  1312. err=compressBlockWithTable2x4percep(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
  1313. if(err<min_error)
  1314. {
  1315. min_error=err;
  1316. best_pixel_indices_MSB = pixel_indices_MSB;
  1317. best_pixel_indices_LSB = pixel_indices_LSB;
  1318. best_table=q >> 1;
  1319. }
  1320. }
  1321. return (int) min_error;
  1322. }
  1323. // Find the best table to use for a 4x2 area by testing all.
  1324. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1325. 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)
  1326. {
  1327. int min_error = 3*255*255*16;
  1328. int q;
  1329. int err;
  1330. unsigned int pixel_indices_MSB, pixel_indices_LSB;
  1331. for(q=0;q<16;q+=2) // try all the 8 tables.
  1332. {
  1333. err=compressBlockWithTable4x2(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
  1334. if(err<min_error)
  1335. {
  1336. min_error=err;
  1337. best_pixel_indices_MSB = pixel_indices_MSB;
  1338. best_pixel_indices_LSB = pixel_indices_LSB;
  1339. best_table=q >> 1;
  1340. }
  1341. }
  1342. return min_error;
  1343. }
  1344. // Find the best table to use for a 4x2 area by testing all.
  1345. // Uses perceptual weighting.
  1346. // Uses fixed point implementation where 1000 equals 1.0
  1347. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1348. 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)
  1349. {
  1350. unsigned int min_error = MAXERR1000;
  1351. int q;
  1352. unsigned int err;
  1353. unsigned int pixel_indices_MSB, pixel_indices_LSB;
  1354. for(q=0;q<16;q+=2) // try all the 8 tables.
  1355. {
  1356. err=compressBlockWithTable4x2percep1000(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
  1357. if(err<min_error)
  1358. {
  1359. min_error=err;
  1360. best_pixel_indices_MSB = pixel_indices_MSB;
  1361. best_pixel_indices_LSB = pixel_indices_LSB;
  1362. best_table=q >> 1;
  1363. }
  1364. }
  1365. return min_error;
  1366. }
  1367. // Find the best table to use for a 4x2 area by testing all.
  1368. // Uses perceptual weighting.
  1369. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1370. 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)
  1371. {
  1372. float min_error = 3*255*255*16;
  1373. int q;
  1374. float err;
  1375. unsigned int pixel_indices_MSB, pixel_indices_LSB;
  1376. for(q=0;q<16;q+=2) // try all the 8 tables.
  1377. {
  1378. err=compressBlockWithTable4x2percep(img,width,height,startx,starty,avg_color,q,&pixel_indices_MSB, &pixel_indices_LSB);
  1379. if(err<min_error)
  1380. {
  1381. min_error=err;
  1382. best_pixel_indices_MSB = pixel_indices_MSB;
  1383. best_pixel_indices_LSB = pixel_indices_LSB;
  1384. best_table=q >> 1;
  1385. }
  1386. }
  1387. return (int) min_error;
  1388. }
  1389. // The below code quantizes a float RGB value to RGB444.
  1390. //
  1391. // The format often allows a pixel to completely compensate an intensity error of the base
  1392. // color. Hence the closest RGB444 point may not be the best, and the code below uses
  1393. // this fact to find a better RGB444 color as the base color.
  1394. //
  1395. // (See the presentation http://www.jacobstrom.com/publications/PACKMAN.ppt for more info.)
  1396. //
  1397. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1398. void quantize444ColorCombined(float *avg_col_in, int *enc_color, uint8 *avg_color)
  1399. {
  1400. float dr, dg, db;
  1401. float kr, kg, kb;
  1402. float wR2, wG2, wB2;
  1403. uint8 low_color[3];
  1404. uint8 high_color[3];
  1405. float min_error=255*255*8*3;
  1406. float lowhightable[8];
  1407. unsigned int best_table=0;
  1408. unsigned int best_index=0;
  1409. int q;
  1410. float kval = (float) (255.0/15.0);
  1411. // These are the values that we want to have:
  1412. float red_average, green_average, blue_average;
  1413. int red_4bit_low, green_4bit_low, blue_4bit_low;
  1414. int red_4bit_high, green_4bit_high, blue_4bit_high;
  1415. // These are the values that we approximate with:
  1416. int red_low, green_low, blue_low;
  1417. int red_high, green_high, blue_high;
  1418. red_average = avg_col_in[0];
  1419. green_average = avg_col_in[1];
  1420. blue_average = avg_col_in[2];
  1421. // Find the 5-bit reconstruction levels red_low, red_high
  1422. // so that red_average is in interval [red_low, red_high].
  1423. // (The same with green and blue.)
  1424. red_4bit_low = (int) (red_average/kval);
  1425. green_4bit_low = (int) (green_average/kval);
  1426. blue_4bit_low = (int) (blue_average/kval);
  1427. red_4bit_high = CLAMP(0, red_4bit_low + 1, 15);
  1428. green_4bit_high = CLAMP(0, green_4bit_low + 1, 15);
  1429. blue_4bit_high = CLAMP(0, blue_4bit_low + 1, 15);
  1430. red_low = (red_4bit_low << 4) | (red_4bit_low >> 0);
  1431. green_low = (green_4bit_low << 4) | (green_4bit_low >> 0);
  1432. blue_low = (blue_4bit_low << 4) | (blue_4bit_low >> 0);
  1433. red_high = (red_4bit_high << 4) | (red_4bit_high >> 0);
  1434. green_high = (green_4bit_high << 4) | (green_4bit_high >> 0);
  1435. blue_high = (blue_4bit_high << 4) | (blue_4bit_high >> 0);
  1436. kr = (float)red_high - (float)red_low;
  1437. kg = (float)green_high - (float)green_low;
  1438. kb = (float)blue_high - (float)blue_low;
  1439. // Note that dr, dg, and db are all negative.
  1440. dr = red_low - red_average;
  1441. dg = green_low - green_average;
  1442. db = blue_low - blue_average;
  1443. // Use straight (nonperceptive) weights.
  1444. wR2 = (float) 1.0;
  1445. wG2 = (float) 1.0;
  1446. wB2 = (float) 1.0;
  1447. lowhightable[0] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
  1448. lowhightable[1] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
  1449. lowhightable[2] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
  1450. lowhightable[3] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
  1451. lowhightable[4] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
  1452. lowhightable[5] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
  1453. lowhightable[6] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
  1454. lowhightable[7] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
  1455. float min_value = lowhightable[0];
  1456. int min_index = 0;
  1457. for(q = 1; q<8; q++)
  1458. {
  1459. if(lowhightable[q] < min_value)
  1460. {
  1461. min_value = lowhightable[q];
  1462. min_index = q;
  1463. }
  1464. }
  1465. float drh = red_high-red_average;
  1466. float dgh = green_high-green_average;
  1467. float dbh = blue_high-blue_average;
  1468. low_color[0] = red_4bit_low;
  1469. low_color[1] = green_4bit_low;
  1470. low_color[2] = blue_4bit_low;
  1471. high_color[0] = red_4bit_high;
  1472. high_color[1] = green_4bit_high;
  1473. high_color[2] = blue_4bit_high;
  1474. switch(min_index)
  1475. {
  1476. case 0:
  1477. // Since the step size is always 17 in RGB444 format (15*17=255),
  1478. // kr = kg = kb = 17, which means that case 0 and case 7 will
  1479. // always have equal projected error. Choose the one that is
  1480. // closer to the desired color.
  1481. if(dr*dr + dg*dg + db*db > 3*8*8)
  1482. {
  1483. enc_color[0] = high_color[0];
  1484. enc_color[1] = high_color[1];
  1485. enc_color[2] = high_color[2];
  1486. }
  1487. else
  1488. {
  1489. enc_color[0] = low_color[0];
  1490. enc_color[1] = low_color[1];
  1491. enc_color[2] = low_color[2];
  1492. }
  1493. break;
  1494. case 1:
  1495. enc_color[0] = high_color[0];
  1496. enc_color[1] = low_color[1];
  1497. enc_color[2] = low_color[2];
  1498. break;
  1499. case 2:
  1500. enc_color[0] = low_color[0];
  1501. enc_color[1] = high_color[1];
  1502. enc_color[2] = low_color[2];
  1503. break;
  1504. case 3:
  1505. enc_color[0] = low_color[0];
  1506. enc_color[1] = low_color[1];
  1507. enc_color[2] = high_color[2];
  1508. break;
  1509. case 4:
  1510. enc_color[0] = high_color[0];
  1511. enc_color[1] = high_color[1];
  1512. enc_color[2] = low_color[2];
  1513. break;
  1514. case 5:
  1515. enc_color[0] = high_color[0];
  1516. enc_color[1] = low_color[1];
  1517. enc_color[2] = high_color[2];
  1518. break;
  1519. case 6:
  1520. enc_color[0] = low_color[0];
  1521. enc_color[1] = high_color[1];
  1522. enc_color[2] = high_color[2];
  1523. break;
  1524. case 7:
  1525. if(dr*dr + dg*dg + db*db > 3*8*8)
  1526. {
  1527. enc_color[0] = high_color[0];
  1528. enc_color[1] = high_color[1];
  1529. enc_color[2] = high_color[2];
  1530. }
  1531. else
  1532. {
  1533. enc_color[0] = low_color[0];
  1534. enc_color[1] = low_color[1];
  1535. enc_color[2] = low_color[2];
  1536. }
  1537. break;
  1538. }
  1539. // Expand 5-bit encoded color to 8-bit color
  1540. avg_color[0] = (enc_color[0] << 3) | (enc_color[0] >> 2);
  1541. avg_color[1] = (enc_color[1] << 3) | (enc_color[1] >> 2);
  1542. avg_color[2] = (enc_color[2] << 3) | (enc_color[2] >> 2);
  1543. }
  1544. // The below code quantizes a float RGB value to RGB555.
  1545. //
  1546. // The format often allows a pixel to completely compensate an intensity error of the base
  1547. // color. Hence the closest RGB555 point may not be the best, and the code below uses
  1548. // this fact to find a better RGB555 color as the base color.
  1549. //
  1550. // (See the presentation http://www.jacobstrom.com/publications/PACKMAN.ppt for more info.)
  1551. //
  1552. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1553. void quantize555ColorCombined(float *avg_col_in, int *enc_color, uint8 *avg_color)
  1554. {
  1555. float dr, dg, db;
  1556. float kr, kg, kb;
  1557. float wR2, wG2, wB2;
  1558. uint8 low_color[3];
  1559. uint8 high_color[3];
  1560. float min_error=255*255*8*3;
  1561. float lowhightable[8];
  1562. unsigned int best_table=0;
  1563. unsigned int best_index=0;
  1564. int q;
  1565. float kval = (float) (255.0/31.0);
  1566. // These are the values that we want to have:
  1567. float red_average, green_average, blue_average;
  1568. int red_5bit_low, green_5bit_low, blue_5bit_low;
  1569. int red_5bit_high, green_5bit_high, blue_5bit_high;
  1570. // These are the values that we approximate with:
  1571. int red_low, green_low, blue_low;
  1572. int red_high, green_high, blue_high;
  1573. red_average = avg_col_in[0];
  1574. green_average = avg_col_in[1];
  1575. blue_average = avg_col_in[2];
  1576. // Find the 5-bit reconstruction levels red_low, red_high
  1577. // so that red_average is in interval [red_low, red_high].
  1578. // (The same with green and blue.)
  1579. red_5bit_low = (int) (red_average/kval);
  1580. green_5bit_low = (int) (green_average/kval);
  1581. blue_5bit_low = (int) (blue_average/kval);
  1582. red_5bit_high = CLAMP(0, red_5bit_low + 1, 31);
  1583. green_5bit_high = CLAMP(0, green_5bit_low + 1, 31);
  1584. blue_5bit_high = CLAMP(0, blue_5bit_low + 1, 31);
  1585. red_low = (red_5bit_low << 3) | (red_5bit_low >> 2);
  1586. green_low = (green_5bit_low << 3) | (green_5bit_low >> 2);
  1587. blue_low = (blue_5bit_low << 3) | (blue_5bit_low >> 2);
  1588. red_high = (red_5bit_high << 3) | (red_5bit_high >> 2);
  1589. green_high = (green_5bit_high << 3) | (green_5bit_high >> 2);
  1590. blue_high = (blue_5bit_high << 3) | (blue_5bit_high >> 2);
  1591. kr = (float)red_high - (float)red_low;
  1592. kg = (float)green_high - (float)green_low;
  1593. kb = (float)blue_high - (float)blue_low;
  1594. // Note that dr, dg, and db are all negative.
  1595. dr = red_low - red_average;
  1596. dg = green_low - green_average;
  1597. db = blue_low - blue_average;
  1598. // Use straight (nonperceptive) weights.
  1599. wR2 = (float) 1.0;
  1600. wG2 = (float) 1.0;
  1601. wB2 = (float) 1.0;
  1602. lowhightable[0] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
  1603. lowhightable[1] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
  1604. lowhightable[2] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
  1605. lowhightable[3] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
  1606. lowhightable[4] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
  1607. lowhightable[5] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
  1608. lowhightable[6] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
  1609. lowhightable[7] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
  1610. float min_value = lowhightable[0];
  1611. int min_index = 0;
  1612. for(q = 1; q<8; q++)
  1613. {
  1614. if(lowhightable[q] < min_value)
  1615. {
  1616. min_value = lowhightable[q];
  1617. min_index = q;
  1618. }
  1619. }
  1620. float drh = red_high-red_average;
  1621. float dgh = green_high-green_average;
  1622. float dbh = blue_high-blue_average;
  1623. low_color[0] = red_5bit_low;
  1624. low_color[1] = green_5bit_low;
  1625. low_color[2] = blue_5bit_low;
  1626. high_color[0] = red_5bit_high;
  1627. high_color[1] = green_5bit_high;
  1628. high_color[2] = blue_5bit_high;
  1629. switch(min_index)
  1630. {
  1631. case 0:
  1632. enc_color[0] = low_color[0];
  1633. enc_color[1] = low_color[1];
  1634. enc_color[2] = low_color[2];
  1635. break;
  1636. case 1:
  1637. enc_color[0] = high_color[0];
  1638. enc_color[1] = low_color[1];
  1639. enc_color[2] = low_color[2];
  1640. break;
  1641. case 2:
  1642. enc_color[0] = low_color[0];
  1643. enc_color[1] = high_color[1];
  1644. enc_color[2] = low_color[2];
  1645. break;
  1646. case 3:
  1647. enc_color[0] = low_color[0];
  1648. enc_color[1] = low_color[1];
  1649. enc_color[2] = high_color[2];
  1650. break;
  1651. case 4:
  1652. enc_color[0] = high_color[0];
  1653. enc_color[1] = high_color[1];
  1654. enc_color[2] = low_color[2];
  1655. break;
  1656. case 5:
  1657. enc_color[0] = high_color[0];
  1658. enc_color[1] = low_color[1];
  1659. enc_color[2] = high_color[2];
  1660. break;
  1661. case 6:
  1662. enc_color[0] = low_color[0];
  1663. enc_color[1] = high_color[1];
  1664. enc_color[2] = high_color[2];
  1665. break;
  1666. case 7:
  1667. enc_color[0] = high_color[0];
  1668. enc_color[1] = high_color[1];
  1669. enc_color[2] = high_color[2];
  1670. break;
  1671. }
  1672. // Expand 5-bit encoded color to 8-bit color
  1673. avg_color[0] = (enc_color[0] << 3) | (enc_color[0] >> 2);
  1674. avg_color[1] = (enc_color[1] << 3) | (enc_color[1] >> 2);
  1675. avg_color[2] = (enc_color[2] << 3) | (enc_color[2] >> 2);
  1676. }
  1677. // The below code quantizes a float RGB value to RGB444.
  1678. //
  1679. // The format often allows a pixel to completely compensate an intensity error of the base
  1680. // color. Hence the closest RGB444 point may not be the best, and the code below uses
  1681. // this fact to find a better RGB444 color as the base color.
  1682. //
  1683. // (See the presentation http://www.jacobstrom.com/publications/PACKMAN.ppt for more info.)
  1684. //
  1685. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1686. void quantize444ColorCombinedPerceptual(float *avg_col_in, int *enc_color, uint8 *avg_color)
  1687. {
  1688. float dr, dg, db;
  1689. float kr, kg, kb;
  1690. float wR2, wG2, wB2;
  1691. uint8 low_color[3];
  1692. uint8 high_color[3];
  1693. float min_error=255*255*8*3;
  1694. float lowhightable[8];
  1695. unsigned int best_table=0;
  1696. unsigned int best_index=0;
  1697. int q;
  1698. float kval = (float) (255.0/15.0);
  1699. // These are the values that we want to have:
  1700. float red_average, green_average, blue_average;
  1701. int red_4bit_low, green_4bit_low, blue_4bit_low;
  1702. int red_4bit_high, green_4bit_high, blue_4bit_high;
  1703. // These are the values that we approximate with:
  1704. int red_low, green_low, blue_low;
  1705. int red_high, green_high, blue_high;
  1706. red_average = avg_col_in[0];
  1707. green_average = avg_col_in[1];
  1708. blue_average = avg_col_in[2];
  1709. // Find the 5-bit reconstruction levels red_low, red_high
  1710. // so that red_average is in interval [red_low, red_high].
  1711. // (The same with green and blue.)
  1712. red_4bit_low = (int) (red_average/kval);
  1713. green_4bit_low = (int) (green_average/kval);
  1714. blue_4bit_low = (int) (blue_average/kval);
  1715. red_4bit_high = CLAMP(0, red_4bit_low + 1, 15);
  1716. green_4bit_high = CLAMP(0, green_4bit_low + 1, 15);
  1717. blue_4bit_high = CLAMP(0, blue_4bit_low + 1, 15);
  1718. red_low = (red_4bit_low << 4) | (red_4bit_low >> 0);
  1719. green_low = (green_4bit_low << 4) | (green_4bit_low >> 0);
  1720. blue_low = (blue_4bit_low << 4) | (blue_4bit_low >> 0);
  1721. red_high = (red_4bit_high << 4) | (red_4bit_high >> 0);
  1722. green_high = (green_4bit_high << 4) | (green_4bit_high >> 0);
  1723. blue_high = (blue_4bit_high << 4) | (blue_4bit_high >> 0);
  1724. low_color[0] = red_4bit_low;
  1725. low_color[1] = green_4bit_low;
  1726. low_color[2] = blue_4bit_low;
  1727. high_color[0] = red_4bit_high;
  1728. high_color[1] = green_4bit_high;
  1729. high_color[2] = blue_4bit_high;
  1730. kr = (float)red_high - (float)red_low;
  1731. kg = (float)green_high - (float)green_low;
  1732. kb = (float)blue_high- (float)blue_low;
  1733. // Note that dr, dg, and db are all negative.
  1734. dr = red_low - red_average;
  1735. dg = green_low - green_average;
  1736. db = blue_low - blue_average;
  1737. // Perceptual weights to use
  1738. wR2 = (float) PERCEPTUAL_WEIGHT_R_SQUARED;
  1739. wG2 = (float) PERCEPTUAL_WEIGHT_G_SQUARED;
  1740. wB2 = (float) PERCEPTUAL_WEIGHT_B_SQUARED;
  1741. lowhightable[0] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
  1742. lowhightable[1] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
  1743. lowhightable[2] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
  1744. lowhightable[3] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
  1745. lowhightable[4] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
  1746. lowhightable[5] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
  1747. lowhightable[6] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
  1748. lowhightable[7] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
  1749. float min_value = lowhightable[0];
  1750. int min_index = 0;
  1751. for(q = 1; q<8; q++)
  1752. {
  1753. if(lowhightable[q] < min_value)
  1754. {
  1755. min_value = lowhightable[q];
  1756. min_index = q;
  1757. }
  1758. }
  1759. float drh = red_high-red_average;
  1760. float dgh = green_high-green_average;
  1761. float dbh = blue_high-blue_average;
  1762. switch(min_index)
  1763. {
  1764. case 0:
  1765. enc_color[0] = low_color[0];
  1766. enc_color[1] = low_color[1];
  1767. enc_color[2] = low_color[2];
  1768. break;
  1769. case 1:
  1770. enc_color[0] = high_color[0];
  1771. enc_color[1] = low_color[1];
  1772. enc_color[2] = low_color[2];
  1773. break;
  1774. case 2:
  1775. enc_color[0] = low_color[0];
  1776. enc_color[1] = high_color[1];
  1777. enc_color[2] = low_color[2];
  1778. break;
  1779. case 3:
  1780. enc_color[0] = low_color[0];
  1781. enc_color[1] = low_color[1];
  1782. enc_color[2] = high_color[2];
  1783. break;
  1784. case 4:
  1785. enc_color[0] = high_color[0];
  1786. enc_color[1] = high_color[1];
  1787. enc_color[2] = low_color[2];
  1788. break;
  1789. case 5:
  1790. enc_color[0] = high_color[0];
  1791. enc_color[1] = low_color[1];
  1792. enc_color[2] = high_color[2];
  1793. break;
  1794. case 6:
  1795. enc_color[0] = low_color[0];
  1796. enc_color[1] = high_color[1];
  1797. enc_color[2] = high_color[2];
  1798. break;
  1799. case 7:
  1800. enc_color[0] = high_color[0];
  1801. enc_color[1] = high_color[1];
  1802. enc_color[2] = high_color[2];
  1803. break;
  1804. }
  1805. // Expand encoded color to eight bits
  1806. avg_color[0] = (enc_color[0] << 4) | enc_color[0];
  1807. avg_color[1] = (enc_color[1] << 4) | enc_color[1];
  1808. avg_color[2] = (enc_color[2] << 4) | enc_color[2];
  1809. }
  1810. // The below code quantizes a float RGB value to RGB555.
  1811. //
  1812. // The format often allows a pixel to completely compensate an intensity error of the base
  1813. // color. Hence the closest RGB555 point may not be the best, and the code below uses
  1814. // this fact to find a better RGB555 color as the base color.
  1815. //
  1816. // (See the presentation http://www.jacobstrom.com/publications/PACKMAN.ppt for more info.)
  1817. //
  1818. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1819. void quantize555ColorCombinedPerceptual(float *avg_col_in, int *enc_color, uint8 *avg_color)
  1820. {
  1821. float dr, dg, db;
  1822. float kr, kg, kb;
  1823. float wR2, wG2, wB2;
  1824. uint8 low_color[3];
  1825. uint8 high_color[3];
  1826. float min_error=255*255*8*3;
  1827. float lowhightable[8];
  1828. unsigned int best_table=0;
  1829. unsigned int best_index=0;
  1830. int q;
  1831. float kval = (float) (255.0/31.0);
  1832. // These are the values that we want to have:
  1833. float red_average, green_average, blue_average;
  1834. int red_5bit_low, green_5bit_low, blue_5bit_low;
  1835. int red_5bit_high, green_5bit_high, blue_5bit_high;
  1836. // These are the values that we approximate with:
  1837. int red_low, green_low, blue_low;
  1838. int red_high, green_high, blue_high;
  1839. red_average = avg_col_in[0];
  1840. green_average = avg_col_in[1];
  1841. blue_average = avg_col_in[2];
  1842. // Find the 5-bit reconstruction levels red_low, red_high
  1843. // so that red_average is in interval [red_low, red_high].
  1844. // (The same with green and blue.)
  1845. red_5bit_low = (int) (red_average/kval);
  1846. green_5bit_low = (int) (green_average/kval);
  1847. blue_5bit_low = (int) (blue_average/kval);
  1848. red_5bit_high = CLAMP(0, red_5bit_low + 1, 31);
  1849. green_5bit_high = CLAMP(0, green_5bit_low + 1, 31);
  1850. blue_5bit_high = CLAMP(0, blue_5bit_low + 1, 31);
  1851. red_low = (red_5bit_low << 3) | (red_5bit_low >> 2);
  1852. green_low = (green_5bit_low << 3) | (green_5bit_low >> 2);
  1853. blue_low = (blue_5bit_low << 3) | (blue_5bit_low >> 2);
  1854. red_high = (red_5bit_high << 3) | (red_5bit_high >> 2);
  1855. green_high = (green_5bit_high << 3) | (green_5bit_high >> 2);
  1856. blue_high = (blue_5bit_high << 3) | (blue_5bit_high >> 2);
  1857. low_color[0] = red_5bit_low;
  1858. low_color[1] = green_5bit_low;
  1859. low_color[2] = blue_5bit_low;
  1860. high_color[0] = red_5bit_high;
  1861. high_color[1] = green_5bit_high;
  1862. high_color[2] = blue_5bit_high;
  1863. kr = (float)red_high - (float)red_low;
  1864. kg = (float)green_high - (float)green_low;
  1865. kb = (float)blue_high - (float)blue_low;
  1866. // Note that dr, dg, and db are all negative.
  1867. dr = red_low - red_average;
  1868. dg = green_low - green_average;
  1869. db = blue_low - blue_average;
  1870. // Perceptual weights to use
  1871. wR2 = (float) PERCEPTUAL_WEIGHT_R_SQUARED;
  1872. wG2 = (float) PERCEPTUAL_WEIGHT_G_SQUARED;
  1873. wB2 = (float) PERCEPTUAL_WEIGHT_B_SQUARED;
  1874. lowhightable[0] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
  1875. lowhightable[1] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+ 0) );
  1876. lowhightable[2] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
  1877. lowhightable[3] = wR2*wG2*SQUARE( (dr+ 0) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
  1878. lowhightable[4] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+ 0) ) + wG2*wB2*SQUARE( (dg+kg) - (db+ 0) );
  1879. lowhightable[5] = wR2*wG2*SQUARE( (dr+kr) - (dg+ 0) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+ 0) - (db+kb) );
  1880. lowhightable[6] = wR2*wG2*SQUARE( (dr+ 0) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+ 0) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
  1881. lowhightable[7] = wR2*wG2*SQUARE( (dr+kr) - (dg+kg) ) + wR2*wB2*SQUARE( (dr+kr) - (db+kb) ) + wG2*wB2*SQUARE( (dg+kg) - (db+kb) );
  1882. float min_value = lowhightable[0];
  1883. int min_index = 0;
  1884. for(q = 1; q<8; q++)
  1885. {
  1886. if(lowhightable[q] < min_value)
  1887. {
  1888. min_value = lowhightable[q];
  1889. min_index = q;
  1890. }
  1891. }
  1892. float drh = red_high-red_average;
  1893. float dgh = green_high-green_average;
  1894. float dbh = blue_high-blue_average;
  1895. switch(min_index)
  1896. {
  1897. case 0:
  1898. enc_color[0] = low_color[0];
  1899. enc_color[1] = low_color[1];
  1900. enc_color[2] = low_color[2];
  1901. break;
  1902. case 1:
  1903. enc_color[0] = high_color[0];
  1904. enc_color[1] = low_color[1];
  1905. enc_color[2] = low_color[2];
  1906. break;
  1907. case 2:
  1908. enc_color[0] = low_color[0];
  1909. enc_color[1] = high_color[1];
  1910. enc_color[2] = low_color[2];
  1911. break;
  1912. case 3:
  1913. enc_color[0] = low_color[0];
  1914. enc_color[1] = low_color[1];
  1915. enc_color[2] = high_color[2];
  1916. break;
  1917. case 4:
  1918. enc_color[0] = high_color[0];
  1919. enc_color[1] = high_color[1];
  1920. enc_color[2] = low_color[2];
  1921. break;
  1922. case 5:
  1923. enc_color[0] = high_color[0];
  1924. enc_color[1] = low_color[1];
  1925. enc_color[2] = high_color[2];
  1926. break;
  1927. case 6:
  1928. enc_color[0] = low_color[0];
  1929. enc_color[1] = high_color[1];
  1930. enc_color[2] = high_color[2];
  1931. break;
  1932. case 7:
  1933. enc_color[0] = high_color[0];
  1934. enc_color[1] = high_color[1];
  1935. enc_color[2] = high_color[2];
  1936. break;
  1937. }
  1938. // Expand 5-bit encoded color to 8-bit color
  1939. avg_color[0] = (enc_color[0] << 3) | (enc_color[0] >> 2);
  1940. avg_color[1] = (enc_color[1] << 3) | (enc_color[1] >> 2);
  1941. avg_color[2] = (enc_color[2] << 3) | (enc_color[2] >> 2);
  1942. }
  1943. // Compresses the block using only the individual mode in ETC1/ETC2 using the average color as the base color.
  1944. // Uses a perceptual error metric.
  1945. // Uses fixed point arithmetics where 1000 equals 1.0
  1946. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  1947. 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)
  1948. {
  1949. unsigned int compressed1_norm, compressed2_norm;
  1950. unsigned int compressed1_flip, compressed2_flip;
  1951. uint8 avg_color_quant1[3], avg_color_quant2[3];
  1952. float avg_color_float1[3],avg_color_float2[3];
  1953. int enc_color1[3], enc_color2[3];
  1954. unsigned int best_table_indices1=0, best_table_indices2=0;
  1955. unsigned int best_table1=0, best_table2=0;
  1956. int diffbit;
  1957. unsigned int norm_err=0;
  1958. unsigned int flip_err=0;
  1959. unsigned int best_err;
  1960. // First try normal blocks 2x4:
  1961. computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  1962. computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
  1963. enc_color1[0] = int( JAS_ROUND(15.0*avg_color_float1[0]/255.0) );
  1964. enc_color1[1] = int( JAS_ROUND(15.0*avg_color_float1[1]/255.0) );
  1965. enc_color1[2] = int( JAS_ROUND(15.0*avg_color_float1[2]/255.0) );
  1966. enc_color2[0] = int( JAS_ROUND(15.0*avg_color_float2[0]/255.0) );
  1967. enc_color2[1] = int( JAS_ROUND(15.0*avg_color_float2[1]/255.0) );
  1968. enc_color2[2] = int( JAS_ROUND(15.0*avg_color_float2[2]/255.0) );
  1969. diffbit = 0;
  1970. avg_color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0] );
  1971. avg_color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1] );
  1972. avg_color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2] );
  1973. avg_color_quant2[0] = enc_color2[0] << 4 | (enc_color2[0] );
  1974. avg_color_quant2[1] = enc_color2[1] << 4 | (enc_color2[1] );
  1975. avg_color_quant2[2] = enc_color2[2] << 4 | (enc_color2[2] );
  1976. // Pack bits into the first word.
  1977. // ETC1_RGB8_OES:
  1978. //
  1979. // a) bit layout in bits 63 through 32 if diffbit = 0
  1980. //
  1981. // 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
  1982. // ---------------------------------------------------------------------------------------------------
  1983. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  1984. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  1985. // ---------------------------------------------------------------------------------------------------
  1986. //
  1987. // b) bit layout in bits 63 through 32 if diffbit = 1
  1988. //
  1989. // 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
  1990. // ---------------------------------------------------------------------------------------------------
  1991. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  1992. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  1993. // ---------------------------------------------------------------------------------------------------
  1994. //
  1995. // c) bit layout in bits 31 through 0 (in both cases)
  1996. //
  1997. // 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
  1998. // --------------------------------------------------------------------------------------------------
  1999. // | most significant pixel index bits | least significant pixel index bits |
  2000. // | 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 |
  2001. // --------------------------------------------------------------------------------------------------
  2002. compressed1_norm = 0;
  2003. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  2004. PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
  2005. PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
  2006. PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
  2007. PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
  2008. PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
  2009. PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 43);
  2010. unsigned int best_pixel_indices1_MSB;
  2011. unsigned int best_pixel_indices1_LSB;
  2012. unsigned int best_pixel_indices2_MSB;
  2013. unsigned int best_pixel_indices2_LSB;
  2014. best_enc_color1[0] = enc_color1[0];
  2015. best_enc_color1[1] = enc_color1[1];
  2016. best_enc_color1[2] = enc_color1[2];
  2017. best_enc_color2[0] = enc_color2[0];
  2018. best_enc_color2[1] = enc_color2[1];
  2019. best_enc_color2[2] = enc_color2[2];
  2020. best_color_left[0] = enc_color1[0];
  2021. best_color_left[1] = enc_color1[1];
  2022. best_color_left[2] = enc_color1[2];
  2023. best_color_right[0] = enc_color2[0];
  2024. best_color_right[1] = enc_color2[1];
  2025. best_color_right[2] = enc_color2[2];
  2026. norm_err = 0;
  2027. // left part of block
  2028. best_err_left = tryalltables_3bittable2x4percep1000(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2029. norm_err = best_err_left;
  2030. // right part of block
  2031. best_err_right = tryalltables_3bittable2x4percep1000(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2032. norm_err += best_err_right;
  2033. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  2034. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  2035. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  2036. compressed2_norm = 0;
  2037. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  2038. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  2039. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  2040. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  2041. // Now try flipped blocks 4x2:
  2042. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  2043. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
  2044. // First test if avg_color1 is similar enough to avg_color2 so that
  2045. // we can use differential coding of colors.
  2046. enc_color1[0] = int( JAS_ROUND(15.0*avg_color_float1[0]/255.0) );
  2047. enc_color1[1] = int( JAS_ROUND(15.0*avg_color_float1[1]/255.0) );
  2048. enc_color1[2] = int( JAS_ROUND(15.0*avg_color_float1[2]/255.0) );
  2049. enc_color2[0] = int( JAS_ROUND(15.0*avg_color_float2[0]/255.0) );
  2050. enc_color2[1] = int( JAS_ROUND(15.0*avg_color_float2[1]/255.0) );
  2051. enc_color2[2] = int( JAS_ROUND(15.0*avg_color_float2[2]/255.0) );
  2052. best_color_upper[0] = enc_color1[0];
  2053. best_color_upper[1] = enc_color1[1];
  2054. best_color_upper[2] = enc_color1[2];
  2055. best_color_lower[0] = enc_color2[0];
  2056. best_color_lower[1] = enc_color2[1];
  2057. best_color_lower[2] = enc_color2[2];
  2058. diffbit = 0;
  2059. avg_color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0] );
  2060. avg_color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1] );
  2061. avg_color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2] );
  2062. avg_color_quant2[0] = enc_color2[0] << 4 | (enc_color2[0] );
  2063. avg_color_quant2[1] = enc_color2[1] << 4 | (enc_color2[1] );
  2064. avg_color_quant2[2] = enc_color2[2] << 4 | (enc_color2[2] );
  2065. // Pack bits into the first word.
  2066. compressed1_flip = 0;
  2067. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  2068. PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
  2069. PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
  2070. PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
  2071. PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 49);
  2072. PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
  2073. PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
  2074. // upper part of block
  2075. best_err_upper = tryalltables_3bittable4x2percep1000(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2076. flip_err = best_err_upper;
  2077. // lower part of block
  2078. best_err_lower = tryalltables_3bittable4x2percep1000(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2079. flip_err += best_err_lower;
  2080. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  2081. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  2082. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  2083. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  2084. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  2085. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  2086. // Now lets see which is the best table to use. Only 8 tables are possible.
  2087. if(norm_err <= flip_err)
  2088. {
  2089. compressed1 = compressed1_norm | 0;
  2090. compressed2 = compressed2_norm;
  2091. best_err = norm_err;
  2092. best_flip = 0;
  2093. }
  2094. else
  2095. {
  2096. compressed1 = compressed1_flip | 1;
  2097. compressed2 = compressed2_flip;
  2098. best_err = flip_err;
  2099. best_enc_color1[0] = enc_color1[0];
  2100. best_enc_color1[1] = enc_color1[1];
  2101. best_enc_color1[2] = enc_color1[2];
  2102. best_enc_color2[0] = enc_color2[0];
  2103. best_enc_color2[1] = enc_color2[1];
  2104. best_enc_color2[2] = enc_color2[2];
  2105. best_flip = 1;
  2106. }
  2107. return best_err;
  2108. }
  2109. // Compresses the block using only the individual mode in ETC1/ETC2 using the average color as the base color.
  2110. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  2111. 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)
  2112. {
  2113. unsigned int compressed1_norm, compressed2_norm;
  2114. unsigned int compressed1_flip, compressed2_flip;
  2115. uint8 avg_color_quant1[3], avg_color_quant2[3];
  2116. float avg_color_float1[3],avg_color_float2[3];
  2117. int enc_color1[3], enc_color2[3];
  2118. int min_error=255*255*8*3;
  2119. unsigned int best_table_indices1=0, best_table_indices2=0;
  2120. unsigned int best_table1=0, best_table2=0;
  2121. int diffbit;
  2122. int norm_err=0;
  2123. int flip_err=0;
  2124. int best_err;
  2125. // First try normal blocks 2x4:
  2126. computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  2127. computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
  2128. enc_color1[0] = int( JAS_ROUND(15.0*avg_color_float1[0]/255.0) );
  2129. enc_color1[1] = int( JAS_ROUND(15.0*avg_color_float1[1]/255.0) );
  2130. enc_color1[2] = int( JAS_ROUND(15.0*avg_color_float1[2]/255.0) );
  2131. enc_color2[0] = int( JAS_ROUND(15.0*avg_color_float2[0]/255.0) );
  2132. enc_color2[1] = int( JAS_ROUND(15.0*avg_color_float2[1]/255.0) );
  2133. enc_color2[2] = int( JAS_ROUND(15.0*avg_color_float2[2]/255.0) );
  2134. diffbit = 0;
  2135. avg_color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0] );
  2136. avg_color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1] );
  2137. avg_color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2] );
  2138. avg_color_quant2[0] = enc_color2[0] << 4 | (enc_color2[0] );
  2139. avg_color_quant2[1] = enc_color2[1] << 4 | (enc_color2[1] );
  2140. avg_color_quant2[2] = enc_color2[2] << 4 | (enc_color2[2] );
  2141. // Pack bits into the first word.
  2142. // ETC1_RGB8_OES:
  2143. //
  2144. // a) bit layout in bits 63 through 32 if diffbit = 0
  2145. //
  2146. // 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
  2147. // ---------------------------------------------------------------------------------------------------
  2148. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  2149. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  2150. // ---------------------------------------------------------------------------------------------------
  2151. //
  2152. // b) bit layout in bits 63 through 32 if diffbit = 1
  2153. //
  2154. // 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
  2155. // ---------------------------------------------------------------------------------------------------
  2156. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  2157. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  2158. // ---------------------------------------------------------------------------------------------------
  2159. //
  2160. // c) bit layout in bits 31 through 0 (in both cases)
  2161. //
  2162. // 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
  2163. // --------------------------------------------------------------------------------------------------
  2164. // | most significant pixel index bits | least significant pixel index bits |
  2165. // | 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 |
  2166. // --------------------------------------------------------------------------------------------------
  2167. compressed1_norm = 0;
  2168. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  2169. PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
  2170. PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
  2171. PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
  2172. PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
  2173. PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
  2174. PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 43);
  2175. unsigned int best_pixel_indices1_MSB;
  2176. unsigned int best_pixel_indices1_LSB;
  2177. unsigned int best_pixel_indices2_MSB;
  2178. unsigned int best_pixel_indices2_LSB;
  2179. best_enc_color1[0] = enc_color1[0];
  2180. best_enc_color1[1] = enc_color1[1];
  2181. best_enc_color1[2] = enc_color1[2];
  2182. best_enc_color2[0] = enc_color2[0];
  2183. best_enc_color2[1] = enc_color2[1];
  2184. best_enc_color2[2] = enc_color2[2];
  2185. best_color_left[0] = enc_color1[0];
  2186. best_color_left[1] = enc_color1[1];
  2187. best_color_left[2] = enc_color1[2];
  2188. best_color_right[0] = enc_color2[0];
  2189. best_color_right[1] = enc_color2[1];
  2190. best_color_right[2] = enc_color2[2];
  2191. norm_err = 0;
  2192. // left part of block
  2193. best_err_left = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2194. norm_err = best_err_left;
  2195. // right part of block
  2196. best_err_right = tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2197. norm_err += best_err_right;
  2198. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  2199. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  2200. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  2201. compressed2_norm = 0;
  2202. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  2203. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  2204. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  2205. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  2206. // Now try flipped blocks 4x2:
  2207. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  2208. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
  2209. // First test if avg_color1 is similar enough to avg_color2 so that
  2210. // we can use differential coding of colors.
  2211. enc_color1[0] = int( JAS_ROUND(15.0*avg_color_float1[0]/255.0) );
  2212. enc_color1[1] = int( JAS_ROUND(15.0*avg_color_float1[1]/255.0) );
  2213. enc_color1[2] = int( JAS_ROUND(15.0*avg_color_float1[2]/255.0) );
  2214. enc_color2[0] = int( JAS_ROUND(15.0*avg_color_float2[0]/255.0) );
  2215. enc_color2[1] = int( JAS_ROUND(15.0*avg_color_float2[1]/255.0) );
  2216. enc_color2[2] = int( JAS_ROUND(15.0*avg_color_float2[2]/255.0) );
  2217. best_color_upper[0] = enc_color1[0];
  2218. best_color_upper[1] = enc_color1[1];
  2219. best_color_upper[2] = enc_color1[2];
  2220. best_color_lower[0] = enc_color2[0];
  2221. best_color_lower[1] = enc_color2[1];
  2222. best_color_lower[2] = enc_color2[2];
  2223. diffbit = 0;
  2224. avg_color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0] );
  2225. avg_color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1] );
  2226. avg_color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2] );
  2227. avg_color_quant2[0] = enc_color2[0] << 4 | (enc_color2[0] );
  2228. avg_color_quant2[1] = enc_color2[1] << 4 | (enc_color2[1] );
  2229. avg_color_quant2[2] = enc_color2[2] << 4 | (enc_color2[2] );
  2230. // Pack bits into the first word.
  2231. compressed1_flip = 0;
  2232. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  2233. PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
  2234. PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
  2235. PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
  2236. PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 49);
  2237. PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
  2238. PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
  2239. // upper part of block
  2240. best_err_upper = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2241. flip_err = best_err_upper;
  2242. // lower part of block
  2243. best_err_lower = tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2244. flip_err += best_err_lower;
  2245. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  2246. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  2247. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  2248. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  2249. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  2250. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  2251. // Now lets see which is the best table to use. Only 8 tables are possible.
  2252. if(norm_err <= flip_err)
  2253. {
  2254. compressed1 = compressed1_norm | 0;
  2255. compressed2 = compressed2_norm;
  2256. best_err = norm_err;
  2257. best_flip = 0;
  2258. }
  2259. else
  2260. {
  2261. compressed1 = compressed1_flip | 1;
  2262. compressed2 = compressed2_flip;
  2263. best_err = flip_err;
  2264. best_enc_color1[0] = enc_color1[0];
  2265. best_enc_color1[1] = enc_color1[1];
  2266. best_enc_color1[2] = enc_color1[2];
  2267. best_enc_color2[0] = enc_color2[0];
  2268. best_enc_color2[1] = enc_color2[1];
  2269. best_enc_color2[2] = enc_color2[2];
  2270. best_flip = 1;
  2271. }
  2272. return best_err;
  2273. }
  2274. // Compresses the block using either the individual or differential mode in ETC1/ETC2
  2275. // Uses the average color as the base color in each half-block.
  2276. // Tries both flipped and unflipped.
  2277. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  2278. void compressBlockDiffFlipAverage(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  2279. {
  2280. unsigned int compressed1_norm, compressed2_norm;
  2281. unsigned int compressed1_flip, compressed2_flip;
  2282. uint8 avg_color_quant1[3], avg_color_quant2[3];
  2283. float avg_color_float1[3],avg_color_float2[3];
  2284. int enc_color1[3], enc_color2[3], diff[3];
  2285. int min_error=255*255*8*3;
  2286. unsigned int best_table_indices1=0, best_table_indices2=0;
  2287. unsigned int best_table1=0, best_table2=0;
  2288. int diffbit;
  2289. int norm_err=0;
  2290. int flip_err=0;
  2291. // First try normal blocks 2x4:
  2292. computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  2293. computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
  2294. // First test if avg_color1 is similar enough to avg_color2 so that
  2295. // we can use differential coding of colors.
  2296. float eps;
  2297. enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
  2298. enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
  2299. enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
  2300. enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
  2301. enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
  2302. enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
  2303. diff[0] = enc_color2[0]-enc_color1[0];
  2304. diff[1] = enc_color2[1]-enc_color1[1];
  2305. diff[2] = enc_color2[2]-enc_color1[2];
  2306. if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
  2307. {
  2308. diffbit = 1;
  2309. // The difference to be coded:
  2310. diff[0] = enc_color2[0]-enc_color1[0];
  2311. diff[1] = enc_color2[1]-enc_color1[1];
  2312. diff[2] = enc_color2[2]-enc_color1[2];
  2313. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  2314. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  2315. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  2316. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  2317. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  2318. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  2319. // Pack bits into the first word.
  2320. // ETC1_RGB8_OES:
  2321. //
  2322. // a) bit layout in bits 63 through 32 if diffbit = 0
  2323. //
  2324. // 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
  2325. // ---------------------------------------------------------------------------------------------------
  2326. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  2327. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  2328. // ---------------------------------------------------------------------------------------------------
  2329. //
  2330. // b) bit layout in bits 63 through 32 if diffbit = 1
  2331. //
  2332. // 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
  2333. // ---------------------------------------------------------------------------------------------------
  2334. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  2335. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  2336. // ---------------------------------------------------------------------------------------------------
  2337. //
  2338. // c) bit layout in bits 31 through 0 (in both cases)
  2339. //
  2340. // 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
  2341. // --------------------------------------------------------------------------------------------------
  2342. // | most significant pixel index bits | least significant pixel index bits |
  2343. // | 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 |
  2344. // --------------------------------------------------------------------------------------------------
  2345. compressed1_norm = 0;
  2346. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  2347. PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
  2348. PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
  2349. PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
  2350. PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
  2351. PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
  2352. PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
  2353. unsigned int best_pixel_indices1_MSB;
  2354. unsigned int best_pixel_indices1_LSB;
  2355. unsigned int best_pixel_indices2_MSB;
  2356. unsigned int best_pixel_indices2_LSB;
  2357. norm_err = 0;
  2358. // left part of block
  2359. norm_err = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2360. // right part of block
  2361. norm_err += tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2362. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  2363. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  2364. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  2365. compressed2_norm = 0;
  2366. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  2367. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  2368. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  2369. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  2370. }
  2371. else
  2372. {
  2373. diffbit = 0;
  2374. // The difference is bigger than what fits in 555 plus delta-333, so we will have
  2375. // to deal with 444 444.
  2376. eps = (float) 0.0001;
  2377. enc_color1[0] = int( ((float) avg_color_float1[0] / (17.0)) +0.5 + eps);
  2378. enc_color1[1] = int( ((float) avg_color_float1[1] / (17.0)) +0.5 + eps);
  2379. enc_color1[2] = int( ((float) avg_color_float1[2] / (17.0)) +0.5 + eps);
  2380. enc_color2[0] = int( ((float) avg_color_float2[0] / (17.0)) +0.5 + eps);
  2381. enc_color2[1] = int( ((float) avg_color_float2[1] / (17.0)) +0.5 + eps);
  2382. enc_color2[2] = int( ((float) avg_color_float2[2] / (17.0)) +0.5 + eps);
  2383. avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
  2384. avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
  2385. avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
  2386. avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
  2387. avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
  2388. avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
  2389. // Pack bits into the first word.
  2390. // 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
  2391. // ---------------------------------------------------------------------------------------------------
  2392. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  2393. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  2394. // ---------------------------------------------------------------------------------------------------
  2395. compressed1_norm = 0;
  2396. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  2397. PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
  2398. PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
  2399. PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
  2400. PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
  2401. PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
  2402. PUTBITSHIGH( compressed1_norm, enc_color2[2], 4, 43);
  2403. unsigned int best_pixel_indices1_MSB;
  2404. unsigned int best_pixel_indices1_LSB;
  2405. unsigned int best_pixel_indices2_MSB;
  2406. unsigned int best_pixel_indices2_LSB;
  2407. // left part of block
  2408. norm_err = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2409. // right part of block
  2410. norm_err += tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2411. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  2412. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  2413. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  2414. compressed2_norm = 0;
  2415. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  2416. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  2417. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  2418. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  2419. }
  2420. // Now try flipped blocks 4x2:
  2421. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  2422. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
  2423. // First test if avg_color1 is similar enough to avg_color2 so that
  2424. // we can use differential coding of colors.
  2425. enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
  2426. enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
  2427. enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
  2428. enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
  2429. enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
  2430. enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
  2431. diff[0] = enc_color2[0]-enc_color1[0];
  2432. diff[1] = enc_color2[1]-enc_color1[1];
  2433. diff[2] = enc_color2[2]-enc_color1[2];
  2434. if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
  2435. {
  2436. diffbit = 1;
  2437. // The difference to be coded:
  2438. diff[0] = enc_color2[0]-enc_color1[0];
  2439. diff[1] = enc_color2[1]-enc_color1[1];
  2440. diff[2] = enc_color2[2]-enc_color1[2];
  2441. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  2442. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  2443. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  2444. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  2445. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  2446. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  2447. // Pack bits into the first word.
  2448. compressed1_flip = 0;
  2449. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  2450. PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
  2451. PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
  2452. PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
  2453. PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
  2454. PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
  2455. PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
  2456. unsigned int best_pixel_indices1_MSB;
  2457. unsigned int best_pixel_indices1_LSB;
  2458. unsigned int best_pixel_indices2_MSB;
  2459. unsigned int best_pixel_indices2_LSB;
  2460. // upper part of block
  2461. flip_err = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2462. // lower part of block
  2463. flip_err += tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2464. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  2465. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  2466. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  2467. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  2468. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  2469. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  2470. }
  2471. else
  2472. {
  2473. diffbit = 0;
  2474. // The difference is bigger than what fits in 555 plus delta-333, so we will have
  2475. // to deal with 444 444.
  2476. eps = (float) 0.0001;
  2477. enc_color1[0] = int( ((float) avg_color_float1[0] / (17.0)) +0.5 + eps);
  2478. enc_color1[1] = int( ((float) avg_color_float1[1] / (17.0)) +0.5 + eps);
  2479. enc_color1[2] = int( ((float) avg_color_float1[2] / (17.0)) +0.5 + eps);
  2480. enc_color2[0] = int( ((float) avg_color_float2[0] / (17.0)) +0.5 + eps);
  2481. enc_color2[1] = int( ((float) avg_color_float2[1] / (17.0)) +0.5 + eps);
  2482. enc_color2[2] = int( ((float) avg_color_float2[2] / (17.0)) +0.5 + eps);
  2483. avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
  2484. avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
  2485. avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
  2486. avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
  2487. avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
  2488. avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
  2489. // 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
  2490. // ---------------------------------------------------------------------------------------------------
  2491. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  2492. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  2493. // ---------------------------------------------------------------------------------------------------
  2494. // Pack bits into the first word.
  2495. compressed1_flip = 0;
  2496. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  2497. PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
  2498. PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
  2499. PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
  2500. PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 59);
  2501. PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
  2502. PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
  2503. unsigned int best_pixel_indices1_MSB;
  2504. unsigned int best_pixel_indices1_LSB;
  2505. unsigned int best_pixel_indices2_MSB;
  2506. unsigned int best_pixel_indices2_LSB;
  2507. // upper part of block
  2508. flip_err = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2509. // lower part of block
  2510. flip_err += tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2511. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  2512. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  2513. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  2514. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  2515. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  2516. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  2517. }
  2518. // Now lets see which is the best table to use. Only 8 tables are possible.
  2519. if(norm_err <= flip_err)
  2520. {
  2521. compressed1 = compressed1_norm | 0;
  2522. compressed2 = compressed2_norm;
  2523. }
  2524. else
  2525. {
  2526. compressed1 = compressed1_flip | 1;
  2527. compressed2 = compressed2_flip;
  2528. }
  2529. }
  2530. // Compresses the block using only the differential mode in ETC1/ETC2
  2531. // Uses the average color as the base color in each half-block.
  2532. // If average colors are too different, use the average color of the entire block in both half-blocks.
  2533. // Tries both flipped and unflipped.
  2534. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  2535. 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)
  2536. {
  2537. unsigned int compressed1_norm, compressed2_norm;
  2538. unsigned int compressed1_flip, compressed2_flip;
  2539. uint8 avg_color_quant1[3], avg_color_quant2[3];
  2540. float avg_color_float1[3],avg_color_float2[3];
  2541. int enc_color1[3], enc_color2[3], diff[3];
  2542. int min_error=255*255*8*3;
  2543. unsigned int best_table_indices1=0, best_table_indices2=0;
  2544. unsigned int best_table1=0, best_table2=0;
  2545. int diffbit;
  2546. int norm_err=0;
  2547. int flip_err=0;
  2548. int best_err;
  2549. // First try normal blocks 2x4:
  2550. computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  2551. computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
  2552. // First test if avg_color1 is similar enough to avg_color2 so that
  2553. // we can use differential coding of colors.
  2554. enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
  2555. enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
  2556. enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
  2557. enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
  2558. enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
  2559. enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
  2560. diff[0] = enc_color2[0]-enc_color1[0];
  2561. diff[1] = enc_color2[1]-enc_color1[1];
  2562. diff[2] = enc_color2[2]-enc_color1[2];
  2563. if( !((diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3)) )
  2564. {
  2565. // The colors are too different. Use the same color in both blocks.
  2566. enc_color1[0] = int( JAS_ROUND(31.0*((avg_color_float1[0]+avg_color_float2[0])/2.0)/255.0) );
  2567. enc_color1[1] = int( JAS_ROUND(31.0*((avg_color_float1[1]+avg_color_float2[1])/2.0)/255.0) );
  2568. enc_color1[2] = int( JAS_ROUND(31.0*((avg_color_float1[2]+avg_color_float2[2])/2.0)/255.0) );
  2569. enc_color2[0] = enc_color1[0];
  2570. enc_color2[1] = enc_color1[1];
  2571. enc_color2[2] = enc_color1[2];
  2572. diff[0] = enc_color2[0]-enc_color1[0];
  2573. diff[1] = enc_color2[1]-enc_color1[1];
  2574. diff[2] = enc_color2[2]-enc_color1[2];
  2575. }
  2576. diffbit = 1;
  2577. // The difference to be coded:
  2578. diff[0] = enc_color2[0]-enc_color1[0];
  2579. diff[1] = enc_color2[1]-enc_color1[1];
  2580. diff[2] = enc_color2[2]-enc_color1[2];
  2581. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  2582. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  2583. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  2584. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  2585. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  2586. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  2587. // Pack bits into the first word.
  2588. // ETC1_RGB8_OES:
  2589. //
  2590. // a) bit layout in bits 63 through 32 if diffbit = 0
  2591. //
  2592. // 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
  2593. // ---------------------------------------------------------------------------------------------------
  2594. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  2595. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  2596. // ---------------------------------------------------------------------------------------------------
  2597. //
  2598. // b) bit layout in bits 63 through 32 if diffbit = 1
  2599. //
  2600. // 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
  2601. // ---------------------------------------------------------------------------------------------------
  2602. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  2603. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  2604. // ---------------------------------------------------------------------------------------------------
  2605. //
  2606. // c) bit layout in bits 31 through 0 (in both cases)
  2607. //
  2608. // 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
  2609. // --------------------------------------------------------------------------------------------------
  2610. // | most significant pixel index bits | least significant pixel index bits |
  2611. // | 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 |
  2612. // --------------------------------------------------------------------------------------------------
  2613. compressed1_norm = 0;
  2614. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  2615. PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
  2616. PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
  2617. PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
  2618. PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
  2619. PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
  2620. PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
  2621. unsigned int best_pixel_indices1_MSB;
  2622. unsigned int best_pixel_indices1_LSB;
  2623. unsigned int best_pixel_indices2_MSB;
  2624. unsigned int best_pixel_indices2_LSB;
  2625. best_enc_color1[0] = enc_color1[0];
  2626. best_enc_color1[1] = enc_color1[1];
  2627. best_enc_color1[2] = enc_color1[2];
  2628. best_enc_color2[0] = enc_color2[0];
  2629. best_enc_color2[1] = enc_color2[1];
  2630. best_enc_color2[2] = enc_color2[2];
  2631. norm_err = 0;
  2632. // left part of block
  2633. norm_err = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2634. // right part of block
  2635. norm_err += tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2636. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  2637. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  2638. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  2639. compressed2_norm = 0;
  2640. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  2641. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  2642. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  2643. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  2644. // Now try flipped blocks 4x2:
  2645. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  2646. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
  2647. // First test if avg_color1 is similar enough to avg_color2 so that
  2648. // we can use differential coding of colors.
  2649. enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
  2650. enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
  2651. enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
  2652. enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
  2653. enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
  2654. enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
  2655. diff[0] = enc_color2[0]-enc_color1[0];
  2656. diff[1] = enc_color2[1]-enc_color1[1];
  2657. diff[2] = enc_color2[2]-enc_color1[2];
  2658. if( !((diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3)) )
  2659. {
  2660. // The colors are too different. Use the same color in both blocks.
  2661. enc_color1[0] = int( JAS_ROUND(31.0*((avg_color_float1[0]+avg_color_float2[0])/2.0)/255.0) );
  2662. enc_color1[1] = int( JAS_ROUND(31.0*((avg_color_float1[1]+avg_color_float2[1])/2.0)/255.0) );
  2663. enc_color1[2] = int( JAS_ROUND(31.0*((avg_color_float1[2]+avg_color_float2[2])/2.0)/255.0) );
  2664. enc_color2[0] = enc_color1[0];
  2665. enc_color2[1] = enc_color1[1];
  2666. enc_color2[2] = enc_color1[2];
  2667. diff[0] = enc_color2[0]-enc_color1[0];
  2668. diff[1] = enc_color2[1]-enc_color1[1];
  2669. diff[2] = enc_color2[2]-enc_color1[2];
  2670. }
  2671. diffbit = 1;
  2672. // The difference to be coded:
  2673. diff[0] = enc_color2[0]-enc_color1[0];
  2674. diff[1] = enc_color2[1]-enc_color1[1];
  2675. diff[2] = enc_color2[2]-enc_color1[2];
  2676. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  2677. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  2678. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  2679. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  2680. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  2681. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  2682. // Pack bits into the first word.
  2683. compressed1_flip = 0;
  2684. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  2685. PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
  2686. PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
  2687. PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
  2688. PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
  2689. PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
  2690. PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
  2691. // upper part of block
  2692. flip_err = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2693. // lower part of block
  2694. flip_err += tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2695. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  2696. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  2697. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  2698. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  2699. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  2700. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  2701. // Now lets see which is the best table to use. Only 8 tables are possible.
  2702. if(norm_err <= flip_err)
  2703. {
  2704. compressed1 = compressed1_norm | 0;
  2705. compressed2 = compressed2_norm;
  2706. best_err = norm_err;
  2707. best_flip = 0;
  2708. }
  2709. else
  2710. {
  2711. compressed1 = compressed1_flip | 1;
  2712. compressed2 = compressed2_flip;
  2713. best_err = flip_err;
  2714. best_enc_color1[0] = enc_color1[0];
  2715. best_enc_color1[1] = enc_color1[1];
  2716. best_enc_color1[2] = enc_color1[2];
  2717. best_enc_color2[0] = enc_color2[0];
  2718. best_enc_color2[1] = enc_color2[1];
  2719. best_enc_color2[2] = enc_color2[2];
  2720. best_flip = 1;
  2721. }
  2722. return best_err;
  2723. }
  2724. // Compresses the block using only the differential mode in ETC1/ETC2
  2725. // Uses the average color as the base color in each half-block.
  2726. // If average colors are too different, use the average color of the entire block in both half-blocks.
  2727. // Tries both flipped and unflipped.
  2728. // Uses fixed point arithmetics where 1000 represents 1.0.
  2729. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  2730. unsigned int compressBlockOnlyDiffFlipAveragePerceptual1000(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  2731. {
  2732. unsigned int compressed1_norm, compressed2_norm;
  2733. unsigned int compressed1_flip, compressed2_flip;
  2734. uint8 avg_color_quant1[3], avg_color_quant2[3];
  2735. float avg_color_float1[3],avg_color_float2[3];
  2736. int enc_color1[3], enc_color2[3], diff[3];
  2737. unsigned int min_error=MAXERR1000;
  2738. unsigned int best_table_indices1=0, best_table_indices2=0;
  2739. unsigned int best_table1=0, best_table2=0;
  2740. int diffbit;
  2741. int norm_err=0;
  2742. int flip_err=0;
  2743. // First try normal blocks 2x4:
  2744. computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  2745. computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
  2746. // First test if avg_color1 is similar enough to avg_color2 so that
  2747. // we can use differential coding of colors.
  2748. enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
  2749. enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
  2750. enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
  2751. enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
  2752. enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
  2753. enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
  2754. diff[0] = enc_color2[0]-enc_color1[0];
  2755. diff[1] = enc_color2[1]-enc_color1[1];
  2756. diff[2] = enc_color2[2]-enc_color1[2];
  2757. if( !((diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3)) )
  2758. {
  2759. enc_color1[0] = (enc_color1[0] + enc_color2[0]) >> 1;
  2760. enc_color1[1] = (enc_color1[1] + enc_color2[1]) >> 1;
  2761. enc_color1[2] = (enc_color1[2] + enc_color2[2]) >> 1;
  2762. enc_color2[0] = enc_color1[0];
  2763. enc_color2[1] = enc_color1[1];
  2764. enc_color2[2] = enc_color1[2];
  2765. }
  2766. {
  2767. diffbit = 1;
  2768. // The difference to be coded:
  2769. diff[0] = enc_color2[0]-enc_color1[0];
  2770. diff[1] = enc_color2[1]-enc_color1[1];
  2771. diff[2] = enc_color2[2]-enc_color1[2];
  2772. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  2773. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  2774. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  2775. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  2776. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  2777. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  2778. // Pack bits into the first word.
  2779. // ETC1_RGB8_OES:
  2780. //
  2781. // a) bit layout in bits 63 through 32 if diffbit = 0
  2782. //
  2783. // 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
  2784. // ---------------------------------------------------------------------------------------------------
  2785. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  2786. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  2787. // ---------------------------------------------------------------------------------------------------
  2788. //
  2789. // b) bit layout in bits 63 through 32 if diffbit = 1
  2790. //
  2791. // 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
  2792. // ---------------------------------------------------------------------------------------------------
  2793. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  2794. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  2795. // ---------------------------------------------------------------------------------------------------
  2796. //
  2797. // c) bit layout in bits 31 through 0 (in both cases)
  2798. //
  2799. // 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
  2800. // --------------------------------------------------------------------------------------------------
  2801. // | most significant pixel index bits | least significant pixel index bits |
  2802. // | 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 |
  2803. // --------------------------------------------------------------------------------------------------
  2804. compressed1_norm = 0;
  2805. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  2806. PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
  2807. PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
  2808. PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
  2809. PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
  2810. PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
  2811. PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
  2812. unsigned int best_pixel_indices1_MSB;
  2813. unsigned int best_pixel_indices1_LSB;
  2814. unsigned int best_pixel_indices2_MSB;
  2815. unsigned int best_pixel_indices2_LSB;
  2816. norm_err = 0;
  2817. // left part of block
  2818. norm_err = tryalltables_3bittable2x4percep1000(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2819. // right part of block
  2820. norm_err += tryalltables_3bittable2x4percep1000(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2821. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  2822. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  2823. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  2824. compressed2_norm = 0;
  2825. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  2826. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  2827. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  2828. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  2829. }
  2830. // Now try flipped blocks 4x2:
  2831. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  2832. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
  2833. // First test if avg_color1 is similar enough to avg_color2 so that
  2834. // we can use differential coding of colors.
  2835. enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
  2836. enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
  2837. enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
  2838. enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
  2839. enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
  2840. enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
  2841. diff[0] = enc_color2[0]-enc_color1[0];
  2842. diff[1] = enc_color2[1]-enc_color1[1];
  2843. diff[2] = enc_color2[2]-enc_color1[2];
  2844. if( !((diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3)) )
  2845. {
  2846. enc_color1[0] = (enc_color1[0] + enc_color2[0]) >> 1;
  2847. enc_color1[1] = (enc_color1[1] + enc_color2[1]) >> 1;
  2848. enc_color1[2] = (enc_color1[2] + enc_color2[2]) >> 1;
  2849. enc_color2[0] = enc_color1[0];
  2850. enc_color2[1] = enc_color1[1];
  2851. enc_color2[2] = enc_color1[2];
  2852. }
  2853. {
  2854. diffbit = 1;
  2855. // The difference to be coded:
  2856. diff[0] = enc_color2[0]-enc_color1[0];
  2857. diff[1] = enc_color2[1]-enc_color1[1];
  2858. diff[2] = enc_color2[2]-enc_color1[2];
  2859. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  2860. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  2861. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  2862. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  2863. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  2864. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  2865. // Pack bits into the first word.
  2866. compressed1_flip = 0;
  2867. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  2868. PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
  2869. PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
  2870. PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
  2871. PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
  2872. PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
  2873. PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
  2874. unsigned int best_pixel_indices1_MSB;
  2875. unsigned int best_pixel_indices1_LSB;
  2876. unsigned int best_pixel_indices2_MSB;
  2877. unsigned int best_pixel_indices2_LSB;
  2878. // upper part of block
  2879. flip_err = tryalltables_3bittable4x2percep1000(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2880. // lower part of block
  2881. flip_err += tryalltables_3bittable4x2percep1000(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2882. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  2883. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  2884. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  2885. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  2886. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  2887. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  2888. }
  2889. unsigned int best_err;
  2890. if(norm_err <= flip_err)
  2891. {
  2892. compressed1 = compressed1_norm | 0;
  2893. compressed2 = compressed2_norm;
  2894. best_err = norm_err;
  2895. }
  2896. else
  2897. {
  2898. compressed1 = compressed1_flip | 1;
  2899. compressed2 = compressed2_flip;
  2900. best_err = flip_err;
  2901. }
  2902. return best_err;
  2903. }
  2904. // Compresses the block using both the individual and the differential mode in ETC1/ETC2
  2905. // Uses the average color as the base color in each half-block.
  2906. // Uses a perceptual error metric.
  2907. // Tries both flipped and unflipped.
  2908. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  2909. double compressBlockDiffFlipAveragePerceptual(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  2910. {
  2911. unsigned int compressed1_norm, compressed2_norm;
  2912. unsigned int compressed1_flip, compressed2_flip;
  2913. uint8 avg_color_quant1[3], avg_color_quant2[3];
  2914. float avg_color_float1[3],avg_color_float2[3];
  2915. int enc_color1[3], enc_color2[3], diff[3];
  2916. int min_error=255*255*8*3;
  2917. unsigned int best_table_indices1=0, best_table_indices2=0;
  2918. unsigned int best_table1=0, best_table2=0;
  2919. int diffbit;
  2920. int norm_err=0;
  2921. int flip_err=0;
  2922. // First try normal blocks 2x4:
  2923. computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  2924. computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
  2925. // First test if avg_color1 is similar enough to avg_color2 so that
  2926. // we can use differential coding of colors.
  2927. float eps;
  2928. enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
  2929. enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
  2930. enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
  2931. enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
  2932. enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
  2933. enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
  2934. diff[0] = enc_color2[0]-enc_color1[0];
  2935. diff[1] = enc_color2[1]-enc_color1[1];
  2936. diff[2] = enc_color2[2]-enc_color1[2];
  2937. if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
  2938. {
  2939. diffbit = 1;
  2940. // The difference to be coded:
  2941. diff[0] = enc_color2[0]-enc_color1[0];
  2942. diff[1] = enc_color2[1]-enc_color1[1];
  2943. diff[2] = enc_color2[2]-enc_color1[2];
  2944. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  2945. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  2946. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  2947. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  2948. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  2949. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  2950. // Pack bits into the first word.
  2951. // ETC1_RGB8_OES:
  2952. //
  2953. // a) bit layout in bits 63 through 32 if diffbit = 0
  2954. //
  2955. // 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
  2956. // ---------------------------------------------------------------------------------------------------
  2957. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  2958. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  2959. // ---------------------------------------------------------------------------------------------------
  2960. //
  2961. // b) bit layout in bits 63 through 32 if diffbit = 1
  2962. //
  2963. // 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
  2964. // ---------------------------------------------------------------------------------------------------
  2965. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  2966. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  2967. // ---------------------------------------------------------------------------------------------------
  2968. //
  2969. // c) bit layout in bits 31 through 0 (in both cases)
  2970. //
  2971. // 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
  2972. // --------------------------------------------------------------------------------------------------
  2973. // | most significant pixel index bits | least significant pixel index bits |
  2974. // | 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 |
  2975. // --------------------------------------------------------------------------------------------------
  2976. compressed1_norm = 0;
  2977. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  2978. PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
  2979. PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
  2980. PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
  2981. PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
  2982. PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
  2983. PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
  2984. unsigned int best_pixel_indices1_MSB;
  2985. unsigned int best_pixel_indices1_LSB;
  2986. unsigned int best_pixel_indices2_MSB;
  2987. unsigned int best_pixel_indices2_LSB;
  2988. norm_err = 0;
  2989. // left part of block
  2990. norm_err = tryalltables_3bittable2x4percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  2991. // right part of block
  2992. norm_err += tryalltables_3bittable2x4percep(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  2993. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  2994. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  2995. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  2996. compressed2_norm = 0;
  2997. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  2998. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  2999. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  3000. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  3001. }
  3002. else
  3003. {
  3004. diffbit = 0;
  3005. // The difference is bigger than what fits in 555 plus delta-333, so we will have
  3006. // to deal with 444 444.
  3007. eps = (float) 0.0001;
  3008. enc_color1[0] = int( ((float) avg_color_float1[0] / (17.0)) +0.5 + eps);
  3009. enc_color1[1] = int( ((float) avg_color_float1[1] / (17.0)) +0.5 + eps);
  3010. enc_color1[2] = int( ((float) avg_color_float1[2] / (17.0)) +0.5 + eps);
  3011. enc_color2[0] = int( ((float) avg_color_float2[0] / (17.0)) +0.5 + eps);
  3012. enc_color2[1] = int( ((float) avg_color_float2[1] / (17.0)) +0.5 + eps);
  3013. enc_color2[2] = int( ((float) avg_color_float2[2] / (17.0)) +0.5 + eps);
  3014. avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
  3015. avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
  3016. avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
  3017. avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
  3018. avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
  3019. avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
  3020. // Pack bits into the first word.
  3021. // 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
  3022. // ---------------------------------------------------------------------------------------------------
  3023. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  3024. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  3025. // ---------------------------------------------------------------------------------------------------
  3026. compressed1_norm = 0;
  3027. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  3028. PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
  3029. PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
  3030. PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
  3031. PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
  3032. PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
  3033. PUTBITSHIGH( compressed1_norm, enc_color2[2], 4, 43);
  3034. unsigned int best_pixel_indices1_MSB;
  3035. unsigned int best_pixel_indices1_LSB;
  3036. unsigned int best_pixel_indices2_MSB;
  3037. unsigned int best_pixel_indices2_LSB;
  3038. // left part of block
  3039. norm_err = tryalltables_3bittable2x4percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  3040. // right part of block
  3041. norm_err += tryalltables_3bittable2x4percep(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  3042. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  3043. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  3044. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  3045. compressed2_norm = 0;
  3046. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  3047. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  3048. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  3049. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  3050. }
  3051. // Now try flipped blocks 4x2:
  3052. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  3053. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
  3054. // First test if avg_color1 is similar enough to avg_color2 so that
  3055. // we can use differential coding of colors.
  3056. enc_color1[0] = int( JAS_ROUND(31.0*avg_color_float1[0]/255.0) );
  3057. enc_color1[1] = int( JAS_ROUND(31.0*avg_color_float1[1]/255.0) );
  3058. enc_color1[2] = int( JAS_ROUND(31.0*avg_color_float1[2]/255.0) );
  3059. enc_color2[0] = int( JAS_ROUND(31.0*avg_color_float2[0]/255.0) );
  3060. enc_color2[1] = int( JAS_ROUND(31.0*avg_color_float2[1]/255.0) );
  3061. enc_color2[2] = int( JAS_ROUND(31.0*avg_color_float2[2]/255.0) );
  3062. diff[0] = enc_color2[0]-enc_color1[0];
  3063. diff[1] = enc_color2[1]-enc_color1[1];
  3064. diff[2] = enc_color2[2]-enc_color1[2];
  3065. if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
  3066. {
  3067. diffbit = 1;
  3068. // The difference to be coded:
  3069. diff[0] = enc_color2[0]-enc_color1[0];
  3070. diff[1] = enc_color2[1]-enc_color1[1];
  3071. diff[2] = enc_color2[2]-enc_color1[2];
  3072. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  3073. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  3074. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  3075. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  3076. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  3077. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  3078. // Pack bits into the first word.
  3079. compressed1_flip = 0;
  3080. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  3081. PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
  3082. PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
  3083. PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
  3084. PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
  3085. PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
  3086. PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
  3087. unsigned int best_pixel_indices1_MSB;
  3088. unsigned int best_pixel_indices1_LSB;
  3089. unsigned int best_pixel_indices2_MSB;
  3090. unsigned int best_pixel_indices2_LSB;
  3091. // upper part of block
  3092. flip_err = tryalltables_3bittable4x2percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  3093. // lower part of block
  3094. flip_err += tryalltables_3bittable4x2percep(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  3095. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  3096. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  3097. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  3098. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  3099. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  3100. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  3101. }
  3102. else
  3103. {
  3104. diffbit = 0;
  3105. // The difference is bigger than what fits in 555 plus delta-333, so we will have
  3106. // to deal with 444 444.
  3107. eps = (float) 0.0001;
  3108. enc_color1[0] = int( ((float) avg_color_float1[0] / (17.0)) +0.5 + eps);
  3109. enc_color1[1] = int( ((float) avg_color_float1[1] / (17.0)) +0.5 + eps);
  3110. enc_color1[2] = int( ((float) avg_color_float1[2] / (17.0)) +0.5 + eps);
  3111. enc_color2[0] = int( ((float) avg_color_float2[0] / (17.0)) +0.5 + eps);
  3112. enc_color2[1] = int( ((float) avg_color_float2[1] / (17.0)) +0.5 + eps);
  3113. enc_color2[2] = int( ((float) avg_color_float2[2] / (17.0)) +0.5 + eps);
  3114. avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
  3115. avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
  3116. avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
  3117. avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
  3118. avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
  3119. avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
  3120. // 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
  3121. // ---------------------------------------------------------------------------------------------------
  3122. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  3123. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  3124. // ---------------------------------------------------------------------------------------------------
  3125. // Pack bits into the first word.
  3126. compressed1_flip = 0;
  3127. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  3128. PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
  3129. PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
  3130. PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
  3131. PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 59);
  3132. PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
  3133. PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
  3134. unsigned int best_pixel_indices1_MSB;
  3135. unsigned int best_pixel_indices1_LSB;
  3136. unsigned int best_pixel_indices2_MSB;
  3137. unsigned int best_pixel_indices2_LSB;
  3138. // upper part of block
  3139. flip_err = tryalltables_3bittable4x2percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  3140. // lower part of block
  3141. flip_err += tryalltables_3bittable4x2percep(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  3142. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  3143. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  3144. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  3145. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  3146. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  3147. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  3148. }
  3149. // Now lets see which is the best table to use. Only 8 tables are possible.
  3150. double best_err;
  3151. if(norm_err <= flip_err)
  3152. {
  3153. compressed1 = compressed1_norm | 0;
  3154. compressed2 = compressed2_norm;
  3155. best_err = norm_err;
  3156. }
  3157. else
  3158. {
  3159. compressed1 = compressed1_flip | 1;
  3160. compressed2 = compressed2_flip;
  3161. best_err = flip_err;
  3162. }
  3163. return best_err;
  3164. }
  3165. // This is our structure for matrix data
  3166. struct dMatrix
  3167. {
  3168. int width; // The number of coloumns in the matrix
  3169. int height; // The number of rows in the matrix
  3170. double *data; // The matrix data in row order
  3171. };
  3172. // Multiplies two matrices
  3173. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3174. dMatrix *multiplyMatrices( dMatrix *Amat, dMatrix *Bmat)
  3175. {
  3176. int xx,yy, q;
  3177. dMatrix *resmatrix;
  3178. if(Amat->width != Bmat->height)
  3179. {
  3180. printf("Cannot multiply matrices -- dimensions do not agree.\n");
  3181. exit(1);
  3182. }
  3183. // Allocate space for result
  3184. resmatrix = (dMatrix*) malloc(sizeof(dMatrix));
  3185. resmatrix->width = Bmat->width;
  3186. resmatrix->height = Amat->height;
  3187. resmatrix->data = (double*) malloc(sizeof(double)*(resmatrix->width)*(resmatrix->height));
  3188. for(yy = 0; yy<resmatrix->height; yy++)
  3189. for(xx = 0; xx<resmatrix->width; xx++)
  3190. for(q=0, resmatrix->data[yy*resmatrix->width+xx] = 0.0; q<Amat->width; q++)
  3191. resmatrix->data[yy*resmatrix->width+xx] += Amat->data[yy*Amat->width + q] * Bmat->data[q*Bmat->width+xx];
  3192. return(resmatrix);
  3193. }
  3194. // Transposes a matrix
  3195. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3196. void transposeMatrix( dMatrix *mat)
  3197. {
  3198. int xx, yy, zz;
  3199. double *temp;
  3200. int newwidth, newheight;
  3201. temp = (double*) malloc (sizeof(double)*(mat->width)*(mat->height));
  3202. for(zz = 0; zz<((mat->width)*(mat->height)); zz++)
  3203. temp[zz] = mat->data[zz];
  3204. newwidth = mat->height;
  3205. newheight= mat->width;
  3206. for(yy = 0; yy<newheight; yy++)
  3207. for(xx = 0; xx<newwidth; xx++)
  3208. mat->data[yy*newwidth+xx] = temp[xx*(mat->width)+yy];
  3209. mat->height = newheight;
  3210. mat->width = newwidth;
  3211. free(temp);
  3212. }
  3213. // In the planar mode in ETC2, the block can be partitioned as follows:
  3214. //
  3215. // O A A A H
  3216. // B D1 D3 C3
  3217. // B D2 C2 D5
  3218. // B C1 D4 D6
  3219. // V
  3220. // Here A-pixels, B-pixels and C-pixels only depend on two values. For instance, B-pixels only depend on O and V.
  3221. // This can be used to quickly rule out combinations of colors.
  3222. // Here we calculate the minimum error for the block if we know the red component for O and V.
  3223. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3224. unsigned int calcBBBred(uint8 *block, int colorO, int colorV)
  3225. {
  3226. colorO = (colorO << 2) | (colorO >> 4);
  3227. colorV = (colorV << 2) | (colorV >> 4);
  3228. unsigned int error = 0;
  3229. // Now first column: B B B
  3230. /* unroll loop for( yy=0; (yy<4) && (error <= best_error_sofar); yy++)*/
  3231. {
  3232. error = error + square_table[(block[4*4 + 0] - clamp_table[ ((((colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3233. error = error + square_table[(block[4*4*2 + 0] - clamp_table[ (((((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3234. error = error + square_table[(block[4*4*3 + 0] - clamp_table[ (((3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3235. }
  3236. return error;
  3237. }
  3238. // Calculating the minimum error for the block if we know the red component for H and V.
  3239. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3240. unsigned int calcCCCred(uint8 *block, int colorH, int colorV)
  3241. {
  3242. colorH = (colorH << 2) | (colorH >> 4);
  3243. colorV = (colorV << 2) | (colorV >> 4);
  3244. unsigned int error=0;
  3245. error = error + square_table[(block[4*4*3 + 4 + 0] - clamp_table[ (((colorH + 3*colorV)+2)>>2) + 255])+255];
  3246. error = error + square_table[(block[4*4*2 + 4*2 + 0] - clamp_table[ (((2*colorH + 2*colorV)+2)>>2) + 255])+255];
  3247. error = error + square_table[(block[4*4 + 4*3 + 0] - clamp_table[ (((3*colorH + colorV)+2)>>2) + 255])+255];
  3248. return error;
  3249. }
  3250. // Calculating the minimum error for the block if we know the red component for O and H.
  3251. // Uses perceptual error metric.
  3252. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3253. unsigned int calcLowestPossibleRedOHperceptual(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
  3254. {
  3255. colorO = (colorO << 2) | (colorO >> 4);
  3256. colorH = (colorH << 2) | (colorH >> 4);
  3257. unsigned int error;
  3258. error = square_table_percep_red[(block[0] - colorO) + 255];
  3259. error = error + square_table_percep_red[(block[4] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3260. if(error <= best_error_sofar)
  3261. {
  3262. error = error + square_table_percep_red[(block[4*2] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3263. error = error + square_table_percep_red[(block[4*3] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3264. }
  3265. return error;
  3266. }
  3267. // Calculating the minimum error for the block (in planar mode) if we know the red component for O and H.
  3268. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3269. unsigned int calcLowestPossibleRedOH(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
  3270. {
  3271. colorO = (colorO << 2) | (colorO >> 4);
  3272. colorH = (colorH << 2) | (colorH >> 4);
  3273. unsigned int error;
  3274. error = square_table[(block[0] - colorO) + 255];
  3275. error = error + square_table[(block[4] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3276. if(error <= best_error_sofar)
  3277. {
  3278. error = error + square_table[(block[4*2] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3279. error = error + square_table[(block[4*3] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3280. }
  3281. return error;
  3282. }
  3283. // Calculating the minimum error for the block (in planar mode) if we know the red component for O and H and V.
  3284. // Uses perceptual error metric.
  3285. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3286. 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)
  3287. {
  3288. colorO = (colorO << 2) | (colorO >> 4);
  3289. colorH = (colorH << 2) | (colorH >> 4);
  3290. colorV = (colorV << 2) | (colorV >> 4);
  3291. unsigned int error;
  3292. // The block can be partitioned into: O A A A
  3293. // B D1 D3 C3
  3294. // B D2 C2 D5
  3295. // B C1 D4 D6
  3296. int xpart_times_4;
  3297. // The first part: O A A A. It equals lowest_possible_error previously calculated.
  3298. // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
  3299. error = lowest_possible_error + BBBvalue + CCCvalue;
  3300. // The remaining pixels to cover are D1 through D6.
  3301. if(error <= best_error_sofar)
  3302. {
  3303. // Second column: D1 D2 but not C1
  3304. xpart_times_4 = (colorH-colorO);
  3305. error = error + square_table_percep_red[(block[4*4 + 4 + 0] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3306. 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];
  3307. // Third column: D3 notC2 D4
  3308. xpart_times_4 = (colorH-colorO) << 1;
  3309. 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];
  3310. if(error <= best_error_sofar)
  3311. {
  3312. 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];
  3313. // Forth column: notC3 D5 D6
  3314. xpart_times_4 = 3*(colorH-colorO);
  3315. 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];
  3316. 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];
  3317. }
  3318. }
  3319. return error;
  3320. }
  3321. // Calculating the minimum error for the block (in planar mode) if we know the red component for O and H and V.
  3322. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3323. 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)
  3324. {
  3325. colorO = (colorO << 2) | (colorO >> 4);
  3326. colorH = (colorH << 2) | (colorH >> 4);
  3327. colorV = (colorV << 2) | (colorV >> 4);
  3328. unsigned int error;
  3329. // The block can be partitioned into: O A A A
  3330. // B D1 D3 C3
  3331. // B D2 C2 D5
  3332. // B C1 D4 D6
  3333. int xpart_times_4;
  3334. // The first part: O A A A. It equals lowest_possible_error previously calculated.
  3335. // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
  3336. error = lowest_possible_error + BBBvalue + CCCvalue;
  3337. // The remaining pixels to cover are D1 through D6.
  3338. if(error <= best_error_sofar)
  3339. {
  3340. // Second column: D1 D2 but not C1
  3341. xpart_times_4 = (colorH-colorO);
  3342. error = error + square_table[(block[4*4 + 4 + 0] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3343. error = error + square_table[(block[4*4*2 + 4 + 0] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3344. // Third column: D3 notC2 D4
  3345. xpart_times_4 = (colorH-colorO) << 1;
  3346. error = error + square_table[(block[4*4 + 4*2 + 0] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3347. if(error <= best_error_sofar)
  3348. {
  3349. 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];
  3350. // Forth column: notC3 D5 D6
  3351. xpart_times_4 = 3*(colorH-colorO);
  3352. 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];
  3353. 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];
  3354. }
  3355. }
  3356. return error;
  3357. }
  3358. // Calculating the minimum error for the block (in planar mode) if we know the red component for O and H.
  3359. // Uses perceptual error metrics.
  3360. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3361. unsigned int calcLowestPossibleGreenOHperceptual(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
  3362. {
  3363. colorO = (colorO << 1) | (colorO >> 6);
  3364. colorH = (colorH << 1) | (colorH >> 6);
  3365. unsigned int error;
  3366. error = square_table_percep_green[(block[1] - colorO) + 255];
  3367. error = error + square_table_percep_green[(block[4 + 1] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3368. if(error <= best_error_sofar)
  3369. {
  3370. error = error + square_table_percep_green[(block[4*2 + 1] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3371. error = error + square_table_percep_green[(block[4*3 + 1] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3372. }
  3373. return error;
  3374. }
  3375. // Calculating the minimum error for the block (in planar mode) if we know the red component for O and H.
  3376. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3377. unsigned int calcLowestPossibleGreenOH(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
  3378. {
  3379. colorO = (colorO << 1) | (colorO >> 6);
  3380. colorH = (colorH << 1) | (colorH >> 6);
  3381. unsigned int error;
  3382. error = square_table[(block[1] - colorO) + 255];
  3383. error = error + square_table[(block[4 + 1] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3384. if(error <= best_error_sofar)
  3385. {
  3386. error = error + square_table[(block[4*2 + 1] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3387. error = error + square_table[(block[4*3 + 1] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3388. }
  3389. return error;
  3390. }
  3391. // Calculating the minimum error for the block (in planar mode) if we know the green component for O and V.
  3392. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3393. unsigned int calcBBBgreen(uint8 *block, int colorO, int colorV)
  3394. {
  3395. colorO = (colorO << 1) | (colorO >> 6);
  3396. colorV = (colorV << 1) | (colorV >> 6);
  3397. unsigned int error = 0;
  3398. // Now first column: B B B
  3399. /* unroll loop for( yy=0; (yy<4) && (error <= best_error_sofar); yy++)*/
  3400. {
  3401. error = error + square_table[(block[4*4 + 1] - clamp_table[ ((((colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3402. error = error + square_table[(block[4*4*2 + 1] - clamp_table[ (((((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3403. error = error + square_table[(block[4*4*3 + 1] - clamp_table[ (((3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3404. }
  3405. return error;
  3406. }
  3407. // Calculating the minimum error for the block (in planar mode) if we know the green component for H and V.
  3408. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3409. unsigned int calcCCCgreen(uint8 *block, int colorH, int colorV)
  3410. {
  3411. colorH = (colorH << 1) | (colorH >> 6);
  3412. colorV = (colorV << 1) | (colorV >> 6);
  3413. unsigned int error=0;
  3414. error = error + square_table[(block[4*4*3 + 4 + 1] - clamp_table[ (((colorH + 3*colorV)+2)>>2) + 255])+255];
  3415. error = error + square_table[(block[4*4*2 + 4*2 + 1] - clamp_table[ (((2*colorH + 2*colorV)+2)>>2) + 255])+255];
  3416. error = error + square_table[(block[4*4 + 4*3 + 1] - clamp_table[ (((3*colorH + colorV)+2)>>2) + 255])+255];
  3417. return error;
  3418. }
  3419. // Calculating the minimum error for the block (in planar mode) if we know the green component for H V and O.
  3420. // Uses perceptual error metric.
  3421. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3422. 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)
  3423. {
  3424. colorO = (colorO << 1) | (colorO >> 6);
  3425. colorH = (colorH << 1) | (colorH >> 6);
  3426. colorV = (colorV << 1) | (colorV >> 6);
  3427. unsigned int error;
  3428. // The block can be partitioned into: O A A A
  3429. // B D1 D3 C3
  3430. // B D2 C2 D5
  3431. // B C1 D4 D6
  3432. int xpart_times_4;
  3433. // The first part: O A A A. It equals lowest_possible_error previously calculated.
  3434. // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
  3435. error = lowest_possible_error + BBBvalue + CCCvalue;
  3436. // The remaining pixels to cover are D1 through D6.
  3437. if(error <= best_error_sofar)
  3438. {
  3439. // Second column: D1 D2 but not C1
  3440. xpart_times_4 = (colorH-colorO);
  3441. error = error + square_table_percep_green[(block[4*4 + 4 + 1] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3442. 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];
  3443. // Third column: D3 notC2 D4
  3444. xpart_times_4 = (colorH-colorO) << 1;
  3445. 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];
  3446. if(error <= best_error_sofar)
  3447. {
  3448. 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];
  3449. // Forth column: notC3 D5 D6
  3450. xpart_times_4 = 3*(colorH-colorO);
  3451. 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];
  3452. 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];
  3453. }
  3454. }
  3455. return error;
  3456. }
  3457. // Calculating the minimum error for the block (in planar mode) if we know the green component for H V and O.
  3458. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3459. 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)
  3460. {
  3461. colorO = (colorO << 1) | (colorO >> 6);
  3462. colorH = (colorH << 1) | (colorH >> 6);
  3463. colorV = (colorV << 1) | (colorV >> 6);
  3464. unsigned int error;
  3465. // The block can be partitioned into: O A A A
  3466. // B D1 D3 C3
  3467. // B D2 C2 D5
  3468. // B C1 D4 D6
  3469. int xpart_times_4;
  3470. // The first part: O A A A. It equals lowest_possible_error previously calculated.
  3471. // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
  3472. error = lowest_possible_error + BBBvalue + CCCvalue;
  3473. // The remaining pixels to cover are D1 through D6.
  3474. if(error <= best_error_sofar)
  3475. {
  3476. // Second column: D1 D2 but not C1
  3477. xpart_times_4 = (colorH-colorO);
  3478. error = error + square_table[(block[4*4 + 4 + 1] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3479. error = error + square_table[(block[4*4*2 + 4 + 1] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3480. // Third column: D3 notC2 D4
  3481. xpart_times_4 = (colorH-colorO) << 1;
  3482. error = error + square_table[(block[4*4 + 4*2 + 1] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3483. if(error <= best_error_sofar)
  3484. {
  3485. 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];
  3486. // Forth column: notC3 D5 D6
  3487. xpart_times_4 = 3*(colorH-colorO);
  3488. 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];
  3489. 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];
  3490. }
  3491. }
  3492. return error;
  3493. }
  3494. // Calculating the minimum error for the block (in planar mode) if we know the blue component for O and V.
  3495. // Uses perceptual error metric.
  3496. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3497. unsigned int calcBBBbluePerceptual(uint8 *block, int colorO, int colorV)
  3498. {
  3499. colorO = (colorO << 2) | (colorO >> 4);
  3500. colorV = (colorV << 2) | (colorV >> 4);
  3501. unsigned int error = 0;
  3502. // Now first column: B B B
  3503. /* unroll loop for( yy=0; (yy<4) && (error <= best_error_sofar); yy++)*/
  3504. {
  3505. error = error + square_table_percep_blue[(block[4*4 + 2] - clamp_table[ ((((colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3506. error = error + square_table_percep_blue[(block[4*4*2 + 2] - clamp_table[ (((((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3507. error = error + square_table_percep_blue[(block[4*4*3 + 2] - clamp_table[ (((3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3508. }
  3509. return error;
  3510. }
  3511. // Calculating the minimum error for the block (in planar mode) if we know the blue component for O and V.
  3512. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3513. unsigned int calcBBBblue(uint8 *block, int colorO, int colorV)
  3514. {
  3515. colorO = (colorO << 2) | (colorO >> 4);
  3516. colorV = (colorV << 2) | (colorV >> 4);
  3517. unsigned int error = 0;
  3518. // Now first column: B B B
  3519. /* unroll loop for( yy=0; (yy<4) && (error <= best_error_sofar); yy++)*/
  3520. {
  3521. error = error + square_table[(block[4*4 + 2] - clamp_table[ ((((colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3522. error = error + square_table[(block[4*4*2 + 2] - clamp_table[ (((((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3523. error = error + square_table[(block[4*4*3 + 2] - clamp_table[ (((3*(colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3524. }
  3525. return error;
  3526. }
  3527. // Calculating the minimum error for the block (in planar mode) if we know the blue component for H and V.
  3528. // Uses perceptual error metric.
  3529. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3530. unsigned int calcCCCbluePerceptual(uint8 *block, int colorH, int colorV)
  3531. {
  3532. colorH = (colorH << 2) | (colorH >> 4);
  3533. colorV = (colorV << 2) | (colorV >> 4);
  3534. unsigned int error=0;
  3535. error = error + square_table_percep_blue[(block[4*4*3 + 4 + 2] - clamp_table[ (((colorH + 3*colorV)+2)>>2) + 255])+255];
  3536. error = error + square_table_percep_blue[(block[4*4*2 + 4*2 + 2] - clamp_table[ (((2*colorH + 2*colorV)+2)>>2) + 255])+255];
  3537. error = error + square_table_percep_blue[(block[4*4 + 4*3 + 2] - clamp_table[ (((3*colorH + colorV)+2)>>2) + 255])+255];
  3538. return error;
  3539. }
  3540. // Calculating the minimum error for the block (in planar mode) if we know the blue component for O and V.
  3541. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3542. unsigned int calcCCCblue(uint8 *block, int colorH, int colorV)
  3543. {
  3544. colorH = (colorH << 2) | (colorH >> 4);
  3545. colorV = (colorV << 2) | (colorV >> 4);
  3546. unsigned int error=0;
  3547. error = error + square_table[(block[4*4*3 + 4 + 2] - clamp_table[ (((colorH + 3*colorV)+2)>>2) + 255])+255];
  3548. error = error + square_table[(block[4*4*2 + 4*2 + 2] - clamp_table[ (((2*colorH + 2*colorV)+2)>>2) + 255])+255];
  3549. error = error + square_table[(block[4*4 + 4*3 + 2] - clamp_table[ (((3*colorH + colorV)+2)>>2) + 255])+255];
  3550. return error;
  3551. }
  3552. // Calculating the minimum error for the block (in planar mode) if we know the blue component for O and H.
  3553. // Uses perceptual error metric.
  3554. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3555. unsigned int calcLowestPossibleBlueOHperceptual(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
  3556. {
  3557. colorO = (colorO << 2) | (colorO >> 4);
  3558. colorH = (colorH << 2) | (colorH >> 4);
  3559. unsigned int error;
  3560. error = square_table_percep_blue[(block[2] - colorO) + 255];
  3561. error = error + square_table_percep_blue[(block[4+2] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3562. if(error <= best_error_sofar)
  3563. {
  3564. error = error + square_table_percep_blue[(block[4*2+2] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3565. error = error + square_table_percep_blue[(block[4*3+2] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3566. }
  3567. return error;
  3568. }
  3569. // Calculating the minimum error for the block (in planar mode) if we know the blue component for O and H.
  3570. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3571. unsigned int calcLowestPossibleBlueOH(uint8 *block, int colorO, int colorH, unsigned int best_error_sofar)
  3572. {
  3573. colorO = (colorO << 2) | (colorO >> 4);
  3574. colorH = (colorH << 2) | (colorH >> 4);
  3575. unsigned int error;
  3576. error = square_table[(block[2] - colorO) + 255];
  3577. error = error + square_table[(block[4+2] - clamp_table[ ((( (colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3578. if(error <= best_error_sofar)
  3579. {
  3580. error = error + square_table[(block[4*2+2] - clamp_table[ ((( ((colorH-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3581. error = error + square_table[(block[4*3+2] - clamp_table[ ((( 3*(colorH-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3582. }
  3583. return error;
  3584. }
  3585. // Calculating the minimum error for the block (in planar mode) if we know the blue component for O, V and H.
  3586. // Uses perceptual error metric.
  3587. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3588. 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)
  3589. {
  3590. colorO = (colorO << 2) | (colorO >> 4);
  3591. colorH = (colorH << 2) | (colorH >> 4);
  3592. colorV = (colorV << 2) | (colorV >> 4);
  3593. unsigned int error;
  3594. // The block can be partitioned into: O A A A
  3595. // B D1 D3 C3
  3596. // B D2 C2 D5
  3597. // B C1 D4 D6
  3598. int xpart_times_4;
  3599. // The first part: O A A A. It equals lowest_possible_error previously calculated.
  3600. // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
  3601. error = lowest_possible_error + BBBvalue + CCCvalue;
  3602. // The remaining pixels to cover are D1 through D6.
  3603. if(error <= best_error_sofar)
  3604. {
  3605. // Second column: D1 D2 but not C1
  3606. xpart_times_4 = (colorH-colorO);
  3607. error = error + square_table_percep_blue[(block[4*4 + 4 + 2] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3608. 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];
  3609. // Third column: D3 notC2 D4
  3610. xpart_times_4 = (colorH-colorO) << 1;
  3611. 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];
  3612. if(error <= best_error_sofar)
  3613. {
  3614. 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];
  3615. // Forth column: notC3 D5 D6
  3616. xpart_times_4 = 3*(colorH-colorO);
  3617. 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];
  3618. 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];
  3619. }
  3620. }
  3621. return error;
  3622. }
  3623. // Calculating the minimum error for the block (in planar mode) if we know the blue component for O, V and H.
  3624. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3625. 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)
  3626. {
  3627. colorO = (colorO << 2) | (colorO >> 4);
  3628. colorH = (colorH << 2) | (colorH >> 4);
  3629. colorV = (colorV << 2) | (colorV >> 4);
  3630. unsigned int error;
  3631. // The block can be partitioned into: O A A A
  3632. // B D1 D3 C3
  3633. // B D2 C2 D5
  3634. // B C1 D4 D6
  3635. int xpart_times_4;
  3636. // The first part: O A A A. It equals lowest_possible_error previously calculated.
  3637. // lowest_possible_error is OAAA, BBBvalue is BBB and CCCvalue is C1C2C3.
  3638. error = lowest_possible_error + BBBvalue + CCCvalue;
  3639. // The remaining pixels to cover are D1 through D6.
  3640. if(error <= best_error_sofar)
  3641. {
  3642. // Second column: D1 D2 but not C1
  3643. xpart_times_4 = (colorH-colorO);
  3644. error = error + square_table[(block[4*4 + 4 + 2] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3645. error = error + square_table[(block[4*4*2 + 4 + 2] - clamp_table[ (((xpart_times_4 + ((colorV-colorO)<<1) + 4*colorO)+2)>>2) + 255])+255];
  3646. // Third column: D3 notC2 D4
  3647. xpart_times_4 = (colorH-colorO) << 1;
  3648. error = error + square_table[(block[4*4 + 4*2 + 2] - clamp_table[ (((xpart_times_4 + (colorV-colorO) + 4*colorO)+2)>>2) + 255])+255];
  3649. if(error <= best_error_sofar)
  3650. {
  3651. 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];
  3652. // Forth column: notC3 D5 D6
  3653. xpart_times_4 = 3*(colorH-colorO);
  3654. 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];
  3655. 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];
  3656. }
  3657. }
  3658. return error;
  3659. }
  3660. // This function uses least squares in order to determine the best values of the plane.
  3661. // This is close to optimal, but not quite, due to nonlinearities in the expantion from 6 and 7 bits to 8, and
  3662. // in the clamping to a number between 0 and the maximum.
  3663. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3664. void compressBlockPlanar57(uint8 *img, int width,int height,int startx,int starty, unsigned int &compressed57_1, unsigned int &compressed57_2)
  3665. {
  3666. // Use least squares to find the solution with the smallest error.
  3667. // That is, find the vector x so that |Ax-b|^2 is minimized, where
  3668. // x = [Ro Rr Rv]';
  3669. // 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 ;
  3670. // 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 ;
  3671. // 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]';
  3672. // b = [r11 r12 r13 r14 r21 r22 r23 r24 r31 r32 r33 r34 r41 r42 r43 r44];
  3673. //
  3674. // That is, find solution x = inv(A' * A) * A' * b
  3675. // = C * A' * b;
  3676. // C is always the same, so we have calculated it off-line here.
  3677. // = C * D
  3678. int xx,yy, cc;
  3679. double coeffsA[48]= { 1.00, 0.00, 0.00,
  3680. 0.75, 0.25, 0.00,
  3681. 0.50, 0.50, 0.00,
  3682. 0.25, 0.75, 0.00,
  3683. 0.75, 0.00, 0.25,
  3684. 0.50, 0.25, 0.25,
  3685. 0.25, 0.50, 0.25,
  3686. 0.00, 0.75, 0.25,
  3687. 0.50, 0.00, 0.50,
  3688. 0.25, 0.25, 0.50,
  3689. 0.00, 0.50, 0.50,
  3690. -0.25, 0.75, 0.50,
  3691. 0.25, 0.00, 0.75,
  3692. 0.00, 0.25, 0.75,
  3693. -0.25, 0.50, 0.75,
  3694. -0.50, 0.75, 0.75};
  3695. double coeffsC[9] = {0.2875, -0.0125, -0.0125, -0.0125, 0.4875, -0.3125, -0.0125, -0.3125, 0.4875};
  3696. double colorO[3], colorH[3], colorV[3];
  3697. uint8 colorO8[3], colorH8[3], colorV8[3];
  3698. dMatrix *D_matrix;
  3699. dMatrix *x_vector;
  3700. dMatrix A_matrix; A_matrix.width = 3; A_matrix.height = 16;
  3701. A_matrix.data = coeffsA;
  3702. dMatrix C_matrix; C_matrix.width = 3; C_matrix.height = 3;
  3703. C_matrix.data = coeffsC;
  3704. dMatrix b_vector; b_vector.width = 1; b_vector.height = 16;
  3705. b_vector.data = (double*) malloc(sizeof(double)*b_vector.width*b_vector.height);
  3706. transposeMatrix(&A_matrix);
  3707. // Red component
  3708. // Load color data into vector b:
  3709. for(cc = 0, yy = 0; yy<4; yy++)
  3710. for(xx = 0; xx<4; xx++)
  3711. b_vector.data[cc++] = img[3*width*(starty+yy) + 3*(startx+xx) + 0];
  3712. D_matrix = multiplyMatrices(&A_matrix, &b_vector);
  3713. x_vector = multiplyMatrices(&C_matrix, D_matrix);
  3714. colorO[0] = CLAMP(0.0, x_vector->data[0], 255.0);
  3715. colorH[0] = CLAMP(0.0, x_vector->data[1], 255.0);
  3716. colorV[0] = CLAMP(0.0, x_vector->data[2], 255.0);
  3717. free(D_matrix->data); free(D_matrix);
  3718. free(x_vector->data); free(x_vector);
  3719. // Green component
  3720. // Load color data into vector b:
  3721. for(cc = 0, yy = 0; yy<4; yy++)
  3722. for(xx = 0; xx<4; xx++)
  3723. b_vector.data[cc++] = img[3*width*(starty+yy) + 3*(startx+xx) + 1];
  3724. D_matrix = multiplyMatrices(&A_matrix, &b_vector);
  3725. x_vector = multiplyMatrices(&C_matrix, D_matrix);
  3726. colorO[1] = CLAMP(0.0, x_vector->data[0], 255.0);
  3727. colorH[1] = CLAMP(0.0, x_vector->data[1], 255.0);
  3728. colorV[1] = CLAMP(0.0, x_vector->data[2], 255.0);
  3729. free(D_matrix->data); free(D_matrix);
  3730. free(x_vector->data); free(x_vector);
  3731. // Blue component
  3732. // Load color data into vector b:
  3733. for(cc = 0, yy = 0; yy<4; yy++)
  3734. for(xx = 0; xx<4; xx++)
  3735. b_vector.data[cc++] = img[3*width*(starty+yy) + 3*(startx+xx) + 2];
  3736. D_matrix = multiplyMatrices(&A_matrix, &b_vector);
  3737. x_vector = multiplyMatrices(&C_matrix, D_matrix);
  3738. colorO[2] = CLAMP(0.0, x_vector->data[0], 255.0);
  3739. colorH[2] = CLAMP(0.0, x_vector->data[1], 255.0);
  3740. colorV[2] = CLAMP(0.0, x_vector->data[2], 255.0);
  3741. free(D_matrix->data); free(D_matrix);
  3742. free(x_vector->data); free(x_vector);
  3743. // Quantize to 6 bits
  3744. double D = 255*(1.0/((1<<6)-1.0) );
  3745. colorO8[0] = JAS_ROUND((1.0*colorO[0])/D);
  3746. colorO8[2] = JAS_ROUND((1.0*colorO[2])/D);
  3747. colorH8[0] = JAS_ROUND((1.0*colorH[0])/D);
  3748. colorH8[2] = JAS_ROUND((1.0*colorH[2])/D);
  3749. colorV8[0] = JAS_ROUND((1.0*colorV[0])/D);
  3750. colorV8[2] = JAS_ROUND((1.0*colorV[2])/D);
  3751. // Quantize to 7 bits
  3752. D = 255*(1.0/((1<<7)-1.0) );
  3753. colorO8[1] = JAS_ROUND((1.0*colorO[1])/D);
  3754. colorH8[1] = JAS_ROUND((1.0*colorH[1])/D);
  3755. colorV8[1] = JAS_ROUND((1.0*colorV[1])/D);
  3756. // Pack bits in 57 bits
  3757. // 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
  3758. // ------------------------------------------------------------------------------------------------
  3759. // | R0 | G0 | B0 | RH | GH |
  3760. // ------------------------------------------------------------------------------------------------
  3761. //
  3762. // 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
  3763. // ------------------------------------------------------------------------------------------------
  3764. // | BH | RV | GV | BV | not used |
  3765. // ------------------------------------------------------------------------------------------------
  3766. compressed57_1 = 0;
  3767. compressed57_2 = 0;
  3768. PUTBITSHIGH( compressed57_1, colorO8[0], 6, 63);
  3769. PUTBITSHIGH( compressed57_1, colorO8[1], 7, 57);
  3770. PUTBITSHIGH( compressed57_1, colorO8[2], 6, 50);
  3771. PUTBITSHIGH( compressed57_1, colorH8[0], 6, 44);
  3772. PUTBITSHIGH( compressed57_1, colorH8[1], 7, 38);
  3773. PUTBITS( compressed57_2, colorH8[2], 6, 31);
  3774. PUTBITS( compressed57_2, colorV8[0], 6, 25);
  3775. PUTBITS( compressed57_2, colorV8[1], 7, 19);
  3776. PUTBITS( compressed57_2, colorV8[2], 6, 12);
  3777. }
  3778. // During search it is not convenient to store the bits the way they are stored in the
  3779. // file format. Hence, after search, it is converted to this format.
  3780. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3781. void stuff57bits(unsigned int planar57_word1, unsigned int planar57_word2, unsigned int &planar_word1, unsigned int &planar_word2)
  3782. {
  3783. // Put bits in twotimer configuration for 57 bits (red and green dont overflow, green does)
  3784. //
  3785. // Go from this bit layout:
  3786. //
  3787. // 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
  3788. // -----------------------------------------------------------------------------------------------
  3789. // |R0 |G01G02 |B01B02 ;B03 |RH1 |RH2|GH |
  3790. // -----------------------------------------------------------------------------------------------
  3791. //
  3792. // 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
  3793. // -----------------------------------------------------------------------------------------------
  3794. // |BH |RV |GV |BV | not used |
  3795. // -----------------------------------------------------------------------------------------------
  3796. //
  3797. // To this:
  3798. //
  3799. // 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
  3800. // ------------------------------------------------------------------------------------------------
  3801. // |//|R0 |G01|/|G02 |B01|/ // //|B02 |//|B03 |RH1 |df|RH2|
  3802. // ------------------------------------------------------------------------------------------------
  3803. //
  3804. // 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
  3805. // -----------------------------------------------------------------------------------------------
  3806. // |GH |BH |RV |GV |BV |
  3807. // -----------------------------------------------------------------------------------------------
  3808. //
  3809. // 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
  3810. // ---------------------------------------------------------------------------------------------------
  3811. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  3812. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  3813. // ---------------------------------------------------------------------------------------------------
  3814. uint8 RO, GO1, GO2, BO1, BO2, BO3, RH1, RH2, GH, BH, RV, GV, BV;
  3815. uint8 bit, a, b, c, d, bits;
  3816. RO = GETBITSHIGH( planar57_word1, 6, 63);
  3817. GO1= GETBITSHIGH( planar57_word1, 1, 57);
  3818. GO2= GETBITSHIGH( planar57_word1, 6, 56);
  3819. BO1= GETBITSHIGH( planar57_word1, 1, 50);
  3820. BO2= GETBITSHIGH( planar57_word1, 2, 49);
  3821. BO3= GETBITSHIGH( planar57_word1, 3, 47);
  3822. RH1= GETBITSHIGH( planar57_word1, 5, 44);
  3823. RH2= GETBITSHIGH( planar57_word1, 1, 39);
  3824. GH = GETBITSHIGH( planar57_word1, 7, 38);
  3825. BH = GETBITS( planar57_word2, 6, 31);
  3826. RV = GETBITS( planar57_word2, 6, 25);
  3827. GV = GETBITS( planar57_word2, 7, 19);
  3828. BV = GETBITS( planar57_word2, 6, 12);
  3829. planar_word1 = 0; planar_word2 = 0;
  3830. PUTBITSHIGH( planar_word1, RO, 6, 62);
  3831. PUTBITSHIGH( planar_word1, GO1, 1, 56);
  3832. PUTBITSHIGH( planar_word1, GO2, 6, 54);
  3833. PUTBITSHIGH( planar_word1, BO1, 1, 48);
  3834. PUTBITSHIGH( planar_word1, BO2, 2, 44);
  3835. PUTBITSHIGH( planar_word1, BO3, 3, 41);
  3836. PUTBITSHIGH( planar_word1, RH1, 5, 38);
  3837. PUTBITSHIGH( planar_word1, RH2, 1, 32);
  3838. PUTBITS( planar_word2, GH, 7, 31);
  3839. PUTBITS( planar_word2, BH, 6, 24);
  3840. PUTBITS( planar_word2, RV, 6, 18);
  3841. PUTBITS( planar_word2, GV, 7, 12);
  3842. PUTBITS( planar_word2, BV, 6, 5);
  3843. // Make sure that red does not overflow:
  3844. bit = GETBITSHIGH( planar_word1, 1, 62);
  3845. PUTBITSHIGH( planar_word1, !bit, 1, 63);
  3846. // Make sure that green does not overflow:
  3847. bit = GETBITSHIGH( planar_word1, 1, 54);
  3848. PUTBITSHIGH( planar_word1, !bit, 1, 55);
  3849. // Make sure that blue overflows:
  3850. a = GETBITSHIGH( planar_word1, 1, 44);
  3851. b = GETBITSHIGH( planar_word1, 1, 43);
  3852. c = GETBITSHIGH( planar_word1, 1, 41);
  3853. d = GETBITSHIGH( planar_word1, 1, 40);
  3854. // The following bit abcd bit sequences should be padded with ones: 0111, 1010, 1011, 1101, 1110, 1111
  3855. // The following logical expression checks for the presence of any of those:
  3856. bit = (a & c) | (!a & b & c & d) | (a & b & !c & d);
  3857. bits = 0xf*bit;
  3858. PUTBITSHIGH( planar_word1, bits, 3, 47);
  3859. PUTBITSHIGH( planar_word1, !bit, 1, 42);
  3860. // Set diffbit
  3861. PUTBITSHIGH( planar_word1, 1, 1, 33);
  3862. }
  3863. // During search it is not convenient to store the bits the way they are stored in the
  3864. // file format. Hence, after search, it is converted to this format.
  3865. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3866. void stuff58bits(unsigned int thumbH58_word1, unsigned int thumbH58_word2, unsigned int &thumbH_word1, unsigned int &thumbH_word2)
  3867. {
  3868. // Put bits in twotimer configuration for 58 (red doesn't overflow, green does)
  3869. //
  3870. // Go from this bit layout:
  3871. //
  3872. //
  3873. // |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|
  3874. // |-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
  3875. //
  3876. // |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|
  3877. // |---------------------------------------index bits----------------------------------------------|
  3878. //
  3879. // To this:
  3880. //
  3881. // 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
  3882. // -----------------------------------------------------------------------------------------------
  3883. // |//|R0 |G0 |// // //|G0|B0|//|B0b |R1 |G1 |B0 |d2|df|d1|
  3884. // -----------------------------------------------------------------------------------------------
  3885. //
  3886. // |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|
  3887. // |---------------------------------------index bits----------------------------------------------|
  3888. //
  3889. // 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
  3890. // -----------------------------------------------------------------------------------------------
  3891. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |df|fp|
  3892. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bt|bt|
  3893. // -----------------------------------------------------------------------------------------------
  3894. //
  3895. //
  3896. // Thus, what we are really doing is going from this bit layout:
  3897. //
  3898. //
  3899. // |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 |
  3900. // |-------empty-----|part0---------------|part1|part2------------------------------------------|part3|
  3901. //
  3902. // To this:
  3903. //
  3904. // 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
  3905. // --------------------------------------------------------------------------------------------------|
  3906. // |//|part0 |// // //|part1|//|part2 |df|part3|
  3907. // --------------------------------------------------------------------------------------------------|
  3908. unsigned int part0, part1, part2, part3;
  3909. uint8 bit, a, b, c, d, bits;
  3910. // move parts
  3911. part0 = GETBITSHIGH( thumbH58_word1, 7, 57);
  3912. part1 = GETBITSHIGH( thumbH58_word1, 2, 50);
  3913. part2 = GETBITSHIGH( thumbH58_word1,16, 48);
  3914. part3 = GETBITSHIGH( thumbH58_word1, 1, 32);
  3915. thumbH_word1 = 0;
  3916. PUTBITSHIGH( thumbH_word1, part0, 7, 62);
  3917. PUTBITSHIGH( thumbH_word1, part1, 2, 52);
  3918. PUTBITSHIGH( thumbH_word1, part2, 16, 49);
  3919. PUTBITSHIGH( thumbH_word1, part3, 1, 32);
  3920. // Make sure that red does not overflow:
  3921. bit = GETBITSHIGH( thumbH_word1, 1, 62);
  3922. PUTBITSHIGH( thumbH_word1, !bit, 1, 63);
  3923. // Make sure that green overflows:
  3924. a = GETBITSHIGH( thumbH_word1, 1, 52);
  3925. b = GETBITSHIGH( thumbH_word1, 1, 51);
  3926. c = GETBITSHIGH( thumbH_word1, 1, 49);
  3927. d = GETBITSHIGH( thumbH_word1, 1, 48);
  3928. // The following bit abcd bit sequences should be padded with ones: 0111, 1010, 1011, 1101, 1110, 1111
  3929. // The following logical expression checks for the presence of any of those:
  3930. bit = (a & c) | (!a & b & c & d) | (a & b & !c & d);
  3931. bits = 0xf*bit;
  3932. PUTBITSHIGH( thumbH_word1, bits, 3, 55);
  3933. PUTBITSHIGH( thumbH_word1, !bit, 1, 50);
  3934. // Set diffbit
  3935. PUTBITSHIGH( thumbH_word1, 1, 1, 33);
  3936. thumbH_word2 = thumbH58_word2;
  3937. }
  3938. // copy of above, but diffbit is 0
  3939. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3940. void stuff58bitsDiffFalse(unsigned int thumbH58_word1, unsigned int thumbH58_word2, unsigned int &thumbH_word1, unsigned int &thumbH_word2)
  3941. {
  3942. unsigned int part0, part1, part2, part3;
  3943. uint8 bit, a, b, c, d, bits;
  3944. // move parts
  3945. part0 = GETBITSHIGH( thumbH58_word1, 7, 57);
  3946. part1 = GETBITSHIGH( thumbH58_word1, 2, 50);
  3947. part2 = GETBITSHIGH( thumbH58_word1,16, 48);
  3948. part3 = GETBITSHIGH( thumbH58_word1, 1, 32);
  3949. thumbH_word1 = 0;
  3950. PUTBITSHIGH( thumbH_word1, part0, 7, 62);
  3951. PUTBITSHIGH( thumbH_word1, part1, 2, 52);
  3952. PUTBITSHIGH( thumbH_word1, part2, 16, 49);
  3953. PUTBITSHIGH( thumbH_word1, part3, 1, 32);
  3954. // Make sure that red does not overflow:
  3955. bit = GETBITSHIGH( thumbH_word1, 1, 62);
  3956. PUTBITSHIGH( thumbH_word1, !bit, 1, 63);
  3957. // Make sure that green overflows:
  3958. a = GETBITSHIGH( thumbH_word1, 1, 52);
  3959. b = GETBITSHIGH( thumbH_word1, 1, 51);
  3960. c = GETBITSHIGH( thumbH_word1, 1, 49);
  3961. d = GETBITSHIGH( thumbH_word1, 1, 48);
  3962. // The following bit abcd bit sequences should be padded with ones: 0111, 1010, 1011, 1101, 1110, 1111
  3963. // The following logical expression checks for the presence of any of those:
  3964. bit = (a & c) | (!a & b & c & d) | (a & b & !c & d);
  3965. bits = 0xf*bit;
  3966. PUTBITSHIGH( thumbH_word1, bits, 3, 55);
  3967. PUTBITSHIGH( thumbH_word1, !bit, 1, 50);
  3968. // Set diffbit
  3969. PUTBITSHIGH( thumbH_word1, 0, 1, 33);
  3970. thumbH_word2 = thumbH58_word2;
  3971. }
  3972. // During search it is not convenient to store the bits the way they are stored in the
  3973. // file format. Hence, after search, it is converted to this format.
  3974. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  3975. void stuff59bits(unsigned int thumbT59_word1, unsigned int thumbT59_word2, unsigned int &thumbT_word1, unsigned int &thumbT_word2)
  3976. {
  3977. // Put bits in twotimer configuration for 59 (red overflows)
  3978. //
  3979. // Go from this bit layout:
  3980. //
  3981. // |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|
  3982. // |----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  3983. //
  3984. // |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|
  3985. // |----------------------------------------index bits---------------------------------------------|
  3986. //
  3987. //
  3988. // To this:
  3989. //
  3990. // 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
  3991. // -----------------------------------------------------------------------------------------------
  3992. // |// // //|R0a |//|R0b |G0 |B0 |R1 |G1 |B1 |da |df|db|
  3993. // -----------------------------------------------------------------------------------------------
  3994. //
  3995. // |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|
  3996. // |----------------------------------------index bits---------------------------------------------|
  3997. //
  3998. // 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
  3999. // -----------------------------------------------------------------------------------------------
  4000. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |df|fp|
  4001. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bt|bt|
  4002. // ------------------------------------------------------------------------------------------------
  4003. uint8 R0a;
  4004. uint8 bit, a, b, c, d, bits;
  4005. R0a = GETBITSHIGH( thumbT59_word1, 2, 58);
  4006. // Fix middle part
  4007. thumbT_word1 = thumbT59_word1 << 1;
  4008. // Fix R0a (top two bits of R0)
  4009. PUTBITSHIGH( thumbT_word1, R0a, 2, 60);
  4010. // Fix db (lowest bit of d)
  4011. PUTBITSHIGH( thumbT_word1, thumbT59_word1, 1, 32);
  4012. //
  4013. // Make sure that red overflows:
  4014. a = GETBITSHIGH( thumbT_word1, 1, 60);
  4015. b = GETBITSHIGH( thumbT_word1, 1, 59);
  4016. c = GETBITSHIGH( thumbT_word1, 1, 57);
  4017. d = GETBITSHIGH( thumbT_word1, 1, 56);
  4018. // The following bit abcd bit sequences should be padded with ones: 0111, 1010, 1011, 1101, 1110, 1111
  4019. // The following logical expression checks for the presence of any of those:
  4020. bit = (a & c) | (!a & b & c & d) | (a & b & !c & d);
  4021. bits = 0xf*bit;
  4022. PUTBITSHIGH( thumbT_word1, bits, 3, 63);
  4023. PUTBITSHIGH( thumbT_word1, !bit, 1, 58);
  4024. // Set diffbit
  4025. PUTBITSHIGH( thumbT_word1, 1, 1, 33);
  4026. thumbT_word2 = thumbT59_word2;
  4027. }
  4028. // Decompress the planar mode and calculate the error per component compared to original image.
  4029. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  4030. 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)
  4031. {
  4032. uint8 colorO[3], colorH[3], colorV[3];
  4033. colorO[0] = GETBITSHIGH( compressed57_1, 6, 63);
  4034. colorO[1] = GETBITSHIGH( compressed57_1, 7, 57);
  4035. colorO[2] = GETBITSHIGH( compressed57_1, 6, 50);
  4036. colorH[0] = GETBITSHIGH( compressed57_1, 6, 44);
  4037. colorH[1] = GETBITSHIGH( compressed57_1, 7, 38);
  4038. colorH[2] = GETBITS( compressed57_2, 6, 31);
  4039. colorV[0] = GETBITS( compressed57_2, 6, 25);
  4040. colorV[1] = GETBITS( compressed57_2, 7, 19);
  4041. colorV[2] = GETBITS( compressed57_2, 6, 12);
  4042. colorO[0] = (colorO[0] << 2) | (colorO[0] >> 4);
  4043. colorO[1] = (colorO[1] << 1) | (colorO[1] >> 6);
  4044. colorO[2] = (colorO[2] << 2) | (colorO[2] >> 4);
  4045. colorH[0] = (colorH[0] << 2) | (colorH[0] >> 4);
  4046. colorH[1] = (colorH[1] << 1) | (colorH[1] >> 6);
  4047. colorH[2] = (colorH[2] << 2) | (colorH[2] >> 4);
  4048. colorV[0] = (colorV[0] << 2) | (colorV[0] >> 4);
  4049. colorV[1] = (colorV[1] << 1) | (colorV[1] >> 6);
  4050. colorV[2] = (colorV[2] << 2) | (colorV[2] >> 4);
  4051. int xx, yy;
  4052. for( xx=0; xx<4; xx++)
  4053. {
  4054. for( yy=0; yy<4; yy++)
  4055. {
  4056. 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);
  4057. 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);
  4058. 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);
  4059. }
  4060. }
  4061. error_red = 0;
  4062. error_green= 0;
  4063. error_blue = 0;
  4064. for( xx=0; xx<4; xx++)
  4065. {
  4066. for( yy=0; yy<4; yy++)
  4067. {
  4068. error_red = error_red + SQUARE(srcimg[3*width*(starty+yy) + 3*(startx+xx) + 0] - img[3*width*(starty+yy) + 3*(startx+xx) + 0]);
  4069. error_green = error_green + SQUARE(srcimg[3*width*(starty+yy) + 3*(startx+xx) + 1] - img[3*width*(starty+yy) + 3*(startx+xx) + 1]);
  4070. error_blue = error_blue + SQUARE(srcimg[3*width*(starty+yy) + 3*(startx+xx) + 2] - img[3*width*(starty+yy) + 3*(startx+xx) + 2]);
  4071. }
  4072. }
  4073. }
  4074. // Compress using both individual and differential mode in ETC1/ETC2 using combined color
  4075. // quantization. Both flip modes are tried.
  4076. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  4077. void compressBlockDiffFlipCombined(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  4078. {
  4079. unsigned int compressed1_norm, compressed2_norm;
  4080. unsigned int compressed1_flip, compressed2_flip;
  4081. uint8 avg_color_quant1[3], avg_color_quant2[3];
  4082. float avg_color_float1[3],avg_color_float2[3];
  4083. int enc_color1[3], enc_color2[3], diff[3];
  4084. int min_error=255*255*8*3;
  4085. unsigned int best_table_indices1=0, best_table_indices2=0;
  4086. unsigned int best_table1=0, best_table2=0;
  4087. int diffbit;
  4088. int norm_err=0;
  4089. int flip_err=0;
  4090. // First try normal blocks 2x4:
  4091. computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  4092. computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
  4093. // First test if avg_color1 is similar enough to avg_color2 so that
  4094. // we can use differential coding of colors.
  4095. float eps;
  4096. uint8 dummy[3];
  4097. quantize555ColorCombined(avg_color_float1, enc_color1, dummy);
  4098. quantize555ColorCombined(avg_color_float2, enc_color2, dummy);
  4099. diff[0] = enc_color2[0]-enc_color1[0];
  4100. diff[1] = enc_color2[1]-enc_color1[1];
  4101. diff[2] = enc_color2[2]-enc_color1[2];
  4102. if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
  4103. {
  4104. diffbit = 1;
  4105. // The difference to be coded:
  4106. diff[0] = enc_color2[0]-enc_color1[0];
  4107. diff[1] = enc_color2[1]-enc_color1[1];
  4108. diff[2] = enc_color2[2]-enc_color1[2];
  4109. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  4110. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  4111. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  4112. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  4113. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  4114. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  4115. // Pack bits into the first word.
  4116. // ETC1_RGB8_OES:
  4117. //
  4118. // a) bit layout in bits 63 through 32 if diffbit = 0
  4119. //
  4120. // 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
  4121. // ---------------------------------------------------------------------------------------------------
  4122. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  4123. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  4124. // ---------------------------------------------------------------------------------------------------
  4125. //
  4126. // b) bit layout in bits 63 through 32 if diffbit = 1
  4127. //
  4128. // 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
  4129. // ---------------------------------------------------------------------------------------------------
  4130. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  4131. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  4132. // ---------------------------------------------------------------------------------------------------
  4133. //
  4134. // c) bit layout in bits 31 through 0 (in both cases)
  4135. //
  4136. // 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
  4137. // --------------------------------------------------------------------------------------------------
  4138. // | most significant pixel index bits | least significant pixel index bits |
  4139. // | 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 |
  4140. // --------------------------------------------------------------------------------------------------
  4141. compressed1_norm = 0;
  4142. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  4143. PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
  4144. PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
  4145. PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
  4146. PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
  4147. PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
  4148. PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
  4149. unsigned int best_pixel_indices1_MSB;
  4150. unsigned int best_pixel_indices1_LSB;
  4151. unsigned int best_pixel_indices2_MSB;
  4152. unsigned int best_pixel_indices2_LSB;
  4153. norm_err = 0;
  4154. // left part of block
  4155. norm_err = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  4156. // right part of block
  4157. norm_err += tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  4158. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  4159. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  4160. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  4161. compressed2_norm = 0;
  4162. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  4163. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  4164. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  4165. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  4166. }
  4167. else
  4168. {
  4169. diffbit = 0;
  4170. // The difference is bigger than what fits in 555 plus delta-333, so we will have
  4171. // to deal with 444 444.
  4172. eps = (float) 0.0001;
  4173. uint8 dummy[3];
  4174. quantize444ColorCombined(avg_color_float1, enc_color1, dummy);
  4175. quantize444ColorCombined(avg_color_float2, enc_color2, dummy);
  4176. avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
  4177. avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
  4178. avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
  4179. avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
  4180. avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
  4181. avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
  4182. // Pack bits into the first word.
  4183. // 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
  4184. // ---------------------------------------------------------------------------------------------------
  4185. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  4186. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  4187. // ---------------------------------------------------------------------------------------------------
  4188. compressed1_norm = 0;
  4189. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  4190. PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
  4191. PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
  4192. PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
  4193. PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
  4194. PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
  4195. PUTBITSHIGH( compressed1_norm, enc_color2[2], 4, 43);
  4196. unsigned int best_pixel_indices1_MSB;
  4197. unsigned int best_pixel_indices1_LSB;
  4198. unsigned int best_pixel_indices2_MSB;
  4199. unsigned int best_pixel_indices2_LSB;
  4200. // left part of block
  4201. norm_err = tryalltables_3bittable2x4(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  4202. // right part of block
  4203. norm_err += tryalltables_3bittable2x4(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  4204. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  4205. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  4206. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  4207. compressed2_norm = 0;
  4208. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  4209. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  4210. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  4211. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  4212. }
  4213. // Now try flipped blocks 4x2:
  4214. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  4215. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
  4216. // First test if avg_color1 is similar enough to avg_color2 so that
  4217. // we can use differential coding of colors.
  4218. quantize555ColorCombined(avg_color_float1, enc_color1, dummy);
  4219. quantize555ColorCombined(avg_color_float2, enc_color2, dummy);
  4220. diff[0] = enc_color2[0]-enc_color1[0];
  4221. diff[1] = enc_color2[1]-enc_color1[1];
  4222. diff[2] = enc_color2[2]-enc_color1[2];
  4223. if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
  4224. {
  4225. diffbit = 1;
  4226. // The difference to be coded:
  4227. diff[0] = enc_color2[0]-enc_color1[0];
  4228. diff[1] = enc_color2[1]-enc_color1[1];
  4229. diff[2] = enc_color2[2]-enc_color1[2];
  4230. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  4231. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  4232. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  4233. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  4234. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  4235. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  4236. // Pack bits into the first word.
  4237. compressed1_flip = 0;
  4238. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  4239. PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
  4240. PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
  4241. PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
  4242. PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
  4243. PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
  4244. PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
  4245. unsigned int best_pixel_indices1_MSB;
  4246. unsigned int best_pixel_indices1_LSB;
  4247. unsigned int best_pixel_indices2_MSB;
  4248. unsigned int best_pixel_indices2_LSB;
  4249. // upper part of block
  4250. flip_err = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  4251. // lower part of block
  4252. flip_err += tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  4253. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  4254. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  4255. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  4256. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  4257. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  4258. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  4259. }
  4260. else
  4261. {
  4262. diffbit = 0;
  4263. // The difference is bigger than what fits in 555 plus delta-333, so we will have
  4264. // to deal with 444 444.
  4265. eps = (float) 0.0001;
  4266. uint8 dummy[3];
  4267. quantize444ColorCombined(avg_color_float1, enc_color1, dummy);
  4268. quantize444ColorCombined(avg_color_float2, enc_color2, dummy);
  4269. avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
  4270. avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
  4271. avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
  4272. avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
  4273. avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
  4274. avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
  4275. // 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
  4276. // ---------------------------------------------------------------------------------------------------
  4277. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  4278. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  4279. // ---------------------------------------------------------------------------------------------------
  4280. // Pack bits into the first word.
  4281. compressed1_flip = 0;
  4282. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  4283. PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
  4284. PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
  4285. PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
  4286. PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 59);
  4287. PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
  4288. PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
  4289. unsigned int best_pixel_indices1_MSB;
  4290. unsigned int best_pixel_indices1_LSB;
  4291. unsigned int best_pixel_indices2_MSB;
  4292. unsigned int best_pixel_indices2_LSB;
  4293. // upper part of block
  4294. flip_err = tryalltables_3bittable4x2(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  4295. // lower part of block
  4296. flip_err += tryalltables_3bittable4x2(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  4297. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  4298. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  4299. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  4300. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  4301. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  4302. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  4303. }
  4304. // Now lets see which is the best table to use. Only 8 tables are possible.
  4305. if(norm_err <= flip_err)
  4306. {
  4307. compressed1 = compressed1_norm | 0;
  4308. compressed2 = compressed2_norm;
  4309. }
  4310. else
  4311. {
  4312. compressed1 = compressed1_flip | 1;
  4313. compressed2 = compressed2_flip;
  4314. }
  4315. }
  4316. // Calculation of the two block colors using the LBG-algorithm
  4317. // The following method scales down the intensity, since this can be compensated for anyway by both the H and T mode.
  4318. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  4319. void computeColorLBGHalfIntensityFast(uint8 *img,int width,int startx,int starty, uint8 (LBG_colors)[2][3])
  4320. {
  4321. uint8 block_mask[4][4];
  4322. // reset rand so that we get predictable output per block
  4323. srand(10000);
  4324. //LBG-algorithm
  4325. double D = 0, oldD, bestD = MAXIMUM_ERROR, eps = 0.0000000001;
  4326. double error_a, error_b;
  4327. int number_of_iterations = 10;
  4328. double t_color[2][3];
  4329. double original_colors[4][4][3];
  4330. double current_colors[2][3];
  4331. double best_colors[2][3];
  4332. double max_v[3];
  4333. double min_v[3];
  4334. int x,y,i;
  4335. double red, green, blue;
  4336. bool continue_seeding;
  4337. int maximum_number_of_seedings = 10;
  4338. int seeding;
  4339. bool continue_iterate;
  4340. max_v[R] = -512.0; max_v[G] = -512.0; max_v[B] = -512.0;
  4341. min_v[R] = 512.0; min_v[G] = 512.0; min_v[B] = 512.0;
  4342. // resolve trainingdata
  4343. for (y = 0; y < BLOCKHEIGHT; ++y)
  4344. {
  4345. for (x = 0; x < BLOCKWIDTH; ++x)
  4346. {
  4347. red = img[3*((starty+y)*width+startx+x)+R];
  4348. green = img[3*((starty+y)*width+startx+x)+G];
  4349. blue = img[3*((starty+y)*width+startx+x)+B];
  4350. // Use qrs representation instead of rgb
  4351. // 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);
  4352. // rgb = inv(Q)*qrs = Q' * qrs where ' denotes transpose.
  4353. // The q variable holds intensity. r and s hold chrominance.
  4354. // q = [0, sqrt(3)*255], r = [-255/sqrt(2), 255/sqrt(2)], s = [-2*255/sqrt(6), 2*255/sqrt(6)];
  4355. //
  4356. // The LGB algorithm will only act on the r and s variables and not on q.
  4357. //
  4358. 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;
  4359. original_colors[x][y][G] = (1.0/sqrt(1.0*2))*red - (1.0/sqrt(1.0*2))*green;
  4360. 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;
  4361. // find max
  4362. if (original_colors[x][y][R] > max_v[R]) max_v[R] = original_colors[x][y][R];
  4363. if (original_colors[x][y][G] > max_v[G]) max_v[G] = original_colors[x][y][G];
  4364. if (original_colors[x][y][B] > max_v[B]) max_v[B] = original_colors[x][y][B];
  4365. // find min
  4366. if (original_colors[x][y][R] < min_v[R]) min_v[R] = original_colors[x][y][R];
  4367. if (original_colors[x][y][G] < min_v[G]) min_v[G] = original_colors[x][y][G];
  4368. if (original_colors[x][y][B] < min_v[B]) min_v[B] = original_colors[x][y][B];
  4369. }
  4370. }
  4371. D = 512*512*3*16.0;
  4372. bestD = 512*512*3*16.0;
  4373. continue_seeding = true;
  4374. // loop seeds
  4375. for (seeding = 0; (seeding < maximum_number_of_seedings) && continue_seeding; seeding++)
  4376. {
  4377. // hopefully we will not need more seedings:
  4378. continue_seeding = false;
  4379. // calculate seeds
  4380. for (uint8 s = 0; s < 2; ++s)
  4381. {
  4382. for (uint8 c = 0; c < 3; ++c)
  4383. {
  4384. current_colors[s][c] = double((double(rand())/RAND_MAX)*(max_v[c]-min_v[c])) + min_v[c];
  4385. }
  4386. }
  4387. // divide into two quantization sets and calculate distortion
  4388. continue_iterate = true;
  4389. for(i = 0; (i < number_of_iterations) && continue_iterate; i++)
  4390. {
  4391. oldD = D;
  4392. D = 0;
  4393. int n = 0;
  4394. for (y = 0; y < BLOCKHEIGHT; ++y)
  4395. {
  4396. for (int x = 0; x < BLOCKWIDTH; ++x)
  4397. {
  4398. error_a = 0.5*SQUARE(original_colors[x][y][R] - current_colors[0][R]) +
  4399. SQUARE(original_colors[x][y][G] - current_colors[0][G]) +
  4400. SQUARE(original_colors[x][y][B] - current_colors[0][B]);
  4401. error_b = 0.5*SQUARE(original_colors[x][y][R] - current_colors[1][R]) +
  4402. SQUARE(original_colors[x][y][G] - current_colors[1][G]) +
  4403. SQUARE(original_colors[x][y][B] - current_colors[1][B]);
  4404. if (error_a < error_b)
  4405. {
  4406. block_mask[x][y] = 0;
  4407. D += error_a;
  4408. ++n;
  4409. }
  4410. else
  4411. {
  4412. block_mask[x][y] = 1;
  4413. D += error_b;
  4414. }
  4415. }
  4416. }
  4417. // compare with old distortion
  4418. if (D == 0)
  4419. {
  4420. // Perfect score -- we dont need to go further iterations.
  4421. continue_iterate = false;
  4422. continue_seeding = false;
  4423. }
  4424. if (D == oldD)
  4425. {
  4426. // Same score as last round -- no need to go for further iterations.
  4427. continue_iterate = false;
  4428. continue_seeding = false;
  4429. }
  4430. if (D < bestD)
  4431. {
  4432. bestD = D;
  4433. for(uint8 s = 0; s < 2; ++s)
  4434. {
  4435. for(uint8 c = 0; c < 3; ++c)
  4436. {
  4437. best_colors[s][c] = current_colors[s][c];
  4438. }
  4439. }
  4440. }
  4441. if (n == 0 || n == BLOCKWIDTH*BLOCKHEIGHT)
  4442. {
  4443. // All colors end up in the same voroni region. We need to reseed.
  4444. continue_iterate = false;
  4445. continue_seeding = true;
  4446. }
  4447. else
  4448. {
  4449. // Calculate new reconstruction points using the centroids
  4450. // Find new construction values from average
  4451. t_color[0][R] = 0;
  4452. t_color[0][G] = 0;
  4453. t_color[0][B] = 0;
  4454. t_color[1][R] = 0;
  4455. t_color[1][G] = 0;
  4456. t_color[1][B] = 0;
  4457. for (y = 0; y < BLOCKHEIGHT; ++y)
  4458. {
  4459. for (int x = 0; x < BLOCKWIDTH; ++x)
  4460. {
  4461. // use dummy value for q-parameter
  4462. t_color[block_mask[x][y]][R] += original_colors[x][y][R];
  4463. t_color[block_mask[x][y]][G] += original_colors[x][y][G];
  4464. t_color[block_mask[x][y]][B] += original_colors[x][y][B];
  4465. }
  4466. }
  4467. current_colors[0][R] = t_color[0][R] / n;
  4468. current_colors[1][R] = t_color[1][R] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4469. current_colors[0][G] = t_color[0][G] / n;
  4470. current_colors[1][G] = t_color[1][G] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4471. current_colors[0][B] = t_color[0][B] / n;
  4472. current_colors[1][B] = t_color[1][B] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4473. }
  4474. }
  4475. }
  4476. for(x=0;x<2;x++)
  4477. {
  4478. double qq, rr, ss;
  4479. qq = best_colors[x][0];
  4480. rr = best_colors[x][1];
  4481. ss = best_colors[x][2];
  4482. 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);
  4483. 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);
  4484. current_colors[x][2] = CLAMP(0, (1.0/sqrt(1.0*3))*qq + (0.0 )*rr - (2.0/sqrt(1.0*6))*ss, 255);
  4485. }
  4486. for(x=0;x<2;x++)
  4487. for(y=0;y<3;y++)
  4488. LBG_colors[x][y] = JAS_ROUND(current_colors[x][y]);
  4489. }
  4490. // Calculation of the two block colors using the LBG-algorithm
  4491. // The following method scales down the intensity, since this can be compensated for anyway by both the H and T mode.
  4492. // Faster version
  4493. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  4494. void computeColorLBGNotIntensityFast(uint8 *img,int width,int startx,int starty, uint8 (LBG_colors)[2][3])
  4495. {
  4496. uint8 block_mask[4][4];
  4497. // reset rand so that we get predictable output per block
  4498. srand(10000);
  4499. //LBG-algorithm
  4500. double D = 0, oldD, bestD = MAXIMUM_ERROR, eps = 0.0000000001;
  4501. double error_a, error_b;
  4502. int number_of_iterations = 10;
  4503. double t_color[2][3];
  4504. double original_colors[4][4][3];
  4505. double current_colors[2][3];
  4506. double best_colors[2][3];
  4507. double max_v[3];
  4508. double min_v[3];
  4509. int x,y,i;
  4510. double red, green, blue;
  4511. bool continue_seeding;
  4512. int maximum_number_of_seedings = 10;
  4513. int seeding;
  4514. bool continue_iterate;
  4515. max_v[R] = -512.0; max_v[G] = -512.0; max_v[B] = -512.0;
  4516. min_v[R] = 512.0; min_v[G] = 512.0; min_v[B] = 512.0;
  4517. // resolve trainingdata
  4518. for (y = 0; y < BLOCKHEIGHT; ++y)
  4519. {
  4520. for (x = 0; x < BLOCKWIDTH; ++x)
  4521. {
  4522. red = img[3*((starty+y)*width+startx+x)+R];
  4523. green = img[3*((starty+y)*width+startx+x)+G];
  4524. blue = img[3*((starty+y)*width+startx+x)+B];
  4525. // Use qrs representation instead of rgb
  4526. // 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);
  4527. // rgb = inv(Q)*qrs = Q' * qrs where ' denotes transpose.
  4528. // The q variable holds intensity. r and s hold chrominance.
  4529. // 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)];
  4530. //
  4531. // The LGB algorithm will only act on the r and s variables and not on q.
  4532. //
  4533. 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;
  4534. original_colors[x][y][G] = (1.0/sqrt(1.0*2))*red - (1.0/sqrt(1.0*2))*green;
  4535. 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;
  4536. // find max
  4537. if (original_colors[x][y][R] > max_v[R]) max_v[R] = original_colors[x][y][R];
  4538. if (original_colors[x][y][G] > max_v[G]) max_v[G] = original_colors[x][y][G];
  4539. if (original_colors[x][y][B] > max_v[B]) max_v[B] = original_colors[x][y][B];
  4540. // find min
  4541. if (original_colors[x][y][R] < min_v[R]) min_v[R] = original_colors[x][y][R];
  4542. if (original_colors[x][y][G] < min_v[G]) min_v[G] = original_colors[x][y][G];
  4543. if (original_colors[x][y][B] < min_v[B]) min_v[B] = original_colors[x][y][B];
  4544. }
  4545. }
  4546. D = 512*512*3*16.0;
  4547. bestD = 512*512*3*16.0;
  4548. continue_seeding = true;
  4549. // loop seeds
  4550. for (seeding = 0; (seeding < maximum_number_of_seedings) && continue_seeding; seeding++)
  4551. {
  4552. // hopefully we will not need more seedings:
  4553. continue_seeding = false;
  4554. // calculate seeds
  4555. for (uint8 s = 0; s < 2; ++s)
  4556. {
  4557. for (uint8 c = 0; c < 3; ++c)
  4558. {
  4559. current_colors[s][c] = double((double(rand())/RAND_MAX)*(max_v[c]-min_v[c])) + min_v[c];
  4560. }
  4561. }
  4562. // divide into two quantization sets and calculate distortion
  4563. continue_iterate = true;
  4564. for(i = 0; (i < number_of_iterations) && continue_iterate; i++)
  4565. {
  4566. oldD = D;
  4567. D = 0;
  4568. int n = 0;
  4569. for (y = 0; y < BLOCKHEIGHT; ++y)
  4570. {
  4571. for (int x = 0; x < BLOCKWIDTH; ++x)
  4572. {
  4573. error_a = 0.0*SQUARE(original_colors[x][y][R] - current_colors[0][R]) +
  4574. SQUARE(original_colors[x][y][G] - current_colors[0][G]) +
  4575. SQUARE(original_colors[x][y][B] - current_colors[0][B]);
  4576. error_b = 0.0*SQUARE(original_colors[x][y][R] - current_colors[1][R]) +
  4577. SQUARE(original_colors[x][y][G] - current_colors[1][G]) +
  4578. SQUARE(original_colors[x][y][B] - current_colors[1][B]);
  4579. if (error_a < error_b)
  4580. {
  4581. block_mask[x][y] = 0;
  4582. D += error_a;
  4583. ++n;
  4584. }
  4585. else
  4586. {
  4587. block_mask[x][y] = 1;
  4588. D += error_b;
  4589. }
  4590. }
  4591. }
  4592. // compare with old distortion
  4593. if (D == 0)
  4594. {
  4595. // Perfect score -- we dont need to go further iterations.
  4596. continue_iterate = false;
  4597. continue_seeding = false;
  4598. }
  4599. if (D == oldD)
  4600. {
  4601. // Same score as last round -- no need to go for further iterations.
  4602. continue_iterate = false;
  4603. continue_seeding = false;
  4604. }
  4605. if (D < bestD)
  4606. {
  4607. bestD = D;
  4608. for(uint8 s = 0; s < 2; ++s)
  4609. {
  4610. for(uint8 c = 0; c < 3; ++c)
  4611. {
  4612. best_colors[s][c] = current_colors[s][c];
  4613. }
  4614. }
  4615. }
  4616. if (n == 0 || n == BLOCKWIDTH*BLOCKHEIGHT)
  4617. {
  4618. // All colors end up in the same voroni region. We need to reseed.
  4619. continue_iterate = false;
  4620. continue_seeding = true;
  4621. }
  4622. else
  4623. {
  4624. // Calculate new reconstruction points using the centroids
  4625. // Find new construction values from average
  4626. t_color[0][R] = 0;
  4627. t_color[0][G] = 0;
  4628. t_color[0][B] = 0;
  4629. t_color[1][R] = 0;
  4630. t_color[1][G] = 0;
  4631. t_color[1][B] = 0;
  4632. for (y = 0; y < BLOCKHEIGHT; ++y)
  4633. {
  4634. for (int x = 0; x < BLOCKWIDTH; ++x)
  4635. {
  4636. // use dummy value for q-parameter
  4637. t_color[block_mask[x][y]][R] += original_colors[x][y][R];
  4638. t_color[block_mask[x][y]][G] += original_colors[x][y][G];
  4639. t_color[block_mask[x][y]][B] += original_colors[x][y][B];
  4640. }
  4641. }
  4642. current_colors[0][R] = t_color[0][R] / n;
  4643. current_colors[1][R] = t_color[1][R] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4644. current_colors[0][G] = t_color[0][G] / n;
  4645. current_colors[1][G] = t_color[1][G] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4646. current_colors[0][B] = t_color[0][B] / n;
  4647. current_colors[1][B] = t_color[1][B] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4648. }
  4649. }
  4650. }
  4651. for(x=0;x<2;x++)
  4652. {
  4653. double qq, rr, ss;
  4654. qq = best_colors[x][0];
  4655. rr = best_colors[x][1];
  4656. ss = best_colors[x][2];
  4657. 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);
  4658. 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);
  4659. current_colors[x][2] = CLAMP(0, (1.0/sqrt(1.0*3))*qq + (0.0 )*rr - (2.0/sqrt(1.0*6))*ss, 255);
  4660. }
  4661. for(x=0;x<2;x++)
  4662. for(y=0;y<3;y++)
  4663. LBG_colors[x][y] = JAS_ROUND(current_colors[x][y]);
  4664. }
  4665. // Calculation of the two block colors using the LBG-algorithm
  4666. // The following method completely ignores the intensity, since this can be compensated for anyway by both the H and T mode.
  4667. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  4668. void computeColorLBGNotIntensity(uint8 *img,int width,int startx,int starty, uint8 (LBG_colors)[2][3])
  4669. {
  4670. uint8 block_mask[4][4];
  4671. // reset rand so that we get predictable output per block
  4672. srand(10000);
  4673. //LBG-algorithm
  4674. double D = 0, oldD, bestD = MAXIMUM_ERROR, eps = 0.0000000001;
  4675. double error_a, error_b;
  4676. int number_of_iterations = 10;
  4677. double t_color[2][3];
  4678. double original_colors[4][4][3];
  4679. double current_colors[2][3];
  4680. double best_colors[2][3];
  4681. double max_v[3];
  4682. double min_v[3];
  4683. int x,y,i;
  4684. double red, green, blue;
  4685. bool continue_seeding;
  4686. int maximum_number_of_seedings = 10;
  4687. int seeding;
  4688. bool continue_iterate;
  4689. max_v[R] = -512.0; max_v[G] = -512.0; max_v[B] = -512.0;
  4690. min_v[R] = 512.0; min_v[G] = 512.0; min_v[B] = 512.0;
  4691. // resolve trainingdata
  4692. for (y = 0; y < BLOCKHEIGHT; ++y)
  4693. {
  4694. for (x = 0; x < BLOCKWIDTH; ++x)
  4695. {
  4696. red = img[3*((starty+y)*width+startx+x)+R];
  4697. green = img[3*((starty+y)*width+startx+x)+G];
  4698. blue = img[3*((starty+y)*width+startx+x)+B];
  4699. // Use qrs representation instead of rgb
  4700. // 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);
  4701. // rgb = inv(Q)*qrs = Q' * qrs where ' denotes transpose.
  4702. // The q variable holds intensity. r and s hold chrominance.
  4703. // 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)];
  4704. //
  4705. // The LGB algorithm will only act on the r and s variables and not on q.
  4706. //
  4707. 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;
  4708. original_colors[x][y][G] = (1.0/sqrt(1.0*2))*red - (1.0/sqrt(1.0*2))*green;
  4709. 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;
  4710. // find max
  4711. if (original_colors[x][y][R] > max_v[R]) max_v[R] = original_colors[x][y][R];
  4712. if (original_colors[x][y][G] > max_v[G]) max_v[G] = original_colors[x][y][G];
  4713. if (original_colors[x][y][B] > max_v[B]) max_v[B] = original_colors[x][y][B];
  4714. // find min
  4715. if (original_colors[x][y][R] < min_v[R]) min_v[R] = original_colors[x][y][R];
  4716. if (original_colors[x][y][G] < min_v[G]) min_v[G] = original_colors[x][y][G];
  4717. if (original_colors[x][y][B] < min_v[B]) min_v[B] = original_colors[x][y][B];
  4718. }
  4719. }
  4720. D = 512*512*3*16.0;
  4721. bestD = 512*512*3*16.0;
  4722. continue_seeding = true;
  4723. // loop seeds
  4724. for (seeding = 0; (seeding < maximum_number_of_seedings) && continue_seeding; seeding++)
  4725. {
  4726. // hopefully we will not need more seedings:
  4727. continue_seeding = false;
  4728. // calculate seeds
  4729. for (uint8 s = 0; s < 2; ++s)
  4730. {
  4731. for (uint8 c = 0; c < 3; ++c)
  4732. {
  4733. current_colors[s][c] = double((double(rand())/RAND_MAX)*(max_v[c]-min_v[c])) + min_v[c];
  4734. }
  4735. }
  4736. // divide into two quantization sets and calculate distortion
  4737. continue_iterate = true;
  4738. for(i = 0; (i < number_of_iterations) && continue_iterate; i++)
  4739. {
  4740. oldD = D;
  4741. D = 0;
  4742. int n = 0;
  4743. for (y = 0; y < BLOCKHEIGHT; ++y)
  4744. {
  4745. for (int x = 0; x < BLOCKWIDTH; ++x)
  4746. {
  4747. error_a = 0.0*SQUARE(original_colors[x][y][R] - current_colors[0][R]) +
  4748. SQUARE(original_colors[x][y][G] - current_colors[0][G]) +
  4749. SQUARE(original_colors[x][y][B] - current_colors[0][B]);
  4750. error_b = 0.0*SQUARE(original_colors[x][y][R] - current_colors[1][R]) +
  4751. SQUARE(original_colors[x][y][G] - current_colors[1][G]) +
  4752. SQUARE(original_colors[x][y][B] - current_colors[1][B]);
  4753. if (error_a < error_b)
  4754. {
  4755. block_mask[x][y] = 0;
  4756. D += error_a;
  4757. ++n;
  4758. }
  4759. else
  4760. {
  4761. block_mask[x][y] = 1;
  4762. D += error_b;
  4763. }
  4764. }
  4765. }
  4766. // compare with old distortion
  4767. if (D == 0)
  4768. {
  4769. // Perfect score -- we dont need to go further iterations.
  4770. continue_iterate = false;
  4771. continue_seeding = false;
  4772. }
  4773. if (D == oldD)
  4774. {
  4775. // Same score as last round -- no need to go for further iterations.
  4776. continue_iterate = false;
  4777. continue_seeding = true;
  4778. }
  4779. if (D < bestD)
  4780. {
  4781. bestD = D;
  4782. for(uint8 s = 0; s < 2; ++s)
  4783. {
  4784. for(uint8 c = 0; c < 3; ++c)
  4785. {
  4786. best_colors[s][c] = current_colors[s][c];
  4787. }
  4788. }
  4789. }
  4790. if (n == 0 || n == BLOCKWIDTH*BLOCKHEIGHT)
  4791. {
  4792. // All colors end up in the same voroni region. We need to reseed.
  4793. continue_iterate = false;
  4794. continue_seeding = true;
  4795. }
  4796. else
  4797. {
  4798. // Calculate new reconstruction points using the centroids
  4799. // Find new construction values from average
  4800. t_color[0][R] = 0;
  4801. t_color[0][G] = 0;
  4802. t_color[0][B] = 0;
  4803. t_color[1][R] = 0;
  4804. t_color[1][G] = 0;
  4805. t_color[1][B] = 0;
  4806. for (y = 0; y < BLOCKHEIGHT; ++y)
  4807. {
  4808. for (int x = 0; x < BLOCKWIDTH; ++x)
  4809. {
  4810. // use dummy value for q-parameter
  4811. t_color[block_mask[x][y]][R] += original_colors[x][y][R];
  4812. t_color[block_mask[x][y]][G] += original_colors[x][y][G];
  4813. t_color[block_mask[x][y]][B] += original_colors[x][y][B];
  4814. }
  4815. }
  4816. current_colors[0][R] = t_color[0][R] / n;
  4817. current_colors[1][R] = t_color[1][R] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4818. current_colors[0][G] = t_color[0][G] / n;
  4819. current_colors[1][G] = t_color[1][G] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4820. current_colors[0][B] = t_color[0][B] / n;
  4821. current_colors[1][B] = t_color[1][B] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4822. }
  4823. }
  4824. }
  4825. for(x=0;x<2;x++)
  4826. {
  4827. double qq, rr, ss;
  4828. qq = best_colors[x][0];
  4829. rr = best_colors[x][1];
  4830. ss = best_colors[x][2];
  4831. 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);
  4832. 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);
  4833. current_colors[x][2] = CLAMP(0, (1.0/sqrt(1.0*3))*qq + (0.0 )*rr - (2.0/sqrt(1.0*6))*ss, 255);
  4834. }
  4835. for(x=0;x<2;x++)
  4836. for(y=0;y<3;y++)
  4837. LBG_colors[x][y] = JAS_ROUND(current_colors[x][y]);
  4838. }
  4839. // Calculation of the two block colors using the LBG-algorithm
  4840. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  4841. void computeColorLBG(uint8 *img,int width,int startx,int starty, uint8 (LBG_colors)[2][3])
  4842. {
  4843. uint8 block_mask[4][4];
  4844. // reset rand so that we get predictable output per block
  4845. srand(10000);
  4846. //LBG-algorithm
  4847. double D = 0, oldD, bestD = MAXIMUM_ERROR, eps = 0.0000000001;
  4848. double error_a, error_b;
  4849. int number_of_iterations = 10;
  4850. double t_color[2][3];
  4851. double original_colors[4][4][3];
  4852. double current_colors[2][3];
  4853. double best_colors[2][3];
  4854. double max_v[3];
  4855. double min_v[3];
  4856. int x,y,i;
  4857. double red, green, blue;
  4858. bool continue_seeding;
  4859. int maximum_number_of_seedings = 10;
  4860. int seeding;
  4861. bool continue_iterate;
  4862. max_v[R] = -512.0; max_v[G] = -512.0; max_v[B] = -512.0;
  4863. min_v[R] = 512.0; min_v[G] = 512.0; min_v[B] = 512.0;
  4864. // resolve trainingdata
  4865. for (y = 0; y < BLOCKHEIGHT; ++y)
  4866. {
  4867. for (x = 0; x < BLOCKWIDTH; ++x)
  4868. {
  4869. red = img[3*((starty+y)*width+startx+x)+R];
  4870. green = img[3*((starty+y)*width+startx+x)+G];
  4871. blue = img[3*((starty+y)*width+startx+x)+B];
  4872. original_colors[x][y][R] = red;
  4873. original_colors[x][y][G] = green;
  4874. original_colors[x][y][B] = blue;
  4875. // find max
  4876. if (original_colors[x][y][R] > max_v[R]) max_v[R] = original_colors[x][y][R];
  4877. if (original_colors[x][y][G] > max_v[G]) max_v[G] = original_colors[x][y][G];
  4878. if (original_colors[x][y][B] > max_v[B]) max_v[B] = original_colors[x][y][B];
  4879. // find min
  4880. if (original_colors[x][y][R] < min_v[R]) min_v[R] = original_colors[x][y][R];
  4881. if (original_colors[x][y][G] < min_v[G]) min_v[G] = original_colors[x][y][G];
  4882. if (original_colors[x][y][B] < min_v[B]) min_v[B] = original_colors[x][y][B];
  4883. }
  4884. }
  4885. D = 512*512*3*16.0;
  4886. bestD = 512*512*3*16.0;
  4887. continue_seeding = true;
  4888. // loop seeds
  4889. for (seeding = 0; (seeding < maximum_number_of_seedings) && continue_seeding; seeding++)
  4890. {
  4891. // hopefully we will not need more seedings:
  4892. continue_seeding = false;
  4893. // calculate seeds
  4894. for (uint8 s = 0; s < 2; ++s)
  4895. {
  4896. for (uint8 c = 0; c < 3; ++c)
  4897. {
  4898. current_colors[s][c] = double((double(rand())/RAND_MAX)*(max_v[c]-min_v[c])) + min_v[c];
  4899. }
  4900. }
  4901. // divide into two quantization sets and calculate distortion
  4902. continue_iterate = true;
  4903. for(i = 0; (i < number_of_iterations) && continue_iterate; i++)
  4904. {
  4905. oldD = D;
  4906. D = 0;
  4907. int n = 0;
  4908. for (y = 0; y < BLOCKHEIGHT; ++y)
  4909. {
  4910. for (int x = 0; x < BLOCKWIDTH; ++x)
  4911. {
  4912. error_a = SQUARE(original_colors[x][y][R] - JAS_ROUND(current_colors[0][R])) +
  4913. SQUARE(original_colors[x][y][G] - JAS_ROUND(current_colors[0][G])) +
  4914. SQUARE(original_colors[x][y][B] - JAS_ROUND(current_colors[0][B]));
  4915. error_b = SQUARE(original_colors[x][y][R] - JAS_ROUND(current_colors[1][R])) +
  4916. SQUARE(original_colors[x][y][G] - JAS_ROUND(current_colors[1][G])) +
  4917. SQUARE(original_colors[x][y][B] - JAS_ROUND(current_colors[1][B]));
  4918. if (error_a < error_b)
  4919. {
  4920. block_mask[x][y] = 0;
  4921. D += error_a;
  4922. ++n;
  4923. }
  4924. else
  4925. {
  4926. block_mask[x][y] = 1;
  4927. D += error_b;
  4928. }
  4929. }
  4930. }
  4931. // compare with old distortion
  4932. if (D == 0)
  4933. {
  4934. // Perfect score -- we dont need to go further iterations.
  4935. continue_iterate = false;
  4936. continue_seeding = false;
  4937. }
  4938. if (D == oldD)
  4939. {
  4940. // Same score as last round -- no need to go for further iterations.
  4941. continue_iterate = false;
  4942. continue_seeding = true;
  4943. }
  4944. if (D < bestD)
  4945. {
  4946. bestD = D;
  4947. for(uint8 s = 0; s < 2; ++s)
  4948. {
  4949. for(uint8 c = 0; c < 3; ++c)
  4950. {
  4951. best_colors[s][c] = current_colors[s][c];
  4952. }
  4953. }
  4954. }
  4955. if (n == 0 || n == BLOCKWIDTH*BLOCKHEIGHT)
  4956. {
  4957. // All colors end up in the same voroni region. We need to reseed.
  4958. continue_iterate = false;
  4959. continue_seeding = true;
  4960. }
  4961. else
  4962. {
  4963. // Calculate new reconstruction points using the centroids
  4964. // Find new construction values from average
  4965. t_color[0][R] = 0;
  4966. t_color[0][G] = 0;
  4967. t_color[0][B] = 0;
  4968. t_color[1][R] = 0;
  4969. t_color[1][G] = 0;
  4970. t_color[1][B] = 0;
  4971. for (y = 0; y < BLOCKHEIGHT; ++y)
  4972. {
  4973. for (int x = 0; x < BLOCKWIDTH; ++x)
  4974. {
  4975. // use dummy value for q-parameter
  4976. t_color[block_mask[x][y]][R] += original_colors[x][y][R];
  4977. t_color[block_mask[x][y]][G] += original_colors[x][y][G];
  4978. t_color[block_mask[x][y]][B] += original_colors[x][y][B];
  4979. }
  4980. }
  4981. current_colors[0][R] = t_color[0][R] / n;
  4982. current_colors[1][R] = t_color[1][R] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4983. current_colors[0][G] = t_color[0][G] / n;
  4984. current_colors[1][G] = t_color[1][G] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4985. current_colors[0][B] = t_color[0][B] / n;
  4986. current_colors[1][B] = t_color[1][B] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  4987. }
  4988. }
  4989. }
  4990. // Set the best colors as the final block colors
  4991. for(int s = 0; s < 2; ++s)
  4992. {
  4993. for(uint8 c = 0; c < 3; ++c)
  4994. {
  4995. current_colors[s][c] = best_colors[s][c];
  4996. }
  4997. }
  4998. for(x=0;x<2;x++)
  4999. for(y=0;y<3;y++)
  5000. LBG_colors[x][y] = JAS_ROUND(current_colors[x][y]);
  5001. }
  5002. // Calculation of the two block colors using the LBG-algorithm
  5003. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5004. void computeColorLBGfast(uint8 *img,int width,int startx,int starty, uint8 (LBG_colors)[2][3])
  5005. {
  5006. uint8 block_mask[4][4];
  5007. // reset rand so that we get predictable output per block
  5008. srand(10000);
  5009. //LBG-algorithm
  5010. double D = 0, oldD, bestD = MAXIMUM_ERROR, eps = 0.0000000001;
  5011. double error_a, error_b;
  5012. int number_of_iterations = 10;
  5013. double t_color[2][3];
  5014. uint8 original_colors[4][4][3];
  5015. double current_colors[2][3];
  5016. double best_colors[2][3];
  5017. double max_v[3];
  5018. double min_v[3];
  5019. int x,y,i;
  5020. bool continue_seeding;
  5021. int maximum_number_of_seedings = 10;
  5022. int seeding;
  5023. bool continue_iterate;
  5024. max_v[R] = -512.0; max_v[G] = -512.0; max_v[B] = -512.0;
  5025. min_v[R] = 512.0; min_v[G] = 512.0; min_v[B] = 512.0;
  5026. // resolve trainingdata
  5027. for (y = 0; y < BLOCKHEIGHT; ++y)
  5028. {
  5029. for (x = 0; x < BLOCKWIDTH; ++x)
  5030. {
  5031. original_colors[x][y][R] = img[3*((starty+y)*width+startx+x)+R];
  5032. original_colors[x][y][G] = img[3*((starty+y)*width+startx+x)+G];
  5033. original_colors[x][y][B] = img[3*((starty+y)*width+startx+x)+B];
  5034. // find max
  5035. if (original_colors[x][y][R] > max_v[R]) max_v[R] = original_colors[x][y][R];
  5036. if (original_colors[x][y][G] > max_v[G]) max_v[G] = original_colors[x][y][G];
  5037. if (original_colors[x][y][B] > max_v[B]) max_v[B] = original_colors[x][y][B];
  5038. // find min
  5039. if (original_colors[x][y][R] < min_v[R]) min_v[R] = original_colors[x][y][R];
  5040. if (original_colors[x][y][G] < min_v[G]) min_v[G] = original_colors[x][y][G];
  5041. if (original_colors[x][y][B] < min_v[B]) min_v[B] = original_colors[x][y][B];
  5042. }
  5043. }
  5044. D = 512*512*3*16.0;
  5045. bestD = 512*512*3*16.0;
  5046. continue_seeding = true;
  5047. // loop seeds
  5048. for (seeding = 0; (seeding < maximum_number_of_seedings) && continue_seeding; seeding++)
  5049. {
  5050. // hopefully we will not need more seedings:
  5051. continue_seeding = false;
  5052. // calculate seeds
  5053. for (uint8 s = 0; s < 2; ++s)
  5054. {
  5055. for (uint8 c = 0; c < 3; ++c)
  5056. {
  5057. current_colors[s][c] = double((double(rand())/RAND_MAX)*(max_v[c]-min_v[c])) + min_v[c];
  5058. }
  5059. }
  5060. // divide into two quantization sets and calculate distortion
  5061. continue_iterate = true;
  5062. for(i = 0; (i < number_of_iterations) && continue_iterate; i++)
  5063. {
  5064. oldD = D;
  5065. D = 0;
  5066. int n = 0;
  5067. for (y = 0; y < BLOCKHEIGHT; ++y)
  5068. {
  5069. for (int x = 0; x < BLOCKWIDTH; ++x)
  5070. {
  5071. error_a = SQUARE(original_colors[x][y][R] - JAS_ROUND(current_colors[0][R])) +
  5072. SQUARE(original_colors[x][y][G] - JAS_ROUND(current_colors[0][G])) +
  5073. SQUARE(original_colors[x][y][B] - JAS_ROUND(current_colors[0][B]));
  5074. error_b = SQUARE(original_colors[x][y][R] - JAS_ROUND(current_colors[1][R])) +
  5075. SQUARE(original_colors[x][y][G] - JAS_ROUND(current_colors[1][G])) +
  5076. SQUARE(original_colors[x][y][B] - JAS_ROUND(current_colors[1][B]));
  5077. if (error_a < error_b)
  5078. {
  5079. block_mask[x][y] = 0;
  5080. D += error_a;
  5081. ++n;
  5082. }
  5083. else
  5084. {
  5085. block_mask[x][y] = 1;
  5086. D += error_b;
  5087. }
  5088. }
  5089. }
  5090. // compare with old distortion
  5091. if (D == 0)
  5092. {
  5093. // Perfect score -- we dont need to go further iterations.
  5094. continue_iterate = false;
  5095. continue_seeding = false;
  5096. }
  5097. if (D == oldD)
  5098. {
  5099. // Same score as last round -- no need to go for further iterations.
  5100. continue_iterate = false;
  5101. continue_seeding = false;
  5102. }
  5103. if (D < bestD)
  5104. {
  5105. bestD = D;
  5106. for(uint8 s = 0; s < 2; ++s)
  5107. {
  5108. for(uint8 c = 0; c < 3; ++c)
  5109. {
  5110. best_colors[s][c] = current_colors[s][c];
  5111. }
  5112. }
  5113. }
  5114. if (n == 0 || n == BLOCKWIDTH*BLOCKHEIGHT)
  5115. {
  5116. // All colors end up in the same voroni region. We need to reseed.
  5117. continue_iterate = false;
  5118. continue_seeding = true;
  5119. }
  5120. else
  5121. {
  5122. // Calculate new reconstruction points using the centroids
  5123. // Find new construction values from average
  5124. t_color[0][R] = 0;
  5125. t_color[0][G] = 0;
  5126. t_color[0][B] = 0;
  5127. t_color[1][R] = 0;
  5128. t_color[1][G] = 0;
  5129. t_color[1][B] = 0;
  5130. for (y = 0; y < BLOCKHEIGHT; ++y)
  5131. {
  5132. for (int x = 0; x < BLOCKWIDTH; ++x)
  5133. {
  5134. // use dummy value for q-parameter
  5135. t_color[block_mask[x][y]][R] += original_colors[x][y][R];
  5136. t_color[block_mask[x][y]][G] += original_colors[x][y][G];
  5137. t_color[block_mask[x][y]][B] += original_colors[x][y][B];
  5138. }
  5139. }
  5140. current_colors[0][R] = t_color[0][R] / n;
  5141. current_colors[1][R] = t_color[1][R] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  5142. current_colors[0][G] = t_color[0][G] / n;
  5143. current_colors[1][G] = t_color[1][G] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  5144. current_colors[0][B] = t_color[0][B] / n;
  5145. current_colors[1][B] = t_color[1][B] / (BLOCKWIDTH*BLOCKHEIGHT - n);
  5146. }
  5147. }
  5148. }
  5149. // Set the best colors as the final block colors
  5150. for(int s = 0; s < 2; ++s)
  5151. {
  5152. for(uint8 c = 0; c < 3; ++c)
  5153. {
  5154. current_colors[s][c] = best_colors[s][c];
  5155. }
  5156. }
  5157. for(x=0;x<2;x++)
  5158. for(y=0;y<3;y++)
  5159. LBG_colors[x][y] = JAS_ROUND(current_colors[x][y]);
  5160. }
  5161. // Each color component is compressed to fit in its specified number of bits
  5162. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5163. void compressColor(int R_B, int G_B, int B_B, uint8 (current_color)[2][3], uint8 (quantized_color)[2][3])
  5164. {
  5165. //
  5166. // The color is calculated as:
  5167. //
  5168. // c = (c + (2^(8-b))/2) / (255 / (2^b - 1)) where b is the number of bits
  5169. // to code color c with
  5170. // For instance, if b = 3:
  5171. //
  5172. // c = (c + 16) / (255 / 7) = 7 * (c + 16) / 255
  5173. //
  5174. quantized_color[0][R] = CLAMP(0,(BINPOW(R_B)-1) * (current_color[0][R] + BINPOW(8-R_B-1)) / 255,255);
  5175. quantized_color[0][G] = CLAMP(0,(BINPOW(G_B)-1) * (current_color[0][G] + BINPOW(8-G_B-1)) / 255,255);
  5176. quantized_color[0][B] = CLAMP(0,(BINPOW(B_B)-1) * (current_color[0][B] + BINPOW(8-B_B-1)) / 255,255);
  5177. quantized_color[1][R] = CLAMP(0,(BINPOW(R_B)-1) * (current_color[1][R] + BINPOW(8-R_B-1)) / 255,255);
  5178. quantized_color[1][G] = CLAMP(0,(BINPOW(G_B)-1) * (current_color[1][G] + BINPOW(8-G_B-1)) / 255,255);
  5179. quantized_color[1][B] = CLAMP(0,(BINPOW(B_B)-1) * (current_color[1][B] + BINPOW(8-B_B-1)) / 255,255);
  5180. }
  5181. // Swapping two RGB-colors
  5182. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5183. void swapColors(uint8 (colors)[2][3])
  5184. {
  5185. uint8 temp = colors[0][R];
  5186. colors[0][R] = colors[1][R];
  5187. colors[1][R] = temp;
  5188. temp = colors[0][G];
  5189. colors[0][G] = colors[1][G];
  5190. colors[1][G] = temp;
  5191. temp = colors[0][B];
  5192. colors[0][B] = colors[1][B];
  5193. colors[1][B] = temp;
  5194. }
  5195. // Calculate the paint colors from the block colors
  5196. // using a distance d and one of the H- or T-patterns.
  5197. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5198. // Calculate the error for the block at position (startx,starty)
  5199. // The parameters needed for reconstruction are calculated as well
  5200. //
  5201. // Please note that the function can change the order between the two colors in colorsRGB444
  5202. //
  5203. // In the 59T bit mode, we only have pattern T.
  5204. //
  5205. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5206. unsigned int calculateError59Tperceptual1000(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
  5207. {
  5208. unsigned int block_error = 0,
  5209. best_block_error = MAXERR1000,
  5210. pixel_error,
  5211. best_pixel_error;
  5212. int diff[3];
  5213. uint8 best_sw;
  5214. unsigned int pixel_colors;
  5215. uint8 colors[2][3];
  5216. uint8 possible_colors[4][3];
  5217. // First use the colors as they are, then swap them
  5218. for (uint8 sw = 0; sw <2; ++sw)
  5219. {
  5220. if (sw == 1)
  5221. {
  5222. swapColors(colorsRGB444);
  5223. }
  5224. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  5225. // Test all distances
  5226. for (uint8 d = 0; d < BINPOW(TABLE_BITS_59T); ++d)
  5227. {
  5228. calculatePaintColors59T(d,PATTERN_T, colors, possible_colors);
  5229. block_error = 0;
  5230. pixel_colors = 0;
  5231. // Loop block
  5232. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  5233. {
  5234. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  5235. {
  5236. best_pixel_error = MAXERR1000;
  5237. pixel_colors <<=2; // Make room for next value
  5238. // Loop possible block colors
  5239. for (uint8 c = 0; c < 4; ++c)
  5240. {
  5241. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  5242. diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
  5243. diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
  5244. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) +
  5245. PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]) +
  5246. PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*SQUARE(diff[B]);
  5247. // Choose best error
  5248. if (pixel_error < best_pixel_error)
  5249. {
  5250. best_pixel_error = pixel_error;
  5251. pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
  5252. pixel_colors |= c;
  5253. }
  5254. }
  5255. block_error += best_pixel_error;
  5256. }
  5257. }
  5258. if (block_error < best_block_error)
  5259. {
  5260. best_block_error = block_error;
  5261. distance = d;
  5262. pixel_indices = pixel_colors;
  5263. best_sw = sw;
  5264. }
  5265. }
  5266. if (sw == 1 && best_sw == 0)
  5267. {
  5268. swapColors(colorsRGB444);
  5269. }
  5270. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  5271. }
  5272. return best_block_error;
  5273. }
  5274. // Calculate the error for the block at position (startx,starty)
  5275. // The parameters needed for reconstruction is calculated as well
  5276. //
  5277. // Please note that the function can change the order between the two colors in colorsRGB444
  5278. //
  5279. // In the 59T bit mode, we only have pattern T.
  5280. //
  5281. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5282. double calculateError59T(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
  5283. {
  5284. double block_error = 0,
  5285. best_block_error = MAXIMUM_ERROR,
  5286. pixel_error,
  5287. best_pixel_error;
  5288. int diff[3];
  5289. uint8 best_sw;
  5290. unsigned int pixel_colors;
  5291. uint8 colors[2][3];
  5292. uint8 possible_colors[4][3];
  5293. // First use the colors as they are, then swap them
  5294. for (uint8 sw = 0; sw <2; ++sw)
  5295. {
  5296. if (sw == 1)
  5297. {
  5298. swapColors(colorsRGB444);
  5299. }
  5300. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  5301. // Test all distances
  5302. for (uint8 d = 0; d < BINPOW(TABLE_BITS_59T); ++d)
  5303. {
  5304. calculatePaintColors59T(d,PATTERN_T, colors, possible_colors);
  5305. block_error = 0;
  5306. pixel_colors = 0;
  5307. // Loop block
  5308. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  5309. {
  5310. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  5311. {
  5312. best_pixel_error = MAXIMUM_ERROR;
  5313. pixel_colors <<=2; // Make room for next value
  5314. // Loop possible block colors
  5315. for (uint8 c = 0; c < 4; ++c)
  5316. {
  5317. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  5318. diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
  5319. diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
  5320. pixel_error = weight[R]*SQUARE(diff[R]) +
  5321. weight[G]*SQUARE(diff[G]) +
  5322. weight[B]*SQUARE(diff[B]);
  5323. // Choose best error
  5324. if (pixel_error < best_pixel_error)
  5325. {
  5326. best_pixel_error = pixel_error;
  5327. pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
  5328. pixel_colors |= c;
  5329. }
  5330. }
  5331. block_error += best_pixel_error;
  5332. }
  5333. }
  5334. if (block_error < best_block_error)
  5335. {
  5336. best_block_error = block_error;
  5337. distance = d;
  5338. pixel_indices = pixel_colors;
  5339. best_sw = sw;
  5340. }
  5341. }
  5342. if (sw == 1 && best_sw == 0)
  5343. {
  5344. swapColors(colorsRGB444);
  5345. }
  5346. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  5347. }
  5348. return best_block_error;
  5349. }
  5350. // Calculate the error for the block at position (startx,starty)
  5351. // The parameters needed for reconstruction is calculated as well
  5352. //
  5353. // In the 59T bit mode, we only have pattern T.
  5354. //
  5355. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5356. unsigned int calculateError59TnoSwapPerceptual1000(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
  5357. {
  5358. unsigned int block_error = 0,
  5359. best_block_error = MAXERR1000,
  5360. pixel_error,
  5361. best_pixel_error;
  5362. int diff[3];
  5363. unsigned int pixel_colors;
  5364. uint8 colors[2][3];
  5365. uint8 possible_colors[4][3];
  5366. int thebestintheworld;
  5367. // First use the colors as they are, then swap them
  5368. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  5369. // Test all distances
  5370. for (uint8 d = 0; d < BINPOW(TABLE_BITS_59T); ++d)
  5371. {
  5372. calculatePaintColors59T(d,PATTERN_T, colors, possible_colors);
  5373. block_error = 0;
  5374. pixel_colors = 0;
  5375. // Loop block
  5376. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  5377. {
  5378. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  5379. {
  5380. best_pixel_error = MAXERR1000;
  5381. pixel_colors <<=2; // Make room for next value
  5382. // Loop possible block colors
  5383. for (uint8 c = 0; c < 4; ++c)
  5384. {
  5385. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  5386. diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
  5387. diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
  5388. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) +
  5389. PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]) +
  5390. PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*SQUARE(diff[B]);
  5391. // Choose best error
  5392. if (pixel_error < best_pixel_error)
  5393. {
  5394. best_pixel_error = pixel_error;
  5395. pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
  5396. pixel_colors |= c;
  5397. thebestintheworld = c;
  5398. }
  5399. }
  5400. block_error += best_pixel_error;
  5401. }
  5402. }
  5403. if (block_error < best_block_error)
  5404. {
  5405. best_block_error = block_error;
  5406. distance = d;
  5407. pixel_indices = pixel_colors;
  5408. }
  5409. }
  5410. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  5411. return best_block_error;
  5412. }
  5413. // Calculate the error for the block at position (startx,starty)
  5414. // The parameters needed for reconstruction is calculated as well
  5415. //
  5416. // In the 59T bit mode, we only have pattern T.
  5417. //
  5418. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5419. double calculateError59TnoSwap(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
  5420. {
  5421. double block_error = 0,
  5422. best_block_error = MAXIMUM_ERROR,
  5423. pixel_error,
  5424. best_pixel_error;
  5425. int diff[3];
  5426. unsigned int pixel_colors;
  5427. uint8 colors[2][3];
  5428. uint8 possible_colors[4][3];
  5429. int thebestintheworld;
  5430. // First use the colors as they are, then swap them
  5431. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  5432. // Test all distances
  5433. for (uint8 d = 0; d < BINPOW(TABLE_BITS_59T); ++d)
  5434. {
  5435. calculatePaintColors59T(d,PATTERN_T, colors, possible_colors);
  5436. block_error = 0;
  5437. pixel_colors = 0;
  5438. // Loop block
  5439. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  5440. {
  5441. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  5442. {
  5443. best_pixel_error = MAXIMUM_ERROR;
  5444. pixel_colors <<=2; // Make room for next value
  5445. // Loop possible block colors
  5446. for (uint8 c = 0; c < 4; ++c)
  5447. {
  5448. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  5449. diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
  5450. diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
  5451. pixel_error = weight[R]*SQUARE(diff[R]) +
  5452. weight[G]*SQUARE(diff[G]) +
  5453. weight[B]*SQUARE(diff[B]);
  5454. // Choose best error
  5455. if (pixel_error < best_pixel_error)
  5456. {
  5457. best_pixel_error = pixel_error;
  5458. pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
  5459. pixel_colors |= c;
  5460. thebestintheworld = c;
  5461. }
  5462. }
  5463. block_error += best_pixel_error;
  5464. }
  5465. }
  5466. if (block_error < best_block_error)
  5467. {
  5468. best_block_error = block_error;
  5469. distance = d;
  5470. pixel_indices = pixel_colors;
  5471. }
  5472. }
  5473. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  5474. return best_block_error;
  5475. }
  5476. // Put the compress params into the compression block
  5477. //
  5478. //
  5479. //|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|
  5480. //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  5481. //
  5482. //|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|
  5483. //|----------------------------------------index bits---------------------------------------------|
  5484. //
  5485. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5486. void packBlock59T(uint8 (colors)[2][3], uint8 d, unsigned int pixel_indices, unsigned int &compressed1, unsigned int &compressed2)
  5487. {
  5488. compressed1 = 0;
  5489. PUTBITSHIGH( compressed1, colors[0][R], 4, 58);
  5490. PUTBITSHIGH( compressed1, colors[0][G], 4, 54);
  5491. PUTBITSHIGH( compressed1, colors[0][B], 4, 50);
  5492. PUTBITSHIGH( compressed1, colors[1][R], 4, 46);
  5493. PUTBITSHIGH( compressed1, colors[1][G], 4, 42);
  5494. PUTBITSHIGH( compressed1, colors[1][B], 4, 38);
  5495. PUTBITSHIGH( compressed1, d, TABLE_BITS_59T, 34);
  5496. pixel_indices=indexConversion(pixel_indices);
  5497. compressed2 = 0;
  5498. PUTBITS( compressed2, pixel_indices, 32, 31);
  5499. }
  5500. // Copy colors from source to dest
  5501. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5502. void copyColors(uint8 (source)[2][3], uint8 (dest)[2][3])
  5503. {
  5504. int x,y;
  5505. for (x=0; x<2; x++)
  5506. for (y=0; y<3; y++)
  5507. dest[x][y] = source[x][y];
  5508. }
  5509. // The below code should compress the block to 59 bits.
  5510. //
  5511. //|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|
  5512. //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  5513. //
  5514. //|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|
  5515. //|----------------------------------------index bits---------------------------------------------|
  5516. //
  5517. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5518. unsigned int compressBlockTHUMB59TFastestOnlyColorPerceptual1000(uint8 *img,int width,int height,int startx,int starty, int (best_colorsRGB444_packed)[2])
  5519. {
  5520. unsigned int best_error = MAXERR1000;
  5521. unsigned int best_pixel_indices;
  5522. uint8 best_distance;
  5523. unsigned int error_no_i;
  5524. uint8 colorsRGB444_no_i[2][3];
  5525. unsigned int pixel_indices_no_i;
  5526. uint8 distance_no_i;
  5527. uint8 colors[2][3];
  5528. // Calculate average color using the LBG-algorithm
  5529. computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
  5530. compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
  5531. // Determine the parameters for the lowest error
  5532. error_no_i = calculateError59Tperceptual1000(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
  5533. best_error = error_no_i;
  5534. best_distance = distance_no_i;
  5535. best_pixel_indices = pixel_indices_no_i;
  5536. best_colorsRGB444_packed[0] = (colorsRGB444_no_i[0][0] << 8) + (colorsRGB444_no_i[0][1] << 4) + (colorsRGB444_no_i[0][2] << 0);
  5537. best_colorsRGB444_packed[1] = (colorsRGB444_no_i[1][0] << 8) + (colorsRGB444_no_i[1][1] << 4) + (colorsRGB444_no_i[1][2] << 0);
  5538. return best_error;
  5539. }
  5540. // The below code should compress the block to 59 bits.
  5541. // This is supposed to match the first of the three modes in TWOTIMER.
  5542. //
  5543. //|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|
  5544. //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  5545. //
  5546. //|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|
  5547. //|----------------------------------------index bits---------------------------------------------|
  5548. //
  5549. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5550. double compressBlockTHUMB59TFastestOnlyColor(uint8 *img,int width,int height,int startx,int starty, int (best_colorsRGB444_packed)[2])
  5551. {
  5552. double best_error = MAXIMUM_ERROR;
  5553. unsigned int best_pixel_indices;
  5554. uint8 best_distance;
  5555. double error_no_i;
  5556. uint8 colorsRGB444_no_i[2][3];
  5557. unsigned int pixel_indices_no_i;
  5558. uint8 distance_no_i;
  5559. uint8 colors[2][3];
  5560. // Calculate average color using the LBG-algorithm
  5561. computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
  5562. compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
  5563. // Determine the parameters for the lowest error
  5564. error_no_i = calculateError59T(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
  5565. best_error = error_no_i;
  5566. best_distance = distance_no_i;
  5567. best_pixel_indices = pixel_indices_no_i;
  5568. best_colorsRGB444_packed[0] = (colorsRGB444_no_i[0][0] << 8) + (colorsRGB444_no_i[0][1] << 4) + (colorsRGB444_no_i[0][2] << 0);
  5569. best_colorsRGB444_packed[1] = (colorsRGB444_no_i[1][0] << 8) + (colorsRGB444_no_i[1][1] << 4) + (colorsRGB444_no_i[1][2] << 0);
  5570. return best_error;
  5571. }
  5572. // The below code should compress the block to 59 bits.
  5573. // This is supposed to match the first of the three modes in TWOTIMER.
  5574. //
  5575. //|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|
  5576. //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  5577. //
  5578. //|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|
  5579. //|----------------------------------------index bits---------------------------------------------|
  5580. //
  5581. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5582. double compressBlockTHUMB59TFastestPerceptual1000(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  5583. {
  5584. double best_error = MAXIMUM_ERROR;
  5585. uint8 best_colorsRGB444[2][3];
  5586. unsigned int best_pixel_indices;
  5587. uint8 best_distance;
  5588. double error_no_i;
  5589. uint8 colorsRGB444_no_i[2][3];
  5590. unsigned int pixel_indices_no_i;
  5591. uint8 distance_no_i;
  5592. uint8 colors[2][3];
  5593. // Calculate average color using the LBG-algorithm
  5594. computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
  5595. compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
  5596. // Determine the parameters for the lowest error
  5597. error_no_i = calculateError59Tperceptual1000(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
  5598. best_error = error_no_i;
  5599. best_distance = distance_no_i;
  5600. best_pixel_indices = pixel_indices_no_i;
  5601. copyColors(colorsRGB444_no_i, best_colorsRGB444);
  5602. // Put the compress params into the compression block
  5603. packBlock59T(best_colorsRGB444, best_distance, best_pixel_indices, compressed1, compressed2);
  5604. return best_error;
  5605. }
  5606. // The below code should compress the block to 59 bits.
  5607. // This is supposed to match the first of the three modes in TWOTIMER.
  5608. //
  5609. //|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|
  5610. //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  5611. //
  5612. //|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|
  5613. //|----------------------------------------index bits---------------------------------------------|
  5614. //
  5615. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5616. double compressBlockTHUMB59TFastest(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  5617. {
  5618. double best_error = MAXIMUM_ERROR;
  5619. uint8 best_colorsRGB444[2][3];
  5620. unsigned int best_pixel_indices;
  5621. uint8 best_distance;
  5622. double error_no_i;
  5623. uint8 colorsRGB444_no_i[2][3];
  5624. unsigned int pixel_indices_no_i;
  5625. uint8 distance_no_i;
  5626. uint8 colors[2][3];
  5627. // Calculate average color using the LBG-algorithm
  5628. computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
  5629. compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
  5630. // Determine the parameters for the lowest error
  5631. error_no_i = calculateError59T(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
  5632. best_error = error_no_i;
  5633. best_distance = distance_no_i;
  5634. best_pixel_indices = pixel_indices_no_i;
  5635. copyColors(colorsRGB444_no_i, best_colorsRGB444);
  5636. // Put the compress params into the compression block
  5637. packBlock59T(best_colorsRGB444, best_distance, best_pixel_indices, compressed1, compressed2);
  5638. return best_error;
  5639. }
  5640. // The below code should compress the block to 59 bits.
  5641. // This is supposed to match the first of the three modes in TWOTIMER.
  5642. //
  5643. //|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|
  5644. //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  5645. //
  5646. //|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|
  5647. //|----------------------------------------index bits---------------------------------------------|
  5648. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5649. double compressBlockTHUMB59TFast(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  5650. {
  5651. double best_error = MAXIMUM_ERROR;
  5652. uint8 best_colorsRGB444[2][3];
  5653. unsigned int best_pixel_indices;
  5654. uint8 best_distance;
  5655. double error_no_i;
  5656. uint8 colorsRGB444_no_i[2][3];
  5657. unsigned int pixel_indices_no_i;
  5658. uint8 distance_no_i;
  5659. double error_half_i;
  5660. uint8 colorsRGB444_half_i[2][3];
  5661. unsigned int pixel_indices_half_i;
  5662. uint8 distance_half_i;
  5663. double error;
  5664. uint8 colorsRGB444[2][3];
  5665. unsigned int pixel_indices;
  5666. uint8 distance;
  5667. uint8 colors[2][3];
  5668. // Calculate average color using the LBG-algorithm
  5669. computeColorLBGNotIntensityFast(img,width,startx,starty, colors);
  5670. compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
  5671. // Determine the parameters for the lowest error
  5672. error_no_i = calculateError59T(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
  5673. // Calculate average color using the LBG-algorithm
  5674. computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
  5675. compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_half_i);
  5676. // Determine the parameters for the lowest error
  5677. error_half_i = calculateError59T(img, width, startx, starty, colorsRGB444_half_i, distance_half_i, pixel_indices_half_i);
  5678. // Calculate average color using the LBG-algorithm
  5679. computeColorLBGfast(img,width,startx,starty, colors);
  5680. compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444);
  5681. // Determine the parameters for the lowest error
  5682. error = calculateError59T(img, width, startx, starty, colorsRGB444, distance, pixel_indices);
  5683. best_error = error_no_i;
  5684. best_distance = distance_no_i;
  5685. best_pixel_indices = pixel_indices_no_i;
  5686. copyColors(colorsRGB444_no_i, best_colorsRGB444);
  5687. if(error_half_i < best_error)
  5688. {
  5689. best_error = error_half_i;
  5690. best_distance = distance_half_i;
  5691. best_pixel_indices = pixel_indices_half_i;
  5692. copyColors (colorsRGB444_half_i, best_colorsRGB444);
  5693. }
  5694. if(error < best_error)
  5695. {
  5696. best_error = error;
  5697. best_distance = distance;
  5698. best_pixel_indices = pixel_indices;
  5699. copyColors (colorsRGB444, best_colorsRGB444);
  5700. }
  5701. // Put the compress params into the compression block
  5702. packBlock59T(best_colorsRGB444, best_distance, best_pixel_indices, compressed1, compressed2);
  5703. return best_error;
  5704. }
  5705. // Calculate the error for the block at position (startx,starty)
  5706. // The parameters needed for reconstruction is calculated as well
  5707. //
  5708. // In the 58H bit mode, we only have pattern H.
  5709. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5710. unsigned int calculateErrorAndCompress58Hperceptual1000(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
  5711. {
  5712. unsigned int block_error = 0,
  5713. best_block_error = MAXERR1000,
  5714. pixel_error,
  5715. best_pixel_error;
  5716. int diff[3];
  5717. unsigned int pixel_colors;
  5718. uint8 possible_colors[4][3];
  5719. uint8 colors[2][3];
  5720. decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
  5721. // Test all distances
  5722. for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
  5723. {
  5724. calculatePaintColors58H(d, PATTERN_H, colors, possible_colors);
  5725. block_error = 0;
  5726. pixel_colors = 0;
  5727. // Loop block
  5728. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  5729. {
  5730. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  5731. {
  5732. best_pixel_error = MAXERR1000;
  5733. pixel_colors <<=2; // Make room for next value
  5734. // Loop possible block colors
  5735. for (uint8 c = 0; c < 4; ++c)
  5736. {
  5737. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  5738. diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
  5739. diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
  5740. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) +
  5741. PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]) +
  5742. PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*SQUARE(diff[B]);
  5743. // Choose best error
  5744. if (pixel_error < best_pixel_error)
  5745. {
  5746. best_pixel_error = pixel_error;
  5747. pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
  5748. pixel_colors |= c;
  5749. }
  5750. }
  5751. block_error += best_pixel_error;
  5752. }
  5753. }
  5754. if (block_error < best_block_error)
  5755. {
  5756. best_block_error = block_error;
  5757. distance = d;
  5758. pixel_indices = pixel_colors;
  5759. }
  5760. }
  5761. return best_block_error;
  5762. }
  5763. // The H-mode but with punchthrough alpha
  5764. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5765. double calculateErrorAndCompress58HAlpha(uint8* srcimg, uint8* alphaimg,int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
  5766. {
  5767. double block_error = 0,
  5768. best_block_error = MAXIMUM_ERROR,
  5769. pixel_error,
  5770. best_pixel_error;
  5771. int diff[3];
  5772. unsigned int pixel_colors;
  5773. uint8 possible_colors[4][3];
  5774. uint8 colors[2][3];
  5775. int alphaindex;
  5776. int colorsRGB444_packed[2];
  5777. colorsRGB444_packed[0] = (colorsRGB444[0][R] << 8) + (colorsRGB444[0][G] << 4) + colorsRGB444[0][B];
  5778. colorsRGB444_packed[1] = (colorsRGB444[1][R] << 8) + (colorsRGB444[1][G] << 4) + colorsRGB444[1][B];
  5779. decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
  5780. // Test all distances
  5781. for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
  5782. {
  5783. alphaindex=2;
  5784. if( (colorsRGB444_packed[0] >= colorsRGB444_packed[1]) ^ ((d & 1)==1) )
  5785. {
  5786. //we're going to have to swap the colors to be able to choose this distance.. that means
  5787. //that the indices will be swapped as well, so C1 will be the one with alpha instead of C3..
  5788. alphaindex=0;
  5789. }
  5790. calculatePaintColors58H(d, PATTERN_H, colors, possible_colors);
  5791. block_error = 0;
  5792. pixel_colors = 0;
  5793. // Loop block
  5794. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  5795. {
  5796. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  5797. {
  5798. int alpha=0;
  5799. if(alphaimg[((starty+y)*width+startx+x)]>0)
  5800. alpha=1;
  5801. if(alphaimg[((starty+y)*width+startx+x)]>0&&alphaimg[((starty+y)*width+startx+x)]<255)
  5802. printf("INVALID ALPHA DATA!!\n");
  5803. best_pixel_error = MAXIMUM_ERROR;
  5804. pixel_colors <<=2; // Make room for next value
  5805. // Loop possible block colors
  5806. for (uint8 c = 0; c < 4; ++c)
  5807. {
  5808. if(c==alphaindex&&alpha)
  5809. {
  5810. pixel_error=0;
  5811. }
  5812. else if(c==alphaindex||alpha)
  5813. {
  5814. pixel_error=MAXIMUM_ERROR;
  5815. }
  5816. else
  5817. {
  5818. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  5819. diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
  5820. diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
  5821. pixel_error = weight[R]*SQUARE(diff[R]) +
  5822. weight[G]*SQUARE(diff[G]) +
  5823. weight[B]*SQUARE(diff[B]);
  5824. }
  5825. // Choose best error
  5826. if (pixel_error < best_pixel_error)
  5827. {
  5828. best_pixel_error = pixel_error;
  5829. pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
  5830. pixel_colors |= c;
  5831. }
  5832. }
  5833. block_error += best_pixel_error;
  5834. }
  5835. }
  5836. if (block_error < best_block_error)
  5837. {
  5838. best_block_error = block_error;
  5839. distance = d;
  5840. pixel_indices = pixel_colors;
  5841. }
  5842. }
  5843. return best_block_error;
  5844. }
  5845. // Calculate the error for the block at position (startx,starty)
  5846. // The parameters needed for reconstruction is calculated as well
  5847. //
  5848. // In the 58H bit mode, we only have pattern H.
  5849. //
  5850. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5851. double calculateErrorAndCompress58H(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
  5852. {
  5853. double block_error = 0,
  5854. best_block_error = MAXIMUM_ERROR,
  5855. pixel_error,
  5856. best_pixel_error;
  5857. int diff[3];
  5858. unsigned int pixel_colors;
  5859. uint8 possible_colors[4][3];
  5860. uint8 colors[2][3];
  5861. decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
  5862. // Test all distances
  5863. for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
  5864. {
  5865. calculatePaintColors58H(d, PATTERN_H, colors, possible_colors);
  5866. block_error = 0;
  5867. pixel_colors = 0;
  5868. // Loop block
  5869. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  5870. {
  5871. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  5872. {
  5873. best_pixel_error = MAXIMUM_ERROR;
  5874. pixel_colors <<=2; // Make room for next value
  5875. // Loop possible block colors
  5876. for (uint8 c = 0; c < 4; ++c)
  5877. {
  5878. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  5879. diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
  5880. diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
  5881. pixel_error = weight[R]*SQUARE(diff[R]) +
  5882. weight[G]*SQUARE(diff[G]) +
  5883. weight[B]*SQUARE(diff[B]);
  5884. // Choose best error
  5885. if (pixel_error < best_pixel_error)
  5886. {
  5887. best_pixel_error = pixel_error;
  5888. pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
  5889. pixel_colors |= c;
  5890. }
  5891. }
  5892. block_error += best_pixel_error;
  5893. }
  5894. }
  5895. if (block_error < best_block_error)
  5896. {
  5897. best_block_error = block_error;
  5898. distance = d;
  5899. pixel_indices = pixel_colors;
  5900. }
  5901. }
  5902. return best_block_error;
  5903. }
  5904. // Makes sure that col0 < col1;
  5905. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5906. void sortColorsRGB444(uint8 (colorsRGB444)[2][3])
  5907. {
  5908. unsigned int col0, col1, tcol;
  5909. // sort colors
  5910. col0 = 16*16*colorsRGB444[0][R] + 16*colorsRGB444[0][G] + colorsRGB444[0][B];
  5911. col1 = 16*16*colorsRGB444[1][R] + 16*colorsRGB444[1][G] + colorsRGB444[1][B];
  5912. // After this, col0 should be smaller than col1 (col0 < col1)
  5913. if( col0 > col1)
  5914. {
  5915. tcol = col0;
  5916. col0 = col1;
  5917. col1 = tcol;
  5918. }
  5919. else
  5920. {
  5921. if(col0 == col1)
  5922. {
  5923. // Both colors are the same. That is useless. If they are both black,
  5924. // col1 can just as well be (0,0,1). Else, col0 can be col1 - 1.
  5925. if(col0 == 0)
  5926. col1 = col0+1;
  5927. else
  5928. col0 = col1-1;
  5929. }
  5930. }
  5931. colorsRGB444[0][R] = GETBITS(col0, 4, 11);
  5932. colorsRGB444[0][G] = GETBITS(col0, 4, 7);
  5933. colorsRGB444[0][B] = GETBITS(col0, 4, 3);
  5934. colorsRGB444[1][R] = GETBITS(col1, 4, 11);
  5935. colorsRGB444[1][G] = GETBITS(col1, 4, 7);
  5936. colorsRGB444[1][B] = GETBITS(col1, 4, 3);
  5937. }
  5938. // The below code should compress the block to 58 bits.
  5939. // The bit layout is thought to be:
  5940. //
  5941. //|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|
  5942. //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
  5943. //
  5944. //|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|
  5945. //|----------------------------------------index bits---------------------------------------------|
  5946. //
  5947. // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
  5948. // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
  5949. // Else, it is assumed to be 1.
  5950. //
  5951. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  5952. unsigned int compressBlockTHUMB58HFastestPerceptual1000(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  5953. {
  5954. unsigned int best_error = MAXERR1000;
  5955. uint8 best_colorsRGB444[2][3];
  5956. unsigned int best_pixel_indices;
  5957. uint8 best_distance;
  5958. unsigned int error_no_i;
  5959. uint8 colorsRGB444_no_i[2][3];
  5960. unsigned int pixel_indices_no_i;
  5961. uint8 distance_no_i;
  5962. uint8 colors[2][3];
  5963. // Calculate average color using the LBG-algorithm but discarding the intensity in the error function
  5964. computeColorLBGHalfIntensityFast(img, width, startx, starty, colors);
  5965. compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444_no_i);
  5966. sortColorsRGB444(colorsRGB444_no_i);
  5967. error_no_i = calculateErrorAndCompress58Hperceptual1000(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
  5968. best_error = error_no_i;
  5969. best_distance = distance_no_i;
  5970. best_pixel_indices = pixel_indices_no_i;
  5971. copyColors(colorsRGB444_no_i, best_colorsRGB444);
  5972. // | col0 >= col1 col0 < col1
  5973. //------------------------------------------------------
  5974. // (dist & 1) = 1 | no need to swap | need to swap
  5975. // |-----------------+----------------
  5976. // (dist & 1) = 0 | need to swap | no need to swap
  5977. //
  5978. // This can be done with an xor test.
  5979. int best_colorsRGB444_packed[2];
  5980. best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
  5981. best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
  5982. if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
  5983. {
  5984. swapColors(best_colorsRGB444);
  5985. // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
  5986. best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
  5987. }
  5988. // Put the compress params into the compression block
  5989. compressed1 = 0;
  5990. PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
  5991. PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
  5992. PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
  5993. PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
  5994. PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
  5995. PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
  5996. PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
  5997. compressed2 = 0;
  5998. best_pixel_indices=indexConversion(best_pixel_indices);
  5999. PUTBITS( compressed2, best_pixel_indices, 32, 31);
  6000. return best_error;
  6001. }
  6002. // The below code should compress the block to 58 bits.
  6003. // This is supposed to match the first of the three modes in TWOTIMER.
  6004. // The bit layout is thought to be:
  6005. //
  6006. //|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|
  6007. //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
  6008. //
  6009. //|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|
  6010. //|----------------------------------------index bits---------------------------------------------|
  6011. //
  6012. // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
  6013. // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
  6014. // Else, it is assumed to be 1.
  6015. //
  6016. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6017. double compressBlockTHUMB58HFastest(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  6018. {
  6019. double best_error = MAXIMUM_ERROR;
  6020. uint8 best_colorsRGB444[2][3];
  6021. unsigned int best_pixel_indices;
  6022. uint8 best_distance;
  6023. double error_no_i;
  6024. uint8 colorsRGB444_no_i[2][3];
  6025. unsigned int pixel_indices_no_i;
  6026. uint8 distance_no_i;
  6027. uint8 colors[2][3];
  6028. // Calculate average color using the LBG-algorithm but discarding the intensity in the error function
  6029. computeColorLBGHalfIntensityFast(img, width, startx, starty, colors);
  6030. compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444_no_i);
  6031. sortColorsRGB444(colorsRGB444_no_i);
  6032. error_no_i = calculateErrorAndCompress58H(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
  6033. best_error = error_no_i;
  6034. best_distance = distance_no_i;
  6035. best_pixel_indices = pixel_indices_no_i;
  6036. copyColors(colorsRGB444_no_i, best_colorsRGB444);
  6037. // | col0 >= col1 col0 < col1
  6038. //------------------------------------------------------
  6039. // (dist & 1) = 1 | no need to swap | need to swap
  6040. // |-----------------+----------------
  6041. // (dist & 1) = 0 | need to swap | no need to swap
  6042. //
  6043. // This can be done with an xor test.
  6044. int best_colorsRGB444_packed[2];
  6045. best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
  6046. best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
  6047. if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
  6048. {
  6049. swapColors(best_colorsRGB444);
  6050. // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
  6051. best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
  6052. }
  6053. // Put the compress params into the compression block
  6054. compressed1 = 0;
  6055. PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
  6056. PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
  6057. PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
  6058. PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
  6059. PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
  6060. PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
  6061. PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
  6062. best_pixel_indices=indexConversion(best_pixel_indices);
  6063. compressed2 = 0;
  6064. PUTBITS( compressed2, best_pixel_indices, 32, 31);
  6065. return best_error;
  6066. }
  6067. //same as above, but with 1-bit alpha
  6068. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6069. double compressBlockTHUMB58HAlpha(uint8 *img, uint8* alphaimg, int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  6070. {
  6071. double best_error = MAXIMUM_ERROR;
  6072. uint8 best_colorsRGB444[2][3];
  6073. unsigned int best_pixel_indices;
  6074. uint8 best_distance;
  6075. double error_no_i;
  6076. uint8 colorsRGB444_no_i[2][3];
  6077. unsigned int pixel_indices_no_i;
  6078. uint8 distance_no_i;
  6079. uint8 colors[2][3];
  6080. // Calculate average color using the LBG-algorithm but discarding the intensity in the error function
  6081. computeColorLBGHalfIntensityFast(img, width, startx, starty, colors);
  6082. compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444_no_i);
  6083. sortColorsRGB444(colorsRGB444_no_i);
  6084. error_no_i = calculateErrorAndCompress58HAlpha(img, alphaimg,width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
  6085. best_error = error_no_i;
  6086. best_distance = distance_no_i;
  6087. best_pixel_indices = pixel_indices_no_i;
  6088. copyColors(colorsRGB444_no_i, best_colorsRGB444);
  6089. // | col0 >= col1 col0 < col1
  6090. //------------------------------------------------------
  6091. // (dist & 1) = 1 | no need to swap | need to swap
  6092. // |-----------------+----------------
  6093. // (dist & 1) = 0 | need to swap | no need to swap
  6094. //
  6095. // This can be done with an xor test.
  6096. int best_colorsRGB444_packed[2];
  6097. best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
  6098. best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
  6099. if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
  6100. {
  6101. swapColors(best_colorsRGB444);
  6102. // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
  6103. best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
  6104. }
  6105. // Put the compress params into the compression block
  6106. compressed1 = 0;
  6107. PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
  6108. PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
  6109. PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
  6110. PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
  6111. PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
  6112. PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
  6113. PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
  6114. best_pixel_indices=indexConversion(best_pixel_indices);
  6115. compressed2 = 0;
  6116. PUTBITS( compressed2, best_pixel_indices, 32, 31);
  6117. return best_error;
  6118. }
  6119. // The below code should compress the block to 58 bits.
  6120. // This is supposed to match the first of the three modes in TWOTIMER.
  6121. // The bit layout is thought to be:
  6122. //
  6123. //|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|
  6124. //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
  6125. //
  6126. //|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|
  6127. //|----------------------------------------index bits---------------------------------------------|
  6128. //
  6129. // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
  6130. // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
  6131. // Else, it is assumed to be 1.
  6132. //
  6133. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6134. double compressBlockTHUMB58HFast(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  6135. {
  6136. double best_error = MAXIMUM_ERROR;
  6137. uint8 best_colorsRGB444[2][3];
  6138. unsigned int best_pixel_indices;
  6139. uint8 best_distance;
  6140. double error_no_i;
  6141. uint8 colorsRGB444_no_i[2][3];
  6142. unsigned int pixel_indices_no_i;
  6143. uint8 distance_no_i;
  6144. double error_half_i;
  6145. uint8 colorsRGB444_half_i[2][3];
  6146. unsigned int pixel_indices_half_i;
  6147. uint8 distance_half_i;
  6148. double error;
  6149. uint8 colorsRGB444[2][3];
  6150. unsigned int pixel_indices;
  6151. uint8 distance;
  6152. uint8 colors[2][3];
  6153. // Calculate average color using the LBG-algorithm but discarding the intensity in the error function
  6154. computeColorLBGNotIntensity(img, width, startx, starty, colors);
  6155. compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444_no_i);
  6156. sortColorsRGB444(colorsRGB444_no_i);
  6157. error_no_i = calculateErrorAndCompress58H(img, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
  6158. // Calculate average color using the LBG-algorithm but halfing the influence of the intensity in the error function
  6159. computeColorLBGNotIntensity(img, width, startx, starty, colors);
  6160. compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444_half_i);
  6161. sortColorsRGB444(colorsRGB444_half_i);
  6162. error_half_i = calculateErrorAndCompress58H(img, width, startx, starty, colorsRGB444_half_i, distance_half_i, pixel_indices_half_i);
  6163. // Calculate average color using the LBG-algorithm
  6164. computeColorLBG(img, width, startx, starty, colors);
  6165. compressColor(R_BITS58H, G_BITS58H, B_BITS58H, colors, colorsRGB444);
  6166. sortColorsRGB444(colorsRGB444);
  6167. error = calculateErrorAndCompress58H(img, width, startx, starty, colorsRGB444, distance, pixel_indices);
  6168. best_error = error_no_i;
  6169. best_distance = distance_no_i;
  6170. best_pixel_indices = pixel_indices_no_i;
  6171. copyColors(colorsRGB444_no_i, best_colorsRGB444);
  6172. if(error_half_i < best_error)
  6173. {
  6174. best_error = error_half_i;
  6175. best_distance = distance_half_i;
  6176. best_pixel_indices = pixel_indices_half_i;
  6177. copyColors(colorsRGB444_half_i, best_colorsRGB444);
  6178. }
  6179. if(error < best_error)
  6180. {
  6181. best_error = error;
  6182. best_distance = distance;
  6183. best_pixel_indices = pixel_indices;
  6184. copyColors(colorsRGB444, best_colorsRGB444);
  6185. }
  6186. // | col0 >= col1 col0 < col1
  6187. //------------------------------------------------------
  6188. // (dist & 1) = 1 | no need to swap | need to swap
  6189. // |-----------------+----------------
  6190. // (dist & 1) = 0 | need to swap | no need to swap
  6191. //
  6192. // This can be done with an xor test.
  6193. int best_colorsRGB444_packed[2];
  6194. best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
  6195. best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
  6196. if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
  6197. {
  6198. swapColors(best_colorsRGB444);
  6199. // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
  6200. best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
  6201. }
  6202. // Put the compress params into the compression block
  6203. compressed1 = 0;
  6204. PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
  6205. PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
  6206. PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
  6207. PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
  6208. PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
  6209. PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
  6210. PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
  6211. best_pixel_indices=indexConversion(best_pixel_indices);
  6212. compressed2 = 0;
  6213. PUTBITS( compressed2, best_pixel_indices, 32, 31);
  6214. return best_error;
  6215. }
  6216. // Compress block testing both individual and differential mode.
  6217. // Perceptual error metric.
  6218. // Combined quantization for colors.
  6219. // Both flipped and unflipped tested.
  6220. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6221. void compressBlockDiffFlipCombinedPerceptual(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  6222. {
  6223. unsigned int compressed1_norm, compressed2_norm;
  6224. unsigned int compressed1_flip, compressed2_flip;
  6225. uint8 avg_color_quant1[3], avg_color_quant2[3];
  6226. float avg_color_float1[3],avg_color_float2[3];
  6227. int enc_color1[3], enc_color2[3], diff[3];
  6228. int min_error=255*255*8*3;
  6229. unsigned int best_table_indices1=0, best_table_indices2=0;
  6230. unsigned int best_table1=0, best_table2=0;
  6231. int diffbit;
  6232. int norm_err=0;
  6233. int flip_err=0;
  6234. // First try normal blocks 2x4:
  6235. computeAverageColor2x4noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  6236. computeAverageColor2x4noQuantFloat(img,width,height,startx+2,starty,avg_color_float2);
  6237. // First test if avg_color1 is similar enough to avg_color2 so that
  6238. // we can use differential coding of colors.
  6239. float eps;
  6240. uint8 dummy[3];
  6241. quantize555ColorCombinedPerceptual(avg_color_float1, enc_color1, dummy);
  6242. quantize555ColorCombinedPerceptual(avg_color_float2, enc_color2, dummy);
  6243. diff[0] = enc_color2[0]-enc_color1[0];
  6244. diff[1] = enc_color2[1]-enc_color1[1];
  6245. diff[2] = enc_color2[2]-enc_color1[2];
  6246. if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
  6247. {
  6248. diffbit = 1;
  6249. // The difference to be coded:
  6250. diff[0] = enc_color2[0]-enc_color1[0];
  6251. diff[1] = enc_color2[1]-enc_color1[1];
  6252. diff[2] = enc_color2[2]-enc_color1[2];
  6253. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  6254. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  6255. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  6256. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  6257. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  6258. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  6259. // Pack bits into the first word.
  6260. // ETC1_RGB8_OES:
  6261. //
  6262. // a) bit layout in bits 63 through 32 if diffbit = 0
  6263. //
  6264. // 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
  6265. // ---------------------------------------------------------------------------------------------------
  6266. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  6267. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  6268. // ---------------------------------------------------------------------------------------------------
  6269. //
  6270. // b) bit layout in bits 63 through 32 if diffbit = 1
  6271. //
  6272. // 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
  6273. // ---------------------------------------------------------------------------------------------------
  6274. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  6275. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  6276. // ---------------------------------------------------------------------------------------------------
  6277. //
  6278. // c) bit layout in bits 31 through 0 (in both cases)
  6279. //
  6280. // 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
  6281. // --------------------------------------------------------------------------------------------------
  6282. // | most significant pixel index bits | least significant pixel index bits |
  6283. // | 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 |
  6284. // --------------------------------------------------------------------------------------------------
  6285. compressed1_norm = 0;
  6286. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  6287. PUTBITSHIGH( compressed1_norm, enc_color1[0], 5, 63);
  6288. PUTBITSHIGH( compressed1_norm, enc_color1[1], 5, 55);
  6289. PUTBITSHIGH( compressed1_norm, enc_color1[2], 5, 47);
  6290. PUTBITSHIGH( compressed1_norm, diff[0], 3, 58);
  6291. PUTBITSHIGH( compressed1_norm, diff[1], 3, 50);
  6292. PUTBITSHIGH( compressed1_norm, diff[2], 3, 42);
  6293. unsigned int best_pixel_indices1_MSB;
  6294. unsigned int best_pixel_indices1_LSB;
  6295. unsigned int best_pixel_indices2_MSB;
  6296. unsigned int best_pixel_indices2_LSB;
  6297. norm_err = 0;
  6298. // left part of block
  6299. norm_err = tryalltables_3bittable2x4percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  6300. // right part of block
  6301. norm_err += tryalltables_3bittable2x4percep(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  6302. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  6303. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  6304. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  6305. compressed2_norm = 0;
  6306. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  6307. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  6308. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  6309. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  6310. }
  6311. else
  6312. {
  6313. diffbit = 0;
  6314. // The difference is bigger than what fits in 555 plus delta-333, so we will have
  6315. // to deal with 444 444.
  6316. eps = (float) 0.0001;
  6317. quantize444ColorCombinedPerceptual(avg_color_float1, enc_color1, dummy);
  6318. quantize444ColorCombinedPerceptual(avg_color_float2, enc_color2, dummy);
  6319. avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
  6320. avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
  6321. avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
  6322. avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
  6323. avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
  6324. avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
  6325. // Pack bits into the first word.
  6326. // 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
  6327. // ---------------------------------------------------------------------------------------------------
  6328. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  6329. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  6330. // ---------------------------------------------------------------------------------------------------
  6331. compressed1_norm = 0;
  6332. PUTBITSHIGH( compressed1_norm, diffbit, 1, 33);
  6333. PUTBITSHIGH( compressed1_norm, enc_color1[0], 4, 63);
  6334. PUTBITSHIGH( compressed1_norm, enc_color1[1], 4, 55);
  6335. PUTBITSHIGH( compressed1_norm, enc_color1[2], 4, 47);
  6336. PUTBITSHIGH( compressed1_norm, enc_color2[0], 4, 59);
  6337. PUTBITSHIGH( compressed1_norm, enc_color2[1], 4, 51);
  6338. PUTBITSHIGH( compressed1_norm, enc_color2[2], 4, 43);
  6339. unsigned int best_pixel_indices1_MSB;
  6340. unsigned int best_pixel_indices1_LSB;
  6341. unsigned int best_pixel_indices2_MSB;
  6342. unsigned int best_pixel_indices2_LSB;
  6343. // left part of block
  6344. norm_err = tryalltables_3bittable2x4percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  6345. // right part of block
  6346. norm_err += tryalltables_3bittable2x4percep(img,width,height,startx+2,starty,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  6347. PUTBITSHIGH( compressed1_norm, best_table1, 3, 39);
  6348. PUTBITSHIGH( compressed1_norm, best_table2, 3, 36);
  6349. PUTBITSHIGH( compressed1_norm, 0, 1, 32);
  6350. compressed2_norm = 0;
  6351. PUTBITS( compressed2_norm, (best_pixel_indices1_MSB ), 8, 23);
  6352. PUTBITS( compressed2_norm, (best_pixel_indices2_MSB ), 8, 31);
  6353. PUTBITS( compressed2_norm, (best_pixel_indices1_LSB ), 8, 7);
  6354. PUTBITS( compressed2_norm, (best_pixel_indices2_LSB ), 8, 15);
  6355. }
  6356. // Now try flipped blocks 4x2:
  6357. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty,avg_color_float1);
  6358. computeAverageColor4x2noQuantFloat(img,width,height,startx,starty+2,avg_color_float2);
  6359. // First test if avg_color1 is similar enough to avg_color2 so that
  6360. // we can use differential coding of colors.
  6361. quantize555ColorCombinedPerceptual(avg_color_float1, enc_color1, dummy);
  6362. quantize555ColorCombinedPerceptual(avg_color_float2, enc_color2, dummy);
  6363. diff[0] = enc_color2[0]-enc_color1[0];
  6364. diff[1] = enc_color2[1]-enc_color1[1];
  6365. diff[2] = enc_color2[2]-enc_color1[2];
  6366. if( (diff[0] >= -4) && (diff[0] <= 3) && (diff[1] >= -4) && (diff[1] <= 3) && (diff[2] >= -4) && (diff[2] <= 3) )
  6367. {
  6368. diffbit = 1;
  6369. // The difference to be coded:
  6370. diff[0] = enc_color2[0]-enc_color1[0];
  6371. diff[1] = enc_color2[1]-enc_color1[1];
  6372. diff[2] = enc_color2[2]-enc_color1[2];
  6373. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  6374. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  6375. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  6376. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  6377. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  6378. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  6379. // Pack bits into the first word.
  6380. compressed1_flip = 0;
  6381. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  6382. PUTBITSHIGH( compressed1_flip, enc_color1[0], 5, 63);
  6383. PUTBITSHIGH( compressed1_flip, enc_color1[1], 5, 55);
  6384. PUTBITSHIGH( compressed1_flip, enc_color1[2], 5, 47);
  6385. PUTBITSHIGH( compressed1_flip, diff[0], 3, 58);
  6386. PUTBITSHIGH( compressed1_flip, diff[1], 3, 50);
  6387. PUTBITSHIGH( compressed1_flip, diff[2], 3, 42);
  6388. unsigned int best_pixel_indices1_MSB;
  6389. unsigned int best_pixel_indices1_LSB;
  6390. unsigned int best_pixel_indices2_MSB;
  6391. unsigned int best_pixel_indices2_LSB;
  6392. // upper part of block
  6393. flip_err = tryalltables_3bittable4x2percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  6394. // lower part of block
  6395. flip_err += tryalltables_3bittable4x2percep(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  6396. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  6397. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  6398. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  6399. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  6400. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  6401. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  6402. }
  6403. else
  6404. {
  6405. diffbit = 0;
  6406. // The difference is bigger than what fits in 555 plus delta-333, so we will have
  6407. // to deal with 444 444.
  6408. eps = (float) 0.0001;
  6409. quantize444ColorCombinedPerceptual(avg_color_float1, enc_color1, dummy);
  6410. quantize444ColorCombinedPerceptual(avg_color_float2, enc_color2, dummy);
  6411. avg_color_quant1[0] = enc_color1[0] << 4 | enc_color1[0];
  6412. avg_color_quant1[1] = enc_color1[1] << 4 | enc_color1[1];
  6413. avg_color_quant1[2] = enc_color1[2] << 4 | enc_color1[2];
  6414. avg_color_quant2[0] = enc_color2[0] << 4 | enc_color2[0];
  6415. avg_color_quant2[1] = enc_color2[1] << 4 | enc_color2[1];
  6416. avg_color_quant2[2] = enc_color2[2] << 4 | enc_color2[2];
  6417. // 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
  6418. // ---------------------------------------------------------------------------------------------------
  6419. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  6420. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  6421. // ---------------------------------------------------------------------------------------------------
  6422. // Pack bits into the first word.
  6423. compressed1_flip = 0;
  6424. PUTBITSHIGH( compressed1_flip, diffbit, 1, 33);
  6425. PUTBITSHIGH( compressed1_flip, enc_color1[0], 4, 63);
  6426. PUTBITSHIGH( compressed1_flip, enc_color1[1], 4, 55);
  6427. PUTBITSHIGH( compressed1_flip, enc_color1[2], 4, 47);
  6428. PUTBITSHIGH( compressed1_flip, enc_color2[0], 4, 59);
  6429. PUTBITSHIGH( compressed1_flip, enc_color2[1], 4, 51);
  6430. PUTBITSHIGH( compressed1_flip, enc_color2[2], 4, 43);
  6431. unsigned int best_pixel_indices1_MSB;
  6432. unsigned int best_pixel_indices1_LSB;
  6433. unsigned int best_pixel_indices2_MSB;
  6434. unsigned int best_pixel_indices2_LSB;
  6435. // upper part of block
  6436. flip_err = tryalltables_3bittable4x2percep(img,width,height,startx,starty,avg_color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  6437. // lower part of block
  6438. flip_err += tryalltables_3bittable4x2percep(img,width,height,startx,starty+2,avg_color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  6439. PUTBITSHIGH( compressed1_flip, best_table1, 3, 39);
  6440. PUTBITSHIGH( compressed1_flip, best_table2, 3, 36);
  6441. PUTBITSHIGH( compressed1_flip, 1, 1, 32);
  6442. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  6443. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  6444. compressed2_flip = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  6445. }
  6446. // Now lets see which is the best table to use. Only 8 tables are possible.
  6447. if(norm_err <= flip_err)
  6448. {
  6449. compressed1 = compressed1_norm | 0;
  6450. compressed2 = compressed2_norm;
  6451. }
  6452. else
  6453. {
  6454. compressed1 = compressed1_flip | 1;
  6455. compressed2 = compressed2_flip;
  6456. }
  6457. }
  6458. // Calculate the error of a block
  6459. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6460. double calcBlockErrorRGB(uint8 *img, uint8 *imgdec, int width, int height, int startx, int starty)
  6461. {
  6462. int xx,yy;
  6463. double err;
  6464. err = 0;
  6465. for(xx = startx; xx< startx+4; xx++)
  6466. {
  6467. for(yy = starty; yy<starty+4; yy++)
  6468. {
  6469. err += SQUARE(1.0*RED(img,width,xx,yy) - 1.0*RED(imgdec, width, xx,yy));
  6470. err += SQUARE(1.0*GREEN(img,width,xx,yy)- 1.0*GREEN(imgdec, width, xx,yy));
  6471. err += SQUARE(1.0*BLUE(img,width,xx,yy) - 1.0*BLUE(imgdec, width, xx,yy));
  6472. }
  6473. }
  6474. return err;
  6475. }
  6476. // Calculate the perceptually weighted error of a block
  6477. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6478. double calcBlockPerceptualErrorRGB(uint8 *img, uint8 *imgdec, int width, int height, int startx, int starty)
  6479. {
  6480. int xx,yy;
  6481. double err;
  6482. err = 0;
  6483. for(xx = startx; xx< startx+4; xx++)
  6484. {
  6485. for(yy = starty; yy<starty+4; yy++)
  6486. {
  6487. err += PERCEPTUAL_WEIGHT_R_SQUARED*SQUARE(1.0*RED(img,width,xx,yy) - 1.0*RED(imgdec, width, xx,yy));
  6488. err += PERCEPTUAL_WEIGHT_G_SQUARED*SQUARE(1.0*GREEN(img,width,xx,yy)- 1.0*GREEN(imgdec, width, xx,yy));
  6489. err += PERCEPTUAL_WEIGHT_B_SQUARED*SQUARE(1.0*BLUE(img,width,xx,yy) - 1.0*BLUE(imgdec, width, xx,yy));
  6490. }
  6491. }
  6492. return err;
  6493. }
  6494. // Compress an ETC1 block (or the individual and differential modes of an ETC2 block)
  6495. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6496. double compressBlockDiffFlipFast(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  6497. {
  6498. unsigned int average_block1;
  6499. unsigned int average_block2;
  6500. double error_average;
  6501. unsigned int combined_block1;
  6502. unsigned int combined_block2;
  6503. double error_combined;
  6504. double best_error;
  6505. // First quantize the average color to the nearest neighbor.
  6506. compressBlockDiffFlipAverage(img, width, height, startx, starty, average_block1, average_block2);
  6507. decompressBlockDiffFlip(average_block1, average_block2, imgdec, width, height, startx, starty);
  6508. error_average = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  6509. // Then quantize the average color taking into consideration that intensity can change
  6510. compressBlockDiffFlipCombined(img, width, height, startx, starty, combined_block1, combined_block2);
  6511. decompressBlockDiffFlip(combined_block1, combined_block2, imgdec, width, height, startx, starty);
  6512. error_combined = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  6513. if(error_combined < error_average)
  6514. {
  6515. compressed1 = combined_block1;
  6516. compressed2 = combined_block2;
  6517. best_error = error_combined;
  6518. }
  6519. else
  6520. {
  6521. compressed1 = average_block1;
  6522. compressed2 = average_block2;
  6523. best_error = error_average;
  6524. }
  6525. return best_error;
  6526. }
  6527. // Compress an ETC1 block (or the individual and differential modes of an ETC2 block)
  6528. // Uses perceptual error metric.
  6529. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6530. void compressBlockDiffFlipFastPerceptual(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  6531. {
  6532. unsigned int average_block1;
  6533. unsigned int average_block2;
  6534. double error_average;
  6535. unsigned int combined_block1;
  6536. unsigned int combined_block2;
  6537. double error_combined;
  6538. // First quantize the average color to the nearest neighbor.
  6539. compressBlockDiffFlipAveragePerceptual(img, width, height, startx, starty, average_block1, average_block2);
  6540. decompressBlockDiffFlip(average_block1, average_block2, imgdec, width, height, startx, starty);
  6541. error_average = calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
  6542. // Then quantize the average color taking into consideration that intensity can change
  6543. compressBlockDiffFlipCombinedPerceptual(img, width, height, startx, starty, combined_block1, combined_block2);
  6544. decompressBlockDiffFlip(combined_block1, combined_block2, imgdec, width, height, startx, starty);
  6545. error_combined = calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
  6546. if(error_combined < error_average)
  6547. {
  6548. compressed1 = combined_block1;
  6549. compressed2 = combined_block2;
  6550. }
  6551. else
  6552. {
  6553. compressed1 = average_block1;
  6554. compressed2 = average_block2;
  6555. }
  6556. }
  6557. // Compresses the differential mode of an ETC2 block with punchthrough alpha
  6558. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6559. 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)
  6560. {
  6561. unsigned int compressed1_norm, compressed2_norm;
  6562. unsigned int compressed1_flip, compressed2_flip;
  6563. unsigned int compressed1_temp, compressed2_temp;
  6564. uint8 avg_color_quant1[3], avg_color_quant2[3];
  6565. float avg_color_float1[3],avg_color_float2[3];
  6566. int enc_color1[3], enc_color2[3], diff[3];
  6567. int min_error=255*255*8*3;
  6568. int norm_err=0;
  6569. int flip_err=0;
  6570. int temp_err=0;
  6571. for(int flipbit=0; flipbit<2; flipbit++)
  6572. {
  6573. //compute average color for each half.
  6574. for(int c=0; c<3; c++)
  6575. {
  6576. avg_color_float1[c]=0;
  6577. avg_color_float2[c]=0;
  6578. float sum1=0;
  6579. float sum2=0;
  6580. for(int x=0; x<4; x++)
  6581. {
  6582. for(int y=0; y<4; y++)
  6583. {
  6584. float fac=1;
  6585. int index = x+startx+(y+starty)*width;
  6586. //transparent pixels are only barely figured into the average. This ensures that they DO matter if we have only
  6587. //transparent pixels in one half of the block, and not otherwise. A bit ugly perhaps.
  6588. if(alphaimg[index]<128)
  6589. fac=0.0001f;
  6590. float col = fac*img[index*3+c];
  6591. if( (flipbit==0&&x<2) || (flipbit==1&&y<2) )
  6592. {
  6593. sum1+=fac;
  6594. avg_color_float1[c]+=col;
  6595. }
  6596. else
  6597. {
  6598. sum2+=fac;
  6599. avg_color_float2[c]+=col;
  6600. }
  6601. }
  6602. }
  6603. avg_color_float1[c]/=sum1;
  6604. avg_color_float2[c]/=sum2;
  6605. }
  6606. uint8 dummy[3];
  6607. quantize555ColorCombined(avg_color_float1, enc_color1, dummy);
  6608. quantize555ColorCombined(avg_color_float2, enc_color2, dummy);
  6609. diff[0] = enc_color2[0]-enc_color1[0];
  6610. diff[1] = enc_color2[1]-enc_color1[1];
  6611. diff[2] = enc_color2[2]-enc_color1[2];
  6612. //make sure diff is small enough for diff-coding
  6613. for(int c=0; c<3; c++)
  6614. {
  6615. if(diff[c]<-4)
  6616. diff[c]=-4;
  6617. if(diff[c]>3)
  6618. diff[c]=3;
  6619. enc_color2[c]=enc_color1[c]+diff[c];
  6620. }
  6621. avg_color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  6622. avg_color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  6623. avg_color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  6624. avg_color_quant2[0] = enc_color2[0] << 3 | (enc_color2[0] >> 2);
  6625. avg_color_quant2[1] = enc_color2[1] << 3 | (enc_color2[1] >> 2);
  6626. avg_color_quant2[2] = enc_color2[2] << 3 | (enc_color2[2] >> 2);
  6627. // Pack bits into the first word.
  6628. // see regular compressblockdiffflipfast for details
  6629. compressed1_temp = 0;
  6630. PUTBITSHIGH( compressed1_temp, !isTransparent, 1, 33);
  6631. PUTBITSHIGH( compressed1_temp, enc_color1[0], 5, 63);
  6632. PUTBITSHIGH( compressed1_temp, enc_color1[1], 5, 55);
  6633. PUTBITSHIGH( compressed1_temp, enc_color1[2], 5, 47);
  6634. PUTBITSHIGH( compressed1_temp, diff[0], 3, 58);
  6635. PUTBITSHIGH( compressed1_temp, diff[1], 3, 50);
  6636. PUTBITSHIGH( compressed1_temp, diff[2], 3, 42);
  6637. temp_err = 0;
  6638. int besterror[2];
  6639. besterror[0]=255*255*3*16;
  6640. besterror[1]=255*255*3*16;
  6641. int besttable[2];
  6642. int best_indices_LSB[16];
  6643. int best_indices_MSB[16];
  6644. //for each table, we're going to compute the indices required to get minimum error in each half.
  6645. //then we'll check if this was the best table for either half, and set besterror/besttable accordingly.
  6646. for(int table=0; table<8; table++)
  6647. {
  6648. 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.
  6649. int pixel_indices_LSB[16],pixel_indices_MSB[16];
  6650. for(int i=0; i<2; i++)
  6651. {
  6652. taberror[i]=0;
  6653. }
  6654. for(int x=0; x<4; x++)
  6655. {
  6656. for(int y=0; y<4; y++)
  6657. {
  6658. int index = x+startx+(y+starty)*width;
  6659. uint8 basecol[3];
  6660. bool transparentPixel=alphaimg[index]<128;
  6661. //determine which half of the block this pixel is in, based on the flipbit.
  6662. int half=0;
  6663. if( (flipbit==0&&x<2) || (flipbit&&y<2) )
  6664. {
  6665. basecol[0]=avg_color_quant1[0];
  6666. basecol[1]=avg_color_quant1[1];
  6667. basecol[2]=avg_color_quant1[2];
  6668. }
  6669. else
  6670. {
  6671. half=1;
  6672. basecol[0]=avg_color_quant2[0];
  6673. basecol[1]=avg_color_quant2[1];
  6674. basecol[2]=avg_color_quant2[2];
  6675. }
  6676. int besterri=255*255*3*2;
  6677. int besti=0;
  6678. int erri;
  6679. for(int i=0; i<4; i++)
  6680. {
  6681. if(i==1&&isTransparent)
  6682. continue;
  6683. erri=0;
  6684. for(int c=0; c<3; c++)
  6685. {
  6686. int col=CLAMP(0,((int)basecol[c])+compressParams[table*2][i],255);
  6687. if(i==2&&isTransparent)
  6688. {
  6689. col=(int)basecol[c];
  6690. }
  6691. int errcol=col-((int)(img[index*3+c]));
  6692. erri=erri+(errcol*errcol);
  6693. }
  6694. if(erri<besterri)
  6695. {
  6696. besterri=erri;
  6697. besti=i;
  6698. }
  6699. }
  6700. if(transparentPixel)
  6701. {
  6702. besterri=0;
  6703. besti=1;
  6704. }
  6705. //the best index for this pixel using this table for its half is known.
  6706. //add its error to error for this table and half.
  6707. taberror[half]+=besterri;
  6708. //store the index! we might toss it later though if this was a bad table.
  6709. int pixel_index = scramble[besti];
  6710. pixel_indices_MSB[x*4+y]=(pixel_index >> 1);
  6711. pixel_indices_LSB[x*4+y]=(pixel_index & 1);
  6712. }
  6713. }
  6714. for(int half=0; half<2; half++)
  6715. {
  6716. if(taberror[half]<besterror[half])
  6717. {
  6718. besterror[half]=taberror[half];
  6719. besttable[half]=table;
  6720. for(int i=0; i<16; i++)
  6721. {
  6722. int thishalf=0;
  6723. int y=i%4;
  6724. int x=i/4;
  6725. if( !(flipbit==0&&x<2) && !(flipbit&&y<2) )
  6726. thishalf=1;
  6727. if(half!=thishalf) //this pixel is not in given half, don't update best index!
  6728. continue;
  6729. best_indices_MSB[i]=pixel_indices_MSB[i];
  6730. best_indices_LSB[i]=pixel_indices_LSB[i];
  6731. }
  6732. }
  6733. }
  6734. }
  6735. PUTBITSHIGH( compressed1_temp, besttable[0], 3, 39);
  6736. PUTBITSHIGH( compressed1_temp, besttable[1], 3, 36);
  6737. PUTBITSHIGH( compressed1_temp, 0, 1, 32);
  6738. compressed2_temp = 0;
  6739. for(int i=0; i<16; i++)
  6740. {
  6741. PUTBITS( compressed2_temp, (best_indices_MSB[i] ), 1, 16+i);
  6742. PUTBITS( compressed2_temp, (best_indices_LSB[i] ), 1, i);
  6743. }
  6744. if(flipbit)
  6745. {
  6746. flip_err=besterror[0]+besterror[1];
  6747. compressed1_flip=compressed1_temp;
  6748. compressed2_flip=compressed2_temp;
  6749. }
  6750. else
  6751. {
  6752. norm_err=besterror[0]+besterror[1];
  6753. compressed1_norm=compressed1_temp;
  6754. compressed2_norm=compressed2_temp;
  6755. }
  6756. }
  6757. // Now to find out if flipping was a good idea or not
  6758. if(norm_err <= flip_err)
  6759. {
  6760. etc1_word1 = compressed1_norm | 0;
  6761. etc1_word2 = compressed2_norm;
  6762. return norm_err;
  6763. }
  6764. else
  6765. {
  6766. etc1_word1 = compressed1_flip | 1;
  6767. etc1_word2 = compressed2_flip;
  6768. return flip_err;
  6769. }
  6770. }
  6771. // Calculate RGBA error --- only count non-transparent pixels (alpha > 128)
  6772. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6773. double calcBlockErrorRGBA(uint8 *img, uint8 *imgdec, uint8* alpha, int width, int height, int startx, int starty)
  6774. {
  6775. int xx,yy;
  6776. double err;
  6777. err = 0;
  6778. for(xx = startx; xx< startx+4; xx++)
  6779. {
  6780. for(yy = starty; yy<starty+4; yy++)
  6781. {
  6782. //only count non-transparent pixels.
  6783. if(alpha[yy*width+xx]>128)
  6784. {
  6785. err += SQUARE(1.0*RED(img,width,xx,yy) - 1.0*RED(imgdec, width, xx,yy));
  6786. err += SQUARE(1.0*GREEN(img,width,xx,yy)- 1.0*GREEN(imgdec, width, xx,yy));
  6787. err += SQUARE(1.0*BLUE(img,width,xx,yy) - 1.0*BLUE(imgdec, width, xx,yy));
  6788. }
  6789. }
  6790. }
  6791. return err;
  6792. }
  6793. //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.
  6794. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6795. double calculateError59TAlpha(uint8* srcimg, uint8* alpha,int width, int startx, int starty, uint8 (colorsRGB444)[2][3], uint8 &distance, unsigned int &pixel_indices)
  6796. {
  6797. double block_error = 0,
  6798. best_block_error = MAXIMUM_ERROR,
  6799. pixel_error,
  6800. best_pixel_error;
  6801. int diff[3];
  6802. uint8 best_sw;
  6803. unsigned int pixel_colors;
  6804. uint8 colors[2][3];
  6805. uint8 possible_colors[4][3];
  6806. // First use the colors as they are, then swap them
  6807. for (uint8 sw = 0; sw <2; ++sw)
  6808. {
  6809. if (sw == 1)
  6810. {
  6811. swapColors(colorsRGB444);
  6812. }
  6813. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  6814. // Test all distances
  6815. for (uint8 d = 0; d < BINPOW(TABLE_BITS_59T); ++d)
  6816. {
  6817. calculatePaintColors59T(d,PATTERN_T, colors, possible_colors);
  6818. block_error = 0;
  6819. pixel_colors = 0;
  6820. // Loop block
  6821. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  6822. {
  6823. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  6824. {
  6825. best_pixel_error = MAXIMUM_ERROR;
  6826. pixel_colors <<=2; // Make room for next value
  6827. // Loop possible block colors
  6828. if(alpha[x+startx+(y+starty)*width]==0)
  6829. {
  6830. best_pixel_error=0;
  6831. pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
  6832. pixel_colors |= 2; //insert the index for this pixel, two meaning transparent.
  6833. }
  6834. else
  6835. {
  6836. for (uint8 c = 0; c < 4; ++c)
  6837. {
  6838. if(c==2)
  6839. continue; //don't use this, because we don't have alpha here and index 2 means transparent.
  6840. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  6841. diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
  6842. diff[B] = srcimg[3*((starty+y)*width+startx+x)+B] - CLAMP(0,possible_colors[c][B],255);
  6843. pixel_error = weight[R]*SQUARE(diff[R]) +
  6844. weight[G]*SQUARE(diff[G]) +
  6845. weight[B]*SQUARE(diff[B]);
  6846. // Choose best error
  6847. if (pixel_error < best_pixel_error)
  6848. {
  6849. best_pixel_error = pixel_error;
  6850. pixel_colors ^= (pixel_colors & 3); // Reset the two first bits
  6851. pixel_colors |= c; //insert the index for this pixel
  6852. }
  6853. }
  6854. }
  6855. block_error += best_pixel_error;
  6856. }
  6857. }
  6858. if (block_error < best_block_error)
  6859. {
  6860. best_block_error = block_error;
  6861. distance = d;
  6862. pixel_indices = pixel_colors;
  6863. best_sw = sw;
  6864. }
  6865. }
  6866. if (sw == 1 && best_sw == 0)
  6867. {
  6868. swapColors(colorsRGB444);
  6869. }
  6870. decompressColor(R_BITS59T, G_BITS59T, B_BITS59T, colorsRGB444, colors);
  6871. }
  6872. return best_block_error;
  6873. }
  6874. // 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.
  6875. // the only difference is that calculateError has been swapped out to one that considers alpha.
  6876. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6877. double compressBlockTHUMB59TAlpha(uint8 *img, uint8* alpha, int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  6878. {
  6879. double best_error = MAXIMUM_ERROR;
  6880. uint8 best_colorsRGB444[2][3];
  6881. unsigned int best_pixel_indices;
  6882. uint8 best_distance;
  6883. double error_no_i;
  6884. uint8 colorsRGB444_no_i[2][3];
  6885. unsigned int pixel_indices_no_i;
  6886. uint8 distance_no_i;
  6887. uint8 colors[2][3];
  6888. // Calculate average color using the LBG-algorithm
  6889. computeColorLBGHalfIntensityFast(img,width,startx,starty, colors);
  6890. compressColor(R_BITS59T, G_BITS59T, B_BITS59T, colors, colorsRGB444_no_i);
  6891. // Determine the parameters for the lowest error
  6892. error_no_i = calculateError59TAlpha(img, alpha, width, startx, starty, colorsRGB444_no_i, distance_no_i, pixel_indices_no_i);
  6893. best_error = error_no_i;
  6894. best_distance = distance_no_i;
  6895. best_pixel_indices = pixel_indices_no_i;
  6896. copyColors(colorsRGB444_no_i, best_colorsRGB444);
  6897. // Put the compress params into the compression block
  6898. packBlock59T(best_colorsRGB444, best_distance, best_pixel_indices, compressed1, compressed2);
  6899. return best_error;
  6900. }
  6901. // Put bits in order for the format.
  6902. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6903. void stuff59bitsDiffFalse(unsigned int thumbT59_word1, unsigned int thumbT59_word2, unsigned int &thumbT_word1, unsigned int &thumbT_word2)
  6904. {
  6905. // Put bits in twotimer configuration for 59 (red overflows)
  6906. //
  6907. // Go from this bit layout:
  6908. //
  6909. // |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|
  6910. // |----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  6911. //
  6912. // |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|
  6913. // |----------------------------------------index bits---------------------------------------------|
  6914. //
  6915. //
  6916. // To this:
  6917. //
  6918. // 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
  6919. // -----------------------------------------------------------------------------------------------
  6920. // |// // //|R0a |//|R0b |G0 |B0 |R1 |G1 |B1 |da |df|db|
  6921. // -----------------------------------------------------------------------------------------------
  6922. //
  6923. // |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|
  6924. // |----------------------------------------index bits---------------------------------------------|
  6925. //
  6926. // 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
  6927. // -----------------------------------------------------------------------------------------------
  6928. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |df|fp|
  6929. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bt|bt|
  6930. // ------------------------------------------------------------------------------------------------
  6931. uint8 R0a;
  6932. uint8 bit, a, b, c, d, bits;
  6933. R0a = GETBITSHIGH( thumbT59_word1, 2, 58);
  6934. // Fix middle part
  6935. thumbT_word1 = thumbT59_word1 << 1;
  6936. // Fix R0a (top two bits of R0)
  6937. PUTBITSHIGH( thumbT_word1, R0a, 2, 60);
  6938. // Fix db (lowest bit of d)
  6939. PUTBITSHIGH( thumbT_word1, thumbT59_word1, 1, 32);
  6940. //
  6941. // Make sure that red overflows:
  6942. a = GETBITSHIGH( thumbT_word1, 1, 60);
  6943. b = GETBITSHIGH( thumbT_word1, 1, 59);
  6944. c = GETBITSHIGH( thumbT_word1, 1, 57);
  6945. d = GETBITSHIGH( thumbT_word1, 1, 56);
  6946. // The following bit abcd bit sequences should be padded with ones: 0111, 1010, 1011, 1101, 1110, 1111
  6947. // The following logical expression checks for the presence of any of those:
  6948. bit = (a & c) | (!a & b & c & d) | (a & b & !c & d);
  6949. bits = 0xf*bit;
  6950. PUTBITSHIGH( thumbT_word1, bits, 3, 63);
  6951. PUTBITSHIGH( thumbT_word1, !bit, 1, 58);
  6952. // Set diffbit
  6953. PUTBITSHIGH( thumbT_word1, 0, 1, 33);
  6954. thumbT_word2 = thumbT59_word2;
  6955. }
  6956. // Tests if there is at least one pixel in the image which would get alpha = 0 in punchtrough mode.
  6957. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6958. bool hasAlpha(uint8* alphaimg, int ix, int iy, int width)
  6959. {
  6960. for(int x=ix; x<ix+4; x++)
  6961. {
  6962. for(int y=iy; y<iy+4; y++)
  6963. {
  6964. int index = x+y*width;
  6965. if(alphaimg[index]<128)
  6966. {
  6967. return true;
  6968. }
  6969. }
  6970. }
  6971. return false;
  6972. }
  6973. // Compress a block with ETC2 RGB
  6974. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  6975. void compressBlockETC2Fast(uint8 *img, uint8* alphaimg, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  6976. {
  6977. unsigned int etc1_word1;
  6978. unsigned int etc1_word2;
  6979. double error_etc1;
  6980. unsigned int planar57_word1;
  6981. unsigned int planar57_word2;
  6982. unsigned int planar_word1;
  6983. unsigned int planar_word2;
  6984. double error_planar;
  6985. unsigned int thumbT59_word1;
  6986. unsigned int thumbT59_word2;
  6987. unsigned int thumbT_word1;
  6988. unsigned int thumbT_word2;
  6989. double error_thumbT;
  6990. unsigned int thumbH58_word1;
  6991. unsigned int thumbH58_word2;
  6992. unsigned int thumbH_word1;
  6993. unsigned int thumbH_word2;
  6994. double error_thumbH;
  6995. double error_best;
  6996. signed char best_char;
  6997. int best_mode;
  6998. if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  6999. {
  7000. /* if we have one-bit alpha, we never use the individual mode,
  7001. instead that bit flags that one of our four offsets will instead
  7002. mean transparent (with 0 offset for color channels) */
  7003. /* the regular ETC individual mode is disabled, but the old T, H and planar modes
  7004. are kept unchanged and may be used for blocks without transparency.
  7005. Introduced are old ETC with only differential coding,
  7006. ETC differential but with 3 offsets and transparent,
  7007. and T-mode with 3 colors plus transparent.*/
  7008. /* in a fairly hackish manner, error_etc1, etc1_word1 and etc1_word2 will
  7009. represent the best out of the three introduced modes, to be compared
  7010. with the three kept modes in the old code*/
  7011. unsigned int tempword1, tempword2;
  7012. double temperror;
  7013. //try regular differential transparent mode
  7014. int testerr= compressBlockDifferentialWithAlpha(true,img,alphaimg, imgdec,width,height,startx,starty,etc1_word1,etc1_word2);
  7015. uint8* alphadec = new uint8[width*height];
  7016. decompressBlockDifferentialWithAlpha(etc1_word1, etc1_word2, imgdec, alphadec,width, height, startx, starty);
  7017. error_etc1 = calcBlockErrorRGBA(img, imgdec, alphaimg,width, height, startx, starty);
  7018. if(error_etc1!=testerr)
  7019. {
  7020. printf("testerr: %d, etcerr: %lf\n",testerr,error_etc1);
  7021. }
  7022. //try T-mode with transparencies
  7023. //for now, skip this...
  7024. compressBlockTHUMB59TAlpha(img,alphaimg,width,height,startx,starty,tempword1,tempword2);
  7025. decompressBlockTHUMB59TAlpha(tempword1,tempword2,imgdec, alphadec, width,height,startx,starty);
  7026. temperror=calcBlockErrorRGBA(img, imgdec, alphaimg, width, height, startx, starty);
  7027. if(temperror<error_etc1)
  7028. {
  7029. error_etc1=temperror;
  7030. stuff59bitsDiffFalse(tempword1,tempword2,etc1_word1,etc1_word2);
  7031. }
  7032. compressBlockTHUMB58HAlpha(img,alphaimg,width,height,startx,starty,tempword1,tempword2);
  7033. decompressBlockTHUMB58HAlpha(tempword1,tempword2,imgdec, alphadec, width,height,startx,starty);
  7034. temperror=calcBlockErrorRGBA(img, imgdec, alphaimg, width, height, startx, starty);
  7035. if(temperror<error_etc1)
  7036. {
  7037. error_etc1=temperror;
  7038. stuff58bitsDiffFalse(tempword1,tempword2,etc1_word1,etc1_word2);
  7039. }
  7040. //if we have transparency in this pixel, we know that one of these two modes was best..
  7041. if(hasAlpha(alphaimg,startx,starty,width))
  7042. {
  7043. compressed1=etc1_word1;
  7044. compressed2=etc1_word2;
  7045. delete[] alphadec;
  7046. return;
  7047. }
  7048. //error_etc1=255*255*1000;
  7049. //otherwise, they MIGHT have been the best, although that's unlikely.. anyway, try old differential mode now
  7050. compressBlockDifferentialWithAlpha(false,img,alphaimg,imgdec,width,height,startx,starty,tempword1,tempword2);
  7051. decompressBlockDiffFlip(tempword1, tempword2, imgdec, width, height, startx, starty);
  7052. temperror = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  7053. decompressBlockDifferentialWithAlpha(tempword1,tempword2,imgdec,alphadec,width,height,startx,starty);
  7054. if(temperror<error_etc1)
  7055. {
  7056. error_etc1=temperror;
  7057. etc1_word1=tempword1;
  7058. etc1_word2=tempword2;
  7059. }
  7060. delete[] alphadec;
  7061. //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)
  7062. }
  7063. else
  7064. {
  7065. //this includes individual mode, and therefore doesn't apply in case of punch-through alpha.
  7066. compressBlockDiffFlipFast(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
  7067. decompressBlockDiffFlip(etc1_word1, etc1_word2, imgdec, width, height, startx, starty);
  7068. error_etc1 = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  7069. }
  7070. //these modes apply regardless of whether we want punch-through alpha or not.
  7071. //error etc_1 and etc1_word1/etc1_word2 contain previous best candidate.
  7072. compressBlockPlanar57(img, width, height, startx, starty, planar57_word1, planar57_word2);
  7073. decompressBlockPlanar57(planar57_word1, planar57_word2, imgdec, width, height, startx, starty);
  7074. error_planar = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  7075. stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
  7076. compressBlockTHUMB59TFastest(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
  7077. decompressBlockTHUMB59T(thumbT59_word1, thumbT59_word2, imgdec, width, height, startx, starty);
  7078. error_thumbT = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  7079. stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
  7080. compressBlockTHUMB58HFastest(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
  7081. decompressBlockTHUMB58H(thumbH58_word1, thumbH58_word2, imgdec, width, height, startx, starty);
  7082. error_thumbH = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  7083. stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
  7084. error_best = error_etc1;
  7085. compressed1 = etc1_word1;
  7086. compressed2 = etc1_word2;
  7087. best_char = '.';
  7088. best_mode = MODE_ETC1;
  7089. if(error_planar < error_best)
  7090. {
  7091. compressed1 = planar_word1;
  7092. compressed2 = planar_word2;
  7093. best_char = 'p';
  7094. error_best = error_planar;
  7095. best_mode = MODE_PLANAR;
  7096. }
  7097. if(error_thumbT < error_best)
  7098. {
  7099. compressed1 = thumbT_word1;
  7100. compressed2 = thumbT_word2;
  7101. best_char = 'T';
  7102. error_best = error_thumbT;
  7103. best_mode = MODE_THUMB_T;
  7104. }
  7105. if(error_thumbH < error_best)
  7106. {
  7107. compressed1 = thumbH_word1;
  7108. compressed2 = thumbH_word2;
  7109. best_char = 'H';
  7110. error_best = error_thumbH;
  7111. best_mode = MODE_THUMB_H;
  7112. }
  7113. switch(best_mode)
  7114. {
  7115. // Now see which mode won and compress that a little bit harder
  7116. case MODE_THUMB_T:
  7117. compressBlockTHUMB59TFast(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
  7118. decompressBlockTHUMB59T(thumbT59_word1, thumbT59_word2, imgdec, width, height, startx, starty);
  7119. error_thumbT = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  7120. stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
  7121. if(error_thumbT < error_best)
  7122. {
  7123. compressed1 = thumbT_word1;
  7124. compressed2 = thumbT_word2;
  7125. }
  7126. break;
  7127. case MODE_THUMB_H:
  7128. compressBlockTHUMB58HFast(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
  7129. decompressBlockTHUMB58H(thumbH58_word1, thumbH58_word2, imgdec, width, height, startx, starty);
  7130. error_thumbH = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  7131. stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
  7132. if(error_thumbH < error_best)
  7133. {
  7134. compressed1 = thumbH_word1;
  7135. compressed2 = thumbH_word2;
  7136. }
  7137. break;
  7138. default:
  7139. break;
  7140. }
  7141. }
  7142. // Compress an ETC2 RGB block using perceptual error metric
  7143. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7144. void compressBlockETC2FastPerceptual(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  7145. {
  7146. unsigned int etc1_word1;
  7147. unsigned int etc1_word2;
  7148. double error_etc1;
  7149. unsigned int planar57_word1;
  7150. unsigned int planar57_word2;
  7151. unsigned int planar_word1;
  7152. unsigned int planar_word2;
  7153. double error_planar;
  7154. unsigned int thumbT59_word1;
  7155. unsigned int thumbT59_word2;
  7156. unsigned int thumbT_word1;
  7157. unsigned int thumbT_word2;
  7158. double error_thumbT;
  7159. unsigned int thumbH58_word1;
  7160. unsigned int thumbH58_word2;
  7161. unsigned int thumbH_word1;
  7162. unsigned int thumbH_word2;
  7163. double error_thumbH;
  7164. double error_best;
  7165. signed char best_char;
  7166. int best_mode;
  7167. compressBlockDiffFlipFastPerceptual(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
  7168. decompressBlockDiffFlip(etc1_word1, etc1_word2, imgdec, width, height, startx, starty);
  7169. error_etc1 = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
  7170. compressBlockPlanar57(img, width, height, startx, starty, planar57_word1, planar57_word2);
  7171. decompressBlockPlanar57(planar57_word1, planar57_word2, imgdec, width, height, startx, starty);
  7172. error_planar = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
  7173. stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
  7174. compressBlockTHUMB59TFastestPerceptual1000(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
  7175. decompressBlockTHUMB59T(thumbT59_word1, thumbT59_word2, imgdec, width, height, startx, starty);
  7176. error_thumbT = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
  7177. stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
  7178. compressBlockTHUMB58HFastestPerceptual1000(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
  7179. decompressBlockTHUMB58H(thumbH58_word1, thumbH58_word2, imgdec, width, height, startx, starty);
  7180. error_thumbH = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
  7181. stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
  7182. error_best = error_etc1;
  7183. compressed1 = etc1_word1;
  7184. compressed2 = etc1_word2;
  7185. best_char = '.';
  7186. best_mode = MODE_ETC1;
  7187. if(error_planar < error_best)
  7188. {
  7189. compressed1 = planar_word1;
  7190. compressed2 = planar_word2;
  7191. best_char = 'p';
  7192. error_best = error_planar;
  7193. best_mode = MODE_PLANAR;
  7194. }
  7195. if(error_thumbT < error_best)
  7196. {
  7197. compressed1 = thumbT_word1;
  7198. compressed2 = thumbT_word2;
  7199. best_char = 'T';
  7200. error_best = error_thumbT;
  7201. best_mode = MODE_THUMB_T;
  7202. }
  7203. if(error_thumbH < error_best)
  7204. {
  7205. compressed1 = thumbH_word1;
  7206. compressed2 = thumbH_word2;
  7207. best_char = 'H';
  7208. error_best = error_thumbH;
  7209. best_mode = MODE_THUMB_H;
  7210. }
  7211. switch(best_mode)
  7212. {
  7213. // Now see which mode won and compress that a little bit harder
  7214. case MODE_THUMB_T:
  7215. compressBlockTHUMB59TFast(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
  7216. decompressBlockTHUMB59T(thumbT59_word1, thumbT59_word2, imgdec, width, height, startx, starty);
  7217. error_thumbT = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  7218. stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
  7219. if(error_thumbT < error_best)
  7220. {
  7221. compressed1 = thumbT_word1;
  7222. compressed2 = thumbT_word2;
  7223. }
  7224. break;
  7225. case MODE_THUMB_H:
  7226. compressBlockTHUMB58HFast(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
  7227. decompressBlockTHUMB58H(thumbH58_word1, thumbH58_word2, imgdec, width, height, startx, starty);
  7228. error_thumbH = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  7229. stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
  7230. if(error_thumbH < error_best)
  7231. {
  7232. compressed1 = thumbH_word1;
  7233. compressed2 = thumbH_word2;
  7234. }
  7235. break;
  7236. default:
  7237. break;
  7238. }
  7239. }
  7240. // Write a word in big endian style
  7241. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7242. void write_big_endian_2byte_word(unsigned short *blockadr, FILE *f)
  7243. {
  7244. uint8 bytes[2];
  7245. unsigned short block;
  7246. block = blockadr[0];
  7247. bytes[0] = (block >> 8) & 0xff;
  7248. bytes[1] = (block >> 0) & 0xff;
  7249. fwrite(&bytes[0],1,1,f);
  7250. fwrite(&bytes[1],1,1,f);
  7251. }
  7252. // Write a word in big endian style
  7253. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7254. void write_big_endian_4byte_word(unsigned int *blockadr, FILE *f)
  7255. {
  7256. uint8 bytes[4];
  7257. unsigned int block;
  7258. block = blockadr[0];
  7259. bytes[0] = (block >> 24) & 0xff;
  7260. bytes[1] = (block >> 16) & 0xff;
  7261. bytes[2] = (block >> 8) & 0xff;
  7262. bytes[3] = (block >> 0) & 0xff;
  7263. fwrite(&bytes[0],1,1,f);
  7264. fwrite(&bytes[1],1,1,f);
  7265. fwrite(&bytes[2],1,1,f);
  7266. fwrite(&bytes[3],1,1,f);
  7267. }
  7268. extern int alphaTable[256][8];
  7269. extern int alphaBase[16][4];
  7270. // valtab holds precalculated data used for compressing using EAC2.
  7271. // Note that valtab is constructed using get16bits11bits, which means
  7272. // that it already is expanded to 16 bits.
  7273. // Note also that it its contents will depend on the value of formatSigned.
  7274. int *valtab;
  7275. void setupAlphaTableAndValtab()
  7276. {
  7277. setupAlphaTable();
  7278. //fix precomputation table..!
  7279. valtab = new int[1024*512];
  7280. int16 val16;
  7281. int count=0;
  7282. for(int base=0; base<256; base++)
  7283. {
  7284. for(int tab=0; tab<16; tab++)
  7285. {
  7286. for(int mul=0; mul<16; mul++)
  7287. {
  7288. for(int index=0; index<8; index++)
  7289. {
  7290. if(formatSigned)
  7291. {
  7292. val16=get16bits11signed(base,tab,mul,index);
  7293. valtab[count] = val16 + 256*128;
  7294. }
  7295. else
  7296. valtab[count]=get16bits11bits(base,tab,mul,index);
  7297. count++;
  7298. }
  7299. }
  7300. }
  7301. }
  7302. }
  7303. // Reads alpha data
  7304. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7305. void readAlpha(uint8* &data, int &width, int &height, int &extendedwidth, int &extendedheight)
  7306. {
  7307. //width and height are already known..?
  7308. uint8* tempdata;
  7309. int wantedBitDepth;
  7310. if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  7311. {
  7312. wantedBitDepth=8;
  7313. }
  7314. else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
  7315. {
  7316. wantedBitDepth=16;
  7317. }
  7318. else
  7319. {
  7320. printf("invalid format for alpha reading!\n");
  7321. exit(1);
  7322. }
  7323. fReadPGM("alpha.pgm",width,height,tempdata,wantedBitDepth);
  7324. extendedwidth=4*((width+3)/4);
  7325. extendedheight=4*((height+3)/4);
  7326. if(width==extendedwidth&&height==extendedheight)
  7327. {
  7328. data=tempdata;
  7329. }
  7330. else
  7331. {
  7332. data = (uint8*)malloc(extendedwidth*extendedheight*wantedBitDepth/8);
  7333. uint8 last=0;
  7334. uint8 lastlast=0;
  7335. for(int x=0; x<extendedwidth; x++)
  7336. {
  7337. for(int y=0; y<extendedheight; y++)
  7338. {
  7339. if(wantedBitDepth==8)
  7340. {
  7341. if(x<width&&y<height)
  7342. {
  7343. last = tempdata[x+y*width];
  7344. }
  7345. data[x+y*extendedwidth]=last;
  7346. }
  7347. else
  7348. {
  7349. if(x<width&&y<height)
  7350. {
  7351. last = tempdata[(x+y*width)*2];
  7352. lastlast = tempdata[(x+y*width)*2+1];
  7353. }
  7354. data[(x+y*extendedwidth)*2]=last;
  7355. data[(x+y*extendedwidth)*2+1]=lastlast;
  7356. }
  7357. }
  7358. }
  7359. }
  7360. if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  7361. {
  7362. for(int x=0; x<extendedwidth; x++)
  7363. {
  7364. for(int y=0; y<extendedheight; y++)
  7365. {
  7366. if(data[x+y*extendedwidth]<128)
  7367. data[x+y*extendedwidth]=0;
  7368. else
  7369. data[x+y*extendedwidth]=255;
  7370. }
  7371. }
  7372. }
  7373. }
  7374. // Compresses the alpha part of a GL_COMPRESSED_RGBA8_ETC2_EAC block.
  7375. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7376. void compressBlockAlphaFast(uint8 * data, int ix, int iy, int width, int height, uint8* returnData)
  7377. {
  7378. int alphasum=0;
  7379. int maxdist=-2;
  7380. for(int x=0; x<4; x++)
  7381. {
  7382. for(int y=0; y<4; y++)
  7383. {
  7384. alphasum+=data[ix+x+(iy+y)*width];
  7385. }
  7386. }
  7387. int alpha = (int)( ((float)alphasum)/16.0f+0.5f); //average pixel value, used as guess for base value.
  7388. for(int x=0; x<4; x++)
  7389. {
  7390. for(int y=0; y<4; y++)
  7391. {
  7392. if(abs(alpha-data[ix+x+(iy+y)*width])>maxdist)
  7393. maxdist=abs(alpha-data[ix+x+(iy+y)*width]); //maximum distance from average
  7394. }
  7395. }
  7396. int approxPos = (maxdist*255)/160-4; //experimentally derived formula for calculating approximate table position given a max distance from average
  7397. if(approxPos>255)
  7398. approxPos=255;
  7399. int startTable=approxPos-15; //first table to be tested
  7400. if(startTable<0)
  7401. startTable=0;
  7402. int endTable=clamp(approxPos+15); //last table to be tested
  7403. int bestsum=1000000000;
  7404. int besttable=-3;
  7405. int bestalpha=128;
  7406. int prevalpha=alpha;
  7407. //main loop: determine best base alpha value and offset table to use for compression
  7408. //try some different alpha tables.
  7409. for(int table = startTable; table<endTable&&bestsum>0; table++)
  7410. {
  7411. int tablealpha=prevalpha;
  7412. int tablebestsum=1000000000;
  7413. //test some different alpha values, trying to find the best one for the given table.
  7414. for(int alphascale=16; alphascale>0; alphascale/=4)
  7415. {
  7416. int startalpha;
  7417. int endalpha;
  7418. if(alphascale==16)
  7419. {
  7420. startalpha = clamp(tablealpha-alphascale*4);
  7421. endalpha = clamp(tablealpha+alphascale*4);
  7422. }
  7423. else
  7424. {
  7425. startalpha = clamp(tablealpha-alphascale*2);
  7426. endalpha = clamp(tablealpha+alphascale*2);
  7427. }
  7428. for(alpha=startalpha; alpha<=endalpha; alpha+=alphascale)
  7429. {
  7430. int sum=0;
  7431. int val,diff,bestdiff=10000000,index;
  7432. for(int x=0; x<4; x++)
  7433. {
  7434. for(int y=0; y<4; y++)
  7435. {
  7436. //compute best offset here, add square difference to sum..
  7437. val=data[ix+x+(iy+y)*width];
  7438. bestdiff=1000000000;
  7439. //the values are always ordered from small to large, with the first 4 being negative and the last 4 positive
  7440. //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.
  7441. if(val>alpha)
  7442. {
  7443. for(index=7; index>3; index--)
  7444. {
  7445. diff=clamp_table[alpha+(int)(alphaTable[table][index])+255]-val;
  7446. diff*=diff;
  7447. if(diff<=bestdiff)
  7448. {
  7449. bestdiff=diff;
  7450. }
  7451. else
  7452. break;
  7453. }
  7454. }
  7455. else
  7456. {
  7457. for(index=0; index<4; index++)
  7458. {
  7459. diff=clamp_table[alpha+(int)(alphaTable[table][index])+255]-val;
  7460. diff*=diff;
  7461. if(diff<bestdiff)
  7462. {
  7463. bestdiff=diff;
  7464. }
  7465. else
  7466. break;
  7467. }
  7468. }
  7469. //best diff here is bestdiff, add it to sum!
  7470. sum+=bestdiff;
  7471. //if the sum here is worse than previously best already, there's no use in continuing the count..
  7472. //note that tablebestsum could be used for more precise estimation, but the speedup gained here is deemed more important.
  7473. if(sum>bestsum)
  7474. {
  7475. x=9999; //just to make it large and get out of the x<4 loop
  7476. break;
  7477. }
  7478. }
  7479. }
  7480. if(sum<tablebestsum)
  7481. {
  7482. tablebestsum=sum;
  7483. tablealpha=alpha;
  7484. }
  7485. if(sum<bestsum)
  7486. {
  7487. bestsum=sum;
  7488. besttable=table;
  7489. bestalpha=alpha;
  7490. }
  7491. }
  7492. if(alphascale<=2)
  7493. alphascale=0;
  7494. }
  7495. }
  7496. alpha=bestalpha;
  7497. //"good" alpha value and table are known!
  7498. //store them, then loop through the pixels again and print indices.
  7499. returnData[0]=alpha;
  7500. returnData[1]=besttable;
  7501. for(int pos=2; pos<8; pos++)
  7502. {
  7503. returnData[pos]=0;
  7504. }
  7505. int byte=2;
  7506. int bit=0;
  7507. for(int x=0; x<4; x++)
  7508. {
  7509. for(int y=0; y<4; y++)
  7510. {
  7511. //find correct index
  7512. int besterror=1000000;
  7513. int bestindex=99;
  7514. for(int index=0; index<8; index++) //no clever ordering this time, as this loop is only run once per block anyway
  7515. {
  7516. 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]);
  7517. if(error<besterror)
  7518. {
  7519. besterror=error;
  7520. bestindex=index;
  7521. }
  7522. }
  7523. //best table index has been determined.
  7524. //pack 3-bit index into compressed data, one bit at a time
  7525. for(int numbit=0; numbit<3; numbit++)
  7526. {
  7527. returnData[byte]|=getbit(bestindex,2-numbit,7-bit);
  7528. bit++;
  7529. if(bit>7)
  7530. {
  7531. bit=0;
  7532. byte++;
  7533. }
  7534. }
  7535. }
  7536. }
  7537. }
  7538. // Helper function for the below function
  7539. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7540. int getPremulIndex(int base, int tab, int mul, int index)
  7541. {
  7542. return (base<<11)+(tab<<7)+(mul<<3)+index;
  7543. }
  7544. // Calculates the error used in compressBlockAlpha16()
  7545. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7546. double calcError(uint8* data, int ix, int iy, int width, int height, int base, int tab, int mul, double prevbest)
  7547. {
  7548. int offset = getPremulIndex(base,tab,mul,0);
  7549. double error=0;
  7550. for (int y=0; y<4; y++)
  7551. {
  7552. for(int x=0; x<4; x++)
  7553. {
  7554. double besthere = (1<<20);
  7555. besthere*=besthere;
  7556. uint8 byte1 = data[2*(x+ix+(y+iy)*width)];
  7557. uint8 byte2 = data[2*(x+ix+(y+iy)*width)+1];
  7558. int alpha = (byte1<<8)+byte2;
  7559. for(int index=0; index<8; index++)
  7560. {
  7561. double indexError;
  7562. indexError = alpha-valtab[offset+index];
  7563. indexError*=indexError;
  7564. if(indexError<besthere)
  7565. besthere=indexError;
  7566. }
  7567. error+=besthere;
  7568. if(error>=prevbest)
  7569. return prevbest+(1<<30);
  7570. }
  7571. }
  7572. return error;
  7573. }
  7574. // compressBlockAlpha16
  7575. //
  7576. // Compresses a block using the 11-bit EAC formats.
  7577. // Depends on the global variable formatSigned.
  7578. //
  7579. // COMPRESSED_R11_EAC (if formatSigned = 0)
  7580. // This is an 11-bit unsigned format. Since we do not have a good 11-bit file format, we use 16-bit pgm instead.
  7581. // Here we assume that, in the input 16-bit pgm file, 0 represents 0.0 and 65535 represents 1.0. The function compressBlockAlpha16
  7582. // will find the compressed block which best matches the data. In detail, it will find the compressed block, which
  7583. // if decompressed, will generate an 11-bit block that after bit replication to 16-bits will generate the closest
  7584. // block to the original 16-bit pgm block.
  7585. //
  7586. // COMPRESSED_SIGNED_R11_EAC (if formatSigned = 1)
  7587. // This is an 11-bit signed format. Since we do not have any signed file formats, we use unsigned 16-bit pgm instead.
  7588. // Hence we assume that, in the input 16-bit pgm file, 1 represents -1.0, 32768 represents 0.0 and 65535 represents 1.0.
  7589. // The function compresseBlockAlpha16 will find the compressed block, which if decompressed, will generate a signed
  7590. // 11-bit block that after bit replication to 16-bits and conversion to unsigned (1 equals -1.0, 32768 equals 0.0 and
  7591. // 65535 equals 1.0) will generate the closest block to the original 16-bit pgm block.
  7592. //
  7593. // COMPRESSED_RG11_EAC is compressed by calling the function twice, dito for COMPRESSED_SIGNED_RG11_EAC.
  7594. //
  7595. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7596. void compressBlockAlpha16(uint8* data, int ix, int iy, int width, int height, uint8* returnData)
  7597. {
  7598. unsigned int bestbase, besttable, bestmul;
  7599. double besterror;
  7600. besterror=1<<20;
  7601. besterror*=besterror;
  7602. for(int base=0; base<256; base++)
  7603. {
  7604. for(int table=0; table<16; table++)
  7605. {
  7606. for(int mul=0; mul<16; mul++)
  7607. {
  7608. double e = calcError(data, ix, iy, width, height,base,table,mul,besterror);
  7609. if(e<besterror)
  7610. {
  7611. bestbase=base;
  7612. besttable=table;
  7613. bestmul=mul;
  7614. besterror=e;
  7615. }
  7616. }
  7617. }
  7618. }
  7619. returnData[0]=bestbase;
  7620. returnData[1]=(bestmul<<4)+besttable;
  7621. if(formatSigned)
  7622. {
  7623. //if we have a signed format, the base value should be given as a signed byte.
  7624. signed char signedbase = bestbase-128;
  7625. returnData[0]=*((uint8*)(&signedbase));
  7626. }
  7627. for(int i=2; i<8; i++)
  7628. {
  7629. returnData[i]=0;
  7630. }
  7631. int byte=2;
  7632. int bit=0;
  7633. for (int x=0; x<4; x++)
  7634. {
  7635. for(int y=0; y<4; y++)
  7636. {
  7637. double besterror=255*255;
  7638. besterror*=besterror;
  7639. int bestindex=99;
  7640. uint8 byte1 = data[2*(x+ix+(y+iy)*width)];
  7641. uint8 byte2 = data[2*(x+ix+(y+iy)*width)+1];
  7642. int alpha = (byte1<<8)+byte2;
  7643. for(unsigned int index=0; index<8; index++)
  7644. {
  7645. double indexError;
  7646. if(formatSigned)
  7647. {
  7648. int16 val16;
  7649. int val;
  7650. val16 = get16bits11signed(bestbase,besttable,bestmul,index);
  7651. val = val16 + 256*128;
  7652. indexError = alpha-val;
  7653. }
  7654. else
  7655. indexError = alpha-get16bits11bits(bestbase,besttable,bestmul,index);
  7656. indexError*=indexError;
  7657. if(indexError<besterror)
  7658. {
  7659. besterror=indexError;
  7660. bestindex=index;
  7661. }
  7662. }
  7663. for(int numbit=0; numbit<3; numbit++)
  7664. {
  7665. returnData[byte]|=getbit(bestindex,2-numbit,7-bit);
  7666. bit++;
  7667. if(bit>7)
  7668. {
  7669. bit=0;
  7670. byte++;
  7671. }
  7672. }
  7673. }
  7674. }
  7675. }
  7676. // Exhaustive compression of alpha compression in a GL_COMPRESSED_RGB8_ETC2 block
  7677. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7678. void compressBlockAlphaSlow(uint8* data, int ix, int iy, int width, int height, uint8* returnData)
  7679. {
  7680. //determine the best table and base alpha value for this block using MSE
  7681. int alphasum=0;
  7682. int maxdist=-2;
  7683. for(int x=0; x<4; x++)
  7684. {
  7685. for(int y=0; y<4; y++)
  7686. {
  7687. alphasum+=data[ix+x+(iy+y)*width];
  7688. }
  7689. }
  7690. int alpha = (int)( ((float)alphasum)/16.0f+0.5f); //average pixel value, used as guess for base value.
  7691. int bestsum=1000000000;
  7692. int besttable=-3;
  7693. int bestalpha=128;
  7694. int prevalpha=alpha;
  7695. //main loop: determine best base alpha value and offset table to use for compression
  7696. //try some different alpha tables.
  7697. for(int table = 0; table<256&&bestsum>0; table++)
  7698. {
  7699. int tablealpha=prevalpha;
  7700. int tablebestsum=1000000000;
  7701. //test some different alpha values, trying to find the best one for the given table.
  7702. for(int alphascale=32; alphascale>0; alphascale/=8)
  7703. {
  7704. int startalpha = clamp(tablealpha-alphascale*4);
  7705. int endalpha = clamp(tablealpha+alphascale*4);
  7706. for(alpha=startalpha; alpha<=endalpha; alpha+=alphascale) {
  7707. int sum=0;
  7708. int val,diff,bestdiff=10000000,index;
  7709. for(int x=0; x<4; x++)
  7710. {
  7711. for(int y=0; y<4; y++)
  7712. {
  7713. //compute best offset here, add square difference to sum..
  7714. val=data[ix+x+(iy+y)*width];
  7715. bestdiff=1000000000;
  7716. //the values are always ordered from small to large, with the first 4 being negative and the last 4 positive
  7717. //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.
  7718. if(val>alpha)
  7719. {
  7720. for(index=7; index>3; index--)
  7721. {
  7722. diff=clamp_table[alpha+(alphaTable[table][index])+255]-val;
  7723. diff*=diff;
  7724. if(diff<=bestdiff)
  7725. {
  7726. bestdiff=diff;
  7727. }
  7728. else
  7729. break;
  7730. }
  7731. }
  7732. else
  7733. {
  7734. for(index=0; index<5; index++)
  7735. {
  7736. diff=clamp_table[alpha+(alphaTable[table][index])+255]-val;
  7737. diff*=diff;
  7738. if(diff<bestdiff)
  7739. {
  7740. bestdiff=diff;
  7741. }
  7742. else
  7743. break;
  7744. }
  7745. }
  7746. //best diff here is bestdiff, add it to sum!
  7747. sum+=bestdiff;
  7748. //if the sum here is worse than previously best already, there's no use in continuing the count..
  7749. if(sum>tablebestsum)
  7750. {
  7751. x=9999; //just to make it large and get out of the x<4 loop
  7752. break;
  7753. }
  7754. }
  7755. }
  7756. if(sum<tablebestsum)
  7757. {
  7758. tablebestsum=sum;
  7759. tablealpha=alpha;
  7760. }
  7761. if(sum<bestsum)
  7762. {
  7763. bestsum=sum;
  7764. besttable=table;
  7765. bestalpha=alpha;
  7766. }
  7767. }
  7768. if(alphascale==4)
  7769. alphascale=8;
  7770. }
  7771. }
  7772. alpha=bestalpha;
  7773. //the best alpha value and table are known!
  7774. //store them, then loop through the pixels again and print indices.
  7775. returnData[0]=alpha;
  7776. returnData[1]=besttable;
  7777. for(int pos=2; pos<8; pos++)
  7778. {
  7779. returnData[pos]=0;
  7780. }
  7781. int byte=2;
  7782. int bit=0;
  7783. for(int x=0; x<4; x++)
  7784. {
  7785. for(int y=0; y<4; y++)
  7786. {
  7787. //find correct index
  7788. int besterror=1000000;
  7789. int bestindex=99;
  7790. for(int index=0; index<8; index++) //no clever ordering this time, as this loop is only run once per block anyway
  7791. {
  7792. 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]);
  7793. if(error<besterror)
  7794. {
  7795. besterror=error;
  7796. bestindex=index;
  7797. }
  7798. }
  7799. //best table index has been determined.
  7800. //pack 3-bit index into compressed data, one bit at a time
  7801. for(int numbit=0; numbit<3; numbit++)
  7802. {
  7803. returnData[byte]|=getbit(bestindex,2-numbit,7-bit);
  7804. bit++;
  7805. if(bit>7)
  7806. {
  7807. bit=0;
  7808. byte++;
  7809. }
  7810. }
  7811. }
  7812. }
  7813. }
  7814. // Calculate weighted PSNR
  7815. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7816. double calculateWeightedPSNR(uint8 *lossyimg, uint8 *origimg, int width, int height, double w1, double w2, double w3)
  7817. {
  7818. // Note: This calculation of PSNR uses the formula
  7819. //
  7820. // PSNR = 10 * log_10 ( 255^2 / wMSE )
  7821. //
  7822. // where the wMSE is calculated as
  7823. //
  7824. // 1/(N*M) * sum ( ( w1*(R' - R)^2 + w2*(G' - G)^2 + w3*(B' - B)^2) )
  7825. //
  7826. // typical weights are 0.299, 0.587, 0.114 for perceptually weighted PSNR and
  7827. // 1.0/3.0, 1.0/3.0, 1.0/3.0 for nonweighted PSNR
  7828. int x,y;
  7829. double wMSE;
  7830. double PSNR;
  7831. double err;
  7832. wMSE = 0;
  7833. for(y=0;y<height;y++)
  7834. {
  7835. for(x=0;x<width;x++)
  7836. {
  7837. err = lossyimg[y*width*3+x*3+0] - origimg[y*width*3+x*3+0];
  7838. wMSE = wMSE + (w1*(err * err));
  7839. err = lossyimg[y*width*3+x*3+1] - origimg[y*width*3+x*3+1];
  7840. wMSE = wMSE + (w2*(err * err));
  7841. err = lossyimg[y*width*3+x*3+2] - origimg[y*width*3+x*3+2];
  7842. wMSE = wMSE + (w3*(err * err));
  7843. }
  7844. }
  7845. wMSE = wMSE / (width * height);
  7846. if(wMSE == 0)
  7847. {
  7848. printf("********************************************************************\n");
  7849. printf("There is no difference at all between image files --- infinite PSNR.\n");
  7850. printf("********************************************************************\n");
  7851. }
  7852. PSNR = 10*log((1.0*255*255)/wMSE)/log(10.0);
  7853. return PSNR;
  7854. }
  7855. // Calculate unweighted PSNR (weights are (1,1,1))
  7856. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7857. double calculatePSNR(uint8 *lossyimg, uint8 *origimg, int width, int height)
  7858. {
  7859. // Note: This calculation of PSNR uses the formula
  7860. //
  7861. // PSNR = 10 * log_10 ( 255^2 / MSE )
  7862. //
  7863. // where the MSE is calculated as
  7864. //
  7865. // 1/(N*M) * sum ( 1/3 * ((R' - R)^2 + (G' - G)^2 + (B' - B)^2) )
  7866. //
  7867. // The reason for having the 1/3 factor is the following:
  7868. // Presume we have a grayscale image, that is acutally just the red component
  7869. // of a color image.. The squared error is then (R' - R)^2.
  7870. // Assume that we have a certain signal to noise ratio, say 30 dB. If we add
  7871. // another two components (say green and blue) with the same signal to noise
  7872. // ratio, we want the total signal to noise ratio be the same. For the
  7873. // squared error to remain constant we must divide by three after adding
  7874. // together the squared errors of the components.
  7875. return calculateWeightedPSNR(lossyimg, origimg, width, height, (1.0/3.0), (1.0/3.0), (1.0/3.0));
  7876. }
  7877. // Decompresses a file
  7878. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  7879. void uncompressFile(char *srcfile, uint8* &img, uint8 *&alphaimg, int& active_width, int& active_height)
  7880. {
  7881. FILE *f;
  7882. int width,height;
  7883. unsigned int block_part1, block_part2;
  7884. uint8 *newimg, *newalphaimg, *alphaimg2;
  7885. unsigned short w, h;
  7886. int xx, yy;
  7887. unsigned char magic[4];
  7888. unsigned char version[2];
  7889. unsigned short texture_type;
  7890. if(f=fopen(srcfile,"rb"))
  7891. {
  7892. // Load table
  7893. readCompressParams();
  7894. if(ktxFile)
  7895. {
  7896. //read ktx header..
  7897. KTX_header header;
  7898. fread(&header,sizeof(KTX_header),1,f);
  7899. //read size parameter, which we don't actually need..
  7900. unsigned int bitsize;
  7901. fread(&bitsize,sizeof(unsigned int),1,f);
  7902. active_width=header.pixelWidth;
  7903. active_height = header.pixelHeight;
  7904. w = ((active_width+3)/4)*4;
  7905. h = ((active_height+3)/4)*4;
  7906. width=w;
  7907. height=h;
  7908. if(header.glInternalFormat==GL_COMPRESSED_SIGNED_R11_EAC)
  7909. {
  7910. format=ETC2PACKAGE_R_NO_MIPMAPS;
  7911. formatSigned=1;
  7912. }
  7913. else if(header.glInternalFormat==GL_COMPRESSED_R11_EAC)
  7914. {
  7915. format=ETC2PACKAGE_R_NO_MIPMAPS;
  7916. }
  7917. else if(header.glInternalFormat==GL_COMPRESSED_SIGNED_RG11_EAC)
  7918. {
  7919. format=ETC2PACKAGE_RG_NO_MIPMAPS;
  7920. formatSigned=1;
  7921. }
  7922. else if(header.glInternalFormat==GL_COMPRESSED_RG11_EAC)
  7923. {
  7924. format=ETC2PACKAGE_RG_NO_MIPMAPS;
  7925. }
  7926. else if(header.glInternalFormat==GL_COMPRESSED_RGB8_ETC2)
  7927. {
  7928. format=ETC2PACKAGE_RGB_NO_MIPMAPS;
  7929. }
  7930. else if(header.glInternalFormat==GL_COMPRESSED_SRGB8_ETC2)
  7931. {
  7932. format=ETC2PACKAGE_sRGB_NO_MIPMAPS;
  7933. }
  7934. else if(header.glInternalFormat==GL_COMPRESSED_RGBA8_ETC2_EAC)
  7935. {
  7936. format=ETC2PACKAGE_RGBA_NO_MIPMAPS;
  7937. }
  7938. else if(header.glInternalFormat==GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC)
  7939. {
  7940. format=ETC2PACKAGE_sRGBA_NO_MIPMAPS;
  7941. }
  7942. else if(header.glInternalFormat==GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2)
  7943. {
  7944. format=ETC2PACKAGE_RGBA1_NO_MIPMAPS;
  7945. }
  7946. else if(header.glInternalFormat==GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2)
  7947. {
  7948. format=ETC2PACKAGE_sRGBA1_NO_MIPMAPS;
  7949. }
  7950. else if(header.glInternalFormat==GL_ETC1_RGB8_OES)
  7951. {
  7952. format=ETC1_RGB_NO_MIPMAPS;
  7953. codec=CODEC_ETC;
  7954. }
  7955. else
  7956. {
  7957. printf("ktx file has unknown glInternalFormat (not etc compressed)!\n");
  7958. exit(1);
  7959. }
  7960. }
  7961. else
  7962. {
  7963. // Read magic nunmber
  7964. fread(&magic[0], sizeof(unsigned char), 1, f);
  7965. fread(&magic[1], sizeof(unsigned char), 1, f);
  7966. fread(&magic[2], sizeof(unsigned char), 1, f);
  7967. fread(&magic[3], sizeof(unsigned char), 1, f);
  7968. if(!(magic[0] == 'P' && magic[1] == 'K' && magic[2] == 'M' && magic[3] == ' '))
  7969. {
  7970. printf("\n\n The file %s is not a .pkm file.\n",srcfile);
  7971. exit(1);
  7972. }
  7973. // Read version
  7974. fread(&version[0], sizeof(unsigned char), 1, f);
  7975. fread(&version[1], sizeof(unsigned char), 1, f);
  7976. if( version[0] == '1' && version[1] == '0' )
  7977. {
  7978. // Read texture type
  7979. read_big_endian_2byte_word(&texture_type, f);
  7980. if(!(texture_type == ETC1_RGB_NO_MIPMAPS))
  7981. {
  7982. printf("\n\n The file %s (of version %c.%c) does not contain a texture of known format.\n", srcfile, version[0],version[1]);
  7983. printf("Known formats: ETC1_RGB_NO_MIPMAPS.\n", srcfile);
  7984. exit(1);
  7985. }
  7986. }
  7987. else if( version[0] == '2' && version[1] == '0' )
  7988. {
  7989. // Read texture type
  7990. read_big_endian_2byte_word(&texture_type, f);
  7991. if(texture_type==ETC2PACKAGE_RG_SIGNED_NO_MIPMAPS)
  7992. {
  7993. texture_type=ETC2PACKAGE_RG_NO_MIPMAPS;
  7994. formatSigned=1;
  7995. //printf("Decompressing 2-channel signed data\n");
  7996. }
  7997. if(texture_type==ETC2PACKAGE_R_SIGNED_NO_MIPMAPS)
  7998. {
  7999. texture_type=ETC2PACKAGE_R_NO_MIPMAPS;
  8000. formatSigned=1;
  8001. //printf("Decompressing 1-channel signed data\n");
  8002. }
  8003. if(texture_type==ETC2PACKAGE_sRGB_NO_MIPMAPS)
  8004. {
  8005. // The SRGB formats are decoded just as RGB formats -- use RGB format for decompression.
  8006. texture_type=ETC2PACKAGE_RGB_NO_MIPMAPS;
  8007. }
  8008. if(texture_type==ETC2PACKAGE_sRGBA_NO_MIPMAPS)
  8009. {
  8010. // The SRGB formats are decoded just as RGB formats -- use RGB format for decompression.
  8011. texture_type=ETC2PACKAGE_RGBA_NO_MIPMAPS;
  8012. }
  8013. if(texture_type==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  8014. {
  8015. // The SRGB formats are decoded just as RGB formats -- use RGB format for decompression.
  8016. texture_type=ETC2PACKAGE_sRGBA1_NO_MIPMAPS;
  8017. }
  8018. if(texture_type==ETC2PACKAGE_RGBA_NO_MIPMAPS_OLD)
  8019. {
  8020. printf("\n\nThe file %s contains a compressed texture created using an old version of ETCPACK.\n",srcfile);
  8021. printf("decompression is not supported in this version.\n");
  8022. exit(1);
  8023. }
  8024. 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))
  8025. {
  8026. printf("\n\n The file %s does not contain a texture of known format.\n", srcfile);
  8027. printf("Known formats: ETC2PACKAGE_RGB_NO_MIPMAPS.\n", srcfile);
  8028. exit(1);
  8029. }
  8030. }
  8031. else
  8032. {
  8033. 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]);
  8034. printf("Aborting.\n");
  8035. exit(1);
  8036. }
  8037. format=texture_type;
  8038. printf("textype: %d\n",texture_type);
  8039. // ETC2 is backwards compatible, which means that an ETC2-capable decompressor can also handle
  8040. // old ETC1 textures without any problems. Thus a version 1.0 file with ETC1_RGB_NO_MIPMAPS and a
  8041. // version 2.0 file with ETC2PACKAGE_RGB_NO_MIPMAPS can be handled by the same ETC2-capable decompressor
  8042. // Read how many pixels the blocks make up
  8043. read_big_endian_2byte_word(&w, f);
  8044. read_big_endian_2byte_word(&h, f);
  8045. width = w;
  8046. height = h;
  8047. // Read how many pixels contain active data (the rest are just
  8048. // for making sure we have a 4*a x 4*b size).
  8049. read_big_endian_2byte_word(&w, f);
  8050. read_big_endian_2byte_word(&h, f);
  8051. active_width = w;
  8052. active_height = h;
  8053. }
  8054. printf("Width = %d, Height = %d\n",width, height);
  8055. printf("active pixel area: top left %d x %d area.\n",active_width, active_height);
  8056. if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  8057. img=(uint8*)malloc(3*width*height*2);
  8058. else
  8059. img=(uint8*)malloc(3*width*height);
  8060. if(!img)
  8061. {
  8062. printf("Error: could not allocate memory\n");
  8063. exit(0);
  8064. }
  8065. 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)
  8066. {
  8067. //printf("alpha channel decompression\n");
  8068. alphaimg=(uint8*)malloc(width*height*2);
  8069. setupAlphaTableAndValtab();
  8070. if(!alphaimg)
  8071. {
  8072. printf("Error: could not allocate memory for alpha\n");
  8073. exit(0);
  8074. }
  8075. }
  8076. if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  8077. {
  8078. alphaimg2=(uint8*)malloc(width*height*2);
  8079. if(!alphaimg2)
  8080. {
  8081. printf("Error: could not allocate memory\n");
  8082. exit(0);
  8083. }
  8084. }
  8085. for(int y=0;y<height/4;y++)
  8086. {
  8087. for(int x=0;x<width/4;x++)
  8088. {
  8089. //decode alpha channel for RGBA
  8090. if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS)
  8091. {
  8092. uint8 alphablock[8];
  8093. fread(alphablock,1,8,f);
  8094. decompressBlockAlpha(alphablock,alphaimg,width,height,4*x,4*y);
  8095. }
  8096. //color channels for most normal modes
  8097. if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
  8098. {
  8099. //we have normal ETC2 color channels, decompress these
  8100. read_big_endian_4byte_word(&block_part1,f);
  8101. read_big_endian_4byte_word(&block_part2,f);
  8102. if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  8103. decompressBlockETC21BitAlpha(block_part1, block_part2,img,alphaimg,width,height,4*x,4*y);
  8104. else
  8105. decompressBlockETC2(block_part1, block_part2,img,width,height,4*x,4*y);
  8106. }
  8107. //one or two 11-bit alpha channels for R or RG.
  8108. if(format==ETC2PACKAGE_R_NO_MIPMAPS||format==ETC2PACKAGE_RG_NO_MIPMAPS)
  8109. {
  8110. uint8 alphablock[8];
  8111. fread(alphablock,1,8,f);
  8112. decompressBlockAlpha16bit(alphablock,alphaimg,width,height,4*x,4*y);
  8113. }
  8114. if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  8115. {
  8116. uint8 alphablock[8];
  8117. fread(alphablock,1,8,f);
  8118. decompressBlockAlpha16bit(alphablock,alphaimg2,width,height,4*x,4*y);
  8119. }
  8120. }
  8121. }
  8122. if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  8123. {
  8124. for(int y=0;y<height;y++)
  8125. {
  8126. for(int x=0;x<width;x++)
  8127. {
  8128. img[6*(y*width+x)]=alphaimg[2*(y*width+x)];
  8129. img[6*(y*width+x)+1]=alphaimg[2*(y*width+x)+1];
  8130. img[6*(y*width+x)+2]=alphaimg2[2*(y*width+x)];
  8131. img[6*(y*width+x)+3]=alphaimg2[2*(y*width+x)+1];
  8132. img[6*(y*width+x)+4]=0;
  8133. img[6*(y*width+x)+5]=0;
  8134. }
  8135. }
  8136. }
  8137. // Ok, and now only write out the active pixels to the .ppm file.
  8138. // (But only if the active pixels differ from the total pixels)
  8139. if( !(height == active_height && width == active_width) )
  8140. {
  8141. if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  8142. newimg=(uint8*)malloc(3*active_width*active_height*2);
  8143. else
  8144. newimg=(uint8*)malloc(3*active_width*active_height);
  8145. 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)
  8146. {
  8147. newalphaimg = (uint8*)malloc(active_width*active_height*2);
  8148. }
  8149. if(!newimg)
  8150. {
  8151. printf("Error: could not allocate memory\n");
  8152. exit(0);
  8153. }
  8154. // Convert from total area to active area:
  8155. for(yy = 0; yy<active_height; yy++)
  8156. {
  8157. for(xx = 0; xx< active_width; xx++)
  8158. {
  8159. if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
  8160. {
  8161. newimg[ (yy*active_width)*3 + xx*3 + 0 ] = img[ (yy*width)*3 + xx*3 + 0];
  8162. newimg[ (yy*active_width)*3 + xx*3 + 1 ] = img[ (yy*width)*3 + xx*3 + 1];
  8163. newimg[ (yy*active_width)*3 + xx*3 + 2 ] = img[ (yy*width)*3 + xx*3 + 2];
  8164. }
  8165. else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  8166. {
  8167. newimg[ (yy*active_width)*6 + xx*6 + 0 ] = img[ (yy*width)*6 + xx*6 + 0];
  8168. newimg[ (yy*active_width)*6 + xx*6 + 1 ] = img[ (yy*width)*6 + xx*6 + 1];
  8169. newimg[ (yy*active_width)*6 + xx*6 + 2 ] = img[ (yy*width)*6 + xx*6 + 2];
  8170. newimg[ (yy*active_width)*6 + xx*6 + 3 ] = img[ (yy*width)*6 + xx*6 + 3];
  8171. newimg[ (yy*active_width)*6 + xx*6 + 4 ] = img[ (yy*width)*6 + xx*6 + 4];
  8172. newimg[ (yy*active_width)*6 + xx*6 + 5 ] = img[ (yy*width)*6 + xx*6 + 5];
  8173. }
  8174. if(format==ETC2PACKAGE_R_NO_MIPMAPS)
  8175. {
  8176. newalphaimg[ ((yy*active_width) + xx)*2] = alphaimg[2*((yy*width) + xx)];
  8177. newalphaimg[ ((yy*active_width) + xx)*2+1] = alphaimg[2*((yy*width) + xx)+1];
  8178. }
  8179. if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  8180. {
  8181. newalphaimg[ ((yy*active_width) + xx)] = alphaimg[((yy*width) + xx)];
  8182. }
  8183. }
  8184. }
  8185. free(img);
  8186. img = newimg;
  8187. 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)
  8188. {
  8189. free(alphaimg);
  8190. alphaimg=newalphaimg;
  8191. }
  8192. if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  8193. {
  8194. free(alphaimg);
  8195. free(alphaimg2);
  8196. alphaimg = NULL;
  8197. alphaimg2 = NULL;
  8198. }
  8199. }
  8200. }
  8201. else
  8202. {
  8203. printf("Error: could not open <%s>.\n",srcfile);
  8204. exit(1);
  8205. }
  8206. height=active_height;
  8207. width=active_width;
  8208. fclose(f);
  8209. }
  8210. // Writes output file
  8211. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  8212. void writeOutputFile(char *dstfile, uint8* img, uint8* alphaimg, int width, int height)
  8213. {
  8214. char str[300];
  8215. if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
  8216. {
  8217. fWritePPM("tmp.ppm",width,height,img,8,false);
  8218. printf("Saved file tmp.ppm \n\n");
  8219. }
  8220. else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  8221. {
  8222. fWritePPM("tmp.ppm",width,height,img,16,false);
  8223. }
  8224. if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  8225. fWritePGM("alphaout.pgm",width,height,alphaimg,false,8);
  8226. if(format==ETC2PACKAGE_R_NO_MIPMAPS)
  8227. fWritePGM("alphaout.pgm",width,height,alphaimg,false,16);
  8228. // Delete destination file if it exists
  8229. if(fileExist(dstfile))
  8230. {
  8231. sprintf(str, "del %s\n",dstfile);
  8232. system(str);
  8233. }
  8234. int q = find_pos_of_extension(dstfile);
  8235. if(!strcmp(&dstfile[q],".ppm")&&format!=ETC2PACKAGE_R_NO_MIPMAPS)
  8236. {
  8237. // Already a .ppm file. Just rename.
  8238. sprintf(str,"move tmp.ppm %s\n",dstfile);
  8239. printf("Renaming destination file to %s\n",dstfile);
  8240. }
  8241. else
  8242. {
  8243. // Converting from .ppm to other file format
  8244. //
  8245. // Use your favorite command line image converter program,
  8246. // for instance Image Magick. Just make sure the syntax can
  8247. // be written as below:
  8248. //
  8249. // C:\magick convert source.ppm dest.jpg
  8250. //
  8251. if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  8252. {
  8253. // Somewhere after version 6.7.1-2 of ImageMagick the following command gives the wrong result due to a bug.
  8254. // sprintf(str,"composite -compose CopyOpacity alphaout.pgm tmp.ppm %s\n",dstfile);
  8255. // Instead we read the file and write a tga.
  8256. printf("Converting destination file from .ppm/.pgm to %s with alpha\n",dstfile);
  8257. int rw, rh;
  8258. unsigned char *pixelsRGB;
  8259. unsigned char *pixelsA;
  8260. fReadPPM("tmp.ppm", rw, rh, pixelsRGB, 8);
  8261. fReadPGM("alphaout.pgm", rw, rh, pixelsA, 8);
  8262. fWriteTGAfromRGBandA(dstfile, rw, rh, pixelsRGB, pixelsA, true);
  8263. free(pixelsRGB);
  8264. free(pixelsA);
  8265. sprintf(str,""); // Nothing to execute.
  8266. }
  8267. else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
  8268. {
  8269. sprintf(str,"magick convert alphaout.pgm %s\n",dstfile);
  8270. printf("Converting destination file from .pgm to %s\n",dstfile);
  8271. }
  8272. else
  8273. {
  8274. sprintf(str,"magick convert tmp.ppm %s\n",dstfile);
  8275. printf("Converting destination file from .ppm to %s\n",dstfile);
  8276. }
  8277. }
  8278. // Execute system call
  8279. system(str);
  8280. free(img);
  8281. if(alphaimg!=NULL)
  8282. free(alphaimg);
  8283. }
  8284. // Calculates the PSNR between two files
  8285. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  8286. double calculatePSNRfile(char *srcfile, uint8 *origimg, uint8* origalpha)
  8287. {
  8288. uint8 *alphaimg, *img;
  8289. int active_width, active_height;
  8290. uncompressFile(srcfile,img,alphaimg,active_width,active_height);
  8291. // calculate Mean Square Error (MSE)
  8292. double MSER=0,MSEG=0,MSEB=0,MSEA, PSNRR,PSNRG,PSNRA;
  8293. double MSE;
  8294. double wMSE;
  8295. double PSNR=0;
  8296. double wPSNR;
  8297. double err;
  8298. MSE = 0;
  8299. MSEA=0;
  8300. wMSE = 0;
  8301. int width=((active_width+3)/4)*4;
  8302. int height=((active_height+3)/4)*4;
  8303. int numpixels = 0;
  8304. for(int y=0;y<active_height;y++)
  8305. {
  8306. for(int x=0;x<active_width;x++)
  8307. {
  8308. if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
  8309. {
  8310. //we have regular color channels..
  8311. if((format != ETC2PACKAGE_RGBA1_NO_MIPMAPS && format != ETC2PACKAGE_sRGBA1_NO_MIPMAPS) || alphaimg[y*width + x] > 0)
  8312. {
  8313. err = img[y*active_width*3+x*3+0] - origimg[y*width*3+x*3+0];
  8314. MSE += ((err * err)/3.0);
  8315. wMSE += PERCEPTUAL_WEIGHT_R_SQUARED * (err*err);
  8316. err = img[y*active_width*3+x*3+1] - origimg[y*width*3+x*3+1];
  8317. MSE += ((err * err)/3.0);
  8318. wMSE += PERCEPTUAL_WEIGHT_G_SQUARED * (err*err);
  8319. err = img[y*active_width*3+x*3+2] - origimg[y*width*3+x*3+2];
  8320. MSE += ((err * err)/3.0);
  8321. wMSE += PERCEPTUAL_WEIGHT_B_SQUARED * (err*err);
  8322. numpixels++;
  8323. }
  8324. }
  8325. else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  8326. {
  8327. int rorig = (origimg[6*(y*width+x)+0]<<8)+origimg[6*(y*width+x)+1];
  8328. int rnew = ( img[6*(y*active_width+x)+0]<<8)+ img[6*(y*active_width+x)+1];
  8329. int gorig = (origimg[6*(y*width+x)+2]<<8)+origimg[6*(y*width+x)+3];
  8330. int gnew = ( img[6*(y*active_width+x)+2]<<8)+ img[6*(y*active_width+x)+3];
  8331. err=rorig-rnew;
  8332. MSER+=(err*err);
  8333. err=gorig-gnew;
  8334. MSEG+=(err*err);
  8335. }
  8336. else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
  8337. {
  8338. int aorig = (((int)origalpha[2*(y*width+x)+0])<<8)+origalpha[2*(y*width+x)+1];
  8339. int anew = (((int)alphaimg[2*(y*active_width+x)+0])<<8)+alphaimg[2*(y*active_width+x)+1];
  8340. err=aorig-anew;
  8341. MSEA+=(err*err);
  8342. }
  8343. }
  8344. }
  8345. if(format == ETC2PACKAGE_RGBA1_NO_MIPMAPS || format == ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  8346. {
  8347. MSE = MSE / (1.0 * numpixels);
  8348. wMSE = wMSE / (1.0 * numpixels);
  8349. PSNR = 10*log((1.0*255*255)/MSE)/log(10.0);
  8350. wPSNR = 10*log((1.0*255*255)/wMSE)/log(10.0);
  8351. printf("PSNR only calculated on pixels where compressed alpha > 0\n");
  8352. printf("color PSNR: %lf\nweighted PSNR: %lf\n",PSNR,wPSNR);
  8353. }
  8354. else if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
  8355. {
  8356. MSE = MSE / (active_width * active_height);
  8357. wMSE = wMSE / (active_width * active_height);
  8358. PSNR = 10*log((1.0*255*255)/MSE)/log(10.0);
  8359. wPSNR = 10*log((1.0*255*255)/wMSE)/log(10.0);
  8360. if(format == ETC2PACKAGE_RGBA_NO_MIPMAPS || format == ETC2PACKAGE_sRGBA_NO_MIPMAPS)
  8361. printf("PSNR only calculated on RGB, not on alpha\n");
  8362. printf("color PSNR: %lf\nweighted PSNR: %lf\n",PSNR,wPSNR);
  8363. }
  8364. else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  8365. {
  8366. MSER = MSER / (active_width * active_height);
  8367. MSEG = MSEG / (active_width * active_height);
  8368. PSNRR = 10*log((1.0*65535*65535)/MSER)/log(10.0);
  8369. PSNRG = 10*log((1.0*65535*65535)/MSEG)/log(10.0);
  8370. printf("red PSNR: %lf\ngreen PSNR: %lf\n",PSNRR,PSNRG);
  8371. }
  8372. else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
  8373. {
  8374. MSEA = MSEA / (active_width * active_height);
  8375. PSNRA = 10*log((1.0*65535.0*65535.0)/MSEA)/log(10.0);
  8376. printf("PSNR: %lf\n",PSNRA);
  8377. }
  8378. free(img);
  8379. return PSNR;
  8380. }
  8381. //// Exhaustive code starts here.
  8382. #if EXHAUSTIVE_CODE_ACTIVE
  8383. // Precomutes a table that is used when compressing a block exhaustively
  8384. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  8385. 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)
  8386. {
  8387. int table;
  8388. int index;
  8389. int orig[3],approx[3][4];
  8390. int x;
  8391. int intensity_modifier;
  8392. const int *table_indices;
  8393. int good_enough_to_test;
  8394. unsigned int err[4];
  8395. unsigned int err_this_table_upper;
  8396. unsigned int err_this_table_lower;
  8397. unsigned int err_this_table_left;
  8398. unsigned int err_this_table_right;
  8399. // If the error in the red and green component is already larger than best_err for all 8 tables in
  8400. // all of upper, lower, left and right, this combination of red and green will never be used in
  8401. // the optimal color configuration. Therefore we can avoid testing all the blue colors for this
  8402. // combination.
  8403. good_enough_to_test = false;
  8404. for(table=0;table<8;table++) // try all the 8 tables.
  8405. {
  8406. table_indices = &compressParamsFast[table*4];
  8407. intensity_modifier = table_indices[0];
  8408. approx[1][0]=CLAMP(0, avg_color[1]+intensity_modifier,255);
  8409. intensity_modifier = table_indices[1];
  8410. approx[1][1]=CLAMP(0, avg_color[1]+intensity_modifier,255);
  8411. intensity_modifier = table_indices[2];
  8412. approx[1][2]=CLAMP(0, avg_color[1]+intensity_modifier,255);
  8413. intensity_modifier = table_indices[3];
  8414. approx[1][3]=CLAMP(0, avg_color[1]+intensity_modifier,255);
  8415. err_this_table_upper = 0;
  8416. err_this_table_lower = 0;
  8417. err_this_table_left = 0;
  8418. err_this_table_right = 0;
  8419. for(x=0; x<4; x++)
  8420. {
  8421. orig[0]=block[x*4];
  8422. orig[1]=block[x*4+1];
  8423. orig[2]=block[x*4+2];
  8424. for(index=0;index<4;index++)
  8425. {
  8426. err[index] = precalc_err_UL_R[table*4*4+x*4+index]
  8427. + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000 * SQUARE(approx[1][index]-orig[1]);
  8428. precalc_err_UL_RG[table*4*4+x*4+index] = err[index];
  8429. }
  8430. if(err[0] > err[1])
  8431. err[0] = err[1];
  8432. if(err[2] > err[3])
  8433. err[2] = err[3];
  8434. if(err[0] > err[2])
  8435. err[0] = err[2];
  8436. err_this_table_upper+=err[0];
  8437. err_this_table_left+=err[0];
  8438. }
  8439. for(x=4; x<8; x++)
  8440. {
  8441. orig[0]=block[x*4];
  8442. orig[1]=block[x*4+1];
  8443. orig[2]=block[x*4+2];
  8444. for(index=0;index<4;index++)
  8445. {
  8446. err[index] = precalc_err_UR_R[table*4*4+(x-4)*4+index]
  8447. + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000 * SQUARE(approx[1][index]-orig[1]);
  8448. precalc_err_UR_RG[table*4*4+(x-4)*4+index] = err[index];
  8449. }
  8450. if(err[0] > err[1])
  8451. err[0] = err[1];
  8452. if(err[2] > err[3])
  8453. err[2] = err[3];
  8454. if(err[0] > err[2])
  8455. err[0] = err[2];
  8456. err_this_table_upper+=err[0];
  8457. err_this_table_right+=err[0];
  8458. }
  8459. for(x=8; x<12; x++)
  8460. {
  8461. orig[0]=block[x*4];
  8462. orig[1]=block[x*4+1];
  8463. orig[2]=block[x*4+2];
  8464. for(index=0;index<4;index++)
  8465. {
  8466. err[index] = precalc_err_LL_R[table*4*4+(x-8)*4+index]
  8467. + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000 * SQUARE(approx[1][index]-orig[1]);
  8468. precalc_err_LL_RG[table*4*4+(x-8)*4+index] = err[index];
  8469. }
  8470. if(err[0] > err[1])
  8471. err[0] = err[1];
  8472. if(err[2] > err[3])
  8473. err[2] = err[3];
  8474. if(err[0] > err[2])
  8475. err[0] = err[2];
  8476. err_this_table_lower+=err[0];
  8477. err_this_table_left+=err[0];
  8478. }
  8479. for(x=12; x<16; x++)
  8480. {
  8481. orig[0]=block[x*4];
  8482. orig[1]=block[x*4+1];
  8483. orig[2]=block[x*4+2];
  8484. for(index=0;index<4;index++)
  8485. {
  8486. err[index] = precalc_err_LR_R[table*4*4+(x-12)*4+index]
  8487. + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000 * SQUARE(approx[1][index]-orig[1]);
  8488. precalc_err_LR_RG[table*4*4+(x-12)*4+index] = err[index];
  8489. }
  8490. if(err[0] > err[1])
  8491. err[0] = err[1];
  8492. if(err[2] > err[3])
  8493. err[2] = err[3];
  8494. if(err[0] > err[2])
  8495. err[0] = err[2];
  8496. err_this_table_lower+=err[0];
  8497. err_this_table_right+=err[0];
  8498. }
  8499. if(err_this_table_upper < best_err)
  8500. good_enough_to_test = true;
  8501. if(err_this_table_lower < best_err)
  8502. good_enough_to_test = true;
  8503. if(err_this_table_left < best_err)
  8504. good_enough_to_test = true;
  8505. if(err_this_table_right < best_err)
  8506. good_enough_to_test = true;
  8507. }
  8508. return good_enough_to_test;
  8509. }
  8510. #endif
  8511. #if EXHAUSTIVE_CODE_ACTIVE
  8512. // Precomutes a table that is used when compressing a block exhaustively
  8513. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  8514. 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)
  8515. {
  8516. int table;
  8517. int index;
  8518. int orig[3],approx[3][4];
  8519. int x;
  8520. int intensity_modifier;
  8521. const int *table_indices;
  8522. int good_enough_to_test;
  8523. unsigned int err[4];
  8524. unsigned int err_this_table_upper;
  8525. unsigned int err_this_table_lower;
  8526. unsigned int err_this_table_left;
  8527. unsigned int err_this_table_right;
  8528. // If the error in the red and green component is already larger than best_err for all 8 tables in
  8529. // all of upper, lower, left and right, this combination of red and green will never be used in
  8530. // the optimal color configuration. Therefore we can avoid testing all the blue colors for this
  8531. // combination.
  8532. good_enough_to_test = false;
  8533. for(table=0;table<8;table++) // try all the 8 tables.
  8534. {
  8535. table_indices = &compressParamsFast[table*4];
  8536. intensity_modifier = table_indices[0];
  8537. approx[1][0]=CLAMP(0, avg_color[1]+intensity_modifier,255);
  8538. intensity_modifier = table_indices[1];
  8539. approx[1][1]=CLAMP(0, avg_color[1]+intensity_modifier,255);
  8540. intensity_modifier = table_indices[2];
  8541. approx[1][2]=CLAMP(0, avg_color[1]+intensity_modifier,255);
  8542. intensity_modifier = table_indices[3];
  8543. approx[1][3]=CLAMP(0, avg_color[1]+intensity_modifier,255);
  8544. err_this_table_upper = 0;
  8545. err_this_table_lower = 0;
  8546. err_this_table_left = 0;
  8547. err_this_table_right = 0;
  8548. for(x=0; x<4; x++)
  8549. {
  8550. orig[0]=block[x*4];
  8551. orig[1]=block[x*4+1];
  8552. orig[2]=block[x*4+2];
  8553. for(index=0;index<4;index++)
  8554. {
  8555. err[index] = precalc_err_UL_R[table*4*4+x*4+index]+SQUARE(approx[1][index]-orig[1]);
  8556. precalc_err_UL_RG[table*4*4+x*4+index] = err[index];
  8557. }
  8558. if(err[0] > err[1])
  8559. err[0] = err[1];
  8560. if(err[2] > err[3])
  8561. err[2] = err[3];
  8562. if(err[0] > err[2])
  8563. err[0] = err[2];
  8564. err_this_table_upper+=err[0];
  8565. err_this_table_left+=err[0];
  8566. }
  8567. for(x=4; x<8; x++)
  8568. {
  8569. orig[0]=block[x*4];
  8570. orig[1]=block[x*4+1];
  8571. orig[2]=block[x*4+2];
  8572. for(index=0;index<4;index++)
  8573. {
  8574. err[index] = precalc_err_UR_R[table*4*4+(x-4)*4+index]+SQUARE(approx[1][index]-orig[1]);
  8575. precalc_err_UR_RG[table*4*4+(x-4)*4+index] = err[index];
  8576. }
  8577. if(err[0] > err[1])
  8578. err[0] = err[1];
  8579. if(err[2] > err[3])
  8580. err[2] = err[3];
  8581. if(err[0] > err[2])
  8582. err[0] = err[2];
  8583. err_this_table_upper+=err[0];
  8584. err_this_table_right+=err[0];
  8585. }
  8586. for(x=8; x<12; x++)
  8587. {
  8588. orig[0]=block[x*4];
  8589. orig[1]=block[x*4+1];
  8590. orig[2]=block[x*4+2];
  8591. for(index=0;index<4;index++)
  8592. {
  8593. err[index] = precalc_err_LL_R[table*4*4+(x-8)*4+index]+SQUARE(approx[1][index]-orig[1]);
  8594. precalc_err_LL_RG[table*4*4+(x-8)*4+index] = err[index];
  8595. }
  8596. if(err[0] > err[1])
  8597. err[0] = err[1];
  8598. if(err[2] > err[3])
  8599. err[2] = err[3];
  8600. if(err[0] > err[2])
  8601. err[0] = err[2];
  8602. err_this_table_lower+=err[0];
  8603. err_this_table_left+=err[0];
  8604. }
  8605. for(x=12; x<16; x++)
  8606. {
  8607. orig[0]=block[x*4];
  8608. orig[1]=block[x*4+1];
  8609. orig[2]=block[x*4+2];
  8610. for(index=0;index<4;index++)
  8611. {
  8612. err[index] = precalc_err_LR_R[table*4*4+(x-12)*4+index]+SQUARE(approx[1][index]-orig[1]);
  8613. precalc_err_LR_RG[table*4*4+(x-12)*4+index] = err[index];
  8614. }
  8615. if(err[0] > err[1])
  8616. err[0] = err[1];
  8617. if(err[2] > err[3])
  8618. err[2] = err[3];
  8619. if(err[0] > err[2])
  8620. err[0] = err[2];
  8621. err_this_table_lower+=err[0];
  8622. err_this_table_right+=err[0];
  8623. }
  8624. if(err_this_table_upper < best_err)
  8625. good_enough_to_test = true;
  8626. if(err_this_table_lower < best_err)
  8627. good_enough_to_test = true;
  8628. if(err_this_table_left < best_err)
  8629. good_enough_to_test = true;
  8630. if(err_this_table_right < best_err)
  8631. good_enough_to_test = true;
  8632. }
  8633. return good_enough_to_test;
  8634. }
  8635. #endif
  8636. #if EXHAUSTIVE_CODE_ACTIVE
  8637. // Precomutes a table that is used when compressing a block exhaustively
  8638. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  8639. 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)
  8640. {
  8641. int table;
  8642. int index;
  8643. int orig[3],approx[3][4];
  8644. int x;
  8645. int intensity_modifier;
  8646. const int *table_indices;
  8647. unsigned int err[4];
  8648. unsigned int err_this_table_upper;
  8649. unsigned int err_this_table_lower;
  8650. unsigned int err_this_table_left;
  8651. unsigned int err_this_table_right;
  8652. int good_enough_to_test;
  8653. good_enough_to_test = false;
  8654. for(table=0;table<8;table++) // try all the 8 tables.
  8655. {
  8656. err_this_table_upper = 0;
  8657. err_this_table_lower = 0;
  8658. err_this_table_left = 0;
  8659. err_this_table_right = 0;
  8660. table_indices = &compressParamsFast[table*4];
  8661. intensity_modifier = table_indices[0];
  8662. approx[0][0]=CLAMP(0, avg_color[0]+intensity_modifier,255);
  8663. intensity_modifier = table_indices[1];
  8664. approx[0][1]=CLAMP(0, avg_color[0]+intensity_modifier,255);
  8665. intensity_modifier = table_indices[2];
  8666. approx[0][2]=CLAMP(0, avg_color[0]+intensity_modifier,255);
  8667. intensity_modifier = table_indices[3];
  8668. approx[0][3]=CLAMP(0, avg_color[0]+intensity_modifier,255);
  8669. for(x=0; x<4; x++)
  8670. {
  8671. orig[0]=block[x*4];
  8672. orig[1]=block[x*4+1];
  8673. orig[2]=block[x*4+2];
  8674. for(index=0;index<4;index++)
  8675. {
  8676. err[index]=PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(approx[0][index]-orig[0]);
  8677. precalc_err_UL_R[table*4*4+x*4+index]=err[index];
  8678. }
  8679. if(err[0] > err[1])
  8680. err[0] = err[1];
  8681. if(err[2] > err[3])
  8682. err[2] = err[3];
  8683. if(err[0] > err[2])
  8684. err[0] = err[2];
  8685. err_this_table_upper+=err[0];
  8686. err_this_table_left+=err[0];
  8687. }
  8688. for(x=4; x<8; x++)
  8689. {
  8690. orig[0]=block[x*4];
  8691. orig[1]=block[x*4+1];
  8692. orig[2]=block[x*4+2];
  8693. for(index=0;index<4;index++)
  8694. {
  8695. err[index]=PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(approx[0][index]-orig[0]);
  8696. precalc_err_UR_R[table*4*4+(x-4)*4+index]=err[index];
  8697. }
  8698. if(err[0] > err[1])
  8699. err[0] = err[1];
  8700. if(err[2] > err[3])
  8701. err[2] = err[3];
  8702. if(err[0] > err[2])
  8703. err[0] = err[2];
  8704. err_this_table_upper+=err[0];
  8705. err_this_table_right+=err[0];
  8706. }
  8707. for(x=8; x<12; x++)
  8708. {
  8709. orig[0]=block[x*4];
  8710. orig[1]=block[x*4+1];
  8711. orig[2]=block[x*4+2];
  8712. for(index=0;index<4;index++)
  8713. {
  8714. err[index]=PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(approx[0][index]-orig[0]);
  8715. precalc_err_LL_R[table*4*4+(x-8)*4+index]=err[index];
  8716. }
  8717. if(err[0] > err[1])
  8718. err[0] = err[1];
  8719. if(err[2] > err[3])
  8720. err[2] = err[3];
  8721. if(err[0] > err[2])
  8722. err[0] = err[2];
  8723. err_this_table_lower+=err[0];
  8724. err_this_table_left+=err[0];
  8725. }
  8726. for(x=12; x<16; x++)
  8727. {
  8728. orig[0]=block[x*4];
  8729. orig[1]=block[x*4+1];
  8730. orig[2]=block[x*4+2];
  8731. for(index=0;index<4;index++)
  8732. {
  8733. err[index]=PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(approx[0][index]-orig[0]);
  8734. precalc_err_LR_R[table*4*4+(x-12)*4+index]=err[index];
  8735. }
  8736. if(err[0] > err[1])
  8737. err[0] = err[1];
  8738. if(err[2] > err[3])
  8739. err[2] = err[3];
  8740. if(err[0] > err[2])
  8741. err[0] = err[2];
  8742. err_this_table_lower+=err[0];
  8743. err_this_table_right+=err[0];
  8744. }
  8745. if(err_this_table_upper < best_err)
  8746. good_enough_to_test = true;
  8747. if(err_this_table_lower < best_err)
  8748. good_enough_to_test = true;
  8749. if(err_this_table_left < best_err)
  8750. good_enough_to_test = true;
  8751. if(err_this_table_right < best_err)
  8752. good_enough_to_test = true;
  8753. }
  8754. return good_enough_to_test;
  8755. }
  8756. #endif
  8757. #if EXHAUSTIVE_CODE_ACTIVE
  8758. // Precomutes a table that is used when compressing a block exhaustively
  8759. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  8760. 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)
  8761. {
  8762. int table;
  8763. int index;
  8764. int orig[3],approx[3][4];
  8765. int x;
  8766. int intensity_modifier;
  8767. const int *table_indices;
  8768. unsigned int err[4];
  8769. unsigned int err_this_table_upper;
  8770. unsigned int err_this_table_lower;
  8771. unsigned int err_this_table_left;
  8772. unsigned int err_this_table_right;
  8773. int good_enough_to_test;
  8774. good_enough_to_test = false;
  8775. for(table=0;table<8;table++) // try all the 8 tables.
  8776. {
  8777. err_this_table_upper = 0;
  8778. err_this_table_lower = 0;
  8779. err_this_table_left = 0;
  8780. err_this_table_right = 0;
  8781. table_indices = &compressParamsFast[table*4];
  8782. intensity_modifier = table_indices[0];
  8783. approx[0][0]=CLAMP(0, avg_color[0]+intensity_modifier,255);
  8784. intensity_modifier = table_indices[1];
  8785. approx[0][1]=CLAMP(0, avg_color[0]+intensity_modifier,255);
  8786. intensity_modifier = table_indices[2];
  8787. approx[0][2]=CLAMP(0, avg_color[0]+intensity_modifier,255);
  8788. intensity_modifier = table_indices[3];
  8789. approx[0][3]=CLAMP(0, avg_color[0]+intensity_modifier,255);
  8790. for(x=0; x<4; x++)
  8791. {
  8792. orig[0]=block[x*4];
  8793. orig[1]=block[x*4+1];
  8794. orig[2]=block[x*4+2];
  8795. for(index=0;index<4;index++)
  8796. {
  8797. err[index]=SQUARE(approx[0][index]-orig[0]);
  8798. precalc_err_UL_R[table*4*4+x*4+index]=err[index];
  8799. }
  8800. if(err[0] > err[1])
  8801. err[0] = err[1];
  8802. if(err[2] > err[3])
  8803. err[2] = err[3];
  8804. if(err[0] > err[2])
  8805. err[0] = err[2];
  8806. err_this_table_upper+=err[0];
  8807. err_this_table_left+=err[0];
  8808. }
  8809. for(x=4; x<8; x++)
  8810. {
  8811. orig[0]=block[x*4];
  8812. orig[1]=block[x*4+1];
  8813. orig[2]=block[x*4+2];
  8814. for(index=0;index<4;index++)
  8815. {
  8816. err[index]=SQUARE(approx[0][index]-orig[0]);
  8817. precalc_err_UR_R[table*4*4+(x-4)*4+index]=err[index];
  8818. }
  8819. if(err[0] > err[1])
  8820. err[0] = err[1];
  8821. if(err[2] > err[3])
  8822. err[2] = err[3];
  8823. if(err[0] > err[2])
  8824. err[0] = err[2];
  8825. err_this_table_upper+=err[0];
  8826. err_this_table_right+=err[0];
  8827. }
  8828. for(x=8; x<12; x++)
  8829. {
  8830. orig[0]=block[x*4];
  8831. orig[1]=block[x*4+1];
  8832. orig[2]=block[x*4+2];
  8833. for(index=0;index<4;index++)
  8834. {
  8835. err[index]=SQUARE(approx[0][index]-orig[0]);
  8836. precalc_err_LL_R[table*4*4+(x-8)*4+index]=err[index];
  8837. }
  8838. if(err[0] > err[1])
  8839. err[0] = err[1];
  8840. if(err[2] > err[3])
  8841. err[2] = err[3];
  8842. if(err[0] > err[2])
  8843. err[0] = err[2];
  8844. err_this_table_lower+=err[0];
  8845. err_this_table_left+=err[0];
  8846. }
  8847. for(x=12; x<16; x++)
  8848. {
  8849. orig[0]=block[x*4];
  8850. orig[1]=block[x*4+1];
  8851. orig[2]=block[x*4+2];
  8852. for(index=0;index<4;index++)
  8853. {
  8854. err[index]=SQUARE(approx[0][index]-orig[0]);
  8855. precalc_err_LR_R[table*4*4+(x-12)*4+index]=err[index];
  8856. }
  8857. if(err[0] > err[1])
  8858. err[0] = err[1];
  8859. if(err[2] > err[3])
  8860. err[2] = err[3];
  8861. if(err[0] > err[2])
  8862. err[0] = err[2];
  8863. err_this_table_lower+=err[0];
  8864. err_this_table_right+=err[0];
  8865. }
  8866. if(err_this_table_upper < best_err)
  8867. good_enough_to_test = true;
  8868. if(err_this_table_lower < best_err)
  8869. good_enough_to_test = true;
  8870. if(err_this_table_left < best_err)
  8871. good_enough_to_test = true;
  8872. if(err_this_table_right < best_err)
  8873. good_enough_to_test = true;
  8874. }
  8875. return good_enough_to_test;
  8876. }
  8877. #endif
  8878. #if EXHAUSTIVE_CODE_ACTIVE
  8879. // Tries all index-tables, used when compressing a block exhaustively
  8880. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  8881. 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)
  8882. {
  8883. unsigned int err_this_table_upper;
  8884. unsigned int err_this_table_lower;
  8885. unsigned int err_this_table_left;
  8886. unsigned int err_this_table_right;
  8887. int orig[3],approx[4];
  8888. int err[4];
  8889. err_upper = 3*255*255*16;
  8890. err_lower = 3*255*255*16;
  8891. err_left = 3*255*255*16;
  8892. err_right = 3*255*255*16;
  8893. #define ONE_PIXEL_UL(table_nbr,xx)\
  8894. orig[0]=block_2x2[xx*4];\
  8895. orig[1]=block_2x2[xx*4+1];\
  8896. orig[2]=block_2x2[xx*4+2];\
  8897. /* unrolled loop for(index=0;index<4;index++)*/\
  8898. err[0]=precalc_err_UL_RG[table_nbr*4*4+xx*4+0] + square_table[approx[0]-orig[2]];\
  8899. err[1]=precalc_err_UL_RG[table_nbr*4*4+xx*4+1] + square_table[approx[1]-orig[2]];\
  8900. err[2]=precalc_err_UL_RG[table_nbr*4*4+xx*4+2] + square_table[approx[2]-orig[2]];\
  8901. err[3]=precalc_err_UL_RG[table_nbr*4*4+xx*4+3] + square_table[approx[3]-orig[2]];\
  8902. /* end unrolled loop*/\
  8903. if(err[0] > err[1])\
  8904. err[0] = err[1];\
  8905. if(err[2] > err[3])\
  8906. err[2] = err[3];\
  8907. if(err[0] > err[2])\
  8908. err[0] = err[2];\
  8909. err_this_table_upper+=err[0];\
  8910. err_this_table_left+=err[0];\
  8911. #define ONE_PIXEL_UR(table_nbr,xx)\
  8912. orig[0]=block_2x2[xx*4];\
  8913. orig[1]=block_2x2[xx*4+1];\
  8914. orig[2]=block_2x2[xx*4+2];\
  8915. /* unrolled loop for(index=0;index<4;index++)*/\
  8916. err[0]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+0] + square_table[approx[0]-orig[2]];\
  8917. err[1]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+1] + square_table[approx[1]-orig[2]];\
  8918. err[2]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+2] + square_table[approx[2]-orig[2]];\
  8919. err[3]=precalc_err_UR_RG[table_nbr*4*4+(xx-4)*4+3] + square_table[approx[3]-orig[2]];\
  8920. /* end unrolled loop */\
  8921. if(err[0] > err[1])\
  8922. err[0] = err[1];\
  8923. if(err[2] > err[3])\
  8924. err[2] = err[3];\
  8925. if(err[0] > err[2])\
  8926. err[0] = err[2];\
  8927. err_this_table_upper+=err[0];\
  8928. err_this_table_right+=err[0];
  8929. #define ONE_PIXEL_LL(table_nbr,xx)\
  8930. orig[0]=block_2x2[xx*4];\
  8931. orig[1]=block_2x2[xx*4+1];\
  8932. orig[2]=block_2x2[xx*4+2];\
  8933. /* unrolled loop for(index=0;index<4;index++)*/\
  8934. err[0]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+0] + square_table[approx[0]-orig[2]];\
  8935. err[1]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+1] + square_table[approx[1]-orig[2]];\
  8936. err[2]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+2] + square_table[approx[2]-orig[2]];\
  8937. err[3]=precalc_err_LL_RG[table_nbr*4*4+(xx-8)*4+3] + square_table[approx[3]-orig[2]];\
  8938. /* end unrolled loop*/\
  8939. if(err[0] > err[1])\
  8940. err[0] = err[1];\
  8941. if(err[2] > err[3])\
  8942. err[2] = err[3];\
  8943. if(err[0] > err[2])\
  8944. err[0] = err[2];\
  8945. err_this_table_lower+=err[0];\
  8946. err_this_table_left+=err[0];\
  8947. #define ONE_PIXEL_LR(table_nbr,xx)\
  8948. orig[0]=block_2x2[xx*4];\
  8949. orig[1]=block_2x2[xx*4+1];\
  8950. orig[2]=block_2x2[xx*4+2];\
  8951. /* unrolled loop for(index=0;index<4;index++)*/\
  8952. err[0]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+0] + square_table[approx[0]-orig[2]];\
  8953. err[1]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+1] + square_table[approx[1]-orig[2]];\
  8954. err[2]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+2] + square_table[approx[2]-orig[2]];\
  8955. err[3]=precalc_err_LR_RG[table_nbr*4*4+(xx-12)*4+3] + square_table[approx[3]-orig[2]];\
  8956. /* end unrolled loop*/\
  8957. if(err[0] > err[1])\
  8958. err[0] = err[1];\
  8959. if(err[2] > err[3])\
  8960. err[2] = err[3];\
  8961. if(err[0] > err[2])\
  8962. err[0] = err[2];\
  8963. err_this_table_lower+=err[0];\
  8964. err_this_table_right+=err[0];\
  8965. #define ONE_TABLE_3(table_nbr)\
  8966. err_this_table_upper = 0;\
  8967. err_this_table_lower = 0;\
  8968. err_this_table_left = 0;\
  8969. err_this_table_right = 0;\
  8970. approx[0]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+0]+255];\
  8971. approx[1]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+1]+255];\
  8972. approx[2]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+2]+255];\
  8973. approx[3]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+3]+255];\
  8974. /* unroll loop for(xx=0; xx<4; xx++) */\
  8975. ONE_PIXEL_UL(table_nbr,0)\
  8976. ONE_PIXEL_UL(table_nbr,1)\
  8977. ONE_PIXEL_UL(table_nbr,2)\
  8978. ONE_PIXEL_UL(table_nbr,3)\
  8979. /* end unroll loop */\
  8980. /* unroll loop for(xx=4; xx<8; xx++) */\
  8981. ONE_PIXEL_LR(table_nbr,12)\
  8982. ONE_PIXEL_LR(table_nbr,13)\
  8983. ONE_PIXEL_LR(table_nbr,14)\
  8984. ONE_PIXEL_LR(table_nbr,15)\
  8985. /* end unroll loop */\
  8986. /* If error in the top left 2x2 pixel area is already larger than the best error, and */\
  8987. /* The same is true for the bottom right 2x2 pixel area, this combination of table and color */\
  8988. /* can never be part of an optimal solution and therefore we do not need to test the other */\
  8989. /* two 2x2 pixel areas */\
  8990. if((err_this_table_upper<best_err)||(err_this_table_lower<best_err))\
  8991. {\
  8992. /* unroll loop for(xx=4; xx<8; xx++) */\
  8993. ONE_PIXEL_UR(table_nbr,4)\
  8994. ONE_PIXEL_UR(table_nbr,5)\
  8995. ONE_PIXEL_UR(table_nbr,6)\
  8996. ONE_PIXEL_UR(table_nbr,7)\
  8997. /* end unroll loop */\
  8998. /* unroll loop for(xx=4; xx<8; xx++) */\
  8999. ONE_PIXEL_LL(table_nbr,8)\
  9000. ONE_PIXEL_LL(table_nbr,9)\
  9001. ONE_PIXEL_LL(table_nbr,10)\
  9002. ONE_PIXEL_LL(table_nbr,11)\
  9003. /* end unroll loop */\
  9004. if(err_this_table_upper<err_upper)\
  9005. err_upper = err_this_table_upper;\
  9006. if(err_this_table_lower<err_lower)\
  9007. err_lower = err_this_table_lower;\
  9008. if(err_this_table_left<err_left)\
  9009. err_left = err_this_table_left;\
  9010. if(err_this_table_right<err_right)\
  9011. err_right = err_this_table_right;\
  9012. }\
  9013. /*unroll loop for(table_nbr=0;table_nbr<8;table_nbr++)*/
  9014. ONE_TABLE_3(0);
  9015. ONE_TABLE_3(1);
  9016. ONE_TABLE_3(2);
  9017. ONE_TABLE_3(3);
  9018. ONE_TABLE_3(4);
  9019. ONE_TABLE_3(5);
  9020. ONE_TABLE_3(6);
  9021. ONE_TABLE_3(7);
  9022. /*end unroll loop*/
  9023. }
  9024. #endif
  9025. #if EXHAUSTIVE_CODE_ACTIVE
  9026. // Tries all index-tables, used when compressing a block exhaustively using perceptual error measure
  9027. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  9028. 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)
  9029. {
  9030. unsigned int err_this_table_upper;
  9031. unsigned int err_this_table_lower;
  9032. unsigned int err_this_table_left;
  9033. unsigned int err_this_table_right;
  9034. int orig[3],approx[4];
  9035. int err[4];
  9036. err_upper = MAXERR1000;
  9037. err_lower = MAXERR1000;
  9038. err_left = MAXERR1000;
  9039. err_right =MAXERR1000;
  9040. #define ONE_PIXEL_UL_PERCEP(table_nbr,xx)\
  9041. orig[0]=block_2x2[xx*4];\
  9042. orig[1]=block_2x2[xx*4+1];\
  9043. orig[2]=block_2x2[xx*4+2];\
  9044. /* unrolled loop for(index=0;index<4;index++)*/\
  9045. err[0]=precalc_err_UL_RG[table_nbr*4*4+xx*4+0] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[0]-orig[2]];\
  9046. err[1]=precalc_err_UL_RG[table_nbr*4*4+xx*4+1] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[1]-orig[2]];\
  9047. err[2]=precalc_err_UL_RG[table_nbr*4*4+xx*4+2] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[2]-orig[2]];\
  9048. err[3]=precalc_err_UL_RG[table_nbr*4*4+xx*4+3] + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[approx[3]-orig[2]];\
  9049. /* end unrolled loop*/\
  9050. if(err[0] > err[1])\
  9051. err[0] = err[1];\
  9052. if(err[2] > err[3])\
  9053. err[2] = err[3];\
  9054. if(err[0] > err[2])\
  9055. err[0] = err[2];\
  9056. err_this_table_upper+=err[0];\
  9057. err_this_table_left+=err[0];\
  9058. #define ONE_PIXEL_UR_PERCEP(table_nbr,xx)\
  9059. orig[0]=block_2x2[xx*4];\
  9060. orig[1]=block_2x2[xx*4+1];\
  9061. orig[2]=block_2x2[xx*4+2];\
  9062. /* unrolled loop for(index=0;index<4;index++)*/\
  9063. 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]];\
  9064. 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]];\
  9065. 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]];\
  9066. 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]];\
  9067. /* end unrolled loop */\
  9068. if(err[0] > err[1])\
  9069. err[0] = err[1];\
  9070. if(err[2] > err[3])\
  9071. err[2] = err[3];\
  9072. if(err[0] > err[2])\
  9073. err[0] = err[2];\
  9074. err_this_table_upper+=err[0];\
  9075. err_this_table_right+=err[0];
  9076. #define ONE_PIXEL_LL_PERCEP(table_nbr,xx)\
  9077. orig[0]=block_2x2[xx*4];\
  9078. orig[1]=block_2x2[xx*4+1];\
  9079. orig[2]=block_2x2[xx*4+2];\
  9080. /* unrolled loop for(index=0;index<4;index++)*/\
  9081. 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]];\
  9082. 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]];\
  9083. 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]];\
  9084. 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]];\
  9085. /* end unrolled loop*/\
  9086. if(err[0] > err[1])\
  9087. err[0] = err[1];\
  9088. if(err[2] > err[3])\
  9089. err[2] = err[3];\
  9090. if(err[0] > err[2])\
  9091. err[0] = err[2];\
  9092. err_this_table_lower+=err[0];\
  9093. err_this_table_left+=err[0];\
  9094. #define ONE_PIXEL_LR_PERCEP(table_nbr,xx)\
  9095. orig[0]=block_2x2[xx*4];\
  9096. orig[1]=block_2x2[xx*4+1];\
  9097. orig[2]=block_2x2[xx*4+2];\
  9098. /* unrolled loop for(index=0;index<4;index++)*/\
  9099. 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]];\
  9100. 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]];\
  9101. 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]];\
  9102. 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]];\
  9103. /* end unrolled loop*/\
  9104. if(err[0] > err[1])\
  9105. err[0] = err[1];\
  9106. if(err[2] > err[3])\
  9107. err[2] = err[3];\
  9108. if(err[0] > err[2])\
  9109. err[0] = err[2];\
  9110. err_this_table_lower+=err[0];\
  9111. err_this_table_right+=err[0];\
  9112. #define ONE_TABLE_3_PERCEP(table_nbr)\
  9113. err_this_table_upper = 0;\
  9114. err_this_table_lower = 0;\
  9115. err_this_table_left = 0;\
  9116. err_this_table_right = 0;\
  9117. approx[0]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+0]+255];\
  9118. approx[1]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+1]+255];\
  9119. approx[2]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+2]+255];\
  9120. approx[3]=clamp_table_plus_255[color_quant1[2]+compressParamsFast[table_nbr*4+3]+255];\
  9121. /* unroll loop for(xx=0; xx<4; xx++) */\
  9122. ONE_PIXEL_UL_PERCEP(table_nbr,0)\
  9123. ONE_PIXEL_UL_PERCEP(table_nbr,1)\
  9124. ONE_PIXEL_UL_PERCEP(table_nbr,2)\
  9125. ONE_PIXEL_UL_PERCEP(table_nbr,3)\
  9126. /* end unroll loop */\
  9127. /* unroll loop for(xx=4; xx<8; xx++) */\
  9128. ONE_PIXEL_LR_PERCEP(table_nbr,12)\
  9129. ONE_PIXEL_LR_PERCEP(table_nbr,13)\
  9130. ONE_PIXEL_LR_PERCEP(table_nbr,14)\
  9131. ONE_PIXEL_LR_PERCEP(table_nbr,15)\
  9132. /* end unroll loop */\
  9133. /* If error in the top left 2x2 pixel area is already larger than the best error, and */\
  9134. /* The same is true for the bottom right 2x2 pixel area, this combination of table and color */\
  9135. /* can never be part of an optimal solution and therefore we do not need to test the other */\
  9136. /* two 2x2 pixel areas */\
  9137. if((err_this_table_upper<best_err)||(err_this_table_lower<best_err))\
  9138. {\
  9139. /* unroll loop for(xx=4; xx<8; xx++) */\
  9140. ONE_PIXEL_UR_PERCEP(table_nbr,4)\
  9141. ONE_PIXEL_UR_PERCEP(table_nbr,5)\
  9142. ONE_PIXEL_UR_PERCEP(table_nbr,6)\
  9143. ONE_PIXEL_UR_PERCEP(table_nbr,7)\
  9144. /* end unroll loop */\
  9145. /* unroll loop for(xx=4; xx<8; xx++) */\
  9146. ONE_PIXEL_LL_PERCEP(table_nbr,8)\
  9147. ONE_PIXEL_LL_PERCEP(table_nbr,9)\
  9148. ONE_PIXEL_LL_PERCEP(table_nbr,10)\
  9149. ONE_PIXEL_LL_PERCEP(table_nbr,11)\
  9150. /* end unroll loop */\
  9151. if(err_this_table_upper<err_upper)\
  9152. err_upper = err_this_table_upper;\
  9153. if(err_this_table_lower<err_lower)\
  9154. err_lower = err_this_table_lower;\
  9155. if(err_this_table_left<err_left)\
  9156. err_left = err_this_table_left;\
  9157. if(err_this_table_right<err_right)\
  9158. err_right = err_this_table_right;\
  9159. }\
  9160. /*unroll loop for(table_nbr=0;table_nbr<8;table_nbr++)*/
  9161. ONE_TABLE_3_PERCEP(0);
  9162. ONE_TABLE_3_PERCEP(1);
  9163. ONE_TABLE_3_PERCEP(2);
  9164. ONE_TABLE_3_PERCEP(3);
  9165. ONE_TABLE_3_PERCEP(4);
  9166. ONE_TABLE_3_PERCEP(5);
  9167. ONE_TABLE_3_PERCEP(6);
  9168. ONE_TABLE_3_PERCEP(7);
  9169. /*end unroll loop*/
  9170. }
  9171. #endif
  9172. #if EXHAUSTIVE_CODE_ACTIVE
  9173. // Compresses the individual mode exhaustively (perecptual error metric).
  9174. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  9175. unsigned int compressBlockIndividualExhaustivePerceptual(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, unsigned int total_best_err)
  9176. {
  9177. unsigned int best_err_norm_diff = MAXERR1000;
  9178. unsigned int best_err_norm_444 = MAXERR1000;
  9179. unsigned int best_err_flip_diff = MAXERR1000;
  9180. unsigned int best_err_flip_444 = MAXERR1000;
  9181. uint8 color_quant1[3], color_quant2[3];
  9182. int enc_color1[3];
  9183. int best_enc_color1[3], best_enc_color2[3];
  9184. int min_error=MAXERR1000;
  9185. unsigned int best_pixel_indices1_MSB=0;
  9186. unsigned int best_pixel_indices1_LSB=0;
  9187. unsigned int best_pixel_indices2_MSB=0;
  9188. unsigned int best_pixel_indices2_LSB=0;
  9189. unsigned int pixel_indices1_MSB=0;
  9190. unsigned int pixel_indices1_LSB=0;
  9191. unsigned int pixel_indices2_MSB=0;
  9192. unsigned int err_upper, err_lower;
  9193. unsigned int err_left, err_right;
  9194. unsigned int pixel_indices2_LSB=0;
  9195. unsigned int best_err_upper = MAXERR1000;
  9196. unsigned int best_err_lower = MAXERR1000;
  9197. unsigned int best_err_left = MAXERR1000;
  9198. unsigned int best_err_right = MAXERR1000;
  9199. int best_upper_col[3];
  9200. int best_lower_col[3];
  9201. int best_left_col[3];
  9202. int best_right_col[3];
  9203. unsigned int table1=0, table2=0;
  9204. unsigned int best_table1=0, best_table2=0;
  9205. unsigned int precalc_err_UL_R[8*4*4];
  9206. unsigned int precalc_err_UR_R[8*4*4];
  9207. unsigned int precalc_err_LL_R[8*4*4];
  9208. unsigned int precalc_err_LR_R[8*4*4];
  9209. unsigned int precalc_err_UL_RG[8*4*4];
  9210. unsigned int precalc_err_UR_RG[8*4*4];
  9211. unsigned int precalc_err_LL_RG[8*4*4];
  9212. unsigned int precalc_err_LR_RG[8*4*4];
  9213. int diffbit;
  9214. uint8 block_2x2[4*4*4];
  9215. unsigned int best_err;
  9216. int best_flip;
  9217. int xx,yy,count = 0;
  9218. // 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.
  9219. for(xx = 0; xx<2; xx++)
  9220. {
  9221. for(yy=0; yy<2; yy++)
  9222. {
  9223. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9224. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9225. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9226. block_2x2[(count)*4+3] = 0;
  9227. count++;
  9228. }
  9229. }
  9230. for(xx = 2; xx<4; xx++)
  9231. {
  9232. for(yy=0; yy<2; yy++)
  9233. {
  9234. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9235. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9236. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9237. block_2x2[(count)*4+3] = 0;
  9238. count++;
  9239. }
  9240. }
  9241. for(xx = 0; xx<2; xx++)
  9242. {
  9243. for(yy=2; yy<4; yy++)
  9244. {
  9245. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9246. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9247. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9248. block_2x2[(count)*4+3] = 0;
  9249. count++;
  9250. }
  9251. }
  9252. for(xx = 2; xx<4; xx++)
  9253. {
  9254. for(yy=2; yy<4; yy++)
  9255. {
  9256. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9257. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9258. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9259. block_2x2[(count)*4+3] = 0;
  9260. count++;
  9261. }
  9262. }
  9263. unsigned int test1, test2;
  9264. 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);
  9265. if(best_err < total_best_err)
  9266. total_best_err = best_err;
  9267. unsigned int tryblocks = 0;
  9268. unsigned int allblocks = 0;
  9269. int needtest;
  9270. for(enc_color1[0]=0; enc_color1[0]<16; enc_color1[0]++)
  9271. {
  9272. color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0]);
  9273. 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))
  9274. {
  9275. for(enc_color1[1]=0; enc_color1[1]<16; enc_color1[1]++)
  9276. {
  9277. color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1]);
  9278. 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))
  9279. {
  9280. needtest = false;
  9281. for(enc_color1[2]=0; enc_color1[2]<16; enc_color1[2]++)
  9282. {
  9283. color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2]);
  9284. 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);
  9285. if(err_upper<best_err_upper)
  9286. {
  9287. best_err_upper = err_upper;
  9288. best_upper_col[0] = enc_color1[0];
  9289. best_upper_col[1] = enc_color1[1];
  9290. best_upper_col[2] = enc_color1[2];
  9291. needtest = true;
  9292. }
  9293. if(err_lower<best_err_lower)
  9294. {
  9295. best_err_lower = err_lower;
  9296. best_lower_col[0] = enc_color1[0];
  9297. best_lower_col[1] = enc_color1[1];
  9298. best_lower_col[2] = enc_color1[2];
  9299. needtest=true;
  9300. }
  9301. if(err_left<best_err_left)
  9302. {
  9303. best_err_left = err_left;
  9304. best_left_col[0] = enc_color1[0];
  9305. best_left_col[1] = enc_color1[1];
  9306. best_left_col[2] = enc_color1[2];
  9307. needtest=true;
  9308. }
  9309. if(err_right<best_err_right)
  9310. {
  9311. best_err_right = err_right;
  9312. best_right_col[0] = enc_color1[0];
  9313. best_right_col[1] = enc_color1[1];
  9314. best_right_col[2] = enc_color1[2];
  9315. needtest = true;
  9316. }
  9317. }
  9318. if(needtest)
  9319. {
  9320. if(best_err_upper+best_err_lower < best_err_left+best_err_right)
  9321. {
  9322. best_err = best_err_upper+best_err_lower;
  9323. if(best_err < total_best_err)
  9324. total_best_err = best_err;
  9325. }
  9326. else
  9327. {
  9328. best_err = best_err_left+best_err_right;
  9329. if(best_err < total_best_err)
  9330. total_best_err = best_err;
  9331. }
  9332. }
  9333. }
  9334. }
  9335. }
  9336. }
  9337. if(best_err_upper+best_err_lower < best_err_left+best_err_right)
  9338. {
  9339. best_flip = 1;
  9340. best_enc_color1[0] = best_upper_col[0];
  9341. best_enc_color1[1] = best_upper_col[1];
  9342. best_enc_color1[2] = best_upper_col[2];
  9343. best_enc_color2[0] = best_lower_col[0];
  9344. best_enc_color2[1] = best_lower_col[1];
  9345. best_enc_color2[2] = best_lower_col[2];
  9346. best_err = best_err_upper+best_err_lower;
  9347. if(best_err < total_best_err)
  9348. total_best_err = best_err;
  9349. }
  9350. else
  9351. {
  9352. best_flip = 0;
  9353. best_enc_color1[0] = best_left_col[0];
  9354. best_enc_color1[1] = best_left_col[1];
  9355. best_enc_color1[2] = best_left_col[2];
  9356. best_enc_color2[0] = best_right_col[0];
  9357. best_enc_color2[1] = best_right_col[1];
  9358. best_enc_color2[2] = best_right_col[2];
  9359. best_err = best_err_left+best_err_right;
  9360. if(best_err < total_best_err)
  9361. total_best_err = best_err;
  9362. }
  9363. color_quant1[0] = best_enc_color1[0] << 4 | (best_enc_color1[0]);
  9364. color_quant1[1] = best_enc_color1[1] << 4 | (best_enc_color1[1]);
  9365. color_quant1[2] = best_enc_color1[2] << 4 | (best_enc_color1[2]);
  9366. if(best_flip == 0)
  9367. tryalltables_3bittable2x4percep1000(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  9368. else
  9369. tryalltables_3bittable4x2percep1000(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  9370. color_quant2[0] = best_enc_color2[0] << 4 | (best_enc_color2[0]);
  9371. color_quant2[1] = best_enc_color2[1] << 4 | (best_enc_color2[1]);
  9372. color_quant2[2] = best_enc_color2[2] << 4 | (best_enc_color2[2]);
  9373. if(best_flip == 0)
  9374. tryalltables_3bittable2x4percep1000(img,width,height,startx+2,starty,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  9375. else
  9376. tryalltables_3bittable4x2percep1000(img,width,height,startx,starty+2,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  9377. // ETC1_RGB8_OES:
  9378. //
  9379. // a) bit layout in bits 63 through 32 if diffbit = 0
  9380. //
  9381. // 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
  9382. // ---------------------------------------------------------------------------------------------------
  9383. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  9384. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  9385. // ---------------------------------------------------------------------------------------------------
  9386. //
  9387. // b) bit layout in bits 63 through 32 if diffbit = 1
  9388. //
  9389. // 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
  9390. // ---------------------------------------------------------------------------------------------------
  9391. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  9392. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  9393. // ---------------------------------------------------------------------------------------------------
  9394. //
  9395. // c) bit layout in bits 31 through 0 (in both cases)
  9396. //
  9397. // 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
  9398. // --------------------------------------------------------------------------------------------------
  9399. // | most significant pixel index bits | least significant pixel index bits |
  9400. // | 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 |
  9401. // --------------------------------------------------------------------------------------------------
  9402. diffbit = 1;
  9403. compressed1 = 0;
  9404. PUTBITSHIGH( compressed1, diffbit, 0, 33);
  9405. PUTBITSHIGH( compressed1, best_enc_color1[0], 4, 63);
  9406. PUTBITSHIGH( compressed1, best_enc_color1[1], 4, 55);
  9407. PUTBITSHIGH( compressed1, best_enc_color1[2], 4, 47);
  9408. PUTBITSHIGH( compressed1, best_enc_color2[0], 4, 59);
  9409. PUTBITSHIGH( compressed1, best_enc_color2[1], 4, 51);
  9410. PUTBITSHIGH( compressed1, best_enc_color2[2], 4, 43);
  9411. PUTBITSHIGH( compressed1, best_table1, 3, 39);
  9412. PUTBITSHIGH( compressed1, best_table2, 3, 36);
  9413. PUTBITSHIGH( compressed1, best_flip, 1, 32);
  9414. if(best_flip == 0)
  9415. {
  9416. compressed2 = 0;
  9417. PUTBITS( compressed2, (best_pixel_indices1_MSB ), 8, 23);
  9418. PUTBITS( compressed2, (best_pixel_indices2_MSB ), 8, 31);
  9419. PUTBITS( compressed2, (best_pixel_indices1_LSB ), 8, 7);
  9420. PUTBITS( compressed2, (best_pixel_indices2_LSB ), 8, 15);
  9421. }
  9422. else
  9423. {
  9424. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  9425. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  9426. compressed2 = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  9427. }
  9428. return best_err;
  9429. }
  9430. #endif
  9431. #if EXHAUSTIVE_CODE_ACTIVE
  9432. // Compresses the individual mode exhaustively.
  9433. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  9434. unsigned int compressBlockIndividualExhaustive(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, unsigned int total_best_err)
  9435. {
  9436. unsigned int best_err_norm_diff = 255*255*16*3;
  9437. unsigned int best_err_norm_444 = 255*255*16*3;
  9438. unsigned int best_err_flip_diff = 255*255*16*3;
  9439. unsigned int best_err_flip_444 = 255*255*16*3;
  9440. uint8 color_quant1[3], color_quant2[3];
  9441. int enc_color1[3];
  9442. int best_enc_color1[3], best_enc_color2[3];
  9443. int min_error=255*255*8*3;
  9444. unsigned int best_pixel_indices1_MSB=0;
  9445. unsigned int best_pixel_indices1_LSB=0;
  9446. unsigned int best_pixel_indices2_MSB=0;
  9447. unsigned int best_pixel_indices2_LSB=0;
  9448. unsigned int pixel_indices1_MSB=0;
  9449. unsigned int pixel_indices1_LSB=0;
  9450. unsigned int pixel_indices2_MSB=0;
  9451. unsigned int err_upper, err_lower;
  9452. unsigned int err_left, err_right;
  9453. unsigned int pixel_indices2_LSB=0;
  9454. unsigned int best_err_upper = 255*255*16*3;
  9455. unsigned int best_err_lower = 255*255*16*3;
  9456. unsigned int best_err_left = 255*255*16*3;
  9457. unsigned int best_err_right = 255*255*16*3;
  9458. int best_upper_col[3];
  9459. int best_lower_col[3];
  9460. int best_left_col[3];
  9461. int best_right_col[3];
  9462. unsigned int table1=0, table2=0;
  9463. unsigned int best_table1=0, best_table2=0;
  9464. unsigned int precalc_err_UL_R[8*4*4];
  9465. unsigned int precalc_err_UR_R[8*4*4];
  9466. unsigned int precalc_err_LL_R[8*4*4];
  9467. unsigned int precalc_err_LR_R[8*4*4];
  9468. unsigned int precalc_err_UL_RG[8*4*4];
  9469. unsigned int precalc_err_UR_RG[8*4*4];
  9470. unsigned int precalc_err_LL_RG[8*4*4];
  9471. unsigned int precalc_err_LR_RG[8*4*4];
  9472. int diffbit;
  9473. uint8 block_2x2[4*4*4];
  9474. unsigned int best_err;
  9475. int best_flip;
  9476. int xx,yy,count = 0;
  9477. // 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.
  9478. for(xx = 0; xx<2; xx++)
  9479. {
  9480. for(yy=0; yy<2; yy++)
  9481. {
  9482. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9483. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9484. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9485. block_2x2[(count)*4+3] = 0;
  9486. count++;
  9487. }
  9488. }
  9489. for(xx = 2; xx<4; xx++)
  9490. {
  9491. for(yy=0; yy<2; yy++)
  9492. {
  9493. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9494. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9495. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9496. block_2x2[(count)*4+3] = 0;
  9497. count++;
  9498. }
  9499. }
  9500. for(xx = 0; xx<2; xx++)
  9501. {
  9502. for(yy=2; yy<4; yy++)
  9503. {
  9504. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9505. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9506. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9507. block_2x2[(count)*4+3] = 0;
  9508. count++;
  9509. }
  9510. }
  9511. for(xx = 2; xx<4; xx++)
  9512. {
  9513. for(yy=2; yy<4; yy++)
  9514. {
  9515. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9516. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9517. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9518. block_2x2[(count)*4+3] = 0;
  9519. count++;
  9520. }
  9521. }
  9522. unsigned int test1, test2;
  9523. 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);
  9524. if(best_err < total_best_err)
  9525. total_best_err = best_err;
  9526. unsigned int tryblocks = 0;
  9527. unsigned int allblocks = 0;
  9528. int needtest;
  9529. for(enc_color1[0]=0; enc_color1[0]<16; enc_color1[0]++)
  9530. {
  9531. color_quant1[0] = enc_color1[0] << 4 | (enc_color1[0]);
  9532. 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))
  9533. {
  9534. for(enc_color1[1]=0; enc_color1[1]<16; enc_color1[1]++)
  9535. {
  9536. color_quant1[1] = enc_color1[1] << 4 | (enc_color1[1]);
  9537. 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))
  9538. {
  9539. needtest = false;
  9540. for(enc_color1[2]=0; enc_color1[2]<16; enc_color1[2]++)
  9541. {
  9542. color_quant1[2] = enc_color1[2] << 4 | (enc_color1[2]);
  9543. 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);
  9544. if(err_upper<best_err_upper)
  9545. {
  9546. best_err_upper = err_upper;
  9547. best_upper_col[0] = enc_color1[0];
  9548. best_upper_col[1] = enc_color1[1];
  9549. best_upper_col[2] = enc_color1[2];
  9550. needtest = true;
  9551. }
  9552. if(err_lower<best_err_lower)
  9553. {
  9554. best_err_lower = err_lower;
  9555. best_lower_col[0] = enc_color1[0];
  9556. best_lower_col[1] = enc_color1[1];
  9557. best_lower_col[2] = enc_color1[2];
  9558. needtest=true;
  9559. }
  9560. if(err_left<best_err_left)
  9561. {
  9562. best_err_left = err_left;
  9563. best_left_col[0] = enc_color1[0];
  9564. best_left_col[1] = enc_color1[1];
  9565. best_left_col[2] = enc_color1[2];
  9566. needtest=true;
  9567. }
  9568. if(err_right<best_err_right)
  9569. {
  9570. best_err_right = err_right;
  9571. best_right_col[0] = enc_color1[0];
  9572. best_right_col[1] = enc_color1[1];
  9573. best_right_col[2] = enc_color1[2];
  9574. needtest = true;
  9575. }
  9576. }
  9577. if(needtest)
  9578. {
  9579. if(best_err_upper+best_err_lower < best_err_left+best_err_right)
  9580. {
  9581. best_err = best_err_upper+best_err_lower;
  9582. if(best_err < total_best_err)
  9583. total_best_err = best_err;
  9584. }
  9585. else
  9586. {
  9587. best_err = best_err_left+best_err_right;
  9588. if(best_err < total_best_err)
  9589. total_best_err = best_err;
  9590. }
  9591. }
  9592. }
  9593. }
  9594. }
  9595. }
  9596. if(best_err_upper+best_err_lower < best_err_left+best_err_right)
  9597. {
  9598. best_flip = 1;
  9599. best_enc_color1[0] = best_upper_col[0];
  9600. best_enc_color1[1] = best_upper_col[1];
  9601. best_enc_color1[2] = best_upper_col[2];
  9602. best_enc_color2[0] = best_lower_col[0];
  9603. best_enc_color2[1] = best_lower_col[1];
  9604. best_enc_color2[2] = best_lower_col[2];
  9605. best_err = best_err_upper+best_err_lower;
  9606. if(best_err < total_best_err)
  9607. total_best_err = best_err;
  9608. }
  9609. else
  9610. {
  9611. best_flip = 0;
  9612. best_enc_color1[0] = best_left_col[0];
  9613. best_enc_color1[1] = best_left_col[1];
  9614. best_enc_color1[2] = best_left_col[2];
  9615. best_enc_color2[0] = best_right_col[0];
  9616. best_enc_color2[1] = best_right_col[1];
  9617. best_enc_color2[2] = best_right_col[2];
  9618. best_err = best_err_left+best_err_right;
  9619. if(best_err < total_best_err)
  9620. total_best_err = best_err;
  9621. }
  9622. color_quant1[0] = best_enc_color1[0] << 4 | (best_enc_color1[0]);
  9623. color_quant1[1] = best_enc_color1[1] << 4 | (best_enc_color1[1]);
  9624. color_quant1[2] = best_enc_color1[2] << 4 | (best_enc_color1[2]);
  9625. if(best_flip == 0)
  9626. tryalltables_3bittable2x4(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  9627. else
  9628. tryalltables_3bittable4x2(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  9629. color_quant2[0] = best_enc_color2[0] << 4 | (best_enc_color2[0]);
  9630. color_quant2[1] = best_enc_color2[1] << 4 | (best_enc_color2[1]);
  9631. color_quant2[2] = best_enc_color2[2] << 4 | (best_enc_color2[2]);
  9632. if(best_flip == 0)
  9633. tryalltables_3bittable2x4(img,width,height,startx+2,starty,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  9634. else
  9635. tryalltables_3bittable4x2(img,width,height,startx,starty+2,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  9636. // ETC1_RGB8_OES:
  9637. //
  9638. // a) bit layout in bits 63 through 32 if diffbit = 0
  9639. //
  9640. // 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
  9641. // ---------------------------------------------------------------------------------------------------
  9642. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  9643. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  9644. // ---------------------------------------------------------------------------------------------------
  9645. //
  9646. // b) bit layout in bits 63 through 32 if diffbit = 1
  9647. //
  9648. // 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
  9649. // ---------------------------------------------------------------------------------------------------
  9650. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  9651. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  9652. // ---------------------------------------------------------------------------------------------------
  9653. //
  9654. // c) bit layout in bits 31 through 0 (in both cases)
  9655. //
  9656. // 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
  9657. // --------------------------------------------------------------------------------------------------
  9658. // | most significant pixel index bits | least significant pixel index bits |
  9659. // | 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 |
  9660. // --------------------------------------------------------------------------------------------------
  9661. diffbit = 1;
  9662. compressed1 = 0;
  9663. PUTBITSHIGH( compressed1, diffbit, 0, 33);
  9664. PUTBITSHIGH( compressed1, best_enc_color1[0], 4, 63);
  9665. PUTBITSHIGH( compressed1, best_enc_color1[1], 4, 55);
  9666. PUTBITSHIGH( compressed1, best_enc_color1[2], 4, 47);
  9667. PUTBITSHIGH( compressed1, best_enc_color2[0], 4, 59);
  9668. PUTBITSHIGH( compressed1, best_enc_color2[1], 4, 51);
  9669. PUTBITSHIGH( compressed1, best_enc_color2[2], 4, 43);
  9670. PUTBITSHIGH( compressed1, best_table1, 3, 39);
  9671. PUTBITSHIGH( compressed1, best_table2, 3, 36);
  9672. PUTBITSHIGH( compressed1, best_flip, 1, 32);
  9673. if(best_flip == 0)
  9674. {
  9675. compressed2 = 0;
  9676. PUTBITS( compressed2, (best_pixel_indices1_MSB ), 8, 23);
  9677. PUTBITS( compressed2, (best_pixel_indices2_MSB ), 8, 31);
  9678. PUTBITS( compressed2, (best_pixel_indices1_LSB ), 8, 7);
  9679. PUTBITS( compressed2, (best_pixel_indices2_LSB ), 8, 15);
  9680. }
  9681. else
  9682. {
  9683. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  9684. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  9685. compressed2 = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  9686. }
  9687. return best_err;
  9688. }
  9689. #endif
  9690. #if EXHAUSTIVE_CODE_ACTIVE
  9691. // Compresses the differential mode exhaustively (perecptual error metric).
  9692. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  9693. 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)
  9694. {
  9695. unsigned int best_err_norm_diff = MAXERR1000;
  9696. unsigned int best_err_norm_444 = MAXERR1000;
  9697. unsigned int best_err_flip_diff = MAXERR1000;
  9698. unsigned int best_err_flip_444 = MAXERR1000;
  9699. uint8 color_quant1[3], color_quant2[3];
  9700. int enc_color1[3], enc_color2[3], diff[3];
  9701. int best_enc_color1[3], best_enc_color2[3];
  9702. signed char bytediff[3];
  9703. unsigned int best_pixel_indices1_MSB=0;
  9704. unsigned int best_pixel_indices1_LSB=0;
  9705. unsigned int best_pixel_indices2_MSB=0;
  9706. unsigned int best_pixel_indices2_LSB=0;
  9707. unsigned int pixel_indices1_MSB=0;
  9708. unsigned int pixel_indices1_LSB=0;
  9709. unsigned int pixel_indices2_MSB=0;
  9710. unsigned int *err_upper, *err_lower;
  9711. unsigned int *err_left, *err_right;
  9712. unsigned int pixel_indices2_LSB=0;
  9713. unsigned int table1=0, table2=0;
  9714. unsigned int best_table1=0, best_table2=0;
  9715. unsigned int precalc_err_UL_R[8*4*4];
  9716. unsigned int precalc_err_UR_R[8*4*4];
  9717. unsigned int precalc_err_LL_R[8*4*4];
  9718. unsigned int precalc_err_LR_R[8*4*4];
  9719. unsigned int precalc_err_UL_RG[8*4*4];
  9720. unsigned int precalc_err_UR_RG[8*4*4];
  9721. unsigned int precalc_err_LL_RG[8*4*4];
  9722. unsigned int precalc_err_LR_RG[8*4*4];
  9723. unsigned int best_error_using_diff_mode;
  9724. int diffbit;
  9725. uint8 block_2x2[4*4*4];
  9726. unsigned int error, error_lying, error_standing;
  9727. unsigned int *err_lower_adr;
  9728. int best_flip;
  9729. unsigned int *err_right_adr;
  9730. int xx,yy,count = 0;
  9731. // 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.
  9732. for(xx = 0; xx<2; xx++)
  9733. {
  9734. for(yy=0; yy<2; yy++)
  9735. {
  9736. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9737. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9738. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9739. block_2x2[(count)*4+3] = 0;
  9740. count++;
  9741. }
  9742. }
  9743. for(xx = 2; xx<4; xx++)
  9744. {
  9745. for(yy=0; yy<2; yy++)
  9746. {
  9747. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9748. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9749. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9750. block_2x2[(count)*4+3] = 0;
  9751. count++;
  9752. }
  9753. }
  9754. for(xx = 0; xx<2; xx++)
  9755. {
  9756. for(yy=2; yy<4; yy++)
  9757. {
  9758. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9759. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9760. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9761. block_2x2[(count)*4+3] = 0;
  9762. count++;
  9763. }
  9764. }
  9765. for(xx = 2; xx<4; xx++)
  9766. {
  9767. for(yy=2; yy<4; yy++)
  9768. {
  9769. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  9770. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  9771. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  9772. block_2x2[(count)*4+3] = 0;
  9773. count++;
  9774. }
  9775. }
  9776. unsigned int test1, test2;
  9777. best_error_using_diff_mode = compressBlockOnlyDiffFlipAveragePerceptual1000(img, width, height, startx, starty, test1, test2);
  9778. if(best_error_using_diff_mode < best_error_so_far)
  9779. best_error_so_far = best_error_using_diff_mode;
  9780. // Decode the parameters so that we have a worst case color pair and a flip status
  9781. best_flip = test1 & 1;
  9782. best_enc_color1[0] = GETBITSHIGH( test1, 5, 63);
  9783. best_enc_color1[1] = GETBITSHIGH( test1, 5, 55);
  9784. best_enc_color1[2] = GETBITSHIGH( test1, 5, 47);
  9785. bytediff[0] = GETBITSHIGH( test1, 3, 58);
  9786. bytediff[1] = GETBITSHIGH( test1, 3, 50);
  9787. bytediff[2] = GETBITSHIGH( test1, 3, 42);
  9788. bytediff[0] = (bytediff[0] << 5);
  9789. bytediff[1] = (bytediff[1] << 5);
  9790. bytediff[2] = (bytediff[2] << 5);
  9791. bytediff[0] = bytediff[0] >> 5;
  9792. bytediff[1] = bytediff[1] >> 5;
  9793. bytediff[2] = bytediff[2] >> 5;
  9794. best_enc_color2[0]= best_enc_color1[0] + bytediff[0];
  9795. best_enc_color2[1]= best_enc_color1[1] + bytediff[1];
  9796. best_enc_color2[2]= best_enc_color1[2] + bytediff[2];
  9797. // allocate memory for errors:
  9798. err_upper = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
  9799. if(!err_upper){printf("Out of memory allocating \n");exit(1);}
  9800. err_lower = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
  9801. if(!err_lower){printf("Out of memory allocating \n");exit(1);}
  9802. err_left = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
  9803. if(!err_left){printf("Out of memory allocating \n");exit(1);}
  9804. err_right = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
  9805. if(!err_right){printf("Out of memory allocating \n");exit(1);}
  9806. int q;
  9807. // Calculate all errors
  9808. for(enc_color1[0]=0; enc_color1[0]<32; enc_color1[0]++)
  9809. {
  9810. color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  9811. 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))
  9812. {
  9813. for(enc_color1[1]=0; enc_color1[1]<32; enc_color1[1]++)
  9814. {
  9815. color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  9816. 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))
  9817. {
  9818. for(enc_color1[2]=0; enc_color1[2]<32; enc_color1[2]++)
  9819. {
  9820. color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  9821. 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);
  9822. }
  9823. }
  9824. else
  9825. {
  9826. for(q=0;q<32;q++)
  9827. {
  9828. err_upper[32*32*enc_color1[0]+32*enc_color1[1]+q] = MAXERR1000;
  9829. err_lower[32*32*enc_color1[0]+32*enc_color1[1]+q] = MAXERR1000;
  9830. err_left[32*32*enc_color1[0]+32*enc_color1[1]+q] = MAXERR1000;
  9831. err_right[32*32*enc_color1[0]+32*enc_color1[1]+q] = MAXERR1000;
  9832. }
  9833. }
  9834. }
  9835. }
  9836. else
  9837. {
  9838. for(q=0;q<32*32;q++)
  9839. {
  9840. err_upper[32*32*enc_color1[0]+q] = MAXERR1000;
  9841. err_lower[32*32*enc_color1[0]+q] = MAXERR1000;
  9842. err_left[32*32*enc_color1[0]+q] = MAXERR1000;
  9843. err_right[32*32*enc_color1[0]+q] = MAXERR1000;
  9844. }
  9845. }
  9846. }
  9847. for(enc_color1[0]=0; enc_color1[0]<32; enc_color1[0]++)
  9848. {
  9849. for(enc_color1[1]=0; enc_color1[1]<32; enc_color1[1]++)
  9850. {
  9851. for(enc_color1[2]=0; enc_color1[2]<4; enc_color1[2]++)
  9852. {
  9853. error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  9854. error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  9855. if(error_lying < best_error_so_far || error_standing < best_error_so_far)
  9856. {
  9857. for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
  9858. {
  9859. for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
  9860. {
  9861. err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
  9862. err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
  9863. for(enc_color2[2]=JAS_MAX(0,enc_color1[2]-4); enc_color2[2]<JAS_MIN(enc_color1[2]+4,32); enc_color2[2]++)
  9864. {
  9865. error = error_lying+err_lower_adr[enc_color2[2]];
  9866. if(error<best_error_so_far)
  9867. {
  9868. best_flip = 1;
  9869. best_error_so_far = error;
  9870. best_error_using_diff_mode = error;
  9871. best_enc_color1[0] = enc_color1[0];
  9872. best_enc_color1[1] = enc_color1[1];
  9873. best_enc_color1[2] = enc_color1[2];
  9874. best_enc_color2[0] = enc_color2[0];
  9875. best_enc_color2[1] = enc_color2[1];
  9876. best_enc_color2[2] = enc_color2[2];
  9877. }
  9878. error = error_standing+err_right_adr[enc_color2[2]];
  9879. if(error<best_error_so_far)
  9880. {
  9881. best_flip = 0;
  9882. best_error_so_far = error;
  9883. best_error_using_diff_mode = error;
  9884. best_enc_color1[0] = enc_color1[0];
  9885. best_enc_color1[1] = enc_color1[1];
  9886. best_enc_color1[2] = enc_color1[2];
  9887. best_enc_color2[0] = enc_color2[0];
  9888. best_enc_color2[1] = enc_color2[1];
  9889. best_enc_color2[2] = enc_color2[2];
  9890. }
  9891. }
  9892. }
  9893. }
  9894. }
  9895. }
  9896. for(enc_color1[2]=4; enc_color1[2]<28; enc_color1[2]++)
  9897. {
  9898. error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  9899. error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  9900. if(error_lying < best_error_so_far || error_standing < best_error_so_far)
  9901. {
  9902. for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
  9903. {
  9904. for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
  9905. {
  9906. err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
  9907. err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
  9908. // since enc_color[2] is between 4 and 29 we do not need to clamp the loop on the next line
  9909. for(enc_color2[2]=enc_color1[2]-4; enc_color2[2]<enc_color1[2]+4; enc_color2[2]++)
  9910. {
  9911. error = error_lying+err_lower_adr[enc_color2[2]];
  9912. if(error<best_error_so_far)
  9913. {
  9914. best_flip = 1;
  9915. best_error_so_far = error;
  9916. best_error_using_diff_mode = error;
  9917. best_enc_color1[0] = enc_color1[0];
  9918. best_enc_color1[1] = enc_color1[1];
  9919. best_enc_color1[2] = enc_color1[2];
  9920. best_enc_color2[0] = enc_color2[0];
  9921. best_enc_color2[1] = enc_color2[1];
  9922. best_enc_color2[2] = enc_color2[2];
  9923. }
  9924. error = error_standing+err_right_adr[enc_color2[2]];
  9925. if(error<best_error_so_far)
  9926. {
  9927. best_flip = 0;
  9928. best_error_so_far = error;
  9929. best_error_using_diff_mode = error;
  9930. best_enc_color1[0] = enc_color1[0];
  9931. best_enc_color1[1] = enc_color1[1];
  9932. best_enc_color1[2] = enc_color1[2];
  9933. best_enc_color2[0] = enc_color2[0];
  9934. best_enc_color2[1] = enc_color2[1];
  9935. best_enc_color2[2] = enc_color2[2];
  9936. }
  9937. }
  9938. }
  9939. }
  9940. }
  9941. }
  9942. for(enc_color1[2]=28; enc_color1[2]<32; enc_color1[2]++)
  9943. {
  9944. error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  9945. error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  9946. if(error_lying < best_error_so_far || error_standing < best_error_so_far)
  9947. {
  9948. for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
  9949. {
  9950. for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
  9951. {
  9952. err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
  9953. err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
  9954. for(enc_color2[2]=JAS_MAX(0,enc_color1[2]-4); enc_color2[2]<JAS_MIN(enc_color1[2]+4,32); enc_color2[2]++)
  9955. {
  9956. error = error_lying+err_lower_adr[enc_color2[2]];
  9957. if(error<best_error_so_far)
  9958. {
  9959. best_flip = 1;
  9960. best_error_so_far = error;
  9961. best_error_using_diff_mode = error;
  9962. best_enc_color1[0] = enc_color1[0];
  9963. best_enc_color1[1] = enc_color1[1];
  9964. best_enc_color1[2] = enc_color1[2];
  9965. best_enc_color2[0] = enc_color2[0];
  9966. best_enc_color2[1] = enc_color2[1];
  9967. best_enc_color2[2] = enc_color2[2];
  9968. }
  9969. error = error_standing+err_right_adr[enc_color2[2]];
  9970. if(error<best_error_so_far)
  9971. {
  9972. best_flip = 0;
  9973. best_error_so_far = error;
  9974. best_error_using_diff_mode = error;
  9975. best_enc_color1[0] = enc_color1[0];
  9976. best_enc_color1[1] = enc_color1[1];
  9977. best_enc_color1[2] = enc_color1[2];
  9978. best_enc_color2[0] = enc_color2[0];
  9979. best_enc_color2[1] = enc_color2[1];
  9980. best_enc_color2[2] = enc_color2[2];
  9981. }
  9982. }
  9983. }
  9984. }
  9985. }
  9986. }
  9987. }
  9988. }
  9989. free(err_upper);
  9990. free(err_lower);
  9991. free(err_left);
  9992. free(err_right);
  9993. color_quant1[0] = best_enc_color1[0] << 3 | (best_enc_color1[0] >> 2);
  9994. color_quant1[1] = best_enc_color1[1] << 3 | (best_enc_color1[1] >> 2);
  9995. color_quant1[2] = best_enc_color1[2] << 3 | (best_enc_color1[2] >> 2);
  9996. if(best_flip == 0)
  9997. tryalltables_3bittable2x4percep1000(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  9998. else
  9999. tryalltables_3bittable4x2percep1000(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  10000. color_quant2[0] = best_enc_color2[0] << 3 | (best_enc_color2[0] >> 2);
  10001. color_quant2[1] = best_enc_color2[1] << 3 | (best_enc_color2[1] >> 2);
  10002. color_quant2[2] = best_enc_color2[2] << 3 | (best_enc_color2[2] >> 2);
  10003. if(best_flip == 0)
  10004. tryalltables_3bittable2x4percep1000(img,width,height,startx+2,starty,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  10005. else
  10006. tryalltables_3bittable4x2percep1000(img,width,height,startx,starty+2,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  10007. diff[0] = best_enc_color2[0]-best_enc_color1[0];
  10008. diff[1] = best_enc_color2[1]-best_enc_color1[1];
  10009. diff[2] = best_enc_color2[2]-best_enc_color1[2];
  10010. // ETC1_RGB8_OES:
  10011. //
  10012. // a) bit layout in bits 63 through 32 if diffbit = 0
  10013. //
  10014. // 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
  10015. // ---------------------------------------------------------------------------------------------------
  10016. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  10017. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  10018. // ---------------------------------------------------------------------------------------------------
  10019. //
  10020. // b) bit layout in bits 63 through 32 if diffbit = 1
  10021. //
  10022. // 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
  10023. // ---------------------------------------------------------------------------------------------------
  10024. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  10025. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  10026. // ---------------------------------------------------------------------------------------------------
  10027. //
  10028. // c) bit layout in bits 31 through 0 (in both cases)
  10029. //
  10030. // 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
  10031. // --------------------------------------------------------------------------------------------------
  10032. // | most significant pixel index bits | least significant pixel index bits |
  10033. // | 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 |
  10034. // --------------------------------------------------------------------------------------------------
  10035. diffbit = 1;
  10036. compressed1 = 0;
  10037. PUTBITSHIGH( compressed1, diffbit, 1, 33);
  10038. PUTBITSHIGH( compressed1, best_enc_color1[0], 5, 63);
  10039. PUTBITSHIGH( compressed1, best_enc_color1[1], 5, 55);
  10040. PUTBITSHIGH( compressed1, best_enc_color1[2], 5, 47);
  10041. PUTBITSHIGH( compressed1, diff[0], 3, 58);
  10042. PUTBITSHIGH( compressed1, diff[1], 3, 50);
  10043. PUTBITSHIGH( compressed1, diff[2], 3, 42);
  10044. PUTBITSHIGH( compressed1, best_table1, 3, 39);
  10045. PUTBITSHIGH( compressed1, best_table2, 3, 36);
  10046. PUTBITSHIGH( compressed1, best_flip, 1, 32);
  10047. if(best_flip == 0)
  10048. {
  10049. compressed2 = 0;
  10050. PUTBITS( compressed2, (best_pixel_indices1_MSB ), 8, 23);
  10051. PUTBITS( compressed2, (best_pixel_indices2_MSB ), 8, 31);
  10052. PUTBITS( compressed2, (best_pixel_indices1_LSB ), 8, 7);
  10053. PUTBITS( compressed2, (best_pixel_indices2_LSB ), 8, 15);
  10054. }
  10055. else
  10056. {
  10057. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  10058. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  10059. compressed2 = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  10060. }
  10061. return best_error_using_diff_mode;
  10062. }
  10063. #endif
  10064. #if EXHAUSTIVE_CODE_ACTIVE
  10065. // Compresses the differential mode exhaustively.
  10066. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  10067. unsigned int compressBlockDifferentialExhaustive(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2, unsigned int previous_best_err)
  10068. {
  10069. unsigned int best_err_norm_diff = 255*255*16*3;
  10070. unsigned int best_err_norm_444 = 255*255*16*3;
  10071. unsigned int best_err_flip_diff = 255*255*16*3;
  10072. unsigned int best_err_flip_444 = 255*255*16*3;
  10073. uint8 color_quant1[3], color_quant2[3];
  10074. int enc_color1[3], enc_color2[3], diff[3];
  10075. int best_enc_color1[3], best_enc_color2[3];
  10076. int min_error=255*255*8*3;
  10077. unsigned int best_pixel_indices1_MSB=0;
  10078. unsigned int best_pixel_indices1_LSB=0;
  10079. unsigned int best_pixel_indices2_MSB=0;
  10080. unsigned int best_pixel_indices2_LSB=0;
  10081. unsigned int pixel_indices1_MSB=0;
  10082. unsigned int pixel_indices1_LSB=0;
  10083. unsigned int pixel_indices2_MSB=0;
  10084. unsigned int *err_upper, *err_lower;
  10085. unsigned int *err_left, *err_right;
  10086. unsigned int pixel_indices2_LSB=0;
  10087. unsigned int table1=0, table2=0;
  10088. unsigned int best_table1=0, best_table2=0;
  10089. unsigned int precalc_err_UL_R[8*4*4];
  10090. unsigned int precalc_err_UR_R[8*4*4];
  10091. unsigned int precalc_err_LL_R[8*4*4];
  10092. unsigned int precalc_err_LR_R[8*4*4];
  10093. unsigned int precalc_err_UL_RG[8*4*4];
  10094. unsigned int precalc_err_UR_RG[8*4*4];
  10095. unsigned int precalc_err_LL_RG[8*4*4];
  10096. unsigned int precalc_err_LR_RG[8*4*4];
  10097. int diffbit;
  10098. uint8 block_2x2[4*4*4];
  10099. unsigned int error, error_lying, error_standing, best_err, total_best_err;
  10100. unsigned int *err_lower_adr;
  10101. int best_flip;
  10102. unsigned int *err_right_adr;
  10103. int xx,yy,count = 0;
  10104. // 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.
  10105. for(xx = 0; xx<2; xx++)
  10106. {
  10107. for(yy=0; yy<2; yy++)
  10108. {
  10109. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  10110. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  10111. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  10112. block_2x2[(count)*4+3] = 0;
  10113. count++;
  10114. }
  10115. }
  10116. for(xx = 2; xx<4; xx++)
  10117. {
  10118. for(yy=0; yy<2; yy++)
  10119. {
  10120. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  10121. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  10122. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  10123. block_2x2[(count)*4+3] = 0;
  10124. count++;
  10125. }
  10126. }
  10127. for(xx = 0; xx<2; xx++)
  10128. {
  10129. for(yy=2; yy<4; yy++)
  10130. {
  10131. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  10132. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  10133. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  10134. block_2x2[(count)*4+3] = 0;
  10135. count++;
  10136. }
  10137. }
  10138. for(xx = 2; xx<4; xx++)
  10139. {
  10140. for(yy=2; yy<4; yy++)
  10141. {
  10142. block_2x2[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  10143. block_2x2[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  10144. block_2x2[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  10145. block_2x2[(count)*4+3] = 0;
  10146. count++;
  10147. }
  10148. }
  10149. unsigned int test1, test2;
  10150. best_err = (unsigned int)compressBlockOnlyDiffFlipAverage(img, width, height, startx, starty, test1, test2, best_enc_color1, best_enc_color2, best_flip);
  10151. if(previous_best_err < best_err)
  10152. total_best_err = previous_best_err;
  10153. else
  10154. total_best_err = best_err;
  10155. // allocate memory for errors:
  10156. err_upper = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
  10157. if(!err_upper){printf("Out of memory allocating \n");exit(1);}
  10158. err_lower = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
  10159. if(!err_lower){printf("Out of memory allocating \n");exit(1);}
  10160. err_left = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
  10161. if(!err_left){printf("Out of memory allocating \n");exit(1);}
  10162. err_right = (unsigned int*) malloc(32*32*32*sizeof(unsigned int));
  10163. if(!err_right){printf("Out of memory allocating \n");exit(1);}
  10164. int q;
  10165. // Calculate all errors
  10166. for(enc_color1[0]=0; enc_color1[0]<32; enc_color1[0]++)
  10167. {
  10168. color_quant1[0] = enc_color1[0] << 3 | (enc_color1[0] >> 2);
  10169. 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))
  10170. {
  10171. for(enc_color1[1]=0; enc_color1[1]<32; enc_color1[1]++)
  10172. {
  10173. color_quant1[1] = enc_color1[1] << 3 | (enc_color1[1] >> 2);
  10174. 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))
  10175. {
  10176. for(enc_color1[2]=0; enc_color1[2]<32; enc_color1[2]++)
  10177. {
  10178. color_quant1[2] = enc_color1[2] << 3 | (enc_color1[2] >> 2);
  10179. 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);
  10180. }
  10181. }
  10182. else
  10183. {
  10184. for(q=0;q<32;q++)
  10185. {
  10186. err_upper[32*32*enc_color1[0]+32*enc_color1[1]+q] = 255*255*16*3;
  10187. err_lower[32*32*enc_color1[0]+32*enc_color1[1]+q] = 255*255*16*3;
  10188. err_left[32*32*enc_color1[0]+32*enc_color1[1]+q] = 255*255*16*3;
  10189. err_right[32*32*enc_color1[0]+32*enc_color1[1]+q] = 255*255*16*3;
  10190. }
  10191. }
  10192. }
  10193. }
  10194. else
  10195. {
  10196. for(q=0;q<32*32;q++)
  10197. {
  10198. err_upper[32*32*enc_color1[0]+q] = 255*255*16*3;
  10199. err_lower[32*32*enc_color1[0]+q] = 255*255*16*3;
  10200. err_left[32*32*enc_color1[0]+q] = 255*255*16*3;
  10201. err_right[32*32*enc_color1[0]+q] = 255*255*16*3;
  10202. }
  10203. }
  10204. }
  10205. for(enc_color1[0]=0; enc_color1[0]<32; enc_color1[0]++)
  10206. {
  10207. for(enc_color1[1]=0; enc_color1[1]<32; enc_color1[1]++)
  10208. {
  10209. for(enc_color1[2]=0; enc_color1[2]<4; enc_color1[2]++)
  10210. {
  10211. error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  10212. error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  10213. if(error_lying < total_best_err || error_standing < total_best_err)
  10214. {
  10215. for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
  10216. {
  10217. for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
  10218. {
  10219. err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
  10220. err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
  10221. for(enc_color2[2]=JAS_MAX(0,enc_color1[2]-4); enc_color2[2]<JAS_MIN(enc_color1[2]+4,32); enc_color2[2]++)
  10222. {
  10223. error = error_lying+err_lower_adr[enc_color2[2]];
  10224. if(error<best_err)
  10225. {
  10226. best_flip = 1;
  10227. best_err = error;
  10228. best_enc_color1[0] = enc_color1[0];
  10229. best_enc_color1[1] = enc_color1[1];
  10230. best_enc_color1[2] = enc_color1[2];
  10231. best_enc_color2[0] = enc_color2[0];
  10232. best_enc_color2[1] = enc_color2[1];
  10233. best_enc_color2[2] = enc_color2[2];
  10234. }
  10235. error = error_standing+err_right_adr[enc_color2[2]];
  10236. if(error<best_err)
  10237. {
  10238. best_flip = 0;
  10239. best_err = error;
  10240. best_enc_color1[0] = enc_color1[0];
  10241. best_enc_color1[1] = enc_color1[1];
  10242. best_enc_color1[2] = enc_color1[2];
  10243. best_enc_color2[0] = enc_color2[0];
  10244. best_enc_color2[1] = enc_color2[1];
  10245. best_enc_color2[2] = enc_color2[2];
  10246. }
  10247. }
  10248. }
  10249. }
  10250. if(best_err < total_best_err)
  10251. total_best_err = best_err;
  10252. }
  10253. }
  10254. for(enc_color1[2]=4; enc_color1[2]<28; enc_color1[2]++)
  10255. {
  10256. error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  10257. error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  10258. if(error_lying < total_best_err || error_standing < total_best_err)
  10259. {
  10260. for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
  10261. {
  10262. for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
  10263. {
  10264. err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
  10265. err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
  10266. // since enc_color[2] is between 4 and 29 we do not need to clamp the loop on the next line
  10267. for(enc_color2[2]=enc_color1[2]-4; enc_color2[2]<enc_color1[2]+4; enc_color2[2]++)
  10268. {
  10269. error = error_lying+err_lower_adr[enc_color2[2]];
  10270. if(error<best_err)
  10271. {
  10272. best_flip = 1;
  10273. best_err = error;
  10274. best_enc_color1[0] = enc_color1[0];
  10275. best_enc_color1[1] = enc_color1[1];
  10276. best_enc_color1[2] = enc_color1[2];
  10277. best_enc_color2[0] = enc_color2[0];
  10278. best_enc_color2[1] = enc_color2[1];
  10279. best_enc_color2[2] = enc_color2[2];
  10280. }
  10281. error = error_standing+err_right_adr[enc_color2[2]];
  10282. if(error<best_err)
  10283. {
  10284. best_flip = 0;
  10285. best_err = error;
  10286. best_enc_color1[0] = enc_color1[0];
  10287. best_enc_color1[1] = enc_color1[1];
  10288. best_enc_color1[2] = enc_color1[2];
  10289. best_enc_color2[0] = enc_color2[0];
  10290. best_enc_color2[1] = enc_color2[1];
  10291. best_enc_color2[2] = enc_color2[2];
  10292. }
  10293. }
  10294. }
  10295. }
  10296. if(best_err < total_best_err)
  10297. total_best_err = best_err;
  10298. }
  10299. }
  10300. for(enc_color1[2]=28; enc_color1[2]<32; enc_color1[2]++)
  10301. {
  10302. error_lying = err_upper[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  10303. error_standing = err_left[32*32*enc_color1[0]+32*enc_color1[1]+enc_color1[2]];
  10304. if(error_lying < total_best_err || error_standing < total_best_err)
  10305. {
  10306. for(enc_color2[0]=JAS_MAX(0,enc_color1[0]-4); enc_color2[0]<JAS_MIN(enc_color1[0]+4,32); enc_color2[0]++)
  10307. {
  10308. for(enc_color2[1]=JAS_MAX(0,enc_color1[1]-4); enc_color2[1]<JAS_MIN(enc_color1[1]+4,32); enc_color2[1]++)
  10309. {
  10310. err_lower_adr = &err_lower[32*32*enc_color2[0]+32*enc_color2[1]];
  10311. err_right_adr = &err_right[32*32*enc_color2[0]+32*enc_color2[1]];
  10312. for(enc_color2[2]=JAS_MAX(0,enc_color1[2]-4); enc_color2[2]<JAS_MIN(enc_color1[2]+4,32); enc_color2[2]++)
  10313. {
  10314. error = error_lying+err_lower_adr[enc_color2[2]];
  10315. if(error<best_err)
  10316. {
  10317. best_flip = 1;
  10318. best_err = error;
  10319. best_enc_color1[0] = enc_color1[0];
  10320. best_enc_color1[1] = enc_color1[1];
  10321. best_enc_color1[2] = enc_color1[2];
  10322. best_enc_color2[0] = enc_color2[0];
  10323. best_enc_color2[1] = enc_color2[1];
  10324. best_enc_color2[2] = enc_color2[2];
  10325. }
  10326. error = error_standing+err_right_adr[enc_color2[2]];
  10327. if(error<best_err)
  10328. {
  10329. best_flip = 0;
  10330. best_err = error;
  10331. best_enc_color1[0] = enc_color1[0];
  10332. best_enc_color1[1] = enc_color1[1];
  10333. best_enc_color1[2] = enc_color1[2];
  10334. best_enc_color2[0] = enc_color2[0];
  10335. best_enc_color2[1] = enc_color2[1];
  10336. best_enc_color2[2] = enc_color2[2];
  10337. }
  10338. }
  10339. }
  10340. }
  10341. if(best_err < total_best_err)
  10342. total_best_err = best_err;
  10343. }
  10344. }
  10345. }
  10346. }
  10347. free(err_upper);
  10348. free(err_lower);
  10349. free(err_left);
  10350. free(err_right);
  10351. color_quant1[0] = best_enc_color1[0] << 3 | (best_enc_color1[0] >> 2);
  10352. color_quant1[1] = best_enc_color1[1] << 3 | (best_enc_color1[1] >> 2);
  10353. color_quant1[2] = best_enc_color1[2] << 3 | (best_enc_color1[2] >> 2);
  10354. if(best_flip == 0)
  10355. tryalltables_3bittable2x4(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  10356. else
  10357. tryalltables_3bittable4x2(img,width,height,startx,starty,color_quant1,best_table1,best_pixel_indices1_MSB, best_pixel_indices1_LSB);
  10358. color_quant2[0] = best_enc_color2[0] << 3 | (best_enc_color2[0] >> 2);
  10359. color_quant2[1] = best_enc_color2[1] << 3 | (best_enc_color2[1] >> 2);
  10360. color_quant2[2] = best_enc_color2[2] << 3 | (best_enc_color2[2] >> 2);
  10361. if(best_flip == 0)
  10362. tryalltables_3bittable2x4(img,width,height,startx+2,starty,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  10363. else
  10364. tryalltables_3bittable4x2(img,width,height,startx,starty+2,color_quant2,best_table2,best_pixel_indices2_MSB, best_pixel_indices2_LSB);
  10365. diff[0] = best_enc_color2[0]-best_enc_color1[0];
  10366. diff[1] = best_enc_color2[1]-best_enc_color1[1];
  10367. diff[2] = best_enc_color2[2]-best_enc_color1[2];
  10368. // ETC1_RGB8_OES:
  10369. //
  10370. // a) bit layout in bits 63 through 32 if diffbit = 0
  10371. //
  10372. // 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
  10373. // ---------------------------------------------------------------------------------------------------
  10374. // | base col1 | base col2 | base col1 | base col2 | base col1 | base col2 | table | table |diff|flip|
  10375. // | R1 (4bits)| R2 (4bits)| G1 (4bits)| G2 (4bits)| B1 (4bits)| B2 (4bits)| cw 1 | cw 2 |bit |bit |
  10376. // ---------------------------------------------------------------------------------------------------
  10377. //
  10378. // b) bit layout in bits 63 through 32 if diffbit = 1
  10379. //
  10380. // 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
  10381. // ---------------------------------------------------------------------------------------------------
  10382. // | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
  10383. // | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
  10384. // ---------------------------------------------------------------------------------------------------
  10385. //
  10386. // c) bit layout in bits 31 through 0 (in both cases)
  10387. //
  10388. // 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
  10389. // --------------------------------------------------------------------------------------------------
  10390. // | most significant pixel index bits | least significant pixel index bits |
  10391. // | 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 |
  10392. // --------------------------------------------------------------------------------------------------
  10393. diffbit = 1;
  10394. compressed1 = 0;
  10395. PUTBITSHIGH( compressed1, diffbit, 1, 33);
  10396. PUTBITSHIGH( compressed1, best_enc_color1[0], 5, 63);
  10397. PUTBITSHIGH( compressed1, best_enc_color1[1], 5, 55);
  10398. PUTBITSHIGH( compressed1, best_enc_color1[2], 5, 47);
  10399. PUTBITSHIGH( compressed1, diff[0], 3, 58);
  10400. PUTBITSHIGH( compressed1, diff[1], 3, 50);
  10401. PUTBITSHIGH( compressed1, diff[2], 3, 42);
  10402. PUTBITSHIGH( compressed1, best_table1, 3, 39);
  10403. PUTBITSHIGH( compressed1, best_table2, 3, 36);
  10404. PUTBITSHIGH( compressed1, best_flip, 1, 32);
  10405. if(best_flip == 0)
  10406. {
  10407. compressed2 = 0;
  10408. PUTBITS( compressed2, (best_pixel_indices1_MSB ), 8, 23);
  10409. PUTBITS( compressed2, (best_pixel_indices2_MSB ), 8, 31);
  10410. PUTBITS( compressed2, (best_pixel_indices1_LSB ), 8, 7);
  10411. PUTBITS( compressed2, (best_pixel_indices2_LSB ), 8, 15);
  10412. }
  10413. else
  10414. {
  10415. best_pixel_indices1_MSB |= (best_pixel_indices2_MSB << 2);
  10416. best_pixel_indices1_LSB |= (best_pixel_indices2_LSB << 2);
  10417. compressed2 = ((best_pixel_indices1_MSB & 0xffff) << 16) | (best_pixel_indices1_LSB & 0xffff);
  10418. }
  10419. return best_err;
  10420. }
  10421. #endif
  10422. #if EXHAUSTIVE_CODE_ACTIVE
  10423. // This function uses real exhaustive search for the planar mode.
  10424. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  10425. 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)
  10426. {
  10427. int colorO_enc[3], colorH_enc[3], colorV_enc[3];
  10428. int best_colorO_enc[3], best_colorH_enc[3], best_colorV_enc[3];
  10429. unsigned int error;
  10430. unsigned int best_error;
  10431. unsigned int lowest_possible_error;
  10432. unsigned int best_error_red_sofar;
  10433. unsigned int best_error_green_sofar;
  10434. unsigned int best_error_blue_sofar;
  10435. unsigned int BBBtable[128*128];
  10436. unsigned int CCCtable[128*128];
  10437. uint8 block[4*4*4];
  10438. // Use 4 bytes per pixel to make it 32-word aligned.
  10439. int count = 0;
  10440. int xx, yy;
  10441. for(yy=0; yy<4; yy++)
  10442. {
  10443. for(xx = 0; xx<4; xx++)
  10444. {
  10445. block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  10446. block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  10447. block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  10448. block[(count)*4+3] = 0;
  10449. count++;
  10450. }
  10451. }
  10452. // The task is to calculate the sum of the error over the entire area of the block.
  10453. //
  10454. // The block can be partitioned into: O A A A
  10455. // B D D C
  10456. // B D C D
  10457. // B C D D
  10458. // where the error in
  10459. // O only depends on colorO
  10460. // A only depends on colorO and colorH
  10461. // B only depends on colorO and colorV
  10462. // C only depends on colorH and colorV
  10463. // D depends on all three (colorO, colorH and colorV)
  10464. //
  10465. // Note that B can be precalculated for all combinations of colorO and colorV
  10466. // and the precalculated values can be used instead of calculating it in the inner loop.
  10467. // The same applies to C.
  10468. //
  10469. // In the code below, the squared error over O A A A is calculated and stored in lowest_possible_error
  10470. // Precalc BBB errors
  10471. for(colorO_enc[0] = 0; colorO_enc[0]<64; colorO_enc[0]++)
  10472. {
  10473. for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
  10474. {
  10475. BBBtable[colorO_enc[0]*64+colorV_enc[0]] = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*calcBBBred(block, colorO_enc[0], colorV_enc[0]);
  10476. }
  10477. }
  10478. // Precalc CCC errors
  10479. for(colorH_enc[0] = 0; colorH_enc[0]<64; colorH_enc[0]++)
  10480. {
  10481. for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
  10482. {
  10483. CCCtable[colorH_enc[0]*64+colorV_enc[0]] = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*calcCCCred(block, colorH_enc[0], colorV_enc[0]);
  10484. }
  10485. }
  10486. best_error = MAXERR1000;
  10487. best_error_red_sofar = JAS_MIN(best_error_planar_red, best_error_sofar);
  10488. for(colorO_enc[0] = 0; colorO_enc[0]<64; colorO_enc[0]++)
  10489. {
  10490. for(colorH_enc[0] = 0; colorH_enc[0]<64; colorH_enc[0]++)
  10491. {
  10492. lowest_possible_error = calcLowestPossibleRedOHperceptual(block, colorO_enc[0], colorH_enc[0], best_error_red_sofar);
  10493. if(lowest_possible_error <= best_error_red_sofar)
  10494. {
  10495. for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
  10496. {
  10497. 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);
  10498. if(error < best_error)
  10499. {
  10500. best_error = error;
  10501. best_colorO_enc[0] = colorO_enc[0];
  10502. best_colorH_enc[0] = colorH_enc[0];
  10503. best_colorV_enc[0] = colorV_enc[0];
  10504. }
  10505. }
  10506. }
  10507. }
  10508. }
  10509. if(best_error < best_error_planar_red)
  10510. best_error_planar_red = best_error;
  10511. if(best_error_planar_red > best_error_sofar)
  10512. {
  10513. // The red component in itself is already bigger than the previously best value ---- we can give up.
  10514. // use the dummy color black for all colors and report that the errors for the different color components are infinite
  10515. best_error_planar_green = MAXERR1000;
  10516. best_error_planar_blue = MAXERR1000;
  10517. compressed57_1 = 0;
  10518. compressed57_2 = 0;
  10519. return;
  10520. }
  10521. // The task is to calculate the sum of the error over the entire area of the block.
  10522. //
  10523. // The block can be partitioned into: O A A A
  10524. // B D D C
  10525. // B D C D
  10526. // B C D D
  10527. // where the error in
  10528. // O only depends on colorO
  10529. // A only depends on colorO and colorH
  10530. // B only depends on colorO and colorV
  10531. // C only depends on colorH and colorV
  10532. // D depends on all three (colorO, colorH and colorV)
  10533. //
  10534. // Note that B can be precalculated for all combinations of colorO and colorV
  10535. // and the precalculated values can be used instead of calculating it in the inner loop.
  10536. // The same applies to C.
  10537. //
  10538. // In the code below, the squared error over O A A A is calculated and store in lowest_possible_error
  10539. // Precalc BBB errors
  10540. for(colorO_enc[1] = 0; colorO_enc[1]<128; colorO_enc[1]++)
  10541. {
  10542. for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
  10543. {
  10544. BBBtable[colorO_enc[1]*128+colorV_enc[1]] = PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*calcBBBgreen(block, colorO_enc[1], colorV_enc[1]);
  10545. }
  10546. }
  10547. // Precalc CCC errors
  10548. for(colorH_enc[1] = 0; colorH_enc[1]<128; colorH_enc[1]++)
  10549. {
  10550. for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
  10551. {
  10552. CCCtable[colorH_enc[1]*128+colorV_enc[1]] = PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*calcCCCgreen(block, colorH_enc[1], colorV_enc[1]);
  10553. }
  10554. }
  10555. best_error = MAXERR1000;
  10556. best_error_green_sofar = JAS_MIN(best_error_planar_green, best_error_sofar);
  10557. for(colorO_enc[1] = 0; colorO_enc[1]<128; colorO_enc[1]++)
  10558. {
  10559. for(colorH_enc[1] = 0; colorH_enc[1]<128; colorH_enc[1]++)
  10560. {
  10561. lowest_possible_error = calcLowestPossibleGreenOHperceptual(block, colorO_enc[1], colorH_enc[1], best_error_green_sofar);
  10562. if(lowest_possible_error <= best_error_green_sofar)
  10563. {
  10564. for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
  10565. {
  10566. 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);
  10567. if(error < best_error)
  10568. {
  10569. best_error = error;
  10570. best_colorO_enc[1] = colorO_enc[1];
  10571. best_colorH_enc[1] = colorH_enc[1];
  10572. best_colorV_enc[1] = colorV_enc[1];
  10573. }
  10574. }
  10575. }
  10576. }
  10577. }
  10578. if(best_error < best_error_planar_green)
  10579. best_error_planar_green = best_error;
  10580. if(best_error_planar_red + best_error_planar_green > best_error_sofar)
  10581. {
  10582. // The red component in itself is already bigger than the previously best value ---- we can give up.
  10583. // use the dummy color black for all colors and report that the errors for the different color components are infinite
  10584. best_error_planar_blue = MAXERR1000;
  10585. compressed57_1 = 0;
  10586. compressed57_2 = 0;
  10587. return;
  10588. }
  10589. // The task is to calculate the sum of the error over the entire area of the block.
  10590. //
  10591. // The block can be partitioned into: O A A A
  10592. // B D D C
  10593. // B D C D
  10594. // B C D D
  10595. // where the error in
  10596. // O only depends on colorO
  10597. // A only depends on colorO and colorH
  10598. // B only depends on colorO and colorV
  10599. // C only depends on colorH and colorV
  10600. // D depends on all three (colorO, colorH and colorV)
  10601. //
  10602. // Note that B can be precalculated for all combinations of colorO and colorV
  10603. // and the precalculated values can be used instead of calculating it in the inner loop.
  10604. // The same applies to C.
  10605. //
  10606. // In the code below, the squared error over O A A A is calculated and store in lowest_possible_error
  10607. // Precalc BBB errors
  10608. for(colorO_enc[2] = 0; colorO_enc[2]<64; colorO_enc[2]++)
  10609. {
  10610. for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
  10611. {
  10612. BBBtable[colorO_enc[2]*64+colorV_enc[2]] = calcBBBbluePerceptual(block, colorO_enc[2], colorV_enc[2]);
  10613. }
  10614. }
  10615. // Precalc CCC errors
  10616. for(colorH_enc[2] = 0; colorH_enc[2]<64; colorH_enc[2]++)
  10617. {
  10618. for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
  10619. {
  10620. CCCtable[colorH_enc[2]*64+colorV_enc[2]] = calcCCCbluePerceptual(block, colorH_enc[2], colorV_enc[2]);
  10621. }
  10622. }
  10623. best_error = MAXERR1000;
  10624. best_error_blue_sofar = JAS_MIN(best_error_planar_blue, best_error_sofar);
  10625. for(colorO_enc[2] = 0; colorO_enc[2]<64; colorO_enc[2]++)
  10626. {
  10627. for(colorH_enc[2] = 0; colorH_enc[2]<64; colorH_enc[2]++)
  10628. {
  10629. lowest_possible_error = calcLowestPossibleBlueOHperceptual(block, colorO_enc[2], colorH_enc[2], best_error_blue_sofar);
  10630. if(lowest_possible_error <= best_error_blue_sofar)
  10631. {
  10632. for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
  10633. {
  10634. 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);
  10635. if(error < best_error)
  10636. {
  10637. best_error = error;
  10638. best_colorO_enc[2] = colorO_enc[2];
  10639. best_colorH_enc[2] = colorH_enc[2];
  10640. best_colorV_enc[2] = colorV_enc[2];
  10641. }
  10642. }
  10643. }
  10644. }
  10645. }
  10646. if(best_error < best_error_planar_blue)
  10647. best_error_planar_blue = best_error;
  10648. compressed57_1 = 0;
  10649. compressed57_2 = 0;
  10650. PUTBITSHIGH( compressed57_1, best_colorO_enc[0], 6, 63);
  10651. PUTBITSHIGH( compressed57_1, best_colorO_enc[1], 7, 57);
  10652. PUTBITSHIGH( compressed57_1, best_colorO_enc[2], 6, 50);
  10653. PUTBITSHIGH( compressed57_1, best_colorH_enc[0], 6, 44);
  10654. PUTBITSHIGH( compressed57_1, best_colorH_enc[1], 7, 38);
  10655. PUTBITS( compressed57_2, best_colorH_enc[2], 6, 31);
  10656. PUTBITS( compressed57_2, best_colorV_enc[0], 6, 25);
  10657. PUTBITS( compressed57_2, best_colorV_enc[1], 7, 19);
  10658. PUTBITS( compressed57_2, best_colorV_enc[2], 6, 12);
  10659. }
  10660. #endif
  10661. #if EXHAUSTIVE_CODE_ACTIVE
  10662. // This function uses real exhaustive search for the planar mode.
  10663. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  10664. 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)
  10665. {
  10666. int colorO_enc[3], colorH_enc[3], colorV_enc[3];
  10667. int best_colorO_enc[3], best_colorH_enc[3], best_colorV_enc[3];
  10668. unsigned int error;
  10669. unsigned int best_error;
  10670. unsigned int lowest_possible_error;
  10671. unsigned int best_error_red_sofar;
  10672. unsigned int best_error_green_sofar;
  10673. unsigned int best_error_blue_sofar;
  10674. unsigned int BBBtable[128*128];
  10675. unsigned int CCCtable[128*128];
  10676. uint8 block[4*4*4];
  10677. // Use 4 bytes per pixel to make it 32-word aligned.
  10678. int count = 0;
  10679. int xx, yy;
  10680. for(yy=0; yy<4; yy++)
  10681. {
  10682. for(xx = 0; xx<4; xx++)
  10683. {
  10684. block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  10685. block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  10686. block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  10687. block[(count)*4+3] = 0;
  10688. count++;
  10689. }
  10690. }
  10691. // The task is to calculate the sum of the error over the entire area of the block.
  10692. //
  10693. // The block can be partitioned into: O A A A
  10694. // B D D C
  10695. // B D C D
  10696. // B C D D
  10697. // where the error in
  10698. // O only depends on colorO
  10699. // A only depends on colorO and colorH
  10700. // B only depends on colorO and colorV
  10701. // C only depends on colorH and colorV
  10702. // D depends on all three (colorO, colorH and colorV)
  10703. //
  10704. // Note that B can be precalculated for all combinations of colorO and colorV
  10705. // and the precalculated values can be used instead of calculating it in the inner loop.
  10706. // The same applies to C.
  10707. //
  10708. // In the code below, the squared error over O A A A is calculated and store in lowest_possible_error
  10709. // Precalc BBB errors
  10710. for(colorO_enc[0] = 0; colorO_enc[0]<64; colorO_enc[0]++)
  10711. {
  10712. for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
  10713. {
  10714. BBBtable[colorO_enc[0]*64+colorV_enc[0]] = calcBBBred(block, colorO_enc[0], colorV_enc[0]);
  10715. }
  10716. }
  10717. // Precalc CCC errors
  10718. for(colorH_enc[0] = 0; colorH_enc[0]<64; colorH_enc[0]++)
  10719. {
  10720. for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
  10721. {
  10722. CCCtable[colorH_enc[0]*64+colorV_enc[0]] = calcCCCred(block, colorH_enc[0], colorV_enc[0]);
  10723. }
  10724. }
  10725. best_error = MAXERR1000;
  10726. best_error_red_sofar = JAS_MIN(best_error_red, best_error_sofar);
  10727. for(colorO_enc[0] = 0; colorO_enc[0]<64; colorO_enc[0]++)
  10728. {
  10729. for(colorH_enc[0] = 0; colorH_enc[0]<64; colorH_enc[0]++)
  10730. {
  10731. lowest_possible_error = calcLowestPossibleRedOH(block, colorO_enc[0], colorH_enc[0], best_error_red_sofar);
  10732. if(lowest_possible_error <= best_error_red_sofar)
  10733. {
  10734. for(colorV_enc[0] = 0; colorV_enc[0]<64; colorV_enc[0]++)
  10735. {
  10736. 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);
  10737. if(error < best_error)
  10738. {
  10739. best_error = error;
  10740. best_colorO_enc[0] = colorO_enc[0];
  10741. best_colorH_enc[0] = colorH_enc[0];
  10742. best_colorV_enc[0] = colorV_enc[0];
  10743. }
  10744. }
  10745. }
  10746. }
  10747. }
  10748. // The task is to calculate the sum of the error over the entire area of the block.
  10749. //
  10750. // The block can be partitioned into: O A A A
  10751. // B D D C
  10752. // B D C D
  10753. // B C D D
  10754. // where the error in
  10755. // O only depends on colorO
  10756. // A only depends on colorO and colorH
  10757. // B only depends on colorO and colorV
  10758. // C only depends on colorH and colorV
  10759. // D depends on all three (colorO, colorH and colorV)
  10760. //
  10761. // Note that B can be precalculated for all combinations of colorO and colorV
  10762. // and the precalculated values can be used instead of calculating it in the inner loop.
  10763. // The same applies to C.
  10764. //
  10765. // In the code below, the squared error over O A A A is calculated and store in lowest_possible_error
  10766. // Precalc BBB errors
  10767. for(colorO_enc[1] = 0; colorO_enc[1]<128; colorO_enc[1]++)
  10768. {
  10769. for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
  10770. {
  10771. BBBtable[colorO_enc[1]*128+colorV_enc[1]] = calcBBBgreen(block, colorO_enc[1], colorV_enc[1]);
  10772. }
  10773. }
  10774. // Precalc CCC errors
  10775. for(colorH_enc[1] = 0; colorH_enc[1]<128; colorH_enc[1]++)
  10776. {
  10777. for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
  10778. {
  10779. CCCtable[colorH_enc[1]*128+colorV_enc[1]] = calcCCCgreen(block, colorH_enc[1], colorV_enc[1]);
  10780. }
  10781. }
  10782. best_error = MAXERR1000;
  10783. best_error_green_sofar = JAS_MIN(best_error_green, best_error_sofar);
  10784. for(colorO_enc[1] = 0; colorO_enc[1]<128; colorO_enc[1]++)
  10785. {
  10786. for(colorH_enc[1] = 0; colorH_enc[1]<128; colorH_enc[1]++)
  10787. {
  10788. lowest_possible_error = calcLowestPossibleGreenOH(block, colorO_enc[1], colorH_enc[1], best_error_green_sofar);
  10789. if(lowest_possible_error <= best_error_green_sofar)
  10790. {
  10791. for(colorV_enc[1] = 0; colorV_enc[1]<128; colorV_enc[1]++)
  10792. {
  10793. 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);
  10794. if(error < best_error)
  10795. {
  10796. best_error = error;
  10797. best_colorO_enc[1] = colorO_enc[1];
  10798. best_colorH_enc[1] = colorH_enc[1];
  10799. best_colorV_enc[1] = colorV_enc[1];
  10800. }
  10801. }
  10802. }
  10803. }
  10804. }
  10805. // The task is to calculate the sum of the error over the entire area of the block.
  10806. //
  10807. // The block can be partitioned into: O A A A
  10808. // B D D C
  10809. // B D C D
  10810. // B C D D
  10811. // where the error in
  10812. // O only depends on colorO
  10813. // A only depends on colorO and colorH
  10814. // B only depends on colorO and colorV
  10815. // C only depends on colorH and colorV
  10816. // D depends on all three (colorO, colorH and colorV)
  10817. //
  10818. // Note that B can be precalculated for all combinations of colorO and colorV
  10819. // and the precalculated values can be used instead of calculating it in the inner loop.
  10820. // The same applies to C.
  10821. //
  10822. // In the code below, the squared error over O A A A is calculated and store in lowest_possible_error
  10823. // Precalc BBB errors
  10824. for(colorO_enc[2] = 0; colorO_enc[2]<64; colorO_enc[2]++)
  10825. {
  10826. for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
  10827. {
  10828. BBBtable[colorO_enc[2]*64+colorV_enc[2]] = calcBBBblue(block, colorO_enc[2], colorV_enc[2]);
  10829. }
  10830. }
  10831. // Precalc CCC errors
  10832. for(colorH_enc[2] = 0; colorH_enc[2]<64; colorH_enc[2]++)
  10833. {
  10834. for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
  10835. {
  10836. CCCtable[colorH_enc[2]*64+colorV_enc[2]] = calcCCCblue(block, colorH_enc[2], colorV_enc[2]);
  10837. }
  10838. }
  10839. best_error = MAXERR1000;
  10840. best_error_blue_sofar = JAS_MIN(best_error_blue, best_error_sofar);
  10841. for(colorO_enc[2] = 0; colorO_enc[2]<64; colorO_enc[2]++)
  10842. {
  10843. for(colorH_enc[2] = 0; colorH_enc[2]<64; colorH_enc[2]++)
  10844. {
  10845. lowest_possible_error = calcLowestPossibleBlueOH(block, colorO_enc[2], colorH_enc[2], best_error_blue_sofar);
  10846. if(lowest_possible_error <= best_error_blue_sofar)
  10847. {
  10848. for(colorV_enc[2] = 0; colorV_enc[2]<64; colorV_enc[2]++)
  10849. {
  10850. 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);
  10851. if(error < best_error)
  10852. {
  10853. best_error = error;
  10854. best_colorO_enc[2] = colorO_enc[2];
  10855. best_colorH_enc[2] = colorH_enc[2];
  10856. best_colorV_enc[2] = colorV_enc[2];
  10857. }
  10858. }
  10859. }
  10860. }
  10861. }
  10862. compressed57_1 = 0;
  10863. compressed57_2 = 0;
  10864. PUTBITSHIGH( compressed57_1, best_colorO_enc[0], 6, 63);
  10865. PUTBITSHIGH( compressed57_1, best_colorO_enc[1], 7, 57);
  10866. PUTBITSHIGH( compressed57_1, best_colorO_enc[2], 6, 50);
  10867. PUTBITSHIGH( compressed57_1, best_colorH_enc[0], 6, 44);
  10868. PUTBITSHIGH( compressed57_1, best_colorH_enc[1], 7, 38);
  10869. PUTBITS( compressed57_2, best_colorH_enc[2], 6, 31);
  10870. PUTBITS( compressed57_2, best_colorV_enc[0], 6, 25);
  10871. PUTBITS( compressed57_2, best_colorV_enc[1], 7, 19);
  10872. PUTBITS( compressed57_2, best_colorV_enc[2], 6, 12);
  10873. }
  10874. #endif
  10875. #if EXHAUSTIVE_CODE_ACTIVE
  10876. // Precalculates a table used in exhaustive compression of the T-mode.
  10877. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  10878. void precalcError59T_col0_Rpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_R)
  10879. {
  10880. unsigned int block_error = 0,
  10881. best_block_error = MAXERR1000,
  10882. pixel_error,
  10883. best_pixel_error;
  10884. int diff;
  10885. uint8 color;
  10886. uint8 possible_colors[3];
  10887. color = ((colorRGB444_packed >> 8) & 0xf)*17;
  10888. // Test all distances
  10889. for (uint8 d = 0; d < 8; d++)
  10890. {
  10891. possible_colors[0] = CLAMP(0,color - table59T[d],255);
  10892. possible_colors[1] = CLAMP(0,color,255);
  10893. possible_colors[2] = CLAMP(0,color + table59T[d],255);
  10894. // Loop block
  10895. for (int x = 0; x < 16; x++)
  10896. {
  10897. best_pixel_error = MAXERR1000;
  10898. // Loop possible block colors
  10899. for (uint8 c = 0; c < 3; c++)
  10900. {
  10901. diff = block[4*x + R] - CLAMP(0,possible_colors[c],255);
  10902. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff);
  10903. // Choose best error
  10904. if (pixel_error < best_pixel_error)
  10905. best_pixel_error = pixel_error;
  10906. }
  10907. precalc_err_col0_R[((colorRGB444_packed>>8)*8 + d)*16 + x] = (unsigned int) best_pixel_error;
  10908. }
  10909. }
  10910. }
  10911. #endif
  10912. #if EXHAUSTIVE_CODE_ACTIVE
  10913. // Precalculates a table used in exhaustive compression of the T-mode.
  10914. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  10915. void precalcError59T_col0_R(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_R)
  10916. {
  10917. unsigned int block_error = 0,
  10918. best_block_error = MAXIMUM_ERROR,
  10919. pixel_error,
  10920. best_pixel_error;
  10921. int diff;
  10922. uint8 color;
  10923. uint8 possible_colors[3];
  10924. color = ((colorRGB444_packed >> 8) & 0xf)*17;
  10925. // Test all distances
  10926. for (uint8 d = 0; d < 8; d++)
  10927. {
  10928. possible_colors[0] = CLAMP(0,color - table59T[d],255);
  10929. possible_colors[1] = CLAMP(0,color,255);
  10930. possible_colors[2] = CLAMP(0,color + table59T[d],255);
  10931. // Loop block
  10932. for (int x = 0; x < 16; x++)
  10933. {
  10934. best_pixel_error = MAXIMUM_ERROR;
  10935. // Loop possible block colors
  10936. for (uint8 c = 0; c < 3; c++)
  10937. {
  10938. diff = block[4*x + R] - CLAMP(0,possible_colors[c],255);
  10939. pixel_error = SQUARE(diff);
  10940. // Choose best error
  10941. if (pixel_error < best_pixel_error)
  10942. best_pixel_error = pixel_error;
  10943. }
  10944. precalc_err_col0_R[((colorRGB444_packed>>8)*8 + d)*16 + x] = (unsigned int) best_pixel_error;
  10945. }
  10946. }
  10947. }
  10948. #endif
  10949. #if EXHAUSTIVE_CODE_ACTIVE
  10950. // Precalculates a table used in exhaustive compression of the T-mode.
  10951. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  10952. void precalcError59T_col0_RGpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_RG)
  10953. {
  10954. unsigned int block_error = 0,
  10955. best_block_error = MAXERR1000,
  10956. pixel_error,
  10957. best_pixel_error;
  10958. int diff[3];
  10959. uint8 color[3];
  10960. uint8 possible_colors[3][2];
  10961. color[R] = ((colorRGB444_packed >> 8) & 0xf)*17;
  10962. color[G] = ((colorRGB444_packed >> 4) & 0xf)*17;
  10963. // Test all distances
  10964. for (uint8 d = 0; d < 8; d++)
  10965. {
  10966. possible_colors[0][R] = CLAMP(0,color[R] - table59T[d],255);
  10967. possible_colors[0][G] = CLAMP(0,color[G] - table59T[d],255);
  10968. possible_colors[1][R] = CLAMP(0,color[R],255);
  10969. possible_colors[1][G] = CLAMP(0,color[G],255);
  10970. possible_colors[2][R] = CLAMP(0,color[R] + table59T[d],255);
  10971. possible_colors[2][G] = CLAMP(0,color[G] + table59T[d],255);
  10972. // Loop block
  10973. for (int x = 0; x < 16; x++)
  10974. {
  10975. best_pixel_error = MAXERR1000;
  10976. // Loop possible block colors
  10977. for (uint8 c = 0; c < 3; c++)
  10978. {
  10979. diff[R] = block[4*x + R] - CLAMP(0,possible_colors[c][R],255);
  10980. diff[G] = block[4*x + G] - CLAMP(0,possible_colors[c][G],255);
  10981. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]);
  10982. // Choose best error
  10983. if (pixel_error < best_pixel_error)
  10984. best_pixel_error = pixel_error;
  10985. }
  10986. precalc_err_col0_RG[((colorRGB444_packed>>4)*8 + d)*16 + x] = (unsigned int) best_pixel_error;
  10987. }
  10988. }
  10989. }
  10990. #endif
  10991. #if EXHAUSTIVE_CODE_ACTIVE
  10992. // Precalculates a table used in exhaustive compression of the T-mode.
  10993. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  10994. void precalcError59T_col0_RG(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_RG)
  10995. {
  10996. unsigned int block_error = 0,
  10997. best_block_error = MAXIMUM_ERROR,
  10998. pixel_error,
  10999. best_pixel_error;
  11000. int diff[3];
  11001. uint8 color[3];
  11002. uint8 possible_colors[3][2];
  11003. color[R] = ((colorRGB444_packed >> 8) & 0xf)*17;
  11004. color[G] = ((colorRGB444_packed >> 4) & 0xf)*17;
  11005. // Test all distances
  11006. for (uint8 d = 0; d < 8; d++)
  11007. {
  11008. possible_colors[0][R] = CLAMP(0,color[R] - table59T[d],255);
  11009. possible_colors[0][G] = CLAMP(0,color[G] - table59T[d],255);
  11010. possible_colors[1][R] = CLAMP(0,color[R],255);
  11011. possible_colors[1][G] = CLAMP(0,color[G],255);
  11012. possible_colors[2][R] = CLAMP(0,color[R] + table59T[d],255);
  11013. possible_colors[2][G] = CLAMP(0,color[G] + table59T[d],255);
  11014. // Loop block
  11015. for (int x = 0; x < 16; x++)
  11016. {
  11017. best_pixel_error = MAXIMUM_ERROR;
  11018. // Loop possible block colors
  11019. for (uint8 c = 0; c < 3; c++)
  11020. {
  11021. diff[R] = block[4*x + R] - CLAMP(0,possible_colors[c][R],255);
  11022. diff[G] = block[4*x + G] - CLAMP(0,possible_colors[c][G],255);
  11023. pixel_error = SQUARE(diff[R]) + SQUARE(diff[G]);
  11024. // Choose best error
  11025. if (pixel_error < best_pixel_error)
  11026. best_pixel_error = pixel_error;
  11027. }
  11028. precalc_err_col0_RG[((colorRGB444_packed>>4)*8 + d)*16 + x] = (unsigned int) best_pixel_error;
  11029. }
  11030. }
  11031. }
  11032. #endif
  11033. #if EXHAUSTIVE_CODE_ACTIVE
  11034. // Precalculates a table used in exhaustive compression of the T-mode.
  11035. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11036. void precalcError59T_col1_Rpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_R)
  11037. {
  11038. unsigned int pixel_error;
  11039. int diff;
  11040. uint8 color;
  11041. color = ((colorRGB444_packed >> 8) & 0xf)*17;
  11042. // Loop block
  11043. for (int x = 0; x < 16; x++)
  11044. {
  11045. diff = block[4*x + R] - color;
  11046. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff);
  11047. precalc_err_col1_R[((colorRGB444_packed>>8))*16 + x] = (unsigned int) pixel_error;
  11048. }
  11049. }
  11050. #endif
  11051. #if EXHAUSTIVE_CODE_ACTIVE
  11052. /**
  11053. * Calculate the error for the block at position (startx,starty)
  11054. * The parameters needed for reconstruction is calculated as well
  11055. *
  11056. * In the 59T bit mode, we only have pattern T.
  11057. */
  11058. void precalcError59T_col1_R(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_R)
  11059. {
  11060. unsigned int pixel_error;
  11061. int diff;
  11062. uint8 color;
  11063. color = ((colorRGB444_packed >> 8) & 0xf)*17;
  11064. // Loop block
  11065. for (int x = 0; x < 16; x++)
  11066. {
  11067. diff = block[4*x + R] - color;
  11068. pixel_error = SQUARE(diff);
  11069. precalc_err_col1_R[((colorRGB444_packed>>8))*16 + x] = (unsigned int) pixel_error;
  11070. }
  11071. }
  11072. #endif
  11073. #if EXHAUSTIVE_CODE_ACTIVE
  11074. // Precalculates a table used in exhaustive compression of the T-mode.
  11075. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11076. void precalcError59T_col1_RGpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_RG)
  11077. {
  11078. unsigned int pixel_error;
  11079. int diff[3];
  11080. uint8 color[2];
  11081. color[R] = ((colorRGB444_packed >> 8) & 0xf)*17;
  11082. color[G] = ((colorRGB444_packed >> 4) & 0xf)*17;
  11083. // Loop block
  11084. for (int x = 0; x < 16; x++)
  11085. {
  11086. diff[R] = block[4*x + R] - color[R];
  11087. diff[G] = block[4*x + G] - color[G];
  11088. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]);
  11089. precalc_err_col1_RG[((colorRGB444_packed>>4))*16 + x] = (unsigned int) pixel_error;
  11090. }
  11091. }
  11092. #endif
  11093. #if EXHAUSTIVE_CODE_ACTIVE
  11094. // Precalculates a table used in exhaustive compression of the T-mode.
  11095. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11096. void precalcError59T_col1_RG(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_RG)
  11097. {
  11098. unsigned int pixel_error;
  11099. int diff[3];
  11100. uint8 color[2];
  11101. color[R] = ((colorRGB444_packed >> 8) & 0xf)*17;
  11102. color[G] = ((colorRGB444_packed >> 4) & 0xf)*17;
  11103. // Loop block
  11104. for (int x = 0; x < 16; x++)
  11105. {
  11106. diff[R] = block[4*x + R] - color[R];
  11107. diff[G] = block[4*x + G] - color[G];
  11108. pixel_error = SQUARE(diff[R]) + SQUARE(diff[G]);
  11109. precalc_err_col1_RG[((colorRGB444_packed>>4))*16 + x] = (unsigned int) pixel_error;
  11110. }
  11111. }
  11112. #endif
  11113. #if EXHAUSTIVE_CODE_ACTIVE
  11114. // Precalculates a table used in exhaustive compression of the T-mode.
  11115. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11116. void precalcError59T_col0_RGBpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_RGB)
  11117. {
  11118. unsigned int block_error = 0,
  11119. best_block_error = MAXERR1000,
  11120. pixel_error,
  11121. best_pixel_error;
  11122. uint8 color[3];
  11123. int possible_colors[3][3];
  11124. unsigned int *precalc_err_col0_RGB_adr;
  11125. #define ONEPOINT59RGB_PERCEP(xval) \
  11126. /* Loop possible block colors */\
  11127. /* unroll loop for (uint8 c = 0; c < 3; c++) */\
  11128. {\
  11129. best_pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*square_table[block[4*xval + R] - possible_colors[0][R]]\
  11130. + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*square_table[block[4*xval + G] - possible_colors[0][G]] \
  11131. + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[block[4*xval + B] - possible_colors[0][B]];\
  11132. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*square_table[block[4*xval + R] - possible_colors[1][R]]\
  11133. + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*square_table[block[4*xval + G] - possible_colors[1][G]]\
  11134. + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[block[4*xval + B] - possible_colors[1][B]];\
  11135. if (pixel_error < best_pixel_error)\
  11136. best_pixel_error = pixel_error;\
  11137. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*square_table[block[4*xval + R] - possible_colors[2][R]]\
  11138. + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*square_table[block[4*xval + G] - possible_colors[2][G]]\
  11139. + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[block[4*xval + B] - possible_colors[2][B]];\
  11140. if (pixel_error < best_pixel_error)\
  11141. best_pixel_error = pixel_error;\
  11142. }\
  11143. precalc_err_col0_RGB_adr[xval] = (unsigned int) best_pixel_error;\
  11144. #define ONETABLE59RGB_PERCEP(dval) \
  11145. possible_colors[0][R] = clamp_table[color[R] - table59T[dval]+255]-255;\
  11146. possible_colors[0][G] = clamp_table[color[G] - table59T[dval]+255]-255;\
  11147. possible_colors[0][B] = clamp_table[color[B] - table59T[dval]+255]-255;\
  11148. possible_colors[1][R] = color[R]-255;\
  11149. possible_colors[1][G] = color[G]-255;\
  11150. possible_colors[1][B] = color[B]-255;\
  11151. possible_colors[2][R] = clamp_table[color[R] + table59T[dval]+255]-255;\
  11152. possible_colors[2][G] = clamp_table[color[G] + table59T[dval]+255]-255;\
  11153. possible_colors[2][B] = clamp_table[color[B] + table59T[dval]+255]-255;\
  11154. precalc_err_col0_RGB_adr = &precalc_err_col0_RGB[(colorRGB444_packed*8 + dval)*16];\
  11155. /* Loop block */\
  11156. /* unroll loop for (int x = 0; x < 16; x++) */\
  11157. {\
  11158. ONEPOINT59RGB_PERCEP(0)\
  11159. ONEPOINT59RGB_PERCEP(1)\
  11160. ONEPOINT59RGB_PERCEP(2)\
  11161. ONEPOINT59RGB_PERCEP(3)\
  11162. ONEPOINT59RGB_PERCEP(4)\
  11163. ONEPOINT59RGB_PERCEP(5)\
  11164. ONEPOINT59RGB_PERCEP(6)\
  11165. ONEPOINT59RGB_PERCEP(7)\
  11166. ONEPOINT59RGB_PERCEP(8)\
  11167. ONEPOINT59RGB_PERCEP(9)\
  11168. ONEPOINT59RGB_PERCEP(10)\
  11169. ONEPOINT59RGB_PERCEP(11)\
  11170. ONEPOINT59RGB_PERCEP(12)\
  11171. ONEPOINT59RGB_PERCEP(13)\
  11172. ONEPOINT59RGB_PERCEP(14)\
  11173. ONEPOINT59RGB_PERCEP(15)\
  11174. }\
  11175. color[R] = (((colorRGB444_packed >> 8) ) << 4) | ((colorRGB444_packed >> 8) ) ;
  11176. color[G] = (((colorRGB444_packed >> 4) & 0xf) << 4) | ((colorRGB444_packed >> 4) & 0xf) ;
  11177. color[B] = (((colorRGB444_packed) & 0xf) << 4) | ((colorRGB444_packed) & 0xf) ;
  11178. /* Test all distances */
  11179. /* unroll loop for (uint8 d = 0; d < 8; ++d) */
  11180. {
  11181. ONETABLE59RGB_PERCEP(0)
  11182. ONETABLE59RGB_PERCEP(1)
  11183. ONETABLE59RGB_PERCEP(2)
  11184. ONETABLE59RGB_PERCEP(3)
  11185. ONETABLE59RGB_PERCEP(4)
  11186. ONETABLE59RGB_PERCEP(5)
  11187. ONETABLE59RGB_PERCEP(6)
  11188. ONETABLE59RGB_PERCEP(7)
  11189. }
  11190. }
  11191. #endif
  11192. #if EXHAUSTIVE_CODE_ACTIVE
  11193. // Precalculates a table used in exhaustive compression of the T-mode.
  11194. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11195. void precalcError59T_col0_RGB(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col0_RGB)
  11196. {
  11197. unsigned int block_error = 0,
  11198. best_block_error = MAXIMUM_ERROR,
  11199. pixel_error,
  11200. best_pixel_error;
  11201. uint8 color[3];
  11202. int possible_colors[3][3];
  11203. unsigned int *precalc_err_col0_RGB_adr;
  11204. #define ONEPOINT59RGB(xval) \
  11205. /* Loop possible block colors */\
  11206. /* unroll loop for (uint8 c = 0; c < 3; c++) */\
  11207. {\
  11208. best_pixel_error = square_table[block[4*xval + R] - possible_colors[0][R]]\
  11209. + square_table[block[4*xval + G] - possible_colors[0][G]] \
  11210. + square_table[block[4*xval + B] - possible_colors[0][B]];\
  11211. pixel_error = square_table[block[4*xval + R] - possible_colors[1][R]]\
  11212. + square_table[block[4*xval + G] - possible_colors[1][G]]\
  11213. + square_table[block[4*xval + B] - possible_colors[1][B]];\
  11214. if (pixel_error < best_pixel_error)\
  11215. best_pixel_error = pixel_error;\
  11216. pixel_error = square_table[block[4*xval + R] - possible_colors[2][R]]\
  11217. + square_table[block[4*xval + G] - possible_colors[2][G]]\
  11218. + square_table[block[4*xval + B] - possible_colors[2][B]];\
  11219. if (pixel_error < best_pixel_error)\
  11220. best_pixel_error = pixel_error;\
  11221. }\
  11222. precalc_err_col0_RGB_adr[xval] = (unsigned int) best_pixel_error;\
  11223. #define ONETABLE59RGB(dval) \
  11224. possible_colors[0][R] = clamp_table[color[R] - table59T[dval]+255]-255;\
  11225. possible_colors[0][G] = clamp_table[color[G] - table59T[dval]+255]-255;\
  11226. possible_colors[0][B] = clamp_table[color[B] - table59T[dval]+255]-255;\
  11227. possible_colors[1][R] = color[R]-255;\
  11228. possible_colors[1][G] = color[G]-255;\
  11229. possible_colors[1][B] = color[B]-255;\
  11230. possible_colors[2][R] = clamp_table[color[R] + table59T[dval]+255]-255;\
  11231. possible_colors[2][G] = clamp_table[color[G] + table59T[dval]+255]-255;\
  11232. possible_colors[2][B] = clamp_table[color[B] + table59T[dval]+255]-255;\
  11233. precalc_err_col0_RGB_adr = &precalc_err_col0_RGB[(colorRGB444_packed*8 + dval)*16];\
  11234. /* Loop block */\
  11235. /* unroll loop for (int x = 0; x < 16; x++) */\
  11236. {\
  11237. ONEPOINT59RGB(0)\
  11238. ONEPOINT59RGB(1)\
  11239. ONEPOINT59RGB(2)\
  11240. ONEPOINT59RGB(3)\
  11241. ONEPOINT59RGB(4)\
  11242. ONEPOINT59RGB(5)\
  11243. ONEPOINT59RGB(6)\
  11244. ONEPOINT59RGB(7)\
  11245. ONEPOINT59RGB(8)\
  11246. ONEPOINT59RGB(9)\
  11247. ONEPOINT59RGB(10)\
  11248. ONEPOINT59RGB(11)\
  11249. ONEPOINT59RGB(12)\
  11250. ONEPOINT59RGB(13)\
  11251. ONEPOINT59RGB(14)\
  11252. ONEPOINT59RGB(15)\
  11253. }\
  11254. color[R] = (((colorRGB444_packed >> 8) ) << 4) | ((colorRGB444_packed >> 8) ) ;
  11255. color[G] = (((colorRGB444_packed >> 4) & 0xf) << 4) | ((colorRGB444_packed >> 4) & 0xf) ;
  11256. color[B] = (((colorRGB444_packed) & 0xf) << 4) | ((colorRGB444_packed) & 0xf) ;
  11257. /* Test all distances */
  11258. /* unroll loop for (uint8 d = 0; d < 8; ++d) */
  11259. {
  11260. ONETABLE59RGB(0)
  11261. ONETABLE59RGB(1)
  11262. ONETABLE59RGB(2)
  11263. ONETABLE59RGB(3)
  11264. ONETABLE59RGB(4)
  11265. ONETABLE59RGB(5)
  11266. ONETABLE59RGB(6)
  11267. ONETABLE59RGB(7)
  11268. }
  11269. }
  11270. #endif
  11271. #if EXHAUSTIVE_CODE_ACTIVE
  11272. // Precalculates a table used in exhaustive compression of the T-mode.
  11273. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11274. void precalcError59T_col1_RGBpercep1000(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_RGB)
  11275. {
  11276. unsigned int pixel_error;
  11277. int diff[3];
  11278. uint8 colorRGB[3];
  11279. colorRGB[0] = ((colorRGB444_packed >> 8) & 0xf)*17;
  11280. colorRGB[1] = ((colorRGB444_packed >> 4) & 0xf)*17;
  11281. colorRGB[2] = ((colorRGB444_packed >> 0) & 0xf)*17;
  11282. // Loop block
  11283. for (int x = 0; x < 16; x++)
  11284. {
  11285. diff[R] = block[4*x + R] - colorRGB[R];
  11286. diff[G] = block[4*x + G] - colorRGB[G];
  11287. diff[B] = block[4*x + B] - colorRGB[B];
  11288. 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]);
  11289. precalc_err_col1_RGB[(colorRGB444_packed)*16 + x] = (unsigned int) pixel_error;
  11290. }
  11291. }
  11292. #endif
  11293. #if EXHAUSTIVE_CODE_ACTIVE
  11294. // Precalculates a table used in exhaustive compression of the T-mode.
  11295. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11296. void precalcError59T_col1_RGB(uint8* block, int colorRGB444_packed, unsigned int *precalc_err_col1_RGB)
  11297. {
  11298. unsigned int pixel_error;
  11299. int diff[3];
  11300. uint8 colorRGB[3];
  11301. colorRGB[0] = ((colorRGB444_packed >> 8) & 0xf)*17;
  11302. colorRGB[1] = ((colorRGB444_packed >> 4) & 0xf)*17;
  11303. colorRGB[2] = ((colorRGB444_packed >> 0) & 0xf)*17;
  11304. // Loop block
  11305. for (int x = 0; x < 16; x++)
  11306. {
  11307. diff[R] = block[4*x + R] - colorRGB[R];
  11308. diff[G] = block[4*x + G] - colorRGB[G];
  11309. diff[B] = block[4*x + B] - colorRGB[B];
  11310. pixel_error = SQUARE(diff[R]) + SQUARE(diff[G]) + SQUARE(diff[B]);
  11311. precalc_err_col1_RGB[(colorRGB444_packed)*16 + x] = (unsigned int) pixel_error;
  11312. }
  11313. }
  11314. #endif
  11315. #if EXHAUSTIVE_CODE_ACTIVE
  11316. // Calculate a minimal error for the T-mode when compressing exhaustively.
  11317. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11318. 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)
  11319. {
  11320. unsigned int block_error = 0,
  11321. best_block_error = MAXERR1000;
  11322. unsigned int *pixel_error_col0_base_adr;
  11323. unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
  11324. #define FIRSTCHOICE59R_PERCEP\
  11325. if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
  11326. block_error = *pixel_error_col0_adr;\
  11327. else\
  11328. block_error = *pixel_error_col1_adr;\
  11329. #define CHOICE59R_PERCEP(xval)\
  11330. if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
  11331. block_error += pixel_error_col0_adr[xval];\
  11332. else\
  11333. block_error += pixel_error_col1_adr[xval];\
  11334. #define ONETABLE59R_PERCEP(dval) \
  11335. pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
  11336. /* unroll loop for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
  11337. {\
  11338. FIRSTCHOICE59R_PERCEP\
  11339. if( block_error < best_error_so_far)\
  11340. {\
  11341. CHOICE59R_PERCEP(1)\
  11342. if( block_error < best_error_so_far)\
  11343. {\
  11344. CHOICE59R_PERCEP(2)\
  11345. CHOICE59R_PERCEP(3)\
  11346. if( block_error < best_error_so_far)\
  11347. {\
  11348. CHOICE59R_PERCEP(4)\
  11349. CHOICE59R_PERCEP(5)\
  11350. if( block_error < best_error_so_far)\
  11351. {\
  11352. CHOICE59R_PERCEP(6)\
  11353. CHOICE59R_PERCEP(7)\
  11354. if( block_error < best_error_so_far)\
  11355. {\
  11356. CHOICE59R_PERCEP(8)\
  11357. CHOICE59R_PERCEP(9)\
  11358. if( block_error < best_error_so_far)\
  11359. {\
  11360. CHOICE59R_PERCEP(10)\
  11361. CHOICE59R_PERCEP(11)\
  11362. if( block_error < best_error_so_far)\
  11363. {\
  11364. CHOICE59R_PERCEP(12)\
  11365. CHOICE59R_PERCEP(13)\
  11366. if( block_error < best_error_so_far)\
  11367. {\
  11368. CHOICE59R_PERCEP(14)\
  11369. CHOICE59R_PERCEP(15)\
  11370. }\
  11371. }\
  11372. }\
  11373. }\
  11374. }\
  11375. }\
  11376. }\
  11377. }\
  11378. }\
  11379. if (block_error < best_block_error)\
  11380. best_block_error = block_error;\
  11381. pixel_error_col0_base_adr = &precalc_err_col0_R[((colorsRGB444_packed[0]>>8)*8)*16];
  11382. pixel_error_col1_adr = &precalc_err_col1_R[((colorsRGB444_packed[1]>>8))*16];
  11383. // Test all distances
  11384. /* unroll loop for (uint8 d = 0; d < 8; d++) */
  11385. {
  11386. ONETABLE59R_PERCEP(0)
  11387. ONETABLE59R_PERCEP(1)
  11388. ONETABLE59R_PERCEP(2)
  11389. ONETABLE59R_PERCEP(3)
  11390. ONETABLE59R_PERCEP(4)
  11391. ONETABLE59R_PERCEP(5)
  11392. ONETABLE59R_PERCEP(6)
  11393. ONETABLE59R_PERCEP(7)
  11394. }
  11395. return best_block_error;
  11396. }
  11397. #endif
  11398. #if EXHAUSTIVE_CODE_ACTIVE
  11399. // Calculate a minimal error for the T-mode when compressing exhaustively.
  11400. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11401. 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)
  11402. {
  11403. unsigned int block_error = 0,
  11404. best_block_error = MAXIMUM_ERROR;
  11405. unsigned int *pixel_error_col0_base_adr;
  11406. unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
  11407. #define FIRSTCHOICE59R\
  11408. if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
  11409. block_error = *pixel_error_col0_adr;\
  11410. else\
  11411. block_error = *pixel_error_col1_adr;\
  11412. #define CHOICE59R(xval)\
  11413. if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
  11414. block_error += pixel_error_col0_adr[xval];\
  11415. else\
  11416. block_error += pixel_error_col1_adr[xval];\
  11417. #define ONETABLE59R(dval) \
  11418. pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
  11419. /* unroll loop for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
  11420. {\
  11421. FIRSTCHOICE59R\
  11422. if( block_error < best_error_so_far)\
  11423. {\
  11424. CHOICE59R(1)\
  11425. if( block_error < best_error_so_far)\
  11426. {\
  11427. CHOICE59R(2)\
  11428. CHOICE59R(3)\
  11429. if( block_error < best_error_so_far)\
  11430. {\
  11431. CHOICE59R(4)\
  11432. CHOICE59R(5)\
  11433. if( block_error < best_error_so_far)\
  11434. {\
  11435. CHOICE59R(6)\
  11436. CHOICE59R(7)\
  11437. if( block_error < best_error_so_far)\
  11438. {\
  11439. CHOICE59R(8)\
  11440. CHOICE59R(9)\
  11441. if( block_error < best_error_so_far)\
  11442. {\
  11443. CHOICE59R(10)\
  11444. CHOICE59R(11)\
  11445. if( block_error < best_error_so_far)\
  11446. {\
  11447. CHOICE59R(12)\
  11448. CHOICE59R(13)\
  11449. if( block_error < best_error_so_far)\
  11450. {\
  11451. CHOICE59R(14)\
  11452. CHOICE59R(15)\
  11453. }\
  11454. }\
  11455. }\
  11456. }\
  11457. }\
  11458. }\
  11459. }\
  11460. }\
  11461. }\
  11462. if (block_error < best_block_error)\
  11463. best_block_error = block_error;\
  11464. pixel_error_col0_base_adr = &precalc_err_col0_R[((colorsRGB444_packed[0]>>8)*8)*16];
  11465. pixel_error_col1_adr = &precalc_err_col1_R[((colorsRGB444_packed[1]>>8))*16];
  11466. // Test all distances
  11467. /* unroll loop for (uint8 d = 0; d < 8; d++) */
  11468. {
  11469. ONETABLE59R(0)
  11470. ONETABLE59R(1)
  11471. ONETABLE59R(2)
  11472. ONETABLE59R(3)
  11473. ONETABLE59R(4)
  11474. ONETABLE59R(5)
  11475. ONETABLE59R(6)
  11476. ONETABLE59R(7)
  11477. }
  11478. return best_block_error;
  11479. }
  11480. #endif
  11481. #if EXHAUSTIVE_CODE_ACTIVE
  11482. // Calculate a minimal error for the T-mode when compressing exhaustively.
  11483. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11484. 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)
  11485. {
  11486. unsigned int block_error = 0,
  11487. best_block_error = MAXERR1000;
  11488. unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
  11489. unsigned int *pixel_error_col0_base_adr;
  11490. #define FIRSTCHOICE59RG_PERCEP \
  11491. if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
  11492. block_error = *pixel_error_col0_adr;\
  11493. else\
  11494. block_error = *pixel_error_col1_adr;\
  11495. #define CHOICE59RG_PERCEP(xval) \
  11496. if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
  11497. block_error += pixel_error_col0_adr[xval];\
  11498. else\
  11499. block_error += pixel_error_col1_adr[xval];\
  11500. #define ONETABLE59RG_PERCEP(dval)\
  11501. pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
  11502. /* unroll loop for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
  11503. {\
  11504. FIRSTCHOICE59RG_PERCEP\
  11505. if( block_error < best_error_so_far)\
  11506. {\
  11507. CHOICE59RG_PERCEP(1)\
  11508. if( block_error < best_error_so_far)\
  11509. {\
  11510. CHOICE59RG_PERCEP(2)\
  11511. CHOICE59RG_PERCEP(3)\
  11512. if( block_error < best_error_so_far)\
  11513. {\
  11514. CHOICE59RG_PERCEP(4)\
  11515. CHOICE59RG_PERCEP(5)\
  11516. if( block_error < best_error_so_far)\
  11517. {\
  11518. CHOICE59RG_PERCEP(6)\
  11519. CHOICE59RG_PERCEP(7)\
  11520. if( block_error < best_error_so_far)\
  11521. {\
  11522. CHOICE59RG_PERCEP(8)\
  11523. CHOICE59RG_PERCEP(9)\
  11524. if( block_error < best_error_so_far)\
  11525. {\
  11526. CHOICE59RG_PERCEP(10)\
  11527. CHOICE59RG_PERCEP(11)\
  11528. if( block_error < best_error_so_far)\
  11529. {\
  11530. CHOICE59RG_PERCEP(12)\
  11531. CHOICE59RG_PERCEP(13)\
  11532. if( block_error < best_error_so_far)\
  11533. {\
  11534. CHOICE59RG_PERCEP(14)\
  11535. CHOICE59RG_PERCEP(15)\
  11536. }\
  11537. }\
  11538. }\
  11539. }\
  11540. }\
  11541. }\
  11542. }\
  11543. }\
  11544. }\
  11545. if (block_error < best_block_error)\
  11546. best_block_error = block_error;\
  11547. pixel_error_col0_base_adr = &precalc_err_col0_RG[((colorsRGB444_packed[0]>>4)*8)*16];
  11548. pixel_error_col1_adr = &precalc_err_col1_RG[((colorsRGB444_packed[1]>>4))*16];
  11549. // Test all distances
  11550. /* unroll loop for (uint8 d = 0; d < 8; d++) */
  11551. {
  11552. ONETABLE59RG_PERCEP(0)
  11553. ONETABLE59RG_PERCEP(1)
  11554. ONETABLE59RG_PERCEP(2)
  11555. ONETABLE59RG_PERCEP(3)
  11556. ONETABLE59RG_PERCEP(4)
  11557. ONETABLE59RG_PERCEP(5)
  11558. ONETABLE59RG_PERCEP(6)
  11559. ONETABLE59RG_PERCEP(7)
  11560. }
  11561. return best_block_error;
  11562. }
  11563. #endif
  11564. #if EXHAUSTIVE_CODE_ACTIVE
  11565. // Calculate a minimal error for the T-mode when compressing exhaustively.
  11566. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11567. 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)
  11568. {
  11569. unsigned int block_error = 0,
  11570. best_block_error = MAXIMUM_ERROR;
  11571. unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
  11572. unsigned int *pixel_error_col0_base_adr;
  11573. #define FIRSTCHOICE59RG \
  11574. if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
  11575. block_error = *pixel_error_col0_adr;\
  11576. else\
  11577. block_error = *pixel_error_col1_adr;\
  11578. #define CHOICE59RG(xval) \
  11579. if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
  11580. block_error += pixel_error_col0_adr[xval];\
  11581. else\
  11582. block_error += pixel_error_col1_adr[xval];\
  11583. #define ONETABLE59RG(dval)\
  11584. pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
  11585. /* unroll loop for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
  11586. {\
  11587. FIRSTCHOICE59RG\
  11588. if( block_error < best_error_so_far)\
  11589. {\
  11590. CHOICE59RG(1)\
  11591. if( block_error < best_error_so_far)\
  11592. {\
  11593. CHOICE59RG(2)\
  11594. CHOICE59RG(3)\
  11595. if( block_error < best_error_so_far)\
  11596. {\
  11597. CHOICE59RG(4)\
  11598. CHOICE59RG(5)\
  11599. if( block_error < best_error_so_far)\
  11600. {\
  11601. CHOICE59RG(6)\
  11602. CHOICE59RG(7)\
  11603. if( block_error < best_error_so_far)\
  11604. {\
  11605. CHOICE59RG(8)\
  11606. CHOICE59RG(9)\
  11607. if( block_error < best_error_so_far)\
  11608. {\
  11609. CHOICE59RG(10)\
  11610. CHOICE59RG(11)\
  11611. if( block_error < best_error_so_far)\
  11612. {\
  11613. CHOICE59RG(12)\
  11614. CHOICE59RG(13)\
  11615. if( block_error < best_error_so_far)\
  11616. {\
  11617. CHOICE59RG(14)\
  11618. CHOICE59RG(15)\
  11619. }\
  11620. }\
  11621. }\
  11622. }\
  11623. }\
  11624. }\
  11625. }\
  11626. }\
  11627. }\
  11628. if (block_error < best_block_error)\
  11629. best_block_error = block_error;\
  11630. pixel_error_col0_base_adr = &precalc_err_col0_RG[((colorsRGB444_packed[0]>>4)*8)*16];
  11631. pixel_error_col1_adr = &precalc_err_col1_RG[((colorsRGB444_packed[1]>>4))*16];
  11632. // Test all distances
  11633. /* unroll loop for (uint8 d = 0; d < 8; d++) */
  11634. {
  11635. ONETABLE59RG(0)
  11636. ONETABLE59RG(1)
  11637. ONETABLE59RG(2)
  11638. ONETABLE59RG(3)
  11639. ONETABLE59RG(4)
  11640. ONETABLE59RG(5)
  11641. ONETABLE59RG(6)
  11642. ONETABLE59RG(7)
  11643. }
  11644. return best_block_error;
  11645. }
  11646. #endif
  11647. #if EXHAUSTIVE_CODE_ACTIVE
  11648. // Calculate a minimal error for the T-mode when compressing exhaustively.
  11649. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11650. 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)
  11651. {
  11652. unsigned int block_error = 0,
  11653. best_block_error = MAXERR1000;
  11654. unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
  11655. unsigned int *pixel_error_col0_base_adr;
  11656. #define FIRSTCHOICE59_PERCEP \
  11657. if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
  11658. block_error = *pixel_error_col0_adr;\
  11659. else\
  11660. block_error = *pixel_error_col1_adr;\
  11661. #define CHOICE59_PERCEP(xval) \
  11662. if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
  11663. block_error += pixel_error_col0_adr[xval];\
  11664. else\
  11665. block_error += pixel_error_col1_adr[xval];\
  11666. #define ONETABLE59T_PERCEP(dval)\
  11667. pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
  11668. /* unroll for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
  11669. {\
  11670. FIRSTCHOICE59_PERCEP\
  11671. if( block_error < best_error_so_far)\
  11672. {\
  11673. CHOICE59_PERCEP(1)\
  11674. if( block_error < best_error_so_far)\
  11675. {\
  11676. CHOICE59_PERCEP(2)\
  11677. CHOICE59_PERCEP(3)\
  11678. if( block_error < best_error_so_far)\
  11679. {\
  11680. CHOICE59_PERCEP(4)\
  11681. CHOICE59_PERCEP(5)\
  11682. if( block_error < best_error_so_far)\
  11683. {\
  11684. CHOICE59_PERCEP(6)\
  11685. CHOICE59_PERCEP(7)\
  11686. if( block_error < best_error_so_far)\
  11687. {\
  11688. CHOICE59_PERCEP(8)\
  11689. CHOICE59_PERCEP(9)\
  11690. if( block_error < best_error_so_far)\
  11691. {\
  11692. CHOICE59_PERCEP(10)\
  11693. CHOICE59_PERCEP(11)\
  11694. if( block_error < best_error_so_far)\
  11695. {\
  11696. CHOICE59_PERCEP(12)\
  11697. CHOICE59_PERCEP(13)\
  11698. if( block_error < best_error_so_far)\
  11699. {\
  11700. CHOICE59_PERCEP(14)\
  11701. CHOICE59_PERCEP(15)\
  11702. }\
  11703. }\
  11704. }\
  11705. }\
  11706. }\
  11707. }\
  11708. }\
  11709. }\
  11710. }\
  11711. if (block_error < best_block_error)\
  11712. best_block_error = block_error;\
  11713. pixel_error_col1_adr = &precalc_err_col1_RGB[(colorsRGB444_packed[1])*16];
  11714. pixel_error_col0_base_adr = &precalc_err_col0_RGB[(colorsRGB444_packed[0]*8)*16];
  11715. // Test all distances
  11716. /* unroll loop for (uint8 d = 0; d < 8; d++)*/
  11717. {
  11718. ONETABLE59T_PERCEP(0)
  11719. ONETABLE59T_PERCEP(1)
  11720. ONETABLE59T_PERCEP(2)
  11721. ONETABLE59T_PERCEP(3)
  11722. ONETABLE59T_PERCEP(4)
  11723. ONETABLE59T_PERCEP(5)
  11724. ONETABLE59T_PERCEP(6)
  11725. ONETABLE59T_PERCEP(7)
  11726. }
  11727. return best_block_error;
  11728. }
  11729. #endif
  11730. #if EXHAUSTIVE_CODE_ACTIVE
  11731. // Calculate a minimal error for the T-mode when compressing exhaustively.
  11732. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11733. 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)
  11734. {
  11735. unsigned int block_error = 0,
  11736. best_block_error = MAXIMUM_ERROR;
  11737. unsigned int *pixel_error_col0_adr, *pixel_error_col1_adr;
  11738. unsigned int *pixel_error_col0_base_adr;
  11739. #define FIRSTCHOICE59 \
  11740. if(*pixel_error_col0_adr < *pixel_error_col1_adr)\
  11741. block_error = *pixel_error_col0_adr;\
  11742. else\
  11743. block_error = *pixel_error_col1_adr;\
  11744. #define CHOICE59(xval) \
  11745. if(pixel_error_col0_adr[xval] < pixel_error_col1_adr[xval])\
  11746. block_error += pixel_error_col0_adr[xval];\
  11747. else\
  11748. block_error += pixel_error_col1_adr[xval];\
  11749. #define ONETABLE59T(dval)\
  11750. pixel_error_col0_adr = &pixel_error_col0_base_adr[dval*16];\
  11751. /* unroll for(int x = 0; block_error < best_error_so_far && x<16; x++) */\
  11752. {\
  11753. FIRSTCHOICE59\
  11754. if( block_error < best_error_so_far)\
  11755. {\
  11756. CHOICE59(1)\
  11757. if( block_error < best_error_so_far)\
  11758. {\
  11759. CHOICE59(2)\
  11760. CHOICE59(3)\
  11761. if( block_error < best_error_so_far)\
  11762. {\
  11763. CHOICE59(4)\
  11764. CHOICE59(5)\
  11765. if( block_error < best_error_so_far)\
  11766. {\
  11767. CHOICE59(6)\
  11768. CHOICE59(7)\
  11769. if( block_error < best_error_so_far)\
  11770. {\
  11771. CHOICE59(8)\
  11772. CHOICE59(9)\
  11773. if( block_error < best_error_so_far)\
  11774. {\
  11775. CHOICE59(10)\
  11776. CHOICE59(11)\
  11777. if( block_error < best_error_so_far)\
  11778. {\
  11779. CHOICE59(12)\
  11780. CHOICE59(13)\
  11781. if( block_error < best_error_so_far)\
  11782. {\
  11783. CHOICE59(14)\
  11784. CHOICE59(15)\
  11785. }\
  11786. }\
  11787. }\
  11788. }\
  11789. }\
  11790. }\
  11791. }\
  11792. }\
  11793. }\
  11794. if (block_error < best_block_error)\
  11795. best_block_error = block_error;\
  11796. pixel_error_col1_adr = &precalc_err_col1_RGB[(colorsRGB444_packed[1])*16];
  11797. pixel_error_col0_base_adr = &precalc_err_col0_RGB[(colorsRGB444_packed[0]*8)*16];
  11798. // Test all distances
  11799. /* unroll loop for (uint8 d = 0; d < 8; d++)*/
  11800. {
  11801. ONETABLE59T(0)
  11802. ONETABLE59T(1)
  11803. ONETABLE59T(2)
  11804. ONETABLE59T(3)
  11805. ONETABLE59T(4)
  11806. ONETABLE59T(5)
  11807. ONETABLE59T(6)
  11808. ONETABLE59T(7)
  11809. }
  11810. return best_block_error;
  11811. }
  11812. #endif
  11813. #if EXHAUSTIVE_CODE_ACTIVE
  11814. // The below code should compress the block to 59 bits.
  11815. // This is supposed to match the first of the three modes in TWOTIMER.
  11816. //
  11817. //|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|
  11818. //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  11819. //
  11820. //|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|
  11821. //|----------------------------------------index bits---------------------------------------------|
  11822. //
  11823. // 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
  11824. // is less than best_error_so_far. To guarantee that the best possible T-representation is found, the function should be called using
  11825. // best_error_so_far = 255*255*3*16, which is the maximum error for a block.
  11826. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11827. 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)
  11828. {
  11829. uint8 colorsRGB444[2][3];
  11830. unsigned int pixel_indices;
  11831. uint8 distance;
  11832. uint8 block[4*4*4];
  11833. unsigned int *precalc_err_col0_RGB;
  11834. unsigned int *precalc_err_col1_RGB;
  11835. unsigned int *precalc_err_col0_RG;
  11836. unsigned int *precalc_err_col1_RG;
  11837. unsigned int *precalc_err_col0_R;
  11838. unsigned int *precalc_err_col1_R;
  11839. int colorRGB444_packed;
  11840. int colorsRGB444_packed[2];
  11841. int best_colorsRGB444_packed[2];
  11842. unsigned int best_error_using_Tmode;
  11843. // First compress block quickly to a resonable quality so that we can
  11844. // rule out all blocks that are of worse quality than that.
  11845. best_error_using_Tmode = (unsigned int) compressBlockTHUMB59TFastestOnlyColorPerceptual1000(img, width, height, startx, starty, best_colorsRGB444_packed);
  11846. if(best_error_using_Tmode < best_error_so_far)
  11847. best_error_so_far = best_error_using_Tmode;
  11848. // Color numbering is reversed between the above function and the precalc functions below; swap colors.
  11849. int temp = best_colorsRGB444_packed[0];
  11850. best_colorsRGB444_packed[0] = best_colorsRGB444_packed[1];
  11851. best_colorsRGB444_packed[1] = temp;
  11852. int xx,yy,count = 0;
  11853. // Use 4 bytes per pixel to make it 32-word aligned.
  11854. for(xx = 0; xx<4; xx++)
  11855. {
  11856. for(yy=0; yy<4; yy++)
  11857. {
  11858. block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  11859. block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  11860. block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  11861. block[(count)*4+3] = 0;
  11862. count++;
  11863. }
  11864. }
  11865. // Precalculate error for color 0 (which produces the upper half of the T)
  11866. precalc_err_col0_RGB = (unsigned int*) malloc(4096*8*16*sizeof(unsigned int));
  11867. if(!precalc_err_col0_RGB){printf("Out of memory allocating \n");exit(1);}
  11868. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
  11869. {
  11870. precalcError59T_col0_RGBpercep1000(block, colorRGB444_packed, precalc_err_col0_RGB);
  11871. }
  11872. // Precalculate error for color 1 (which produces the lower half of the T -- the lone color)
  11873. precalc_err_col1_RGB = (unsigned int*) malloc(4096*16*sizeof(unsigned int));
  11874. if(!precalc_err_col1_RGB){printf("Out of memory allocating \n");exit(1);}
  11875. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
  11876. {
  11877. precalcError59T_col1_RGBpercep1000(block, colorRGB444_packed, precalc_err_col1_RGB);
  11878. }
  11879. precalc_err_col0_RG = (unsigned int*) malloc(16*16*8*16*sizeof(unsigned int));
  11880. if(!precalc_err_col0_RG){printf("Out of memory allocating \n");exit(1);}
  11881. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
  11882. {
  11883. precalcError59T_col0_RGpercep1000(block, colorRGB444_packed, precalc_err_col0_RG);
  11884. }
  11885. precalc_err_col1_RG = (unsigned int*) malloc(16*16*16*sizeof(unsigned int));
  11886. if(!precalc_err_col1_RG){printf("Out of memory allocating \n");exit(1);}
  11887. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
  11888. {
  11889. precalcError59T_col1_RGpercep1000(block, colorRGB444_packed, precalc_err_col1_RG);
  11890. }
  11891. precalc_err_col0_R = (unsigned int*) malloc(16*8*16*sizeof(unsigned int));
  11892. if(!precalc_err_col0_R){printf("Out of memory allocating \n");exit(1);}
  11893. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
  11894. {
  11895. precalcError59T_col0_Rpercep1000(block, colorRGB444_packed, precalc_err_col0_R);
  11896. }
  11897. precalc_err_col1_R = (unsigned int*) malloc(16*16*sizeof(unsigned int));
  11898. if(!precalc_err_col1_R){printf("Out of memory allocating \n");exit(1);}
  11899. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
  11900. {
  11901. precalcError59T_col1_Rpercep1000(block, colorRGB444_packed, precalc_err_col1_R);
  11902. }
  11903. unsigned int error;
  11904. unsigned int avoided = 0;
  11905. unsigned int notavoided = 0;
  11906. for(colorsRGB444[0][0] = 0; colorsRGB444[0][0] < 16; colorsRGB444[0][0]++)
  11907. {
  11908. for(colorsRGB444[1][0] = 0; colorsRGB444[1][0] < 16; colorsRGB444[1][0]++)
  11909. {
  11910. colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8);
  11911. colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8);
  11912. error = calculateError59TusingPrecalcRperceptual1000(block, colorsRGB444_packed, precalc_err_col0_R, precalc_err_col1_R, best_error_so_far);
  11913. if(error < best_error_so_far)
  11914. {
  11915. notavoided = notavoided + 1;
  11916. for(colorsRGB444[0][1] = 0; colorsRGB444[0][1] < 16; colorsRGB444[0][1]++)
  11917. {
  11918. colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8) + (colorsRGB444[0][1] <<4);
  11919. for(colorsRGB444[1][1] = 0; colorsRGB444[1][1] < 16; colorsRGB444[1][1]++)
  11920. {
  11921. colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8) + (colorsRGB444[1][1] <<4);
  11922. error = calculateError59TusingPrecalcRGperceptual1000(block, colorsRGB444_packed, precalc_err_col0_RG, precalc_err_col1_RG, best_error_so_far);
  11923. if(error < best_error_so_far)
  11924. {
  11925. for(colorsRGB444[0][2] = 0; colorsRGB444[0][2] < 16; colorsRGB444[0][2]++)
  11926. {
  11927. colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8) + (colorsRGB444[0][1] <<4) + colorsRGB444[0][2];
  11928. for(colorsRGB444[1][2] = 0; colorsRGB444[1][2] < 16; colorsRGB444[1][2]++)
  11929. {
  11930. colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8) + (colorsRGB444[1][1] <<4) + colorsRGB444[1][2];
  11931. error = calculateError59TusingPrecalcRGBperceptual1000(block, colorsRGB444_packed, precalc_err_col0_RGB, precalc_err_col1_RGB, best_error_so_far);
  11932. if(error < best_error_so_far)
  11933. {
  11934. best_error_so_far = error;
  11935. best_error_using_Tmode = error;
  11936. best_colorsRGB444_packed[0] = colorsRGB444_packed[0];
  11937. best_colorsRGB444_packed[1] = colorsRGB444_packed[1];
  11938. }
  11939. }
  11940. }
  11941. }
  11942. }
  11943. }
  11944. }
  11945. }
  11946. }
  11947. free(precalc_err_col0_RGB);
  11948. free(precalc_err_col1_RGB);
  11949. free(precalc_err_col0_RG);
  11950. free(precalc_err_col1_RG);
  11951. free(precalc_err_col0_R);
  11952. free(precalc_err_col1_R);
  11953. // We have got the two best colors. Now find the best distance and pixel indices.
  11954. // Color numbering are reversed between precalc and noSwap
  11955. colorsRGB444[0][0] = (best_colorsRGB444_packed[1] >> 8) & 0xf;
  11956. colorsRGB444[0][1] = (best_colorsRGB444_packed[1] >> 4) & 0xf;
  11957. colorsRGB444[0][2] = (best_colorsRGB444_packed[1] >> 0) & 0xf;
  11958. colorsRGB444[1][0] = (best_colorsRGB444_packed[0] >> 8) & 0xf;
  11959. colorsRGB444[1][1] = (best_colorsRGB444_packed[0] >> 4) & 0xf;
  11960. colorsRGB444[1][2] = (best_colorsRGB444_packed[0] >> 0) & 0xf;
  11961. calculateError59TnoSwapPerceptual1000(img, width, startx, starty, colorsRGB444, distance, pixel_indices);
  11962. // Put the compress params into the compression block
  11963. packBlock59T(colorsRGB444, distance, pixel_indices, compressed1, compressed2);
  11964. return best_error_using_Tmode;
  11965. }
  11966. #endif
  11967. #if EXHAUSTIVE_CODE_ACTIVE
  11968. // The below code should compress the block to 59 bits.
  11969. // This is supposed to match the first of the three modes in TWOTIMER.
  11970. //
  11971. //|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|
  11972. //|----empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|--dist--|
  11973. //
  11974. //|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|
  11975. //|----------------------------------------index bits---------------------------------------------|
  11976. //
  11977. // 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
  11978. // is less than best_error_so_far. To guarantee that the best possible T-representation is found, the function should be called using
  11979. // best_error_so_far = 255*255*3*16, which is the maximum error for a block.
  11980. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  11981. 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)
  11982. {
  11983. uint8 colorsRGB444[2][3];
  11984. unsigned int pixel_indices;
  11985. uint8 distance;
  11986. uint8 block[4*4*4];
  11987. unsigned int *precalc_err_col0_RGB;
  11988. unsigned int *precalc_err_col1_RGB;
  11989. unsigned int *precalc_err_col0_RG;
  11990. unsigned int *precalc_err_col1_RG;
  11991. unsigned int *precalc_err_col0_R;
  11992. unsigned int *precalc_err_col1_R;
  11993. int colorRGB444_packed;
  11994. int colorsRGB444_packed[2];
  11995. int best_colorsRGB444_packed[2];
  11996. unsigned int best_error_using_Tmode;
  11997. // First compress block quickly to a resonable quality so that we can
  11998. // rule out all blocks that are of worse quality than that.
  11999. best_error_using_Tmode = (unsigned int) compressBlockTHUMB59TFastestOnlyColor(img, width, height, startx, starty, best_colorsRGB444_packed);
  12000. if(best_error_using_Tmode < best_error_so_far)
  12001. best_error_so_far = best_error_using_Tmode;
  12002. // Colors numbering is reversed between the above function and the precalc below:
  12003. int temp = best_colorsRGB444_packed[0];
  12004. best_colorsRGB444_packed[0] = best_colorsRGB444_packed[1];
  12005. best_colorsRGB444_packed[1] = temp;
  12006. int xx,yy,count = 0;
  12007. // Use 4 bytes per pixel to make it 32-word aligned.
  12008. for(xx = 0; xx<4; xx++)
  12009. {
  12010. for(yy=0; yy<4; yy++)
  12011. {
  12012. block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  12013. block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  12014. block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  12015. block[(count)*4+3] = 0;
  12016. count++;
  12017. }
  12018. }
  12019. // Precalculate error for color 0 (which produces the upper half of the T)
  12020. precalc_err_col0_RGB = (unsigned int*) malloc(4096*8*16*sizeof(unsigned int));
  12021. if(!precalc_err_col0_RGB){printf("Out of memory allocating \n");exit(1);}
  12022. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
  12023. {
  12024. precalcError59T_col0_RGB(block, colorRGB444_packed, precalc_err_col0_RGB);
  12025. }
  12026. // Precalculate error for color 1 (which produces the lower half of the T -- the lone color)
  12027. precalc_err_col1_RGB = (unsigned int*) malloc(4096*16*sizeof(unsigned int));
  12028. if(!precalc_err_col1_RGB){printf("Out of memory allocating \n");exit(1);}
  12029. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
  12030. {
  12031. precalcError59T_col1_RGB(block, colorRGB444_packed, precalc_err_col1_RGB);
  12032. }
  12033. precalc_err_col0_RG = (unsigned int*) malloc(16*16*8*16*sizeof(unsigned int));
  12034. if(!precalc_err_col0_RG){printf("Out of memory allocating \n");exit(1);}
  12035. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
  12036. {
  12037. precalcError59T_col0_RG(block, colorRGB444_packed, precalc_err_col0_RG);
  12038. }
  12039. precalc_err_col1_RG = (unsigned int*) malloc(16*16*16*sizeof(unsigned int));
  12040. if(!precalc_err_col1_RG){printf("Out of memory allocating \n");exit(1);}
  12041. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
  12042. {
  12043. precalcError59T_col1_RG(block, colorRGB444_packed, precalc_err_col1_RG);
  12044. }
  12045. precalc_err_col0_R = (unsigned int*) malloc(16*8*16*sizeof(unsigned int));
  12046. if(!precalc_err_col0_R){printf("Out of memory allocating \n");exit(1);}
  12047. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
  12048. {
  12049. precalcError59T_col0_R(block, colorRGB444_packed, precalc_err_col0_R);
  12050. }
  12051. precalc_err_col1_R = (unsigned int*) malloc(16*16*sizeof(unsigned int));
  12052. if(!precalc_err_col1_R){printf("Out of memory allocating \n");exit(1);}
  12053. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
  12054. {
  12055. precalcError59T_col1_R(block, colorRGB444_packed, precalc_err_col1_R);
  12056. }
  12057. unsigned int error;
  12058. unsigned int avoided = 0;
  12059. unsigned int notavoided = 0;
  12060. for(colorsRGB444[0][0] = 0; colorsRGB444[0][0] < 16; colorsRGB444[0][0]++)
  12061. {
  12062. for(colorsRGB444[1][0] = 0; colorsRGB444[1][0] < 16; colorsRGB444[1][0]++)
  12063. {
  12064. colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8);
  12065. colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8);
  12066. error = calculateError59TusingPrecalcR(block, colorsRGB444_packed, precalc_err_col0_R, precalc_err_col1_R, best_error_so_far);
  12067. if(error < best_error_so_far)
  12068. {
  12069. notavoided = notavoided + 1;
  12070. for(colorsRGB444[0][1] = 0; colorsRGB444[0][1] < 16; colorsRGB444[0][1]++)
  12071. {
  12072. colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8) + (colorsRGB444[0][1] <<4);
  12073. for(colorsRGB444[1][1] = 0; colorsRGB444[1][1] < 16; colorsRGB444[1][1]++)
  12074. {
  12075. colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8) + (colorsRGB444[1][1] <<4);
  12076. error = calculateError59TusingPrecalcRG(block, colorsRGB444_packed, precalc_err_col0_RG, precalc_err_col1_RG, best_error_so_far);
  12077. if(error < best_error_so_far)
  12078. {
  12079. for(colorsRGB444[0][2] = 0; colorsRGB444[0][2] < 16; colorsRGB444[0][2]++)
  12080. {
  12081. colorsRGB444_packed[0] = (colorsRGB444[0][0] << 8) + (colorsRGB444[0][1] <<4) + colorsRGB444[0][2];
  12082. for(colorsRGB444[1][2] = 0; colorsRGB444[1][2] < 16; colorsRGB444[1][2]++)
  12083. {
  12084. colorsRGB444_packed[1] = (colorsRGB444[1][0] << 8) + (colorsRGB444[1][1] <<4) + colorsRGB444[1][2];
  12085. error = calculateError59TusingPrecalcRGB(block, colorsRGB444_packed, precalc_err_col0_RGB, precalc_err_col1_RGB, best_error_so_far);
  12086. if(error < best_error_so_far)
  12087. {
  12088. best_error_so_far = error;
  12089. best_error_using_Tmode = error;
  12090. best_colorsRGB444_packed[0] = colorsRGB444_packed[0];
  12091. best_colorsRGB444_packed[1] = colorsRGB444_packed[1];
  12092. }
  12093. }
  12094. }
  12095. }
  12096. }
  12097. }
  12098. }
  12099. }
  12100. }
  12101. free(precalc_err_col0_RGB);
  12102. free(precalc_err_col1_RGB);
  12103. free(precalc_err_col0_RG);
  12104. free(precalc_err_col1_RG);
  12105. free(precalc_err_col0_R);
  12106. free(precalc_err_col1_R);
  12107. // We have got the two best colors. Now find the best distance and pixel indices.
  12108. // Color numbering are reversed between precalc and noSwap
  12109. colorsRGB444[0][0] = (best_colorsRGB444_packed[1] >> 8) & 0xf;
  12110. colorsRGB444[0][1] = (best_colorsRGB444_packed[1] >> 4) & 0xf;
  12111. colorsRGB444[0][2] = (best_colorsRGB444_packed[1] >> 0) & 0xf;
  12112. colorsRGB444[1][0] = (best_colorsRGB444_packed[0] >> 8) & 0xf;
  12113. colorsRGB444[1][1] = (best_colorsRGB444_packed[0] >> 4) & 0xf;
  12114. colorsRGB444[1][2] = (best_colorsRGB444_packed[0] >> 0) & 0xf;
  12115. calculateError59TnoSwap(img, width, startx, starty, colorsRGB444, distance, pixel_indices);
  12116. // Put the compress params into the compression block
  12117. packBlock59T(colorsRGB444, distance, pixel_indices, compressed1, compressed2);
  12118. return best_error_using_Tmode;
  12119. }
  12120. #endif
  12121. #if EXHAUSTIVE_CODE_ACTIVE
  12122. // Precalculates tables used in the exhaustive compression of the H-mode.
  12123. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12124. void precalcErrorR_58Hperceptual1000(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_errR)
  12125. {
  12126. unsigned int block_error = 0,
  12127. best_block_error = MAXERR1000,
  12128. pixel_error,
  12129. best_pixel_error;
  12130. int diff[3];
  12131. unsigned int pixel_colors;
  12132. uint8 possible_colors[2][3];
  12133. uint8 colors[2][3];
  12134. decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
  12135. // Test all distances
  12136. for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
  12137. {
  12138. possible_colors[0][R] = CLAMP(0,colors[0][R] - table58H[d],255);
  12139. possible_colors[1][R] = CLAMP(0,colors[0][R] + table58H[d],255);
  12140. block_error = 0;
  12141. pixel_colors = 0;
  12142. // Loop block
  12143. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  12144. {
  12145. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  12146. {
  12147. best_pixel_error = MAXERR1000;
  12148. // Loop possible block colors
  12149. for (uint8 c = 0; c < 2; ++c)
  12150. {
  12151. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  12152. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]);
  12153. // Choose best error
  12154. if (pixel_error < best_pixel_error)
  12155. {
  12156. best_pixel_error = pixel_error;
  12157. }
  12158. }
  12159. precalc_errR[((colorRGB444_packed>>8)*8 + d)*16 + (y*4)+x] = (unsigned int) best_pixel_error;
  12160. }
  12161. }
  12162. }
  12163. }
  12164. #endif
  12165. #if EXHAUSTIVE_CODE_ACTIVE
  12166. // Precalculates tables used in the exhaustive compression of the H-mode.
  12167. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12168. void precalcErrorR_58H(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_errR)
  12169. {
  12170. double block_error = 0,
  12171. best_block_error = MAXIMUM_ERROR,
  12172. pixel_error,
  12173. best_pixel_error;
  12174. int diff[3];
  12175. unsigned int pixel_colors;
  12176. uint8 possible_colors[2][3];
  12177. uint8 colors[2][3];
  12178. decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
  12179. // Test all distances
  12180. for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
  12181. {
  12182. possible_colors[0][R] = CLAMP(0,colors[0][R] - table58H[d],255);
  12183. possible_colors[1][R] = CLAMP(0,colors[0][R] + table58H[d],255);
  12184. block_error = 0;
  12185. pixel_colors = 0;
  12186. // Loop block
  12187. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  12188. {
  12189. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  12190. {
  12191. best_pixel_error = MAXIMUM_ERROR;
  12192. // Loop possible block colors
  12193. for (uint8 c = 0; c < 2; ++c)
  12194. {
  12195. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  12196. pixel_error = weight[R]*SQUARE(diff[R]);
  12197. // Choose best error
  12198. if (pixel_error < best_pixel_error)
  12199. {
  12200. best_pixel_error = pixel_error;
  12201. }
  12202. }
  12203. precalc_errR[((colorRGB444_packed>>8)*8 + d)*16 + (y*4)+x] = (unsigned int) best_pixel_error;
  12204. }
  12205. }
  12206. }
  12207. }
  12208. #endif
  12209. #if EXHAUSTIVE_CODE_ACTIVE
  12210. // Precalculates tables used in the exhaustive compression of the H-mode.
  12211. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12212. void precalcErrorRG_58Hperceptual1000(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_errRG)
  12213. {
  12214. unsigned int block_error = 0,
  12215. best_block_error = MAXERR1000,
  12216. pixel_error,
  12217. best_pixel_error;
  12218. int diff[3];
  12219. unsigned int pixel_colors;
  12220. uint8 possible_colors[2][3];
  12221. uint8 colors[2][3];
  12222. decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
  12223. // Test all distances
  12224. for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
  12225. {
  12226. possible_colors[0][R] = CLAMP(0,colors[0][R] - table58H[d],255);
  12227. possible_colors[0][G] = CLAMP(0,colors[0][G] - table58H[d],255);
  12228. possible_colors[1][R] = CLAMP(0,colors[0][R] + table58H[d],255);
  12229. possible_colors[1][G] = CLAMP(0,colors[0][G] + table58H[d],255);
  12230. block_error = 0;
  12231. pixel_colors = 0;
  12232. // Loop block
  12233. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  12234. {
  12235. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  12236. {
  12237. best_pixel_error = MAXERR1000;
  12238. // Loop possible block colors
  12239. for (uint8 c = 0; c < 2; ++c)
  12240. {
  12241. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  12242. diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
  12243. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*SQUARE(diff[R]) +
  12244. PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*SQUARE(diff[G]);
  12245. // Choose best error
  12246. if (pixel_error < best_pixel_error)
  12247. {
  12248. best_pixel_error = pixel_error;
  12249. }
  12250. }
  12251. precalc_errRG[((colorRGB444_packed>>4)*8 + d)*16 + (y*4)+x] = (unsigned int) best_pixel_error;
  12252. }
  12253. }
  12254. }
  12255. }
  12256. #endif
  12257. #if EXHAUSTIVE_CODE_ACTIVE
  12258. // Precalculates tables used in the exhaustive compression of the H-mode.
  12259. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12260. void precalcErrorRG_58H(uint8* srcimg, int width, int startx, int starty, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_errRG)
  12261. {
  12262. double block_error = 0,
  12263. best_block_error = MAXIMUM_ERROR,
  12264. pixel_error,
  12265. best_pixel_error;
  12266. int diff[3];
  12267. unsigned int pixel_colors;
  12268. uint8 possible_colors[2][3];
  12269. uint8 colors[2][3];
  12270. decompressColor(R_BITS58H, G_BITS58H, B_BITS58H, colorsRGB444, colors);
  12271. // Test all distances
  12272. for (uint8 d = 0; d < BINPOW(TABLE_BITS_58H); ++d)
  12273. {
  12274. possible_colors[0][R] = CLAMP(0,colors[0][R] - table58H[d],255);
  12275. possible_colors[0][G] = CLAMP(0,colors[0][G] - table58H[d],255);
  12276. possible_colors[1][R] = CLAMP(0,colors[0][R] + table58H[d],255);
  12277. possible_colors[1][G] = CLAMP(0,colors[0][G] + table58H[d],255);
  12278. block_error = 0;
  12279. pixel_colors = 0;
  12280. // Loop block
  12281. for (size_t y = 0; y < BLOCKHEIGHT; ++y)
  12282. {
  12283. for (size_t x = 0; x < BLOCKWIDTH; ++x)
  12284. {
  12285. best_pixel_error = MAXIMUM_ERROR;
  12286. // Loop possible block colors
  12287. for (uint8 c = 0; c < 2; ++c)
  12288. {
  12289. diff[R] = srcimg[3*((starty+y)*width+startx+x)+R] - CLAMP(0,possible_colors[c][R],255);
  12290. diff[G] = srcimg[3*((starty+y)*width+startx+x)+G] - CLAMP(0,possible_colors[c][G],255);
  12291. pixel_error = weight[R]*SQUARE(diff[R]) +
  12292. weight[G]*SQUARE(diff[G]);
  12293. // Choose best error
  12294. if (pixel_error < best_pixel_error)
  12295. {
  12296. best_pixel_error = pixel_error;
  12297. }
  12298. }
  12299. precalc_errRG[((colorRGB444_packed>>4)*8 + d)*16 + (y*4)+x] = (unsigned int) best_pixel_error;
  12300. }
  12301. }
  12302. }
  12303. }
  12304. #endif
  12305. #if EXHAUSTIVE_CODE_ACTIVE
  12306. // Precalculates a table used in the exhaustive compression of the H-mode.
  12307. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12308. void precalcError58Hperceptual1000(uint8* block, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_err)
  12309. {
  12310. unsigned int pixel_error,
  12311. best_pixel_error;
  12312. int possible_colors[2][3];
  12313. uint8 colors[2][3];
  12314. unsigned int *precalc_err_tab;
  12315. int red_original;
  12316. int green_original;
  12317. int blue_original;
  12318. #define PRECALC_ONE_58H_PERCEP(qvalue)\
  12319. red_original = block[qvalue*4];\
  12320. green_original = block[qvalue*4+1];\
  12321. blue_original = block[qvalue*4+2];\
  12322. /* unroll loop for (color = 0; color< 2; color++) */\
  12323. best_pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*square_table[(possible_colors[0][R] - red_original)] \
  12324. + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*square_table[(possible_colors[0][G] - green_original)]\
  12325. + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[(possible_colors[0][B] - blue_original)];\
  12326. pixel_error = PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000*square_table[(possible_colors[1][R] - red_original)]\
  12327. + PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000*square_table[(possible_colors[1][G] - green_original)]\
  12328. + PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000*square_table[(possible_colors[1][B] - blue_original)];\
  12329. if (pixel_error < best_pixel_error)\
  12330. best_pixel_error = pixel_error;\
  12331. /* end unroll loop */\
  12332. precalc_err_tab[qvalue] = best_pixel_error;\
  12333. #define PRECALC_ONE_TABLE_58H_PERCEP(dvalue)\
  12334. precalc_err_tab = &precalc_err[((colorRGB444_packed*8)+dvalue)*16];\
  12335. possible_colors[0][R] = CLAMP_LEFT_ZERO(colors[0][R] - table58H[dvalue])+255;\
  12336. possible_colors[0][G] = CLAMP_LEFT_ZERO(colors[0][G] - table58H[dvalue])+255;\
  12337. possible_colors[0][B] = CLAMP_LEFT_ZERO(colors[0][B] - table58H[dvalue])+255;\
  12338. possible_colors[1][R] = CLAMP_RIGHT_255(colors[0][R] + table58H[dvalue])+255;\
  12339. possible_colors[1][G] = CLAMP_RIGHT_255(colors[0][G] + table58H[dvalue])+255;\
  12340. possible_colors[1][B] = CLAMP_RIGHT_255(colors[0][B] + table58H[dvalue])+255;\
  12341. /* unrolled loop for(q = 0; q<16; q++)*/\
  12342. PRECALC_ONE_58H_PERCEP(0)\
  12343. PRECALC_ONE_58H_PERCEP(1)\
  12344. PRECALC_ONE_58H_PERCEP(2)\
  12345. PRECALC_ONE_58H_PERCEP(3)\
  12346. PRECALC_ONE_58H_PERCEP(4)\
  12347. PRECALC_ONE_58H_PERCEP(5)\
  12348. PRECALC_ONE_58H_PERCEP(6)\
  12349. PRECALC_ONE_58H_PERCEP(7)\
  12350. PRECALC_ONE_58H_PERCEP(8)\
  12351. PRECALC_ONE_58H_PERCEP(9)\
  12352. PRECALC_ONE_58H_PERCEP(10)\
  12353. PRECALC_ONE_58H_PERCEP(11)\
  12354. PRECALC_ONE_58H_PERCEP(12)\
  12355. PRECALC_ONE_58H_PERCEP(13)\
  12356. PRECALC_ONE_58H_PERCEP(14)\
  12357. PRECALC_ONE_58H_PERCEP(15)\
  12358. /* end unroll loop */\
  12359. colors[0][R] = (colorsRGB444[0][R] << 4) | colorsRGB444[0][R];
  12360. colors[0][G] = (colorsRGB444[0][G] << 4) | colorsRGB444[0][G];
  12361. colors[0][B] = (colorsRGB444[0][B] << 4) | colorsRGB444[0][B];
  12362. // Test all distances
  12363. /* unroll loop for (uint8 d = 0; d < 8; ++d) */
  12364. PRECALC_ONE_TABLE_58H_PERCEP(0)
  12365. PRECALC_ONE_TABLE_58H_PERCEP(1)
  12366. PRECALC_ONE_TABLE_58H_PERCEP(2)
  12367. PRECALC_ONE_TABLE_58H_PERCEP(3)
  12368. PRECALC_ONE_TABLE_58H_PERCEP(4)
  12369. PRECALC_ONE_TABLE_58H_PERCEP(5)
  12370. PRECALC_ONE_TABLE_58H_PERCEP(6)
  12371. PRECALC_ONE_TABLE_58H_PERCEP(7)
  12372. /* end unroll loop */
  12373. }
  12374. #endif
  12375. #if EXHAUSTIVE_CODE_ACTIVE
  12376. // Precalculates a table used in the exhaustive compression of the H-mode.
  12377. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12378. void precalcError58H(uint8* block, uint8 (colorsRGB444)[2][3],int colorRGB444_packed, unsigned int *precalc_err)
  12379. {
  12380. unsigned int pixel_error,
  12381. best_pixel_error;
  12382. int possible_colors[2][3];
  12383. uint8 colors[2][3];
  12384. unsigned int *precalc_err_tab;
  12385. int red_original;
  12386. int green_original;
  12387. int blue_original;
  12388. #define PRECALC_ONE_58H(qvalue)\
  12389. red_original = block[qvalue*4];\
  12390. green_original = block[qvalue*4+1];\
  12391. blue_original = block[qvalue*4+2];\
  12392. /* unroll loop for (color = 0; color< 2; color++) */\
  12393. 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)];\
  12394. 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)];\
  12395. if (pixel_error < best_pixel_error)\
  12396. best_pixel_error = pixel_error;\
  12397. /* end unroll loop */\
  12398. precalc_err_tab[qvalue] = best_pixel_error;\
  12399. #define PRECALC_ONE_TABLE_58H(dvalue)\
  12400. precalc_err_tab = &precalc_err[((colorRGB444_packed*8)+dvalue)*16];\
  12401. possible_colors[0][R] = CLAMP_LEFT_ZERO(colors[0][R] - table58H[dvalue])+255;\
  12402. possible_colors[0][G] = CLAMP_LEFT_ZERO(colors[0][G] - table58H[dvalue])+255;\
  12403. possible_colors[0][B] = CLAMP_LEFT_ZERO(colors[0][B] - table58H[dvalue])+255;\
  12404. possible_colors[1][R] = CLAMP_RIGHT_255(colors[0][R] + table58H[dvalue])+255;\
  12405. possible_colors[1][G] = CLAMP_RIGHT_255(colors[0][G] + table58H[dvalue])+255;\
  12406. possible_colors[1][B] = CLAMP_RIGHT_255(colors[0][B] + table58H[dvalue])+255;\
  12407. /* unrolled loop for(q = 0; q<16; q++)*/\
  12408. PRECALC_ONE_58H(0)\
  12409. PRECALC_ONE_58H(1)\
  12410. PRECALC_ONE_58H(2)\
  12411. PRECALC_ONE_58H(3)\
  12412. PRECALC_ONE_58H(4)\
  12413. PRECALC_ONE_58H(5)\
  12414. PRECALC_ONE_58H(6)\
  12415. PRECALC_ONE_58H(7)\
  12416. PRECALC_ONE_58H(8)\
  12417. PRECALC_ONE_58H(9)\
  12418. PRECALC_ONE_58H(10)\
  12419. PRECALC_ONE_58H(11)\
  12420. PRECALC_ONE_58H(12)\
  12421. PRECALC_ONE_58H(13)\
  12422. PRECALC_ONE_58H(14)\
  12423. PRECALC_ONE_58H(15)\
  12424. /* end unroll loop */\
  12425. colors[0][R] = (colorsRGB444[0][R] << 4) | colorsRGB444[0][R];
  12426. colors[0][G] = (colorsRGB444[0][G] << 4) | colorsRGB444[0][G];
  12427. colors[0][B] = (colorsRGB444[0][B] << 4) | colorsRGB444[0][B];
  12428. // Test all distances
  12429. /* unroll loop for (uint8 d = 0; d < 8; ++d) */
  12430. PRECALC_ONE_TABLE_58H(0)
  12431. PRECALC_ONE_TABLE_58H(1)
  12432. PRECALC_ONE_TABLE_58H(2)
  12433. PRECALC_ONE_TABLE_58H(3)
  12434. PRECALC_ONE_TABLE_58H(4)
  12435. PRECALC_ONE_TABLE_58H(5)
  12436. PRECALC_ONE_TABLE_58H(6)
  12437. PRECALC_ONE_TABLE_58H(7)
  12438. /* end unroll loop */
  12439. }
  12440. #endif
  12441. #if EXHAUSTIVE_CODE_ACTIVE
  12442. // Calculate a minimum error for the H-mode when doing exhaustive compression.
  12443. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12444. unsigned int calculateErrorFromPrecalcR58Hperceptual1000(int *colorsRGB444_packed, unsigned int *precalc_errR, unsigned int best_err_so_far)
  12445. {
  12446. unsigned int block_error = 0;
  12447. unsigned int best_block_error = MAXERR1000;
  12448. unsigned int *precalc_col1, *precalc_col2;
  12449. unsigned int *precalc_col1tab, *precalc_col2tab;
  12450. precalc_col1 = &precalc_errR[(colorsRGB444_packed[0]>>8)*8*16];
  12451. precalc_col2 = &precalc_errR[(colorsRGB444_packed[1]>>8)*8*16];
  12452. #define CHOICE_R58H_PERCEP(value)\
  12453. if(precalc_col1tab[value] < precalc_col2tab[value])\
  12454. block_error += precalc_col1tab[value];\
  12455. else\
  12456. block_error += precalc_col2tab[value];\
  12457. // Test all distances
  12458. for (uint8 d = 0; d < 8; ++d)
  12459. {
  12460. block_error = 0;
  12461. precalc_col1tab = &precalc_col1[d*16];
  12462. precalc_col2tab = &precalc_col2[d*16];
  12463. // Loop block
  12464. /* unroll loop for(q = 0; q<16 && block_error < best_err_so_far; q++) */
  12465. CHOICE_R58H_PERCEP(0)
  12466. if( block_error < best_err_so_far )
  12467. {
  12468. CHOICE_R58H_PERCEP(1)
  12469. if( block_error < best_err_so_far )
  12470. {
  12471. CHOICE_R58H_PERCEP(2)
  12472. if( block_error < best_err_so_far )
  12473. {
  12474. CHOICE_R58H_PERCEP(3)
  12475. if( block_error < best_err_so_far )
  12476. {
  12477. CHOICE_R58H_PERCEP(4)
  12478. if( block_error < best_err_so_far )
  12479. {
  12480. CHOICE_R58H_PERCEP(5)
  12481. if( block_error < best_err_so_far )
  12482. {
  12483. CHOICE_R58H_PERCEP(6)
  12484. if( block_error < best_err_so_far )
  12485. {
  12486. CHOICE_R58H_PERCEP(7)
  12487. if( block_error < best_err_so_far )
  12488. {
  12489. CHOICE_R58H_PERCEP(8)
  12490. if( block_error < best_err_so_far )
  12491. {
  12492. CHOICE_R58H_PERCEP(9)
  12493. if( block_error < best_err_so_far )
  12494. {
  12495. CHOICE_R58H_PERCEP(10)
  12496. if( block_error < best_err_so_far )
  12497. {
  12498. CHOICE_R58H_PERCEP(11)
  12499. if( block_error < best_err_so_far )
  12500. {
  12501. CHOICE_R58H_PERCEP(12)
  12502. if( block_error < best_err_so_far )
  12503. {
  12504. CHOICE_R58H_PERCEP(13)
  12505. if( block_error < best_err_so_far )
  12506. {
  12507. CHOICE_R58H_PERCEP(14)
  12508. if( block_error < best_err_so_far )
  12509. {
  12510. CHOICE_R58H_PERCEP(15)
  12511. }
  12512. }
  12513. }
  12514. }
  12515. }
  12516. }
  12517. }
  12518. }
  12519. }
  12520. }
  12521. }
  12522. }
  12523. }
  12524. }
  12525. }
  12526. /* end unroll loop */
  12527. if (block_error < best_block_error)
  12528. best_block_error = block_error;
  12529. }
  12530. return best_block_error;
  12531. }
  12532. #endif
  12533. #if EXHAUSTIVE_CODE_ACTIVE
  12534. // Calculate a minimum error for the H-mode when doing exhaustive compression.
  12535. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12536. unsigned int calculateErrorFromPrecalcR58H(int *colorsRGB444_packed, unsigned int *precalc_errR, unsigned int best_err_so_far)
  12537. {
  12538. unsigned int block_error = 0;
  12539. unsigned int best_block_error = MAXIMUM_ERROR;
  12540. unsigned int *precalc_col1, *precalc_col2;
  12541. unsigned int *precalc_col1tab, *precalc_col2tab;
  12542. precalc_col1 = &precalc_errR[(colorsRGB444_packed[0]>>8)*8*16];
  12543. precalc_col2 = &precalc_errR[(colorsRGB444_packed[1]>>8)*8*16];
  12544. #define CHOICE_R58H(value)\
  12545. if(precalc_col1tab[value] < precalc_col2tab[value])\
  12546. block_error += precalc_col1tab[value];\
  12547. else\
  12548. block_error += precalc_col2tab[value];\
  12549. // Test all distances
  12550. for (uint8 d = 0; d < 8; ++d)
  12551. {
  12552. block_error = 0;
  12553. precalc_col1tab = &precalc_col1[d*16];
  12554. precalc_col2tab = &precalc_col2[d*16];
  12555. // Loop block
  12556. /* unroll loop for(q = 0; q<16 && block_error < best_err_so_far; q++) */
  12557. CHOICE_R58H(0)
  12558. if( block_error < best_err_so_far )
  12559. {
  12560. CHOICE_R58H(1)
  12561. if( block_error < best_err_so_far )
  12562. {
  12563. CHOICE_R58H(2)
  12564. if( block_error < best_err_so_far )
  12565. {
  12566. CHOICE_R58H(3)
  12567. if( block_error < best_err_so_far )
  12568. {
  12569. CHOICE_R58H(4)
  12570. if( block_error < best_err_so_far )
  12571. {
  12572. CHOICE_R58H(5)
  12573. if( block_error < best_err_so_far )
  12574. {
  12575. CHOICE_R58H(6)
  12576. if( block_error < best_err_so_far )
  12577. {
  12578. CHOICE_R58H(7)
  12579. if( block_error < best_err_so_far )
  12580. {
  12581. CHOICE_R58H(8)
  12582. if( block_error < best_err_so_far )
  12583. {
  12584. CHOICE_R58H(9)
  12585. if( block_error < best_err_so_far )
  12586. {
  12587. CHOICE_R58H(10)
  12588. if( block_error < best_err_so_far )
  12589. {
  12590. CHOICE_R58H(11)
  12591. if( block_error < best_err_so_far )
  12592. {
  12593. CHOICE_R58H(12)
  12594. if( block_error < best_err_so_far )
  12595. {
  12596. CHOICE_R58H(13)
  12597. if( block_error < best_err_so_far )
  12598. {
  12599. CHOICE_R58H(14)
  12600. if( block_error < best_err_so_far )
  12601. {
  12602. CHOICE_R58H(15)
  12603. }
  12604. }
  12605. }
  12606. }
  12607. }
  12608. }
  12609. }
  12610. }
  12611. }
  12612. }
  12613. }
  12614. }
  12615. }
  12616. }
  12617. }
  12618. /* end unroll loop */
  12619. if (block_error < best_block_error)
  12620. best_block_error = block_error;
  12621. }
  12622. return best_block_error;
  12623. }
  12624. #endif
  12625. #if EXHAUSTIVE_CODE_ACTIVE
  12626. // Calculate a minimum error for the H-mode when doing exhaustive compression.
  12627. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12628. unsigned int calculateErrorFromPrecalcRG58Hperceptual1000(int *colorsRGB444_packed, unsigned int *precalc_errRG, unsigned int best_err_so_far)
  12629. {
  12630. unsigned int block_error = 0;
  12631. unsigned int best_block_error = MAXIMUM_ERROR;
  12632. unsigned int *precalc_col1, *precalc_col2;
  12633. unsigned int *precalc_col1tab, *precalc_col2tab;
  12634. precalc_col1 = &precalc_errRG[(colorsRGB444_packed[0]>>4)*8*16];
  12635. precalc_col2 = &precalc_errRG[(colorsRGB444_packed[1]>>4)*8*16];
  12636. #define CHOICE_RG58H_PERCEP(value)\
  12637. if(precalc_col1tab[value] < precalc_col2tab[value])\
  12638. block_error += precalc_col1tab[value];\
  12639. else\
  12640. block_error += precalc_col2tab[value];\
  12641. // Test all distances
  12642. for (uint8 d = 0; d < 8; ++d)
  12643. {
  12644. block_error = 0;
  12645. precalc_col1tab = &precalc_col1[d*16];
  12646. precalc_col2tab = &precalc_col2[d*16];
  12647. // Loop block
  12648. /* unroll loop for(q = 0; q<16 && block_error < best_err_so_far; q++) */
  12649. CHOICE_RG58H_PERCEP(0)
  12650. if( block_error < best_err_so_far )
  12651. {
  12652. CHOICE_RG58H_PERCEP(1)
  12653. if( block_error < best_err_so_far )
  12654. {
  12655. CHOICE_RG58H_PERCEP(2)
  12656. if( block_error < best_err_so_far )
  12657. {
  12658. CHOICE_RG58H_PERCEP(3)
  12659. if( block_error < best_err_so_far )
  12660. {
  12661. CHOICE_RG58H_PERCEP(4)
  12662. if( block_error < best_err_so_far )
  12663. {
  12664. CHOICE_RG58H_PERCEP(5)
  12665. if( block_error < best_err_so_far )
  12666. {
  12667. CHOICE_RG58H_PERCEP(6)
  12668. if( block_error < best_err_so_far )
  12669. {
  12670. CHOICE_RG58H_PERCEP(7)
  12671. if( block_error < best_err_so_far )
  12672. {
  12673. CHOICE_RG58H_PERCEP(8)
  12674. if( block_error < best_err_so_far )
  12675. {
  12676. CHOICE_RG58H_PERCEP(9)
  12677. if( block_error < best_err_so_far )
  12678. {
  12679. CHOICE_RG58H_PERCEP(10)
  12680. if( block_error < best_err_so_far )
  12681. {
  12682. CHOICE_RG58H_PERCEP(11)
  12683. if( block_error < best_err_so_far )
  12684. {
  12685. CHOICE_RG58H_PERCEP(12)
  12686. if( block_error < best_err_so_far )
  12687. {
  12688. CHOICE_RG58H_PERCEP(13)
  12689. if( block_error < best_err_so_far )
  12690. {
  12691. CHOICE_RG58H_PERCEP(14)
  12692. if( block_error < best_err_so_far )
  12693. {
  12694. CHOICE_RG58H_PERCEP(15)
  12695. }
  12696. }
  12697. }
  12698. }
  12699. }
  12700. }
  12701. }
  12702. }
  12703. }
  12704. }
  12705. }
  12706. }
  12707. }
  12708. }
  12709. }
  12710. /* end unroll loop */
  12711. if (block_error < best_block_error)
  12712. best_block_error = block_error;
  12713. }
  12714. return best_block_error;
  12715. }
  12716. #endif
  12717. #if EXHAUSTIVE_CODE_ACTIVE
  12718. // Calculate a minimum error for the H-mode when doing exhaustive compression.
  12719. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12720. unsigned int calculateErrorFromPrecalcRG58H(int *colorsRGB444_packed, unsigned int *precalc_errRG, unsigned int best_err_so_far)
  12721. {
  12722. unsigned int block_error = 0;
  12723. unsigned int best_block_error = MAXIMUM_ERROR;
  12724. unsigned int *precalc_col1, *precalc_col2;
  12725. unsigned int *precalc_col1tab, *precalc_col2tab;
  12726. precalc_col1 = &precalc_errRG[(colorsRGB444_packed[0]>>4)*8*16];
  12727. precalc_col2 = &precalc_errRG[(colorsRGB444_packed[1]>>4)*8*16];
  12728. #define CHOICE_RG58H(value)\
  12729. if(precalc_col1tab[value] < precalc_col2tab[value])\
  12730. block_error += precalc_col1tab[value];\
  12731. else\
  12732. block_error += precalc_col2tab[value];\
  12733. // Test all distances
  12734. for (uint8 d = 0; d < 8; ++d)
  12735. {
  12736. block_error = 0;
  12737. precalc_col1tab = &precalc_col1[d*16];
  12738. precalc_col2tab = &precalc_col2[d*16];
  12739. // Loop block
  12740. /* unroll loop for(q = 0; q<16 && block_error < best_err_so_far; q++) */
  12741. CHOICE_RG58H(0)
  12742. if( block_error < best_err_so_far )
  12743. {
  12744. CHOICE_RG58H(1)
  12745. if( block_error < best_err_so_far )
  12746. {
  12747. CHOICE_RG58H(2)
  12748. if( block_error < best_err_so_far )
  12749. {
  12750. CHOICE_RG58H(3)
  12751. if( block_error < best_err_so_far )
  12752. {
  12753. CHOICE_RG58H(4)
  12754. if( block_error < best_err_so_far )
  12755. {
  12756. CHOICE_RG58H(5)
  12757. if( block_error < best_err_so_far )
  12758. {
  12759. CHOICE_RG58H(6)
  12760. if( block_error < best_err_so_far )
  12761. {
  12762. CHOICE_RG58H(7)
  12763. if( block_error < best_err_so_far )
  12764. {
  12765. CHOICE_RG58H(8)
  12766. if( block_error < best_err_so_far )
  12767. {
  12768. CHOICE_RG58H(9)
  12769. if( block_error < best_err_so_far )
  12770. {
  12771. CHOICE_RG58H(10)
  12772. if( block_error < best_err_so_far )
  12773. {
  12774. CHOICE_RG58H(11)
  12775. if( block_error < best_err_so_far )
  12776. {
  12777. CHOICE_RG58H(12)
  12778. if( block_error < best_err_so_far )
  12779. {
  12780. CHOICE_RG58H(13)
  12781. if( block_error < best_err_so_far )
  12782. {
  12783. CHOICE_RG58H(14)
  12784. if( block_error < best_err_so_far )
  12785. {
  12786. CHOICE_RG58H(15)
  12787. }
  12788. }
  12789. }
  12790. }
  12791. }
  12792. }
  12793. }
  12794. }
  12795. }
  12796. }
  12797. }
  12798. }
  12799. }
  12800. }
  12801. }
  12802. /* end unroll loop */
  12803. if (block_error < best_block_error)
  12804. best_block_error = block_error;
  12805. }
  12806. return best_block_error;
  12807. }
  12808. #endif
  12809. #if EXHAUSTIVE_CODE_ACTIVE
  12810. // Calculate a minimum error for the H-mode when doing exhaustive compression.
  12811. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12812. unsigned int calculateErrorFromPrecalc58Hperceptual1000(int *colorsRGB444_packed, unsigned int *precalc_err, unsigned int total_best_err)
  12813. {
  12814. unsigned int block_error;\
  12815. unsigned int *precalc_col1, *precalc_col2;\
  12816. unsigned int *precalc_col1tab, *precalc_col2tab;\
  12817. unsigned int error;
  12818. #define FIRSTCHOICE_RGB58H_PERCEP(value)\
  12819. if(precalc_col1tab[value] < precalc_col2tab[value])\
  12820. block_error = precalc_col1tab[value];\
  12821. else\
  12822. block_error = precalc_col2tab[value];\
  12823. #define CHOICE_RGB58H_PERCEP(value)\
  12824. if(precalc_col1tab[value] < precalc_col2tab[value])\
  12825. block_error += precalc_col1tab[value];\
  12826. else\
  12827. block_error += precalc_col2tab[value];\
  12828. #define ONETABLE_RGB58H_PERCEP(distance)\
  12829. precalc_col1tab = &precalc_col1[distance*16];\
  12830. precalc_col2tab = &precalc_col2[distance*16];\
  12831. /* unroll loop for(q = 0; q<16 && block_error < total_best_err; q++) */\
  12832. FIRSTCHOICE_RGB58H_PERCEP(0)\
  12833. if( block_error < total_best_err)\
  12834. {\
  12835. CHOICE_RGB58H_PERCEP(1)\
  12836. if( block_error < total_best_err)\
  12837. {\
  12838. CHOICE_RGB58H_PERCEP(2)\
  12839. CHOICE_RGB58H_PERCEP(3)\
  12840. if( block_error < total_best_err)\
  12841. {\
  12842. CHOICE_RGB58H_PERCEP(4)\
  12843. CHOICE_RGB58H_PERCEP(5)\
  12844. if( block_error < total_best_err)\
  12845. {\
  12846. CHOICE_RGB58H_PERCEP(6)\
  12847. CHOICE_RGB58H_PERCEP(7)\
  12848. if( block_error < total_best_err)\
  12849. {\
  12850. CHOICE_RGB58H_PERCEP(8)\
  12851. CHOICE_RGB58H_PERCEP(9)\
  12852. if( block_error < total_best_err)\
  12853. {\
  12854. CHOICE_RGB58H_PERCEP(10)\
  12855. CHOICE_RGB58H_PERCEP(11)\
  12856. if( block_error < total_best_err)\
  12857. {\
  12858. CHOICE_RGB58H_PERCEP(12)\
  12859. CHOICE_RGB58H_PERCEP(13)\
  12860. if( block_error < total_best_err)\
  12861. {\
  12862. CHOICE_RGB58H_PERCEP(14)\
  12863. CHOICE_RGB58H_PERCEP(15)\
  12864. }\
  12865. }\
  12866. }\
  12867. }\
  12868. }\
  12869. }\
  12870. }\
  12871. }\
  12872. /* end unroll loop */\
  12873. if (block_error < error)\
  12874. error = block_error;\
  12875. #define CALCULATE_ERROR_FROM_PRECALC_RGB58H_PERCEP\
  12876. error = MAXERR1000;\
  12877. precalc_col1 = &precalc_err[colorsRGB444_packed[0]*8*16];\
  12878. precalc_col2 = &precalc_err[colorsRGB444_packed[1]*8*16];\
  12879. /* Test all distances*/\
  12880. /* unroll loop for (uint8 d = 0; d < 8; ++d) */\
  12881. ONETABLE_RGB58H_PERCEP(0)\
  12882. ONETABLE_RGB58H_PERCEP(1)\
  12883. ONETABLE_RGB58H_PERCEP(2)\
  12884. ONETABLE_RGB58H_PERCEP(3)\
  12885. ONETABLE_RGB58H_PERCEP(4)\
  12886. ONETABLE_RGB58H_PERCEP(5)\
  12887. ONETABLE_RGB58H_PERCEP(6)\
  12888. ONETABLE_RGB58H_PERCEP(7)\
  12889. /* end unroll loop */\
  12890. CALCULATE_ERROR_FROM_PRECALC_RGB58H_PERCEP
  12891. return error;\
  12892. }
  12893. #endif
  12894. #if EXHAUSTIVE_CODE_ACTIVE
  12895. // Calculate a minimum error for the H-mode when doing exhaustive compression.
  12896. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  12897. unsigned int calculateErrorFromPrecalc58H(int *colorsRGB444_packed, unsigned int *precalc_err, unsigned int total_best_err)
  12898. {
  12899. unsigned int block_error;\
  12900. unsigned int *precalc_col1, *precalc_col2;\
  12901. unsigned int *precalc_col1tab, *precalc_col2tab;\
  12902. unsigned int error;
  12903. #define FIRSTCHOICE_RGB58H(value)\
  12904. if(precalc_col1tab[value] < precalc_col2tab[value])\
  12905. block_error = precalc_col1tab[value];\
  12906. else\
  12907. block_error = precalc_col2tab[value];\
  12908. #define CHOICE_RGB58H(value)\
  12909. if(precalc_col1tab[value] < precalc_col2tab[value])\
  12910. block_error += precalc_col1tab[value];\
  12911. else\
  12912. block_error += precalc_col2tab[value];\
  12913. #define ONETABLE_RGB58H(distance)\
  12914. precalc_col1tab = &precalc_col1[distance*16];\
  12915. precalc_col2tab = &precalc_col2[distance*16];\
  12916. /* unroll loop for(q = 0; q<16 && block_error < total_best_err; q++) */\
  12917. FIRSTCHOICE_RGB58H(0)\
  12918. if( block_error < total_best_err)\
  12919. {\
  12920. CHOICE_RGB58H(1)\
  12921. if( block_error < total_best_err)\
  12922. {\
  12923. CHOICE_RGB58H(2)\
  12924. CHOICE_RGB58H(3)\
  12925. if( block_error < total_best_err)\
  12926. {\
  12927. CHOICE_RGB58H(4)\
  12928. CHOICE_RGB58H(5)\
  12929. if( block_error < total_best_err)\
  12930. {\
  12931. CHOICE_RGB58H(6)\
  12932. CHOICE_RGB58H(7)\
  12933. if( block_error < total_best_err)\
  12934. {\
  12935. CHOICE_RGB58H(8)\
  12936. CHOICE_RGB58H(9)\
  12937. if( block_error < total_best_err)\
  12938. {\
  12939. CHOICE_RGB58H(10)\
  12940. CHOICE_RGB58H(11)\
  12941. if( block_error < total_best_err)\
  12942. {\
  12943. CHOICE_RGB58H(12)\
  12944. CHOICE_RGB58H(13)\
  12945. if( block_error < total_best_err)\
  12946. {\
  12947. CHOICE_RGB58H(14)\
  12948. CHOICE_RGB58H(15)\
  12949. }\
  12950. }\
  12951. }\
  12952. }\
  12953. }\
  12954. }\
  12955. }\
  12956. }\
  12957. /* end unroll loop */\
  12958. if (block_error < error)\
  12959. error = block_error;\
  12960. #define CALCULATE_ERROR_FROM_PRECALC_RGB58H\
  12961. error = MAXIMUM_ERROR;\
  12962. precalc_col1 = &precalc_err[colorsRGB444_packed[0]*8*16];\
  12963. precalc_col2 = &precalc_err[colorsRGB444_packed[1]*8*16];\
  12964. /* Test all distances*/\
  12965. /* unroll loop for (uint8 d = 0; d < 8; ++d) */\
  12966. ONETABLE_RGB58H(0)\
  12967. ONETABLE_RGB58H(1)\
  12968. ONETABLE_RGB58H(2)\
  12969. ONETABLE_RGB58H(3)\
  12970. ONETABLE_RGB58H(4)\
  12971. ONETABLE_RGB58H(5)\
  12972. ONETABLE_RGB58H(6)\
  12973. ONETABLE_RGB58H(7)\
  12974. /* end unroll loop */\
  12975. CALCULATE_ERROR_FROM_PRECALC_RGB58H
  12976. return error;\
  12977. }
  12978. #endif
  12979. #if EXHAUSTIVE_CODE_ACTIVE
  12980. // The below code should compress the block to 58 bits.
  12981. // This is supposed to match the first of the three modes in TWOTIMER.
  12982. // The bit layout is thought to be:
  12983. //
  12984. //|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|
  12985. //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
  12986. //
  12987. //|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|
  12988. //|----------------------------------------index bits---------------------------------------------|
  12989. //
  12990. // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
  12991. // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
  12992. // Else, it is assumed to be 1.
  12993. // The below code should compress the block to 58 bits.
  12994. // This is supposed to match the first of the three modes in TWOTIMER.
  12995. // The bit layout is thought to be:
  12996. //
  12997. //|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|
  12998. //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
  12999. //
  13000. //|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|
  13001. //|----------------------------------------index bits---------------------------------------------|
  13002. //
  13003. // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
  13004. // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
  13005. // Else, it is assumed to be 1.
  13006. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  13007. 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)
  13008. {
  13009. unsigned int best_error_using_Hmode;
  13010. uint8 best_colorsRGB444[2][3];
  13011. unsigned int best_pixel_indices;
  13012. uint8 best_distance;
  13013. unsigned int error;
  13014. uint8 colorsRGB444[2][3];
  13015. int colorsRGB444_packed[2];
  13016. int best_colorsRGB444_packed[2];
  13017. int colorRGB444_packed;
  13018. unsigned int pixel_indices;
  13019. uint8 distance;
  13020. unsigned int *precalc_err; // smallest error per color, table and pixel
  13021. unsigned int *precalc_err_RG; // smallest pixel error for an entire table
  13022. unsigned int *precalc_err_R; // smallest pixel error for an entire table
  13023. uint8 block[4*4*4];
  13024. best_error_using_Hmode = MAXERR1000;
  13025. precalc_err = (unsigned int*) malloc(4096*8*16*sizeof(unsigned int));
  13026. if(!precalc_err){printf("Out of memory allocating \n");exit(1);}
  13027. precalc_err_RG = (unsigned int*) malloc(16*16*8*16*sizeof(unsigned int));
  13028. if(!precalc_err_RG){printf("Out of memory allocating \n");exit(1);}
  13029. precalc_err_R = (unsigned int*) malloc(16*8*16*sizeof(unsigned int));
  13030. if(!precalc_err_R){printf("Out of memory allocating \n");exit(1);}
  13031. unsigned int test1, test2;
  13032. best_error_using_Hmode = (unsigned int)compressBlockTHUMB58HFastestPerceptual1000(img,width, height, startx, starty, test1, test2);
  13033. best_colorsRGB444_packed[0] = 0;
  13034. best_colorsRGB444_packed[0] = GETBITSHIGH(test1, 12, 57);
  13035. best_colorsRGB444_packed[1] = 0;
  13036. best_colorsRGB444_packed[1] = GETBITSHIGH(test1, 12, 45);
  13037. if(best_error_using_Hmode < best_error_so_far)
  13038. best_error_so_far = best_error_using_Hmode;
  13039. int xx,yy,count = 0;
  13040. // Use 4 bytes per pixel to make it 32-word aligned.
  13041. for(xx = 0; xx<4; xx++)
  13042. {
  13043. for(yy=0; yy<4; yy++)
  13044. {
  13045. block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  13046. block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  13047. block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  13048. block[(count)*4+3] = 0;
  13049. count++;
  13050. }
  13051. }
  13052. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
  13053. {
  13054. colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
  13055. colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
  13056. colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
  13057. precalcError58Hperceptual1000(block, colorsRGB444, colorRGB444_packed, precalc_err);
  13058. }
  13059. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
  13060. {
  13061. colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
  13062. colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
  13063. colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
  13064. precalcErrorRG_58Hperceptual1000(img, width, startx, starty, colorsRGB444, colorRGB444_packed, precalc_err_RG);
  13065. }
  13066. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
  13067. {
  13068. colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
  13069. colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
  13070. colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
  13071. precalcErrorR_58Hperceptual1000(img, width, startx, starty, colorsRGB444, colorRGB444_packed, precalc_err_R);
  13072. }
  13073. int trycols = 0;
  13074. int allcols = 0;
  13075. for( colorsRGB444[0][0] = 0; colorsRGB444[0][0] <16; colorsRGB444[0][0]++)
  13076. {
  13077. colorsRGB444_packed[0] = colorsRGB444[0][0]*256;
  13078. for( colorsRGB444[1][0] = 0; colorsRGB444[1][0] <16; colorsRGB444[1][0]++)
  13079. {
  13080. colorsRGB444_packed[1] = colorsRGB444[1][0]*256;
  13081. if(colorsRGB444_packed[0] <= colorsRGB444_packed[1])
  13082. {
  13083. error = calculateErrorFromPrecalcR58Hperceptual1000(colorsRGB444_packed, precalc_err_R, best_error_so_far);
  13084. if(error < best_error_so_far)
  13085. {
  13086. for( colorsRGB444[0][1] = 0; colorsRGB444[0][1] <16; colorsRGB444[0][1]++)
  13087. {
  13088. colorsRGB444_packed[0] = colorsRGB444[0][0]*256 + colorsRGB444[0][1]*16;
  13089. for( colorsRGB444[1][1] = 0; colorsRGB444[1][1] <16; colorsRGB444[1][1]++)
  13090. {
  13091. colorsRGB444_packed[1] = colorsRGB444[1][0]*256 + colorsRGB444[1][1]*16;
  13092. if(colorsRGB444_packed[0] <= colorsRGB444_packed[1])
  13093. {
  13094. error = calculateErrorFromPrecalcRG58Hperceptual1000(colorsRGB444_packed, precalc_err_RG, best_error_so_far);
  13095. if(error < best_error_so_far)
  13096. {
  13097. for( colorsRGB444[0][2] = 0; colorsRGB444[0][2] <16; colorsRGB444[0][2]++)
  13098. {
  13099. colorsRGB444_packed[0] = colorsRGB444[0][0]*256 + colorsRGB444[0][1]*16 + colorsRGB444[0][2];
  13100. for( colorsRGB444[1][2] = 0; colorsRGB444[1][2] <16; colorsRGB444[1][2]++)
  13101. {
  13102. colorsRGB444_packed[1] = colorsRGB444[1][0]*256 + colorsRGB444[1][1]*16 + colorsRGB444[1][2];
  13103. if(colorsRGB444_packed[0] < colorsRGB444_packed[1])
  13104. {
  13105. error = calculateErrorFromPrecalc58Hperceptual1000(colorsRGB444_packed, precalc_err, best_error_so_far);
  13106. if(error < best_error_so_far)
  13107. {
  13108. best_error_so_far = error;
  13109. best_error_using_Hmode = error;
  13110. best_colorsRGB444_packed[0] = colorsRGB444_packed[0];
  13111. best_colorsRGB444_packed[1] = colorsRGB444_packed[1];
  13112. }
  13113. }
  13114. }
  13115. }
  13116. }
  13117. }
  13118. }
  13119. }
  13120. }
  13121. }
  13122. }
  13123. }
  13124. best_colorsRGB444[0][0] = (best_colorsRGB444_packed[0] >> 8) & 0xf;
  13125. best_colorsRGB444[0][1] = (best_colorsRGB444_packed[0] >> 4) & 0xf;
  13126. best_colorsRGB444[0][2] = (best_colorsRGB444_packed[0]) & 0xf;
  13127. best_colorsRGB444[1][0] = (best_colorsRGB444_packed[1] >> 8) & 0xf;
  13128. best_colorsRGB444[1][1] = (best_colorsRGB444_packed[1] >> 4) & 0xf;
  13129. best_colorsRGB444[1][2] = (best_colorsRGB444_packed[1]) & 0xf;
  13130. free(precalc_err);
  13131. free(precalc_err_RG);
  13132. free(precalc_err_R);
  13133. error = (unsigned int) calculateErrorAndCompress58Hperceptual1000(img, width, startx, starty, best_colorsRGB444, distance, pixel_indices);
  13134. best_distance = distance;
  13135. best_pixel_indices = pixel_indices;
  13136. // | col0 >= col1 col0 < col1
  13137. //------------------------------------------------------
  13138. // (dist & 1) = 1 | no need to swap | need to swap
  13139. // |-----------------+----------------
  13140. // (dist & 1) = 0 | need to swap | no need to swap
  13141. //
  13142. // This can be done with an xor test.
  13143. best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
  13144. best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
  13145. if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
  13146. {
  13147. swapColors(best_colorsRGB444);
  13148. // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
  13149. best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
  13150. }
  13151. // Put the compress params into the compression block
  13152. compressed1 = 0;
  13153. PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
  13154. PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
  13155. PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
  13156. PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
  13157. PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
  13158. PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
  13159. PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
  13160. best_pixel_indices=indexConversion(best_pixel_indices);
  13161. compressed2 = 0;
  13162. PUTBITS( compressed2, best_pixel_indices, 32, 31);
  13163. return best_error_using_Hmode;
  13164. }
  13165. #endif
  13166. #if EXHAUSTIVE_CODE_ACTIVE
  13167. // The below code should compress the block to 58 bits.
  13168. // This is supposed to match the first of the three modes in TWOTIMER.
  13169. // The bit layout is thought to be:
  13170. //
  13171. //|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|
  13172. //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
  13173. //
  13174. //|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|
  13175. //|----------------------------------------index bits---------------------------------------------|
  13176. //
  13177. // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
  13178. // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
  13179. // Else, it is assumed to be 1.
  13180. // The below code should compress the block to 58 bits.
  13181. // This is supposed to match the first of the three modes in TWOTIMER.
  13182. // The bit layout is thought to be:
  13183. //
  13184. //|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|
  13185. //|-------empty-----|---red 0---|--green 0--|--blue 0---|---red 1---|--green 1--|--blue 1---|d2 d1|
  13186. //
  13187. //|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|
  13188. //|----------------------------------------index bits---------------------------------------------|
  13189. //
  13190. // The distance d is three bits, d2 (MSB), d1 and d0 (LSB). d0 is not stored explicitly.
  13191. // Instead if the 12-bit word red0,green0,blue0 < red1,green1,blue1, d0 is assumed to be 0.
  13192. // Else, it is assumed to be 1.
  13193. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  13194. 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)
  13195. {
  13196. unsigned int best_error_using_Hmode;
  13197. uint8 best_colorsRGB444[2][3];
  13198. unsigned int best_pixel_indices;
  13199. uint8 best_distance;
  13200. unsigned int error;
  13201. uint8 colorsRGB444[2][3];
  13202. int colorsRGB444_packed[2];
  13203. int best_colorsRGB444_packed[2];
  13204. int colorRGB444_packed;
  13205. unsigned int pixel_indices;
  13206. uint8 distance;
  13207. unsigned int *precalc_err; // smallest error per color, table and pixel
  13208. unsigned int *precalc_err_RG; // smallest pixel error for an entire table
  13209. unsigned int *precalc_err_R; // smallest pixel error for an entire table
  13210. uint8 block[4*4*4];
  13211. best_error_using_Hmode = MAXIMUM_ERROR;
  13212. precalc_err = (unsigned int*) malloc(4096*8*16*sizeof(unsigned int));
  13213. if(!precalc_err){printf("Out of memory allocating \n");exit(1);}
  13214. precalc_err_RG = (unsigned int*) malloc(16*16*8*16*sizeof(unsigned int));
  13215. if(!precalc_err_RG){printf("Out of memory allocating \n");exit(1);}
  13216. precalc_err_R = (unsigned int*) malloc(16*8*16*sizeof(unsigned int));
  13217. if(!precalc_err_R){printf("Out of memory allocating \n");exit(1);}
  13218. unsigned int test1, test2;
  13219. best_error_using_Hmode = (unsigned int)compressBlockTHUMB58HFastest(img,width, height, startx, starty, test1, test2);
  13220. best_colorsRGB444_packed[0] = 0;
  13221. best_colorsRGB444_packed[0] = GETBITSHIGH(test1, 12, 57);
  13222. best_colorsRGB444_packed[1] = 0;
  13223. best_colorsRGB444_packed[1] = GETBITSHIGH(test1, 12, 45);
  13224. if(best_error_using_Hmode < best_error_so_far)
  13225. best_error_so_far = best_error_using_Hmode;
  13226. int xx,yy,count = 0;
  13227. // 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.
  13228. for(xx = 0; xx<4; xx++)
  13229. {
  13230. for(yy=0; yy<4; yy++)
  13231. {
  13232. block[(count)*4] = img[((starty+yy)*width+(startx+xx))*3];
  13233. block[(count)*4+1] = img[((starty+yy)*width+(startx+xx))*3+1];
  13234. block[(count)*4+2] = img[((starty+yy)*width+(startx+xx))*3+2];
  13235. block[(count)*4+3] = 0;
  13236. count++;
  13237. }
  13238. }
  13239. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed++)
  13240. {
  13241. colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
  13242. colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
  13243. colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
  13244. precalcError58H(block, colorsRGB444, colorRGB444_packed, precalc_err);
  13245. }
  13246. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16)
  13247. {
  13248. colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
  13249. colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
  13250. colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
  13251. precalcErrorRG_58H(img, width, startx, starty, colorsRGB444, colorRGB444_packed, precalc_err_RG);
  13252. }
  13253. for( colorRGB444_packed = 0; colorRGB444_packed<16*16*16; colorRGB444_packed+=16*16)
  13254. {
  13255. colorsRGB444[0][0] = (colorRGB444_packed >> 8) & 0xf;
  13256. colorsRGB444[0][1] = (colorRGB444_packed >> 4) & 0xf;
  13257. colorsRGB444[0][2] = (colorRGB444_packed) & 0xf;
  13258. precalcErrorR_58H(img, width, startx, starty, colorsRGB444, colorRGB444_packed, precalc_err_R);
  13259. }
  13260. int trycols = 0;
  13261. int allcols = 0;
  13262. for( colorsRGB444[0][0] = 0; colorsRGB444[0][0] <16; colorsRGB444[0][0]++)
  13263. {
  13264. colorsRGB444_packed[0] = colorsRGB444[0][0]*256;
  13265. for( colorsRGB444[1][0] = 0; colorsRGB444[1][0] <16; colorsRGB444[1][0]++)
  13266. {
  13267. colorsRGB444_packed[1] = colorsRGB444[1][0]*256;
  13268. if(colorsRGB444_packed[0] <= colorsRGB444_packed[1])
  13269. {
  13270. error = calculateErrorFromPrecalcR58H(colorsRGB444_packed, precalc_err_R, best_error_so_far);
  13271. if(error < best_error_so_far)
  13272. {
  13273. for( colorsRGB444[0][1] = 0; colorsRGB444[0][1] <16; colorsRGB444[0][1]++)
  13274. {
  13275. colorsRGB444_packed[0] = colorsRGB444[0][0]*256 + colorsRGB444[0][1]*16;
  13276. for( colorsRGB444[1][1] = 0; colorsRGB444[1][1] <16; colorsRGB444[1][1]++)
  13277. {
  13278. colorsRGB444_packed[1] = colorsRGB444[1][0]*256 + colorsRGB444[1][1]*16;
  13279. if(colorsRGB444_packed[0] <= colorsRGB444_packed[1])
  13280. {
  13281. error = calculateErrorFromPrecalcRG58H(colorsRGB444_packed, precalc_err_RG, best_error_so_far);
  13282. if(error < best_error_so_far)
  13283. {
  13284. for( colorsRGB444[0][2] = 0; colorsRGB444[0][2] <16; colorsRGB444[0][2]++)
  13285. {
  13286. colorsRGB444_packed[0] = colorsRGB444[0][0]*256 + colorsRGB444[0][1]*16 + colorsRGB444[0][2];
  13287. for( colorsRGB444[1][2] = 0; colorsRGB444[1][2] <16; colorsRGB444[1][2]++)
  13288. {
  13289. colorsRGB444_packed[1] = colorsRGB444[1][0]*256 + colorsRGB444[1][1]*16 + colorsRGB444[1][2];
  13290. if(colorsRGB444_packed[0] < colorsRGB444_packed[1])
  13291. {
  13292. error = calculateErrorFromPrecalc58H(colorsRGB444_packed, precalc_err, best_error_so_far);
  13293. if(error < best_error_so_far)
  13294. {
  13295. best_error_so_far = error;
  13296. best_error_using_Hmode = error;
  13297. best_colorsRGB444_packed[0] = colorsRGB444_packed[0];
  13298. best_colorsRGB444_packed[1] = colorsRGB444_packed[1];
  13299. }
  13300. }
  13301. }
  13302. }
  13303. }
  13304. }
  13305. }
  13306. }
  13307. }
  13308. }
  13309. }
  13310. }
  13311. best_colorsRGB444[0][0] = (best_colorsRGB444_packed[0] >> 8) & 0xf;
  13312. best_colorsRGB444[0][1] = (best_colorsRGB444_packed[0] >> 4) & 0xf;
  13313. best_colorsRGB444[0][2] = (best_colorsRGB444_packed[0]) & 0xf;
  13314. best_colorsRGB444[1][0] = (best_colorsRGB444_packed[1] >> 8) & 0xf;
  13315. best_colorsRGB444[1][1] = (best_colorsRGB444_packed[1] >> 4) & 0xf;
  13316. best_colorsRGB444[1][2] = (best_colorsRGB444_packed[1]) & 0xf;
  13317. free(precalc_err);
  13318. free(precalc_err_RG);
  13319. free(precalc_err_R);
  13320. error = (unsigned int) calculateErrorAndCompress58H(img, width, startx, starty, best_colorsRGB444, distance, pixel_indices);
  13321. best_distance = distance;
  13322. best_pixel_indices = pixel_indices;
  13323. // | col0 >= col1 col0 < col1
  13324. //------------------------------------------------------
  13325. // (dist & 1) = 1 | no need to swap | need to swap
  13326. // |-----------------+----------------
  13327. // (dist & 1) = 0 | need to swap | no need to swap
  13328. //
  13329. // This can be done with an xor test.
  13330. best_colorsRGB444_packed[0] = (best_colorsRGB444[0][R] << 8) + (best_colorsRGB444[0][G] << 4) + best_colorsRGB444[0][B];
  13331. best_colorsRGB444_packed[1] = (best_colorsRGB444[1][R] << 8) + (best_colorsRGB444[1][G] << 4) + best_colorsRGB444[1][B];
  13332. if( (best_colorsRGB444_packed[0] >= best_colorsRGB444_packed[1]) ^ ((best_distance & 1)==1) )
  13333. {
  13334. swapColors(best_colorsRGB444);
  13335. // Reshuffle pixel indices to to exchange C1 with C3, and C2 with C4
  13336. best_pixel_indices = (0x55555555 & best_pixel_indices) | (0xaaaaaaaa & (~best_pixel_indices));
  13337. }
  13338. // Put the compress params into the compression block
  13339. compressed1 = 0;
  13340. PUTBITSHIGH( compressed1, best_colorsRGB444[0][R], 4, 57);
  13341. PUTBITSHIGH( compressed1, best_colorsRGB444[0][G], 4, 53);
  13342. PUTBITSHIGH( compressed1, best_colorsRGB444[0][B], 4, 49);
  13343. PUTBITSHIGH( compressed1, best_colorsRGB444[1][R], 4, 45);
  13344. PUTBITSHIGH( compressed1, best_colorsRGB444[1][G], 4, 41);
  13345. PUTBITSHIGH( compressed1, best_colorsRGB444[1][B], 4, 37);
  13346. PUTBITSHIGH( compressed1, (best_distance >> 1), 2, 33);
  13347. best_pixel_indices=indexConversion(best_pixel_indices);
  13348. compressed2 = 0;
  13349. PUTBITS( compressed2, best_pixel_indices, 32, 31);
  13350. return best_error_using_Hmode;
  13351. }
  13352. #endif
  13353. #if EXHAUSTIVE_CODE_ACTIVE
  13354. // Compress a block exhaustively for the ETC1 codec.
  13355. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  13356. void compressBlockETC1Exhaustive(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  13357. {
  13358. unsigned int error_currently_best;
  13359. unsigned int etc1_differential_word1;
  13360. unsigned int etc1_differential_word2;
  13361. unsigned int error_etc1_differential;
  13362. unsigned int etc1_individual_word1;
  13363. unsigned int etc1_individual_word2;
  13364. unsigned int error_etc1_individual;
  13365. unsigned int error_best;
  13366. signed char best_char;
  13367. int best_mode;
  13368. error_currently_best = 255*255*16*3;
  13369. // First pass -- quickly find a low error so that we can later cull away a lot of
  13370. // calculations later that are guaranteed to be higher than that error.
  13371. unsigned int error_etc1;
  13372. unsigned int etc1_word1;
  13373. unsigned int etc1_word2;
  13374. error_etc1 = (unsigned int) compressBlockDiffFlipFast(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
  13375. if(error_etc1 < error_currently_best)
  13376. error_currently_best = error_etc1;
  13377. error_etc1_individual = compressBlockIndividualExhaustive(img, width, height, startx, starty, etc1_individual_word1, etc1_individual_word2, error_currently_best);
  13378. if(error_etc1_individual < error_currently_best)
  13379. error_currently_best = error_etc1_individual;
  13380. error_etc1_differential = compressBlockDifferentialExhaustive(img, width, height, startx, starty, etc1_differential_word1, etc1_differential_word2, error_currently_best);
  13381. if(error_etc1_differential < error_currently_best)
  13382. error_currently_best = error_etc1_differential;
  13383. error_best = error_etc1_differential;
  13384. compressed1 = etc1_differential_word1;
  13385. compressed2 = etc1_differential_word2;
  13386. best_char = '.';
  13387. best_mode = MODE_ETC1;
  13388. if(error_etc1_individual < error_best)
  13389. {
  13390. compressed1 = etc1_individual_word1;
  13391. compressed2 = etc1_individual_word2;
  13392. best_char = ',';
  13393. error_best = error_etc1_individual;
  13394. best_mode = MODE_ETC1;
  13395. }
  13396. if(error_etc1 < error_best)
  13397. {
  13398. compressed1 = etc1_word1;
  13399. compressed2 = etc1_word2;
  13400. best_char = '.';
  13401. error_best = error_etc1;
  13402. best_mode = MODE_ETC1;
  13403. }
  13404. }
  13405. #endif
  13406. #if EXHAUSTIVE_CODE_ACTIVE
  13407. // Compress a block exhaustively for the ETC1 codec using perceptual error measure.
  13408. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  13409. void compressBlockETC1ExhaustivePerceptual(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  13410. {
  13411. unsigned int error_currently_best;
  13412. unsigned int etc1_differential_word1;
  13413. unsigned int etc1_differential_word2;
  13414. unsigned int error_etc1_differential;
  13415. unsigned int etc1_individual_word1;
  13416. unsigned int etc1_individual_word2;
  13417. unsigned int error_etc1_individual;
  13418. unsigned int error_best;
  13419. signed char best_char;
  13420. int best_mode;
  13421. error_currently_best = 255*255*16*1000;
  13422. // First pass -- quickly find a low error so that we can later cull away a lot of
  13423. // calculations later that are guaranteed to be higher than that error.
  13424. unsigned int error_etc1;
  13425. unsigned int etc1_word1;
  13426. unsigned int etc1_word2;
  13427. compressBlockDiffFlipFastPerceptual(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
  13428. decompressBlockDiffFlip(etc1_word1, etc1_word2, imgdec, width, height, startx, starty);
  13429. error_etc1 = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
  13430. if(error_etc1 < error_currently_best)
  13431. error_currently_best = error_etc1;
  13432. // Second pass --- now find the lowest error, but only if it is lower than error_currently_best
  13433. error_etc1_differential = compressBlockDifferentialExhaustivePerceptual(img, width, height, startx, starty, etc1_differential_word1, etc1_differential_word2, error_currently_best);
  13434. if(error_etc1_differential < error_currently_best)
  13435. error_currently_best = error_etc1_differential;
  13436. error_etc1_individual = compressBlockIndividualExhaustivePerceptual(img, width, height, startx, starty, etc1_individual_word1, etc1_individual_word2, error_currently_best);
  13437. if(error_etc1_individual < error_currently_best)
  13438. error_currently_best = error_etc1_individual;
  13439. // Now find the best error.
  13440. error_best = error_etc1;
  13441. compressed1 = etc1_word1;
  13442. compressed2 = etc1_word2;
  13443. best_char = '.';
  13444. best_mode = MODE_ETC1;
  13445. if(error_etc1_differential < error_best)
  13446. {
  13447. error_best = error_etc1_differential;
  13448. compressed1 = etc1_differential_word1;
  13449. compressed2 = etc1_differential_word2;
  13450. best_char = '.';
  13451. best_mode = MODE_ETC1;
  13452. }
  13453. if(error_etc1_individual < error_best)
  13454. {
  13455. compressed1 = etc1_individual_word1;
  13456. compressed2 = etc1_individual_word2;
  13457. best_char = ',';
  13458. error_best = error_etc1_individual;
  13459. best_mode = MODE_ETC1;
  13460. }
  13461. }
  13462. #endif
  13463. #if EXHAUSTIVE_CODE_ACTIVE
  13464. // Compress a block exhaustively for the ETC2 RGB codec using perceptual error measure.
  13465. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  13466. void compressBlockETC2ExhaustivePerceptual(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  13467. {
  13468. unsigned int error_currently_best;
  13469. unsigned int etc1_differential_word1;
  13470. unsigned int etc1_differential_word2;
  13471. unsigned int error_etc1_differential;
  13472. unsigned int etc1_individual_word1;
  13473. unsigned int etc1_individual_word2;
  13474. unsigned int error_etc1_individual;
  13475. unsigned int planar57_word1;
  13476. unsigned int planar57_word2;
  13477. unsigned int planar_word1;
  13478. unsigned int planar_word2;
  13479. double error_planar;
  13480. unsigned int error_planar_red, error_planar_green, error_planar_blue;
  13481. unsigned int thumbH58_word1;
  13482. unsigned int thumbH58_word2;
  13483. unsigned int thumbH_word1;
  13484. unsigned int thumbH_word2;
  13485. unsigned int error_thumbH;
  13486. unsigned int thumbT59_word1;
  13487. unsigned int thumbT59_word2;
  13488. unsigned int thumbT_word1;
  13489. unsigned int thumbT_word2;
  13490. unsigned int error_thumbT;
  13491. unsigned int error_best;
  13492. signed char best_char;
  13493. int best_mode;
  13494. error_currently_best = 255*255*16*1000;
  13495. // First pass -- quickly find a low error so that we can later cull away a lot of
  13496. // calculations later that are guaranteed to be higher than that error.
  13497. unsigned int error_etc1;
  13498. unsigned int etc1_word1;
  13499. unsigned int etc1_word2;
  13500. compressBlockDiffFlipFastPerceptual(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
  13501. decompressBlockDiffFlip(etc1_word1, etc1_word2, imgdec, width, height, startx, starty);
  13502. error_etc1 = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
  13503. if(error_etc1 < error_currently_best)
  13504. error_currently_best = error_etc1;
  13505. // The planar mode treats every channel independently and should not be affected by the weights in the error measure.
  13506. // We can hence use the nonperceptual version of the encoder also to find the best perceptual description of the block.
  13507. compressBlockPlanar57(img, width, height, startx, starty, planar57_word1, planar57_word2);
  13508. decompressBlockPlanar57errorPerComponent(planar57_word1, planar57_word2, imgdec, width, height, startx, starty, img, error_planar_red, error_planar_green, error_planar_blue);
  13509. error_planar = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
  13510. stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
  13511. if(error_planar < error_currently_best)
  13512. error_currently_best = (unsigned int) error_planar;
  13513. error_thumbT = (unsigned int) compressBlockTHUMB59TFastestPerceptual1000(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
  13514. stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
  13515. if(error_thumbT < error_currently_best)
  13516. error_currently_best = error_thumbT;
  13517. error_thumbH = (unsigned int) compressBlockTHUMB58HFastestPerceptual1000(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
  13518. stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
  13519. if(error_thumbH < error_currently_best)
  13520. error_currently_best = error_thumbH;
  13521. // Second pass --- now find the lowest error, but only if it is lower than error_currently_best
  13522. // Correct the individual errors for the different planes so that they sum to 1000 instead of 1.
  13523. error_planar_red *=PERCEPTUAL_WEIGHT_R_SQUARED_TIMES1000;
  13524. error_planar_green *=PERCEPTUAL_WEIGHT_G_SQUARED_TIMES1000;
  13525. error_planar_blue *=PERCEPTUAL_WEIGHT_B_SQUARED_TIMES1000;
  13526. compressBlockPlanar57ExhaustivePerceptual(img, width, height, startx, starty, planar57_word1, planar57_word2, error_currently_best, error_planar_red, error_planar_green, error_planar_blue);
  13527. decompressBlockPlanar57(planar57_word1, planar57_word2, imgdec, width, height, startx, starty);
  13528. error_planar = 1000*calcBlockPerceptualErrorRGB(img, imgdec, width, height, startx, starty);
  13529. stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
  13530. if(error_planar < error_currently_best)
  13531. error_currently_best = (unsigned int) error_planar;
  13532. error_etc1_differential = compressBlockDifferentialExhaustivePerceptual(img, width, height, startx, starty, etc1_differential_word1, etc1_differential_word2, error_currently_best);
  13533. if(error_etc1_differential < error_currently_best)
  13534. error_currently_best = error_etc1_differential;
  13535. error_etc1_individual = compressBlockIndividualExhaustivePerceptual(img, width, height, startx, starty, etc1_individual_word1, etc1_individual_word2, error_currently_best);
  13536. if(error_etc1_individual < error_currently_best)
  13537. error_currently_best = error_etc1_individual;
  13538. error_thumbH = compressBlockTHUMB58HExhaustivePerceptual(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2, error_currently_best);
  13539. stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
  13540. if( error_thumbH < error_currently_best)
  13541. error_currently_best = error_thumbH;
  13542. error_thumbT = compressBlockTHUMB59TExhaustivePerceptual(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2, error_currently_best);
  13543. stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
  13544. if(error_thumbT < error_currently_best)
  13545. error_currently_best = error_thumbT;
  13546. // Now find the best error.
  13547. error_best = error_etc1;
  13548. compressed1 = etc1_word1;
  13549. compressed2 = etc1_word2;
  13550. best_char = '.';
  13551. best_mode = MODE_ETC1;
  13552. if(error_etc1_differential < error_best)
  13553. {
  13554. error_best = error_etc1_differential;
  13555. compressed1 = etc1_differential_word1;
  13556. compressed2 = etc1_differential_word2;
  13557. best_char = '.';
  13558. best_mode = MODE_ETC1;
  13559. }
  13560. if(error_etc1_individual < error_best)
  13561. {
  13562. compressed1 = etc1_individual_word1;
  13563. compressed2 = etc1_individual_word2;
  13564. best_char = ',';
  13565. error_best = error_etc1_individual;
  13566. best_mode = MODE_ETC1;
  13567. }
  13568. if(error_planar < error_best)
  13569. {
  13570. compressed1 = planar_word1;
  13571. compressed2 = planar_word2;
  13572. best_char = 'p';
  13573. error_best = (unsigned int) error_planar;
  13574. best_mode = MODE_PLANAR;
  13575. }
  13576. if(error_thumbH < error_best)
  13577. {
  13578. compressed1 = thumbH_word1;
  13579. compressed2 = thumbH_word2;
  13580. best_char = 'H';
  13581. error_best = error_thumbH;
  13582. best_mode = MODE_THUMB_H;
  13583. }
  13584. if(error_thumbT < error_best)
  13585. {
  13586. compressed1 = thumbT_word1;
  13587. compressed2 = thumbT_word2;
  13588. best_char = 'T';
  13589. error_best = error_thumbT;
  13590. best_mode = MODE_THUMB_T;
  13591. }
  13592. }
  13593. #endif
  13594. #if EXHAUSTIVE_CODE_ACTIVE
  13595. // Compress a block exhaustively for the ETC2 RGB codec.
  13596. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  13597. void compressBlockETC2Exhaustive(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2)
  13598. {
  13599. unsigned int error_currently_best;
  13600. unsigned int etc1_differential_word1;
  13601. unsigned int etc1_differential_word2;
  13602. unsigned int error_etc1_differential;
  13603. unsigned int etc1_individual_word1;
  13604. unsigned int etc1_individual_word2;
  13605. unsigned int error_etc1_individual;
  13606. unsigned int planar57_word1;
  13607. unsigned int planar57_word2;
  13608. unsigned int planar_word1;
  13609. unsigned int planar_word2;
  13610. double error_planar;
  13611. unsigned int error_planar_red;
  13612. unsigned int error_planar_green;
  13613. unsigned int error_planar_blue;
  13614. unsigned int thumbH58_word1;
  13615. unsigned int thumbH58_word2;
  13616. unsigned int thumbH_word1;
  13617. unsigned int thumbH_word2;
  13618. unsigned int error_thumbH;
  13619. unsigned int thumbT59_word1;
  13620. unsigned int thumbT59_word2;
  13621. unsigned int thumbT_word1;
  13622. unsigned int thumbT_word2;
  13623. unsigned int error_thumbT;
  13624. unsigned int error_best;
  13625. signed char best_char;
  13626. int best_mode;
  13627. error_currently_best = 255*255*16*3;
  13628. // First pass -- quickly find a low error so that we can later cull away a lot of
  13629. // calculations later that are guaranteed to be higher than that error.
  13630. unsigned int error_etc1;
  13631. unsigned int etc1_word1;
  13632. unsigned int etc1_word2;
  13633. error_etc1 = (unsigned int) compressBlockDiffFlipFast(img, imgdec, width, height, startx, starty, etc1_word1, etc1_word2);
  13634. if(error_etc1 < error_currently_best)
  13635. error_currently_best = error_etc1;
  13636. compressBlockPlanar57(img, width, height, startx, starty, planar57_word1, planar57_word2);
  13637. decompressBlockPlanar57errorPerComponent(planar57_word1, planar57_word2, imgdec, width, height, startx, starty, img, error_planar_red, error_planar_green, error_planar_blue);
  13638. error_planar = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  13639. stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
  13640. if(error_planar < error_currently_best)
  13641. error_currently_best = (unsigned int) error_planar;
  13642. error_thumbT = (unsigned int) compressBlockTHUMB59TFastest(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2);
  13643. stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
  13644. if(error_thumbT < error_currently_best)
  13645. error_currently_best = error_thumbT;
  13646. error_thumbH = (unsigned int) compressBlockTHUMB58HFastest(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2);
  13647. stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
  13648. if(error_thumbH < error_currently_best)
  13649. error_currently_best = error_thumbH;
  13650. // Second pass --- now find the lowest error, but only if it is lower than error_currently_best
  13651. error_etc1_differential = compressBlockDifferentialExhaustive(img, width, height, startx, starty, etc1_differential_word1, etc1_differential_word2, error_currently_best);
  13652. if(error_etc1_differential < error_currently_best)
  13653. error_currently_best = error_etc1_differential;
  13654. compressBlockPlanar57Exhaustive(img, width, height, startx, starty, planar57_word1, planar57_word2, error_currently_best, error_planar_red, error_planar_green, error_planar_blue);
  13655. decompressBlockPlanar57(planar57_word1, planar57_word2, imgdec, width, height, startx, starty);
  13656. error_planar = calcBlockErrorRGB(img, imgdec, width, height, startx, starty);
  13657. stuff57bits(planar57_word1, planar57_word2, planar_word1, planar_word2);
  13658. if(error_planar < error_currently_best)
  13659. error_currently_best = (unsigned int) error_planar;
  13660. error_etc1_individual = compressBlockIndividualExhaustive(img, width, height, startx, starty, etc1_individual_word1, etc1_individual_word2, error_currently_best);
  13661. if(error_etc1_individual < error_currently_best)
  13662. error_currently_best = error_etc1_individual;
  13663. error_thumbH = compressBlockTHUMB58HExhaustive(img,width,height,startx, starty, thumbH58_word1, thumbH58_word2, error_currently_best);
  13664. if( error_thumbH < error_currently_best)
  13665. error_currently_best = error_thumbH;
  13666. stuff58bits(thumbH58_word1, thumbH58_word2, thumbH_word1, thumbH_word2);
  13667. error_thumbT = compressBlockTHUMB59TExhaustive(img,width, height, startx, starty, thumbT59_word1, thumbT59_word2, error_currently_best);
  13668. if(error_thumbT < error_currently_best)
  13669. error_currently_best = error_thumbT;
  13670. stuff59bits(thumbT59_word1, thumbT59_word2, thumbT_word1, thumbT_word2);
  13671. error_best = 255*255*3*16;
  13672. // Now find the best error.
  13673. error_best = error_etc1;
  13674. compressed1 = etc1_word1;
  13675. compressed2 = etc1_word2;
  13676. best_char = '.';
  13677. best_mode = MODE_ETC1;
  13678. if(error_etc1_differential < error_best)
  13679. {
  13680. error_best = error_etc1_differential;
  13681. compressed1 = etc1_differential_word1;
  13682. compressed2 = etc1_differential_word2;
  13683. best_char = '.';
  13684. best_mode = MODE_ETC1;
  13685. }
  13686. if(error_etc1_individual < error_best)
  13687. {
  13688. compressed1 = etc1_individual_word1;
  13689. compressed2 = etc1_individual_word2;
  13690. best_char = ',';
  13691. error_best = error_etc1_individual;
  13692. best_mode = MODE_ETC1;
  13693. }
  13694. if(error_planar < error_best)
  13695. {
  13696. compressed1 = planar_word1;
  13697. compressed2 = planar_word2;
  13698. best_char = 'p';
  13699. error_best = (unsigned int) error_planar;
  13700. best_mode = MODE_PLANAR;
  13701. }
  13702. if(error_thumbH < error_best)
  13703. {
  13704. compressed1 = thumbH_word1;
  13705. compressed2 = thumbH_word2;
  13706. best_char = 'H';
  13707. error_best = error_thumbH;
  13708. best_mode = MODE_THUMB_H;
  13709. }
  13710. if(error_thumbT < error_best)
  13711. {
  13712. compressed1 = thumbT_word1;
  13713. compressed2 = thumbT_word2;
  13714. best_char = 'T';
  13715. error_best = error_thumbT;
  13716. best_mode = MODE_THUMB_T;
  13717. }
  13718. }
  13719. #endif
  13720. //// Exhaustive code ends here.
  13721. // Compress an image file.
  13722. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  13723. void compressImageFile(uint8 *img, uint8 *alphaimg,int width,int height,char *dstfile, int expandedwidth, int expandedheight)
  13724. {
  13725. FILE *f;
  13726. int x,y,w,h;
  13727. unsigned int block1, block2;
  13728. unsigned short wi, hi;
  13729. unsigned char magic[4];
  13730. unsigned char version[2];
  13731. unsigned short texture_type=format;
  13732. uint8 *imgdec;
  13733. uint8* alphaimg2;
  13734. imgdec = (unsigned char*) malloc(expandedwidth*expandedheight*3);
  13735. if(!imgdec)
  13736. {
  13737. printf("Could not allocate decompression buffer --- exiting\n");
  13738. }
  13739. magic[0] = 'P'; magic[1] = 'K'; magic[2] = 'M'; magic[3] = ' ';
  13740. if(codec==CODEC_ETC2)
  13741. {
  13742. version[0] = '2'; version[1] = '0';
  13743. }
  13744. else
  13745. {
  13746. version[0] = '1'; version[1] = '0';
  13747. }
  13748. if(f=fopen(dstfile,"wb"))
  13749. {
  13750. w=expandedwidth/4; w*=4;
  13751. h=expandedheight/4; h*=4;
  13752. wi = w;
  13753. hi = h;
  13754. if(ktxFile)
  13755. {
  13756. //.ktx file: KTX header followed by compressed binary data.
  13757. KTX_header header;
  13758. //identifier
  13759. for(int i=0; i<12; i++)
  13760. {
  13761. header.identifier[i]=ktx_identifier[i];
  13762. }
  13763. //endianess int.. if this comes out reversed, all of the other ints will too.
  13764. header.endianness=KTX_ENDIAN_REF;
  13765. //these values are always 0/1 for compressed textures.
  13766. header.glType=0;
  13767. header.glTypeSize=1;
  13768. header.glFormat=0;
  13769. header.pixelWidth=width;
  13770. header.pixelHeight=height;
  13771. header.pixelDepth=0;
  13772. //we only support single non-mipmapped non-cubemap textures..
  13773. header.numberOfArrayElements=0;
  13774. header.numberOfFaces=1;
  13775. header.numberOfMipmapLevels=1;
  13776. //and no metadata..
  13777. header.bytesOfKeyValueData=0;
  13778. int halfbytes=1;
  13779. //header.glInternalFormat=?
  13780. //header.glBaseInternalFormat=?
  13781. if(format==ETC2PACKAGE_R_NO_MIPMAPS)
  13782. {
  13783. header.glBaseInternalFormat=GL_R;
  13784. if(formatSigned)
  13785. header.glInternalFormat=GL_COMPRESSED_SIGNED_R11_EAC;
  13786. else
  13787. header.glInternalFormat=GL_COMPRESSED_R11_EAC;
  13788. }
  13789. else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  13790. {
  13791. halfbytes=2;
  13792. header.glBaseInternalFormat=GL_RG;
  13793. if(formatSigned)
  13794. header.glInternalFormat=GL_COMPRESSED_SIGNED_RG11_EAC;
  13795. else
  13796. header.glInternalFormat=GL_COMPRESSED_RG11_EAC;
  13797. }
  13798. else if(format==ETC2PACKAGE_RGB_NO_MIPMAPS)
  13799. {
  13800. header.glBaseInternalFormat=GL_RGB;
  13801. header.glInternalFormat=GL_COMPRESSED_RGB8_ETC2;
  13802. }
  13803. else if(format==ETC2PACKAGE_sRGB_NO_MIPMAPS)
  13804. {
  13805. header.glBaseInternalFormat=GL_SRGB;
  13806. header.glInternalFormat=GL_COMPRESSED_SRGB8_ETC2;
  13807. }
  13808. else if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS)
  13809. {
  13810. halfbytes=2;
  13811. header.glBaseInternalFormat=GL_RGBA;
  13812. header.glInternalFormat=GL_COMPRESSED_RGBA8_ETC2_EAC;
  13813. }
  13814. else if(format==ETC2PACKAGE_sRGBA_NO_MIPMAPS)
  13815. {
  13816. halfbytes=2;
  13817. header.glBaseInternalFormat=GL_SRGB8_ALPHA8;
  13818. header.glInternalFormat=GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
  13819. }
  13820. else if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS)
  13821. {
  13822. header.glBaseInternalFormat=GL_RGBA;
  13823. header.glInternalFormat=GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
  13824. }
  13825. else if(format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  13826. {
  13827. header.glBaseInternalFormat=GL_SRGB8_ALPHA8;
  13828. header.glInternalFormat=GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
  13829. }
  13830. else if(format==ETC1_RGB_NO_MIPMAPS)
  13831. {
  13832. header.glBaseInternalFormat=GL_RGB;
  13833. header.glInternalFormat=GL_ETC1_RGB8_OES;
  13834. }
  13835. else
  13836. {
  13837. printf("internal error: bad format!\n");
  13838. exit(1);
  13839. }
  13840. //write header
  13841. fwrite(&header,sizeof(KTX_header),1,f);
  13842. //write size of compressed data.. which depend on the expanded size..
  13843. unsigned int imagesize=(w*h*halfbytes)/2;
  13844. fwrite(&imagesize,sizeof(int),1,f);
  13845. }
  13846. else
  13847. {
  13848. //.pkm file, contains small header..
  13849. // Write magic number
  13850. fwrite(&magic[0], sizeof(unsigned char), 1, f);
  13851. fwrite(&magic[1], sizeof(unsigned char), 1, f);
  13852. fwrite(&magic[2], sizeof(unsigned char), 1, f);
  13853. fwrite(&magic[3], sizeof(unsigned char), 1, f);
  13854. // Write version
  13855. fwrite(&version[0], sizeof(unsigned char), 1, f);
  13856. fwrite(&version[1], sizeof(unsigned char), 1, f);
  13857. // Write texture type
  13858. if(texture_type==ETC2PACKAGE_RG_NO_MIPMAPS&&formatSigned)
  13859. {
  13860. unsigned short temp = ETC2PACKAGE_RG_SIGNED_NO_MIPMAPS;
  13861. write_big_endian_2byte_word(&temp,f);
  13862. }
  13863. else if(texture_type==ETC2PACKAGE_R_NO_MIPMAPS&&formatSigned)
  13864. {
  13865. unsigned short temp = ETC2PACKAGE_R_SIGNED_NO_MIPMAPS;
  13866. write_big_endian_2byte_word(&temp,f);
  13867. }
  13868. else
  13869. write_big_endian_2byte_word(&texture_type, f);
  13870. // Write binary header: the width and height as unsigned 16-bit words
  13871. write_big_endian_2byte_word(&wi, f);
  13872. write_big_endian_2byte_word(&hi, f);
  13873. // Also write the active pixels. For instance, if we want to compress
  13874. // a 128 x 129 image, we have to extend it to 128 x 132 pixels.
  13875. // Then the wi and hi written above will be 128 and 132, but the
  13876. // additional information that we write below will be 128 and 129,
  13877. // to indicate that it is only the top 129 lines of data in the
  13878. // decompressed image that will be valid data, and the rest will
  13879. // be just garbage.
  13880. unsigned short activew, activeh;
  13881. activew = width;
  13882. activeh = height;
  13883. write_big_endian_2byte_word(&activew, f);
  13884. write_big_endian_2byte_word(&activeh, f);
  13885. }
  13886. int totblocks = expandedheight/4 * expandedwidth/4;
  13887. int countblocks = 0;
  13888. double percentageblocks=-1.0;
  13889. double oldpercentageblocks;
  13890. if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  13891. {
  13892. //extract data from red and green channel into two alpha channels.
  13893. //note that the image will be 16-bit per channel in this case.
  13894. alphaimg= (unsigned char*)malloc(expandedwidth*expandedheight*2);
  13895. alphaimg2=(unsigned char*)malloc(expandedwidth*expandedheight*2);
  13896. setupAlphaTableAndValtab();
  13897. if(!alphaimg||!alphaimg2)
  13898. {
  13899. printf("failed allocating space for alpha buffers!\n");
  13900. exit(1);
  13901. }
  13902. for(y=0;y<expandedheight;y++)
  13903. {
  13904. for(x=0;x<expandedwidth;x++)
  13905. {
  13906. alphaimg[2*(y*expandedwidth+x)]=img[6*(y*expandedwidth+x)];
  13907. alphaimg[2*(y*expandedwidth+x)+1]=img[6*(y*expandedwidth+x)+1];
  13908. alphaimg2[2*(y*expandedwidth+x)]=img[6*(y*expandedwidth+x)+2];
  13909. alphaimg2[2*(y*expandedwidth+x)+1]=img[6*(y*expandedwidth+x)+3];
  13910. }
  13911. }
  13912. }
  13913. for(y=0;y<expandedheight/4;y++)
  13914. {
  13915. for(x=0;x<expandedwidth/4;x++)
  13916. {
  13917. countblocks++;
  13918. oldpercentageblocks = percentageblocks;
  13919. percentageblocks = 100.0*countblocks/(1.0*totblocks);
  13920. //compress color channels
  13921. if(codec==CODEC_ETC)
  13922. {
  13923. if(metric==METRIC_NONPERCEPTUAL)
  13924. {
  13925. if(speed==SPEED_FAST)
  13926. compressBlockDiffFlipFast(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
  13927. else
  13928. #if EXHAUSTIVE_CODE_ACTIVE
  13929. compressBlockETC1Exhaustive(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
  13930. #else
  13931. printf("Not implemented in this version\n");
  13932. #endif
  13933. }
  13934. else
  13935. {
  13936. if(speed==SPEED_FAST)
  13937. compressBlockDiffFlipFastPerceptual(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
  13938. else
  13939. #if EXHAUSTIVE_CODE_ACTIVE
  13940. compressBlockETC1ExhaustivePerceptual(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
  13941. #else
  13942. printf("Not implemented in this version\n");
  13943. #endif
  13944. }
  13945. }
  13946. else
  13947. {
  13948. if(format==ETC2PACKAGE_R_NO_MIPMAPS||format==ETC2PACKAGE_RG_NO_MIPMAPS)
  13949. {
  13950. //don't compress color
  13951. }
  13952. else if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  13953. {
  13954. //this is only available for fast/nonperceptual
  13955. if(speed == SPEED_SLOW && first_time_message)
  13956. {
  13957. printf("Slow codec not implemented for RGBA1 --- using fast codec instead.\n");
  13958. first_time_message = false;
  13959. }
  13960. compressBlockETC2Fast(img, alphaimg,imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
  13961. }
  13962. else if(metric==METRIC_NONPERCEPTUAL)
  13963. {
  13964. if(speed==SPEED_FAST)
  13965. compressBlockETC2Fast(img, alphaimg,imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
  13966. else
  13967. #if EXHAUSTIVE_CODE_ACTIVE
  13968. compressBlockETC2Exhaustive(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
  13969. #else
  13970. printf("Not implemented in this version\n");
  13971. #endif
  13972. }
  13973. else
  13974. {
  13975. if(speed==SPEED_FAST)
  13976. compressBlockETC2FastPerceptual(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
  13977. else
  13978. #if EXHAUSTIVE_CODE_ACTIVE
  13979. compressBlockETC2ExhaustivePerceptual(img, imgdec, expandedwidth, expandedheight, 4*x, 4*y, block1, block2);
  13980. #else
  13981. printf("Not implemented in this version\n");
  13982. #endif
  13983. }
  13984. }
  13985. //compression of alpha channel in case of 4-bit alpha. Uses 8-bit alpha channel as input, and has 8-bit precision.
  13986. if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS)
  13987. {
  13988. uint8 alphadata[8];
  13989. if(speed==SPEED_SLOW)
  13990. compressBlockAlphaSlow(alphaimg,4*x,4*y,expandedwidth,expandedheight,alphadata);
  13991. else
  13992. compressBlockAlphaFast(alphaimg,4*x,4*y,expandedwidth,expandedheight,alphadata);
  13993. //write the 8 bytes of alphadata into f.
  13994. fwrite(alphadata,1,8,f);
  13995. }
  13996. //store compressed color channels
  13997. if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS)
  13998. {
  13999. write_big_endian_4byte_word(&block1, f);
  14000. write_big_endian_4byte_word(&block2, f);
  14001. }
  14002. //1-channel or 2-channel alpha compression: uses 16-bit data as input, and has 11-bit precision
  14003. if(format==ETC2PACKAGE_R_NO_MIPMAPS||format==ETC2PACKAGE_RG_NO_MIPMAPS)
  14004. {
  14005. uint8 alphadata[8];
  14006. compressBlockAlpha16(alphaimg,4*x,4*y,expandedwidth,expandedheight,alphadata);
  14007. fwrite(alphadata,1,8,f);
  14008. }
  14009. //compression of second alpha channel in RG-compression
  14010. if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  14011. {
  14012. uint8 alphadata[8];
  14013. compressBlockAlpha16(alphaimg2,4*x,4*y,expandedwidth,expandedheight,alphadata);
  14014. fwrite(alphadata,1,8,f);
  14015. }
  14016. #if 1
  14017. if(verbose)
  14018. {
  14019. if(speed==SPEED_FAST)
  14020. {
  14021. if( ((int)(percentageblocks) != (int)(oldpercentageblocks) ) || percentageblocks == 100.0)
  14022. 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));
  14023. }
  14024. else
  14025. 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));
  14026. }
  14027. #endif
  14028. }
  14029. }
  14030. printf("\n");
  14031. fclose(f);
  14032. printf("Saved file <%s>.\n",dstfile);
  14033. }
  14034. }
  14035. // Compress an file.
  14036. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  14037. void compressFile(char *srcfile,char *dstfile)
  14038. {
  14039. uint8 *srcimg;
  14040. int width,height;
  14041. int extendedwidth, extendedheight;
  14042. struct _timeb tstruct;
  14043. int tstart;
  14044. int tstop;
  14045. // 0: compress from .any to .pkm with SPEED_FAST, METRIC_NONPERCEPTUAL, ETC
  14046. // 1: compress from .any to .pkm with SPEED_MEDIUM, METRIC_NONPERCEPTUAL, ETC
  14047. // 2: compress from .any to .pkm with SPEED_SLOW, METRIC_NONPERCEPTUAL, ETC
  14048. // 3: compress from .any to .pkm with SPEED_FAST, METRIC_PERCEPTUAL, ETC
  14049. // 4: compress from .any to .pkm with SPEED_MEDIUM, METRIC_PERCEPTUAL, ETC
  14050. // 5: compress from .any to .pkm with SPEED_SLOW, METRIC_PERCEPTUAL, ETC
  14051. // 6: decompress from .pkm to .any
  14052. // 7: calculate PSNR between .any and .any
  14053. // 8: compress from .any to .pkm with SPEED_FAST, METRIC_NONPERCEPTUAL, ETC2
  14054. // 9: compress from .any to .pkm with SPEED_MEDIUM, METRIC_NONPERCEPTUAL, ETC2
  14055. //10: compress from .any to .pkm with SPEED_SLOW, METRIC_NONPERCEPTUAL, ETC2
  14056. //11: compress from .any to .pkm with SPEED_FAST, METRIC_PERCEPTUAL, ETC2
  14057. //12: compress from .any to .pkm with SPEED_MEDIUM, METRIC_PERCEPTUAL, ETC2
  14058. //13: compress from .any to .pkm with SPEED_SLOW, METRIC_PERCEPTUAL, ETC2
  14059. printf("\n");
  14060. if(codec==CODEC_ETC)
  14061. printf("ETC codec, ");
  14062. else
  14063. printf("ETC2 codec, ");
  14064. if(speed==SPEED_FAST)
  14065. printf("using FAST compression mode and ");
  14066. else if(speed==SPEED_MEDIUM)
  14067. printf("using MEDIUM compression mode and ");
  14068. else
  14069. printf("using SLOW compression mode and ");
  14070. if(metric==METRIC_PERCEPTUAL)
  14071. printf("PERCEPTUAL error metric, ");
  14072. else
  14073. printf("NONPERCEPTUAL error metric, ");
  14074. if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS)
  14075. printf("in RGBA format");
  14076. else if(format==ETC2PACKAGE_sRGBA_NO_MIPMAPS)
  14077. printf("in sRGBA format");
  14078. else if(format==ETC2PACKAGE_RGBA1_NO_MIPMAPS)
  14079. printf("in RGB + punch-through alpha format");
  14080. else if(format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  14081. printf("in sRGB + punch-through alpha format");
  14082. else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
  14083. printf("in R format");
  14084. else if(format==ETC2PACKAGE_RGB_NO_MIPMAPS||format==ETC1_RGB_NO_MIPMAPS)
  14085. printf("in RGB format");
  14086. else if(format==ETC2PACKAGE_RG_NO_MIPMAPS)
  14087. printf("in RG format");
  14088. else
  14089. printf("in OTHER format");
  14090. printf("\n");
  14091. if(readCompressParams())
  14092. {
  14093. if(format==ETC2PACKAGE_R_NO_MIPMAPS||readSrcFile(srcfile,srcimg,width,height,extendedwidth, extendedheight))
  14094. {
  14095. //make sure that alphasrcimg contains the alpha channel or is null here, and pass it to compressimagefile
  14096. uint8* alphaimg=NULL;
  14097. if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
  14098. {
  14099. char str[300];
  14100. //printf("reading alpha channel....");
  14101. sprintf(str,"magick convert %s -alpha extract alpha.pgm\n",srcfile);
  14102. system(str);
  14103. readAlpha(alphaimg,width,height,extendedwidth,extendedheight);
  14104. printf("ok!\n");
  14105. setupAlphaTableAndValtab();
  14106. }
  14107. else if(format==ETC2PACKAGE_R_NO_MIPMAPS)
  14108. {
  14109. char str[300];
  14110. sprintf(str,"magick convert %s alpha.pgm\n",srcfile);
  14111. system(str);
  14112. readAlpha(alphaimg,width,height,extendedwidth,extendedheight);
  14113. printf("read alpha ok, size is %d,%d (%d,%d)",width,height,extendedwidth,extendedheight);
  14114. setupAlphaTableAndValtab();
  14115. }
  14116. printf("Compressing...\n");
  14117. tstart=time(NULL);
  14118. _ftime( &tstruct );
  14119. tstart=tstart*1000+tstruct.millitm;
  14120. compressImageFile(srcimg,alphaimg,width,height,dstfile,extendedwidth, extendedheight);
  14121. tstop = time(NULL);
  14122. _ftime( &tstruct );
  14123. tstop = tstop*1000+tstruct.millitm;
  14124. printf( "It took %u milliseconds to compress:\n", tstop - tstart);
  14125. calculatePSNRfile(dstfile,srcimg,alphaimg);
  14126. }
  14127. }
  14128. }
  14129. // Calculates the PSNR between two files.
  14130. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  14131. double calculatePSNRTwoFiles(char *srcfile1,char *srcfile2)
  14132. {
  14133. uint8 *srcimg1;
  14134. uint8 *srcimg2;
  14135. int width1, height1;
  14136. int width2, height2;
  14137. double PSNR;
  14138. double perceptually_weighted_PSNR;
  14139. if(readSrcFileNoExpand(srcfile1,srcimg1,width1,height1))
  14140. {
  14141. if(readSrcFileNoExpand(srcfile2,srcimg2,width2,height2))
  14142. {
  14143. if((width1 == width2) && (height1 == height2))
  14144. {
  14145. PSNR = calculatePSNR(srcimg1, srcimg2, width1, height1);
  14146. printf("%f\n",PSNR);
  14147. perceptually_weighted_PSNR = calculateWeightedPSNR(srcimg1, srcimg2, width1, height1, 0.299, 0.587, 0.114);
  14148. }
  14149. else
  14150. {
  14151. printf("\n Width and height do no not match for image: width, height = (%d, %d) and (%d, %d)\n",width1,height1, width2, height2);
  14152. }
  14153. }
  14154. else
  14155. {
  14156. printf("Couldn't open file %s.\n",srcfile2);
  14157. }
  14158. }
  14159. else
  14160. {
  14161. printf("Couldn't open file %s.\n",srcfile1);
  14162. }
  14163. return PSNR;
  14164. }
  14165. // Main function
  14166. // NO WARRANTY --- SEE STATEMENT IN TOP OF FILE (C) Ericsson AB 2005-2013. All Rights Reserved.
  14167. int main(int argc,char *argv[])
  14168. {
  14169. if(argc==3 || argc==4 || argc == 5 || argc == 7 || argc == 9 || argc == 11 || argc == 13)
  14170. {
  14171. // The source file is always the second last one.
  14172. char srcfile[200];
  14173. char dstfile[200];
  14174. readArguments(argc,argv,srcfile,dstfile);
  14175. int q = find_pos_of_extension(srcfile);
  14176. int q2 = find_pos_of_extension(dstfile);
  14177. if(!fileExist(srcfile))
  14178. {
  14179. printf("Error: file <%s> does not exist.\n",srcfile);
  14180. exit(0);
  14181. }
  14182. if(mode==MODE_UNCOMPRESS)
  14183. {
  14184. printf("Decompressing .pkm/.ktx file ...\n");
  14185. uint8* alphaimg=NULL, *img;
  14186. int w, h;
  14187. uncompressFile(srcfile,img,alphaimg,w,h);
  14188. writeOutputFile(dstfile,img,alphaimg,w,h);
  14189. }
  14190. else if(mode==MODE_PSNR)
  14191. {
  14192. calculatePSNRTwoFiles(srcfile,dstfile);
  14193. }
  14194. else
  14195. {
  14196. compressFile(srcfile, dstfile);
  14197. }
  14198. }
  14199. else
  14200. {
  14201. printf("ETCPACK v2.74 For ETC and ETC2\n");
  14202. printf("Compresses and decompresses images using the Ericsson Texture Compression (ETC) version 1.0 and 2.0.\n\nUsage: etcpack srcfile dstfile\n\n");
  14203. printf(" -s {fast|slow} Compression speed. Slow = exhaustive \n");
  14204. printf(" search for optimal quality\n");
  14205. printf(" (default: fast)\n");
  14206. printf(" -e {perceptual|nonperceptual} Error metric: Perceptual (nicest) or \n");
  14207. printf(" nonperceptual (highest PSNR)\n");
  14208. printf(" (default: perceptual)\n");
  14209. printf(" -c {etc1|etc2} Codec: etc1 (most compatible) or \n");
  14210. printf(" etc2 (highest quality)\n");
  14211. printf(" (default: etc2)\n");
  14212. printf(" -f {R|R_signed|RG|RG_signed| Format: one, two, three or four \n");
  14213. printf(" RGB|RGBA1|RGBA8| channels, and 1 or 8 bits for alpha\n");
  14214. printf(" sRGB|sRGBA1|sRGBA8|} RGB or sRGB.\n");
  14215. printf(" (1 equals punchthrough)\n");
  14216. printf(" (default: RGB)\n");
  14217. printf(" -v {on|off} Detailed progress info. (default on)\n");
  14218. printf(" \n");
  14219. printf("Examples: \n");
  14220. printf(" etcpack img.ppm img.pkm Compresses img.ppm to img.pkm in\n");
  14221. printf(" ETC2 RGB format\n");
  14222. printf(" etcpack img.ppm img.ktx Compresses img.ppm to img.ktx in\n");
  14223. printf(" ETC2 RGB format\n");
  14224. printf(" etcpack img.pkm img_copy.ppm Decompresses img.pkm to img_copy.ppm\n");
  14225. printf(" etcpack -s slow img.ppm img.pkm Compress using the slow mode.\n");
  14226. printf(" etcpack -p orig.ppm copy.ppm Calculate PSNR between orig and copy\n");
  14227. printf(" etcpack -f RGBA8 img.tga img.pkm Compresses img.tga to img.pkm, using \n");
  14228. printf(" etc2 + alpha.\n");
  14229. printf(" etcpack -f RG img.ppm img.pkm Compresses red and green channels of\n");
  14230. printf(" img.ppm\n");
  14231. }
  14232. return 0;
  14233. }