bugs: propose moving gamma and black from frame to transformation.
[libale.git] / src / libale.h
blobba1f0d3b89b4b1c0c68c7f87702c3b99bd1a7228
1 /*
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)
9 * any later version.
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
14 * more details.
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/>.
20 #include <ale.h>
22 #include <stdlib.h>
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) \
34 return 0; \
35 return 1; \
36 } \
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; \
43 xarg->refcount++; \
44 return ALE_SUCCESS; \
45 } \
47 static TYPENAME TYPENAME ## _alloc() { \
48 TYPENAME retval = (TYPENAME) malloc(sizeof(struct _ ## TYPENAME)); \
49 if (retval) \
50 retval->refcount = 1; \
51 return retval; \
52 } \
54 static void TYPENAME ## _free(TYPENAME this) { \
55 DESTRUCTOR \
56 } \
58 int TYPENAME ## _release(TYPENAME xarg) { \
59 if (!TYPENAME ## _valid(xarg)) \
60 return ALE_UNSPECIFIED_FAILURE; \
61 xarg->refcount--; \
62 if (xarg->refcount == 0) {\
63 TYPENAME ## _free(xarg); \
64 free(xarg); \
65 } \
66 return ALE_SUCCESS; \
69 #define TYPE(TYPENAME, TYPE_ELEMENTS, DESTRUCTOR) \
70 struct _ ## TYPENAME { \
71 TYPE_COMMON_ELEMENTS \
72 TYPE_ELEMENTS \
73 }; \
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; \
92 return ALE_SUCCESS; \
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)
105 return 1;
106 if (type == ALE_TYPE_UINT_16)
107 return 2;
108 if (type == ALE_TYPE_FLOAT_32)
109 return 4;
110 if (type == ALE_TYPE_FLOAT_64)
111 return 8;
114 static int ale_has_channel(int row, int column, int channel, int format) {
115 if (format == ALE_BAYER_NONE)
116 return 1;
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);