/src/libraw/src/libraw_c_api.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- C++ -*- |
2 | | * File: libraw_c_api.cpp |
3 | | * Copyright 2008-2024 LibRaw LLC (info@libraw.org) |
4 | | * Created: Sat Mar 8 , 2008 |
5 | | * |
6 | | * LibRaw C interface |
7 | | |
8 | | |
9 | | LibRaw is free software; you can redistribute it and/or modify |
10 | | it under the terms of the one of two licenses as you choose: |
11 | | |
12 | | 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1 |
13 | | (See file LICENSE.LGPL provided in LibRaw distribution archive for details). |
14 | | |
15 | | 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 |
16 | | (See file LICENSE.CDDL provided in LibRaw distribution archive for details). |
17 | | |
18 | | */ |
19 | | |
20 | | #include <math.h> |
21 | | #include <errno.h> |
22 | | #include "libraw/libraw.h" |
23 | | |
24 | | #ifdef __cplusplus |
25 | | #include <new> |
26 | | extern "C" |
27 | | { |
28 | | #endif |
29 | | |
30 | | libraw_data_t *libraw_init(unsigned int flags) |
31 | 43.3k | { |
32 | 43.3k | LibRaw *ret; |
33 | 43.3k | try |
34 | 43.3k | { |
35 | 43.3k | ret = new LibRaw(flags); |
36 | 43.3k | } |
37 | 43.3k | catch (const std::bad_alloc& ) |
38 | 43.3k | { |
39 | 0 | return NULL; |
40 | 0 | } |
41 | 43.3k | return &(ret->imgdata); |
42 | 43.3k | } |
43 | | |
44 | 0 | unsigned libraw_capabilities() { return LibRaw::capabilities(); } |
45 | 10 | const char *libraw_version() { return LibRaw::version(); } |
46 | | const char *libraw_strprogress(enum LibRaw_progress p) |
47 | 0 | { |
48 | 0 | return LibRaw::strprogress(p); |
49 | 0 | } |
50 | 0 | int libraw_versionNumber() { return LibRaw::versionNumber(); } |
51 | 0 | const char **libraw_cameraList() { return LibRaw::cameraList(); } |
52 | 0 | int libraw_cameraCount() { return LibRaw::cameraCount(); } |
53 | | const char *libraw_unpack_function_name(libraw_data_t *lr) |
54 | 0 | { |
55 | 0 | if (!lr) |
56 | 0 | return "NULL parameter passed"; |
57 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
58 | 0 | return ip->unpack_function_name(); |
59 | 0 | } |
60 | | |
61 | | void libraw_subtract_black(libraw_data_t *lr) |
62 | 0 | { |
63 | 0 | if (!lr) |
64 | 0 | return; |
65 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
66 | 0 | ip->subtract_black(); |
67 | 0 | } |
68 | | |
69 | | int libraw_open_file(libraw_data_t *lr, const char *file) |
70 | 43.3k | { |
71 | 43.3k | if (!lr) |
72 | 0 | return EINVAL; |
73 | 43.3k | LibRaw *ip = (LibRaw *)lr->parent_class; |
74 | 43.3k | return ip->open_file(file); |
75 | 43.3k | } |
76 | | |
77 | | libraw_iparams_t *libraw_get_iparams(libraw_data_t *lr) |
78 | 0 | { |
79 | 0 | if (!lr) |
80 | 0 | return NULL; |
81 | 0 | return &(lr->idata); |
82 | 0 | } |
83 | | |
84 | | libraw_lensinfo_t *libraw_get_lensinfo(libraw_data_t *lr) |
85 | 0 | { |
86 | 0 | if (!lr) |
87 | 0 | return NULL; |
88 | 0 | return &(lr->lens); |
89 | 0 | } |
90 | | |
91 | | libraw_imgother_t *libraw_get_imgother(libraw_data_t *lr) |
92 | 0 | { |
93 | 0 | if (!lr) |
94 | 0 | return NULL; |
95 | 0 | return &(lr->other); |
96 | 0 | } |
97 | | |
98 | | #ifndef LIBRAW_NO_IOSTREAMS_DATASTREAM |
99 | | int libraw_open_file_ex(libraw_data_t *lr, const char *file, INT64 sz) |
100 | | { |
101 | | if (!lr) |
102 | | return EINVAL; |
103 | | LibRaw *ip = (LibRaw *)lr->parent_class; |
104 | | return ip->open_file(file, sz); |
105 | | } |
106 | | #endif |
107 | | |
108 | | #ifdef LIBRAW_WIN32_UNICODEPATHS |
109 | | int libraw_open_wfile(libraw_data_t *lr, const wchar_t *file) |
110 | | { |
111 | | if (!lr) |
112 | | return EINVAL; |
113 | | LibRaw *ip = (LibRaw *)lr->parent_class; |
114 | | return ip->open_file(file); |
115 | | } |
116 | | |
117 | | #ifndef LIBRAW_NO_IOSTREAMS_DATASTREAM |
118 | | int libraw_open_wfile_ex(libraw_data_t *lr, const wchar_t *file, INT64 sz) |
119 | | { |
120 | | if (!lr) |
121 | | return EINVAL; |
122 | | LibRaw *ip = (LibRaw *)lr->parent_class; |
123 | | return ip->open_file(file, sz); |
124 | | } |
125 | | #endif |
126 | | #endif |
127 | | int libraw_open_buffer(libraw_data_t *lr, const void *buffer, size_t size) |
128 | 0 | { |
129 | 0 | if (!lr) |
130 | 0 | return EINVAL; |
131 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
132 | 0 | return ip->open_buffer(buffer, size); |
133 | 0 | } |
134 | | int libraw_open_bayer(libraw_data_t *lr, unsigned char *data, |
135 | | unsigned datalen, ushort _raw_width, ushort _raw_height, |
136 | | ushort _left_margin, ushort _top_margin, |
137 | | ushort _right_margin, ushort _bottom_margin, |
138 | | unsigned char procflags, unsigned char bayer_pattern, |
139 | | unsigned unused_bits, unsigned otherflags, |
140 | | unsigned black_level) |
141 | 0 | { |
142 | 0 | if (!lr) |
143 | 0 | return EINVAL; |
144 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
145 | 0 | return ip->open_bayer(data, datalen, _raw_width, _raw_height, _left_margin, |
146 | 0 | _top_margin, _right_margin, _bottom_margin, procflags, |
147 | 0 | bayer_pattern, unused_bits, otherflags, black_level); |
148 | 0 | } |
149 | | int libraw_unpack(libraw_data_t *lr) |
150 | 4.94k | { |
151 | 4.94k | if (!lr) |
152 | 0 | return EINVAL; |
153 | 4.94k | LibRaw *ip = (LibRaw *)lr->parent_class; |
154 | 4.94k | return ip->unpack(); |
155 | 4.94k | } |
156 | | int libraw_unpack_thumb(libraw_data_t *lr) |
157 | 0 | { |
158 | 0 | if (!lr) |
159 | 0 | return EINVAL; |
160 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
161 | 0 | return ip->unpack_thumb(); |
162 | 0 | } |
163 | | int libraw_unpack_thumb_ex(libraw_data_t *lr, int i) |
164 | 0 | { |
165 | 0 | if (!lr) |
166 | 0 | return EINVAL; |
167 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
168 | 0 | return ip->unpack_thumb_ex(i); |
169 | 0 | } |
170 | | void libraw_recycle_datastream(libraw_data_t *lr) |
171 | 0 | { |
172 | 0 | if (!lr) |
173 | 0 | return; |
174 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
175 | 0 | ip->recycle_datastream(); |
176 | 0 | } |
177 | | void libraw_recycle(libraw_data_t *lr) |
178 | 0 | { |
179 | 0 | if (!lr) |
180 | 0 | return; |
181 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
182 | 0 | ip->recycle(); |
183 | 0 | } |
184 | | void libraw_close(libraw_data_t *lr) |
185 | 43.3k | { |
186 | 43.3k | if (!lr) |
187 | 0 | return; |
188 | 43.3k | LibRaw *ip = (LibRaw *)lr->parent_class; |
189 | 43.3k | delete ip; |
190 | 43.3k | } |
191 | | |
192 | | void libraw_set_exifparser_handler(libraw_data_t *lr, exif_parser_callback cb, |
193 | | void *data) |
194 | 0 | { |
195 | 0 | if (!lr) |
196 | 0 | return; |
197 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
198 | 0 | ip->set_exifparser_handler(cb, data); |
199 | 0 | } |
200 | | |
201 | | void libraw_set_makernotes_handler(libraw_data_t *lr, exif_parser_callback cb, |
202 | | void *data) |
203 | 0 | { |
204 | 0 | if (!lr) |
205 | 0 | return; |
206 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
207 | 0 | ip->set_makernotes_handler(cb, data); |
208 | 0 | } |
209 | | |
210 | | void libraw_set_dataerror_handler(libraw_data_t *lr, data_callback func, |
211 | | void *data) |
212 | 43.3k | { |
213 | 43.3k | if (!lr) |
214 | 0 | return; |
215 | 43.3k | LibRaw *ip = (LibRaw *)lr->parent_class; |
216 | 43.3k | ip->set_dataerror_handler(func, data); |
217 | 43.3k | } |
218 | | void libraw_set_progress_handler(libraw_data_t *lr, progress_callback cb, |
219 | | void *data) |
220 | 0 | { |
221 | 0 | if (!lr) |
222 | 0 | return; |
223 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
224 | 0 | ip->set_progress_handler(cb, data); |
225 | 0 | } |
226 | | |
227 | | int libraw_adjust_to_raw_inset_crop(libraw_data_t *lr, unsigned mask, float maxcrop) |
228 | 0 | { |
229 | 0 | if (!lr) |
230 | 0 | return EINVAL; |
231 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
232 | 0 | return ip->adjust_to_raw_inset_crop(mask,maxcrop); |
233 | 0 | } |
234 | | |
235 | | // DCRAW |
236 | | int libraw_adjust_sizes_info_only(libraw_data_t *lr) |
237 | 0 | { |
238 | 0 | if (!lr) |
239 | 0 | return EINVAL; |
240 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
241 | 0 | return ip->adjust_sizes_info_only(); |
242 | 0 | } |
243 | | int libraw_dcraw_ppm_tiff_writer(libraw_data_t *lr, const char *filename) |
244 | 0 | { |
245 | 0 | if (!lr) |
246 | 0 | return EINVAL; |
247 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
248 | 0 | return ip->dcraw_ppm_tiff_writer(filename); |
249 | 0 | } |
250 | | int libraw_dcraw_thumb_writer(libraw_data_t *lr, const char *fname) |
251 | 0 | { |
252 | 0 | if (!lr) |
253 | 0 | return EINVAL; |
254 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
255 | 0 | return ip->dcraw_thumb_writer(fname); |
256 | 0 | } |
257 | | int libraw_dcraw_process(libraw_data_t *lr) |
258 | 2.77k | { |
259 | 2.77k | if (!lr) |
260 | 0 | return EINVAL; |
261 | 2.77k | LibRaw *ip = (LibRaw *)lr->parent_class; |
262 | 2.77k | return ip->dcraw_process(); |
263 | 2.77k | } |
264 | | libraw_processed_image_t *libraw_dcraw_make_mem_image(libraw_data_t *lr, |
265 | | int *errc) |
266 | 2.71k | { |
267 | 2.71k | if (!lr) |
268 | 0 | { |
269 | 0 | if (errc) |
270 | 0 | *errc = EINVAL; |
271 | 0 | return NULL; |
272 | 0 | } |
273 | 2.71k | LibRaw *ip = (LibRaw *)lr->parent_class; |
274 | 2.71k | return ip->dcraw_make_mem_image(errc); |
275 | 2.71k | } |
276 | | libraw_processed_image_t *libraw_dcraw_make_mem_thumb(libraw_data_t *lr, |
277 | | int *errc) |
278 | 0 | { |
279 | 0 | if (!lr) |
280 | 0 | { |
281 | 0 | if (errc) |
282 | 0 | *errc = EINVAL; |
283 | 0 | return NULL; |
284 | 0 | } |
285 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
286 | 0 | return ip->dcraw_make_mem_thumb(errc); |
287 | 0 | } |
288 | | |
289 | | void libraw_dcraw_clear_mem(libraw_processed_image_t *p) |
290 | 2.71k | { |
291 | 2.71k | LibRaw::dcraw_clear_mem(p); |
292 | 2.71k | } |
293 | | |
294 | | int libraw_raw2image(libraw_data_t *lr) |
295 | 0 | { |
296 | 0 | if (!lr) |
297 | 0 | return EINVAL; |
298 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
299 | 0 | return ip->raw2image(); |
300 | 0 | } |
301 | | void libraw_free_image(libraw_data_t *lr) |
302 | 0 | { |
303 | 0 | if (!lr) |
304 | 0 | return; |
305 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
306 | 0 | ip->free_image(); |
307 | 0 | } |
308 | | int libraw_get_decoder_info(libraw_data_t *lr, libraw_decoder_info_t *d) |
309 | 0 | { |
310 | 0 | if (!lr || !d) |
311 | 0 | return EINVAL; |
312 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
313 | 0 | return ip->get_decoder_info(d); |
314 | 0 | } |
315 | | int libraw_COLOR(libraw_data_t *lr, int row, int col) |
316 | 0 | { |
317 | 0 | if (!lr) |
318 | 0 | return EINVAL; |
319 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
320 | 0 | return ip->COLOR(row, col); |
321 | 0 | } |
322 | | |
323 | | /* getters/setters used by 3DLut Creator */ |
324 | | DllDef void libraw_set_demosaic(libraw_data_t *lr, int value) |
325 | 0 | { |
326 | 0 | if (!lr) |
327 | 0 | return; |
328 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
329 | 0 | ip->imgdata.params.user_qual = value; |
330 | 0 | } |
331 | | |
332 | | DllDef void libraw_set_output_color(libraw_data_t *lr, int value) |
333 | 0 | { |
334 | 0 | if (!lr) |
335 | 0 | return; |
336 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
337 | 0 | ip->imgdata.params.output_color = value; |
338 | 0 | } |
339 | | |
340 | | DllDef void libraw_set_adjust_maximum_thr(libraw_data_t *lr, float value) |
341 | 0 | { |
342 | 0 | if (!lr) |
343 | 0 | return; |
344 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
345 | 0 | ip->imgdata.params.adjust_maximum_thr = value; |
346 | 0 | } |
347 | | |
348 | | DllDef void libraw_set_output_bps(libraw_data_t *lr, int value) |
349 | 0 | { |
350 | 0 | if (!lr) |
351 | 0 | return; |
352 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
353 | 0 | ip->imgdata.params.output_bps = value; |
354 | 0 | } |
355 | | |
356 | | DllDef void libraw_set_output_tif(libraw_data_t *lr, int value) |
357 | 0 | { |
358 | 0 | if (!lr) |
359 | 0 | return; |
360 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
361 | 0 | ip->imgdata.params.output_tiff = value; |
362 | 0 | } |
363 | | |
364 | | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
365 | 0 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
366 | 0 | #define LIM(x, min, max) MAX(min, MIN(x, max)) |
367 | | |
368 | | DllDef void libraw_set_user_mul(libraw_data_t *lr, int index, float val) |
369 | 0 | { |
370 | 0 | if (!lr) |
371 | 0 | return; |
372 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
373 | 0 | ip->imgdata.params.user_mul[LIM(index, 0, 3)] = val; |
374 | 0 | } |
375 | | |
376 | | DllDef void libraw_set_gamma(libraw_data_t *lr, int index, float value) |
377 | 0 | { |
378 | 0 | if (!lr) |
379 | 0 | return; |
380 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
381 | 0 | ip->imgdata.params.gamm[LIM(index, 0, 5)] = value; |
382 | 0 | } |
383 | | |
384 | | DllDef void libraw_set_no_auto_bright(libraw_data_t *lr, int value) |
385 | 0 | { |
386 | 0 | if (!lr) |
387 | 0 | return; |
388 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
389 | 0 | ip->imgdata.params.no_auto_bright = value; |
390 | 0 | } |
391 | | |
392 | | DllDef void libraw_set_bright(libraw_data_t *lr, float value) |
393 | 0 | { |
394 | 0 | if (!lr) |
395 | 0 | return; |
396 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
397 | 0 | ip->imgdata.params.bright = value; |
398 | 0 | } |
399 | | |
400 | | DllDef void libraw_set_highlight(libraw_data_t *lr, int value) |
401 | 0 | { |
402 | 0 | if (!lr) |
403 | 0 | return; |
404 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
405 | 0 | ip->imgdata.params.highlight = value; |
406 | 0 | } |
407 | | |
408 | | DllDef void libraw_set_fbdd_noiserd(libraw_data_t *lr, int value) |
409 | 0 | { |
410 | 0 | if (!lr) |
411 | 0 | return; |
412 | 0 | LibRaw *ip = (LibRaw *)lr->parent_class; |
413 | 0 | ip->imgdata.params.fbdd_noiserd = value; |
414 | 0 | } |
415 | | |
416 | | DllDef int libraw_get_raw_height(libraw_data_t *lr) |
417 | 0 | { |
418 | 0 | if (!lr) |
419 | 0 | return EINVAL; |
420 | 0 | return lr->sizes.raw_height; |
421 | 0 | } |
422 | | |
423 | | DllDef int libraw_get_raw_width(libraw_data_t *lr) |
424 | 0 | { |
425 | 0 | if (!lr) |
426 | 0 | return EINVAL; |
427 | 0 | return lr->sizes.raw_width; |
428 | 0 | } |
429 | | |
430 | | DllDef int libraw_get_iheight(libraw_data_t *lr) |
431 | 0 | { |
432 | 0 | if (!lr) |
433 | 0 | return EINVAL; |
434 | 0 | return lr->sizes.iheight; |
435 | 0 | } |
436 | | |
437 | | DllDef int libraw_get_iwidth(libraw_data_t *lr) |
438 | 0 | { |
439 | 0 | if (!lr) |
440 | 0 | return EINVAL; |
441 | 0 | return lr->sizes.iwidth; |
442 | 0 | } |
443 | | |
444 | | DllDef float libraw_get_cam_mul(libraw_data_t *lr, int index) |
445 | 0 | { |
446 | 0 | if (!lr) |
447 | 0 | return EINVAL; |
448 | 0 | return lr->color.cam_mul[LIM(index, 0, 3)]; |
449 | 0 | } |
450 | | |
451 | | DllDef float libraw_get_pre_mul(libraw_data_t *lr, int index) |
452 | 0 | { |
453 | 0 | if (!lr) |
454 | 0 | return EINVAL; |
455 | 0 | return lr->color.pre_mul[LIM(index, 0, 3)]; |
456 | 0 | } |
457 | | |
458 | | DllDef float libraw_get_rgb_cam(libraw_data_t *lr, int index1, int index2) |
459 | 0 | { |
460 | 0 | if (!lr) |
461 | 0 | return EINVAL; |
462 | 0 | return lr->color.rgb_cam[LIM(index1, 0, 2)][LIM(index2, 0, 3)]; |
463 | 0 | } |
464 | | |
465 | | DllDef int libraw_get_color_maximum(libraw_data_t *lr) |
466 | 0 | { |
467 | 0 | if (!lr) |
468 | 0 | return EINVAL; |
469 | 0 | return lr->color.maximum; |
470 | 0 | } |
471 | | |
472 | | #ifdef __cplusplus |
473 | | } |
474 | | #endif |