2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012 -- 2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.meviz.htmlindexer;
12 import eu.svjatoslav.meviz.htmlindexer.layouts.Layout;
13 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
14 import org.w3c.dom.NamedNodeMap;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
18 import javax.imageio.ImageReader;
19 import javax.imageio.metadata.IIOMetadata;
20 import javax.imageio.metadata.IIOMetadataNode;
22 import java.awt.image.BufferedImage;
23 import java.io.BufferedReader;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.zip.CRC32;
31 import static org.openimaj.image.ImageUtilities.createBufferedImage;
32 import static org.openimaj.image.ImageUtilities.readMBF;
35 private static File lastLoadedFile;
36 private static BufferedImage lastLoadedBufferedImage;
39 * Load image into {@link BufferedImage} and return it. Caches last loaded
40 * image to speed up subsequent loading attempts.
42 * @throws ImageFormatError
45 public static BufferedImage getBufferedImage(final File file)
46 throws ImageFormatError, IOException {
47 if (file.equals(lastLoadedFile))
48 return lastLoadedBufferedImage;
50 lastLoadedBufferedImage = createBufferedImage(readMBF(file));
51 lastLoadedFile = file;
53 if (lastLoadedBufferedImage == null) {
54 System.out.println("Error reading image: " + file);
55 throw new ImageFormatError("File: " + file
56 + " is not a valid image.");
60 return lastLoadedBufferedImage;
63 public static File getLayoutIndexFile(final Layout layout,
64 final File directoryToIndex) {
66 final String indexFilePath = directoryToIndex.getAbsolutePath()
67 + "/index" + layout.getFileNameSuffix() + ".html";
69 return new File(indexFilePath);
72 public static HashSet<Layout> getLayouts() {
73 final HashSet<Layout> layouts = new HashSet<>();
74 layouts.add(new MixedLayout());
78 public static String getStringCrcAsHex(final String input) {
80 // create a new CRC-calculating object
81 final CRC32 crc = new CRC32();
83 // loop, calculating CRC for each byte of the string
84 // There is no CRC16.update(byte[]) method.
85 for (final byte b : input.getBytes())
88 // note use crc.value, not crc.getValue()
90 return Integer.toHexString((int) crc.getValue())
94 public static File getThumbnailsDirectory(final File directoryToIndex) {
95 return new File(getThumbnailsDirectoryPath(directoryToIndex));
98 public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
99 return directoryToIndex.getAbsolutePath() + "/"
100 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
103 public static boolean isMevizGeneratedIndexFile(final File indexFile)
106 boolean isMevizFile = false;
108 final FileReader fileReader = new FileReader(indexFile);
109 final BufferedReader reader = new BufferedReader(fileReader);
114 final String line = reader.readLine();
119 if (line.contains(Constants.HTML_MAGIC_STRING)) {
132 * TODO: URL path component is encoded differently from URL query parameter.
133 * Also some URL encoding might work for HTML on local filesystem, while other
134 * stuff works for web. Things must be cleared up here. Currently they are mixed and
137 public static String urlEncode(String string) {
138 if (string.startsWith("./"))
139 string = string.substring(2);
141 // TODO: get rid of UrlParamEncoder.
142 return UrlParamEncoder.encode(string);
145 // return URLEncoder.encode(string, UTF_8).replace("+", "%20");
146 // } catch (UnsupportedEncodingException e) {
147 // throw new RuntimeException(e);
151 public static ImageFrame[] readGIF(ImageReader reader) throws IOException {
152 ArrayList<ImageFrame> frames = new ArrayList<ImageFrame>(2);
157 IIOMetadata metadata = reader.getStreamMetadata();
158 if (metadata != null) {
159 IIOMetadataNode globalRoot = (IIOMetadataNode) metadata.getAsTree(metadata.getNativeMetadataFormatName());
161 NodeList globalScreenDescriptor = globalRoot.getElementsByTagName("LogicalScreenDescriptor");
163 if (globalScreenDescriptor != null && globalScreenDescriptor.getLength() > 0) {
164 IIOMetadataNode screenDescriptor = (IIOMetadataNode) globalScreenDescriptor.item(0);
166 if (screenDescriptor != null) {
167 width = Integer.parseInt(screenDescriptor.getAttribute("logicalScreenWidth"));
168 height = Integer.parseInt(screenDescriptor.getAttribute("logicalScreenHeight"));
173 BufferedImage master = null;
174 Graphics2D masterGraphics = null;
176 for (int frameIndex = 0;; frameIndex++) {
179 image = reader.read(frameIndex);
180 } catch (IndexOutOfBoundsException io) {
184 if (width == -1 || height == -1) {
185 width = image.getWidth();
186 height = image.getHeight();
189 IIOMetadataNode root = (IIOMetadataNode) reader.getImageMetadata(frameIndex).getAsTree("javax_imageio_gif_image_1.0");
190 IIOMetadataNode gce = (IIOMetadataNode) root.getElementsByTagName("GraphicControlExtension").item(0);
191 int delay = Integer.valueOf(gce.getAttribute("delayTime"));
192 String disposal = gce.getAttribute("disposalMethod");
197 if (master == null) {
198 master = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
199 masterGraphics = master.createGraphics();
200 masterGraphics.setBackground(new Color(0, 0, 0, 0));
202 NodeList children = root.getChildNodes();
203 for (int nodeIndex = 0; nodeIndex < children.getLength(); nodeIndex++) {
204 Node nodeItem = children.item(nodeIndex);
205 if (nodeItem.getNodeName().equals("ImageDescriptor")) {
206 NamedNodeMap map = nodeItem.getAttributes();
207 x = Integer.valueOf(map.getNamedItem("imageLeftPosition").getNodeValue());
208 y = Integer.valueOf(map.getNamedItem("imageTopPosition").getNodeValue());
212 masterGraphics.drawImage(image, x, y, null);
214 BufferedImage copy = new BufferedImage(master.getColorModel(), master.copyData(null), master.isAlphaPremultiplied(), null);
215 frames.add(new ImageFrame(copy, delay, disposal));
217 if (disposal.equals("restoreToPrevious")) {
218 BufferedImage from = null;
219 for (int i = frameIndex - 1; i >= 0; i--) {
220 if (!frames.get(i).getDisposal().equals("restoreToPrevious") || frameIndex == 0) {
221 from = frames.get(i).image;
226 master = new BufferedImage(from.getColorModel(), from.copyData(null), from.isAlphaPremultiplied(), null);
227 masterGraphics = master.createGraphics();
228 masterGraphics.setBackground(new Color(0, 0, 0, 0));
229 } else if (disposal.equals("restoreToBackgroundColor")) {
230 masterGraphics.clearRect(x, y, image.getWidth(), image.getHeight());
235 return frames.toArray(new ImageFrame[frames.size()]);
238 public static class ImageFrame {
239 private final int delay;
240 public BufferedImage image;
241 private final String disposal;
243 public ImageFrame(BufferedImage image, int delay, String disposal) {
246 this.disposal = disposal;
249 public int getDelay() {
253 public String getDisposal() {