Converting 12-bit RAW nef file data into an 8-bit RGB bitmap using LibRaw

Hey all,

I'm having trouble figuring out how to exactly convert a 12-bit RAW nef file to an 8-bit RGB bitmap, most specifically the 8-bit part. I've got this code section:

    if ((n_return = RawProcessor.open_file(l_input_file_name.c_str())) != LIBRAW_SUCCESS)
    	fprintf(stdout,"Cannot open_file. Moving to next image. Please check bad_files.csv when process completes.");
    	b_bad_files = true;
    //	RawProcessor is the main class for libraw. 
    //	It is used here to unpack the file into data structures to be used later.
    if ((n_return = RawProcessor.unpack()) != LIBRAW_SUCCESS)
    	fprintf(stdout,"Cannot unpack file. Moving to next image. Please check bad_files.csv when process completes.\n");
    	b_bad_files = true;
    if (!b_bad_files)
    {// if !b_bad_files -- begin
     //	Corrects for differences between the way the picture was taken
     //	and how the data is layed out -- ensures that it's always in landscape mode:
    	//	This metric tells us which way the picture was actually taken so that we can 
    	//	take the landscape raw values and rotate them appropriately during output.
    	n_orientation = RawProcessor.imgdata.sizes.flip;
    	//	sets pointer to LibRaw internal structure of raw data:
    	n_raw_input_vector = &RawProcessor.imgdata.rawdata.raw_image[0];
    	//	set color max value:
    	n_color_max_value = RawProcessor.imgdata.rawdata.color.maximum;

At this point, I could do quadratic imputing and use other techniques to obtain an R, G, and B per pixel but my issue is this data is still 12-bits. Does libraw offer any conversion functionality directly from RAW format to 8-bit RGB?


Libraw internal pixel format

Libraw internal pixel format is always 16-bit unsigned integer.

16->8 bit conversion (and gamma correction) is performed on make_mem_image() stage.

-- Alex Tutubalin