|
|
@@ -15,7 +15,12 @@
|
|
|
*/
|
|
|
package com.tencent.qgame.playerproj.animtool;
|
|
|
|
|
|
+import com.tencent.qgame.playerproj.animtool.data.PointRect;
|
|
|
+
|
|
|
import javax.imageio.ImageIO;
|
|
|
+
|
|
|
+import java.awt.geom.AffineTransform;
|
|
|
+import java.awt.image.AffineTransformOp;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
@@ -23,52 +28,26 @@ import java.util.Arrays;
|
|
|
|
|
|
public class GetAlphaFrame {
|
|
|
|
|
|
- public static final int ORIN_H = 1; // 左右对齐
|
|
|
- public static final int ORIN_V = 2; // 上下对齐
|
|
|
-
|
|
|
-
|
|
|
public static class AlphaFrameOut {
|
|
|
|
|
|
-
|
|
|
- public int orin;
|
|
|
public int[] argb;
|
|
|
- public int w;
|
|
|
- public int h;
|
|
|
- public int outW;
|
|
|
- public int outH;
|
|
|
- public int gap;
|
|
|
|
|
|
-
|
|
|
- public AlphaFrameOut(int orin, int[] argb, int w, int h, int outW, int outH, int gap) {
|
|
|
- this.orin = orin;
|
|
|
+ public AlphaFrameOut(int[] argb) {
|
|
|
this.argb = argb;
|
|
|
- this.w = w;
|
|
|
- this.h = h;
|
|
|
- this.outW = outW;
|
|
|
- this.outH = outH;
|
|
|
- this.gap = gap;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- *
|
|
|
- * @param orin
|
|
|
- * @param w 原图像宽
|
|
|
- * @param h 原图像高
|
|
|
- * @param gap rgb 与 alpha 之间间隔距离
|
|
|
- * @param inputFile
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public AlphaFrameOut createFrame(int orin, int w, int h, int gap, int wFill, int hFill, File inputFile) throws IOException {
|
|
|
+ public AlphaFrameOut createFrame(CommonArg commonArg, File inputFile) throws IOException {
|
|
|
|
|
|
if (!inputFile.exists()) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- int outW = (orin == ORIN_H ? (w * 2 + gap) : w) + wFill;
|
|
|
- int outH = (orin == ORIN_H ? h : (h * 2 + gap)) + hFill;
|
|
|
+ int w = commonArg.rgbPoint.w;
|
|
|
+ int h = commonArg.rgbPoint.h;
|
|
|
+ int outW = commonArg.outputW;
|
|
|
+ int outH = commonArg.outputH;
|
|
|
|
|
|
BufferedImage inputBuf = ImageIO.read(inputFile);
|
|
|
int[] inputArgb = inputBuf.getRGB(0, 0, w, h, null, 0, w);
|
|
|
@@ -76,23 +55,57 @@ public class GetAlphaFrame {
|
|
|
int[] outputArgb = new int[outW * outH];
|
|
|
Arrays.fill(outputArgb, 0xff000000);
|
|
|
|
|
|
- for (int k=0; k<2; k++) {
|
|
|
- for (int x = 0; x < w; x++) {
|
|
|
- for (int y = 0; y < h; y++) {
|
|
|
- int outPoint = orin == ORIN_H ? k * (w + gap) + x + y * outW : k * outW * (h + gap) + x + y * outW;
|
|
|
- if (k == 0) {
|
|
|
- int alpha = inputArgb[x + y * w] >>> 24;
|
|
|
- // r = g = b
|
|
|
- outputArgb[outPoint] = 0xff000000 + (alpha << 16) + (alpha << 8) + alpha;
|
|
|
- } else {
|
|
|
- outputArgb[outPoint] = blendBg(inputArgb[x + y * w], 0xff000000);
|
|
|
- }
|
|
|
+ BufferedImage alphaBuf = inputBuf;
|
|
|
+ int[] alphaArgb = inputArgb;
|
|
|
+
|
|
|
+ if (commonArg.scale < 1f) {
|
|
|
+ AffineTransform at = new AffineTransform();
|
|
|
+ at.scale(commonArg.scale, commonArg.scale);
|
|
|
+
|
|
|
+ alphaBuf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
|
|
|
+ AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
|
|
|
+ alphaBuf = scaleOp.filter(inputBuf, alphaBuf);
|
|
|
+
|
|
|
+ alphaArgb = alphaBuf.getRGB(0, 0, w, h, null, 0, w);
|
|
|
+ }
|
|
|
+
|
|
|
+ // rgb 区域
|
|
|
+ fillColor(outputArgb, outW, commonArg.rgbPoint, false, inputArgb, w);
|
|
|
+
|
|
|
+ // alpha 区域
|
|
|
+ fillColor(outputArgb, outW, commonArg.alphaPoint, true, alphaArgb, w);
|
|
|
+
|
|
|
+
|
|
|
+ return new AlphaFrameOut(outputArgb);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void fillColor(int[] outputArgb, int outputW, PointRect point, boolean isAlpha, int[] inputArgb, int inputW) {
|
|
|
+ int outX = 0;
|
|
|
+ int outY = 0;
|
|
|
+ for (int y = 0; y < point.h ; y++) {
|
|
|
+ outY = point.y + y;
|
|
|
+ for (int x = 0; x < point.w ; x++) {
|
|
|
+ outX = point.x + x;
|
|
|
+ int tmpP = x + y * inputW;
|
|
|
+ if (tmpP >= inputArgb.length) {
|
|
|
+ TLog.i("hexleo_test", "x=" + x + ",y=" + y + ",inputW=" + inputW);
|
|
|
}
|
|
|
+ int color = inputArgb[x + y * inputW];
|
|
|
+ outputArgb[outX + outY * outputW] = isAlpha ? getAlpha(color) : getColor(color);
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- return new AlphaFrameOut(orin, outputArgb, w, h, outW, outH, gap);
|
|
|
+ private int getColor(int color) {
|
|
|
+ return blendBg(color, 0xff000000);
|
|
|
+ }
|
|
|
|
|
|
+ private int getAlpha(int color) {
|
|
|
+ int alpha = color >>> 24;
|
|
|
+ // r = g = b
|
|
|
+ return 0xff000000 + (alpha << 16) + (alpha << 8) + alpha;
|
|
|
}
|
|
|
|
|
|
private int blendBg(int color, int colorBg) {
|