Reputation: 3396
I'm currently making a program for my school project. It's supposed to read a ppm image and return a gradient as an output. But I'm having a problem with large image such as 420 x 360. It works fine with image below 300 pixels on both side. Could anyone tell me what's wrong with my code? Thx. Oh and it's in C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct imageHeader{
char code[3];
char startComment;
char comment[1000];
int width;
int height;
int offset;
} header;
struct imagePixel{
int red;
int green;
int blue;
int total;
};
int main(){
FILE *image;
FILE *imageOut;
image = fopen("sunset.ppm", "rb");
imageOut = fopen("output.ppm", "wb");
fseek(image, 0, SEEK_SET);
fseek(imageOut, 0, SEEK_SET);
// ********* Get the right Code *************
fgets(header.code, sizeof(header.code), image);
if(strcmp(header.code, "P3")){
puts("Not suitable file format!");
}
fputs(header.code, imageOut);
fputs("\n", imageOut);
//*********** SKIP THE COMMENT IF EXIST *****************
header.startComment = fgetc(image);
if(header.startComment = '#'){
fgets(header.comment, sizeof(header.comment), image);
}
fputs(header.comment, imageOut);
//**************** Get the Width of the picture ***************
fscanf(image, "%d", &header.width);
fprintf(imageOut, "%d ", header.width);
//**************** Get the Height of the picture **************
fscanf(image, "%d", &header.height);
fprintf(imageOut, "%d\n", header.height);
//***************** Get the offset color of the picture ************
fscanf(image, "%d", &header.offset);
fprintf(imageOut, "%d\n", header.offset);
struct imagePixel *ptrOne;
ptrOne = (struct imagePixel*) malloc(sizeof(ptrOne) * header.height *header.width);
if (ptrOne == NULL){
printf("Error! Run out of memory!");
return 1;
}
struct imagePixel number[header.height][header.width];
//**************read the image*****************
for(int i = 0; i <= header.height - 1; i++){
for(int j = 0; j <= header.width - 1; j++){
fscanf(image, "%d", &number[i][j].red);
fscanf(image, "%d", &number[i][j].green);
fscanf(image, "%d", &number[i][j].blue);
number[i][j].total = (0.2126 * number[i][j].red) + (0.7152 * number[i][j].green) + (0.0722 * number[i][j].blue);
//number[i][j].total = (number[i][j].red) + (number[i][j].green) + (number[i][j].blue);
}
}
for(int i = 0; i <= header.height - 1; i++){
for(int j = 0; j <= header.width - 1; j++){
int axisXswap = i;
int axisYswap = j;
for(int x = i; x <= header.height - 1; x++){
for(int y = j; y <= header.width - 1; y++){
if(number[x][y].total > number[axisXswap][axisYswap].total){
axisXswap = x;
axisYswap = y;
}
}
}
struct imagePixel temp;
temp.red = number[i][j].red;
temp.green = number[i][j].green;
temp.blue = number[i][j].blue;
temp.total = number[i][j].total;
number[i][j].red = number[axisXswap][axisYswap].red;
number[i][j].green = number[axisXswap][axisYswap].green;
number[i][j].blue = number[axisXswap][axisYswap].blue;
number[i][j].total = number[axisXswap][axisYswap].total;
number[axisXswap][axisYswap].red = temp.red;
number[axisXswap][axisYswap].green = temp.green;
number[axisXswap][axisYswap].blue = temp.blue;
number[axisXswap][axisYswap].total = temp.total;
}
}
for(int i = 0; i <= header.height-1; i++){
for(int j = 0; j <= header.width-1; j++){
fprintf(imageOut, "%d %d %d\n", number[i][j].red, number[i][j].green, number[i][j].blue);
}
}
fclose(image);
fclose(imageOut);
}
Upvotes: 0
Views: 120
Reputation: 173552
Instead of allocating on the stack you should use the heap:
struct imagePixel **number; // two dimensional array of pixels
// allocate rows
number = malloc(sizeof(struct imagePixel *) * header.height);
for (i = 0; i < header.height; ++i) {
// allocate columns for each row
number[0] = malloc(sizeof(struct imagePixel) * header.width);
}
// do free() in reverse as above
Upvotes: 1