/* * writeimage.c -- By Nathan Folkert -- 06/15/10 * * Creates a bmp image from the video device * */ #include #include #include #include #include /* * Structures for the bmp file header */ struct header { unsigned char type[2]; unsigned char size[4]; unsigned char res1[2]; unsigned char res2[2]; unsigned char offset[4]; }; struct information { unsigned char size[4]; unsigned char width[4]; unsigned char height[4]; unsigned char planes[2]; unsigned char bitcount[2]; unsigned char compression[4]; unsigned char sizeimage[4]; unsigned char meter[4]; unsigned char meter2[4]; unsigned char colors[4]; unsigned char important[4]; }; struct header head; struct information info; /* Height and Width of image in pixels */ #define WIDTH 512 #define HEIGHT 480 /* * Helper functions to simplify code */ void initStructs(void); void initBuf(char* buf); void writePicture(char* buf , char* buffer); int fdwrite; int main() { int fd; /* Open the two needed files */ /* Output file */ fdwrite = open ("pic.bmp" , O_RDWR | O_CREAT); if( ! fdwrite ) { printf("Error Opening Output File\n" ); return -1; } /* Video Device File */ fd = open ("/dev/video0" , O_RDWR ); if( ! fd ) { printf("Error opening video0\n" ); return -1; } /* Buffer that is mapped to the device memory */ char* vidbuf; /* Buffer that is written to the output file */ char* buf; /* Allocated output buffer */ buf = (char *) calloc(HEIGHT*WIDTH*3+54 , 1); /* Map vidbuf to the memory on the device */ vidbuf = mmap(NULL , getpagesize()*4, PROT_READ | PROT_WRITE , MAP_SHARED , fd , 0); initStructs(); initBuf(buf); writePicture(buf , vidbuf); /* Cleanup */ free(buf); close( fd ); close( fdwrite ); return 0; } /* * void writePicture(char* buf , char* buffer) * Write the picture out to the file opened by fdwrite */ void writePicture(char* buf , char* vidbuf) { int j = 0; int i = 0; int x = 54; vidbuf[8195] = 0; for( j = 0; j < HEIGHT; j++ ) { /* Increment the HRT_Y_LOW_REG */ vidbuf[8194] = j; for( i =WIDTH-1 ; i >= 0 ; i-- ) { /* Each pixel needs to be written three times */ buf[x++] = vidbuf[i]; buf[x++] = vidbuf[i]; buf[x++] = vidbuf[i]; } /* Increment the HRT_Y_HIGH_REG when needed */ if( j == 255 ) vidbuf[8195] = 1; } write( fdwrite , buf ,HEIGHT*WIDTH*3+54 ); } /* * void initStructs(void) * Initialize the structures for the bmp header */ void initStructs(void) { head.type[0] = 0x42; head.type[1] = 0x4D; head.size[0] = 0x0; head.size[1] = 0x3; head.size[2] = 0xAD; head.size[3] = 0xD4; head.res1[0] = 0x0; head.res1[1] = 0x0; head.res2[0] = 0x0; head.res2[1] = 0x0; head.offset[0] = 0x36; head.offset[1] = 0x0; head.offset[2] = 0x0; head.offset[3] = 0x0; info.size[0] = 0x28; info.size[1] = 0x0; info.size[2] = 0x0; info.size[3] = 0x0; //512 0x200 info.width[0] = 0x0; info.width[1] = 0x02; info.width[2] = 0x0; info.width[3] = 0x0; //480 info.height[0] = 0xE0; info.height[1] = 0x1; info.height[2] = 0x0; info.height[3] = 0x0; info.planes[0] = 0x1; info.planes[1] = 0x0; info.bitcount[0] = 0x18; info.bitcount[1] = 0x0; info.compression[0] = 0x0; info.compression[1] = 0x0; info.compression[2] = 0x0; info.compression[3] = 0x0; info.sizeimage[0] = 0x0; info.sizeimage[1] = 0x0; info.sizeimage[2] = 0x0; info.sizeimage[3] = 0x0; info.meter[0] = 0x20; info.meter[1] = 0x2e; info.meter[2] = 0x0; info.meter[3] = 0x0; info.meter2[0] = 0x20; info.meter2[1] = 0x2e; info.meter2[2] = 0x0; info.meter2[3] = 0x0; info.colors[0] = 0x0; info.colors[1] = 0x0; info.colors[2] = 0x0; info.colors[3] = 0x0; info.important[0] = 0x0; info.important[1] = 0x0; info.important[2] = 0x0; info.important[3] = 0x0; } /* * void initBuf(char *buf) * Initialize the buffer with the bmp header */ void initBuf(char* buf) { buf[0] = head.type[0]; buf[1] = head.type[1]; buf[2] = head.size[0]; buf[3] = head.size[1]; buf[4] = head.size[2]; buf[5] = head.size[3]; buf[6] = head.res1[0]; buf[7] = head.res1[1]; buf[8] = head.res2[0]; buf[9] = head.res2[1]; buf[10] = head.offset[0]; buf[11] = head.offset[1]; buf[12] = head.offset[2]; buf[13] = head.offset[3]; buf[14] = info.size[0]; buf[15] = info.size[1]; buf[16] = info.size[2]; buf[17] = info.size[3]; buf[18] = info.width[0]; buf[19] = info.width[1]; buf[20] = info.width[2]; buf[21] = info.width[3]; buf[22] = info.height[0]; buf[23] = info.height[1]; buf[24] = info.height[2]; buf[25] = info.height[3]; buf[26] = info.planes[0]; buf[27] = info.planes[1]; buf[28] = info.bitcount[0]; buf[29] = info.bitcount[1]; buf[30] = info.compression[0]; buf[31] = info.compression[1]; buf[32] = info.compression[2]; buf[33] = info.compression[3]; buf[34] = info.sizeimage[0]; buf[35] = info.sizeimage[1]; buf[36] = info.sizeimage[2]; buf[37] = info.sizeimage[3]; buf[38] = info.meter[0]; buf[39] = info.meter[1]; buf[40] = info.meter[2]; buf[41] = info.meter[3]; buf[42] = info.meter2[0]; buf[43] = info.meter2[1]; buf[44] = info.meter2[2]; buf[45] = info.meter2[3]; buf[46] = info.colors[0]; buf[47] = info.colors[1]; buf[48] = info.colors[2]; buf[49] = info.colors[3]; buf[50] = info.important[0]; buf[51] = info.important[1]; buf[52] = info.important[2]; buf[53] = info.important[3]; }