File indexing completed on 2025-01-05 03:56:46

0001 /* -*- C++ -*-
0002  * File: libraw_internal_funcs.h
0003  * Copyright 2008-2021 LibRaw LLC (info@libraw.org)
0004  * Created: Sat Mar  14, 2008
0005 
0006 LibRaw is free software; you can redistribute it and/or modify
0007 it under the terms of the one of two licenses as you choose:
0008 
0009 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
0010    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
0011 
0012 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
0013    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
0014 
0015  */
0016 
0017 #ifndef _LIBRAW_INTERNAL_FUNCS_H
0018 #define _LIBRAW_INTERNAL_FUNCS_H
0019 
0020 #ifndef LIBRAW_LIBRARY_BUILD
0021 #error "This file should be used only for libraw library build"
0022 #else
0023 
0024 /* inline functions */
0025     static int stread(char *buf, size_t len, LibRaw_abstract_datastream *fp);
0026     static int getwords(char *line, char *words[], int maxwords, int maxlen);
0027     static void remove_trailing_spaces(char *string, size_t len);
0028     static void remove_caseSubstr(char *string, char *remove);
0029     static void removeExcessiveSpaces(char *string);
0030     static void trimSpaces(char *s);
0031 /* static tables/variables */
0032     static libraw_static_table_t tagtype_dataunit_bytes;
0033     static libraw_static_table_t Canon_wbi2std;
0034     static libraw_static_table_t Canon_KeyIsZero_Len2048_linenums_2_StdWBi;
0035     static libraw_static_table_t Canon_KeyIs0x0410_Len3072_linenums_2_StdWBi;
0036     static libraw_static_table_t Canon_KeyIs0x0410_Len2048_linenums_2_StdWBi;
0037     static libraw_static_table_t Canon_D30_linenums_2_StdWBi;
0038     static libraw_static_table_t Canon_G9_linenums_2_StdWBi;
0039 
0040     static libraw_static_table_t Fuji_wb_list1;
0041     static libraw_static_table_t FujiCCT_K;
0042     static libraw_static_table_t Fuji_wb_list2;
0043 
0044     static libraw_static_table_t Pentax_wb_list1;
0045     static libraw_static_table_t Pentax_wb_list2;
0046 
0047     static libraw_static_table_t Oly_wb_list1;
0048     static libraw_static_table_t Oly_wb_list2;
0049 
0050     static libraw_static_table_t Sony_SRF_wb_list;
0051     static libraw_static_table_t Sony_SR2_wb_list;
0052     static libraw_static_table_t Sony_SR2_wb_list1;
0053 /*  */
0054     int find_ifd_by_offset(int );
0055     void    libraw_swab(void *arr, size_t len);
0056     ushort  sget2 (uchar *s);
0057     ushort  sget2Rev(uchar *s);
0058     libraw_area_t   get_CanonArea();
0059     int parseCR3(INT64 oAtomList, INT64 szAtomList, short &nesting, char *AtomNameStack, short& nTrack, short &TrackType);
0060     void    selectCRXTrack();
0061     void    parseCR3_Free();
0062     int     parseCR3_CTMD(short trackNum);
0063     int     selectCRXFrame(short trackNum, unsigned frameIndex);
0064     void    setCanonBodyFeatures (unsigned long long id);
0065     void    processCanonCameraInfo (unsigned long long id, uchar *CameraInfo, unsigned maxlen, unsigned type, unsigned dng_writer);
0066     static float _CanonConvertAperture(ushort in);
0067     void    Canon_CameraSettings(unsigned len);
0068     void    Canon_WBpresets (int skip1, int skip2);
0069     void    Canon_WBCTpresets (short WBCTversion);
0070     void    parseCanonMakernotes (unsigned tag, unsigned type, unsigned len, unsigned dng_writer);
0071     void    processNikonLensData (uchar *LensData, unsigned len);
0072     void    Nikon_NRW_WBtag (int wb, int skip);
0073     void    parseNikonMakernote (int base, int uptag, unsigned dng_writer);
0074     void    parseEpsonMakernote (int base, int uptag, unsigned dng_writer);
0075     void    parseSigmaMakernote (int base, int uptag, unsigned dng_writer);
0076     void    setOlympusBodyFeatures (unsigned long long id);
0077     void    getOlympus_CameraType2 ();
0078     void    getOlympus_SensorTemperature (unsigned len);
0079     void    parseOlympus_Equipment (unsigned tag, unsigned type, unsigned len, unsigned dng_writer);
0080     void    parseOlympus_CameraSettings (int base, unsigned tag, unsigned type, unsigned len, unsigned dng_writer);
0081     void    parseOlympus_ImageProcessing (unsigned tag, unsigned type, unsigned len, unsigned dng_writer);
0082     void    parseOlympus_RawInfo (unsigned tag, unsigned type, unsigned len, unsigned dng_writer);
0083     void parseOlympusMakernotes (int base, unsigned tag, unsigned type, unsigned len, unsigned dng_writer);
0084     void    setPhaseOneFeatures (unsigned long long id);
0085     void    setPentaxBodyFeatures (unsigned long long id);
0086     void    PentaxISO (ushort c);
0087     void    PentaxLensInfo (unsigned long long id, unsigned len);
0088     void    parsePentaxMakernotes(int base, unsigned tag, unsigned type, unsigned len, unsigned dng_writer);
0089     void    parseRicohMakernotes(int base, unsigned tag, unsigned type, unsigned len, unsigned dng_writer);
0090     void    parseSamsungMakernotes(int base, unsigned tag, unsigned type, unsigned len, unsigned dng_writer);
0091 #ifdef LIBRAW_OLD_VIDEO_SUPPORT
0092     void    fixupArri();
0093 #endif
0094     void    setSonyBodyFeatures (unsigned long long id);
0095     void    parseSonyLensType2 (uchar a, uchar b);
0096     void    parseSonyLensFeatures (uchar a, uchar b);
0097     void    process_Sony_0x0116 (uchar * buf, ushort, unsigned long long id);
0098     void    process_Sony_0x2010 (uchar * buf, ushort);
0099     void    process_Sony_0x9050 (uchar * buf, ushort, unsigned long long id);
0100     void    process_Sony_0x9400 (uchar * buf, ushort, unsigned long long id);
0101     void    process_Sony_0x9402 (uchar * buf, ushort);
0102     void    process_Sony_0x9403 (uchar * buf, ushort);
0103     void    process_Sony_0x9406 (uchar * buf, ushort);
0104     void    process_Sony_0x940c (uchar * buf, ushort);
0105     void    process_Sony_0x940e (uchar * buf, ushort, unsigned long long id);
0106     void    parseSonyMakernotes (int base, unsigned tag, unsigned type, unsigned len, unsigned dng_writer,
0107                                uchar *&table_buf_0x0116, ushort &table_buf_0x0116_len,
0108                                uchar *&table_buf_0x2010, ushort &table_buf_0x2010_len,
0109                                uchar *&table_buf_0x9050, ushort &table_buf_0x9050_len,
0110                                uchar *&table_buf_0x9400, ushort &table_buf_0x9400_len,
0111                                uchar *&table_buf_0x9402, ushort &table_buf_0x9402_len,
0112                                uchar *&table_buf_0x9403, ushort &table_buf_0x9403_len,
0113                                uchar *&table_buf_0x9406, ushort &table_buf_0x9406_len,
0114                                uchar *&table_buf_0x940c, ushort &table_buf_0x940c_len,
0115                                uchar *&table_buf_0x940e, ushort &table_buf_0x940e_len);
0116     void    parseSonySR2 (uchar *cbuf_SR2, unsigned SR2SubIFDOffset, unsigned SR2SubIFDLength, unsigned dng_writer);
0117     void    parseSonySRF (unsigned len);
0118     void    parseFujiMakernotes (unsigned tag, unsigned type, unsigned len, unsigned dng_writer);
0119     const char* HassyRawFormat_idx2HR(unsigned idx);
0120     void    process_Hassy_Lens (int LensMount);
0121     void parseHassyModel ();
0122 
0123     void    setLeicaBodyFeatures(int LeicaMakernoteSignature);
0124     void    parseLeicaLensID();
0125     int     parseLeicaLensName(unsigned len);
0126     int     parseLeicaInternalBodySerial(unsigned len);
0127     void    parseLeicaMakernote(int base, int uptag, unsigned MakernoteTagType);
0128     void    parseAdobePanoMakernote ();
0129     void    parseAdobeRAFMakernote ();
0130     void    GetNormalizedModel ();
0131     void    SetStandardIlluminants (unsigned, const char* );
0132 
0133     ushort      get2();
0134     unsigned    sget4 (uchar *s);
0135     unsigned    getint(int type);
0136     float       int_to_float (int i);
0137     double      getreal (int type);
0138     double      sgetreal(int type, uchar *s);
0139     void        read_shorts (ushort *pixel, unsigned count);
0140 
0141 /* Canon P&S cameras */
0142     void        canon_600_fixed_wb (int temp);
0143     int         canon_600_color (int ratio[2], int mar);
0144     void        canon_600_auto_wb();
0145     void        canon_600_coeff();
0146     void        canon_600_load_raw();
0147     void        canon_600_correct();
0148     int         canon_s2is();
0149     void        parse_ciff (int offset, int length, int);
0150     void        ciff_block_1030();
0151 
0152 
0153 // LJPEG decoder
0154     unsigned    getbithuff (int nbits, ushort *huff);
0155     ushort*     make_decoder_ref (const uchar **source);
0156     ushort*     make_decoder (const uchar *source);
0157     int         ljpeg_start (struct jhead *jh, int info_only);
0158     void        ljpeg_end(struct jhead *jh);
0159     int         ljpeg_diff (ushort *huff);
0160     ushort *    ljpeg_row (int jrow, struct jhead *jh);
0161     ushort *    ljpeg_row_unrolled (int jrow, struct jhead *jh);
0162     void        ljpeg_idct (struct jhead *jh);
0163     unsigned    ph1_bithuff (int nbits, ushort *huff);
0164 
0165 // Canon DSLRs
0166     void        crw_init_tables (unsigned table, ushort *huff[2]);
0167     int         canon_has_lowbits();
0168     void        canon_load_raw();
0169     void        lossless_jpeg_load_raw();
0170     void        canon_sraw_load_raw();
0171 // Adobe DNG
0172     void        adobe_copy_pixel (unsigned int row, unsigned int col, ushort **rp);
0173     void        lossless_dng_load_raw();
0174     void        deflate_dng_load_raw();
0175     void        packed_dng_load_raw();
0176     void        packed_tiled_dng_load_raw();
0177     void        uncompressed_fp_dng_load_raw();
0178     void        lossy_dng_load_raw();
0179 //void        adobe_dng_load_raw_nc();
0180 
0181 // Pentax
0182     void        pentax_load_raw();
0183     void    pentax_4shot_load_raw();
0184 
0185     void        pentax_tree();
0186 
0187 // Nikon (and Minolta Z2)
0188     void        nikon_load_raw();
0189     void        nikon_he_load_raw_placeholder();
0190     void        nikon_read_curve();
0191     void        nikon_load_striped_packed_raw();
0192     void        nikon_load_padded_packed_raw();
0193     void        nikon_load_sraw();
0194     void        nikon_yuv_load_raw();
0195     void        nikon_coolscan_load_raw();
0196     int         nikon_e995();
0197     int         nikon_e2100();
0198     void        nikon_3700();
0199     int         minolta_z2();
0200 //  void        nikon_e2100_load_raw();
0201 
0202 // Fuji
0203 //  void        fuji_load_raw();
0204     int         guess_RAFDataGeneration (uchar *RAFData_start);
0205     void        parse_fuji (int offset);
0206     void        parse_fuji_thumbnail(int offset);
0207 #ifdef LIBRAW_OLD_VIDEO_SUPPORT
0208 // RedCine
0209     void        parse_redcine();
0210     void        redcine_load_raw();
0211 #endif
0212 
0213 // Rollei
0214     void        rollei_load_raw();
0215     void        parse_rollei();
0216 
0217 // Contax
0218     void        parse_kyocera ();
0219 
0220 // MF backs
0221 //int         bayer (unsigned row, unsigned col);
0222     int         p1raw(unsigned,unsigned);
0223     void        phase_one_flat_field (int is_float, int nc);
0224     int         p1rawc(unsigned row, unsigned col, unsigned& count);
0225     void        phase_one_fix_col_pixel_avg(unsigned row, unsigned col);
0226     void        phase_one_fix_pixel_grad(unsigned row, unsigned col);
0227     void        phase_one_load_raw();
0228     unsigned    ph1_bits (int nbits);
0229     void        phase_one_load_raw_c();
0230     void        phase_one_load_raw_s();
0231     void        hasselblad_load_raw();
0232     void        leaf_hdr_load_raw();
0233     void        sinar_4shot_load_raw();
0234     void        imacon_full_load_raw();
0235     void        hasselblad_full_load_raw();
0236     void        packed_load_raw();
0237     float       find_green(int,int,int,int);
0238     void        unpacked_load_raw();
0239     void        unpacked_load_raw_FujiDBP();
0240     void        unpacked_load_raw_reversed();
0241     void        unpacked_load_raw_fuji_f700s20();
0242     void        parse_sinar_ia();
0243     void        parse_phase_one (int base);
0244 
0245 // Misc P&S cameras
0246     void        parse_broadcom();
0247     void        broadcom_load_raw();
0248     void        nokia_load_raw();
0249     void        android_loose_load_raw();
0250     void        android_tight_load_raw();
0251 #ifdef LIBRAW_OLD_VIDEO_SUPPORT
0252     void        canon_rmf_load_raw();
0253 #endif
0254     unsigned    pana_data (int nb, unsigned *bytes);
0255     void        panasonic_load_raw();
0256 //  void        panasonic_16x10_load_raw();
0257     void        olympus_load_raw();
0258 //  void        olympus_cseries_load_raw();
0259     void        minolta_rd175_load_raw();
0260     void        quicktake_100_load_raw();
0261     const int*  make_decoder_int (const int *source, int level);
0262     int         radc_token (int tree);
0263     void        kodak_radc_load_raw();
0264     void        kodak_jpeg_load_raw();
0265     void        kodak_dc120_load_raw();
0266     void        eight_bit_load_raw();
0267     void        smal_decode_segment (unsigned seg[2][2], int holes);
0268     void        smal_v6_load_raw();
0269     int         median4 (int *p);
0270     void        fill_holes (int holes);
0271     void        smal_v9_load_raw();
0272     void        parse_riff(int maxdepth);
0273     void        parse_cine();
0274     void        parse_smal (int offset, int fsize);
0275     int         parse_jpeg (int offset);
0276 
0277 // Kodak
0278     void        kodak_262_load_raw();
0279     int         kodak_65000_decode (short *out, int bsize);
0280     void        kodak_65000_load_raw();
0281     void        kodak_rgb_load_raw();
0282     void        kodak_ycbcr_load_raw();
0283 //  void        kodak_yrgb_load_raw();
0284     void        kodak_c330_load_raw();
0285     void        kodak_c603_load_raw();
0286     void        kodak_rgb_load_thumb();
0287     void        kodak_ycbcr_load_thumb();
0288     void        vc5_dng_load_raw_placeholder();
0289 // It's a Sony (and K&M)
0290     void        sony_decrypt (unsigned *data, int len, int start, int key);
0291     void        sony_load_raw();
0292     void        sony_arw_load_raw();
0293     void        sony_arw2_load_raw();
0294     void        sony_arq_load_raw();
0295     void        sony_ljpeg_load_raw();
0296     void        samsung_load_raw();
0297     void        samsung2_load_raw();
0298     void        samsung3_load_raw();
0299     void        parse_minolta (int base);
0300 
0301 #ifdef USE_X3FTOOLS
0302 // Foveon/Sigma
0303 // We always have x3f code compiled in!
0304     void        parse_x3f();
0305     void        x3f_load_raw();
0306     void        x3f_dpq_interpolate_rg();
0307     void        x3f_dpq_interpolate_af(int xstep, int ystep, int scale); // 1x1 af pixels
0308     void        x3f_dpq_interpolate_af_sd(int xstart,int ystart, int xend, int yend, int xstep, int ystep, int scale); // sd Quattro interpolation
0309 #else
0310     void        parse_x3f() {}
0311     void        x3f_load_raw(){}
0312 #endif
0313 #ifdef USE_6BY9RPI
0314     void        rpi_load_raw8();
0315     void        rpi_load_raw12();
0316     void        rpi_load_raw14();
0317     void        rpi_load_raw16();
0318     void        parse_raspberrypi();
0319 #endif
0320 
0321 // CAM/RGB
0322     void        pseudoinverse (double (*in)[3], double (*out)[3], int size);
0323     void        simple_coeff (int index);
0324 
0325 // Openp
0326     char** malloc_omp_buffers(int buffer_count, size_t buffer_size);
0327     void free_omp_buffers(char** buffers, int buffer_count);
0328 
0329 
0330 // Tiff/Exif parsers
0331     void        tiff_get (unsigned base,unsigned *tag, unsigned *type, unsigned *len, unsigned *save);
0332     short       tiff_sget(unsigned save, uchar *buf, unsigned buf_len, INT64 *tag_offset,
0333                           unsigned *tag_id, unsigned *tag_type, INT64 *tag_dataoffset,
0334                           unsigned *tag_datalen, int *tag_dataunit_len);
0335     void        parse_thumb_note (int base, unsigned toff, unsigned tlen);
0336     void        parse_makernote (int base, int uptag);
0337     void        parse_makernote_0xc634(int base, int uptag, unsigned dng_writer);
0338     void        parse_exif (int base);
0339     void        parse_exif_interop(int base);
0340     void        linear_table(unsigned len);
0341     void        Kodak_DCR_WBtags(int wb, unsigned type, int wbi);
0342     void        Kodak_KDC_WBtags(int wb, int wbi);
0343     short       KodakIllumMatrix (unsigned type, float *romm_camIllum);
0344     void        parse_kodak_ifd (int base);
0345     int         parse_tiff_ifd (int base);
0346     int         parse_tiff (int base);
0347     void        apply_tiff(void);
0348     void        parse_gps (int base);
0349     void        parse_gps_libraw(int base);
0350     void        aRGB_coeff(double aRGB_cam[3][3]);
0351     void        romm_coeff(float romm_cam[3][3]);
0352     void        parse_mos (INT64 offset);
0353     void        parse_qt (int end);
0354     void        get_timestamp (int reversed);
0355 
0356 // The identify
0357     short       guess_byte_order (int words);
0358     void        identify_process_dng_fields();
0359     void        identify_finetune_pentax();
0360     void        identify_finetune_by_filesize(int);
0361     void        identify_finetune_dcr(char head[64],int,int);
0362 // Tiff writer
0363     void        tiff_set(struct tiff_hdr *th, ushort *ntag,ushort tag, ushort type, int count, int val);
0364     void        tiff_head (struct tiff_hdr *th, int full);
0365 
0366 // split AHD code
0367     void ahd_interpolate_green_h_and_v(int top, int left, ushort (*out_rgb)[LIBRAW_AHD_TILE][LIBRAW_AHD_TILE][3]);
0368     void ahd_interpolate_r_and_b_in_rgb_and_convert_to_cielab(int top, int left, ushort (*inout_rgb)[LIBRAW_AHD_TILE][3], short (*out_lab)[LIBRAW_AHD_TILE][3]);
0369     void ahd_interpolate_r_and_b_and_convert_to_cielab(int top, int left, ushort (*inout_rgb)[LIBRAW_AHD_TILE][LIBRAW_AHD_TILE][3], short (*out_lab)[LIBRAW_AHD_TILE][LIBRAW_AHD_TILE][3]);
0370     void ahd_interpolate_build_homogeneity_map(int top, int left, short (*lab)[LIBRAW_AHD_TILE][LIBRAW_AHD_TILE][3], char (*out_homogeneity_map)[LIBRAW_AHD_TILE][2]);
0371     void ahd_interpolate_combine_homogeneous_pixels(int top, int left, ushort (*rgb)[LIBRAW_AHD_TILE][LIBRAW_AHD_TILE][3], char (*homogeneity_map)[LIBRAW_AHD_TILE][2]);
0372 
0373     void init_fuji_compr(struct fuji_compressed_params* info);
0374     void init_fuji_block(struct fuji_compressed_block* info, const struct fuji_compressed_params *params, INT64 raw_offset, unsigned dsize);
0375     void copy_line_to_xtrans(struct fuji_compressed_block* info, int cur_line, int cur_block, int cur_block_width);
0376     void copy_line_to_bayer(struct fuji_compressed_block* info, int cur_line, int cur_block, int cur_block_width);
0377     void xtrans_decode_block(struct fuji_compressed_block* info, const struct fuji_compressed_params *params, int cur_line);
0378     void fuji_bayer_decode_block(struct fuji_compressed_block* info, const struct fuji_compressed_params *params, int cur_line);
0379     void fuji_compressed_load_raw();
0380     void fuji_14bit_load_raw();
0381     void parse_fuji_compressed_header();
0382     void crxLoadRaw();
0383     int  crxParseImageHeader(uchar *cmp1TagData, int nTrack, int size);
0384     void panasonicC6_load_raw();
0385     void panasonicC7_load_raw();
0386 
0387     void nikon_14bit_load_raw();
0388 
0389 // DCB
0390     void    dcb_pp();
0391     void    dcb_copy_to_buffer(float (*image2)[3]);
0392     void    dcb_restore_from_buffer(float (*image2)[3]);
0393     void    dcb_color();
0394     void    dcb_color_full();
0395     void    dcb_map();
0396     void    dcb_correction();
0397     void    dcb_correction2();
0398     void    dcb_refinement();
0399     void    rgb_to_lch(double (*image3)[3]);
0400     void    lch_to_rgb(double (*image3)[3]);
0401     void    fbdd_correction();
0402     void    fbdd_correction2(double (*image3)[3]);
0403     void    fbdd_green();
0404     void    dcb_ver(float (*image3)[3]);
0405     void    dcb_hor(float (*image2)[3]);
0406     void    dcb_color2(float (*image2)[3]);
0407     void    dcb_color3(float (*image3)[3]);
0408     void    dcb_decide(float (*image2)[3], float (*image3)[3]);
0409     void    dcb_nyquist();
0410 #endif
0411 
0412 #endif