Skip to content
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

How to manipulate raw pixel data? #95

Closed
Danielku15 opened this issue Feb 28, 2021 · 8 comments
Closed

How to manipulate raw pixel data? #95

Danielku15 opened this issue Feb 28, 2021 · 8 comments

Comments

@Danielku15
Copy link

I was wondering how I can access the raw pixel byte array of an Image in Skija. After loading an Image via Image.makeFromEncoded I somehow could not find a good way to convert the Image to a Bitmap. And even if I would be able to create a Bitmap there seems no accessor for the raw byte[] of the image representing the pixels. I need to do some raw pixel manipulation (for diffing purposes).

On SkiaSharp you can create a SKBitmap either by using SKBitmap.Decode() or via SKBitmap.FromImage. The raw pixels are then accessible via skBitmap.Bytes.

Is this missing in Skija?

@tonsky
Copy link
Collaborator

tonsky commented Feb 28, 2021

Looks like it is missing, and I already have a request to add it #72. Will add next week

@Danielku15
Copy link
Author

Thanks for the fast reply. Digging deeper I saw that there are quite some essentials missing. My use cases would be:

  1. Converting Image to Bitmap for accessing the actual image contents.
    Related SkiaSharp methods: SKBitmap.Decode(SKData), SKBitmap.FromImage(SKImage)
  2. Converting Bitmap to Image for saving the bitmap again to disk after manipulations.
    Related SkiaSharp method: SKImage.FromBitmap(SKBitmap)
  3. Access the binary data of a Bitmap for raw pixel manipulations. I want to do diffing of images with anti-alias detection, basically a port of PixelMatch.
    Related SkiaSharp property: SKBitmap.Bytes
  4. Converting a raw pixel byte array to a Bitmap/Image for further saving. The pixels which differ are written to a raw byte array. To save these "diff" pixels as PNG to disk I need to create an image out of it.
    Related SkiaSharp method: SKImage.FromPixels(SKImageInfo, SKData)

Hope this helps as input to cover my needs 😁 .

tonsky added a commit that referenced this issue Mar 5, 2021
tonsky added a commit that referenced this issue Mar 5, 2021
tonsky added a commit that referenced this issue Mar 5, 2021
@tonsky
Copy link
Collaborator

tonsky commented Mar 5, 2021

Added everything except SKBitmap.Decode (will add later). Check out https://github.com/JetBrains/skija/blob/3fda7b5b74a42b2b015bebe2031fccff631fb409/examples/scenes/src/BitmapImageScene.java for examples. From your description, you should be able to do what you set up to do. Jar version 0.89.32

Screenshot 2021-03-05 at 23 40 41

@Danielku15
Copy link
Author

Thanks a lot for the fast extensions. The changes seem to cover my usecases. The memory load might not be optimal because the read/installPixel workflows usually result in a full copy of the raw buffer instead sharing buffers or accessing the raw buffer of a bitmap for in-place modifications.

In my concrete case it shouldn't be a problem with the memory consumption.

It also seems with my rework that I do not have a direct need for SKBitmap.Decode anymore. I work now rather on Image base and just for the diffing I do a one time load of the raw pixels via Bitmap.makeFromImage(img).readPixels(). But I leave it up to you to still implement it. From my side the issue can be closed 😉

@tonsky
Copy link
Collaborator

tonsky commented Mar 6, 2021

Will see if direct memory access is possible via ByteBuffers

@Danielku15
Copy link
Author

Danielku15 commented Mar 6, 2021

https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#NewDirectByteBuffer
This should do the job 😉 . Beside that Skia has some built-in conversion functions which work based on the underlying pixel buffer. e.g. SkImage.MakeFromBitmap

@cqjjjzr
Copy link
Contributor

cqjjjzr commented Jul 6, 2021

What about manipulating raw pixels in a raster based SkSurface? on SkiaSharp we have ´SkSurface.peekPixels´ which returns a SkPixmap,and no copying is involved. So how to do so in Skija?
Thanks.

@tonsky
Copy link
Collaborator

tonsky commented Jul 10, 2021

@cqjjjzr if you need it, please open a new issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants