2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
8 package eu.svjatoslav.sixth.e3d.renderer.raster.texture;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
12 public class TextureBitmap {
17 public final byte[] bytes;
19 public final int width;
21 public final int height;
23 public double multiplicationFactor;
25 public TextureBitmap(final int width, final int height, final byte[] bytes,
26 final double multiplicationFactor) {
31 this.multiplicationFactor = multiplicationFactor;
34 public TextureBitmap(final int width, final int height,
35 final double multiplicationFactor) {
37 this(width, height, new byte[width * height * 4], multiplicationFactor);
40 public void drawPixel(int textureOffset,
41 final byte[] targetRenderingAreaBytes, int targetRenderingAreaOffset) {
43 final int textureAlpha = bytes[textureOffset] & 0xff;
45 if (textureAlpha == 0)
48 if (textureAlpha == 255) {
49 // skip reading of background for fully opaque pixels
50 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) 255;
52 targetRenderingAreaOffset++;
54 targetRenderingAreaBytes[targetRenderingAreaOffset] = bytes[textureOffset];
56 targetRenderingAreaOffset++;
58 targetRenderingAreaBytes[targetRenderingAreaOffset] = bytes[textureOffset];
60 targetRenderingAreaOffset++;
62 targetRenderingAreaBytes[targetRenderingAreaOffset] = bytes[textureOffset];
66 final int backgroundAlpha = 255 - textureAlpha;
69 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) 255;
70 targetRenderingAreaOffset++;
72 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) ((((targetRenderingAreaBytes[targetRenderingAreaOffset] & 0xff) * backgroundAlpha) + ((bytes[textureOffset] & 0xff) * textureAlpha)) / 256);
74 targetRenderingAreaOffset++;
76 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) ((((targetRenderingAreaBytes[targetRenderingAreaOffset] & 0xff) * backgroundAlpha) + ((bytes[textureOffset] & 0xff) * textureAlpha)) / 256);
78 targetRenderingAreaOffset++;
80 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) ((((targetRenderingAreaBytes[targetRenderingAreaOffset] & 0xff) * backgroundAlpha) + ((bytes[textureOffset] & 0xff) * textureAlpha)) / 256);
83 public void drawPixel(final int x, final int y, final Color color) {
84 int address = getAddress(x, y);
86 bytes[address] = (byte) color.a;
89 bytes[address] = (byte) color.b;
92 bytes[address] = (byte) color.g;
95 bytes[address] = (byte) color.r;
98 public void drawRectangle(int x1, final int y1, int x2, final int y2,
113 for (int y = y1; y < y2; y++)
114 for (int x = x1; x < x2; x++)
115 drawPixel(x, y, color);
118 public void fillColor(final Color color) {
120 while (address < bytes.length) {
121 bytes[address] = (byte) color.a;
124 bytes[address] = (byte) color.b;
127 bytes[address] = (byte) color.g;
130 bytes[address] = (byte) color.r;
135 public int getAddress(int x, int y) {
148 return ((y * width) + x) * 4;