Advertisement
Guest User

Untitled

a guest
Jun 24th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import javax.imageio.ImageIO;
  6.  
  7. public class ResImageConvertor {
  8.     /** Converts and save Images from the TI-Nspire OS to TI.Image used by the Lua API of the
  9.      ** TI-Nspire framework. Also can saves or load a PNG image.
  10.      ** Those images are composed of a header part (20 bytes) and a sequence of pixels
  11.      ** each coded on 2 bytes (RRRRRGGG-GGGBBBBB). Then, comes the alpha channel where
  12.      ** each pixel is coded on 1 byte.
  13.      *
  14.      ** How to use : java ResImageConvertor output_image_path sequence of bytes
  15.      *
  16.      ** 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 ....]
  17.      * Levak 24nd June 2012 */
  18.     public static int string2char(String s) {
  19.         int a = 0;
  20.         String b = s;
  21.         for(int j = 0; j < b.length(); ++j) {      
  22.             String c = b.substring(j, j+1);
  23.             a <<= 4;
  24.             try { a += Integer.parseInt(c); }
  25.             catch(Exception err) { a += 10 + (int)(c.toLowerCase().charAt(0)) - (int)'a'; }
  26.         }
  27.         return a;
  28.     }
  29.    
  30.     public static int readNbytesInt(String[] args, int o, int s) {
  31.         int a = 0;
  32.         int m = 0;
  33.         for(int i = o; i < o + s; a += string2char(args[i]) << m, m += 8, i++);
  34.         return a;
  35.     }
  36.    
  37.     public static void writeNbytesInt(int n, int s) {
  38.         byte a[] = new byte[s];
  39.         for(int i = 0; i < s; a[i] = (byte) (n&0xFF), n >>= 8, i++);
  40.         System.out.write(a, 0, s);
  41.     }
  42.    
  43.     public static void output(String[] args) throws IOException {
  44.         // header
  45.         int i = 2;
  46.         int w = readNbytesInt(args, i, 4);
  47.         i += 4;
  48.         int h = readNbytesInt(args, i, 4);
  49.         i += 4;
  50.         readNbytesInt(args, i, 4); // unused : empty
  51.         i += 4;
  52.         readNbytesInt(args, i, 4); // unused : buffer size
  53.         i += 4;
  54.         readNbytesInt(args, i, 2); // unused : image depth
  55.         i += 2;
  56.         readNbytesInt(args, i, 2); // unused : unknown (1)
  57.         i += 2;
  58.        
  59.         BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
  60.         int bufferSize = w*h;
  61.         int headerSize = 20;
  62.  
  63.         for(i = 2; i < headerSize+2; i++)
  64.             System.out.printf("\\%03d", string2char(args[i]));
  65.        
  66.         // RGB sequence + Alpha Map
  67.         for(i = 0; i < bufferSize; i++) {
  68.             int b1 = string2char(args[2+headerSize+i*2]);
  69.             int b2 = string2char(args[2+headerSize+i*2+1]);
  70.             int c = (b2 << 8) + b1;
  71.             int r = (c >> 11) << 3;
  72.             int g = (c >> 5 & 0x3F) << 2;
  73.             int b = (c & 0x1F) << 3;
  74.             int alpha = string2char(args[2+headerSize+bufferSize*2+i]);
  75.             int color = new Color(r, g, b, alpha).getRGB();
  76.             image.setRGB(i%w, i/w, color);
  77.             int bb1 = (alpha & 0x80) | ((r >> 1) & 0x7C) | (g >> 6);
  78.             int bb2 = ((g << 2) & 0xE0) | (b >> 3);
  79.             System.out.printf("\\%03d\\%03d", bb2, bb1);
  80.         }
  81.         File file = new File(args[1]);
  82.         ImageIO.write(image, "PNG", file);
  83.         System.out.println();
  84.     }
  85.    
  86.     public static void input(String[] args) throws IOException {
  87.         File file = new File(args[1]);
  88.         BufferedImage image = ImageIO.read(file);
  89.         int w = image.getWidth();
  90.         int h = image.getHeight();
  91.        
  92.         // Header
  93.         writeNbytesInt(w, 4);
  94.         writeNbytesInt(h, 4);
  95.         writeNbytesInt(0, 4);
  96.         writeNbytesInt(w*2, 4);
  97.         writeNbytesInt(16, 2);
  98.         writeNbytesInt(1, 2);
  99.        
  100.         // RGB sequence
  101.         for(int j = 0; j < h; j++) {
  102.             for(int i = 0; i < w; i++) {
  103.                 Color c = new Color(image.getRGB(i, j), true);
  104.                 int b = ((c.getRed() & 0xF8) << 8) | ((c.getGreen() & 0xFC) << 3) | ((c.getBlue() & 0xF8) >> 3);
  105.                 writeNbytesInt(b, 2);
  106.             }
  107.         }
  108.  
  109.         // Alpha sequence
  110.         for(int j = 0; j < h; j++) {
  111.             for(int i = 0; i < w; i++) {
  112.                 Color c = new Color(image.getRGB(i, j), true);
  113.                 writeNbytesInt(c.getAlpha(), 1) ;
  114.             }
  115.         }
  116.     }
  117.    
  118.     public static void main(String[] args) throws IOException {
  119.         if(args[0].equals("-O"))
  120.             output(args);
  121.         else if (args[0].equals("-I"))
  122.             input(args);
  123.         else
  124.             System.out.println("usage : java ResImageConvertor -[O|I] ./image.png [10 00 00 00 10 00 00 00 <TI.Image> ....] ");
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement