Hi,
I am facing a weird problem. Hope someone can figure out the problem for me. I am working on displaying 16-bit grayscale image using OpenGL textures. It works for one of the data perfectly, but when I tried it on another data, it just displays white screen. I checked all possibilities that might be causing the error. I am pasting the entire code for your reference.
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <windows.h>
#include <tiffio.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <glext.h>
typedef unsigned short ushort;
TIFF *image, *out_image;
uint16 photo, bps, spp, fillorder, rps;
uint16 bpp, res_unit;
// uint16* raster;
ushort* raster;
ushort* raster1;
uint16 width, length, n_length;
// uint32 *buffer;
tsize_t stripSize;
unsigned long imageOffset, result;
int stripMax, stripCount;
char tempbyte;
char *buffer;
unsigned long bufferSize, count;
GLuint gl_Tex;
int initGL( int *argc, char **argv )
{
printf("Initializing GLUT...
");
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(width, length);
glutInitWindowPosition(512 - width / 2, 384 - length / 2);
glutCreateWindow(argv[0]);
printf("OpenGL window created.
");
glewInit();
return 0;
}
void display(void)
{
/* Clear the color buffer. */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Re-blit the image. */
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, length, GL_LUMINANCE, GL_UNSIGNED_SHORT, raster);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1, +1);
glTexCoord2f(1.0f, 0.0f); glVertex2f(+1, +1);
glTexCoord2f(1.0f, 1.0f); glVertex2f(+1, -1);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1, -1);
glEnd();
glFinish();
/* Swap the buffers if necessary. */
glutSwapBuffers();
glutPostRedisplay();
}
void initOpenGLBuffers()
{
printf("Creating GL texture...
");
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &gl_Tex);
glBindTexture(GL_TEXTURE_2D, gl_Tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, length, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, raster);
printf("Texture created.
");
}
int main(int argc, char *argv[]){
// Open the TIFF image
if((image = TIFFOpen(argv[1], "r")) == NULL){
fprintf(stderr, "Could not open incoming image
");
exit(42);
}
TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(image, TIFFTAG_IMAGELENGTH, &length);
TIFFGetField(image, TIFFTAG_ROWSPERSTRIP, &rps);
TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bps);
TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);
imageOffset = 0;
printf("The width of the image is %d
", width);
printf("The length of the image is %d
", length);
printf("The rows per strip is %d
", rps);
printf("The bits per sample is %d
", bps);
printf("The Samples per pixel is %d
", spp);
if(length%rps != 0)
n_length = length + (rps - (length%rps));
// Read in the possibly multiple strips
stripSize = TIFFStripSize (image);
stripMax = TIFFNumberOfStrips (image);
printf("The number of strips is %d
", stripMax);
printf("The strip size is %d
", stripSize);
bufferSize = TIFFNumberOfStrips(image) * stripSize;
printf("The buffer size is %d
", bufferSize);
if((buffer = (char*) malloc(bufferSize)) == NULL){
fprintf(stderr, "Could not allocate enough memory for the uncompressed image
");
exit(42);
}
for (stripCount = 0; stripCount < stripMax; stripCount++){
if((result = TIFFReadEncodedStrip (image, stripCount,
buffer + imageOffset,
stripSize)) == -1){
fprintf(stderr, "Read error on input strip number %d
", stripCount);
exit(42);
}
//printf("test and step: %d
", stripCount);
imageOffset += result;
}
raster = (ushort*)(buffer);
initGL(&argc, argv);
initOpenGLBuffers();
glutDisplayFunc(display);
glClearColor(0.2, 0.2, 0.2, 1.0);
glutMainLoop();
out_image = TIFFOpen(argv[2], "w");
if (!out_image)
{
fprintf (stderr, "Can't open for writing
");
return 1;
}
// spp = 1; /* Samples per pixel */
bpp = bps; /* Bits per sample */
photo = PHOTOMETRIC_MINISBLACK;
TIFFSetField(out_image, TIFFTAG_IMAGEWIDTH, width / spp);
TIFFSetField(out_image, TIFFTAG_BITSPERSAMPLE, bpp);
TIFFSetField(out_image, TIFFTAG_SAMPLESPERPIXEL, spp);
TIFFSetField(out_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(out_image, TIFFTAG_PHOTOMETRIC, photo);
TIFFSetField(out_image, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
for (int j = 0; j < n_length; j++)
TIFFWriteScanline(out_image, &raster[j * width], j, 0);
TIFFClose(out_image);
TIFFClose(image);
}