-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_fpng.cpp
36 lines (28 loc) · 903 Bytes
/
image_fpng.cpp
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
//
// Created by TYTY on 2023-02-10 010.
//
#include <mutex>
#include <fstream>
#include "nn-scaler.h"
#include "fpng.h"
static std::once_flag fpng_inited;
std::string save_image_png(Work::output_t file, md_view<uint8_t, int32_t, 3> data) {
std::call_once(fpng_inited, fpng::fpng_init);
auto [height, width, components] = data.shape;
std::vector<uint8_t> output;
if (!fpng::fpng_encode_image_to_memory(data.data, width, height, components, output)) {
return "fpng encode fail";
}
if (file.index() == 0) {
std::ofstream of(std::get<0>(file), std::ios::out | std::ios::binary | std::ios::trunc);
if (!of.is_open()) {
return "can't open output file";
}
of.write(reinterpret_cast<char *>(output.data()), output.size());
} else if (file.index() == 1) {
std::get<1>(file).set_value(std::move(output));
} else {
return "unexpected";
}
return "";
}