Skip to content
Navigation Menu
{{ message }}
forked from kovzol/Java-Geometry-Expert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFImage.java
More file actions
409 lines (357 loc) · 12 KB
/
Copy pathPDFImage.java
File metadata and controls
409 lines (357 loc) · 12 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* $Id: PDFImage.java,v 1.2 2007/08/26 18:56:35 gil1 Exp $
*
* $Date: 2007/08/26 18:56:35 $
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package pdf;
import java.awt.Image;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
/**
* <p>This implements the Image XObject. Calling one of the
* <code>drawImage</code> methods of <code>PDFGraphics</code> will
* put all the necessary code into the pdf file, and the image will
* be encoded in ascii base 85, then deflated in zip format.</p>
*
* @author Eric Z. Beard (original version by Peter Mount)
* @author Matthew Hreljac, mhreljac@hotmail.com
* @version $Revision: 1.2 $, $Date: 2007/08/26 18:56:35 $
*/
public class PDFImage extends PDFStream implements ImageObserver, Serializable
{
/*
* NOTE: The original class is the work of Peter T. Mount, who released it
* in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as
* follows:
* The package name was changed to gnu.jpdf.
* The formatting was changed a little bit.
* Images were not yet implemented, so the core of this
* class was mostly rewritten
* It is still licensed under the LGPL.
* Got some help with base85 methods from Mathew Hreljac
*/
// Dimensions of the object.
private int objwidth;
private int objheight;
// Dimensions of the image.
private int width;
private int height;
private Image img;
private String name;
/**
* Creates a new <code>PDFImage</code> instance.
*
*/
public PDFImage()
{
super("/XObject");
}
/**
* Creates a new <code>PDFImage</code> instance.
*
* @param img an <code>Image</code> value
*/
public PDFImage(Image img) {
this();
setImage(img, 0, 0, img.getWidth(this), img.getHeight(this), this);
}
/**
* Creates a new <code>PDFImage</code> instance.
*
* @param img an <code>Image</code> value
* @param x an <code>int</code> value
* @param y an <code>int</code> value
* @param w an <code>int</code> value
* @param h an <code>int</code> value
* @param obs an <code>ImageObserver</code> value
*/
public PDFImage(Image img,int x,int y,int w,int h,ImageObserver obs) {
this();
objwidth = w;
objheight = h;
setImage(img, x, y, img.getWidth(this), img.getHeight(this), obs);
}
/**
* Get the value of width.
* @return value of width.
*/
public int getWidth() {
return width;
}
/**
* Set the value of width.
* @param v Value to assign to width.
*/
public void setWidth(int v) {
this.width = v;
}
/**
* Get the value of height.
* @return value of height.
*/
public int getHeight() {
return height;
}
/**
* Set the value of height.
* @param v Value to assign to height.
*/
public void setHeight(int v) {
this.height = v;
}
/**
* Set the name
*
* @param n a <code>String</code> value
*/
public void setName(String n) {
name = n;
}
/**
* Get the name
*
* @return a <code>String</code> value
*/
public String getName() {
return name;
}
/**
* Set the image
*
* @param img an <code>Image</code> value
* @param x an <code>int</code> value
* @param y an <code>int</code> value
* @param w an <code>int</code> value
* @param h an <code>int</code> value
* @param obs an <code>ImageObserver</code> value
*/
public void setImage(Image img,int x,int y,int w,int h,ImageObserver obs) {
this.img = img;
width = w;
height = h;
}
/**
* <p>Adobe's base 85 does not follow the format used by ipv6
* addresses. It simply starts with 33 and goes straight up without
* skipping any characters</p>
*
* <p>Parts of this method contributed by Mathew Hreljac</p>
*
* @param stringToEncode a <code>String</code> value
* @return a <code>String</code> value
*/
private String base85Encoding(String stringToEncode)
throws NumberFormatException {
if ((stringToEncode == null) || (stringToEncode.length() == 0)) {
//System.out.println("PDFImage.base85Encoding() null or blank String");
return "";
}
if ((stringToEncode.length() > 8) ||
((stringToEncode.length() % 2) != 0)) {
System.out.println("PDFImage.base85Encoding, Incorrect tuple length: " +
stringToEncode.length());
return "";
}
//System.out.println("str: " + stringToEncode);
// String buffer to use to return the String encoding
StringBuffer sb = new StringBuffer();
// Deal with a partial tuple (less than 8 hex digits)
// From Adobe's docs:
// "Given n (1, 2 or 3) bytes of binary data, the encoding first
// appends 4 - n zero bytes to make a complete 4-tuple. This 4-tuple
// is encoded in the usual way, but without applying the special
// z-case. Finally, only the first n+1 characters of the resulting
// 5-tuple are written out. Those characters are immediately followed
// by the EOD marker, ~>"
int numHexDigits = stringToEncode.length() / 2;
int numAppendBytes = 4 - numHexDigits;
for (int i = 0; i < numAppendBytes; i++) {
stringToEncode += "00";
}
Vector<Integer> digitVector = new Vector<Integer>();
long number = Long.parseLong(stringToEncode, 16);
int remainder = 0;
while (number >= 85) {
remainder = (int) (number % 85);
number = number / 85;
digitVector.add( 0, new Integer( remainder ) );
}
digitVector.add( 0, new Integer( (int)number ) );
for ( int i = 0; i < digitVector.size(); i++) {
char c = (char) (((Integer)digitVector.elementAt(i)).intValue() + 33);
sb.append(c);
}
String tuple = sb.toString();
int len = tuple.length();
switch (len) {
case 1: tuple = "!!!!" + tuple; break;
case 2: tuple = "!!!" + tuple; break;
case 3: tuple = "!!" + tuple; break;
case 4: tuple = "!" + tuple; break;
default: break;
} // end switch
//System.out.println("enc tuple: " + tuple);
return (tuple);
} // end base85encoding
/**
* Writes the image to the stream
*
* @param os an <code>OutputStream</code> value
* @exception IOException if an error occurs
*/
public void writeStream(OutputStream os) throws IOException {
// This is a non-deflated stream
/*
os.write("/Length ".getBytes());
// Accout for stream\n ... >\nendstream
os.write(Integer.toString(buf.size() + 18).getBytes());
os.write("\n/Filter /ASCII85Decode".getBytes());
os.write("\n>>\nstream\n".getBytes());
buf.writeTo(os);
os.write(">\nendstream\nendobj\n\n".getBytes());
*/
ByteArrayOutputStream b = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(b);
buf.writeTo(dos);
dos.finish();
dos.close();
// FlatDecode is compatible with the java.util.zip.Deflater class
//os.write("/Filter [/FlateDecode /ASCIIHexDecode]\n".getBytes());
os.write("/Filter [/FlateDecode /ASCII85Decode]\n".getBytes());
os.write("/Length ".getBytes());
os.write(Integer.toString(b.size()).getBytes());
os.write("\n>>\nstream\n".getBytes());
b.writeTo(os);
os.write("\nendstream\nendobj\n".getBytes());
} // end writeStream
/**
* <p>Compression needs to be improved here</p>
*
* @param os OutputStream to send the object to
* @exception IOException on error
*/
public void write(OutputStream os) throws IOException
{
writeStart(os);
// write the extra details
os.write("/Subtype /Image\n/Name ".getBytes());
os.write(name.getBytes());
os.write("\n/Width ".getBytes());
os.write(Integer.toString(width).getBytes());
os.write("\n/Height ".getBytes());
os.write(Integer.toString(height).getBytes());
os.write("\n/BitsPerComponent 8\n/ColorSpace /DeviceRGB\n".getBytes());
// write the pixels to the stream
//System.err.println("Processing image "+width+"x"+height+" pixels");
ByteArrayOutputStream bos = getStream();
int w = width;
int h = height;
int x = 0;
int y = 0;
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return;
}
StringBuffer out = new StringBuffer();
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
//System.out.print("p[" + j * w + i+ "]=" + pixels[j * w + i] + ".");
out.append(handlePixel(x+i, y+j, pixels[j * w + i]));
if (out.toString().length() >= 8) {
String tuple = out.substring(0, 8);
out.delete(0, 8);
// Convert !!!!! to 'z'
String encTuple = base85Encoding(tuple);
if (encTuple.equals("!!!!!")) {
encTuple = "z";
}
bos.write(encTuple.getBytes());
}
}
}
// This should be the only partial tuple case,
String lastTuple = base85Encoding(out.toString());
//System.out.println("lastTuple: " + lastTuple);
bos.write(lastTuple.getBytes());
bos.write("~".getBytes());
//System.out.println("Processing done");
// this will write the actual stream
setDeflate(false);
writeStream(os);
// Note: we do not call writeEnd() on streams!
}
/**
* <p>Converts a pixel to a hex string</p>
*
* @param x an <code>int</code> value
* @param y an <code>int</code> value
* @param p an <code>int</code> value
* @return a <code>String</code> value
*/
public static String handlePixel(int x, int y, int p) {
int alpha = (p >> 24) & 0xff;
int red = (p >> 16) & 0xff;
int green = (p >> 8) & 0xff;
int blue = (p ) & 0xff;
String redHex = Integer.toHexString(red);
String greenHex = Integer.toHexString(green);
String blueHex = Integer.toHexString(blue);
if (redHex.length() == 1) {
redHex = "0" + redHex;
}
if (greenHex.length() == 1) {
greenHex = "0" + greenHex;
}
if (blueHex.length() == 1) {
blueHex = "0" + blueHex;
}
return redHex + greenHex + blueHex;
} // end handlePixel
/**
* Describe <code>imageUpdate</code> method here.
*
* @param img an <code>Image</code> value
* @param infoflags an <code>int</code> value
* @param x an <code>int</code> value
* @param y an <code>int</code> value
* @param w an <code>int</code> value
* @param h an <code>int</code> value
* @return a <code>boolean</code> value
*/
public boolean imageUpdate(Image img,int infoflags,int x,int y,int w,int h) {
System.err.println("img="+img+"\ninfoflags="+infoflags+
"\nx="+x+" y="+y+" w="+w+" h="+h);
//if(img == this.img) {
if(infoflags==ImageObserver.WIDTH)
width = w;
if(infoflags==ImageObserver.HEIGHT)
height = h;
//return true;
//}
return false;
}
} // end class PDFImage
You can’t perform that action at this time.
