basisu_enc.cpp 54 KB

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