no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / gfx / 2d / BufferUnrotate.cpp
blob7b6768fdbde661818754a47923c0500ba3cb18ea
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
10 #include <cstdlib>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
16 namespace mozilla {
17 namespace gfx {
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;
32 smallDest = 0;
33 largeStart = 0;
34 largeLen = aXBoundary;
35 largeDest = smallLen;
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],
42 largeLen);
43 memcpy(&aBuffer[yOffset + smallDest], line, smallLen);
46 delete[] line;
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;
58 largeOffset = 0;
59 largeDestOffset = smallestHeight * aByteStride;
60 smallDestOffset = 0;
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;
73 } // namespace gfx
74 } // namespace mozilla