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

0001 /* -*- C++ -*-
0002  * Copyright 2019-2021 LibRaw LLC (info@libraw.org)
0003  *
0004  LibRaw uses code from dcraw.c -- Dave Coffin's raw photo decoder,
0005  dcraw.c is copyright 1997-2018 by Dave Coffin, dcoffin a cybercom o net.
0006  LibRaw do not use RESTRICTED code from dcraw.c
0007 
0008  LibRaw is free software; you can redistribute it and/or modify
0009  it under the terms of the one of two licenses as you choose:
0010 
0011 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
0012    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
0013 
0014 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
0015    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
0016 
0017  */
0018 
0019 #include "../../internal/dcraw_defs.h"
0020 
0021 short LibRaw::guess_byte_order(int words)
0022 {
0023   uchar test[4][2];
0024   int t = 2, msb;
0025   double diff, sum[2] = {0, 0};
0026 
0027   fread(test[0], 2, 2, ifp);
0028   for (words -= 2; words--;)
0029   {
0030     fread(test[t], 2, 1, ifp);
0031     for (msb = 0; msb < 2; msb++)
0032     {
0033       diff = (test[t ^ 2][msb] << 8 | test[t ^ 2][!msb]) -
0034              (test[t][msb] << 8 | test[t][!msb]);
0035       sum[msb] += diff * diff;
0036     }
0037     t = (t + 1) & 3;
0038   }
0039   return sum[0] < sum[1] ? 0x4d4d : 0x4949;
0040 }
0041 
0042 float LibRaw::find_green(int bps, int bite, int off0, int off1)
0043 {
0044   UINT64 bitbuf = 0;
0045   int vbits, col, i, c;
0046   ushort img[2][2064];
0047   double sum[] = {0, 0};
0048   if (width > 2064)
0049     return 0.f; // too wide
0050 
0051   FORC(2)
0052   {
0053     fseek(ifp, c ? off1 : off0, SEEK_SET);
0054     for (vbits = col = 0; col < width; col++)
0055     {
0056       for (vbits -= bps; vbits < 0; vbits += bite)
0057       {
0058         bitbuf <<= bite;
0059         for (i = 0; i < bite; i += 8)
0060           bitbuf |= (unsigned)(fgetc(ifp) << i);
0061       }
0062       img[c][col] = bitbuf << (64 - bps - vbits) >> (64 - bps);
0063     }
0064   }
0065   FORC(width - 1)
0066   {
0067     sum[c & 1] += ABS(img[0][c] - img[1][c + 1]);
0068     sum[~c & 1] += ABS(img[1][c] - img[0][c + 1]);
0069   }
0070   if (sum[0] >= 1.0 && sum[1] >= 1.0)
0071     return 100 * log(sum[0] / sum[1]);
0072   else
0073     return 0.f;
0074 }
0075 
0076 void LibRaw::trimSpaces(char *s)
0077 {
0078   char *p = s;
0079   int l = int(strlen(p));
0080   if (!l)
0081     return;
0082   while (isspace(p[l - 1]))
0083     p[--l] = 0; /* trim trailing spaces */
0084   while (*p && isspace(*p))
0085     ++p, --l;   /* trim leading spaces */
0086   memmove(s, p, l + 1);
0087 }
0088 
0089 void LibRaw::remove_trailing_spaces(char *string, size_t len)
0090 {
0091   if (len < 1)
0092     return; // not needed, b/c sizeof of make/model is 64
0093   string[len - 1] = 0;
0094   if (len < 3)
0095     return; // also not needed
0096   len = strnlen(string, len - 1);
0097   for (int i = int(len) - 1; i >= 0; i--)
0098   {
0099     if (isspace((unsigned char)string[i]))
0100       string[i] = 0;
0101     else
0102       break;
0103   }
0104 }
0105 
0106 void LibRaw::remove_caseSubstr(char *string, char *subStr) // replace a substring with an equal length of spaces
0107 {
0108   char *found;
0109   while ((found = strcasestr(string,subStr))) {
0110     if (!found) return;
0111     int fill_len = int(strlen(subStr));
0112     int p = found - string;
0113     for (int i=p; i<p+fill_len; i++) {
0114       string[i] = 32;
0115     }
0116   }
0117   trimSpaces (string);
0118 }
0119 
0120 void LibRaw::removeExcessiveSpaces(char *string) // replace repeating spaces with one space
0121 {
0122     int orig_len = int(strlen(string));
0123     int i = 0;   // counter for resulting string
0124     int j = -1;
0125     bool prev_char_is_space = false;
0126     while (++j < orig_len && string[j] == ' ');
0127     while (j < orig_len)  {
0128         if (string[j] != ' ')  {
0129                 string[i++] = string[j++];
0130                 prev_char_is_space = false;
0131         } else if (string[j++] == ' ') {
0132             if (!prev_char_is_space) {
0133                 string[i++] = ' ';
0134                 prev_char_is_space = true;
0135             }
0136         }
0137     }
0138     if (string[i-1] == ' ')
0139     string[i-1] = 0;
0140 }