Code:
#include <stdio.h>#include <stdlib.h>
#include "iplib2New.c"
# define n 3
#define FILTER_SIZE 3
//Function prototypes
image_ptr read_pnm(char *filename, int *rows, int *cols, int *type);
int getnum(FILE *fp);
void write_pnm(image_ptr ptr, char *filename, int rows, int cols, int type);
int apply_horizontal_filter(unsigned char small_block[FILTER_SIZE][FILTER_SIZE], int sobel_h[FILTER_SIZE][FILTER_SIZE], int sobel_v[FILTER_SIZE][FILTER_SIZE]);
int ROWS, COLS, TYPE;
int main(int argc, char **argv){
int rows, cols, type;
int i=0, j=0, x=0, y=0, value_h = 0; //Loop vars and conditions
int tp; //File type
double ave=0;
char aveStr[10], *aveStrEnd;
unsigned char small_block[n][n]; //sub block for image
image_ptr imagePtr, imagePtr2; //image pointer types
image_ptr imageNegPtr;
//Creating sobel filters
int sobel_h[3][3] = {-1, -2, -1, 0, 0, 0, 1, 2, 1}; //Horizontal
int sobel_v[3][3] = {-1, 0, 1, -2, 0, 2, -1, 0, 1}; //Vertical
/* check inputs */
if (argc != 4){
printf("wrong inputs: use %s infile out1 out2 \n", argv[0]);
return 0;
}
/* first read-in the image */
printf("\nReading input image... \n");
//Pointer to access the whole image
imagePtr = read_pnm(argv[1], &rows, &cols, &type); //Returns ptr from read_pnm
printf("\nYour image was read successfully!\n");
printf("Info about your image: rows = %d, cols = %d, type = %d\n", rows, cols, type);
unsigned char image2[rows][cols]; /* space for output image */
//Going over the whole image
for(i=0; i<rows; i++) //The height of the image
{
for(j=0; j<cols; j++) //The width of the image
{
//Picking up our sub block of the image
for(y=0; y<n; y++) //Width of the sub block - cols
{
for(x=0; x<n; x++) //Height of the sub block - rows
{
small_block[x][y]=imagePtr[(i+x)*cols+(j+y)];
}
}
//Analyze sub block here
//printf("Analyzing a sub block...\n");
value_h = apply_horizontal_filter(small_block, sobel_h, sobel_v);
//Update output image
image2[i][j] = value_h;
}
}
printf("\nPicked up a piece of image.\n");
tp = 5;
imagePtr2 = (image_ptr)image2;
printf("\nNow writing to image file ... \n");
printf("rows=%d, cols=%d, type=%d \n", rows, cols, tp);
write_pnm(imagePtr2, argv[2], rows, cols, tp); //Creating the out1.pgm
/* image negatives */
imageNegPtr=(image_ptr)malloc(rows*cols*(sizeof(unsigned char)) );
printf("\nNow writing negative image file ... \n");
printf("rows=%d, cols=%d, type=%d \n", rows, cols, type);
write_pnm(imageNegPtr, argv[3], rows, cols, type); //Creating the out2.pgm
return 0;
}
int apply_horizontal_filter(unsigned char small_block[n][n], int sobel_h[3][3], int sobel_v[3][3]){
int x = 0;
int y = 0;
int pixel_value_h = 0;
unsigned char filtered_block[3][3];
for(x = 0; x<3; x++){
for(y=0; y<3; y++){
pixel_value_h += sobel_h[x][y]*small_block[x][y];
}
}
return pixel_value_h;
}