kyamagu / skia-python

Python binding to Skia Graphics Library
https://kyamagu.github.io/skia-python/
BSD 3-Clause "New" or "Revised" License
231 stars 42 forks source link

Missing `skia.TableColorFilter.MakeARGB` equivalent in latest builds #227

Closed stenson closed 7 months ago

stenson commented 8 months ago

I’ve been using skia.TableColorFilter.MakeARGB extensively for a while now in the m87 version, and I can see there was a port of skia.TableColorFilter.Make (without ARGB) and a commented-out port of skia.TableColorFilter.MakeARGB — is there a chance of that backfill function being uncommented or of SkColorFilter::TableARGB being exposed?

HinTak commented 7 months ago

Yes, those two classes are substantiallly different after m87. If you had something you have been using that you like to work in m12x, posting some small examples (ideally complete/stand-alone) would be useful.

stenson commented 7 months ago

I use skia.TableColorFilter.MakeARGB regularly to do alpha-channel contrast cuts on graphics (i.e. blurring them and then "choking" the blur with an alpha contrast cut), so I'd very much like to see that carried forward in skia-python.

I've adapted one of the notebook examples in skia-python to demonstrate how I use the skia.TableColorFilter.MakeARGB. I've made available as an interactive notebook (https://colab.research.google.com/drive/1s43QdL7W0RyUB1_Z2Vg8iW3kPVZQxlNe?usp=sharing), and copied the code inline below with the images that result.

This isn't a great example in terms of "why I want it" (since this just ends up looking like a rounded rectangle), but I do regularly use this effect with Paths to simulate high-contrast film stock, which is an effect that I've found almost impossible to achieve in realtime with code with any other python libraries.

import skia
import contextlib
from IPython.display import display, Image

@contextlib.contextmanager
def draw_target(width=100, height=100, save=None):
    surface = skia.Surface(width, height)
    canvas = surface.getCanvas()
    yield canvas
    image = surface.makeImageSnapshot()
    pimg = Image(data=image.encodeToData())
    if save:
        with open(save, "wb") as img:
            img.write(image.encodeToData())
    display(pimg)

with draw_target(save="test.png") as canvas:
    paint = skia.Paint(Color=skia.ColorBLACK, MaskFilter=skia.MaskFilter.MakeBlur(skia.kNormal_BlurStyle, 5.0))
    rect = skia.Rect.MakeXYWH(10, 10, 50, 50)
    canvas.drawRect(rect, paint)

def contrast_cut(mp=127, w=5):
    ct = bytearray(256)
    for i in range(256):
        if i < mp - w: ct[i] = 0
        elif i < mp: ct[i] = int((255.0/2)*(1-(mp-i)/w))
        elif i == mp: ct[i] = 127
        elif i < mp + w: ct[i] = int(127+(255.0/2)*((i-mp)/w))
        else: ct[i] = 255
    return ct

with draw_target() as canvas:
    ct = contrast_cut(157, 15)
    image = skia.Image.MakeFromEncoded(skia.Data.MakeFromFileName("test.png"))
    paint = skia.Paint(ColorFilter=skia.TableColorFilter.MakeARGB(ct, None, None, None))
    canvas.drawImage(image, 0, 0, paint)

download-1 download-2

HinTak commented 7 months ago

Oh, it probably isn't hard to put it back - it is mentioned in skia's release note how to:

https://github.com/google/skia/blob/main/RELEASE_NOTES.md

Milestone 116

The deprecated SkTableColorFilter class and its methods have been removed. Clients should use SkColorFilters::Table and SkColorFilters::TableARGB (defined in include/core/SkColorFilter.h).

HinTak commented 7 months ago

Fixed in https://github.com/kyamagu/skia-python/pull/222 - if you could download the new build and give it a go to your code base and see what else is needed, that would be nice. Since skia m122 should be out any time now, skia-python m121 should be released in the next few days (and #222 will get a README.m121, etc before that).

By the way, I needed to change one line in your code - many image-related API gains a new "skia.SampleOption" class argument. For now, just the default constructor is available in skia-python. If you need anything else, let us know. It is supposed to replace the ealier "FilterQuality" arguments (mentioned in relnotes/README.m116.md ) . The one line change is as below (the last line in your code above):

-    canvas.drawImage(image, 0, 0, paint)
+    canvas.drawImage(image, 0, 0, skia.SamplingOptions(), paint)
stenson commented 7 months ago

Thank for looking into it! Is there a doc on how to pip install a pre-release version of skia-python from a branch and have it use the pre-built artifacts? I tried pip install git+https://github.com/HinTak/skia-python.git@m121-public but that tries (and fails) to build skia from scratch.

HinTak commented 7 months ago

@stenson I believe you can download the CI build artefacts at the bottom of https://github.com/kyamagu/skia-python/actions/runs/7563583701 , unpack it , pick the one you need (presumably x86_64 linux) and just "pip install " . I have never done that myself, however :-).

HinTak commented 7 months ago

The CI builds are linked from the action tab - https://github.com/kyamagu/skia-python/actions - you want the latest m121 public build.

stenson commented 7 months ago

Ah thanks! That works perfectly just pip installing the .whl file directly — and skia.TableColorFilter.MakeARGB now works perfectly as well

HinTak commented 7 months ago

@stenson if you find anything else you need that's broken since m87, filing them in the next few days would be a good idea. we'll put it in if it isn't too troublesome to add.