bugs: Don't require client knowledge of flattened representations.
[libale.git] / client / beer.c
blobf8698e5634a26ca053510bd78e79f06c550b1859
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/>.
21 * beer: a simple client program for libale.
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <assert.h>
29 #include <ale.h>
32 * beer_info_printf: Display informational message for format C.
35 static void beer_info_printf(const char *c, ...) {
36 va_list ap;
38 fprintf(stderr, "beer: ");
40 va_start(ap, c);
41 vfprintf(stderr, c, ap);
42 va_end(ap);
44 fprintf(stderr, "\n");
48 * beer_error_printf: Display error message for format C.
51 static void beer_error_printf(const char *c, ...) {
52 va_list ap;
54 fprintf(stderr, "beer: ");
56 va_start(ap, c);
57 vfprintf(stderr, c, ap);
58 va_end(ap);
60 fprintf(stderr, "\n");
61 fprintf(stderr, "beer: Exiting.\n");
62 exit(1);
66 * main: Main routine (entry point).
69 int main(int argc, const char *argv[]) {
72 * Obtain OpenCL context CC.
75 cl_context cc = clCreateContextFromType(0, CL_DEVICE_TYPE_GPU, NULL, NULL, NULL);
77 if (cc == ((cl_context) 0)) {
78 beer_error_printf("Failed to create an OpenCL context.");
82 * Obtain ALE context AC.
85 ale_context ac = ale_new_context(cc);
87 if (ac == NULL) {
88 beer_error_printf("Failed to create an ALE context.");
92 * Create sample input frame AI from trivial image array DOMAIN_DATA.
95 unsigned char domain_data[27] =
96 { 0, 0, 0, 0, 0, 0, 0, 0, 0,
97 0, 0, 0, 1, 1, 1, 0, 0, 0,
98 0, 0, 0, 0, 0, 0, 0, 0, 0 };
100 ale_image ai = ale_new_image(ac, ALE_IMAGE_RGB, ALE_TYPE_UINT_8);
102 ale_image_set_host_static(ai, 3, 3, domain_data, NULL, NULL);
105 * Create incremental renderer AR.
108 ale_render ar = ale_new_render_incremental(ac, ALE_TYPE_FLOAT_32, ALE_INVARIANT_MEAN);
111 * Create identity transformation AT with bounds AI.
114 ale_trans at = ale_new_trans(ac, ai);
117 * Resize render area AR to bounds AT.
120 ale_render_resize(ar, at);
123 * Merge AI into AR with transformation AT.
126 ale_render_merge(ar, at, ai);
129 * Retain rendered host image DOMAIN_DATA2 from AR.
132 ale_image ari = ale_render_retain_image(ar);
134 float *domain_data2 = ale_image_retain_host(ari);
137 * Display results as text.
140 int di;
142 printf("Input data:");
143 for (di = 0; di < 27; di++) {
144 unsigned int i = domain_data[di];
145 printf(" %u", i);
147 printf("\n");
149 printf("Output data:");
150 for (di = 0; di < 27; di++) {
151 float f = domain_data2[di];
152 printf(" %f", f);
154 printf("\n");
157 * Release dynamic memory.
160 ale_image_release_host(ari, domain_data2);
161 ale_image_release(ai);
162 ale_image_release(ari);
163 ale_trans_release(at);
164 ale_render_release(ar);
165 ale_context_release(ac);
168 * Return success (no error).
171 return 0;