Skip to content

Commit 1efbfce

Browse files
committed
Update README.md
1 parent 1c2a51d commit 1efbfce

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,26 @@ print("written ", ffi.sizeof(mem), "bytes to", mem)
547547

548548
An allocated char array pointer (GCd with a `ffi.gc` callback) and the length in bytes of the image data is directly returned from libvips (no intermediate FFI allocation).
549549

550+
## True Streaming
551+
552+
When processing images an image library would usually read an image from a file into memory, decode and process it and finally write the encoded result into a file. The processing can only start when the image is fully read into memory and the writing can only start when the processing step is completed.
553+
Libvips can process images directly from a pipe and write directly to a pipe, without the need to read the whole image to memory before being able to start and without the need to finish processing before being able to start writing. This is achieved using a technique called true streaming. In this context there are sources and targets and the processing step happens from source to target. Sources can be created from files, memory or descriptors (like stdin) and targets can be created to files, memory or descriptors (like stdout). Here is an example:
554+
555+
```lua test.lua
556+
local vips = require "vips"
557+
local stdin, stdout = 0, 1
558+
local source = vips.Source.new_from_descriptor(stdin)
559+
local target = vips.Target.new_to_descriptor(stdout)
560+
local image = vips.Image.new_from_source(source, '', { access = 'sequential' })
561+
image = image:invert()
562+
image:write_to_target(target, '.jpg')
563+
```
564+
Running this script in a Unix terminal via
565+
```term
566+
curl https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/600px-Cat03.jpg | lua test.lua > out.jpg```
567+
568+
will feed a cat image from the internet into standard input, from which the Lua script reads and inverts it and writes it to standard output, where it is redirected to a file. This all happens simultaneously, so the processing and writing doesn't need to wait until the whole image is downloaded from the internet.
569+
550570
## Error handling
551571
552572
Most `lua-vips` methods will call `error()` if they detect an error. Use

0 commit comments

Comments
 (0)