mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
commit
4992789c1b
@ -18,6 +18,7 @@
|
||||
#include "mdefs.h"
|
||||
#include "font.h"
|
||||
#include "fb_alloc.h"
|
||||
#include "xalloc.h"
|
||||
|
||||
// Gamma uncompress
|
||||
extern const float xyz_table[256];
|
||||
@ -289,6 +290,32 @@ void imlib_save_image(image_t *img, const char *path, rectangle_t *roi)
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_copy_image(image_t *dst, image_t *src, rectangle_t *roi)
|
||||
{
|
||||
if (IM_IS_JPEG(src)) {
|
||||
dst->w = src->w;
|
||||
dst->h = src->h;
|
||||
dst->bpp = src->bpp;
|
||||
dst->pixels = xalloc(src->bpp);
|
||||
memcpy(dst->pixels, src->pixels, src->bpp);
|
||||
} else {
|
||||
rectangle_t rect;
|
||||
if (!rectangle_subimg(src, roi, &rect)) ff_no_intersection(NULL);
|
||||
dst->w = rect.w;
|
||||
dst->h = rect.h;
|
||||
dst->bpp = src->bpp;
|
||||
dst->pixels = xalloc(rect.w * rect.h * src->bpp);
|
||||
uint8_t *dst_pointer = dst->pixels;
|
||||
for (int i = rect.y; i < (rect.y + rect.h); i++) {
|
||||
int length = rect.w * src->bpp;
|
||||
memcpy(dst_pointer,
|
||||
src->pixels + (rect.x * src->bpp) + (i * src->w * src->bpp),
|
||||
length);
|
||||
dst_pointer += length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Get pixel (handles boundary check and image type check).
|
||||
@ -713,18 +740,54 @@ void imlib_difference(image_t *img, const char *path, image_t *other)
|
||||
imlib_image_operation(img, path, other, imlib_difference_line_op);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void imlib_rainbow(image_t *src, image_t *dst)
|
||||
static void imlib_replace_line_op(image_t *img, int line, uint8_t *other)
|
||||
{
|
||||
uint8_t *srcp = src->pixels;
|
||||
uint16_t *dstp = (uint16_t*)dst->pixels;
|
||||
|
||||
for (int i=0; i<(src->w*src->h); i++) {
|
||||
dstp[i] = rainbow_table[srcp[i]];
|
||||
if (IM_IS_GS(img)) {
|
||||
uint8_t *pixels = img->pixels + (img->w * line);
|
||||
memcpy(pixels, other, img->w * sizeof(uint8_t));
|
||||
} else {
|
||||
uint16_t *pixels = ((uint16_t *) img->pixels) + (img->w * line);
|
||||
memcpy(pixels, other, img->w * sizeof(uint16_t));
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_replace(image_t *img, const char *path, image_t *other)
|
||||
{
|
||||
imlib_image_operation(img, path, other, imlib_replace_line_op);
|
||||
}
|
||||
|
||||
static uint32_t alpha_temp;
|
||||
|
||||
static void imlib_blend_line_op(image_t *img, int line, uint8_t *other)
|
||||
{
|
||||
if (IM_IS_GS(img)) {
|
||||
uint8_t *pixels = img->pixels + (img->w * line);
|
||||
for (int i=0; i<img->w; i++) {
|
||||
pixels[i] = __SMUAD(alpha_temp,__PKHBT(pixels[i],other[i],16))>>8;
|
||||
}
|
||||
} else {
|
||||
uint16_t *pixels = ((uint16_t *) img->pixels) + (img->w * line);
|
||||
for (int i=0; i<img->w; i++) {
|
||||
const int pixel = pixels[i], other_pixel = ((uint16_t *) other)[i];
|
||||
uint32_t vr = __PKHBT(IM_R565(pixel), IM_R565(other_pixel), 16);
|
||||
uint32_t vg = __PKHBT(IM_G565(pixel), IM_G565(other_pixel), 16);
|
||||
uint32_t vb = __PKHBT(IM_B565(pixel), IM_B565(other_pixel), 16);
|
||||
uint32_t r = __SMUAD(alpha_temp, vr)>>8;
|
||||
uint32_t g = __SMUAD(alpha_temp, vg)>>8;
|
||||
uint32_t b = __SMUAD(alpha_temp, vb)>>8;
|
||||
pixels[i] = IM_RGB565(r, g, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_blend(image_t *img, const char *path, image_t *other, int alpha)
|
||||
{
|
||||
alpha_temp = __PKHBT((256-alpha), alpha, 16);
|
||||
imlib_image_operation(img, path, other, imlib_blend_line_op);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
int imlib_image_mean(struct image *src)
|
||||
{
|
||||
int s=0;
|
||||
@ -741,239 +804,6 @@ int imlib_image_mean(struct image *src)
|
||||
return s/n;
|
||||
}
|
||||
|
||||
void imlib_subimage(struct image *src_img, struct image *dst_img, int x_off, int y_off)
|
||||
{
|
||||
int x, y;
|
||||
typeof(*src_img->data) *src = src_img->data;
|
||||
typeof(*dst_img->data) *dst = dst_img->data;
|
||||
|
||||
for (y=y_off; y<dst_img->h+y_off; y++) {
|
||||
for (x=x_off; x<dst_img->w+x_off; x++) {
|
||||
*dst++ = src[y*src_img->w+x];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_blit_bytes(struct image *src, struct image *dst, int x_off, int y_off)
|
||||
{
|
||||
int x, y;
|
||||
typeof(*src->data) *srcp = src->data;
|
||||
typeof(*dst->data) *dstp = dst->data;
|
||||
|
||||
for (y=y_off; y<src->h+y_off; y++) {
|
||||
for (x=x_off; x<src->w+x_off; x++) {
|
||||
dstp[y*dst->w+x]=*srcp++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_blit_gs_to_rgb(struct image *src, struct image *dst, int x_off, int y_off)
|
||||
{
|
||||
int x, y;
|
||||
uint8_t *srcp = src->data;
|
||||
uint16_t *dstp = (uint16_t*) dst->data;
|
||||
|
||||
for (y=y_off; y<src->h+y_off; y++) {
|
||||
for (x=x_off; x<src->w+x_off; x++) {
|
||||
uint8_t p =*srcp++;
|
||||
dstp[y*dst->w+x]= ((uint16_t)p<<8)|p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_blit(struct image *src, struct image *dst, int x_off, int y_off)
|
||||
{
|
||||
if (src->bpp == dst->bpp) {
|
||||
imlib_blit_bytes(src, dst, x_off, y_off);
|
||||
} else if (src->bpp == 1 && dst->bpp == 2) {
|
||||
imlib_blit_gs_to_rgb(src, dst, x_off, y_off);
|
||||
} else if (src->bpp == 2 && dst->bpp == 1) {
|
||||
//imlib_blit_rgb_to_gs(src, dst, x_off, y_off);
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_blend(struct image *src, struct image *dst, int x_off, int y_off, uint8_t alpha)
|
||||
{
|
||||
uint16_t i,r, g, b;
|
||||
|
||||
uint16_t spix, dpix;
|
||||
uint16_t *srcp = (uint16_t *)src->pixels;
|
||||
uint16_t *dstp = (uint16_t *)dst->pixels;
|
||||
|
||||
uint32_t v0, vr, vg, vb;
|
||||
v0 = __PKHBT((256-alpha), alpha, 16);
|
||||
for (int y=y_off; y<src->h+y_off; y++) {
|
||||
i=y*dst->w;
|
||||
for (int x=x_off; x<src->w+x_off; x++) {
|
||||
spix = *srcp++;
|
||||
dpix = dstp[i+x];
|
||||
vr = __PKHBT(IM_R565(dpix), IM_R565(spix), 16);
|
||||
vg = __PKHBT(IM_G565(dpix), IM_G565(spix), 16);
|
||||
vb = __PKHBT(IM_B565(dpix), IM_B565(spix), 16);
|
||||
r = __SMUAD(v0, vr)>>8;
|
||||
g = __SMUAD(v0, vg)>>8;
|
||||
b = __SMUAD(v0, vb)>>8;
|
||||
dstp[i+x]= IM_RGB565(r, g, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_scale_nearest(struct image *src, struct image *dst)
|
||||
{
|
||||
int x, y, i, j;
|
||||
int w1 = src->w;
|
||||
int h1 = src->h;
|
||||
int w2 = dst->w;
|
||||
int h2 = dst->h;
|
||||
|
||||
int rat = 0;
|
||||
if (src->bpp ==1) {
|
||||
uint8_t *t, *p;
|
||||
uint8_t *src_data = src->pixels;
|
||||
uint8_t *dst_data = dst->pixels;
|
||||
int x_ratio = (int)((w1<<16)/w2) +1;
|
||||
int y_ratio = (int)((h1<<16)/h2) +1;
|
||||
|
||||
for (i=0; i<h2; i++) {
|
||||
t = dst_data + i*w2;
|
||||
y = ((i*y_ratio)>>16);
|
||||
p = src_data + y*w1;
|
||||
rat = 0;
|
||||
for (j=0; j<w2; j++) {
|
||||
x = (rat>>16);
|
||||
*t++ = p[x];
|
||||
rat += x_ratio;
|
||||
}
|
||||
}
|
||||
} else if (src->bpp==2) {
|
||||
uint16_t *t, *p;
|
||||
uint16_t *src_data = (uint16_t *)src->pixels;
|
||||
uint16_t *dst_data = (uint16_t *)dst->pixels;
|
||||
int x_ratio = (int)((w1<<16)/w2) +1;
|
||||
int y_ratio = (int)((h1<<16)/h2) +1;
|
||||
|
||||
for (i=0; i<h2; i++) {
|
||||
t = dst_data + i*w2;
|
||||
y = ((i*y_ratio)>>16);
|
||||
p = src_data + y*w1;
|
||||
rat = 0;
|
||||
for (j=0; j<w2; j++) {
|
||||
x = (rat>>16);
|
||||
*t++ = p[x];
|
||||
rat += x_ratio;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_scale_bilinear(struct image *src, struct image *dst)
|
||||
{
|
||||
int w1 = src->w;
|
||||
int h1 = src->h;
|
||||
int w2 = dst->w;
|
||||
int h2 = dst->h;
|
||||
|
||||
int offset = 0 ;
|
||||
int x, y, index;
|
||||
|
||||
int r, g, b;
|
||||
uint16_t A, B, C, D;
|
||||
|
||||
float x_diff, y_diff;
|
||||
float x_ratio = ((float)(w1-1))/w2 ;
|
||||
float y_ratio = ((float)(h1-1))/h2 ;
|
||||
uint16_t *srcp = (uint16_t *)src->pixels;
|
||||
uint16_t *dstp = (uint16_t *)dst->pixels;
|
||||
|
||||
for (int i=0;i<h2;i++) {
|
||||
for (int j=0;j<w2;j++) {
|
||||
x = (int)(x_ratio * j) ;
|
||||
y = (int)(y_ratio * i) ;
|
||||
x_diff = (x_ratio * j) - x ;
|
||||
y_diff = (y_ratio * i) - y ;
|
||||
index = y*w1+x ;
|
||||
|
||||
A = srcp[index];
|
||||
B = srcp[index+1];
|
||||
C = srcp[index+w1];
|
||||
D = srcp[index+w1+1];
|
||||
|
||||
// Yb = Ar(1-w)(1-h) + Br(w)(1-h) + Cr(h)(1-w) + Dr(wh)
|
||||
r = (int)(IM_R565(A)*(1-x_diff)*(1-y_diff) + IM_R565(B)*(x_diff)*(1-y_diff) +
|
||||
IM_R565(C)*(y_diff)*(1-x_diff) + IM_R565(D)*(x_diff*y_diff));
|
||||
|
||||
// Yb = Ag(1-w)(1-h) + Bg(w)(1-h) + Cg(h)(1-w) + Dg(wh)
|
||||
g = (int)(IM_G565(A)*(1-x_diff)*(1-y_diff) + IM_G565(B)*(x_diff)*(1-y_diff) +
|
||||
IM_G565(C)*(y_diff)*(1-x_diff) + IM_G565(D)*(x_diff*y_diff));
|
||||
|
||||
// Yb = Ab(1-w)(1-h) + Bb(w)(1-h) + Cb(h)(1-w) + Db(wh)
|
||||
b =(int)(IM_B565(A)*(1-x_diff)*(1-y_diff) + IM_B565(B)*(x_diff)*(1-y_diff) +
|
||||
IM_B565(C)*(y_diff)*(1-x_diff) + IM_B565(D)*(x_diff*y_diff));
|
||||
|
||||
dstp[offset++] = IM_RGB565(r, g, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_scale_bilinear_gray(struct image *src, struct image *dst)
|
||||
{
|
||||
int w1 = src->w;
|
||||
int h1 = src->h;
|
||||
int w2 = dst->w;
|
||||
int h2 = dst->h;
|
||||
|
||||
int offset = 0 ;
|
||||
int A, B, C, D, x, y, index, gray ;
|
||||
|
||||
float x_diff, y_diff;
|
||||
float x_ratio = ((float)(w1-1))/w2 ;
|
||||
float y_ratio = ((float)(h1-1))/h2 ;
|
||||
|
||||
uint8_t *srcp = src->pixels;
|
||||
uint8_t *dstp = dst->pixels;
|
||||
|
||||
for (int i=0;i<h2;i++) {
|
||||
for (int j=0;j<w2;j++) {
|
||||
x = (int)(x_ratio * j) ;
|
||||
y = (int)(y_ratio * i) ;
|
||||
x_diff = (x_ratio * j) - x ;
|
||||
y_diff = (y_ratio * i) - y ;
|
||||
|
||||
index = y*w1+x;
|
||||
A = srcp[index];
|
||||
B = srcp[index+1];
|
||||
C = srcp[index+w1];
|
||||
D = srcp[index+w1+1];
|
||||
|
||||
// Y = A(1-w)(1-h) + B(w)(1-h) + C(h)(1-w) + Dwh
|
||||
gray = (int)(A*(1-x_diff)*(1-y_diff) + B*(x_diff)*(1-y_diff) +
|
||||
C*(y_diff)*(1-x_diff) + D*(x_diff*y_diff));
|
||||
|
||||
dstp[offset++] = gray ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_scale(struct image *src, struct image *dst, interp_t interp)
|
||||
{
|
||||
switch (interp) {
|
||||
case INTERP_NEAREST:
|
||||
imlib_scale_nearest(src, dst);
|
||||
break;
|
||||
case INTERP_BILINEAR:
|
||||
if (src->bpp==2) {
|
||||
imlib_scale_bilinear(src, dst);
|
||||
} else {
|
||||
imlib_scale_bilinear_gray(src, dst);
|
||||
}
|
||||
break;
|
||||
case INTERP_BICUBIC:
|
||||
//NOT implemented
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void imlib_histeq(struct image *src)
|
||||
{
|
||||
int i, sum;
|
||||
|
||||
@ -405,6 +405,7 @@ void jpeg_write(image_t *img, const char *path);
|
||||
void imlib_image_operation(image_t *img, const char *path, image_t *other, line_op_t op);
|
||||
void imlib_load_image(image_t *img, const char *path);
|
||||
void imlib_save_image(image_t *img, const char *path, rectangle_t *roi);
|
||||
void imlib_copy_image(image_t *dst, image_t *src, rectangle_t *roi);
|
||||
|
||||
/* GIF functions */
|
||||
void gif_open(FIL *fp, int width, int height, bool color, bool loop);
|
||||
@ -455,6 +456,8 @@ void imlib_dilate(image_t *img, int ksize, int threshold);
|
||||
/* Background Subtraction (Frame Differencing) functions */
|
||||
void imlib_negate(image_t *img);
|
||||
void imlib_difference(image_t *img, const char *path, image_t *other);
|
||||
void imlib_replace(image_t *img, const char *path, image_t *other);
|
||||
void imlib_blend(image_t *img, const char *path, image_t *other, int alpha);
|
||||
|
||||
/* Image Morphing */
|
||||
void imlib_morph(image_t *img, const int ksize, const int8_t *krn, const float m, const int b);
|
||||
@ -483,7 +486,6 @@ array_t *cluster_kmeans(array_t *points, int k);
|
||||
/* Image filtering functions */
|
||||
int imlib_image_mean(struct image *src);
|
||||
void imlib_histeq(struct image *src);
|
||||
void imlib_rainbow(image_t *src, struct image *dst);
|
||||
|
||||
/* Integral image functions */
|
||||
void imlib_integral_image_alloc(struct integral_image *sum, int w, int h);
|
||||
@ -530,10 +532,6 @@ int imlib_lbp_desc_load(FIL *fp, uint8_t **desc);
|
||||
void imlib_find_iris(image_t *src, point_t *iris, rectangle_t *roi);
|
||||
|
||||
/* Misc */
|
||||
void imlib_scale(struct image *src, struct image *dst, interp_t interp);
|
||||
void imlib_blit(struct image *src, struct image *dst, int x_off, int y_off);
|
||||
void imlib_blend(struct image *src, struct image *dst, int x_off, int y_off, uint8_t alpha);
|
||||
void imlib_subimage(struct image *src, struct image *dst, int x_off, int y_off);
|
||||
void jpeg_compress(image_t *src, image_t *dst, int quality);
|
||||
|
||||
// Image filter functions
|
||||
|
||||
@ -156,6 +156,20 @@ static mp_obj_t py_image_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t va
|
||||
}
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_copy(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
|
||||
{
|
||||
image_t *arg_img = py_image_cobj(args[0]);
|
||||
|
||||
rectangle_t roi;
|
||||
py_helper_lookup_rectangle(kw_args, arg_img, &roi);
|
||||
|
||||
mp_obj_t img_obj = py_image(0, 0, 0, 0);
|
||||
image_t *img = py_image_cobj(img_obj);
|
||||
|
||||
imlib_copy_image(img, arg_img, &roi);
|
||||
return img_obj;
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_save(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
|
||||
{
|
||||
image_t *arg_img = py_image_cobj(args[0]);
|
||||
@ -571,6 +585,39 @@ static mp_obj_t py_image_difference(mp_obj_t img_obj, mp_obj_t other_obj)
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_replace(mp_obj_t img_obj, mp_obj_t other_obj)
|
||||
{
|
||||
image_t *arg_img = py_image_cobj(img_obj);
|
||||
PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img),
|
||||
"Operation not supported on JPEG");
|
||||
|
||||
if (MP_OBJ_IS_STR(other_obj)) {
|
||||
imlib_replace(arg_img, mp_obj_str_get_str(other_obj), NULL);
|
||||
} else {
|
||||
image_t *arg_other = py_image_cobj(other_obj);
|
||||
imlib_replace(arg_img, NULL, arg_other);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_blend(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
|
||||
{
|
||||
image_t *arg_img = py_image_cobj(args[0]);
|
||||
PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img),
|
||||
"Operation not supported on JPEG");
|
||||
|
||||
int alpha = IM_MIN(IM_MAX(py_helper_lookup_int(kw_args,
|
||||
MP_OBJ_NEW_QSTR(MP_QSTR_alpha), 128), 0), 256);
|
||||
|
||||
if (MP_OBJ_IS_STR(args[1])) {
|
||||
imlib_blend(arg_img, mp_obj_str_get_str(args[1]), NULL, alpha);
|
||||
} else {
|
||||
image_t *arg_other = py_image_cobj(args[1]);
|
||||
imlib_blend(arg_img, NULL, arg_other, alpha);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_morph(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
|
||||
{
|
||||
image_t *arg_img = py_image_cobj(args[0]);
|
||||
@ -866,142 +913,6 @@ static mp_obj_t py_image_find_markers(uint n_args, const mp_obj_t *args, mp_map_
|
||||
return objects_list;
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_scale(mp_obj_t image_obj, mp_obj_t size_obj)
|
||||
{
|
||||
int w,h;
|
||||
mp_obj_t *array;
|
||||
image_t *src_image = NULL;
|
||||
|
||||
/* get C image pointer */
|
||||
src_image = py_image_cobj(image_obj);
|
||||
|
||||
/* get x,y */
|
||||
mp_obj_get_array_fixed_n(size_obj, 2, &array);
|
||||
w = mp_obj_get_int(array[0]);
|
||||
h = mp_obj_get_int(array[1]);
|
||||
|
||||
image_t dst_image = {
|
||||
.w=w,
|
||||
.h=h,
|
||||
.bpp=src_image->bpp,
|
||||
.pixels=xalloc(w*h*src_image->bpp)
|
||||
};
|
||||
|
||||
imlib_scale(src_image, &dst_image, INTERP_BILINEAR);
|
||||
|
||||
*src_image = dst_image;
|
||||
return image_obj;
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_scaled(mp_obj_t image_obj, mp_obj_t size_obj)
|
||||
{
|
||||
int w,h;
|
||||
mp_obj_t *array;
|
||||
image_t *src_image = NULL;
|
||||
|
||||
/* get C image pointer */
|
||||
src_image = py_image_cobj(image_obj);
|
||||
|
||||
/* get x,y */
|
||||
mp_obj_get_array_fixed_n(size_obj, 2, &array);
|
||||
w = mp_obj_get_int(array[0]);
|
||||
h = mp_obj_get_int(array[1]);
|
||||
|
||||
image_t dst_image = {
|
||||
.w=w,
|
||||
.h=h,
|
||||
.bpp=src_image->bpp,
|
||||
.pixels=xalloc(w*h*src_image->bpp)
|
||||
};
|
||||
|
||||
imlib_scale(src_image, &dst_image, INTERP_NEAREST);
|
||||
|
||||
return py_image_from_struct(&dst_image);
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_subimg(mp_obj_t image_obj, mp_obj_t subimg_obj)
|
||||
{
|
||||
rectangle_t r;
|
||||
image_t *image;
|
||||
mp_obj_t *array;
|
||||
|
||||
/* image pointer */
|
||||
image = py_image_cobj(image_obj);
|
||||
|
||||
/* sub image */
|
||||
mp_obj_get_array_fixed_n(subimg_obj, 4, &array);
|
||||
r.x = mp_obj_get_int(array[0]);
|
||||
r.y = mp_obj_get_int(array[1]);
|
||||
r.w = mp_obj_get_int(array[2]);
|
||||
r.h = mp_obj_get_int(array[3]);
|
||||
|
||||
image_t subimg = {
|
||||
.w=r.w,
|
||||
.h=r.h,
|
||||
.bpp=image->bpp,
|
||||
.pixels=xalloc(r.w*r.h*image->bpp)
|
||||
};
|
||||
|
||||
imlib_subimage(image, &subimg, r.x, r.y);
|
||||
|
||||
return py_image_from_struct(&subimg);
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_blit(mp_obj_t dst_image_obj, mp_obj_t src_image_obj, mp_obj_t offset_obj)
|
||||
{
|
||||
int x,y;
|
||||
image_t *src_image = NULL;
|
||||
image_t *dst_image = NULL;
|
||||
|
||||
/* get C image pointer */
|
||||
src_image = py_image_cobj(src_image_obj);
|
||||
dst_image = py_image_cobj(dst_image_obj);
|
||||
|
||||
/* get x,y */
|
||||
mp_obj_t *array;
|
||||
mp_obj_get_array_fixed_n(offset_obj, 2, &array);
|
||||
x = mp_obj_get_int(array[0]);
|
||||
y = mp_obj_get_int(array[1]);
|
||||
|
||||
if ((src_image->w+x)>dst_image->w ||
|
||||
(src_image->h+y)>dst_image->h) {
|
||||
printf("src image > dst image\n");
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
imlib_blit(src_image, dst_image, x, y);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_blend(mp_obj_t dst_image_obj,
|
||||
mp_obj_t src_image_obj, mp_obj_t param_obj)
|
||||
{
|
||||
int x,y;
|
||||
float alpha;
|
||||
image_t *src_image = NULL;
|
||||
image_t *dst_image = NULL;
|
||||
|
||||
/* get C image pointer */
|
||||
src_image = py_image_cobj(src_image_obj);
|
||||
dst_image = py_image_cobj(dst_image_obj);
|
||||
|
||||
/* get x,y,alpha */
|
||||
mp_obj_t *array;
|
||||
mp_obj_get_array_fixed_n(param_obj, 3, &array);
|
||||
x = mp_obj_get_int(array[0]);
|
||||
y = mp_obj_get_int(array[1]);
|
||||
alpha = mp_obj_get_float(array[2]);
|
||||
|
||||
if ((src_image->w+x)>dst_image->w ||
|
||||
(src_image->h+y)>dst_image->h) {
|
||||
printf("src image > dst image\n");
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
imlib_blend(src_image, dst_image, x, y, (uint8_t)(alpha*256));
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_histeq(mp_obj_t image_obj)
|
||||
{
|
||||
struct image *image;
|
||||
@ -1016,29 +927,6 @@ static mp_obj_t py_image_histeq(mp_obj_t image_obj)
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_rainbow(mp_obj_t src_image_obj)
|
||||
{
|
||||
image_t *src_image = NULL;
|
||||
|
||||
/* get C image pointer */
|
||||
src_image = py_image_cobj(src_image_obj);
|
||||
|
||||
/* sanity checks */
|
||||
PY_ASSERT_TRUE_MSG(src_image->bpp == 1,
|
||||
"This function is only supported on GRAYSCALE images");
|
||||
|
||||
image_t dst_image = {
|
||||
.w=src_image->w,
|
||||
.h=src_image->h,
|
||||
.bpp=2,
|
||||
.pixels=xalloc(src_image->w*src_image->h*2)
|
||||
};
|
||||
|
||||
imlib_rainbow(src_image, &dst_image);
|
||||
*src_image = dst_image;
|
||||
return src_image_obj;
|
||||
}
|
||||
|
||||
static mp_obj_t py_image_compress(mp_obj_t image_obj, mp_obj_t quality)
|
||||
{
|
||||
image_t *image = py_image_cobj(image_obj);
|
||||
@ -1235,6 +1123,7 @@ static mp_obj_t py_image_find_eyes(mp_obj_t image_obj, mp_obj_t roi_obj)
|
||||
}
|
||||
|
||||
/* Image file functions */
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_copy_obj, 1, py_image_copy);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_save_obj, 2, py_image_save);
|
||||
/* Basic image functions */
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_image_width_obj, py_image_width);
|
||||
@ -1264,6 +1153,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_dilate_obj, 2, py_image_dilate);
|
||||
/* Background Subtraction (Frame Differencing) functions */
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_image_negate_obj, py_image_negate);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(py_image_difference_obj, py_image_difference);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(py_image_replace_obj, py_image_replace);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_blend_obj, 2, py_image_blend);
|
||||
/* Image Morphing */
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_morph_obj, 3, py_image_morph);
|
||||
/* Image Statistics */
|
||||
@ -1277,15 +1168,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_median_obj, 2, py_image_median);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_find_blobs_obj, 2, py_image_find_blobs);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_find_markers_obj, 2, py_image_find_markers);
|
||||
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(py_image_scale_obj, py_image_scale);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(py_image_scaled_obj, py_image_scaled);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(py_image_subimg_obj, py_image_subimg);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(py_image_blit_obj, py_image_blit);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(py_image_blend_obj, py_image_blend);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_image_histeq_obj, py_image_histeq);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_image_rainbow_obj, py_image_rainbow);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(py_image_compress_obj, py_image_compress);
|
||||
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(py_image_find_template_obj, py_image_find_template);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_find_features_obj, 2, py_image_find_features);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_find_keypoints_obj, 1, py_image_find_keypoints);
|
||||
@ -1294,6 +1178,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(py_image_find_eyes_obj, py_image_find_eyes);
|
||||
|
||||
static const mp_map_elem_t locals_dict_table[] = {
|
||||
/* Image file functions */
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&py_image_copy_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_save), (mp_obj_t)&py_image_save_obj},
|
||||
/* Basic image functions */
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_width), (mp_obj_t)&py_image_width_obj},
|
||||
@ -1323,6 +1208,8 @@ static const mp_map_elem_t locals_dict_table[] = {
|
||||
/* Background Subtraction (Frame Differencing) functions */
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_negate), (mp_obj_t)&py_image_negate_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_difference), (mp_obj_t)&py_image_difference_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_replace), (mp_obj_t)&py_image_replace_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_blend), (mp_obj_t)&py_image_blend_obj},
|
||||
/* Image Morphing */
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_morph), (mp_obj_t)&py_image_morph_obj},
|
||||
/* Image Statistics */
|
||||
@ -1336,13 +1223,7 @@ static const mp_map_elem_t locals_dict_table[] = {
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_find_blobs), (mp_obj_t)&py_image_find_blobs_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_find_markers), (mp_obj_t)&py_image_find_markers_obj},
|
||||
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_scale), (mp_obj_t)&py_image_scale_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_scaled), (mp_obj_t)&py_image_scaled_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_subimg), (mp_obj_t)&py_image_subimg_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_blit), (mp_obj_t)&py_image_blit_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_blend), (mp_obj_t)&py_image_blend_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_histeq), (mp_obj_t)&py_image_histeq_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_rainbow), (mp_obj_t)&py_image_rainbow_obj},
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_compress), (mp_obj_t)&py_image_compress_obj},
|
||||
/* objects/feature detection */
|
||||
{MP_OBJ_NEW_QSTR(MP_QSTR_find_template), (mp_obj_t)&py_image_find_template_obj},
|
||||
|
||||
@ -22,7 +22,7 @@ Q(save_descriptor)
|
||||
Q(match_descriptor)
|
||||
|
||||
// Image class
|
||||
Q(load)
|
||||
Q(copy)
|
||||
Q(save)
|
||||
Q(width)
|
||||
Q(height)
|
||||
@ -48,6 +48,8 @@ Q(erode)
|
||||
Q(dilate)
|
||||
Q(negate)
|
||||
Q(difference)
|
||||
Q(replace)
|
||||
Q(blend)
|
||||
Q(morph)
|
||||
Q(statistics)
|
||||
Q(midpoint)
|
||||
@ -59,12 +61,7 @@ Q(find_markers)
|
||||
Q(kp_desc)
|
||||
Q(lbp_desc)
|
||||
Q(Cascade)
|
||||
Q(blit)
|
||||
Q(blend)
|
||||
Q(scaled)
|
||||
Q(subimg)
|
||||
Q(compress)
|
||||
Q(rainbow)
|
||||
Q(histeq)
|
||||
Q(find_template)
|
||||
Q(find_features)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user