CMYKBitmap.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) 2024, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Checked.h>
  7. #include <LibGfx/CMYKBitmap.h>
  8. namespace Gfx {
  9. ErrorOr<NonnullRefPtr<CMYKBitmap>> CMYKBitmap::create_with_size(IntSize const& size)
  10. {
  11. VERIFY(size.width() >= 0 && size.height() >= 0);
  12. Checked<int> final_size { size.width() };
  13. final_size.mul(size.height());
  14. final_size.mul(sizeof(CMYK));
  15. if (final_size.has_overflow())
  16. return Error::from_string_literal("Image dimensions cause an integer overflow");
  17. auto data = TRY(ByteBuffer::create_uninitialized(final_size.value()));
  18. return adopt_ref(*new CMYKBitmap(size, move(data)));
  19. }
  20. ErrorOr<RefPtr<Bitmap>> CMYKBitmap::to_low_quality_rgb() const
  21. {
  22. if (!m_rgb_bitmap) {
  23. m_rgb_bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { m_size.width(), m_size.height() }));
  24. for (int y = 0; y < m_size.height(); ++y) {
  25. for (int x = 0; x < m_size.width(); ++x) {
  26. auto const& cmyk = scanline(y)[x];
  27. u8 k = 255 - cmyk.k;
  28. m_rgb_bitmap->scanline(y)[x] = Color((255 - cmyk.c) * k / 255, (255 - cmyk.m) * k / 255, (255 - cmyk.y) * k / 255).value();
  29. }
  30. }
  31. }
  32. return m_rgb_bitmap;
  33. }
  34. }