Skip to content

Commit 2eed02b

Browse files
committed
feat: ✨ 包装 API
1 parent 469824d commit 2eed02b

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,32 @@ high quality image upscaling filter by Zenju
44

55
![npm](https://img.shields.io/npm/v/@d2-projects/xbrz)
66

7-
在包管理器中使用
7+
在包管理器中引入
88

99
``` sh
1010
npm i @d2-projects/xbrz
1111
```
1212

13-
在浏览器中使用
13+
``` js
14+
import xbrz from '@d2-projects/xbrz'
15+
```
16+
17+
在浏览器中引入
1418

1519
``` html
1620
<script src="https://cdn.jsdelivr.net/npm/@d2-projects/xbrz/dist/xbrz.js"></script>
1721
```
1822

23+
使用
24+
25+
```
26+
xbrz.scaleToCanvas({
27+
source: './source.png',
28+
canvas: document.querySelector('#canvas'),
29+
scale: 6
30+
})
31+
```
32+
1933
## 算法实现
2034

2135
* [xbrz](https://sourceforge.net/projects/xbrz/)

src/index.js

+81-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,83 @@
11
import { scaleImage } from './core/xBRZ.js'
22

3-
export default scaleImage
3+
function rgbaZip(data) {
4+
let result = [];
5+
for (let i = 0, len = data.length; i < len; i += 4) {
6+
const r = data[i];
7+
const g = data[i + 1];
8+
const b = data[i + 2];
9+
const a = data[i + 3];
10+
const pixel = a << 24 | r << 16 | g << 8 | b;
11+
result.push(pixel)
12+
}
13+
return result
14+
}
15+
16+
function rgbaUnzip(data) {
17+
let result = []
18+
for (let i = 0, len = data.length; i < len; ++i) {
19+
const pixel = data[i]
20+
const a = (pixel >> 24) & 0xff
21+
const r = (pixel >> 16) & 0xff
22+
const g = (pixel >> 8) & 0xff
23+
const b = (pixel) & 0xff
24+
result.push(r)
25+
result.push(g)
26+
result.push(b)
27+
result.push(a)
28+
}
29+
return result
30+
}
31+
32+
async function readImage(source) {
33+
return new Promise(resolve => {
34+
const canvas = document.createElement('canvas')
35+
const ctx = canvas.getContext('2d')
36+
const srcImage = new Image()
37+
srcImage.src = source
38+
srcImage.onload = () => {
39+
const {
40+
width,
41+
height
42+
} = srcImage
43+
canvas.width = width
44+
canvas.height = height
45+
ctx.drawImage(srcImage, 0, 0, width, height)
46+
const imageData = ctx.getImageData(0, 0, width, height)
47+
const data = Array.from(imageData.data)
48+
resolve({
49+
width,
50+
height,
51+
data
52+
})
53+
}
54+
})
55+
}
56+
57+
function scaleData({ data, width, height, scale }) {
58+
let target = new Array(width * scale * height * scale).fill(0)
59+
scaleImage(scale, rgbaZip(data), target, width, height, 0, height)
60+
return rgbaUnzip(target)
61+
}
62+
63+
function refreshCanvas ({ canvas, width, height, data }) {
64+
const ctx = canvas.getContext('2d')
65+
canvas.width = width
66+
canvas.height = height
67+
ctx.putImageData(new ImageData(new Uint8ClampedArray(data), width, height), 0, 0)
68+
}
69+
70+
export async function scaleToCanvas ({ source, canvas, scale }) {
71+
const { width, height, data } = await readImage(source)
72+
refreshCanvas({
73+
canvas,
74+
width: width * scale,
75+
height: height * scale,
76+
data: scaleData({
77+
data,
78+
width,
79+
height,
80+
scale
81+
})
82+
})
83+
}

0 commit comments

Comments
 (0)