拾取像素值时出现问题 [英] Problem with picking up pixel value
本文介绍了拾取像素值时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
void get_pixels( char path []){
int i,j = 0 ;
int offset;
char 深度;
FILE * fp;
char bmp [ 5 ];
for (i = strlen(path) - 4 ; i< strlen (路径); i ++){
bmp [j] = path [i];
j ++;
}
if (strstr(bmp, .bmp)== NULL){
printf( \ nIndicare un file .bmp来了immagine!\ n);
退出( 1 );
}
fp = fopen(路径, rb );
fseek(fp, 28 ,SEEK_SET);
fscanf(fp, %c,& depth);
if (深度!= 8 ){
printf( \ nSelezionare una immagine a 8 bit!\ n);
退出( 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,像素[ 0 跨度>]);
fclose(fp);
}
对不起我的英文,我是意大利人......:D
解决方案
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
.
Try
unsigned char * pixels; //... allocate 'pixels' buffer //... fseek(fp, offset, SEEK_SET); fread(&pixels[0], sizeof(pixels[0]), 1, fp); printf("\n%d", pixels[0]);
这篇关于拾取像素值时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文