image.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. export { PixelData };
  7. interface PixelData {
  8. readonly array: Uint8Array
  9. readonly width: number
  10. readonly height: number
  11. }
  12. namespace PixelData {
  13. export function create(array: Uint8Array, width: number, height: number): PixelData {
  14. return { array, width, height };
  15. }
  16. /** horizontally flips the pixel data in-place */
  17. export function flipY(pixelData: PixelData): PixelData {
  18. const { array, width, height } = pixelData;
  19. const width4 = width * 4;
  20. for (let i = 0, maxI = height / 2; i < maxI; ++i) {
  21. for (let j = 0, maxJ = width4; j < maxJ; ++j) {
  22. const index1 = i * width4 + j;
  23. const index2 = (height - i - 1) * width4 + j;
  24. const tmp = array[index1];
  25. array[index1] = array[index2];
  26. array[index2] = tmp;
  27. }
  28. }
  29. return pixelData;
  30. }
  31. }