2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.renderer.raster.texture;
7 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
9 public class TextureBitmap {
14 public final byte[] bytes;
16 public final int width;
18 public final int height;
20 public double multiplicationFactor;
22 public TextureBitmap(final int width, final int height, final byte[] bytes,
23 final double multiplicationFactor) {
28 this.multiplicationFactor = multiplicationFactor;
31 public TextureBitmap(final int width, final int height,
32 final double multiplicationFactor) {
34 this(width, height, new byte[width * height * 4], multiplicationFactor);
37 public void drawPixel(int textureOffset,
38 final byte[] targetRenderingAreaBytes, int targetRenderingAreaOffset) {
40 final int textureAlpha = bytes[textureOffset] & 0xff;
42 if (textureAlpha == 0)
45 if (textureAlpha == 255) {
46 // skip reading of background for fully opaque pixels
47 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) 255;
49 targetRenderingAreaOffset++;
51 targetRenderingAreaBytes[targetRenderingAreaOffset] = bytes[textureOffset];
53 targetRenderingAreaOffset++;
55 targetRenderingAreaBytes[targetRenderingAreaOffset] = bytes[textureOffset];
57 targetRenderingAreaOffset++;
59 targetRenderingAreaBytes[targetRenderingAreaOffset] = bytes[textureOffset];
63 final int backgroundAlpha = 255 - textureAlpha;
66 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) 255;
67 targetRenderingAreaOffset++;
69 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) ((((targetRenderingAreaBytes[targetRenderingAreaOffset] & 0xff) * backgroundAlpha) + ((bytes[textureOffset] & 0xff) * textureAlpha)) / 256);
71 targetRenderingAreaOffset++;
73 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) ((((targetRenderingAreaBytes[targetRenderingAreaOffset] & 0xff) * backgroundAlpha) + ((bytes[textureOffset] & 0xff) * textureAlpha)) / 256);
75 targetRenderingAreaOffset++;
77 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) ((((targetRenderingAreaBytes[targetRenderingAreaOffset] & 0xff) * backgroundAlpha) + ((bytes[textureOffset] & 0xff) * textureAlpha)) / 256);
80 public void drawPixel(final int x, final int y, final Color color) {
81 int address = getAddress(x, y);
83 bytes[address] = (byte) color.a;
86 bytes[address] = (byte) color.b;
89 bytes[address] = (byte) color.g;
92 bytes[address] = (byte) color.r;
95 public void drawRectangle(int x1, final int y1, int x2, final int y2,
110 for (int y = y1; y < y2; y++)
111 for (int x = x1; x < x2; x++)
112 drawPixel(x, y, color);
115 public void fillColor(final Color color) {
117 while (address < bytes.length) {
118 bytes[address] = (byte) color.a;
121 bytes[address] = (byte) color.b;
124 bytes[address] = (byte) color.g;
127 bytes[address] = (byte) color.r;
132 public int getAddress(int x, int y) {
145 return ((y * width) + x) * 4;