1
2 package org.freehep.graphicsio.pdf;
3
4 import org.freehep.graphicsio.ImageConstants;
5 import org.freehep.graphicsio.ImageGraphics2D;
6
7 import java.awt.image.RenderedImage;
8 import java.awt.Color;
9 import java.io.IOException;
10
11
12
13
14
15
16
17
18
19
20
21 class ImageBytes {
22
23
24
25
26 private String format;
27
28
29
30
31 private byte[] bytes;
32
33
34
35
36
37
38
39
40
41
42 public ImageBytes(RenderedImage image, Color bkg, String format, String colorModel) throws IOException {
43
44
45 if (ImageConstants.ZLIB.equals(format) || (image.getColorModel().hasAlpha() && (bkg == null))) {
46 bytes = toZLIB(image, bkg, colorModel);
47 this.format = ImageConstants.ZLIB;
48 }
49
50
51 else if (ImageConstants.JPEG.equals(format)) {
52 bytes = toJPG(image);
53 this.format = ImageConstants.JPG;
54 } else {
55
56 byte[] jpgBytes = toJPG(image);
57 byte[] zlibBytes = toZLIB(image, bkg, colorModel);
58
59
60 if (jpgBytes.length < 0.5 * zlibBytes.length) {
61 bytes = jpgBytes;
62 this.format = ImageConstants.JPG;
63 } else {
64 bytes = zlibBytes;
65 this.format = ImageConstants.ZLIB;
66 }
67 }
68 }
69
70
71
72
73
74
75
76
77
78
79 private byte[] toZLIB(RenderedImage image, Color bkg, String colorModel) throws IOException {
80 return ImageGraphics2D.toByteArray(
81 image,
82 ImageConstants.RAW,
83 ImageConstants.ENCODING_FLATE_ASCII85,
84 ImageGraphics2D.getRAWProperties(bkg, colorModel));
85 }
86
87
88
89
90
91
92
93
94 private byte[] toJPG(RenderedImage image) throws IOException {
95 return ImageGraphics2D.toByteArray(
96 image,
97 ImageConstants.JPG,
98 ImageConstants.ENCODING_ASCII85,
99 null);
100 }
101
102
103
104
105 public byte[] getBytes() {
106 return bytes;
107 }
108
109
110
111
112 public String getFormat() {
113 return format;
114 }
115 }
116
117
118