add something like `mutable`

未關閉
#79 1 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
5/5
預估耗時
一週以上
新手友好度
25/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
lua
領域
backend

研究方向

Start by reading the existing image class and the set, remove, and draw_circle implementations; the issue does not name their files or tests. Then study how Lua anonymous functions could support a mutate block and how libvips handles copied image data. Done means a mutable image class safely gates destructive operations and returns a new image after the block.

由索引模型根據 Issue 內容生成。

描述

enhancement

The ruby-vips binding has a nice feature for encapsulating destructive operations, like metadata change or the draw operations:

https://www.libvips.org/2021/03/08/ruby-vips-mutate.html

tldr: you can write eg.:

z = x.mutate do |y|
  1000.times do 
    y.draw_circle! Array.new(3) {rand(255)},
      rand(y.width), rand(y.height), rand(100), fill: true
  end
end
  1. mutate takes an image (x in this case) and makes an instance of a class called MutableImage
  2. This constructor runs copy() to make a private copy of the underlying vips image, and subclasses Image, adding a few new methods such as draw_circle! (ruby uses the ! suffix on method names to indicate destructive operations)
  3. mutate executes the block after, passing in the mutable image (y in this case)
  4. draw_circle! modifies the image you give it by erm drawing a circle ... the first time this happens, libvips will force the iamge into memory, ie. allocate a huge ram buffer and render the image into that
  5. Now the 1000 circles are drawn directly into memory with no copying and no chance of side effects
  6. Finally, the mutable block ends, and mutate uses the modified vips image to build a new instance of the Image class
  7. And this new Image instance is returned (and assigned to z in this code)

So this scheme automates the copying that lua-vips requires right now before you can use destructive operations like metadata changes, or line or circle draw. It has locks to prevent things like set or remove being used on a non-mutable image, so it's always safe.

You see a nice speedup too: that code is more than 10x faster and needs less than 1/10th the memory compared to ruby-vips without mutate.

This would be a nice thing to add to lua-vips. You could perhaps use anonymous functions, so the code above might be:

z = x:mutate(function (y)
  for i = 1, 1000 do
    y:draw_circle(... stuff)
  end
end)

You'd need to add a new mutable image class and some locks to gate access to set, remove, draw_circle, etc.

主要語言
Lua
星號
151
分支
10
PR 合併指標
30 天內沒有已合併 PR

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

libvips/lua-vips 的其他 Issue

查看 libvips/lua-vips 的全部 Issue

相似的 Issue

更多 Lua Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。