Hi,
On Mon, 28 Nov 2022 at 18:53, Hairi Vogel <
hairi...@gmail.com> wrote:
> Just stuck with this:
> how to load a Folder of .jpg (or any image format) files into a List of PImages ???
> it is the strange loadImage(path) in Processing
>
> I am working on the video-delay, i need a video-looper type thing that can scratch
> for now i am hardcoding dozens of @OffScreen PGraphics b1.....
> mimicking what the frame-delay component does.
> i actually only need max 2 seconds for now. RAM is not an issue I got 64GB.
>
> any clue ??
OK, this is a fun one. Some of the resource loading is a little
hidden, so I needed to come up with a utility class that you can add
into the shared code space.
You then need to use an advanced feature on Ref types to async compute
(ie. background load) the list of images in the component.
Code for both below - hope it makes some sense. Note the glob pattern
in the image loader for *.jpg - change that if you need something
else.
Incidentally, the other possible way to do this might be to adapt a
video:capture component to use the GStreamer multifilesrc -
https://gstreamer.freedesktop.org/documentation/multifile/multifilesrc.html?gi-language=c
Best wishes,
Neil
#### SHARED.ImageFolderLoader class
package SHARED;
import java.util.ArrayList;
import java.util.List;
import org.praxislive.core.types.PResource;
import org.praxislive.video.pgl.PGLContext;
import org.praxislive.video.pgl.code.userapi.PImage;
import org.praxislive.video.render.Surface;
import org.praxislive.video.render.utils.BufferedImageSurface;
import java.nio.file.Files;
import java.nio.file.Path;
public class ImageFolderLoader {
public static List<PImage> loadImages(PResource folder) {
List<PImage> list = new ArrayList<>();
try (var files =
Files.newDirectoryStream(Path.of(folder.value()), "*.jpg")) {
for (var file : files) {
list.add(new
PImageImpl(BufferedImageSurface.load(file.toUri())));
}
} catch (Exception ex) {}
return list;
}
private static class PImageImpl extends PImage {
private final Surface surface;
private PImageImpl(Surface surface) {
super(surface.getWidth(), surface.getHeight());
this.surface = surface;
}
@Override
protected processing.core.PImage unwrap(PGLContext context) {
return context.asImage(surface);
}
}
}
#####################################
#### Example component code
@P(1) @OnChange("loadImages") PResource folder;
@T(1) boolean next;
@Inject Ref<List<PImage>> images;
@Inject int index;
@Override
public void setup() {
images.init(ArrayList::new);
loadImages();
}
@Override
public void draw() {
if (next) {
index++;
next = false;
}
List<PImage> list = images.get();
if (list.isEmpty()) {
index = 0;
} else {
index %= list.size();
image(list.get(index), 0, 0, width, height);
}
}
private void loadImages() {
if (folder != null) {
images.asyncCompute(folder, SHARED.ImageFolderLoader::loadImages);
} else {
images.ifPresent(List::clear);
}
}
########################################