1 module libjpeg.turbojpeg; 2 /* 3 * Copyright (C)2009-2013 D. R. Commander. All Rights Reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * - Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * - Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * - Neither the name of the libjpeg-turbo Project nor the names of its 14 * contributors may be used to endorse or promote products derived from this 15 * software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS", 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 31 /** 32 * @addtogroup TurboJPEG 33 * TurboJPEG API. This API provides an interface for generating, decoding, and 34 * transforming planar YUV and JPEG images in memory. 35 * 36 * @{ 37 */ 38 39 40 public import core.stdc.config : c_ulong; 41 42 /** 43 * The number of chrominance subsampling options 44 */ 45 enum TJ_NUMSAMP = 5; 46 47 /** 48 * Chrominance subsampling options. 49 * When an image is converted from the RGB to the YUV colorspace as part of 50 * the JPEG compression process, some of the U and V (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 */ 56 enum TJSAMP 57 { 58 /** 59 * 4:4:4 chrominance subsampling (no chrominance subsampling). The JPEG or 60 * YUV image will contain one chrominance component for every pixel in the 61 * source image. 62 */ 63 TJSAMP_444=0, 64 /** 65 * 4:2:2 chrominance subsampling. The JPEG or YUV image will contain one 66 * chrominance component for every 2x1 block of pixels in the source image. 67 */ 68 TJSAMP_422, 69 /** 70 * 4:2:0 chrominance subsampling. The JPEG or YUV image will contain one 71 * chrominance component for every 2x2 block of pixels in the source image. 72 */ 73 TJSAMP_420, 74 /** 75 * Grayscale. The JPEG or YUV image will contain no chrominance components. 76 */ 77 TJSAMP_GRAY, 78 /** 79 * 4:4:0 chrominance subsampling. The JPEG or YUV image will contain one 80 * chrominance component for every 1x2 block of pixels in the source image. 81 */ 82 TJSAMP_440 83 } 84 85 /** 86 * MCU block width (in pixels) for a given level of chrominance subsampling. 87 * MCU block sizes: 88 * - 8x8 for no subsampling or grayscale 89 * - 16x8 for 4:2:2 90 * - 8x16 for 4:4:0 91 * - 16x16 for 4:2:0 92 */ 93 immutable(int)[TJ_NUMSAMP] tjMCUWidth = [8, 16, 16, 8, 8]; 94 95 /** 96 * MCU block height (in pixels) for a given level of chrominance subsampling. 97 * MCU block sizes: 98 * - 8x8 for no subsampling or grayscale 99 * - 16x8 for 4:2:2 100 * - 8x16 for 4:4:0 101 * - 16x16 for 4:2:0 102 */ 103 immutable(int)[TJ_NUMSAMP] tjMCUHeight = [8, 8, 16, 8, 16]; 104 105 106 /** 107 * The number of pixel formats 108 */ 109 enum TJ_NUMPF = 11; 110 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 /** 188 * Red offset (in bytes) for a given pixel format. This specifies the number 189 * of bytes that the red component is offset from the start of the pixel. For 190 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>, 191 * then the red component will be <tt>pixel[tjRedOffset[TJ_BGRX]]</tt>. 192 */ 193 immutable(int)[TJ_NUMPF] tjRedOffset = [0, 2, 0, 2, 3, 1, 0, 0, 2, 3, 1]; 194 /** 195 * Green offset (in bytes) for a given pixel format. This specifies the number 196 * of bytes that the green component is offset from the start of the pixel. 197 * For instance, if a pixel of format TJ_BGRX is stored in 198 * <tt>char pixel[]</tt>, then the green component will be 199 * <tt>pixel[tjGreenOffset[TJ_BGRX]]</tt>. 200 */ 201 immutable(int)[TJ_NUMPF] tjGreenOffset = [1, 1, 1, 1, 2, 2, 0, 1, 1, 2, 2]; 202 /** 203 * Blue offset (in bytes) for a given pixel format. This specifies the number 204 * of bytes that the Blue component is offset from the start of the pixel. For 205 * instance, if a pixel of format TJ_BGRX is stored in <tt>char pixel[]</tt>, 206 * then the blue component will be <tt>pixel[tjBlueOffset[TJ_BGRX]]</tt>. 207 */ 208 immutable(int)[TJ_NUMPF] tjBlueOffset = [2, 0, 2, 0, 1, 3, 0, 2, 0, 1, 3]; 209 210 /** 211 * Pixel size (in bytes) for a given pixel format. 212 */ 213 immutable(int)[TJ_NUMPF] tjPixelSize = [3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4]; 214 215 216 /** 217 * The uncompressed source/destination image is stored in bottom-up (Windows, 218 * OpenGL) order, not top-down (X11) order. 219 */ 220 enum TJFLAG_BOTTOMUP = 2; 221 /** 222 * Turn off CPU auto-detection and force TurboJPEG to use MMX code (if the 223 * underlying codec supports it.) 224 */ 225 enum TJFLAG_FORCEMMX = 8; 226 /** 227 * Turn off CPU auto-detection and force TurboJPEG to use SSE code (if the 228 * underlying codec supports it.) 229 */ 230 enum TJFLAG_FORCESSE = 16; 231 /** 232 * Turn off CPU auto-detection and force TurboJPEG to use SSE2 code (if the 233 * underlying codec supports it.) 234 */ 235 enum TJFLAG_FORCESSE2 = 32; 236 /** 237 * Turn off CPU auto-detection and force TurboJPEG to use SSE3 code (if the 238 * underlying codec supports it.) 239 */ 240 enum TJFLAG_FORCESSE3 = 128; 241 /** 242 * When decompressing an image that was compressed using chrominance 243 * subsampling, use the fastest chrominance upsampling algorithm available in 244 * the underlying codec. The default is to use smooth upsampling, which 245 * creates a smooth transition between neighboring chrominance components in 246 * order to reduce upsampling artifacts in the decompressed image. 247 */ 248 enum TJFLAG_FASTUPSAMPLE = 256; 249 /** 250 * Disable buffer (re)allocation. If passed to #tjCompress2() or 251 * #tjTransform(), this flag will cause those functions to generate an error if 252 * the JPEG image buffer is invalid or too small rather than attempting to 253 * allocate or reallocate that buffer. This reproduces the behavior of earlier 254 * versions of TurboJPEG. 255 */ 256 enum TJFLAG_NOREALLOC = 1024; 257 /** 258 * Use the fastest DCT/IDCT algorithm available in the underlying codec. The 259 * default if this flag is not specified is implementation-specific. The 260 * libjpeg implementation, for example, uses the fast algorithm by default when 261 * compressing, because this has been shown to have only a very slight effect 262 * on accuracy, but it uses the accurate algorithm when decompressing, because 263 * this has been shown to have a larger effect. 264 */ 265 enum TJFLAG_FASTDCT = 2048; 266 /** 267 * Use the most accurate DCT/IDCT algorithm available in the underlying codec. 268 * The default if this flag is not specified is implementation-specific. The 269 * libjpeg implementation, for example, uses the fast algorithm by default when 270 * compressing, because this has been shown to have only a very slight effect 271 * on accuracy, but it uses the accurate algorithm when decompressing, because 272 * this has been shown to have a larger effect. 273 */ 274 enum TJFLAG_ACCURATEDCT = 4096; 275 276 277 /** 278 * The number of transform operations 279 */ 280 enum TJ_NUMXOP = 8; 281 282 /** 283 * Transform operations for #tjTransform() 284 */ 285 enum TJXOP 286 { 287 /** 288 * Do not transform the position of the image pixels 289 */ 290 TJXOP_NONE=0, 291 /** 292 * Flip (mirror) image horizontally. This transform is imperfect if there 293 * are any partial MCU blocks on the right edge (see #TJXOPT_PERFECT.) 294 */ 295 TJXOP_HFLIP, 296 /** 297 * Flip (mirror) image vertically. This transform is imperfect if there are 298 * any partial MCU blocks on the bottom edge (see #TJXOPT_PERFECT.) 299 */ 300 TJXOP_VFLIP, 301 /** 302 * Transpose image (flip/mirror along upper left to lower right axis.) This 303 * transform is always perfect. 304 */ 305 TJXOP_TRANSPOSE, 306 /** 307 * Transverse transpose image (flip/mirror along upper right to lower left 308 * axis.) This transform is imperfect if there are any partial MCU blocks in 309 * the image (see #TJXOPT_PERFECT.) 310 */ 311 TJXOP_TRANSVERSE, 312 /** 313 * Rotate image clockwise by 90 degrees. This transform is imperfect if 314 * there are any partial MCU blocks on the bottom edge (see 315 * #TJXOPT_PERFECT.) 316 */ 317 TJXOP_ROT90, 318 /** 319 * Rotate image 180 degrees. This transform is imperfect if there are any 320 * partial MCU blocks in the image (see #TJXOPT_PERFECT.) 321 */ 322 TJXOP_ROT180, 323 /** 324 * Rotate image counter-clockwise by 90 degrees. This transform is imperfect 325 * if there are any partial MCU blocks on the right edge (see 326 * #TJXOPT_PERFECT.) 327 */ 328 TJXOP_ROT270 329 } 330 331 332 /** 333 * This option will cause #tjTransform() to return an error if the transform is 334 * not perfect. Lossless transforms operate on MCU blocks, whose size depends 335 * on the level of chrominance subsampling used (see #tjMCUWidth 336 * and #tjMCUHeight.) If the image's width or height is not evenly divisible 337 * by the MCU block size, then there will be partial MCU blocks on the right 338 * and/or bottom edges. It is not possible to move these partial MCU blocks to 339 * the top or left of the image, so any transform that would require that is 340 * "imperfect." If this option is not specified, then any partial MCU blocks 341 * that cannot be transformed will be left in place, which will create 342 * odd-looking strips on the right or bottom edge of the image. 343 */ 344 enum TJXOPT_PERFECT = 1; 345 /** 346 * This option will cause #tjTransform() to discard any partial MCU blocks that 347 * cannot be transformed. 348 */ 349 enum TJXOPT_TRIM = 2; 350 /** 351 * This option will enable lossless cropping. See #tjTransform() for more 352 * information. 353 */ 354 enum TJXOPT_CROP = 4; 355 /** 356 * This option will discard the color data in the input image and produce 357 * a grayscale output image. 358 */ 359 enum TJXOPT_GRAY = 8; 360 /** 361 * This option will prevent #tjTransform() from outputting a JPEG image for 362 * this particular transform (this can be used in conjunction with a custom 363 * filter to capture the transformed DCT coefficients without transcoding 364 * them.) 365 */ 366 enum TJXOPT_NOOUTPUT = 16; 367 368 369 /** 370 * Scaling factor 371 */ 372 struct tjscalingfactor 373 { 374 /** 375 * Numerator 376 */ 377 int num; 378 /** 379 * Denominator 380 */ 381 int denom; 382 } 383 384 /** 385 * Cropping region 386 */ 387 struct tjregion 388 { 389 /** 390 * The left boundary of the cropping region. This must be evenly divisible 391 * by the MCU block width (see #tjMCUWidth.) 392 */ 393 int x; 394 /** 395 * The upper boundary of the cropping region. This must be evenly divisible 396 * by the MCU block height (see #tjMCUHeight.) 397 */ 398 int y; 399 /** 400 * The width of the cropping region. Setting this to 0 is the equivalent of 401 * setting it to the width of the source JPEG image - x. 402 */ 403 int w; 404 /** 405 * The height of the cropping region. Setting this to 0 is the equivalent of 406 * setting it to the height of the source JPEG image - y. 407 */ 408 int h; 409 } 410 411 /** 412 * Lossless transform 413 */ 414 struct tjtransform 415 { 416 /** 417 * Cropping region 418 */ 419 tjregion r; 420 /** 421 * One of the @ref TJXOP "transform operations" 422 */ 423 int op; 424 /** 425 * The bitwise OR of one of more of the @ref TJXOPT_CROP "transform options" 426 */ 427 int options; 428 /** 429 * Arbitrary data that can be accessed within the body of the callback 430 * function 431 */ 432 void *data; 433 /** 434 * A callback function that can be used to modify the DCT coefficients 435 * after they are losslessly transformed but before they are transcoded to a 436 * new JPEG file. This allows for custom filters or other transformations to 437 * be applied in the frequency domain. 438 * 439 * @param coeffs pointer to an array of transformed DCT coefficients. (NOTE: 440 * this pointer is not guaranteed to be valid once the callback 441 * returns, so applications wishing to hand off the DCT coefficients 442 * to another function or library should make a copy of them within 443 * the body of the callback.) 444 * @param arrayRegion #tjregion structure containing the width and height of 445 * the array pointed to by <tt>coeffs</tt> as well as its offset 446 * relative to the component plane. TurboJPEG implementations may 447 * choose to split each component plane into multiple DCT coefficient 448 * arrays and call the callback function once for each array. 449 * @param planeRegion #tjregion structure containing the width and height of 450 * the component plane to which <tt>coeffs</tt> belongs 451 * @param componentID ID number of the component plane to which 452 * <tt>coeffs</tt> belongs (Y, U, and V have, respectively, ID's of 453 * 0, 1, and 2 in typical JPEG images.) 454 * @param transformID ID number of the transformed image to which 455 * <tt>coeffs</tt> belongs. This is the same as the index of the 456 * transform in the <tt>transforms</tt> array that was passed to 457 * #tjTransform(). 458 * @param transform a pointer to a #tjtransform structure that specifies the 459 * parameters and/or cropping region for this transform 460 * 461 * @return 0 if the callback was successful, or -1 if an error occurred. 462 */ 463 int function (short *coeffs, tjregion arrayRegion, 464 tjregion planeRegion, int componentIndex, int transformIndex, 465 tjtransform *transform) customFilter; 466 } 467 468 /** 469 * TurboJPEG instance handle 470 */ 471 alias tjhandle = void*; 472 473 474 /** 475 * Pad the given width to the nearest 32-bit boundary 476 */ 477 auto TJPAD(T)(T width) { return (((width)+3)&(~3)); } 478 479 /** 480 * Compute the scaled value of <tt>dimension</tt> using the given scaling 481 * factor. This macro performs the integer equivalent of <tt>ceil(dimension * 482 * scalingFactor)</tt>. 483 */ 484 auto TJSCALED(D, SF)(D dimension, SF scalingFactor) { 485 return (dimension * scalingFactor.num + scalingFactor.denom - 1) / scalingFactor.denom; 486 } 487 488 489 extern (C) { 490 491 492 493 /** 494 * Create a TurboJPEG compressor instance. 495 * 496 * @return a handle to the newly-created instance, or NULL if an error 497 * occurred (see #tjGetErrorStr().) 498 */ 499 tjhandle tjInitCompress(); 500 501 502 /** 503 * Compress an RGB or grayscale image into a JPEG image. 504 * 505 * @param handle a handle to a TurboJPEG compressor or transformer instance 506 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels 507 * to be compressed 508 * @param width width (in pixels) of the source image 509 * @param pitch bytes per line of the source image. Normally, this should be 510 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, 511 * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of 512 * the image is padded to the nearest 32-bit boundary, as is the case 513 * for Windows bitmaps. You can also be clever and use this parameter 514 * to skip lines, etc. Setting this parameter to 0 is the equivalent of 515 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>. 516 * @param height height (in pixels) of the source image 517 * @param pixelFormat pixel format of the source image (see @ref TJPF 518 * "Pixel formats".) 519 * @param jpegBuf address of a pointer to an image buffer that will receive the 520 * JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer 521 * to accommodate the size of the JPEG image. Thus, you can choose to: 522 * -# pre-allocate the JPEG buffer with an arbitrary size using 523 * #tjAlloc() and let TurboJPEG grow the buffer as needed, 524 * -# set <tt>*jpegBuf</tt> to NULL to tell TurboJPEG to allocate the 525 * buffer for you, or 526 * -# pre-allocate the buffer to a "worst case" size determined by 527 * calling #tjBufSize(). This should ensure that the buffer never has 528 * to be re-allocated (setting #TJFLAG_NOREALLOC guarantees this.) 529 * . 530 * If you choose option 1, <tt>*jpegSize</tt> should be set to the 531 * size of your pre-allocated buffer. In any case, unless you have 532 * set #TJFLAG_NOREALLOC, you should always check <tt>*jpegBuf</tt> upon 533 * return from this function, as it may have changed. 534 * @param jpegSize pointer to an ulong variable that holds the size of 535 * the JPEG image buffer. If <tt>*jpegBuf</tt> points to a 536 * pre-allocated buffer, then <tt>*jpegSize</tt> should be set to the 537 * size of the buffer. Upon return, <tt>*jpegSize</tt> will contain the 538 * size of the JPEG image (in bytes.) 539 * @param jpegSubsamp the level of chrominance subsampling to be used when 540 * generating the JPEG image (see @ref TJSAMP 541 * "Chrominance subsampling options".) 542 * @param jpegQual the image quality of the generated JPEG image (1 = worst, 543 100 = best) 544 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP 545 * "flags". 546 * 547 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) 548 */ 549 int tjCompress2(tjhandle handle, ubyte *srcBuf, 550 int width, int pitch, int height, int pixelFormat, ubyte **jpegBuf, 551 c_ulong *jpegSize, int jpegSubsamp, int jpegQual, int flags); 552 553 554 /** 555 * The maximum size of the buffer (in bytes) required to hold a JPEG image with 556 * the given parameters. The number of bytes returned by this function is 557 * larger than the size of the uncompressed source image. The reason for this 558 * is that the JPEG format uses 16-bit coefficients, and it is thus possible 559 * for a very high-quality JPEG image with very high-frequency content to 560 * expand rather than compress when converted to the JPEG format. Such images 561 * represent a very rare corner case, but since there is no way to predict the 562 * size of a JPEG image prior to compression, the corner case has to be 563 * handled. 564 * 565 * @param width width of the image (in pixels) 566 * @param height height of the image (in pixels) 567 * @param jpegSubsamp the level of chrominance subsampling to be used when 568 * generating the JPEG image (see @ref TJSAMP 569 * "Chrominance subsampling options".) 570 * 571 * @return the maximum size of the buffer (in bytes) required to hold the 572 * image, or -1 if the arguments are out of bounds. 573 */ 574 c_ulong tjBufSize(int width, int height, 575 int jpegSubsamp); 576 577 578 /** 579 * The size of the buffer (in bytes) required to hold a YUV planar image with 580 * the given parameters. 581 * 582 * @param width width of the image (in pixels) 583 * @param height height of the image (in pixels) 584 * @param subsamp level of chrominance subsampling in the image (see 585 * @ref TJSAMP "Chrominance subsampling options".) 586 * 587 * @return the size of the buffer (in bytes) required to hold the image, or 588 * -1 if the arguments are out of bounds. 589 */ 590 c_ulong tjBufSizeYUV(int width, int height, 591 int subsamp); 592 593 594 /** 595 * Encode an RGB or grayscale image into a YUV planar image. This function 596 * uses the accelerated color conversion routines in TurboJPEG's underlying 597 * codec to produce a planar YUV image that is suitable for X Video. 598 * Specifically, if the chrominance components are subsampled along the 599 * horizontal dimension, then the width of the luminance plane is padded to the 600 * nearest multiple of 2 in the output image (same goes for the height of the 601 * luminance plane, if the chrominance components are subsampled along the 602 * vertical dimension.) Also, each line of each plane in the output image is 603 * padded to 4 bytes. Although this will work with any subsampling option, it 604 * is really only useful in combination with TJ_420, which produces an image 605 * compatible with the I420 (AKA "YUV420P") format. 606 * 607 * @param handle a handle to a TurboJPEG compressor or transformer instance 608 * @param srcBuf pointer to an image buffer containing RGB or grayscale pixels 609 * to be encoded 610 * @param width width (in pixels) of the source image 611 * @param pitch bytes per line of the source image. Normally, this should be 612 * <tt>width * #tjPixelSize[pixelFormat]</tt> if the image is unpadded, 613 * or <tt>#TJPAD(width * #tjPixelSize[pixelFormat])</tt> if each line of 614 * the image is padded to the nearest 32-bit boundary, as is the case 615 * for Windows bitmaps. You can also be clever and use this parameter 616 * to skip lines, etc. Setting this parameter to 0 is the equivalent of 617 * setting it to <tt>width * #tjPixelSize[pixelFormat]</tt>. 618 * @param height height (in pixels) of the source image 619 * @param pixelFormat pixel format of the source image (see @ref TJPF 620 * "Pixel formats".) 621 * @param dstBuf pointer to an image buffer that will receive the YUV image. 622 * Use #tjBufSizeYUV() to determine the appropriate size for this buffer 623 * based on the image width, height, and level of chrominance 624 * subsampling. 625 * @param subsamp the level of chrominance subsampling to be used when 626 * generating the YUV image (see @ref TJSAMP 627 * "Chrominance subsampling options".) 628 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP 629 * "flags". 630 * 631 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) 632 */ 633 int tjEncodeYUV2(tjhandle handle, 634 ubyte *srcBuf, int width, int pitch, int height, int pixelFormat, 635 ubyte *dstBuf, int subsamp, int flags); 636 637 638 /** 639 * Create a TurboJPEG decompressor instance. 640 * 641 * @return a handle to the newly-created instance, or NULL if an error 642 * occurred (see #tjGetErrorStr().) 643 */ 644 tjhandle tjInitDecompress(); 645 646 647 /** 648 * Retrieve information about a JPEG image without decompressing it. 649 * 650 * @param handle a handle to a TurboJPEG decompressor or transformer instance 651 * @param jpegBuf pointer to a buffer containing a JPEG image 652 * @param jpegSize size of the JPEG image (in bytes) 653 * @param width pointer to an integer variable that will receive the width (in 654 * pixels) of the JPEG image 655 * @param height pointer to an integer variable that will receive the height 656 * (in pixels) of the JPEG image 657 * @param jpegSubsamp pointer to an integer variable that will receive the 658 * level of chrominance subsampling used when compressing the JPEG image 659 * (see @ref TJSAMP "Chrominance subsampling options".) 660 * 661 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) 662 */ 663 int tjDecompressHeader2(tjhandle handle, 664 ubyte *jpegBuf, c_ulong jpegSize, int *width, int *height, 665 int *jpegSubsamp); 666 667 668 /** 669 * Returns a list of fractional scaling factors that the JPEG decompressor in 670 * this implementation of TurboJPEG supports. 671 * 672 * @param numscalingfactors pointer to an integer variable that will receive 673 * the number of elements in the list 674 * 675 * @return a pointer to a list of fractional scaling factors, or NULL if an 676 * error is encountered (see #tjGetErrorStr().) 677 */ 678 tjscalingfactor* tjGetScalingFactors(int *numscalingfactors); 679 680 681 /** 682 * Decompress a JPEG image to an RGB or grayscale image. 683 * 684 * @param handle a handle to a TurboJPEG decompressor or transformer instance 685 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress 686 * @param jpegSize size of the JPEG image (in bytes) 687 * @param dstBuf pointer to an image buffer that will receive the decompressed 688 * image. This buffer should normally be <tt>pitch * scaledHeight</tt> 689 * bytes in size, where <tt>scaledHeight</tt> can be determined by 690 * calling #TJSCALED() with the JPEG image height and one of the scaling 691 * factors returned by #tjGetScalingFactors(). The <tt>dstBuf</tt> 692 * pointer may also be used to decompress into a specific region of a 693 * larger buffer. 694 * @param width desired width (in pixels) of the destination image. If this is 695 * different than the width of the JPEG image being decompressed, then 696 * TurboJPEG will use scaling in the JPEG decompressor to generate the 697 * largest possible image that will fit within the desired width. If 698 * <tt>width</tt> is set to 0, then only the height will be considered 699 * when determining the scaled image size. 700 * @param pitch bytes per line of the destination image. Normally, this is 701 * <tt>scaledWidth * #tjPixelSize[pixelFormat]</tt> if the decompressed 702 * image is unpadded, else <tt>#TJPAD(scaledWidth * 703 * #tjPixelSize[pixelFormat])</tt> if each line of the decompressed 704 * image is padded to the nearest 32-bit boundary, as is the case for 705 * Windows bitmaps. (NOTE: <tt>scaledWidth</tt> can be determined by 706 * calling #TJSCALED() with the JPEG image width and one of the scaling 707 * factors returned by #tjGetScalingFactors().) You can also be clever 708 * and use the pitch parameter to skip lines, etc. Setting this 709 * parameter to 0 is the equivalent of setting it to <tt>scaledWidth 710 * * #tjPixelSize[pixelFormat]</tt>. 711 * @param height desired height (in pixels) of the destination image. If this 712 * is different than the height of the JPEG image being decompressed, 713 * then TurboJPEG will use scaling in the JPEG decompressor to generate 714 * the largest possible image that will fit within the desired height. 715 * If <tt>height</tt> is set to 0, then only the width will be 716 * considered when determining the scaled image size. 717 * @param pixelFormat pixel format of the destination image (see @ref 718 * TJPF "Pixel formats".) 719 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP 720 * "flags". 721 * 722 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) 723 */ 724 int tjDecompress2(tjhandle handle, 725 ubyte *jpegBuf, c_ulong jpegSize, ubyte *dstBuf, 726 int width, int pitch, int height, int pixelFormat, int flags); 727 728 729 /** 730 * Decompress a JPEG image to a YUV planar image. This function performs JPEG 731 * decompression but leaves out the color conversion step, so a planar YUV 732 * image is generated instead of an RGB image. The padding of the planes in 733 * this image is the same as in the images generated by #tjEncodeYUV2(). Note 734 * that, if the width or height of the image is not an even multiple of the MCU 735 * block size (see #tjMCUWidth and #tjMCUHeight), then an intermediate buffer 736 * copy will be performed within TurboJPEG. 737 * 738 * @param handle a handle to a TurboJPEG decompressor or transformer instance 739 * @param jpegBuf pointer to a buffer containing the JPEG image to decompress 740 * @param jpegSize size of the JPEG image (in bytes) 741 * @param dstBuf pointer to an image buffer that will receive the YUV image. 742 * Use #tjBufSizeYUV() to determine the appropriate size for this buffer 743 * based on the image width, height, and level of subsampling. 744 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP 745 * "flags". 746 * 747 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) 748 */ 749 int tjDecompressToYUV(tjhandle handle, 750 ubyte *jpegBuf, c_ulong jpegSize, ubyte *dstBuf, 751 int flags); 752 753 754 /** 755 * Create a new TurboJPEG transformer instance. 756 * 757 * @return a handle to the newly-created instance, or NULL if an error 758 * occurred (see #tjGetErrorStr().) 759 */ 760 tjhandle tjInitTransform(); 761 762 763 /** 764 * Losslessly transform a JPEG image into another JPEG image. Lossless 765 * transforms work by moving the raw coefficients from one JPEG image structure 766 * to another without altering the values of the coefficients. While this is 767 * typically faster than decompressing the image, transforming it, and 768 * re-compressing it, lossless transforms are not free. Each lossless 769 * transform requires reading and performing Huffman decoding on all of the 770 * coefficients in the source image, regardless of the size of the destination 771 * image. Thus, this function provides a means of generating multiple 772 * transformed images from the same source or applying multiple 773 * transformations simultaneously, in order to eliminate the need to read the 774 * source coefficients multiple times. 775 * 776 * @param handle a handle to a TurboJPEG transformer instance 777 * @param jpegBuf pointer to a buffer containing the JPEG image to transform 778 * @param jpegSize size of the JPEG image (in bytes) 779 * @param n the number of transformed JPEG images to generate 780 * @param dstBufs pointer to an array of n image buffers. <tt>dstBufs[i]</tt> 781 * will receive a JPEG image that has been transformed using the 782 * parameters in <tt>transforms[i]</tt>. TurboJPEG has the ability to 783 * reallocate the JPEG buffer to accommodate the size of the JPEG image. 784 * Thus, you can choose to: 785 * -# pre-allocate the JPEG buffer with an arbitrary size using 786 * #tjAlloc() and let TurboJPEG grow the buffer as needed, 787 * -# set <tt>dstBufs[i]</tt> to NULL to tell TurboJPEG to allocate the 788 * buffer for you, or 789 * -# pre-allocate the buffer to a "worst case" size determined by 790 * calling #tjBufSize() with the transformed or cropped width and 791 * height. This should ensure that the buffer never has to be 792 * re-allocated (setting #TJFLAG_NOREALLOC guarantees this.) 793 * . 794 * If you choose option 1, <tt>dstSizes[i]</tt> should be set to 795 * the size of your pre-allocated buffer. In any case, unless you have 796 * set #TJFLAG_NOREALLOC, you should always check <tt>dstBufs[i]</tt> 797 * upon return from this function, as it may have changed. 798 * @param dstSizes pointer to an array of n ulong variables that will 799 * receive the actual sizes (in bytes) of each transformed JPEG image. 800 * If <tt>dstBufs[i]</tt> points to a pre-allocated buffer, then 801 * <tt>dstSizes[i]</tt> should be set to the size of the buffer. Upon 802 * return, <tt>dstSizes[i]</tt> will contain the size of the JPEG image 803 * (in bytes.) 804 * @param transforms pointer to an array of n #tjtransform structures, each of 805 * which specifies the transform parameters and/or cropping region for 806 * the corresponding transformed output image. 807 * @param flags the bitwise OR of one or more of the @ref TJFLAG_BOTTOMUP 808 * "flags". 809 * 810 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) 811 */ 812 int tjTransform(tjhandle handle, ubyte *jpegBuf, 813 c_ulong jpegSize, int n, ubyte **dstBufs, 814 c_ulong *dstSizes, tjtransform *transforms, int flags); 815 816 817 /** 818 * Destroy a TurboJPEG compressor, decompressor, or transformer instance. 819 * 820 * @param handle a handle to a TurboJPEG compressor, decompressor or 821 * transformer instance 822 * 823 * @return 0 if successful, or -1 if an error occurred (see #tjGetErrorStr().) 824 */ 825 int tjDestroy(tjhandle handle); 826 827 828 /** 829 * Allocate an image buffer for use with TurboJPEG. You should always use 830 * this function to allocate the JPEG destination buffer(s) for #tjCompress2() 831 * and #tjTransform() unless you are disabling automatic buffer 832 * (re)allocation (by setting #TJFLAG_NOREALLOC.) 833 * 834 * @param bytes the number of bytes to allocate 835 * 836 * @return a pointer to a newly-allocated buffer with the specified number of 837 * bytes 838 * 839 * @sa tjFree() 840 */ 841 ubyte* tjAlloc(int bytes); 842 843 844 /** 845 * Free an image buffer previously allocated by TurboJPEG. You should always 846 * use this function to free JPEG destination buffer(s) that were automatically 847 * (re)allocated by #tjCompress2() or #tjTransform() or that were manually 848 * allocated using #tjAlloc(). 849 * 850 * @param buffer address of the buffer to free 851 * 852 * @sa tjAlloc() 853 */ 854 void tjFree(ubyte *buffer); 855 856 857 /** 858 * Returns a descriptive error message explaining why the last command failed. 859 * 860 * @return a descriptive error message explaining why the last command failed. 861 */ 862 char* tjGetErrorStr(); 863 864 865 /* Backward compatibility functions and macros (nothing to see here) */ 866 enum NUMSUBOPT = TJ_NUMSAMP; 867 enum TJ_444 = TJSAMP.TJSAMP_444; 868 enum TJ_422 = TJSAMP.TJSAMP_422; 869 enum TJ_420 = TJSAMP.TJSAMP_420; 870 enum TJ_411 = TJSAMP.TJSAMP_420; 871 enum TJ_GRAYSCALE = TJSAMP.TJSAMP_GRAY; 872 873 enum TJ_BGR = 1; 874 enum TJ_BOTTOMUP = TJFLAG_BOTTOMUP; 875 enum TJ_FORCEMMX = TJFLAG_FORCEMMX; 876 enum TJ_FORCESSE = TJFLAG_FORCESSE; 877 enum TJ_FORCESSE2 = TJFLAG_FORCESSE2; 878 enum TJ_ALPHAFIRST = 64; 879 enum TJ_FORCESSE3 = TJFLAG_FORCESSE3; 880 enum TJ_FASTUPSAMPLE = TJFLAG_FASTUPSAMPLE; 881 enum TJ_YUV = 512; 882 883 c_ulong TJBUFSIZE(int width, int height); 884 885 c_ulong TJBUFSIZEYUV(int width, int height, 886 int jpegSubsamp); 887 888 int tjCompress(tjhandle handle, ubyte *srcBuf, 889 int width, int pitch, int height, int pixelSize, ubyte *dstBuf, 890 c_ulong *compressedSize, int jpegSubsamp, int jpegQual, int flags); 891 892 int tjEncodeYUV(tjhandle handle, 893 ubyte *srcBuf, int width, int pitch, int height, int pixelSize, 894 ubyte *dstBuf, int subsamp, int flags); 895 896 int tjDecompressHeader(tjhandle handle, 897 ubyte *jpegBuf, c_ulong jpegSize, int *width, int *height); 898 899 int tjDecompress(tjhandle handle, 900 ubyte *jpegBuf, c_ulong jpegSize, ubyte *dstBuf, 901 int width, int pitch, int height, int pixelSize, int flags); 902 903 904 /** 905 * @} 906 */ 907 908 } // extern (C) 909