拾取像素值时出现问题 [英] Problem with picking up pixel value

查看:70
本文介绍了拾取像素值时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们!我是这个论坛的新手。我写了一个函数来解决从.bmp图像文件中获取像素值的问题。当我尝试读取文件的第10个字节(表示图像信息的开头)时,它没问题。但是当我尝试读取第一个像素时,我从fread()函数得到一个-92值。你可以帮我修改我的代码吗?

  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 a unsigned 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆