Repository: mozilla/pdf.js
File: test/unit/image_utils_spec.js (JavaScript)

Code before:
      const dest = new Uint8ClampedArray(10 * 4);
      convertBlackAndWhiteToRGBA({ src, dest, width: 10, height: 1 });

      const dest32 = new Uint32Array(dest.buffer);
      expect(dest32[0]).toEqual(WHITE);
      expect(dest32[1]).toEqual(WHITE);
      expect(dest32[2]).toEqual(WHITE);
      expect(dest32[3]).toEqual(WHITE);
      expect(dest32[4]).toEqual(BLACK);
      expect(dest32[5]).toEqual(BLACK);
      expect(dest32[6]).toEqual(BLACK);
      expect(dest32[7]).toEqual(BLACK);
      // Missing second byte → treated as 0xff, so bits 7,6 → W,W
      expect(dest32[8]).toEqual(WHITE);
      expect(dest32[9]).toEqual(WHITE);
    });
  });

  describe("grayToRGBA", function () {
    it("converts black (0), mid-gray (128), and white (255)", function () {
      const src = new Uint8Array([0, 128, 255]);
      const dest = new Uint32Array(3);
      grayToRGBA(src, dest);

      if (isLE) {
        expect(dest[0]).toEqual(0xff000000); // black + full alpha
        expect(dest[1]).toEqual(0xff808080); // 128 * 0x10101 | 0xff000000
        expect(dest[2]).toEqual(0xffffffff); // white + full alpha
      } else {
        expect(dest[0]).toEqual(0x000000ff);
        expect(dest[1]).toEqual(0x808080ff);
        expect(dest[2]).toEqual(0xffffffff);
      }
    });

    it("handles an empty input array", function () {
      grayToRGBA(new Uint8Array(0), new Uint32Array(0));
      // No crash, nothing to check beyond reaching here.
    });

    it("alpha channel is always 0xff for every pixel", function () {
      const N = 256;
      const src = new Uint8Array(N);
      const dest = new Uint32Array(N);
      for (let i = 0; i < N; i++) {
        src[i] = i;
      }
      grayToRGBA(src, dest);

      // Extract the alpha byte: high byte in LE, low byte in BE.
      const alphaShift = isLE ? 24 : 0;

Review comment (refactor):
We can use the `BLACK` constant here and below, and similar below also for `WHITE`. `BLACK` is already changed depending on the endianness, and if we use a ternary for the middle color we should be able to remove the `if` statement entirely and simplify this block to:

```
expect(dest[0]).toEqual(BLACK);
expect(dest[1]).toEqual(isLE ? 0xff808080 : 0x808080ff);
expect(dest[2]).toEqual(WHITE);
```

This has the added benefit of removing some "magic numbers" (less of an issue here because you added helpful comments) in favor of meaningful constants.

Code after:
      const src = new Uint8Array([0b11110000]);
      const dest = new Uint8ClampedArray(10 * 4);
      convertBlackAndWhiteToRGBA({ src, dest, width: 10, height: 1 });

      const dest32 = new Uint32Array(dest.buffer);
      expect(dest32[0]).toEqual(WHITE);
      expect(dest32[1]).toEqual(WHITE);
      expect(dest32[2]).toEqual(WHITE);
      expect(dest32[3]).toEqual(WHITE);
      expect(dest32[4]).toEqual(BLACK);
      expect(dest32[5]).toEqual(BLACK);
      expect(dest32[6]).toEqual(BLACK);
      expect(dest32[7]).toEqual(BLACK);
      // Missing second byte → treated as 0xff, so bits 7,6 → W,W
      expect(dest32[8]).toEqual(WHITE);
      expect(dest32[9]).toEqual(WHITE);
    });
  });

  describe("grayToRGBA", function () {
    it("converts black (0), mid-gray (128), and white (255)", function () {
      const src = new Uint8Array([0, 128, 255]);
      const dest = new Uint32Array(3);
      grayToRGBA(src, dest);

      expect(dest[0]).toEqual(BLACK);
      expect(dest[1]).toEqual(isLE ? 0xff808080 : 0x808080ff);
      expect(dest[2]).toEqual(WHITE);
    });

    it("handles an empty input array", function () {
      grayToRGBA(new Uint8Array(0), new Uint32Array(0));
      // No crash, nothing to check beyond reaching here.
    });

    it("alpha channel is always 0xff for every pixel", function () {
      const N = 256;
      const src = new Uint8Array(N);
      const dest = new Uint32Array(N);
      for (let i = 0; i < N; i++) {
        src[i] = i;
      }
      grayToRGBA(src, dest);

      // Extract the alpha byte: high byte in LE, low byte in BE.
      const alphaShift = isLE ? 24 : 0;
      for (let i = 0; i < N; i++) {
        expect((dest[i] >>> alphaShift) & 0xff)
          .withContext(`alpha for value ${i}`)
          .toEqual(0xff);
      }