Mr.CQ
Mr.CQ

Reputation: 21

Which PixelFormat in WritableBitmap for uncompressed image data?

I'm writing an image viewer application showing pure content of digital images stored in CR2, CR3, ARW, TIFF, JPG and DNG format. To show the image content, I use the WritableBitmap. The image data is extracted from the image file, stored in a buffer and written to the WritableBitmap using the WritePixels method. Most of these digital image formats use internally 3 x 8 or 3 x 16 bits to store the pixel data (uncompressed or as JPG). For these images, I use the PixelFormats.Rgb24or PixelFormats.Rgb48.

Some DNG images have the following tags:

and I'm not able to find the proper PixelFormats value. "Value does not fall within the expected range" exception is raised, and I have no idea what is wrong here.

Does someone have an idea or some detailed explanation about WritableBitmap usage ?

[UPDATE] To better explain the problem here a short code snapshot:

With dngItemRD
  If .SourceIFD IsNot Nothing AndAlso .MainImageBuffer IsNot Nothing Then
    Dim cpr As CompressionValues = .SourceIFD.GetEntryValueIFD(TagIDsTIFF.Compression)
    If cpr = CompressionValues.Jpeg_old OrElse cpr = CompressionValues.JPEG Then
      Me.MainImage = GetJpgBitmapImage(.MainImageBuffer)
    ElseIf cpr = CompressionValues.Uncompressed Then
      Dim iWidth As Integer = .SourceIFD.GetEntryValueIFD(TagIDsTIFF.ImageWidth)
      Dim iHeight As Integer = .SourceIFD.GetEntryValueIFD(TagIDsTIFF.ImageHeight)
      Dim stride As Integer = iWidth * .SourceIFD.GetEntryValueIFD(TagIDsTIFF.SamplesPerPixel)
      Dim resolution As Double = 96.0
      If .SourceIFD.ContainsKey(TagIDsTIFF.XResolution) Then resolution = .SourceIFD.GetEntryValueIFD(TagIDsTIFF.XResolution)
      Me.MainImage = GetUncompressedBitmapImage(.MainImageBuffer, iWidth, iHeight, resolution, stride)
    End If  
  End If
End Width

This part of code reads the TIFF metatags from the SourceIFD, which is a code representation of the TIFF's Image File Directory structure. The GetUncompressedBitmapImage function is very simple. It creates a WritableBitmap instance using the computed image values:

Dim wrImage As New WriteableBitmap(iWidth, iHeigth, resolution, resolution, PixelFormats.Rgb24, Nothing)

and try to write the pixels from the data buffer:

wrImage.WritePixels(New Int32Rect(0, 0, iWidth, iHeigth), buffer, stride, 0)

This is also the point, where the Exception is raised, that the value isn't in the proper range. I'm using similar logic for all other image formats without any problem. My guess is, that probably the PixelFormats value is wrong, but I'm not sure.

Upvotes: 0

Views: 142

Answers (0)

Related Questions