31 #include "../../include/effects/ColorShift.h" 36 ColorShift::ColorShift() : red_x(-0.05), red_y(0.0), green_x(0.05), green_y(0.0), blue_x(0.0), blue_y(0.0), alpha_x(0.0), alpha_y(0.0) {
38 init_effect_details();
43 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)
46 init_effect_details();
50 void ColorShift::init_effect_details()
58 info.
description =
"Shift the colors of an image up, down, left, and right (with infinite wrapping).";
68 std::shared_ptr<QImage> frame_image = frame->GetImage();
69 unsigned char *pixels = (
unsigned char *) frame_image->bits();
72 int frame_image_width = frame_image->width();
73 int frame_image_height = frame_image->height();
78 int red_x_shift_limit = round(frame_image_width * fmod(abs(red_x_shift), 1.0));
80 int red_y_shift_limit = round(frame_image_height * fmod(abs(red_y_shift), 1.0));
83 int green_x_shift_limit = round(frame_image_width * fmod(abs(green_x_shift), 1.0));
85 int green_y_shift_limit = round(frame_image_height * fmod(abs(green_y_shift), 1.0));
88 int blue_x_shift_limit = round(frame_image_width * fmod(abs(blue_x_shift), 1.0));
90 int blue_y_shift_limit = round(frame_image_height * fmod(abs(blue_y_shift), 1.0));
93 int alpha_x_shift_limit = round(frame_image_width * fmod(abs(alpha_x_shift), 1.0));
95 int alpha_y_shift_limit = round(frame_image_height * fmod(abs(alpha_y_shift), 1.0));
99 unsigned char *temp_image =
new unsigned char[frame_image_width * frame_image_height * 4]();
100 memcpy(temp_image, pixels,
sizeof(
char) * frame_image_width * frame_image_height * 4);
103 int starting_row_index = 0;
112 int red_starting_row_index = 0;
113 int green_starting_row_index = 0;
114 int blue_starting_row_index = 0;
115 int alpha_starting_row_index = 0;
117 int red_pixel_offset = 0;
118 int green_pixel_offset = 0;
119 int blue_pixel_offset = 0;
120 int alpha_pixel_offset = 0;
123 for (
int row = 0; row < frame_image_height; row++) {
124 for (
int col = 0; col < frame_image_width; col++) {
126 starting_row_index = row * frame_image_width * 4;
127 byte_index = starting_row_index + (col * 4);
128 red_starting_row_index = starting_row_index;
129 green_starting_row_index = starting_row_index;
130 blue_starting_row_index = starting_row_index;
131 alpha_starting_row_index = starting_row_index;
134 red_pixel_offset = 0;
135 green_pixel_offset = 0;
136 blue_pixel_offset = 0;
137 alpha_pixel_offset = 0;
140 R = temp_image[byte_index];
141 G = temp_image[byte_index + 1];
142 B = temp_image[byte_index + 2];
143 A = temp_image[byte_index + 3];
146 if (red_x_shift > 0.0)
147 red_pixel_offset = (col + red_x_shift_limit) % frame_image_width;
148 if (red_x_shift < 0.0)
149 red_pixel_offset = (frame_image_width + col - red_x_shift_limit) % frame_image_width;
150 if (green_x_shift > 0.0)
151 green_pixel_offset = (col + green_x_shift_limit) % frame_image_width;
152 if (green_x_shift < 0.0)
153 green_pixel_offset = (frame_image_width + col - green_x_shift_limit) % frame_image_width;
154 if (blue_x_shift > 0.0)
155 blue_pixel_offset = (col + blue_x_shift_limit) % frame_image_width;
156 if (blue_x_shift < 0.0)
157 blue_pixel_offset = (frame_image_width + col - blue_x_shift_limit) % frame_image_width;
158 if (alpha_x_shift > 0.0)
159 alpha_pixel_offset = (col + alpha_x_shift_limit) % frame_image_width;
160 if (alpha_x_shift < 0.0)
161 alpha_pixel_offset = (frame_image_width + col - alpha_x_shift_limit) % frame_image_width;
164 if (red_y_shift > 0.0)
165 red_starting_row_index = ((row + red_y_shift_limit) % frame_image_height) * frame_image_width * 4;
166 if (red_y_shift < 0.0)
167 red_starting_row_index = ((frame_image_height + row - red_y_shift_limit) % frame_image_height) * frame_image_width * 4;
168 if (green_y_shift > 0.0)
169 green_starting_row_index = ((row + green_y_shift_limit) % frame_image_height) * frame_image_width * 4;
170 if (green_y_shift < 0.0)
171 green_starting_row_index = ((frame_image_height + row - green_y_shift_limit) % frame_image_height) * frame_image_width * 4;
172 if (blue_y_shift > 0.0)
173 blue_starting_row_index = ((row + blue_y_shift_limit) % frame_image_height) * frame_image_width * 4;
174 if (blue_y_shift < 0.0)
175 blue_starting_row_index = ((frame_image_height + row - blue_y_shift_limit) % frame_image_height) * frame_image_width * 4;
176 if (alpha_y_shift > 0.0)
177 alpha_starting_row_index = ((row + alpha_y_shift_limit) % frame_image_height) * frame_image_width * 4;
178 if (alpha_y_shift < 0.0)
179 alpha_starting_row_index = ((frame_image_height + row - alpha_y_shift_limit) % frame_image_height) * frame_image_width * 4;
182 pixels[red_starting_row_index + 0 + (red_pixel_offset * 4)] = R;
183 pixels[green_starting_row_index + 1 + (green_pixel_offset * 4)] = G;
184 pixels[blue_starting_row_index + 2 + (blue_pixel_offset * 4)] = B;
185 pixels[alpha_starting_row_index + 3 + (alpha_pixel_offset * 4)] = A;
227 Json::CharReaderBuilder rbuilder;
228 Json::CharReader* reader(rbuilder.newCharReader());
231 bool success = reader->parse( value.c_str(),
232 value.c_str() + value.size(), &root, &errors );
237 throw InvalidJSON(
"JSON could not be parsed (or is invalid)");
244 catch (
const std::exception& e)
247 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
258 if (!root[
"red_x"].isNull())
260 if (!root[
"red_y"].isNull())
262 if (!root[
"green_x"].isNull())
264 if (!root[
"green_y"].isNull())
266 if (!root[
"blue_x"].isNull())
268 if (!root[
"blue_y"].isNull())
270 if (!root[
"alpha_x"].isNull())
272 if (!root[
"alpha_y"].isNull())
281 root[
"id"] =
add_property_json(
"ID", 0.0,
"string",
Id(), NULL, -1, -1,
true, requested_frame);
282 root[
"position"] =
add_property_json(
"Position",
Position(),
"float",
"", NULL, 0, 1000 * 60 * 30,
false, requested_frame);
284 root[
"start"] =
add_property_json(
"Start",
Start(),
"float",
"", NULL, 0, 1000 * 60 * 30,
false, requested_frame);
285 root[
"end"] =
add_property_json(
"End",
End(),
"float",
"", NULL, 0, 1000 * 60 * 30,
false, requested_frame);
286 root[
"duration"] =
add_property_json(
"Duration",
Duration(),
"float",
"", NULL, 0, 1000 * 60 * 30,
true, requested_frame);
299 return root.toStyledString();
Keyframe alpha_y
Shift the Alpha Y coordinates (up or down)
Keyframe red_x
Shift the Red X coordinates (left or right)
Keyframe blue_x
Shift the Blue X coordinates (left or right)
Keyframe green_y
Shift the Green Y coordinates (up or down)
float End()
Get end position (in seconds) of clip (trim end of video)
Keyframe green_x
Shift the Green X coordinates (left or right)
int Layer()
Get layer of clip on timeline (lower number is covered by higher numbers)
virtual Json::Value JsonValue()=0
Generate Json::JsonValue for this object.
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
bool has_audio
Determines if this effect manipulates the audio of a frame.
std::shared_ptr< Frame > GetFrame(std::shared_ptr< Frame > frame, int64_t frame_number)
This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame...
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Keyframe alpha_x
Shift the Alpha X coordinates (left or right)
Keyframe red_y
Shift the Red Y coordinates (up or down)
std::string Id()
Get basic properties.
float Position()
Get position on timeline (in seconds)
std::string Json()
Get and Set JSON methods.
virtual void SetJsonValue(Json::Value root)=0
Load Json::JsonValue into this object.
std::string class_name
The class name of the effect.
std::string name
The name of the effect.
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame)
Generate JSON for a property.
This namespace is the default namespace for all code in the openshot library.
Json::Value JsonValue() const
Generate Json::JsonValue for this object.
std::string description
The description of this effect and what it does.
void SetJson(std::string value)
Load JSON string into this object.
bool has_video
Determines if this effect manipulates the image of a frame.
Exception for invalid JSON.
double GetValue(int64_t index) const
Get the value at a specific index.
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
std::string PropertiesJSON(int64_t requested_frame)
float Duration()
Get the length of this clip (in seconds)
float Start()
Get start position (in seconds) of clip (trim start of video)
EffectInfoStruct info
Information about the current effect.
Keyframe blue_y
Shift the Blue Y coordinates (up or down)
ColorShift()
Blank constructor, useful when using Json to load the effect properties.