Skip to content

Instantly share code, notes, and snippets.

@OneB1t
Last active September 30, 2023 17:55
Show Gist options
  • Save OneB1t/2241cab4aff77b733bc946880e8fbcaa to your computer and use it in GitHub Desktop.
Save OneB1t/2241cab4aff77b733bc946880e8fbcaa to your computer and use it in GitHub Desktop.
bmp generator just with plain java
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
public class MonochromaticBMPGeneratorWithText {
public static void main(String[] args) {
long start1 = System.currentTimeMillis();
int width = 1280;
int height = 640;
int imageSize = (width + 31) / 32 * 4 * height; // Image size (in bytes)
int pixelDataOffset = 54; // Offset to pixel data
try (FileOutputStream fos = new FileOutputStream("monochromatic_with_text.bmp")) {
// BMP header
fos.write(new byte[] {
'B', 'M', // BMP magic number
0, 0, 0, 0, // File size (dummy)
0, 0, 0, 0, // Reserved
54, 0, 0, 0, // Pixel data offset
40, 0, 0, 0, // Header size
(byte) (width & 0xFF), (byte) ((width >> 8) & 0xFF), 0, 0, // Image width
(byte) (height & 0xFF), (byte) ((height >> 8) & 0xFF), 0, 0, // Image height
1, 0, // Number of color planes
1, 0, // Bits per pixel
0, 0, 0, 0, // Compression method (none)
0, 0, 0, 0, // Image size (dummy)
0, 0, 0, 0, // Horizontal resolution (dummy)
0, 0, 0, 0, // Vertical resolution (dummy)
0, 0, 0, 0, // Number of colors in the palette
0, 0, 0, 0, // Important colors
});
// Palette (black and white)
fos.write(new byte[]{0, 0, 0, 0}); // Black
fos.write(new byte[]{(byte) 255, (byte) 255, (byte) 255, 0}); // White
// Monochrome pixel data (all zeros for a black background)
byte[] pixelData = new byte[imageSize];
// Define the text "HELLO, WORLD!"
Random random = new Random();
String text = "RANDOM VALUE " + random.nextInt(0, 9);
// Calculate the starting X position to center the text horizontally
int textWidth = text.length() * 12; // Assuming each character is 8 pixels wide
int textHeight = 16; // Assuming each character is 8 pixels tall
int x = width / 2 - textWidth - 200;
int y = (height - textHeight) / 2;
// Define the font for characters and numbers
byte[] font = {
// Character 'A'
(byte) 0b00000000,
(byte) 0b00011000,
(byte) 0b00011000,
(byte) 0b00011000,
(byte) 0b00011000,
(byte) 0b00100100,
(byte) 0b00100100,
(byte) 0b00100100,
(byte) 0b00111100,
(byte) 0b00100100,
(byte) 0b01100110,
(byte) 0b01000010,
(byte) 0b01000010,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b00000000, // Character 'A'
// Character 'B'
(byte) 0b11111100,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b11111100,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b11111100,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b11111100,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'B'
// Character 'C'
(byte) 0b00000000,
(byte) 0b00111110,
(byte) 0b01000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b01000000,
(byte) 0b00111110,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'C'
// Character 'D'
(byte) 0b11111000,
(byte) 0b10000100,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000100,
(byte) 0b11111000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'D'
// Character 'E'
(byte) 0b11111110,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b11111000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b11111110,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'E'
// Character 'F'
(byte) 0b11111110,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b11111000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'F'
// Character 'G'
(byte) 0b00000000,
(byte) 0b00111110,
(byte) 0b01000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10001110,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b01000010,
(byte) 0b00111100,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'G'
// Character 'H'
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b11111110,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'H'
// Character 'I'
(byte) 0b01111100,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b01111100,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'I'
// Character 'J'
(byte) 0b00111110,
(byte) 0b00000100,
(byte) 0b00000100,
(byte) 0b00000100,
(byte) 0b00000100,
(byte) 0b00000100,
(byte) 0b00000100,
(byte) 0b00000100,
(byte) 0b00000100,
(byte) 0b00000100,
(byte) 0b00000100,
(byte) 0b10001000,
(byte) 0b01110000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'J'
// Character 'K'
(byte) 0b10000010,
(byte) 0b10000100,
(byte) 0b10001000,
(byte) 0b10010000,
(byte) 0b10100000,
(byte) 0b11000000,
(byte) 0b11000000,
(byte) 0b11010000,
(byte) 0b10010000,
(byte) 0b10001000,
(byte) 0b10000100,
(byte) 0b10000010,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'K'
// Character 'L'
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b11111110,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'L'
// Character 'M'
(byte) 0b10000001,
(byte) 0b11000011,
(byte) 0b11000011,
(byte) 0b10100101,
(byte) 0b10100101,
(byte) 0b10011001,
(byte) 0b10011001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'M'
// Character 'N'
(byte) 0b10000001,
(byte) 0b11000001,
(byte) 0b11000001,
(byte) 0b10100001,
(byte) 0b10100001,
(byte) 0b10010001,
(byte) 0b10010001,
(byte) 0b10001001,
(byte) 0b10001001,
(byte) 0b10000101,
(byte) 0b10000101,
(byte) 0b10000011,
(byte) 0b10000011,
(byte) 0b10000001,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'N'
// Character 'O'
(byte) 0b00000000,
(byte) 0b00111100,
(byte) 0b01000010,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b01000010,
(byte) 0b00111100,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'O'
// Character 'P'
(byte) 0b11111100,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b11111100,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'P'
// Character 'Q'
(byte) 0b00000000,
(byte) 0b00111100,
(byte) 0b01000010,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10010001,
(byte) 0b01000010,
(byte) 0b00111100,
(byte) 0b00000110,
(byte) 0b00000001,
(byte) 0b00000000, // Character 'Q'
// Character 'R'
(byte) 0b11111100,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b11111100,
(byte) 0b10010000,
(byte) 0b10001000,
(byte) 0b10000100,
(byte) 0b10000100,
(byte) 0b10000010,
(byte) 0b10000010,
(byte) 0b10000001,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'R'
// Character 'S'
(byte) 0b00111100,
(byte) 0b01000010,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b01111100,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b10000010,
(byte) 0b01111100,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'S'
// Character 'T'
(byte) 0b11111111,
(byte) 0b10010010,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'T'
// Character 'U'
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b01111110,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'U'
// Character 'V'
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b01000010,
(byte) 0b01000010,
(byte) 0b01000010,
(byte) 0b00100100,
(byte) 0b00100100,
(byte) 0b00100100,
(byte) 0b00011000,
(byte) 0b00011000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'V'
// Character 'W'
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10010001,
(byte) 0b10010001,
(byte) 0b10010001,
(byte) 0b10010001,
(byte) 0b10010001,
(byte) 0b10010001,
(byte) 0b01001010,
(byte) 0b01001010,
(byte) 0b00100100,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'W'
// Character 'X'
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10010001,
(byte) 0b10010001,
(byte) 0b01001010,
(byte) 0b00100100,
(byte) 0b00100100,
(byte) 0b01001010,
(byte) 0b10010001,
(byte) 0b10010001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'X'
// Character 'Y'
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10010001,
(byte) 0b10010001,
(byte) 0b01001010,
(byte) 0b01001010,
(byte) 0b00100100,
(byte) 0b00100100,
(byte) 0b00100100,
(byte) 0b00100100,
(byte) 0b00100100,
(byte) 0b00100100,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'Y'
// Character 'Z'
(byte) 0b11111111,
(byte) 0b00000001,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000100,
(byte) 0b00000100,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00100000,
(byte) 0b00100000,
(byte) 0b11111111,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character 'Z'
// Character '0'
(byte) 0b00011000,
(byte) 0b00100100,
(byte) 0b01000010,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b01000010,
(byte) 0b00100100,
(byte) 0b00011000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character '0'
// Character '1'
(byte) 0b00010000,
(byte) 0b00110000,
(byte) 0b01010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b11111111,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00000000, // Character '1'
// Character '2'
(byte) 0b00111100,
(byte) 0b01000010,
(byte) 0b10000001,
(byte) 0b00000001,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000100,
(byte) 0b00001000,
(byte) 0b00010000,
(byte) 0b00100000,
(byte) 0b01000000,
(byte) 0b11111111,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character '2'
// Character '3'
(byte) 0b00111100,
(byte) 0b01000010,
(byte) 0b10000001,
(byte) 0b00000001,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00011100,
(byte) 0b00000010,
(byte) 0b00000001,
(byte) 0b00000001,
(byte) 0b10000010,
(byte) 0b01111100,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character '3'
// Character '4'
(byte) 0b00000010,
(byte) 0b00000110,
(byte) 0b00001010,
(byte) 0b00010010,
(byte) 0b00100010,
(byte) 0b01000010,
(byte) 0b01111111,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character '4'
// Character '5'
(byte) 0b11111111,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b11111110,
(byte) 0b00000001,
(byte) 0b00000001,
(byte) 0b00000001,
(byte) 0b00000001,
(byte) 0b10000010,
(byte) 0b01111100,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character '5'
// Character '6'
(byte) 0b00111100,
(byte) 0b01000010,
(byte) 0b10000001,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b10000000,
(byte) 0b11111110,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b01000010,
(byte) 0b00111100,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character '6'
// Character '7'
(byte) 0b11111111,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000100,
(byte) 0b00000100,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00001000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00010000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character '7'
// Character '8'
(byte) 0b00111100,
(byte) 0b01000010,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b01000010,
(byte) 0b00111100,
(byte) 0b01000010,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b01000010,
(byte) 0b00111100,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character '8'
// Character '9'
(byte) 0b00111100,
(byte) 0b01000010,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b10000001,
(byte) 0b01000011,
(byte) 0b00111111,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000010,
(byte) 0b00000100,
(byte) 0b11111000,
(byte) 0b00000000,
(byte) 0b00000000,
(byte) 0b00000000, // Character '9'
};
// Write the text to the pixel data with rows rotated and characters scaled
for (int i = 0; i < text.length(); i++) {
char character = text.charAt(i);
int fontIndex = (int) character - 'A'; // Assuming your font starts from 'A'
if(Character.isDigit(character))
{
fontIndex += 43;
}
if (fontIndex >= 0 && fontIndex < font.length) {
for (int row = 15; row >= 0; row--) { // Iterate from highest to lowest rows
for (int col = 0; col < 8; col++) {
byte fontByte = font[fontIndex * 16 + row]; // Get the row in reverse order
byte mask = (byte) (1 << (7 - col));
if ((fontByte & mask) != 0) {
// Scale the character 2x horizontally
int pixelX = x + i * 16 + col * 2;
int pixelY = y + (15 - row); // Reverse the row index
int pixelIndex = pixelY * (width / 8) + (pixelX / 8);
int bitOffset = 7 - (pixelX % 8);
pixelData[pixelDataOffset + pixelIndex] |= (byte) (1 << bitOffset);
}
}
}
}
}
// Write the pixel data
fos.write(pixelData);
long end1 = System.currentTimeMillis();
System.out.println("Elapsed Time in milliseconds: "+ (end1-start1) + "ms");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment