turbojpeg.h 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. /*
  2. * Copyright (C)2009-2015, 2017, 2020-2021 D. R. Commander.
  3. * All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * - Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * - Neither the name of the libjpeg-turbo Project nor the names of its
  14. * contributors may be used to endorse or promote products derived from this
  15. * software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef __TURBOJPEG_H__
  30. #define __TURBOJPEG_H__
  31. #if defined(_WIN32) && defined(DLLDEFINE)
  32. #define DLLEXPORT __declspec(dllexport)
  33. #else
  34. #define DLLEXPORT
  35. #endif
  36. #define DLLCALL
  37. /**
  38. * @addtogroup TurboJPEG
  39. * TurboJPEG API. This API provides an interface for generating, decoding, and
  40. * transforming planar YUV and JPEG images in memory.
  41. *
  42. * @anchor YUVnotes
  43. * YUV Image Format Notes
  44. * ----------------------
  45. * Technically, the JPEG format uses the YCbCr colorspace (which is technically
  46. * not a colorspace but a color transform), but per the convention of the
  47. * digital video community, the TurboJPEG API uses "YUV" to refer to an image
  48. * format consisting of Y, Cb, and Cr image planes.
  49. *
  50. * Each plane is simply a 2D array of bytes, each byte representing the value
  51. * of one of the components (Y, Cb, or Cr) at a particular location in the
  52. * image. The width and height of each plane are determined by the image
  53. * width, height, and level of chrominance subsampling. The luminance plane
  54. * width is the image width padded to the nearest multiple of the horizontal
  55. * subsampling factor (2 in the case of 4:2:0 and 4:2:2, 4 in the case of
  56. * 4:1:1, 1 in the case of 4:4:4 or grayscale.) Similarly, the luminance plane
  57. * height is the image height padded to the nearest multiple of the vertical
  58. * subsampling factor (2 in the case of 4:2:0 or 4:4:0, 1 in the case of 4:4:4
  59. * or grayscale.) This is irrespective of any additional padding that may be
  60. * specified as an argument to the various YUV functions. The chrominance
  61. * plane width is equal to the luminance plane width divided by the horizontal
  62. * subsampling factor, and the chrominance plane height is equal to the
  63. * luminance plane height divided by the vertical subsampling factor.
  64. *
  65. * For example, if the source image is 35 x 35 pixels and 4:2:2 subsampling is
  66. * used, then the luminance plane would be 36 x 35 bytes, and each of the
  67. * chrominance planes would be 18 x 35 bytes. If you specify a line padding of
  68. * 4 bytes on top of this, then the luminance plane would be 36 x 35 bytes, and
  69. * each of the chrominance planes would be 20 x 35 bytes.
  70. *
  71. * @{
  72. */
  73. /**
  74. * The number of chrominance subsampling options
  75. */
  76. #define TJ_NUMSAMP 6
  77. /**
  78. * Chrominance subsampling options.
  79. * When pixels are converted from RGB to YCbCr (see #TJCS_YCbCr) or from CMYK
  80. * to YCCK (see #TJCS_YCCK) as part of the JPEG compression process, some of
  81. * the Cb and Cr (chrominance) components can be discarded or averaged together
  82. * to produce a smaller image with little perceptible loss of image clarity
  83. * (the human eye is more sensitive to small changes in brightness than to
  84. * small changes in color.) This is called "chrominance subsampling".
  85. */
  86. enum TJSAMP {
  87. /**
  88. * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG or
  89. * YUV image will contain one chrominance component for every pixel in the
  90. * source image.
  91. */
  92. TJSAMP_444 = 0,
  93. /**
  94. * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
  95. * chrominance component for every 2x1 block of pixels in the source image.
  96. */
  97. TJSAMP_422,
  98. /**
  99. * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
  100. * chrominance component for every 2x2 block of pixels in the source image.
  101. */
  102. TJSAMP_420,
  103. /**
  104. * Grayscale. The JPEG or YUV image will contain no chrominance components.
  105. */
  106. TJSAMP_GRAY,
  107. /**
  108. * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
  109. * chrominance component for every 1x2 block of pixels in the source image.
  110. *
  111. * @note 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
  112. */
  113. TJSAMP_440,
  114. /**
  115. * 4:1:1 chrominance subsampling. The JPEG or YUV image will contain one
  116. * chrominance component for every 4x1 block of pixels in the source image.
  117. * JPEG images compressed with 4:1:1 subsampling will be almost exactly the
  118. * same size as those compressed with 4:2:0 subsampling, and in the
  119. * aggregate, both subsampling methods produce approximately the same
  120. * perceptual quality. However, 4:1:1 is better able to reproduce sharp
  121. * horizontal features.
  122. *
  123. * @note 4:1:1 subsampling is not fully accelerated in libjpeg-turbo.
  124. */
  125. TJSAMP_411
  126. };
  127. /**
  128. * MCU block width (in pixels) for a given level of chrominance subsampling.
  129. * MCU block sizes:
  130. * - 8x8 for no subsampling or grayscale
  131. * - 16x8 for 4:2:2
  132. * - 8x16 for 4:4:0
  133. * - 16x16 for 4:2:0
  134. * - 32x8 for 4:1:1
  135. */
  136. static const int tjMCUWidth[TJ_NUMSAMP] = { 8, 16, 16, 8, 8, 32 };
  137. /**
  138. * MCU block height (in pixels) for a given level of chrominance subsampling.
  139. * MCU block sizes:
  140. * - 8x8 for no subsampling or grayscale
  141. * - 16x8 for 4:2:2
  142. * - 8x16 for 4:4:0
  143. * - 16x16 for 4:2:0
  144. * - 32x8 for 4:1:1
  145. */
  146. static const int tjMCUHeight[TJ_NUMSAMP] = { 8, 8, 16, 8, 16, 8 };
  147. /**
  148. * The number of pixel formats
  149. */
  150. #define TJ_NUMPF 12
  151. /**
  152. * Pixel formats
  153. */
  154. enum TJPF {
  155. /**
  156. * RGB pixel format. The red, green, and blue components in the image are
  157. * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
  158. * address within each pixel.
  159. */
  160. TJPF_RGB = 0,
  161. /**
  162. * BGR pixel format. The red, green, and blue components in the image are
  163. * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
  164. * address within each pixel.
  165. */
  166. TJPF_BGR,
  167. /**
  168. * RGBX pixel format. The red, green, and blue components in the image are
  169. * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
  170. * address within each pixel. The X component is ignored when compressing
  171. * and undefined when decompressing.
  172. */
  173. TJPF_RGBX,
  174. /**
  175. * BGRX pixel format. The red, green, and blue components in the image are
  176. * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
  177. * address within each pixel. The X component is ignored when compressing
  178. * and undefined when decompressing.
  179. */
  180. TJPF_BGRX,
  181. /**
  182. * XBGR pixel format. The red, green, and blue components in the image are
  183. * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
  184. * address within each pixel. The X component is ignored when compressing
  185. * and undefined when decompressing.
  186. */
  187. TJPF_XBGR,
  188. /**
  189. * XRGB pixel format. The red, green, and blue components in the image are
  190. * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
  191. * address within each pixel. The X component is ignored when compressing
  192. * and undefined when decompressing.
  193. */
  194. TJPF_XRGB,
  195. /**
  196. * Grayscale pixel format. Each 1-byte pixel represents a luminance
  197. * (brightness) level from 0 to 255.
  198. */
  199. TJPF_GRAY,
  200. /**
  201. * RGBA pixel format. This is the same as @ref TJPF_RGBX, except that when
  202. * decompressing, the X component is guaranteed to be 0xFF, which can be
  203. * interpreted as an opaque alpha channel.
  204. */
  205. TJPF_RGBA,
  206. /**
  207. * BGRA pixel format. This is the same as @ref TJPF_BGRX, except that when
  208. * decompressing, the X component is guaranteed to be 0xFF, which can be
  209. * interpreted as an opaque alpha channel.
  210. */
  211. TJPF_BGRA,
  212. /**
  213. * ABGR pixel format. This is the same as @ref TJPF_XBGR, except that when
  214. * decompressing, the X component is guaranteed to be 0xFF, which can be
  215. * interpreted as an opaque alpha channel.
  216. */
  217. TJPF_ABGR,
  218. /**
  219. * ARGB pixel format. This is the same as @ref TJPF_XRGB, except that when
  220. * decompressing, the X component is guaranteed to be 0xFF, which can be
  221. * interpreted as an opaque alpha channel.
  222. */
  223. TJPF_ARGB,
  224. /**
  225. * CMYK pixel format. Unlike RGB, which is an additive color model used
  226. * primarily for display, CMYK (Cyan/Magenta/Yellow/Key) is a subtractive
  227. * color model used primarily for printing. In the CMYK color model, the
  228. * value of each color component typically corresponds to an amount of cyan,
  229. * magenta, yellow, or black ink that is applied to a white background. In
  230. * order to convert between CMYK and RGB, it is necessary to use a color
  231. * management system (CMS.) A CMS will attempt to map colors within the
  232. * printer's gamut to perceptually similar colors in the display's gamut and
  233. * vice versa, but the mapping is typically not 1:1 or reversible, nor can it
  234. * be defined with a simple formula. Thus, such a conversion is out of scope
  235. * for a codec library. However, the TurboJPEG API allows for compressing
  236. * CMYK pixels into a YCCK JPEG image (see #TJCS_YCCK) and decompressing YCCK
  237. * JPEG images into CMYK pixels.
  238. */
  239. TJPF_CMYK,
  240. /**
  241. * Unknown pixel format. Currently this is only used by #tjLoadImage().
  242. */
  243. TJPF_UNKNOWN = -1
  244. };
  245. /**
  246. * Red offset (in bytes) for a given pixel format. This specifies the number
  247. * of bytes that the red component is offset from the start of the pixel. For
  248. * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
  249. * then the red component will be <tt>pixel[tjRedOffset[TJ_BGRX]]</tt>. This
  250. * will be -1 if the pixel format does not have a red component.
  251. */
  252. static const int tjRedOffset[TJ_NUMPF] = {
  253. 0, 2, 0, 2, 3, 1, -1, 0, 2, 3, 1, -1
  254. };
  255. /**
  256. * Green offset (in bytes) for a given pixel format. This specifies the number
  257. * of bytes that the green component is offset from the start of the pixel.
  258. * For instance, if a pixel of format TJ_BGRX is stored in
  259. * <tt>char pixel[]</tt>, then the green component will be
  260. * <tt>pixel[tjGreenOffset[TJ_BGRX]]</tt>. This will be -1 if the pixel format
  261. * does not have a green component.
  262. */
  263. static const int tjGreenOffset[TJ_NUMPF] = {
  264. 1, 1, 1, 1, 2, 2, -1, 1, 1, 2, 2, -1
  265. };
  266. /**
  267. * Blue offset (in bytes) for a given pixel format. This specifies the number
  268. * of bytes that the Blue component is offset from the start of the pixel. For
  269. * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
  270. * then the blue component will be <tt>pixel[tjBlueOffset[TJ_BGRX]]</tt>. This
  271. * will be -1 if the pixel format does not have a blue component.
  272. */
  273. static const int tjBlueOffset[TJ_NUMPF] = {
  274. 2, 0, 2, 0, 1, 3, -1, 2, 0, 1, 3, -1
  275. };
  276. /**
  277. * Alpha offset (in bytes) for a given pixel format. This specifies the number
  278. * of bytes that the Alpha component is offset from the start of the pixel.
  279. * For instance, if a pixel of format TJ_BGRA is stored in
  280. * <tt>char pixel[]</tt>, then the alpha component will be
  281. * <tt>pixel[tjAlphaOffset[TJ_BGRA]]</tt>. This will be -1 if the pixel format
  282. * does not have an alpha component.
  283. */
  284. static const int tjAlphaOffset[TJ_NUMPF] = {
  285. -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
  286. };
  287. /**
  288. * Pixel size (in bytes) for a given pixel format
  289. */
  290. static const int tjPixelSize[TJ_NUMPF] = {
  291. 3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
  292. };
  293. /**
  294. * The number of JPEG colorspaces
  295. */
  296. #define TJ_NUMCS 5
  297. /**
  298. * JPEG colorspaces
  299. */
  300. enum TJCS {
  301. /**
  302. * RGB colorspace. When compressing the JPEG image, the R, G, and B
  303. * components in the source image are reordered into image planes, but no
  304. * colorspace conversion or subsampling is performed. RGB JPEG images can be
  305. * decompressed to any of the extended RGB pixel formats or grayscale, but
  306. * they cannot be decompressed to YUV images.
  307. */
  308. TJCS_RGB = 0,
  309. /**
  310. * YCbCr colorspace. YCbCr is not an absolute colorspace but rather a
  311. * mathematical transformation of RGB designed solely for storage and
  312. * transmission. YCbCr images must be converted to RGB before they can
  313. * actually be displayed. In the YCbCr colorspace, the Y (luminance)
  314. * component represents the black & white portion of the original image, and
  315. * the Cb and Cr (chrominance) components represent the color portion of the
  316. * original image. Originally, the analog equivalent of this transformation
  317. * allowed the same signal to drive both black & white and color televisions,
  318. * but JPEG images use YCbCr primarily because it allows the color data to be
  319. * optionally subsampled for the purposes of reducing bandwidth or disk
  320. * space. YCbCr is the most common JPEG colorspace, and YCbCr JPEG images
  321. * can be compressed from and decompressed to any of the extended RGB pixel
  322. * formats or grayscale, or they can be decompressed to YUV planar images.
  323. */
  324. TJCS_YCbCr,
  325. /**
  326. * Grayscale colorspace. The JPEG image retains only the luminance data (Y
  327. * component), and any color data from the source image is discarded.
  328. * Grayscale JPEG images can be compressed from and decompressed to any of
  329. * the extended RGB pixel formats or grayscale, or they can be decompressed
  330. * to YUV planar images.
  331. */
  332. TJCS_GRAY,
  333. /**
  334. * CMYK colorspace. When compressing the JPEG image, the C, M, Y, and K
  335. * components in the source image are reordered into image planes, but no
  336. * colorspace conversion or subsampling is performed. CMYK JPEG images can
  337. * only be decompressed to CMYK pixels.
  338. */
  339. TJCS_CMYK,
  340. /**
  341. * YCCK colorspace. YCCK (AKA "YCbCrK") is not an absolute colorspace but
  342. * rather a mathematical transformation of CMYK designed solely for storage
  343. * and transmission. It is to CMYK as YCbCr is to RGB. CMYK pixels can be
  344. * reversibly transformed into YCCK, and as with YCbCr, the chrominance
  345. * components in the YCCK pixels can be subsampled without incurring major
  346. * perceptual loss. YCCK JPEG images can only be compressed from and
  347. * decompressed to CMYK pixels.
  348. */
  349. TJCS_YCCK
  350. };
  351. /**
  352. * The uncompressed source/destination image is stored in bottom-up (Windows,
  353. * OpenGL) order, not top-down (X11) order.
  354. */
  355. #define TJFLAG_BOTTOMUP 2
  356. /**
  357. * When decompressing an image that was compressed using chrominance
  358. * subsampling, use the fastest chrominance upsampling algorithm available in
  359. * the underlying codec. The default is to use smooth upsampling, which
  360. * creates a smooth transition between neighboring chrominance components in
  361. * order to reduce upsampling artifacts in the decompressed image.
  362. */
  363. #define TJFLAG_FASTUPSAMPLE 256
  364. /**
  365. * Disable buffer (re)allocation. If passed to one of the JPEG compression or
  366. * transform functions, this flag will cause those functions to generate an
  367. * error if the JPEG image buffer is invalid or too small rather than
  368. * attempting to allocate or reallocate that buffer. This reproduces the
  369. * behavior of earlier versions of TurboJPEG.
  370. */
  371. #define TJFLAG_NOREALLOC 1024
  372. /**
  373. * Use the fastest DCT/IDCT algorithm available in the underlying codec. The
  374. * default if this flag is not specified is implementation-specific. For
  375. * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
  376. * algorithm by default when compressing, because this has been shown to have
  377. * only a very slight effect on accuracy, but it uses the accurate algorithm
  378. * when decompressing, because this has been shown to have a larger effect.
  379. */
  380. #define TJFLAG_FASTDCT 2048
  381. /**
  382. * Use the most accurate DCT/IDCT algorithm available in the underlying codec.
  383. * The default if this flag is not specified is implementation-specific. For
  384. * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
  385. * algorithm by default when compressing, because this has been shown to have
  386. * only a very slight effect on accuracy, but it uses the accurate algorithm
  387. * when decompressing, because this has been shown to have a larger effect.
  388. */
  389. #define TJFLAG_ACCURATEDCT 4096
  390. /**
  391. * Immediately discontinue the current compression/decompression/transform
  392. * operation if the underlying codec throws a warning (non-fatal error). The
  393. * default behavior is to allow the operation to complete unless a fatal error
  394. * is encountered.
  395. */
  396. #define TJFLAG_STOPONWARNING 8192
  397. /**
  398. * Use progressive entropy coding in JPEG images generated by the compression
  399. * and transform functions. Progressive entropy coding will generally improve
  400. * compression relative to baseline entropy coding (the default), but it will
  401. * reduce compression and decompression performance considerably.
  402. */
  403. #define TJFLAG_PROGRESSIVE 16384
  404. /**
  405. * Limit the number of progressive JPEG scans that the decompression and
  406. * transform functions will process. If a progressive JPEG image contains an
  407. * unreasonably large number of scans, then this flag will cause the
  408. * decompression and transform functions to return an error. The primary
  409. * purpose of this is to allow security-critical applications to guard against
  410. * an exploit of the progressive JPEG format described in
  411. * <a href="https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf" target="_blank">this report</a>.
  412. */
  413. #define TJFLAG_LIMITSCANS 32768
  414. /**
  415. * The number of error codes
  416. */
  417. #define TJ_NUMERR 2
  418. /**
  419. * Error codes
  420. */
  421. enum TJERR {
  422. /**
  423. * The error was non-fatal and recoverable, but the image may still be
  424. * corrupt.
  425. */
  426. TJERR_WARNING = 0,
  427. /**
  428. * The error was fatal and non-recoverable.
  429. */
  430. TJERR_FATAL
  431. };
  432. /**
  433. * The number of transform operations
  434. */
  435. #define TJ_NUMXOP 8
  436. /**
  437. * Transform operations for #tjTransform()
  438. */
  439. enum TJXOP {
  440. /**
  441. * Do not transform the position of the image pixels
  442. */
  443. TJXOP_NONE = 0,
  444. /**
  445. * Flip (mirror) image horizontally. This transform is imperfect if there
  446. * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
  447. */
  448. TJXOP_HFLIP,
  449. /**
  450. * Flip (mirror) image vertically. This transform is imperfect if there are
  451. * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
  452. */
  453. TJXOP_VFLIP,
  454. /**
  455. * Transpose image (flip/mirror along upper left to lower right axis.) This
  456. * transform is always perfect.
  457. */
  458. TJXOP_TRANSPOSE,
  459. /**
  460. * Transverse transpose image (flip/mirror along upper right to lower left
  461. * axis.) This transform is imperfect if there are any partial MCU blocks in
  462. * the image (see #TJXOPT_PERFECT.)
  463. */
  464. TJXOP_TRANSVERSE,
  465. /**
  466. * Rotate image clockwise by 90 degrees. This transform is imperfect if
  467. * there are any partial MCU blocks on the bottom edge (see
  468. * #TJXOPT_PERFECT.)
  469. */
  470. TJXOP_ROT90,
  471. /**
  472. * Rotate image 180 degrees. This transform is imperfect if there are any
  473. * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
  474. */
  475. TJXOP_ROT180,
  476. /**
  477. * Rotate image counter-clockwise by 90 degrees. This transform is imperfect
  478. * if there are any partial MCU blocks on the right edge (see
  479. * #TJXOPT_PERFECT.)
  480. */
  481. TJXOP_ROT270
  482. };
  483. /**
  484. * This option will cause #tjTransform() to return an error if the transform is
  485. * not perfect. Lossless transforms operate on MCU blocks, whose size depends
  486. * on the level of chrominance subsampling used (see #tjMCUWidth
  487. * and #tjMCUHeight.) If the image's width or height is not evenly divisible
  488. * by the MCU block size, then there will be partial MCU blocks on the right
  489. * and/or bottom edges. It is not possible to move these partial MCU blocks to
  490. * the top or left of the image, so any transform that would require that is
  491. * "imperfect." If this option is not specified, then any partial MCU blocks
  492. * that cannot be transformed will be left in place, which will create
  493. * odd-looking strips on the right or bottom edge of the image.
  494. */
  495. #define TJXOPT_PERFECT 1
  496. /**
  497. * This option will cause #tjTransform() to discard any partial MCU blocks that
  498. * cannot be transformed.
  499. */
  500. #define TJXOPT_TRIM 2
  501. /**
  502. * This option will enable lossless cropping. See #tjTransform() for more
  503. * information.
  504. */
  505. #define TJXOPT_CROP 4
  506. /**
  507. * This option will discard the color data in the input image and produce
  508. * a grayscale output image.
  509. */
  510. #define TJXOPT_GRAY 8
  511. /**
  512. * This option will prevent #tjTransform() from outputting a JPEG image for
  513. * this particular transform (this can be used in conjunction with a custom
  514. * filter to capture the transformed DCT coefficients without transcoding
  515. * them.)
  516. */
  517. #define TJXOPT_NOOUTPUT 16
  518. /**
  519. * This option will enable progressive entropy coding in the output image
  520. * generated by this particular transform. Progressive entropy coding will
  521. * generally improve compression relative to baseline entropy coding (the
  522. * default), but it will reduce compression and decompression performance
  523. * considerably.
  524. */
  525. #define TJXOPT_PROGRESSIVE 32
  526. /**
  527. * This option will prevent #tjTransform() from copying any extra markers
  528. * (including EXIF and ICC profile data) from the source image to the output
  529. * image.
  530. */
  531. #define TJXOPT_COPYNONE 64
  532. /**
  533. * Scaling factor
  534. */
  535. typedef struct {
  536. /**
  537. * Numerator
  538. */
  539. int num;
  540. /**
  541. * Denominator
  542. */
  543. int denom;
  544. } tjscalingfactor;
  545. /**
  546. * Cropping region
  547. */
  548. typedef struct {
  549. /**
  550. * The left boundary of the cropping region. This must be evenly divisible
  551. * by the MCU block width (see #tjMCUWidth.)
  552. */
  553. int x;
  554. /**
  555. * The upper boundary of the cropping region. This must be evenly divisible
  556. * by the MCU block height (see #tjMCUHeight.)
  557. */
  558. int y;
  559. /**
  560. * The width of the cropping region. Setting this to 0 is the equivalent of
  561. * setting it to the width of the source JPEG image - x.
  562. */
  563. int w;
  564. /**
  565. * The height of the cropping region. Setting this to 0 is the equivalent of
  566. * setting it to the height of the source JPEG image - y.
  567. */
  568. int h;
  569. } tjregion;
  570. /**
  571. * Lossless transform
  572. */
  573. typedef struct tjtransform {
  574. /**
  575. * Cropping region
  576. */
  577. tjregion r;
  578. /**
  579. * One of the @ref TJXOP "transform operations"
  580. */
  581. int op;
  582. /**
  583. * The bitwise OR of one of more of the @ref TJXOPT_CROP "transform options"
  584. */
  585. int options;
  586. /**
  587. * Arbitrary data that can be accessed within the body of the callback
  588. * function
  589. */
  590. void *data;
  591. /**
  592. * A callback function that can be used to modify the DCT coefficients
  593. * after they are losslessly transformed but before they are transcoded to a
  594. * new JPEG image. This allows for custom filters or other transformations
  595. * to be applied in the frequency domain.
  596. *
  597. * @param coeffs pointer to an array of transformed DCT coefficients. (NOTE:
  598. * this pointer is not guaranteed to be valid once the callback returns, so
  599. * applications wishing to hand off the DCT coefficients to another function
  600. * or library should make a copy of them within the body of the callback.)
  601. *
  602. * @param arrayRegion #tjregion structure containing the width and height of
  603. * the array pointed to by <tt>coeffs</tt> as well as its offset relative to
  604. * the component plane. TurboJPEG implementations may choose to split each
  605. * component plane into multiple DCT coefficient arrays and call the callback
  606. * function once for each array.
  607. *
  608. * @param planeRegion #tjregion structure containing the width and height of
  609. * the component plane to which <tt>coeffs</tt> belongs
  610. *
  611. * @param componentID ID number of the component plane to which
  612. * <tt>coeffs</tt> belongs (Y, Cb, and Cr have, respectively, ID's of 0, 1,
  613. * and 2 in typical JPEG images.)
  614. *
  615. * @param transformID ID number of the transformed image to which
  616. * <tt>coeffs</tt> belongs. This is the same as the index of the transform
  617. * in the <tt>transforms</tt> array that was passed to #tjTransform().
  618. *
  619. * @param transform a pointer to a #tjtransform structure that specifies the
  620. * parameters and/or cropping region for this transform
  621. *
  622. * @return 0 if the callback was successful, or -1 if an error occurred.
  623. */
  624. int (*customFilter) (short *coeffs, tjregion arrayRegion,
  625. tjregion planeRegion, int componentIndex,
  626. int transformIndex, struct tjtransform *transform);
  627. } tjtransform;
  628. /**
  629. * TurboJPEG instance handle
  630. */
  631. typedef void *tjhandle;
  632. /**
  633. * Pad the given width to the nearest 32-bit boundary
  634. */
  635. #define TJPAD(width) (((width) + 3) & (~3))
  636. /**
  637. * Compute the scaled value of <tt>dimension</tt> using the given scaling
  638. * factor. This macro performs the integer equivalent of <tt>ceil(dimension *
  639. * scalingFactor)</tt>.
  640. */
  641. #define TJSCALED(dimension, scalingFactor) \
  642. (((dimension) * scalingFactor.num + scalingFactor.denom - 1) / \
  643. scalingFactor.denom)
  644. #ifdef __cplusplus
  645. extern "C" {
  646. #endif
  647. /**
  648. * Create a TurboJPEG compressor instance.
  649. *
  650. * @return a handle to the newly-created instance, or NULL if an error
  651. * occurred (see #tjGetErrorStr2().)
  652. */
  653. DLLEXPORT tjhandle tjInitCompress(void);
  654. /**
  655. * Compress an RGB, grayscale, or CMYK image into a JPEG image.
  656. *
  657. * @param handle a handle to a TurboJPEG compressor or transformer instance
  658. *
  659. * @param srcBuf pointer to an image buffer containing RGB, grayscale, or
  660. * CMYK pixels to be compressed
  661. *
  662. * @param width width (in pixels) of the source image
  663. *
  664. * @param pitch bytes per line in the source image. Normally, this should be
  665. * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, or
  666. * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of the image
  667. * is padded to the nearest 32-bit boundary, as is the case for Windows
  668. * bitmaps. You can also be clever and use this parameter to skip lines, etc.
  669. * Setting this parameter to 0 is the equivalent of setting it to
  670. * <tt>width * #tjPixelSize[pixelFormat]</tt>.
  671. *
  672. * @param height height (in pixels) of the source image
  673. *
  674. * @param pixelFormat pixel format of the source image (see @ref TJPF
  675. * "Pixel formats".)
  676. *
  677. * @param jpegBuf address of a pointer to an image buffer that will receive the
  678. * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer
  679. * to accommodate the size of the JPEG image. Thus, you can choose to:
  680. * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
  681. * let TurboJPEG grow the buffer as needed,
  682. * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the buffer
  683. * for you, or
  684. * -# pre-allocate the buffer to a "worst case" size determined by calling
  685. * #tjBufSize(). This should ensure that the buffer never has to be
  686. * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.)
  687. * .
  688. * If you choose option 1, <tt>*jpegSize</tt> should be set to the size of your
  689. * pre-allocated buffer. In any case, unless you have set #TJFLAG_NOREALLOC,
  690. * you should always check <tt>*jpegBuf</tt> upon return from this function, as
  691. * it may have changed.
  692. *
  693. * @param jpegSize pointer to an unsigned long variable that holds the size of
  694. * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a pre-allocated
  695. * buffer, then <tt>*jpegSize</tt> should be set to the size of the buffer.
  696. * Upon return, <tt>*jpegSize</tt> will contain the size of the JPEG image (in
  697. * bytes.) If <tt>*jpegBuf</tt> points to a JPEG image buffer that is being
  698. * reused from a previous call to one of the JPEG compression functions, then
  699. * <tt>*jpegSize</tt> is ignored.
  700. *
  701. * @param jpegSubsamp the level of chrominance subsampling to be used when
  702. * generating the JPEG image (see @ref TJSAMP
  703. * "Chrominance subsampling options".)
  704. *
  705. * @param jpegQual the image quality of the generated JPEG image (1 = worst,
  706. * 100 = best)
  707. *
  708. * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
  709. * "flags"
  710. *
  711. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  712. * and #tjGetErrorCode().)
  713. */
  714. DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,
  715. int width, int pitch, int height, int pixelFormat,
  716. unsigned char **jpegBuf, unsigned long *jpegSize,
  717. int jpegSubsamp, int jpegQual, int flags);
  718. /**
  719. * Compress a YUV planar image into a JPEG image.
  720. *
  721. * @param handle a handle to a TurboJPEG compressor or transformer instance
  722. *
  723. * @param srcBuf pointer to an image buffer containing a YUV planar image to be
  724. * compressed. The size of this buffer should match the value returned by
  725. * #tjBufSizeYUV2() for the given image width, height, padding, and level of
  726. * chrominance subsampling. The Y, U (Cb), and V (Cr) image planes should be
  727. * stored sequentially in the source buffer (refer to @ref YUVnotes
  728. * "YUV Image Format Notes".)
  729. *
  730. * @param width width (in pixels) of the source image. If the width is not an
  731. * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
  732. * buffer copy will be performed within TurboJPEG.
  733. *
  734. * @param pad the line padding used in the source image. For instance, if each
  735. * line in each plane of the YUV image is padded to the nearest multiple of 4
  736. * bytes, then <tt>pad</tt> should be set to 4.
  737. *
  738. * @param height height (in pixels) of the source image. If the height is not
  739. * an even multiple of the MCU block height (see #tjMCUHeight), then an
  740. * intermediate buffer copy will be performed within TurboJPEG.
  741. *
  742. * @param subsamp the level of chrominance subsampling used in the source
  743. * image (see @ref TJSAMP "Chrominance subsampling options".)
  744. *
  745. * @param jpegBuf address of a pointer to an image buffer that will receive the
  746. * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
  747. * accommodate the size of the JPEG image. Thus, you can choose to:
  748. * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
  749. * let TurboJPEG grow the buffer as needed,
  750. * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the buffer
  751. * for you, or
  752. * -# pre-allocate the buffer to a "worst case" size determined by calling
  753. * #tjBufSize(). This should ensure that the buffer never has to be
  754. * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.)
  755. * .
  756. * If you choose option 1, <tt>*jpegSize</tt> should be set to the size of your
  757. * pre-allocated buffer. In any case, unless you have set #TJFLAG_NOREALLOC,
  758. * you should always check <tt>*jpegBuf</tt> upon return from this function, as
  759. * it may have changed.
  760. *
  761. * @param jpegSize pointer to an unsigned long variable that holds the size of
  762. * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a pre-allocated
  763. * buffer, then <tt>*jpegSize</tt> should be set to the size of the buffer.
  764. * Upon return, <tt>*jpegSize</tt> will contain the size of the JPEG image (in
  765. * bytes.) If <tt>*jpegBuf</tt> points to a JPEG image buffer that is being
  766. * reused from a previous call to one of the JPEG compression functions, then
  767. * <tt>*jpegSize</tt> is ignored.
  768. *
  769. * @param jpegQual the image quality of the generated JPEG image (1 = worst,
  770. * 100 = best)
  771. *
  772. * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
  773. * "flags"
  774. *
  775. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  776. * and #tjGetErrorCode().)
  777. */
  778. DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf,
  779. int width, int pad, int height, int subsamp,
  780. unsigned char **jpegBuf,
  781. unsigned long *jpegSize, int jpegQual,
  782. int flags);
  783. /**
  784. * Compress a set of Y, U (Cb), and V (Cr) image planes into a JPEG image.
  785. *
  786. * @param handle a handle to a TurboJPEG compressor or transformer instance
  787. *
  788. * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
  789. * (or just a Y plane, if compressing a grayscale image) that contain a YUV
  790. * image to be compressed. These planes can be contiguous or non-contiguous in
  791. * memory. The size of each plane should match the value returned by
  792. * #tjPlaneSizeYUV() for the given image width, height, strides, and level of
  793. * chrominance subsampling. Refer to @ref YUVnotes "YUV Image Format Notes"
  794. * for more details.
  795. *
  796. * @param width width (in pixels) of the source image. If the width is not an
  797. * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
  798. * buffer copy will be performed within TurboJPEG.
  799. *
  800. * @param strides an array of integers, each specifying the number of bytes per
  801. * line in the corresponding plane of the YUV source image. Setting the stride
  802. * for any plane to 0 is the same as setting it to the plane width (see
  803. * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
  804. * the strides for all planes will be set to their respective plane widths.
  805. * You can adjust the strides in order to specify an arbitrary amount of line
  806. * padding in each plane or to create a JPEG image from a subregion of a larger
  807. * YUV planar image.
  808. *
  809. * @param height height (in pixels) of the source image. If the height is not
  810. * an even multiple of the MCU block height (see #tjMCUHeight), then an
  811. * intermediate buffer copy will be performed within TurboJPEG.
  812. *
  813. * @param subsamp the level of chrominance subsampling used in the source
  814. * image (see @ref TJSAMP "Chrominance subsampling options".)
  815. *
  816. * @param jpegBuf address of a pointer to an image buffer that will receive the
  817. * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
  818. * accommodate the size of the JPEG image. Thus, you can choose to:
  819. * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
  820. * let TurboJPEG grow the buffer as needed,
  821. * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the buffer
  822. * for you, or
  823. * -# pre-allocate the buffer to a "worst case" size determined by calling
  824. * #tjBufSize(). This should ensure that the buffer never has to be
  825. * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.)
  826. * .
  827. * If you choose option 1, <tt>*jpegSize</tt> should be set to the size of your
  828. * pre-allocated buffer. In any case, unless you have set #TJFLAG_NOREALLOC,
  829. * you should always check <tt>*jpegBuf</tt> upon return from this function, as
  830. * it may have changed.
  831. *
  832. * @param jpegSize pointer to an unsigned long variable that holds the size of
  833. * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a pre-allocated
  834. * buffer, then <tt>*jpegSize</tt> should be set to the size of the buffer.
  835. * Upon return, <tt>*jpegSize</tt> will contain the size of the JPEG image (in
  836. * bytes.) If <tt>*jpegBuf</tt> points to a JPEG image buffer that is being
  837. * reused from a previous call to one of the JPEG compression functions, then
  838. * <tt>*jpegSize</tt> is ignored.
  839. *
  840. * @param jpegQual the image quality of the generated JPEG image (1 = worst,
  841. * 100 = best)
  842. *
  843. * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
  844. * "flags"
  845. *
  846. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  847. * and #tjGetErrorCode().)
  848. */
  849. DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
  850. const unsigned char **srcPlanes,
  851. int width, const int *strides,
  852. int height, int subsamp,
  853. unsigned char **jpegBuf,
  854. unsigned long *jpegSize, int jpegQual,
  855. int flags);
  856. /**
  857. * The maximum size of the buffer (in bytes) required to hold a JPEG image with
  858. * the given parameters. The number of bytes returned by this function is
  859. * larger than the size of the uncompressed source image. The reason for this
  860. * is that the JPEG format uses 16-bit coefficients, and it is thus possible
  861. * for a very high-quality JPEG image with very high-frequency content to
  862. * expand rather than compress when converted to the JPEG format. Such images
  863. * represent a very rare corner case, but since there is no way to predict the
  864. * size of a JPEG image prior to compression, the corner case has to be
  865. * handled.
  866. *
  867. * @param width width (in pixels) of the image
  868. *
  869. * @param height height (in pixels) of the image
  870. *
  871. * @param jpegSubsamp the level of chrominance subsampling to be used when
  872. * generating the JPEG image (see @ref TJSAMP
  873. * "Chrominance subsampling options".)
  874. *
  875. * @return the maximum size of the buffer (in bytes) required to hold the
  876. * image, or -1 if the arguments are out of bounds.
  877. */
  878. DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp);
  879. /**
  880. * The size of the buffer (in bytes) required to hold a YUV planar image with
  881. * the given parameters.
  882. *
  883. * @param width width (in pixels) of the image
  884. *
  885. * @param pad the width of each line in each plane of the image is padded to
  886. * the nearest multiple of this number of bytes (must be a power of 2.)
  887. *
  888. * @param height height (in pixels) of the image
  889. *
  890. * @param subsamp level of chrominance subsampling in the image (see
  891. * @ref TJSAMP "Chrominance subsampling options".)
  892. *
  893. * @return the size of the buffer (in bytes) required to hold the image, or
  894. * -1 if the arguments are out of bounds.
  895. */
  896. DLLEXPORT unsigned long tjBufSizeYUV2(int width, int pad, int height,
  897. int subsamp);
  898. /**
  899. * The size of the buffer (in bytes) required to hold a YUV image plane with
  900. * the given parameters.
  901. *
  902. * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
  903. *
  904. * @param width width (in pixels) of the YUV image. NOTE: this is the width of
  905. * the whole image, not the plane width.
  906. *
  907. * @param stride bytes per line in the image plane. Setting this to 0 is the
  908. * equivalent of setting it to the plane width.
  909. *
  910. * @param height height (in pixels) of the YUV image. NOTE: this is the height
  911. * of the whole image, not the plane height.
  912. *
  913. * @param subsamp level of chrominance subsampling in the image (see
  914. * @ref TJSAMP "Chrominance subsampling options".)
  915. *
  916. * @return the size of the buffer (in bytes) required to hold the YUV image
  917. * plane, or -1 if the arguments are out of bounds.
  918. */
  919. DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
  920. int height, int subsamp);
  921. /**
  922. * The plane width of a YUV image plane with the given parameters. Refer to
  923. * @ref YUVnotes "YUV Image Format Notes" for a description of plane width.
  924. *
  925. * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
  926. *
  927. * @param width width (in pixels) of the YUV image
  928. *
  929. * @param subsamp level of chrominance subsampling in the image (see
  930. * @ref TJSAMP "Chrominance subsampling options".)
  931. *
  932. * @return the plane width of a YUV image plane with the given parameters, or
  933. * -1 if the arguments are out of bounds.
  934. */
  935. DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp);
  936. /**
  937. * The plane height of a YUV image plane with the given parameters. Refer to
  938. * @ref YUVnotes "YUV Image Format Notes" for a description of plane height.
  939. *
  940. * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
  941. *
  942. * @param height height (in pixels) of the YUV image
  943. *
  944. * @param subsamp level of chrominance subsampling in the image (see
  945. * @ref TJSAMP "Chrominance subsampling options".)
  946. *
  947. * @return the plane height of a YUV image plane with the given parameters, or
  948. * -1 if the arguments are out of bounds.
  949. */
  950. DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp);
  951. /**
  952. * Encode an RGB or grayscale image into a YUV planar image. This function
  953. * uses the accelerated color conversion routines in the underlying
  954. * codec but does not execute any of the other steps in the JPEG compression
  955. * process.
  956. *
  957. * @param handle a handle to a TurboJPEG compressor or transformer instance
  958. *
  959. * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
  960. * to be encoded
  961. *
  962. * @param width width (in pixels) of the source image
  963. *
  964. * @param pitch bytes per line in the source image. Normally, this should be
  965. * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, or
  966. * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of the image
  967. * is padded to the nearest 32-bit boundary, as is the case for Windows
  968. * bitmaps. You can also be clever and use this parameter to skip lines, etc.
  969. * Setting this parameter to 0 is the equivalent of setting it to
  970. * <tt>width * #tjPixelSize[pixelFormat]</tt>.
  971. *
  972. * @param height height (in pixels) of the source image
  973. *
  974. * @param pixelFormat pixel format of the source image (see @ref TJPF
  975. * "Pixel formats".)
  976. *
  977. * @param dstBuf pointer to an image buffer that will receive the YUV image.
  978. * Use #tjBufSizeYUV2() to determine the appropriate size for this buffer based
  979. * on the image width, height, padding, and level of chrominance subsampling.
  980. * The Y, U (Cb), and V (Cr) image planes will be stored sequentially in the
  981. * buffer (refer to @ref YUVnotes "YUV Image Format Notes".)
  982. *
  983. * @param pad the width of each line in each plane of the YUV image will be
  984. * padded to the nearest multiple of this number of bytes (must be a power of
  985. * 2.) To generate images suitable for X Video, <tt>pad</tt> should be set to
  986. * 4.
  987. *
  988. * @param subsamp the level of chrominance subsampling to be used when
  989. * generating the YUV image (see @ref TJSAMP
  990. * "Chrominance subsampling options".) To generate images suitable for X
  991. * Video, <tt>subsamp</tt> should be set to @ref TJSAMP_420. This produces an
  992. * image compatible with the I420 (AKA "YUV420P") format.
  993. *
  994. * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
  995. * "flags"
  996. *
  997. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  998. * and #tjGetErrorCode().)
  999. */
  1000. DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf,
  1001. int width, int pitch, int height, int pixelFormat,
  1002. unsigned char *dstBuf, int pad, int subsamp,
  1003. int flags);
  1004. /**
  1005. * Encode an RGB or grayscale image into separate Y, U (Cb), and V (Cr) image
  1006. * planes. This function uses the accelerated color conversion routines in the
  1007. * underlying codec but does not execute any of the other steps in the JPEG
  1008. * compression process.
  1009. *
  1010. * @param handle a handle to a TurboJPEG compressor or transformer instance
  1011. *
  1012. * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
  1013. * to be encoded
  1014. *
  1015. * @param width width (in pixels) of the source image
  1016. *
  1017. * @param pitch bytes per line in the source image. Normally, this should be
  1018. * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, or
  1019. * <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of the image
  1020. * is padded to the nearest 32-bit boundary, as is the case for Windows
  1021. * bitmaps. You can also be clever and use this parameter to skip lines, etc.
  1022. * Setting this parameter to 0 is the equivalent of setting it to
  1023. * <tt>width * #tjPixelSize[pixelFormat]</tt>.
  1024. *
  1025. * @param height height (in pixels) of the source image
  1026. *
  1027. * @param pixelFormat pixel format of the source image (see @ref TJPF
  1028. * "Pixel formats".)
  1029. *
  1030. * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
  1031. * (or just a Y plane, if generating a grayscale image) that will receive the
  1032. * encoded image. These planes can be contiguous or non-contiguous in memory.
  1033. * Use #tjPlaneSizeYUV() to determine the appropriate size for each plane based
  1034. * on the image width, height, strides, and level of chrominance subsampling.
  1035. * Refer to @ref YUVnotes "YUV Image Format Notes" for more details.
  1036. *
  1037. * @param strides an array of integers, each specifying the number of bytes per
  1038. * line in the corresponding plane of the output image. Setting the stride for
  1039. * any plane to 0 is the same as setting it to the plane width (see
  1040. * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
  1041. * the strides for all planes will be set to their respective plane widths.
  1042. * You can adjust the strides in order to add an arbitrary amount of line
  1043. * padding to each plane or to encode an RGB or grayscale image into a
  1044. * subregion of a larger YUV planar image.
  1045. *
  1046. * @param subsamp the level of chrominance subsampling to be used when
  1047. * generating the YUV image (see @ref TJSAMP
  1048. * "Chrominance subsampling options".) To generate images suitable for X
  1049. * Video, <tt>subsamp</tt> should be set to @ref TJSAMP_420. This produces an
  1050. * image compatible with the I420 (AKA "YUV420P") format.
  1051. *
  1052. * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
  1053. * "flags"
  1054. *
  1055. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  1056. * and #tjGetErrorCode().)
  1057. */
  1058. DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,
  1059. int width, int pitch, int height,
  1060. int pixelFormat, unsigned char **dstPlanes,
  1061. int *strides, int subsamp, int flags);
  1062. /**
  1063. * Create a TurboJPEG decompressor instance.
  1064. *
  1065. * @return a handle to the newly-created instance, or NULL if an error
  1066. * occurred (see #tjGetErrorStr2().)
  1067. */
  1068. DLLEXPORT tjhandle tjInitDecompress(void);
  1069. /**
  1070. * Retrieve information about a JPEG image without decompressing it, or prime
  1071. * the decompressor with quantization and Huffman tables.
  1072. *
  1073. * @param handle a handle to a TurboJPEG decompressor or transformer instance
  1074. *
  1075. * @param jpegBuf pointer to a buffer containing a JPEG image or an
  1076. * "abbreviated table specification" (AKA "tables-only") datastream. Passing a
  1077. * tables-only datastream to this function primes the decompressor with
  1078. * quantization and Huffman tables that can be used when decompressing
  1079. * subsequent "abbreviated image" datastreams. This is useful, for instance,
  1080. * when decompressing video streams in which all frames share the same
  1081. * quantization and Huffman tables.
  1082. *
  1083. * @param jpegSize size of the JPEG image or tables-only datastream (in bytes)
  1084. *
  1085. * @param width pointer to an integer variable that will receive the width (in
  1086. * pixels) of the JPEG image. If <tt>jpegBuf</tt> points to a tables-only
  1087. * datastream, then <tt>width</tt> is ignored.
  1088. *
  1089. * @param height pointer to an integer variable that will receive the height
  1090. * (in pixels) of the JPEG image. If <tt>jpegBuf</tt> points to a tables-only
  1091. * datastream, then <tt>height</tt> is ignored.
  1092. *
  1093. * @param jpegSubsamp pointer to an integer variable that will receive the
  1094. * level of chrominance subsampling used when the JPEG image was compressed
  1095. * (see @ref TJSAMP "Chrominance subsampling options".) If <tt>jpegBuf</tt>
  1096. * points to a tables-only datastream, then <tt>jpegSubsamp</tt> is ignored.
  1097. *
  1098. * @param jpegColorspace pointer to an integer variable that will receive one
  1099. * of the JPEG colorspace constants, indicating the colorspace of the JPEG
  1100. * image (see @ref TJCS "JPEG colorspaces".) If <tt>jpegBuf</tt>
  1101. * points to a tables-only datastream, then <tt>jpegColorspace</tt> is ignored.
  1102. *
  1103. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  1104. * and #tjGetErrorCode().)
  1105. */
  1106. DLLEXPORT int tjDecompressHeader3(tjhandle handle,
  1107. const unsigned char *jpegBuf,
  1108. unsigned long jpegSize, int *width,
  1109. int *height, int *jpegSubsamp,
  1110. int *jpegColorspace);
  1111. /**
  1112. * Returns a list of fractional scaling factors that the JPEG decompressor in
  1113. * this implementation of TurboJPEG supports.
  1114. *
  1115. * @param numscalingfactors pointer to an integer variable that will receive
  1116. * the number of elements in the list
  1117. *
  1118. * @return a pointer to a list of fractional scaling factors, or NULL if an
  1119. * error is encountered (see #tjGetErrorStr2().)
  1120. */
  1121. DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numscalingfactors);
  1122. /**
  1123. * Decompress a JPEG image to an RGB, grayscale, or CMYK image.
  1124. *
  1125. * @param handle a handle to a TurboJPEG decompressor or transformer instance
  1126. *
  1127. * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
  1128. *
  1129. * @param jpegSize size of the JPEG image (in bytes)
  1130. *
  1131. * @param dstBuf pointer to an image buffer that will receive the decompressed
  1132. * image. This buffer should normally be <tt>pitch * scaledHeight</tt> bytes
  1133. * in size, where <tt>scaledHeight</tt> can be determined by calling
  1134. * #TJSCALED() with the JPEG image height and one of the scaling factors
  1135. * returned by #tjGetScalingFactors(). The <tt>dstBuf</tt> pointer may also be
  1136. * used to decompress into a specific region of a larger buffer.
  1137. *
  1138. * @param width desired width (in pixels) of the destination image. If this is
  1139. * different than the width of the JPEG image being decompressed, then
  1140. * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
  1141. * possible image that will fit within the desired width. If <tt>width</tt> is
  1142. * set to 0, then only the height will be considered when determining the
  1143. * scaled image size.
  1144. *
  1145. * @param pitch bytes per line in the destination image. Normally, this is
  1146. * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed image
  1147. * is unpadded, else <tt>#TJPAD(scaledWidth * #tjPixelSize[pixelFormat])</tt>
  1148. * if each line of the decompressed image is padded to the nearest 32-bit
  1149. * boundary, as is the case for Windows bitmaps. (NOTE: <tt>scaledWidth</tt>
  1150. * can be determined by calling #TJSCALED() with the JPEG image width and one
  1151. * of the scaling factors returned by #tjGetScalingFactors().) You can also be
  1152. * clever and use the pitch parameter to skip lines, etc. Setting this
  1153. * parameter to 0 is the equivalent of setting it to
  1154. * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt>.
  1155. *
  1156. * @param height desired height (in pixels) of the destination image. If this
  1157. * is different than the height of the JPEG image being decompressed, then
  1158. * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
  1159. * possible image that will fit within the desired height. If <tt>height</tt>
  1160. * is set to 0, then only the width will be considered when determining the
  1161. * scaled image size.
  1162. *
  1163. * @param pixelFormat pixel format of the destination image (see @ref
  1164. * TJPF "Pixel formats".)
  1165. *
  1166. * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
  1167. * "flags"
  1168. *
  1169. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  1170. * and #tjGetErrorCode().)
  1171. */
  1172. DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
  1173. unsigned long jpegSize, unsigned char *dstBuf,
  1174. int width, int pitch, int height, int pixelFormat,
  1175. int flags);
  1176. /**
  1177. * Decompress a JPEG image to a YUV planar image. This function performs JPEG
  1178. * decompression but leaves out the color conversion step, so a planar YUV
  1179. * image is generated instead of an RGB image.
  1180. *
  1181. * @param handle a handle to a TurboJPEG decompressor or transformer instance
  1182. *
  1183. * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
  1184. *
  1185. * @param jpegSize size of the JPEG image (in bytes)
  1186. *
  1187. * @param dstBuf pointer to an image buffer that will receive the YUV image.
  1188. * Use #tjBufSizeYUV2() to determine the appropriate size for this buffer based
  1189. * on the image width, height, padding, and level of subsampling. The Y,
  1190. * U (Cb), and V (Cr) image planes will be stored sequentially in the buffer
  1191. * (refer to @ref YUVnotes "YUV Image Format Notes".)
  1192. *
  1193. * @param width desired width (in pixels) of the YUV image. If this is
  1194. * different than the width of the JPEG image being decompressed, then
  1195. * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
  1196. * possible image that will fit within the desired width. If <tt>width</tt> is
  1197. * set to 0, then only the height will be considered when determining the
  1198. * scaled image size. If the scaled width is not an even multiple of the MCU
  1199. * block width (see #tjMCUWidth), then an intermediate buffer copy will be
  1200. * performed within TurboJPEG.
  1201. *
  1202. * @param pad the width of each line in each plane of the YUV image will be
  1203. * padded to the nearest multiple of this number of bytes (must be a power of
  1204. * 2.) To generate images suitable for X Video, <tt>pad</tt> should be set to
  1205. * 4.
  1206. *
  1207. * @param height desired height (in pixels) of the YUV image. If this is
  1208. * different than the height of the JPEG image being decompressed, then
  1209. * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
  1210. * possible image that will fit within the desired height. If <tt>height</tt>
  1211. * is set to 0, then only the width will be considered when determining the
  1212. * scaled image size. If the scaled height is not an even multiple of the MCU
  1213. * block height (see #tjMCUHeight), then an intermediate buffer copy will be
  1214. * performed within TurboJPEG.
  1215. *
  1216. * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
  1217. * "flags"
  1218. *
  1219. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  1220. * and #tjGetErrorCode().)
  1221. */
  1222. DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
  1223. unsigned long jpegSize, unsigned char *dstBuf,
  1224. int width, int pad, int height, int flags);
  1225. /**
  1226. * Decompress a JPEG image into separate Y, U (Cb), and V (Cr) image
  1227. * planes. This function performs JPEG decompression but leaves out the color
  1228. * conversion step, so a planar YUV image is generated instead of an RGB image.
  1229. *
  1230. * @param handle a handle to a TurboJPEG decompressor or transformer instance
  1231. *
  1232. * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
  1233. *
  1234. * @param jpegSize size of the JPEG image (in bytes)
  1235. *
  1236. * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
  1237. * (or just a Y plane, if decompressing a grayscale image) that will receive
  1238. * the YUV image. These planes can be contiguous or non-contiguous in memory.
  1239. * Use #tjPlaneSizeYUV() to determine the appropriate size for each plane based
  1240. * on the scaled image width, scaled image height, strides, and level of
  1241. * chrominance subsampling. Refer to @ref YUVnotes "YUV Image Format Notes"
  1242. * for more details.
  1243. *
  1244. * @param width desired width (in pixels) of the YUV image. If this is
  1245. * different than the width of the JPEG image being decompressed, then
  1246. * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
  1247. * possible image that will fit within the desired width. If <tt>width</tt> is
  1248. * set to 0, then only the height will be considered when determining the
  1249. * scaled image size. If the scaled width is not an even multiple of the MCU
  1250. * block width (see #tjMCUWidth), then an intermediate buffer copy will be
  1251. * performed within TurboJPEG.
  1252. *
  1253. * @param strides an array of integers, each specifying the number of bytes per
  1254. * line in the corresponding plane of the output image. Setting the stride for
  1255. * any plane to 0 is the same as setting it to the scaled plane width (see
  1256. * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
  1257. * the strides for all planes will be set to their respective scaled plane
  1258. * widths. You can adjust the strides in order to add an arbitrary amount of
  1259. * line padding to each plane or to decompress the JPEG image into a subregion
  1260. * of a larger YUV planar image.
  1261. *
  1262. * @param height desired height (in pixels) of the YUV image. If this is
  1263. * different than the height of the JPEG image being decompressed, then
  1264. * TurboJPEG will use scaling in the JPEG decompressor to generate the largest
  1265. * possible image that will fit within the desired height. If <tt>height</tt>
  1266. * is set to 0, then only the width will be considered when determining the
  1267. * scaled image size. If the scaled height is not an even multiple of the MCU
  1268. * block height (see #tjMCUHeight), then an intermediate buffer copy will be
  1269. * performed within TurboJPEG.
  1270. *
  1271. * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
  1272. * "flags"
  1273. *
  1274. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  1275. * and #tjGetErrorCode().)
  1276. */
  1277. DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
  1278. const unsigned char *jpegBuf,
  1279. unsigned long jpegSize,
  1280. unsigned char **dstPlanes, int width,
  1281. int *strides, int height, int flags);
  1282. /**
  1283. * Decode a YUV planar image into an RGB or grayscale image. This function
  1284. * uses the accelerated color conversion routines in the underlying
  1285. * codec but does not execute any of the other steps in the JPEG decompression
  1286. * process.
  1287. *
  1288. * @param handle a handle to a TurboJPEG decompressor or transformer instance
  1289. *
  1290. * @param srcBuf pointer to an image buffer containing a YUV planar image to be
  1291. * decoded. The size of this buffer should match the value returned by
  1292. * #tjBufSizeYUV2() for the given image width, height, padding, and level of
  1293. * chrominance subsampling. The Y, U (Cb), and V (Cr) image planes should be
  1294. * stored sequentially in the source buffer (refer to @ref YUVnotes
  1295. * "YUV Image Format Notes".)
  1296. *
  1297. * @param pad Use this parameter to specify that the width of each line in each
  1298. * plane of the YUV source image is padded to the nearest multiple of this
  1299. * number of bytes (must be a power of 2.)
  1300. *
  1301. * @param subsamp the level of chrominance subsampling used in the YUV source
  1302. * image (see @ref TJSAMP "Chrominance subsampling options".)
  1303. *
  1304. * @param dstBuf pointer to an image buffer that will receive the decoded
  1305. * image. This buffer should normally be <tt>pitch * height</tt> bytes in
  1306. * size, but the <tt>dstBuf</tt> pointer can also be used to decode into a
  1307. * specific region of a larger buffer.
  1308. *
  1309. * @param width width (in pixels) of the source and destination images
  1310. *
  1311. * @param pitch bytes per line in the destination image. Normally, this should
  1312. * be <tt>width * #tjPixelSize[pixelFormat]</tt> if the destination image is
  1313. * unpadded, or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line
  1314. * of the destination image should be padded to the nearest 32-bit boundary, as
  1315. * is the case for Windows bitmaps. You can also be clever and use the pitch
  1316. * parameter to skip lines, etc. Setting this parameter to 0 is the equivalent
  1317. * of setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
  1318. *
  1319. * @param height height (in pixels) of the source and destination images
  1320. *
  1321. * @param pixelFormat pixel format of the destination image (see @ref TJPF
  1322. * "Pixel formats".)
  1323. *
  1324. * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
  1325. * "flags"
  1326. *
  1327. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  1328. * and #tjGetErrorCode().)
  1329. */
  1330. DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
  1331. int pad, int subsamp, unsigned char *dstBuf,
  1332. int width, int pitch, int height, int pixelFormat,
  1333. int flags);
  1334. /**
  1335. * Decode a set of Y, U (Cb), and V (Cr) image planes into an RGB or grayscale
  1336. * image. This function uses the accelerated color conversion routines in the
  1337. * underlying codec but does not execute any of the other steps in the JPEG
  1338. * decompression process.
  1339. *
  1340. * @param handle a handle to a TurboJPEG decompressor or transformer instance
  1341. *
  1342. * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
  1343. * (or just a Y plane, if decoding a grayscale image) that contain a YUV image
  1344. * to be decoded. These planes can be contiguous or non-contiguous in memory.
  1345. * The size of each plane should match the value returned by #tjPlaneSizeYUV()
  1346. * for the given image width, height, strides, and level of chrominance
  1347. * subsampling. Refer to @ref YUVnotes "YUV Image Format Notes" for more
  1348. * details.
  1349. *
  1350. * @param strides an array of integers, each specifying the number of bytes per
  1351. * line in the corresponding plane of the YUV source image. Setting the stride
  1352. * for any plane to 0 is the same as setting it to the plane width (see
  1353. * @ref YUVnotes "YUV Image Format Notes".) If <tt>strides</tt> is NULL, then
  1354. * the strides for all planes will be set to their respective plane widths.
  1355. * You can adjust the strides in order to specify an arbitrary amount of line
  1356. * padding in each plane or to decode a subregion of a larger YUV planar image.
  1357. *
  1358. * @param subsamp the level of chrominance subsampling used in the YUV source
  1359. * image (see @ref TJSAMP "Chrominance subsampling options".)
  1360. *
  1361. * @param dstBuf pointer to an image buffer that will receive the decoded
  1362. * image. This buffer should normally be <tt>pitch * height</tt> bytes in
  1363. * size, but the <tt>dstBuf</tt> pointer can also be used to decode into a
  1364. * specific region of a larger buffer.
  1365. *
  1366. * @param width width (in pixels) of the source and destination images
  1367. *
  1368. * @param pitch bytes per line in the destination image. Normally, this should
  1369. * be <tt>width * #tjPixelSize[pixelFormat]</tt> if the destination image is
  1370. * unpadded, or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line
  1371. * of the destination image should be padded to the nearest 32-bit boundary, as
  1372. * is the case for Windows bitmaps. You can also be clever and use the pitch
  1373. * parameter to skip lines, etc. Setting this parameter to 0 is the equivalent
  1374. * of setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
  1375. *
  1376. * @param height height (in pixels) of the source and destination images
  1377. *
  1378. * @param pixelFormat pixel format of the destination image (see @ref TJPF
  1379. * "Pixel formats".)
  1380. *
  1381. * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
  1382. * "flags"
  1383. *
  1384. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  1385. * and #tjGetErrorCode().)
  1386. */
  1387. DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
  1388. const unsigned char **srcPlanes,
  1389. const int *strides, int subsamp,
  1390. unsigned char *dstBuf, int width, int pitch,
  1391. int height, int pixelFormat, int flags);
  1392. /**
  1393. * Create a new TurboJPEG transformer instance.
  1394. *
  1395. * @return a handle to the newly-created instance, or NULL if an error
  1396. * occurred (see #tjGetErrorStr2().)
  1397. */
  1398. DLLEXPORT tjhandle tjInitTransform(void);
  1399. /**
  1400. * Losslessly transform a JPEG image into another JPEG image. Lossless
  1401. * transforms work by moving the raw DCT coefficients from one JPEG image
  1402. * structure to another without altering the values of the coefficients. While
  1403. * this is typically faster than decompressing the image, transforming it, and
  1404. * re-compressing it, lossless transforms are not free. Each lossless
  1405. * transform requires reading and performing Huffman decoding on all of the
  1406. * coefficients in the source image, regardless of the size of the destination
  1407. * image. Thus, this function provides a means of generating multiple
  1408. * transformed images from the same source or applying multiple
  1409. * transformations simultaneously, in order to eliminate the need to read the
  1410. * source coefficients multiple times.
  1411. *
  1412. * @param handle a handle to a TurboJPEG transformer instance
  1413. *
  1414. * @param jpegBuf pointer to a buffer containing the JPEG source image to
  1415. * transform
  1416. *
  1417. * @param jpegSize size of the JPEG source image (in bytes)
  1418. *
  1419. * @param n the number of transformed JPEG images to generate
  1420. *
  1421. * @param dstBufs pointer to an array of n image buffers. <tt>dstBufs[i]</tt>
  1422. * will receive a JPEG image that has been transformed using the parameters in
  1423. * <tt>transforms[i]</tt>. TurboJPEG has the ability to reallocate the JPEG
  1424. * buffer to accommodate the size of the JPEG image. Thus, you can choose to:
  1425. * -# pre-allocate the JPEG buffer with an arbitrary size using #tjAlloc() and
  1426. * let TurboJPEG grow the buffer as needed,
  1427. * -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the buffer
  1428. * for you, or
  1429. * -# pre-allocate the buffer to a "worst case" size determined by calling
  1430. * #tjBufSize() with the transformed or cropped width and height. Under normal
  1431. * circumstances, this should ensure that the buffer never has to be
  1432. * re-allocated (setting #TJFLAG_NOREALLOC guarantees that it won't be.) Note,
  1433. * however, that there are some rare cases (such as transforming images with a
  1434. * large amount of embedded EXIF or ICC profile data) in which the output image
  1435. * will be larger than the worst-case size, and #TJFLAG_NOREALLOC cannot be
  1436. * used in those cases.
  1437. * .
  1438. * If you choose option 1, <tt>dstSizes[i]</tt> should be set to the size of
  1439. * your pre-allocated buffer. In any case, unless you have set
  1440. * #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt> upon return
  1441. * from this function, as it may have changed.
  1442. *
  1443. * @param dstSizes pointer to an array of n unsigned long variables that will
  1444. * receive the actual sizes (in bytes) of each transformed JPEG image. If
  1445. * <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then
  1446. * <tt>dstSizes[i]</tt> should be set to the size of the buffer. Upon return,
  1447. * <tt>dstSizes[i]</tt> will contain the size of the JPEG image (in bytes.)
  1448. *
  1449. * @param transforms pointer to an array of n #tjtransform structures, each of
  1450. * which specifies the transform parameters and/or cropping region for the
  1451. * corresponding transformed output image.
  1452. *
  1453. * @param flags the bitwise OR of one or more of the @ref TJFLAG_ACCURATEDCT
  1454. * "flags"
  1455. *
  1456. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2()
  1457. * and #tjGetErrorCode().)
  1458. */
  1459. DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
  1460. unsigned long jpegSize, int n,
  1461. unsigned char **dstBufs, unsigned long *dstSizes,
  1462. tjtransform *transforms, int flags);
  1463. /**
  1464. * Destroy a TurboJPEG compressor, decompressor, or transformer instance.
  1465. *
  1466. * @param handle a handle to a TurboJPEG compressor, decompressor or
  1467. * transformer instance
  1468. *
  1469. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2().)
  1470. */
  1471. DLLEXPORT int tjDestroy(tjhandle handle);
  1472. /**
  1473. * Allocate an image buffer for use with TurboJPEG. You should always use
  1474. * this function to allocate the JPEG destination buffer(s) for the compression
  1475. * and transform functions unless you are disabling automatic buffer
  1476. * (re)allocation (by setting #TJFLAG_NOREALLOC.)
  1477. *
  1478. * @param bytes the number of bytes to allocate
  1479. *
  1480. * @return a pointer to a newly-allocated buffer with the specified number of
  1481. * bytes.
  1482. *
  1483. * @sa tjFree()
  1484. */
  1485. DLLEXPORT unsigned char *tjAlloc(int bytes);
  1486. /**
  1487. * Load an uncompressed image from disk into memory.
  1488. *
  1489. * @param filename name of a file containing an uncompressed image in Windows
  1490. * BMP or PBMPLUS (PPM/PGM) format
  1491. *
  1492. * @param width pointer to an integer variable that will receive the width (in
  1493. * pixels) of the uncompressed image
  1494. *
  1495. * @param align row alignment of the image buffer to be returned (must be a
  1496. * power of 2.) For instance, setting this parameter to 4 will cause all rows
  1497. * in the image buffer to be padded to the nearest 32-bit boundary, and setting
  1498. * this parameter to 1 will cause all rows in the image buffer to be unpadded.
  1499. *
  1500. * @param height pointer to an integer variable that will receive the height
  1501. * (in pixels) of the uncompressed image
  1502. *
  1503. * @param pixelFormat pointer to an integer variable that specifies or will
  1504. * receive the pixel format of the uncompressed image buffer. The behavior of
  1505. * #tjLoadImage() will vary depending on the value of <tt>*pixelFormat</tt>
  1506. * passed to the function:
  1507. * - @ref TJPF_UNKNOWN : The uncompressed image buffer returned by the function
  1508. * will use the most optimal pixel format for the file type, and
  1509. * <tt>*pixelFormat</tt> will contain the ID of this pixel format upon
  1510. * successful return from the function.
  1511. * - @ref TJPF_GRAY : Only PGM files and 8-bit BMP files with a grayscale
  1512. * colormap can be loaded.
  1513. * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
  1514. * converted using a quick & dirty algorithm that is suitable only for testing
  1515. * purposes (proper conversion between CMYK and other formats requires a color
  1516. * management system.)
  1517. * - Other @ref TJPF "pixel formats" : The uncompressed image buffer will use
  1518. * the specified pixel format, and pixel format conversion will be performed if
  1519. * necessary.
  1520. *
  1521. * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
  1522. * "flags".
  1523. *
  1524. * @return a pointer to a newly-allocated buffer containing the uncompressed
  1525. * image, converted to the chosen pixel format and with the chosen row
  1526. * alignment, or NULL if an error occurred (see #tjGetErrorStr2().) This
  1527. * buffer should be freed using #tjFree().
  1528. */
  1529. DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
  1530. int align, int *height, int *pixelFormat,
  1531. int flags);
  1532. /**
  1533. * Save an uncompressed image from memory to disk.
  1534. *
  1535. * @param filename name of a file to which to save the uncompressed image.
  1536. * The image will be stored in Windows BMP or PBMPLUS (PPM/PGM) format,
  1537. * depending on the file extension.
  1538. *
  1539. * @param buffer pointer to an image buffer containing RGB, grayscale, or
  1540. * CMYK pixels to be saved
  1541. *
  1542. * @param width width (in pixels) of the uncompressed image
  1543. *
  1544. * @param pitch bytes per line in the image buffer. Setting this parameter to
  1545. * 0 is the equivalent of setting it to
  1546. * <tt>width * #tjPixelSize[pixelFormat]</tt>.
  1547. *
  1548. * @param height height (in pixels) of the uncompressed image
  1549. *
  1550. * @param pixelFormat pixel format of the image buffer (see @ref TJPF
  1551. * "Pixel formats".) If this parameter is set to @ref TJPF_GRAY, then the
  1552. * image will be stored in PGM or 8-bit (indexed color) BMP format. Otherwise,
  1553. * the image will be stored in PPM or 24-bit BMP format. If this parameter
  1554. * is set to @ref TJPF_CMYK, then the CMYK pixels will be converted to RGB
  1555. * using a quick & dirty algorithm that is suitable only for testing (proper
  1556. * conversion between CMYK and other formats requires a color management
  1557. * system.)
  1558. *
  1559. * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
  1560. * "flags".
  1561. *
  1562. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr2().)
  1563. */
  1564. DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
  1565. int width, int pitch, int height, int pixelFormat,
  1566. int flags);
  1567. /**
  1568. * Free an image buffer previously allocated by TurboJPEG. You should always
  1569. * use this function to free JPEG destination buffer(s) that were automatically
  1570. * (re)allocated by the compression and transform functions or that were
  1571. * manually allocated using #tjAlloc().
  1572. *
  1573. * @param buffer address of the buffer to free. If the address is NULL, then
  1574. * this function has no effect.
  1575. *
  1576. * @sa tjAlloc()
  1577. */
  1578. DLLEXPORT void tjFree(unsigned char *buffer);
  1579. /**
  1580. * Returns a descriptive error message explaining why the last command failed.
  1581. *
  1582. * @param handle a handle to a TurboJPEG compressor, decompressor, or
  1583. * transformer instance, or NULL if the error was generated by a global
  1584. * function (but note that retrieving the error message for a global function
  1585. * is thread-safe only on platforms that support thread-local storage.)
  1586. *
  1587. * @return a descriptive error message explaining why the last command failed.
  1588. */
  1589. DLLEXPORT char *tjGetErrorStr2(tjhandle handle);
  1590. /**
  1591. * Returns a code indicating the severity of the last error. See
  1592. * @ref TJERR "Error codes".
  1593. *
  1594. * @param handle a handle to a TurboJPEG compressor, decompressor or
  1595. * transformer instance
  1596. *
  1597. * @return a code indicating the severity of the last error. See
  1598. * @ref TJERR "Error codes".
  1599. */
  1600. DLLEXPORT int tjGetErrorCode(tjhandle handle);
  1601. /* Deprecated functions and macros */
  1602. #define TJFLAG_FORCEMMX 8
  1603. #define TJFLAG_FORCESSE 16
  1604. #define TJFLAG_FORCESSE2 32
  1605. #define TJFLAG_FORCESSE3 128
  1606. /* Backward compatibility functions and macros (nothing to see here) */
  1607. #define NUMSUBOPT TJ_NUMSAMP
  1608. #define TJ_444 TJSAMP_444
  1609. #define TJ_422 TJSAMP_422
  1610. #define TJ_420 TJSAMP_420
  1611. #define TJ_411 TJSAMP_420
  1612. #define TJ_GRAYSCALE TJSAMP_GRAY
  1613. #define TJ_BGR 1
  1614. #define TJ_BOTTOMUP TJFLAG_BOTTOMUP
  1615. #define TJ_FORCEMMX TJFLAG_FORCEMMX
  1616. #define TJ_FORCESSE TJFLAG_FORCESSE
  1617. #define TJ_FORCESSE2 TJFLAG_FORCESSE2
  1618. #define TJ_ALPHAFIRST 64
  1619. #define TJ_FORCESSE3 TJFLAG_FORCESSE3
  1620. #define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE
  1621. #define TJ_YUV 512
  1622. DLLEXPORT unsigned long TJBUFSIZE(int width, int height);
  1623. DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int jpegSubsamp);
  1624. DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp);
  1625. DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
  1626. int pitch, int height, int pixelSize,
  1627. unsigned char *dstBuf, unsigned long *compressedSize,
  1628. int jpegSubsamp, int jpegQual, int flags);
  1629. DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
  1630. int pitch, int height, int pixelSize,
  1631. unsigned char *dstBuf, int subsamp, int flags);
  1632. DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
  1633. int pitch, int height, int pixelFormat,
  1634. unsigned char *dstBuf, int subsamp, int flags);
  1635. DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
  1636. unsigned long jpegSize, int *width,
  1637. int *height);
  1638. DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
  1639. unsigned long jpegSize, int *width,
  1640. int *height, int *jpegSubsamp);
  1641. DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
  1642. unsigned long jpegSize, unsigned char *dstBuf,
  1643. int width, int pitch, int height, int pixelSize,
  1644. int flags);
  1645. DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
  1646. unsigned long jpegSize, unsigned char *dstBuf,
  1647. int flags);
  1648. DLLEXPORT char *tjGetErrorStr(void);
  1649. /**
  1650. * @}
  1651. */
  1652. #ifdef __cplusplus
  1653. }
  1654. #endif
  1655. #endif