0

I have encountered a strange behavior of TransformedBitmap.

A 16-bit grayscale TIFF image (PixelFormats.Gray16) loads just fine into a BitmapSource, but when the image is scaled using TransformedBitmap containing scaling transform, WIC changes pixel format to PixelFormats.Gray32Float and the image gets darker.

Here is the original BitmapSource and the TransformedBitmap:

enter image description here

enter image description here

My clue is that WIC for some reason applies gamma correction to the second image, but the documentation says both Gray16 and Gray32Float use gamma of 1.0 so there should be no gamma conversion at all.

Is this a bug in WIC? Is there any way to workaround for TransformedBitmap modifying content of my images?

Libor
  • 3,285
  • 1
  • 33
  • 41
  • I can not reproduce this behavior. Which ScaleTansform are you using exactly, and what is the original bitmap size? – Clemens Oct 08 '16 at 06:28
  • @Clemens The image needs to be 16-bit grayscale (e.g. TIFF) so that the original BitmapSource has pixel format Gray16. The ScaleTransform downscales the image, e.g. by 60%. Then the new BitmapSource has format Gray32Float and the colors get distorted. Unfortunately, I have deleted my test app when I resolved the issue and I have no spare time to re-create it completely. I looked up some 16-bit TIFF images on the net but they are unfortunately RGB, not grayscale (I used original images from my customer which I cannot share and also recreated some programmaticaly from a byte array). – Libor Oct 09 '16 at 17:40
  • I've tested it with a BitmapSource created programmatically from a pixel buffer, with PixelFormats.Gray16. My TransformedBitmap also has PixelFormats.Gray32Float, but there's no noticable color distortion. – Clemens Oct 09 '16 at 18:04

1 Answers1

0

Okay I found a solution. Instead creating the TransformedBitmap inline:

bitmapSource = new TransformedBitmap(bitmapSource, transform);

I have used the following code with BeginInit() and EndInit() calls:

TransformedBitmap transformedBitmap = new TransformedBitmap();

transformedBitmap.BeginInit();

transformedBitmap.Source = bitmapSource;
transformedBitmap.Transform = transform;

transformedBitmap.EndInit();

bitmapSource = transformedBitmap;

Luckily, the resulting bitmap is just transformed with colours intact.

The pixel format still changes to Gray32Float, but this can be treated using FormatConvertedBitmap to bring it back to Gray16.

Libor
  • 3,285
  • 1
  • 33
  • 41