| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include "img-precomp.h" // Precompiled headers |
| 11 |
// |
| 12 |
#include <mrpt/config.h> |
| 13 |
|
| 14 |
#if MRPT_ARCH_INTEL_COMPATIBLE |
| 15 |
// --------------------------------------------------------------------------- |
| 16 |
// This file contains the SSE2 optimized functions for mrpt::img::CImage |
| 17 |
// See the sources and the doxygen documentation page "sse_optimizations" for |
| 18 |
// more details. |
| 19 |
// |
| 20 |
// Some functions here are derived from sources in libcvd, released |
| 21 |
// under BSD. https://www.edwardrosten.com/cvd/ |
| 22 |
// |
| 23 |
// --------------------------------------------------------------------------- |
| 24 |
|
| 25 |
#include <mrpt/core/SSE_macros.h> |
| 26 |
#include <mrpt/core/SSE_types.h> |
| 27 |
#include <mrpt/img/CImage.h> |
| 28 |
#include <mrpt/system/memory.h> |
| 29 |
|
| 30 |
#include "CImage.SSEx.h" |
| 31 |
|
| 32 |
/** \addtogroup sse_optimizations |
| 33 |
* SSE optimized functions |
| 34 |
* @{ |
| 35 |
*/ |
| 36 |
|
| 37 |
template <bool MemIsAligned> |
| 38 |
void impl_image_SSE2_scale_half_1c8u( |
| 39 |
const uint8_t* in, uint8_t* out, int w, int h, size_t step_in, size_t step_out) |
| 40 |
{ |
| 41 |
SSE_DISABLE_WARNINGS |
| 42 |
// clang-format off |
| 43 |
|
| 44 |
const __m128i m = _mm_set_epi8(0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff); |
| 45 |
|
| 46 |
// clang-format on |
| 47 |
SSE_RESTORE_SIGN_WARNINGS |
| 48 |
|
| 49 |
const int sw = w / 16; |
| 50 |
const int sh = h / 2; |
| 51 |
const int rest_w = w - (16 * w); |
| 52 |
|
| 53 |
for (int i = 0; i < sh; i++) |
| 54 |
{ |
| 55 |
auto inp = reinterpret_cast<const __m128i*>(in); |
| 56 |
uint8_t* outp = out; |
| 57 |
for (int j = 0; j < sw; j++) |
| 58 |
{ |
| 59 |
const __m128i x = _mm_and_si128(mm_load_si128<MemIsAligned>(inp++), m); |
| 60 |
auto o = reinterpret_cast<__m128i*>(outp); |
| 61 |
_mm_storel_epi64(o, _mm_packus_epi16(x, x)); |
| 62 |
outp += 8; |
| 63 |
} |
| 64 |
// Extra pixels? (w mod 16 != 0) |
| 65 |
if (rest_w != 0) |
| 66 |
{ |
| 67 |
const uint8_t* in_rest = in + 16 * sw; |
| 68 |
for (int p = 0; p < rest_w / 2; p++) |
| 69 |
{ |
| 70 |
*outp++ = in_rest[0]; |
| 71 |
in_rest += 2; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
in += 2 * step_in; // Skip one row |
| 76 |
out += step_out; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
template <bool MemIsAligned> |
| 81 |
void impl_image_SSE2_scale_half_smooth_1c8u( |
| 82 |
const uint8_t* in, uint8_t* out, int w, int h, size_t step_in, size_t step_out) |
| 83 |
{ |
| 84 |
SSE_DISABLE_WARNINGS |
| 85 |
// clang-format off |
| 86 |
|
| 87 |
const __m128i m = _mm_set_epi8(0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff); |
| 88 |
|
| 89 |
// clang-format on |
| 90 |
SSE_RESTORE_SIGN_WARNINGS |
| 91 |
|
| 92 |
const int sw = w / 16; |
| 93 |
const int sh = h / 2; |
| 94 |
const int rest_w = w - (16 * w); |
| 95 |
|
| 96 |
for (int i = 0; i < sh; i++) |
| 97 |
{ |
| 98 |
auto inp = reinterpret_cast<const __m128i*>(in); |
| 99 |
auto nextRow = reinterpret_cast<const __m128i*>(in + step_in); |
| 100 |
uint8_t* outp = out; |
| 101 |
|
| 102 |
for (int j = 0; j < sw; j++) |
| 103 |
{ |
| 104 |
__m128i here = mm_load_si128<MemIsAligned>(inp++); |
| 105 |
__m128i next = mm_load_si128<MemIsAligned>(nextRow++); |
| 106 |
here = _mm_avg_epu8(here, next); |
| 107 |
next = _mm_and_si128(_mm_srli_si128(here, 1), m); |
| 108 |
here = _mm_and_si128(here, m); |
| 109 |
here = _mm_avg_epu16(here, next); |
| 110 |
_mm_storel_epi64(reinterpret_cast<__m128i*>(outp), _mm_packus_epi16(here, here)); |
| 111 |
outp += 8; |
| 112 |
} |
| 113 |
|
| 114 |
// Extra pixels? (w mod 16 != 0) |
| 115 |
if (rest_w != 0) |
| 116 |
{ |
| 117 |
const uint8_t* ir = in + 16 * sw; |
| 118 |
const uint8_t* irr = in + step_in; |
| 119 |
for (int p = 0; p < rest_w / 2; p++) |
| 120 |
{ |
| 121 |
*outp++ = (ir[0] + ir[1] + irr[0] + irr[1]) / 4; |
| 122 |
ir += 2; |
| 123 |
irr += 2; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
in += 2 * step_in; // Skip one row |
| 128 |
out += step_out; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** Subsample each 2x2 pixel block into 1x1 pixel, taking the first pixel & |
| 133 |
* ignoring the other 3 |
| 134 |
* - <b>Input format:</b> uint8_t, 1 channel |
| 135 |
* - <b>Output format:</b> uint8_t, 1 channel |
| 136 |
* - <b>Preconditions:</b> in & out aligned to 16bytes (faster) or not, step = |
| 137 |
* k*16 (faster) or not |
| 138 |
* - <b>Notes:</b> |
| 139 |
* - <b>Requires:</b> SSE2 |
| 140 |
* - <b>Invoked from:</b> mrpt::img::CImage::scaleHalf() |
| 141 |
*/ |
| 142 |
void image_SSE2_scale_half_1c8u( |
| 143 |
const uint8_t* in, uint8_t* out, int w, int h, size_t step_in, size_t step_out) |
| 144 |
{ |
| 145 |
if (mrpt::system::is_aligned<16>(in) && mrpt::system::is_aligned<16>(out) && |
| 146 |
is_multiple<16>(step_in) && is_multiple<16>(step_out)) |
| 147 |
{ |
| 148 |
impl_image_SSE2_scale_half_1c8u<true>(in, out, w, h, step_in, step_out); |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
impl_image_SSE2_scale_half_1c8u<false>(in, out, w, h, step_in, step_out); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** Average each 2x2 pixels into 1x1 pixel (arithmetic average) |
| 157 |
* - <b>Input format:</b> uint8_t, 1 channel |
| 158 |
* - <b>Output format:</b> uint8_t, 1 channel |
| 159 |
* - <b>Preconditions:</b> in & out aligned to 16bytes (faster) or not, step = |
| 160 |
* k*16 (faster) or not |
| 161 |
* - <b>Notes:</b> |
| 162 |
* - <b>Requires:</b> SSE2 |
| 163 |
* - <b>Invoked from:</b> mrpt::img::CImage::scaleHalfSmooth() |
| 164 |
*/ |
| 165 |
void image_SSE2_scale_half_smooth_1c8u( |
| 166 |
const uint8_t* in, uint8_t* out, int w, int h, size_t step_in, size_t step_out) |
| 167 |
{ |
| 168 |
if (mrpt::system::is_aligned<16>(in) && mrpt::system::is_aligned<16>(out) && |
| 169 |
is_multiple<16>(step_in) && is_multiple<16>(step_out)) |
| 170 |
{ |
| 171 |
impl_image_SSE2_scale_half_smooth_1c8u<true>(in, out, w, h, step_in, step_out); |
| 172 |
} |
| 173 |
else |
| 174 |
{ |
| 175 |
impl_image_SSE2_scale_half_smooth_1c8u<false>(in, out, w, h, step_in, step_out); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// TODO: |
| 180 |
// Sum of absolute differences: Use _mm_sad_epu8 |
| 181 |
|
| 182 |
/** @} */ |
| 183 |
|
| 184 |
#endif // end if MRPT_ARCH_INTEL_COMPATIBLE |