Пример #1
0
        public int IntegerValue(ImageFileDirectory ifd, int tag)
        {
            if (TIFFData == null || ifd == null)
            {
                return(0);
            }

            var field = ifd.GetTag(tag);

            if (field == null || field.Count < 1)
            {
                return(0);
            }

            if (field.Type == 3)
            {
                return(field.AsShort());
            }

            if (field.Type == 4)
            {
                return(LongValue(ifd, tag));
            }

            return(0);
        }
Пример #2
0
        /// <summary>
        /// Returns the number of times the shutter has opened on the camera.
        /// </summary>
        /// <returns>The number of times the camera's shutter has opened.</returns>
        public int ShutterCount()
        {
            // See if we can pull the data from a makernote.

            string make = Make().ToLowerInvariant();

            if (make.Contains("nikon") == true && ExifIFD != null)
            {
                // Find the maker note...

                var maker_note_field = ExifIFD.GetTag(MakerNote);

                if (maker_note_field != null)
                {
                    byte[] maker_note_buffer = new byte[maker_note_field.Count - 10];

                    Array.Copy(TIFFData, maker_note_field.OffsetToValue + 10, maker_note_buffer, 0, maker_note_field.Count - 10);

                    var tiff_header = new TIFFHeader();

                    if (tiff_header.FromBytes(maker_note_buffer, 0) == true)
                    {
                        var maker_note = new ImageFileDirectory();

                        if (maker_note.FromBytes(maker_note_buffer, tiff_header.OffsetToZerothIFD, tiff_header.LittleEndian) == true)
                        {
                            return(IntegerValue(maker_note, Exif_MakerNote_Nikon3_ShutterCount));
                        }
                    }
                }
            }

            return(0);
        }
Пример #3
0
 /// <summary>
 /// The default constructor.
 /// </summary>
 public Exif()
 {
     Header = new TIFFHeader();
     IFD0   = new ImageFileDirectory();
     IFD0.InitializeIFD0TagNames();
     ExifIFD  = null;
     GPSIFD   = null;
     TIFFData = null;
 }
Пример #4
0
        public string StringValue(ImageFileDirectory ifd, int tag)
        {
            if (TIFFData == null || ifd == null)
            {
                return(string.Empty);
            }

            return(StringValue(ifd, tag, TIFFData));
        }
Пример #5
0
        public string SerialNumber()
        {
            string return_value = StringValue(Exif_Photo_BodySerialNumber);

            if (string.IsNullOrEmpty(return_value) == false)
            {
                return(return_value);
            }

            // See if we can pull the data from a makernote.

            string make = Make().ToLowerInvariant();

            if (make.Contains("nikon") == true && ExifIFD != null)
            {
                // Find the maker note...

                InteroperabilityField maker_note_field = ExifIFD.GetTag(MakerNote);

                if (maker_note_field != null)
                {
                    byte[] maker_note_buffer = new byte[maker_note_field.Count - 10];

                    Array.Copy(TIFFData, maker_note_field.OffsetToValue + 10, maker_note_buffer, 0, maker_note_field.Count - 10);

                    TIFFHeader th = new TIFFHeader();

                    if (th.FromBytes(maker_note_buffer, 0) == true)
                    {
                        ImageFileDirectory maker_note = new ImageFileDirectory();

                        if (maker_note.FromBytes(maker_note_buffer, th.OffsetToZerothIFD, th.LittleEndian) == true)
                        {
                            // Now read from the Maker Note Buffer!

                            return_value = StringValue(maker_note, Exif_MakerNote_Nikon3_SerialNumber, maker_note_buffer);

                            if (string.IsNullOrEmpty(return_value) == true)
                            {
                                return_value = StringValue(maker_note, Exif_MakerNote_Nikon3_SerialNO, maker_note_buffer);

                                if (return_value.StartsWith("NO=") == true)
                                {
                                    return(return_value.Substring(3, return_value.Length - 3).Trim());
                                }
                            }
                        }
                    }

                    return(return_value);
                }
            }

            return(string.Empty);
        }
Пример #6
0
        public DateTime Date(ImageFileDirectory ifd, int tag)
        {
            string date_string = StringValue(ifd, tag);

            if (string.IsNullOrEmpty(date_string) == true)
            {
                return(DateTime.MinValue);
            }

            if (date_string.Length != 10)
            {
                return(DateTime.MinValue);
            }

            if (date_string[4] != ':' || date_string[7] != ':')
            {
                return(DateTime.MinValue);
            }

            int year  = 0;
            int month = 0;
            int day   = 0;

            string string_to_parse = date_string.Substring(0, 4);

            if (Int32.TryParse(string_to_parse, out year) == false)
            {
                return(DateTime.MinValue);
            }

            string_to_parse = date_string.Substring(5, 2);

            if (Int32.TryParse(string_to_parse, out month) == false)
            {
                return(DateTime.MinValue);
            }

            string_to_parse = date_string.Substring(8, 2);

            if (Int32.TryParse(string_to_parse, out day) == false)
            {
                return(DateTime.MinValue);
            }

            return(new DateTime(year, month, day));
        }
Пример #7
0
        public double DecimalValue(ImageFileDirectory ifd, int tag, byte[] buffer)
        {
            if (buffer == null || ifd == null)
            {
                return(0.0D);
            }

            var field = ifd.GetTag(tag);

            if (field == null || field.Type != 5)
            {
                return(0.0D);
            }

            int numerator   = TIFFHeader.ToInt32(buffer, field.OffsetToValue, Header.LittleEndian);
            int denominator = TIFFHeader.ToInt32(buffer, field.OffsetToValue + 4, Header.LittleEndian);

            double return_value = Rational(numerator, denominator);

            return(return_value);
        }
Пример #8
0
        public bool GPSTime(ImageFileDirectory ifd, int tag, out double hours, out double minutes, out double seconds)
        {
            hours   = 0.0D;
            minutes = 0.0D;
            seconds = 0.0D;

            if (TIFFData == null || ifd == null)
            {
                return(false);
            }

            var field = ifd.GetTag(tag);

            if (field == null || field.Type != 5)
            {
                return(false);
            }

            if (field.Count < 3)
            {
                return(false);
            }

            int[] integer_array = new int[field.Count * 2]; // Rationals are two integers each

            int array_index = 0;

            while (array_index < integer_array.Length)
            {
                integer_array[array_index] = TIFFHeader.ToInt32(TIFFData, field.OffsetToValue + (array_index * 4), Header.LittleEndian);
                array_index++;
            }

            hours   = Rational(integer_array[0], integer_array[1]);
            minutes = Rational(integer_array[2], integer_array[3]);
            seconds = Rational(integer_array[4], integer_array[5]);

            return(true);
        }
Пример #9
0
        public int LongValue(ImageFileDirectory ifd, int tag)
        {
            if (TIFFData == null || ifd == null)
            {
                return(0);
            }

            var field = ifd.GetTag(tag);

            if (field == null || field.Type != 4 || field.Count < 1)
            {
                return(0);
            }

            // Lookout for values less than 4...

            if (field.Count <= 1)
            {
                return(field.OffsetToValue);
            }

            return(0);
        }
Пример #10
0
        public double DegreesValue(ImageFileDirectory ifd, int tag)
        {
            if (TIFFData == null || ifd == null)
            {
                return(0.0D);
            }

            InteroperabilityField field = ifd.GetTag(tag);

            if (field == null || (field.Type != 5 && field.Type != 10))
            {
                return(0.0D);
            }

            if (field.Count < 3)
            {
                return(0.0D);
            }

            int[] integer_array = new int[field.Count * 2]; // Rationals are two integers each

            int array_index = 0;

            while (array_index < integer_array.Length)
            {
                integer_array[array_index] = TIFFHeader.ToInt32(TIFFData, field.OffsetToValue + (array_index * 4), Header.LittleEndian);
                array_index++;
            }

            double degrees = Rational(integer_array[0], integer_array[1]);
            double minutes = Rational(integer_array[2], integer_array[3]);
            double seconds = Rational(integer_array[4], integer_array[5]);

            double return_value = ConvertDegreesMinutesSecondsCoordinateToDecimalDegrees(degrees, minutes, seconds);

            return(return_value);
        }
Пример #11
0
        /// <summary>
        /// This parses the given bytes as EXIF information.
        /// </summary>
        /// <param name="bytes">The bytes to parse.</param>
        /// <returns>True on success, false otherwise.</returns>
        public bool FromBytes(byte[] bytes)
        {
            if (bytes == null || bytes.Length < 16)
            {
                return(false);
            }

            // Find the Exif signature...

            int array_index = 0;

            int exif_signature_offset = -1;
            int tiff_offset           = -1;

            while (array_index < (bytes.Length - 6))
            {
                if (bytes[array_index] == 'E' &&
                    bytes[array_index + 1] == 'x' &&
                    bytes[array_index + 2] == 'i' &&
                    bytes[array_index + 3] == 'f' &&
                    bytes[array_index + 4] == 0 &&
                    bytes[array_index + 5] == 0)
                {
                    exif_signature_offset = array_index;
                    array_index           = bytes.Length; // Exit the loop
                }

                array_index++;
            }

            if (exif_signature_offset < 0)
            {
                // Let's try to find the TIFF Header

                array_index = 0;

                while (array_index < (bytes.Length - 4))
                {
                    if (((bytes[array_index] == 'M' && bytes[array_index + 1] == 'M') ||
                         (bytes[array_index] == 'I' && bytes[array_index + 1] == 'I')) &&
                        bytes[array_index + 2] == 0x00 &&
                        bytes[array_index + 3] == 0x2A)
                    {
                        exif_signature_offset = array_index;
                        array_index           = bytes.Length; // Exit the loop
                    }

                    array_index++;
                }

                if (exif_signature_offset < 0)
                {
                    // Can't find the TIFF signature either
                    return(false);
                }

                tiff_offset = exif_signature_offset;
            }
            else
            {
                tiff_offset = exif_signature_offset + 6;
            }

            if (Header.FromBytes(bytes, tiff_offset) == false)
            {
                return(false);
            }

            int tiff_data_length = bytes.Length - tiff_offset;

            if (tiff_data_length > Program.OneMegabyte)
            {
                tiff_data_length = Program.OneMegabyte;
            }

            TIFFData = new byte[tiff_data_length];

            Array.Copy(bytes, tiff_offset, TIFFData, 0, tiff_data_length);

            IFD0.FromBytes(TIFFData, Header.OffsetToZerothIFD, Header.LittleEndian);

            var exif_field = IFD0.GetTag(ExifIFDPointer);

            if (exif_field != null)
            {
                ExifIFD = new ImageFileDirectory();
                ExifIFD.InitializeExifTagNames();

                ExifIFD.FromBytes(TIFFData, exif_field.OffsetToValue, Header.LittleEndian);
            }

            var gps_field = IFD0.GetTag(GpsIFDPointer);

            if (gps_field != null)
            {
                GPSIFD = new ImageFileDirectory();
                GPSIFD.InitializeGPSTagNames();

                GPSIFD.FromBytes(TIFFData, gps_field.OffsetToValue, Header.LittleEndian);
            }

            return(true);
        }
Пример #12
0
        public string StringValue(ImageFileDirectory ifd, int tag, byte[] buffer)
        {
            if (buffer == null || ifd == null)
            {
                return(string.Empty);
            }

            var field = ifd.GetTag(tag);

            if (field == null || (field.Type != 2 && field.Type != 1) || field.Count < 2)
            {
                return(string.Empty);
            }

            // Lookout for values less than 4...

            if (field.Count <= 4)
            {
                byte[] bytes = new byte[4];

                bytes[0] = (field.Count > 0) ? field.Byte0 : (byte)0;
                bytes[1] = (field.Count > 1) ? field.Byte1 : (byte)0;
                bytes[2] = (field.Count > 2) ? field.Byte2 : (byte)0;
                bytes[3] = (field.Count > 3) ? field.Byte3 : (byte)0;

                if (field.Count >= 1)
                {
                    return(System.Text.Encoding.ASCII.GetString(bytes, 0, field.Count).Trim("\0".ToCharArray()));
                }
                else
                {
                    return(string.Empty);
                }
            }

            if (field.OffsetToValue > 0 && field.Count > 1 && buffer.Length > (field.OffsetToValue + field.Count))
            {
                // last check...
                if (field.Type == 2)
                {
                    return(System.Text.Encoding.ASCII.GetString(buffer, field.OffsetToValue, field.Count).Trim("\0".ToCharArray()));
                }
                else if (field.Type == 1)
                {
                    // Could be ASCII or Unicode
                    if (buffer[field.OffsetToValue] != 0x00 && buffer[field.OffsetToValue + 1] == 0x00)
                    {
                        // Little Endian Unicode
                        return(System.Text.Encoding.Unicode.GetString(buffer, field.OffsetToValue, field.Count).Trim("\0".ToCharArray()));
                    }
                    else if (buffer[field.OffsetToValue] == 0x00 && buffer[field.OffsetToValue + 1] != 0x00)
                    {
                        // Big Endian Unicode
                        return(System.Text.Encoding.BigEndianUnicode.GetString(buffer, field.OffsetToValue, field.Count).Trim("\0".ToCharArray()));
                    }
                    else if (buffer[field.OffsetToValue] != 0x00 && buffer[field.OffsetToValue + 1] != 0x00)
                    {
                        return(System.Text.Encoding.UTF8.GetString(buffer, field.OffsetToValue, field.Count).Trim("\0".ToCharArray()));
                    }
                }
            }

            return(string.Empty);
        }
Пример #13
0
 public double DecimalValue(ImageFileDirectory ifd, int tag)
 {
     return(DecimalValue(ifd, tag, TIFFData));
 }