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
Header File:
NOTE:
stores image (a video frame) as a 2D array within a structure \"image\"
stores video (a sequence of frames) as a double-linked list of \"video\"
Example using API:
Solution
submissions.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)
{
struct image *im=(struct image *)malloc(sizeof(struct image));
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;
v->img=NULL;
}
return vid;
}
struct image *getNthFrame(struct video *v,int frno)
{
struct video *tmp=v;
int flag=0;
int i=0,n=frno-1;
printf(\"frno,n:%d, %d\ \", frno,n);
while(tmp!=NULL)
{
if(n==0){
return tmp->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=NULL;
int i=0,n=frameNo-1;
while(tmp!=NULL)
{
if(n==0){
v=v->next;
free(tmp);
break;
}
else if(++i==n)
{
tmp2->next=tmp->next;
free(tmp);
break;
}
tmp2=tmp;
tmp=tmp->next;
}
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 \"submission.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);
print(v);
if(d==NULL)
printf(\"NULL d\ \");
unsigned char px=getPixel(d,10,4);
if(px==54)
printf(\"Works\ \");
else
printf(\"Bummer\ \");
return 0;
}



