Merge commit '7621e2f8dec938cf48181c8b10afc9b01f444e68' into beta

This commit is contained in:
Ilya Laktyushin
2025-12-06 02:17:48 +04:00
commit 8344b97e03
28070 changed files with 7995182 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
objc_library(
name = "FastBlur",
enable_modules = True,
module_name = "FastBlur",
srcs = glob([
"Sources/*.m",
]),
hdrs = glob([
"PublicHeaders/**/*.h",
]),
includes = [
"PublicHeaders",
],
sdk_frameworks = [
"Foundation",
"UIKit",
"Accelerate",
],
visibility = [
"//visibility:public",
],
)
@@ -0,0 +1,4 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
UIImage * _Nullable applyScreenshotEffectToImage(UIImage * _Nonnull image);
@@ -0,0 +1,13 @@
#ifndef Telegram_FastBlur_h
#define Telegram_FastBlur_h
#import <Foundation/Foundation.h>
#import <FastBlur/ApplyScreenshotEffect.h>
void imageFastBlur(int imageWidth, int imageHeight, int imageStride, void * _Nonnull pixels);
void telegramFastBlurMore(int imageWidth, int imageHeight, int imageStride, void * _Nonnull pixels);
void stickerThumbnailAlphaBlur(int imageWidth, int imageHeight, int imageStride, void * _Nonnull pixels);
void telegramBrightenImage(int imageWidth, int imageHeight, int imageStride, void * _Nonnull pixels);
#endif
@@ -0,0 +1,137 @@
#import <FastBlur/ApplyScreenshotEffect.h>
#import <Accelerate/Accelerate.h>
#import <float.h>
UIImage * _Nullable applyBlurWithRadius(UIImage *image, CGFloat blurRadius, UIColor * tintColor, CGFloat saturationDeltaFactor, UIImage * _Nullable maskImage) {
// Check pre-conditions.
if (image.size.width < 1 || image.size.height < 1) {
NSLog (@"*** error: invalid size: (%.2f x %.2f). Both dimensions must be >= 1: %@", image.size.width, image.size.height, image);
return nil;
}
if (!image.CGImage) {
NSLog (@"*** error: image must be backed by a CGImage: %@", image);
return nil;
}
if (maskImage && !maskImage.CGImage) {
NSLog (@"*** error: maskImage must be backed by a CGImage: %@", maskImage);
return nil;
}
CGRect imageRect = { CGPointZero, image.size };
UIImage *effectImage = image;
BOOL hasBlur = blurRadius > __FLT_EPSILON__;
BOOL hasSaturationChange = fabs(saturationDeltaFactor - 1.) > __FLT_EPSILON__;
if (hasBlur || hasSaturationChange) {
UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef effectInContext = UIGraphicsGetCurrentContext();
CGContextScaleCTM(effectInContext, 1.0, -1.0);
CGContextTranslateCTM(effectInContext, 0, -image.size.height);
CGContextDrawImage(effectInContext, imageRect, image.CGImage);
vImage_Buffer effectInBuffer;
effectInBuffer.data = CGBitmapContextGetData(effectInContext);
effectInBuffer.width = CGBitmapContextGetWidth(effectInContext);
effectInBuffer.height = CGBitmapContextGetHeight(effectInContext);
effectInBuffer.rowBytes = CGBitmapContextGetBytesPerRow(effectInContext);
UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef effectOutContext = UIGraphicsGetCurrentContext();
vImage_Buffer effectOutBuffer;
effectOutBuffer.data = CGBitmapContextGetData(effectOutContext);
effectOutBuffer.width = CGBitmapContextGetWidth(effectOutContext);
effectOutBuffer.height = CGBitmapContextGetHeight(effectOutContext);
effectOutBuffer.rowBytes = CGBitmapContextGetBytesPerRow(effectOutContext);
if (hasBlur) {
// A description of how to compute the box kernel width from the Gaussian
// radius (aka standard deviation) appears in the SVG spec:
// http://www.w3.org/TR/SVG/filters.html#feGaussianBlurElement
//
// For larger values of 's' (s >= 2.0), an approximation can be used: Three
// successive box-blurs build a piece-wise quadratic convolution kernel, which
// approximates the Gaussian kernel to within roughly 3%.
//
// let d = floor(s * 3*sqrt(2*pi)/4 + 0.5)
//
// ... if d is odd, use three box-blurs of size 'd', centered on the output pixel.
//
CGFloat inputRadius = blurRadius * [[UIScreen mainScreen] scale];
NSUInteger radius = (NSUInteger)(floor(inputRadius * 3.0f * ((CGFloat)sqrt(2 * M_PI)) / 4 + 0.5f));
if (radius % 2 != 1) {
radius += 1; // force radius to be odd so that the three box-blur methodology works.
}
vImageBoxConvolve_ARGB8888(&effectInBuffer, &effectOutBuffer, NULL, 0, 0, (uint32_t)radius, (uint32_t)radius, 0, kvImageEdgeExtend);
vImageBoxConvolve_ARGB8888(&effectOutBuffer, &effectInBuffer, NULL, 0, 0, (uint32_t)radius, (uint32_t)radius, 0, kvImageEdgeExtend);
vImageBoxConvolve_ARGB8888(&effectInBuffer, &effectOutBuffer, NULL, 0, 0, (uint32_t)radius, (uint32_t)radius, 0, kvImageEdgeExtend);
}
BOOL effectImageBuffersAreSwapped = NO;
if (hasSaturationChange) {
CGFloat s = saturationDeltaFactor;
CGFloat floatingPointSaturationMatrix[] = {
0.0722f + 0.9278f * s, 0.0722f - 0.0722f * s, 0.0722f - 0.0722f * s, 0,
0.7152f - 0.7152f * s, 0.7152f + 0.2848f * s, 0.7152f - 0.7152f * s, 0,
0.2126f - 0.2126f * s, 0.2126f - 0.2126f * s, 0.2126f + 0.7873f * s, 0,
0, 0, 0, 1,
};
const int32_t divisor = 256;
NSUInteger matrixSize = sizeof(floatingPointSaturationMatrix)/sizeof(floatingPointSaturationMatrix[0]);
int16_t saturationMatrix[matrixSize];
for (NSUInteger i = 0; i < matrixSize; ++i) {
saturationMatrix[i] = (int16_t)floor(floatingPointSaturationMatrix[i] * divisor);
}
if (hasBlur) {
vImageMatrixMultiply_ARGB8888(&effectOutBuffer, &effectInBuffer, saturationMatrix, divisor, NULL, NULL, kvImageNoFlags);
effectImageBuffersAreSwapped = YES;
}
else {
vImageMatrixMultiply_ARGB8888(&effectInBuffer, &effectOutBuffer, saturationMatrix, divisor, NULL, NULL, kvImageNoFlags);
}
}
if (!effectImageBuffersAreSwapped)
effectImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (effectImageBuffersAreSwapped)
effectImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
// Set up output context.
UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef outputContext = UIGraphicsGetCurrentContext();
CGContextScaleCTM(outputContext, 1.0, -1.0);
CGContextTranslateCTM(outputContext, 0, -image.size.height);
// Draw base image.
CGContextDrawImage(outputContext, imageRect, image.CGImage);
// Draw effect image.
if (hasBlur) {
CGContextSaveGState(outputContext);
if (maskImage) {
CGContextClipToMask(outputContext, imageRect, maskImage.CGImage);
}
CGContextDrawImage(outputContext, imageRect, effectImage.CGImage);
CGContextRestoreGState(outputContext);
}
// Add in color tint.
if (tintColor) {
CGContextSaveGState(outputContext);
CGContextSetFillColorWithColor(outputContext, tintColor.CGColor);
CGContextFillRect(outputContext, imageRect);
CGContextRestoreGState(outputContext);
}
// Output image is ready.
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
UIImage * _Nullable applyScreenshotEffectToImage(UIImage * _Nonnull image) {
return applyBlurWithRadius(image, 10.0f, nil, 1.8f, nil);
}
+298
View File
@@ -0,0 +1,298 @@
#import <FastBlur/FastBlur.h>
#import <Accelerate/Accelerate.h>
static inline uint64_t get_colors (const uint8_t *p) {
return p[0] + (p[1] << 16) + ((uint64_t)p[2] << 32);
}
void imageFastBlur(int imageWidth, int imageHeight, int imageStride, void * _Nonnull pixels)
{
uint8_t *pix = (uint8_t *)pixels;
const int w = imageWidth;
const int h = imageHeight;
const int stride = imageStride;
const int radius = 3;
const int r1 = radius + 1;
const int div = radius * 2 + 1;
if (radius > 15 || div >= w || div >= h)
{
return;
}
uint64_t *rgb = malloc(imageStride * imageHeight * sizeof(uint64_t));
int x, y, i;
int yw = 0;
const int we = w - r1;
for (y = 0; y < h; y++) {
uint64_t cur = get_colors (&pix[yw]);
uint64_t rgballsum = -radius * cur;
uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1);
for (i = 1; i <= radius; i++) {
uint64_t cur = get_colors (&pix[yw + i * 4]);
rgbsum += cur * (r1 - i);
rgballsum += cur;
}
x = 0;
#define update(start, middle, end) \
rgb[y * w + x] = (rgbsum >> 4) & 0x00FF00FF00FF00FF; \
\
rgballsum += get_colors (&pix[yw + (start) * 4]) - \
2 * get_colors (&pix[yw + (middle) * 4]) + \
get_colors (&pix[yw + (end) * 4]); \
rgbsum += rgballsum; \
x++; \
while (x < r1) {
update (0, x, x + r1);
}
while (x < we) {
update (x - r1, x, x + r1);
}
while (x < w) {
update (x - r1, x, w - 1);
}
#undef update
yw += stride;
}
const int he = h - r1;
for (x = 0; x < w; x++) {
uint64_t rgballsum = -radius * rgb[x];
uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1);
for (i = 1; i <= radius; i++) {
rgbsum += rgb[i * w + x] * (r1 - i);
rgballsum += rgb[i * w + x];
}
y = 0;
int yi = x * 4;
#define update(start, middle, end) \
int64_t res = rgbsum >> 4; \
pix[yi] = (uint8_t)res; \
pix[yi + 1] = (uint8_t)(res >> 16); \
pix[yi + 2] = (uint8_t)(res >> 32); \
\
rgballsum += rgb[x + (start) * w] - \
2 * rgb[x + (middle) * w] + \
rgb[x + (end) * w]; \
rgbsum += rgballsum; \
y++; \
yi += stride;
while (y < r1) {
update (0, y, y + r1);
}
while (y < he) {
update (y - r1, y, y + r1);
}
while (y < h) {
update (y - r1, y, h - 1);
}
#undef update
}
free(rgb);
}
void telegramFastBlurMore(int imageWidth, int imageHeight, int imageStride, void * _Nonnull pixels)
{
uint8_t *pix = (uint8_t *)pixels;
const int w = imageWidth;
const int h = imageHeight;
const int stride = imageStride;
const int radius = 7;
const int r1 = radius + 1;
const int div = radius * 2 + 1;
if (radius > 15 || div >= w || div >= h)
{
return;
}
uint64_t *rgb = malloc(imageStride * imageHeight * sizeof(uint64_t));
int x, y, i;
int yw = 0;
const int we = w - r1;
for (y = 0; y < h; y++) {
uint64_t cur = get_colors (&pix[yw]);
uint64_t rgballsum = -radius * cur;
uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1);
for (i = 1; i <= radius; i++) {
uint64_t cur = get_colors (&pix[yw + i * 4]);
rgbsum += cur * (r1 - i);
rgballsum += cur;
}
x = 0;
#define update(start, middle, end) \
rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \
\
rgballsum += get_colors (&pix[yw + (start) * 4]) - \
2 * get_colors (&pix[yw + (middle) * 4]) + \
get_colors (&pix[yw + (end) * 4]); \
rgbsum += rgballsum; \
x++; \
while (x < r1) {
update (0, x, x + r1);
}
while (x < we) {
update (x - r1, x, x + r1);
}
while (x < w) {
update (x - r1, x, w - 1);
}
#undef update
yw += stride;
}
const int he = h - r1;
for (x = 0; x < w; x++) {
uint64_t rgballsum = -radius * rgb[x];
uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1);
for (i = 1; i <= radius; i++) {
rgbsum += rgb[i * w + x] * (r1 - i);
rgballsum += rgb[i * w + x];
}
y = 0;
int yi = x * 4;
#define update(start, middle, end) \
int64_t res = rgbsum >> 6; \
pix[yi] = (uint8_t)res; \
pix[yi + 1] = (uint8_t)(res >> 16); \
pix[yi + 2] = (uint8_t)(res >> 32); \
\
rgballsum += rgb[x + (start) * w] - \
2 * rgb[x + (middle) * w] + \
rgb[x + (end) * w]; \
rgbsum += rgballsum; \
y++; \
yi += stride;
while (y < r1) {
update (0, y, y + r1);
}
while (y < he) {
update (y - r1, y, y + r1);
}
while (y < h) {
update (y - r1, y, h - 1);
}
#undef update
}
free(rgb);
}
void stickerThumbnailAlphaBlur(int imageWidth, int imageHeight, int imageStride, void * _Nonnull pixels) {
vImage_Buffer srcBuffer;
srcBuffer.width = imageWidth;
srcBuffer.height = imageHeight;
srcBuffer.rowBytes = imageStride;
srcBuffer.data = pixels;
void *tempBytes = malloc(imageHeight * imageStride);
{
vImage_Buffer dstBuffer;
dstBuffer.width = imageWidth;
dstBuffer.height = imageHeight;
dstBuffer.rowBytes = imageStride;
dstBuffer.data = tempBytes;
int boxSize = 2;
boxSize = boxSize - (boxSize % 2) + 1;
vImageBoxConvolve_ARGB8888(&srcBuffer, &dstBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
}
memcpy(pixels, tempBytes, imageHeight * imageStride);
free(tempBytes);
}
static void modifyImage(void *pixels, unsigned int width, unsigned int height, unsigned int stride, int16_t * _Nonnull matrix)
{
vImage_Buffer dstBuffer;
dstBuffer.width = width;
dstBuffer.height = height;
dstBuffer.rowBytes = stride;
dstBuffer.data = pixels;
int32_t divisor = 256;
vImageMatrixMultiply_ARGB8888(&dstBuffer, &dstBuffer, matrix, divisor, NULL, NULL, kvImageDoNotTile);
}
static void matrixMul(CGFloat * _Nonnull a, CGFloat * _Nonnull b, CGFloat *result)
{
for (int i = 0; i != 4; ++i)
{
for (int j = 0; j != 4; ++j)
{
CGFloat sum = 0;
for (int k = 0; k != 4; ++k)
{
sum += a[i + k * 4] * b[k + j * 4];
}
result[i + j * 4] = sum;
}
}
}
static int16_t *lightBrightenMatrix(int32_t * _Nullable outDivisor)
{
static int16_t saturationMatrix[16];
static const int32_t divisor = 256;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
CGFloat s = 1.2f;
CGFloat offset = 0.01f;
CGFloat factor = 1.02f;
CGFloat satMatrix[] = {
0.0722f + 0.9278f * s, 0.0722f - 0.0722f * s, 0.0722f - 0.0722f * s, 0,
0.7152f - 0.7152f * s, 0.7152f + 0.2848f * s, 0.7152f - 0.7152f * s, 0,
0.2126f - 0.2126f * s, 0.2126f - 0.2126f * s, 0.2126f + 0.7873f * s, 0,
0.0f, 0.0f, 0.0f, 1,
};
CGFloat contrastMatrix[] = {
factor, 0.0f, 0.0f, 0.0f,
0.0f, factor, 0.0f, 0.0f,
0.0f, 0.0f, factor, 0.0f,
offset, offset, offset, 1.0f
};
CGFloat colorMatrix[16];
matrixMul(satMatrix, contrastMatrix, colorMatrix);
NSUInteger matrixSize = sizeof(colorMatrix) / sizeof(colorMatrix[0]);
for (NSUInteger i = 0; i < matrixSize; ++i) {
saturationMatrix[i] = (int16_t)round(colorMatrix[i] * divisor);
}
});
if (outDivisor != NULL)
*outDivisor = divisor;
return saturationMatrix;
}
void telegramBrightenImage(int imageWidth, int imageHeight, int imageStride, void * _Nonnull pixels)
{
modifyImage(pixels, imageWidth, imageHeight, imageStride, lightBrightenMatrix(NULL));
}