Example #1
0
 private void DecodeColorImage(uint itemId, DecodeInfo decodeInfo, CICPColorData?colorConversionInfo, Surface fullSurface)
 {
     using (AvifItemData color = ReadColorImage(itemId))
     {
         AvifNative.DecompressColor(color, colorConversionInfo, decodeInfo, fullSurface);
     }
 }
Example #2
0
 private void DecodeAlphaImage(uint itemId, DecodeInfo decodeInfo, Surface fullSurface)
 {
     using (AvifItemData alpha = ReadAlphaImage(itemId))
     {
         AvifNative.DecompressAlpha(alpha, decodeInfo, fullSurface);
     }
 }
Example #3
0
        public static void DecompressAlpha(AvifItemData alphaImage,
                                           DecodeInfo decodeInfo,
                                           Surface fullSurface)
        {
            if (alphaImage is null)
            {
                ExceptionUtil.ThrowArgumentNullException(nameof(alphaImage));
            }

            if (decodeInfo is null)
            {
                ExceptionUtil.ThrowArgumentNullException(nameof(decodeInfo));
            }

            if (fullSurface is null)
            {
                ExceptionUtil.ThrowArgumentNullException(nameof(fullSurface));
            }

            DecoderStatus status = DecoderStatus.Ok;

            unsafe
            {
                alphaImage.UseBufferPointer((ptr, length) =>
                {
                    BitmapData bitmapData = new BitmapData
                    {
                        scan0  = fullSurface.Scan0.Pointer,
                        width  = (uint)fullSurface.Width,
                        height = (uint)fullSurface.Height,
                        stride = (uint)fullSurface.Stride
                    };

                    UIntPtr alphaImageSize = new UIntPtr(length);

                    if (IntPtr.Size == 8)
                    {
                        status = AvifNative_64.DecompressAlphaImage(ptr,
                                                                    alphaImageSize,
                                                                    decodeInfo,
                                                                    ref bitmapData);
                    }
                    else
                    {
                        status = AvifNative_86.DecompressAlphaImage(ptr,
                                                                    alphaImageSize,
                                                                    decodeInfo,
                                                                    ref bitmapData);
                    }
                });
            }

            if (status != DecoderStatus.Ok)
            {
                HandleError(status);
            }
        }
Example #4
0
        public byte[] GetExifData()
        {
            VerifyNotDisposed();

            ItemLocationEntry entry = this.parser.TryGetExifLocation(this.primaryItemId);

            if (entry != null)
            {
                ulong length = entry.TotalItemSize;

                // Ignore any EXIF blocks that are larger than 2GB.
                if (length < int.MaxValue)
                {
                    using (AvifItemData itemData = this.parser.ReadItemData(entry))
                        using (Stream stream = itemData.GetStream())
                        {
                            // The EXIF data block has a header that indicates the number of bytes
                            // that come before the start of the TIFF header.
                            // See ISO/IEC 23008-12:2017 section A.2.1.

                            long tiffStartOffset = stream.TryReadUInt32BigEndian();

                            if (tiffStartOffset != -1)
                            {
                                long dataLength = (long)length - tiffStartOffset - sizeof(uint);

                                if (dataLength > 0)
                                {
                                    if (tiffStartOffset != 0)
                                    {
                                        stream.Position += tiffStartOffset;
                                    }

                                    byte[] bytes = new byte[dataLength];

                                    stream.ProperRead(bytes, 0, bytes.Length);

                                    return(bytes);
                                }
                            }
                        }
                }
            }

            return(null);
        }
Example #5
0
        private ImageGridDescriptor TryGetImageGridDescriptor(uint itemId)
        {
            IItemInfoEntry entry = TryGetItemInfoEntry(itemId);

            if (entry != null && entry.ItemType == ItemInfoEntryTypes.ImageGrid)
            {
                ItemLocationEntry locationEntry = TryGetItemLocation(itemId);

                if (locationEntry != null)
                {
                    if (locationEntry.TotalItemSize < ImageGridDescriptor.SmallDescriptorLength)
                    {
                        ExceptionUtil.ThrowFormatException("Invalid image grid descriptor length.");
                    }

                    using (AvifItemData itemData = ReadItemData(locationEntry))
                    {
                        Stream stream = null;

                        try
                        {
                            stream = itemData.GetStream();

                            using (EndianBinaryReader imageGridReader = new EndianBinaryReader(stream, this.reader.Endianess, this.arrayPool))
                            {
                                stream = null;

                                return(new ImageGridDescriptor(imageGridReader, itemData.Length));
                            }
                        }
                        finally
                        {
                            stream?.Dispose();
                        }
                    }
                }
            }

            return(null);
        }
Example #6
0
        public byte[] GetXmpData()
        {
            VerifyNotDisposed();

            ItemLocationEntry entry = this.parser.TryGetXmpLocation(this.primaryItemId);

            if (entry != null)
            {
                ulong length = entry.TotalItemSize;

                // Ignore any XMP packets that are larger than 2GB.
                if (length < int.MaxValue)
                {
                    using (AvifItemData itemData = this.parser.ReadItemData(entry))
                    {
                        return(itemData.ToArray());
                    }
                }
            }

            return(null);
        }
Example #7
0
        public static void DecompressColor(AvifItemData colorImage,
                                           CICPColorData?colorConversionInfo,
                                           DecodeInfo decodeInfo,
                                           Surface fullSurface)
        {
            if (colorImage is null)
            {
                ExceptionUtil.ThrowArgumentNullException(nameof(colorImage));
            }

            if (decodeInfo is null)
            {
                ExceptionUtil.ThrowArgumentNullException(nameof(decodeInfo));
            }

            if (fullSurface is null)
            {
                ExceptionUtil.ThrowArgumentNullException(nameof(fullSurface));
            }


            DecoderStatus status = DecoderStatus.Ok;

            unsafe
            {
                colorImage.UseBufferPointer((ptr, length) =>
                {
                    BitmapData bitmapData = new BitmapData
                    {
                        scan0  = fullSurface.Scan0.Pointer,
                        width  = (uint)fullSurface.Width,
                        height = (uint)fullSurface.Height,
                        stride = (uint)fullSurface.Stride
                    };
                    UIntPtr colorImageSize = new UIntPtr(length);

                    if (colorConversionInfo.HasValue)
                    {
                        CICPColorData colorData = colorConversionInfo.Value;

                        if (IntPtr.Size == 8)
                        {
                            status = AvifNative_64.DecompressColorImage(ptr,
                                                                        colorImageSize,
                                                                        ref colorData,
                                                                        decodeInfo,
                                                                        ref bitmapData);
                        }
                        else
                        {
                            status = AvifNative_86.DecompressColorImage(ptr,
                                                                        colorImageSize,
                                                                        ref colorData,
                                                                        decodeInfo,
                                                                        ref bitmapData);
                        }
                    }
                    else
                    {
                        if (IntPtr.Size == 8)
                        {
                            status = AvifNative_64.DecompressColorImage(ptr,
                                                                        colorImageSize,
                                                                        IntPtr.Zero,
                                                                        decodeInfo,
                                                                        ref bitmapData);
                        }
                        else
                        {
                            status = AvifNative_86.DecompressColorImage(ptr,
                                                                        colorImageSize,
                                                                        IntPtr.Zero,
                                                                        decodeInfo,
                                                                        ref bitmapData);
                        }
                    }
                });
            }

            if (status != DecoderStatus.Ok)
            {
                HandleError(status);
            }
        }