invisible

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