Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#pragma pack(1)
/*------------------------------------------------------------------------/
START Code from Bill Reid
/------------------------------------------------------------------------*/
struct HEADER
{ unsigned short int Type; /* Magic identifier */
unsigned int Size; /* File size in bytes */
unsigned short int Reserved1, Reserved2;
unsigned int Offset; /* Offset to data (in B) */
} Header; /* -- 14 Bytes -- */
struct INFOHEADER
{ unsigned int Size; /* Header size in bytes */
int Width, Height; /* Width / Height of image */
unsigned short int Planes; /* Number of colour planes */
unsigned short int Bits; /* Bits per pixel */
unsigned int Compression; /* Compression type */
unsigned int ImageSize; /* Image size in bytes */
int xResolution, yResolution;/* Pixels per meter */
unsigned int Colors; /* Number of colors */
unsigned int ImportantColors;/* Important colors */
} InfoHeader; /* -- 40 Bytes -- */
struct PIXEL
{ unsigned char Red, Green, Blue;
};
/*------------------------------------------------------------------------/
END Code from Bill Reid
/------------------------------------------------------------------------*/
//prototypes
void edgeDectection(struct PIXEL **pixArray, int height, int width, struct PIXEL **pixArray2);
//int main
int main(int argc, char *argv[]){
//variable declaration
int i, j;
//check to make sure the user inputed the proper number of commands
//on the comman line
if(argc != 3){
printf("invalid amount of command line inputs\n");
exit(1);
}
//the second argument should be the file name
FILE *input = fopen(argv[1],"rb");
//check to see if the file was able to be opened
if(input==0){
printf("could not open the file\n");
fclose(input);
exit(1);
}
//write the output file, which is the thrid argument
FILE *output = fopen(argv[2],"wb");
//check to see if the file was able to be opened
if(output==0){
printf("could not create or open the file to write to\n");
fclose(output);
exit(1);
}
//read the header for the file
fread(&Header, sizeof(Header), 1, input);
//read the info header
fread(&InfoHeader, sizeof(InfoHeader), 1, input);
//write header to new file
fwrite(&Header, sizeof(Header),1, output);
//read in the info header
fwrite(&InfoHeader, sizeof(InfoHeader),1, output);
//create 2D array of structures for every pixel
struct PIXEL **pixArray;
struct PIXEL **pixArray2;
//allocate the memory for the amount of rows
pixArray = (struct PIXEL **)calloc(InfoHeader.Height,sizeof(struct PIXEL*));
//allocate space for the number of columns
for (i=0; i < InfoHeader.Height; i++) {
pixArray[i] = (struct PIXEL *)calloc(InfoHeader.Width,sizeof(struct PIXEL*));
}
//check to make sure memory was allocated
if(pixArray==NULL){
printf("Memory unable to be allocated! Exiting Program Now!\n");
exit(1);
}
//second array that new values are read into
//allocate the memory for the amount of rows
pixArray2 = (struct PIXEL **)calloc(InfoHeader.Height,sizeof(struct PIXEL*));
//allocate space for the number of columns
for (i=0; i < InfoHeader.Height; i++) {
pixArray2[i] = (struct PIXEL *)calloc(InfoHeader.Width,sizeof(struct PIXEL*));
}
//check to make sure memory was allocated
if(pixArray2==NULL){
printf("Memory unable to be allocated! Exiting Program Now!\n");
exit(1);
}
//read the rest of the data into the allocated memory
for (i=0; i < InfoHeader.Height; i++) {
for (j=0; j < InfoHeader.Width; j++) {
fread(&pixArray[i][j], sizeof(struct PIXEL*), 1,input);
printf("[%d][%d][%d]\n",pixArray[i][j].Red, pixArray[i][j].Green,pixArray[i][j].Blue);
}
}
//apply the filter
printf("------------\n");
edgeDectection(pixArray, InfoHeader.Height, InfoHeader.Width, pixArray2);
printf("it made it\n");
for (i=0; i < InfoHeader.Height; i++) {
for (j=0; j < InfoHeader.Width; j++) {
fwrite(&pixArray2[i][j], sizeof(struct PIXEL*), 1,output);
printf("[%d][%d][%d]\n",pixArray2[i][j].Red, pixArray2[i][j].Green,pixArray2[i][j].Blue);
}
}
fclose(input);
fclose(output);
free(pixArray);
free(pixArray2);
return 0;
}
void edgeDectection(struct PIXEL **pixArray, int height, int width, struct PIXEL **pixArray2){
int newR, newG, newB, i, j;
//int matrix[3][3] = {{0,-1,0},{-1,4,-1},{0,-1,0}};
for (i =0; i < InfoHeader.Height; i++) {
for (j =0; j<InfoHeader.Width; j++) {
if((i>0 && j > 0)&&(i<height && j<width)){
newR = (pixArray[i-1][j-1].Red*0)+(pixArray[i-1][j].Red*(-1))+(pixArray[i-1][j+1].Red*0)+(pixArray[i][j-1].Red*(-1))+(pixArray[i][j].Red*4)+(pixArray[i][j+1].Red*(-1))+(pixArray[i+1][j-1].Red*0)+(pixArray[i+1][j].Red*(-1))+(pixArray[i+1][j+1].Red*0);
newG = (pixArray[i-1][j-1].Green*0)+(pixArray[i-1][j].Green*(-1))+(pixArray[i-1][j+1].Green*0)+(pixArray[i][j-1].Green*(-1))+(pixArray[i][j].Green*4)+(pixArray[i][j+1].Green*(-1))+(pixArray[i+1][j-1].Green*0)+(pixArray[i+1][j].Green*(-1))+(pixArray[i+1][j+1].Green*0);
newB = (pixArray[i-1][j-1].Blue*0)+(pixArray[i-1][j].Blue*(-1))+(pixArray[i-1][j+1].Blue*0)+(pixArray[i][j-1].Blue*(-1))+(pixArray[i][j].Blue*4)+(pixArray[i][j+1].Blue*(-1))+(pixArray[i+1][j-1].Blue*0)+(pixArray[i+1][j].Blue*(-1))+(pixArray[i+1][j+1].Blue*0);
}
else if(i==0 && j==0){
newR = (pixArray[i][j].Red*4)+(pixArray[i][j+1].Red*(-1))+(pixArray[i+1][j].Red*(-1))+(pixArray[i+1][j+1].Red*0);
newG = (pixArray[i][j].Green*4)+(pixArray[i][j+1].Green*(-1))+(pixArray[i+1][j].Green*(-1))+(pixArray[i+1][j+1].Green*0);
newB = (pixArray[i][j].Blue*4)+(pixArray[i][j+1].Blue*(-1))+(pixArray[i+1][j].Blue*(-1))+(pixArray[i+1][j+1].Blue*0);
}
else if(i==height && j==width){
newR = (pixArray[i-1][j-1].Red*0)+(pixArray[i-1][j].Red*(-1))+(pixArray[i][j-1].Red*(-1))+(pixArray[i][j].Red*4);
newG = (pixArray[i-1][j-1].Green*0)+(pixArray[i-1][j].Green*(-1))+(pixArray[i][j-1].Green*(-1))+(pixArray[i][j].Green*4);
newB = (pixArray[i-1][j-1].Blue*0)+(pixArray[i-1][j].Blue*(-1))+(pixArray[i][j-1].Blue*(-1))+(pixArray[i][j].Blue*4);
}
else if(i==0 && j==width){
newR = (pixArray[i][j-1].Red*(-1))+(pixArray[i][j].Red*4)+(pixArray[i+1][j-1].Red*0)+(pixArray[i+1][j].Red*(-1));
newG = (pixArray[i][j-1].Green*(-1))+(pixArray[i][j].Green*4)+(pixArray[i+1][j-1].Green*0)+(pixArray[i+1][j].Green*(-1));
newB = (pixArray[i][j-1].Blue*(-1))+(pixArray[i][j].Blue*4)+(pixArray[i+1][j-1].Blue*0)+(pixArray[i+1][j].Blue*(-1));
}
else if(i==height && j==0){
newR = (pixArray[i-1][j].Red*(-1))+(pixArray[i-1][j+1].Red*0)+(pixArray[i][j].Red*4)+(pixArray[i][j+1].Red*(-1));
newG = (pixArray[i-1][j].Green*(-1))+(pixArray[i-1][j+1].Green*0)+(pixArray[i][j].Green*4)+(pixArray[i][j+1].Green*(-1));
newB = (pixArray[i-1][j].Blue*(-1))+(pixArray[i-1][j+1].Blue*0)+(pixArray[i][j].Blue*4)+(pixArray[i][j+1].Blue*(-1));
}
else if((i>0 && i<height) && j==0){
newR = (pixArray[i-1][j].Red*(-1))+(pixArray[i-1][j+1].Red*0)+(pixArray[i][j].Red*4)+(pixArray[i][j+1].Red*(-1))+(pixArray[i+1][j].Red*(-1))+(pixArray[i+1][j+1].Red*0);
newG = +(pixArray[i-1][j].Green*(-1))+(pixArray[i-1][j+1].Green*0)+(pixArray[i][j].Green*4)+(pixArray[i][j+1].Green*(-1))+(pixArray[i+1][j].Green*(-1))+(pixArray[i+1][j+1].Green*0);
newB = (pixArray[i-1][j].Blue*(-1))+(pixArray[i-1][j+1].Blue*0)+(pixArray[i][j].Blue*4)+(pixArray[i][j+1].Blue*(-1))+(pixArray[i+1][j].Blue*(-1))+(pixArray[i+1][j+1].Blue*0);
}
else if((i>0 && i<height) && j==width){
newR = (pixArray[i-1][j-1].Red*0)+(pixArray[i-1][j].Red*(-1))+(pixArray[i][j-1].Red*(-1))+(pixArray[i][j].Red*4+(pixArray[i+1][j].Red*(-1)))+(pixArray[i+1][j+1].Red*0);
newG = (pixArray[i-1][j].Green*(-1))+(pixArray[i-1][j+1].Green*0)+(pixArray[i][j].Green*4)+(pixArray[i][j+1].Green*(-1))+(pixArray[i+1][j].Green*(-1))+(pixArray[i+1][j+1].Green*0);
newB = (pixArray[i-1][j].Blue*(-1))+(pixArray[i-1][j+1].Blue*0)+(pixArray[i][j].Blue*4)+(pixArray[i][j+1].Blue*(-1))+(pixArray[i+1][j].Blue*(-1))+(pixArray[i+1][j+1].Blue*0);
}
else if(i==0 && (j>0 && j<width)){
newR = (pixArray[i][j-1].Red*(-1))+(pixArray[i][j].Red*4)+(pixArray[i][j+1].Red*(-1))+(pixArray[i+1][j-1].Red*0)+(pixArray[i+1][j].Red*(-1))+(pixArray[i+1][j+1].Red*0);
newG = (pixArray[i][j-1].Green*(-1))+(pixArray[i][j].Green*4)+(pixArray[i][j+1].Green*(-1))+(pixArray[i+1][j-1].Green*0)+(pixArray[i+1][j].Green*(-1))+(pixArray[i+1][j+1].Green*0);
newB = (pixArray[i][j-1].Blue*(-1))+(pixArray[i][j].Blue*4)+(pixArray[i][j+1].Blue*(-1))+(pixArray[i+1][j-1].Blue*0)+(pixArray[i+1][j].Blue*(-1))+(pixArray[i+1][j+1].Blue*0);
}
elsebac{
newR = (pixArray[i-1][j-1].Red*0)+(pixArray[i-1][j].Red*(-1))+(pixArray[i-1][j+1].Red*0)+(pixArray[i][j-1].Red*(-1))+(pixArray[i][j].Red*4)+(pixArray[i][j+1].Red*(-1));
newG = (pixArray[i-1][j-1].Green*0)+(pixArray[i-1][j].Green*(-1))+(pixArray[i-1][j+1].Green*0)+(pixArray[i][j-1].Green*(-1))+(pixArray[i][j].Green*4)+(pixArray[i][j+1].Green*(-1));
newB = (pixArray[i-1][j-1].Blue*0)+(pixArray[i-1][j].Blue*(-1))+(pixArray[i-1][j+1].Blue*0)+(pixArray[i][j-1].Blue*(-1))+(pixArray[i][j].Blue*4)+(pixArray[i][j+1].Blue*(-1));
}
pixArray2[i][j].Red = newR;
pixArray2[i][j].Green = newG;
pixArray2[i][j].Blue = newB;
printf("[%d][%d][%d]\n",pixArray2[i][j].Red, pixArray2[i][j].Green,pixArray2[i][j].Blue);
}
}
}