Reputation: 10315
Currently, SkiaSharp doesn't support tiff images. (It supports jpg, gif, bmp, png, and a few others.)
How can you convert a tiff image into a SKBitmap object?
One idea: Perhaps there's an efficient way to convert a tiff stream > png stream > SKBitmap object? I'm not sure System.Drawing
could handle the tiff>png stream efficiently. Another possible option is LibTiff.Net, though would need an example of how to convert a tiff stream to a png stream.
Another idea: Access the tiff pixels and draw it directly onto a SKCanvas?
Other ideas?
Upvotes: 4
Views: 6238
Reputation: 5202
@DougS
Your implementation is mostly correct, but it is not very performant because of multiple memory allocations and copies.
I noticed that you are creating 3 memory chunks with a total size of (w*h*4 bytes) each:
// the int[]
raster = new int[width * height];
// the SKColor[]
pixels = new SKColor[width * height];
// the bitmap
bitmap = new SKBitmap(width, height)
You are also copying the pixels between the memory multiple times:
// decode the TIFF (first copy)
tifImg.ReadRGBAImageOriented(width, height, raster, Orientation.TOPLEFT)
// convert to SKColor (second copy)
pixels[arrayOffset] = new SKColor(...);
// set bitmap pixels (third copy)
bitmap.Pixels = pixels;
I think I managed to create a similar method that decodes the stream, with only a single copy and memory allocation:
public static SKBitmap OpenTiff(Stream tiffStream)
{
// open a TIFF stored in the stream
using (var tifImg = Tiff.ClientOpen("in-memory", "r", tiffStream, new TiffStream()))
{
// read the dimensions
var width = tifImg.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
var height = tifImg.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
// create the bitmap
var bitmap = new SKBitmap();
var info = new SKImageInfo(width, height);
// create the buffer that will hold the pixels
var raster = new int[width * height];
// get a pointer to the buffer, and give it to the bitmap
var ptr = GCHandle.Alloc(raster, GCHandleType.Pinned);
bitmap.InstallPixels(info, ptr.AddrOfPinnedObject(), info.RowBytes, null, (addr, ctx) => ptr.Free(), null);
// read the image into the memory buffer
if (!tifImg.ReadRGBAImageOriented(width, height, raster, Orientation.TOPLEFT))
{
// not a valid TIF image.
return null;
}
// swap the red and blue because SkiaSharp may differ from the tiff
if (SKImageInfo.PlatformColorType == SKColorType.Bgra8888)
{
SKSwizzle.SwapRedBlue(ptr.AddrOfPinnedObject(), raster.Length);
}
return bitmap;
}
}
A gist lives here: https://gist.github.com/mattleibow/0a09babdf0dc9d2bc3deedf85f9b57d6
Let me explain the code... I basically am creating the int[]
as you are, but then passing that to the SKBitmap
and letting it take over. I am pinning it as the SKBitmap
lives in unmanaged memory and the GC may move it, but I am sure to unpin it when the bitmap is disposed.
Here is a more detailed step through:
// this does not actually allocate anything
// - the size is 0x0 / 0 bytes of pixels
var bitmap = new SKBitmap();
// I create the only buffer for pixel data
var raster = new int[width * height];
// pin the managed array so it can be passed to unmanaged memory
var ptr = GCHandle.Alloc(raster, GCHandleType.Pinned);
// pass the pointer of the array to the bitmap
// making sure to free the pinned memory in the dispose delegate
// - this is also not an allocation, as the memory already exists
bitmap.InstallPixels(info, ptr.AddrOfPinnedObject(), info.RowBytes, null, (addr, ctx) => ptr.Free(), null);
// the first and only copy from the TIFF stream into memory
tifImg.ReadRGBAImageOriented(width, height, raster, Orientation.TOPLEFT)
// an unfortunate extra memory operation for some platforms
// - this is usually just for Windows as it uses a BGR color format
// - Linux, macOS, iOS, Android all are RGB, so no swizzle is needed
SKSwizzle.SwapRedBlue(ptr.AddrOfPinnedObject(), raster.Length);
Just for some raw stats from a debug session, your code takes about 500ms for one of my images, but my code only takes 20ms.
I hope I don't sound too harsh/negative towards your code, I am not meaning that in any way.
Upvotes: 15
Reputation: 10315
I'm no expert, so I welcome any expert who can make this code more efficient (or has completely different ideas to get a tiff into a SKBitmap).
This uses LibTiff.Net
using BitMiracle.LibTiff.Classic;
. . . .
public static void ConvertTiffToSKBitmap(MemoryStream tifImage)
{
SKColor[] pixels;
int width, height;
// open a Tiff stored in the memory stream, and grab its pixels
using (Tiff tifImg = Tiff.ClientOpen("in-memory", "r", tifImage, new TiffStream()))
{
FieldValue[] value = tifImg.GetField(TiffTag.IMAGEWIDTH);
width = value[0].ToInt();
value = tifImg.GetField(TiffTag.IMAGELENGTH);
height = value[0].ToInt();
// Read the image into the memory buffer
int[] raster = new int[width * height];
if (!tifImg.ReadRGBAImageOriented(width, height, raster, Orientation.TOPLEFT))
{
// Not a valid TIF image.
}
// store the pixels
pixels = new SKColor[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int arrayOffset = y * width + x;
int rgba = raster[arrayOffset];
pixels[arrayOffset] = new SKColor((byte)Tiff.GetR(rgba), (byte)Tiff.GetG(rgba), (byte)Tiff.GetB(rgba), (byte)Tiff.GetA(rgba));
}
}
}
using (SKBitmap bitmap = new SKBitmap(width, height))
{
bitmap.Pixels = pixels;
// do something with the SKBitmap
}
}
Upvotes: 2