namegame
April 11th, 2008, 03:29 AM
I have written a program to convert an image from color to Sepia color tone. However, when I try to run it using
./a.out input.ppm output.ppm , it will not run.
#include <stdio.h>
#include <stdlib.h>
#include "img.h"
int main(int argc, char *argv[]){
int x;
int y;
int width;
int height;
int index;
pixel_t *pixel;
unsigned char *inputImage;
unsigned char *outputImage;
inputImage = (unsigned char *)malloc (sizeof(pixel_t) * width * height);
outputImage = (unsigned char *)malloc (sizeof(pixel_t) * width * height);
FILE *input;
FILE *output;
if (argc < 3){
fprintf(stderr,"Incorrect input.\n");
fprintf(stderr,"Exiting.");
exit(1);
}
input = fopen(argv[1], "r");
output = fopen(argv[2], "w");
if ( input == NULL || output == NULL){
fprintf(stderr,"Error opening file.\n");
fprintf(stderr,"Exiting.");
exit(2);
}
readHeader (&width, &height, input);
pixel = (pixel_t *) malloc(sizeof(pixel_t));
input = malloc (sizeof(pixel_t) * width * height);
output = malloc(sizeof(pixel_t) * width * height);
fread(inputImage, sizeof(pixel_t), width * height, input);
for(y = 0; y < height; y++){
for(x = 0; x < width; x++){
index = sizeof(pixel_t) * y * width + sizeof(pixel_t) * x;
pixel->r = inputImage[index];
pixel->g = inputImage[index + 1];
pixel->b = inputImage[index + 2];
grayPixel(pixel);
sepiaPixel(pixel);
outputImage[index] = pixel->r;
outputImage[index + 1] = pixel->g;
outputImage[index + 2] = pixel->b;
}
}
makeHeader( width, height, input);
fwrite(outputImage, sizeof(pixel_t), width * height, output);
free(inputImage);
free(outputImage);
fclose(input);
fclose(output);
return 0;
}
void grayPixel(pixel_t *pixel){
int sum;
pixel->r = pixel->r * 0.3;
pixel->g = pixel->g * 0.5;
pixel->b = pixel->b * 0.2;
sum = pixel->r + pixel->g + pixel->b;
pixel->r = sum;
pixel->g = sum;
pixel->b = sum;
}
void sepiaPixel(pixel_t *pixel){
if (pixel->r < 60){
pixel->r = pixel->r * 0.9;
pixel->g = pixel->g * 0.9;
pixel->b = pixel->b * 0.9;
}
else if (pixel->r >= 60 && pixel->r < 190){
pixel->b = pixel->b * 0.8;
}
else {
pixel->b = pixel->b * 0.9;
}
}
Any suggestions/ideas will be welcome.
./a.out input.ppm output.ppm , it will not run.
#include <stdio.h>
#include <stdlib.h>
#include "img.h"
int main(int argc, char *argv[]){
int x;
int y;
int width;
int height;
int index;
pixel_t *pixel;
unsigned char *inputImage;
unsigned char *outputImage;
inputImage = (unsigned char *)malloc (sizeof(pixel_t) * width * height);
outputImage = (unsigned char *)malloc (sizeof(pixel_t) * width * height);
FILE *input;
FILE *output;
if (argc < 3){
fprintf(stderr,"Incorrect input.\n");
fprintf(stderr,"Exiting.");
exit(1);
}
input = fopen(argv[1], "r");
output = fopen(argv[2], "w");
if ( input == NULL || output == NULL){
fprintf(stderr,"Error opening file.\n");
fprintf(stderr,"Exiting.");
exit(2);
}
readHeader (&width, &height, input);
pixel = (pixel_t *) malloc(sizeof(pixel_t));
input = malloc (sizeof(pixel_t) * width * height);
output = malloc(sizeof(pixel_t) * width * height);
fread(inputImage, sizeof(pixel_t), width * height, input);
for(y = 0; y < height; y++){
for(x = 0; x < width; x++){
index = sizeof(pixel_t) * y * width + sizeof(pixel_t) * x;
pixel->r = inputImage[index];
pixel->g = inputImage[index + 1];
pixel->b = inputImage[index + 2];
grayPixel(pixel);
sepiaPixel(pixel);
outputImage[index] = pixel->r;
outputImage[index + 1] = pixel->g;
outputImage[index + 2] = pixel->b;
}
}
makeHeader( width, height, input);
fwrite(outputImage, sizeof(pixel_t), width * height, output);
free(inputImage);
free(outputImage);
fclose(input);
fclose(output);
return 0;
}
void grayPixel(pixel_t *pixel){
int sum;
pixel->r = pixel->r * 0.3;
pixel->g = pixel->g * 0.5;
pixel->b = pixel->b * 0.2;
sum = pixel->r + pixel->g + pixel->b;
pixel->r = sum;
pixel->g = sum;
pixel->b = sum;
}
void sepiaPixel(pixel_t *pixel){
if (pixel->r < 60){
pixel->r = pixel->r * 0.9;
pixel->g = pixel->g * 0.9;
pixel->b = pixel->b * 0.9;
}
else if (pixel->r >= 60 && pixel->r < 190){
pixel->b = pixel->b * 0.8;
}
else {
pixel->b = pixel->b * 0.9;
}
}
Any suggestions/ideas will be welcome.