title: How can I simply load a greyscale tiff in libtiff and get an array of pixel intensities?
body: <p>I am trying to understand images some more, and I'm having a great deal of trouble.  From using matlab, I have experience in using imread('test.tif'), and getting a beautiful matrix of rows vs. columns, where you have the intensity of each pixel as an integer.  So, a 720 x 250 image will give a 720 x 250 matrix, where each cell contains the intensity of the pixel, on a scale from 0-255 (depending on the data type).  So, 0 was black, 255 was white.</p>
  
  <p>It was so simple and made so much sense.  Now I am attempting to use libtiff, and I am really struggling.  I want to do the same thing--access those pixels, and I just can't get it.  </p>
  
  <p>I have the following code: </p>
  
  <pre><code>int main(int argc, char *argv[]){
    TIFF* tif = TIFFOpen( argv[1], "r");
      FILE *fp = fopen("test2.txt", "w+");
  
    if (tif) {
        int * buf;
        tstrip_t strip;
        uint32* bc;
        uint32 stripsize;
    TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &amp;bc);
    stripsize = bc[0];
    buf   = _TIFFmalloc(stripsize);
    for(strip = 0; strip &lt; TIFFNumberOfStrips(tif); strip++ ) {
        if( bc[strip] &gt; stripsize) {
            buf = _TIFFrealloc(buf, bc[strip]);
            stripsize = bc[strip];
        }
        TIFFReadRawStrip(tif, strip, buf, bc[strip]);
    }
    int i;
    for (i=0; i&lt;stripsize; i++) {
        if ( i % 960 ==0 )
            fprintf(fp, "\n");
        fprintf(fp,"%d ",  buf[i]);
    }
    _TIFFfree(buf);
    TIFFClose(tif);
    }
    exit(0);
  }
  </code></pre>
  
  <p>But I get completely meaningless results--just completely wacked out numbers.  Nothing like the numbers I see when I load the image in matlab.</p>
  
  <p>How can I simply access the pixel values, and look at them?</p>
  
  <p>Thanks so much.</p>