PBMLoader.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "PBMLoader.h"
  8. #include "AK/Endian.h"
  9. #include "PortableImageLoaderCommon.h"
  10. #include "Userland/Libraries/LibGfx/Streamer.h"
  11. #include <string.h>
  12. namespace Gfx {
  13. bool read_image_data(PBMLoadingContext& context, Streamer& streamer)
  14. {
  15. u8 byte;
  16. Vector<Gfx::Color> color_data;
  17. if (context.type == PBMLoadingContext::Type::ASCII) {
  18. while (streamer.read(byte)) {
  19. if (byte == '0') {
  20. color_data.append(Color::White);
  21. } else if (byte == '1') {
  22. color_data.append(Color::Black);
  23. }
  24. }
  25. } else if (context.type == PBMLoadingContext::Type::RAWBITS) {
  26. size_t color_index = 0;
  27. while (streamer.read(byte)) {
  28. for (int i = 0; i < 8; i++) {
  29. int val = byte & 0x80;
  30. if (val == 0) {
  31. color_data.append(Color::White);
  32. } else {
  33. color_data.append(Color::Black);
  34. }
  35. byte = byte << 1;
  36. color_index++;
  37. if (color_index % context.width == 0) {
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. size_t context_size = (u32)context.width * (u32)context.height;
  44. if (context_size != color_data.size()) {
  45. dbgln("Not enough color data in image.");
  46. return false;
  47. }
  48. if (!create_bitmap(context)) {
  49. return false;
  50. }
  51. set_pixels(context, color_data);
  52. context.state = PBMLoadingContext::State::Bitmap;
  53. return true;
  54. }
  55. }