turbojpeg.h 72 KB

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