Home > other >  Nearest neighbour scaling in Core Image
Nearest neighbour scaling in Core Image

Time:11-20

I'd like to efficiently create an up-scaled CIImage from a minimally sized one, using Nearest Neighbour scaling.

Say I want to create an image at arbitrary resolutions such as these EBU Color Bars:

enter image description here

In frameworks like OpenGL, we can store this a tiny 8x1 pixel texture and render it to arbitrary sized quads, and as long as we use Nearest Neighbour scaling the resulting image is sharp.

Our options with CIImage appear to be limited to .transformedBy(CAAffineTransform(scaleX:y:)) and .filteredBy(filterName: "CILanczosScaleTransform") which both use smooth sampling, which is a good choice for photographic images but will blur edges of line art images such as these color bars - I specifically want a pixellated effect.

Because I'm trying to take advantage of GPU processing in the Core Image backend, I'd rather not provide an already upscaled bitmap image to the process (using CGImage, for example)

Is there some way of either telling Core Image to use Nearest Neighbour sampling, or perhaps write a custom subclass of CIImage to achieve this?

CodePudding user response:

I think you can use samplingNearest() for that:

let scaled = image.samplingNearest().transformedBy()
  • Related