dymokomi/luce-image
Source of release 0.45.1
0.45.1 / src / canvas / files.lucb
#==============================================================================================
#
# canvas/files - Opening and saving pictures and Photoshop files (.l2d: package_*.lucb)
#
#==============================================================================================
extend Canvas:
## Put the picture at `path`, of any size, on layer `id` centred on document
## pixel (`center_x`, `center_y`): what falls outside the document is cut off
## and the rest of the layer is transparent. Dropping a file on a document
## does this. The document must have been drawn once.
pub func place_layer(id: i64, path: str, center_x: i64, center_y: i64) -> !:
if is_svg(path):
if let found = try svg_vectors(path):
var imported = found
let left = (f64)(center_x - (i64)imported.width // 2)
let top = (f64)(center_y - (i64)imported.height // 2)
adopt_svg_vectors(&self, (u64)id, &imported, left, top) catch failure:
imported.close()
error(failure.code, failure.message)
return
let picture = try Image.open(path, max_bytes = picture_bytes, max_pixels = picture_pixels)
if places_in_background(picture) and self.document != none:
try place_in_background(&self, id, picture, center_x, center_y)
return
defer picture.release()
try place_image_at(&self, id, picture, center_x, center_y)
## Write the document with all its layers to `path`, one `.l2d` file of
## their half-float tiles, a manifest and a preview (docs/FORMATS.md).
## Needs the document drawn once.
pub func save_package(path: str) -> !:
try require_loaded(&self)
try self.wait_idle()
try save_canvas_package(&self, path)
self.mark_saved()
## Write the flattened document to `path` as an 8-bit RGBA picture in the
## format its extension names. Needs the document drawn at least once.
pub func save(path: str, quality: i64 = 90, matte_red: f64 = -1.0, matte_green: f64 = 0.0, matte_blue: f64 = 0.0) -> !:
try require_loaded(&self)
try self.wait_idle()
try save_flattened(&self, path, quality, matte_red, matte_green, matte_blue)
## The most pixels a flattened save in a format without streaming takes (TIFF, OpenEXR).
let whole_picture_save_limit: u64 = 64 * 1024 * 1024
## The deflate level of a PNG export.
let png_export_level: i32 = 4
## `Canvas.save`: the flattened document as a picture, the format from `path`'s extension.
func save_flattened(canvas: Canvas*, path: str, quality: i64, matte_red: f64, matte_green: f64, matte_blue: f64) -> !:
let lower = try strings.to_lower(path)
defer strings.release(lower)
if strings.ends_with(lower, ".jpg") or strings.ends_with(lower, ".jpeg"):
# JPEG has no alpha: without a matte, white is what shows through.
if matte_red >= 0.0:
try save_jpeg_streamed(canvas, path, quality, matte_red, matte_green, matte_blue)
else:
try save_jpeg_streamed(canvas, path, quality, 1.0, 1.0, 1.0)
return
if strings.ends_with(lower, ".png"):
try save_png_streamed(canvas, path, matte_red, matte_green, matte_blue)
return
# TIFF and OpenEXR encode a whole picture at once; past a size their
# encoders would need the document many times over in memory.
if (u64)canvas.extent_x * (u64)canvas.extent_y > whole_picture_save_limit:
error(limit, "documents over 64 megapixels export as PNG or JPEG, or save as .l2d; TIFF and OpenEXR are not streamed yet")
let pixels = try flattened_pixels(canvas)
defer free(pixels) in memory.heap
# Under a matte (linear light; a negative red means none) the picture comes out
# opaque: what JPEG, without alpha, needs. It mixes as the document blends.
if matte_red >= 0.0:
let matte: f64[3] = [matte_red, matte_green, matte_blue]
let encoded = not canvas.linear_blend()
for at in 0..<pixels.length // 4:
let alpha = (f64)pixels[at * 4 + 3]
for channel in 0..<(usize)3:
let value = (f64)pixels[at * 4 + channel]
if encoded:
let mixed = transfer.srgb_encode(value) * alpha + transfer.srgb_encode(matte[channel]) * (1.0 - alpha)
pixels[at * 4 + channel] = (f32)transfer.srgb_decode(mixed)
else:
pixels[at * 4 + channel] = (f32)(value * alpha + matte[channel] * (1.0 - alpha))
pixels[at * 4 + 3] = 1.0
let picture = try Image.from_linear_rgba((usize)canvas.extent_x, (usize)canvas.extent_y, pixels)
defer picture.release()
if matte_red >= 0.0:
# Opaque now: RGB, which every format takes (JPEG takes nothing else).
let opaque = try picture.value().convert("RGB")
defer opaque.release()
try opaque.value().save(path, quality = quality)
return
try picture.value().save(path, quality = quality)
## A JPEG of the flattened document over the matte, made a row of cells at a
## time: each row of composited tiles becomes sRGB bytes and goes straight to the
## encoder, so no buffer the size of the document is ever held.
func save_jpeg_streamed(canvas: Canvas*, path: str, quality: i64, matte_red: f64, matte_green: f64, matte_blue: f64) -> !:
var bands = try Bands.begin(canvas, [matte_red, matte_green, matte_blue])
defer bands.close()
var encoded = Buffer(maximum = (usize)1 << 40)
defer encoded.close()
var encoder = try jpeg.Encoder.begin((usize)canvas.extent_x, (usize)canvas.extent_y, jpeg.Input.rgba, &encoded, jpeg.EncodeOptions(quality = quality))
defer encoder.close()
for row in 0..<tile_columns(canvas.extent_y):
let rows = try bands.fill(canvas, row)
try encoder.write_rows(bands.band[..<(usize)canvas.extent_x * rows * 4], rows)
try encoder.finish()
try write_file(path, encoded.view())
## A PNG of the flattened document, straight alpha kept (or over the matte when
## one is given), streamed a row of cells at a time as the JPEG is.
func save_png_streamed(canvas: Canvas*, path: str, matte_red: f64, matte_green: f64, matte_blue: f64) -> !:
var bands = try Bands.begin(canvas, [matte_red, matte_green, matte_blue])
defer bands.close()
var encoded = Buffer(maximum = (usize)1 << 40)
defer encoded.close()
# Level 4: about a third less work than 6 for a few percent more size, as
# an editor's export should trade.
var encoder = try png.Encoder.begin((usize)canvas.extent_x, (usize)canvas.extent_y, png.Format.rgba8, &encoded, png.EncodeOptions(level = png_export_level))
defer encoder.close()
for row in 0..<tile_columns(canvas.extent_y):
let rows = try bands.fill(canvas, row)
try encoder.write_rows(bands.band[..<(usize)canvas.extent_x * rows * 4], rows)
try encoder.finish()
try write_file(path, encoded.view())
func write_file(path: str, bytes: const u8[]) -> !:
let destination = try strings.copy(path)
defer strings.release(destination)
try files.write_atomic((c_api.str)destination, bytes)
## The flattened document as straight-alpha linear RGBA f32 samples, top-down,
## four per pixel: heap storage the caller frees.
func flattened_pixels(canvas: Canvas*) -> f32[]!:
try canvas.require_open()
let document = canvas.document else error(invalid, "draw the document once before reading it flattened")
let composer = canvas.composer else error(invalid, "draw the document once before reading it flattened")
let device = canvas.device else error(invalid, "draw the document once before reading it flattened")
let count = (usize)canvas.extent_x * (usize)canvas.extent_y * 4
let pixels = try new f32[count] --- in memory.heap
errdefer free(pixels) in memory.heap
var flat = FlatRead(composer = composer, document = document, pixels = pixels, extent_x = canvas.extent_x, extent_y = canvas.extent_y, level = 0,
columns = tile_columns(canvas.extent_x), width = (usize)canvas.extent_x)
try read_pieces(device, (usize)flat.columns * (usize)tile_columns(canvas.extent_y), (void*)&flat, flat_piece, flat_visit)
# The tiles the read brought back to RAM may leave again at once.
try settle_job(canvas)
return pixels
## A flattened read at pyramid level `level`: the composited tiles, `columns`
## a row, into `pixels`, `width` pixels a row.
struct FlatRead:
var composer: Composer*
var document: const Document*
var pixels: f32[]
var extent_x: u32
var extent_y: u32
var level: u32
var columns: u32
var width: usize
## Composited cell `index` of the level, as much of it as the level covers.
func flat_piece(context: void*?, index: usize) -> Piece!:
let flat = (FlatRead*)(context else trap("a flattened read lost its state"))
let column = (u32)(index % (usize)flat.columns)
let row = (u32)(index // (usize)flat.columns)
let texture = try flat.composer.tile(flat.document, column, row, flat.level)
let width = min_u32(tile_size, level_size(flat.extent_x, flat.level) - column * tile_size)
let height = min_u32(tile_size, level_size(flat.extent_y, flat.level) - row * tile_size)
return Piece(texture = texture, region = gpu.Region(0, 0, width, height), present = true)
## A composited cell's half floats into place among the samples.
func flat_visit(context: void*?, index: usize, bytes: const u8[]) -> !:
let flat = (FlatRead*)(context else trap("a flattened read lost its state"))
let column = (u32)(index % (usize)flat.columns)
let row = (u32)(index // (usize)flat.columns)
let width = (usize)min_u32(tile_size, level_size(flat.extent_x, flat.level) - column * tile_size)
let height = bytes.length // (width * 8)
for y in 0..<height:
for x in 0..<width:
let source = (y * width + x) * 8
let destination = (((usize)row * (usize)tile_size + y) * flat.width + (usize)column * (usize)tile_size + x) * 4
for channel in 0..<(usize)4:
flat.pixels[destination + channel] = from_half((u16)bytes[source + channel * 2] | ((u16)bytes[source + channel * 2 + 1] << 8))
## A side's length at pyramid level `level`.
func level_size(extent: u32, level: u32) -> u32:
return (extent + (1u32 << level) - 1) >> level
## The flattened picture's bottom-right pixel as 8-bit sRGB, from its last band
## on bands of their own (the scan that follows goes through them in order).
func bottom_right_pixel(canvas: Canvas*) -> u8[4]!:
var bands = try Bands.begin(canvas, [-1.0, -1.0, -1.0])
defer bands.close()
let width = (usize)canvas.extent_x
let rows = try bands.fill(canvas, tile_columns(canvas.extent_y) - 1)
let at = ((rows - 1) * width + width - 1) * 4
return [bands.band[at], bands.band[at + 1], bands.band[at + 2], bands.band[at + 3]]
## Whether the pixel at `at` stays: any alpha (`based_on` 0), else unlike `corner`.
func trim_kept(band: const u8[], at: usize, based_on: i64, corner: u8[4]) -> bool:
if based_on == 0:
return band[at + 3] > 0
return band[at] != corner[0] or band[at + 1] != corner[1] or band[at + 2] != corner[2] or band[at + 3] != corner[3]
## A text layer's words and setting, in its manifest element.
func write_type(manifest: prism.Document*, element: str, spec: TypeSpec) -> !:
for (key, value) in [("text", spec.text), ("family", spec.family), ("font_style", spec.style)]:
let text = try prism.Value.text(value)
defer text.release()
try manifest.set(element, key, text)
let names = type_value_names()
let values: f64[10] = [spec.size, spec.red, spec.green, spec.blue, spec.x, spec.y, spec.box_width, (f64)spec.alignment, spec.tracking, spec.leading]
for index in 0..<(usize)10:
let number = try prism.Value.number(values[index])
defer number.release()
try manifest.set(element, names[index], number)
## The text a layer's element carries, if any: the layer is a text layer again
## (its pixels, already read, are what it was set as).
## A Curves layer's points: each channel's count (`cn0`..`cn3`), then every
## point's input and output (`cx`/`cy`, numbered channel * 16 + point).
func write_curves(manifest: prism.Document*, element: str, shape: Curves) -> !:
for channel in 0..<(usize)4:
let curve = try shape.channel(channel)
var key_buffer: u8[32]
try set_integer(manifest, element, try format_file(key_buffer, "cn", channel, ""), (i64)curve.length())
for point in 0..<curve.length():
for (prefix, value) in [("cx", curve.x(point)), ("cy", curve.y(point))]:
let number = try prism.Value.number(value)
defer number.release()
try manifest.set(element, try format_file(key_buffer, prefix, channel * 16 + point, ""), number)
## Stage the curves a Curves layer's element holds (`write_curves`).
func read_curves(doc: prism.Document*, element: str, canvas: Canvas*) -> !:
for channel in 0..<(usize)4:
var key_buffer: u8[32]
let count = get_integer(doc, element, try format_file(key_buffer, "cn", channel, "")) catch _:
continue
try canvas.clear_curve((i64)channel)
for point in 0..<(usize)count:
let x = try get_number(doc, element, try format_file(key_buffer, "cx", channel * 16 + point, ""))
let y = try get_number(doc, element, try format_file(key_buffer, "cy", channel * 16 + point, ""))
try canvas.add_curve_point((i64)channel, x, y)
func read_type(doc: prism.Document*, element: str, canvas: Canvas*, id: i64) -> !:
let text_value = doc.get(element, "text") catch _:
return
defer text_value.release()
let family_value = try doc.get(element, "family")
defer family_value.release()
let style_value = try doc.get(element, "font_style")
defer style_value.release()
let names = type_value_names()
var values: f64[10]
for index in 0..<(usize)10:
values[index] = try get_number(doc, element, names[index])
let index = try canvas.index((u64)id)
var spec = TypeSpec(text = "", family = "", style = "", size = values[0], red = values[1], green = values[2], blue = values[3], x = values[4], y = values[5],
box_width = values[6], alignment = (i64)values[7], tracking = values[8], leading = values[9])
with memory.heap:
spec.text = try strings.copy(try text_value.value().text_at())
spec.family = try strings.copy(try family_value.value().text_at())
spec.style = try strings.copy(try style_value.value().text_at())
release_type(&canvas.records[index])
canvas.records[index].typeset = spec
canvas.records[index].typed = true
func type_value_names() -> str[10]:
return ["type_size", "type_red", "type_green", "type_blue", "type_x", "type_y", "box_width", "alignment", "tracking", "leading"]
## Pictures a document opens or places may be this large: they go to tiles a
## band at a time (`linear_rgba_bands`), so only the file itself is held.
let picture_pixels: usize = (usize)1 << 32
let picture_bytes: usize = (usize)1 << 40
## Tiles being made from a picture, a band of 256 rows at a time.
struct PictureBands:
var canvas: Canvas*
var tiles: Tiles*
## For encoded samples: a band packed at a time, and each code's half float.
var band: u8[]
var painted: bool[]
var color: u16[]
var alpha: u16[]
func close():
if self.band.length > 0:
free(self.band) in memory.heap
if self.painted.length > 0:
free(self.painted) in memory.heap
if self.color.length > 0:
free(self.color) in memory.heap
free(self.alpha) in memory.heap
## `linear_rgba_bands`' sink: a band of rows becomes a row of cells.
func picture_band(context: void*?, first: usize, count: usize, pixels: f32[]) -> !:
let banding = (PictureBands*)(context else return)
_ = count
try banding.tiles.write_band((u32)(first // (usize)tile_size), pixels)
try settle_job(banding.canvas)
## `encoded_rgba_bands`' sink: the samples through tables of their half
## floats become a row of cells, no floats made on the way.
func picture_coded_band(context: void*?, first: usize, count: usize, samples: const u8[], deep: bool) -> !:
let banding = (PictureBands*)(context else return)
_ = count
if banding.band.length == 0:
let codes: usize = 65536 if deep else 256
banding.band = try new u8[packed_band_bytes(banding.tiles.width())] --- in memory.heap
banding.painted = try new bool[(usize)banding.tiles.columns()] --- in memory.heap
banding.color = try new u16[codes] --- in memory.heap
banding.alpha = try new u16[codes] --- in memory.heap
try encoded_tables(deep, banding.color, banding.alpha)
let row = (u32)(first // (usize)tile_size)
try pack_band_coded(banding.tiles.width(), banding.tiles.height(), row, samples, deep, banding.color, banding.alpha, banding.band, banding.painted)
try banding.tiles.write_packed(row, banding.band, banding.painted)
try settle_job(banding.canvas)
## `place_image_at` while frames go on: the layer fills in as the picture
## decodes (canvas/opening), after any picture still opening is in. Takes
## `picture`.
func place_in_background(canvas: Canvas*, id: i64, picture: interop.Reference[Image], center_x: i64, center_y: i64) -> !:
var width: usize = 0
var height: usize = 0
prepare_placing(canvas, id, picture, &width, &height) catch failure:
picture.release()
error(failure.code, failure.message)
try begin_opening(canvas, (u64)id, picture, center_x - (i64)width // 2, center_y - (i64)height // 2)
## What `place_in_background` checks before it starts: the layer, the picture's size.
func prepare_placing(canvas: Canvas*, id: i64, picture: interop.Reference[Image], width: usize*, height: usize*) -> !:
try canvas.require_open()
try canvas.finish_opening()
_ = try canvas.index((u64)id)
*width = try picture.value().width()
*height = try picture.value().height()
## `picture` becomes all of layer `id`'s pixels, centred on a document point;
## what falls outside the document is cut off.
func place_image_at(canvas: Canvas*, id: i64, picture: interop.Reference[Image], center_x: i64, center_y: i64) -> !:
try canvas.require_open()
let document = canvas.document else error(invalid, "draw the document once before placing a picture")
_ = try canvas.index((u64)id)
let width = try picture.value().width()
let height = try picture.value().height()
# The picture's tiles a band at a time, moved onto the canvas's grid: what
# lies past the canvas is kept past it, as a moved layer keeps it.
let tiles = try canvas.tiles_of(picture, center_x - (i64)width // 2, center_y - (i64)height // 2)
try document.set_tiles((u64)id, tiles)
## `Canvas.open`: a picture, a Photoshop document or an `.l2d` package, by extension.
func open_canvas(path: str) -> interop.Reference[Canvas]!:
if ends_with(path, ".l2d"):
return try open_package(path)
if ends_with(path, ".psd") or ends_with(path, ".PSD") or ends_with(path, ".psb") or ends_with(path, ".PSB"):
return try open_psd(path)
let picture = try Image.open(path, max_bytes = picture_bytes, max_pixels = picture_pixels)
errdefer picture.release()
let width = try picture.value().width()
let height = try picture.value().height()
var reservation = try interop.Reservation[Canvas](canvas_type)
errdefer reservation.cancel()
let native = try new Canvas((i64)width, (i64)height) in memory.heap
errdefer native.close()
let id = try native.add_layer("Background")
# A window that draws progressively shows it filling in (canvas/opening).
native.open_in_background = not is_svg(path)
try native.place((u64)id, picture)
# An SVG keeps its elements too: the picture is what they draw, until one is edited.
if is_svg(path):
if let found = try svg_vectors(path):
var imported = found
adopt_svg_vectors(native, (u64)id, &imported, 0.0, 0.0) catch failure:
imported.close()
error(failure.code, failure.message)
return reservation.publish(native, memory.heap)
## Whether `text` ends with `suffix`, byte for byte.
func ends_with(text: str, suffix: str) -> bool:
if text.length < suffix.length:
return false
return (str)text.bytes[text.length - suffix.length..] == suffix
## Manifest helpers: an integer or number at `path`.`name`.
func set_integer(doc: prism.Document*, path: str, name: str, value: i64) -> !:
let holder = try prism.Value.integer(value)
defer holder.release()
try doc.set(path, name, holder)
func get_integer(doc: prism.Document*, path: str, name: str) -> i64!:
let holder = try doc.get(path, name)
defer holder.release()
return try holder.value().integer_at()
func get_number(doc: prism.Document*, path: str, name: str) -> f64!:
let holder = try doc.get(path, name)
defer holder.release()
return try holder.value().number_at()
## Decimal digits of `value` into `buffer` at `at`; returns where they end.
func format_digits(buffer: u8[], at: usize, value: usize) -> usize:
var digits: u8[24]
var count: usize = 0
var remaining = value
if remaining == 0:
digits[0] = '0'
count = 1
while remaining > 0:
digits[count] = (u8)('0' + (u8)(remaining % 10))
remaining //= 10
count += 1
var position = at
while count > 0:
count -= 1
buffer[position] = digits[count]
position += 1
return position
## `prefix` followed by `index`, as a package member's name.
func format_path(buffer: u8[], prefix: str, index: usize) -> str!:
if prefix.length + 24 > buffer.length:
error(invalid, "path too long")
memory.copy(buffer, prefix.bytes, prefix.length)
let end = format_digits(buffer, prefix.length, index)
return (str)buffer[..<end]
## `prefix`, `index` and `suffix`, as a package member's file name.
func format_file(buffer: u8[], prefix: str, index: usize, suffix: str) -> str!:
if prefix.length + suffix.length + 24 > buffer.length:
error(invalid, "file name too long")
memory.copy(buffer, prefix.bytes, prefix.length)
var end = format_digits(buffer, prefix.length, index)
memory.copy(buffer[end..], suffix.bytes, suffix.length)
end += suffix.length
return (str)buffer[..<end]
## A Photoshop document (PSD or PSB): its layers bottom-most first, with
## their names, visibility, opacity, blend mode, clipping and masks, in their
## groups; a merged-only document as its one composite layer.
func open_psd(path: str) -> interop.Reference[Canvas]!:
var name: str = ""
with memory.heap:
name = try strings.copy(path)
defer strings.release(name)
let data = try files.read((c_api.str)name)
defer data.release()
var parsed = try psd.parse(data.value)
defer parsed.close()
var reservation = try interop.Reservation[Canvas](canvas_type)
errdefer reservation.cancel()
let native = try new Canvas((i64)parsed.width, (i64)parsed.height) in memory.heap
errdefer native.close()
# Which group holds each record: walking down from the top, a group's own
# record opens it and the divider below its layers closes it.
let parents = try new i64[parsed.count if parsed.count > 0 else 1] --- in memory.heap
defer free(parents) in memory.heap
let open_groups = try new i64[parsed.count if parsed.count > 0 else 1] --- in memory.heap
defer free(open_groups) in memory.heap
var depth: usize = 0
var index = parsed.count
while index > 0:
index -= 1
let kind = parsed.layers[index].kind
parents[index] = -1
if depth > 0:
parents[index] = open_groups[depth - 1]
if kind == psd.group_start:
open_groups[depth] = (i64)index
depth += 1
elif kind == psd.group_end and depth > 0:
depth -= 1
# Every record becomes a layer or a group, bottom first; then each goes into its group.
let ids = try new i64[parsed.count if parsed.count > 0 else 1] --- in memory.heap
defer free(ids) in memory.heap
# One mask's gray pixels at a time, as RGBA for a picture.
let gray = try new u8[(usize)parsed.width * (usize)parsed.height * 4] --- in memory.heap
defer free(gray) in memory.heap
for at in 0..<parsed.count:
let layer = &parsed.layers[at]
ids[at] = 0
let named = (str)layer.name if layer.name.length > 0 else ("Group" if layer.kind == psd.group_start else "Layer")
if layer.kind == psd.group_end:
continue
if layer.kind == psd.group_start:
ids[at] = try native.add_group(named)
else:
if layer.pixels.length == 0:
continue
ids[at] = try native.add_layer(named)
try native.place((u64)ids[at], try Image.from_rgba8((usize)parsed.width, (usize)parsed.height, layer.pixels))
try native.set_clipped(ids[at], layer.clipped)
try native.set_layer(ids[at], layer.visible, layer.opacity, (i64)layer.blend)
if layer.mask.length > 0:
try native.set_mask(ids[at], true)
for pixel in 0..<layer.mask.length:
gray[pixel * 4] = layer.mask[pixel]
gray[pixel * 4 + 1] = layer.mask[pixel]
gray[pixel * 4 + 2] = layer.mask[pixel]
gray[pixel * 4 + 3] = 255
let slot = try native.index((u64)ids[at])
native.records[slot].mask_picture = try Image.from_rgba8((usize)parsed.width, (usize)parsed.height, gray)
native.records[slot].mask_enabled = layer.mask_enabled
for at in 0..<parsed.count:
let parent = parents[at]
if ids[at] != 0 and parent >= 0 and ids[(usize)parent] != 0:
try native.move_into(ids[at], ids[(usize)parent], -1)
if native.count == 0:
_ = try native.add_layer("Background")
return reservation.publish(native, memory.heap)