File indexing completed on 2025-01-05 03:57:10

0001 /* -*- C++ -*-
0002  * File: libraw_c_api.cpp
0003  * Copyright 2008-2021 LibRaw LLC (info@libraw.org)
0004  * Created: Sat Mar  8 , 2008
0005  *
0006  * LibRaw C interface
0007 
0008 
0009 LibRaw is free software; you can redistribute it and/or modify
0010 it under the terms of the one of two licenses as you choose:
0011 
0012 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
0013    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
0014 
0015 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
0016    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
0017 
0018  */
0019 
0020 #include <math.h>
0021 #include <errno.h>
0022 #include "libraw/libraw.h"
0023 
0024 #ifdef __cplusplus
0025 #include <new>
0026 extern "C"
0027 {
0028 #endif
0029 
0030   libraw_data_t *libraw_init(unsigned int flags)
0031   {
0032     LibRaw *ret;
0033     try
0034     {
0035       ret = new LibRaw(flags);
0036     }
0037     catch (const std::bad_alloc& )
0038     {
0039       return NULL;
0040     }
0041     return &(ret->imgdata);
0042   }
0043 
0044   unsigned libraw_capabilities() { return LibRaw::capabilities(); }
0045   const char *libraw_version() { return LibRaw::version(); }
0046   const char *libraw_strprogress(enum LibRaw_progress p)
0047   {
0048     return LibRaw::strprogress(p);
0049   }
0050   int libraw_versionNumber() { return LibRaw::versionNumber(); }
0051   const char **libraw_cameraList() { return LibRaw::cameraList(); }
0052   int libraw_cameraCount() { return LibRaw::cameraCount(); }
0053   const char *libraw_unpack_function_name(libraw_data_t *lr)
0054   {
0055     if (!lr)
0056       return "NULL parameter passed";
0057     LibRaw *ip = (LibRaw *)lr->parent_class;
0058     return ip->unpack_function_name();
0059   }
0060 
0061   void libraw_subtract_black(libraw_data_t *lr)
0062   {
0063     if (!lr)
0064       return;
0065     LibRaw *ip = (LibRaw *)lr->parent_class;
0066     ip->subtract_black();
0067   }
0068 
0069   int libraw_open_file(libraw_data_t *lr, const char *file)
0070   {
0071     if (!lr)
0072       return EINVAL;
0073     LibRaw *ip = (LibRaw *)lr->parent_class;
0074     return ip->open_file(file);
0075   }
0076 
0077   libraw_iparams_t *libraw_get_iparams(libraw_data_t *lr)
0078   {
0079     if (!lr)
0080       return NULL;
0081     return &(lr->idata);
0082   }
0083 
0084   libraw_lensinfo_t *libraw_get_lensinfo(libraw_data_t *lr)
0085   {
0086     if (!lr)
0087       return NULL;
0088     return &(lr->lens);
0089   }
0090 
0091   libraw_imgother_t *libraw_get_imgother(libraw_data_t *lr)
0092   {
0093     if (!lr)
0094       return NULL;
0095     return &(lr->other);
0096   }
0097 
0098 #ifndef LIBRAW_NO_IOSTREAMS_DATASTREAM
0099   int libraw_open_file_ex(libraw_data_t *lr, const char *file, INT64 sz)
0100   {
0101     if (!lr)
0102       return EINVAL;
0103     LibRaw *ip = (LibRaw *)lr->parent_class;
0104     return ip->open_file(file, sz);
0105   }
0106 #endif
0107 
0108 #ifdef LIBRAW_WIN32_UNICODEPATHS
0109   int libraw_open_wfile(libraw_data_t *lr, const wchar_t *file)
0110   {
0111     if (!lr)
0112       return EINVAL;
0113     LibRaw *ip = (LibRaw *)lr->parent_class;
0114     return ip->open_file(file);
0115   }
0116 
0117 #ifndef LIBRAW_NO_IOSTREAMS_DATASTREAM
0118   int libraw_open_wfile_ex(libraw_data_t *lr, const wchar_t *file, INT64 sz)
0119   {
0120     if (!lr)
0121       return EINVAL;
0122     LibRaw *ip = (LibRaw *)lr->parent_class;
0123     return ip->open_file(file, sz);
0124   }
0125 #endif
0126 #endif
0127   int libraw_open_buffer(libraw_data_t *lr, const void *buffer, size_t size)
0128   {
0129     if (!lr)
0130       return EINVAL;
0131     LibRaw *ip = (LibRaw *)lr->parent_class;
0132     return ip->open_buffer(buffer, size);
0133   }
0134   int libraw_open_bayer(libraw_data_t *lr, unsigned char *data,
0135                         unsigned datalen, ushort _raw_width, ushort _raw_height,
0136                         ushort _left_margin, ushort _top_margin,
0137                         ushort _right_margin, ushort _bottom_margin,
0138                         unsigned char procflags, unsigned char bayer_pattern,
0139                         unsigned unused_bits, unsigned otherflags,
0140                         unsigned black_level)
0141   {
0142     if (!lr)
0143       return EINVAL;
0144     LibRaw *ip = (LibRaw *)lr->parent_class;
0145     return ip->open_bayer(data, datalen, _raw_width, _raw_height, _left_margin,
0146                           _top_margin, _right_margin, _bottom_margin, procflags,
0147                           bayer_pattern, unused_bits, otherflags, black_level);
0148   }
0149   int libraw_unpack(libraw_data_t *lr)
0150   {
0151     if (!lr)
0152       return EINVAL;
0153     LibRaw *ip = (LibRaw *)lr->parent_class;
0154     return ip->unpack();
0155   }
0156   int libraw_unpack_thumb(libraw_data_t *lr)
0157   {
0158     if (!lr)
0159       return EINVAL;
0160     LibRaw *ip = (LibRaw *)lr->parent_class;
0161     return ip->unpack_thumb();
0162   }
0163   int libraw_unpack_thumb_ex(libraw_data_t *lr, int i)
0164   {
0165     if (!lr)
0166       return EINVAL;
0167     LibRaw *ip = (LibRaw *)lr->parent_class;
0168     return ip->unpack_thumb_ex(i);
0169   }
0170   void libraw_recycle_datastream(libraw_data_t *lr)
0171   {
0172     if (!lr)
0173       return;
0174     LibRaw *ip = (LibRaw *)lr->parent_class;
0175     ip->recycle_datastream();
0176   }
0177   void libraw_recycle(libraw_data_t *lr)
0178   {
0179     if (!lr)
0180       return;
0181     LibRaw *ip = (LibRaw *)lr->parent_class;
0182     ip->recycle();
0183   }
0184   void libraw_close(libraw_data_t *lr)
0185   {
0186     if (!lr)
0187       return;
0188     LibRaw *ip = (LibRaw *)lr->parent_class;
0189     delete ip;
0190   }
0191 
0192   void libraw_set_exifparser_handler(libraw_data_t *lr, exif_parser_callback cb,
0193                                      void *data)
0194   {
0195     if (!lr)
0196       return;
0197     LibRaw *ip = (LibRaw *)lr->parent_class;
0198     ip->set_exifparser_handler(cb, data);
0199   }
0200 
0201   void libraw_set_dataerror_handler(libraw_data_t *lr, data_callback func,
0202                                     void *data)
0203   {
0204     if (!lr)
0205       return;
0206     LibRaw *ip = (LibRaw *)lr->parent_class;
0207     ip->set_dataerror_handler(func, data);
0208   }
0209   void libraw_set_progress_handler(libraw_data_t *lr, progress_callback cb,
0210                                    void *data)
0211   {
0212     if (!lr)
0213       return;
0214     LibRaw *ip = (LibRaw *)lr->parent_class;
0215     ip->set_progress_handler(cb, data);
0216   }
0217 
0218   // DCRAW
0219   int libraw_adjust_sizes_info_only(libraw_data_t *lr)
0220   {
0221     if (!lr)
0222       return EINVAL;
0223     LibRaw *ip = (LibRaw *)lr->parent_class;
0224     return ip->adjust_sizes_info_only();
0225   }
0226   int libraw_dcraw_ppm_tiff_writer(libraw_data_t *lr, const char *filename)
0227   {
0228     if (!lr)
0229       return EINVAL;
0230     LibRaw *ip = (LibRaw *)lr->parent_class;
0231     return ip->dcraw_ppm_tiff_writer(filename);
0232   }
0233   int libraw_dcraw_thumb_writer(libraw_data_t *lr, const char *fname)
0234   {
0235     if (!lr)
0236       return EINVAL;
0237     LibRaw *ip = (LibRaw *)lr->parent_class;
0238     return ip->dcraw_thumb_writer(fname);
0239   }
0240   int libraw_dcraw_process(libraw_data_t *lr)
0241   {
0242     if (!lr)
0243       return EINVAL;
0244     LibRaw *ip = (LibRaw *)lr->parent_class;
0245     return ip->dcraw_process();
0246   }
0247   libraw_processed_image_t *libraw_dcraw_make_mem_image(libraw_data_t *lr,
0248                                                         int *errc)
0249   {
0250     if (!lr)
0251     {
0252       if (errc)
0253         *errc = EINVAL;
0254       return NULL;
0255     }
0256     LibRaw *ip = (LibRaw *)lr->parent_class;
0257     return ip->dcraw_make_mem_image(errc);
0258   }
0259   libraw_processed_image_t *libraw_dcraw_make_mem_thumb(libraw_data_t *lr,
0260                                                         int *errc)
0261   {
0262     if (!lr)
0263     {
0264       if (errc)
0265         *errc = EINVAL;
0266       return NULL;
0267     }
0268     LibRaw *ip = (LibRaw *)lr->parent_class;
0269     return ip->dcraw_make_mem_thumb(errc);
0270   }
0271 
0272   void libraw_dcraw_clear_mem(libraw_processed_image_t *p)
0273   {
0274     LibRaw::dcraw_clear_mem(p);
0275   }
0276 
0277   int libraw_raw2image(libraw_data_t *lr)
0278   {
0279     if (!lr)
0280       return EINVAL;
0281     LibRaw *ip = (LibRaw *)lr->parent_class;
0282     return ip->raw2image();
0283   }
0284   void libraw_free_image(libraw_data_t *lr)
0285   {
0286     if (!lr)
0287       return;
0288     LibRaw *ip = (LibRaw *)lr->parent_class;
0289     ip->free_image();
0290   }
0291   int libraw_get_decoder_info(libraw_data_t *lr, libraw_decoder_info_t *d)
0292   {
0293     if (!lr || !d)
0294       return EINVAL;
0295     LibRaw *ip = (LibRaw *)lr->parent_class;
0296     return ip->get_decoder_info(d);
0297   }
0298   int libraw_COLOR(libraw_data_t *lr, int row, int col)
0299   {
0300     if (!lr)
0301       return EINVAL;
0302     LibRaw *ip = (LibRaw *)lr->parent_class;
0303     return ip->COLOR(row, col);
0304   }
0305 
0306   /* getters/setters used by 3DLut Creator */
0307   DllDef void libraw_set_demosaic(libraw_data_t *lr, int value)
0308   {
0309     if (!lr)
0310       return;
0311     LibRaw *ip = (LibRaw *)lr->parent_class;
0312     ip->imgdata.params.user_qual = value;
0313   }
0314 
0315   DllDef void libraw_set_output_color(libraw_data_t *lr, int value)
0316   {
0317     if (!lr)
0318       return;
0319     LibRaw *ip = (LibRaw *)lr->parent_class;
0320     ip->imgdata.params.output_color = value;
0321   }
0322 
0323   DllDef void libraw_set_adjust_maximum_thr(libraw_data_t *lr, float value)
0324   {
0325     if (!lr)
0326       return;
0327     LibRaw *ip = (LibRaw *)lr->parent_class;
0328     ip->imgdata.params.adjust_maximum_thr = value;
0329   }
0330 
0331   DllDef void libraw_set_output_bps(libraw_data_t *lr, int value)
0332   {
0333     if (!lr)
0334       return;
0335     LibRaw *ip = (LibRaw *)lr->parent_class;
0336     ip->imgdata.params.output_bps = value;
0337   }
0338 
0339     DllDef void libraw_set_output_tif(libraw_data_t *lr, int value)
0340   {
0341     if (!lr)
0342       return;
0343     LibRaw *ip = (LibRaw *)lr->parent_class;
0344     ip->imgdata.params.output_tiff = value;
0345   }
0346 
0347 #define MIN(a, b) ((a) < (b) ? (a) : (b))
0348 #define MAX(a, b) ((a) > (b) ? (a) : (b))
0349 #define LIM(x, min, max) MAX(min, MIN(x, max))
0350 
0351   DllDef void libraw_set_user_mul(libraw_data_t *lr, int index, float val)
0352   {
0353     if (!lr)
0354       return;
0355     LibRaw *ip = (LibRaw *)lr->parent_class;
0356     ip->imgdata.params.user_mul[LIM(index, 0, 3)] = val;
0357   }
0358 
0359   DllDef void libraw_set_gamma(libraw_data_t *lr, int index, float value)
0360   {
0361     if (!lr)
0362       return;
0363     LibRaw *ip = (LibRaw *)lr->parent_class;
0364     ip->imgdata.params.gamm[LIM(index, 0, 5)] = value;
0365   }
0366 
0367   DllDef void libraw_set_no_auto_bright(libraw_data_t *lr, int value)
0368   {
0369     if (!lr)
0370       return;
0371     LibRaw *ip = (LibRaw *)lr->parent_class;
0372     ip->imgdata.params.no_auto_bright = value;
0373   }
0374 
0375   DllDef void libraw_set_bright(libraw_data_t *lr, float value)
0376   {
0377     if (!lr)
0378       return;
0379     LibRaw *ip = (LibRaw *)lr->parent_class;
0380     ip->imgdata.params.bright = value;
0381   }
0382 
0383   DllDef void libraw_set_highlight(libraw_data_t *lr, int value)
0384   {
0385     if (!lr)
0386       return;
0387     LibRaw *ip = (LibRaw *)lr->parent_class;
0388     ip->imgdata.params.highlight = value;
0389   }
0390 
0391   DllDef void libraw_set_fbdd_noiserd(libraw_data_t *lr, int value)
0392   {
0393     if (!lr)
0394       return;
0395     LibRaw *ip = (LibRaw *)lr->parent_class;
0396     ip->imgdata.params.fbdd_noiserd = value;
0397   }
0398 
0399   DllDef int libraw_get_raw_height(libraw_data_t *lr)
0400   {
0401     if (!lr)
0402       return EINVAL;
0403     return lr->sizes.raw_height;
0404   }
0405 
0406   DllDef int libraw_get_raw_width(libraw_data_t *lr)
0407   {
0408     if (!lr)
0409       return EINVAL;
0410     return lr->sizes.raw_width;
0411   }
0412 
0413   DllDef int libraw_get_iheight(libraw_data_t *lr)
0414   {
0415     if (!lr)
0416       return EINVAL;
0417     return lr->sizes.iheight;
0418   }
0419 
0420   DllDef int libraw_get_iwidth(libraw_data_t *lr)
0421   {
0422     if (!lr)
0423       return EINVAL;
0424     return lr->sizes.iwidth;
0425   }
0426 
0427   DllDef float libraw_get_cam_mul(libraw_data_t *lr, int index)
0428   {
0429     if (!lr)
0430       return EINVAL;
0431     return lr->color.cam_mul[LIM(index, 0, 3)];
0432   }
0433 
0434   DllDef float libraw_get_pre_mul(libraw_data_t *lr, int index)
0435   {
0436     if (!lr)
0437       return EINVAL;
0438     return lr->color.pre_mul[LIM(index, 0, 3)];
0439   }
0440 
0441   DllDef float libraw_get_rgb_cam(libraw_data_t *lr, int index1, int index2)
0442   {
0443     if (!lr)
0444       return EINVAL;
0445     return lr->color.rgb_cam[LIM(index1, 0, 2)][LIM(index2, 0, 3)];
0446   }
0447 
0448   DllDef int libraw_get_color_maximum(libraw_data_t *lr)
0449   {
0450     if (!lr)
0451       return EINVAL;
0452     return lr->color.maximum;
0453   }
0454 
0455 #ifdef __cplusplus
0456 }
0457 #endif