Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody, I wrote a function, which I already talked of here, that should save in an array each pixel value of a bmp image file, but now I have a new problem. If I give to my function a path for a bmp image I made by myself I get as depth value '0', instead of '8', that is the depth value I saved my image with. But if I try to read this value on an image not made by me, it's alright. Can anybody help me?
This is my code function:
C++
void get_pixels(char path[]){
FILE *fp;
int i, j=0, dim=0, count=0;
int offset;
unsigned char depth=NULL;
char bmp[5];
	
	fp = fopen(path, "rb");
	
	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);
	}
	
	fseek(fp, 28, SEEK_SET);
	fread(&depth, sizeof(depth), 1, fp);
	printf("\n%d", 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);

	dim=(4-(ima_lar % 4))+ima_lar;
	j=0;
	fseek(fp, offset, SEEK_SET);
	for(i=0;i<ima_alt*dim;i++){
		if(count!=ima_lar){
			fread(&pixels[j], sizeof(char), 1, fp);
			printf("\n%d: %d", j+1, pixels[j]);
			j++;
			count++;
		}else{
			fseek(fp, 1, SEEK_CUR);
			count=0;
		}
	}
	fclose(fp);
}

path is passed to the function in a for cicle, through a string array in the form of 'filenames[i]'.
Posted
Comments
Richard MacCutchan 25-May-14 6:53am    
It sounds as if there is something wrong with the file you created rather than with this code.

1 solution

The 'standard' bitmap have 7 different versions (for almost every OS/2 and Windows version), so the DIB section can be different, depending on the application used to create it.
That means that not in all format depth info will be exactly at position 28...
http://en.wikipedia.org/wiki/BMP_file_format[^]
Find you more: http://www.fileformat.info/format/bmp/egff.htm#MICBMP-DMYID.3[^]
 
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