<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bee Eee Blog &#187; c# graphics</title>
	<atom:link href="http://blog.bee-eee.com/tag/c-graphics/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bee-eee.com</link>
	<description>-- C# hints and tips</description>
	<lastBuildDate>Wed, 23 Dec 2009 19:00:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>c# reading Tiff using libtiff</title>
		<link>http://blog.bee-eee.com/2008/03/20/c-reading-tiff-using-libtiff/</link>
		<comments>http://blog.bee-eee.com/2008/03/20/c-reading-tiff-using-libtiff/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 17:10:50 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[c# coding GUI]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[c# graphics]]></category>

		<guid isPermaLink="false">http://blog.bee-eee.com/2008/03/20/c-reading-tiff-using-libtiff/</guid>
		<description><![CDATA[Here is the promised code for reading Tiffs.  I make no claim that this code will read all TIFF files that would be quite the accomplishment indeed.  Because of time constraints,  I&#8217;m just going to cut this right out of the application I&#8217;m writing.  As way of explanation the class ImageArray is basically an array [...]]]></description>
			<content:encoded><![CDATA[<table>
<tr>
<td>Here is the promised code for reading Tiffs.  I make no claim that this code will read all TIFF files that would be quite the accomplishment indeed.  Because of time constraints,  I&#8217;m just going to cut this right out of the application I&#8217;m writing.  As way of explanation the class ImageArray is basically an array of pixels wrapped around a class.  A pixel here is a class that consists of r,g,b colors and a few other things.  In my last post I had some of the libtiff header defined.  I&#8217;m going to skip that here<br />
and just post the code.
</td>
<td width="120"><iframe src="http://rcm.amazon.com/e/cm?t=beeebl-20&#038;o=1&#038;p=8&#038;l=as1&#038;asins=0321555562&#038;fc1=777777&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=000000&#038;f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</td>
</tr>
</table>
<pre class="code">
public static ImageArray LoadTiff(String Image_Path)
{
    ImageArray arr = null;
    unsafe
    {
        UInt32 w = 0;
        UInt32 h = 0;
        uint samples = 3;
        uint bits = 8;
        int i, j, ptr, lptr;

        int tif = TIFFOpen(Image_Path, "r");

        if (0 != tif)
        {
            TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, ref w);
            TIFFGetField(tif, TIFFTAG_IMAGELENGTH, ref h);
            TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, ref bits);
            TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, ref samples);
            arr = new ImageArray((int)w, (int)h);

            // make sure we get all of the data.
            if (bits == 16)
            {
                UInt32 size = w;
                ushort* raster = (ushort *)_TIFFmalloc((uint)(size * sizeof(ushort) * samples));
                // read in all of the lines
                for (j = 0; j &lt; h; j++)
                {
                    TIFFReadScanline(tif, (byte *)raster, j, 0);
                    // copy line into data
                    for (i = 0, ptr=0; i &lt; w; i++, ptr+=(int)samples)
                    {
                        if (samples == 3)
                        {
                            arr.Data[i + j * w].SetPixel(raster[i], raster[i + 1], raster[i + 2]);
                        }
                        else
                        {
                            ushort val = raster[i + samples - 1];
                            arr.Data[i + j * w].SetPixel(val,val,val);
                        }
                    }
                }

                _TIFFfree((byte *)raster);
            }
            else
            {
                // anything beside 16 bit
                UInt32 size = h * w;
                byte *raster = _TIFFmalloc( size );
                int val = TIFFReadRGBAImage(tif, w, h, raster, 0);
                // copy image data into a bitmap
                if (val != 0)
                {
                    for (j = 0, ptr = 0, lptr=0; j &lt; h; j++)
                    {
                        for (i = 0; i &lt; w; i++, ptr+=4, ptr++)
                        {
                            arr.Data[ptr].SetPixel((ushort)raster[lptr], (ushort)raster[lptr + 1], (ushort)raster[lptr + 2]);
                        } // for i
                    } // for j
                } // if val

                _TIFFfree(raster);
            }
            TIFFClose(tif);
        } // if tif
    } // unsafe

    return arr;
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.bee-eee.com/2008/03/20/c-reading-tiff-using-libtiff/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

