Coverage Report

Created: 2023-12-08 06:53

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