| @@ -52893,11 +52893,9 @@ | ||
| 52893 | 52893 | var vips_heif_default = bytes2; |
| 52894 | 52894 | |
| 52895 | 52895 | // packages/vips/src/utils.ts |
| 52896 | 52896 | function supportsQuality(type) { |
| 52897 | - return ["image/jpeg", "image/png", "image/webp", "image/avif"].includes( | |
| 52898 | - type | |
| 52899 | - ); | |
| 52897 | + return ["image/jpeg", "image/webp", "image/avif"].includes(type); | |
| 52900 | 52898 | } |
| 52901 | 52899 | function supportsAnimation(type) { |
| 52902 | 52900 | return ["image/webp", "image/gif"].includes(type); |
| 52903 | 52901 | } |
| @@ -52998,8 +52996,11 @@ | ||
| 52998 | 52996 | } |
| 52999 | 52997 | if (interlaced && supportsInterlace(outputType)) { |
| 53000 | 52998 | saveOptions.interlace = interlaced; |
| 53001 | 52999 | } |
| 53000 | + if ("image/png" === outputType && isPaletteImage(image)) { | |
| 53001 | + saveOptions.palette = true; | |
| 53002 | + } | |
| 53002 | 53003 | if ("image/avif" === outputType) { |
| 53003 | 53004 | saveOptions.effort = 2; |
| 53004 | 53005 | const sourceBitdepth = getSourceBitdepth(image); |
| 53005 | 53006 | if (sourceBitdepth > 8) { |
| @@ -53089,8 +53090,11 @@ | ||
| 53089 | 53090 | const result = copy.call(cropped); |
| 53090 | 53091 | setImage.call(result, "gainmap", newGainmap); |
| 53091 | 53092 | return result; |
| 53092 | 53093 | } |
| 53094 | +function isPaletteImage(image) { | |
| 53095 | + return image.getTypeof("palette") !== 0; | |
| 53096 | +} | |
| 53093 | 53097 | function getSourceBitdepth(image) { |
| 53094 | 53098 | try { |
| 53095 | 53099 | const bitdepth = image.getInt("heif-bitdepth"); |
| 53096 | 53100 | if (bitdepth > 8) { |
| @@ -53109,9 +53113,15 @@ | ||
| 53109 | 53113 | return 10; |
| 53110 | 53114 | } |
| 53111 | 53115 | return 8; |
| 53112 | 53116 | } |
| 53113 | -function buildSaveOptions(type, quality, bitdepth = 8, stripMeta = true) { | |
| 53117 | +function buildSaveOptions({ | |
| 53118 | + type, | |
| 53119 | + quality, | |
| 53120 | + bitdepth = 8, | |
| 53121 | + stripMeta = true, | |
| 53122 | + isPalette = false | |
| 53123 | +}) { | |
| 53114 | 53124 | const saveOptions = { |
| 53115 | 53125 | // Strip metadata except ICC color profiles or gainmaps, |
| 53116 | 53126 | // matching WordPress core's behavior. The \`image_strip_meta\` |
| 53117 | 53127 | // filter can disable stripping entirely. |
| @@ -53119,8 +53129,11 @@ | ||
| 53119 | 53129 | }; |
| 53120 | 53130 | if (supportsQuality(type)) { |
| 53121 | 53131 | saveOptions.Q = quality * 100; |
| 53122 | 53132 | } |
| 53133 | + if ("image/png" === type && isPalette) { | |
| 53134 | + saveOptions.palette = true; | |
| 53135 | + } | |
| 53123 | 53136 | if ("image/avif" === type) { |
| 53124 | 53137 | saveOptions.effort = 2; |
| 53125 | 53138 | if (bitdepth > 8) { |
| 53126 | 53139 | saveOptions.bitdepth = bitdepth; |
| @@ -53207,14 +53220,15 @@ | ||
| 53207 | 53220 | thumb.onProgress = onProgress; |
| 53208 | 53221 | return thumb; |
| 53209 | 53222 | } |
| 53210 | 53223 | ); |
| 53211 | - const saveOptions = buildSaveOptions( | |
| 53224 | + const saveOptions = buildSaveOptions({ | |
| 53212 | 53225 | type, |
| 53213 | 53226 | quality, |
| 53214 | - saveBitdepth, | |
| 53215 | - stripMeta | |
| 53216 | - ); | |
| 53227 | + bitdepth: saveBitdepth, | |
| 53228 | + stripMeta, | |
| 53229 | + isPalette: isPaletteImage(sourceImage) | |
| 53230 | + }); | |
| 53217 | 53231 | const outBuffer = image.writeToBuffer(\`.\${ext}\`, saveOptions); |
| 53218 | 53232 | const result = { |
| 53219 | 53233 | buffer: outBuffer.buffer, |
| 53220 | 53234 | width: image.width, |
| @@ -53270,8 +53284,9 @@ | ||
| 53270 | 53284 | if (!inProgressOperations.has(id)) { |
| 53271 | 53285 | image.kill = true; |
| 53272 | 53286 | } |
| 53273 | 53287 | }; |
| 53288 | + const isPalette = isPaletteImage(image); | |
| 53274 | 53289 | switch (orientation) { |
| 53275 | 53290 | case 2: |
| 53276 | 53291 | image = image.flipHor(); |
| 53277 | 53292 | break; |
| @@ -53295,8 +53310,11 @@ | ||
| 53295 | 53310 | break; |
| 53296 | 53311 | } |
| 53297 | 53312 | image.remove("orientation"); |
| 53298 | 53313 | const saveOptions = {}; |
| 53314 | + if ("image/png" === type && isPalette) { | |
| 53315 | + saveOptions.palette = true; | |
| 53316 | + } | |
| 53299 | 53317 | const outBuffer = image.writeToBuffer(\`.\${ext}\`, saveOptions); |
| 53300 | 53318 | const result = { |
| 53301 | 53319 | buffer: outBuffer.buffer, |
| 53302 | 53320 | width: image.width, |