001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.hardware.kinect; 031 032import java.nio.ByteBuffer; 033 034import org.bridj.Pointer; 035import org.openimaj.hardware.kinect.freenect.libfreenectLibrary; 036import org.openimaj.hardware.kinect.freenect.libfreenectLibrary.freenect_device; 037import org.openimaj.hardware.kinect.freenect.libfreenectLibrary.freenect_resolution; 038import org.openimaj.hardware.kinect.freenect.libfreenectLibrary.freenect_video_format; 039import org.openimaj.image.FImage; 040 041/** 042 * Callback for IR data 043 * 044 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 045 * 046 */ 047class IRVideoCallback extends KinectVideoStreamCallback<FImage> { 048 private static final float[] LUT = new float[1024]; 049 static { 050 for (int i = 0; i < LUT.length; i++) 051 LUT[i] = i / (LUT.length - 1f); 052 } 053 054 public IRVideoCallback(KinectStream<FImage> stream) { 055 super(stream); 056 final Pointer<freenect_device> device = stream.controller.device; 057 058 libfreenectLibrary.freenect_set_video_mode_proxy(device, freenect_resolution.FREENECT_RESOLUTION_MEDIUM, 059 freenect_video_format.FREENECT_VIDEO_IR_10BIT); 060 061 buffer = ByteBuffer.allocateDirect(libfreenectLibrary.freenect_get_video_buffer_size(device)); 062 libfreenectLibrary.freenect_set_video_buffer(device, Pointer.pointerToBuffer(buffer)); 063 064 nextFrame = new FImage(stream.width, stream.height); 065 066 libfreenectLibrary.freenect_set_video_callback(device, toPointer()); 067 libfreenectLibrary.freenect_start_video(device); 068 } 069 070 @Override 071 public void setImage() { 072 final ByteBuffer buf = buffer.duplicate(); 073 074 final int width = stream.width; 075 final int height = stream.height; 076 077 final float[][] pix = nextFrame.pixels; 078 079 for (int y = 0; y < height; y++) { 080 for (int x = 0; x < width; x++) { 081 final int first = (buf.get() & 0xFF); 082 final int second = (buf.get() & 0xFF); 083 pix[y][x] = LUT[first + second * 256]; 084 } 085 } 086 } 087 088 @Override 089 public void stop() { 090 libfreenectLibrary.freenect_stop_video(stream.controller.device); 091 } 092} 093 094/** 095 * The stream of IR information 096 * 097 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 098 * 099 */ 100public class KinectIRVideoStream extends KinectStream<FImage> { 101 /** 102 * Construct with a reference to the controller 103 * 104 * @param controller 105 * The controller 106 */ 107 public KinectIRVideoStream(KinectController controller) { 108 super(controller); 109 110 fps = 30; 111 width = 640; 112 height = 480; 113 frame = new FImage(width, height); 114 115 callback = new IRVideoCallback(this); 116 } 117}