turbojpeg.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /*
  2. * Copyright (C)2009-2013 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. * @{
  42. */
  43. /**
  44. * The number of chrominance subsampling options
  45. */
  46. #define TJ_NUMSAMP 5
  47. /**
  48. * Chrominance subsampling options.
  49. * When an image is converted from the RGB to the YCbCr colorspace as part of
  50. * the JPEG compression process, some of the Cb and Cr (chrominance) components
  51. * can be discarded or averaged together to produce a smaller image with little
  52. * perceptible loss of image clarity (the human eye is more sensitive to small
  53. * changes in brightness than small changes in color.) This is called
  54. * "chrominance subsampling".
  55. * <p>
  56. * NOTE: Technically, the JPEG format uses the YCbCr colorspace, but per the
  57. * convention of the digital video community, the TurboJPEG API uses "YUV" to
  58. * refer to an image format consisting of Y, Cb, and Cr image planes.
  59. */
  60. enum TJSAMP
  61. {
  62. /**
  63. * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG or
  64. * YUV image will contain one chrominance component for every pixel in the
  65. * source image.
  66. */
  67. TJSAMP_444=0,
  68. /**
  69. * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one
  70. * chrominance component for every 2x1 block of pixels in the source image.
  71. */
  72. TJSAMP_422,
  73. /**
  74. * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one
  75. * chrominance component for every 2x2 block of pixels in the source image.
  76. */
  77. TJSAMP_420,
  78. /**
  79. * Grayscale. The JPEG or YUV image will contain no chrominance components.
  80. */
  81. TJSAMP_GRAY,
  82. /**
  83. * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one
  84. * chrominance component for every 1x2 block of pixels in the source image.
  85. * Note that 4:4:0 subsampling is not fully accelerated in libjpeg-turbo.
  86. */
  87. TJSAMP_440
  88. };
  89. /**
  90. * MCU block width (in pixels) for a given level of chrominance subsampling.
  91. * MCU block sizes:
  92. * - 8x8 for no subsampling or grayscale
  93. * - 16x8 for 4:2:2
  94. * - 8x16 for 4:4:0
  95. * - 16x16 for 4:2:0
  96. */
  97. static const int tjMCUWidth[TJ_NUMSAMP] = {8, 16, 16, 8, 8};
  98. /**
  99. * MCU block height (in pixels) for a given level of chrominance subsampling.
  100. * MCU block sizes:
  101. * - 8x8 for no subsampling or grayscale
  102. * - 16x8 for 4:2:2
  103. * - 8x16 for 4:4:0
  104. * - 16x16 for 4:2:0
  105. */
  106. static const int tjMCUHeight[TJ_NUMSAMP] = {8, 8, 16, 8, 16};
  107. /**
  108. * The number of pixel formats
  109. */
  110. #define TJ_NUMPF 11
  111. /**
  112. * Pixel formats
  113. */
  114. enum TJPF
  115. {
  116. /**
  117. * RGB pixel format. The red, green, and blue components in the image are
  118. * stored in 3-byte pixels in the order R, G, B from lowest to highest byte
  119. * address within each pixel.
  120. */
  121. TJPF_RGB=0,
  122. /**
  123. * BGR pixel format. The red, green, and blue components in the image are
  124. * stored in 3-byte pixels in the order B, G, R from lowest to highest byte
  125. * address within each pixel.
  126. */
  127. TJPF_BGR,
  128. /**
  129. * RGBX pixel format. The red, green, and blue components in the image are
  130. * stored in 4-byte pixels in the order R, G, B from lowest to highest byte
  131. * address within each pixel. The X component is ignored when compressing
  132. * and undefined when decompressing.
  133. */
  134. TJPF_RGBX,
  135. /**
  136. * BGRX pixel format. The red, green, and blue components in the image are
  137. * stored in 4-byte pixels in the order B, G, R from lowest to highest byte
  138. * address within each pixel. The X component is ignored when compressing
  139. * and undefined when decompressing.
  140. */
  141. TJPF_BGRX,
  142. /**
  143. * XBGR pixel format. The red, green, and blue components in the image are
  144. * stored in 4-byte pixels in the order R, G, B from highest to lowest byte
  145. * address within each pixel. The X component is ignored when compressing
  146. * and undefined when decompressing.
  147. */
  148. TJPF_XBGR,
  149. /**
  150. * XRGB pixel format. The red, green, and blue components in the image are
  151. * stored in 4-byte pixels in the order B, G, R from highest to lowest byte
  152. * address within each pixel. The X component is ignored when compressing
  153. * and undefined when decompressing.
  154. */
  155. TJPF_XRGB,
  156. /**
  157. * Grayscale pixel format. Each 1-byte pixel represents a luminance
  158. * (brightness) level from 0 to 255.
  159. */
  160. TJPF_GRAY,
  161. /**
  162. * RGBA pixel format. This is the same as @ref TJPF_RGBX, except that when
  163. * decompressing, the X component is guaranteed to be 0xFF, which can be
  164. * interpreted as an opaque alpha channel.
  165. */
  166. TJPF_RGBA,
  167. /**
  168. * BGRA pixel format. This is the same as @ref TJPF_BGRX, except that when
  169. * decompressing, the X component is guaranteed to be 0xFF, which can be
  170. * interpreted as an opaque alpha channel.
  171. */
  172. TJPF_BGRA,
  173. /**
  174. * ABGR pixel format. This is the same as @ref TJPF_XBGR, except that when
  175. * decompressing, the X component is guaranteed to be 0xFF, which can be
  176. * interpreted as an opaque alpha channel.
  177. */
  178. TJPF_ABGR,
  179. /**
  180. * ARGB pixel format. This is the same as @ref TJPF_XRGB, except that when
  181. * decompressing, the X component is guaranteed to be 0xFF, which can be
  182. * interpreted as an opaque alpha channel.
  183. */
  184. TJPF_ARGB
  185. };
  186. /**
  187. * Red offset (in bytes) for a given pixel format. This specifies the number
  188. * of bytes that the red component is offset from the start of the pixel. For
  189. * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
  190. * then the red component will be <tt>pixel[tjRedOffset[TJ_BGRX]]</tt>.
  191. */
  192. static const int tjRedOffset[TJ_NUMPF] = {0, 2, 0, 2, 3, 1, 0, 0, 2, 3, 1};
  193. /**
  194. * Green offset (in bytes) for a given pixel format. This specifies the number
  195. * of bytes that the green component is offset from the start of the pixel.
  196. * For instance, if a pixel of format TJ_BGRX is stored in
  197. * <tt>char pixel[]</tt>, then the green component will be
  198. * <tt>pixel[tjGreenOffset[TJ_BGRX]]</tt>.
  199. */
  200. static const int tjGreenOffset[TJ_NUMPF] = {1, 1, 1, 1, 2, 2, 0, 1, 1, 2, 2};
  201. /**
  202. * Blue offset (in bytes) for a given pixel format. This specifies the number
  203. * of bytes that the Blue component is offset from the start of the pixel. For
  204. * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>,
  205. * then the blue component will be <tt>pixel[tjBlueOffset[TJ_BGRX]]</tt>.
  206. */
  207. static const int tjBlueOffset[TJ_NUMPF] = {2, 0, 2, 0, 1, 3, 0, 2, 0, 1, 3};
  208. /**
  209. * Pixel size (in bytes) for a given pixel format.
  210. */
  211. static const int tjPixelSize[TJ_NUMPF] = {3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4};
  212. /**
  213. * The uncompressed source/destination image is stored in bottom-up (Windows,
  214. * OpenGL) order, not top-down (X11) order.
  215. */
  216. #define TJFLAG_BOTTOMUP 2
  217. /**
  218. * Turn off CPU auto-detection and force TurboJPEG to use MMX code (if the
  219. * underlying codec supports it.)
  220. */
  221. #define TJFLAG_FORCEMMX 8
  222. /**
  223. * Turn off CPU auto-detection and force TurboJPEG to use SSE code (if the
  224. * underlying codec supports it.)
  225. */
  226. #define TJFLAG_FORCESSE 16
  227. /**
  228. * Turn off CPU auto-detection and force TurboJPEG to use SSE2 code (if the
  229. * underlying codec supports it.)
  230. */
  231. #define TJFLAG_FORCESSE2 32
  232. /**
  233. * Turn off CPU auto-detection and force TurboJPEG to use SSE3 code (if the
  234. * underlying codec supports it.)
  235. */
  236. #define TJFLAG_FORCESSE3 128
  237. /**
  238. * When decompressing an image that was compressed using chrominance
  239. * subsampling, use the fastest chrominance upsampling algorithm available in
  240. * the underlying codec. The default is to use smooth upsampling, which
  241. * creates a smooth transition between neighboring chrominance components in
  242. * order to reduce upsampling artifacts in the decompressed image.
  243. */
  244. #define TJFLAG_FASTUPSAMPLE 256
  245. /**
  246. * Disable buffer (re)allocation. If passed to #tjCompress2() or
  247. * #tjTransform(), this flag will cause those functions to generate an error if
  248. * the JPEG image buffer is invalid or too small rather than attempting to
  249. * allocate or reallocate that buffer. This reproduces the behavior of earlier
  250. * versions of TurboJPEG.
  251. */
  252. #define TJFLAG_NOREALLOC 1024
  253. /**
  254. * Use the fastest DCT/IDCT algorithm available in the underlying codec. The
  255. * default if this flag is not specified is implementation-specific. For
  256. * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
  257. * algorithm by default when compressing, because this has been shown to have
  258. * only a very slight effect on accuracy, but it uses the accurate algorithm
  259. * when decompressing, because this has been shown to have a larger effect.
  260. */
  261. #define TJFLAG_FASTDCT 2048
  262. /**
  263. * Use the most accurate DCT/IDCT algorithm available in the underlying codec.
  264. * The default if this flag is not specified is implementation-specific. For
  265. * example, the implementation of TurboJPEG for libjpeg[-turbo] uses the fast
  266. * algorithm by default when compressing, because this has been shown to have
  267. * only a very slight effect on accuracy, but it uses the accurate algorithm
  268. * when decompressing, because this has been shown to have a larger effect.
  269. */
  270. #define TJFLAG_ACCURATEDCT 4096
  271. /**
  272. * The number of transform operations
  273. */
  274. #define TJ_NUMXOP 8
  275. /**
  276. * Transform operations for #tjTransform()
  277. */
  278. enum TJXOP
  279. {
  280. /**
  281. * Do not transform the position of the image pixels
  282. */
  283. TJXOP_NONE=0,
  284. /**
  285. * Flip (mirror) image horizontally. This transform is imperfect if there
  286. * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.)
  287. */
  288. TJXOP_HFLIP,
  289. /**
  290. * Flip (mirror) image vertically. This transform is imperfect if there are
  291. * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.)
  292. */
  293. TJXOP_VFLIP,
  294. /**
  295. * Transpose image (flip/mirror along upper left to lower right axis.) This
  296. * transform is always perfect.
  297. */
  298. TJXOP_TRANSPOSE,
  299. /**
  300. * Transverse transpose image (flip/mirror along upper right to lower left
  301. * axis.) This transform is imperfect if there are any partial MCU blocks in
  302. * the image (see #TJXOPT_PERFECT.)
  303. */
  304. TJXOP_TRANSVERSE,
  305. /**
  306. * Rotate image clockwise by 90 degrees. This transform is imperfect if
  307. * there are any partial MCU blocks on the bottom edge (see
  308. * #TJXOPT_PERFECT.)
  309. */
  310. TJXOP_ROT90,
  311. /**
  312. * Rotate image 180 degrees. This transform is imperfect if there are any
  313. * partial MCU blocks in the image (see #TJXOPT_PERFECT.)
  314. */
  315. TJXOP_ROT180,
  316. /**
  317. * Rotate image counter-clockwise by 90 degrees. This transform is imperfect
  318. * if there are any partial MCU blocks on the right edge (see
  319. * #TJXOPT_PERFECT.)
  320. */
  321. TJXOP_ROT270
  322. };
  323. /**
  324. * This option will cause #tjTransform() to return an error if the transform is
  325. * not perfect. Lossless transforms operate on MCU blocks, whose size depends
  326. * on the level of chrominance subsampling used (see #tjMCUWidth
  327. * and #tjMCUHeight.) If the image's width or height is not evenly divisible
  328. * by the MCU block size, then there will be partial MCU blocks on the right
  329. * and/or bottom edges. It is not possible to move these partial MCU blocks to
  330. * the top or left of the image, so any transform that would require that is
  331. * "imperfect." If this option is not specified, then any partial MCU blocks
  332. * that cannot be transformed will be left in place, which will create
  333. * odd-looking strips on the right or bottom edge of the image.
  334. */
  335. #define TJXOPT_PERFECT 1
  336. /**
  337. * This option will cause #tjTransform() to discard any partial MCU blocks that
  338. * cannot be transformed.
  339. */
  340. #define TJXOPT_TRIM 2
  341. /**
  342. * This option will enable lossless cropping. See #tjTransform() for more
  343. * information.
  344. */
  345. #define TJXOPT_CROP 4
  346. /**
  347. * This option will discard the color data in the input image and produce
  348. * a grayscale output image.
  349. */
  350. #define TJXOPT_GRAY 8
  351. /**
  352. * This option will prevent #tjTransform() from outputting a JPEG image for
  353. * this particular transform (this can be used in conjunction with a custom
  354. * filter to capture the transformed DCT coefficients without transcoding
  355. * them.)
  356. */
  357. #define TJXOPT_NOOUTPUT 16
  358. /**
  359. * Scaling factor
  360. */
  361. typedef struct
  362. {
  363. /**
  364. * Numerator
  365. */
  366. int num;
  367. /**
  368. * Denominator
  369. */
  370. int denom;
  371. } tjscalingfactor;
  372. /**
  373. * Cropping region
  374. */
  375. typedef struct
  376. {
  377. /**
  378. * The left boundary of the cropping region. This must be evenly divisible
  379. * by the MCU block width (see #tjMCUWidth.)
  380. */
  381. int x;
  382. /**
  383. * The upper boundary of the cropping region. This must be evenly divisible
  384. * by the MCU block height (see #tjMCUHeight.)
  385. */
  386. int y;
  387. /**
  388. * The width of the cropping region. Setting this to 0 is the equivalent of
  389. * setting it to the width of the source JPEG image - x.
  390. */
  391. int w;
  392. /**
  393. * The height of the cropping region. Setting this to 0 is the equivalent of
  394. * setting it to the height of the source JPEG image - y.
  395. */
  396. int h;
  397. } tjregion;
  398. /**
  399. * Lossless transform
  400. */
  401. typedef struct tjtransform
  402. {
  403. /**
  404. * Cropping region
  405. */
  406. tjregion r;
  407. /**
  408. * One of the @ref TJXOP "transform operations"
  409. */
  410. int op;
  411. /**
  412. * The bitwise OR of one of more of the @ref TJXOPT_CROP "transform options"
  413. */
  414. int options;
  415. /**
  416. * Arbitrary data that can be accessed within the body of the callback
  417. * function
  418. */
  419. void *data;
  420. /**
  421. * A callback function that can be used to modify the DCT coefficients
  422. * after they are losslessly transformed but before they are transcoded to a
  423. * new JPEG image. This allows for custom filters or other transformations
  424. * to be applied in the frequency domain.
  425. *
  426. * @param coeffs pointer to an array of transformed DCT coefficients. (NOTE:
  427. * this pointer is not guaranteed to be valid once the callback
  428. * returns, so applications wishing to hand off the DCT coefficients
  429. * to another function or library should make a copy of them within
  430. * the body of the callback.)
  431. * @param arrayRegion #tjregion structure containing the width and height of
  432. * the array pointed to by <tt>coeffs</tt> as well as its offset
  433. * relative to the component plane. TurboJPEG implementations may
  434. * choose to split each component plane into multiple DCT coefficient
  435. * arrays and call the callback function once for each array.
  436. * @param planeRegion #tjregion structure containing the width and height of
  437. * the component plane to which <tt>coeffs</tt> belongs
  438. * @param componentID ID number of the component plane to which
  439. * <tt>coeffs</tt> belongs (Y, Cb, and Cr have, respectively, ID's of
  440. * 0, 1, and 2 in typical JPEG images.)
  441. * @param transformID ID number of the transformed image to which
  442. * <tt>coeffs</tt> belongs. This is the same as the index of the
  443. * transform in the <tt>transforms</tt> array that was passed to
  444. * #tjTransform().
  445. * @param transform a pointer to a #tjtransform structure that specifies the
  446. * parameters and/or cropping region for this transform
  447. *
  448. * @return 0 if the callback was successful, or -1 if an error occurred.
  449. */
  450. int (*customFilter)(short *coeffs, tjregion arrayRegion,
  451. tjregion planeRegion, int componentIndex, int transformIndex,
  452. struct tjtransform *transform);
  453. } tjtransform;
  454. /**
  455. * TurboJPEG instance handle
  456. */
  457. typedef void* tjhandle;
  458. /**
  459. * Pad the given width to the nearest 32-bit boundary
  460. */
  461. #define TJPAD(width) (((width)+3)&(~3))
  462. /**
  463. * Compute the scaled value of <tt>dimension</tt> using the given scaling
  464. * factor. This macro performs the integer equivalent of <tt>ceil(dimension *
  465. * scalingFactor)</tt>.
  466. */
  467. #define TJSCALED(dimension, scalingFactor) ((dimension * scalingFactor.num \
  468. + scalingFactor.denom - 1) / scalingFactor.denom)
  469. #ifdef __cplusplus
  470. extern "C" {
  471. #endif
  472. /**
  473. * Create a TurboJPEG compressor instance.
  474. *
  475. * @return a handle to the newly-created instance, or NULL if an error
  476. * occurred (see #tjGetErrorStr().)
  477. */
  478. DLLEXPORT tjhandle DLLCALL tjInitCompress(void);
  479. /**
  480. * Compress an RGB or grayscale image into a JPEG image.
  481. *
  482. * @param handle a handle to a TurboJPEG compressor or transformer instance
  483. * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
  484. * to be compressed
  485. * @param width width (in pixels) of the source image
  486. * @param pitch bytes per line of the source image. Normally, this should be
  487. * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded,
  488. * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of
  489. * the image is padded to the nearest 32-bit boundary, as is the case
  490. * for Windows bitmaps. You can also be clever and use this parameter
  491. * to skip lines, etc. Setting this parameter to 0 is the equivalent of
  492. * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
  493. * @param height height (in pixels) of the source image
  494. * @param pixelFormat pixel format of the source image (see @ref TJPF
  495. * "Pixel formats".)
  496. * @param jpegBuf address of a pointer to an image buffer that will receive the
  497. * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer
  498. * to accommodate the size of the JPEG image. Thus, you can choose to:
  499. * -# pre-allocate the JPEG buffer with an arbitrary size using
  500. * #tjAlloc() and let TurboJPEG grow the buffer as needed,
  501. * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the
  502. * buffer for you, or
  503. * -# pre-allocate the buffer to a "worst case" size determined by
  504. * calling #tjBufSize(). This should ensure that the buffer never has
  505. * to be re-allocated (setting #TJFLAG_NOREALLOC guarantees this.)
  506. * .
  507. * If you choose option 1, <tt>*jpegSize</tt> should be set to the
  508. * size of your pre-allocated buffer. In any case, unless you have
  509. * set #TJFLAG_NOREALLOC, you should always check <tt>*jpegBuf</tt> upon
  510. * return from this function, as it may have changed.
  511. * @param jpegSize pointer to an unsigned long variable that holds the size of
  512. * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a
  513. * pre-allocated buffer, then <tt>*jpegSize</tt> should be set to the
  514. * size of the buffer. Upon return, <tt>*jpegSize</tt> will contain the
  515. * size of the JPEG image (in bytes.)
  516. * @param jpegSubsamp the level of chrominance subsampling to be used when
  517. * generating the JPEG image (see @ref TJSAMP
  518. * "Chrominance subsampling options".)
  519. * @param jpegQual the image quality of the generated JPEG image (1 = worst,
  520. 100 = best)
  521. * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
  522. * "flags".
  523. *
  524. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
  525. */
  526. DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf,
  527. int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf,
  528. unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags);
  529. /**
  530. * The maximum size of the buffer (in bytes) required to hold a JPEG image with
  531. * the given parameters. The number of bytes returned by this function is
  532. * larger than the size of the uncompressed source image. The reason for this
  533. * is that the JPEG format uses 16-bit coefficients, and it is thus possible
  534. * for a very high-quality JPEG image with very high-frequency content to
  535. * expand rather than compress when converted to the JPEG format. Such images
  536. * represent a very rare corner case, but since there is no way to predict the
  537. * size of a JPEG image prior to compression, the corner case has to be
  538. * handled.
  539. *
  540. * @param width width of the image (in pixels)
  541. * @param height height of the image (in pixels)
  542. * @param jpegSubsamp the level of chrominance subsampling to be used when
  543. * generating the JPEG image (see @ref TJSAMP
  544. * "Chrominance subsampling options".)
  545. *
  546. * @return the maximum size of the buffer (in bytes) required to hold the
  547. * image, or -1 if the arguments are out of bounds.
  548. */
  549. DLLEXPORT unsigned long DLLCALL tjBufSize(int width, int height,
  550. int jpegSubsamp);
  551. /**
  552. * The size of the buffer (in bytes) required to hold a YUV planar image with
  553. * the given parameters.
  554. *
  555. * @param width width of the image (in pixels)
  556. * @param height height of the image (in pixels)
  557. * @param subsamp level of chrominance subsampling in the image (see
  558. * @ref TJSAMP "Chrominance subsampling options".)
  559. *
  560. * @return the size of the buffer (in bytes) required to hold the image, or
  561. * -1 if the arguments are out of bounds.
  562. */
  563. DLLEXPORT unsigned long DLLCALL tjBufSizeYUV(int width, int height,
  564. int subsamp);
  565. /**
  566. * Encode an RGB or grayscale image into a YUV planar image. This function
  567. * uses the accelerated color conversion routines in TurboJPEG's underlying
  568. * codec to produce a planar YUV image that is suitable for X Video.
  569. * Specifically, if the chrominance components are subsampled along the
  570. * horizontal dimension, then the width of the luminance plane is padded to the
  571. * nearest multiple of 2 in the output image (same goes for the height of the
  572. * luminance plane, if the chrominance components are subsampled along the
  573. * vertical dimension.) Also, each line of each plane in the output image is
  574. * padded to 4 bytes. Although this will work with any subsampling option, it
  575. * is really only useful in combination with TJ_420, which produces an image
  576. * compatible with the I420 (AKA "YUV420P") format.
  577. * <p>
  578. * NOTE: Technically, the JPEG format uses the YCbCr colorspace, but per the
  579. * convention of the digital video community, the TurboJPEG API uses "YUV" to
  580. * refer to an image format consisting of Y, Cb, and Cr image planes.
  581. *
  582. * @param handle a handle to a TurboJPEG compressor or transformer instance
  583. * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels
  584. * to be encoded
  585. * @param width width (in pixels) of the source image
  586. * @param pitch bytes per line of the source image. Normally, this should be
  587. * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded,
  588. * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of
  589. * the image is padded to the nearest 32-bit boundary, as is the case
  590. * for Windows bitmaps. You can also be clever and use this parameter
  591. * to skip lines, etc. Setting this parameter to 0 is the equivalent of
  592. * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>.
  593. * @param height height (in pixels) of the source image
  594. * @param pixelFormat pixel format of the source image (see @ref TJPF
  595. * "Pixel formats".)
  596. * @param dstBuf pointer to an image buffer that will receive the YUV image.
  597. * Use #tjBufSizeYUV() to determine the appropriate size for this buffer
  598. * based on the image width, height, and level of chrominance
  599. * subsampling.
  600. * @param subsamp the level of chrominance subsampling to be used when
  601. * generating the YUV image (see @ref TJSAMP
  602. * "Chrominance subsampling options".)
  603. * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
  604. * "flags".
  605. *
  606. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
  607. */
  608. DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle,
  609. unsigned char *srcBuf, int width, int pitch, int height, int pixelFormat,
  610. unsigned char *dstBuf, int subsamp, int flags);
  611. /**
  612. * Create a TurboJPEG decompressor instance.
  613. *
  614. * @return a handle to the newly-created instance, or NULL if an error
  615. * occurred (see #tjGetErrorStr().)
  616. */
  617. DLLEXPORT tjhandle DLLCALL tjInitDecompress(void);
  618. /**
  619. * Retrieve information about a JPEG image without decompressing it.
  620. *
  621. * @param handle a handle to a TurboJPEG decompressor or transformer instance
  622. * @param jpegBuf pointer to a buffer containing a JPEG image
  623. * @param jpegSize size of the JPEG image (in bytes)
  624. * @param width pointer to an integer variable that will receive the width (in
  625. * pixels) of the JPEG image
  626. * @param height pointer to an integer variable that will receive the height
  627. * (in pixels) of the JPEG image
  628. * @param jpegSubsamp pointer to an integer variable that will receive the
  629. * level of chrominance subsampling used when compressing the JPEG image
  630. * (see @ref TJSAMP "Chrominance subsampling options".)
  631. *
  632. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
  633. */
  634. DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle,
  635. unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height,
  636. int *jpegSubsamp);
  637. /**
  638. * Returns a list of fractional scaling factors that the JPEG decompressor in
  639. * this implementation of TurboJPEG supports.
  640. *
  641. * @param numscalingfactors pointer to an integer variable that will receive
  642. * the number of elements in the list
  643. *
  644. * @return a pointer to a list of fractional scaling factors, or NULL if an
  645. * error is encountered (see #tjGetErrorStr().)
  646. */
  647. DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors);
  648. /**
  649. * Decompress a JPEG image to an RGB or grayscale image.
  650. *
  651. * @param handle a handle to a TurboJPEG decompressor or transformer instance
  652. * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
  653. * @param jpegSize size of the JPEG image (in bytes)
  654. * @param dstBuf pointer to an image buffer that will receive the decompressed
  655. * image. This buffer should normally be <tt>pitch * scaledHeight</tt>
  656. * bytes in size, where <tt>scaledHeight</tt> can be determined by
  657. * calling #TJSCALED() with the JPEG image height and one of the scaling
  658. * factors returned by #tjGetScalingFactors(). The <tt>dstBuf</tt>
  659. * pointer may also be used to decompress into a specific region of a
  660. * larger buffer.
  661. * @param width desired width (in pixels) of the destination image. If this is
  662. * different than the width of the JPEG image being decompressed, then
  663. * TurboJPEG will use scaling in the JPEG decompressor to generate the
  664. * largest possible image that will fit within the desired width. If
  665. * <tt>width</tt> is set to 0, then only the height will be considered
  666. * when determining the scaled image size.
  667. * @param pitch bytes per line of the destination image. Normally, this is
  668. * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed
  669. * image is unpadded, else <tt>#TJPAD(scaledWidth *
  670. * #tjPixelSize[pixelFormat])</tt> if each line of the decompressed
  671. * image is padded to the nearest 32-bit boundary, as is the case for
  672. * Windows bitmaps. (NOTE: <tt>scaledWidth</tt> can be determined by
  673. * calling #TJSCALED() with the JPEG image width and one of the scaling
  674. * factors returned by #tjGetScalingFactors().) You can also be clever
  675. * and use the pitch parameter to skip lines, etc. Setting this
  676. * parameter to 0 is the equivalent of setting it to <tt>scaledWidth
  677. * * #tjPixelSize[pixelFormat]</tt>.
  678. * @param height desired height (in pixels) of the destination image. If this
  679. * is different than the height of the JPEG image being decompressed,
  680. * then TurboJPEG will use scaling in the JPEG decompressor to generate
  681. * the largest possible image that will fit within the desired height.
  682. * If <tt>height</tt> is set to 0, then only the width will be
  683. * considered when determining the scaled image size.
  684. * @param pixelFormat pixel format of the destination image (see @ref
  685. * TJPF "Pixel formats".)
  686. * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
  687. * "flags".
  688. *
  689. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
  690. */
  691. DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle,
  692. unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
  693. int width, int pitch, int height, int pixelFormat, int flags);
  694. /**
  695. * Decompress a JPEG image to a YUV planar image. This function performs JPEG
  696. * decompression but leaves out the color conversion step, so a planar YUV
  697. * image is generated instead of an RGB image. The padding of the planes in
  698. * this image is the same as in the images generated by #tjEncodeYUV2(). Note
  699. * that, if the width or height of the image is not an even multiple of the MCU
  700. * block size (see #tjMCUWidth and #tjMCUHeight), then an intermediate buffer
  701. * copy will be performed within TurboJPEG.
  702. * <p>
  703. * NOTE: Technically, the JPEG format uses the YCbCr colorspace, but per the
  704. * convention of the digital video community, the TurboJPEG API uses "YUV" to
  705. * refer to an image format consisting of Y, Cb, and Cr image planes.
  706. *
  707. * @param handle a handle to a TurboJPEG decompressor or transformer instance
  708. * @param jpegBuf pointer to a buffer containing the JPEG image to decompress
  709. * @param jpegSize size of the JPEG image (in bytes)
  710. * @param dstBuf pointer to an image buffer that will receive the YUV image.
  711. * Use #tjBufSizeYUV() to determine the appropriate size for this buffer
  712. * based on the image width, height, and level of subsampling.
  713. * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
  714. * "flags".
  715. *
  716. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
  717. */
  718. DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle,
  719. unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
  720. int flags);
  721. /**
  722. * Create a new TurboJPEG transformer instance.
  723. *
  724. * @return a handle to the newly-created instance, or NULL if an error
  725. * occurred (see #tjGetErrorStr().)
  726. */
  727. DLLEXPORT tjhandle DLLCALL tjInitTransform(void);
  728. /**
  729. * Losslessly transform a JPEG image into another JPEG image. Lossless
  730. * transforms work by moving the raw coefficients from one JPEG image structure
  731. * to another without altering the values of the coefficients. While this is
  732. * typically faster than decompressing the image, transforming it, and
  733. * re-compressing it, lossless transforms are not free. Each lossless
  734. * transform requires reading and performing Huffman decoding on all of the
  735. * coefficients in the source image, regardless of the size of the destination
  736. * image. Thus, this function provides a means of generating multiple
  737. * transformed images from the same source or applying multiple
  738. * transformations simultaneously, in order to eliminate the need to read the
  739. * source coefficients multiple times.
  740. *
  741. * @param handle a handle to a TurboJPEG transformer instance
  742. * @param jpegBuf pointer to a buffer containing the JPEG image to transform
  743. * @param jpegSize size of the JPEG image (in bytes)
  744. * @param n the number of transformed JPEG images to generate
  745. * @param dstBufs pointer to an array of n image buffers. <tt>dstBufs[i]</tt>
  746. * will receive a JPEG image that has been transformed using the
  747. * parameters in <tt>transforms[i]</tt>. TurboJPEG has the ability to
  748. * reallocate the JPEG buffer to accommodate the size of the JPEG image.
  749. * Thus, you can choose to:
  750. * -# pre-allocate the JPEG buffer with an arbitrary size using
  751. * #tjAlloc() and let TurboJPEG grow the buffer as needed,
  752. * -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the
  753. * buffer for you, or
  754. * -# pre-allocate the buffer to a "worst case" size determined by
  755. * calling #tjBufSize() with the transformed or cropped width and
  756. * height. This should ensure that the buffer never has to be
  757. * re-allocated (setting #TJFLAG_NOREALLOC guarantees this.)
  758. * .
  759. * If you choose option 1, <tt>dstSizes[i]</tt> should be set to
  760. * the size of your pre-allocated buffer. In any case, unless you have
  761. * set #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt>
  762. * upon return from this function, as it may have changed.
  763. * @param dstSizes pointer to an array of n unsigned long variables that will
  764. * receive the actual sizes (in bytes) of each transformed JPEG image.
  765. * If <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then
  766. * <tt>dstSizes[i]</tt> should be set to the size of the buffer. Upon
  767. * return, <tt>dstSizes[i]</tt> will contain the size of the JPEG image
  768. * (in bytes.)
  769. * @param transforms pointer to an array of n #tjtransform structures, each of
  770. * which specifies the transform parameters and/or cropping region for
  771. * the corresponding transformed output image.
  772. * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP
  773. * "flags".
  774. *
  775. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
  776. */
  777. DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf,
  778. unsigned long jpegSize, int n, unsigned char **dstBufs,
  779. unsigned long *dstSizes, tjtransform *transforms, int flags);
  780. /**
  781. * Destroy a TurboJPEG compressor, decompressor, or transformer instance.
  782. *
  783. * @param handle a handle to a TurboJPEG compressor, decompressor or
  784. * transformer instance
  785. *
  786. * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().)
  787. */
  788. DLLEXPORT int DLLCALL tjDestroy(tjhandle handle);
  789. /**
  790. * Allocate an image buffer for use with TurboJPEG. You should always use
  791. * this function to allocate the JPEG destination buffer(s) for #tjCompress2()
  792. * and #tjTransform() unless you are disabling automatic buffer
  793. * (re)allocation (by setting #TJFLAG_NOREALLOC.)
  794. *
  795. * @param bytes the number of bytes to allocate
  796. *
  797. * @return a pointer to a newly-allocated buffer with the specified number of
  798. * bytes
  799. *
  800. * @sa tjFree()
  801. */
  802. DLLEXPORT unsigned char* DLLCALL tjAlloc(int bytes);
  803. /**
  804. * Free an image buffer previously allocated by TurboJPEG. You should always
  805. * use this function to free JPEG destination buffer(s) that were automatically
  806. * (re)allocated by #tjCompress2() or #tjTransform() or that were manually
  807. * allocated using #tjAlloc().
  808. *
  809. * @param buffer address of the buffer to free
  810. *
  811. * @sa tjAlloc()
  812. */
  813. DLLEXPORT void DLLCALL tjFree(unsigned char *buffer);
  814. /**
  815. * Returns a descriptive error message explaining why the last command failed.
  816. *
  817. * @return a descriptive error message explaining why the last command failed.
  818. */
  819. DLLEXPORT char* DLLCALL tjGetErrorStr(void);
  820. /* Backward compatibility functions and macros (nothing to see here) */
  821. #define NUMSUBOPT TJ_NUMSAMP
  822. #define TJ_444 TJSAMP_444
  823. #define TJ_422 TJSAMP_422
  824. #define TJ_420 TJSAMP_420
  825. #define TJ_411 TJSAMP_420
  826. #define TJ_GRAYSCALE TJSAMP_GRAY
  827. #define TJ_BGR 1
  828. #define TJ_BOTTOMUP TJFLAG_BOTTOMUP
  829. #define TJ_FORCEMMX TJFLAG_FORCEMMX
  830. #define TJ_FORCESSE TJFLAG_FORCESSE
  831. #define TJ_FORCESSE2 TJFLAG_FORCESSE2
  832. #define TJ_ALPHAFIRST 64
  833. #define TJ_FORCESSE3 TJFLAG_FORCESSE3
  834. #define TJ_FASTUPSAMPLE TJFLAG_FASTUPSAMPLE
  835. #define TJ_YUV 512
  836. DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height);
  837. DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
  838. int jpegSubsamp);
  839. DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf,
  840. int width, int pitch, int height, int pixelSize, unsigned char *dstBuf,
  841. unsigned long *compressedSize, int jpegSubsamp, int jpegQual, int flags);
  842. DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle,
  843. unsigned char *srcBuf, int width, int pitch, int height, int pixelSize,
  844. unsigned char *dstBuf, int subsamp, int flags);
  845. DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle,
  846. unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height);
  847. DLLEXPORT int DLLCALL tjDecompress(tjhandle handle,
  848. unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
  849. int width, int pitch, int height, int pixelSize, int flags);
  850. /**
  851. * @}
  852. */
  853. #ifdef __cplusplus
  854. }
  855. #endif
  856. #endif