File indexing completed on 2024-04-28 07:31:25

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