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,2320 @@
1
+ /*
2
+ * Copyright (C)2009-2023 D. R. Commander. All Rights Reserved.
3
+ * Copyright (C)2021 Alex Richardson. All Rights Reserved.
4
+ * mozjpeg Modifications:
5
+ * Copyright (C) 2014, Mozilla Corporation.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * - Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ * - Redistributions in binary form must reproduce the above copyright notice,
13
+ * this list of conditions and the following disclaimer in the documentation
14
+ * and/or other materials provided with the distribution.
15
+ * - Neither the name of the libjpeg-turbo Project nor the names of its
16
+ * contributors may be used to endorse or promote products derived from this
17
+ * software without specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
+ * POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+
32
+ /* TurboJPEG/LJT: this implements the TurboJPEG API using libjpeg or
33
+ libjpeg-turbo */
34
+
35
+ #include <ctype.h>
36
+ #include <limits.h>
37
+ #include <jinclude.h>
38
+ #define JPEG_INTERNALS
39
+ #include <jpeglib.h>
40
+ #include <jerror.h>
41
+ #include <setjmp.h>
42
+ #include <errno.h>
43
+ #include "./turbojpeg.h"
44
+ #include "./tjutil.h"
45
+ #include "transupp.h"
46
+ #include "./jpegcomp.h"
47
+ #include "./cdjpeg.h"
48
+
49
+ extern void jpeg_mem_dest_tj(j_compress_ptr, unsigned char **, unsigned long *,
50
+ boolean);
51
+ extern void jpeg_mem_src_tj(j_decompress_ptr, const unsigned char *,
52
+ unsigned long);
53
+
54
+ #define PAD(v, p) ((v + (p) - 1) & (~((p) - 1)))
55
+ #define IS_POW2(x) (((x) & (x - 1)) == 0)
56
+
57
+
58
+ /* Error handling (based on example in example.txt) */
59
+
60
+ static THREAD_LOCAL char errStr[JMSG_LENGTH_MAX] = "No error";
61
+
62
+ struct my_error_mgr {
63
+ struct jpeg_error_mgr pub;
64
+ jmp_buf setjmp_buffer;
65
+ void (*emit_message) (j_common_ptr, int);
66
+ boolean warning, stopOnWarning;
67
+ };
68
+ typedef struct my_error_mgr *my_error_ptr;
69
+
70
+ #define JMESSAGE(code, string) string,
71
+ static const char *turbojpeg_message_table[] = {
72
+ #include "cderror.h"
73
+ NULL
74
+ };
75
+
76
+ static void my_error_exit(j_common_ptr cinfo)
77
+ {
78
+ my_error_ptr myerr = (my_error_ptr)cinfo->err;
79
+
80
+ (*cinfo->err->output_message) (cinfo);
81
+ longjmp(myerr->setjmp_buffer, 1);
82
+ }
83
+
84
+ /* Based on output_message() in jerror.c */
85
+
86
+ static void my_output_message(j_common_ptr cinfo)
87
+ {
88
+ (*cinfo->err->format_message) (cinfo, errStr);
89
+ }
90
+
91
+ static void my_emit_message(j_common_ptr cinfo, int msg_level)
92
+ {
93
+ my_error_ptr myerr = (my_error_ptr)cinfo->err;
94
+
95
+ myerr->emit_message(cinfo, msg_level);
96
+ if (msg_level < 0) {
97
+ myerr->warning = TRUE;
98
+ if (myerr->stopOnWarning) longjmp(myerr->setjmp_buffer, 1);
99
+ }
100
+ }
101
+
102
+
103
+ /********************** Global structures, macros, etc. **********************/
104
+
105
+ enum { COMPRESS = 1, DECOMPRESS = 2 };
106
+
107
+ typedef struct _tjinstance {
108
+ struct jpeg_compress_struct cinfo;
109
+ struct jpeg_decompress_struct dinfo;
110
+ struct my_error_mgr jerr;
111
+ int init, headerRead;
112
+ char errStr[JMSG_LENGTH_MAX];
113
+ boolean isInstanceError;
114
+ } tjinstance;
115
+
116
+ struct my_progress_mgr {
117
+ struct jpeg_progress_mgr pub;
118
+ tjinstance *this;
119
+ };
120
+ typedef struct my_progress_mgr *my_progress_ptr;
121
+
122
+ static void my_progress_monitor(j_common_ptr dinfo)
123
+ {
124
+ my_error_ptr myerr = (my_error_ptr)dinfo->err;
125
+ my_progress_ptr myprog = (my_progress_ptr)dinfo->progress;
126
+
127
+ if (dinfo->is_decompressor) {
128
+ int scan_no = ((j_decompress_ptr)dinfo)->input_scan_number;
129
+
130
+ if (scan_no > 500) {
131
+ SNPRINTF(myprog->this->errStr, JMSG_LENGTH_MAX,
132
+ "Progressive JPEG image has more than 500 scans");
133
+ SNPRINTF(errStr, JMSG_LENGTH_MAX,
134
+ "Progressive JPEG image has more than 500 scans");
135
+ myprog->this->isInstanceError = TRUE;
136
+ myerr->warning = FALSE;
137
+ longjmp(myerr->setjmp_buffer, 1);
138
+ }
139
+ }
140
+ }
141
+
142
+ static const int pixelsize[TJ_NUMSAMP] = { 3, 3, 3, 1, 3, 3 };
143
+
144
+ static const JXFORM_CODE xformtypes[TJ_NUMXOP] = {
145
+ JXFORM_NONE, JXFORM_FLIP_H, JXFORM_FLIP_V, JXFORM_TRANSPOSE,
146
+ JXFORM_TRANSVERSE, JXFORM_ROT_90, JXFORM_ROT_180, JXFORM_ROT_270
147
+ };
148
+
149
+ #define NUMSF 16
150
+ static const tjscalingfactor sf[NUMSF] = {
151
+ { 2, 1 },
152
+ { 15, 8 },
153
+ { 7, 4 },
154
+ { 13, 8 },
155
+ { 3, 2 },
156
+ { 11, 8 },
157
+ { 5, 4 },
158
+ { 9, 8 },
159
+ { 1, 1 },
160
+ { 7, 8 },
161
+ { 3, 4 },
162
+ { 5, 8 },
163
+ { 1, 2 },
164
+ { 3, 8 },
165
+ { 1, 4 },
166
+ { 1, 8 }
167
+ };
168
+
169
+ static J_COLOR_SPACE pf2cs[TJ_NUMPF] = {
170
+ JCS_EXT_RGB, JCS_EXT_BGR, JCS_EXT_RGBX, JCS_EXT_BGRX, JCS_EXT_XBGR,
171
+ JCS_EXT_XRGB, JCS_GRAYSCALE, JCS_EXT_RGBA, JCS_EXT_BGRA, JCS_EXT_ABGR,
172
+ JCS_EXT_ARGB, JCS_CMYK
173
+ };
174
+
175
+ static int cs2pf[JPEG_NUMCS] = {
176
+ TJPF_UNKNOWN, TJPF_GRAY,
177
+ #if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
178
+ TJPF_RGB,
179
+ #elif RGB_RED == 2 && RGB_GREEN == 1 && RGB_BLUE == 0 && RGB_PIXELSIZE == 3
180
+ TJPF_BGR,
181
+ #elif RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 4
182
+ TJPF_RGBX,
183
+ #elif RGB_RED == 2 && RGB_GREEN == 1 && RGB_BLUE == 0 && RGB_PIXELSIZE == 4
184
+ TJPF_BGRX,
185
+ #elif RGB_RED == 3 && RGB_GREEN == 2 && RGB_BLUE == 1 && RGB_PIXELSIZE == 4
186
+ TJPF_XBGR,
187
+ #elif RGB_RED == 1 && RGB_GREEN == 2 && RGB_BLUE == 3 && RGB_PIXELSIZE == 4
188
+ TJPF_XRGB,
189
+ #endif
190
+ TJPF_UNKNOWN, TJPF_CMYK, TJPF_UNKNOWN, TJPF_RGB, TJPF_RGBX, TJPF_BGR,
191
+ TJPF_BGRX, TJPF_XBGR, TJPF_XRGB, TJPF_RGBA, TJPF_BGRA, TJPF_ABGR, TJPF_ARGB,
192
+ TJPF_UNKNOWN
193
+ };
194
+
195
+ #define THROWG(m) { \
196
+ SNPRINTF(errStr, JMSG_LENGTH_MAX, "%s", m); \
197
+ retval = -1; goto bailout; \
198
+ }
199
+ #ifdef _MSC_VER
200
+ #define THROW_UNIX(m) { \
201
+ char strerrorBuf[80] = { 0 }; \
202
+ strerror_s(strerrorBuf, 80, errno); \
203
+ SNPRINTF(errStr, JMSG_LENGTH_MAX, "%s\n%s", m, strerrorBuf); \
204
+ retval = -1; goto bailout; \
205
+ }
206
+ #else
207
+ #define THROW_UNIX(m) { \
208
+ SNPRINTF(errStr, JMSG_LENGTH_MAX, "%s\n%s", m, strerror(errno)); \
209
+ retval = -1; goto bailout; \
210
+ }
211
+ #endif
212
+ #define THROW(m) { \
213
+ SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s", m); \
214
+ this->isInstanceError = TRUE; THROWG(m) \
215
+ }
216
+
217
+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
218
+ /* Private flag that triggers different TurboJPEG API behavior when fuzzing */
219
+ #define TJFLAG_FUZZING (1 << 30)
220
+ #endif
221
+
222
+ #define GET_INSTANCE(handle) \
223
+ tjinstance *this = (tjinstance *)handle; \
224
+ j_compress_ptr cinfo = NULL; \
225
+ j_decompress_ptr dinfo = NULL; \
226
+ \
227
+ if (!this) { \
228
+ SNPRINTF(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
229
+ return -1; \
230
+ } \
231
+ cinfo = &this->cinfo; dinfo = &this->dinfo; \
232
+ this->jerr.warning = FALSE; \
233
+ this->isInstanceError = FALSE;
234
+
235
+ #define GET_CINSTANCE(handle) \
236
+ tjinstance *this = (tjinstance *)handle; \
237
+ j_compress_ptr cinfo = NULL; \
238
+ \
239
+ if (!this) { \
240
+ SNPRINTF(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
241
+ return -1; \
242
+ } \
243
+ cinfo = &this->cinfo; \
244
+ this->jerr.warning = FALSE; \
245
+ this->isInstanceError = FALSE;
246
+
247
+ #define GET_DINSTANCE(handle) \
248
+ tjinstance *this = (tjinstance *)handle; \
249
+ j_decompress_ptr dinfo = NULL; \
250
+ \
251
+ if (!this) { \
252
+ SNPRINTF(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
253
+ return -1; \
254
+ } \
255
+ dinfo = &this->dinfo; \
256
+ this->jerr.warning = FALSE; \
257
+ this->isInstanceError = FALSE;
258
+
259
+ static int getPixelFormat(int pixelSize, int flags)
260
+ {
261
+ if (pixelSize == 1) return TJPF_GRAY;
262
+ if (pixelSize == 3) {
263
+ if (flags & TJ_BGR) return TJPF_BGR;
264
+ else return TJPF_RGB;
265
+ }
266
+ if (pixelSize == 4) {
267
+ if (flags & TJ_ALPHAFIRST) {
268
+ if (flags & TJ_BGR) return TJPF_XBGR;
269
+ else return TJPF_XRGB;
270
+ } else {
271
+ if (flags & TJ_BGR) return TJPF_BGRX;
272
+ else return TJPF_RGBX;
273
+ }
274
+ }
275
+ return -1;
276
+ }
277
+
278
+ static void setCompDefaults(struct jpeg_compress_struct *cinfo,
279
+ int pixelFormat, int subsamp, int jpegQual,
280
+ int flags)
281
+ {
282
+ #ifndef NO_GETENV
283
+ char env[7] = { 0 };
284
+ #endif
285
+
286
+ cinfo->in_color_space = pf2cs[pixelFormat];
287
+ cinfo->input_components = tjPixelSize[pixelFormat];
288
+ #ifndef NO_GETENV
289
+ if (!GETENV_S(env, 7, "TJ_REVERT") && !strcmp(env, "1"))
290
+ cinfo->master->compress_profile=JCP_FASTEST;
291
+ #endif
292
+ jpeg_set_defaults(cinfo);
293
+
294
+ #ifndef NO_GETENV
295
+ if (!GETENV_S(env, 7, "TJ_OPTIMIZE") && !strcmp(env, "1"))
296
+ cinfo->optimize_coding = TRUE;
297
+ if (!GETENV_S(env, 7, "TJ_ARITHMETIC") && !strcmp(env, "1"))
298
+ cinfo->arith_code = TRUE;
299
+ if (!GETENV_S(env, 7, "TJ_RESTART") && strlen(env) > 0) {
300
+ int temp = -1;
301
+ char tempc = 0;
302
+
303
+ #ifdef _MSC_VER
304
+ if (sscanf_s(env, "%d%c", &temp, &tempc, 1) >= 1 && temp >= 0 &&
305
+ temp <= 65535) {
306
+ #else
307
+ if (sscanf(env, "%d%c", &temp, &tempc) >= 1 && temp >= 0 &&
308
+ temp <= 65535) {
309
+ #endif
310
+ if (toupper(tempc) == 'B') {
311
+ cinfo->restart_interval = temp;
312
+ cinfo->restart_in_rows = 0;
313
+ } else
314
+ cinfo->restart_in_rows = temp;
315
+ }
316
+ }
317
+ #endif
318
+
319
+ if (jpegQual >= 0) {
320
+ jpeg_set_quality(cinfo, jpegQual, TRUE);
321
+ if (jpegQual >= 96 || flags & TJFLAG_ACCURATEDCT)
322
+ cinfo->dct_method = JDCT_ISLOW;
323
+ else
324
+ cinfo->dct_method = JDCT_FASTEST;
325
+ }
326
+ if (subsamp == TJSAMP_GRAY)
327
+ jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
328
+ else if (pixelFormat == TJPF_CMYK)
329
+ jpeg_set_colorspace(cinfo, JCS_YCCK);
330
+ else
331
+ jpeg_set_colorspace(cinfo, JCS_YCbCr);
332
+
333
+ #ifdef C_PROGRESSIVE_SUPPORTED
334
+ if (flags & TJFLAG_PROGRESSIVE)
335
+ jpeg_simple_progression(cinfo);
336
+ #ifndef NO_GETENV
337
+ else if (!GETENV_S(env, 7, "TJ_PROGRESSIVE") && !strcmp(env, "1"))
338
+ jpeg_simple_progression(cinfo);
339
+ #endif
340
+ #endif
341
+
342
+ /* Set scan pattern again as colorspace might have changed */
343
+ if(cinfo->master->compress_profile == JCP_MAX_COMPRESSION)
344
+ jpeg_simple_progression(cinfo);
345
+
346
+ cinfo->comp_info[0].h_samp_factor = tjMCUWidth[subsamp] / 8;
347
+ cinfo->comp_info[1].h_samp_factor = 1;
348
+ cinfo->comp_info[2].h_samp_factor = 1;
349
+ if (cinfo->num_components > 3)
350
+ cinfo->comp_info[3].h_samp_factor = tjMCUWidth[subsamp] / 8;
351
+ cinfo->comp_info[0].v_samp_factor = tjMCUHeight[subsamp] / 8;
352
+ cinfo->comp_info[1].v_samp_factor = 1;
353
+ cinfo->comp_info[2].v_samp_factor = 1;
354
+ if (cinfo->num_components > 3)
355
+ cinfo->comp_info[3].v_samp_factor = tjMCUHeight[subsamp] / 8;
356
+ }
357
+
358
+
359
+ static int getSubsamp(j_decompress_ptr dinfo)
360
+ {
361
+ int retval = -1, i, k;
362
+
363
+ /* The sampling factors actually have no meaning with grayscale JPEG files,
364
+ and in fact it's possible to generate grayscale JPEGs with sampling
365
+ factors > 1 (even though those sampling factors are ignored by the
366
+ decompressor.) Thus, we need to treat grayscale as a special case. */
367
+ if (dinfo->num_components == 1 && dinfo->jpeg_color_space == JCS_GRAYSCALE)
368
+ return TJSAMP_GRAY;
369
+
370
+ for (i = 0; i < TJ_NUMSAMP; i++) {
371
+ if (dinfo->num_components == pixelsize[i] ||
372
+ ((dinfo->jpeg_color_space == JCS_YCCK ||
373
+ dinfo->jpeg_color_space == JCS_CMYK) &&
374
+ pixelsize[i] == 3 && dinfo->num_components == 4)) {
375
+ if (dinfo->comp_info[0].h_samp_factor == tjMCUWidth[i] / 8 &&
376
+ dinfo->comp_info[0].v_samp_factor == tjMCUHeight[i] / 8) {
377
+ int match = 0;
378
+
379
+ for (k = 1; k < dinfo->num_components; k++) {
380
+ int href = 1, vref = 1;
381
+
382
+ if ((dinfo->jpeg_color_space == JCS_YCCK ||
383
+ dinfo->jpeg_color_space == JCS_CMYK) && k == 3) {
384
+ href = tjMCUWidth[i] / 8; vref = tjMCUHeight[i] / 8;
385
+ }
386
+ if (dinfo->comp_info[k].h_samp_factor == href &&
387
+ dinfo->comp_info[k].v_samp_factor == vref)
388
+ match++;
389
+ }
390
+ if (match == dinfo->num_components - 1) {
391
+ retval = i; break;
392
+ }
393
+ }
394
+ /* Handle 4:2:2 and 4:4:0 images whose sampling factors are specified
395
+ in non-standard ways. */
396
+ if (dinfo->comp_info[0].h_samp_factor == 2 &&
397
+ dinfo->comp_info[0].v_samp_factor == 2 &&
398
+ (i == TJSAMP_422 || i == TJSAMP_440)) {
399
+ int match = 0;
400
+
401
+ for (k = 1; k < dinfo->num_components; k++) {
402
+ int href = tjMCUHeight[i] / 8, vref = tjMCUWidth[i] / 8;
403
+
404
+ if ((dinfo->jpeg_color_space == JCS_YCCK ||
405
+ dinfo->jpeg_color_space == JCS_CMYK) && k == 3) {
406
+ href = vref = 2;
407
+ }
408
+ if (dinfo->comp_info[k].h_samp_factor == href &&
409
+ dinfo->comp_info[k].v_samp_factor == vref)
410
+ match++;
411
+ }
412
+ if (match == dinfo->num_components - 1) {
413
+ retval = i; break;
414
+ }
415
+ }
416
+ /* Handle 4:4:4 images whose sampling factors are specified in
417
+ non-standard ways. */
418
+ if (dinfo->comp_info[0].h_samp_factor *
419
+ dinfo->comp_info[0].v_samp_factor <=
420
+ D_MAX_BLOCKS_IN_MCU / pixelsize[i] && i == TJSAMP_444) {
421
+ int match = 0;
422
+ for (k = 1; k < dinfo->num_components; k++) {
423
+ if (dinfo->comp_info[k].h_samp_factor ==
424
+ dinfo->comp_info[0].h_samp_factor &&
425
+ dinfo->comp_info[k].v_samp_factor ==
426
+ dinfo->comp_info[0].v_samp_factor)
427
+ match++;
428
+ if (match == dinfo->num_components - 1) {
429
+ retval = i; break;
430
+ }
431
+ }
432
+ }
433
+ }
434
+ }
435
+ return retval;
436
+ }
437
+
438
+
439
+ /*************************** General API functions ***************************/
440
+
441
+ /* TurboJPEG 2.0+ */
442
+ DLLEXPORT char *tjGetErrorStr2(tjhandle handle)
443
+ {
444
+ tjinstance *this = (tjinstance *)handle;
445
+
446
+ if (this && this->isInstanceError) {
447
+ this->isInstanceError = FALSE;
448
+ return this->errStr;
449
+ } else
450
+ return errStr;
451
+ }
452
+
453
+
454
+ /* TurboJPEG 1.0+ */
455
+ DLLEXPORT char *tjGetErrorStr(void)
456
+ {
457
+ return errStr;
458
+ }
459
+
460
+
461
+ /* TurboJPEG 2.0+ */
462
+ DLLEXPORT int tjGetErrorCode(tjhandle handle)
463
+ {
464
+ tjinstance *this = (tjinstance *)handle;
465
+
466
+ if (this && this->jerr.warning) return TJERR_WARNING;
467
+ else return TJERR_FATAL;
468
+ }
469
+
470
+
471
+ /* TurboJPEG 1.0+ */
472
+ DLLEXPORT int tjDestroy(tjhandle handle)
473
+ {
474
+ GET_INSTANCE(handle);
475
+
476
+ if (setjmp(this->jerr.setjmp_buffer)) return -1;
477
+ if (this->init & COMPRESS) jpeg_destroy_compress(cinfo);
478
+ if (this->init & DECOMPRESS) jpeg_destroy_decompress(dinfo);
479
+ free(this);
480
+ return 0;
481
+ }
482
+
483
+
484
+ /* These are exposed mainly because Windows can't malloc() and free() across
485
+ DLL boundaries except when the CRT DLL is used, and we don't use the CRT DLL
486
+ with turbojpeg.dll for compatibility reasons. However, these functions
487
+ can potentially be used for other purposes by different implementations. */
488
+
489
+ /* TurboJPEG 1.2+ */
490
+ DLLEXPORT void tjFree(unsigned char *buf)
491
+ {
492
+ free(buf);
493
+ }
494
+
495
+
496
+ /* TurboJPEG 1.2+ */
497
+ DLLEXPORT unsigned char *tjAlloc(int bytes)
498
+ {
499
+ return (unsigned char *)malloc(bytes);
500
+ }
501
+
502
+
503
+ /******************************** Compressor *********************************/
504
+
505
+ static tjhandle _tjInitCompress(tjinstance *this)
506
+ {
507
+ static unsigned char buffer[1];
508
+ unsigned char *buf = buffer;
509
+ unsigned long size = 1;
510
+
511
+ /* This is also straight out of example.txt */
512
+ this->cinfo.err = jpeg_std_error(&this->jerr.pub);
513
+ this->jerr.pub.error_exit = my_error_exit;
514
+ this->jerr.pub.output_message = my_output_message;
515
+ this->jerr.emit_message = this->jerr.pub.emit_message;
516
+ this->jerr.pub.emit_message = my_emit_message;
517
+ this->jerr.pub.addon_message_table = turbojpeg_message_table;
518
+ this->jerr.pub.first_addon_message = JMSG_FIRSTADDONCODE;
519
+ this->jerr.pub.last_addon_message = JMSG_LASTADDONCODE;
520
+
521
+ if (setjmp(this->jerr.setjmp_buffer)) {
522
+ /* If we get here, the JPEG code has signaled an error. */
523
+ free(this);
524
+ return NULL;
525
+ }
526
+
527
+ jpeg_create_compress(&this->cinfo);
528
+
529
+ #ifndef NO_GETENV
530
+ /* This is used in unit tests */
531
+ char *env = NULL;
532
+ if((env=getenv("TJ_REVERT"))!=NULL && strlen(env)>0 && !strcmp(env, "1"))
533
+ jpeg_c_set_int_param(&this->cinfo, JINT_COMPRESS_PROFILE, JCP_FASTEST);
534
+ #endif
535
+
536
+ /* Make an initial call so it will create the destination manager */
537
+ jpeg_mem_dest_tj(&this->cinfo, &buf, &size, 0);
538
+
539
+ this->init |= COMPRESS;
540
+ return (tjhandle)this;
541
+ }
542
+
543
+ /* TurboJPEG 1.0+ */
544
+ DLLEXPORT tjhandle tjInitCompress(void)
545
+ {
546
+ tjinstance *this = NULL;
547
+
548
+ if ((this = (tjinstance *)malloc(sizeof(tjinstance))) == NULL) {
549
+ SNPRINTF(errStr, JMSG_LENGTH_MAX,
550
+ "tjInitCompress(): Memory allocation failure");
551
+ return NULL;
552
+ }
553
+ memset(this, 0, sizeof(tjinstance));
554
+ SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "No error");
555
+ return _tjInitCompress(this);
556
+ }
557
+
558
+
559
+ /* TurboJPEG 1.2+ */
560
+ DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp)
561
+ {
562
+ unsigned long long retval = 0;
563
+ int mcuw, mcuh, chromasf;
564
+
565
+ if (width < 1 || height < 1 || jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP)
566
+ THROWG("tjBufSize(): Invalid argument");
567
+
568
+ /* This allows for rare corner cases in which a JPEG image can actually be
569
+ larger than the uncompressed input (we wouldn't mention it if it hadn't
570
+ happened before.) */
571
+ mcuw = tjMCUWidth[jpegSubsamp];
572
+ mcuh = tjMCUHeight[jpegSubsamp];
573
+ chromasf = jpegSubsamp == TJSAMP_GRAY ? 0 : 4 * 64 / (mcuw * mcuh);
574
+ retval = PAD(width, mcuw) * PAD(height, mcuh) * (2ULL + chromasf) + 2048ULL;
575
+ if (retval > (unsigned long long)((unsigned long)-1))
576
+ THROWG("tjBufSize(): Image is too large");
577
+
578
+ bailout:
579
+ return (unsigned long)retval;
580
+ }
581
+
582
+ /* TurboJPEG 1.0+ */
583
+ DLLEXPORT unsigned long TJBUFSIZE(int width, int height)
584
+ {
585
+ unsigned long long retval = 0;
586
+
587
+ if (width < 1 || height < 1)
588
+ THROWG("TJBUFSIZE(): Invalid argument");
589
+
590
+ /* This allows for rare corner cases in which a JPEG image can actually be
591
+ larger than the uncompressed input (we wouldn't mention it if it hadn't
592
+ happened before.) */
593
+ retval = PAD(width, 16) * PAD(height, 16) * 6ULL + 2048ULL;
594
+ if (retval > (unsigned long long)((unsigned long)-1))
595
+ THROWG("TJBUFSIZE(): Image is too large");
596
+
597
+ bailout:
598
+ return (unsigned long)retval;
599
+ }
600
+
601
+
602
+ /* TurboJPEG 1.4+ */
603
+ DLLEXPORT unsigned long tjBufSizeYUV2(int width, int align, int height,
604
+ int subsamp)
605
+ {
606
+ unsigned long long retval = 0;
607
+ int nc, i;
608
+
609
+ if (align < 1 || !IS_POW2(align) || subsamp < 0 || subsamp >= TJ_NUMSAMP)
610
+ THROWG("tjBufSizeYUV2(): Invalid argument");
611
+
612
+ nc = (subsamp == TJSAMP_GRAY ? 1 : 3);
613
+ for (i = 0; i < nc; i++) {
614
+ int pw = tjPlaneWidth(i, width, subsamp);
615
+ int stride = PAD(pw, align);
616
+ int ph = tjPlaneHeight(i, height, subsamp);
617
+
618
+ if (pw < 0 || ph < 0) return -1;
619
+ else retval += (unsigned long long)stride * ph;
620
+ }
621
+ if (retval > (unsigned long long)((unsigned long)-1))
622
+ THROWG("tjBufSizeYUV2(): Image is too large");
623
+
624
+ bailout:
625
+ return (unsigned long)retval;
626
+ }
627
+
628
+ /* TurboJPEG 1.2+ */
629
+ DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp)
630
+ {
631
+ return tjBufSizeYUV2(width, 4, height, subsamp);
632
+ }
633
+
634
+ /* TurboJPEG 1.1+ */
635
+ DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int subsamp)
636
+ {
637
+ return tjBufSizeYUV(width, height, subsamp);
638
+ }
639
+
640
+
641
+ /* TurboJPEG 1.4+ */
642
+ DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp)
643
+ {
644
+ unsigned long long pw, retval = 0;
645
+ int nc;
646
+
647
+ if (width < 1 || subsamp < 0 || subsamp >= TJ_NUMSAMP)
648
+ THROWG("tjPlaneWidth(): Invalid argument");
649
+ nc = (subsamp == TJSAMP_GRAY ? 1 : 3);
650
+ if (componentID < 0 || componentID >= nc)
651
+ THROWG("tjPlaneWidth(): Invalid argument");
652
+
653
+ pw = PAD((unsigned long long)width, tjMCUWidth[subsamp] / 8);
654
+ if (componentID == 0)
655
+ retval = pw;
656
+ else
657
+ retval = pw * 8 / tjMCUWidth[subsamp];
658
+
659
+ if (retval > (unsigned long long)INT_MAX)
660
+ THROWG("tjPlaneWidth(): Width is too large");
661
+
662
+ bailout:
663
+ return (int)retval;
664
+ }
665
+
666
+
667
+ /* TurboJPEG 1.4+ */
668
+ DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp)
669
+ {
670
+ unsigned long long ph, retval = 0;
671
+ int nc;
672
+
673
+ if (height < 1 || subsamp < 0 || subsamp >= TJ_NUMSAMP)
674
+ THROWG("tjPlaneHeight(): Invalid argument");
675
+ nc = (subsamp == TJSAMP_GRAY ? 1 : 3);
676
+ if (componentID < 0 || componentID >= nc)
677
+ THROWG("tjPlaneHeight(): Invalid argument");
678
+
679
+ ph = PAD((unsigned long long)height, tjMCUHeight[subsamp] / 8);
680
+ if (componentID == 0)
681
+ retval = ph;
682
+ else
683
+ retval = ph * 8 / tjMCUHeight[subsamp];
684
+
685
+ if (retval > (unsigned long long)INT_MAX)
686
+ THROWG("tjPlaneHeight(): Height is too large");
687
+
688
+ bailout:
689
+ return (int)retval;
690
+ }
691
+
692
+
693
+ /* TurboJPEG 1.4+ */
694
+ DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
695
+ int height, int subsamp)
696
+ {
697
+ unsigned long long retval = 0;
698
+ int pw, ph;
699
+
700
+ if (width < 1 || height < 1 || subsamp < 0 || subsamp >= TJ_NUMSAMP)
701
+ THROWG("tjPlaneSizeYUV(): Invalid argument");
702
+
703
+ pw = tjPlaneWidth(componentID, width, subsamp);
704
+ ph = tjPlaneHeight(componentID, height, subsamp);
705
+ if (pw < 0 || ph < 0) return -1;
706
+
707
+ if (stride == 0) stride = pw;
708
+ else stride = abs(stride);
709
+
710
+ retval = (unsigned long long)stride * (ph - 1) + pw;
711
+ if (retval > (unsigned long long)((unsigned long)-1))
712
+ THROWG("tjPlaneSizeYUV(): Image is too large");
713
+
714
+ bailout:
715
+ return (unsigned long)retval;
716
+ }
717
+
718
+
719
+ /* TurboJPEG 1.2+ */
720
+ DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,
721
+ int width, int pitch, int height, int pixelFormat,
722
+ unsigned char **jpegBuf, unsigned long *jpegSize,
723
+ int jpegSubsamp, int jpegQual, int flags)
724
+ {
725
+ int i, retval = 0;
726
+ boolean alloc = TRUE;
727
+ JSAMPROW *row_pointer = NULL;
728
+
729
+ GET_CINSTANCE(handle)
730
+ this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
731
+ if ((this->init & COMPRESS) == 0)
732
+ THROW("tjCompress2(): Instance has not been initialized for compression");
733
+
734
+ if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
735
+ pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
736
+ jpegSize == NULL || jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP ||
737
+ jpegQual < 0 || jpegQual > 100)
738
+ THROW("tjCompress2(): Invalid argument");
739
+
740
+ if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
741
+
742
+ if ((row_pointer = (JSAMPROW *)malloc(sizeof(JSAMPROW) * height)) == NULL)
743
+ THROW("tjCompress2(): Memory allocation failure");
744
+
745
+ if (setjmp(this->jerr.setjmp_buffer)) {
746
+ /* If we get here, the JPEG code has signaled an error. */
747
+ retval = -1; goto bailout;
748
+ }
749
+
750
+ cinfo->image_width = width;
751
+ cinfo->image_height = height;
752
+
753
+ #ifndef NO_PUTENV
754
+ if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1");
755
+ else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1");
756
+ else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1");
757
+ #endif
758
+
759
+ if (flags & TJFLAG_NOREALLOC) {
760
+ alloc = FALSE; *jpegSize = tjBufSize(width, height, jpegSubsamp);
761
+ }
762
+ jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
763
+ setCompDefaults(cinfo, pixelFormat, jpegSubsamp, jpegQual, flags);
764
+
765
+ jpeg_start_compress(cinfo, TRUE);
766
+ for (i = 0; i < height; i++) {
767
+ if (flags & TJFLAG_BOTTOMUP)
768
+ row_pointer[i] = (JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
769
+ else
770
+ row_pointer[i] = (JSAMPROW)&srcBuf[i * (size_t)pitch];
771
+ }
772
+ while (cinfo->next_scanline < cinfo->image_height)
773
+ jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
774
+ cinfo->image_height - cinfo->next_scanline);
775
+ jpeg_finish_compress(cinfo);
776
+
777
+ bailout:
778
+ if (cinfo->global_state > CSTATE_START) {
779
+ if (alloc) (*cinfo->dest->term_destination) (cinfo);
780
+ jpeg_abort_compress(cinfo);
781
+ }
782
+ free(row_pointer);
783
+ if (this->jerr.warning) retval = -1;
784
+ this->jerr.stopOnWarning = FALSE;
785
+ return retval;
786
+ }
787
+
788
+ /* TurboJPEG 1.0+ */
789
+ DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
790
+ int pitch, int height, int pixelSize,
791
+ unsigned char *jpegBuf, unsigned long *jpegSize,
792
+ int jpegSubsamp, int jpegQual, int flags)
793
+ {
794
+ int retval = 0;
795
+ unsigned long size;
796
+
797
+ if (flags & TJ_YUV) {
798
+ size = tjBufSizeYUV(width, height, jpegSubsamp);
799
+ retval = tjEncodeYUV2(handle, srcBuf, width, pitch, height,
800
+ getPixelFormat(pixelSize, flags), jpegBuf,
801
+ jpegSubsamp, flags);
802
+ } else {
803
+ retval = tjCompress2(handle, srcBuf, width, pitch, height,
804
+ getPixelFormat(pixelSize, flags), &jpegBuf, &size,
805
+ jpegSubsamp, jpegQual, flags | TJFLAG_NOREALLOC);
806
+ }
807
+ *jpegSize = size;
808
+ return retval;
809
+ }
810
+
811
+
812
+ /* TurboJPEG 1.4+ */
813
+ DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,
814
+ int width, int pitch, int height,
815
+ int pixelFormat, unsigned char **dstPlanes,
816
+ int *strides, int subsamp, int flags)
817
+ {
818
+ JSAMPROW *row_pointer = NULL;
819
+ JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS];
820
+ JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS];
821
+ JSAMPROW *outbuf[MAX_COMPONENTS];
822
+ int i, retval = 0, row, pw0, ph0, pw[MAX_COMPONENTS], ph[MAX_COMPONENTS];
823
+ JSAMPLE *ptr;
824
+ jpeg_component_info *compptr;
825
+
826
+ GET_CINSTANCE(handle);
827
+ this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
828
+
829
+ for (i = 0; i < MAX_COMPONENTS; i++) {
830
+ tmpbuf[i] = NULL; _tmpbuf[i] = NULL;
831
+ tmpbuf2[i] = NULL; _tmpbuf2[i] = NULL; outbuf[i] = NULL;
832
+ }
833
+
834
+ if ((this->init & COMPRESS) == 0)
835
+ THROW("tjEncodeYUVPlanes(): Instance has not been initialized for compression");
836
+
837
+ if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
838
+ pixelFormat < 0 || pixelFormat >= TJ_NUMPF || !dstPlanes ||
839
+ !dstPlanes[0] || subsamp < 0 || subsamp >= TJ_NUMSAMP)
840
+ THROW("tjEncodeYUVPlanes(): Invalid argument");
841
+ if (subsamp != TJSAMP_GRAY && (!dstPlanes[1] || !dstPlanes[2]))
842
+ THROW("tjEncodeYUVPlanes(): Invalid argument");
843
+
844
+ if (pixelFormat == TJPF_CMYK)
845
+ THROW("tjEncodeYUVPlanes(): Cannot generate YUV images from packed-pixel CMYK images");
846
+
847
+ if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
848
+
849
+ if (setjmp(this->jerr.setjmp_buffer)) {
850
+ /* If we get here, the JPEG code has signaled an error. */
851
+ retval = -1; goto bailout;
852
+ }
853
+
854
+ cinfo->image_width = width;
855
+ cinfo->image_height = height;
856
+
857
+ #ifndef NO_PUTENV
858
+ if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1");
859
+ else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1");
860
+ else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1");
861
+ #endif
862
+
863
+ setCompDefaults(cinfo, pixelFormat, subsamp, -1, flags);
864
+
865
+ /* Execute only the parts of jpeg_start_compress() that we need. If we
866
+ were to call the whole jpeg_start_compress() function, then it would try
867
+ to write the file headers, which could overflow the output buffer if the
868
+ YUV image were very small. */
869
+ if (cinfo->global_state != CSTATE_START)
870
+ THROW("tjEncodeYUVPlanes(): libjpeg API is in the wrong state");
871
+ (*cinfo->err->reset_error_mgr) ((j_common_ptr)cinfo);
872
+ jinit_c_master_control(cinfo, FALSE);
873
+ jinit_color_converter(cinfo);
874
+ jinit_downsampler(cinfo);
875
+ (*cinfo->cconvert->start_pass) (cinfo);
876
+
877
+ pw0 = PAD(width, cinfo->max_h_samp_factor);
878
+ ph0 = PAD(height, cinfo->max_v_samp_factor);
879
+
880
+ if ((row_pointer = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph0)) == NULL)
881
+ THROW("tjEncodeYUVPlanes(): Memory allocation failure");
882
+ for (i = 0; i < height; i++) {
883
+ if (flags & TJFLAG_BOTTOMUP)
884
+ row_pointer[i] = (JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
885
+ else
886
+ row_pointer[i] = (JSAMPROW)&srcBuf[i * (size_t)pitch];
887
+ }
888
+ if (height < ph0)
889
+ for (i = height; i < ph0; i++) row_pointer[i] = row_pointer[height - 1];
890
+
891
+ for (i = 0; i < cinfo->num_components; i++) {
892
+ compptr = &cinfo->comp_info[i];
893
+ _tmpbuf[i] = (JSAMPLE *)malloc(
894
+ PAD((compptr->width_in_blocks * cinfo->max_h_samp_factor * DCTSIZE) /
895
+ compptr->h_samp_factor, 32) *
896
+ cinfo->max_v_samp_factor + 32);
897
+ if (!_tmpbuf[i])
898
+ THROW("tjEncodeYUVPlanes(): Memory allocation failure");
899
+ tmpbuf[i] =
900
+ (JSAMPROW *)malloc(sizeof(JSAMPROW) * cinfo->max_v_samp_factor);
901
+ if (!tmpbuf[i])
902
+ THROW("tjEncodeYUVPlanes(): Memory allocation failure");
903
+ for (row = 0; row < cinfo->max_v_samp_factor; row++) {
904
+ unsigned char *_tmpbuf_aligned =
905
+ (unsigned char *)PAD((JUINTPTR)_tmpbuf[i], 32);
906
+
907
+ tmpbuf[i][row] = &_tmpbuf_aligned[
908
+ PAD((compptr->width_in_blocks * cinfo->max_h_samp_factor * DCTSIZE) /
909
+ compptr->h_samp_factor, 32) * row];
910
+ }
911
+ _tmpbuf2[i] =
912
+ (JSAMPLE *)malloc(PAD(compptr->width_in_blocks * DCTSIZE, 32) *
913
+ compptr->v_samp_factor + 32);
914
+ if (!_tmpbuf2[i])
915
+ THROW("tjEncodeYUVPlanes(): Memory allocation failure");
916
+ tmpbuf2[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * compptr->v_samp_factor);
917
+ if (!tmpbuf2[i])
918
+ THROW("tjEncodeYUVPlanes(): Memory allocation failure");
919
+ for (row = 0; row < compptr->v_samp_factor; row++) {
920
+ unsigned char *_tmpbuf2_aligned =
921
+ (unsigned char *)PAD((JUINTPTR)_tmpbuf2[i], 32);
922
+
923
+ tmpbuf2[i][row] =
924
+ &_tmpbuf2_aligned[PAD(compptr->width_in_blocks * DCTSIZE, 32) * row];
925
+ }
926
+ pw[i] = pw0 * compptr->h_samp_factor / cinfo->max_h_samp_factor;
927
+ ph[i] = ph0 * compptr->v_samp_factor / cinfo->max_v_samp_factor;
928
+ outbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i]);
929
+ if (!outbuf[i])
930
+ THROW("tjEncodeYUVPlanes(): Memory allocation failure");
931
+ ptr = dstPlanes[i];
932
+ for (row = 0; row < ph[i]; row++) {
933
+ outbuf[i][row] = ptr;
934
+ ptr += (strides && strides[i] != 0) ? strides[i] : pw[i];
935
+ }
936
+ }
937
+
938
+ if (setjmp(this->jerr.setjmp_buffer)) {
939
+ /* If we get here, the JPEG code has signaled an error. */
940
+ retval = -1; goto bailout;
941
+ }
942
+
943
+ for (row = 0; row < ph0; row += cinfo->max_v_samp_factor) {
944
+ (*cinfo->cconvert->color_convert) (cinfo, &row_pointer[row], tmpbuf, 0,
945
+ cinfo->max_v_samp_factor);
946
+ (cinfo->downsample->downsample) (cinfo, tmpbuf, 0, tmpbuf2, 0);
947
+ for (i = 0, compptr = cinfo->comp_info; i < cinfo->num_components;
948
+ i++, compptr++)
949
+ jcopy_sample_rows(tmpbuf2[i], 0, outbuf[i],
950
+ row * compptr->v_samp_factor / cinfo->max_v_samp_factor,
951
+ compptr->v_samp_factor, pw[i]);
952
+ }
953
+ cinfo->next_scanline += height;
954
+ jpeg_abort_compress(cinfo);
955
+
956
+ bailout:
957
+ if (cinfo->global_state > CSTATE_START) jpeg_abort_compress(cinfo);
958
+ free(row_pointer);
959
+ for (i = 0; i < MAX_COMPONENTS; i++) {
960
+ free(tmpbuf[i]);
961
+ free(_tmpbuf[i]);
962
+ free(tmpbuf2[i]);
963
+ free(_tmpbuf2[i]);
964
+ free(outbuf[i]);
965
+ }
966
+ if (this->jerr.warning) retval = -1;
967
+ this->jerr.stopOnWarning = FALSE;
968
+ return retval;
969
+ }
970
+
971
+ /* TurboJPEG 1.4+ */
972
+ DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf,
973
+ int width, int pitch, int height, int pixelFormat,
974
+ unsigned char *dstBuf, int align, int subsamp,
975
+ int flags)
976
+ {
977
+ unsigned char *dstPlanes[3];
978
+ int pw0, ph0, strides[3], retval = -1;
979
+ tjinstance *this = (tjinstance *)handle;
980
+
981
+ if (!this) THROWG("tjEncodeYUV3(): Invalid handle");
982
+ this->isInstanceError = FALSE;
983
+
984
+ if (width <= 0 || height <= 0 || dstBuf == NULL || align < 1 ||
985
+ !IS_POW2(align) || subsamp < 0 || subsamp >= TJ_NUMSAMP)
986
+ THROW("tjEncodeYUV3(): Invalid argument");
987
+
988
+ pw0 = tjPlaneWidth(0, width, subsamp);
989
+ ph0 = tjPlaneHeight(0, height, subsamp);
990
+ dstPlanes[0] = dstBuf;
991
+ strides[0] = PAD(pw0, align);
992
+ if (subsamp == TJSAMP_GRAY) {
993
+ strides[1] = strides[2] = 0;
994
+ dstPlanes[1] = dstPlanes[2] = NULL;
995
+ } else {
996
+ int pw1 = tjPlaneWidth(1, width, subsamp);
997
+ int ph1 = tjPlaneHeight(1, height, subsamp);
998
+
999
+ strides[1] = strides[2] = PAD(pw1, align);
1000
+ dstPlanes[1] = dstPlanes[0] + strides[0] * ph0;
1001
+ dstPlanes[2] = dstPlanes[1] + strides[1] * ph1;
1002
+ }
1003
+
1004
+ return tjEncodeYUVPlanes(handle, srcBuf, width, pitch, height, pixelFormat,
1005
+ dstPlanes, strides, subsamp, flags);
1006
+
1007
+ bailout:
1008
+ return retval;
1009
+ }
1010
+
1011
+ /* TurboJPEG 1.2+ */
1012
+ DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
1013
+ int pitch, int height, int pixelFormat,
1014
+ unsigned char *dstBuf, int subsamp, int flags)
1015
+ {
1016
+ return tjEncodeYUV3(handle, srcBuf, width, pitch, height, pixelFormat,
1017
+ dstBuf, 4, subsamp, flags);
1018
+ }
1019
+
1020
+ /* TurboJPEG 1.1+ */
1021
+ DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
1022
+ int pitch, int height, int pixelSize,
1023
+ unsigned char *dstBuf, int subsamp, int flags)
1024
+ {
1025
+ return tjEncodeYUV2(handle, srcBuf, width, pitch, height,
1026
+ getPixelFormat(pixelSize, flags), dstBuf, subsamp,
1027
+ flags);
1028
+ }
1029
+
1030
+
1031
+ /* TurboJPEG 1.4+ */
1032
+ DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
1033
+ const unsigned char **srcPlanes,
1034
+ int width, const int *strides,
1035
+ int height, int subsamp,
1036
+ unsigned char **jpegBuf,
1037
+ unsigned long *jpegSize, int jpegQual,
1038
+ int flags)
1039
+ {
1040
+ int i, row, retval = 0;
1041
+ boolean alloc = TRUE;
1042
+ int pw[MAX_COMPONENTS], ph[MAX_COMPONENTS], iw[MAX_COMPONENTS],
1043
+ tmpbufsize = 0, usetmpbuf = 0, th[MAX_COMPONENTS];
1044
+ JSAMPLE *_tmpbuf = NULL, *ptr;
1045
+ JSAMPROW *inbuf[MAX_COMPONENTS], *tmpbuf[MAX_COMPONENTS];
1046
+
1047
+ GET_CINSTANCE(handle)
1048
+ this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
1049
+
1050
+ for (i = 0; i < MAX_COMPONENTS; i++) {
1051
+ tmpbuf[i] = NULL; inbuf[i] = NULL;
1052
+ }
1053
+
1054
+ if ((this->init & COMPRESS) == 0)
1055
+ THROW("tjCompressFromYUVPlanes(): Instance has not been initialized for compression");
1056
+
1057
+ if (!srcPlanes || !srcPlanes[0] || width <= 0 || height <= 0 ||
1058
+ subsamp < 0 || subsamp >= TJ_NUMSAMP || jpegBuf == NULL ||
1059
+ jpegSize == NULL || jpegQual < 0 || jpegQual > 100)
1060
+ THROW("tjCompressFromYUVPlanes(): Invalid argument");
1061
+ if (subsamp != TJSAMP_GRAY && (!srcPlanes[1] || !srcPlanes[2]))
1062
+ THROW("tjCompressFromYUVPlanes(): Invalid argument");
1063
+
1064
+ if (setjmp(this->jerr.setjmp_buffer)) {
1065
+ /* If we get here, the JPEG code has signaled an error. */
1066
+ retval = -1; goto bailout;
1067
+ }
1068
+
1069
+ cinfo->image_width = width;
1070
+ cinfo->image_height = height;
1071
+
1072
+ #ifndef NO_PUTENV
1073
+ if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1");
1074
+ else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1");
1075
+ else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1");
1076
+ #endif
1077
+
1078
+ if (flags & TJFLAG_NOREALLOC) {
1079
+ alloc = FALSE; *jpegSize = tjBufSize(width, height, subsamp);
1080
+ }
1081
+ jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
1082
+ setCompDefaults(cinfo, TJPF_RGB, subsamp, jpegQual, flags);
1083
+ cinfo->raw_data_in = TRUE;
1084
+
1085
+ jpeg_start_compress(cinfo, TRUE);
1086
+ for (i = 0; i < cinfo->num_components; i++) {
1087
+ jpeg_component_info *compptr = &cinfo->comp_info[i];
1088
+ int ih;
1089
+
1090
+ iw[i] = compptr->width_in_blocks * DCTSIZE;
1091
+ ih = compptr->height_in_blocks * DCTSIZE;
1092
+ pw[i] = PAD(cinfo->image_width, cinfo->max_h_samp_factor) *
1093
+ compptr->h_samp_factor / cinfo->max_h_samp_factor;
1094
+ ph[i] = PAD(cinfo->image_height, cinfo->max_v_samp_factor) *
1095
+ compptr->v_samp_factor / cinfo->max_v_samp_factor;
1096
+ if (iw[i] != pw[i] || ih != ph[i]) usetmpbuf = 1;
1097
+ th[i] = compptr->v_samp_factor * DCTSIZE;
1098
+ tmpbufsize += iw[i] * th[i];
1099
+ if ((inbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i])) == NULL)
1100
+ THROW("tjCompressFromYUVPlanes(): Memory allocation failure");
1101
+ ptr = (JSAMPLE *)srcPlanes[i];
1102
+ for (row = 0; row < ph[i]; row++) {
1103
+ inbuf[i][row] = ptr;
1104
+ ptr += (strides && strides[i] != 0) ? strides[i] : pw[i];
1105
+ }
1106
+ }
1107
+ if (usetmpbuf) {
1108
+ if ((_tmpbuf = (JSAMPLE *)malloc(sizeof(JSAMPLE) * tmpbufsize)) == NULL)
1109
+ THROW("tjCompressFromYUVPlanes(): Memory allocation failure");
1110
+ ptr = _tmpbuf;
1111
+ for (i = 0; i < cinfo->num_components; i++) {
1112
+ if ((tmpbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * th[i])) == NULL)
1113
+ THROW("tjCompressFromYUVPlanes(): Memory allocation failure");
1114
+ for (row = 0; row < th[i]; row++) {
1115
+ tmpbuf[i][row] = ptr;
1116
+ ptr += iw[i];
1117
+ }
1118
+ }
1119
+ }
1120
+
1121
+ if (setjmp(this->jerr.setjmp_buffer)) {
1122
+ /* If we get here, the JPEG code has signaled an error. */
1123
+ retval = -1; goto bailout;
1124
+ }
1125
+
1126
+ for (row = 0; row < (int)cinfo->image_height;
1127
+ row += cinfo->max_v_samp_factor * DCTSIZE) {
1128
+ JSAMPARRAY yuvptr[MAX_COMPONENTS];
1129
+ int crow[MAX_COMPONENTS];
1130
+
1131
+ for (i = 0; i < cinfo->num_components; i++) {
1132
+ jpeg_component_info *compptr = &cinfo->comp_info[i];
1133
+
1134
+ crow[i] = row * compptr->v_samp_factor / cinfo->max_v_samp_factor;
1135
+ if (usetmpbuf) {
1136
+ int j, k;
1137
+
1138
+ for (j = 0; j < MIN(th[i], ph[i] - crow[i]); j++) {
1139
+ memcpy(tmpbuf[i][j], inbuf[i][crow[i] + j], pw[i]);
1140
+ /* Duplicate last sample in row to fill out MCU */
1141
+ for (k = pw[i]; k < iw[i]; k++)
1142
+ tmpbuf[i][j][k] = tmpbuf[i][j][pw[i] - 1];
1143
+ }
1144
+ /* Duplicate last row to fill out MCU */
1145
+ for (j = ph[i] - crow[i]; j < th[i]; j++)
1146
+ memcpy(tmpbuf[i][j], tmpbuf[i][ph[i] - crow[i] - 1], iw[i]);
1147
+ yuvptr[i] = tmpbuf[i];
1148
+ } else
1149
+ yuvptr[i] = &inbuf[i][crow[i]];
1150
+ }
1151
+ jpeg_write_raw_data(cinfo, yuvptr, cinfo->max_v_samp_factor * DCTSIZE);
1152
+ }
1153
+ jpeg_finish_compress(cinfo);
1154
+
1155
+ bailout:
1156
+ if (cinfo->global_state > CSTATE_START) {
1157
+ if (alloc) (*cinfo->dest->term_destination) (cinfo);
1158
+ jpeg_abort_compress(cinfo);
1159
+ }
1160
+ for (i = 0; i < MAX_COMPONENTS; i++) {
1161
+ free(tmpbuf[i]);
1162
+ free(inbuf[i]);
1163
+ }
1164
+ free(_tmpbuf);
1165
+ if (this->jerr.warning) retval = -1;
1166
+ this->jerr.stopOnWarning = FALSE;
1167
+ return retval;
1168
+ }
1169
+
1170
+ /* TurboJPEG 1.4+ */
1171
+ DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf,
1172
+ int width, int align, int height, int subsamp,
1173
+ unsigned char **jpegBuf,
1174
+ unsigned long *jpegSize, int jpegQual,
1175
+ int flags)
1176
+ {
1177
+ const unsigned char *srcPlanes[3];
1178
+ int pw0, ph0, strides[3], retval = -1;
1179
+ tjinstance *this = (tjinstance *)handle;
1180
+
1181
+ if (!this) THROWG("tjCompressFromYUV(): Invalid handle");
1182
+ this->isInstanceError = FALSE;
1183
+
1184
+ if (srcBuf == NULL || width <= 0 || align < 1 || !IS_POW2(align) ||
1185
+ height <= 0 || subsamp < 0 || subsamp >= TJ_NUMSAMP)
1186
+ THROW("tjCompressFromYUV(): Invalid argument");
1187
+
1188
+ pw0 = tjPlaneWidth(0, width, subsamp);
1189
+ ph0 = tjPlaneHeight(0, height, subsamp);
1190
+ srcPlanes[0] = srcBuf;
1191
+ strides[0] = PAD(pw0, align);
1192
+ if (subsamp == TJSAMP_GRAY) {
1193
+ strides[1] = strides[2] = 0;
1194
+ srcPlanes[1] = srcPlanes[2] = NULL;
1195
+ } else {
1196
+ int pw1 = tjPlaneWidth(1, width, subsamp);
1197
+ int ph1 = tjPlaneHeight(1, height, subsamp);
1198
+
1199
+ strides[1] = strides[2] = PAD(pw1, align);
1200
+ srcPlanes[1] = srcPlanes[0] + strides[0] * ph0;
1201
+ srcPlanes[2] = srcPlanes[1] + strides[1] * ph1;
1202
+ }
1203
+
1204
+ return tjCompressFromYUVPlanes(handle, srcPlanes, width, strides, height,
1205
+ subsamp, jpegBuf, jpegSize, jpegQual, flags);
1206
+
1207
+ bailout:
1208
+ return retval;
1209
+ }
1210
+
1211
+
1212
+ /******************************* Decompressor ********************************/
1213
+
1214
+ static tjhandle _tjInitDecompress(tjinstance *this)
1215
+ {
1216
+ static unsigned char buffer[1];
1217
+
1218
+ /* This is also straight out of example.txt */
1219
+ this->dinfo.err = jpeg_std_error(&this->jerr.pub);
1220
+ this->jerr.pub.error_exit = my_error_exit;
1221
+ this->jerr.pub.output_message = my_output_message;
1222
+ this->jerr.emit_message = this->jerr.pub.emit_message;
1223
+ this->jerr.pub.emit_message = my_emit_message;
1224
+ this->jerr.pub.addon_message_table = turbojpeg_message_table;
1225
+ this->jerr.pub.first_addon_message = JMSG_FIRSTADDONCODE;
1226
+ this->jerr.pub.last_addon_message = JMSG_LASTADDONCODE;
1227
+
1228
+ if (setjmp(this->jerr.setjmp_buffer)) {
1229
+ /* If we get here, the JPEG code has signaled an error. */
1230
+ free(this);
1231
+ return NULL;
1232
+ }
1233
+
1234
+ jpeg_create_decompress(&this->dinfo);
1235
+ /* Make an initial call so it will create the source manager */
1236
+ jpeg_mem_src_tj(&this->dinfo, buffer, 1);
1237
+
1238
+ this->init |= DECOMPRESS;
1239
+ return (tjhandle)this;
1240
+ }
1241
+
1242
+ /* TurboJPEG 1.0+ */
1243
+ DLLEXPORT tjhandle tjInitDecompress(void)
1244
+ {
1245
+ tjinstance *this;
1246
+
1247
+ if ((this = (tjinstance *)malloc(sizeof(tjinstance))) == NULL) {
1248
+ SNPRINTF(errStr, JMSG_LENGTH_MAX,
1249
+ "tjInitDecompress(): Memory allocation failure");
1250
+ return NULL;
1251
+ }
1252
+ memset(this, 0, sizeof(tjinstance));
1253
+ SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "No error");
1254
+ return _tjInitDecompress(this);
1255
+ }
1256
+
1257
+
1258
+ /* TurboJPEG 1.4+ */
1259
+ DLLEXPORT int tjDecompressHeader3(tjhandle handle,
1260
+ const unsigned char *jpegBuf,
1261
+ unsigned long jpegSize, int *width,
1262
+ int *height, int *jpegSubsamp,
1263
+ int *jpegColorspace)
1264
+ {
1265
+ int retval = 0;
1266
+
1267
+ GET_DINSTANCE(handle);
1268
+ if ((this->init & DECOMPRESS) == 0)
1269
+ THROW("tjDecompressHeader3(): Instance has not been initialized for decompression");
1270
+
1271
+ if (jpegBuf == NULL || jpegSize <= 0 || width == NULL || height == NULL ||
1272
+ jpegSubsamp == NULL || jpegColorspace == NULL)
1273
+ THROW("tjDecompressHeader3(): Invalid argument");
1274
+
1275
+ if (setjmp(this->jerr.setjmp_buffer)) {
1276
+ /* If we get here, the JPEG code has signaled an error. */
1277
+ return -1;
1278
+ }
1279
+
1280
+ jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
1281
+
1282
+ /* jpeg_read_header() calls jpeg_abort() and returns JPEG_HEADER_TABLES_ONLY
1283
+ if the datastream is a tables-only datastream. Since we aren't using a
1284
+ suspending data source, the only other value it can return is
1285
+ JPEG_HEADER_OK. */
1286
+ if (jpeg_read_header(dinfo, FALSE) == JPEG_HEADER_TABLES_ONLY)
1287
+ return 0;
1288
+
1289
+ *width = dinfo->image_width;
1290
+ *height = dinfo->image_height;
1291
+ *jpegSubsamp = getSubsamp(dinfo);
1292
+ switch (dinfo->jpeg_color_space) {
1293
+ case JCS_GRAYSCALE: *jpegColorspace = TJCS_GRAY; break;
1294
+ case JCS_RGB: *jpegColorspace = TJCS_RGB; break;
1295
+ case JCS_YCbCr: *jpegColorspace = TJCS_YCbCr; break;
1296
+ case JCS_CMYK: *jpegColorspace = TJCS_CMYK; break;
1297
+ case JCS_YCCK: *jpegColorspace = TJCS_YCCK; break;
1298
+ default: *jpegColorspace = -1; break;
1299
+ }
1300
+
1301
+ jpeg_abort_decompress(dinfo);
1302
+
1303
+ if (*jpegSubsamp < 0)
1304
+ THROW("tjDecompressHeader3(): Could not determine subsampling type for JPEG image");
1305
+ if (*jpegColorspace < 0)
1306
+ THROW("tjDecompressHeader3(): Could not determine colorspace of JPEG image");
1307
+ if (*width < 1 || *height < 1)
1308
+ THROW("tjDecompressHeader3(): Invalid data returned in header");
1309
+
1310
+ bailout:
1311
+ if (this->jerr.warning) retval = -1;
1312
+ return retval;
1313
+ }
1314
+
1315
+ /* TurboJPEG 1.1+ */
1316
+ DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
1317
+ unsigned long jpegSize, int *width,
1318
+ int *height, int *jpegSubsamp)
1319
+ {
1320
+ int jpegColorspace;
1321
+
1322
+ return tjDecompressHeader3(handle, jpegBuf, jpegSize, width, height,
1323
+ jpegSubsamp, &jpegColorspace);
1324
+ }
1325
+
1326
+ /* TurboJPEG 1.0+ */
1327
+ DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
1328
+ unsigned long jpegSize, int *width,
1329
+ int *height)
1330
+ {
1331
+ int jpegSubsamp;
1332
+
1333
+ return tjDecompressHeader2(handle, jpegBuf, jpegSize, width, height,
1334
+ &jpegSubsamp);
1335
+ }
1336
+
1337
+
1338
+ /* TurboJPEG 1.2+ */
1339
+ DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numScalingFactors)
1340
+ {
1341
+ if (numScalingFactors == NULL) {
1342
+ SNPRINTF(errStr, JMSG_LENGTH_MAX,
1343
+ "tjGetScalingFactors(): Invalid argument");
1344
+ return NULL;
1345
+ }
1346
+
1347
+ *numScalingFactors = NUMSF;
1348
+ return (tjscalingfactor *)sf;
1349
+ }
1350
+
1351
+
1352
+ /* TurboJPEG 1.2+ */
1353
+ DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
1354
+ unsigned long jpegSize, unsigned char *dstBuf,
1355
+ int width, int pitch, int height, int pixelFormat,
1356
+ int flags)
1357
+ {
1358
+ JSAMPROW *row_pointer = NULL;
1359
+ int i, retval = 0, jpegwidth, jpegheight, scaledw, scaledh;
1360
+ struct my_progress_mgr progress;
1361
+
1362
+ GET_DINSTANCE(handle);
1363
+ this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
1364
+ if ((this->init & DECOMPRESS) == 0)
1365
+ THROW("tjDecompress2(): Instance has not been initialized for decompression");
1366
+
1367
+ if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || width < 0 ||
1368
+ pitch < 0 || height < 0 || pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
1369
+ THROW("tjDecompress2(): Invalid argument");
1370
+
1371
+ #ifndef NO_PUTENV
1372
+ if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1");
1373
+ else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1");
1374
+ else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1");
1375
+ #endif
1376
+
1377
+ if (flags & TJFLAG_LIMITSCANS) {
1378
+ memset(&progress, 0, sizeof(struct my_progress_mgr));
1379
+ progress.pub.progress_monitor = my_progress_monitor;
1380
+ progress.this = this;
1381
+ dinfo->progress = &progress.pub;
1382
+ } else
1383
+ dinfo->progress = NULL;
1384
+
1385
+ if (setjmp(this->jerr.setjmp_buffer)) {
1386
+ /* If we get here, the JPEG code has signaled an error. */
1387
+ retval = -1; goto bailout;
1388
+ }
1389
+
1390
+ jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
1391
+ jpeg_read_header(dinfo, TRUE);
1392
+ this->dinfo.out_color_space = pf2cs[pixelFormat];
1393
+ if (flags & TJFLAG_FASTDCT) this->dinfo.dct_method = JDCT_FASTEST;
1394
+ if (flags & TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling = FALSE;
1395
+
1396
+ jpegwidth = dinfo->image_width; jpegheight = dinfo->image_height;
1397
+ if (width == 0) width = jpegwidth;
1398
+ if (height == 0) height = jpegheight;
1399
+ for (i = 0; i < NUMSF; i++) {
1400
+ scaledw = TJSCALED(jpegwidth, sf[i]);
1401
+ scaledh = TJSCALED(jpegheight, sf[i]);
1402
+ if (scaledw <= width && scaledh <= height)
1403
+ break;
1404
+ }
1405
+ if (i >= NUMSF)
1406
+ THROW("tjDecompress2(): Could not scale down to desired image dimensions");
1407
+ width = scaledw; height = scaledh;
1408
+ dinfo->scale_num = sf[i].num;
1409
+ dinfo->scale_denom = sf[i].denom;
1410
+
1411
+ jpeg_start_decompress(dinfo);
1412
+ if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
1413
+
1414
+ if ((row_pointer =
1415
+ (JSAMPROW *)malloc(sizeof(JSAMPROW) * dinfo->output_height)) == NULL)
1416
+ THROW("tjDecompress2(): Memory allocation failure");
1417
+ if (setjmp(this->jerr.setjmp_buffer)) {
1418
+ /* If we get here, the JPEG code has signaled an error. */
1419
+ retval = -1; goto bailout;
1420
+ }
1421
+ for (i = 0; i < (int)dinfo->output_height; i++) {
1422
+ if (flags & TJFLAG_BOTTOMUP)
1423
+ row_pointer[i] = &dstBuf[(dinfo->output_height - i - 1) * (size_t)pitch];
1424
+ else
1425
+ row_pointer[i] = &dstBuf[i * (size_t)pitch];
1426
+ }
1427
+ while (dinfo->output_scanline < dinfo->output_height)
1428
+ jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
1429
+ dinfo->output_height - dinfo->output_scanline);
1430
+ jpeg_finish_decompress(dinfo);
1431
+
1432
+ bailout:
1433
+ if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
1434
+ free(row_pointer);
1435
+ if (this->jerr.warning) retval = -1;
1436
+ this->jerr.stopOnWarning = FALSE;
1437
+ return retval;
1438
+ }
1439
+
1440
+ /* TurboJPEG 1.0+ */
1441
+ DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
1442
+ unsigned long jpegSize, unsigned char *dstBuf,
1443
+ int width, int pitch, int height, int pixelSize,
1444
+ int flags)
1445
+ {
1446
+ if (flags & TJ_YUV)
1447
+ return tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags);
1448
+ else
1449
+ return tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, width, pitch,
1450
+ height, getPixelFormat(pixelSize, flags), flags);
1451
+ }
1452
+
1453
+
1454
+ static void setDecodeDefaults(struct jpeg_decompress_struct *dinfo,
1455
+ int pixelFormat, int subsamp, int flags)
1456
+ {
1457
+ int i;
1458
+
1459
+ dinfo->scale_num = dinfo->scale_denom = 1;
1460
+
1461
+ if (subsamp == TJSAMP_GRAY) {
1462
+ dinfo->num_components = dinfo->comps_in_scan = 1;
1463
+ dinfo->jpeg_color_space = JCS_GRAYSCALE;
1464
+ } else {
1465
+ dinfo->num_components = dinfo->comps_in_scan = 3;
1466
+ dinfo->jpeg_color_space = JCS_YCbCr;
1467
+ }
1468
+
1469
+ dinfo->comp_info = (jpeg_component_info *)
1470
+ (*dinfo->mem->alloc_small) ((j_common_ptr)dinfo, JPOOL_IMAGE,
1471
+ dinfo->num_components *
1472
+ sizeof(jpeg_component_info));
1473
+
1474
+ for (i = 0; i < dinfo->num_components; i++) {
1475
+ jpeg_component_info *compptr = &dinfo->comp_info[i];
1476
+
1477
+ compptr->h_samp_factor = (i == 0) ? tjMCUWidth[subsamp] / 8 : 1;
1478
+ compptr->v_samp_factor = (i == 0) ? tjMCUHeight[subsamp] / 8 : 1;
1479
+ compptr->component_index = i;
1480
+ compptr->component_id = i + 1;
1481
+ compptr->quant_tbl_no = compptr->dc_tbl_no =
1482
+ compptr->ac_tbl_no = (i == 0) ? 0 : 1;
1483
+ dinfo->cur_comp_info[i] = compptr;
1484
+ }
1485
+ dinfo->data_precision = 8;
1486
+ for (i = 0; i < 2; i++) {
1487
+ if (dinfo->quant_tbl_ptrs[i] == NULL)
1488
+ dinfo->quant_tbl_ptrs[i] = jpeg_alloc_quant_table((j_common_ptr)dinfo);
1489
+ }
1490
+ }
1491
+
1492
+
1493
+ static int my_read_markers(j_decompress_ptr dinfo)
1494
+ {
1495
+ return JPEG_REACHED_SOS;
1496
+ }
1497
+
1498
+ static void my_reset_marker_reader(j_decompress_ptr dinfo)
1499
+ {
1500
+ }
1501
+
1502
+ /* TurboJPEG 1.4+ */
1503
+ DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
1504
+ const unsigned char **srcPlanes,
1505
+ const int *strides, int subsamp,
1506
+ unsigned char *dstBuf, int width, int pitch,
1507
+ int height, int pixelFormat, int flags)
1508
+ {
1509
+ JSAMPROW *row_pointer = NULL;
1510
+ JSAMPLE *_tmpbuf[MAX_COMPONENTS];
1511
+ JSAMPROW *tmpbuf[MAX_COMPONENTS], *inbuf[MAX_COMPONENTS];
1512
+ int i, retval = 0, row, pw0, ph0, pw[MAX_COMPONENTS], ph[MAX_COMPONENTS];
1513
+ JSAMPLE *ptr;
1514
+ jpeg_component_info *compptr;
1515
+ int (*old_read_markers) (j_decompress_ptr);
1516
+ void (*old_reset_marker_reader) (j_decompress_ptr);
1517
+
1518
+ GET_DINSTANCE(handle);
1519
+ this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
1520
+
1521
+ for (i = 0; i < MAX_COMPONENTS; i++) {
1522
+ tmpbuf[i] = NULL; _tmpbuf[i] = NULL; inbuf[i] = NULL;
1523
+ }
1524
+
1525
+ if ((this->init & DECOMPRESS) == 0)
1526
+ THROW("tjDecodeYUVPlanes(): Instance has not been initialized for decompression");
1527
+
1528
+ if (!srcPlanes || !srcPlanes[0] || subsamp < 0 || subsamp >= TJ_NUMSAMP ||
1529
+ dstBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
1530
+ pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
1531
+ THROW("tjDecodeYUVPlanes(): Invalid argument");
1532
+ if (subsamp != TJSAMP_GRAY && (!srcPlanes[1] || !srcPlanes[2]))
1533
+ THROW("tjDecodeYUVPlanes(): Invalid argument");
1534
+
1535
+ if (setjmp(this->jerr.setjmp_buffer)) {
1536
+ /* If we get here, the JPEG code has signaled an error. */
1537
+ retval = -1; goto bailout;
1538
+ }
1539
+
1540
+ if (pixelFormat == TJPF_CMYK)
1541
+ THROW("tjDecodeYUVPlanes(): Cannot decode YUV images into packed-pixel CMYK images.");
1542
+
1543
+ if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
1544
+ dinfo->image_width = width;
1545
+ dinfo->image_height = height;
1546
+
1547
+ #ifndef NO_PUTENV
1548
+ if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1");
1549
+ else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1");
1550
+ else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1");
1551
+ #endif
1552
+
1553
+ dinfo->progressive_mode = dinfo->inputctl->has_multiple_scans = FALSE;
1554
+ dinfo->Ss = dinfo->Ah = dinfo->Al = 0;
1555
+ dinfo->Se = DCTSIZE2 - 1;
1556
+ setDecodeDefaults(dinfo, pixelFormat, subsamp, flags);
1557
+ old_read_markers = dinfo->marker->read_markers;
1558
+ dinfo->marker->read_markers = my_read_markers;
1559
+ old_reset_marker_reader = dinfo->marker->reset_marker_reader;
1560
+ dinfo->marker->reset_marker_reader = my_reset_marker_reader;
1561
+ jpeg_read_header(dinfo, TRUE);
1562
+ dinfo->marker->read_markers = old_read_markers;
1563
+ dinfo->marker->reset_marker_reader = old_reset_marker_reader;
1564
+
1565
+ this->dinfo.out_color_space = pf2cs[pixelFormat];
1566
+ if (flags & TJFLAG_FASTDCT) this->dinfo.dct_method = JDCT_FASTEST;
1567
+ dinfo->do_fancy_upsampling = FALSE;
1568
+ dinfo->Se = DCTSIZE2 - 1;
1569
+ jinit_master_decompress(dinfo);
1570
+ (*dinfo->upsample->start_pass) (dinfo);
1571
+
1572
+ pw0 = PAD(width, dinfo->max_h_samp_factor);
1573
+ ph0 = PAD(height, dinfo->max_v_samp_factor);
1574
+
1575
+ if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
1576
+
1577
+ if ((row_pointer = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph0)) == NULL)
1578
+ THROW("tjDecodeYUVPlanes(): Memory allocation failure");
1579
+ for (i = 0; i < height; i++) {
1580
+ if (flags & TJFLAG_BOTTOMUP)
1581
+ row_pointer[i] = &dstBuf[(height - i - 1) * (size_t)pitch];
1582
+ else
1583
+ row_pointer[i] = &dstBuf[i * (size_t)pitch];
1584
+ }
1585
+ if (height < ph0)
1586
+ for (i = height; i < ph0; i++) row_pointer[i] = row_pointer[height - 1];
1587
+
1588
+ for (i = 0; i < dinfo->num_components; i++) {
1589
+ compptr = &dinfo->comp_info[i];
1590
+ _tmpbuf[i] =
1591
+ (JSAMPLE *)malloc(PAD(compptr->width_in_blocks * DCTSIZE, 32) *
1592
+ compptr->v_samp_factor + 32);
1593
+ if (!_tmpbuf[i])
1594
+ THROW("tjDecodeYUVPlanes(): Memory allocation failure");
1595
+ tmpbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * compptr->v_samp_factor);
1596
+ if (!tmpbuf[i])
1597
+ THROW("tjDecodeYUVPlanes(): Memory allocation failure");
1598
+ for (row = 0; row < compptr->v_samp_factor; row++) {
1599
+ unsigned char *_tmpbuf_aligned =
1600
+ (unsigned char *)PAD((JUINTPTR)_tmpbuf[i], 32);
1601
+
1602
+ tmpbuf[i][row] =
1603
+ &_tmpbuf_aligned[PAD(compptr->width_in_blocks * DCTSIZE, 32) * row];
1604
+ }
1605
+ pw[i] = pw0 * compptr->h_samp_factor / dinfo->max_h_samp_factor;
1606
+ ph[i] = ph0 * compptr->v_samp_factor / dinfo->max_v_samp_factor;
1607
+ inbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i]);
1608
+ if (!inbuf[i])
1609
+ THROW("tjDecodeYUVPlanes(): Memory allocation failure");
1610
+ ptr = (JSAMPLE *)srcPlanes[i];
1611
+ for (row = 0; row < ph[i]; row++) {
1612
+ inbuf[i][row] = ptr;
1613
+ ptr += (strides && strides[i] != 0) ? strides[i] : pw[i];
1614
+ }
1615
+ }
1616
+
1617
+ if (setjmp(this->jerr.setjmp_buffer)) {
1618
+ /* If we get here, the JPEG code has signaled an error. */
1619
+ retval = -1; goto bailout;
1620
+ }
1621
+
1622
+ for (row = 0; row < ph0; row += dinfo->max_v_samp_factor) {
1623
+ JDIMENSION inrow = 0, outrow = 0;
1624
+
1625
+ for (i = 0, compptr = dinfo->comp_info; i < dinfo->num_components;
1626
+ i++, compptr++)
1627
+ jcopy_sample_rows(inbuf[i],
1628
+ row * compptr->v_samp_factor / dinfo->max_v_samp_factor, tmpbuf[i], 0,
1629
+ compptr->v_samp_factor, pw[i]);
1630
+ (dinfo->upsample->upsample) (dinfo, tmpbuf, &inrow,
1631
+ dinfo->max_v_samp_factor, &row_pointer[row],
1632
+ &outrow, dinfo->max_v_samp_factor);
1633
+ }
1634
+ jpeg_abort_decompress(dinfo);
1635
+
1636
+ bailout:
1637
+ if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
1638
+ free(row_pointer);
1639
+ for (i = 0; i < MAX_COMPONENTS; i++) {
1640
+ free(tmpbuf[i]);
1641
+ free(_tmpbuf[i]);
1642
+ free(inbuf[i]);
1643
+ }
1644
+ if (this->jerr.warning) retval = -1;
1645
+ this->jerr.stopOnWarning = FALSE;
1646
+ return retval;
1647
+ }
1648
+
1649
+ /* TurboJPEG 1.4+ */
1650
+ DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
1651
+ int align, int subsamp, unsigned char *dstBuf,
1652
+ int width, int pitch, int height, int pixelFormat,
1653
+ int flags)
1654
+ {
1655
+ const unsigned char *srcPlanes[3];
1656
+ int pw0, ph0, strides[3], retval = -1;
1657
+ tjinstance *this = (tjinstance *)handle;
1658
+
1659
+ if (!this) THROWG("tjDecodeYUV(): Invalid handle");
1660
+ this->isInstanceError = FALSE;
1661
+
1662
+ if (srcBuf == NULL || align < 1 || !IS_POW2(align) || subsamp < 0 ||
1663
+ subsamp >= TJ_NUMSAMP || width <= 0 || height <= 0)
1664
+ THROW("tjDecodeYUV(): Invalid argument");
1665
+
1666
+ pw0 = tjPlaneWidth(0, width, subsamp);
1667
+ ph0 = tjPlaneHeight(0, height, subsamp);
1668
+ srcPlanes[0] = srcBuf;
1669
+ strides[0] = PAD(pw0, align);
1670
+ if (subsamp == TJSAMP_GRAY) {
1671
+ strides[1] = strides[2] = 0;
1672
+ srcPlanes[1] = srcPlanes[2] = NULL;
1673
+ } else {
1674
+ int pw1 = tjPlaneWidth(1, width, subsamp);
1675
+ int ph1 = tjPlaneHeight(1, height, subsamp);
1676
+
1677
+ strides[1] = strides[2] = PAD(pw1, align);
1678
+ srcPlanes[1] = srcPlanes[0] + strides[0] * ph0;
1679
+ srcPlanes[2] = srcPlanes[1] + strides[1] * ph1;
1680
+ }
1681
+
1682
+ return tjDecodeYUVPlanes(handle, srcPlanes, strides, subsamp, dstBuf, width,
1683
+ pitch, height, pixelFormat, flags);
1684
+
1685
+ bailout:
1686
+ return retval;
1687
+ }
1688
+
1689
+ /* TurboJPEG 1.4+ */
1690
+ DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
1691
+ const unsigned char *jpegBuf,
1692
+ unsigned long jpegSize,
1693
+ unsigned char **dstPlanes, int width,
1694
+ int *strides, int height, int flags)
1695
+ {
1696
+ int i, sfi, row, retval = 0;
1697
+ int jpegwidth, jpegheight, jpegSubsamp, scaledw, scaledh;
1698
+ int pw[MAX_COMPONENTS], ph[MAX_COMPONENTS], iw[MAX_COMPONENTS],
1699
+ tmpbufsize = 0, usetmpbuf = 0, th[MAX_COMPONENTS];
1700
+ JSAMPLE *_tmpbuf = NULL, *ptr;
1701
+ JSAMPROW *outbuf[MAX_COMPONENTS], *tmpbuf[MAX_COMPONENTS];
1702
+ int dctsize;
1703
+ struct my_progress_mgr progress;
1704
+
1705
+ GET_DINSTANCE(handle);
1706
+ this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
1707
+
1708
+ for (i = 0; i < MAX_COMPONENTS; i++) {
1709
+ tmpbuf[i] = NULL; outbuf[i] = NULL;
1710
+ }
1711
+
1712
+ if ((this->init & DECOMPRESS) == 0)
1713
+ THROW("tjDecompressToYUVPlanes(): Instance has not been initialized for decompression");
1714
+
1715
+ if (jpegBuf == NULL || jpegSize <= 0 || !dstPlanes || !dstPlanes[0] ||
1716
+ width < 0 || height < 0)
1717
+ THROW("tjDecompressToYUVPlanes(): Invalid argument");
1718
+
1719
+ #ifndef NO_PUTENV
1720
+ if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1");
1721
+ else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1");
1722
+ else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1");
1723
+ #endif
1724
+
1725
+ if (flags & TJFLAG_LIMITSCANS) {
1726
+ memset(&progress, 0, sizeof(struct my_progress_mgr));
1727
+ progress.pub.progress_monitor = my_progress_monitor;
1728
+ progress.this = this;
1729
+ dinfo->progress = &progress.pub;
1730
+ } else
1731
+ dinfo->progress = NULL;
1732
+
1733
+ if (setjmp(this->jerr.setjmp_buffer)) {
1734
+ /* If we get here, the JPEG code has signaled an error. */
1735
+ retval = -1; goto bailout;
1736
+ }
1737
+
1738
+ if (!this->headerRead) {
1739
+ jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
1740
+ jpeg_read_header(dinfo, TRUE);
1741
+ }
1742
+ this->headerRead = 0;
1743
+ jpegSubsamp = getSubsamp(dinfo);
1744
+ if (jpegSubsamp < 0)
1745
+ THROW("tjDecompressToYUVPlanes(): Could not determine subsampling type for JPEG image");
1746
+
1747
+ if (jpegSubsamp != TJSAMP_GRAY && (!dstPlanes[1] || !dstPlanes[2]))
1748
+ THROW("tjDecompressToYUVPlanes(): Invalid argument");
1749
+
1750
+ jpegwidth = dinfo->image_width; jpegheight = dinfo->image_height;
1751
+ if (width == 0) width = jpegwidth;
1752
+ if (height == 0) height = jpegheight;
1753
+ for (i = 0; i < NUMSF; i++) {
1754
+ scaledw = TJSCALED(jpegwidth, sf[i]);
1755
+ scaledh = TJSCALED(jpegheight, sf[i]);
1756
+ if (scaledw <= width && scaledh <= height)
1757
+ break;
1758
+ }
1759
+ if (i >= NUMSF)
1760
+ THROW("tjDecompressToYUVPlanes(): Could not scale down to desired image dimensions");
1761
+ if (dinfo->num_components > 3)
1762
+ THROW("tjDecompressToYUVPlanes(): JPEG image must have 3 or fewer components");
1763
+
1764
+ width = scaledw; height = scaledh;
1765
+ dinfo->scale_num = sf[i].num;
1766
+ dinfo->scale_denom = sf[i].denom;
1767
+ sfi = i;
1768
+ jpeg_calc_output_dimensions(dinfo);
1769
+
1770
+ dctsize = DCTSIZE * sf[sfi].num / sf[sfi].denom;
1771
+
1772
+ for (i = 0; i < dinfo->num_components; i++) {
1773
+ jpeg_component_info *compptr = &dinfo->comp_info[i];
1774
+ int ih;
1775
+
1776
+ iw[i] = compptr->width_in_blocks * dctsize;
1777
+ ih = compptr->height_in_blocks * dctsize;
1778
+ pw[i] = tjPlaneWidth(i, dinfo->output_width, jpegSubsamp);
1779
+ ph[i] = tjPlaneHeight(i, dinfo->output_height, jpegSubsamp);
1780
+ if (iw[i] != pw[i] || ih != ph[i]) usetmpbuf = 1;
1781
+ th[i] = compptr->v_samp_factor * dctsize;
1782
+ tmpbufsize += iw[i] * th[i];
1783
+ if ((outbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i])) == NULL)
1784
+ THROW("tjDecompressToYUVPlanes(): Memory allocation failure");
1785
+ ptr = dstPlanes[i];
1786
+ for (row = 0; row < ph[i]; row++) {
1787
+ outbuf[i][row] = ptr;
1788
+ ptr += (strides && strides[i] != 0) ? strides[i] : pw[i];
1789
+ }
1790
+ }
1791
+ if (usetmpbuf) {
1792
+ if ((_tmpbuf = (JSAMPLE *)malloc(sizeof(JSAMPLE) * tmpbufsize)) == NULL)
1793
+ THROW("tjDecompressToYUVPlanes(): Memory allocation failure");
1794
+ ptr = _tmpbuf;
1795
+ for (i = 0; i < dinfo->num_components; i++) {
1796
+ if ((tmpbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * th[i])) == NULL)
1797
+ THROW("tjDecompressToYUVPlanes(): Memory allocation failure");
1798
+ for (row = 0; row < th[i]; row++) {
1799
+ tmpbuf[i][row] = ptr;
1800
+ ptr += iw[i];
1801
+ }
1802
+ }
1803
+ }
1804
+
1805
+ if (setjmp(this->jerr.setjmp_buffer)) {
1806
+ /* If we get here, the JPEG code has signaled an error. */
1807
+ retval = -1; goto bailout;
1808
+ }
1809
+
1810
+ if (flags & TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling = FALSE;
1811
+ if (flags & TJFLAG_FASTDCT) dinfo->dct_method = JDCT_FASTEST;
1812
+ dinfo->raw_data_out = TRUE;
1813
+
1814
+ jpeg_start_decompress(dinfo);
1815
+ for (row = 0; row < (int)dinfo->output_height;
1816
+ row += dinfo->max_v_samp_factor * dinfo->_min_DCT_scaled_size) {
1817
+ JSAMPARRAY yuvptr[MAX_COMPONENTS];
1818
+ int crow[MAX_COMPONENTS];
1819
+
1820
+ for (i = 0; i < dinfo->num_components; i++) {
1821
+ jpeg_component_info *compptr = &dinfo->comp_info[i];
1822
+
1823
+ if (jpegSubsamp == TJSAMP_420) {
1824
+ /* When 4:2:0 subsampling is used with IDCT scaling, libjpeg will try
1825
+ to be clever and use the IDCT to perform upsampling on the U and V
1826
+ planes. For instance, if the output image is to be scaled by 1/2
1827
+ relative to the JPEG image, then the scaling factor and upsampling
1828
+ effectively cancel each other, so a normal 8x8 IDCT can be used.
1829
+ However, this is not desirable when using the decompress-to-YUV
1830
+ functionality in TurboJPEG, since we want to output the U and V
1831
+ planes in their subsampled form. Thus, we have to override some
1832
+ internal libjpeg parameters to force it to use the "scaled" IDCT
1833
+ functions on the U and V planes. */
1834
+ compptr->_DCT_scaled_size = dctsize;
1835
+ compptr->MCU_sample_width = tjMCUWidth[jpegSubsamp] *
1836
+ sf[sfi].num / sf[sfi].denom *
1837
+ compptr->v_samp_factor / dinfo->max_v_samp_factor;
1838
+ dinfo->idct->inverse_DCT[i] = dinfo->idct->inverse_DCT[0];
1839
+ }
1840
+ crow[i] = row * compptr->v_samp_factor / dinfo->max_v_samp_factor;
1841
+ if (usetmpbuf) yuvptr[i] = tmpbuf[i];
1842
+ else yuvptr[i] = &outbuf[i][crow[i]];
1843
+ }
1844
+ jpeg_read_raw_data(dinfo, yuvptr,
1845
+ dinfo->max_v_samp_factor * dinfo->_min_DCT_scaled_size);
1846
+ if (usetmpbuf) {
1847
+ int j;
1848
+
1849
+ for (i = 0; i < dinfo->num_components; i++) {
1850
+ for (j = 0; j < MIN(th[i], ph[i] - crow[i]); j++) {
1851
+ memcpy(outbuf[i][crow[i] + j], tmpbuf[i][j], pw[i]);
1852
+ }
1853
+ }
1854
+ }
1855
+ }
1856
+ jpeg_finish_decompress(dinfo);
1857
+
1858
+ bailout:
1859
+ if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
1860
+ for (i = 0; i < MAX_COMPONENTS; i++) {
1861
+ free(tmpbuf[i]);
1862
+ free(outbuf[i]);
1863
+ }
1864
+ free(_tmpbuf);
1865
+ if (this->jerr.warning) retval = -1;
1866
+ this->jerr.stopOnWarning = FALSE;
1867
+ return retval;
1868
+ }
1869
+
1870
+ /* TurboJPEG 1.4+ */
1871
+ DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
1872
+ unsigned long jpegSize, unsigned char *dstBuf,
1873
+ int width, int align, int height, int flags)
1874
+ {
1875
+ unsigned char *dstPlanes[3];
1876
+ int pw0, ph0, strides[3], retval = -1, jpegSubsamp = -1;
1877
+ int i, jpegwidth, jpegheight, scaledw, scaledh;
1878
+
1879
+ GET_DINSTANCE(handle);
1880
+ this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
1881
+
1882
+ if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || width < 0 ||
1883
+ align < 1 || !IS_POW2(align) || height < 0)
1884
+ THROW("tjDecompressToYUV2(): Invalid argument");
1885
+
1886
+ if (setjmp(this->jerr.setjmp_buffer)) {
1887
+ /* If we get here, the JPEG code has signaled an error. */
1888
+ return -1;
1889
+ }
1890
+
1891
+ jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
1892
+ jpeg_read_header(dinfo, TRUE);
1893
+ jpegSubsamp = getSubsamp(dinfo);
1894
+ if (jpegSubsamp < 0)
1895
+ THROW("tjDecompressToYUV2(): Could not determine subsampling type for JPEG image");
1896
+
1897
+ jpegwidth = dinfo->image_width; jpegheight = dinfo->image_height;
1898
+ if (width == 0) width = jpegwidth;
1899
+ if (height == 0) height = jpegheight;
1900
+ for (i = 0; i < NUMSF; i++) {
1901
+ scaledw = TJSCALED(jpegwidth, sf[i]);
1902
+ scaledh = TJSCALED(jpegheight, sf[i]);
1903
+ if (scaledw <= width && scaledh <= height)
1904
+ break;
1905
+ }
1906
+ if (i >= NUMSF)
1907
+ THROW("tjDecompressToYUV2(): Could not scale down to desired image dimensions");
1908
+
1909
+ width = scaledw; height = scaledh;
1910
+
1911
+ pw0 = tjPlaneWidth(0, width, jpegSubsamp);
1912
+ ph0 = tjPlaneHeight(0, height, jpegSubsamp);
1913
+ dstPlanes[0] = dstBuf;
1914
+ strides[0] = PAD(pw0, align);
1915
+ if (jpegSubsamp == TJSAMP_GRAY) {
1916
+ strides[1] = strides[2] = 0;
1917
+ dstPlanes[1] = dstPlanes[2] = NULL;
1918
+ } else {
1919
+ int pw1 = tjPlaneWidth(1, width, jpegSubsamp);
1920
+ int ph1 = tjPlaneHeight(1, height, jpegSubsamp);
1921
+
1922
+ strides[1] = strides[2] = PAD(pw1, align);
1923
+ dstPlanes[1] = dstPlanes[0] + strides[0] * ph0;
1924
+ dstPlanes[2] = dstPlanes[1] + strides[1] * ph1;
1925
+ }
1926
+
1927
+ this->headerRead = 1;
1928
+ return tjDecompressToYUVPlanes(handle, jpegBuf, jpegSize, dstPlanes, width,
1929
+ strides, height, flags);
1930
+
1931
+ bailout:
1932
+ this->jerr.stopOnWarning = FALSE;
1933
+ return retval;
1934
+ }
1935
+
1936
+ /* TurboJPEG 1.1+ */
1937
+ DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
1938
+ unsigned long jpegSize, unsigned char *dstBuf,
1939
+ int flags)
1940
+ {
1941
+ return tjDecompressToYUV2(handle, jpegBuf, jpegSize, dstBuf, 0, 4, 0, flags);
1942
+ }
1943
+
1944
+
1945
+ /******************************** Transformer ********************************/
1946
+
1947
+ /* TurboJPEG 1.2+ */
1948
+ DLLEXPORT tjhandle tjInitTransform(void)
1949
+ {
1950
+ tjinstance *this = NULL;
1951
+ tjhandle handle = NULL;
1952
+
1953
+ if ((this = (tjinstance *)malloc(sizeof(tjinstance))) == NULL) {
1954
+ SNPRINTF(errStr, JMSG_LENGTH_MAX,
1955
+ "tjInitTransform(): Memory allocation failure");
1956
+ return NULL;
1957
+ }
1958
+ memset(this, 0, sizeof(tjinstance));
1959
+ SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "No error");
1960
+ handle = _tjInitCompress(this);
1961
+ if (!handle) return NULL;
1962
+ handle = _tjInitDecompress(this);
1963
+ return handle;
1964
+ }
1965
+
1966
+
1967
+ /* TurboJPEG 1.2+ */
1968
+ DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
1969
+ unsigned long jpegSize, int n,
1970
+ unsigned char **dstBufs, unsigned long *dstSizes,
1971
+ tjtransform *t, int flags)
1972
+ {
1973
+ jpeg_transform_info *xinfo = NULL;
1974
+ jvirt_barray_ptr *srccoefs, *dstcoefs;
1975
+ int retval = 0, i, jpegSubsamp, saveMarkers = 0;
1976
+ boolean alloc = TRUE;
1977
+ struct my_progress_mgr progress;
1978
+
1979
+ GET_INSTANCE(handle);
1980
+ this->jerr.stopOnWarning = (flags & TJFLAG_STOPONWARNING) ? TRUE : FALSE;
1981
+ if ((this->init & COMPRESS) == 0 || (this->init & DECOMPRESS) == 0)
1982
+ THROW("tjTransform(): Instance has not been initialized for transformation");
1983
+
1984
+ if (jpegBuf == NULL || jpegSize <= 0 || n < 1 || dstBufs == NULL ||
1985
+ dstSizes == NULL || t == NULL || flags < 0)
1986
+ THROW("tjTransform(): Invalid argument");
1987
+
1988
+ #ifndef NO_PUTENV
1989
+ if (flags & TJFLAG_FORCEMMX) PUTENV_S("JSIMD_FORCEMMX", "1");
1990
+ else if (flags & TJFLAG_FORCESSE) PUTENV_S("JSIMD_FORCESSE", "1");
1991
+ else if (flags & TJFLAG_FORCESSE2) PUTENV_S("JSIMD_FORCESSE2", "1");
1992
+ #endif
1993
+
1994
+ if (flags & TJFLAG_LIMITSCANS) {
1995
+ memset(&progress, 0, sizeof(struct my_progress_mgr));
1996
+ progress.pub.progress_monitor = my_progress_monitor;
1997
+ progress.this = this;
1998
+ dinfo->progress = &progress.pub;
1999
+ } else
2000
+ dinfo->progress = NULL;
2001
+
2002
+ if ((xinfo =
2003
+ (jpeg_transform_info *)malloc(sizeof(jpeg_transform_info) * n)) == NULL)
2004
+ THROW("tjTransform(): Memory allocation failure");
2005
+ memset(xinfo, 0, sizeof(jpeg_transform_info) * n);
2006
+
2007
+ if (setjmp(this->jerr.setjmp_buffer)) {
2008
+ /* If we get here, the JPEG code has signaled an error. */
2009
+ retval = -1; goto bailout;
2010
+ }
2011
+
2012
+ jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
2013
+
2014
+ for (i = 0; i < n; i++) {
2015
+ xinfo[i].transform = xformtypes[t[i].op];
2016
+ xinfo[i].perfect = (t[i].options & TJXOPT_PERFECT) ? 1 : 0;
2017
+ xinfo[i].trim = (t[i].options & TJXOPT_TRIM) ? 1 : 0;
2018
+ xinfo[i].force_grayscale = (t[i].options & TJXOPT_GRAY) ? 1 : 0;
2019
+ xinfo[i].crop = (t[i].options & TJXOPT_CROP) ? 1 : 0;
2020
+ if (n != 1 && t[i].op == TJXOP_HFLIP) xinfo[i].slow_hflip = 1;
2021
+ else xinfo[i].slow_hflip = 0;
2022
+
2023
+ if (xinfo[i].crop) {
2024
+ xinfo[i].crop_xoffset = t[i].r.x; xinfo[i].crop_xoffset_set = JCROP_POS;
2025
+ xinfo[i].crop_yoffset = t[i].r.y; xinfo[i].crop_yoffset_set = JCROP_POS;
2026
+ if (t[i].r.w != 0) {
2027
+ xinfo[i].crop_width = t[i].r.w; xinfo[i].crop_width_set = JCROP_POS;
2028
+ } else
2029
+ xinfo[i].crop_width = JCROP_UNSET;
2030
+ if (t[i].r.h != 0) {
2031
+ xinfo[i].crop_height = t[i].r.h; xinfo[i].crop_height_set = JCROP_POS;
2032
+ } else
2033
+ xinfo[i].crop_height = JCROP_UNSET;
2034
+ }
2035
+ if (!(t[i].options & TJXOPT_COPYNONE)) saveMarkers = 1;
2036
+ }
2037
+
2038
+ jcopy_markers_setup(dinfo, saveMarkers ? JCOPYOPT_ALL : JCOPYOPT_NONE);
2039
+ jpeg_read_header(dinfo, TRUE);
2040
+ jpegSubsamp = getSubsamp(dinfo);
2041
+ if (jpegSubsamp < 0)
2042
+ THROW("tjTransform(): Could not determine subsampling type for JPEG image");
2043
+
2044
+ for (i = 0; i < n; i++) {
2045
+ if (!jtransform_request_workspace(dinfo, &xinfo[i]))
2046
+ THROW("tjTransform(): Transform is not perfect");
2047
+
2048
+ if (xinfo[i].crop) {
2049
+ if ((t[i].r.x % tjMCUWidth[jpegSubsamp]) != 0 ||
2050
+ (t[i].r.y % tjMCUHeight[jpegSubsamp]) != 0) {
2051
+ SNPRINTF(this->errStr, JMSG_LENGTH_MAX,
2052
+ "To crop this JPEG image, x must be a multiple of %d\n"
2053
+ "and y must be a multiple of %d.\n",
2054
+ tjMCUWidth[jpegSubsamp], tjMCUHeight[jpegSubsamp]);
2055
+ this->isInstanceError = TRUE;
2056
+ retval = -1; goto bailout;
2057
+ }
2058
+ }
2059
+ }
2060
+
2061
+ srccoefs = jpeg_read_coefficients(dinfo);
2062
+
2063
+ for (i = 0; i < n; i++) {
2064
+ int w, h;
2065
+
2066
+ if (!xinfo[i].crop) {
2067
+ w = dinfo->image_width; h = dinfo->image_height;
2068
+ if (t[i].op == TJXOP_TRANSPOSE || t[i].op == TJXOP_TRANSVERSE ||
2069
+ t[i].op == TJXOP_ROT90 || t[i].op == TJXOP_ROT270) {
2070
+ w = dinfo->image_height; h = dinfo->image_width;
2071
+ }
2072
+ } else {
2073
+ w = xinfo[i].crop_width; h = xinfo[i].crop_height;
2074
+ }
2075
+ if (flags & TJFLAG_NOREALLOC) {
2076
+ alloc = FALSE; dstSizes[i] = tjBufSize(w, h, jpegSubsamp);
2077
+ }
2078
+ if (!(t[i].options & TJXOPT_NOOUTPUT))
2079
+ jpeg_mem_dest_tj(cinfo, &dstBufs[i], &dstSizes[i], alloc);
2080
+ jpeg_copy_critical_parameters(dinfo, cinfo);
2081
+ dstcoefs = jtransform_adjust_parameters(dinfo, cinfo, srccoefs, &xinfo[i]);
2082
+ #ifdef C_PROGRESSIVE_SUPPORTED
2083
+ if (flags & TJFLAG_PROGRESSIVE || t[i].options & TJXOPT_PROGRESSIVE)
2084
+ jpeg_simple_progression(cinfo);
2085
+ #endif
2086
+ if (!(t[i].options & TJXOPT_NOOUTPUT)) {
2087
+ jpeg_write_coefficients(cinfo, dstcoefs);
2088
+ jcopy_markers_execute(dinfo, cinfo, t[i].options & TJXOPT_COPYNONE ?
2089
+ JCOPYOPT_NONE : JCOPYOPT_ALL);
2090
+ } else
2091
+ jinit_c_master_control(cinfo, TRUE);
2092
+ jtransform_execute_transformation(dinfo, cinfo, srccoefs, &xinfo[i]);
2093
+ if (t[i].customFilter) {
2094
+ int ci, y;
2095
+ JDIMENSION by;
2096
+
2097
+ for (ci = 0; ci < cinfo->num_components; ci++) {
2098
+ jpeg_component_info *compptr = &cinfo->comp_info[ci];
2099
+ tjregion arrayRegion = { 0, 0, 0, 0 };
2100
+ tjregion planeRegion = { 0, 0, 0, 0 };
2101
+
2102
+ arrayRegion.w = compptr->width_in_blocks * DCTSIZE;
2103
+ arrayRegion.h = DCTSIZE;
2104
+ planeRegion.w = compptr->width_in_blocks * DCTSIZE;
2105
+ planeRegion.h = compptr->height_in_blocks * DCTSIZE;
2106
+
2107
+ for (by = 0; by < compptr->height_in_blocks;
2108
+ by += compptr->v_samp_factor) {
2109
+ JBLOCKARRAY barray = (dinfo->mem->access_virt_barray)
2110
+ ((j_common_ptr)dinfo, dstcoefs[ci], by, compptr->v_samp_factor,
2111
+ TRUE);
2112
+
2113
+ for (y = 0; y < compptr->v_samp_factor; y++) {
2114
+ if (t[i].customFilter(barray[y][0], arrayRegion, planeRegion, ci,
2115
+ i, &t[i]) == -1)
2116
+ THROW("tjTransform(): Error in custom filter");
2117
+ arrayRegion.y += DCTSIZE;
2118
+ }
2119
+ }
2120
+ }
2121
+ }
2122
+ if (!(t[i].options & TJXOPT_NOOUTPUT)) jpeg_finish_compress(cinfo);
2123
+ }
2124
+
2125
+ jpeg_finish_decompress(dinfo);
2126
+
2127
+ bailout:
2128
+ if (cinfo->global_state > CSTATE_START) {
2129
+ if (alloc) (*cinfo->dest->term_destination) (cinfo);
2130
+ jpeg_abort_compress(cinfo);
2131
+ }
2132
+ if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
2133
+ free(xinfo);
2134
+ if (this->jerr.warning) retval = -1;
2135
+ this->jerr.stopOnWarning = FALSE;
2136
+ return retval;
2137
+ }
2138
+
2139
+
2140
+ /*************************** Packed-Pixel Image I/O **************************/
2141
+
2142
+ /* TurboJPEG 2.0+ */
2143
+ DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
2144
+ int align, int *height, int *pixelFormat,
2145
+ int flags)
2146
+ {
2147
+ int retval = 0, tempc;
2148
+ size_t pitch;
2149
+ tjhandle handle = NULL;
2150
+ tjinstance *this;
2151
+ j_compress_ptr cinfo = NULL;
2152
+ cjpeg_source_ptr src;
2153
+ unsigned char *dstBuf = NULL;
2154
+ FILE *file = NULL;
2155
+ boolean invert;
2156
+
2157
+ if (!filename || !width || align < 1 || !height || !pixelFormat ||
2158
+ *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
2159
+ THROWG("tjLoadImage(): Invalid argument");
2160
+ if ((align & (align - 1)) != 0)
2161
+ THROWG("tjLoadImage(): Alignment must be a power of 2");
2162
+
2163
+ if ((handle = tjInitCompress()) == NULL) return NULL;
2164
+ this = (tjinstance *)handle;
2165
+ cinfo = &this->cinfo;
2166
+
2167
+ #ifdef _MSC_VER
2168
+ if (fopen_s(&file, filename, "rb") || file == NULL)
2169
+ #else
2170
+ if ((file = fopen(filename, "rb")) == NULL)
2171
+ #endif
2172
+ THROW_UNIX("tjLoadImage(): Cannot open input file");
2173
+
2174
+ if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
2175
+ THROW_UNIX("tjLoadImage(): Could not read input file")
2176
+ else if (tempc == EOF)
2177
+ THROWG("tjLoadImage(): Input file contains no data");
2178
+
2179
+ if (setjmp(this->jerr.setjmp_buffer)) {
2180
+ /* If we get here, the JPEG code has signaled an error. */
2181
+ retval = -1; goto bailout;
2182
+ }
2183
+
2184
+ if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
2185
+ else cinfo->in_color_space = pf2cs[*pixelFormat];
2186
+ if (tempc == 'B') {
2187
+ if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
2188
+ THROWG("tjLoadImage(): Could not initialize bitmap loader");
2189
+ invert = (flags & TJFLAG_BOTTOMUP) == 0;
2190
+ } else if (tempc == 'P') {
2191
+ if ((src = jinit_read_ppm(cinfo)) == NULL)
2192
+ THROWG("tjLoadImage(): Could not initialize PPM loader");
2193
+ invert = (flags & TJFLAG_BOTTOMUP) != 0;
2194
+ } else
2195
+ THROWG("tjLoadImage(): Unsupported file type");
2196
+
2197
+ src->input_file = file;
2198
+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2199
+ /* Refuse to load images larger than 1 Megapixel when fuzzing. */
2200
+ if (flags & TJFLAG_FUZZING)
2201
+ src->max_pixels = 1048576;
2202
+ #endif
2203
+ (*src->start_input) (cinfo, src);
2204
+ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
2205
+
2206
+ *width = cinfo->image_width; *height = cinfo->image_height;
2207
+ *pixelFormat = cs2pf[cinfo->in_color_space];
2208
+
2209
+ pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
2210
+ if ((unsigned long long)pitch * (unsigned long long)(*height) >
2211
+ (unsigned long long)((size_t)-1) ||
2212
+ (dstBuf = (unsigned char *)malloc(pitch * (*height))) == NULL)
2213
+ THROWG("tjLoadImage(): Memory allocation failure");
2214
+
2215
+ if (setjmp(this->jerr.setjmp_buffer)) {
2216
+ /* If we get here, the JPEG code has signaled an error. */
2217
+ retval = -1; goto bailout;
2218
+ }
2219
+
2220
+ while (cinfo->next_scanline < cinfo->image_height) {
2221
+ int i, nlines = (*src->get_pixel_rows) (cinfo, src);
2222
+
2223
+ for (i = 0; i < nlines; i++) {
2224
+ unsigned char *dstptr;
2225
+ int row;
2226
+
2227
+ row = cinfo->next_scanline + i;
2228
+ if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
2229
+ else dstptr = &dstBuf[row * pitch];
2230
+ memcpy(dstptr, src->buffer[i], (*width) * tjPixelSize[*pixelFormat]);
2231
+ }
2232
+ cinfo->next_scanline += nlines;
2233
+ }
2234
+
2235
+ (*src->finish_input) (cinfo, src);
2236
+
2237
+ bailout:
2238
+ if (handle) tjDestroy(handle);
2239
+ if (file) fclose(file);
2240
+ if (retval < 0) { free(dstBuf); dstBuf = NULL; }
2241
+ return dstBuf;
2242
+ }
2243
+
2244
+
2245
+ /* TurboJPEG 2.0+ */
2246
+ DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
2247
+ int width, int pitch, int height, int pixelFormat,
2248
+ int flags)
2249
+ {
2250
+ int retval = 0;
2251
+ tjhandle handle = NULL;
2252
+ tjinstance *this;
2253
+ j_decompress_ptr dinfo = NULL;
2254
+ djpeg_dest_ptr dst;
2255
+ FILE *file = NULL;
2256
+ char *ptr = NULL;
2257
+ boolean invert;
2258
+
2259
+ if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
2260
+ pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
2261
+ THROWG("tjSaveImage(): Invalid argument");
2262
+
2263
+ if ((handle = tjInitDecompress()) == NULL)
2264
+ return -1;
2265
+ this = (tjinstance *)handle;
2266
+ dinfo = &this->dinfo;
2267
+
2268
+ #ifdef _MSC_VER
2269
+ if (fopen_s(&file, filename, "wb") || file == NULL)
2270
+ #else
2271
+ if ((file = fopen(filename, "wb")) == NULL)
2272
+ #endif
2273
+ THROW_UNIX("tjSaveImage(): Cannot open output file");
2274
+
2275
+ if (setjmp(this->jerr.setjmp_buffer)) {
2276
+ /* If we get here, the JPEG code has signaled an error. */
2277
+ retval = -1; goto bailout;
2278
+ }
2279
+
2280
+ this->dinfo.out_color_space = pf2cs[pixelFormat];
2281
+ dinfo->image_width = width; dinfo->image_height = height;
2282
+ dinfo->global_state = DSTATE_READY;
2283
+ dinfo->scale_num = dinfo->scale_denom = 1;
2284
+
2285
+ ptr = strrchr(filename, '.');
2286
+ if (ptr && !strcasecmp(ptr, ".bmp")) {
2287
+ if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
2288
+ THROWG("tjSaveImage(): Could not initialize bitmap writer");
2289
+ invert = (flags & TJFLAG_BOTTOMUP) == 0;
2290
+ } else {
2291
+ if ((dst = jinit_write_ppm(dinfo)) == NULL)
2292
+ THROWG("tjSaveImage(): Could not initialize PPM writer");
2293
+ invert = (flags & TJFLAG_BOTTOMUP) != 0;
2294
+ }
2295
+
2296
+ dst->output_file = file;
2297
+ (*dst->start_output) (dinfo, dst);
2298
+ (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
2299
+
2300
+ if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
2301
+
2302
+ while (dinfo->output_scanline < dinfo->output_height) {
2303
+ unsigned char *rowptr;
2304
+
2305
+ if (invert)
2306
+ rowptr = &buffer[(height - dinfo->output_scanline - 1) * pitch];
2307
+ else
2308
+ rowptr = &buffer[dinfo->output_scanline * pitch];
2309
+ memcpy(dst->buffer[0], rowptr, width * tjPixelSize[pixelFormat]);
2310
+ (*dst->put_pixel_rows) (dinfo, dst, 1);
2311
+ dinfo->output_scanline++;
2312
+ }
2313
+
2314
+ (*dst->finish_output) (dinfo, dst);
2315
+
2316
+ bailout:
2317
+ if (handle) tjDestroy(handle);
2318
+ if (file) fclose(file);
2319
+ return retval;
2320
+ }