turbojpeg.h 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328
  1. /*
  2. * Copyright (C)2009-2015, 2017, 2020-2023 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. #include <stddef.h>
  32. #if defined(_WIN32) && defined(DLLDEFINE)
  33. #define DLLEXPORT __declspec(dllexport)
  34. #else
  35. #define DLLEXPORT
  36. #endif
  37. #define DLLCALL
  38. /**
  39. * @addtogroup TurboJPEG
  40. * TurboJPEG API. This API provides an interface for generating, decoding, and
  41. * transforming planar YUV and JPEG images in memory.
  42. *
  43. * @anchor YUVnotes
  44. * YUV Image Format Notes
  45. * ----------------------
  46. * Technically, the JPEG format uses the YCbCr colorspace (which is technically
  47. * not a colorspace but a color transform), but per the convention of the
  48. * digital video community, the TurboJPEG API uses "YUV" to refer to an image
  49. * format consisting of Y, Cb, and Cr image planes.
  50. *
  51. * Each plane is simply a 2D array of bytes, each byte representing the value
  52. * of one of the components (Y, Cb, or Cr) at a particular location in the
  53. * image. The width and height of each plane are determined by the image
  54. * width, height, and level of chrominance subsampling. The luminance plane
  55. * width is the image width padded to the nearest multiple of the horizontal
  56. * subsampling factor (1 in the case of 4:4:4, grayscale, 4:4:0, or 4:4:1; 2 in
  57. * the case of 4:2:2 or 4:2:0; 4 in the case of 4:1:1.) Similarly, the
  58. * luminance plane height is the image height padded to the nearest multiple of
  59. * the vertical subsampling factor (1 in the case of 4:4:4, 4:2:2, grayscale,
  60. * or 4:1:1; 2 in the case of 4:2:0 or 4:4:0; 4 in the case of 4:4:1.) This is
  61. * irrespective of any additional padding that may be specified as an argument
  62. * to the various YUV functions. The chrominance plane width is equal to the
  63. * luminance plane width divided by the horizontal subsampling factor, and the
  64. * chrominance plane height is equal to the luminance plane height divided by
  65. * the vertical subsampling factor.
  66. *
  67. * For example, if the source image is 35 x 35 pixels and 4:2:2 subsampling is
  68. * used, then the luminance plane would be 36 x 35 bytes, and each of the
  69. * chrominance planes would be 18 x 35 bytes. If you specify a row alignment
  70. * of 4 bytes on top of this, then the luminance plane would be 36 x 35 bytes,
  71. * and each of the chrominance planes would be 20 x 35 bytes.
  72. *
  73. * @{
  74. */
  75. /**
  76. * The number of initialization options
  77. */
  78. #define TJ_NUMINIT 3
  79. /**
  80. * Initialization options.
  81. */
  82. enum TJINIT {
  83. /**
  84. * Initialize the TurboJPEG instance for compression.
  85. */
  86. TJINIT_COMPRESS,
  87. /**
  88. * Initialize the TurboJPEG instance for decompression.
  89. */
  90. TJINIT_DECOMPRESS,
  91. /**
  92. * Initialize the TurboJPEG instance for lossless transformation (both
  93. * compression and decompression.)
  94. */
  95. TJINIT_TRANSFORM
  96. };
  97. /**
  98. * The number of chrominance subsampling options
  99. */
  100. #define TJ_NUMSAMP 7
  101. /**
  102. * Chrominance subsampling options.
  103. * When pixels are converted from RGB to YCbCr (see #TJCS_YCbCr) or from CMYK
  104. * to YCCK (see #TJCS_YCCK) as part of the JPEG compression process, some of
  105. * the Cb and Cr (chrominance) components can be discarded or averaged together
  106. * to produce a smaller image with little perceptible loss of image clarity.
  107. * (The human eye is more sensitive to small changes in brightness than to
  108. * small changes in color.) This is called "chrominance subsampling".
  109. */
  110. enum TJSAMP {
  111. /**
  112. * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG or
  113. * YUV image will contain one chrominance component for every pixel in the
  114. * source image.
  115. */
  116. TJSAMP_444,
  117. /**
  118. * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
  119. * chrominance component for every 2x1 block of pixels in the source image.
  120. */
  121. TJSAMP_422,
  122. /**
  123. * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
  124. * chrominance component for every 2x2 block of pixels in the source image.
  125. */
  126. TJSAMP_420,
  127. /**
  128. * Grayscale. The JPEG or YUV image will contain no chrominance components.
  129. */
  130. TJSAMP_GRAY,
  131. /**
  132. * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
  133. * chrominance component for every 1x2 block of pixels in the source image.
  134. *
  135. * @note 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
  136. */
  137. TJSAMP_440,
  138. /**
  139. * 4:1:1 chrominance subsampling. The JPEG or YUV image will contain one
  140. * chrominance component for every 4x1 block of pixels in the source image.
  141. * JPEG images compressed with 4:1:1 subsampling will be almost exactly the
  142. * same size as those compressed with 4:2:0 subsampling, and in the
  143. * aggregate, both subsampling methods produce approximately the same
  144. * perceptual quality. However, 4:1:1 is better able to reproduce sharp
  145. * horizontal features.
  146. *
  147. * @note 4:1:1 subsampling is not fully accelerated in libjpeg-turbo.
  148. */
  149. TJSAMP_411,
  150. /**
  151. * 4:4:1 chrominance subsampling. The JPEG or YUV image will contain one
  152. * chrominance component for every 1x4 block of pixels in the source image.
  153. * JPEG images compressed with 4:4:1 subsampling will be almost exactly the
  154. * same size as those compressed with 4:2:0 subsampling, and in the
  155. * aggregate, both subsampling methods produce approximately the same
  156. * perceptual quality. However, 4:4:1 is better able to reproduce sharp
  157. * vertical features.
  158. *
  159. * @note 4:4:1 subsampling is not fully accelerated in libjpeg-turbo.
  160. */
  161. TJSAMP_441,
  162. /**
  163. * Unknown subsampling. The JPEG image uses an unusual type of chrominance
  164. * subsampling. Such images can be decompressed into packed-pixel images,
  165. * but they cannot be
  166. * - decompressed into planar YUV images,
  167. * - losslessly transformed if #TJXOPT_CROP is specified, or
  168. * - partially decompressed using a cropping region.
  169. */
  170. TJSAMP_UNKNOWN = -1
  171. };
  172. /**
  173. * MCU block width (in pixels) for a given level of chrominance subsampling.
  174. * MCU block sizes:
  175. * - 8x8 for no subsampling or grayscale
  176. * - 16x8 for 4:2:2
  177. * - 8x16 for 4:4:0
  178. * - 16x16 for 4:2:0
  179. * - 32x8 for 4:1:1
  180. * - 8x32 for 4:4:1
  181. */
  182. static const int tjMCUWidth[TJ_NUMSAMP] = { 8, 16, 16, 8, 8, 32, 8 };
  183. /**
  184. * MCU block height (in pixels) for a given level of chrominance subsampling.
  185. * MCU block sizes:
  186. * - 8x8 for no subsampling or grayscale
  187. * - 16x8 for 4:2:2
  188. * - 8x16 for 4:4:0
  189. * - 16x16 for 4:2:0
  190. * - 32x8 for 4:1:1
  191. * - 8x32 for 4:4:1
  192. */
  193. static const int tjMCUHeight[TJ_NUMSAMP] = { 8, 8, 16, 8, 16, 8, 32 };
  194. /**
  195. * The number of pixel formats
  196. */
  197. #define TJ_NUMPF 12
  198. /**
  199. * Pixel formats
  200. */
  201. enum TJPF {
  202. /**
  203. * RGB pixel format. The red, green, and blue components in the image are
  204. * stored in 3-sample pixels in the order R, G, B from lowest to highest
  205. * memory address within each pixel.
  206. */
  207. TJPF_RGB,
  208. /**
  209. * BGR pixel format. The red, green, and blue components in the image are
  210. * stored in 3-sample pixels in the order B, G, R from lowest to highest
  211. * memory address within each pixel.
  212. */
  213. TJPF_BGR,
  214. /**
  215. * RGBX pixel format. The red, green, and blue components in the image are
  216. * stored in 4-sample pixels in the order R, G, B from lowest to highest
  217. * memory address within each pixel. The X component is ignored when
  218. * compressing and undefined when decompressing.
  219. */
  220. TJPF_RGBX,
  221. /**
  222. * BGRX pixel format. The red, green, and blue components in the image are
  223. * stored in 4-sample pixels in the order B, G, R from lowest to highest
  224. * memory address within each pixel. The X component is ignored when
  225. * compressing and undefined when decompressing.
  226. */
  227. TJPF_BGRX,
  228. /**
  229. * XBGR pixel format. The red, green, and blue components in the image are
  230. * stored in 4-sample pixels in the order R, G, B from highest to lowest
  231. * memory address within each pixel. The X component is ignored when
  232. * compressing and undefined when decompressing.
  233. */
  234. TJPF_XBGR,
  235. /**
  236. * XRGB pixel format. The red, green, and blue components in the image are
  237. * stored in 4-sample pixels in the order B, G, R from highest to lowest
  238. * memory address within each pixel. The X component is ignored when
  239. * compressing and undefined when decompressing.
  240. */
  241. TJPF_XRGB,
  242. /**
  243. * Grayscale pixel format. Each 1-sample pixel represents a luminance
  244. * (brightness) level from 0 to the maximum sample value (255 for 8-bit
  245. * samples, 4095 for 12-bit samples, and 65535 for 16-bit samples.)
  246. */
  247. TJPF_GRAY,
  248. /**
  249. * RGBA pixel format. This is the same as @ref TJPF_RGBX, except that when
  250. * decompressing, the X component is guaranteed to be equal to the maximum
  251. * sample value, which can be interpreted as an opaque alpha channel.
  252. */
  253. TJPF_RGBA,
  254. /**
  255. * BGRA pixel format. This is the same as @ref TJPF_BGRX, except that when
  256. * decompressing, the X component is guaranteed to be equal to the maximum
  257. * sample value, which can be interpreted as an opaque alpha channel.
  258. */
  259. TJPF_BGRA,
  260. /**
  261. * ABGR pixel format. This is the same as @ref TJPF_XBGR, except that when
  262. * decompressing, the X component is guaranteed to be equal to the maximum
  263. * sample value, which can be interpreted as an opaque alpha channel.
  264. */
  265. TJPF_ABGR,
  266. /**
  267. * ARGB pixel format. This is the same as @ref TJPF_XRGB, except that when
  268. * decompressing, the X component is guaranteed to be equal to the maximum
  269. * sample value, which can be interpreted as an opaque alpha channel.
  270. */
  271. TJPF_ARGB,
  272. /**
  273. * CMYK pixel format. Unlike RGB, which is an additive color model used
  274. * primarily for display, CMYK (Cyan/Magenta/Yellow/Key) is a subtractive
  275. * color model used primarily for printing. In the CMYK color model, the
  276. * value of each color component typically corresponds to an amount of cyan,
  277. * magenta, yellow, or black ink that is applied to a white background. In
  278. * order to convert between CMYK and RGB, it is necessary to use a color
  279. * management system (CMS.) A CMS will attempt to map colors within the
  280. * printer's gamut to perceptually similar colors in the display's gamut and
  281. * vice versa, but the mapping is typically not 1:1 or reversible, nor can it
  282. * be defined with a simple formula. Thus, such a conversion is out of scope
  283. * for a codec library. However, the TurboJPEG API allows for compressing
  284. * packed-pixel CMYK images into YCCK JPEG images (see #TJCS_YCCK) and
  285. * decompressing YCCK JPEG images into packed-pixel CMYK images.
  286. */
  287. TJPF_CMYK,
  288. /**
  289. * Unknown pixel format. Currently this is only used by #tj3LoadImage8(),
  290. * #tj3LoadImage12(), and #tj3LoadImage16().
  291. */
  292. TJPF_UNKNOWN = -1
  293. };
  294. /**
  295. * Red offset (in samples) for a given pixel format. This specifies the number
  296. * of samples that the red component is offset from the start of the pixel.
  297. * For instance, if an 8-bit-per-component pixel of format TJPF_BGRX is stored
  298. * in `unsigned char pixel[]`, then the red component will be
  299. * `pixel[tjRedOffset[TJPF_BGRX]]`. This will be -1 if the pixel format does
  300. * not have a red component.
  301. */
  302. static const int tjRedOffset[TJ_NUMPF] = {
  303. 0, 2, 0, 2, 3, 1, -1, 0, 2, 3, 1, -1
  304. };
  305. /**
  306. * Green offset (in samples) for a given pixel format. This specifies the
  307. * number of samples that the green component is offset from the start of the
  308. * pixel. For instance, if an 8-bit-per-component pixel of format TJPF_BGRX is
  309. * stored in `unsigned char pixel[]`, then the green component will be
  310. * `pixel[tjGreenOffset[TJPF_BGRX]]`. This will be -1 if the pixel format does
  311. * not have a green component.
  312. */
  313. static const int tjGreenOffset[TJ_NUMPF] = {
  314. 1, 1, 1, 1, 2, 2, -1, 1, 1, 2, 2, -1
  315. };
  316. /**
  317. * Blue offset (in samples) for a given pixel format. This specifies the
  318. * number of samples that the blue component is offset from the start of the
  319. * pixel. For instance, if an 8-bit-per-component pixel of format TJPF_BGRX is
  320. * stored in `unsigned char pixel[]`, then the blue component will be
  321. * `pixel[tjBlueOffset[TJPF_BGRX]]`. This will be -1 if the pixel format does
  322. * not have a blue component.
  323. */
  324. static const int tjBlueOffset[TJ_NUMPF] = {
  325. 2, 0, 2, 0, 1, 3, -1, 2, 0, 1, 3, -1
  326. };
  327. /**
  328. * Alpha offset (in samples) for a given pixel format. This specifies the
  329. * number of samples that the alpha component is offset from the start of the
  330. * pixel. For instance, if an 8-bit-per-component pixel of format TJPF_BGRA is
  331. * stored in `unsigned char pixel[]`, then the alpha component will be
  332. * `pixel[tjAlphaOffset[TJPF_BGRA]]`. This will be -1 if the pixel format does
  333. * not have an alpha component.
  334. */
  335. static const int tjAlphaOffset[TJ_NUMPF] = {
  336. -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
  337. };
  338. /**
  339. * Pixel size (in samples) for a given pixel format
  340. */
  341. static const int tjPixelSize[TJ_NUMPF] = {
  342. 3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4
  343. };
  344. /**
  345. * The number of JPEG colorspaces
  346. */
  347. #define TJ_NUMCS 5
  348. /**
  349. * JPEG colorspaces
  350. */
  351. enum TJCS {
  352. /**
  353. * RGB colorspace. When compressing the JPEG image, the R, G, and B
  354. * components in the source image are reordered into image planes, but no
  355. * colorspace conversion or subsampling is performed. RGB JPEG images can be
  356. * compressed from and decompressed to packed-pixel images with any of the
  357. * extended RGB or grayscale pixel formats, but they cannot be compressed
  358. * from or decompressed to planar YUV images.
  359. */
  360. TJCS_RGB,
  361. /**
  362. * YCbCr colorspace. YCbCr is not an absolute colorspace but rather a
  363. * mathematical transformation of RGB designed solely for storage and
  364. * transmission. YCbCr images must be converted to RGB before they can
  365. * actually be displayed. In the YCbCr colorspace, the Y (luminance)
  366. * component represents the black & white portion of the original image, and
  367. * the Cb and Cr (chrominance) components represent the color portion of the
  368. * original image. Originally, the analog equivalent of this transformation
  369. * allowed the same signal to drive both black & white and color televisions,
  370. * but JPEG images use YCbCr primarily because it allows the color data to be
  371. * optionally subsampled for the purposes of reducing network or disk usage.
  372. * YCbCr is the most common JPEG colorspace, and YCbCr JPEG images can be
  373. * compressed from and decompressed to packed-pixel images with any of the
  374. * extended RGB or grayscale pixel formats. YCbCr JPEG images can also be
  375. * compressed from and decompressed to planar YUV images.
  376. */
  377. TJCS_YCbCr,
  378. /**
  379. * Grayscale colorspace. The JPEG image retains only the luminance data (Y
  380. * component), and any color data from the source image is discarded.
  381. * Grayscale JPEG images can be compressed from and decompressed to
  382. * packed-pixel images with any of the extended RGB or grayscale pixel
  383. * formats, or they can be compressed from and decompressed to planar YUV
  384. * images.
  385. */
  386. TJCS_GRAY,
  387. /**
  388. * CMYK colorspace. When compressing the JPEG image, the C, M, Y, and K
  389. * components in the source image are reordered into image planes, but no
  390. * colorspace conversion or subsampling is performed. CMYK JPEG images can
  391. * only be compressed from and decompressed to packed-pixel images with the
  392. * CMYK pixel format.
  393. */
  394. TJCS_CMYK,
  395. /**
  396. * YCCK colorspace. YCCK (AKA "YCbCrK") is not an absolute colorspace but
  397. * rather a mathematical transformation of CMYK designed solely for storage
  398. * and transmission. It is to CMYK as YCbCr is to RGB. CMYK pixels can be
  399. * reversibly transformed into YCCK, and as with YCbCr, the chrominance
  400. * components in the YCCK pixels can be subsampled without incurring major
  401. * perceptual loss. YCCK JPEG images can only be compressed from and
  402. * decompressed to packed-pixel images with the CMYK pixel format.
  403. */
  404. TJCS_YCCK
  405. };
  406. /**
  407. * Parameters
  408. */
  409. enum TJPARAM {
  410. /**
  411. * Error handling behavior
  412. *
  413. * **Value**
  414. * - `0` *[default]* Allow the current compression/decompression/transform
  415. * operation to complete unless a fatal error is encountered.
  416. * - `1` Immediately discontinue the current
  417. * compression/decompression/transform operation if a warning (non-fatal
  418. * error) occurs.
  419. */
  420. TJPARAM_STOPONWARNING,
  421. /**
  422. * Row order in packed-pixel source/destination images
  423. *
  424. * **Value**
  425. * - `0` *[default]* top-down (X11) order
  426. * - `1` bottom-up (Windows, OpenGL) order
  427. */
  428. TJPARAM_BOTTOMUP,
  429. /**
  430. * JPEG destination buffer (re)allocation [compression, lossless
  431. * transformation]
  432. *
  433. * **Value**
  434. * - `0` *[default]* Attempt to allocate or reallocate the JPEG destination
  435. * buffer as needed.
  436. * - `1` Generate an error if the JPEG destination buffer is invalid or too
  437. * small.
  438. */
  439. TJPARAM_NOREALLOC,
  440. /**
  441. * Perceptual quality of lossy JPEG images [compression only]
  442. *
  443. * **Value**
  444. * - `1`-`100` (`1` = worst quality but best compression, `100` = best
  445. * quality but worst compression) *[no default; must be explicitly
  446. * specified]*
  447. */
  448. TJPARAM_QUALITY,
  449. /**
  450. * Chrominance subsampling level
  451. *
  452. * The JPEG or YUV image uses (decompression, decoding) or will use (lossy
  453. * compression, encoding) the specified level of chrominance subsampling.
  454. *
  455. * **Value**
  456. * - One of the @ref TJSAMP "chrominance subsampling options" *[no default;
  457. * must be explicitly specified for lossy compression, encoding, and
  458. * decoding]*
  459. */
  460. TJPARAM_SUBSAMP,
  461. /**
  462. * JPEG width (in pixels) [decompression only, read-only]
  463. */
  464. TJPARAM_JPEGWIDTH,
  465. /**
  466. * JPEG height (in pixels) [decompression only, read-only]
  467. */
  468. TJPARAM_JPEGHEIGHT,
  469. /**
  470. * JPEG data precision (bits per sample) [decompression only, read-only]
  471. *
  472. * The JPEG image uses the specified number of bits per sample.
  473. *
  474. * **Value**
  475. * - `8`, `12`, or `16`
  476. *
  477. * 12-bit data precision implies #TJPARAM_OPTIMIZE unless #TJPARAM_ARITHMETIC
  478. * is set.
  479. */
  480. TJPARAM_PRECISION,
  481. /**
  482. * JPEG colorspace
  483. *
  484. * The JPEG image uses (decompression) or will use (lossy compression) the
  485. * specified colorspace.
  486. *
  487. * **Value**
  488. * - One of the @ref TJCS "JPEG colorspaces" *[default for lossy compression:
  489. * automatically selected based on the subsampling level and pixel format]*
  490. */
  491. TJPARAM_COLORSPACE,
  492. /**
  493. * Chrominance upsampling algorithm [lossy decompression only]
  494. *
  495. * **Value**
  496. * - `0` *[default]* Use smooth upsampling when decompressing a JPEG image
  497. * that was compressed using chrominance subsampling. This creates a smooth
  498. * transition between neighboring chrominance components in order to reduce
  499. * upsampling artifacts in the decompressed image.
  500. * - `1` Use the fastest chrominance upsampling algorithm available, which
  501. * may combine upsampling with color conversion.
  502. */
  503. TJPARAM_FASTUPSAMPLE,
  504. /**
  505. * DCT/IDCT algorithm [lossy compression and decompression]
  506. *
  507. * **Value**
  508. * - `0` *[default]* Use the most accurate DCT/IDCT algorithm available.
  509. * - `1` Use the fastest DCT/IDCT algorithm available.
  510. *
  511. * This parameter is provided mainly for backward compatibility with libjpeg,
  512. * which historically implemented several different DCT/IDCT algorithms
  513. * because of performance limitations with 1990s CPUs. In the libjpeg-turbo
  514. * implementation of the TurboJPEG API:
  515. * - The "fast" and "accurate" DCT/IDCT algorithms perform similarly on
  516. * modern x86/x86-64 CPUs that support AVX2 instructions.
  517. * - The "fast" algorithm is generally only about 5-15% faster than the
  518. * "accurate" algorithm on other types of CPUs.
  519. * - The difference in accuracy between the "fast" and "accurate" algorithms
  520. * is the most pronounced at JPEG quality levels above 90 and tends to be
  521. * more pronounced with decompression than with compression.
  522. * - The "fast" algorithm degrades and is not fully accelerated for JPEG
  523. * quality levels above 97, so it will be slower than the "accurate"
  524. * algorithm.
  525. */
  526. TJPARAM_FASTDCT,
  527. /**
  528. * Optimized baseline entropy coding [lossy compression only]
  529. *
  530. * **Value**
  531. * - `0` *[default]* The JPEG image will use the default Huffman tables.
  532. * - `1` Optimal Huffman tables will be computed for the JPEG image. For
  533. * lossless transformation, this can also be specified using
  534. * #TJXOPT_OPTIMIZE.
  535. *
  536. * Optimized baseline entropy coding will improve compression slightly
  537. * (generally 5% or less), but it will reduce compression performance
  538. * considerably.
  539. */
  540. TJPARAM_OPTIMIZE,
  541. /**
  542. * Progressive entropy coding
  543. *
  544. * **Value**
  545. * - `0` *[default for compression, lossless transformation]* The lossy JPEG
  546. * image uses (decompression) or will use (compression, lossless
  547. * transformation) baseline entropy coding.
  548. * - `1` The lossy JPEG image uses (decompression) or will use (compression,
  549. * lossless transformation) progressive entropy coding. For lossless
  550. * transformation, this can also be specified using #TJXOPT_PROGRESSIVE.
  551. *
  552. * Progressive entropy coding will generally improve compression relative to
  553. * baseline entropy coding, but it will reduce compression and decompression
  554. * performance considerably. Can be combined with #TJPARAM_ARITHMETIC.
  555. * Implies #TJPARAM_OPTIMIZE unless #TJPARAM_ARITHMETIC is also set.
  556. */
  557. TJPARAM_PROGRESSIVE,
  558. /**
  559. * Progressive JPEG scan limit for lossy JPEG images [decompression, lossless
  560. * transformation]
  561. *
  562. * Setting this parameter will cause the decompression and transform
  563. * functions to return an error if the number of scans in a progressive JPEG
  564. * image exceeds the specified limit. The primary purpose of this is to
  565. * allow security-critical applications to guard against an exploit of the
  566. * progressive JPEG format described in
  567. * <a href="https://libjpeg-turbo.org/pmwiki/uploads/About/TwoIssueswiththeJPEGStandard.pdf" target="_blank">this report</a>.
  568. *
  569. * **Value**
  570. * - maximum number of progressive JPEG scans that the decompression and
  571. * transform functions will process *[default: `0` (no limit)]*
  572. *
  573. * @see #TJPARAM_PROGRESSIVE
  574. */
  575. TJPARAM_SCANLIMIT,
  576. /**
  577. * Arithmetic entropy coding
  578. *
  579. * **Value**
  580. * - `0` *[default for compression, lossless transformation]* The lossy JPEG
  581. * image uses (decompression) or will use (compression, lossless
  582. * transformation) Huffman entropy coding.
  583. * - `1` The lossy JPEG image uses (decompression) or will use (compression,
  584. * lossless transformation) arithmetic entropy coding. For lossless
  585. * transformation, this can also be specified using #TJXOPT_ARITHMETIC.
  586. *
  587. * Arithmetic entropy coding will generally improve compression relative to
  588. * Huffman entropy coding, but it will reduce compression and decompression
  589. * performance considerably. Can be combined with #TJPARAM_PROGRESSIVE.
  590. */
  591. TJPARAM_ARITHMETIC,
  592. /**
  593. * Lossless JPEG
  594. *
  595. * **Value**
  596. * - `0` *[default for compression]* The JPEG image is (decompression) or
  597. * will be (compression) lossy/DCT-based.
  598. * - `1` The JPEG image is (decompression) or will be (compression)
  599. * lossless/predictive.
  600. *
  601. * In most cases, compressing and decompressing lossless JPEG images is
  602. * considerably slower than compressing and decompressing lossy JPEG images,
  603. * and lossless JPEG images are much larger than lossy JPEG images. Thus,
  604. * lossless JPEG images are typically used only for applications that require
  605. * mathematically lossless compression. Also note that the following
  606. * features are not available with lossless JPEG images:
  607. * - Colorspace conversion (lossless JPEG images always use #TJCS_RGB,
  608. * #TJCS_GRAY, or #TJCS_CMYK, depending on the pixel format of the source
  609. * image)
  610. * - Chrominance subsampling (lossless JPEG images always use #TJSAMP_444)
  611. * - JPEG quality selection
  612. * - DCT/IDCT algorithm selection
  613. * - Progressive entropy coding
  614. * - Arithmetic entropy coding
  615. * - Compression from/decompression to planar YUV images
  616. * - Decompression scaling
  617. * - Lossless transformation
  618. *
  619. * @see #TJPARAM_LOSSLESSPSV, #TJPARAM_LOSSLESSPT
  620. */
  621. TJPARAM_LOSSLESS,
  622. /**
  623. * Lossless JPEG predictor selection value (PSV)
  624. *
  625. * **Value**
  626. * - `1`-`7` *[default for compression: `1`]*
  627. *
  628. * Lossless JPEG compression shares no algorithms with lossy JPEG
  629. * compression. Instead, it uses differential pulse-code modulation (DPCM),
  630. * an algorithm whereby each sample is encoded as the difference between the
  631. * sample's value and a "predictor", which is based on the values of
  632. * neighboring samples. If Ra is the sample immediately to the left of the
  633. * current sample, Rb is the sample immediately above the current sample, and
  634. * Rc is the sample diagonally to the left and above the current sample, then
  635. * the relationship between the predictor selection value and the predictor
  636. * is as follows:
  637. *
  638. * PSV | Predictor
  639. * ----|----------
  640. * 1 | Ra
  641. * 2 | Rb
  642. * 3 | Rc
  643. * 4 | Ra + Rb – Rc
  644. * 5 | Ra + (Rb – Rc) / 2
  645. * 6 | Rb + (Ra – Rc) / 2
  646. * 7 | (Ra + Rb) / 2
  647. *
  648. * Predictors 1-3 are 1-dimensional predictors, whereas Predictors 4-7 are
  649. * 2-dimensional predictors. The best predictor for a particular image
  650. * depends on the image.
  651. *
  652. * @see #TJPARAM_LOSSLESS
  653. */
  654. TJPARAM_LOSSLESSPSV,
  655. /**
  656. * Lossless JPEG point transform (Pt)
  657. *
  658. * **Value**
  659. * - `0` through ***precision*** *- 1*, where ***precision*** is the JPEG
  660. * data precision in bits *[default for compression: `0`]*
  661. *
  662. * A point transform value of `0` is necessary in order to generate a fully
  663. * lossless JPEG image. (A non-zero point transform value right-shifts the
  664. * input samples by the specified number of bits, which is effectively a form
  665. * of lossy color quantization.)
  666. *
  667. * @see #TJPARAM_LOSSLESS, #TJPARAM_PRECISION
  668. */
  669. TJPARAM_LOSSLESSPT,
  670. /**
  671. * JPEG restart marker interval in MCU blocks (lossy) or samples (lossless)
  672. * [compression only]
  673. *
  674. * The nature of entropy coding is such that a corrupt JPEG image cannot
  675. * be decompressed beyond the point of corruption unless it contains restart
  676. * markers. A restart marker stops and restarts the entropy coding algorithm
  677. * so that, if a JPEG image is corrupted, decompression can resume at the
  678. * next marker. Thus, adding more restart markers improves the fault
  679. * tolerance of the JPEG image, but adding too many restart markers can
  680. * adversely affect the compression ratio and performance.
  681. *
  682. * **Value**
  683. * - the number of MCU blocks or samples between each restart marker
  684. * *[default: `0` (no restart markers)]*
  685. *
  686. * Setting this parameter to a non-zero value sets #TJPARAM_RESTARTROWS to 0.
  687. */
  688. TJPARAM_RESTARTBLOCKS,
  689. /**
  690. * JPEG restart marker interval in MCU rows (lossy) or sample rows (lossless)
  691. * [compression only]
  692. *
  693. * See #TJPARAM_RESTARTBLOCKS for a description of restart markers.
  694. *
  695. * **Value**
  696. * - the number of MCU rows or sample rows between each restart marker
  697. * *[default: `0` (no restart markers)]*
  698. *
  699. * Setting this parameter to a non-zero value sets #TJPARAM_RESTARTBLOCKS to
  700. * 0.
  701. */
  702. TJPARAM_RESTARTROWS,
  703. /**
  704. * JPEG horizontal pixel density
  705. *
  706. * **Value**
  707. * - The JPEG image has (decompression) or will have (compression) the
  708. * specified horizontal pixel density *[default for compression: `1`]*.
  709. *
  710. * This value is stored in or read from the JPEG header. It does not affect
  711. * the contents of the JPEG image. Note that this parameter is set by
  712. * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
  713. * density information, and the value of this parameter is stored to a
  714. * Windows BMP file by #tj3SaveImage8() if the value of #TJPARAM_DENSITYUNITS
  715. * is `2`.
  716. *
  717. * @see TJPARAM_DENSITYUNITS
  718. */
  719. TJPARAM_XDENSITY,
  720. /**
  721. * JPEG vertical pixel density
  722. *
  723. * **Value**
  724. * - The JPEG image has (decompression) or will have (compression) the
  725. * specified vertical pixel density *[default for compression: `1`]*.
  726. *
  727. * This value is stored in or read from the JPEG header. It does not affect
  728. * the contents of the JPEG image. Note that this parameter is set by
  729. * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
  730. * density information, and the value of this parameter is stored to a
  731. * Windows BMP file by #tj3SaveImage8() if the value of #TJPARAM_DENSITYUNITS
  732. * is `2`.
  733. *
  734. * @see TJPARAM_DENSITYUNITS
  735. */
  736. TJPARAM_YDENSITY,
  737. /**
  738. * JPEG pixel density units
  739. *
  740. * **Value**
  741. * - `0` *[default for compression]* The pixel density of the JPEG image is
  742. * expressed (decompression) or will be expressed (compression) in unknown
  743. * units.
  744. * - `1` The pixel density of the JPEG image is expressed (decompression) or
  745. * will be expressed (compression) in units of pixels/inch.
  746. * - `2` The pixel density of the JPEG image is expressed (decompression) or
  747. * will be expressed (compression) in units of pixels/cm.
  748. *
  749. * This value is stored in or read from the JPEG header. It does not affect
  750. * the contents of the JPEG image. Note that this parameter is set by
  751. * #tj3LoadImage8() when loading a Windows BMP file that contains pixel
  752. * density information, and the value of this parameter is stored to a
  753. * Windows BMP file by #tj3SaveImage8() if the value is `2`.
  754. *
  755. * @see TJPARAM_XDENSITY, TJPARAM_YDENSITY
  756. */
  757. TJPARAM_DENSITYUNITS,
  758. /**
  759. * Memory limit for intermediate buffers
  760. *
  761. * **Value**
  762. * - the maximum amount of memory (in megabytes) that will be allocated for
  763. * intermediate buffers, which are used with progressive JPEG compression and
  764. * decompression, optimized baseline entropy coding, lossless JPEG
  765. * compression, and lossless transformation *[default: `0` (no limit)]*
  766. */
  767. TJPARAM_MAXMEMORY,
  768. /**
  769. * Image size limit [decompression, lossless transformation, packed-pixel
  770. * image loading]
  771. *
  772. * Setting this parameter will cause the decompression, transform, and image
  773. * loading functions to return an error if the number of pixels in the source
  774. * image exceeds the specified limit. This allows security-critical
  775. * applications to guard against excessive memory consumption.
  776. *
  777. * **Value**
  778. * - maximum number of pixels that the decompression, transform, and image
  779. * loading functions will process *[default: `0` (no limit)]*
  780. */
  781. TJPARAM_MAXPIXELS
  782. };
  783. /**
  784. * The number of error codes
  785. */
  786. #define TJ_NUMERR 2
  787. /**
  788. * Error codes
  789. */
  790. enum TJERR {
  791. /**
  792. * The error was non-fatal and recoverable, but the destination image may
  793. * still be corrupt.
  794. */
  795. TJERR_WARNING,
  796. /**
  797. * The error was fatal and non-recoverable.
  798. */
  799. TJERR_FATAL
  800. };
  801. /**
  802. * The number of transform operations
  803. */
  804. #define TJ_NUMXOP 8
  805. /**
  806. * Transform operations for #tj3Transform()
  807. */
  808. enum TJXOP {
  809. /**
  810. * Do not transform the position of the image pixels
  811. */
  812. TJXOP_NONE,
  813. /**
  814. * Flip (mirror) image horizontally. This transform is imperfect if there
  815. * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
  816. */
  817. TJXOP_HFLIP,
  818. /**
  819. * Flip (mirror) image vertically. This transform is imperfect if there are
  820. * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
  821. */
  822. TJXOP_VFLIP,
  823. /**
  824. * Transpose image (flip/mirror along upper left to lower right axis.) This
  825. * transform is always perfect.
  826. */
  827. TJXOP_TRANSPOSE,
  828. /**
  829. * Transverse transpose image (flip/mirror along upper right to lower left
  830. * axis.) This transform is imperfect if there are any partial MCU blocks in
  831. * the image (see #TJXOPT_PERFECT.)
  832. */
  833. TJXOP_TRANSVERSE,
  834. /**
  835. * Rotate image clockwise by 90 degrees. This transform is imperfect if
  836. * there are any partial MCU blocks on the bottom edge (see
  837. * #TJXOPT_PERFECT.)
  838. */
  839. TJXOP_ROT90,
  840. /**
  841. * Rotate image 180 degrees. This transform is imperfect if there are any
  842. * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
  843. */
  844. TJXOP_ROT180,
  845. /**
  846. * Rotate image counter-clockwise by 90 degrees. This transform is imperfect
  847. * if there are any partial MCU blocks on the right edge (see
  848. * #TJXOPT_PERFECT.)
  849. */
  850. TJXOP_ROT270
  851. };
  852. /**
  853. * This option will cause #tj3Transform() to return an error if the transform
  854. * is not perfect. Lossless transforms operate on MCU blocks, whose size
  855. * depends on the level of chrominance subsampling used (see #tjMCUWidth and
  856. * #tjMCUHeight.) If the image's width or height is not evenly divisible by
  857. * the MCU block size, then there will be partial MCU blocks on the right
  858. * and/or bottom edges. It is not possible to move these partial MCU blocks to
  859. * the top or left of the image, so any transform that would require that is
  860. * "imperfect." If this option is not specified, then any partial MCU blocks
  861. * that cannot be transformed will be left in place, which will create
  862. * odd-looking strips on the right or bottom edge of the image.
  863. */
  864. #define TJXOPT_PERFECT (1 << 0)
  865. /**
  866. * This option will cause #tj3Transform() to discard any partial MCU blocks
  867. * that cannot be transformed.
  868. */
  869. #define TJXOPT_TRIM (1 << 1)
  870. /**
  871. * This option will enable lossless cropping. See #tj3Transform() for more
  872. * information.
  873. */
  874. #define TJXOPT_CROP (1 << 2)
  875. /**
  876. * This option will discard the color data in the source image and produce a
  877. * grayscale destination image.
  878. */
  879. #define TJXOPT_GRAY (1 << 3)
  880. /**
  881. * This option will prevent #tj3Transform() from outputting a JPEG image for
  882. * this particular transform. (This can be used in conjunction with a custom
  883. * filter to capture the transformed DCT coefficients without transcoding
  884. * them.)
  885. */
  886. #define TJXOPT_NOOUTPUT (1 << 4)
  887. /**
  888. * This option will enable progressive entropy coding in the JPEG image
  889. * generated by this particular transform. Progressive entropy coding will
  890. * generally improve compression relative to baseline entropy coding (the
  891. * default), but it will reduce decompression performance considerably.
  892. * Can be combined with #TJXOPT_ARITHMETIC. Implies #TJXOPT_OPTIMIZE unless
  893. * #TJXOPT_ARITHMETIC is also specified.
  894. */
  895. #define TJXOPT_PROGRESSIVE (1 << 5)
  896. /**
  897. * This option will prevent #tj3Transform() from copying any extra markers
  898. * (including EXIF and ICC profile data) from the source image to the
  899. * destination image.
  900. */
  901. #define TJXOPT_COPYNONE (1 << 6)
  902. /**
  903. * This option will enable arithmetic entropy coding in the JPEG image
  904. * generated by this particular transform. Arithmetic entropy coding will
  905. * generally improve compression relative to Huffman entropy coding (the
  906. * default), but it will reduce decompression performance considerably. Can be
  907. * combined with #TJXOPT_PROGRESSIVE.
  908. */
  909. #define TJXOPT_ARITHMETIC (1 << 7)
  910. /**
  911. * This option will enable optimized baseline entropy coding in the JPEG image
  912. * generated by this particular transform. Optimized baseline entropy coding
  913. * will improve compression slightly (generally 5% or less.)
  914. */
  915. #define TJXOPT_OPTIMIZE (1 << 8)
  916. /**
  917. * Scaling factor
  918. */
  919. typedef struct {
  920. /**
  921. * Numerator
  922. */
  923. int num;
  924. /**
  925. * Denominator
  926. */
  927. int denom;
  928. } tjscalingfactor;
  929. /**
  930. * Cropping region
  931. */
  932. typedef struct {
  933. /**
  934. * The left boundary of the cropping region. This must be evenly divisible
  935. * by the MCU block width (see #tjMCUWidth.)
  936. */
  937. int x;
  938. /**
  939. * The upper boundary of the cropping region. For lossless transformation,
  940. * this must be evenly divisible by the MCU block height (see #tjMCUHeight.)
  941. */
  942. int y;
  943. /**
  944. * The width of the cropping region. Setting this to 0 is the equivalent of
  945. * setting it to the width of the source JPEG image - x.
  946. */
  947. int w;
  948. /**
  949. * The height of the cropping region. Setting this to 0 is the equivalent of
  950. * setting it to the height of the source JPEG image - y.
  951. */
  952. int h;
  953. } tjregion;
  954. /**
  955. * A #tjregion structure that specifies no cropping
  956. */
  957. static const tjregion TJUNCROPPED = { 0, 0, 0, 0 };
  958. /**
  959. * Lossless transform
  960. */
  961. typedef struct tjtransform {
  962. /**
  963. * Cropping region
  964. */
  965. tjregion r;
  966. /**
  967. * One of the @ref TJXOP "transform operations"
  968. */
  969. int op;
  970. /**
  971. * The bitwise OR of one of more of the @ref TJXOPT_ARITHMETIC
  972. * "transform options"
  973. */
  974. int options;
  975. /**
  976. * Arbitrary data that can be accessed within the body of the callback
  977. * function
  978. */
  979. void *data;
  980. /**
  981. * A callback function that can be used to modify the DCT coefficients after
  982. * they are losslessly transformed but before they are transcoded to a new
  983. * JPEG image. This allows for custom filters or other transformations to be
  984. * applied in the frequency domain.
  985. *
  986. * @param coeffs pointer to an array of transformed DCT coefficients. (NOTE:
  987. * this pointer is not guaranteed to be valid once the callback returns, so
  988. * applications wishing to hand off the DCT coefficients to another function
  989. * or library should make a copy of them within the body of the callback.)
  990. *
  991. * @param arrayRegion #tjregion structure containing the width and height of
  992. * the array pointed to by `coeffs` as well as its offset relative to the
  993. * component plane. TurboJPEG implementations may choose to split each
  994. * component plane into multiple DCT coefficient arrays and call the callback
  995. * function once for each array.
  996. *
  997. * @param planeRegion #tjregion structure containing the width and height of
  998. * the component plane to which `coeffs` belongs
  999. *
  1000. * @param componentID ID number of the component plane to which `coeffs`
  1001. * belongs. (Y, Cb, and Cr have, respectively, ID's of 0, 1, and 2 in
  1002. * typical JPEG images.)
  1003. *
  1004. * @param transformID ID number of the transformed image to which `coeffs`
  1005. * belongs. This is the same as the index of the transform in the
  1006. * `transforms` array that was passed to #tj3Transform().
  1007. *
  1008. * @param transform a pointer to a #tjtransform structure that specifies the
  1009. * parameters and/or cropping region for this transform
  1010. *
  1011. * @return 0 if the callback was successful, or -1 if an error occurred.
  1012. */
  1013. int (*customFilter) (short *coeffs, tjregion arrayRegion,
  1014. tjregion planeRegion, int componentID, int transformID,
  1015. struct tjtransform *transform);
  1016. } tjtransform;
  1017. /**
  1018. * TurboJPEG instance handle
  1019. */
  1020. typedef void *tjhandle;
  1021. /**
  1022. * Compute the scaled value of `dimension` using the given scaling factor.
  1023. * This macro performs the integer equivalent of `ceil(dimension *
  1024. * scalingFactor)`.
  1025. */
  1026. #define TJSCALED(dimension, scalingFactor) \
  1027. (((dimension) * scalingFactor.num + scalingFactor.denom - 1) / \
  1028. scalingFactor.denom)
  1029. /**
  1030. * A #tjscalingfactor structure that specifies a scaling factor of 1/1 (no
  1031. * scaling)
  1032. */
  1033. static const tjscalingfactor TJUNSCALED = { 1, 1 };
  1034. #ifdef __cplusplus
  1035. extern "C" {
  1036. #endif
  1037. /**
  1038. * Create a new TurboJPEG instance.
  1039. *
  1040. * @param initType one of the @ref TJINIT "initialization options"
  1041. *
  1042. * @return a handle to the newly-created instance, or NULL if an error occurred
  1043. * (see #tj3GetErrorStr().)
  1044. */
  1045. DLLEXPORT tjhandle tj3Init(int initType);
  1046. /**
  1047. * Set the value of a parameter.
  1048. *
  1049. * @param handle handle to a TurboJPEG instance
  1050. *
  1051. * @param param one of the @ref TJPARAM "parameters"
  1052. *
  1053. * @param value value of the parameter (refer to @ref TJPARAM
  1054. * "parameter documentation")
  1055. *
  1056. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
  1057. */
  1058. DLLEXPORT int tj3Set(tjhandle handle, int param, int value);
  1059. /**
  1060. * Get the value of a parameter.
  1061. *
  1062. * @param handle handle to a TurboJPEG instance
  1063. *
  1064. * @param param one of the @ref TJPARAM "parameters"
  1065. *
  1066. * @return the value of the specified parameter, or -1 if the value is unknown.
  1067. */
  1068. DLLEXPORT int tj3Get(tjhandle handle, int param);
  1069. /**
  1070. * Compress an 8-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
  1071. * an 8-bit-per-sample JPEG image.
  1072. *
  1073. * @param handle handle to a TurboJPEG instance that has been initialized for
  1074. * compression
  1075. *
  1076. * @param srcBuf pointer to a buffer containing a packed-pixel RGB, grayscale,
  1077. * or CMYK source image to be compressed. This buffer should normally be
  1078. * `pitch * height` samples in size. However, you can also use this parameter
  1079. * to compress from a specific region of a larger buffer.
  1080. *
  1081. * @param width width (in pixels) of the source image
  1082. *
  1083. * @param pitch samples per row in the source image. Normally this should be
  1084. * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
  1085. * (Setting this parameter to 0 is the equivalent of setting it to
  1086. * <tt>width * #tjPixelSize[pixelFormat]</tt>.) However, you can also use this
  1087. * parameter to specify the row alignment/padding of the source image, to skip
  1088. * rows, or to compress from a specific region of a larger buffer.
  1089. *
  1090. * @param height height (in pixels) of the source image
  1091. *
  1092. * @param pixelFormat pixel format of the source image (see @ref TJPF
  1093. * "Pixel formats".)
  1094. *
  1095. * @param jpegBuf address of a pointer to a byte buffer that will receive the
  1096. * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
  1097. * accommodate the size of the JPEG image. Thus, you can choose to:
  1098. * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
  1099. * let TurboJPEG grow the buffer as needed,
  1100. * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
  1101. * or
  1102. * -# pre-allocate the buffer to a "worst case" size determined by calling
  1103. * #tj3JPEGBufSize(). This should ensure that the buffer never has to be
  1104. * re-allocated. (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
  1105. * .
  1106. * If you choose option 1, then `*jpegSize` should be set to the size of your
  1107. * pre-allocated buffer. In any case, unless you have set #TJPARAM_NOREALLOC,
  1108. * you should always check `*jpegBuf` upon return from this function, as it may
  1109. * have changed.
  1110. *
  1111. * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
  1112. * buffer. If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
  1113. * should be set to the size of the buffer. Upon return, `*jpegSize` will
  1114. * contain the size of the JPEG image (in bytes.) If `*jpegBuf` points to a
  1115. * JPEG buffer that is being reused from a previous call to one of the JPEG
  1116. * compression functions, then `*jpegSize` is ignored.
  1117. *
  1118. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1119. * and #tj3GetErrorCode().)
  1120. */
  1121. DLLEXPORT int tj3Compress8(tjhandle handle, const unsigned char *srcBuf,
  1122. int width, int pitch, int height, int pixelFormat,
  1123. unsigned char **jpegBuf, size_t *jpegSize);
  1124. /**
  1125. * Compress a 12-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
  1126. * a 12-bit-per-sample JPEG image.
  1127. *
  1128. * \details \copydetails tj3Compress8()
  1129. */
  1130. DLLEXPORT int tj3Compress12(tjhandle handle, const short *srcBuf, int width,
  1131. int pitch, int height, int pixelFormat,
  1132. unsigned char **jpegBuf, size_t *jpegSize);
  1133. /**
  1134. * Compress a 16-bit-per-sample packed-pixel RGB, grayscale, or CMYK image into
  1135. * a 16-bit-per-sample lossless JPEG image.
  1136. *
  1137. * \details \copydetails tj3Compress8()
  1138. */
  1139. DLLEXPORT int tj3Compress16(tjhandle handle, const unsigned short *srcBuf,
  1140. int width, int pitch, int height, int pixelFormat,
  1141. unsigned char **jpegBuf, size_t *jpegSize);
  1142. /**
  1143. * Compress an 8-bit-per-sample unified planar YUV image into an
  1144. * 8-bit-per-sample JPEG image.
  1145. *
  1146. * @param handle handle to a TurboJPEG instance that has been initialized for
  1147. * compression
  1148. *
  1149. * @param srcBuf pointer to a buffer containing a unified planar YUV source
  1150. * image to be compressed. The size of this buffer should match the value
  1151. * returned by #tj3YUVBufSize() for the given image width, height, row
  1152. * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.) The
  1153. * Y, U (Cb), and V (Cr) image planes should be stored sequentially in the
  1154. * buffer. (Refer to @ref YUVnotes "YUV Image Format Notes".)
  1155. *
  1156. * @param width width (in pixels) of the source image. If the width is not an
  1157. * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
  1158. * buffer copy will be performed.
  1159. *
  1160. * @param align row alignment (in bytes) of the source image (must be a power
  1161. * of 2.) Setting this parameter to n indicates that each row in each plane of
  1162. * the source image is padded to the nearest multiple of n bytes
  1163. * (1 = unpadded.)
  1164. *
  1165. * @param height height (in pixels) of the source image. If the height is not
  1166. * an even multiple of the MCU block height (see #tjMCUHeight), then an
  1167. * intermediate buffer copy will be performed.
  1168. *
  1169. * @param jpegBuf address of a pointer to a byte buffer that will receive the
  1170. * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
  1171. * accommodate the size of the JPEG image. Thus, you can choose to:
  1172. * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
  1173. * let TurboJPEG grow the buffer as needed,
  1174. * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
  1175. * or
  1176. * -# pre-allocate the buffer to a "worst case" size determined by calling
  1177. * #tj3JPEGBufSize(). This should ensure that the buffer never has to be
  1178. * re-allocated. (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
  1179. * .
  1180. * If you choose option 1, then `*jpegSize` should be set to the size of your
  1181. * pre-allocated buffer. In any case, unless you have set #TJPARAM_NOREALLOC,
  1182. * you should always check `*jpegBuf` upon return from this function, as it may
  1183. * have changed.
  1184. *
  1185. * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
  1186. * buffer. If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
  1187. * should be set to the size of the buffer. Upon return, `*jpegSize` will
  1188. * contain the size of the JPEG image (in bytes.) If `*jpegBuf` points to a
  1189. * JPEG buffer that is being reused from a previous call to one of the JPEG
  1190. * compression functions, then `*jpegSize` is ignored.
  1191. *
  1192. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1193. * and #tj3GetErrorCode().)
  1194. */
  1195. DLLEXPORT int tj3CompressFromYUV8(tjhandle handle,
  1196. const unsigned char *srcBuf, int width,
  1197. int align, int height,
  1198. unsigned char **jpegBuf, size_t *jpegSize);
  1199. /**
  1200. * Compress a set of 8-bit-per-sample Y, U (Cb), and V (Cr) image planes into
  1201. * an 8-bit-per-sample JPEG image.
  1202. *
  1203. * @param handle handle to a TurboJPEG instance that has been initialized for
  1204. * compression
  1205. *
  1206. * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
  1207. * (or just a Y plane, if compressing a grayscale image) that contain a YUV
  1208. * source image to be compressed. These planes can be contiguous or
  1209. * non-contiguous in memory. The size of each plane should match the value
  1210. * returned by #tj3YUVPlaneSize() for the given image width, height, strides,
  1211. * and level of chrominance subsampling (see #TJPARAM_SUBSAMP.) Refer to
  1212. * @ref YUVnotes "YUV Image Format Notes" for more details.
  1213. *
  1214. * @param width width (in pixels) of the source image. If the width is not an
  1215. * even multiple of the MCU block width (see #tjMCUWidth), then an intermediate
  1216. * buffer copy will be performed.
  1217. *
  1218. * @param strides an array of integers, each specifying the number of bytes per
  1219. * row in the corresponding plane of the YUV source image. Setting the stride
  1220. * for any plane to 0 is the same as setting it to the plane width (see
  1221. * @ref YUVnotes "YUV Image Format Notes".) If `strides` is NULL, then the
  1222. * strides for all planes will be set to their respective plane widths. You
  1223. * can adjust the strides in order to specify an arbitrary amount of row
  1224. * padding in each plane or to create a JPEG image from a subregion of a larger
  1225. * planar YUV image.
  1226. *
  1227. * @param height height (in pixels) of the source image. If the height is not
  1228. * an even multiple of the MCU block height (see #tjMCUHeight), then an
  1229. * intermediate buffer copy will be performed.
  1230. *
  1231. * @param jpegBuf address of a pointer to a byte buffer that will receive the
  1232. * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to
  1233. * accommodate the size of the JPEG image. Thus, you can choose to:
  1234. * -# pre-allocate the JPEG buffer with an arbitrary size using #tj3Alloc() and
  1235. * let TurboJPEG grow the buffer as needed,
  1236. * -# set `*jpegBuf` to NULL to tell TurboJPEG to allocate the buffer for you,
  1237. * or
  1238. * -# pre-allocate the buffer to a "worst case" size determined by calling
  1239. * #tj3JPEGBufSize(). This should ensure that the buffer never has to be
  1240. * re-allocated. (Setting #TJPARAM_NOREALLOC guarantees that it won't be.)
  1241. * .
  1242. * If you choose option 1, then `*jpegSize` should be set to the size of your
  1243. * pre-allocated buffer. In any case, unless you have set #TJPARAM_NOREALLOC,
  1244. * you should always check `*jpegBuf` upon return from this function, as it may
  1245. * have changed.
  1246. *
  1247. * @param jpegSize pointer to a size_t variable that holds the size of the JPEG
  1248. * buffer. If `*jpegBuf` points to a pre-allocated buffer, then `*jpegSize`
  1249. * should be set to the size of the buffer. Upon return, `*jpegSize` will
  1250. * contain the size of the JPEG image (in bytes.) If `*jpegBuf` points to a
  1251. * JPEG buffer that is being reused from a previous call to one of the JPEG
  1252. * compression functions, then `*jpegSize` is ignored.
  1253. *
  1254. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1255. * and #tj3GetErrorCode().)
  1256. */
  1257. DLLEXPORT int tj3CompressFromYUVPlanes8(tjhandle handle,
  1258. const unsigned char * const *srcPlanes,
  1259. int width, const int *strides,
  1260. int height, unsigned char **jpegBuf,
  1261. size_t *jpegSize);
  1262. /**
  1263. * The maximum size of the buffer (in bytes) required to hold a JPEG image with
  1264. * the given parameters. The number of bytes returned by this function is
  1265. * larger than the size of the uncompressed source image. The reason for this
  1266. * is that the JPEG format uses 16-bit coefficients, so it is possible for a
  1267. * very high-quality source image with very high-frequency content to expand
  1268. * rather than compress when converted to the JPEG format. Such images
  1269. * represent very rare corner cases, but since there is no way to predict the
  1270. * size of a JPEG image prior to compression, the corner cases have to be
  1271. * handled.
  1272. *
  1273. * @param width width (in pixels) of the image
  1274. *
  1275. * @param height height (in pixels) of the image
  1276. *
  1277. * @param jpegSubsamp the level of chrominance subsampling to be used when
  1278. * generating the JPEG image (see @ref TJSAMP
  1279. * "Chrominance subsampling options".) #TJSAMP_UNKNOWN is treated like
  1280. * #TJSAMP_444, since a buffer large enough to hold a JPEG image with no
  1281. * subsampling should also be large enough to hold a JPEG image with an
  1282. * arbitrary level of subsampling. Note that lossless JPEG images always
  1283. * use #TJSAMP_444.
  1284. *
  1285. * @return the maximum size of the buffer (in bytes) required to hold the
  1286. * image, or 0 if the arguments are out of bounds.
  1287. */
  1288. DLLEXPORT size_t tj3JPEGBufSize(int width, int height, int jpegSubsamp);
  1289. /**
  1290. * The size of the buffer (in bytes) required to hold a unified planar YUV
  1291. * image with the given parameters.
  1292. *
  1293. * @param width width (in pixels) of the image
  1294. *
  1295. * @param align row alignment (in bytes) of the image (must be a power of 2.)
  1296. * Setting this parameter to n specifies that each row in each plane of the
  1297. * image will be padded to the nearest multiple of n bytes (1 = unpadded.)
  1298. *
  1299. * @param height height (in pixels) of the image
  1300. *
  1301. * @param subsamp level of chrominance subsampling in the image (see
  1302. * @ref TJSAMP "Chrominance subsampling options".)
  1303. *
  1304. * @return the size of the buffer (in bytes) required to hold the image, or 0
  1305. * if the arguments are out of bounds.
  1306. */
  1307. DLLEXPORT size_t tj3YUVBufSize(int width, int align, int height, int subsamp);
  1308. /**
  1309. * The size of the buffer (in bytes) required to hold a YUV image plane with
  1310. * the given parameters.
  1311. *
  1312. * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
  1313. *
  1314. * @param width width (in pixels) of the YUV image. NOTE: this is the width of
  1315. * the whole image, not the plane width.
  1316. *
  1317. * @param stride bytes per row in the image plane. Setting this to 0 is the
  1318. * equivalent of setting it to the plane width.
  1319. *
  1320. * @param height height (in pixels) of the YUV image. NOTE: this is the height
  1321. * of the whole image, not the plane height.
  1322. *
  1323. * @param subsamp level of chrominance subsampling in the image (see
  1324. * @ref TJSAMP "Chrominance subsampling options".)
  1325. *
  1326. * @return the size of the buffer (in bytes) required to hold the YUV image
  1327. * plane, or 0 if the arguments are out of bounds.
  1328. */
  1329. DLLEXPORT size_t tj3YUVPlaneSize(int componentID, int width, int stride,
  1330. int height, int subsamp);
  1331. /**
  1332. * The plane width of a YUV image plane with the given parameters. Refer to
  1333. * @ref YUVnotes "YUV Image Format Notes" for a description of plane width.
  1334. *
  1335. * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
  1336. *
  1337. * @param width width (in pixels) of the YUV image
  1338. *
  1339. * @param subsamp level of chrominance subsampling in the image (see
  1340. * @ref TJSAMP "Chrominance subsampling options".)
  1341. *
  1342. * @return the plane width of a YUV image plane with the given parameters, or 0
  1343. * if the arguments are out of bounds.
  1344. */
  1345. DLLEXPORT int tj3YUVPlaneWidth(int componentID, int width, int subsamp);
  1346. /**
  1347. * The plane height of a YUV image plane with the given parameters. Refer to
  1348. * @ref YUVnotes "YUV Image Format Notes" for a description of plane height.
  1349. *
  1350. * @param componentID ID number of the image plane (0 = Y, 1 = U/Cb, 2 = V/Cr)
  1351. *
  1352. * @param height height (in pixels) of the YUV image
  1353. *
  1354. * @param subsamp level of chrominance subsampling in the image (see
  1355. * @ref TJSAMP "Chrominance subsampling options".)
  1356. *
  1357. * @return the plane height of a YUV image plane with the given parameters, or
  1358. * 0 if the arguments are out of bounds.
  1359. */
  1360. DLLEXPORT int tj3YUVPlaneHeight(int componentID, int height, int subsamp);
  1361. /**
  1362. * Encode an 8-bit-per-sample packed-pixel RGB or grayscale image into an
  1363. * 8-bit-per-sample unified planar YUV image. This function performs color
  1364. * conversion (which is accelerated in the libjpeg-turbo implementation) but
  1365. * does not execute any of the other steps in the JPEG compression process.
  1366. *
  1367. * @param handle handle to a TurboJPEG instance that has been initialized for
  1368. * compression
  1369. *
  1370. * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
  1371. * source image to be encoded. This buffer should normally be `pitch * height`
  1372. * bytes in size. However, you can also use this parameter to encode from a
  1373. * specific region of a larger buffer.
  1374. *
  1375. * @param width width (in pixels) of the source image
  1376. *
  1377. * @param pitch bytes per row in the source image. Normally this should be
  1378. * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
  1379. * (Setting this parameter to 0 is the equivalent of setting it to
  1380. * <tt>width * #tjPixelSize[pixelFormat]</tt>.) However, you can also use this
  1381. * parameter to specify the row alignment/padding of the source image, to skip
  1382. * rows, or to encode from a specific region of a larger packed-pixel image.
  1383. *
  1384. * @param height height (in pixels) of the source image
  1385. *
  1386. * @param pixelFormat pixel format of the source image (see @ref TJPF
  1387. * "Pixel formats".)
  1388. *
  1389. * @param dstBuf pointer to a buffer that will receive the unified planar YUV
  1390. * image. Use #tj3YUVBufSize() to determine the appropriate size for this
  1391. * buffer based on the image width, height, row alignment, and level of
  1392. * chrominance subsampling (see #TJPARAM_SUBSAMP.) The Y, U (Cb), and V (Cr)
  1393. * image planes will be stored sequentially in the buffer. (Refer to
  1394. * @ref YUVnotes "YUV Image Format Notes".)
  1395. *
  1396. * @param align row alignment (in bytes) of the YUV image (must be a power of
  1397. * 2.) Setting this parameter to n will cause each row in each plane of the
  1398. * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
  1399. * To generate images suitable for X Video, `align` should be set to 4.
  1400. *
  1401. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1402. * and #tj3GetErrorCode().)
  1403. */
  1404. DLLEXPORT int tj3EncodeYUV8(tjhandle handle, const unsigned char *srcBuf,
  1405. int width, int pitch, int height, int pixelFormat,
  1406. unsigned char *dstBuf, int align);
  1407. /**
  1408. * Encode an 8-bit-per-sample packed-pixel RGB or grayscale image into separate
  1409. * 8-bit-per-sample Y, U (Cb), and V (Cr) image planes. This function performs
  1410. * color conversion (which is accelerated in the libjpeg-turbo implementation)
  1411. * but does not execute any of the other steps in the JPEG compression process.
  1412. *
  1413. * @param handle handle to a TurboJPEG instance that has been initialized for
  1414. * compression
  1415. *
  1416. * @param srcBuf pointer to a buffer containing a packed-pixel RGB or grayscale
  1417. * source image to be encoded. This buffer should normally be `pitch * height`
  1418. * bytes in size. However, you can also use this parameter to encode from a
  1419. * specific region of a larger buffer.
  1420. *
  1421. *
  1422. * @param width width (in pixels) of the source image
  1423. *
  1424. * @param pitch bytes per row in the source image. Normally this should be
  1425. * <tt>width * #tjPixelSize[pixelFormat]</tt>, if the image is unpadded.
  1426. * (Setting this parameter to 0 is the equivalent of setting it to
  1427. * <tt>width * #tjPixelSize[pixelFormat]</tt>.) However, you can also use this
  1428. * parameter to specify the row alignment/padding of the source image, to skip
  1429. * rows, or to encode from a specific region of a larger packed-pixel image.
  1430. *
  1431. * @param height height (in pixels) of the source image
  1432. *
  1433. * @param pixelFormat pixel format of the source image (see @ref TJPF
  1434. * "Pixel formats".)
  1435. *
  1436. * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
  1437. * (or just a Y plane, if generating a grayscale image) that will receive the
  1438. * encoded image. These planes can be contiguous or non-contiguous in memory.
  1439. * Use #tj3YUVPlaneSize() to determine the appropriate size for each plane
  1440. * based on the image width, height, strides, and level of chrominance
  1441. * subsampling (see #TJPARAM_SUBSAMP.) Refer to @ref YUVnotes
  1442. * "YUV Image Format Notes" for more details.
  1443. *
  1444. * @param strides an array of integers, each specifying the number of bytes per
  1445. * row in the corresponding plane of the YUV image. Setting the stride for any
  1446. * plane to 0 is the same as setting it to the plane width (see @ref YUVnotes
  1447. * "YUV Image Format Notes".) If `strides` is NULL, then the strides for all
  1448. * planes will be set to their respective plane widths. You can adjust the
  1449. * strides in order to add an arbitrary amount of row padding to each plane or
  1450. * to encode an RGB or grayscale image into a subregion of a larger planar YUV
  1451. * image.
  1452. *
  1453. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1454. * and #tj3GetErrorCode().)
  1455. */
  1456. DLLEXPORT int tj3EncodeYUVPlanes8(tjhandle handle, const unsigned char *srcBuf,
  1457. int width, int pitch, int height,
  1458. int pixelFormat, unsigned char **dstPlanes,
  1459. int *strides);
  1460. /**
  1461. * Retrieve information about a JPEG image without decompressing it, or prime
  1462. * the decompressor with quantization and Huffman tables. If a JPEG image is
  1463. * passed to this function, then the @ref TJPARAM "parameters" that describe
  1464. * the JPEG image will be set when the function returns.
  1465. *
  1466. * @param handle handle to a TurboJPEG instance that has been initialized for
  1467. * decompression
  1468. *
  1469. * @param jpegBuf pointer to a byte buffer containing a JPEG image or an
  1470. * "abbreviated table specification" (AKA "tables-only") datastream. Passing a
  1471. * tables-only datastream to this function primes the decompressor with
  1472. * quantization and Huffman tables that can be used when decompressing
  1473. * subsequent "abbreviated image" datastreams. This is useful, for instance,
  1474. * when decompressing video streams in which all frames share the same
  1475. * quantization and Huffman tables.
  1476. *
  1477. * @param jpegSize size of the JPEG image or tables-only datastream (in bytes)
  1478. *
  1479. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1480. * and #tj3GetErrorCode().)
  1481. */
  1482. DLLEXPORT int tj3DecompressHeader(tjhandle handle,
  1483. const unsigned char *jpegBuf,
  1484. size_t jpegSize);
  1485. /**
  1486. * Returns a list of fractional scaling factors that the JPEG decompressor
  1487. * supports.
  1488. *
  1489. * @param numScalingFactors pointer to an integer variable that will receive
  1490. * the number of elements in the list
  1491. *
  1492. * @return a pointer to a list of fractional scaling factors, or NULL if an
  1493. * error is encountered (see #tj3GetErrorStr().)
  1494. */
  1495. DLLEXPORT tjscalingfactor *tj3GetScalingFactors(int *numScalingFactors);
  1496. /**
  1497. * Set the scaling factor for subsequent lossy decompression operations.
  1498. *
  1499. * @param handle handle to a TurboJPEG instance that has been initialized for
  1500. * decompression
  1501. *
  1502. * @param scalingFactor #tjscalingfactor structure that specifies a fractional
  1503. * scaling factor that the decompressor supports (see #tj3GetScalingFactors()),
  1504. * or <tt>#TJUNSCALED</tt> for no scaling. Decompression scaling is a function
  1505. * of the IDCT algorithm, so scaling factors are generally limited to multiples
  1506. * of 1/8. If the entire JPEG image will be decompressed, then the width and
  1507. * height of the scaled destination image can be determined by calling
  1508. * #TJSCALED() with the JPEG width and height (see #TJPARAM_JPEGWIDTH and
  1509. * #TJPARAM_JPEGHEIGHT) and the specified scaling factor. When decompressing
  1510. * into a planar YUV image, an intermediate buffer copy will be performed if
  1511. * the width or height of the scaled destination image is not an even multiple
  1512. * of the MCU block size (see #tjMCUWidth and #tjMCUHeight.) Note that
  1513. * decompression scaling is not available (and the specified scaling factor is
  1514. * ignored) when decompressing lossless JPEG images (see #TJPARAM_LOSSLESS),
  1515. * since the IDCT algorithm is not used with those images. Note also that
  1516. * #TJPARAM_FASTDCT is ignored when decompression scaling is enabled.
  1517. *
  1518. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
  1519. */
  1520. DLLEXPORT int tj3SetScalingFactor(tjhandle handle,
  1521. tjscalingfactor scalingFactor);
  1522. /**
  1523. * Set the cropping region for partially decompressing a lossy JPEG image into
  1524. * a packed-pixel image
  1525. *
  1526. * @param handle handle to a TurboJPEG instance that has been initialized for
  1527. * decompression
  1528. *
  1529. * @param croppingRegion #tjregion structure that specifies a subregion of the
  1530. * JPEG image to decompress, or <tt>#TJUNCROPPED</tt> for no cropping. The
  1531. * left boundary of the cropping region must be evenly divisible by the scaled
  1532. * MCU block width (<tt>#TJSCALED(#tjMCUWidth[subsamp], scalingFactor)</tt>,
  1533. * where `subsamp` is the level of chrominance subsampling in the JPEG image
  1534. * (see #TJPARAM_SUBSAMP) and `scalingFactor` is the decompression scaling
  1535. * factor (see #tj3SetScalingFactor().) The cropping region should be
  1536. * specified relative to the scaled image dimensions. Unless `croppingRegion`
  1537. * is <tt>#TJUNCROPPED</tt>, the JPEG header must be read (see
  1538. * #tj3DecompressHeader()) prior to calling this function.
  1539. *
  1540. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
  1541. */
  1542. DLLEXPORT int tj3SetCroppingRegion(tjhandle handle, tjregion croppingRegion);
  1543. /**
  1544. * Decompress an 8-bit-per-sample JPEG image into an 8-bit-per-sample
  1545. * packed-pixel RGB, grayscale, or CMYK image. The @ref TJPARAM "parameters"
  1546. * that describe the JPEG image will be set when this function returns.
  1547. *
  1548. * @param handle handle to a TurboJPEG instance that has been initialized for
  1549. * decompression
  1550. *
  1551. * @param jpegBuf pointer to a byte buffer containing the JPEG image to
  1552. * decompress
  1553. *
  1554. * @param jpegSize size of the JPEG image (in bytes)
  1555. *
  1556. * @param dstBuf pointer to a buffer that will receive the packed-pixel
  1557. * decompressed image. This buffer should normally be
  1558. * `pitch * destinationHeight` samples in size. However, you can also use this
  1559. * parameter to decompress into a specific region of a larger buffer. NOTE:
  1560. * If the JPEG image is lossy, then `destinationHeight` is either the scaled
  1561. * JPEG height (see #TJSCALED(), #TJPARAM_JPEGHEIGHT, and
  1562. * #tj3SetScalingFactor()) or the height of the cropping region (see
  1563. * #tj3SetCroppingRegion().) If the JPEG image is lossless, then
  1564. * `destinationHeight` is the JPEG height.
  1565. *
  1566. * @param pitch samples per row in the destination image. Normally this should
  1567. * be set to <tt>destinationWidth * #tjPixelSize[pixelFormat]</tt>, if the
  1568. * destination image should be unpadded. (Setting this parameter to 0 is the
  1569. * equivalent of setting it to
  1570. * <tt>destinationWidth * #tjPixelSize[pixelFormat]</tt>.) However, you can
  1571. * also use this parameter to specify the row alignment/padding of the
  1572. * destination image, to skip rows, or to decompress into a specific region of
  1573. * a larger buffer. NOTE: If the JPEG image is lossy, then `destinationWidth`
  1574. * is either the scaled JPEG width (see #TJSCALED(), #TJPARAM_JPEGWIDTH, and
  1575. * #tj3SetScalingFactor()) or the width of the cropping region (see
  1576. * #tj3SetCroppingRegion().) If the JPEG image is lossless, then
  1577. * `destinationWidth` is the JPEG width.
  1578. *
  1579. * @param pixelFormat pixel format of the destination image (see @ref
  1580. * TJPF "Pixel formats".)
  1581. *
  1582. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1583. * and #tj3GetErrorCode().)
  1584. */
  1585. DLLEXPORT int tj3Decompress8(tjhandle handle, const unsigned char *jpegBuf,
  1586. size_t jpegSize, unsigned char *dstBuf, int pitch,
  1587. int pixelFormat);
  1588. /**
  1589. * Decompress a 12-bit-per-sample JPEG image into a 12-bit-per-sample
  1590. * packed-pixel RGB, grayscale, or CMYK image.
  1591. *
  1592. * \details \copydetails tj3Decompress8()
  1593. */
  1594. DLLEXPORT int tj3Decompress12(tjhandle handle, const unsigned char *jpegBuf,
  1595. size_t jpegSize, short *dstBuf, int pitch,
  1596. int pixelFormat);
  1597. /**
  1598. * Decompress a 16-bit-per-sample lossless JPEG image into a 16-bit-per-sample
  1599. * packed-pixel RGB, grayscale, or CMYK image.
  1600. *
  1601. * \details \copydetails tj3Decompress8()
  1602. */
  1603. DLLEXPORT int tj3Decompress16(tjhandle handle, const unsigned char *jpegBuf,
  1604. size_t jpegSize, unsigned short *dstBuf,
  1605. int pitch, int pixelFormat);
  1606. /**
  1607. * Decompress an 8-bit-per-sample JPEG image into an 8-bit-per-sample unified
  1608. * planar YUV image. This function performs JPEG decompression but leaves out
  1609. * the color conversion step, so a planar YUV image is generated instead of a
  1610. * packed-pixel image. The @ref TJPARAM "parameters" that describe the JPEG
  1611. * image will be set when this function returns.
  1612. *
  1613. * @param handle handle to a TurboJPEG instance that has been initialized for
  1614. * decompression
  1615. *
  1616. * @param jpegBuf pointer to a byte buffer containing the JPEG image to
  1617. * decompress
  1618. *
  1619. * @param jpegSize size of the JPEG image (in bytes)
  1620. *
  1621. * @param dstBuf pointer to a buffer that will receive the unified planar YUV
  1622. * decompressed image. Use #tj3YUVBufSize() to determine the appropriate size
  1623. * for this buffer based on the scaled JPEG width and height (see #TJSCALED(),
  1624. * #TJPARAM_JPEGWIDTH, #TJPARAM_JPEGHEIGHT, and #tj3SetScalingFactor()), row
  1625. * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.) The
  1626. * Y, U (Cb), and V (Cr) image planes will be stored sequentially in the
  1627. * buffer. (Refer to @ref YUVnotes "YUV Image Format Notes".)
  1628. *
  1629. * @param align row alignment (in bytes) of the YUV image (must be a power of
  1630. * 2.) Setting this parameter to n will cause each row in each plane of the
  1631. * YUV image to be padded to the nearest multiple of n bytes (1 = unpadded.)
  1632. * To generate images suitable for X Video, `align` should be set to 4.
  1633. *
  1634. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1635. * and #tj3GetErrorCode().)
  1636. */
  1637. DLLEXPORT int tj3DecompressToYUV8(tjhandle handle,
  1638. const unsigned char *jpegBuf,
  1639. size_t jpegSize,
  1640. unsigned char *dstBuf, int align);
  1641. /**
  1642. * Decompress an 8-bit-per-sample JPEG image into separate 8-bit-per-sample Y,
  1643. * U (Cb), and V (Cr) image planes. This function performs JPEG decompression
  1644. * but leaves out the color conversion step, so a planar YUV image is generated
  1645. * instead of a packed-pixel image. The @ref TJPARAM "parameters" that
  1646. * describe the JPEG image will be set when this function returns.
  1647. *
  1648. * @param handle handle to a TurboJPEG instance that has been initialized for
  1649. * decompression
  1650. *
  1651. * @param jpegBuf pointer to a byte buffer containing the JPEG image to
  1652. * decompress
  1653. *
  1654. * @param jpegSize size of the JPEG image (in bytes)
  1655. *
  1656. * @param dstPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
  1657. * (or just a Y plane, if decompressing a grayscale image) that will receive
  1658. * the decompressed image. These planes can be contiguous or non-contiguous in
  1659. * memory. Use #tj3YUVPlaneSize() to determine the appropriate size for each
  1660. * plane based on the scaled JPEG width and height (see #TJSCALED(),
  1661. * #TJPARAM_JPEGWIDTH, #TJPARAM_JPEGHEIGHT, and #tj3SetScalingFactor()),
  1662. * strides, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.) Refer
  1663. * to @ref YUVnotes "YUV Image Format Notes" for more details.
  1664. *
  1665. * @param strides an array of integers, each specifying the number of bytes per
  1666. * row in the corresponding plane of the YUV image. Setting the stride for any
  1667. * plane to 0 is the same as setting it to the scaled plane width (see
  1668. * @ref YUVnotes "YUV Image Format Notes".) If `strides` is NULL, then the
  1669. * strides for all planes will be set to their respective scaled plane widths.
  1670. * You can adjust the strides in order to add an arbitrary amount of row
  1671. * padding to each plane or to decompress the JPEG image into a subregion of a
  1672. * larger planar YUV image.
  1673. *
  1674. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1675. * and #tj3GetErrorCode().)
  1676. */
  1677. DLLEXPORT int tj3DecompressToYUVPlanes8(tjhandle handle,
  1678. const unsigned char *jpegBuf,
  1679. size_t jpegSize,
  1680. unsigned char **dstPlanes,
  1681. int *strides);
  1682. /**
  1683. * Decode an 8-bit-per-sample unified planar YUV image into an 8-bit-per-sample
  1684. * packed-pixel RGB or grayscale image. This function performs color
  1685. * conversion (which is accelerated in the libjpeg-turbo implementation) but
  1686. * does not execute any of the other steps in the JPEG decompression process.
  1687. *
  1688. * @param handle handle to a TurboJPEG instance that has been initialized for
  1689. * decompression
  1690. *
  1691. * @param srcBuf pointer to a buffer containing a unified planar YUV source
  1692. * image to be decoded. The size of this buffer should match the value
  1693. * returned by #tj3YUVBufSize() for the given image width, height, row
  1694. * alignment, and level of chrominance subsampling (see #TJPARAM_SUBSAMP.) The
  1695. * Y, U (Cb), and V (Cr) image planes should be stored sequentially in the
  1696. * source buffer. (Refer to @ref YUVnotes "YUV Image Format Notes".)
  1697. *
  1698. * @param align row alignment (in bytes) of the YUV source image (must be a
  1699. * power of 2.) Setting this parameter to n indicates that each row in each
  1700. * plane of the YUV source image is padded to the nearest multiple of n bytes
  1701. * (1 = unpadded.)
  1702. *
  1703. * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
  1704. * image. This buffer should normally be `pitch * height` bytes in size.
  1705. * However, you can also use this parameter to decode into a specific region of
  1706. * a larger buffer.
  1707. *
  1708. * @param width width (in pixels) of the source and destination images
  1709. *
  1710. * @param pitch bytes per row in the destination image. Normally this should
  1711. * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
  1712. * image should be unpadded. (Setting this parameter to 0 is the equivalent of
  1713. * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.) However, you can
  1714. * also use this parameter to specify the row alignment/padding of the
  1715. * destination image, to skip rows, or to decode into a specific region of a
  1716. * larger buffer.
  1717. *
  1718. * @param height height (in pixels) of the source and destination images
  1719. *
  1720. * @param pixelFormat pixel format of the destination image (see @ref TJPF
  1721. * "Pixel formats".)
  1722. *
  1723. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1724. * and #tj3GetErrorCode().)
  1725. */
  1726. DLLEXPORT int tj3DecodeYUV8(tjhandle handle, const unsigned char *srcBuf,
  1727. int align, unsigned char *dstBuf, int width,
  1728. int pitch, int height, int pixelFormat);
  1729. /**
  1730. * Decode a set of 8-bit-per-sample Y, U (Cb), and V (Cr) image planes into an
  1731. * 8-bit-per-sample packed-pixel RGB or grayscale image. This function
  1732. * performs color conversion (which is accelerated in the libjpeg-turbo
  1733. * implementation) but does not execute any of the other steps in the JPEG
  1734. * decompression process.
  1735. *
  1736. * @param handle handle to a TurboJPEG instance that has been initialized for
  1737. * decompression
  1738. *
  1739. * @param srcPlanes an array of pointers to Y, U (Cb), and V (Cr) image planes
  1740. * (or just a Y plane, if decoding a grayscale image) that contain a YUV image
  1741. * to be decoded. These planes can be contiguous or non-contiguous in memory.
  1742. * The size of each plane should match the value returned by #tj3YUVPlaneSize()
  1743. * for the given image width, height, strides, and level of chrominance
  1744. * subsampling (see #TJPARAM_SUBSAMP.) Refer to @ref YUVnotes
  1745. * "YUV Image Format Notes" for more details.
  1746. *
  1747. * @param strides an array of integers, each specifying the number of bytes per
  1748. * row in the corresponding plane of the YUV source image. Setting the stride
  1749. * for any plane to 0 is the same as setting it to the plane width (see
  1750. * @ref YUVnotes "YUV Image Format Notes".) If `strides` is NULL, then the
  1751. * strides for all planes will be set to their respective plane widths. You
  1752. * can adjust the strides in order to specify an arbitrary amount of row
  1753. * padding in each plane or to decode a subregion of a larger planar YUV image.
  1754. *
  1755. * @param dstBuf pointer to a buffer that will receive the packed-pixel decoded
  1756. * image. This buffer should normally be `pitch * height` bytes in size.
  1757. * However, you can also use this parameter to decode into a specific region of
  1758. * a larger buffer.
  1759. *
  1760. * @param width width (in pixels) of the source and destination images
  1761. *
  1762. * @param pitch bytes per row in the destination image. Normally this should
  1763. * be set to <tt>width * #tjPixelSize[pixelFormat]</tt>, if the destination
  1764. * image should be unpadded. (Setting this parameter to 0 is the equivalent of
  1765. * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.) However, you can
  1766. * also use this parameter to specify the row alignment/padding of the
  1767. * destination image, to skip rows, or to decode into a specific region of a
  1768. * larger buffer.
  1769. *
  1770. * @param height height (in pixels) of the source and destination images
  1771. *
  1772. * @param pixelFormat pixel format of the destination image (see @ref TJPF
  1773. * "Pixel formats".)
  1774. *
  1775. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1776. * and #tj3GetErrorCode().)
  1777. */
  1778. DLLEXPORT int tj3DecodeYUVPlanes8(tjhandle handle,
  1779. const unsigned char * const *srcPlanes,
  1780. const int *strides, unsigned char *dstBuf,
  1781. int width, int pitch, int height,
  1782. int pixelFormat);
  1783. /**
  1784. * Losslessly transform a JPEG image into another JPEG image. Lossless
  1785. * transforms work by moving the raw DCT coefficients from one JPEG image
  1786. * structure to another without altering the values of the coefficients. While
  1787. * this is typically faster than decompressing the image, transforming it, and
  1788. * re-compressing it, lossless transforms are not free. Each lossless
  1789. * transform requires reading and performing entropy decoding on all of the
  1790. * coefficients in the source image, regardless of the size of the destination
  1791. * image. Thus, this function provides a means of generating multiple
  1792. * transformed images from the same source or applying multiple transformations
  1793. * simultaneously, in order to eliminate the need to read the source
  1794. * coefficients multiple times.
  1795. *
  1796. * @param handle handle to a TurboJPEG instance that has been initialized for
  1797. * lossless transformation
  1798. *
  1799. * @param jpegBuf pointer to a byte buffer containing the JPEG source image to
  1800. * transform
  1801. *
  1802. * @param jpegSize size of the JPEG source image (in bytes)
  1803. *
  1804. * @param n the number of transformed JPEG images to generate
  1805. *
  1806. * @param dstBufs pointer to an array of n byte buffers. `dstBufs[i]` will
  1807. * receive a JPEG image that has been transformed using the parameters in
  1808. * `transforms[i]`. TurboJPEG has the ability to reallocate the JPEG
  1809. * destination buffer to accommodate the size of the transformed JPEG image.
  1810. * Thus, you can choose to:
  1811. * -# pre-allocate the JPEG destination buffer with an arbitrary size using
  1812. * #tj3Alloc() and let TurboJPEG grow the buffer as needed,
  1813. * -# set `dstBufs[i]` to NULL to tell TurboJPEG to allocate the buffer for
  1814. * you, or
  1815. * -# pre-allocate the buffer to a "worst case" size determined by calling
  1816. * #tj3JPEGBufSize() with the transformed or cropped width and height and the
  1817. * level of subsampling used in the source image. Under normal circumstances,
  1818. * this should ensure that the buffer never has to be re-allocated. (Setting
  1819. * #TJPARAM_NOREALLOC guarantees that it won't be.) Note, however, that there
  1820. * are some rare cases (such as transforming images with a large amount of
  1821. * embedded EXIF or ICC profile data) in which the transformed JPEG image will
  1822. * be larger than the worst-case size, and #TJPARAM_NOREALLOC cannot be used in
  1823. * those cases.
  1824. * .
  1825. * If you choose option 1, then `dstSizes[i]` should be set to the size of your
  1826. * pre-allocated buffer. In any case, unless you have set #TJPARAM_NOREALLOC,
  1827. * you should always check `dstBufs[i]` upon return from this function, as it
  1828. * may have changed.
  1829. *
  1830. * @param dstSizes pointer to an array of n size_t variables that will receive
  1831. * the actual sizes (in bytes) of each transformed JPEG image. If `dstBufs[i]`
  1832. * points to a pre-allocated buffer, then `dstSizes[i]` should be set to the
  1833. * size of the buffer. Upon return, `dstSizes[i]` will contain the size of the
  1834. * transformed JPEG image (in bytes.)
  1835. *
  1836. * @param transforms pointer to an array of n #tjtransform structures, each of
  1837. * which specifies the transform parameters and/or cropping region for the
  1838. * corresponding transformed JPEG image.
  1839. *
  1840. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr()
  1841. * and #tj3GetErrorCode().)
  1842. */
  1843. DLLEXPORT int tj3Transform(tjhandle handle, const unsigned char *jpegBuf,
  1844. size_t jpegSize, int n, unsigned char **dstBufs,
  1845. size_t *dstSizes, const tjtransform *transforms);
  1846. /**
  1847. * Destroy a TurboJPEG instance.
  1848. *
  1849. * @param handle handle to a TurboJPEG instance. If the handle is NULL, then
  1850. * this function has no effect.
  1851. */
  1852. DLLEXPORT void tj3Destroy(tjhandle handle);
  1853. /**
  1854. * Allocate a byte buffer for use with TurboJPEG. You should always use this
  1855. * function to allocate the JPEG destination buffer(s) for the compression and
  1856. * transform functions unless you are disabling automatic buffer (re)allocation
  1857. * (by setting #TJPARAM_NOREALLOC.)
  1858. *
  1859. * @param bytes the number of bytes to allocate
  1860. *
  1861. * @return a pointer to a newly-allocated buffer with the specified number of
  1862. * bytes.
  1863. *
  1864. * @see tj3Free()
  1865. */
  1866. DLLEXPORT void *tj3Alloc(size_t bytes);
  1867. /**
  1868. * Load an 8-bit-per-sample packed-pixel image from disk into memory.
  1869. *
  1870. * @param handle handle to a TurboJPEG instance
  1871. *
  1872. * @param filename name of a file containing a packed-pixel image in Windows
  1873. * BMP or PBMPLUS (PPM/PGM) format. Windows BMP files require 8-bit-per-sample
  1874. * data precision. If the data precision of the PBMPLUS file does not match
  1875. * the target data precision, then upconverting or downconverting will be
  1876. * performed.
  1877. *
  1878. * @param width pointer to an integer variable that will receive the width (in
  1879. * pixels) of the packed-pixel image
  1880. *
  1881. * @param align row alignment (in samples) of the packed-pixel buffer to be
  1882. * returned (must be a power of 2.) Setting this parameter to n will cause all
  1883. * rows in the buffer to be padded to the nearest multiple of n samples
  1884. * (1 = unpadded.)
  1885. *
  1886. * @param height pointer to an integer variable that will receive the height
  1887. * (in pixels) of the packed-pixel image
  1888. *
  1889. * @param pixelFormat pointer to an integer variable that specifies or will
  1890. * receive the pixel format of the packed-pixel buffer. The behavior of this
  1891. * function will vary depending on the value of `*pixelFormat` passed to the
  1892. * function:
  1893. * - @ref TJPF_UNKNOWN : The packed-pixel buffer returned by this function will
  1894. * use the most optimal pixel format for the file type, and `*pixelFormat` will
  1895. * contain the ID of that pixel format upon successful return from this
  1896. * function.
  1897. * - @ref TJPF_GRAY : Only PGM files and 8-bit-per-pixel BMP files with a
  1898. * grayscale colormap can be loaded.
  1899. * - @ref TJPF_CMYK : The RGB or grayscale pixels stored in the file will be
  1900. * converted using a quick & dirty algorithm that is suitable only for testing
  1901. * purposes. (Proper conversion between CMYK and other formats requires a
  1902. * color management system.)
  1903. * - Other @ref TJPF "pixel formats" : The packed-pixel buffer will use the
  1904. * specified pixel format, and pixel format conversion will be performed if
  1905. * necessary.
  1906. *
  1907. * @return a pointer to a newly-allocated buffer containing the packed-pixel
  1908. * image, converted to the chosen pixel format and with the chosen row
  1909. * alignment, or NULL if an error occurred (see #tj3GetErrorStr().) This
  1910. * buffer should be freed using #tj3Free().
  1911. */
  1912. DLLEXPORT unsigned char *tj3LoadImage8(tjhandle handle, const char *filename,
  1913. int *width, int align, int *height,
  1914. int *pixelFormat);
  1915. /**
  1916. * Load a 12-bit-per-sample packed-pixel image from disk into memory.
  1917. *
  1918. * \details \copydetails tj3LoadImage8()
  1919. */
  1920. DLLEXPORT short *tj3LoadImage12(tjhandle handle, const char *filename,
  1921. int *width, int align, int *height,
  1922. int *pixelFormat);
  1923. /**
  1924. * Load a 16-bit-per-sample packed-pixel image from disk into memory.
  1925. *
  1926. * \details \copydetails tj3LoadImage8()
  1927. */
  1928. DLLEXPORT unsigned short *tj3LoadImage16(tjhandle handle, const char *filename,
  1929. int *width, int align, int *height,
  1930. int *pixelFormat);
  1931. /**
  1932. * Save an 8-bit-per-sample packed-pixel image from memory to disk.
  1933. *
  1934. * @param handle handle to a TurboJPEG instance
  1935. *
  1936. * @param filename name of a file to which to save the packed-pixel image. The
  1937. * image will be stored in Windows BMP or PBMPLUS (PPM/PGM) format, depending
  1938. * on the file extension. Windows BMP files require 8-bit-per-sample data
  1939. * precision.
  1940. *
  1941. * @param buffer pointer to a buffer containing a packed-pixel RGB, grayscale,
  1942. * or CMYK image to be saved
  1943. *
  1944. * @param width width (in pixels) of the packed-pixel image
  1945. *
  1946. * @param pitch samples per row in the packed-pixel image. Setting this
  1947. * parameter to 0 is the equivalent of setting it to
  1948. * <tt>width * #tjPixelSize[pixelFormat]</tt>.
  1949. *
  1950. * @param height height (in pixels) of the packed-pixel image
  1951. *
  1952. * @param pixelFormat pixel format of the packed-pixel image (see @ref TJPF
  1953. * "Pixel formats".) If this parameter is set to @ref TJPF_GRAY, then the
  1954. * image will be stored in PGM or 8-bit-per-pixel (indexed color) BMP format.
  1955. * Otherwise, the image will be stored in PPM or 24-bit-per-pixel BMP format.
  1956. * If this parameter is set to @ref TJPF_CMYK, then the CMYK pixels will be
  1957. * converted to RGB using a quick & dirty algorithm that is suitable only for
  1958. * testing purposes. (Proper conversion between CMYK and other formats
  1959. * requires a color management system.)
  1960. *
  1961. * @return 0 if successful, or -1 if an error occurred (see #tj3GetErrorStr().)
  1962. */
  1963. DLLEXPORT int tj3SaveImage8(tjhandle handle, const char *filename,
  1964. const unsigned char *buffer, int width, int pitch,
  1965. int height, int pixelFormat);
  1966. /**
  1967. * Save a 12-bit-per-sample packed-pixel image from memory to disk.
  1968. *
  1969. * \details \copydetails tj3SaveImage8()
  1970. */
  1971. DLLEXPORT int tj3SaveImage12(tjhandle handle, const char *filename,
  1972. const short *buffer, int width, int pitch,
  1973. int height, int pixelFormat);
  1974. /**
  1975. * Save a 16-bit-per-sample packed-pixel image from memory to disk.
  1976. *
  1977. * \details \copydetails tj3SaveImage8()
  1978. */
  1979. DLLEXPORT int tj3SaveImage16(tjhandle handle, const char *filename,
  1980. const unsigned short *buffer, int width,
  1981. int pitch, int height, int pixelFormat);
  1982. /**
  1983. * Free a byte buffer previously allocated by TurboJPEG. You should always use
  1984. * this function to free JPEG destination buffer(s) that were automatically
  1985. * (re)allocated by the compression and transform functions or that were
  1986. * manually allocated using #tj3Alloc().
  1987. *
  1988. * @param buffer address of the buffer to free. If the address is NULL, then
  1989. * this function has no effect.
  1990. *
  1991. * @see tj3Alloc()
  1992. */
  1993. DLLEXPORT void tj3Free(void *buffer);
  1994. /**
  1995. * Returns a descriptive error message explaining why the last command failed.
  1996. *
  1997. * @param handle handle to a TurboJPEG instance, or NULL if the error was
  1998. * generated by a global function (but note that retrieving the error message
  1999. * for a global function is thread-safe only on platforms that support
  2000. * thread-local storage.)
  2001. *
  2002. * @return a descriptive error message explaining why the last command failed.
  2003. */
  2004. DLLEXPORT char *tj3GetErrorStr(tjhandle handle);
  2005. /**
  2006. * Returns a code indicating the severity of the last error. See
  2007. * @ref TJERR "Error codes".
  2008. *
  2009. * @param handle handle to a TurboJPEG instance
  2010. *
  2011. * @return a code indicating the severity of the last error. See
  2012. * @ref TJERR "Error codes".
  2013. */
  2014. DLLEXPORT int tj3GetErrorCode(tjhandle handle);
  2015. /* Backward compatibility functions and macros (nothing to see here) */
  2016. /* TurboJPEG 1.0+ */
  2017. #define NUMSUBOPT TJ_NUMSAMP
  2018. #define TJ_444 TJSAMP_444
  2019. #define TJ_422 TJSAMP_422
  2020. #define TJ_420 TJSAMP_420
  2021. #define TJ_411 TJSAMP_420
  2022. #define TJ_GRAYSCALE TJSAMP_GRAY
  2023. #define TJ_BGR 1
  2024. #define TJ_BOTTOMUP TJFLAG_BOTTOMUP
  2025. #define TJ_FORCEMMX TJFLAG_FORCEMMX
  2026. #define TJ_FORCESSE TJFLAG_FORCESSE
  2027. #define TJ_FORCESSE2 TJFLAG_FORCESSE2
  2028. #define TJ_ALPHAFIRST 64
  2029. #define TJ_FORCESSE3 TJFLAG_FORCESSE3
  2030. #define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE
  2031. #define TJPAD(width) (((width) + 3) & (~3))
  2032. DLLEXPORT unsigned long TJBUFSIZE(int width, int height);
  2033. DLLEXPORT int tjCompress(tjhandle handle, unsigned char *srcBuf, int width,
  2034. int pitch, int height, int pixelSize,
  2035. unsigned char *dstBuf, unsigned long *compressedSize,
  2036. int jpegSubsamp, int jpegQual, int flags);
  2037. DLLEXPORT int tjDecompress(tjhandle handle, unsigned char *jpegBuf,
  2038. unsigned long jpegSize, unsigned char *dstBuf,
  2039. int width, int pitch, int height, int pixelSize,
  2040. int flags);
  2041. DLLEXPORT int tjDecompressHeader(tjhandle handle, unsigned char *jpegBuf,
  2042. unsigned long jpegSize, int *width,
  2043. int *height);
  2044. DLLEXPORT int tjDestroy(tjhandle handle);
  2045. DLLEXPORT char *tjGetErrorStr(void);
  2046. DLLEXPORT tjhandle tjInitCompress(void);
  2047. DLLEXPORT tjhandle tjInitDecompress(void);
  2048. /* TurboJPEG 1.1+ */
  2049. #define TJ_YUV 512
  2050. DLLEXPORT unsigned long TJBUFSIZEYUV(int width, int height, int jpegSubsamp);
  2051. DLLEXPORT int tjDecompressHeader2(tjhandle handle, unsigned char *jpegBuf,
  2052. unsigned long jpegSize, int *width,
  2053. int *height, int *jpegSubsamp);
  2054. DLLEXPORT int tjDecompressToYUV(tjhandle handle, unsigned char *jpegBuf,
  2055. unsigned long jpegSize, unsigned char *dstBuf,
  2056. int flags);
  2057. DLLEXPORT int tjEncodeYUV(tjhandle handle, unsigned char *srcBuf, int width,
  2058. int pitch, int height, int pixelSize,
  2059. unsigned char *dstBuf, int subsamp, int flags);
  2060. /* TurboJPEG 1.2+ */
  2061. #define TJFLAG_BOTTOMUP 2
  2062. #define TJFLAG_FORCEMMX 8
  2063. #define TJFLAG_FORCESSE 16
  2064. #define TJFLAG_FORCESSE2 32
  2065. #define TJFLAG_FORCESSE3 128
  2066. #define TJFLAG_FASTUPSAMPLE 256
  2067. #define TJFLAG_NOREALLOC 1024
  2068. DLLEXPORT unsigned char *tjAlloc(int bytes);
  2069. DLLEXPORT unsigned long tjBufSize(int width, int height, int jpegSubsamp);
  2070. DLLEXPORT unsigned long tjBufSizeYUV(int width, int height, int subsamp);
  2071. DLLEXPORT int tjCompress2(tjhandle handle, const unsigned char *srcBuf,
  2072. int width, int pitch, int height, int pixelFormat,
  2073. unsigned char **jpegBuf, unsigned long *jpegSize,
  2074. int jpegSubsamp, int jpegQual, int flags);
  2075. DLLEXPORT int tjDecompress2(tjhandle handle, const unsigned char *jpegBuf,
  2076. unsigned long jpegSize, unsigned char *dstBuf,
  2077. int width, int pitch, int height, int pixelFormat,
  2078. int flags);
  2079. DLLEXPORT int tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, int width,
  2080. int pitch, int height, int pixelFormat,
  2081. unsigned char *dstBuf, int subsamp, int flags);
  2082. DLLEXPORT void tjFree(unsigned char *buffer);
  2083. DLLEXPORT tjscalingfactor *tjGetScalingFactors(int *numscalingfactors);
  2084. DLLEXPORT tjhandle tjInitTransform(void);
  2085. DLLEXPORT int tjTransform(tjhandle handle, const unsigned char *jpegBuf,
  2086. unsigned long jpegSize, int n,
  2087. unsigned char **dstBufs, unsigned long *dstSizes,
  2088. tjtransform *transforms, int flags);
  2089. /* TurboJPEG 1.2.1+ */
  2090. #define TJFLAG_FASTDCT 2048
  2091. #define TJFLAG_ACCURATEDCT 4096
  2092. /* TurboJPEG 1.4+ */
  2093. DLLEXPORT unsigned long tjBufSizeYUV2(int width, int align, int height,
  2094. int subsamp);
  2095. DLLEXPORT int tjCompressFromYUV(tjhandle handle, const unsigned char *srcBuf,
  2096. int width, int align, int height, int subsamp,
  2097. unsigned char **jpegBuf,
  2098. unsigned long *jpegSize, int jpegQual,
  2099. int flags);
  2100. DLLEXPORT int tjCompressFromYUVPlanes(tjhandle handle,
  2101. const unsigned char **srcPlanes,
  2102. int width, const int *strides,
  2103. int height, int subsamp,
  2104. unsigned char **jpegBuf,
  2105. unsigned long *jpegSize, int jpegQual,
  2106. int flags);
  2107. DLLEXPORT int tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
  2108. int align, int subsamp, unsigned char *dstBuf,
  2109. int width, int pitch, int height, int pixelFormat,
  2110. int flags);
  2111. DLLEXPORT int tjDecodeYUVPlanes(tjhandle handle,
  2112. const unsigned char **srcPlanes,
  2113. const int *strides, int subsamp,
  2114. unsigned char *dstBuf, int width, int pitch,
  2115. int height, int pixelFormat, int flags);
  2116. DLLEXPORT int tjDecompressHeader3(tjhandle handle,
  2117. const unsigned char *jpegBuf,
  2118. unsigned long jpegSize, int *width,
  2119. int *height, int *jpegSubsamp,
  2120. int *jpegColorspace);
  2121. DLLEXPORT int tjDecompressToYUV2(tjhandle handle, const unsigned char *jpegBuf,
  2122. unsigned long jpegSize, unsigned char *dstBuf,
  2123. int width, int align, int height, int flags);
  2124. DLLEXPORT int tjDecompressToYUVPlanes(tjhandle handle,
  2125. const unsigned char *jpegBuf,
  2126. unsigned long jpegSize,
  2127. unsigned char **dstPlanes, int width,
  2128. int *strides, int height, int flags);
  2129. DLLEXPORT int tjEncodeYUV3(tjhandle handle, const unsigned char *srcBuf,
  2130. int width, int pitch, int height, int pixelFormat,
  2131. unsigned char *dstBuf, int align, int subsamp,
  2132. int flags);
  2133. DLLEXPORT int tjEncodeYUVPlanes(tjhandle handle, const unsigned char *srcBuf,
  2134. int width, int pitch, int height,
  2135. int pixelFormat, unsigned char **dstPlanes,
  2136. int *strides, int subsamp, int flags);
  2137. DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp);
  2138. DLLEXPORT unsigned long tjPlaneSizeYUV(int componentID, int width, int stride,
  2139. int height, int subsamp);
  2140. DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp);
  2141. /* TurboJPEG 2.0+ */
  2142. #define TJFLAG_STOPONWARNING 8192
  2143. #define TJFLAG_PROGRESSIVE 16384
  2144. DLLEXPORT int tjGetErrorCode(tjhandle handle);
  2145. DLLEXPORT char *tjGetErrorStr2(tjhandle handle);
  2146. DLLEXPORT unsigned char *tjLoadImage(const char *filename, int *width,
  2147. int align, int *height, int *pixelFormat,
  2148. int flags);
  2149. DLLEXPORT int tjSaveImage(const char *filename, unsigned char *buffer,
  2150. int width, int pitch, int height, int pixelFormat,
  2151. int flags);
  2152. /* TurboJPEG 2.1+ */
  2153. #define TJFLAG_LIMITSCANS 32768
  2154. /**
  2155. * @}
  2156. */
  2157. #ifdef __cplusplus
  2158. }
  2159. #endif
  2160. #endif