Skip to content

Commit

Permalink
Added back support for ICO files by using TwelveMonkeys ImageIO GMP p…
Browse files Browse the repository at this point in the history
…lugin

- added TwelveMonkeys ImageIO library for ICO formats
- added Apache Aries SPI Fly to mediate the publishing and consumption
  of ImageReaderSpi implementations
- added the ImageioSpiRegistration to inject discovered SPI instances
  into the ImageIO registry (did not get the dynamic weaving framework
  extension to work inside the application)
- when processing an ICO file, pick the best resolution given the
  maximum constraints
  • Loading branch information
buchen committed Dec 13, 2023
1 parent 7c44ec7 commit 5aac558
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 12 deletions.
14 changes: 14 additions & 0 deletions name.abuchen.portfolio.feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,18 @@ For purposes of the EPL, "Program" will mean the Content.
version="0.0.0"
unpack="false"/>

<plugin
id="com.twelvemonkeys.imageio.bmp"
download-size="0"
install-size="0"
version="0.0.0"
unpack="false"/>

<plugin
id="org.apache.aries.spifly.dynamic.bundle"
download-size="0"
install-size="0"
version="0.0.0"
unpack="false"/>

</feature>
5 changes: 5 additions & 0 deletions name.abuchen.portfolio/.project
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ds.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
Expand Down
6 changes: 5 additions & 1 deletion name.abuchen.portfolio/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ Import-Package: com.google.common.annotations,
org.jsoup,
org.jsoup.nodes,
org.jsoup.safety,
org.jsoup.select
org.jsoup.select,
org.osgi.service.component.annotations
Bundle-ClassPath: .
Bundle-Vendor: %Bundle-Vendor
Require-Bundle: org.eclipse.core.runtime,
Expand All @@ -73,3 +74,6 @@ Require-Bundle: org.eclipse.core.runtime,
json-path,
com.google.gson
Automatic-Module-Name: name.abuchen.portfolio
Bundle-ActivationPolicy: lazy
Service-Component: OSGI-INF/name.abuchen.portfolio.util.ImageioSpiRegistration.xml
Require-Capability: osgi.extender;filter:="(osgi.extender=osgi.serviceloader.processor)",osgi.serviceloader;filter:="(osgi.serviceloader=javax.imageio.spi.ImageReaderSpi)";cardinality:=multiple
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="name.abuchen.portfolio.util.ImageioSpiRegistration">
<reference bind="addImageReaderSpi" cardinality="0..n" interface="javax.imageio.spi.ImageReaderSpi" name="javax.imageio.spi.ImageReaderSpi" policy="dynamic"/>
<implementation class="name.abuchen.portfolio.util.ImageioSpiRegistration"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package name.abuchen.portfolio.util;

import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageReaderSpi;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;

@Component
public class ImageioSpiRegistration
{
@Reference(cardinality = ReferenceCardinality.MULTIPLE)
public void addImageReaderSpi(ImageReaderSpi readerProvider)
{
IIORegistry.getDefaultInstance().registerServiceProvider(readerProvider);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
Expand Down Expand Up @@ -109,6 +110,8 @@ public Image toImage(String value, int logicalWidth, int logicalHeight)
return null;

BufferedImage imgData = ImageIO.read(new ByteArrayInputStream(buff));
if (imgData == null)
return null;

return new Image(null, new ZoomingImageDataProvider(imgData, logicalWidth, logicalHeight));
}
Expand All @@ -130,14 +133,59 @@ private static byte[] encode(BufferedImage image) throws IOException
@Override
public String loadAndPrepare(String filename, int maxWidth, int maxHeight) throws IOException
{
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("ICO");
while (readers.hasNext()) {
System.out.println("reader: " + readers.next());
BufferedImage imgData = null;

try (ImageInputStream input = ImageIO.createImageInputStream(new File(filename)))
{
Iterator<ImageReader> readers = ImageIO.getImageReaders(input);

if (!readers.hasNext())
return null;

ImageReader reader = readers.next();

try
{
reader.setInput(input);
if (reader.getFormatName().equalsIgnoreCase("ico")) //$NON-NLS-1$
{
// ico files have multiple resolutions. We pick the largest
// one with the best pixelSize that fits into maxWidth and
// maxHeight
var numImages = reader.getNumImages(true);
if (numImages <= 0)
return null;

int size = 0;
int pixelSize = 0;

for (int ii = 0; ii < numImages; ii++)
{
BufferedImage icon = reader.read(ii);
if ((icon.getWidth() > size
|| (icon.getWidth() == size && icon.getColorModel().getPixelSize() > pixelSize))
&& icon.getWidth() <= maxWidth)
{
imgData = icon;
size = icon.getWidth();
pixelSize = icon.getColorModel().getPixelSize();
}
}
}
else
{
imgData = reader.read(0);
}

}
finally
{
reader.dispose();
}
}

BufferedImage imgData = ImageIO.read(new File(filename));

if (imgData == null)
throw new IOException("Image format not supported.");
return null;

if (imgData.getWidth() > maxWidth || imgData.getHeight() > maxHeight)
{
Expand Down
7 changes: 7 additions & 0 deletions portfolio-app/eclipse/launches.lc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ eclipse configuration PortfolioPerformance {
feature org.eclipse.nebula.cwt.feature;
feature org.eclipse.nebula.widgets.cdatetime.feature;
plugin org.apache.felix.scr;
plugin org.apache.aries.spifly.dynamic.bundle;
plugin com.twelvemonkeys.imageio.bmp autostart;

ignore name.abuchen.portfolio.junit;
ignore name.abuchen.portfolio.tests;
Expand Down Expand Up @@ -57,6 +59,11 @@ abstract junit-plugin configuration TestBase {
feature org.eclipse.nebula.cwt.feature;
feature org.eclipse.nebula.widgets.cdatetime.feature;
plugin org.apache.felix.scr;

plugin name.abuchen.portfolio.junit;
plugin org.objenesis;
plugin org.mockito.mockito-core;
plugin net.bytebuddy.byte-buddy;

ignore org.eclipse.jdt.annotation;
ignore org.eclipse.ui;
Expand Down
6 changes: 4 additions & 2 deletions portfolio-product/name.abuchen.portfolio.distro.product
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@
</features>

<configurations>
<plugin id="name.abuchen.portfolio.ui" autoStart="false" startLevel="5" />
<plugin id="org.apache.felix.scr" autoStart="true" startLevel="2" />
<plugin id="name.abuchen.portfolio.ui" autoStart="false" startLevel="4" />
<plugin id="com.twelvemonkeys.imageio.bmp" autoStart="true" startLevel="4"/>
<plugin id="org.eclipse.core.runtime" autoStart="true" startLevel="4" />
<plugin id="org.apache.felix.scr" autoStart="true" startLevel="2" />
<plugin id="org.eclipse.equinox.common" autoStart="true" startLevel="2" />
<plugin id="org.eclipse.equinox.simpleconfigurator" autoStart="true" startLevel="1" />
<plugin id="org.apache.aries.spifly.dynamic.bundle" autoStart="true" startLevel="1" />
<plugin id="org.eclipse.osgi" autoStart="true" startLevel="-1" />
<property name="osgi.instance.area.default" value="$LOCALAPPDATA$/PortfolioPerformance/workspace" os="win32" />
<property name="osgi.instance.area.default" value="@user.home/Library/Application Support/name.abuchen.portfolio.product/workspace" os="macosx" />
Expand Down
6 changes: 4 additions & 2 deletions portfolio-product/name.abuchen.portfolio.product
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@
</features>

<configurations>
<plugin id="name.abuchen.portfolio.ui" autoStart="false" startLevel="5" />
<plugin id="org.apache.felix.scr" autoStart="true" startLevel="2" />
<plugin id="name.abuchen.portfolio.ui" autoStart="false" startLevel="4" />
<plugin id="com.twelvemonkeys.imageio.bmp" autoStart="true" startLevel="4"/>
<plugin id="org.eclipse.core.runtime" autoStart="true" startLevel="4" />
<plugin id="org.apache.felix.scr" autoStart="true" startLevel="2" />
<plugin id="org.eclipse.equinox.common" autoStart="true" startLevel="2" />
<plugin id="org.eclipse.equinox.simpleconfigurator" autoStart="true" startLevel="1" />
<plugin id="org.apache.aries.spifly.dynamic.bundle" autoStart="true" startLevel="1" />
<plugin id="org.eclipse.osgi" autoStart="true" startLevel="-1" />
<property name="osgi.instance.area.default" value="$LOCALAPPDATA$/PortfolioPerformance/workspace" os="win32" />
<property name="osgi.instance.area.default" value="@user.home/Library/Application Support/name.abuchen.portfolio.product/workspace" os="macosx" />
Expand Down
79 changes: 78 additions & 1 deletion portfolio-target-definition/portfolio-target-definition.target
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<unit id="org.hamcrest.library.source" version="1.3.0.v20180524-2246"/>
<unit id="org.mockito.mockito-core" version="4.8.1.v20221103-2317"/>
<unit id="org.mockito.mockito-core.source" version="4.8.1.v20221103-2317"/>
<unit id="org.objectweb.asm" version="9.4.0.v20221107-1714"/>
</location>
<location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="planner" includeSource="true" type="InstallableUnit">
<repository location="https://download.eclipse.org/nebula/releases/2.5.0"/>
Expand Down Expand Up @@ -228,5 +227,83 @@
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" label="TwelveMonkey ImageIO" missingManifest="generate" type="Maven">
<dependencies>
<dependency>
<groupId>com.twelvemonkeys.common</groupId>
<artifactId>common-image</artifactId>
<version>3.10.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.common</groupId>
<artifactId>common-io</artifactId>
<version>3.10.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.common</groupId>
<artifactId>common-lang</artifactId>
<version>3.10.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-bmp</artifactId>
<version>3.10.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-core</artifactId>
<version>3.10.1</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" label="Apache Aries" missingManifest="generate" type="Maven">
<dependencies>
<dependency>
<groupId>org.apache.aries.spifly</groupId>
<artifactId>org.apache.aries.spifly.dynamic.bundle</artifactId>
<version>1.3.7</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" label="org.objectweb.asm" missingManifest="generate" type="Maven">
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-analysis</artifactId>
<version>9.6</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>9.6</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-tree</artifactId>
<version>9.6</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-util</artifactId>
<version>9.6</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.6</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
</locations>
</target>

0 comments on commit 5aac558

Please sign in to comment.