BinaryWriter.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/ICC/BinaryFormat.h>
  7. #include <LibGfx/ICC/BinaryWriter.h>
  8. #include <LibGfx/ICC/Profile.h>
  9. #include <time.h>
  10. namespace Gfx::ICC {
  11. ErrorOr<ByteBuffer> encode(Profile const& profile)
  12. {
  13. // Leaves enough room for the profile header and the tag table count.
  14. // FIXME: Serialize tag data and write tag table and tag data too.
  15. auto bytes = TRY(ByteBuffer::create_zeroed(sizeof(ICCHeader) + sizeof(u32)));
  16. VERIFY(bytes.size() >= sizeof(ICCHeader));
  17. auto& raw_header = *bit_cast<ICCHeader*>(bytes.data());
  18. raw_header.profile_size = bytes.size();
  19. raw_header.preferred_cmm_type = profile.preferred_cmm_type().value_or(PreferredCMMType { 0 });
  20. raw_header.profile_version_major = profile.version().major_version();
  21. raw_header.profile_version_minor_bugfix = profile.version().minor_and_bugfix_version();
  22. raw_header.profile_version_zero = 0;
  23. raw_header.profile_device_class = profile.device_class();
  24. raw_header.data_color_space = profile.data_color_space();
  25. raw_header.profile_connection_space = profile.connection_space();
  26. time_t profile_timestamp = profile.creation_timestamp();
  27. struct tm tm;
  28. if (!gmtime_r(&profile_timestamp, &tm))
  29. return Error::from_errno(errno);
  30. raw_header.profile_creation_time.year = tm.tm_year + 1900;
  31. raw_header.profile_creation_time.month = tm.tm_mon + 1;
  32. raw_header.profile_creation_time.day = tm.tm_mday;
  33. raw_header.profile_creation_time.hours = tm.tm_hour;
  34. raw_header.profile_creation_time.minutes = tm.tm_min;
  35. raw_header.profile_creation_time.seconds = tm.tm_sec;
  36. raw_header.profile_file_signature = ProfileFileSignature;
  37. raw_header.primary_platform = profile.primary_platform().value_or(PrimaryPlatform { 0 });
  38. raw_header.profile_flags = profile.flags().bits();
  39. raw_header.device_manufacturer = profile.device_manufacturer().value_or(DeviceManufacturer { 0 });
  40. raw_header.device_model = profile.device_model().value_or(DeviceModel { 0 });
  41. raw_header.device_attributes = profile.device_attributes().bits();
  42. raw_header.rendering_intent = profile.rendering_intent();
  43. raw_header.pcs_illuminant = profile.pcs_illuminant();
  44. raw_header.profile_creator = profile.creator().value_or(Creator { 0 });
  45. memset(raw_header.reserved, 0, sizeof(raw_header.reserved));
  46. auto id = Profile::compute_id(bytes);
  47. static_assert(sizeof(id.data) == sizeof(raw_header.profile_id));
  48. memcpy(raw_header.profile_id, id.data, sizeof(id.data));
  49. return bytes;
  50. }
  51. }