koishi-plugin-img-tool 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +89 -13
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -114,31 +114,107 @@ function mirrorPixels(src, width, height, mode) {
114
114
  __name(mirrorPixels, "mirrorPixels");
115
115
  async function mirrorStaticImage(canvas, buffer, mode) {
116
116
  let img;
117
- let srcCanvas;
118
117
  let outCanvas;
119
118
  try {
120
119
  img = await canvas.loadImage(buffer);
121
120
  const w = img.naturalWidth ?? img.width;
122
121
  const h2 = img.naturalHeight ?? img.height;
123
- srcCanvas = await canvas.createCanvas(w, h2);
124
- const src = srcCanvas.getContext("2d");
125
- src.drawImage(img, 0, 0, w, h2);
126
122
  outCanvas = await canvas.createCanvas(w, h2);
127
123
  const out = outCanvas.getContext("2d");
128
- const srcImageData = src.getImageData(0, 0, w, h2);
129
- const outImageData = out.createImageData(w, h2);
130
- outImageData.data.set(mirrorPixels(srcImageData.data, w, h2, mode));
131
- out.putImageData(outImageData, 0, 0);
124
+ out.clearRect(0, 0, w, h2);
125
+ if (mode === "left") {
126
+ const rightWidth = Math.floor(w / 2);
127
+ const leftKeepWidth = w - rightWidth;
128
+ out.drawImage(
129
+ img,
130
+ 0,
131
+ 0,
132
+ leftKeepWidth,
133
+ h2,
134
+ 0,
135
+ 0,
136
+ leftKeepWidth,
137
+ h2
138
+ );
139
+ out.save();
140
+ out.translate(w, 0);
141
+ out.scale(-1, 1);
142
+ out.drawImage(img, 0, 0, rightWidth, h2, 0, 0, rightWidth, h2);
143
+ out.restore();
144
+ } else if (mode === "right") {
145
+ const leftWidth = Math.floor(w / 2);
146
+ const rightKeepWidth = w - leftWidth;
147
+ out.drawImage(
148
+ img,
149
+ w - rightKeepWidth,
150
+ 0,
151
+ rightKeepWidth,
152
+ h2,
153
+ w - rightKeepWidth,
154
+ 0,
155
+ rightKeepWidth,
156
+ h2
157
+ );
158
+ out.save();
159
+ out.translate(leftWidth, 0);
160
+ out.scale(-1, 1);
161
+ out.drawImage(
162
+ img,
163
+ w - leftWidth,
164
+ 0,
165
+ leftWidth,
166
+ h2,
167
+ 0,
168
+ 0,
169
+ leftWidth,
170
+ h2
171
+ );
172
+ out.restore();
173
+ } else if (mode === "up") {
174
+ const bottomHeight = Math.floor(h2 / 2);
175
+ const topKeepHeight = h2 - bottomHeight;
176
+ out.drawImage(img, 0, 0, w, topKeepHeight, 0, 0, w, topKeepHeight);
177
+ out.save();
178
+ out.translate(0, h2);
179
+ out.scale(1, -1);
180
+ out.drawImage(img, 0, 0, w, bottomHeight, 0, 0, w, bottomHeight);
181
+ out.restore();
182
+ } else {
183
+ const topHeight = Math.floor(h2 / 2);
184
+ const bottomKeepHeight = h2 - topHeight;
185
+ out.drawImage(
186
+ img,
187
+ 0,
188
+ h2 - bottomKeepHeight,
189
+ w,
190
+ bottomKeepHeight,
191
+ 0,
192
+ h2 - bottomKeepHeight,
193
+ w,
194
+ bottomKeepHeight
195
+ );
196
+ out.save();
197
+ out.translate(0, topHeight);
198
+ out.scale(1, -1);
199
+ out.drawImage(
200
+ img,
201
+ 0,
202
+ h2 - topHeight,
203
+ w,
204
+ topHeight,
205
+ 0,
206
+ 0,
207
+ w,
208
+ topHeight
209
+ );
210
+ out.restore();
211
+ }
132
212
  return {
133
213
  buffer: await outCanvas.toBuffer("image/png"),
134
214
  mime: "image/png"
135
215
  };
136
216
  } finally {
137
- await Promise.allSettled([
138
- img?.dispose?.(),
139
- srcCanvas?.dispose?.(),
140
- outCanvas?.dispose?.()
141
- ]);
217
+ await Promise.allSettled([img?.dispose?.(), outCanvas?.dispose?.()]);
142
218
  }
143
219
  }
144
220
  __name(mirrorStaticImage, "mirrorStaticImage");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-img-tool",
3
3
  "description": "modify img",
4
- "version": "1.0.1",
4
+ "version": "1.0.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [