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
Question about saving and re-opening an image from a memory array #494
Comments
You could also use the close signal on the image to free the memory, that might be easier. Actually, you can do it even more simply: just make a memory image with Something like: VImage mem = VImage::new_memory();
x.write(mem); Now mem is a memory buffer containing the contents of Your "disc for large files, mem for small files" code might be something like: VImage cache;
if (is_a_huge_file(x))
cache = VImage::new_temp_file("%s.v");
else
cache = VImage::new_memory();
x.write(cache); You can use an env var to say where |
Oh I forgot, sorry, Anyway, for 8.3 and earlier, yes, you need to make a memory buffer and free it, as you said. From 8.4, you can just use |
If I understand correctly, you propose the following:
Is that correct? |
Yes, exactly. This is how https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/memory.c#L130 |
I did a quick test, and it works like a charm. Thanks again! |
Good stuff, I'll close. |
In my photoflow editor I am internally building a pre-computed pyramid of zoom levels whenever I open an image from disk, to speed-up rendering of scaled-down previews.
Currently I am writing each level into a RAW disk buffer, which I re-open with vips_rawload().
For next version, I would like to limit the disk usage and use memory buffers for not-so-large images, and only revert to disk buffers whenever the image would be too large to be kept into RAM.
The solution seems to be simple: use vips_image_write_to_memory() to save the data, and re-open the memory array with either vips_image_new_from_memory() or vips_image_new_from_memory_copy().
The _copy() version has the advantage to avoid any risk of freeing the memory while it is still in use, however it requires a temporary double-allocation of the pixel array which I would like to avoid.
So I was thinking about a way to use vips_image_new_from_memory() and still let VIPS manage the freeing of the memory buffer when it is not needed anymore. One solution I could figure out is to attach the memory array to the image itself as a metadata blob. In this case, the blob would then be deallocated together with the image, right? Would this be a safe and efficient solution?
Thanks!
The text was updated successfully, but these errors were encountered: