// TODO: different sorting methods, e.g. "widest SSIs" sclass G22DataWrangler > Stages2 is IHasChangeListeners { replace Stage with Runnable. event change; settable BufferedImage inputImage; // try to re-use anything that is identical settable G22DataWrangler stealingFrom; settable bool withDiagonals = true; settableWithVar int blur = 1; // in pixels // kilobytes per compressed image (pessimistic estimate // counting 2 bytes for each int) settable TargetAndActual kilobytes = new(250.0); settable new TargetAndActual coveredPixelsPercentage; settable new TargetAndActual detailLevel; settable bool vectorize = true; settable bool allowPartialSSIs = true; settable SortMode sortMode = SortMode.compressibility; enum SortMode { compressibility, pixels } settable IPosterizer posterizer = new SinglePixelPosterizer(4); BufferedImage blurredImage; int maxLines, maxInts; L currentSSIs; // ssis at current stage L initialSSIs; AbstractSSIList sortedSSIs, cutSSIs, vectorizedSSIs, cutVectorizedSSIs; CutListToBudget cutter; // We store the posterized image as Hi15 Hi15Image posterizedImage; // Region maker + regions FastRegions_Hi15Image regionMaker; L> regions; // Constructors *() {} *(BufferedImage *inputImage) {} // Methods selfType kb(TargetAndActual kb) { ret kilobytes(kb); } TargetAndActual kb() { ret kilobytes; } WidthAndHeight resolution() { ret imageSize(inputImage); } double detailDivisor() { ret areaRoot(inputImage); } selfType colorsPerChannel(int perChannel) { ret posterizer(new SinglePixelPosterizer(perChannel)); } int colorsPerChannel() { ret posterizer/SinglePixelPosterizer.brightnessLevels; } int colors() { ret cubed(colorsPerChannel()); } transient simplyCached FieldVar varColorsPerChannel() { ret new FieldVar(this, "colorsPerChannel", l0 colorsPerChannel, l1 colorsPerChannel); } transient simplyCached FieldVar varColors() { ret new FieldVar(this, "colors", l0 colors, l1 colors); } // choose number of colors for posterized image selfType colors(int colors) { int perChannel = iceil(cbrt(colors)); ret colorsPerChannel(perChannel); } stage "Stop Stealing" { if (stealingFrom != null && stealingFrom.inputImage != inputImage) stealingFrom = null; } stage "Blur" { if (stealingFrom != null) if (stealingFrom.blur == blur) ret with blurredImage = stealingFrom.blurredImage; else stealingFrom = null; blurredImage = blurBufferedImage(blur, inputImage); } stage "Posterize" { if (stealingFrom != null) if (eq(stealingFrom.posterizer, posterizer)) ret with posterizedImage = stealingFrom.posterizedImage; else stealingFrom = null; posterizedImage = posterizeBufferedImageToHi15(blurredImage, posterizer); } stage regionsStage "Regions" { if (stealingFrom != null) if (stealingFrom.withDiagonals == withDiagonals) { regionMaker = stealingFrom.regionMaker; regions = stealingFrom.regions; ret; } else stealingFrom = null; regionMaker = new FastRegions_Hi15Image(posterizedImage); regionMaker.withDiagonals(withDiagonals); regions = regionMaker!; } stage "Sort regions" { regions = biggestRegionsFirst(regions); } stage "SSIs" { initialSSIs = new L; for (region : regions) initialSSIs.addAll(new G22_RegionToSSIs_v2(region).withDiagonals (withDiagonals)!); currentSSIs = initialSSIs; } int initialSSILines() { ret totalSSILines(initialSSIs); } stage "Vector-Optimize" { currentSSIs = vectorizedSSIs = vectorize ? new VectorOptimizedSSIList(currentSSIs) : new GeneralSSIList(currentSSIs); } stage "Sort SSIs" { if (sortMode == SortMode.compressibility) sortedSSIs = new GeneralSSIList(sortedDesc(currentSSIs, (a, b) -> { int x = cmp(a.compressibility(), b.compressibility()); if (x != 0) ret x; ret cmp(a.numberOfPixels(), b.numberOfPixels()); })); else if (sortMode == SortMode.pixels) sortedSSIs = new GeneralSSIList(biggestSSIsFirst(currentSSIs)); else fail("Unknown sort mode"); currentSSIs = sortedSSIs; } stage "Cut SSI List by detail level" { maxLines = !detailLevel.hasTarget() ? Int.MAX_VALUE : iround(detailDivisor()*detailLevel.target()); currentSSIs = cutSSIs = new GeneralSSIList(takeFirstNSSILines(maxLines, currentSSIs)); detailLevel.set(l(cutSSIs)/detailDivisor()); } stage "Cut Vector-Optimized SSIs by file size" { maxInts = !kilobytes.hasTarget() ? Int.MAX_VALUE : iround(kilobytes.target()*512); // assuming 16 bit ints cutter = new CutListToBudget(ssi -> (double) ssi.sizeInInts(), maxInts, (L) currentSSIs); if (allowPartialSSIs) cutter.allowPartial((ssi, budget) -> ssi.reduceToInts(iround(budget))); currentSSIs = cutVectorizedSSIs = new GeneralSSIList(cutter!); kilobytes.set(totalSizeInInts(cutVectorizedSSIs)/512.0); } L> regions() { stepUntilStage(regionsStage); ret regions; } L regionsAsIBinaryImages() { ret map regionToIBinaryImage(regions()); } BlurAndPosterizeSettings bnpSettings() { ret new BlurAndPosterizeSettings() .blur(blur) .colors(colors()); } void importSettings(BlurAndPosterizeSettings bnp) { blur(bnp.blur); colors(bnp.colors); } FastRegions_Hi15Image regionMaker() { stepUntilStage(regionsStage); ret regionMaker; } }