@@ -37,16 +37,20 @@ std::optional<FrameDims> ResizeTransform::getOutputFrameDims() const {
3737 return outputDims_;
3838}
3939
40+ CropTransform::CropTransform (const FrameDims& dims) : outputDims_(dims) {}
41+
4042CropTransform::CropTransform (const FrameDims& dims, int x, int y)
4143 : outputDims_(dims), x_(x), y_(y) {
4244 TORCH_CHECK (x_ >= 0 , " Crop x position must be >= 0, got: " , x_);
4345 TORCH_CHECK (y_ >= 0 , " Crop y position must be >= 0, got: " , y_);
4446}
4547
4648std::string CropTransform::getFilterGraphCpu () const {
49+ std::string coordinates = x_.has_value ()
50+ ? (" :" + std::to_string (x_.value ()) + " :" + std::to_string (y_.value ()))
51+ : " " ;
4752 return " crop=" + std::to_string (outputDims_.width ) + " :" +
48- std::to_string (outputDims_.height ) + " :" + std::to_string (x_) + " :" +
49- std::to_string (y_) + " :exact=1" ;
53+ std::to_string (outputDims_.height ) + coordinates + " :exact=1" ;
5054}
5155
5256std::optional<FrameDims> CropTransform::getOutputFrameDims () const {
@@ -69,29 +73,34 @@ void CropTransform::validate(const FrameDims& inputDims) const {
6973 inputDims.width ,
7074 " )" );
7175 TORCH_CHECK (
72- x_ <= inputDims.width ,
73- " Crop x start position, " ,
74- x_,
75- " , out of bounds of input width, " ,
76- inputDims.width );
77- TORCH_CHECK (
78- x_ + outputDims_.width <= inputDims.width ,
79- " Crop x end position, " ,
80- x_ + outputDims_.width ,
81- " , out of bounds of input width " ,
82- inputDims.width );
83- TORCH_CHECK (
84- y_ <= inputDims.height ,
85- " Crop y start position, " ,
86- y_,
87- " , out of bounds of input height, " ,
88- inputDims.height );
89- TORCH_CHECK (
90- y_ + outputDims_.height <= inputDims.height ,
91- " Crop y end position, " ,
92- y_ + outputDims_.height ,
93- " , out of bounds of input height " ,
94- inputDims.height );
76+ x_.has_value () == y_.has_value (),
77+ " Crop x and y values must be both set or both unset" );
78+ if (x_.has_value ()) {
79+ TORCH_CHECK (
80+ x_.value () <= inputDims.width ,
81+ " Crop x start position, " ,
82+ x_.value (),
83+ " , out of bounds of input width, " ,
84+ inputDims.width );
85+ TORCH_CHECK (
86+ x_.value () + outputDims_.width <= inputDims.width ,
87+ " Crop x end position, " ,
88+ x_.value () + outputDims_.width ,
89+ " , out of bounds of input width " ,
90+ inputDims.width );
91+ TORCH_CHECK (
92+ y_.value () <= inputDims.height ,
93+ " Crop y start position, " ,
94+ y_.value (),
95+ " , out of bounds of input height, " ,
96+ inputDims.height );
97+ TORCH_CHECK (
98+ y_.value () + outputDims_.height <= inputDims.height ,
99+ " Crop y end position, " ,
100+ y_.value () + outputDims_.height ,
101+ " , out of bounds of input height " ,
102+ inputDims.height );
103+ }
95104}
96105
97106} // namespace facebook::torchcodec
0 commit comments