2004.12.07 12:25 "[Tiff] Fetching pixel data from 4bit tiff", by Dan Field

2004.12.07 12:57 "Re: [Tiff] Fetching pixel data from 4bit tiff", by Dan Field

Ah, excelent. I was starting to doubt my data. So if it converts from greyscale to RGBA, surely the R, G, B values would all be the same? eg

R=45
G=45
B=45

The data I'm pulling out doesn't fit this pattern at all:

eg:

R       G        B

71      202     94
14      209     94
213     215     94
156     222     94
99      229     94

I'm not using the stuff myself, but it should indeed return R=G=B if the source is indeed grayscale. Perhaps we can see what's going on if you post a tagdump of the source tiff, and a code snippet. (A tagdump can be made with a tool included in LibTiff, tiffinfo, or by the tag viewer pointed to in my signature if you're a windows user.)

dof@nasty cropwill $ tiffinfo images/E0380040.tif
TIFF Directory at offset 0x8
  Subfile Type: (0 = 0x0)
  Image Width: 1735 Image Length: 3871
  Resolution: 200, 200 pixels/inch
  Bits/Sample: 4
  Compression Scheme: None
  Photometric Interpretation: min-is-white
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 1
  Rows/Strip: 3871
  Planar Configuration: single image plane
  Software: Pixel Translations Inc., PIXTIFF Version 54.2.210

Here's the code I've been writing.

Basically I'm trying to take a sample of pixels across the middle of the image, both horizontal and vertical (yes I know the sample algo is wrong for the horizontal.. I've yet to get to that :) )

// gnuplot settings

// set autoscale
// unset label
// set xtic auto
// set ytic auto
// set title "RGB Image Data for TIFF"
// set xlabel "Sampleline Pixels"
// set ylabel "Byte Value"
// plot    "input.dat" using 0:1 title 'Red' smooth csplines , \
//         "" using 0:2 title 'Green' smooth csplines , \
//         "" using 0:3 title 'Blue' smooth csplines

// just set DEBUG = 1 and ./cropwill > ./test.dat to gain gnuplot input data


#define DEBUG
#define SUCCESS 1

#define SAMPLESIZE 100

#include <tiffio.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct pixel {
        int red;
        int green;
        int blue;
} Pixel;

typedef struct image {
        char*   name;
        int     width;
        int     height;
        int     size;
        uint32  raster;
        Pixel   verticalSample[SAMPLESIZE];
        Pixel   horizontalSample[SAMPLESIZE];
} Image;

struct image TheImage;

void printImageInfo() {
        printf("\n#########################################");
        printf("\n# Filename: %s",TheImage.name);
        printf("\n# Width: %i",TheImage.width);
        printf("\n# Height: %i",TheImage.height);
        printf("\n# Size: %i",TheImage.size);
        printf("\n# Pixels: %i",TheImage.width * TheImage.height);
        printf("\n#########################################\n");
}
TIFF* fetchImage(char* filename) {
        TIFF* tif;
        uint16 bps, spp;
        if ((tif = TIFFOpen(filename, "r")) == NULL) {
                printf("\nERROR: Could not open tif: %s.\n",filename);
                exit (1);
        }
        if((TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bps) == 0) || (bps != 1)){
                // fprintf(stderr, "Either undefined or unsupported number of bits per sample\n");
                TIFFSetField( tif, TIFFTAG_BITSPERSAMPLE, 4 );
                /* exit(42); */
        }
        if((TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &spp) == 0) || (spp != 1)){
                fprintf(stderr, "Either undefined or unsupported number of samples per pixel\n");
                exit(42);
        }

        return tif;
}

unsigned long fetchRaster(TIFF* tif) {
        uint32 w, h;
        size_t npixels;
        uint32* raster;
        TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
        TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
        npixels = w * h;
        TheImage.width = w;
        TheImage.height = h;
        raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
        TheImage.size = npixels * sizeof (uint32);
        TIFFReadRGBAImage(tif, w, h, raster, 0);
        return *raster;
}

int main(int argc, char *argv[]) {

        char *filename;
        TIFF* tif;
        uint32 raster, row;
        tdata_t buf;
        /* uint32 row; */
        tsample_t s;
        unsigned int i, vert_size, horiz_size, index;
        unsigned int *vert, *horiz;
        unsigned char R,G,B,A;
        char already_allocated_string[255];


        if (argc == 2) {
                filename = argv[1];
                TheImage.name = filename;
        } else {
                printf("\nERROR: Incorrect number of arguments to cropwill.\n");
                exit(1);
        }

        tif = fetchImage(filename);
        if (tif == NULL) {
                printf("\nERROR: NULL tiff returned\n");
                exit(1);
        }

        raster = fetchRaster(tif);
        TheImage.raster = raster;

        #ifdef DEBUG
        printImageInfo();
        #endif

        vert_size  = (int)((float)TheImage.height/2.0);
        horiz_size = (int)((float)TheImage.width/2.0);

        vert  = malloc(vert_size * sizeof(unsigned int));
        horiz = malloc(horiz_size * sizeof(unsigned int));
        index = 0;
        for (i =TheImage.width/2; i < (TheImage.width*TheImage.height); i += TheImage.width) {
                R=(unsigned char)TIFFGetR(raster+i);
                G=(unsigned char)TIFFGetG(raster+i);
                B=(unsigned char)TIFFGetB(raster+i);
                A=(unsigned char)TIFFGetA(raster+i);
                vert[index] = (unsigned int)(R+G+B);
                #ifdef DEBUG
                /* print out gnuplot data */
                printf("%i\t%i\t%i\t%i\n", R, G, B, (R+G+B));
                #endif
                index++;
        }
        free(vert);

        index = 0;
        for (i = (((TheImage.width*TheImage.height)/2)-(TheImage.width/2)); i < (((TheImage.width*TheImage.height)/2)); i ++) {
                R=(unsigned char)TIFFGetR(raster+i);
                G=(unsigned char)TIFFGetG(raster+i);
                B=(unsigned char)TIFFGetB(raster+i);
                A=(unsigned char)TIFFGetA(raster+i);
                horiz[index] = (unsigned int)(R+G+B);
                //printf("h: %i\n",horiz[index]);
                index++;
        }
        // free(horiz);

        TIFFClose(tif);
        return SUCCESS;
}

Dan Field <dof@llgc.org.uk>            http://www.cymruarywe.org/
                                       http://www.walesontheweb.org/
     Developer / Sysadmin
     Llyfyrgell Genedlaethol Cymru