invisible
view src/frame.cc @ 5:700127288dc5
quick backup
todo: fix save_ppm
subimage2d
author | Eleni Maria Stea <eleni@mutantstargoat.com> |
---|---|
date | Thu, 24 Oct 2013 00:09:13 +0300 |
parents | 1ff5a1a50b41 |
children | db8f1c036d0e |
line source
1 #include <GL/gl.h>
2 #include <cxcore.h>
3 #include <highgui.h> //TODO remove
5 #include <pthread.h>
6 #include <stdio.h>
7 #include <string.h>
9 #include "frame.h"
10 #include "kinect.h"
12 extern Frame *frame;
13 extern KinectParams *kin_params;
15 extern bool has_video;
16 extern bool has_depth;
18 Frame::Frame()
19 {
20 video_buf = cv::Mat::zeros(KINECT_VIDEO_HEIGHT, KINECT_VIDEO_WIDTH, CV_8UC3);
21 depth_buf = cv::Mat::zeros(KINECT_DEPTH_HEIGHT, KINECT_DEPTH_WIDTH, CV_8UC1);
23 tex_setup();
24 }
26 void Frame::tex_setup()
27 {
28 glGenTextures(1, &video_tex);
29 glGenTextures(1, &depth_tex);
31 glBindTexture(GL_TEXTURE_2D, video_tex);
32 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
33 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
34 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
35 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
36 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, video_buf.size().width, video_buf.size().height,
37 0, GL_BGRA, GL_UNSIGNED_BYTE, &video_buf);
39 glBindTexture(GL_TEXTURE_2D, depth_tex);
40 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
44 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, depth_buf.size().width, depth_buf.size().height,
45 0, GL_BGRA, GL_UNSIGNED_BYTE, &depth_buf);
47 }
49 void Frame::process()
50 {
51 if(has_video && has_depth)
52 printf("Depth and Rgb\n");
53 }
55 void video_cb(freenect_device *kin_dev, void *video, uint32_t time)
56 {
57 if(!video || !frame) {
58 has_video = false;
59 return;
60 }
62 /* freenect video data to cv mat: */
63 unsigned char* src = (unsigned char*)video;
64 unsigned char* dest = frame->video_buf.data;
66 for(int i=0; i<KINECT_VIDEO_HEIGHT * KINECT_VIDEO_WIDTH; i++) {
67 dest[0] = src[2];
68 dest[1] = src[1];
69 dest[2] = src[0];
70 dest += 3;
71 src += 3;
72 }
73 has_video = true;
74 }
76 void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time)
77 {
78 if(!depth || !frame) {
79 has_depth = false;
80 return;
81 }
83 /* freenect depth data to cv mat: */
84 unsigned char* src = (unsigned char*)depth;
85 unsigned char* dest = frame->depth_buf.data;
87 for(int i=0; i<KINECT_DEPTH_HEIGHT * KINECT_DEPTH_WIDTH; i++) {
88 *dest = *src;
89 dest++;
90 src++;
91 }
93 save_depth_ppm(depth, 640, 480);
94 has_depth = true;
95 }
97 bool save_video_ppm(void *video, int w, int h)
98 {
99 FILE *fp;
100 if(!(fp = fopen("test_video.ppm", "wb"))) {
101 fprintf(stderr, "Failed to open video file for writing.\n");
102 return false;
103 }
104 fprintf(fp, "P6\n640 480\n255\n");
105 /* unsigned char *ptr = (unsigned char*)video;
106 for(int i=0; i<w * h * 3; i++) {
107 fputc(*ptr, fp);
108 ptr++;
109 }
110 fclose(fp);
111 return true;
112 */
113 return false;
114 }
116 bool save_depth_ppm(void *depth, int w, int h)
117 {
118 FILE *fp;
119 if(!(fp = fopen("test_depth.ppm", "wb"))) {
120 fprintf(stderr, "Failed to open depth file for writing.\n");
121 return false;
122 }
123 fprintf(fp, "P6\n640 480\n255\n");
124 unsigned char *ptr = (unsigned char*)depth;
125 unsigned char *foo = (unsigned char*)depth;
126 for(int i=0; i<h; i++) {
127 for(int j=0; j<w; j++) {
128 *foo = *ptr;
129 fputc(foo[i * w + j], fp);
130 ptr++;
131 }
132 }
133 fclose(fp);
134 return true;
135 }