| 1 |
/* |
| 2 |
* This file is a part of Luminance HDR package. |
| 3 |
* ---------------------------------------------------------------------- |
| 4 |
* Copyright (C) 2013 Davide Anastasia |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
* ---------------------------------------------------------------------- |
| 20 |
*/ |
| 21 |
|
| 22 |
//! \brief colorspace conversion base functions |
| 23 |
//! \author Davide Anastasia <[email protected]> |
| 24 |
|
| 25 |
#ifndef PFS_COLORSPACE_TRANSFORM_HXX |
| 26 |
#define PFS_COLORSPACE_TRANSFORM_HXX |
| 27 |
|
| 28 |
#include <Libpfs/utils/transform.h> |
| 29 |
#include <algorithm> |
| 30 |
#include <cassert> |
| 31 |
#include <iterator> |
| 32 |
|
| 33 |
namespace pfs { |
| 34 |
namespace utils { |
| 35 |
|
| 36 |
namespace detail { |
| 37 |
|
| 38 |
// transform for generic iterators |
| 39 |
template <typename InputIterator, typename OutputIterator, |
| 40 |
typename ConversionOperator, typename InputIteratorTag, |
| 41 |
typename OutputIteratorTag> |
| 42 |
void transform(InputIterator in1, InputIterator in1End, InputIterator in2, |
| 43 |
InputIterator in3, OutputIterator out1, OutputIterator out2, |
| 44 |
OutputIterator out3, ConversionOperator convOp, InputIteratorTag, |
| 45 |
OutputIteratorTag) { |
| 46 |
while (in1 != in1End) { |
| 47 |
convOp(*in1++, *in2++, *in3++, *out1++, *out2++, *out3++); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// transform for random_access_iterator_tag, so we can use OpenMP (optimized) |
| 52 |
template <typename InputIterator, typename OutputIterator, |
| 53 |
typename ConversionOperator> |
| 54 |
void transform(InputIterator in1, InputIterator in1End, InputIterator in2, |
| 55 |
InputIterator in3, OutputIterator out1, OutputIterator out2, |
| 56 |
OutputIterator out3, ConversionOperator &convOp, |
| 57 |
std::random_access_iterator_tag, |
| 58 |
std::random_access_iterator_tag) { |
| 59 |
typename std::iterator_traits<InputIterator>::difference_type numElem = |
| 60 |
(in1End - in1); |
| 61 |
#pragma omp parallel for |
| 62 |
for (int idx = 0; idx < numElem; ++idx) { |
| 63 |
convOp(in1[idx], in2[idx], in3[idx], out1[idx], out2[idx], out3[idx]); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
} // detail |
| 68 |
|
| 69 |
template <typename InputIterator, typename OutputIterator, |
| 70 |
typename ConversionOperator> |
| 71 |
void transform(InputIterator in1, InputIterator in1End, InputIterator in2, |
| 72 |
InputIterator in3, OutputIterator out1, OutputIterator out2, |
| 73 |
OutputIterator out3, ConversionOperator convOp) { |
| 74 |
// dispatch to best implementation! |
| 75 |
detail::transform( |
| 76 |
in1, in1End, in2, in3, out1, out2, out3, convOp, |
| 77 |
typename std::iterator_traits<InputIterator>::iterator_category(), |
| 78 |
typename std::iterator_traits<OutputIterator>::iterator_category()); |
| 79 |
} |
| 80 |
|
| 81 |
namespace detail { |
| 82 |
|
| 83 |
// transform for generic iterators |
| 84 |
template <typename InputIterator, typename OutputIterator, |
| 85 |
typename ConversionOperator, typename InputIteratorTag, |
| 86 |
typename OutputIteratorTag> |
| 87 |
void transform(InputIterator in1, InputIterator in1End, InputIterator in2, |
| 88 |
InputIterator in3, InputIterator in4, OutputIterator out1, |
| 89 |
OutputIterator out2, OutputIterator out3, |
| 90 |
ConversionOperator convOp, InputIteratorTag, OutputIteratorTag) { |
| 91 |
while (in1 != in1End) { |
| 92 |
convOp(*in1++, *in2++, *in3++, *in4++, *out1++, *out2++, *out3++); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// transform for random_access_iterator_tag, so we can use OpenMP (optimized) |
| 97 |
template <typename InputIterator, typename OutputIterator, |
| 98 |
typename ConversionOperator> |
| 99 |
void transform(InputIterator in1, InputIterator in1End, InputIterator in2, |
| 100 |
InputIterator in3, InputIterator in4, OutputIterator out1, |
| 101 |
OutputIterator out2, OutputIterator out3, |
| 102 |
ConversionOperator convOp, std::random_access_iterator_tag, |
| 103 |
std::random_access_iterator_tag) { |
| 104 |
typename std::iterator_traits<InputIterator>::difference_type numElem = |
| 105 |
(in1End - in1); |
| 106 |
#pragma omp parallel for |
| 107 |
for (int idx = 0; idx < numElem; ++idx) { |
| 108 |
convOp(in1[idx], in2[idx], in3[idx], in4[idx], out1[idx], out2[idx], |
| 109 |
out3[idx]); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
} // detail |
| 114 |
|
| 115 |
template <typename InputIterator, typename OutputIterator, |
| 116 |
typename ConversionOperator> |
| 117 |
void transform(InputIterator in1, InputIterator in1End, InputIterator in2, |
| 118 |
InputIterator in3, InputIterator in4, OutputIterator out1, |
| 119 |
OutputIterator out2, OutputIterator out3, |
| 120 |
ConversionOperator convOp) { |
| 121 |
// dispatch to best implementation! |
| 122 |
detail::transform( |
| 123 |
in1, in1End, in2, in3, in4, out1, out2, out3, convOp, |
| 124 |
typename std::iterator_traits<InputIterator>::iterator_category(), |
| 125 |
typename std::iterator_traits<OutputIterator>::iterator_category()); |
| 126 |
} |
| 127 |
|
| 128 |
namespace detail { |
| 129 |
|
| 130 |
// transform for generic iterators |
| 131 |
template <typename InputIterator, typename OutputIterator, |
| 132 |
typename ConversionOperator, typename InputIteratorTag, |
| 133 |
typename OutputIteratorTag> |
| 134 |
void transform(InputIterator in1, InputIterator in1End, InputIterator in2, |
| 135 |
InputIterator in3, OutputIterator out, ConversionOperator &convOp, |
| 136 |
InputIteratorTag, OutputIteratorTag) { |
| 137 |
while (in1 != in1End) { |
| 138 |
convOp(*in1++, *in2++, *in3++, *out++); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// transform for random_access_iterator_tag, so we can use OpenMP (optimized) |
| 143 |
template <typename InputIterator, typename OutputIterator, |
| 144 |
typename ConversionOperator> |
| 145 |
void transform(InputIterator in1, InputIterator in1End, InputIterator in2, |
| 146 |
InputIterator in3, OutputIterator out, ConversionOperator &convOp, |
| 147 |
std::random_access_iterator_tag, |
| 148 |
std::random_access_iterator_tag) { |
| 149 |
typename std::iterator_traits<InputIterator>::difference_type numElem = |
| 150 |
(in1End - in1); |
| 151 |
#pragma omp parallel for |
| 152 |
for (int idx = 0; idx < numElem; ++idx) { |
| 153 |
convOp(in1[idx], in2[idx], in3[idx], out[idx]); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
} // detail |
| 158 |
|
| 159 |
template <typename InputIterator, typename OutputIterator, |
| 160 |
typename ConversionOperator> |
| 161 |
void transform(InputIterator in1, InputIterator in1End, InputIterator in2, |
| 162 |
InputIterator in3, OutputIterator out, |
| 163 |
ConversionOperator convOp) { |
| 164 |
// dispatch to best implementation! |
| 165 |
detail::transform( |
| 166 |
in1, in1End, in2, in3, out, convOp, |
| 167 |
typename std::iterator_traits<InputIterator>::iterator_category(), |
| 168 |
typename std::iterator_traits<OutputIterator>::iterator_category()); |
| 169 |
} |
| 170 |
|
| 171 |
} // utils |
| 172 |
} // pfs |
| 173 |
|
| 174 |
#endif // PFS_COLORSPACE_TRANSFORM_HXX |