Пример #1
0
        private static IExifValue GetOffsetValue(List <IExifValue> ifdValues, List <IExifValue> values, ExifTag offset)
        {
            int index = -1;

            for (int i = 0; i < ifdValues.Count; i++)
            {
                if (ifdValues[i].Tag == offset)
                {
                    index = i;
                }
            }

            if (values.Count > 0)
            {
                if (index != -1)
                {
                    return(ifdValues[index]);
                }

                ExifValue result = ExifValues.Create(offset);
                ifdValues.Add(result);

                return(result);
            }
            else if (index != -1)
            {
                ifdValues.RemoveAt(index);
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Sets the value of the specified tag.
        /// </summary>
        /// <param name="tag">The tag of the exif value.</param>
        /// <param name="value">The value.</param>
        internal void SetValueInternal(ExifTag tag, object value)
        {
            foreach (IExifValue exifValue in this.Values)
            {
                if (exifValue.Tag == tag)
                {
                    exifValue.TrySetValue(value);
                    return;
                }
            }

            ExifValue newExifValue = ExifValues.Create(tag);

            if (newExifValue is null)
            {
                throw new NotSupportedException();
            }

            newExifValue.TrySetValue(value);
            this.values.Add(newExifValue);
        }
Пример #3
0
        private bool TryReadValue(out ExifValue exifValue)
        {
            exifValue = default;

            // 2   | 2    | 4     | 4
            // tag | type | count | value offset
            if (this.RemainingLength < 12)
            {
                return(false);
            }

            var          tag      = (ExifTagValue)this.ReadUInt16();
            ExifDataType dataType = EnumUtils.Parse(this.ReadUInt16(), ExifDataType.Unknown);

            // Ensure that the data type is valid
            if (dataType == ExifDataType.Unknown)
            {
                return(false);
            }

            uint numberOfComponents = this.ReadUInt32();

            // Issue #132: ExifDataType == Undefined is treated like a byte array.
            // If numberOfComponents == 0 this value can only be handled as an inline value and must fallback to 4 (bytes)
            if (dataType == ExifDataType.Undefined && numberOfComponents == 0)
            {
                numberOfComponents = 4;
            }

            uint size = numberOfComponents * ExifDataTypes.GetSize(dataType);

            this.TryReadSpan(4, out ReadOnlySpan <byte> offsetBuffer);

            object value;

            if (size > 4)
            {
                int  oldIndex = this.position;
                uint newIndex = this.ConvertToUInt32(offsetBuffer);

                // Ensure that the new index does not overrun the data
                if (newIndex > int.MaxValue)
                {
                    this.AddInvalidTag(new UnkownExifTag(tag));
                    return(false);
                }

                this.position = (int)newIndex;

                if (this.RemainingLength < size)
                {
                    this.AddInvalidTag(new UnkownExifTag(tag));

                    this.position = oldIndex;
                    return(false);
                }

                this.TryReadSpan((int)size, out ReadOnlySpan <byte> dataBuffer);

                value         = this.ConvertValue(dataType, dataBuffer, numberOfComponents);
                this.position = oldIndex;
            }
            else
            {
                value = this.ConvertValue(dataType, offsetBuffer, numberOfComponents);
            }

            exifValue = ExifValues.Create(tag) ?? ExifValues.Create(tag, dataType, numberOfComponents);

            if (exifValue is null)
            {
                this.AddInvalidTag(new UnkownExifTag(tag));
                return(false);
            }

            if (!exifValue.TrySetValue(value))
            {
                return(false);
            }

            return(true);
        }