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/>.
26 * libale requires OpenCL
28 * http://www.khronos.org/registry/cl/
30 * We assume the following header file, according to official documentation
31 * example code. If this doesn't work, try OpenCL/cl.h (or perhaps CL/cl.h),
32 * as even Khronos does not seem to be entirely consistent in OpenCL header
36 #include <OpenCL/opencl.h>
39 * Macro for API type definition
41 * API type name selection is based roughly on the approach taken in
43 * http://www.khronos.org/registry/cl/api/1.0/cl.h
45 * *_retain() and *_release() are patterned after OpenCL reference counting.
47 * *_valid() attempts to determine validity, returning non-zero if valid.
50 #define ALE_API_TYPEDEF(TYPENAME) struct _ ## TYPENAME; typedef struct _ ## TYPENAME *TYPENAME; \
51 int TYPENAME ## _valid(TYPENAME xx_valid_argument); \
52 int TYPENAME ## _retain(TYPENAME xx_retain_argument); \
53 int TYPENAME ## _release(TYPENAME xx_release_argument);
56 * Macros for API parameter declaration.
59 #define ALE_API_PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
60 PARAMETER_TYPE OBJECT_TYPE ## _get_ ## PARAMETER_NAME (OBJECT_TYPE object);
62 #define ALE_API_PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
63 int OBJECT_TYPE ## _set_ ## PARAMETER_NAME (OBJECT_TYPE object, PARAMETER_TYPE value);
65 #define ALE_API_PARAMETER_RW(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
66 ALE_API_PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
67 ALE_API_PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE)
73 ALE_API_TYPEDEF(ale_context
)
75 ALE_API_TYPEDEF(ale_image
)
76 ALE_API_TYPEDEF(ale_render
)
77 ALE_API_TYPEDEF(ale_trans
)
78 ALE_API_TYPEDEF(ale_align_properties
)
80 ALE_API_TYPEDEF(ale_filter
)
81 ALE_API_TYPEDEF(ale_psf
)
83 ALE_API_TYPEDEF(ale_exclusion_list
)
85 ALE_API_TYPEDEF(ale_sequence
)
87 ALE_API_TYPEDEF(ale_scene
)
91 * Bayer patterns. (Values are based on original ALE d2::image code, but using
92 * standard clockwise ordering rather than ALE counterclockwise ordering.)
97 ALE_BAYER_RGBG
= 0x4, /* binary 100 */
98 ALE_BAYER_GRGB
= 0x5, /* binary 101 */
99 ALE_BAYER_GBGR
= 0x6, /* binary 110 */
100 ALE_BAYER_BGRG
= 0x7 /* binary 111 */
104 * Bayer utility functions. (Based on original ALE d2::image_bayer_ale_real
109 * Return the color of a given pixel, for a given bayer pattern.
111 static unsigned int ale_bayer_color(unsigned int i
, unsigned int j
, unsigned int bayer
) {
112 unsigned int r_x_offset
= bayer
& 0x1;
113 unsigned int r_y_offset
= (bayer
& 0x2) >> 1;
115 return (i
+ r_y_offset
) % 2 + (j
+ r_x_offset
) % 2;
119 * Return a vector indicating set channels.
121 static char ale_get_channels(unsigned int i
, unsigned int j
, unsigned int bayer
) {
122 if (bayer
== ALE_BAYER_NONE
)
124 return (1 << ale_bayer_color(i
, j
, bayer
));
128 * Indicate whether a given channel exists.
130 static int ale_has_channel(unsigned int i
, unsigned int j
, unsigned int k
, unsigned int bayer
) {
131 return (1 << k
) & ale_get_channels(i
, j
, bayer
);
143 ALE_IMAGE_WEIGHTED_RGB
162 * For ALE avgf, use MEAN|FIRST with saturation threshold;
163 * other invariants are analogous; e.g.,
169 * Not all expressible combinations are accepted, or, indeed, meaningful.
173 ALE_INVARIANT_NONE
= 0,
174 ALE_INVARIANT_MEAN
= 1,
175 ALE_INVARIANT_FIRST
= 2,
176 ALE_INVARIANT_LAST
= 4,
177 ALE_INVARIANT_MAX
= 8,
178 ALE_INVARIANT_MIN
= 16,
179 ALE_INVARIANT_MEDIAN
= 32
183 * Status return values.
185 * Based loosely on OpenCL status return values.
189 ALE_UNSPECIFIED_FAILURE
= -1,
194 * Image buffer types.
204 * Obtain a Libale context given an OpenCL context
207 ale_context
ale_new_context(cl_context cc
);
210 * Retain an OpenCL context from a Libale context.
213 cl_context
ale_context_retain_cl(ale_context ac
);
216 * Set a context property.
219 int ale_context_set_property(ale_context ac
, const char *name
, const char *value
);
222 * Obtain a Libale transformation
225 ale_trans
ale_new_trans(ale_context ac
, ale_image ai
);
228 * Transformation exposure parameter.
231 ALE_API_PARAMETER_RW(ale_trans
, exposure
, double)
234 * Transformation gamma parameter. (Default is 0.45.)
237 ALE_API_PARAMETER_RW(ale_trans
, gamma
, double)
240 * Transformation black level parameter, expressed as a fraction of saturation.
243 ALE_API_PARAMETER_RW(ale_trans
, black
, double)
246 * Create a Libale image.
249 ale_image
ale_new_image(ale_context ac
, int format
, int type
);
252 * Set a Libale image to a given OpenCL buffer, host buffer, or file buffer.
255 int ale_image_set_cl(ale_image ai
, size_t width
, size_t height
, cl_mem buffer
);
256 int ale_image_set_host_dynamic(ale_image ai
, size_t width
, size_t height
, void *buffer
);
257 int ale_image_set_host_static(ale_image ai
, size_t width
, size_t height
, void *buffer
, void (*release_callback
)(void *), void *callback_data
);
258 int ale_image_set_file_dynamic(ale_image ai
, size_t width
, size_t height
, FILE *buffer
);
259 int ale_image_set_file_static(ale_image ai
, size_t width
, size_t height
, FILE *buffer
, long offset
, int (*release_callback
)(void *), void *callback_data
);
262 * Buffer type for a given Libale image.
265 ALE_API_PARAMETER_R(ale_image
, buffer_type
, int)
268 * Retain an OpenCL buffer, host buffer, or file buffer, given a Libale image.
269 * (returns NULL or ((cl_mem) 0) on failure).
272 cl_mem
ale_image_retain_cl(ale_image ai
);
273 void *ale_image_retain_host(ale_image ai
);
274 FILE *ale_image_retain_file(ale_image ai
);
277 * Release data without internal reference counters.
280 int ale_image_release_host(ale_image ai
, void *hi
);
281 int ale_image_release_file(ale_image ai
, FILE *fi
);
284 * Resize a Libale image.
287 int ale_resize_image(ale_image ai
, int x_offset
, int y_offset
, size_t width
, size_t height
);
290 * Get image statistics.
293 ALE_API_PARAMETER_R(ale_image
, height
, size_t)
294 ALE_API_PARAMETER_R(ale_image
, width
, size_t)
295 ALE_API_PARAMETER_R(ale_image
, format
, int)
296 ALE_API_PARAMETER_R(ale_image
, type
, int)
299 * Create a Libale incremental renderer, with given invariant.
302 ale_render
ale_new_render_incremental(ale_context ac
, int type
, int invariant
);
305 * Libale renderer definition threshold parameter
308 ALE_API_PARAMETER_RW(ale_render
, definition_threshold
, double)
311 * Libale renderer saturation threshold parameter
314 ALE_API_PARAMETER_RW(ale_render
, saturation_threshold
, double)
317 * Libale renderer maximum frame range parameter
320 ALE_API_PARAMETER_RW(ale_render
, range_threshold
, double)
326 int ale_render_resize(ale_render ar
, ale_trans at
);
332 int ale_render_merge(ale_render ar
, ale_trans at
, ale_image ai
);
335 * Render retain image.
338 ale_image
ale_render_retain_image(ale_render ar
);
341 * Specify a synchronization sequence for a renderer.
344 ALE_API_PARAMETER_RW(ale_render
, sync
, ale_sequence
)
347 * Synchronized renderer resize.
350 int ale_render_resize_sync(ale_render ar
, int i
);
353 * Synchronized renderer merge.
356 int ale_render_merge_sync(ale_render ar
, int i
);
359 * Create a Libale Irani-Peleg renderer, with given reference.
362 ale_render
ale_new_render_ip(ale_context ac
, ale_render reference
);
365 * Create a renderer ensemble.
368 ale_render
ale_new_render_ensemble(ale_context ac
);
371 * Retain a renderer from an ensemble.
374 ale_render
ale_render_ensemble_retain(ale_render ar
, const char *spec
);
377 * Perform a stream processing (e.g., Irani-Peleg) cycle.
380 int ale_render_stream_process(ale_render ar
);
386 ale_filter
ale_new_filter(ale_context ac
, const char *type
);
389 * Create a point-spread function.
392 ale_psf
ale_new_psf(ale_context ac
, const char *type
);
395 * Create an exclusion region list.
398 ale_exclusion_list
ale_new_exclusion_list(ale_context ac
);
401 * Append an exclusion region to a list
404 int ale_exclusion_list_append(ale_exclusion_list ae
, int is_frame_coords
, double xmin
, double xmax
, double ymin
, double ymax
, double fmin
, double fmax
);
407 * Obtain an alignment RESULT between frame A and current approximation B,
408 * starting from transformation START, and given perturbation limits
412 int ale_align(ale_image a
, ale_image b
, ale_trans start
,
413 ale_align_properties align_properties
, ale_trans result
);
417 * Return a PSF error based on a captured sequence and a scene approximation.
420 double ale_psf_error(ale_image approx
, ale_sequence sequence
);