PngScanlineBuffer { public static int sizeFrom(PngHeader header, int minBufferSize) { int bytesPerRow = header.bytesPerRow; int bytesFullBitmap = bytesPerRow * header.height; if (bytesFullBitmap < minBufferSize) { return bytesFullBitmap; } int numRows = Math.max(1, minBufferSize / bytesPerRow); return numRows * bytesPerRow; } PngScanlineBuffer(int size, int maxBytesPerLine, int filterUnit); byte[] getBytes(); int getReadPosition(); int getWritePosition(); boolean canRead(int minNumBytes); int availableRead(); int availableWrite(); void skip(int numBytes); void reset(); void shift(); int read(InputStream is); boolean decompress(InputStream inputStream, PngScanlineProcessor processor); void undoFilterScanline(int bytesPerLine); static PngScanlineBuffer from(PngHeader header); static PngScanlineBuffer from(PngHeader header, int minBufferSize); static int sizeFrom(PngHeader header, int minBufferSize);  }
@Test public void calculatesSize() { assertEquals(100, PngScanlineBuffer.sizeFrom(PngHeader.makeTruecolour(8, 4), ONE_K)); assertEquals(100, PngScanlineBuffer.sizeFrom(PngHeader.makeTruecolour(8, 4), TWO_K)); assertEquals(100, PngScanlineBuffer.sizeFrom(PngHeader.makeTruecolour(8, 4), FOUR_K)); assertEquals(601, PngScanlineBuffer.sizeFrom(PngHeader.makeTruecolour(200, 200), ONE_K)); assertEquals(3*601, PngScanlineBuffer.sizeFrom(PngHeader.makeTruecolour(200, 200), TWO_K)); assertEquals(6*601, PngScanlineBuffer.sizeFrom(PngHeader.makeTruecolour(200, 200), FOUR_K)); assertEquals(200*601, PngScanlineBuffer.sizeFrom(PngHeader.makeTruecolour(200, 200), FIVE_HUNDRED_K)); assertEquals(1*1601, PngScanlineBuffer.sizeFrom(PngHeader.makeTruecolourAlpha(400, 200), ONE_K)); assertEquals(1*1601, PngScanlineBuffer.sizeFrom(PngHeader.makeTruecolourAlpha(400, 200), TWO_K)); assertEquals(2*1601, PngScanlineBuffer.sizeFrom(PngHeader.makeTruecolourAlpha(400, 200), FOUR_K)); assertEquals(200*1601, PngScanlineBuffer.sizeFrom(PngHeader.makeTruecolourAlpha(400, 200), FIVE_HUNDRED_K)); }