srecord noeq PosterizeBWImage(int brightnessLevels, BWImage img) is PixelPreservingImageOpResult { BWImage result; run { if (brightnessLevels >= 256 || img == null) ret with result = img; byte[] pixels = img.getBytes(); int w = img.getWidth(), h = img.getHeight(), n = pixels.length; byte[] pixels2 = new[n]; double factor1 = doubleRatio(brightnessLevels, 256); double factor2 = doubleRatio(255, brightnessLevels-1); for i to n: pixels2[i] = (byte) iround(ifloor(ubyteToInt(pixels[i])*factor1)*factor2); result = new BWImage(w, h, pixels2); } public BWImage pixelPreservingSrcImage() { ret img; } }