Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this program, you will work with PPM images. Using the ppm_info.h file below, create 2 files lab.c, ppm_info.c. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Lab.c will contain a main

In this program, you will work with PPM images. Using the ppm_info.h file below, create 2 files lab.c, ppm_info.c.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Lab.c will contain a main method that should require 4 command line arguments at execution: the executable, input file name, output file name, and 2nd output file name. If arguments are not provided, the program should print an error message and return with a status of -1.

Open the input file for reading and the 2 output files for writing. If any of the three fail, print some error message and return -1. Using the functions listed below, read in a ppm image and write it back out as a P6 image and a P3 image. Make sure to free() all allocated memory, close() the open files, and return 0

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

ppm_info.c should implement all the function defined in ppm_info.h which includes:

header_t read_header(FILE* image_file);

File Pointer that points to ppm image file. Function should read the ppm header and store the values in a header_t struct which will be returned. See the struct definition in the header file. This should use a fscanf() function call, and should be completed in 3 lines of code

void write_header(FILE* out_file, header_t header);

Output file pointer that points to an output file and an existing header_t struct. Function will write the header to the file parameter using fprintf(), and should only be 1 line of code! There must be whitespace after the last line of the header in a PPM image!

image_t* read_ppm(FILE* image_file);

File pointer to the input ppm. This function will first call read_header to read the header information. Need to call the function read_pixels to read the pixel data.

image_t* read_pixels(FILE* image_file, header_t header);

Input file pointer and populated header. A header_t already has information about the image being read. Use the header_t to determine how many pixels exist in the image (width * height) Call function allocate_memory passing in the header. Use fscanf() to read the red, green, and blue components of each pixel and populate the pixel array. For this function your format string in fscanf() will look like %c%c%c since P6 is stored as raw bytes.

image_t* allocate_memory(header_t header);

Responsible for allocating the memory for an image_t. Reviewing what data types are in a image_t (a header and a double pointer of type pixel_t) The Parameter for this function is a header_t which will be used to determine how much memory will be allocated. Use malloc() to dynamically allocate memory for an image_t* struct and allocate the memory for a 2D array of type pixel_t. Set the header_t that belongs to the allocated image_t to the header information passed into the function.

void write_p6(FILE* out_file, image_t* image);

Output file to write the image to and the image you are writing. This function should call write_header() first. Then write the pixels to the out_file.

void write_p3(FILE* out_file, image_t* image);

Same as the P6 version, but the headers magicNum should be set to P3 and you should write integers to the file. You should use %ds rather than %c. The %ds should have a space between each of them, as well as after the last %d.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// ppm_info.h //

#ifndef PPM_INFO

#define PPM_INFO

#include

#include

#include

// First meaningful line of the PPM file

typedef struct header {

char magicNum[3];

int width, height, maxVal;

} header_t;

// Represents an RGB pixel with integer values between 0-255

typedef struct pixel {

unsigned int r, g, b;

} pixel_t;

// PPM Image representation

typedef struct image {

header_t header;

pixel_t** pixels;

} image_t;

header_t read_header(FILE* image_file);

void write_header(FILE* out_file, header_t header);

image_t* read_ppm(FILE* image_file);

image_t* read_pixels(FILE* image_file, header_t header);

image_t* allocate_memory(header_t header);

void write_p6(FILE* out_file, image_t* image);

void write_p3(FILE* out_file, image_t* image);

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Next Generation Databases NoSQLand Big Data

Authors: Guy Harrison

1st Edition

1484213300, 978-1484213308

Students also viewed these Databases questions