Click here to Skip to main content
15,908,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
estoy leyendo una imagen tiff usando TIFFReadEncodedStrip, la lee bien, al momento de mostrarla en una textura los pixeles se muestran desplazados, con cierta inclinacion hacia la derecha, es decir el limite de la imagen aparece en medio de la textura mostrando los pixeles de manera continua, se muestra una imagen con inclinacion


Translation:
I'm reading a tiff image using TIFFReadEncodedStrip, it reads it well, at the time of display in a texture pixels are displaced, with certain inclination to the right, i.e. the limit of the image appears in the middle of the texture showing pixels continuously, shows an image with tilt

I Show the code to test:

C++
#include <stdio.h>
#include <stdlib.h>
#include <tiffio.h>
#include <tiffiop.h>
#include <GL/glut.h>
#include "tr.h"
#include "arraycontainer.h";
#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;

unsigned char* raster;
ushort* raster1;

uint16 width, length, n_length;
// uint32 *buffer;
tsize_t stripSize;
unsigned long imageOffset, result;
int stripMax, stripCount;
char tempbyte;
unsigned char* buffer;
unsigned long bufferSize, count;
GLuint gl_Tex;
uint32 tileWidth, tileLength, tilezise;
char			*lpBits;
unsigned long	rowsperstrip;


//uint16 photo;

int initGL(int *argc, char **argv)
{
	printf("Initializing GLUT...\n");
	glutInit(argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(width, length);
	glutInitWindowPosition(512 - width / 2, 384 - length / 2);
	glutCreateWindow(argv[0]);
	printf("OpenGL window created.\n");

	//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);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, gl_Tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	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();*/

	glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0);
	glTexCoord2f(1.0, 0.0); glVertex2f(0.50, 0.0);
	glTexCoord2f(1.0, 1.0); glVertex2f(0.50, 0.50);
	glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 0.50);
	glEnd();
	glFinish();

	/* Swap the buffers if necessary. */
	glutSwapBuffers();
	glutPostRedisplay();
}


void initOpenGLBuffers()
{
	printf("Creating GL texture...\n");
	//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_NEAREST);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width-1, length-1, 0, GL_RGB, GL_UNSIGNED_BYTE, raster);
	printf("Texture created.\n");
}

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

	// Open the TIFF image
	if ((image = TIFFOpen("C:\\temp\\L04-01Irfan.tif", "r")) == NULL){
		fprintf(stderr, "Could not open incoming image\n");
		exit(42);
	}
	rowsperstrip = (uint32)-1;
	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);
	TIFFGetField(image, TIFFTAG_PHOTOMETRIC, &photo);
	TIFFGetField(image, TIFFTAG_TILEWIDTH, &tileWidth);
	TIFFGetField(image, TIFFTAG_TILELENGTH, &tileLength);
	TIFFGetField(image, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
	imageOffset = 0;

	printf("The width of the image is %d\n", width);
	printf("The length of the image is %d\n", length);
	printf("The rows per strip is %d\n", rps);
	printf("The bits per sample is %d\n", bps);
	printf("The Samples per pixel is %d\n", spp);
	printf("The photometric  is %d\n", photo);
	printf("The tile width per pixel is %d\n", tileWidth);
	printf("The tile height  is %d\n", tileLength);
	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\n", stripMax);
	printf("The strip size is %d\n", stripSize);

	bufferSize = TIFFNumberOfStrips(image) * stripSize;

	printf("The buffer size is %d\n", bufferSize);
		unsigned long	row;
		long			nrow;
		//unsigned long	RowsPerStrip;
		unsigned long	LineSize;
		int				i, l;
		unsigned int	SamplePerPixel;
		UINT32 next_row = 0;
		UINT32 current_row = 0;
		UINT32 img_row = 0;
		UINT32 src_col, src_row, dst_ndx;
		UINT32 row_incr = 0.50;

		//total_width = dib.GetTotalWidth();
		LineSize = TIFFScanlineSize(image); //Number of byte in ine line
		SamplePerPixel = (int)(LineSize / width);
		buffer = (unsigned char*)_TIFFmalloc(TIFFStripSize(image));
		lpBits = (char *)_TIFFmalloc((length*width*sizeof(char)) * 3);
		for (row = 0; row < length; row ++){
			nrow = (row + rowsperstrip > length ? length - row :
				rowsperstrip);

			if (TIFFReadEncodedStrip(image, TIFFComputeStrip(image, row, 0),
				buffer, nrow*LineSize) == -1)
			{
				
			}
			else
			{
				for (l = 0; l < nrow; l++){
					if (SamplePerPixel == 3)
					{
						for (i = 0; i<width; i++){
							src_col =  i + 0.50;
							if (src_col > width) src_col = width;
							src_row = l*LineSize;
							dst_ndx = 3 * (i + (length - img_row - 1) * width);
							lpBits[dst_ndx] = buffer[src_row + src_col*SamplePerPixel + 2];
							lpBits[++dst_ndx] = buffer[src_row + src_col*SamplePerPixel + 1];
							lpBits[++dst_ndx] = buffer[src_row + src_col*SamplePerPixel + 0];
						}

						img_row++;
						if (img_row == length) break;
					}
				}

				if (img_row == length) break;
			}
		}

		result++;
		raster = (unsigned char*)(lpBits);
		//GlobalUnlock(hStrip);
		//GlobalFree(hStrip);
	//if ((buffer = (unsigned char*)malloc(bufferSize)) == NULL){
	//	fprintf(stderr, "Could not allocate enough memory for the uncompressed image\n");
	//	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\n", stripCount);
	//		exit(42);
	//	}

	//	//printf("test and step: %d\n", stripCount);

	//	imageOffset += result;
	//}
	////if (photo != PHOTOMETRIC_MINISWHITE){
	////	// Flip bits
	////	printf("Fixing the photometric interpretation\n");

	////	for (count = 0; count < bufferSize; count++)
	////		buffer[count] = ~buffer[count];
	////}
	//raster = (unsigned char*)(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\n");
	//	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);
}


I´m updating the code, to read a tiff using TIFFReadEncodedStrip, but again my display showing the image whith displacement and inclination using texture
Posted
Updated 3-Feb-16 9:50am
v9
Comments
Garth J Lancaster 26-Jan-16 20:07pm    
muy bueno - Please note the default language here on CP is English, although we have a few people who are multilingual - by using English you expose your question to a wider audience of people who may be able to help you

You dont offer any code, or actually say what you've done to try and solve the issue, and Im not certain that you've describe what the actual issue is or what you need to happen - you may need to edit your question with these points in mind

muchas gracias (and my Spanish is pretty rusty except for 'dos cervesas por favor' and 'donde esta el bagno') so please do use English :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900