2 * Copyright 2008, 2009 David Hilvert <dhilvert@gmail.com>
4 * This file is part of libale.
6 * libale is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU Affero General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * libale is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
16 * You should have received a copy of the GNU Affero General Public License
17 * along with libale. If not, see <http://www.gnu.org/licenses/>.
25 * Macros for common API type elements and functions.
28 #define TYPE_COMMON_ELEMENTS \
29 unsigned int refcount;
31 #define TYPE_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR) \
32 int TYPENAME ## _valid(TYPENAME xarg) { \
33 if (xarg == NULL || xarg->refcount == 0) \
38 int TYPENAME ## _retain(TYPENAME xarg) { \
39 if (!TYPENAME ## _valid(xarg)) \
40 return ALE_UNSPECIFIED_FAILURE; \
41 if (!(xarg->refcount + 1 > 0)) \
42 return ALE_UNSPECIFIED_FAILURE; \
47 static TYPENAME TYPENAME ## _alloc() { \
48 TYPENAME retval = (TYPENAME) malloc(sizeof(struct _ ## TYPENAME)); \
50 retval->refcount = 1; \
54 static void TYPENAME ## _free(TYPENAME this) { \
58 int TYPENAME ## _release(TYPENAME xarg) { \
59 if (!TYPENAME ## _valid(xarg)) \
60 return ALE_UNSPECIFIED_FAILURE; \
62 if (xarg->refcount == 0) {\
63 TYPENAME ## _free(xarg); \
69 #define TYPE(TYPENAME, TYPE_ELEMENTS, DESTRUCTOR) \
70 struct _ ## TYPENAME { \
71 TYPE_COMMON_ELEMENTS \
74 TYPE_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR)
77 * Macros for API parameter operations.
80 #define PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
81 PARAMETER_TYPE OBJECT_TYPE ## _ ## PARAMETER_NAME ## _get(OBJECT_TYPE object) { \
82 if (!OBJECT_TYPE ## _valid(object)) \
83 return ((PARAMETER_TYPE) 0); \
84 return object->PARAMETER_NAME; \
87 #define PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
88 int OBJECT_TYPE ## _ ## PARAMETER_NAME ## _set(OBJECT_TYPE object, PARAMETER_TYPE value){ \
89 if (!OBJECT_TYPE ## _valid(object)) \
90 return ALE_UNSPECIFIED_FAILURE; \
91 object->PARAMETER_NAME = value; \
95 #define PARAMETER_RW(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
96 PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
97 PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE)
100 * Detemine type size.
103 static int type_size(int type
) {
104 if (type
== ALE_TYPE_UINT_8
)
106 if (type
== ALE_TYPE_UINT_16
)
108 if (type
== ALE_TYPE_FLOAT_32
)
110 if (type
== ALE_TYPE_FLOAT_64
)
114 static int ale_has_channel(int row
, int column
, int channel
, int format
) {
115 if (format
== ALE_BAYER_NONE
)
119 * Offset is (row_offset << 1) | col_offset
122 int red_offset
= (format
== ALE_BAYER_RGBG
) ? 0
123 :((format
== ALE_BAYER_GRGB
) ? 1
124 :((format
== ALE_BAYER_GBGR
) ? 2
125 :((format
== ALE_BAYER_BGRG
) ? 3 : 3)));
127 int blue_offset
= ((red_offset
& 1) << 1) | ((red_offset
& 2) >> 1);
129 if (((column
% 2) == (red_offset
& 1))
130 && ((row
% 2) == ((red_offset
& 2) >> 1)))
131 return (channel
== 0);
133 if (((column
% 2) == (blue_offset
& 1))
134 && ((row
% 2) == ((blue_offset
& 2) >> 1)))
135 return (channel
== 2);
137 return (channel
== 1);