コード例 #1
0
        internal static unsafe uint GetMetadataSize(byte[] data, MetadataType type)
        {
            uint metadataSize;

            fixed(byte *ptr = data)
            {
                if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
                {
                    metadataSize = WebP_x64.GetMetadataSize(ptr, new UIntPtr((ulong)data.Length), type);
                }
                else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
                {
                    metadataSize = WebP_x86.GetMetadataSize(ptr, new UIntPtr((ulong)data.Length), type);
                }
                else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
                {
                    metadataSize = WebP_ARM64.GetMetadataSize(ptr, new UIntPtr((ulong)data.Length), type);
                }
                else
                {
                    throw new PlatformNotSupportedException();
                }
            }

            return(metadataSize);
        }
コード例 #2
0
        /// <summary>
        /// The WebP load function.
        /// </summary>
        /// <param name="webpBytes">The input image data</param>
        /// <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 void WebPLoad(byte[] webpBytes, Surface output)
        {
            VP8StatusCode status;

            fixed(byte *ptr = webpBytes)
            {
                int   stride     = output.Stride;
                ulong outputSize = (ulong)stride * (ulong)output.Height;

                if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
                {
                    status = WebP_x64.WebPLoad(ptr, new UIntPtr((ulong)webpBytes.Length), (byte *)output.Scan0.VoidStar, new UIntPtr(outputSize), stride);
                }
                else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
                {
                    status = WebP_x86.WebPLoad(ptr, new UIntPtr((ulong)webpBytes.Length), (byte *)output.Scan0.VoidStar, new UIntPtr(outputSize), stride);
                }
                else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
                {
                    status = WebP_ARM64.WebPLoad(ptr, new UIntPtr((ulong)webpBytes.Length), (byte *)output.Scan0.VoidStar, new UIntPtr(outputSize), stride);
                }
                else
                {
                    throw new PlatformNotSupportedException();
                }
            }

            if (status != VP8StatusCode.Ok)
            {
                switch (status)
                {
                case VP8StatusCode.OutOfMemory:
                    throw new OutOfMemoryException();

                case VP8StatusCode.InvalidParam:
                    throw new WebPException(string.Format(CultureInfo.InvariantCulture, Resources.InvalidParameterFormat, nameof(WebPLoad)));

                case VP8StatusCode.UnsupportedFeature:
                    throw new WebPException(Resources.UnsupportedWebPFeature);

                case VP8StatusCode.BitStreamError:
                case VP8StatusCode.NotEnoughData:
                default:
                    throw new WebPException(Resources.InvalidWebPImage);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Gets the WebP image information.
        /// </summary>
        /// <param name="data">The input image data.</param>
        /// <param name="info">The output image information.</param>
        /// <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 void WebPGetImageInfo(byte[] data, out ImageInfo info)
        {
            VP8StatusCode status;

            fixed(byte *ptr = data)
            {
                if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
                {
                    status = WebP_x64.WebPGetImageInfo(ptr, new UIntPtr((ulong)data.Length), out info);
                }
                else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
                {
                    status = WebP_x86.WebPGetImageInfo(ptr, new UIntPtr((ulong)data.Length), out info);
                }
                else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
                {
                    status = WebP_ARM64.WebPGetImageInfo(ptr, new UIntPtr((ulong)data.Length), out info);
                }
                else
                {
                    throw new PlatformNotSupportedException();
                }
            }

            if (status != VP8StatusCode.Ok)
            {
                switch (status)
                {
                case VP8StatusCode.OutOfMemory:
                    throw new OutOfMemoryException();

                case VP8StatusCode.InvalidParam:
                    throw new WebPException(string.Format(CultureInfo.InvariantCulture, Resources.InvalidParameterFormat, nameof(WebPGetImageInfo)));

                case VP8StatusCode.UnsupportedFeature:
                    throw new WebPException(Resources.UnsupportedWebPFeature);

                case VP8StatusCode.BitStreamError:
                case VP8StatusCode.NotEnoughData:
                default:
                    throw new WebPException(Resources.InvalidWebPImage);
                }
            }
        }
コード例 #4
0
 internal static unsafe void ExtractMetadata(byte[] data, MetadataType type, byte[] outData, uint outSize)
 {
     fixed(byte *ptr = data, outPtr = outData)
     {
         if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
         {
             WebP_x64.ExtractMetadata(ptr, new UIntPtr((ulong)data.Length), outPtr, outSize, type);
         }
         else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
         {
             WebP_x86.ExtractMetadata(ptr, new UIntPtr((ulong)data.Length), outPtr, outSize, type);
         }
         else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
         {
             WebP_ARM64.ExtractMetadata(ptr, new UIntPtr((ulong)data.Length), outPtr, outSize, type);
         }
         else
         {
             throw new PlatformNotSupportedException();
         }
     }
 }
コード例 #5
0
        /// <summary>
        /// The WebP save function.
        /// </summary>
        /// <param name="writeImageCallback">The callback used to write the WebP image.</param>
        /// <param name="input">The input surface.</param>
        /// <param name="parameters">The encode parameters.</param>
        /// <param name="metadata">The image metadata.</param>
        /// <param name="callback">The progress callback.</param>
        /// <exception cref="ArgumentNullException"><paramref name="writeImageCallback"/> is null.
        /// or
        /// <paramref name="input"/> is null.</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 WebPSave(
            Surface input,
            Stream output,
            EncodeParams parameters,
            MetadataParams metadata,
            WebPReportProgress callback)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            StreamIOHandler handler            = new(output);
            WebPWriteImage  writeImageCallback = handler.WriteImageCallback;

            WebPEncodingError retVal = WebPEncodingError.Ok;

            if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
            {
                retVal = WebP_x64.WebPSave(writeImageCallback, input.Scan0.Pointer, input.Width, input.Height, input.Stride, parameters, metadata, callback);
            }
            else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
            {
                retVal = WebP_x86.WebPSave(writeImageCallback, input.Scan0.Pointer, input.Width, input.Height, input.Stride, parameters, metadata, callback);
            }
            else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
            {
                retVal = WebP_ARM64.WebPSave(writeImageCallback, input.Scan0.Pointer, input.Width, input.Height, input.Stride, parameters, metadata, callback);
            }
            else
            {
                throw new PlatformNotSupportedException();
            }

            GC.KeepAlive(writeImageCallback);

            if (retVal != WebPEncodingError.Ok)
            {
                switch (retVal)
                {
                case WebPEncodingError.OutOfMemory:
                case WebPEncodingError.BitStreamOutOfMemory:
                    throw new OutOfMemoryException(Resources.InsufficientMemoryOnSave);

                case WebPEncodingError.FileTooBig:
                    throw new WebPException(Resources.EncoderFileTooBig);

                case WebPEncodingError.ApiVersionMismatch:
                    throw new WebPException(Resources.ApiVersionMismatch);

                case WebPEncodingError.MetadataEncoding:
                    throw new WebPException(Resources.EncoderMetadataError);

                case WebPEncodingError.UserAbort:
                    throw new OperationCanceledException();

                case WebPEncodingError.BadDimension:
                    throw new WebPException(Resources.InvalidImageDimensions);

                case WebPEncodingError.NullParameter:
                    throw new WebPException(Resources.EncoderNullParameter);

                case WebPEncodingError.InvalidConfiguration:
                    throw new WebPException(Resources.EncoderInvalidConfiguration);

                case WebPEncodingError.PartitionZeroOverflow:
                    throw new WebPException(Resources.EncoderPartitionZeroOverflow);

                case WebPEncodingError.PartitionOverflow:
                    throw new WebPException(Resources.EncoderPartitionOverflow);

                case WebPEncodingError.BadWrite:
                    if (handler.WriteException != null)
                    {
                        throw new IOException(Resources.EncoderBadWrite, handler.WriteException);
                    }
                    else
                    {
                        throw new IOException(Resources.EncoderBadWrite);
                    }

                default:
                    throw new WebPException(Resources.EncoderGenericError);
                }
            }
        }