WebCFace 2.5.2
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
image.h
Go to the documentation of this file.
1#pragma once
2#include "./base.h"
4#include <optional>
5#include <vector>
6
7#ifndef MSGPACK_DEFINE_MAP
8#define MSGPACK_DEFINE_MAP(...)
9#endif
10
12namespace message {
13
14enum class ImageColorMode {
15 gray = 0,
16 bgr = 1,
17 bgra = 2,
18 rgb = 3,
19 rgba = 4,
20};
22 raw = 0,
23 jpeg = 1,
24 webp = 2,
25 png = 3,
26};
27
28}
30
31#ifdef MSGPACK_ADD_ENUM
34#endif
35
37namespace message {
38
39struct ImageFrame {
40 int width_ = 0, height_ = 0;
41 std::shared_ptr<std::vector<unsigned char>> data_;
42 ImageColorMode color_mode_ = ImageColorMode::gray;
43 ImageCompressMode cmp_mode_ = ImageCompressMode::raw;
44
45 bool empty() const { return !data_ || data_->empty(); }
46 unsigned char *rawPtr() const {
47 if (!data_) {
48 throw "ImageFrame data is empty";
49 }
50 return data_->data();
51 }
52 std::size_t rawSize() const {
53 if (!data_) {
54 throw "ImageFrame data is empty";
55 }
56 return data_->size();
57 }
58};
59struct Image : public MessageBase<MessageKind::image>, public ImageFrame {
61 Image() = default;
62 Image(const SharedString &field, const ImageFrame &img)
63 : message::MessageBase<MessageKind::image>(), ImageFrame(img),
64 field(field) {}
65 MSGPACK_DEFINE_MAP(MSGPACK_NVP("f", field), MSGPACK_NVP("d", data_),
66 MSGPACK_NVP("h", height_), MSGPACK_NVP("w", width_),
67 MSGPACK_NVP("l", color_mode_),
68 MSGPACK_NVP("p", cmp_mode_))
69};
70
71struct ImageReq {
72 std::optional<int> rows = std::nullopt, cols = std::nullopt;
73 std::optional<ImageColorMode> color_mode = std::nullopt;
74 ImageCompressMode cmp_mode = ImageCompressMode::raw;
75 int quality = 0;
76 std::optional<double> frame_rate = std::nullopt;
77
78 bool operator==(const ImageReq &rhs) const {
79 return rows == rhs.rows && cols == rhs.cols &&
80 color_mode == rhs.color_mode && cmp_mode == rhs.cmp_mode &&
81 quality == rhs.quality;
82 }
83 bool operator!=(const ImageReq &rhs) const { return !(*this == rhs); }
84};
85template <typename T>
86struct Req;
87template <>
88struct Req<Image> : public MessageBase<MessageKind::image + MessageKind::req>,
89 public ImageReq {
92 unsigned int req_id;
93
94 Req() = default;
95 Req(const SharedString &member, const SharedString &field,
96 unsigned int req_id, const ImageReq &ireq)
97 : MessageBase<MessageKind::image + MessageKind::req>(), ImageReq(ireq),
98 member(member), field(field), req_id(req_id) {}
99 MSGPACK_DEFINE_MAP(MSGPACK_NVP("i", req_id), MSGPACK_NVP("M", member),
100 MSGPACK_NVP("f", field), MSGPACK_NVP("w", cols),
101 MSGPACK_NVP("h", rows), MSGPACK_NVP("l", color_mode),
102 MSGPACK_NVP("p", cmp_mode), MSGPACK_NVP("q", quality),
103 MSGPACK_NVP("r", frame_rate))
104};
105template <>
106struct Res<Image> : public MessageBase<MessageKind::image + MessageKind::res>,
107 public ImageFrame {
108 unsigned int req_id = 0;
110 Res() = default;
111 Res(unsigned int req_id, const SharedString &sub_field,
112 const ImageFrame &img)
113 : MessageBase<MessageKind::image + MessageKind::res>(), ImageFrame(img),
114 req_id(req_id), sub_field(sub_field) {}
115 MSGPACK_DEFINE_MAP(MSGPACK_NVP("i", req_id), MSGPACK_NVP("f", sub_field),
116 MSGPACK_NVP("d", data_), MSGPACK_NVP("w", width_),
117 MSGPACK_NVP("h", height_), MSGPACK_NVP("l", color_mode_),
118 MSGPACK_NVP("p", cmp_mode_))
119};
120
121} // namespace message
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:69
#define MSGPACK_DEFINE_MAP(...)
Definition canvas2d.h:10
ImageCompressMode
Definition image.h:21
ImageColorMode
Definition image.h:14
Definition image.h:39
unsigned char * rawPtr() const
Definition image.h:46
bool empty() const
Definition image.h:45
std::size_t rawSize() const
Definition image.h:52
std::shared_ptr< std::vector< unsigned char > > data_
Definition image.h:41
Definition image.h:71
std::optional< int > rows
Definition image.h:72
std::optional< ImageColorMode > color_mode
Definition image.h:73
bool operator!=(const ImageReq &rhs) const
Definition image.h:83
bool operator==(const ImageReq &rhs) const
Definition image.h:78
int quality
Definition image.h:75
ImageCompressMode cmp_mode
Definition image.h:74
std::optional< int > cols
Definition image.h:72
Definition image.h:59
SharedString field
Definition image.h:60
Image(const SharedString &field, const ImageFrame &img)
Definition image.h:62
型からkindを取得するためだけのベースクラス
Definition base.h:53
unsigned int req_id
Definition image.h:92
SharedString field
Definition image.h:91
SharedString member
Definition image.h:90
Req(const SharedString &member, const SharedString &field, unsigned int req_id, const ImageReq &ireq)
Definition image.h:95
client->server 以降Recvを送るようリクエスト
Definition pack.h:40
Res(unsigned int req_id, const SharedString &sub_field, const ImageFrame &img)
Definition image.h:111
SharedString sub_field
Definition image.h:109
Definition base.h:58
#define WEBCFACE_NS_END
Definition webcface-config.h:104
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:103