AnimationWriter.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2024, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibGfx/ImageFormats/AnimationWriter.h>
  8. #include <LibGfx/Rect.h>
  9. namespace Gfx {
  10. AnimationWriter::~AnimationWriter() = default;
  11. static bool are_scanlines_equal(Bitmap const& a, Bitmap const& b, int y)
  12. {
  13. for (int x = 0; x < a.width(); ++x) {
  14. if (a.get_pixel(x, y) != b.get_pixel(x, y))
  15. return false;
  16. }
  17. return true;
  18. }
  19. static bool are_columns_equal(Bitmap const& a, Bitmap const& b, int x, int y1, int y2)
  20. {
  21. for (int y = y1; y < y2; ++y) {
  22. if (a.get_pixel(x, y) != b.get_pixel(x, y))
  23. return false;
  24. }
  25. return true;
  26. }
  27. static Gfx::IntRect rect_where_pixels_are_different(Bitmap const& a, Bitmap const& b)
  28. {
  29. VERIFY(a.size() == b.size());
  30. int number_of_equal_pixels_at_top = 0;
  31. while (number_of_equal_pixels_at_top < a.height() && are_scanlines_equal(a, b, number_of_equal_pixels_at_top))
  32. ++number_of_equal_pixels_at_top;
  33. int number_of_equal_pixels_at_bottom = 0;
  34. while (number_of_equal_pixels_at_bottom < a.height() - number_of_equal_pixels_at_top && are_scanlines_equal(a, b, a.height() - number_of_equal_pixels_at_bottom - 1))
  35. ++number_of_equal_pixels_at_bottom;
  36. int const y1 = number_of_equal_pixels_at_top;
  37. int const y2 = a.height() - number_of_equal_pixels_at_bottom;
  38. int number_of_equal_pixels_at_left = 0;
  39. while (number_of_equal_pixels_at_left < a.width() && are_columns_equal(a, b, number_of_equal_pixels_at_left, y1, y2))
  40. ++number_of_equal_pixels_at_left;
  41. int number_of_equal_pixels_at_right = 0;
  42. while (number_of_equal_pixels_at_right < a.width() - number_of_equal_pixels_at_left && are_columns_equal(a, b, a.width() - number_of_equal_pixels_at_right - 1, y1, y2))
  43. ++number_of_equal_pixels_at_right;
  44. // WebP can only encode even-sized animation frame positions.
  45. // FIXME: Change API shape in some way so that the AnimationWriter base class doesn't have to know about this detail of a subclass.
  46. if (number_of_equal_pixels_at_left % 2 != 0)
  47. --number_of_equal_pixels_at_left;
  48. if (number_of_equal_pixels_at_top % 2 != 0)
  49. --number_of_equal_pixels_at_top;
  50. Gfx::IntRect rect;
  51. rect.set_x(number_of_equal_pixels_at_left);
  52. rect.set_y(number_of_equal_pixels_at_top);
  53. rect.set_width(a.width() - number_of_equal_pixels_at_left - number_of_equal_pixels_at_right);
  54. rect.set_height(a.height() - number_of_equal_pixels_at_top - number_of_equal_pixels_at_bottom);
  55. return rect;
  56. }
  57. ErrorOr<void> AnimationWriter::add_frame_relative_to_last_frame(Bitmap& frame, int duration_ms, RefPtr<Bitmap> last_frame)
  58. {
  59. if (!last_frame)
  60. return add_frame(frame, duration_ms);
  61. auto rect = rect_where_pixels_are_different(*last_frame, frame);
  62. if (rect.is_empty()) {
  63. // The frame is identical to the last frame. Don't store an empty bitmap.
  64. // FIXME: We could delay writing the last frame until we know that the next frame is different,
  65. // and just keep increasing that frame's duration instead.
  66. rect = { 0, 0, 1, 1 };
  67. }
  68. // FIXME: It would be nice to have a way to crop a bitmap without copying the data.
  69. auto differences = TRY(frame.cropped(rect));
  70. // FIXME: Another idea: If all frames of the animation have no alpha,
  71. // this could set color values of pixels that are in the changed rect that are
  72. // equal to the last frame to transparent black and set the frame to be blended.
  73. // That might take less space after compression.
  74. // This assumes a replacement disposal method.
  75. return add_frame(differences, duration_ms, rect.location());
  76. }
  77. }