@eduardo191labs/opencode-free-pi 0.3.19 → 0.3.20
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.
- package/dist/tools/image.js +13 -5
- package/package.json +1 -1
- package/src/tools/image.ts +15 -4
package/dist/tools/image.js
CHANGED
|
@@ -122,7 +122,7 @@ function unchanged(bytes, mime, width, height) {
|
|
|
122
122
|
* Re-encodes the image at `width` x `height`, trying PNG and several JPEG
|
|
123
123
|
* qualities, and returns the smallest candidate under the size limit.
|
|
124
124
|
*/
|
|
125
|
-
function encodeCandidate(photon, image, width, height) {
|
|
125
|
+
function encodeCandidate(photon, image, width, height, preferred) {
|
|
126
126
|
const resized = photon.resize(image, width, height, photon.SamplingFilter.Lanczos3);
|
|
127
127
|
try {
|
|
128
128
|
const png = Buffer.from(resized.get_bytes());
|
|
@@ -137,9 +137,17 @@ function encodeCandidate(photon, image, width, height) {
|
|
|
137
137
|
bytes: jpeg.length,
|
|
138
138
|
});
|
|
139
139
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
140
|
+
const usable = candidates.filter((candidate) => candidate.bytes <= MAX_IMAGE_BYTES);
|
|
141
|
+
if (usable.length === 0)
|
|
142
|
+
return undefined;
|
|
143
|
+
// Keep PNG for PNG screenshots as long as it fits: JPEG artifacts blur UI
|
|
144
|
+
// text and transparency, and the vision model charges by pixels, not bytes.
|
|
145
|
+
if (preferred === "image/png") {
|
|
146
|
+
const pngCandidate = usable.find((candidate) => candidate.mime === "image/png");
|
|
147
|
+
if (pngCandidate)
|
|
148
|
+
return pngCandidate;
|
|
149
|
+
}
|
|
150
|
+
return usable.sort((a, b) => a.bytes - b.bytes)[0];
|
|
143
151
|
}
|
|
144
152
|
finally {
|
|
145
153
|
resized.free();
|
|
@@ -192,7 +200,7 @@ export async function prepareImage(bytes, mime, name) {
|
|
|
192
200
|
height = MAX_DIMENSION;
|
|
193
201
|
}
|
|
194
202
|
while (true) {
|
|
195
|
-
const best = encodeCandidate(photo, image, width, height);
|
|
203
|
+
const best = encodeCandidate(photo, image, width, height, mime);
|
|
196
204
|
if (best) {
|
|
197
205
|
return {
|
|
198
206
|
ok: true,
|
package/package.json
CHANGED
package/src/tools/image.ts
CHANGED
|
@@ -178,6 +178,7 @@ function encodeCandidate(
|
|
|
178
178
|
image: InstanceType<PhotonModule["PhotonImage"]>,
|
|
179
179
|
width: number,
|
|
180
180
|
height: number,
|
|
181
|
+
preferred: VisionMime,
|
|
181
182
|
): Candidate | undefined {
|
|
182
183
|
const resized = photon.resize(image, width, height, photon.SamplingFilter.Lanczos3);
|
|
183
184
|
try {
|
|
@@ -193,9 +194,19 @@ function encodeCandidate(
|
|
|
193
194
|
bytes: jpeg.length,
|
|
194
195
|
});
|
|
195
196
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
197
|
+
const usable = candidates.filter(
|
|
198
|
+
(candidate) => candidate.bytes <= MAX_IMAGE_BYTES,
|
|
199
|
+
);
|
|
200
|
+
if (usable.length === 0) return undefined;
|
|
201
|
+
// Keep PNG for PNG screenshots as long as it fits: JPEG artifacts blur UI
|
|
202
|
+
// text and transparency, and the vision model charges by pixels, not bytes.
|
|
203
|
+
if (preferred === "image/png") {
|
|
204
|
+
const pngCandidate = usable.find(
|
|
205
|
+
(candidate) => candidate.mime === "image/png",
|
|
206
|
+
);
|
|
207
|
+
if (pngCandidate) return pngCandidate;
|
|
208
|
+
}
|
|
209
|
+
return usable.sort((a, b) => a.bytes - b.bytes)[0];
|
|
199
210
|
} finally {
|
|
200
211
|
resized.free();
|
|
201
212
|
}
|
|
@@ -255,7 +266,7 @@ export async function prepareImage(
|
|
|
255
266
|
}
|
|
256
267
|
|
|
257
268
|
while (true) {
|
|
258
|
-
const best = encodeCandidate(photo, image, width, height);
|
|
269
|
+
const best = encodeCandidate(photo, image, width, height, mime);
|
|
259
270
|
if (best) {
|
|
260
271
|
return {
|
|
261
272
|
ok: true,
|