ChapterForge
Loading...
Searching...
No Matches
fourcc_utils.hpp
Go to the documentation of this file.
1//
2// fourcc_utils.hpp
3// ChapterForge
4//
5// Created by Till Toenshoff on 12/9/25.
6// Copyright © 2025 Till Toenshoff. All rights reserved.
7//
8
9#pragma once
10
11#include <cstdint>
12#include <stdexcept>
13#include <string>
14
15// FourCC helpers.
16inline constexpr uint32_t fourcc(const char a, const char b, const char c, const char d) {
17 return (uint32_t(uint8_t(a)) << 24) | (uint32_t(uint8_t(b)) << 16) |
18 (uint32_t(uint8_t(c)) << 8) | (uint32_t(uint8_t(d)));
19}
20
21inline constexpr uint32_t fourcc(const char t[4]) { return fourcc(t[0], t[1], t[2], t[3]); }
22
23inline uint32_t fourcc(const std::string &s) {
24 if (s.size() < 4) {
25 throw std::runtime_error("fourcc string too short");
26 }
27 return fourcc(s[0], s[1], s[2], s[3]);
28}
29
30inline bool is_printable_fourcc(uint32_t type) {
31 for (int i = 0; i < 4; ++i) {
32 const uint8_t c = static_cast<uint8_t>(type >> (24 - 8 * i));
33 if (c < 0x20 || c > 0x7E) {
34 return false;
35 }
36 }
37 return true;
38}
39
40inline std::string fourcc_to_string(uint32_t type) {
41 std::string s(4, ' ');
42 s[0] = static_cast<char>((type >> 24) & 0xFF);
43 s[1] = static_cast<char>((type >> 16) & 0xFF);
44 s[2] = static_cast<char>((type >> 8) & 0xFF);
45 s[3] = static_cast<char>(type & 0xFF);
46 return s;
47}
constexpr uint32_t fourcc(const char a, const char b, const char c, const char d)
Definition fourcc_utils.hpp:16
bool is_printable_fourcc(uint32_t type)
Definition fourcc_utils.hpp:30
std::string fourcc_to_string(uint32_t type)
Definition fourcc_utils.hpp:40