Created
January 26, 2019 19:10
-
-
Save MaxGraey/ab8654e65b00d8427ea1121add94fbdd to your computer and use it in GitHub Desktop.
squoosh rotate v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function rotate(inputWidth: i32, inputHeight: i32, rotate: i32): void { | |
const bpp = 4; | |
let offset = inputWidth * inputHeight * bpp; | |
// In the straight-copy case, d1 is x, d2 is y. | |
// x starts at 0 and increases. | |
// y starts at 0 and increases. | |
let d1Start: i32; | |
let d1Limit: i32; | |
let d1Multiplier: i32; | |
let d2Limit: i32; | |
let d2Multiplier: i32; | |
let i = 0; | |
switch (rotate) { | |
case 90: | |
// d1 is y, d2 is x. | |
// y starts at its max value and decreases. | |
// x starts at 0 and increases. | |
d1Multiplier = inputWidth * 4; | |
d1Start = inputHeight - 1; | |
for (let d2 = 0, d2Limit = inputWidth * 4; d2 < d2Limit; d2 += 4) { | |
for (let d1 = d1Start; d1 >= 0; d1--) { | |
let start = d1 * d1Multiplier + d2; | |
store<u32>(offset + i, load<u32>(start)); | |
i += 4; | |
} | |
} | |
break; | |
case 180: | |
// d1 is x, d2 is y. | |
// x starts at its max and decreases. | |
// y starts at its max and decreases. | |
d1Start = inputWidth - 1; | |
d2Multiplier = inputWidth * 4; | |
for (let d2 = inputHeight - 1; d2 >= 0; d2--) { | |
let d2Offset = d2 * d2Multiplier; | |
for (let d1 = d1Start; d1 >= 0; d1--) { | |
let start = d1 * 4 + d2Offset; | |
store<u32>(offset + i, load<u32>(start)); | |
i += 4; | |
} | |
} | |
break; | |
case 270: | |
// d1 is y, d2 is x. | |
// y starts at 0 and increases. | |
// x starts at its max and decreases. | |
d1Start = 0; | |
d1Limit = inputHeight; | |
d1Multiplier = inputWidth * 4; | |
for (let d2 = inputWidth - 1; d2 >= 0; d2--) { | |
let d2Offset = d2 * 4; | |
for (let d1 = 0; d1 < d1Limit; d1++) { | |
let start = d1 * d1Multiplier + d2Offset; | |
store<u32>(offset + i, load<u32>(start)); | |
i += 4; | |
} | |
} | |
break; | |
default: | |
d1Limit = inputWidth; | |
d2Limit = inputHeight; | |
d2Multiplier = inputWidth * 4; | |
for (let d2 = 0; d2 < d2Limit; d2++) { | |
let d2Offset = d2 * d2Multiplier; | |
for (let d1 = 0; d1 < d1Limit; d1++) { | |
let start = d1 * 4 + d2Offset; | |
store<u32>(offset + i, load<u32>(start)); | |
i += 4; | |
} | |
} | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment