Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *** dcraw.c 2014-12-05 16:57:39.000000000 -0800
- --- dcraw-sonify.c 2015-09-06 19:14:13.000000000 -0700
- ***************
- *** 9101,9112 ****
- --- 9101,9204 ----
- free (ppm);
- }
- + void CLASS sony_artifacts(int bit_depth)
- + {
- + int j, k, r, c, rggb;
- + int diffs_only = 0;
- + ushort *encode = curve, *decode = curve + 0x8000;
- + int sony_black = 512;
- +
- + static const int inflection[] = { 0, 2000, 3200, 5696, 8092, 17204, 65536 };
- + int histo[16];
- +
- + if (bit_depth > 14) {
- + diffs_only = 1;
- + bit_depth -= 14;
- + }
- + if (bit_depth < 1)
- + bit_depth = 1;
- + if (verbose)
- + fprintf(stderr, _("Sony artifacts (bit depth %d)...\n"), bit_depth);
- + for (j = 0; j < 16; ++j)
- + histo[j] = 0;
- +
- + /* Create encoding and decoding tables that apply Sony's tone curve and any
- + * extra bit-depth reduction from bulb, long-exposure noise reduction, or
- + * a continuously-driven shooting mode.
- + */
- + if (bit_depth <= 14) {
- + int orig = 0, code = 0, shift = 1;
- + unsigned mask = ~0 << (14 - bit_depth);
- + for (; orig < 0x4000; ++shift) {
- + int lim = inflection[shift];
- + int step = 1 << shift;
- + for (; orig < lim; ++code) {
- + decode[code] = orig & mask;
- + for (j = 0; j < step; ++j)
- + encode[orig++] = code;
- + }
- + }
- + } else {
- + /* special mode: no tone curve or bit depth loss, just block encoding */
- + for (j = 0; j < 0x4000; ++j)
- + encode[j] = decode[j] = j;
- + }
- +
- + /* Apply this curve and the 7-bit differential encoding to interleaved 16px
- + * isochromatic blocks. Assumes an RGGB Bayer array similar to Sony's color
- + * filter arrays.
- + */
- + for (r = 0; r < height; ++r) {
- + for (c = 0; c + 32 <= raw_width; c += 32) {
- + for (rggb = 0; rggb < 2; ++rggb) {
- + int imin = 0, imax = 0, shift = 0;
- + ushort minpx = encode[RAW(r, c + rggb) - black + sony_black], maxpx = minpx;
- + unsigned mask;
- + for (j = 2; j < 32; j += 2) {
- + ushort px = encode[RAW(r, c + rggb + j) - black + sony_black];
- + if (px < minpx)
- + imin = j, minpx = px;
- + else if (px > maxpx)
- + imax = j, maxpx = px;
- + }
- + while ((maxpx - minpx) >= 128 << shift)
- + ++shift;
- + mask = ~0 << shift;
- + for (j = 0; j < 32; j += 2) {
- + ushort opx = RAW(r, c + rggb + j), npx = encode[opx - black + sony_black];
- + if (j != imin && j != imax)
- + npx = minpx + ((npx - minpx) & mask);
- + npx = decode[npx] - sony_black + black;
- + if (verbose) {
- + int diff = npx > opx ? npx - opx : opx - npx;
- + for (k = 0; diff >> k; ++k)
- + continue;
- + ++histo[k];
- + }
- + if (diffs_only)
- + npx = npx > opx ? npx - opx : opx - npx;
- + RAW(r, c + rggb + j) = npx;
- + }
- + }
- + }
- + }
- +
- + if (verbose) {
- + double x = 100.0 / height / width;
- + for (j = 0; j < 16; ++j)
- + if (histo[j] > 0)
- + fprintf(stderr, _("%d pixels lost %d bit(s) (%g%%)\n"),
- + histo[j], j, x * histo[j]);
- + }
- + }
- +
- int CLASS main (int argc, const char **argv)
- {
- int arg, status=0, quality, i, c;
- int timestamp_only=0, thumbnail_only=0, identify_only=0;
- int user_qual=-1, user_black=-1, user_sat=-1, user_flip=-1;
- int use_fuji_rotate=1, write_to_stdout=0, read_from_stdin=0;
- + int sony_bit_depth=0;
- const char *sp, *bpfile=0, *dark_frame=0, *write_ext;
- char opm, opt, *ofname, *cp;
- struct utimbuf ut;
- ***************
- *** 9166,9179 ****
- puts(_("-6 Write 16-bit instead of 8-bit"));
- puts(_("-4 Linear 16-bit, same as \"-6 -W -g 1 1\""));
- puts(_("-T Write TIFF instead of PPM"));
- puts("");
- return 1;
- }
- argv[argc] = "";
- for (arg=1; (((opm = argv[arg][0]) - 2) | 2) == '+'; ) {
- opt = argv[arg++][1];
- ! if ((cp = (char *) strchr (sp="nbrkStqmHACg", opt)))
- ! for (i=0; i < "114111111422"[cp-sp]-'0'; i++)
- if (!isdigit(argv[arg+i][0])) {
- fprintf (stderr,_("Non-numeric argument to \"-%c\"\n"), opt);
- return 1;
- --- 9258,9272 ----
- puts(_("-6 Write 16-bit instead of 8-bit"));
- puts(_("-4 Linear 16-bit, same as \"-6 -W -g 1 1\""));
- puts(_("-T Write TIFF instead of PPM"));
- + puts(_("-Y <13|12> Emulate Sony encoding artifacts and reduced bit depth"));
- puts("");
- return 1;
- }
- argv[argc] = "";
- for (arg=1; (((opm = argv[arg][0]) - 2) | 2) == '+'; ) {
- opt = argv[arg++][1];
- ! if ((cp = (char *) strchr (sp="nbrkStqmHACgY", opt)))
- ! for (i=0; i < "1141111114221"[cp-sp]-'0'; i++)
- if (!isdigit(argv[arg+i][0])) {
- fprintf (stderr,_("Non-numeric argument to \"-%c\"\n"), opt);
- return 1;
- ***************
- *** 9230,9235 ****
- --- 9323,9329 ----
- case '4': gamm[0] = gamm[1] =
- no_auto_bright = 1;
- case '6': output_bps = 16; break;
- + case 'Y': sony_bit_depth = atoi(argv[arg++]); break;
- default:
- fprintf (stderr,_("Unknown option \"-%c\".\n"), opt);
- return 1;
- ***************
- *** 9407,9412 ****
- --- 9501,9508 ----
- if (raw_image && read_from_stdin)
- fread (raw_image, 2, raw_height*raw_width, stdin);
- else (*load_raw)();
- + if (sony_bit_depth)
- + sony_artifacts(sony_bit_depth);
- if (document_mode == 3) {
- top_margin = left_margin = fuji_width = 0;
- height = raw_height;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement