rotate_argb.cc 7 KB
Newer Older
1
/*
2
 *  Copyright 2012 The LibYuv Project Authors. All rights reserved.
3 4 5 6
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS. All contributing project authors may
8 9 10 11 12 13
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "libyuv/rotate.h"

#include "libyuv/convert.h"
14
#include "libyuv/cpu_id.h"
15
#include "libyuv/planar_functions.h"
16
#include "libyuv/row.h"
17
#include "libyuv/scale_row.h" /* for ScaleARGBRowDownEven_ */
18 19 20 21 22 23

#ifdef __cplusplus
namespace libyuv {
extern "C" {
#endif

24 25 26 27
static void ARGBTranspose(const uint8_t* src_argb,
                          int src_stride_argb,
                          uint8_t* dst_argb,
                          int dst_stride_argb,
Frank Barchard's avatar
Frank Barchard committed
28 29
                          int width,
                          int height) {
30
  int i;
31
  int src_pixel_step = src_stride_argb >> 2;
32 33 34
  void (*ScaleARGBRowDownEven)(
      const uint8_t* src_argb, ptrdiff_t src_stride_argb, int src_step,
      uint8_t* dst_argb, int dst_width) = ScaleARGBRowDownEven_C;
35
#if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2)
36 37 38 39 40
  if (TestCpuFlag(kCpuHasSSE2)) {
    ScaleARGBRowDownEven = ScaleARGBRowDownEven_Any_SSE2;
    if (IS_ALIGNED(height, 4)) {  // Width of dest.
      ScaleARGBRowDownEven = ScaleARGBRowDownEven_SSE2;
    }
41
  }
42 43
#endif
#if defined(HAS_SCALEARGBROWDOWNEVEN_NEON)
44 45 46 47 48 49 50 51 52 53 54 55 56
  if (TestCpuFlag(kCpuHasNEON)) {
    ScaleARGBRowDownEven = ScaleARGBRowDownEven_Any_NEON;
    if (IS_ALIGNED(height, 4)) {  // Width of dest.
      ScaleARGBRowDownEven = ScaleARGBRowDownEven_NEON;
    }
  }
#endif
#if defined(HAS_SCALEARGBROWDOWNEVEN_MSA)
  if (TestCpuFlag(kCpuHasMSA)) {
    ScaleARGBRowDownEven = ScaleARGBRowDownEven_Any_MSA;
    if (IS_ALIGNED(height, 4)) {  // Width of dest.
      ScaleARGBRowDownEven = ScaleARGBRowDownEven_MSA;
    }
57
  }
58 59
#endif

60
  for (i = 0; i < width; ++i) {  // column of source to row of dest.
61 62 63
    ScaleARGBRowDownEven(src_argb, 0, src_pixel_step, dst_argb, height);
    dst_argb += dst_stride_argb;
    src_argb += 4;
64 65 66
  }
}

67 68 69 70
void ARGBRotate90(const uint8_t* src_argb,
                  int src_stride_argb,
                  uint8_t* dst_argb,
                  int dst_stride_argb,
Frank Barchard's avatar
Frank Barchard committed
71 72
                  int width,
                  int height) {
73
  // Rotate by 90 is a ARGBTranspose with the source read
74
  // from bottom to top. So set the source pointer to the end
75
  // of the buffer and flip the sign of the source stride.
76 77
  src_argb += src_stride_argb * (height - 1);
  src_stride_argb = -src_stride_argb;
78 79
  ARGBTranspose(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
                height);
80 81
}

82 83 84 85
void ARGBRotate270(const uint8_t* src_argb,
                   int src_stride_argb,
                   uint8_t* dst_argb,
                   int dst_stride_argb,
Frank Barchard's avatar
Frank Barchard committed
86 87
                   int width,
                   int height) {
88
  // Rotate by 270 is a ARGBTranspose with the destination written
89
  // from bottom to top. So set the destination pointer to the end
90
  // of the buffer and flip the sign of the destination stride.
91 92
  dst_argb += dst_stride_argb * (width - 1);
  dst_stride_argb = -dst_stride_argb;
93 94
  ARGBTranspose(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
                height);
95 96
}

97 98 99 100
void ARGBRotate180(const uint8_t* src_argb,
                   int src_stride_argb,
                   uint8_t* dst_argb,
                   int dst_stride_argb,
Frank Barchard's avatar
Frank Barchard committed
101 102
                   int width,
                   int height) {
103 104
  // Swap first and last row and mirror the content. Uses a temporary row.
  align_buffer_64(row, width * 4);
105 106
  const uint8_t* src_bot = src_argb + src_stride_argb * (height - 1);
  uint8_t* dst_bot = dst_argb + dst_stride_argb * (height - 1);
107 108
  int half_height = (height + 1) >> 1;
  int y;
109
  void (*ARGBMirrorRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width) =
110
      ARGBMirrorRow_C;
111 112
  void (*CopyRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width) =
      CopyRow_C;
fbarchard@google.com's avatar
fbarchard@google.com committed
113 114 115 116 117 118
#if defined(HAS_ARGBMIRRORROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    ARGBMirrorRow = ARGBMirrorRow_Any_NEON;
    if (IS_ALIGNED(width, 4)) {
      ARGBMirrorRow = ARGBMirrorRow_NEON;
    }
119
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
120
#endif
121 122 123
#if defined(HAS_ARGBMIRRORROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    ARGBMirrorRow = ARGBMirrorRow_Any_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
124
    if (IS_ALIGNED(width, 4)) {
125
      ARGBMirrorRow = ARGBMirrorRow_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
126
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
127 128
  }
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
129 130 131 132 133 134
#if defined(HAS_ARGBMIRRORROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    ARGBMirrorRow = ARGBMirrorRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBMirrorRow = ARGBMirrorRow_AVX2;
    }
135
  }
136
#endif
137 138 139 140 141 142 143 144
#if defined(HAS_ARGBMIRRORROW_MSA)
  if (TestCpuFlag(kCpuHasMSA)) {
    ARGBMirrorRow = ARGBMirrorRow_Any_MSA;
    if (IS_ALIGNED(width, 16)) {
      ARGBMirrorRow = ARGBMirrorRow_MSA;
    }
  }
#endif
145
#if defined(HAS_COPYROW_SSE2)
146 147
  if (TestCpuFlag(kCpuHasSSE2)) {
    CopyRow = IS_ALIGNED(width * 4, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2;
148
  }
149
#endif
150
#if defined(HAS_COPYROW_AVX)
151 152
  if (TestCpuFlag(kCpuHasAVX)) {
    CopyRow = IS_ALIGNED(width * 4, 64) ? CopyRow_AVX : CopyRow_Any_AVX;
153 154
  }
#endif
155 156 157
#if defined(HAS_COPYROW_ERMS)
  if (TestCpuFlag(kCpuHasERMS)) {
    CopyRow = CopyRow_ERMS;
fbarchard@google.com's avatar
fbarchard@google.com committed
158 159
  }
#endif
160 161 162 163 164
#if defined(HAS_COPYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    CopyRow = IS_ALIGNED(width * 4, 32) ? CopyRow_NEON : CopyRow_Any_NEON;
  }
#endif
165

166
  // Odd height will harmlessly mirror the middle row twice.
167
  for (y = 0; y < half_height; ++y) {
168 169
    ARGBMirrorRow(src_argb, row, width);      // Mirror first row into a buffer
    ARGBMirrorRow(src_bot, dst_argb, width);  // Mirror last row into first row
170
    CopyRow(row, dst_bot, width * 4);  // Copy first mirrored row into last
171 172 173 174
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
    src_bot -= src_stride_argb;
    dst_bot -= dst_stride_argb;
175
  }
176
  free_aligned_buffer_64(row);
177 178
}

179
LIBYUV_API
Frank Barchard's avatar
Frank Barchard committed
180
int ARGBRotate(const uint8_t* src_argb,
Frank Barchard's avatar
Frank Barchard committed
181
               int src_stride_argb,
Frank Barchard's avatar
Frank Barchard committed
182
               uint8_t* dst_argb,
Frank Barchard's avatar
Frank Barchard committed
183 184 185
               int dst_stride_argb,
               int width,
               int height,
186
               enum RotationMode mode) {
187 188 189 190 191 192 193 194 195 196 197 198 199 200
  if (!src_argb || width <= 0 || height == 0 || !dst_argb) {
    return -1;
  }

  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }

  switch (mode) {
    case kRotate0:
      // copy frame
Frank Barchard's avatar
Frank Barchard committed
201
      return ARGBCopy(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
202 203
                      width, height);
    case kRotate90:
Frank Barchard's avatar
Frank Barchard committed
204 205
      ARGBRotate90(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
                   height);
206 207
      return 0;
    case kRotate270:
Frank Barchard's avatar
Frank Barchard committed
208 209
      ARGBRotate270(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
                    height);
210 211
      return 0;
    case kRotate180:
Frank Barchard's avatar
Frank Barchard committed
212 213
      ARGBRotate180(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
                    height);
214 215 216 217 218 219 220 221 222 223 224
      return 0;
    default:
      break;
  }
  return -1;
}

#ifdef __cplusplus
}  // extern "C"
}  // namespace libyuv
#endif