| 76 | * @returns Decoded data |
| 77 | */ |
| 78 | export function decodeBase64AsArray( |
| 79 | input: string, |
| 80 | bytes: number = 1, |
| 81 | ): Uint32Array { |
| 82 | const dec = globalThis.atob(input.replace(/[^A-Za-z0-9+/=]/g, "")); |
| 83 | const len = (dec.length / bytes) | 0; |
| 84 | const ar = new Uint32Array(len); |
| 85 | |
| 86 | if (bytes === 4) { |
| 87 | // fast path for the common case (tile data is always 4 bytes per entry) |
| 88 | for (let i = 0; i < len; i++) { |
| 89 | const base = i << 2; // i * 4 |
| 90 | ar[i] = |
| 91 | dec.charCodeAt(base) | |
| 92 | (dec.charCodeAt(base + 1) << 8) | |
| 93 | (dec.charCodeAt(base + 2) << 16) | |
| 94 | (dec.charCodeAt(base + 3) << 24); |
| 95 | } |
| 96 | } else { |
| 97 | for (let i = 0; i < len; i++) { |
| 98 | let val = 0; |
| 99 | const base = i * bytes; |
| 100 | for (let j = bytes - 1; j >= 0; --j) { |
| 101 | val += dec.charCodeAt(base + j) << (j << 3); |
| 102 | } |
| 103 | ar[i] = val; |
| 104 | } |
| 105 | } |
| 106 | return ar; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Decode a base64 encoded image string into an HTMLImageElement |