Loading...
Searching...
No Matches
Shift.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "Shift.h"
#include "Exceptions.h"
using namespace openshot;
Shift::Shift() : x(0.0), y(0.0) {
// Init effect properties
init_effect_details();
}
// Default constructor
Shift::Shift(Keyframe x, Keyframe y) : x(x), y(y)
{
// Init effect properties
init_effect_details();
}
// Init effect settings
void Shift::init_effect_details()
{
InitEffectInfo();
info.class_name = "Shift";
info.name = "Shift";
info.description = "Shift the 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<openshot::Frame> Shift::GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number)
{
// Get the frame's image
std::shared_ptr<QImage> frame_image = frame->GetImage();
unsigned char *pixels = (unsigned char *) frame_image->bits();
// Get the current shift amount, and clamp to range (-1 to 1 range)
double x_shift = x.GetValue(frame_number);
double x_shift_limit = fmod(fabs(x_shift), 1.0);
double y_shift = y.GetValue(frame_number);
double y_shift_limit = fmod(fabs(y_shift), 1.0);
// Declare temp arrays to hold pixels while we move things around
unsigned char *temp_row = new unsigned char[frame_image->width() * 4]();
// X-SHIFT
// Loop through rows
for (int row = 0; row < frame_image->height(); row++) {
// Copy current row's pixels
int starting_row_pixel = row * frame_image->width();
memcpy(temp_row, &pixels[starting_row_pixel * 4], sizeof(char) * frame_image->width() * 4);
// Replace current row with left part of the pixels
if (x_shift > 0.0) {
// Move left side to the right
int relative_pixel_start = (int)round(frame_image->width() * x_shift_limit);
memcpy(&pixels[(starting_row_pixel + relative_pixel_start) * 4], &temp_row[0], sizeof(char) * (frame_image->width() - relative_pixel_start) * 4);
// Move right side to the left
memcpy(&pixels[starting_row_pixel * 4], &temp_row[(frame_image->width() - relative_pixel_start) * 4], sizeof(char) * relative_pixel_start * 4);
} else if (x_shift < 0.0) {
// Move right side to the left
int relative_pixel_start = (int)round(frame_image->width() * x_shift_limit);
memcpy(&pixels[starting_row_pixel * 4], &temp_row[relative_pixel_start * 4], sizeof(char) * (frame_image->width() - relative_pixel_start) * 4);
// Move left side to the right
memcpy(&pixels[(starting_row_pixel + (frame_image->width() - relative_pixel_start)) * 4], &temp_row[0], sizeof(char) * relative_pixel_start * 4);
}
}
// Make temp copy of pixels for Y-SHIFT
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);
// Y-SHIFT
// Replace current row with left part of the pixels
if (y_shift > 0.0) {
// Move top side to bottom
int relative_pixel_start = frame_image->width() * (int)round(frame_image->height() * y_shift_limit);
memcpy(&pixels[relative_pixel_start * 4], temp_image, sizeof(char) * ((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4);
// Move bottom side to top
memcpy(pixels, &temp_image[((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4], sizeof(char) * relative_pixel_start * 4);
} else if (y_shift < 0.0) {
// Move bottom side to top
int relative_pixel_start = frame_image->width() * (int)round(frame_image->height() * y_shift_limit);
memcpy(pixels, &temp_image[relative_pixel_start * 4], sizeof(char) * ((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4);
// Move left side to the right
memcpy(&pixels[((frame_image->width() * frame_image->height()) - relative_pixel_start) * 4], temp_image, sizeof(char) * relative_pixel_start * 4);
}
// Delete arrays
delete[] temp_row;
delete[] temp_image;
// return the modified frame
return frame;
}
// Generate JSON string of this object
std::string Shift::Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value Shift::JsonValue() const {
// Create root json object
Json::Value root = EffectBase::JsonValue(); // get parent properties
root["type"] = info.class_name;
root["x"] = x.JsonValue();
root["y"] = y.JsonValue();
// return JsonValue
return root;
}
// Load JSON string into this object
void Shift::SetJson(const std::string value) {
// Parse JSON string into JSON objects
try
{
const Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
catch (const std::exception& e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
}
// Load Json::Value into this object
void Shift::SetJsonValue(const Json::Value root) {
// Set parent data
EffectBase::SetJsonValue(root);
// Set data from Json (if key is found)
if (!root["x"].isNull())
x.SetJsonValue(root["x"]);
if (!root["y"].isNull())
y.SetJsonValue(root["y"]);
}
// Get all properties for a specific frame
std::string Shift::PropertiesJSON(int64_t requested_frame) const {
// 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["x"] = add_property_json("X Shift", x.GetValue(requested_frame), "float", "", &x, -1, 1, false, requested_frame);
root["y"] = add_property_json("Y Shift", y.GetValue(requested_frame), "float", "", &y, -1, 1, false, requested_frame);
// Set the parent effect which properties this effect will inherit
root["parent_effect_id"] = add_property_json("Parent", 0.0, "string", info.parent_effect_id, NULL, -1, -1, false, requested_frame);
// Return formatted string
return root.toStyledString();
}
Exceptions.h
Header file for all Exception classes.
Shift.h
Header file for Shift effect class.
openshot::ClipBase::Start
float Start() const
Get start position (in seconds) of clip (trim start of video)
Definition ClipBase.h:88
openshot::ClipBase::Duration
float Duration() const
Get the length of this clip (in seconds)
Definition ClipBase.h:90
openshot::ClipBase::End
virtual float End() const
Get end position (in seconds) of clip (trim end of video)
Definition ClipBase.h:89
openshot::ClipBase::Id
std::string Id() const
Get the Id of this clip object.
Definition ClipBase.h:85
openshot::ClipBase::Layer
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition ClipBase.h:87
openshot::ClipBase::Position
float Position() const
Get position on timeline (in seconds)
Definition ClipBase.h:86
openshot::ClipBase::add_property_json
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const
Generate JSON for a property.
Definition ClipBase.cpp:96
openshot::EffectBase::JsonValue
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
Definition EffectBase.cpp:77
openshot::EffectBase::InitEffectInfo
void InitEffectInfo()
Definition EffectBase.cpp:24
openshot::EffectBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition EffectBase.cpp:112
openshot::EffectBase::info
EffectInfoStruct info
Information about the current effect.
Definition EffectBase.h:69
openshot::InvalidJSON
Exception for invalid JSON.
Definition Exceptions.h:218
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition KeyFrame.h:53
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition KeyFrame.cpp:372
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition KeyFrame.cpp:258
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition KeyFrame.cpp:339
openshot::Shift::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number) override
This method is required for all derived classes of ClipBase, and returns a new openshot::Frame object...
Definition Shift.h:62
openshot::Shift::y
Keyframe y
Shift the Y coordinates (up or down)
Definition Shift.h:45
openshot::Shift::Json
std::string Json() const override
Generate JSON string of this object.
Definition Shift.cpp:119
openshot::Shift::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition Shift.cpp:126
openshot::Shift::x
Keyframe x
Shift the X coordinates (left or right)
Definition Shift.h:44
openshot::Shift::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition Shift.cpp:156
openshot::Shift::PropertiesJSON
std::string PropertiesJSON(int64_t requested_frame) const override
Definition Shift.cpp:169
openshot::Shift::Shift
Shift()
Blank constructor, useful when using Json to load the effect properties.
Definition Shift.cpp:19
openshot::Shift::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Shift.cpp:139
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
openshot::EffectInfoStruct::has_video
bool has_video
Determines if this effect manipulates the image of a frame.
Definition EffectBase.h:40
openshot::EffectInfoStruct::parent_effect_id
std::string parent_effect_id
Id of the parent effect (if there is one)
Definition EffectBase.h:39
openshot::EffectInfoStruct::has_audio
bool has_audio
Determines if this effect manipulates the audio of a frame.
Definition EffectBase.h:41
openshot::EffectInfoStruct::class_name
std::string class_name
The class name of the effect.
Definition EffectBase.h:36
openshot::EffectInfoStruct::name
std::string name
The name of the effect.
Definition EffectBase.h:37
openshot::EffectInfoStruct::description
std::string description
The description of this effect and what it does.
Definition EffectBase.h:38