From 7f9fc302039958d984e1235fffaba706c803f62b Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Sat, 3 Mar 2018 18:02:14 -0600 Subject: [PATCH] Adding new Color Shift effect, which can shift any color (RGBA) with infinite wrapping (and full supports animation). --- include/Effects.h | 1 + include/effects/ColorShift.h | 107 ++++++++++++ src/EffectInfo.cpp | 4 + src/bindings/python/openshot.i | 1 + src/bindings/ruby/openshot.i | 1 + src/effects/ColorShift.cpp | 292 +++++++++++++++++++++++++++++++++ 6 files changed, 406 insertions(+) create mode 100644 include/effects/ColorShift.h create mode 100644 src/effects/ColorShift.cpp diff --git a/include/Effects.h b/include/Effects.h index 824de454..1245d741 100644 --- a/include/Effects.h +++ b/include/Effects.h @@ -33,6 +33,7 @@ #include "effects/Blur.h" #include "effects/Brightness.h" #include "effects/ChromaKey.h" +#include "effects/ColorShift.h" #include "effects/Deinterlace.h" #include "effects/Hue.h" #include "effects/Mask.h" diff --git a/include/effects/ColorShift.h b/include/effects/ColorShift.h new file mode 100644 index 00000000..4b3de2bb --- /dev/null +++ b/include/effects/ColorShift.h @@ -0,0 +1,107 @@ +/** + * @file + * @brief Header file for Color Shift effect class + * @author Jonathan Thomas + * + * @section LICENSE + * + * Copyright (c) 2008-2014 OpenShot Studios, LLC + * . This file is part of + * OpenShot Library (libopenshot), an open-source project dedicated to + * delivering high quality video editing and animation solutions to the + * world. For more information visit . + * + * OpenShot Library (libopenshot) is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * OpenShot Library (libopenshot) is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with OpenShot Library. If not, see . + */ + +#ifndef OPENSHOT_COLOR_SHIFT_EFFECT_H +#define OPENSHOT_COLOR_SHIFT_EFFECT_H + +#include "../EffectBase.h" + +#include +#include +#include +#include "../Json.h" +#include "../KeyFrame.h" + + +using namespace std; + +namespace openshot +{ + + /** + * @brief This class shifts the pixels of an image up, down, left, or right, and can be animated + * with openshot::Keyframe curves over time. + * + * Shifting pixels can be used in many interesting ways, especially when animating the movement of the pixels. + * The pixels wrap around the image (the pixels drop off one side and appear on the other side of the image). + */ + class ColorShift : public EffectBase + { + private: + /// Init effect settings + void init_effect_details(); + + public: + Keyframe red_x; ///< Shift the Red X coordinates (left or right) + Keyframe red_y; ///< Shift the Red Y coordinates (up or down) + Keyframe green_x; ///< Shift the Green X coordinates (left or right) + Keyframe green_y; ///< Shift the Green Y coordinates (up or down) + Keyframe blue_x; ///< Shift the Blue X coordinates (left or right) + Keyframe blue_y; ///< Shift the Blue Y coordinates (up or down) + Keyframe alpha_x; ///< Shift the Alpha X coordinates (left or right) + Keyframe alpha_y; ///< Shift the Alpha Y coordinates (up or down) + + /// Blank constructor, useful when using Json to load the effect properties + ColorShift(); + + /// Default constructor, which takes 8 curves. The curves will shift the RGBA pixels up, down, left, or right + /// + /// @param red_x The curve to adjust the Red x shift (between -1 and 1, percentage) + /// @param red_y The curve to adjust the Red y shift (between -1 and 1, percentage) + /// @param green_x The curve to adjust the Green x shift (between -1 and 1, percentage) + /// @param green_y The curve to adjust the Green y shift (between -1 and 1, percentage) + /// @param blue_x The curve to adjust the Blue x shift (between -1 and 1, percentage) + /// @param blue_y The curve to adjust the Blue y shift (between -1 and 1, percentage) + /// @param alpha_x The curve to adjust the Alpha x shift (between -1 and 1, percentage) + /// @param alpha_y The curve to adjust the Alpha y shift (between -1 and 1, percentage) + ColorShift(Keyframe red_x, Keyframe red_y, Keyframe green_x, Keyframe green_y, Keyframe blue_x, Keyframe blue_y, Keyframe alpha_x, Keyframe alpha_y); + + /// @brief This method is required for all derived classes of EffectBase, and returns a + /// modified openshot::Frame object + /// + /// The frame object is passed into this method, and a frame_number is passed in which + /// tells the effect which settings to use from it's keyframes (starting at 1). + /// + /// @returns The modified openshot::Frame object + /// @param frame The frame object that needs the effect applied to it + /// @param frame_number The frame number (starting at 1) of the effect on the timeline. + std::shared_ptr GetFrame(std::shared_ptr frame, int64_t frame_number); + + /// Get and Set JSON methods + string Json(); ///< Generate JSON string of this object + void SetJson(string value); ///< Load JSON string into this object + Json::Value JsonValue(); ///< Generate Json::JsonValue for this object + void SetJsonValue(Json::Value root); ///< Load Json::JsonValue into this object + + /// Get all properties for a specific frame (perfect for a UI to display the current state + /// of all properties at any time) + string PropertiesJSON(int64_t requested_frame); + }; + +} + +#endif diff --git a/src/EffectInfo.cpp b/src/EffectInfo.cpp index bbdd8ef6..5c9df283 100644 --- a/src/EffectInfo.cpp +++ b/src/EffectInfo.cpp @@ -53,6 +53,9 @@ EffectBase* EffectInfo::CreateEffect(string effect_type) { else if (effect_type == "ChromaKey") return new ChromaKey(); + else if (effect_type == "Color Shift") + return new ColorShift(); + else if (effect_type == "Deinterlace") return new Deinterlace(); @@ -89,6 +92,7 @@ Json::Value EffectInfo::JsonValue() { root.append(Blur().JsonInfo()); root.append(Brightness().JsonInfo()); root.append(ChromaKey().JsonInfo()); + root.append(ColorShift().JsonInfo()); root.append(Deinterlace().JsonInfo()); root.append(Hue().JsonInfo()); root.append(Mask().JsonInfo()); diff --git a/src/bindings/python/openshot.i b/src/bindings/python/openshot.i index 593a6dee..bd00f8aa 100644 --- a/src/bindings/python/openshot.i +++ b/src/bindings/python/openshot.i @@ -164,6 +164,7 @@ %include "../../../include/effects/Blur.h" %include "../../../include/effects/Brightness.h" %include "../../../include/effects/ChromaKey.h" +%include "../../../include/effects/ColorShift.h" %include "../../../include/effects/Deinterlace.h" %include "../../../include/effects/Hue.h" %include "../../../include/effects/Mask.h" diff --git a/src/bindings/ruby/openshot.i b/src/bindings/ruby/openshot.i index 4d7298b6..08c00673 100644 --- a/src/bindings/ruby/openshot.i +++ b/src/bindings/ruby/openshot.i @@ -158,6 +158,7 @@ namespace std { %include "../../../include/effects/Blur.h" %include "../../../include/effects/Brightness.h" %include "../../../include/effects/ChromaKey.h" +%include "../../../include/effects/ColorShift.h" %include "../../../include/effects/Deinterlace.h" %include "../../../include/effects/Hue.h" %include "../../../include/effects/Mask.h" diff --git a/src/effects/ColorShift.cpp b/src/effects/ColorShift.cpp new file mode 100644 index 00000000..28884de5 --- /dev/null +++ b/src/effects/ColorShift.cpp @@ -0,0 +1,292 @@ +/** + * @file + * @brief Source file for Color Shift effect class + * @author Jonathan Thomas + * + * @section LICENSE + * + * Copyright (c) 2008-2014 OpenShot Studios, LLC + * . This file is part of + * OpenShot Library (libopenshot), an open-source project dedicated to + * delivering high quality video editing and animation solutions to the + * world. For more information visit . + * + * OpenShot Library (libopenshot) is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * OpenShot Library (libopenshot) is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with OpenShot Library. If not, see . + */ + +#include "../../include/effects/ColorShift.h" + +using namespace openshot; + +/// Blank constructor, useful when using Json to load the effect properties +ColorShift::ColorShift() : red_x(0.0), red_y(0.0), green_x(0.0), green_y(0.0), blue_x(0.0), blue_y(0.0), alpha_x(0.0), alpha_y(0.0) { + // Init effect properties + init_effect_details(); +} + +// Default constructor +ColorShift::ColorShift(Keyframe red_x, Keyframe red_y, Keyframe green_x, Keyframe green_y, Keyframe blue_x, Keyframe blue_y, Keyframe alpha_x, Keyframe alpha_y) : + red_x(red_x), red_y(red_y), green_x(green_x), green_y(green_y), blue_x(blue_x), blue_y(blue_y), alpha_x(alpha_x), alpha_y(alpha_y) +{ + // Init effect properties + init_effect_details(); +} + +// Init effect settings +void ColorShift::init_effect_details() +{ + /// Initialize the values of the EffectInfo struct. + InitEffectInfo(); + + /// Set the effect info + info.class_name = "Color Shift"; + info.name = "Color Shift"; + info.description = "Shift the colors of an image up, down, left, and right (with infinite wrapping)."; + info.has_audio = false; + info.has_video = true; +} + +// This method is required for all derived classes of EffectBase, and returns a +// modified openshot::Frame object +std::shared_ptr ColorShift::GetFrame(std::shared_ptr frame, int64_t frame_number) +{ + // Get the frame's image + std::shared_ptr frame_image = frame->GetImage(); + unsigned char *pixels = (unsigned char *) frame_image->bits(); + + // Get image size + int frame_image_width = frame_image->width(); + int frame_image_height = frame_image->height(); + + // Get the current shift amount, and clamp to range (-1 to 1 range) + // Red Keyframes + float red_x_shift = red_x.GetValue(frame_number); + int red_x_shift_limit = round(frame_image_width * fmod(abs(red_x_shift), 1.0)); + float red_y_shift = red_y.GetValue(frame_number); + int red_y_shift_limit = round(frame_image_height * fmod(abs(red_y_shift), 1.0)); + // Green Keyframes + float green_x_shift = green_x.GetValue(frame_number); + int green_x_shift_limit = round(frame_image_width * fmod(abs(green_x_shift), 1.0)); + float green_y_shift = green_y.GetValue(frame_number); + int green_y_shift_limit = round(frame_image_height * fmod(abs(green_y_shift), 1.0)); + // Blue Keyframes + float blue_x_shift = blue_x.GetValue(frame_number); + int blue_x_shift_limit = round(frame_image_width * fmod(abs(blue_x_shift), 1.0)); + float blue_y_shift = blue_y.GetValue(frame_number); + int blue_y_shift_limit = round(frame_image_height * fmod(abs(blue_y_shift), 1.0)); + // Alpha Keyframes + float alpha_x_shift = alpha_x.GetValue(frame_number); + int alpha_x_shift_limit = round(frame_image_width * fmod(abs(alpha_x_shift), 1.0)); + float alpha_y_shift = alpha_y.GetValue(frame_number); + int alpha_y_shift_limit = round(frame_image_height * fmod(abs(alpha_y_shift), 1.0)); + + + // Make temp copy of pixels + unsigned char *temp_image = new unsigned char[frame_image_width * frame_image_height * 4](); + memcpy(temp_image, pixels, sizeof(char) * frame_image_width * frame_image_height * 4); + + // Init position of current row and pixel + int starting_row_index = 0; + int byte_index = 0; + + // Init RGBA values + unsigned char R = 0; + unsigned char G = 0; + unsigned char B = 0; + unsigned char A = 0; + + int red_starting_row_index = 0; + int green_starting_row_index = 0; + int blue_starting_row_index = 0; + int alpha_starting_row_index = 0; + + int red_pixel_offset = 0; + int green_pixel_offset = 0; + int blue_pixel_offset = 0; + int alpha_pixel_offset = 0; + + // Loop through rows of pixels + for (int row = 0; row < frame_image_height; row++) { + for (int col = 0; col < frame_image_width; col++) { + // Get position of current row and pixel + starting_row_index = row * frame_image_width * 4; + byte_index = starting_row_index + (col * 4); + red_starting_row_index = starting_row_index; + green_starting_row_index = starting_row_index; + blue_starting_row_index = starting_row_index; + alpha_starting_row_index = starting_row_index; + + + red_pixel_offset = 0; + green_pixel_offset = 0; + blue_pixel_offset = 0; + alpha_pixel_offset = 0; + + // Get the RGBA value from each pixel (depending on offset) + R = temp_image[byte_index]; + G = temp_image[byte_index + 1]; + B = temp_image[byte_index + 2]; + A = temp_image[byte_index + 3]; + + // Shift X + if (red_x_shift > 0.0) + red_pixel_offset = (col + red_x_shift_limit) % frame_image_width; + if (red_x_shift < 0.0) + red_pixel_offset = (frame_image_width + col - red_x_shift_limit) % frame_image_width; + if (green_x_shift > 0.0) + green_pixel_offset = (col + green_x_shift_limit) % frame_image_width; + if (green_x_shift < 0.0) + green_pixel_offset = (frame_image_width + col - green_x_shift_limit) % frame_image_width; + if (blue_x_shift > 0.0) + blue_pixel_offset = (col + blue_x_shift_limit) % frame_image_width; + if (blue_x_shift < 0.0) + blue_pixel_offset = (frame_image_width + col - blue_x_shift_limit) % frame_image_width; + if (alpha_x_shift > 0.0) + alpha_pixel_offset = (col + alpha_x_shift_limit) % frame_image_width; + if (alpha_x_shift < 0.0) + alpha_pixel_offset = (frame_image_width + col - alpha_x_shift_limit) % frame_image_width; + + // Shift Y + if (red_y_shift > 0.0) + red_starting_row_index = ((row + red_y_shift_limit) % frame_image_height) * frame_image_width * 4; + if (red_y_shift < 0.0) + red_starting_row_index = ((frame_image_height + row - red_y_shift_limit) % frame_image_height) * frame_image_width * 4; + if (green_y_shift > 0.0) + green_starting_row_index = ((row + green_y_shift_limit) % frame_image_height) * frame_image_width * 4; + if (green_y_shift < 0.0) + green_starting_row_index = ((frame_image_height + row - green_y_shift_limit) % frame_image_height) * frame_image_width * 4; + if (blue_y_shift > 0.0) + blue_starting_row_index = ((row + blue_y_shift_limit) % frame_image_height) * frame_image_width * 4; + if (blue_y_shift < 0.0) + blue_starting_row_index = ((frame_image_height + row - blue_y_shift_limit) % frame_image_height) * frame_image_width * 4; + if (alpha_y_shift > 0.0) + alpha_starting_row_index = ((row + alpha_y_shift_limit) % frame_image_height) * frame_image_width * 4; + if (alpha_y_shift < 0.0) + alpha_starting_row_index = ((frame_image_height + row - alpha_y_shift_limit) % frame_image_height) * frame_image_width * 4; + + // Copy new values to this pixel + pixels[red_starting_row_index + 0 + (red_pixel_offset * 4)] = R; + pixels[green_starting_row_index + 1 + (green_pixel_offset * 4)] = G; + pixels[blue_starting_row_index + 2 + (blue_pixel_offset * 4)] = B; + pixels[alpha_starting_row_index + 3 + (alpha_pixel_offset * 4)] = A; + } + } + + // Delete arrays + delete[] temp_image; + + // return the modified frame + return frame; +} + +// Generate JSON string of this object +string ColorShift::Json() { + + // Return formatted string + return JsonValue().toStyledString(); +} + +// Generate Json::JsonValue for this object +Json::Value ColorShift::JsonValue() { + + // Create root json object + Json::Value root = EffectBase::JsonValue(); // get parent properties + root["type"] = info.class_name; + root["red_x"] = red_x.JsonValue(); + root["red_y"] = red_y.JsonValue(); + root["green_x"] = green_x.JsonValue(); + root["green_y"] = green_y.JsonValue(); + root["blue_x"] = blue_x.JsonValue(); + root["blue_y"] = blue_y.JsonValue(); + root["alpha_x"] = alpha_x.JsonValue(); + root["alpha_y"] = alpha_y.JsonValue(); + + // return JsonValue + return root; +} + +// Load JSON string into this object +void ColorShift::SetJson(string value) { + + // Parse JSON string into JSON objects + Json::Value root; + Json::Reader reader; + bool success = reader.parse( value, root ); + if (!success) + // Raise exception + throw InvalidJSON("JSON could not be parsed (or is invalid)", ""); + + try + { + // Set all values that match + SetJsonValue(root); + } + catch (exception e) + { + // Error parsing JSON (or missing keys) + throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", ""); + } +} + +// Load Json::JsonValue into this object +void ColorShift::SetJsonValue(Json::Value root) { + + // Set parent data + EffectBase::SetJsonValue(root); + + // Set data from Json (if key is found) + if (!root["red_x"].isNull()) + red_x.SetJsonValue(root["red_x"]); + if (!root["red_y"].isNull()) + red_y.SetJsonValue(root["red_y"]); + if (!root["green_x"].isNull()) + green_x.SetJsonValue(root["green_x"]); + if (!root["green_y"].isNull()) + green_y.SetJsonValue(root["green_y"]); + if (!root["blue_x"].isNull()) + blue_x.SetJsonValue(root["blue_x"]); + if (!root["blue_y"].isNull()) + blue_y.SetJsonValue(root["blue_y"]); + if (!root["alpha_x"].isNull()) + alpha_x.SetJsonValue(root["alpha_x"]); + if (!root["alpha_y"].isNull()) + alpha_y.SetJsonValue(root["alpha_y"]); +} + +// Get all properties for a specific frame +string ColorShift::PropertiesJSON(int64_t requested_frame) { + + // Generate JSON properties list + Json::Value root; + root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame); + root["position"] = add_property_json("Position", Position(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame); + root["layer"] = add_property_json("Track", Layer(), "int", "", NULL, 0, 20, false, requested_frame); + root["start"] = add_property_json("Start", Start(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame); + root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame); + root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame); + + // Keyframes + root["red_x"] = add_property_json("Red X Shift", red_x.GetValue(requested_frame), "float", "", &red_x, -1, 1, false, requested_frame); + root["red_y"] = add_property_json("Red Y Shift", red_y.GetValue(requested_frame), "float", "", &red_y, -1, 1, false, requested_frame); + root["green_x"] = add_property_json("Green X Shift", green_x.GetValue(requested_frame), "float", "", &green_x, -1, 1, false, requested_frame); + root["green_y"] = add_property_json("Green Y Shift", green_y.GetValue(requested_frame), "float", "", &green_y, -1, 1, false, requested_frame); + root["blue_x"] = add_property_json("Blue X Shift", blue_x.GetValue(requested_frame), "float", "", &blue_x, -1, 1, false, requested_frame); + root["blue_y"] = add_property_json("Blue Y Shift", blue_y.GetValue(requested_frame), "float", "", &blue_y, -1, 1, false, requested_frame); + root["alpha_x"] = add_property_json("Alpha X Shift", alpha_x.GetValue(requested_frame), "float", "", &alpha_x, -1, 1, false, requested_frame); + root["alpha_y"] = add_property_json("Alpha Y Shift", alpha_y.GetValue(requested_frame), "float", "", &alpha_y, -1, 1, false, requested_frame); + + // Return formatted string + return root.toStyledString(); +} +