Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class ResImageConvertor {
- /** Converts and save Images from the TI-Nspire OS to TI.Image used by the Lua API of the
- ** TI-Nspire framework. Also can saves or load a PNG image.
- ** Those images are composed of a header part (20 bytes) and a sequence of pixels
- ** each coded on 2 bytes (RRRRRGGG-GGGBBBBB). Then, comes the alpha channel where
- ** each pixel is coded on 1 byte.
- *
- ** How to use : java ResImageConvertor output_image_path sequence of bytes
- *
- ** Example : java ResImageConvertor -[O|I] ./image.png [10 00 00 00 10 00 00 00 00 00 00 00 20 00 00 00 10 00 01 00 b0 5b b0 5b b0 5b b0 5b b0 5b b0 5b b0 5b ff ff ff ff ff ff ....]
- * Levak 24nd June 2012 */
- public static int string2char(String s) {
- int a = 0;
- String b = s;
- for(int j = 0; j < b.length(); ++j) {
- String c = b.substring(j, j+1);
- a <<= 4;
- try { a += Integer.parseInt(c); }
- catch(Exception err) { a += 10 + (int)(c.toLowerCase().charAt(0)) - (int)'a'; }
- }
- return a;
- }
- public static int readNbytesInt(String[] args, int o, int s) {
- int a = 0;
- int m = 0;
- for(int i = o; i < o + s; a += string2char(args[i]) << m, m += 8, i++);
- return a;
- }
- public static void writeNbytesInt(int n, int s) {
- byte a[] = new byte[s];
- for(int i = 0; i < s; a[i] = (byte) (n&0xFF), n >>= 8, i++);
- System.out.write(a, 0, s);
- }
- public static void output(String[] args) throws IOException {
- // header
- int i = 2;
- int w = readNbytesInt(args, i, 4);
- i += 4;
- int h = readNbytesInt(args, i, 4);
- i += 4;
- readNbytesInt(args, i, 4); // unused : empty
- i += 4;
- readNbytesInt(args, i, 4); // unused : buffer size
- i += 4;
- readNbytesInt(args, i, 2); // unused : image depth
- i += 2;
- readNbytesInt(args, i, 2); // unused : unknown (1)
- i += 2;
- BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
- int bufferSize = w*h;
- int headerSize = 20;
- for(i = 2; i < headerSize+2; i++)
- System.out.printf("\\%03d", string2char(args[i]));
- // RGB sequence + Alpha Map
- for(i = 0; i < bufferSize; i++) {
- int b1 = string2char(args[2+headerSize+i*2]);
- int b2 = string2char(args[2+headerSize+i*2+1]);
- int c = (b2 << 8) + b1;
- int r = (c >> 11) << 3;
- int g = (c >> 5 & 0x3F) << 2;
- int b = (c & 0x1F) << 3;
- int alpha = string2char(args[2+headerSize+bufferSize*2+i]);
- int color = new Color(r, g, b, alpha).getRGB();
- image.setRGB(i%w, i/w, color);
- int bb1 = (alpha & 0x80) | ((r >> 1) & 0x7C) | (g >> 6);
- int bb2 = ((g << 2) & 0xE0) | (b >> 3);
- System.out.printf("\\%03d\\%03d", bb2, bb1);
- }
- File file = new File(args[1]);
- ImageIO.write(image, "PNG", file);
- System.out.println();
- }
- public static void input(String[] args) throws IOException {
- File file = new File(args[1]);
- BufferedImage image = ImageIO.read(file);
- int w = image.getWidth();
- int h = image.getHeight();
- // Header
- writeNbytesInt(w, 4);
- writeNbytesInt(h, 4);
- writeNbytesInt(0, 4);
- writeNbytesInt(w*2, 4);
- writeNbytesInt(16, 2);
- writeNbytesInt(1, 2);
- // RGB sequence
- for(int j = 0; j < h; j++) {
- for(int i = 0; i < w; i++) {
- Color c = new Color(image.getRGB(i, j), true);
- int b = ((c.getRed() & 0xF8) << 8) | ((c.getGreen() & 0xFC) << 3) | ((c.getBlue() & 0xF8) >> 3);
- writeNbytesInt(b, 2);
- }
- }
- // Alpha sequence
- for(int j = 0; j < h; j++) {
- for(int i = 0; i < w; i++) {
- Color c = new Color(image.getRGB(i, j), true);
- writeNbytesInt(c.getAlpha(), 1) ;
- }
- }
- }
- public static void main(String[] args) throws IOException {
- if(args[0].equals("-O"))
- output(args);
- else if (args[0].equals("-I"))
- input(args);
- else
- System.out.println("usage : java ResImageConvertor -[O|I] ./image.png [10 00 00 00 10 00 00 00 <TI.Image> ....] ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement