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_IMAGECOLORSPACE_OP_H_ 17 #define TENSORFLOW_CORE_KERNELS_IMAGECOLORSPACE_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 Eigen::IndexList<Eigen::type2index<1> > channel_axis; 42 43 V.device(d) = input_data.maximum(channel_axis); 44 45 range.device(d) = V - input_data.minimum(channel_axis); 46 47 S.device(d) = (V > T(0)).select(range / V, V.constant(T(0))); 48 49 auto norm = range.inverse() * (T(1) / T(6)); 50 // TODO(wicke): all these assignments are only necessary because a combined 51 // expression is larger than kernel parameter space. A custom kernel is 52 // probably in order. 53 H.device(d) = (R == V).select( 54 norm * (G - B), (G == V).select(norm * (B - R) + T(2) / T(6), 55 norm * (R - G) + T(4) / T(6))); 56 H.device(d) = (range > T(0)).select(H, H.constant(T(0))); 57 H.device(d) = (H < T(0)).select(H + T(1), H); 58 } 59 }; 60 61 template <typename Device, typename T> 62 struct HSVToRGB { operatorHSVToRGB63 void operator()(const Device &d, 64 typename TTypes<T, 2>::ConstTensor input_data, 65 typename TTypes<T, 2>::Tensor output_data) { 66 auto H = input_data.template chip<1>(0); 67 auto S = input_data.template chip<1>(1); 68 auto V = input_data.template chip<1>(2); 69 70 // TODO(wicke): compute only the fractional part of H for robustness 71 auto dh = H * T(6); 72 auto dr = ((dh - T(3)).abs() - T(1)).cwiseMax(T(0)).cwiseMin(T(1)); 73 auto dg = (-(dh - T(2)).abs() + T(2)).cwiseMax(T(0)).cwiseMin(T(1)); 74 auto db = (-(dh - T(4)).abs() + T(2)).cwiseMax(T(0)).cwiseMin(T(1)); 75 auto one_s = -S + T(1); 76 77 auto R = output_data.template chip<1>(0); 78 auto G = output_data.template chip<1>(1); 79 auto B = output_data.template chip<1>(2); 80 81 R.device(d) = (one_s + S * dr) * V; 82 G.device(d) = (one_s + S * dg) * V; 83 B.device(d) = (one_s + S * db) * V; 84 } 85 }; 86 87 } // namespace functor 88 } // namespace tensorflow 89 90 #endif // TENSORFLOW_CORE_KERNELS_IMAGECOLORSPACE_OP_H_ 91