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