Jump to content

Featured Replies

Posted

bool LoadPNG(const char* filename, ImageData* structure)
{
char header[8];
int x;
int y;
int width;
int height;
int number_of_passes;
long int count3 = 0;
unsigned char* buffer;
png_byte color_type;
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr;

FILE *fp = fopen(filename, "rb");
if (!fp)
	return false;
fread(header, 1, 8, fp);
if (png_sig_cmp((png_byte*)header, 0, 8))
	return false;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
	return false;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
	return false;
if (setjmp(png_jmpbuf(png_ptr)))
	return false;
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
structure->w = info_ptr->width;
structure->h = info_ptr->height;
width = info_ptr->width;
height = info_ptr->height;
number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);

if (setjmp(png_jmpbuf(png_ptr)))
	return false;

png_bytep* row_pointers = new png_bytep[height];
for (int y=0; y<height; y++)
	row_pointers[y] = new png_byte[info_ptr->rowbytes];
png_read_image(png_ptr, row_pointers);

buffer = new unsigned char[4*width*height];

for (int count = 0; count < height; count++){
	for (int count2 = 0; count2 < width*4; count2++){
		buffer[count3] = row_pointers[count][count2];
		count3++;
	}
               [color="Red"][b]Code Was Added Here[/b][/color]
	delete[] row_pointers[count];
}
delete[] row_pointers;
GenerateTexture(buffer,structure);
delete[] buffer;
   fclose(fp);
   return true;
}

 

There's my LoadPNG function. It just suddenly stopped working on one of the two images I'm trying to load, a 1024x1024 starfield. So I added in the following code into the spot indicated above:

 

if (count > 1022){
	char temp[5];
	char line[60];
	itoa(count3,temp,10);
	strcpy(line,"Pixels Counted: ");
	strcat(line,temp);
	itoa(count,temp,10);
	strcat(line,"\nHeight: ");
	strcat(line,temp);
	MessageBox(NULL,line,APP_NAME,MB_OK);
	}

 

And now it works. Why the hell would it work?

Guest
This topic is now closed to further replies.