Exemplo n.º 1
0
        /// <summary>
        /// The WebP save function.
        /// </summary>
        /// <param name="input">The input Document.</param>
        /// <param name="output">The output Stream.</param>
        /// <param name="quality">The WebP save quality.</param>
        /// <param name="preset">The WebP encoding preset.</param>
        /// <param name="keepMetadata"><c>true</c> if metadata should be preserved; otherwise <c>false</c>.</param>
        /// <param name="scratchSurface">The scratch surface.</param>
        /// <param name="progressCallback">The progress callback.</param>
        /// <exception cref="FormatException">The image exceeds 16383 pixels in width and/or height.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory to save the image.</exception>
        /// <exception cref="WebPException">The encoder returned a non-memory related error.</exception>
        internal static void Save(
            Document input,
            Stream output,
            int quality,
            WebPPreset preset,
            bool keepMetadata,
            Surface scratchSurface,
            ProgressEventHandler progressCallback)
        {
            if (input.Width > WebPNative.WebPMaxDimension || input.Height > WebPNative.WebPMaxDimension)
            {
                throw new FormatException(Resources.InvalidImageDimensions);
            }

            WebPNative.EncodeParams encParams = new WebPNative.EncodeParams
            {
                quality = quality,
                preset  = preset
            };

            using (RenderArgs ra = new RenderArgs(scratchSurface))
            {
                input.Render(ra, true);
            }

            WebPNative.MetadataParams metadata = null;
            if (keepMetadata)
            {
                metadata = CreateWebPMetadata(input, scratchSurface);
            }

            WebPNative.WebPReportProgress encProgress = null;

            if (progressCallback != null)
            {
                encProgress = delegate(int percent)
                {
                    try
                    {
                        progressCallback(null, new ProgressEventArgs(percent, true));
                        return(true);
                    }
                    catch (OperationCanceledException)
                    {
                        return(false);
                    }
                };
            }

            WebPNative.WebPSave(scratchSurface, output, encParams, metadata, encProgress);
        }
Exemplo n.º 2
0
        /// <summary>
        /// The WebP load function.
        /// </summary>
        /// <param name="webpBytes">The input image data</param>
        /// <returns>
        /// A <see cref="Bitmap"/> containing the WebP image.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="webpBytes"/> is null.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory to load the WebP image.</exception>
        /// <exception cref="WebPException">
        /// The WebP image is invalid.
        /// -or-
        /// A native API parameter is invalid.
        /// </exception>
        internal static unsafe Bitmap Load(byte[] webpBytes)
        {
            if (webpBytes == null)
            {
                throw new ArgumentNullException(nameof(webpBytes));
            }

            WebPNative.ImageInfo imageInfo;

            WebPNative.WebPGetImageInfo(webpBytes, out imageInfo);

            if (imageInfo.hasAnimation)
            {
                throw new WebPException(Resources.AnimatedWebPNotSupported);
            }

            Bitmap image = null;
            Bitmap temp  = null;

            try
            {
                temp = new Bitmap(imageInfo.width, imageInfo.height, PixelFormat.Format32bppArgb);

                BitmapData bitmapData = temp.LockBits(new Rectangle(0, 0, imageInfo.width, imageInfo.height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

                try
                {
                    WebPNative.WebPLoad(webpBytes, bitmapData);
                }
                finally
                {
                    temp.UnlockBits(bitmapData);
                }

                image = temp;
                temp  = null;
            }
            finally
            {
                if (temp != null)
                {
                    temp.Dispose();
                }
            }

            return(image);
        }
Exemplo n.º 3
0
        /// <summary>
        /// The WebP load function.
        /// </summary>
        /// <param name="webpBytes">The input image data</param>
        /// <returns>
        /// A <see cref="Bitmap"/> containing the WebP image.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="webpBytes"/> is null.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory to load the WebP image.</exception>
        /// <exception cref="WebPException">
        /// The WebP image is invalid.
        /// -or-
        /// A native API parameter is invalid.
        /// </exception>
        internal static unsafe Bitmap Load(byte[] webpBytes)
        {
            if (webpBytes == null)
            {
                throw new ArgumentNullException(nameof(webpBytes));
            }

            int width;
            int height;

            if (!WebPNative.WebPGetDimensions(webpBytes, out width, out height))
            {
                throw new WebPException(Resources.InvalidWebPImage);
            }

            Bitmap image = null;
            Bitmap temp  = null;

            try
            {
                temp = new Bitmap(width, height, PixelFormat.Format32bppArgb);

                BitmapData bitmapData = temp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

                try
                {
                    WebPNative.WebPLoad(webpBytes, bitmapData);
                }
                finally
                {
                    temp.UnlockBits(bitmapData);
                }

                image = temp;
                temp  = null;
            }
            finally
            {
                if (temp != null)
                {
                    temp.Dispose();
                }
            }

            return(image);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the metadata from the WebP image.
        /// </summary>
        /// <param name="webpBytes">The WebP image data.</param>
        /// <param name="type">The metadata type.</param>
        /// <returns>
        /// A byte array containing the requested metadata, if present; otherwise, <see langword="null"/>
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="webpBytes"/> is null.</exception>
        private static byte[] GetMetadataBytes(byte[] webpBytes, WebPNative.MetadataType type)
        {
            if (webpBytes == null)
            {
                throw new ArgumentNullException(nameof(webpBytes));
            }

            byte[] bytes = null;

            uint size = WebPNative.GetMetadataSize(webpBytes, type);

            if (size > 0)
            {
                bytes = new byte[size];
                WebPNative.ExtractMetadata(webpBytes, type, bytes, size);
            }

            return(bytes);
        }
Exemplo n.º 5
0
        /// <summary>
        /// The WebP load function.
        /// </summary>
        /// <param name="webpBytes">The input image data</param>
        /// <returns>
        /// A <see cref="Bitmap"/> containing the WebP image.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="webpBytes"/> is null.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory to load the WebP image.</exception>
        /// <exception cref="WebPException">
        /// The WebP image is invalid.
        /// -or-
        /// A native API parameter is invalid.
        /// </exception>
        internal static unsafe Surface Load(byte[] webpBytes)
        {
            if (webpBytes == null)
            {
                throw new ArgumentNullException(nameof(webpBytes));
            }

            WebPNative.WebPGetImageInfo(webpBytes, out WebPNative.ImageInfo imageInfo);

            if (imageInfo.hasAnimation)
            {
                throw new WebPException(Resources.AnimatedWebPNotSupported);
            }

            Surface image = null;
            Surface temp  = null;

            try
            {
                temp = new Surface(imageInfo.width, imageInfo.height);

                WebPNative.WebPLoad(webpBytes, temp);

                image = temp;
                temp  = null;
            }
            finally
            {
                if (temp != null)
                {
                    temp.Dispose();
                }
            }

            return(image);
        }