mozilla/pdf.js · error · JpegError

Unsupported color mode

Error message

Unsupported color mode

What it means

Thrown by getData() if the parsed JPEG has more than 4 color components. The decoder supports grayscale (1), RGB/YCbCr (3), and CMYK/YCCK (4) only; 5+ components (e.g. hexachrome, multi-spectral) have no defined color-conversion path.

Source

Thrown at src/core/jpg.js:1393

    if (ColorSpaceUtils.cmyk instanceof DeviceCmykCS) {
      // The alpha-component isn't updated by `DeviceCmykCS`, doing it manually.
      for (let i = 3, ii = data.length; i < ii; i += 4) {
        data[i] = 255;
      }
    }
    return data;
  }

  getData({
    width,
    height,
    forceRGBA = false,
    forceRGB = false,
    isSourcePDF = typeof PDFJSDev === "undefined" ||
      !PDFJSDev.test("IMAGE_DECODERS"),
  }) {
    if (this.numComponents > 4) {
      throw new JpegError("Unsupported color mode");
    }
    // Type of data: Uint8ClampedArray(width * height * numComponents)
    const data = this.#getLinearizedBlockData(width, height, isSourcePDF);

    if (this.numComponents === 1 && (forceRGBA || forceRGB)) {
      const len = data.length * (forceRGBA ? 4 : 3);
      const rgbaData = new Uint8ClampedArray(len);
      let offset = 0;
      if (forceRGBA) {
        grayToRGBA(data, new Uint32Array(rgbaData.buffer));
      } else {
        for (const grayColor of data) {
          rgbaData[offset++] = grayColor;
          rgbaData[offset++] = grayColor;
          rgbaData[offset++] = grayColor;
        }
      }
      return rgbaData;

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Check jpegImg.numComponents before calling getData and reject/handle 5+ explicitly.
  2. Re-encode the image to a standard 1/3/4-component color space before embedding.
  3. If you must decode it, use a specialized multi-channel decoder outside PDF.js.

Example fix

// before
const pixels = jpegImg.getData({ width, height, forceRGBA: true });

// after
if (jpegImg.numComponents > 4) {
  throw new Error(`Unsupported: ${jpegImg.numComponents} color components`);
}
const pixels = jpegImg.getData({ width, height, forceRGBA: true });
Defensive patterns

Strategy: validation

Validate before calling

if (jpegImg.numComponents > 4) {
  throw new Error('JPEG has ' + jpegImg.numComponents + ' components; only 1, 3, or 4 are supported');
}
// only then:
const pixels = jpegImg.getData({ width, height });

Prevention

When it happens

Trigger: Calling jpegImg.getData({...}) when jpegImg.numComponents > 4. The numComponents value comes from the SOF's componentsCount during parse().

Common situations: An unusual JPEG with extra ink channels (hexachrome/Hi-Fi color), or an image with an alpha-like 5th component. Very rare in PDF workflows; usually from specialized print/spectral imaging.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/e819a91b33ecba6e. Report an issue: GitHub.