LevelsDialog.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2022, Torsten Engelmann <engelTorsten@gmx.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "LevelsDialog.h"
  7. #include <Applications/PixelPaint/LevelsDialogGML.h>
  8. #include <LibGUI/Button.h>
  9. #include <LibGUI/Label.h>
  10. #include <LibGUI/ValueSlider.h>
  11. namespace PixelPaint {
  12. LevelsDialog::LevelsDialog(GUI::Window* parent_window, ImageEditor* editor)
  13. : GUI::Dialog(parent_window)
  14. {
  15. set_title("Levels");
  16. set_icon(parent_window->icon());
  17. auto& main_widget = set_main_widget<GUI::Widget>();
  18. if (!main_widget.load_from_gml(levels_dialog_gml))
  19. VERIFY_NOT_REACHED();
  20. resize(305, 202);
  21. set_resizable(false);
  22. m_editor = editor;
  23. m_brightness_slider = main_widget.find_descendant_of_type_named<GUI::ValueSlider>("brightness_slider");
  24. m_contrast_slider = main_widget.find_descendant_of_type_named<GUI::ValueSlider>("contrast_slider");
  25. m_gamma_slider = main_widget.find_descendant_of_type_named<GUI::ValueSlider>("gamma_slider");
  26. auto context_label = main_widget.find_descendant_of_type_named<GUI::Label>("context_label");
  27. auto apply_button = main_widget.find_descendant_of_type_named<GUI::Button>("apply_button");
  28. auto cancel_button = main_widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
  29. VERIFY(m_brightness_slider);
  30. VERIFY(m_contrast_slider);
  31. VERIFY(m_gamma_slider);
  32. VERIFY(context_label);
  33. VERIFY(apply_button);
  34. VERIFY(cancel_button);
  35. VERIFY(m_editor->active_layer());
  36. context_label->set_text(DeprecatedString::formatted("Working on layer: {}", m_editor->active_layer()->name()));
  37. m_gamma_slider->set_value(100);
  38. m_brightness_slider->on_change = [this](auto) {
  39. generate_new_image();
  40. };
  41. m_contrast_slider->on_change = [this](auto) {
  42. generate_new_image();
  43. };
  44. m_gamma_slider->on_change = [this](auto) {
  45. generate_new_image();
  46. };
  47. apply_button->on_click = [this](auto) {
  48. if (m_did_change)
  49. m_editor->did_complete_action("Levels"sv);
  50. cleanup_resources();
  51. done(ExecResult::OK);
  52. };
  53. cancel_button->on_click = [this](auto) {
  54. done(ExecResult::Cancel);
  55. };
  56. }
  57. void LevelsDialog::revert_possible_changes()
  58. {
  59. // FIXME: Find a faster way to revert all the changes that we have done.
  60. if (m_did_change && m_reference_bitmap) {
  61. for (int x = 0; x < m_reference_bitmap->width(); x++) {
  62. for (int y = 0; y < m_reference_bitmap->height(); y++) {
  63. m_editor->active_layer()->content_bitmap().set_pixel(x, y, m_reference_bitmap->get_pixel(x, y));
  64. }
  65. }
  66. m_editor->layers_did_change();
  67. }
  68. cleanup_resources();
  69. }
  70. void LevelsDialog::generate_new_image()
  71. {
  72. (void)ensure_reference_bitmap();
  73. if (m_reference_bitmap.is_null())
  74. return;
  75. generate_precomputed_color_correction();
  76. Color current_pixel_color;
  77. Color new_pixel_color;
  78. Gfx::StorageFormat storage_format = Gfx::determine_storage_format(m_editor->active_layer()->content_bitmap().format());
  79. for (int x = 0; x < m_reference_bitmap->width(); x++) {
  80. for (int y = 0; y < m_reference_bitmap->height(); y++) {
  81. current_pixel_color = m_reference_bitmap->get_pixel(x, y);
  82. new_pixel_color.set_alpha(current_pixel_color.alpha());
  83. new_pixel_color.set_red(m_precomputed_color_correction[current_pixel_color.red()]);
  84. new_pixel_color.set_green(m_precomputed_color_correction[current_pixel_color.green()]);
  85. new_pixel_color.set_blue(m_precomputed_color_correction[current_pixel_color.blue()]);
  86. switch (storage_format) {
  87. case Gfx::StorageFormat::BGRx8888:
  88. case Gfx::StorageFormat::BGRA8888:
  89. m_editor->active_layer()->content_bitmap().scanline(y)[x] = new_pixel_color.value();
  90. break;
  91. default:
  92. m_editor->active_layer()->content_bitmap().set_pixel(x, y, new_pixel_color);
  93. }
  94. }
  95. }
  96. m_editor->active_layer()->did_modify_bitmap();
  97. m_did_change = true;
  98. }
  99. ErrorOr<void> LevelsDialog::ensure_reference_bitmap()
  100. {
  101. if (m_reference_bitmap.is_null())
  102. m_reference_bitmap = TRY(m_editor->active_layer()->content_bitmap().clone());
  103. return {};
  104. }
  105. void LevelsDialog::cleanup_resources()
  106. {
  107. if (m_reference_bitmap)
  108. m_reference_bitmap = nullptr;
  109. }
  110. void LevelsDialog::generate_precomputed_color_correction()
  111. {
  112. int delta_brightness = m_brightness_slider->value();
  113. float contrast_correction_factor = static_cast<float>(259 * (m_contrast_slider->value() + 255) / static_cast<float>(255 * (259 - m_contrast_slider->value())));
  114. float gamma_correction = 1 / (m_gamma_slider->value() / 100.0);
  115. for (int color_val = 0; color_val < 256; color_val++) {
  116. m_precomputed_color_correction[color_val] = color_val + delta_brightness;
  117. m_precomputed_color_correction[color_val] = m_precomputed_color_correction[color_val] < 0 ? 0 : m_precomputed_color_correction[color_val];
  118. m_precomputed_color_correction[color_val] = m_precomputed_color_correction[color_val] > 255 ? 255 : m_precomputed_color_correction[color_val];
  119. m_precomputed_color_correction[color_val] = 255 * AK::pow<float>((m_precomputed_color_correction[color_val] / 255.0), gamma_correction);
  120. m_precomputed_color_correction[color_val] = contrast_correction_factor * (m_precomputed_color_correction[color_val] - 128) + 128;
  121. m_precomputed_color_correction[color_val] = m_precomputed_color_correction[color_val] < 0 ? 0 : m_precomputed_color_correction[color_val];
  122. m_precomputed_color_correction[color_val] = m_precomputed_color_correction[color_val] > 255 ? 255 : m_precomputed_color_correction[color_val];
  123. }
  124. }
  125. }