PageRenderTime 73ms CodeModel.GetById 14ms app.highlight 54ms RepoModel.GetById 1ms app.codeStats 1ms

/src/FreeImage/Source/LibRawLite/internal/dcraw_fileio.cpp

https://bitbucket.org/cabalistic/ogredeps/
C++ | 240 lines | 207 code | 7 blank | 26 comment | 65 complexity | e49e86b5d26ad5140261e1fdca0fc2ba MD5 | raw file
  1/* 
  2  Copyright 2008-2010 LibRaw LLC (info@libraw.org)
  3
  4LibRaw is free software; you can redistribute it and/or modify
  5it under the terms of the one of three licenses as you choose:
  6
  71. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
  8   (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
  9
 102. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
 11   (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
 12
 133. LibRaw Software License 27032010
 14   (See file LICENSE.LibRaw.pdf provided in LibRaw distribution archive for details).
 15
 16   This file is generated from Dave Coffin's dcraw.c
 17   dcraw.c -- Dave Coffin's raw photo decoder
 18   Copyright 1997-2010 by Dave Coffin, dcoffin a cybercom o net
 19
 20   Look into dcraw homepage (probably http://cybercom.net/~dcoffin/dcraw/)
 21   for more information
 22*/
 23
 24#include <math.h>
 25#define CLASS LibRaw::
 26#include "libraw/libraw_types.h"
 27#define LIBRAW_LIBRARY_BUILD
 28#include "libraw/libraw.h"
 29#include "internal/defines.h"
 30#include "internal/var_defines.h"
 31
 32/*
 33   Seach from the current directory up to the root looking for
 34   a ".badpixels" file, and fix those pixels now.
 35 */
 36void CLASS bad_pixels (const char *cfname)
 37{
 38  FILE *fp=0;
 39#ifndef LIBRAW_LIBRARY_BUILD
 40  char *fname, *cp, line[128];
 41  int len, time, row, col, r, c, rad, tot, n, fixed=0;
 42#else
 43  char *cp, line[128];
 44  int time, row, col, r, c, rad, tot, n;
 45#ifdef DCRAW_VERBOSE
 46  int fixed = 0;
 47#endif
 48#endif
 49
 50  if (!filters) return;
 51#ifdef LIBRAW_LIBRARY_BUILD
 52  RUN_CALLBACK(LIBRAW_PROGRESS_BAD_PIXELS,0,2);
 53#endif
 54  if (cfname)
 55    fp = fopen (cfname, "r");
 56  if (!fp) 
 57      {
 58#ifdef LIBRAW_LIBRARY_BUILD
 59          imgdata.process_warnings |= LIBRAW_WARN_NO_BADPIXELMAP;
 60#endif
 61          return;
 62      }
 63  while (fgets (line, 128, fp)) {
 64    cp = strchr (line, '#');
 65    if (cp) *cp = 0;
 66    if (sscanf (line, "%d %d %d", &col, &row, &time) != 3) continue;
 67    if ((unsigned) col >= width || (unsigned) row >= height) continue;
 68    if (time > timestamp) continue;
 69    for (tot=n=0, rad=1; rad < 3 && n==0; rad++)
 70      for (r = row-rad; r <= row+rad; r++)
 71	for (c = col-rad; c <= col+rad; c++)
 72	  if ((unsigned) r < height && (unsigned) c < width &&
 73		(r != row || c != col) && fc(r,c) == fc(row,col)) {
 74	    tot += BAYER2(r,c);
 75	    n++;
 76	  }
 77    BAYER2(row,col) = tot/n;
 78#ifdef DCRAW_VERBOSE
 79    if (verbose) {
 80      if (!fixed++)
 81	fprintf (stderr,_("Fixed dead pixels at:"));
 82      fprintf (stderr, " %d,%d", col, row);
 83    }
 84#endif
 85  }
 86#ifdef DCRAW_VERBOSE
 87  if (fixed) fputc ('\n', stderr);
 88#endif
 89  fclose (fp);
 90#ifdef LIBRAW_LIBRARY_BUILD
 91  RUN_CALLBACK(LIBRAW_PROGRESS_BAD_PIXELS,1,2);
 92#endif
 93}
 94
 95void CLASS subtract (const char *fname)
 96{
 97  FILE *fp;
 98  int dim[3]={0,0,0}, comment=0, number=0, error=0, nd=0, c, row, col;
 99  ushort *pixel;
100#ifdef LIBRAW_LIBRARY_BUILD
101  RUN_CALLBACK(LIBRAW_PROGRESS_DARK_FRAME,0,2);
102#endif
103
104  if (!(fp = fopen (fname, "rb"))) {
105#ifdef DCRAW_VERBOSE
106    perror (fname); 
107#endif
108#ifdef LIBRAW_LIBRARY_BUILD
109    imgdata.process_warnings |= LIBRAW_WARN_BAD_DARKFRAME_FILE;
110#endif
111    return;
112  }
113  if (fgetc(fp) != 'P' || fgetc(fp) != '5') error = 1;
114  while (!error && nd < 3 && (c = fgetc(fp)) != EOF) {
115    if (c == '#')  comment = 1;
116    if (c == '\n') comment = 0;
117    if (comment) continue;
118    if (isdigit(c)) number = 1;
119    if (number) {
120      if (isdigit(c)) dim[nd] = dim[nd]*10 + c -'0';
121      else if (isspace(c)) {
122	number = 0;  nd++;
123      } else error = 1;
124    }
125  }
126  if (error || nd < 3) {
127    fprintf (stderr,_("%s is not a valid PGM file!\n"), fname);
128    fclose (fp);  return;
129  } else if (dim[0] != width || dim[1] != height || dim[2] != 65535) {
130#ifdef DCRAW_VERBOSE
131      fprintf (stderr,_("%s has the wrong dimensions!\n"), fname);
132#endif
133#ifdef LIBRAW_LIBRARY_BUILD
134      imgdata.process_warnings |= LIBRAW_WARN_BAD_DARKFRAME_DIM;
135#endif
136    fclose (fp);  return;
137  }
138  pixel = (ushort *) calloc (width, sizeof *pixel);
139  merror (pixel, "subtract()");
140  for (row=0; row < height; row++) {
141    fread (pixel, 2, width, fp);
142    for (col=0; col < width; col++)
143      BAYER(row,col) = MAX (BAYER(row,col) - ntohs(pixel[col]), 0);
144  }
145  free (pixel);
146  fclose (fp);
147  memset (cblack, 0, sizeof cblack);
148  black = 0;
149#ifdef LIBRAW_LIBRARY_BUILD
150  RUN_CALLBACK(LIBRAW_PROGRESS_DARK_FRAME,1,2);
151#endif
152}
153
154#ifndef NO_LCMS
155void CLASS apply_profile (const char *input, const char *output)
156{
157  char *prof;
158  cmsHPROFILE hInProfile=0, hOutProfile=0;
159  cmsHTRANSFORM hTransform;
160  FILE *fp;
161  unsigned size;
162
163#ifndef USE_LCMS2
164  cmsErrorAction (LCMS_ERROR_SHOW);
165#endif
166  if (strcmp (input, "embed"))
167    hInProfile = cmsOpenProfileFromFile (input, "r");
168  else if (profile_length) {
169#ifndef LIBRAW_LIBRARY_BUILD
170    prof = (char *) malloc (profile_length);
171    merror (prof, "apply_profile()");
172    fseek (ifp, profile_offset, SEEK_SET);
173    fread (prof, 1, profile_length, ifp);
174    hInProfile = cmsOpenProfileFromMem (prof, profile_length);
175    free (prof);
176#else
177    hInProfile = cmsOpenProfileFromMem (imgdata.color.profile, profile_length);
178#endif
179  } else
180      {
181#ifdef LIBRAW_LIBRARY_BUILD
182          imgdata.process_warnings |= LIBRAW_WARN_NO_EMBEDDED_PROFILE;
183#endif
184#ifdef DCRAW_VERBOSE
185          fprintf (stderr,_("%s has no embedded profile.\n"), ifname);
186#endif
187      }
188  if (!hInProfile)
189      {
190#ifdef LIBRAW_LIBRARY_BUILD
191          imgdata.process_warnings |= LIBRAW_WARN_NO_INPUT_PROFILE;
192#endif
193          return;
194      }
195  if (!output)
196    hOutProfile = cmsCreate_sRGBProfile();
197  else if ((fp = fopen (output, "rb"))) {
198    fread (&size, 4, 1, fp);
199    fseek (fp, 0, SEEK_SET);
200    oprof = (unsigned *) malloc (size = ntohl(size));
201    merror (oprof, "apply_profile()");
202    fread (oprof, 1, size, fp);
203    fclose (fp);
204    if (!(hOutProfile = cmsOpenProfileFromMem (oprof, size))) {
205      free (oprof);
206      oprof = 0;
207    }
208#ifdef DCRAW_VERBOSE
209  } else
210    fprintf (stderr,_("Cannot open file %s!\n"), output);
211#else
212}
213#endif
214  if (!hOutProfile)
215      {
216#ifdef LIBRAW_LIBRARY_BUILD
217          imgdata.process_warnings |= LIBRAW_WARN_BAD_OUTPUT_PROFILE;
218#endif
219          goto quit;
220      }
221#ifdef DCRAW_VERBOSE
222  if (verbose)
223    fprintf (stderr,_("Applying color profile...\n"));
224#endif
225#ifdef LIBRAW_LIBRARY_BUILD
226  RUN_CALLBACK(LIBRAW_PROGRESS_APPLY_PROFILE,0,2);
227#endif
228  hTransform = cmsCreateTransform (hInProfile, TYPE_RGBA_16,
229	hOutProfile, TYPE_RGBA_16, INTENT_PERCEPTUAL, 0);
230  cmsDoTransform (hTransform, image, image, width*height);
231  raw_color = 1;		/* Don't use rgb_cam with a profile */
232  cmsDeleteTransform (hTransform);
233  cmsCloseProfile (hOutProfile);
234quit:
235  cmsCloseProfile (hInProfile);
236#ifdef LIBRAW_LIBRARY_BUILD
237  RUN_CALLBACK(LIBRAW_PROGRESS_APPLY_PROFILE,1,2);
238#endif
239}
240#endif