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,1776 @@
1
+ /*
2
+ * jcdctmgr.c
3
+ *
4
+ * This file was part of the Independent JPEG Group's software:
5
+ * Copyright (C) 1994-1996, Thomas G. Lane.
6
+ * libjpeg-turbo Modifications:
7
+ * Copyright (C) 1999-2006, MIYASAKA Masaru.
8
+ * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
9
+ * Copyright (C) 2011, 2014-2015 D. R. Commander
10
+ * mozjpeg Modifications:
11
+ * Copyright (C) 2014, Mozilla Corporation.
12
+ * For conditions of distribution and use, see the accompanying README file.
13
+ *
14
+ * This file contains the forward-DCT management logic.
15
+ * This code selects a particular DCT implementation to be used,
16
+ * and it performs related housekeeping chores including coefficient
17
+ * quantization.
18
+ */
19
+
20
+ #define JPEG_INTERNALS
21
+ #include "jinclude.h"
22
+ #include "jpeglib.h"
23
+ #include "jdct.h" /* Private declarations for DCT subsystem */
24
+ #include "jsimddct.h"
25
+ #include "jchuff.h"
26
+ #include <assert.h>
27
+ #include <math.h>
28
+
29
+
30
+ /* Private subobject for this module */
31
+
32
+ typedef void (*forward_DCT_method_ptr) (DCTELEM *data);
33
+ typedef void (*float_DCT_method_ptr) (FAST_FLOAT *data);
34
+
35
+ typedef void (*preprocess_method_ptr)(DCTELEM*, const JQUANT_TBL*);
36
+ typedef void (*float_preprocess_method_ptr)(FAST_FLOAT*, const JQUANT_TBL*);
37
+
38
+ typedef void (*convsamp_method_ptr) (JSAMPARRAY sample_data,
39
+ JDIMENSION start_col,
40
+ DCTELEM *workspace);
41
+ typedef void (*float_convsamp_method_ptr) (JSAMPARRAY sample_data,
42
+ JDIMENSION start_col,
43
+ FAST_FLOAT *workspace);
44
+
45
+ typedef void (*quantize_method_ptr) (JCOEFPTR coef_block, DCTELEM *divisors,
46
+ DCTELEM *workspace);
47
+ typedef void (*float_quantize_method_ptr) (JCOEFPTR coef_block,
48
+ FAST_FLOAT *divisors,
49
+ FAST_FLOAT *workspace);
50
+
51
+ METHODDEF(void) quantize (JCOEFPTR, DCTELEM *, DCTELEM *);
52
+
53
+ typedef struct {
54
+ struct jpeg_forward_dct pub; /* public fields */
55
+
56
+ /* Pointer to the DCT routine actually in use */
57
+ forward_DCT_method_ptr dct;
58
+ convsamp_method_ptr convsamp;
59
+ preprocess_method_ptr preprocess;
60
+ quantize_method_ptr quantize;
61
+
62
+ /* The actual post-DCT divisors --- not identical to the quant table
63
+ * entries, because of scaling (especially for an unnormalized DCT).
64
+ * Each table is given in normal array order.
65
+ */
66
+ DCTELEM *divisors[NUM_QUANT_TBLS];
67
+
68
+ /* work area for FDCT subroutine */
69
+ DCTELEM *workspace;
70
+
71
+ #ifdef DCT_FLOAT_SUPPORTED
72
+ /* Same as above for the floating-point case. */
73
+ float_DCT_method_ptr float_dct;
74
+ float_convsamp_method_ptr float_convsamp;
75
+ float_preprocess_method_ptr float_preprocess;
76
+ float_quantize_method_ptr float_quantize;
77
+ FAST_FLOAT *float_divisors[NUM_QUANT_TBLS];
78
+ FAST_FLOAT *float_workspace;
79
+ #endif
80
+ } my_fdct_controller;
81
+
82
+ typedef my_fdct_controller *my_fdct_ptr;
83
+
84
+
85
+ #if BITS_IN_JSAMPLE == 8
86
+
87
+ /*
88
+ * Find the highest bit in an integer through binary search.
89
+ */
90
+
91
+ LOCAL(int)
92
+ flss (UINT16 val)
93
+ {
94
+ int bit;
95
+
96
+ bit = 16;
97
+
98
+ if (!val)
99
+ return 0;
100
+
101
+ if (!(val & 0xff00)) {
102
+ bit -= 8;
103
+ val <<= 8;
104
+ }
105
+ if (!(val & 0xf000)) {
106
+ bit -= 4;
107
+ val <<= 4;
108
+ }
109
+ if (!(val & 0xc000)) {
110
+ bit -= 2;
111
+ val <<= 2;
112
+ }
113
+ if (!(val & 0x8000)) {
114
+ bit -= 1;
115
+ val <<= 1;
116
+ }
117
+
118
+ return bit;
119
+ }
120
+
121
+
122
+ /*
123
+ * Compute values to do a division using reciprocal.
124
+ *
125
+ * This implementation is based on an algorithm described in
126
+ * "How to optimize for the Pentium family of microprocessors"
127
+ * (http://www.agner.org/assem/).
128
+ * More information about the basic algorithm can be found in
129
+ * the paper "Integer Division Using Reciprocals" by Robert Alverson.
130
+ *
131
+ * The basic idea is to replace x/d by x * d^-1. In order to store
132
+ * d^-1 with enough precision we shift it left a few places. It turns
133
+ * out that this algoright gives just enough precision, and also fits
134
+ * into DCTELEM:
135
+ *
136
+ * b = (the number of significant bits in divisor) - 1
137
+ * r = (word size) + b
138
+ * f = 2^r / divisor
139
+ *
140
+ * f will not be an integer for most cases, so we need to compensate
141
+ * for the rounding error introduced:
142
+ *
143
+ * no fractional part:
144
+ *
145
+ * result = input >> r
146
+ *
147
+ * fractional part of f < 0.5:
148
+ *
149
+ * round f down to nearest integer
150
+ * result = ((input + 1) * f) >> r
151
+ *
152
+ * fractional part of f > 0.5:
153
+ *
154
+ * round f up to nearest integer
155
+ * result = (input * f) >> r
156
+ *
157
+ * This is the original algorithm that gives truncated results. But we
158
+ * want properly rounded results, so we replace "input" with
159
+ * "input + divisor/2".
160
+ *
161
+ * In order to allow SIMD implementations we also tweak the values to
162
+ * allow the same calculation to be made at all times:
163
+ *
164
+ * dctbl[0] = f rounded to nearest integer
165
+ * dctbl[1] = divisor / 2 (+ 1 if fractional part of f < 0.5)
166
+ * dctbl[2] = 1 << ((word size) * 2 - r)
167
+ * dctbl[3] = r - (word size)
168
+ *
169
+ * dctbl[2] is for stupid instruction sets where the shift operation
170
+ * isn't member wise (e.g. MMX).
171
+ *
172
+ * The reason dctbl[2] and dctbl[3] reduce the shift with (word size)
173
+ * is that most SIMD implementations have a "multiply and store top
174
+ * half" operation.
175
+ *
176
+ * Lastly, we store each of the values in their own table instead
177
+ * of in a consecutive manner, yet again in order to allow SIMD
178
+ * routines.
179
+ */
180
+
181
+ LOCAL(int)
182
+ compute_reciprocal (UINT16 divisor, DCTELEM *dtbl)
183
+ {
184
+ UDCTELEM2 fq, fr;
185
+ UDCTELEM c;
186
+ int b, r;
187
+
188
+ if (divisor == 1) {
189
+ /* divisor == 1 means unquantized, so these reciprocal/correction/shift
190
+ * values will cause the C quantization algorithm to act like the
191
+ * identity function. Since only the C quantization algorithm is used in
192
+ * these cases, the scale value is irrelevant.
193
+ */
194
+ dtbl[DCTSIZE2 * 0] = (DCTELEM) 1; /* reciprocal */
195
+ dtbl[DCTSIZE2 * 1] = (DCTELEM) 0; /* correction */
196
+ dtbl[DCTSIZE2 * 2] = (DCTELEM) 1; /* scale */
197
+ dtbl[DCTSIZE2 * 3] = -(DCTELEM) (sizeof(DCTELEM) * 8); /* shift */
198
+ return 0;
199
+ }
200
+
201
+ b = flss(divisor) - 1;
202
+ r = sizeof(DCTELEM) * 8 + b;
203
+
204
+ fq = ((UDCTELEM2)1 << r) / divisor;
205
+ fr = ((UDCTELEM2)1 << r) % divisor;
206
+
207
+ c = divisor / 2; /* for rounding */
208
+
209
+ if (fr == 0) { /* divisor is power of two */
210
+ /* fq will be one bit too large to fit in DCTELEM, so adjust */
211
+ fq >>= 1;
212
+ r--;
213
+ } else if (fr <= (divisor / 2U)) { /* fractional part is < 0.5 */
214
+ c++;
215
+ } else { /* fractional part is > 0.5 */
216
+ fq++;
217
+ }
218
+
219
+ dtbl[DCTSIZE2 * 0] = (DCTELEM) fq; /* reciprocal */
220
+ dtbl[DCTSIZE2 * 1] = (DCTELEM) c; /* correction + roundfactor */
221
+ #ifdef WITH_SIMD
222
+ dtbl[DCTSIZE2 * 2] = (DCTELEM) (1 << (sizeof(DCTELEM)*8*2 - r)); /* scale */
223
+ #else
224
+ dtbl[DCTSIZE2 * 2] = 1;
225
+ #endif
226
+ dtbl[DCTSIZE2 * 3] = (DCTELEM) r - sizeof(DCTELEM)*8; /* shift */
227
+
228
+ if (r <= 16) return 0;
229
+ else return 1;
230
+ }
231
+
232
+ #endif
233
+
234
+
235
+ /*
236
+ * Initialize for a processing pass.
237
+ * Verify that all referenced Q-tables are present, and set up
238
+ * the divisor table for each one.
239
+ * In the current implementation, DCT of all components is done during
240
+ * the first pass, even if only some components will be output in the
241
+ * first scan. Hence all components should be examined here.
242
+ */
243
+
244
+ METHODDEF(void)
245
+ start_pass_fdctmgr (j_compress_ptr cinfo)
246
+ {
247
+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
248
+ int ci, qtblno, i;
249
+ jpeg_component_info *compptr;
250
+ JQUANT_TBL *qtbl;
251
+ DCTELEM *dtbl;
252
+
253
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
254
+ ci++, compptr++) {
255
+ qtblno = compptr->quant_tbl_no;
256
+ /* Make sure specified quantization table is present */
257
+ if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
258
+ cinfo->quant_tbl_ptrs[qtblno] == NULL)
259
+ ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
260
+ qtbl = cinfo->quant_tbl_ptrs[qtblno];
261
+ /* Compute divisors for this quant table */
262
+ /* We may do this more than once for same table, but it's not a big deal */
263
+ switch (cinfo->dct_method) {
264
+ #ifdef DCT_ISLOW_SUPPORTED
265
+ case JDCT_ISLOW:
266
+ /* For LL&M IDCT method, divisors are equal to raw quantization
267
+ * coefficients multiplied by 8 (to counteract scaling).
268
+ */
269
+ if (fdct->divisors[qtblno] == NULL) {
270
+ fdct->divisors[qtblno] = (DCTELEM *)
271
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
272
+ (DCTSIZE2 * 4) * sizeof(DCTELEM));
273
+ }
274
+ dtbl = fdct->divisors[qtblno];
275
+ for (i = 0; i < DCTSIZE2; i++) {
276
+ #if BITS_IN_JSAMPLE == 8
277
+ if (!compute_reciprocal(qtbl->quantval[i] << 3, &dtbl[i]) &&
278
+ fdct->quantize == jsimd_quantize)
279
+ fdct->quantize = quantize;
280
+ #else
281
+ dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
282
+ #endif
283
+ }
284
+ break;
285
+ #endif
286
+ #ifdef DCT_IFAST_SUPPORTED
287
+ case JDCT_IFAST:
288
+ {
289
+ /* For AA&N IDCT method, divisors are equal to quantization
290
+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
291
+ * scalefactor[0] = 1
292
+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
293
+ * We apply a further scale factor of 8.
294
+ */
295
+ #define CONST_BITS 14
296
+ static const INT16 aanscales[DCTSIZE2] = {
297
+ /* precomputed values scaled up by 14 bits */
298
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
299
+ 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
300
+ 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
301
+ 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
302
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
303
+ 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
304
+ 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
305
+ 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
306
+ };
307
+ SHIFT_TEMPS
308
+
309
+ if (fdct->divisors[qtblno] == NULL) {
310
+ fdct->divisors[qtblno] = (DCTELEM *)
311
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
312
+ (DCTSIZE2 * 4) * sizeof(DCTELEM));
313
+ }
314
+ dtbl = fdct->divisors[qtblno];
315
+ for (i = 0; i < DCTSIZE2; i++) {
316
+ #if BITS_IN_JSAMPLE == 8
317
+ if (!compute_reciprocal(
318
+ DESCALE(MULTIPLY16V16((JLONG) qtbl->quantval[i],
319
+ (JLONG) aanscales[i]),
320
+ CONST_BITS-3), &dtbl[i]) &&
321
+ fdct->quantize == jsimd_quantize)
322
+ fdct->quantize = quantize;
323
+ #else
324
+ dtbl[i] = (DCTELEM)
325
+ DESCALE(MULTIPLY16V16((JLONG) qtbl->quantval[i],
326
+ (JLONG) aanscales[i]),
327
+ CONST_BITS-3);
328
+ #endif
329
+ }
330
+ }
331
+ break;
332
+ #endif
333
+ #ifdef DCT_FLOAT_SUPPORTED
334
+ case JDCT_FLOAT:
335
+ {
336
+ /* For float AA&N IDCT method, divisors are equal to quantization
337
+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
338
+ * scalefactor[0] = 1
339
+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
340
+ * We apply a further scale factor of 8.
341
+ * What's actually stored is 1/divisor so that the inner loop can
342
+ * use a multiplication rather than a division.
343
+ */
344
+ FAST_FLOAT *fdtbl;
345
+ int row, col;
346
+ static const double aanscalefactor[DCTSIZE] = {
347
+ 1.0, 1.387039845, 1.306562965, 1.175875602,
348
+ 1.0, 0.785694958, 0.541196100, 0.275899379
349
+ };
350
+
351
+ if (fdct->float_divisors[qtblno] == NULL) {
352
+ fdct->float_divisors[qtblno] = (FAST_FLOAT *)
353
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
354
+ DCTSIZE2 * sizeof(FAST_FLOAT));
355
+ }
356
+ fdtbl = fdct->float_divisors[qtblno];
357
+ i = 0;
358
+ for (row = 0; row < DCTSIZE; row++) {
359
+ for (col = 0; col < DCTSIZE; col++) {
360
+ fdtbl[i] = (FAST_FLOAT)
361
+ (1.0 / (((double) qtbl->quantval[i] *
362
+ aanscalefactor[row] * aanscalefactor[col] * 8.0)));
363
+ i++;
364
+ }
365
+ }
366
+ }
367
+ break;
368
+ #endif
369
+ default:
370
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
371
+ break;
372
+ }
373
+ }
374
+ }
375
+
376
+ METHODDEF(float)
377
+ catmull_rom(const DCTELEM value1, const DCTELEM value2, const DCTELEM value3, const DCTELEM value4, const float t, int size)
378
+ {
379
+ const int tan1 = (value3 - value1) * size;
380
+ const int tan2 = (value4 - value2) * size;
381
+
382
+ const float t2 = t * t;
383
+ const float t3 = t2 * t;
384
+
385
+ const float f1 = 2.f * t3 - 3.f * t2 + 1.f;
386
+ const float f2 = -2.f * t3 + 3.f * t2;
387
+ const float f3 = t3 - 2.f * t2 + t;
388
+ const float f4 = t3 - t2;
389
+
390
+ return value2 * f1 + tan1 * f3 +
391
+ value3 * f2 + tan2 * f4;
392
+ }
393
+
394
+ /** Prevents visible ringing artifacts near hard edges on white backgrounds.
395
+
396
+ 1. JPEG can encode samples with higher values than it's possible to display (higher than 255 in RGB),
397
+ and the decoder will always clamp values to 0-255. To encode 255 you can use any value >= 255,
398
+ and distortions of the out-of-range values won't be visible as long as they decode to anything >= 255.
399
+
400
+ 2. From DCT perspective pixels in a block are a waveform. Hard edges form square waves (bad).
401
+ Edges with white are similar to waveform clipping, and anti-clipping algorithms can turn square waves
402
+ into softer ones that compress better.
403
+
404
+ */
405
+ METHODDEF(void)
406
+ preprocess_deringing(DCTELEM *data, const JQUANT_TBL *quantization_table)
407
+ {
408
+ const DCTELEM maxsample = 255 - CENTERJSAMPLE;
409
+ const int size = DCTSIZE * DCTSIZE;
410
+
411
+ /* Decoders don't handle overflow of DC very well, so calculate
412
+ maximum overflow that is safe to do without increasing DC out of range */
413
+ int sum = 0;
414
+ int maxsample_count = 0;
415
+ int i;
416
+ DCTELEM maxovershoot;
417
+ int n;
418
+
419
+ for(i=0; i < size; i++) {
420
+ sum += data[i];
421
+ if (data[i] >= maxsample) {
422
+ maxsample_count++;
423
+ }
424
+ }
425
+
426
+ /* If nothing reaches max value there's nothing to overshoot
427
+ and if the block is completely flat, it's already the best case. */
428
+ if (!maxsample_count || maxsample_count == size) {
429
+ return;
430
+ }
431
+
432
+ /* Too much overshoot is not good: increased amplitude will cost bits, and the cost is proportional to quantization (here using DC quant as a rough guide). */
433
+ maxovershoot = maxsample + MIN(MIN(31, 2*quantization_table->quantval[0]), (maxsample * size - sum) / maxsample_count);
434
+
435
+ n = 0;
436
+ do {
437
+ int start, end, length;
438
+ DCTELEM f1, f2, l1, l2, fslope, lslope;
439
+ float step, position;
440
+
441
+ /* Pixels are traversed in zig-zag order to process them as a line */
442
+ if (data[jpeg_natural_order[n]] < maxsample) {
443
+ n++;
444
+ continue;
445
+ }
446
+
447
+ /* Find a run of maxsample pixels. Start is the first pixel inside the range, end the first pixel outside. */
448
+ start = n;
449
+ while(++n < size && data[jpeg_natural_order[n]] >= maxsample) {}
450
+ end = n;
451
+
452
+ /* the run will be replaced with a catmull-rom interpolation of values from the edges */
453
+
454
+ /* Find suitable upward slope from pixels around edges of the run.
455
+ Just feeding nearby pixels as catmull rom points isn't good enough,
456
+ as slope with one sample before the edge may have been flattened by clipping,
457
+ and slope of two samples before the edge could be downward. */
458
+ f1 = data[jpeg_natural_order[start >= 1 ? start-1 : 0]];
459
+ f2 = data[jpeg_natural_order[start >= 2 ? start-2 : 0]];
460
+
461
+ l1 = data[jpeg_natural_order[end < size-1 ? end : size-1]];
462
+ l2 = data[jpeg_natural_order[end < size-2 ? end+1 : size-1]];
463
+
464
+ fslope = MAX(f1-f2, maxsample-f1);
465
+ lslope = MAX(l1-l2, maxsample-l1);
466
+
467
+ /* if slope at the start/end is unknown, just make the curve symmetric */
468
+ if (start == 0) {
469
+ fslope = lslope;
470
+ }
471
+ if (end == size) {
472
+ lslope = fslope;
473
+ }
474
+
475
+ /* The curve fits better if first and last pixel is omitted */
476
+ length = end - start;
477
+ step = 1.f/(float)(length + 1);
478
+ position = step;
479
+
480
+ for(i = start; i < end; i++, position += step) {
481
+ DCTELEM tmp = ceilf(catmull_rom(maxsample - fslope, maxsample, maxsample, maxsample - lslope, position, length));
482
+ data[jpeg_natural_order[i]] = MIN(tmp, maxovershoot);
483
+ }
484
+ n++;
485
+ }
486
+ while(n < size);
487
+ }
488
+
489
+ /*
490
+ Float version of preprocess_deringing()
491
+ */
492
+ METHODDEF(void)
493
+ float_preprocess_deringing(FAST_FLOAT *data, const JQUANT_TBL *quantization_table)
494
+ {
495
+ const FAST_FLOAT maxsample = 255 - CENTERJSAMPLE;
496
+ const int size = DCTSIZE * DCTSIZE;
497
+
498
+ FAST_FLOAT sum = 0;
499
+ int maxsample_count = 0;
500
+ int i;
501
+ int n;
502
+ FAST_FLOAT maxovershoot;
503
+
504
+ for(i=0; i < size; i++) {
505
+ sum += data[i];
506
+ if (data[i] >= maxsample) {
507
+ maxsample_count++;
508
+ }
509
+ }
510
+
511
+ if (!maxsample_count || maxsample_count == size) {
512
+ return;
513
+ }
514
+
515
+ maxovershoot = maxsample + MIN(MIN(31, 2*quantization_table->quantval[0]), (maxsample * size - sum) / maxsample_count);
516
+
517
+ n = 0;
518
+ do {
519
+ int start, end, length;
520
+ FAST_FLOAT f1, f2, l1, l2, fslope, lslope;
521
+ float step, position;
522
+
523
+ if (data[jpeg_natural_order[n]] < maxsample) {
524
+ n++;
525
+ continue;
526
+ }
527
+
528
+ start = n;
529
+ while(++n < size && data[jpeg_natural_order[n]] >= maxsample) {}
530
+ end = n;
531
+
532
+ f1 = data[jpeg_natural_order[start >= 1 ? start-1 : 0]];
533
+ f2 = data[jpeg_natural_order[start >= 2 ? start-2 : 0]];
534
+
535
+ l1 = data[jpeg_natural_order[end < size-1 ? end : size-1]];
536
+ l2 = data[jpeg_natural_order[end < size-2 ? end+1 : size-1]];
537
+
538
+ fslope = MAX(f1-f2, maxsample-f1);
539
+ lslope = MAX(l1-l2, maxsample-l1);
540
+
541
+ if (start == 0) {
542
+ fslope = lslope;
543
+ }
544
+ if (end == size) {
545
+ lslope = fslope;
546
+ }
547
+
548
+ length = end - start;
549
+ step = 1.f/(float)(length + 1);
550
+ position = step;
551
+
552
+ for(i = start; i < end; i++, position += step) {
553
+ FAST_FLOAT tmp = catmull_rom(maxsample - fslope, maxsample, maxsample, maxsample - lslope, position, length);
554
+ data[jpeg_natural_order[i]] = MIN(tmp, maxovershoot);
555
+ }
556
+ n++;
557
+ }
558
+ while(n < size);
559
+ }
560
+
561
+ /*
562
+ * Load data into workspace, applying unsigned->signed conversion.
563
+ */
564
+
565
+ METHODDEF(void)
566
+ convsamp (JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM *workspace)
567
+ {
568
+ register DCTELEM *workspaceptr;
569
+ register JSAMPROW elemptr;
570
+ register int elemr;
571
+
572
+ workspaceptr = workspace;
573
+ for (elemr = 0; elemr < DCTSIZE; elemr++) {
574
+ elemptr = sample_data[elemr] + start_col;
575
+
576
+ #if DCTSIZE == 8 /* unroll the inner loop */
577
+ *workspaceptr++ = (*elemptr++) - CENTERJSAMPLE;
578
+ *workspaceptr++ = (*elemptr++) - CENTERJSAMPLE;
579
+ *workspaceptr++ = (*elemptr++) - CENTERJSAMPLE;
580
+ *workspaceptr++ = (*elemptr++) - CENTERJSAMPLE;
581
+ *workspaceptr++ = (*elemptr++) - CENTERJSAMPLE;
582
+ *workspaceptr++ = (*elemptr++) - CENTERJSAMPLE;
583
+ *workspaceptr++ = (*elemptr++) - CENTERJSAMPLE;
584
+ *workspaceptr++ = (*elemptr++) - CENTERJSAMPLE;
585
+ #else
586
+ {
587
+ register int elemc;
588
+ for (elemc = DCTSIZE; elemc > 0; elemc--)
589
+ *workspaceptr++ = (*elemptr++) - CENTERJSAMPLE;
590
+ }
591
+ #endif
592
+ }
593
+ }
594
+
595
+
596
+ /*
597
+ * Quantize/descale the coefficients, and store into coef_blocks[].
598
+ */
599
+
600
+ METHODDEF(void)
601
+ quantize (JCOEFPTR coef_block, DCTELEM *divisors, DCTELEM *workspace)
602
+ {
603
+ int i;
604
+ DCTELEM temp;
605
+ JCOEFPTR output_ptr = coef_block;
606
+
607
+ #if BITS_IN_JSAMPLE == 8
608
+
609
+ UDCTELEM recip, corr;
610
+ int shift;
611
+ UDCTELEM2 product;
612
+
613
+ for (i = 0; i < DCTSIZE2; i++) {
614
+ temp = workspace[i];
615
+ recip = divisors[i + DCTSIZE2 * 0];
616
+ corr = divisors[i + DCTSIZE2 * 1];
617
+ shift = divisors[i + DCTSIZE2 * 3];
618
+
619
+ if (temp < 0) {
620
+ temp = -temp;
621
+ product = (UDCTELEM2)(temp + corr) * recip;
622
+ product >>= shift + sizeof(DCTELEM)*8;
623
+ temp = (DCTELEM)product;
624
+ temp = -temp;
625
+ } else {
626
+ product = (UDCTELEM2)(temp + corr) * recip;
627
+ product >>= shift + sizeof(DCTELEM)*8;
628
+ temp = (DCTELEM)product;
629
+ }
630
+ output_ptr[i] = (JCOEF) temp;
631
+ }
632
+
633
+ #else
634
+
635
+ register DCTELEM qval;
636
+
637
+ for (i = 0; i < DCTSIZE2; i++) {
638
+ qval = divisors[i];
639
+ temp = workspace[i];
640
+ /* Divide the coefficient value by qval, ensuring proper rounding.
641
+ * Since C does not specify the direction of rounding for negative
642
+ * quotients, we have to force the dividend positive for portability.
643
+ *
644
+ * In most files, at least half of the output values will be zero
645
+ * (at default quantization settings, more like three-quarters...)
646
+ * so we should ensure that this case is fast. On many machines,
647
+ * a comparison is enough cheaper than a divide to make a special test
648
+ * a win. Since both inputs will be nonnegative, we need only test
649
+ * for a < b to discover whether a/b is 0.
650
+ * If your machine's division is fast enough, define FAST_DIVIDE.
651
+ */
652
+ #ifdef FAST_DIVIDE
653
+ #define DIVIDE_BY(a,b) a /= b
654
+ #else
655
+ #define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0
656
+ #endif
657
+ if (temp < 0) {
658
+ temp = -temp;
659
+ temp += qval>>1; /* for rounding */
660
+ DIVIDE_BY(temp, qval);
661
+ temp = -temp;
662
+ } else {
663
+ temp += qval>>1; /* for rounding */
664
+ DIVIDE_BY(temp, qval);
665
+ }
666
+ output_ptr[i] = (JCOEF) temp;
667
+ }
668
+
669
+ #endif
670
+
671
+ }
672
+
673
+
674
+ /*
675
+ * Perform forward DCT on one or more blocks of a component.
676
+ *
677
+ * The input samples are taken from the sample_data[] array starting at
678
+ * position start_row/start_col, and moving to the right for any additional
679
+ * blocks. The quantized coefficients are returned in coef_blocks[].
680
+ */
681
+
682
+ METHODDEF(void)
683
+ forward_DCT (j_compress_ptr cinfo, jpeg_component_info *compptr,
684
+ JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
685
+ JDIMENSION start_row, JDIMENSION start_col,
686
+ JDIMENSION num_blocks, JBLOCKROW dst)
687
+ /* This version is used for integer DCT implementations. */
688
+ {
689
+ /* This routine is heavily used, so it's worth coding it tightly. */
690
+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
691
+ DCTELEM *divisors = fdct->divisors[compptr->quant_tbl_no];
692
+ JQUANT_TBL *qtbl = cinfo->quant_tbl_ptrs[compptr->quant_tbl_no];
693
+ DCTELEM *workspace;
694
+ JDIMENSION bi;
695
+
696
+ /* Make sure the compiler doesn't look up these every pass */
697
+ forward_DCT_method_ptr do_dct = fdct->dct;
698
+ convsamp_method_ptr do_convsamp = fdct->convsamp;
699
+ preprocess_method_ptr do_preprocess = fdct->preprocess;
700
+ quantize_method_ptr do_quantize = fdct->quantize;
701
+ workspace = fdct->workspace;
702
+
703
+ sample_data += start_row; /* fold in the vertical offset once */
704
+
705
+ for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
706
+ /* Load data into workspace, applying unsigned->signed conversion */
707
+ (*do_convsamp) (sample_data, start_col, workspace);
708
+
709
+ if (do_preprocess) {
710
+ (*do_preprocess) (workspace, qtbl);
711
+ }
712
+
713
+ /* Perform the DCT */
714
+ (*do_dct) (workspace);
715
+
716
+ /* Save unquantized transform coefficients for later trellis quantization */
717
+ if (dst) {
718
+ int i;
719
+ if (cinfo->dct_method == JDCT_IFAST) {
720
+ static const INT16 aanscales[DCTSIZE2] = {
721
+ /* precomputed values scaled up by 14 bits */
722
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
723
+ 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
724
+ 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
725
+ 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
726
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
727
+ 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
728
+ 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
729
+ 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
730
+ };
731
+
732
+ for (i = 0; i < DCTSIZE2; i++) {
733
+ int x = workspace[i];
734
+ int s = aanscales[i];
735
+ x = (x >= 0) ? (x * 32768 + s) / (2*s) : (x * 32768 - s) / (2*s);
736
+ dst[bi][i] = x;
737
+ }
738
+
739
+ } else {
740
+ for (i = 0; i < DCTSIZE2; i++) {
741
+ dst[bi][i] = workspace[i];
742
+ }
743
+ }
744
+ }
745
+
746
+ /* Quantize/descale the coefficients, and store into coef_blocks[] */
747
+ (*do_quantize) (coef_blocks[bi], divisors, workspace);
748
+
749
+ if (do_preprocess) {
750
+ int i;
751
+ int maxval = (1 << MAX_COEF_BITS) - 1;
752
+ for (i = 0; i < 64; i++) {
753
+ if (coef_blocks[bi][i] < -maxval)
754
+ coef_blocks[bi][i] = -maxval;
755
+ if (coef_blocks[bi][i] > maxval)
756
+ coef_blocks[bi][i] = maxval;
757
+ }
758
+ }
759
+ }
760
+ }
761
+
762
+
763
+ #ifdef DCT_FLOAT_SUPPORTED
764
+
765
+ METHODDEF(void)
766
+ convsamp_float(JSAMPARRAY sample_data, JDIMENSION start_col,
767
+ FAST_FLOAT *workspace)
768
+ {
769
+ register FAST_FLOAT *workspaceptr;
770
+ register JSAMPROW elemptr;
771
+ register int elemr;
772
+
773
+ workspaceptr = workspace;
774
+ for (elemr = 0; elemr < DCTSIZE; elemr++) {
775
+ elemptr = sample_data[elemr] + start_col;
776
+ #if DCTSIZE == 8 /* unroll the inner loop */
777
+ *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - CENTERJSAMPLE);
778
+ *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - CENTERJSAMPLE);
779
+ *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - CENTERJSAMPLE);
780
+ *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - CENTERJSAMPLE);
781
+ *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - CENTERJSAMPLE);
782
+ *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - CENTERJSAMPLE);
783
+ *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - CENTERJSAMPLE);
784
+ *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - CENTERJSAMPLE);
785
+ #else
786
+ {
787
+ register int elemc;
788
+ for (elemc = DCTSIZE; elemc > 0; elemc--)
789
+ *workspaceptr++ = (FAST_FLOAT)((*elemptr++) - CENTERJSAMPLE);
790
+ }
791
+ #endif
792
+ }
793
+ }
794
+
795
+
796
+ METHODDEF(void)
797
+ quantize_float(JCOEFPTR coef_block, FAST_FLOAT *divisors,
798
+ FAST_FLOAT *workspace)
799
+ {
800
+ register FAST_FLOAT temp;
801
+ register int i;
802
+ register JCOEFPTR output_ptr = coef_block;
803
+
804
+ for (i = 0; i < DCTSIZE2; i++) {
805
+ /* Apply the quantization and scaling factor */
806
+ temp = workspace[i] * divisors[i];
807
+
808
+ /* Round to nearest integer.
809
+ * Since C does not specify the direction of rounding for negative
810
+ * quotients, we have to force the dividend positive for portability.
811
+ * The maximum coefficient size is +-16K (for 12-bit data), so this
812
+ * code should work for either 16-bit or 32-bit ints.
813
+ */
814
+ output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
815
+ }
816
+ }
817
+
818
+
819
+ METHODDEF(void)
820
+ forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info *compptr,
821
+ JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
822
+ JDIMENSION start_row, JDIMENSION start_col,
823
+ JDIMENSION num_blocks, JBLOCKROW dst)
824
+ /* This version is used for floating-point DCT implementations. */
825
+ {
826
+ /* This routine is heavily used, so it's worth coding it tightly. */
827
+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
828
+ FAST_FLOAT *divisors = fdct->float_divisors[compptr->quant_tbl_no];
829
+ JQUANT_TBL *qtbl = cinfo->quant_tbl_ptrs[compptr->quant_tbl_no];
830
+ FAST_FLOAT *workspace;
831
+ JDIMENSION bi;
832
+ float v;
833
+ int x;
834
+
835
+
836
+ /* Make sure the compiler doesn't look up these every pass */
837
+ float_DCT_method_ptr do_dct = fdct->float_dct;
838
+ float_convsamp_method_ptr do_convsamp = fdct->float_convsamp;
839
+ float_preprocess_method_ptr do_preprocess = fdct->float_preprocess;
840
+ float_quantize_method_ptr do_quantize = fdct->float_quantize;
841
+ workspace = fdct->float_workspace;
842
+
843
+ sample_data += start_row; /* fold in the vertical offset once */
844
+
845
+ for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
846
+ /* Load data into workspace, applying unsigned->signed conversion */
847
+ (*do_convsamp) (sample_data, start_col, workspace);
848
+
849
+ if (do_preprocess) {
850
+ (*do_preprocess) (workspace, qtbl);
851
+ }
852
+
853
+ /* Perform the DCT */
854
+ (*do_dct) (workspace);
855
+
856
+ /* Save unquantized transform coefficients for later trellis quantization */
857
+ /* Currently save as integer values. Could save float values but would require */
858
+ /* modifications to memory allocation and trellis quantization */
859
+
860
+ if (dst) {
861
+ int i;
862
+ static const double aanscalefactor[DCTSIZE] = {
863
+ 1.0, 1.387039845, 1.306562965, 1.175875602,
864
+ 1.0, 0.785694958, 0.541196100, 0.275899379
865
+ };
866
+
867
+ for (i = 0; i < DCTSIZE2; i++) {
868
+ v = workspace[i];
869
+ v /= aanscalefactor[i%8];
870
+ v /= aanscalefactor[i/8];
871
+ x = (v >= 0.0) ? (int)(v + 0.5) : (int)(v - 0.5);
872
+ dst[bi][i] = x;
873
+ }
874
+ }
875
+
876
+ /* Quantize/descale the coefficients, and store into coef_blocks[] */
877
+ (*do_quantize) (coef_blocks[bi], divisors, workspace);
878
+
879
+ if (do_preprocess) {
880
+ int i;
881
+ int maxval = (1 << MAX_COEF_BITS) - 1;
882
+ for (i = 0; i < 64; i++) {
883
+ if (coef_blocks[bi][i] < -maxval)
884
+ coef_blocks[bi][i] = -maxval;
885
+ if (coef_blocks[bi][i] > maxval)
886
+ coef_blocks[bi][i] = maxval;
887
+ }
888
+ }
889
+ }
890
+ }
891
+
892
+ #endif /* DCT_FLOAT_SUPPORTED */
893
+
894
+ #include "jpeg_nbits_table.h"
895
+
896
+ static const float jpeg_lambda_weights_flat[64] = {
897
+ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
898
+ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
899
+ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
900
+ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
901
+ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
902
+ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
903
+ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
904
+ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f
905
+ };
906
+
907
+ static const float jpeg_lambda_weights_csf_luma[64] = {
908
+ 3.35630f, 3.59892f, 3.20921f, 2.28102f, 1.42378f, 0.88079f, 0.58190f, 0.43454f,
909
+ 3.59893f, 3.21284f, 2.71282f, 1.98092f, 1.30506f, 0.83852f, 0.56346f, 0.42146f,
910
+ 3.20921f, 2.71282f, 2.12574f, 1.48616f, 0.99660f, 0.66132f, 0.45610f, 0.34609f,
911
+ 2.28102f, 1.98092f, 1.48616f, 0.97492f, 0.64622f, 0.43812f, 0.31074f, 0.24072f,
912
+ 1.42378f, 1.30506f, 0.99660f, 0.64623f, 0.42051f, 0.28446f, 0.20380f, 0.15975f,
913
+ 0.88079f, 0.83852f, 0.66132f, 0.43812f, 0.28446f, 0.19092f, 0.13635f, 0.10701f,
914
+ 0.58190f, 0.56346f, 0.45610f, 0.31074f, 0.20380f, 0.13635f, 0.09674f, 0.07558f,
915
+ 0.43454f, 0.42146f, 0.34609f, 0.24072f, 0.15975f, 0.10701f, 0.07558f, 0.05875f,
916
+ };
917
+
918
+ #define DC_TRELLIS_MAX_CANDIDATES 9
919
+
920
+ LOCAL(int) get_num_dc_trellis_candidates(int dc_quantval) {
921
+ /* Higher qualities can tolerate higher DC distortion */
922
+ return MIN(DC_TRELLIS_MAX_CANDIDATES, (2 + 60 / dc_quantval)|1);
923
+ }
924
+
925
+ GLOBAL(void)
926
+ quantize_trellis(j_compress_ptr cinfo, c_derived_tbl *dctbl, c_derived_tbl *actbl, JBLOCKROW coef_blocks, JBLOCKROW src, JDIMENSION num_blocks,
927
+ JQUANT_TBL * qtbl, double *norm_src, double *norm_coef, JCOEF *last_dc_val,
928
+ JBLOCKROW coef_blocks_above, JBLOCKROW src_above)
929
+ {
930
+ int i, j, k, l;
931
+ float accumulated_zero_dist[DCTSIZE2];
932
+ float accumulated_cost[DCTSIZE2];
933
+ int run_start[DCTSIZE2];
934
+ int bi;
935
+ float best_cost;
936
+ int last_coeff_idx; /* position of last nonzero coefficient */
937
+ float norm = 0.0;
938
+ float lambda_base;
939
+ float lambda;
940
+ float lambda_dc;
941
+ const float *lambda_tbl = (cinfo->master->use_lambda_weight_tbl) ?
942
+ jpeg_lambda_weights_csf_luma :
943
+ jpeg_lambda_weights_flat;
944
+ int Ss, Se;
945
+ float *accumulated_zero_block_cost = NULL;
946
+ float *accumulated_block_cost = NULL;
947
+ int *block_run_start = NULL;
948
+ int *requires_eob = NULL;
949
+ int has_eob;
950
+ float cost_all_zeros;
951
+ float best_cost_skip;
952
+ float cost;
953
+ int zero_run;
954
+ int run_bits;
955
+ int rate;
956
+ float *accumulated_dc_cost[DC_TRELLIS_MAX_CANDIDATES];
957
+ int *dc_cost_backtrack[DC_TRELLIS_MAX_CANDIDATES];
958
+ JCOEF *dc_candidate[DC_TRELLIS_MAX_CANDIDATES];
959
+ int mode = 1;
960
+ float lambda_table[DCTSIZE2];
961
+ const int dc_trellis_candidates = get_num_dc_trellis_candidates(qtbl->quantval[0]);
962
+
963
+ Ss = cinfo->Ss;
964
+ Se = cinfo->Se;
965
+ if (Ss == 0)
966
+ Ss = 1;
967
+ if (Se < Ss)
968
+ return;
969
+ if (cinfo->master->trellis_eob_opt) {
970
+ accumulated_zero_block_cost = (float *)malloc((num_blocks + 1) * sizeof(float));
971
+ accumulated_block_cost = (float *)malloc((num_blocks + 1) * sizeof(float));
972
+ block_run_start = (int *)malloc(num_blocks * sizeof(int));
973
+ requires_eob = (int *)malloc((num_blocks + 1) * sizeof(int));
974
+ if (!accumulated_zero_block_cost ||
975
+ !accumulated_block_cost ||
976
+ !block_run_start ||
977
+ !requires_eob) {
978
+ ERREXIT(cinfo, JERR_OUT_OF_MEMORY);
979
+ }
980
+
981
+ accumulated_zero_block_cost[0] = 0;
982
+ accumulated_block_cost[0] = 0;
983
+ requires_eob[0] = 0;
984
+ }
985
+
986
+ if (cinfo->master->trellis_quant_dc) {
987
+ for (i = 0; i < dc_trellis_candidates; i++) {
988
+ accumulated_dc_cost[i] = (float *)malloc(num_blocks * sizeof(float));
989
+ dc_cost_backtrack[i] = (int *)malloc(num_blocks * sizeof(int));
990
+ dc_candidate[i] = (JCOEF *)malloc(num_blocks * sizeof(JCOEF));
991
+ if (!accumulated_dc_cost[i] ||
992
+ !dc_cost_backtrack[i] ||
993
+ !dc_candidate[i]) {
994
+ ERREXIT(cinfo, JERR_OUT_OF_MEMORY);
995
+ }
996
+ }
997
+ }
998
+
999
+ norm = 0.0;
1000
+ for (i = 1; i < DCTSIZE2; i++) {
1001
+ norm += qtbl->quantval[i] * qtbl->quantval[i];
1002
+ }
1003
+ norm /= 63.0;
1004
+
1005
+ if (mode == 1) {
1006
+ lambda_base = 1.0;
1007
+ lambda_tbl = lambda_table;
1008
+ for (i = 0; i < DCTSIZE2; i++)
1009
+ lambda_table[i] = 1.0 / (qtbl->quantval[i] * qtbl->quantval[i]);
1010
+ } else
1011
+ lambda_base = 1.0 / norm;
1012
+
1013
+ for (bi = 0; bi < num_blocks; bi++) {
1014
+
1015
+ norm = 0.0;
1016
+ for (i = 1; i < DCTSIZE2; i++) {
1017
+ norm += src[bi][i] * src[bi][i];
1018
+ }
1019
+ norm /= 63.0;
1020
+
1021
+ if (cinfo->master->lambda_log_scale2 > 0.0)
1022
+ lambda = pow(2.0, cinfo->master->lambda_log_scale1) * lambda_base /
1023
+ (pow(2.0, cinfo->master->lambda_log_scale2) + norm);
1024
+ else
1025
+ lambda = pow(2.0, cinfo->master->lambda_log_scale1 - 12.0) * lambda_base;
1026
+
1027
+ lambda_dc = lambda * lambda_tbl[0];
1028
+
1029
+ accumulated_zero_dist[Ss-1] = 0.0;
1030
+ accumulated_cost[Ss-1] = 0.0;
1031
+
1032
+ /* Do DC coefficient */
1033
+ if (cinfo->master->trellis_quant_dc) {
1034
+ int sign = src[bi][0] >> 31;
1035
+ int x = abs(src[bi][0]);
1036
+ int q = 8 * qtbl->quantval[0];
1037
+ int qval;
1038
+ float dc_candidate_dist;
1039
+
1040
+ qval = (x + q/2) / q; /* quantized value (round nearest) */
1041
+ for (k = 0; k < dc_trellis_candidates; k++) {
1042
+ int delta;
1043
+ int dc_delta;
1044
+ int bits;
1045
+
1046
+ dc_candidate[k][bi] = qval - dc_trellis_candidates/2 + k;
1047
+ if (dc_candidate[k][bi] >= (1<<MAX_COEF_BITS))
1048
+ dc_candidate[k][bi] = (1<<MAX_COEF_BITS)-1;
1049
+ if (dc_candidate[k][bi] <= -(1<<MAX_COEF_BITS))
1050
+ dc_candidate[k][bi] = -(1<<MAX_COEF_BITS)+1;
1051
+
1052
+ delta = dc_candidate[k][bi] * q - x;
1053
+ dc_candidate_dist = delta * delta * lambda_dc;
1054
+ dc_candidate[k][bi] *= 1 + 2*sign;
1055
+
1056
+ /* Take into account DC differences */
1057
+ if (coef_blocks_above && src_above && cinfo->master->trellis_delta_dc_weight > 0.0) {
1058
+ int dc_above_orig;
1059
+ int dc_above_recon;
1060
+ int dc_orig;
1061
+ int dc_recon;
1062
+ float vertical_dist;
1063
+
1064
+ dc_above_orig = src_above[bi][0];
1065
+ dc_above_recon = coef_blocks_above[bi][0] * q;
1066
+ dc_orig = src[bi][0];
1067
+ dc_recon = dc_candidate[k][bi] * q;
1068
+ /* delta is difference of vertical gradients */
1069
+ delta = (dc_above_orig - dc_orig) - (dc_above_recon - dc_recon);
1070
+ vertical_dist = delta * delta * lambda_dc;
1071
+ dc_candidate_dist += cinfo->master->trellis_delta_dc_weight * (vertical_dist - dc_candidate_dist);
1072
+ }
1073
+
1074
+ if (bi == 0) {
1075
+ dc_delta = dc_candidate[k][bi] - *last_dc_val;
1076
+
1077
+ /* Derive number of suffix bits */
1078
+ bits = 0;
1079
+ dc_delta = abs(dc_delta);
1080
+ while (dc_delta) {
1081
+ dc_delta >>= 1;
1082
+ bits++;
1083
+ }
1084
+ cost = bits + dctbl->ehufsi[bits] + dc_candidate_dist;
1085
+ accumulated_dc_cost[k][0] = cost;
1086
+ dc_cost_backtrack[k][0] = -1;
1087
+ } else {
1088
+ for (l = 0; l < dc_trellis_candidates; l++) {
1089
+ dc_delta = dc_candidate[k][bi] - dc_candidate[l][bi-1];
1090
+
1091
+ /* Derive number of suffix bits */
1092
+ bits = 0;
1093
+ dc_delta = abs(dc_delta);
1094
+ while (dc_delta) {
1095
+ dc_delta >>= 1;
1096
+ bits++;
1097
+ }
1098
+ cost = bits + dctbl->ehufsi[bits] + dc_candidate_dist + accumulated_dc_cost[l][bi-1];
1099
+ if (l == 0 || cost < accumulated_dc_cost[k][bi]) {
1100
+ accumulated_dc_cost[k][bi] = cost;
1101
+ dc_cost_backtrack[k][bi] = l;
1102
+ }
1103
+ }
1104
+ }
1105
+ }
1106
+ }
1107
+
1108
+ /* Do AC coefficients */
1109
+ for (i = Ss; i <= Se; i++) {
1110
+ int z = jpeg_natural_order[i];
1111
+
1112
+ int sign = src[bi][z] >> 31;
1113
+ int x = abs(src[bi][z]);
1114
+ int q = 8 * qtbl->quantval[z];
1115
+ int candidate[16];
1116
+ int candidate_bits[16];
1117
+ float candidate_dist[16];
1118
+ int num_candidates;
1119
+ int qval;
1120
+
1121
+ accumulated_zero_dist[i] = x * x * lambda * lambda_tbl[z] + accumulated_zero_dist[i-1];
1122
+
1123
+ qval = (x + q/2) / q; /* quantized value (round nearest) */
1124
+
1125
+ if (qval == 0) {
1126
+ coef_blocks[bi][z] = 0;
1127
+ accumulated_cost[i] = 1e38; /* Shouldn't be needed */
1128
+ continue;
1129
+ }
1130
+
1131
+ if (qval >= (1<<MAX_COEF_BITS))
1132
+ qval = (1<<MAX_COEF_BITS)-1;
1133
+
1134
+ num_candidates = jpeg_nbits_table[qval];
1135
+ for (k = 0; k < num_candidates; k++) {
1136
+ int delta;
1137
+ candidate[k] = (k < num_candidates - 1) ? (2 << k) - 1 : qval;
1138
+ delta = candidate[k] * q - x;
1139
+ candidate_bits[k] = k+1;
1140
+ candidate_dist[k] = delta * delta * lambda * lambda_tbl[z];
1141
+ }
1142
+
1143
+ accumulated_cost[i] = 1e38;
1144
+
1145
+ for (j = Ss-1; j < i; j++) {
1146
+ int zz = jpeg_natural_order[j];
1147
+ if (j != Ss-1 && coef_blocks[bi][zz] == 0)
1148
+ continue;
1149
+
1150
+ zero_run = i - 1 - j;
1151
+ if ((zero_run >> 4) && actbl->ehufsi[0xf0] == 0)
1152
+ continue;
1153
+
1154
+ run_bits = (zero_run >> 4) * actbl->ehufsi[0xf0];
1155
+ zero_run &= 15;
1156
+
1157
+ for (k = 0; k < num_candidates; k++) {
1158
+ int coef_bits = actbl->ehufsi[16 * zero_run + candidate_bits[k]];
1159
+ if (coef_bits == 0)
1160
+ continue;
1161
+
1162
+ rate = coef_bits + candidate_bits[k] + run_bits;
1163
+ cost = rate + candidate_dist[k];
1164
+ cost += accumulated_zero_dist[i-1] - accumulated_zero_dist[j] + accumulated_cost[j];
1165
+
1166
+ if (cost < accumulated_cost[i]) {
1167
+ coef_blocks[bi][z] = (candidate[k] ^ sign) - sign;
1168
+ accumulated_cost[i] = cost;
1169
+ run_start[i] = j;
1170
+ }
1171
+ }
1172
+ }
1173
+ }
1174
+
1175
+ last_coeff_idx = Ss-1;
1176
+ best_cost = accumulated_zero_dist[Se] + actbl->ehufsi[0];
1177
+ cost_all_zeros = accumulated_zero_dist[Se];
1178
+ best_cost_skip = cost_all_zeros;
1179
+
1180
+ for (i = Ss; i <= Se; i++) {
1181
+ int z = jpeg_natural_order[i];
1182
+ if (coef_blocks[bi][z] != 0) {
1183
+ float cost = accumulated_cost[i] + accumulated_zero_dist[Se] - accumulated_zero_dist[i];
1184
+ float cost_wo_eob = cost;
1185
+
1186
+ if (i < Se)
1187
+ cost += actbl->ehufsi[0];
1188
+
1189
+ if (cost < best_cost) {
1190
+ best_cost = cost;
1191
+ last_coeff_idx = i;
1192
+ best_cost_skip = cost_wo_eob;
1193
+ }
1194
+ }
1195
+ }
1196
+
1197
+ has_eob = (last_coeff_idx < Se) + (last_coeff_idx == Ss-1);
1198
+
1199
+ /* Zero out coefficients that are part of runs */
1200
+ i = Se;
1201
+ while (i >= Ss)
1202
+ {
1203
+ while (i > last_coeff_idx) {
1204
+ int z = jpeg_natural_order[i];
1205
+ coef_blocks[bi][z] = 0;
1206
+ i--;
1207
+ }
1208
+ last_coeff_idx = run_start[i];
1209
+ i--;
1210
+ }
1211
+
1212
+ if (cinfo->master->trellis_eob_opt) {
1213
+ accumulated_zero_block_cost[bi+1] = accumulated_zero_block_cost[bi];
1214
+ accumulated_zero_block_cost[bi+1] += cost_all_zeros;
1215
+ requires_eob[bi+1] = has_eob;
1216
+
1217
+ best_cost = 1e38;
1218
+
1219
+ if (has_eob != 2) {
1220
+ for (i = 0; i <= bi; i++) {
1221
+ int zero_block_run;
1222
+ int nbits;
1223
+ float cost;
1224
+
1225
+ if (requires_eob[i] == 2)
1226
+ continue;
1227
+
1228
+ cost = best_cost_skip; /* cost of coding a nonzero block */
1229
+ cost += accumulated_zero_block_cost[bi];
1230
+ cost -= accumulated_zero_block_cost[i];
1231
+ cost += accumulated_block_cost[i];
1232
+ zero_block_run = bi - i + requires_eob[i];
1233
+ nbits = jpeg_nbits_table[zero_block_run];
1234
+ cost += actbl->ehufsi[16*nbits] + nbits;
1235
+
1236
+ if (cost < best_cost) {
1237
+ block_run_start[bi] = i;
1238
+ best_cost = cost;
1239
+ accumulated_block_cost[bi+1] = cost;
1240
+ }
1241
+ }
1242
+ }
1243
+ }
1244
+ }
1245
+
1246
+ if (cinfo->master->trellis_eob_opt) {
1247
+ int last_block = num_blocks;
1248
+ best_cost = 1e38;
1249
+
1250
+ for (i = 0; i <= num_blocks; i++) {
1251
+ int zero_block_run;
1252
+ int nbits;
1253
+ float cost = 0.0;
1254
+
1255
+ if (requires_eob[i] == 2)
1256
+ continue;
1257
+
1258
+ cost += accumulated_zero_block_cost[num_blocks];
1259
+ cost -= accumulated_zero_block_cost[i];
1260
+ zero_block_run = num_blocks - i + requires_eob[i];
1261
+ nbits = jpeg_nbits_table[zero_block_run];
1262
+ cost += actbl->ehufsi[16*nbits] + nbits;
1263
+ if (cost < best_cost) {
1264
+ best_cost = cost;
1265
+ last_block = i;
1266
+ }
1267
+ }
1268
+ last_block--;
1269
+ bi = num_blocks - 1;
1270
+ while (bi >= 0) {
1271
+ while (bi > last_block) {
1272
+ for (j = Ss; j <= Se; j++) {
1273
+ int z = jpeg_natural_order[j];
1274
+ coef_blocks[bi][z] = 0;
1275
+ }
1276
+ bi--;
1277
+ }
1278
+ last_block = block_run_start[bi]-1;
1279
+ bi--;
1280
+ }
1281
+ free(accumulated_zero_block_cost);
1282
+ free(accumulated_block_cost);
1283
+ free(block_run_start);
1284
+ free(requires_eob);
1285
+ }
1286
+
1287
+ if (cinfo->master->trellis_q_opt) {
1288
+ for (bi = 0; bi < num_blocks; bi++) {
1289
+ for (i = 1; i < DCTSIZE2; i++) {
1290
+ norm_src[i] += src[bi][i] * coef_blocks[bi][i];
1291
+ norm_coef[i] += 8 * coef_blocks[bi][i] * coef_blocks[bi][i];
1292
+ }
1293
+ }
1294
+ }
1295
+
1296
+ if (cinfo->master->trellis_quant_dc) {
1297
+ j = 0;
1298
+ for (i = 1; i < dc_trellis_candidates; i++) {
1299
+ if (accumulated_dc_cost[i][num_blocks-1] < accumulated_dc_cost[j][num_blocks-1])
1300
+ j = i;
1301
+ }
1302
+ for (bi = num_blocks-1; bi >= 0; bi--) {
1303
+ coef_blocks[bi][0] = dc_candidate[j][bi];
1304
+ j = dc_cost_backtrack[j][bi];
1305
+ }
1306
+
1307
+ /* Save DC predictor */
1308
+ *last_dc_val = coef_blocks[num_blocks-1][0];
1309
+
1310
+ for (i = 0; i < dc_trellis_candidates; i++) {
1311
+ free(accumulated_dc_cost[i]);
1312
+ free(dc_cost_backtrack[i]);
1313
+ free(dc_candidate[i]);
1314
+ }
1315
+ }
1316
+
1317
+ }
1318
+
1319
+ #ifdef C_ARITH_CODING_SUPPORTED
1320
+ GLOBAL(void)
1321
+ quantize_trellis_arith(j_compress_ptr cinfo, arith_rates *r, JBLOCKROW coef_blocks, JBLOCKROW src, JDIMENSION num_blocks,
1322
+ JQUANT_TBL * qtbl, double *norm_src, double *norm_coef, JCOEF *last_dc_val,
1323
+ JBLOCKROW coef_blocks_above, JBLOCKROW src_above)
1324
+ {
1325
+ int i, j, k, l;
1326
+ float accumulated_zero_dist[DCTSIZE2];
1327
+ float accumulated_cost[DCTSIZE2];
1328
+ int run_start[DCTSIZE2];
1329
+ int bi;
1330
+ float best_cost;
1331
+ int last_coeff_idx; /* position of last nonzero coefficient */
1332
+ float norm = 0.0;
1333
+ float lambda_base;
1334
+ float lambda;
1335
+ float lambda_dc;
1336
+ const float *lambda_tbl = (cinfo->master->use_lambda_weight_tbl) ?
1337
+ jpeg_lambda_weights_csf_luma :
1338
+ jpeg_lambda_weights_flat;
1339
+ int Ss, Se;
1340
+ float cost;
1341
+ float run_bits;
1342
+ int rate;
1343
+ float *accumulated_dc_cost[DC_TRELLIS_MAX_CANDIDATES];
1344
+ int *dc_cost_backtrack[DC_TRELLIS_MAX_CANDIDATES];
1345
+ JCOEF *dc_candidate[DC_TRELLIS_MAX_CANDIDATES];
1346
+ int *dc_context[DC_TRELLIS_MAX_CANDIDATES];
1347
+
1348
+ int mode = 1;
1349
+ float lambda_table[DCTSIZE2];
1350
+ const int dc_trellis_candidates = get_num_dc_trellis_candidates(qtbl->quantval[0]);
1351
+
1352
+ Ss = cinfo->Ss;
1353
+ Se = cinfo->Se;
1354
+ if (Ss == 0)
1355
+ Ss = 1;
1356
+ if (Se < Ss)
1357
+ return;
1358
+
1359
+ if (cinfo->master->trellis_quant_dc) {
1360
+ for (i = 0; i < dc_trellis_candidates; i++) {
1361
+ accumulated_dc_cost[i] = (float *)malloc(num_blocks * sizeof(float));
1362
+ dc_cost_backtrack[i] = (int *)malloc(num_blocks * sizeof(int));
1363
+ dc_candidate[i] = (JCOEF *)malloc(num_blocks * sizeof(JCOEF));
1364
+ dc_context[i] = (int *)malloc(num_blocks * sizeof(int));
1365
+ if (!accumulated_dc_cost[i] ||
1366
+ !dc_cost_backtrack[i] ||
1367
+ !dc_candidate[i] ||
1368
+ !dc_context[i]) {
1369
+ ERREXIT(cinfo, JERR_OUT_OF_MEMORY);
1370
+ }
1371
+ }
1372
+ }
1373
+
1374
+ norm = 0.0;
1375
+ for (i = 1; i < DCTSIZE2; i++) {
1376
+ norm += qtbl->quantval[i] * qtbl->quantval[i];
1377
+ }
1378
+ norm /= 63.0;
1379
+
1380
+ if (mode == 1) {
1381
+ lambda_base = 1.0;
1382
+ lambda_tbl = lambda_table;
1383
+ for (i = 0; i < DCTSIZE2; i++)
1384
+ lambda_table[i] = 1.0 / (qtbl->quantval[i] * qtbl->quantval[i]);
1385
+ } else
1386
+ lambda_base = 1.0 / norm;
1387
+
1388
+ for (bi = 0; bi < num_blocks; bi++) {
1389
+
1390
+ norm = 0.0;
1391
+ for (i = 1; i < DCTSIZE2; i++) {
1392
+ norm += src[bi][i] * src[bi][i];
1393
+ }
1394
+ norm /= 63.0;
1395
+
1396
+ if (cinfo->master->lambda_log_scale2 > 0.0)
1397
+ lambda = pow(2.0, cinfo->master->lambda_log_scale1) * lambda_base /
1398
+ (pow(2.0, cinfo->master->lambda_log_scale2) + norm);
1399
+ else
1400
+ lambda = pow(2.0, cinfo->master->lambda_log_scale1 - 12.0) * lambda_base;
1401
+
1402
+ lambda_dc = lambda * lambda_tbl[0];
1403
+
1404
+ accumulated_zero_dist[Ss-1] = 0.0;
1405
+ accumulated_cost[Ss-1] = 0.0;
1406
+
1407
+ /* Do DC coefficient */
1408
+ if (cinfo->master->trellis_quant_dc) {
1409
+ int sign = src[bi][0] >> 31;
1410
+ int x = abs(src[bi][0]);
1411
+ int q = 8 * qtbl->quantval[0];
1412
+ int qval;
1413
+ float dc_candidate_dist;
1414
+
1415
+ qval = (x + q/2) / q; /* quantized value (round nearest) */
1416
+
1417
+ /* loop over candidates in current block */
1418
+ for (k = 0; k < dc_trellis_candidates; k++) {
1419
+ int delta;
1420
+ int dc_delta;
1421
+ float bits;
1422
+ int m;
1423
+ int v2;
1424
+
1425
+ dc_candidate[k][bi] = qval - dc_trellis_candidates/2 + k;
1426
+ delta = dc_candidate[k][bi] * q - x;
1427
+ dc_candidate_dist = delta * delta * lambda_dc;
1428
+ dc_candidate[k][bi] *= 1 + 2*sign;
1429
+
1430
+ /* Take into account DC differences */
1431
+ if (coef_blocks_above && src_above && cinfo->master->trellis_delta_dc_weight > 0.0) {
1432
+ int dc_above_orig;
1433
+ int dc_above_recon;
1434
+ int dc_orig;
1435
+ int dc_recon;
1436
+ float vertical_dist;
1437
+
1438
+ dc_above_orig = src_above[bi][0];
1439
+ dc_above_recon = coef_blocks_above[bi][0] * q;
1440
+ dc_orig = src[bi][0];
1441
+ dc_recon = dc_candidate[k][bi] * q;
1442
+ /* delta is difference of vertical gradients */
1443
+ delta = (dc_above_orig - dc_orig) - (dc_above_recon - dc_recon);
1444
+ vertical_dist = delta * delta * lambda_dc;
1445
+ dc_candidate_dist += cinfo->master->trellis_delta_dc_weight * (vertical_dist - dc_candidate_dist);
1446
+ }
1447
+
1448
+ /* loop of candidates from previous block */
1449
+ for (l = 0; l < (bi == 0 ? 1 : dc_trellis_candidates); l++) {
1450
+ int dc_pred = (bi == 0 ? *last_dc_val : dc_candidate[l][bi-1]);
1451
+ int updated_dc_context = 0;
1452
+ int st = (bi == 0) ? 0 : dc_context[l][bi-1];
1453
+ dc_delta = dc_candidate[k][bi] - dc_pred;
1454
+
1455
+ bits = r->rate_dc[st][dc_delta != 0];
1456
+
1457
+ if (dc_delta != 0) {
1458
+ bits += r->rate_dc[st+1][dc_delta < 0];
1459
+ st += 2 + (dc_delta < 0);
1460
+ updated_dc_context = (dc_delta < 0) ? 8 : 4;
1461
+
1462
+ dc_delta = abs(dc_delta);
1463
+
1464
+ m = 0;
1465
+ if (dc_delta -= 1) {
1466
+ bits += r->rate_dc[st][1];
1467
+ st = 20;
1468
+ m = 1;
1469
+ v2 = dc_delta;
1470
+ while (v2 >>= 1) {
1471
+ bits += r->rate_dc[st][1];
1472
+ m <<= 1;
1473
+ st++;
1474
+ }
1475
+ }
1476
+ bits += r->rate_dc[st][0];
1477
+
1478
+ if (m < (int) ((1L << r->arith_dc_L) >> 1))
1479
+ updated_dc_context = 0; /* zero diff category */
1480
+ else if (m > (int) ((1L << r->arith_dc_U) >> 1))
1481
+ updated_dc_context += 8; /* large diff category */
1482
+
1483
+ st += 14;
1484
+ while (m >>= 1)
1485
+ bits += r->rate_dc[st][(m & dc_delta) ? 1 : 0];
1486
+ }
1487
+
1488
+ cost = bits + dc_candidate_dist;
1489
+ if (bi != 0)
1490
+ cost += accumulated_dc_cost[l][bi-1];
1491
+
1492
+ if (l == 0 || cost < accumulated_dc_cost[k][bi]) {
1493
+ accumulated_dc_cost[k][bi] = cost;
1494
+ dc_cost_backtrack[k][bi] = (bi == 0 ? -1 : l);
1495
+ dc_context[k][bi] = updated_dc_context;
1496
+ }
1497
+ }
1498
+ }
1499
+ }
1500
+
1501
+ /* Do AC coefficients */
1502
+ for (i = Ss; i <= Se; i++) {
1503
+ int z = jpeg_natural_order[i];
1504
+
1505
+ int sign = src[bi][z] >> 31;
1506
+ int x = abs(src[bi][z]);
1507
+ int q = 8 * qtbl->quantval[z];
1508
+ int candidate[16];
1509
+ float candidate_dist[16];
1510
+ int num_candidates;
1511
+ int qval;
1512
+ int delta;
1513
+
1514
+ accumulated_zero_dist[i] = x * x * lambda * lambda_tbl[z] + accumulated_zero_dist[i-1];
1515
+
1516
+ qval = (x + q/2) / q; /* quantized value (round nearest) */
1517
+
1518
+ if (qval == 0) {
1519
+ coef_blocks[bi][z] = 0;
1520
+ accumulated_cost[i] = 1e38; /* Shouldn't be needed */
1521
+ continue;
1522
+ }
1523
+
1524
+ k = 0;
1525
+ candidate[k] = qval;
1526
+ delta = candidate[k] * q - x;
1527
+ candidate_dist[k] = delta * delta * lambda * lambda_tbl[z];
1528
+ k++;
1529
+ if (qval > 1) {
1530
+ candidate[k] = qval - 1;
1531
+ delta = candidate[k] * q - x;
1532
+ candidate_dist[k] = delta * delta * lambda * lambda_tbl[z];
1533
+ k++;
1534
+ }
1535
+ num_candidates = k;
1536
+
1537
+ accumulated_cost[i] = 1e38;
1538
+
1539
+ for (j = Ss-1; j < i; j++) {
1540
+ int zz = jpeg_natural_order[j];
1541
+ if (j != Ss-1 && coef_blocks[bi][zz] == 0)
1542
+ continue;
1543
+
1544
+ run_bits = r->rate_ac[3*j][0]; /* EOB */
1545
+ for (k = j+1; k < i; k++)
1546
+ run_bits += r->rate_ac[3*(k-1)+1][0];
1547
+ run_bits += r->rate_ac[3*(i-1)+1][1];
1548
+
1549
+ for (k = 0; k < num_candidates; k++) {
1550
+ float coef_bits = 1.0; /* sign bit */
1551
+ int v = candidate[k];
1552
+ int v2;
1553
+ int m;
1554
+ int st;
1555
+
1556
+ st = 3*(i-1)+2;
1557
+ m = 0;
1558
+ if (v -= 1) {
1559
+ coef_bits += r->rate_ac[st][1];
1560
+ m = 1;
1561
+ v2 = v;
1562
+ if (v2 >>= 1) {
1563
+ coef_bits += r->rate_ac[st][1];
1564
+ m <<= 1;
1565
+ st = (i <= r->arith_ac_K) ? 189 : 217;
1566
+ while (v2 >>= 1) {
1567
+ coef_bits += r->rate_ac[st][1];
1568
+ m <<= 1;
1569
+ st++;
1570
+ }
1571
+ }
1572
+ }
1573
+ coef_bits += r->rate_ac[st][0];
1574
+ st += 14;
1575
+ while (m >>= 1)
1576
+ coef_bits += r->rate_ac[st][(m & v) ? 1 : 0];
1577
+
1578
+ rate = coef_bits + run_bits;
1579
+ cost = rate + candidate_dist[k];
1580
+ cost += accumulated_zero_dist[i-1] - accumulated_zero_dist[j] + accumulated_cost[j];
1581
+
1582
+ if (cost < accumulated_cost[i]) {
1583
+ coef_blocks[bi][z] = (candidate[k] ^ sign) - sign;
1584
+ accumulated_cost[i] = cost;
1585
+ run_start[i] = j;
1586
+ }
1587
+ }
1588
+ }
1589
+ }
1590
+
1591
+ last_coeff_idx = Ss-1;
1592
+ best_cost = accumulated_zero_dist[Se] + r->rate_ac[0][1];
1593
+
1594
+ for (i = Ss; i <= Se; i++) {
1595
+ int z = jpeg_natural_order[i];
1596
+ if (coef_blocks[bi][z] != 0) {
1597
+ float cost = accumulated_cost[i] + accumulated_zero_dist[Se] - accumulated_zero_dist[i];
1598
+
1599
+ if (i < Se)
1600
+ cost += r->rate_ac[3*(i-1)][1];
1601
+
1602
+ if (cost < best_cost) {
1603
+ best_cost = cost;
1604
+ last_coeff_idx = i;
1605
+ }
1606
+ }
1607
+ }
1608
+
1609
+ /* Zero out coefficients that are part of runs */
1610
+ i = Se;
1611
+ while (i >= Ss)
1612
+ {
1613
+ while (i > last_coeff_idx) {
1614
+ int z = jpeg_natural_order[i];
1615
+ coef_blocks[bi][z] = 0;
1616
+ i--;
1617
+ }
1618
+ last_coeff_idx = run_start[i];
1619
+ i--;
1620
+ }
1621
+
1622
+ }
1623
+
1624
+ if (cinfo->master->trellis_q_opt) {
1625
+ for (bi = 0; bi < num_blocks; bi++) {
1626
+ for (i = 1; i < DCTSIZE2; i++) {
1627
+ norm_src[i] += src[bi][i] * coef_blocks[bi][i];
1628
+ norm_coef[i] += 8 * coef_blocks[bi][i] * coef_blocks[bi][i];
1629
+ }
1630
+ }
1631
+ }
1632
+
1633
+ if (cinfo->master->trellis_quant_dc) {
1634
+ j = 0;
1635
+ for (i = 1; i < dc_trellis_candidates; i++) {
1636
+ if (accumulated_dc_cost[i][num_blocks-1] < accumulated_dc_cost[j][num_blocks-1])
1637
+ j = i;
1638
+ }
1639
+ for (bi = num_blocks-1; bi >= 0; bi--) {
1640
+ coef_blocks[bi][0] = dc_candidate[j][bi];
1641
+ j = dc_cost_backtrack[j][bi];
1642
+ }
1643
+
1644
+ /* Save DC predictor */
1645
+ *last_dc_val = coef_blocks[num_blocks-1][0];
1646
+
1647
+ for (i = 0; i < dc_trellis_candidates; i++) {
1648
+ free(accumulated_dc_cost[i]);
1649
+ free(dc_cost_backtrack[i]);
1650
+ free(dc_candidate[i]);
1651
+ free(dc_context[i]);
1652
+ }
1653
+ }
1654
+ }
1655
+ #endif
1656
+
1657
+ /*
1658
+ * Initialize FDCT manager.
1659
+ */
1660
+
1661
+ GLOBAL(void)
1662
+ jinit_forward_dct (j_compress_ptr cinfo)
1663
+ {
1664
+ my_fdct_ptr fdct;
1665
+ int i;
1666
+
1667
+ fdct = (my_fdct_ptr)
1668
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1669
+ sizeof(my_fdct_controller));
1670
+ cinfo->fdct = (struct jpeg_forward_dct *) fdct;
1671
+ fdct->pub.start_pass = start_pass_fdctmgr;
1672
+
1673
+ /* First determine the DCT... */
1674
+ switch (cinfo->dct_method) {
1675
+ #ifdef DCT_ISLOW_SUPPORTED
1676
+ case JDCT_ISLOW:
1677
+ fdct->pub.forward_DCT = forward_DCT;
1678
+ if (jsimd_can_fdct_islow())
1679
+ fdct->dct = jsimd_fdct_islow;
1680
+ else
1681
+ fdct->dct = jpeg_fdct_islow;
1682
+ break;
1683
+ #endif
1684
+ #ifdef DCT_IFAST_SUPPORTED
1685
+ case JDCT_IFAST:
1686
+ fdct->pub.forward_DCT = forward_DCT;
1687
+ if (jsimd_can_fdct_ifast())
1688
+ fdct->dct = jsimd_fdct_ifast;
1689
+ else
1690
+ fdct->dct = jpeg_fdct_ifast;
1691
+ break;
1692
+ #endif
1693
+ #ifdef DCT_FLOAT_SUPPORTED
1694
+ case JDCT_FLOAT:
1695
+ fdct->pub.forward_DCT = forward_DCT_float;
1696
+ if (jsimd_can_fdct_float())
1697
+ fdct->float_dct = jsimd_fdct_float;
1698
+ else
1699
+ fdct->float_dct = jpeg_fdct_float;
1700
+ break;
1701
+ #endif
1702
+ default:
1703
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
1704
+ break;
1705
+ }
1706
+
1707
+ /* ...then the supporting stages. */
1708
+ switch (cinfo->dct_method) {
1709
+ #ifdef DCT_ISLOW_SUPPORTED
1710
+ case JDCT_ISLOW:
1711
+ #endif
1712
+ #ifdef DCT_IFAST_SUPPORTED
1713
+ case JDCT_IFAST:
1714
+ #endif
1715
+ #if defined(DCT_ISLOW_SUPPORTED) || defined(DCT_IFAST_SUPPORTED)
1716
+ if (jsimd_can_convsamp())
1717
+ fdct->convsamp = jsimd_convsamp;
1718
+ else
1719
+ fdct->convsamp = convsamp;
1720
+
1721
+ if (cinfo->master->overshoot_deringing) {
1722
+ fdct->preprocess = preprocess_deringing;
1723
+ } else {
1724
+ fdct->preprocess = NULL;
1725
+ }
1726
+
1727
+ if (jsimd_can_quantize())
1728
+ fdct->quantize = jsimd_quantize;
1729
+ else
1730
+ fdct->quantize = quantize;
1731
+ break;
1732
+ #endif
1733
+ #ifdef DCT_FLOAT_SUPPORTED
1734
+ case JDCT_FLOAT:
1735
+ if (jsimd_can_convsamp_float())
1736
+ fdct->float_convsamp = jsimd_convsamp_float;
1737
+ else
1738
+ fdct->float_convsamp = convsamp_float;
1739
+
1740
+ if (cinfo->master->overshoot_deringing) {
1741
+ fdct->float_preprocess = float_preprocess_deringing;
1742
+ } else {
1743
+ fdct->float_preprocess = NULL;
1744
+ }
1745
+
1746
+ if (jsimd_can_quantize_float())
1747
+ fdct->float_quantize = jsimd_quantize_float;
1748
+ else
1749
+ fdct->float_quantize = quantize_float;
1750
+ break;
1751
+ #endif
1752
+ default:
1753
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
1754
+ break;
1755
+ }
1756
+
1757
+ /* Allocate workspace memory */
1758
+ #ifdef DCT_FLOAT_SUPPORTED
1759
+ if (cinfo->dct_method == JDCT_FLOAT)
1760
+ fdct->float_workspace = (FAST_FLOAT *)
1761
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1762
+ sizeof(FAST_FLOAT) * DCTSIZE2);
1763
+ else
1764
+ #endif
1765
+ fdct->workspace = (DCTELEM *)
1766
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1767
+ sizeof(DCTELEM) * DCTSIZE2);
1768
+
1769
+ /* Mark divisor tables unallocated */
1770
+ for (i = 0; i < NUM_QUANT_TBLS; i++) {
1771
+ fdct->divisors[i] = NULL;
1772
+ #ifdef DCT_FLOAT_SUPPORTED
1773
+ fdct->float_divisors[i] = NULL;
1774
+ #endif
1775
+ }
1776
+ }