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

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_fileio_defs.h"
0020 
0021 /*
0022    Search from the current directory up to the root looking for
0023    a ".badpixels" file, and fix those pixels now.
0024  */
0025 void LibRaw::bad_pixels(const char *cfname)
0026 {
0027   FILE *fp = NULL;
0028   char *cp, line[128];
0029   int time, row, col, r, c, rad, tot, n;
0030 
0031   if (!filters)
0032     return;
0033   RUN_CALLBACK(LIBRAW_PROGRESS_BAD_PIXELS, 0, 2);
0034   if (cfname)
0035     fp = fopen(cfname, "r");
0036   if (!fp)
0037   {
0038     imgdata.process_warnings |= LIBRAW_WARN_NO_BADPIXELMAP;
0039     return;
0040   }
0041   while (fgets(line, 128, fp))
0042   {
0043     cp = strchr(line, '#');
0044     if (cp)
0045       *cp = 0;
0046     if (sscanf(line, "%d %d %d", &col, &row, &time) != 3)
0047       continue;
0048     if ((unsigned)col >= width || (unsigned)row >= height)
0049       continue;
0050     if (time > timestamp)
0051       continue;
0052     for (tot = n = 0, rad = 1; rad < 3 && n == 0; rad++)
0053       for (r = row - rad; r <= row + rad; r++)
0054         for (c = col - rad; c <= col + rad; c++)
0055           if ((unsigned)r < height && (unsigned)c < width &&
0056               (r != row || c != col) && fcol(r, c) == fcol(row, col))
0057           {
0058             tot += BAYER2(r, c);
0059             n++;
0060           }
0061     if (n > 0)
0062       BAYER2(row, col) = tot / n;
0063   }
0064   fclose(fp);
0065   RUN_CALLBACK(LIBRAW_PROGRESS_BAD_PIXELS, 1, 2);
0066 }
0067 
0068 void LibRaw::subtract(const char *fname)
0069 {
0070   FILE *fp;
0071   int dim[3] = {0, 0, 0}, comment = 0, number = 0, error = 0, nd = 0, c, row,
0072       col;
0073   RUN_CALLBACK(LIBRAW_PROGRESS_DARK_FRAME, 0, 2);
0074 
0075   if (!(fp = fopen(fname, "rb")))
0076   {
0077     imgdata.process_warnings |= LIBRAW_WARN_BAD_DARKFRAME_FILE;
0078     return;
0079   }
0080   if (fgetc(fp) != 'P' || fgetc(fp) != '5')
0081     error = 1;
0082   while (!error && nd < 3 && (c = fgetc(fp)) != EOF)
0083   {
0084     if (c == '#')
0085       comment = 1;
0086     if (c == '\n')
0087       comment = 0;
0088     if (comment)
0089       continue;
0090     if (isdigit(c))
0091       number = 1;
0092     if (number)
0093     {
0094       if (isdigit(c))
0095         dim[nd] = dim[nd] * 10 + c - '0';
0096       else if (isspace(c))
0097       {
0098         number = 0;
0099         nd++;
0100       }
0101       else
0102         error = 1;
0103     }
0104   }
0105   if (error || nd < 3)
0106   {
0107     fclose(fp);
0108     return;
0109   }
0110   else if (dim[0] != width || dim[1] != height || dim[2] != 65535)
0111   {
0112     imgdata.process_warnings |= LIBRAW_WARN_BAD_DARKFRAME_DIM;
0113     fclose(fp);
0114     return;
0115   }
0116   std::vector<ushort> pixel(width, 0);
0117   for (row = 0; row < height; row++)
0118   {
0119     fread(pixel.data(), 2, width, fp);
0120     for (col = 0; col < width; col++)
0121       BAYER(row, col) = MAX(BAYER(row, col) - ntohs(pixel[col]), 0);
0122   }
0123   fclose(fp);
0124   memset(cblack, 0, sizeof cblack);
0125   black = 0;
0126   RUN_CALLBACK(LIBRAW_PROGRESS_DARK_FRAME, 1, 2);
0127 }