1 | /**
|
---|
2 | * FrameBitmap.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 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 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, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.items;
|
---|
20 |
|
---|
21 | import java.awt.Color;
|
---|
22 | import java.awt.Image;
|
---|
23 | import java.awt.image.BufferedImage;
|
---|
24 | import java.awt.image.ImageObserver;
|
---|
25 | import java.util.List;
|
---|
26 |
|
---|
27 | import org.expeditee.gui.Frame;
|
---|
28 | import org.expeditee.gui.FrameIO;
|
---|
29 |
|
---|
30 | //TODO tidy up the mess I caused with refresh during the period where I was going into XRay mode before saving
|
---|
31 |
|
---|
32 | public class FrameBitmap extends FramePicture {
|
---|
33 | public FrameBitmap(Text source, ImageObserver observer, Image image){
|
---|
34 | super(source, observer, image);
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public boolean refresh() {
|
---|
39 | assert (_source.getLink() != null);
|
---|
40 | Frame frame = FrameIO.LoadFrame(_source.getAbsoluteLink());
|
---|
41 | // if the frame cant be found just use the current image
|
---|
42 | if (frame == null) {
|
---|
43 | return false;
|
---|
44 | }
|
---|
45 |
|
---|
46 | List<Text> textList = frame.getBodyTextItems(false);
|
---|
47 | if (textList.size() == 0)
|
---|
48 | return false;
|
---|
49 | List<String> imageLines = textList.get(0).getTextList();
|
---|
50 | int width = 0;
|
---|
51 | int height = imageLines.size();
|
---|
52 | // Determine the image width by finding the widest line of text
|
---|
53 | for (String s : imageLines) {
|
---|
54 | if (s.length() > width)
|
---|
55 | width = s.length();
|
---|
56 | }
|
---|
57 |
|
---|
58 | if(width == 0)
|
---|
59 | return false;
|
---|
60 |
|
---|
61 | BufferedImage bi = new BufferedImage(width, height,
|
---|
62 | BufferedImage.TYPE_INT_ARGB);
|
---|
63 | // now set the bits on the image
|
---|
64 | final int transparent = (new Color(0F, 0F, 0F, 0F)).getRGB();
|
---|
65 | final int main = _source.getPaintColor().getRGB();
|
---|
66 | final Color c = _source.getPaintColor();
|
---|
67 | int currentColor = main;
|
---|
68 | int row = 0;
|
---|
69 | for (String s : imageLines) {
|
---|
70 | for (int i = 0; i < width; i++) {
|
---|
71 | currentColor = transparent;
|
---|
72 | if (i < s.length()) {
|
---|
73 | char currentPixel = s.charAt(i);
|
---|
74 | // Space is transparent as is 0
|
---|
75 | if (Character.isDigit(currentPixel)) {
|
---|
76 | int alpha = Math.round((currentPixel - '0') * 25.5F);
|
---|
77 | currentColor = new Color(c.getRed(), c.getGreen(), c
|
---|
78 | .getBlue(), alpha).getRGB();
|
---|
79 | }else if (currentPixel != ' ') {
|
---|
80 | currentColor = main;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | bi.setRGB(i, row, currentColor);
|
---|
84 | }
|
---|
85 | row++;
|
---|
86 | }
|
---|
87 | _image = bi;
|
---|
88 |
|
---|
89 | return true;
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | protected Picture createPicture() {
|
---|
94 | return new FrameBitmap((Text) _source.copy(),
|
---|
95 | _imageObserver, _image);
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | protected String getTagText() {
|
---|
100 | return "@b: ";
|
---|
101 | }
|
---|
102 | }
|
---|