-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewlibcamera.cpp
More file actions
247 lines (192 loc) · 7.9 KB
/
Copy pathnewlibcamera.cpp
File metadata and controls
247 lines (192 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iomanip>
#include <iostream>
#include <memory>
#include <thread>
#include <sys/mman.h>
#include <fstream>
#include <png.h>
#include <libcamera/libcamera.h>
using namespace libcamera;
using namespace std::chrono_literals;
static void requestComplete(Request *request);
static void writePNG(const std::string &filename, uint8_t *rgb, int width, int height);
#define WIDTH 4056
#define HEIGHT 3040
static std::shared_ptr<libcamera::Camera> camera;
int main() {
std::unique_ptr<CameraManager> cm = std::make_unique<CameraManager>();
cm->start();
for (auto const &camera : cm->cameras())
std::cout << camera->id() << std::endl;
auto cameras = cm->cameras();
if (cameras.empty()) {
std::cout << "No cameras were identified on the system.\n";
cm->stop();
return EXIT_FAILURE;
}
std::string cameraName = cameras[0]->id();
camera = cm->get(cameraName);
camera->acquire();
std::unique_ptr<CameraConfiguration> config = camera->generateConfiguration( {StreamRole::Viewfinder}); // Should prob be still
StreamConfiguration &streamConfig = config->at(0);
std::cout << "Default viewfinder configuration is: " << streamConfig.toString() << std::endl;
streamConfig.size.width = 4056;
streamConfig.size.height = 3040;
config->validate();
std::cout << "Validated viewfinder configuration is: " << streamConfig.toString() << std::endl;
camera->configure(config.get());
FrameBufferAllocator *allocator = new FrameBufferAllocator(camera);
for (StreamConfiguration &cfg : *config) {
int ret = allocator->allocate(cfg.stream());
if (ret < 0) {
std::cerr << "Can't allocate buffers" << std::endl;
return -ENOMEM;
}
size_t allocated = allocator->buffers(cfg.stream()).size();
std::cout << "Allocated " << allocated << " buffers for stream\n";
}
Stream *stream = streamConfig.stream();
const std::vector<std::unique_ptr<FrameBuffer>> &buffers = allocator->buffers(stream);
std::vector<std::unique_ptr<Request>> requests;
for (unsigned int i = 0; i < buffers.size(); ++i) {
std::unique_ptr<Request> request = camera->createRequest();
if (!request) {
std::cerr << "Can't create request\n";
return -ENOMEM;
}
const std::unique_ptr<FrameBuffer> &buffer = buffers[i];
int ret = request->addBuffer(stream, buffer.get());
if (ret < 0) {
std::cerr << "Can't set buffer for request\n";
return ret;
}
requests.push_back(std::move(request));
}
camera->requestCompleted.connect(requestComplete);
camera->start();
for (std::unique_ptr<Request> &request : requests)
camera->queueRequest(request.get());
std::this_thread::sleep_for(3000ms);
camera->stop();
allocator->free(stream);
delete allocator;
camera->release();
camera.reset();
cm->stop();
return 0;
}
static void requestComplete(Request *request) {
if(request->status() == Request::RequestCancelled)
return;
const std::map<const Stream*, FrameBuffer*> &buffers = request->buffers();
for (auto bufferPair : buffers) {
FrameBuffer *buffer = bufferPair.second;
const FrameMetadata &metadata = buffer->metadata();
std::cout << " seq: " << std::setw(6) << std::setfill('0') << metadata.sequence << " bytesused: ";
unsigned int nplane = 0;
for (const FrameMetadata::Plane &plane : metadata.planes()) {
//const uint8_t *data = plane.
std::cout << plane.bytesused;
if (++nplane < metadata.planes().size()) std::cout << "/";
}
std::cout << std::endl;
// ---- Save raw frame data
int index = 0;
for (const FrameBuffer::Plane &plane : buffer->planes()) {
void *memory = mmap(nullptr,
plane.length,
PROT_READ,
MAP_SHARED,
plane.fd.get(),
plane.offset);
if (memory == MAP_FAILED) {
std::cerr << "mmap failed" << std::endl;
continue;
}
uint16_t *raw12 = (uint16_t *)memory;
std::vector<uint8_t> rgb(WIDTH * HEIGHT * 3);
auto getRaw = [&](int x, int y) {
return raw12[y * WIDTH + x] >> 4;
};
for (int y = 0; y < HEIGHT - 1; ++y) {
for (int x = 0; x < WIDTH - 1; ++ x) {
uint8_t R, G, B;
bool yOdd = y & 1;
bool xOdd = x & 1;
if (!yOdd && !xOdd) { // B G
B = getRaw(x, y);
G = getRaw(x + 1, y);
R = getRaw(x + 1, y + 1);
} else if (!yOdd && xOdd) { // G R
B = getRaw(x - 1, y);
G = getRaw(x, y);
R = getRaw(x, y + 1);
} else if (yOdd && !xOdd) { // G B
B = getRaw(x, y - 1);
G = getRaw(x, y);
R = getRaw(x + 1, y);
} else { // R G
B = getRaw(x - 1, y - 1);
G = getRaw(x, y);
R = getRaw(x, y);
}
int idx = (y * WIDTH + x) * 3;
rgb[idx + 0] = R;
rgb[idx + 1] = G;
rgb[idx + 2] = B;
}
}
writePNG("capture.png", rgb.data(), WIDTH, HEIGHT);
std::cout << "saved capture.png\n";
std::string filename = "frame_" + std::to_string(metadata.sequence)
+ "_plane" + std::to_string(index) + ".raw";
std::ofstream file(filename, std::ios::binary);
file.write(static_cast<const char *>(memory), metadata.planes()[index].bytesused);
munmap(memory, plane.length);
index++;
}
}
request->reuse(Request::ReuseBuffers);
camera->queueRequest(request);
}
static void writePNG(const std::string &filename, uint8_t *rgb, int width, int height) {
FILE *fp = fopen(filename.c_str(), "wb");
if (!fp) {
std::cerr << "Failed to open PNG file\n";
return;
}
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr,nullptr);
png_infop info = png_create_info_struct(png);
if (setjmp(png_jmpbuf(png))) {
png_destroy_write_struct(&png, &info);
fclose(fp);
return;
}
png_init_io(png, fp);
png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png, info);
for (int y = 0; y < height; ++y) {
png_write_row(png, rgb + y * width * 3);
}
png_write_end(png, nullptr);
png_destroy_write_struct(&png, &info);
fclose(fp);
}
// static void requestComplete(Request *request) {
// if(request->status() == Request::RequestCancelled)
// return;
// const std::map<const Stream*, FrameBuffer*> &buffers = request->buffers();
// for (auto bufferPair : buffers) {
// FrameBuffer *buffer = bufferPair.second;
// const FrameMetadata &metadata = buffer->metadata();
// std::cout << " seq: " << std::setw(6) << std::setfill('0') << metadata.sequence << " bytesused: ";
// unsigned int nplane = 0;
// for (const FrameMetadata::Plane &plane : metadata.planes()) {
// //const uint8_t *data = plane.
// std::cout << plane.bytesused;
// if (++nplane < metadata.planes().size()) std::cout << "/";
// }
// }
// request->reuse(Request::ReuseBuffers);
// camera->queueRequest(request);
// }