Remove ZIP specific IBM437 decoder and make it generic.
[SquirrelJME.git] / tools / txt-to-pbm / src / main / java / dev / shadowtail / txttopbm / Main.java
blob02e1556bb28eca0ff9ccef72809fe0e8a5530722
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 package dev.shadowtail.txttopbm;
12 import java.io.BufferedReader;
13 import java.io.InputStreamReader;
14 import java.io.PrintStream;
15 import java.nio.file.Files;
16 import java.nio.file.Paths;
17 import java.nio.file.StandardOpenOption;
18 import java.util.Deque;
19 import java.util.LinkedList;
20 import javax.microedition.lcdui.Font;
21 import javax.microedition.lcdui.Graphics;
22 import javax.microedition.lcdui.Image;
24 /**
25 * This class is used to convert a text file to a PBM.
27 * @since 2019/06/15
29 public class Main
31 /**
32 * Main entry point.
34 * @param __args Arguments.
35 * @since 2019/06/15
37 public static final void main(String... __args)
38 throws Throwable
40 // Read in strings, find maximum length
41 int maxstrlen = 0,
42 numlines = 0;
43 Deque<String> lines = new LinkedList<>();
44 try (BufferedReader br = new BufferedReader(new InputStreamReader(
45 Files.newInputStream(Paths.get(__args[0]),
46 StandardOpenOption.READ))))
48 for (;;)
50 String ln = br.readLine();
52 if (ln == null)
53 break;
55 // Add line
56 lines.add(ln);
57 numlines++;
59 // Maximum line size
60 int strlen = ln.length();
61 if (strlen > maxstrlen)
62 maxstrlen = strlen;
66 // Write output graphics file, line by line!
67 try (PrintStream ps = new PrintStream(System.out))
69 // Get a large monospace font which is easy to read hopefully
70 Font font = Font.getFont(Font.FACE_MONOSPACE, 0, Font.SIZE_LARGE);
72 // Get the size properties of the font
73 int fw = (font.charWidth('a') + font.charWidth('w')) >> 1,
74 fh = font.getHeight();
76 // Calculate size of image, width is rounded to 8 because of
77 // the pixel packing!
78 int iw = ((maxstrlen * fw) + 7) & (~7),
79 ih = numlines * fh,
80 sa = iw * fh,
81 ua = sa / 8;
83 // Write PBM header, use P4 format
84 ps.print("P4\n");
85 ps.printf("%d %d\n", iw, ih);
87 // Create image to contain a buffer for a single line, it is not
88 // important to have an entire image because we can just pipe
89 // this out!
90 Image image = Image.createImage(iw, fh);
91 Graphics g = image.getGraphics();
93 // Use the monospace font!
94 g.setFont(font);
96 // RGB pixel output for lines
97 int[] rgb = new int[sa];
99 // Bulk byte data, for the fastest possible writing
100 byte[] bulk = new byte[ua];
102 // Used to keep track of the current column since PBM cannot exceed
103 // 72 characters
104 int col = 1;
106 // Process each line!
107 while (!lines.isEmpty())
109 // Read in line
110 String ln = lines.removeFirst();
112 // Clear background with white
113 g.setColor(0xFFFFFF);
114 g.fillRect(0, 0, iw, fh);
116 // Draw text in black
117 g.setColor(0x000000);
118 g.drawString(ln, 0, 0, Graphics.TOP | Graphics.LEFT);
120 // Get RGB pixel data
121 image.getRGB(rgb, 0, iw, 0, 0, iw, fh);
123 // Go through pixels and export to bulk format
124 for (int i = 0, o = 0; i < sa; i += 8, o++)
125 bulk[o] = (byte)(((rgb[i + 0] & 1) << 7) |
126 ((rgb[i + 1] & 1) << 6) |
127 ((rgb[i + 2] & 1) << 5) |
128 ((rgb[i + 3] & 1) << 4) |
129 ((rgb[i + 4] & 1) << 3) |
130 ((rgb[i + 5] & 1) << 2) |
131 ((rgb[i + 6] & 1) << 1) |
132 ((rgb[i + 7] & 1)));
134 // Write all bytes at once
135 ps.write(bulk);