/// <summary> /// Decompress a JPEG image to an RGB, grayscale, or CMYK image into precreated unmanaged buffer /// </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">Pointer to output buffer where decompressed image will be put. Buffer must be created before call</param> /// <param name="outBufSize">Size of output buffer (in bytes). It must be greater or equal than size of decompressed image</param> /// <param name="destPixelFormat">Pixel format of the destination image (see <see cref="TJPixelFormats"/> "Pixel formats".)</param> /// <param name="flags">The bitwise OR of one or more of the <see cref="TJFlags"/> "flags"</param> /// <exception cref="TJException">Throws if underlying decompress function failed</exception> /// <exception cref="ObjectDisposedException">Object is disposed and can not be used anymore</exception> // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Global public void Decompress(IntPtr jpegBuf, ulong jpegBufSize, IntPtr outBuf, int outBufSize, TJPixelFormats destPixelFormat, TJFlags flags) { GetImageInfo(jpegBuf, jpegBufSize, destPixelFormat, out var width, out var height, out var stride, out var bufSize); if (outBufSize < bufSize) { throw new ArgumentOutOfRangeException("outBufSize"); } var funcResult = TurboJpegImport.tjDecompress( _decompressorHandle, jpegBuf, jpegBufSize, outBuf, width, stride, height, (int)destPixelFormat, (int)flags); if (funcResult == -1) { TJUtils.GetErrorAndThrow(); } }
public unsafe void Decompress(IntPtr jpegBuf, ulong jpegBufSize, IntPtr outBuf, int outBufSize, TJPixelFormats destPixelFormat, TJFlags flags, out int width, out int height, out int stride) { if (_isDisposed) { throw new ObjectDisposedException("this"); } int subsampl; int colorspace; var funcResult = TurboJpegImport.tjDecompressHeader(_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( _decompressorHandle, jpegBuf, jpegBufSize, outBuf, width, stride, height, (int)targetFormat, (int)flags); if (funcResult == -1) { TJUtils.GetErrorAndThrow(); } }
/// <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); } }