2022-06-11 04:46:54 +02:00
|
|
|
/* This file is part of libravatarserv.
|
|
|
|
* Copyright © 2022 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "image.hpp"
|
|
|
|
|
|
|
|
#include "types.hpp"
|
|
|
|
|
|
|
|
#include <identiconpp.hpp>
|
|
|
|
#include <Magick++/Blob.h>
|
2022-06-11 05:15:22 +02:00
|
|
|
#include <Magick++/Geometry.h>
|
2022-06-11 04:46:54 +02:00
|
|
|
#include <Magick++/Image.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <ios>
|
|
|
|
#include <iostream>
|
2022-06-11 05:15:22 +02:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2022-06-11 04:46:54 +02:00
|
|
|
|
|
|
|
namespace libravatarserv::image {
|
|
|
|
|
2022-06-11 05:15:22 +02:00
|
|
|
Magick::Image get(const fs::path &filepath, const size_t size) {
|
|
|
|
Magick::Image img;
|
|
|
|
img.read(filepath);
|
|
|
|
img.resize(Magick::Geometry(size, size));
|
|
|
|
return img;
|
2022-06-11 04:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Magick::Image generate_identicon(const request_t &req) {
|
|
|
|
|
|
|
|
const auto padwidth{[&req]() -> std::uint8_t {
|
|
|
|
if (req.size < 60) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return static_cast<std::uint8_t>(std::max(req.size / 10, 10));
|
|
|
|
}()};
|
|
|
|
|
|
|
|
Identiconpp identicon(5, 5, Identiconpp::algorithm::sigil, "fefefeff",
|
|
|
|
{
|
|
|
|
// The same colors ivatar uses.
|
|
|
|
"2d4fffff", // Blue
|
|
|
|
"feb42cff", // Yellow
|
|
|
|
"e279eaff", // Bright pink
|
|
|
|
"1eb3fdff", // Cyan
|
|
|
|
"E84D41ff", // Red
|
|
|
|
"31CB73ff", // Green
|
|
|
|
"8D45AAff" // Dark pink
|
|
|
|
},
|
|
|
|
{padwidth, padwidth});
|
|
|
|
Magick::Image img{
|
|
|
|
identicon.generate(req.hash, static_cast<std::uint8_t>(req.size))};
|
|
|
|
img.magick("PNG");
|
|
|
|
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
2022-06-11 05:15:22 +02:00
|
|
|
void write(Magick::Image &img,
|
|
|
|
const std::optional<std::string_view> magick_force) {
|
|
|
|
std::string magick{magick_force.value_or(img.magick())};
|
|
|
|
std::transform(magick.begin(), magick.end(), magick.begin(), ::tolower);
|
|
|
|
img.magick(magick);
|
|
|
|
|
|
|
|
Magick::Blob buffer;
|
|
|
|
img.write(&buffer);
|
|
|
|
|
|
|
|
std::cout << "Content-Type: image/" << magick << '\n';
|
|
|
|
std::cout << "Content-Length: " << buffer.length() << "\n\n";
|
|
|
|
std::cout.write(static_cast<const char *>(buffer.data()),
|
|
|
|
static_cast<std::streamsize>(buffer.length()));
|
|
|
|
}
|
|
|
|
|
2022-06-11 04:46:54 +02:00
|
|
|
} // namespace libravatarserv::image
|