views:

292

answers:

4

Does anyone know of some sample code that shows how Delphi 2010 can read RAW files using its new COM interface to WIC?

I want to read Canon RAW images and then access specific pixels...

+3  A: 

This is the simplest usage:

procedure TForm116.Button1Click(Sender: TObject);
var
  WIC: TWICImage;
begin
  WIC := TWICImage.Create;
  try
    WIC.LoadFromFile('MyFilename.raw');
    Image1.Picture.Graphic.Assign(WIC);
  finally
    WIC.Free;
  end;
end;

There are many, many, many different types of "raw" image file formats, so there is no telling if WIC will be able to handle it.

Nick Hodges
I can't get Nick's code to work.I've got some sample files (good source: http://www.rawsamples.ch/) and also what appears to be a good set of drivers (http://www.fastpictureviewer.com/codecs/).These two work well together, as evidenced that in the Windows Explorer I can see the file's thumbnails and view them in the Windows Photo Viewer. (Or is the preceding sentence no proof of WIC availability??)But the above code below results in an access violation when I try it on several pictures.The files have the extension .CR2. I don't know whether WIC requires .RAW or what.
Tom1952
If WIC will handle the file, then that code will work. What error do you get?
Nick Hodges
Hmmmm... I get access violations in WIC.LoadFromFile on CRW and CR2 files. Any suggestions, Nick.
Tom1952
My suggestion is that WIC can't process your file. :-(
Nick Hodges
A: 

Nick's answer was correct after all! I went back looked more closely and found the exception was occurring on the Assign statement ... because the TImage on my form didn't have a Picture! Assigning any picture in the IDE Object Inspector or initializing it in a way similar to my code below made it work great!

The code below will convert a RAW file to a BMP file. I haven't worked much with images, so I'm not absolutely certain that the code below is correct, but it seems to be working. Feedback welcome.

Reminder to other developers: my earlier comment above has links to a great source of sample RAW files and codecs. These are invaluable.

Thanks, Nick, AND Embarcadero!!!

  procedure TForm1.Button1Click(Sender: TObject);
  var
    WIC: TWICImage;
    BMP: TBitMap;
  begin
    WIC := TWICImage.Create;
    BMP := TBitMap.Create;
    try
      WIC.LoadFromFile('MyFileName.Raw');
      BMP.Assign(WIC);
      BMP.SaveToFile('MyFilename.bmp');
    finally
      WIC.Free;
      BMP.Free;
    end;
  end;
Tom1952
+1  A: 

TPicture is very tricky to work with, when you access the Graphic property it does not check anything. To make Nick's code work you can force the Picture to create a bitmap first:

Image1.Picture.Bitmap;
Image1.Picture.Graphic.Assign(WIC);

It would be nice if the TPicture class was more succesful in hiding its implementation details :-)

Victor
A: 

Having loaded the Canon RAW Codec, rc170upd_7l.exe, from http://www.usa.canon.com/cusa/windows_vista/cameras/eos_slr_camera_systems/canon_raw_codec_software#DriversAndSoftware, this displays an image in Delphi 2010 on XP SP3:

var  
    WIC: TWICImage;  
begin  
    WIC := TWICImage.Create;  
    try  
        WIC.LoadFromFile('IMG_0201.CR2'); // WIC.ImageFormat reports wifOther  
        Img1.Picture.Assign(WIC);  
    finally  
        WIC.Free;  
    end;  
end;