invisible

diff 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 diff
     1.1 --- a/src/frame.cc	Wed Oct 16 15:41:08 2013 +0300
     1.2 +++ b/src/frame.cc	Thu Oct 24 00:09:13 2013 +0300
     1.3 @@ -1,3 +1,4 @@
     1.4 +#include <GL/gl.h>
     1.5  #include <cxcore.h>
     1.6  #include <highgui.h> //TODO remove
     1.7  
     1.8 @@ -16,8 +17,33 @@
     1.9  
    1.10  Frame::Frame()
    1.11  {
    1.12 -    video_buf = cv::Mat(FREENECT_VIDEO_WIDTH, FREENECT_VIDEO_HEIGHT, CV_8UC1, 0);
    1.13 -    depth_buf = cv::Mat(FREENECT_DEPTH_WIDTH, FREENECT_DEPTH_HEIGHT, CV_16UC1, 0);
    1.14 +    video_buf = cv::Mat::zeros(KINECT_VIDEO_HEIGHT, KINECT_VIDEO_WIDTH, CV_8UC3);
    1.15 +    depth_buf = cv::Mat::zeros(KINECT_DEPTH_HEIGHT, KINECT_DEPTH_WIDTH, CV_8UC1);
    1.16 +
    1.17 +	tex_setup();
    1.18 +}
    1.19 +
    1.20 +void Frame::tex_setup()
    1.21 +{
    1.22 +	glGenTextures(1, &video_tex);
    1.23 +	glGenTextures(1, &depth_tex);
    1.24 +
    1.25 +	glBindTexture(GL_TEXTURE_2D, video_tex);
    1.26 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    1.27 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    1.28 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    1.29 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    1.30 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, video_buf.size().width, video_buf.size().height,
    1.31 +			0, GL_BGRA, GL_UNSIGNED_BYTE, &video_buf);
    1.32 +
    1.33 +	glBindTexture(GL_TEXTURE_2D, depth_tex);
    1.34 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    1.35 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    1.36 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    1.37 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    1.38 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, depth_buf.size().width, depth_buf.size().height,
    1.39 +			0, GL_BGRA, GL_UNSIGNED_BYTE, &depth_buf);
    1.40 +
    1.41  }
    1.42  
    1.43  void Frame::process()
    1.44 @@ -33,22 +59,77 @@
    1.45  		return;
    1.46  	}
    1.47  
    1.48 -    uint8_t* video_data = static_cast<uint8_t*>(video);
    1.49 -    frame->video_buf.data = (uchar*)video_data;
    1.50 -    //frame->video_buf.convertTo(frame->video_buf, CV_8UC1);
    1.51 -    cv::imshow("wx", frame->video_buf);
    1.52 -    cv::waitKey(100);
    1.53 +	/* freenect video data to cv mat: */
    1.54 +	unsigned char* src = (unsigned char*)video;
    1.55 +	unsigned char* dest = frame->video_buf.data;
    1.56 +
    1.57 +	for(int i=0; i<KINECT_VIDEO_HEIGHT * KINECT_VIDEO_WIDTH; i++) {
    1.58 +		dest[0] = src[2];
    1.59 +		dest[1] = src[1];
    1.60 +		dest[2] = src[0];
    1.61 +		dest += 3;
    1.62 +		src += 3;
    1.63 +	}
    1.64  	has_video = true;
    1.65  }
    1.66  
    1.67  void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time)
    1.68  {
    1.69 -	if(!depth) {
    1.70 +	if(!depth || !frame) {
    1.71  		has_depth = false;
    1.72  		return;
    1.73  	}
    1.74  
    1.75 -    uint16_t* depth_data = static_cast<uint16_t*>(depth);
    1.76 -    frame->depth_buf.data = (uchar*)depth_data;
    1.77 +	/* freenect depth data to cv mat: */
    1.78 +	unsigned char* src = (unsigned char*)depth;
    1.79 +	unsigned char* dest = frame->depth_buf.data;
    1.80 +
    1.81 +	for(int i=0; i<KINECT_DEPTH_HEIGHT * KINECT_DEPTH_WIDTH; i++) {
    1.82 +		*dest = *src;
    1.83 +		dest++;
    1.84 +		src++;
    1.85 +	}
    1.86 +
    1.87 +	save_depth_ppm(depth, 640, 480);
    1.88  	has_depth = true;
    1.89  }
    1.90 +
    1.91 +bool save_video_ppm(void *video, int w, int h)
    1.92 +{
    1.93 +	FILE *fp;
    1.94 +	if(!(fp = fopen("test_video.ppm", "wb"))) {
    1.95 +		fprintf(stderr, "Failed to open video file for writing.\n");
    1.96 +		return false;
    1.97 +	}
    1.98 +	fprintf(fp, "P6\n640 480\n255\n");
    1.99 +/*	unsigned char *ptr = (unsigned char*)video;
   1.100 +	for(int i=0; i<w * h * 3; i++) {
   1.101 +		fputc(*ptr, fp);
   1.102 +		ptr++;
   1.103 +	}
   1.104 +	fclose(fp);
   1.105 +	return true;
   1.106 +	*/
   1.107 +	return false;
   1.108 +}
   1.109 +
   1.110 +bool save_depth_ppm(void *depth, int w, int h)
   1.111 +{
   1.112 +	FILE *fp;
   1.113 +	if(!(fp = fopen("test_depth.ppm", "wb"))) {
   1.114 +		fprintf(stderr, "Failed to open depth file for writing.\n");
   1.115 +		return false;
   1.116 +	}
   1.117 +	fprintf(fp, "P6\n640 480\n255\n");
   1.118 +	unsigned char *ptr = (unsigned char*)depth;
   1.119 +	unsigned char *foo = (unsigned char*)depth;
   1.120 +	for(int i=0; i<h; i++) {
   1.121 +		for(int j=0; j<w; j++) {
   1.122 +			*foo = *ptr;
   1.123 +			fputc(foo[i * w + j], fp);
   1.124 +			ptr++;
   1.125 +		}
   1.126 +	}
   1.127 +	fclose(fp);
   1.128 +	return true;
   1.129 +}