aboutsummaryrefslogtreecommitdiffstats
path: root/src/libgamma_GammaRamps.c
blob: 169af4677258dbe9c37a05c28ad972ef6ad50357 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
 * jlibgamma -- Display server abstraction layer for gamma ramp and Java
 * Copyright (C) 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "libgamma_GammaRamps.h"

#include <stdlib.h>
#include <errno.h>

#include <libgamma.h>


#ifndef __GCC__
# ifndef __attribute__
#  define __attribute__(X)  /* empty */
# endif
#endif


#define J  JNIEnv* env, jclass __attribute__((unused)) class



/**
 * Make a failure-return.
 * 
 * @param   error_code  The error code returned from the failing function or zero to read `errno`.
 * @return              The object to return.
 */
static jlongArray fail(JNIEnv* env, int error_code)
{
  jlongArray rc = (*env)->NewLongArray(env, 5);
  jlong e, z = 0;
  if ((error_code == LIBGAMMA_ERRNO_SET) || !error_code)
    error_code = errno;
  e = (jlong)error_code;
  (*env)->SetLongArrayRegion(env, rc, 0, 1, &z);
  (*env)->SetLongArrayRegion(env, rc, 1, 1, &z);
  (*env)->SetLongArrayRegion(env, rc, 2, 1, &z);
  (*env)->SetLongArrayRegion(env, rc, 3, 1, &z);
  (*env)->SetLongArrayRegion(env, rc, 4, 1, &e);
  return rc;
}


/**
 * Make a success-return.
 * 
 * @param   ramps  The native object.
 * @param   red    The red gamma ramp.
 * @param   green  The green gamma ramp.
 * @param   blue   The blue gamma ramp.
 * @return         The object to return.
 */
static jlongArray ok(JNIEnv* env, void* ramps, void* red, void* green, void* blue)
{
  jlong a = (jlong)(size_t)ramps;
  jlong b = (jlong)(size_t)red;
  jlong c = (jlong)(size_t)green;
  jlong d = (jlong)(size_t)blue, z = 0;
  jlongArray rc = (*env)->NewLongArray(env, 5);
  (*env)->SetLongArrayRegion(env, rc, 0, 1, &a);
  (*env)->SetLongArrayRegion(env, rc, 1, 1, &b);
  (*env)->SetLongArrayRegion(env, rc, 2, 1, &c);
  (*env)->SetLongArrayRegion(env, rc, 3, 1, &d);
  (*env)->SetLongArrayRegion(env, rc, 4, 1, &z);
  return rc;
}



/**
 * Create and initialise a gamma ramp in the proper way that allows all adjustment
 * methods to read from and write to it without causing segmentation violation.
 * 
 * @param   red_size    The size of the encoding axis of the red gamma ramp.
 * @param   green_size  The size of the encoding axis of the green gamma ramp.
 * @param   blue_size   The size of the encoding axis of the blue gamma ramp.
 * @return              Element 0:  The address of the native object.
 *                      Element 1:  The address of the red gamma ramp.
 *                      Element 2:  The address of the green gamma ramp.
 *                      Element 3:  The address of the blue gamma ramp.
 *                      Element 4:  Zero on success, an error code on error.
 */
jlongArray Java_libgamma_GammaRamps_libgamma_1gamma_1ramps8_1create(J, jint red_size, jint green_size, jint blue_size)
{
  libgamma_gamma_ramps8_t* ramps = malloc(sizeof(libgamma_gamma_ramps8_t));
  int r;
  if (ramps == NULL)
    return fail(env, 0);
  ramps->red_size = red_size;
  ramps->green_size = green_size;
  ramps->blue_size = blue_size;
  r = libgamma_gamma_ramps8_initialise(ramps);
  if (r != 0)
    return fail(env, r);
  return ok(env, ramps, ramps->red, ramps->green, ramps->blue);
}


/**
 * Create and initialise a gamma ramp in the proper way that allows all adjustment
 * methods to read from and write to it without causing segmentation violation.
 * 
 * @param   red_size    The size of the encoding axis of the red gamma ramp.
 * @param   green_size  The size of the encoding axis of the green gamma ramp.
 * @param   blue_size   The size of the encoding axis of the blue gamma ramp.
 * @return              Element 0:  The address of the native object.
 *                      Element 1:  The address of the red gamma ramp.
 *                      Element 2:  The address of the green gamma ramp.
 *                      Element 3:  The address of the blue gamma ramp.
 *                      Element 4:  Zero on success, an error code on error.
 */
jlongArray Java_libgamma_GammaRamps_libgamma_1gamma_1ramps16_1create(J, jint red_size, jint green_size, jint blue_size)
{
  libgamma_gamma_ramps16_t* ramps = malloc(sizeof(libgamma_gamma_ramps16_t));
  int r;
  if (ramps == NULL)
    return fail(env, 0);
  ramps->red_size = red_size;
  ramps->green_size = green_size;
  ramps->blue_size = blue_size;
  r = libgamma_gamma_ramps16_initialise(ramps);
  if (r != 0)
    return fail(env, r);
  return ok(env, ramps, ramps->red, ramps->green, ramps->blue);
}


/**
 * Create and initialise a gamma ramp in the proper way that allows all adjustment
 * methods to read from and write to it without causing segmentation violation.
 * 
 * @param   red_size    The size of the encoding axis of the red gamma ramp.
 * @param   green_size  The size of the encoding axis of the green gamma ramp.
 * @param   blue_size   The size of the encoding axis of the blue gamma ramp.
 * @return              Element 0:  The address of the native object.
 *                      Element 1:  The address of the red gamma ramp.
 *                      Element 2:  The address of the green gamma ramp.
 *                      Element 3:  The address of the blue gamma ramp.
 *                      Element 4:  Zero on success, an error code on error.
 */
jlongArray Java_libgamma_GammaRamps_libgamma_1gamma_1ramps32_1create(J, jint red_size, jint green_size, jint blue_size)
{
  libgamma_gamma_ramps32_t* ramps = malloc(sizeof(libgamma_gamma_ramps32_t));
  int r;
  if (ramps == NULL)
    return fail(env, 0);
  ramps->red_size = red_size;
  ramps->green_size = green_size;
  ramps->blue_size = blue_size;
  r = libgamma_gamma_ramps32_initialise(ramps);
  if (r != 0)
    return fail(env, r);
  return ok(env, ramps, ramps->red, ramps->green, ramps->blue);
}


/**
 * Create and initialise a gamma ramp in the proper way that allows all adjustment
 * methods to read from and write to it without causing segmentation violation.
 * 
 * @param   red_size    The size of the encoding axis of the red gamma ramp.
 * @param   green_size  The size of the encoding axis of the green gamma ramp.
 * @param   blue_size   The size of the encoding axis of the blue gamma ramp.
 * @return              Element 0:  The address of the native object.
 *                      Element 1:  The address of the red gamma ramp.
 *                      Element 2:  The address of the green gamma ramp.
 *                      Element 3:  The address of the blue gamma ramp.
 *                      Element 4:  Zero on success, an error code on error.
 */
jlongArray Java_libgamma_GammaRamps_libgamma_1gamma_1ramps64_1create(J, jint red_size, jint green_size, jint blue_size)
{
  libgamma_gamma_ramps64_t* ramps = malloc(sizeof(libgamma_gamma_ramps64_t));
  int r;
  if (ramps == NULL)
    return fail(env, 0);
  ramps->red_size = red_size;
  ramps->green_size = green_size;
  ramps->blue_size = blue_size;
  r = libgamma_gamma_ramps64_initialise(ramps);
  if (r != 0)
    return fail(env, r);
  return ok(env, ramps, ramps->red, ramps->green, ramps->blue);
}


/**
 * Create and initialise a gamma ramp in the proper way that allows all adjustment
 * methods to read from and write to it without causing segmentation violation.
 * 
 * @param   red_size    The size of the encoding axis of the red gamma ramp.
 * @param   green_size  The size of the encoding axis of the green gamma ramp.
 * @param   blue_size   The size of the encoding axis of the blue gamma ramp.
 * @return              Element 0:  The address of the native object.
 *                      Element 1:  The address of the red gamma ramp.
 *                      Element 2:  The address of the green gamma ramp.
 *                      Element 3:  The address of the blue gamma ramp.
 *                      Element 4:  Zero on success, an error code on error.
 */
jlongArray Java_libgamma_GammaRamps_libgamma_1gamma_1rampsf_1create(J, jint red_size, jint green_size, jint blue_size)
{
  libgamma_gamma_rampsf_t* ramps = malloc(sizeof(libgamma_gamma_rampsf_t));
  int r;
  if (ramps == NULL)
    return fail(env, 0);
  ramps->red_size = red_size;
  ramps->green_size = green_size;
  ramps->blue_size = blue_size;
  r = libgamma_gamma_rampsf_initialise(ramps);
  if (r != 0)
    return fail(env, r);
  return ok(env, ramps, ramps->red, ramps->green, ramps->blue);
}


/**
 * Create and initialise a gamma ramp in the proper way that allows all adjustment
 * methods to read from and write to it without causing segmentation violation.
 * 
 * @param   red_size    The size of the encoding axis of the red gamma ramp.
 * @param   green_size  The size of the encoding axis of the green gamma ramp.
 * @param   blue_size   The size of the encoding axis of the blue gamma ramp.
 * @return              Element 0:  The address of the native object.
 *                      Element 1:  The address of the red gamma ramp.
 *                      Element 2:  The address of the green gamma ramp.
 *                      Element 3:  The address of the blue gamma ramp.
 *                      Element 4:  Zero on success, an error code on error.
 */
jlongArray Java_libgamma_GammaRamps_libgamma_1gamma_1rampsd_1create(J, jint red_size, jint green_size, jint blue_size)
{
  libgamma_gamma_rampsd_t* ramps = malloc(sizeof(libgamma_gamma_rampsd_t));
  int r;
  if (ramps == NULL)
    return fail(env, 0);
  ramps->red_size = red_size;
  ramps->green_size = green_size;
  ramps->blue_size = blue_size;
  r = libgamma_gamma_rampsd_initialise(ramps);
  if (r != 0)
    return fail(env, r);
  return ok(env, ramps, ramps->red, ramps->green, ramps->blue);
}



/**
 * Release resources that are held by a gamma ramp strcuture that
 * has been allocated by {@link #libgamma_gamma_ramps8_create} or
 * otherwise initialised in the proper manner, as well as release
 * the pointer to the structure.
 * 
 * @param  address  The gamma ramps.
 */
void Java_libgamma_GammaRamps_libgamma_1gamma_1ramps8_1free(J, jlong address)
{
  void* this = (void*)(size_t)address;
  libgamma_gamma_ramps8_free(this);
  (void) env;
}


/**
 * Release resources that are held by a gamma ramp strcuture that
 * has been allocated by {@link #libgamma_gamma_ramps16_create} or
 * otherwise initialised in the proper manner, as well as release
 * the pointer to the structure.
 * 
 * @param  address  The gamma ramps.
 */
void Java_libgamma_GammaRamps_libgamma_1gamma_1ramps16_1free(J, jlong address)
{
  void* this = (void*)(size_t)address;
  libgamma_gamma_ramps16_free(this);
  (void) env;
}


/**
 * Release resources that are held by a gamma ramp strcuture that
 * has been allocated by {@link #libgamma_gamma_ramps32_create} or
 * otherwise initialised in the proper manner, as well as release
 * the pointer to the structure.
 * 
 * @param  address  The gamma ramps.
 */
void Java_libgamma_GammaRamps_libgamma_1gamma_1ramps32_1free(J, jlong address)
{
  void* this = (void*)(size_t)address;
  libgamma_gamma_ramps32_free(this);
  (void) env;
}


/**
 * Release resources that are held by a gamma ramp strcuture that
 * has been allocated by {@link #libgamma_gamma_ramps64_create} or
 * otherwise initialised in the proper manner, as well as release
 * the pointer to the structure.
 * 
 * @param  address  The gamma ramps.
 */
void Java_libgamma_GammaRamps_libgamma_1gamma_1ramps64_1free(J, jlong address)
{
  void* this = (void*)(size_t)address;
  libgamma_gamma_ramps64_free(this);
  (void) env;
}


/**
 * Release resources that are held by a gamma ramp strcuture that
 * has been allocated by {@link #libgamma_gamma_rampsf_create} or
 * otherwise initialised in the proper manner, as well as release
 * the pointer to the structure.
 * 
 * @param  address  The gamma ramps.
 */
void Java_libgamma_GammaRamps_libgamma_1gamma_1rampsf_1free(J, jlong address)
{
  void* this = (void*)(size_t)address;
  libgamma_gamma_rampsf_free(this);
  (void) env;
}


/**
 * Release resources that are held by a gamma ramp strcuture that
 * has been allocated by {@link #libgamma_gamma_rampsd_create} or
 * otherwise initialised in the proper manner, as well as release
 * the pointer to the structure.
 * 
 * @param  address  The gamma ramps.
 */
void Java_libgamma_GammaRamps_libgamma_1gamma_1rampsd_1free(J, jlong address)
{
  void* this = (void*)(size_t)address;
  libgamma_gamma_rampsd_free(this);
  (void) env;
}