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

Documentation issues : flags not that simple #60

Closed
HuskyNator opened this issue Nov 1, 2023 · 5 comments
Closed

Documentation issues : flags not that simple #60

HuskyNator opened this issue Nov 1, 2023 · 5 comments

Comments

@HuskyNator
Copy link

I'm trying to save an array of pixels but can't seem to do so.
I can't access the pixel data directly (mutate it), can't seem to load pixel data directly (eg using loadFromMemory.
I tried using packed (gapless) data and allPixelsAtOnce on an initialized Image, but it only seems readable.

@p0nce
Copy link
Collaborator

p0nce commented Nov 1, 2023

What would have help you achieving this more readily?

@p0nce
Copy link
Collaborator

p0nce commented Nov 2, 2023

@HuskyNator perhaps there is a documentation issue we could fix?

@HuskyNator
Copy link
Author

It'd be nice to be able to use loadFromMemory using a plain pixel array?
I feel like I'm hacking my way around this by using an assignment like allPixelsAtOnce()[] = pixelsArray[].
Apart from that, it wasn't really clear whether this was possible from the docs. (Though I got it to work :) )

@p0nce
Copy link
Collaborator

p0nce commented Nov 4, 2023

Ah yes, loadFromMemory() is to load en encoded-image.
You are supposed to use image.createViewFromData() and then you have an Image which is a reference to that data (that you can .clone).

Maybe a new call that do that internally would be cool, please suggest a name maybe? I'm thinking of loadFromPixels or loadFromData.

@HuskyNator
Copy link
Author

HuskyNator commented Nov 7, 2023

Ah I see. I totally missed that comment! Totally my bad :)

(EDIT: from p0nce: I'm sorry I accidentally replied in your comment!!!)

In what way would the two be different? I'd normally say making it doable using loadFromMemory, but that might be confusing/complex. I think loadFromPixels or loadFromPixelData works well. :)

Now I see how loadFromMemory can be a trap as an identifier.

Also slightly confused about the existence of LoadFlags in this case.
They're not used by createViewFromData right?

No because by creating from pixel data you cannot force some layout from load only (unlike, say, an image decoder could load a rgb8 image as rgba8 and save some time).

But you can change type with:
image.convertTo(PixelType type);
and change layout constraints with:
image.setLayout(LayoutConstraints constraints);

If your memory pixel already fulfilled some of the layout constraints, the constraints should be accepted without reallocating or cloning those pixels.

Honestly quite confused about the difference between Layout, PixelFormat and LoadFlags.
They seem to overlap a bit?

  • PixelType is how the pixels are arranged, number of channels and bits. You can change it with convertToXXX.
  • LayoutConstraints are how these pixels need to be arranged in memory with excess bytes outside the scanlines or vertical flip, but that doesn't change the content (interior) of the image. You can change it with setLayout. Layout constraints are "ad-hoc" inferred in order to not reallocate if some constraints can be followed already. For example, if you load an image whose width is 104, then the constraints "width must be multiple of 4" will be for free if you where to apply it.
  • LoadFlags are an optimization, a way to apply setLayout and convertTo at load time, in order to save on reallocations. Indeed some image decoders can take advantage of that to convert scanlines as they are decoded. Similar to how stb_image can force a channel count at load, if we don't have that then gamut would be slower in some cases.

@p0nce p0nce changed the title How do I save an array of pixels? Documentation issues : flags not that simple Nov 7, 2023
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

2 participants