| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2008 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.comic; |
| 17 | |
| 18 | import java.awt.Color; |
| 19 | import java.awt.Dimension; |
| 20 | import java.awt.image.RenderedImage; |
| 21 | import java.io.File; |
| 22 | |
| 23 | import net.sf.jomic.tools.ImageTools; |
| 24 | import net.sf.jomic.tools.LocaleTools; |
| 25 | import net.sf.wraplog.Logger; |
| 26 | |
| 27 | /** |
| 28 | * Image for one comic page. <p> |
| 29 | * |
| 30 | * The actual image data are not kept in memory, but can be obtained in demand using <code>getImage()</code> |
| 31 | * . |
| 32 | * |
| 33 | * @author Thomas Aglassinger |
| 34 | */ |
| 35 | public class ComicImage |
| 36 | { |
| 37 | private static final int DEFAULT_BROKEN_IMAGE_HEIGHT = 600; |
| 38 | private static final int DEFAULT_BROKEN_IMAGE_WIDTH = 400; |
| 39 | |
| 40 | private File file; |
| 41 | private int height; |
| 42 | |
| 43 | /** |
| 44 | * Error occured when loading the image file the first time, <code>null</code> if image could |
| 45 | * be loaded without problems. |
| 46 | */ |
| 47 | private Throwable imageError; |
| 48 | private ImageTools imageTools; |
| 49 | private LocaleTools localeTools; |
| 50 | private Logger logger; |
| 51 | private String name; |
| 52 | private int width; |
| 53 | |
| 54 | /** |
| 55 | * Creates a new comic image from <code>newFile</code>. |
| 56 | * |
| 57 | * @param newFile file that contains the image in any format supported by ImageIO. |
| 58 | */ |
| 59 | public ComicImage(File newFile) { |
| 60 | assert newFile != null; |
| 61 | assert !newFile.isDirectory(); |
| 62 | |
| 63 | imageTools = ImageTools.instance(); |
| 64 | localeTools = LocaleTools.instance(); |
| 65 | logger = Logger.getLogger(ComicImage.class); |
| 66 | |
| 67 | file = newFile; |
| 68 | name = file.getName(); |
| 69 | |
| 70 | try { |
| 71 | Dimension dimension = imageTools.getImageDimension(file); |
| 72 | |
| 73 | width = dimension.width; |
| 74 | height = dimension.height; |
| 75 | } catch (Exception error) { |
| 76 | String message = localeTools.getMessage( |
| 77 | "errors.cannotGetDimensionFromImageFile", file); |
| 78 | |
| 79 | setError(new ComicException(message, error)); |
| 80 | } |
| 81 | |
| 82 | assert file != null; |
| 83 | assert name != null; |
| 84 | } |
| 85 | |
| 86 | void setError(Throwable newError) { |
| 87 | assert newError != null; |
| 88 | imageError = newError; |
| 89 | if ((width == 0) || (height == 0)) { |
| 90 | width = DEFAULT_BROKEN_IMAGE_WIDTH; |
| 91 | height = DEFAULT_BROKEN_IMAGE_HEIGHT; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the error that occured during getting the size or reading the image. |
| 97 | */ |
| 98 | public Throwable getError() { |
| 99 | assert imageError != null; |
| 100 | return imageError; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get the file from which the image was read. |
| 105 | */ |
| 106 | public File getFile() { |
| 107 | return file; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get the height of the image in pixels. |
| 112 | */ |
| 113 | public int getHeight() { |
| 114 | return height; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get image for current page, read from file in cache. |
| 119 | */ |
| 120 | public RenderedImage getImage() { |
| 121 | RenderedImage result = null; |
| 122 | |
| 123 | if (imageError == null) { |
| 124 | try { |
| 125 | ComicCache comicCache = ComicCache.instance(); |
| 126 | |
| 127 | result = comicCache.getImage(getFile()); |
| 128 | } catch (Throwable error) { |
| 129 | logger.debug("cannot get image: " + getFile(), error); |
| 130 | setError(error); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | assert getWidth() > 0; |
| 135 | assert getHeight() > 0; |
| 136 | |
| 137 | if (result == null) { |
| 138 | result = ImageTools.instance().createBrokenImage(getWidth(), getHeight(), Color.WHITE, Color.LIGHT_GRAY); |
| 139 | } |
| 140 | assert result != null; |
| 141 | |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Original name of the image as used in the archive (for example "page_17.jpg"). |
| 147 | */ |
| 148 | public String getName() { |
| 149 | return name; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get the width of the image in pixels. |
| 154 | */ |
| 155 | public int getWidth() { |
| 156 | return width; |
| 157 | } |
| 158 | |
| 159 | public boolean isDoublePage() { |
| 160 | return imageTools.isLandscape(getWidth(), getHeight()); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Did an error occur during getting the size or reading the image? |
| 165 | */ |
| 166 | public boolean hasError() { |
| 167 | return imageError != null; |
| 168 | } |
| 169 | } |