turbojpeg.h 72 KB

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