Check-in [2c52c8ba0e]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add utility to export to PNM.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:2c52c8ba0e8044913b54ca38130209fb70b3f842
User & Date: stephanie.gawroriski 2019-06-16 04:11:08
Context
2019-06-16
04:22
Correct Text.getText(int, int) when requesting characters from an empty Text. check-in: dd6d38fc9d user: stephanie.gawroriski tags: trunk
04:11
Add utility to export to PNM. check-in: 2c52c8ba0e user: stephanie.gawroriski tags: trunk
2019-06-15
20:40
Add memset() system call to possibly and more quickly wipe memory. check-in: d13dde95bf user: stephanie.gawroriski tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added bldt/mids/txt-to-pbm/META-INF/MANIFEST.MF.





















>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
Manifest-Version: 1.0
X-SquirrelJME-UUID: a0ad1c6a-7826-4f5e-b1f0-e252d9c8dbab
X-SquirrelJME-Error: BN
X-SquirrelJME-Name: txt-to-pbm
X-SquirrelJME-Vendor: Stephanie Gawroriski
X-SquirrelJME-Version: 0.3.0
X-SquirrelJME-Description: txt-to-pbm
X-SquirrelJME-Depends: midp-lcdui
Microedition-Configuration: CLDC-1.8
Main-Class: dev.shadowtail.txttopbm.Main

Added bldt/mids/txt-to-pbm/dev/shadowtail/txttopbm/Main.java.





























































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
// ---------------------------------------------------------------------------
// Multi-Phasic Applications: SquirrelJME
//     Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------

package dev.shadowtail.txttopbm;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;

/**
 * This class is used to convert a text file to a PBM.
 *
 * @since 2019/06/15
 */
public class Main
{
	/** Output column limit for PBM. */
	public static final int PBM_COLUMNS =
		72;
	
	/**
	 * Main entry point.
	 *
	 * @param __args Arguments.
	 * @since 2019/06/15
	 */
	public static final void main(String... __args)
		throws Throwable
	{
		// Read in strings, find maximum length
		int maxstrlen = 0,
			numlines = 0;
		Deque<String> lines = new LinkedList<>();
		try (BufferedReader br = new BufferedReader(new InputStreamReader(
			Files.newInputStream(Paths.get(__args[0]),
			StandardOpenOption.READ))))
		{
			for (;;)
			{
				String ln = br.readLine();
				
				if (ln == null)
					break;
				
				// Add line
				lines.add(ln);
				numlines++;
				
				// Maximum line size
				int strlen = ln.length();
				if (strlen > maxstrlen)
					maxstrlen = strlen;
			}
		}
		
		// Write output graphics file, line by line!
		try (PrintStream ps = new PrintStream(System.out))
		{
			// Get a large monospace font which is easy to read hopefully
			Font font = Font.getFont(Font.FACE_MONOSPACE, 0, Font.SIZE_LARGE);
			
			// Get the size properties of the font
			int fw = (font.charWidth('a') + font.charWidth('w')) >> 1,
				fh = font.getHeight();
				
			// Calculate size of image
			int iw = maxstrlen * fw,
				ih = numlines * fh,
				sa = iw * fh;
			
			// Write PBM header
			ps.printf("P1\n");
			ps.printf("%d %d\n", iw, ih);
			
			// Create image to contain a buffer for a single line, it is not
			// important to have an entire image because we can just pipe
			// this out!
			Image image = Image.createImage(iw, fh);
			Graphics g = image.getGraphics();
			
			// RGB pixel output for lines
			int[] rgb = new int[sa];
			
			// Used to keep track of the current column since PBM cannot exceed
			// 72 characters
			int col = 1;
			
			// Process each line!
			while (!lines.isEmpty())
			{
				// Read in line
				String ln = lines.removeFirst();
				
				// Clear background with white
				g.setColor(0xFFFFFF);
				g.fillRect(0, 0, iw, fh);
				
				// Draw text in black
				g.setColor(0x000000);
				g.drawString(ln, 0, 0, Graphics.TOP | Graphics.LEFT);
				
				// Get RGB pixel data
				image.getRGB(rgb, 0, iw, 0, 0, iw, fh);
				
				// Go through pixels and export
				for (int i = 0; i < sa; i++)
				{
					// Print either off or on, note that the values are as
					// if they were truncated hex values!
					int v = rgb[i];
					ps.print(((v & 0xFFFFFF) == 0) ? '0' : '1');
					
					// End column?
					if ((col++) == PBM_COLUMNS)
					{
						ps.print('\n');
						col = 1;
					}
				}
			}
		}
	}
}

Added bldt/mids/txt-to-pbm/dev/shadowtail/txttopbm/package-info.java.



































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
// ---------------------------------------------------------------------------
// Multi-Phasic Applications: SquirrelJME
//     Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------

/**
 * This package contains the utility to convert a text file to a PBM.
 *
 * @since 2019/06/15
 */

package dev.shadowtail.txttopbm;