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

loadImage() requires a PGraphics which makes API harder to use #4501

Closed
jdf opened this issue May 22, 2016 · 11 comments
Closed

loadImage() requires a PGraphics which makes API harder to use #4501

jdf opened this issue May 22, 2016 · 11 comments

Comments

@jdf
Copy link
Contributor

jdf commented May 22, 2016

There's no way to call loadImage() from outside of setup() or other blessed main thread methods because of

https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L5250

but, if I understand correctly from following the code around, this is just to guard against certain circumstances when concurrently loading images from the network. In the case of trying to create an image from a local data file, can't that check be skipped?

@jdf jdf changed the title createImage() requires a PGraphics which makes API harder to use loadImage() requires a PGraphics which makes API harder to use May 22, 2016
@JakubValtar
Copy link
Contributor

Hi @jdf, can you give me a short example?

The check is there because we have asynchronous saving and some people like to call loadImage() right after saving it, in which case we have to block until the saving is finished, otherwise the image gets corrupted.

If there is a good reason to call loadImage() from other threads, I can make it thread safe.

@jdf
Copy link
Contributor Author

jdf commented May 23, 2016

Sure. Several Python Mode examples were broken by this commit, but the general form is

img = loadImage('banana.jpg')

def setup():
     size(300, 300)

def draw():
    image(img, 0, 0)

@jdf
Copy link
Contributor Author

jdf commented May 23, 2016

Is there a reason that the PGraphics has to be the synchronization provider?

@JakubValtar
Copy link
Contributor

You still won't be able to load banana.jpg before setup(), because sketch path is not initialized yet.

Sure. Several Python Mode examples were broken by this commit,

I just tried Topics - Image Processing - Blending and it was already broken in 3.0, because it can't find the files without the sketch path. There is a reason why Java Mode examples never load images before setup(). Loading anything before setup() will only work for absolute pats (and possibly web addresses).

Is there a reason that the PGraphics has to be the synchronization provider?

Yes, PGraphicsOpenGL launches DMA transfers to get pixels from graphics card without blocking the cpu and it has to complete them on the rendering thread.

However, it can be solved to make it work in your case, that is not the problem. Sketch path is going to be your problem, and I don't think we can solve that one.

@jdf
Copy link
Contributor Author

jdf commented May 23, 2016

Thanks very much for your detailed answers. While it happens to be the case that sketch path is indeed available for a Python mode sketch from the get-go, I understand that it may not be convenient to make it so for Java mode. Again, thanks for your consideration.

@jdf jdf closed this as completed May 23, 2016
@JakubValtar
Copy link
Contributor

JakubValtar commented May 23, 2016

Thanks very much for your detailed answers.

You're welcome!

While it happens to be the case that sketch path is indeed available for a Python mode sketch from the get-go

Not on my machine. Python mode in Processing 3.0.2 just loads null and crashes with NullPointerException when I try to draw the image.

img1 = loadImage("layer1.jpg")

def setup():
    size(640, 360)

def draw():
    background(0)
    image(img1, 0, 0)  # NullPointerException here

When I move the loadImage into setup(), it works.

def setup():
    size(640, 360)
    global img1
    img1 = loadImage("layer1.jpg")

def draw():
    background(0)
    image(img1, 0, 0)  # works

It might just be a coincidence on your side (maybe you are launching processing from the sketch folder), but generally sketch path before setup() (more precisely before settings()) is undefined, and you should probably update your examples accordingly. Java mode prints a warning that sketch path is not defined yet and you should move the initializer into setup().

Again, thanks for your consideration.

I would also liked if it just worked everywhere, but sadly it's not possible with the current architecture.

@jdf
Copy link
Contributor Author

jdf commented May 23, 2016

How about with an up-to-date processing? What build of Python Mode? Anyway, it won't work anymore at all, so it's mooooooot.

@JakubValtar
Copy link
Contributor

Python mode 3015. Nope, doesn't work with 3.0 nor with 3.1.1 (where I fixed the problem with PGraphics you mentioned). It just tells me files are not accessible and then crashes with NullPonterException when I try to draw the image (which is null). However, I don't see how it could have worked in the past, unless it's some coincidence in OSX version.

@jdf
Copy link
Contributor Author

jdf commented May 23, 2016

Well then. That seems to be that!

@JakubValtar
Copy link
Contributor

Yeah... sorry.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants