2 * Sixth - System for data storage, computation, exploration and interaction.
3 * Copyright ©2012-2016, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.sixth.e3d.renderer.raster.texture;
12 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
14 public class TextureBitmap {
19 public final byte[] bytes;
21 public final int width;
23 public final int height;
25 public double multiplicationFactor;
27 public TextureBitmap(final int width, final int height, final byte[] bytes,
28 final double multiplicationFactor) {
33 this.multiplicationFactor = multiplicationFactor;
36 public TextureBitmap(final int width, final int height,
37 final double multiplicationFactor) {
39 this(width, height, new byte[width * height * 4], multiplicationFactor);
42 public void drawPixel(int textureOffset,
43 final byte[] targetRenderingAreaBytes, int targetRenderingAreaOffset) {
45 final int textureAlpha = bytes[textureOffset] & 0xff;
47 if (textureAlpha == 0)
50 if (textureAlpha == 255) {
51 // skip reading of background for fully opaque pixels
52 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) 255;
54 targetRenderingAreaOffset++;
56 targetRenderingAreaBytes[targetRenderingAreaOffset] = bytes[textureOffset];
58 targetRenderingAreaOffset++;
60 targetRenderingAreaBytes[targetRenderingAreaOffset] = bytes[textureOffset];
62 targetRenderingAreaOffset++;
64 targetRenderingAreaBytes[targetRenderingAreaOffset] = bytes[textureOffset];
68 final int backgroundAlpha = 255 - textureAlpha;
71 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) 255;
72 targetRenderingAreaOffset++;
74 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) ((((targetRenderingAreaBytes[targetRenderingAreaOffset] & 0xff) * backgroundAlpha) + ((bytes[textureOffset] & 0xff) * textureAlpha)) / 256);
76 targetRenderingAreaOffset++;
78 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) ((((targetRenderingAreaBytes[targetRenderingAreaOffset] & 0xff) * backgroundAlpha) + ((bytes[textureOffset] & 0xff) * textureAlpha)) / 256);
80 targetRenderingAreaOffset++;
82 targetRenderingAreaBytes[targetRenderingAreaOffset] = (byte) ((((targetRenderingAreaBytes[targetRenderingAreaOffset] & 0xff) * backgroundAlpha) + ((bytes[textureOffset] & 0xff) * textureAlpha)) / 256);
85 public void drawPixel(final int x, final int y, final Color color) {
86 int address = getAddress(x, y);
88 bytes[address] = (byte) color.a;
91 bytes[address] = (byte) color.b;
94 bytes[address] = (byte) color.g;
97 bytes[address] = (byte) color.r;
100 public void drawRectangle(int x1, final int y1, int x2, final int y2,
115 for (int y = y1; y < y2; y++)
116 for (int x = x1; x < x2; x++)
117 drawPixel(x, y, color);
120 public void fillColor(final Color color) {
122 while (address < bytes.length) {
123 bytes[address] = (byte) color.a;
126 bytes[address] = (byte) color.b;
129 bytes[address] = (byte) color.g;
132 bytes[address] = (byte) color.r;
137 public int getAddress(int x, int y) {
150 return ((y * width) + x) * 4;