Add an ignore_mask argument to ignore certain source pixels #11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Explanation
Descaling models resampling as a linear transformation, but in practice the resampling process is not always entirely linear: After rescaling, pixel values might be clamped to their value range. For example, when a white image is resampled with zero-padding and the kernel has negative lobes, the second and third pixel lines from the edge will be clamped.
When descaling, these clamped pixels give wrong information, so they will negatively affect the descale result. In the above example, they will cause dirty borders in the descaled image.
One approach to fixing this problem is to simply discard any pixels which are suspected to be clipped from the linear system of equations. Since the system of equations is overdetermined, this can still give an accurate descale result, as long as no longer contiguous stretches of pixels are discarded.
This commit adds an
ignore_maskargument to the descale functions. Pixels selected by this mask will have their corresponding equations dropped from the system of equations. This requires partially recomputing the matrix and its LDLT decomposition whenever this mask changes, but this is still reasonably efficient since the matrix is banded.The
ignore_maskis only supported when only descaling along one axis: When descaling along both axes, a second mask at an intermediate resolution would be required for the second descale, so this is better off being left to the userOther details
ignore_maskforcesDESCALE_OPT_NONE. Getting this to work with AVX2 is likely somewhere between very hard and impossible.ignore_maskis required to use 8-bit integer samples, this could be improved in the future.ignore_maskto handle clipping around borders was around four times slower than a normal descale using AVX2 and around two times slower than a normal descale using the C function.Example usage
This code masks clipped values around borders to prevent dirty edges when descaling. Other masks can be used to also handle clipping around high-contrast edges in the middle of the frame, as long as care is taken to not have too many contiguous ignored pixels.
Note also the order of the descales in the
clipmasked_descalefunction - first descaling vertically, and then horizontally. The order is important here and needs to match the order used when originally resampling to properly handle values clipping in between the two steps. Getting the order wrong causes artifacts around corners.