| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 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 | |
| 26 | import org.apache.commons.logging.Log; |
| 27 | import org.apache.commons.logging.LogFactory; |
| 28 | |
| 29 | /** |
| 30 | * Image for one comic page. <p> |
| 31 | * |
| 32 | * The actual image data are not kept in memory, but can be obtained in demand using <code>getImage()</code> |
| 33 | * . |
| 34 | * |
| 35 | * @author Thomas Aglassinger |
| 36 | */ |
| 37 | public class ComicImage |
| 38 | { |
| 39 | private static final int DEFAULT_BROKEN_IMAGE_HEIGHT = 600; |
| 40 | private static final int DEFAULT_BROKEN_IMAGE_WIDTH = 400; |
| 41 | |
| 42 | private File file; |
| 43 | private int height; |
| 44 | |
| 45 | /** |
| 46 | * Error occurred when loading the image file the first time, <code>null</code> if image could |
| 47 | * be loaded without problems. |
| 48 | */ |
| 49 | private Throwable imageError; |
| 50 | private ImageTools imageTools; |
| 51 | private LocaleTools localeTools; |
| 52 | private Log logger; |
| 53 | private String name; |
| 54 | private int width; |
| 55 | |
| 56 | /** |
| 57 | * Creates a new comic image from <code>newFile</code>. |
| 58 | * |
| 59 | * @param newFile file that contains the image in any format supported by ImageIO. |
| 60 | */ |
| 61 | public ComicImage(File newFile) { |
| 62 | assert newFile != null; |
| 63 | assert !newFile.isDirectory(); |
| 64 | |
| 65 | imageTools = ImageTools.instance(); |
| 66 | localeTools = LocaleTools.instance(); |
| 67 | logger = LogFactory.getLog(ComicImage.class); |
| 68 | |
| 69 | file = newFile; |
| 70 | name = file.getName(); |
| 71 | |
| 72 | try { |
| 73 | Dimension dimension = imageTools.getImageDimension(file); |
| 74 | |
| 75 | width = dimension.width; |
| 76 | height = dimension.height; |
| 77 | } catch (Exception error) { |
| 78 | String message = localeTools.getMessage( |
| 79 | "errors.cannotGetDimensionFromImageFile", file); |
| 80 | |
| 81 | setError(new ComicException(message, error)); |
| 82 | } |
| 83 | |
| 84 | assert file != null; |
| 85 | assert name != null; |
| 86 | } |
| 87 | |
| 88 | void setError(Throwable newError) { |
| 89 | assert newError != null; |
| 90 | imageError = newError; |
| 91 | if ((width == 0) || (height == 0)) { |
| 92 | width = DEFAULT_BROKEN_IMAGE_WIDTH; |
| 93 | height = DEFAULT_BROKEN_IMAGE_HEIGHT; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get the error that occured during getting the size or reading the image. |
| 99 | */ |
| 100 | public Throwable getError() { |
| 101 | assert imageError != null; |
| 102 | return imageError; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get the file from which the image was read. |
| 107 | */ |
| 108 | public File getFile() { |
| 109 | return file; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the height of the image in pixels. |
| 114 | */ |
| 115 | public int getHeight() { |
| 116 | return height; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get image for current page, read from file in cache. |
| 121 | */ |
| 122 | public RenderedImage getImage() { |
| 123 | RenderedImage result = null; |
| 124 | |
| 125 | if (imageError == null) { |
| 126 | try { |
| 127 | ComicCache comicCache = ComicCache.instance(); |
| 128 | |
| 129 | result = comicCache.getImage(getFile()); |
| 130 | } catch (Throwable error) { |
| 131 | logger.debug("cannot get image: " + getFile(), error); |
| 132 | setError(error); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | assert getWidth() > 0; |
| 137 | assert getHeight() > 0; |
| 138 | |
| 139 | if (result == null) { |
| 140 | result = ImageTools.instance().createBrokenImage(getWidth(), getHeight(), Color.WHITE, Color.LIGHT_GRAY); |
| 141 | } |
| 142 | assert result != null; |
| 143 | |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Original name of the image as used in the archive (for example "page_17.jpg"). |
| 149 | */ |
| 150 | public String getName() { |
| 151 | return name; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get the width of the image in pixels. |
| 156 | */ |
| 157 | public int getWidth() { |
| 158 | return width; |
| 159 | } |
| 160 | |
| 161 | public boolean isDoublePage() { |
| 162 | return imageTools.isLandscape(getWidth(), getHeight()); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Did an error occur during getting the size or reading the image? |
| 167 | */ |
| 168 | public boolean hasError() { |
| 169 | return imageError != null; |
| 170 | } |
| 171 | } |