BufferUnrotate.cpp (2427B)
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/. */ 6 7 #include "BufferUnrotate.h" 8 9 #include <algorithm> // min & max 10 #include <stdint.h> 11 #include <string.h> 12 13 namespace mozilla { 14 namespace gfx { 15 16 void BufferUnrotate(uint8_t* aBuffer, int aByteWidth, int aHeight, 17 int aByteStride, int aXBoundary, int aYBoundary) { 18 if (aXBoundary != 0) { 19 uint8_t* line = new uint8_t[aByteWidth]; 20 uint32_t smallStart = 0; 21 uint32_t smallLen = aXBoundary; 22 uint32_t smallDest = aByteWidth - aXBoundary; 23 uint32_t largeStart = aXBoundary; 24 uint32_t largeLen = aByteWidth - aXBoundary; 25 uint32_t largeDest = 0; 26 if (aXBoundary > aByteWidth / 2) { 27 smallStart = aXBoundary; 28 smallLen = aByteWidth - aXBoundary; 29 smallDest = 0; 30 largeStart = 0; 31 largeLen = aXBoundary; 32 largeDest = smallLen; 33 } 34 35 for (int y = 0; y < aHeight; y++) { 36 int yOffset = y * aByteStride; 37 memcpy(line, &aBuffer[yOffset + smallStart], smallLen); 38 memmove(&aBuffer[yOffset + largeDest], &aBuffer[yOffset + largeStart], 39 largeLen); 40 memcpy(&aBuffer[yOffset + smallDest], line, smallLen); 41 } 42 43 delete[] line; 44 } 45 46 if (aYBoundary != 0) { 47 uint32_t smallestHeight = std::min(aHeight - aYBoundary, aYBoundary); 48 uint32_t largestHeight = std::max(aHeight - aYBoundary, aYBoundary); 49 uint32_t smallOffset = 0; 50 uint32_t largeOffset = aYBoundary * aByteStride; 51 uint32_t largeDestOffset = 0; 52 uint32_t smallDestOffset = largestHeight * aByteStride; 53 if (aYBoundary > aHeight / 2) { 54 smallOffset = aYBoundary * aByteStride; 55 largeOffset = 0; 56 largeDestOffset = smallestHeight * aByteStride; 57 smallDestOffset = 0; 58 } 59 60 uint8_t* smallestSide = new uint8_t[aByteStride * smallestHeight]; 61 memcpy(smallestSide, &aBuffer[smallOffset], aByteStride * smallestHeight); 62 memmove(&aBuffer[largeDestOffset], &aBuffer[largeOffset], 63 aByteStride * largestHeight); 64 memcpy(&aBuffer[smallDestOffset], smallestSide, 65 aByteStride * smallestHeight); 66 delete[] smallestSide; 67 } 68 } 69 70 } // namespace gfx 71 } // namespace mozilla