コード例 #1
0
        private void tbCreateTIFF_Click(object sender, System.EventArgs e)
        {
            if (sfdCreateTIFF.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            DateTime startTime = DateTime.Now;

            // image size
            int   width        = 1024 * Convert.ToInt16(tbSizeFactorX.Text);
            int   height       = 1024 * Convert.ToInt16(tbSizeFactorY.Text);
            short rowsPerStrip = (short)height;

            // create file
            IntPtr newTiff = LibTIFF.Open(sfdCreateTIFF.FileName, "w");
            IntPtr pBuffer;

            if (chkSaveStrips.Checked)
            {
                rowsPerStrip = Convert.ToInt16(tbSaveRowsPerStrip.Text);
            }
            // set required tag. WATCH THE TYPES!!!
            LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_IMAGEWIDTH, (short)width);
            LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_IMAGELENGTH, (short)height);
            LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_BITSPERSAMPLE, (int)16);
            LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_SAMPLESPERPIXEL, (short)1);
            LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_ROWSPERSTRIP, (short)rowsPerStrip);
            LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_PHOTOMETRIC, (short)TIFFTags.PHOTOMETRIC_MINISBLACK);
            LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_PLANARCONFIG, (short)TIFFTags.PLANARCONFIG_CONTIG);

            if (chkSaveStrips.Checked)
            {
                // write strips
                pBuffer = LibTIFF._malloc(width * 2 * rowsPerStrip);

                // assume raw data to be provided as short[]
                short[] shortbits = new short[width * rowsPerStrip];

                // strips
                int strips = height / rowsPerStrip;
                for (int strip = 0; strip < strips; strip++)
                {
                    for (int y = 0; y < rowsPerStrip; y++)
                    {
                        // pixels in line: set to specific gray value
                        for (int x = 0; x < width; x++)
                        {
                            // short = -32,768 to 32,767 = signed 16-bit integer
                            // ushort is not supported by Marshal.Copy
                            shortbits[y * width + x] = (short)(10 * y);
                        }
                    }
                    // copy data to unsafe context
                    Marshal.Copy(shortbits, 0, pBuffer, width * rowsPerStrip);
                    // ... and write to file
                    int ret = LibTIFF.WriteRawStrip(newTiff, (uint)strip, pBuffer, width * 2 * rowsPerStrip);
                    if (ret == -1)
                    {
                        Console.WriteLine("writing strip " + strip + " returned " + ret);
                    }
                }
            }
            else
            {
                // write scanline by scanline
                pBuffer = LibTIFF._malloc(width * 2);                   // 16 bpp

                // assume raw data to be provided as short[]
                short[] shortbits = new short[width];

                // lines
                for (int y = 0; y < height; y++)
                {
                    // pixels in line: set to specific gray value
                    for (int x = 0; x < width; x++)
                    {
                        // short = -32,768 to 32,767 = signed 16-bit integer
                        // ushort is not supported by Marshal.Copy
                        shortbits[x] = (short)(100 * y);
                    }
                    // copy data to unsafe context
                    Marshal.Copy(shortbits, 0, pBuffer, width);
                    // ... and write to file
                    if (!LibTIFF.WriteScanline(newTiff, pBuffer, (uint)y))
                    {
                        Console.WriteLine("writing line " + y + " failed.");
                    }
                }
            }

            LibTIFF._free(pBuffer);
            LibTIFF.Close(newTiff);
            MessageBox.Show("Time: " + (DateTime.Now - startTime));
        }
コード例 #2
0
        private void btnOpenTIFF_Click(object sender, System.EventArgs e)
        {
            if (ofdOpenTIFF.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            // open TIFF file
            string filename = ofdOpenTIFF.FileName;

            tiff = LibTIFF.Open(filename, "r");

            if (tiff == IntPtr.Zero)
            {
                MessageBox.Show("Failed to open " + filename);
                return;
            }

            // report some file info
            tbTIFFPtr.Text            = tiff.ToString();
            tbRasterScanlineSize.Text = LibTIFF.RasterScanlineSize(tiff).ToString();
            tbScanlineSize.Text       = LibTIFF.ScanlineSize(tiff).ToString();

            UInt32 width, length;

            if (LibTIFF.GetField(tiff, (int)TIFFTags.TIFFTAG_IMAGEWIDTH, out width) &&
                LibTIFF.GetField(tiff, (int)TIFFTags.TIFFTAG_IMAGELENGTH, out length))
            {
                tbImageDimensions.Text = width.ToString() + "x" + length.ToString();
            }
            if (LibTIFF.IsTiled(tiff))
            {
                if (LibTIFF.GetField(tiff, (int)TIFFTags.TIFFTAG_TILEWIDTH, out width) &&
                    LibTIFF.GetField(tiff, (int)TIFFTags.TIFFTAG_TILELENGTH, out length))
                {
                    tbTileDimensions.Text = width.ToString() + "x" + length.ToString();
                }
            }
            else
            {
                tbTileDimensions.Text = "not tiled";
                UInt32 rows;
                if (LibTIFF.GetField(tiff, (int)TIFFTags.TIFFTAG_ROWSPERSTRIP, out rows))
                {
                    tbRowsPerStrip.Text = rows.ToString();
                }
            }
            UInt16 bps;

            if (LibTIFF.GetField(tiff, (int)TIFFTags.TIFFTAG_BITSPERSAMPLE, out bps))
            {
                tbBitsPerSample.Text = bps.ToString();
            }

            UInt16 spp;

            if (LibTIFF.GetField(tiff, (int)TIFFTags.TIFFTAG_SAMPLESPERPIXEL, out spp))
            {
                tbSamplesPerPixel.Text = spp.ToString();
            }

            // place TIFF content in the picture box
            pbPicture.SizeMode = PictureBoxSizeMode.StretchImage;
            pbPicture.Image    = GetBitmapFromTIFFHandle(tiff, 0, 65535);
            System.Console.WriteLine("System.GC.GetTotalMemory: " + System.GC.GetTotalMemory(true));
        }