//Mahzan Musa //051020247 Computer Engineering //This for capture single frame picture type ppm & pgm #include #include #include #include #include #include #include #include #include #include #include #include #include #define CLIP(color) (unsigned char)((color>0xFF)?0xff:((color<0)?0:color)) #define IMAGE_WIDTH 640 #define IMAGE_HEIGHT 480 int cam; int format = VIDEO_PALETTE_YUV420P; static char *videodevice = 0; unsigned char *cap_data; unsigned char *tmp=NULL; unsigned char *buf=NULL; unsigned int cap_fsize=0; unsigned int frame; unsigned int fframe; struct video_capability vcapability; // struct video_channel vchannel; struct video_window vwindow; struct video_picture vpicture; struct video_mbuf vmbuf; struct video_mmap vmmap; static void YUV420toRGB (unsigned char *src, unsigned char *dst, int width,int height,int flipUV, int ColSpace); static void open_device (void); static void image_capture (void); static void save_image (void); static void convimage_RGBtoGRAYSCALE(void); int main() { open_device(); image_capture(); printf("Process end\n"); } static void YUV420toRGB (unsigned char *src, unsigned char *dst, int width,int height,int flipUV,int ColSpace) { unsigned char *Y; unsigned char *V; unsigned char *U; int y1, y2, u, v; int v1, v2, u1, u2; unsigned char *pty1, *pty2; int i, j; unsigned char *RGB1, *RGB2; int r, g, b; //Initialization Y = src; V = Y + width * height; U = Y + width * height + width * height / 4; pty1 = Y; pty2 = pty1 + width; RGB1 = dst; RGB2 = RGB1 + 3 * width; for (j = 0; j < height; j += 2) { //printf ("process line %d\n",j); for (i = 0; i < width; i += 2) { if (flipUV) { u = (*V++) - 128; v = (*U++) - 128; } else { v = (*V++) - 128; u = (*U++) - 128; } switch (ColSpace) { // M$ color space case 0:{ v1 = ((v << 10) + (v << 9) + (v << 6) + (v << 5)) >> 10; // 1.593 u1 = ((u << 8) + (u << 7) + (u << 4)) >> 10; // 0.390 v2 = ((v << 9) + (v << 4)) >> 10; // 0.515 u2 = ((u << 11) + (u << 4)) >> 10; // 2.015 } break; // PAL specific case 1:{ v1 = ((v << 10) + (v << 7) + (v << 4)) >> 10; // 1.1406 u1 = ((u << 8) + (u << 7) + (u << 4) + (u << 3)) >> 10; // 0.3984 v2 = ((v << 9) + (v << 6) + (v << 4) + (v << 1)) >> 10; // 0.5800 u2 = ((u << 11) + (u << 5)) >> 10; // 2.0312 } break; // V4l2 case 2:{ v1 = ((v << 10) + (v << 8) + (v << 7) + (v << 5)) >> 10; // 1.406 u1 = ((u << 8) + (u << 6) + (u << 5)) >> 10; // 0.343 v2 = ((v << 9) + (v << 7) + (v << 6) + (v << 5)) >> 10; // 0.718 u2 = ((u << 10) + (u << 9) + (u << 8) + (u << 4) + (u << 3)) >> 10; // 1.773 } break; case 3:{ v1 = u1 = v2 = u2 = 0; } break; default: break; } //up-left y1 = (*pty1++); if (y1 > 0) { r = y1 + (v1); g = y1 - (u1) - (v2); b = y1 + (u2); r = CLIP (r); g = CLIP (g); b = CLIP (b); } else { r = g = b = 0; } *RGB1++ = r; *RGB1++ = g; *RGB1++ = b; //down-left y2 = (*pty2++); if (y2 > 0) { r = y2 + (v1); g = y2 - (u1) - (v2); b = y2 + (u2); r = CLIP (r); g = CLIP (g); b = CLIP (b); } else { r = b = g = 0; } *RGB2++ = r; *RGB2++ = g; *RGB2++ = b; //up-right y1 = (*pty1++); if (y1 > 0) { r = y1 + (v1); g = y1 - (u1) - (v2); b = y1 + (u2); r = CLIP (r); g = CLIP (g); b = CLIP (b); } else { r = g = b = 0; } *RGB1++ = r; *RGB1++ = g; *RGB1++ = b; //down-right y2 = (*pty2++); if (y2 > 0) { r = y2 + (v1); g = y2 - (u1) - (v2); b = y2 + (u2); r = CLIP (r); g = CLIP (g); b = CLIP (b); } else { r = b = g = 0; } *RGB2++ = r; *RGB2++ = g; *RGB2++ = b; } RGB1 += 3 * width; RGB2 += 3 * width; pty1 += width; pty2 += width; } } static void open_device (void) { //------------------------------------------------------------ // Open Video Device //------------------------------------------------------------ { printf("####################################\n"); printf(" Open Video Device \n"); printf("####################################\n"); printf("\n"); videodevice = "/dev/video"; // (cam = open(videodevice, O_RDWR)) if ((cam = open(videodevice, O_RDWR))==-1) //O_RDWR Open for reading and writing { perror("ERROR: No Camera Device Conneted\n"); exit(-1); } if(cam==1); { printf(" Device is connected \n"); printf("\n"); } } //end line for open video device //------------------------------------------------------------ // Get Video Capabilities //------------------------------------------------------------ /***struct video_capability vcapability***/ { printf("####################################\n"); printf(" Checking Video Capabilities \n"); printf("####################################\n"); printf("\n"); if(ioctl(cam,VIDIOCGCAP,&vcapability)==-1) //ioctl VIDIOCGCAP used to get capabilities of framegrabber { perror("ioctl VIDIOCGCAP"); exit(-1); } printf("************************************\n"); printf("device name ->%s\n", vcapability.name); printf("type -> %d\n", vcapability.type); printf("channels ->%d\n", vcapability.channels); printf("maxwidth ->%d\n", vcapability.maxwidth ); printf("maxheight -> %d\n", vcapability.maxheight); printf("minwidth -> %d\n", vcapability.minwidth ); printf("minheight -> %d\n", vcapability.minheight ); printf("************************************\n"); printf("\n"); }//end line for Get Video Capabilities //------------------------------------------------------------ // Set Video Channel //------------------------------------------------------------ /***struct video_channel vchannel***/ /*{ printf("####################################\n"); printf(" Set Video Channel \n"); printf("####################################\n"); printf("\n"); vchannel.channel=1; vchannel.flags=0; vchannel.type=VIDEO_TYPE_CAMERA; vchannel.norm=VIDEO_MODE_AUTO; if(ioctl(cam,VIDIOCGCHAN,&vchannel)==-1) { perror("VIDIOCGCHAN error"); exit(-1); } if(ioctl(cam,VIDIOCSCHAN,&vchannel)==-1) { perror("VIDOCSCHAN error"); exit(-1); } printf("************************************\n"); printf("channel ->%d\n", vchannel.channel); printf("flag -> %d\n", vchannel.flags); printf("type ->%d\n", vchannel.type); printf("norm ->%d\n", vchannel.norm); printf("************************************\n"); printf("\n"); }*/ //end line for Set Video Channel } static void image_capture (void) { //------------------------------------------------------------ // Capture Image Window // ----------------------------------------------------------- /***struct video_window vwindow***/ { if (ioctl(cam,VIDIOCGWIN,&vwindow)==-1) //ioctl VIDIOCSWIN set overlay windows if it used { perror ("ioctl VIDIOCSWIN"); exit (-1); } }//end line Capture Image Window //------------------------------------------------------------ // Image Properties // ----------------------------------------------------------- /***struct video_picture vpicture***/ { printf("####################################\n"); printf(" Image Properties \n"); printf("####################################\n"); printf("\n"); if (ioctl(cam,VIDIOCGPICT,&vpicture)) { perror("ioctl VIDIOCGPICT"); exit(-1); } //ioctl VIDIOCGPICT used to get the properties of picture printf("************************************\n"); printf("brightness -> %d \n", vpicture.brightness/256 ); printf("hue -> %d\n", vpicture.hue/256); printf("colour -> %d\n", vpicture.colour/256 ); printf("contrast -> %d \n", vpicture.contrast/256 ); printf("whiteness -> %d\n", vpicture.whiteness/256 ); printf("depth -> %d\n", vpicture.depth ); printf("palette -> %d \n", vpicture.palette ); printf("************************************\n"); printf("\n"); }//end line for Image Properties //------------------------------------------------------------ // Mapping Buffer // ----------------------------------------------------------- /***struct video_mbuf vmbuf***/ { printf("####################################\n"); printf(" Mapping Buffer \n"); printf("####################################\n"); printf("\n"); if(ioctl(cam,VIDIOCGMBUF,&vmbuf)==-1) //ioctl VIDIOCGMBUF get infomation of the buffer of the driver { perror("ioctl VIDIOCGMBUF"); exit(-1); } printf("************************************\n"); printf("size -> %d\n",vmbuf.size); //total of frame memory allocated in the driver printf("frames -> %d\n", vmbuf.frames); //number of frames the memory can contain printf("offsets -> %d\n",vmbuf.offsets); //the memory offset to each buffer printf("************************************\n"); printf("\n"); vmmap.frame = 0; vmmap.width = IMAGE_WIDTH; //width & height the dimension of the video frame vmmap.height = IMAGE_HEIGHT; vmmap.format = format; cap_data = (unsigned char *)mmap(0,vmbuf.size, PROT_READ | PROT_WRITE, MAP_SHARED, cam, 0); if(cap_data==(unsigned char *)-1) { perror("mmap"); exit(-1); } cap_fsize =(IMAGE_WIDTH *IMAGE_HEIGHT * 3); tmp = (unsigned char*)malloc (cap_fsize); }//end line for Mapping Buffer //------------------------------------------------------------ // Capture Frame from Camera // ----------------------------------------------------------- /***struct video_mmap vmmap***/ { for (frame = 0; frame < vmbuf.frames; frame++) { vmmap.frame = frame; if(ioctl(cam,VIDIOCMCAPTURE,&vmmap)<0) //Now they can store image { perror("VIDIOCMCAPTURE"); exit(-1); } } }//end line for Capture Frame from Camera //------------------------------------------------------------ // Waiting End Frame // ----------------------------------------------------------- { fframe = vmmap.frame; if(ioctl(cam,VIDIOCSYNC,&fframe)<0) { perror("VIDIOCSYNC"); exit(-1); } memcpy(tmp, cap_data +vmbuf.offsets[vmmap.frame], cap_fsize); buf = cap_data; YUV420toRGB (tmp, buf, IMAGE_WIDTH, IMAGE_HEIGHT, 0, 0); printf("####################################\n"); printf("Saving File and Convert Image File\n"); printf("####################################\n"); printf("\n"); printf("************************************\n"); printf("Start convert YUV420P to RGB\n"); save_image(); convimage_RGBtoGRAYSCALE(); } } static void save_image (void) { //------------------------------------------------------------ // Write Data File PPM // ----------------------------------------------------------- char *fname1 = "pict.ppm"; FILE* fp; printf("\nwrite output file\n"); printf("\n"); //sprintf (fname1, "pict.ppm"); printf ("Writing output file 1 %s\n", fname1); if ((fp = fopen (fname1, "w")) == NULL) { printf ("Could not open file %s for writing.\n", fname1); } fprintf (fp, "P6\n%d %d\n255\n", vmmap.width, vmmap.height); int n = vmmap.width * vmmap.height; int count = 0; while (count < n) { putc (buf[count*3+2], fp); putc (buf[count*3+1], fp); putc (buf[count*3+0], fp); ++ count; } fclose(fp); } static void convimage_RGBtoGRAYSCALE(void) { //------------------------------------------------------------ // Write Data File PGM // ----------------------------------------------------------- // unsigned char *p = NULL; char *fname2="pict.pgm"; FILE *fk; int i, j, k, l; unsigned char inpgrey[IMAGE_HEIGHT][IMAGE_WIDTH]; unsigned char data[IMAGE_HEIGHT][IMAGE_WIDTH][3]; l=0; for(i=0;i