public override TiffHandle WriteData(byte[] data)
        {
            var _handle = TiffHandle.Create(this.BaseStream);

            this.BaseStream.Write(data, 0, data.Length);
            return(_handle);
        }
Пример #2
0
        public bool SaveTiff(string Filename)
        {
            try {
                using (var stream = File.Create(Filename))
                {
                    var writer = TiffWriter.Create(stream);
                    var handle = writer.WriteHeader(); // Write Header

                    var bitPerSample = (ushort)Dictionary[TiffTags.BitsPerSample][0];

                    var strips          = new TiffHandle[ValuesList.Count];
                    var stripByteCounts = new uint[ValuesList.Count];
                    for (int i = 0; i < ValuesList.Count; i++)
                    {
                        var _buf = new byte[ValuesList[i].Length];
                        Buffer.BlockCopy(ValuesList[i], 0, _buf, 0, ValuesList[i].Length);

                        // Generating data ...
                        strips[i]          = writer.WriteData(_buf); // Write Strip
                        stripByteCounts[i] = (uint)_buf.Length;
                    }

                    // Write Image File Directory
                    handle = writer.WriteImageFileDirectory(handle, new Collection <ITag> {
                        Tag <uint> .Create(TiffTags.ImageWidth, ImageWidth),
                        Tag <uint> .Create(TiffTags.ImageLength, ImageHeight),
                        Tag <ushort> .Create(TiffTags.BitsPerSample, bitPerSample),
                        Tag <TiffCompression> .Create(TiffTags.Compression, TiffCompression.NONE),
                        Tag <TiffPhotoMetric> .Create(TiffTags.PhotometricInterpretation, TiffPhotoMetric.BlackIsZero),
                        Tag <TiffHandle> .Create(TiffTags.StripOffsets, strips),
                        Tag <ushort> .Create(TiffTags.SamplesPerPixel, 1),
                        Tag <uint> .Create(TiffTags.RowsPerStrip, ImageWidth),
                        Tag <uint> .Create(TiffTags.StripByteCounts, stripByteCounts),
                        Tag <char> .Create(TiffTags.SampleFormat, "INT".ToCharArray()),
                        Tag <double> .Create((TiffTags)33550, 0.000277777777778001, 0.000277777777778001, 0),
                        Tag <double> .Create((TiffTags)33922, 0, 0, 0, 0, 0, 0),
                        Tag <double> .Create((TiffTags)34735, 1, 1, 0, 7, 1024, 0, 1, 2, 1025, 0, 1, 1, 2048,
                                             0, 1, 4326, 2049, 34737, 7, 0, 2054, 0, 1, 9102, 2057, 34736, 1, 1, 2059, 34736, 1, 0),
                        Tag <double> .Create((TiffTags)298.257223563, 6378137),
                        Tag <char> .Create((TiffTags)34737, "WGS 84|".ToCharArray()),
                        Tag <char> .Create(TiffTags.Software, "BirdEye Software".ToCharArray()),
                        Tag <char> .Create(TiffTags.Copyright, "(c) Alos Gdem".ToCharArray())
                    });
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Пример #3
0
        public byte[] ReadData(TiffHandle handle, long count)
        {
            if (!handle.IsOpen)
            {
                throw new ArgumentException("Дескриптор закрыт. Handle is closed.");
            }
            var _position = this.Context.Reader.BaseStream.Position;

            try {
                this.Context.Reader.BaseStream.Seek(handle.Offset, SeekOrigin.Begin);
                var _result = new byte[count];
                this.Context.Reader.BaseStream.Read(_result, 0, _result.Length);
                return(_result);
            } finally {
                this.Context.Reader.BaseStream.Seek(_position, SeekOrigin.Begin);
            }
        }
Пример #4
0
        internal static TiffHandle Write <T>(Stream stream, T obj)
        {
            var _size = Marshal.SizeOf(typeof(T));
            var _ptr  = Marshal.AllocHGlobal(_size);

            try {
                Marshal.StructureToPtr(obj, _ptr, true);
                var _data = new byte[_size];
                Marshal.Copy(_ptr, _data, 0, _data.Length);
                var _position = stream.Position;
                stream.Write(_data, 0, _data.Length);
                foreach (var _field in typeof(T).GetFields())
                {
                    if (_field.GetCustomAttributes(typeof(TiffHandleAttribute), false).Length > 0)
                    {
                        return(TiffHandle.Create(_position + Marshal.OffsetOf(typeof(T), _field.Name).ToInt64()));
                    }
                }
            } finally {
                Marshal.FreeHGlobal(_ptr);
            }
            return(null);
        }
 public override byte[] ReadData(TiffHandle handle, long count)
 {
     return(this.Context.ReadData(handle, count));
 }
        public override TiffHandle ReadHandle()
        {
            var _val = this.Context.ReadValue();

            return(_val != null?TiffHandle.Create(Convert.ToInt64(_val)) : null);
        }
        public override TiffHandle WriteImageFileDirectory(TiffHandle owner, Collection <ITag> tags)
        {
            if (!owner.IsOpen)
            {
                throw new ArgumentException("Дескриптор владельца закрыт. Owner handle is closed.");
            }
            owner.Close(this.BaseStream);
            var _delayed = new Collection <_DelayedData>();

            #region IFD

            Collection <ITag> _tags = new Collection <ITag>(tags);
            for (int i = 0; i < _tags.Count; i++)
            {
                for (int ii = i; ii < _tags.Count; ii++)
                {
                    if ((ushort)_tags[ii].TagId < (ushort)_tags[i].TagId)
                    {
                        var _tag = _tags[i];
                        _tags[i]  = _tags[ii];
                        _tags[ii] = _tag;
                    }
                }
            }
            Helper.Write(this.BaseStream, (ushort)_tags.Count);
            foreach (_Tag _tag in _tags)
            {
                if (_tag.ItemType != typeof(TiffHandle))
                {
                    int _itemSize = TiffDataTypeHelper.Sizeof(_tag.TiffDataType);
                    if (_itemSize * _tag.Count > 4)
                    {
                        #region Массив значений

                        var _handle = Helper.Write(this.BaseStream, new TiffDirEntry {
                            tag   = _tag.TagId,
                            type  = _tag.TiffDataType,
                            count = (uint)_tag.Count
                        });
                        using (var _stream = new MemoryStream()) {
                            for (int i = 0; i < _tag.Count; i++)
                            {
                                Helper.Write(_stream, _tag.GetValue(i));
                            }
                            _delayed.Add(new _DelayedData(_handle, _stream.ToArray()));
                        }

                        #endregion
                    }
                    else
                    {
                        #region Массив, вписанный в поле offset

                        using (var _stream = new MemoryStream(new byte[4], true)) {
                            for (int i = 0; i < _tag.Count; i++)
                            {
                                Helper.Write(_stream, _tag.GetValue(i));
                            }
                            _stream.Position = 0;
                            Helper.Write(this.BaseStream, new TiffDirEntry {
                                tag    = _tag.TagId,
                                type   = _tag.TiffDataType,
                                count  = (uint)_tag.Count,
                                offset = new BinaryReader(_stream).ReadUInt32()
                            });
                        }

                        #endregion
                    }
                }
                else
                {
                    if (_tag.Count > 1)
                    {
                        #region Массив смещений

                        var _handle = Helper.Write(this.BaseStream, new TiffDirEntry {
                            tag   = _tag.TagId,
                            type  = _tag.TiffDataType,
                            count = (uint)_tag.Count
                        });
                        using (var _stream = new MemoryStream()) {
                            for (int i = 0; i < _tag.Count; i++)
                            {
                                using (var _value = _tag.GetValue(i) as TiffHandle) {
                                    Helper.Write(_stream, (uint)_value.Offset);
                                }
                            }
                            _delayed.Add(new _DelayedData(_handle, _stream.ToArray()));
                        }

                        #endregion
                    }
                    else
                    {
                        #region Смещение

                        using (var _value = _tag.GetValue(0) as TiffHandle) {
                            Helper.Write(this.BaseStream, new TiffDirEntry {
                                tag    = _tag.TagId,
                                type   = _tag.TiffDataType,
                                count  = (uint)_tag.Count,
                                offset = (uint)_value.Offset
                            });
                        }

                        #endregion
                    }
                }
            }
            var _ifdHandle = TiffHandle.Create(this.BaseStream);
            Helper.Write(this.BaseStream, (uint)0);

            #endregion

            #region Отложенные фрагменты

            foreach (var _item in _delayed)
            {
                _item.Owner.Close(this.BaseStream);
                this.BaseStream.Write(_item.Data, 0, _item.Data.Length);
            }

            #endregion

            return(_ifdHandle);
        }
 internal _DelayedData(TiffHandle owner, byte[] data)
 {
     this.Owner = owner;
     this.Data  = data;
 }
 public byte[] ReadData(TiffHandle handle, long count)
 {
     return(this.Current.ReadData(handle, count));
 }