Okay y'all were completely correct (I think). Here is my new code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <tiffio.h>
int main()
{
FILE *fp;
fp = fopen("c:\\output.txt", "w");
TIFF *tif;
uint32 i;
uint32 j;
uint32 w, h, z, npixels;
uint32 *raster;
tif = TIFFOpen("c:\\image1.tif", "r");
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
npixels = w * h;
uint32 **Red;
Red = (uint32**) malloc(h*sizeof(uint32*));
if (Red == NULL) printf("no memory");
for (i=0;i<h;i++)
{
Red[i] = (uint32*) malloc(w*sizeof(uint32));
if (Red[i] == NULL) printf("no memory");
}
raster = (uint32*) _TIFFmalloc(npixels * sizeof(uint32));
if (raster == NULL) printf("no memory");
TIFFReadRGBAImage(tif, w, h, raster, 0);
for (i = 0; i<h; i++)
{
for (j = 0; j<w; j++)
{
z = raster[i*w + h];
Red[i][j] = (z & 0xff);
}
}
for (i = 0; i<h; i++)
{
fprintf(fp, "\n");
for (j = 0; j<w; j++)
{
fprintf(fp, "%u \n", Red[i][j]);
}
}
_TIFFfree(raster);
TIFFClose(tif);
free(Red);
fclose(fp);
return 0;
}
I'm just doing things with Red to simplify matters.
Now, when I open c:\output.txt, it should be 1200 lines (I think), but instead it is 4764, but I'm pretty sure this just refelcts a limitation of Notepad's maximum width. But there are little things that look like small boxes, or perhaps small 0's. I think this just is an artifact of printing in "%u" mode.
So I think I'm good for now. But what I want to do is encapsulate the malloc() part in a function. I tried writing one that took a pointer as an argument and allocated memory for it, but that segfaulted. But I would prefer to write one that mimicked malloc, so I could call it by
Code:
Red = create(w*h*sizeof(uint32));
or something like that. Does anyone have any tips?