|
34 | 34 | import java.awt.Graphics2D; |
35 | 35 | import java.awt.RenderingHints.Key; |
36 | 36 | import java.awt.image.BufferedImage; |
37 | | -import java.awt.image.ColorModel; |
38 | | -import java.awt.image.ComponentColorModel; |
39 | | -import java.awt.image.DirectColorModel; |
40 | | -import java.awt.image.IndexColorModel; |
41 | | -import java.awt.image.WritableRaster; |
| 37 | +import java.awt.image.DataBufferInt; |
42 | 38 | import java.io.IOException; |
43 | 39 | import java.io.InputStream; |
44 | 40 | import java.util.Map; |
45 | 41 |
|
46 | 42 | import org.eclipse.swt.SWT; |
47 | 43 | import org.eclipse.swt.graphics.ImageData; |
48 | 44 | import org.eclipse.swt.graphics.PaletteData; |
49 | | -import org.eclipse.swt.graphics.RGB; |
50 | 45 | import org.eclipse.swt.internal.SVGRasterizer; |
51 | 46 |
|
52 | 47 | import com.github.weisj.jsvg.SVGDocument; |
@@ -94,7 +89,7 @@ private SVGDocument loadSVG(InputStream inputStream) { |
94 | 89 |
|
95 | 90 | private ImageData[] generateRasterizedImageData(SVGDocument svgDocument, int zoom) { |
96 | 91 | BufferedImage rasterizedImage = renderSVG(svgDocument, zoom); |
97 | | - return transformToSWTImageData(rasterizedImage); |
| 92 | + return convertToSWTImageData(rasterizedImage); |
98 | 93 | } |
99 | 94 |
|
100 | 95 | private BufferedImage renderSVG(SVGDocument svgDocument, int zoom) { |
@@ -129,83 +124,21 @@ private Graphics2D configureRenderingOptions(float scalingFactor, BufferedImage |
129 | 124 | g.scale(scalingFactor, scalingFactor); |
130 | 125 | return g; |
131 | 126 | } |
132 | | - |
133 | | - private ImageData[] transformToSWTImageData(BufferedImage bufferedImage) { |
134 | | - ColorModel colorModel = bufferedImage.getColorModel(); |
135 | | - if (colorModel instanceof DirectColorModel directColorModel) { |
136 | | - return generateSWTImageData(bufferedImage, directColorModel); |
137 | | - } else if (colorModel instanceof IndexColorModel indexColorModel) { |
138 | | - return generateSWTImageData(bufferedImage, indexColorModel); |
139 | | - } else if (colorModel instanceof ComponentColorModel componentColorModel) { |
140 | | - return generateSWTImageData(bufferedImage, componentColorModel); |
141 | | - } |
142 | | - return null; |
143 | | - } |
144 | | - |
145 | | - private ImageData[] generateSWTImageData(BufferedImage bufferedImage, DirectColorModel colorModel) { |
146 | | - PaletteData paletteData = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), |
147 | | - colorModel.getBlueMask()); |
148 | | - ImageData imageData = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), |
149 | | - colorModel.getPixelSize(), paletteData); |
150 | | - for (int y = 0; y < imageData.height; y++) { |
151 | | - for (int x = 0; x < imageData.width; x++) { |
152 | | - int rgb = bufferedImage.getRGB(x, y); |
153 | | - int pixel = paletteData.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF)); |
154 | | - imageData.setPixel(x, y, pixel); |
155 | | - if (colorModel.hasAlpha()) { |
156 | | - imageData.setAlpha(x, y, (rgb >> 24) & 0xFF); |
157 | | - } |
158 | | - } |
159 | | - } |
160 | | - return new ImageData[] { imageData }; |
161 | | - } |
162 | | - |
163 | | - private ImageData[] generateSWTImageData(BufferedImage bufferedImage, IndexColorModel indexColorModel) { |
164 | | - RGB[] colors = calculateColors(indexColorModel); |
165 | | - PaletteData paletteData = new PaletteData(colors); |
166 | | - ImageData imageData = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), |
167 | | - indexColorModel.getPixelSize(), paletteData); |
168 | | - imageData.transparentPixel = indexColorModel.getTransparentPixel(); |
169 | | - WritableRaster raster = bufferedImage.getRaster(); |
170 | | - int[] pixelArray = new int[1]; |
171 | | - for (int y = 0; y < imageData.height; y++) { |
172 | | - for (int x = 0; x < imageData.width; x++) { |
173 | | - raster.getPixel(x, y, pixelArray); |
174 | | - imageData.setPixel(x, y, pixelArray[0]); |
175 | | - } |
176 | | - } |
177 | | - return new ImageData[] { imageData }; |
178 | | - } |
179 | | - |
180 | | - private RGB[] calculateColors(IndexColorModel indexColorModel) { |
181 | | - int size = indexColorModel.getMapSize(); |
182 | | - byte[] reds = new byte[size]; |
183 | | - byte[] greens = new byte[size]; |
184 | | - byte[] blues = new byte[size]; |
185 | | - indexColorModel.getReds(reds); |
186 | | - indexColorModel.getGreens(greens); |
187 | | - indexColorModel.getBlues(blues); |
188 | | - RGB[] rgbs = new RGB[size]; |
189 | | - for (int i = 0; i < rgbs.length; i++) { |
190 | | - rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF); |
191 | | - } |
192 | | - return rgbs; |
193 | | - } |
194 | | - |
195 | | - private ImageData[] generateSWTImageData(BufferedImage bufferedImage, ComponentColorModel componentColorModel) { |
196 | | - PaletteData paletteData = new PaletteData(0x0000FF, 0x00FF00, 0xFF0000); |
197 | | - ImageData imageData = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), |
198 | | - componentColorModel.getPixelSize(), paletteData); |
199 | | - imageData.transparentPixel = -1; |
200 | | - WritableRaster raster = bufferedImage.getRaster(); |
201 | | - int[] pixelArray = new int[3]; |
202 | | - for (int y = 0; y < imageData.height; y++) { |
| 127 | + |
| 128 | + private ImageData[] convertToSWTImageData(BufferedImage rasterizedImage) { |
| 129 | + int width = rasterizedImage.getWidth(); |
| 130 | + int height = rasterizedImage.getHeight(); |
| 131 | + int[] pixels = ((DataBufferInt) rasterizedImage.getRaster().getDataBuffer()).getData(); |
| 132 | + PaletteData paletteData = new PaletteData(0x00FF0000, 0x0000FF00, 0x000000FF); |
| 133 | + ImageData imageData = new ImageData(width, height, 32, paletteData); |
| 134 | + int index = 0; |
| 135 | + for (int y = 0; y < imageData.height; y++) { |
203 | 136 | for (int x = 0; x < imageData.width; x++) { |
204 | | - raster.getPixel(x, y, pixelArray); |
205 | | - int pixel = paletteData.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2])); |
206 | | - imageData.setPixel(x, y, pixel); |
| 137 | + int alpha = (pixels[index] >> 24) & 0xFF; |
| 138 | + imageData.setAlpha(x, y, alpha); |
| 139 | + imageData.setPixel(x, y, pixels[index++]); |
207 | 140 | } |
208 | 141 | } |
209 | | - return new ImageData[] { imageData }; |
| 142 | + return new ImageData[]{imageData}; |
210 | 143 | } |
211 | 144 | } |
0 commit comments