PGMLoader.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "PGMLoader.h"
  8. #include "PortableImageLoaderCommon.h"
  9. namespace Gfx {
  10. ErrorOr<void> read_image_data(PGMLoadingContext& context)
  11. {
  12. TRY(create_bitmap(context));
  13. auto& stream = *context.stream;
  14. auto const context_size = context.width * context.height;
  15. if (context.type == PGMLoadingContext::Type::ASCII) {
  16. for (u64 i = 0; i < context_size; ++i) {
  17. auto value = TRY(read_number(stream));
  18. TRY(read_whitespace(context));
  19. Color color { static_cast<u8>(value), static_cast<u8>(value), static_cast<u8>(value) };
  20. if (context.format_details.max_val < 255)
  21. color = adjust_color(context.format_details.max_val, color);
  22. context.bitmap->set_pixel(i % context.width, i / context.width, color);
  23. }
  24. } else if (context.type == PGMLoadingContext::Type::RAWBITS) {
  25. for (u64 i = 0; i < context_size; ++i) {
  26. auto const pixel = TRY(stream.read_value<u8>());
  27. context.bitmap->set_pixel(i % context.width, i / context.width, { pixel, pixel, pixel });
  28. }
  29. }
  30. return {};
  31. }
  32. }