MotionMasks

A solution looking for problem?

 

by David Thomas — May 2016

Wasp: a Shape Processor

  • Picsel's 2D vector graphics engine
    • Written by Graham Borland
  • A software renderer
    • Big chunks of hand optimised ARM
    • Portable C equivalents too
  • Common PostScript style rendering model
    • Paths with Bezier curves
    • Filled paths
    • Stroked paths
  • Transparency
  • Figures composed of
    • Shape, Fill and Alpha

Figures

Clip Masks

  • Wasp has a thing called a Clip Mask
  • A cached result of scan conversion
    • the shape part of the figure
  • Like a stencil the same mask can be reused even if the fill and alpha attributes are changed

 

  • (The shape and the alpha do a similar job once rasterised)

Process (1)

An input path is given to Wasp

Process (2)

Its curves are flattened and the polygon is turned into a bunch of line intercepts (an Edge Buffer)

Process (3)

The Edge Buffer is downsampled 8:1 adding anti-aliasing

Process (4)

This gives us runs of on/off/edge data

Process (5)

Wasp encodes this naturally run-length encoded data as a Clip Mask

Adaptive RLE

Compression

  • Replace runs of identical values with R(count, value)
  • Replace runs of different values with L(count, values)
  • Pseudocode:
    1. find the longest string of identical input bytes

    2. find the longest string of non-identical input bytes

    3. emit them if non-zero long

    4. repeat

Decompression

  • if R: while count: emit value

  • if L: while count: copy values from input

Benefits of Clip Masks

  • Clip Masks are small
  • Very appropriate for rasterised font glyphs
    • Glyphs are mainly edges at small sizes
    • But mainly empty space at large sizes
  • Bigger clip masks save even more
  • Small means fast

Downsides...

  • Clip Masks are fixed single-resolution and orientation
  • Non-trivial to edit once created
    • You have to unpack, edit, pack
    • Almost might as well just start over
  • The compression can have some worst-case behaviour, e.g. certain patterns

But Here's the Thing

  • Sparse RLE'd data sits nicely in the CPU's cache
  • Consider memcpy vs memset
  • CPU is faster than RAM
  • CPU waits for RAM all the time
  • So the less RAM we shift around the quicker we can work
  • So is trivial compression of sparse data structures a good idea in general?

So...

Can I take this idea, then make something cool with it (and not get sued)?

(Clip Masks are patented...)

Motion Masks!

  • Start with a slightly different problem
  • Let's say you have two static images and you want to create an animated transition between them

A

B

Fade

Wipe

Masks

  • You could use a third "image" to say which input image to select for each output pixel
  • Storing a byte per pixel you can have "full A" to "full B"
    • Intermediate values would mean a proportional blend of the two inputs

Fade & Mask

Wipe & Mask

Masks

  • Of course, storing the masks literally will be wasteful
  • The animation frames are likely to be 'sparse'
    • containing long runs of identical intensity
    • but few 'edge' pixels
  • Clearly they're prime for RLE compression
  • We can use the Clip Mask techniques

There is a QUIZ on the next page. Get ready!

STOP:

IT IS A QUIZ

  • What does SCUBA stand for?

The Third Dimension

  • But this isn't just about static 2D images
  • Motion Masks animate over time
  • Many complete scanlines (rows) will be identical across all frames in the animation
  • We can therefore factor out identical scanlines by considering a frame to be a vector of pointers to compressed scanlines
    • We can logically stack the images vertically, hash the scanlines then sort all the scanlines into order

IT BE DEMO TIME

IT BE DEMO TIME

IT BE DEMO TIME

IT BE DEMO TIME

IT BE DEMO TIME

#DEMOTIME

Lessons Learned

  • The technique is neat but it's hard to find a solid use for the project as-is
    • The fades and wipes require static imagery and careful design so any designer will likely be frustrated with its limitations
    • All the cool kids are on GPU these days, even on mobile
    • Distinct lack of voxels
  • So the project starts to look useful more as a demo of the cache benefits of RLE compression rather than a useful thing in itself

Links

The MotionMasks project on github:

https://github.com/dpt/MotionMasks

Made with Slides.com