Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-image

Source of release 0.45.0

0.45.0 / src / canvas / histograms.lucb

#==============================================================================================
#
#   canvas/histograms - The Levels histogram, counted a cell or a band at a time
#
#   DESCRIPTION:
#       A histogram's bins are the 256 values of 8-bit sRGB, so the counting reads exactly
#       that: a layer's cells one at a time, turned to 8-bit sRGB through the same 4097-step
#       table the export uses, or the flattened picture as the export's composited bands.
#       Transparent pixels are left out; luminosity weighs the encoded channels 0.3, 0.59 and
#       0.11. Nothing the size of the document is made.
#
#==============================================================================================

extend Canvas:
    ## Count layer `id`'s visible pixels by their encoded values for
    ## `histogram`: Auto levels and the Levels dialog read it.
    pub func measure_histogram(id: i64) -> !:
        try self.require_open()
        let document = self.document else error(invalid, "draw the document once before measuring")
        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 count")
        self.clear_histogram()
        try count_layer(self.histogram_counts[..<1024], tiles)

    ## Count what shows below layer `id` — the whole picture with it and the
    ## layers over it hidden — for `histogram`: what an adjustment layer's
    ## Levels takes in.
    pub func measure_histogram_below(id: i64) -> !:
        try self.require_open()
        let document = self.document else error(invalid, "draw the document once before measuring")
        let index = try self.index((u64)id)
        let hidden = try new bool[self.count] --- in memory.heap
        defer free(hidden) in memory.heap
        for at in 0..<self.count:
            let record = self.records[at]
            # The groups holding it stay: hiding them would hide what is below it inside them.
            hidden[at] = at >= index and record.visible and not self.holds(record.id, (u64)id)
            if hidden[at]:
                try document.set_appearance(record.id, false, record.opacity, (u32)record.blend)
        self.clear_histogram()
        count_flattened(self.histogram_counts[..<1024], &self) catch failure:
            self.show_again(hidden)
            error(failure.code, failure.message)
        self.show_again(hidden)

    ## Whether group `group` holds layer `id`, however deep.
    func holds(group: u64, id: u64) -> bool:
        var parent = id
        while parent != 0:
            let at = self.index(parent) catch _:
                return false
            parent = self.records[at].parent
            if parent == group:
                return true
        return false

    ## Put back the visibility `measure_histogram_below` took away.
    func show_again(hidden: const bool[]):
        let document = self.document else return
        for at in 0..<self.count:
            if hidden[at]:
                let record = self.records[at]
                document.set_appearance(record.id, true, record.opacity, (u32)record.blend) catch _:
                    recover ()

    ## Empty the histogram's bins before a count.
    func clear_histogram():
        for slot in 0..<(usize)1024:
            self.histogram_counts[slot] = 0

    ## How many pixels `measure_histogram` counted in bin `slot` (0..255) of
    ## `channel`: 0 luminosity, 1 red, 2 green, 3 blue.
    pub func histogram(channel: i64, slot: i64) -> i64!:
        if channel < 0 or channel > 3 or slot < 0 or slot > 255:
            error(invalid, "a histogram has channels 0..3 and bins 0..255")
        return (i64)self.histogram_counts[(usize)(channel * 256 + slot)]

## Count `count` straight 8-bit sRGB RGBA pixels of `rgba` into `counts`
## (luminosity, then red, green and blue, 256 bins each).
func count_rgba8(counts: u64[], rgba: const u8[], count: usize):
    for at in 0..<count:
        let base = at * 4
        if rgba[base + 3] == 0:
            continue
        let red = (usize)rgba[base]
        let green = (usize)rgba[base + 1]
        let blue = (usize)rgba[base + 2]
        counts[256 + red] += 1
        counts[512 + green] += 1
        counts[768 + blue] += 1
        counts[(usize)(((f64)red * 0.3 + (f64)green * 0.59 + (f64)blue * 0.11) + 0.5)] += 1

## Layer `tiles` counted a cell at a time into `counts`.
func count_layer(counts: u64[], tiles: const Tiles*) -> !:
    let side = (usize)tile_size
    let samples = try new f32[side * side * 4] --- in memory.heap
    defer free(samples) in memory.heap
    let rgba = try new u8[side * 4] --- in memory.heap
    defer free(rgba) in memory.heap
    var steps: u8[4097] = ---
    transfer.srgb_byte_table(steps)
    for row in 0..<tiles.rows():
        for column in 0..<tiles.columns():
            if not tiles.painted(column, row):
                continue
            try tiles.read_cell(column, row, samples)
            let area = tiles.region(column, row)
            for y in 0..<(usize)area.height:
                for x in 0..<(usize)area.width:
                    let source = (y * side + x) * 4
                    for channel in 0..<(usize)3:
                        rgba[x * 4 + channel] = steps[(usize)(math.clamp((f64)samples[source + channel], 0.0, 1.0) * 4096.0 + 0.5)]
                    rgba[x * 4 + 3] = 0 if samples[source + 3] <= 0.0 else 255
                count_rgba8(counts, rgba, (usize)area.width)

## The flattened picture counted a composited band at a time into `counts`.
func count_flattened(counts: u64[], canvas: Canvas*) -> !:
    var bands = try Bands.begin(canvas, [-1.0, -1.0, -1.0])
    defer bands.close()
    let width = (usize)canvas.extent_x
    for row in 0..<tile_columns(canvas.extent_y):
        let rows = try bands.fill(canvas, row)
        count_rgba8(counts, bands.band, width * rows)