-
Notifications
You must be signed in to change notification settings - Fork 546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[QUESTION] Performance of SKImage via SKCodec and SKSurface vs. SKBitmap #1269
Comments
So, for Q1, the reason the codec needs to be copied is because that is more of a decode operation. I think I might need to deprecate the The better way would be to either call Finally, A good way would be to just use GetPixels to a new image: // load
using var codec = SKCodec.Create(path);
var sourceInfo = codec.Info;
// prepare new image
var destInfo = sourceInfo.WithColorType(SKImageInfo.PlatformColorType);
using var image = SKImage.Create(destInfo);
using var pixmap = image.PeekPixels();
var pixelAddress= pixmap.GetPixels();
// read
var result = codec.GetPixels(destInfo, pixelAddress); Full code: using var codec = SKCodec.Create(pathToImage);
// get things
var origin = codec.EncodedOrigin;
Console.WriteLine("Origin: " + origin);
var sourceInfo = codec.Info;
Console.WriteLine(
$"W: {sourceInfo.Width}, " +
$"H: {sourceInfo.Height}, " +
$"CT: {sourceInfo.ColorType}, " +
$"AT: {sourceInfo.AlphaType}, " +
$"CS.G{sourceInfo.ColorSpace.NamedGamma}, " +
$"CS.T{sourceInfo.ColorSpace.Type}");
// make sure the color type is the best for drawing
var destInfo = sourceInfo.WithColorType(SKImageInfo.PlatformColorType);
Console.WriteLine(
$"W: {destInfo.Width}, " +
$"H: {destInfo.Height}, " +
$"CT: {destInfo.ColorType}, " +
$"AT: {destInfo.AlphaType}, " +
$"CS.G{destInfo.ColorSpace.NamedGamma}, " +
$"CS.T{destInfo.ColorSpace.Type}");
// create the container image
using var image = SKImage.Create(destInfo);
// get the pixmap so e can get the pointer to the memory location
using var pixmap = image.PeekPixels();
var pixelAddress= pixmap.GetPixels();
// get pixels
var result = codec.GetPixels(destInfo, pixelAddress);
Console.WriteLine($"Decode result: {result}"); |
For Q2, yes,
But, all this doesn't really matter. A surface can wrap an image. Both can wrap a chunk of random memory. But the typical use is images provide pixels to draw, surfaces provide a place to draw on. For raster, this is all the same, but for GPU, this matters because you can't really draw on a GPU image. |
thanks for the detailed answer!.. Q1: in the
Q2: when I get the time will try measure the diff between:
vs:
|
maybe this 3. way is the fastest/best way (mem wise)?
|
Probably the alpha type, yes.
Hmmm. That will work for raster, yes. Then you can draw directly on the surface. But, you need to make sure you use the correct color/alpha types for best results. The surface is more picky about the combos. But, the defaults usually work. But, I believe all the implementations of But, the bitmap can be used in place of an image if it is marked But... again. This is 100% a perf test requirement. could/should/might means nothing. Test some options, and then let us all know. |
I tried two ways now - the diff seems to be negligible - however I'm baffled about the reduced memory consumption using the image (sharpening) filter:
I did not test the alpha as I could not get it to work with LinqPad5 |
Good to know the difference is negligible. It is a bit weird when drawing with the filter. What does the test source look like? |
ran this fragment in LINQPad 5:
|
Q1) I use
SKCodec
to read image (needEncodedOrigin
), then create andSKImage
from that usingSKImage.FromPixelCopy(codec.info, codec.Pixels)
. Question: Is there a faster way of doing this? As I read it as a double up on memory there even though data is immutable. E.g. ifSKCodec
could expose the data using e.g.ReadOnlySpan<>
.Q2) Also i use
SKSurface
for drawing (mostly scaling and filters) instead ofSKBitmap
as that is newer/better - true? Or is that only true when GPU is available (I am doing all on server/asp.net).The text was updated successfully, but these errors were encountered: