DistinctFourCC.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Format.h>
  8. #include <AK/Types.h>
  9. namespace Gfx::ICC {
  10. // The ICC spec uses FourCCs for many different things.
  11. // This is used to give FourCCs for different roles distinct types, so that they can only be compared to the correct constants.
  12. // (FourCCs that have only a small and fixed set of values should use an enum class instead, see e.g. DeviceClass and ColorSpace in Enums.h.)
  13. enum class FourCCType {
  14. PreferredCMMType,
  15. DeviceManufacturer,
  16. DeviceModel,
  17. Creator,
  18. TagSignature,
  19. TagTypeSignature,
  20. };
  21. template<FourCCType type>
  22. struct [[gnu::packed]] DistinctFourCC {
  23. constexpr explicit DistinctFourCC(u32 value)
  24. : value(value)
  25. {
  26. }
  27. constexpr operator u32() const { return value; }
  28. char c0() const { return value >> 24; }
  29. char c1() const { return (value >> 16) & 0xff; }
  30. char c2() const { return (value >> 8) & 0xff; }
  31. char c3() const { return value & 0xff; }
  32. bool operator==(DistinctFourCC b) const { return value == b.value; }
  33. u32 value { 0 };
  34. };
  35. using PreferredCMMType = DistinctFourCC<FourCCType::PreferredCMMType>; // ICC v4, "7.2.3 Preferred CMM type field"
  36. using DeviceManufacturer = DistinctFourCC<FourCCType::DeviceManufacturer>; // ICC v4, "7.2.12 Device manufacturer field"
  37. using DeviceModel = DistinctFourCC<FourCCType::DeviceModel>; // ICC v4, "7.2.13 Device model field"
  38. using Creator = DistinctFourCC<FourCCType::Creator>; // ICC v4, "7.2.17 Profile creator field"
  39. using TagSignature = DistinctFourCC<FourCCType::TagSignature>; // ICC v4, "9.2 Tag listing"
  40. using TagTypeSignature = DistinctFourCC<FourCCType::TagTypeSignature>; // ICC v4, "10 Tag type definitions"
  41. }
  42. template<Gfx::ICC::FourCCType Type>
  43. struct AK::Formatter<Gfx::ICC::DistinctFourCC<Type>> : StandardFormatter {
  44. ErrorOr<void> format(FormatBuilder& builder, Gfx::ICC::DistinctFourCC<Type> const& four_cc)
  45. {
  46. TRY(builder.put_padding('\'', 1));
  47. TRY(builder.put_padding(four_cc.c0(), 1));
  48. TRY(builder.put_padding(four_cc.c1(), 1));
  49. TRY(builder.put_padding(four_cc.c2(), 1));
  50. TRY(builder.put_padding(four_cc.c3(), 1));
  51. TRY(builder.put_padding('\'', 1));
  52. return {};
  53. }
  54. };
  55. template<Gfx::ICC::FourCCType Type>
  56. struct AK::Traits<Gfx::ICC::DistinctFourCC<Type>> : public DefaultTraits<Gfx::ICC::DistinctFourCC<Type>> {
  57. static unsigned hash(Gfx::ICC::DistinctFourCC<Type> const& key)
  58. {
  59. return int_hash(key.value);
  60. }
  61. static bool equals(Gfx::ICC::DistinctFourCC<Type> const& a, Gfx::ICC::DistinctFourCC<Type> const& b)
  62. {
  63. return a == b;
  64. }
  65. };