Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

4.2 KB
Raw Blame
package opensuite.dashboard;

import java.awt.Dimension;
import java.awt.image.BufferedImage;

/**
 * Sizing and shrinking the picture of a page for the Live crawl view. The
 * browser sends a picture with more dots than the tab has room for; shrinking
 * it by averaging the dots under each one shown keeps small text clean, where
 * the quick ways of shrinking leave it soft or broken.
 */
final class PageImage {

    private PageImage() {}

    /** A page is shown up to this much larger than it is, to fill the tab; more would look blown up. */
    static final double MAX_ENLARGE = 1.25;

    /**
     * The size to show a page at, in screen dots. It fills the width there is,
     * since a page reads best the full width of the view, and keeps its
     * shape, so a tall one runs on below and is scrolled to. It is never much
     * larger than the page really is ({@code cssWidth} points across, on a
     * screen of {@code uiScale} dots to the point), and never has more dots
     * than the picture does.
     */
    static Dimension fitSize(int imgW, int imgH, int cssWidth, int roomW, double uiScale) {
        if (imgW <= 0 || imgH <= 0 || roomW <= 0) return new Dimension(0, 0);
        double natural = Math.max(1, cssWidth > 0 ? cssWidth : imgW) * Math.max(1.0, uiScale);
        double most = cssWidth > 0 ? Math.min(imgW, natural * MAX_ENLARGE) : Math.min(imgW, natural);
        int w = (int) Math.max(1, Math.min(roomW, Math.round(most)));
        return new Dimension(w, Math.max(1, (int) Math.round((double) w * imgH / imgW)));
    }

    @SuppressWarnings("unchecked")
    private static final java.lang.ref.SoftReference<int[]>[] KEPT = new java.lang.ref.SoftReference[2];

    private static int[] room(int which, int size) {
        int[] a = KEPT[which] == null ? null : KEPT[which].get();
        if (a == null || a.length < size) { a = new int[size]; KEPT[which] = new java.lang.ref.SoftReference<>(a); }
        return a;
    }

    /** The picture at this size. Shrinking averages every dot that falls under each new one. */
    static synchronized BufferedImage scaled(BufferedImage src, int w, int h) {
        int sw = src.getWidth(), sh = src.getHeight();
        if (w == sw && h == sh) return src;
        if (w > sw || h > sh) {
            BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            java.awt.Graphics2D g = out.createGraphics();
            g.setRenderingHint(java.awt.RenderingHints.KEY_INTERPOLATION, java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g.drawImage(src, 0, 0, w, h, null);
            g.dispose();
            return out;
        }
        // The two large work areas are kept between pictures while memory allows: a new
        // picture comes more than once a second, and making them afresh each time is waste.
        int[] in = src.getRGB(0, 0, sw, sh, room(0, sw * sh), 0, sw);
        // Across first, then down: each pass averages a run of dots into one.
        int[] mid = room(1, w * sh);
        int[] x0 = new int[w + 1];
        for (int x = 0; x <= w; x++) x0[x] = (int) ((long) x * sw / w);
        for (int y = 0; y < sh; y++) {
            int row = y * sw, to = y * w;
            for (int x = 0; x < w; x++) {
                int a = x0[x], b = Math.max(a + 1, x0[x + 1]);
                int r = 0, g = 0, bl = 0;
                for (int i = a; i < b; i++) { int p = in[row + i]; r += (p >> 16) & 255; g += (p >> 8) & 255; bl += p & 255; }
                int n = b - a;
                mid[to + x] = ((r / n) << 16) | ((g / n) << 8) | (bl / n);
            }
        }
        int[] out = new int[w * h];
        int[] y0 = new int[h + 1];
        for (int y = 0; y <= h; y++) y0[y] = (int) ((long) y * sh / h);
        for (int y = 0; y < h; y++) {
            int a = y0[y], b = Math.max(a + 1, y0[y + 1]), n = b - a;
            for (int x = 0; x < w; x++) {
                int r = 0, g = 0, bl = 0;
                for (int i = a; i < b; i++) { int p = mid[i * w + x]; r += (p >> 16) & 255; g += (p >> 8) & 255; bl += p & 255; }
                out[y * w + x] = ((r / n) << 16) | ((g / n) << 8) | (bl / n);
            }
        }
        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        img.setRGB(0, 0, w, h, out, 0, w);
        return img;
    }
}