1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.layoutlib.bridge.intensive;
18
19import android.annotation.NonNull;
20
21import java.awt.AlphaComposite;
22import java.awt.Color;
23import java.awt.Graphics;
24import java.awt.Graphics2D;
25import java.awt.image.BufferedImage;
26import java.io.File;
27import java.io.IOException;
28import java.io.InputStream;
29
30import javax.imageio.ImageIO;
31
32import static java.awt.RenderingHints.*;
33import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
34import static java.io.File.separatorChar;
35import static org.junit.Assert.assertEquals;
36import static org.junit.Assert.assertTrue;
37import static org.junit.Assert.fail;
38
39
40// Adapted by taking the relevant pieces of code from the following classes:
41//
42// com.android.tools.idea.rendering.ImageUtils,
43// com.android.tools.idea.tests.gui.framework.fixture.layout.ImageFixture and
44// com.android.tools.idea.rendering.RenderTestBase
45/**
46 * Utilities related to image processing.
47 */
48public class ImageUtils {
49    /**
50     * Normally, this test will fail when there is a missing thumbnail. However, when
51     * you create creating a new test, it's useful to be able to turn this off such that
52     * you can generate all the missing thumbnails in one go, rather than having to run
53     * the test repeatedly to get to each new render assertion generating its thumbnail.
54     */
55    private static final boolean FAIL_ON_MISSING_THUMBNAIL = true;
56
57    private static final int THUMBNAIL_SIZE = 250;
58
59    private static final double MAX_PERCENT_DIFFERENCE = 0.3;
60
61    public static void requireSimilar(@NonNull String relativePath, @NonNull BufferedImage image)
62            throws IOException {
63        int maxDimension = Math.max(image.getWidth(), image.getHeight());
64        double scale = THUMBNAIL_SIZE / (double)maxDimension;
65        BufferedImage thumbnail = scale(image, scale, scale);
66
67        InputStream is = ImageUtils.class.getResourceAsStream(relativePath);
68        if (is == null) {
69            String message = "Unable to load golden thumbnail: " + relativePath + "\n";
70            message = saveImageAndAppendMessage(thumbnail, message, relativePath);
71            if (FAIL_ON_MISSING_THUMBNAIL) {
72                fail(message);
73            } else {
74                System.out.println(message);
75            }
76        }
77        else {
78            try {
79                BufferedImage goldenImage = ImageIO.read(is);
80                assertImageSimilar(relativePath, goldenImage, thumbnail, MAX_PERCENT_DIFFERENCE);
81            } finally {
82                is.close();
83            }
84        }
85    }
86
87    public static void assertImageSimilar(String relativePath, BufferedImage goldenImage,
88            BufferedImage image, double maxPercentDifferent) throws IOException {
89        assertEquals("Only TYPE_INT_ARGB image types are supported",  TYPE_INT_ARGB, image.getType());
90
91        if (goldenImage.getType() != TYPE_INT_ARGB) {
92            BufferedImage temp = new BufferedImage(goldenImage.getWidth(), goldenImage.getHeight(),
93                    TYPE_INT_ARGB);
94            temp.getGraphics().drawImage(goldenImage, 0, 0, null);
95            goldenImage = temp;
96        }
97        assertEquals(TYPE_INT_ARGB, goldenImage.getType());
98
99        int imageWidth = Math.min(goldenImage.getWidth(), image.getWidth());
100        int imageHeight = Math.min(goldenImage.getHeight(), image.getHeight());
101
102        // Blur the images to account for the scenarios where there are pixel
103        // differences
104        // in where a sharp edge occurs
105        // goldenImage = blur(goldenImage, 6);
106        // image = blur(image, 6);
107
108        int width = 3 * imageWidth;
109        @SuppressWarnings("UnnecessaryLocalVariable")
110        int height = imageHeight; // makes code more readable
111        BufferedImage deltaImage = new BufferedImage(width, height, TYPE_INT_ARGB);
112        Graphics g = deltaImage.getGraphics();
113
114        // Compute delta map
115        long delta = 0;
116        for (int y = 0; y < imageHeight; y++) {
117            for (int x = 0; x < imageWidth; x++) {
118                int goldenRgb = goldenImage.getRGB(x, y);
119                int rgb = image.getRGB(x, y);
120                if (goldenRgb == rgb) {
121                    deltaImage.setRGB(imageWidth + x, y, 0x00808080);
122                    continue;
123                }
124
125                // If the pixels have no opacity, don't delta colors at all
126                if (((goldenRgb & 0xFF000000) == 0) && (rgb & 0xFF000000) == 0) {
127                    deltaImage.setRGB(imageWidth + x, y, 0x00808080);
128                    continue;
129                }
130
131                int deltaR = ((rgb & 0xFF0000) >>> 16) - ((goldenRgb & 0xFF0000) >>> 16);
132                int newR = 128 + deltaR & 0xFF;
133                int deltaG = ((rgb & 0x00FF00) >>> 8) - ((goldenRgb & 0x00FF00) >>> 8);
134                int newG = 128 + deltaG & 0xFF;
135                int deltaB = (rgb & 0x0000FF) - (goldenRgb & 0x0000FF);
136                int newB = 128 + deltaB & 0xFF;
137
138                int avgAlpha = ((((goldenRgb & 0xFF000000) >>> 24)
139                        + ((rgb & 0xFF000000) >>> 24)) / 2) << 24;
140
141                int newRGB = avgAlpha | newR << 16 | newG << 8 | newB;
142                deltaImage.setRGB(imageWidth + x, y, newRGB);
143
144                delta += Math.abs(deltaR);
145                delta += Math.abs(deltaG);
146                delta += Math.abs(deltaB);
147            }
148        }
149
150        // 3 different colors, 256 color levels
151        long total = imageHeight * imageWidth * 3L * 256L;
152        float percentDifference = (float) (delta * 100 / (double) total);
153
154        String error = null;
155        String imageName = getName(relativePath);
156        if (percentDifference > maxPercentDifferent) {
157            error = String.format("Images differ (by %.1f%%)", percentDifference);
158        } else if (Math.abs(goldenImage.getWidth() - image.getWidth()) >= 2) {
159            error = "Widths differ too much for " + imageName + ": " +
160                    goldenImage.getWidth() + "x" + goldenImage.getHeight() +
161                    "vs" + image.getWidth() + "x" + image.getHeight();
162        } else if (Math.abs(goldenImage.getHeight() - image.getHeight()) >= 2) {
163            error = "Heights differ too much for " + imageName + ": " +
164                    goldenImage.getWidth() + "x" + goldenImage.getHeight() +
165                    "vs" + image.getWidth() + "x" + image.getHeight();
166        }
167
168        assertEquals(TYPE_INT_ARGB, image.getType());
169        if (error != null) {
170            // Expected on the left
171            // Golden on the right
172            g.drawImage(goldenImage, 0, 0, null);
173            g.drawImage(image, 2 * imageWidth, 0, null);
174
175            // Labels
176            if (imageWidth > 80) {
177                g.setColor(Color.RED);
178                g.drawString("Expected", 10, 20);
179                g.drawString("Actual", 2 * imageWidth + 10, 20);
180            }
181
182            File output = new File(getTempDir(), "delta-" + imageName);
183            if (output.exists()) {
184                boolean deleted = output.delete();
185                assertTrue(deleted);
186            }
187            ImageIO.write(deltaImage, "PNG", output);
188            error += " - see details in " + output.getPath() + "\n";
189            error = saveImageAndAppendMessage(image, error, relativePath);
190            System.out.println(error);
191            fail(error);
192        }
193
194        g.dispose();
195    }
196
197    /**
198     * Resize the given image
199     *
200     * @param source the image to be scaled
201     * @param xScale x scale
202     * @param yScale y scale
203     * @return the scaled image
204     */
205    @NonNull
206    public static BufferedImage scale(@NonNull BufferedImage source, double xScale, double yScale) {
207
208        int sourceWidth = source.getWidth();
209        int sourceHeight = source.getHeight();
210        int destWidth = Math.max(1, (int) (xScale * sourceWidth));
211        int destHeight = Math.max(1, (int) (yScale * sourceHeight));
212        int imageType = source.getType();
213        if (imageType == BufferedImage.TYPE_CUSTOM) {
214            imageType = BufferedImage.TYPE_INT_ARGB;
215        }
216        if (xScale > 0.5 && yScale > 0.5) {
217            BufferedImage scaled =
218                    new BufferedImage(destWidth, destHeight, imageType);
219            Graphics2D g2 = scaled.createGraphics();
220            g2.setComposite(AlphaComposite.Src);
221            g2.setColor(new Color(0, true));
222            g2.fillRect(0, 0, destWidth, destHeight);
223            if (xScale == 1 && yScale == 1) {
224                g2.drawImage(source, 0, 0, null);
225            } else {
226                setRenderingHints(g2);
227                g2.drawImage(source, 0, 0, destWidth, destHeight, 0, 0, sourceWidth, sourceHeight,
228                        null);
229            }
230            g2.dispose();
231            return scaled;
232        } else {
233            // When creating a thumbnail, using the above code doesn't work very well;
234            // you get some visible artifacts, especially for text. Instead use the
235            // technique of repeatedly scaling the image into half; this will cause
236            // proper averaging of neighboring pixels, and will typically (for the kinds
237            // of screen sizes used by this utility method in the layout editor) take
238            // about 3-4 iterations to get the result since we are logarithmically reducing
239            // the size. Besides, each successive pass in operating on much fewer pixels
240            // (a reduction of 4 in each pass).
241            //
242            // However, we may not be resizing to a size that can be reached exactly by
243            // successively diving in half. Therefore, once we're within a factor of 2 of
244            // the final size, we can do a resize to the exact target size.
245            // However, we can get even better results if we perform this final resize
246            // up front. Let's say we're going from width 1000 to a destination width of 85.
247            // The first approach would cause a resize from 1000 to 500 to 250 to 125, and
248            // then a resize from 125 to 85. That last resize can distort/blur a lot.
249            // Instead, we can start with the destination width, 85, and double it
250            // successfully until we're close to the initial size: 85, then 170,
251            // then 340, and finally 680. (The next one, 1360, is larger than 1000).
252            // So, now we *start* the thumbnail operation by resizing from width 1000 to
253            // width 680, which will preserve a lot of visual details such as text.
254            // Then we can successively resize the image in half, 680 to 340 to 170 to 85.
255            // We end up with the expected final size, but we've been doing an exact
256            // divide-in-half resizing operation at the end so there is less distortion.
257
258            int iterations = 0; // Number of halving operations to perform after the initial resize
259            int nearestWidth = destWidth; // Width closest to source width that = 2^x, x is integer
260            int nearestHeight = destHeight;
261            while (nearestWidth < sourceWidth / 2) {
262                nearestWidth *= 2;
263                nearestHeight *= 2;
264                iterations++;
265            }
266
267            BufferedImage scaled = new BufferedImage(nearestWidth, nearestHeight, imageType);
268
269            Graphics2D g2 = scaled.createGraphics();
270            setRenderingHints(g2);
271            g2.drawImage(source, 0, 0, nearestWidth, nearestHeight, 0, 0, sourceWidth, sourceHeight,
272                    null);
273            g2.dispose();
274
275            sourceWidth = nearestWidth;
276            sourceHeight = nearestHeight;
277            source = scaled;
278
279            for (int iteration = iterations - 1; iteration >= 0; iteration--) {
280                int halfWidth = sourceWidth / 2;
281                int halfHeight = sourceHeight / 2;
282                scaled = new BufferedImage(halfWidth, halfHeight, imageType);
283                g2 = scaled.createGraphics();
284                setRenderingHints(g2);
285                g2.drawImage(source, 0, 0, halfWidth, halfHeight, 0, 0, sourceWidth, sourceHeight,
286                        null);
287                g2.dispose();
288
289                sourceWidth = halfWidth;
290                sourceHeight = halfHeight;
291                source = scaled;
292                iterations--;
293            }
294            return scaled;
295        }
296    }
297
298    private static void setRenderingHints(@NonNull Graphics2D g2) {
299        g2.setRenderingHint(KEY_INTERPOLATION,VALUE_INTERPOLATION_BILINEAR);
300        g2.setRenderingHint(KEY_RENDERING, VALUE_RENDER_QUALITY);
301        g2.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
302    }
303
304    /**
305     * Temp directory where to write the thumbnails and deltas.
306     */
307    @NonNull
308    private static File getTempDir() {
309        if (System.getProperty("os.name").equals("Mac OS X")) {
310            return new File("/tmp"); //$NON-NLS-1$
311        }
312
313        return new File(System.getProperty("java.io.tmpdir")); //$NON-NLS-1$
314    }
315
316    /**
317     * Saves the generated thumbnail image and appends the info message to an initial message
318     */
319    @NonNull
320    private static String saveImageAndAppendMessage(@NonNull BufferedImage image,
321            @NonNull String initialMessage, @NonNull String relativePath) throws IOException {
322        File output = new File(getTempDir(), getName(relativePath));
323        if (output.exists()) {
324            boolean deleted = output.delete();
325            assertTrue(deleted);
326        }
327        ImageIO.write(image, "PNG", output);
328        initialMessage += "Thumbnail for current rendering stored at " + output.getPath();
329//        initialMessage += "\nRun the following command to accept the changes:\n";
330//        initialMessage += String.format("mv %1$s %2$s", output.getPath(),
331//                ImageUtils.class.getResource(relativePath).getPath());
332        // The above has been commented out, since the destination path returned is in out dir
333        // and it makes the tests pass without the code being actually checked in.
334        return initialMessage;
335    }
336
337    private static String getName(@NonNull String relativePath) {
338        return relativePath.substring(relativePath.lastIndexOf(separatorChar) + 1);
339    }
340}
341