Here is a simple baseline patch showing you how to work with the dcraw.c source and add your own
output method for mathematical image processing research.
Add global variable for realraw command line option (-R).
--- dcraw.c.5-22-20172016-05-10 16:40:17.000000000 -0500
+++ realraw.1.c2017-05-23 10:11:24.000000000 -0500
@@ -125,6 +125,8 @@ ushort white[8][8], curve[0x10000], cr2_
double pixel_aspect, aber[4]={1,1,1,1}, gamm[6]={ 0.45,4.5,0,0,0,0 };
float bright=1, user_mul[4]={0,0,0,0}, threshold=0;
int mask[8][4];
+/* jkm */
+int realraw=0;
int half_size=0, four_color_rgb=0, document_mode=0, highlight=0;
int verbose=0, use_auto_wb=0, use_camera_wb=0, use_camera_matrix=1;
int output_color=1, output_bps=8, output_tiff=0, med_passes=0;
Add custom output function (nearly all your image processing code belongs here).
@@ -9836,6 +9838,96 @@ void CLASS write_ppm_tiff()
free (ppm);
}
+/* jkm */
+union Quad {
+uint64_t data;
+uint16_t rgbw[4];
+};
+
+struct Color {
+uint16_t min;
+uint16_t max;
+float *percentile;
+};
+
+void update_color(struct Color *c, union Quad *q, int p, int len)
+{
+unsigned int *count, i, v, min, max;
+long long total;
+count = malloc(256*64 * (sizeof (unsigned int)));
+c->percentile = malloc(256*64 * (sizeof (float)));
+for (i=0; i<256*64; i++)
+count[i] = 0;
+min = 256*64-1;
+max = 0;
+for (i=0; i<len; i++)
+{
+v = q[i].rgbw[p];
+if (v < min) min = v;
+if (v > max) max = v;
+count[v]++;
+}
+c->min = min;
+c->max = max;
+total = 0;
+for (i=0; i<256*64; i++)
+{
+c->percentile[i] = ( (float) total) /len;
+total += count[i];
+if (count[i] > 100)
+fprintf(stdout, "(%d) %d %d %f\n", p, i, count[i], c->percentile[i]);
+}
+free(count);
+}
+
+void CLASS save_realraw()
+{
+union Quad *quad;
+struct Color red, green, blue, white;
+unsigned char *row;
+int rows, cols, scols, len, i, j, k, n;
+int topmarg = 32, leftmarg = 64, rightmarg = 4, bottommarg = 4;
+unsigned short *px;
+rows = raw_height;
+cols = raw_width;
+len = rows*cols;
+quad = malloc (len * (sizeof (*quad)));
+ row = malloc (cols * (sizeof (*row)));
+k=0;
+for (i=topmarg; i<rows/2-bottommarg; i++) {
+for (j=leftmarg; j<cols/2-rightmarg; j++) {
+px = raw_image + 2*i*cols + 2*j;
+quad[k].rgbw[0] = *px;
+quad[k].rgbw[1] = px[1];
+quad[k].rgbw[2] = px[cols+1];;
+quad[k].rgbw[3] = px[cols];
+k++;
+}
+}
+update_color(&red, quad, 0, k);
+update_color(&green, quad, 1, k);
+update_color(&blue, quad, 2, k);
+update_color(&white, quad, 3, k);
+scols = cols/2-leftmarg-rightmarg;
+fprintf(ofp, "P5\n%d %d\n255\n", scols, rows/2-topmarg-bottommarg);
+j = 0;
+for (n=0; n<k; n++) {
+row[j] = (quad[n].rgbw[0] + quad[n].rgbw[1]
+ quad[n].rgbw[2] + quad[n].rgbw[3]) / 256;
+row[j] = 256/4*(red.percentile[quad[n].rgbw[0]]
+green.percentile[quad[n].rgbw[1]]
+blue.percentile[quad[n].rgbw[2]]
+white.percentile[quad[n].rgbw[3]]);
+j++;
+if (j == scols) {
+fwrite(row, sizeof *row, scols, ofp);
+j = 0;
+}
+}
+free(quad);
+free(row);
+free(red.percentile);
+free(green.percentile);
+free(blue.percentile);
+free(white.percentile);
+}
+
int CLASS main (int argc, const char **argv)
{
int arg, status=0, quality, i, c;
Modify the main function's options switch to add the command line -R option for RealRaw support.
@@ -9914,6 +10006,8 @@ int CLASS main (int argc, const char **a
return 1;
}
switch (opt) {
+/* jkm */
+ case 'R': realraw = 1; break;
case 'n': threshold = atof(argv[arg++]); break;
case 'b': bright = atof(argv[arg++]); break;
case 'r':
Skip to RealRaw processing to avoid wasting time doing all the other default
image processing operations.
@@ -10148,6 +10242,10 @@ next:
if (raw_image && read_from_stdin)
fread (raw_image, 2, raw_height*raw_width, stdin);
else (*load_raw)();
+/* jkm */
+if (realraw) {
+goto realraw;
+}
if (document_mode == 3) {
top_margin = left_margin = fuji_width = 0;
height = raw_height;
@@ -10221,6 +10319,12 @@ thumbnail:
write_ext = ".tiff";
else
write_ext = ".pgm\0.ppm\0.ppm\0.pam" + colors*5-5;
+/* jkm */
+realraw:
+if (realraw) {
+write_ext = ".realraw.pgm";
+write_fun = &CLASS save_realraw;
+}
ofname = (char *) malloc (strlen(ifname) + 64);
merror (ofname, "main()");
if (write_to_stdout)
@@ -10245,6 +10349,8 @@ thumbnail:
fprintf (stderr,_("Writing data to %s ...\n"), ofname);
(*write_fun)();
fclose(ifp);
+/* jkm */
+ if (realraw) free (raw_image);
if (ofp != stdout) fclose(ofp);
cleanup:
if (meta_data) free (meta_data);