PPMLoader.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. Vector<Gfx::Color> color_data;
  13. auto const context_size = context.width * context.height;
  14. color_data.resize(context_size);
  15. auto& stream = *context.stream;
  16. if (context.type == PPMLoadingContext::Type::ASCII) {
  17. for (u64 i = 0; i < context_size; ++i) {
  18. auto const red = TRY(read_number(stream));
  19. TRY(read_whitespace(context));
  20. auto const green = TRY(read_number(stream));
  21. TRY(read_whitespace(context));
  22. auto const blue = TRY(read_number(stream));
  23. TRY(read_whitespace(context));
  24. Color color { static_cast<u8>(red), static_cast<u8>(green), static_cast<u8>(blue) };
  25. if (context.format_details.max_val < 255)
  26. color = adjust_color(context.format_details.max_val, color);
  27. color_data[i] = color;
  28. }
  29. } else if (context.type == PPMLoadingContext::Type::RAWBITS) {
  30. for (u64 i = 0; i < context_size; ++i) {
  31. Array<u8, 3> pixel;
  32. Bytes buffer { pixel };
  33. TRY(stream.read_until_filled(buffer));
  34. color_data[i] = { pixel[0], pixel[1], pixel[2] };
  35. }
  36. }
  37. TRY(create_bitmap(context));
  38. set_pixels(context, color_data);
  39. context.state = PPMLoadingContext::State::Bitmap;
  40. return {};
  41. }
  42. }