@Test
    void testExtraSamples() throws Exception {

        final int bytesPerSample = 4;
        final int width = 10;
        final int height = 10;
        final int nBytesPerStrip = bytesPerSample * height * width;
        final ByteOrder byteOrder = ByteOrder.nativeOrder();

        final int[] samples = new int[width * height];
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                final int index = i * width + j;
                samples[index] = j > i ? 0xffff0000 : 0x88ff0000;
            }
        }

        for (int iExtra = 0; iExtra < 3; iExtra++) {
            final TiffOutputSet outputSet = new TiffOutputSet(byteOrder);
            final TiffOutputDirectory outDir = outputSet.addRootDirectory();
            outDir.add(TiffTagConstants.TIFF_TAG_IMAGE_WIDTH, width);
            outDir.add(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH, height);
            outDir.add(TiffTagConstants.TIFF_TAG_SAMPLES_PER_PIXEL, (short) 4);
            outDir.add(TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE, new short[] { 8, 8, 8, 8 });
            outDir.add(TiffTagConstants.TIFF_TAG_PHOTOMETRIC_INTERPRETATION, (short) TiffTagConstants.PHOTOMETRIC_INTERPRETATION_VALUE_RGB);
            outDir.add(TiffTagConstants.TIFF_TAG_COMPRESSION, (short) TiffTagConstants.COMPRESSION_VALUE_UNCOMPRESSED);
            outDir.add(TiffTagConstants.TIFF_TAG_PLANAR_CONFIGURATION, (short) TiffTagConstants.PLANAR_CONFIGURATION_VALUE_CHUNKY);
            outDir.add(TiffTagConstants.TIFF_TAG_ROWS_PER_STRIP, height);
            outDir.add(TiffTagConstants.TIFF_TAG_STRIP_BYTE_COUNTS, nBytesPerStrip);

            outDir.add(TiffTagConstants.TIFF_TAG_EXTRA_SAMPLES, (short) iExtra);

            final byte[] b = new byte[nBytesPerStrip];
            int k = 0;
            for (final int sample : samples) {
                b[k++] = (byte) (sample >> 16 & 0xff); // R
                b[k++] = (byte) (sample >> 8 & 0xff); // G
                b[k++] = (byte) (sample & 0xff); // B
                b[k++] = (byte) (sample >> 24 & 0xff); // A
            }

            final AbstractTiffElement.DataElement[] imageData = new AbstractTiffElement.DataElement[1];
            imageData[0] = new AbstractTiffImageData.Data(0, b.length, b);

            final AbstractTiffImageData abstractTiffImageData = new AbstractTiffImageData.Strips(imageData, height);

            outDir.setTiffImageData(abstractTiffImageData);

            final File outputFile = new File(tempDir.toFile(), "TestExtraSamples" + iExtra + ".tiff");
            try (FileOutputStream fos = new FileOutputStream(outputFile);
                    BufferedOutputStream bos = new BufferedOutputStream(fos)) {
                final TiffImageWriterLossy writer = new TiffImageWriterLossy(byteOrder);
                writer.write(bos, outputSet);
                bos.flush();
            }

            final BufferedImage result = Imaging.getBufferedImage(outputFile);
            final int[] argb = new int[samples.length];
            result.getRGB(0, 0, width, height, argb, 0, width);
            final int index = 3 * width + 1;
            int iSample = samples[index];
            final int iArgb = argb[index];
            if (iExtra == 0) {
                // when extra samples is zero, the alpha channel is ignored.
                // We expect ARGB to start with 0xff. So we OR in 0xff for
                // the alpha value of the sample
                iSample |= 0xff000000;
            } else if (iExtra == 1) {
                // The pre-multiply alpha case
                iSample = 0x89de0000;
            }
            final String p = String.format("%08x", iSample);
            final String q = String.format("%08x", iArgb);
            assertEquals(p, q, "Failure on ExtraSamples=" + iExtra);
        }
    }
Checks to see if a pixel component (A, R, G, or B) for two specified values are within a specified tolerance.

@param a          the first value
@param b          the second value
@param iShift     a multiple of 8 telling how far to shift values to extract components (24, 16, 8, or zero for ARGB)
@param iTolerance a small positive integer
@return true if the components of the values match
src/test/java/org/apache/commons/imaging/formats/tiff/TiffAlphaRoundTripTest.java