Add new comment

Finally I figured out how to

Finally I figured out how to correctly dump memory image to Windows Bitmap file.
Simple we need add some padding bytes after each line before saving to Bitmap.

Number of padding bytes is given by following formula:

num = img.width mod 4

Since I did everything in C# language, including a wrapper around LibRaw library, I can provide only C# sample code:

var num = img.width % 4;
var padding = new byte[num];
var stride = img.width * img.colors * (img.bits / 8);
var line = new byte[stride];
var tmp = new List<byte>();
for (var i = 0; i < img.height; i++) {
        // BlockCopy: src, srcIndex, dst, dstIndex, count
	Buffer.BlockCopy(img.data, stride * i, line, 0, stride);
	tmp.AddRange(line);
	tmp.AddRange(padding);
}
tmp.ToArray(); // this will contain the correct data ready for export to Bitmap