|
| 1 | +/* |
| 2 | + * This file is part of the OpenKinect Project. http://www.openkinect.org |
| 3 | + * |
| 4 | + * Copyright (c) 2014 Benn Snyder, 2015 individual OpenKinect contributors. |
| 5 | + * See the CONTRIB file for details. |
| 6 | + * |
| 7 | + * This code is licensed to you under the terms of the Apache License, version |
| 8 | + * 2.0, or, at your option, the terms of the GNU General Public License, |
| 9 | + * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, |
| 10 | + * or the following URLs: |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * http://www.gnu.org/licenses/gpl-2.0.txt |
| 13 | + * |
| 14 | + * If you redistribute this file in source form, modified or unmodified, you |
| 15 | + * may: |
| 16 | + * 1) Leave this header intact and distribute it under the same terms, |
| 17 | + * accompanying it with the APACHE20 and GPL20 files, or |
| 18 | + * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or |
| 19 | + * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file |
| 20 | + * In all cases you must keep the copyright notice intact and include a copy |
| 21 | + * of the CONTRIB file. |
| 22 | + * |
| 23 | + * Binary distributions must follow the binary distribution requirements of |
| 24 | + * either License. |
| 25 | + */ |
| 26 | + |
| 27 | +#define _USE_MATH_DEFINES |
| 28 | +#include <cmath> // for M_PI |
| 29 | +#include "ColorStream.hpp" |
| 30 | + |
| 31 | +using namespace Freenect2Driver; |
| 32 | + |
| 33 | +// from NUI library & converted to radians |
| 34 | +const float ColorStream::DIAGONAL_FOV = 73.9 * (M_PI / 180); |
| 35 | +const float ColorStream::HORIZONTAL_FOV = 62 * (M_PI / 180); |
| 36 | +const float ColorStream::VERTICAL_FOV = 48.6 * (M_PI / 180); |
| 37 | + |
| 38 | +ColorStream::ColorStream(libfreenect2::Freenect2Device* pDevice, Freenect2Driver::Registration *reg) : VideoStream(pDevice, reg) |
| 39 | +{ |
| 40 | + video_mode = makeOniVideoMode(ONI_PIXEL_FORMAT_RGB888, 1920, 1080, 30); |
| 41 | + setVideoMode(video_mode); |
| 42 | +} |
| 43 | + |
| 44 | +// Add video modes here as you implement them |
| 45 | +ColorStream::FreenectVideoModeMap ColorStream::getSupportedVideoModes() const |
| 46 | +{ |
| 47 | + FreenectVideoModeMap modes; |
| 48 | + // pixelFormat, resolutionX, resolutionY, fps freenect_video_format, freenect_resolution |
| 49 | + modes[makeOniVideoMode(ONI_PIXEL_FORMAT_RGB888, 512, 424, 30)] = 0; |
| 50 | + modes[makeOniVideoMode(ONI_PIXEL_FORMAT_RGB888, 1920, 1080, 30)] = 1; |
| 51 | + |
| 52 | + return modes; |
| 53 | + |
| 54 | + /* working format possiblities |
| 55 | + FREENECT_VIDEO_RGB |
| 56 | + FREENECT_VIDEO_YUV_RGB |
| 57 | + FREENECT_VIDEO_YUV_RAW |
| 58 | + */ |
| 59 | +} |
| 60 | + |
| 61 | +void ColorStream::populateFrame(libfreenect2::Frame* srcFrame, int srcX, int srcY, OniFrame* dstFrame, int dstX, int dstY, int width, int height) const |
| 62 | +{ |
| 63 | + dstFrame->sensorType = getSensorType(); |
| 64 | + dstFrame->stride = dstFrame->width * 3; |
| 65 | + |
| 66 | + // copy stream buffer from freenect |
| 67 | + switch (video_mode.pixelFormat) |
| 68 | + { |
| 69 | + default: |
| 70 | + LogError("Pixel format " + to_string(video_mode.pixelFormat) + " not supported by populateFrame()"); |
| 71 | + return; |
| 72 | + |
| 73 | + case ONI_PIXEL_FORMAT_RGB888: |
| 74 | + if (reg->isEnabled()) { |
| 75 | + libfreenect2::Frame registered(512, 424, 4); |
| 76 | + |
| 77 | + reg->colorFrameRGB888(srcFrame, ®istered); |
| 78 | + |
| 79 | + copyFrame(static_cast<uint8_t*>(registered.data), srcX, srcY, registered.width * registered.bytes_per_pixel, |
| 80 | + static_cast<uint8_t*>(dstFrame->data), dstX, dstY, dstFrame->stride, |
| 81 | + width, height, mirroring); |
| 82 | + } else { |
| 83 | + copyFrame(static_cast<uint8_t*>(srcFrame->data), srcX, srcY, srcFrame->width * srcFrame->bytes_per_pixel, |
| 84 | + static_cast<uint8_t*>(dstFrame->data), dstX, dstY, dstFrame->stride, |
| 85 | + width, height, mirroring); |
| 86 | + } |
| 87 | + return; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +void ColorStream::copyFrame(uint8_t* srcPix, int srcX, int srcY, int srcStride, uint8_t* dstPix, int dstX, int dstY, int dstStride, int width, int height, bool mirroring) |
| 92 | +{ |
| 93 | + srcPix += srcX + srcY * srcStride; |
| 94 | + dstPix += dstX + dstY * dstStride; |
| 95 | + |
| 96 | + for (int y = 0; y < height; y++) { |
| 97 | + uint8_t* dst = dstPix + y * dstStride; |
| 98 | + uint8_t* src = srcPix + y * srcStride; |
| 99 | + if (mirroring) { |
| 100 | + dst += dstStride - 1; |
| 101 | + for (int x = 0; x < srcStride; ++x) |
| 102 | + { |
| 103 | + if (x % 4 != 3) |
| 104 | + { |
| 105 | + *dst-- = *src++; |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + ++src; |
| 110 | + } |
| 111 | + } |
| 112 | + } else { |
| 113 | + for (int x = 0; x < dstStride-2; x += 3) |
| 114 | + { |
| 115 | + *dst++ = src[2]; |
| 116 | + *dst++ = src[1]; |
| 117 | + *dst++ = src[0]; |
| 118 | + src += 4; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +OniSensorType ColorStream::getSensorType() const { return ONI_SENSOR_COLOR; } |
| 125 | + |
| 126 | +OniStatus ColorStream::setImageRegistrationMode(OniImageRegistrationMode mode) |
| 127 | +{ |
| 128 | + if (mode == ONI_IMAGE_REGISTRATION_DEPTH_TO_COLOR) { |
| 129 | + // XXX, switch color resolution to 512x424 for registrarion here |
| 130 | + OniVideoMode video_mode = makeOniVideoMode(ONI_PIXEL_FORMAT_RGB888, 512, 424, 30); |
| 131 | + setProperty(ONI_STREAM_PROPERTY_VIDEO_MODE, &video_mode, sizeof(video_mode)); |
| 132 | + } |
| 133 | + return ONI_STATUS_OK; |
| 134 | +} |
| 135 | + |
| 136 | +// from StreamBase |
| 137 | +OniBool ColorStream::isPropertySupported(int propertyId) |
| 138 | +{ |
| 139 | + switch(propertyId) |
| 140 | + { |
| 141 | + default: |
| 142 | + return VideoStream::isPropertySupported(propertyId); |
| 143 | + |
| 144 | + case ONI_STREAM_PROPERTY_HORIZONTAL_FOV: |
| 145 | + case ONI_STREAM_PROPERTY_VERTICAL_FOV: |
| 146 | + case ONI_STREAM_PROPERTY_AUTO_WHITE_BALANCE: |
| 147 | + case ONI_STREAM_PROPERTY_AUTO_EXPOSURE: |
| 148 | + return true; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +OniStatus ColorStream::getProperty(int propertyId, void* data, int* pDataSize) |
| 153 | +{ |
| 154 | + switch (propertyId) |
| 155 | + { |
| 156 | + default: |
| 157 | + return VideoStream::getProperty(propertyId, data, pDataSize); |
| 158 | + |
| 159 | + case ONI_STREAM_PROPERTY_HORIZONTAL_FOV: // float (radians) |
| 160 | + { |
| 161 | + if (*pDataSize != sizeof(float)) |
| 162 | + { |
| 163 | + LogError("Unexpected size for ONI_STREAM_PROPERTY_HORIZONTAL_FOV"); |
| 164 | + return ONI_STATUS_ERROR; |
| 165 | + } |
| 166 | + *(static_cast<float*>(data)) = HORIZONTAL_FOV; |
| 167 | + return ONI_STATUS_OK; |
| 168 | + } |
| 169 | + case ONI_STREAM_PROPERTY_VERTICAL_FOV: // float (radians) |
| 170 | + { |
| 171 | + if (*pDataSize != sizeof(float)) |
| 172 | + { |
| 173 | + LogError("Unexpected size for ONI_STREAM_PROPERTY_VERTICAL_FOV"); |
| 174 | + return ONI_STATUS_ERROR; |
| 175 | + } |
| 176 | + *(static_cast<float*>(data)) = VERTICAL_FOV; |
| 177 | + return ONI_STATUS_OK; |
| 178 | + } |
| 179 | + |
| 180 | + // camera |
| 181 | + case ONI_STREAM_PROPERTY_AUTO_WHITE_BALANCE: // OniBool |
| 182 | + { |
| 183 | + if (*pDataSize != sizeof(OniBool)) |
| 184 | + { |
| 185 | + LogError("Unexpected size for ONI_STREAM_PROPERTY_AUTO_WHITE_BALANCE"); |
| 186 | + return ONI_STATUS_ERROR; |
| 187 | + } |
| 188 | + *(static_cast<OniBool*>(data)) = auto_white_balance; |
| 189 | + return ONI_STATUS_OK; |
| 190 | + } |
| 191 | + case ONI_STREAM_PROPERTY_AUTO_EXPOSURE: // OniBool |
| 192 | + { |
| 193 | + if (*pDataSize != sizeof(OniBool)) |
| 194 | + { |
| 195 | + LogError("Unexpected size for ONI_STREAM_PROPERTY_AUTO_EXPOSURE"); |
| 196 | + return ONI_STATUS_ERROR; |
| 197 | + } |
| 198 | + *(static_cast<OniBool*>(data)) = auto_exposure; |
| 199 | + return ONI_STATUS_OK; |
| 200 | + } |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +OniStatus ColorStream::setProperty(int propertyId, const void* data, int dataSize) |
| 205 | +{ |
| 206 | + switch (propertyId) |
| 207 | + { |
| 208 | + case 0: |
| 209 | + default: |
| 210 | + return VideoStream::setProperty(propertyId, data, dataSize); |
| 211 | + } |
| 212 | +} |
0 commit comments