1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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
;
25 * This class is used to convert a text file to a PBM.
34 * @param __args Arguments.
37 public static final void main(String
... __args
)
40 // Read in strings, find maximum length
43 Deque
<String
> lines
= new LinkedList
<>();
44 try (BufferedReader br
= new BufferedReader(new InputStreamReader(
45 Files
.newInputStream(Paths
.get(__args
[0]),
46 StandardOpenOption
.READ
))))
50 String ln
= br
.readLine();
60 int strlen
= ln
.length();
61 if (strlen
> maxstrlen
)
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
78 int iw
= ((maxstrlen
* fw
) + 7) & (~
7),
83 // Write PBM header, use P4 format
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
90 Image image
= Image
.createImage(iw
, fh
);
91 Graphics g
= image
.getGraphics();
93 // Use the monospace 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
106 // Process each line!
107 while (!lines
.isEmpty())
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) |
134 // Write all bytes at once