public static void Main()
        {
            // Register the extender callback
            // It's a good idea to keep track of the previous tag extender (if any) so that we can call it
            // from our extender allowing a chain of customizations to take effect.
            m_parentExtender = Tiff.SetTagExtender(TagExtender);

            File.Copy(@"Sample Data\dummy.tif", @"Sample Data\ToBeModifed.tif", true);

            string existingTiffName = @"Sample Data\ToBeModifed.tif";

            using (Tiff image = Tiff.Open(existingTiffName, "a"))
            {
                // we should rewind to first directory (first image) because of append mode
                image.SetDirectory(0);

                // set the custom tag
                string value = "<GDALMetadata>\n<Item name=\"IMG_GUID\">" +
                               "817C0168-0688-45CD-B799-CF8C4DE9AB2B</Item>\n<Item" +
                               " name=\"LAYER_TYPE\" sample=\"0\">athematic</Item>\n</GDALMetadata>";
                image.SetField(TIFFTAG_GDAL_METADATA, value);

                // rewrites directory saving new tag
                image.CheckpointDirectory();
            }

            // restore previous tag extender
            Tiff.SetTagExtender(m_parentExtender);
        }
예제 #2
0
        public static void Main()
        {
            string fileName   = "random.tif";
            int    totalPages = 3;
            int    width      = 100;
            int    height     = 150;

            using (Tiff output = Tiff.Open(fileName, "w"))
            {
                for (short page = 0; page <= totalPages - 1; page++)
                {
                    if (page != 0)
                    {
                        // save previous directory data
                        output.WriteDirectory();

                        // create new directory and make it current
                        output.CreateDirectory();
                        output.SetDirectory(page);
                    }

                    // setup image properties
                    output.SetField(TiffTag.IMAGEWIDTH, width);
                    output.SetField(TiffTag.IMAGELENGTH, height);
                    output.SetField(TiffTag.SAMPLESPERPIXEL, 1);
                    output.SetField(TiffTag.BITSPERSAMPLE, 16);
                    output.SetField(TiffTag.ORIENTATION, Orientation.TOPLEFT);
                    output.SetField(TiffTag.ROWSPERSTRIP, height);
                    output.SetField(TiffTag.XRESOLUTION, 88.0);
                    output.SetField(TiffTag.YRESOLUTION, 88.0);
                    output.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.CENTIMETER);
                    output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
                    output.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISBLACK);
                    output.SetField(TiffTag.COMPRESSION, Compression.NONE);
                    output.SetField(TiffTag.FILLORDER, FillOrder.MSB2LSB);

                    // cause tags data to be put in an image
                    output.CheckpointDirectory();

                    // create image data
                    Random random = new Random();
                    for (int i = 0; i <= height - 1; i++)
                    {
                        short[] samples = new short[width];
                        for (int j = 0; j <= width - 1; j++)
                        {
                            samples[j] = Convert.ToInt16(random.Next(0, short.MaxValue));
                        }

                        byte[] buf = new byte[samples.Length * 2];
                        Buffer.BlockCopy(samples, 0, buf, 0, buf.Length);
                        output.WriteScanline(buf, i);
                    }
                }
            }

            System.Diagnostics.Process.Start(fileName);
        }
예제 #3
0
        private static void SetTags(Tiff sourceTiff, string name, double north, double west, int width, int height, Tiff geoTiff)
        {
            //LibTiff.net doesn't seem to be thread safe
            lock (sourceTiff)
            {
                CopyTags(sourceTiff, geoTiff);

                geoTiff.SetField(TiffTag.DOCUMENTNAME, name + ".tif");
                geoTiff.SetField(TiffTag.IMAGEWIDTH, width);
                geoTiff.SetField(TiffTag.IMAGELENGTH, height);

                SetGeoTiffTags(sourceTiff, north, west, geoTiff);
            }

            geoTiff.CheckpointDirectory();
        }
예제 #4
0
        private static void AddTag(string dir, string value)
        {
            // Register the extender callback
            m_parentExtender = Tiff.SetTagExtender(TagExtender);

            using (Tiff image = Tiff.Open(dir, "a"))
            {
                image.SetDirectory((short)(image.NumberOfDirectories() - 1));
                // we should rewind to first directory (first image) because of append mode

                // set the custom tag
                image.SetField(TIFFTAG_CellTool_METADATA, value);

                // rewrites directory saving new tag
                image.CheckpointDirectory();
            }

            // restore previous tag extender
            Tiff.SetTagExtender(m_parentExtender);
        }
예제 #5
0
        public void TestAppendedNulls()
        {
            // A test to see if nulls get appended to custom ascii tags
            // when they don't have a valid FieldInfo object registered.
            // Contributed by Alex J. Kennedy

            int    freetag  = 42117;
            int    freetag2 = 42118;
            string value    = "abc";
            string value2   = "def";

            byte[] expectedReadValue = new byte[] { (byte)'a', (byte)'b', (byte)'c', (byte)'\0' };

            string filename = "testAppendedNulls.tif";

            // Clean
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            // Create a basic tiff image
            CreateBasicTif(filename);

            // Register the new tag
            TagToWrite = freetag;
            Tiff.SetTagExtender(TagExtender);

            using (Tiff image = Tiff.Open(filename, "a"))
            {
                image.SetDirectory(0);

                // write "abc" to directory 0, tag 42117
                image.SetField((TiffTag)freetag, value);

                // save
                image.CheckpointDirectory();
            }

            // Reset TagExtender
            Tiff.SetTagExtender(null);

            // Try to read it back in
            using (Tiff image = Tiff.Open(filename, "r"))
            {
                image.SetDirectory(0);

                FieldValue[] fv = image.GetField((TiffTag)freetag);
                Assert.AreEqual(2, fv.Length);

                // Ensure that a null was appended.
                byte[] readValue = (byte[])(fv[1].Value);
                Assert.AreEqual(4, (int)fv[0].Value);
                Assert.AreEqual(4, readValue.Length);
                Assert.AreEqual('a', readValue[0]);
                Assert.AreEqual('b', readValue[1]);
                Assert.AreEqual('c', readValue[2]);
                Assert.AreEqual('\0', readValue[3]);
            }

            // Now write a different tag to see if it appends a null char to 42117
            TagToWrite = freetag2;
            Tiff.SetTagExtender(TagExtender);

            using (Tiff image = Tiff.Open(filename, "a"))
            {
                image.SetDirectory(0);

                // write "def" to directory 0, tag 42118
                image.SetField((TiffTag)freetag2, value2);

                // save
                image.CheckpointDirectory();
            }

            Tiff.SetTagExtender(null);

            // Read in tag 42117 to make sure it doesn't have an extra null appended
            using (Tiff image = Tiff.Open(filename, "r"))
            {
                image.SetDirectory(0);

                TiffFieldInfo tfi  = image.FindFieldInfo((TiffTag)freetag, TiffType.ANY);
                TiffFieldInfo tfi2 = image.FindFieldInfo((TiffTag)freetag2, TiffType.ANY);

                FieldValue[] fv = image.GetField((TiffTag)freetag);
                Assert.AreEqual(2, fv.Length);

                // Ensure that only one null was appended.
                byte[] readValue = (byte[])(fv[1].Value);
                Assert.AreEqual(4, (int)fv[0].Value);
                Assert.AreEqual(4, readValue.Length);
                Assert.AreEqual('a', readValue[0]);
                Assert.AreEqual('b', readValue[1]);
                Assert.AreEqual('c', readValue[2]);
                Assert.AreEqual('\0', readValue[3]);
            }
        }