| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.util; |
| 23 |
|
| 24 |
import static java.awt.image.BufferedImage.TYPE_INT_ARGB; |
| 25 |
import static java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE; |
| 26 |
|
| 27 |
import java.awt.*; |
| 28 |
import java.awt.geom.AffineTransform; |
| 29 |
import java.awt.image.*; |
| 30 |
|
| 31 |
import org.jetbrains.annotations.NotNull; |
| 32 |
import org.jetbrains.annotations.Nullable; |
| 33 |
|
| 34 |
import com.github.weisj.jsvg.geometry.util.GeometryUtil; |
| 35 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 36 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 37 |
import com.github.weisj.jsvg.renderer.output.impl.GraphicsUtil; |
| 38 |
|
| 39 |
public final class ImageUtil { |
| 40 |
|
| 41 |
private ImageUtil() {} |
| 42 |
|
| 43 |
public enum Premultiplied { |
| 44 |
Yes, |
| 45 |
No |
| 46 |
} |
| 47 |
|
| 48 |
public static @NotNull BufferedImage createCompatibleTransparentImage(@NotNull Output output, |
| 49 |
double width, double height) { |
| 50 |
return createCompatibleTransparentImage(output.transform(), width, height); |
| 51 |
} |
| 52 |
|
| 53 |
public static @NotNull BufferedImage createCompatibleTransparentImage(int width, int height) { |
| 54 |
return createCompatibleTransparentImage(width, height, Premultiplied.No); |
| 55 |
} |
| 56 |
|
| 57 |
public static @NotNull BufferedImage createCompatibleTransparentImage(int width, int height, |
| 58 |
Premultiplied preMultiplied) { |
| 59 |
int type = preMultiplied == Premultiplied.Yes ? TYPE_INT_ARGB_PRE : TYPE_INT_ARGB; |
| 60 |
return new BufferedImage(width, height, type); |
| 61 |
} |
| 62 |
|
| 63 |
public static @NotNull BufferedImage createCompatibleTransparentImage(@Nullable AffineTransform at, double width, |
| 64 |
double height) { |
| 65 |
return createCompatibleTransparentImage( |
| 66 |
(int) Math.ceil(GeometryUtil.scaleXOfTransform(at) * width), |
| 67 |
(int) Math.ceil(GeometryUtil.scaleYOfTransform(at) * height)); |
| 68 |
} |
| 69 |
|
| 70 |
public static @NotNull BufferedImage createLuminosityBuffer(@Nullable AffineTransform at, double width, |
| 71 |
double height) { |
| 72 |
return new BufferedImage( |
| 73 |
(int) Math.ceil(GeometryUtil.scaleXOfTransform(at) * width), |
| 74 |
(int) Math.ceil(GeometryUtil.scaleYOfTransform(at) * height), BufferedImage.TYPE_BYTE_GRAY); |
| 75 |
} |
| 76 |
|
| 77 |
public static boolean is_INT_PACK_Data(@NotNull SampleModel sm, boolean requireAlpha) { |
| 78 |
if (!(sm instanceof SinglePixelPackedSampleModel)) return false; |
| 79 |
if (sm.getDataType() != DataBuffer.TYPE_INT) return false; |
| 80 |
|
| 81 |
int[] masks = ((SinglePixelPackedSampleModel) sm).getBitMasks(); |
| 82 |
if (masks.length == 3) { |
| 83 |
if (requireAlpha) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
} else if (masks.length != 4) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
if (masks[0] != 0x00ff0000) return false; |
| 91 |
if (masks[1] != 0x0000ff00) return false; |
| 92 |
if (masks[2] != 0x000000ff) return false; |
| 93 |
return (masks.length != 4) || (masks[3] == 0xff000000); |
| 94 |
} |
| 95 |
|
| 96 |
public static int[] getINT_RGBA_DataBank(@NotNull Raster raster) { |
| 97 |
DataBufferInt dstDB = (DataBufferInt) raster.getDataBuffer(); |
| 98 |
return dstDB.getBankData()[0]; |
| 99 |
} |
| 100 |
|
| 101 |
public static int getINT_RGBA_DataOffset(@NotNull Raster raster) { |
| 102 |
DataBufferInt dstDB = (DataBufferInt) raster.getDataBuffer(); |
| 103 |
SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) raster.getSampleModel(); |
| 104 |
return dstDB.getOffset() + sppsm.getOffset( |
| 105 |
raster.getMinX() - raster.getSampleModelTranslateX(), |
| 106 |
raster.getMinY() - raster.getSampleModelTranslateY()); |
| 107 |
} |
| 108 |
|
| 109 |
public static int getINT_RGBA_DataAdjust(@NotNull Raster raster) { |
| 110 |
return getINT_RGBA_ScanlineStride(raster) - raster.getWidth(); |
| 111 |
} |
| 112 |
|
| 113 |
public static int getINT_RGBA_ScanlineStride(@NotNull Raster raster) { |
| 114 |
return ((SinglePixelPackedSampleModel) raster.getSampleModel()).getScanlineStride(); |
| 115 |
} |
| 116 |
|
| 117 |
public static @NotNull BufferedImage copy(@NotNull RenderContext context, @NotNull ImageProducer producer, |
| 118 |
Premultiplied premultiplied) { |
| 119 |
return toBufferedImage(context.platformSupport().createImage(producer), premultiplied); |
| 120 |
} |
| 121 |
|
| 122 |
public static @NotNull BufferedImage toBufferedImage(@NotNull Image img) { |
| 123 |
return toBufferedImage(img, Premultiplied.No); |
| 124 |
} |
| 125 |
|
| 126 |
public static @NotNull BufferedImage toBufferedImage(@NotNull Image img, Premultiplied premultiplied) { |
| 127 |
BufferedImage bufferedImage = createCompatibleTransparentImage(img.getWidth(null), img.getHeight(null), |
| 128 |
premultiplied); |
| 129 |
Graphics2D g = GraphicsUtil.createGraphics(bufferedImage); |
| 130 |
g.drawImage(img, null, null); |
| 131 |
g.dispose(); |
| 132 |
return bufferedImage; |
| 133 |
} |
| 134 |
|
| 135 |
public static @NotNull ColorModel coerceData(@NotNull WritableRaster wr, @NotNull ColorModel cm, |
| 136 |
boolean newAlphaPreMultiplied) { |
| 137 |
if (!cm.hasAlpha()) return cm; |
| 138 |
if (cm.isAlphaPremultiplied() == newAlphaPreMultiplied) return cm; |
| 139 |
return cm.coerceData(wr, newAlphaPreMultiplied); |
| 140 |
} |
| 141 |
} |