예제 #1
0
        /// <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, TJPixelFormat destPixelFormat)
        {
            Verify.NotDisposed(this);

            int stride = TurboJpegImport.TJPAD(width * TurboJpegImport.PixelSizes[destPixelFormat]);

            return(stride * height);
        }
예제 #2
0
        /// <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="outBuf">The buffer into which to store the decompressed JPEG image.</param>
        /// <param name="destPixelFormat">Pixel format of the destination image (see <see cref="TJPixelFormat"/> "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(Span <byte> jpegBuf, Span <byte> outBuf, TJPixelFormat destPixelFormat, TJFlags flags, out int width, out int height, out int stride)
        {
            Verify.NotDisposed(this);

            fixed(byte *jpegBufPtr = jpegBuf)
            fixed(byte *outBufPtr = outBuf)
            {
                int subsampl;
                int colorspace;
                var funcResult = TurboJpegImport.TjDecompressHeader(
                    this.decompressorHandle,
                    jpegBufPtr,
                    (nuint)jpegBuf.Length,
                    out width,
                    out height,
                    out subsampl,
                    out colorspace);

                TJUtils.ThrowOnError(funcResult);

                var targetFormat = destPixelFormat;

                stride = TurboJpegImport.TJPAD(width * TurboJpegImport.PixelSizes[targetFormat]);
                var bufSize = stride * height;

                if (outBuf.Length < bufSize)
                {
                    throw new ArgumentOutOfRangeException(nameof(outBuf));
                }

                funcResult = TurboJpegImport.TjDecompress(
                    this.decompressorHandle,
                    jpegBufPtr,
                    (nuint)jpegBuf.Length,
                    outBufPtr,
                    width,
                    stride,
                    height,
                    (int)targetFormat,
                    (int)flags);

                TJUtils.ThrowOnError(funcResult);
            }
        }
예제 #3
0
        /// <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="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(Span <byte> jpegBuf, TJPixelFormat destPixelFormat, out int width, out int height, out int stride, out int bufSize)
        {
            Verify.NotDisposed(this);

            int subsampl;
            int colorspace;

            fixed(byte *jpegBufPtr = jpegBuf)
            {
                var funcResult = TurboJpegImport.TjDecompressHeader(
                    this.decompressorHandle,
                    jpegBufPtr,
                    (nuint)jpegBuf.Length,
                    out width,
                    out height,
                    out subsampl,
                    out colorspace);

                stride  = TurboJpegImport.TJPAD(width * TurboJpegImport.PixelSizes[destPixelFormat]);
                bufSize = stride * height;
            }
        }