ru.olegcherednik.icoman.ico.IcoFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of icon-manager Show documentation
Show all versions of icon-manager Show documentation
Icon Manager (ico, icl, icns).
The newest version!
package ru.olegcherednik.icoman.ico;
import ru.olegcherednik.icoman.AbstractIconFile;
import ru.olegcherednik.icoman.IconIO;
import ru.olegcherednik.icoman.exceptions.IconManagerException;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.stream.ImageInputStream;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* @author Oleg Cherednik
* @since 03.07.2013
*/
@Slf4j
public final class IcoFile extends AbstractIconFile {
public IcoFile(ImageInputStream in) throws IOException, IconManagerException {
super(createImageById(new FileHeader(in), in));
}
// ========== static ==========
private static List readImageHeaders(int total, ImageInputStream in) throws IOException {
assert total > 0;
assert in != null;
List headers = new ArrayList<>(total);
for (int pos = 0; pos < total; pos++)
headers.add(ImageHeader.read(pos, in));
return Collections.unmodifiableList(headers);
}
private static Map createImageById(FileHeader fileHeader, ImageInputStream in) throws IOException, IconManagerException {
List imageHeaders = readImageHeaders(fileHeader.getImageCount(), in);
Map imageByHeader = new TreeMap<>();
int offs = FileHeader.SIZE + imageHeaders.size() * ImageHeader.SIZE;
for (ImageHeader imageHeader : imageHeaders) {
checkOffs(offs, imageHeader);
BufferedImage image = IconIO.readImage(in, imageHeader.getSize());
if (image == null)
log.error("Image '{}' cannot be read", imageHeader);
else
imageByHeader.put(imageHeader, image);
offs += imageHeader.getSize();
}
if (imageByHeader.isEmpty())
return Collections.emptyMap();
Map imageById = new LinkedHashMap<>();
imageByHeader.entrySet().forEach(entry -> imageById.put(entry.getKey().getId(), entry.getValue()));
return imageById;
}
private static void checkOffs(int expected, ImageHeader imageHeader) throws IconManagerException {
if (expected != imageHeader.getOffs())
throw new IconManagerException("rva image no. " + imageHeader.getPos() + " incorrect. actual=" +
imageHeader.getOffs() + ", expected=" + expected);
}
}