I've been trying for days to figure out why my program is just reprinting the input image. I know my other functions work, but for some reason this one is stumping me I've tried every way I could think of to move pixels around, but absolutely nothing i have tried works.
header:
#include <stdio.h>
#include <stdlib.h>
struct pixel {
char r, g, b;
};
int g_width, g_height;
void parseHeader( FILE *input );
void parseImage( FILE *input, struct pixel *theArray );
void print(struct pixel a[]);
void my_Mirror(struct pixel a[]);
void rotate(struct pixel a[]);
void my_Flip(struct pixel a[]);
main:
#include "transform.h"
int main (int argc, char *argv[]) {
// declarations here
FILE *inFile;
// open input file
inFile = fopen(argv[2],"r");
if (inFile == NULL)
{
fprintf(stderr, "File open error. Exiting program\n");
exit(1);
}
// parseHeader function call here
parseHeader(inFile);
// malloc space for the array (example given in assignment write-up)
struct pixel * image =
(struct pixel *) malloc(sizeof(struct pixel) * g_width * g_height);
// parseImage function call here
parseImage(inFile, image);
// close input file
fclose(inFile);
// manipulate the image according to command-line parameter
// 1: mirror image
// 2: upside down image
// 3: rotate to the right 90 degrees
if (atoi(argv[1]) == 1)
{
my_Mirror(image);
}
if (atoi(argv[1]) == 2)
{
my_Flip(image);
}
if (atoi(argv[1]) ==3)
{
rotate(image);
}
print(image);
return 0;
}
mirror:
void my_Mirror(struct pixel a[])
{
int i,j,limit = 0;
struct pixel temp;
for(j = 0; j < g_height; ++j) //move through vertical pixels
{
for( i = 0; i < (g_width/2); i++)
{
temp = a[(j * g_width) + i];
a[(j * g_width) + i] = a[((j+1)*g_width) - (1 + i)];
a[((j+1)*g_width) - (1 + i)] = temp;
}
}
}
This is my horizontal flip function which works up until the mirror call if that helps:
#include "transform.h"
void my_Flip(struct pixel a[])
{
struct pixel temp;
int i;
int j = 0;
for (i = (g_width * g_height); i >= 0; --i)
{
temp = a[i];
a[i] = a[(i-i)+j]; //swap values of pixels
a[(i-i)+j] = temp;
++j;
if(j == (g_width * g_height)/2)
{
i = 0;
}
}
my_Mirror(a);
}
A pixel is not an intrinsic type, so must be copied via
memcpy
rather than as a simple assignment.The code needs to validate the command line parameter and if the parameter does not contain a valid value then complain before exiting.
The code needs to validate the returned value from the input I/O functions to assure the operation was successful.
So here is a possible solution to your question: