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.ImageUtilities;
040import org.openimaj.image.MBFImage;
041import org.openimaj.image.colour.ColourSpace;
042
043/**
044 * Callback for rgb camera data
045 * 
046 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
047 * 
048 */
049class RGBVideoCallback extends KinectVideoStreamCallback<MBFImage> {
050        public RGBVideoCallback(KinectStream<MBFImage> stream) {
051                super(stream);
052                final Pointer<freenect_device> device = stream.controller.device;
053
054                libfreenectLibrary.freenect_set_video_mode_proxy(device, freenect_resolution.FREENECT_RESOLUTION_MEDIUM,
055                                freenect_video_format.FREENECT_VIDEO_RGB);
056
057                buffer = ByteBuffer.allocateDirect(libfreenectLibrary.freenect_get_video_buffer_size(device));
058                libfreenectLibrary.freenect_set_video_buffer(device, Pointer.pointerToBuffer(buffer));
059
060                nextFrame = new MBFImage(stream.width, stream.height, ColourSpace.RGB);
061
062                libfreenectLibrary.freenect_set_video_callback(device, toPointer());
063                libfreenectLibrary.freenect_start_video(device);
064        }
065
066        @Override
067        public void setImage() {
068                final ByteBuffer buf = buffer.duplicate();
069
070                final int width = stream.width;
071                final int height = stream.height;
072
073                final float[][] r = nextFrame.bands.get(0).pixels;
074                final float[][] g = nextFrame.bands.get(1).pixels;
075                final float[][] b = nextFrame.bands.get(2).pixels;
076
077                for (int y = 0; y < height; y++) {
078                        for (int x = 0; x < width; x++) {
079                                final int red = buf.get() & 0xFF;
080                                final int green = buf.get() & 0xFF;
081                                final int blue = buf.get() & 0xFF;
082                                r[y][x] = ImageUtilities.BYTE_TO_FLOAT_LUT[red];
083                                g[y][x] = ImageUtilities.BYTE_TO_FLOAT_LUT[green];
084                                b[y][x] = ImageUtilities.BYTE_TO_FLOAT_LUT[blue];
085                        }
086                }
087        }
088
089        @Override
090        public void stop() {
091                libfreenectLibrary.freenect_stop_video(stream.controller.device);
092        }
093}
094
095/**
096 * The stream of RGB information
097 * 
098 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
099 * 
100 */
101public class KinectRGBVideoStream extends KinectStream<MBFImage> {
102        /**
103         * Construct with a reference to the controller
104         * 
105         * @param controller
106         *            The controller
107         */
108        public KinectRGBVideoStream(KinectController controller) {
109                super(controller);
110
111                fps = 30;
112                width = 640;
113                height = 480;
114                frame = new MBFImage(width, height, ColourSpace.RGB);
115
116                callback = new RGBVideoCallback(this);
117        }
118}