Question
Work through the steps below in order. 1 - (2 points) Storing PPM images In steg.c,(attached pic) complete the declaration of struct PPM, which holds
Work through the steps below in order. 1 - (2 points) Storing PPM images In steg.c,(attached pic) complete the declaration of struct PPM, which holds the image read from a PPM file. This will need to include the information from the PPM header (width, height, max), and a pointer to a dynamically-allocated array containing the pixel data. Your program should be able to deal with PPM files with arbitrary numbers of rows and columns.
2 - PPM functions Implement in steg.c the following functions. You may also write additional helper functions or type declarations if you like. The template includes some already, e.g. struct Pixel and readPPM, but you can rename or remove these provided you implement struct PPM, getPPM, showPPM, encode and decode. 2a - (2 points) Reading PPM files. struct PPM *getPPM(FILE *f); To return a new struct PPM, containing the PPM image read from open file f. Use the fscanf function to read numbers from the file. Once you know width and height, construct a dynamic array of the appropriate size. Don't worry about handling comments in the PPM file for now.
2b - (2 points) Writing PPM files. void showPPM(const struct PPM *img); To output the PPM image img to stdout (e.g. by using printf), following the syntax in the PPM specification. You should test this function by using it to write out an image loaded with getPPM. The template code's main function has an extra t t mode for this.
2c - (2 points) Encode data. struct PPM *encode(const char *text, const struct PPM *img); To return a copy of PPM image img with message text hidden in its red pixel values. You will need to make it replace successive random red pixels with the characters from the message. How you select pixels is up to you, but make sure that the pixels are chosen in a consistent order (e.g. left to right, top to bottom) and enough pixels are selected to encode all of the message.
2d - (2 points) Decode data char *decode(const struct PPM *oldimg, const struct PPM *newimg); to return a new string containing the message hidden in the red pixel values of PPM image newimg, by comparing it with the red pixel values of PPM image oldimg. The length of the string that it allocates, and returns will depend on the length of the message. You may impose a limit on the maximum length of a decoded message if this makes the implementation easier.
3 - (6 points) Steganography program Complete the main behaviour of the steg program, which encodes or decodes images based on its command-line arguments as described above.
4 - (2 points) Additional PPM features For an additional 2 points, implement one of the following two features: Make getPPM and showPPMread and write comments, by storing the comments as a linked list of strings in struct PPM. You can assume that any comment lines will always be immediately before width (although the PPM specification says they're allowed to occur anywhere in the file). You will need to rework how you read width if the first character you read is a #, then you have a comment, else it must be a digit and should be counted as part of width.
Make getPPM handle normal PPM files as well as Plain PPM. Normal PPM files start with P6 instead of P3, and have the image data stored directly as bytes (one or two bytes per colour, depending on max) rather than as decimal numbers see the PPM specification for more details. You can read data like this using the fread function.
5 - (2 points) Report In Report.md, you should give a brief description no more than a couple of pages of text of: Your program designs Your choice of data structures and algorithms
#include
/* The RGB values of a pixel. */ struct Pixel { int red; int green; int blue; };
/* An image loaded from a PPM file. */ struct PPM { struct Pixel **pixels; int width; int height; };
/* Reads an image from an open PPM file. * Returns a new struct PPM, or NULL if the image cannot be read. */ struct PPM *getPPM(FILE * f) { char magic[3]; int width, height, maxval; struct Pixel **pixels; struct PPM *img;
/* Read the image metadata */ if (fscanf(f, "%2s %d %d %d", magic, &width, &height, &maxval) != 4) { return NULL; }
/* Check that the image is a P6 PPM */ if (strcmp(magic, "P6") != 0 || maxval != 255) { return NULL; }
/* Allocate memory for the pixel array */ pixels = malloc(sizeof(struct Pixel *) * height); if (pixels == NULL) { return NULL; }
for (int i = 0; i < height; i++) { pixels[i] = malloc(sizeof(struct Pixel) * width); if (pixels[i] == NULL) { /* Free the allocated memory if there is an error */ for (int j = 0; j < i; j++) { free(pixels[j]); } free(pixels); return NULL; } }
/* Read the pixel data */ for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { pixels[i][j].red = fgetc(f); pixels[i][j].green = fgetc(f); pixels[i][j].blue = fgetc(f); } }
/* Create the PPM struct */ img = malloc(sizeof(struct PPM)); if (img == NULL) { /* Free the allocated memory if there is an error */ for (int i = 0; i < height; i++) { free(pixels[i]); } free(pixels); return NULL; }
img->pixels = pixels; img->width = width; img->height = height;
return img; }
/* Write img to stdout in PPM format. */ void showPPM(const struct PPM *img) { printf("P6 %d %d %d ", img->width, img->height, 255); for (int i = 0; i < img->height; i++) { for (int j = 0; j < img->width; j++) { putchar(img->pixels[i][j].red); putchar(img->pixels[i][j].green); putchar(img->pixels[i][j].blue); } } }
/* Opens and reads a PPM file, returning a pointer to a new struct PPM. * On error, prints an error message and returns NULL. */ struct PPM *readPPM(const char *filename) { /* Open the file for reading */ FILE *f = fopen(filename, "rb"); if (f == NULL) { fprintf(stderr, "File %s could not be opened", filename); return NULL; }
/* Read the PPM data from the file */ struct PPM *img = getPPM(f);
/* Close the file and return the image */ fclose(f); return img; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started