更新时间:2023-01-06 12:17:57
char
是签名类型。您应该使用unsigned char
。
尝试unsigned char *像素;
// ...分配'像素'缓冲区
// ...
fseek(fp,offset,SEEK_SET);
fread(& pixels [ 0 ], sizeof (pixels [ 0 ]), 1 ,fp);
printf( \ n%d,像素[ 0 跨度>]);
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?
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
char
is a signed type. You should use instead aunsigned char
.
Tryunsigned char * pixels; //... allocate 'pixels' buffer //... fseek(fp, offset, SEEK_SET); fread(&pixels[0], sizeof(pixels[0]), 1, fp); printf("\n%d", pixels[0]);