LibRaw: raw images decoder library

LibRaw is a library for reading RAW files from digital photo cameras (CRW/CR2, NEF, RAF, DNG, MOS, KDC, DCR, etc.; virtually all RAW formats are supported). It pays special attention to correct retrieval of data required for subsequent RAW conversion.

The library is intended for embedding in RAW converters, data analyzers, and other programs using RAW files as the initial data.

Copyright

LibRaw library, Copyright (C) 2008-2009 LibRaw LLC (info@libraw.org)
The library includes source code from
dcraw.c, Dave Coffin's raw photo decoder
Copyright 1997-2008 by Dave Coffin, dcoffin a cybercom o net

LibRaw is distributed for free under three different licenses:

You may use one of these licensing modes and switch between them.

Contents

API Overview (C++)

General Remarks

  1. The entire processing is carried out by an instance of the LibRaw class, which is an image processor.
  2. One image processor can simultaneously process only one data source file, but consecutive processing of any number of files is possible.
  3. There may be several simultaneously working image processors in a software program (e.g., in different threads), although one should remember that each image processor may require much memory.
  4. Reading of source data from the RAW file requires virtually no customization (see API Notes for exceptions to this rule).
  5. All data extracted from the RAW file are accessible through data fields of the image processor (LibRaw class instance).
  6. Although LibRaw is not intended for RAW data postprocessing, the library includes calls that enable complete emulation of the dcraw utility.
  7. All customization for the processing is performed via data fields of the LibRaw class.

Brief Demonstration

The example below contains no error processing for the sake of brevity.

#include "libraw/libraw.h"
int process_image(char *file)
{
        // Let us create an image processor
        LibRaw iProcessor;

        // Open the file and read the metadata
        iProcessor.open_file(file);

        // The metadata are accessible through data fields of the class
        printf("Image size: %d x %d\n",iProcessor.imgdata.sizes.width,iProcessor.imgdata.sizes.height);

        // Let us unpack the image
        iProcessor.unpack();

        // And let us print its dump; the data are accessible through data fields of the class
        for(i = 0;
            i < iProcessor.imgdata.sizes.iwidth *  iProcessor.imgdata.sizes.iheight;
            i++)
           printf("i=%d R=%d G=%d B=%d G2=%d\n",
                        i,
                        iProcessor.imgdata.image[i][0],
                        iProcessor.imgdata.image[i][1],
                        iProcessor.imgdata.image[i][2],
                        iProcessor.imgdata.image[i][3]
                );

        // Finally, let us free the image processor for work with the next image
        iProcessor.recycle();
}

Compilation and Installation

LibRaw is distributed in the form of source codes and/or precompiled binaries. For further use, they should be compiled (and, if desired, placed into system folders with libraries and include-files).

Unix Systems (FreeBSD, Linux, Mac OS X)

To build the library, you will need a working C++ compiler (gcc ver. 3.x or 4.x will be OK; other compilers have not been tested) and the make utility. No other libraries or utilities are required.

LibRaw has been tested on 32- and 64-bit Unix systems working on x86- (and AMD64-) compatible processors. Building and work on other architectures have not been tested.

Compilation of Library and Examples

Unpack the downloaded distribution package, go to the resultant folder and run make:

        tar xzvf LibRaw-X.YY.tar.gz
        cd  LibRaw-X.YY
        make
    

As a result, you will compile

In the current version, only static libraries are built:

Compilation with LCMS (ICC profiles) support

For LCMS support one should uncomment two lines of Makefile after the line

# LCMS support
It is assumed, that LCMS library is installed in /usr/local/lib or /usr/lib, and lcms.h installed in /usr/local/include or /usr/include. If you have different LCMS installation path, you should edit Makefile settings.

OpenMP Support

For compilation with OpenMP support uncomennt line of Makefile next after the line

# OpenMP support

Installation and Usage

To install the library, run

    make install
    

It will place the libraries in /usr/local/lib and the include-files in /usr/local/include (subfolder of libraw).
To use LibRaw, add the following parameters to the compiler call (when building your own projects):

Installation of Examples

To install examples, run make install-binaries. This command will copy all compiled examples to /usr/local/bin.

Windows: Building under Cygwin

Building and installation are completely similar to building and installation under Unix systems.

Windows: Native Building

Building under Windows has three steps:

If all paths are set correctly and the include-files/libraries have been found, then the following will be compiled:

Only the thread-safe library is built under Win32, but it can be used with non-threaded applications as well. All examples are linked with the dynamic library (DLL); if static linking is necessary, one should link applications with library libraw_static.lib and set the preprocessor option /DLIBRAW_NODLL during compilation.

Windows-version compiles without LCMS support for now.

During building of DLL, all public functions are exported; further, the exported subset may be reduced.

Unfortunately, paths to include/ libraries depend on the way Visual C (or other compiler) is installed; therefore, it is impossible to specify some standard paths in Makefile.msvc.

Windows Installation

No installation under Windows is supported. It is assumed that all DLLs will be supplied together with the software using them (and this software will perform the installation). Accordingly, in building of programs using LibRaw, the paths to libraries, DLLs, and include-files should be specified manually.

Data Structures and Constants

LibRaw data structures are defined in header file libraw/libraw_types.h
Constants used in its work are defined in file libraw/libraw_const.h

Contents:

  1. Data structures
    1. libraw_data_t: Main Data Structure in LibRaw
    2. Structure libraw_iparams_t: Main Parameters of the Image
    3. Structure libraw_image_sizes_t: Image Dimensions
    4. Structure libraw_colordata_t: Color Information
    5. Structure color_data_state_t: Description of Color Data Source
    6. Structure libraw_imgother_t: Other Parameters of the Image
    7. Structure libraw_thumbnail_t: Description of Thumbnail
    8. Structure libraw_output_params_t: Management of dcraw-Style Postprocessing.
    9. Structure libraw_masked_t - black (masked) pixels data
    10. Stucture libraw_processed_image_t - result set for dcraw_make_mem_image()/dcraw_make_mem_thumb() functions
  2. Input abstraction layer
  3. Constants
    1. enum LibRaw_errors: Error Codes
    2. enum LibRaw_progress: Current State of LibRaw Object
    3. enum LibRaw_thumbnail_formats: Thumbnail Data Formats
    4. Nonstandard Situations (Warnings) during RAW Data Processing
    5. enum LibRaw_colorstate: Type of Color Data Source.
    6. enum LibRaw_filtering - RAW data filtering settings
    7. enum LibRaw_image_formats - possible types of data, contains in libraw_processed_image_t structure

Data Structures

libraw_data_t: Main Data Structure of LibRaw

Structure libraw_data_t is a "wrapping" for data structures accessible to the user of the library.
When one uses C++ API, it is accessible as LibRaw::imgdata (class_instance.imgdata). The data in this structure appear after a file is opened through open_file (and other open_ calls), except for the image itself (filled by unpack()) and data containing the preview information (filled by calling unpack_thumb()).
Data fields:

unsigned int progress_flags;
This field records the past phases of image processing.
unsigned int progress_flags;
This field records suspicious situations (warnings) that have emerged during image processing.
libraw_iparams_t idata;
The structure describes the main image parameters retrieved from the RAW file. Fields of this structure are described in detail below.
libraw_image_sizes_t sizes;
The structure describes the geometrical parameters of the image. Fields of this structure are described in detail below.
libraw_colordata_t color;
The structure contains color data retrieved from the file. Fields of this structure are described in detail below.
libraw_imgother_t other;
Data structure for information purposes: it contains the image parameters that have been extracted from the file but are not needed in further file processing. Fields of this structure are described in detail below.
libraw_thumbnail_t thumbnail;
Data structure containing information on the preview and the preview data themselves. All fields of this structure but thumbnail itself are filled when open_file() is called. Thumbnail readed by unpack_thumb() call. The fields are described in detail below.
libraw_masked_t masked_pixels;
Structure containing pixel data for black (masked) border pixels. Fields of this structure are described below. It is filled when unpack() is called.
ushort (*image)[4];
The memory area that contains the image pixels per se. It is filled when unpack() is called.
libraw_output_params_t params;
Data structure intended for management of image postprocessing (using the dcraw emulator). Fields of this structure are described in detail below.

Structure libraw_iparams_t: Main Parameters of the Image

char make[64];
Camera manufacturer.
char model[64];
Camera model.
unsigned raw_count;
Number of RAW images in file (0 means that the file has not been recognized).
unsigned dng_version;
DNG version (for the DNG format).
int colors;
Number of colors in the file.
unsigned filters;
Bit mask describing the order of color pixels in the matrix (0 for full-color images). 32 bits of this field describe 16 pixels (8 rows with two pixels in each, from left to right and from top to bottom). Each two bits have values 0 to 3, which correspond to four possible colors. Convenient work with this field is ensured by the COLOR(row,column) function, which returns the number of the active color for a given pixel.
char cdesc[5];
Description of colors numbered from 0 to 3 (RGBG,RGBE,GMCY, or GBTG).

Structure libraw_image_sizes_t: Image Dimensions

Structure libraw_image_sizes_t is a collection of all file data that describe the size of the image.
Data fields:

ushort raw_height, raw_width;
Full size of RAW image (including the frame) in pixels.
ushort height, width;
Size of visible ("meaningful") part of the image (without the frame).
ushort top_margin, left_margin;
Coordinates of the top left corner of the frame (the second corner is calculated from the full size of the image and size of its visible part).
ushort bottom_margin, right_margin;
Width (in pixels) of bottom and right part of masked pixels area.
ushort iheight, iwidth;
Size of the output image (may differ from height/width for cameras that require image rotation or have non-square pixels).
double pixel_aspect;
Pixel width/height ratio. If it is not unity, scaling of the image along one of the axes is required during output.
int flip;
Image orientation (0 if does not require rotation; 3 if requires 180-deg rotation; 5 if 90 deg counterclockwise, 6 if 90 deg clockwise).

Structure libraw_colordata_t: Color Information

Structure libraw_colordata_t unites all color data, both retrieved from the RAW file and calculated on the basis of the image itself. For different cameras, there are different ways of color handling.
Data fields:

color_data_state_t color_flags;
Data structure describing the sources of color data. Described below in more detail.
ushort white[8][8];
Block of white pixels extracted from files CIFF/CRW. Not extracted for other formats. Used to calculate white balance coefficients.
float cam_xyz[4][3];
Camera RGB - XYZ conversion matrix. This matrix is constant (different for different models). Last row is zero for RGB cameras and non-zero for different color models (CMYG and so on).
float cam_mul[4];
White balance coefficients (as shot). Either read from file or calculated.
float pre_mul[4];
White balance coefficients for daylight (daylight balance). Either read from file, or calculated on the basis of file data, or taken from hardcoded constants.
float cmatrix[3][4];
White balance matrix. Read from file for some cameras, calculated for others.
float rgb_cam[3][4];
Another white balance matrix, read from file for Leaf and Kodak cameras.
ushort curve[0x4001];
Camera tone curve, read from file for Nikon, Sony and some other cameras.
unsigned black;
Black level. Depending on the camera, it may be zero (this means that black has been subtracted at the unpacking stage or by the camera itself), calculated at the unpacking stage, read from the RAW file, or hardcoded.
unsigned maximum;
Maximum pixel value. Calculated from the data for most cameras, hardcoded for others. This value may be changed on postprocessing stage (when black subtraction performed) and by automated maximum adjustment (this adjustment performed if params.adjust_maximum_thr is set to nonzero).
unsigned channel_maximum[4];
Per channel maximum pixel value. Calculated from RAW data on unpack stage.
ph1_t phase_one_data;
Color data block that is read for Phase One cameras.
float flash_used;
float canon_ev;
Fields used for white balance calculations (for some P&S Canon cameras).
char model2[64];
Firmware revision (for some cameras).
void *profile;
Pointer to the retrieved ICC profile (if it is present in the RAW file).
unsigned profile_length;
Length of ICC profile in bytes.

Structure color_data_state_t: Description of Color Data Source

This structure (actually, a bit field) describes the source of color data for each field of structure libraw_colordata_t, which may be obtained from different data sources.
Data fields:

        unsigned curve_state        : 3;
        unsigned rgb_cam_state      : 3;
        unsigned cmatrix_state      : 3;
        unsigned pre_mul_state      : 3;
        unsigned cam_mul_state      : 3;
    

Each field assumes one of the values that are possible for enum LibRaw_colorstate.

Structure libraw_imgother_t: Other Parameters of the Image

Structure libraw_imgother_t is a collection of data that have been read from the RAW file but are not needed for image postprocessing.
Data fields:

float iso_speed;
ISO sensitivity.
float shutter;
Shutter speed.
float aperture;
Aperture.
float focal_len;
Focal length.
time_t timestamp;
Date of shooting.
unsigned shot_order;
Serial number of image.
unsigned gpsdata[32];
GPS data.
char desc[512];
Image description.
char artist[64];
Author of image.

Structure libraw_thumbnail_t: Description of Thumbnail

Structure libraw_thumbnail_t describes all parameters associated with the preview saved in the RAW file.
Data fields:

LibRaw_thumbnail_formats tformat;
Thumbnail data format. One of the values among enum LibRaw_thumbnail_formats.
ushort twidth, theight;
Dimensions of the preview image in pixels.
unsigned tlength;
Thumbnail length in bytes.
int tcolors;
Number of colors in the preview.
char *thumb;
Pointer to thumbmail, extracted from the data file.

Structure libraw_output_params_t: Management of dcraw-Style Postprocessing

Structure libraw_output_params_t is used for management of dcraw-compatible calls dcraw_process(), dcraw_ppm_tiff_writer(), dcraw_thumb_writer(), and dcraw_document_mode_processing(). Fields of this structure correspond to command line keys of dcraw.
Data fields:

unsigned greybox[4];
dcraw keys: -A x1 y1 x2 y2
4 numbers corresponding to the coordinates (in pixels) of the rectangle that is used to calculate the white balance.
double aber[4];
dcraw keys: -C
Correction of chromatic aberrations; the only specified values are
aber[0], the red multiplier
aber[2], the green multiplier. For some formats, it affects RAW data reading, since correction of aberrations changes the output size.
double gamm[6];
dcraw keys: -g power toe_slope
Sets user gamma-curve. Library user should set first two fields of gamm array:
gamm[0] - inverted gamma value)
gamm[1] - slope for linear part (so called toe slope). Set to zero for simple power curve.
Remaining 4 values are filled automatically.

By default settings for rec. BT.709 are used: power 2.222 (i.e. gamm[0]=1/2.222) and slope 4.5. For sRGB curve use gamm[0]=1/2.4 and gamm[1]=12.92, for linear curve set gamm[0]/gamm[1] to 1.0.

float user_mul[4];
dcraw keys: -r mul0 mul1 mul2 mul3
4 multipliers (r,g,b,g) of the user's white balance.
unsigned shot_select, multi_out;
dcraw keys: -s
Selection of image number for processing (for formats that contain several RAW images in one file). The multi_out ( -s all) mode should be programmed by the user, since dcraw_process() does not support it.
float bright;
dcraw keys: -b
Brightness (default 1.0).
float threshold;
dcraw keys: -n
Parameter for noise reduction through wavelet denoising.
int half_size;
dcraw keys: -h
Outputs the image in 50% size. For some formats, it affects RAW data reading.
int four_color_rgb;
dcraw keys: -f
Switches on separate interpolations for two green components.
int document_mode;
dcraw keys: -d/-D
0: standard processing (with white balance)
1: corresponds to -d (without color processing or debayer)
2: corresponds to -D (-d without white balance).
int highlight;
dcraw keys: -H
0-9: Highlight mode (0=clip, 1=unclip, 2=blend, 3+=rebuild).
int use_auto_wb;
dcraw keys: -a
Use automatic white balance obtained after averaging over the entire image.
int use_camera_wb;
dcraw keys: -w
If possible, use the white balance from the camera.
int use_camera_matrix;
dcraw keys: +M/-M
Use (1)/don't use camera color matrix.
int output_color;
dcraw keys: -o
[0-5] Output colorspace (raw, sRGB, Adobe, Wide, ProPhoto, XYZ).
char* output_profile;
dcraw keys: -o filename
Path to output profile ICC file (used only if LibRaw compiled with LCMS support)
char* camera_profile;
dcraw keys: -o file
Path to input (camera) profile ICC file (or 'embed' for embedded profile). Used only if LCMS support compiled in.
char* bad_pixels;
dcraw keys: -P file
Path to file with bad pixels map (in dcraw format: "column row date-of-pixel-death-in-UNIX-format", one pixel per row).
char* dark_frame;
dcraw keys: -K file
Path to dark frame file (in 16-bit PGM format)
enum LibRaw_filtering filtering_mode;
dcraw keys: none
Sets RAW data filtering mode (black level subtraction, zero pixels cleaning, tone curve processing) See details in filtration modes description and in mode list below.
int output_bps;
dcraw keys: -4
8 bit (default)/16 bit (key -4).
int output_tiff;
dcraw keys: -T
0/1: output PPM/TIFF.
int user_flip;
dcraw keys: -t
[0-7] Flip image (0=none, 3=180, 5=90CCW, 6=90CW). Default -1, which means taking the corresponding value from RAW.
For some formats, affects RAW data reading, e.g., unpacking of thumbnails from Kodak cameras.
int user_qual;
dcraw keys: -q
0-3: interpolation quality (0 - linear, 1- VNG, 2 - PPG, 3 - AHD).
int user_black;
dcraw keys: -k
User black level.
int user_sat;
dcraw keys: -S
Saturation adjustment.
int med_passes;
dcraw keys: -m
Number of median filter passes.
int no_auto_bright;
dcraw keys: -W
Don't use automatic increase of brightness by histogram.
float auto_bright_thr;
dcraw keys:none
Portion of clipped pixels when auto brighness increase is used. Default value is 0.01 (1%) for dcraw compatibility. Recommended value for modern low-noise multimegapixel cameras depends on shooting style. Values in 0.001-0.00003 range looks reasonable.
float adjust_maximum_thr;
dcraw keys:none
This parameters controls auto-adjusting of maximum value based on channel_maximum[] data, calculated from real frame data. If calculated maximum is greater than adjust_maximum_thr*maximum, than maximum is set to calculated_maximum.
Default: 0.75. If you set this value above 0.99999, than default value will be used. If you set this value below 0.00001, than no maximum adjustment will be performed.
Adjusting maximum should not damage any picture (esp. if you use default value) and is very useful for correcting channel overflow problems (magenta clouds on landscape shots, green-blue highlights for indoor shots).
int use_fuji_rotate;
dcraw keys: -j
Default -1 (use), 0 - don't use rotation for cameras on a Fuji sensor.
char *bpfile;
dcraw keys: -P
Name of file with bad pixel map.
char *dark_frame;
dcraw keys: -K
Name of file with dark frame.

Structure libraw_masked_t - saved masked pixels (black frame) data

Structure libraw_masked_t designed for storing pixel data for dark frame (black or masked pixels, not included in active image sensor area). These pixel values can be used for black level subtraction, noise and banding removal and so on.
Unlike imgdata.image bitmap which has 4-component pixels, masked pixel data stored in 1-component 16-bit values. Different parts of border are stored in different buffers within libraw_masked_t structure, see picture:

Data fields:

ushort *buffer;
Whole allocated buffer. Buffer size is (raw_width*raw_height - width*height).
ushort *tl;
Pointer to part of buffer designated for storing top-left corner of black frame. Data size equal to (top_margin*left_margin).
ushort *top;
Pointer to part of buffer for storing top part of black frame. Size is (top_margin*width).
ushort *tr;
Pointer to right top corned data. Size is (top_margin*right_margin).
ushort *left;
Pointer to pixel data of left frame side. (left_margin*height) pixels.
ushort *right;
Right side of frame. (right_margin*height) pixels.
ushort *bl;
Bottom left corner of frame. (bottom_margin*left_margin) pixels.
ushort *bottom;
Bottom side of frame. Pixel count is (bottom_margin*width).
ushort *br;
Bottom right corner with (bottom_margin*right_margin) pixels.
ushort (*ph1_black)[2];
Buffer containing black level data, read from RAW file. Each item contains two elements, one for left half-row, one for right half row. Number of items is equal to row count.

Some cameras does not provide dark frame data. In this case buffer for frame data is not allocated and all pointers are initialized to zero. Also, structure data is not allocated if image is extracted into half-sized bitmap (i.e. if half_size, wavelet threshold or aber[] fields is set in processing options).
Some cameras provides not full masked frame, but only several sides of it (only left and top for Canons, only left and right for some Nikons and so on). In this case all structure fields are initialized, but allocated size for this part of frame is equal to zero and corresponding size parameter (top_/left_/bottom_/right_margin) is set too zero too.

Stucture libraw_processed_image_t - result set for dcraw_make_mem_image()/dcraw_make_mem_thumb() functions

Structure libraw_processed_image_t is produced by call of dcraw_make_mem_image()/dcraw_make_mem_thumb() and contains in-memory image of interpolated data or thumbnail.
Data fields:

LibRaw_image_formats type
This field records type of data, containing in remaining fields of structure.
  • LIBRAW_IMAGE_BITMAP - structure contains RGB bitmap. All metadata fields (see below) are valid and describes image data.
  • LIBRAW_IMAGE_JPEG - structure contain in-memory image of JPEG file. Only type, data_size and data fields are valid (and nonzero);
ushort height,width
Image size (in pixels). Valid only if type==LIBRAW_IMAGE_BITMAP.
ushort colors, bits
Number of colors components (1 or 3) and color depth in bits (8 or 16). These fields are valid only if type==LIBRAW_IMAGE_BITMAP.
ushort gamma_corrected
Is bitmap data gamma-corrected (always 1 for 8-bit data, may be 0 or 1 for 16-bit). Valid only if type==LIBRAW_IMAGE_BITMAP.
unsigned int data_size
Size of data field (in bytes). For bitmap image equal to (height*width*colors * (bits/8)). For JPEG image - exact JPEG size (i.e. extracted thnumbnail size + JPEG header + EXIF header).
unsigned char data[]
Data array itself. Should be interpreted as RGB triplets for bitmap type and as JPEG file for JPEG type.

Input abstraction layer

RAW data input (read) in LibRaw implemented by calling methods of object derived from LibRaw_abstract_datastream abstract class. Full list of methods is described in href="API-CXX-eng.html#datastream">C++ API reference.

There is two ready to use implementations of datastream objects:

LibRaw user can create own datastream object derived from LibRaw_abstract_datastream. For example, such object may implement reading RAW data directly from camera (by remote interface). LibRaw can use these objects via LibRaw::open_datastream() interface.

Datastreams can be used either via LibRaw::open_datastream() call (in this case datastream object should be created an maintained by user) or via LibRaw::open_file() and LibRaw::open_buffer() shortcuts.

Only C++ API users may use object-oriented interface and implement own input interfaces. For C API users only built-on libraw_open_file()/libraw_open_buffer() shortcuts are avaliable.

Data fields

Definition:

class LibRaw_abstract_datastream {
...
protected:
    LibRaw_abstract_datastream *substream;
}

Description: Ojects derived from LibRaw_abstract_datastream always contains pointer to secondary data stream (substream). This substream initialized internally when needed (really used only for Sony RAW data) and used for temporary switch input stream to temporary memory buffer allocated internally in LibRaw.

Substream usage details described more precisely in own datastream objects creation guide.

Constants

enum LibRaw_errors: Error Codes

All functions returning integer numbers must return either errno or one of the following error codes (see also error code conventions).

Fatal errors (return of such an error code implies that file processing has to be terminated, since the state of data structures is unknown).

LIBRAW_UNSUFFICIENT_MEMORY
Attempt to get memory from the system has failed.
All allocated resources will be freed, recycle() will be called, and the LibRaw object will be brought to the state "right after creation."
LIBRAW_DATA_ERROR
A fatal error emerged during data unpacking.
All allocated resources will be freed, recycle() will be called, and the LibRaw object will be brought to the state "right after creation."
LIBRAW_IO_ERROR
A fatal error emerged during file reading (premature end-of-file encountered or file is corrupt).
All allocated resources will be freed, recycle() will be called, and the LibRaw object will be brought to the state "right after creation."
LIBRAW_CANCELLED_BY_CALLBACK
Processing cancelled due to calling application demand (by returning nonzero code from progress callback).
All allocated resources will be freed, recycle() will be called, and the LibRaw object will be brought to the state "right after creation."

Non-Fatal Errors

LIBRAW_SUCCESS=0
No error; function terminated successfully.
LIBRAW_UNSPECIFIED_ERROR
An unknown error has been encountered. This code should never be generated.
LIBRAW_FILE_UNSUPPORTED
Unsupported file format (attempt to open a RAW file with a format unknown to the program).
LIBRAW_REQUEST_FOR_NONEXISTENT_IMAGE
Attempt to retrieve a RAW image with a number absent in the data file (only for formats supporting storage of several images in a file).
LIBRAW_OUT_OF_ORDER_CALL
API functions have been called in wrong order (e.g., unpack() before open_file()) or the previous stage has ended with an error (e.g., unpack() is called after open_file() has returned an error).
LIBRAW_NO_THUMBNAIL
Returned upon an attempt to retrieve a thumbnail from a file containing no preview.
LIBRAW_UNSUPPORTED_THUMBNAIL
RAW file contains a preview of unsupported format.

enum LibRaw_progress: Current State of LibRaw Object

LibRaw::imgdata.progress_flags contains a bit mask describing all stages of file processing that have already been performed.

File opening and RAW data extraction phase.

LIBRAW_PROGRESS_START=0
Object just created, no processing carried out.
LIBRAW_PROGRESS_OPEN
File to be processed has been opened.
LIBRAW_PROGRESS_IDENTIFY
Data identification performed, format recognized, metadata extracted.
LIBRAW_PROGRESS_SIZE_ADJUST
Data sizes adjusted (for files that require such adjustment, namely, certain files from Kodak cameras).
LIBRAW_PROGRESS_LOAD_RAW
RAW data loaded.

The following flags are set during usage of image processing that has been taken from dcraw.

LIBRAW_PROGRESS_REMOVE_ZEROES
Zero values removed for cameras that require such removal (Panasonic cameras).
LIBRAW_PROGRESS_BAD_PIXELS
Bad (dead) pixels removed.
LIBRAW_PROGRESS_DARK_FRAME
Dark frame subtracted from RAW data.
LIBRAW_PROGRESS_SCALE_COLORS
White balance performed.
LIBRAW_PROGRESS_PRE_INTERPOLATE
Image size reduction (for the half_size mode) performed, as well as copying of 2nd green channel to the 1st one in points where the second channel is present and the first one is absent.
LIBRAW_PROGRESS_INTERPOLATE
Interpolation (debayer) performed.
LIBRAW_PROGRESS_MIX_GREEN
Averaging of green channels performed.
LIBRAW_PROGRESS_MEDIAN_FILTER
Median filtration performed.
LIBRAW_PROGRESS_HIGHLIGHTS
Work with highlights performed.
LIBRAW_PROGRESS_FUJI_ROTATE
For images from Fuji cameras, rotation performed (or adjust_sizes_info_only() called).
LIBRAW_PROGRESS_FLIP
Dimensions recalculated for images shot with a rotated camera (sizes.iwidth/sizes.iheight swapped).
LIBRAW_PROGRESS_CONVERT_RGB
Conversion into output RGB space performed.
LIBRAW_PROGRESS_STRETCH
Image dimensions changed for cameras with non-square pixels.
LIBRAW_PROGRESS_STAGE17 - LIBRAW_PROGRESS_STAGE27
Reserved for possible appearance of other processing stages.

The following flags are set during loading of thumbnails.

LIBRAW_PROGRESS_THUMB_LOAD
Thumbnail data have been loaded (for Kodak cameras, the necessary conversions have also been made).
LIBRAW_PROGRESS_TRESERVED1 - LIBRAW_PROGRESS_TRESERVED3
Reserved for possible future processing stages.

enum LibRaw_thumbnail_formats: Thumbnail Data Formats

Thumbnail data format is written in the imgdata.thumbnail.tformat data field.
Presently LibRaw knows about four thumbnail formats, among which two are unpacked:

LIBRAW_THUMBNAIL_UNKNOWN
Format unknown or thumbnail not yet read.
LIBRAW_THUMBNAIL_JPEG
The thumbnail buffer contains a JPEG file (read from the RAW file "as is," without any manipulations performed on it).
LIBRAW_THUMBNAIL_BITMAP
The thumbnail buffer contains the gamma-adjusted RGB bitmap (for Kodak cameras, the gamma correction is performed with allowance for maximum values and the white balance is set in accordance with the camera settings).
In this format, each pixel of the image is represented by a 8-bit RGB triplet.
LIBRAW_THUMBNAIL_LAYER
Data format is presently recognized upon opening of RAW file but not supported: not unpacked into LibRaw::unpack_thumb.
LIBRAW_THUMBNAIL_ROLLEI
Data format is presently recognized upon opening of RAW file but not supported: not unpacked into LibRaw::unpack_thumb.

Nonstandard Situations (Warnings) during RAW Data Processing

Some suspicious situations emerging during image processing are not fatal but may affect the result of data retrieval or postprocessing. Such states are indicated by setting a bit in the imgdata.process_warnings field.

LIBRAW_WARN_BAD_CAMERA_WB
Postprocessing must use white balance of the camera but this balance is not suitable for use.
LIBRAW_WARN_NO_METADATA
Only for cameras where the metadata are taken from an external JPEG file: metadata extraction has failed.
LIBRAW_WARN_NO_JPEGLIB
Only for P&S Kodak cameras: data in JPEG format. At the same time, open_file() will return LIBRAW_FILE_UNSUPPORTED.
LIBRAW_WARN_NO_EMBEDDED_PROFILE
(only if LCMS support compiled in). Caller set embedded input profile use, but no such profile exists in RAW.
LIBRAW_WARN_NO_INPUT_PROFILE
(only if LCMS support compiled in). Error when opening input profile ICC file.
LIBRAW_WARN_BAD_OUTPUT_PROFILE
(only if LCMS support compiled in). Error when opening output profile ICC file.
LIBRAW_WARN_NO_BADPIXELMAP
Error when opening bad pixels map file.
LIBRAW_WARN_BAD_DARKFRAME_FILE
Error when opening dark frame file.
LIBRAW_WARN_BAD_DARKFRAME_DIM
Dark frame file either differs in dimensions from RAW-file processed, or have wrong format. Dark frame should be in 16-bit PGM format (one can generate it using simple_dcraw -4 -D).

enum LibRaw_filtering - RAW data filtering settings

This enum describes possible RAW data filtration modes during data unpacking and postprocessing. Usage recommendations are described in API comments. Filtering mode should be set by altering filtering_mode option before unpack() is called.

LIBRAW_FILTERING_DEFAULT
Default value: dcraw filtration mode (for compatibility with previous LibRaw versions without data filtering options).
LIBRAW_FILTERING_NOZEROES
If this bit set, then zero values in RAW data will not be cleared (averaged). Zero averaging is needed for some cameras, such as Point-and-Shot Canon cameras, Panasonic cameras and some other.
LIBRAW_FILTERING_NOBLACK
If this bit is set, then no black level subtraction will be done on unpacking stage (for cameras with black subtraction on unpacking stage: e.g. Canon cameras). Also, if this bit is set, then automatically calculated black level (this value is subtracted on postprocessing stage) is set to zero. This bit does not affects black subtraction set via user_black option.
LIBRAW_FILTERING_NORAWCURVE
This bit turns off tone curve processing (for tone curves read from file metadata or calculated from constants). This setting is supported only for bayer-pattern cameras with tone curve;
LIBRAW_FILTERING_NONE
Equal to (LIBRAW_FILTERING_NOZEROES|LIBRAW_FILTERING_NOBLACK|LIBRAW_FILTERING_NORAWCURVE)
LIBRAW_FILTERING_LIBRAWOWN
This bit turns on LibRaw specialized functions for data filtering. These funcions will be made individually for each camera model (and, possibly, firmware version). For now, no such subroutines ready, so this parameter is reserved for future.
LIBRAW_FILTERING_AUTOMATIC
Equals to LIBRAW_FILTERING_LIBRAWOWN with fallback to LIBRAW_FILTERING_DEFAULT if no specialized filtering function exists for given camera.
LIBRAW_FILTERING_AUTOMATIC_BIT
This bit reserved for LIBRAW_FILTERING_AUTOMATIC support.

Best possible setting for common RAW-processing software is LIBRAW_FILTERING_AUTOMATIC. For RAW analyzers we recommend use LIBRAW_FILTERING_NONE or LIBRAW_FILTERING_NOBLACK.

enum LibRaw_colorstate: Types of Color Data Source

For each type of retrieved color information (see above for description of structure imgdata.color.color_flags), the data source is recorded. The possible values are listed below.

LIBRAW_COLORSTATE_UNKNOWN
Data source unknown.
LIBRAW_COLORSTATE_INIT
Data field initialized by default (the same value for all cameras) before opening the RAW file.
LIBRAW_COLORSTATE_CONST
Data source is a hardcoded constant.
LIBRAW_COLORSTATE_LOADED
Data loaded from RAW file.
LIBRAW_COLORSTATE_CALCULATED
Data calculated on the basis of RAW data.
LIBRAW_COLORSTATE_RESERVED1-LIBRAW_COLORSTATE_RESERVED3
Reserved.

enum LibRaw_image_formats - possible types of data, contains in libraw_processed_image_t structure

type field of libraw_processed_image_t structure may have one of these values:

LIBRAW_IMAGE_BITMAP
The structure contains RGB-bitmap, metadata described in other fields of libraw_processed_image_t.
LIBRAW_IMAGE_JPEG
libraw_processed_image_t structure contains JPEG image (in memory). Only data_size field is meaningful.

C++ API

Contents

  1. LibRaw Objects
  2. Returned values
  3. Methods Loading Data from a File
  4. Auxiliary Functions
  5. Data Postprocessing: Emulation of dcraw Behavior
  6. Data Output to Files: Emulation of dcraw Behavior
  7. Copying unpacked data into memory buffer
  8. Input layer abstraction

LibRaw Objects

The main LibRaw object (class) is created either without parameters or with flags determining the object behavior.

#include "libraw/libraw.h"
...

   LibRaw ImageProcessor(unsigned int flags=0);
...
    

Flags (several flags are combined via operator |, i.e., bitwise OR):

  • LIBRAW_OPTIONS_NO_MEMERR_CALLBACK: do not set the standard out-of-memory error handler (standard handler outputs the error report in stderr);
  • LIBRAW_OPTIONS_NO_DATAERR_CALLBACK: do not set the standard file read error handler (standard handler outputs the error report in stderr).

Three groups of methods are used for image processing

The results of processing are placed in the imgdata field of type libraw_data_t; the same data set contains fields that control the postprocessing and output.

Returned Values

All LibRaw API functions return an integer number in accordance with the return code convention. Please read the descriptions of this convention and LibRaw behavior in cases of fatal errors.

Methods Loading Data from a File

int LibRaw::open_datastream(LibRaw_abstract_datastream *stream)

Opens a datastream with RAW data, reads metadata (EXIF) from it, and fills the following structures:

The function returns an integer number in accordance with the return code convention: positive if any system call has returned an error, negative (from the LibRaw error list) if there has been an error situation within LibRaw.

Before file opening, recycle() is always called; hence, if several images are processed in the batch mode, there is no need to call recycle() at the end of each processing cycle.

Input data: pointer to object, derived from LibRaw_abstract_datastream class. This object should be initialized and ready to read. This object should be destroyed in calling application after use.

int LibRaw::open_file(const char *filename)

Creates an LibRaw_file_datastream object, calls open_datastream(). If succeed, sets internal flag which signals to destroy internal datastream object on recycle(). On failure, just created file_datastream destroyed immediately.

The function returns an integer number in accordance with the return code convention: positive if any system call has returned an error, negative (from the LibRaw error list) if there has been an error situation within LibRaw.

int LibRaw::open_buffer(void *buffer, size_t bufsize)

Created an LibRaw_buffer_datastream object, calls open_datastream(). If succeed, sets internal flag which signals to destroy internal datastream object on recycle(). On failure, just created file_datastream destroyed immediately.

The function returns an integer number in accordance with the return code convention: positive if any system call has returned an error, negative (from the LibRaw error list) if there has been an error situation within LibRaw.

int LibRaw::unpack(void)

Unpacks the RAW files of the image, calculates the black level (not for all formats), subtracts black (not for all formats). The results are placed in imgdata.image.

Data reading is sometimes (not frequently) affected by settings made in imgdata.params (libraw_output_params_t); see API notes for details.

The function returns an integer number in accordance with the return code convention: positive if any system call has returned an error, negative (from the LibRaw error list) if there has been an error situation within LibRaw.

int LibRaw::unpack_thumb(void)

Reads (or unpacks) the image preview (thumbnail), placing the result into the imgdata.thumbnail.thumb buffer.
JPEG previews are placed into this buffer without any changes (with the header etc.). Other preview formats are placed into the buffer in the form of the unpacked bitmap image (three components, 8 bits per component).
The thumbnail format is written to the imgdata.thumbnail.tformat field; for the possible values, see description of constants and data structures.

The function returns an integer number in accordance with the return code convention: positive if any system call has returned an error, negative (from the LibRaw error list) if there has been an error situation within LibRaw.

Auxiliary Functions

Library version check

const char* LibRaw::version()

Returns string representation of LibRaw version in MAJOR.MINOR.PATCH-Status format (i.e. 0.6.0-Alpha2 or 0.6.1-Release).

int LibRaw::versionNumber()

Returns integer representation of LibRaw version. During LibRaw development, the version number is always increase .

bool LIBRAW_CHECK_VERSION(major,minor,patch)

Macro for version check in caller applications. Returns 'true' if current library version is greater or equal to set in macro parameters. This macro executes at runtime (not at compile time) and may be used for checking version of dynamically loaded LibRaw (from DLL/shared library).

List of supported RAW formats (cameras)

int LibRaw::cameraCount()

Returns count of cameras supported.

const char** LibRaw::cameraList()

Returns list of supported cameras. Latest item of list is set to NULL (for easy printing).

const char* LibRaw::unpack_function_name()

Returns function name of file unpacking function. Intended only for LibRaw test suite designers to use in test coverage evaluation.

void LibRaw::add_masked_borders_to_bitmap

This call rebuilds imgdata.image bitmap and makes full bitmap, contains both image data (active pixels) and 'black border' (masked pixels). If no masked pixels data present, then this call does nothing. Function call changes data fields sizes.width,sizes.height,sizes.iwidth,sizes.iheight to full image size (i.e. raw_width/raw_height).
This call does nothing:

  • for images containing more than one color component per pixel (full-color DNG, already demosaiced files: Canon sRAW, Sinar 4-shot etc).
  • for images, unpacked with half resolution (one of parameters set in options: half_mode, wavelet denoising threshold, aberration fixing data).

The function returns an integer number in accordance with the return code convention: positive if any system call has returned an error, negative (from the LibRaw error list) if there has been an error situation within LibRaw.

int LibRaw:: rotate_fuji_raw()

This function rotates non-interpolated RAW bitmap from FujiCCD sensors into 45-degree-rotated position, compatible with old LibRaw versions (prior to LibRaw 0.7). This call is intended for applications, which works with non-interpolated/non-demosaiced RAW data itself. If dcraw_process/dcraw_document_mode_processing calls are used, these call make the rotation internally.

The function returns an integer number in accordance with the return code convention: positive if any system call has returned an error, negative (from the LibRaw error list) if there has been an error situation within LibRaw.

void LibRaw::recycle(void)

Frees the allocated data of LibRaw instance, enabling one to process the next file using the same processor. Repeated calls of recycle() are quite possible and do not conflict with anything.

LibRaw::~LibRaw()

Destructor, which consists in calling recycle().

const char* LibRaw::strprogress(enum LibRaw_progress code)

Converts progress stage code to description string (in English).

const char* LibRaw::strerror(int errorcode)

Analog of strerror(3) function: outputs the text descriptions of LibRaw error codes (in English).

Setting Error Notification Functions

In process of RAW conversion LibRaw can call user-setted callback. This callback can be used for:

  • Dynamic status update (progress bar and so on).
  • Cancel of processing (for example, user pressed Cancel button).

Also, work of the library may cause two types of exceptional situations that require notification of the calling application:

  • Memory shortage
  • Data read error.

An application may set its own callbacks that will be called in the cases mentioned above to notify the user (or the calling program).

Progress indication/processing termination

        typedef int (*progress_callback)(void *callback_data,enum LibRaw_progress stage, int iteration, int expected);
        void LibRaw::set_progress_handler(progress_callback func,void *callback_data);
    

LibRaw user can set own callback which will be called 10-50 times during RAW postprocessing by dcraw_process()/dcraw_document_mode_processing();

This callback may terminate current image processing by returning of non-zero value. In such case all processing will be cancelled immediately and all resources will be returned to system by recycle() call. Current call of dcraw_process()/dcraw_document_mode_processing() will return error code LIBRAW_CANCELLED_BY_CALLBACK.

Callback parameters:

void *callback_data
void*-pointer, passed as 2nd argument to set_progress_handler(). This pointer should be used to pass additional data to callback (i.e. thread local data and so on).
enum LibRaw_progress stage
Current processing stage. This number can be converted to string by call to LibRaw::strprogress. Not all processing stages are covered by callback calls.
int iteration
Iteration number within current stage (from 0 to expected-1).
int expected
Expected number of iterations on current stage.
Callback should return value of: 0 for continue processing and non-zero for immediate cancel of processing.

If LibRaw compiled with OpenMP support, iteration parameter may not always increase within one stage. Out of order callback calls are possible.

Callback code sample:

int my_progress_callback(void *data,enum LibRaw_progress p,int iteration, int expected)
{
    char *passed_string = (char *data);
    printf("Callback: %s  pass %d of %d, data passed: %s\n",libraw_strprogress(p),iteration,expected,passed_string);
    if(timeout || key_pressed )
        return 1; // cancel processing immediately
    else
        return 0; // can continue
}

Out-of-Memory Notifier

        typedef void (* memory_callback)(void *callback_data,const char *file, const char *where);
        void LibRaw::set_memerror_handler(memory_callback func,void *callback_data);
    

The user may set his or her own function called in the case of memory shortage. It is a void function receiving two string parameters:

  • void *callback_data - void*-pointer, passed as 2nd argument to set_progress_handler(). This pointer should be used to pass additional data to callback (i.e. thread local data and so on).
  • file is the name of the RAW file whose processing evoked the out-of-memory error. This name can be NULL if underlying data input layer does not know the name. So, if calling application sets own callback, this callback should work with NULL file name.
  • where is the name of the function where memory shortage occurred.

The callback function is intended for information purposes: it notifies the user or the program code that processing is impossible.

If the user does not set his or her own handler, the standard one (output of error message in stderr) will be used.

One can set the null handler by passing NULL to set_memerror_handler; then no notifier function will be called. The same effect can be achieved by creating a LibRaw object with the LIBRAW_OPTIONS_NO_MEMERR_CALLBACK flag in the contructor.

In the case of memory shortage, processing of the current file is terminated and a notifier is called; all allocated resources are freed, and recycle() is performed. The current call will return LIBRAW_UNSUFFICIENT_MEMORY.
At an attempt to continue data processing, all subsequent calls will return LIBRAW_OUT_OF_ORDER_CALL. Processing of a new file may be started in the usual way, by calling LibRaw::open_file().

File Read Error Notifier

        typedef void (*data_callback)(void *callback_data,const char *file, const int offset);
        void LibRaw::set_dataerror_handler(data_callback func, void *callback_data); 
    

The user can define his or her own function to be called in the case of error in the input data. It is a void function receiving two parameters:

  • void *callback_data - void*-pointer, passed as 2nd argument to set_progress_handler(). This pointer should be used to pass additional data to callback (i.e. thread local data and so on).
  • file is the name of the RAW file whose processing evoked the file read error. This name can be NULL if underlying data input layer does not know the name. So, if calling application sets own callback, this callback should work with NULL file name.
  • offset is -1 at end-of-file (if LibRaw expects more data) or a positive number equal to the file position (bytes from file beginning) where the unpacking error occurred.

The callback function is intended for information purposes: it notifies the user or the program code that processing is impossible.

If the user does not set his or her own handler, the standard one (output of error message in stderr) will be used.

One can set the null handler by passing NULL to set_memerror_handler; then no notifier function will be called. The same effect can be achieved by creating a LibRaw object with the LIBRAW_OPTIONS_NO_DATAERR_CALLBACK flag in the contructor.

In the case of error in the input data, processing of the current file is terminated and a notifier is called; all allocated resources are freed, and recycle() is performed. The current call will return LIBRAW_IO_ERROR.
At an attempt to continue data processing, all subsequent calls will return LIBRAW_OUT_OF_ORDER_CALL. Processing of a new file may be started in the usual way, by calling LibRaw::open_file().

Data Postprocessing: Emulation of dcraw Behavior

Instead of writing one's own Bayer pattern postprocessing, one can use the dcraw functions, which are called after the calls of open_file() + unpack() /+ unpack_thumb()/

Parameter Setting

Virtually all parameters that can be set through the dcraw command line are specified by assigning values to fields of the LibRaw::imgdata.params structure. The type of this structure is libraw_output_params_t; all fields are listed and described in sufficient detail in the description of data structures.

int LibRaw::adjust_sizes_info_only(void)

The function calculates the correct size of the output image (imgdata.sizes.iwidth and imgdata.sizes.iheight) for the following cases:

  • Files from Fuji cameras (with a 45-degree rotation)
  • Files from cameras with non-square pixels
  • Images shot by a rotated camera.

In the aforementioned cases, the function changes the fields of the image output size; note that this change cannot be repeated again.

The function should be used for information purposes; it is incompatible with dcraw_document_mode_processing() and dcraw_process() calls.

int LibRaw::dcraw_document_mode_processing(void)

The function emulates dcraw -D: no interpolation, white balance, and color conversion.
It is called after calling LibRaw::unpack();

The function returns an integer number in accordance with the error code convention: positive if any system call has returned an error, negative (from the LibRaw error list) if there has been an error situation within LibRaw.

int LibRaw::dcraw_process(void)

The function emulates the postprocessing capabilities available in dcraw.
Called after calling LibRaw::unpack();

The entire functionality of dcraw (set via the field values in imgdata.params) is supported, except for

  • Dark frame subtraction
  • Work with bad pixels.

The function is intended solely for demonstration and testing purposes; it is assumed that its source code will be used in most real applications as the reference material concerning the order of RAW data processing.

The function returns an integer number in accordance with the error code convention: positive if any system call has returned an error, negative (from the LibRaw error list) if there has been an error situation within LibRaw.

Data Output to Files: Emulation of dcraw Behavior

In spite of the abundance of libraries for file output in any formats, LibRaw includes calls that emulate the file output provided by dcraw. This is done primarily for easier verification of library work: the resultant files must be binary identical.

int LibRaw::dcraw_ppm_tiff_writer(const char *outfile)

The function outputs the postprocessing results to a file in the PPM/PGM or TIFF format (the format is set via imgdata.params.output_tiff). The results are binary identical to those provided by dcraw.

The function returns an integer number in accordance with the error code convention: positive if any system call has returned an error, negative (from the LibRaw error list) if there has been an error situation within LibRaw.

int LibRaw::dcraw_thumb_writer(const char *thumbfile)

Writes the thumbnail to a file in the PPM format for bitmap thumbnails and in the JPEG format for JPEG thumbnails, i.e., in the format completely identical to the results provided by dcraw.

The function returns an integer number in accordance with the error code convention: positive if any system call has returned an error, negative (from the LibRaw error list) if there has been an error situation within LibRaw.

Copying unpacked data into memory buffer

There is two function calls for store unpacked data into memory buffer (after using dcraw_process() and so on):

  • dcraw_make_mem_image - store processed image data into allocated buffer;
  • dcraw_make_mem_thumb - store extracted thumbnail into buffer as JPEG-file image (for most cameras) or as RGB-bitmap.
For usage primer see samples/mem_image.c sample.

libraw_processed_image_t *dcraw_make_mem_image(int *errorcode=NULL) - store unpacked and processed image into memory buffer as RGB-bitmap

This function allocates memory buffer and stores unpacked-preprocessed image into this buffer. Function returns allocated structure libraw_processed_image_t with filled fields. Always returns data as RGB bitmap (i.e. type field is equal to LIBRAW_IMAGE_BITMAP).

dcraw_process() or dcraw_document_mode_processing() should be called before dcraw_make_mem_image();

Returns NULL in case of an error. If caller has passed not-NULL value as errorcode parameter, than *errorcode will be set to error code according to error code convention.

NOTE! Memory, allocated for return value will not be fried at destructor or LibRaw::recycle calls. Caller of dcraw_make_mem_image should free this memory by call to free().

libraw_processed_image_t *dcraw_make_mem_thumb(int *errorcode=NULL) - store unpacked thumbnail into memory buffer

This function allocates memory buffer and stores thumbnail data in it. Function returns allocated structure libraw_processed_image_t with filled fields. For most RAW images allocated structure will contains JPEG image (i.e. type field is equal to LIBRAW_IMAGE_JPEG). For some cameras with RGB-bitmap thumbnail (Kodak SLRs) returned structure contains RGB bitmap (type field is equal to LIBRAW_IMAGE_JPEG, see structure description for details).

unpack_thumb() should be called before dcraw_make_mem_thumb();

Returns NULL in case of an error. If caller has passed not-NULL value as errorcode parameter, than *errorcode will be set to error code according to с error code convention.

NOTE! Memory, allocated for return value will not be fried at destructor or LibRaw::recycle calls. Caller of dcraw_make_mem_image should free this memory by call to free().

Input layer abstraction

class LibRaw_abstract_datastream - abstract RAW read interface

LibRaw reads RAW-data by calling (virtual) methods of C++ object derived from LibRaw_abstract_datastream. This C++ class does not implement any read, but defines interface to be called. Call to base class methods always results in error.

LibRaw_abstract_datastream class methods

Object verification
virtual int valid()
Checks input datastream validity. Returns 1 on valid stream and 0 if datastream was created on non-valid input parameters (wrong filename for file stream and so on).
Stream read and positioning

This group of methods implements file object (FILE*) semantics.

virtual int read(void * ptr,size_t size, size_t nmemb)
Similar to fread(ptr,size,nmemb,file).
virtual int seek(off_t o, int whence)
Similar to fseek(file,o,whence).
virtual int tell(
Similar to ftell(file).
virtual int get_char()
Similar to getc(file)/fgetc(file).
virtual char* gets(char *s, int n)
Similar to fgets(s,n,file).
virtual int eof()
Similar to feof(file).
virtual int scanf_one(const char *fmt, void *val)
Simplified variant of fscanf(file,fmt,val): format string is always contains one argument to read. So, variable args call is not needed and only one pointer to data passed.
Other methods

This group of methods includes several supplementary calls. These calls are used to temporary switch to another data stream (file and/or memory buffer).

virtual const char* fname()
Returns name of opened file if datastream object knows it (for example, LibRaw_file_datastream used). Filename used in:
  • error notofocation callbacks;
  • generation of filename of JPEG-file with metadata when needed (i.e. cameras with 'Diag RAW hack').
virtual int subfile_open(const char *fn)
This call temporary switches input to file fn. Returns 0 on success and error code on error.
The function used to read metadata from external JPEG file (on cameras with "Diag RAW hack").
This call is not implemented for LibRaw_buffer_datastream, so external JPEG processing is not possible when buffer datastream used.
This functon should be implemented in real input class, base class call always return error.
Working implementation sample can be found in LibRaw_file_datastream implementation in libraw/libraw_datastream.h file.
virtual void subfile_close()
This call switches input stream from temporary open file back to main data stream.
virtual int tempbuffer_open(void *buf, size_t size)
This call temporary switches input to LibRaw_buffer_datastream object, created from buf.
This method is needed for Sony encrypted metadata parser.

This call implemented in base class (LibRaw_abstract_datastream), there is no need to reimplement in in derived classes.
Possible activity of temporary datastream requires very accurate programming when implementing datastreams derived from base LibRaw_abstract_datastream. See below for more details.

virtual void tempbuffer_close()
This call switch input back from temporary datastream to main stream. This call implemented in base LibRaw_abstract_datastream class.

Derived input classes included in LibRaw

There is two "standard" input classes in LibRaw distribution:

LibRaw C++ interface users can implement their own input classes and use them via LibRaw::open_datastream call. Requirements and implementation specifics are described below.

class LibRaw_file_datastream - file input interface

This class implements input from file.

Class methods:

LibRaw_file_datastream(const char *fname)
This constructor creates LibRaw_file_datastream object from file fname.
Unfortunately, C++ constructor cannot return an error. So if bad filename passed (e.g. nonexistent file) object is created as non-valid (valid() call returns zero).

All other class methods are described above.
This class implements all possble methods, including fname() and subfile_open().

class LibRaw_buffer_datastream - memory buffer input interface

This class implements input from memory buffer.

Class methods:

LibRaw_buffer_datastream(void *buffer, size_t bsize)
This constructor creates datastream object from buffer with size bsize.
It is not possibly to verify the pointer passed, so buffer address is checked against 0 and -1 only.

All other class methods are described above.
This class does not implement fname() and subfile_open() calls, so external JPEG metadata parsing is not possible.

Own datastream derived classes

To create own read interface LibRaw user should implement C++ class derived from LibRaw_abstract_datastream with all read methods.
LibRaw standard implementations may be used as reference. See libraw/libraw_datastream.h file for details (all standard LibRaw input classes are implemented using inline functions only).

substream field: secondary input stream

substream field, defined in base LibRaw_abstract_datastream class requires some additional notes. This field is used when input switched to temporary buffer (used for Sony metadata processing). Unfortunately, there is now ideal C++ way to hide this functionality into base class internals. So, any read call in derived class should include 1-line statement like this:
int method(...args...){ if(substream) return substream->method(...args...). For example:

    virtual int eof() 
    { 
        if(substream) return substream->eof();
....
    virtual int scanf_one(const char *fmt, void* val) 
    { 
        if(substream) return substream->scanf_one(fmt,val);

C API

LibRaw C API is a wrapper around C++ API; therefore, virtually all documentation to C API functions is represented by a set of hyperlinks to the corresponding places in the description of C++ API.

Contents

  1. Initialization: libraw_data_t *libraw_init(unsigned int flags);
  2. Returned values
  3. Data loading
  4. Auxiliary Functions
  5. Data Postprocessing, Emulation of dcraw Behavior
  6. Writing to Output Files
  7. Writing processing results to memory buffer

Initialization: libraw_data_t *libraw_init(unsigned int flags);

The function returns the pointer to the instance of libraw_data_t structure.
The resultant pointer should be passed as the first argument to all C API functions (except for libraw_strerror).

Returns NULL in case of error, pointer to the structure in all other cases.

Returned values

Functions of C API return EINVAL (see errno.h) if the null pointer was passed to them as the first argument. In all other cases, the C++ API return code is returned.

Data Loading from a File

int libraw_open_file(libraw_data_t*, const char *)
See LibRaw::open_file()
int libraw_open_buffer(libraw_data_t*, void *buffer, size_t bufsize)
See LibRaw::open_buffer()
int libraw_unpack(libraw_data_t*);
See LibRaw::unpack()
int libraw_unpack_thumb(libraw_data_t*);
See LibRaw::unpack_thumb()

Auxiliary Functions

const char* libraw_version()
See LibRaw::version()
const char* libraw_versionNumber()
See LibRaw::versionNumber()
bool LIBRAW_CHECK_VERSION(major,minor,patch)
See LIBRAW_CHECK_VERSION в описании C++ API
int libraw_cameraCount()
See LibRaw::cameraCount()
int libraw_cameraList()
See LibRaw::cameraList()
void libraw_unpack_function_name(libraw_data_t*);
See LibRaw::unpack_function_name()
void libraw_add_masked_borders_to_bitmap(libraw_data_t*);
See LibRaw::add_masked_borders_to_bitmap()
void libraw_rotate_fuji_raw(libraw_data_t*);
See LibRaw::rotate_fuji_raw()
void libraw_recycle(libraw_data_t*);
See LibRaw::recycle()
void libraw_close(libraw_data_t*);
See LibRaw::~LibRaw()
const char *libraw_strerror(int errorcode);
See LibRaw::strerror
const char *libraw_strprogress(enum LibRaw_progress);
See LibRaw::strprogress
void libraw_set_memerror_handler(libraw_data_t*, memory_callback cb);
See LibRaw::set_memerror_handler()
void libraw_set_dataerror_handler(libraw_data_t*,data_callback func);
See LibRaw::set_dataerror_handler()
void libraw_set_progress_handler(libraw_data_t*,progress_callback func);
See LibRaw::set_progress_handler()

Data Postprocessing, Emulation of dcraw Behavior

Setting of Parameters

The postprocessing parameters for the calls described below are set, just as for C++ API, via setting of fields in the libraw_output_params_t structure:

 libraw_data_t *ptr = libraw_init(0);
 ptr->params.output_tiff = 1; //  output to TIFF
    

Fields of this structure are described in the documentation to libraw_output_params_t. For notes on their use, see API notes.

Emulation of dcraw Behavior

int libraw_adjust_sizes_info_only(libraw_data_t*);
See LibRaw::adjust_sizes_info_only()
int libraw_dcraw_document_mode_processing(libraw_data_t*);
See LibRaw::dcraw_document_mode_processing()
int libraw_dcraw_process(libraw_data_t* lr);
See LibRaw::dcraw_process()

Writing to Output Files

int libraw_dcraw_ppm_tiff_writer(libraw_data_t* lr,const char *filename);
See LibRaw::dcraw_ppm_tiff_writer()
int libraw_dcraw_thumb_writer(libraw_data_t* lr,const char *fname);
See LibRaw::dcraw_thumb_writer()

Writing processing results to memory buffer

libraw_processed_image_t *libraw_dcraw_make_mem_image(libraw_data_t* lr,int * errcode)
See LibRaw::dcraw_make_mem_image()
libraw_processed_image_t *libraw_dcraw_make_mem_thumb(libraw_data_t* lr,int * errcode)
See LibRaw::dcraw_make_mem_thumb()

General Notes on API

Contents

  1. LibRaw editions
  2. Error Code Conventions and Error Handling
  3. Nonstandard Situations That Are Not Errors
  4. Input Layer Abstraction
  5. Thread Safety
  6. The Use of C++
  7. Parameters of the LibRaw::imgdata.params Structure Affecting the Behavior of open_file/unpack/unpack_thumb
  8. RAW-data filtering
  9. Masked pixels storage
  10. Memory Usage
    1. Stack Usage
    2. Dynamic Memory Management
    3. Dynamic Memory Usage
      1. Memory for the Decoded Image
      2. Memory for the Decoded Thumbnail
      3. Memory for the Decoded ICC Profile
      4. Memory for RAW Unpacking
      5. Memory for Postprocessing
      6. Memory for File Writing
      7. Unpacking into memory buffer
  11. Incompatibilities with dcraw
    1. Processing of Thumbnails from Kodak cameras

LibRaw Versions

Since version 0.9, there is only one LibRaw variants. Older versions have three separate editions (normal, -Lite and -Commercial versions).

Error Code Conventions and Error Handling

The following conventions concern the returned errors:

  1. All functions that can return an error code have integer type of return data.
  2. If there is no error, the return value is 0 (LIBRAW_SUCCESS).
  3. If an error has happened in a system call, the return value is errno (a positive number), which can be analyzed using strerror() or similar means.
  4. All LibRaw's own error codes are negative; each of these errors belongs to one of two types:
    Non-fatal errors
    Non-fatal errors do not forbid execution of other functions in the processing succession (e.g., unpack_thumb() can easily return the code corresponding to "preview is absent" but this does not prevent further call of unpack().
    Fatal errors
    In the case of fatal errors (memory shortage, input data error, data unpacking failure), the current stage of processing is terminated and all allocated resouces are freed.
    If an attempt to continue processing is made, all subsequent API calls will return the LIBRAW_OUT_OF_ORDER_CALL error.
    At the same time, the LibRaw instance in which a fatal error has occurred can process the next RAW files in the usual way (by calling open_file() (or other input methods), then unpack(), etc.).
  5. The macro LIBRAW_FATAL_ERROR(error code) checks if an error is fatal or not.
  6. The error codes are listed and deciphered here.

Nonstandard Situations That Are Not Errors

If the program has encountered a nonstandard situation that does not prevent retrieval of some data from a file, it sends a signal by setting the corresponding bit in imgdata.process_warnings. The possible types of warnings are listed and deciphered here.

Input Layer Abstraction

LibRaw uses objects derived from LibRaw_abstract_datastream for data input. Semantics of these objects is similar to 'file with arbitrary seek' object: both read and seek operations are used.

Some RAW formats requires temporary switch to another data stream created on top on memory buffer for metadata read. Methods for doing so are implemented in base class LibRaw_abstract_datastream by internal data field substream. Look into source code of LibRaw_file_datastream class in libraw/libraw_datastream.h file for more details.
When implementing own datastream classes, you need to take substream into account and pass control to methods of this field if it is active (not NULL).

If datastream implementaton knows name of input file, it should provide fname() call. This name will be used in error callbacks and in guessing name of JPEG file with metadata (for RAW files with external metadata).

For external metadata support input class should implement subfile_open()/subfile_close() methods.
Sample of these methods implementation may be found in LibRaw_file_datastream class (look into libraw/libraw_datastream.h file for details).

Thread safety

Thread safety is ensured if a LibRaw object is created and used within one thread. At the same time, the number of threads (each with its own LibRaw object) is not limited in any way (except by memory requirements).

If a LibRaw object is created in one execution thread and used in another, external synchronization is necessary.

There is two libraries under Unix enviroment (Linux/FreeBSD/MacOS): libraw_r.a (thread-safe) and libraw.a (single-threaded, slightly faster).

Thread-safe library version stores intermediate unpacker data into LibRaw class data. So, several copies of LibRaw, working in parallel, is possible.

Not thread-safe library uses global variable for intermediate data store which is faster but not reenterant. This library may be used in multi-threaded apps, but only if exactly one LibRaw class copy exists in program.

Windows version is similar to multi-threaded Unix one.

The Use of C++

Exception situations within LibRaw are handled using the C++ exception mechanism. All exceptions are caught inside the library functions and should not penetrate outside.

Memory is allocated/freed using functions malloc(calloc)/free rather than new/delete.

No specific libraries (STL, Boost, smart pointers) are used.

If C API is used, references to C++ calls new/delete still remain, and so linking with libstdc++(Unix)/....(Windows) is necessary.

Parameters of the LibRaw::imgdata.params Structure Affecting the Behavior of open_file/unpack/unpack_thumb

Most data fields of structure LibRaw::imgdata.params affect only data postprocessing, but there are some exceptions, which have been inherited by the current version of LibRaw from/ dcraw source texts (these dependences will be gradually removed).

imgdata.params.use_camera_matrix and imgdata.params.use_camera_wb
These fields affect loading of RAW data for cameras with a color matrix.
Attention! If parameter imgdata.params.use_camera_matrix is not set by the user, it is copied from imgdata.params.use_camera_wb at the stage of file opening.
imgdata.params.user_flip
If this parameter is greater than or equal to zero, assignment imgdata.sizes.flip = imgdata.params.user_flip is performed at the open_file() stage.
imgdata.params.shot_select
This parameter makes it possible to select the number of the extracted image for data formats in which storage of several RAW images in one data file is possible.
imgdata.params.half_size
Affects RAW data loading for Phase One and Sinar backs. Also, it this parameter is set then image bitmap will be reduced by half in each dimension. In later case, all 4 components of bitmap will be filled during data extraction phase.
imgdata.params.threshold, imgdata.params.aber
If these parameters used, then half-sized bitmap will be used for data unpacking. See above for details.
imgdata.params.filtering_mode
Affects RAW data loading for cameras with black level subtraction/de-banding during RAW read phase: Canon A600, A5, CRW/CR2; cameras with LJPEG-compression (some Nikon, Kodak and Hasselblad cameras).
imgdata.params.use_camera_wb
Affects loading of white balance matrix for Leaf backs.
imgdata.params.document_mode
Does not affect data reading in the current version of LibRaw. In dcraw, this parameter affects the thumbnail data for certain Kodak cameras.

RAW-data filtration

During RAW unpacking and post-process stages LibRaw can filter RAW data:

  • Black level subtraction (required if not already done by camera firmware).
  • Zero pixels removal (averaging with nearest pixel values).
  • Tone curve operations on RAW-data.

Current LibRaw version allows tuning of filtration process by setting imgdata.params.filtering_mode option to one of enum LibRaw_filtering vales:

  • Dcraw-compatible filtration. This mode is default for old LibRaw versions compatibility. This mode is not recommended for new applications.
  • One or several (or all) filtering stages can be turned off. This mode recommended for RAW analysers and advanced data filtration made by calling program (this programs may calculate black level by analyzing masked frame data.
  • Automatic selection of best filtration avaliable in LibRaw (e.g. camera/model specific routines). This mode is recommended for most applications.

Masked pixels storage

Some of RAW images contains data for not-active ("black","masked") pixels. For some of these formats LibRaw can extract these pixels values and store it in imgdata.masked_pixels data structure. These values can be explored for:

  • Black level calculation (with banding suppression on some cameras).
  • Noise level calculation, e.g. per-channel noise. This is useful for cameras with per-channel amplification.

Masked pixel data avaliable only for bayer-pattern data (one component per pixel) and only for cameras with masked frame. Masked pixels extraction for other color models (Canon sRAW, Kodak YRGB, Sinar 4-shot files) will possibly be added in future LibRaw versions.

LibRaw provides interface for merging active pixel data and masked pixels into one bitmap: add_masked_borders_to_bitmap(). This call is useful for RAW analyzers.

Memory Usage

Stack Usage

An instance of the LibRaw class has its own size about 100 Kb; if constructions like LibRaw imageProcessor; are used, this memory is stack-allocated.

Methods of class LibRaw (and C API calls) may allocate up to 130-140 Kb of data on the stack (to place auto variables) during their work.

Thus, the work of one LibRaw instance may require about 250 Kb of stack memory. This is not a problem for most contemporary architectures. However, when working in a multithreaded environment, one should not forget to allocate a sufficient amount of memory for the thread stack.

In the case of dynamic allocation (LibRaw *iProcessor = new LibRaw;), the requirements to stack memory will decrease by 100 Kb, which is the size of a class instance). If C API is used, the LibRaw instance is allocated dynamically.

Dynamic Memory Management

LibRaw keeps record of all allocated dynamic memory blocks; in the case of an exceptional situation (fatal error), they are all freed. The code for keeping this record is fairly primitive and not designed to consider allocation of many blocks (in the normal situation, allocation takes place from 2 to 6 times during file processing); this fact should be taken into account by developers trying to add new methods to LibRaw.

Dynamic Memory Usage

LibRaw uses dynamic memory

  • for the decoded image;
  • for the decoded thumbnail;
  • for the ICC profile retrieved from the RAW file (if available);
  • for temporary data at the stage of RAW file unpacking;
  • for temporary data at the stage of postprocessing and result output;
  • for reading of the RAW source file (only under Win32).

Memory for the Decoded Image

To simplify further processing, memory for the extracted RAW data is allocated with a fourfold (for Bayer sensor cameras) excess: for each pixel, four 16-bit components are available (three of them will be zero after RAW unpacking). Thus, one can perform debayer and other postprocessing actions directly in the same buffer as the one used for data extraction, but the required amount of memory becomes four times higher.
Hence, the size of memory for the image buffer is 6-10 times greater than the size of the source RAW file.
It is quite likely that allocation of this buffer in next versions of LibRaw will be more economical, under the condition that postprocessing calls inherited from dcraw will not be used..

The buffer for the decoded image is allocated upon calling unpack() and freed upon calling recycle().

Memory for the Decoded Thumbnail

Memory for the thumbmail is allocated upon calling unpack_thumb() and freed upon calling recycle(). The size of the allocated buffer is precisely adjusted to the thumbnail size, i.e., up to several Mb.

Memory for the Decoded ICC Profile

Memory for the ICC profile is allocated upon calling unpack_profile() and freed upon calling recycle(). The size of the allocated buffer is precisely adjusted to the ICC profile size, i.e., up to several hundred Kb.

Memory for RAW Unpacking

Memory for temporary buffer needed during RAW data unpacking may be allocated during the work of unpack() and freed before completion of this function. The sizes of the allocated buffers are small, up to tens of Kb.

Memory for Postprocessing

During image postprocessing (inherited from dcraw), memory for the histogram (128 Kb) is allocated. This memory is allocated upon calling dcraw_document_mode_processing() and dcraw_process() and freed upon calling recycle().

In addition, during the work of dcraw_process() and during the usage of some available possibilities, like

  • rotation of images from FUJI cameras;
  • correction of chromatic aberrations;
  • image size changes (including correction of non-square pixels);
  • highlight recovery;

a temporary buffer with the size equal to the size of the resultant image (6-8 bytes per pixel for various processing stages) will be allocated. As soon as the intermediate substage of processing is completed, the buffer with the previous copy of the image will be freed.
If postprocessing is not used, then temporary buffers are not allocated.

Memory for File Writing

Upon calling dcraw_ppm_tiff_writer(), memory for a single row of the output image is allocated. The allocated memory is freed before the end of this call.

Unpacking into memory buffer

Functons dcraw_make_mem_image() Х dcraw_make_mem_thumb() (and complementary calls in C-API) allocates memory for entire output datasets (full RGB bitmap and thumbnail, respectively). Calling function should free() this memory themself.

Incompatibilities with dcraw

Processing of Thumbnails from Kodak cameras

In some Kodak cameras, the preview (thumbnail) is stored in the form of uncorrected image. During its extraction using dcraw -e, the white balance, color conversion, and other settings are the same as those used for extraction of the main RAW data (including defect removal and dark frame subtraction, which is erroneous, since the image size is different).
In LibRaw::unpack_thumb() calls, the white balance taken from the camera ("as shot") is used and no settings from imgdata.params are considered.

For all other cameras, thumbnails are extracted "as is," without any color conversions, both in dcraw and in LibRaw.

Usage Examples

Overview of Examples in the Distribution Package (samples/*)

The LibRaw package contains several examples illustrating the use of this library. Their source codes are located in the samples/ folder, and after library build they will be in the bin/ folder:

  • raw-identify The only LibRaw call it uses is open_file(); further code prints the values of the fields of the imgdata structure. The output of raw-identify/raw-identify -v is virtually identical to the output of dcraw -i/dcraw -i -v (in the output of this example, the sources of color data in the numeric form are additionally printed). Command line key -u shows unpacking function name.
  • simple_dcraw A simple "emulation" of dcraw reproducing the behavior of dcraw [-D] [-e] [-v] [-T]. The result of its work must be binary identical to the result produced by a dcraw run with relevant keys. A simplified version of this example is considered below.
    -B command-line switch turns on use of open_buffer() API call used via mmap() of input file (Unix only).
  • dcraw_half Demonstrates the use of C API. The example emulates the behavior of dcraw -h (no other control parameters can be specified in this example). The result of its work must be binary identical to the results produced by dcraw -h.
  • dcraw_emu Complete emulation of dcraw (except for keys -D -d -P -K -i -e, which are considered in other usage examples). Of most interest is processing of command line keys (copied from dcraw). The result of its work must be binary identical to the results produced by dcraw with the same command line keys.
    This sample supports gamma-corrected output of 16-bit data (use -1 command-line switch).
    -B command-line switch turns on use of open_buffer() API call used via mmap() of input file (Unix only).
    The -c float-value command-line switch sets params.adjust_maximum value to nondefault. Set it to zero to disable maximum adjustment and to nonzero to enable it. Disabling this feature may result to incorrect highlights processing on some cameras.
  • half_mt Emulation of dcraw -h. It "understands" the following keys: -a (automatic white balance over the entire image), -w (white balance of the camera), -T (output in the tiff format), and -J n (number of parallel threads launched for image processing).
    On multiprocessor/multicore computers, the speed gain is notable in the case of mass processing. On a Win32 machine, the example is assembled from the initial file half_mt_win32.c, since work with threads under Windows is fundamentally different and it it easier to copy simple source codes than write one complex code.
  • mem_image This sample uses dcraw_make_mem_image and dcraw_make_mem_thumb calls, than writes data in PPM format. Results should be identical with dcraw with same command-line options (options supported: -4, -1, -e, -h).
  • unprocessed_raw This sample extracts (mostly) unaltered RAW data including masked pixels data (on supported cameras). Black level subtraction and zero pixel averaging is turned off. If black frame exists and black frame extraction is supported for given format, masked pixels data is added to resulting .TIFF file. Command line options: -q - be quiet, -A - autoscale data (integer multiplier), -g gamma-correction (gamma 2.2) for data (instead of precise linear one), -N turns off tone curve correction of RAW data.
  • 4channnels - splits RAW-file into four separate 16-bit grayscale TIFFs (per RAW channel).
    Command line switches:
    • -s N selects N-th image from RAW with multiple images
    • -g gamma correction (gamma 2.2)
    • -A values autoscale by auto-calculated integer factor
    • -B turn off black subtraction
    • -N no RAW curve

Example of docmode

Below we consider the samples/simple_dcraw.cpp example, which emulates the behavior of dcraw [-D] [-e][-v][-t]. To save space, let us assume that keys -t -v are always specified (to avoid comments on command line parsing) and there is always one parameter (name of file), which is the only one and always passed to the program.

int main(int ac, char *av[])
{
    int  i, ret, verbose=0, output_thumbs=0;
    char outfn[1024],thumbfn[1024]; 

    // Creation of image processing object
    LibRaw RawProcessor;
    
    // The date in TIFF is written in the local format; let us specify the timezone for compatibility with dcraw 
    putenv ((char*)"TZ=UTC"); 

// Let us define variables for convenient access to fields of RawProcessor

#define P1  RawProcessor.imgdata.idata
#define S   RawProcessor.imgdata.sizes
#define C   RawProcessor.imgdata.color
#define T   RawProcessor.imgdata.thumbnail
#define P2  RawProcessor.imgdata.other
#define OUT RawProcessor.imgdata.params

    OUT.output_tiff = 1; // Let us output TIFF

   // Let us open the file
   if( (ret = RawProcessor.open_file(av[1])) != LIBRAW_SUCCESS)
      {
          fprintf(stderr,"Cannot open %s: %s\n",av[i],libraw_strerror(ret));

          // recycle() is needed only if we want to free the resources right now. 
          // If we process files in a cycle, the next open_file() 
          // will also call recycle(). If a fatal error has happened, it means that recycle()
          // has already been called (repeated call will not cause any harm either).

          RawProcessor.recycle(); 
          goto end;
      }

   // Let us unpack the image
   if( (ret = RawProcessor.unpack() ) != LIBRAW_SUCCESS)
      {
          fprintf(stderr,"Cannot unpack_thumb %s: %s\n",av[i],libraw_strerror(ret));

          if(LIBRAW_FATAL_ERROR(ret))
                    goto end;
          // if there has been a non-fatal error, we will try to continue
      }
  // Let us unpack the thumbnail
  if( (ret = RawProcessor.unpack_thumb() ) != LIBRAW_SUCCESS)
     {
          // error processing is completely similar to the previous case
           fprintf(stderr,"Cannot unpack_thumb %s: %s\n",av[i],libraw_strerror(ret));
           if(LIBRAW_FATAL_ERROR(ret))
                   goto end; 
    }
  else // We have successfully unpacked the thumbnail, now let us write it to a file
    {
      snprintf(thumbfn,sizeof(thumbfn),"%s.%s",av[i],T.tformat == LIBRAW_THUMBNAIL_JPEG ? "thumb.jpg" : "thumb.ppm");
      if( LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_thumb_writer(thumbfn)))
        {
                fprintf(stderr,"Cannot write %s: %s\n",thumbfn,libraw_strerror(ret));

                // in the case of fatal error, we should terminate processing of the current file
                if(LIBRAW_FATAL_ERROR(ret))
                          goto end; 
        }
    }
   // Data unpacking
   if(OUT.document_mode)
           ret = RawProcessor.dcraw_document_mode_processing();
   else
           ret = RawProcessor.dcraw_process();

    if(LIBRAW_SUCCESS != ret ) // error at the previous step
          {
               fprintf(stderr,"Cannot do postprocessing on %s: %s\n",av[i],libraw_strerror(ret));
               if(LIBRAW_FATAL_ERROR(ret))
                        goto end;
          }
   else  // Successful document processing
     {
        snprintf(outfn,sizeof(outfn),"%s.%s", av[i], "tiff");
        if( LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_ppm_tiff_writer(outfn)))
                fprintf(stderr,"Cannot write %s: error %d\n",outfn,ret);
     }

  // we don't evoke recycle() or call the desctructor; C++ will do everything for us
  return 0;
end:
  // got here after an error
  return 1;
}