Source code: jpl2/documents/MBMDocument.java
1 /***********************************************************************
2 * JavaPsionLink 2.0, a java implementation of the psion link protocol
3 * Copyright (C) 2002, 2003 John S Montgomery (john.montgomery@lineone.net)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ************************************************************************/
19 package jpl2.documents;
20
21 import jpl2.common.*;
22 import jpl2.convert.ImageWriter;
23 import jpl2.documents.sections.*;
24
25 import java.io.*;
26 import java.awt.Image;
27
28 /** A Psion MBM (Multiple BitMap) document.
29 **/
30 public class MBMDocument extends PsionDocument {
31 /** Header section layout (first four bytes). **/
32 public static final int UID1 = 0x10000037;
33 /** File Kind (second four bytes). **/
34 public static final int UID2 = 0x10000042;
35 /** Application ID (third four bytes). **/
36 public static final int UID3 = 0x00000000;
37 /** Checksum of other UIDs (fourth four bytes). **/
38 public static final int UID4 = 0x47396439;
39
40 private Image[] images = null;
41 private PaintDataSection[] paintDataSections = null;
42
43 /** Read an MBM document from the supplied data.
44 **/
45 public MBMDocument( Data data ) throws IOException {
46 super( data, UID1, UID2, UID3, UID4 );
47
48 int mbmJumpTableOffset = data.readInt();
49
50 data.setOffset( mbmJumpTableOffset );
51
52 int tableLen = data.readInt();
53
54 paintDataSections = new PaintDataSection[ tableLen ];
55 images = new Image[ tableLen ];
56
57 for ( int i = 0; i < tableLen; i++ ) {
58 int sectionOffset = data.readInt();
59 // store current offset
60 int offset = data.getOffset();
61 // move to offset of section
62 data.setOffset( sectionOffset );
63 PaintDataSection section = new PaintDataSection( data );
64 images[ i ] = section.getImage();
65 paintDataSections[ i ] = section;
66 // reset position to where we were
67 data.setOffset( offset );
68 }
69 }
70
71 public Image[] getImages() {
72 return images;
73 }
74
75 public boolean convertsToFolder( String fileFormat ) {
76 return getImages().length > 1;
77 }
78
79 public void convertDocument( String fileFormat, OutputStream out ) throws IOException {
80 if ( fileFormat.equals( "gif" ) ) {
81 Image[] images = getImages();
82 if ( images.length > 0 )
83 ImageWriter.writeGIF( images[ 0 ], out );
84 }
85 }
86
87 public void convertDocument( String fileFormat, File folder ) throws IOException {
88 if ( fileFormat.equals( "gif" ) ) {
89 Image[] images = getImages();
90 for ( int i = 0; i < images.length; i++ ) {
91 Image image = images[ i ];
92 File file = new File( folder, "image" + i + ".gif" );
93 OutputStream out = new BufferedOutputStream( new FileOutputStream( file ) );
94 ImageWriter.writeGIF( images[ 0 ], out );
95 }
96 }
97 }
98
99 }