image_filtering.c
int read_image(char *img_filename, image *img)
{
	FILE *img_file = fopen(img_filename, "r");
	if (img_file == NULL)
	{
		printf("Cannot open image %s\n", img_filename);
		return ERR_IO;
	}
	char buff_type[3];
	fscanf(img_file, "%s\n%d %d\n%hhd\n", buff_type, &(img->width), &(img->height), &(img->maxval));
	img->type = buff_type[1] - '0';
	int start_image = ftell(img_file);
	fclose(img_file);
	img_file = fopen(img_filename, "rb");
	fseek(img_file, start_image, SEEK_SET);
	if (img->type == 5) // grayscale
	{
		img->rgb_image = NULL;
		img->gray_image = (gray_pixel *)malloc(img->height * img->width * sizeof(gray_pixel));
		if (img->gray_image == NULL)
			return ERR_MEMORY;
		fread((img->gray_image), sizeof(gray_pixel) * img->height * img->width, 1, img_file);
	}
	else // color
	{
		img->gray_image = NULL;
		img->rgb_image = (rgb_pixel *)malloc(img->height * img->width * sizeof(rgb_pixel));
		if (img->rgb_image == NULL)
			return ERR_MEMORY;
		fread((img->rgb_image), sizeof(rgb_pixel) * img->height * img->width, 1, img_file);
	}
	fclose(img_file);
	return STATUS_SUCCES;
}
// Read image from .pnm file
Read image from .pnm file
Read image from . pnm file
int read_image(char *img_filename, image *img)
{
	FILE *img_file = fopen(img_filename, "r");
	if (img_file == NULL)
	{
		printf("Cannot open image %s\n", img_filename);
		return ERR_IO;
	}
	char buff_type[3];
	fscanf(img_file, "%s\n%d %d\n%hhd\n", buff_type, &(img->width), &(img->height), &(img->maxval));
	img->type = buff_type[1] - '0';
	int start_image = ftell(img_file);
	fclose(img_file);
	img_file = fopen(img_filename, "rb");
	fseek(img_file, start_image, SEEK_SET);
	if (img->type == 5) 
	{
		img->rgb_image = NULL;
		img->gray_image = (gray_pixel *)malloc(img->height * img->width * sizeof(gray_pixel));
		if (img->gray_image == NULL)
			return ERR_MEMORY;
		fread((img->gray_image), sizeof(gray_pixel) * img->height * img->width, 1, img_file);
	}
	else 
	{
		img->gray_image = NULL;
		img->rgb_image = (rgb_pixel *)malloc(img->height * img->width * sizeof(rgb_pixel));
		if (img->rgb_image == NULL)
			return ERR_MEMORY;
		fread((img->rgb_image), sizeof(rgb_pixel) * img->height * img->width, 1, img_file);
	}
	fclose(img_file);
	return STATUS_SUCCES;
}
int read_image ( char * img_filename , image * img ) { FILE * img_file = fopen ( img_filename , " " ) ; if ( img_file == NULL ) { printf ( " \n " , img_filename ) ; return ERR_IO ; } char buff_type [ 3 ] ; fscanf ( img_file , " \n \n \n " , buff_type , & ( img -> width ) , & ( img -> height ) , & ( img -> maxval ) ) ; img -> type = buff_type [ 1 ] - ' ' ; int start_image = ftell ( img_file ) ; fclose ( img_file ) ; img_file = fopen ( img_filename , " " ) ; fseek ( img_file , start_image , SEEK_SET ) ; if ( img -> type == 5 ) { img -> rgb_image = NULL ; img -> gray_image = ( gray_pixel * ) malloc ( img -> height * img -> width * sizeof ( gray_pixel ) ) ; if ( img -> gray_image == NULL ) return ERR_MEMORY ; fread ( ( img -> gray_image ) , sizeof ( gray_pixel ) * img -> height * img -> width , 1 , img_file ) ; } else { img -> gray_image = NULL ; img -> rgb_image = ( rgb_pixel * ) malloc ( img -> height * img -> width * sizeof ( rgb_pixel ) ) ; if ( img -> rgb_image == NULL ) return ERR_MEMORY ; fread ( ( img -> rgb_image ) , sizeof ( rgb_pixel ) * img -> height * img -> width , 1 , img_file ) ; } fclose ( img_file ) ; return STATUS_SUCCES ; }
Read image from .pnm file
Read image from . pnm file
// grayscale // color