| 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.tools; |
| 17 | |
| 18 | import java.awt.Dimension; |
| 19 | import java.io.File; |
| 20 | |
| 21 | import javax.imageio.stream.ImageInputStream; |
| 22 | |
| 23 | /** |
| 24 | * Information about size and format of an image. During construction, this information is obtained |
| 25 | * and later available via various getters. However, if the information cannot be obtained, <code>hasError()</code> |
| 26 | * is <code>true</code> and most getters won't be available. An exception of this is <code>getError()</code> |
| 27 | * which returs details about why the image information could not be obtained. |
| 28 | * |
| 29 | * @see #hasError() |
| 30 | * @see #getError() |
| 31 | * @author Thomas Aglassinger |
| 32 | */ |
| 33 | public class ImageInfo |
| 34 | { |
| 35 | private Throwable error; |
| 36 | private File file; |
| 37 | private String format; |
| 38 | private Dimension size; |
| 39 | |
| 40 | public ImageInfo(File newFile) { |
| 41 | this(); |
| 42 | assert newFile != null; |
| 43 | file = newFile; |
| 44 | |
| 45 | ImageTools imageTools = ImageTools.instance(); |
| 46 | |
| 47 | try { |
| 48 | ImageInputStream imageStream = imageTools.createImageInputStream(file); |
| 49 | |
| 50 | try { |
| 51 | format = imageTools.getImageFormat(imageStream); |
| 52 | size = imageTools.getImageDimension(imageStream); |
| 53 | } finally { |
| 54 | imageStream.close(); |
| 55 | } |
| 56 | } catch (Throwable newError) { |
| 57 | error = newError; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private ImageInfo() { |
| 62 | super(); |
| 63 | } |
| 64 | |
| 65 | public Throwable getError() { |
| 66 | assert hasError(); |
| 67 | return error; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Return the file the image information was obtained from. This information is available even |
| 72 | * if <code>hasError()</code> is <code>true</code>. |
| 73 | */ |
| 74 | public File getFile() { |
| 75 | return file; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the name of the format of the image, for example "jpeg". This is the format according to |
| 80 | * ImageIO, not the file suffix. |
| 81 | */ |
| 82 | public String getFormat() { |
| 83 | assert !hasError(); |
| 84 | return format; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get the height of the image in pixels. |
| 89 | */ |
| 90 | public int getHeight() { |
| 91 | assert !hasError(); |
| 92 | return size.height; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the size of the image in pixels. |
| 97 | */ |
| 98 | public Dimension getSize() { |
| 99 | assert !hasError(); |
| 100 | return size; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get the width of the image in pixels. |
| 105 | */ |
| 106 | public int getWidth() { |
| 107 | assert !hasError(); |
| 108 | return size.width; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Return <code>true</code> if the image is definitely broken, in other words either the format |
| 113 | * or the size could not be obtained. |
| 114 | */ |
| 115 | public boolean hasError() { |
| 116 | return error != null; |
| 117 | } |
| 118 | } |