basisu_enc.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  1. // basisu_enc.cpp
  2. // Copyright (C) 2019-2021 Binomial LLC. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "basisu_enc.h"
  16. #include "basisu_resampler.h"
  17. #include "basisu_resampler_filters.h"
  18. #include "basisu_etc.h"
  19. #include "../transcoder/basisu_transcoder.h"
  20. #include "basisu_bc7enc.h"
  21. #include "jpgd.h"
  22. #include "pvpngreader.h"
  23. #include "basisu_opencl.h"
  24. #include <vector>
  25. #define MINIZ_HEADER_FILE_ONLY
  26. #define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
  27. #include "basisu_miniz.h"
  28. #if defined(_WIN32)
  29. // For QueryPerformanceCounter/QueryPerformanceFrequency
  30. #define WIN32_LEAN_AND_MEAN
  31. #include <windows.h>
  32. #endif
  33. namespace basisu
  34. {
  35. uint64_t interval_timer::g_init_ticks, interval_timer::g_freq;
  36. double interval_timer::g_timer_freq;
  37. #if BASISU_SUPPORT_SSE
  38. bool g_cpu_supports_sse41;
  39. #endif
  40. uint8_t g_hamming_dist[256] =
  41. {
  42. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  43. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  44. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  45. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  46. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  47. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  48. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  49. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  50. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  51. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  52. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  53. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  54. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  55. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  56. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  57. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
  58. };
  59. // This is a Public Domain 8x8 font from here:
  60. // https://github.com/dhepper/font8x8/blob/master/font8x8_basic.h
  61. const uint8_t g_debug_font8x8_basic[127 - 32 + 1][8] =
  62. {
  63. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0020 ( )
  64. { 0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00}, // U+0021 (!)
  65. { 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0022 (")
  66. { 0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36, 0x00}, // U+0023 (#)
  67. { 0x0C, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x0C, 0x00}, // U+0024 ($)
  68. { 0x00, 0x63, 0x33, 0x18, 0x0C, 0x66, 0x63, 0x00}, // U+0025 (%)
  69. { 0x1C, 0x36, 0x1C, 0x6E, 0x3B, 0x33, 0x6E, 0x00}, // U+0026 (&)
  70. { 0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0027 (')
  71. { 0x18, 0x0C, 0x06, 0x06, 0x06, 0x0C, 0x18, 0x00}, // U+0028 (()
  72. { 0x06, 0x0C, 0x18, 0x18, 0x18, 0x0C, 0x06, 0x00}, // U+0029 ())
  73. { 0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00}, // U+002A (*)
  74. { 0x00, 0x0C, 0x0C, 0x3F, 0x0C, 0x0C, 0x00, 0x00}, // U+002B (+)
  75. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x06}, // U+002C (,)
  76. { 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00}, // U+002D (-)
  77. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x00}, // U+002E (.)
  78. { 0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00}, // U+002F (/)
  79. { 0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00}, // U+0030 (0)
  80. { 0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00}, // U+0031 (1)
  81. { 0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00}, // U+0032 (2)
  82. { 0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00}, // U+0033 (3)
  83. { 0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00}, // U+0034 (4)
  84. { 0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00}, // U+0035 (5)
  85. { 0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00}, // U+0036 (6)
  86. { 0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00}, // U+0037 (7)
  87. { 0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00}, // U+0038 (8)
  88. { 0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00}, // U+0039 (9)
  89. { 0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x00}, // U+003A (:)
  90. { 0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x06}, // U+003B (;)
  91. { 0x18, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x18, 0x00}, // U+003C (<)
  92. { 0x00, 0x00, 0x3F, 0x00, 0x00, 0x3F, 0x00, 0x00}, // U+003D (=)
  93. { 0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06, 0x00}, // U+003E (>)
  94. { 0x1E, 0x33, 0x30, 0x18, 0x0C, 0x00, 0x0C, 0x00}, // U+003F (?)
  95. { 0x3E, 0x63, 0x7B, 0x7B, 0x7B, 0x03, 0x1E, 0x00}, // U+0040 (@)
  96. { 0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00}, // U+0041 (A)
  97. { 0x3F, 0x66, 0x66, 0x3E, 0x66, 0x66, 0x3F, 0x00}, // U+0042 (B)
  98. { 0x3C, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3C, 0x00}, // U+0043 (C)
  99. { 0x1F, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1F, 0x00}, // U+0044 (D)
  100. { 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x46, 0x7F, 0x00}, // U+0045 (E)
  101. { 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x06, 0x0F, 0x00}, // U+0046 (F)
  102. { 0x3C, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7C, 0x00}, // U+0047 (G)
  103. { 0x33, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x33, 0x00}, // U+0048 (H)
  104. { 0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0049 (I)
  105. { 0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E, 0x00}, // U+004A (J)
  106. { 0x67, 0x66, 0x36, 0x1E, 0x36, 0x66, 0x67, 0x00}, // U+004B (K)
  107. { 0x0F, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7F, 0x00}, // U+004C (L)
  108. { 0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, 0x00}, // U+004D (M)
  109. { 0x63, 0x67, 0x6F, 0x7B, 0x73, 0x63, 0x63, 0x00}, // U+004E (N)
  110. { 0x1C, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00}, // U+004F (O)
  111. { 0x3F, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x0F, 0x00}, // U+0050 (P)
  112. { 0x1E, 0x33, 0x33, 0x33, 0x3B, 0x1E, 0x38, 0x00}, // U+0051 (Q)
  113. { 0x3F, 0x66, 0x66, 0x3E, 0x36, 0x66, 0x67, 0x00}, // U+0052 (R)
  114. { 0x1E, 0x33, 0x07, 0x0E, 0x38, 0x33, 0x1E, 0x00}, // U+0053 (S)
  115. { 0x3F, 0x2D, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0054 (T)
  116. { 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3F, 0x00}, // U+0055 (U)
  117. { 0x33, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00}, // U+0056 (V)
  118. { 0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00}, // U+0057 (W)
  119. { 0x63, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00}, // U+0058 (X)
  120. { 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x0C, 0x1E, 0x00}, // U+0059 (Y)
  121. { 0x7F, 0x63, 0x31, 0x18, 0x4C, 0x66, 0x7F, 0x00}, // U+005A (Z)
  122. { 0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1E, 0x00}, // U+005B ([)
  123. { 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00}, // U+005C (\)
  124. { 0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00}, // U+005D (])
  125. { 0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00}, // U+005E (^)
  126. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}, // U+005F (_)
  127. { 0x0C, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0060 (`)
  128. { 0x00, 0x00, 0x1E, 0x30, 0x3E, 0x33, 0x6E, 0x00}, // U+0061 (a)
  129. { 0x07, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3B, 0x00}, // U+0062 (b)
  130. { 0x00, 0x00, 0x1E, 0x33, 0x03, 0x33, 0x1E, 0x00}, // U+0063 (c)
  131. { 0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6E, 0x00}, // U+0064 (d)
  132. { 0x00, 0x00, 0x1E, 0x33, 0x3f, 0x03, 0x1E, 0x00}, // U+0065 (e)
  133. { 0x1C, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0F, 0x00}, // U+0066 (f)
  134. { 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x1F}, // U+0067 (g)
  135. { 0x07, 0x06, 0x36, 0x6E, 0x66, 0x66, 0x67, 0x00}, // U+0068 (h)
  136. { 0x0C, 0x00, 0x0E, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0069 (i)
  137. { 0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E}, // U+006A (j)
  138. { 0x07, 0x06, 0x66, 0x36, 0x1E, 0x36, 0x67, 0x00}, // U+006B (k)
  139. { 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+006C (l)
  140. { 0x00, 0x00, 0x33, 0x7F, 0x7F, 0x6B, 0x63, 0x00}, // U+006D (m)
  141. { 0x00, 0x00, 0x1F, 0x33, 0x33, 0x33, 0x33, 0x00}, // U+006E (n)
  142. { 0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00}, // U+006F (o)
  143. { 0x00, 0x00, 0x3B, 0x66, 0x66, 0x3E, 0x06, 0x0F}, // U+0070 (p)
  144. { 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x78}, // U+0071 (q)
  145. { 0x00, 0x00, 0x3B, 0x6E, 0x66, 0x06, 0x0F, 0x00}, // U+0072 (r)
  146. { 0x00, 0x00, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x00}, // U+0073 (s)
  147. { 0x08, 0x0C, 0x3E, 0x0C, 0x0C, 0x2C, 0x18, 0x00}, // U+0074 (t)
  148. { 0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6E, 0x00}, // U+0075 (u)
  149. { 0x00, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00}, // U+0076 (v)
  150. { 0x00, 0x00, 0x63, 0x6B, 0x7F, 0x7F, 0x36, 0x00}, // U+0077 (w)
  151. { 0x00, 0x00, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x00}, // U+0078 (x)
  152. { 0x00, 0x00, 0x33, 0x33, 0x33, 0x3E, 0x30, 0x1F}, // U+0079 (y)
  153. { 0x00, 0x00, 0x3F, 0x19, 0x0C, 0x26, 0x3F, 0x00}, // U+007A (z)
  154. { 0x38, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x38, 0x00}, // U+007B ({)
  155. { 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00}, // U+007C (|)
  156. { 0x07, 0x0C, 0x0C, 0x38, 0x0C, 0x0C, 0x07, 0x00}, // U+007D (})
  157. { 0x6E, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+007E (~)
  158. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // U+007F
  159. };
  160. bool g_library_initialized;
  161. std::mutex g_encoder_init_mutex;
  162. // Encoder library initialization (just call once at startup)
  163. void basisu_encoder_init(bool use_opencl, bool opencl_force_serialization)
  164. {
  165. std::lock_guard<std::mutex> lock(g_encoder_init_mutex);
  166. if (g_library_initialized)
  167. return;
  168. detect_sse41();
  169. basist::basisu_transcoder_init();
  170. pack_etc1_solid_color_init();
  171. //uastc_init();
  172. bc7enc_compress_block_init(); // must be after uastc_init()
  173. // Don't bother initializing the OpenCL module at all if it's been completely disabled.
  174. if (use_opencl)
  175. {
  176. opencl_init(opencl_force_serialization);
  177. }
  178. g_library_initialized = true;
  179. }
  180. void basisu_encoder_deinit()
  181. {
  182. opencl_deinit();
  183. g_library_initialized = false;
  184. }
  185. void error_vprintf(const char* pFmt, va_list args)
  186. {
  187. char buf[8192];
  188. #ifdef _WIN32
  189. vsprintf_s(buf, sizeof(buf), pFmt, args);
  190. #else
  191. vsnprintf(buf, sizeof(buf), pFmt, args);
  192. #endif
  193. fprintf(stderr, "ERROR: %s", buf);
  194. }
  195. void error_printf(const char *pFmt, ...)
  196. {
  197. va_list args;
  198. va_start(args, pFmt);
  199. error_vprintf(pFmt, args);
  200. va_end(args);
  201. }
  202. #if defined(_WIN32)
  203. inline void query_counter(timer_ticks* pTicks)
  204. {
  205. QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(pTicks));
  206. }
  207. inline void query_counter_frequency(timer_ticks* pTicks)
  208. {
  209. QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(pTicks));
  210. }
  211. #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  212. #include <sys/time.h>
  213. inline void query_counter(timer_ticks* pTicks)
  214. {
  215. struct timeval cur_time;
  216. gettimeofday(&cur_time, NULL);
  217. *pTicks = static_cast<unsigned long long>(cur_time.tv_sec) * 1000000ULL + static_cast<unsigned long long>(cur_time.tv_usec);
  218. }
  219. inline void query_counter_frequency(timer_ticks* pTicks)
  220. {
  221. *pTicks = 1000000;
  222. }
  223. #elif defined(__GNUC__)
  224. #include <sys/timex.h>
  225. inline void query_counter(timer_ticks* pTicks)
  226. {
  227. struct timeval cur_time;
  228. gettimeofday(&cur_time, NULL);
  229. *pTicks = static_cast<unsigned long long>(cur_time.tv_sec) * 1000000ULL + static_cast<unsigned long long>(cur_time.tv_usec);
  230. }
  231. inline void query_counter_frequency(timer_ticks* pTicks)
  232. {
  233. *pTicks = 1000000;
  234. }
  235. #else
  236. #error TODO
  237. #endif
  238. interval_timer::interval_timer() : m_start_time(0), m_stop_time(0), m_started(false), m_stopped(false)
  239. {
  240. if (!g_timer_freq)
  241. init();
  242. }
  243. void interval_timer::start()
  244. {
  245. query_counter(&m_start_time);
  246. m_started = true;
  247. m_stopped = false;
  248. }
  249. void interval_timer::stop()
  250. {
  251. assert(m_started);
  252. query_counter(&m_stop_time);
  253. m_stopped = true;
  254. }
  255. double interval_timer::get_elapsed_secs() const
  256. {
  257. assert(m_started);
  258. if (!m_started)
  259. return 0;
  260. timer_ticks stop_time = m_stop_time;
  261. if (!m_stopped)
  262. query_counter(&stop_time);
  263. timer_ticks delta = stop_time - m_start_time;
  264. return delta * g_timer_freq;
  265. }
  266. void interval_timer::init()
  267. {
  268. if (!g_timer_freq)
  269. {
  270. query_counter_frequency(&g_freq);
  271. g_timer_freq = 1.0f / g_freq;
  272. query_counter(&g_init_ticks);
  273. }
  274. }
  275. timer_ticks interval_timer::get_ticks()
  276. {
  277. if (!g_timer_freq)
  278. init();
  279. timer_ticks ticks;
  280. query_counter(&ticks);
  281. return ticks - g_init_ticks;
  282. }
  283. double interval_timer::ticks_to_secs(timer_ticks ticks)
  284. {
  285. if (!g_timer_freq)
  286. init();
  287. return ticks * g_timer_freq;
  288. }
  289. const uint32_t MAX_32BIT_ALLOC_SIZE = 250000000;
  290. bool load_tga(const char* pFilename, image& img)
  291. {
  292. int w = 0, h = 0, n_chans = 0;
  293. uint8_t* pImage_data = read_tga(pFilename, w, h, n_chans);
  294. if ((!pImage_data) || (!w) || (!h) || ((n_chans != 3) && (n_chans != 4)))
  295. {
  296. error_printf("Failed loading .TGA image \"%s\"!\n", pFilename);
  297. if (pImage_data)
  298. free(pImage_data);
  299. return false;
  300. }
  301. if (sizeof(void *) == sizeof(uint32_t))
  302. {
  303. if ((w * h * n_chans) > MAX_32BIT_ALLOC_SIZE)
  304. {
  305. error_printf("Image \"%s\" is too large (%ux%u) to process in a 32-bit build!\n", pFilename, w, h);
  306. if (pImage_data)
  307. free(pImage_data);
  308. return false;
  309. }
  310. }
  311. img.resize(w, h);
  312. const uint8_t *pSrc = pImage_data;
  313. for (int y = 0; y < h; y++)
  314. {
  315. color_rgba *pDst = &img(0, y);
  316. for (int x = 0; x < w; x++)
  317. {
  318. pDst->r = pSrc[0];
  319. pDst->g = pSrc[1];
  320. pDst->b = pSrc[2];
  321. pDst->a = (n_chans == 3) ? 255 : pSrc[3];
  322. pSrc += n_chans;
  323. ++pDst;
  324. }
  325. }
  326. free(pImage_data);
  327. return true;
  328. }
  329. bool load_png(const uint8_t *pBuf, size_t buf_size, image &img, const char *pFilename)
  330. {
  331. interval_timer tm;
  332. tm.start();
  333. if (!buf_size)
  334. return false;
  335. uint32_t width = 0, height = 0, num_chans = 0;
  336. void* pImage = pv_png::load_png(pBuf, buf_size, 4, width, height, num_chans);
  337. if (!pBuf)
  338. {
  339. error_printf("pv_png::load_png failed while loading image \"%s\"\n", pFilename);
  340. return false;
  341. }
  342. img.grant_ownership(reinterpret_cast<color_rgba*>(pImage), width, height);
  343. //debug_printf("Total load_png() time: %3.3f secs\n", tm.get_elapsed_secs());
  344. return true;
  345. }
  346. bool load_png(const char* pFilename, image& img)
  347. {
  348. uint8_vec buffer;
  349. if (!read_file_to_vec(pFilename, buffer))
  350. {
  351. error_printf("load_png: Failed reading file \"%s\"!\n", pFilename);
  352. return false;
  353. }
  354. return load_png(buffer.data(), buffer.size(), img, pFilename);
  355. }
  356. bool load_jpg(const char *pFilename, image& img)
  357. {
  358. int width = 0, height = 0, actual_comps = 0;
  359. uint8_t *pImage_data = jpgd::decompress_jpeg_image_from_file(pFilename, &width, &height, &actual_comps, 4, jpgd::jpeg_decoder::cFlagLinearChromaFiltering);
  360. if (!pImage_data)
  361. return false;
  362. img.init(pImage_data, width, height, 4);
  363. free(pImage_data);
  364. return true;
  365. }
  366. bool load_image(const char* pFilename, image& img)
  367. {
  368. std::string ext(string_get_extension(std::string(pFilename)));
  369. if (ext.length() == 0)
  370. return false;
  371. const char *pExt = ext.c_str();
  372. if (strcasecmp(pExt, "png") == 0)
  373. return load_png(pFilename, img);
  374. if (strcasecmp(pExt, "tga") == 0)
  375. return load_tga(pFilename, img);
  376. if ( (strcasecmp(pExt, "jpg") == 0) || (strcasecmp(pExt, "jfif") == 0) || (strcasecmp(pExt, "jpeg") == 0) )
  377. return load_jpg(pFilename, img);
  378. return false;
  379. }
  380. bool save_png(const char* pFilename, const image &img, uint32_t image_save_flags, uint32_t grayscale_comp)
  381. {
  382. if (!img.get_total_pixels())
  383. return false;
  384. void* pPNG_data = nullptr;
  385. size_t PNG_data_size = 0;
  386. if (image_save_flags & cImageSaveGrayscale)
  387. {
  388. uint8_vec g_pixels(img.get_total_pixels());
  389. uint8_t* pDst = &g_pixels[0];
  390. for (uint32_t y = 0; y < img.get_height(); y++)
  391. for (uint32_t x = 0; x < img.get_width(); x++)
  392. *pDst++ = img(x, y)[grayscale_comp];
  393. pPNG_data = buminiz::tdefl_write_image_to_png_file_in_memory_ex(g_pixels.data(), img.get_width(), img.get_height(), 1, &PNG_data_size, 1, false);
  394. }
  395. else
  396. {
  397. bool has_alpha = false;
  398. if ((image_save_flags & cImageSaveIgnoreAlpha) == 0)
  399. has_alpha = img.has_alpha();
  400. if (!has_alpha)
  401. {
  402. uint8_vec rgb_pixels(img.get_total_pixels() * 3);
  403. uint8_t* pDst = &rgb_pixels[0];
  404. for (uint32_t y = 0; y < img.get_height(); y++)
  405. {
  406. const color_rgba* pSrc = &img(0, y);
  407. for (uint32_t x = 0; x < img.get_width(); x++)
  408. {
  409. pDst[0] = pSrc->r;
  410. pDst[1] = pSrc->g;
  411. pDst[2] = pSrc->b;
  412. pSrc++;
  413. pDst += 3;
  414. }
  415. }
  416. pPNG_data = buminiz::tdefl_write_image_to_png_file_in_memory_ex(rgb_pixels.data(), img.get_width(), img.get_height(), 3, &PNG_data_size, 1, false);
  417. }
  418. else
  419. {
  420. pPNG_data = buminiz::tdefl_write_image_to_png_file_in_memory_ex(img.get_ptr(), img.get_width(), img.get_height(), 4, &PNG_data_size, 1, false);
  421. }
  422. }
  423. if (!pPNG_data)
  424. return false;
  425. bool status = write_data_to_file(pFilename, pPNG_data, PNG_data_size);
  426. if (!status)
  427. {
  428. error_printf("save_png: Failed writing to filename \"%s\"!\n", pFilename);
  429. }
  430. free(pPNG_data);
  431. return status;
  432. }
  433. bool read_file_to_vec(const char* pFilename, uint8_vec& data)
  434. {
  435. FILE* pFile = nullptr;
  436. #ifdef _WIN32
  437. fopen_s(&pFile, pFilename, "rb");
  438. #else
  439. pFile = fopen(pFilename, "rb");
  440. #endif
  441. if (!pFile)
  442. return false;
  443. fseek(pFile, 0, SEEK_END);
  444. #ifdef _WIN32
  445. int64_t filesize = _ftelli64(pFile);
  446. #else
  447. int64_t filesize = ftello(pFile);
  448. #endif
  449. if (filesize < 0)
  450. {
  451. fclose(pFile);
  452. return false;
  453. }
  454. fseek(pFile, 0, SEEK_SET);
  455. if (sizeof(size_t) == sizeof(uint32_t))
  456. {
  457. if (filesize > 0x70000000)
  458. {
  459. // File might be too big to load safely in one alloc
  460. fclose(pFile);
  461. return false;
  462. }
  463. }
  464. if (!data.try_resize((size_t)filesize))
  465. {
  466. fclose(pFile);
  467. return false;
  468. }
  469. if (filesize)
  470. {
  471. if (fread(&data[0], 1, (size_t)filesize, pFile) != (size_t)filesize)
  472. {
  473. fclose(pFile);
  474. return false;
  475. }
  476. }
  477. fclose(pFile);
  478. return true;
  479. }
  480. bool write_data_to_file(const char* pFilename, const void* pData, size_t len)
  481. {
  482. FILE* pFile = nullptr;
  483. #ifdef _WIN32
  484. fopen_s(&pFile, pFilename, "wb");
  485. #else
  486. pFile = fopen(pFilename, "wb");
  487. #endif
  488. if (!pFile)
  489. return false;
  490. if (len)
  491. {
  492. if (fwrite(pData, 1, len, pFile) != len)
  493. {
  494. fclose(pFile);
  495. return false;
  496. }
  497. }
  498. return fclose(pFile) != EOF;
  499. }
  500. float linear_to_srgb(float l)
  501. {
  502. assert(l >= 0.0f && l <= 1.0f);
  503. if (l < .0031308f)
  504. return saturate(l * 12.92f);
  505. else
  506. return saturate(1.055f * powf(l, 1.0f/2.4f) - .055f);
  507. }
  508. float srgb_to_linear(float s)
  509. {
  510. assert(s >= 0.0f && s <= 1.0f);
  511. if (s < .04045f)
  512. return saturate(s * (1.0f/12.92f));
  513. else
  514. return saturate(powf((s + .055f) * (1.0f/1.055f), 2.4f));
  515. }
  516. bool image_resample(const image &src, image &dst, bool srgb,
  517. const char *pFilter, float filter_scale,
  518. bool wrapping,
  519. uint32_t first_comp, uint32_t num_comps)
  520. {
  521. assert((first_comp + num_comps) <= 4);
  522. const int cMaxComps = 4;
  523. const uint32_t src_w = src.get_width(), src_h = src.get_height();
  524. const uint32_t dst_w = dst.get_width(), dst_h = dst.get_height();
  525. if (maximum(src_w, src_h) > BASISU_RESAMPLER_MAX_DIMENSION)
  526. {
  527. printf("Image is too large!\n");
  528. return false;
  529. }
  530. if (!src_w || !src_h || !dst_w || !dst_h)
  531. return false;
  532. if ((num_comps < 1) || (num_comps > cMaxComps))
  533. return false;
  534. if ((minimum(dst_w, dst_h) < 1) || (maximum(dst_w, dst_h) > BASISU_RESAMPLER_MAX_DIMENSION))
  535. {
  536. printf("Image is too large!\n");
  537. return false;
  538. }
  539. if ((src_w == dst_w) && (src_h == dst_h))
  540. {
  541. dst = src;
  542. return true;
  543. }
  544. float srgb_to_linear_table[256];
  545. if (srgb)
  546. {
  547. for (int i = 0; i < 256; ++i)
  548. srgb_to_linear_table[i] = srgb_to_linear((float)i * (1.0f/255.0f));
  549. }
  550. const int LINEAR_TO_SRGB_TABLE_SIZE = 8192;
  551. uint8_t linear_to_srgb_table[LINEAR_TO_SRGB_TABLE_SIZE];
  552. if (srgb)
  553. {
  554. for (int i = 0; i < LINEAR_TO_SRGB_TABLE_SIZE; ++i)
  555. linear_to_srgb_table[i] = (uint8_t)clamp<int>((int)(255.0f * linear_to_srgb((float)i * (1.0f / (LINEAR_TO_SRGB_TABLE_SIZE - 1))) + .5f), 0, 255);
  556. }
  557. std::vector<float> samples[cMaxComps];
  558. Resampler *resamplers[cMaxComps];
  559. resamplers[0] = new Resampler(src_w, src_h, dst_w, dst_h,
  560. wrapping ? Resampler::BOUNDARY_WRAP : Resampler::BOUNDARY_CLAMP, 0.0f, 1.0f,
  561. pFilter, nullptr, nullptr, filter_scale, filter_scale, 0, 0);
  562. samples[0].resize(src_w);
  563. for (uint32_t i = 1; i < num_comps; ++i)
  564. {
  565. resamplers[i] = new Resampler(src_w, src_h, dst_w, dst_h,
  566. wrapping ? Resampler::BOUNDARY_WRAP : Resampler::BOUNDARY_CLAMP, 0.0f, 1.0f,
  567. pFilter, resamplers[0]->get_clist_x(), resamplers[0]->get_clist_y(), filter_scale, filter_scale, 0, 0);
  568. samples[i].resize(src_w);
  569. }
  570. uint32_t dst_y = 0;
  571. for (uint32_t src_y = 0; src_y < src_h; ++src_y)
  572. {
  573. const color_rgba *pSrc = &src(0, src_y);
  574. // Put source lines into resampler(s)
  575. for (uint32_t x = 0; x < src_w; ++x)
  576. {
  577. for (uint32_t c = 0; c < num_comps; ++c)
  578. {
  579. const uint32_t comp_index = first_comp + c;
  580. const uint32_t v = (*pSrc)[comp_index];
  581. if (!srgb || (comp_index == 3))
  582. samples[c][x] = v * (1.0f / 255.0f);
  583. else
  584. samples[c][x] = srgb_to_linear_table[v];
  585. }
  586. pSrc++;
  587. }
  588. for (uint32_t c = 0; c < num_comps; ++c)
  589. {
  590. if (!resamplers[c]->put_line(&samples[c][0]))
  591. {
  592. for (uint32_t i = 0; i < num_comps; i++)
  593. delete resamplers[i];
  594. return false;
  595. }
  596. }
  597. // Now retrieve any output lines
  598. for (;;)
  599. {
  600. uint32_t c;
  601. for (c = 0; c < num_comps; ++c)
  602. {
  603. const uint32_t comp_index = first_comp + c;
  604. const float *pOutput_samples = resamplers[c]->get_line();
  605. if (!pOutput_samples)
  606. break;
  607. const bool linear_flag = !srgb || (comp_index == 3);
  608. color_rgba *pDst = &dst(0, dst_y);
  609. for (uint32_t x = 0; x < dst_w; x++)
  610. {
  611. // TODO: Add dithering
  612. if (linear_flag)
  613. {
  614. int j = (int)(255.0f * pOutput_samples[x] + .5f);
  615. (*pDst)[comp_index] = (uint8_t)clamp<int>(j, 0, 255);
  616. }
  617. else
  618. {
  619. int j = (int)((LINEAR_TO_SRGB_TABLE_SIZE - 1) * pOutput_samples[x] + .5f);
  620. (*pDst)[comp_index] = linear_to_srgb_table[clamp<int>(j, 0, LINEAR_TO_SRGB_TABLE_SIZE - 1)];
  621. }
  622. pDst++;
  623. }
  624. }
  625. if (c < num_comps)
  626. break;
  627. ++dst_y;
  628. }
  629. }
  630. for (uint32_t i = 0; i < num_comps; ++i)
  631. delete resamplers[i];
  632. return true;
  633. }
  634. void canonical_huffman_calculate_minimum_redundancy(sym_freq *A, int num_syms)
  635. {
  636. // See the paper "In-Place Calculation of Minimum Redundancy Codes" by Moffat and Katajainen
  637. if (!num_syms)
  638. return;
  639. if (1 == num_syms)
  640. {
  641. A[0].m_key = 1;
  642. return;
  643. }
  644. A[0].m_key += A[1].m_key;
  645. int s = 2, r = 0, next;
  646. for (next = 1; next < (num_syms - 1); ++next)
  647. {
  648. if ((s >= num_syms) || (A[r].m_key < A[s].m_key))
  649. {
  650. A[next].m_key = A[r].m_key;
  651. A[r].m_key = next;
  652. ++r;
  653. }
  654. else
  655. {
  656. A[next].m_key = A[s].m_key;
  657. ++s;
  658. }
  659. if ((s >= num_syms) || ((r < next) && A[r].m_key < A[s].m_key))
  660. {
  661. A[next].m_key = A[next].m_key + A[r].m_key;
  662. A[r].m_key = next;
  663. ++r;
  664. }
  665. else
  666. {
  667. A[next].m_key = A[next].m_key + A[s].m_key;
  668. ++s;
  669. }
  670. }
  671. A[num_syms - 2].m_key = 0;
  672. for (next = num_syms - 3; next >= 0; --next)
  673. {
  674. A[next].m_key = 1 + A[A[next].m_key].m_key;
  675. }
  676. int num_avail = 1, num_used = 0, depth = 0;
  677. r = num_syms - 2;
  678. next = num_syms - 1;
  679. while (num_avail > 0)
  680. {
  681. for ( ; (r >= 0) && ((int)A[r].m_key == depth); ++num_used, --r )
  682. ;
  683. for ( ; num_avail > num_used; --next, --num_avail)
  684. A[next].m_key = depth;
  685. num_avail = 2 * num_used;
  686. num_used = 0;
  687. ++depth;
  688. }
  689. }
  690. void canonical_huffman_enforce_max_code_size(int *pNum_codes, int code_list_len, int max_code_size)
  691. {
  692. int i;
  693. uint32_t total = 0;
  694. if (code_list_len <= 1)
  695. return;
  696. for (i = max_code_size + 1; i <= cHuffmanMaxSupportedInternalCodeSize; i++)
  697. pNum_codes[max_code_size] += pNum_codes[i];
  698. for (i = max_code_size; i > 0; i--)
  699. total += (((uint32_t)pNum_codes[i]) << (max_code_size - i));
  700. while (total != (1UL << max_code_size))
  701. {
  702. pNum_codes[max_code_size]--;
  703. for (i = max_code_size - 1; i > 0; i--)
  704. {
  705. if (pNum_codes[i])
  706. {
  707. pNum_codes[i]--;
  708. pNum_codes[i + 1] += 2;
  709. break;
  710. }
  711. }
  712. total--;
  713. }
  714. }
  715. sym_freq *canonical_huffman_radix_sort_syms(uint32_t num_syms, sym_freq *pSyms0, sym_freq *pSyms1)
  716. {
  717. uint32_t total_passes = 2, pass_shift, pass, i, hist[256 * 2];
  718. sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1;
  719. clear_obj(hist);
  720. for (i = 0; i < num_syms; i++)
  721. {
  722. uint32_t freq = pSyms0[i].m_key;
  723. // We scale all input frequencies to 16-bits.
  724. assert(freq <= UINT16_MAX);
  725. hist[freq & 0xFF]++;
  726. hist[256 + ((freq >> 8) & 0xFF)]++;
  727. }
  728. while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256]))
  729. total_passes--;
  730. for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8)
  731. {
  732. const uint32_t *pHist = &hist[pass << 8];
  733. uint32_t offsets[256], cur_ofs = 0;
  734. for (i = 0; i < 256; i++)
  735. {
  736. offsets[i] = cur_ofs;
  737. cur_ofs += pHist[i];
  738. }
  739. for (i = 0; i < num_syms; i++)
  740. pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];
  741. sym_freq *t = pCur_syms;
  742. pCur_syms = pNew_syms;
  743. pNew_syms = t;
  744. }
  745. return pCur_syms;
  746. }
  747. bool huffman_encoding_table::init(uint32_t num_syms, const uint16_t *pFreq, uint32_t max_code_size)
  748. {
  749. if (max_code_size > cHuffmanMaxSupportedCodeSize)
  750. return false;
  751. if ((!num_syms) || (num_syms > cHuffmanMaxSyms))
  752. return false;
  753. uint32_t total_used_syms = 0;
  754. for (uint32_t i = 0; i < num_syms; i++)
  755. if (pFreq[i])
  756. total_used_syms++;
  757. if (!total_used_syms)
  758. return false;
  759. std::vector<sym_freq> sym_freq0(total_used_syms), sym_freq1(total_used_syms);
  760. for (uint32_t i = 0, j = 0; i < num_syms; i++)
  761. {
  762. if (pFreq[i])
  763. {
  764. sym_freq0[j].m_key = pFreq[i];
  765. sym_freq0[j++].m_sym_index = static_cast<uint16_t>(i);
  766. }
  767. }
  768. sym_freq *pSym_freq = canonical_huffman_radix_sort_syms(total_used_syms, &sym_freq0[0], &sym_freq1[0]);
  769. canonical_huffman_calculate_minimum_redundancy(pSym_freq, total_used_syms);
  770. int num_codes[cHuffmanMaxSupportedInternalCodeSize + 1];
  771. clear_obj(num_codes);
  772. for (uint32_t i = 0; i < total_used_syms; i++)
  773. {
  774. if (pSym_freq[i].m_key > cHuffmanMaxSupportedInternalCodeSize)
  775. return false;
  776. num_codes[pSym_freq[i].m_key]++;
  777. }
  778. canonical_huffman_enforce_max_code_size(num_codes, total_used_syms, max_code_size);
  779. m_code_sizes.resize(0);
  780. m_code_sizes.resize(num_syms);
  781. m_codes.resize(0);
  782. m_codes.resize(num_syms);
  783. for (uint32_t i = 1, j = total_used_syms; i <= max_code_size; i++)
  784. for (uint32_t l = num_codes[i]; l > 0; l--)
  785. m_code_sizes[pSym_freq[--j].m_sym_index] = static_cast<uint8_t>(i);
  786. uint32_t next_code[cHuffmanMaxSupportedInternalCodeSize + 1];
  787. next_code[1] = 0;
  788. for (uint32_t j = 0, i = 2; i <= max_code_size; i++)
  789. next_code[i] = j = ((j + num_codes[i - 1]) << 1);
  790. for (uint32_t i = 0; i < num_syms; i++)
  791. {
  792. uint32_t rev_code = 0, code, code_size;
  793. if ((code_size = m_code_sizes[i]) == 0)
  794. continue;
  795. if (code_size > cHuffmanMaxSupportedInternalCodeSize)
  796. return false;
  797. code = next_code[code_size]++;
  798. for (uint32_t l = code_size; l > 0; l--, code >>= 1)
  799. rev_code = (rev_code << 1) | (code & 1);
  800. m_codes[i] = static_cast<uint16_t>(rev_code);
  801. }
  802. return true;
  803. }
  804. bool huffman_encoding_table::init(uint32_t num_syms, const uint32_t *pSym_freq, uint32_t max_code_size)
  805. {
  806. if ((!num_syms) || (num_syms > cHuffmanMaxSyms))
  807. return false;
  808. uint16_vec sym_freq(num_syms);
  809. uint32_t max_freq = 0;
  810. for (uint32_t i = 0; i < num_syms; i++)
  811. max_freq = maximum(max_freq, pSym_freq[i]);
  812. if (max_freq < UINT16_MAX)
  813. {
  814. for (uint32_t i = 0; i < num_syms; i++)
  815. sym_freq[i] = static_cast<uint16_t>(pSym_freq[i]);
  816. }
  817. else
  818. {
  819. for (uint32_t i = 0; i < num_syms; i++)
  820. {
  821. if (pSym_freq[i])
  822. {
  823. uint32_t f = static_cast<uint32_t>((static_cast<uint64_t>(pSym_freq[i]) * 65534U + (max_freq >> 1)) / max_freq);
  824. sym_freq[i] = static_cast<uint16_t>(clamp<uint32_t>(f, 1, 65534));
  825. }
  826. }
  827. }
  828. return init(num_syms, &sym_freq[0], max_code_size);
  829. }
  830. void bitwise_coder::end_nonzero_run(uint16_vec &syms, uint32_t &run_size, uint32_t len)
  831. {
  832. if (run_size)
  833. {
  834. if (run_size < cHuffmanSmallRepeatSizeMin)
  835. {
  836. while (run_size--)
  837. syms.push_back(static_cast<uint16_t>(len));
  838. }
  839. else if (run_size <= cHuffmanSmallRepeatSizeMax)
  840. {
  841. syms.push_back(static_cast<uint16_t>(cHuffmanSmallRepeatCode | ((run_size - cHuffmanSmallRepeatSizeMin) << 6)));
  842. }
  843. else
  844. {
  845. assert((run_size >= cHuffmanBigRepeatSizeMin) && (run_size <= cHuffmanBigRepeatSizeMax));
  846. syms.push_back(static_cast<uint16_t>(cHuffmanBigRepeatCode | ((run_size - cHuffmanBigRepeatSizeMin) << 6)));
  847. }
  848. }
  849. run_size = 0;
  850. }
  851. void bitwise_coder::end_zero_run(uint16_vec &syms, uint32_t &run_size)
  852. {
  853. if (run_size)
  854. {
  855. if (run_size < cHuffmanSmallZeroRunSizeMin)
  856. {
  857. while (run_size--)
  858. syms.push_back(0);
  859. }
  860. else if (run_size <= cHuffmanSmallZeroRunSizeMax)
  861. {
  862. syms.push_back(static_cast<uint16_t>(cHuffmanSmallZeroRunCode | ((run_size - cHuffmanSmallZeroRunSizeMin) << 6)));
  863. }
  864. else
  865. {
  866. assert((run_size >= cHuffmanBigZeroRunSizeMin) && (run_size <= cHuffmanBigZeroRunSizeMax));
  867. syms.push_back(static_cast<uint16_t>(cHuffmanBigZeroRunCode | ((run_size - cHuffmanBigZeroRunSizeMin) << 6)));
  868. }
  869. }
  870. run_size = 0;
  871. }
  872. uint32_t bitwise_coder::emit_huffman_table(const huffman_encoding_table &tab)
  873. {
  874. const uint64_t start_bits = m_total_bits;
  875. const uint8_vec &code_sizes = tab.get_code_sizes();
  876. uint32_t total_used = tab.get_total_used_codes();
  877. put_bits(total_used, cHuffmanMaxSymsLog2);
  878. if (!total_used)
  879. return 0;
  880. uint16_vec syms;
  881. syms.reserve(total_used + 16);
  882. uint32_t prev_code_len = UINT_MAX, zero_run_size = 0, nonzero_run_size = 0;
  883. for (uint32_t i = 0; i <= total_used; ++i)
  884. {
  885. const uint32_t code_len = (i == total_used) ? 0xFF : code_sizes[i];
  886. assert((code_len == 0xFF) || (code_len <= 16));
  887. if (code_len)
  888. {
  889. end_zero_run(syms, zero_run_size);
  890. if (code_len != prev_code_len)
  891. {
  892. end_nonzero_run(syms, nonzero_run_size, prev_code_len);
  893. if (code_len != 0xFF)
  894. syms.push_back(static_cast<uint16_t>(code_len));
  895. }
  896. else if (++nonzero_run_size == cHuffmanBigRepeatSizeMax)
  897. end_nonzero_run(syms, nonzero_run_size, prev_code_len);
  898. }
  899. else
  900. {
  901. end_nonzero_run(syms, nonzero_run_size, prev_code_len);
  902. if (++zero_run_size == cHuffmanBigZeroRunSizeMax)
  903. end_zero_run(syms, zero_run_size);
  904. }
  905. prev_code_len = code_len;
  906. }
  907. histogram h(cHuffmanTotalCodelengthCodes);
  908. for (uint32_t i = 0; i < syms.size(); i++)
  909. h.inc(syms[i] & 63);
  910. huffman_encoding_table ct;
  911. if (!ct.init(h, 7))
  912. return 0;
  913. assert(cHuffmanTotalSortedCodelengthCodes == cHuffmanTotalCodelengthCodes);
  914. uint32_t total_codelength_codes;
  915. for (total_codelength_codes = cHuffmanTotalSortedCodelengthCodes; total_codelength_codes > 0; total_codelength_codes--)
  916. if (ct.get_code_sizes()[g_huffman_sorted_codelength_codes[total_codelength_codes - 1]])
  917. break;
  918. assert(total_codelength_codes);
  919. put_bits(total_codelength_codes, 5);
  920. for (uint32_t i = 0; i < total_codelength_codes; i++)
  921. put_bits(ct.get_code_sizes()[g_huffman_sorted_codelength_codes[i]], 3);
  922. for (uint32_t i = 0; i < syms.size(); ++i)
  923. {
  924. const uint32_t l = syms[i] & 63, e = syms[i] >> 6;
  925. put_code(l, ct);
  926. if (l == cHuffmanSmallZeroRunCode)
  927. put_bits(e, cHuffmanSmallZeroRunExtraBits);
  928. else if (l == cHuffmanBigZeroRunCode)
  929. put_bits(e, cHuffmanBigZeroRunExtraBits);
  930. else if (l == cHuffmanSmallRepeatCode)
  931. put_bits(e, cHuffmanSmallRepeatExtraBits);
  932. else if (l == cHuffmanBigRepeatCode)
  933. put_bits(e, cHuffmanBigRepeatExtraBits);
  934. }
  935. return (uint32_t)(m_total_bits - start_bits);
  936. }
  937. bool huffman_test(int rand_seed)
  938. {
  939. histogram h(19);
  940. // Feed in a fibonacci sequence to force large codesizes
  941. h[0] += 1; h[1] += 1; h[2] += 2; h[3] += 3;
  942. h[4] += 5; h[5] += 8; h[6] += 13; h[7] += 21;
  943. h[8] += 34; h[9] += 55; h[10] += 89; h[11] += 144;
  944. h[12] += 233; h[13] += 377; h[14] += 610; h[15] += 987;
  945. h[16] += 1597; h[17] += 2584; h[18] += 4181;
  946. huffman_encoding_table etab;
  947. etab.init(h, 16);
  948. {
  949. bitwise_coder c;
  950. c.init(1024);
  951. c.emit_huffman_table(etab);
  952. for (int i = 0; i < 19; i++)
  953. c.put_code(i, etab);
  954. c.flush();
  955. basist::bitwise_decoder d;
  956. d.init(&c.get_bytes()[0], static_cast<uint32_t>(c.get_bytes().size()));
  957. basist::huffman_decoding_table dtab;
  958. bool success = d.read_huffman_table(dtab);
  959. if (!success)
  960. {
  961. assert(0);
  962. printf("Failure 5\n");
  963. return false;
  964. }
  965. for (uint32_t i = 0; i < 19; i++)
  966. {
  967. uint32_t s = d.decode_huffman(dtab);
  968. if (s != i)
  969. {
  970. assert(0);
  971. printf("Failure 5\n");
  972. return false;
  973. }
  974. }
  975. }
  976. basisu::rand r;
  977. r.seed(rand_seed);
  978. for (int iter = 0; iter < 500000; iter++)
  979. {
  980. printf("%u\n", iter);
  981. uint32_t max_sym = r.irand(0, 8193);
  982. uint32_t num_codes = r.irand(1, 10000);
  983. uint_vec syms(num_codes);
  984. for (uint32_t i = 0; i < num_codes; i++)
  985. {
  986. if (r.bit())
  987. syms[i] = r.irand(0, max_sym);
  988. else
  989. {
  990. int s = (int)(r.gaussian((float)max_sym / 2, (float)maximum<int>(1, max_sym / 2)) + .5f);
  991. s = basisu::clamp<int>(s, 0, max_sym);
  992. syms[i] = s;
  993. }
  994. }
  995. histogram h1(max_sym + 1);
  996. for (uint32_t i = 0; i < num_codes; i++)
  997. h1[syms[i]]++;
  998. huffman_encoding_table etab2;
  999. if (!etab2.init(h1, 16))
  1000. {
  1001. assert(0);
  1002. printf("Failed 0\n");
  1003. return false;
  1004. }
  1005. bitwise_coder c;
  1006. c.init(1024);
  1007. c.emit_huffman_table(etab2);
  1008. for (uint32_t i = 0; i < num_codes; i++)
  1009. c.put_code(syms[i], etab2);
  1010. c.flush();
  1011. basist::bitwise_decoder d;
  1012. d.init(&c.get_bytes()[0], (uint32_t)c.get_bytes().size());
  1013. basist::huffman_decoding_table dtab;
  1014. bool success = d.read_huffman_table(dtab);
  1015. if (!success)
  1016. {
  1017. assert(0);
  1018. printf("Failed 2\n");
  1019. return false;
  1020. }
  1021. for (uint32_t i = 0; i < num_codes; i++)
  1022. {
  1023. uint32_t s = d.decode_huffman(dtab);
  1024. if (s != syms[i])
  1025. {
  1026. assert(0);
  1027. printf("Failed 4\n");
  1028. return false;
  1029. }
  1030. }
  1031. }
  1032. return true;
  1033. }
  1034. void palette_index_reorderer::init(uint32_t num_indices, const uint32_t *pIndices, uint32_t num_syms, pEntry_dist_func pDist_func, void *pCtx, float dist_func_weight)
  1035. {
  1036. assert((num_syms > 0) && (num_indices > 0));
  1037. assert((dist_func_weight >= 0.0f) && (dist_func_weight <= 1.0f));
  1038. clear();
  1039. m_remap_table.resize(num_syms);
  1040. m_entries_picked.reserve(num_syms);
  1041. m_total_count_to_picked.resize(num_syms);
  1042. if (num_indices <= 1)
  1043. return;
  1044. prepare_hist(num_syms, num_indices, pIndices);
  1045. find_initial(num_syms);
  1046. while (m_entries_to_do.size())
  1047. {
  1048. // Find the best entry to move into the picked list.
  1049. uint32_t best_entry;
  1050. double best_count;
  1051. find_next_entry(best_entry, best_count, pDist_func, pCtx, dist_func_weight);
  1052. // We now have chosen an entry to place in the picked list, now determine which side it goes on.
  1053. const uint32_t entry_to_move = m_entries_to_do[best_entry];
  1054. float side = pick_side(num_syms, entry_to_move, pDist_func, pCtx, dist_func_weight);
  1055. // Put entry_to_move either on the "left" or "right" side of the picked entries
  1056. if (side <= 0)
  1057. m_entries_picked.push_back(entry_to_move);
  1058. else
  1059. m_entries_picked.insert(m_entries_picked.begin(), entry_to_move);
  1060. // Erase best_entry from the todo list
  1061. m_entries_to_do.erase(m_entries_to_do.begin() + best_entry);
  1062. // We've just moved best_entry to the picked list, so now we need to update m_total_count_to_picked[] to factor the additional count to best_entry
  1063. for (uint32_t i = 0; i < m_entries_to_do.size(); i++)
  1064. m_total_count_to_picked[m_entries_to_do[i]] += get_hist(m_entries_to_do[i], entry_to_move, num_syms);
  1065. }
  1066. for (uint32_t i = 0; i < num_syms; i++)
  1067. m_remap_table[m_entries_picked[i]] = i;
  1068. }
  1069. void palette_index_reorderer::prepare_hist(uint32_t num_syms, uint32_t num_indices, const uint32_t *pIndices)
  1070. {
  1071. m_hist.resize(0);
  1072. m_hist.resize(num_syms * num_syms);
  1073. for (uint32_t i = 0; i < num_indices; i++)
  1074. {
  1075. const uint32_t idx = pIndices[i];
  1076. inc_hist(idx, (i < (num_indices - 1)) ? pIndices[i + 1] : -1, num_syms);
  1077. inc_hist(idx, (i > 0) ? pIndices[i - 1] : -1, num_syms);
  1078. }
  1079. }
  1080. void palette_index_reorderer::find_initial(uint32_t num_syms)
  1081. {
  1082. uint32_t max_count = 0, max_index = 0;
  1083. for (uint32_t i = 0; i < num_syms * num_syms; i++)
  1084. if (m_hist[i] > max_count)
  1085. max_count = m_hist[i], max_index = i;
  1086. uint32_t a = max_index / num_syms, b = max_index % num_syms;
  1087. m_entries_picked.push_back(a);
  1088. m_entries_picked.push_back(b);
  1089. for (uint32_t i = 0; i < num_syms; i++)
  1090. if ((i != b) && (i != a))
  1091. m_entries_to_do.push_back(i);
  1092. for (uint32_t i = 0; i < m_entries_to_do.size(); i++)
  1093. for (uint32_t j = 0; j < m_entries_picked.size(); j++)
  1094. m_total_count_to_picked[m_entries_to_do[i]] += get_hist(m_entries_to_do[i], m_entries_picked[j], num_syms);
  1095. }
  1096. void palette_index_reorderer::find_next_entry(uint32_t &best_entry, double &best_count, pEntry_dist_func pDist_func, void *pCtx, float dist_func_weight)
  1097. {
  1098. best_entry = 0;
  1099. best_count = 0;
  1100. for (uint32_t i = 0; i < m_entries_to_do.size(); i++)
  1101. {
  1102. const uint32_t u = m_entries_to_do[i];
  1103. double total_count = m_total_count_to_picked[u];
  1104. if (pDist_func)
  1105. {
  1106. float w = maximum<float>((*pDist_func)(u, m_entries_picked.front(), pCtx), (*pDist_func)(u, m_entries_picked.back(), pCtx));
  1107. assert((w >= 0.0f) && (w <= 1.0f));
  1108. total_count = (total_count + 1.0f) * lerp(1.0f - dist_func_weight, 1.0f + dist_func_weight, w);
  1109. }
  1110. if (total_count <= best_count)
  1111. continue;
  1112. best_entry = i;
  1113. best_count = total_count;
  1114. }
  1115. }
  1116. float palette_index_reorderer::pick_side(uint32_t num_syms, uint32_t entry_to_move, pEntry_dist_func pDist_func, void *pCtx, float dist_func_weight)
  1117. {
  1118. float which_side = 0;
  1119. int l_count = 0, r_count = 0;
  1120. for (uint32_t j = 0; j < m_entries_picked.size(); j++)
  1121. {
  1122. const int count = get_hist(entry_to_move, m_entries_picked[j], num_syms), r = ((int)m_entries_picked.size() + 1 - 2 * (j + 1));
  1123. which_side += static_cast<float>(r * count);
  1124. if (r >= 0)
  1125. l_count += r * count;
  1126. else
  1127. r_count += -r * count;
  1128. }
  1129. if (pDist_func)
  1130. {
  1131. float w_left = lerp(1.0f - dist_func_weight, 1.0f + dist_func_weight, (*pDist_func)(entry_to_move, m_entries_picked.front(), pCtx));
  1132. float w_right = lerp(1.0f - dist_func_weight, 1.0f + dist_func_weight, (*pDist_func)(entry_to_move, m_entries_picked.back(), pCtx));
  1133. which_side = w_left * l_count - w_right * r_count;
  1134. }
  1135. return which_side;
  1136. }
  1137. void image_metrics::calc(const image &a, const image &b, uint32_t first_chan, uint32_t total_chans, bool avg_comp_error, bool use_601_luma)
  1138. {
  1139. assert((first_chan < 4U) && (first_chan + total_chans <= 4U));
  1140. const uint32_t width = basisu::minimum(a.get_width(), b.get_width());
  1141. const uint32_t height = basisu::minimum(a.get_height(), b.get_height());
  1142. double hist[256];
  1143. clear_obj(hist);
  1144. for (uint32_t y = 0; y < height; y++)
  1145. {
  1146. for (uint32_t x = 0; x < width; x++)
  1147. {
  1148. const color_rgba &ca = a(x, y), &cb = b(x, y);
  1149. if (total_chans)
  1150. {
  1151. for (uint32_t c = 0; c < total_chans; c++)
  1152. hist[iabs(ca[first_chan + c] - cb[first_chan + c])]++;
  1153. }
  1154. else
  1155. {
  1156. if (use_601_luma)
  1157. hist[iabs(ca.get_601_luma() - cb.get_601_luma())]++;
  1158. else
  1159. hist[iabs(ca.get_709_luma() - cb.get_709_luma())]++;
  1160. }
  1161. }
  1162. }
  1163. m_max = 0;
  1164. double sum = 0.0f, sum2 = 0.0f;
  1165. for (uint32_t i = 0; i < 256; i++)
  1166. {
  1167. if (hist[i])
  1168. {
  1169. m_max = basisu::maximum<float>(m_max, (float)i);
  1170. double v = i * hist[i];
  1171. sum += v;
  1172. sum2 += i * v;
  1173. }
  1174. }
  1175. double total_values = (double)width * (double)height;
  1176. if (avg_comp_error)
  1177. total_values *= (double)clamp<uint32_t>(total_chans, 1, 4);
  1178. m_mean = (float)clamp<double>(sum / total_values, 0.0f, 255.0);
  1179. m_mean_squared = (float)clamp<double>(sum2 / total_values, 0.0f, 255.0f * 255.0f);
  1180. m_rms = (float)sqrt(m_mean_squared);
  1181. m_psnr = m_rms ? (float)clamp<double>(log10(255.0 / m_rms) * 20.0f, 0.0f, 100.0f) : 100.0f;
  1182. }
  1183. void fill_buffer_with_random_bytes(void *pBuf, size_t size, uint32_t seed)
  1184. {
  1185. rand r(seed);
  1186. uint8_t *pDst = static_cast<uint8_t *>(pBuf);
  1187. while (size >= sizeof(uint32_t))
  1188. {
  1189. *(uint32_t *)pDst = r.urand32();
  1190. pDst += sizeof(uint32_t);
  1191. size -= sizeof(uint32_t);
  1192. }
  1193. while (size)
  1194. {
  1195. *pDst++ = r.byte();
  1196. size--;
  1197. }
  1198. }
  1199. uint32_t hash_hsieh(const uint8_t *pBuf, size_t len)
  1200. {
  1201. if (!pBuf || !len)
  1202. return 0;
  1203. uint32_t h = static_cast<uint32_t>(len);
  1204. const uint32_t bytes_left = len & 3;
  1205. len >>= 2;
  1206. while (len--)
  1207. {
  1208. const uint16_t *pWords = reinterpret_cast<const uint16_t *>(pBuf);
  1209. h += pWords[0];
  1210. const uint32_t t = (pWords[1] << 11) ^ h;
  1211. h = (h << 16) ^ t;
  1212. pBuf += sizeof(uint32_t);
  1213. h += h >> 11;
  1214. }
  1215. switch (bytes_left)
  1216. {
  1217. case 1:
  1218. h += *reinterpret_cast<const signed char*>(pBuf);
  1219. h ^= h << 10;
  1220. h += h >> 1;
  1221. break;
  1222. case 2:
  1223. h += *reinterpret_cast<const uint16_t *>(pBuf);
  1224. h ^= h << 11;
  1225. h += h >> 17;
  1226. break;
  1227. case 3:
  1228. h += *reinterpret_cast<const uint16_t *>(pBuf);
  1229. h ^= h << 16;
  1230. h ^= (static_cast<signed char>(pBuf[sizeof(uint16_t)])) << 18;
  1231. h += h >> 11;
  1232. break;
  1233. default:
  1234. break;
  1235. }
  1236. h ^= h << 3;
  1237. h += h >> 5;
  1238. h ^= h << 4;
  1239. h += h >> 17;
  1240. h ^= h << 25;
  1241. h += h >> 6;
  1242. return h;
  1243. }
  1244. job_pool::job_pool(uint32_t num_threads) :
  1245. m_num_active_jobs(0),
  1246. m_kill_flag(false)
  1247. {
  1248. assert(num_threads >= 1U);
  1249. debug_printf("job_pool::job_pool: %u total threads\n", num_threads);
  1250. if (num_threads > 1)
  1251. {
  1252. m_threads.resize(num_threads - 1);
  1253. for (int i = 0; i < ((int)num_threads - 1); i++)
  1254. m_threads[i] = std::thread([this, i] { job_thread(i); });
  1255. }
  1256. }
  1257. job_pool::~job_pool()
  1258. {
  1259. debug_printf("job_pool::~job_pool\n");
  1260. // Notify all workers that they need to die right now.
  1261. m_kill_flag = true;
  1262. m_has_work.notify_all();
  1263. // Wait for all workers to die.
  1264. for (uint32_t i = 0; i < m_threads.size(); i++)
  1265. m_threads[i].join();
  1266. }
  1267. void job_pool::add_job(const std::function<void()>& job)
  1268. {
  1269. std::unique_lock<std::mutex> lock(m_mutex);
  1270. m_queue.emplace_back(job);
  1271. const size_t queue_size = m_queue.size();
  1272. lock.unlock();
  1273. if (queue_size > 1)
  1274. m_has_work.notify_one();
  1275. }
  1276. void job_pool::add_job(std::function<void()>&& job)
  1277. {
  1278. std::unique_lock<std::mutex> lock(m_mutex);
  1279. m_queue.emplace_back(std::move(job));
  1280. const size_t queue_size = m_queue.size();
  1281. lock.unlock();
  1282. if (queue_size > 1)
  1283. {
  1284. m_has_work.notify_one();
  1285. }
  1286. }
  1287. void job_pool::wait_for_all()
  1288. {
  1289. std::unique_lock<std::mutex> lock(m_mutex);
  1290. // Drain the job queue on the calling thread.
  1291. while (!m_queue.empty())
  1292. {
  1293. std::function<void()> job(m_queue.back());
  1294. m_queue.pop_back();
  1295. lock.unlock();
  1296. job();
  1297. lock.lock();
  1298. }
  1299. // The queue is empty, now wait for all active jobs to finish up.
  1300. m_no_more_jobs.wait(lock, [this]{ return !m_num_active_jobs; } );
  1301. }
  1302. void job_pool::job_thread(uint32_t index)
  1303. {
  1304. BASISU_NOTE_UNUSED(index);
  1305. //debug_printf("job_pool::job_thread: starting %u\n", index);
  1306. while (true)
  1307. {
  1308. std::unique_lock<std::mutex> lock(m_mutex);
  1309. // Wait for any jobs to be issued.
  1310. m_has_work.wait(lock, [this] { return m_kill_flag || m_queue.size(); } );
  1311. // Check to see if we're supposed to exit.
  1312. if (m_kill_flag)
  1313. break;
  1314. // Get the job and execute it.
  1315. std::function<void()> job(m_queue.back());
  1316. m_queue.pop_back();
  1317. ++m_num_active_jobs;
  1318. lock.unlock();
  1319. job();
  1320. lock.lock();
  1321. --m_num_active_jobs;
  1322. // Now check if there are no more jobs remaining.
  1323. const bool all_done = m_queue.empty() && !m_num_active_jobs;
  1324. lock.unlock();
  1325. if (all_done)
  1326. m_no_more_jobs.notify_all();
  1327. }
  1328. //debug_printf("job_pool::job_thread: exiting\n");
  1329. }
  1330. // .TGA image loading
  1331. #pragma pack(push)
  1332. #pragma pack(1)
  1333. struct tga_header
  1334. {
  1335. uint8_t m_id_len;
  1336. uint8_t m_cmap;
  1337. uint8_t m_type;
  1338. packed_uint<2> m_cmap_first;
  1339. packed_uint<2> m_cmap_len;
  1340. uint8_t m_cmap_bpp;
  1341. packed_uint<2> m_x_org;
  1342. packed_uint<2> m_y_org;
  1343. packed_uint<2> m_width;
  1344. packed_uint<2> m_height;
  1345. uint8_t m_depth;
  1346. uint8_t m_desc;
  1347. };
  1348. #pragma pack(pop)
  1349. const uint32_t MAX_TGA_IMAGE_SIZE = 16384;
  1350. enum tga_image_type
  1351. {
  1352. cITPalettized = 1,
  1353. cITRGB = 2,
  1354. cITGrayscale = 3
  1355. };
  1356. uint8_t *read_tga(const uint8_t *pBuf, uint32_t buf_size, int &width, int &height, int &n_chans)
  1357. {
  1358. width = 0;
  1359. height = 0;
  1360. n_chans = 0;
  1361. if (buf_size <= sizeof(tga_header))
  1362. return nullptr;
  1363. const tga_header &hdr = *reinterpret_cast<const tga_header *>(pBuf);
  1364. if ((!hdr.m_width) || (!hdr.m_height) || (hdr.m_width > MAX_TGA_IMAGE_SIZE) || (hdr.m_height > MAX_TGA_IMAGE_SIZE))
  1365. return nullptr;
  1366. if (hdr.m_desc >> 6)
  1367. return nullptr;
  1368. // Simple validation
  1369. if ((hdr.m_cmap != 0) && (hdr.m_cmap != 1))
  1370. return nullptr;
  1371. if (hdr.m_cmap)
  1372. {
  1373. if ((hdr.m_cmap_bpp == 0) || (hdr.m_cmap_bpp > 32))
  1374. return nullptr;
  1375. // Nobody implements CMapFirst correctly, so we're not supporting it. Never seen it used, either.
  1376. if (hdr.m_cmap_first != 0)
  1377. return nullptr;
  1378. }
  1379. const bool x_flipped = (hdr.m_desc & 0x10) != 0;
  1380. const bool y_flipped = (hdr.m_desc & 0x20) == 0;
  1381. bool rle_flag = false;
  1382. int file_image_type = hdr.m_type;
  1383. if (file_image_type > 8)
  1384. {
  1385. file_image_type -= 8;
  1386. rle_flag = true;
  1387. }
  1388. const tga_image_type image_type = static_cast<tga_image_type>(file_image_type);
  1389. switch (file_image_type)
  1390. {
  1391. case cITRGB:
  1392. if (hdr.m_depth == 8)
  1393. return nullptr;
  1394. break;
  1395. case cITPalettized:
  1396. if ((hdr.m_depth != 8) || (hdr.m_cmap != 1) || (hdr.m_cmap_len == 0))
  1397. return nullptr;
  1398. break;
  1399. case cITGrayscale:
  1400. if ((hdr.m_cmap != 0) || (hdr.m_cmap_len != 0))
  1401. return nullptr;
  1402. if ((hdr.m_depth != 8) && (hdr.m_depth != 16))
  1403. return nullptr;
  1404. break;
  1405. default:
  1406. return nullptr;
  1407. }
  1408. uint32_t tga_bytes_per_pixel = 0;
  1409. switch (hdr.m_depth)
  1410. {
  1411. case 32:
  1412. tga_bytes_per_pixel = 4;
  1413. n_chans = 4;
  1414. break;
  1415. case 24:
  1416. tga_bytes_per_pixel = 3;
  1417. n_chans = 3;
  1418. break;
  1419. case 16:
  1420. case 15:
  1421. tga_bytes_per_pixel = 2;
  1422. // For compatibility with stb_image_write.h
  1423. n_chans = ((file_image_type == cITGrayscale) && (hdr.m_depth == 16)) ? 4 : 3;
  1424. break;
  1425. case 8:
  1426. tga_bytes_per_pixel = 1;
  1427. // For palettized RGBA support, which both FreeImage and stb_image support.
  1428. n_chans = ((file_image_type == cITPalettized) && (hdr.m_cmap_bpp == 32)) ? 4 : 3;
  1429. break;
  1430. default:
  1431. return nullptr;
  1432. }
  1433. //const uint32_t bytes_per_line = hdr.m_width * tga_bytes_per_pixel;
  1434. const uint8_t *pSrc = pBuf + sizeof(tga_header);
  1435. uint32_t bytes_remaining = buf_size - sizeof(tga_header);
  1436. if (hdr.m_id_len)
  1437. {
  1438. if (bytes_remaining < hdr.m_id_len)
  1439. return nullptr;
  1440. pSrc += hdr.m_id_len;
  1441. bytes_remaining += hdr.m_id_len;
  1442. }
  1443. color_rgba pal[256];
  1444. for (uint32_t i = 0; i < 256; i++)
  1445. pal[i].set(0, 0, 0, 255);
  1446. if ((hdr.m_cmap) && (hdr.m_cmap_len))
  1447. {
  1448. if (image_type == cITPalettized)
  1449. {
  1450. // Note I cannot find any files using 32bpp palettes in the wild (never seen any in ~30 years).
  1451. if ( ((hdr.m_cmap_bpp != 32) && (hdr.m_cmap_bpp != 24) && (hdr.m_cmap_bpp != 15) && (hdr.m_cmap_bpp != 16)) || (hdr.m_cmap_len > 256) )
  1452. return nullptr;
  1453. if (hdr.m_cmap_bpp == 32)
  1454. {
  1455. const uint32_t pal_size = hdr.m_cmap_len * 4;
  1456. if (bytes_remaining < pal_size)
  1457. return nullptr;
  1458. for (uint32_t i = 0; i < hdr.m_cmap_len; i++)
  1459. {
  1460. pal[i].r = pSrc[i * 4 + 2];
  1461. pal[i].g = pSrc[i * 4 + 1];
  1462. pal[i].b = pSrc[i * 4 + 0];
  1463. pal[i].a = pSrc[i * 4 + 3];
  1464. }
  1465. bytes_remaining -= pal_size;
  1466. pSrc += pal_size;
  1467. }
  1468. else if (hdr.m_cmap_bpp == 24)
  1469. {
  1470. const uint32_t pal_size = hdr.m_cmap_len * 3;
  1471. if (bytes_remaining < pal_size)
  1472. return nullptr;
  1473. for (uint32_t i = 0; i < hdr.m_cmap_len; i++)
  1474. {
  1475. pal[i].r = pSrc[i * 3 + 2];
  1476. pal[i].g = pSrc[i * 3 + 1];
  1477. pal[i].b = pSrc[i * 3 + 0];
  1478. pal[i].a = 255;
  1479. }
  1480. bytes_remaining -= pal_size;
  1481. pSrc += pal_size;
  1482. }
  1483. else
  1484. {
  1485. const uint32_t pal_size = hdr.m_cmap_len * 2;
  1486. if (bytes_remaining < pal_size)
  1487. return nullptr;
  1488. for (uint32_t i = 0; i < hdr.m_cmap_len; i++)
  1489. {
  1490. const uint32_t v = pSrc[i * 2 + 0] | (pSrc[i * 2 + 1] << 8);
  1491. pal[i].r = (((v >> 10) & 31) * 255 + 15) / 31;
  1492. pal[i].g = (((v >> 5) & 31) * 255 + 15) / 31;
  1493. pal[i].b = ((v & 31) * 255 + 15) / 31;
  1494. pal[i].a = 255;
  1495. }
  1496. bytes_remaining -= pal_size;
  1497. pSrc += pal_size;
  1498. }
  1499. }
  1500. else
  1501. {
  1502. const uint32_t bytes_to_skip = (hdr.m_cmap_bpp >> 3) * hdr.m_cmap_len;
  1503. if (bytes_remaining < bytes_to_skip)
  1504. return nullptr;
  1505. pSrc += bytes_to_skip;
  1506. bytes_remaining += bytes_to_skip;
  1507. }
  1508. }
  1509. width = hdr.m_width;
  1510. height = hdr.m_height;
  1511. const uint32_t source_pitch = width * tga_bytes_per_pixel;
  1512. const uint32_t dest_pitch = width * n_chans;
  1513. uint8_t *pImage = (uint8_t *)malloc(dest_pitch * height);
  1514. if (!pImage)
  1515. return nullptr;
  1516. std::vector<uint8_t> input_line_buf;
  1517. if (rle_flag)
  1518. input_line_buf.resize(source_pitch);
  1519. int run_type = 0, run_remaining = 0;
  1520. uint8_t run_pixel[4];
  1521. memset(run_pixel, 0, sizeof(run_pixel));
  1522. for (int y = 0; y < height; y++)
  1523. {
  1524. const uint8_t *pLine_data;
  1525. if (rle_flag)
  1526. {
  1527. int pixels_remaining = width;
  1528. uint8_t *pDst = &input_line_buf[0];
  1529. do
  1530. {
  1531. if (!run_remaining)
  1532. {
  1533. if (bytes_remaining < 1)
  1534. {
  1535. free(pImage);
  1536. return nullptr;
  1537. }
  1538. int v = *pSrc++;
  1539. bytes_remaining--;
  1540. run_type = v & 0x80;
  1541. run_remaining = (v & 0x7F) + 1;
  1542. if (run_type)
  1543. {
  1544. if (bytes_remaining < tga_bytes_per_pixel)
  1545. {
  1546. free(pImage);
  1547. return nullptr;
  1548. }
  1549. memcpy(run_pixel, pSrc, tga_bytes_per_pixel);
  1550. pSrc += tga_bytes_per_pixel;
  1551. bytes_remaining -= tga_bytes_per_pixel;
  1552. }
  1553. }
  1554. const uint32_t n = basisu::minimum<uint32_t>(pixels_remaining, run_remaining);
  1555. pixels_remaining -= n;
  1556. run_remaining -= n;
  1557. if (run_type)
  1558. {
  1559. for (uint32_t i = 0; i < n; i++)
  1560. for (uint32_t j = 0; j < tga_bytes_per_pixel; j++)
  1561. *pDst++ = run_pixel[j];
  1562. }
  1563. else
  1564. {
  1565. const uint32_t bytes_wanted = n * tga_bytes_per_pixel;
  1566. if (bytes_remaining < bytes_wanted)
  1567. {
  1568. free(pImage);
  1569. return nullptr;
  1570. }
  1571. memcpy(pDst, pSrc, bytes_wanted);
  1572. pDst += bytes_wanted;
  1573. pSrc += bytes_wanted;
  1574. bytes_remaining -= bytes_wanted;
  1575. }
  1576. } while (pixels_remaining);
  1577. assert((pDst - &input_line_buf[0]) == width * tga_bytes_per_pixel);
  1578. pLine_data = &input_line_buf[0];
  1579. }
  1580. else
  1581. {
  1582. if (bytes_remaining < source_pitch)
  1583. {
  1584. free(pImage);
  1585. return nullptr;
  1586. }
  1587. pLine_data = pSrc;
  1588. bytes_remaining -= source_pitch;
  1589. pSrc += source_pitch;
  1590. }
  1591. // Convert to 24bpp RGB or 32bpp RGBA.
  1592. uint8_t *pDst = pImage + (y_flipped ? (height - 1 - y) : y) * dest_pitch + (x_flipped ? (width - 1) * n_chans : 0);
  1593. const int dst_stride = x_flipped ? -((int)n_chans) : n_chans;
  1594. switch (hdr.m_depth)
  1595. {
  1596. case 32:
  1597. assert(tga_bytes_per_pixel == 4 && n_chans == 4);
  1598. for (int i = 0; i < width; i++, pLine_data += 4, pDst += dst_stride)
  1599. {
  1600. pDst[0] = pLine_data[2];
  1601. pDst[1] = pLine_data[1];
  1602. pDst[2] = pLine_data[0];
  1603. pDst[3] = pLine_data[3];
  1604. }
  1605. break;
  1606. case 24:
  1607. assert(tga_bytes_per_pixel == 3 && n_chans == 3);
  1608. for (int i = 0; i < width; i++, pLine_data += 3, pDst += dst_stride)
  1609. {
  1610. pDst[0] = pLine_data[2];
  1611. pDst[1] = pLine_data[1];
  1612. pDst[2] = pLine_data[0];
  1613. }
  1614. break;
  1615. case 16:
  1616. case 15:
  1617. if (image_type == cITRGB)
  1618. {
  1619. assert(tga_bytes_per_pixel == 2 && n_chans == 3);
  1620. for (int i = 0; i < width; i++, pLine_data += 2, pDst += dst_stride)
  1621. {
  1622. const uint32_t v = pLine_data[0] | (pLine_data[1] << 8);
  1623. pDst[0] = (((v >> 10) & 31) * 255 + 15) / 31;
  1624. pDst[1] = (((v >> 5) & 31) * 255 + 15) / 31;
  1625. pDst[2] = ((v & 31) * 255 + 15) / 31;
  1626. }
  1627. }
  1628. else
  1629. {
  1630. assert(image_type == cITGrayscale && tga_bytes_per_pixel == 2 && n_chans == 4);
  1631. for (int i = 0; i < width; i++, pLine_data += 2, pDst += dst_stride)
  1632. {
  1633. pDst[0] = pLine_data[0];
  1634. pDst[1] = pLine_data[0];
  1635. pDst[2] = pLine_data[0];
  1636. pDst[3] = pLine_data[1];
  1637. }
  1638. }
  1639. break;
  1640. case 8:
  1641. assert(tga_bytes_per_pixel == 1);
  1642. if (image_type == cITPalettized)
  1643. {
  1644. if (hdr.m_cmap_bpp == 32)
  1645. {
  1646. assert(n_chans == 4);
  1647. for (int i = 0; i < width; i++, pLine_data++, pDst += dst_stride)
  1648. {
  1649. const uint32_t c = *pLine_data;
  1650. pDst[0] = pal[c].r;
  1651. pDst[1] = pal[c].g;
  1652. pDst[2] = pal[c].b;
  1653. pDst[3] = pal[c].a;
  1654. }
  1655. }
  1656. else
  1657. {
  1658. assert(n_chans == 3);
  1659. for (int i = 0; i < width; i++, pLine_data++, pDst += dst_stride)
  1660. {
  1661. const uint32_t c = *pLine_data;
  1662. pDst[0] = pal[c].r;
  1663. pDst[1] = pal[c].g;
  1664. pDst[2] = pal[c].b;
  1665. }
  1666. }
  1667. }
  1668. else
  1669. {
  1670. assert(n_chans == 3);
  1671. for (int i = 0; i < width; i++, pLine_data++, pDst += dst_stride)
  1672. {
  1673. const uint8_t c = *pLine_data;
  1674. pDst[0] = c;
  1675. pDst[1] = c;
  1676. pDst[2] = c;
  1677. }
  1678. }
  1679. break;
  1680. default:
  1681. assert(0);
  1682. break;
  1683. }
  1684. } // y
  1685. return pImage;
  1686. }
  1687. uint8_t *read_tga(const char *pFilename, int &width, int &height, int &n_chans)
  1688. {
  1689. width = height = n_chans = 0;
  1690. uint8_vec filedata;
  1691. if (!read_file_to_vec(pFilename, filedata))
  1692. return nullptr;
  1693. if (!filedata.size() || (filedata.size() > UINT32_MAX))
  1694. return nullptr;
  1695. return read_tga(&filedata[0], (uint32_t)filedata.size(), width, height, n_chans);
  1696. }
  1697. void image::debug_text(uint32_t x_ofs, uint32_t y_ofs, uint32_t scale_x, uint32_t scale_y, const color_rgba& fg, const color_rgba* pBG, bool alpha_only, const char* pFmt, ...)
  1698. {
  1699. char buf[2048];
  1700. va_list args;
  1701. va_start(args, pFmt);
  1702. #ifdef _WIN32
  1703. vsprintf_s(buf, sizeof(buf), pFmt, args);
  1704. #else
  1705. vsnprintf(buf, sizeof(buf), pFmt, args);
  1706. #endif
  1707. va_end(args);
  1708. const char* p = buf;
  1709. const uint32_t orig_x_ofs = x_ofs;
  1710. while (*p)
  1711. {
  1712. uint8_t c = *p++;
  1713. if ((c < 32) || (c > 127))
  1714. c = '.';
  1715. const uint8_t* pGlpyh = &g_debug_font8x8_basic[c - 32][0];
  1716. for (uint32_t y = 0; y < 8; y++)
  1717. {
  1718. uint32_t row_bits = pGlpyh[y];
  1719. for (uint32_t x = 0; x < 8; x++)
  1720. {
  1721. const uint32_t q = row_bits & (1 << x);
  1722. const color_rgba* pColor = q ? &fg : pBG;
  1723. if (!pColor)
  1724. continue;
  1725. if (alpha_only)
  1726. fill_box_alpha(x_ofs + x * scale_x, y_ofs + y * scale_y, scale_x, scale_y, *pColor);
  1727. else
  1728. fill_box(x_ofs + x * scale_x, y_ofs + y * scale_y, scale_x, scale_y, *pColor);
  1729. }
  1730. }
  1731. x_ofs += 8 * scale_x;
  1732. if ((x_ofs + 8 * scale_x) > m_width)
  1733. {
  1734. x_ofs = orig_x_ofs;
  1735. y_ofs += 8 * scale_y;
  1736. }
  1737. }
  1738. }
  1739. } // namespace basisu