17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.openoffice.spreadsheet;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.openoffice.ODFrame;
|
|
|
17 |
import org.openconcerto.utils.ImageInfo;
|
|
|
18 |
import org.openconcerto.utils.ImageUtils;
|
|
|
19 |
|
|
|
20 |
import java.awt.Color;
|
|
|
21 |
import java.awt.Image;
|
|
|
22 |
import java.awt.image.BufferedImage;
|
|
|
23 |
import java.io.ByteArrayInputStream;
|
|
|
24 |
import java.io.ByteArrayOutputStream;
|
|
|
25 |
import java.io.IOException;
|
|
|
26 |
|
|
|
27 |
import javax.imageio.ImageIO;
|
|
|
28 |
|
|
|
29 |
import org.jdom.Element;
|
|
|
30 |
|
180 |
ilm |
31 |
public abstract class BytesProducer {
|
17 |
ilm |
32 |
|
|
|
33 |
/**
|
|
|
34 |
* The data of an image to put in <code>frame</code>.
|
|
|
35 |
*
|
|
|
36 |
* @param frame the frame where this image will be put.
|
|
|
37 |
* @return the corresponding bytes.
|
|
|
38 |
*/
|
180 |
ilm |
39 |
public abstract byte[] getBytes(ODFrame<?> frame);
|
17 |
ilm |
40 |
|
|
|
41 |
/**
|
|
|
42 |
* The format of the data returned by {@link #getBytes(Element)}.
|
|
|
43 |
*
|
|
|
44 |
* @return the name of the format, <code>null</code> if unknown, eg "png".
|
|
|
45 |
*/
|
180 |
ilm |
46 |
public abstract String getFormat();
|
17 |
ilm |
47 |
|
|
|
48 |
// *** concrete subclasses
|
|
|
49 |
|
|
|
50 |
// a no-op Producer
|
180 |
ilm |
51 |
static public final class ByteArrayProducer extends BytesProducer {
|
17 |
ilm |
52 |
|
|
|
53 |
private final byte[] data;
|
|
|
54 |
private final boolean keepRatio;
|
|
|
55 |
|
|
|
56 |
public ByteArrayProducer(byte[] data) {
|
|
|
57 |
this(data, false);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public ByteArrayProducer(byte[] data, boolean keepRatio) {
|
|
|
61 |
super();
|
|
|
62 |
this.data = data;
|
|
|
63 |
this.keepRatio = keepRatio;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
@Override
|
|
|
67 |
public byte[] getBytes(final ODFrame<?> frame) {
|
|
|
68 |
if (this.keepRatio) {
|
|
|
69 |
final ImageInfo info = new ImageInfo();
|
|
|
70 |
info.setInput(new ByteArrayInputStream(this.data));
|
|
|
71 |
if (!info.check())
|
|
|
72 |
throw new IllegalStateException("unable to parse the picture");
|
|
|
73 |
final double imgRatio = info.getWidth() / (double) info.getHeight();
|
|
|
74 |
|
|
|
75 |
final double ratio = frame.getRatio();
|
|
|
76 |
|
|
|
77 |
// svg:x="0.075cm" svg:y="0.343cm"
|
|
|
78 |
if (imgRatio > ratio) {
|
|
|
79 |
final double newFrameHeight = frame.getWidth().doubleValue() / imgRatio;
|
|
|
80 |
final double diff = frame.getHeight().doubleValue() - newFrameHeight;
|
|
|
81 |
frame.setSVGAttr("y", frame.getY().doubleValue() + diff / 2.0d);
|
|
|
82 |
frame.setSVGAttr("height", newFrameHeight);
|
|
|
83 |
} else {
|
|
|
84 |
final double newFrameWidth = frame.getHeight().doubleValue() * imgRatio;
|
|
|
85 |
final double diff = frame.getWidth().doubleValue() - newFrameWidth;
|
|
|
86 |
frame.setSVGAttr("x", frame.getX().doubleValue() + diff / 2.0d);
|
|
|
87 |
frame.setSVGAttr("width", newFrameWidth);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
// table:end-cell-address="Feuille1.F52" table:end-x="2.247cm" table:end-y="0.066cm"
|
|
|
91 |
final Element frameElem = frame.getElement();
|
|
|
92 |
frameElem.removeAttribute("end-cell-address", frameElem.getNamespace("table"));
|
|
|
93 |
frameElem.removeAttribute("end-x", frameElem.getNamespace("table"));
|
|
|
94 |
frameElem.removeAttribute("end-y", frameElem.getNamespace("table"));
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
return this.data;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
@Override
|
|
|
101 |
public String getFormat() {
|
|
|
102 |
return null;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
// will generate a new png image (and can also keep ratio)
|
180 |
ilm |
107 |
static public final class ImageProducer extends BytesProducer {
|
17 |
ilm |
108 |
|
|
|
109 |
private final Image img;
|
|
|
110 |
private final boolean keepRatio;
|
|
|
111 |
|
|
|
112 |
public ImageProducer(Image img, boolean keepRatio) {
|
|
|
113 |
super();
|
|
|
114 |
this.img = img;
|
|
|
115 |
this.keepRatio = keepRatio;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
@Override
|
|
|
119 |
public byte[] getBytes(final ODFrame<?> frame) {
|
|
|
120 |
final BufferedImage bImg;
|
|
|
121 |
if (this.keepRatio) {
|
|
|
122 |
final float ratio = (float) frame.getRatio();
|
|
|
123 |
bImg = ImageUtils.createQualityResizedImage(this.img, ratio, true, Color.WHITE, true);
|
|
|
124 |
} else
|
|
|
125 |
bImg = ImageUtils.createQualityResizedImage(this.img, this.img.getWidth(null), this.img.getHeight(null), true, true, Color.WHITE, true);
|
|
|
126 |
final ByteArrayOutputStream out = new ByteArrayOutputStream(1024 * 1024);
|
|
|
127 |
try {
|
|
|
128 |
ImageIO.write(bImg, getFormat(), out);
|
|
|
129 |
} catch (IOException e) {
|
|
|
130 |
throw new IllegalStateException("unable to export " + bImg + " to " + getFormat());
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
return out.toByteArray();
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
@Override
|
|
|
137 |
public String getFormat() {
|
|
|
138 |
return "png";
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
}
|