Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 17, 2026

Runtime decoders (ImageDecoder/OffscreenCanvas) were corrupting pixel data when creating views from Uint8ClampedArray buffers with non-zero offsets.

Problem

Decoding via runtime APIs produced scrambled images on re-encode:

const bytes = await Deno.readFile("image.jpg");
const img = await Image.decode(bytes, "jpeg");
const reencoded = await img.encode("jpeg");
// Result: visibly corrupted pixel data

The decoders assumed imageData.data started at buffer offset 0:

// BROKEN: ignores byteOffset
return new Uint8Array(imageData.data.buffer);

Changes

  • JPEG, WebP, GIF, TIFF, HEIC, AVIF decoders: Changed buffer access to properly copy pixel data regardless of offset

    // FIXED: respects byteOffset and byteLength
    return new Uint8Array(imageData.data);
  • Tests: Added roundtrip validation for lossless (PNG) and lossy (JPEG) formats with pixel-level verification

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • github-production-user-asset-6210df.s3.amazonaws.com
    • Triggering command: /usr/bin/curl curl -L -o A.jpg REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Instances of pixel-offset corruption after decoding/re-encoding</issue_title>
<issue_description>Hello! Thank you for this very useful tool.

I've discovered a couple of instances of image distortion when simply decoding and then re-encoding in the same format. I've only tested in Deno (v2.6.4).

To reproduce, simply drop the attached files into a directory and then run the following script with all permissions:

import { Image } from "@cross/image";
import * as path from "@std/path";

const aBytes = await Deno.readFile(path.join(Deno.cwd(), "A.jpg"));
const aImg = await Image.decode(aBytes, "jpeg");
const aImgPrime = await aImg.encode("jpeg");
await Deno.writeFile(path.join(Deno.cwd(), "aPrime.jpg"), aImgPrime);

const bBytes = await Deno.readFile(path.join(Deno.cwd(), "B.png"));
const bImg = await Image.decode(bBytes, "png");
const bImgPrime = await bImg.encode("png");
await Deno.writeFile(path.join(Deno.cwd(), "bPrime.png"), bImgPrime);
Image

Image

Really hoping you can help ...</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix instances of pixel-offset corruption after decoding and re-encoding Fix buffer offset corruption in runtime image decoders Jan 17, 2026
Copilot AI requested a review from Hexagon January 17, 2026 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Instances of pixel-offset corruption after decoding/re-encoding

2 participants