[919] | 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 |
|
---|
[79] | 19 | package org.expeditee.items;
|
---|
| 20 |
|
---|
[601] | 21 | import java.util.List;
|
---|
[79] | 22 |
|
---|
[1102] | 23 | import org.expeditee.core.Colour;
|
---|
| 24 | import org.expeditee.core.Image;
|
---|
[601] | 25 | import org.expeditee.gui.Frame;
|
---|
| 26 | import org.expeditee.gui.FrameIO;
|
---|
[79] | 27 |
|
---|
[108] | 28 | //TODO tidy up the mess I caused with refresh during the period where I was going into XRay mode before saving
|
---|
| 29 |
|
---|
[79] | 30 | public class FrameBitmap extends FramePicture {
|
---|
[1102] | 31 | public FrameBitmap(Text source, Image image){
|
---|
| 32 | super(source, image);
|
---|
[601] | 33 | }
|
---|
[79] | 34 |
|
---|
[601] | 35 | @Override
|
---|
| 36 | public boolean refresh() {
|
---|
| 37 | assert (_source.getLink() != null);
|
---|
| 38 | Frame frame = FrameIO.LoadFrame(_source.getAbsoluteLink());
|
---|
| 39 | // if the frame cant be found just use the current image
|
---|
| 40 | if (frame == null) {
|
---|
| 41 | return false;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | List<Text> textList = frame.getBodyTextItems(false);
|
---|
| 45 | if (textList.size() == 0)
|
---|
| 46 | return false;
|
---|
| 47 | List<String> imageLines = textList.get(0).getTextList();
|
---|
| 48 | int width = 0;
|
---|
| 49 | int height = imageLines.size();
|
---|
| 50 | // Determine the image width by finding the widest line of text
|
---|
| 51 | for (String s : imageLines) {
|
---|
| 52 | if (s.length() > width)
|
---|
| 53 | width = s.length();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | if(width == 0)
|
---|
| 57 | return false;
|
---|
| 58 |
|
---|
[1102] | 59 | Image bi = Image.createImage(width, height);
|
---|
[601] | 60 | // now set the bits on the image
|
---|
[1102] | 61 | final int transparent = (new Colour(0F, 0F, 0F, 0F)).getARGB32BitPacked();
|
---|
| 62 | final int main = _source.getPaintColor().getARGB32BitPacked();
|
---|
| 63 | final Colour c = _source.getPaintColor();
|
---|
[601] | 64 | int currentColor = main;
|
---|
| 65 | int row = 0;
|
---|
| 66 | for (String s : imageLines) {
|
---|
| 67 | for (int i = 0; i < width; i++) {
|
---|
| 68 | currentColor = transparent;
|
---|
| 69 | if (i < s.length()) {
|
---|
| 70 | char currentPixel = s.charAt(i);
|
---|
| 71 | // Space is transparent as is 0
|
---|
| 72 | if (Character.isDigit(currentPixel)) {
|
---|
[1102] | 73 | int alpha = Math.round((currentPixel - '0') * (Colour.COMPONENT_MAX_VALUE / 10.0f));
|
---|
| 74 | currentColor = new Colour(c.getRed(), c.getGreen(), c
|
---|
| 75 | .getBlue(), alpha).getARGB32BitPacked();
|
---|
[601] | 76 | }else if (currentPixel != ' ') {
|
---|
| 77 | currentColor = main;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
[1102] | 80 | bi.setPixel(i, row, Colour.fromARGB32BitPacked(currentColor));
|
---|
[601] | 81 | }
|
---|
| 82 | row++;
|
---|
| 83 | }
|
---|
| 84 | _image = bi;
|
---|
| 85 |
|
---|
| 86 | return true;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | @Override
|
---|
| 90 | protected Picture createPicture() {
|
---|
[1102] | 91 | return new FrameBitmap((Text) _source.copy(), _image);
|
---|
[601] | 92 | }
|
---|
| 93 |
|
---|
| 94 | @Override
|
---|
| 95 | protected String getTagText() {
|
---|
| 96 | return "@b: ";
|
---|
| 97 | }
|
---|
[79] | 98 | }
|
---|