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 # endif
75 
76 #endif
77 
78 #include "libraw_datastream.h"
79 #include "libraw_types.h"
80 #include "libraw_const.h"
81 #include "libraw_internal.h"
82 #include "libraw_alloc.h"
83 
84 #ifdef __cplusplus
85 extern "C"
86 {
87 #endif
88   DllDef const char *libraw_strerror(int errorcode);
89   DllDef const char *libraw_strprogress(enum LibRaw_progress);
90   /* LibRaw C API */
91   DllDef libraw_data_t *libraw_init(unsigned int flags);
92   DllDef int libraw_open_file(libraw_data_t *, const char *);
93 #ifndef LIBRAW_NO_IOSTREAMS_DATASTREAM
94   DllDef int libraw_open_file_ex(libraw_data_t *, const char *,
95                                  INT64 max_buff_sz);
96 #endif
97 #if defined(_WIN32) || defined(WIN32)
98   DllDef int libraw_open_wfile(libraw_data_t *, const wchar_t *);
99 #ifndef LIBRAW_NO_IOSTREAMS_DATASTREAM
100   DllDef int libraw_open_wfile_ex(libraw_data_t *, const wchar_t *,
101                                   INT64 max_buff_sz);
102 #endif
103 #endif
104 
105   DllDef int libraw_open_buffer(libraw_data_t *, const void *buffer, size_t size);
106   DllDef int libraw_open_bayer(libraw_data_t *lr, unsigned char *data,
107                                unsigned datalen, ushort _raw_width,
108                                ushort _raw_height, ushort _left_margin,
109                                ushort _top_margin, ushort _right_margin,
110                                ushort _bottom_margin, unsigned char procflags,
111                                unsigned char bayer_battern,
112                                unsigned unused_bits, unsigned otherflags,
113                                unsigned black_level);
114   DllDef int libraw_unpack(libraw_data_t *);
115   DllDef int libraw_unpack_thumb(libraw_data_t *);
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();
124   DllDef int libraw_versionNumber();
125   /* Camera list */
126   DllDef const char **libraw_cameraList();
127   DllDef int libraw_cameraCount();
128 
129   /* helpers */
130   DllDef void libraw_set_memerror_handler(libraw_data_t *, memory_callback cb,
131                                           void *datap);
132   DllDef void libraw_set_exifparser_handler(libraw_data_t *,
133                                             exif_parser_callback cb,
134                                             void *datap);
135   DllDef void libraw_set_dataerror_handler(libraw_data_t *, data_callback func,
136                                            void *datap);
137   DllDef void libraw_set_progress_handler(libraw_data_t *, progress_callback cb,
138                                           void *datap);
139   DllDef const char *libraw_unpack_function_name(libraw_data_t *lr);
140   DllDef int libraw_get_decoder_info(libraw_data_t *lr,
141                                      libraw_decoder_info_t *d);
142   DllDef int libraw_COLOR(libraw_data_t *, int row, int col);
143   DllDef unsigned libraw_capabilities();
144 
145   /* DCRAW compatibility */
146   DllDef int libraw_adjust_sizes_info_only(libraw_data_t *);
147   DllDef int libraw_dcraw_ppm_tiff_writer(libraw_data_t *lr,
148                                           const char *filename);
149   DllDef int libraw_dcraw_thumb_writer(libraw_data_t *lr, const char *fname);
150   DllDef int libraw_dcraw_process(libraw_data_t *lr);
151   DllDef libraw_processed_image_t *
152   libraw_dcraw_make_mem_image(libraw_data_t *lr, int *errc);
153   DllDef libraw_processed_image_t *
154   libraw_dcraw_make_mem_thumb(libraw_data_t *lr, int *errc);
155   DllDef void libraw_dcraw_clear_mem(libraw_processed_image_t *);
156   /* getters/setters used by 3DLut Creator */
157   DllDef void libraw_set_demosaic(libraw_data_t *lr, int value);
158   DllDef void libraw_set_output_color(libraw_data_t *lr, int value);
159   DllDef void libraw_set_user_mul(libraw_data_t *lr, int index, float val);
160   DllDef void libraw_set_output_bps(libraw_data_t *lr, int value);
161   DllDef void libraw_set_gamma(libraw_data_t *lr, int index, float value);
162   DllDef void libraw_set_no_auto_bright(libraw_data_t *lr, int value);
163   DllDef void libraw_set_bright(libraw_data_t *lr, float value);
164   DllDef void libraw_set_highlight(libraw_data_t *lr, int value);
165   DllDef void libraw_set_fbdd_noiserd(libraw_data_t *lr, int value);
166   DllDef int libraw_get_raw_height(libraw_data_t *lr);
167   DllDef int libraw_get_raw_width(libraw_data_t *lr);
168   DllDef int libraw_get_iheight(libraw_data_t *lr);
169   DllDef int libraw_get_iwidth(libraw_data_t *lr);
170   DllDef float libraw_get_cam_mul(libraw_data_t *lr, int index);
171   DllDef float libraw_get_pre_mul(libraw_data_t *lr, int index);
172   DllDef float libraw_get_rgb_cam(libraw_data_t *lr, int index1, int index2);
173   DllDef int libraw_get_color_maximum(libraw_data_t *lr);
174   DllDef void libraw_set_output_tif(libraw_data_t *lr, int value);
175   DllDef libraw_iparams_t *libraw_get_iparams(libraw_data_t *lr);
176   DllDef libraw_lensinfo_t *libraw_get_lensinfo(libraw_data_t *lr);
177   DllDef libraw_imgother_t *libraw_get_imgother(libraw_data_t *lr);
178 
179 #ifdef __cplusplus
180 }
181 #endif
182 
183 #ifdef __cplusplus
184 
185 class DllDef LibRaw
186 {
187 public:
188   libraw_data_t imgdata;
189 
190   LibRaw(unsigned int flags = LIBRAW_OPTIONS_NONE);
output_params_ptr()191   libraw_output_params_t *output_params_ptr() { return &imgdata.params; }
192 #ifndef LIBRAW_NO_IOSTREAMS_DATASTREAM
193   int open_file(const char *fname,
194                 INT64 max_buffered_sz = LIBRAW_USE_STREAMS_DATASTREAM_MAXSIZE);
195 #if defined(_WIN32) || defined(WIN32)
196   int open_file(const wchar_t *fname,
197                 INT64 max_buffered_sz = LIBRAW_USE_STREAMS_DATASTREAM_MAXSIZE);
198 #endif
199 #else
200   int open_file(const char *fname);
201 #if defined(_WIN32) || defined(WIN32)
202   int open_file(const wchar_t *fname);
203 #endif
204 
205 #endif
206   int open_buffer(const void *buffer, size_t size);
207   virtual int open_datastream(LibRaw_abstract_datastream *);
208   virtual int open_bayer(const unsigned char *data, unsigned datalen,
209                          ushort _raw_width, ushort _raw_height,
210                          ushort _left_margin, ushort _top_margin,
211                          ushort _right_margin, ushort _bottom_margin,
212                          unsigned char procflags, unsigned char bayer_pattern,
213                          unsigned unused_bits, unsigned otherflags,
214                          unsigned black_level);
error_count()215   int error_count() { return libraw_internal_data.unpacker_data.data_error; }
216   void recycle_datastream();
217   int unpack(void);
218   int unpack_thumb(void);
219   int thumbOK(INT64 maxsz = -1);
220   int adjust_sizes_info_only(void);
221   int subtract_black();
222   int subtract_black_internal();
223   int raw2image();
224   int raw2image_ex(int do_subtract_black);
225   void raw2image_start();
226   void free_image();
227   int adjust_maximum();
228   int adjust_to_raw_inset_crop(unsigned mask, float maxcrop = 0.55f);
set_exifparser_handler(exif_parser_callback cb,void * data)229   void set_exifparser_handler(exif_parser_callback cb, void *data)
230   {
231     callbacks.exifparser_data = data;
232     callbacks.exif_cb = cb;
233   }
set_memerror_handler(memory_callback cb,void * data)234   void set_memerror_handler(memory_callback cb, void *data)
235   {
236     callbacks.memcb_data = data;
237     callbacks.mem_cb = cb;
238   }
set_dataerror_handler(data_callback func,void * data)239   void set_dataerror_handler(data_callback func, void *data)
240   {
241     callbacks.datacb_data = data;
242     callbacks.data_cb = func;
243   }
set_progress_handler(progress_callback pcb,void * data)244   void set_progress_handler(progress_callback pcb, void *data)
245   {
246     callbacks.progresscb_data = data;
247     callbacks.progress_cb = pcb;
248   }
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 */
is_fuji_rotated()268   int is_fuji_rotated()
269   {
270     return libraw_internal_data.internal_output_params.fuji_width;
271   }
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 
COLOR(int row,int col)293   int COLOR(int row, int col)
294   {
295     if (!imgdata.idata.filters)
296       return 6; /* Special value 0+1+2+3 */
297     if (imgdata.idata.filters < 1000)
298       return fcol(row, col);
299     return libraw_internal_data.internal_output_params.fuji_width
300                ? FCF(row, col)
301                : FC(row, col);
302   }
303 
FC(int row,int col)304   int FC(int row, int col)
305   {
306     return (imgdata.idata.filters >> (((row << 1 & 14) | (col & 1)) << 1) & 3);
307   }
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);
get_internal_data_pointer()312   libraw_internal_data_t *get_internal_data_pointer()
313   {
314     return &libraw_internal_data;
315   }
316 
powf_lim(float a,float b,float limup)317   static float powf_lim(float a, float b, float limup)
318   {
319     return (b > limup || b < -limup) ? 0.f : powf(a, b);
320   }
libraw_powf64l(float a,float b)321   static float libraw_powf64l(float a, float b) { return powf_lim(a, b, 64.f); }
322 
sgetn(int n,uchar * s)323   static unsigned sgetn(int n, uchar *s)
324   {
325     unsigned result = 0;
326     while (n-- > 0)
327       result = (result << 8) | (*s++);
328     return result;
329   }
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 
FCF(int row,int col)382   int FCF(int row, int col)
383   {
384     int rr, cc;
385     if (libraw_internal_data.unpacker_data.fuji_layout)
386     {
387       rr = libraw_internal_data.internal_output_params.fuji_width - 1 - col +
388            (row >> 1);
389       cc = col + ((row + 1) >> 1);
390     }
391     else
392     {
393       rr = libraw_internal_data.internal_output_params.fuji_width - 1 + row -
394            (col >> 1);
395       cc = row + ((col + 1) >> 1);
396     }
397     return FC(rr, cc);
398   }
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 merror(void *ptr, const char *where);
406   void derror();
407 
408   LibRaw_TLS *tls;
409   libraw_internal_data_t libraw_internal_data;
410   decode first_decode[2048], *second_decode, *free_decode;
411   tiff_ifd_t tiff_ifd[LIBRAW_IFD_MAXCOUNT];
412   libraw_memmgr memmgr;
413   libraw_callbacks_t callbacks;
414 
415   void (LibRaw::*write_thumb)();
416   void (LibRaw::*write_fun)();
417   void (LibRaw::*load_raw)();
418   void (LibRaw::*thumb_load_raw)();
419   void (LibRaw::*pentax_component_load_raw)();
420 
421   void kodak_thumb_loader();
422   void write_thumb_ppm_tiff(FILE *);
423 #ifdef USE_X3FTOOLS
424   void x3f_thumb_loader();
425   INT64 x3f_thumb_size();
426 #endif
427 
own_filtering_supported()428   int own_filtering_supported() { return 0; }
429   void identify();
430   void initdata();
431   unsigned parse_custom_cameras(unsigned limit, libraw_custom_camera_t table[],
432                                 char **list);
433   void write_ppm_tiff();
434   void convert_to_rgb();
435   void remove_zeroes();
436   void crop_masked_pixels();
437 #ifndef NO_LCMS
438   void apply_profile(const char *, const char *);
439 #endif
440   void pre_interpolate();
441   void border_interpolate(int border);
442   void lin_interpolate();
443   void vng_interpolate();
444   void ppg_interpolate();
445   void cielab(ushort rgb[3], short lab[3]);
446   void xtrans_interpolate(int);
447   void ahd_interpolate();
448   void dht_interpolate();
449   void aahd_interpolate();
450 
451   void dcb(int iterations, int dcb_enhance);
452   void fbdd(int noiserd);
453   void exp_bef(float expos, float preser);
454 
455   void bad_pixels(const char *);
456   void subtract(const char *);
457   void hat_transform(float *temp, float *base, int st, int size, int sc);
458   void wavelet_denoise();
459   void scale_colors();
460   void median_filter();
461   void blend_highlights();
462   void recover_highlights();
463   void green_matching();
464 
465   void stretch();
466 
467   void jpeg_thumb_writer(FILE *tfp, char *thumb, int thumb_length);
468   void jpeg_thumb();
469   void ppm_thumb();
470   void ppm16_thumb();
471   void layer_thumb();
472   void rollei_thumb();
473   void kodak_thumb_load_raw();
474 
475   unsigned get4();
476 
477   int flip_index(int row, int col);
478   void gamma_curve(double pwr, double ts, int mode, int imax);
479   void cubic_spline(const int *x_, const int *y_, const int len);
480 
481   /* RawSpeed data */
482   void *_rawspeed_camerameta;
483   void *_rawspeed_decoder;
484   void fix_after_rawspeed(int bl);
485   int try_rawspeed(); /* returns LIBRAW_SUCCESS on success */
486   /* Fast cancel flag */
487   long _exitflag;
488 
489   /* DNG SDK data */
490   void *dnghost;
491   void *dngnegative;
492   void *dngimage;
493   int valid_for_dngsdk();
494   int try_dngsdk();
495   /* X3F data */
496   void *_x3f_data; /* keep it even if USE_X3FTOOLS is not defined to do not change sizeof(LibRaw)*/
497 
raw_was_read()498   int raw_was_read()
499   {
500     return imgdata.rawdata.raw_image || imgdata.rawdata.color4_image ||
501            imgdata.rawdata.color3_image || imgdata.rawdata.float_image ||
502            imgdata.rawdata.float3_image || imgdata.rawdata.float4_image;
503   }
504 
505 #ifdef LIBRAW_LIBRARY_BUILD
506 #include "internal/libraw_internal_funcs.h"
507 #endif
508 };
509 
510 #ifdef LIBRAW_LIBRARY_BUILD
511 ushort libraw_sget2_static(short _order, uchar *s);
512 unsigned libraw_sget4_static(short _order, uchar *s);
513 int libraw_tagtype_dataunit_bytes(int tagtype);
514 double  libraw_sgetreal_static(short _order, int type, uchar *s);
515 float   libraw_int_to_float (int i);
516 #endif
517 
518 
519 #ifdef LIBRAW_LIBRARY_BUILD
520 #define RUN_CALLBACK(stage, iter, expect)                                      \
521   if (callbacks.progress_cb)                                                   \
522   {                                                                            \
523     int rr = (*callbacks.progress_cb)(callbacks.progresscb_data, stage, iter,  \
524                                       expect);                                 \
525     if (rr != 0)                                                               \
526       throw LIBRAW_EXCEPTION_CANCELLED_BY_CALLBACK;                            \
527   }
528 #endif
529 
530 #endif /* __cplusplus */
531 
532 #endif /* _LIBRAW_CLASS_H */
533