Loading raw images into a 'raw' bitmap

  • Nathan Towne

    Nathan Towne - 2014-01-06


    I am wondering if there is a way to load the pixels of raw image files (.cr2 particularly) directly into bitmaps as unsigned int16s, that is, without conversion to color pixels? The idea is to get the pixel values as they were taken, without interpreting their values according to their positions in the Bayer matrix (undeveloped images), and saving them in 16-bit monochrome bitmaps. This is useful for astrophotography where bias, dark, and flat frames are applied before developing. I have been using the load function specifying RAW_DISPLAY, but only rgb bitmaps are returned.

    Thanks for your comments.


  • neo

    neo - 2014-07-10

    I'm interested in the same. I need to process the raw Bayer pixels to eliminate hot pixels. After modifying these, freeimage should produce the normal RGB image. Is there support for that? Meaning accessing the Bayer matrix before the RGB image is generated. If not, could it be implemented?

  • Hervé Drolon

    Hervé Drolon - 2014-07-20


    I can add a load option to load the unprocessed Bayer matrix (as an unsigned 16-bit image).
    However, this image will be greater than the processed image (it will include a frame).
    Do you need to extract additional info (e.g. the top, left margins, the true image size, the CFA pattern, etc.) in order to process the Bayer matrix ?

    Note that once the image loaded, you will need to apply your own demosaicing algorithm in order to create the RGB image.


    • Nathan Towne

      Nathan Towne - 2014-07-24

      H Herve,

      Thanks for the reply. I personally don't use the color info, instead binning 2x2 and interpreting the binned image as unfiltered monochrome (for astrophotography). Regarding the frame, I know the image size and can crop the frame if necessary. So what I need is relatively simple. Others may need more.


  • Hervé Drolon

    Hervé Drolon - 2014-07-30


    Here is a suggested solution (other suggestions / comments are welcomed).

    I've added a new load flag to the FIF_RAW format, named RAW_UNPROCESSED.
    This flag will load a RAW file as a FIT_UINT16 Bayer matrix.
    Note that some formats don't have a Bayer matrix (e.g. Foveon, Canon sRAW, demosaiced DNG files).
    For these cases, using the RAW_UNPROCESSED load flag, the RAW plugin will return NULL (this can be changed if needed, in order to return other unprocessed formats).

    I was wondering how you can apply a demosaicing algorithm from a Bayer matrix, without any further info.
    Then I've added some metadata to the FIMD_COMMENTS metadata model (this can be changed / adjusted if needed).

    Whenever you load a RAW image using the RAW_UNPROCESSED flag, these metadata will be automatically filled inside the FIMD_COMMENTS metadata model (all metadata are strings) :

    Raw.Output.Width // what output width should be if the image was processed
    Raw.Output.Height // what output height should be if the image was processed

    The "Raw.BayerPattern" represents a mask describing the order of color pixels in the matrix.
    This field describe 16 pixels (8 rows with two pixels in each, from left to right and from top to bottom).

    Changes are available in the CVS.

    Hope this help,

  • Nathan Towne

    Nathan Towne - 2014-08-02


    I submitted most of this message earlier, but it did not seem to stick. If in fact it did stick, then this message supersedes the earlier one.

    Thanks for all of your effort. I am now able to read the raw files from my camera and have integrated the data into my pipeline. It took a couple of days to learn enough to get it done, as I know very little about FreeImage.

    One thing that tripped me up as a VB.Net programmer was accessing data when an IntPtr is provided. A quick note for the few VB.Net developers out there who are not aware of this, one can use the Marshal class to copy the image data to a managed object.

    Dim src As IntPtr = FreeImage.GetBits(image)
    Dim buffer(framesize - 1) As Short
    Marshal.Copy(src, buffer, 0, framesize)

    Perhaps there are better ways to do this. In another step the image is extracted from the frame.

    I noticed that the Raw.Output.Width and Raw.Frame.Width metadata have the same value, and the Raw.Output.Height and Raw.Frame.Height have the same value, i.e., the image values. Is this an error? The methods FreeImage.GetWidth and FreeImage.GetHeight seem to return frame dimensions.

    You mentioned about my not using information about the Bayer matrix to convert to monochrome pixels. Because I know that the Bayer matrix of my camera has a 2x2 unit cell, all I do is bin 2x2. I am aware that this is camera specific and not optimal. Now that FreeImage is a useful tool for me, the plan is to learn more about how to use its capabilities as it is intended.

    Thank you again.



Log in to post a comment.