|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2018 Laurent Caron and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Jerry Huxtable - Original Version |
| 13 | + * Laurent Caron (laurent.caron at gmail dot com) - Conversion to SWT |
| 14 | + * IBM Corporation - adaptation to GraphicsExample |
| 15 | + *******************************************************************************/ |
| 16 | + |
| 17 | +package org.eclipse.swt.examples.graphics; |
| 18 | + |
| 19 | +import org.eclipse.swt.graphics.GC; |
| 20 | +import org.eclipse.swt.graphics.Image; |
| 21 | +import org.eclipse.swt.graphics.ImageData; |
| 22 | + |
| 23 | +/** |
| 24 | + * This tab displays an animated block pixelation effect that oscillates block |
| 25 | + * size from single pixels to large blocks and back on an image. |
| 26 | + */ |
| 27 | +public class BlockEffectTab extends AnimatedGraphicsTab { |
| 28 | + |
| 29 | + private ImageData sourceImage; |
| 30 | + private ImageData imageData; |
| 31 | + private Image outputImage; |
| 32 | + private int imgWidth, imgHeight; |
| 33 | + private int blockSize; |
| 34 | + private int direction; |
| 35 | + |
| 36 | + public BlockEffectTab(GraphicsExample example) { |
| 37 | + super(example); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getCategory() { |
| 42 | + return GraphicsExample.getResourceString("Misc"); //$NON-NLS-1$ |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getText() { |
| 47 | + return GraphicsExample.getResourceString("BlockEffect"); //$NON-NLS-1$ |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getDescription() { |
| 52 | + return GraphicsExample.getResourceString("BlockEffectDescription"); //$NON-NLS-1$ |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public int getInitialAnimationTime() { |
| 57 | + return 10; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void dispose() { |
| 62 | + if (outputImage != null) { |
| 63 | + outputImage.dispose(); |
| 64 | + outputImage = null; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void next(int width, int height) { |
| 70 | + if (sourceImage == null) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + imageData = filter(sourceImage, blockSize); |
| 75 | + blockSize += direction; |
| 76 | + |
| 77 | + if (blockSize <= 1) { |
| 78 | + direction = 1; |
| 79 | + blockSize = 1; |
| 80 | + } |
| 81 | + |
| 82 | + if (blockSize >= imgWidth / 4) { |
| 83 | + direction = -1; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void paint(GC gc, int width, int height) { |
| 89 | + if (!example.checkAdvancedGraphics()) return; |
| 90 | + |
| 91 | + if (sourceImage == null) { |
| 92 | + Image loaded = example.loadImage(gc.getDevice(), "flower.jpg"); //$NON-NLS-1$ |
| 93 | + if (loaded == null) return; |
| 94 | + sourceImage = loaded.getImageData(); |
| 95 | + imgWidth = sourceImage.width; |
| 96 | + imgHeight = sourceImage.height; |
| 97 | + |
| 98 | + int[] pixels = new int[imgWidth * imgHeight]; |
| 99 | + sourceImage.getPixels(0, 0, imgWidth * imgHeight, pixels, 0); |
| 100 | + imageData = new ImageData(imgWidth, imgHeight, sourceImage.depth, sourceImage.palette); |
| 101 | + imageData.setPixels(0, 0, imgWidth * imgHeight, pixels, 0); |
| 102 | + |
| 103 | + blockSize = 1; |
| 104 | + direction = 1; |
| 105 | + } |
| 106 | + |
| 107 | + if (imageData == null) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if (outputImage != null) { |
| 112 | + outputImage.dispose(); |
| 113 | + } |
| 114 | + outputImage = new Image(gc.getDevice(), imageData); |
| 115 | + |
| 116 | + int x = (width - imgWidth) / 2; |
| 117 | + int y = (height - imgHeight) / 2; |
| 118 | + gc.drawImage(outputImage, x, y); |
| 119 | + } |
| 120 | + |
| 121 | + private ImageData filter(ImageData src, int size) { |
| 122 | + if (size <= 1) { |
| 123 | + int[] pixels = new int[imgWidth * imgHeight]; |
| 124 | + src.getPixels(0, 0, imgWidth * imgHeight, pixels, 0); |
| 125 | + ImageData copy = new ImageData(imgWidth, imgHeight, src.depth, src.palette); |
| 126 | + copy.setPixels(0, 0, imgWidth * imgHeight, pixels, 0); |
| 127 | + return copy; |
| 128 | + } |
| 129 | + |
| 130 | + ImageData dst = new ImageData(imgWidth, imgHeight, src.depth, src.palette); |
| 131 | + int[] pixels = new int[size * size]; |
| 132 | + |
| 133 | + for (int y = 0; y < imgHeight; y += size) { |
| 134 | + for (int x = 0; x < imgWidth; x += size) { |
| 135 | + int w = Math.min(size, imgWidth - x); |
| 136 | + int h = Math.min(size, imgHeight - y); |
| 137 | + int t = w * h; |
| 138 | + getRGB(src, x, y, w, h, pixels, 0, w); |
| 139 | + int r = 0, g = 0, b = 0; |
| 140 | + int i = 0; |
| 141 | + for (int by = 0; by < h; by++) { |
| 142 | + for (int bx = 0; bx < w; bx++) { |
| 143 | + int argb = pixels[i]; |
| 144 | + r += (argb >> 16) & 0xff; |
| 145 | + g += (argb >> 8) & 0xff; |
| 146 | + b += argb & 0xff; |
| 147 | + i++; |
| 148 | + } |
| 149 | + } |
| 150 | + int argb = ((r / t) << 16) | ((g / t) << 8) | (b / t); |
| 151 | + i = 0; |
| 152 | + for (int by = 0; by < h; by++) { |
| 153 | + for (int bx = 0; bx < w; bx++) { |
| 154 | + pixels[i] = (pixels[i] & 0xff000000) | argb; |
| 155 | + i++; |
| 156 | + } |
| 157 | + } |
| 158 | + setRGB(dst, x, y, w, h, pixels, 0, w); |
| 159 | + } |
| 160 | + } |
| 161 | + return dst; |
| 162 | + } |
| 163 | + |
| 164 | + private static void getRGB(ImageData image, int startX, int startY, int w, int h, int[] pixels, int offset, int scansize) { |
| 165 | + int yoff = offset; |
| 166 | + for (int y = startY; y < startY + h; y++, yoff += scansize) { |
| 167 | + int off = yoff; |
| 168 | + for (int x = startX; x < startX + w; x++) { |
| 169 | + pixels[off++] = image.getPixel(x, y); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private static void setRGB(ImageData image, int startX, int startY, int w, int h, int[] pixels, int offset, int scansize) { |
| 175 | + int yoff = offset; |
| 176 | + for (int y = startY; y < startY + h; y++, yoff += scansize) { |
| 177 | + int off = yoff; |
| 178 | + for (int x = startX; x < startX + w; x++) { |
| 179 | + image.setPixel(x, y, pixels[off++]); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments