title: 32-bit Grayscale Tiff with floating point pixel values to array using LibTIFF.NET C#
body: <p>I just started using LibTIFF.NET in my c# application to read Tiff images as heightmaps obtained from ArcGIS servers. All I need is to populate an array with image's pixel values for terrain generation based on smooth gradients. The image is a LZW compressed 32-bit Grayscale Tiff with floating point pixel values representing elevaion in meters.</p>
  
  <p>It's been some days now that I struggle to return right values but all I get is just "0" values assuming it's a total black or white image!</p>
  
  <p>Here's the code so far: (Updated - Read Update 1)</p>
  
  <pre><code>using (Tiff inputImage = Tiff.Open(fileName, "r"))
          {
              int width = inputImage.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
              int height = inputImage.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
              int bytesPerPixel = 4;
              int count = (int)inputImage.RawTileSize(0); //Has to be: "width * height * bytesPerPixel" ?
              int resolution = (int)Math.Sqrt(count);
              byte[] inputImageData = new byte[count]; //Has to be: byte[] inputImageData = new byte[width * height * bytesPerPixel];
              int offset = 0;
  
              for (int i = 0; i &lt; inputImage.NumberOfTiles(); i++)
              {
                  offset += inputImage.ReadEncodedTile(i, inputImageData, offset, (int)inputImage.RawTileSize(i));
              }
  
              float[,] outputImageData = new float[resolution, resolution]; //Has to be: float[,] outputImageData = new float[width * height];
              int length = inputImageData.Length;
              Buffer.BlockCopy(inputImageData, 0, outputImageData, 0, length);
  
              using (StreamWriter sr = new StreamWriter(fileName.Replace(".tif", ".txt"))) {
                  string row = "";
  
                  for(int i = 0; i &lt; resolution; i++) { //Change "resolution" to "width" in order to have correct array size
                      for(int j = 0; j &lt; resolution; j++) { //Change "resolution" to "height" in order to have correct array size
                          row += outputImageData[i, j] + " ";
                      }
                      sr.Write(row.Remove(row.Length - 1) + Environment.NewLine);
                      row = "";
                  }
              }
          }
  </code></pre>
  
  <p>Sample Files &amp; Results: <a href="http://terraunity.com/SampleElevationTiff_Results.zip" rel="nofollow">http://terraunity.com/SampleElevationTiff_Results.zip</a></p>
  
  <p>Already searched everywhere on internet and couldn't find the solution for this specific issue. So I really appreciate the help which makes it useful for others too.</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>Changed the code based on <a href="https://stackoverflow.com/users/4947650/antti-lepp%C3%A4nen">Antti Leppänen</a>'s answer but got weird results which seems to be a bug or am I missing something? Please see uploaded zip file to see the results with new 32x32 tiff images here:</p>
  
  <p><a href="http://terraunity.com/SampleElevationTiff_Results.zip" rel="nofollow">http://terraunity.com/SampleElevationTiff_Results.zip</a></p>
  
  <p>Results:</p>
  
  <ul>
  <li>LZW Compressed: RawStripSize = ArraySize = 3081 = 55x55 grid</li>
  <li>Unompressed: RawStripSize = ArraySize = 65536 = 256x256 grid</li>
  </ul>
  
  <p>Has to be: RawStripSize = ArraySize = 4096 = 32x32 grid</p>
  
  <p>As you see the results, LibTIFF skips some rows and gives irrelevant orderings and it even gets worse if the image size is not power of 2!</p>