|
| 1 | +#include "opentimelineio/imageSequenceReference.h" |
| 2 | + |
| 3 | +namespace opentimelineio { namespace OPENTIMELINEIO_VERSION { |
| 4 | + |
| 5 | +ImageSequenceReference::ImageSequenceReference(std::string const& target_url_base, |
| 6 | + std::string const& name_prefix, |
| 7 | + std::string const& name_suffix, |
| 8 | + int start_frame, |
| 9 | + int frame_step, |
| 10 | + double const rate, |
| 11 | + int frame_zero_padding, |
| 12 | + MissingFramePolicy const missing_frame_policy, |
| 13 | + optional<TimeRange> const& available_range, |
| 14 | + AnyDictionary const& metadata) |
| 15 | + : Parent(std::string(), available_range, metadata), |
| 16 | + _target_url_base(target_url_base), |
| 17 | + _name_prefix(name_prefix), |
| 18 | + _name_suffix(name_suffix), |
| 19 | + _start_frame {start_frame}, |
| 20 | + _frame_step {frame_step}, |
| 21 | + _rate {rate}, |
| 22 | + _frame_zero_padding {frame_zero_padding}, |
| 23 | + _missing_frame_policy {missing_frame_policy} { |
| 24 | + } |
| 25 | + |
| 26 | + ImageSequenceReference::~ImageSequenceReference() { |
| 27 | + } |
| 28 | + |
| 29 | + RationalTime |
| 30 | + ImageSequenceReference::frame_duration() const { |
| 31 | + return RationalTime((double)_frame_step, _rate); |
| 32 | + } |
| 33 | + |
| 34 | + int ImageSequenceReference::end_frame() const { |
| 35 | + if (!this->available_range().has_value()) { |
| 36 | + return _start_frame; |
| 37 | + } |
| 38 | + |
| 39 | + int num_frames = this->available_range().value().duration().to_frames(_rate); |
| 40 | + |
| 41 | + // Subtract 1 for inclusive frame ranges |
| 42 | + return (_start_frame + num_frames - 1); |
| 43 | + } |
| 44 | + |
| 45 | + int ImageSequenceReference::number_of_images_in_sequence() const { |
| 46 | + if (!this->available_range().has_value()) { |
| 47 | + return 0; |
| 48 | + } |
| 49 | + |
| 50 | + double playback_rate = (_rate / (double)_frame_step); |
| 51 | + int num_frames = this->available_range().value().duration().to_frames(playback_rate); |
| 52 | + return num_frames; |
| 53 | + } |
| 54 | + |
| 55 | + int ImageSequenceReference::frame_for_time(RationalTime const time, ErrorStatus* error_status) const { |
| 56 | + if (!this->available_range().has_value() || !this->available_range().value().contains(time)) { |
| 57 | + *error_status = ErrorStatus(ErrorStatus::INVALID_TIME_RANGE); |
| 58 | + return 0; |
| 59 | + } |
| 60 | + |
| 61 | + RationalTime start = this->available_range().value().start_time(); |
| 62 | + RationalTime duration_from_start = (time - start); |
| 63 | + int frame_offset = duration_from_start.to_frames(_rate); |
| 64 | + |
| 65 | + *error_status = ErrorStatus(ErrorStatus::OK); |
| 66 | + |
| 67 | + return (_start_frame + frame_offset); |
| 68 | + } |
| 69 | + |
| 70 | + std::string |
| 71 | + ImageSequenceReference::target_url_for_image_number(int const image_number, ErrorStatus* error_status) const { |
| 72 | + if (image_number >= this->number_of_images_in_sequence()) { |
| 73 | + *error_status = ErrorStatus(ErrorStatus::ILLEGAL_INDEX); |
| 74 | + return std::string(); |
| 75 | + } |
| 76 | + const int file_image_num = _start_frame + (image_number * _frame_step); |
| 77 | + const bool is_negative = (file_image_num < 0); |
| 78 | + |
| 79 | + std::string image_num_string = std::to_string(abs(file_image_num)); |
| 80 | + |
| 81 | + std::string zero_pad = std::string(); |
| 82 | + if (image_num_string.length() < _frame_zero_padding) { |
| 83 | + zero_pad = std::string(_frame_zero_padding - image_num_string.length(), '0'); |
| 84 | + } |
| 85 | + |
| 86 | + std::string sign = std::string(); |
| 87 | + if (is_negative) { |
| 88 | + sign = "-"; |
| 89 | + } |
| 90 | + |
| 91 | + // If the base does not include a trailing slash, add it |
| 92 | + std::string path_sep = std::string(); |
| 93 | + if (_target_url_base.compare(_target_url_base.length() - 1, 1, "/") != 0) { |
| 94 | + path_sep = "/"; |
| 95 | + } |
| 96 | + |
| 97 | + std::string out_string = _target_url_base + path_sep + _name_prefix + sign + zero_pad + image_num_string + _name_suffix; |
| 98 | + *error_status = ErrorStatus(ErrorStatus::OK); |
| 99 | + return out_string; |
| 100 | + } |
| 101 | + |
| 102 | + RationalTime |
| 103 | + ImageSequenceReference::presentation_time_for_image_number(int const image_number, ErrorStatus* error_status) const { |
| 104 | + if (image_number >= this->number_of_images_in_sequence()) { |
| 105 | + *error_status = ErrorStatus(ErrorStatus::ILLEGAL_INDEX); |
| 106 | + return RationalTime(); |
| 107 | + } |
| 108 | + |
| 109 | + auto first_frame_time = this->available_range().value().start_time(); |
| 110 | + auto time_multiplier = TimeTransform(first_frame_time, image_number, -1); |
| 111 | + return time_multiplier.applied_to(frame_duration()); |
| 112 | + } |
| 113 | + |
| 114 | + bool ImageSequenceReference::read_from(Reader& reader) { |
| 115 | + auto result = reader.read("target_url_base", &_target_url_base) && |
| 116 | + reader.read("name_prefix", &_name_prefix) && |
| 117 | + reader.read("name_suffix", &_name_suffix) && |
| 118 | + reader.read("start_frame", &_start_frame) && |
| 119 | + reader.read("frame_step", &_frame_step) && |
| 120 | + reader.read("rate", &_rate) && |
| 121 | + reader.read("frame_zero_padding", &_frame_zero_padding); |
| 122 | + |
| 123 | + std::string missing_frame_policy_value; |
| 124 | + result && reader.read("missing_frame_policy", &missing_frame_policy_value); |
| 125 | + if (!result) { |
| 126 | + return result; |
| 127 | + } |
| 128 | + |
| 129 | + if (missing_frame_policy_value == "error") { |
| 130 | + _missing_frame_policy = MissingFramePolicy::error; |
| 131 | + } |
| 132 | + else if (missing_frame_policy_value == "black") { |
| 133 | + _missing_frame_policy = MissingFramePolicy::black; |
| 134 | + } |
| 135 | + else if (missing_frame_policy_value == "hold") { |
| 136 | + _missing_frame_policy = MissingFramePolicy::hold; |
| 137 | + } |
| 138 | + else { |
| 139 | + // Unrecognized value |
| 140 | + ErrorStatus error_status = ErrorStatus(ErrorStatus::JSON_PARSE_ERROR, |
| 141 | + "Unknown missing_frame_policy: " + missing_frame_policy_value); |
| 142 | + reader.error(error_status); |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + return result && Parent::read_from(reader); |
| 147 | + } |
| 148 | + |
| 149 | + void ImageSequenceReference::write_to(Writer& writer) const { |
| 150 | + Parent::write_to(writer); |
| 151 | + writer.write("target_url_base", _target_url_base); |
| 152 | + writer.write("name_prefix", _name_prefix); |
| 153 | + writer.write("name_suffix", _name_suffix); |
| 154 | + writer.write("start_frame", _start_frame); |
| 155 | + writer.write("frame_step", _frame_step); |
| 156 | + writer.write("rate", _rate); |
| 157 | + writer.write("frame_zero_padding", _frame_zero_padding); |
| 158 | + |
| 159 | + std::string missing_frame_policy_value; |
| 160 | + switch (_missing_frame_policy) |
| 161 | + { |
| 162 | + case MissingFramePolicy::error: |
| 163 | + missing_frame_policy_value = "error"; |
| 164 | + break; |
| 165 | + case MissingFramePolicy::black: |
| 166 | + missing_frame_policy_value = "black"; |
| 167 | + break; |
| 168 | + case MissingFramePolicy::hold: |
| 169 | + missing_frame_policy_value = "hold"; |
| 170 | + break; |
| 171 | + } |
| 172 | + writer.write("missing_frame_policy", missing_frame_policy_value); |
| 173 | + } |
| 174 | +} } |
0 commit comments