Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Test the code before submitting answer. Create a simple set of functions for sto

ID: 3795178 • Letter: T

Question

Test the code before submitting answer.

Create a simple set of functions for storing video ·A video is a sequence of images (frames) . Each image is contained in a structure, which contains a 2D array ·A video is a linked list of images 1 2 API Listing 1: Video API struct image* createwhiteImg (unsigned int width, unsigned int height) 2 void setPixel (struct image *img, unsigned int x, unsigned int y, unsigned char grayscale) 4 unsigned char getPixel (struct image img, unsigned int x, unsigned int y)i s struct video* createEmptyFrameList 6 struct video* insertFrameUpFront (struct video video, struct image img) 7 struct image* getNthFrame (struct video video, int frameNo) s struct video* deleteNthFrame (struct video *video, int frameNo); Note: 1. Width and height of an image can never be less than l: width21& height 21 2. There will be no testing (set and get) for out of bounds pixels 3. Requesting an out of bounds frame should return NULL 3 Create two files: Header and Code 3.1 Code file The code file contains the implementation of all functions declared in the header file.

Explanation / Answer

Header.h

#include<stdio.h>

struct image
{
int width;
int height;
char **img;
};

struct video
{
int frameNo;
struct image *img;
struct video*next;
};

struct image * createWhiteImg(unsigned int w,unsigned int h);

void setPixel(struct image *im,unsigned int x,unsigned int y, unsigned char c);

unsigned char getPixel(struct image *img,unsigned int x,unsigned int y);

struct video * createEmptyFrameList();

struct video * insertFrameUpFront(struct video * v, struct image *img);

struct image * getNthFrame(struct video *v, int frameNo);

struct video * deleteNthframe(struct video *v,int frameNo);

void print(struct video *);

Header.c:

#include<stdio.h>
#include<stdlib.h>
#include "Header.h"


struct image * createWhiteImg(unsigned int w,unsigned int h)
{
struct image *im=(struct image *)malloc(sizeof(struct image));
if(w<=1 || h<=1)
{
   printf("Height/width is <=1 ");
   return NULL;
}
im->width=w;
im->height=h;
im->img=(char **)malloc(w * sizeof(char*));
int i=0,j=0;


for(i=0;i<w;i++)
im->img[i]=(char*) malloc(h * sizeof(char));

for(i=0;i<w;i++)
for(j=0;j<h;j++)
   im->img[i][j]=0;

return im;

}

void setPixel(struct image *im,unsigned int x,unsigned int y, unsigned char c)
{


im->img[x][y]=c;

}

unsigned char getPixel(struct image *im,unsigned int x,unsigned int y)
{
return im->img[x][y];
}


struct video * createEmptyFrameList(){

struct video *v=(struct video*)malloc(sizeof(struct video));

v->frameNo=0;
v->img=NULL;
v->next=NULL;


return v;

}

void copyImage(struct image *im1, struct image *im2)
{
int i=0,j=0;
im1->img=(char **)malloc(sizeof(char*) * im2->width);

im1->width=im2->width;
im1->height=im2->height;

for(i=0;i<im1->width;i++)
   im1->img[i]=(char *)malloc(sizeof(char) * im1->height);

for(i=0;i<im1->width;i++)
   for(j=0;j<im1->height;j++)
       im1->img[i][j]=im2->img[i][j];

}

struct video * insertFrameUpFront(struct video *vid,struct image *im)
{
struct video *v;
struct video *tmp;

if(vid->frameNo==0)//Inserting the first image, if not added before
{
  
   vid->frameNo=1;
   vid->img=(struct image *)malloc(sizeof(struct image));
   copyImage(vid->img,im);
}
else{

v=(struct video *)malloc(sizeof(struct video));
tmp=vid;

   while(tmp->next!=NULL)
       tmp=tmp->next;

   v->frameNo=tmp->frameNo+1;
   v->img=(struct image*)malloc(sizeof(struct image));
   copyImage(v->img,im);

   tmp->next=v;
   v->next=NULL;
}
  
return vid;
}

struct image *getNthFrame(struct video *v,int frno)
{
struct video *tmp=v;
int flag=0;
int i=0,n=frno;

while(tmp!=NULL)
{
   if(n==0){
       return v->img;
   }
   else if(++i==n)
   {
       return tmp->img;
   }
   tmp=tmp->next;
}

return NULL;
}

struct video *deleteNthframe(struct video *v,int frameNo)
{
struct video *tmp=v;
struct video *tmp2;
int i=0,n=frameNo;


while(i<n)
{
tmp2=tmp;
tmp=tmp->next;
i++;
}

if(n==0){//deleting 0th node
   v=v->next;
   free(tmp);
}
else if(tmp!=NULL)//deleting nth node
{
   tmp2->next=tmp->next;
   free(tmp);
}
return v;
}

void print(struct video *v)
{
struct video *tmp=v;

while(tmp!=NULL){
printf("Video Frame:%d ",tmp->frameNo);
tmp=tmp->next;
}
}

main.c

#include<stdlib.h>
#include<stdio.h>
#include "Header.h"

int main(void){

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);

print(v);
v=deleteNthframe(v,1);
d=getNthFrame(v,1);
printf("After deleting Node 1: ");
print(v);


unsigned char px=getPixel(d,10,4);

if(px==54)
   printf("Works ");
else
   printf("Bummer ");


return 0;
}

Output:

Video Frame:1
Video Frame:2
Video Frame:3
After deleting Node 1:
Video Frame:1
Video Frame:3
Works

Note:

I assumed deleting nth node means like 0 means 1st Node, 1 means 2nd Node etc.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote