File indexing completed on 2025-01-05 04:02:09
0001 /* 0002 * 1394-Based Digital Camera Control Library 0003 * 0004 * Bayer pattern decoding functions 0005 * 0006 * Written by Damien Douxchamps and Frederic Devernay 0007 * The original VNG and AHD Bayer decoding are from Dave Coffin's DCRAW. 0008 * 0009 * This library is free software; you can redistribute it and/or 0010 * modify it under the terms of the GNU Lesser General Public 0011 * License as published by the Free Software Foundation; either 0012 * version 2.1 of the License, or (at your option) any later version. 0013 * 0014 * This library is distributed in the hope that it will be useful, 0015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0017 * Lesser General Public License for more details. 0018 * 0019 * You should have received a copy of the GNU Lesser General Public 0020 * License along with this library; if not, write to the Free Software 0021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 0022 */ 0023 0024 /* clang-format off */ 0025 0026 #pragma once 0027 0028 #include <stdint.h> 0029 0030 /** 0031 * Error codes returned by most libdc1394 functions. 0032 * 0033 * General rule: 0 is success, negative denotes a problem. 0034 */ 0035 typedef enum { 0036 DC1394_SUCCESS = 0, 0037 DC1394_FAILURE = -1, 0038 DC1394_NOT_A_CAMERA = -2, 0039 DC1394_FUNCTION_NOT_SUPPORTED = -3, 0040 DC1394_CAMERA_NOT_INITIALIZED = -4, 0041 DC1394_MEMORY_ALLOCATION_FAILURE = -5, 0042 DC1394_TAGGED_REGISTER_NOT_FOUND = -6, 0043 DC1394_NO_ISO_CHANNEL = -7, 0044 DC1394_NO_BANDWIDTH = -8, 0045 DC1394_IOCTL_FAILURE = -9, 0046 DC1394_CAPTURE_IS_NOT_SET = -10, 0047 DC1394_CAPTURE_IS_RUNNING = -11, 0048 DC1394_RAW1394_FAILURE = -12, 0049 DC1394_FORMAT7_ERROR_FLAG_1 = -13, 0050 DC1394_FORMAT7_ERROR_FLAG_2 = -14, 0051 DC1394_INVALID_ARGUMENT_VALUE = -15, 0052 DC1394_REQ_VALUE_OUTSIDE_RANGE = -16, 0053 DC1394_INVALID_FEATURE = -17, 0054 DC1394_INVALID_VIDEO_FORMAT = -18, 0055 DC1394_INVALID_VIDEO_MODE = -19, 0056 DC1394_INVALID_FRAMERATE = -20, 0057 DC1394_INVALID_TRIGGER_MODE = -21, 0058 DC1394_INVALID_TRIGGER_SOURCE = -22, 0059 DC1394_INVALID_ISO_SPEED = -23, 0060 DC1394_INVALID_IIDC_VERSION = -24, 0061 DC1394_INVALID_COLOR_CODING = -25, 0062 DC1394_INVALID_COLOR_FILTER = -26, 0063 DC1394_INVALID_CAPTURE_POLICY = -27, 0064 DC1394_INVALID_ERROR_CODE = -28, 0065 DC1394_INVALID_BAYER_METHOD = -29, 0066 DC1394_INVALID_VIDEO1394_DEVICE = -30, 0067 DC1394_INVALID_OPERATION_MODE = -31, 0068 DC1394_INVALID_TRIGGER_POLARITY = -32, 0069 DC1394_INVALID_FEATURE_MODE = -33, 0070 DC1394_INVALID_LOG_TYPE = -34, 0071 DC1394_INVALID_BYTE_ORDER = -35, 0072 DC1394_INVALID_STEREO_METHOD = -36, 0073 DC1394_BASLER_NO_MORE_SFF_CHUNKS = -37, 0074 DC1394_BASLER_CORRUPTED_SFF_CHUNK = -38, 0075 DC1394_BASLER_UNKNOWN_SFF_CHUNK = -39, 0076 } dc1394error_t; 0077 #define DC1394_ERROR_MIN DC1394_BASLER_UNKNOWN_SFF_CHUNK 0078 #define DC1394_ERROR_MAX DC1394_SUCCESS 0079 #define DC1394_ERROR_NUM (DC1394_ERROR_MAX - DC1394_ERROR_MIN + 1) 0080 0081 /** 0082 * Enumeration of video modes. Note that the notion of IIDC "format" is not present here, except in the format_7 name. 0083 */ 0084 typedef enum { 0085 DC1394_VIDEO_MODE_160x120_YUV444 = 64, 0086 DC1394_VIDEO_MODE_320x240_YUV422, 0087 DC1394_VIDEO_MODE_640x480_YUV411, 0088 DC1394_VIDEO_MODE_640x480_YUV422, 0089 DC1394_VIDEO_MODE_640x480_RGB8, 0090 DC1394_VIDEO_MODE_640x480_MONO8, 0091 DC1394_VIDEO_MODE_640x480_MONO16, 0092 DC1394_VIDEO_MODE_800x600_YUV422, 0093 DC1394_VIDEO_MODE_800x600_RGB8, 0094 DC1394_VIDEO_MODE_800x600_MONO8, 0095 DC1394_VIDEO_MODE_1024x768_YUV422, 0096 DC1394_VIDEO_MODE_1024x768_RGB8, 0097 DC1394_VIDEO_MODE_1024x768_MONO8, 0098 DC1394_VIDEO_MODE_800x600_MONO16, 0099 DC1394_VIDEO_MODE_1024x768_MONO16, 0100 DC1394_VIDEO_MODE_1280x960_YUV422, 0101 DC1394_VIDEO_MODE_1280x960_RGB8, 0102 DC1394_VIDEO_MODE_1280x960_MONO8, 0103 DC1394_VIDEO_MODE_1600x1200_YUV422, 0104 DC1394_VIDEO_MODE_1600x1200_RGB8, 0105 DC1394_VIDEO_MODE_1600x1200_MONO8, 0106 DC1394_VIDEO_MODE_1280x960_MONO16, 0107 DC1394_VIDEO_MODE_1600x1200_MONO16, 0108 DC1394_VIDEO_MODE_EXIF, 0109 DC1394_VIDEO_MODE_FORMAT7_0, 0110 DC1394_VIDEO_MODE_FORMAT7_1, 0111 DC1394_VIDEO_MODE_FORMAT7_2, 0112 DC1394_VIDEO_MODE_FORMAT7_3, 0113 DC1394_VIDEO_MODE_FORMAT7_4, 0114 DC1394_VIDEO_MODE_FORMAT7_5, 0115 DC1394_VIDEO_MODE_FORMAT7_6, 0116 DC1394_VIDEO_MODE_FORMAT7_7, 0117 } dc1394video_mode_t; 0118 #define DC1394_VIDEO_MODE_MIN DC1394_VIDEO_MODE_160x120_YUV444 0119 #define DC1394_VIDEO_MODE_MAX DC1394_VIDEO_MODE_FORMAT7_7 0120 #define DC1394_VIDEO_MODE_NUM (DC1394_VIDEO_MODE_MAX - DC1394_VIDEO_MODE_MIN + 1) 0121 0122 /* Special min/max are defined for Format_7 */ 0123 #define DC1394_VIDEO_MODE_FORMAT7_MIN DC1394_VIDEO_MODE_FORMAT7_0 0124 #define DC1394_VIDEO_MODE_FORMAT7_MAX DC1394_VIDEO_MODE_FORMAT7_7 0125 #define DC1394_VIDEO_MODE_FORMAT7_NUM (DC1394_VIDEO_MODE_FORMAT7_MAX - DC1394_VIDEO_MODE_FORMAT7_MIN + 1) 0126 0127 /** 0128 * Enumeration of colour codings. For details on the data format please read the IIDC specifications. 0129 */ 0130 typedef enum { 0131 DC1394_COLOR_CODING_MONO8 = 352, 0132 DC1394_COLOR_CODING_YUV411, 0133 DC1394_COLOR_CODING_YUV422, 0134 DC1394_COLOR_CODING_YUV444, 0135 DC1394_COLOR_CODING_RGB8, 0136 DC1394_COLOR_CODING_MONO16, 0137 DC1394_COLOR_CODING_RGB16, 0138 DC1394_COLOR_CODING_MONO16S, 0139 DC1394_COLOR_CODING_RGB16S, 0140 DC1394_COLOR_CODING_RAW8, 0141 DC1394_COLOR_CODING_RAW16, 0142 } dc1394color_coding_t; 0143 #define DC1394_COLOR_CODING_MIN DC1394_COLOR_CODING_MONO8 0144 #define DC1394_COLOR_CODING_MAX DC1394_COLOR_CODING_RAW16 0145 #define DC1394_COLOR_CODING_NUM (DC1394_COLOR_CODING_MAX - DC1394_COLOR_CODING_MIN + 1) 0146 0147 /** 0148 * RAW sensor filters. These elementary tiles tesselate the image plane in RAW modes. RGGB should be interpreted in 2D as 0149 * 0150 * RG 0151 * GB 0152 * 0153 * and similarly for other filters. 0154 */ 0155 typedef enum { 0156 DC1394_COLOR_FILTER_RGGB = 512, 0157 DC1394_COLOR_FILTER_GBRG, 0158 DC1394_COLOR_FILTER_GRBG, 0159 DC1394_COLOR_FILTER_BGGR, 0160 } dc1394color_filter_t; 0161 #define DC1394_COLOR_FILTER_MIN DC1394_COLOR_FILTER_RGGB 0162 #define DC1394_COLOR_FILTER_MAX DC1394_COLOR_FILTER_BGGR 0163 #define DC1394_COLOR_FILTER_NUM (DC1394_COLOR_FILTER_MAX - DC1394_COLOR_FILTER_MIN + 1) 0164 0165 /** 0166 * Byte order for YUV formats (may be expanded to RGB in the future) 0167 * 0168 * IIDC cameras always return data in UYVY order, but conversion functions can change this if requested. 0169 */ 0170 typedef enum { DC1394_BYTE_ORDER_UYVY = 800, DC1394_BYTE_ORDER_YUYV, } dc1394byte_order_t; 0171 #define DC1394_BYTE_ORDER_MIN DC1394_BYTE_ORDER_UYVY 0172 #define DC1394_BYTE_ORDER_MAX DC1394_BYTE_ORDER_YUYV 0173 #define DC1394_BYTE_ORDER_NUM (DC1394_BYTE_ORDER_MAX - DC1394_BYTE_ORDER_MIN + 1) 0174 0175 /** 0176 * A struct containing a list of color codings 0177 */ 0178 typedef struct 0179 { 0180 uint32_t num; 0181 dc1394color_coding_t codings[DC1394_COLOR_CODING_NUM]; 0182 } dc1394color_codings_t; 0183 0184 /** 0185 * A struct containing a list of video modes 0186 */ 0187 typedef struct 0188 { 0189 uint32_t num; 0190 dc1394video_mode_t modes[DC1394_VIDEO_MODE_NUM]; 0191 } dc1394video_modes_t; 0192 0193 /** 0194 * Yet another boolean data type 0195 */ 0196 typedef enum { DC1394_FALSE = 0, DC1394_TRUE, } dc1394bool_t; 0197 0198 /** 0199 * Yet another boolean data type, a bit more oriented towards electrical-engineers 0200 */ 0201 typedef enum { DC1394_OFF = 0, DC1394_ON, } dc1394switch_t; 0202 0203 /** 0204 * A list of de-mosaicing techniques for Bayer-patterns. 0205 * 0206 * The speed of the techniques can vary greatly, as well as their quality. 0207 */ 0208 typedef enum { 0209 DC1394_BAYER_METHOD_NEAREST = 0, 0210 DC1394_BAYER_METHOD_SIMPLE, 0211 DC1394_BAYER_METHOD_BILINEAR, 0212 DC1394_BAYER_METHOD_HQLINEAR, 0213 DC1394_BAYER_METHOD_DOWNSAMPLE, 0214 DC1394_BAYER_METHOD_EDGESENSE, 0215 DC1394_BAYER_METHOD_VNG, 0216 DC1394_BAYER_METHOD_AHD, 0217 } dc1394bayer_method_t; 0218 #define DC1394_BAYER_METHOD_MIN DC1394_BAYER_METHOD_NEAREST 0219 #define DC1394_BAYER_METHOD_MAX DC1394_BAYER_METHOD_AHD 0220 #define DC1394_BAYER_METHOD_NUM (DC1394_BAYER_METHOD_MAX - DC1394_BAYER_METHOD_MIN + 1) 0221 0222 typedef struct 0223 { 0224 dc1394bayer_method_t method; /* Debayer method */ 0225 dc1394color_filter_t filter; /* Debayer pattern */ 0226 int offsetX, offsetY; /* Debayer offset */ 0227 } BayerParams; 0228 0229 /************************************************************************************************ 0230 * * 0231 * Color conversion functions for cameras that can output raw Bayer pattern images (color * 0232 * codings DC1394_COLOR_CODING_RAW8 and DC1394_COLOR_CODING_RAW16). * 0233 * * 0234 * Credits and sources: * 0235 * - Nearest Neighbor : OpenCV library * 0236 * - Bilinear : OpenCV library * 0237 * - HQLinear : High-Quality Linear Interpolation For Demosaicing Of Bayer-Patterned * 0238 * Color Images, by Henrique S. Malvar, Li-wei He, and Ross Cutler, * 0239 * in Proceedings of the ICASSP'04 Conference. * 0240 * - Edge Sense II : Laroche, Claude A. "Apparatus and method for adaptively interpolating * 0241 * a full color image utilizing chrominance gradients" * 0242 * U.S. Patent 5,373,322. Based on the code found on the website * 0243 * http://www-ise.stanford.edu/~tingchen/ Converted to C and adapted to * 0244 * all four elementary patterns. * 0245 * - Downsample : "Known to the Ancients" * 0246 * - Simple : Implemented from the information found in the manual of Allied Vision * 0247 * Technologies (AVT) cameras. * 0248 * - VNG : Variable Number of Gradients, a method described in * 0249 * http://www-ise.stanford.edu/~tingchen/algodep/vargra.html * 0250 * Sources import from DCRAW by Frederic Devernay. DCRAW is a RAW * 0251 * converter program by Dave Coffin. URL: * 0252 * https://www.dechifro.org/dcraw/ * 0253 * - AHD : Adaptive Homogeneity-Directed Demosaicing Algorithm, by K. Hirakawa * 0254 * and T.W. Parks, IEEE Transactions on Image Processing, Vol. 14, Nr. 3, * 0255 * March 2005, pp. 360 - 369. * 0256 * * 0257 ************************************************************************************************/ 0258 0259 #ifdef __cplusplus 0260 extern "C" { 0261 #endif 0262 0263 /** 0264 * Perform de-mosaicing on an 8-bit image buffer 0265 */ 0266 dc1394error_t dc1394_bayer_decoding_8bit(const uint8_t *bayer, uint8_t *rgb, uint32_t width, uint32_t height, 0267 dc1394color_filter_t tile, dc1394bayer_method_t method); 0268 0269 /** 0270 * Perform de-mosaicing on an 16-bit image buffer 0271 */ 0272 dc1394error_t dc1394_bayer_decoding_16bit(const uint16_t *bayer, uint16_t *rgb, uint32_t width, uint32_t height, 0273 dc1394color_filter_t tile, dc1394bayer_method_t method, uint32_t bits); 0274 0275 #ifdef __cplusplus 0276 } 0277 #endif