Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hey guys! I'm new in this forum. I have a problem with a function I wrote to pick up pixel values from a .bmp image file. When I try to read the 10th byte of the file(which indicates the beginning of the image's information), it's allright. But when I try to read the first pixel I get a -92 as value from the fread() function. Can you please help me to fix my code?
C++
void get_pixels(char path[]){
int i, j=0;
int offset;
char depth;
FILE *fp;
char bmp[5];
	
	for(i=strlen(path)-4;i<strlen(path);i++){
		bmp[j]=path[i];
		j++;
	}
	if(strstr(bmp, ".bmp")==NULL){
		printf("\nIndicare un file .bmp come immagine!\n");
		exit(1);
	}
	
	fp = fopen(path, "rb");
	
	fseek(fp, 28, SEEK_SET);
	fscanf(fp, "%c", &depth);
	
	if(depth!=8){
		printf("\nSelezionare una immagine a 8 bit!\n");
		exit(2);
	}
	
	fseek(fp, 10, SEEK_SET);
	fread(&offset, sizeof(offset), 1, fp);
	printf("\n%d", offset);
	
	fseek(fp, offset, SEEK_SET);
	fread(&pixels[0], sizeof(char), 1, fp);
	printf("\n%d", pixels[0]);
	
	fclose(fp);
}

Sorry for my english, I'm italian... :D
Posted

1 solution

char is a signed type. You should use instead a unsigned char.
Try
C
unsigned char * pixels;
//... allocate 'pixels' buffer
//...
fseek(fp, offset, SEEK_SET);
fread(&pixels[0], sizeof(pixels[0]), 1, fp);
printf("\n%d", pixels[0]);
 
Share this answer
 
v2

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