KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > drawing > BlipStoreEntry


1 /*********************************************************************
2 *
3 * Copyright (C) 2003 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.biff.drawing;
21
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import common.Assert;
25 import jxl.biff.IntegerHelper;
26
27 /**
28  * The data for this blip store entry. Typically this is the raw image data
29  */

30 class BlipStoreEntry extends EscherAtom
31 {
32   /**
33    * The type of the blip
34    */

35   private BlipType type;
36
37   
38   /**
39    * The image data read in
40    */

41   private byte[] data;
42
43   /**
44    * The length of the image data
45    */

46   private int imageDataLength;
47
48   /**
49    * The reference count on this blip
50    */

51   private int referenceCount;
52
53   /**
54    * Flag to indicate that this entry was specified by the API, and not
55    * read in
56    */

57   private boolean write;
58
59   /**
60    * The start of the image data within this blip entry
61    */

62   private final static int IMAGE_DATA_OFFSET = 61;
63
64   /**
65    * Constructor
66    *
67    * @param erd
68    */

69   public BlipStoreEntry(EscherRecordData erd)
70   {
71     super(erd);
72     type = BlipType.getType(getInstance());
73     write = false;
74     byte[] bytes = getBytes();
75     referenceCount = IntegerHelper.getInt(bytes[24], bytes[25],
76                                            bytes[26], bytes[27]);
77   }
78
79   /**
80    * Constructor
81    *
82    * @param d the drawing
83    * @exception IOException
84    */

85   public BlipStoreEntry(Drawing d)
86     throws IOException JavaDoc
87   {
88     super(EscherRecordType.BSE);
89     type = BlipType.PNG;
90     setVersion(2);
91     setInstance(type.getValue());
92
93     byte[] imageData = d.getImageBytes();
94     imageDataLength = imageData.length;
95     data = new byte[imageDataLength + IMAGE_DATA_OFFSET];
96     System.arraycopy(imageData, 0, data, IMAGE_DATA_OFFSET, imageDataLength);
97     referenceCount = d.getReferenceCount();
98     write = true;
99   }
100
101   /**
102    * Accessor for the blip type
103    *
104    * @return
105    */

106   public BlipType getBlipType()
107   {
108     return type;
109   }
110
111   /**
112    * Gets the data for this blip so that it can be written out
113    *
114    * @return the data for the blip
115    */

116   public byte[] getData()
117   {
118     if (write)
119     {
120       // Drawing has been specified by API
121

122       // Type on win32
123
data[0] = (byte) type.getValue();
124
125       // Type on MacOs
126
data[1] = (byte) type.getValue();
127
128       // The blip identifier
129
// IntegerHelper.getTwoBytes(0xfce1, data, 2);
130

131       // Unused tags - 18 bytes
132
// System.arraycopy(stuff, 0, data, 2, stuff.length);
133

134       // The size of the file
135
IntegerHelper.getFourBytes(imageDataLength + 8 + 17, data, 20);
136
137       // The reference count on the blip
138
IntegerHelper.getFourBytes(referenceCount, data, 24);
139
140       // Offset in the delay stream
141
IntegerHelper.getFourBytes(0, data, 28);
142
143       // Usage byte
144
data[32] = (byte) 0;
145
146       // Length of the blip name
147
data[33] = (byte) 0;
148
149       // Last two bytes unused
150
data[34] = (byte) 0x7e;
151       data[35] = (byte) 0x01;
152
153       // The blip itself
154
data[36] = (byte) 0;
155       data[37] = (byte) 0x6e;
156     
157       // The blip identifier
158
IntegerHelper.getTwoBytes(0xf01e, data, 38);
159
160       // The length of the blip. This is the length of the image file plus
161
// 16 bytes
162
IntegerHelper.getFourBytes(imageDataLength + 17, data, 40);
163
164       // Unknown stuff
165
// System.arraycopy(stuff, 0, data, 44, stuff.length);
166
}
167     else
168     {
169       // drawing has been read in
170
data = getBytes();
171     }
172    
173     return setHeaderData(data);
174   }
175
176   /**
177    * Reduces the reference count in this blip. Called when a drawing is
178    * removed
179    */

180   void dereference()
181   {
182     referenceCount--;
183     Assert.verify(referenceCount >= 0);
184   }
185
186   /**
187    * Accessor for the reference count on the blip
188    *
189    * @return the reference count on the blip
190    */

191   int getReferenceCount()
192   {
193     return referenceCount;
194   }
195
196   /**
197    * Accessor for the image data.
198    *
199    * @return the image data
200    */

201   byte[] getImageData()
202   {
203     byte[] allData = getBytes();
204     byte[] imageData = new byte[allData.length - IMAGE_DATA_OFFSET];
205     System.arraycopy(allData, IMAGE_DATA_OFFSET,
206                      imageData, 0, imageData.length);
207     return imageData;
208   }
209 }
210
Popular Tags