1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_CORE_KERNELS_COLORSPACE_OP_H_ 17 #define TENSORFLOW_CORE_KERNELS_COLORSPACE_OP_H_ 18 19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 20 #include "tensorflow/core/framework/tensor_shape.h" 21 #include "tensorflow/core/framework/tensor_types.h" 22 23 namespace tensorflow { 24 25 namespace functor { 26 27 template <typename Device, typename T> 28 struct RGBToHSV { operatorRGBToHSV29 void operator()(const Device &d, 30 typename TTypes<T, 2>::ConstTensor input_data, 31 typename TTypes<T, 1>::Tensor range, 32 typename TTypes<T, 2>::Tensor output_data) { 33 auto H = output_data.template chip<1>(0); 34 auto S = output_data.template chip<1>(1); 35 auto V = output_data.template chip<1>(2); 36 37 auto R = input_data.template chip<1>(0); 38 auto G = input_data.template chip<1>(1); 39 auto B = input_data.template chip<1>(2); 40 41 #if !defined(EIGEN_HAS_INDEX_LIST) 42 Eigen::array<int, 1> channel_axis{{1}}; 43 #else 44 Eigen::IndexList<Eigen::type2index<1> > channel_axis; 45 #endif 46 47 V.device(d) = input_data.maximum(channel_axis); 48 49 range.device(d) = V - input_data.minimum(channel_axis); 50 51 S.device(d) = (V > T(0)).select(range / V, V.constant(T(0))); 52 53 auto norm = range.inverse() * (T(1) / T(6)); 54 // TODO(wicke): all these assignments are only necessary because a combined 55 // expression is larger than kernel parameter space. A custom kernel is 56 // probably in order. 57 H.device(d) = (R == V).select( 58 norm * (G - B), (G == V).select(norm * (B - R) + T(2) / T(6), 59 norm * (R - G) + T(4) / T(6))); 60 H.device(d) = (range > T(0)).select(H, H.constant(T(0))); 61 H.device(d) = (H < T(0)).select(H + T(1), H); 62 } 63 }; 64 65 template <typename Device, typename T> 66 struct HSVToRGB { operatorHSVToRGB67 void operator()(const Device &d, 68 typename TTypes<T, 2>::ConstTensor input_data, 69 typename TTypes<T, 2>::Tensor output_data) { 70 auto H = input_data.template chip<1>(0); 71 auto S = input_data.template chip<1>(1); 72 auto V = input_data.template chip<1>(2); 73 74 // TODO(wicke): compute only the fractional part of H for robustness 75 auto dh = H * T(6); 76 auto dr = ((dh - T(3)).abs() - T(1)).cwiseMax(T(0)).cwiseMin(T(1)); 77 auto dg = (-(dh - T(2)).abs() + T(2)).cwiseMax(T(0)).cwiseMin(T(1)); 78 auto db = (-(dh - T(4)).abs() + T(2)).cwiseMax(T(0)).cwiseMin(T(1)); 79 auto one_s = -S + T(1); 80 81 auto R = output_data.template chip<1>(0); 82 auto G = output_data.template chip<1>(1); 83 auto B = output_data.template chip<1>(2); 84 85 R.device(d) = (one_s + S * dr) * V; 86 G.device(d) = (one_s + S * dg) * V; 87 B.device(d) = (one_s + S * db) * V; 88 } 89 }; 90 91 } // namespace functor 92 } // namespace tensorflow 93 94 #endif // TENSORFLOW_CORE_KERNELS_COLORSPACE_OP_H_ 95