I'm trying to read a CR2 file using the Canon EDSDKv0309W. I didn't found an example for this SDK version so I looked at several examples from older versions and created the code below. But I always get EDS_ERR_NOT_SUPPORTED in line EDSDK.EdsGetImage(..).
Using a 32Bit compilation under .Net4.6.1 I can read the correct with and height from images taken with EOS500D and M100. But I don't get the image. So my assumption is I get a wrong pointer from EdsCreateMemoryStream. But I don't see what is wrong and how to debug this. Any help will be appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EDSDKLib;
using System.Drawing;
using System.Drawing.Imaging;
namespace CR2Reader
{
class Program
{
static Bitmap GetImage(IntPtr img_stream, EDSDK.EdsImageSource imageSource)
{
IntPtr stream = IntPtr.Zero;
IntPtr img_ref = IntPtr.Zero;
IntPtr streamPointer = IntPtr.Zero;
EDSDK.EdsImageInfo imageInfo;
uint error = 0;
try
{
//create reference and get image info
error = EDSDK.EdsCreateImageRef(img_stream, out img_ref);
if (error == 0)
{
error = EDSDK.EdsGetImageInfo(img_ref, imageSource, out imageInfo);
if (error == 0)
{
EDSDK.EdsSize outputSize = new EDSDK.EdsSize();
outputSize.width = imageInfo.EffectiveRect.width;
outputSize.height = imageInfo.EffectiveRect.height;
//calculate amount of data
int datalength = outputSize.height * outputSize.width * (int)imageInfo.NumOfComponents * (int)(imageInfo.ComponentDepth / 8);
//create buffer that stores the image
error = EDSDK.EdsCreateMemoryStream((ulong)datalength, out stream);
if (error == 0)
{
//load image into the buffer
error = EDSDK.EdsGetImage(img_ref, imageSource, EDSDK.EdsTargetImageType.RGB16, imageInfo.EffectiveRect, outputSize, stream);
if (error == 0)
{
//make BGR from RGB (System.Drawing (i.e. GDI+) uses BGR)
byte[] buffer = new byte[datalength];
unsafe
{
System.Runtime.InteropServices.Marshal.Copy(stream, buffer, 0, datalength);
byte tmp;
fixed (byte* pix = buffer)
{
for (int i = 0; i < datalength; i += 3)
{
tmp = pix[i]; //Save B value
pix[i] = pix[i + 2]; //Set B value with R value
pix[i + 2] = tmp; //Set R value with B value
}
}
}
//Get pointer to stream
error = EDSDK.EdsGetPointer(stream, out streamPointer);
if (error == 0)
{
//Create bitmap with the data in the buffer
return new Bitmap(outputSize.width, outputSize.height, datalength, PixelFormat.Format24bppRgb, streamPointer);
}
}
}
}
}
return null;
}
finally
{
//Release all data
if (img_ref != IntPtr.Zero) error = EDSDK.EdsRelease(img_ref);
if (stream != IntPtr.Zero) error = EDSDK.EdsRelease(stream);
}
}
static Bitmap ReadCR2Image(string fileName)
{
IntPtr outStream = new IntPtr();
uint error = EDSDK.EdsInitializeSDK();
error += EDSDK.EdsCreateFileStream(fileName,
EDSDK.EdsFileCreateDisposition.OpenExisting,
EDSDK.EdsAccess.Read,
out outStream);
Bitmap bmp = null;
if (error == 0)
{
bmp = GetImage(outStream, EDSDK.EdsImageSource.FullView);
}
if (outStream != IntPtr.Zero)
{
error = EDSDK.EdsRelease(outStream);
}
EDSDK.EdsTerminateSDK();
return bmp;
}
static void Main(string[] args)
{
Bitmap bmp = ReadCR2Image("IMG_3113.CR2");
}
}
}
You are using the wrong EdsImageSource
type.
Since you are loading a RAW image you also have to use EdsImageSource.RAWFullView
. EdsImageSource.FullView
would be appropriate for e.g. a JPG or TIFF.
Once you change that, everything should work just fine.
Edit: just saw you are using RGB16
as target but the rest of the code assumes normal 8bit RGB. You'll have to change a whole bunch of stuff to get that working correctly. I'd advise you to use RGB
unless you really need 16bit.
Edit 2: Looks like the library is a bit out of that in that regard (I should really update it). In any case, you can always check the header files of the SDK for up-to-date values. Here's the current definition for EdsImageSource
:
enum EdsImageSource
{
FullView = 0,
Thumbnail,
Preview,
RAWThumbnail,
RAWFullView,
}
As for changes required for 16bit:
datalength
is incorrectbyte
instead of ushort
to set the pixelsBitmap
with PixelFormat.Format24bppRgb
Bitmap
doesn't fully support 16Bit. See this article for some in-depth information.Depending on what you need to do it's probably better to either use the raw pixel data directly as you get it from the SDK or use a different graphics library (e.g. WPF, SkiaSharp, ImageSharp, etc.)