Want to understand the working of drawImageRect

36 views
Skip to first unread message

Chetan Pandey

unread,
Feb 27, 2024, 9:20:06 AMFeb 27
to skia-discuss
Hi folks

I am using drawImageRect for a project where I have to enter source and destination rect. 
Can some explain the working of drawImageRect using the following snippet:

Skia Fiddle

Brian Osman

unread,
Feb 27, 2024, 9:40:42 AMFeb 27
to skia-d...@googlegroups.com
Sure. drawImageRect lets you draw a specific portion (sub-rectangle) of an image (this is the source rectangle), and stretch/squish that to fit into a different (destination) rectangle on the surface. In the (first) version that only takes a single rectangle, the source is assumed to be the entire image, and the rectangle you specify is the destination. So the result is to always stretch the whole image to fit whatever rectangle you specify - that's why you have a full copy of the image covering the stretched top-left region in your sample.

When you specify two rectangles, the first one is the source, and the second one is the destination. So for your second call, you're taking just an 80x120 portion of the image (that image happens to be 128x128 - this is why it's cut-off), then drawing that to a rectangle that's 40x60 (and offset to 50,50). Does that make sense?

-Brian

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/782cdf99-78f4-4763-9d23-d0b5e1f03ffen%40googlegroups.com.

Chetan Pandey

unread,
Feb 28, 2024, 7:42:03 AMFeb 28
to skia-discuss
If I am using the second API and I pass source as { 0, 0, image.width(), image.height() } then I can fill the whole destination rect with the image right?
There's also a constraint parameter that we pass to the API. What is the purpose of that parameter?
 

Brian Osman

unread,
Feb 28, 2024, 9:40:10 AMFeb 28
to skia-d...@googlegroups.com
Right - if the src is the entire image bounds (which is the same as just calling the single-rectangle version), then you can stretch/squish the full image to cover whatever destination rectangle you want. The constraint parameter is specifically for when you are using a sub-rectangle of the source. With texture filtering, it's possible that pixels outside the source rectangle could "bleed" into the result (because filtering averages neighboring pixels). If you specify Fast, then it's allowed for that to happen. If you specify Strict, then we will guarantee that no pixels from outside of the source rectangle are used when computing the final colors that fill the destination. If you're just taking a sub-rectangle of a larger image, Fast is usually okay. If you have something like a sprite-sheet or atlas, where there might be unrelated content directly adjacent to the source rectangle that you're drawing, then you want to use Strict.

On Wed, Feb 28, 2024 at 7:42 AM Chetan Pandey <chetanc...@gmail.com> wrote:
If I am using the second API and I pass source as { 0, 0, image.width(), image.height() } then I can fill the whole destination rect with the image right?
There's also a constraint parameter that we pass to the API. What is the purpose of that parameter?
 

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.

Chetan Pandey

unread,
Feb 28, 2024, 10:59:06 AMFeb 28
to skia-discuss
Got it. Thanks
Reply all
Reply to author
Forward
0 new messages