Implement set_pixel

This commit is contained in:
iabdalkader 2015-08-20 16:25:37 +02:00
parent 6f531214c2
commit d031ea87f1
2 changed files with 59 additions and 1 deletions

View File

@ -926,9 +926,38 @@ static mp_obj_t py_image_get_pixel(mp_obj_t image_obj, mp_obj_t x_obj, mp_obj_t
return ret_obj;
}
#define RGB565(r, g, b)\
(uint32_t)(((r&0x1F)<<3)|((g&0x3F)>>3)|(g<<13)|((b&0x1F)<<8))
static mp_obj_t py_image_set_pixel(uint n_args, const mp_obj_t *args)
{
//TODO implement
// read args
int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]);
image_t *image = py_image_cobj(args[0]);
// check x, y, format
PY_ASSERT_TRUE_MSG(x>=0 && x<image->w, "image index out of range");
PY_ASSERT_TRUE_MSG(y>=0 && y<image->h, "image index out of range");
PY_ASSERT_TRUE_MSG(image->bpp <= 2, "Operation not supported on JPEG");
switch (image->bpp) {
case 1:
image->pixels[y*image->w+x] = mp_obj_get_int(args[3]);
break;
case 2: {
mp_obj_t *color_obj;
uint16_t *pixels = (uint16_t*)image->pixels;
mp_obj_get_array_fixed_n(args[3], 3, &color_obj);
pixels[y*image->w+x] = RGB565(mp_obj_get_int(color_obj[0]),
mp_obj_get_int(color_obj[1]),
mp_obj_get_int(color_obj[2]));
break;
}
default:
// shouldn't happen
break;
}
return mp_const_none;
}

29
usr/examples/set_pixel.py Normal file
View File

@ -0,0 +1,29 @@
import sensor, time
sensor.reset()
# Set sensor settings
sensor.set_brightness(0)
sensor.set_saturation(0)
sensor.set_gainceiling(8)
sensor.set_contrast(2)
# Set sensor pixel format
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
# Capture image and set pixels
image = sensor.snapshot()
for y in range(0, 240):
for x in range(0, 320):
image.set_pixel(x, y, 0xFF)
time.sleep(1000)
# Switch to RGB565
sensor.set_pixformat(sensor.RGB565)
# Capture image and set pixels
image = sensor.snapshot()
for y in range(0, 240):
for x in range(0, 320):
image.set_pixel(x, y, (0xFF, 0x00, 0x00))