/*--------------------------------------------------------------------------------------------*/

void true_raw_text_out (char* input_file_name)

/* 	A mod to Dave Coffin's dcraw to output the unprocessed raw ADC data in decimal text format.
	This is purely of use to those of a mathematical bent who want to study the figures. It doesn't produce a viewable image.
	
	BUILD INSTRUCTIONS
	You can find the latest dcraw.c at http://www.cybercom.net/~dcoffin/dcraw/dcraw.c
	Add this function before the other functions in dcraw.c 
	Find this code in the main function near the end of dcraw.c:
		(*load_raw)();
	Insert this code immediately after:
		true_raw_text_out(ifname);
		continue;
	Compile using Dave Coffin's command line. You may need to define DJGPP on the command line.	
	Good luck!
	
	v1.0 by Warren Mars 2010 */


{
	typedef ushort			pixel_bayer_data_type[4];
	char					output_file_name[256];
	ushort					colour_value;
	pixel_bayer_data_type	*image_bayer_data_ptr;
	unsigned long			true_row, true_col, row, col, true_width, true_height;
	FILE					*true_raw_text_file_ptr;

	/* Set output file name */
	strcpy (output_file_name, input_file_name);
	*strrchr(output_file_name, '.') = 0;
	strcat (output_file_name, "_TR.txt");

	/* Set some parameters */
	image_bayer_data_ptr = image;
	true_width = iwidth/2;
	true_height = iheight/2;

	/* Tell the user what's happening */
	fprintf (stderr, "Unprocessed raw data from %s %s - %s\n", make, model, ifname);
	fprintf (stderr, "Writing %s True Raw text file.\n", output_file_name);

	/* Open the text mode output file */
	true_raw_text_file_ptr = fopen(output_file_name, "w");
	if (true_raw_text_file_ptr == NULL)
	{
		fprintf (stderr, "Can't open True Raw Text file\n");
		return;
	}
	
	/* Output some essential camera parameters */
	fprintf (true_raw_text_file_ptr, "Unprocessed raw data from %s %s - %s. ", make, model, ifname);
	fprintf (true_raw_text_file_ptr, "Nominal maximum %d, black %d, ISO %6.0f, exposure %2.3f sec, aperture f/%2.1f, focal length %3.0f mm\n", maximum, black, iso_speed, shutter, aperture, focal_len);

	/* Output the data a row at a time */
	for (true_row=0; true_row < (true_height); true_row++)
	{
		row = true_row*2;

		/* Do all the top left Bayer pixels for this line */
		fprintf (true_raw_text_file_ptr, "\nL_%d\tcolour_1", true_row);
		for (true_col=0; true_col < (true_width); true_col++)
		{
			col = true_col*2;
			colour_value = BAYER(row, col);		
			fprintf (true_raw_text_file_ptr, "\t%d", colour_value);
		}

		/* Do all the top right Bayer pixels for this line */
		fprintf (true_raw_text_file_ptr, "\nL_%d\tcolour_2", true_row);
		for (true_col=0; true_col < (true_width); true_col++)
		{
			col = true_col*2;
			colour_value = BAYER(row, col+1);		
			fprintf (true_raw_text_file_ptr, "\t%d", colour_value);
		}

		/* Do all the bottom left Bayer pixels for this line */
		fprintf (true_raw_text_file_ptr, "\nL_%d\tcolour_3", true_row);
		for (true_col=0; true_col < (true_width); true_col++)
		{
			col = true_col*2;
			colour_value = BAYER(row+1, col);		
			fprintf (true_raw_text_file_ptr, "\t%d", colour_value);
		}

		/* Do all the bottom right Bayer pixels for this line */
		fprintf (true_raw_text_file_ptr, "\nL_%d\tcolour_4", true_row);
		for (true_col=0; true_col < (true_width); true_col++)
		{
			col = true_col*2;
			colour_value = BAYER(row+1, col+1);		
			fprintf (true_raw_text_file_ptr, "\t%d", colour_value);
		}
		fprintf (true_raw_text_file_ptr, "\n");

	}
	fclose (true_raw_text_file_ptr);

	return;
}

/*--------------------------------------------------------------------------------------------*/
