<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lank_M</title>
    <description>The latest articles on DEV Community by Lank_M (@pm_cheng_3f36acecfb9c59f5).</description>
    <link>https://dev.to/pm_cheng_3f36acecfb9c59f5</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4086794%2F931fea88-28cd-4592-bb00-cbf315e37b06.png</url>
      <title>DEV Community: Lank_M</title>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pm_cheng_3f36acecfb9c59f5"/>
    <language>en</language>
    <item>
      <title>Your compressor dropped color_range, so now the player decides</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Tue, 17 Nov 2026 08:03:26 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/your-compressor-dropped-colorrange-so-now-the-player-decides-f87</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/your-compressor-dropped-colorrange-so-now-the-player-decides-f87</guid>
      <description>&lt;p&gt;The clip was 6.4 seconds of 1080p ocean waves, 17.4 MB, tagged as limited range (&lt;code&gt;tv&lt;/code&gt;) with BT.709 matrix, primaries and transfer. In this test it went through a free online compressor without logging in, and 9.25 MB came back in about 15 seconds. Same resolution and frame rate, whole-clip SSIM 0.986, in the same band as an ffmpeg CRF 23 encode. By the usual checks it was fine. Then ffprobe ran on it, and &lt;code&gt;color_range&lt;/code&gt;, &lt;code&gt;color_space&lt;/code&gt;, &lt;code&gt;color_primaries&lt;/code&gt; and &lt;code&gt;color_transfer&lt;/code&gt; all came back &lt;code&gt;unknown&lt;/code&gt;. I won't name the service, because the point is how a reasonable compression step quietly turned four answers into four questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pixels were fine and only the labels were gone
&lt;/h2&gt;

&lt;p&gt;A botched range conversion was the first suspect. After aligning frames, the mean luma of output and source came to 123.73 and 123.71, so nothing was stretched, which points to the data still being limited range. What disappeared was the sentence that says so. That moves the call. The compressor had the answer, since it decoded a source that carried it. Now every player, browser and thumbnailer that opens the file has to guess, and nothing in the file tells it how. This is one clip, one Apple-silicon Mac, one run: an observation about one file, not a survey.&lt;/p&gt;

&lt;h2&gt;
  
  
  A wrong guess has two directions
&lt;/h2&gt;

&lt;p&gt;To price a wrong guess, the test encoded a grey pattern (0–255 ramp plus nine patches) as 3-second H.264 in four combinations of actual and declared range. Decoded to RGB, the two honest cases read back exactly: 0, 8, 16, 64, 128, 192, 235, 247, 255. Limited-range data declared as full came back as 16, 23, 30, 71, 126, 181, 218, 228, 235. Black became 16 and white became 235, so the usable contrast was 219/255, about 86%, the washed-out grey people complain about. The opposite mistake looks different. Full-range data declared as limited came back 0, 0, 0, 56, 130, 205, 255, 255, 255: 17 shadow levels collapsed into black and 21 highlights clipped to white. That one reads as crushed shadows and blown highlights rather than grey.&lt;/p&gt;

&lt;p&gt;An &lt;code&gt;unknown&lt;/code&gt; file of limited-range data only fails one way, if a reader guesses full. The unknown file was not run through a set of players, so who guesses what is open. My unverified understanding is that ffmpeg's scaler treats unspecified YUV as limited, so the SSIM comparison was probably decoded correctly. The matrix is the other open gap. The fallback I remember from codec SDK work is picking by frame height, BT.709 for HD and BT.601 below. Whether current browsers still do that, nothing here tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  One MP4 carries the tag in two places
&lt;/h2&gt;

&lt;p&gt;There's a second trap. An H.264 MP4 can declare range in the bitstream's VUI and again in the container's &lt;code&gt;colr&lt;/code&gt; box, and the two don't have to agree. Rewrite the bitstream flag with &lt;code&gt;h264_metadata&lt;/code&gt; and the container keeps its old value. &lt;code&gt;ffprobe -show_streams&lt;/code&gt; then reports the container value while &lt;code&gt;-show_frames&lt;/code&gt; reports the bitstream one. Two mismatched files were built to see which one decoders believe. Both ffmpeg decoding and a &lt;code&gt;&amp;lt;video&amp;gt;&lt;/code&gt; element drawn to a canvas in desktop Chromium followed the bitstream and ignored the container, and their readings agreed within 1. QuickTime, VLC, Safari and phone galleries were not tested, so "players disagree" is neither proven nor disproven. A contract that only asserts the container field would have passed both broken files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Colour metadata is part of the output contract
&lt;/h2&gt;

&lt;p&gt;What I'd write down for any compression step is short. The output declares range, matrix, primaries and transfer explicitly, never &lt;code&gt;unknown&lt;/code&gt;. Bitstream and container agree. If the step converts range, the label changes with the pixels. The check sits next to resolution and frame rate and fails when a field goes missing. When the source carries no tags, the step still chooses and records it, since it has the decoded pixels in hand.&lt;/p&gt;

&lt;p&gt;For comparison I used ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; in the same desktop Chromium on the same Mac: the full-range grey pattern went in on its "visually near-lossless" setting. Video compression isn't a module I worked on, so this is an outside reading of its output. Both outputs came back converted to limited range and tagged &lt;code&gt;tv&lt;/code&gt; with BT.709 for all three colour fields. Decoded, black and white held up: 0 and 255 for H.265, 2 and 254 for H.264, with no grey lift and no clipping. Full range isn't preserved, 256 levels become at most 220, but the file says what it is.&lt;/p&gt;

&lt;p&gt;I haven't settled whether a pipeline should reject unknown input outright or default it and log a warning. Rejecting is cleaner, but user uploads are messy. Either way, diffing those four fields between input and output of every step that touches video is cheap. For the mislabelled case, a stream copy with &lt;code&gt;h264_metadata&lt;/code&gt; setting the full-range flag to 0, plus &lt;code&gt;-color_range tv&lt;/code&gt; on the container, brought the readings back to 0, 128 and 255 without re-encoding. Restoring a fully &lt;code&gt;unknown&lt;/code&gt; file that way was not tested.&lt;/p&gt;

&lt;p&gt;The wave clip is from Wikimedia Commons, CC0, by Mostafameraji, re-encoded for this test. The grey pattern is my own.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Blurry canvas export: give preview and export separate render paths</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Mon, 16 Nov 2026 08:04:21 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/blurry-canvas-export-give-preview-and-export-separate-render-paths-4p2e</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/blurry-canvas-export-give-preview-and-export-separate-render-paths-4p2e</guid>
      <description>&lt;p&gt;The bug report fits in one line: the text looks crisp in the editor and soft in the exported PNG. A quick fix is to take whatever the preview canvas holds and scale it up on the way out. This test put that fix next to the alternative: one Apple-silicon Mac, desktop Chromium, one run per case. The sample is a homemade text line, bold 28px, black on white, logical size 400×100, target file 800×200. Upscaling the preview bitmap gave an edge transition of 5.98 px. Drawing the text again at 800×200 gave 1.641 px. An 8x supersampled reference came out at 1.768.&lt;/p&gt;

&lt;p&gt;Both metrics are custom. Edge transition width is the average length, row by row, of runs of pixels whose gray value sits between 32 and 223, so closer to the reference means a more faithful edge. Error is the mean absolute gray difference from the reference on a 0 to 255 scale. Neither is a standard, just two numbers I could compare without arguing about taste.&lt;/p&gt;

&lt;h2&gt;
  
  
  Upscaling the preview made edges 3.6 times wider
&lt;/h2&gt;

&lt;p&gt;In path A the backing store equals the CSS size, 400×100, gets exported with &lt;code&gt;toDataURL&lt;/code&gt;, and a bilinear resize takes it to 800×200. Edge width 5.98, error 4.759, identical at DPR 1, 2 and 3, because nothing in that path asks about the screen. Path C ignores the screen and draws the text again on an 800×200 canvas: 1.641 and 1.603. A's edge is about 3.6 times as wide as C's. I spent three years on codecs before moving to graphics tools, and the oldest lesson from scaler work applies: an upscaler cannot recover edge detail that was never rasterized.&lt;/p&gt;

&lt;p&gt;The preview can have the same disease on its own. With the canvas at 400×100 and CSS stretching it to 800×200, the on-screen edge in device pixels measured 5.99 at DPR 1, 11.262 at DPR 2 and 16.756 at DPR 3. Sizing the backing store by DPR brought those to 1.641, 1.873 and 1.639. A real bug, but a preview bug, and its fix belongs in the preview path.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sizing the export by DPR fixed only one screen
&lt;/h2&gt;

&lt;p&gt;Path B is the common DPR fix: multiply the canvas by &lt;code&gt;devicePixelRatio&lt;/code&gt;, call &lt;code&gt;ctx.scale(dpr, dpr)&lt;/code&gt;, export. At DPR 2 it produced 800×200 and matched C exactly. At DPR 1 it collapsed into path A, so anyone on an ordinary monitor still downloaded a 1x file. At DPR 3 it produced 1200×300. Scaled down to 800×200, that one had the lowest error of every run, 1.46, but an edge width of 2.517, wider than the reference. My guess is the downscale filter softened edges while the extra samples pulled the average closer. Which matters more for text people actually read is open; no viewing test was run. Without that downscale, the DPR 3 user ships a third file size. One document, three assets, decided by whose laptop pressed the button.&lt;/p&gt;

&lt;h2&gt;
  
  
  fillText output followed canvas pixels only
&lt;/h2&gt;

&lt;p&gt;Paths A and C produced pixel-identical files at DPR 1, 2 and 3, so the rasterized text depended only on canvas pixels. The maximum red-blue difference was 0 in every export: grayscale antialiasing, no color fringes. A dense screen only makes an underprovisioned canvas easier to see, which is the 5.99 against 11.262 above. Safari, Firefox, Windows ClearType, &lt;code&gt;imageSmoothingQuality&lt;/code&gt;, &lt;code&gt;OffscreenCanvas&lt;/code&gt; and the &lt;code&gt;scale&lt;/code&gt; option in html2canvas are untested.&lt;/p&gt;

&lt;h2&gt;
  
  
  Export should depend on the document and the target size only
&lt;/h2&gt;

&lt;p&gt;So the contract is two functions. Preview takes the document, the screen and a performance budget, and may follow &lt;code&gt;devicePixelRatio&lt;/code&gt; or drop resolution while the user drags. Export takes the document and a target size, nothing else. Codecs draw the same line: what a decoder outputs does not depend on the display that shows it.&lt;/p&gt;

&lt;p&gt;One shipping editor was checked against this contract: ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; . In this test a 1024×1024 PNG with two lines of text was exported from its online editor at screen scale 1 and then 2. Both files stayed 1024×1024 and were pixel-identical. Its site says preview uses adaptive resolution and export recomposites at the original canvas size. The online editor isn't a part I worked on, so what I can vouch for is the site's description plus one test, not its internals. The base image is a CC0 photo from Wikimedia Commons by Bonnie Moreland; the text is homemade.&lt;/p&gt;

&lt;p&gt;A pure export path is also testable: render one document at DPR 1, 2 and 3 and assert identical pixels. Path C passed that check in this run; path B can't by design. Fonts may rasterize differently across engines, so I'd keep one golden file per engine; only Chromium was tested here. If you own an export button, search the export code for &lt;code&gt;devicePixelRatio&lt;/code&gt;, then export one document at DPR 1 and 2 and compare. A hit or two different files means the user's screen is leaking into the asset.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>PNG beat JPEG on one PDF page and lost 7.7x on another</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Sun, 15 Nov 2026 08:01:42 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/png-beat-jpeg-on-one-pdf-page-and-lost-77x-on-another-4ph2</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/png-beat-jpeg-on-one-pdf-page-and-lost-77x-on-another-4ph2</guid>
      <description>&lt;p&gt;"Documents are mostly flat color, so rendered pages come out smaller as PNG than as JPEG." I'd have half believed that myself: I spent three years on codecs, and flat regions really are where DCT suffers. PDF rendering isn't something I've built, so the claim was put to a test on 2026-11-13 with a self-made 3-page A4 PDF: a text page with Chinese body text from 10.5 pt down to 5 pt plus a 0.3 pt hairline table, a chart page with vector bars, lines and a pie, and a photo page. Poppler rendered each page at 216 DPI on white; Pillow and pngquant encoded the same pixels in each format. One Apple silicon Mac, one run per setting. PNG won one page out of three.&lt;/p&gt;

&lt;h2&gt;
  
  
  Same render, three pages, three verdicts
&lt;/h2&gt;

&lt;p&gt;On the chart page the claim holds: PNG was 127,051 bytes, lossless, against 178,232, 201,943 and 233,599 for JPEG q75, q85 and q92. On the text page it falls apart. PNG was 838,404 bytes and JPEG q92 was 796,608 at 45.92 dB, so the "obviously smaller" format is about 5% bigger. On the photo page PNG was 2,431,138 bytes against 550,440 for JPEG q92, which nobody will find surprising.&lt;/p&gt;

&lt;p&gt;The color count points the wrong way too. On a quarter-scale copy, the text page has about 447 colors and 81% white, the chart page about 2,117 colors and 82% white, the photo page about 26,914 colors and 49% white. The page where PNG wins has almost five times the colors of the page where it loses. My reading, which is inference rather than measurement: dense antialiased glyphs leave deflate few long runs, while big flat fills with hard edges make every JPEG block on a boundary pay for ringing. That ringing was counted, as the share of pure-white pixels 1 to 3 pixels from a stroke that moved more than 8 levels after encoding (my own metric, not a standard one). JPEG q75 grayed 11.69% of them on the text page and 10.13% on the chart page; q92 brought that to 0.40% and 0.70%.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encoder effort moved the line again
&lt;/h2&gt;

&lt;p&gt;For a real browser export, the same three pages went through ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; in desktop Chromium on the same Mac, at 216 DPI. It converts in the browser, offers 144, 216 and 300 DPI with 216 as default, and exports PNG, JPEG or WebP. There PNG lost on every page: 881,244 against 818,045 bytes on text, 228,983 against 194,664 on charts, and 3,658,462 against 476,565 on the photo page. That last one is 7.7 times the JPEG, while JPEG and WebP sat at 44.49 and 44.62 dB against the PNG.&lt;/p&gt;

&lt;p&gt;The exported PNG pixels were then re-encoded unchanged with Pillow's optimize. Text went from 881,244 to 602,883 bytes, 32% smaller, and charts from 228,983 to 125,175, so both now beat the JPEG. My guess is that the browser's PNG encoder uses a lighter deflate setting; that code isn't mine and its settings weren't checked. Back on the poppler renders, the smallest lossless result on both non-photo pages wasn't PNG anyway: WebP lossless gave 161,212 and 37,390 bytes, smaller even than a lossy 256-color palette PNG at 359,391 and 49,019.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pick the format per page and write down why
&lt;/h2&gt;

&lt;p&gt;If I designed the encode step of a rendering service, and this isn't my module, I wouldn't set one format. I'd compute cheap signals on a small copy of each page, let them pick lossy or lossless per page, and store the reason with the output. Roughly this shape, filled with the text-page numbers (a sketch, not output from a running service):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"page"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"dpi"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;216&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"signals"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"colors_quarter_scale"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;447&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"white_share"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.81&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"bytes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"png"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;838404&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"jpeg_q92"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;796608&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"webp_lossless"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;161212&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"chosen"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"webp_lossless"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"reason"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"downstream needs lossless text; smallest lossless candidate"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without a reason field, six months later there's no way to tell why page 14 is JPEG and page 15 is WebP short of rerunning the pipeline. It also lets you replay old decisions against new thresholds. I have no thresholds to offer: this is one sample with no scans, handwriting or CMYK pages, and I don't know where a half-photo, half-text page should flip. The sample is self-made; the photo on page 3 is a CC0 image by Bonnie Moreland from Wikimedia Commons. Whatever converter you use, zoom into the smallest text afterwards and check it still looks like the source. If you own a pipeline, encode three of your own pages as PNG with strong deflate, JPEG q92 and WebP lossless, and log bytes and PSNR per page.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Hair-edge error in background removal: about 54 from the model, about 1 from mask upsampling</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Sat, 14 Nov 2026 08:01:51 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/hair-edge-error-in-background-removal-about-54-from-the-model-about-1-from-mask-upsampling-497a</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/hair-edge-error-in-background-removal-about-54-from-the-model-about-1-from-mask-upsampling-497a</guid>
      <description>&lt;p&gt;This is a 2026-11-13 measurement of where the hair-edge error in one background-removal pipeline actually comes from. The pipeline is the common one: shrink the photo to the model's input size, predict a mask, stretch the mask back to the original resolution. When hair comes out mushy, the tempting fix is to blame the last step and reach for a better upsampler. Coming from codec work, the more natural move is to split the distortion into its sources before touching any filter. Split that way, the error had two terms on a 0–255 alpha scale: about 54 from the model disagreeing with a stronger model, and about 1 from the resize round trip.&lt;/p&gt;

&lt;p&gt;The model under test is the INT8 ISNet that the fast tier of ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; loads in a desktop browser on an Apple silicon Mac. Its publicly loaded config gives a 1024×1024 input. The professional tier also takes 1024, and the highest tier, BiRefNet HR-Matting, takes 2048×2048. The same ISNet file was run locally with ONNX Runtime, and the highest tier's mask of a 2048×2048 portrait served as the reference. That reference is a model's output, not ground truth. The portrait was scaled to 768, 1024, 1440 and 1920 px per side to stand in for four "originals", and only the hair band was scored: pixels where the reference alpha sits between 0.02 and 0.98, grown by about 2 px. The number is the mean absolute alpha error inside that band.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separating the two error terms
&lt;/h2&gt;

&lt;p&gt;It takes one control path, which I'll call the ideal model. Take the reference mask, shrink it to 1024, then upsample it back to the original size. That path has no model disagreement in it at all, only resolution loss plus whatever upsampler you pick. Anything the real model scores above that line is the model's share.&lt;/p&gt;

&lt;h2&gt;
  
  
  The resolution round trip cost about one level
&lt;/h2&gt;

&lt;p&gt;At 1440 px, a 1.41× upscale, the ideal model's band error was 1.51 with bilinear and 0.73 with bicubic. At 1920 px, 1.88×, it was 1.43 and 0.79. At 1024 it is zero by construction, and at 768, where the mask gets shrunk instead, 0.89. The share of band pixels off by more than 25 levels never went above 0.02%. On this image the round trip barely registers below 2×, and bicubic takes another 0.6 to 0.8 off bilinear at those two sizes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The model term stayed near 54 at every size
&lt;/h2&gt;

&lt;p&gt;The fast model against the reference scored 51.57 at 768, 55.32 at 1024, 53.85 at 1440 and 54.22 at 1920, with 51.5% to 53.6% of band pixels off by more than 25. A bigger original means a bigger stretch, so one might expect that number to climb with image size. It stayed within a four-level range. The direction of the error matters too. On thin, sparse strands (reference alpha between 0.05 and 0.6) the fast model's alpha summed to about 1.45 times the reference. It wasn't losing hair there. It was calling the strand zone more solid and keeping part of the background between strands.&lt;/p&gt;

&lt;h2&gt;
  
  
  A guided filter made the small term bigger
&lt;/h2&gt;

&lt;p&gt;The obvious hypothesis was that a guided filter, with the original RGB as the guide, radius 2 × side / 1024 and eps 1e-4, would beat bilinear. On the fast model's mask it was slightly worse at every size: 53.38, 55.96, 54.75 and 55.01. On the ideal model it was clearly worse, 11.42 to 14.96 against roughly 1, because it softened edges the reference had kept sharp. A sweep then covered r over 1, 2, 4 and eps over 1e-6, 1e-4, 1e-2 at 1920. The best ideal-model setting, r=1 and eps=1e-2, still landed at 2.67, worse than bicubic's 0.79. With the fast model every setting fell between 54.07 and 55.01, while bilinear sat at 54.22. A filter applied over the whole mask only moves the small term, and in the wrong direction. Whether a filter restricted to the hair region, or a trimap-based refinement pass, would close part of the 54, is unknown. Neither was run here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where I would spend the effort next
&lt;/h2&gt;

&lt;p&gt;Read as a budget, the order is simple. Model choice comes first, because the 54 lives there. A local refinement stage that only touches the hair band comes second; that is my inference from the numbers above, not something measured here. The upsampler comes last. Bicubic took 0.6 to 0.8 off bilinear here, so take it and move on. It is also worth keeping the ideal-model line in every evaluation report, so that the next proposal for a new upsampler arrives with its ceiling already printed: about 1.5 levels at up to 1.88× on this image.&lt;/p&gt;

&lt;p&gt;The limits are real, though. Even the 2048 reference didn't separate single flyaway strands; every mask in this test treated them as one clump, so the band score measures the outer contour of the hair mass, not individual strands. The 54 is disagreement with a stronger model, not distance from truth, and a hand-pulled matte might move both terms. It is one portrait, one machine, one run each, nothing above 1920 px, and the professional tier never went into the table. Nobody can say yet how much of the 54 a better reference would take away.&lt;/p&gt;

&lt;p&gt;Sample photo: "Windblown" by Kaci Baum, CC0, via Wikimedia Commons.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>One text upscale won on SSIM and lost on PSNR, so let the downstream pick the metric</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Fri, 13 Nov 2026 08:33:25 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/one-text-upscale-won-on-ssim-and-lost-on-psnr-so-let-the-downstream-pick-the-metric-1920</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/one-text-upscale-won-on-ssim-and-lost-on-psnr-so-let-the-downstream-pick-the-metric-1920</guid>
      <description>&lt;p&gt;On 2026-11-12 one run pushed eight upscaling methods through the same loop test, and the text sample came back with two answers. Measured per pixel, plain Lanczos won: PSNR 19.97 dB against 19.68 for the fast GAN tier. Measured on structure, the GAN tier won by a wide margin: SSIM 0.9048 against 0.8177. Same pair of files, same script, two metrics that are both standard, and they ranked the methods in opposite orders. Everything below is read off the outputs, so treat it as one outside reading of a table rather than a claim about how any of these models work inside.&lt;/p&gt;

&lt;p&gt;The numbers came out of a loop: a clean 1024×1024 original goes down to 512 or 256 with Pillow 11.3.0 LANCZOS, every method puts it back to 1024, and the result is compared against the original it was made from. Three samples, two scales, seven methods at 2× and eight at 4×, so 21 plus 24 is 45 rows. The interpolation side is Pillow nearest, bicubic and Lanczos. The classic CNN side is OpenCV 5.0.0 dnn_superres with the official ESPCN, FSRCNN, EDSR and LapSRN weights. SSIM comes from scikit-image 0.24.0, PSNR from a few lines beside it. Every GAN number here comes from a single tier of ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; , the fast preset whose panel names Real-ESRGAN General x4v3, and only that tier was run: one Apple-silicon Mac, desktop Chromium, one run per cell. Portrait and landscape samples are CC0 images from Wikimedia Commons by William Stitt and Bonnie Moreland; the text sample is my own.&lt;/p&gt;

&lt;h2&gt;
  
  
  Opposite orders on the same text sample
&lt;/h2&gt;

&lt;p&gt;PSNR asks how far each pixel moved. SSIM asks whether the local structure still looks like the structure in the original. The GAN tier redraws a stroke sharp and lands it a hair off from where it used to be, so subtracting pixel by pixel makes that displacement expensive, while a structural comparison puts it closer to the original than anything else in the table. Neither number is wrong. They answer different questions, and on this sample the two questions disagree.&lt;/p&gt;

&lt;p&gt;EDSR x4 is the other half of the story. On the same text sample it scored 18.74 / 0.8018, the worst pair in the whole run, below even nearest neighbour at 19.27, and the failure is visible as a second faint stroke beside the first. The larger model bought nothing here, on this one text sample with this set of pretrained weights.&lt;/p&gt;

&lt;h2&gt;
  
  
  Whoever reads the pixels picks the metric
&lt;/h2&gt;

&lt;p&gt;So the useful question is not which of the two metrics is correct. It is what happens to the image after the stage finishes.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What the image is for next&lt;/th&gt;
&lt;th&gt;Which number to read&lt;/th&gt;
&lt;th&gt;What this run showed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Text somebody will read back word for word&lt;/td&gt;
&lt;td&gt;per-pixel error inside the text region&lt;/td&gt;
&lt;td&gt;the fast tier's body-text region came in at 17.81 dB, lowest of the eight, while its whole-image SSIM was the highest at 0.9048&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A person glancing at a photo on screen&lt;/td&gt;
&lt;td&gt;structural similarity&lt;/td&gt;
&lt;td&gt;0.9048 against 0.8177 on the same text sample&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A face whose details have to be recognised&lt;/td&gt;
&lt;td&gt;both, which here happened to agree&lt;/td&gt;
&lt;td&gt;the fast tier's portrait was 30.28 / 0.8145, against Lanczos at 32.50 / 0.8523&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A flat area such as sky or paper&lt;/td&gt;
&lt;td&gt;neither, and skip the upscale&lt;/td&gt;
&lt;td&gt;all eight methods landed between 46.57 and 51.06 dB in the sky region&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Both region figures come from rectangles drawn by hand on the original, one over a block of body text and one over open sky, not from any automatic segmentation. The first row is the interesting one. The version that looks clearest on screen is the version that is least trustworthy for checking a word against the original, and a single quality column would have hidden that completely. ImgIng's own notes, read on 2026-11-12, draw the same line: the output suits visual enhancement, not verification against the original text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Record both numbers and the consumer they are for
&lt;/h2&gt;

&lt;p&gt;That leaves something cheap to do inside the stage. Write down both numbers, the downstream they were judged for, and enough about the pixels to say later where they came from. A record carrying one number is where the trouble begins. Six months on, a 0.90 sitting in a column called quality says nothing about whether the text in that image can be quoted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# shape of the record, not the measuring script; the numbers above come from one loop-test run&lt;/span&gt;
&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"invoice_0412.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"method"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"gan_sr"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;# interpolation | classic_cnn | gan_sr&lt;/span&gt;
    &lt;span class="s2"&gt;"scale"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"weights"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"real-esrgan-general-x4v3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"psnr"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;19.68&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"ssim"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.9048&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"judged_for"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"preview"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="c1"&gt;# preview | text_readback | archive&lt;/span&gt;
    &lt;span class="s2"&gt;"original_kept"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The limits are worth stating plainly, because a table of 45 rows invites over-reading. Three samples, one machine, one run per cell, one tier out of five on the GAN side, and the low-resolution inputs were produced by downscaling clean originals. A genuinely blurry photo also carries focus error, hand shake, high-ISO noise and the damage of being re-encoded by a few apps on the way, so none of this predicts what a method will do to that photo. With no repeats, no cell here has an error bar either. And how much of the GAN tier's per-pixel penalty is pure displacement that a small alignment step would give back, this run does not say.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Four DOCX converters agreed on screen and disagreed in the markup</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Thu, 12 Nov 2026 08:03:05 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/four-docx-converters-agreed-on-screen-and-disagreed-in-the-markup-jfe</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/four-docx-converters-agreed-on-screen-and-disagreed-in-the-markup-jfe</guid>
      <description>&lt;p&gt;On one self-made DOCX, four converters put the same merged-cell table on screen in the same shape, and the HTML underneath did not match. Two of them wrote 2, 5, 3 and 3 cells for the four rows of that table; the other two wrote 2, 5, 4 and 5. Both look right in a browser, so an acceptance gate made of screenshots waves both through, and whatever reads the table next gets two different documents.&lt;/p&gt;

&lt;p&gt;The scope is narrow. One sample document, 43,790 bytes, built on purpose: headings, lists, two generated PNGs, header, footer, a footnote, and three tables, horizontal merges, vertical merges, and a mixed one at 5 columns by 4 rows. One Apple-silicon Mac, a desktop Chromium, one run per tool. The four were Mammoth 1.13.0, pandoc 3.13, docx-preview 0.4.1, and the browser-side DOCX to HTML on ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; which ran without signing in. A forty-line reconstruction script went in as a control. Office formats are not the part of that product I work on, so everything below is read off the outputs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The column count lives in tblGrid and the rowspan does not exist
&lt;/h2&gt;

&lt;p&gt;Two facts about the format decide the whole comparison. A table's column count comes from the &lt;code&gt;w:gridCol&lt;/code&gt; children of &lt;code&gt;w:tblGrid&lt;/code&gt;, not from how many &lt;code&gt;w:tc&lt;/code&gt; a row happens to have, and a horizontal merge is a plain number that copies straight into &lt;code&gt;colspan&lt;/code&gt;. A vertical merge has no equivalent, because OOXML carries no rowspan field at all.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;w:gridCol&lt;/span&gt; &lt;span class="na"&gt;w:w&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"1728"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt; ... &lt;span class="c"&gt;&amp;lt;!-- five of these inside w:tblGrid = five grid columns --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;w:tc&amp;gt;&amp;lt;w:tcPr&amp;gt;&amp;lt;w:gridSpan&lt;/span&gt; &lt;span class="na"&gt;w:val&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&amp;lt;/w:tcPr&amp;gt;&lt;/span&gt; ...   &lt;span class="c"&gt;&amp;lt;!-- becomes colspan="2" --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;w:tc&amp;gt;&amp;lt;w:tcPr&amp;gt;&amp;lt;w:vMerge&lt;/span&gt; &lt;span class="na"&gt;w:val&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"restart"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&amp;lt;/w:tcPr&amp;gt;&lt;/span&gt; ... &lt;span class="c"&gt;&amp;lt;!-- the merge starts here --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;w:tc&amp;gt;&amp;lt;w:tcPr&amp;gt;&amp;lt;w:vMerge/&amp;gt;&amp;lt;/w:tcPr&amp;gt;&amp;lt;w:p/&amp;gt;&amp;lt;/w:tc&amp;gt;&lt;/span&gt;        &lt;span class="c"&gt;&amp;lt;!-- continuation, no w:val --&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So a row holding 2 cells is correct against 5 grid columns, and the spans a converter emits are a reconstruction, not a copy. That is where the two groups part ways.&lt;/p&gt;

&lt;h2&gt;
  
  
  The extra cells are hidden, not wrong
&lt;/h2&gt;

&lt;p&gt;In the mixed table, Mammoth, pandoc and the control script emitted exactly the expected 2, 5, 3, 3. docx-preview and the browser-side converter emitted 2, 5, 4, 5, and the three extra cells are empty &lt;code&gt;&amp;lt;td&amp;gt;&lt;/code&gt; elements carrying &lt;code&gt;display:none&lt;/code&gt;. The purely vertical table behaved the same way: 3, 3, 2, 2 from the first group, 3, 3, 3, 3 from the second, the two extras hidden again. Walk any of these outputs with a running account of rowspan carry-over and every row comes out at exactly five: on a well-formed file all of them got the merge values right. The familiar claim that open-source libraries cannot handle merged cells did not reproduce here, on this sample and these versions.&lt;/p&gt;

&lt;p&gt;One detail from the same run: the JavaScript and the Python builds of Mammoth produced byte-identical files, both 10,167 bytes.&lt;/p&gt;

&lt;h2&gt;
  
  
  One missing restart split the failure two ways
&lt;/h2&gt;

&lt;p&gt;The only real breakage this round came from the document, not the tools. In a variant of the sample, the first cell of the vertically merged group carries a bare &lt;code&gt;&amp;lt;w:vMerge/&amp;gt;&lt;/code&gt; instead of &lt;code&gt;&amp;lt;w:vMerge w:val="restart"/&amp;gt;&lt;/code&gt;, every other byte left alone. Mammoth 1.13.0 and pandoc 3.13 both treated the header cell one row above as the start of the merge, gave it &lt;code&gt;rowspan="4"&lt;/code&gt;, and dropped the text of the cell that should have started the merge: that string is nowhere in the output. docx-preview 0.4.1 and the browser-side converter kept the text and dropped the merge, leaving four plain rows. Neither group raised an error. One side loses characters, the other loses structure, and a single non-standard attribute is enough. Why the split runs in that direction I can't tell from the outputs alone, and I didn't go reading either implementation to find out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Assert on the grid and on the text
&lt;/h2&gt;

&lt;p&gt;That leaves two checks worth putting in CI for this step, both cheap. Rebuild the grid: take the column count from &lt;code&gt;w:tblGrid&lt;/code&gt;, add up &lt;code&gt;colspan&lt;/code&gt; per row, carry each &lt;code&gt;rowspan=n&lt;/code&gt; down into the next n-1 rows, and require every row to come out exactly equal to the column count. Drop cells styled &lt;code&gt;display:none&lt;/code&gt; before counting, otherwise the second group reports a row occupying six slots out of five and you spend an afternoon on a non-problem. Then diff the content: pull every &lt;code&gt;w:t&lt;/code&gt; string out of &lt;code&gt;document.xml&lt;/code&gt; and look for each one in the converted HTML. In this round the text check was the only thing that caught the missing string. The script that produced the expected grid is forty-odd lines and deliberately minimal: one anchor per column slot pointing at the cell still merging downward, no support for &lt;code&gt;w:tblHeader&lt;/code&gt;, nested tables, &lt;code&gt;w:tcBorders&lt;/code&gt; or diagonal headers.&lt;/p&gt;

&lt;p&gt;All of this is one document, one machine, one run each, and the sample's footnote and header were written into the package by script rather than saved out of a real Word, so the file's own standing is partly inferred. A real business document with floating shapes, equations and section breaks could reorder the result. What I would keep anyway: the gate on a table conversion is the reconstructed grid plus the text inventory, not how the page looks.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>One runtime, one GPU, two matting models: one matched WASM to 1.3e-6, the other returned an empty mask</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Wed, 11 Nov 2026 08:02:18 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/one-runtime-one-gpu-two-matting-models-one-matched-wasm-to-13e-6-the-other-returned-an-empty-387a</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/one-runtime-one-gpu-two-matting-models-one-matched-wasm-to-13e-6-the-other-returned-an-empty-387a</guid>
      <description>&lt;p&gt;The table looked like a clean result for WebGPU. Same model, same frame, same machine: Robust Video Matting with the MobileNetV3 backbone, FP16, 7.5 MB, needed 35.0 ms per steady-state frame on WebGPU and 87.2 ms on 4-thread WASM at 1280×720. Then came the alpha it had produced. By frame 13 the WebGPU mask had 0.00% foreground pixels against 5.03% on WASM, and nothing had complained. Session creation succeeded, every &lt;code&gt;run()&lt;/code&gt; resolved, and both backends handed back a &lt;code&gt;Float16Array&lt;/code&gt;. Those 35 ms were the time it took to compute a wrong answer.&lt;/p&gt;

&lt;p&gt;All of this is one machine: an Apple M4 Mac, desktop Chromium, ONNX Runtime Web 1.27.0. The RVM file and its settings match what the video matting page in ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; (a beta feature for desktop browsers) ships in its client script, with full frames going in, &lt;code&gt;downsample_ratio&lt;/code&gt; at 512 over the long edge, and recurrent state carried from frame to frame. The benchmark copied those settings from the script so it would match a real deployment. Steady state is the median of frames 2 to 13, and the WebGPU and 4-thread WASM figures are medians of 3 separate browser processes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The faster backend returned an empty mask
&lt;/h2&gt;

&lt;p&gt;At 512×288, 1280×720 and 1920×1080, WebGPU took 29.4, 35.0 and 43.6 ms per frame, 4-thread WASM 89.4, 87.2 and 91.8 ms, and single-thread WASM 228.3, 251.2 and 279.3 ms (one run). Every WebGPU number there belongs to unusable output. On frame 1 its mask kept 1.85%, 0.54% and 0.37% foreground against about 5.05% on WASM, and after twelve rounds of state feedback it was 0.00% at all three sizes. The ResNet50 variant ran 51, 86 and 95 ms on WebGPU against 307, 504 and 512 ms on WASM, and its WebGPU output was wrong too, just not empty: 1.9 to 3.5% foreground on frame 11 where WASM stayed near 5.0%. A thin mask is harder to spot than a blank one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same runtime got another model right
&lt;/h2&gt;

&lt;p&gt;On September 28, same machine, same ORT build, an earlier run had timed ISNet still-image matting at 1024×1024. The INT8 version ran 359 ms on WebGPU and 2133 ms on 4-thread WASM, and the two alphas matched to a maximum per-pixel difference of 1.3×10⁻⁶.&lt;/p&gt;

&lt;p&gt;So the device and the runtime stayed put, and only the model changed. A rule like "adapter found, use WebGPU" would have been right for ISNet and silently wrong for RVM on the same machine. The cause hasn't been traced. My guess is how the WebGPU path handles some operators or FP16 precision in this recurrent network, but there was no operator-by-operator comparison, so all I can say is that this model on this machine produced an empty mask on WebGPU. On Windows with a discrete GPU, Safari or Firefox, RVM might come out fine. None of those were tested.&lt;/p&gt;

&lt;h2&gt;
  
  
  A non-empty check on frame 1 would have passed
&lt;/h2&gt;

&lt;p&gt;At 1280×720 the first WebGPU frame still had 0.54% foreground, so an "is the mask all zeros" test passes. It took a dozen frames of recurrent state to drive it to zero. What caught it was comparing against WASM on the same input: mean absolute difference 0.050 to 0.069, with 5.0 to 6.8% of pixels off by more than 0.5. For a temporal model the reference has to span several frames with state carried over. The speed table also hid a smaller thing: converting the alpha from FP16 to 32-bit floats in JavaScript added roughly 77 to 88 ms per 1080p frame, longer than the 43.6 ms WebGPU inference beside it, which was producing an empty mask anyway.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pick the backend per model and verify it once
&lt;/h2&gt;

&lt;p&gt;From the outside, ImgIng already splits it this way. Its two RVM tiers are pinned to WASM, and the page says this avoids some WebGPU devices silently producing empty masks. Its heaviest tier, MatAnyone2, tries WebGPU first and switches to WASM with a progress message if that fails. That's a per-model policy, and after these numbers I think it's the right shape.&lt;/p&gt;

&lt;p&gt;If I were writing the loader, the device check would stay but stop being the decision. &lt;code&gt;navigator.gpu&lt;/code&gt; existing isn't enough, since &lt;code&gt;requestAdapter()&lt;/code&gt; can return null, and passing &lt;code&gt;['webgpu', 'wasm']&lt;/code&gt; falls back silently without telling you which backend ran. I'd create the WebGPU session explicitly, push a bundled 13-frame reference clip through it, compare against stored WASM alphas, and cache the verdict per model hash and adapter. Session creation took about 270 to 290 ms on either backend for the 7.5 MB model, so that's roughly one extra cold start on first use.&lt;/p&gt;

&lt;p&gt;The column I now put next to the milliseconds is maximum difference against WASM after state has been carried through. Without it, the RVM rows above read as a 2.1 to 3.0× win for a backend whose mask was empty.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Screen recordings get fat at capture time: 50,002 bytes per still frame at constant bitrate, 77 after re-encoding</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Tue, 10 Nov 2026 08:02:06 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/screen-recordings-get-fat-at-capture-time-50002-bytes-per-still-frame-at-constant-bitrate-77-4666</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/screen-recordings-get-fat-at-capture-time-50002-bytes-per-still-frame-at-constant-bitrate-77-4666</guid>
      <description>&lt;p&gt;A 4-minute 1080p screen recording that weighs 360 MB looks like a compression problem. On the file I measured, one setting at capture time decided most of that weight. A still frame of a slide cost 50,002 bytes in the original and 77 bytes after a quality-based re-encode, and the small text scored almost identically in both.&lt;/p&gt;

&lt;p&gt;The sample is synthetic, which limits how far the numbers travel. It's a home-built 4-minute mock lecture recording with page turns, pointer moves, a burst of typing and one scroll, 1920×1080 at 30 fps, encoded as H.264 at a constant 12 Mbps, 360.0 MB, no audio. The frames are screenshots of a page I wrote, so there's no noise, and for about 78% of the running time nothing moves. A real recording will be harder to compress. I picked the 12 Mbps myself. Neither Apple nor Microsoft documents a default bitrate or frame rate for its built-in recorder, and the Xbox capture page only says the quality setting controls resolution and bitrate. All of it is one pass per setting on an Apple M4 Mac with ffmpeg (x264), HandBrake and macOS's avconvert. 1 MB here is 1,000,000 bytes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Constant bitrate pays for frames where nothing changed
&lt;/h2&gt;

&lt;p&gt;Non-key frames in the still stretches have a median of 50,002 bytes, about the same as frames where text is being typed or the handout is scrolling. The encoder had 12 megabits to spend every second and spent them whether or not the screen changed. The still stretches held 71.1% of all bytes, roughly their share of the running time: the bytes followed the clock instead of the content.&lt;/p&gt;

&lt;p&gt;Re-encoding the same file with x264 at CRF 23, preset medium and otherwise defaults, brought it to 5.2 MB, 98.5% smaller. The median still frame dropped to 77 bytes. Typing frames came in at 80 bytes, and only scrolling needed real money at 906. My reading, not isolated in a separate test, is that a still macroblock can be coded as a skip for a few bits, and constant bitrate throws that saving away by padding up to the target. On small English text at the 60-second mark, SSIM against the lossless master was 0.970 for the re-encode and 0.971 for the 360 MB original.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keyframes carry the cost once still frames are free
&lt;/h2&gt;

&lt;p&gt;After the re-encode the weight moves to keyframes. The 29 keyframes averaged 140,557 bytes each, so one of them is worth roughly 1,600 still frames, and together they held 79.4% of the video stream. Two camera clips from Wikimedia Commons (CC BY-SA, re-encoded) look the opposite way: 59.4% and 77.7% of their bytes sit in P frames. Presumably sensor noise leaves residual in every block even when the shot looks static; I didn't isolate that. The three slide flips didn't trigger a keyframe at all, and x264 coded each new slide as a P frame of 22 to 36 KB. My guess is that the rest of the screen stayed put, so prediction still paid off.&lt;/p&gt;

&lt;p&gt;That makes the keyframe interval the lever for recordings. Stretching the keyframe interval from 2 seconds to 10 cut the recording from 18,065,145 to 4,402,121 bytes, 75.6% smaller, while the camera clip shrank only 5.0%. The price is seeking: to land on an arbitrary frame, the decoder works through about 150 frames on average instead of about 30. Text quality didn't move with the interval.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixed-rate re-encoding keeps part of the waste
&lt;/h2&gt;

&lt;p&gt;The avconvert same-size H.264 preset returned 359,952,116 bytes, effectively the original. Its HEVC preset reached 60.5 MB at a fixed 2.0 Mbps or so, still about 11 times the CRF 23 file. HandBrake's Fast 1080p30 preset is quality-based and produced 6.0 MB with small-text SSIM 0.969. The browser-side data point was ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; , whose video compression runs on local files inside a desktop browser and supports batch jobs. On its smart recommendation setting, H.264 came out at 20.2 MB with SSIM 0.969, the same text tier as HandBrake at roughly three times the size, and H.265 came out at 30.5 MB, larger than its own H.264. The page made no upload requests during these runs. How it picks its settings I can't tell from the outside.&lt;/p&gt;

&lt;h2&gt;
  
  
  The recorder setting decides most of the size
&lt;/h2&gt;

&lt;p&gt;Then the control case. The same lossless frames went into the Mac's hardware H.264 encoder (VideoToolbox) with a 12 Mbps target but no constant-bitrate constraint, and came out at 23,737,333 bytes, about 0.79 Mbps. Same picture, same machine, about a fifteenth of the size. So not every screen recording is fat. It depends on whether the recorder writes a fixed rate or lets the content decide, and I didn't test which mode the stock macOS or Windows recorders actually use.&lt;/p&gt;

&lt;p&gt;If your recorder offers a quality-based or variable-bitrate mode, pick it and stop thinking in megabits per second. If it only offers a bitrate, record at that and re-encode afterwards with CRF and a 10-second keyframe interval for anything viewers won't scrub through constantly. Before touching frame rate or resolution, run ffprobe on a recording you have and compare a still frame's size with a typing frame's. If they come out about the same, your bytes are going to the clock.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Each OCR engine had its own minimum text height, and none had a maximum up to 60 px</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Mon, 09 Nov 2026 08:01:52 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/each-ocr-engine-had-its-own-minimum-text-height-and-none-had-a-maximum-up-to-60-px-5h51</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/each-ocr-engine-had-its-own-minimum-text-height-and-none-had-a-maximum-up-to-60-px-5h51</guid>
      <description>&lt;p&gt;When a screenshot is too small for OCR, upscaling it first is the tempting move. After three years on video codecs that bothers me: the detail was spent, or not, at capture time. So here is a test of how much text height each recognizer needs, and what upscaling recovers below it.&lt;/p&gt;

&lt;p&gt;The samples are deliberately clean: a self-written Chinese passage of 275 characters and an English one of 452, black on white, single column, rendered directly at each height. Text height is the ink bounding box of 国 for Chinese and of a capital H for English. Nine heights from 6 to 60 px, two Chinese fonts: Songti SC, a serif with thin horizontals, and Heiti SC, a sans. Readers were macOS built-in text recognition in accurate mode, Tesseract 5.5.3 on English only, and the image text recognition in ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; , run in a desktop browser. One run each on an Apple M4 Mac. Accuracy is 1 minus character edit distance over length; one wrong Chinese character costs 0.36 points.&lt;/p&gt;

&lt;h2&gt;
  
  
  The floor is different for every recognizer
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Text height&lt;/th&gt;
&lt;th&gt;macOS Songti&lt;/th&gt;
&lt;th&gt;macOS Heiti&lt;/th&gt;
&lt;th&gt;ImgIng Songti&lt;/th&gt;
&lt;th&gt;ImgIng Heiti&lt;/th&gt;
&lt;th&gt;Tesseract English&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;6 px&lt;/td&gt;
&lt;td&gt;1.82&lt;/td&gt;
&lt;td&gt;6.91&lt;/td&gt;
&lt;td&gt;71.27&lt;/td&gt;
&lt;td&gt;95.27&lt;/td&gt;
&lt;td&gt;16.81&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8 px&lt;/td&gt;
&lt;td&gt;92.0&lt;/td&gt;
&lt;td&gt;88.0&lt;/td&gt;
&lt;td&gt;97.82&lt;/td&gt;
&lt;td&gt;98.55&lt;/td&gt;
&lt;td&gt;95.35&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10 px&lt;/td&gt;
&lt;td&gt;96.0&lt;/td&gt;
&lt;td&gt;99.64&lt;/td&gt;
&lt;td&gt;98.55&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;99.34&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12 px&lt;/td&gt;
&lt;td&gt;99.64&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;99.34&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16 px&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20 px&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;30 px&lt;/td&gt;
&lt;td&gt;99.64&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;40 px&lt;/td&gt;
&lt;td&gt;99.64&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;60 px&lt;/td&gt;
&lt;td&gt;99.27&lt;/td&gt;
&lt;td&gt;99.64&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Every column hits a cliff at a different row. The browser recognizer was error-free on Chinese from 12 px in Songti and 10 px in Heiti. macOS was clean from 12 px in Heiti, but Songti only reached zero errors at 16 px. Tesseract needed 16 px for clean English. From 12 to 40 px both Chinese recognizers sat between 99.64% and 100%, and macOS Songti dipped to 99.27% at 60 px; 8 px is where they split. At 6 px macOS is essentially gone. Near the floor the font matters about as much as the engine: at 10 px macOS made 11 errors on Songti and 1 on Heiti, which fits Songti's horizontals shrinking to about a pixel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nothing got worse up to 60 px
&lt;/h2&gt;

&lt;p&gt;Large text didn't hurt in this range. The browser recognizer stayed at 100% from 12 to 60 px on both fonts, Tesseract at zero errors from 16 to 60. The macOS losses at 30, 40 and 60 px are all one event: the last character of the first line went missing, a line-end drop rather than a misread glyph. The useful number is a floor, not a sweet spot. The test stopped at 60 px, so where a real ceiling starts is still open.&lt;/p&gt;

&lt;h2&gt;
  
  
  Upscaling won back part of the gap
&lt;/h2&gt;

&lt;p&gt;Then the obvious fix: resizing the 6 to 12 px images to roughly 32 px text height with bilinear, bicubic and Lanczos. It suits Tesseract: English at 8 px went from 21 errors to 0 with bilinear. macOS recovered a lot at 6 px, Heiti from 256 errors to 37, yet Songti at 6 px peaked at 53.1% while native 12 px scored 99.64%. The browser recognizer barely moved, and Lanczos often made it worse, Songti at 6 px going from 79 errors to 133. Bilinear was best or tied most often; I suspect Lanczos ringing on stroke edges but haven't checked. Interpolation moves a small image into the scale an engine expects. My reading, not something this test isolated, is that it can't restore strokes that already merged at six rows tall, any more than upscaling a 360p encode recovers what the encoder dropped.&lt;/p&gt;

&lt;p&gt;One side note: the same 8 px Heiti image pasted unscaled onto a 1200×1200 white canvas dropped macOS from 88.0% to 0%, so for that engine the size of the whole image seems to matter too; why is only a guess.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measure the text height first
&lt;/h2&gt;

&lt;p&gt;This all needs the text height in hand, and two cheap estimators held up. Horizontal projection binarizes with Otsu and takes the median height of ink-bearing row runs. Connected components takes the 90th percentile of component heights, since Chinese characters split into parts. On the Chinese renders both landed within ±2 px up to 30 px. On English, projection measures the whole line, about 1.37 times cap height, so trust connected components there. Ruled tables break projection: one bordered table came out as a single 559 px line, connected components gave 23. A self-made slide screenshot at device pixel ratio 1 measured about 19 to 20 px, a chat screen at ratio 3 about 41 to 45 px.&lt;/p&gt;

&lt;p&gt;A sensible order from these numbers: measure first, and above about 16 px leave the image alone. If you control capture, take it larger with a higher device pixel ratio or zoom, since native 12 px beat every upscaled 6 px result here. If not, and Chinese text is under 12 px, consider upscaling to about 30 px with bilinear and compare one output with the original rather than assume it helped. These were clean renders, so real screenshots and photos may do worse, and single runs can shift by a character or two.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Size PDF downsampling tiers by the smallest text you must read, not by dpi</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Sun, 08 Nov 2026 08:01:27 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/size-pdf-downsampling-tiers-by-the-smallest-text-you-must-read-not-by-dpi-1983</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/size-pdf-downsampling-tiers-by-the-smallest-text-you-must-read-not-by-dpi-1983</guid>
      <description>&lt;p&gt;A downsampling tier is usually labeled by image resolution: 72, 150, 300 dpi. On a scanned page that label hides the real question, because the whole page is one image and the text is just pixels inside it. The number that decides whether a tier is safe is the smallest point size someone still has to read. In a downsampling ladder tested on 2026-11-06, 150 dpi kept 6 pt Chinese and English above 99% recognition accuracy, while 72 dpi pulled even 12 pt Chinese down to 71.0%.&lt;/p&gt;

&lt;p&gt;Scope before numbers. The test page is a self-made A4 page image: four Chinese and four English sentences at 12, 10, 8 and 6 pt in one Hei typeface, drawn at 300 dpi with a scan look added (noise, blur, slight rotation) and stored as JPEG, 1,591,319 bytes for the page. It was forced down with Ghostscript 10.07.1 to 300, 200, 150, 100 and 72 dpi, every result was rendered back at 300 dpi so the recognizer sees an enlarged view, and scored each block with macOS text recognition, where accuracy is 1 minus edit distance over character count. English got a second opinion from Tesseract 5.5.3, with only its English model installed. One Apple M4 Mac, one page, one run per step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gs &lt;span class="nt"&gt;-o&lt;/span&gt; out.pdf &lt;span class="nt"&gt;-sDEVICE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;pdfwrite &lt;span class="nt"&gt;-dPDFSETTINGS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/ebook &lt;span class="nt"&gt;-dDownsampleColorImages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt; &lt;span class="nt"&gt;-dColorImageResolution&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;100 &lt;span class="nt"&gt;-dColorImageDownsampleThreshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1.0 &lt;span class="nt"&gt;-dGrayImageResolution&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;100 &lt;span class="nt"&gt;-dGrayImageDownsampleThreshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1.0 &lt;span class="k"&gt;in&lt;/span&gt;.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The threshold of 1.0 is deliberate: the stock presets only downsample images more than 1.5 times above the target, so a preset-only ladder would quietly skip steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the text gave out
&lt;/h2&gt;

&lt;p&gt;Down to 150 dpi nothing broke. The 200 dpi step scored 100% on every block, and 150 dpi only lost a little on 6 pt Chinese (99.1%) while the file went from 624,930 bytes at 300 dpi to 167,933. One oddity I haven't explained: the 300 dpi step (re-encoded, not downsampled) scored 99.1% on 12 pt Chinese, slightly lower than the 200 dpi step. My guess is recognizer noise; I didn't chase it.&lt;/p&gt;

&lt;p&gt;At 100 dpi (87,346 bytes) Chinese started to go. 8 pt fell to 96.3% and 6 pt to 81.3%, with errors like 清晰 ("clear") coming back as 清断 and 逾期 ("overdue") as 適期. English 6 pt still scored 99.1% with macOS and 94.3% with Tesseract. At 72 dpi (61,010 bytes) the Chinese blocks scored 71.0%, 41.1%, 46.7% and 0% from 12 pt down to 6 pt. English held at 100% for 12 pt, then dropped to 84.7% at 8 pt and 11.3% at 6 pt; Tesseract gave 99.1, 94.3, 76.0 and 41.1% going from 12 down to 6 pt.&lt;/p&gt;

&lt;p&gt;The pixel arithmetic is short: glyph size in pixels is roughly point size × dpi ÷ 72. 6 pt at 100 dpi is about 8 px, at 72 dpi about 6 px, and 8 pt at 72 dpi is back at 8 px. My inference from this one typeface, and it is only that, is that dense CJK strokes start turning into blobs somewhere around 8 px. That would explain why Chinese gave out a full tier before English did.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the score does not tell you
&lt;/h2&gt;

&lt;p&gt;Two things kept me from drawing a clean curve. At 72 dpi part of the Chinese loss is whole lines the recognizer skipped rather than wrong characters, which probably explains why 10 pt scored below 8 pt. And macOS recognition runs with language correction, so it can guess some words right from context and likely flatters the low steps; Tesseract on English fell earlier and more evenly.&lt;/p&gt;

&lt;p&gt;Resolution is not the only knob either. On a separate two-page scanned sample (300 dpi page images, 2,357,639 bytes), two tools that both landed at 72 dpi gave very different 6 pt results: 41.5% for one and 7.6% for Ghostscript /screen. Same dpi, so my guess is the gap comes from encoding; I didn't dig into which settings. The 41.5% came from ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; , run in a desktop browser on the same Mac. Its PDF engine isn't a part I worked on, so I'm reading it from the outside. Its tiers are screen 72, ebook 150 (marked as recommended), print 300 and a structure-only option, and images already below a tier's dpi aren't resampled. At 150 dpi that sample came out at 637,933 bytes, more than double iLovePDF's 280,869 and Ghostscript /ebook's 316,227, while 6 pt recognition stayed within a point of both (98.8% against 98.2%). Whether those extra bytes buy anything depends on how you weight the small-text area; I have no test that settles it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turning it into tiers
&lt;/h2&gt;

&lt;p&gt;If I were writing the acceptance test for a tier list, the input would be the smallest text the document type carries, not the image's native resolution: fine print on a form, 6 pt table footnotes, 10 pt body copy. The test page needs that size in every script you ship, since CJK and Latin don't fail at the same step. Run the ladder, recognize each step, and keep the lowest dpi that still scores around 99% on the smallest size. On this page that answer was 150 dpi for 6 pt Chinese, while 100 dpi only passes if the smallest text is 8 pt English (100% macOS, 99.6% Tesseract). 72 dpi belongs to a tier labeled for screens and slides, with a note that scans carrying small print shouldn't go there.&lt;/p&gt;

&lt;p&gt;The gap I haven't closed is typefaces. This was one Hei face and one noise profile, with no thin-stroked Song or Ming faces, which I'd expect to break earlier. That is the next page I'd add to the ladder before trusting 150 dpi for every document.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Three color checkpoints for HEIC to JPEG, and why sips -g profile isn't one</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Sat, 07 Nov 2026 08:01:35 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/three-color-checkpoints-for-heic-to-jpeg-and-why-sips-g-profile-isnt-one-387o</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/three-color-checkpoints-for-heic-to-jpeg-and-why-sips-g-profile-isnt-one-387o</guid>
      <description>&lt;p&gt;Why do some HEIC-to-JPEG conversions come out washed out? The measurements I ran on 2026-11-06 pointed to three checks rather than to a library. Is there an ICC profile after decode, does the encoder carry it or convert the pixels, and what does your inspector actually read. The third is the easiest to get wrong.&lt;/p&gt;

&lt;p&gt;Scope first. Three HEIC files were encoded with macOS &lt;code&gt;sips&lt;/code&gt; from a synthetic chart and a CC0 Wikimedia Commons photo; the two used here are tagged Display P3 with a 536-byte ICC. They are not iPhone originals: no HDR gain map, no 10-bit, no nclx. One Apple M4 Mac, single runs, Pillow 11.3, sharp 0.34.5, Chromium 149 and WebKit 26.5. Color difference is CIEDE2000 on 12 patches and the full photo; "below 1 is hard to see, 2 to 3 shows side by side" is a rule of thumb, not something I measured.&lt;/p&gt;

&lt;h2&gt;
  
  
  The decoder decides what you are holding
&lt;/h2&gt;

&lt;p&gt;After decode you have P3 numbers plus a profile, P3 numbers with nothing attached, or pixels already converted to sRGB, and mixing those up is how a file ends up washed out. Browser canvases convert at decode time. WebKit 26.5 decoded the HEIC through the macOS decoder and handed over sRGB pixels; Chromium 149 couldn't decode HEIC at all, and given a P3 PNG it converted to sRGB too. sharp 0.34.5 from npm read the metadata fine (ICC 536 bytes, EXIF present), then failed on the pixels with "Support for this compression format has not been built in", since the prebuilt binary only ships AVIF decoding. The risky path is raw values out of libheif or a WASM build: P3 numbers, and the profile is your job. I didn't rerun libheif this time; a September run showed exactly that.&lt;/p&gt;

&lt;h2&gt;
  
  
  The encoder either carries the profile or converts the pixels
&lt;/h2&gt;

&lt;p&gt;Pillow's plain &lt;code&gt;save()&lt;/code&gt; writes the JPEG without the ICC and leaves the numbers alone. That was the only place the washed-out look showed up. The P3 skin patch [189,124,88], shown as sRGB, lands ΔE2000 3.24 off (a correct conversion gives [199,120,81]). Sky blue came out at 2.32, deep green at 3.33, saturated P3 pure red at 6.98. On the photo, all inside sRGB, the mean was 1.94 and the 95th percentile 3.39.&lt;/p&gt;

&lt;p&gt;The two correct options part ways at the gamut edge. Carrying P3 (sips default, Pillow with &lt;code&gt;icc_profile&lt;/code&gt; passed, sharp &lt;code&gt;keepIccProfile()&lt;/code&gt;) kept in-gamut patches at 0.31 or below and saturated ones at 0.40 or below. Converting to sRGB kept in-gamut patches at 0.37 or below, but saturated P3 colors still read up to 7.55 off because sRGB can't hold them. That's clipping, not a bug.&lt;/p&gt;

&lt;p&gt;sharp's switches also decide metadata. The default converts to sRGB and drops ICC and EXIF; &lt;code&gt;keepIccProfile()&lt;/code&gt; keeps P3, drops EXIF; &lt;code&gt;keepMetadata()&lt;/code&gt; keeps P3, EXIF and GPS; &lt;code&gt;withMetadata()&lt;/code&gt; converts to sRGB and embeds a 480-byte sRGB profile plus EXIF. Canvas exports in both engines lost EXIF and GPS.&lt;/p&gt;

&lt;h2&gt;
  
  
  The inspector may be reporting a default
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;sips --matchTo&lt;/code&gt; with the system sRGB profile produced correctly converted pixels, and &lt;code&gt;sips -g profile&lt;/code&gt; said &lt;code&gt;sRGB IEC61966-2.1&lt;/code&gt;. The file carried no ICC at all; sips had set ColorSpace to 1 in EXIF, and its query shows sRGB when nothing is embedded. sharp's default output reads the same way. Harmless for those two, since browsers treat untagged JPEG as sRGB. Pillow's untagged P3 file, the broken one, got the same &lt;code&gt;sRGB IEC61966-2.1&lt;/code&gt; line, so one answer covered a correct file and a wrong one. And &lt;code&gt;sips -d profile&lt;/code&gt;, which sounds like it strips the ICC, produced output byte-identical to the default, P3 profile still inside.&lt;/p&gt;

&lt;p&gt;Reading the bytes avoids both traps. This is a stripped-down sketch written for this post; the numbers above came from the full measurement run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;embeddedIcc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jpegPath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jpegPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iccSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mh"&gt;0xFF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;marker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;segLen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt16BE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;marker&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mh"&gt;0xDA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// start of scan&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;marker&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mh"&gt;0xE2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;latin1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ICC_PROFILE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;iccSize&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;segLen&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// length field, id string, chunk bytes&lt;/span&gt;
    &lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;segLen&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;iccSize&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;embeddedIcc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;536 means P3 was carried, 480 is sharp's sRGB tag, and 0 means the file can't tell you what its pixels are.&lt;/p&gt;

&lt;p&gt;For comparison I used ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; in a desktop browser on the same Mac, one HEIC at a time. Three conversions went through with no upload request, and the JPEGs kept the Display P3 profile, capture time and GPS, all 12 patches at 0.31 or below. How it decodes I can't say; that converter isn't a part I worked on. The sips default landed in the same place.&lt;/p&gt;

&lt;h2&gt;
  
  
  A real iPhone file would test the first check harder
&lt;/h2&gt;

&lt;p&gt;What's missing is an iPhone original. Those often describe color with nclx rather than an embedded profile, and HDR gain maps and 10-bit data make the first check harder in ways these sips-made files can't show. Until one goes through the same run, the byte check is the cheapest of the three to add to a pipeline.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Inside a pngquant PNG: 23 palette colors, 6 tRNS bytes, 7 alpha levels</title>
      <dc:creator>Lank_M</dc:creator>
      <pubDate>Fri, 06 Nov 2026 08:01:57 +0000</pubDate>
      <link>https://dev.to/pm_cheng_3f36acecfb9c59f5/inside-a-pngquant-png-23-palette-colors-6-trns-bytes-7-alpha-levels-feo</link>
      <guid>https://dev.to/pm_cheng_3f36acecfb9c59f5/inside-a-pngquant-png-23-palette-colors-6-trns-bytes-7-alpha-levels-feo</guid>
      <description>&lt;p&gt;A 512×512 transparent icon from Wikimedia Commons went from 23,375 bytes to 4,171 bytes through pngquant at &lt;code&gt;--quality=65-80&lt;/code&gt;. On 2026-11-05 I opened the output chunk by chunk to see where the bytes went. Almost nothing lives in the palette: PLTE holds 23 colors in 69 bytes, tRNS is 6 bytes, and the IDAT stream is 4,015 bytes. What I wanted to write down is why tRNS is 6 bytes and not 23, and why the file still carries 7 levels of transparency.&lt;/p&gt;

&lt;p&gt;The files are the 6 transparent PNGs (3 icons, 3 illustrations, all RGBA with real 0 to 255 alpha). They ran on an Apple M4 Mac with pngquant 3.0.3 (&lt;code&gt;--quality=65-80 --speed 4&lt;/code&gt;), oxipng 10.2.1 (&lt;code&gt;-o 4 --strip safe&lt;/code&gt;) and zopflipng with &lt;code&gt;--filters=01234mepb&lt;/code&gt;. For contrast I also opened iLoveIMG's output for two of the same transparent files. When I batch-compress images in the browser I use the image compressor in ImgIng &lt;a href="https://imging.ai/" rel="noopener noreferrer"&gt;https://imging.ai/&lt;/a&gt; ; that module isn't mine, and as far as I understand it belongs to the same reduce-colors-then-recompress family, but nothing below comes from it. Everything here is from one run, with no visual quality scoring.&lt;/p&gt;

&lt;h2&gt;
  
  
  Most of the saving happens before deflate sees the data
&lt;/h2&gt;

&lt;p&gt;The original icon is RGBA with 1,035 colors and 240 alpha values. Its IDAT inflates to 1,049,088 bytes, which is exactly 512 rows of one filter byte plus 512 × 4 bytes of pixels. After quantization the image is color type 3 with 8-bit indices, so the same arithmetic gives 512 × (1 + 512) = 262,656 bytes. Deflate now gets a quarter of the data, built from 23 symbols. Lossless tools working on the RGBA original can't touch that: oxipng got the icon to 15,363 bytes and zopflipng to 15,023. Over all 6 transparent files, pngquant alone saved 77.0%, and running oxipng on its output added only 2.1 percentage points.&lt;/p&gt;

&lt;p&gt;The palette itself is cheap. Even pngquant's default mode, which kept 256 colors for this icon, pays 768 bytes of PLTE and 62 bytes of tRNS. That file is 8,079 bytes with a 7,168-byte IDAT, so going from 23 to 256 colors grew the palette side by roughly 750 bytes and the index stream by over 3,000.&lt;/p&gt;

&lt;h2&gt;
  
  
  Opaque palette entries at the end cost nothing
&lt;/h2&gt;

&lt;p&gt;For palette images the PNG spec gives tRNS one alpha byte per palette entry and lets it be shorter than PLTE; entries past its end count as fully opaque. An encoder that sorts its non-opaque colors to the front can therefore drop every trailing 255. My small reader prints the PLTE entry count, the raw tRNS bytes and the distinct alpha values, counting the implied 255 when tRNS is shorter than the palette.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fname&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;got&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;rb&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;&amp;gt;I4s&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="n"&gt;got&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;trns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;got&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;PLTE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;got&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;tRNS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;levels&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;fname&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: PLTE &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; entries (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; B), tRNS &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; B&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;  tRNS = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;  tRNS[-4:] = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trns&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;  alpha levels = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;levels&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:]:&lt;/span&gt; &lt;span class="nf"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;icon-q.png: PLTE 23 entries (69 B), tRNS 6 B
  tRNS = [0, 211, 27, 110, 161, 64]
  alpha levels = 7
icon-qz.png: PLTE 23 entries (69 B), tRNS 7 B
  tRNS = [0, 27, 110, 161, 211, 255, 64]
  alpha levels = 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first file is the 65–80 output. Six tRNS bytes, values 0, 211, 27, 110, 161 and 64, and the other 17 colors fully opaque, which makes 7 levels. The second one is more interesting. It is the pngquant file after zopflipng, and now a 255 sits at slot six, in front of the 64. zopflipng reordered the palette, my guess is for the sake of the index stream though I didn't check, and once an opaque entry stopped being at the tail it had to write one more tRNS byte. The file still got smaller, 3,713 bytes against 4,171.&lt;/p&gt;

&lt;h2&gt;
  
  
  Seven alpha levels instead of 240
&lt;/h2&gt;

&lt;p&gt;The anti-aliased edge is coarsened rather than dropped. The Anime girl illustration kept 27 colors, 9 tRNS bytes and 10 alpha levels, down from 256 levels in the original. Across the six transparent files the palettes ran from 6 to 116 colors and the kept alpha from 7 to 58 levels. The apple cartoon is the top end, with 116 colors, 74 tRNS bytes and 58 levels. Each palette entry is a full RGBA tuple, so every alpha level spends a color slot and the quality range sets both budgets at once. Drop the range and you get pngquant's default, which on these transparent files came out 1.36 to 1.94 times the size of the 65–80 output. Whether 7 levels look acceptable on that icon's edge I can't say from bytes; I'd zoom it at 2× on dark and light backgrounds first, and haven't yet.&lt;/p&gt;

&lt;p&gt;iLoveIMG made a different set of choices. On the same icon it wrote 23 colors and 7 tRNS bytes in ascending order (0, 27, 64, 109, 156, 198, 231), which is 8 levels, in a 3,924-byte file, and oxipng then found 0 bytes to save. On the Anime girl it wrote 32 colors with a tRNS covering all 32 entries, whose last value is 74, so there was no opaque tail to drop, and 13 alpha levels across them. Its pixels differ from pngquant's; which quantizer it uses, I don't know. TinyPNG describes its own approach as reducing the number of colors; I had no TinyPNG file to open, so it isn't part of this teardown.&lt;/p&gt;

&lt;p&gt;So there are three numbers worth reading in a quantized PNG before its file size: how many entries PLTE has, how long tRNS is compared with it, and how many distinct alpha values remain. After that, run oxipng once. If it finds nothing, as with all three of iLoveIMG's PNG outputs I had (one of them a screenshot), the filter and deflate side has little left, and a smaller file would have to come from choices made at quantization.&lt;/p&gt;

&lt;p&gt;Sample images from Wikimedia Commons: Afro Mobile Icon by Ben plc (public domain), Anime girl by oksmith (CC0), Apple cartoon with transparent background by Toby8991 (CC0).&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
