2014-05-17 01:05:49 +02:00
|
|
|
#include <jni.h>
|
2021-06-25 02:43:10 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <csetjmp>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cmath>
|
2020-09-30 15:48:47 +02:00
|
|
|
#include <unistd.h>
|
2014-07-27 00:28:33 +02:00
|
|
|
#include <android/bitmap.h>
|
2020-09-30 15:48:47 +02:00
|
|
|
#include <string>
|
|
|
|
#include <mozjpeg/java/org_libjpegturbo_turbojpeg_TJ.h>
|
|
|
|
#include <mozjpeg/jpeglib.h>
|
2021-06-25 02:43:10 +02:00
|
|
|
#include <tgnet/FileLog.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2020-10-04 12:14:00 +02:00
|
|
|
#include "webp/decode.h"
|
|
|
|
#include "webp/encode.h"
|
2020-09-30 15:48:47 +02:00
|
|
|
#include "mozjpeg/turbojpeg.h"
|
2019-03-03 21:40:48 +01:00
|
|
|
#include "c_utils.h"
|
2015-01-02 23:15:07 +01:00
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
extern "C" {
|
2015-01-02 23:15:07 +01:00
|
|
|
jclass jclass_NullPointerException;
|
|
|
|
jclass jclass_RuntimeException;
|
|
|
|
|
|
|
|
jclass jclass_Options;
|
|
|
|
jfieldID jclass_Options_inJustDecodeBounds;
|
|
|
|
jfieldID jclass_Options_outHeight;
|
|
|
|
jfieldID jclass_Options_outWidth;
|
|
|
|
|
2019-03-03 21:40:48 +01:00
|
|
|
jint imageOnJNILoad(JavaVM *vm, JNIEnv *env) {
|
2020-06-04 18:47:15 +02:00
|
|
|
jclass_NullPointerException = (jclass) env->NewGlobalRef(env->FindClass("java/lang/NullPointerException"));
|
2015-01-02 23:15:07 +01:00
|
|
|
if (jclass_NullPointerException == 0) {
|
2019-03-03 21:40:48 +01:00
|
|
|
return JNI_FALSE;
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
jclass_RuntimeException = (jclass) env->NewGlobalRef(env->FindClass("java/lang/RuntimeException"));
|
2015-01-02 23:15:07 +01:00
|
|
|
if (jclass_RuntimeException == 0) {
|
2019-03-03 21:40:48 +01:00
|
|
|
return JNI_FALSE;
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
jclass_Options = (jclass) env->NewGlobalRef(env->FindClass("android/graphics/BitmapFactory$Options"));
|
2015-01-02 23:15:07 +01:00
|
|
|
if (jclass_Options == 0) {
|
2019-03-03 21:40:48 +01:00
|
|
|
return JNI_FALSE;
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
jclass_Options_inJustDecodeBounds = env->GetFieldID(jclass_Options, "inJustDecodeBounds", "Z");
|
2015-01-02 23:15:07 +01:00
|
|
|
if (jclass_Options_inJustDecodeBounds == 0) {
|
2019-03-03 21:40:48 +01:00
|
|
|
return JNI_FALSE;
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
jclass_Options_outHeight = env->GetFieldID(jclass_Options, "outHeight", "I");
|
2015-01-02 23:15:07 +01:00
|
|
|
if (jclass_Options_outHeight == 0) {
|
2019-03-03 21:40:48 +01:00
|
|
|
return JNI_FALSE;
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
jclass_Options_outWidth = env->GetFieldID(jclass_Options, "outWidth", "I");
|
2015-01-02 23:15:07 +01:00
|
|
|
if (jclass_Options_outWidth == 0) {
|
2019-03-03 21:40:48 +01:00
|
|
|
return JNI_FALSE;
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2019-03-03 21:40:48 +01:00
|
|
|
return JNI_TRUE;
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2014-05-17 01:05:49 +02:00
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
static inline uint64_t getColors(const uint8_t *p) {
|
2019-07-18 15:01:39 +02:00
|
|
|
return p[0] + (p[1] << 16) + ((uint64_t) p[2] << 32) + ((uint64_t) p[3] << 48);
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
static inline uint64_t getColors565(const uint8_t *p) {
|
|
|
|
uint16_t *ps = (uint16_t *) p;
|
2020-06-04 18:47:15 +02:00
|
|
|
return ((((ps[0] & 0xF800) >> 11) * 255) / 31) + (((((ps[0] & 0x07E0) >> 5) * 255) / 63) << 16) + ((uint64_t)(((ps[0] & 0x001F) * 255) / 31) << 32);
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fastBlurMore(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) {
|
|
|
|
const int32_t r1 = radius + 1;
|
|
|
|
const int32_t div = radius * 2 + 1;
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 4) {
|
2014-07-27 00:28:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-07-30 04:07:02 +02:00
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
uint64_t *rgb = new uint64_t[w * h];
|
2014-10-09 17:55:05 +02:00
|
|
|
if (rgb == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-30 04:07:02 +02:00
|
|
|
|
|
|
|
int32_t x, y, i;
|
|
|
|
|
|
|
|
int32_t yw = 0;
|
|
|
|
const int32_t we = w - r1;
|
2014-10-09 17:55:05 +02:00
|
|
|
for (y = 0; y < h; y++) {
|
2018-07-30 04:07:02 +02:00
|
|
|
uint64_t cur = getColors(&pix[yw]);
|
2014-10-09 17:55:05 +02:00
|
|
|
uint64_t rgballsum = -radius * cur;
|
|
|
|
uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1);
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2014-10-09 17:55:05 +02:00
|
|
|
for (i = 1; i <= radius; i++) {
|
2018-07-30 04:07:02 +02:00
|
|
|
cur = getColors(&pix[yw + i * 4]);
|
2014-10-09 17:55:05 +02:00
|
|
|
rgbsum += cur * (r1 - i);
|
|
|
|
rgballsum += cur;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2014-10-09 17:55:05 +02:00
|
|
|
x = 0;
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
#define update(start, middle, end) \
|
2014-10-09 17:55:05 +02:00
|
|
|
rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \
|
2018-07-30 04:07:02 +02:00
|
|
|
rgballsum += getColors(&pix[yw + (start) * 4]) - 2 * getColors(&pix[yw + (middle) * 4]) + getColors(&pix[yw + (end) * 4]); \
|
2014-10-09 17:55:05 +02:00
|
|
|
rgbsum += rgballsum; \
|
|
|
|
x++; \
|
|
|
|
|
|
|
|
while (x < r1) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (0, x, x + r1)
|
2014-10-09 17:55:05 +02:00
|
|
|
}
|
|
|
|
while (x < we) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (x - r1, x, x + r1)
|
2014-10-09 17:55:05 +02:00
|
|
|
}
|
|
|
|
while (x < w) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (x - r1, x, w - 1)
|
2014-10-09 17:55:05 +02:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
#undef update
|
|
|
|
|
2014-10-09 17:55:05 +02:00
|
|
|
yw += stride;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
const int32_t he = h - r1;
|
2014-10-09 17:55:05 +02:00
|
|
|
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];
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2014-10-09 17:55:05 +02:00
|
|
|
y = 0;
|
2018-07-30 04:07:02 +02:00
|
|
|
int32_t yi = x * 4;
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
#define update(start, middle, end) \
|
2014-10-09 17:55:05 +02:00
|
|
|
int64_t res = rgbsum >> 6; \
|
2018-07-30 04:07:02 +02:00
|
|
|
pix[yi] = res; \
|
|
|
|
pix[yi + 1] = res >> 16; \
|
|
|
|
pix[yi + 2] = res >> 32; \
|
2019-07-18 15:01:39 +02:00
|
|
|
pix[yi + 3] = res >> 48; \
|
2014-10-09 17:55:05 +02:00
|
|
|
rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \
|
|
|
|
rgbsum += rgballsum; \
|
|
|
|
y++; \
|
|
|
|
yi += stride;
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2014-10-09 17:55:05 +02:00
|
|
|
while (y < r1) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (0, y, y + r1)
|
2014-10-09 17:55:05 +02:00
|
|
|
}
|
|
|
|
while (y < he) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (y - r1, y, y + r1)
|
2014-10-09 17:55:05 +02:00
|
|
|
}
|
|
|
|
while (y < h) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (y - r1, y, h - 1)
|
2014-10-09 17:55:05 +02:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
#undef update
|
2014-10-09 17:55:05 +02:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
delete[] rgb;
|
2014-10-09 17:55:05 +02:00
|
|
|
}
|
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
static void fastBlur(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) {
|
2021-06-25 02:43:10 +02:00
|
|
|
if (pix == nullptr) {
|
2016-01-11 18:19:48 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-07-30 04:07:02 +02:00
|
|
|
const int32_t r1 = radius + 1;
|
|
|
|
const int32_t div = radius * 2 + 1;
|
|
|
|
int32_t shift;
|
2015-01-02 23:15:07 +01:00
|
|
|
if (radius == 1) {
|
|
|
|
shift = 2;
|
|
|
|
} else if (radius == 3) {
|
|
|
|
shift = 4;
|
|
|
|
} else if (radius == 7) {
|
|
|
|
shift = 6;
|
|
|
|
} else if (radius == 15) {
|
|
|
|
shift = 8;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 4) {
|
2014-10-09 17:55:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-07-30 04:07:02 +02:00
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
uint64_t *rgb = new uint64_t[w * h];
|
2021-06-25 02:43:10 +02:00
|
|
|
if (rgb == nullptr) {
|
2014-10-09 17:55:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-07-30 04:07:02 +02:00
|
|
|
|
|
|
|
int32_t x, y, i;
|
|
|
|
|
|
|
|
int32_t yw = 0;
|
|
|
|
const int32_t we = w - r1;
|
2014-07-27 00:28:33 +02:00
|
|
|
for (y = 0; y < h; y++) {
|
2018-07-30 04:07:02 +02:00
|
|
|
uint64_t cur = getColors(&pix[yw]);
|
2014-07-27 00:28:33 +02:00
|
|
|
uint64_t rgballsum = -radius * cur;
|
|
|
|
uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1);
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2014-07-27 00:28:33 +02:00
|
|
|
for (i = 1; i <= radius; i++) {
|
2018-07-30 04:07:02 +02:00
|
|
|
cur = getColors(&pix[yw + i * 4]);
|
2014-07-27 00:28:33 +02:00
|
|
|
rgbsum += cur * (r1 - i);
|
|
|
|
rgballsum += cur;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2014-07-27 00:28:33 +02:00
|
|
|
x = 0;
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
#define update(start, middle, end) \
|
2015-01-02 23:15:07 +01:00
|
|
|
rgb[y * w + x] = (rgbsum >> shift) & 0x00FF00FF00FF00FFLL; \
|
2018-07-30 04:07:02 +02:00
|
|
|
rgballsum += getColors(&pix[yw + (start) * 4]) - 2 * getColors(&pix[yw + (middle) * 4]) + getColors(&pix[yw + (end) * 4]); \
|
2014-07-27 00:28:33 +02:00
|
|
|
rgbsum += rgballsum; \
|
|
|
|
x++; \
|
|
|
|
|
|
|
|
while (x < r1) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (0, x, x + r1)
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
|
|
|
while (x < we) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (x - r1, x, x + r1)
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
|
|
|
while (x < w) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (x - r1, x, w - 1)
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
#undef update
|
|
|
|
|
2014-07-27 00:28:33 +02:00
|
|
|
yw += stride;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
const int32_t he = h - r1;
|
2014-07-27 00:28:33 +02:00
|
|
|
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];
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2014-07-27 00:28:33 +02:00
|
|
|
y = 0;
|
2018-07-30 04:07:02 +02:00
|
|
|
int32_t yi = x * 4;
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
#define update(start, middle, end) \
|
2015-01-02 23:15:07 +01:00
|
|
|
int64_t res = rgbsum >> shift; \
|
2014-07-27 00:28:33 +02:00
|
|
|
pix[yi] = res; \
|
|
|
|
pix[yi + 1] = res >> 16; \
|
|
|
|
pix[yi + 2] = res >> 32; \
|
2019-07-18 15:01:39 +02:00
|
|
|
pix[yi + 3] = res >> 48; \
|
2014-07-27 00:28:33 +02:00
|
|
|
rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \
|
|
|
|
rgbsum += rgballsum; \
|
|
|
|
y++; \
|
|
|
|
yi += stride;
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2014-07-27 00:28:33 +02:00
|
|
|
while (y < r1) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (0, y, y + r1)
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
|
|
|
while (y < he) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (y - r1, y, y + r1)
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
|
|
|
while (y < h) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (y - r1, y, h - 1)
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
#undef update
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
delete[] rgb;
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
static void fastBlurMore565(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) {
|
|
|
|
const int32_t r1 = radius + 1;
|
|
|
|
const int32_t div = radius * 2 + 1;
|
|
|
|
|
|
|
|
if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
uint64_t *rgb = new uint64_t[w * h];
|
2018-07-30 04:07:02 +02:00
|
|
|
if (rgb == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t x, y, i;
|
|
|
|
|
|
|
|
int32_t yw = 0;
|
|
|
|
const int32_t we = w - r1;
|
|
|
|
for (y = 0; y < h; y++) {
|
|
|
|
uint64_t cur = getColors565(&pix[yw]);
|
|
|
|
uint64_t rgballsum = -radius * cur;
|
|
|
|
uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1);
|
|
|
|
|
|
|
|
for (i = 1; i <= radius; i++) {
|
|
|
|
cur = getColors565(&pix[yw + i * 2]);
|
|
|
|
rgbsum += cur * (r1 - i);
|
|
|
|
rgballsum += cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
x = 0;
|
|
|
|
|
|
|
|
#define update(start, middle, end) \
|
|
|
|
rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \
|
|
|
|
rgballsum += getColors565(&pix[yw + (start) * 2]) - 2 * getColors565(&pix[yw + (middle) * 2]) + getColors565(&pix[yw + (end) * 2]); \
|
|
|
|
rgbsum += rgballsum; \
|
|
|
|
x++; \
|
|
|
|
|
|
|
|
while (x < r1) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (0, x, x + r1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
while (x < we) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (x - r1, x, x + r1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
while (x < w) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (x - r1, x, w - 1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
#undef update
|
|
|
|
|
|
|
|
yw += stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int32_t 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;
|
|
|
|
int32_t yi = x * 2;
|
|
|
|
|
|
|
|
#define update(start, middle, end) \
|
|
|
|
int64_t res = rgbsum >> 6; \
|
|
|
|
pix[yi] = ((res >> 13) & 0xe0) | ((res >> 35) & 0x1f); \
|
|
|
|
pix[yi + 1] = (res & 0xf8) | ((res >> 21) & 0x7); \
|
|
|
|
rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \
|
|
|
|
rgbsum += rgballsum; \
|
|
|
|
y++; \
|
|
|
|
yi += stride;
|
|
|
|
|
|
|
|
while (y < r1) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (0, y, y + r1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
while (y < he) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (y - r1, y, y + r1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
while (y < h) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (y - r1, y, h - 1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
#undef update
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
delete[] rgb;
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fastBlur565(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) {
|
|
|
|
if (pix == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const int32_t r1 = radius + 1;
|
|
|
|
const int32_t div = radius * 2 + 1;
|
|
|
|
int32_t shift;
|
|
|
|
if (radius == 1) {
|
|
|
|
shift = 2;
|
|
|
|
} else if (radius == 3) {
|
|
|
|
shift = 4;
|
|
|
|
} else if (radius == 7) {
|
|
|
|
shift = 6;
|
|
|
|
} else if (radius == 15) {
|
|
|
|
shift = 8;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
uint64_t *rgb = new uint64_t[w * h];
|
2018-07-30 04:07:02 +02:00
|
|
|
if (rgb == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t x, y, i;
|
|
|
|
|
|
|
|
int32_t yw = 0;
|
|
|
|
const int32_t we = w - r1;
|
|
|
|
for (y = 0; y < h; y++) {
|
|
|
|
uint64_t cur = getColors565(&pix[yw]);
|
|
|
|
uint64_t rgballsum = -radius * cur;
|
|
|
|
uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1);
|
|
|
|
|
|
|
|
for (i = 1; i <= radius; i++) {
|
|
|
|
cur = getColors565(&pix[yw + i * 2]);
|
|
|
|
rgbsum += cur * (r1 - i);
|
|
|
|
rgballsum += cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
x = 0;
|
|
|
|
|
|
|
|
#define update(start, middle, end) \
|
|
|
|
rgb[y * w + x] = (rgbsum >> shift) & 0x00FF00FF00FF00FFLL; \
|
|
|
|
rgballsum += getColors565(&pix[yw + (start) * 2]) - 2 * getColors565(&pix[yw + (middle) * 2]) + getColors565(&pix[yw + (end) * 2]); \
|
|
|
|
rgbsum += rgballsum; \
|
|
|
|
x++;
|
|
|
|
|
|
|
|
while (x < r1) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update(0, x, x + r1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
while (x < we) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update(x - r1, x, x + r1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
while (x < w) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update(x - r1, x, w - 1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef update
|
|
|
|
|
|
|
|
yw += stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int32_t 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;
|
|
|
|
int32_t yi = x * 2;
|
|
|
|
|
|
|
|
#define update(start, middle, end) \
|
|
|
|
uint64_t res = rgbsum >> shift; \
|
|
|
|
pix[yi] = ((res >> 13) & 0xe0) | ((res >> 35) & 0x1f); \
|
|
|
|
pix[yi + 1] = (res & 0xf8) | ((res >> 21) & 0x7); \
|
|
|
|
rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \
|
|
|
|
rgbsum += rgballsum; \
|
|
|
|
y++; \
|
|
|
|
yi += stride;
|
|
|
|
|
|
|
|
while (y < r1) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (0, y, y + r1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
while (y < he) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (y - r1, y, y + r1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
while (y < h) {
|
2020-09-30 15:48:47 +02:00
|
|
|
update (y - r1, y, h - 1)
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
#undef update
|
|
|
|
}
|
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
delete[] rgb;
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 15:48:47 +02:00
|
|
|
JNIEXPORT int Java_org_telegram_messenger_Utilities_needInvert(JNIEnv *env, jclass clazz, jobject bitmap, jint unpin, jint width, jint height, jint stride) {
|
2019-01-23 18:03:33 +01:00
|
|
|
if (!bitmap) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!width || !height || !stride || stride != width * 4 || width * height > 150 * 150) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
void *pixels = nullptr;
|
2019-01-23 18:03:33 +01:00
|
|
|
if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
if (pixels == nullptr) {
|
2019-01-23 18:03:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint8_t *pix = (uint8_t *) pixels;
|
|
|
|
|
|
|
|
int32_t hasAlpha = 0;
|
|
|
|
float matching = 0;
|
|
|
|
float total = 0;
|
|
|
|
for (int32_t y = 0; y < height; y++) {
|
|
|
|
for (int32_t x = 0; x < width; x++) {
|
|
|
|
int32_t index = y * stride + x * 4;
|
|
|
|
uint8_t a = pix[index + 3];
|
|
|
|
float alpha = a / 255.0f;
|
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
uint8_t r = (uint8_t)(pix[index] * alpha);
|
|
|
|
uint8_t g = (uint8_t)(pix[index + 1] * alpha);
|
|
|
|
uint8_t b = (uint8_t)(pix[index + 2] * alpha);
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
uint8_t cmax = (r > g) ? r : g;
|
|
|
|
if (b > cmax) {
|
|
|
|
cmax = b;
|
|
|
|
}
|
|
|
|
uint8_t cmin = (r < g) ? r : g;
|
|
|
|
if (b < cmin) {
|
|
|
|
cmin = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
float saturation;
|
|
|
|
float brightness = ((float) cmax) / 255.0f;
|
|
|
|
if (cmax != 0) {
|
|
|
|
saturation = ((float) (cmax - cmin)) / ((float) cmax);
|
|
|
|
} else {
|
|
|
|
saturation = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alpha < 1.0) {
|
|
|
|
hasAlpha = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alpha > 0.0) {
|
|
|
|
total += 1;
|
|
|
|
if (saturation < 0.1f && brightness < 0.25f) {
|
|
|
|
matching += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (unpin) {
|
|
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
|
|
}
|
|
|
|
return hasAlpha && matching / total > 0.85;
|
|
|
|
}
|
|
|
|
|
2020-09-30 15:48:47 +02:00
|
|
|
JNIEXPORT void Java_org_telegram_messenger_Utilities_blurBitmap(JNIEnv *env, jclass clazz, jobject bitmap, jint radius, jint unpin, jint width, jint height, jint stride) {
|
2014-09-14 01:40:36 +02:00
|
|
|
if (!bitmap) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2016-01-11 18:19:48 +01:00
|
|
|
if (!width || !height || !stride) {
|
2014-08-30 21:30:42 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
void *pixels = nullptr;
|
2014-07-27 00:28:33 +02:00
|
|
|
if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-30 04:07:02 +02:00
|
|
|
if (stride == width * 2) {
|
|
|
|
if (radius <= 3) {
|
2020-06-04 18:47:15 +02:00
|
|
|
fastBlur565(width, height, stride, (uint8_t *) pixels, radius);
|
2018-07-30 04:07:02 +02:00
|
|
|
} else {
|
2020-06-04 18:47:15 +02:00
|
|
|
fastBlurMore565(width, height, stride, (uint8_t *) pixels, radius);
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
2014-10-09 17:55:05 +02:00
|
|
|
} else {
|
2018-07-30 04:07:02 +02:00
|
|
|
if (radius <= 3) {
|
2020-06-04 18:47:15 +02:00
|
|
|
fastBlur(width, height, stride, (uint8_t *) pixels, radius);
|
2018-07-30 04:07:02 +02:00
|
|
|
} else {
|
2020-06-04 18:47:15 +02:00
|
|
|
fastBlurMore(width, height, stride, (uint8_t *) pixels, radius);
|
2018-07-30 04:07:02 +02:00
|
|
|
}
|
2014-10-09 17:55:05 +02:00
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
if (unpin) {
|
|
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
const uint32_t PGPhotoEnhanceHistogramBins = 256;
|
|
|
|
const uint32_t PGPhotoEnhanceSegments = 4;
|
|
|
|
|
2020-09-30 15:48:47 +02:00
|
|
|
JNIEXPORT void Java_org_telegram_messenger_Utilities_calcCDT(JNIEnv *env, jclass clazz, jobject hsvBuffer, jint width, jint height, jobject buffer, jobject calcBuffer) {
|
2015-02-26 02:32:51 +01:00
|
|
|
float imageWidth = width;
|
|
|
|
float imageHeight = height;
|
|
|
|
float _clipLimit = 1.25f;
|
|
|
|
|
|
|
|
uint32_t totalSegments = PGPhotoEnhanceSegments * PGPhotoEnhanceSegments;
|
2018-07-30 04:07:02 +02:00
|
|
|
uint32_t tileArea = (uint32_t) (floorf(imageWidth / PGPhotoEnhanceSegments) * floorf(imageHeight / PGPhotoEnhanceSegments));
|
2019-03-03 21:40:48 +01:00
|
|
|
uint32_t clipLimit = (uint32_t) MAX(1, _clipLimit * tileArea / (float) PGPhotoEnhanceHistogramBins);
|
2015-02-26 02:32:51 +01:00
|
|
|
float scale = 255.0f / (float) tileArea;
|
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
unsigned char *bytes = (unsigned char *) env->GetDirectBufferAddress(hsvBuffer);
|
|
|
|
uint32_t *calcBytes = (uint32_t *) env->GetDirectBufferAddress(calcBuffer);
|
|
|
|
unsigned char *result = (unsigned char *) env->GetDirectBufferAddress(buffer);
|
|
|
|
|
|
|
|
uint32_t *cdfsMin = calcBytes;
|
|
|
|
calcBytes += totalSegments;
|
|
|
|
uint32_t *cdfsMax = calcBytes;
|
|
|
|
calcBytes += totalSegments;
|
|
|
|
uint32_t *cdfs = calcBytes;
|
|
|
|
calcBytes += totalSegments * PGPhotoEnhanceHistogramBins;
|
|
|
|
uint32_t *hist = calcBytes;
|
|
|
|
memset(hist, 0, sizeof(uint32_t) * totalSegments * PGPhotoEnhanceHistogramBins);
|
2015-02-26 02:32:51 +01:00
|
|
|
|
|
|
|
float xMul = PGPhotoEnhanceSegments / imageWidth;
|
|
|
|
float yMul = PGPhotoEnhanceSegments / imageHeight;
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
uint32_t i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < imageHeight; i++) {
|
|
|
|
uint32_t yOffset = i * width * 4;
|
|
|
|
for (j = 0; j < imageWidth; j++) {
|
|
|
|
uint32_t index = j * 4 + yOffset;
|
|
|
|
|
|
|
|
uint32_t tx = (uint32_t)(j * xMul);
|
|
|
|
uint32_t ty = (uint32_t)(i * yMul);
|
2015-02-26 02:32:51 +01:00
|
|
|
uint32_t t = ty * PGPhotoEnhanceSegments + tx;
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
hist[t * PGPhotoEnhanceHistogramBins + bytes[index + 2]]++;
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
for (i = 0; i < totalSegments; i++) {
|
2015-02-26 02:32:51 +01:00
|
|
|
if (clipLimit > 0) {
|
|
|
|
uint32_t clipped = 0;
|
2020-06-04 18:47:15 +02:00
|
|
|
for (j = 0; j < PGPhotoEnhanceHistogramBins; j++) {
|
|
|
|
if (hist[i * PGPhotoEnhanceHistogramBins + j] > clipLimit) {
|
|
|
|
clipped += hist[i * PGPhotoEnhanceHistogramBins + j] - clipLimit;
|
|
|
|
hist[i * PGPhotoEnhanceHistogramBins + j] = clipLimit;
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2015-02-26 02:32:51 +01:00
|
|
|
uint32_t redistBatch = clipped / PGPhotoEnhanceHistogramBins;
|
|
|
|
uint32_t residual = clipped - redistBatch * PGPhotoEnhanceHistogramBins;
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
for (j = 0; j < PGPhotoEnhanceHistogramBins; j++) {
|
|
|
|
hist[i * PGPhotoEnhanceHistogramBins + j] += redistBatch;
|
|
|
|
if (j < residual) {
|
|
|
|
hist[i * PGPhotoEnhanceHistogramBins + j]++;
|
|
|
|
}
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
memcpy(cdfs + i * PGPhotoEnhanceHistogramBins, hist + i * PGPhotoEnhanceHistogramBins, PGPhotoEnhanceHistogramBins * sizeof(uint32_t));
|
|
|
|
|
2015-02-26 02:32:51 +01:00
|
|
|
uint32_t hMin = PGPhotoEnhanceHistogramBins - 1;
|
2020-06-04 18:47:15 +02:00
|
|
|
for (j = 0; j < hMin; ++j) {
|
|
|
|
if (cdfs[i * PGPhotoEnhanceHistogramBins + j] != 0) {
|
2015-02-26 02:32:51 +01:00
|
|
|
hMin = j;
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2015-02-26 02:32:51 +01:00
|
|
|
uint32_t cdf = 0;
|
2020-06-04 18:47:15 +02:00
|
|
|
for (j = hMin; j < PGPhotoEnhanceHistogramBins; j++) {
|
|
|
|
cdf += cdfs[i * PGPhotoEnhanceHistogramBins + j];
|
|
|
|
cdfs[i * PGPhotoEnhanceHistogramBins + j] = (uint8_t) MIN(255, cdf * scale);
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
cdfsMin[i] = cdfs[i * PGPhotoEnhanceHistogramBins + hMin];
|
|
|
|
cdfsMax[i] = cdfs[i * PGPhotoEnhanceHistogramBins + PGPhotoEnhanceHistogramBins - 1];
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
for (j = 0; j < totalSegments; j++) {
|
|
|
|
uint32_t yOffset = j * PGPhotoEnhanceHistogramBins * 4;
|
|
|
|
for (i = 0; i < PGPhotoEnhanceHistogramBins; i++) {
|
2015-02-26 02:32:51 +01:00
|
|
|
uint32_t index = i * 4 + yOffset;
|
2020-06-04 18:47:15 +02:00
|
|
|
result[index] = (uint8_t) cdfs[j * PGPhotoEnhanceHistogramBins + i];
|
|
|
|
result[index + 1] = (uint8_t) cdfsMin[j];
|
|
|
|
result[index + 2] = (uint8_t) cdfsMax[j];
|
2015-02-26 02:32:51 +01:00
|
|
|
result[index + 3] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 15:48:47 +02:00
|
|
|
JNIEXPORT jint Java_org_telegram_messenger_Utilities_pinBitmap(JNIEnv *env, jclass clazz, jobject bitmap) {
|
2020-06-04 18:47:15 +02:00
|
|
|
if (bitmap == nullptr) {
|
2016-04-22 15:49:00 +02:00
|
|
|
return 0;
|
2016-01-11 18:19:48 +01:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
void *pixels;
|
2015-04-09 20:00:14 +02:00
|
|
|
return AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0 ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2020-09-30 15:48:47 +02:00
|
|
|
JNIEXPORT void Java_org_telegram_messenger_Utilities_unpinBitmap(JNIEnv *env, jclass clazz, jobject bitmap) {
|
2021-06-25 02:43:10 +02:00
|
|
|
if (bitmap == nullptr) {
|
2016-01-11 18:19:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
|
|
}
|
|
|
|
|
2020-09-30 15:48:47 +02:00
|
|
|
JNIEXPORT jboolean Java_org_telegram_messenger_Utilities_loadWebpImage(JNIEnv *env, jclass clazz, jobject outputBitmap, jobject buffer, jint len, jobject options, jboolean unpin) {
|
2015-01-02 23:15:07 +01:00
|
|
|
if (!buffer) {
|
2020-06-04 18:47:15 +02:00
|
|
|
env->ThrowNew(jclass_NullPointerException, "Input buffer can not be null");
|
2015-01-02 23:15:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
jbyte *inputBuffer = (jbyte *) env->GetDirectBufferAddress(buffer);
|
|
|
|
|
2018-07-30 04:07:02 +02:00
|
|
|
int32_t bitmapWidth = 0;
|
|
|
|
int32_t bitmapHeight = 0;
|
2020-06-04 18:47:15 +02:00
|
|
|
if (!WebPGetInfo((uint8_t *)inputBuffer, len, &bitmapWidth, &bitmapHeight)) {
|
|
|
|
env->ThrowNew(jclass_RuntimeException, "Invalid WebP format");
|
2015-01-02 23:15:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
if (options && env->GetBooleanField(options, jclass_Options_inJustDecodeBounds) == JNI_TRUE) {
|
|
|
|
env->SetIntField(options, jclass_Options_outWidth, bitmapWidth);
|
|
|
|
env->SetIntField(options, jclass_Options_outHeight, bitmapHeight);
|
2015-10-29 18:10:07 +01:00
|
|
|
return 1;
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
if (!outputBitmap) {
|
2020-06-04 18:47:15 +02:00
|
|
|
env->ThrowNew(jclass_NullPointerException, "output bitmap can not be null");
|
2015-01-02 23:15:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
AndroidBitmapInfo bitmapInfo;
|
|
|
|
if (AndroidBitmap_getInfo(env, outputBitmap, &bitmapInfo) != ANDROID_BITMAP_RESUT_SUCCESS) {
|
2020-06-04 18:47:15 +02:00
|
|
|
env->ThrowNew(jclass_RuntimeException, "Failed to get Bitmap information");
|
2015-01-02 23:15:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
void *bitmapPixels = nullptr;
|
2015-01-02 23:15:07 +01:00
|
|
|
if (AndroidBitmap_lockPixels(env, outputBitmap, &bitmapPixels) != ANDROID_BITMAP_RESUT_SUCCESS) {
|
2020-06-04 18:47:15 +02:00
|
|
|
env->ThrowNew(jclass_RuntimeException, "Failed to lock Bitmap pixels");
|
2015-01-02 23:15:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
|
|
|
if (!WebPDecodeRGBAInto((uint8_t *) inputBuffer, len, (uint8_t *) bitmapPixels, bitmapInfo.height * bitmapInfo.stride, bitmapInfo.stride)) {
|
2015-01-02 23:15:07 +01:00
|
|
|
AndroidBitmap_unlockPixels(env, outputBitmap);
|
2020-06-04 18:47:15 +02:00
|
|
|
env->ThrowNew(jclass_RuntimeException, "Failed to decode webp image");
|
2015-01-02 23:15:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2015-10-29 18:10:07 +01:00
|
|
|
if (unpin && AndroidBitmap_unlockPixels(env, outputBitmap) != ANDROID_BITMAP_RESUT_SUCCESS) {
|
2020-06-04 18:47:15 +02:00
|
|
|
env->ThrowNew(jclass_RuntimeException, "Failed to unlock Bitmap pixels");
|
2015-01-02 23:15:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
|
2015-10-29 18:10:07 +01:00
|
|
|
return 1;
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
#define SQUARE(i) ((i)*(i))
|
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
inline static void zeroClearInt(int *p, size_t count) {
|
|
|
|
memset(p, 0, sizeof(int) * count);
|
|
|
|
}
|
|
|
|
|
2020-09-30 15:48:47 +02:00
|
|
|
JNIEXPORT void Java_org_telegram_messenger_Utilities_stackBlurBitmap(JNIEnv *env, jclass clazz, jobject bitmap, jint radius) {
|
2020-06-04 18:47:15 +02:00
|
|
|
if (radius < 1) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
AndroidBitmapInfo info;
|
2020-06-04 18:47:15 +02:00
|
|
|
if (AndroidBitmap_getInfo(env, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS) {
|
2019-01-23 18:03:33 +01:00
|
|
|
return;
|
2020-06-04 18:47:15 +02:00
|
|
|
}
|
|
|
|
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
|
2019-01-23 18:03:33 +01:00
|
|
|
return;
|
2020-06-04 18:47:15 +02:00
|
|
|
}
|
2019-01-23 18:03:33 +01:00
|
|
|
|
2019-07-18 15:01:39 +02:00
|
|
|
int w = info.width;
|
|
|
|
int h = info.height;
|
|
|
|
int stride = info.stride;
|
2019-01-23 18:03:33 +01:00
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
unsigned char *pixels = nullptr;
|
2019-01-23 18:03:33 +01:00
|
|
|
AndroidBitmap_lockPixels(env, bitmap, (void **) &pixels);
|
2019-07-18 15:01:39 +02:00
|
|
|
if (!pixels) {
|
2019-01-23 18:03:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Constants
|
|
|
|
//const int radius = (int)inradius; // Transform unsigned into signed for further operations
|
|
|
|
const int wm = w - 1;
|
|
|
|
const int hm = h - 1;
|
2019-07-18 15:01:39 +02:00
|
|
|
const int wh = w * h;
|
2019-01-23 18:03:33 +01:00
|
|
|
const int div = radius + radius + 1;
|
|
|
|
const int r1 = radius + 1;
|
2019-07-18 15:01:39 +02:00
|
|
|
const int divsum = SQUARE((div + 1) >> 1);
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
// Small buffers
|
2020-03-30 14:00:09 +02:00
|
|
|
int stack[div * 4];
|
|
|
|
zeroClearInt(stack, div * 4);
|
2019-01-23 18:03:33 +01:00
|
|
|
|
2019-07-18 15:01:39 +02:00
|
|
|
int vmin[MAX(w, h)];
|
|
|
|
zeroClearInt(vmin, MAX(w, h));
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
// Large buffers
|
2020-06-04 18:47:15 +02:00
|
|
|
int *r = new int[wh];
|
|
|
|
int *g = new int[wh];
|
|
|
|
int *b = new int[wh];
|
|
|
|
int *a = new int[wh];
|
2019-01-23 18:03:33 +01:00
|
|
|
zeroClearInt(r, wh);
|
|
|
|
zeroClearInt(g, wh);
|
|
|
|
zeroClearInt(b, wh);
|
2020-03-30 14:00:09 +02:00
|
|
|
zeroClearInt(a, wh);
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
const size_t dvcount = 256 * divsum;
|
2020-06-04 18:47:15 +02:00
|
|
|
int *dv = new int[dvcount];
|
2019-01-23 18:03:33 +01:00
|
|
|
int i;
|
2019-07-18 15:01:39 +02:00
|
|
|
for (i = 0; (size_t) i < dvcount; i++) {
|
2019-01-23 18:03:33 +01:00
|
|
|
dv[i] = (i / divsum);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variables
|
|
|
|
int x, y;
|
|
|
|
int *sir;
|
2020-03-30 14:00:09 +02:00
|
|
|
int routsum, goutsum, boutsum, aoutsum;
|
2020-06-04 18:47:15 +02:00
|
|
|
int rinsum, ginsum, binsum, ainsum;
|
2020-03-30 14:00:09 +02:00
|
|
|
int rsum, gsum, bsum, asum, p, yp;
|
2019-01-23 18:03:33 +01:00
|
|
|
int stackpointer;
|
|
|
|
int stackstart;
|
|
|
|
int rbs;
|
|
|
|
|
|
|
|
int yw = 0, yi = 0;
|
2019-07-18 15:01:39 +02:00
|
|
|
for (y = 0; y < h; y++) {
|
2020-03-30 14:00:09 +02:00
|
|
|
ainsum = aoutsum = asum = rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
|
2019-01-23 18:03:33 +01:00
|
|
|
|
2019-07-18 15:01:39 +02:00
|
|
|
for (i = -radius; i <= radius; i++) {
|
2020-03-30 14:00:09 +02:00
|
|
|
sir = &stack[(i + radius) * 4];
|
2019-07-18 15:01:39 +02:00
|
|
|
int offset = (y * stride + (MIN(wm, MAX(i, 0))) * 4);
|
2019-01-23 18:03:33 +01:00
|
|
|
sir[0] = pixels[offset];
|
|
|
|
sir[1] = pixels[offset + 1];
|
|
|
|
sir[2] = pixels[offset + 2];
|
2020-03-30 14:00:09 +02:00
|
|
|
sir[3] = pixels[offset + 3];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
rbs = r1 - abs(i);
|
|
|
|
rsum += sir[0] * rbs;
|
|
|
|
gsum += sir[1] * rbs;
|
|
|
|
bsum += sir[2] * rbs;
|
2020-03-30 14:00:09 +02:00
|
|
|
asum += sir[3] * rbs;
|
2019-07-18 15:01:39 +02:00
|
|
|
if (i > 0) {
|
2019-01-23 18:03:33 +01:00
|
|
|
rinsum += sir[0];
|
|
|
|
ginsum += sir[1];
|
|
|
|
binsum += sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
ainsum += sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
} else {
|
|
|
|
routsum += sir[0];
|
|
|
|
goutsum += sir[1];
|
|
|
|
boutsum += sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
aoutsum += sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
stackpointer = radius;
|
|
|
|
|
2019-07-18 15:01:39 +02:00
|
|
|
for (x = 0; x < w; x++) {
|
2019-01-23 18:03:33 +01:00
|
|
|
r[yi] = dv[rsum];
|
|
|
|
g[yi] = dv[gsum];
|
|
|
|
b[yi] = dv[bsum];
|
2020-03-30 14:00:09 +02:00
|
|
|
a[yi] = dv[asum];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
rsum -= routsum;
|
|
|
|
gsum -= goutsum;
|
|
|
|
bsum -= boutsum;
|
2020-03-30 14:00:09 +02:00
|
|
|
asum -= aoutsum;
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
stackstart = stackpointer - radius + div;
|
2020-03-30 14:00:09 +02:00
|
|
|
sir = &stack[(stackstart % div) * 4];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
routsum -= sir[0];
|
|
|
|
goutsum -= sir[1];
|
|
|
|
boutsum -= sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
aoutsum -= sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
2019-07-18 15:01:39 +02:00
|
|
|
if (y == 0) {
|
2019-01-23 18:03:33 +01:00
|
|
|
vmin[x] = MIN(x + radius + 1, wm);
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:39 +02:00
|
|
|
int offset = (y * stride + vmin[x] * 4);
|
2019-01-23 18:03:33 +01:00
|
|
|
sir[0] = pixels[offset];
|
|
|
|
sir[1] = pixels[offset + 1];
|
|
|
|
sir[2] = pixels[offset + 2];
|
2020-03-30 14:00:09 +02:00
|
|
|
sir[3] = pixels[offset + 3];
|
2019-01-23 18:03:33 +01:00
|
|
|
rinsum += sir[0];
|
|
|
|
ginsum += sir[1];
|
|
|
|
binsum += sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
ainsum += sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
rsum += rinsum;
|
|
|
|
gsum += ginsum;
|
|
|
|
bsum += binsum;
|
2020-03-30 14:00:09 +02:00
|
|
|
asum += ainsum;
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
stackpointer = (stackpointer + 1) % div;
|
2020-03-30 14:00:09 +02:00
|
|
|
sir = &stack[(stackpointer % div) * 4];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
routsum += sir[0];
|
|
|
|
goutsum += sir[1];
|
|
|
|
boutsum += sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
aoutsum += sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
rinsum -= sir[0];
|
|
|
|
ginsum -= sir[1];
|
|
|
|
binsum -= sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
ainsum -= sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
yi++;
|
|
|
|
}
|
|
|
|
yw += w;
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:39 +02:00
|
|
|
for (x = 0; x < w; x++) {
|
2020-03-30 14:00:09 +02:00
|
|
|
ainsum = aoutsum = asum = rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
|
2019-07-18 15:01:39 +02:00
|
|
|
yp = -radius * w;
|
|
|
|
for (i = -radius; i <= radius; i++) {
|
2019-01-23 18:03:33 +01:00
|
|
|
yi = MAX(0, yp) + x;
|
|
|
|
|
2020-03-30 14:00:09 +02:00
|
|
|
sir = &stack[(i + radius) * 4];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
sir[0] = r[yi];
|
|
|
|
sir[1] = g[yi];
|
|
|
|
sir[2] = b[yi];
|
2020-03-30 14:00:09 +02:00
|
|
|
sir[3] = a[yi];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
rbs = r1 - abs(i);
|
|
|
|
|
2019-07-18 15:01:39 +02:00
|
|
|
rsum += r[yi] * rbs;
|
|
|
|
gsum += g[yi] * rbs;
|
|
|
|
bsum += b[yi] * rbs;
|
2020-03-30 14:00:09 +02:00
|
|
|
asum += a[yi] * rbs;
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
rinsum += sir[0];
|
|
|
|
ginsum += sir[1];
|
|
|
|
binsum += sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
ainsum += sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
} else {
|
|
|
|
routsum += sir[0];
|
|
|
|
goutsum += sir[1];
|
|
|
|
boutsum += sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
aoutsum += sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i < hm) {
|
|
|
|
yp += w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stackpointer = radius;
|
2019-07-18 15:01:39 +02:00
|
|
|
for (y = 0; y < h; y++) {
|
|
|
|
int offset = stride * y + x * 4;
|
|
|
|
pixels[offset] = dv[rsum];
|
2019-01-23 18:03:33 +01:00
|
|
|
pixels[offset + 1] = dv[gsum];
|
|
|
|
pixels[offset + 2] = dv[bsum];
|
2020-03-30 14:00:09 +02:00
|
|
|
pixels[offset + 3] = dv[asum];
|
2019-01-23 18:03:33 +01:00
|
|
|
rsum -= routsum;
|
|
|
|
gsum -= goutsum;
|
|
|
|
bsum -= boutsum;
|
2020-03-30 14:00:09 +02:00
|
|
|
asum -= aoutsum;
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
stackstart = stackpointer - radius + div;
|
2020-03-30 14:00:09 +02:00
|
|
|
sir = &stack[(stackstart % div) * 4];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
routsum -= sir[0];
|
|
|
|
goutsum -= sir[1];
|
|
|
|
boutsum -= sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
aoutsum -= sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
2019-07-18 15:01:39 +02:00
|
|
|
if (x == 0) {
|
|
|
|
vmin[y] = (MIN(y + r1, hm)) * w;
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|
|
|
|
p = x + vmin[y];
|
|
|
|
|
|
|
|
sir[0] = r[p];
|
|
|
|
sir[1] = g[p];
|
|
|
|
sir[2] = b[p];
|
2020-03-30 14:00:09 +02:00
|
|
|
sir[3] = a[p];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
rinsum += sir[0];
|
|
|
|
ginsum += sir[1];
|
|
|
|
binsum += sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
ainsum += sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
rsum += rinsum;
|
|
|
|
gsum += ginsum;
|
|
|
|
bsum += binsum;
|
2020-03-30 14:00:09 +02:00
|
|
|
asum += ainsum;
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
stackpointer = (stackpointer + 1) % div;
|
2020-03-30 14:00:09 +02:00
|
|
|
sir = &stack[stackpointer * 4];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
routsum += sir[0];
|
|
|
|
goutsum += sir[1];
|
|
|
|
boutsum += sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
aoutsum += sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
rinsum -= sir[0];
|
|
|
|
ginsum -= sir[1];
|
|
|
|
binsum -= sir[2];
|
2020-03-30 14:00:09 +02:00
|
|
|
ainsum -= sir[3];
|
2019-01-23 18:03:33 +01:00
|
|
|
|
|
|
|
yi += w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
delete[] r;
|
|
|
|
delete[] g;
|
|
|
|
delete[] b;
|
|
|
|
delete[] a;
|
|
|
|
delete[] dv;
|
2019-01-23 18:03:33 +01:00
|
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
2019-12-31 14:08:08 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 15:48:47 +02:00
|
|
|
JNIEXPORT void Java_org_telegram_messenger_Utilities_drawDitheredGradient(JNIEnv *env, jclass clazz, jobject bitmap, jintArray colors, jint startX, jint startY, jint endX, jint endY) {
|
2019-12-31 14:08:08 +01:00
|
|
|
AndroidBitmapInfo info;
|
2020-06-04 18:47:15 +02:00
|
|
|
void *pixelsBuffer;
|
2019-12-31 14:08:08 +01:00
|
|
|
int reason;
|
|
|
|
|
|
|
|
if ((reason = AndroidBitmap_getInfo(env, bitmap, &info)) != ANDROID_BITMAP_RESULT_SUCCESS) {
|
2020-06-04 18:47:15 +02:00
|
|
|
env->ThrowNew(jclass_RuntimeException, "AndroidBitmap_getInfo failed with a reason: " + reason);
|
2019-12-31 14:08:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
|
2020-06-04 18:47:15 +02:00
|
|
|
env->ThrowNew(jclass_RuntimeException, "Bitmap must be in ARGB_8888 format");
|
2019-12-31 14:08:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((reason = AndroidBitmap_lockPixels(env, bitmap, &pixelsBuffer)) != ANDROID_BITMAP_RESULT_SUCCESS) {
|
2020-06-04 18:47:15 +02:00
|
|
|
env->ThrowNew(jclass_RuntimeException, "AndroidBitmap_lockPixels failed with a reason: " + reason);
|
2019-12-31 14:08:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t i, j, n;
|
|
|
|
|
|
|
|
// gradient colors extracting
|
2020-06-04 18:47:15 +02:00
|
|
|
jint *colorsBuffer = env->GetIntArrayElements(colors, 0);
|
2019-12-31 14:08:08 +01:00
|
|
|
uint8_t *colorsComponents = (uint8_t *) colorsBuffer;
|
|
|
|
float colorsF[4][2];
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
// swap red and green channels
|
|
|
|
n = (uint8_t) (i == 0 ? 2 : (i == 2 ? 0 : i));
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
colorsF[n][j] = colorsComponents[j * 4 + i] / 255.F;
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
env->ReleaseIntArrayElements(colors, colorsBuffer, JNI_ABORT);
|
2019-12-31 14:08:08 +01:00
|
|
|
|
|
|
|
// gradient vector
|
|
|
|
const int32_t vx = endX - startX;
|
|
|
|
const int32_t vy = endY - startY;
|
|
|
|
const float vSquaredMag = vx * vx + vy * vy;
|
|
|
|
|
|
|
|
float noise, fraction, error, componentF;
|
2020-06-04 18:47:15 +02:00
|
|
|
float *pixelsComponentsF = new float[info.height * info.stride * 4];
|
2019-12-31 14:08:08 +01:00
|
|
|
memset(pixelsComponentsF, 0, info.height * info.stride * 4 * sizeof(float));
|
2020-06-04 18:47:15 +02:00
|
|
|
uint8_t * bitmapPixelsComponents = (uint8_t * )
|
|
|
|
pixelsBuffer;
|
2019-12-31 14:08:08 +01:00
|
|
|
|
|
|
|
int32_t x, y;
|
|
|
|
int32_t offset;
|
|
|
|
int32_t position;
|
|
|
|
for (y = 0; y < info.height; y++) {
|
|
|
|
offset = y * info.stride;
|
|
|
|
for (x = 0; x < info.width; x++) {
|
|
|
|
// triangular probability density function dither noise
|
|
|
|
noise = (rand() - rand()) / 255.F / RAND_MAX;
|
|
|
|
|
|
|
|
// alpha channel
|
|
|
|
bitmapPixelsComponents[offset + x * 4 + 3] = 255;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
position = offset + x * 4 + i;
|
|
|
|
fraction = (vx * (x - startX) + vy * (y - startY)) / vSquaredMag;
|
|
|
|
|
|
|
|
// gradient interpolation and noise
|
|
|
|
pixelsComponentsF[position] += colorsF[i][0] + fraction * (colorsF[i][1] - colorsF[i][0]) + noise;
|
|
|
|
|
|
|
|
// clamp
|
|
|
|
if (pixelsComponentsF[position] > 1.F) {
|
|
|
|
pixelsComponentsF[position] = 1.F;
|
|
|
|
} else if (pixelsComponentsF[position] < 0.F) {
|
|
|
|
pixelsComponentsF[position] = 0.F;
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw
|
|
|
|
componentF = roundf(pixelsComponentsF[position] * 255.F);
|
2020-06-04 18:47:15 +02:00
|
|
|
bitmapPixelsComponents[position] = (uint8_t)
|
|
|
|
componentF;
|
2019-12-31 14:08:08 +01:00
|
|
|
|
|
|
|
// floyd-steinberg dithering
|
|
|
|
error = pixelsComponentsF[position] - componentF / 255.F;
|
|
|
|
if (x + 1 < info.width) {
|
|
|
|
pixelsComponentsF[position + 4] += error * 7.F / 16.F;
|
|
|
|
if (y + 1 < info.height) {
|
|
|
|
pixelsComponentsF[position + info.height + 4] += error * 1.F / 16.F;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (y + 1 < info.height) {
|
|
|
|
pixelsComponentsF[position + info.height] += error * 5.F / 16.F;
|
|
|
|
if (x - 1 >= 0) {
|
|
|
|
pixelsComponentsF[position + info.height - 4] += error * 3.F / 16.F;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
delete[] pixelsComponentsF;
|
2019-12-31 14:08:08 +01:00
|
|
|
|
|
|
|
if ((reason = AndroidBitmap_unlockPixels(env, bitmap)) != ANDROID_BITMAP_RESULT_SUCCESS) {
|
2020-06-04 18:47:15 +02:00
|
|
|
env->ThrowNew(jclass_RuntimeException, "AndroidBitmap_unlockPixels failed with a reason: " + reason);
|
2019-12-31 14:08:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-06-04 18:47:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 15:48:47 +02:00
|
|
|
JNIEXPORT jint Java_org_telegram_messenger_Utilities_saveProgressiveJpeg(JNIEnv *env, jclass clazz, jobject bitmap, jint width, jint height, jint stride, jint quality, jstring path) {
|
|
|
|
if (!bitmap || !path || !width || !height || !stride || stride != width * 4) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void *pixels = 0;
|
|
|
|
if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (pixels == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
tjhandle handle = 0;
|
|
|
|
if ((handle = tjInitCompress()) == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const char *pathStr = env->GetStringUTFChars(path, 0);
|
|
|
|
std::string filePath = std::string(pathStr);
|
|
|
|
if (pathStr != 0) {
|
|
|
|
env->ReleaseStringUTFChars(path, pathStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *enabledValue = "1";
|
|
|
|
const char *disabledValue = "0";
|
|
|
|
setenv("TJ_OPTIMIZE", enabledValue, 1);
|
|
|
|
setenv("TJ_ARITHMETIC", disabledValue, 1);
|
|
|
|
setenv("TJ_PROGRESSIVE", enabledValue, 1);
|
|
|
|
setenv("TJ_REVERT", enabledValue, 1);
|
|
|
|
|
|
|
|
TJSAMP jpegSubsamp = TJSAMP::TJSAMP_420;
|
|
|
|
jint buffSize = (jint) tjBufSize(width, height, jpegSubsamp);
|
|
|
|
unsigned char *jpegBuf = new unsigned char[buffSize];
|
|
|
|
unsigned char *srcBuf = (unsigned char *) pixels;
|
|
|
|
|
|
|
|
int pf = org_libjpegturbo_turbojpeg_TJ_PF_RGBA;
|
|
|
|
|
|
|
|
jsize actualPitch = width * tjPixelSize[pf];
|
|
|
|
jsize arraySize = (height - 1) * actualPitch + (width) * tjPixelSize[pf];
|
|
|
|
unsigned long jpegSize = tjBufSize(width, height, jpegSubsamp);
|
|
|
|
|
|
|
|
if (tjCompress2(handle, srcBuf, width, stride, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, quality, TJFLAG_ACCURATEDCT | TJFLAG_PROGRESSIVE | TJFLAG_NOREALLOC) == 0) {
|
|
|
|
FILE *f = fopen(filePath.c_str(), "wb");
|
|
|
|
if (f && fwrite(jpegBuf, sizeof(unsigned char), jpegSize, f) == jpegSize) {
|
|
|
|
fflush(f);
|
|
|
|
fsync(fileno(f));
|
|
|
|
} else {
|
|
|
|
jpegSize = -1;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
} else {
|
|
|
|
jpegSize = -1;
|
|
|
|
}
|
|
|
|
delete[] jpegBuf;
|
|
|
|
tjDestroy(handle);
|
|
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
|
|
return jpegSize;
|
|
|
|
|
|
|
|
/*struct jpeg_compress_struct cinfo;
|
|
|
|
struct jpeg_error_mgr jerr;
|
|
|
|
cinfo.err = jpeg_std_error(&jerr);
|
|
|
|
jpeg_create_compress(&cinfo);
|
|
|
|
|
|
|
|
const char *pathStr = env->GetStringUTFChars(path, 0);
|
|
|
|
std::string filePath = std::string(pathStr);
|
|
|
|
if (pathStr != 0) {
|
|
|
|
env->ReleaseStringUTFChars(path, pathStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *outBuffer = NULL;
|
|
|
|
unsigned long outSize = 0;
|
|
|
|
jpeg_mem_dest(&cinfo, &outBuffer, &outSize);
|
|
|
|
unsigned char *srcBuf = (unsigned char *) pixels;
|
|
|
|
|
|
|
|
cinfo.image_width = (uint32_t) width;
|
|
|
|
cinfo.image_height = (uint32_t) height;
|
|
|
|
cinfo.input_components = 4;
|
|
|
|
cinfo.in_color_space = JCS_EXT_RGBA;
|
|
|
|
jpeg_c_set_int_param(&cinfo, JINT_COMPRESS_PROFILE, JCP_FASTEST);
|
|
|
|
jpeg_set_defaults(&cinfo);
|
|
|
|
cinfo.arith_code = FALSE;
|
|
|
|
cinfo.dct_method = JDCT_ISLOW;
|
|
|
|
cinfo.optimize_coding = TRUE;
|
|
|
|
jpeg_set_quality(&cinfo, 78, 1);
|
|
|
|
jpeg_simple_progression(&cinfo);
|
|
|
|
jpeg_start_compress(&cinfo, 1);
|
|
|
|
|
|
|
|
JSAMPROW rowPointer[1];
|
|
|
|
while (cinfo.next_scanline < cinfo.image_height) {
|
|
|
|
rowPointer[0] = (JSAMPROW) (srcBuf + cinfo.next_scanline * stride);
|
|
|
|
jpeg_write_scanlines(&cinfo, rowPointer, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
jpeg_finish_compress(&cinfo);
|
|
|
|
|
|
|
|
FILE *f = fopen(filePath.c_str(), "wb");
|
|
|
|
if (f && fwrite(outBuffer, sizeof(uint8_t), outSize, f) == outSize) {
|
|
|
|
fflush(f);
|
|
|
|
fsync(fileno(f));
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
jpeg_destroy_compress(&cinfo);
|
|
|
|
return outSize;*/
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
std::vector<std::pair<float, float>> gatherPositions(std::vector<std::pair<float, float>> list, int phase) {
|
|
|
|
std::vector<std::pair<float, float>> result(4);
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
int pos = phase + i * 2;
|
|
|
|
while (pos >= 8) {
|
|
|
|
pos -= 8;
|
|
|
|
}
|
|
|
|
result[i] = list[pos];
|
|
|
|
result[i].second = 1.0f - result[i].second;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:24:57 +02:00
|
|
|
thread_local static float *pixelCache = nullptr;
|
2021-06-25 02:43:10 +02:00
|
|
|
|
|
|
|
JNIEXPORT void Java_org_telegram_messenger_Utilities_generateGradient(JNIEnv *env, jclass clazz, jobject bitmap, jboolean unpin, jint phase, jfloat progress, jint width, jint height, jint stride, jintArray colors) {
|
|
|
|
if (!bitmap) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!width || !height) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *pixels = nullptr;
|
|
|
|
if (AndroidBitmap_lockPixels(env, bitmap, (void **) &pixels) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<float, float>> positions{
|
|
|
|
{0.80f, 0.10f},
|
|
|
|
{0.60f, 0.20f},
|
|
|
|
{0.35f, 0.25f},
|
|
|
|
{0.25f, 0.60f},
|
|
|
|
{0.20f, 0.90f},
|
|
|
|
{0.40f, 0.80f},
|
|
|
|
{0.65f, 0.75f},
|
|
|
|
{0.75f, 0.40f}
|
|
|
|
};
|
|
|
|
|
|
|
|
int32_t previousPhase = phase + 1;
|
|
|
|
if (previousPhase > 7) {
|
|
|
|
previousPhase = 0;
|
|
|
|
}
|
|
|
|
std::vector<std::pair<float, float>> previous = gatherPositions(positions, previousPhase);
|
|
|
|
std::vector<std::pair<float, float>> current = gatherPositions(positions, phase);
|
|
|
|
|
|
|
|
auto colorsArray = (uint8_t *) env->GetIntArrayElements(colors, nullptr);
|
2021-07-15 16:24:57 +02:00
|
|
|
float *newPixelCache = nullptr;
|
2021-06-25 02:43:10 +02:00
|
|
|
if (pixelCache == nullptr) {
|
|
|
|
newPixelCache = new float[width * height * 2];
|
2021-07-15 16:24:57 +02:00
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
float directPixelY;
|
|
|
|
float centerDistanceY;
|
|
|
|
float centerDistanceY2;
|
2021-08-31 21:06:39 +02:00
|
|
|
int32_t colorsCount = colorsArray[12] == 0 && colorsArray[13] == 0 && colorsArray[14] == 0 && colorsArray[15] == 0 ? 3 : 4;
|
2021-06-25 02:43:10 +02:00
|
|
|
|
|
|
|
for (int y = 0; y < height; y++) {
|
2021-07-15 16:24:57 +02:00
|
|
|
if (pixelCache == nullptr) {
|
2021-06-25 02:43:10 +02:00
|
|
|
directPixelY = (float) y / (float) height;
|
|
|
|
centerDistanceY = directPixelY - 0.5f;
|
|
|
|
centerDistanceY2 = centerDistanceY * centerDistanceY;
|
2021-07-15 16:24:57 +02:00
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
uint32_t offset = y * stride;
|
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
|
float pixelX;
|
|
|
|
float pixelY;
|
2021-07-15 16:24:57 +02:00
|
|
|
if (pixelCache != nullptr) {
|
2021-06-25 02:43:10 +02:00
|
|
|
pixelX = pixelCache[(y * width + x) * 2];
|
2021-07-15 16:24:57 +02:00
|
|
|
pixelY = pixelCache[(y * width + x) * 2 + 1];
|
|
|
|
} else {
|
2021-06-25 02:43:10 +02:00
|
|
|
float directPixelX = (float) x / (float) width;
|
|
|
|
|
|
|
|
float centerDistanceX = directPixelX - 0.5f;
|
|
|
|
float centerDistance = sqrtf(centerDistanceX * centerDistanceX + centerDistanceY2);
|
|
|
|
|
|
|
|
float swirlFactor = 0.35f * centerDistance;
|
|
|
|
float theta = swirlFactor * swirlFactor * 0.8f * 8.0f;
|
|
|
|
float sinTheta = sinf(theta);
|
|
|
|
float cosTheta = cosf(theta);
|
|
|
|
|
2021-07-15 16:24:57 +02:00
|
|
|
pixelX = newPixelCache[(y * width + x) * 2] = std::max(0.0f, std::min(1.0f, 0.5f + centerDistanceX * cosTheta - centerDistanceY * sinTheta));
|
|
|
|
pixelY = newPixelCache[(y * width + x) * 2 + 1] = std::max(0.0f, std::min(1.0f, 0.5f + centerDistanceX * sinTheta + centerDistanceY * cosTheta));
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
|
|
|
|
float distanceSum = 0.0f;
|
|
|
|
|
|
|
|
float r = 0.0f;
|
|
|
|
float g = 0.0f;
|
|
|
|
float b = 0.0f;
|
|
|
|
|
|
|
|
for (int i = 0; i < colorsCount; i++) {
|
|
|
|
float colorX = previous[i].first + (current[i].first - previous[i].first) * progress;
|
|
|
|
float colorY = previous[i].second + (current[i].second - previous[i].second) * progress;
|
|
|
|
|
|
|
|
float distanceX = pixelX - colorX;
|
|
|
|
float distanceY = pixelY - colorY;
|
|
|
|
|
|
|
|
float distance = std::max(0.0f, 0.9f - sqrtf(distanceX * distanceX + distanceY * distanceY));
|
|
|
|
distance = distance * distance * distance * distance;
|
|
|
|
distanceSum += distance;
|
|
|
|
|
|
|
|
r = r + distance * ((float) colorsArray[i * 4] / 255.0f);
|
|
|
|
g = g + distance * ((float) colorsArray[i * 4 + 1] / 255.0f);
|
|
|
|
b = b + distance * ((float) colorsArray[i * 4 + 2] / 255.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
pixels[offset + x * 4] = (uint8_t) (b / distanceSum * 255.0f);
|
|
|
|
pixels[offset + x * 4 + 1] = (uint8_t) (g / distanceSum * 255.0f);
|
|
|
|
pixels[offset + x * 4 + 2] = (uint8_t) (r / distanceSum * 255.0f);
|
|
|
|
pixels[offset + x * 4 + 3] = 0xff;
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 16:24:57 +02:00
|
|
|
if (newPixelCache != nullptr) {
|
2021-06-25 02:43:10 +02:00
|
|
|
delete [] pixelCache;
|
|
|
|
pixelCache = newPixelCache;
|
2021-07-15 16:24:57 +02:00
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
|
|
|
|
env->ReleaseIntArrayElements(colors, (jint *) colorsArray, JNI_ABORT);
|
|
|
|
|
|
|
|
if (unpin) {
|
|
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 18:03:33 +01:00
|
|
|
}
|