camera: Change API

This commit is contained in:
Kamil Tomaszewski 2020-09-11 13:27:11 +02:00
parent 143a1ff94a
commit c2fc592c2c
10 changed files with 247 additions and 336 deletions

View File

@ -35,9 +35,6 @@
#include "shared-bindings/camera/Camera.h" #include "shared-bindings/camera/Camera.h"
#define JPG_COMPRESS_RATIO (9)
#define SPRESENSE_CAMIMAGE_MEM_ALIGN (32)
typedef struct { typedef struct {
const char* devpath; const char* devpath;
int fd; int fd;
@ -45,39 +42,36 @@ typedef struct {
STATIC camera_dev_t camera_dev = {"/dev/video", -1}; STATIC camera_dev_t camera_dev = {"/dev/video", -1};
static void camera_size_to_width_and_height(camera_imagesize_t size, uint16_t *width, uint16_t *height) { static bool camera_check_width_and_height(uint16_t width, uint16_t height) {
switch (size) { if ((width == VIDEO_HSIZE_QVGA && height == VIDEO_VSIZE_QVGA) ||
case IMAGESIZE_320x240: (width == VIDEO_HSIZE_VGA && height == VIDEO_VSIZE_VGA) ||
*height = VIDEO_VSIZE_QVGA; (width == VIDEO_HSIZE_HD && height == VIDEO_VSIZE_HD) ||
*width = VIDEO_HSIZE_QVGA; (width == VIDEO_HSIZE_QUADVGA && height == VIDEO_VSIZE_QUADVGA) ||
break; (width == VIDEO_HSIZE_FULLHD && height == VIDEO_VSIZE_FULLHD) ||
case IMAGESIZE_640x320: (width == VIDEO_HSIZE_3M && height == VIDEO_VSIZE_3M) ||
*height = VIDEO_VSIZE_VGA; (width == VIDEO_HSIZE_5M && height == VIDEO_VSIZE_5M)) {
*width = VIDEO_HSIZE_VGA; return true;
break; } else {
case IMAGESIZE_1280x720: return false;
*height = VIDEO_VSIZE_HD; }
*width = VIDEO_HSIZE_HD; }
break;
case IMAGESIZE_1280x960: static bool camera_check_buffer_length(uint16_t width, uint16_t height, camera_imageformat_t format, size_t length) {
*height = VIDEO_VSIZE_QUADVGA; if (format == IMAGEFORMAT_JPG) {
*width = VIDEO_HSIZE_QUADVGA; // In SPRESENSE SDK, JPEG compression quality=80 by default.
break; // In such setting, the maximum actual measured size of JPEG image
case IMAGESIZE_1920x1080: // is about width * height * 2 / 9.
*height = VIDEO_VSIZE_FULLHD; return length >= (size_t)(width * height * 2 / 9) ? true : false;
*width = VIDEO_HSIZE_FULLHD; } else {
break; return false;
case IMAGESIZE_2048x1536: }
*height = VIDEO_VSIZE_3M; }
*width = VIDEO_HSIZE_3M;
break; static bool camera_check_format(camera_imageformat_t format) {
case IMAGESIZE_2560x1920: if (format == IMAGEFORMAT_JPG) {
*height = VIDEO_VSIZE_5M; return true;
*width = VIDEO_HSIZE_5M; } else {
break; return false;
default:
mp_raise_ValueError(translate("Size not supported"));
break;
} }
} }
@ -118,7 +112,9 @@ static void camera_start_preview() {
camera_start_streaming(V4L2_BUF_TYPE_VIDEO_CAPTURE); camera_start_streaming(V4L2_BUF_TYPE_VIDEO_CAPTURE);
} }
void common_hal_camera_construct(camera_obj_t *self, camera_imagesize_t size) { extern uint32_t _ebss;
extern uint32_t _stext;
void common_hal_camera_construct(camera_obj_t *self, uint16_t width, uint16_t height) {
if (camera_dev.fd < 0) { if (camera_dev.fd < 0) {
if (video_initialize(camera_dev.devpath) < 0) { if (video_initialize(camera_dev.devpath) < 0) {
mp_raise_ValueError(translate("Could not initialize Camera")); mp_raise_ValueError(translate("Could not initialize Camera"));
@ -129,25 +125,13 @@ void common_hal_camera_construct(camera_obj_t *self, camera_imagesize_t size) {
} }
} }
uint16_t width, height; if (!camera_check_width_and_height(width, height)) {
mp_raise_ValueError(translate("Size not supported"));
camera_size_to_width_and_height(size, &width, &height);
self->size = size;
// In SPRESENSE SDK, JPEG compression quality=80 by default.
// In such setting, the maximum actual measured size of JPEG image
// is about width * height * 2 / 9.
self->buffer_size = (size_t)(width * height * 2 / JPG_COMPRESS_RATIO);;
self->buffer = m_malloc(self->buffer_size, true);
if (self->buffer == NULL) {
mp_raise_msg(&mp_type_MemoryError, translate("Couldn't allocate picture buffer"));
}
self->picture_buffer = self->buffer;
while ((uint32_t)self->picture_buffer % SPRESENSE_CAMIMAGE_MEM_ALIGN != 0) {
self->picture_buffer++;
} }
self->width = width;
self->height = height;
camera_start_preview(); camera_start_preview();
camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, width, height); camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, width, height);
@ -164,8 +148,6 @@ void common_hal_camera_deinit(camera_obj_t *self) {
video_uninitialize(); video_uninitialize();
m_free(self->buffer);
close(camera_dev.fd); close(camera_dev.fd);
camera_dev.fd = -1; camera_dev.fd = -1;
} }
@ -174,14 +156,26 @@ bool common_hal_camera_deinited(camera_obj_t *self) {
return camera_dev.fd < 0; return camera_dev.fd < 0;
} }
void common_hal_camera_take_picture(camera_obj_t *self) { size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_t len, camera_imageformat_t format) {
if (!camera_check_width_and_height(self->width, self->height)) {
mp_raise_ValueError(translate("Size not supported"));
}
if (!camera_check_buffer_length(self->width, self->height, format, len)) {
mp_raise_ValueError(translate("Buffer is too small"));
}
if (!camera_check_format(format)) {
mp_raise_ValueError(translate("Format not supported"));
}
camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, self->width, self->height);
v4l2_buffer_t buf; v4l2_buffer_t buf;
memset(&buf, 0, sizeof(v4l2_buffer_t)); memset(&buf, 0, sizeof(v4l2_buffer_t));
buf.type = V4L2_BUF_TYPE_STILL_CAPTURE; buf.type = V4L2_BUF_TYPE_STILL_CAPTURE;
buf.memory = V4L2_MEMORY_USERPTR; buf.memory = V4L2_MEMORY_USERPTR;
buf.m.userptr = (unsigned long)self->picture_buffer; buf.m.userptr = (unsigned long)buffer;
buf.length = self->buffer_size - SPRESENSE_CAMIMAGE_MEM_ALIGN; buf.length = len;
ioctl(camera_dev.fd, VIDIOC_QBUF, (unsigned long)&buf); ioctl(camera_dev.fd, VIDIOC_QBUF, (unsigned long)&buf);
ioctl(camera_dev.fd, VIDIOC_TAKEPICT_START, 0); ioctl(camera_dev.fd, VIDIOC_TAKEPICT_START, 0);
@ -190,35 +184,21 @@ void common_hal_camera_take_picture(camera_obj_t *self) {
ioctl(camera_dev.fd, VIDIOC_TAKEPICT_STOP, false); ioctl(camera_dev.fd, VIDIOC_TAKEPICT_STOP, false);
self->picture_size = (size_t)buf.bytesused; return (size_t)buf.bytesused;
} }
uint8_t *common_hal_camera_get_picture_buffer(camera_obj_t *self) { uint16_t common_hal_camera_get_width(camera_obj_t *self) {
return self->picture_buffer; return self->width;
} }
size_t common_hal_camera_get_picture_size(camera_obj_t *self) { void common_hal_camera_set_width(camera_obj_t *self, uint16_t width) {
return self->picture_size; self->width = width;
} }
camera_imagesize_t common_hal_camera_get_size(camera_obj_t *self) { uint16_t common_hal_camera_get_height(camera_obj_t *self) {
return self->size; return self->height;
} }
void common_hal_camera_set_size(camera_obj_t *self, camera_imagesize_t size) { void common_hal_camera_set_height(camera_obj_t *self, uint16_t height) {
uint16_t width, height; self->height = height;
camera_size_to_width_and_height(size, &width, &height);
self->buffer_size = (size_t)(width * height * 2 / JPG_COMPRESS_RATIO);;
self->buffer = m_realloc(self->buffer, self->buffer_size);
if (self->buffer == NULL) {
mp_raise_msg(&mp_type_MemoryError, translate("Couldn't allocate picture buffer"));
}
self->picture_buffer = self->buffer;
while ((uint32_t)self->picture_buffer % SPRESENSE_CAMIMAGE_MEM_ALIGN != 0) {
self->picture_buffer++;
}
camera_set_format(V4L2_BUF_TYPE_STILL_CAPTURE, V4L2_PIX_FMT_JPEG, width, height);
} }

View File

@ -29,15 +29,10 @@
#include "py/obj.h" #include "py/obj.h"
#include "shared-bindings/camera/ImageSize.h"
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
uint8_t *buffer; uint16_t width;
size_t buffer_size; uint16_t height;
uint8_t *picture_buffer;
size_t picture_size;
camera_imagesize_t size;
} camera_obj_t; } camera_obj_t;
#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_CAMERA_CAMERA_H #endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_CAMERA_CAMERA_H

View File

@ -315,7 +315,6 @@ SRC_COMMON_HAL_ALL = \
busio/__init__.c \ busio/__init__.c \
camera/__init__.c \ camera/__init__.c \
camera/Camera.c \ camera/Camera.c \
camera/ImageSize.c \
countio/Counter.c \ countio/Counter.c \
countio/__init__.c \ countio/__init__.c \
digitalio/DigitalInOut.c \ digitalio/DigitalInOut.c \
@ -386,6 +385,7 @@ $(filter $(SRC_PATTERNS), \
_bleio/Attribute.c \ _bleio/Attribute.c \
_bleio/ScanEntry.c \ _bleio/ScanEntry.c \
_eve/__init__.c \ _eve/__init__.c \
camera/ImageFormat.c \
digitalio/Direction.c \ digitalio/Direction.c \
digitalio/DriveMode.c \ digitalio/DriveMode.c \
digitalio/Pull.c \ digitalio/Pull.c \

View File

@ -48,37 +48,38 @@
//| vfs = storage.VfsFat(sd) //| vfs = storage.VfsFat(sd)
//| storage.mount(vfs, '/sd') //| storage.mount(vfs, '/sd')
//| //|
//| cam = camera.Camera(camera.ImageSize.IMAGE_SIZE_1920x1080) //| cam = camera.Camera(1920, 1080)
//| //|
//| buffer = bytearray(512 * 1024)
//| file = open("/sd/image.jpg","wb") //| file = open("/sd/image.jpg","wb")
//| cam.take_picture() //| size = cam.take_picture()
//| file.write(cam.picture) //| file.write(buffer, size)
//| file.close()""" //| file.close()"""
//| //|
//| def __init__(self, ): //| def __init__(self, width: int, height: int) -> None:
//| """Initialize camera. //| """Initialize camera.
//| //|
//| :param camera.ImageSize size: image size""" //| :param int width: Width in pixels
//| :param int height: Height in pixels"""
//| ... //| ...
//| //|
STATIC mp_obj_t camera_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC mp_obj_t camera_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
camera_obj_t *self = m_new_obj(camera_obj_t); camera_obj_t *self = m_new_obj(camera_obj_t);
self->base.type = &camera_type; self->base.type = &camera_type;
enum { ARG_size }; enum { ARG_width, ARG_height };
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
{ MP_QSTR_size, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_width, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_height, MP_ARG_REQUIRED | MP_ARG_INT },
}; };
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
camera_imagesize_t size = camera_imagesize_obj_to_type(args[ARG_size].u_obj); common_hal_camera_construct(self, args[ARG_width].u_int, args[ARG_height].u_int);
common_hal_camera_construct(self, size);
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);
} }
//| def deinit(self, ) -> Any: //| def deinit(self) -> None:
//| """De-initialize camera.""" //| """De-initialize camera."""
//| ... //| ...
//| //|
@ -95,69 +96,84 @@ STATIC void check_for_deinit(camera_obj_t *self) {
} }
} }
//| def take_picture(self, ) -> Any: //| def take_picture(self, buf: WriteableBuffer, format: ImageFormat) -> int:
//| """Take picture.""" //| """Take picture and save to ``buf`` in the given ``format``
//|
//| :return: the size of the picture taken
//| :rtype: int"""
//| ... //| ...
//| //|
STATIC mp_obj_t camera_obj_take_picture(mp_obj_t self_in) { STATIC mp_obj_t camera_obj_take_picture(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_buffer, ARG_format };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_format, MP_ARG_REQUIRED | MP_ARG_OBJ },
};
camera_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
check_for_deinit(self);
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);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE);
camera_imageformat_t format = camera_imageformat_obj_to_type(args[ARG_format].u_obj);
return MP_OBJ_NEW_SMALL_INT(common_hal_camera_take_picture(self, (uint8_t *)bufinfo.buf, bufinfo.len, format));
}
MP_DEFINE_CONST_FUN_OBJ_KW(camera_take_picture_obj, 3, camera_obj_take_picture);
//| width: int
//| """Image width in pixels."""
//|
STATIC mp_obj_t camera_obj_get_width(mp_obj_t self_in) {
camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_camera_get_width(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(camera_get_width_obj, camera_obj_get_width);
STATIC mp_obj_t camera_obj_set_width(mp_obj_t self_in, mp_obj_t value) {
camera_obj_t *self = MP_OBJ_TO_PTR(self_in); camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self); check_for_deinit(self);
common_hal_camera_take_picture(self); common_hal_camera_set_width(self, mp_obj_get_int(value));
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_1(camera_take_picture_obj, camera_obj_take_picture); MP_DEFINE_CONST_FUN_OBJ_2(camera_set_width_obj, camera_obj_set_width);
//| picture: Any = ... const mp_obj_property_t camera_width_obj = {
//| """Image buffer."""
//|
STATIC mp_obj_t camera_obj_get_picture(mp_obj_t self_in) {
camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
uint8_t *buffer = common_hal_camera_get_picture_buffer(self);
size_t size = common_hal_camera_get_picture_size(self);
return mp_obj_new_bytearray_by_ref(size, buffer);
}
MP_DEFINE_CONST_FUN_OBJ_1(camera_get_picture_obj, camera_obj_get_picture);
const mp_obj_property_t camera_picture_obj = {
.base.type = &mp_type_property, .base.type = &mp_type_property,
.proxy = {(mp_obj_t)&camera_get_picture_obj, .proxy = {(mp_obj_t)&camera_get_width_obj,
(mp_obj_t)&mp_const_none_obj, (mp_obj_t)&camera_set_width_obj,
(mp_obj_t)&mp_const_none_obj}, (mp_obj_t)&mp_const_none_obj},
}; };
//| size: Any = ... //| height: int
//| """Image size.""" //| """Image height in pixels."""
//| //|
STATIC mp_obj_t camera_obj_get_size(mp_obj_t self_in) { STATIC mp_obj_t camera_obj_get_height(mp_obj_t self_in) {
camera_obj_t *self = MP_OBJ_TO_PTR(self_in); camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self); check_for_deinit(self);
return camera_imagesize_type_to_obj(common_hal_camera_get_size(self)); return MP_OBJ_NEW_SMALL_INT(common_hal_camera_get_height(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(camera_get_size_obj, camera_obj_get_size); MP_DEFINE_CONST_FUN_OBJ_1(camera_get_height_obj, camera_obj_get_height);
STATIC mp_obj_t camera_obj_set_size(mp_obj_t self_in, mp_obj_t value) { STATIC mp_obj_t camera_obj_set_height(mp_obj_t self_in, mp_obj_t value) {
camera_obj_t *self = MP_OBJ_TO_PTR(self_in); camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self); check_for_deinit(self);
camera_imagesize_t size = camera_imagesize_obj_to_type(value); common_hal_camera_set_height(self, mp_obj_get_int(value));
if (size == IMAGESIZE_NONE) {
mp_raise_ValueError(translate("Invalid image size."));
}
common_hal_camera_set_size(self, size);
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_2(camera_set_size_obj, camera_obj_set_size); MP_DEFINE_CONST_FUN_OBJ_2(camera_set_height_obj, camera_obj_set_height);
const mp_obj_property_t camera_size_obj = { const mp_obj_property_t camera_height_obj = {
.base.type = &mp_type_property, .base.type = &mp_type_property,
.proxy = {(mp_obj_t)&camera_get_size_obj, .proxy = {(mp_obj_t)&camera_get_height_obj,
(mp_obj_t)&camera_set_size_obj, (mp_obj_t)&camera_set_height_obj,
(mp_obj_t)&mp_const_none_obj}, (mp_obj_t)&mp_const_none_obj},
}; };
@ -165,14 +181,14 @@ STATIC const mp_rom_map_elem_t camera_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&camera_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&camera_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_take_picture), MP_ROM_PTR(&camera_take_picture_obj) }, { MP_ROM_QSTR(MP_QSTR_take_picture), MP_ROM_PTR(&camera_take_picture_obj) },
{ MP_ROM_QSTR(MP_QSTR_picture), MP_ROM_PTR(&camera_picture_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&camera_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&camera_size_obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&camera_height_obj) },
}; };
STATIC MP_DEFINE_CONST_DICT(camera_locals_dict, camera_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(camera_locals_dict, camera_locals_dict_table);
const mp_obj_type_t camera_type = { const mp_obj_type_t camera_type = {
{ &mp_type_type }, { &mp_type_type },
.name = MP_QSTR_GNSS, .name = MP_QSTR_Camera,
.make_new = camera_make_new, .make_new = camera_make_new,
.locals_dict = (mp_obj_dict_t*)&camera_locals_dict, .locals_dict = (mp_obj_dict_t*)&camera_locals_dict,
}; };

View File

@ -28,18 +28,18 @@
#define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H #define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H
#include "common-hal/camera/Camera.h" #include "common-hal/camera/Camera.h"
#include "shared-bindings/camera/ImageSize.h" #include "shared-bindings/camera/ImageFormat.h"
extern const mp_obj_type_t camera_type; extern const mp_obj_type_t camera_type;
void common_hal_camera_construct(camera_obj_t *self, camera_imagesize_t size); void common_hal_camera_construct(camera_obj_t *self, uint16_t width, uint16_t height);
void common_hal_camera_deinit(camera_obj_t *self); void common_hal_camera_deinit(camera_obj_t *self);
bool common_hal_camera_deinited(camera_obj_t *self); bool common_hal_camera_deinited(camera_obj_t *self);
void common_hal_camera_take_picture(camera_obj_t *self); size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_t len, camera_imageformat_t format);
uint8_t* common_hal_camera_get_picture_buffer(camera_obj_t *self); uint16_t common_hal_camera_get_width(camera_obj_t *self);
size_t common_hal_camera_get_picture_size(camera_obj_t *self); void common_hal_camera_set_width(camera_obj_t *self, uint16_t width);
camera_imagesize_t common_hal_camera_get_size(camera_obj_t *self); uint16_t common_hal_camera_get_height(camera_obj_t *self);
void common_hal_camera_set_size(camera_obj_t *self, camera_imagesize_t size); void common_hal_camera_set_height(camera_obj_t *self, uint16_t height);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H

View File

@ -0,0 +1,93 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright 2020 Sony Semiconductor Solutions Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-bindings/camera/ImageFormat.h"
//| class ImageFormat:
//| """Image format"""
//|
//| def __init__(self) -> None:
//| """Enum-like class to define the image format."""
//|
//| JPG: ImageFormat
//| """JPG format."""
//|
//| RGB565: ImageFormat
//| """RGB565 format."""
//|
const mp_obj_type_t camera_imageformat_type;
const camera_imageformat_obj_t camera_imageformat_jpg_obj = {
{ &camera_imageformat_type },
};
const camera_imageformat_obj_t camera_imageformat_rgb565_obj = {
{ &camera_imageformat_type },
};
camera_imageformat_t camera_imageformat_obj_to_type(mp_obj_t obj) {
if (obj == MP_ROM_PTR(&camera_imageformat_jpg_obj)) {
return IMAGEFORMAT_JPG;
} else if (obj == MP_ROM_PTR(&camera_imageformat_rgb565_obj)) {
return IMAGEFORMAT_RGB565;
}
return IMAGEFORMAT_NONE;
}
mp_obj_t camera_imageformat_type_to_obj(camera_imageformat_t format) {
switch (format) {
case IMAGEFORMAT_JPG:
return (mp_obj_t)MP_ROM_PTR(&camera_imageformat_jpg_obj);
case IMAGEFORMAT_RGB565:
return (mp_obj_t)MP_ROM_PTR(&camera_imageformat_rgb565_obj);
case IMAGEFORMAT_NONE:
default:
return (mp_obj_t)MP_ROM_PTR(&mp_const_none_obj);
}
}
STATIC const mp_rom_map_elem_t camera_imageformat_locals_dict_table[] = {
{MP_ROM_QSTR(MP_QSTR_JPG), MP_ROM_PTR(&camera_imageformat_jpg_obj)},
{MP_ROM_QSTR(MP_QSTR_RGB565), MP_ROM_PTR(&camera_imageformat_rgb565_obj)},
};
STATIC MP_DEFINE_CONST_DICT(camera_imageformat_locals_dict, camera_imageformat_locals_dict_table);
STATIC void camera_imageformat_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
qstr format = MP_QSTR_None;
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imageformat_jpg_obj)) {
format = MP_QSTR_JPG;
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imageformat_rgb565_obj)) {
format = MP_QSTR_RGB565;
}
mp_printf(print, "%q.%q.%q", MP_QSTR_camera, MP_QSTR_ImageSize, format);
}
const mp_obj_type_t camera_imageformat_type = {
{ &mp_type_type },
.name = MP_QSTR_ImageFormat,
.print = camera_imageformat_print,
.locals_dict = (mp_obj_t)&camera_imageformat_locals_dict,
};

View File

@ -24,36 +24,26 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGESIZE_H #ifndef MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGESIZE_H #define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H
#include "py/obj.h" #include "py/obj.h"
typedef enum { typedef enum {
IMAGESIZE_NONE, IMAGEFORMAT_NONE,
IMAGESIZE_320x240, IMAGEFORMAT_JPG,
IMAGESIZE_640x320, IMAGEFORMAT_RGB565,
IMAGESIZE_1280x720, } camera_imageformat_t;
IMAGESIZE_1280x960,
IMAGESIZE_1920x1080,
IMAGESIZE_2048x1536,
IMAGESIZE_2560x1920,
} camera_imagesize_t;
const mp_obj_type_t camera_imagesize_type; const mp_obj_type_t camera_imageformat_type;
camera_imagesize_t camera_imagesize_obj_to_type(mp_obj_t obj); camera_imageformat_t camera_imageformat_obj_to_type(mp_obj_t obj);
mp_obj_t camera_imagesize_type_to_obj(camera_imagesize_t mode); mp_obj_t camera_imageformat_type_to_obj(camera_imageformat_t mode);
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
} camera_imagesize_obj_t; } camera_imageformat_obj_t;
extern const camera_imagesize_obj_t camera_imagesize_320x240_obj; extern const camera_imageformat_obj_t camera_imageformat_jpg_obj;
extern const camera_imagesize_obj_t camera_imagesize_640x320_obj; extern const camera_imageformat_obj_t camera_imageformat_rgb565_obj;
extern const camera_imagesize_obj_t camera_imagesize_1280x720_obj;
extern const camera_imagesize_obj_t camera_imagesize_1280x960_obj;
extern const camera_imagesize_obj_t camera_imagesize_1920x1080_obj;
extern const camera_imagesize_obj_t camera_imagesize_2048x1536_obj;
extern const camera_imagesize_obj_t camera_imagesize_2560x1920_obj;
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGESIZE_H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H

View File

@ -1,163 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright 2020 Sony Semiconductor Solutions Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-bindings/camera/ImageSize.h"
//| class ImageSize:
//| """Image size"""
//|
//| def __init__(self) -> None:
//| """Enum-like class to define the image size."""
//|
//| IMAGE_SIZE_320x240: ImageSize
//| """Image size 320x240."""
//|
//| IMAGE_SIZE_640x480: ImageSize
//| """Image size 640x480."""
//|
//| IMAGE_SIZE_1280x720: ImageSize
//| """Image size 1280x720."""
//|
//| IMAGE_SIZE_1280x960: ImageSize
//| """Image size 1280x960."""
//|
//| IMAGE_SIZE_1920x1080: ImageSize
//| """Image size 1920x1080."""
//|
//| IMAGE_SIZE_2048x1536: ImageSize
//| """Image size 2048x1536."""
//|
//| IMAGE_SIZE_2560x1920: ImageSize
//| """Image size 2560x1920."""
//|
const mp_obj_type_t camera_imagesize_type;
const camera_imagesize_obj_t camera_imagesize_320x240_obj = {
{ &camera_imagesize_type },
};
const camera_imagesize_obj_t camera_imagesize_640x320_obj = {
{ &camera_imagesize_type },
};
const camera_imagesize_obj_t camera_imagesize_1280x720_obj = {
{ &camera_imagesize_type },
};
const camera_imagesize_obj_t camera_imagesize_1280x960_obj = {
{ &camera_imagesize_type },
};
const camera_imagesize_obj_t camera_imagesize_1920x1080_obj = {
{ &camera_imagesize_type },
};
const camera_imagesize_obj_t camera_imagesize_2048x1536_obj = {
{ &camera_imagesize_type },
};
const camera_imagesize_obj_t camera_imagesize_2560x1920_obj = {
{ &camera_imagesize_type },
};
camera_imagesize_t camera_imagesize_obj_to_type(mp_obj_t obj) {
if (obj == MP_ROM_PTR(&camera_imagesize_320x240_obj)) {
return IMAGESIZE_320x240;
} else if (obj == MP_ROM_PTR(&camera_imagesize_640x320_obj)) {
return IMAGESIZE_640x320;
} else if (obj == MP_ROM_PTR(&camera_imagesize_1280x720_obj)) {
return IMAGESIZE_1280x720;
} else if (obj == MP_ROM_PTR(&camera_imagesize_1280x960_obj)) {
return IMAGESIZE_1280x960;
} else if (obj == MP_ROM_PTR(&camera_imagesize_1920x1080_obj)) {
return IMAGESIZE_1920x1080;
} else if (obj == MP_ROM_PTR(&camera_imagesize_2048x1536_obj)) {
return IMAGESIZE_2048x1536;
} else if (obj == MP_ROM_PTR(&camera_imagesize_2560x1920_obj)) {
return IMAGESIZE_2560x1920;
}
return IMAGESIZE_NONE;
}
mp_obj_t camera_imagesize_type_to_obj(camera_imagesize_t size) {
switch (size) {
case IMAGESIZE_320x240:
return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_320x240_obj);
case IMAGESIZE_640x320:
return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_640x320_obj);
case IMAGESIZE_1280x720:
return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_1280x720_obj);
case IMAGESIZE_1280x960:
return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_1280x960_obj);
case IMAGESIZE_1920x1080:
return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_1920x1080_obj);
case IMAGESIZE_2048x1536:
return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_2048x1536_obj);
case IMAGESIZE_2560x1920:
return (mp_obj_t)MP_ROM_PTR(&camera_imagesize_2560x1920_obj);
case IMAGESIZE_NONE:
default:
return (mp_obj_t)MP_ROM_PTR(&mp_const_none_obj);
}
}
STATIC const mp_rom_map_elem_t camera_imagesize_locals_dict_table[] = {
{MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_320x240), MP_ROM_PTR(&camera_imagesize_320x240_obj)},
{MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_640x320), MP_ROM_PTR(&camera_imagesize_640x320_obj)},
{MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_1280x720), MP_ROM_PTR(&camera_imagesize_1280x720_obj)},
{MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_1280x960), MP_ROM_PTR(&camera_imagesize_1280x960_obj)},
{MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_1920x1080), MP_ROM_PTR(&camera_imagesize_1920x1080_obj)},
{MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_2048x1536), MP_ROM_PTR(&camera_imagesize_2048x1536_obj)},
{MP_ROM_QSTR(MP_QSTR_IMAGE_SIZE_2560x1920), MP_ROM_PTR(&camera_imagesize_2560x1920_obj)},
};
STATIC MP_DEFINE_CONST_DICT(camera_imagesize_locals_dict, camera_imagesize_locals_dict_table);
STATIC void camera_imagesize_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
qstr size = MP_QSTR_None;
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_320x240_obj)) {
size = MP_QSTR_IMAGE_SIZE_320x240;
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_640x320_obj)) {
size = MP_QSTR_IMAGE_SIZE_640x320;
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_1280x720_obj)) {
size = MP_QSTR_IMAGE_SIZE_1280x720;
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_1280x960_obj)) {
size = MP_QSTR_IMAGE_SIZE_1280x960;
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_1920x1080_obj)) {
size = MP_QSTR_IMAGE_SIZE_1920x1080;
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_2048x1536_obj)) {
size = MP_QSTR_IMAGE_SIZE_2048x1536;
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imagesize_2560x1920_obj)) {
size = MP_QSTR_IMAGE_SIZE_2560x1920;
}
mp_printf(print, "%q.%q.%q", MP_QSTR_camera, MP_QSTR_ImageSize, size);
}
const mp_obj_type_t camera_imagesize_type = {
{ &mp_type_type },
.name = MP_QSTR_ImageSize,
.print = camera_imagesize_print,
.locals_dict = (mp_obj_t)&camera_imagesize_locals_dict,
};

View File

@ -39,7 +39,7 @@ STATIC const mp_rom_map_elem_t camera_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_Camera), MP_ROM_PTR(&camera_type) }, { MP_ROM_QSTR(MP_QSTR_Camera), MP_ROM_PTR(&camera_type) },
// Enum-like Classes. // Enum-like Classes.
{ MP_ROM_QSTR(MP_QSTR_ImageSize), MP_ROM_PTR(&camera_imagesize_type) }, { MP_ROM_QSTR(MP_QSTR_ImageSize), MP_ROM_PTR(&camera_imageformat_type) },
}; };
STATIC MP_DEFINE_CONST_DICT(camera_module_globals, camera_module_globals_table); STATIC MP_DEFINE_CONST_DICT(camera_module_globals, camera_module_globals_table);