PPMLoader.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 "PPMLoader.h"
  8. #include "PortableImageLoaderCommon.h"
  9. namespace Gfx {
  10. ErrorOr<void> read_image_data(PPMLoadingContext& context)
  11. {
  12. auto const context_size = context.width * context.height;
  13. TRY(create_bitmap(context));
  14. auto& stream = *context.stream;
  15. if (context.type == PPMLoadingContext::Type::ASCII) {
  16. for (u64 i = 0; i < context_size; ++i) {
  17. auto const red = TRY(read_number(stream));
  18. TRY(read_whitespace(context));
  19. auto const green = TRY(read_number(stream));
  20. TRY(read_whitespace(context));
  21. auto const blue = TRY(read_number(stream));
  22. TRY(read_whitespace(context));
  23. Color color { static_cast<u8>(red), static_cast<u8>(green), static_cast<u8>(blue) };
  24. if (context.format_details.max_val < 255)
  25. color = adjust_color(context.format_details.max_val, color);
  26. context.bitmap->set_pixel(i % context.width, i / context.width, color);
  27. }
  28. } else if (context.type == PPMLoadingContext::Type::RAWBITS) {
  29. for (u64 i = 0; i < context_size; ++i) {
  30. Array<u8, 3> pixel;
  31. Bytes buffer { pixel };
  32. TRY(stream.read_until_filled(buffer));
  33. context.bitmap->set_pixel(i % context.width, i / context.width, { pixel[0], pixel[1], pixel[2] });
  34. }
  35. }
  36. return {};
  37. }
  38. }