In C Create a simple set of functions for storing video A li
In C
Create a simple set of functions for storing video
A linked list of videos where each video object contains an image object
Each image is contained in a structure, which contains a 2D array
Implement the following header file function declarations in a code file (.c)
Header File:
struct image is the struct declaration, and createWhiteImg creates an image object, and returns a pointer to it
Example using API:
Solution
Given below are all the files . Output of main.cpp is shown at end. Please don\'t forget to rate the answer if it helped. Thank you.
submission.h
 #ifndef submission_h
 #define submission_h
 struct image //define the image struct
 {
 unsigned char **pixels; //2D array
 unsigned int width;
 unsigned int height;
 };
struct video
 {
 struct image *frameImg;
 struct video *next; //pointer to next frame
 }; //define the video struct
//stores image (a video frame) as a 2D array within a structure \"image\"
 struct image* createWhiteImg(unsigned int width, unsigned int height);
 void setPixel(struct image *img, unsigned int x, unsigned int y, unsigned char grayscale); //x and y are width and height
 unsigned char getPixel(struct image *img, unsigned int x, unsigned int y); //x and y are width and height
 //stores video (a sequence of frames) as a double-linked list of \"video\"
 struct video *createEmptyFrameList();
 struct video *insertFrameUpFront(struct video *video, struct image *img);
 //counting from 0
 struct image *getNthFrame(struct video *video, int frameNo);
 struct video *deleteNthFrame(struct video *video, int frameNo);
 
 #endif /* submission_h */
submission.c
#include \"submission.h\"
 #include <stdlib.h>
 struct image* createWhiteImg(unsigned int width, unsigned int height)
 {
 int i, j;
 //allocate memory for the structure
 struct image *img = (struct image*) malloc(sizeof(struct image));
   
 //allocate memory for the pixels i.e pointer to char pointer i.e 2D array
 //allocate meory for rows for pointers and then allocate each row
 img->pixels = (unsigned char **) malloc(height * sizeof(unsigned char *)); //for all rows
 for (i = 0; i < height; i++)
 {
 img->pixels[i] = (unsigned char*) malloc(width * sizeof(char)); //allocate current row
 for(j = 0; j < width; j++) //initial all pixels in current row
 img->pixels[i][j] = 255; //255 representing a white pixel
 }
   
 img->width = width;
 img->height = height;
 return img;
 }
void setPixel(struct image *img, unsigned int x, unsigned int y, unsigned char grayscale) //x and y are width and height
 {
 if(x < img->width && y < img->height)
 {
 img->pixels[y][x] = grayscale;
 }
 }
 unsigned char getPixel(struct image *img, unsigned int x, unsigned int y) //x and y are width and height
 {
 return img->pixels[y][x];
 }
 //stores video (a sequence of frames) as a double-linked list of \"video\"
 struct video *createEmptyFrameList()
 {
 struct video *vid = (struct video *) malloc(sizeof(struct video));
 vid->frameImg = NULL;
 vid->next = NULL;
 return vid;
 }
 struct video *insertFrameUpFront(struct video *video, struct image *img)
 {
 struct video *vid = (struct video *) malloc(sizeof(struct video));
 vid->frameImg = img;
 vid->next = video;
 return vid;
 }
 //counting from 0
 struct image *getNthFrame(struct video *video, int frameNo)
 {
 int i;
 struct video * curr = video;
 for(i = 0; curr != NULL && i < frameNo; i++)
 curr = curr->next;
 if(curr != NULL)
 return curr->frameImg;
 else
 return NULL;
 }
//helper function to free the memory allocated to the image.
 void freeFrame(struct image *img)
 {
 int i ;
 if(img != NULL)
 {
 for(i = 0; i < img->height; i++)
 free(img->pixels[i]);
 free(img->pixels);
 }
 }
struct video *deleteNthFrame(struct video *video, int frameNo)
 {
 int i;
 struct video *curr = video, *prev = NULL;
 for(i = 0; curr != NULL && i < frameNo; i++)
 {
 prev = curr;
 curr = curr->next;
 }
 if(curr == NULL)
 return video;
 else
 {
 if(prev == NULL)
 video = curr->next;
 else
 prev->next = curr->next;
   
 freeFrame(curr->frameImg);
 free(curr);
 return video;
 }
 }
main.c
#include <stdio.h>
 #include \"submission.h\"
int main()
 {
 struct image *a=createWhiteImg(16,9);
 struct image *b=createWhiteImg(16,9);
 struct image *c=createWhiteImg(16,9);
 setPixel(a,0,0,14);
 setPixel(b,15,8,28);
 setPixel(c,10,4,54);
 struct image *d=NULL;
 struct video *v=createEmptyFrameList();
 v=insertFrameUpFront(v,c);
 v=insertFrameUpFront(v,b);
 v=insertFrameUpFront(v,a);
 v=deleteNthFrame(v,1);
 d=getNthFrame(v,1);
 unsigned char px=getPixel(d,10,4);
 if (px==54)
 printf(\"WORKS\ \");
 else
 printf(\"BUMMER\ \");
 return 0;
 }
output
WORKS



