Coverage Report

Created: 2025-06-22 08:04

/src/LibRaw/libraw/libraw.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- C++ -*-
2
 * File: libraw.h
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
#ifndef _LIBRAW_CLASS_H
21
#define _LIBRAW_CLASS_H
22
23
#ifdef __linux__
24
#define _FILE_OFFSET_BITS 64
25
#endif
26
27
#ifndef LIBRAW_USE_DEPRECATED_IOSTREAMS_DATASTREAM
28
#define LIBRAW_NO_IOSTREAMS_DATASTREAM
29
#endif
30
31
#ifndef LIBRAW_NO_IOSTREAMS_DATASTREAM
32
/* maximum file size to use LibRaw_file_datastream (fully buffered) I/O */
33
#define LIBRAW_USE_STREAMS_DATASTREAM_MAXSIZE (250 * 1024L * 1024L)
34
#endif
35
36
#include <limits.h>
37
#include <memory.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <math.h>
41
42
/* better WIN32 defines */
43
44
/* better WIN32 defines */
45
46
#if defined(WIN32) || defined(_WIN32)
47
48
/* Win32 API */
49
#  ifndef LIBRAW_WIN32_CALLS
50
#   define LIBRAW_WIN32_CALLS
51
#  endif
52
53
/* DLLs: Microsoft or Intel compiler */
54
# if defined(_MSC_VER) || defined(__INTEL_COMPILER)
55
# ifndef LIBRAW_WIN32_DLLDEFS
56
#  define LIBRAW_WIN32_DLLDEFS
57
# endif
58
#endif
59
60
/* wchar_t* API for std::filebuf */
61
# if (defined(_MSC_VER)  && (_MSC_VER > 1310)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 910))
62
#  ifndef LIBRAW_WIN32_UNICODEPATHS
63
#   define LIBRAW_WIN32_UNICODEPATHS
64
#  endif
65
# elif _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
66
#  ifndef LIBRAW_WIN32_UNICODEPATHS
67
#    define LIBRAW_WIN32_UNICODEPATHS
68
#  endif
69
# elif defined(_LIBCPP_HAS_OPEN_WITH_WCHAR)
70
#  ifndef LIBRAW_WIN32_UNICODEPATHS
71
#    define LIBRAW_WIN32_UNICODEPATHS
72
#  endif
73
# endif
74
75
#endif
76
77
#include "libraw_datastream.h"
78
#include "libraw_types.h"
79
#include "libraw_const.h"
80
#include "libraw_internal.h"
81
#include "libraw_alloc.h"
82
83
#ifdef __cplusplus
84
extern "C"
85
{
86
#endif
87
  DllDef const char *libraw_strerror(int errorcode);
88
  DllDef const char *libraw_strprogress(enum LibRaw_progress);
89
  /* LibRaw C API */
90
  DllDef libraw_data_t *libraw_init(unsigned int flags);
91
  DllDef int libraw_open_file(libraw_data_t *, const char *);
92
#ifndef LIBRAW_NO_IOSTREAMS_DATASTREAM
93
  DllDef int libraw_open_file_ex(libraw_data_t *, const char *,
94
                                 INT64 max_buff_sz);
95
#endif
96
#if defined(_WIN32) || defined(WIN32)
97
  DllDef int libraw_open_wfile(libraw_data_t *, const wchar_t *);
98
#ifndef LIBRAW_NO_IOSTREAMS_DATASTREAM
99
  DllDef int libraw_open_wfile_ex(libraw_data_t *, const wchar_t *,
100
                                  INT64 max_buff_sz);
101
#endif
102
#endif
103
104
  DllDef int libraw_open_buffer(libraw_data_t *, const void *buffer, size_t size);
105
  DllDef int libraw_open_bayer(libraw_data_t *lr, unsigned char *data,
106
                               unsigned datalen, ushort _raw_width,
107
                               ushort _raw_height, ushort _left_margin,
108
                               ushort _top_margin, ushort _right_margin,
109
                               ushort _bottom_margin, unsigned char procflags,
110
                               unsigned char bayer_battern,
111
                               unsigned unused_bits, unsigned otherflags,
112
                               unsigned black_level);
113
  DllDef int libraw_unpack(libraw_data_t *);
114
  DllDef int libraw_unpack_thumb(libraw_data_t *);
115
  DllDef int libraw_unpack_thumb_ex(libraw_data_t *,int);
116
  DllDef void libraw_recycle_datastream(libraw_data_t *);
117
  DllDef void libraw_recycle(libraw_data_t *);
118
  DllDef void libraw_close(libraw_data_t *);
119
  DllDef void libraw_subtract_black(libraw_data_t *);
120
  DllDef int libraw_raw2image(libraw_data_t *);
121
  DllDef void libraw_free_image(libraw_data_t *);
122
  /* version helpers */
123
  DllDef const char *libraw_version(void);
124
  DllDef int libraw_versionNumber(void);
125
  /* Camera list */
126
  DllDef const char **libraw_cameraList(void);
127
  DllDef int libraw_cameraCount(void);
128
129
  /* helpers */
130
  DllDef void libraw_set_exifparser_handler(libraw_data_t *,
131
                                            exif_parser_callback cb,
132
                                            void *datap);
133
  DllDef void libraw_set_makernotes_handler(libraw_data_t *,
134
                                            exif_parser_callback cb,
135
                                            void *datap);
136
  DllDef void libraw_set_dataerror_handler(libraw_data_t *, data_callback func,
137
                                           void *datap);
138
  DllDef void libraw_set_progress_handler(libraw_data_t *, progress_callback cb,
139
                                          void *datap);
140
  DllDef const char *libraw_unpack_function_name(libraw_data_t *lr);
141
  DllDef int libraw_get_decoder_info(libraw_data_t *lr,
142
                                     libraw_decoder_info_t *d);
143
  DllDef int libraw_COLOR(libraw_data_t *, int row, int col);
144
  DllDef unsigned libraw_capabilities(void);
145
  DllDef int libraw_adjust_to_raw_inset_crop(libraw_data_t *lr, unsigned mask, float maxcrop);
146
147
  /* DCRAW compatibility */
148
  DllDef int libraw_adjust_sizes_info_only(libraw_data_t *);
149
  DllDef int libraw_dcraw_ppm_tiff_writer(libraw_data_t *lr,
150
                                          const char *filename);
151
  DllDef int libraw_dcraw_thumb_writer(libraw_data_t *lr, const char *fname);
152
  DllDef int libraw_dcraw_process(libraw_data_t *lr);
153
  DllDef libraw_processed_image_t *
154
  libraw_dcraw_make_mem_image(libraw_data_t *lr, int *errc);
155
  DllDef libraw_processed_image_t *
156
  libraw_dcraw_make_mem_thumb(libraw_data_t *lr, int *errc);
157
  DllDef void libraw_dcraw_clear_mem(libraw_processed_image_t *);
158
  /* getters/setters used by 3DLut Creator */
159
  DllDef void libraw_set_demosaic(libraw_data_t *lr, int value);
160
  DllDef void libraw_set_output_color(libraw_data_t *lr, int value);
161
  DllDef void libraw_set_adjust_maximum_thr(libraw_data_t *lr, float value);
162
  DllDef void libraw_set_user_mul(libraw_data_t *lr, int index, float val);
163
  DllDef void libraw_set_output_bps(libraw_data_t *lr, int value);
164
  DllDef void libraw_set_gamma(libraw_data_t *lr, int index, float value);
165
  DllDef void libraw_set_no_auto_bright(libraw_data_t *lr, int value);
166
  DllDef void libraw_set_bright(libraw_data_t *lr, float value);
167
  DllDef void libraw_set_highlight(libraw_data_t *lr, int value);
168
  DllDef void libraw_set_fbdd_noiserd(libraw_data_t *lr, int value);
169
  DllDef int libraw_get_raw_height(libraw_data_t *lr);
170
  DllDef int libraw_get_raw_width(libraw_data_t *lr);
171
  DllDef int libraw_get_iheight(libraw_data_t *lr);
172
  DllDef int libraw_get_iwidth(libraw_data_t *lr);
173
  DllDef float libraw_get_cam_mul(libraw_data_t *lr, int index);
174
  DllDef float libraw_get_pre_mul(libraw_data_t *lr, int index);
175
  DllDef float libraw_get_rgb_cam(libraw_data_t *lr, int index1, int index2);
176
  DllDef int libraw_get_color_maximum(libraw_data_t *lr);
177
  DllDef void libraw_set_output_tif(libraw_data_t *lr, int value);
178
  DllDef libraw_iparams_t *libraw_get_iparams(libraw_data_t *lr);
179
  DllDef libraw_lensinfo_t *libraw_get_lensinfo(libraw_data_t *lr);
180
  DllDef libraw_imgother_t *libraw_get_imgother(libraw_data_t *lr);
181
182
#ifdef __cplusplus
183
}
184
#endif
185
186
#ifdef __cplusplus
187
188
class DllDef LibRaw
189
{
190
public:
191
  libraw_data_t imgdata;
192
193
  LibRaw(unsigned int flags = LIBRAW_OPTIONS_NONE);
194
0
  libraw_output_params_t *output_params_ptr() { return &imgdata.params; }
195
#ifndef LIBRAW_NO_IOSTREAMS_DATASTREAM
196
  int open_file(const char *fname,
197
                INT64 max_buffered_sz = LIBRAW_USE_STREAMS_DATASTREAM_MAXSIZE);
198
#if defined(_WIN32) || defined(WIN32)
199
  int open_file(const wchar_t *fname,
200
                INT64 max_buffered_sz = LIBRAW_USE_STREAMS_DATASTREAM_MAXSIZE);
201
#endif
202
#else
203
  int open_file(const char *fname);
204
#if defined(_WIN32) || defined(WIN32)
205
  int open_file(const wchar_t *fname);
206
#endif
207
208
#endif
209
  int open_buffer(const void *buffer, size_t size);
210
  virtual int open_datastream(LibRaw_abstract_datastream *);
211
  virtual int open_bayer(const unsigned char *data, unsigned datalen,
212
                         ushort _raw_width, ushort _raw_height,
213
                         ushort _left_margin, ushort _top_margin,
214
                         ushort _right_margin, ushort _bottom_margin,
215
                         unsigned char procflags, unsigned char bayer_pattern,
216
                         unsigned unused_bits, unsigned otherflags,
217
                         unsigned black_level);
218
0
  int error_count() { return libraw_internal_data.unpacker_data.data_error; }
219
  void recycle_datastream();
220
  int unpack(void);
221
  int unpack_thumb(void);
222
  int unpack_thumb_ex(int);
223
  int thumbOK(INT64 maxsz = -1);
224
  int adjust_sizes_info_only(void);
225
  int subtract_black();
226
  int subtract_black_internal();
227
  int raw2image();
228
  int raw2image_ex(int do_subtract_black);
229
  void raw2image_start();
230
  void free_image();
231
  int adjust_maximum();
232
  int adjust_to_raw_inset_crop(unsigned mask, float maxcrop = 0.55f); 
233
  void set_exifparser_handler(exif_parser_callback cb, void *data)
234
0
  {
235
0
    callbacks.exifparser_data = data;
236
0
    callbacks.exif_cb = cb;
237
0
  }
238
  void set_makernotes_handler(exif_parser_callback cb, void *data)
239
0
  {
240
0
    callbacks.makernotesparser_data = data;
241
0
    callbacks.makernotes_cb = cb;
242
0
  }
243
  void set_dataerror_handler(data_callback func, void *data)
244
0
  {
245
0
    callbacks.datacb_data = data;
246
0
    callbacks.data_cb = func;
247
0
  }
248
  void set_progress_handler(progress_callback pcb, void *data)
249
0
  {
250
0
    callbacks.progresscb_data = data;
251
0
    callbacks.progress_cb = pcb;
252
0
  }
253
254
  static const char* cameramakeridx2maker(unsigned maker);
255
  int setMakeFromIndex(unsigned index);
256
257
  void convertFloatToInt(float dmin = 4096.f, float dmax = 32767.f,
258
                         float dtarget = 16383.f);
259
260
  /* Make/Model simplification */
261
  static int simplify_make_model(unsigned *_maker_index, char *_make, unsigned _make_buf_size, char *_model, unsigned _model_buf_size);
262
263
  /* helpers */
264
  static unsigned capabilities();
265
  static const char *version();
266
  static int versionNumber();
267
  static const char **cameraList();
268
  static int cameraCount();
269
  static const char *strprogress(enum LibRaw_progress);
270
  static const char *strerror(int p);
271
  /* dcraw emulation */
272
  int dcraw_ppm_tiff_writer(const char *filename);
273
  int dcraw_thumb_writer(const char *fname);
274
  int dcraw_process(void);
275
  /* information calls */
276
  int is_fuji_rotated()
277
0
  {
278
0
    return libraw_internal_data.internal_output_params.fuji_width;
279
0
  }
280
  int is_sraw();
281
  int sraw_midpoint();
282
  int is_nikon_sraw();
283
  int is_coolscan_nef();
284
  int is_jpeg_thumb();
285
  int is_floating_point();
286
  int have_fpdata();
287
  /* memory writers */
288
  virtual libraw_processed_image_t *dcraw_make_mem_image(int *errcode = NULL);
289
  virtual libraw_processed_image_t *dcraw_make_mem_thumb(int *errcode = NULL);
290
  static void dcraw_clear_mem(libraw_processed_image_t *);
291
292
  /* Additional calls for make_mem_image */
293
  void get_mem_image_format(int *width, int *height, int *colors,
294
                            int *bps) const;
295
  int copy_mem_image(void *scan0, int stride, int bgr);
296
297
  /* free all internal data structures */
298
  void recycle();
299
  virtual ~LibRaw(void);
300
301
  int COLOR(int row, int col)
302
0
  {
303
0
    if (!imgdata.idata.filters)
304
0
      return 6; /* Special value 0+1+2+3 */
305
0
    if (imgdata.idata.filters < 1000)
306
0
      return fcol(row, col);
307
0
    return libraw_internal_data.internal_output_params.fuji_width
308
0
               ? FCF(row, col)
309
0
               : FC(row, col);
310
0
  }
311
312
  int FC(int row, int col)
313
4.34G
  {
314
4.34G
    return (imgdata.idata.filters >> (((row << 1 & 14) | (col & 1)) << 1) & 3);
315
4.34G
  }
316
  int fcol(int row, int col);
317
318
  const char *unpack_function_name();
319
  virtual int get_decoder_info(libraw_decoder_info_t *d_info);
320
  libraw_internal_data_t *get_internal_data_pointer()
321
0
  {
322
0
    return &libraw_internal_data;
323
0
  }
324
325
  static float powf_lim(float a, float b, float limup)
326
443k
  {
327
443k
    return (b > limup || b < -limup) ? 0.f : powf(a, b);
328
443k
  }
329
443k
  static float libraw_powf64l(float a, float b) { return powf_lim(a, b, 64.f); }
330
331
  static unsigned sgetn(int n, uchar *s)
332
7.75k
  {
333
7.75k
    unsigned result = 0;
334
24.3k
    while (n-- > 0)
335
16.6k
      result = (result << 8) | (*s++);
336
7.75k
    return result;
337
7.75k
  }
338
339
  /* Phase one correction/subtractBL calls */
340
  /* Returns libraw error code */
341
342
  int phase_one_subtract_black(ushort *src, ushort *dest);
343
  int phase_one_correct();
344
345
  int set_rawspeed_camerafile(char *filename);
346
  virtual void setCancelFlag();
347
  virtual void clearCancelFlag();
348
  virtual int adobe_coeff(unsigned, const char *, int internal_only = 0);
349
350
  void set_dng_host(void *);
351
352
protected:
353
  static void *memmem(char *haystack, size_t haystacklen, char *needle,
354
                      size_t needlelen);
355
  static char *strcasestr(char *h, const char *n);
356
  static size_t strnlen(const char *s, size_t n);
357
358
#ifdef LIBRAW_NO_IOSTREAMS_DATASTREAM
359
  int libraw_openfile_tail(LibRaw_abstract_datastream *stream);
360
#endif
361
362
  int is_curve_linear();
363
  void checkCancel();
364
  void cam_xyz_coeff(float _rgb_cam[3][4], double cam_xyz[4][3]);
365
  void phase_one_allocate_tempbuffer();
366
  void phase_one_free_tempbuffer();
367
  virtual int is_phaseone_compressed();
368
  virtual int is_canon_600();
369
  /* Hotspots */
370
  virtual void copy_fuji_uncropped(unsigned short cblack[4],
371
                                   unsigned short *dmaxp);
372
  virtual void copy_bayer(unsigned short cblack[4], unsigned short *dmaxp);
373
  virtual void fuji_rotate();
374
  virtual void convert_to_rgb_loop(float out_cam[3][4]);
375
  virtual void lin_interpolate_loop(int *code, int size);
376
  virtual void scale_colors_loop(float scale_mul[4]);
377
378
  /* Fujifilm compressed decoder public interface (to make parallel decoder) */
379
  virtual void
380
  fuji_decode_loop(struct fuji_compressed_params *common_info, int count,
381
                   INT64 *offsets, unsigned *sizes, uchar *q_bases);
382
  void fuji_decode_strip(struct fuji_compressed_params *info_common,
383
                         int cur_block, INT64 raw_offset, unsigned size, uchar *q_bases);
384
  /* CR3 decoder public interface to make parallel decoder */
385
  virtual void crxLoadDecodeLoop(void *, int);
386
  int crxDecodePlane(void *, uint32_t planeNumber);
387
  virtual void crxLoadFinalizeLoopE3(void *, int);
388
  void crxConvertPlaneLineDf(void *, int);
389
  /* Panasonic Compression 8 parallel decoder stubs*/
390
  virtual void pana8_decode_loop(void*);
391
  int pana8_decode_strip(void*, int); // return: 0 if OK, non-zero on error
392
  int FCF(int row, int col)
393
0
  {
394
0
    int rr, cc;
395
0
    if (libraw_internal_data.unpacker_data.fuji_layout)
396
0
    {
397
0
      rr = libraw_internal_data.internal_output_params.fuji_width - 1 - col +
398
0
           (row >> 1);
399
0
      cc = col + ((row + 1) >> 1);
400
0
    }
401
0
    else
402
0
    {
403
0
      rr = libraw_internal_data.internal_output_params.fuji_width - 1 + row -
404
0
           (col >> 1);
405
0
      cc = row + ((col + 1) >> 1);
406
0
    }
407
0
    return FC(rr, cc);
408
0
  }
409
410
  void adjust_bl();
411
  void *malloc(size_t t);
412
  void *calloc(size_t n, size_t t);
413
  void *realloc(void *p, size_t s);
414
  void free(void *p);
415
  void derror();
416
417
  LibRaw_TLS *tls;
418
  libraw_internal_data_t libraw_internal_data;
419
  decode first_decode[2048], *second_decode, *free_decode;
420
  tiff_ifd_t tiff_ifd[LIBRAW_IFD_MAXCOUNT];
421
  libraw_memmgr memmgr;
422
  libraw_callbacks_t callbacks;
423
424
  //void (LibRaw::*write_thumb)();
425
  void (LibRaw::*write_fun)();
426
  void (LibRaw::*load_raw)();
427
  //void (LibRaw::*thumb_load_raw)();
428
  void (LibRaw::*pentax_component_load_raw)();
429
430
  void write_thumb_ppm_tiff(FILE *);
431
432
0
  int own_filtering_supported() { return 0; }
433
  void identify();
434
  void initdata();
435
  unsigned parse_custom_cameras(unsigned limit, libraw_custom_camera_t table[],
436
                                char **list);
437
  void write_ppm_tiff();
438
  void convert_to_rgb();
439
  void remove_zeroes();
440
  void crop_masked_pixels();
441
#ifndef NO_LCMS
442
  void apply_profile(const char *, const char *);
443
#endif
444
  void pre_interpolate();
445
  void border_interpolate(int border);
446
  void lin_interpolate();
447
  void vng_interpolate();
448
  void ppg_interpolate();
449
  void cielab(ushort rgb[3], short lab[3]);
450
  void xtrans_interpolate(int);
451
  void ahd_interpolate();
452
  void dht_interpolate();
453
  void aahd_interpolate();
454
455
  void dcb(int iterations, int dcb_enhance);
456
  void fbdd(int noiserd);
457
  void exp_bef(float expos, float preser);
458
459
  void bad_pixels(const char *);
460
  void subtract(const char *);
461
  void hat_transform(float *temp, float *base, int st, int size, int sc);
462
  void wavelet_denoise();
463
  void scale_colors();
464
  void median_filter();
465
  void blend_highlights();
466
  void recover_highlights();
467
  void green_matching();
468
469
  void stretch();
470
471
  void jpeg_thumb_writer(FILE *tfp, char *thumb, int thumb_length);
472
#if 0
473
  void jpeg_thumb();
474
  void ppm_thumb();
475
  void ppm16_thumb();
476
  void layer_thumb();
477
  void rollei_thumb();
478
#endif
479
  void kodak_thumb_load_raw();
480
481
  unsigned get4();
482
483
  int flip_index(int row, int col);
484
  void gamma_curve(double pwr, double ts, int mode, int imax);
485
  void cubic_spline(const int *x_, const int *y_, const int len);
486
487
  /* RawSpeed data */
488
  void *_rawspeed_camerameta;
489
  void *_rawspeed_decoder;
490
  void *_rawspeed3_handle;
491
  void fix_after_rawspeed(int bl);
492
  int try_rawspeed(); /* returns LIBRAW_SUCCESS on success */
493
  /* Fast cancel flag */
494
  long _exitflag;
495
496
  /* DNG SDK data */
497
  void *dnghost;
498
  void *dngnegative;
499
  void *dngimage;
500
  int valid_for_dngsdk();
501
  int try_dngsdk();
502
  /* X3F data */
503
  void *_x3f_data; /* keep it even if USE_X3FTOOLS is not defined to do not change sizeof(LibRaw)*/
504
505
  int raw_was_read()
506
3.09k
  {
507
3.09k
    return imgdata.rawdata.raw_image || imgdata.rawdata.color4_image ||
508
3.09k
           imgdata.rawdata.color3_image || imgdata.rawdata.float_image ||
509
3.09k
           imgdata.rawdata.float3_image || imgdata.rawdata.float4_image;
510
3.09k
  }
511
512
#ifdef LIBRAW_LIBRARY_BUILD
513
#include "internal/libraw_internal_funcs.h"
514
#endif
515
};
516
517
#ifdef LIBRAW_LIBRARY_BUILD
518
ushort libraw_sget2_static(short _order, uchar *s);
519
unsigned libraw_sget4_static(short _order, uchar *s);
520
int libraw_tagtype_dataunit_bytes(int tagtype);
521
double  libraw_sgetreal_static(short _order, int type, uchar *s);
522
float   libraw_int_to_float (int i);
523
#endif
524
525
526
#ifdef LIBRAW_LIBRARY_BUILD
527
#define RUN_CALLBACK(stage, iter, expect)                                      \
528
30.3k
  if (callbacks.progress_cb)                                                   \
529
30.3k
  {                                                                            \
530
0
    int rr = (*callbacks.progress_cb)(callbacks.progresscb_data, stage, iter,  \
531
0
                                      expect);                                 \
532
0
    if (rr != 0)                                                               \
533
0
      throw LIBRAW_EXCEPTION_CANCELLED_BY_CALLBACK;                            \
534
0
  }
535
#endif
536
537
#endif /* __cplusplus */
538
539
#endif /* _LIBRAW_CLASS_H */