SHARE
TWEET

DCRAW Sonification patch (updated)

a guest Sep 6th, 2015 143 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. *** dcraw.c     2014-12-05 16:57:39.000000000 -0800
  2. --- dcraw-sonify.c      2015-09-06 19:14:13.000000000 -0700
  3. ***************
  4. *** 9101,9112 ****
  5. --- 9101,9204 ----
  6.     free (ppm);
  7.   }
  8.  
  9. + void CLASS sony_artifacts(int bit_depth)
  10. + {
  11. +   int j, k, r, c, rggb;
  12. +   int diffs_only = 0;
  13. +   ushort *encode = curve, *decode = curve + 0x8000;
  14. +   int sony_black = 512;
  15. +
  16. +   static const int inflection[] = { 0, 2000, 3200, 5696, 8092, 17204, 65536 };
  17. +   int histo[16];
  18. +
  19. +   if (bit_depth > 14) {
  20. +     diffs_only = 1;
  21. +     bit_depth -= 14;
  22. +   }
  23. +   if (bit_depth < 1)
  24. +     bit_depth = 1;
  25. +   if (verbose)
  26. +     fprintf(stderr, _("Sony artifacts (bit depth %d)...\n"), bit_depth);
  27. +   for (j = 0; j < 16; ++j)
  28. +     histo[j] = 0;
  29. +
  30. +   /* Create encoding and decoding tables that apply Sony's tone curve and any
  31. +    * extra bit-depth reduction from bulb, long-exposure noise reduction, or
  32. +    * a continuously-driven shooting mode.
  33. +    */
  34. +   if (bit_depth <= 14) {
  35. +     int orig = 0, code = 0, shift = 1;
  36. +     unsigned mask = ~0 << (14 - bit_depth);
  37. +     for (; orig < 0x4000; ++shift) {
  38. +       int lim = inflection[shift];
  39. +       int step = 1 << shift;
  40. +       for (; orig < lim; ++code) {
  41. +       decode[code] = orig & mask;
  42. +       for (j = 0; j < step; ++j)
  43. +         encode[orig++] = code;
  44. +       }
  45. +     }
  46. +   } else {
  47. +     /* special mode: no tone curve or bit depth loss, just block encoding */
  48. +     for (j = 0; j < 0x4000; ++j)
  49. +       encode[j] = decode[j] = j;
  50. +   }
  51. +
  52. +   /* Apply this curve and the 7-bit differential encoding to interleaved 16px
  53. +    * isochromatic blocks.  Assumes an RGGB Bayer array similar to Sony's color
  54. +    * filter arrays.
  55. +    */
  56. +   for (r = 0; r < height; ++r) {
  57. +     for (c = 0; c + 32 <= raw_width; c += 32) {
  58. +       for (rggb = 0; rggb < 2; ++rggb) {
  59. +       int imin = 0, imax = 0, shift = 0;
  60. +       ushort minpx = encode[RAW(r, c + rggb) - black + sony_black], maxpx = minpx;
  61. +       unsigned mask;
  62. +       for (j = 2; j < 32; j += 2) {
  63. +         ushort px = encode[RAW(r, c + rggb + j) - black + sony_black];
  64. +         if (px < minpx)
  65. +           imin = j, minpx = px;
  66. +         else if (px > maxpx)
  67. +           imax = j, maxpx = px;
  68. +       }
  69. +       while ((maxpx - minpx) >= 128 << shift)
  70. +         ++shift;
  71. +       mask = ~0 << shift;
  72. +       for (j = 0; j < 32; j += 2) {
  73. +         ushort opx = RAW(r, c + rggb + j), npx = encode[opx - black + sony_black];
  74. +         if (j != imin && j != imax)
  75. +           npx = minpx + ((npx - minpx) & mask);
  76. +         npx = decode[npx] - sony_black + black;
  77. +         if (verbose) {
  78. +           int diff = npx > opx ? npx - opx : opx - npx;
  79. +           for (k = 0; diff >> k; ++k)
  80. +             continue;
  81. +           ++histo[k];
  82. +         }
  83. +         if (diffs_only)
  84. +           npx = npx > opx ? npx - opx : opx - npx;
  85. +         RAW(r, c + rggb + j) = npx;
  86. +       }
  87. +       }
  88. +     }
  89. +   }
  90. +
  91. +   if (verbose) {
  92. +     double x = 100.0 / height / width;
  93. +     for (j = 0; j < 16; ++j)
  94. +       if (histo[j] > 0)
  95. +       fprintf(stderr, _("%d pixels lost %d bit(s) (%g%%)\n"),
  96. +               histo[j], j, x * histo[j]);
  97. +   }
  98. + }
  99. +
  100.   int CLASS main (int argc, const char **argv)
  101.   {
  102.     int arg, status=0, quality, i, c;
  103.     int timestamp_only=0, thumbnail_only=0, identify_only=0;
  104.     int user_qual=-1, user_black=-1, user_sat=-1, user_flip=-1;
  105.     int use_fuji_rotate=1, write_to_stdout=0, read_from_stdin=0;
  106. +   int sony_bit_depth=0;
  107.     const char *sp, *bpfile=0, *dark_frame=0, *write_ext;
  108.     char opm, opt, *ofname, *cp;
  109.     struct utimbuf ut;
  110. ***************
  111. *** 9166,9179 ****
  112.       puts(_("-6        Write 16-bit instead of 8-bit"));
  113.       puts(_("-4        Linear 16-bit, same as \"-6 -W -g 1 1\""));
  114.       puts(_("-T        Write TIFF instead of PPM"));
  115.       puts("");
  116.       return 1;
  117.     }
  118.     argv[argc] = "";
  119.     for (arg=1; (((opm = argv[arg][0]) - 2) | 2) == '+'; ) {
  120.       opt = argv[arg++][1];
  121. !     if ((cp = (char *) strchr (sp="nbrkStqmHACg", opt)))
  122. !       for (i=0; i < "114111111422"[cp-sp]-'0'; i++)
  123.         if (!isdigit(argv[arg+i][0])) {
  124.           fprintf (stderr,_("Non-numeric argument to \"-%c\"\n"), opt);
  125.           return 1;
  126. --- 9258,9272 ----
  127.       puts(_("-6        Write 16-bit instead of 8-bit"));
  128.       puts(_("-4        Linear 16-bit, same as \"-6 -W -g 1 1\""));
  129.       puts(_("-T        Write TIFF instead of PPM"));
  130. +     puts(_("-Y <13|12>  Emulate Sony encoding artifacts and reduced bit depth"));
  131.       puts("");
  132.       return 1;
  133.     }
  134.     argv[argc] = "";
  135.     for (arg=1; (((opm = argv[arg][0]) - 2) | 2) == '+'; ) {
  136.       opt = argv[arg++][1];
  137. !     if ((cp = (char *) strchr (sp="nbrkStqmHACgY", opt)))
  138. !       for (i=0; i < "1141111114221"[cp-sp]-'0'; i++)
  139.         if (!isdigit(argv[arg+i][0])) {
  140.           fprintf (stderr,_("Non-numeric argument to \"-%c\"\n"), opt);
  141.           return 1;
  142. ***************
  143. *** 9230,9235 ****
  144. --- 9323,9329 ----
  145.         case '4':  gamm[0] = gamm[1] =
  146.                  no_auto_bright    = 1;
  147.         case '6':  output_bps       = 16;  break;
  148. +       case 'Y':  sony_bit_depth    = atoi(argv[arg++]); break;
  149.         default:
  150.         fprintf (stderr,_("Unknown option \"-%c\".\n"), opt);
  151.         return 1;
  152. ***************
  153. *** 9407,9412 ****
  154. --- 9501,9508 ----
  155.       if (raw_image && read_from_stdin)
  156.         fread (raw_image, 2, raw_height*raw_width, stdin);
  157.       else (*load_raw)();
  158. +     if (sony_bit_depth)
  159. +       sony_artifacts(sony_bit_depth);
  160.       if (document_mode == 3) {
  161.         top_margin = left_margin = fuji_width = 0;
  162.         height = raw_height;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top