Silence-GitHub / BBMetalImage

A high performance Swift library for GPU-accelerated image/video processing based on Metal.
MIT License
988 stars 126 forks source link

outputTexture is nil on some images within the Assets #5

Closed iDevid closed 4 years ago

iDevid commented 5 years ago

I tried to create a filteredImage from an image, but it returns nil. The strange thing is that this image is under the Assets folder, If I move it in the general projects file, the filter then works.

Have you any idea?

iDevid commented 5 years ago

You could test it out with this image: test

Silence-GitHub commented 5 years ago

Thanks for issuing me. I found the problem.

To process an image, we need the metal texture of the image. We use MTKTextureLoader and CGImage to get the metal texture. When the test image is a general project file, the color space of CGImage is RGB. However, when the test image is under the Asset folder, the color space becomes Monochrome. MTKTextureLoader can not decode image for Monochrome color space. So we can not get the metal texture of the image. The similar problem can be found here.

To solve the problem:

  1. Create an empty metal texture
  2. Create CGContext with device RGB color space, draw the CGImage, and get the bitmap data
  3. Copy bitmap data to the metal texture

I will fix it in the next version.

Silence-GitHub commented 5 years ago

Version 1.0.1 fixes it.

iDevid commented 5 years ago

Image Quality seems very bad, the same Filter trough CIFilter is very much better. You can see in the demo, that if you apply the filter on this type of images, they become shelled.

Silence-GitHub commented 5 years ago

I have not reproduced the bad quality problem. Could you please show me the code? It will help me if there is a simple demo.

Silence-GitHub commented 5 years ago

I test BBMetalColorMatrixFilter and CIFilter with CIColorMatrix. It is true that they are different, and monochrome image becomes shelled after apply BBMetalColorMatrixFilter. But I can not tell which filter gets good or bad image quality. I also test GPUImageColorMatrixFilter (famous GPUImage framework). It has the same result as BBMetalColorMatrixFilter, making monochrome image shelled. Maybe CIColorMatrix uses different shader, or contains more image processing steps.