/// <summary> /// Retrieve information about a JPEG image without decompressing it. /// </summary> /// <param name="jpegBuf"> /// Pointer to a buffer containing a JPEG image. This buffer is not modified. /// </param> /// <param name="jpegBufSize"> /// Size of the JPEG image (in bytes) /// </param> /// <param name="destPixelFormat"> /// The pixel format of the uncompressed image. /// </param> /// <param name="width"> /// Pointer to an integer variable that will receive the width (in pixels) of the JPEG image /// </param> /// <param name="height"> /// Pointer to an integer variable that will receive the height (in pixels) of the JPEG image /// </param> /// <param name="stride"> /// Pointer to an integer variable that will receive the stride (in bytes) of the JPEG image. /// </param> /// <param name="bufSize"> /// The size of a buffer that can receive the uncompressed JPEG image. /// </param> public void GetImageInfo(IntPtr jpegBuf, ulong jpegBufSize, TJPixelFormats destPixelFormat, out int width, out int height, out int stride, out int bufSize) { int subsampl; int colorspace; var funcResult = TurboJpegImport.tjDecompressHeader(_decompressorHandle, jpegBuf, jpegBufSize, out width, out height, out subsampl, out colorspace); stride = TurboJpegImport.TJPAD(width * TurboJpegImport.PixelSizes[destPixelFormat]); bufSize = stride * height; }
/// <summary> /// Decompress a JPEG image to an RGB, grayscale, or CMYK image. /// </summary> /// <param name="jpegBuf">Pointer to a buffer containing the JPEG image to decompress. This buffer is not modified.</param> /// <param name="jpegBufSize">Size of the JPEG image (in bytes).</param> /// <param name="outBuf">The buffer into which to store the decompressed JPEG image.</param> /// <param name="outBufSize">Size of <paramref name="outBuf"/> (in bytes).</param> /// <param name="destPixelFormat">Pixel format of the destination image (see <see cref="PixelFormat"/> "Pixel formats".)</param> /// <param name="flags">The bitwise OR of one or more of the <see cref="TJFlags"/> "flags".</param> /// <param name="width">Width of image in pixels.</param> /// <param name="height">Height of image in pixels.</param> /// <param name="stride">Bytes per line in the destination image.</param> public unsafe void Decompress(IntPtr jpegBuf, ulong jpegBufSize, IntPtr outBuf, int outBufSize, TJPixelFormat destPixelFormat, TJFlags flags, out int width, out int height, out int stride) { if (this.isDisposed) { throw new ObjectDisposedException("this"); } int subsampl; int colorspace; var funcResult = TurboJpegImport.TjDecompressHeader( this.decompressorHandle, jpegBuf, jpegBufSize, out width, out height, out subsampl, out colorspace); if (funcResult == -1) { TJUtils.GetErrorAndThrow(); } var targetFormat = destPixelFormat; stride = TurboJpegImport.TJPAD(width * TurboJpegImport.PixelSizes[targetFormat]); var bufSize = stride * height; if (outBufSize < bufSize) { throw new ArgumentOutOfRangeException(nameof(outBufSize)); } funcResult = TurboJpegImport.TjDecompress( this.decompressorHandle, jpegBuf, jpegBufSize, outBuf, width, stride, height, (int)targetFormat, (int)flags); if (funcResult == -1) { TJUtils.GetErrorAndThrow(); } }
/// <summary> /// Returns info about jpeg image without decompressing it /// </summary> /// <param name="jpegBuf">Pointer to a buffer containing the JPEG image to decompress. This buffer is not modified</param> /// <param name="jpegBufSize">Size of the JPEG image (in bytes)</param> /// <param name="destPixelFormat">Pixel format of the destination image</param> /// <param name="width">Width of image in pixels</param> /// <param name="height">Height of image in pixels</param> /// <param name="stride">Bytes per line in the destination image</param> /// <param name="decompressedBufferSize">Size in bytes for buffer where decompressed image will be put</param> /// <exception cref="TJException">Throws if underlying decompress function failed</exception> /// <exception cref="ObjectDisposedException">Object is disposed and can not be used anymore</exception> /// <returns></returns> public void GetImageInfo(IntPtr jpegBuf, ulong jpegBufSize, TJPixelFormats destPixelFormat, out int width, out int height, out int stride, out int decompressedBufferSize) { if (_isDisposed) { throw new ObjectDisposedException("this"); } var funcResult = TurboJpegImport.tjDecompressHeader(_decompressorHandle, jpegBuf, jpegBufSize, out width, out height, out _, out _); if (funcResult == -1) { TJUtils.GetErrorAndThrow(); } stride = TurboJpegImport.TJPAD(width * TurboJpegImport.PixelSizes[destPixelFormat]); decompressedBufferSize = stride * height; }
/// <summary> /// Decompress a JPEG image to an RGB, grayscale, or CMYK image. /// </summary> /// <param name="jpegBuf">Pointer to a buffer containing the JPEG image to decompress. This buffer is not modified</param> /// <param name="jpegBufSize">Size of the JPEG image (in bytes)</param> /// <param name="destPixelFormat">Pixel format of the destination image</param> /// <param name="flags">The bitwise OR of one or more of the <see cref="TJFlags"/> "flags"</param> /// <param name="width">Width of image in pixels</param> /// <param name="height">Height of image in pixels</param> /// <param name="stride">Bytes per line in the destination image</param> /// <returns>Raw pixel data of specified format</returns> /// <exception cref="TJException">Throws if underlying decompress function failed</exception> /// <exception cref="ObjectDisposedException">Object is disposed and can not be used anymore</exception> public unsafe byte[] Decompress(IntPtr jpegBuf, ulong jpegBufSize, TJPixelFormats destPixelFormat, TJFlags flags, out int width, out int height, out int stride) { if (_isDisposed) { throw new ObjectDisposedException("this"); } var funcResult = TurboJpegImport.tjDecompressHeader(_decompressorHandle, jpegBuf, jpegBufSize, out width, out height, out _, out _); if (funcResult == -1) { TJUtils.GetErrorAndThrow(); } var targetFormat = destPixelFormat; stride = TurboJpegImport.TJPAD(width * TurboJpegImport.PixelSizes[targetFormat]); var bufSize = stride * height; var buf = new byte[bufSize]; fixed(byte *bufPtr = buf) { funcResult = TurboJpegImport.tjDecompress( _decompressorHandle, jpegBuf, jpegBufSize, (IntPtr)bufPtr, width, stride, height, (int)targetFormat, (int)flags); if (funcResult == -1) { TJUtils.GetErrorAndThrow(); } return(buf); } }
/// <summary> /// Given the size of an image, determines the size of a decompressed image. /// </summary> /// <param name="height"> /// The height of the image. /// </param> /// <param name="width"> /// The width of the image. /// </param> /// <param name="destPixelFormat"> /// The pixel format of the uncompressed image. /// </param> /// <returns> /// The size of a buffer that can hold the uncompressed image. /// </returns> public int GetBufferSize(int height, int width, TJPixelFormats destPixelFormat) { int stride = TurboJpegImport.TJPAD(width * TurboJpegImport.PixelSizes[destPixelFormat]); return(stride * height); }