Windows path correction.
[SquirrelJME.git] / assets / snippets / RGBAlpha.java
blobc14bb588eeb0e49dde1502580a8c5c4a4dc35336
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 import java.awt.Graphics;
11 import java.awt.image.BufferedImage;
12 import java.awt.image.DataBufferInt;
13 import java.io.ByteArrayOutputStream;
14 import java.io.File;
15 import java.io.IOException;
16 import java.nio.ByteBuffer;
17 import java.nio.ByteOrder;
18 import java.nio.IntBuffer;
19 import java.nio.file.Files;
20 import java.nio.file.Path;
21 import java.nio.file.Paths;
22 import java.util.Arrays;
23 import java.util.zip.ZipEntry;
24 import java.util.zip.ZipOutputStream;
25 import javax.imageio.ImageIO;
26 import javax.imageio.ImageWriter;
28 /**
29 * Generates RGB and Alpha table mappings.
31 * @since 2024/07/11
33 public final class RGBAlpha
35 /** Sequence of values for ranges. */
36 private static final int[] _SEQ =
37 new int[]{0x00, 0x11, 0x33, 0x66, 0x80, 0x99, 0xCC, 0xEE, 0xFF};
39 /**
40 * Main entry point.
42 * @param __args Ignored.
43 * @throws IOException On write errors.
44 * @since 2024/07/11
46 public static void main(String... __args)
47 throws IOException
49 int n = RGBAlpha._SEQ.length;
51 // Generate base table of colors
52 int[] rgb = new int[n * n * n];
53 for (int ri = 0, at = 0; ri < n; ri++)
54 for (int gi = 0; gi < n; gi++)
55 for (int bi = 0; bi < n; bi++)
57 rgb[at++] = (RGBAlpha._SEQ[ri] << 16) |
58 (RGBAlpha._SEQ[gi] << 8) |
59 (RGBAlpha._SEQ[bi]);
62 // Dump base color set
63 RGBAlpha.__dump(Paths.get("______.rgb"), rgb);
65 int len = rgb.length;
66 int dim = (int)Math.ceil(Math.sqrt(len));
68 // Go through each channel value
69 for (int fi = 0; fi < n; fi++)
70 for (int ti = 0; ti < n; ti++)
72 int fromA = RGBAlpha._SEQ[fi];
73 int toA = RGBAlpha._SEQ[ti];
75 BufferedImage from = new BufferedImage(dim, dim,
76 BufferedImage.TYPE_INT_ARGB);
77 BufferedImage to = new BufferedImage(dim, dim,
78 BufferedImage.TYPE_INT_ARGB);
80 int[] fromB = ((DataBufferInt)from.getRaster().getDataBuffer())
81 .getData();
82 int[] toB = ((DataBufferInt)to.getRaster().getDataBuffer())
83 .getData();
85 int actA = fromA;/*(fromA & (~0b1111)) | (fromA >>> 4);*/
86 int actB = toA;/*(toA & (~0b1111)) | (toA >>> 4);*/
88 for (int i = 0, q = rgb.length; i < q; i++)
90 fromB[i] = (actA << 24) | rgb[i];
91 toB[(rgb.length - 1) - i] = (actB << 24) | rgb[i];
94 // Blend into the target
95 Graphics g = from.getGraphics();
96 g.drawImage(to, 0, 0, null);
98 ImageIO.write(from, "png",
99 new File(String.format("_%02x_%02x.png", fromA, toA)));
101 // Dump to file
102 RGBAlpha.__dump(Paths.get(String.format("_%02x_%02x.rgb",
103 fromA, toA)), fromB);
108 * Dumps the colors.
110 * @param __to Where to write to.
111 * @param __colors The colors to write.
112 * @throws IOException On write errors.
113 * @since 2024/07/11
115 private static void __dump(Path __to, int... __colors)
116 throws IOException
118 ByteBuffer buf = ByteBuffer.allocate(__colors.length * 4);
119 buf.order(ByteOrder.BIG_ENDIAN);
120 buf.asIntBuffer().put(IntBuffer.wrap(__colors));
122 byte[] data;
123 try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
124 ZipOutputStream zip = new ZipOutputStream(baos))
126 zip.setLevel(9);
127 zip.setMethod(ZipOutputStream.DEFLATED);
129 zip.putNextEntry(new ZipEntry(__to.getFileName().toString()));
131 zip.write(buf.array());
133 zip.closeEntry();
135 zip.finish();
136 zip.flush();
138 data = baos.toByteArray();
141 Files.write(__to, data);