| 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.image.RenderedImage; |
| 19 | import java.io.File; |
| 20 | |
| 21 | /** |
| 22 | * Entry in ImageCache. |
| 23 | * |
| 24 | * @see net.sf.jomic.tools.ImageCache |
| 25 | * @author Thomas Aglassinger |
| 26 | */ |
| 27 | public class ImageCacheEntry |
| 28 | { |
| 29 | private boolean disposed; |
| 30 | private RenderedImage image; |
| 31 | private File imageFile; |
| 32 | private long lastAccessed; |
| 33 | |
| 34 | public ImageCacheEntry(File newImageFile, RenderedImage newImage) { |
| 35 | assert newImageFile != null; |
| 36 | assert newImage != null; |
| 37 | image = newImage; |
| 38 | imageFile = newImageFile; |
| 39 | updateLastAccessed(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get the actual image data from memory. |
| 44 | */ |
| 45 | public RenderedImage getImage() { |
| 46 | assert !disposed; |
| 47 | return image; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get the file from which the image data where read. |
| 52 | */ |
| 53 | public File getImageFile() { |
| 54 | assert !disposed; |
| 55 | return imageFile; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get the approximate size the image uses in memory. |
| 60 | */ |
| 61 | // TODO: Figure out how to compute the memory size of tiled images. |
| 62 | public long getImageSize() { |
| 63 | assert !disposed; |
| 64 | int width = image.getWidth(); |
| 65 | int height = image.getHeight(); |
| 66 | int depth = image.getSampleModel().getNumBands(); |
| 67 | // Note: multiplying by 2 seems to consider the overhead somewhat properly. |
| 68 | long result = 2 * ((long) width) * height * depth; |
| 69 | |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get timestamp when entry was last accessed. |
| 75 | */ |
| 76 | public long getLastAccessed() { |
| 77 | assert !disposed; |
| 78 | return lastAccessed; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Dispose entry by removing the reference to the source image, thus allowing the garbage |
| 83 | * collector to release the memory allocated by it. After calling this method, the entry must |
| 84 | * not be used anymore. |
| 85 | */ |
| 86 | public void dispose() { |
| 87 | assert !disposed; |
| 88 | image = null; |
| 89 | imageFile = null; |
| 90 | disposed = true; |
| 91 | } |
| 92 | |
| 93 | void updateLastAccessed() { |
| 94 | assert !disposed; |
| 95 | lastAccessed = System.currentTimeMillis(); |
| 96 | } |
| 97 | } |