image_pack 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (319) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +18 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +140 -0
  5. data/THIRD_PARTY_NOTICES.md +8 -0
  6. data/ext/image_pack/extconf.rb +515 -0
  7. data/ext/image_pack/image_pack.c +1618 -0
  8. data/ext/image_pack/vendor/.vendored +1 -0
  9. data/ext/image_pack/vendor/mozjpeg/BUILDING.txt +902 -0
  10. data/ext/image_pack/vendor/mozjpeg/CMakeLists.txt +1593 -0
  11. data/ext/image_pack/vendor/mozjpeg/LICENSE.md +132 -0
  12. data/ext/image_pack/vendor/mozjpeg/README-mozilla.txt +194 -0
  13. data/ext/image_pack/vendor/mozjpeg/README-turbo.txt +346 -0
  14. data/ext/image_pack/vendor/mozjpeg/README.ijg +258 -0
  15. data/ext/image_pack/vendor/mozjpeg/README.md +29 -0
  16. data/ext/image_pack/vendor/mozjpeg/cderror.h +128 -0
  17. data/ext/image_pack/vendor/mozjpeg/cdjpeg.c +156 -0
  18. data/ext/image_pack/vendor/mozjpeg/cdjpeg.h +171 -0
  19. data/ext/image_pack/vendor/mozjpeg/cjpeg.c +961 -0
  20. data/ext/image_pack/vendor/mozjpeg/cmyk.h +60 -0
  21. data/ext/image_pack/vendor/mozjpeg/coderules.txt +78 -0
  22. data/ext/image_pack/vendor/mozjpeg/croptest.in +95 -0
  23. data/ext/image_pack/vendor/mozjpeg/djpeg.c +855 -0
  24. data/ext/image_pack/vendor/mozjpeg/example.txt +464 -0
  25. data/ext/image_pack/vendor/mozjpeg/jaricom.c +157 -0
  26. data/ext/image_pack/vendor/mozjpeg/jcapimin.c +307 -0
  27. data/ext/image_pack/vendor/mozjpeg/jcapistd.c +168 -0
  28. data/ext/image_pack/vendor/mozjpeg/jcarith.c +972 -0
  29. data/ext/image_pack/vendor/mozjpeg/jccoefct.c +609 -0
  30. data/ext/image_pack/vendor/mozjpeg/jccolext.c +144 -0
  31. data/ext/image_pack/vendor/mozjpeg/jccolor.c +721 -0
  32. data/ext/image_pack/vendor/mozjpeg/jcdctmgr.c +1776 -0
  33. data/ext/image_pack/vendor/mozjpeg/jcext.c +219 -0
  34. data/ext/image_pack/vendor/mozjpeg/jchuff.c +1146 -0
  35. data/ext/image_pack/vendor/mozjpeg/jchuff.h +57 -0
  36. data/ext/image_pack/vendor/mozjpeg/jcicc.c +105 -0
  37. data/ext/image_pack/vendor/mozjpeg/jcinit.c +82 -0
  38. data/ext/image_pack/vendor/mozjpeg/jcmainct.c +162 -0
  39. data/ext/image_pack/vendor/mozjpeg/jcmarker.c +844 -0
  40. data/ext/image_pack/vendor/mozjpeg/jcmaster.c +958 -0
  41. data/ext/image_pack/vendor/mozjpeg/jcmaster.h +56 -0
  42. data/ext/image_pack/vendor/mozjpeg/jcomapi.c +109 -0
  43. data/ext/image_pack/vendor/mozjpeg/jconfig.h.in +37 -0
  44. data/ext/image_pack/vendor/mozjpeg/jconfig.txt +93 -0
  45. data/ext/image_pack/vendor/mozjpeg/jconfigint.h.in +44 -0
  46. data/ext/image_pack/vendor/mozjpeg/jcparam.c +991 -0
  47. data/ext/image_pack/vendor/mozjpeg/jcphuff.c +1123 -0
  48. data/ext/image_pack/vendor/mozjpeg/jcprepct.c +351 -0
  49. data/ext/image_pack/vendor/mozjpeg/jcsample.c +522 -0
  50. data/ext/image_pack/vendor/mozjpeg/jcstest.c +126 -0
  51. data/ext/image_pack/vendor/mozjpeg/jctrans.c +408 -0
  52. data/ext/image_pack/vendor/mozjpeg/jdapimin.c +407 -0
  53. data/ext/image_pack/vendor/mozjpeg/jdapistd.c +691 -0
  54. data/ext/image_pack/vendor/mozjpeg/jdarith.c +782 -0
  55. data/ext/image_pack/vendor/mozjpeg/jdatadst-tj.c +198 -0
  56. data/ext/image_pack/vendor/mozjpeg/jdatadst.c +299 -0
  57. data/ext/image_pack/vendor/mozjpeg/jdatasrc-tj.c +194 -0
  58. data/ext/image_pack/vendor/mozjpeg/jdatasrc.c +295 -0
  59. data/ext/image_pack/vendor/mozjpeg/jdcoefct.c +881 -0
  60. data/ext/image_pack/vendor/mozjpeg/jdcoefct.h +83 -0
  61. data/ext/image_pack/vendor/mozjpeg/jdcol565.c +384 -0
  62. data/ext/image_pack/vendor/mozjpeg/jdcolext.c +141 -0
  63. data/ext/image_pack/vendor/mozjpeg/jdcolor.c +881 -0
  64. data/ext/image_pack/vendor/mozjpeg/jdct.h +208 -0
  65. data/ext/image_pack/vendor/mozjpeg/jddctmgr.c +367 -0
  66. data/ext/image_pack/vendor/mozjpeg/jdhuff.c +834 -0
  67. data/ext/image_pack/vendor/mozjpeg/jdhuff.h +247 -0
  68. data/ext/image_pack/vendor/mozjpeg/jdicc.c +167 -0
  69. data/ext/image_pack/vendor/mozjpeg/jdinput.c +408 -0
  70. data/ext/image_pack/vendor/mozjpeg/jdmainct.c +460 -0
  71. data/ext/image_pack/vendor/mozjpeg/jdmainct.h +71 -0
  72. data/ext/image_pack/vendor/mozjpeg/jdmarker.c +1374 -0
  73. data/ext/image_pack/vendor/mozjpeg/jdmaster.c +727 -0
  74. data/ext/image_pack/vendor/mozjpeg/jdmaster.h +33 -0
  75. data/ext/image_pack/vendor/mozjpeg/jdmerge.c +587 -0
  76. data/ext/image_pack/vendor/mozjpeg/jdmerge.h +47 -0
  77. data/ext/image_pack/vendor/mozjpeg/jdmrg565.c +354 -0
  78. data/ext/image_pack/vendor/mozjpeg/jdmrgext.c +184 -0
  79. data/ext/image_pack/vendor/mozjpeg/jdphuff.c +679 -0
  80. data/ext/image_pack/vendor/mozjpeg/jdpostct.c +294 -0
  81. data/ext/image_pack/vendor/mozjpeg/jdsample.c +524 -0
  82. data/ext/image_pack/vendor/mozjpeg/jdsample.h +50 -0
  83. data/ext/image_pack/vendor/mozjpeg/jdtrans.c +156 -0
  84. data/ext/image_pack/vendor/mozjpeg/jerror.c +251 -0
  85. data/ext/image_pack/vendor/mozjpeg/jerror.h +335 -0
  86. data/ext/image_pack/vendor/mozjpeg/jfdctflt.c +169 -0
  87. data/ext/image_pack/vendor/mozjpeg/jfdctfst.c +227 -0
  88. data/ext/image_pack/vendor/mozjpeg/jfdctint.c +288 -0
  89. data/ext/image_pack/vendor/mozjpeg/jidctflt.c +240 -0
  90. data/ext/image_pack/vendor/mozjpeg/jidctfst.c +371 -0
  91. data/ext/image_pack/vendor/mozjpeg/jidctint.c +2627 -0
  92. data/ext/image_pack/vendor/mozjpeg/jidctred.c +409 -0
  93. data/ext/image_pack/vendor/mozjpeg/jinclude.h +147 -0
  94. data/ext/image_pack/vendor/mozjpeg/jmemmgr.c +1180 -0
  95. data/ext/image_pack/vendor/mozjpeg/jmemnobs.c +110 -0
  96. data/ext/image_pack/vendor/mozjpeg/jmemsys.h +178 -0
  97. data/ext/image_pack/vendor/mozjpeg/jmorecfg.h +382 -0
  98. data/ext/image_pack/vendor/mozjpeg/jpeg_nbits_table.h +4098 -0
  99. data/ext/image_pack/vendor/mozjpeg/jpegcomp.h +32 -0
  100. data/ext/image_pack/vendor/mozjpeg/jpegint.h +453 -0
  101. data/ext/image_pack/vendor/mozjpeg/jpeglib.h +1211 -0
  102. data/ext/image_pack/vendor/mozjpeg/jpegtran.c +827 -0
  103. data/ext/image_pack/vendor/mozjpeg/jpegyuv.c +172 -0
  104. data/ext/image_pack/vendor/mozjpeg/jquant1.c +856 -0
  105. data/ext/image_pack/vendor/mozjpeg/jquant2.c +1286 -0
  106. data/ext/image_pack/vendor/mozjpeg/jsimd.h +123 -0
  107. data/ext/image_pack/vendor/mozjpeg/jsimd_none.c +431 -0
  108. data/ext/image_pack/vendor/mozjpeg/jsimddct.h +70 -0
  109. data/ext/image_pack/vendor/mozjpeg/jstdhuff.c +144 -0
  110. data/ext/image_pack/vendor/mozjpeg/jutils.c +133 -0
  111. data/ext/image_pack/vendor/mozjpeg/jversion.h.in +56 -0
  112. data/ext/image_pack/vendor/mozjpeg/libjpeg.map.in +11 -0
  113. data/ext/image_pack/vendor/mozjpeg/libjpeg.txt +3150 -0
  114. data/ext/image_pack/vendor/mozjpeg/rdbmp.c +690 -0
  115. data/ext/image_pack/vendor/mozjpeg/rdcolmap.c +253 -0
  116. data/ext/image_pack/vendor/mozjpeg/rdgif.c +720 -0
  117. data/ext/image_pack/vendor/mozjpeg/rdjpeg.c +160 -0
  118. data/ext/image_pack/vendor/mozjpeg/rdjpgcom.c +494 -0
  119. data/ext/image_pack/vendor/mozjpeg/rdpng.c +194 -0
  120. data/ext/image_pack/vendor/mozjpeg/rdppm.c +781 -0
  121. data/ext/image_pack/vendor/mozjpeg/rdswitch.c +642 -0
  122. data/ext/image_pack/vendor/mozjpeg/rdtarga.c +508 -0
  123. data/ext/image_pack/vendor/mozjpeg/simd/arm/aarch32/jccolext-neon.c +148 -0
  124. data/ext/image_pack/vendor/mozjpeg/simd/arm/aarch32/jchuff-neon.c +334 -0
  125. data/ext/image_pack/vendor/mozjpeg/simd/arm/aarch32/jsimd.c +976 -0
  126. data/ext/image_pack/vendor/mozjpeg/simd/arm/aarch32/jsimd_neon.S +1200 -0
  127. data/ext/image_pack/vendor/mozjpeg/simd/arm/aarch64/jccolext-neon.c +316 -0
  128. data/ext/image_pack/vendor/mozjpeg/simd/arm/aarch64/jchuff-neon.c +411 -0
  129. data/ext/image_pack/vendor/mozjpeg/simd/arm/aarch64/jsimd.c +1053 -0
  130. data/ext/image_pack/vendor/mozjpeg/simd/arm/aarch64/jsimd_neon.S +2254 -0
  131. data/ext/image_pack/vendor/mozjpeg/simd/arm/align.h +28 -0
  132. data/ext/image_pack/vendor/mozjpeg/simd/arm/jccolor-neon.c +160 -0
  133. data/ext/image_pack/vendor/mozjpeg/simd/arm/jcgray-neon.c +120 -0
  134. data/ext/image_pack/vendor/mozjpeg/simd/arm/jcgryext-neon.c +106 -0
  135. data/ext/image_pack/vendor/mozjpeg/simd/arm/jchuff.h +131 -0
  136. data/ext/image_pack/vendor/mozjpeg/simd/arm/jcphuff-neon.c +623 -0
  137. data/ext/image_pack/vendor/mozjpeg/simd/arm/jcsample-neon.c +192 -0
  138. data/ext/image_pack/vendor/mozjpeg/simd/arm/jdcolext-neon.c +374 -0
  139. data/ext/image_pack/vendor/mozjpeg/simd/arm/jdcolor-neon.c +141 -0
  140. data/ext/image_pack/vendor/mozjpeg/simd/arm/jdmerge-neon.c +144 -0
  141. data/ext/image_pack/vendor/mozjpeg/simd/arm/jdmrgext-neon.c +723 -0
  142. data/ext/image_pack/vendor/mozjpeg/simd/arm/jdsample-neon.c +569 -0
  143. data/ext/image_pack/vendor/mozjpeg/simd/arm/jfdctfst-neon.c +214 -0
  144. data/ext/image_pack/vendor/mozjpeg/simd/arm/jfdctint-neon.c +376 -0
  145. data/ext/image_pack/vendor/mozjpeg/simd/arm/jidctfst-neon.c +472 -0
  146. data/ext/image_pack/vendor/mozjpeg/simd/arm/jidctint-neon.c +801 -0
  147. data/ext/image_pack/vendor/mozjpeg/simd/arm/jidctred-neon.c +486 -0
  148. data/ext/image_pack/vendor/mozjpeg/simd/arm/jquanti-neon.c +193 -0
  149. data/ext/image_pack/vendor/mozjpeg/simd/arm/neon-compat.h +26 -0
  150. data/ext/image_pack/vendor/mozjpeg/simd/arm/neon-compat.h.in +37 -0
  151. data/ext/image_pack/vendor/mozjpeg/simd/i386/jccolext-avx2.asm +578 -0
  152. data/ext/image_pack/vendor/mozjpeg/simd/i386/jccolext-mmx.asm +476 -0
  153. data/ext/image_pack/vendor/mozjpeg/simd/i386/jccolext-sse2.asm +503 -0
  154. data/ext/image_pack/vendor/mozjpeg/simd/i386/jccolor-avx2.asm +121 -0
  155. data/ext/image_pack/vendor/mozjpeg/simd/i386/jccolor-mmx.asm +121 -0
  156. data/ext/image_pack/vendor/mozjpeg/simd/i386/jccolor-sse2.asm +120 -0
  157. data/ext/image_pack/vendor/mozjpeg/simd/i386/jcgray-avx2.asm +113 -0
  158. data/ext/image_pack/vendor/mozjpeg/simd/i386/jcgray-mmx.asm +113 -0
  159. data/ext/image_pack/vendor/mozjpeg/simd/i386/jcgray-sse2.asm +112 -0
  160. data/ext/image_pack/vendor/mozjpeg/simd/i386/jcgryext-avx2.asm +457 -0
  161. data/ext/image_pack/vendor/mozjpeg/simd/i386/jcgryext-mmx.asm +355 -0
  162. data/ext/image_pack/vendor/mozjpeg/simd/i386/jcgryext-sse2.asm +382 -0
  163. data/ext/image_pack/vendor/mozjpeg/simd/i386/jchuff-sse2.asm +761 -0
  164. data/ext/image_pack/vendor/mozjpeg/simd/i386/jcphuff-sse2.asm +662 -0
  165. data/ext/image_pack/vendor/mozjpeg/simd/i386/jcsample-avx2.asm +388 -0
  166. data/ext/image_pack/vendor/mozjpeg/simd/i386/jcsample-mmx.asm +324 -0
  167. data/ext/image_pack/vendor/mozjpeg/simd/i386/jcsample-sse2.asm +351 -0
  168. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdcolext-avx2.asm +515 -0
  169. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdcolext-mmx.asm +404 -0
  170. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdcolext-sse2.asm +458 -0
  171. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdcolor-avx2.asm +118 -0
  172. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdcolor-mmx.asm +117 -0
  173. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdcolor-sse2.asm +117 -0
  174. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdmerge-avx2.asm +136 -0
  175. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdmerge-mmx.asm +123 -0
  176. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdmerge-sse2.asm +135 -0
  177. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdmrgext-avx2.asm +575 -0
  178. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdmrgext-mmx.asm +460 -0
  179. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdmrgext-sse2.asm +517 -0
  180. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdsample-avx2.asm +760 -0
  181. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdsample-mmx.asm +731 -0
  182. data/ext/image_pack/vendor/mozjpeg/simd/i386/jdsample-sse2.asm +724 -0
  183. data/ext/image_pack/vendor/mozjpeg/simd/i386/jfdctflt-3dn.asm +318 -0
  184. data/ext/image_pack/vendor/mozjpeg/simd/i386/jfdctflt-sse.asm +369 -0
  185. data/ext/image_pack/vendor/mozjpeg/simd/i386/jfdctfst-mmx.asm +395 -0
  186. data/ext/image_pack/vendor/mozjpeg/simd/i386/jfdctfst-sse2.asm +403 -0
  187. data/ext/image_pack/vendor/mozjpeg/simd/i386/jfdctint-avx2.asm +331 -0
  188. data/ext/image_pack/vendor/mozjpeg/simd/i386/jfdctint-mmx.asm +620 -0
  189. data/ext/image_pack/vendor/mozjpeg/simd/i386/jfdctint-sse2.asm +633 -0
  190. data/ext/image_pack/vendor/mozjpeg/simd/i386/jidctflt-3dn.asm +451 -0
  191. data/ext/image_pack/vendor/mozjpeg/simd/i386/jidctflt-sse.asm +571 -0
  192. data/ext/image_pack/vendor/mozjpeg/simd/i386/jidctflt-sse2.asm +497 -0
  193. data/ext/image_pack/vendor/mozjpeg/simd/i386/jidctfst-mmx.asm +499 -0
  194. data/ext/image_pack/vendor/mozjpeg/simd/i386/jidctfst-sse2.asm +501 -0
  195. data/ext/image_pack/vendor/mozjpeg/simd/i386/jidctint-avx2.asm +453 -0
  196. data/ext/image_pack/vendor/mozjpeg/simd/i386/jidctint-mmx.asm +851 -0
  197. data/ext/image_pack/vendor/mozjpeg/simd/i386/jidctint-sse2.asm +858 -0
  198. data/ext/image_pack/vendor/mozjpeg/simd/i386/jidctred-mmx.asm +704 -0
  199. data/ext/image_pack/vendor/mozjpeg/simd/i386/jidctred-sse2.asm +592 -0
  200. data/ext/image_pack/vendor/mozjpeg/simd/i386/jquant-3dn.asm +230 -0
  201. data/ext/image_pack/vendor/mozjpeg/simd/i386/jquant-mmx.asm +276 -0
  202. data/ext/image_pack/vendor/mozjpeg/simd/i386/jquant-sse.asm +208 -0
  203. data/ext/image_pack/vendor/mozjpeg/simd/i386/jquantf-sse2.asm +168 -0
  204. data/ext/image_pack/vendor/mozjpeg/simd/i386/jquanti-avx2.asm +188 -0
  205. data/ext/image_pack/vendor/mozjpeg/simd/i386/jquanti-sse2.asm +201 -0
  206. data/ext/image_pack/vendor/mozjpeg/simd/i386/jsimd.c +1312 -0
  207. data/ext/image_pack/vendor/mozjpeg/simd/i386/jsimdcpu.asm +135 -0
  208. data/ext/image_pack/vendor/mozjpeg/simd/jsimd.h +1258 -0
  209. data/ext/image_pack/vendor/mozjpeg/simd/mips/jsimd.c +1143 -0
  210. data/ext/image_pack/vendor/mozjpeg/simd/mips/jsimd_dspr2.S +4543 -0
  211. data/ext/image_pack/vendor/mozjpeg/simd/mips/jsimd_dspr2_asm.h +292 -0
  212. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jccolext-mmi.c +455 -0
  213. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jccolor-mmi.c +148 -0
  214. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jcgray-mmi.c +132 -0
  215. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jcgryext-mmi.c +374 -0
  216. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jcsample-mmi.c +98 -0
  217. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jcsample.h +28 -0
  218. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jdcolext-mmi.c +415 -0
  219. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jdcolor-mmi.c +139 -0
  220. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jdmerge-mmi.c +149 -0
  221. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jdmrgext-mmi.c +615 -0
  222. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jdsample-mmi.c +304 -0
  223. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jfdctfst-mmi.c +255 -0
  224. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jfdctint-mmi.c +398 -0
  225. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jidctfst-mmi.c +395 -0
  226. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jidctint-mmi.c +571 -0
  227. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jquanti-mmi.c +124 -0
  228. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jsimd.c +866 -0
  229. data/ext/image_pack/vendor/mozjpeg/simd/mips64/jsimd_mmi.h +69 -0
  230. data/ext/image_pack/vendor/mozjpeg/simd/mips64/loongson-mmintrin.h +1334 -0
  231. data/ext/image_pack/vendor/mozjpeg/simd/nasm/jcolsamp.inc +135 -0
  232. data/ext/image_pack/vendor/mozjpeg/simd/nasm/jdct.inc +31 -0
  233. data/ext/image_pack/vendor/mozjpeg/simd/nasm/jsimdcfg.inc +93 -0
  234. data/ext/image_pack/vendor/mozjpeg/simd/nasm/jsimdcfg.inc.h +133 -0
  235. data/ext/image_pack/vendor/mozjpeg/simd/nasm/jsimdext.inc +520 -0
  236. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jccolext-altivec.c +269 -0
  237. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jccolor-altivec.c +116 -0
  238. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jcgray-altivec.c +111 -0
  239. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jcgryext-altivec.c +228 -0
  240. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jcsample-altivec.c +159 -0
  241. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jcsample.h +28 -0
  242. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jdcolext-altivec.c +276 -0
  243. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jdcolor-altivec.c +106 -0
  244. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jdmerge-altivec.c +130 -0
  245. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jdmrgext-altivec.c +329 -0
  246. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jdsample-altivec.c +400 -0
  247. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jfdctfst-altivec.c +154 -0
  248. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jfdctint-altivec.c +258 -0
  249. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jidctfst-altivec.c +255 -0
  250. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jidctint-altivec.c +357 -0
  251. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jquanti-altivec.c +250 -0
  252. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jsimd.c +884 -0
  253. data/ext/image_pack/vendor/mozjpeg/simd/powerpc/jsimd_altivec.h +98 -0
  254. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jccolext-avx2.asm +559 -0
  255. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jccolext-sse2.asm +484 -0
  256. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jccolor-avx2.asm +121 -0
  257. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jccolor-sse2.asm +120 -0
  258. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jcgray-avx2.asm +113 -0
  259. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jcgray-sse2.asm +112 -0
  260. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jcgryext-avx2.asm +438 -0
  261. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jcgryext-sse2.asm +363 -0
  262. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jchuff-sse2.asm +583 -0
  263. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jcphuff-sse2.asm +639 -0
  264. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jcsample-avx2.asm +367 -0
  265. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jcsample-sse2.asm +330 -0
  266. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jdcolext-avx2.asm +496 -0
  267. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jdcolext-sse2.asm +439 -0
  268. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jdcolor-avx2.asm +118 -0
  269. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jdcolor-sse2.asm +117 -0
  270. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jdmerge-avx2.asm +136 -0
  271. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jdmerge-sse2.asm +135 -0
  272. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jdmrgext-avx2.asm +596 -0
  273. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jdmrgext-sse2.asm +538 -0
  274. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jdsample-avx2.asm +696 -0
  275. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jdsample-sse2.asm +665 -0
  276. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jfdctflt-sse.asm +355 -0
  277. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jfdctfst-sse2.asm +389 -0
  278. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jfdctint-avx2.asm +320 -0
  279. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jfdctint-sse2.asm +619 -0
  280. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jidctflt-sse2.asm +482 -0
  281. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jidctfst-sse2.asm +491 -0
  282. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jidctint-avx2.asm +418 -0
  283. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jidctint-sse2.asm +847 -0
  284. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jidctred-sse2.asm +574 -0
  285. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jquantf-sse2.asm +155 -0
  286. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jquanti-avx2.asm +163 -0
  287. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jquanti-sse2.asm +188 -0
  288. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jsimd.c +1110 -0
  289. data/ext/image_pack/vendor/mozjpeg/simd/x86_64/jsimdcpu.asm +86 -0
  290. data/ext/image_pack/vendor/mozjpeg/strtest.c +170 -0
  291. data/ext/image_pack/vendor/mozjpeg/structure.txt +900 -0
  292. data/ext/image_pack/vendor/mozjpeg/tjbench.c +1044 -0
  293. data/ext/image_pack/vendor/mozjpeg/tjbenchtest.in +256 -0
  294. data/ext/image_pack/vendor/mozjpeg/tjbenchtest.java.in +215 -0
  295. data/ext/image_pack/vendor/mozjpeg/tjexample.c +406 -0
  296. data/ext/image_pack/vendor/mozjpeg/tjexampletest.in +149 -0
  297. data/ext/image_pack/vendor/mozjpeg/tjexampletest.java.in +151 -0
  298. data/ext/image_pack/vendor/mozjpeg/tjunittest.c +961 -0
  299. data/ext/image_pack/vendor/mozjpeg/tjutil.c +70 -0
  300. data/ext/image_pack/vendor/mozjpeg/tjutil.h +53 -0
  301. data/ext/image_pack/vendor/mozjpeg/transupp.c +2373 -0
  302. data/ext/image_pack/vendor/mozjpeg/transupp.h +243 -0
  303. data/ext/image_pack/vendor/mozjpeg/turbojpeg-jni.c +1259 -0
  304. data/ext/image_pack/vendor/mozjpeg/turbojpeg.c +2320 -0
  305. data/ext/image_pack/vendor/mozjpeg/turbojpeg.h +1784 -0
  306. data/ext/image_pack/vendor/mozjpeg/usage.txt +679 -0
  307. data/ext/image_pack/vendor/mozjpeg/wizard.txt +220 -0
  308. data/ext/image_pack/vendor/mozjpeg/wrbmp.c +552 -0
  309. data/ext/image_pack/vendor/mozjpeg/wrgif.c +580 -0
  310. data/ext/image_pack/vendor/mozjpeg/wrjpgcom.c +577 -0
  311. data/ext/image_pack/vendor/mozjpeg/wrppm.c +366 -0
  312. data/ext/image_pack/vendor/mozjpeg/wrtarga.c +258 -0
  313. data/ext/image_pack/vendor/mozjpeg/yuvjpeg.c +268 -0
  314. data/lib/image_pack/backend.rb +8 -0
  315. data/lib/image_pack/configuration.rb +23 -0
  316. data/lib/image_pack/errors.rb +13 -0
  317. data/lib/image_pack/version.rb +5 -0
  318. data/lib/image_pack.rb +208 -0
  319. metadata +433 -0
@@ -0,0 +1,1146 @@
1
+ /*
2
+ * jchuff.c
3
+ *
4
+ * This file was part of the Independent JPEG Group's software:
5
+ * Copyright (C) 1991-1997, Thomas G. Lane.
6
+ * libjpeg-turbo Modifications:
7
+ * Copyright (C) 2009-2011, 2014-2016, 2018-2023, D. R. Commander.
8
+ * Copyright (C) 2015, Matthieu Darbois.
9
+ * Copyright (C) 2018, Matthias Räncker.
10
+ * Copyright (C) 2020, Arm Limited.
11
+ * For conditions of distribution and use, see the accompanying README.ijg
12
+ * file.
13
+ *
14
+ * This file contains Huffman entropy encoding routines.
15
+ *
16
+ * Much of the complexity here has to do with supporting output suspension.
17
+ * If the data destination module demands suspension, we want to be able to
18
+ * back up to the start of the current MCU. To do this, we copy state
19
+ * variables into local working storage, and update them back to the
20
+ * permanent JPEG objects only upon successful completion of an MCU.
21
+ *
22
+ * NOTE: All referenced figures are from
23
+ * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
24
+ */
25
+
26
+ #define JPEG_INTERNALS
27
+ #include "jinclude.h"
28
+ #include "jpeglib.h"
29
+ #include "jsimd.h"
30
+ #include <limits.h>
31
+
32
+ /*
33
+ * NOTE: If USE_CLZ_INTRINSIC is defined, then clz/bsr instructions will be
34
+ * used for bit counting rather than the lookup table. This will reduce the
35
+ * memory footprint by 64k, which is important for some mobile applications
36
+ * that create many isolated instances of libjpeg-turbo (web browsers, for
37
+ * instance.) This may improve performance on some mobile platforms as well.
38
+ * This feature is enabled by default only on Arm processors, because some x86
39
+ * chips have a slow implementation of bsr, and the use of clz/bsr cannot be
40
+ * shown to have a significant performance impact even on the x86 chips that
41
+ * have a fast implementation of it. When building for Armv6, you can
42
+ * explicitly disable the use of clz/bsr by adding -mthumb to the compiler
43
+ * flags (this defines __thumb__).
44
+ */
45
+
46
+ /* NOTE: Both GCC and Clang define __GNUC__ */
47
+ #if (defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__))) || \
48
+ defined(_M_ARM) || defined(_M_ARM64)
49
+ #if !defined(__thumb__) || defined(__thumb2__)
50
+ #define USE_CLZ_INTRINSIC
51
+ #endif
52
+ #endif
53
+
54
+ #ifdef USE_CLZ_INTRINSIC
55
+ #if defined(_MSC_VER) && !defined(__clang__)
56
+ #define JPEG_NBITS_NONZERO(x) (32 - _CountLeadingZeros(x))
57
+ #else
58
+ #define JPEG_NBITS_NONZERO(x) (32 - __builtin_clz(x))
59
+ #endif
60
+ #define JPEG_NBITS(x) (x ? JPEG_NBITS_NONZERO(x) : 0)
61
+ #else
62
+ #include "jpeg_nbits_table.h"
63
+ #define JPEG_NBITS(x) (jpeg_nbits_table[x])
64
+ #define JPEG_NBITS_NONZERO(x) JPEG_NBITS(x)
65
+ #endif
66
+
67
+
68
+ /* Expanded entropy encoder object for Huffman encoding.
69
+ *
70
+ * The savable_state subrecord contains fields that change within an MCU,
71
+ * but must not be updated permanently until we complete the MCU.
72
+ */
73
+
74
+ #if defined(__x86_64__) && defined(__ILP32__)
75
+ typedef unsigned long long bit_buf_type;
76
+ #else
77
+ typedef size_t bit_buf_type;
78
+ #endif
79
+
80
+ /* NOTE: The more optimal Huffman encoding algorithm is only used by the
81
+ * intrinsics implementation of the Arm Neon SIMD extensions, which is why we
82
+ * retain the old Huffman encoder behavior when using the GAS implementation.
83
+ */
84
+ #if defined(WITH_SIMD) && !(defined(__arm__) || defined(__aarch64__) || \
85
+ defined(_M_ARM) || defined(_M_ARM64))
86
+ typedef unsigned long long simd_bit_buf_type;
87
+ #else
88
+ typedef bit_buf_type simd_bit_buf_type;
89
+ #endif
90
+
91
+ #if (defined(SIZEOF_SIZE_T) && SIZEOF_SIZE_T == 8) || defined(_WIN64) || \
92
+ (defined(__x86_64__) && defined(__ILP32__))
93
+ #define BIT_BUF_SIZE 64
94
+ #elif (defined(SIZEOF_SIZE_T) && SIZEOF_SIZE_T == 4) || defined(_WIN32)
95
+ #define BIT_BUF_SIZE 32
96
+ #else
97
+ #error Cannot determine word size
98
+ #endif
99
+ #define SIMD_BIT_BUF_SIZE (sizeof(simd_bit_buf_type) * 8)
100
+
101
+ typedef struct {
102
+ union {
103
+ bit_buf_type c;
104
+ simd_bit_buf_type simd;
105
+ } put_buffer; /* current bit accumulation buffer */
106
+ int free_bits; /* # of bits available in it */
107
+ /* (Neon GAS: # of bits now in it) */
108
+ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
109
+ } savable_state;
110
+
111
+ typedef struct {
112
+ struct jpeg_entropy_encoder pub; /* public fields */
113
+
114
+ savable_state saved; /* Bit buffer & DC state at start of MCU */
115
+
116
+ /* These fields are NOT loaded into local working state. */
117
+ unsigned int restarts_to_go; /* MCUs left in this restart interval */
118
+ int next_restart_num; /* next restart number to write (0-7) */
119
+
120
+ /* Pointers to derived tables (these workspaces have image lifespan) */
121
+ c_derived_tbl *dc_derived_tbls[NUM_HUFF_TBLS];
122
+ c_derived_tbl *ac_derived_tbls[NUM_HUFF_TBLS];
123
+
124
+ #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
125
+ long *dc_count_ptrs[NUM_HUFF_TBLS];
126
+ long *ac_count_ptrs[NUM_HUFF_TBLS];
127
+ #endif
128
+
129
+ int simd;
130
+ } huff_entropy_encoder;
131
+
132
+ typedef huff_entropy_encoder *huff_entropy_ptr;
133
+
134
+ /* Working state while writing an MCU.
135
+ * This struct contains all the fields that are needed by subroutines.
136
+ */
137
+
138
+ typedef struct {
139
+ JOCTET *next_output_byte; /* => next byte to write in buffer */
140
+ size_t free_in_buffer; /* # of byte spaces remaining in buffer */
141
+ savable_state cur; /* Current bit buffer & DC state */
142
+ j_compress_ptr cinfo; /* dump_buffer needs access to this */
143
+ int simd;
144
+ } working_state;
145
+
146
+
147
+ /* Forward declarations */
148
+ METHODDEF(boolean) encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data);
149
+ METHODDEF(void) finish_pass_huff(j_compress_ptr cinfo);
150
+ #ifdef ENTROPY_OPT_SUPPORTED
151
+ METHODDEF(boolean) encode_mcu_gather(j_compress_ptr cinfo,
152
+ JBLOCKROW *MCU_data);
153
+ METHODDEF(void) finish_pass_gather(j_compress_ptr cinfo);
154
+ #endif
155
+
156
+
157
+ /*
158
+ * Initialize for a Huffman-compressed scan.
159
+ * If gather_statistics is TRUE, we do not output anything during the scan,
160
+ * just count the Huffman symbols used and generate Huffman code tables.
161
+ */
162
+
163
+ METHODDEF(void)
164
+ start_pass_huff(j_compress_ptr cinfo, boolean gather_statistics)
165
+ {
166
+ huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
167
+ int ci, dctbl, actbl;
168
+ jpeg_component_info *compptr;
169
+
170
+ if (gather_statistics) {
171
+ #ifdef ENTROPY_OPT_SUPPORTED
172
+ entropy->pub.encode_mcu = encode_mcu_gather;
173
+ entropy->pub.finish_pass = finish_pass_gather;
174
+ #else
175
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
176
+ #endif
177
+ } else {
178
+ entropy->pub.encode_mcu = encode_mcu_huff;
179
+ entropy->pub.finish_pass = finish_pass_huff;
180
+ }
181
+
182
+ entropy->simd = jsimd_can_huff_encode_one_block();
183
+
184
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
185
+ compptr = cinfo->cur_comp_info[ci];
186
+ dctbl = compptr->dc_tbl_no;
187
+ actbl = compptr->ac_tbl_no;
188
+ if (gather_statistics) {
189
+ #ifdef ENTROPY_OPT_SUPPORTED
190
+ /* Check for invalid table indexes */
191
+ /* (make_c_derived_tbl does this in the other path) */
192
+ if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
193
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
194
+ if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
195
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
196
+ /* Allocate and zero the statistics tables */
197
+ /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
198
+ if (entropy->dc_count_ptrs[dctbl] == NULL)
199
+ entropy->dc_count_ptrs[dctbl] = (long *)
200
+ (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
201
+ 257 * sizeof(long));
202
+ memset(entropy->dc_count_ptrs[dctbl], 0, 257 * sizeof(long));
203
+ if (entropy->ac_count_ptrs[actbl] == NULL)
204
+ entropy->ac_count_ptrs[actbl] = (long *)
205
+ (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
206
+ 257 * sizeof(long));
207
+ memset(entropy->ac_count_ptrs[actbl], 0, 257 * sizeof(long));
208
+ #endif
209
+ } else {
210
+ /* Compute derived values for Huffman tables */
211
+ /* We may do this more than once for a table, but it's not expensive */
212
+ jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
213
+ &entropy->dc_derived_tbls[dctbl]);
214
+ jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
215
+ &entropy->ac_derived_tbls[actbl]);
216
+ }
217
+ /* Initialize DC predictions to 0 */
218
+ entropy->saved.last_dc_val[ci] = 0;
219
+ }
220
+
221
+ /* Initialize bit buffer to empty */
222
+ if (entropy->simd) {
223
+ entropy->saved.put_buffer.simd = 0;
224
+ #if defined(__aarch64__) && !defined(NEON_INTRINSICS)
225
+ entropy->saved.free_bits = 0;
226
+ #else
227
+ entropy->saved.free_bits = SIMD_BIT_BUF_SIZE;
228
+ #endif
229
+ } else {
230
+ entropy->saved.put_buffer.c = 0;
231
+ entropy->saved.free_bits = BIT_BUF_SIZE;
232
+ }
233
+
234
+ /* Initialize restart stuff */
235
+ entropy->restarts_to_go = cinfo->restart_interval;
236
+ entropy->next_restart_num = 0;
237
+ }
238
+
239
+
240
+ /*
241
+ * Compute the derived values for a Huffman table.
242
+ * This routine also performs some validation checks on the table.
243
+ *
244
+ * Note this is also used by jcphuff.c.
245
+ */
246
+
247
+ GLOBAL(void)
248
+ jpeg_make_c_derived_tbl(j_compress_ptr cinfo, boolean isDC, int tblno,
249
+ c_derived_tbl **pdtbl)
250
+ {
251
+ JHUFF_TBL *htbl;
252
+ c_derived_tbl *dtbl;
253
+ int p, i, l, lastp, si, maxsymbol;
254
+ char huffsize[257];
255
+ unsigned int huffcode[257];
256
+ unsigned int code;
257
+
258
+ /* Note that huffsize[] and huffcode[] are filled in code-length order,
259
+ * paralleling the order of the symbols themselves in htbl->huffval[].
260
+ */
261
+
262
+ /* Find the input Huffman table */
263
+ if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
264
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
265
+ htbl =
266
+ isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
267
+ if (htbl == NULL)
268
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
269
+
270
+ /* Allocate a workspace if we haven't already done so. */
271
+ if (*pdtbl == NULL)
272
+ *pdtbl = (c_derived_tbl *)
273
+ (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
274
+ sizeof(c_derived_tbl));
275
+ dtbl = *pdtbl;
276
+
277
+ /* Figure C.1: make table of Huffman code length for each symbol */
278
+
279
+ p = 0;
280
+ for (l = 1; l <= 16; l++) {
281
+ i = (int)htbl->bits[l];
282
+ if (i < 0 || p + i > 256) /* protect against table overrun */
283
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
284
+ while (i--)
285
+ huffsize[p++] = (char)l;
286
+ }
287
+ huffsize[p] = 0;
288
+ lastp = p;
289
+
290
+ /* Figure C.2: generate the codes themselves */
291
+ /* We also validate that the counts represent a legal Huffman code tree. */
292
+
293
+ code = 0;
294
+ si = huffsize[0];
295
+ p = 0;
296
+ while (huffsize[p]) {
297
+ while (((int)huffsize[p]) == si) {
298
+ huffcode[p++] = code;
299
+ code++;
300
+ }
301
+ /* code is now 1 more than the last code used for codelength si; but
302
+ * it must still fit in si bits, since no code is allowed to be all ones.
303
+ */
304
+ if (((JLONG)code) >= (((JLONG)1) << si))
305
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
306
+ code <<= 1;
307
+ si++;
308
+ }
309
+
310
+ /* Figure C.3: generate encoding tables */
311
+ /* These are code and size indexed by symbol value */
312
+
313
+ /* Set all codeless symbols to have code length 0;
314
+ * this lets us detect duplicate VAL entries here, and later
315
+ * allows emit_bits to detect any attempt to emit such symbols.
316
+ */
317
+ memset(dtbl->ehufco, 0, sizeof(dtbl->ehufco));
318
+ memset(dtbl->ehufsi, 0, sizeof(dtbl->ehufsi));
319
+
320
+ /* This is also a convenient place to check for out-of-range
321
+ * and duplicated VAL entries. We allow 0..255 for AC symbols
322
+ * but only 0..15 for DC. (We could constrain them further
323
+ * based on data depth and mode, but this seems enough.)
324
+ */
325
+ maxsymbol = isDC ? 15 : 255;
326
+
327
+ for (p = 0; p < lastp; p++) {
328
+ i = htbl->huffval[p];
329
+ if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
330
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
331
+ dtbl->ehufco[i] = huffcode[p];
332
+ dtbl->ehufsi[i] = huffsize[p];
333
+ }
334
+ }
335
+
336
+
337
+ /* Outputting bytes to the file */
338
+
339
+ /* Emit a byte, taking 'action' if must suspend. */
340
+ #define emit_byte(state, val, action) { \
341
+ *(state)->next_output_byte++ = (JOCTET)(val); \
342
+ if (--(state)->free_in_buffer == 0) \
343
+ if (!dump_buffer(state)) \
344
+ { action; } \
345
+ }
346
+
347
+
348
+ LOCAL(boolean)
349
+ dump_buffer(working_state *state)
350
+ /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
351
+ {
352
+ struct jpeg_destination_mgr *dest = state->cinfo->dest;
353
+
354
+ if (!(*dest->empty_output_buffer) (state->cinfo))
355
+ return FALSE;
356
+ /* After a successful buffer dump, must reset buffer pointers */
357
+ state->next_output_byte = dest->next_output_byte;
358
+ state->free_in_buffer = dest->free_in_buffer;
359
+ return TRUE;
360
+ }
361
+
362
+
363
+ /* Outputting bits to the file */
364
+
365
+ /* Output byte b and, speculatively, an additional 0 byte. 0xFF must be
366
+ * encoded as 0xFF 0x00, so the output buffer pointer is advanced by 2 if the
367
+ * byte is 0xFF. Otherwise, the output buffer pointer is advanced by 1, and
368
+ * the speculative 0 byte will be overwritten by the next byte.
369
+ */
370
+ #define EMIT_BYTE(b) { \
371
+ buffer[0] = (JOCTET)(b); \
372
+ buffer[1] = 0; \
373
+ buffer -= -2 + ((JOCTET)(b) < 0xFF); \
374
+ }
375
+
376
+ /* Output the entire bit buffer. If there are no 0xFF bytes in it, then write
377
+ * directly to the output buffer. Otherwise, use the EMIT_BYTE() macro to
378
+ * encode 0xFF as 0xFF 0x00.
379
+ */
380
+ #if BIT_BUF_SIZE == 64
381
+
382
+ #define FLUSH() { \
383
+ if (put_buffer & 0x8080808080808080 & ~(put_buffer + 0x0101010101010101)) { \
384
+ EMIT_BYTE(put_buffer >> 56) \
385
+ EMIT_BYTE(put_buffer >> 48) \
386
+ EMIT_BYTE(put_buffer >> 40) \
387
+ EMIT_BYTE(put_buffer >> 32) \
388
+ EMIT_BYTE(put_buffer >> 24) \
389
+ EMIT_BYTE(put_buffer >> 16) \
390
+ EMIT_BYTE(put_buffer >> 8) \
391
+ EMIT_BYTE(put_buffer ) \
392
+ } else { \
393
+ buffer[0] = (JOCTET)(put_buffer >> 56); \
394
+ buffer[1] = (JOCTET)(put_buffer >> 48); \
395
+ buffer[2] = (JOCTET)(put_buffer >> 40); \
396
+ buffer[3] = (JOCTET)(put_buffer >> 32); \
397
+ buffer[4] = (JOCTET)(put_buffer >> 24); \
398
+ buffer[5] = (JOCTET)(put_buffer >> 16); \
399
+ buffer[6] = (JOCTET)(put_buffer >> 8); \
400
+ buffer[7] = (JOCTET)(put_buffer); \
401
+ buffer += 8; \
402
+ } \
403
+ }
404
+
405
+ #else
406
+
407
+ #define FLUSH() { \
408
+ if (put_buffer & 0x80808080 & ~(put_buffer + 0x01010101)) { \
409
+ EMIT_BYTE(put_buffer >> 24) \
410
+ EMIT_BYTE(put_buffer >> 16) \
411
+ EMIT_BYTE(put_buffer >> 8) \
412
+ EMIT_BYTE(put_buffer ) \
413
+ } else { \
414
+ buffer[0] = (JOCTET)(put_buffer >> 24); \
415
+ buffer[1] = (JOCTET)(put_buffer >> 16); \
416
+ buffer[2] = (JOCTET)(put_buffer >> 8); \
417
+ buffer[3] = (JOCTET)(put_buffer); \
418
+ buffer += 4; \
419
+ } \
420
+ }
421
+
422
+ #endif
423
+
424
+ /* Fill the bit buffer to capacity with the leading bits from code, then output
425
+ * the bit buffer and put the remaining bits from code into the bit buffer.
426
+ */
427
+ #define PUT_AND_FLUSH(code, size) { \
428
+ put_buffer = (put_buffer << (size + free_bits)) | (code >> -free_bits); \
429
+ FLUSH() \
430
+ free_bits += BIT_BUF_SIZE; \
431
+ put_buffer = code; \
432
+ }
433
+
434
+ /* Insert code into the bit buffer and output the bit buffer if needed.
435
+ * NOTE: We can't flush with free_bits == 0, since the left shift in
436
+ * PUT_AND_FLUSH() would have undefined behavior.
437
+ */
438
+ #define PUT_BITS(code, size) { \
439
+ free_bits -= size; \
440
+ if (free_bits < 0) \
441
+ PUT_AND_FLUSH(code, size) \
442
+ else \
443
+ put_buffer = (put_buffer << size) | code; \
444
+ }
445
+
446
+ #define PUT_CODE(code, size) { \
447
+ temp &= (((JLONG)1) << nbits) - 1; \
448
+ temp |= code << nbits; \
449
+ nbits += size; \
450
+ PUT_BITS(temp, nbits) \
451
+ }
452
+
453
+
454
+ /* Although it is exceedingly rare, it is possible for a Huffman-encoded
455
+ * coefficient block to be larger than the 128-byte unencoded block. For each
456
+ * of the 64 coefficients, PUT_BITS is invoked twice, and each invocation can
457
+ * theoretically store 16 bits (for a maximum of 2048 bits or 256 bytes per
458
+ * encoded block.) If, for instance, one artificially sets the AC
459
+ * coefficients to alternating values of 32767 and -32768 (using the JPEG
460
+ * scanning order-- 1, 8, 16, etc.), then this will produce an encoded block
461
+ * larger than 200 bytes.
462
+ */
463
+ #define BUFSIZE (DCTSIZE2 * 8)
464
+
465
+ #define LOAD_BUFFER() { \
466
+ if (state->free_in_buffer < BUFSIZE) { \
467
+ localbuf = 1; \
468
+ buffer = _buffer; \
469
+ } else \
470
+ buffer = state->next_output_byte; \
471
+ }
472
+
473
+ #define STORE_BUFFER() { \
474
+ if (localbuf) { \
475
+ size_t bytes, bytestocopy; \
476
+ bytes = buffer - _buffer; \
477
+ buffer = _buffer; \
478
+ while (bytes > 0) { \
479
+ bytestocopy = MIN(bytes, state->free_in_buffer); \
480
+ memcpy(state->next_output_byte, buffer, bytestocopy); \
481
+ state->next_output_byte += bytestocopy; \
482
+ buffer += bytestocopy; \
483
+ state->free_in_buffer -= bytestocopy; \
484
+ if (state->free_in_buffer == 0) \
485
+ if (!dump_buffer(state)) return FALSE; \
486
+ bytes -= bytestocopy; \
487
+ } \
488
+ } else { \
489
+ state->free_in_buffer -= (buffer - state->next_output_byte); \
490
+ state->next_output_byte = buffer; \
491
+ } \
492
+ }
493
+
494
+
495
+ LOCAL(boolean)
496
+ flush_bits(working_state *state)
497
+ {
498
+ JOCTET _buffer[BUFSIZE], *buffer, temp;
499
+ simd_bit_buf_type put_buffer; int put_bits;
500
+ int localbuf = 0;
501
+
502
+ if (state->simd) {
503
+ if (state->cur.free_bits < 0)
504
+ ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
505
+ #if defined(__aarch64__) && !defined(NEON_INTRINSICS)
506
+ put_bits = state->cur.free_bits;
507
+ #else
508
+ put_bits = SIMD_BIT_BUF_SIZE - state->cur.free_bits;
509
+ #endif
510
+ put_buffer = state->cur.put_buffer.simd;
511
+ } else {
512
+ put_bits = BIT_BUF_SIZE - state->cur.free_bits;
513
+ put_buffer = state->cur.put_buffer.c;
514
+ }
515
+
516
+ LOAD_BUFFER()
517
+
518
+ while (put_bits >= 8) {
519
+ put_bits -= 8;
520
+ temp = (JOCTET)(put_buffer >> put_bits);
521
+ EMIT_BYTE(temp)
522
+ }
523
+ if (put_bits > 0) {
524
+ /* fill partial byte with ones */
525
+ temp = (JOCTET)((put_buffer << (8 - put_bits)) | (0xFF >> put_bits));
526
+ EMIT_BYTE(temp)
527
+ }
528
+
529
+ if (state->simd) { /* and reset bit buffer to empty */
530
+ state->cur.put_buffer.simd = 0;
531
+ #if defined(__aarch64__) && !defined(NEON_INTRINSICS)
532
+ state->cur.free_bits = 0;
533
+ #else
534
+ state->cur.free_bits = SIMD_BIT_BUF_SIZE;
535
+ #endif
536
+ } else {
537
+ state->cur.put_buffer.c = 0;
538
+ state->cur.free_bits = BIT_BUF_SIZE;
539
+ }
540
+ STORE_BUFFER()
541
+
542
+ return TRUE;
543
+ }
544
+
545
+
546
+ /* Encode a single block's worth of coefficients */
547
+
548
+ LOCAL(boolean)
549
+ encode_one_block_simd(working_state *state, JCOEFPTR block, int last_dc_val,
550
+ c_derived_tbl *dctbl, c_derived_tbl *actbl)
551
+ {
552
+ JOCTET _buffer[BUFSIZE], *buffer;
553
+ int localbuf = 0;
554
+
555
+ LOAD_BUFFER()
556
+
557
+ buffer = jsimd_huff_encode_one_block(state, buffer, block, last_dc_val,
558
+ dctbl, actbl);
559
+
560
+ STORE_BUFFER()
561
+
562
+ return TRUE;
563
+ }
564
+
565
+ LOCAL(boolean)
566
+ encode_one_block(working_state *state, JCOEFPTR block, int last_dc_val,
567
+ c_derived_tbl *dctbl, c_derived_tbl *actbl)
568
+ {
569
+ int temp, nbits, free_bits;
570
+ bit_buf_type put_buffer;
571
+ JOCTET _buffer[BUFSIZE], *buffer;
572
+ int localbuf = 0;
573
+
574
+ free_bits = state->cur.free_bits;
575
+ put_buffer = state->cur.put_buffer.c;
576
+ LOAD_BUFFER()
577
+
578
+ /* Encode the DC coefficient difference per section F.1.2.1 */
579
+
580
+ temp = block[0] - last_dc_val;
581
+
582
+ /* This is a well-known technique for obtaining the absolute value without a
583
+ * branch. It is derived from an assembly language technique presented in
584
+ * "How to Optimize for the Pentium Processors", Copyright (c) 1996, 1997 by
585
+ * Agner Fog. This code assumes we are on a two's complement machine.
586
+ */
587
+ nbits = temp >> (CHAR_BIT * sizeof(int) - 1);
588
+ temp += nbits;
589
+ nbits ^= temp;
590
+
591
+ /* Find the number of bits needed for the magnitude of the coefficient */
592
+ nbits = JPEG_NBITS(nbits);
593
+ /* Check for out-of-range coefficient values.
594
+ * Since we're encoding a difference, the range limit is twice as much.
595
+ */
596
+ if (nbits > MAX_COEF_BITS + 1)
597
+ ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
598
+
599
+ /* Emit the Huffman-coded symbol for the number of bits.
600
+ * Emit that number of bits of the value, if positive,
601
+ * or the complement of its magnitude, if negative.
602
+ */
603
+ PUT_CODE(dctbl->ehufco[nbits], dctbl->ehufsi[nbits])
604
+
605
+ /* Encode the AC coefficients per section F.1.2.2 */
606
+
607
+ {
608
+ int r = 0; /* r = run length of zeros */
609
+
610
+ /* Manually unroll the k loop to eliminate the counter variable. This
611
+ * improves performance greatly on systems with a limited number of
612
+ * registers (such as x86.)
613
+ */
614
+ #define kloop(jpeg_natural_order_of_k) { \
615
+ if ((temp = block[jpeg_natural_order_of_k]) == 0) { \
616
+ r += 16; \
617
+ } else { \
618
+ /* Branch-less absolute value, bitwise complement, etc., same as above */ \
619
+ nbits = temp >> (CHAR_BIT * sizeof(int) - 1); \
620
+ temp += nbits; \
621
+ nbits ^= temp; \
622
+ nbits = JPEG_NBITS_NONZERO(nbits); \
623
+ /* Check for out-of-range coefficient values */ \
624
+ if (nbits > MAX_COEF_BITS) \
625
+ ERREXIT(state->cinfo, JERR_BAD_DCT_COEF); \
626
+ /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \
627
+ while (r >= 16 * 16) { \
628
+ r -= 16 * 16; \
629
+ PUT_BITS(actbl->ehufco[0xf0], actbl->ehufsi[0xf0]) \
630
+ } \
631
+ /* Emit Huffman symbol for run length / number of bits */ \
632
+ r += nbits; \
633
+ PUT_CODE(actbl->ehufco[r], actbl->ehufsi[r]) \
634
+ r = 0; \
635
+ } \
636
+ }
637
+
638
+ /* One iteration for each value in jpeg_natural_order[] */
639
+ kloop(1); kloop(8); kloop(16); kloop(9); kloop(2); kloop(3);
640
+ kloop(10); kloop(17); kloop(24); kloop(32); kloop(25); kloop(18);
641
+ kloop(11); kloop(4); kloop(5); kloop(12); kloop(19); kloop(26);
642
+ kloop(33); kloop(40); kloop(48); kloop(41); kloop(34); kloop(27);
643
+ kloop(20); kloop(13); kloop(6); kloop(7); kloop(14); kloop(21);
644
+ kloop(28); kloop(35); kloop(42); kloop(49); kloop(56); kloop(57);
645
+ kloop(50); kloop(43); kloop(36); kloop(29); kloop(22); kloop(15);
646
+ kloop(23); kloop(30); kloop(37); kloop(44); kloop(51); kloop(58);
647
+ kloop(59); kloop(52); kloop(45); kloop(38); kloop(31); kloop(39);
648
+ kloop(46); kloop(53); kloop(60); kloop(61); kloop(54); kloop(47);
649
+ kloop(55); kloop(62); kloop(63);
650
+
651
+ /* If the last coef(s) were zero, emit an end-of-block code */
652
+ if (r > 0) {
653
+ PUT_BITS(actbl->ehufco[0], actbl->ehufsi[0])
654
+ }
655
+ }
656
+
657
+ state->cur.put_buffer.c = put_buffer;
658
+ state->cur.free_bits = free_bits;
659
+ STORE_BUFFER()
660
+
661
+ return TRUE;
662
+ }
663
+
664
+
665
+ /*
666
+ * Emit a restart marker & resynchronize predictions.
667
+ */
668
+
669
+ LOCAL(boolean)
670
+ emit_restart(working_state *state, int restart_num)
671
+ {
672
+ int ci;
673
+
674
+ if (!flush_bits(state))
675
+ return FALSE;
676
+
677
+ emit_byte(state, 0xFF, return FALSE);
678
+ emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
679
+
680
+ /* Re-initialize DC predictions to 0 */
681
+ for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
682
+ state->cur.last_dc_val[ci] = 0;
683
+
684
+ /* The restart counter is not updated until we successfully write the MCU. */
685
+
686
+ return TRUE;
687
+ }
688
+
689
+
690
+ /*
691
+ * Encode and output one MCU's worth of Huffman-compressed coefficients.
692
+ */
693
+
694
+ METHODDEF(boolean)
695
+ encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
696
+ {
697
+ huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
698
+ working_state state;
699
+ int blkn, ci;
700
+ jpeg_component_info *compptr;
701
+
702
+ /* Load up working state */
703
+ state.next_output_byte = cinfo->dest->next_output_byte;
704
+ state.free_in_buffer = cinfo->dest->free_in_buffer;
705
+ state.cur = entropy->saved;
706
+ state.cinfo = cinfo;
707
+ state.simd = entropy->simd;
708
+
709
+ /* Emit restart marker if needed */
710
+ if (cinfo->restart_interval) {
711
+ if (entropy->restarts_to_go == 0)
712
+ if (!emit_restart(&state, entropy->next_restart_num))
713
+ return FALSE;
714
+ }
715
+
716
+ /* Encode the MCU data blocks */
717
+ if (entropy->simd) {
718
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
719
+ ci = cinfo->MCU_membership[blkn];
720
+ compptr = cinfo->cur_comp_info[ci];
721
+ if (!encode_one_block_simd(&state,
722
+ MCU_data[blkn][0], state.cur.last_dc_val[ci],
723
+ entropy->dc_derived_tbls[compptr->dc_tbl_no],
724
+ entropy->ac_derived_tbls[compptr->ac_tbl_no]))
725
+ return FALSE;
726
+ /* Update last_dc_val */
727
+ state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
728
+ }
729
+ } else {
730
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
731
+ ci = cinfo->MCU_membership[blkn];
732
+ compptr = cinfo->cur_comp_info[ci];
733
+ if (!encode_one_block(&state,
734
+ MCU_data[blkn][0], state.cur.last_dc_val[ci],
735
+ entropy->dc_derived_tbls[compptr->dc_tbl_no],
736
+ entropy->ac_derived_tbls[compptr->ac_tbl_no]))
737
+ return FALSE;
738
+ /* Update last_dc_val */
739
+ state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
740
+ }
741
+ }
742
+
743
+ /* Completed MCU, so update state */
744
+ cinfo->dest->next_output_byte = state.next_output_byte;
745
+ cinfo->dest->free_in_buffer = state.free_in_buffer;
746
+ entropy->saved = state.cur;
747
+
748
+ /* Update restart-interval state too */
749
+ if (cinfo->restart_interval) {
750
+ if (entropy->restarts_to_go == 0) {
751
+ entropy->restarts_to_go = cinfo->restart_interval;
752
+ entropy->next_restart_num++;
753
+ entropy->next_restart_num &= 7;
754
+ }
755
+ entropy->restarts_to_go--;
756
+ }
757
+
758
+ return TRUE;
759
+ }
760
+
761
+
762
+ /*
763
+ * Finish up at the end of a Huffman-compressed scan.
764
+ */
765
+
766
+ METHODDEF(void)
767
+ finish_pass_huff(j_compress_ptr cinfo)
768
+ {
769
+ huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
770
+ working_state state;
771
+
772
+ /* Load up working state ... flush_bits needs it */
773
+ state.next_output_byte = cinfo->dest->next_output_byte;
774
+ state.free_in_buffer = cinfo->dest->free_in_buffer;
775
+ state.cur = entropy->saved;
776
+ state.cinfo = cinfo;
777
+ state.simd = entropy->simd;
778
+
779
+ /* Flush out the last data */
780
+ if (!flush_bits(&state))
781
+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
782
+
783
+ /* Update state */
784
+ cinfo->dest->next_output_byte = state.next_output_byte;
785
+ cinfo->dest->free_in_buffer = state.free_in_buffer;
786
+ entropy->saved = state.cur;
787
+ }
788
+
789
+
790
+ /*
791
+ * Huffman coding optimization.
792
+ *
793
+ * We first scan the supplied data and count the number of uses of each symbol
794
+ * that is to be Huffman-coded. (This process MUST agree with the code above.)
795
+ * Then we build a Huffman coding tree for the observed counts.
796
+ * Symbols which are not needed at all for the particular image are not
797
+ * assigned any code, which saves space in the DHT marker as well as in
798
+ * the compressed data.
799
+ */
800
+
801
+ #ifdef ENTROPY_OPT_SUPPORTED
802
+
803
+
804
+ /* Process a single block's worth of coefficients */
805
+
806
+ LOCAL(void)
807
+ htest_one_block(j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
808
+ long dc_counts[], long ac_counts[])
809
+ {
810
+ register int temp;
811
+ register int nbits;
812
+ register int k, r;
813
+
814
+ /* Encode the DC coefficient difference per section F.1.2.1 */
815
+
816
+ temp = block[0] - last_dc_val;
817
+ if (temp < 0)
818
+ temp = -temp;
819
+
820
+ /* Find the number of bits needed for the magnitude of the coefficient */
821
+ nbits = 0;
822
+ while (temp) {
823
+ nbits++;
824
+ temp >>= 1;
825
+ }
826
+ /* Check for out-of-range coefficient values.
827
+ * Since we're encoding a difference, the range limit is twice as much.
828
+ */
829
+ if (nbits > MAX_COEF_BITS + 1)
830
+ ERREXIT(cinfo, JERR_BAD_DCT_COEF);
831
+
832
+ /* Count the Huffman symbol for the number of bits */
833
+ dc_counts[nbits]++;
834
+
835
+ /* Encode the AC coefficients per section F.1.2.2 */
836
+
837
+ r = 0; /* r = run length of zeros */
838
+
839
+ for (k = 1; k < DCTSIZE2; k++) {
840
+ if ((temp = block[jpeg_natural_order[k]]) == 0) {
841
+ r++;
842
+ } else {
843
+ /* if run length > 15, must emit special run-length-16 codes (0xF0) */
844
+ while (r > 15) {
845
+ ac_counts[0xF0]++;
846
+ r -= 16;
847
+ }
848
+
849
+ /* Find the number of bits needed for the magnitude of the coefficient */
850
+ if (temp < 0)
851
+ temp = -temp;
852
+
853
+ /* Find the number of bits needed for the magnitude of the coefficient */
854
+ nbits = 1; /* there must be at least one 1 bit */
855
+ while ((temp >>= 1))
856
+ nbits++;
857
+ /* Check for out-of-range coefficient values */
858
+ if (nbits > MAX_COEF_BITS)
859
+ ERREXIT(cinfo, JERR_BAD_DCT_COEF);
860
+
861
+ /* Count Huffman symbol for run length / number of bits */
862
+ ac_counts[(r << 4) + nbits]++;
863
+
864
+ r = 0;
865
+ }
866
+ }
867
+
868
+ /* If the last coef(s) were zero, emit an end-of-block code */
869
+ if (r > 0)
870
+ ac_counts[0]++;
871
+ }
872
+
873
+
874
+ /*
875
+ * Trial-encode one MCU's worth of Huffman-compressed coefficients.
876
+ * No data is actually output, so no suspension return is possible.
877
+ */
878
+
879
+ METHODDEF(boolean)
880
+ encode_mcu_gather(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
881
+ {
882
+ huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
883
+ int blkn, ci;
884
+ jpeg_component_info *compptr;
885
+
886
+ /* Take care of restart intervals if needed */
887
+ if (cinfo->restart_interval) {
888
+ if (entropy->restarts_to_go == 0) {
889
+ /* Re-initialize DC predictions to 0 */
890
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++)
891
+ entropy->saved.last_dc_val[ci] = 0;
892
+ /* Update restart state */
893
+ entropy->restarts_to_go = cinfo->restart_interval;
894
+ }
895
+ entropy->restarts_to_go--;
896
+ }
897
+
898
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
899
+ ci = cinfo->MCU_membership[blkn];
900
+ compptr = cinfo->cur_comp_info[ci];
901
+ htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
902
+ entropy->dc_count_ptrs[compptr->dc_tbl_no],
903
+ entropy->ac_count_ptrs[compptr->ac_tbl_no]);
904
+ entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
905
+ }
906
+
907
+ return TRUE;
908
+ }
909
+
910
+
911
+ /*
912
+ * Generate the best Huffman code table for the given counts, fill htbl.
913
+ * Note this is also used by jcphuff.c.
914
+ *
915
+ * The JPEG standard requires that no symbol be assigned a codeword of all
916
+ * one bits (so that padding bits added at the end of a compressed segment
917
+ * can't look like a valid code). Because of the canonical ordering of
918
+ * codewords, this just means that there must be an unused slot in the
919
+ * longest codeword length category. Annex K (Clause K.2) of
920
+ * Rec. ITU-T T.81 (1992) | ISO/IEC 10918-1:1994 suggests reserving such a slot
921
+ * by pretending that symbol 256 is a valid symbol with count 1. In theory
922
+ * that's not optimal; giving it count zero but including it in the symbol set
923
+ * anyway should give a better Huffman code. But the theoretically better code
924
+ * actually seems to come out worse in practice, because it produces more
925
+ * all-ones bytes (which incur stuffed zero bytes in the final file). In any
926
+ * case the difference is tiny.
927
+ *
928
+ * The JPEG standard requires Huffman codes to be no more than 16 bits long.
929
+ * If some symbols have a very small but nonzero probability, the Huffman tree
930
+ * must be adjusted to meet the code length restriction. We currently use
931
+ * the adjustment method suggested in JPEG section K.2. This method is *not*
932
+ * optimal; it may not choose the best possible limited-length code. But
933
+ * typically only very-low-frequency symbols will be given less-than-optimal
934
+ * lengths, so the code is almost optimal. Experimental comparisons against
935
+ * an optimal limited-length-code algorithm indicate that the difference is
936
+ * microscopic --- usually less than a hundredth of a percent of total size.
937
+ * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
938
+ */
939
+
940
+ GLOBAL(void)
941
+ jpeg_gen_optimal_table(j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[])
942
+ {
943
+ #define MAX_CLEN 32 /* assumed maximum initial code length */
944
+ UINT8 bits[MAX_CLEN + 1]; /* bits[k] = # of symbols with code length k */
945
+ int codesize[257]; /* codesize[k] = code length of symbol k */
946
+ int others[257]; /* next symbol in current branch of tree */
947
+ int c1, c2;
948
+ int p, i, j;
949
+ long v;
950
+
951
+ /* This algorithm is explained in section K.2 of the JPEG standard */
952
+
953
+ memset(bits, 0, sizeof(bits));
954
+ memset(codesize, 0, sizeof(codesize));
955
+ for (i = 0; i < 257; i++)
956
+ others[i] = -1; /* init links to empty */
957
+
958
+ freq[256] = 1; /* make sure 256 has a nonzero count */
959
+ /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
960
+ * that no real symbol is given code-value of all ones, because 256
961
+ * will be placed last in the largest codeword category.
962
+ */
963
+
964
+ /* Huffman's basic algorithm to assign optimal code lengths to symbols */
965
+
966
+ for (;;) {
967
+ /* Find the smallest nonzero frequency, set c1 = its symbol */
968
+ /* In case of ties, take the larger symbol number */
969
+ c1 = -1;
970
+ v = 1000000000L;
971
+ for (i = 0; i <= 256; i++) {
972
+ if (freq[i] && freq[i] <= v) {
973
+ v = freq[i];
974
+ c1 = i;
975
+ }
976
+ }
977
+
978
+ /* Find the next smallest nonzero frequency, set c2 = its symbol */
979
+ /* In case of ties, take the larger symbol number */
980
+ c2 = -1;
981
+ v = 1000000000L;
982
+ for (i = 0; i <= 256; i++) {
983
+ if (freq[i] && freq[i] <= v && i != c1) {
984
+ v = freq[i];
985
+ c2 = i;
986
+ }
987
+ }
988
+
989
+ /* Done if we've merged everything into one frequency */
990
+ if (c2 < 0)
991
+ break;
992
+
993
+ /* Else merge the two counts/trees */
994
+ freq[c1] += freq[c2];
995
+ freq[c2] = 0;
996
+
997
+ /* Increment the codesize of everything in c1's tree branch */
998
+ codesize[c1]++;
999
+ while (others[c1] >= 0) {
1000
+ c1 = others[c1];
1001
+ codesize[c1]++;
1002
+ }
1003
+
1004
+ others[c1] = c2; /* chain c2 onto c1's tree branch */
1005
+
1006
+ /* Increment the codesize of everything in c2's tree branch */
1007
+ codesize[c2]++;
1008
+ while (others[c2] >= 0) {
1009
+ c2 = others[c2];
1010
+ codesize[c2]++;
1011
+ }
1012
+ }
1013
+
1014
+ /* Now count the number of symbols of each code length */
1015
+ for (i = 0; i <= 256; i++) {
1016
+ if (codesize[i]) {
1017
+ /* The JPEG standard seems to think that this can't happen, */
1018
+ /* but I'm paranoid... */
1019
+ if (codesize[i] > MAX_CLEN)
1020
+ ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
1021
+
1022
+ bits[codesize[i]]++;
1023
+ }
1024
+ }
1025
+
1026
+ /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
1027
+ * Huffman procedure assigned any such lengths, we must adjust the coding.
1028
+ * Here is what Rec. ITU-T T.81 | ISO/IEC 10918-1 says about how this next
1029
+ * bit works: Since symbols are paired for the longest Huffman code, the
1030
+ * symbols are removed from this length category two at a time. The prefix
1031
+ * for the pair (which is one bit shorter) is allocated to one of the pair;
1032
+ * then, skipping the BITS entry for that prefix length, a code word from the
1033
+ * next shortest nonzero BITS entry is converted into a prefix for two code
1034
+ * words one bit longer.
1035
+ */
1036
+
1037
+ for (i = MAX_CLEN; i > 16; i--) {
1038
+ while (bits[i] > 0) {
1039
+ j = i - 2; /* find length of new prefix to be used */
1040
+ while (bits[j] == 0)
1041
+ j--;
1042
+
1043
+ bits[i] -= 2; /* remove two symbols */
1044
+ bits[i - 1]++; /* one goes in this length */
1045
+ bits[j + 1] += 2; /* two new symbols in this length */
1046
+ bits[j]--; /* symbol of this length is now a prefix */
1047
+ }
1048
+ }
1049
+
1050
+ /* Remove the count for the pseudo-symbol 256 from the largest codelength */
1051
+ while (bits[i] == 0) /* find largest codelength still in use */
1052
+ i--;
1053
+ bits[i]--;
1054
+
1055
+ /* Return final symbol counts (only for lengths 0..16) */
1056
+ memcpy(htbl->bits, bits, sizeof(htbl->bits));
1057
+
1058
+ /* Return a list of the symbols sorted by code length */
1059
+ /* It's not real clear to me why we don't need to consider the codelength
1060
+ * changes made above, but Rec. ITU-T T.81 | ISO/IEC 10918-1 seems to think
1061
+ * this works.
1062
+ */
1063
+ p = 0;
1064
+ for (i = 1; i <= MAX_CLEN; i++) {
1065
+ for (j = 0; j <= 255; j++) {
1066
+ if (codesize[j] == i) {
1067
+ htbl->huffval[p] = (UINT8)j;
1068
+ p++;
1069
+ }
1070
+ }
1071
+ }
1072
+
1073
+ /* Set sent_table FALSE so updated table will be written to JPEG file. */
1074
+ htbl->sent_table = FALSE;
1075
+ }
1076
+
1077
+
1078
+ /*
1079
+ * Finish up a statistics-gathering pass and create the new Huffman tables.
1080
+ */
1081
+
1082
+ METHODDEF(void)
1083
+ finish_pass_gather(j_compress_ptr cinfo)
1084
+ {
1085
+ huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
1086
+ int ci, dctbl, actbl;
1087
+ jpeg_component_info *compptr;
1088
+ JHUFF_TBL **htblptr;
1089
+ boolean did_dc[NUM_HUFF_TBLS];
1090
+ boolean did_ac[NUM_HUFF_TBLS];
1091
+
1092
+ /* It's important not to apply jpeg_gen_optimal_table more than once
1093
+ * per table, because it clobbers the input frequency counts!
1094
+ */
1095
+ memset(did_dc, 0, sizeof(did_dc));
1096
+ memset(did_ac, 0, sizeof(did_ac));
1097
+
1098
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1099
+ compptr = cinfo->cur_comp_info[ci];
1100
+ dctbl = compptr->dc_tbl_no;
1101
+ actbl = compptr->ac_tbl_no;
1102
+ if (!did_dc[dctbl]) {
1103
+ htblptr = &cinfo->dc_huff_tbl_ptrs[dctbl];
1104
+ if (*htblptr == NULL)
1105
+ *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
1106
+ jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
1107
+ did_dc[dctbl] = TRUE;
1108
+ }
1109
+ if (!did_ac[actbl]) {
1110
+ htblptr = &cinfo->ac_huff_tbl_ptrs[actbl];
1111
+ if (*htblptr == NULL)
1112
+ *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
1113
+ jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
1114
+ did_ac[actbl] = TRUE;
1115
+ }
1116
+ }
1117
+ }
1118
+
1119
+
1120
+ #endif /* ENTROPY_OPT_SUPPORTED */
1121
+
1122
+
1123
+ /*
1124
+ * Module initialization routine for Huffman entropy encoding.
1125
+ */
1126
+
1127
+ GLOBAL(void)
1128
+ jinit_huff_encoder(j_compress_ptr cinfo)
1129
+ {
1130
+ huff_entropy_ptr entropy;
1131
+ int i;
1132
+
1133
+ entropy = (huff_entropy_ptr)
1134
+ (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
1135
+ sizeof(huff_entropy_encoder));
1136
+ cinfo->entropy = (struct jpeg_entropy_encoder *)entropy;
1137
+ entropy->pub.start_pass = start_pass_huff;
1138
+
1139
+ /* Mark tables unallocated */
1140
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
1141
+ entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
1142
+ #ifdef ENTROPY_OPT_SUPPORTED
1143
+ entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
1144
+ #endif
1145
+ }
1146
+ }