1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "BufferUnrotate.h"
9 #include <algorithm> // min & max
19 void BufferUnrotate(uint8_t* aBuffer
, int aByteWidth
, int aHeight
,
20 int aByteStride
, int aXBoundary
, int aYBoundary
) {
21 if (aXBoundary
!= 0) {
22 uint8_t* line
= new uint8_t[aByteWidth
];
23 uint32_t smallStart
= 0;
24 uint32_t smallLen
= aXBoundary
;
25 uint32_t smallDest
= aByteWidth
- aXBoundary
;
26 uint32_t largeStart
= aXBoundary
;
27 uint32_t largeLen
= aByteWidth
- aXBoundary
;
28 uint32_t largeDest
= 0;
29 if (aXBoundary
> aByteWidth
/ 2) {
30 smallStart
= aXBoundary
;
31 smallLen
= aByteWidth
- aXBoundary
;
34 largeLen
= aXBoundary
;
38 for (int y
= 0; y
< aHeight
; y
++) {
39 int yOffset
= y
* aByteStride
;
40 memcpy(line
, &aBuffer
[yOffset
+ smallStart
], smallLen
);
41 memmove(&aBuffer
[yOffset
+ largeDest
], &aBuffer
[yOffset
+ largeStart
],
43 memcpy(&aBuffer
[yOffset
+ smallDest
], line
, smallLen
);
49 if (aYBoundary
!= 0) {
50 uint32_t smallestHeight
= std::min(aHeight
- aYBoundary
, aYBoundary
);
51 uint32_t largestHeight
= std::max(aHeight
- aYBoundary
, aYBoundary
);
52 uint32_t smallOffset
= 0;
53 uint32_t largeOffset
= aYBoundary
* aByteStride
;
54 uint32_t largeDestOffset
= 0;
55 uint32_t smallDestOffset
= largestHeight
* aByteStride
;
56 if (aYBoundary
> aHeight
/ 2) {
57 smallOffset
= aYBoundary
* aByteStride
;
59 largeDestOffset
= smallestHeight
* aByteStride
;
63 uint8_t* smallestSide
= new uint8_t[aByteStride
* smallestHeight
];
64 memcpy(smallestSide
, &aBuffer
[smallOffset
], aByteStride
* smallestHeight
);
65 memmove(&aBuffer
[largeDestOffset
], &aBuffer
[largeOffset
],
66 aByteStride
* largestHeight
);
67 memcpy(&aBuffer
[smallDestOffset
], smallestSide
,
68 aByteStride
* smallestHeight
);
69 delete[] smallestSide
;
74 } // namespace mozilla