invisible

annotate src/frame.cc @ 6:db8f1c036d0e

quick backup - skata o kwdikas
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sun, 03 Nov 2013 20:36:45 +0200
parents 700127288dc5
children bd1b0385a10b
rev   line source
eleni@5 1 #include <GL/gl.h>
eleni@4 2 #include <cxcore.h>
eleni@4 3 #include <highgui.h> //TODO remove
eleni@6 4 #include <cv.h> //TODO remove
eleni@4 5
eleni@2 6 #include <pthread.h>
eleni@1 7 #include <stdio.h>
eleni@2 8 #include <string.h>
eleni@1 9
eleni@1 10 #include "frame.h"
eleni@2 11 #include "kinect.h"
eleni@1 12
eleni@2 13 extern Frame *frame;
eleni@2 14 extern KinectParams *kin_params;
eleni@1 15
eleni@2 16 extern bool has_video;
eleni@2 17 extern bool has_depth;
eleni@2 18
eleni@2 19 Frame::Frame()
eleni@1 20 {
eleni@5 21 video_buf = cv::Mat::zeros(KINECT_VIDEO_HEIGHT, KINECT_VIDEO_WIDTH, CV_8UC3);
eleni@6 22 depth_buf = cv::Mat::zeros(KINECT_DEPTH_HEIGHT, KINECT_DEPTH_WIDTH, CV_16UC1);
eleni@5 23
eleni@5 24 tex_setup();
eleni@5 25 }
eleni@5 26
eleni@5 27 void Frame::tex_setup()
eleni@5 28 {
eleni@5 29 glGenTextures(1, &video_tex);
eleni@5 30 glGenTextures(1, &depth_tex);
eleni@5 31
eleni@5 32 glBindTexture(GL_TEXTURE_2D, video_tex);
eleni@5 33 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
eleni@5 34 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
eleni@5 35 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
eleni@5 36 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
eleni@5 37 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, video_buf.size().width, video_buf.size().height,
eleni@5 38 0, GL_BGRA, GL_UNSIGNED_BYTE, &video_buf);
eleni@5 39
eleni@5 40 glBindTexture(GL_TEXTURE_2D, depth_tex);
eleni@5 41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
eleni@5 42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
eleni@5 43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
eleni@5 44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
eleni@5 45 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, depth_buf.size().width, depth_buf.size().height,
eleni@5 46 0, GL_BGRA, GL_UNSIGNED_BYTE, &depth_buf);
eleni@5 47
eleni@1 48 }
eleni@1 49
eleni@2 50 void Frame::process()
eleni@1 51 {
eleni@2 52 if(has_video && has_depth)
eleni@2 53 printf("Depth and Rgb\n");
eleni@2 54 }
eleni@2 55
eleni@2 56 void video_cb(freenect_device *kin_dev, void *video, uint32_t time)
eleni@2 57 {
eleni@4 58 if(!video || !frame) {
eleni@2 59 has_video = false;
eleni@2 60 return;
eleni@2 61 }
eleni@2 62
eleni@5 63 /* freenect video data to cv mat: */
eleni@5 64 unsigned char* src = (unsigned char*)video;
eleni@5 65 unsigned char* dest = frame->video_buf.data;
eleni@5 66
eleni@5 67 for(int i=0; i<KINECT_VIDEO_HEIGHT * KINECT_VIDEO_WIDTH; i++) {
eleni@5 68 dest[0] = src[2];
eleni@5 69 dest[1] = src[1];
eleni@5 70 dest[2] = src[0];
eleni@5 71 dest += 3;
eleni@5 72 src += 3;
eleni@5 73 }
eleni@2 74 has_video = true;
eleni@1 75 }
eleni@1 76
eleni@1 77 void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time)
eleni@1 78 {
eleni@5 79 if(!depth || !frame) {
eleni@2 80 has_depth = false;
eleni@2 81 return;
eleni@2 82 }
eleni@2 83
eleni@5 84 /* freenect depth data to cv mat: */
eleni@5 85
eleni@6 86 memcpy(frame->depth_buf.data, depth, KINECT_DEPTH_WIDTH * KINECT_DEPTH_HEIGHT * 2);
eleni@5 87
eleni@6 88 save_depth_ppm(depth, KINECT_DEPTH_WIDTH, KINECT_DEPTH_HEIGHT);
eleni@6 89 cv::imshow("foo", frame->depth_buf);
eleni@6 90 cv::waitKey(100);
eleni@2 91 has_depth = true;
eleni@1 92 }
eleni@5 93
eleni@5 94 bool save_video_ppm(void *video, int w, int h)
eleni@5 95 {
eleni@5 96 FILE *fp;
eleni@5 97 if(!(fp = fopen("test_video.ppm", "wb"))) {
eleni@5 98 fprintf(stderr, "Failed to open video file for writing.\n");
eleni@5 99 return false;
eleni@5 100 }
eleni@6 101 fprintf(fp, "P6\n%d %d\n255\n", w, h);
eleni@6 102 unsigned char *ptr = (unsigned char*)video;
eleni@5 103 for(int i=0; i<w * h * 3; i++) {
eleni@5 104 fputc(*ptr, fp);
eleni@5 105 ptr++;
eleni@5 106 }
eleni@5 107 fclose(fp);
eleni@5 108 return true;
eleni@5 109 }
eleni@5 110
eleni@5 111 bool save_depth_ppm(void *depth, int w, int h)
eleni@5 112 {
eleni@5 113 FILE *fp;
eleni@5 114 if(!(fp = fopen("test_depth.ppm", "wb"))) {
eleni@5 115 fprintf(stderr, "Failed to open depth file for writing.\n");
eleni@5 116 return false;
eleni@5 117 }
eleni@6 118 fprintf(fp, "P6\n%d %d\n255\n", w, h);
eleni@6 119 uint16_t *ptr = (uint16_t*)depth;
eleni@6 120 for(int i=0; i<w * h; i++) {
eleni@6 121 unsigned char c = *ptr++ >> 8;
eleni@6 122 fputc(c, fp);
eleni@6 123 fputc(c, fp);
eleni@6 124 fputc(c, fp);
eleni@5 125 }
eleni@5 126 fclose(fp);
eleni@5 127 return true;
eleni@5 128 }