mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Merge pull request #2146 from kwagyeman/kwabena/simplify_binary
modules/py_image: Update binary to use mp_arg_parse_all.
This commit is contained in:
commit
b2b62dbac8
@ -11,6 +11,113 @@
|
|||||||
#include "imlib.h"
|
#include "imlib.h"
|
||||||
|
|
||||||
#ifdef IMLIB_ENABLE_BINARY_OPS
|
#ifdef IMLIB_ENABLE_BINARY_OPS
|
||||||
|
void imlib_zero_line_op(int x, int x_end, int y_row, imlib_draw_row_data_t *data) {
|
||||||
|
image_t *mask = data->callback_arg;
|
||||||
|
|
||||||
|
switch (data->dst_img->pixfmt) {
|
||||||
|
case PIXFORMAT_BINARY: {
|
||||||
|
uint32_t *row = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(data->dst_img, y_row);
|
||||||
|
|
||||||
|
if (!mask) {
|
||||||
|
for (; x < x_end; x++) {
|
||||||
|
if (IMAGE_GET_BINARY_PIXEL_FAST(((uint32_t *) data->dst_row_override), x)) {
|
||||||
|
IMAGE_CLEAR_BINARY_PIXEL_FAST(row, x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (; x < x_end; x++) {
|
||||||
|
if (image_get_mask_pixel(mask, x, y_row) &&
|
||||||
|
IMAGE_GET_BINARY_PIXEL_FAST(((uint32_t *) data->dst_row_override), x)) {
|
||||||
|
IMAGE_CLEAR_BINARY_PIXEL_FAST(row, x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PIXFORMAT_GRAYSCALE: {
|
||||||
|
uint8_t *row = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(data->dst_img, y_row);
|
||||||
|
|
||||||
|
if (!mask) {
|
||||||
|
for (; x < x_end; x++) {
|
||||||
|
if (IMAGE_GET_GRAYSCALE_PIXEL_FAST(((uint8_t *) data->dst_row_override), x)) {
|
||||||
|
IMAGE_PUT_GRAYSCALE_PIXEL_FAST(row, x, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (; x < x_end; x++) {
|
||||||
|
if (image_get_mask_pixel(mask, x, y_row) &&
|
||||||
|
IMAGE_GET_GRAYSCALE_PIXEL_FAST(((uint8_t *) data->dst_row_override), x)) {
|
||||||
|
IMAGE_PUT_GRAYSCALE_PIXEL_FAST(row, x, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PIXFORMAT_RGB565: {
|
||||||
|
uint16_t *row = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(data->dst_img, y_row);
|
||||||
|
|
||||||
|
if (!mask) {
|
||||||
|
for (; x < x_end; x++) {
|
||||||
|
if (IMAGE_GET_RGB565_PIXEL_FAST(((uint16_t *) data->dst_row_override), x)) {
|
||||||
|
IMAGE_PUT_RGB565_PIXEL_FAST(row, x, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (; x < x_end; x++) {
|
||||||
|
if (image_get_mask_pixel(mask, x, y_row) &&
|
||||||
|
IMAGE_GET_RGB565_PIXEL_FAST(((uint16_t *) data->dst_row_override), x)) {
|
||||||
|
IMAGE_PUT_RGB565_PIXEL_FAST(row, x, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void imlib_mask_line_op(int x, int x_end, int y_row, imlib_draw_row_data_t *data) {
|
||||||
|
image_t *mask = data->callback_arg;
|
||||||
|
|
||||||
|
switch (data->dst_img->pixfmt) {
|
||||||
|
case PIXFORMAT_BINARY: {
|
||||||
|
uint32_t *row = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(data->dst_img, y_row);
|
||||||
|
for (; x < x_end; x++) {
|
||||||
|
if (image_get_mask_pixel(mask, x, y_row)) {
|
||||||
|
int otherPixel = IMAGE_GET_BINARY_PIXEL_FAST(((uint32_t *) data->dst_row_override), x);
|
||||||
|
IMAGE_PUT_BINARY_PIXEL_FAST(row, x, otherPixel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PIXFORMAT_GRAYSCALE: {
|
||||||
|
uint8_t *row = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(data->dst_img, y_row);
|
||||||
|
for (; x < x_end; x++) {
|
||||||
|
if (image_get_mask_pixel(mask, x, y_row)) {
|
||||||
|
int otherPixel = IMAGE_GET_GRAYSCALE_PIXEL_FAST(((uint8_t *) data->dst_row_override), x);
|
||||||
|
IMAGE_PUT_GRAYSCALE_PIXEL_FAST(row, x, otherPixel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PIXFORMAT_RGB565: {
|
||||||
|
uint16_t *row = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(data->dst_img, y_row);
|
||||||
|
for (; x < x_end; x++) {
|
||||||
|
if (image_get_mask_pixel(mask, x, y_row)) {
|
||||||
|
int otherPixel = IMAGE_GET_RGB565_PIXEL_FAST(((uint16_t *) data->dst_row_override), x);
|
||||||
|
IMAGE_PUT_RGB565_PIXEL_FAST(row, x, otherPixel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void imlib_binary(image_t *out, image_t *img, list_t *thresholds, bool invert, bool zero, image_t *mask) {
|
void imlib_binary(image_t *out, image_t *img, list_t *thresholds, bool invert, bool zero, image_t *mask) {
|
||||||
image_t bmp;
|
image_t bmp;
|
||||||
bmp.w = img->w;
|
bmp.w = img->w;
|
||||||
@ -64,163 +171,27 @@ void imlib_binary(image_t *out, image_t *img, list_t *thresholds, bool invert, b
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (img->pixfmt) {
|
imlib_draw_row_callback_t callback = NULL;
|
||||||
case PIXFORMAT_BINARY: {
|
if (zero) {
|
||||||
if (!zero) {
|
callback = imlib_zero_line_op;
|
||||||
for (int y = 0, yy = img->h; y < yy; y++) {
|
|
||||||
uint32_t *old_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, y);
|
|
||||||
uint32_t *bmp_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&bmp, y);
|
|
||||||
uint32_t *out_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(out, y);
|
|
||||||
for (int x = 0, xx = img->w; x < xx; x++) {
|
|
||||||
int pixel = ((!mask) || image_get_mask_pixel(mask, x, y))
|
|
||||||
? IMAGE_GET_BINARY_PIXEL_FAST(bmp_row_ptr, x)
|
|
||||||
: IMAGE_GET_BINARY_PIXEL_FAST(old_row_ptr, x);
|
|
||||||
IMAGE_PUT_BINARY_PIXEL_FAST(out_row_ptr, x, pixel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (int y = 0, yy = img->h; y < yy; y++) {
|
|
||||||
uint32_t *old_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, y);
|
|
||||||
uint32_t *bmp_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&bmp, y);
|
|
||||||
uint32_t *out_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(out, y);
|
|
||||||
for (int x = 0, xx = img->w; x < xx; x++) {
|
|
||||||
int pixel = IMAGE_GET_BINARY_PIXEL_FAST(old_row_ptr, x);
|
|
||||||
if (((!mask) || image_get_mask_pixel(mask, x, y))
|
|
||||||
&& IMAGE_GET_BINARY_PIXEL_FAST(bmp_row_ptr, x)) {
|
|
||||||
pixel = 0;
|
|
||||||
}
|
|
||||||
IMAGE_PUT_BINARY_PIXEL_FAST(out_row_ptr, x, pixel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PIXFORMAT_GRAYSCALE: {
|
|
||||||
if (out->pixfmt == PIXFORMAT_BINARY) {
|
|
||||||
if (!zero) {
|
|
||||||
for (int y = 0, yy = img->h; y < yy; y++) {
|
|
||||||
uint8_t *old_row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, y);
|
|
||||||
uint32_t *bmp_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&bmp, y);
|
|
||||||
uint32_t *out_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(out, y);
|
|
||||||
for (int x = 0, xx = img->w; x < xx; x++) {
|
|
||||||
int pixel = ((!mask) || image_get_mask_pixel(mask, x, y))
|
|
||||||
? IMAGE_GET_BINARY_PIXEL_FAST(bmp_row_ptr, x)
|
|
||||||
: COLOR_GRAYSCALE_TO_BINARY(IMAGE_GET_GRAYSCALE_PIXEL_FAST(old_row_ptr, x));
|
|
||||||
IMAGE_PUT_BINARY_PIXEL_FAST(out_row_ptr, x, pixel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (int y = 0, yy = img->h; y < yy; y++) {
|
|
||||||
uint8_t *old_row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, y);
|
|
||||||
uint32_t *bmp_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&bmp, y);
|
|
||||||
uint32_t *out_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(out, y);
|
|
||||||
for (int x = 0, xx = img->w; x < xx; x++) {
|
|
||||||
int pixel = COLOR_GRAYSCALE_TO_BINARY(IMAGE_GET_GRAYSCALE_PIXEL_FAST(old_row_ptr, x));
|
|
||||||
if (((!mask) || image_get_mask_pixel(mask, x, y))
|
|
||||||
&& IMAGE_GET_BINARY_PIXEL_FAST(bmp_row_ptr, x)) {
|
|
||||||
pixel = 0;
|
|
||||||
}
|
|
||||||
IMAGE_PUT_BINARY_PIXEL_FAST(out_row_ptr, x, pixel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!zero) {
|
|
||||||
for (int y = 0, yy = img->h; y < yy; y++) {
|
|
||||||
uint8_t *old_row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, y);
|
|
||||||
uint32_t *bmp_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&bmp, y);
|
|
||||||
uint8_t *out_row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(out, y);
|
|
||||||
for (int x = 0, xx = img->w; x < xx; x++) {
|
|
||||||
int pixel = ((!mask) || image_get_mask_pixel(mask, x, y))
|
|
||||||
? COLOR_BINARY_TO_GRAYSCALE(IMAGE_GET_BINARY_PIXEL_FAST(bmp_row_ptr, x))
|
|
||||||
: IMAGE_GET_GRAYSCALE_PIXEL_FAST(old_row_ptr, x);
|
|
||||||
IMAGE_PUT_GRAYSCALE_PIXEL_FAST(out_row_ptr, x, pixel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (int y = 0, yy = img->h; y < yy; y++) {
|
|
||||||
uint8_t *old_row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, y);
|
|
||||||
uint32_t *bmp_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&bmp, y);
|
|
||||||
uint8_t *out_row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(out, y);
|
|
||||||
for (int x = 0, xx = img->w; x < xx; x++) {
|
|
||||||
int pixel = IMAGE_GET_GRAYSCALE_PIXEL_FAST(old_row_ptr, x);
|
|
||||||
if (((!mask) || image_get_mask_pixel(mask, x, y))
|
|
||||||
&& IMAGE_GET_BINARY_PIXEL_FAST(bmp_row_ptr, x)) {
|
|
||||||
pixel = 0;
|
|
||||||
}
|
|
||||||
IMAGE_PUT_GRAYSCALE_PIXEL_FAST(out_row_ptr, x, pixel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case PIXFORMAT_RGB565: {
|
|
||||||
if (out->pixfmt == PIXFORMAT_BINARY) {
|
|
||||||
if (!zero) {
|
|
||||||
for (int y = 0, yy = img->h; y < yy; y++) {
|
|
||||||
uint16_t *old_row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y);
|
|
||||||
uint32_t *bmp_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&bmp, y);
|
|
||||||
uint32_t *out_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(out, y);
|
|
||||||
for (int x = 0, xx = img->w; x < xx; x++) {
|
|
||||||
int pixel = ((!mask) || image_get_mask_pixel(mask, x, y))
|
|
||||||
? IMAGE_GET_BINARY_PIXEL_FAST(bmp_row_ptr, x)
|
|
||||||
: COLOR_RGB565_TO_BINARY(IMAGE_GET_RGB565_PIXEL_FAST(old_row_ptr, x));
|
|
||||||
IMAGE_PUT_BINARY_PIXEL_FAST(out_row_ptr, x, pixel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (int y = 0, yy = img->h; y < yy; y++) {
|
|
||||||
uint16_t *old_row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y);
|
|
||||||
uint32_t *bmp_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&bmp, y);
|
|
||||||
uint32_t *out_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(out, y);
|
|
||||||
for (int x = 0, xx = img->w; x < xx; x++) {
|
|
||||||
int pixel = COLOR_RGB565_TO_BINARY(IMAGE_GET_RGB565_PIXEL_FAST(old_row_ptr, x));
|
|
||||||
if (((!mask) || image_get_mask_pixel(mask, x, y))
|
|
||||||
&& IMAGE_GET_BINARY_PIXEL_FAST(bmp_row_ptr, x)) {
|
|
||||||
pixel = 0;
|
|
||||||
}
|
|
||||||
IMAGE_PUT_BINARY_PIXEL_FAST(out_row_ptr, x, pixel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!zero) {
|
|
||||||
for (int y = 0, yy = img->h; y < yy; y++) {
|
|
||||||
uint16_t *old_row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y);
|
|
||||||
uint32_t *bmp_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&bmp, y);
|
|
||||||
uint16_t *out_row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(out, y);
|
|
||||||
for (int x = 0, xx = img->w; x < xx; x++) {
|
|
||||||
int pixel = ((!mask) || image_get_mask_pixel(mask, x, y))
|
|
||||||
? COLOR_BINARY_TO_RGB565(IMAGE_GET_BINARY_PIXEL_FAST(bmp_row_ptr, x))
|
|
||||||
: IMAGE_GET_RGB565_PIXEL_FAST(old_row_ptr, x);
|
|
||||||
IMAGE_PUT_RGB565_PIXEL_FAST(out_row_ptr, x, pixel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (int y = 0, yy = img->h; y < yy; y++) {
|
|
||||||
uint16_t *old_row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y);
|
|
||||||
uint32_t *bmp_row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&bmp, y);
|
|
||||||
uint16_t *out_row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(out, y);
|
|
||||||
for (int x = 0, xx = img->w; x < xx; x++) {
|
|
||||||
int pixel = IMAGE_GET_RGB565_PIXEL_FAST(old_row_ptr, x);
|
|
||||||
if (((!mask) || image_get_mask_pixel(mask, x, y))
|
|
||||||
&& IMAGE_GET_BINARY_PIXEL_FAST(bmp_row_ptr, x)) {
|
|
||||||
pixel = 0;
|
|
||||||
}
|
|
||||||
IMAGE_PUT_RGB565_PIXEL_FAST(out_row_ptr, x, pixel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fb_free();
|
if ((!callback) && mask) {
|
||||||
|
callback = imlib_mask_line_op;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *dst_row_override = NULL;
|
||||||
|
if (callback) {
|
||||||
|
dst_row_override = fb_alloc0(image_line_size(out), FB_ALLOC_CACHE_ALIGN);
|
||||||
|
}
|
||||||
|
|
||||||
|
imlib_draw_image(out, &bmp, 0, 0, 1.0f, 1.0f, NULL, -1, 256, NULL, NULL, 0, callback, mask, dst_row_override);
|
||||||
|
|
||||||
|
if (dst_row_override) {
|
||||||
|
fb_free(); // dst_row_override
|
||||||
|
}
|
||||||
|
|
||||||
|
fb_free(); // bmp.data
|
||||||
}
|
}
|
||||||
|
|
||||||
void imlib_invert(image_t *img) {
|
void imlib_invert(image_t *img) {
|
||||||
|
|||||||
@ -1371,6 +1371,8 @@ void imlib_awb(image_t *img, uint32_t r_out, uint32_t g_out, uint32_t b_out);
|
|||||||
void imlib_ccm(image_t *img, float *ccm, bool offset);
|
void imlib_ccm(image_t *img, float *ccm, bool offset);
|
||||||
void imlib_gamma(image_t *img, float gamma, float scale, float offset);
|
void imlib_gamma(image_t *img, float gamma, float scale, float offset);
|
||||||
// Binary Functions
|
// Binary Functions
|
||||||
|
void imlib_zero_line_op(int x, int x_end, int y_row, imlib_draw_row_data_t *data);
|
||||||
|
void imlib_mask_line_op(int x, int x_end, int y_row, imlib_draw_row_data_t *data);
|
||||||
void imlib_binary(image_t *out, image_t *img, list_t *thresholds, bool invert, bool zero, image_t *mask);
|
void imlib_binary(image_t *out, image_t *img, list_t *thresholds, bool invert, bool zero, image_t *mask);
|
||||||
void imlib_invert(image_t *img);
|
void imlib_invert(image_t *img);
|
||||||
void imlib_b_and_line_op(image_t *img, int line, void *other, void *data, bool vflipped);
|
void imlib_b_and_line_op(image_t *img, int line, void *other, void *data, bool vflipped);
|
||||||
|
|||||||
@ -1914,34 +1914,35 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_gamma_obj, 1, py_image_gamma);
|
|||||||
// Binary Methods
|
// Binary Methods
|
||||||
/////////////////
|
/////////////////
|
||||||
|
|
||||||
STATIC mp_obj_t py_image_binary(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
STATIC mp_obj_t py_image_binary(uint n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||||
image_t *arg_img = py_helper_arg_to_image(args[0], ARG_IMAGE_MUTABLE);
|
enum { ARG_thresholds, ARG_invert, ARG_zero, ARG_mask, ARG_to_bitmap, ARG_copy };
|
||||||
|
static const mp_arg_t allowed_args[] = {
|
||||||
|
{ MP_QSTR_thresholds, MP_ARG_OBJ | MP_ARG_REQUIRED, },
|
||||||
|
{ MP_QSTR_invert, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
|
||||||
|
{ MP_QSTR_zero, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
|
||||||
|
{ MP_QSTR_mask, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = MP_ROM_NONE} },
|
||||||
|
{ MP_QSTR_to_bitmap, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
|
||||||
|
{ MP_QSTR_copy, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
|
||||||
|
};
|
||||||
|
|
||||||
list_t arg_thresholds;
|
// Parse args.
|
||||||
list_init(&arg_thresholds, sizeof(color_thresholds_list_lnk_data_t));
|
image_t *image = py_helper_arg_to_image(pos_args[0], ARG_IMAGE_MUTABLE);
|
||||||
py_helper_arg_to_thresholds(args[1], &arg_thresholds);
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||||
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||||
|
|
||||||
bool arg_invert =
|
if (args[ARG_to_bitmap].u_int && (image->pixfmt != PIXFORMAT_BINARY) &&
|
||||||
py_helper_keyword_int(n_args, args, 2, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_invert), false);
|
(args[ARG_zero].u_int || (args[ARG_mask].u_obj != mp_const_none))) {
|
||||||
bool arg_zero =
|
mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Incompatible arguments!"));
|
||||||
py_helper_keyword_int(n_args, args, 3, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_zero), false);
|
}
|
||||||
image_t *arg_msk =
|
|
||||||
py_helper_keyword_to_image(n_args, args, 4, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_mask), NULL);
|
|
||||||
bool arg_to_bitmap =
|
|
||||||
py_helper_keyword_int(n_args, args, 5, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_to_bitmap), false);
|
|
||||||
bool arg_copy =
|
|
||||||
py_helper_keyword_int(n_args, args, 6, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_copy), false);
|
|
||||||
|
|
||||||
if (arg_to_bitmap && (!arg_copy)) {
|
if (args[ARG_to_bitmap].u_int && (!args[ARG_copy].u_int)) {
|
||||||
switch (arg_img->pixfmt) {
|
switch (image->pixfmt) {
|
||||||
case PIXFORMAT_GRAYSCALE: {
|
case PIXFORMAT_GRAYSCALE: {
|
||||||
PY_ASSERT_TRUE_MSG((arg_img->w >= (sizeof(uint32_t) / sizeof(uint8_t))),
|
PY_ASSERT_TRUE_MSG((image->w >= 4), "Can't convert to bitmap in place!");
|
||||||
"Can't convert to bitmap in place!");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PIXFORMAT_RGB565: {
|
case PIXFORMAT_RGB565: {
|
||||||
PY_ASSERT_TRUE_MSG((arg_img->w >= (sizeof(uint32_t) / sizeof(uint16_t))),
|
PY_ASSERT_TRUE_MSG((image->w >= 2), "Can't convert to bitmap in place!");
|
||||||
"Can't convert to bitmap in place!");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
@ -1950,26 +1951,35 @@ STATIC mp_obj_t py_image_binary(uint n_args, const mp_obj_t *args, mp_map_t *kw_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
list_t thresholds;
|
||||||
|
list_init(&thresholds, sizeof(color_thresholds_list_lnk_data_t));
|
||||||
|
py_helper_arg_to_thresholds(args[ARG_thresholds].u_obj, &thresholds);
|
||||||
|
|
||||||
image_t out;
|
image_t out;
|
||||||
out.w = arg_img->w;
|
out.w = image->w;
|
||||||
out.h = arg_img->h;
|
out.h = image->h;
|
||||||
out.pixfmt = arg_to_bitmap ? PIXFORMAT_BINARY : arg_img->pixfmt;
|
out.pixfmt = args[ARG_to_bitmap].u_int ? PIXFORMAT_BINARY : image->pixfmt;
|
||||||
out.pixels = arg_copy ? xalloc(image_size(&out)) : arg_img->pixels;
|
out.data = args[ARG_copy].u_int ? xalloc(image_size(&out)) : image->pixels;
|
||||||
|
|
||||||
fb_alloc_mark();
|
fb_alloc_mark();
|
||||||
imlib_binary(&out, arg_img, &arg_thresholds, arg_invert, arg_zero, arg_msk);
|
image_t *mask = NULL;
|
||||||
|
if (args[ARG_mask].u_obj != mp_const_none) {
|
||||||
|
mask = py_helper_arg_to_image(args[ARG_mask].u_obj, ARG_IMAGE_MUTABLE | ARG_IMAGE_ALLOC);
|
||||||
|
}
|
||||||
|
|
||||||
|
imlib_binary(&out, image, &thresholds, args[ARG_invert].u_int, args[ARG_zero].u_int, mask);
|
||||||
fb_alloc_free_till_mark();
|
fb_alloc_free_till_mark();
|
||||||
|
|
||||||
list_free(&arg_thresholds);
|
list_free(&thresholds);
|
||||||
|
|
||||||
if (arg_to_bitmap && (!arg_copy)) {
|
if (args[ARG_to_bitmap].u_int && (!args[ARG_copy].u_int)) {
|
||||||
arg_img->pixfmt = PIXFORMAT_BINARY;
|
image->pixfmt = PIXFORMAT_BINARY;
|
||||||
py_helper_update_framebuffer(&out);
|
py_helper_update_framebuffer(&out);
|
||||||
}
|
}
|
||||||
|
|
||||||
return py_image_from_struct(&out);
|
return py_image_from_struct(&out);
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_binary_obj, 2, py_image_binary);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_binary_obj, 1, py_image_binary);
|
||||||
|
|
||||||
STATIC mp_obj_t py_image_invert(mp_obj_t img_obj) {
|
STATIC mp_obj_t py_image_invert(mp_obj_t img_obj) {
|
||||||
imlib_invert(py_helper_arg_to_image(img_obj, ARG_IMAGE_MUTABLE));
|
imlib_invert(py_helper_arg_to_image(img_obj, ARG_IMAGE_MUTABLE));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user