I have gone absolutely mind numb trying merge two PPM images of the same type and size together. My code can read, display and save PPM images but won't enter the for loop which combine two images. Here's the combining function:
struct MFI AddImages (struct MFI AddImgOne, struct MFI AddImgTwo)
{
struct MFI AddImgThree = {'P6', AddImgOne.ImgHeight, AddImgOne.ImgWidth, AddImgOne.MaxInfo, AddImgOne.ImageSize};
int CombineCounter;
if(AddImgOne.ImgWidth != AddImgTwo.ImgWidth || AddImgOne.ImgHeight != AddImgTwo.ImgHeight)
{
printf("They're not the same size you dummy!");
exit(0);
}
AddImgThree.ImageSize = AddImgOne.ImgHeight * AddImgTwo.ImgWidth;
AddImgThree.ImageMemory = malloc(AddImgThree.ImageSize * sizeof(unsigned long long));
if (!AddImgThree.ImageMemory)
{printf("theres no space!\n");
exit(0);}
if (AddImgThree.ImageMemory)
{
for (CombineCounter = 1; CombineCounter < AddImgOne.ImageSize; CombineCounter++)
{
AddImgThree.RGB[CombineCounter].red = AddImgOne.RGB[CombineCounter].red + AddImgTwo.RGB[CombineCounter].red;
AddImgThree.RGB[CombineCounter].blue = AddImgOne.RGB[CombineCounter].blue + AddImgTwo.RGB[CombineCounter].blue;
AddImgThree.RGB[CombineCounter].green = AddImgOne.RGB[CombineCounter].green + AddImgTwo.RGB[CombineCounter].green;
if (AddImgThree.RGB[CombineCounter].red > 255)
{AddImgThree.RGB[CombineCounter].red = 255;}
if (AddImgThree.RGB[CombineCounter].blue > 255)
{AddImgThree.RGB[CombineCounter].blue = 255;}
if (AddImgThree.RGB[CombineCounter].green > 255)
{AddImgThree.RGB[CombineCounter].green = 255;}
}
}
else
printf ("it's not right\n");
free(AddImgThree.ImageMemory);
return AddImgThree;
}
For context heres my main function with the structures too:
#include <stdio.h> /*printf, scanf, seekf*/
#include <string.h> /*strstr, strcmp, strcat*/
#include <stdlib.h> /*malloc free command*/
struct MFI {
char Format[3];
int ImgHeight, ImgWidth, MaxInfo, ImageSize;
int *ImageMemory;
struct RGBPixel *RGB;
};
struct RGBPixel {
int red, green, blue;
};
struct MFI Reading (char filename[128]);
struct MFI AddImages (struct MFI AddImgOne, struct MFI AddImgTwo);
void Display (struct MFI D);
void Save(struct MFI S);
int main()
{
struct MFI ImgOne;
struct MFI ImgTwo;
struct MFI ImgThree;
char Image1[128];
char Image2[128];
printf("please enter the name of image 1:");
scanf(" %s", Image1);
fseek(stdin,0,SEEK_END);
printf("please enter the name of image 2:");
scanf(" %s", Image2);
fseek(stdin,0,SEEK_END);
ImgOne = Reading(Image1);
ImgTwo = Reading(Image2);
Display(ImgOne);
Display(ImgTwo);
ImgThree = AddImages(ImgOne, ImgTwo);
Save(ImgThree);
printf("\nfile is closed\n");
free(ImgOne.ImageMemory);
free(ImgTwo.ImageMemory);
free(ImgThree.ImageMemory);
printf("\nimage freed\n");
return 0;
}
And my reading function:
struct MFI Reading (char filename[128])
{
struct MFI R;
FILE *ImageRead; // file pointer or handle
int p;
if(strstr(filename, ".ppm") == NULL)
{strcat(filename, ".ppm");}
ImageRead = fopen(filename, "rb");
//check if it exists
if (ImageRead == NULL) {
printf("Nope doesn't exist\n");
exit(0);}
//get all the image information
fscanf(ImageRead, "%s %d %d %d", R.Format, &R.ImgWidth, &R.ImgHeight, &R.MaxInfo);
//allocate memory for image//
R.ImageMemory = malloc(R.ImgHeight * R.ImgWidth * sizeof(int*));
if (!R.ImageMemory)
{printf("theres no space!\n");
exit(0);}
//check if its a PPM
if (!strcmp(R.Format, "P3"))
printf("This is a ppm\n");
else
{
if (!strcmp (R.Format, "P2"))
printf("This is a pgm\n");
else
{
if (!strcmp (R.Format, "P5"))
printf("This is a pgm\n");
else
{printf("this isn't a ppm file");}
}
}
p = getc(ImageRead);
while (p == '#')
{
while (getc(ImageRead) != '\n')
{
p = getc(ImageRead);
}
}
ungetc(p, ImageRead);
//check image size
if(R.ImgHeight > 1080 || R.ImgWidth > 1920)
{printf ("This is too big!!");}
R.ImageSize = R.ImgWidth*R.ImgHeight;
while (fgetc(ImageRead) != '\n');
R.RGB = malloc(3*R.ImageSize * sizeof(R.RGB));
if (fread(R.RGB, 3*R.ImgHeight, 4*R.ImgWidth-3, ImageRead) != EOF)
printf("error with loading image");
//reading image width and height
printf("The height of the image is %d and the width is %d\n", R.ImgWidth, R.ImgHeight);
printf("The maximum value of each pixel is %d\n", R.MaxInfo);
return R;
}
And my saving function:
void Save(struct MFI S)
{ FILE *SavePointer;
char InputFileName[128];
printf("\nPlease Enter the Name of the file you want to Store this Array In: \n");
scanf("%s", InputFileName);
SavePointer = fopen(InputFileName, "r");
if(SavePointer != NULL)
{
printf("File exits, try another name");
fclose(SavePointer);
exit(0);
}
else
{
fclose(SavePointer);
printf("File is good to go!");
}
if(strstr(InputFileName, ".ppm") == NULL)
{strcat(InputFileName, ".ppm");}
SavePointer = fopen(InputFileName, "wb");
if (!SavePointer)
{
printf ("Lost your File pointer!");
exit (0);
}
fprintf(SavePointer, "%s\n%d %d\n%d\n", S.Format, S.ImgWidth, S.ImgHeight, S.MaxInfo);
fwrite(S.RGB, 3*S.ImgHeight, 4*S.ImgWidth-3, SavePointer);
fclose(SavePointer);
return;
}
EDIT
I've rewrote the final part of the function so now the program will enter the for loop but will stop when assigning the array element value...
for (CombineCounter = 0; CombineCounter < AddImgOne.ImageSize; CombineCounter++)
{
unsigned int red = AddImgOne.RGB[CombineCounter].red + AddImgTwo.RGB[CombineCounter].red;
if (red > 255)
{red = 255;}
AddImgThree.RGB[CombineCounter].red = red; //exits entire code when here
unsigned int blue = AddImgOne.RGB[CombineCounter].blue + AddImgTwo.RGB[CombineCounter].blue;
if (blue > 255)
{blue = 255;}
AddImgThree.RGB[CombineCounter].blue = blue;
unsigned int green = AddImgOne.RGB[CombineCounter].green + AddImgTwo.RGB[CombineCounter].green;
if (green > 255)
{green = 255;}
AddImgThree.RGB[CombineCounter].green = green;
}