dymokomi/luce-image
Source of release 0.45.0
0.45.0 / src / canvas / ranging.lucb
#==============================================================================================
#
# canvas/ranging - Select › Color Range while its dialog is up
#
# DESCRIPTION:
# The dialog shows its selection live as samples, Fuzziness and the range change. Each
# change is worked out on a small copy of the picture — the compositor's pyramid level
# no wider than 2048 pixels, with every pixel's Lab kept — and scaled up into the
# selection, so a change costs a fraction of a second on any document. None of it is
# history: OK works the range out once at full size and records one step from the
# selection the dialog opened on; Cancel puts that selection back.
#
#==============================================================================================
extend Canvas:
## Forget Color Range's sampled colors.
pub func clear_range_samples():
self.range_count = 0
## A color (linear light) for Color Range to select, or, `subtract`, to take back out.
pub func add_range_sample(red: f64, green: f64, blue: f64, subtract: bool) -> !:
if not math.is_finite(red) or not math.is_finite(green) or not math.is_finite(blue):
error(invalid, "range samples must be finite")
if self.range_count >= self.range_samples.length:
error(limit, "Color Range keeps at most 32 samples")
self.range_samples[self.range_count] = range_sample(red, green, blue, subtract)
self.range_count += 1
## The Color Range dialog opened: remember the selection, and make the small
## copy of the picture its changes are worked out on.
pub func begin_color_range() -> !:
try self.require_open()
end_range(&self)
let document = self.document else error(invalid, "draw the document once before selecting a color range")
let composer = self.composer else error(invalid, "draw the document once before selecting a color range")
var level: u32 = 0
while (max_u32(self.extent_x, self.extent_y) >> level) > range_preview_side:
level += 1
let scale = 1u32 << level
let width = (usize)((self.extent_x + scale - 1) >> level)
let height = (usize)((self.extent_y + scale - 1) >> level)
let pixels = try new f32[width * height * 4] --- in memory.heap
errdefer free(pixels) in memory.heap
let device = self.device else error(invalid, "draw the document once before selecting a color range")
# The picture at the level, read back in batches (canvas/files.lucb's FlatRead).
var flat = FlatRead(composer = composer, document = document, pixels = pixels, extent_x = self.extent_x, extent_y = self.extent_y, level = level,
columns = (u32)((width + (usize)tile_size - 1) // (usize)tile_size), width = width)
try read_pieces(device, (usize)flat.columns * ((height + (usize)tile_size - 1) // (usize)tile_size), (void*)&flat, flat_piece, flat_visit)
let labs = try new f32[width * height * 3] --- in memory.heap
errdefer free(labs) in memory.heap
try range_labs(pixels, labs)
let session = try new RangeSession(before = try self.current().copy(), width = width, height = height, scale = scale, pixels = pixels, labs = labs) in memory.heap
self.range_session = session
## The dialog's selection for these settings and the samples added, shown at
## once and kept out of history.
pub func preview_color_range(range: i64, fuzziness: f64, invert: bool) -> !:
let session = self.range_session else error(invalid, "begin the color range first")
let small = try new u8[session.width * session.height] --- in memory.heap
defer free(small) in memory.heap
try range_coverage(session.pixels, self.range_samples[..<self.range_count], range, fuzziness, invert, small, session.labs)
let found = try new u8[(usize)self.extent_x * (usize)self.extent_y] --- in memory.heap
defer free(found) in memory.heap
enlarge(small, session.width, session.height, session.scale, found, (usize)self.extent_x, (usize)self.extent_y)
try self.live().from_coverage(found, Mode.replace)
## The dialog closed: OK (`keep`) selects the range at full size as one step;
## Cancel puts back the selection it opened on.
pub func finish_color_range(keep: bool, range: i64, fuzziness: f64, invert: bool) -> !:
let session = self.range_session else error(invalid, "begin the color range first")
try self.live().merge(&session.before, Mode.replace)
end_range(&self)
if keep:
try self.select_color_range(0, range, fuzziness, invert)
## Select › Color Range: the pixels of layer `id` — or of the whole picture
## with `all_layers` — in `range` (0 the sampled colors within `fuzziness`,
## 1..6 reds to magentas, 7 highlights, 8 midtones, 9 shadows), turned
## around by `invert`, combined by `mode`.
pub func select_color_range(id: i64, range: i64, fuzziness: f64, invert: bool, mode: i64 = 0, all_layers: bool = true) -> !:
try self.require_open()
try self.remember_state("Color Range")
var pixels: f32[]
if all_layers:
pixels = try flattened_pixels(&self)
else:
let document = self.document else error(invalid, "draw the document once before selecting a color range")
let layer = document.find((u64)id) else error(invalid, "no such layer")
let tiles = layer.tiles() else error(invalid, "a group has no pixels to look at")
pixels = try new f32[(usize)self.extent_x * (usize)self.extent_y * 4] --- in memory.heap
tiles.read(pixels) catch failure:
free(pixels) in memory.heap
error(failure.code, failure.message)
defer free(pixels) in memory.heap
let found = try new u8[(usize)self.extent_x * (usize)self.extent_y] --- in memory.heap
defer free(found) in memory.heap
try range_coverage(pixels, self.range_samples[..<self.range_count], range, fuzziness, invert, found)
if not self.shaping():
try self.live().from_coverage(found, try selection_mode(mode))
return
var shape = try Selection.create(self.extent_x, self.extent_y)
defer shape.destroy()
try shape.from_coverage(found, Mode.replace)
try self.merge_shape(&shape, mode)
## What the dialog works on: the selection it opened on, and the small copy.
struct RangeSession:
var before: Selection
var width: usize
var height: usize
## Document pixels a copy pixel spans across.
var scale: u32
var pixels: f32[]
var labs: f32[]
func destroy():
self.before.destroy()
free(self.pixels) in memory.heap
free(self.labs) in memory.heap
## The longest side the dialog's copy of the picture has.
let range_preview_side: u32 = 2048
## Drop the dialog's session, if any.
func end_range(canvas: Canvas*):
let session = canvas.range_session else return
canvas.range_session = none
session.destroy()
free(session) in memory.heap
## `small` (`width` by `height`, one byte a pixel, each spanning `scale`
## document pixels) read between its pixels into every byte of `found`.
func enlarge(small: const u8[], width: usize, height: usize, scale: u32, found: u8[], extent_x: usize, extent_y: usize):
let step = 1.0 / (f64)scale
for y in 0..<extent_y:
let sy = math.clamp(((f64)y + 0.5) * step - 0.5, 0.0, (f64)(height - 1))
let y0 = (usize)sy
let y1 = min_usize(y0 + 1, height - 1)
let fy = sy - (f64)y0
for x in 0..<extent_x:
let sx = math.clamp(((f64)x + 0.5) * step - 0.5, 0.0, (f64)(width - 1))
let x0 = (usize)sx
let x1 = min_usize(x0 + 1, width - 1)
let fx = sx - (f64)x0
let top = (f64)small[y0 * width + x0] * (1.0 - fx) + (f64)small[y0 * width + x1] * fx
let bottom = (f64)small[y1 * width + x0] * (1.0 - fx) + (f64)small[y1 * width + x1] * fx
found[y * extent_x + x] = (u8)(top * (1.0 - fy) + bottom * fy + 0.5)
func min_usize(a: usize, b: usize) -> usize:
return a if a < b else b